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 | |
Bernhard Reutner-Fischer | 421d9e5 | 2006-04-03 16:39:31 +0000 | [diff] [blame] | 20 | #include "busybox.h" |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 21 | |
Denis Vlasenko | 2c91652 | 2007-01-12 14:57:37 +0000 | [diff] [blame^] | 22 | static const char *fileconf = "/etc/dnsd.conf"; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 23 | #define LOCK_FILE "/var/run/dnsd.lock" |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 24 | |
Denis Vlasenko | 2c91652 | 2007-01-12 14:57:37 +0000 | [diff] [blame^] | 25 | // Must match getopt32 call |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 26 | #define OPT_daemon (option_mask32 & 0x10) |
| 27 | #define OPT_verbose (option_mask32 & 0x20) |
| 28 | |
| 29 | //#define DEBUG 1 |
Denis Vlasenko | 2c91652 | 2007-01-12 14:57:37 +0000 | [diff] [blame^] | 30 | #define DEBUG 0 |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 31 | |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 32 | enum { |
| 33 | MAX_HOST_LEN = 16, // longest host name allowed is 15 |
| 34 | IP_STRING_LEN = 18, // .xxx.xxx.xxx.xxx\0 |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 35 | |
| 36 | //must be strlen('.in-addr.arpa') larger than IP_STRING_LEN |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 37 | MAX_NAME_LEN = (IP_STRING_LEN + 13), |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 38 | |
| 39 | /* Cannot get bigger packets than 512 per RFC1035 |
| 40 | In practice this can be set considerably smaller: |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 41 | Length of response packet is header (12B) + 2*type(4B) + 2*class(4B) + |
| 42 | ttl(4B) + rlen(2B) + r (MAX_NAME_LEN =21B) + |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 43 | 2*querystring (2 MAX_NAME_LEN= 42B), all together 90 Byte |
| 44 | */ |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 45 | MAX_PACK_LEN = 512 + 1, |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 46 | |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 47 | DEFAULT_TTL = 30, // increase this when not testing? |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 48 | |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 49 | REQ_A = 1, |
| 50 | REQ_PTR = 12 |
| 51 | }; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 52 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 53 | struct dns_repl { // resource record, add 0 or 1 to accepted dns_msg in resp |
| 54 | uint16_t rlen; |
| 55 | uint8_t *r; // resource |
| 56 | uint16_t flags; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 57 | }; |
| 58 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 59 | struct dns_head { // the message from client and first part of response mag |
| 60 | uint16_t id; |
| 61 | uint16_t flags; |
| 62 | uint16_t nquer; // accepts 0 |
| 63 | uint16_t nansw; // 1 in response |
| 64 | uint16_t nauth; // 0 |
| 65 | uint16_t nadd; // 0 |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 66 | }; |
| 67 | struct dns_prop { |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 68 | uint16_t type; |
| 69 | uint16_t class; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 70 | }; |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 71 | struct dns_entry { // element of known name, ip address and reversed ip address |
| 72 | struct dns_entry *next; |
| 73 | char ip[IP_STRING_LEN]; // dotted decimal IP |
| 74 | char rip[IP_STRING_LEN]; // length decimal reversed IP |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 75 | char name[MAX_HOST_LEN]; |
| 76 | }; |
| 77 | |
| 78 | static struct dns_entry *dnsentry = NULL; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 79 | static uint32_t ttl = DEFAULT_TTL; |
| 80 | |
| 81 | /* |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 82 | * Convert host name from C-string to dns length/string. |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 83 | */ |
Rob Landley | a6e131d | 2006-05-29 06:43:55 +0000 | [diff] [blame] | 84 | static void convname(char *a, uint8_t *q) |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 85 | { |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 86 | int i = (q[0] == '.') ? 0 : 1; |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 87 | for (; i < MAX_HOST_LEN-1 && *q; i++, q++) |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 88 | a[i] = tolower(*q); |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 89 | a[0] = i - 1; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 90 | a[i] = 0; |
| 91 | } |
| 92 | |
| 93 | /* |
Bernhard Reutner-Fischer | c418d48 | 2006-05-31 10:19:51 +0000 | [diff] [blame] | 94 | * Insert length of substrings instead of dots |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 95 | */ |
Rob Landley | a6e131d | 2006-05-29 06:43:55 +0000 | [diff] [blame] | 96 | static void undot(uint8_t * rip) |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 97 | { |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 98 | int i = 0, s = 0; |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 99 | while (rip[i]) |
| 100 | i++; |
| 101 | for (--i; i >= 0; i--) { |
| 102 | if (rip[i] == '.') { |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 103 | rip[i] = s; |
| 104 | s = 0; |
| 105 | } else s++; |
| 106 | } |
| 107 | } |
| 108 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 109 | /* |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 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 is not handled correctly. |
| 114 | * Presently the dot is copied into name without |
| 115 | * converting to a length/string substring for that label. |
| 116 | */ |
| 117 | |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 118 | static int getfileentry(FILE * fp, struct dns_entry *s) |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 119 | { |
| 120 | unsigned int a,b,c,d; |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 121 | char *r, *name; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 122 | |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 123 | restart: |
Denis Vlasenko | 2d5ca60 | 2006-10-12 22:43:20 +0000 | [diff] [blame] | 124 | r = xmalloc_fgets(fp); |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 125 | if (!r) |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 126 | return -1; |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 127 | while (*r == ' ' || *r == '\t') { |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 128 | r++; |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 129 | if (!*r || *r == '#' || *r == '\n') |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 130 | goto restart; /* skipping empty/blank and commented lines */ |
| 131 | } |
| 132 | name = r; |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 133 | while (*r != ' ' && *r != '\t') |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 134 | r++; |
| 135 | *r++ = 0; |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 136 | if (sscanf(r, "%u.%u.%u.%u", &a, &b, &c, &d) != 4) |
| 137 | goto restart; /* skipping wrong lines */ |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 138 | |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 139 | sprintf(s->ip, "%u.%u.%u.%u", a, b, c, d); |
| 140 | sprintf(s->rip, ".%u.%u.%u.%u", d, c, b, a); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 141 | undot((uint8_t*)s->rip); |
| 142 | convname(s->name,(uint8_t*)name); |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 143 | |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 144 | if (OPT_verbose) |
| 145 | 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] | 146 | |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 147 | return 0; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /* |
| 151 | * Read hostname/IP records from file |
| 152 | */ |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 153 | static void dnsentryinit(void) |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 154 | { |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 155 | FILE *fp; |
| 156 | struct dns_entry *m, *prev; |
| 157 | prev = dnsentry = NULL; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 158 | |
Rob Landley | 86b4d64 | 2006-08-03 17:58:17 +0000 | [diff] [blame] | 159 | fp = xfopen(fileconf, "r"); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 160 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 161 | while (1) { |
Rob Landley | a6e131d | 2006-05-29 06:43:55 +0000 | [diff] [blame] | 162 | m = xmalloc(sizeof(struct dns_entry)); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 163 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 164 | m->next = NULL; |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 165 | if (getfileentry(fp, m)) |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 166 | break; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 167 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 168 | if (prev == NULL) |
| 169 | dnsentry = m; |
| 170 | else |
| 171 | prev->next = m; |
| 172 | prev = m; |
| 173 | } |
| 174 | fclose(fp); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 175 | } |
| 176 | |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 177 | /* |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 178 | * Look query up in dns records and return answer if found |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 179 | * qs is the query string, first byte the string length |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 180 | */ |
Rob Landley | a6e131d | 2006-05-29 06:43:55 +0000 | [diff] [blame] | 181 | 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] | 182 | { |
| 183 | int i; |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 184 | struct dns_entry *d=dnsentry; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 185 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 186 | do { |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 187 | #ifdef DEBUG |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 188 | char *p,*q; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 189 | q = (char *)&(qs[1]); |
| 190 | p = &(d->name[1]); |
Denis Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 191 | fprintf(stderr, "\n%s: %d/%d p:%s q:%s %d", |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 192 | __FUNCTION__, strlen(p), (int)(d->name[0]), p, q, strlen(q)); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 193 | #endif |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 194 | if (type == REQ_A) { /* search by host name */ |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 195 | for (i = 1; i <= (int)(d->name[0]); i++) |
| 196 | if (tolower(qs[i]) != d->name[i]) |
| 197 | break; |
| 198 | if (i > (int)(d->name[0])) { |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 199 | #ifdef DEBUG |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 200 | fprintf(stderr, " OK"); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 201 | #endif |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 202 | strcpy((char *)as, d->ip); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 203 | #ifdef DEBUG |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 204 | fprintf(stderr, " as:%s\n", as); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 205 | #endif |
| 206 | return 0; |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 207 | } |
Denis Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 208 | } else |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 209 | if (type == REQ_PTR) { /* search by IP-address */ |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 210 | if (!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] | 211 | strcpy((char *)as, d->name); |
| 212 | return 0; |
| 213 | } |
| 214 | } |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 215 | d = d->next; |
| 216 | } while (d); |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 217 | return -1; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 221 | /* |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 222 | * Decode message and generate answer |
| 223 | */ |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 224 | #define eret(s) do { fputs(s, stderr); return -1; } while (0) |
Rob Landley | a6e131d | 2006-05-29 06:43:55 +0000 | [diff] [blame] | 225 | static int process_packet(uint8_t * buf) |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 226 | { |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 227 | struct dns_head *head; |
| 228 | struct dns_prop *qprop; |
| 229 | struct dns_repl outr; |
| 230 | void *next, *from, *answb; |
"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 | uint8_t answstr[MAX_NAME_LEN + 1]; |
| 233 | int lookup_result, type, len, packet_len; |
| 234 | uint16_t flags; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 235 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 236 | answstr[0] = '\0'; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 237 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 238 | head = (struct dns_head *)buf; |
| 239 | if (head->nquer == 0) |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 240 | eret("no queries\n"); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 241 | |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 242 | if (head->flags & 0x8000) |
| 243 | eret("ignoring response packet\n"); |
"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 | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 246 | next = answb = from + strlen((char *)from) + 1 + sizeof(struct dns_prop); // where to append answer block |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 247 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 248 | outr.rlen = 0; // may change later |
| 249 | outr.r = NULL; |
| 250 | outr.flags = 0; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 251 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 252 | qprop = (struct dns_prop *)(answb - 4); |
| 253 | type = ntohs(qprop->type); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 254 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 255 | // only let REQ_A and REQ_PTR pass |
| 256 | if (!(type == REQ_A || type == REQ_PTR)) { |
| 257 | goto empty_packet; /* we can't handle the query type */ |
| 258 | } |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 259 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 260 | if (ntohs(qprop->class) != 1 /* class INET */ ) { |
| 261 | outr.flags = 4; /* not supported */ |
| 262 | goto empty_packet; |
| 263 | } |
| 264 | /* we only support standard queries */ |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 265 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 266 | if ((ntohs(head->flags) & 0x7800) != 0) |
| 267 | goto empty_packet; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 268 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 269 | // We have a standard query |
Denis Vlasenko | 2c91652 | 2007-01-12 14:57:37 +0000 | [diff] [blame^] | 270 | bb_info_msg("%s", (char *)from); |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 271 | lookup_result = table_lookup(type, answstr, (uint8_t*)from); |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 272 | if (lookup_result != 0) { |
| 273 | outr.flags = 3 | 0x0400; //name do not exist and auth |
| 274 | goto empty_packet; |
| 275 | } |
| 276 | if (type == REQ_A) { // return an address |
| 277 | struct in_addr a; |
| 278 | if (!inet_aton((char*)answstr, &a)) {//dotted dec to long conv |
| 279 | outr.flags = 1; /* Frmt err */ |
| 280 | goto empty_packet; |
| 281 | } |
| 282 | memcpy(answstr, &a.s_addr, 4); // save before a disappears |
| 283 | outr.rlen = 4; // uint32_t IP |
| 284 | } |
| 285 | else |
| 286 | outr.rlen = strlen((char *)answstr) + 1; // a host name |
| 287 | outr.r = answstr; // 32 bit ip or a host name |
| 288 | outr.flags |= 0x0400; /* authority-bit */ |
| 289 | // we have an answer |
| 290 | head->nansw = htons(1); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 291 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 292 | // copy query block to answer block |
| 293 | len = answb - from; |
| 294 | memcpy(answb, from, len); |
| 295 | next += 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 |
| 298 | *(uint32_t *) next = htonl(ttl); |
| 299 | next += 4; |
| 300 | *(uint16_t *) next = htons(outr.rlen); |
| 301 | next += 2; |
| 302 | memcpy(next, (void *)answstr, outr.rlen); |
| 303 | next += outr.rlen; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 304 | |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 305 | empty_packet: |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 306 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 307 | flags = ntohs(head->flags); |
| 308 | // clear rcode and RA, set responsebit and our new flags |
| 309 | flags |= (outr.flags & 0xff80) | 0x8000; |
| 310 | head->flags = htons(flags); |
| 311 | head->nauth = head->nadd = htons(0); |
| 312 | head->nquer = htons(1); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 313 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 314 | packet_len = next - (void *)buf; |
| 315 | return packet_len; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 316 | } |
| 317 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 318 | /* |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 319 | * Exit on signal |
| 320 | */ |
Rob Landley | a6e131d | 2006-05-29 06:43:55 +0000 | [diff] [blame] | 321 | static void interrupt(int x) |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 322 | { |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 323 | unlink(LOCK_FILE); |
Denis Vlasenko | 2c91652 | 2007-01-12 14:57:37 +0000 | [diff] [blame^] | 324 | bb_error_msg("interrupt, exiting\n"); |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 325 | exit(2); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 326 | } |
| 327 | |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 328 | int dnsd_main(int argc, char **argv) |
| 329 | { |
Denis Vlasenko | 2c91652 | 2007-01-12 14:57:37 +0000 | [diff] [blame^] | 330 | char *listen_interface = NULL; |
| 331 | char *sttl, *sport; |
| 332 | len_and_sockaddr *lsa; |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 333 | int udps; |
| 334 | uint16_t port = 53; |
| 335 | uint8_t buf[MAX_PACK_LEN]; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 336 | |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 337 | getopt32(argc, argv, "i:c:t:p:dv", &listen_interface, &fileconf, &sttl, &sport); |
| 338 | //if (option_mask32 & 0x1) // -i |
| 339 | //if (option_mask32 & 0x2) // -c |
| 340 | if (option_mask32 & 0x4) // -t |
Denis Vlasenko | 2c91652 | 2007-01-12 14:57:37 +0000 | [diff] [blame^] | 341 | ttl = xatou_range(sttl, 1, 0xffffffff); |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 342 | if (option_mask32 & 0x8) // -p |
Denis Vlasenko | 2c91652 | 2007-01-12 14:57:37 +0000 | [diff] [blame^] | 343 | port = xatou_range(sttl, 1, 0xffff); |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 344 | |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 345 | if (OPT_verbose) { |
| 346 | bb_info_msg("listen_interface: %s", listen_interface); |
| 347 | bb_info_msg("ttl: %d, port: %d", ttl, port); |
| 348 | bb_info_msg("fileconf: %s", fileconf); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 349 | } |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 350 | |
Denis Vlasenko | 2c91652 | 2007-01-12 14:57:37 +0000 | [diff] [blame^] | 351 | if (OPT_daemon) { |
| 352 | //FIXME: NOMMU will NOT set LOGMODE_SYSLOG! |
Bernhard Reutner-Fischer | c418d48 | 2006-05-31 10:19:51 +0000 | [diff] [blame] | 353 | #ifdef BB_NOMMU |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 354 | /* reexec for vfork() do continue parent */ |
| 355 | vfork_daemon_rexec(1, 0, argc, argv, "-d"); |
Bernhard Reutner-Fischer | c418d48 | 2006-05-31 10:19:51 +0000 | [diff] [blame] | 356 | #else |
Rob Landley | 86b4d64 | 2006-08-03 17:58:17 +0000 | [diff] [blame] | 357 | xdaemon(1, 0); |
Bernhard Reutner-Fischer | c418d48 | 2006-05-31 10:19:51 +0000 | [diff] [blame] | 358 | #endif |
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); |
| 365 | signal(SIGPIPE, SIG_IGN); |
| 366 | signal(SIGHUP, SIG_IGN); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 367 | #ifdef SIGTSTP |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 368 | signal(SIGTSTP, SIG_IGN); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 369 | #endif |
| 370 | #ifdef SIGURG |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 371 | signal(SIGURG, SIG_IGN); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 372 | #endif |
| 373 | |
Denis Vlasenko | 2c91652 | 2007-01-12 14:57:37 +0000 | [diff] [blame^] | 374 | lsa = host2sockaddr(listen_interface, port); |
| 375 | udps = xsocket(lsa->sa.sa_family, SOCK_DGRAM, 0); |
| 376 | xbind(udps, &lsa->sa, lsa->len); |
| 377 | // xlisten(udps, 50); - ?!! DGRAM sockets are never listened on I think? |
| 378 | bb_info_msg("Accepting UDP packets on %s", |
| 379 | xmalloc_sockaddr2dotted(&lsa->sa, lsa->len)); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 380 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 381 | while (1) { |
| 382 | fd_set fdset; |
| 383 | int r; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 384 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 385 | FD_ZERO(&fdset); |
| 386 | FD_SET(udps, &fdset); |
| 387 | // Block until a message arrives |
Denis Vlasenko | 2c91652 | 2007-01-12 14:57:37 +0000 | [diff] [blame^] | 388 | // FIXME: Fantastic. select'ing on just one fd?? |
| 389 | // Why no just block on it doing recvfrom() ? |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 390 | r = select(udps + 1, &fdset, NULL, NULL, NULL); |
| 391 | if (r < 0) |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 392 | bb_perror_msg_and_die("select error"); |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 393 | if (r == 0) |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 394 | bb_perror_msg_and_die("select spurious return"); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 395 | |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 396 | /* Can this test ever be false? - yes */ |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 397 | if (FD_ISSET(udps, &fdset)) { |
Denis Vlasenko | 2c91652 | 2007-01-12 14:57:37 +0000 | [diff] [blame^] | 398 | socklen_t fromlen = lsa->len; |
| 399 | // FIXME: need to get *DEST* address (to which of our addresses |
| 400 | // this query was directed), and reply from the same address. |
| 401 | // Or else we can exhibit usual UDP ugliness: |
| 402 | // [ip1.multihomed.ip2] <= query to ip1 <= peer |
| 403 | // [ip1.multihomed.ip2] => reply from ip2 => peer (confused) |
| 404 | r = recvfrom(udps, buf, sizeof(buf), 0, &lsa->sa, &fromlen); |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 405 | if (OPT_verbose) |
Denis Vlasenko | 2c91652 | 2007-01-12 14:57:37 +0000 | [diff] [blame^] | 406 | bb_info_msg("Got UDP packet"); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 407 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 408 | if (r < 12 || r > 512) { |
| 409 | bb_error_msg("invalid packet size"); |
| 410 | continue; |
| 411 | } |
Denis Vlasenko | 2c91652 | 2007-01-12 14:57:37 +0000 | [diff] [blame^] | 412 | if (r <= 0) |
| 413 | continue; |
| 414 | r = process_packet(buf); |
| 415 | if (r <= 0) |
| 416 | continue; |
| 417 | sendto(udps, buf, r, 0, &lsa->sa, fromlen); |
| 418 | } |
| 419 | } |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 420 | } |