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