2 56324ebe 2022-07-14 thomas * Copyright (c) 2010 - 2016 Reyk Floeter <reyk@openbsd.org>
3 56324ebe 2022-07-14 thomas * Copyright (c) 2008 Pierre-Yves Ritschard <pyr@openbsd.org>
5 56324ebe 2022-07-14 thomas * Permission to use, copy, modify, and distribute this software for any
6 56324ebe 2022-07-14 thomas * purpose with or without fee is hereby granted, provided that the above
7 56324ebe 2022-07-14 thomas * copyright notice and this permission notice appear in all copies.
9 56324ebe 2022-07-14 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 56324ebe 2022-07-14 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 56324ebe 2022-07-14 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 56324ebe 2022-07-14 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 56324ebe 2022-07-14 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 56324ebe 2022-07-14 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 56324ebe 2022-07-14 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 56324ebe 2022-07-14 thomas #include <sys/types.h>
19 56324ebe 2022-07-14 thomas #include <sys/socket.h>
20 56324ebe 2022-07-14 thomas #include <sys/wait.h>
22 d7cad54e 2022-07-14 thomas /* TA: FIXME */
23 d7cad54e 2022-07-14 thomas #include <grp.h>
25 56324ebe 2022-07-14 thomas #include <fcntl.h>
26 56324ebe 2022-07-14 thomas #include <stdio.h>
27 56324ebe 2022-07-14 thomas #include <stdlib.h>
28 56324ebe 2022-07-14 thomas #include <unistd.h>
29 56324ebe 2022-07-14 thomas #include <string.h>
30 56324ebe 2022-07-14 thomas #include <errno.h>
31 56324ebe 2022-07-14 thomas #include <signal.h>
32 56324ebe 2022-07-14 thomas #include <pwd.h>
33 56324ebe 2022-07-14 thomas #include <event.h>
35 56324ebe 2022-07-14 thomas #include "proc.h"
37 d7cad54e 2022-07-14 thomas #include "got_compat.h"
39 56324ebe 2022-07-14 thomas void proc_exec(struct privsep *, struct privsep_proc *, unsigned int,
40 56324ebe 2022-07-14 thomas int, char **);
41 56324ebe 2022-07-14 thomas void proc_setup(struct privsep *, struct privsep_proc *, unsigned int);
42 56324ebe 2022-07-14 thomas void proc_open(struct privsep *, int, int);
43 56324ebe 2022-07-14 thomas void proc_accept(struct privsep *, int, enum privsep_procid,
44 56324ebe 2022-07-14 thomas unsigned int);
45 56324ebe 2022-07-14 thomas void proc_close(struct privsep *);
46 56324ebe 2022-07-14 thomas int proc_ispeer(struct privsep_proc *, unsigned int, enum privsep_procid);
47 56324ebe 2022-07-14 thomas void proc_shutdown(struct privsep_proc *);
48 56324ebe 2022-07-14 thomas void proc_sig_handler(int, short, void *);
49 56324ebe 2022-07-14 thomas int proc_dispatch_null(int, struct privsep_proc *, struct imsg *);
51 56324ebe 2022-07-14 thomas enum privsep_procid privsep_process;
54 56324ebe 2022-07-14 thomas proc_ispeer(struct privsep_proc *procs, unsigned int nproc,
55 56324ebe 2022-07-14 thomas enum privsep_procid type)
57 56324ebe 2022-07-14 thomas unsigned int i;
59 56324ebe 2022-07-14 thomas for (i = 0; i < nproc; i++)
60 56324ebe 2022-07-14 thomas if (procs[i].p_id == type)
61 56324ebe 2022-07-14 thomas return (1);
63 56324ebe 2022-07-14 thomas return (0);
66 56324ebe 2022-07-14 thomas enum privsep_procid
67 56324ebe 2022-07-14 thomas proc_getid(struct privsep_proc *procs, unsigned int nproc,
68 56324ebe 2022-07-14 thomas const char *proc_name)
70 56324ebe 2022-07-14 thomas struct privsep_proc *p;
71 56324ebe 2022-07-14 thomas unsigned int proc;
73 56324ebe 2022-07-14 thomas for (proc = 0; proc < nproc; proc++) {
74 56324ebe 2022-07-14 thomas p = &procs[proc];
75 56324ebe 2022-07-14 thomas if (strcmp(p->p_title, proc_name))
78 56324ebe 2022-07-14 thomas return (p->p_id);
81 56324ebe 2022-07-14 thomas return (PROC_MAX);
85 56324ebe 2022-07-14 thomas proc_exec(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
86 56324ebe 2022-07-14 thomas int argc, char **argv)
88 56324ebe 2022-07-14 thomas unsigned int proc, nargc, i, proc_i;
89 56324ebe 2022-07-14 thomas char **nargv;
90 56324ebe 2022-07-14 thomas struct privsep_proc *p;
91 56324ebe 2022-07-14 thomas char num[32];
94 56324ebe 2022-07-14 thomas /* Prepare the new process argv. */
95 56324ebe 2022-07-14 thomas nargv = calloc(argc + 5, sizeof(char *));
96 56324ebe 2022-07-14 thomas if (nargv == NULL)
97 56324ebe 2022-07-14 thomas fatal("%s: calloc", __func__);
99 56324ebe 2022-07-14 thomas /* Copy call argument first. */
100 56324ebe 2022-07-14 thomas nargc = 0;
101 56324ebe 2022-07-14 thomas nargv[nargc++] = argv[0];
103 56324ebe 2022-07-14 thomas /* Set process name argument and save the position. */
104 56324ebe 2022-07-14 thomas nargv[nargc] = strdup("-P");
105 56324ebe 2022-07-14 thomas if (nargv[nargc] == NULL)
106 56324ebe 2022-07-14 thomas fatal("%s: strdup", __func__);
108 56324ebe 2022-07-14 thomas proc_i = nargc;
111 56324ebe 2022-07-14 thomas /* Point process instance arg to stack and copy the original args. */
112 56324ebe 2022-07-14 thomas nargv[nargc] = strdup("-I");
113 56324ebe 2022-07-14 thomas if (nargv[nargc] == NULL)
114 56324ebe 2022-07-14 thomas fatal("%s: strdup", __func__);
116 56324ebe 2022-07-14 thomas nargv[nargc++] = num;
117 56324ebe 2022-07-14 thomas for (i = 1; i < (unsigned int) argc; i++)
118 56324ebe 2022-07-14 thomas nargv[nargc++] = argv[i];
120 56324ebe 2022-07-14 thomas nargv[nargc] = NULL;
122 56324ebe 2022-07-14 thomas for (proc = 0; proc < nproc; proc++) {
123 56324ebe 2022-07-14 thomas p = &procs[proc];
125 56324ebe 2022-07-14 thomas /* Update args with process title. */
126 56324ebe 2022-07-14 thomas nargv[proc_i] = (char *)(uintptr_t)p->p_title;
128 56324ebe 2022-07-14 thomas /* Fire children processes. */
129 56324ebe 2022-07-14 thomas for (i = 0; i < ps->ps_instances[p->p_id]; i++) {
130 56324ebe 2022-07-14 thomas /* Update the process instance number. */
131 56324ebe 2022-07-14 thomas snprintf(num, sizeof(num), "%u", i);
133 56324ebe 2022-07-14 thomas fd = ps->ps_pipes[p->p_id][i].pp_pipes[PROC_GOTWEBD][0];
134 56324ebe 2022-07-14 thomas ps->ps_pipes[p->p_id][i].pp_pipes[PROC_GOTWEBD][0] = -1;
136 56324ebe 2022-07-14 thomas switch (fork()) {
138 56324ebe 2022-07-14 thomas fatal("%s: fork", __func__);
141 56324ebe 2022-07-14 thomas /* First create a new session */
142 56324ebe 2022-07-14 thomas if (setsid() == -1)
143 56324ebe 2022-07-14 thomas fatal("setsid");
145 56324ebe 2022-07-14 thomas /* Prepare parent socket. */
146 56324ebe 2022-07-14 thomas if (fd != PROC_GOTWEBD_SOCK_FILENO) {
147 56324ebe 2022-07-14 thomas if (dup2(fd, PROC_GOTWEBD_SOCK_FILENO)
149 56324ebe 2022-07-14 thomas fatal("dup2");
150 56324ebe 2022-07-14 thomas } else if (fcntl(fd, F_SETFD, 0) == -1)
151 56324ebe 2022-07-14 thomas fatal("fcntl");
153 56324ebe 2022-07-14 thomas execvp(argv[0], nargv);
154 56324ebe 2022-07-14 thomas fatal("%s: execvp", __func__);
157 56324ebe 2022-07-14 thomas /* Close child end. */
158 56324ebe 2022-07-14 thomas close(fd);
164 56324ebe 2022-07-14 thomas free(nargv);
168 56324ebe 2022-07-14 thomas proc_connect(struct privsep *ps)
170 56324ebe 2022-07-14 thomas struct imsgev *iev;
171 56324ebe 2022-07-14 thomas unsigned int src, dst, inst;
173 56324ebe 2022-07-14 thomas /* Don't distribute any sockets if we are not really going to run. */
174 56324ebe 2022-07-14 thomas if (ps->ps_noaction)
177 56324ebe 2022-07-14 thomas for (dst = 0; dst < PROC_MAX; dst++) {
178 56324ebe 2022-07-14 thomas /* We don't communicate with ourselves. */
179 56324ebe 2022-07-14 thomas if (dst == PROC_GOTWEBD)
180 56324ebe 2022-07-14 thomas continue;
182 56324ebe 2022-07-14 thomas for (inst = 0; inst < ps->ps_instances[dst]; inst++) {
183 56324ebe 2022-07-14 thomas iev = &ps->ps_ievs[dst][inst];
184 56324ebe 2022-07-14 thomas imsg_init(&iev->ibuf, ps->ps_pp->pp_pipes[dst][inst]);
185 56324ebe 2022-07-14 thomas event_set(&iev->ev, iev->ibuf.fd, iev->events,
186 56324ebe 2022-07-14 thomas iev->handler, iev->data);
187 56324ebe 2022-07-14 thomas event_add(&iev->ev, NULL);
191 56324ebe 2022-07-14 thomas /* Distribute the socketpair()s for everyone. */
192 56324ebe 2022-07-14 thomas for (src = 0; src < PROC_MAX; src++)
193 56324ebe 2022-07-14 thomas for (dst = src; dst < PROC_MAX; dst++) {
194 56324ebe 2022-07-14 thomas /* Parent already distributed its fds. */
195 56324ebe 2022-07-14 thomas if (src == PROC_GOTWEBD || dst == PROC_GOTWEBD)
196 56324ebe 2022-07-14 thomas continue;
198 56324ebe 2022-07-14 thomas proc_open(ps, src, dst);
203 56324ebe 2022-07-14 thomas proc_init(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
204 56324ebe 2022-07-14 thomas int argc, char **argv, enum privsep_procid proc_id)
206 56324ebe 2022-07-14 thomas struct privsep_proc *p = NULL;
207 56324ebe 2022-07-14 thomas struct privsep_pipes *pa, *pb;
208 56324ebe 2022-07-14 thomas unsigned int proc;
209 56324ebe 2022-07-14 thomas unsigned int dst;
210 56324ebe 2022-07-14 thomas int fds[2];
212 56324ebe 2022-07-14 thomas /* Don't initiate anything if we are not really going to run. */
213 56324ebe 2022-07-14 thomas if (ps->ps_noaction)
216 56324ebe 2022-07-14 thomas if (proc_id == PROC_GOTWEBD) {
217 56324ebe 2022-07-14 thomas privsep_process = PROC_GOTWEBD;
218 56324ebe 2022-07-14 thomas proc_setup(ps, procs, nproc);
221 56324ebe 2022-07-14 thomas * Create the children sockets so we can use them
222 56324ebe 2022-07-14 thomas * to distribute the rest of the socketpair()s using
223 56324ebe 2022-07-14 thomas * proc_connect() later.
225 56324ebe 2022-07-14 thomas for (dst = 0; dst < PROC_MAX; dst++) {
226 56324ebe 2022-07-14 thomas /* Don't create socket for ourselves. */
227 56324ebe 2022-07-14 thomas if (dst == PROC_GOTWEBD)
228 56324ebe 2022-07-14 thomas continue;
230 56324ebe 2022-07-14 thomas for (proc = 0; proc < ps->ps_instances[dst]; proc++) {
231 56324ebe 2022-07-14 thomas pa = &ps->ps_pipes[PROC_GOTWEBD][0];
232 56324ebe 2022-07-14 thomas pb = &ps->ps_pipes[dst][proc];
233 56324ebe 2022-07-14 thomas if (socketpair(AF_UNIX,
234 56324ebe 2022-07-14 thomas SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC,
235 56324ebe 2022-07-14 thomas PF_UNSPEC, fds) == -1)
236 56324ebe 2022-07-14 thomas fatal("%s: socketpair", __func__);
238 56324ebe 2022-07-14 thomas pa->pp_pipes[dst][proc] = fds[0];
239 56324ebe 2022-07-14 thomas pb->pp_pipes[PROC_GOTWEBD][0] = fds[1];
243 56324ebe 2022-07-14 thomas /* Engage! */
244 56324ebe 2022-07-14 thomas proc_exec(ps, procs, nproc, argc, argv);
248 56324ebe 2022-07-14 thomas /* Initialize a child */
249 56324ebe 2022-07-14 thomas for (proc = 0; proc < nproc; proc++) {
250 56324ebe 2022-07-14 thomas if (procs[proc].p_id != proc_id)
251 56324ebe 2022-07-14 thomas continue;
252 56324ebe 2022-07-14 thomas p = &procs[proc];
255 56324ebe 2022-07-14 thomas if (p == NULL || p->p_init == NULL)
256 56324ebe 2022-07-14 thomas fatalx("%s: process %d missing process initialization",
257 56324ebe 2022-07-14 thomas __func__, proc_id);
259 56324ebe 2022-07-14 thomas p->p_init(ps, p);
261 56324ebe 2022-07-14 thomas fatalx("failed to initiate child process");
265 56324ebe 2022-07-14 thomas proc_accept(struct privsep *ps, int fd, enum privsep_procid dst,
266 56324ebe 2022-07-14 thomas unsigned int n)
268 56324ebe 2022-07-14 thomas struct privsep_pipes *pp = ps->ps_pp;
269 56324ebe 2022-07-14 thomas struct imsgev *iev;
271 56324ebe 2022-07-14 thomas if (ps->ps_ievs[dst] == NULL) {
272 56324ebe 2022-07-14 thomas #if DEBUG > 1
273 56324ebe 2022-07-14 thomas log_debug("%s: %s src %d %d to dst %d %d not connected",
274 56324ebe 2022-07-14 thomas __func__, ps->ps_title[privsep_process],
275 56324ebe 2022-07-14 thomas privsep_process, ps->ps_instance + 1,
276 56324ebe 2022-07-14 thomas dst, n + 1);
278 56324ebe 2022-07-14 thomas close(fd);
282 56324ebe 2022-07-14 thomas if (pp->pp_pipes[dst][n] != -1) {
283 56324ebe 2022-07-14 thomas log_warnx("%s: duplicated descriptor", __func__);
284 56324ebe 2022-07-14 thomas close(fd);
287 56324ebe 2022-07-14 thomas pp->pp_pipes[dst][n] = fd;
289 56324ebe 2022-07-14 thomas iev = &ps->ps_ievs[dst][n];
290 56324ebe 2022-07-14 thomas imsg_init(&iev->ibuf, fd);
291 56324ebe 2022-07-14 thomas event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
292 56324ebe 2022-07-14 thomas event_add(&iev->ev, NULL);
296 56324ebe 2022-07-14 thomas proc_setup(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc)
298 56324ebe 2022-07-14 thomas unsigned int i, j, src, dst, id;
299 56324ebe 2022-07-14 thomas struct privsep_pipes *pp;
301 56324ebe 2022-07-14 thomas /* Initialize parent title, ps_instances and procs. */
302 56324ebe 2022-07-14 thomas ps->ps_title[PROC_GOTWEBD] = "parent";
304 56324ebe 2022-07-14 thomas for (src = 0; src < PROC_MAX; src++)
305 56324ebe 2022-07-14 thomas /* Default to 1 process instance */
306 56324ebe 2022-07-14 thomas if (ps->ps_instances[src] < 1)
307 56324ebe 2022-07-14 thomas ps->ps_instances[src] = 1;
309 56324ebe 2022-07-14 thomas for (src = 0; src < nproc; src++) {
310 56324ebe 2022-07-14 thomas procs[src].p_ps = ps;
311 56324ebe 2022-07-14 thomas if (procs[src].p_cb == NULL)
312 56324ebe 2022-07-14 thomas procs[src].p_cb = proc_dispatch_null;
314 56324ebe 2022-07-14 thomas id = procs[src].p_id;
315 56324ebe 2022-07-14 thomas ps->ps_title[id] = procs[src].p_title;
316 56324ebe 2022-07-14 thomas ps->ps_ievs[id] = calloc(ps->ps_instances[id],
317 56324ebe 2022-07-14 thomas sizeof(struct imsgev));
318 56324ebe 2022-07-14 thomas if (ps->ps_ievs[id] == NULL)
319 56324ebe 2022-07-14 thomas fatal("%s: calloc", __func__);
321 56324ebe 2022-07-14 thomas /* With this set up, we are ready to call imsg_init(). */
322 56324ebe 2022-07-14 thomas for (i = 0; i < ps->ps_instances[id]; i++) {
323 56324ebe 2022-07-14 thomas ps->ps_ievs[id][i].handler = proc_dispatch;
324 56324ebe 2022-07-14 thomas ps->ps_ievs[id][i].events = EV_READ;
325 56324ebe 2022-07-14 thomas ps->ps_ievs[id][i].proc = &procs[src];
326 56324ebe 2022-07-14 thomas ps->ps_ievs[id][i].data = &ps->ps_ievs[id][i];
331 56324ebe 2022-07-14 thomas * Allocate pipes for all process instances (incl. parent)
333 56324ebe 2022-07-14 thomas * - ps->ps_pipes: N:M mapping
334 56324ebe 2022-07-14 thomas * N source processes connected to M destination processes:
335 56324ebe 2022-07-14 thomas * [src][instances][dst][instances], for example
336 56324ebe 2022-07-14 thomas * [PROC_RELAY][3][PROC_CA][3]
338 56324ebe 2022-07-14 thomas * - ps->ps_pp: per-process 1:M part of ps->ps_pipes
339 56324ebe 2022-07-14 thomas * Each process instance has a destination array of socketpair fds:
340 56324ebe 2022-07-14 thomas * [dst][instances], for example
341 56324ebe 2022-07-14 thomas * [PROC_GOTWEBD][0]
343 56324ebe 2022-07-14 thomas for (src = 0; src < PROC_MAX; src++) {
344 56324ebe 2022-07-14 thomas /* Allocate destination array for each process */
345 56324ebe 2022-07-14 thomas ps->ps_pipes[src] = calloc(ps->ps_instances[src],
346 56324ebe 2022-07-14 thomas sizeof(struct privsep_pipes));
347 56324ebe 2022-07-14 thomas if (ps->ps_pipes[src] == NULL)
348 56324ebe 2022-07-14 thomas fatal("%s: calloc", __func__);
350 56324ebe 2022-07-14 thomas for (i = 0; i < ps->ps_instances[src]; i++) {
351 56324ebe 2022-07-14 thomas pp = &ps->ps_pipes[src][i];
353 56324ebe 2022-07-14 thomas for (dst = 0; dst < PROC_MAX; dst++) {
354 56324ebe 2022-07-14 thomas /* Allocate maximum fd integers */
355 56324ebe 2022-07-14 thomas pp->pp_pipes[dst] =
356 56324ebe 2022-07-14 thomas calloc(ps->ps_instances[dst],
357 56324ebe 2022-07-14 thomas sizeof(int));
358 56324ebe 2022-07-14 thomas if (pp->pp_pipes[dst] == NULL)
359 56324ebe 2022-07-14 thomas fatal("%s: calloc", __func__);
361 56324ebe 2022-07-14 thomas /* Mark fd as unused */
362 56324ebe 2022-07-14 thomas for (j = 0; j < ps->ps_instances[dst]; j++)
363 56324ebe 2022-07-14 thomas pp->pp_pipes[dst][j] = -1;
368 56324ebe 2022-07-14 thomas ps->ps_pp = &ps->ps_pipes[privsep_process][ps->ps_instance];
372 56324ebe 2022-07-14 thomas proc_kill(struct privsep *ps)
374 56324ebe 2022-07-14 thomas char *cause;
375 56324ebe 2022-07-14 thomas pid_t pid;
376 56324ebe 2022-07-14 thomas int len, status;
378 56324ebe 2022-07-14 thomas if (privsep_process != PROC_GOTWEBD)
381 56324ebe 2022-07-14 thomas proc_close(ps);
384 56324ebe 2022-07-14 thomas pid = waitpid(WAIT_ANY, &status, 0);
385 56324ebe 2022-07-14 thomas if (pid <= 0)
386 56324ebe 2022-07-14 thomas continue;
388 56324ebe 2022-07-14 thomas if (WIFSIGNALED(status)) {
389 56324ebe 2022-07-14 thomas len = asprintf(&cause, "terminated; signal %d",
390 56324ebe 2022-07-14 thomas WTERMSIG(status));
391 56324ebe 2022-07-14 thomas } else if (WIFEXITED(status)) {
392 56324ebe 2022-07-14 thomas if (WEXITSTATUS(status) != 0)
393 56324ebe 2022-07-14 thomas len = asprintf(&cause, "exited abnormally");
397 56324ebe 2022-07-14 thomas len = -1;
399 56324ebe 2022-07-14 thomas if (len == 0) {
400 56324ebe 2022-07-14 thomas /* child exited OK, don't print a warning message */
401 56324ebe 2022-07-14 thomas } else if (len != -1) {
402 56324ebe 2022-07-14 thomas log_warnx("lost child: pid %u %s", pid, cause);
403 56324ebe 2022-07-14 thomas free(cause);
405 56324ebe 2022-07-14 thomas log_warnx("lost child: pid %u", pid);
406 56324ebe 2022-07-14 thomas } while (pid != -1 || (pid == -1 && errno == EINTR));
410 56324ebe 2022-07-14 thomas proc_open(struct privsep *ps, int src, int dst)
412 56324ebe 2022-07-14 thomas struct privsep_pipes *pa, *pb;
413 56324ebe 2022-07-14 thomas struct privsep_fd pf;
414 56324ebe 2022-07-14 thomas int fds[2];
415 56324ebe 2022-07-14 thomas unsigned int i, j;
417 56324ebe 2022-07-14 thomas /* Exchange pipes between process. */
418 56324ebe 2022-07-14 thomas for (i = 0; i < ps->ps_instances[src]; i++) {
419 56324ebe 2022-07-14 thomas for (j = 0; j < ps->ps_instances[dst]; j++) {
420 56324ebe 2022-07-14 thomas /* Don't create sockets for ourself. */
421 56324ebe 2022-07-14 thomas if (src == dst && i == j)
422 56324ebe 2022-07-14 thomas continue;
424 56324ebe 2022-07-14 thomas pa = &ps->ps_pipes[src][i];
425 56324ebe 2022-07-14 thomas pb = &ps->ps_pipes[dst][j];
426 56324ebe 2022-07-14 thomas if (socketpair(AF_UNIX,
427 56324ebe 2022-07-14 thomas SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC,
428 56324ebe 2022-07-14 thomas PF_UNSPEC, fds) == -1)
429 56324ebe 2022-07-14 thomas fatal("%s: socketpair", __func__);
431 56324ebe 2022-07-14 thomas pa->pp_pipes[dst][j] = fds[0];
432 56324ebe 2022-07-14 thomas pb->pp_pipes[src][i] = fds[1];
434 56324ebe 2022-07-14 thomas pf.pf_procid = src;
435 56324ebe 2022-07-14 thomas pf.pf_instance = i;
436 56324ebe 2022-07-14 thomas if (proc_compose_imsg(ps, dst, j, IMSG_CTL_PROCFD,
437 56324ebe 2022-07-14 thomas -1, pb->pp_pipes[src][i], &pf, sizeof(pf)) == -1)
438 56324ebe 2022-07-14 thomas fatal("%s: proc_compose_imsg", __func__);
440 56324ebe 2022-07-14 thomas pf.pf_procid = dst;
441 56324ebe 2022-07-14 thomas pf.pf_instance = j;
442 56324ebe 2022-07-14 thomas if (proc_compose_imsg(ps, src, i, IMSG_CTL_PROCFD,
443 56324ebe 2022-07-14 thomas -1, pa->pp_pipes[dst][j], &pf, sizeof(pf)) == -1)
444 56324ebe 2022-07-14 thomas fatal("%s: proc_compose_imsg", __func__);
447 56324ebe 2022-07-14 thomas * We have to flush to send the descriptors and close
448 56324ebe 2022-07-14 thomas * them to avoid the fd ramp on startup.
450 56324ebe 2022-07-14 thomas if (proc_flush_imsg(ps, src, i) == -1 ||
451 56324ebe 2022-07-14 thomas proc_flush_imsg(ps, dst, j) == -1)
452 56324ebe 2022-07-14 thomas fatal("%s: imsg_flush", __func__);
458 56324ebe 2022-07-14 thomas proc_close(struct privsep *ps)
460 56324ebe 2022-07-14 thomas unsigned int dst, n;
461 56324ebe 2022-07-14 thomas struct privsep_pipes *pp;
463 56324ebe 2022-07-14 thomas if (ps == NULL)
466 56324ebe 2022-07-14 thomas pp = ps->ps_pp;
468 56324ebe 2022-07-14 thomas for (dst = 0; dst < PROC_MAX; dst++) {
469 56324ebe 2022-07-14 thomas if (ps->ps_ievs[dst] == NULL)
470 56324ebe 2022-07-14 thomas continue;
472 56324ebe 2022-07-14 thomas for (n = 0; n < ps->ps_instances[dst]; n++) {
473 56324ebe 2022-07-14 thomas if (pp->pp_pipes[dst][n] == -1)
474 56324ebe 2022-07-14 thomas continue;
476 56324ebe 2022-07-14 thomas /* Cancel the fd, close and invalidate the fd */
477 56324ebe 2022-07-14 thomas event_del(&(ps->ps_ievs[dst][n].ev));
478 56324ebe 2022-07-14 thomas imsg_clear(&(ps->ps_ievs[dst][n].ibuf));
479 56324ebe 2022-07-14 thomas close(pp->pp_pipes[dst][n]);
480 56324ebe 2022-07-14 thomas pp->pp_pipes[dst][n] = -1;
482 56324ebe 2022-07-14 thomas free(ps->ps_ievs[dst]);
487 56324ebe 2022-07-14 thomas proc_shutdown(struct privsep_proc *p)
489 56324ebe 2022-07-14 thomas struct privsep *ps = p->p_ps;
491 56324ebe 2022-07-14 thomas if (p->p_shutdown != NULL)
492 56324ebe 2022-07-14 thomas (*p->p_shutdown)();
494 56324ebe 2022-07-14 thomas proc_close(ps);
496 56324ebe 2022-07-14 thomas log_info("%s, %s exiting, pid %d", getprogname(), p->p_title, getpid());
502 56324ebe 2022-07-14 thomas proc_sig_handler(int sig, short event, void *arg)
504 56324ebe 2022-07-14 thomas struct privsep_proc *p = arg;
506 56324ebe 2022-07-14 thomas switch (sig) {
507 56324ebe 2022-07-14 thomas case SIGINT:
508 56324ebe 2022-07-14 thomas case SIGTERM:
509 56324ebe 2022-07-14 thomas proc_shutdown(p);
511 56324ebe 2022-07-14 thomas case SIGCHLD:
512 56324ebe 2022-07-14 thomas case SIGHUP:
513 56324ebe 2022-07-14 thomas case SIGPIPE:
514 56324ebe 2022-07-14 thomas case SIGUSR1:
515 56324ebe 2022-07-14 thomas /* ignore */
518 56324ebe 2022-07-14 thomas fatalx("proc_sig_handler: unexpected signal");
519 56324ebe 2022-07-14 thomas /* NOTREACHED */
524 56324ebe 2022-07-14 thomas proc_run(struct privsep *ps, struct privsep_proc *p,
525 56324ebe 2022-07-14 thomas struct privsep_proc *procs, unsigned int nproc,
526 56324ebe 2022-07-14 thomas void (*run)(struct privsep *, struct privsep_proc *, void *), void *arg)
528 56324ebe 2022-07-14 thomas struct passwd *pw;
529 56324ebe 2022-07-14 thomas const char *root;
531 56324ebe 2022-07-14 thomas log_procinit(p->p_title);
533 56324ebe 2022-07-14 thomas /* Set the process group of the current process */
534 56324ebe 2022-07-14 thomas setpgid(0, 0);
536 56324ebe 2022-07-14 thomas /* Use non-standard user */
537 56324ebe 2022-07-14 thomas if (p->p_pw != NULL)
538 56324ebe 2022-07-14 thomas pw = p->p_pw;
540 56324ebe 2022-07-14 thomas pw = ps->ps_pw;
542 56324ebe 2022-07-14 thomas /* Change root directory */
543 56324ebe 2022-07-14 thomas if (p->p_chroot != NULL)
544 56324ebe 2022-07-14 thomas root = p->p_chroot;
546 56324ebe 2022-07-14 thomas root = pw->pw_dir;
548 56324ebe 2022-07-14 thomas if (chroot(root) == -1)
549 56324ebe 2022-07-14 thomas fatal("proc_run: chroot");
550 56324ebe 2022-07-14 thomas if (chdir("/") == -1)
551 56324ebe 2022-07-14 thomas fatal("proc_run: chdir(\"/\")");
553 56324ebe 2022-07-14 thomas privsep_process = p->p_id;
555 56324ebe 2022-07-14 thomas setproctitle("%s", p->p_title);
557 56324ebe 2022-07-14 thomas if (setgroups(1, &pw->pw_gid) ||
558 56324ebe 2022-07-14 thomas setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
559 56324ebe 2022-07-14 thomas setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
560 56324ebe 2022-07-14 thomas fatal("proc_run: cannot drop privileges");
562 56324ebe 2022-07-14 thomas event_init();
564 56324ebe 2022-07-14 thomas signal_set(&ps->ps_evsigint, SIGINT, proc_sig_handler, p);
565 56324ebe 2022-07-14 thomas signal_set(&ps->ps_evsigterm, SIGTERM, proc_sig_handler, p);
566 56324ebe 2022-07-14 thomas signal_set(&ps->ps_evsigchld, SIGCHLD, proc_sig_handler, p);
567 56324ebe 2022-07-14 thomas signal_set(&ps->ps_evsighup, SIGHUP, proc_sig_handler, p);
568 56324ebe 2022-07-14 thomas signal_set(&ps->ps_evsigpipe, SIGPIPE, proc_sig_handler, p);
569 56324ebe 2022-07-14 thomas signal_set(&ps->ps_evsigusr1, SIGUSR1, proc_sig_handler, p);
571 56324ebe 2022-07-14 thomas signal_add(&ps->ps_evsigint, NULL);
572 56324ebe 2022-07-14 thomas signal_add(&ps->ps_evsigterm, NULL);
573 56324ebe 2022-07-14 thomas signal_add(&ps->ps_evsigchld, NULL);
574 56324ebe 2022-07-14 thomas signal_add(&ps->ps_evsighup, NULL);
575 56324ebe 2022-07-14 thomas signal_add(&ps->ps_evsigpipe, NULL);
576 56324ebe 2022-07-14 thomas signal_add(&ps->ps_evsigusr1, NULL);
578 56324ebe 2022-07-14 thomas proc_setup(ps, procs, nproc);
579 56324ebe 2022-07-14 thomas proc_accept(ps, PROC_GOTWEBD_SOCK_FILENO, PROC_GOTWEBD, 0);
581 56324ebe 2022-07-14 thomas DPRINTF("%s: %s %d/%d, pid %d", __func__, p->p_title,
582 56324ebe 2022-07-14 thomas ps->ps_instance + 1, ps->ps_instances[p->p_id], getpid());
584 56324ebe 2022-07-14 thomas if (run != NULL)
585 56324ebe 2022-07-14 thomas run(ps, p, arg);
587 56324ebe 2022-07-14 thomas event_dispatch();
589 56324ebe 2022-07-14 thomas proc_shutdown(p);
593 56324ebe 2022-07-14 thomas proc_dispatch(int fd, short event, void *arg)
595 56324ebe 2022-07-14 thomas struct imsgev *iev = arg;
596 56324ebe 2022-07-14 thomas struct privsep_proc *p = iev->proc;
597 56324ebe 2022-07-14 thomas struct privsep *ps = p->p_ps;
598 56324ebe 2022-07-14 thomas struct imsgbuf *ibuf;
599 56324ebe 2022-07-14 thomas struct imsg imsg;
600 56324ebe 2022-07-14 thomas ssize_t n;
601 56324ebe 2022-07-14 thomas int verbose;
602 56324ebe 2022-07-14 thomas const char *title;
603 56324ebe 2022-07-14 thomas struct privsep_fd pf;
605 56324ebe 2022-07-14 thomas title = ps->ps_title[privsep_process];
606 56324ebe 2022-07-14 thomas ibuf = &iev->ibuf;
608 56324ebe 2022-07-14 thomas if (event & EV_READ) {
609 56324ebe 2022-07-14 thomas n = imsg_read(ibuf);
610 56324ebe 2022-07-14 thomas if (n == -1 && errno != EAGAIN)
611 56324ebe 2022-07-14 thomas fatal("%s: imsg_read", __func__);
612 56324ebe 2022-07-14 thomas if (n == 0) {
613 56324ebe 2022-07-14 thomas /* this pipe is dead, so remove the event handler */
614 56324ebe 2022-07-14 thomas event_del(&iev->ev);
615 56324ebe 2022-07-14 thomas event_loopexit(NULL);
620 56324ebe 2022-07-14 thomas if (event & EV_WRITE) {
621 56324ebe 2022-07-14 thomas n = msgbuf_write(&ibuf->w);
622 56324ebe 2022-07-14 thomas if (n == -1 && errno != EAGAIN)
623 56324ebe 2022-07-14 thomas fatal("%s: msgbuf_write", __func__);
624 56324ebe 2022-07-14 thomas if (n == 0) {
625 56324ebe 2022-07-14 thomas /* this pipe is dead, so remove the event handler */
626 56324ebe 2022-07-14 thomas event_del(&iev->ev);
627 56324ebe 2022-07-14 thomas event_loopexit(NULL);
632 56324ebe 2022-07-14 thomas for (;;) {
633 56324ebe 2022-07-14 thomas n = imsg_get(ibuf, &imsg);
634 56324ebe 2022-07-14 thomas if (n == -1)
635 56324ebe 2022-07-14 thomas fatal("%s: imsg_get", __func__);
636 56324ebe 2022-07-14 thomas if (n == 0)
639 56324ebe 2022-07-14 thomas #if DEBUG > 1
640 56324ebe 2022-07-14 thomas log_debug("%s: %s %d got imsg %d peerid %d from %s %d",
641 56324ebe 2022-07-14 thomas __func__, title, ps->ps_instance + 1,
642 56324ebe 2022-07-14 thomas imsg.hdr.type, imsg.hdr.peerid, p->p_title, imsg.hdr.pid);
646 56324ebe 2022-07-14 thomas * Check the message with the program callback
648 56324ebe 2022-07-14 thomas if ((p->p_cb)(fd, p, &imsg) == 0) {
649 56324ebe 2022-07-14 thomas /* Message was handled by the callback, continue */
650 56324ebe 2022-07-14 thomas imsg_free(&imsg);
651 56324ebe 2022-07-14 thomas continue;
655 56324ebe 2022-07-14 thomas * Generic message handling
657 56324ebe 2022-07-14 thomas switch (imsg.hdr.type) {
658 56324ebe 2022-07-14 thomas case IMSG_CTL_VERBOSE:
659 56324ebe 2022-07-14 thomas log_info("%s", __func__);
660 56324ebe 2022-07-14 thomas IMSG_SIZE_CHECK(&imsg, &verbose);
661 56324ebe 2022-07-14 thomas memcpy(&verbose, imsg.data, sizeof(verbose));
662 56324ebe 2022-07-14 thomas log_setverbose(verbose);
664 56324ebe 2022-07-14 thomas case IMSG_CTL_PROCFD:
665 56324ebe 2022-07-14 thomas IMSG_SIZE_CHECK(&imsg, &pf);
666 56324ebe 2022-07-14 thomas memcpy(&pf, imsg.data, sizeof(pf));
667 56324ebe 2022-07-14 thomas proc_accept(ps, imsg.fd, pf.pf_procid,
668 56324ebe 2022-07-14 thomas pf.pf_instance);
671 56324ebe 2022-07-14 thomas log_warnx("%s: %s %d got invalid imsg %d peerid %d "
672 56324ebe 2022-07-14 thomas "from %s %d",
673 56324ebe 2022-07-14 thomas __func__, title, ps->ps_instance + 1,
674 56324ebe 2022-07-14 thomas imsg.hdr.type, imsg.hdr.peerid,
675 56324ebe 2022-07-14 thomas p->p_title, imsg.hdr.pid);
677 56324ebe 2022-07-14 thomas imsg_free(&imsg);
679 56324ebe 2022-07-14 thomas imsg_event_add(iev);
683 56324ebe 2022-07-14 thomas proc_dispatch_null(int fd, struct privsep_proc *p, struct imsg *imsg)
685 56324ebe 2022-07-14 thomas return (-1);
689 56324ebe 2022-07-14 thomas * imsg helper functions
693 56324ebe 2022-07-14 thomas imsg_event_add(struct imsgev *iev)
695 56324ebe 2022-07-14 thomas if (iev->handler == NULL) {
696 56324ebe 2022-07-14 thomas imsg_flush(&iev->ibuf);
700 56324ebe 2022-07-14 thomas iev->events = EV_READ;
701 56324ebe 2022-07-14 thomas if (iev->ibuf.w.queued)
702 56324ebe 2022-07-14 thomas iev->events |= EV_WRITE;
704 56324ebe 2022-07-14 thomas event_del(&iev->ev);
705 56324ebe 2022-07-14 thomas event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
706 56324ebe 2022-07-14 thomas event_add(&iev->ev, NULL);
710 56324ebe 2022-07-14 thomas imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
711 56324ebe 2022-07-14 thomas pid_t pid, int fd, void *data, uint16_t datalen)
715 56324ebe 2022-07-14 thomas ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data, datalen);
716 56324ebe 2022-07-14 thomas if (ret == -1)
717 56324ebe 2022-07-14 thomas return (ret);
718 56324ebe 2022-07-14 thomas imsg_event_add(iev);
719 56324ebe 2022-07-14 thomas return (ret);
723 56324ebe 2022-07-14 thomas imsg_composev_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
724 56324ebe 2022-07-14 thomas pid_t pid, int fd, const struct iovec *iov, int iovcnt)
728 56324ebe 2022-07-14 thomas ret = imsg_composev(&iev->ibuf, type, peerid, pid, fd, iov, iovcnt);
729 56324ebe 2022-07-14 thomas if (ret == -1)
730 56324ebe 2022-07-14 thomas return (ret);
731 56324ebe 2022-07-14 thomas imsg_event_add(iev);
732 56324ebe 2022-07-14 thomas return (ret);
736 56324ebe 2022-07-14 thomas proc_range(struct privsep *ps, enum privsep_procid id, int *n, int *m)
738 56324ebe 2022-07-14 thomas if (*n == -1) {
739 56324ebe 2022-07-14 thomas /* Use a range of all target instances */
741 56324ebe 2022-07-14 thomas *m = ps->ps_instances[id];
743 56324ebe 2022-07-14 thomas /* Use only a single slot of the specified peer process */
744 56324ebe 2022-07-14 thomas *m = *n + 1;
749 56324ebe 2022-07-14 thomas proc_compose_imsg(struct privsep *ps, enum privsep_procid id, int n,
750 56324ebe 2022-07-14 thomas uint16_t type, uint32_t peerid, int fd, void *data, uint16_t datalen)
754 56324ebe 2022-07-14 thomas proc_range(ps, id, &n, &m);
755 56324ebe 2022-07-14 thomas for (; n < m; n++) {
756 56324ebe 2022-07-14 thomas if (imsg_compose_event(&ps->ps_ievs[id][n],
757 56324ebe 2022-07-14 thomas type, peerid, ps->ps_instance + 1, fd, data, datalen) == -1)
758 56324ebe 2022-07-14 thomas return (-1);
761 56324ebe 2022-07-14 thomas return (0);
765 56324ebe 2022-07-14 thomas proc_compose(struct privsep *ps, enum privsep_procid id,
766 56324ebe 2022-07-14 thomas uint16_t type, void *data, uint16_t datalen)
768 56324ebe 2022-07-14 thomas return (proc_compose_imsg(ps, id, -1, type, -1, -1, data, datalen));
772 56324ebe 2022-07-14 thomas proc_composev_imsg(struct privsep *ps, enum privsep_procid id, int n,
773 56324ebe 2022-07-14 thomas uint16_t type, uint32_t peerid, int fd, const struct iovec *iov, int iovcnt)
777 56324ebe 2022-07-14 thomas proc_range(ps, id, &n, &m);
778 56324ebe 2022-07-14 thomas for (; n < m; n++)
779 56324ebe 2022-07-14 thomas if (imsg_composev_event(&ps->ps_ievs[id][n],
780 56324ebe 2022-07-14 thomas type, peerid, ps->ps_instance + 1, fd, iov, iovcnt) == -1)
781 56324ebe 2022-07-14 thomas return (-1);
783 56324ebe 2022-07-14 thomas return (0);
787 56324ebe 2022-07-14 thomas proc_composev(struct privsep *ps, enum privsep_procid id,
788 56324ebe 2022-07-14 thomas uint16_t type, const struct iovec *iov, int iovcnt)
790 56324ebe 2022-07-14 thomas return (proc_composev_imsg(ps, id, -1, type, -1, -1, iov, iovcnt));
794 56324ebe 2022-07-14 thomas proc_forward_imsg(struct privsep *ps, struct imsg *imsg,
795 56324ebe 2022-07-14 thomas enum privsep_procid id, int n)
797 56324ebe 2022-07-14 thomas return (proc_compose_imsg(ps, id, n, imsg->hdr.type,
798 56324ebe 2022-07-14 thomas imsg->hdr.peerid, imsg->fd, imsg->data, IMSG_DATA_SIZE(imsg)));
801 56324ebe 2022-07-14 thomas struct imsgbuf *
802 56324ebe 2022-07-14 thomas proc_ibuf(struct privsep *ps, enum privsep_procid id, int n)
806 56324ebe 2022-07-14 thomas proc_range(ps, id, &n, &m);
807 56324ebe 2022-07-14 thomas return (&ps->ps_ievs[id][n].ibuf);
810 56324ebe 2022-07-14 thomas struct imsgev *
811 56324ebe 2022-07-14 thomas proc_iev(struct privsep *ps, enum privsep_procid id, int n)
815 56324ebe 2022-07-14 thomas proc_range(ps, id, &n, &m);
816 56324ebe 2022-07-14 thomas return (&ps->ps_ievs[id][n]);
819 56324ebe 2022-07-14 thomas /* This function should only be called with care as it breaks async I/O */
821 56324ebe 2022-07-14 thomas proc_flush_imsg(struct privsep *ps, enum privsep_procid id, int n)
823 56324ebe 2022-07-14 thomas struct imsgbuf *ibuf;
824 56324ebe 2022-07-14 thomas int m, ret = 0;
826 56324ebe 2022-07-14 thomas proc_range(ps, id, &n, &m);
827 56324ebe 2022-07-14 thomas for (; n < m; n++) {
828 56324ebe 2022-07-14 thomas ibuf = proc_ibuf(ps, id, n);
829 56324ebe 2022-07-14 thomas if (ibuf == NULL)
830 56324ebe 2022-07-14 thomas return (-1);
832 56324ebe 2022-07-14 thomas ret = imsg_flush(ibuf);
833 56324ebe 2022-07-14 thomas } while (ret == -1 && errno == EAGAIN);
834 56324ebe 2022-07-14 thomas if (ret == -1)
836 56324ebe 2022-07-14 thomas imsg_event_add(&ps->ps_ievs[id][n]);
839 56324ebe 2022-07-14 thomas return (ret);