xref: /openbsd-src/usr.sbin/ypldap/ypldap_dns.c (revision 1ad61ae0a79a724d2d3ec69e69c8e1d1ff6b53a0)
1 /*	$OpenBSD: ypldap_dns.c,v 1.16 2023/07/18 13:06:33 claudio 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 #include "log.h"
42 
43 volatile sig_atomic_t	 quit_dns = 0;
44 struct imsgev		*iev_dns;
45 
46 void	dns_dispatch_imsg(int, short, void *);
47 void	dns_sig_handler(int, short, void *);
48 void	dns_shutdown(void);
49 int	host_dns(const char *, struct ypldap_addr_list *);
50 
51 void
52 dns_sig_handler(int sig, short event, void *p)
53 {
54 	switch (sig) {
55 	case SIGINT:
56 	case SIGTERM:
57 		dns_shutdown();
58 		break;
59 	case SIGHUP:
60 		/* ignore */
61 		break;
62 	default:
63 		fatalx("unexpected signal");
64 	}
65 }
66 
67 void
68 dns_shutdown(void)
69 {
70 	log_info("dns engine exiting");
71 	_exit(0);
72 }
73 
74 pid_t
75 ypldap_dns(int pipe_ntp[2], struct passwd *pw)
76 {
77 	pid_t		 pid;
78 	struct event	 ev_sigint;
79 	struct event	 ev_sigterm;
80 	struct event	 ev_sighup;
81 	struct env	 env;
82 
83 	switch (pid = fork()) {
84 	case -1:
85 		fatal("cannot fork");
86 		break;
87 	case 0:
88 		break;
89 	default:
90 		return (pid);
91 	}
92 
93 	setproctitle("dns engine");
94 	close(pipe_ntp[0]);
95 
96 	if (setgroups(1, &pw->pw_gid) ||
97 	    setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
98 	    setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
99 		fatal("can't drop privileges");
100 	endservent();
101 
102 	if (pledge("stdio dns", NULL) == -1)
103 		fatal("pledge");
104 
105 	event_init();
106 	signal_set(&ev_sigint, SIGINT, dns_sig_handler, NULL);
107 	signal_set(&ev_sigterm, SIGTERM, dns_sig_handler, NULL);
108 	signal_set(&ev_sighup, SIGHUP, dns_sig_handler, NULL);
109 	signal_add(&ev_sigint, NULL);
110 	signal_add(&ev_sigterm, NULL);
111 	signal_add(&ev_sighup, NULL);
112 
113 	if ((env.sc_iev = calloc(1, sizeof(*env.sc_iev))) == NULL)
114 		fatal(NULL);
115 
116 	env.sc_iev->events = EV_READ;
117 	env.sc_iev->data = &env;
118 	imsg_init(&env.sc_iev->ibuf, pipe_ntp[1]);
119 	env.sc_iev->handler = dns_dispatch_imsg;
120 	event_set(&env.sc_iev->ev, env.sc_iev->ibuf.fd, env.sc_iev->events,
121 	    env.sc_iev->handler, &env);
122 	event_add(&env.sc_iev->ev, NULL);
123 
124 	event_dispatch();
125 	dns_shutdown();
126 
127 	return (0);
128 }
129 
130 void
131 dns_dispatch_imsg(int fd, short events, void *p)
132 {
133 	struct imsg		 imsg;
134 	int			 n, cnt;
135 	char			*name;
136 	struct ypldap_addr_list	 hn = TAILQ_HEAD_INITIALIZER(hn);
137 	struct ypldap_addr	*h;
138 	struct ibuf		*buf;
139 	struct env		*env = p;
140 	struct imsgev		*iev = env->sc_iev;
141 	struct imsgbuf		*ibuf = &iev->ibuf;
142 	int			 shut = 0;
143 	size_t			 len;
144 
145 	if ((events & (EV_READ | EV_WRITE)) == 0)
146 		fatalx("unknown event");
147 
148 	if (events & EV_READ) {
149 		if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
150 			fatal("imsg_read error");
151 		if (n == 0)
152 			shut = 1;
153 	}
154 	if (events & EV_WRITE) {
155 		if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
156 			fatal("msgbuf_write");
157 		if (n == 0)
158 			shut = 1;
159 		goto done;
160 	}
161 
162 	for (;;) {
163 		if ((n = imsg_get(ibuf, &imsg)) == -1)
164 			fatal("client_dispatch_imsg: imsg_get error");
165 		if (n == 0)
166 			break;
167 
168 		switch (imsg.hdr.type) {
169 		case IMSG_HOST_DNS:
170 			name = imsg.data;
171 			if (imsg.hdr.len < 1 + IMSG_HEADER_SIZE)
172 				fatalx("invalid IMSG_HOST_DNS received");
173 			len = imsg.hdr.len - 1 - IMSG_HEADER_SIZE;
174 			if (name[len] != '\0' ||
175 			    strlen(name) != len)
176 				fatalx("invalid IMSG_HOST_DNS received");
177 			if ((cnt = host_dns(name, &hn)) == -1)
178 				break;
179 			buf = imsg_create(ibuf, IMSG_HOST_DNS,
180 			    imsg.hdr.peerid, 0,
181 			    cnt * sizeof(struct sockaddr_storage));
182 			if (buf == NULL)
183 				break;
184 			if (cnt > 0) {
185 				while (!TAILQ_EMPTY(&hn)) {
186 					h = TAILQ_FIRST(&hn);
187 					TAILQ_REMOVE(&hn, h, next);
188 					imsg_add(buf, &h->ss, sizeof(h->ss));
189 					free(h);
190 				}
191 			}
192 
193 			imsg_close(ibuf, buf);
194 			break;
195 		default:
196 			break;
197 		}
198 		imsg_free(&imsg);
199 	}
200 
201 done:
202 	if (!shut)
203 		imsg_event_add(iev);
204 	else {
205 		/* this pipe is dead, so remove the event handler */
206 		event_del(&iev->ev);
207 		event_loopexit(NULL);
208 	}
209 }
210 
211 int
212 host_dns(const char *s, struct ypldap_addr_list *hn)
213 {
214 	struct addrinfo		 hints, *res0, *res;
215 	int			 error, cnt = 0;
216 	struct sockaddr_in	*sa_in;
217 	struct sockaddr_in6	*sa_in6;
218 	struct ypldap_addr	*h;
219 
220 	memset(&hints, 0, sizeof(hints));
221 	hints.ai_family = PF_UNSPEC;
222 	hints.ai_socktype = SOCK_DGRAM; /* DUMMY */
223 	error = getaddrinfo(s, NULL, &hints, &res0);
224 	if (error != 0) {
225 		log_warnx("could not resolve \"%s\": %s", s,
226 		    gai_strerror(error));
227 		if (error == EAI_AGAIN || error == EAI_NODATA ||
228 		    error == EAI_NONAME)
229 			return (0);
230 		return (-1);
231 	}
232 
233 	for (res = res0; res && cnt < MAX_SERVERS_DNS; res = res->ai_next) {
234 		if (res->ai_family != AF_INET &&
235 		    res->ai_family != AF_INET6)
236 			continue;
237 		if ((h = calloc(1, sizeof(struct ypldap_addr))) == NULL)
238 			fatal(NULL);
239 		h->ss.ss_family = res->ai_family;
240 		if (res->ai_family == AF_INET) {
241 			sa_in = (struct sockaddr_in *)&h->ss;
242 			sa_in->sin_len = sizeof(struct sockaddr_in);
243 			sa_in->sin_addr.s_addr = ((struct sockaddr_in *)
244 			    res->ai_addr)->sin_addr.s_addr;
245 		} else {
246 			sa_in6 = (struct sockaddr_in6 *)&h->ss;
247 			sa_in6->sin6_len = sizeof(struct sockaddr_in6);
248 			memcpy(&sa_in6->sin6_addr, &((struct sockaddr_in6 *)
249 			    res->ai_addr)->sin6_addr, sizeof(struct in6_addr));
250 		}
251 
252 		TAILQ_INSERT_HEAD(hn, h, next);
253 		cnt++;
254 	}
255 	freeaddrinfo(res0);
256 	return (cnt);
257 }
258