Bernhard Reutner-Fischer | dac7ff1 | 2006-04-12 17:55:51 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini DNS server implementation for busybox |
| 4 | * |
| 5 | * Copyright (C) 2005 Roberto A. Foglietta (me@roberto.foglietta.name) |
| 6 | * Copyright (C) 2005 Odd Arild Olsen (oao at fibula dot no) |
| 7 | * Copyright (C) 2003 Paul Sheer |
Bernhard Reutner-Fischer | 2c99851 | 2006-04-12 18:09:26 +0000 | [diff] [blame] | 8 | * |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 9 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
| 10 | * |
| 11 | * Odd Arild Olsen started out with the sheerdns [1] of Paul Sheer and rewrote |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 12 | * it into a shape which I believe is both easier to understand and maintain. |
| 13 | * I also reused the input buffer for output and removed services he did not |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 14 | * need. [1] http://threading.2038bug.com/sheerdns/ |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 15 | * |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 16 | * Some bugfix and minor changes was applied by Roberto A. Foglietta who made |
| 17 | * the first porting of oao' scdns to busybox also. |
| 18 | */ |
| 19 | |
Denis Vlasenko | 5b27fbe | 2007-03-24 14:06:51 +0000 | [diff] [blame] | 20 | #include <syslog.h> |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 21 | #include "libbb.h" |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 22 | |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 23 | //#define DEBUG 1 |
Denis Vlasenko | 2c91652 | 2007-01-12 14:57:37 +0000 | [diff] [blame] | 24 | #define DEBUG 0 |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 25 | |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 26 | enum { |
| 27 | MAX_HOST_LEN = 16, // longest host name allowed is 15 |
| 28 | IP_STRING_LEN = 18, // .xxx.xxx.xxx.xxx\0 |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 29 | |
| 30 | //must be strlen('.in-addr.arpa') larger than IP_STRING_LEN |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 31 | MAX_NAME_LEN = (IP_STRING_LEN + 13), |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 32 | |
| 33 | /* Cannot get bigger packets than 512 per RFC1035 |
| 34 | In practice this can be set considerably smaller: |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 35 | Length of response packet is header (12B) + 2*type(4B) + 2*class(4B) + |
| 36 | ttl(4B) + rlen(2B) + r (MAX_NAME_LEN =21B) + |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 37 | 2*querystring (2 MAX_NAME_LEN= 42B), all together 90 Byte |
| 38 | */ |
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 | |
Denis Vlasenko | d3bac03 | 2007-03-24 12:13:04 +0000 | [diff] [blame] | 72 | static struct dns_entry *dnsentry; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 73 | static uint32_t ttl = DEFAULT_TTL; |
| 74 | |
Denis Vlasenko | d3bac03 | 2007-03-24 12:13:04 +0000 | [diff] [blame] | 75 | static const char *fileconf = "/etc/dnsd.conf"; |
| 76 | |
| 77 | // Must match getopt32 call |
| 78 | #define OPT_daemon (option_mask32 & 0x10) |
| 79 | #define OPT_verbose (option_mask32 & 0x20) |
| 80 | |
| 81 | |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 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 | */ |
Rob Landley | a6e131d | 2006-05-29 06:43:55 +0000 | [diff] [blame] | 85 | static void convname(char *a, uint8_t *q) |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 86 | { |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 87 | int i = (q[0] == '.') ? 0 : 1; |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 88 | for (; i < MAX_HOST_LEN-1 && *q; i++, q++) |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 89 | a[i] = tolower(*q); |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 90 | a[0] = i - 1; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 91 | a[i] = 0; |
| 92 | } |
| 93 | |
| 94 | /* |
Bernhard Reutner-Fischer | c418d48 | 2006-05-31 10:19:51 +0000 | [diff] [blame] | 95 | * Insert length of substrings instead of dots |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 96 | */ |
Rob Landley | a6e131d | 2006-05-29 06:43:55 +0000 | [diff] [blame] | 97 | static void undot(uint8_t * rip) |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 98 | { |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 99 | int i = 0, s = 0; |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 100 | while (rip[i]) |
| 101 | i++; |
| 102 | for (--i; i >= 0; i--) { |
| 103 | if (rip[i] == '.') { |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 104 | rip[i] = s; |
| 105 | s = 0; |
| 106 | } else s++; |
| 107 | } |
| 108 | } |
| 109 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 110 | /* |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 111 | * Read one line of hostname/IP from file |
| 112 | * Returns 0 for each valid entry read, -1 at EOF |
| 113 | * Assumes all host names are lower case only |
Denis Vlasenko | 6cd2d2b | 2007-01-22 14:06:03 +0000 | [diff] [blame] | 114 | * Hostnames with more than one label are not handled correctly. |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 115 | * Presently the dot is copied into name without |
| 116 | * converting to a length/string substring for that label. |
| 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; |
Denis Vlasenko | 6cd2d2b | 2007-01-22 14:06:03 +0000 | [diff] [blame] | 121 | char *line, *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 | 6cd2d2b | 2007-01-22 14:06:03 +0000 | [diff] [blame] | 124 | line = 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 | 6cd2d2b | 2007-01-22 14:06:03 +0000 | [diff] [blame] | 129 | if (!*r || *r == '#' || *r == '\n') { |
| 130 | free(line); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 131 | goto restart; /* skipping empty/blank and commented lines */ |
Denis Vlasenko | 6cd2d2b | 2007-01-22 14:06:03 +0000 | [diff] [blame] | 132 | } |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 133 | } |
| 134 | name = r; |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 135 | while (*r != ' ' && *r != '\t') |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 136 | r++; |
Denis Vlasenko | 6cd2d2b | 2007-01-22 14:06:03 +0000 | [diff] [blame] | 137 | *r++ = '\0'; |
Denis Vlasenko | d3bac03 | 2007-03-24 12:13:04 +0000 | [diff] [blame] | 138 | if (sscanf(r, ".%u.%u.%u.%u"+1, &a, &b, &c, &d) != 4) { |
Denis Vlasenko | 6cd2d2b | 2007-01-22 14:06:03 +0000 | [diff] [blame] | 139 | free(line); |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 140 | goto restart; /* skipping wrong lines */ |
Denis Vlasenko | 6cd2d2b | 2007-01-22 14:06:03 +0000 | [diff] [blame] | 141 | } |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 142 | |
Denis Vlasenko | d3bac03 | 2007-03-24 12:13:04 +0000 | [diff] [blame] | 143 | sprintf(s->ip, ".%u.%u.%u.%u"+1, a, b, c, d); |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 144 | sprintf(s->rip, ".%u.%u.%u.%u", d, c, b, a); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 145 | undot((uint8_t*)s->rip); |
Denis Vlasenko | 6cd2d2b | 2007-01-22 14:06:03 +0000 | [diff] [blame] | 146 | convname(s->name, (uint8_t*)name); |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 147 | |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 148 | if (OPT_verbose) |
| 149 | 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] | 150 | |
Denis Vlasenko | 6cd2d2b | 2007-01-22 14:06:03 +0000 | [diff] [blame] | 151 | free(line); |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 152 | return 0; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | /* |
| 156 | * Read hostname/IP records from file |
| 157 | */ |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 158 | static void dnsentryinit(void) |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 159 | { |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 160 | FILE *fp; |
| 161 | struct dns_entry *m, *prev; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 162 | |
Denis Vlasenko | 6cd2d2b | 2007-01-22 14:06:03 +0000 | [diff] [blame] | 163 | prev = dnsentry = NULL; |
Rob Landley | 86b4d64 | 2006-08-03 17:58:17 +0000 | [diff] [blame] | 164 | fp = xfopen(fileconf, "r"); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 165 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 166 | while (1) { |
Denis Vlasenko | 6cd2d2b | 2007-01-22 14:06:03 +0000 | [diff] [blame] | 167 | m = xzalloc(sizeof(*m)); |
| 168 | /*m->next = NULL;*/ |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 169 | if (getfileentry(fp, m)) |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 170 | break; |
"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 | if (prev == NULL) |
| 173 | dnsentry = m; |
| 174 | else |
| 175 | prev->next = m; |
| 176 | prev = m; |
| 177 | } |
| 178 | fclose(fp); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 179 | } |
| 180 | |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 181 | /* |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 182 | * Look query up in dns records and return answer if found |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 183 | * qs is the query string, first byte the string length |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 184 | */ |
Rob Landley | a6e131d | 2006-05-29 06:43:55 +0000 | [diff] [blame] | 185 | 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] | 186 | { |
| 187 | int i; |
Denis Vlasenko | d3bac03 | 2007-03-24 12:13:04 +0000 | [diff] [blame] | 188 | struct dns_entry *d = dnsentry; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 189 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 190 | do { |
Denis Vlasenko | 91f20ab | 2007-01-20 01:47:44 +0000 | [diff] [blame] | 191 | #if DEBUG |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 192 | char *p,*q; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 193 | q = (char *)&(qs[1]); |
| 194 | p = &(d->name[1]); |
Denis Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 195 | fprintf(stderr, "\n%s: %d/%d p:%s q:%s %d", |
Denis Vlasenko | 91f20ab | 2007-01-20 01:47:44 +0000 | [diff] [blame] | 196 | __FUNCTION__, (int)strlen(p), (int)(d->name[0]), |
| 197 | p, q, (int)strlen(q)); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 198 | #endif |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 199 | if (type == REQ_A) { /* search by host name */ |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 200 | for (i = 1; i <= (int)(d->name[0]); i++) |
| 201 | if (tolower(qs[i]) != d->name[i]) |
| 202 | break; |
| 203 | if (i > (int)(d->name[0])) { |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 204 | strcpy((char *)as, d->ip); |
Denis Vlasenko | 91f20ab | 2007-01-20 01:47:44 +0000 | [diff] [blame] | 205 | #if DEBUG |
Denis Vlasenko | d3bac03 | 2007-03-24 12:13:04 +0000 | [diff] [blame] | 206 | fprintf(stderr, " OK as:%s\n", as); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 207 | #endif |
Denis Vlasenko | d3bac03 | 2007-03-24 12:13:04 +0000 | [diff] [blame] | 208 | return 0; |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 209 | } |
Denis Vlasenko | d3bac03 | 2007-03-24 12:13:04 +0000 | [diff] [blame] | 210 | } else if (type == REQ_PTR) { /* search by IP-address */ |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 211 | 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] | 212 | strcpy((char *)as, d->name); |
| 213 | return 0; |
| 214 | } |
| 215 | } |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 216 | d = d->next; |
| 217 | } while (d); |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 218 | return -1; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 222 | /* |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 223 | * Decode message and generate answer |
| 224 | */ |
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; |
Denis Vlasenko | d3bac03 | 2007-03-24 12:13:04 +0000 | [diff] [blame] | 239 | if (head->nquer == 0) { |
| 240 | bb_error_msg("no queries"); |
Denis Vlasenko | b5b45a9 | 2007-03-24 13:09:07 +0000 | [diff] [blame] | 241 | return -1; |
Denis Vlasenko | d3bac03 | 2007-03-24 12:13:04 +0000 | [diff] [blame] | 242 | } |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 243 | |
Denis Vlasenko | d3bac03 | 2007-03-24 12:13:04 +0000 | [diff] [blame] | 244 | if (head->flags & 0x8000) { |
| 245 | bb_error_msg("ignoring response packet"); |
Denis Vlasenko | b5b45a9 | 2007-03-24 13:09:07 +0000 | [diff] [blame] | 246 | return -1; |
Denis Vlasenko | d3bac03 | 2007-03-24 12:13:04 +0000 | [diff] [blame] | 247 | } |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 248 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 249 | from = (void *)&head[1]; // start of query string |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 250 | 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] | 251 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 252 | outr.rlen = 0; // may change later |
| 253 | outr.r = NULL; |
| 254 | outr.flags = 0; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 255 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 256 | qprop = (struct dns_prop *)(answb - 4); |
| 257 | type = ntohs(qprop->type); |
"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 | // only let REQ_A and REQ_PTR pass |
| 260 | if (!(type == REQ_A || type == REQ_PTR)) { |
| 261 | goto empty_packet; /* we can't handle the query type */ |
| 262 | } |
"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 | if (ntohs(qprop->class) != 1 /* class INET */ ) { |
| 265 | outr.flags = 4; /* not supported */ |
| 266 | goto empty_packet; |
| 267 | } |
| 268 | /* we only support standard queries */ |
"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 | if ((ntohs(head->flags) & 0x7800) != 0) |
| 271 | goto empty_packet; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 272 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 273 | // We have a standard query |
Denis Vlasenko | 2c91652 | 2007-01-12 14:57:37 +0000 | [diff] [blame] | 274 | bb_info_msg("%s", (char *)from); |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 275 | lookup_result = table_lookup(type, answstr, (uint8_t*)from); |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 276 | if (lookup_result != 0) { |
| 277 | outr.flags = 3 | 0x0400; //name do not exist and auth |
| 278 | goto empty_packet; |
| 279 | } |
| 280 | if (type == REQ_A) { // return an address |
| 281 | struct in_addr a; |
| 282 | if (!inet_aton((char*)answstr, &a)) {//dotted dec to long conv |
| 283 | outr.flags = 1; /* Frmt err */ |
| 284 | goto empty_packet; |
| 285 | } |
| 286 | memcpy(answstr, &a.s_addr, 4); // save before a disappears |
| 287 | outr.rlen = 4; // uint32_t IP |
Denis Vlasenko | d3bac03 | 2007-03-24 12:13:04 +0000 | [diff] [blame] | 288 | } else |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 289 | outr.rlen = strlen((char *)answstr) + 1; // a host name |
| 290 | outr.r = answstr; // 32 bit ip or a host name |
| 291 | outr.flags |= 0x0400; /* authority-bit */ |
| 292 | // we have an answer |
| 293 | head->nansw = htons(1); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 294 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 295 | // copy query block to answer block |
| 296 | len = answb - from; |
| 297 | memcpy(answb, from, len); |
| 298 | next += len; |
"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 | // and append answer rr |
| 301 | *(uint32_t *) next = htonl(ttl); |
| 302 | next += 4; |
| 303 | *(uint16_t *) next = htons(outr.rlen); |
| 304 | next += 2; |
| 305 | memcpy(next, (void *)answstr, outr.rlen); |
| 306 | next += outr.rlen; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 307 | |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 308 | empty_packet: |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 309 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 310 | flags = ntohs(head->flags); |
| 311 | // clear rcode and RA, set responsebit and our new flags |
| 312 | flags |= (outr.flags & 0xff80) | 0x8000; |
| 313 | head->flags = htons(flags); |
| 314 | head->nauth = head->nadd = htons(0); |
| 315 | head->nquer = htons(1); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 316 | |
Denis Vlasenko | d3bac03 | 2007-03-24 12:13:04 +0000 | [diff] [blame] | 317 | packet_len = (uint8_t *)next - buf; |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 318 | return packet_len; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 319 | } |
| 320 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 321 | /* |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 322 | * Exit on signal |
| 323 | */ |
Rob Landley | a6e131d | 2006-05-29 06:43:55 +0000 | [diff] [blame] | 324 | static void interrupt(int x) |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 325 | { |
Denis Vlasenko | d3bac03 | 2007-03-24 12:13:04 +0000 | [diff] [blame] | 326 | /* unlink("/var/run/dnsd.lock"); */ |
Denis Vlasenko | 2c91652 | 2007-01-12 14:57:37 +0000 | [diff] [blame] | 327 | bb_error_msg("interrupt, exiting\n"); |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 328 | exit(2); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 331 | int dnsd_main(int argc, char **argv); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 332 | int dnsd_main(int argc, char **argv) |
| 333 | { |
Denis Vlasenko | d3bac03 | 2007-03-24 12:13:04 +0000 | [diff] [blame] | 334 | const char *listen_interface = "0.0.0.0"; |
Denis Vlasenko | 2c91652 | 2007-01-12 14:57:37 +0000 | [diff] [blame] | 335 | char *sttl, *sport; |
| 336 | len_and_sockaddr *lsa; |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 337 | int udps; |
| 338 | uint16_t port = 53; |
| 339 | uint8_t buf[MAX_PACK_LEN]; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 340 | |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 341 | getopt32(argc, argv, "i:c:t:p:dv", &listen_interface, &fileconf, &sttl, &sport); |
| 342 | //if (option_mask32 & 0x1) // -i |
| 343 | //if (option_mask32 & 0x2) // -c |
| 344 | if (option_mask32 & 0x4) // -t |
Denis Vlasenko | 2c91652 | 2007-01-12 14:57:37 +0000 | [diff] [blame] | 345 | ttl = xatou_range(sttl, 1, 0xffffffff); |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 346 | if (option_mask32 & 0x8) // -p |
Denis Vlasenko | d3bac03 | 2007-03-24 12:13:04 +0000 | [diff] [blame] | 347 | port = xatou_range(sport, 1, 0xffff); |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 348 | |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 349 | if (OPT_verbose) { |
| 350 | bb_info_msg("listen_interface: %s", listen_interface); |
| 351 | bb_info_msg("ttl: %d, port: %d", ttl, port); |
| 352 | bb_info_msg("fileconf: %s", fileconf); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 353 | } |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 354 | |
Denis Vlasenko | 2c91652 | 2007-01-12 14:57:37 +0000 | [diff] [blame] | 355 | if (OPT_daemon) { |
Denis Vlasenko | 5a14202 | 2007-03-26 13:20:54 +0000 | [diff] [blame] | 356 | bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv); |
Denis Vlasenko | 5b27fbe | 2007-03-24 14:06:51 +0000 | [diff] [blame] | 357 | openlog(applet_name, LOG_PID, LOG_DAEMON); |
Denis Vlasenko | 2c91652 | 2007-01-12 14:57:37 +0000 | [diff] [blame] | 358 | logmode = LOGMODE_SYSLOG; |
| 359 | } |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 360 | |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 361 | dnsentryinit(); |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 362 | |
| 363 | signal(SIGINT, interrupt); |
Denis Vlasenko | d3bac03 | 2007-03-24 12:13:04 +0000 | [diff] [blame] | 364 | /* why? signal(SIGPIPE, SIG_IGN); */ |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 365 | signal(SIGHUP, SIG_IGN); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 366 | #ifdef SIGTSTP |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 367 | signal(SIGTSTP, SIG_IGN); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 368 | #endif |
| 369 | #ifdef SIGURG |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 370 | signal(SIGURG, SIG_IGN); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 371 | #endif |
| 372 | |
Denis Vlasenko | d3bac03 | 2007-03-24 12:13:04 +0000 | [diff] [blame] | 373 | lsa = xdotted2sockaddr(listen_interface, port); |
Denis Vlasenko | 2c91652 | 2007-01-12 14:57:37 +0000 | [diff] [blame] | 374 | udps = xsocket(lsa->sa.sa_family, SOCK_DGRAM, 0); |
| 375 | xbind(udps, &lsa->sa, lsa->len); |
Denis Vlasenko | d3bac03 | 2007-03-24 12:13:04 +0000 | [diff] [blame] | 376 | /* xlisten(udps, 50); - ?!! DGRAM sockets are never listened on I think? */ |
Denis Vlasenko | 2c91652 | 2007-01-12 14:57:37 +0000 | [diff] [blame] | 377 | bb_info_msg("Accepting UDP packets on %s", |
| 378 | xmalloc_sockaddr2dotted(&lsa->sa, lsa->len)); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 379 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 380 | while (1) { |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 381 | int r; |
Denis Vlasenko | d3bac03 | 2007-03-24 12:13:04 +0000 | [diff] [blame] | 382 | socklen_t fromlen = lsa->len; |
Denis Vlasenko | 2c91652 | 2007-01-12 14:57:37 +0000 | [diff] [blame] | 383 | // FIXME: need to get *DEST* address (to which of our addresses |
| 384 | // this query was directed), and reply from the same address. |
| 385 | // Or else we can exhibit usual UDP ugliness: |
| 386 | // [ip1.multihomed.ip2] <= query to ip1 <= peer |
| 387 | // [ip1.multihomed.ip2] => reply from ip2 => peer (confused) |
Denis Vlasenko | d3bac03 | 2007-03-24 12:13:04 +0000 | [diff] [blame] | 388 | r = recvfrom(udps, buf, sizeof(buf), 0, &lsa->sa, &fromlen); |
| 389 | if (OPT_verbose) |
| 390 | bb_info_msg("Got UDP packet"); |
| 391 | if (r < 12 || r > 512) { |
| 392 | bb_error_msg("invalid packet size"); |
| 393 | continue; |
Denis Vlasenko | 2c91652 | 2007-01-12 14:57:37 +0000 | [diff] [blame] | 394 | } |
Denis Vlasenko | d3bac03 | 2007-03-24 12:13:04 +0000 | [diff] [blame] | 395 | r = process_packet(buf); |
| 396 | if (r <= 0) |
| 397 | continue; |
| 398 | sendto(udps, buf, r, 0, &lsa->sa, fromlen); |
Denis Vlasenko | 2c91652 | 2007-01-12 14:57:37 +0000 | [diff] [blame] | 399 | } |
Denis Vlasenko | b5b45a9 | 2007-03-24 13:09:07 +0000 | [diff] [blame] | 400 | return 0; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 401 | } |