init: downgrade ENABLE_DEBUG_INIT to just a #define to prevent people
[oweals/busybox.git] / networking / dnsd.c
index 8f01d5c06f2b0738cc28fb422deb4d2810777821..e8dcb404b059a75a90c093affcff4516e01b0b37 100644 (file)
@@ -17,8 +17,8 @@
  * the first porting of oao' scdns to busybox also.
  */
 
-#include <syslog.h>
 #include "libbb.h"
+#include <syslog.h>
 
 //#define DEBUG 1
 #define DEBUG 0
@@ -36,7 +36,7 @@ enum {
    ttl(4B) + rlen(2B) + r (MAX_NAME_LEN =21B) +
    2*querystring (2 MAX_NAME_LEN= 42B), all together 90 Byte
 */
-       MAX_PACK_LEN = 512 + 1,
+       MAX_PACK_LEN = 512,
 
        DEFAULT_TTL = 30,       // increase this when not testing?
 
@@ -44,12 +44,6 @@ enum {
        REQ_PTR = 12
 };
 
-struct dns_repl {              // resource record, add 0 or 1 to accepted dns_msg in resp
-       uint16_t rlen;
-       uint8_t *r;             // resource
-       uint16_t flags;
-};
-
 struct dns_head {              // the message from client and first part of response mag
        uint16_t id;
        uint16_t flags;
@@ -107,67 +101,38 @@ static void undot(uint8_t * rip)
        }
 }
 
-/*
- * Read one line of hostname/IP from file
- * Returns 0 for each valid entry read, -1 at EOF
- * Assumes all host names are lower case only
- * Hostnames with more than one label are not handled correctly.
- * Presently the dot is copied into name without
- * converting to a length/string substring for that label.
- */
-static int getfileentry(FILE * fp, struct dns_entry *s)
-{
-       unsigned int a,b,c,d;
-       char *line, *r, *name;
-
- restart:
-       line = r = xmalloc_fgets(fp);
-       if (!r)
-               return -1;
-       while (*r == ' ' || *r == '\t') {
-               r++;
-               if (!*r || *r == '#' || *r == '\n') {
-                       free(line);
-                       goto restart; /* skipping empty/blank and commented lines  */
-               }
-       }
-       name = r;
-       while (*r != ' ' && *r != '\t')
-               r++;
-       *r++ = '\0';
-       if (sscanf(r, ".%u.%u.%u.%u"+1, &a, &b, &c, &d) != 4) {
-               free(line);
-               goto restart; /* skipping wrong lines */
-       }
-
-       sprintf(s->ip, ".%u.%u.%u.%u"+1, a, b, c, d);
-       sprintf(s->rip, ".%u.%u.%u.%u", d, c, b, a);
-       undot((uint8_t*)s->rip);
-       convname(s->name, (uint8_t*)name);
-
-       if (OPT_verbose)
-               fprintf(stderr, "\tname:%s, ip:%s\n", &(s->name[1]),s->ip);
-
-       free(line);
-       return 0;
-}
-
 /*
  * Read hostname/IP records from file
  */
 static void dnsentryinit(void)
 {
-       FILE *fp;
+       char *token[2];
+       parser_t *parser;
        struct dns_entry *m, *prev;
 
        prev = dnsentry = NULL;
-       fp = xfopen(fileconf, "r");
+       parser = config_open(fileconf);
+       while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) {
+               unsigned a, b, c, d;
+               /*
+                * Assumes all host names are lower case only
+                * Hostnames with more than one label are not handled correctly.
+                * Presently the dot is copied into name without
+                * converting to a length/string substring for that label.
+                */
+//             if (!token[1] || sscanf(token[1], ".%u.%u.%u.%u"+1, &a, &b, &c, &d) != 4)
+               if (sscanf(token[1], ".%u.%u.%u.%u"+1, &a, &b, &c, &d) != 4)
+                       continue;
 
-       while (1) {
                m = xzalloc(sizeof(*m));
                /*m->next = NULL;*/
-               if (getfileentry(fp, m))
-                       break;
+               sprintf(m->ip, ".%u.%u.%u.%u"+1, a, b, c, d);
+               sprintf(m->rip, ".%u.%u.%u.%u", d, c, b, a);
+               undot((uint8_t*)m->rip);
+               convname(m->name, (uint8_t*)token[0]);
+
+               if (OPT_verbose)
+                       fprintf(stderr, "\tname:%s, ip:%s\n", &(m->name[1]), m->ip);
 
                if (prev == NULL)
                        dnsentry = m;
@@ -175,7 +140,7 @@ static void dnsentryinit(void)
                        prev->next = m;
                prev = m;
        }
-       fclose(fp);
+       config_close(parser);
 }
 
 /*
@@ -200,7 +165,8 @@ static int table_lookup(uint16_t type, uint8_t * as, uint8_t * qs)
                        for (i = 1; i <= (int)(d->name[0]); i++)
                                if (tolower(qs[i]) != d->name[i])
                                        break;
-                       if (i > (int)(d->name[0])) {
+                       if (i > (int)(d->name[0]) ||
+                           (d->name[0] == 1 && d->name[1] == '*')) {
                                strcpy((char *)as, d->ip);
 #if DEBUG
                                fprintf(stderr, " OK as:%s\n", as);
@@ -208,7 +174,8 @@ static int table_lookup(uint16_t type, uint8_t * as, uint8_t * qs)
                                return 0;
                        }
                } else if (type == REQ_PTR) { /* search by IP-address */
-                       if (!strncmp((char*)&d->rip[1], (char*)&qs[1], strlen(d->rip)-1)) {
+                       if ((d->name[0] != 1 || d->name[1] != '*') &&
+                           !strncmp((char*)&d->rip[1], (char*)&qs[1], strlen(d->rip)-1)) {
                                strcpy((char *)as, d->name);
                                return 0;
                        }
@@ -218,20 +185,20 @@ static int table_lookup(uint16_t type, uint8_t * as, uint8_t * qs)
        return -1;
 }
 
-
 /*
  * Decode message and generate answer
  */
-static int process_packet(uint8_t * buf)
+static int process_packet(uint8_t *buf)
 {
+       uint8_t answstr[MAX_NAME_LEN + 1];
        struct dns_head *head;
        struct dns_prop *qprop;
-       struct dns_repl outr;
-       void *next, *from, *answb;
-
-       uint8_t answstr[MAX_NAME_LEN + 1];
-       int lookup_result, type, len, packet_len;
+       uint8_t *from, *answb;
+       uint16_t outr_rlen;
+       uint16_t outr_flags;
        uint16_t flags;
+       int lookup_result, type, packet_len;
+       int querystr_len;
 
        answstr[0] = '\0';
 
@@ -247,11 +214,12 @@ static int process_packet(uint8_t * buf)
        }
 
        from = (void *)&head[1];        //  start of query string
-       next = answb = from + strlen((char *)from) + 1 + sizeof(struct dns_prop);   // where to append answer block
+//FIXME: strlen of untrusted data??!
+       querystr_len = strlen((char *)from) + 1 + sizeof(struct dns_prop);
+       answb = from + querystr_len;   // where to append answer block
 
-       outr.rlen = 0;                  // may change later
-       outr.r = NULL;
-       outr.flags = 0;
+       outr_rlen = 0;
+       outr_flags = 0;
 
        qprop = (struct dns_prop *)(answb - 4);
        type = ntohs(qprop->type);
@@ -262,7 +230,7 @@ static int process_packet(uint8_t * buf)
        }
 
        if (ntohs(qprop->class) != 1 /* class INET */ ) {
-               outr.flags = 4; /* not supported */
+               outr_flags = 4; /* not supported */
                goto empty_packet;
        }
        /* we only support standard queries */
@@ -272,71 +240,75 @@ static int process_packet(uint8_t * buf)
 
        // We have a standard query
        bb_info_msg("%s", (char *)from);
-       lookup_result = table_lookup(type, answstr, (uint8_t*)from);
+       lookup_result = table_lookup(type, answstr, from);
        if (lookup_result != 0) {
-               outr.flags = 3 | 0x0400;        //name do not exist and auth
+               outr_flags = 3 | 0x0400;        // name do not exist and auth
                goto empty_packet;
        }
        if (type == REQ_A) {    // return an address
-               struct in_addr a;
-               if (!inet_aton((char*)answstr, &a)) {//dotted dec to long conv
-                       outr.flags = 1; /* Frmt err */
+               struct in_addr a; // NB! its "struct { unsigned __long__ s_addr; }"
+               uint32_t v32;
+               if (!inet_aton((char*)answstr, &a)) { //dotted dec to long conv
+                       outr_flags = 1; /* Frmt err */
                        goto empty_packet;
                }
-               memcpy(answstr, &a.s_addr, 4);  // save before a disappears
-               outr.rlen = 4;                  // uint32_t IP
+               v32 = a.s_addr; /* in case long != int */
+               memcpy(answstr, &v32, 4);
+               outr_rlen = 4;                  // uint32_t IP
        } else
-               outr.rlen = strlen((char *)answstr) + 1;        // a host name
-       outr.r = answstr;                       // 32 bit ip or a host name
-       outr.flags |= 0x0400;                   /* authority-bit */
+               outr_rlen = strlen((char *)answstr) + 1;        // a host name
+       outr_flags |= 0x0400;                   /* authority-bit */
        // we have an answer
        head->nansw = htons(1);
 
        // copy query block to answer block
-       len = answb - from;
-       memcpy(answb, from, len);
-       next += len;
+       memcpy(answb, from, querystr_len);
+       answb += querystr_len;
 
        // and append answer rr
-       *(uint32_t *) next = htonl(ttl);
-       next += 4;
-       *(uint16_t *) next = htons(outr.rlen);
-       next += 2;
-       memcpy(next, (void *)answstr, outr.rlen);
-       next += outr.rlen;
+// FIXME: unaligned accesses??
+       *(uint32_t *) answb = htonl(ttl);
+       answb += 4;
+       *(uint16_t *) answb = htons(outr_rlen);
+       answb += 2;
+       memcpy(answb, answstr, outr_rlen);
+       answb += outr_rlen;
 
  empty_packet:
 
        flags = ntohs(head->flags);
        // clear rcode and RA, set responsebit and our new flags
-       flags |= (outr.flags & 0xff80) | 0x8000;
+       flags |= (outr_flags & 0xff80) | 0x8000;
        head->flags = htons(flags);
-       head->nauth = head->nadd = htons(0);
+       head->nauth = head->nadd = 0;
        head->nquer = htons(1);
 
-       packet_len = (uint8_t *)next - buf;
+       packet_len = answb - buf;
        return packet_len;
 }
 
 /*
  * Exit on signal
  */
-static void interrupt(int x)
+static void interrupt(int sig)
 {
        /* unlink("/var/run/dnsd.lock"); */
        bb_error_msg("interrupt, exiting\n");
-       exit(2);
+       kill_myself_with_sig(sig);
 }
 
 int dnsd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
-int dnsd_main(int argc, char **argv)
+int dnsd_main(int argc UNUSED_PARAM, char **argv)
 {
        const char *listen_interface = "0.0.0.0";
        char *sttl, *sport;
-       len_and_sockaddr *lsa;
+       len_and_sockaddr *lsa, *from, *to;
+       unsigned lsa_size;
        int udps;
        uint16_t port = 53;
-       uint8_t buf[MAX_PACK_LEN];
+       /* Paranoid sizing: querystring x2 + ttl + outr_rlen + answstr */
+       /* I'd rather see process_packet() fixed instead... */
+       uint8_t buf[MAX_PACK_LEN * 2 + 4 + 2 + (MAX_NAME_LEN+1)];
 
        getopt32(argv, "i:c:t:p:dv", &listen_interface, &fileconf, &sttl, &sport);
        //if (option_mask32 & 0x1) // -i
@@ -361,41 +333,48 @@ int dnsd_main(int argc, char **argv)
        dnsentryinit();
 
        signal(SIGINT, interrupt);
-       /* why? signal(SIGPIPE, SIG_IGN); */
-       signal(SIGHUP, SIG_IGN);
+       bb_signals(0
+               /* why? + (1 << SIGPIPE) */
+               + (1 << SIGHUP)
 #ifdef SIGTSTP
-       signal(SIGTSTP, SIG_IGN);
+               + (1 << SIGTSTP)
 #endif
 #ifdef SIGURG
-       signal(SIGURG, SIG_IGN);
+               + (1 << SIGURG)
 #endif
+               , SIG_IGN);
 
        lsa = xdotted2sockaddr(listen_interface, port);
-       udps = xsocket(lsa->sa.sa_family, SOCK_DGRAM, 0);
-       xbind(udps, &lsa->sa, lsa->len);
-       /* xlisten(udps, 50); - ?!! DGRAM sockets are never listened on I think? */
+       udps = xsocket(lsa->u.sa.sa_family, SOCK_DGRAM, 0);
+       xbind(udps, &lsa->u.sa, lsa->len);
+       socket_want_pktinfo(udps); /* needed for recv_from_to to work */
+       lsa_size = LSA_LEN_SIZE + lsa->len;
+       from = xzalloc(lsa_size);
+       to = xzalloc(lsa_size);
+
        bb_info_msg("Accepting UDP packets on %s",
-                       xmalloc_sockaddr2dotted(&lsa->sa));
+                       xmalloc_sockaddr2dotted(&lsa->u.sa));
 
        while (1) {
                int r;
-               socklen_t fromlen = lsa->len;
-// FIXME: need to get *DEST* address (to which of our addresses
-// this query was directed), and reply from the same address.
-// Or else we can exhibit usual UDP ugliness:
-// [ip1.multihomed.ip2] <=  query to ip1  <= peer
-// [ip1.multihomed.ip2] => reply from ip2 => peer (confused)
-               r = recvfrom(udps, buf, sizeof(buf), 0, &lsa->sa, &fromlen);
-               if (OPT_verbose)
-                       bb_info_msg("Got UDP packet");
-               if (r < 12 || r > 512) {
+               /* Try to get *DEST* address (to which of our addresses
+                * this query was directed), and reply from the same address.
+                * Or else we can exhibit usual UDP ugliness:
+                * [ip1.multihomed.ip2] <=  query to ip1  <= peer
+                * [ip1.multihomed.ip2] => reply from ip2 => peer (confused) */
+               memcpy(to, lsa, lsa_size);
+               r = recv_from_to(udps, buf, MAX_PACK_LEN + 1, 0, &from->u.sa, &to->u.sa, lsa->len);
+               if (r < 12 || r > MAX_PACK_LEN) {
                        bb_error_msg("invalid packet size");
                        continue;
                }
+               if (OPT_verbose)
+                       bb_info_msg("Got UDP packet");
+               buf[r] = '\0'; /* paranoia */
                r = process_packet(buf);
                if (r <= 0)
                        continue;
-               sendto(udps, buf, r, 0, &lsa->sa, fromlen);
+               send_to_from(udps, buf, r, 0, &from->u.sa, &to->u.sa, lsa->len);
        }
        return 0;
 }