Blame


1 a596b957 2022-07-14 tracey /*
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>
4 a596b957 2022-07-14 tracey *
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.
8 a596b957 2022-07-14 tracey *
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.
16 a596b957 2022-07-14 tracey */
17 a596b957 2022-07-14 tracey
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>
22 a596b957 2022-07-14 tracey
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>
33 a596b957 2022-07-14 tracey
34 a596b957 2022-07-14 tracey #include "proc.h"
35 a596b957 2022-07-14 tracey
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 *);
47 a596b957 2022-07-14 tracey
48 a596b957 2022-07-14 tracey enum privsep_procid privsep_process;
49 a596b957 2022-07-14 tracey
50 a596b957 2022-07-14 tracey int
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)
53 a596b957 2022-07-14 tracey {
54 a596b957 2022-07-14 tracey unsigned int i;
55 a596b957 2022-07-14 tracey
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);
59 a596b957 2022-07-14 tracey
60 a596b957 2022-07-14 tracey return (0);
61 a596b957 2022-07-14 tracey }
62 a596b957 2022-07-14 tracey
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)
66 a596b957 2022-07-14 tracey {
67 a596b957 2022-07-14 tracey struct privsep_proc *p;
68 a596b957 2022-07-14 tracey unsigned int proc;
69 a596b957 2022-07-14 tracey
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))
73 a596b957 2022-07-14 tracey continue;
74 a596b957 2022-07-14 tracey
75 a596b957 2022-07-14 tracey return (p->p_id);
76 a596b957 2022-07-14 tracey }
77 a596b957 2022-07-14 tracey
78 a596b957 2022-07-14 tracey return (PROC_MAX);
79 a596b957 2022-07-14 tracey }
80 a596b957 2022-07-14 tracey
81 a596b957 2022-07-14 tracey void
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)
84 a596b957 2022-07-14 tracey {
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];
89 a596b957 2022-07-14 tracey int fd;
90 a596b957 2022-07-14 tracey
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__);
95 a596b957 2022-07-14 tracey
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];
99 a596b957 2022-07-14 tracey
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__);
104 a596b957 2022-07-14 tracey nargc++;
105 a596b957 2022-07-14 tracey proc_i = nargc;
106 a596b957 2022-07-14 tracey nargc++;
107 a596b957 2022-07-14 tracey
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__);
112 a596b957 2022-07-14 tracey nargc++;
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];
116 a596b957 2022-07-14 tracey
117 a596b957 2022-07-14 tracey nargv[nargc] = NULL;
118 a596b957 2022-07-14 tracey
119 a596b957 2022-07-14 tracey for (proc = 0; proc < nproc; proc++) {
120 a596b957 2022-07-14 tracey p = &procs[proc];
121 a596b957 2022-07-14 tracey
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;
124 a596b957 2022-07-14 tracey
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);
129 a596b957 2022-07-14 tracey
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;
132 a596b957 2022-07-14 tracey
133 a596b957 2022-07-14 tracey switch (fork()) {
134 a596b957 2022-07-14 tracey case -1:
135 a596b957 2022-07-14 tracey fatal("%s: fork", __func__);
136 a596b957 2022-07-14 tracey break;
137 a596b957 2022-07-14 tracey case 0:
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");
141 a596b957 2022-07-14 tracey
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)
145 a596b957 2022-07-14 tracey == -1)
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");
149 a596b957 2022-07-14 tracey
150 a596b957 2022-07-14 tracey execvp(argv[0], nargv);
151 a596b957 2022-07-14 tracey fatal("%s: execvp", __func__);
152 a596b957 2022-07-14 tracey break;
153 a596b957 2022-07-14 tracey default:
154 a596b957 2022-07-14 tracey /* Close child end. */
155 a596b957 2022-07-14 tracey close(fd);
156 a596b957 2022-07-14 tracey break;
157 a596b957 2022-07-14 tracey }
158 a596b957 2022-07-14 tracey }
159 a596b957 2022-07-14 tracey }
160 a596b957 2022-07-14 tracey
161 a596b957 2022-07-14 tracey free(nargv);
162 a596b957 2022-07-14 tracey }
163 a596b957 2022-07-14 tracey
164 a596b957 2022-07-14 tracey void
165 a596b957 2022-07-14 tracey proc_connect(struct privsep *ps)
166 a596b957 2022-07-14 tracey {
167 a596b957 2022-07-14 tracey struct imsgev *iev;
168 a596b957 2022-07-14 tracey unsigned int src, dst, inst;
169 a596b957 2022-07-14 tracey
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)
172 a596b957 2022-07-14 tracey return;
173 a596b957 2022-07-14 tracey
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;
178 a596b957 2022-07-14 tracey
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);
185 a596b957 2022-07-14 tracey }
186 a596b957 2022-07-14 tracey }
187 a596b957 2022-07-14 tracey
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;
194 a596b957 2022-07-14 tracey
195 a596b957 2022-07-14 tracey proc_open(ps, src, dst);
196 a596b957 2022-07-14 tracey }
197 a596b957 2022-07-14 tracey }
198 a596b957 2022-07-14 tracey
199 a596b957 2022-07-14 tracey void
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)
202 a596b957 2022-07-14 tracey {
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];
208 a596b957 2022-07-14 tracey
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)
211 a596b957 2022-07-14 tracey return;
212 a596b957 2022-07-14 tracey
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);
216 a596b957 2022-07-14 tracey
217 a596b957 2022-07-14 tracey /*
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.
221 a596b957 2022-07-14 tracey */
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;
226 a596b957 2022-07-14 tracey
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__);
234 a596b957 2022-07-14 tracey
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];
237 a596b957 2022-07-14 tracey }
238 a596b957 2022-07-14 tracey }
239 a596b957 2022-07-14 tracey
240 a596b957 2022-07-14 tracey /* Engage! */
241 a596b957 2022-07-14 tracey proc_exec(ps, procs, nproc, argc, argv);
242 a596b957 2022-07-14 tracey return;
243 a596b957 2022-07-14 tracey }
244 a596b957 2022-07-14 tracey
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];
250 a596b957 2022-07-14 tracey break;
251 a596b957 2022-07-14 tracey }
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);
255 a596b957 2022-07-14 tracey
256 a596b957 2022-07-14 tracey p->p_init(ps, p);
257 a596b957 2022-07-14 tracey
258 a596b957 2022-07-14 tracey fatalx("failed to initiate child process");
259 a596b957 2022-07-14 tracey }
260 a596b957 2022-07-14 tracey
261 a596b957 2022-07-14 tracey void
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)
264 a596b957 2022-07-14 tracey {
265 a596b957 2022-07-14 tracey struct privsep_pipes *pp = ps->ps_pp;
266 a596b957 2022-07-14 tracey struct imsgev *iev;
267 a596b957 2022-07-14 tracey
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);
274 a596b957 2022-07-14 tracey #endif
275 a596b957 2022-07-14 tracey close(fd);
276 a596b957 2022-07-14 tracey return;
277 a596b957 2022-07-14 tracey }
278 a596b957 2022-07-14 tracey
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);
282 a596b957 2022-07-14 tracey return;
283 a596b957 2022-07-14 tracey } else
284 a596b957 2022-07-14 tracey pp->pp_pipes[dst][n] = fd;
285 a596b957 2022-07-14 tracey
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);
290 a596b957 2022-07-14 tracey }
291 a596b957 2022-07-14 tracey
292 a596b957 2022-07-14 tracey void
293 a596b957 2022-07-14 tracey proc_setup(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc)
294 a596b957 2022-07-14 tracey {
295 a596b957 2022-07-14 tracey unsigned int i, j, src, dst, id;
296 a596b957 2022-07-14 tracey struct privsep_pipes *pp;
297 a596b957 2022-07-14 tracey
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";
300 a596b957 2022-07-14 tracey
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;
305 a596b957 2022-07-14 tracey
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;
310 a596b957 2022-07-14 tracey
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__);
317 a596b957 2022-07-14 tracey
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];
324 a596b957 2022-07-14 tracey }
325 a596b957 2022-07-14 tracey }
326 a596b957 2022-07-14 tracey
327 a596b957 2022-07-14 tracey /*
328 a596b957 2022-07-14 tracey * Allocate pipes for all process instances (incl. parent)
329 a596b957 2022-07-14 tracey *
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]
334 a596b957 2022-07-14 tracey *
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]
339 a596b957 2022-07-14 tracey */
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__);
346 a596b957 2022-07-14 tracey
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];
349 a596b957 2022-07-14 tracey
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__);
357 a596b957 2022-07-14 tracey
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;
361 a596b957 2022-07-14 tracey }
362 a596b957 2022-07-14 tracey }
363 a596b957 2022-07-14 tracey }
364 a596b957 2022-07-14 tracey
365 a596b957 2022-07-14 tracey ps->ps_pp = &ps->ps_pipes[privsep_process][ps->ps_instance];
366 a596b957 2022-07-14 tracey }
367 a596b957 2022-07-14 tracey
368 a596b957 2022-07-14 tracey void
369 a596b957 2022-07-14 tracey proc_kill(struct privsep *ps)
370 a596b957 2022-07-14 tracey {
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;
374 a596b957 2022-07-14 tracey
375 a596b957 2022-07-14 tracey if (privsep_process != PROC_GOTWEBD)
376 a596b957 2022-07-14 tracey return;
377 a596b957 2022-07-14 tracey
378 a596b957 2022-07-14 tracey proc_close(ps);
379 a596b957 2022-07-14 tracey
380 a596b957 2022-07-14 tracey do {
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;
384 a596b957 2022-07-14 tracey
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");
391 a596b957 2022-07-14 tracey else
392 a596b957 2022-07-14 tracey len = 0;
393 a596b957 2022-07-14 tracey } else
394 a596b957 2022-07-14 tracey len = -1;
395 a596b957 2022-07-14 tracey
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);
401 a596b957 2022-07-14 tracey } else
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));
404 a596b957 2022-07-14 tracey }
405 a596b957 2022-07-14 tracey
406 a596b957 2022-07-14 tracey void
407 a596b957 2022-07-14 tracey proc_open(struct privsep *ps, int src, int dst)
408 a596b957 2022-07-14 tracey {
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;
413 a596b957 2022-07-14 tracey
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;
420 a596b957 2022-07-14 tracey
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__);
427 a596b957 2022-07-14 tracey
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];
430 a596b957 2022-07-14 tracey
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__);
436 a596b957 2022-07-14 tracey
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__);
442 a596b957 2022-07-14 tracey
443 a596b957 2022-07-14 tracey /*
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.
446 a596b957 2022-07-14 tracey */
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__);
450 a596b957 2022-07-14 tracey }
451 a596b957 2022-07-14 tracey }
452 a596b957 2022-07-14 tracey }
453 a596b957 2022-07-14 tracey
454 a596b957 2022-07-14 tracey void
455 a596b957 2022-07-14 tracey proc_close(struct privsep *ps)
456 a596b957 2022-07-14 tracey {
457 a596b957 2022-07-14 tracey unsigned int dst, n;
458 a596b957 2022-07-14 tracey struct privsep_pipes *pp;
459 a596b957 2022-07-14 tracey
460 a596b957 2022-07-14 tracey if (ps == NULL)
461 a596b957 2022-07-14 tracey return;
462 a596b957 2022-07-14 tracey
463 a596b957 2022-07-14 tracey pp = ps->ps_pp;
464 a596b957 2022-07-14 tracey
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;
468 a596b957 2022-07-14 tracey
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;
472 a596b957 2022-07-14 tracey
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;
478 a596b957 2022-07-14 tracey }
479 a596b957 2022-07-14 tracey free(ps->ps_ievs[dst]);
480 a596b957 2022-07-14 tracey }
481 a596b957 2022-07-14 tracey }
482 a596b957 2022-07-14 tracey
483 a596b957 2022-07-14 tracey void
484 a596b957 2022-07-14 tracey proc_shutdown(struct privsep_proc *p)
485 a596b957 2022-07-14 tracey {
486 a596b957 2022-07-14 tracey struct privsep *ps = p->p_ps;
487 a596b957 2022-07-14 tracey
488 a596b957 2022-07-14 tracey if (p->p_shutdown != NULL)
489 a596b957 2022-07-14 tracey (*p->p_shutdown)();
490 a596b957 2022-07-14 tracey
491 a596b957 2022-07-14 tracey proc_close(ps);
492 a596b957 2022-07-14 tracey
493 a596b957 2022-07-14 tracey log_info("%s, %s exiting, pid %d", getprogname(), p->p_title, getpid());
494 a596b957 2022-07-14 tracey
495 a596b957 2022-07-14 tracey exit(0);
496 a596b957 2022-07-14 tracey }
497 a596b957 2022-07-14 tracey
498 a596b957 2022-07-14 tracey void
499 a596b957 2022-07-14 tracey proc_sig_handler(int sig, short event, void *arg)
500 a596b957 2022-07-14 tracey {
501 a596b957 2022-07-14 tracey struct privsep_proc *p = arg;
502 a596b957 2022-07-14 tracey
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);
507 a596b957 2022-07-14 tracey break;
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 */
513 a596b957 2022-07-14 tracey break;
514 a596b957 2022-07-14 tracey default:
515 a596b957 2022-07-14 tracey fatalx("proc_sig_handler: unexpected signal");
516 a596b957 2022-07-14 tracey /* NOTREACHED */
517 a596b957 2022-07-14 tracey }
518 a596b957 2022-07-14 tracey }
519 a596b957 2022-07-14 tracey
520 a596b957 2022-07-14 tracey void
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)
524 a596b957 2022-07-14 tracey {
525 a596b957 2022-07-14 tracey struct passwd *pw;
526 a596b957 2022-07-14 tracey const char *root;
527 a596b957 2022-07-14 tracey
528 a596b957 2022-07-14 tracey log_procinit(p->p_title);
529 a596b957 2022-07-14 tracey
530 a596b957 2022-07-14 tracey /* Set the process group of the current process */
531 a596b957 2022-07-14 tracey setpgid(0, 0);
532 a596b957 2022-07-14 tracey
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;
536 a596b957 2022-07-14 tracey else
537 a596b957 2022-07-14 tracey pw = ps->ps_pw;
538 a596b957 2022-07-14 tracey
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;
542 a596b957 2022-07-14 tracey else
543 a596b957 2022-07-14 tracey root = pw->pw_dir;
544 a596b957 2022-07-14 tracey
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(\"/\")");
549 a596b957 2022-07-14 tracey
550 a596b957 2022-07-14 tracey privsep_process = p->p_id;
551 a596b957 2022-07-14 tracey
552 a596b957 2022-07-14 tracey setproctitle("%s", p->p_title);
553 a596b957 2022-07-14 tracey
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");
558 a596b957 2022-07-14 tracey
559 a596b957 2022-07-14 tracey event_init();
560 a596b957 2022-07-14 tracey
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);
567 a596b957 2022-07-14 tracey
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);
574 a596b957 2022-07-14 tracey
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);
577 a596b957 2022-07-14 tracey
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());
580 a596b957 2022-07-14 tracey
581 a596b957 2022-07-14 tracey if (run != NULL)
582 a596b957 2022-07-14 tracey run(ps, p, arg);
583 a596b957 2022-07-14 tracey
584 a596b957 2022-07-14 tracey event_dispatch();
585 a596b957 2022-07-14 tracey
586 a596b957 2022-07-14 tracey proc_shutdown(p);
587 a596b957 2022-07-14 tracey }
588 a596b957 2022-07-14 tracey
589 a596b957 2022-07-14 tracey void
590 a596b957 2022-07-14 tracey proc_dispatch(int fd, short event, void *arg)
591 a596b957 2022-07-14 tracey {
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;
601 a596b957 2022-07-14 tracey
602 a596b957 2022-07-14 tracey title = ps->ps_title[privsep_process];
603 a596b957 2022-07-14 tracey ibuf = &iev->ibuf;
604 a596b957 2022-07-14 tracey
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);
613 a596b957 2022-07-14 tracey return;
614 a596b957 2022-07-14 tracey }
615 a596b957 2022-07-14 tracey }
616 a596b957 2022-07-14 tracey
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);
625 a596b957 2022-07-14 tracey return;
626 a596b957 2022-07-14 tracey }
627 a596b957 2022-07-14 tracey }
628 a596b957 2022-07-14 tracey
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)
634 a596b957 2022-07-14 tracey break;
635 a596b957 2022-07-14 tracey
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);
640 a596b957 2022-07-14 tracey #endif
641 a596b957 2022-07-14 tracey
642 a596b957 2022-07-14 tracey /*
643 a596b957 2022-07-14 tracey * Check the message with the program callback
644 a596b957 2022-07-14 tracey */
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;
649 a596b957 2022-07-14 tracey }
650 a596b957 2022-07-14 tracey
651 a596b957 2022-07-14 tracey /*
652 a596b957 2022-07-14 tracey * Generic message handling
653 a596b957 2022-07-14 tracey */
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);
660 a596b957 2022-07-14 tracey break;
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);
666 a596b957 2022-07-14 tracey break;
667 a596b957 2022-07-14 tracey default:
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);
673 a596b957 2022-07-14 tracey }
674 a596b957 2022-07-14 tracey imsg_free(&imsg);
675 a596b957 2022-07-14 tracey }
676 a596b957 2022-07-14 tracey imsg_event_add(iev);
677 a596b957 2022-07-14 tracey }
678 a596b957 2022-07-14 tracey
679 a596b957 2022-07-14 tracey int
680 a596b957 2022-07-14 tracey proc_dispatch_null(int fd, struct privsep_proc *p, struct imsg *imsg)
681 a596b957 2022-07-14 tracey {
682 a596b957 2022-07-14 tracey return (-1);
683 a596b957 2022-07-14 tracey }
684 a596b957 2022-07-14 tracey
685 a596b957 2022-07-14 tracey /*
686 a596b957 2022-07-14 tracey * imsg helper functions
687 a596b957 2022-07-14 tracey */
688 a596b957 2022-07-14 tracey
689 a596b957 2022-07-14 tracey void
690 a596b957 2022-07-14 tracey imsg_event_add(struct imsgev *iev)
691 a596b957 2022-07-14 tracey {
692 a596b957 2022-07-14 tracey if (iev->handler == NULL) {
693 a596b957 2022-07-14 tracey imsg_flush(&iev->ibuf);
694 a596b957 2022-07-14 tracey return;
695 a596b957 2022-07-14 tracey }
696 a596b957 2022-07-14 tracey
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;
700 a596b957 2022-07-14 tracey
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);
704 a596b957 2022-07-14 tracey }
705 a596b957 2022-07-14 tracey
706 a596b957 2022-07-14 tracey int
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)
709 a596b957 2022-07-14 tracey {
710 a596b957 2022-07-14 tracey int ret;
711 a596b957 2022-07-14 tracey
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);
717 a596b957 2022-07-14 tracey }
718 a596b957 2022-07-14 tracey
719 a596b957 2022-07-14 tracey int
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)
722 a596b957 2022-07-14 tracey {
723 a596b957 2022-07-14 tracey int ret;
724 a596b957 2022-07-14 tracey
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);
730 a596b957 2022-07-14 tracey }
731 a596b957 2022-07-14 tracey
732 a596b957 2022-07-14 tracey void
733 a596b957 2022-07-14 tracey proc_range(struct privsep *ps, enum privsep_procid id, int *n, int *m)
734 a596b957 2022-07-14 tracey {
735 a596b957 2022-07-14 tracey if (*n == -1) {
736 a596b957 2022-07-14 tracey /* Use a range of all target instances */
737 a596b957 2022-07-14 tracey *n = 0;
738 a596b957 2022-07-14 tracey *m = ps->ps_instances[id];
739 a596b957 2022-07-14 tracey } else {
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;
742 a596b957 2022-07-14 tracey }
743 a596b957 2022-07-14 tracey }
744 a596b957 2022-07-14 tracey
745 a596b957 2022-07-14 tracey int
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)
748 a596b957 2022-07-14 tracey {
749 a596b957 2022-07-14 tracey int m;
750 a596b957 2022-07-14 tracey
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);
756 a596b957 2022-07-14 tracey }
757 a596b957 2022-07-14 tracey
758 a596b957 2022-07-14 tracey return (0);
759 a596b957 2022-07-14 tracey }
760 a596b957 2022-07-14 tracey
761 a596b957 2022-07-14 tracey int
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)
764 a596b957 2022-07-14 tracey {
765 a596b957 2022-07-14 tracey return (proc_compose_imsg(ps, id, -1, type, -1, -1, data, datalen));
766 a596b957 2022-07-14 tracey }
767 a596b957 2022-07-14 tracey
768 a596b957 2022-07-14 tracey int
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)
771 a596b957 2022-07-14 tracey {
772 a596b957 2022-07-14 tracey int m;
773 a596b957 2022-07-14 tracey
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);
779 a596b957 2022-07-14 tracey
780 a596b957 2022-07-14 tracey return (0);
781 a596b957 2022-07-14 tracey }
782 a596b957 2022-07-14 tracey
783 a596b957 2022-07-14 tracey int
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)
786 a596b957 2022-07-14 tracey {
787 a596b957 2022-07-14 tracey return (proc_composev_imsg(ps, id, -1, type, -1, -1, iov, iovcnt));
788 a596b957 2022-07-14 tracey }
789 a596b957 2022-07-14 tracey
790 a596b957 2022-07-14 tracey int
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)
793 a596b957 2022-07-14 tracey {
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)));
796 a596b957 2022-07-14 tracey }
797 a596b957 2022-07-14 tracey
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)
800 a596b957 2022-07-14 tracey {
801 a596b957 2022-07-14 tracey int m;
802 a596b957 2022-07-14 tracey
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);
805 a596b957 2022-07-14 tracey }
806 a596b957 2022-07-14 tracey
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)
809 a596b957 2022-07-14 tracey {
810 a596b957 2022-07-14 tracey int m;
811 a596b957 2022-07-14 tracey
812 a596b957 2022-07-14 tracey proc_range(ps, id, &n, &m);
813 a596b957 2022-07-14 tracey return (&ps->ps_ievs[id][n]);
814 a596b957 2022-07-14 tracey }
815 a596b957 2022-07-14 tracey
816 a596b957 2022-07-14 tracey /* This function should only be called with care as it breaks async I/O */
817 a596b957 2022-07-14 tracey int
818 a596b957 2022-07-14 tracey proc_flush_imsg(struct privsep *ps, enum privsep_procid id, int n)
819 a596b957 2022-07-14 tracey {
820 a596b957 2022-07-14 tracey struct imsgbuf *ibuf;
821 a596b957 2022-07-14 tracey int m, ret = 0;
822 a596b957 2022-07-14 tracey
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);
828 a596b957 2022-07-14 tracey do {
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)
832 a596b957 2022-07-14 tracey break;
833 a596b957 2022-07-14 tracey imsg_event_add(&ps->ps_ievs[id][n]);
834 a596b957 2022-07-14 tracey }
835 a596b957 2022-07-14 tracey
836 a596b957 2022-07-14 tracey return (ret);
837 a596b957 2022-07-14 tracey }