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