blob: e73e244b0767747bd97cde5dda4d03e6ad73397d [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 Vlasenko5fb38492010-02-06 22:48:10 +010047struct type_and_class {
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000048 uint16_t type;
49 uint16_t class;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000050};
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000051/* element of known name, ip address and reversed ip address */
52struct dns_entry {
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000053 struct dns_entry *next;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000054 uint32_t ip;
55 char rip[IP_STRING_LEN]; /* length decimal reversed IP */
56 char name[1];
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000057};
58
Denys Vlasenko343dfd72010-02-07 02:45:03 +010059#define OPT_verbose (option_mask32 & 1)
60#define OPT_silent (option_mask32 & 2)
Denis Vlasenkod3bac032007-03-24 12:13:04 +000061
62
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000063/*
Bernhard Reutner-Fischerc418d482006-05-31 10:19:51 +000064 * Insert length of substrings instead of dots
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000065 */
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000066static void undot(char *rip)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000067{
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000068 int i = 0;
69 int s = 0;
70
Denis Vlasenkoc12f5302006-10-06 09:49:47 +000071 while (rip[i])
72 i++;
73 for (--i; i >= 0; i--) {
74 if (rip[i] == '.') {
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000075 rip[i] = s;
76 s = 0;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000077 } else {
78 s++;
79 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000080 }
81}
82
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000083/*
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000084 * Read hostname/IP records from file
85 */
Denis Vlasenkoa19e6492009-03-11 14:40:00 +000086static struct dns_entry *parse_conf_file(const char *fileconf)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000087{
Denis Vlasenko084266e2008-07-26 23:08:31 +000088 char *token[2];
Denis Vlasenkoa34f1ed2008-07-20 17:48:59 +000089 parser_t *parser;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000090 struct dns_entry *m, *conf_data;
91 struct dns_entry **nextp;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000092
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000093 conf_data = NULL;
94 nextp = &conf_data;
95
Denis Vlasenkoa34f1ed2008-07-20 17:48:59 +000096 parser = config_open(fileconf);
Denis Vlasenko084266e2008-07-26 23:08:31 +000097 while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) {
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000098 struct in_addr ip;
99 uint32_t v32;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000100
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000101 if (inet_aton(token[1], &ip) == 0) {
102 bb_error_msg("error at line %u, skipping", parser->lineno);
103 continue;
104 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000105
Denis Vlasenko084266e2008-07-26 23:08:31 +0000106 if (OPT_verbose)
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000107 bb_error_msg("name:%s, ip:%s", token[0], token[1]);
Denis Vlasenkoa34f1ed2008-07-20 17:48:59 +0000108
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000109 /* sizeof(*m) includes 1 byte for m->name[0] */
110 m = xzalloc(sizeof(*m) + strlen(token[0]) + 1);
111 /*m->next = NULL;*/
112 *nextp = m;
113 nextp = &m->next;
114
115 m->name[0] = '.';
116 strcpy(m->name + 1, token[0]);
117 undot(m->name);
118 m->ip = ip.s_addr; /* in network order */
119 v32 = ntohl(m->ip);
120 /* inverted order */
121 sprintf(m->rip, ".%u.%u.%u.%u",
122 (uint8_t)(v32),
123 (uint8_t)(v32 >> 8),
124 (uint8_t)(v32 >> 16),
125 (v32 >> 24)
126 );
127 undot(m->rip);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000128 }
Denis Vlasenko084266e2008-07-26 23:08:31 +0000129 config_close(parser);
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000130 return conf_data;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000131}
132
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000133/*
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000134 * Look query up in dns records and return answer if found.
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000135 */
Denis Vlasenko5c329932009-04-12 12:16:21 +0000136static char *table_lookup(struct dns_entry *d,
137 uint16_t type,
138 char* query_string)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000139{
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000140 while (d) {
141 unsigned len = d->name[0];
142 /* d->name[len] is the last (non NUL) char */
Denis Vlasenko91f20ab2007-01-20 01:47:44 +0000143#if DEBUG
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000144 char *p, *q;
Denis Vlasenko5c329932009-04-12 12:16:21 +0000145 q = query_string + 1;
146 p = d->name + 1;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000147 fprintf(stderr, "%d/%d p:%s q:%s %d\n",
148 (int)strlen(p), len,
149 p, q, (int)strlen(q)
150 );
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000151#endif
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000152 if (type == htons(REQ_A)) {
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000153 /* search by host name */
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000154 if (len != 1 || d->name[1] != '*') {
Denis Vlasenko5c329932009-04-12 12:16:21 +0000155/* we are lax, hope no name component is ever >64 so that length
156 * (which will be represented as 'A','B'...) matches a lowercase letter.
157 * Actually, I think false matches are hard to construct.
158 * Example.
159 * [31] len is represented as '1', [65] as 'A', [65+32] as 'a'.
160 * [65] <65 same chars>[31]<31 same chars>NUL
161 * [65+32]<65 same chars>1 <31 same chars>NUL
162 * This example seems to be the minimal case when false match occurs.
163 */
164 if (strcasecmp(d->name, query_string) != 0)
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000165 goto next;
166 }
Denis Vlasenko5c329932009-04-12 12:16:21 +0000167 return (char *)&d->ip;
Denis Vlasenko91f20ab2007-01-20 01:47:44 +0000168#if DEBUG
Denis Vlasenko5c329932009-04-12 12:16:21 +0000169 fprintf(stderr, "Found IP:%x\n", (int)d->ip);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000170#endif
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000171 return 0;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000172 }
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000173 /* search by IP-address */
174 if ((len != 1 || d->name[1] != '*')
Denis Vlasenko5c329932009-04-12 12:16:21 +0000175 /* we assume (do not check) that query_string
176 * ends in ".in-addr.arpa" */
177 && strncmp(d->rip, query_string, strlen(d->rip)) == 0
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000178 ) {
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000179#if DEBUG
Denis Vlasenko5c329932009-04-12 12:16:21 +0000180 fprintf(stderr, "Found name:%s\n", d->name);
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000181#endif
Denis Vlasenko5c329932009-04-12 12:16:21 +0000182 return d->name;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000183 }
184 next:
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000185 d = d->next;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000186 }
187
Denis Vlasenko5c329932009-04-12 12:16:21 +0000188 return NULL;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000189}
190
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000191/*
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000192 * Decode message and generate answer
193 */
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000194/* RFC 1035
195...
196Whenever an octet represents a numeric quantity, the left most bit
197in the diagram is the high order or most significant bit.
198That is, the bit labeled 0 is the most significant bit.
199...
200
2014.1.1. Header section format
Denis Vlasenko5c329932009-04-12 12:16:21 +0000202 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000203 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
204 | ID |
205 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
Denis Vlasenko5c329932009-04-12 12:16:21 +0000206 |QR| OPCODE |AA|TC|RD|RA| 0 0 0| RCODE |
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000207 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
208 | QDCOUNT |
209 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
210 | ANCOUNT |
211 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
212 | NSCOUNT |
213 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
214 | ARCOUNT |
215 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
Denis Vlasenko5c329932009-04-12 12:16:21 +0000216ID 16 bit random identifier assigned by querying peer.
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000217 Used to match query/response.
218QR message is a query (0), or a response (1).
219OPCODE 0 standard query (QUERY)
220 1 inverse query (IQUERY)
221 2 server status request (STATUS)
Denis Vlasenko5c329932009-04-12 12:16:21 +0000222AA Authoritative Answer - this bit is valid in responses.
223 Responding name server is an authority for the domain name
Denis Vlasenkoef1b4392009-04-12 19:03:01 +0000224 in question section. Answer section may have multiple owner names
225 because of aliases. The AA bit corresponds to the name which matches
226 the query name, or the first owner name in the answer section.
Denis Vlasenko5c329932009-04-12 12:16:21 +0000227TC TrunCation - this message was truncated.
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000228RD Recursion Desired - this bit may be set in a query and
229 is copied into the response. If RD is set, it directs
230 the name server to pursue the query recursively.
231 Recursive query support is optional.
232RA Recursion Available - this be is set or cleared in a
233 response, and denotes whether recursive query support is
234 available in the name server.
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000235RCODE Response code.
236 0 No error condition
237 1 Format error
Denis Vlasenko5c329932009-04-12 12:16:21 +0000238 2 Server failure - server was unable to process the query
Denis Vlasenkoef1b4392009-04-12 19:03:01 +0000239 due to a problem with the name server.
Denis Vlasenko5c329932009-04-12 12:16:21 +0000240 3 Name Error - meaningful only for responses from
Denis Vlasenkoef1b4392009-04-12 19:03:01 +0000241 an authoritative name server. The referenced domain name
242 does not exist.
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000243 4 Not Implemented.
244 5 Refused.
245QDCOUNT number of entries in the question section.
Denis Vlasenko5c329932009-04-12 12:16:21 +0000246ANCOUNT number of records in the answer section.
247NSCOUNT number of records in the authority records section.
248ARCOUNT number of records in the additional records section.
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000249
2504.1.2. Question section format
251
Denis Vlasenko5c329932009-04-12 12:16:21 +0000252The section contains QDCOUNT (usually 1) entries, each of this format:
253 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000254 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
255 / QNAME /
256 / /
257 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
258 | QTYPE |
259 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
260 | QCLASS |
261 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
262QNAME a domain name represented as a sequence of labels, where
263 each label consists of a length octet followed by that
264 number of octets. The domain name terminates with the
265 zero length octet for the null label of the root. Note
266 that this field may be an odd number of octets; no
267 padding is used.
268QTYPE a two octet type of the query.
269 1 a host address [REQ_A const]
270 2 an authoritative name server
271 3 a mail destination (Obsolete - use MX)
272 4 a mail forwarder (Obsolete - use MX)
273 5 the canonical name for an alias
274 6 marks the start of a zone of authority
275 7 a mailbox domain name (EXPERIMENTAL)
276 8 a mail group member (EXPERIMENTAL)
277 9 a mail rename domain name (EXPERIMENTAL)
278 10 a null RR (EXPERIMENTAL)
279 11 a well known service description
280 12 a domain name pointer [REQ_PTR const]
281 13 host information
282 14 mailbox or mail list information
283 15 mail exchange
284 16 text strings
285 0x1c IPv6?
286 252 a request for a transfer of an entire zone
287 253 a request for mailbox-related records (MB, MG or MR)
288 254 a request for mail agent RRs (Obsolete - see MX)
289 255 a request for all records
290QCLASS a two octet code that specifies the class of the query.
291 1 the Internet
Denis Vlasenkoef1b4392009-04-12 19:03:01 +0000292 (others are historic only)
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000293 255 any class
294
Denys Vlasenko5fb38492010-02-06 22:48:10 +01002954.1.3. Resource Record format
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000296
Denis Vlasenko5c329932009-04-12 12:16:21 +0000297The answer, authority, and additional sections all share the same format:
298a variable number of resource records, where the number of records
299is specified in the corresponding count field in the header.
300Each resource record has this format:
301 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000302 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
303 / /
304 / NAME /
305 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
306 | TYPE |
307 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
308 | CLASS |
309 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
310 | TTL |
311 | |
312 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
313 | RDLENGTH |
314 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--|
315 / RDATA /
316 / /
317 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
318NAME a domain name to which this resource record pertains.
319TYPE two octets containing one of the RR type codes. This
Denis Vlasenko5c329932009-04-12 12:16:21 +0000320 field specifies the meaning of the data in the RDATA field.
321CLASS two octets which specify the class of the data in the RDATA field.
322TTL a 32 bit unsigned integer that specifies the time interval
323 (in seconds) that the record may be cached.
324RDLENGTH a 16 bit integer, length in octets of the RDATA field.
325RDATA a variable length string of octets that describes the resource.
326 The format of this information varies according to the TYPE
327 and CLASS of the resource record.
328 If the TYPE is A and the CLASS is IN, it's a 4 octet IP address.
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000329
3304.1.4. Message compression
331
Denis Vlasenko5c329932009-04-12 12:16:21 +0000332In order to reduce the size of messages, domain names coan be compressed.
333An entire domain name or a list of labels at the end of a domain name
334is replaced with a pointer to a prior occurance of the same name.
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000335
336The pointer takes the form of a two octet sequence:
337 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
338 | 1 1| OFFSET |
339 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
340The first two bits are ones. This allows a pointer to be distinguished
341from a label, since the label must begin with two zero bits because
342labels are restricted to 63 octets or less. The OFFSET field specifies
343an offset from the start of the message (i.e., the first octet
344of the ID field in the domain header).
345A zero offset specifies the first byte of the ID field, etc.
Denis Vlasenko5c329932009-04-12 12:16:21 +0000346Domain name in a message can be represented as either:
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000347 - a sequence of labels ending in a zero octet
348 - a pointer
349 - a sequence of labels ending with a pointer
350 */
Denis Vlasenko5c329932009-04-12 12:16:21 +0000351static int process_packet(struct dns_entry *conf_data,
352 uint32_t conf_ttl,
353 uint8_t *buf)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000354{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000355 struct dns_head *head;
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100356 struct type_and_class *unaligned_type_class;
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100357 const char *err_msg;
Denis Vlasenko5c329932009-04-12 12:16:21 +0000358 char *query_string;
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100359 char *answstr;
Denis Vlasenko5c329932009-04-12 12:16:21 +0000360 uint8_t *answb;
Denis Vlasenko081eb712008-03-17 09:02:21 +0000361 uint16_t outr_rlen;
362 uint16_t outr_flags;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000363 uint16_t type;
364 uint16_t class;
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100365 int query_len;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000366
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000367 head = (struct dns_head *)buf;
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000368 if (head->nquer == 0) {
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000369 bb_error_msg("packet has 0 queries, ignored");
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100370 return 0; /* don't reply */
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000371 }
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000372 if (head->flags & htons(0x8000)) { /* QR bit */
373 bb_error_msg("response packet, ignored");
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100374 return 0; /* don't reply */
375 }
376 /* QR = 1 "response", RCODE = 4 "Not Implemented" */
377 outr_flags = htons(0x8000 | 4);
378 err_msg = NULL;
379 /* OPCODE != 0 "standard query" ? */
380 if ((head->flags & htons(0x7800)) != 0) {
381 err_msg = "opcode != 0";
382 goto empty_packet;
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000383 }
"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 Vlasenko343dfd72010-02-07 02:45:03 +0100395 move_from_unaligned16(class, &unaligned_type_class->class);
396 if (class != htons(1)) { /* not class INET? */
397 err_msg = "class != 1";
398 goto empty_packet;
399 }
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100400 move_from_unaligned16(type, &unaligned_type_class->type);
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000401 if (type != htons(REQ_A) && type != htons(REQ_PTR)) {
Denys Vlasenkoe66a09b2010-02-07 01:11:18 +0100402 /* we can't handle this query type */
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100403//TODO: happens all the time with REQ_AAAA (0x1c) requests - implement those?
404 err_msg = "type is !REQ_A and !REQ_PTR";
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000405 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 */
Denis Vlasenko5c329932009-04-12 12:16:21 +0000409 answstr = table_lookup(conf_data, type, query_string);
Denys Vlasenkoe66a09b2010-02-07 01:11:18 +0100410#if DEBUG
411 /* Shows lengths instead of dots, unusable for !DEBUG */
412 bb_error_msg("'%s'->'%s'", query_string, answstr);
413#endif
Denis Vlasenko5c329932009-04-12 12:16:21 +0000414 outr_rlen = 4;
415 if (answstr && type == htons(REQ_PTR)) {
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100416 /* returning a host name */
Denis Vlasenko5c329932009-04-12 12:16:21 +0000417 outr_rlen = strlen(answstr) + 1;
418 }
419 if (!answstr
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100420 || (unsigned)(answb - buf) + query_len + 4 + 2 + outr_rlen > MAX_PACK_LEN
Denis Vlasenko5c329932009-04-12 12:16:21 +0000421 ) {
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000422 /* QR = 1 "response"
423 * AA = 1 "Authoritative Answer"
424 * RCODE = 3 "Name Error" */
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100425 err_msg = "name is not found";
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000426 outr_flags = htons(0x8000 | 0x0400 | 3);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000427 goto empty_packet;
428 }
Denis Vlasenko5c329932009-04-12 12:16:21 +0000429
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100430 /* Append answer Resource Record */
431 memcpy(answb, query_string, query_len); /* name, type, class */
432 answb += query_len;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000433 move_to_unaligned32((uint32_t *)answb, htonl(conf_ttl));
Denis Vlasenko081eb712008-03-17 09:02:21 +0000434 answb += 4;
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100435 move_to_unaligned16((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",
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100442 * TODO: need to set RA bit 0x80? One user says nslookup complains
443 * "Got recursion not available from SERVER, trying next server"
444 * "** server can't find HOSTNAME"
445 * RCODE = 0 "success"
446 */
Denys Vlasenkoe66a09b2010-02-07 01:11:18 +0100447 if (OPT_verbose)
448 bb_error_msg("returning positive reply");
Denis Vlasenko5c329932009-04-12 12:16:21 +0000449 outr_flags = htons(0x8000 | 0x0400 | 0);
450 /* we have one answer */
451 head->nansw = htons(1);
452
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000453 empty_packet:
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100454 if ((outr_flags & htons(0xf)) != 0) { /* not a positive response */
455 if (OPT_verbose) {
456 bb_error_msg("%s, %s",
457 err_msg,
458 OPT_silent ? "dropping query" : "sending error reply"
459 );
460 }
461 if (OPT_silent)
462 return 0;
463 }
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000464 head->flags |= outr_flags;
Denis Vlasenko081eb712008-03-17 09:02:21 +0000465 head->nauth = head->nadd = 0;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000466 head->nquer = htons(1); // why???
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000467
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000468 return answb - buf;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000469}
470
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000471int dnsd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000472int dnsd_main(int argc UNUSED_PARAM, char **argv)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000473{
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000474 const char *listen_interface = "0.0.0.0";
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000475 const char *fileconf = "/etc/dnsd.conf";
476 struct dns_entry *conf_data;
477 uint32_t conf_ttl = DEFAULT_TTL;
Denis Vlasenko2c916522007-01-12 14:57:37 +0000478 char *sttl, *sport;
Denis Vlasenko081eb712008-03-17 09:02:21 +0000479 len_and_sockaddr *lsa, *from, *to;
480 unsigned lsa_size;
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000481 int udps, opts;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000482 uint16_t port = 53;
Denis Vlasenkoef1b4392009-04-12 19:03:01 +0000483 uint8_t buf[MAX_PACK_LEN + 1];
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000484
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100485 opts = getopt32(argv, "vsi:c:t:p:d", &listen_interface, &fileconf, &sttl, &sport);
486 //if (opts & (1 << 0)) // -v
487 //if (opts & (1 << 1)) // -s
488 //if (opts & (1 << 2)) // -i
489 //if (opts & (1 << 3)) // -c
490 if (opts & (1 << 4)) // -t
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000491 conf_ttl = xatou_range(sttl, 1, 0xffffffff);
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100492 if (opts & (1 << 5)) // -p
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000493 port = xatou_range(sport, 1, 0xffff);
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100494 if (opts & (1 << 6)) { // -d
Denis Vlasenko5a142022007-03-26 13:20:54 +0000495 bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv);
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000496 openlog(applet_name, LOG_PID, LOG_DAEMON);
Denis Vlasenko2c916522007-01-12 14:57:37 +0000497 logmode = LOGMODE_SYSLOG;
498 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000499
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000500 conf_data = parse_conf_file(fileconf);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000501
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000502 lsa = xdotted2sockaddr(listen_interface, port);
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000503 udps = xsocket(lsa->u.sa.sa_family, SOCK_DGRAM, 0);
504 xbind(udps, &lsa->u.sa, lsa->len);
Denis Vlasenko081eb712008-03-17 09:02:21 +0000505 socket_want_pktinfo(udps); /* needed for recv_from_to to work */
506 lsa_size = LSA_LEN_SIZE + lsa->len;
507 from = xzalloc(lsa_size);
508 to = xzalloc(lsa_size);
509
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000510 {
511 char *p = xmalloc_sockaddr2dotted(&lsa->u.sa);
Denys Vlasenkoe66a09b2010-02-07 01:11:18 +0100512 bb_error_msg("accepting UDP packets on %s", p);
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000513 free(p);
514 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000515
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000516 while (1) {
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000517 int r;
Denis Vlasenko081eb712008-03-17 09:02:21 +0000518 /* Try to get *DEST* address (to which of our addresses
519 * this query was directed), and reply from the same address.
520 * Or else we can exhibit usual UDP ugliness:
521 * [ip1.multihomed.ip2] <= query to ip1 <= peer
522 * [ip1.multihomed.ip2] => reply from ip2 => peer (confused) */
523 memcpy(to, lsa, lsa_size);
524 r = recv_from_to(udps, buf, MAX_PACK_LEN + 1, 0, &from->u.sa, &to->u.sa, lsa->len);
525 if (r < 12 || r > MAX_PACK_LEN) {
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000526 bb_error_msg("packet size %d, ignored", r);
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000527 continue;
Denis Vlasenko2c916522007-01-12 14:57:37 +0000528 }
Denis Vlasenko081eb712008-03-17 09:02:21 +0000529 if (OPT_verbose)
Denys Vlasenkoe66a09b2010-02-07 01:11:18 +0100530 bb_error_msg("got UDP packet");
Denis Vlasenko081eb712008-03-17 09:02:21 +0000531 buf[r] = '\0'; /* paranoia */
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000532 r = process_packet(conf_data, conf_ttl, buf);
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000533 if (r <= 0)
534 continue;
Denis Vlasenkoe9b76e12008-05-22 17:41:01 +0000535 send_to_from(udps, buf, r, 0, &from->u.sa, &to->u.sa, lsa->len);
Denis Vlasenko2c916522007-01-12 14:57:37 +0000536 }
Denis Vlasenkob5b45a92007-03-24 13:09:07 +0000537 return 0;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000538}