xref: /openbsd/sbin/dhcpleased/control.c (revision 097a140d)
1 /*	$OpenBSD: control.c,v 1.2 2021/03/02 04:10:07 jsg 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/uio.h>
23 #include <sys/un.h>
24 
25 #include <net/if.h>
26 #include <netinet/in.h>
27 #include <netinet/if_ether.h>
28 
29 #include <errno.h>
30 #include <event.h>
31 #include <imsg.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 
36 #include "log.h"
37 #include "dhcpleased.h"
38 #include "control.h"
39 #include "frontend.h"
40 
41 #define	CONTROL_BACKLOG	5
42 
43 struct {
44 	struct event	ev;
45 	struct event	evt;
46 	int		fd;
47 } control_state = {.fd = -1};
48 
49 struct ctl_conn {
50 	TAILQ_ENTRY(ctl_conn)	entry;
51 	struct imsgev		iev;
52 };
53 
54 struct ctl_conn	*control_connbyfd(int);
55 struct ctl_conn	*control_connbypid(pid_t);
56 void		 control_close(int);
57 
58 TAILQ_HEAD(ctl_conns, ctl_conn) ctl_conns = TAILQ_HEAD_INITIALIZER(ctl_conns);
59 
60 int
61 control_init(char *path)
62 {
63 	struct sockaddr_un	 sun;
64 	int			 fd;
65 	mode_t			 old_umask;
66 
67 	if ((fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
68 	    0)) == -1) {
69 		log_warn("%s: socket", __func__);
70 		return (-1);
71 	}
72 
73 	memset(&sun, 0, sizeof(sun));
74 	sun.sun_family = AF_UNIX;
75 	strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
76 
77 	if (unlink(path) == -1)
78 		if (errno != ENOENT) {
79 			log_warn("%s: unlink %s", __func__, path);
80 			close(fd);
81 			return (-1);
82 		}
83 
84 	old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
85 	if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
86 		log_warn("%s: bind: %s", __func__, path);
87 		close(fd);
88 		umask(old_umask);
89 		return (-1);
90 	}
91 	umask(old_umask);
92 
93 	if (chmod(path, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1) {
94 		log_warn("%s: chmod", __func__);
95 		close(fd);
96 		(void)unlink(path);
97 		return (-1);
98 	}
99 
100 	return (fd);
101 }
102 
103 int
104 control_listen(int fd)
105 {
106 	if (control_state.fd != -1)
107 		fatalx("%s: received unexpected controlsock", __func__);
108 
109 	control_state.fd = fd;
110 	if (listen(control_state.fd, CONTROL_BACKLOG) == -1) {
111 		log_warn("%s: listen", __func__);
112 		return (-1);
113 	}
114 
115 	event_set(&control_state.ev, control_state.fd, EV_READ,
116 	    control_accept, NULL);
117 	event_add(&control_state.ev, NULL);
118 	evtimer_set(&control_state.evt, control_accept, NULL);
119 
120 	return (0);
121 }
122 
123 void
124 control_accept(int listenfd, short event, void *bula)
125 {
126 	int			 connfd;
127 	socklen_t		 len;
128 	struct sockaddr_un	 sun;
129 	struct ctl_conn		*c;
130 
131 	event_add(&control_state.ev, NULL);
132 	if ((event & EV_TIMEOUT))
133 		return;
134 
135 	len = sizeof(sun);
136 	if ((connfd = accept4(listenfd, (struct sockaddr *)&sun, &len,
137 	    SOCK_CLOEXEC | SOCK_NONBLOCK)) == -1) {
138 		/*
139 		 * Pause accept if we are out of file descriptors, or
140 		 * libevent will haunt us here too.
141 		 */
142 		if (errno == ENFILE || errno == EMFILE) {
143 			struct timeval evtpause = { 1, 0 };
144 
145 			event_del(&control_state.ev);
146 			evtimer_add(&control_state.evt, &evtpause);
147 		} else if (errno != EWOULDBLOCK && errno != EINTR &&
148 		    errno != ECONNABORTED)
149 			log_warn("%s: accept4", __func__);
150 		return;
151 	}
152 
153 	if ((c = calloc(1, sizeof(struct ctl_conn))) == NULL) {
154 		log_warn("%s: calloc", __func__);
155 		close(connfd);
156 		return;
157 	}
158 
159 	imsg_init(&c->iev.ibuf, connfd);
160 	c->iev.handler = control_dispatch_imsg;
161 	c->iev.events = EV_READ;
162 	event_set(&c->iev.ev, c->iev.ibuf.fd, c->iev.events,
163 	    c->iev.handler, &c->iev);
164 	event_add(&c->iev.ev, NULL);
165 
166 	TAILQ_INSERT_TAIL(&ctl_conns, c, entry);
167 }
168 
169 struct ctl_conn *
170 control_connbyfd(int fd)
171 {
172 	struct ctl_conn	*c;
173 
174 	TAILQ_FOREACH(c, &ctl_conns, entry) {
175 		if (c->iev.ibuf.fd == fd)
176 			break;
177 	}
178 
179 	return (c);
180 }
181 
182 struct ctl_conn *
183 control_connbypid(pid_t pid)
184 {
185 	struct ctl_conn	*c;
186 
187 	TAILQ_FOREACH(c, &ctl_conns, entry) {
188 		if (c->iev.ibuf.pid == pid)
189 			break;
190 	}
191 
192 	return (c);
193 }
194 
195 void
196 control_close(int fd)
197 {
198 	struct ctl_conn	*c;
199 
200 	if ((c = control_connbyfd(fd)) == NULL) {
201 		log_warnx("%s: fd %d: not found", __func__, fd);
202 		return;
203 	}
204 
205 	msgbuf_clear(&c->iev.ibuf.w);
206 	TAILQ_REMOVE(&ctl_conns, c, entry);
207 
208 	event_del(&c->iev.ev);
209 	close(c->iev.ibuf.fd);
210 
211 	/* Some file descriptors are available again. */
212 	if (evtimer_pending(&control_state.evt, NULL)) {
213 		evtimer_del(&control_state.evt);
214 		event_add(&control_state.ev, NULL);
215 	}
216 
217 	free(c);
218 }
219 
220 void
221 control_dispatch_imsg(int fd, short event, void *bula)
222 {
223 	struct ctl_conn	*c;
224 	struct imsg	 imsg;
225 	ssize_t		 n;
226 	int		 verbose;
227 
228 	if ((c = control_connbyfd(fd)) == NULL) {
229 		log_warnx("%s: fd %d: not found", __func__, fd);
230 		return;
231 	}
232 
233 	if (event & EV_READ) {
234 		if (((n = imsg_read(&c->iev.ibuf)) == -1 && errno != EAGAIN) ||
235 		    n == 0) {
236 			control_close(fd);
237 			return;
238 		}
239 	}
240 	if (event & EV_WRITE) {
241 		if (msgbuf_write(&c->iev.ibuf.w) <= 0 && errno != EAGAIN) {
242 			control_close(fd);
243 			return;
244 		}
245 	}
246 
247 	for (;;) {
248 		if ((n = imsg_get(&c->iev.ibuf, &imsg)) == -1) {
249 			control_close(fd);
250 			return;
251 		}
252 		if (n == 0)
253 			break;
254 
255 		switch (imsg.hdr.type) {
256 		case IMSG_CTL_LOG_VERBOSE:
257 			if (IMSG_DATA_SIZE(imsg) != sizeof(verbose))
258 				break;
259 			c->iev.ibuf.pid = imsg.hdr.pid;
260 			/* Forward to all other processes. */
261 			frontend_imsg_compose_main(imsg.hdr.type, imsg.hdr.pid,
262 			    imsg.data, IMSG_DATA_SIZE(imsg));
263 			frontend_imsg_compose_engine(imsg.hdr.type, 0,
264 			    imsg.hdr.pid, imsg.data, IMSG_DATA_SIZE(imsg));
265 
266 			memcpy(&verbose, imsg.data, sizeof(verbose));
267 			log_setverbose(verbose);
268 			break;
269 		case IMSG_CTL_SHOW_INTERFACE_INFO:
270 			if (IMSG_DATA_SIZE(imsg) != sizeof(uint32_t))
271 				break;
272 			c->iev.ibuf.pid = imsg.hdr.pid;
273 			frontend_imsg_compose_engine(imsg.hdr.type, 0,
274 			    imsg.hdr.pid, imsg.data, IMSG_DATA_SIZE(imsg));
275 			break;
276 		case IMSG_CTL_SEND_REQUEST:
277 			if (IMSG_DATA_SIZE(imsg) != sizeof(uint32_t))
278 				break;
279 			c->iev.ibuf.pid = imsg.hdr.pid;
280 			frontend_imsg_compose_engine(imsg.hdr.type, 0,
281 			    imsg.hdr.pid, imsg.data, IMSG_DATA_SIZE(imsg));
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