blob: 5dbaec1ebe15861fd0d2c1daef3a91d0ee1afb00 [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 Vlasenkoddbf3bf2009-04-12 04:09:09 +000027 /* Can tweak this */
28 DEFAULT_TTL = 120,
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000029
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000030/* Cannot get bigger packets than 512 per RFC1035.
31 * In practice this can be set considerably smaller:
32 * Length of response packet is header (12B) + 2*type(4B) + 2*class(4B) +
33 * ttl(4B) + rlen(2B) + r (MAX_NAME_LEN = 21B) +
34 * 2*querystring (2 MAX_NAME_LEN = 42B), all together 90 Bytes
35 */
Denis Vlasenko081eb712008-03-17 09:02:21 +000036 MAX_PACK_LEN = 512,
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000037 IP_STRING_LEN = sizeof(".xxx.xxx.xxx.xxx"),
38 MAX_NAME_LEN = IP_STRING_LEN - 1 + sizeof(".in-addr.arpa"),
Rob Landleybc68cd12006-03-10 19:22:06 +000039 REQ_A = 1,
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000040 REQ_PTR = 12,
Rob Landleybc68cd12006-03-10 19:22:06 +000041};
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000042
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000043/* the message from client and first part of response msg */
44struct dns_head {
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000045 uint16_t id;
46 uint16_t flags;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000047 uint16_t nquer;
48 uint16_t nansw;
49 uint16_t nauth;
50 uint16_t nadd;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000051};
52struct dns_prop {
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000053 uint16_t type;
54 uint16_t class;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000055};
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
Denis Vlasenkoa19e6492009-03-11 14:40:00 +000064#define OPT_verbose (option_mask32)
Denis Vlasenkod3bac032007-03-24 12:13:04 +000065
66
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000067/*
Bernhard Reutner-Fischerc418d482006-05-31 10:19:51 +000068 * Insert length of substrings instead of dots
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000069 */
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000070static void undot(char *rip)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000071{
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000072 int i = 0;
73 int s = 0;
74
Denis Vlasenkoc12f5302006-10-06 09:49:47 +000075 while (rip[i])
76 i++;
77 for (--i; i >= 0; i--) {
78 if (rip[i] == '.') {
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000079 rip[i] = s;
80 s = 0;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000081 } else {
82 s++;
83 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000084 }
85}
86
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000087/*
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000088 * Read hostname/IP records from file
89 */
Denis Vlasenkoa19e6492009-03-11 14:40:00 +000090static struct dns_entry *parse_conf_file(const char *fileconf)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000091{
Denis Vlasenko084266e2008-07-26 23:08:31 +000092 char *token[2];
Denis Vlasenkoa34f1ed2008-07-20 17:48:59 +000093 parser_t *parser;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000094 struct dns_entry *m, *conf_data;
95 struct dns_entry **nextp;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000096
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000097 conf_data = NULL;
98 nextp = &conf_data;
99
Denis Vlasenkoa34f1ed2008-07-20 17:48:59 +0000100 parser = config_open(fileconf);
Denis Vlasenko084266e2008-07-26 23:08:31 +0000101 while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) {
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000102 struct in_addr ip;
103 uint32_t v32;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000104
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000105 if (inet_aton(token[1], &ip) == 0) {
106 bb_error_msg("error at line %u, skipping", parser->lineno);
107 continue;
108 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000109
Denis Vlasenko084266e2008-07-26 23:08:31 +0000110 if (OPT_verbose)
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000111 bb_error_msg("name:%s, ip:%s", token[0], token[1]);
Denis Vlasenkoa34f1ed2008-07-20 17:48:59 +0000112
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000113 /* sizeof(*m) includes 1 byte for m->name[0] */
114 m = xzalloc(sizeof(*m) + strlen(token[0]) + 1);
115 /*m->next = NULL;*/
116 *nextp = m;
117 nextp = &m->next;
118
119 m->name[0] = '.';
120 strcpy(m->name + 1, token[0]);
121 undot(m->name);
122 m->ip = ip.s_addr; /* in network order */
123 v32 = ntohl(m->ip);
124 /* inverted order */
125 sprintf(m->rip, ".%u.%u.%u.%u",
126 (uint8_t)(v32),
127 (uint8_t)(v32 >> 8),
128 (uint8_t)(v32 >> 16),
129 (v32 >> 24)
130 );
131 undot(m->rip);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000132 }
Denis Vlasenko084266e2008-07-26 23:08:31 +0000133 config_close(parser);
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000134 return conf_data;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000135}
136
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000137/*
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000138 * Look query up in dns records and return answer if found.
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000139 */
Denis Vlasenko5c329932009-04-12 12:16:21 +0000140static char *table_lookup(struct dns_entry *d,
141 uint16_t type,
142 char* query_string)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000143{
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000144 while (d) {
145 unsigned len = d->name[0];
146 /* d->name[len] is the last (non NUL) char */
Denis Vlasenko91f20ab2007-01-20 01:47:44 +0000147#if DEBUG
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000148 char *p, *q;
Denis Vlasenko5c329932009-04-12 12:16:21 +0000149 q = query_string + 1;
150 p = d->name + 1;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000151 fprintf(stderr, "%d/%d p:%s q:%s %d\n",
152 (int)strlen(p), len,
153 p, q, (int)strlen(q)
154 );
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000155#endif
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000156 if (type == htons(REQ_A)) {
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000157 /* search by host name */
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000158 if (len != 1 || d->name[1] != '*') {
Denis Vlasenko5c329932009-04-12 12:16:21 +0000159/* we are lax, hope no name component is ever >64 so that length
160 * (which will be represented as 'A','B'...) matches a lowercase letter.
161 * Actually, I think false matches are hard to construct.
162 * Example.
163 * [31] len is represented as '1', [65] as 'A', [65+32] as 'a'.
164 * [65] <65 same chars>[31]<31 same chars>NUL
165 * [65+32]<65 same chars>1 <31 same chars>NUL
166 * This example seems to be the minimal case when false match occurs.
167 */
168 if (strcasecmp(d->name, query_string) != 0)
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000169 goto next;
170 }
Denis Vlasenko5c329932009-04-12 12:16:21 +0000171 return (char *)&d->ip;
Denis Vlasenko91f20ab2007-01-20 01:47:44 +0000172#if DEBUG
Denis Vlasenko5c329932009-04-12 12:16:21 +0000173 fprintf(stderr, "Found IP:%x\n", (int)d->ip);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000174#endif
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000175 return 0;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000176 }
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000177 /* search by IP-address */
178 if ((len != 1 || d->name[1] != '*')
Denis Vlasenko5c329932009-04-12 12:16:21 +0000179 /* we assume (do not check) that query_string
180 * ends in ".in-addr.arpa" */
181 && strncmp(d->rip, query_string, strlen(d->rip)) == 0
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000182 ) {
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000183#if DEBUG
Denis Vlasenko5c329932009-04-12 12:16:21 +0000184 fprintf(stderr, "Found name:%s\n", d->name);
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000185#endif
Denis Vlasenko5c329932009-04-12 12:16:21 +0000186 return d->name;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000187 }
188 next:
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000189 d = d->next;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000190 }
191
Denis Vlasenko5c329932009-04-12 12:16:21 +0000192 return NULL;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000193}
194
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000195/*
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000196 * Decode message and generate answer
197 */
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000198/* RFC 1035
199...
200Whenever an octet represents a numeric quantity, the left most bit
201in the diagram is the high order or most significant bit.
202That is, the bit labeled 0 is the most significant bit.
203...
204
2054.1.1. Header section format
Denis Vlasenko5c329932009-04-12 12:16:21 +0000206 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000207 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
208 | ID |
209 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
Denis Vlasenko5c329932009-04-12 12:16:21 +0000210 |QR| OPCODE |AA|TC|RD|RA| 0 0 0| RCODE |
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000211 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
212 | QDCOUNT |
213 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
214 | ANCOUNT |
215 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
216 | NSCOUNT |
217 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
218 | ARCOUNT |
219 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
Denis Vlasenko5c329932009-04-12 12:16:21 +0000220ID 16 bit random identifier assigned by querying peer.
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000221 Used to match query/response.
222QR message is a query (0), or a response (1).
223OPCODE 0 standard query (QUERY)
224 1 inverse query (IQUERY)
225 2 server status request (STATUS)
Denis Vlasenko5c329932009-04-12 12:16:21 +0000226AA Authoritative Answer - this bit is valid in responses.
227 Responding name server is an authority for the domain name
228 in question section. Answer section may have multiple owner names
229 because of aliases. The AA bit corresponds to the name which matches
230 the query name, or the first owner name in the answer section.
231TC TrunCation - this message was truncated.
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000232RD Recursion Desired - this bit may be set in a query and
233 is copied into the response. If RD is set, it directs
234 the name server to pursue the query recursively.
235 Recursive query support is optional.
236RA Recursion Available - this be is set or cleared in a
237 response, and denotes whether recursive query support is
238 available in the name server.
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000239RCODE Response code.
240 0 No error condition
241 1 Format error
Denis Vlasenko5c329932009-04-12 12:16:21 +0000242 2 Server failure - server was unable to process the query
243 due to a problem with the name server.
244 3 Name Error - meaningful only for responses from
245 an authoritative name server. The referenced domain name
246 does not exist.
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000247 4 Not Implemented.
248 5 Refused.
249QDCOUNT number of entries in the question section.
Denis Vlasenko5c329932009-04-12 12:16:21 +0000250ANCOUNT number of records in the answer section.
251NSCOUNT number of records in the authority records section.
252ARCOUNT number of records in the additional records section.
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000253
2544.1.2. Question section format
255
Denis Vlasenko5c329932009-04-12 12:16:21 +0000256The section contains QDCOUNT (usually 1) entries, each of this format:
257 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000258 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
259 / QNAME /
260 / /
261 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
262 | QTYPE |
263 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
264 | QCLASS |
265 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
266QNAME a domain name represented as a sequence of labels, where
267 each label consists of a length octet followed by that
268 number of octets. The domain name terminates with the
269 zero length octet for the null label of the root. Note
270 that this field may be an odd number of octets; no
271 padding is used.
272QTYPE a two octet type of the query.
273 1 a host address [REQ_A const]
274 2 an authoritative name server
275 3 a mail destination (Obsolete - use MX)
276 4 a mail forwarder (Obsolete - use MX)
277 5 the canonical name for an alias
278 6 marks the start of a zone of authority
279 7 a mailbox domain name (EXPERIMENTAL)
280 8 a mail group member (EXPERIMENTAL)
281 9 a mail rename domain name (EXPERIMENTAL)
282 10 a null RR (EXPERIMENTAL)
283 11 a well known service description
284 12 a domain name pointer [REQ_PTR const]
285 13 host information
286 14 mailbox or mail list information
287 15 mail exchange
288 16 text strings
289 0x1c IPv6?
290 252 a request for a transfer of an entire zone
291 253 a request for mailbox-related records (MB, MG or MR)
292 254 a request for mail agent RRs (Obsolete - see MX)
293 255 a request for all records
294QCLASS a two octet code that specifies the class of the query.
295 1 the Internet
296 (others are historic only)
297 255 any class
298
2994.1.3. Resource record format
300
Denis Vlasenko5c329932009-04-12 12:16:21 +0000301The answer, authority, and additional sections all share the same format:
302a variable number of resource records, where the number of records
303is specified in the corresponding count field in the header.
304Each resource record has this format:
305 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000306 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
307 / /
308 / NAME /
309 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
310 | TYPE |
311 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
312 | CLASS |
313 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
314 | TTL |
315 | |
316 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
317 | RDLENGTH |
318 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--|
319 / RDATA /
320 / /
321 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
322NAME a domain name to which this resource record pertains.
323TYPE two octets containing one of the RR type codes. This
Denis Vlasenko5c329932009-04-12 12:16:21 +0000324 field specifies the meaning of the data in the RDATA field.
325CLASS two octets which specify the class of the data in the RDATA field.
326TTL a 32 bit unsigned integer that specifies the time interval
327 (in seconds) that the record may be cached.
328RDLENGTH a 16 bit integer, length in octets of the RDATA field.
329RDATA a variable length string of octets that describes the resource.
330 The format of this information varies according to the TYPE
331 and CLASS of the resource record.
332 If the TYPE is A and the CLASS is IN, it's a 4 octet IP address.
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000333
3344.1.4. Message compression
335
Denis Vlasenko5c329932009-04-12 12:16:21 +0000336In order to reduce the size of messages, domain names coan be compressed.
337An entire domain name or a list of labels at the end of a domain name
338is replaced with a pointer to a prior occurance of the same name.
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000339
340The pointer takes the form of a two octet sequence:
341 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
342 | 1 1| OFFSET |
343 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
344The first two bits are ones. This allows a pointer to be distinguished
345from a label, since the label must begin with two zero bits because
346labels are restricted to 63 octets or less. The OFFSET field specifies
347an offset from the start of the message (i.e., the first octet
348of the ID field in the domain header).
349A zero offset specifies the first byte of the ID field, etc.
Denis Vlasenko5c329932009-04-12 12:16:21 +0000350Domain name in a message can be represented as either:
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000351 - a sequence of labels ending in a zero octet
352 - a pointer
353 - a sequence of labels ending with a pointer
354 */
Denis Vlasenko5c329932009-04-12 12:16:21 +0000355static int process_packet(struct dns_entry *conf_data,
356 uint32_t conf_ttl,
357 uint8_t *buf)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000358{
Denis Vlasenko5c329932009-04-12 12:16:21 +0000359 char *answstr;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000360 struct dns_head *head;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000361 struct dns_prop *unaligned_qprop;
Denis Vlasenko5c329932009-04-12 12:16:21 +0000362 char *query_string;
363 uint8_t *answb;
Denis Vlasenko081eb712008-03-17 09:02:21 +0000364 uint16_t outr_rlen;
365 uint16_t outr_flags;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000366 uint16_t type;
367 uint16_t class;
Denis Vlasenko081eb712008-03-17 09:02:21 +0000368 int querystr_len;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000369
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000370 head = (struct dns_head *)buf;
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000371 if (head->nquer == 0) {
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000372 bb_error_msg("packet has 0 queries, ignored");
Denis Vlasenkob5b45a92007-03-24 13:09:07 +0000373 return -1;
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000374 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000375
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000376 if (head->flags & htons(0x8000)) { /* QR bit */
377 bb_error_msg("response packet, ignored");
Denis Vlasenkob5b45a92007-03-24 13:09:07 +0000378 return -1;
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000379 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000380
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000381 /* start of query string */
Denis Vlasenko5c329932009-04-12 12:16:21 +0000382 query_string = (void *)(head + 1);
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000383 /* caller guarantees strlen is <= MAX_PACK_LEN */
Denis Vlasenko5c329932009-04-12 12:16:21 +0000384 querystr_len = strlen(query_string) + 1;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000385 /* may be unaligned! */
Denis Vlasenko5c329932009-04-12 12:16:21 +0000386 unaligned_qprop = (void *)(query_string + querystr_len);
387 querystr_len += sizeof(unaligned_qprop);
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000388 /* where to append answer block */
389 answb = (void *)(unaligned_qprop + 1);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000390
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000391 /* QR = 1 "response", RCODE = 4 "Not Implemented" */
392 outr_flags = htons(0x8000 | 4);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000393
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000394 move_from_unaligned16(type, &unaligned_qprop->type);
395 if (type != htons(REQ_A) && type != htons(REQ_PTR)) {
396 /* we can't handle the query type */
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000397 goto empty_packet;
398 }
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000399 move_from_unaligned16(class, &unaligned_qprop->class);
400 if (class != htons(1)) { /* not class INET? */
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000401 goto empty_packet;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000402 }
403 /* OPCODE != 0 "standard query" ? */
404 if ((head->flags & htons(0x7800)) != 0) {
405 goto empty_packet;
406 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000407
Denis Vlasenko5c329932009-04-12 12:16:21 +0000408 /* look up the name */
409#if DEBUG
410 /* need to convert lengths to dots before we can use it in non-debug */
411 bb_info_msg("%s", query_string);
412#endif
413 answstr = table_lookup(conf_data, type, query_string);
414 outr_rlen = 4;
415 if (answstr && type == htons(REQ_PTR)) {
416 /* return a host name */
417 outr_rlen = strlen(answstr) + 1;
418 }
419 if (!answstr
420 || (unsigned)(answb - buf) + querystr_len + 4 + 2 + outr_rlen > MAX_PACK_LEN
421 ) {
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000422 /* QR = 1 "response"
423 * AA = 1 "Authoritative Answer"
424 * RCODE = 3 "Name Error" */
425 outr_flags = htons(0x8000 | 0x0400 | 3);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000426 goto empty_packet;
427 }
Denis Vlasenko5c329932009-04-12 12:16:21 +0000428
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000429 /* copy query block to answer block */
Denis Vlasenko5c329932009-04-12 12:16:21 +0000430 memcpy(answb, query_string, querystr_len);
Denis Vlasenko081eb712008-03-17 09:02:21 +0000431 answb += querystr_len;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000432 /* append answer Resource Record */
433 move_to_unaligned32((uint32_t *)answb, htonl(conf_ttl));
Denis Vlasenko081eb712008-03-17 09:02:21 +0000434 answb += 4;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000435 move_to_unaligned32((uint16_t *)answb, htons(outr_rlen));
Denis Vlasenko081eb712008-03-17 09:02:21 +0000436 answb += 2;
437 memcpy(answb, answstr, outr_rlen);
438 answb += outr_rlen;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000439
Denis Vlasenko5c329932009-04-12 12:16:21 +0000440 /* QR = 1 "response",
441 * AA = 1 "Authoritative Answer",
442 * RCODE = 0 "success" */
443 outr_flags = htons(0x8000 | 0x0400 | 0);
444 /* we have one answer */
445 head->nansw = htons(1);
446
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000447 empty_packet:
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000448 head->flags |= outr_flags;
Denis Vlasenko081eb712008-03-17 09:02:21 +0000449 head->nauth = head->nadd = 0;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000450 head->nquer = htons(1); // why???
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000451
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000452 return answb - buf;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000453}
454
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000455int dnsd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000456int dnsd_main(int argc UNUSED_PARAM, char **argv)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000457{
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000458 const char *listen_interface = "0.0.0.0";
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000459 const char *fileconf = "/etc/dnsd.conf";
460 struct dns_entry *conf_data;
461 uint32_t conf_ttl = DEFAULT_TTL;
Denis Vlasenko2c916522007-01-12 14:57:37 +0000462 char *sttl, *sport;
Denis Vlasenko081eb712008-03-17 09:02:21 +0000463 len_and_sockaddr *lsa, *from, *to;
464 unsigned lsa_size;
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000465 int udps, opts;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000466 uint16_t port = 53;
Denis Vlasenko081eb712008-03-17 09:02:21 +0000467 /* Paranoid sizing: querystring x2 + ttl + outr_rlen + answstr */
468 /* I'd rather see process_packet() fixed instead... */
469 uint8_t buf[MAX_PACK_LEN * 2 + 4 + 2 + (MAX_NAME_LEN+1)];
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000470
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000471 opts = getopt32(argv, "vi:c:t:p:d", &listen_interface, &fileconf, &sttl, &sport);
472 //if (opts & 0x1) // -v
473 //if (opts & 0x2) // -i
474 //if (opts & 0x4) // -c
475 if (opts & 0x8) // -t
476 conf_ttl = xatou_range(sttl, 1, 0xffffffff);
477 if (opts & 0x10) // -p
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000478 port = xatou_range(sport, 1, 0xffff);
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000479 if (opts & 0x20) { // -d
Denis Vlasenko5a142022007-03-26 13:20:54 +0000480 bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv);
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000481 openlog(applet_name, LOG_PID, LOG_DAEMON);
Denis Vlasenko2c916522007-01-12 14:57:37 +0000482 logmode = LOGMODE_SYSLOG;
483 }
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000484 /* Clear all except "verbose" bit */
485 option_mask32 &= 1;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000486
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000487 conf_data = parse_conf_file(fileconf);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000488
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000489 lsa = xdotted2sockaddr(listen_interface, port);
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000490 udps = xsocket(lsa->u.sa.sa_family, SOCK_DGRAM, 0);
491 xbind(udps, &lsa->u.sa, lsa->len);
Denis Vlasenko081eb712008-03-17 09:02:21 +0000492 socket_want_pktinfo(udps); /* needed for recv_from_to to work */
493 lsa_size = LSA_LEN_SIZE + lsa->len;
494 from = xzalloc(lsa_size);
495 to = xzalloc(lsa_size);
496
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000497 {
498 char *p = xmalloc_sockaddr2dotted(&lsa->u.sa);
499 bb_info_msg("Accepting UDP packets on %s", p);
500 free(p);
501 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000502
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000503 while (1) {
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000504 int r;
Denis Vlasenko081eb712008-03-17 09:02:21 +0000505 /* Try to get *DEST* address (to which of our addresses
506 * this query was directed), and reply from the same address.
507 * Or else we can exhibit usual UDP ugliness:
508 * [ip1.multihomed.ip2] <= query to ip1 <= peer
509 * [ip1.multihomed.ip2] => reply from ip2 => peer (confused) */
510 memcpy(to, lsa, lsa_size);
511 r = recv_from_to(udps, buf, MAX_PACK_LEN + 1, 0, &from->u.sa, &to->u.sa, lsa->len);
512 if (r < 12 || r > MAX_PACK_LEN) {
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000513 bb_error_msg("packet size %d, ignored", r);
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000514 continue;
Denis Vlasenko2c916522007-01-12 14:57:37 +0000515 }
Denis Vlasenko081eb712008-03-17 09:02:21 +0000516 if (OPT_verbose)
517 bb_info_msg("Got UDP packet");
518 buf[r] = '\0'; /* paranoia */
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000519 r = process_packet(conf_data, conf_ttl, buf);
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000520 if (r <= 0)
521 continue;
Denis Vlasenkoe9b76e12008-05-22 17:41:01 +0000522 send_to_from(udps, buf, r, 0, &from->u.sa, &to->u.sa, lsa->len);
Denis Vlasenko2c916522007-01-12 14:57:37 +0000523 }
Denis Vlasenkob5b45a92007-03-24 13:09:07 +0000524 return 0;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000525}