xref: /openbsd/sbin/unwind/control.c (revision 09467b48)
1 /*	$OpenBSD: control.c,v 1.15 2019/12/18 09:18:27 florian Exp $	*/
2 
3 /*
4  * Copyright (c) 2003, 2004 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 USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 #include <sys/types.h>
19 #include <sys/queue.h>
20 #include <sys/stat.h>
21 #include <sys/socket.h>
22 #include <sys/time.h>
23 #include <sys/un.h>
24 
25 #include <net/route.h>
26 
27 #include <errno.h>
28 #include <event.h>
29 #include <imsg.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 
34 #include "log.h"
35 #include "unwind.h"
36 #include "control.h"
37 #include "frontend.h"
38 #include "resolver.h"
39 
40 #define	CONTROL_BACKLOG	5
41 
42 struct ctl_conn	*control_connbyfd(int);
43 struct ctl_conn	*control_connbypid(pid_t);
44 void		 control_close(int);
45 
46 int
47 control_init(char *path)
48 {
49 	struct sockaddr_un	 sun;
50 	int			 fd;
51 	mode_t			 old_umask;
52 
53 	if ((fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
54 	    0)) == -1) {
55 		log_warn("%s: socket", __func__);
56 		return (-1);
57 	}
58 
59 	memset(&sun, 0, sizeof(sun));
60 	sun.sun_family = AF_UNIX;
61 	strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
62 
63 	if (unlink(path) == -1)
64 		if (errno != ENOENT) {
65 			log_warn("%s: unlink %s", __func__, path);
66 			close(fd);
67 			return (-1);
68 		}
69 
70 	old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
71 	if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
72 		log_warn("%s: bind: %s", __func__, path);
73 		close(fd);
74 		umask(old_umask);
75 		return (-1);
76 	}
77 	umask(old_umask);
78 
79 	if (chmod(path,
80 	    S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) == -1) {
81 		log_warn("%s: chmod", __func__);
82 		close(fd);
83 		(void)unlink(path);
84 		return (-1);
85 	}
86 
87 	return (fd);
88 }
89 
90 int
91 control_listen(void)
92 {
93 	if (listen(control_state.fd, CONTROL_BACKLOG) == -1) {
94 		log_warn("%s: listen", __func__);
95 		return (-1);
96 	}
97 
98 	event_set(&control_state.ev, control_state.fd, EV_READ,
99 	    control_accept, NULL);
100 	event_add(&control_state.ev, NULL);
101 	evtimer_set(&control_state.evt, control_accept, NULL);
102 
103 	return (0);
104 }
105 
106 void
107 control_accept(int listenfd, short event, void *bula)
108 {
109 	int			 connfd;
110 	socklen_t		 len;
111 	struct sockaddr_un	 sun;
112 	struct ctl_conn		*c;
113 
114 	event_add(&control_state.ev, NULL);
115 	if ((event & EV_TIMEOUT))
116 		return;
117 
118 	len = sizeof(sun);
119 	if ((connfd = accept4(listenfd, (struct sockaddr *)&sun, &len,
120 	    SOCK_CLOEXEC | SOCK_NONBLOCK)) == -1) {
121 		/*
122 		 * Pause accept if we are out of file descriptors, or
123 		 * libevent will haunt us here too.
124 		 */
125 		if (errno == ENFILE || errno == EMFILE) {
126 			struct timeval evtpause = { 1, 0 };
127 
128 			event_del(&control_state.ev);
129 			evtimer_add(&control_state.evt, &evtpause);
130 		} else if (errno != EWOULDBLOCK && errno != EINTR &&
131 		    errno != ECONNABORTED)
132 			log_warn("%s: accept4", __func__);
133 		return;
134 	}
135 
136 	if ((c = calloc(1, sizeof(struct ctl_conn))) == NULL) {
137 		log_warn("%s: calloc", __func__);
138 		close(connfd);
139 		return;
140 	}
141 
142 	imsg_init(&c->iev.ibuf, connfd);
143 	c->iev.handler = control_dispatch_imsg;
144 	c->iev.events = EV_READ;
145 	event_set(&c->iev.ev, c->iev.ibuf.fd, c->iev.events, c->iev.handler,
146 	    &c->iev);
147 	event_add(&c->iev.ev, NULL);
148 
149 	TAILQ_INSERT_TAIL(&ctl_conns, c, entry);
150 }
151 
152 struct ctl_conn *
153 control_connbyfd(int fd)
154 {
155 	struct ctl_conn	*c;
156 
157 	TAILQ_FOREACH(c, &ctl_conns, entry) {
158 		if (c->iev.ibuf.fd == fd)
159 			break;
160 	}
161 
162 	return (c);
163 }
164 
165 struct ctl_conn *
166 control_connbypid(pid_t pid)
167 {
168 	struct ctl_conn	*c;
169 
170 	TAILQ_FOREACH(c, &ctl_conns, entry) {
171 		if (c->iev.ibuf.pid == pid)
172 			break;
173 	}
174 
175 	return (c);
176 }
177 
178 void
179 control_close(int fd)
180 {
181 	struct ctl_conn	*c;
182 
183 	if ((c = control_connbyfd(fd)) == NULL) {
184 		log_warnx("%s: fd %d: not found", __func__, fd);
185 		return;
186 	}
187 
188 	msgbuf_clear(&c->iev.ibuf.w);
189 	TAILQ_REMOVE(&ctl_conns, c, entry);
190 
191 	event_del(&c->iev.ev);
192 	close(c->iev.ibuf.fd);
193 
194 	/* Some file descriptors are available again. */
195 	if (evtimer_pending(&control_state.evt, NULL)) {
196 		evtimer_del(&control_state.evt);
197 		event_add(&control_state.ev, NULL);
198 	}
199 
200 	free(c);
201 }
202 
203 void
204 control_dispatch_imsg(int fd, short event, void *bula)
205 {
206 	struct ctl_conn	*c;
207 	struct imsg	 imsg;
208 	ssize_t		 n;
209 	int		 verbose;
210 	uid_t		 euid;
211 	gid_t		 egid;
212 
213 	if ((c = control_connbyfd(fd)) == NULL) {
214 		log_warnx("%s: fd %d: not found", __func__, fd);
215 		return;
216 	}
217 
218 	if (event & EV_READ) {
219 		if (((n = imsg_read(&c->iev.ibuf)) == -1 && errno != EAGAIN) ||
220 		    n == 0) {
221 			control_close(fd);
222 			return;
223 		}
224 	}
225 	if (event & EV_WRITE) {
226 		if (msgbuf_write(&c->iev.ibuf.w) <= 0 && errno != EAGAIN) {
227 			control_close(fd);
228 			return;
229 		}
230 	}
231 
232 	if (getpeereid(fd, &euid, &egid) == -1) {
233 		control_close(fd);
234 		return;
235 	}
236 
237 	for (;;) {
238 		if ((n = imsg_get(&c->iev.ibuf, &imsg)) == -1) {
239 			control_close(fd);
240 			return;
241 		}
242 		if (n == 0)
243 			break;
244 
245 		switch (imsg.hdr.type) {
246 		case IMSG_CTL_LOG_VERBOSE:
247 		case IMSG_CTL_RELOAD:
248 			if (euid != 0) {
249 				imsg_free(&imsg);
250 				control_close(fd);
251 				return;
252 			}
253 			break;
254 		default:
255 			break;
256 		}
257 
258 		switch (imsg.hdr.type) {
259 		case IMSG_CTL_RELOAD:
260 			frontend_imsg_compose_main(imsg.hdr.type, 0, NULL, 0);
261 			break;
262 		case IMSG_CTL_LOG_VERBOSE:
263 			if (IMSG_DATA_SIZE(imsg) != sizeof(verbose))
264 				break;
265 
266 			/* Forward to all other processes. */
267 			frontend_imsg_compose_main(imsg.hdr.type, imsg.hdr.pid,
268 			    imsg.data, IMSG_DATA_SIZE(imsg));
269 			frontend_imsg_compose_resolver(imsg.hdr.type,
270 			    imsg.hdr.pid, imsg.data, IMSG_DATA_SIZE(imsg));
271 
272 			memcpy(&verbose, imsg.data, sizeof(verbose));
273 			log_setverbose(verbose);
274 			break;
275 		case IMSG_CTL_STATUS:
276 		case IMSG_CTL_AUTOCONF:
277 		case IMSG_CTL_MEM:
278 			if (IMSG_DATA_SIZE(imsg) != 0)
279 				break;
280 			frontend_imsg_compose_resolver(imsg.hdr.type, 0, NULL,
281 			    0);
282 			break;
283 		default:
284 			log_debug("%s: error handling imsg %d", __func__,
285 			    imsg.hdr.type);
286 			break;
287 		}
288 		imsg_free(&imsg);
289 	}
290 
291 	imsg_event_add(&c->iev);
292 }
293 
294 int
295 control_imsg_relay(struct imsg *imsg)
296 {
297 	struct ctl_conn	*c;
298 
299 	if ((c = control_connbypid(imsg->hdr.pid)) == NULL)
300 		return (0);
301 
302 	return (imsg_compose_event(&c->iev, imsg->hdr.type, 0, imsg->hdr.pid,
303 	    -1, imsg->data, IMSG_DATA_SIZE(*imsg)));
304 }
305