1 /* $OpenBSD: ypldap_dns.c,v 1.14 2021/10/09 18:43:50 deraadt Exp $ */ 2 3 /* 4 * Copyright (c) 2003-2008 Henning Brauer <henning@openbsd.org> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER 15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING 16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <sys/types.h> 20 #include <sys/socket.h> 21 #include <sys/stat.h> 22 #include <sys/time.h> 23 #include <sys/tree.h> 24 #include <sys/queue.h> 25 26 #include <netinet/in.h> 27 28 #include <netdb.h> 29 #include <pwd.h> 30 #include <errno.h> 31 #include <event.h> 32 #include <resolv.h> 33 #include <poll.h> 34 #include <signal.h> 35 #include <stdlib.h> 36 #include <string.h> 37 #include <unistd.h> 38 #include <limits.h> 39 40 #include "ypldap.h" 41 42 volatile sig_atomic_t quit_dns = 0; 43 struct imsgev *iev_dns; 44 45 void dns_dispatch_imsg(int, short, void *); 46 void dns_sig_handler(int, short, void *); 47 void dns_shutdown(void); 48 int host_dns(const char *, struct ypldap_addr_list *); 49 50 void 51 dns_sig_handler(int sig, short event, void *p) 52 { 53 switch (sig) { 54 case SIGINT: 55 case SIGTERM: 56 dns_shutdown(); 57 break; 58 default: 59 fatalx("unexpected signal"); 60 } 61 } 62 63 void 64 dns_shutdown(void) 65 { 66 log_info("dns engine exiting"); 67 _exit(0); 68 } 69 70 pid_t 71 ypldap_dns(int pipe_ntp[2], struct passwd *pw) 72 { 73 pid_t pid; 74 struct event ev_sigint; 75 struct event ev_sigterm; 76 struct event ev_sighup; 77 struct env env; 78 79 switch (pid = fork()) { 80 case -1: 81 fatal("cannot fork"); 82 break; 83 case 0: 84 break; 85 default: 86 return (pid); 87 } 88 89 setproctitle("dns engine"); 90 close(pipe_ntp[0]); 91 92 if (setgroups(1, &pw->pw_gid) || 93 setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) || 94 setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid)) 95 fatal("can't drop privileges"); 96 endservent(); 97 98 if (pledge("stdio dns", NULL) == -1) 99 fatal("pledge"); 100 101 event_init(); 102 signal_set(&ev_sigint, SIGINT, dns_sig_handler, NULL); 103 signal_set(&ev_sigterm, SIGTERM, dns_sig_handler, NULL); 104 signal_set(&ev_sighup, SIGHUP, dns_sig_handler, NULL); 105 signal_add(&ev_sigint, NULL); 106 signal_add(&ev_sigterm, NULL); 107 signal_add(&ev_sighup, NULL); 108 109 if ((env.sc_iev = calloc(1, sizeof(*env.sc_iev))) == NULL) 110 fatal(NULL); 111 112 env.sc_iev->events = EV_READ; 113 env.sc_iev->data = &env; 114 imsg_init(&env.sc_iev->ibuf, pipe_ntp[1]); 115 env.sc_iev->handler = dns_dispatch_imsg; 116 event_set(&env.sc_iev->ev, env.sc_iev->ibuf.fd, env.sc_iev->events, 117 env.sc_iev->handler, &env); 118 event_add(&env.sc_iev->ev, NULL); 119 120 event_dispatch(); 121 dns_shutdown(); 122 123 return (0); 124 } 125 126 void 127 dns_dispatch_imsg(int fd, short events, void *p) 128 { 129 struct imsg imsg; 130 int n, cnt; 131 char *name; 132 struct ypldap_addr_list hn = TAILQ_HEAD_INITIALIZER(hn); 133 struct ypldap_addr *h; 134 struct ibuf *buf; 135 struct env *env = p; 136 struct imsgev *iev = env->sc_iev; 137 struct imsgbuf *ibuf = &iev->ibuf; 138 int shut = 0; 139 size_t len; 140 141 if ((events & (EV_READ | EV_WRITE)) == 0) 142 fatalx("unknown event"); 143 144 if (events & EV_READ) { 145 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN) 146 fatal("imsg_read error"); 147 if (n == 0) 148 shut = 1; 149 } 150 if (events & EV_WRITE) { 151 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN) 152 fatal("msgbuf_write"); 153 if (n == 0) 154 shut = 1; 155 goto done; 156 } 157 158 for (;;) { 159 if ((n = imsg_get(ibuf, &imsg)) == -1) 160 fatal("client_dispatch_imsg: imsg_get error"); 161 if (n == 0) 162 break; 163 164 switch (imsg.hdr.type) { 165 case IMSG_HOST_DNS: 166 name = imsg.data; 167 if (imsg.hdr.len < 1 + IMSG_HEADER_SIZE) 168 fatalx("invalid IMSG_HOST_DNS received"); 169 len = imsg.hdr.len - 1 - IMSG_HEADER_SIZE; 170 if (name[len] != '\0' || 171 strlen(name) != len) 172 fatalx("invalid IMSG_HOST_DNS received"); 173 if ((cnt = host_dns(name, &hn)) == -1) 174 break; 175 buf = imsg_create(ibuf, IMSG_HOST_DNS, 176 imsg.hdr.peerid, 0, 177 cnt * sizeof(struct sockaddr_storage)); 178 if (buf == NULL) 179 break; 180 if (cnt > 0) { 181 while (!TAILQ_EMPTY(&hn)) { 182 h = TAILQ_FIRST(&hn); 183 TAILQ_REMOVE(&hn, h, next); 184 imsg_add(buf, &h->ss, sizeof(h->ss)); 185 free(h); 186 } 187 } 188 189 imsg_close(ibuf, buf); 190 break; 191 default: 192 break; 193 } 194 imsg_free(&imsg); 195 } 196 197 done: 198 if (!shut) 199 imsg_event_add(iev); 200 else { 201 /* this pipe is dead, so remove the event handler */ 202 event_del(&iev->ev); 203 event_loopexit(NULL); 204 } 205 } 206 207 int 208 host_dns(const char *s, struct ypldap_addr_list *hn) 209 { 210 struct addrinfo hints, *res0, *res; 211 int error, cnt = 0; 212 struct sockaddr_in *sa_in; 213 struct sockaddr_in6 *sa_in6; 214 struct ypldap_addr *h; 215 216 memset(&hints, 0, sizeof(hints)); 217 hints.ai_family = PF_UNSPEC; 218 hints.ai_socktype = SOCK_DGRAM; /* DUMMY */ 219 error = getaddrinfo(s, NULL, &hints, &res0); 220 if (error == EAI_AGAIN || error == EAI_NODATA || error == EAI_NONAME) 221 return (0); 222 if (error) { 223 log_warnx("could not parse \"%s\": %s", s, 224 gai_strerror(error)); 225 return (-1); 226 } 227 228 for (res = res0; res && cnt < MAX_SERVERS_DNS; res = res->ai_next) { 229 if (res->ai_family != AF_INET && 230 res->ai_family != AF_INET6) 231 continue; 232 if ((h = calloc(1, sizeof(struct ypldap_addr))) == NULL) 233 fatal(NULL); 234 h->ss.ss_family = res->ai_family; 235 if (res->ai_family == AF_INET) { 236 sa_in = (struct sockaddr_in *)&h->ss; 237 sa_in->sin_len = sizeof(struct sockaddr_in); 238 sa_in->sin_addr.s_addr = ((struct sockaddr_in *) 239 res->ai_addr)->sin_addr.s_addr; 240 } else { 241 sa_in6 = (struct sockaddr_in6 *)&h->ss; 242 sa_in6->sin6_len = sizeof(struct sockaddr_in6); 243 memcpy(&sa_in6->sin6_addr, &((struct sockaddr_in6 *) 244 res->ai_addr)->sin6_addr, sizeof(struct in6_addr)); 245 } 246 247 TAILQ_INSERT_HEAD(hn, h, next); 248 cnt++; 249 } 250 freeaddrinfo(res0); 251 return (cnt); 252 } 253