xref: /openbsd/usr.sbin/rad/control.c (revision 09467b48)
1 /*	$OpenBSD: control.c,v 1.5 2019/03/02 03:40:45 pamela 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 <netinet/in.h>
26 #include <net/if.h>
27 
28 #include <errno.h>
29 #include <event.h>
30 #include <imsg.h>
31 #include <md5.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 
36 #include "log.h"
37 #include "rad.h"
38 #include "control.h"
39 #include "frontend.h"
40 
41 #define	CONTROL_BACKLOG	5
42 
43 struct ctl_conn	*control_connbyfd(int);
44 struct ctl_conn	*control_connbypid(pid_t);
45 void		 control_close(int);
46 
47 int
48 control_init(char *path)
49 {
50 	struct sockaddr_un	 sun;
51 	int			 fd;
52 	mode_t			 old_umask;
53 
54 	if ((fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
55 	    0)) == -1) {
56 		log_warn("%s: socket", __func__);
57 		return (-1);
58 	}
59 
60 	memset(&sun, 0, sizeof(sun));
61 	sun.sun_family = AF_UNIX;
62 	strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
63 
64 	if (unlink(path) == -1)
65 		if (errno != ENOENT) {
66 			log_warn("%s: unlink %s", __func__, path);
67 			close(fd);
68 			return (-1);
69 		}
70 
71 	old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
72 	if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
73 		log_warn("%s: bind: %s", __func__, path);
74 		close(fd);
75 		umask(old_umask);
76 		return (-1);
77 	}
78 	umask(old_umask);
79 
80 	if (chmod(path, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -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,
146 	    c->iev.handler, &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 
211 	if ((c = control_connbyfd(fd)) == NULL) {
212 		log_warnx("%s: fd %d: not found", __func__, fd);
213 		return;
214 	}
215 
216 	if (event & EV_READ) {
217 		if (((n = imsg_read(&c->iev.ibuf)) == -1 && errno != EAGAIN) ||
218 		    n == 0) {
219 			control_close(fd);
220 			return;
221 		}
222 	}
223 	if (event & EV_WRITE) {
224 		if (msgbuf_write(&c->iev.ibuf.w) <= 0 && errno != EAGAIN) {
225 			control_close(fd);
226 			return;
227 		}
228 	}
229 
230 	for (;;) {
231 		if ((n = imsg_get(&c->iev.ibuf, &imsg)) == -1) {
232 			control_close(fd);
233 			return;
234 		}
235 		if (n == 0)
236 			break;
237 
238 		switch (imsg.hdr.type) {
239 		case IMSG_CTL_RELOAD:
240 			frontend_imsg_compose_main(imsg.hdr.type, 0, NULL, 0);
241 			break;
242 		case IMSG_CTL_LOG_VERBOSE:
243 			if (IMSG_DATA_SIZE(imsg) != sizeof(verbose))
244 				break;
245 
246 			/* Forward to all other processes. */
247 			frontend_imsg_compose_main(imsg.hdr.type, imsg.hdr.pid,
248 			    imsg.data, IMSG_DATA_SIZE(imsg));
249 			frontend_imsg_compose_engine(imsg.hdr.type,
250 			    imsg.hdr.pid, imsg.data, IMSG_DATA_SIZE(imsg));
251 
252 			memcpy(&verbose, imsg.data, sizeof(verbose));
253 			log_setverbose(verbose);
254 			break;
255 		default:
256 			log_debug("%s: error handling imsg %d", __func__,
257 			    imsg.hdr.type);
258 			break;
259 		}
260 		imsg_free(&imsg);
261 	}
262 
263 	imsg_event_add(&c->iev);
264 }
265 
266 int
267 control_imsg_relay(struct imsg *imsg)
268 {
269 	struct ctl_conn	*c;
270 
271 	if ((c = control_connbypid(imsg->hdr.pid)) == NULL)
272 		return (0);
273 
274 	return (imsg_compose_event(&c->iev, imsg->hdr.type, 0, imsg->hdr.pid,
275 	    -1, imsg->data, IMSG_DATA_SIZE(*imsg)));
276 }
277