blob: 1a99040ac203c999f82efa4531b99038fb79919a [file] [log] [blame]
Bernhard Reutner-Fischerdac7ff12006-04-12 17:55:51 +00001/* vi: set sw=4 ts=4: */
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +00002/*
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-Fischer2c998512006-04-12 18:09:26 +00008 *
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +00009 * 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"2e5ee8e2006-01-25 14:40:24 +000012 * 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"7b4aa6f2006-01-25 14:19:11 +000014 * need. [1] http://threading.2038bug.com/sheerdns/
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000015 *
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000016 * 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 Vlasenkob6adbf12007-05-26 19:00:18 +000020#include "libbb.h"
Bernhard Reutner-Fischerf4701962008-01-27 12:50:12 +000021#include <syslog.h>
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000022
Denis Vlasenkoc12f5302006-10-06 09:49:47 +000023//#define DEBUG 1
Denis Vlasenko2c916522007-01-12 14:57:37 +000024#define DEBUG 0
Denis Vlasenkoc12f5302006-10-06 09:49:47 +000025
Rob Landleybc68cd12006-03-10 19:22:06 +000026enum {
Denis Vlasenkoef1b4392009-04-12 19:03:01 +000027 /* can tweak this */
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000028 DEFAULT_TTL = 120,
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000029
Denis Vlasenkoef1b4392009-04-12 19:03:01 +000030 /* cannot get bigger packets than 512 per RFC1035. */
Denis Vlasenko081eb712008-03-17 09:02:21 +000031 MAX_PACK_LEN = 512,
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000032 IP_STRING_LEN = sizeof(".xxx.xxx.xxx.xxx"),
33 MAX_NAME_LEN = IP_STRING_LEN - 1 + sizeof(".in-addr.arpa"),
Rob Landleybc68cd12006-03-10 19:22:06 +000034 REQ_A = 1,
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000035 REQ_PTR = 12,
Rob Landleybc68cd12006-03-10 19:22:06 +000036};
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000037
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000038/* the message from client and first part of response msg */
39struct dns_head {
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000040 uint16_t id;
41 uint16_t flags;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000042 uint16_t nquer;
43 uint16_t nansw;
44 uint16_t nauth;
45 uint16_t nadd;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000046};
Denys Vlasenko6646de02010-04-26 14:25:33 +020047/* Structure used to access type and class fields.
48 * They are totally unaligned, but gcc 4.3.4 thinks that pointer of type uint16_t*
49 * is 16-bit aligned and replaces 16-bit memcpy (in move_from_unaligned16 macro)
50 * with aligned halfword access on arm920t!
51 * Oh well. Slapping PACKED everywhere seems to help: */
Denys Vlasenko5fb38492010-02-06 22:48:10 +010052struct type_and_class {
Denys Vlasenko6646de02010-04-26 14:25:33 +020053 uint16_t type PACKED;
54 uint16_t class PACKED;
55} PACKED;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000056/* element of known name, ip address and reversed ip address */
57struct dns_entry {
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000058 struct dns_entry *next;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000059 uint32_t ip;
60 char rip[IP_STRING_LEN]; /* length decimal reversed IP */
61 char name[1];
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000062};
63
Denys Vlasenko343dfd72010-02-07 02:45:03 +010064#define OPT_verbose (option_mask32 & 1)
65#define OPT_silent (option_mask32 & 2)
Denis Vlasenkod3bac032007-03-24 12:13:04 +000066
67
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000068/*
Bernhard Reutner-Fischerc418d482006-05-31 10:19:51 +000069 * Insert length of substrings instead of dots
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000070 */
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000071static void undot(char *rip)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000072{
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000073 int i = 0;
74 int s = 0;
75
Denis Vlasenkoc12f5302006-10-06 09:49:47 +000076 while (rip[i])
77 i++;
78 for (--i; i >= 0; i--) {
79 if (rip[i] == '.') {
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000080 rip[i] = s;
81 s = 0;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000082 } else {
83 s++;
84 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000085 }
86}
87
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000088/*
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000089 * Read hostname/IP records from file
90 */
Denis Vlasenkoa19e6492009-03-11 14:40:00 +000091static struct dns_entry *parse_conf_file(const char *fileconf)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000092{
Denis Vlasenko084266e2008-07-26 23:08:31 +000093 char *token[2];
Denis Vlasenkoa34f1ed2008-07-20 17:48:59 +000094 parser_t *parser;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000095 struct dns_entry *m, *conf_data;
96 struct dns_entry **nextp;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000097
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000098 conf_data = NULL;
99 nextp = &conf_data;
100
Denis Vlasenkoa34f1ed2008-07-20 17:48:59 +0000101 parser = config_open(fileconf);
Denis Vlasenko084266e2008-07-26 23:08:31 +0000102 while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) {
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000103 struct in_addr ip;
104 uint32_t v32;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000105
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000106 if (inet_aton(token[1], &ip) == 0) {
107 bb_error_msg("error at line %u, skipping", parser->lineno);
108 continue;
109 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000110
Denis Vlasenko084266e2008-07-26 23:08:31 +0000111 if (OPT_verbose)
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000112 bb_error_msg("name:%s, ip:%s", token[0], token[1]);
Denis Vlasenkoa34f1ed2008-07-20 17:48:59 +0000113
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000114 /* sizeof(*m) includes 1 byte for m->name[0] */
115 m = xzalloc(sizeof(*m) + strlen(token[0]) + 1);
116 /*m->next = NULL;*/
117 *nextp = m;
118 nextp = &m->next;
119
120 m->name[0] = '.';
121 strcpy(m->name + 1, token[0]);
122 undot(m->name);
123 m->ip = ip.s_addr; /* in network order */
124 v32 = ntohl(m->ip);
125 /* inverted order */
126 sprintf(m->rip, ".%u.%u.%u.%u",
127 (uint8_t)(v32),
128 (uint8_t)(v32 >> 8),
129 (uint8_t)(v32 >> 16),
130 (v32 >> 24)
131 );
132 undot(m->rip);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000133 }
Denis Vlasenko084266e2008-07-26 23:08:31 +0000134 config_close(parser);
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000135 return conf_data;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000136}
137
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000138/*
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000139 * Look query up in dns records and return answer if found.
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000140 */
Denis Vlasenko5c329932009-04-12 12:16:21 +0000141static char *table_lookup(struct dns_entry *d,
142 uint16_t type,
143 char* query_string)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000144{
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000145 while (d) {
146 unsigned len = d->name[0];
147 /* d->name[len] is the last (non NUL) char */
Denis Vlasenko91f20ab2007-01-20 01:47:44 +0000148#if DEBUG
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000149 char *p, *q;
Denis Vlasenko5c329932009-04-12 12:16:21 +0000150 q = query_string + 1;
151 p = d->name + 1;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000152 fprintf(stderr, "%d/%d p:%s q:%s %d\n",
153 (int)strlen(p), len,
154 p, q, (int)strlen(q)
155 );
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000156#endif
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000157 if (type == htons(REQ_A)) {
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000158 /* search by host name */
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000159 if (len != 1 || d->name[1] != '*') {
Denis Vlasenko5c329932009-04-12 12:16:21 +0000160/* we are lax, hope no name component is ever >64 so that length
161 * (which will be represented as 'A','B'...) matches a lowercase letter.
162 * Actually, I think false matches are hard to construct.
163 * Example.
164 * [31] len is represented as '1', [65] as 'A', [65+32] as 'a'.
165 * [65] <65 same chars>[31]<31 same chars>NUL
166 * [65+32]<65 same chars>1 <31 same chars>NUL
167 * This example seems to be the minimal case when false match occurs.
168 */
169 if (strcasecmp(d->name, query_string) != 0)
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000170 goto next;
171 }
Denis Vlasenko5c329932009-04-12 12:16:21 +0000172 return (char *)&d->ip;
Denis Vlasenko91f20ab2007-01-20 01:47:44 +0000173#if DEBUG
Denis Vlasenko5c329932009-04-12 12:16:21 +0000174 fprintf(stderr, "Found IP:%x\n", (int)d->ip);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000175#endif
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000176 return 0;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000177 }
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000178 /* search by IP-address */
179 if ((len != 1 || d->name[1] != '*')
Denis Vlasenko5c329932009-04-12 12:16:21 +0000180 /* we assume (do not check) that query_string
181 * ends in ".in-addr.arpa" */
182 && strncmp(d->rip, query_string, strlen(d->rip)) == 0
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000183 ) {
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000184#if DEBUG
Denis Vlasenko5c329932009-04-12 12:16:21 +0000185 fprintf(stderr, "Found name:%s\n", d->name);
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000186#endif
Denis Vlasenko5c329932009-04-12 12:16:21 +0000187 return d->name;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000188 }
189 next:
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000190 d = d->next;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000191 }
192
Denis Vlasenko5c329932009-04-12 12:16:21 +0000193 return NULL;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000194}
195
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000196/*
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000197 * Decode message and generate answer
198 */
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000199/* RFC 1035
200...
201Whenever an octet represents a numeric quantity, the left most bit
202in the diagram is the high order or most significant bit.
203That is, the bit labeled 0 is the most significant bit.
204...
205
2064.1.1. Header section format
Denis Vlasenko5c329932009-04-12 12:16:21 +0000207 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000208 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
209 | ID |
210 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
Denis Vlasenko5c329932009-04-12 12:16:21 +0000211 |QR| OPCODE |AA|TC|RD|RA| 0 0 0| RCODE |
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000212 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
213 | QDCOUNT |
214 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
215 | ANCOUNT |
216 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
217 | NSCOUNT |
218 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
219 | ARCOUNT |
220 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
Denis Vlasenko5c329932009-04-12 12:16:21 +0000221ID 16 bit random identifier assigned by querying peer.
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000222 Used to match query/response.
223QR message is a query (0), or a response (1).
224OPCODE 0 standard query (QUERY)
225 1 inverse query (IQUERY)
226 2 server status request (STATUS)
Denis Vlasenko5c329932009-04-12 12:16:21 +0000227AA Authoritative Answer - this bit is valid in responses.
228 Responding name server is an authority for the domain name
Denis Vlasenkoef1b4392009-04-12 19:03:01 +0000229 in question section. Answer section may have multiple owner names
230 because of aliases. The AA bit corresponds to the name which matches
231 the query name, or the first owner name in the answer section.
Denis Vlasenko5c329932009-04-12 12:16:21 +0000232TC TrunCation - this message was truncated.
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000233RD Recursion Desired - this bit may be set in a query and
234 is copied into the response. If RD is set, it directs
235 the name server to pursue the query recursively.
236 Recursive query support is optional.
237RA Recursion Available - this be is set or cleared in a
238 response, and denotes whether recursive query support is
239 available in the name server.
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000240RCODE Response code.
241 0 No error condition
242 1 Format error
Denis Vlasenko5c329932009-04-12 12:16:21 +0000243 2 Server failure - server was unable to process the query
Denis Vlasenkoef1b4392009-04-12 19:03:01 +0000244 due to a problem with the name server.
Denis Vlasenko5c329932009-04-12 12:16:21 +0000245 3 Name Error - meaningful only for responses from
Denis Vlasenkoef1b4392009-04-12 19:03:01 +0000246 an authoritative name server. The referenced domain name
247 does not exist.
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000248 4 Not Implemented.
249 5 Refused.
250QDCOUNT number of entries in the question section.
Denis Vlasenko5c329932009-04-12 12:16:21 +0000251ANCOUNT number of records in the answer section.
252NSCOUNT number of records in the authority records section.
253ARCOUNT number of records in the additional records section.
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000254
2554.1.2. Question section format
256
Denis Vlasenko5c329932009-04-12 12:16:21 +0000257The section contains QDCOUNT (usually 1) entries, each of this format:
258 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000259 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
260 / QNAME /
261 / /
262 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
263 | QTYPE |
264 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
265 | QCLASS |
266 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
267QNAME a domain name represented as a sequence of labels, where
268 each label consists of a length octet followed by that
269 number of octets. The domain name terminates with the
270 zero length octet for the null label of the root. Note
271 that this field may be an odd number of octets; no
272 padding is used.
273QTYPE a two octet type of the query.
274 1 a host address [REQ_A const]
275 2 an authoritative name server
276 3 a mail destination (Obsolete - use MX)
277 4 a mail forwarder (Obsolete - use MX)
278 5 the canonical name for an alias
279 6 marks the start of a zone of authority
280 7 a mailbox domain name (EXPERIMENTAL)
281 8 a mail group member (EXPERIMENTAL)
282 9 a mail rename domain name (EXPERIMENTAL)
283 10 a null RR (EXPERIMENTAL)
284 11 a well known service description
285 12 a domain name pointer [REQ_PTR const]
286 13 host information
287 14 mailbox or mail list information
288 15 mail exchange
289 16 text strings
290 0x1c IPv6?
291 252 a request for a transfer of an entire zone
292 253 a request for mailbox-related records (MB, MG or MR)
293 254 a request for mail agent RRs (Obsolete - see MX)
294 255 a request for all records
295QCLASS a two octet code that specifies the class of the query.
296 1 the Internet
Denis Vlasenkoef1b4392009-04-12 19:03:01 +0000297 (others are historic only)
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000298 255 any class
299
Denys Vlasenko5fb38492010-02-06 22:48:10 +01003004.1.3. Resource Record format
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000301
Denis Vlasenko5c329932009-04-12 12:16:21 +0000302The answer, authority, and additional sections all share the same format:
303a variable number of resource records, where the number of records
304is specified in the corresponding count field in the header.
305Each resource record has this format:
306 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000307 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
308 / /
309 / NAME /
310 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
311 | TYPE |
312 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
313 | CLASS |
314 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
315 | TTL |
316 | |
317 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
318 | RDLENGTH |
319 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--|
320 / RDATA /
321 / /
322 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
323NAME a domain name to which this resource record pertains.
324TYPE two octets containing one of the RR type codes. This
Denis Vlasenko5c329932009-04-12 12:16:21 +0000325 field specifies the meaning of the data in the RDATA field.
326CLASS two octets which specify the class of the data in the RDATA field.
327TTL a 32 bit unsigned integer that specifies the time interval
328 (in seconds) that the record may be cached.
329RDLENGTH a 16 bit integer, length in octets of the RDATA field.
330RDATA a variable length string of octets that describes the resource.
331 The format of this information varies according to the TYPE
332 and CLASS of the resource record.
333 If the TYPE is A and the CLASS is IN, it's a 4 octet IP address.
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000334
3354.1.4. Message compression
336
Denis Vlasenko5c329932009-04-12 12:16:21 +0000337In order to reduce the size of messages, domain names coan be compressed.
338An entire domain name or a list of labels at the end of a domain name
339is replaced with a pointer to a prior occurance of the same name.
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000340
341The pointer takes the form of a two octet sequence:
342 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
343 | 1 1| OFFSET |
344 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
345The first two bits are ones. This allows a pointer to be distinguished
346from a label, since the label must begin with two zero bits because
347labels are restricted to 63 octets or less. The OFFSET field specifies
348an offset from the start of the message (i.e., the first octet
349of the ID field in the domain header).
350A zero offset specifies the first byte of the ID field, etc.
Denis Vlasenko5c329932009-04-12 12:16:21 +0000351Domain name in a message can be represented as either:
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000352 - a sequence of labels ending in a zero octet
353 - a pointer
354 - a sequence of labels ending with a pointer
355 */
Denis Vlasenko5c329932009-04-12 12:16:21 +0000356static int process_packet(struct dns_entry *conf_data,
357 uint32_t conf_ttl,
358 uint8_t *buf)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000359{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000360 struct dns_head *head;
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100361 struct type_and_class *unaligned_type_class;
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100362 const char *err_msg;
Denis Vlasenko5c329932009-04-12 12:16:21 +0000363 char *query_string;
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100364 char *answstr;
Denis Vlasenko5c329932009-04-12 12:16:21 +0000365 uint8_t *answb;
Denis Vlasenko081eb712008-03-17 09:02:21 +0000366 uint16_t outr_rlen;
367 uint16_t outr_flags;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000368 uint16_t type;
369 uint16_t class;
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100370 int query_len;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000371
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000372 head = (struct dns_head *)buf;
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000373 if (head->nquer == 0) {
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000374 bb_error_msg("packet has 0 queries, ignored");
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100375 return 0; /* don't reply */
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000376 }
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000377 if (head->flags & htons(0x8000)) { /* QR bit */
378 bb_error_msg("response packet, ignored");
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100379 return 0; /* don't reply */
380 }
381 /* QR = 1 "response", RCODE = 4 "Not Implemented" */
382 outr_flags = htons(0x8000 | 4);
383 err_msg = NULL;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000384
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000385 /* start of query string */
Denis Vlasenko5c329932009-04-12 12:16:21 +0000386 query_string = (void *)(head + 1);
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000387 /* caller guarantees strlen is <= MAX_PACK_LEN */
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100388 query_len = strlen(query_string) + 1;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000389 /* may be unaligned! */
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100390 unaligned_type_class = (void *)(query_string + query_len);
391 query_len += sizeof(unaligned_type_class);
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000392 /* where to append answer block */
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100393 answb = (void *)(unaligned_type_class + 1);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000394
Denys Vlasenkocbcc1232010-03-05 23:38:54 +0100395 /* OPCODE != 0 "standard query"? */
396 if ((head->flags & htons(0x7800)) != 0) {
397 err_msg = "opcode != 0";
398 goto empty_packet;
399 }
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100400 move_from_unaligned16(class, &unaligned_type_class->class);
401 if (class != htons(1)) { /* not class INET? */
402 err_msg = "class != 1";
403 goto empty_packet;
404 }
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100405 move_from_unaligned16(type, &unaligned_type_class->type);
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000406 if (type != htons(REQ_A) && type != htons(REQ_PTR)) {
Denys Vlasenkoe66a09b2010-02-07 01:11:18 +0100407 /* we can't handle this query type */
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100408//TODO: happens all the time with REQ_AAAA (0x1c) requests - implement those?
409 err_msg = "type is !REQ_A and !REQ_PTR";
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000410 goto empty_packet;
411 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000412
Denis Vlasenko5c329932009-04-12 12:16:21 +0000413 /* look up the name */
Denis Vlasenko5c329932009-04-12 12:16:21 +0000414 answstr = table_lookup(conf_data, type, query_string);
Denys Vlasenkoe66a09b2010-02-07 01:11:18 +0100415#if DEBUG
416 /* Shows lengths instead of dots, unusable for !DEBUG */
417 bb_error_msg("'%s'->'%s'", query_string, answstr);
418#endif
Denis Vlasenko5c329932009-04-12 12:16:21 +0000419 outr_rlen = 4;
420 if (answstr && type == htons(REQ_PTR)) {
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100421 /* returning a host name */
Denis Vlasenko5c329932009-04-12 12:16:21 +0000422 outr_rlen = strlen(answstr) + 1;
423 }
424 if (!answstr
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100425 || (unsigned)(answb - buf) + query_len + 4 + 2 + outr_rlen > MAX_PACK_LEN
Denis Vlasenko5c329932009-04-12 12:16:21 +0000426 ) {
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000427 /* QR = 1 "response"
428 * AA = 1 "Authoritative Answer"
429 * RCODE = 3 "Name Error" */
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100430 err_msg = "name is not found";
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000431 outr_flags = htons(0x8000 | 0x0400 | 3);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000432 goto empty_packet;
433 }
Denis Vlasenko5c329932009-04-12 12:16:21 +0000434
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100435 /* Append answer Resource Record */
436 memcpy(answb, query_string, query_len); /* name, type, class */
437 answb += query_len;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000438 move_to_unaligned32((uint32_t *)answb, htonl(conf_ttl));
Denis Vlasenko081eb712008-03-17 09:02:21 +0000439 answb += 4;
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100440 move_to_unaligned16((uint16_t *)answb, htons(outr_rlen));
Denis Vlasenko081eb712008-03-17 09:02:21 +0000441 answb += 2;
442 memcpy(answb, answstr, outr_rlen);
443 answb += outr_rlen;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000444
Denis Vlasenko5c329932009-04-12 12:16:21 +0000445 /* QR = 1 "response",
446 * AA = 1 "Authoritative Answer",
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100447 * TODO: need to set RA bit 0x80? One user says nslookup complains
448 * "Got recursion not available from SERVER, trying next server"
449 * "** server can't find HOSTNAME"
450 * RCODE = 0 "success"
451 */
Denys Vlasenkoe66a09b2010-02-07 01:11:18 +0100452 if (OPT_verbose)
453 bb_error_msg("returning positive reply");
Denis Vlasenko5c329932009-04-12 12:16:21 +0000454 outr_flags = htons(0x8000 | 0x0400 | 0);
455 /* we have one answer */
456 head->nansw = htons(1);
457
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000458 empty_packet:
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100459 if ((outr_flags & htons(0xf)) != 0) { /* not a positive response */
460 if (OPT_verbose) {
461 bb_error_msg("%s, %s",
462 err_msg,
463 OPT_silent ? "dropping query" : "sending error reply"
464 );
465 }
466 if (OPT_silent)
467 return 0;
468 }
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000469 head->flags |= outr_flags;
Denis Vlasenko081eb712008-03-17 09:02:21 +0000470 head->nauth = head->nadd = 0;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000471 head->nquer = htons(1); // why???
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000472
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000473 return answb - buf;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000474}
475
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000476int dnsd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000477int dnsd_main(int argc UNUSED_PARAM, char **argv)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000478{
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000479 const char *listen_interface = "0.0.0.0";
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000480 const char *fileconf = "/etc/dnsd.conf";
481 struct dns_entry *conf_data;
482 uint32_t conf_ttl = DEFAULT_TTL;
Denis Vlasenko2c916522007-01-12 14:57:37 +0000483 char *sttl, *sport;
Denis Vlasenko081eb712008-03-17 09:02:21 +0000484 len_and_sockaddr *lsa, *from, *to;
485 unsigned lsa_size;
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000486 int udps, opts;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000487 uint16_t port = 53;
Denys Vlasenko0ecc1162010-04-14 10:14:25 -0700488 /* Ensure buf is 32bit aligned (we need 16bit, but 32bit can't hurt) */
489 uint8_t buf[MAX_PACK_LEN + 1] ALIGN4;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000490
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100491 opts = getopt32(argv, "vsi:c:t:p:d", &listen_interface, &fileconf, &sttl, &sport);
492 //if (opts & (1 << 0)) // -v
493 //if (opts & (1 << 1)) // -s
494 //if (opts & (1 << 2)) // -i
495 //if (opts & (1 << 3)) // -c
496 if (opts & (1 << 4)) // -t
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000497 conf_ttl = xatou_range(sttl, 1, 0xffffffff);
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100498 if (opts & (1 << 5)) // -p
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000499 port = xatou_range(sport, 1, 0xffff);
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100500 if (opts & (1 << 6)) { // -d
Denis Vlasenko5a142022007-03-26 13:20:54 +0000501 bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv);
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000502 openlog(applet_name, LOG_PID, LOG_DAEMON);
Denis Vlasenko2c916522007-01-12 14:57:37 +0000503 logmode = LOGMODE_SYSLOG;
504 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000505
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000506 conf_data = parse_conf_file(fileconf);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000507
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000508 lsa = xdotted2sockaddr(listen_interface, port);
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000509 udps = xsocket(lsa->u.sa.sa_family, SOCK_DGRAM, 0);
510 xbind(udps, &lsa->u.sa, lsa->len);
Denis Vlasenko081eb712008-03-17 09:02:21 +0000511 socket_want_pktinfo(udps); /* needed for recv_from_to to work */
512 lsa_size = LSA_LEN_SIZE + lsa->len;
513 from = xzalloc(lsa_size);
514 to = xzalloc(lsa_size);
515
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000516 {
517 char *p = xmalloc_sockaddr2dotted(&lsa->u.sa);
Denys Vlasenkoe66a09b2010-02-07 01:11:18 +0100518 bb_error_msg("accepting UDP packets on %s", p);
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000519 free(p);
520 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000521
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000522 while (1) {
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000523 int r;
Denis Vlasenko081eb712008-03-17 09:02:21 +0000524 /* Try to get *DEST* address (to which of our addresses
525 * this query was directed), and reply from the same address.
526 * Or else we can exhibit usual UDP ugliness:
527 * [ip1.multihomed.ip2] <= query to ip1 <= peer
528 * [ip1.multihomed.ip2] => reply from ip2 => peer (confused) */
529 memcpy(to, lsa, lsa_size);
530 r = recv_from_to(udps, buf, MAX_PACK_LEN + 1, 0, &from->u.sa, &to->u.sa, lsa->len);
531 if (r < 12 || r > MAX_PACK_LEN) {
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000532 bb_error_msg("packet size %d, ignored", r);
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000533 continue;
Denis Vlasenko2c916522007-01-12 14:57:37 +0000534 }
Denis Vlasenko081eb712008-03-17 09:02:21 +0000535 if (OPT_verbose)
Denys Vlasenkoe66a09b2010-02-07 01:11:18 +0100536 bb_error_msg("got UDP packet");
Denis Vlasenko081eb712008-03-17 09:02:21 +0000537 buf[r] = '\0'; /* paranoia */
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000538 r = process_packet(conf_data, conf_ttl, buf);
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000539 if (r <= 0)
540 continue;
Denis Vlasenkoe9b76e12008-05-22 17:41:01 +0000541 send_to_from(udps, buf, r, 0, &from->u.sa, &to->u.sa, lsa->len);
Denis Vlasenko2c916522007-01-12 14:57:37 +0000542 }
Denis Vlasenkob5b45a92007-03-24 13:09:07 +0000543 return 0;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000544}