Blame


1 56324ebe 2022-07-14 thomas /*
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>
4 56324ebe 2022-07-14 thomas *
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.
8 56324ebe 2022-07-14 thomas *
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.
16 56324ebe 2022-07-14 thomas */
17 56324ebe 2022-07-14 thomas
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>
21 56324ebe 2022-07-14 thomas
22 d7cad54e 2022-07-14 thomas /* TA: FIXME */
23 d7cad54e 2022-07-14 thomas #include <grp.h>
24 d7cad54e 2022-07-14 thomas
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>
34 56324ebe 2022-07-14 thomas
35 56324ebe 2022-07-14 thomas #include "proc.h"
36 56324ebe 2022-07-14 thomas
37 d7cad54e 2022-07-14 thomas #include "got_compat.h"
38 d7cad54e 2022-07-14 thomas
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 *);
50 56324ebe 2022-07-14 thomas
51 56324ebe 2022-07-14 thomas enum privsep_procid privsep_process;
52 56324ebe 2022-07-14 thomas
53 56324ebe 2022-07-14 thomas int
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)
56 56324ebe 2022-07-14 thomas {
57 56324ebe 2022-07-14 thomas unsigned int i;
58 56324ebe 2022-07-14 thomas
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);
62 56324ebe 2022-07-14 thomas
63 56324ebe 2022-07-14 thomas return (0);
64 56324ebe 2022-07-14 thomas }
65 56324ebe 2022-07-14 thomas
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)
69 56324ebe 2022-07-14 thomas {
70 56324ebe 2022-07-14 thomas struct privsep_proc *p;
71 56324ebe 2022-07-14 thomas unsigned int proc;
72 56324ebe 2022-07-14 thomas
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))
76 56324ebe 2022-07-14 thomas continue;
77 56324ebe 2022-07-14 thomas
78 56324ebe 2022-07-14 thomas return (p->p_id);
79 56324ebe 2022-07-14 thomas }
80 56324ebe 2022-07-14 thomas
81 56324ebe 2022-07-14 thomas return (PROC_MAX);
82 56324ebe 2022-07-14 thomas }
83 56324ebe 2022-07-14 thomas
84 56324ebe 2022-07-14 thomas void
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)
87 56324ebe 2022-07-14 thomas {
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];
92 56324ebe 2022-07-14 thomas int fd;
93 56324ebe 2022-07-14 thomas
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__);
98 56324ebe 2022-07-14 thomas
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];
102 56324ebe 2022-07-14 thomas
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__);
107 56324ebe 2022-07-14 thomas nargc++;
108 56324ebe 2022-07-14 thomas proc_i = nargc;
109 56324ebe 2022-07-14 thomas nargc++;
110 56324ebe 2022-07-14 thomas
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__);
115 56324ebe 2022-07-14 thomas nargc++;
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];
119 56324ebe 2022-07-14 thomas
120 56324ebe 2022-07-14 thomas nargv[nargc] = NULL;
121 56324ebe 2022-07-14 thomas
122 56324ebe 2022-07-14 thomas for (proc = 0; proc < nproc; proc++) {
123 56324ebe 2022-07-14 thomas p = &procs[proc];
124 56324ebe 2022-07-14 thomas
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;
127 56324ebe 2022-07-14 thomas
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);
132 56324ebe 2022-07-14 thomas
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;
135 56324ebe 2022-07-14 thomas
136 56324ebe 2022-07-14 thomas switch (fork()) {
137 56324ebe 2022-07-14 thomas case -1:
138 56324ebe 2022-07-14 thomas fatal("%s: fork", __func__);
139 56324ebe 2022-07-14 thomas break;
140 56324ebe 2022-07-14 thomas case 0:
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");
144 56324ebe 2022-07-14 thomas
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)
148 56324ebe 2022-07-14 thomas == -1)
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");
152 56324ebe 2022-07-14 thomas
153 56324ebe 2022-07-14 thomas execvp(argv[0], nargv);
154 56324ebe 2022-07-14 thomas fatal("%s: execvp", __func__);
155 56324ebe 2022-07-14 thomas break;
156 56324ebe 2022-07-14 thomas default:
157 56324ebe 2022-07-14 thomas /* Close child end. */
158 56324ebe 2022-07-14 thomas close(fd);
159 56324ebe 2022-07-14 thomas break;
160 56324ebe 2022-07-14 thomas }
161 56324ebe 2022-07-14 thomas }
162 56324ebe 2022-07-14 thomas }
163 56324ebe 2022-07-14 thomas
164 56324ebe 2022-07-14 thomas free(nargv);
165 56324ebe 2022-07-14 thomas }
166 56324ebe 2022-07-14 thomas
167 56324ebe 2022-07-14 thomas void
168 56324ebe 2022-07-14 thomas proc_connect(struct privsep *ps)
169 56324ebe 2022-07-14 thomas {
170 56324ebe 2022-07-14 thomas struct imsgev *iev;
171 56324ebe 2022-07-14 thomas unsigned int src, dst, inst;
172 56324ebe 2022-07-14 thomas
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)
175 56324ebe 2022-07-14 thomas return;
176 56324ebe 2022-07-14 thomas
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;
181 56324ebe 2022-07-14 thomas
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);
188 56324ebe 2022-07-14 thomas }
189 56324ebe 2022-07-14 thomas }
190 56324ebe 2022-07-14 thomas
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;
197 56324ebe 2022-07-14 thomas
198 56324ebe 2022-07-14 thomas proc_open(ps, src, dst);
199 56324ebe 2022-07-14 thomas }
200 56324ebe 2022-07-14 thomas }
201 56324ebe 2022-07-14 thomas
202 56324ebe 2022-07-14 thomas void
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)
205 56324ebe 2022-07-14 thomas {
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];
211 56324ebe 2022-07-14 thomas
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)
214 56324ebe 2022-07-14 thomas return;
215 56324ebe 2022-07-14 thomas
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);
219 56324ebe 2022-07-14 thomas
220 56324ebe 2022-07-14 thomas /*
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.
224 56324ebe 2022-07-14 thomas */
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;
229 56324ebe 2022-07-14 thomas
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 62da1d17 2022-07-15 thomas int sock_flags = SOCK_STREAM | SOCK_NONBLOCK;
234 62da1d17 2022-07-15 thomas #ifdef SOCK_CLOEXEC
235 62da1d17 2022-07-15 thomas sock_flags |= SOCK_CLOEXEC;
236 62da1d17 2022-07-15 thomas #endif
237 62da1d17 2022-07-15 thomas if (socketpair(AF_UNIX, sock_flags,
238 56324ebe 2022-07-14 thomas PF_UNSPEC, fds) == -1)
239 56324ebe 2022-07-14 thomas fatal("%s: socketpair", __func__);
240 56324ebe 2022-07-14 thomas
241 56324ebe 2022-07-14 thomas pa->pp_pipes[dst][proc] = fds[0];
242 56324ebe 2022-07-14 thomas pb->pp_pipes[PROC_GOTWEBD][0] = fds[1];
243 56324ebe 2022-07-14 thomas }
244 56324ebe 2022-07-14 thomas }
245 56324ebe 2022-07-14 thomas
246 56324ebe 2022-07-14 thomas /* Engage! */
247 56324ebe 2022-07-14 thomas proc_exec(ps, procs, nproc, argc, argv);
248 56324ebe 2022-07-14 thomas return;
249 56324ebe 2022-07-14 thomas }
250 56324ebe 2022-07-14 thomas
251 56324ebe 2022-07-14 thomas /* Initialize a child */
252 56324ebe 2022-07-14 thomas for (proc = 0; proc < nproc; proc++) {
253 56324ebe 2022-07-14 thomas if (procs[proc].p_id != proc_id)
254 56324ebe 2022-07-14 thomas continue;
255 56324ebe 2022-07-14 thomas p = &procs[proc];
256 56324ebe 2022-07-14 thomas break;
257 56324ebe 2022-07-14 thomas }
258 56324ebe 2022-07-14 thomas if (p == NULL || p->p_init == NULL)
259 56324ebe 2022-07-14 thomas fatalx("%s: process %d missing process initialization",
260 56324ebe 2022-07-14 thomas __func__, proc_id);
261 56324ebe 2022-07-14 thomas
262 56324ebe 2022-07-14 thomas p->p_init(ps, p);
263 56324ebe 2022-07-14 thomas
264 56324ebe 2022-07-14 thomas fatalx("failed to initiate child process");
265 56324ebe 2022-07-14 thomas }
266 56324ebe 2022-07-14 thomas
267 56324ebe 2022-07-14 thomas void
268 56324ebe 2022-07-14 thomas proc_accept(struct privsep *ps, int fd, enum privsep_procid dst,
269 56324ebe 2022-07-14 thomas unsigned int n)
270 56324ebe 2022-07-14 thomas {
271 56324ebe 2022-07-14 thomas struct privsep_pipes *pp = ps->ps_pp;
272 56324ebe 2022-07-14 thomas struct imsgev *iev;
273 56324ebe 2022-07-14 thomas
274 56324ebe 2022-07-14 thomas if (ps->ps_ievs[dst] == NULL) {
275 56324ebe 2022-07-14 thomas #if DEBUG > 1
276 56324ebe 2022-07-14 thomas log_debug("%s: %s src %d %d to dst %d %d not connected",
277 56324ebe 2022-07-14 thomas __func__, ps->ps_title[privsep_process],
278 56324ebe 2022-07-14 thomas privsep_process, ps->ps_instance + 1,
279 56324ebe 2022-07-14 thomas dst, n + 1);
280 56324ebe 2022-07-14 thomas #endif
281 56324ebe 2022-07-14 thomas close(fd);
282 56324ebe 2022-07-14 thomas return;
283 56324ebe 2022-07-14 thomas }
284 56324ebe 2022-07-14 thomas
285 56324ebe 2022-07-14 thomas if (pp->pp_pipes[dst][n] != -1) {
286 56324ebe 2022-07-14 thomas log_warnx("%s: duplicated descriptor", __func__);
287 56324ebe 2022-07-14 thomas close(fd);
288 56324ebe 2022-07-14 thomas return;
289 56324ebe 2022-07-14 thomas } else
290 56324ebe 2022-07-14 thomas pp->pp_pipes[dst][n] = fd;
291 56324ebe 2022-07-14 thomas
292 56324ebe 2022-07-14 thomas iev = &ps->ps_ievs[dst][n];
293 56324ebe 2022-07-14 thomas imsg_init(&iev->ibuf, fd);
294 56324ebe 2022-07-14 thomas event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
295 56324ebe 2022-07-14 thomas event_add(&iev->ev, NULL);
296 56324ebe 2022-07-14 thomas }
297 56324ebe 2022-07-14 thomas
298 56324ebe 2022-07-14 thomas void
299 56324ebe 2022-07-14 thomas proc_setup(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc)
300 56324ebe 2022-07-14 thomas {
301 56324ebe 2022-07-14 thomas unsigned int i, j, src, dst, id;
302 56324ebe 2022-07-14 thomas struct privsep_pipes *pp;
303 56324ebe 2022-07-14 thomas
304 56324ebe 2022-07-14 thomas /* Initialize parent title, ps_instances and procs. */
305 56324ebe 2022-07-14 thomas ps->ps_title[PROC_GOTWEBD] = "parent";
306 56324ebe 2022-07-14 thomas
307 56324ebe 2022-07-14 thomas for (src = 0; src < PROC_MAX; src++)
308 56324ebe 2022-07-14 thomas /* Default to 1 process instance */
309 56324ebe 2022-07-14 thomas if (ps->ps_instances[src] < 1)
310 56324ebe 2022-07-14 thomas ps->ps_instances[src] = 1;
311 56324ebe 2022-07-14 thomas
312 56324ebe 2022-07-14 thomas for (src = 0; src < nproc; src++) {
313 56324ebe 2022-07-14 thomas procs[src].p_ps = ps;
314 56324ebe 2022-07-14 thomas if (procs[src].p_cb == NULL)
315 56324ebe 2022-07-14 thomas procs[src].p_cb = proc_dispatch_null;
316 56324ebe 2022-07-14 thomas
317 56324ebe 2022-07-14 thomas id = procs[src].p_id;
318 56324ebe 2022-07-14 thomas ps->ps_title[id] = procs[src].p_title;
319 56324ebe 2022-07-14 thomas ps->ps_ievs[id] = calloc(ps->ps_instances[id],
320 56324ebe 2022-07-14 thomas sizeof(struct imsgev));
321 56324ebe 2022-07-14 thomas if (ps->ps_ievs[id] == NULL)
322 56324ebe 2022-07-14 thomas fatal("%s: calloc", __func__);
323 56324ebe 2022-07-14 thomas
324 56324ebe 2022-07-14 thomas /* With this set up, we are ready to call imsg_init(). */
325 56324ebe 2022-07-14 thomas for (i = 0; i < ps->ps_instances[id]; i++) {
326 56324ebe 2022-07-14 thomas ps->ps_ievs[id][i].handler = proc_dispatch;
327 56324ebe 2022-07-14 thomas ps->ps_ievs[id][i].events = EV_READ;
328 56324ebe 2022-07-14 thomas ps->ps_ievs[id][i].proc = &procs[src];
329 56324ebe 2022-07-14 thomas ps->ps_ievs[id][i].data = &ps->ps_ievs[id][i];
330 56324ebe 2022-07-14 thomas }
331 56324ebe 2022-07-14 thomas }
332 56324ebe 2022-07-14 thomas
333 56324ebe 2022-07-14 thomas /*
334 56324ebe 2022-07-14 thomas * Allocate pipes for all process instances (incl. parent)
335 56324ebe 2022-07-14 thomas *
336 56324ebe 2022-07-14 thomas * - ps->ps_pipes: N:M mapping
337 56324ebe 2022-07-14 thomas * N source processes connected to M destination processes:
338 56324ebe 2022-07-14 thomas * [src][instances][dst][instances], for example
339 56324ebe 2022-07-14 thomas * [PROC_RELAY][3][PROC_CA][3]
340 56324ebe 2022-07-14 thomas *
341 56324ebe 2022-07-14 thomas * - ps->ps_pp: per-process 1:M part of ps->ps_pipes
342 56324ebe 2022-07-14 thomas * Each process instance has a destination array of socketpair fds:
343 56324ebe 2022-07-14 thomas * [dst][instances], for example
344 56324ebe 2022-07-14 thomas * [PROC_GOTWEBD][0]
345 56324ebe 2022-07-14 thomas */
346 56324ebe 2022-07-14 thomas for (src = 0; src < PROC_MAX; src++) {
347 56324ebe 2022-07-14 thomas /* Allocate destination array for each process */
348 56324ebe 2022-07-14 thomas ps->ps_pipes[src] = calloc(ps->ps_instances[src],
349 56324ebe 2022-07-14 thomas sizeof(struct privsep_pipes));
350 56324ebe 2022-07-14 thomas if (ps->ps_pipes[src] == NULL)
351 56324ebe 2022-07-14 thomas fatal("%s: calloc", __func__);
352 56324ebe 2022-07-14 thomas
353 56324ebe 2022-07-14 thomas for (i = 0; i < ps->ps_instances[src]; i++) {
354 56324ebe 2022-07-14 thomas pp = &ps->ps_pipes[src][i];
355 56324ebe 2022-07-14 thomas
356 56324ebe 2022-07-14 thomas for (dst = 0; dst < PROC_MAX; dst++) {
357 56324ebe 2022-07-14 thomas /* Allocate maximum fd integers */
358 56324ebe 2022-07-14 thomas pp->pp_pipes[dst] =
359 56324ebe 2022-07-14 thomas calloc(ps->ps_instances[dst],
360 56324ebe 2022-07-14 thomas sizeof(int));
361 56324ebe 2022-07-14 thomas if (pp->pp_pipes[dst] == NULL)
362 56324ebe 2022-07-14 thomas fatal("%s: calloc", __func__);
363 56324ebe 2022-07-14 thomas
364 56324ebe 2022-07-14 thomas /* Mark fd as unused */
365 56324ebe 2022-07-14 thomas for (j = 0; j < ps->ps_instances[dst]; j++)
366 56324ebe 2022-07-14 thomas pp->pp_pipes[dst][j] = -1;
367 56324ebe 2022-07-14 thomas }
368 56324ebe 2022-07-14 thomas }
369 56324ebe 2022-07-14 thomas }
370 56324ebe 2022-07-14 thomas
371 56324ebe 2022-07-14 thomas ps->ps_pp = &ps->ps_pipes[privsep_process][ps->ps_instance];
372 56324ebe 2022-07-14 thomas }
373 56324ebe 2022-07-14 thomas
374 56324ebe 2022-07-14 thomas void
375 56324ebe 2022-07-14 thomas proc_kill(struct privsep *ps)
376 56324ebe 2022-07-14 thomas {
377 56324ebe 2022-07-14 thomas char *cause;
378 56324ebe 2022-07-14 thomas pid_t pid;
379 56324ebe 2022-07-14 thomas int len, status;
380 56324ebe 2022-07-14 thomas
381 56324ebe 2022-07-14 thomas if (privsep_process != PROC_GOTWEBD)
382 56324ebe 2022-07-14 thomas return;
383 56324ebe 2022-07-14 thomas
384 56324ebe 2022-07-14 thomas proc_close(ps);
385 56324ebe 2022-07-14 thomas
386 56324ebe 2022-07-14 thomas do {
387 56324ebe 2022-07-14 thomas pid = waitpid(WAIT_ANY, &status, 0);
388 56324ebe 2022-07-14 thomas if (pid <= 0)
389 56324ebe 2022-07-14 thomas continue;
390 56324ebe 2022-07-14 thomas
391 56324ebe 2022-07-14 thomas if (WIFSIGNALED(status)) {
392 56324ebe 2022-07-14 thomas len = asprintf(&cause, "terminated; signal %d",
393 56324ebe 2022-07-14 thomas WTERMSIG(status));
394 56324ebe 2022-07-14 thomas } else if (WIFEXITED(status)) {
395 56324ebe 2022-07-14 thomas if (WEXITSTATUS(status) != 0)
396 56324ebe 2022-07-14 thomas len = asprintf(&cause, "exited abnormally");
397 56324ebe 2022-07-14 thomas else
398 56324ebe 2022-07-14 thomas len = 0;
399 56324ebe 2022-07-14 thomas } else
400 56324ebe 2022-07-14 thomas len = -1;
401 56324ebe 2022-07-14 thomas
402 56324ebe 2022-07-14 thomas if (len == 0) {
403 56324ebe 2022-07-14 thomas /* child exited OK, don't print a warning message */
404 56324ebe 2022-07-14 thomas } else if (len != -1) {
405 56324ebe 2022-07-14 thomas log_warnx("lost child: pid %u %s", pid, cause);
406 56324ebe 2022-07-14 thomas free(cause);
407 56324ebe 2022-07-14 thomas } else
408 56324ebe 2022-07-14 thomas log_warnx("lost child: pid %u", pid);
409 56324ebe 2022-07-14 thomas } while (pid != -1 || (pid == -1 && errno == EINTR));
410 56324ebe 2022-07-14 thomas }
411 56324ebe 2022-07-14 thomas
412 56324ebe 2022-07-14 thomas void
413 56324ebe 2022-07-14 thomas proc_open(struct privsep *ps, int src, int dst)
414 56324ebe 2022-07-14 thomas {
415 56324ebe 2022-07-14 thomas struct privsep_pipes *pa, *pb;
416 56324ebe 2022-07-14 thomas struct privsep_fd pf;
417 56324ebe 2022-07-14 thomas int fds[2];
418 56324ebe 2022-07-14 thomas unsigned int i, j;
419 56324ebe 2022-07-14 thomas
420 56324ebe 2022-07-14 thomas /* Exchange pipes between process. */
421 56324ebe 2022-07-14 thomas for (i = 0; i < ps->ps_instances[src]; i++) {
422 56324ebe 2022-07-14 thomas for (j = 0; j < ps->ps_instances[dst]; j++) {
423 56324ebe 2022-07-14 thomas /* Don't create sockets for ourself. */
424 56324ebe 2022-07-14 thomas if (src == dst && i == j)
425 56324ebe 2022-07-14 thomas continue;
426 56324ebe 2022-07-14 thomas
427 56324ebe 2022-07-14 thomas pa = &ps->ps_pipes[src][i];
428 56324ebe 2022-07-14 thomas pb = &ps->ps_pipes[dst][j];
429 62da1d17 2022-07-15 thomas int sock_flags = SOCK_STREAM | SOCK_NONBLOCK;
430 62da1d17 2022-07-15 thomas #ifdef SOCK_CLOEXEC
431 62da1d17 2022-07-15 thomas sock_flags |= SOCK_CLOEXEC;
432 62da1d17 2022-07-15 thomas #endif
433 62da1d17 2022-07-15 thomas if (socketpair(AF_UNIX, sock_flags,
434 56324ebe 2022-07-14 thomas PF_UNSPEC, fds) == -1)
435 56324ebe 2022-07-14 thomas fatal("%s: socketpair", __func__);
436 56324ebe 2022-07-14 thomas
437 56324ebe 2022-07-14 thomas pa->pp_pipes[dst][j] = fds[0];
438 56324ebe 2022-07-14 thomas pb->pp_pipes[src][i] = fds[1];
439 56324ebe 2022-07-14 thomas
440 56324ebe 2022-07-14 thomas pf.pf_procid = src;
441 56324ebe 2022-07-14 thomas pf.pf_instance = i;
442 56324ebe 2022-07-14 thomas if (proc_compose_imsg(ps, dst, j, IMSG_CTL_PROCFD,
443 56324ebe 2022-07-14 thomas -1, pb->pp_pipes[src][i], &pf, sizeof(pf)) == -1)
444 56324ebe 2022-07-14 thomas fatal("%s: proc_compose_imsg", __func__);
445 56324ebe 2022-07-14 thomas
446 56324ebe 2022-07-14 thomas pf.pf_procid = dst;
447 56324ebe 2022-07-14 thomas pf.pf_instance = j;
448 56324ebe 2022-07-14 thomas if (proc_compose_imsg(ps, src, i, IMSG_CTL_PROCFD,
449 56324ebe 2022-07-14 thomas -1, pa->pp_pipes[dst][j], &pf, sizeof(pf)) == -1)
450 56324ebe 2022-07-14 thomas fatal("%s: proc_compose_imsg", __func__);
451 56324ebe 2022-07-14 thomas
452 56324ebe 2022-07-14 thomas /*
453 56324ebe 2022-07-14 thomas * We have to flush to send the descriptors and close
454 56324ebe 2022-07-14 thomas * them to avoid the fd ramp on startup.
455 56324ebe 2022-07-14 thomas */
456 56324ebe 2022-07-14 thomas if (proc_flush_imsg(ps, src, i) == -1 ||
457 56324ebe 2022-07-14 thomas proc_flush_imsg(ps, dst, j) == -1)
458 56324ebe 2022-07-14 thomas fatal("%s: imsg_flush", __func__);
459 56324ebe 2022-07-14 thomas }
460 56324ebe 2022-07-14 thomas }
461 56324ebe 2022-07-14 thomas }
462 56324ebe 2022-07-14 thomas
463 56324ebe 2022-07-14 thomas void
464 56324ebe 2022-07-14 thomas proc_close(struct privsep *ps)
465 56324ebe 2022-07-14 thomas {
466 56324ebe 2022-07-14 thomas unsigned int dst, n;
467 56324ebe 2022-07-14 thomas struct privsep_pipes *pp;
468 56324ebe 2022-07-14 thomas
469 56324ebe 2022-07-14 thomas if (ps == NULL)
470 56324ebe 2022-07-14 thomas return;
471 56324ebe 2022-07-14 thomas
472 56324ebe 2022-07-14 thomas pp = ps->ps_pp;
473 56324ebe 2022-07-14 thomas
474 56324ebe 2022-07-14 thomas for (dst = 0; dst < PROC_MAX; dst++) {
475 56324ebe 2022-07-14 thomas if (ps->ps_ievs[dst] == NULL)
476 56324ebe 2022-07-14 thomas continue;
477 56324ebe 2022-07-14 thomas
478 56324ebe 2022-07-14 thomas for (n = 0; n < ps->ps_instances[dst]; n++) {
479 56324ebe 2022-07-14 thomas if (pp->pp_pipes[dst][n] == -1)
480 56324ebe 2022-07-14 thomas continue;
481 56324ebe 2022-07-14 thomas
482 56324ebe 2022-07-14 thomas /* Cancel the fd, close and invalidate the fd */
483 56324ebe 2022-07-14 thomas event_del(&(ps->ps_ievs[dst][n].ev));
484 56324ebe 2022-07-14 thomas imsg_clear(&(ps->ps_ievs[dst][n].ibuf));
485 56324ebe 2022-07-14 thomas close(pp->pp_pipes[dst][n]);
486 56324ebe 2022-07-14 thomas pp->pp_pipes[dst][n] = -1;
487 56324ebe 2022-07-14 thomas }
488 56324ebe 2022-07-14 thomas free(ps->ps_ievs[dst]);
489 56324ebe 2022-07-14 thomas }
490 56324ebe 2022-07-14 thomas }
491 56324ebe 2022-07-14 thomas
492 56324ebe 2022-07-14 thomas void
493 56324ebe 2022-07-14 thomas proc_shutdown(struct privsep_proc *p)
494 56324ebe 2022-07-14 thomas {
495 56324ebe 2022-07-14 thomas struct privsep *ps = p->p_ps;
496 56324ebe 2022-07-14 thomas
497 56324ebe 2022-07-14 thomas if (p->p_shutdown != NULL)
498 56324ebe 2022-07-14 thomas (*p->p_shutdown)();
499 56324ebe 2022-07-14 thomas
500 56324ebe 2022-07-14 thomas proc_close(ps);
501 56324ebe 2022-07-14 thomas
502 56324ebe 2022-07-14 thomas log_info("%s, %s exiting, pid %d", getprogname(), p->p_title, getpid());
503 56324ebe 2022-07-14 thomas
504 56324ebe 2022-07-14 thomas exit(0);
505 56324ebe 2022-07-14 thomas }
506 56324ebe 2022-07-14 thomas
507 56324ebe 2022-07-14 thomas void
508 56324ebe 2022-07-14 thomas proc_sig_handler(int sig, short event, void *arg)
509 56324ebe 2022-07-14 thomas {
510 56324ebe 2022-07-14 thomas struct privsep_proc *p = arg;
511 56324ebe 2022-07-14 thomas
512 56324ebe 2022-07-14 thomas switch (sig) {
513 56324ebe 2022-07-14 thomas case SIGINT:
514 56324ebe 2022-07-14 thomas case SIGTERM:
515 56324ebe 2022-07-14 thomas proc_shutdown(p);
516 56324ebe 2022-07-14 thomas break;
517 56324ebe 2022-07-14 thomas case SIGCHLD:
518 56324ebe 2022-07-14 thomas case SIGHUP:
519 56324ebe 2022-07-14 thomas case SIGPIPE:
520 56324ebe 2022-07-14 thomas case SIGUSR1:
521 56324ebe 2022-07-14 thomas /* ignore */
522 56324ebe 2022-07-14 thomas break;
523 56324ebe 2022-07-14 thomas default:
524 56324ebe 2022-07-14 thomas fatalx("proc_sig_handler: unexpected signal");
525 56324ebe 2022-07-14 thomas /* NOTREACHED */
526 56324ebe 2022-07-14 thomas }
527 56324ebe 2022-07-14 thomas }
528 56324ebe 2022-07-14 thomas
529 56324ebe 2022-07-14 thomas void
530 56324ebe 2022-07-14 thomas proc_run(struct privsep *ps, struct privsep_proc *p,
531 56324ebe 2022-07-14 thomas struct privsep_proc *procs, unsigned int nproc,
532 56324ebe 2022-07-14 thomas void (*run)(struct privsep *, struct privsep_proc *, void *), void *arg)
533 56324ebe 2022-07-14 thomas {
534 56324ebe 2022-07-14 thomas struct passwd *pw;
535 56324ebe 2022-07-14 thomas const char *root;
536 56324ebe 2022-07-14 thomas
537 56324ebe 2022-07-14 thomas log_procinit(p->p_title);
538 56324ebe 2022-07-14 thomas
539 56324ebe 2022-07-14 thomas /* Set the process group of the current process */
540 56324ebe 2022-07-14 thomas setpgid(0, 0);
541 56324ebe 2022-07-14 thomas
542 56324ebe 2022-07-14 thomas /* Use non-standard user */
543 56324ebe 2022-07-14 thomas if (p->p_pw != NULL)
544 56324ebe 2022-07-14 thomas pw = p->p_pw;
545 56324ebe 2022-07-14 thomas else
546 56324ebe 2022-07-14 thomas pw = ps->ps_pw;
547 56324ebe 2022-07-14 thomas
548 56324ebe 2022-07-14 thomas /* Change root directory */
549 56324ebe 2022-07-14 thomas if (p->p_chroot != NULL)
550 56324ebe 2022-07-14 thomas root = p->p_chroot;
551 56324ebe 2022-07-14 thomas else
552 56324ebe 2022-07-14 thomas root = pw->pw_dir;
553 56324ebe 2022-07-14 thomas
554 56324ebe 2022-07-14 thomas if (chroot(root) == -1)
555 56324ebe 2022-07-14 thomas fatal("proc_run: chroot");
556 56324ebe 2022-07-14 thomas if (chdir("/") == -1)
557 56324ebe 2022-07-14 thomas fatal("proc_run: chdir(\"/\")");
558 56324ebe 2022-07-14 thomas
559 56324ebe 2022-07-14 thomas privsep_process = p->p_id;
560 56324ebe 2022-07-14 thomas
561 56324ebe 2022-07-14 thomas setproctitle("%s", p->p_title);
562 56324ebe 2022-07-14 thomas
563 56324ebe 2022-07-14 thomas if (setgroups(1, &pw->pw_gid) ||
564 56324ebe 2022-07-14 thomas setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
565 56324ebe 2022-07-14 thomas setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
566 56324ebe 2022-07-14 thomas fatal("proc_run: cannot drop privileges");
567 56324ebe 2022-07-14 thomas
568 56324ebe 2022-07-14 thomas event_init();
569 56324ebe 2022-07-14 thomas
570 56324ebe 2022-07-14 thomas signal_set(&ps->ps_evsigint, SIGINT, proc_sig_handler, p);
571 56324ebe 2022-07-14 thomas signal_set(&ps->ps_evsigterm, SIGTERM, proc_sig_handler, p);
572 56324ebe 2022-07-14 thomas signal_set(&ps->ps_evsigchld, SIGCHLD, proc_sig_handler, p);
573 56324ebe 2022-07-14 thomas signal_set(&ps->ps_evsighup, SIGHUP, proc_sig_handler, p);
574 56324ebe 2022-07-14 thomas signal_set(&ps->ps_evsigpipe, SIGPIPE, proc_sig_handler, p);
575 56324ebe 2022-07-14 thomas signal_set(&ps->ps_evsigusr1, SIGUSR1, proc_sig_handler, p);
576 56324ebe 2022-07-14 thomas
577 56324ebe 2022-07-14 thomas signal_add(&ps->ps_evsigint, NULL);
578 56324ebe 2022-07-14 thomas signal_add(&ps->ps_evsigterm, NULL);
579 56324ebe 2022-07-14 thomas signal_add(&ps->ps_evsigchld, NULL);
580 56324ebe 2022-07-14 thomas signal_add(&ps->ps_evsighup, NULL);
581 56324ebe 2022-07-14 thomas signal_add(&ps->ps_evsigpipe, NULL);
582 56324ebe 2022-07-14 thomas signal_add(&ps->ps_evsigusr1, NULL);
583 56324ebe 2022-07-14 thomas
584 56324ebe 2022-07-14 thomas proc_setup(ps, procs, nproc);
585 56324ebe 2022-07-14 thomas proc_accept(ps, PROC_GOTWEBD_SOCK_FILENO, PROC_GOTWEBD, 0);
586 56324ebe 2022-07-14 thomas
587 56324ebe 2022-07-14 thomas DPRINTF("%s: %s %d/%d, pid %d", __func__, p->p_title,
588 56324ebe 2022-07-14 thomas ps->ps_instance + 1, ps->ps_instances[p->p_id], getpid());
589 56324ebe 2022-07-14 thomas
590 56324ebe 2022-07-14 thomas if (run != NULL)
591 56324ebe 2022-07-14 thomas run(ps, p, arg);
592 56324ebe 2022-07-14 thomas
593 56324ebe 2022-07-14 thomas event_dispatch();
594 56324ebe 2022-07-14 thomas
595 56324ebe 2022-07-14 thomas proc_shutdown(p);
596 56324ebe 2022-07-14 thomas }
597 56324ebe 2022-07-14 thomas
598 56324ebe 2022-07-14 thomas void
599 56324ebe 2022-07-14 thomas proc_dispatch(int fd, short event, void *arg)
600 56324ebe 2022-07-14 thomas {
601 56324ebe 2022-07-14 thomas struct imsgev *iev = arg;
602 56324ebe 2022-07-14 thomas struct privsep_proc *p = iev->proc;
603 56324ebe 2022-07-14 thomas struct privsep *ps = p->p_ps;
604 56324ebe 2022-07-14 thomas struct imsgbuf *ibuf;
605 56324ebe 2022-07-14 thomas struct imsg imsg;
606 56324ebe 2022-07-14 thomas ssize_t n;
607 56324ebe 2022-07-14 thomas int verbose;
608 56324ebe 2022-07-14 thomas const char *title;
609 56324ebe 2022-07-14 thomas struct privsep_fd pf;
610 56324ebe 2022-07-14 thomas
611 56324ebe 2022-07-14 thomas title = ps->ps_title[privsep_process];
612 56324ebe 2022-07-14 thomas ibuf = &iev->ibuf;
613 56324ebe 2022-07-14 thomas
614 56324ebe 2022-07-14 thomas if (event & EV_READ) {
615 56324ebe 2022-07-14 thomas n = imsg_read(ibuf);
616 56324ebe 2022-07-14 thomas if (n == -1 && errno != EAGAIN)
617 56324ebe 2022-07-14 thomas fatal("%s: imsg_read", __func__);
618 56324ebe 2022-07-14 thomas if (n == 0) {
619 56324ebe 2022-07-14 thomas /* this pipe is dead, so remove the event handler */
620 56324ebe 2022-07-14 thomas event_del(&iev->ev);
621 56324ebe 2022-07-14 thomas event_loopexit(NULL);
622 56324ebe 2022-07-14 thomas return;
623 56324ebe 2022-07-14 thomas }
624 56324ebe 2022-07-14 thomas }
625 56324ebe 2022-07-14 thomas
626 56324ebe 2022-07-14 thomas if (event & EV_WRITE) {
627 56324ebe 2022-07-14 thomas n = msgbuf_write(&ibuf->w);
628 56324ebe 2022-07-14 thomas if (n == -1 && errno != EAGAIN)
629 56324ebe 2022-07-14 thomas fatal("%s: msgbuf_write", __func__);
630 56324ebe 2022-07-14 thomas if (n == 0) {
631 56324ebe 2022-07-14 thomas /* this pipe is dead, so remove the event handler */
632 56324ebe 2022-07-14 thomas event_del(&iev->ev);
633 56324ebe 2022-07-14 thomas event_loopexit(NULL);
634 56324ebe 2022-07-14 thomas return;
635 56324ebe 2022-07-14 thomas }
636 56324ebe 2022-07-14 thomas }
637 56324ebe 2022-07-14 thomas
638 56324ebe 2022-07-14 thomas for (;;) {
639 56324ebe 2022-07-14 thomas n = imsg_get(ibuf, &imsg);
640 56324ebe 2022-07-14 thomas if (n == -1)
641 56324ebe 2022-07-14 thomas fatal("%s: imsg_get", __func__);
642 56324ebe 2022-07-14 thomas if (n == 0)
643 56324ebe 2022-07-14 thomas break;
644 56324ebe 2022-07-14 thomas
645 56324ebe 2022-07-14 thomas #if DEBUG > 1
646 56324ebe 2022-07-14 thomas log_debug("%s: %s %d got imsg %d peerid %d from %s %d",
647 56324ebe 2022-07-14 thomas __func__, title, ps->ps_instance + 1,
648 56324ebe 2022-07-14 thomas imsg.hdr.type, imsg.hdr.peerid, p->p_title, imsg.hdr.pid);
649 56324ebe 2022-07-14 thomas #endif
650 56324ebe 2022-07-14 thomas
651 56324ebe 2022-07-14 thomas /*
652 56324ebe 2022-07-14 thomas * Check the message with the program callback
653 56324ebe 2022-07-14 thomas */
654 56324ebe 2022-07-14 thomas if ((p->p_cb)(fd, p, &imsg) == 0) {
655 56324ebe 2022-07-14 thomas /* Message was handled by the callback, continue */
656 56324ebe 2022-07-14 thomas imsg_free(&imsg);
657 56324ebe 2022-07-14 thomas continue;
658 56324ebe 2022-07-14 thomas }
659 56324ebe 2022-07-14 thomas
660 56324ebe 2022-07-14 thomas /*
661 56324ebe 2022-07-14 thomas * Generic message handling
662 56324ebe 2022-07-14 thomas */
663 56324ebe 2022-07-14 thomas switch (imsg.hdr.type) {
664 56324ebe 2022-07-14 thomas case IMSG_CTL_VERBOSE:
665 56324ebe 2022-07-14 thomas log_info("%s", __func__);
666 56324ebe 2022-07-14 thomas IMSG_SIZE_CHECK(&imsg, &verbose);
667 56324ebe 2022-07-14 thomas memcpy(&verbose, imsg.data, sizeof(verbose));
668 56324ebe 2022-07-14 thomas log_setverbose(verbose);
669 56324ebe 2022-07-14 thomas break;
670 56324ebe 2022-07-14 thomas case IMSG_CTL_PROCFD:
671 56324ebe 2022-07-14 thomas IMSG_SIZE_CHECK(&imsg, &pf);
672 56324ebe 2022-07-14 thomas memcpy(&pf, imsg.data, sizeof(pf));
673 56324ebe 2022-07-14 thomas proc_accept(ps, imsg.fd, pf.pf_procid,
674 56324ebe 2022-07-14 thomas pf.pf_instance);
675 56324ebe 2022-07-14 thomas break;
676 56324ebe 2022-07-14 thomas default:
677 56324ebe 2022-07-14 thomas log_warnx("%s: %s %d got invalid imsg %d peerid %d "
678 56324ebe 2022-07-14 thomas "from %s %d",
679 56324ebe 2022-07-14 thomas __func__, title, ps->ps_instance + 1,
680 56324ebe 2022-07-14 thomas imsg.hdr.type, imsg.hdr.peerid,
681 56324ebe 2022-07-14 thomas p->p_title, imsg.hdr.pid);
682 56324ebe 2022-07-14 thomas }
683 56324ebe 2022-07-14 thomas imsg_free(&imsg);
684 56324ebe 2022-07-14 thomas }
685 56324ebe 2022-07-14 thomas imsg_event_add(iev);
686 56324ebe 2022-07-14 thomas }
687 56324ebe 2022-07-14 thomas
688 56324ebe 2022-07-14 thomas int
689 56324ebe 2022-07-14 thomas proc_dispatch_null(int fd, struct privsep_proc *p, struct imsg *imsg)
690 56324ebe 2022-07-14 thomas {
691 56324ebe 2022-07-14 thomas return (-1);
692 56324ebe 2022-07-14 thomas }
693 56324ebe 2022-07-14 thomas
694 56324ebe 2022-07-14 thomas /*
695 56324ebe 2022-07-14 thomas * imsg helper functions
696 56324ebe 2022-07-14 thomas */
697 56324ebe 2022-07-14 thomas
698 56324ebe 2022-07-14 thomas void
699 56324ebe 2022-07-14 thomas imsg_event_add(struct imsgev *iev)
700 56324ebe 2022-07-14 thomas {
701 56324ebe 2022-07-14 thomas if (iev->handler == NULL) {
702 56324ebe 2022-07-14 thomas imsg_flush(&iev->ibuf);
703 56324ebe 2022-07-14 thomas return;
704 56324ebe 2022-07-14 thomas }
705 56324ebe 2022-07-14 thomas
706 56324ebe 2022-07-14 thomas iev->events = EV_READ;
707 56324ebe 2022-07-14 thomas if (iev->ibuf.w.queued)
708 56324ebe 2022-07-14 thomas iev->events |= EV_WRITE;
709 56324ebe 2022-07-14 thomas
710 56324ebe 2022-07-14 thomas event_del(&iev->ev);
711 56324ebe 2022-07-14 thomas event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
712 56324ebe 2022-07-14 thomas event_add(&iev->ev, NULL);
713 56324ebe 2022-07-14 thomas }
714 56324ebe 2022-07-14 thomas
715 56324ebe 2022-07-14 thomas int
716 56324ebe 2022-07-14 thomas imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
717 56324ebe 2022-07-14 thomas pid_t pid, int fd, void *data, uint16_t datalen)
718 56324ebe 2022-07-14 thomas {
719 56324ebe 2022-07-14 thomas int ret;
720 56324ebe 2022-07-14 thomas
721 56324ebe 2022-07-14 thomas ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data, datalen);
722 56324ebe 2022-07-14 thomas if (ret == -1)
723 56324ebe 2022-07-14 thomas return (ret);
724 56324ebe 2022-07-14 thomas imsg_event_add(iev);
725 56324ebe 2022-07-14 thomas return (ret);
726 56324ebe 2022-07-14 thomas }
727 56324ebe 2022-07-14 thomas
728 56324ebe 2022-07-14 thomas int
729 56324ebe 2022-07-14 thomas imsg_composev_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
730 56324ebe 2022-07-14 thomas pid_t pid, int fd, const struct iovec *iov, int iovcnt)
731 56324ebe 2022-07-14 thomas {
732 56324ebe 2022-07-14 thomas int ret;
733 56324ebe 2022-07-14 thomas
734 56324ebe 2022-07-14 thomas ret = imsg_composev(&iev->ibuf, type, peerid, pid, fd, iov, iovcnt);
735 56324ebe 2022-07-14 thomas if (ret == -1)
736 56324ebe 2022-07-14 thomas return (ret);
737 56324ebe 2022-07-14 thomas imsg_event_add(iev);
738 56324ebe 2022-07-14 thomas return (ret);
739 56324ebe 2022-07-14 thomas }
740 56324ebe 2022-07-14 thomas
741 56324ebe 2022-07-14 thomas void
742 56324ebe 2022-07-14 thomas proc_range(struct privsep *ps, enum privsep_procid id, int *n, int *m)
743 56324ebe 2022-07-14 thomas {
744 56324ebe 2022-07-14 thomas if (*n == -1) {
745 56324ebe 2022-07-14 thomas /* Use a range of all target instances */
746 56324ebe 2022-07-14 thomas *n = 0;
747 56324ebe 2022-07-14 thomas *m = ps->ps_instances[id];
748 56324ebe 2022-07-14 thomas } else {
749 56324ebe 2022-07-14 thomas /* Use only a single slot of the specified peer process */
750 56324ebe 2022-07-14 thomas *m = *n + 1;
751 56324ebe 2022-07-14 thomas }
752 56324ebe 2022-07-14 thomas }
753 56324ebe 2022-07-14 thomas
754 56324ebe 2022-07-14 thomas int
755 56324ebe 2022-07-14 thomas proc_compose_imsg(struct privsep *ps, enum privsep_procid id, int n,
756 56324ebe 2022-07-14 thomas uint16_t type, uint32_t peerid, int fd, void *data, uint16_t datalen)
757 56324ebe 2022-07-14 thomas {
758 56324ebe 2022-07-14 thomas int m;
759 56324ebe 2022-07-14 thomas
760 56324ebe 2022-07-14 thomas proc_range(ps, id, &n, &m);
761 56324ebe 2022-07-14 thomas for (; n < m; n++) {
762 56324ebe 2022-07-14 thomas if (imsg_compose_event(&ps->ps_ievs[id][n],
763 56324ebe 2022-07-14 thomas type, peerid, ps->ps_instance + 1, fd, data, datalen) == -1)
764 56324ebe 2022-07-14 thomas return (-1);
765 56324ebe 2022-07-14 thomas }
766 56324ebe 2022-07-14 thomas
767 56324ebe 2022-07-14 thomas return (0);
768 56324ebe 2022-07-14 thomas }
769 56324ebe 2022-07-14 thomas
770 56324ebe 2022-07-14 thomas int
771 56324ebe 2022-07-14 thomas proc_compose(struct privsep *ps, enum privsep_procid id,
772 56324ebe 2022-07-14 thomas uint16_t type, void *data, uint16_t datalen)
773 56324ebe 2022-07-14 thomas {
774 56324ebe 2022-07-14 thomas return (proc_compose_imsg(ps, id, -1, type, -1, -1, data, datalen));
775 56324ebe 2022-07-14 thomas }
776 56324ebe 2022-07-14 thomas
777 56324ebe 2022-07-14 thomas int
778 56324ebe 2022-07-14 thomas proc_composev_imsg(struct privsep *ps, enum privsep_procid id, int n,
779 56324ebe 2022-07-14 thomas uint16_t type, uint32_t peerid, int fd, const struct iovec *iov, int iovcnt)
780 56324ebe 2022-07-14 thomas {
781 56324ebe 2022-07-14 thomas int m;
782 56324ebe 2022-07-14 thomas
783 56324ebe 2022-07-14 thomas proc_range(ps, id, &n, &m);
784 56324ebe 2022-07-14 thomas for (; n < m; n++)
785 56324ebe 2022-07-14 thomas if (imsg_composev_event(&ps->ps_ievs[id][n],
786 56324ebe 2022-07-14 thomas type, peerid, ps->ps_instance + 1, fd, iov, iovcnt) == -1)
787 56324ebe 2022-07-14 thomas return (-1);
788 56324ebe 2022-07-14 thomas
789 56324ebe 2022-07-14 thomas return (0);
790 56324ebe 2022-07-14 thomas }
791 56324ebe 2022-07-14 thomas
792 56324ebe 2022-07-14 thomas int
793 56324ebe 2022-07-14 thomas proc_composev(struct privsep *ps, enum privsep_procid id,
794 56324ebe 2022-07-14 thomas uint16_t type, const struct iovec *iov, int iovcnt)
795 56324ebe 2022-07-14 thomas {
796 56324ebe 2022-07-14 thomas return (proc_composev_imsg(ps, id, -1, type, -1, -1, iov, iovcnt));
797 56324ebe 2022-07-14 thomas }
798 56324ebe 2022-07-14 thomas
799 56324ebe 2022-07-14 thomas int
800 56324ebe 2022-07-14 thomas proc_forward_imsg(struct privsep *ps, struct imsg *imsg,
801 56324ebe 2022-07-14 thomas enum privsep_procid id, int n)
802 56324ebe 2022-07-14 thomas {
803 56324ebe 2022-07-14 thomas return (proc_compose_imsg(ps, id, n, imsg->hdr.type,
804 56324ebe 2022-07-14 thomas imsg->hdr.peerid, imsg->fd, imsg->data, IMSG_DATA_SIZE(imsg)));
805 56324ebe 2022-07-14 thomas }
806 56324ebe 2022-07-14 thomas
807 56324ebe 2022-07-14 thomas struct imsgbuf *
808 56324ebe 2022-07-14 thomas proc_ibuf(struct privsep *ps, enum privsep_procid id, int n)
809 56324ebe 2022-07-14 thomas {
810 56324ebe 2022-07-14 thomas int m;
811 56324ebe 2022-07-14 thomas
812 56324ebe 2022-07-14 thomas proc_range(ps, id, &n, &m);
813 56324ebe 2022-07-14 thomas return (&ps->ps_ievs[id][n].ibuf);
814 56324ebe 2022-07-14 thomas }
815 56324ebe 2022-07-14 thomas
816 56324ebe 2022-07-14 thomas struct imsgev *
817 56324ebe 2022-07-14 thomas proc_iev(struct privsep *ps, enum privsep_procid id, int n)
818 56324ebe 2022-07-14 thomas {
819 56324ebe 2022-07-14 thomas int m;
820 56324ebe 2022-07-14 thomas
821 56324ebe 2022-07-14 thomas proc_range(ps, id, &n, &m);
822 56324ebe 2022-07-14 thomas return (&ps->ps_ievs[id][n]);
823 56324ebe 2022-07-14 thomas }
824 56324ebe 2022-07-14 thomas
825 56324ebe 2022-07-14 thomas /* This function should only be called with care as it breaks async I/O */
826 56324ebe 2022-07-14 thomas int
827 56324ebe 2022-07-14 thomas proc_flush_imsg(struct privsep *ps, enum privsep_procid id, int n)
828 56324ebe 2022-07-14 thomas {
829 56324ebe 2022-07-14 thomas struct imsgbuf *ibuf;
830 56324ebe 2022-07-14 thomas int m, ret = 0;
831 56324ebe 2022-07-14 thomas
832 56324ebe 2022-07-14 thomas proc_range(ps, id, &n, &m);
833 56324ebe 2022-07-14 thomas for (; n < m; n++) {
834 56324ebe 2022-07-14 thomas ibuf = proc_ibuf(ps, id, n);
835 56324ebe 2022-07-14 thomas if (ibuf == NULL)
836 56324ebe 2022-07-14 thomas return (-1);
837 56324ebe 2022-07-14 thomas do {
838 56324ebe 2022-07-14 thomas ret = imsg_flush(ibuf);
839 56324ebe 2022-07-14 thomas } while (ret == -1 && errno == EAGAIN);
840 56324ebe 2022-07-14 thomas if (ret == -1)
841 56324ebe 2022-07-14 thomas break;
842 56324ebe 2022-07-14 thomas imsg_event_add(&ps->ps_ievs[id][n]);
843 56324ebe 2022-07-14 thomas }
844 56324ebe 2022-07-14 thomas
845 56324ebe 2022-07-14 thomas return (ret);
846 56324ebe 2022-07-14 thomas }