2 a596b957 2022-07-14 tracey * Copyright (c) 2010 - 2016 Reyk Floeter <reyk@openbsd.org>
3 a596b957 2022-07-14 tracey * Copyright (c) 2008 Pierre-Yves Ritschard <pyr@openbsd.org>
5 a596b957 2022-07-14 tracey * Permission to use, copy, modify, and distribute this software for any
6 a596b957 2022-07-14 tracey * purpose with or without fee is hereby granted, provided that the above
7 a596b957 2022-07-14 tracey * copyright notice and this permission notice appear in all copies.
9 a596b957 2022-07-14 tracey * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 a596b957 2022-07-14 tracey * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 a596b957 2022-07-14 tracey * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 a596b957 2022-07-14 tracey * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 a596b957 2022-07-14 tracey * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 a596b957 2022-07-14 tracey * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 a596b957 2022-07-14 tracey * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 a596b957 2022-07-14 tracey #include <sys/types.h>
19 a596b957 2022-07-14 tracey #include <sys/queue.h>
20 a596b957 2022-07-14 tracey #include <sys/socket.h>
21 a596b957 2022-07-14 tracey #include <sys/wait.h>
23 a596b957 2022-07-14 tracey #include <fcntl.h>
24 a596b957 2022-07-14 tracey #include <stdio.h>
25 a596b957 2022-07-14 tracey #include <stdlib.h>
26 a596b957 2022-07-14 tracey #include <unistd.h>
27 a596b957 2022-07-14 tracey #include <string.h>
28 a596b957 2022-07-14 tracey #include <errno.h>
29 a596b957 2022-07-14 tracey #include <signal.h>
30 a596b957 2022-07-14 tracey #include <pwd.h>
31 a596b957 2022-07-14 tracey #include <event.h>
32 a596b957 2022-07-14 tracey #include <imsg.h>
34 a596b957 2022-07-14 tracey #include "proc.h"
36 a596b957 2022-07-14 tracey void proc_exec(struct privsep *, struct privsep_proc *, unsigned int,
37 a596b957 2022-07-14 tracey int, char **);
38 a596b957 2022-07-14 tracey void proc_setup(struct privsep *, struct privsep_proc *, unsigned int);
39 a596b957 2022-07-14 tracey void proc_open(struct privsep *, int, int);
40 a596b957 2022-07-14 tracey void proc_accept(struct privsep *, int, enum privsep_procid,
41 a596b957 2022-07-14 tracey unsigned int);
42 a596b957 2022-07-14 tracey void proc_close(struct privsep *);
43 a596b957 2022-07-14 tracey int proc_ispeer(struct privsep_proc *, unsigned int, enum privsep_procid);
44 a596b957 2022-07-14 tracey void proc_shutdown(struct privsep_proc *);
45 a596b957 2022-07-14 tracey void proc_sig_handler(int, short, void *);
46 a596b957 2022-07-14 tracey int proc_dispatch_null(int, struct privsep_proc *, struct imsg *);
48 a596b957 2022-07-14 tracey enum privsep_procid privsep_process;
51 a596b957 2022-07-14 tracey proc_ispeer(struct privsep_proc *procs, unsigned int nproc,
52 a596b957 2022-07-14 tracey enum privsep_procid type)
54 a596b957 2022-07-14 tracey unsigned int i;
56 a596b957 2022-07-14 tracey for (i = 0; i < nproc; i++)
57 a596b957 2022-07-14 tracey if (procs[i].p_id == type)
58 a596b957 2022-07-14 tracey return (1);
60 a596b957 2022-07-14 tracey return (0);
63 a596b957 2022-07-14 tracey enum privsep_procid
64 a596b957 2022-07-14 tracey proc_getid(struct privsep_proc *procs, unsigned int nproc,
65 a596b957 2022-07-14 tracey const char *proc_name)
67 a596b957 2022-07-14 tracey struct privsep_proc *p;
68 a596b957 2022-07-14 tracey unsigned int proc;
70 a596b957 2022-07-14 tracey for (proc = 0; proc < nproc; proc++) {
71 a596b957 2022-07-14 tracey p = &procs[proc];
72 a596b957 2022-07-14 tracey if (strcmp(p->p_title, proc_name))
75 a596b957 2022-07-14 tracey return (p->p_id);
78 a596b957 2022-07-14 tracey return (PROC_MAX);
82 a596b957 2022-07-14 tracey proc_exec(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
83 a596b957 2022-07-14 tracey int argc, char **argv)
85 a596b957 2022-07-14 tracey unsigned int proc, nargc, i, proc_i;
86 a596b957 2022-07-14 tracey char **nargv;
87 a596b957 2022-07-14 tracey struct privsep_proc *p;
88 a596b957 2022-07-14 tracey char num[32];
91 a596b957 2022-07-14 tracey /* Prepare the new process argv. */
92 a596b957 2022-07-14 tracey nargv = calloc(argc + 5, sizeof(char *));
93 a596b957 2022-07-14 tracey if (nargv == NULL)
94 a596b957 2022-07-14 tracey fatal("%s: calloc", __func__);
96 a596b957 2022-07-14 tracey /* Copy call argument first. */
97 a596b957 2022-07-14 tracey nargc = 0;
98 a596b957 2022-07-14 tracey nargv[nargc++] = argv[0];
100 a596b957 2022-07-14 tracey /* Set process name argument and save the position. */
101 a596b957 2022-07-14 tracey nargv[nargc] = strdup("-P");
102 a596b957 2022-07-14 tracey if (nargv[nargc] == NULL)
103 a596b957 2022-07-14 tracey fatal("%s: strdup", __func__);
105 a596b957 2022-07-14 tracey proc_i = nargc;
108 a596b957 2022-07-14 tracey /* Point process instance arg to stack and copy the original args. */
109 a596b957 2022-07-14 tracey nargv[nargc] = strdup("-I");
110 a596b957 2022-07-14 tracey if (nargv[nargc] == NULL)
111 a596b957 2022-07-14 tracey fatal("%s: strdup", __func__);
113 a596b957 2022-07-14 tracey nargv[nargc++] = num;
114 a596b957 2022-07-14 tracey for (i = 1; i < (unsigned int) argc; i++)
115 a596b957 2022-07-14 tracey nargv[nargc++] = argv[i];
117 a596b957 2022-07-14 tracey nargv[nargc] = NULL;
119 a596b957 2022-07-14 tracey for (proc = 0; proc < nproc; proc++) {
120 a596b957 2022-07-14 tracey p = &procs[proc];
122 a596b957 2022-07-14 tracey /* Update args with process title. */
123 a596b957 2022-07-14 tracey nargv[proc_i] = (char *)(uintptr_t)p->p_title;
125 a596b957 2022-07-14 tracey /* Fire children processes. */
126 a596b957 2022-07-14 tracey for (i = 0; i < ps->ps_instances[p->p_id]; i++) {
127 a596b957 2022-07-14 tracey /* Update the process instance number. */
128 a596b957 2022-07-14 tracey snprintf(num, sizeof(num), "%u", i);
130 a596b957 2022-07-14 tracey fd = ps->ps_pipes[p->p_id][i].pp_pipes[PROC_GOTWEBD][0];
131 a596b957 2022-07-14 tracey ps->ps_pipes[p->p_id][i].pp_pipes[PROC_GOTWEBD][0] = -1;
133 a596b957 2022-07-14 tracey switch (fork()) {
135 a596b957 2022-07-14 tracey fatal("%s: fork", __func__);
138 a596b957 2022-07-14 tracey /* First create a new session */
139 a596b957 2022-07-14 tracey if (setsid() == -1)
140 a596b957 2022-07-14 tracey fatal("setsid");
142 a596b957 2022-07-14 tracey /* Prepare parent socket. */
143 a596b957 2022-07-14 tracey if (fd != PROC_GOTWEBD_SOCK_FILENO) {
144 a596b957 2022-07-14 tracey if (dup2(fd, PROC_GOTWEBD_SOCK_FILENO)
146 a596b957 2022-07-14 tracey fatal("dup2");
147 a596b957 2022-07-14 tracey } else if (fcntl(fd, F_SETFD, 0) == -1)
148 a596b957 2022-07-14 tracey fatal("fcntl");
150 a596b957 2022-07-14 tracey execvp(argv[0], nargv);
151 a596b957 2022-07-14 tracey fatal("%s: execvp", __func__);
154 a596b957 2022-07-14 tracey /* Close child end. */
155 a596b957 2022-07-14 tracey close(fd);
161 a596b957 2022-07-14 tracey free(nargv);
165 a596b957 2022-07-14 tracey proc_connect(struct privsep *ps)
167 a596b957 2022-07-14 tracey struct imsgev *iev;
168 a596b957 2022-07-14 tracey unsigned int src, dst, inst;
170 a596b957 2022-07-14 tracey /* Don't distribute any sockets if we are not really going to run. */
171 a596b957 2022-07-14 tracey if (ps->ps_noaction)
174 a596b957 2022-07-14 tracey for (dst = 0; dst < PROC_MAX; dst++) {
175 a596b957 2022-07-14 tracey /* We don't communicate with ourselves. */
176 a596b957 2022-07-14 tracey if (dst == PROC_GOTWEBD)
177 a596b957 2022-07-14 tracey continue;
179 a596b957 2022-07-14 tracey for (inst = 0; inst < ps->ps_instances[dst]; inst++) {
180 a596b957 2022-07-14 tracey iev = &ps->ps_ievs[dst][inst];
181 a596b957 2022-07-14 tracey imsg_init(&iev->ibuf, ps->ps_pp->pp_pipes[dst][inst]);
182 a596b957 2022-07-14 tracey event_set(&iev->ev, iev->ibuf.fd, iev->events,
183 a596b957 2022-07-14 tracey iev->handler, iev->data);
184 a596b957 2022-07-14 tracey event_add(&iev->ev, NULL);
188 a596b957 2022-07-14 tracey /* Distribute the socketpair()s for everyone. */
189 a596b957 2022-07-14 tracey for (src = 0; src < PROC_MAX; src++)
190 a596b957 2022-07-14 tracey for (dst = src; dst < PROC_MAX; dst++) {
191 a596b957 2022-07-14 tracey /* Parent already distributed its fds. */
192 a596b957 2022-07-14 tracey if (src == PROC_GOTWEBD || dst == PROC_GOTWEBD)
193 a596b957 2022-07-14 tracey continue;
195 a596b957 2022-07-14 tracey proc_open(ps, src, dst);
200 a596b957 2022-07-14 tracey proc_init(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
201 a596b957 2022-07-14 tracey int argc, char **argv, enum privsep_procid proc_id)
203 a596b957 2022-07-14 tracey struct privsep_proc *p = NULL;
204 a596b957 2022-07-14 tracey struct privsep_pipes *pa, *pb;
205 a596b957 2022-07-14 tracey unsigned int proc;
206 a596b957 2022-07-14 tracey unsigned int dst;
207 a596b957 2022-07-14 tracey int fds[2];
209 a596b957 2022-07-14 tracey /* Don't initiate anything if we are not really going to run. */
210 a596b957 2022-07-14 tracey if (ps->ps_noaction)
213 a596b957 2022-07-14 tracey if (proc_id == PROC_GOTWEBD) {
214 a596b957 2022-07-14 tracey privsep_process = PROC_GOTWEBD;
215 a596b957 2022-07-14 tracey proc_setup(ps, procs, nproc);
218 5e91dae4 2022-08-30 stsp * Create the children sockets so we can use them
219 a596b957 2022-07-14 tracey * to distribute the rest of the socketpair()s using
220 a596b957 2022-07-14 tracey * proc_connect() later.
222 a596b957 2022-07-14 tracey for (dst = 0; dst < PROC_MAX; dst++) {
223 a596b957 2022-07-14 tracey /* Don't create socket for ourselves. */
224 a596b957 2022-07-14 tracey if (dst == PROC_GOTWEBD)
225 a596b957 2022-07-14 tracey continue;
227 a596b957 2022-07-14 tracey for (proc = 0; proc < ps->ps_instances[dst]; proc++) {
228 a596b957 2022-07-14 tracey pa = &ps->ps_pipes[PROC_GOTWEBD][0];
229 a596b957 2022-07-14 tracey pb = &ps->ps_pipes[dst][proc];
230 a596b957 2022-07-14 tracey if (socketpair(AF_UNIX,
231 a596b957 2022-07-14 tracey SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC,
232 a596b957 2022-07-14 tracey PF_UNSPEC, fds) == -1)
233 a596b957 2022-07-14 tracey fatal("%s: socketpair", __func__);
235 a596b957 2022-07-14 tracey pa->pp_pipes[dst][proc] = fds[0];
236 a596b957 2022-07-14 tracey pb->pp_pipes[PROC_GOTWEBD][0] = fds[1];
240 a596b957 2022-07-14 tracey /* Engage! */
241 a596b957 2022-07-14 tracey proc_exec(ps, procs, nproc, argc, argv);
245 a596b957 2022-07-14 tracey /* Initialize a child */
246 a596b957 2022-07-14 tracey for (proc = 0; proc < nproc; proc++) {
247 a596b957 2022-07-14 tracey if (procs[proc].p_id != proc_id)
248 a596b957 2022-07-14 tracey continue;
249 a596b957 2022-07-14 tracey p = &procs[proc];
252 a596b957 2022-07-14 tracey if (p == NULL || p->p_init == NULL)
253 a596b957 2022-07-14 tracey fatalx("%s: process %d missing process initialization",
254 a596b957 2022-07-14 tracey __func__, proc_id);
256 a596b957 2022-07-14 tracey p->p_init(ps, p);
258 a596b957 2022-07-14 tracey fatalx("failed to initiate child process");
262 a596b957 2022-07-14 tracey proc_accept(struct privsep *ps, int fd, enum privsep_procid dst,
263 a596b957 2022-07-14 tracey unsigned int n)
265 a596b957 2022-07-14 tracey struct privsep_pipes *pp = ps->ps_pp;
266 a596b957 2022-07-14 tracey struct imsgev *iev;
268 a596b957 2022-07-14 tracey if (ps->ps_ievs[dst] == NULL) {
269 a596b957 2022-07-14 tracey #if DEBUG > 1
270 a596b957 2022-07-14 tracey log_debug("%s: %s src %d %d to dst %d %d not connected",
271 a596b957 2022-07-14 tracey __func__, ps->ps_title[privsep_process],
272 a596b957 2022-07-14 tracey privsep_process, ps->ps_instance + 1,
273 a596b957 2022-07-14 tracey dst, n + 1);
275 a596b957 2022-07-14 tracey close(fd);
279 a596b957 2022-07-14 tracey if (pp->pp_pipes[dst][n] != -1) {
280 a596b957 2022-07-14 tracey log_warnx("%s: duplicated descriptor", __func__);
281 a596b957 2022-07-14 tracey close(fd);
284 a596b957 2022-07-14 tracey pp->pp_pipes[dst][n] = fd;
286 a596b957 2022-07-14 tracey iev = &ps->ps_ievs[dst][n];
287 a596b957 2022-07-14 tracey imsg_init(&iev->ibuf, fd);
288 a596b957 2022-07-14 tracey event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
289 a596b957 2022-07-14 tracey event_add(&iev->ev, NULL);
293 a596b957 2022-07-14 tracey proc_setup(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc)
295 a596b957 2022-07-14 tracey unsigned int i, j, src, dst, id;
296 a596b957 2022-07-14 tracey struct privsep_pipes *pp;
298 a596b957 2022-07-14 tracey /* Initialize parent title, ps_instances and procs. */
299 a596b957 2022-07-14 tracey ps->ps_title[PROC_GOTWEBD] = "parent";
301 a596b957 2022-07-14 tracey for (src = 0; src < PROC_MAX; src++)
302 a596b957 2022-07-14 tracey /* Default to 1 process instance */
303 a596b957 2022-07-14 tracey if (ps->ps_instances[src] < 1)
304 a596b957 2022-07-14 tracey ps->ps_instances[src] = 1;
306 a596b957 2022-07-14 tracey for (src = 0; src < nproc; src++) {
307 a596b957 2022-07-14 tracey procs[src].p_ps = ps;
308 a596b957 2022-07-14 tracey if (procs[src].p_cb == NULL)
309 a596b957 2022-07-14 tracey procs[src].p_cb = proc_dispatch_null;
311 a596b957 2022-07-14 tracey id = procs[src].p_id;
312 a596b957 2022-07-14 tracey ps->ps_title[id] = procs[src].p_title;
313 a596b957 2022-07-14 tracey ps->ps_ievs[id] = calloc(ps->ps_instances[id],
314 a596b957 2022-07-14 tracey sizeof(struct imsgev));
315 a596b957 2022-07-14 tracey if (ps->ps_ievs[id] == NULL)
316 a596b957 2022-07-14 tracey fatal("%s: calloc", __func__);
318 a596b957 2022-07-14 tracey /* With this set up, we are ready to call imsg_init(). */
319 a596b957 2022-07-14 tracey for (i = 0; i < ps->ps_instances[id]; i++) {
320 a596b957 2022-07-14 tracey ps->ps_ievs[id][i].handler = proc_dispatch;
321 a596b957 2022-07-14 tracey ps->ps_ievs[id][i].events = EV_READ;
322 a596b957 2022-07-14 tracey ps->ps_ievs[id][i].proc = &procs[src];
323 a596b957 2022-07-14 tracey ps->ps_ievs[id][i].data = &ps->ps_ievs[id][i];
328 a596b957 2022-07-14 tracey * Allocate pipes for all process instances (incl. parent)
330 a596b957 2022-07-14 tracey * - ps->ps_pipes: N:M mapping
331 a596b957 2022-07-14 tracey * N source processes connected to M destination processes:
332 a596b957 2022-07-14 tracey * [src][instances][dst][instances], for example
333 a596b957 2022-07-14 tracey * [PROC_RELAY][3][PROC_CA][3]
335 a596b957 2022-07-14 tracey * - ps->ps_pp: per-process 1:M part of ps->ps_pipes
336 a596b957 2022-07-14 tracey * Each process instance has a destination array of socketpair fds:
337 a596b957 2022-07-14 tracey * [dst][instances], for example
338 a596b957 2022-07-14 tracey * [PROC_GOTWEBD][0]
340 a596b957 2022-07-14 tracey for (src = 0; src < PROC_MAX; src++) {
341 a596b957 2022-07-14 tracey /* Allocate destination array for each process */
342 a596b957 2022-07-14 tracey ps->ps_pipes[src] = calloc(ps->ps_instances[src],
343 a596b957 2022-07-14 tracey sizeof(struct privsep_pipes));
344 a596b957 2022-07-14 tracey if (ps->ps_pipes[src] == NULL)
345 a596b957 2022-07-14 tracey fatal("%s: calloc", __func__);
347 a596b957 2022-07-14 tracey for (i = 0; i < ps->ps_instances[src]; i++) {
348 a596b957 2022-07-14 tracey pp = &ps->ps_pipes[src][i];
350 a596b957 2022-07-14 tracey for (dst = 0; dst < PROC_MAX; dst++) {
351 a596b957 2022-07-14 tracey /* Allocate maximum fd integers */
352 a596b957 2022-07-14 tracey pp->pp_pipes[dst] =
353 a596b957 2022-07-14 tracey calloc(ps->ps_instances[dst],
354 a596b957 2022-07-14 tracey sizeof(int));
355 a596b957 2022-07-14 tracey if (pp->pp_pipes[dst] == NULL)
356 a596b957 2022-07-14 tracey fatal("%s: calloc", __func__);
358 a596b957 2022-07-14 tracey /* Mark fd as unused */
359 a596b957 2022-07-14 tracey for (j = 0; j < ps->ps_instances[dst]; j++)
360 a596b957 2022-07-14 tracey pp->pp_pipes[dst][j] = -1;
365 a596b957 2022-07-14 tracey ps->ps_pp = &ps->ps_pipes[privsep_process][ps->ps_instance];
369 a596b957 2022-07-14 tracey proc_kill(struct privsep *ps)
371 a596b957 2022-07-14 tracey char *cause;
372 a596b957 2022-07-14 tracey pid_t pid;
373 a596b957 2022-07-14 tracey int len, status;
375 a596b957 2022-07-14 tracey if (privsep_process != PROC_GOTWEBD)
378 a596b957 2022-07-14 tracey proc_close(ps);
381 a596b957 2022-07-14 tracey pid = waitpid(WAIT_ANY, &status, 0);
382 a596b957 2022-07-14 tracey if (pid <= 0)
383 a596b957 2022-07-14 tracey continue;
385 a596b957 2022-07-14 tracey if (WIFSIGNALED(status)) {
386 a596b957 2022-07-14 tracey len = asprintf(&cause, "terminated; signal %d",
387 a596b957 2022-07-14 tracey WTERMSIG(status));
388 a596b957 2022-07-14 tracey } else if (WIFEXITED(status)) {
389 a596b957 2022-07-14 tracey if (WEXITSTATUS(status) != 0)
390 a596b957 2022-07-14 tracey len = asprintf(&cause, "exited abnormally");
394 a596b957 2022-07-14 tracey len = -1;
396 a596b957 2022-07-14 tracey if (len == 0) {
397 a596b957 2022-07-14 tracey /* child exited OK, don't print a warning message */
398 a596b957 2022-07-14 tracey } else if (len != -1) {
399 a596b957 2022-07-14 tracey log_warnx("lost child: pid %u %s", pid, cause);
400 a596b957 2022-07-14 tracey free(cause);
402 a596b957 2022-07-14 tracey log_warnx("lost child: pid %u", pid);
403 a596b957 2022-07-14 tracey } while (pid != -1 || (pid == -1 && errno == EINTR));
407 a596b957 2022-07-14 tracey proc_open(struct privsep *ps, int src, int dst)
409 a596b957 2022-07-14 tracey struct privsep_pipes *pa, *pb;
410 a596b957 2022-07-14 tracey struct privsep_fd pf;
411 a596b957 2022-07-14 tracey int fds[2];
412 a596b957 2022-07-14 tracey unsigned int i, j;
414 a596b957 2022-07-14 tracey /* Exchange pipes between process. */
415 a596b957 2022-07-14 tracey for (i = 0; i < ps->ps_instances[src]; i++) {
416 a596b957 2022-07-14 tracey for (j = 0; j < ps->ps_instances[dst]; j++) {
417 a596b957 2022-07-14 tracey /* Don't create sockets for ourself. */
418 a596b957 2022-07-14 tracey if (src == dst && i == j)
419 a596b957 2022-07-14 tracey continue;
421 a596b957 2022-07-14 tracey pa = &ps->ps_pipes[src][i];
422 a596b957 2022-07-14 tracey pb = &ps->ps_pipes[dst][j];
423 a596b957 2022-07-14 tracey if (socketpair(AF_UNIX,
424 a596b957 2022-07-14 tracey SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC,
425 a596b957 2022-07-14 tracey PF_UNSPEC, fds) == -1)
426 a596b957 2022-07-14 tracey fatal("%s: socketpair", __func__);
428 a596b957 2022-07-14 tracey pa->pp_pipes[dst][j] = fds[0];
429 a596b957 2022-07-14 tracey pb->pp_pipes[src][i] = fds[1];
431 a596b957 2022-07-14 tracey pf.pf_procid = src;
432 a596b957 2022-07-14 tracey pf.pf_instance = i;
433 a596b957 2022-07-14 tracey if (proc_compose_imsg(ps, dst, j, IMSG_CTL_PROCFD,
434 a596b957 2022-07-14 tracey -1, pb->pp_pipes[src][i], &pf, sizeof(pf)) == -1)
435 a596b957 2022-07-14 tracey fatal("%s: proc_compose_imsg", __func__);
437 a596b957 2022-07-14 tracey pf.pf_procid = dst;
438 a596b957 2022-07-14 tracey pf.pf_instance = j;
439 a596b957 2022-07-14 tracey if (proc_compose_imsg(ps, src, i, IMSG_CTL_PROCFD,
440 a596b957 2022-07-14 tracey -1, pa->pp_pipes[dst][j], &pf, sizeof(pf)) == -1)
441 a596b957 2022-07-14 tracey fatal("%s: proc_compose_imsg", __func__);
444 a596b957 2022-07-14 tracey * We have to flush to send the descriptors and close
445 a596b957 2022-07-14 tracey * them to avoid the fd ramp on startup.
447 a596b957 2022-07-14 tracey if (proc_flush_imsg(ps, src, i) == -1 ||
448 a596b957 2022-07-14 tracey proc_flush_imsg(ps, dst, j) == -1)
449 a596b957 2022-07-14 tracey fatal("%s: imsg_flush", __func__);
455 a596b957 2022-07-14 tracey proc_close(struct privsep *ps)
457 a596b957 2022-07-14 tracey unsigned int dst, n;
458 a596b957 2022-07-14 tracey struct privsep_pipes *pp;
460 a596b957 2022-07-14 tracey if (ps == NULL)
463 a596b957 2022-07-14 tracey pp = ps->ps_pp;
465 a596b957 2022-07-14 tracey for (dst = 0; dst < PROC_MAX; dst++) {
466 a596b957 2022-07-14 tracey if (ps->ps_ievs[dst] == NULL)
467 a596b957 2022-07-14 tracey continue;
469 a596b957 2022-07-14 tracey for (n = 0; n < ps->ps_instances[dst]; n++) {
470 a596b957 2022-07-14 tracey if (pp->pp_pipes[dst][n] == -1)
471 a596b957 2022-07-14 tracey continue;
473 a596b957 2022-07-14 tracey /* Cancel the fd, close and invalidate the fd */
474 a596b957 2022-07-14 tracey event_del(&(ps->ps_ievs[dst][n].ev));
475 a596b957 2022-07-14 tracey imsg_clear(&(ps->ps_ievs[dst][n].ibuf));
476 a596b957 2022-07-14 tracey close(pp->pp_pipes[dst][n]);
477 a596b957 2022-07-14 tracey pp->pp_pipes[dst][n] = -1;
479 a596b957 2022-07-14 tracey free(ps->ps_ievs[dst]);
484 a596b957 2022-07-14 tracey proc_shutdown(struct privsep_proc *p)
486 a596b957 2022-07-14 tracey struct privsep *ps = p->p_ps;
488 a596b957 2022-07-14 tracey if (p->p_shutdown != NULL)
489 a596b957 2022-07-14 tracey (*p->p_shutdown)();
491 a596b957 2022-07-14 tracey proc_close(ps);
493 a596b957 2022-07-14 tracey log_info("%s, %s exiting, pid %d", getprogname(), p->p_title, getpid());
499 a596b957 2022-07-14 tracey proc_sig_handler(int sig, short event, void *arg)
501 a596b957 2022-07-14 tracey struct privsep_proc *p = arg;
503 a596b957 2022-07-14 tracey switch (sig) {
504 a596b957 2022-07-14 tracey case SIGINT:
505 a596b957 2022-07-14 tracey case SIGTERM:
506 a596b957 2022-07-14 tracey proc_shutdown(p);
508 a596b957 2022-07-14 tracey case SIGCHLD:
509 a596b957 2022-07-14 tracey case SIGHUP:
510 a596b957 2022-07-14 tracey case SIGPIPE:
511 a596b957 2022-07-14 tracey case SIGUSR1:
512 a596b957 2022-07-14 tracey /* ignore */
515 a596b957 2022-07-14 tracey fatalx("proc_sig_handler: unexpected signal");
516 a596b957 2022-07-14 tracey /* NOTREACHED */
521 a596b957 2022-07-14 tracey proc_run(struct privsep *ps, struct privsep_proc *p,
522 a596b957 2022-07-14 tracey struct privsep_proc *procs, unsigned int nproc,
523 a596b957 2022-07-14 tracey void (*run)(struct privsep *, struct privsep_proc *, void *), void *arg)
525 a596b957 2022-07-14 tracey struct passwd *pw;
526 a596b957 2022-07-14 tracey const char *root;
528 a596b957 2022-07-14 tracey log_procinit(p->p_title);
530 a596b957 2022-07-14 tracey /* Set the process group of the current process */
531 a596b957 2022-07-14 tracey setpgid(0, 0);
533 a596b957 2022-07-14 tracey /* Use non-standard user */
534 a596b957 2022-07-14 tracey if (p->p_pw != NULL)
535 a596b957 2022-07-14 tracey pw = p->p_pw;
537 a596b957 2022-07-14 tracey pw = ps->ps_pw;
539 a596b957 2022-07-14 tracey /* Change root directory */
540 a596b957 2022-07-14 tracey if (p->p_chroot != NULL)
541 a596b957 2022-07-14 tracey root = p->p_chroot;
543 a596b957 2022-07-14 tracey root = pw->pw_dir;
545 a596b957 2022-07-14 tracey if (chroot(root) == -1)
546 a596b957 2022-07-14 tracey fatal("proc_run: chroot");
547 a596b957 2022-07-14 tracey if (chdir("/") == -1)
548 a596b957 2022-07-14 tracey fatal("proc_run: chdir(\"/\")");
550 a596b957 2022-07-14 tracey privsep_process = p->p_id;
552 a596b957 2022-07-14 tracey setproctitle("%s", p->p_title);
554 a596b957 2022-07-14 tracey if (setgroups(1, &pw->pw_gid) ||
555 a596b957 2022-07-14 tracey setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
556 a596b957 2022-07-14 tracey setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
557 a596b957 2022-07-14 tracey fatal("proc_run: cannot drop privileges");
559 a596b957 2022-07-14 tracey event_init();
561 a596b957 2022-07-14 tracey signal_set(&ps->ps_evsigint, SIGINT, proc_sig_handler, p);
562 a596b957 2022-07-14 tracey signal_set(&ps->ps_evsigterm, SIGTERM, proc_sig_handler, p);
563 a596b957 2022-07-14 tracey signal_set(&ps->ps_evsigchld, SIGCHLD, proc_sig_handler, p);
564 a596b957 2022-07-14 tracey signal_set(&ps->ps_evsighup, SIGHUP, proc_sig_handler, p);
565 a596b957 2022-07-14 tracey signal_set(&ps->ps_evsigpipe, SIGPIPE, proc_sig_handler, p);
566 a596b957 2022-07-14 tracey signal_set(&ps->ps_evsigusr1, SIGUSR1, proc_sig_handler, p);
568 a596b957 2022-07-14 tracey signal_add(&ps->ps_evsigint, NULL);
569 a596b957 2022-07-14 tracey signal_add(&ps->ps_evsigterm, NULL);
570 a596b957 2022-07-14 tracey signal_add(&ps->ps_evsigchld, NULL);
571 a596b957 2022-07-14 tracey signal_add(&ps->ps_evsighup, NULL);
572 a596b957 2022-07-14 tracey signal_add(&ps->ps_evsigpipe, NULL);
573 a596b957 2022-07-14 tracey signal_add(&ps->ps_evsigusr1, NULL);
575 a596b957 2022-07-14 tracey proc_setup(ps, procs, nproc);
576 a596b957 2022-07-14 tracey proc_accept(ps, PROC_GOTWEBD_SOCK_FILENO, PROC_GOTWEBD, 0);
578 a596b957 2022-07-14 tracey DPRINTF("%s: %s %d/%d, pid %d", __func__, p->p_title,
579 a596b957 2022-07-14 tracey ps->ps_instance + 1, ps->ps_instances[p->p_id], getpid());
581 a596b957 2022-07-14 tracey if (run != NULL)
582 a596b957 2022-07-14 tracey run(ps, p, arg);
584 a596b957 2022-07-14 tracey event_dispatch();
586 a596b957 2022-07-14 tracey proc_shutdown(p);
590 a596b957 2022-07-14 tracey proc_dispatch(int fd, short event, void *arg)
592 a596b957 2022-07-14 tracey struct imsgev *iev = arg;
593 a596b957 2022-07-14 tracey struct privsep_proc *p = iev->proc;
594 a596b957 2022-07-14 tracey struct privsep *ps = p->p_ps;
595 a596b957 2022-07-14 tracey struct imsgbuf *ibuf;
596 a596b957 2022-07-14 tracey struct imsg imsg;
597 a596b957 2022-07-14 tracey ssize_t n;
598 a596b957 2022-07-14 tracey int verbose;
599 a596b957 2022-07-14 tracey const char *title;
600 a596b957 2022-07-14 tracey struct privsep_fd pf;
602 a596b957 2022-07-14 tracey title = ps->ps_title[privsep_process];
603 a596b957 2022-07-14 tracey ibuf = &iev->ibuf;
605 a596b957 2022-07-14 tracey if (event & EV_READ) {
606 a596b957 2022-07-14 tracey n = imsg_read(ibuf);
607 a596b957 2022-07-14 tracey if (n == -1 && errno != EAGAIN)
608 a596b957 2022-07-14 tracey fatal("%s: imsg_read", __func__);
609 a596b957 2022-07-14 tracey if (n == 0) {
610 a596b957 2022-07-14 tracey /* this pipe is dead, so remove the event handler */
611 a596b957 2022-07-14 tracey event_del(&iev->ev);
612 a596b957 2022-07-14 tracey event_loopexit(NULL);
617 a596b957 2022-07-14 tracey if (event & EV_WRITE) {
618 a596b957 2022-07-14 tracey n = msgbuf_write(&ibuf->w);
619 a596b957 2022-07-14 tracey if (n == -1 && errno != EAGAIN)
620 a596b957 2022-07-14 tracey fatal("%s: msgbuf_write", __func__);
621 a596b957 2022-07-14 tracey if (n == 0) {
622 a596b957 2022-07-14 tracey /* this pipe is dead, so remove the event handler */
623 a596b957 2022-07-14 tracey event_del(&iev->ev);
624 a596b957 2022-07-14 tracey event_loopexit(NULL);
629 a596b957 2022-07-14 tracey for (;;) {
630 a596b957 2022-07-14 tracey n = imsg_get(ibuf, &imsg);
631 a596b957 2022-07-14 tracey if (n == -1)
632 a596b957 2022-07-14 tracey fatal("%s: imsg_get", __func__);
633 a596b957 2022-07-14 tracey if (n == 0)
636 a596b957 2022-07-14 tracey #if DEBUG > 1
637 a596b957 2022-07-14 tracey log_debug("%s: %s %d got imsg %d peerid %d from %s %d",
638 a596b957 2022-07-14 tracey __func__, title, ps->ps_instance + 1,
639 a596b957 2022-07-14 tracey imsg.hdr.type, imsg.hdr.peerid, p->p_title, imsg.hdr.pid);
643 a596b957 2022-07-14 tracey * Check the message with the program callback
645 a596b957 2022-07-14 tracey if ((p->p_cb)(fd, p, &imsg) == 0) {
646 a596b957 2022-07-14 tracey /* Message was handled by the callback, continue */
647 a596b957 2022-07-14 tracey imsg_free(&imsg);
648 a596b957 2022-07-14 tracey continue;
652 a596b957 2022-07-14 tracey * Generic message handling
654 a596b957 2022-07-14 tracey switch (imsg.hdr.type) {
655 a596b957 2022-07-14 tracey case IMSG_CTL_VERBOSE:
656 a596b957 2022-07-14 tracey log_info("%s", __func__);
657 a596b957 2022-07-14 tracey IMSG_SIZE_CHECK(&imsg, &verbose);
658 a596b957 2022-07-14 tracey memcpy(&verbose, imsg.data, sizeof(verbose));
659 a596b957 2022-07-14 tracey log_setverbose(verbose);
661 a596b957 2022-07-14 tracey case IMSG_CTL_PROCFD:
662 a596b957 2022-07-14 tracey IMSG_SIZE_CHECK(&imsg, &pf);
663 a596b957 2022-07-14 tracey memcpy(&pf, imsg.data, sizeof(pf));
664 a596b957 2022-07-14 tracey proc_accept(ps, imsg.fd, pf.pf_procid,
665 a596b957 2022-07-14 tracey pf.pf_instance);
668 a596b957 2022-07-14 tracey log_warnx("%s: %s %d got invalid imsg %d peerid %d "
669 a596b957 2022-07-14 tracey "from %s %d",
670 a596b957 2022-07-14 tracey __func__, title, ps->ps_instance + 1,
671 a596b957 2022-07-14 tracey imsg.hdr.type, imsg.hdr.peerid,
672 a596b957 2022-07-14 tracey p->p_title, imsg.hdr.pid);
674 a596b957 2022-07-14 tracey imsg_free(&imsg);
676 a596b957 2022-07-14 tracey imsg_event_add(iev);
680 a596b957 2022-07-14 tracey proc_dispatch_null(int fd, struct privsep_proc *p, struct imsg *imsg)
682 a596b957 2022-07-14 tracey return (-1);
686 a596b957 2022-07-14 tracey * imsg helper functions
690 a596b957 2022-07-14 tracey imsg_event_add(struct imsgev *iev)
692 a596b957 2022-07-14 tracey if (iev->handler == NULL) {
693 a596b957 2022-07-14 tracey imsg_flush(&iev->ibuf);
697 a596b957 2022-07-14 tracey iev->events = EV_READ;
698 a596b957 2022-07-14 tracey if (iev->ibuf.w.queued)
699 a596b957 2022-07-14 tracey iev->events |= EV_WRITE;
701 a596b957 2022-07-14 tracey event_del(&iev->ev);
702 a596b957 2022-07-14 tracey event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
703 a596b957 2022-07-14 tracey event_add(&iev->ev, NULL);
707 a596b957 2022-07-14 tracey imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
708 a596b957 2022-07-14 tracey pid_t pid, int fd, void *data, uint16_t datalen)
712 a596b957 2022-07-14 tracey ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data, datalen);
713 a596b957 2022-07-14 tracey if (ret == -1)
714 a596b957 2022-07-14 tracey return (ret);
715 a596b957 2022-07-14 tracey imsg_event_add(iev);
716 a596b957 2022-07-14 tracey return (ret);
720 a596b957 2022-07-14 tracey imsg_composev_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
721 a596b957 2022-07-14 tracey pid_t pid, int fd, const struct iovec *iov, int iovcnt)
725 a596b957 2022-07-14 tracey ret = imsg_composev(&iev->ibuf, type, peerid, pid, fd, iov, iovcnt);
726 a596b957 2022-07-14 tracey if (ret == -1)
727 a596b957 2022-07-14 tracey return (ret);
728 a596b957 2022-07-14 tracey imsg_event_add(iev);
729 a596b957 2022-07-14 tracey return (ret);
733 a596b957 2022-07-14 tracey proc_range(struct privsep *ps, enum privsep_procid id, int *n, int *m)
735 a596b957 2022-07-14 tracey if (*n == -1) {
736 a596b957 2022-07-14 tracey /* Use a range of all target instances */
738 a596b957 2022-07-14 tracey *m = ps->ps_instances[id];
740 a596b957 2022-07-14 tracey /* Use only a single slot of the specified peer process */
741 a596b957 2022-07-14 tracey *m = *n + 1;
746 a596b957 2022-07-14 tracey proc_compose_imsg(struct privsep *ps, enum privsep_procid id, int n,
747 a596b957 2022-07-14 tracey uint16_t type, uint32_t peerid, int fd, void *data, uint16_t datalen)
751 a596b957 2022-07-14 tracey proc_range(ps, id, &n, &m);
752 a596b957 2022-07-14 tracey for (; n < m; n++) {
753 a596b957 2022-07-14 tracey if (imsg_compose_event(&ps->ps_ievs[id][n],
754 a596b957 2022-07-14 tracey type, peerid, ps->ps_instance + 1, fd, data, datalen) == -1)
755 a596b957 2022-07-14 tracey return (-1);
758 a596b957 2022-07-14 tracey return (0);
762 a596b957 2022-07-14 tracey proc_compose(struct privsep *ps, enum privsep_procid id,
763 a596b957 2022-07-14 tracey uint16_t type, void *data, uint16_t datalen)
765 a596b957 2022-07-14 tracey return (proc_compose_imsg(ps, id, -1, type, -1, -1, data, datalen));
769 a596b957 2022-07-14 tracey proc_composev_imsg(struct privsep *ps, enum privsep_procid id, int n,
770 a596b957 2022-07-14 tracey uint16_t type, uint32_t peerid, int fd, const struct iovec *iov, int iovcnt)
774 a596b957 2022-07-14 tracey proc_range(ps, id, &n, &m);
775 a596b957 2022-07-14 tracey for (; n < m; n++)
776 a596b957 2022-07-14 tracey if (imsg_composev_event(&ps->ps_ievs[id][n],
777 a596b957 2022-07-14 tracey type, peerid, ps->ps_instance + 1, fd, iov, iovcnt) == -1)
778 a596b957 2022-07-14 tracey return (-1);
780 a596b957 2022-07-14 tracey return (0);
784 a596b957 2022-07-14 tracey proc_composev(struct privsep *ps, enum privsep_procid id,
785 a596b957 2022-07-14 tracey uint16_t type, const struct iovec *iov, int iovcnt)
787 a596b957 2022-07-14 tracey return (proc_composev_imsg(ps, id, -1, type, -1, -1, iov, iovcnt));
791 a596b957 2022-07-14 tracey proc_forward_imsg(struct privsep *ps, struct imsg *imsg,
792 a596b957 2022-07-14 tracey enum privsep_procid id, int n)
794 a596b957 2022-07-14 tracey return (proc_compose_imsg(ps, id, n, imsg->hdr.type,
795 a596b957 2022-07-14 tracey imsg->hdr.peerid, imsg->fd, imsg->data, IMSG_DATA_SIZE(imsg)));
798 a596b957 2022-07-14 tracey struct imsgbuf *
799 a596b957 2022-07-14 tracey proc_ibuf(struct privsep *ps, enum privsep_procid id, int n)
803 a596b957 2022-07-14 tracey proc_range(ps, id, &n, &m);
804 a596b957 2022-07-14 tracey return (&ps->ps_ievs[id][n].ibuf);
807 a596b957 2022-07-14 tracey struct imsgev *
808 a596b957 2022-07-14 tracey proc_iev(struct privsep *ps, enum privsep_procid id, int n)
812 a596b957 2022-07-14 tracey proc_range(ps, id, &n, &m);
813 a596b957 2022-07-14 tracey return (&ps->ps_ievs[id][n]);
816 a596b957 2022-07-14 tracey /* This function should only be called with care as it breaks async I/O */
818 a596b957 2022-07-14 tracey proc_flush_imsg(struct privsep *ps, enum privsep_procid id, int n)
820 a596b957 2022-07-14 tracey struct imsgbuf *ibuf;
821 a596b957 2022-07-14 tracey int m, ret = 0;
823 a596b957 2022-07-14 tracey proc_range(ps, id, &n, &m);
824 a596b957 2022-07-14 tracey for (; n < m; n++) {
825 a596b957 2022-07-14 tracey ibuf = proc_ibuf(ps, id, n);
826 a596b957 2022-07-14 tracey if (ibuf == NULL)
827 a596b957 2022-07-14 tracey return (-1);
829 a596b957 2022-07-14 tracey ret = imsg_flush(ibuf);
830 a596b957 2022-07-14 tracey } while (ret == -1 && errno == EAGAIN);
831 a596b957 2022-07-14 tracey if (ret == -1)
833 a596b957 2022-07-14 tracey imsg_event_add(&ps->ps_ievs[id][n]);
836 a596b957 2022-07-14 tracey return (ret);