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