[821] | 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 <unistd.h>
|
---|
| 21 | #include <string.h>
|
---|
| 22 | #include <signal.h>
|
---|
| 23 | #include <arpa/inet.h>
|
---|
| 24 | #include <sys/socket.h>
|
---|
| 25 | #include <ctype.h>
|
---|
| 26 | #include "busybox.h"
|
---|
| 27 |
|
---|
| 28 | static char *fileconf = "/etc/dnsd.conf";
|
---|
| 29 | #define LOCK_FILE "/var/run/dnsd.lock"
|
---|
| 30 | #define LOG_FILE "/var/log/dnsd.log"
|
---|
| 31 |
|
---|
[902] | 32 | #define is_daemon() (flags&16)
|
---|
| 33 | #define is_verbose() (flags&32)
|
---|
| 34 | //#define DEBUG
|
---|
| 35 |
|
---|
| 36 |
|
---|
[821] | 37 | enum {
|
---|
| 38 | MAX_HOST_LEN = 16, // longest host name allowed is 15
|
---|
| 39 | IP_STRING_LEN = 18, // .xxx.xxx.xxx.xxx\0
|
---|
| 40 |
|
---|
| 41 | //must be strlen('.in-addr.arpa') larger than IP_STRING_LEN
|
---|
| 42 | MAX_NAME_LEN = (IP_STRING_LEN + 13),
|
---|
| 43 |
|
---|
| 44 | /* Cannot get bigger packets than 512 per RFC1035
|
---|
| 45 | In practice this can be set considerably smaller:
|
---|
| 46 | Length of response packet is header (12B) + 2*type(4B) + 2*class(4B) +
|
---|
| 47 | ttl(4B) + rlen(2B) + r (MAX_NAME_LEN =21B) +
|
---|
| 48 | 2*querystring (2 MAX_NAME_LEN= 42B), all together 90 Byte
|
---|
| 49 | */
|
---|
| 50 | MAX_PACK_LEN = 512 + 1,
|
---|
| 51 |
|
---|
| 52 | DEFAULT_TTL = 30, // increase this when not testing?
|
---|
| 53 |
|
---|
| 54 | REQ_A = 1,
|
---|
| 55 | REQ_PTR = 12
|
---|
| 56 | };
|
---|
| 57 |
|
---|
| 58 | struct dns_repl { // resource record, add 0 or 1 to accepted dns_msg in resp
|
---|
| 59 | uint16_t rlen;
|
---|
| 60 | uint8_t *r; // resource
|
---|
| 61 | uint16_t flags;
|
---|
| 62 | };
|
---|
| 63 |
|
---|
| 64 | struct dns_head { // the message from client and first part of response mag
|
---|
| 65 | uint16_t id;
|
---|
| 66 | uint16_t flags;
|
---|
| 67 | uint16_t nquer; // accepts 0
|
---|
| 68 | uint16_t nansw; // 1 in response
|
---|
| 69 | uint16_t nauth; // 0
|
---|
| 70 | uint16_t nadd; // 0
|
---|
| 71 | };
|
---|
| 72 | struct dns_prop {
|
---|
| 73 | uint16_t type;
|
---|
| 74 | uint16_t class;
|
---|
| 75 | };
|
---|
| 76 | struct dns_entry { // element of known name, ip address and reversed ip address
|
---|
| 77 | struct dns_entry *next;
|
---|
| 78 | char ip[IP_STRING_LEN]; // dotted decimal IP
|
---|
| 79 | char rip[IP_STRING_LEN]; // length decimal reversed IP
|
---|
| 80 | char name[MAX_HOST_LEN];
|
---|
| 81 | };
|
---|
| 82 |
|
---|
| 83 | static struct dns_entry *dnsentry = NULL;
|
---|
| 84 | static int daemonmode = 0;
|
---|
| 85 | static uint32_t ttl = DEFAULT_TTL;
|
---|
| 86 |
|
---|
| 87 | /*
|
---|
| 88 | * Convert host name from C-string to dns length/string.
|
---|
| 89 | */
|
---|
| 90 | static void convname(char *a, uint8_t *q)
|
---|
| 91 | {
|
---|
| 92 | int i = (q[0] == '.') ? 0 : 1;
|
---|
| 93 | for(; i < MAX_HOST_LEN-1 && *q; i++, q++)
|
---|
| 94 | a[i] = tolower(*q);
|
---|
| 95 | a[0] = i - 1;
|
---|
| 96 | a[i] = 0;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | /*
|
---|
| 100 | * Insert length of substrings instead of dots
|
---|
| 101 | */
|
---|
| 102 | static void undot(uint8_t * rip)
|
---|
| 103 | {
|
---|
| 104 | int i = 0, s = 0;
|
---|
| 105 | while(rip[i]) i++;
|
---|
| 106 | for(--i; i >= 0; i--) {
|
---|
| 107 | if(rip[i] == '.') {
|
---|
| 108 | rip[i] = s;
|
---|
| 109 | s = 0;
|
---|
| 110 | } else s++;
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | /*
|
---|
| 115 | * Append message to log file
|
---|
| 116 | */
|
---|
| 117 | static void log_message(char *filename, char *message)
|
---|
| 118 | {
|
---|
| 119 | FILE *logfile;
|
---|
| 120 | if (!daemonmode)
|
---|
| 121 | return;
|
---|
| 122 | logfile = fopen(filename, "a");
|
---|
| 123 | if (!logfile)
|
---|
| 124 | return;
|
---|
| 125 | fprintf(logfile, "%s\n", message);
|
---|
| 126 | fclose(logfile);
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | /*
|
---|
| 130 | * Read one line of hostname/IP from file
|
---|
| 131 | * Returns 0 for each valid entry read, -1 at EOF
|
---|
| 132 | * Assumes all host names are lower case only
|
---|
| 133 | * Hostnames with more than one label is not handled correctly.
|
---|
| 134 | * Presently the dot is copied into name without
|
---|
| 135 | * converting to a length/string substring for that label.
|
---|
| 136 | */
|
---|
| 137 |
|
---|
| 138 | static int getfileentry(FILE * fp, struct dns_entry *s, int verb)
|
---|
| 139 | {
|
---|
| 140 | unsigned int a,b,c,d;
|
---|
| 141 | char *r, *name;
|
---|
| 142 |
|
---|
| 143 | restart:
|
---|
| 144 | if(!(r = bb_get_line_from_file(fp)))
|
---|
| 145 | return -1;
|
---|
| 146 | while(*r == ' ' || *r == '\t') {
|
---|
| 147 | r++;
|
---|
| 148 | if(!*r || *r == '#' || *r == '\n')
|
---|
| 149 | goto restart; /* skipping empty/blank and commented lines */
|
---|
| 150 | }
|
---|
| 151 | name = r;
|
---|
| 152 | while(*r != ' ' && *r != '\t')
|
---|
| 153 | r++;
|
---|
| 154 | *r++ = 0;
|
---|
| 155 | if(sscanf(r,"%u.%u.%u.%u",&a,&b,&c,&d) != 4)
|
---|
| 156 | goto restart; /* skipping wrong lines */
|
---|
| 157 |
|
---|
| 158 | sprintf(s->ip,"%u.%u.%u.%u",a,b,c,d);
|
---|
| 159 | sprintf(s->rip,".%u.%u.%u.%u",d,c,b,a);
|
---|
| 160 | undot((uint8_t*)s->rip);
|
---|
| 161 | convname(s->name,(uint8_t*)name);
|
---|
| 162 |
|
---|
| 163 | if(verb)
|
---|
| 164 | fprintf(stderr,"\tname:%s, ip:%s\n",&(s->name[1]),s->ip);
|
---|
| 165 |
|
---|
| 166 | return 0; /* warningkiller */
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | /*
|
---|
| 170 | * Read hostname/IP records from file
|
---|
| 171 | */
|
---|
| 172 | static void dnsentryinit(int verb)
|
---|
| 173 | {
|
---|
| 174 | FILE *fp;
|
---|
| 175 | struct dns_entry *m, *prev;
|
---|
| 176 | prev = dnsentry = NULL;
|
---|
| 177 |
|
---|
| 178 | fp = bb_xfopen(fileconf, "r");
|
---|
| 179 |
|
---|
| 180 | while (1) {
|
---|
| 181 | m = xmalloc(sizeof(struct dns_entry));
|
---|
| 182 |
|
---|
| 183 | m->next = NULL;
|
---|
| 184 | if (getfileentry(fp, m, verb))
|
---|
| 185 | break;
|
---|
| 186 |
|
---|
| 187 | if (prev == NULL)
|
---|
| 188 | dnsentry = m;
|
---|
| 189 | else
|
---|
| 190 | prev->next = m;
|
---|
| 191 | prev = m;
|
---|
| 192 | }
|
---|
| 193 | fclose(fp);
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 |
|
---|
| 197 | /*
|
---|
| 198 | * Set up UDP socket
|
---|
| 199 | */
|
---|
| 200 | static int listen_socket(char *iface_addr, int listen_port)
|
---|
| 201 | {
|
---|
| 202 | struct sockaddr_in a;
|
---|
| 203 | char msg[100];
|
---|
| 204 | int s;
|
---|
| 205 | int yes = 1;
|
---|
| 206 | s = bb_xsocket(PF_INET, SOCK_DGRAM, 0);
|
---|
| 207 | #ifdef SO_REUSEADDR
|
---|
| 208 | if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&yes, sizeof(yes)) < 0)
|
---|
| 209 | bb_perror_msg_and_die("setsockopt() failed");
|
---|
| 210 | #endif
|
---|
| 211 | memset(&a, 0, sizeof(a));
|
---|
| 212 | a.sin_port = htons(listen_port);
|
---|
| 213 | a.sin_family = AF_INET;
|
---|
| 214 | if (!inet_aton(iface_addr, &a.sin_addr))
|
---|
| 215 | bb_perror_msg_and_die("bad iface address");
|
---|
| 216 | bb_xbind(s, (struct sockaddr *)&a, sizeof(a));
|
---|
| 217 | listen(s, 50); /* bb_xlisten? */
|
---|
| 218 | sprintf(msg, "accepting UDP packets on addr:port %s:%d\n",
|
---|
| 219 | iface_addr, (int)listen_port);
|
---|
| 220 | log_message(LOG_FILE, msg);
|
---|
| 221 | return s;
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | /*
|
---|
| 225 | * Look query up in dns records and return answer if found
|
---|
| 226 | * qs is the query string, first byte the string length
|
---|
| 227 | */
|
---|
| 228 | static int table_lookup(uint16_t type, uint8_t * as, uint8_t * qs)
|
---|
| 229 | {
|
---|
| 230 | int i;
|
---|
[902] | 231 | struct dns_entry *d = dnsentry;
|
---|
[821] | 232 |
|
---|
[902] | 233 | if(d) do {
|
---|
[821] | 234 | #ifdef DEBUG
|
---|
[902] | 235 | if(qs && d) {
|
---|
| 236 | char *p,*q;
|
---|
| 237 | q = (char *)&(qs[1]);
|
---|
| 238 | p = &(d->name[1]);
|
---|
| 239 | fprintf(stderr, "\n%s: %d/%d p:%s q:%s %d",
|
---|
| 240 | __FUNCTION__, strlen(p), (int)(d->name[0]),
|
---|
| 241 | p, q, strlen(q));
|
---|
| 242 | }
|
---|
[821] | 243 | #endif
|
---|
[902] | 244 | if (type == REQ_A) { /* search by host name */
|
---|
[821] | 245 | for(i = 1; i <= (int)(d->name[0]); i++)
|
---|
| 246 | if(tolower(qs[i]) != d->name[i])
|
---|
[902] | 247 | break;
|
---|
| 248 | if(i > (int)(d->name[0])) {
|
---|
[821] | 249 | #ifdef DEBUG
|
---|
[902] | 250 | fprintf(stderr, " OK");
|
---|
[821] | 251 | #endif
|
---|
[902] | 252 | strcpy((char *)as, d->ip);
|
---|
[821] | 253 | #ifdef DEBUG
|
---|
[902] | 254 | fprintf(stderr, " as:%s\n", as);
|
---|
[821] | 255 | #endif
|
---|
[902] | 256 | return 0;
|
---|
| 257 | }
|
---|
| 258 | } else
|
---|
| 259 | if (type == REQ_PTR) { /* search by IP-address */
|
---|
[821] | 260 | if (!strncmp((char*)&d->rip[1], (char*)&qs[1], strlen(d->rip)-1)) {
|
---|
| 261 | strcpy((char *)as, d->name);
|
---|
| 262 | return 0;
|
---|
| 263 | }
|
---|
| 264 | }
|
---|
| 265 | } while ((d = d->next) != NULL);
|
---|
| 266 | return -1;
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 |
|
---|
| 270 | /*
|
---|
| 271 | * Decode message and generate answer
|
---|
| 272 | */
|
---|
| 273 | #define eret(s) do { fprintf (stderr, "%s\n", s); return -1; } while (0)
|
---|
| 274 | static int process_packet(uint8_t * buf)
|
---|
| 275 | {
|
---|
| 276 | struct dns_head *head;
|
---|
| 277 | struct dns_prop *qprop;
|
---|
| 278 | struct dns_repl outr;
|
---|
| 279 | void *next, *from, *answb;
|
---|
| 280 |
|
---|
| 281 | uint8_t answstr[MAX_NAME_LEN + 1];
|
---|
| 282 | int lookup_result, type, len, packet_len;
|
---|
| 283 | uint16_t flags;
|
---|
| 284 |
|
---|
| 285 | answstr[0] = '\0';
|
---|
| 286 |
|
---|
| 287 | head = (struct dns_head *)buf;
|
---|
| 288 | if (head->nquer == 0)
|
---|
| 289 | eret("no queries");
|
---|
| 290 |
|
---|
| 291 | if ((head->flags & 0x8000))
|
---|
| 292 | eret("ignoring response packet");
|
---|
| 293 |
|
---|
| 294 | from = (void *)&head[1]; // start of query string
|
---|
[902] | 295 | next = answb = from + strlen((char *)from) + 1 + sizeof(struct dns_prop); // where to append answer block
|
---|
[821] | 296 |
|
---|
| 297 | outr.rlen = 0; // may change later
|
---|
| 298 | outr.r = NULL;
|
---|
| 299 | outr.flags = 0;
|
---|
| 300 |
|
---|
| 301 | qprop = (struct dns_prop *)(answb - 4);
|
---|
| 302 | type = ntohs(qprop->type);
|
---|
| 303 |
|
---|
| 304 | // only let REQ_A and REQ_PTR pass
|
---|
| 305 | if (!(type == REQ_A || type == REQ_PTR)) {
|
---|
| 306 | goto empty_packet; /* we can't handle the query type */
|
---|
| 307 | }
|
---|
| 308 |
|
---|
| 309 | if (ntohs(qprop->class) != 1 /* class INET */ ) {
|
---|
| 310 | outr.flags = 4; /* not supported */
|
---|
| 311 | goto empty_packet;
|
---|
| 312 | }
|
---|
| 313 | /* we only support standard queries */
|
---|
| 314 |
|
---|
| 315 | if ((ntohs(head->flags) & 0x7800) != 0)
|
---|
| 316 | goto empty_packet;
|
---|
| 317 |
|
---|
| 318 | // We have a standard query
|
---|
[902] | 319 | log_message(LOG_FILE, (char *)from);
|
---|
| 320 | lookup_result = table_lookup(type, answstr, (uint8_t*)from);
|
---|
[821] | 321 | if (lookup_result != 0) {
|
---|
| 322 | outr.flags = 3 | 0x0400; //name do not exist and auth
|
---|
| 323 | goto empty_packet;
|
---|
| 324 | }
|
---|
| 325 | if (type == REQ_A) { // return an address
|
---|
| 326 | struct in_addr a;
|
---|
| 327 | if (!inet_aton((char*)answstr, &a)) {//dotted dec to long conv
|
---|
| 328 | outr.flags = 1; /* Frmt err */
|
---|
| 329 | goto empty_packet;
|
---|
| 330 | }
|
---|
| 331 | memcpy(answstr, &a.s_addr, 4); // save before a disappears
|
---|
| 332 | outr.rlen = 4; // uint32_t IP
|
---|
| 333 | }
|
---|
| 334 | else
|
---|
| 335 | outr.rlen = strlen((char *)answstr) + 1; // a host name
|
---|
| 336 | outr.r = answstr; // 32 bit ip or a host name
|
---|
| 337 | outr.flags |= 0x0400; /* authority-bit */
|
---|
| 338 | // we have an answer
|
---|
| 339 | head->nansw = htons(1);
|
---|
| 340 |
|
---|
| 341 | // copy query block to answer block
|
---|
| 342 | len = answb - from;
|
---|
| 343 | memcpy(answb, from, len);
|
---|
| 344 | next += len;
|
---|
| 345 |
|
---|
| 346 | // and append answer rr
|
---|
| 347 | *(uint32_t *) next = htonl(ttl);
|
---|
| 348 | next += 4;
|
---|
| 349 | *(uint16_t *) next = htons(outr.rlen);
|
---|
| 350 | next += 2;
|
---|
| 351 | memcpy(next, (void *)answstr, outr.rlen);
|
---|
| 352 | next += outr.rlen;
|
---|
| 353 |
|
---|
[902] | 354 | empty_packet:
|
---|
[821] | 355 | flags = ntohs(head->flags);
|
---|
| 356 | // clear rcode and RA, set responsebit and our new flags
|
---|
| 357 | flags |= (outr.flags & 0xff80) | 0x8000;
|
---|
| 358 | head->flags = htons(flags);
|
---|
| 359 | head->nauth = head->nadd = htons(0);
|
---|
| 360 | head->nquer = htons(1);
|
---|
| 361 |
|
---|
| 362 | packet_len = next - (void *)buf;
|
---|
| 363 | return packet_len;
|
---|
| 364 | }
|
---|
| 365 |
|
---|
| 366 | /*
|
---|
| 367 | * Exit on signal
|
---|
| 368 | */
|
---|
| 369 | static void interrupt(int x)
|
---|
| 370 | {
|
---|
| 371 | unlink(LOCK_FILE);
|
---|
| 372 | write(2, "interrupt exiting\n", 18);
|
---|
| 373 | exit(2);
|
---|
| 374 | }
|
---|
| 375 |
|
---|
| 376 |
|
---|
| 377 | int dnsd_main(int argc, char **argv)
|
---|
| 378 | {
|
---|
| 379 | int udps;
|
---|
| 380 | uint16_t port = 53;
|
---|
| 381 | uint8_t buf[MAX_PACK_LEN];
|
---|
| 382 | unsigned long flags = 0;
|
---|
| 383 | char *listen_interface = "0.0.0.0";
|
---|
| 384 | char *sttl=NULL, *sport=NULL;
|
---|
| 385 |
|
---|
| 386 | if(argc > 1)
|
---|
| 387 | flags = bb_getopt_ulflags(argc, argv, "i:c:t:p:dv", &listen_interface, &fileconf, &sttl, &sport);
|
---|
| 388 | if(sttl)
|
---|
| 389 | if(!(ttl = atol(sttl)))
|
---|
| 390 | bb_show_usage();
|
---|
| 391 | if(sport)
|
---|
| 392 | if(!(port = atol(sport)))
|
---|
| 393 | bb_show_usage();
|
---|
| 394 |
|
---|
| 395 | if(is_verbose()) {
|
---|
| 396 | fprintf(stderr,"listen_interface: %s\n", listen_interface);
|
---|
| 397 | fprintf(stderr,"ttl: %d, port: %d\n", ttl, port);
|
---|
| 398 | fprintf(stderr,"fileconf: %s\n", fileconf);
|
---|
| 399 | }
|
---|
| 400 |
|
---|
| 401 | if(is_daemon())
|
---|
| 402 | #ifdef BB_NOMMU
|
---|
| 403 | /* reexec for vfork() do continue parent */
|
---|
| 404 | vfork_daemon_rexec(1, 0, argc, argv, "-d");
|
---|
| 405 | #else
|
---|
| 406 | bb_xdaemon(1, 0);
|
---|
| 407 | #endif
|
---|
| 408 |
|
---|
| 409 | dnsentryinit(is_verbose());
|
---|
| 410 |
|
---|
| 411 | signal(SIGINT, interrupt);
|
---|
| 412 | signal(SIGPIPE, SIG_IGN);
|
---|
| 413 | signal(SIGHUP, SIG_IGN);
|
---|
| 414 | #ifdef SIGTSTP
|
---|
| 415 | signal(SIGTSTP, SIG_IGN);
|
---|
| 416 | #endif
|
---|
| 417 | #ifdef SIGURG
|
---|
| 418 | signal(SIGURG, SIG_IGN);
|
---|
| 419 | #endif
|
---|
| 420 |
|
---|
| 421 | udps = listen_socket(listen_interface, port);
|
---|
| 422 | if (udps < 0)
|
---|
| 423 | exit(1);
|
---|
| 424 |
|
---|
| 425 | while (1) {
|
---|
| 426 | fd_set fdset;
|
---|
| 427 | int r;
|
---|
| 428 |
|
---|
| 429 | FD_ZERO(&fdset);
|
---|
| 430 | FD_SET(udps, &fdset);
|
---|
| 431 | // Block until a message arrives
|
---|
| 432 | if((r = select(udps + 1, &fdset, NULL, NULL, NULL)) < 0)
|
---|
| 433 | bb_perror_msg_and_die("select error");
|
---|
| 434 | else
|
---|
| 435 | if(r == 0)
|
---|
| 436 | bb_perror_msg_and_die("select spurious return");
|
---|
| 437 |
|
---|
| 438 | /* Can this test ever be false? */
|
---|
| 439 | if (FD_ISSET(udps, &fdset)) {
|
---|
| 440 | struct sockaddr_in from;
|
---|
| 441 | int fromlen = sizeof(from);
|
---|
| 442 | r = recvfrom(udps, buf, sizeof(buf), 0,
|
---|
| 443 | (struct sockaddr *)&from,
|
---|
| 444 | (void *)&fromlen);
|
---|
| 445 | if(is_verbose())
|
---|
[902] | 446 | fprintf(stderr, "\n--- Got UDP size=%d ", r);
|
---|
| 447 | log_message(LOG_FILE, "\n--- Got UDP ");
|
---|
[821] | 448 |
|
---|
| 449 | if (r < 12 || r > 512) {
|
---|
| 450 | bb_error_msg("invalid packet size");
|
---|
| 451 | continue;
|
---|
| 452 | }
|
---|
| 453 | if (r > 0) {
|
---|
| 454 | r = process_packet(buf);
|
---|
| 455 | if (r > 0)
|
---|
| 456 | sendto(udps, buf,
|
---|
| 457 | r, 0, (struct sockaddr *)&from,
|
---|
| 458 | fromlen);
|
---|
| 459 | }
|
---|
| 460 | } // end if
|
---|
| 461 | } // end while
|
---|
| 462 | return 0;
|
---|
| 463 | }
|
---|
| 464 |
|
---|
| 465 |
|
---|