1 /* $OpenBSD: control.c,v 1.65 2026/03/02 19:28:01 rsadowski Exp $ */
4 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
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.
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.
19 #include <sys/queue.h>
21 #include <sys/socket.h>
36 #define CONTROL_BACKLOG 5
38 struct ctl_connlist ctl_conns = TAILQ_HEAD_INITIALIZER(ctl_conns);
40 void control_accept(int, short, void *);
41 void control_close(int, struct control_sock *);
44 control_init(struct privsep *ps, struct control_sock *cs)
46 struct relayd *env = ps->ps_env;
47 struct sockaddr_un sun;
49 mode_t old_umask, mode;
51 if (cs->cs_name == NULL)
54 if ((fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0)) == -1) {
55 log_warn("%s: socket", __func__);
59 sun.sun_family = AF_UNIX;
60 if (strlcpy(sun.sun_path, cs->cs_name,
61 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
62 log_warn("%s: %s name too long", __func__, cs->cs_name);
67 if (unlink(cs->cs_name) == -1)
68 if (errno != ENOENT) {
69 log_warn("%s: unlink %s", __func__, cs->cs_name);
74 if (cs->cs_restricted) {
75 old_umask = umask(S_IXUSR|S_IXGRP|S_IXOTH);
76 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
78 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
79 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP;
82 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
83 log_warn("%s: bind: %s", __func__, cs->cs_name);
85 (void)umask(old_umask);
88 (void)umask(old_umask);
90 if (chmod(cs->cs_name, mode) == -1) {
91 log_warn("%s: chmod", __func__);
93 (void)unlink(cs->cs_name);
104 control_listen(struct control_sock *cs)
106 if (cs->cs_name == NULL)
109 if (listen(cs->cs_fd, CONTROL_BACKLOG) == -1) {
110 log_warn("%s: listen", __func__);
114 event_set(&cs->cs_ev, cs->cs_fd, EV_READ,
116 event_add(&cs->cs_ev, NULL);
117 evtimer_set(&cs->cs_evt, control_accept, cs);
123 control_cleanup(struct control_sock *cs)
125 if (cs->cs_name == NULL)
127 event_del(&cs->cs_ev);
128 event_del(&cs->cs_evt);
132 control_accept(int listenfd, short event, void *arg)
136 struct sockaddr_un sun;
138 struct control_sock *cs = arg;
140 event_add(&cs->cs_ev, NULL);
141 if ((event & EV_TIMEOUT))
145 if ((connfd = accept4(listenfd,
146 (struct sockaddr *)&sun, &len, SOCK_NONBLOCK)) == -1) {
148 * Pause accept if we are out of file descriptors, or
149 * libevent will haunt us here too.
151 if (errno == ENFILE || errno == EMFILE) {
152 struct timeval evtpause = { 1, 0 };
154 event_del(&cs->cs_ev);
155 evtimer_add(&cs->cs_evt, &evtpause);
156 } else if (errno != EWOULDBLOCK && errno != EINTR &&
157 errno != ECONNABORTED)
158 log_warn("%s: accept", __func__);
162 if ((c = calloc(1, sizeof(struct ctl_conn))) == NULL) {
163 log_warn("%s: calloc", __func__);
168 if (imsgbuf_init(&c->iev.ibuf, connfd) == -1) {
169 log_warn("%s: imsgbuf_init", __func__);
175 c->iev.handler = control_dispatch_imsg;
176 c->iev.events = EV_READ;
177 c->iev.data = cs; /* proc.c cheats (reuses the handler) */
178 event_set(&c->iev.ev, c->iev.ibuf.fd, c->iev.events,
180 event_add(&c->iev.ev, NULL);
182 TAILQ_INSERT_TAIL(&ctl_conns, c, entry);
186 control_connbyfd(int fd)
190 TAILQ_FOREACH(c, &ctl_conns, entry) {
191 if (c->iev.ibuf.fd == fd)
199 control_close(int fd, struct control_sock *cs)
203 if ((c = control_connbyfd(fd)) == NULL) {
204 log_warn("%s: fd %d not found", __func__, fd);
208 imsgbuf_clear(&c->iev.ibuf);
209 TAILQ_REMOVE(&ctl_conns, c, entry);
211 event_del(&c->iev.ev);
212 close(c->iev.ibuf.fd);
214 /* Some file descriptors are available again. */
215 if (evtimer_pending(&cs->cs_evt, NULL)) {
216 evtimer_del(&cs->cs_evt);
217 event_add(&cs->cs_ev, NULL);
224 control_dispatch_imsg(int fd, short event, void *arg)
226 struct control_sock *cs = arg;
232 struct relayd *env = cs->cs_env;
233 struct privsep *ps = env->sc_ps;
235 if ((c = control_connbyfd(fd)) == NULL) {
236 log_warn("%s: fd %d not found", __func__, fd);
240 if (event & EV_READ) {
241 if (imsgbuf_read(&c->iev.ibuf) != 1) {
242 control_close(fd, cs);
247 if (event & EV_WRITE) {
248 if (imsgbuf_write(&c->iev.ibuf) == -1) {
249 control_close(fd, cs);
255 if ((n = imsg_get(&c->iev.ibuf, &imsg)) == -1) {
256 control_close(fd, cs);
264 log_debug("%s: unexpected imsg %d",
265 __func__, imsg.hdr.type);
267 control_close(fd, cs);
271 switch (imsg.hdr.type) {
272 case IMSG_CTL_SHOW_SUM:
275 case IMSG_CTL_SESSION:
278 case IMSG_CTL_RDR_DISABLE:
279 if (imsg.hdr.len != IMSG_HEADER_SIZE + sizeof(id))
280 fatalx("invalid imsg header len");
281 memcpy(&id, imsg.data, sizeof(id));
282 if (disable_rdr(c, &id))
283 imsg_compose_event(&c->iev, IMSG_CTL_FAIL,
284 0, ps->ps_instance + 1, -1, NULL, 0);
286 memcpy(imsg.data, &id, sizeof(id));
287 control_imsg_forward(ps, &imsg);
288 imsg_compose_event(&c->iev, IMSG_CTL_OK,
289 0, ps->ps_instance + 1, -1, NULL, 0);
292 case IMSG_CTL_RDR_ENABLE:
293 if (imsg.hdr.len != IMSG_HEADER_SIZE + sizeof(id))
294 fatalx("invalid imsg header len");
295 memcpy(&id, imsg.data, sizeof(id));
296 if (enable_rdr(c, &id))
297 imsg_compose_event(&c->iev, IMSG_CTL_FAIL,
298 0, ps->ps_instance + 1, -1, NULL, 0);
300 memcpy(imsg.data, &id, sizeof(id));
301 control_imsg_forward(ps, &imsg);
302 imsg_compose_event(&c->iev, IMSG_CTL_OK,
303 0, ps->ps_instance + 1, -1, NULL, 0);
306 case IMSG_CTL_TABLE_DISABLE:
307 if (imsg.hdr.len != IMSG_HEADER_SIZE + sizeof(id))
308 fatalx("invalid imsg header len");
309 memcpy(&id, imsg.data, sizeof(id));
310 if (disable_table(c, &id))
311 imsg_compose_event(&c->iev, IMSG_CTL_FAIL,
312 0, ps->ps_instance + 1, -1, NULL, 0);
314 memcpy(imsg.data, &id, sizeof(id));
315 control_imsg_forward(ps, &imsg);
316 imsg_compose_event(&c->iev, IMSG_CTL_OK,
317 0, ps->ps_instance + 1, -1, NULL, 0);
320 case IMSG_CTL_TABLE_ENABLE:
321 if (imsg.hdr.len != IMSG_HEADER_SIZE + sizeof(id))
322 fatalx("invalid imsg header len");
323 memcpy(&id, imsg.data, sizeof(id));
324 if (enable_table(c, &id))
325 imsg_compose_event(&c->iev, IMSG_CTL_FAIL,
326 0, ps->ps_instance + 1, -1, NULL, 0);
328 memcpy(imsg.data, &id, sizeof(id));
329 control_imsg_forward(ps, &imsg);
330 imsg_compose_event(&c->iev, IMSG_CTL_OK,
331 0, ps->ps_instance + 1, -1, NULL, 0);
334 case IMSG_CTL_HOST_DISABLE:
335 if (imsg.hdr.len != IMSG_HEADER_SIZE + sizeof(id))
336 fatalx("invalid imsg header len");
337 memcpy(&id, imsg.data, sizeof(id));
338 if (disable_host(c, &id, NULL))
339 imsg_compose_event(&c->iev, IMSG_CTL_FAIL,
340 0, ps->ps_instance + 1, -1, NULL, 0);
342 memcpy(imsg.data, &id, sizeof(id));
343 control_imsg_forward(ps, &imsg);
344 imsg_compose_event(&c->iev, IMSG_CTL_OK,
345 0, ps->ps_instance + 1, -1, NULL, 0);
348 case IMSG_CTL_HOST_ENABLE:
349 if (imsg.hdr.len != IMSG_HEADER_SIZE + sizeof(id))
350 fatalx("invalid imsg header len");
351 memcpy(&id, imsg.data, sizeof(id));
352 if (enable_host(c, &id, NULL))
353 imsg_compose_event(&c->iev, IMSG_CTL_FAIL,
354 0, ps->ps_instance + 1, -1, NULL, 0);
356 memcpy(imsg.data, &id, sizeof(id));
357 control_imsg_forward(ps, &imsg);
358 imsg_compose_event(&c->iev, IMSG_CTL_OK,
359 0, ps->ps_instance + 1, -1, NULL, 0);
362 case IMSG_CTL_SHUTDOWN:
363 case IMSG_CTL_RELOAD:
364 proc_forward_imsg(env->sc_ps, &imsg, PROC_PARENT, -1);
367 proc_compose(env->sc_ps, PROC_HCE,
368 IMSG_CTL_POLL, NULL, 0);
369 imsg_compose_event(&c->iev, IMSG_CTL_OK,
370 0, ps->ps_instance + 1, -1, NULL, 0);
372 case IMSG_CTL_NOTIFY:
373 if (c->flags & CTL_CONN_NOTIFY) {
375 "client requested notify more than once",
377 imsg_compose_event(&c->iev, IMSG_CTL_FAIL,
378 0, ps->ps_instance + 1, -1, NULL, 0);
381 c->flags |= CTL_CONN_NOTIFY;
383 case IMSG_CTL_VERBOSE:
384 IMSG_SIZE_CHECK(&imsg, &verbose);
386 memcpy(&verbose, imsg.data, sizeof(verbose));
388 proc_forward_imsg(env->sc_ps, &imsg, PROC_PARENT, -1);
389 proc_forward_imsg(env->sc_ps, &imsg, PROC_HCE, -1);
390 proc_forward_imsg(env->sc_ps, &imsg, PROC_RELAY, -1);
392 memcpy(imsg.data, &verbose, sizeof(verbose));
393 control_imsg_forward(ps, &imsg);
394 log_setverbose(verbose);
397 log_debug("%s: error handling imsg %d",
398 __func__, imsg.hdr.type);
404 imsg_event_add(&c->iev);
408 control_imsg_forward(struct privsep *ps, struct imsg *imsg)
412 TAILQ_FOREACH(c, &ctl_conns, entry)
413 if (c->flags & CTL_CONN_NOTIFY)
414 imsg_compose_event(&c->iev, imsg->hdr.type,
415 0, ps->ps_instance + 1, -1, imsg->data,
416 imsg->hdr.len - IMSG_HEADER_SIZE);