blob: 65eae967025b8bddd39d6a8ba12bd9126eaf9716 [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 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02009 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000010 *
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
Pere Orga5bc8c002011-04-11 03:29:49 +020020//usage:#define dnsd_trivial_usage
21//usage: "[-dvs] [-c CONFFILE] [-t TTL_SEC] [-p PORT] [-i ADDR]"
22//usage:#define dnsd_full_usage "\n\n"
23//usage: "Small static DNS server daemon\n"
24//usage: "\nOptions:"
25//usage: "\n -c FILE Config file"
26//usage: "\n -t SEC TTL"
27//usage: "\n -p PORT Listen on PORT"
28//usage: "\n -i ADDR Listen on ADDR"
29//usage: "\n -d Daemonize"
30//usage: "\n -v Verbose"
31//usage: "\n -s Send successful replies only. Use this if you want"
32//usage: "\n to use /etc/resolv.conf with two nameserver lines:"
33//usage: "\n nameserver DNSD_SERVER"
34//usage: "\n nameserver NORNAL_DNS_SERVER"
35
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000036#include "libbb.h"
Bernhard Reutner-Fischerf4701962008-01-27 12:50:12 +000037#include <syslog.h>
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000038
Denis Vlasenkoc12f5302006-10-06 09:49:47 +000039//#define DEBUG 1
Denis Vlasenko2c916522007-01-12 14:57:37 +000040#define DEBUG 0
Denis Vlasenkoc12f5302006-10-06 09:49:47 +000041
Rob Landleybc68cd12006-03-10 19:22:06 +000042enum {
Denis Vlasenkoef1b4392009-04-12 19:03:01 +000043 /* can tweak this */
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000044 DEFAULT_TTL = 120,
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000045
Denis Vlasenkoef1b4392009-04-12 19:03:01 +000046 /* cannot get bigger packets than 512 per RFC1035. */
Denis Vlasenko081eb712008-03-17 09:02:21 +000047 MAX_PACK_LEN = 512,
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000048 IP_STRING_LEN = sizeof(".xxx.xxx.xxx.xxx"),
49 MAX_NAME_LEN = IP_STRING_LEN - 1 + sizeof(".in-addr.arpa"),
Rob Landleybc68cd12006-03-10 19:22:06 +000050 REQ_A = 1,
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000051 REQ_PTR = 12,
Rob Landleybc68cd12006-03-10 19:22:06 +000052};
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000053
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000054/* the message from client and first part of response msg */
55struct dns_head {
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000056 uint16_t id;
57 uint16_t flags;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000058 uint16_t nquer;
59 uint16_t nansw;
60 uint16_t nauth;
61 uint16_t nadd;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000062};
Denys Vlasenko6646de02010-04-26 14:25:33 +020063/* Structure used to access type and class fields.
64 * They are totally unaligned, but gcc 4.3.4 thinks that pointer of type uint16_t*
65 * is 16-bit aligned and replaces 16-bit memcpy (in move_from_unaligned16 macro)
66 * with aligned halfword access on arm920t!
67 * Oh well. Slapping PACKED everywhere seems to help: */
Denys Vlasenko5fb38492010-02-06 22:48:10 +010068struct type_and_class {
Denys Vlasenko6646de02010-04-26 14:25:33 +020069 uint16_t type PACKED;
70 uint16_t class PACKED;
71} PACKED;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000072/* element of known name, ip address and reversed ip address */
73struct dns_entry {
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000074 struct dns_entry *next;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000075 uint32_t ip;
76 char rip[IP_STRING_LEN]; /* length decimal reversed IP */
77 char name[1];
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000078};
79
Denys Vlasenko343dfd72010-02-07 02:45:03 +010080#define OPT_verbose (option_mask32 & 1)
81#define OPT_silent (option_mask32 & 2)
Denis Vlasenkod3bac032007-03-24 12:13:04 +000082
83
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000084/*
Bernhard Reutner-Fischerc418d482006-05-31 10:19:51 +000085 * Insert length of substrings instead of dots
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000086 */
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000087static void undot(char *rip)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000088{
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000089 int i = 0;
90 int s = 0;
91
Denis Vlasenkoc12f5302006-10-06 09:49:47 +000092 while (rip[i])
93 i++;
94 for (--i; i >= 0; i--) {
95 if (rip[i] == '.') {
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000096 rip[i] = s;
97 s = 0;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000098 } else {
99 s++;
100 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000101 }
102}
103
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000104/*
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000105 * Read hostname/IP records from file
106 */
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000107static struct dns_entry *parse_conf_file(const char *fileconf)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000108{
Denis Vlasenko084266e2008-07-26 23:08:31 +0000109 char *token[2];
Denis Vlasenkoa34f1ed2008-07-20 17:48:59 +0000110 parser_t *parser;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000111 struct dns_entry *m, *conf_data;
112 struct dns_entry **nextp;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000113
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000114 conf_data = NULL;
115 nextp = &conf_data;
116
Denis Vlasenkoa34f1ed2008-07-20 17:48:59 +0000117 parser = config_open(fileconf);
Denis Vlasenko084266e2008-07-26 23:08:31 +0000118 while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) {
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000119 struct in_addr ip;
120 uint32_t v32;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000121
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000122 if (inet_aton(token[1], &ip) == 0) {
123 bb_error_msg("error at line %u, skipping", parser->lineno);
124 continue;
125 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000126
Denis Vlasenko084266e2008-07-26 23:08:31 +0000127 if (OPT_verbose)
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000128 bb_error_msg("name:%s, ip:%s", token[0], token[1]);
Denis Vlasenkoa34f1ed2008-07-20 17:48:59 +0000129
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000130 /* sizeof(*m) includes 1 byte for m->name[0] */
131 m = xzalloc(sizeof(*m) + strlen(token[0]) + 1);
132 /*m->next = NULL;*/
133 *nextp = m;
134 nextp = &m->next;
135
136 m->name[0] = '.';
137 strcpy(m->name + 1, token[0]);
138 undot(m->name);
139 m->ip = ip.s_addr; /* in network order */
140 v32 = ntohl(m->ip);
141 /* inverted order */
142 sprintf(m->rip, ".%u.%u.%u.%u",
143 (uint8_t)(v32),
144 (uint8_t)(v32 >> 8),
145 (uint8_t)(v32 >> 16),
146 (v32 >> 24)
147 );
148 undot(m->rip);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000149 }
Denis Vlasenko084266e2008-07-26 23:08:31 +0000150 config_close(parser);
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000151 return conf_data;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000152}
153
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000154/*
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000155 * Look query up in dns records and return answer if found.
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000156 */
Denis Vlasenko5c329932009-04-12 12:16:21 +0000157static char *table_lookup(struct dns_entry *d,
158 uint16_t type,
159 char* query_string)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000160{
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000161 while (d) {
162 unsigned len = d->name[0];
163 /* d->name[len] is the last (non NUL) char */
Denis Vlasenko91f20ab2007-01-20 01:47:44 +0000164#if DEBUG
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000165 char *p, *q;
Denis Vlasenko5c329932009-04-12 12:16:21 +0000166 q = query_string + 1;
167 p = d->name + 1;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000168 fprintf(stderr, "%d/%d p:%s q:%s %d\n",
169 (int)strlen(p), len,
170 p, q, (int)strlen(q)
171 );
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000172#endif
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000173 if (type == htons(REQ_A)) {
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000174 /* search by host name */
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000175 if (len != 1 || d->name[1] != '*') {
Denis Vlasenko5c329932009-04-12 12:16:21 +0000176/* we are lax, hope no name component is ever >64 so that length
177 * (which will be represented as 'A','B'...) matches a lowercase letter.
178 * Actually, I think false matches are hard to construct.
179 * Example.
180 * [31] len is represented as '1', [65] as 'A', [65+32] as 'a'.
181 * [65] <65 same chars>[31]<31 same chars>NUL
182 * [65+32]<65 same chars>1 <31 same chars>NUL
183 * This example seems to be the minimal case when false match occurs.
184 */
185 if (strcasecmp(d->name, query_string) != 0)
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000186 goto next;
187 }
Denis Vlasenko5c329932009-04-12 12:16:21 +0000188 return (char *)&d->ip;
Denis Vlasenko91f20ab2007-01-20 01:47:44 +0000189#if DEBUG
Denis Vlasenko5c329932009-04-12 12:16:21 +0000190 fprintf(stderr, "Found IP:%x\n", (int)d->ip);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000191#endif
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000192 return 0;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000193 }
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000194 /* search by IP-address */
195 if ((len != 1 || d->name[1] != '*')
Denis Vlasenko5c329932009-04-12 12:16:21 +0000196 /* we assume (do not check) that query_string
197 * ends in ".in-addr.arpa" */
198 && strncmp(d->rip, query_string, strlen(d->rip)) == 0
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000199 ) {
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000200#if DEBUG
Denis Vlasenko5c329932009-04-12 12:16:21 +0000201 fprintf(stderr, "Found name:%s\n", d->name);
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000202#endif
Denis Vlasenko5c329932009-04-12 12:16:21 +0000203 return d->name;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000204 }
205 next:
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000206 d = d->next;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000207 }
208
Denis Vlasenko5c329932009-04-12 12:16:21 +0000209 return NULL;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000210}
211
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000212/*
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000213 * Decode message and generate answer
214 */
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000215/* RFC 1035
216...
217Whenever an octet represents a numeric quantity, the left most bit
218in the diagram is the high order or most significant bit.
219That is, the bit labeled 0 is the most significant bit.
220...
221
2224.1.1. Header section format
Denis Vlasenko5c329932009-04-12 12:16:21 +0000223 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000224 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
225 | ID |
226 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
Denis Vlasenko5c329932009-04-12 12:16:21 +0000227 |QR| OPCODE |AA|TC|RD|RA| 0 0 0| RCODE |
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000228 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
229 | QDCOUNT |
230 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
231 | ANCOUNT |
232 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
233 | NSCOUNT |
234 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
235 | ARCOUNT |
236 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
Denis Vlasenko5c329932009-04-12 12:16:21 +0000237ID 16 bit random identifier assigned by querying peer.
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000238 Used to match query/response.
239QR message is a query (0), or a response (1).
240OPCODE 0 standard query (QUERY)
241 1 inverse query (IQUERY)
242 2 server status request (STATUS)
Denis Vlasenko5c329932009-04-12 12:16:21 +0000243AA Authoritative Answer - this bit is valid in responses.
244 Responding name server is an authority for the domain name
Denis Vlasenkoef1b4392009-04-12 19:03:01 +0000245 in question section. Answer section may have multiple owner names
246 because of aliases. The AA bit corresponds to the name which matches
247 the query name, or the first owner name in the answer section.
Denis Vlasenko5c329932009-04-12 12:16:21 +0000248TC TrunCation - this message was truncated.
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000249RD Recursion Desired - this bit may be set in a query and
250 is copied into the response. If RD is set, it directs
251 the name server to pursue the query recursively.
252 Recursive query support is optional.
253RA Recursion Available - this be is set or cleared in a
254 response, and denotes whether recursive query support is
255 available in the name server.
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000256RCODE Response code.
257 0 No error condition
258 1 Format error
Denis Vlasenko5c329932009-04-12 12:16:21 +0000259 2 Server failure - server was unable to process the query
Denis Vlasenkoef1b4392009-04-12 19:03:01 +0000260 due to a problem with the name server.
Denis Vlasenko5c329932009-04-12 12:16:21 +0000261 3 Name Error - meaningful only for responses from
Denis Vlasenkoef1b4392009-04-12 19:03:01 +0000262 an authoritative name server. The referenced domain name
263 does not exist.
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000264 4 Not Implemented.
265 5 Refused.
266QDCOUNT number of entries in the question section.
Denis Vlasenko5c329932009-04-12 12:16:21 +0000267ANCOUNT number of records in the answer section.
268NSCOUNT number of records in the authority records section.
269ARCOUNT number of records in the additional records section.
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000270
2714.1.2. Question section format
272
Denis Vlasenko5c329932009-04-12 12:16:21 +0000273The section contains QDCOUNT (usually 1) entries, each of this format:
274 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000275 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
276 / QNAME /
277 / /
278 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
279 | QTYPE |
280 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
281 | QCLASS |
282 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
283QNAME a domain name represented as a sequence of labels, where
284 each label consists of a length octet followed by that
285 number of octets. The domain name terminates with the
286 zero length octet for the null label of the root. Note
287 that this field may be an odd number of octets; no
288 padding is used.
289QTYPE a two octet type of the query.
290 1 a host address [REQ_A const]
291 2 an authoritative name server
292 3 a mail destination (Obsolete - use MX)
293 4 a mail forwarder (Obsolete - use MX)
294 5 the canonical name for an alias
295 6 marks the start of a zone of authority
296 7 a mailbox domain name (EXPERIMENTAL)
297 8 a mail group member (EXPERIMENTAL)
298 9 a mail rename domain name (EXPERIMENTAL)
299 10 a null RR (EXPERIMENTAL)
300 11 a well known service description
301 12 a domain name pointer [REQ_PTR const]
302 13 host information
303 14 mailbox or mail list information
304 15 mail exchange
305 16 text strings
306 0x1c IPv6?
307 252 a request for a transfer of an entire zone
308 253 a request for mailbox-related records (MB, MG or MR)
309 254 a request for mail agent RRs (Obsolete - see MX)
310 255 a request for all records
311QCLASS a two octet code that specifies the class of the query.
312 1 the Internet
Denis Vlasenkoef1b4392009-04-12 19:03:01 +0000313 (others are historic only)
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000314 255 any class
315
Denys Vlasenko5fb38492010-02-06 22:48:10 +01003164.1.3. Resource Record format
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000317
Denis Vlasenko5c329932009-04-12 12:16:21 +0000318The answer, authority, and additional sections all share the same format:
319a variable number of resource records, where the number of records
320is specified in the corresponding count field in the header.
321Each resource record has this format:
322 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000323 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
324 / /
325 / NAME /
326 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
327 | TYPE |
328 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
329 | CLASS |
330 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
331 | TTL |
332 | |
333 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
334 | RDLENGTH |
335 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--|
336 / RDATA /
337 / /
338 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
339NAME a domain name to which this resource record pertains.
340TYPE two octets containing one of the RR type codes. This
Denis Vlasenko5c329932009-04-12 12:16:21 +0000341 field specifies the meaning of the data in the RDATA field.
342CLASS two octets which specify the class of the data in the RDATA field.
343TTL a 32 bit unsigned integer that specifies the time interval
344 (in seconds) that the record may be cached.
345RDLENGTH a 16 bit integer, length in octets of the RDATA field.
346RDATA a variable length string of octets that describes the resource.
347 The format of this information varies according to the TYPE
348 and CLASS of the resource record.
349 If the TYPE is A and the CLASS is IN, it's a 4 octet IP address.
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000350
3514.1.4. Message compression
352
Denis Vlasenko5c329932009-04-12 12:16:21 +0000353In order to reduce the size of messages, domain names coan be compressed.
354An entire domain name or a list of labels at the end of a domain name
355is replaced with a pointer to a prior occurance of the same name.
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000356
357The pointer takes the form of a two octet sequence:
358 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
359 | 1 1| OFFSET |
360 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
361The first two bits are ones. This allows a pointer to be distinguished
362from a label, since the label must begin with two zero bits because
363labels are restricted to 63 octets or less. The OFFSET field specifies
364an offset from the start of the message (i.e., the first octet
365of the ID field in the domain header).
366A zero offset specifies the first byte of the ID field, etc.
Denis Vlasenko5c329932009-04-12 12:16:21 +0000367Domain name in a message can be represented as either:
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000368 - a sequence of labels ending in a zero octet
369 - a pointer
370 - a sequence of labels ending with a pointer
371 */
Denis Vlasenko5c329932009-04-12 12:16:21 +0000372static int process_packet(struct dns_entry *conf_data,
373 uint32_t conf_ttl,
374 uint8_t *buf)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000375{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000376 struct dns_head *head;
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100377 struct type_and_class *unaligned_type_class;
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100378 const char *err_msg;
Denis Vlasenko5c329932009-04-12 12:16:21 +0000379 char *query_string;
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100380 char *answstr;
Denis Vlasenko5c329932009-04-12 12:16:21 +0000381 uint8_t *answb;
Denis Vlasenko081eb712008-03-17 09:02:21 +0000382 uint16_t outr_rlen;
383 uint16_t outr_flags;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000384 uint16_t type;
385 uint16_t class;
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100386 int query_len;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000387
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000388 head = (struct dns_head *)buf;
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000389 if (head->nquer == 0) {
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000390 bb_error_msg("packet has 0 queries, ignored");
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100391 return 0; /* don't reply */
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000392 }
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000393 if (head->flags & htons(0x8000)) { /* QR bit */
394 bb_error_msg("response packet, ignored");
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100395 return 0; /* don't reply */
396 }
397 /* QR = 1 "response", RCODE = 4 "Not Implemented" */
398 outr_flags = htons(0x8000 | 4);
399 err_msg = NULL;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000400
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000401 /* start of query string */
Denis Vlasenko5c329932009-04-12 12:16:21 +0000402 query_string = (void *)(head + 1);
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000403 /* caller guarantees strlen is <= MAX_PACK_LEN */
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100404 query_len = strlen(query_string) + 1;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000405 /* may be unaligned! */
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100406 unaligned_type_class = (void *)(query_string + query_len);
Denys Vlasenkodc8ef352010-10-29 00:37:56 +0200407 query_len += sizeof(*unaligned_type_class);
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000408 /* where to append answer block */
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100409 answb = (void *)(unaligned_type_class + 1);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000410
Denys Vlasenkocbcc1232010-03-05 23:38:54 +0100411 /* OPCODE != 0 "standard query"? */
412 if ((head->flags & htons(0x7800)) != 0) {
413 err_msg = "opcode != 0";
414 goto empty_packet;
415 }
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100416 move_from_unaligned16(class, &unaligned_type_class->class);
417 if (class != htons(1)) { /* not class INET? */
418 err_msg = "class != 1";
419 goto empty_packet;
420 }
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100421 move_from_unaligned16(type, &unaligned_type_class->type);
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000422 if (type != htons(REQ_A) && type != htons(REQ_PTR)) {
Denys Vlasenkoe66a09b2010-02-07 01:11:18 +0100423 /* we can't handle this query type */
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100424//TODO: happens all the time with REQ_AAAA (0x1c) requests - implement those?
425 err_msg = "type is !REQ_A and !REQ_PTR";
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000426 goto empty_packet;
427 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000428
Denis Vlasenko5c329932009-04-12 12:16:21 +0000429 /* look up the name */
Denis Vlasenko5c329932009-04-12 12:16:21 +0000430 answstr = table_lookup(conf_data, type, query_string);
Denys Vlasenkoe66a09b2010-02-07 01:11:18 +0100431#if DEBUG
432 /* Shows lengths instead of dots, unusable for !DEBUG */
433 bb_error_msg("'%s'->'%s'", query_string, answstr);
434#endif
Denis Vlasenko5c329932009-04-12 12:16:21 +0000435 outr_rlen = 4;
436 if (answstr && type == htons(REQ_PTR)) {
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100437 /* returning a host name */
Denis Vlasenko5c329932009-04-12 12:16:21 +0000438 outr_rlen = strlen(answstr) + 1;
439 }
440 if (!answstr
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100441 || (unsigned)(answb - buf) + query_len + 4 + 2 + outr_rlen > MAX_PACK_LEN
Denis Vlasenko5c329932009-04-12 12:16:21 +0000442 ) {
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000443 /* QR = 1 "response"
444 * AA = 1 "Authoritative Answer"
445 * RCODE = 3 "Name Error" */
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100446 err_msg = "name is not found";
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000447 outr_flags = htons(0x8000 | 0x0400 | 3);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000448 goto empty_packet;
449 }
Denis Vlasenko5c329932009-04-12 12:16:21 +0000450
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100451 /* Append answer Resource Record */
452 memcpy(answb, query_string, query_len); /* name, type, class */
453 answb += query_len;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000454 move_to_unaligned32((uint32_t *)answb, htonl(conf_ttl));
Denis Vlasenko081eb712008-03-17 09:02:21 +0000455 answb += 4;
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100456 move_to_unaligned16((uint16_t *)answb, htons(outr_rlen));
Denis Vlasenko081eb712008-03-17 09:02:21 +0000457 answb += 2;
458 memcpy(answb, answstr, outr_rlen);
459 answb += outr_rlen;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000460
Denis Vlasenko5c329932009-04-12 12:16:21 +0000461 /* QR = 1 "response",
462 * AA = 1 "Authoritative Answer",
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100463 * TODO: need to set RA bit 0x80? One user says nslookup complains
464 * "Got recursion not available from SERVER, trying next server"
465 * "** server can't find HOSTNAME"
466 * RCODE = 0 "success"
467 */
Denys Vlasenkoe66a09b2010-02-07 01:11:18 +0100468 if (OPT_verbose)
469 bb_error_msg("returning positive reply");
Denis Vlasenko5c329932009-04-12 12:16:21 +0000470 outr_flags = htons(0x8000 | 0x0400 | 0);
471 /* we have one answer */
472 head->nansw = htons(1);
473
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000474 empty_packet:
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100475 if ((outr_flags & htons(0xf)) != 0) { /* not a positive response */
476 if (OPT_verbose) {
477 bb_error_msg("%s, %s",
478 err_msg,
479 OPT_silent ? "dropping query" : "sending error reply"
480 );
481 }
482 if (OPT_silent)
483 return 0;
484 }
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000485 head->flags |= outr_flags;
Denis Vlasenko081eb712008-03-17 09:02:21 +0000486 head->nauth = head->nadd = 0;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000487 head->nquer = htons(1); // why???
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000488
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000489 return answb - buf;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000490}
491
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000492int dnsd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000493int dnsd_main(int argc UNUSED_PARAM, char **argv)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000494{
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000495 const char *listen_interface = "0.0.0.0";
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000496 const char *fileconf = "/etc/dnsd.conf";
497 struct dns_entry *conf_data;
498 uint32_t conf_ttl = DEFAULT_TTL;
Denis Vlasenko2c916522007-01-12 14:57:37 +0000499 char *sttl, *sport;
Denis Vlasenko081eb712008-03-17 09:02:21 +0000500 len_and_sockaddr *lsa, *from, *to;
501 unsigned lsa_size;
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000502 int udps, opts;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000503 uint16_t port = 53;
Denys Vlasenko0ecc1162010-04-14 10:14:25 -0700504 /* Ensure buf is 32bit aligned (we need 16bit, but 32bit can't hurt) */
505 uint8_t buf[MAX_PACK_LEN + 1] ALIGN4;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000506
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100507 opts = getopt32(argv, "vsi:c:t:p:d", &listen_interface, &fileconf, &sttl, &sport);
508 //if (opts & (1 << 0)) // -v
509 //if (opts & (1 << 1)) // -s
510 //if (opts & (1 << 2)) // -i
511 //if (opts & (1 << 3)) // -c
512 if (opts & (1 << 4)) // -t
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000513 conf_ttl = xatou_range(sttl, 1, 0xffffffff);
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100514 if (opts & (1 << 5)) // -p
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000515 port = xatou_range(sport, 1, 0xffff);
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100516 if (opts & (1 << 6)) { // -d
Denis Vlasenko5a142022007-03-26 13:20:54 +0000517 bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv);
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000518 openlog(applet_name, LOG_PID, LOG_DAEMON);
Denis Vlasenko2c916522007-01-12 14:57:37 +0000519 logmode = LOGMODE_SYSLOG;
520 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000521
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000522 conf_data = parse_conf_file(fileconf);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000523
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000524 lsa = xdotted2sockaddr(listen_interface, port);
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000525 udps = xsocket(lsa->u.sa.sa_family, SOCK_DGRAM, 0);
526 xbind(udps, &lsa->u.sa, lsa->len);
Denis Vlasenko081eb712008-03-17 09:02:21 +0000527 socket_want_pktinfo(udps); /* needed for recv_from_to to work */
528 lsa_size = LSA_LEN_SIZE + lsa->len;
529 from = xzalloc(lsa_size);
530 to = xzalloc(lsa_size);
531
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000532 {
533 char *p = xmalloc_sockaddr2dotted(&lsa->u.sa);
Denys Vlasenkoe66a09b2010-02-07 01:11:18 +0100534 bb_error_msg("accepting UDP packets on %s", p);
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000535 free(p);
536 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000537
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000538 while (1) {
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000539 int r;
Denis Vlasenko081eb712008-03-17 09:02:21 +0000540 /* Try to get *DEST* address (to which of our addresses
541 * this query was directed), and reply from the same address.
542 * Or else we can exhibit usual UDP ugliness:
543 * [ip1.multihomed.ip2] <= query to ip1 <= peer
544 * [ip1.multihomed.ip2] => reply from ip2 => peer (confused) */
545 memcpy(to, lsa, lsa_size);
546 r = recv_from_to(udps, buf, MAX_PACK_LEN + 1, 0, &from->u.sa, &to->u.sa, lsa->len);
547 if (r < 12 || r > MAX_PACK_LEN) {
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000548 bb_error_msg("packet size %d, ignored", r);
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000549 continue;
Denis Vlasenko2c916522007-01-12 14:57:37 +0000550 }
Denis Vlasenko081eb712008-03-17 09:02:21 +0000551 if (OPT_verbose)
Denys Vlasenkoe66a09b2010-02-07 01:11:18 +0100552 bb_error_msg("got UDP packet");
Denis Vlasenko081eb712008-03-17 09:02:21 +0000553 buf[r] = '\0'; /* paranoia */
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000554 r = process_packet(conf_data, conf_ttl, buf);
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000555 if (r <= 0)
556 continue;
Denis Vlasenkoe9b76e12008-05-22 17:41:01 +0000557 send_to_from(udps, buf, r, 0, &from->u.sa, &to->u.sa, lsa->len);
Denis Vlasenko2c916522007-01-12 14:57:37 +0000558 }
Denis Vlasenkob5b45a92007-03-24 13:09:07 +0000559 return 0;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000560}