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>
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 if (socketpair(AF_UNIX,
217 SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC,
218 PF_UNSPEC, fds) == -1)
219 fatal("%s: socketpair", __func__);
221 pa->pp_pipes[dst][proc] = fds[0];
222 pb->pp_pipes[PROC_GOTWEBD][0] = fds[1];
227 proc_exec(ps, procs, nproc, argc, argv);
231 /* Initialize a child */
232 for (proc = 0; proc < nproc; proc++) {
233 if (procs[proc].p_id != proc_id)
238 if (p == NULL || p->p_init == NULL)
239 fatalx("%s: process %d missing process initialization",
244 fatalx("failed to initiate child process");
248 proc_accept(struct privsep *ps, int fd, enum privsep_procid dst,
251 struct privsep_pipes *pp = ps->ps_pp;
254 if (ps->ps_ievs[dst] == NULL) {
256 log_debug("%s: %s src %d %d to dst %d %d not connected",
257 __func__, ps->ps_title[privsep_process],
258 privsep_process, ps->ps_instance + 1,
265 if (pp->pp_pipes[dst][n] != -1) {
266 log_warnx("%s: duplicated descriptor", __func__);
270 pp->pp_pipes[dst][n] = fd;
272 iev = &ps->ps_ievs[dst][n];
273 imsg_init(&iev->ibuf, fd);
274 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
275 event_add(&iev->ev, NULL);
279 proc_setup(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc)
281 unsigned int i, j, src, dst, id;
282 struct privsep_pipes *pp;
284 /* Initialize parent title, ps_instances and procs. */
285 ps->ps_title[PROC_GOTWEBD] = "parent";
287 for (src = 0; src < PROC_MAX; src++)
288 /* Default to 1 process instance */
289 if (ps->ps_instances[src] < 1)
290 ps->ps_instances[src] = 1;
292 for (src = 0; src < nproc; src++) {
293 procs[src].p_ps = ps;
294 if (procs[src].p_cb == NULL)
295 procs[src].p_cb = proc_dispatch_null;
297 id = procs[src].p_id;
298 ps->ps_title[id] = procs[src].p_title;
299 ps->ps_ievs[id] = calloc(ps->ps_instances[id],
300 sizeof(struct imsgev));
301 if (ps->ps_ievs[id] == NULL)
302 fatal("%s: calloc", __func__);
304 /* With this set up, we are ready to call imsg_init(). */
305 for (i = 0; i < ps->ps_instances[id]; i++) {
306 ps->ps_ievs[id][i].handler = proc_dispatch;
307 ps->ps_ievs[id][i].events = EV_READ;
308 ps->ps_ievs[id][i].proc = &procs[src];
309 ps->ps_ievs[id][i].data = &ps->ps_ievs[id][i];
314 * Allocate pipes for all process instances (incl. parent)
316 * - ps->ps_pipes: N:M mapping
317 * N source processes connected to M destination processes:
318 * [src][instances][dst][instances], for example
319 * [PROC_RELAY][3][PROC_CA][3]
321 * - ps->ps_pp: per-process 1:M part of ps->ps_pipes
322 * Each process instance has a destination array of socketpair fds:
323 * [dst][instances], for example
326 for (src = 0; src < PROC_MAX; src++) {
327 /* Allocate destination array for each process */
328 ps->ps_pipes[src] = calloc(ps->ps_instances[src],
329 sizeof(struct privsep_pipes));
330 if (ps->ps_pipes[src] == NULL)
331 fatal("%s: calloc", __func__);
333 for (i = 0; i < ps->ps_instances[src]; i++) {
334 pp = &ps->ps_pipes[src][i];
336 for (dst = 0; dst < PROC_MAX; dst++) {
337 /* Allocate maximum fd integers */
339 calloc(ps->ps_instances[dst],
341 if (pp->pp_pipes[dst] == NULL)
342 fatal("%s: calloc", __func__);
344 /* Mark fd as unused */
345 for (j = 0; j < ps->ps_instances[dst]; j++)
346 pp->pp_pipes[dst][j] = -1;
351 ps->ps_pp = &ps->ps_pipes[privsep_process][ps->ps_instance];
355 proc_kill(struct privsep *ps)
361 if (privsep_process != PROC_GOTWEBD)
367 pid = waitpid(WAIT_ANY, &status, 0);
371 if (WIFSIGNALED(status)) {
372 len = asprintf(&cause, "terminated; signal %d",
374 } else if (WIFEXITED(status)) {
375 if (WEXITSTATUS(status) != 0)
376 len = asprintf(&cause, "exited abnormally");
383 /* child exited OK, don't print a warning message */
384 } else if (len != -1) {
385 log_warnx("lost child: pid %u %s", pid, cause);
388 log_warnx("lost child: pid %u", pid);
389 } while (pid != -1 || (pid == -1 && errno == EINTR));
393 proc_open(struct privsep *ps, int src, int dst)
395 struct privsep_pipes *pa, *pb;
396 struct privsep_fd pf;
400 /* Exchange pipes between process. */
401 for (i = 0; i < ps->ps_instances[src]; i++) {
402 for (j = 0; j < ps->ps_instances[dst]; j++) {
403 /* Don't create sockets for ourself. */
404 if (src == dst && i == j)
407 pa = &ps->ps_pipes[src][i];
408 pb = &ps->ps_pipes[dst][j];
409 if (socketpair(AF_UNIX,
410 SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC,
411 PF_UNSPEC, fds) == -1)
412 fatal("%s: socketpair", __func__);
414 pa->pp_pipes[dst][j] = fds[0];
415 pb->pp_pipes[src][i] = fds[1];
419 if (proc_compose_imsg(ps, dst, j, IMSG_CTL_PROCFD,
420 -1, pb->pp_pipes[src][i], &pf, sizeof(pf)) == -1)
421 fatal("%s: proc_compose_imsg", __func__);
425 if (proc_compose_imsg(ps, src, i, IMSG_CTL_PROCFD,
426 -1, pa->pp_pipes[dst][j], &pf, sizeof(pf)) == -1)
427 fatal("%s: proc_compose_imsg", __func__);
430 * We have to flush to send the descriptors and close
431 * them to avoid the fd ramp on startup.
433 if (proc_flush_imsg(ps, src, i) == -1 ||
434 proc_flush_imsg(ps, dst, j) == -1)
435 fatal("%s: imsg_flush", __func__);
441 proc_close(struct privsep *ps)
444 struct privsep_pipes *pp;
451 for (dst = 0; dst < PROC_MAX; dst++) {
452 if (ps->ps_ievs[dst] == NULL)
455 for (n = 0; n < ps->ps_instances[dst]; n++) {
456 if (pp->pp_pipes[dst][n] == -1)
459 /* Cancel the fd, close and invalidate the fd */
460 event_del(&(ps->ps_ievs[dst][n].ev));
461 imsg_clear(&(ps->ps_ievs[dst][n].ibuf));
462 close(pp->pp_pipes[dst][n]);
463 pp->pp_pipes[dst][n] = -1;
465 free(ps->ps_ievs[dst]);
470 proc_shutdown(struct privsep_proc *p)
472 struct privsep *ps = p->p_ps;
474 if (p->p_shutdown != NULL)
479 log_info("%s, %s exiting, pid %d", getprogname(), p->p_title, getpid());
485 proc_sig_handler(int sig, short event, void *arg)
487 struct privsep_proc *p = arg;
501 fatalx("proc_sig_handler: unexpected signal");
507 proc_run(struct privsep *ps, struct privsep_proc *p,
508 struct privsep_proc *procs, unsigned int nproc,
509 void (*run)(struct privsep *, struct privsep_proc *, void *), void *arg)
514 log_procinit(p->p_title);
516 /* Set the process group of the current process */
519 /* Use non-standard user */
525 /* Change root directory */
526 if (p->p_chroot != NULL)
531 if (chroot(root) == -1)
532 fatal("proc_run: chroot");
533 if (chdir("/") == -1)
534 fatal("proc_run: chdir(\"/\")");
536 privsep_process = p->p_id;
538 setproctitle("%s", p->p_title);
540 if (setgroups(1, &pw->pw_gid) ||
541 setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
542 setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
543 fatal("proc_run: cannot drop privileges");
547 signal_set(&ps->ps_evsigint, SIGINT, proc_sig_handler, p);
548 signal_set(&ps->ps_evsigterm, SIGTERM, proc_sig_handler, p);
549 signal_set(&ps->ps_evsigchld, SIGCHLD, proc_sig_handler, p);
550 signal_set(&ps->ps_evsighup, SIGHUP, proc_sig_handler, p);
551 signal_set(&ps->ps_evsigpipe, SIGPIPE, proc_sig_handler, p);
552 signal_set(&ps->ps_evsigusr1, SIGUSR1, proc_sig_handler, p);
554 signal_add(&ps->ps_evsigint, NULL);
555 signal_add(&ps->ps_evsigterm, NULL);
556 signal_add(&ps->ps_evsigchld, NULL);
557 signal_add(&ps->ps_evsighup, NULL);
558 signal_add(&ps->ps_evsigpipe, NULL);
559 signal_add(&ps->ps_evsigusr1, NULL);
561 proc_setup(ps, procs, nproc);
562 proc_accept(ps, PROC_GOTWEBD_SOCK_FILENO, PROC_GOTWEBD, 0);
564 DPRINTF("%s: %s %d/%d, pid %d", __func__, p->p_title,
565 ps->ps_instance + 1, ps->ps_instances[p->p_id], getpid());
576 proc_dispatch(int fd, short event, void *arg)
578 struct imsgev *iev = arg;
579 struct privsep_proc *p = iev->proc;
580 struct privsep *ps = p->p_ps;
581 struct imsgbuf *ibuf;
586 struct privsep_fd pf;
588 title = ps->ps_title[privsep_process];
591 if (event & EV_READ) {
593 if (n == -1 && errno != EAGAIN)
594 fatal("%s: imsg_read", __func__);
596 /* this pipe is dead, so remove the event handler */
598 event_loopexit(NULL);
603 if (event & EV_WRITE) {
604 n = msgbuf_write(&ibuf->w);
605 if (n == -1 && errno != EAGAIN)
606 fatal("%s: msgbuf_write", __func__);
608 /* this pipe is dead, so remove the event handler */
610 event_loopexit(NULL);
616 n = imsg_get(ibuf, &imsg);
618 fatal("%s: imsg_get", __func__);
623 log_debug("%s: %s %d got imsg %d peerid %d from %s %d",
624 __func__, title, ps->ps_instance + 1,
625 imsg.hdr.type, imsg.hdr.peerid, p->p_title, imsg.hdr.pid);
629 * Check the message with the program callback
631 if ((p->p_cb)(fd, p, &imsg) == 0) {
632 /* Message was handled by the callback, continue */
638 * Generic message handling
640 switch (imsg.hdr.type) {
641 case IMSG_CTL_VERBOSE:
642 log_info("%s", __func__);
643 IMSG_SIZE_CHECK(&imsg, &verbose);
644 memcpy(&verbose, imsg.data, sizeof(verbose));
645 log_setverbose(verbose);
647 case IMSG_CTL_PROCFD:
648 IMSG_SIZE_CHECK(&imsg, &pf);
649 memcpy(&pf, imsg.data, sizeof(pf));
650 proc_accept(ps, imsg.fd, pf.pf_procid,
654 log_warnx("%s: %s %d got invalid imsg %d peerid %d "
656 __func__, title, ps->ps_instance + 1,
657 imsg.hdr.type, imsg.hdr.peerid,
658 p->p_title, imsg.hdr.pid);
666 proc_dispatch_null(int fd, struct privsep_proc *p, struct imsg *imsg)
672 * imsg helper functions
676 imsg_event_add(struct imsgev *iev)
678 if (iev->handler == NULL) {
679 imsg_flush(&iev->ibuf);
683 iev->events = EV_READ;
684 if (iev->ibuf.w.queued)
685 iev->events |= EV_WRITE;
688 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
689 event_add(&iev->ev, NULL);
693 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
694 pid_t pid, int fd, void *data, uint16_t datalen)
698 ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data, datalen);
706 imsg_composev_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
707 pid_t pid, int fd, const struct iovec *iov, int iovcnt)
711 ret = imsg_composev(&iev->ibuf, type, peerid, pid, fd, iov, iovcnt);
719 proc_range(struct privsep *ps, enum privsep_procid id, int *n, int *m)
722 /* Use a range of all target instances */
724 *m = ps->ps_instances[id];
726 /* Use only a single slot of the specified peer process */
732 proc_compose_imsg(struct privsep *ps, enum privsep_procid id, int n,
733 uint16_t type, uint32_t peerid, int fd, void *data, uint16_t datalen)
737 proc_range(ps, id, &n, &m);
739 if (imsg_compose_event(&ps->ps_ievs[id][n],
740 type, peerid, ps->ps_instance + 1, fd, data, datalen) == -1)
748 proc_compose(struct privsep *ps, enum privsep_procid id,
749 uint16_t type, void *data, uint16_t datalen)
751 return (proc_compose_imsg(ps, id, -1, type, -1, -1, data, datalen));
755 proc_composev_imsg(struct privsep *ps, enum privsep_procid id, int n,
756 uint16_t type, uint32_t peerid, int fd, const struct iovec *iov, int iovcnt)
760 proc_range(ps, id, &n, &m);
762 if (imsg_composev_event(&ps->ps_ievs[id][n],
763 type, peerid, ps->ps_instance + 1, fd, iov, iovcnt) == -1)
770 proc_composev(struct privsep *ps, enum privsep_procid id,
771 uint16_t type, const struct iovec *iov, int iovcnt)
773 return (proc_composev_imsg(ps, id, -1, type, -1, -1, iov, iovcnt));
777 proc_forward_imsg(struct privsep *ps, struct imsg *imsg,
778 enum privsep_procid id, int n)
780 return (proc_compose_imsg(ps, id, n, imsg->hdr.type,
781 imsg->hdr.peerid, imsg->fd, imsg->data, IMSG_DATA_SIZE(imsg)));
785 proc_ibuf(struct privsep *ps, enum privsep_procid id, int n)
789 proc_range(ps, id, &n, &m);
790 return (&ps->ps_ievs[id][n].ibuf);
794 proc_iev(struct privsep *ps, enum privsep_procid id, int n)
798 proc_range(ps, id, &n, &m);
799 return (&ps->ps_ievs[id][n]);
802 /* This function should only be called with care as it breaks async I/O */
804 proc_flush_imsg(struct privsep *ps, enum privsep_procid id, int n)
806 struct imsgbuf *ibuf;
809 proc_range(ps, id, &n, &m);
811 ibuf = proc_ibuf(ps, id, n);
815 ret = imsg_flush(ibuf);
816 } while (ret == -1 && errno == EAGAIN);
819 imsg_event_add(&ps->ps_ievs[id][n]);