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.
17 #include "got_compat.h"
19 #include <sys/types.h>
20 #include <sys/queue.h>
21 #include <sys/socket.h>
36 void proc_exec(struct privsep *, struct privsep_proc *, unsigned int,
38 void proc_setup(struct privsep *, struct privsep_proc *, unsigned int);
39 void proc_open(struct privsep *, int, int);
40 void proc_accept(struct privsep *, int, enum privsep_procid,
42 void proc_close(struct privsep *);
43 void proc_shutdown(struct privsep_proc *);
44 void proc_sig_handler(int, short, void *);
45 int proc_dispatch_null(int, struct privsep_proc *, struct imsg *);
47 enum privsep_procid privsep_process;
50 proc_getid(struct privsep_proc *procs, unsigned int nproc,
51 const char *proc_name)
53 struct privsep_proc *p;
56 for (proc = 0; proc < nproc; proc++) {
58 if (strcmp(p->p_title, proc_name))
68 proc_exec(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
69 int argc, char **argv)
71 unsigned int proc, nargc, i, proc_i;
73 struct privsep_proc *p;
77 /* Prepare the new process argv. */
78 nargv = calloc(argc + 5, sizeof(char *));
80 fatal("%s: calloc", __func__);
82 /* Copy call argument first. */
84 nargv[nargc++] = argv[0];
86 /* Set process name argument and save the position. */
87 nargv[nargc] = strdup("-P");
88 if (nargv[nargc] == NULL)
89 fatal("%s: strdup", __func__);
94 /* Point process instance arg to stack and copy the original args. */
95 nargv[nargc] = strdup("-I");
96 if (nargv[nargc] == NULL)
97 fatal("%s: strdup", __func__);
100 for (i = 1; i < (unsigned int) argc; i++)
101 nargv[nargc++] = argv[i];
105 for (proc = 0; proc < nproc; proc++) {
108 /* Update args with process title. */
109 nargv[proc_i] = (char *)(uintptr_t)p->p_title;
111 /* Fire children processes. */
112 for (i = 0; i < ps->ps_instances[p->p_id]; i++) {
113 /* Update the process instance number. */
114 snprintf(num, sizeof(num), "%u", i);
116 fd = ps->ps_pipes[p->p_id][i].pp_pipes[PROC_GOTWEBD][0];
117 ps->ps_pipes[p->p_id][i].pp_pipes[PROC_GOTWEBD][0] = -1;
121 fatal("%s: fork", __func__);
124 /* First create a new session */
128 /* Prepare parent socket. */
129 if (fd != PROC_GOTWEBD_SOCK_FILENO) {
130 if (dup2(fd, PROC_GOTWEBD_SOCK_FILENO)
133 } else if (fcntl(fd, F_SETFD, 0) == -1)
136 execvp(argv[0], nargv);
137 fatal("%s: execvp", __func__);
140 /* Close child end. */
151 proc_connect(struct privsep *ps)
154 unsigned int src, dst, inst;
156 /* Don't distribute any sockets if we are not really going to run. */
160 for (dst = 0; dst < PROC_MAX; dst++) {
161 /* We don't communicate with ourselves. */
162 if (dst == PROC_GOTWEBD)
165 for (inst = 0; inst < ps->ps_instances[dst]; inst++) {
166 iev = &ps->ps_ievs[dst][inst];
167 imsg_init(&iev->ibuf, ps->ps_pp->pp_pipes[dst][inst]);
168 event_set(&iev->ev, iev->ibuf.fd, iev->events,
169 iev->handler, iev->data);
170 event_add(&iev->ev, NULL);
174 /* Distribute the socketpair()s for everyone. */
175 for (src = 0; src < PROC_MAX; src++)
176 for (dst = src; dst < PROC_MAX; dst++) {
177 /* Parent already distributed its fds. */
178 if (src == PROC_GOTWEBD || dst == PROC_GOTWEBD)
181 proc_open(ps, src, dst);
186 proc_init(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
187 int argc, char **argv, enum privsep_procid proc_id)
189 struct privsep_proc *p = NULL;
190 struct privsep_pipes *pa, *pb;
195 /* Don't initiate anything if we are not really going to run. */
199 if (proc_id == PROC_GOTWEBD) {
200 privsep_process = PROC_GOTWEBD;
201 proc_setup(ps, procs, nproc);
204 * Create the children sockets so we can use them
205 * to distribute the rest of the socketpair()s using
206 * proc_connect() later.
208 for (dst = 0; dst < PROC_MAX; dst++) {
209 /* Don't create socket for ourselves. */
210 if (dst == PROC_GOTWEBD)
213 for (proc = 0; proc < ps->ps_instances[dst]; proc++) {
214 pa = &ps->ps_pipes[PROC_GOTWEBD][0];
215 pb = &ps->ps_pipes[dst][proc];
216 int sock_flags = SOCK_STREAM | SOCK_NONBLOCK;
218 sock_flags |= SOCK_CLOEXEC;
220 if (socketpair(AF_UNIX, sock_flags,
221 PF_UNSPEC, fds) == -1)
222 fatal("%s: socketpair", __func__);
224 pa->pp_pipes[dst][proc] = fds[0];
225 pb->pp_pipes[PROC_GOTWEBD][0] = fds[1];
230 proc_exec(ps, procs, nproc, argc, argv);
234 /* Initialize a child */
235 for (proc = 0; proc < nproc; proc++) {
236 if (procs[proc].p_id != proc_id)
241 if (p == NULL || p->p_init == NULL)
242 fatalx("%s: process %d missing process initialization",
247 fatalx("failed to initiate child process");
251 proc_accept(struct privsep *ps, int fd, enum privsep_procid dst,
254 struct privsep_pipes *pp = ps->ps_pp;
257 if (ps->ps_ievs[dst] == NULL) {
259 log_debug("%s: %s src %d %d to dst %d %d not connected",
260 __func__, ps->ps_title[privsep_process],
261 privsep_process, ps->ps_instance + 1,
268 if (pp->pp_pipes[dst][n] != -1) {
269 log_warnx("%s: duplicated descriptor", __func__);
273 pp->pp_pipes[dst][n] = fd;
275 iev = &ps->ps_ievs[dst][n];
276 imsg_init(&iev->ibuf, fd);
277 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
278 event_add(&iev->ev, NULL);
282 proc_setup(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc)
284 unsigned int i, j, src, dst, id;
285 struct privsep_pipes *pp;
287 /* Initialize parent title, ps_instances and procs. */
288 ps->ps_title[PROC_GOTWEBD] = "parent";
290 for (src = 0; src < PROC_MAX; src++)
291 /* Default to 1 process instance */
292 if (ps->ps_instances[src] < 1)
293 ps->ps_instances[src] = 1;
295 for (src = 0; src < nproc; src++) {
296 procs[src].p_ps = ps;
297 if (procs[src].p_cb == NULL)
298 procs[src].p_cb = proc_dispatch_null;
300 id = procs[src].p_id;
301 ps->ps_title[id] = procs[src].p_title;
302 ps->ps_ievs[id] = calloc(ps->ps_instances[id],
303 sizeof(struct imsgev));
304 if (ps->ps_ievs[id] == NULL)
305 fatal("%s: calloc", __func__);
307 /* With this set up, we are ready to call imsg_init(). */
308 for (i = 0; i < ps->ps_instances[id]; i++) {
309 ps->ps_ievs[id][i].handler = proc_dispatch;
310 ps->ps_ievs[id][i].events = EV_READ;
311 ps->ps_ievs[id][i].proc = &procs[src];
312 ps->ps_ievs[id][i].data = &ps->ps_ievs[id][i];
317 * Allocate pipes for all process instances (incl. parent)
319 * - ps->ps_pipes: N:M mapping
320 * N source processes connected to M destination processes:
321 * [src][instances][dst][instances], for example
322 * [PROC_RELAY][3][PROC_CA][3]
324 * - ps->ps_pp: per-process 1:M part of ps->ps_pipes
325 * Each process instance has a destination array of socketpair fds:
326 * [dst][instances], for example
329 for (src = 0; src < PROC_MAX; src++) {
330 /* Allocate destination array for each process */
331 ps->ps_pipes[src] = calloc(ps->ps_instances[src],
332 sizeof(struct privsep_pipes));
333 if (ps->ps_pipes[src] == NULL)
334 fatal("%s: calloc", __func__);
336 for (i = 0; i < ps->ps_instances[src]; i++) {
337 pp = &ps->ps_pipes[src][i];
339 for (dst = 0; dst < PROC_MAX; dst++) {
340 /* Allocate maximum fd integers */
342 calloc(ps->ps_instances[dst],
344 if (pp->pp_pipes[dst] == NULL)
345 fatal("%s: calloc", __func__);
347 /* Mark fd as unused */
348 for (j = 0; j < ps->ps_instances[dst]; j++)
349 pp->pp_pipes[dst][j] = -1;
354 ps->ps_pp = &ps->ps_pipes[privsep_process][ps->ps_instance];
358 proc_kill(struct privsep *ps)
364 if (privsep_process != PROC_GOTWEBD)
370 pid = waitpid(WAIT_ANY, &status, 0);
374 if (WIFSIGNALED(status)) {
375 len = asprintf(&cause, "terminated; signal %d",
377 } else if (WIFEXITED(status)) {
378 if (WEXITSTATUS(status) != 0)
379 len = asprintf(&cause, "exited abnormally");
386 /* child exited OK, don't print a warning message */
387 } else if (len != -1) {
388 log_warnx("lost child: pid %u %s", pid, cause);
391 log_warnx("lost child: pid %u", pid);
392 } while (pid != -1 || (pid == -1 && errno == EINTR));
396 proc_open(struct privsep *ps, int src, int dst)
398 struct privsep_pipes *pa, *pb;
399 struct privsep_fd pf;
403 /* Exchange pipes between process. */
404 for (i = 0; i < ps->ps_instances[src]; i++) {
405 for (j = 0; j < ps->ps_instances[dst]; j++) {
406 /* Don't create sockets for ourself. */
407 if (src == dst && i == j)
410 pa = &ps->ps_pipes[src][i];
411 pb = &ps->ps_pipes[dst][j];
412 int sock_flags = SOCK_STREAM | SOCK_NONBLOCK;
414 sock_flags |= SOCK_CLOEXEC;
416 if (socketpair(AF_UNIX, sock_flags,
417 PF_UNSPEC, fds) == -1)
418 fatal("%s: socketpair", __func__);
420 pa->pp_pipes[dst][j] = fds[0];
421 pb->pp_pipes[src][i] = fds[1];
425 if (proc_compose_imsg(ps, dst, j, IMSG_CTL_PROCFD,
426 -1, pb->pp_pipes[src][i], &pf, sizeof(pf)) == -1)
427 fatal("%s: proc_compose_imsg", __func__);
431 if (proc_compose_imsg(ps, src, i, IMSG_CTL_PROCFD,
432 -1, pa->pp_pipes[dst][j], &pf, sizeof(pf)) == -1)
433 fatal("%s: proc_compose_imsg", __func__);
436 * We have to flush to send the descriptors and close
437 * them to avoid the fd ramp on startup.
439 if (proc_flush_imsg(ps, src, i) == -1 ||
440 proc_flush_imsg(ps, dst, j) == -1)
441 fatal("%s: imsg_flush", __func__);
447 proc_close(struct privsep *ps)
450 struct privsep_pipes *pp;
457 for (dst = 0; dst < PROC_MAX; dst++) {
458 if (ps->ps_ievs[dst] == NULL)
461 for (n = 0; n < ps->ps_instances[dst]; n++) {
462 if (pp->pp_pipes[dst][n] == -1)
465 /* Cancel the fd, close and invalidate the fd */
466 event_del(&(ps->ps_ievs[dst][n].ev));
467 imsg_clear(&(ps->ps_ievs[dst][n].ibuf));
468 close(pp->pp_pipes[dst][n]);
469 pp->pp_pipes[dst][n] = -1;
471 free(ps->ps_ievs[dst]);
476 proc_shutdown(struct privsep_proc *p)
478 struct privsep *ps = p->p_ps;
480 if (p->p_shutdown != NULL)
485 log_info("%s, %s exiting, pid %d", getprogname(), p->p_title, getpid());
491 proc_sig_handler(int sig, short event, void *arg)
493 struct privsep_proc *p = arg;
507 fatalx("proc_sig_handler: unexpected signal");
513 proc_run(struct privsep *ps, struct privsep_proc *p,
514 struct privsep_proc *procs, unsigned int nproc,
515 void (*run)(struct privsep *, struct privsep_proc *, void *), void *arg)
520 log_procinit(p->p_title);
522 /* Set the process group of the current process */
525 /* Use non-standard user */
531 /* Change root directory */
532 if (p->p_chroot != NULL)
537 if (chroot(root) == -1)
538 fatal("proc_run: chroot");
539 if (chdir("/") == -1)
540 fatal("proc_run: chdir(\"/\")");
542 privsep_process = p->p_id;
544 setproctitle("%s", p->p_title);
546 if (setgroups(1, &pw->pw_gid) ||
547 setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
548 setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
549 fatal("proc_run: cannot drop privileges");
553 signal_set(&ps->ps_evsigint, SIGINT, proc_sig_handler, p);
554 signal_set(&ps->ps_evsigterm, SIGTERM, proc_sig_handler, p);
555 signal_set(&ps->ps_evsigchld, SIGCHLD, proc_sig_handler, p);
556 signal_set(&ps->ps_evsighup, SIGHUP, proc_sig_handler, p);
557 signal_set(&ps->ps_evsigpipe, SIGPIPE, proc_sig_handler, p);
558 signal_set(&ps->ps_evsigusr1, SIGUSR1, proc_sig_handler, p);
560 signal_add(&ps->ps_evsigint, NULL);
561 signal_add(&ps->ps_evsigterm, NULL);
562 signal_add(&ps->ps_evsigchld, NULL);
563 signal_add(&ps->ps_evsighup, NULL);
564 signal_add(&ps->ps_evsigpipe, NULL);
565 signal_add(&ps->ps_evsigusr1, NULL);
567 proc_setup(ps, procs, nproc);
568 proc_accept(ps, PROC_GOTWEBD_SOCK_FILENO, PROC_GOTWEBD, 0);
570 DPRINTF("%s: %s %d/%d, pid %d", __func__, p->p_title,
571 ps->ps_instance + 1, ps->ps_instances[p->p_id], getpid());
582 proc_dispatch(int fd, short event, void *arg)
584 struct imsgev *iev = arg;
585 struct privsep_proc *p = iev->proc;
586 struct privsep *ps = p->p_ps;
587 struct imsgbuf *ibuf;
592 struct privsep_fd pf;
594 title = ps->ps_title[privsep_process];
597 if (event & EV_READ) {
599 if (n == -1 && errno != EAGAIN)
600 fatal("%s: imsg_read", __func__);
602 /* this pipe is dead, so remove the event handler */
604 event_loopexit(NULL);
609 if (event & EV_WRITE) {
610 n = msgbuf_write(&ibuf->w);
611 if (n == -1 && errno != EAGAIN)
612 fatal("%s: msgbuf_write", __func__);
614 /* this pipe is dead, so remove the event handler */
616 event_loopexit(NULL);
622 n = imsg_get(ibuf, &imsg);
624 fatal("%s: imsg_get", __func__);
629 log_debug("%s: %s %d got imsg %d peerid %d from %s %d",
630 __func__, title, ps->ps_instance + 1,
631 imsg.hdr.type, imsg.hdr.peerid, p->p_title, imsg.hdr.pid);
635 * Check the message with the program callback
637 if ((p->p_cb)(fd, p, &imsg) == 0) {
638 /* Message was handled by the callback, continue */
644 * Generic message handling
646 switch (imsg.hdr.type) {
647 case IMSG_CTL_VERBOSE:
648 log_info("%s", __func__);
649 IMSG_SIZE_CHECK(&imsg, &verbose);
650 memcpy(&verbose, imsg.data, sizeof(verbose));
651 log_setverbose(verbose);
653 case IMSG_CTL_PROCFD:
654 IMSG_SIZE_CHECK(&imsg, &pf);
655 memcpy(&pf, imsg.data, sizeof(pf));
656 proc_accept(ps, imsg.fd, pf.pf_procid,
660 log_warnx("%s: %s %d got invalid imsg %d peerid %d "
662 __func__, title, ps->ps_instance + 1,
663 imsg.hdr.type, imsg.hdr.peerid,
664 p->p_title, imsg.hdr.pid);
672 proc_dispatch_null(int fd, struct privsep_proc *p, struct imsg *imsg)
678 * imsg helper functions
682 imsg_event_add(struct imsgev *iev)
684 if (iev->handler == NULL) {
685 imsg_flush(&iev->ibuf);
689 iev->events = EV_READ;
690 if (iev->ibuf.w.queued)
691 iev->events |= EV_WRITE;
694 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
695 event_add(&iev->ev, NULL);
699 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
700 pid_t pid, int fd, void *data, uint16_t datalen)
704 ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data, datalen);
712 imsg_composev_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
713 pid_t pid, int fd, const struct iovec *iov, int iovcnt)
717 ret = imsg_composev(&iev->ibuf, type, peerid, pid, fd, iov, iovcnt);
725 proc_range(struct privsep *ps, enum privsep_procid id, int *n, int *m)
728 /* Use a range of all target instances */
730 *m = ps->ps_instances[id];
732 /* Use only a single slot of the specified peer process */
738 proc_compose_imsg(struct privsep *ps, enum privsep_procid id, int n,
739 uint16_t type, uint32_t peerid, int fd, void *data, uint16_t datalen)
743 proc_range(ps, id, &n, &m);
745 if (imsg_compose_event(&ps->ps_ievs[id][n],
746 type, peerid, ps->ps_instance + 1, fd, data, datalen) == -1)
754 proc_compose(struct privsep *ps, enum privsep_procid id,
755 uint16_t type, void *data, uint16_t datalen)
757 return (proc_compose_imsg(ps, id, -1, type, -1, -1, data, datalen));
761 proc_composev_imsg(struct privsep *ps, enum privsep_procid id, int n,
762 uint16_t type, uint32_t peerid, int fd, const struct iovec *iov, int iovcnt)
766 proc_range(ps, id, &n, &m);
768 if (imsg_composev_event(&ps->ps_ievs[id][n],
769 type, peerid, ps->ps_instance + 1, fd, iov, iovcnt) == -1)
776 proc_composev(struct privsep *ps, enum privsep_procid id,
777 uint16_t type, const struct iovec *iov, int iovcnt)
779 return (proc_composev_imsg(ps, id, -1, type, -1, -1, iov, iovcnt));
783 proc_forward_imsg(struct privsep *ps, struct imsg *imsg,
784 enum privsep_procid id, int n)
786 return (proc_compose_imsg(ps, id, n, imsg->hdr.type,
787 imsg->hdr.peerid, imsg->fd, imsg->data, IMSG_DATA_SIZE(imsg)));
791 proc_ibuf(struct privsep *ps, enum privsep_procid id, int n)
795 proc_range(ps, id, &n, &m);
796 return (&ps->ps_ievs[id][n].ibuf);
800 proc_iev(struct privsep *ps, enum privsep_procid id, int n)
804 proc_range(ps, id, &n, &m);
805 return (&ps->ps_ievs[id][n]);
808 /* This function should only be called with care as it breaks async I/O */
810 proc_flush_imsg(struct privsep *ps, enum privsep_procid id, int n)
812 struct imsgbuf *ibuf;
815 proc_range(ps, id, &n, &m);
817 ibuf = proc_ibuf(ps, id, n);
821 ret = imsg_flush(ibuf);
822 } while (ret == -1 && errno == EAGAIN);
825 imsg_event_add(&ps->ps_ievs[id][n]);