h_dns_server.c revision 1.3 1 1.3 christos /* $NetBSD: h_dns_server.c,v 1.3 2014/01/09 02:18:10 christos Exp $ */
2 1.1 gson
3 1.1 gson /*-
4 1.1 gson * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 1.1 gson * All rights reserved.
6 1.1 gson *
7 1.1 gson * This code is derived from software contributed to The NetBSD Foundation
8 1.1 gson * by Andreas Gustafsson.
9 1.1 gson *
10 1.1 gson * Redistribution and use in source and binary forms, with or without
11 1.1 gson * modification, are permitted provided that the following conditions
12 1.1 gson * are met:
13 1.1 gson * 1. Redistributions of source code must retain the above copyright
14 1.1 gson * notice, this list of conditions and the following disclaimer.
15 1.1 gson * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 gson * notice, this list of conditions and the following disclaimer in the
17 1.1 gson * documentation and/or other materials provided with the distribution.
18 1.1 gson *
19 1.1 gson * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 gson * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 gson * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 gson * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 gson * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 gson * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 gson * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 gson * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 gson * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 gson * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 gson * POSSIBILITY OF SUCH DAMAGE.
30 1.1 gson */
31 1.1 gson
32 1.1 gson /*
33 1.1 gson * A minimal DNS server capable of providing canned answers to the
34 1.1 gson * specific queries issued by t_hostent.sh and nothing more.
35 1.1 gson */
36 1.1 gson
37 1.1 gson #include <sys/cdefs.h>
38 1.3 christos __RCSID("$NetBSD: h_dns_server.c,v 1.3 2014/01/09 02:18:10 christos Exp $");
39 1.1 gson
40 1.1 gson #include <ctype.h>
41 1.1 gson #include <err.h>
42 1.1 gson #include <errno.h>
43 1.1 gson #include <fcntl.h>
44 1.1 gson #include <memory.h>
45 1.1 gson #include <paths.h>
46 1.1 gson #include <stdio.h>
47 1.1 gson #include <stdlib.h>
48 1.1 gson #include <unistd.h>
49 1.1 gson
50 1.1 gson #include <sys/socket.h>
51 1.1 gson
52 1.1 gson #include <netinet/in.h>
53 1.1 gson #include <netinet6/in6.h>
54 1.1 gson
55 1.1 gson union sockaddr_either {
56 1.1 gson struct sockaddr s;
57 1.1 gson struct sockaddr_in sin;
58 1.1 gson struct sockaddr_in6 sin6;
59 1.1 gson };
60 1.1 gson
61 1.3 christos #ifdef DEBUG
62 1.3 christos #define DPRINTF(...) fprintf(stderr, __VA_ARGS__)
63 1.3 christos #else
64 1.3 christos #define DPRINTF(...)
65 1.3 christos #endif
66 1.3 christos
67 1.1 gson /* A DNS question and its corresponding answer */
68 1.1 gson
69 1.1 gson struct dns_data {
70 1.1 gson size_t qname_size;
71 1.1 gson const char *qname; /* Wire-encode question name */
72 1.1 gson int qtype;
73 1.1 gson size_t answer_size;
74 1.1 gson const char *answer; /* One wire-encoded answer RDATA */
75 1.1 gson };
76 1.1 gson
77 1.1 gson /* Convert C string constant to length + data pair */
78 1.1 gson #define STR_DATA(s) sizeof(s) - 1, s
79 1.1 gson
80 1.1 gson /* Canned DNS queestion-answer pairs */
81 1.1 gson struct dns_data data[] = {
82 1.1 gson /* Forward mappings */
83 1.1 gson /* localhost IN A -> 127.0.0.1 */
84 1.1 gson { STR_DATA("\011localhost\000"), 1,
85 1.1 gson STR_DATA("\177\000\000\001") },
86 1.1 gson /* localhost IN AAAA -> ::1 */
87 1.1 gson { STR_DATA("\011localhost\000"), 28,
88 1.1 gson STR_DATA("\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\001") },
89 1.1 gson /* sixthavenue.astron.com IN A -> 38.117.134.16 */
90 1.1 gson { STR_DATA("\013sixthavenue\006astron\003com\000"), 1,
91 1.1 gson STR_DATA("\046\165\206\020") },
92 1.1 gson /* sixthavenue.astron.com IN AAAA -> 2620:106:3003:1f00:3e4a:92ff:fef4:e180 */
93 1.1 gson { STR_DATA("\013sixthavenue\006astron\003com\000"), 28,
94 1.1 gson STR_DATA("\x26\x20\x01\x06\x30\x03\x1f\x00\x3e\x4a\x92\xff\xfe\xf4\xe1\x80") },
95 1.1 gson /* Reverse mappings */
96 1.1 gson { STR_DATA("\0011\0010\0010\003127\007in-addr\004arpa\000"), 12,
97 1.1 gson STR_DATA("\011localhost\000") },
98 1.1 gson { STR_DATA("\0011\0010\0010\0010\0010\0010\0010\0010"
99 1.1 gson "\0010\0010\0010\0010\0010\0010\0010\0010"
100 1.1 gson "\0010\0010\0010\0010\0010\0010\0010\0010"
101 1.1 gson "\0010\0010\0010\0010\0010\0010\0010\0010"
102 1.1 gson "\003ip6\004arpa\000"), 12,
103 1.1 gson STR_DATA("\011localhost\000") },
104 1.1 gson { STR_DATA("\00216\003134\003117\00238"
105 1.1 gson "\007in-addr\004arpa\000"), 12,
106 1.1 gson STR_DATA("\013sixthavenue\006astron\003com\000") },
107 1.1 gson { STR_DATA("\0010\0018\0011\001e\0014\001f\001e\001f"
108 1.1 gson "\001f\001f\0012\0019\001a\0014\001e\0013"
109 1.1 gson "\0010\0010\001f\0011\0013\0010\0010\0013"
110 1.1 gson "\0016\0010\0011\0010\0010\0012\0016\0012"
111 1.1 gson "\003ip6\004arpa\000"), 12,
112 1.1 gson STR_DATA("\013sixthavenue\006astron\003com\000") },
113 1.1 gson /* End marker */
114 1.1 gson { STR_DATA(""), 0, STR_DATA("") }
115 1.1 gson };
116 1.1 gson
117 1.1 gson /*
118 1.1 gson * Compare two DNS names for equality. If equal, return their
119 1.1 gson * length, and if not, return zero. Does not handle compression.
120 1.1 gson */
121 1.1 gson static int
122 1.1 gson name_eq(const unsigned char *a, const unsigned char *b) {
123 1.1 gson const unsigned char *a_save = a;
124 1.1 gson for (;;) {
125 1.1 gson int i;
126 1.1 gson int lena = *a++;
127 1.1 gson int lenb = *b++;
128 1.1 gson if (lena != lenb)
129 1.1 gson return 0;
130 1.1 gson if (lena == 0)
131 1.1 gson return a - a_save;
132 1.1 gson for (i = 0; i < lena; i++)
133 1.1 gson if (tolower(a[i]) != tolower(b[i]))
134 1.1 gson return 0;
135 1.1 gson a += lena;
136 1.1 gson b += lena;
137 1.1 gson }
138 1.1 gson }
139 1.1 gson
140 1.3 christos #ifdef DEBUG
141 1.3 christos static char *
142 1.3 christos name2str(const void *v, char *buf, size_t buflen) {
143 1.3 christos const unsigned char *a = v;
144 1.3 christos char *b = buf;
145 1.3 christos char *eb = buf + buflen;
146 1.3 christos
147 1.3 christos #define ADDC(c) do { \
148 1.3 christos if (b < eb) \
149 1.3 christos *b++ = c; \
150 1.3 christos else \
151 1.3 christos return NULL; \
152 1.3 christos } while (/*CONSTCOND*/0)
153 1.3 christos for (int did = 0;; did++) {
154 1.3 christos int lena = *a++;
155 1.3 christos if (lena == 0) {
156 1.3 christos ADDC('\0');
157 1.3 christos return buf;
158 1.3 christos }
159 1.3 christos if (did)
160 1.3 christos ADDC('.');
161 1.3 christos for (int i = 0; i < lena; i++)
162 1.3 christos ADDC(a[i]);
163 1.3 christos a += lena;
164 1.3 christos }
165 1.3 christos }
166 1.3 christos #endif
167 1.3 christos
168 1.1 gson /* XXX the daemon2_* functions should be in a library */
169 1.1 gson
170 1.2 gson int __daemon2_detach_pipe[2];
171 1.1 gson
172 1.1 gson static int
173 1.1 gson daemon2_fork(void)
174 1.1 gson {
175 1.1 gson int r;
176 1.1 gson int fd;
177 1.1 gson int i;
178 1.1 gson
179 1.1 gson /*
180 1.1 gson * Set up the pipe, making sure the write end does not
181 1.1 gson * get allocated one of the file descriptors that will
182 1.2 gson * be closed in daemon2_detach().
183 1.1 gson */
184 1.1 gson for (i = 0; i < 3; i++) {
185 1.2 gson r = pipe(__daemon2_detach_pipe);
186 1.1 gson if (r < 0)
187 1.1 gson return -1;
188 1.2 gson if (__daemon2_detach_pipe[1] <= STDERR_FILENO &&
189 1.1 gson (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
190 1.2 gson (void)dup2(fd, __daemon2_detach_pipe[0]);
191 1.2 gson (void)dup2(fd, __daemon2_detach_pipe[1]);
192 1.1 gson if (fd > STDERR_FILENO)
193 1.1 gson (void)close(fd);
194 1.1 gson continue;
195 1.1 gson }
196 1.1 gson break;
197 1.1 gson }
198 1.1 gson
199 1.1 gson r = fork();
200 1.1 gson if (r < 0) {
201 1.1 gson return -1;
202 1.1 gson } else if (r == 0) {
203 1.1 gson /* child */
204 1.2 gson close(__daemon2_detach_pipe[0]);
205 1.1 gson return 0;
206 1.1 gson }
207 1.1 gson /* Parent */
208 1.1 gson
209 1.2 gson (void) close(__daemon2_detach_pipe[1]);
210 1.1 gson
211 1.1 gson for (;;) {
212 1.1 gson char dummy;
213 1.2 gson r = read(__daemon2_detach_pipe[0], &dummy, 1);
214 1.1 gson if (r < 0) {
215 1.1 gson if (errno == EINTR)
216 1.1 gson continue;
217 1.1 gson _exit(1);
218 1.1 gson } else if (r == 0) {
219 1.1 gson _exit(1);
220 1.1 gson } else { /* r > 0 */
221 1.1 gson _exit(0);
222 1.1 gson }
223 1.1 gson }
224 1.1 gson }
225 1.1 gson
226 1.1 gson static int
227 1.2 gson daemon2_detach(int nochdir, int noclose)
228 1.1 gson {
229 1.1 gson int r;
230 1.1 gson int fd;
231 1.1 gson
232 1.1 gson if (setsid() == -1)
233 1.1 gson return -1;
234 1.1 gson
235 1.1 gson if (!nochdir)
236 1.1 gson (void)chdir("/");
237 1.1 gson
238 1.1 gson if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
239 1.1 gson (void)dup2(fd, STDIN_FILENO);
240 1.1 gson (void)dup2(fd, STDOUT_FILENO);
241 1.1 gson (void)dup2(fd, STDERR_FILENO);
242 1.1 gson if (fd > STDERR_FILENO)
243 1.1 gson (void)close(fd);
244 1.1 gson }
245 1.1 gson
246 1.1 gson while (1) {
247 1.2 gson r = write(__daemon2_detach_pipe[1], "", 1);
248 1.1 gson if (r < 0) {
249 1.1 gson if (errno == EINTR)
250 1.1 gson continue;
251 1.1 gson /* May get "broken pipe" here if parent is killed */
252 1.1 gson return -1;
253 1.1 gson } else if (r == 0) {
254 1.1 gson /* Should not happen */
255 1.1 gson return -1;
256 1.1 gson } else {
257 1.1 gson break;
258 1.1 gson }
259 1.1 gson }
260 1.1 gson
261 1.2 gson (void) close(__daemon2_detach_pipe[1]);
262 1.1 gson
263 1.1 gson return 0;
264 1.1 gson }
265 1.1 gson
266 1.1 gson int main(int argc, char **argv) {
267 1.1 gson int s, r, protocol;
268 1.1 gson union sockaddr_either saddr;
269 1.1 gson struct dns_data *dp;
270 1.1 gson unsigned char *p;
271 1.1 gson char pidfile_name[40];
272 1.1 gson FILE *f;
273 1.1 gson int one = 1;
274 1.3 christos #ifdef DEBUG
275 1.3 christos char buf1[1024], buf2[1024];
276 1.3 christos #endif
277 1.1 gson
278 1.1 gson daemon2_fork();
279 1.1 gson
280 1.1 gson if (argc < 2 || ((protocol = argv[1][0]) != '4' && protocol != '6'))
281 1.1 gson errx(1, "usage: dns_server 4 | 6");
282 1.1 gson s = socket(protocol == '4' ? PF_INET : PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
283 1.1 gson if (s < 0)
284 1.1 gson err(1, "socket");
285 1.1 gson if (protocol == '4') {
286 1.1 gson memset(&saddr.sin, 0, sizeof(saddr.sin));
287 1.1 gson saddr.sin.sin_family = AF_INET;
288 1.1 gson saddr.sin.sin_len = sizeof(saddr.sin);
289 1.1 gson saddr.sin.sin_port = htons(53);
290 1.1 gson saddr.sin.sin_addr.s_addr = INADDR_ANY;
291 1.1 gson } else {
292 1.1 gson static struct in6_addr loopback = IN6ADDR_LOOPBACK_INIT;
293 1.1 gson memset(&saddr.sin6, 0, sizeof(saddr.sin6));
294 1.1 gson saddr.sin6.sin6_family = AF_INET6;
295 1.1 gson saddr.sin6.sin6_len = sizeof(saddr.sin6);
296 1.1 gson saddr.sin6.sin6_port = htons(53);
297 1.1 gson saddr.sin6.sin6_addr = loopback;
298 1.1 gson }
299 1.1 gson
300 1.1 gson r = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
301 1.1 gson if (r < 0)
302 1.1 gson err(1, "setsockopt");
303 1.1 gson
304 1.1 gson r = bind(s,
305 1.1 gson (struct sockaddr *) &saddr,
306 1.1 gson protocol == '4' ? sizeof(struct sockaddr_in) :
307 1.1 gson sizeof(struct sockaddr_in6));
308 1.1 gson if (r < 0)
309 1.1 gson err(1, "bind");
310 1.1 gson
311 1.1 gson snprintf(pidfile_name, sizeof pidfile_name,
312 1.1 gson "dns_server_%c.pid", protocol);
313 1.1 gson f = fopen(pidfile_name, "w");
314 1.1 gson fprintf(f, "%d", getpid());
315 1.1 gson fclose(f);
316 1.3 christos #ifdef DEBUG
317 1.3 christos daemon2_detach(0, 1);
318 1.3 christos #else
319 1.2 gson daemon2_detach(0, 0);
320 1.3 christos #endif
321 1.1 gson
322 1.1 gson for (;;) {
323 1.1 gson unsigned char buf[512];
324 1.1 gson union sockaddr_either from;
325 1.1 gson ssize_t nrecv, nsent;
326 1.1 gson socklen_t fromlen =
327 1.1 gson protocol == '4' ? sizeof(struct sockaddr_in) :
328 1.1 gson sizeof(struct sockaddr_in6);
329 1.1 gson memset(buf, 0, sizeof buf);
330 1.1 gson nrecv = recvfrom(s, buf, sizeof buf, 0, &from.s, &fromlen);
331 1.1 gson if (nrecv < 0)
332 1.1 gson err(1, "recvfrom");
333 1.3 christos if (nrecv < 12) {
334 1.3 christos DPRINTF("Too short %zd\n", nrecv);
335 1.3 christos continue;
336 1.3 christos }
337 1.3 christos if ((buf[2] & 0x80) != 0) {
338 1.3 christos DPRINTF("Not a query 0x%x\n", buf[2]);
339 1.3 christos continue;
340 1.3 christos }
341 1.3 christos if (!(buf[4] == 0 && buf[5] == 1)) {
342 1.3 christos DPRINTF("QCOUNT is not 1 0x%x 0x%x\n", buf[4], buf[5]);
343 1.3 christos continue; /* QDCOUNT is not 1 */
344 1.3 christos }
345 1.1 gson
346 1.1 gson for (dp = data; dp->qname_size != 0; dp++) {
347 1.1 gson int qtype, qclass;
348 1.1 gson p = buf + 12; /* Point to QNAME */
349 1.1 gson int n = name_eq(p, (const unsigned char *) dp->qname);
350 1.3 christos if (n == 0) {
351 1.3 christos DPRINTF("no match name %s != %s\n",
352 1.3 christos name2str(p, buf1, sizeof(buf1)),
353 1.3 christos name2str(dp->qname, buf2, sizeof(buf2)));
354 1.1 gson continue; /* Name does not match */
355 1.3 christos }
356 1.3 christos DPRINTF("match name %s\n",
357 1.3 christos name2str(p, buf1, sizeof(buf1)));
358 1.1 gson p += n; /* Skip QNAME */
359 1.1 gson qtype = *p++ << 8;
360 1.1 gson qtype |= *p++;
361 1.3 christos if (qtype != dp->qtype) {
362 1.3 christos DPRINTF("no match name 0x%x != 0x%x\n",
363 1.3 christos qtype, dp->qtype);
364 1.1 gson continue;
365 1.3 christos }
366 1.3 christos DPRINTF("match type 0x%x\n", qtype);
367 1.1 gson qclass = *p++ << 8;
368 1.1 gson qclass |= *p++;
369 1.3 christos if (qclass != 1) { /* IN */
370 1.3 christos DPRINTF("no match class %d != 1\n", qclass);
371 1.1 gson continue;
372 1.3 christos }
373 1.3 christos DPRINTF("match class %d\n", qclass);
374 1.1 gson goto found;
375 1.1 gson }
376 1.1 gson continue;
377 1.1 gson found:
378 1.1 gson buf[2] |= 0x80; /* QR */
379 1.1 gson buf[3] |= 0x80; /* RA */
380 1.1 gson memset(buf + 6, 0, 6); /* Clear ANCOUNT, NSCOUNT, ARCOUNT */
381 1.1 gson buf[7] = 1; /* ANCOUNT */
382 1.1 gson memcpy(p, dp->qname, dp->qname_size);
383 1.1 gson p += dp->qname_size;
384 1.1 gson *p++ = dp->qtype >> 8;
385 1.1 gson *p++ = dp->qtype & 0xFF;
386 1.1 gson *p++ = 0;
387 1.1 gson *p++ = 1; /* IN */
388 1.1 gson memset(p, 0, 4); /* TTL = 0 */
389 1.1 gson p += 4;
390 1.1 gson *p++ = 0; /* RDLENGTH MSB */
391 1.1 gson *p++ = dp->answer_size; /* RDLENGTH LSB */
392 1.1 gson memcpy(p, dp->answer, dp->answer_size);
393 1.1 gson p += dp->answer_size;
394 1.1 gson nsent = sendto(s, buf, p - buf, 0, &from.s, fromlen);
395 1.3 christos DPRINTF("sent %zd\n", nsent);
396 1.1 gson if (nsent != p - buf)
397 1.1 gson warn("sendto");
398 1.1 gson }
399 1.1 gson }
400