2 * Copyright (c) 2010 - 2016 Reyk Floeter <reyk@openbsd.org>
3 * Copyright (c) 2008 Pierre-Yves Ritschard <pyr@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/types.h>
19 #include <sys/queue.h>
20 #include <sys/socket.h>
35 #include "got_compat.h"
37 void proc_exec(struct privsep *, struct privsep_proc *, unsigned int,
39 void proc_setup(struct privsep *, struct privsep_proc *, unsigned int);
40 void proc_open(struct privsep *, int, int);
41 void proc_accept(struct privsep *, int, enum privsep_procid,
43 void proc_close(struct privsep *);
44 int proc_ispeer(struct privsep_proc *, unsigned int, enum privsep_procid);
45 void proc_shutdown(struct privsep_proc *);
46 void proc_sig_handler(int, short, void *);
47 int proc_dispatch_null(int, struct privsep_proc *, struct imsg *);
49 enum privsep_procid privsep_process;
52 proc_ispeer(struct privsep_proc *procs, unsigned int nproc,
53 enum privsep_procid type)
57 for (i = 0; i < nproc; i++)
58 if (procs[i].p_id == type)
65 proc_getid(struct privsep_proc *procs, unsigned int nproc,
66 const char *proc_name)
68 struct privsep_proc *p;
71 for (proc = 0; proc < nproc; proc++) {
73 if (strcmp(p->p_title, proc_name))
83 proc_exec(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
84 int argc, char **argv)
86 unsigned int proc, nargc, i, proc_i;
88 struct privsep_proc *p;
92 /* Prepare the new process argv. */
93 nargv = calloc(argc + 5, sizeof(char *));
95 fatal("%s: calloc", __func__);
97 /* Copy call argument first. */
99 nargv[nargc++] = argv[0];
101 /* Set process name argument and save the position. */
102 nargv[nargc] = strdup("-P");
103 if (nargv[nargc] == NULL)
104 fatal("%s: strdup", __func__);
109 /* Point process instance arg to stack and copy the original args. */
110 nargv[nargc] = strdup("-I");
111 if (nargv[nargc] == NULL)
112 fatal("%s: strdup", __func__);
114 nargv[nargc++] = num;
115 for (i = 1; i < (unsigned int) argc; i++)
116 nargv[nargc++] = argv[i];
120 for (proc = 0; proc < nproc; proc++) {
123 /* Update args with process title. */
124 nargv[proc_i] = (char *)(uintptr_t)p->p_title;
126 /* Fire children processes. */
127 for (i = 0; i < ps->ps_instances[p->p_id]; i++) {
128 /* Update the process instance number. */
129 snprintf(num, sizeof(num), "%u", i);
131 fd = ps->ps_pipes[p->p_id][i].pp_pipes[PROC_GOTWEBD][0];
132 ps->ps_pipes[p->p_id][i].pp_pipes[PROC_GOTWEBD][0] = -1;
136 fatal("%s: fork", __func__);
139 /* First create a new session */
143 /* Prepare parent socket. */
144 if (fd != PROC_GOTWEBD_SOCK_FILENO) {
145 if (dup2(fd, PROC_GOTWEBD_SOCK_FILENO)
148 } else if (fcntl(fd, F_SETFD, 0) == -1)
151 execvp(argv[0], nargv);
152 fatal("%s: execvp", __func__);
155 /* Close child end. */
166 proc_connect(struct privsep *ps)
169 unsigned int src, dst, inst;
171 /* Don't distribute any sockets if we are not really going to run. */
175 for (dst = 0; dst < PROC_MAX; dst++) {
176 /* We don't communicate with ourselves. */
177 if (dst == PROC_GOTWEBD)
180 for (inst = 0; inst < ps->ps_instances[dst]; inst++) {
181 iev = &ps->ps_ievs[dst][inst];
182 imsg_init(&iev->ibuf, ps->ps_pp->pp_pipes[dst][inst]);
183 event_set(&iev->ev, iev->ibuf.fd, iev->events,
184 iev->handler, iev->data);
185 event_add(&iev->ev, NULL);
189 /* Distribute the socketpair()s for everyone. */
190 for (src = 0; src < PROC_MAX; src++)
191 for (dst = src; dst < PROC_MAX; dst++) {
192 /* Parent already distributed its fds. */
193 if (src == PROC_GOTWEBD || dst == PROC_GOTWEBD)
196 proc_open(ps, src, dst);
201 proc_init(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
202 int argc, char **argv, enum privsep_procid proc_id)
204 struct privsep_proc *p = NULL;
205 struct privsep_pipes *pa, *pb;
210 /* Don't initiate anything if we are not really going to run. */
214 if (proc_id == PROC_GOTWEBD) {
215 privsep_process = PROC_GOTWEBD;
216 proc_setup(ps, procs, nproc);
219 * Create the children sockets so we can use them
220 * to distribute the rest of the socketpair()s using
221 * proc_connect() later.
223 for (dst = 0; dst < PROC_MAX; dst++) {
224 /* Don't create socket for ourselves. */
225 if (dst == PROC_GOTWEBD)
228 for (proc = 0; proc < ps->ps_instances[dst]; proc++) {
229 pa = &ps->ps_pipes[PROC_GOTWEBD][0];
230 pb = &ps->ps_pipes[dst][proc];
231 int sock_flags = SOCK_STREAM | SOCK_NONBLOCK;
233 sock_flags |= SOCK_CLOEXEC;
235 if (socketpair(AF_UNIX, sock_flags,
236 PF_UNSPEC, fds) == -1)
237 fatal("%s: socketpair", __func__);
239 pa->pp_pipes[dst][proc] = fds[0];
240 pb->pp_pipes[PROC_GOTWEBD][0] = fds[1];
245 proc_exec(ps, procs, nproc, argc, argv);
249 /* Initialize a child */
250 for (proc = 0; proc < nproc; proc++) {
251 if (procs[proc].p_id != proc_id)
256 if (p == NULL || p->p_init == NULL)
257 fatalx("%s: process %d missing process initialization",
262 fatalx("failed to initiate child process");
266 proc_accept(struct privsep *ps, int fd, enum privsep_procid dst,
269 struct privsep_pipes *pp = ps->ps_pp;
272 if (ps->ps_ievs[dst] == NULL) {
274 log_debug("%s: %s src %d %d to dst %d %d not connected",
275 __func__, ps->ps_title[privsep_process],
276 privsep_process, ps->ps_instance + 1,
283 if (pp->pp_pipes[dst][n] != -1) {
284 log_warnx("%s: duplicated descriptor", __func__);
288 pp->pp_pipes[dst][n] = fd;
290 iev = &ps->ps_ievs[dst][n];
291 imsg_init(&iev->ibuf, fd);
292 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
293 event_add(&iev->ev, NULL);
297 proc_setup(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc)
299 unsigned int i, j, src, dst, id;
300 struct privsep_pipes *pp;
302 /* Initialize parent title, ps_instances and procs. */
303 ps->ps_title[PROC_GOTWEBD] = "parent";
305 for (src = 0; src < PROC_MAX; src++)
306 /* Default to 1 process instance */
307 if (ps->ps_instances[src] < 1)
308 ps->ps_instances[src] = 1;
310 for (src = 0; src < nproc; src++) {
311 procs[src].p_ps = ps;
312 if (procs[src].p_cb == NULL)
313 procs[src].p_cb = proc_dispatch_null;
315 id = procs[src].p_id;
316 ps->ps_title[id] = procs[src].p_title;
317 ps->ps_ievs[id] = calloc(ps->ps_instances[id],
318 sizeof(struct imsgev));
319 if (ps->ps_ievs[id] == NULL)
320 fatal("%s: calloc", __func__);
322 /* With this set up, we are ready to call imsg_init(). */
323 for (i = 0; i < ps->ps_instances[id]; i++) {
324 ps->ps_ievs[id][i].handler = proc_dispatch;
325 ps->ps_ievs[id][i].events = EV_READ;
326 ps->ps_ievs[id][i].proc = &procs[src];
327 ps->ps_ievs[id][i].data = &ps->ps_ievs[id][i];
332 * Allocate pipes for all process instances (incl. parent)
334 * - ps->ps_pipes: N:M mapping
335 * N source processes connected to M destination processes:
336 * [src][instances][dst][instances], for example
337 * [PROC_RELAY][3][PROC_CA][3]
339 * - ps->ps_pp: per-process 1:M part of ps->ps_pipes
340 * Each process instance has a destination array of socketpair fds:
341 * [dst][instances], for example
344 for (src = 0; src < PROC_MAX; src++) {
345 /* Allocate destination array for each process */
346 ps->ps_pipes[src] = calloc(ps->ps_instances[src],
347 sizeof(struct privsep_pipes));
348 if (ps->ps_pipes[src] == NULL)
349 fatal("%s: calloc", __func__);
351 for (i = 0; i < ps->ps_instances[src]; i++) {
352 pp = &ps->ps_pipes[src][i];
354 for (dst = 0; dst < PROC_MAX; dst++) {
355 /* Allocate maximum fd integers */
357 calloc(ps->ps_instances[dst],
359 if (pp->pp_pipes[dst] == NULL)
360 fatal("%s: calloc", __func__);
362 /* Mark fd as unused */
363 for (j = 0; j < ps->ps_instances[dst]; j++)
364 pp->pp_pipes[dst][j] = -1;
369 ps->ps_pp = &ps->ps_pipes[privsep_process][ps->ps_instance];
373 proc_kill(struct privsep *ps)
379 if (privsep_process != PROC_GOTWEBD)
385 pid = waitpid(WAIT_ANY, &status, 0);
389 if (WIFSIGNALED(status)) {
390 len = asprintf(&cause, "terminated; signal %d",
392 } else if (WIFEXITED(status)) {
393 if (WEXITSTATUS(status) != 0)
394 len = asprintf(&cause, "exited abnormally");
401 /* child exited OK, don't print a warning message */
402 } else if (len != -1) {
403 log_warnx("lost child: pid %u %s", pid, cause);
406 log_warnx("lost child: pid %u", pid);
407 } while (pid != -1 || (pid == -1 && errno == EINTR));
411 proc_open(struct privsep *ps, int src, int dst)
413 struct privsep_pipes *pa, *pb;
414 struct privsep_fd pf;
418 /* Exchange pipes between process. */
419 for (i = 0; i < ps->ps_instances[src]; i++) {
420 for (j = 0; j < ps->ps_instances[dst]; j++) {
421 /* Don't create sockets for ourself. */
422 if (src == dst && i == j)
425 pa = &ps->ps_pipes[src][i];
426 pb = &ps->ps_pipes[dst][j];
427 int sock_flags = SOCK_STREAM | SOCK_NONBLOCK;
429 sock_flags |= SOCK_CLOEXEC;
431 if (socketpair(AF_UNIX, sock_flags,
432 PF_UNSPEC, fds) == -1)
433 fatal("%s: socketpair", __func__);
435 pa->pp_pipes[dst][j] = fds[0];
436 pb->pp_pipes[src][i] = fds[1];
440 if (proc_compose_imsg(ps, dst, j, IMSG_CTL_PROCFD,
441 -1, pb->pp_pipes[src][i], &pf, sizeof(pf)) == -1)
442 fatal("%s: proc_compose_imsg", __func__);
446 if (proc_compose_imsg(ps, src, i, IMSG_CTL_PROCFD,
447 -1, pa->pp_pipes[dst][j], &pf, sizeof(pf)) == -1)
448 fatal("%s: proc_compose_imsg", __func__);
451 * We have to flush to send the descriptors and close
452 * them to avoid the fd ramp on startup.
454 if (proc_flush_imsg(ps, src, i) == -1 ||
455 proc_flush_imsg(ps, dst, j) == -1)
456 fatal("%s: imsg_flush", __func__);
462 proc_close(struct privsep *ps)
465 struct privsep_pipes *pp;
472 for (dst = 0; dst < PROC_MAX; dst++) {
473 if (ps->ps_ievs[dst] == NULL)
476 for (n = 0; n < ps->ps_instances[dst]; n++) {
477 if (pp->pp_pipes[dst][n] == -1)
480 /* Cancel the fd, close and invalidate the fd */
481 event_del(&(ps->ps_ievs[dst][n].ev));
482 imsg_clear(&(ps->ps_ievs[dst][n].ibuf));
483 close(pp->pp_pipes[dst][n]);
484 pp->pp_pipes[dst][n] = -1;
486 free(ps->ps_ievs[dst]);
491 proc_shutdown(struct privsep_proc *p)
493 struct privsep *ps = p->p_ps;
495 if (p->p_shutdown != NULL)
500 log_info("%s, %s exiting, pid %d", getprogname(), p->p_title, getpid());
506 proc_sig_handler(int sig, short event, void *arg)
508 struct privsep_proc *p = arg;
522 fatalx("proc_sig_handler: unexpected signal");
528 proc_run(struct privsep *ps, struct privsep_proc *p,
529 struct privsep_proc *procs, unsigned int nproc,
530 void (*run)(struct privsep *, struct privsep_proc *, void *), void *arg)
535 log_procinit(p->p_title);
537 /* Set the process group of the current process */
540 /* Use non-standard user */
546 /* Change root directory */
547 if (p->p_chroot != NULL)
552 if (chroot(root) == -1)
553 fatal("proc_run: chroot");
554 if (chdir("/") == -1)
555 fatal("proc_run: chdir(\"/\")");
557 privsep_process = p->p_id;
559 setproctitle("%s", p->p_title);
561 if (setgroups(1, &pw->pw_gid) ||
562 setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
563 setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
564 fatal("proc_run: cannot drop privileges");
568 signal_set(&ps->ps_evsigint, SIGINT, proc_sig_handler, p);
569 signal_set(&ps->ps_evsigterm, SIGTERM, proc_sig_handler, p);
570 signal_set(&ps->ps_evsigchld, SIGCHLD, proc_sig_handler, p);
571 signal_set(&ps->ps_evsighup, SIGHUP, proc_sig_handler, p);
572 signal_set(&ps->ps_evsigpipe, SIGPIPE, proc_sig_handler, p);
573 signal_set(&ps->ps_evsigusr1, SIGUSR1, proc_sig_handler, p);
575 signal_add(&ps->ps_evsigint, NULL);
576 signal_add(&ps->ps_evsigterm, NULL);
577 signal_add(&ps->ps_evsigchld, NULL);
578 signal_add(&ps->ps_evsighup, NULL);
579 signal_add(&ps->ps_evsigpipe, NULL);
580 signal_add(&ps->ps_evsigusr1, NULL);
582 proc_setup(ps, procs, nproc);
583 proc_accept(ps, PROC_GOTWEBD_SOCK_FILENO, PROC_GOTWEBD, 0);
585 DPRINTF("%s: %s %d/%d, pid %d", __func__, p->p_title,
586 ps->ps_instance + 1, ps->ps_instances[p->p_id], getpid());
597 proc_dispatch(int fd, short event, void *arg)
599 struct imsgev *iev = arg;
600 struct privsep_proc *p = iev->proc;
601 struct privsep *ps = p->p_ps;
602 struct imsgbuf *ibuf;
607 struct privsep_fd pf;
609 title = ps->ps_title[privsep_process];
612 if (event & EV_READ) {
614 if (n == -1 && errno != EAGAIN)
615 fatal("%s: imsg_read", __func__);
617 /* this pipe is dead, so remove the event handler */
619 event_loopexit(NULL);
624 if (event & EV_WRITE) {
625 n = msgbuf_write(&ibuf->w);
626 if (n == -1 && errno != EAGAIN)
627 fatal("%s: msgbuf_write", __func__);
629 /* this pipe is dead, so remove the event handler */
631 event_loopexit(NULL);
637 n = imsg_get(ibuf, &imsg);
639 fatal("%s: imsg_get", __func__);
644 log_debug("%s: %s %d got imsg %d peerid %d from %s %d",
645 __func__, title, ps->ps_instance + 1,
646 imsg.hdr.type, imsg.hdr.peerid, p->p_title, imsg.hdr.pid);
650 * Check the message with the program callback
652 if ((p->p_cb)(fd, p, &imsg) == 0) {
653 /* Message was handled by the callback, continue */
659 * Generic message handling
661 switch (imsg.hdr.type) {
662 case IMSG_CTL_VERBOSE:
663 log_info("%s", __func__);
664 IMSG_SIZE_CHECK(&imsg, &verbose);
665 memcpy(&verbose, imsg.data, sizeof(verbose));
666 log_setverbose(verbose);
668 case IMSG_CTL_PROCFD:
669 IMSG_SIZE_CHECK(&imsg, &pf);
670 memcpy(&pf, imsg.data, sizeof(pf));
671 proc_accept(ps, imsg.fd, pf.pf_procid,
675 log_warnx("%s: %s %d got invalid imsg %d peerid %d "
677 __func__, title, ps->ps_instance + 1,
678 imsg.hdr.type, imsg.hdr.peerid,
679 p->p_title, imsg.hdr.pid);
687 proc_dispatch_null(int fd, struct privsep_proc *p, struct imsg *imsg)
693 * imsg helper functions
697 imsg_event_add(struct imsgev *iev)
699 if (iev->handler == NULL) {
700 imsg_flush(&iev->ibuf);
704 iev->events = EV_READ;
705 if (iev->ibuf.w.queued)
706 iev->events |= EV_WRITE;
709 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
710 event_add(&iev->ev, NULL);
714 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
715 pid_t pid, int fd, void *data, uint16_t datalen)
719 ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data, datalen);
727 imsg_composev_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
728 pid_t pid, int fd, const struct iovec *iov, int iovcnt)
732 ret = imsg_composev(&iev->ibuf, type, peerid, pid, fd, iov, iovcnt);
740 proc_range(struct privsep *ps, enum privsep_procid id, int *n, int *m)
743 /* Use a range of all target instances */
745 *m = ps->ps_instances[id];
747 /* Use only a single slot of the specified peer process */
753 proc_compose_imsg(struct privsep *ps, enum privsep_procid id, int n,
754 uint16_t type, uint32_t peerid, int fd, void *data, uint16_t datalen)
758 proc_range(ps, id, &n, &m);
760 if (imsg_compose_event(&ps->ps_ievs[id][n],
761 type, peerid, ps->ps_instance + 1, fd, data, datalen) == -1)
769 proc_compose(struct privsep *ps, enum privsep_procid id,
770 uint16_t type, void *data, uint16_t datalen)
772 return (proc_compose_imsg(ps, id, -1, type, -1, -1, data, datalen));
776 proc_composev_imsg(struct privsep *ps, enum privsep_procid id, int n,
777 uint16_t type, uint32_t peerid, int fd, const struct iovec *iov, int iovcnt)
781 proc_range(ps, id, &n, &m);
783 if (imsg_composev_event(&ps->ps_ievs[id][n],
784 type, peerid, ps->ps_instance + 1, fd, iov, iovcnt) == -1)
791 proc_composev(struct privsep *ps, enum privsep_procid id,
792 uint16_t type, const struct iovec *iov, int iovcnt)
794 return (proc_composev_imsg(ps, id, -1, type, -1, -1, iov, iovcnt));
798 proc_forward_imsg(struct privsep *ps, struct imsg *imsg,
799 enum privsep_procid id, int n)
801 return (proc_compose_imsg(ps, id, n, imsg->hdr.type,
802 imsg->hdr.peerid, imsg->fd, imsg->data, IMSG_DATA_SIZE(imsg)));
806 proc_ibuf(struct privsep *ps, enum privsep_procid id, int n)
810 proc_range(ps, id, &n, &m);
811 return (&ps->ps_ievs[id][n].ibuf);
815 proc_iev(struct privsep *ps, enum privsep_procid id, int n)
819 proc_range(ps, id, &n, &m);
820 return (&ps->ps_ievs[id][n]);
823 /* This function should only be called with care as it breaks async I/O */
825 proc_flush_imsg(struct privsep *ps, enum privsep_procid id, int n)
827 struct imsgbuf *ibuf;
830 proc_range(ps, id, &n, &m);
832 ibuf = proc_ibuf(ps, id, n);
836 ret = imsg_flush(ibuf);
837 } while (ret == -1 && errno == EAGAIN);
840 imsg_event_add(&ps->ps_ievs[id][n]);