h_dns_server.c revision 1.2 1 1.2 gson /* $NetBSD: h_dns_server.c,v 1.2 2014/01/06 16:42:57 gson 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.2 gson __RCSID("$NetBSD: h_dns_server.c,v 1.2 2014/01/06 16:42:57 gson 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.1 gson /* A DNS question and its corresponding answer */
62 1.1 gson
63 1.1 gson struct dns_data {
64 1.1 gson size_t qname_size;
65 1.1 gson const char *qname; /* Wire-encode question name */
66 1.1 gson int qtype;
67 1.1 gson size_t answer_size;
68 1.1 gson const char *answer; /* One wire-encoded answer RDATA */
69 1.1 gson };
70 1.1 gson
71 1.1 gson /* Convert C string constant to length + data pair */
72 1.1 gson #define STR_DATA(s) sizeof(s) - 1, s
73 1.1 gson
74 1.1 gson /* Canned DNS queestion-answer pairs */
75 1.1 gson struct dns_data data[] = {
76 1.1 gson /* Forward mappings */
77 1.1 gson /* localhost IN A -> 127.0.0.1 */
78 1.1 gson { STR_DATA("\011localhost\000"), 1,
79 1.1 gson STR_DATA("\177\000\000\001") },
80 1.1 gson /* localhost IN AAAA -> ::1 */
81 1.1 gson { STR_DATA("\011localhost\000"), 28,
82 1.1 gson STR_DATA("\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\001") },
83 1.1 gson /* sixthavenue.astron.com IN A -> 38.117.134.16 */
84 1.1 gson { STR_DATA("\013sixthavenue\006astron\003com\000"), 1,
85 1.1 gson STR_DATA("\046\165\206\020") },
86 1.1 gson /* sixthavenue.astron.com IN AAAA -> 2620:106:3003:1f00:3e4a:92ff:fef4:e180 */
87 1.1 gson { STR_DATA("\013sixthavenue\006astron\003com\000"), 28,
88 1.1 gson STR_DATA("\x26\x20\x01\x06\x30\x03\x1f\x00\x3e\x4a\x92\xff\xfe\xf4\xe1\x80") },
89 1.1 gson /* Reverse mappings */
90 1.1 gson { STR_DATA("\0011\0010\0010\003127\007in-addr\004arpa\000"), 12,
91 1.1 gson STR_DATA("\011localhost\000") },
92 1.1 gson { STR_DATA("\0011\0010\0010\0010\0010\0010\0010\0010"
93 1.1 gson "\0010\0010\0010\0010\0010\0010\0010\0010"
94 1.1 gson "\0010\0010\0010\0010\0010\0010\0010\0010"
95 1.1 gson "\0010\0010\0010\0010\0010\0010\0010\0010"
96 1.1 gson "\003ip6\004arpa\000"), 12,
97 1.1 gson STR_DATA("\011localhost\000") },
98 1.1 gson { STR_DATA("\00216\003134\003117\00238"
99 1.1 gson "\007in-addr\004arpa\000"), 12,
100 1.1 gson STR_DATA("\013sixthavenue\006astron\003com\000") },
101 1.1 gson { STR_DATA("\0010\0018\0011\001e\0014\001f\001e\001f"
102 1.1 gson "\001f\001f\0012\0019\001a\0014\001e\0013"
103 1.1 gson "\0010\0010\001f\0011\0013\0010\0010\0013"
104 1.1 gson "\0016\0010\0011\0010\0010\0012\0016\0012"
105 1.1 gson "\003ip6\004arpa\000"), 12,
106 1.1 gson STR_DATA("\013sixthavenue\006astron\003com\000") },
107 1.1 gson /* End marker */
108 1.1 gson { STR_DATA(""), 0, STR_DATA("") }
109 1.1 gson };
110 1.1 gson
111 1.1 gson /*
112 1.1 gson * Compare two DNS names for equality. If equal, return their
113 1.1 gson * length, and if not, return zero. Does not handle compression.
114 1.1 gson */
115 1.1 gson static int
116 1.1 gson name_eq(const unsigned char *a, const unsigned char *b) {
117 1.1 gson const unsigned char *a_save = a;
118 1.1 gson for (;;) {
119 1.1 gson int i;
120 1.1 gson int lena = *a++;
121 1.1 gson int lenb = *b++;
122 1.1 gson if (lena != lenb)
123 1.1 gson return 0;
124 1.1 gson if (lena == 0)
125 1.1 gson return a - a_save;
126 1.1 gson for (i = 0; i < lena; i++)
127 1.1 gson if (tolower(a[i]) != tolower(b[i]))
128 1.1 gson return 0;
129 1.1 gson a += lena;
130 1.1 gson b += lena;
131 1.1 gson }
132 1.1 gson }
133 1.1 gson
134 1.1 gson /* XXX the daemon2_* functions should be in a library */
135 1.1 gson
136 1.2 gson int __daemon2_detach_pipe[2];
137 1.1 gson
138 1.1 gson static int
139 1.1 gson daemon2_fork(void)
140 1.1 gson {
141 1.1 gson int r;
142 1.1 gson int fd;
143 1.1 gson int i;
144 1.1 gson
145 1.1 gson /*
146 1.1 gson * Set up the pipe, making sure the write end does not
147 1.1 gson * get allocated one of the file descriptors that will
148 1.2 gson * be closed in daemon2_detach().
149 1.1 gson */
150 1.1 gson for (i = 0; i < 3; i++) {
151 1.2 gson r = pipe(__daemon2_detach_pipe);
152 1.1 gson if (r < 0)
153 1.1 gson return -1;
154 1.2 gson if (__daemon2_detach_pipe[1] <= STDERR_FILENO &&
155 1.1 gson (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
156 1.2 gson (void)dup2(fd, __daemon2_detach_pipe[0]);
157 1.2 gson (void)dup2(fd, __daemon2_detach_pipe[1]);
158 1.1 gson if (fd > STDERR_FILENO)
159 1.1 gson (void)close(fd);
160 1.1 gson continue;
161 1.1 gson }
162 1.1 gson break;
163 1.1 gson }
164 1.1 gson
165 1.1 gson r = fork();
166 1.1 gson if (r < 0) {
167 1.1 gson return -1;
168 1.1 gson } else if (r == 0) {
169 1.1 gson /* child */
170 1.2 gson close(__daemon2_detach_pipe[0]);
171 1.1 gson return 0;
172 1.1 gson }
173 1.1 gson /* Parent */
174 1.1 gson
175 1.2 gson (void) close(__daemon2_detach_pipe[1]);
176 1.1 gson
177 1.1 gson for (;;) {
178 1.1 gson char dummy;
179 1.2 gson r = read(__daemon2_detach_pipe[0], &dummy, 1);
180 1.1 gson if (r < 0) {
181 1.1 gson if (errno == EINTR)
182 1.1 gson continue;
183 1.1 gson _exit(1);
184 1.1 gson } else if (r == 0) {
185 1.1 gson _exit(1);
186 1.1 gson } else { /* r > 0 */
187 1.1 gson _exit(0);
188 1.1 gson }
189 1.1 gson }
190 1.1 gson }
191 1.1 gson
192 1.1 gson static int
193 1.2 gson daemon2_detach(int nochdir, int noclose)
194 1.1 gson {
195 1.1 gson int r;
196 1.1 gson int fd;
197 1.1 gson
198 1.1 gson if (setsid() == -1)
199 1.1 gson return -1;
200 1.1 gson
201 1.1 gson if (!nochdir)
202 1.1 gson (void)chdir("/");
203 1.1 gson
204 1.1 gson if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
205 1.1 gson (void)dup2(fd, STDIN_FILENO);
206 1.1 gson (void)dup2(fd, STDOUT_FILENO);
207 1.1 gson (void)dup2(fd, STDERR_FILENO);
208 1.1 gson if (fd > STDERR_FILENO)
209 1.1 gson (void)close(fd);
210 1.1 gson }
211 1.1 gson
212 1.1 gson while (1) {
213 1.2 gson r = write(__daemon2_detach_pipe[1], "", 1);
214 1.1 gson if (r < 0) {
215 1.1 gson if (errno == EINTR)
216 1.1 gson continue;
217 1.1 gson /* May get "broken pipe" here if parent is killed */
218 1.1 gson return -1;
219 1.1 gson } else if (r == 0) {
220 1.1 gson /* Should not happen */
221 1.1 gson return -1;
222 1.1 gson } else {
223 1.1 gson break;
224 1.1 gson }
225 1.1 gson }
226 1.1 gson
227 1.2 gson (void) close(__daemon2_detach_pipe[1]);
228 1.1 gson
229 1.1 gson return 0;
230 1.1 gson }
231 1.1 gson
232 1.1 gson int main(int argc, char **argv) {
233 1.1 gson int s, r, protocol;
234 1.1 gson union sockaddr_either saddr;
235 1.1 gson struct dns_data *dp;
236 1.1 gson unsigned char *p;
237 1.1 gson char pidfile_name[40];
238 1.1 gson FILE *f;
239 1.1 gson int one = 1;
240 1.1 gson
241 1.1 gson daemon2_fork();
242 1.1 gson
243 1.1 gson if (argc < 2 || ((protocol = argv[1][0]) != '4' && protocol != '6'))
244 1.1 gson errx(1, "usage: dns_server 4 | 6");
245 1.1 gson s = socket(protocol == '4' ? PF_INET : PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
246 1.1 gson if (s < 0)
247 1.1 gson err(1, "socket");
248 1.1 gson if (protocol == '4') {
249 1.1 gson memset(&saddr.sin, 0, sizeof(saddr.sin));
250 1.1 gson saddr.sin.sin_family = AF_INET;
251 1.1 gson saddr.sin.sin_len = sizeof(saddr.sin);
252 1.1 gson saddr.sin.sin_port = htons(53);
253 1.1 gson saddr.sin.sin_addr.s_addr = INADDR_ANY;
254 1.1 gson } else {
255 1.1 gson static struct in6_addr loopback = IN6ADDR_LOOPBACK_INIT;
256 1.1 gson memset(&saddr.sin6, 0, sizeof(saddr.sin6));
257 1.1 gson saddr.sin6.sin6_family = AF_INET6;
258 1.1 gson saddr.sin6.sin6_len = sizeof(saddr.sin6);
259 1.1 gson saddr.sin6.sin6_port = htons(53);
260 1.1 gson saddr.sin6.sin6_addr = loopback;
261 1.1 gson }
262 1.1 gson
263 1.1 gson r = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
264 1.1 gson if (r < 0)
265 1.1 gson err(1, "setsockopt");
266 1.1 gson
267 1.1 gson r = bind(s,
268 1.1 gson (struct sockaddr *) &saddr,
269 1.1 gson protocol == '4' ? sizeof(struct sockaddr_in) :
270 1.1 gson sizeof(struct sockaddr_in6));
271 1.1 gson if (r < 0)
272 1.1 gson err(1, "bind");
273 1.1 gson
274 1.1 gson snprintf(pidfile_name, sizeof pidfile_name,
275 1.1 gson "dns_server_%c.pid", protocol);
276 1.1 gson f = fopen(pidfile_name, "w");
277 1.1 gson fprintf(f, "%d", getpid());
278 1.1 gson fclose(f);
279 1.2 gson daemon2_detach(0, 0);
280 1.1 gson
281 1.1 gson for (;;) {
282 1.1 gson unsigned char buf[512];
283 1.1 gson union sockaddr_either from;
284 1.1 gson ssize_t nrecv, nsent;
285 1.1 gson socklen_t fromlen =
286 1.1 gson protocol == '4' ? sizeof(struct sockaddr_in) :
287 1.1 gson sizeof(struct sockaddr_in6);
288 1.1 gson memset(buf, 0, sizeof buf);
289 1.1 gson nrecv = recvfrom(s, buf, sizeof buf, 0, &from.s, &fromlen);
290 1.1 gson if (nrecv < 0)
291 1.1 gson err(1, "recvfrom");
292 1.1 gson if (nrecv < 12)
293 1.1 gson continue; /* Too short */
294 1.1 gson if ((buf[2] & 0x80) != 0)
295 1.1 gson continue; /* Not a query */
296 1.1 gson if (!(buf[4] == 0 && buf[5] == 1))
297 1.1 gson continue; /* QDCOUNT is not 1 */
298 1.1 gson
299 1.1 gson for (dp = data; dp->qname_size != 0; dp++) {
300 1.1 gson int qtype, qclass;
301 1.1 gson p = buf + 12; /* Point to QNAME */
302 1.1 gson int n = name_eq(p, (const unsigned char *) dp->qname);
303 1.1 gson if (n == 0)
304 1.1 gson continue; /* Name does not match */
305 1.1 gson p += n; /* Skip QNAME */
306 1.1 gson qtype = *p++ << 8;
307 1.1 gson qtype |= *p++;
308 1.1 gson if (qtype != dp->qtype)
309 1.1 gson continue;
310 1.1 gson qclass = *p++ << 8;
311 1.1 gson qclass |= *p++;
312 1.1 gson if (qclass != 1) /* IN */
313 1.1 gson continue;
314 1.1 gson goto found;
315 1.1 gson }
316 1.1 gson continue;
317 1.1 gson found:
318 1.1 gson buf[2] |= 0x80; /* QR */
319 1.1 gson buf[3] |= 0x80; /* RA */
320 1.1 gson memset(buf + 6, 0, 6); /* Clear ANCOUNT, NSCOUNT, ARCOUNT */
321 1.1 gson buf[7] = 1; /* ANCOUNT */
322 1.1 gson memcpy(p, dp->qname, dp->qname_size);
323 1.1 gson p += dp->qname_size;
324 1.1 gson *p++ = dp->qtype >> 8;
325 1.1 gson *p++ = dp->qtype & 0xFF;
326 1.1 gson *p++ = 0;
327 1.1 gson *p++ = 1; /* IN */
328 1.1 gson memset(p, 0, 4); /* TTL = 0 */
329 1.1 gson p += 4;
330 1.1 gson *p++ = 0; /* RDLENGTH MSB */
331 1.1 gson *p++ = dp->answer_size; /* RDLENGTH LSB */
332 1.1 gson memcpy(p, dp->answer, dp->answer_size);
333 1.1 gson p += dp->answer_size;
334 1.1 gson nsent = sendto(s, buf, p - buf, 0, &from.s, fromlen);
335 1.1 gson if (nsent != p - buf)
336 1.1 gson warn("sendto");
337 1.1 gson }
338 1.1 gson }
339