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 | |
| 20 | #include <unistd.h> |
| 21 | #include <string.h> |
| 22 | #include <signal.h> |
| 23 | #include <arpa/inet.h> |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 24 | #include <sys/socket.h> |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 25 | #include <ctype.h> |
Bernhard Reutner-Fischer | 421d9e5 | 2006-04-03 16:39:31 +0000 | [diff] [blame] | 26 | #include "busybox.h" |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 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 | |
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; |
| 79 | static int daemonmode = 0; |
| 80 | static uint32_t ttl = DEFAULT_TTL; |
| 81 | |
| 82 | /* |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 83 | * Convert host name from C-string to dns length/string. |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 84 | */ |
| 85 | static void |
| 86 | convname(char *a, uint8_t *q) |
| 87 | { |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 88 | int i = (q[0] == '.') ? 0 : 1; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 89 | for(; i < MAX_HOST_LEN-1 && *q; i++, q++) |
| 90 | a[i] = tolower(*q); |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 91 | a[0] = i - 1; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 92 | a[i] = 0; |
| 93 | } |
| 94 | |
| 95 | /* |
| 96 | * Insert length of substrings insetad of dots |
| 97 | */ |
| 98 | static void |
| 99 | undot(uint8_t * rip) |
| 100 | { |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 101 | int i = 0, s = 0; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 102 | while(rip[i]) i++; |
| 103 | for(--i; i >= 0; i--) { |
| 104 | if(rip[i] == '.') { |
| 105 | rip[i] = s; |
| 106 | s = 0; |
| 107 | } else s++; |
| 108 | } |
| 109 | } |
| 110 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 111 | /* |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 112 | * Append message to log file |
| 113 | */ |
| 114 | static void |
| 115 | log_message(char *filename, char *message) |
| 116 | { |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 117 | FILE *logfile; |
| 118 | if (!daemonmode) |
| 119 | return; |
| 120 | logfile = fopen(filename, "a"); |
| 121 | if (!logfile) |
| 122 | return; |
| 123 | fprintf(logfile, "%s\n", message); |
| 124 | fclose(logfile); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 125 | } |
| 126 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 127 | /* |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 128 | * Read one line of hostname/IP from file |
| 129 | * Returns 0 for each valid entry read, -1 at EOF |
| 130 | * Assumes all host names are lower case only |
| 131 | * Hostnames with more than one label is not handled correctly. |
| 132 | * Presently the dot is copied into name without |
| 133 | * converting to a length/string substring for that label. |
| 134 | */ |
| 135 | |
| 136 | static int |
| 137 | getfileentry(FILE * fp, struct dns_entry *s, int verb) |
| 138 | { |
| 139 | unsigned int a,b,c,d; |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 140 | char *r, *name; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 141 | |
| 142 | restart: |
| 143 | if(!(r = bb_get_line_from_file(fp))) |
| 144 | return -1; |
| 145 | while(*r == ' ' || *r == '\t') { |
| 146 | r++; |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 147 | if(!*r || *r == '#' || *r == '\n') |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 148 | goto restart; /* skipping empty/blank and commented lines */ |
| 149 | } |
| 150 | name = r; |
| 151 | while(*r != ' ' && *r != '\t') |
| 152 | r++; |
| 153 | *r++ = 0; |
| 154 | if(sscanf(r,"%u.%u.%u.%u",&a,&b,&c,&d) != 4) |
| 155 | goto restart; /* skipping wrong lines */ |
| 156 | |
| 157 | sprintf(s->ip,"%u.%u.%u.%u",a,b,c,d); |
| 158 | sprintf(s->rip,".%u.%u.%u.%u",d,c,b,a); |
| 159 | undot((uint8_t*)s->rip); |
| 160 | convname(s->name,(uint8_t*)name); |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 161 | |
| 162 | if(verb) |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 163 | fprintf(stderr,"\tname:%s, ip:%s\n",&(s->name[1]),s->ip); |
| 164 | |
| 165 | return 0; /* warningkiller */ |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | * Read hostname/IP records from file |
| 170 | */ |
| 171 | static void |
| 172 | dnsentryinit(int verb) |
| 173 | { |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 174 | FILE *fp; |
| 175 | struct dns_entry *m, *prev; |
| 176 | prev = dnsentry = NULL; |
"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 | if(!(fp = fopen(fileconf, "r"))) |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 179 | bb_perror_msg_and_die("open %s",fileconf); |
| 180 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 181 | while (1) { |
| 182 | if(!(m = (struct dns_entry *)malloc(sizeof(struct dns_entry)))) |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 183 | bb_perror_msg_and_die("malloc dns_entry"); |
| 184 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 185 | m->next = NULL; |
| 186 | if (getfileentry(fp, m, verb)) |
| 187 | break; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 188 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 189 | if (prev == NULL) |
| 190 | dnsentry = m; |
| 191 | else |
| 192 | prev->next = m; |
| 193 | prev = m; |
| 194 | } |
| 195 | fclose(fp); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | |
| 199 | /* |
| 200 | * Set up UDP socket |
| 201 | */ |
| 202 | static int |
| 203 | listen_socket(char *iface_addr, int listen_port) |
| 204 | { |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 205 | struct sockaddr_in a; |
| 206 | char msg[100]; |
| 207 | int s; |
| 208 | int yes = 1; |
Bernhard Reutner-Fischer | dac7ff1 | 2006-04-12 17:55:51 +0000 | [diff] [blame] | 209 | s = bb_xsocket(PF_INET, SOCK_DGRAM, 0); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 210 | #ifdef SO_REUSEADDR |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 211 | if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&yes, sizeof(yes)) < 0) |
| 212 | bb_perror_msg_and_die("setsockopt() failed"); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 213 | #endif |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 214 | memset(&a, 0, sizeof(a)); |
| 215 | a.sin_port = htons(listen_port); |
| 216 | a.sin_family = AF_INET; |
| 217 | if (!inet_aton(iface_addr, &a.sin_addr)) |
| 218 | bb_perror_msg_and_die("bad iface address"); |
Bernhard Reutner-Fischer | 67f641e | 2006-04-12 18:24:37 +0000 | [diff] [blame] | 219 | bb_xbind(s, (struct sockaddr *)&a, sizeof(a)); |
| 220 | listen(s, 50); /* bb_xlisten? */ |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 221 | sprintf(msg, "accepting UDP packets on addr:port %s:%d\n", |
| 222 | iface_addr, (int)listen_port); |
| 223 | log_message(LOG_FILE, msg); |
| 224 | return s; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | /* |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 228 | * Look query up in dns records and return answer if found |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 229 | * qs is the query string, first byte the string length |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 230 | */ |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 231 | static int |
| 232 | table_lookup(uint16_t type, uint8_t * as, uint8_t * qs) |
| 233 | { |
| 234 | int i; |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 235 | struct dns_entry *d=dnsentry; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 236 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 237 | do { |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 238 | #ifdef DEBUG |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 239 | char *p,*q; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 240 | q = (char *)&(qs[1]); |
| 241 | p = &(d->name[1]); |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 242 | 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] | 243 | #endif |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 244 | if (type == REQ_A) { /* search by host name */ |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 245 | for(i = 1; i <= (int)(d->name[0]); i++) |
| 246 | if(tolower(qs[i]) != d->name[i]) |
| 247 | continue; |
| 248 | #ifdef DEBUG |
| 249 | fprintf(stderr, " OK"); |
| 250 | #endif |
| 251 | strcpy((char *)as, d->ip); |
| 252 | #ifdef DEBUG |
| 253 | fprintf(stderr, " %s ", as); |
| 254 | #endif |
| 255 | return 0; |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 256 | } |
| 257 | else if (type == REQ_PTR) { /* search by IP-address */ |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 258 | 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] | 259 | strcpy((char *)as, d->name); |
| 260 | return 0; |
| 261 | } |
| 262 | } |
| 263 | } while ((d = d->next) != NULL); |
| 264 | return -1; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 268 | /* |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 269 | * Decode message and generate answer |
| 270 | */ |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 271 | #define eret(s) do { fprintf (stderr, "%s\n", s); return -1; } while (0) |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 272 | static int |
| 273 | process_packet(uint8_t * buf) |
| 274 | { |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 275 | struct dns_head *head; |
| 276 | struct dns_prop *qprop; |
| 277 | struct dns_repl outr; |
| 278 | void *next, *from, *answb; |
"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 | uint8_t answstr[MAX_NAME_LEN + 1]; |
| 281 | int lookup_result, type, len, packet_len; |
| 282 | uint16_t flags; |
"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 | answstr[0] = '\0'; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 285 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 286 | head = (struct dns_head *)buf; |
| 287 | if (head->nquer == 0) |
| 288 | eret("no queries"); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 289 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 290 | if ((head->flags & 0x8000)) |
| 291 | eret("ignoring response packet"); |
"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 | from = (void *)&head[1]; // start of query string |
| 294 | 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] | 295 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 296 | outr.rlen = 0; // may change later |
| 297 | outr.r = NULL; |
| 298 | outr.flags = 0; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 299 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 300 | qprop = (struct dns_prop *)(answb - 4); |
| 301 | type = ntohs(qprop->type); |
"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 | // only let REQ_A and REQ_PTR pass |
| 304 | if (!(type == REQ_A || type == REQ_PTR)) { |
| 305 | goto empty_packet; /* we can't handle the query type */ |
| 306 | } |
"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 | if (ntohs(qprop->class) != 1 /* class INET */ ) { |
| 309 | outr.flags = 4; /* not supported */ |
| 310 | goto empty_packet; |
| 311 | } |
| 312 | /* we only support standard queries */ |
"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 | if ((ntohs(head->flags) & 0x7800) != 0) |
| 315 | goto empty_packet; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 316 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 317 | // We have a standard query |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 318 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 319 | log_message(LOG_FILE, (char *)head); |
| 320 | lookup_result = table_lookup(type, answstr, (uint8_t*)(&head[1])); |
| 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); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 340 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 341 | // copy query block to answer block |
| 342 | len = answb - from; |
| 343 | memcpy(answb, from, len); |
| 344 | next += len; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 345 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 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; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 353 | |
| 354 | empty_packet: |
| 355 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 356 | flags = ntohs(head->flags); |
| 357 | // clear rcode and RA, set responsebit and our new flags |
| 358 | flags |= (outr.flags & 0xff80) | 0x8000; |
| 359 | head->flags = htons(flags); |
| 360 | head->nauth = head->nadd = htons(0); |
| 361 | head->nquer = htons(1); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 362 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 363 | packet_len = next - (void *)buf; |
| 364 | return packet_len; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 365 | } |
| 366 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 367 | /* |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 368 | * Exit on signal |
| 369 | */ |
| 370 | static void |
| 371 | interrupt(int x) |
| 372 | { |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 373 | unlink(LOCK_FILE); |
| 374 | write(2, "interrupt exiting\n", 18); |
| 375 | exit(2); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | #define is_daemon() (flags&16) |
| 379 | #define is_verbose() (flags&32) |
| 380 | //#define DEBUG 1 |
| 381 | |
| 382 | int dnsd_main(int argc, char **argv) |
| 383 | { |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 384 | int udps; |
| 385 | uint16_t port = 53; |
| 386 | uint8_t buf[MAX_PACK_LEN]; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 387 | unsigned long flags = 0; |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 388 | char *listen_interface = "0.0.0.0"; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 389 | char *sttl=NULL, *sport=NULL; |
| 390 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 391 | if(argc > 1) |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 392 | flags = bb_getopt_ulflags(argc, argv, "i:c:t:p:dv", &listen_interface, &fileconf, &sttl, &sport); |
| 393 | if(sttl) |
| 394 | if(!(ttl = atol(sttl))) |
| 395 | bb_show_usage(); |
| 396 | if(sport) |
| 397 | if(!(port = atol(sport))) |
| 398 | bb_show_usage(); |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 399 | |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 400 | if(is_verbose()) { |
| 401 | fprintf(stderr,"listen_interface: %s\n", listen_interface); |
| 402 | fprintf(stderr,"ttl: %d, port: %d\n", ttl, port); |
| 403 | fprintf(stderr,"fileconf: %s\n", fileconf); |
| 404 | } |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 405 | |
| 406 | if(is_daemon()) |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 407 | #if defined(__uClinux__) |
| 408 | /* reexec for vfork() do continue parent */ |
| 409 | vfork_daemon_rexec(1, 0, argc, argv, "-d"); |
| 410 | #else /* uClinux */ |
Bernhard Reutner-Fischer | 2c99851 | 2006-04-12 18:09:26 +0000 | [diff] [blame] | 411 | bb_xdaemon(1, 0); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 412 | #endif /* uClinuvx */ |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 413 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 414 | dnsentryinit(is_verbose()); |
| 415 | |
| 416 | signal(SIGINT, interrupt); |
| 417 | signal(SIGPIPE, SIG_IGN); |
| 418 | signal(SIGHUP, SIG_IGN); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 419 | #ifdef SIGTSTP |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 420 | signal(SIGTSTP, SIG_IGN); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 421 | #endif |
| 422 | #ifdef SIGURG |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 423 | signal(SIGURG, SIG_IGN); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 424 | #endif |
| 425 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 426 | udps = listen_socket(listen_interface, port); |
| 427 | if (udps < 0) |
| 428 | exit(1); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 429 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 430 | while (1) { |
| 431 | fd_set fdset; |
| 432 | int r; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 433 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 434 | FD_ZERO(&fdset); |
| 435 | FD_SET(udps, &fdset); |
| 436 | // Block until a message arrives |
| 437 | if((r = select(udps + 1, &fdset, NULL, NULL, NULL)) < 0) |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 438 | bb_perror_msg_and_die("select error"); |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 439 | else |
| 440 | if(r == 0) |
| 441 | bb_perror_msg_and_die("select spurious return"); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 442 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 443 | /* Can this test ever be false? */ |
| 444 | if (FD_ISSET(udps, &fdset)) { |
| 445 | struct sockaddr_in from; |
| 446 | int fromlen = sizeof(from); |
| 447 | r = recvfrom(udps, buf, sizeof(buf), 0, |
| 448 | (struct sockaddr *)&from, |
| 449 | (void *)&fromlen); |
| 450 | if(is_verbose()) |
| 451 | fprintf(stderr, "\n--- Got UDP "); |
| 452 | log_message(LOG_FILE, "\n--- Got UDP "); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 453 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 454 | if (r < 12 || r > 512) { |
| 455 | bb_error_msg("invalid packet size"); |
| 456 | continue; |
| 457 | } |
| 458 | if (r > 0) { |
| 459 | r = process_packet(buf); |
| 460 | if (r > 0) |
| 461 | sendto(udps, buf, |
| 462 | r, 0, (struct sockaddr *)&from, |
| 463 | fromlen); |
| 464 | } |
| 465 | } // end if |
| 466 | } // end while |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | |