xref: /openbsd-src/usr.sbin/httpd/control.c (revision 1ad61ae0a79a724d2d3ec69e69c8e1d1ff6b53a0)
1 /*	$OpenBSD: control.c,v 1.15 2023/03/08 04:43:13 guenther 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/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 <errno.h>
26 #include <event.h>
27 #include <fcntl.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <imsg.h>
32 
33 #include "httpd.h"
34 
35 #define	CONTROL_BACKLOG	5
36 
37 struct ctl_connlist ctl_conns = TAILQ_HEAD_INITIALIZER(ctl_conns);
38 
39 void		 control_accept(int, short, void *);
40 void		 control_close(int, struct control_sock *);
41 
42 int
43 control_init(struct privsep *ps, struct control_sock *cs)
44 {
45 	struct httpd		*env = ps->ps_env;
46 	struct sockaddr_un	 sun;
47 	int			 fd;
48 	mode_t			 old_umask, mode;
49 
50 	if (cs->cs_name == NULL)
51 		return (0);
52 
53 	if ((fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0)) == -1) {
54 		log_warn("%s: socket", __func__);
55 		return (-1);
56 	}
57 
58 	sun.sun_family = AF_UNIX;
59 	if (strlcpy(sun.sun_path, cs->cs_name,
60 	    sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
61 		log_warn("%s: %s name too long", __func__, cs->cs_name);
62 		close(fd);
63 		return (-1);
64 	}
65 
66 	if (unlink(cs->cs_name) == -1)
67 		if (errno != ENOENT) {
68 			log_warn("%s: unlink %s", __func__, cs->cs_name);
69 			close(fd);
70 			return (-1);
71 		}
72 
73 	if (cs->cs_restricted) {
74 		old_umask = umask(S_IXUSR|S_IXGRP|S_IXOTH);
75 		mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
76 	} else {
77 		old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
78 		mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP;
79 	}
80 
81 	if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
82 		log_warn("%s: bind: %s", __func__, cs->cs_name);
83 		close(fd);
84 		(void)umask(old_umask);
85 		return (-1);
86 	}
87 	(void)umask(old_umask);
88 
89 	if (chmod(cs->cs_name, mode) == -1) {
90 		log_warn("%s: chmod", __func__);
91 		close(fd);
92 		(void)unlink(cs->cs_name);
93 		return (-1);
94 	}
95 
96 	cs->cs_fd = fd;
97 	cs->cs_env = env;
98 
99 	return (0);
100 }
101 
102 int
103 control_listen(struct control_sock *cs)
104 {
105 	if (cs->cs_name == NULL)
106 		return (0);
107 
108 	if (listen(cs->cs_fd, CONTROL_BACKLOG) == -1) {
109 		log_warn("%s: listen", __func__);
110 		return (-1);
111 	}
112 
113 	event_set(&cs->cs_ev, cs->cs_fd, EV_READ,
114 	    control_accept, cs);
115 	event_add(&cs->cs_ev, NULL);
116 	evtimer_set(&cs->cs_evt, control_accept, cs);
117 
118 	return (0);
119 }
120 
121 void
122 control_cleanup(struct control_sock *cs)
123 {
124 	if (cs->cs_name == NULL)
125 		return;
126 	event_del(&cs->cs_ev);
127 	event_del(&cs->cs_evt);
128 }
129 
130 void
131 control_accept(int listenfd, short event, void *arg)
132 {
133 	int			 connfd;
134 	socklen_t		 len;
135 	struct sockaddr_un	 sun;
136 	struct ctl_conn		*c;
137 	struct control_sock	*cs = arg;
138 
139 	event_add(&cs->cs_ev, NULL);
140 	if ((event & EV_TIMEOUT))
141 		return;
142 
143 	len = sizeof(sun);
144 	if ((connfd = accept4(listenfd,
145 	    (struct sockaddr *)&sun, &len, SOCK_NONBLOCK)) == -1) {
146 		/*
147 		 * Pause accept if we are out of file descriptors, or
148 		 * libevent will haunt us here too.
149 		 */
150 		if (errno == ENFILE || errno == EMFILE) {
151 			struct timeval evtpause = { 1, 0 };
152 
153 			event_del(&cs->cs_ev);
154 			evtimer_add(&cs->cs_evt, &evtpause);
155 		} else if (errno != EWOULDBLOCK && errno != EINTR &&
156 		    errno != ECONNABORTED)
157 			log_warn("%s: accept", __func__);
158 		return;
159 	}
160 
161 	if ((c = calloc(1, sizeof(struct ctl_conn))) == NULL) {
162 		close(connfd);
163 		log_warn("%s: calloc", __func__);
164 		return;
165 	}
166 
167 	imsg_init(&c->iev.ibuf, connfd);
168 	c->iev.handler = control_dispatch_imsg;
169 	c->iev.events = EV_READ;
170 	c->iev.data = cs;	/* proc.c cheats (reuses the handler) */
171 	event_set(&c->iev.ev, c->iev.ibuf.fd, c->iev.events,
172 	    c->iev.handler, cs);
173 	event_add(&c->iev.ev, NULL);
174 
175 	TAILQ_INSERT_TAIL(&ctl_conns, c, entry);
176 }
177 
178 struct ctl_conn *
179 control_connbyfd(int fd)
180 {
181 	struct ctl_conn	*c;
182 
183 	TAILQ_FOREACH(c, &ctl_conns, entry) {
184 		if (c->iev.ibuf.fd == fd)
185 			break;
186 	}
187 
188 	return (c);
189 }
190 
191 void
192 control_close(int fd, struct control_sock *cs)
193 {
194 	struct ctl_conn	*c;
195 
196 	if ((c = control_connbyfd(fd)) == NULL) {
197 		log_warn("%s: fd %d not found", __func__, fd);
198 		return;
199 	}
200 
201 	msgbuf_clear(&c->iev.ibuf.w);
202 	TAILQ_REMOVE(&ctl_conns, c, entry);
203 
204 	event_del(&c->iev.ev);
205 	close(c->iev.ibuf.fd);
206 
207 	/* Some file descriptors are available again. */
208 	if (evtimer_pending(&cs->cs_evt, NULL)) {
209 		evtimer_del(&cs->cs_evt);
210 		event_add(&cs->cs_ev, NULL);
211 	}
212 
213 	free(c);
214 }
215 
216 void
217 control_dispatch_imsg(int fd, short event, void *arg)
218 {
219 	struct control_sock	*cs = arg;
220 	struct ctl_conn		*c;
221 	struct imsg		 imsg;
222 	int			 n;
223 	int			 verbose;
224 	struct httpd		*env = cs->cs_env;
225 
226 	if ((c = control_connbyfd(fd)) == NULL) {
227 		log_warn("%s: fd %d not found", __func__, fd);
228 		return;
229 	}
230 
231 	if (event & EV_READ) {
232 		if (((n = imsg_read(&c->iev.ibuf)) == -1 && errno != EAGAIN) ||
233 		    n == 0) {
234 			control_close(fd, cs);
235 			return;
236 		}
237 	}
238 
239 	if (event & EV_WRITE) {
240 		if (msgbuf_write(&c->iev.ibuf.w) <= 0 && errno != EAGAIN) {
241 			control_close(fd, cs);
242 			return;
243 		}
244 	}
245 
246 	for (;;) {
247 		if ((n = imsg_get(&c->iev.ibuf, &imsg)) == -1) {
248 			control_close(fd, cs);
249 			return;
250 		}
251 
252 		if (n == 0)
253 			break;
254 
255 		if (c->waiting) {
256 			log_debug("%s: unexpected imsg %d",
257 			    __func__, imsg.hdr.type);
258 			imsg_free(&imsg);
259 			control_close(fd, cs);
260 			return;
261 		}
262 
263 		switch (imsg.hdr.type) {
264 		case IMSG_CTL_SHUTDOWN:
265 		case IMSG_CTL_RELOAD:
266 		case IMSG_CTL_REOPEN:
267 			proc_forward_imsg(env->sc_ps, &imsg, PROC_PARENT, -1);
268 			break;
269 		case IMSG_CTL_NOTIFY:
270 			if (c->flags & CTL_CONN_NOTIFY) {
271 				log_debug("%s: "
272 				    "client requested notify more than once",
273 				    __func__);
274 				imsg_compose_event(&c->iev, IMSG_CTL_FAIL,
275 				    0, env->sc_ps->ps_instance + 1, -1,
276 				    NULL, 0);
277 				break;
278 			}
279 			c->flags |= CTL_CONN_NOTIFY;
280 			break;
281 		case IMSG_CTL_VERBOSE:
282 			IMSG_SIZE_CHECK(&imsg, &verbose);
283 
284 			memcpy(&verbose, imsg.data, sizeof(verbose));
285 
286 			proc_forward_imsg(env->sc_ps, &imsg, PROC_PARENT, -1);
287 			proc_forward_imsg(env->sc_ps, &imsg, PROC_SERVER, -1);
288 
289 			memcpy(imsg.data, &verbose, sizeof(verbose));
290 			control_imsg_forward(env->sc_ps, &imsg);
291 			log_setverbose(verbose);
292 			break;
293 		default:
294 			log_debug("%s: error handling imsg %d",
295 			    __func__, imsg.hdr.type);
296 			break;
297 		}
298 		imsg_free(&imsg);
299 	}
300 
301 	imsg_event_add(&c->iev);
302 }
303 
304 void
305 control_imsg_forward(struct privsep *ps, struct imsg *imsg)
306 {
307 	struct ctl_conn *c;
308 
309 	TAILQ_FOREACH(c, &ctl_conns, entry)
310 		if (c->flags & CTL_CONN_NOTIFY)
311 			imsg_compose_event(&c->iev, imsg->hdr.type,
312 			    0, ps->ps_instance + 1, -1, imsg->data,
313 			    imsg->hdr.len - IMSG_HEADER_SIZE);
314 }
315