fd2f421d71a85ec8926727ea47b9970b848ae6c1
[oweals/busybox.git] / networking / dnsd.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini DNS server implementation for busybox
4  *
5  * Copyright (C) 2005 Roberto A. Foglietta (me@roberto.foglietta.name)
6  * Copyright (C) 2005 Odd Arild Olsen (oao at fibula dot no)
7  * Copyright (C) 2003 Paul Sheer
8  *
9  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
10  *
11  * Odd Arild Olsen started out with the sheerdns [1] of Paul Sheer and rewrote
12  * it into a shape which I believe is both easier to understand and maintain.
13  * I also reused the input buffer for output and removed services he did not
14  * need.  [1] http://threading.2038bug.com/sheerdns/
15  *
16  * Some bugfix and minor changes was applied by Roberto A. Foglietta who made
17  * the first porting of oao' scdns to busybox also.
18  */
19
20 #include "busybox.h"
21
22 //#define DEBUG 1
23 #define DEBUG 0
24
25 enum {
26         MAX_HOST_LEN = 16,      // longest host name allowed is 15
27         IP_STRING_LEN = 18,     // .xxx.xxx.xxx.xxx\0
28
29 //must be strlen('.in-addr.arpa') larger than IP_STRING_LEN
30         MAX_NAME_LEN = (IP_STRING_LEN + 13),
31
32 /* Cannot get bigger packets than 512 per RFC1035
33    In practice this can be set considerably smaller:
34    Length of response packet is  header (12B) + 2*type(4B) + 2*class(4B) +
35    ttl(4B) + rlen(2B) + r (MAX_NAME_LEN =21B) +
36    2*querystring (2 MAX_NAME_LEN= 42B), all together 90 Byte
37 */
38         MAX_PACK_LEN = 512 + 1,
39
40         DEFAULT_TTL = 30,       // increase this when not testing?
41
42         REQ_A = 1,
43         REQ_PTR = 12
44 };
45
46 struct dns_repl {               // resource record, add 0 or 1 to accepted dns_msg in resp
47         uint16_t rlen;
48         uint8_t *r;             // resource
49         uint16_t flags;
50 };
51
52 struct dns_head {               // the message from client and first part of response mag
53         uint16_t id;
54         uint16_t flags;
55         uint16_t nquer;         // accepts 0
56         uint16_t nansw;         // 1 in response
57         uint16_t nauth;         // 0
58         uint16_t nadd;          // 0
59 };
60 struct dns_prop {
61         uint16_t type;
62         uint16_t class;
63 };
64 struct dns_entry {              // element of known name, ip address and reversed ip address
65         struct dns_entry *next;
66         char ip[IP_STRING_LEN];         // dotted decimal IP
67         char rip[IP_STRING_LEN];        // length decimal reversed IP
68         char name[MAX_HOST_LEN];
69 };
70
71 static struct dns_entry *dnsentry;
72 static uint32_t ttl = DEFAULT_TTL;
73
74 static const char *fileconf = "/etc/dnsd.conf";
75
76 // Must match getopt32 call
77 #define OPT_daemon  (option_mask32 & 0x10)
78 #define OPT_verbose (option_mask32 & 0x20)
79
80
81 /*
82  * Convert host name from C-string to dns length/string.
83  */
84 static void convname(char *a, uint8_t *q)
85 {
86         int i = (q[0] == '.') ? 0 : 1;
87         for (; i < MAX_HOST_LEN-1 && *q; i++, q++)
88                 a[i] = tolower(*q);
89         a[0] = i - 1;
90         a[i] = 0;
91 }
92
93 /*
94  * Insert length of substrings instead of dots
95  */
96 static void undot(uint8_t * rip)
97 {
98         int i = 0, s = 0;
99         while (rip[i])
100                 i++;
101         for (--i; i >= 0; i--) {
102                 if (rip[i] == '.') {
103                         rip[i] = s;
104                         s = 0;
105                 } else s++;
106         }
107 }
108
109 /*
110  * Read one line of hostname/IP from file
111  * Returns 0 for each valid entry read, -1 at EOF
112  * Assumes all host names are lower case only
113  * Hostnames with more than one label are not handled correctly.
114  * Presently the dot is copied into name without
115  * converting to a length/string substring for that label.
116  */
117
118 static int getfileentry(FILE * fp, struct dns_entry *s)
119 {
120         unsigned int a,b,c,d;
121         char *line, *r, *name;
122
123  restart:
124         line = r = xmalloc_fgets(fp);
125         if (!r)
126                 return -1;
127         while (*r == ' ' || *r == '\t') {
128                 r++;
129                 if (!*r || *r == '#' || *r == '\n') {
130                         free(line);
131                         goto restart; /* skipping empty/blank and commented lines  */
132                 }
133         }
134         name = r;
135         while (*r != ' ' && *r != '\t')
136                 r++;
137         *r++ = '\0';
138         if (sscanf(r, ".%u.%u.%u.%u"+1, &a, &b, &c, &d) != 4) {
139                 free(line);
140                 goto restart; /* skipping wrong lines */
141         }
142
143         sprintf(s->ip, ".%u.%u.%u.%u"+1, a, b, c, d);
144         sprintf(s->rip, ".%u.%u.%u.%u", d, c, b, a);
145         undot((uint8_t*)s->rip);
146         convname(s->name, (uint8_t*)name);
147
148         if (OPT_verbose)
149                 fprintf(stderr, "\tname:%s, ip:%s\n", &(s->name[1]),s->ip);
150
151         free(line);
152         return 0;
153 }
154
155 /*
156  * Read hostname/IP records from file
157  */
158 static void dnsentryinit(void)
159 {
160         FILE *fp;
161         struct dns_entry *m, *prev;
162
163         prev = dnsentry = NULL;
164         fp = xfopen(fileconf, "r");
165
166         while (1) {
167                 m = xzalloc(sizeof(*m));
168                 /*m->next = NULL;*/
169                 if (getfileentry(fp, m))
170                         break;
171
172                 if (prev == NULL)
173                         dnsentry = m;
174                 else
175                         prev->next = m;
176                 prev = m;
177         }
178         fclose(fp);
179 }
180
181 /*
182  * Look query up in dns records and return answer if found
183  * qs is the query string, first byte the string length
184  */
185 static int table_lookup(uint16_t type, uint8_t * as, uint8_t * qs)
186 {
187         int i;
188         struct dns_entry *d = dnsentry;
189
190         do {
191 #if DEBUG
192                 char *p,*q;
193                 q = (char *)&(qs[1]);
194                 p = &(d->name[1]);
195                 fprintf(stderr, "\n%s: %d/%d p:%s q:%s %d",
196                         __FUNCTION__, (int)strlen(p), (int)(d->name[0]),
197                         p, q, (int)strlen(q));
198 #endif
199                 if (type == REQ_A) { /* search by host name */
200                         for (i = 1; i <= (int)(d->name[0]); i++)
201                                 if (tolower(qs[i]) != d->name[i])
202                                         break;
203                         if (i > (int)(d->name[0])) {
204                                 strcpy((char *)as, d->ip);
205 #if DEBUG
206                                 fprintf(stderr, " OK as:%s\n", as);
207 #endif
208                                 return 0;
209                         }
210                 } else if (type == REQ_PTR) { /* search by IP-address */
211                         if (!strncmp((char*)&d->rip[1], (char*)&qs[1], strlen(d->rip)-1)) {
212                                 strcpy((char *)as, d->name);
213                                 return 0;
214                         }
215                 }
216                 d = d->next;
217         } while (d);
218         return -1;
219 }
220
221
222 /*
223  * Decode message and generate answer
224  */
225 static int process_packet(uint8_t * buf)
226 {
227         struct dns_head *head;
228         struct dns_prop *qprop;
229         struct dns_repl outr;
230         void *next, *from, *answb;
231
232         uint8_t answstr[MAX_NAME_LEN + 1];
233         int lookup_result, type, len, packet_len;
234         uint16_t flags;
235
236         answstr[0] = '\0';
237
238         head = (struct dns_head *)buf;
239         if (head->nquer == 0) {
240                 bb_error_msg("no queries");
241                 retunr -1;
242         }
243
244         if (head->flags & 0x8000) {
245                 bb_error_msg("ignoring response packet");
246                 retunr -1;
247         }
248
249         from = (void *)&head[1];        //  start of query string
250         next = answb = from + strlen((char *)from) + 1 + sizeof(struct dns_prop);   // where to append answer block
251
252         outr.rlen = 0;                  // may change later
253         outr.r = NULL;
254         outr.flags = 0;
255
256         qprop = (struct dns_prop *)(answb - 4);
257         type = ntohs(qprop->type);
258
259         // only let REQ_A and REQ_PTR pass
260         if (!(type == REQ_A || type == REQ_PTR)) {
261                 goto empty_packet;      /* we can't handle the query type */
262         }
263
264         if (ntohs(qprop->class) != 1 /* class INET */ ) {
265                 outr.flags = 4; /* not supported */
266                 goto empty_packet;
267         }
268         /* we only support standard queries */
269
270         if ((ntohs(head->flags) & 0x7800) != 0)
271                 goto empty_packet;
272
273         // We have a standard query
274         bb_info_msg("%s", (char *)from);
275         lookup_result = table_lookup(type, answstr, (uint8_t*)from);
276         if (lookup_result != 0) {
277                 outr.flags = 3 | 0x0400;        //name do not exist and auth
278                 goto empty_packet;
279         }
280         if (type == REQ_A) {    // return an address
281                 struct in_addr a;
282                 if (!inet_aton((char*)answstr, &a)) {//dotted dec to long conv
283                         outr.flags = 1; /* Frmt err */
284                         goto empty_packet;
285                 }
286                 memcpy(answstr, &a.s_addr, 4);  // save before a disappears
287                 outr.rlen = 4;                  // uint32_t IP
288         } else
289                 outr.rlen = strlen((char *)answstr) + 1;        // a host name
290         outr.r = answstr;                       // 32 bit ip or a host name
291         outr.flags |= 0x0400;                   /* authority-bit */
292         // we have an answer
293         head->nansw = htons(1);
294
295         // copy query block to answer block
296         len = answb - from;
297         memcpy(answb, from, len);
298         next += len;
299
300         // and append answer rr
301         *(uint32_t *) next = htonl(ttl);
302         next += 4;
303         *(uint16_t *) next = htons(outr.rlen);
304         next += 2;
305         memcpy(next, (void *)answstr, outr.rlen);
306         next += outr.rlen;
307
308  empty_packet:
309
310         flags = ntohs(head->flags);
311         // clear rcode and RA, set responsebit and our new flags
312         flags |= (outr.flags & 0xff80) | 0x8000;
313         head->flags = htons(flags);
314         head->nauth = head->nadd = htons(0);
315         head->nquer = htons(1);
316
317         packet_len = (uint8_t *)next - buf;
318         return packet_len;
319 }
320
321 /*
322  * Exit on signal
323  */
324 static void interrupt(int x)
325 {
326         /* unlink("/var/run/dnsd.lock"); */
327         bb_error_msg("interrupt, exiting\n");
328         exit(2);
329 }
330
331 int dnsd_main(int argc, char **argv);
332 int dnsd_main(int argc, char **argv)
333 {
334         const char *listen_interface = "0.0.0.0";
335         char *sttl, *sport;
336         len_and_sockaddr *lsa;
337         int udps;
338         uint16_t port = 53;
339         uint8_t buf[MAX_PACK_LEN];
340
341         getopt32(argc, argv, "i:c:t:p:dv", &listen_interface, &fileconf, &sttl, &sport);
342         //if (option_mask32 & 0x1) // -i
343         //if (option_mask32 & 0x2) // -c
344         if (option_mask32 & 0x4) // -t
345                 ttl = xatou_range(sttl, 1, 0xffffffff);
346         if (option_mask32 & 0x8) // -p
347                 port = xatou_range(sport, 1, 0xffff);
348
349         if (OPT_verbose) {
350                 bb_info_msg("listen_interface: %s", listen_interface);
351                 bb_info_msg("ttl: %d, port: %d", ttl, port);
352                 bb_info_msg("fileconf: %s", fileconf);
353         }
354
355         if (OPT_daemon) {
356 #ifdef BB_NOMMU
357                 if (!re_execed)
358                         vfork_daemon_rexec(1, 0, argv);
359 #else
360                 xdaemon(1, 0);
361 #endif
362                 logmode = LOGMODE_SYSLOG;
363         }
364
365         dnsentryinit();
366
367         signal(SIGINT, interrupt);
368         /* why? signal(SIGPIPE, SIG_IGN); */
369         signal(SIGHUP, SIG_IGN);
370 #ifdef SIGTSTP
371         signal(SIGTSTP, SIG_IGN);
372 #endif
373 #ifdef SIGURG
374         signal(SIGURG, SIG_IGN);
375 #endif
376
377         lsa = xdotted2sockaddr(listen_interface, port);
378         udps = xsocket(lsa->sa.sa_family, SOCK_DGRAM, 0);
379         xbind(udps, &lsa->sa, lsa->len);
380         /* xlisten(udps, 50); - ?!! DGRAM sockets are never listened on I think? */
381         bb_info_msg("Accepting UDP packets on %s",
382                         xmalloc_sockaddr2dotted(&lsa->sa, lsa->len));
383
384         while (1) {
385                 int r;
386                 socklen_t fromlen = lsa->len;
387 // FIXME: need to get *DEST* address (to which of our addresses
388 // this query was directed), and reply from the same address.
389 // Or else we can exhibit usual UDP ugliness:
390 // [ip1.multihomed.ip2] <=  query to ip1  <= peer
391 // [ip1.multihomed.ip2] => reply from ip2 => peer (confused)
392                 r = recvfrom(udps, buf, sizeof(buf), 0, &lsa->sa, &fromlen);
393                 if (OPT_verbose)
394                         bb_info_msg("Got UDP packet");
395                 if (r < 12 || r > 512) {
396                         bb_error_msg("invalid packet size");
397                         continue;
398                 }
399                 r = process_packet(buf);
400                 if (r <= 0)
401                         continue;
402                 sendto(udps, buf, r, 0, &lsa->sa, fromlen);
403         }
404 }