xref: /openbsd-src/usr.sbin/radiusd/control.c (revision 882428cdbdd2944d8f59bc8621c131fd814fb6ee)
1 /*	$OpenBSD: control.c,v 1.7 2024/11/21 13:43:10 claudio 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 
19 #include <sys/types.h>
20 #include <sys/queue.h>
21 #include <sys/socket.h>
22 #include <sys/stat.h>
23 #include <sys/time.h>
24 #include <sys/un.h>
25 
26 #include <errno.h>
27 #include <event.h>
28 #include <imsg.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <unistd.h>
33 
34 #include "radiusd.h"
35 #include "radiusd_local.h"
36 #include "log.h"
37 #include "control.h"
38 
39 static TAILQ_HEAD(, ctl_conn) ctl_conns = TAILQ_HEAD_INITIALIZER(ctl_conns);
40 
41 #define	CONTROL_BACKLOG	5
42 static int	 idseq = 0;
43 
44 struct ctl_conn	*control_connbyfd(int);
45 struct ctl_conn	*control_connbyid(uint32_t);
46 void		 control_close(int);
47 void		 control_connfree(struct ctl_conn *);
48 void		 control_event_add(struct ctl_conn *);
49 
50 struct {
51 	struct event	ev;
52 	struct event	evt;
53 	int		fd;
54 } control_state;
55 
56 int
57 control_init(const char *path)
58 {
59 	struct sockaddr_un	 sun;
60 	int			 fd;
61 	mode_t			 old_umask;
62 
63 	if ((fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
64 	    0)) == -1) {
65 		log_warn("control_init: socket");
66 		return (-1);
67 	}
68 
69 	memset(&sun, 0, sizeof(sun));
70 	sun.sun_family = AF_UNIX;
71 	strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
72 
73 	if (unlink(path) == -1)
74 		if (errno != ENOENT) {
75 			log_warn("control_init: unlink %s", path);
76 			close(fd);
77 			return (-1);
78 		}
79 
80 	old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
81 	if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
82 		log_warn("control_init: bind: %s", path);
83 		close(fd);
84 		umask(old_umask);
85 		return (-1);
86 	}
87 	umask(old_umask);
88 
89 	if (chmod(path, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1) {
90 		log_warn("control_init: chmod");
91 		close(fd);
92 		(void)unlink(path);
93 		return (-1);
94 	}
95 
96 	control_state.fd = fd;
97 
98 	return (0);
99 }
100 
101 int
102 control_listen(void)
103 {
104 
105 	if (listen(control_state.fd, CONTROL_BACKLOG) == -1) {
106 		log_warn("control_listen: listen");
107 		return (-1);
108 	}
109 
110 	event_set(&control_state.ev, control_state.fd, EV_READ,
111 	    control_accept, NULL);
112 	event_add(&control_state.ev, NULL);
113 	evtimer_set(&control_state.evt, control_accept, NULL);
114 
115 	return (0);
116 }
117 
118 void
119 control_cleanup(void)
120 {
121 	struct ctl_conn	*c, *t;
122 
123 	TAILQ_FOREACH_SAFE(c, &ctl_conns, entry, t) {
124 		TAILQ_REMOVE(&ctl_conns, c, entry);
125 		control_connfree(c);
126 	}
127 	event_del(&control_state.ev);
128 	event_del(&control_state.evt);
129 }
130 
131 /* ARGSUSED */
132 void
133 control_accept(int listenfd, short event, void *bula)
134 {
135 	int			 connfd;
136 	socklen_t		 len;
137 	struct sockaddr_un	 sun;
138 	struct ctl_conn		*c;
139 
140 	event_add(&control_state.ev, NULL);
141 	if ((event & EV_TIMEOUT))
142 		return;
143 
144 	len = sizeof(sun);
145 	if ((connfd = accept4(listenfd, (struct sockaddr *)&sun, &len,
146 	    SOCK_CLOEXEC | SOCK_NONBLOCK)) == -1) {
147 		/*
148 		 * Pause accept if we are out of file descriptors, or
149 		 * libevent will haunt us here too.
150 		 */
151 		if (errno == ENFILE || errno == EMFILE) {
152 			struct timeval evtpause = { 1, 0 };
153 
154 			event_del(&control_state.ev);
155 			evtimer_add(&control_state.evt, &evtpause);
156 		} else if (errno != EWOULDBLOCK && errno != EINTR &&
157 		    errno != ECONNABORTED)
158 			log_warn("control_accept: accept");
159 		return;
160 	}
161 
162 	if ((c = calloc(1, sizeof(struct ctl_conn))) == NULL) {
163 		log_warn("control_accept");
164 		close(connfd);
165 		return;
166 	}
167 
168 	if (imsgbuf_init(&c->iev.ibuf, connfd) == -1) {
169 		log_warn("control_accept");
170 		close(connfd);
171 		free(c);
172 		return;
173 	}
174 	if (idseq == 0)	/* don't use zero.  See radiusd_module_imsg */
175 		++idseq;
176 	c->id = idseq++;
177 	c->iev.handler = control_dispatch_imsg;
178 	c->iev.events = EV_READ;
179 	event_set(&c->iev.ev, c->iev.ibuf.fd, c->iev.events, c->iev.handler, c);
180 	event_add(&c->iev.ev, NULL);
181 
182 	TAILQ_INSERT_TAIL(&ctl_conns, c, entry);
183 }
184 
185 struct ctl_conn *
186 control_connbyfd(int fd)
187 {
188 	struct ctl_conn	*c;
189 
190 	TAILQ_FOREACH(c, &ctl_conns, entry) {
191 		if (c->iev.ibuf.fd == fd)
192 			break;
193 	}
194 
195 	return (c);
196 }
197 
198 struct ctl_conn *
199 control_connbyid(uint32_t id)
200 {
201 	struct ctl_conn	*c;
202 
203 	TAILQ_FOREACH(c, &ctl_conns, entry) {
204 		if (c->id == id)
205 			break;
206 	}
207 
208 	return (c);
209 }
210 
211 void
212 control_close(int fd)
213 {
214 	struct ctl_conn	*c;
215 
216 	if ((c = control_connbyfd(fd)) == NULL) {
217 		log_warn("control_close: fd %d: not found", fd);
218 		return;
219 	}
220 	if (c->modulename[0] != '\0')
221 		radiusd_imsg_compose_module(radiusd_s, c->modulename,
222 		    IMSG_RADIUSD_MODULE_CTRL_UNBIND, c->id, -1, -1, NULL, 0);
223 
224 	control_connfree(c);
225 }
226 
227 void
228 control_connfree(struct ctl_conn *c)
229 {
230 	imsgbuf_clear(&c->iev.ibuf);
231 	TAILQ_REMOVE(&ctl_conns, c, entry);
232 
233 	event_del(&c->iev.ev);
234 	close(c->iev.ibuf.fd);
235 
236 	/* Some file descriptors are available again. */
237 	if (evtimer_pending(&control_state.evt, NULL)) {
238 		evtimer_del(&control_state.evt);
239 		event_add(&control_state.ev, NULL);
240 	}
241 
242 	free(c);
243 }
244 
245 /* ARGSUSED */
246 void
247 control_dispatch_imsg(int fd, short event, void *bula)
248 {
249 	struct ctl_conn	*c;
250 	struct imsg	 imsg;
251 	ssize_t		 n, datalen;
252 	char		 modulename[RADIUSD_MODULE_NAME_LEN + 1], msg[128];
253 
254 	if ((c = control_connbyfd(fd)) == NULL) {
255 		log_warn("control_dispatch_imsg: fd %d: not found", fd);
256 		return;
257 	}
258 
259 	if (event & EV_READ) {
260 		if (imsgbuf_read(&c->iev.ibuf) != 1) {
261 			control_close(fd);
262 			return;
263 		}
264 	}
265 	if (event & EV_WRITE) {
266 		if (imsgbuf_write(&c->iev.ibuf) == -1) {
267 			control_close(fd);
268 			return;
269 		}
270 	}
271 
272 	for (;;) {
273 		if ((n = imsg_get(&c->iev.ibuf, &imsg)) == -1) {
274 			control_close(fd);
275 			return;
276 		}
277 
278 		if (n == 0)
279 			break;
280 
281 		datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
282 		switch (imsg.hdr.type) {
283 		default:
284 			if (imsg.hdr.type >= IMSG_RADIUSD_MODULE_MIN) {
285 				if (datalen < RADIUSD_MODULE_NAME_LEN) {
286 					log_warnx( "%s: received an invalid "
287 					    "imsg %d: too small", __func__,
288 					    imsg.hdr.type);
289 					break;
290 				}
291 				memset(modulename, 0, sizeof(modulename));
292 				memcpy(modulename, imsg.data,
293 				    RADIUSD_MODULE_NAME_LEN);
294 				if (radiusd_imsg_compose_module(radiusd_s,
295 				    modulename, imsg.hdr.type, c->id, -1, -1,
296 				    (caddr_t)imsg.data +
297 				    RADIUSD_MODULE_NAME_LEN, datalen -
298 				    RADIUSD_MODULE_NAME_LEN) != 0) {
299 					snprintf(msg, sizeof(msg),
300 					    "module `%s' is not loaded or not "
301 					    "capable for control command",
302 					    modulename);
303 					imsg_compose_event(&c->iev,
304 					    IMSG_NG, c->id, -1, -1, msg,
305 					    strlen(msg) + 1);
306 				}
307 			} else
308 				log_debug("control_dispatch_imsg: "
309 				    "error handling imsg %d", imsg.hdr.type);
310 			break;
311 		}
312 		imsg_free(&imsg);
313 	}
314 	imsg_event_add(&c->iev);
315 }
316 
317 int
318 control_imsg_relay(struct imsg *imsg)
319 {
320 	struct ctl_conn	*c;
321 
322 	if ((c = control_connbyid(imsg->hdr.peerid)) == NULL)
323 		return (0);
324 
325 	return (imsg_compose_event(&c->iev, imsg->hdr.type, 0, imsg->hdr.pid,
326 	    -1, imsg->data, imsg->hdr.len - IMSG_HEADER_SIZE));
327 }
328 
329 void
330 control_conn_bind(uint32_t peerid, const char *modulename)
331 {
332 	struct ctl_conn	*c;
333 
334 	if ((c = control_connbyid(peerid)) == NULL)
335 		return;
336 
337 	if (c->modulename[0] != '\0')
338 		radiusd_imsg_compose_module(radiusd_s, c->modulename,
339 		    IMSG_RADIUSD_MODULE_CTRL_UNBIND, c->id, -1, -1, NULL, 0);
340 	strlcpy(c->modulename, modulename, sizeof(c->modulename));
341 }
342