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