Blame


1 8a35f56c 2022-07-16 thomas /*
2 8a35f56c 2022-07-16 thomas * Copyright (c) 2010 - 2016 Reyk Floeter <reyk@openbsd.org>
3 8a35f56c 2022-07-16 thomas * Copyright (c) 2008 Pierre-Yves Ritschard <pyr@openbsd.org>
4 8a35f56c 2022-07-16 thomas *
5 8a35f56c 2022-07-16 thomas * Permission to use, copy, modify, and distribute this software for any
6 8a35f56c 2022-07-16 thomas * purpose with or without fee is hereby granted, provided that the above
7 8a35f56c 2022-07-16 thomas * copyright notice and this permission notice appear in all copies.
8 8a35f56c 2022-07-16 thomas *
9 8a35f56c 2022-07-16 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 8a35f56c 2022-07-16 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 8a35f56c 2022-07-16 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 8a35f56c 2022-07-16 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 8a35f56c 2022-07-16 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 8a35f56c 2022-07-16 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 8a35f56c 2022-07-16 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 8a35f56c 2022-07-16 thomas */
17 8a35f56c 2022-07-16 thomas
18 8a35f56c 2022-07-16 thomas #include <sys/types.h>
19 8a35f56c 2022-07-16 thomas #include <sys/socket.h>
20 8a35f56c 2022-07-16 thomas #include <sys/wait.h>
21 8a35f56c 2022-07-16 thomas
22 ff36aeea 2022-07-16 thomas /* TA: FIXME */
23 ff36aeea 2022-07-16 thomas #include <grp.h>
24 ff36aeea 2022-07-16 thomas
25 8a35f56c 2022-07-16 thomas #include <fcntl.h>
26 8a35f56c 2022-07-16 thomas #include <stdio.h>
27 8a35f56c 2022-07-16 thomas #include <stdlib.h>
28 8a35f56c 2022-07-16 thomas #include <unistd.h>
29 8a35f56c 2022-07-16 thomas #include <string.h>
30 8a35f56c 2022-07-16 thomas #include <errno.h>
31 8a35f56c 2022-07-16 thomas #include <signal.h>
32 8a35f56c 2022-07-16 thomas #include <pwd.h>
33 8a35f56c 2022-07-16 thomas #include <event.h>
34 8a35f56c 2022-07-16 thomas
35 8a35f56c 2022-07-16 thomas #include "proc.h"
36 8a35f56c 2022-07-16 thomas
37 ff36aeea 2022-07-16 thomas #include "got_compat.h"
38 ff36aeea 2022-07-16 thomas
39 8a35f56c 2022-07-16 thomas void proc_exec(struct privsep *, struct privsep_proc *, unsigned int,
40 8a35f56c 2022-07-16 thomas int, char **);
41 8a35f56c 2022-07-16 thomas void proc_setup(struct privsep *, struct privsep_proc *, unsigned int);
42 8a35f56c 2022-07-16 thomas void proc_open(struct privsep *, int, int);
43 8a35f56c 2022-07-16 thomas void proc_accept(struct privsep *, int, enum privsep_procid,
44 8a35f56c 2022-07-16 thomas unsigned int);
45 8a35f56c 2022-07-16 thomas void proc_close(struct privsep *);
46 8a35f56c 2022-07-16 thomas int proc_ispeer(struct privsep_proc *, unsigned int, enum privsep_procid);
47 8a35f56c 2022-07-16 thomas void proc_shutdown(struct privsep_proc *);
48 8a35f56c 2022-07-16 thomas void proc_sig_handler(int, short, void *);
49 8a35f56c 2022-07-16 thomas int proc_dispatch_null(int, struct privsep_proc *, struct imsg *);
50 8a35f56c 2022-07-16 thomas
51 8a35f56c 2022-07-16 thomas enum privsep_procid privsep_process;
52 8a35f56c 2022-07-16 thomas
53 8a35f56c 2022-07-16 thomas int
54 8a35f56c 2022-07-16 thomas proc_ispeer(struct privsep_proc *procs, unsigned int nproc,
55 8a35f56c 2022-07-16 thomas enum privsep_procid type)
56 8a35f56c 2022-07-16 thomas {
57 8a35f56c 2022-07-16 thomas unsigned int i;
58 8a35f56c 2022-07-16 thomas
59 8a35f56c 2022-07-16 thomas for (i = 0; i < nproc; i++)
60 8a35f56c 2022-07-16 thomas if (procs[i].p_id == type)
61 8a35f56c 2022-07-16 thomas return (1);
62 8a35f56c 2022-07-16 thomas
63 8a35f56c 2022-07-16 thomas return (0);
64 8a35f56c 2022-07-16 thomas }
65 8a35f56c 2022-07-16 thomas
66 8a35f56c 2022-07-16 thomas enum privsep_procid
67 8a35f56c 2022-07-16 thomas proc_getid(struct privsep_proc *procs, unsigned int nproc,
68 8a35f56c 2022-07-16 thomas const char *proc_name)
69 8a35f56c 2022-07-16 thomas {
70 8a35f56c 2022-07-16 thomas struct privsep_proc *p;
71 8a35f56c 2022-07-16 thomas unsigned int proc;
72 8a35f56c 2022-07-16 thomas
73 8a35f56c 2022-07-16 thomas for (proc = 0; proc < nproc; proc++) {
74 8a35f56c 2022-07-16 thomas p = &procs[proc];
75 8a35f56c 2022-07-16 thomas if (strcmp(p->p_title, proc_name))
76 8a35f56c 2022-07-16 thomas continue;
77 8a35f56c 2022-07-16 thomas
78 8a35f56c 2022-07-16 thomas return (p->p_id);
79 8a35f56c 2022-07-16 thomas }
80 8a35f56c 2022-07-16 thomas
81 8a35f56c 2022-07-16 thomas return (PROC_MAX);
82 8a35f56c 2022-07-16 thomas }
83 8a35f56c 2022-07-16 thomas
84 8a35f56c 2022-07-16 thomas void
85 8a35f56c 2022-07-16 thomas proc_exec(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
86 8a35f56c 2022-07-16 thomas int argc, char **argv)
87 8a35f56c 2022-07-16 thomas {
88 8a35f56c 2022-07-16 thomas unsigned int proc, nargc, i, proc_i;
89 8a35f56c 2022-07-16 thomas char **nargv;
90 8a35f56c 2022-07-16 thomas struct privsep_proc *p;
91 8a35f56c 2022-07-16 thomas char num[32];
92 8a35f56c 2022-07-16 thomas int fd;
93 8a35f56c 2022-07-16 thomas
94 8a35f56c 2022-07-16 thomas /* Prepare the new process argv. */
95 8a35f56c 2022-07-16 thomas nargv = calloc(argc + 5, sizeof(char *));
96 8a35f56c 2022-07-16 thomas if (nargv == NULL)
97 8a35f56c 2022-07-16 thomas fatal("%s: calloc", __func__);
98 8a35f56c 2022-07-16 thomas
99 8a35f56c 2022-07-16 thomas /* Copy call argument first. */
100 8a35f56c 2022-07-16 thomas nargc = 0;
101 8a35f56c 2022-07-16 thomas nargv[nargc++] = argv[0];
102 8a35f56c 2022-07-16 thomas
103 8a35f56c 2022-07-16 thomas /* Set process name argument and save the position. */
104 8a35f56c 2022-07-16 thomas nargv[nargc] = strdup("-P");
105 8a35f56c 2022-07-16 thomas if (nargv[nargc] == NULL)
106 8a35f56c 2022-07-16 thomas fatal("%s: strdup", __func__);
107 8a35f56c 2022-07-16 thomas nargc++;
108 8a35f56c 2022-07-16 thomas proc_i = nargc;
109 8a35f56c 2022-07-16 thomas nargc++;
110 8a35f56c 2022-07-16 thomas
111 8a35f56c 2022-07-16 thomas /* Point process instance arg to stack and copy the original args. */
112 8a35f56c 2022-07-16 thomas nargv[nargc] = strdup("-I");
113 8a35f56c 2022-07-16 thomas if (nargv[nargc] == NULL)
114 8a35f56c 2022-07-16 thomas fatal("%s: strdup", __func__);
115 8a35f56c 2022-07-16 thomas nargc++;
116 8a35f56c 2022-07-16 thomas nargv[nargc++] = num;
117 8a35f56c 2022-07-16 thomas for (i = 1; i < (unsigned int) argc; i++)
118 8a35f56c 2022-07-16 thomas nargv[nargc++] = argv[i];
119 8a35f56c 2022-07-16 thomas
120 8a35f56c 2022-07-16 thomas nargv[nargc] = NULL;
121 8a35f56c 2022-07-16 thomas
122 8a35f56c 2022-07-16 thomas for (proc = 0; proc < nproc; proc++) {
123 8a35f56c 2022-07-16 thomas p = &procs[proc];
124 8a35f56c 2022-07-16 thomas
125 8a35f56c 2022-07-16 thomas /* Update args with process title. */
126 8a35f56c 2022-07-16 thomas nargv[proc_i] = (char *)(uintptr_t)p->p_title;
127 8a35f56c 2022-07-16 thomas
128 8a35f56c 2022-07-16 thomas /* Fire children processes. */
129 8a35f56c 2022-07-16 thomas for (i = 0; i < ps->ps_instances[p->p_id]; i++) {
130 8a35f56c 2022-07-16 thomas /* Update the process instance number. */
131 8a35f56c 2022-07-16 thomas snprintf(num, sizeof(num), "%u", i);
132 8a35f56c 2022-07-16 thomas
133 8a35f56c 2022-07-16 thomas fd = ps->ps_pipes[p->p_id][i].pp_pipes[PROC_GOTWEBD][0];
134 8a35f56c 2022-07-16 thomas ps->ps_pipes[p->p_id][i].pp_pipes[PROC_GOTWEBD][0] = -1;
135 8a35f56c 2022-07-16 thomas
136 8a35f56c 2022-07-16 thomas switch (fork()) {
137 8a35f56c 2022-07-16 thomas case -1:
138 8a35f56c 2022-07-16 thomas fatal("%s: fork", __func__);
139 8a35f56c 2022-07-16 thomas break;
140 8a35f56c 2022-07-16 thomas case 0:
141 8a35f56c 2022-07-16 thomas /* First create a new session */
142 8a35f56c 2022-07-16 thomas if (setsid() == -1)
143 8a35f56c 2022-07-16 thomas fatal("setsid");
144 8a35f56c 2022-07-16 thomas
145 8a35f56c 2022-07-16 thomas /* Prepare parent socket. */
146 8a35f56c 2022-07-16 thomas if (fd != PROC_GOTWEBD_SOCK_FILENO) {
147 8a35f56c 2022-07-16 thomas if (dup2(fd, PROC_GOTWEBD_SOCK_FILENO)
148 8a35f56c 2022-07-16 thomas == -1)
149 8a35f56c 2022-07-16 thomas fatal("dup2");
150 8a35f56c 2022-07-16 thomas } else if (fcntl(fd, F_SETFD, 0) == -1)
151 8a35f56c 2022-07-16 thomas fatal("fcntl");
152 8a35f56c 2022-07-16 thomas
153 8a35f56c 2022-07-16 thomas execvp(argv[0], nargv);
154 8a35f56c 2022-07-16 thomas fatal("%s: execvp", __func__);
155 8a35f56c 2022-07-16 thomas break;
156 8a35f56c 2022-07-16 thomas default:
157 8a35f56c 2022-07-16 thomas /* Close child end. */
158 8a35f56c 2022-07-16 thomas close(fd);
159 8a35f56c 2022-07-16 thomas break;
160 8a35f56c 2022-07-16 thomas }
161 8a35f56c 2022-07-16 thomas }
162 8a35f56c 2022-07-16 thomas }
163 8a35f56c 2022-07-16 thomas
164 8a35f56c 2022-07-16 thomas free(nargv);
165 8a35f56c 2022-07-16 thomas }
166 8a35f56c 2022-07-16 thomas
167 8a35f56c 2022-07-16 thomas void
168 8a35f56c 2022-07-16 thomas proc_connect(struct privsep *ps)
169 8a35f56c 2022-07-16 thomas {
170 8a35f56c 2022-07-16 thomas struct imsgev *iev;
171 8a35f56c 2022-07-16 thomas unsigned int src, dst, inst;
172 8a35f56c 2022-07-16 thomas
173 8a35f56c 2022-07-16 thomas /* Don't distribute any sockets if we are not really going to run. */
174 8a35f56c 2022-07-16 thomas if (ps->ps_noaction)
175 8a35f56c 2022-07-16 thomas return;
176 8a35f56c 2022-07-16 thomas
177 8a35f56c 2022-07-16 thomas for (dst = 0; dst < PROC_MAX; dst++) {
178 8a35f56c 2022-07-16 thomas /* We don't communicate with ourselves. */
179 8a35f56c 2022-07-16 thomas if (dst == PROC_GOTWEBD)
180 8a35f56c 2022-07-16 thomas continue;
181 8a35f56c 2022-07-16 thomas
182 8a35f56c 2022-07-16 thomas for (inst = 0; inst < ps->ps_instances[dst]; inst++) {
183 8a35f56c 2022-07-16 thomas iev = &ps->ps_ievs[dst][inst];
184 8a35f56c 2022-07-16 thomas imsg_init(&iev->ibuf, ps->ps_pp->pp_pipes[dst][inst]);
185 8a35f56c 2022-07-16 thomas event_set(&iev->ev, iev->ibuf.fd, iev->events,
186 8a35f56c 2022-07-16 thomas iev->handler, iev->data);
187 8a35f56c 2022-07-16 thomas event_add(&iev->ev, NULL);
188 8a35f56c 2022-07-16 thomas }
189 8a35f56c 2022-07-16 thomas }
190 8a35f56c 2022-07-16 thomas
191 8a35f56c 2022-07-16 thomas /* Distribute the socketpair()s for everyone. */
192 8a35f56c 2022-07-16 thomas for (src = 0; src < PROC_MAX; src++)
193 8a35f56c 2022-07-16 thomas for (dst = src; dst < PROC_MAX; dst++) {
194 8a35f56c 2022-07-16 thomas /* Parent already distributed its fds. */
195 8a35f56c 2022-07-16 thomas if (src == PROC_GOTWEBD || dst == PROC_GOTWEBD)
196 8a35f56c 2022-07-16 thomas continue;
197 8a35f56c 2022-07-16 thomas
198 8a35f56c 2022-07-16 thomas proc_open(ps, src, dst);
199 8a35f56c 2022-07-16 thomas }
200 8a35f56c 2022-07-16 thomas }
201 8a35f56c 2022-07-16 thomas
202 8a35f56c 2022-07-16 thomas void
203 8a35f56c 2022-07-16 thomas proc_init(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
204 8a35f56c 2022-07-16 thomas int argc, char **argv, enum privsep_procid proc_id)
205 8a35f56c 2022-07-16 thomas {
206 8a35f56c 2022-07-16 thomas struct privsep_proc *p = NULL;
207 8a35f56c 2022-07-16 thomas struct privsep_pipes *pa, *pb;
208 8a35f56c 2022-07-16 thomas unsigned int proc;
209 8a35f56c 2022-07-16 thomas unsigned int dst;
210 8a35f56c 2022-07-16 thomas int fds[2];
211 8a35f56c 2022-07-16 thomas
212 8a35f56c 2022-07-16 thomas /* Don't initiate anything if we are not really going to run. */
213 8a35f56c 2022-07-16 thomas if (ps->ps_noaction)
214 8a35f56c 2022-07-16 thomas return;
215 8a35f56c 2022-07-16 thomas
216 8a35f56c 2022-07-16 thomas if (proc_id == PROC_GOTWEBD) {
217 8a35f56c 2022-07-16 thomas privsep_process = PROC_GOTWEBD;
218 8a35f56c 2022-07-16 thomas proc_setup(ps, procs, nproc);
219 8a35f56c 2022-07-16 thomas
220 8a35f56c 2022-07-16 thomas /*
221 8a35f56c 2022-07-16 thomas * Create the children sockets so we can use them
222 8a35f56c 2022-07-16 thomas * to distribute the rest of the socketpair()s using
223 8a35f56c 2022-07-16 thomas * proc_connect() later.
224 8a35f56c 2022-07-16 thomas */
225 8a35f56c 2022-07-16 thomas for (dst = 0; dst < PROC_MAX; dst++) {
226 8a35f56c 2022-07-16 thomas /* Don't create socket for ourselves. */
227 8a35f56c 2022-07-16 thomas if (dst == PROC_GOTWEBD)
228 8a35f56c 2022-07-16 thomas continue;
229 8a35f56c 2022-07-16 thomas
230 8a35f56c 2022-07-16 thomas for (proc = 0; proc < ps->ps_instances[dst]; proc++) {
231 8a35f56c 2022-07-16 thomas pa = &ps->ps_pipes[PROC_GOTWEBD][0];
232 8a35f56c 2022-07-16 thomas pb = &ps->ps_pipes[dst][proc];
233 dd7a7d5b 2022-07-16 thomas int sock_flags = SOCK_STREAM | SOCK_NONBLOCK;
234 dd7a7d5b 2022-07-16 thomas #ifdef SOCK_CLOEXEC
235 dd7a7d5b 2022-07-16 thomas sock_flags |= SOCK_CLOEXEC;
236 dd7a7d5b 2022-07-16 thomas #endif
237 dd7a7d5b 2022-07-16 thomas if (socketpair(AF_UNIX, sock_flags,
238 8a35f56c 2022-07-16 thomas PF_UNSPEC, fds) == -1)
239 8a35f56c 2022-07-16 thomas fatal("%s: socketpair", __func__);
240 8a35f56c 2022-07-16 thomas
241 8a35f56c 2022-07-16 thomas pa->pp_pipes[dst][proc] = fds[0];
242 8a35f56c 2022-07-16 thomas pb->pp_pipes[PROC_GOTWEBD][0] = fds[1];
243 8a35f56c 2022-07-16 thomas }
244 8a35f56c 2022-07-16 thomas }
245 8a35f56c 2022-07-16 thomas
246 8a35f56c 2022-07-16 thomas /* Engage! */
247 8a35f56c 2022-07-16 thomas proc_exec(ps, procs, nproc, argc, argv);
248 8a35f56c 2022-07-16 thomas return;
249 8a35f56c 2022-07-16 thomas }
250 8a35f56c 2022-07-16 thomas
251 8a35f56c 2022-07-16 thomas /* Initialize a child */
252 8a35f56c 2022-07-16 thomas for (proc = 0; proc < nproc; proc++) {
253 8a35f56c 2022-07-16 thomas if (procs[proc].p_id != proc_id)
254 8a35f56c 2022-07-16 thomas continue;
255 8a35f56c 2022-07-16 thomas p = &procs[proc];
256 8a35f56c 2022-07-16 thomas break;
257 8a35f56c 2022-07-16 thomas }
258 8a35f56c 2022-07-16 thomas if (p == NULL || p->p_init == NULL)
259 8a35f56c 2022-07-16 thomas fatalx("%s: process %d missing process initialization",
260 8a35f56c 2022-07-16 thomas __func__, proc_id);
261 8a35f56c 2022-07-16 thomas
262 8a35f56c 2022-07-16 thomas p->p_init(ps, p);
263 8a35f56c 2022-07-16 thomas
264 8a35f56c 2022-07-16 thomas fatalx("failed to initiate child process");
265 8a35f56c 2022-07-16 thomas }
266 8a35f56c 2022-07-16 thomas
267 8a35f56c 2022-07-16 thomas void
268 8a35f56c 2022-07-16 thomas proc_accept(struct privsep *ps, int fd, enum privsep_procid dst,
269 8a35f56c 2022-07-16 thomas unsigned int n)
270 8a35f56c 2022-07-16 thomas {
271 8a35f56c 2022-07-16 thomas struct privsep_pipes *pp = ps->ps_pp;
272 8a35f56c 2022-07-16 thomas struct imsgev *iev;
273 8a35f56c 2022-07-16 thomas
274 8a35f56c 2022-07-16 thomas if (ps->ps_ievs[dst] == NULL) {
275 8a35f56c 2022-07-16 thomas #if DEBUG > 1
276 8a35f56c 2022-07-16 thomas log_debug("%s: %s src %d %d to dst %d %d not connected",
277 8a35f56c 2022-07-16 thomas __func__, ps->ps_title[privsep_process],
278 8a35f56c 2022-07-16 thomas privsep_process, ps->ps_instance + 1,
279 8a35f56c 2022-07-16 thomas dst, n + 1);
280 8a35f56c 2022-07-16 thomas #endif
281 8a35f56c 2022-07-16 thomas close(fd);
282 8a35f56c 2022-07-16 thomas return;
283 8a35f56c 2022-07-16 thomas }
284 8a35f56c 2022-07-16 thomas
285 8a35f56c 2022-07-16 thomas if (pp->pp_pipes[dst][n] != -1) {
286 8a35f56c 2022-07-16 thomas log_warnx("%s: duplicated descriptor", __func__);
287 8a35f56c 2022-07-16 thomas close(fd);
288 8a35f56c 2022-07-16 thomas return;
289 8a35f56c 2022-07-16 thomas } else
290 8a35f56c 2022-07-16 thomas pp->pp_pipes[dst][n] = fd;
291 8a35f56c 2022-07-16 thomas
292 8a35f56c 2022-07-16 thomas iev = &ps->ps_ievs[dst][n];
293 8a35f56c 2022-07-16 thomas imsg_init(&iev->ibuf, fd);
294 8a35f56c 2022-07-16 thomas event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
295 8a35f56c 2022-07-16 thomas event_add(&iev->ev, NULL);
296 8a35f56c 2022-07-16 thomas }
297 8a35f56c 2022-07-16 thomas
298 8a35f56c 2022-07-16 thomas void
299 8a35f56c 2022-07-16 thomas proc_setup(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc)
300 8a35f56c 2022-07-16 thomas {
301 8a35f56c 2022-07-16 thomas unsigned int i, j, src, dst, id;
302 8a35f56c 2022-07-16 thomas struct privsep_pipes *pp;
303 8a35f56c 2022-07-16 thomas
304 8a35f56c 2022-07-16 thomas /* Initialize parent title, ps_instances and procs. */
305 8a35f56c 2022-07-16 thomas ps->ps_title[PROC_GOTWEBD] = "parent";
306 8a35f56c 2022-07-16 thomas
307 8a35f56c 2022-07-16 thomas for (src = 0; src < PROC_MAX; src++)
308 8a35f56c 2022-07-16 thomas /* Default to 1 process instance */
309 8a35f56c 2022-07-16 thomas if (ps->ps_instances[src] < 1)
310 8a35f56c 2022-07-16 thomas ps->ps_instances[src] = 1;
311 8a35f56c 2022-07-16 thomas
312 8a35f56c 2022-07-16 thomas for (src = 0; src < nproc; src++) {
313 8a35f56c 2022-07-16 thomas procs[src].p_ps = ps;
314 8a35f56c 2022-07-16 thomas if (procs[src].p_cb == NULL)
315 8a35f56c 2022-07-16 thomas procs[src].p_cb = proc_dispatch_null;
316 8a35f56c 2022-07-16 thomas
317 8a35f56c 2022-07-16 thomas id = procs[src].p_id;
318 8a35f56c 2022-07-16 thomas ps->ps_title[id] = procs[src].p_title;
319 8a35f56c 2022-07-16 thomas ps->ps_ievs[id] = calloc(ps->ps_instances[id],
320 8a35f56c 2022-07-16 thomas sizeof(struct imsgev));
321 8a35f56c 2022-07-16 thomas if (ps->ps_ievs[id] == NULL)
322 8a35f56c 2022-07-16 thomas fatal("%s: calloc", __func__);
323 8a35f56c 2022-07-16 thomas
324 8a35f56c 2022-07-16 thomas /* With this set up, we are ready to call imsg_init(). */
325 8a35f56c 2022-07-16 thomas for (i = 0; i < ps->ps_instances[id]; i++) {
326 8a35f56c 2022-07-16 thomas ps->ps_ievs[id][i].handler = proc_dispatch;
327 8a35f56c 2022-07-16 thomas ps->ps_ievs[id][i].events = EV_READ;
328 8a35f56c 2022-07-16 thomas ps->ps_ievs[id][i].proc = &procs[src];
329 8a35f56c 2022-07-16 thomas ps->ps_ievs[id][i].data = &ps->ps_ievs[id][i];
330 8a35f56c 2022-07-16 thomas }
331 8a35f56c 2022-07-16 thomas }
332 8a35f56c 2022-07-16 thomas
333 8a35f56c 2022-07-16 thomas /*
334 8a35f56c 2022-07-16 thomas * Allocate pipes for all process instances (incl. parent)
335 8a35f56c 2022-07-16 thomas *
336 8a35f56c 2022-07-16 thomas * - ps->ps_pipes: N:M mapping
337 8a35f56c 2022-07-16 thomas * N source processes connected to M destination processes:
338 8a35f56c 2022-07-16 thomas * [src][instances][dst][instances], for example
339 8a35f56c 2022-07-16 thomas * [PROC_RELAY][3][PROC_CA][3]
340 8a35f56c 2022-07-16 thomas *
341 8a35f56c 2022-07-16 thomas * - ps->ps_pp: per-process 1:M part of ps->ps_pipes
342 8a35f56c 2022-07-16 thomas * Each process instance has a destination array of socketpair fds:
343 8a35f56c 2022-07-16 thomas * [dst][instances], for example
344 8a35f56c 2022-07-16 thomas * [PROC_GOTWEBD][0]
345 8a35f56c 2022-07-16 thomas */
346 8a35f56c 2022-07-16 thomas for (src = 0; src < PROC_MAX; src++) {
347 8a35f56c 2022-07-16 thomas /* Allocate destination array for each process */
348 8a35f56c 2022-07-16 thomas ps->ps_pipes[src] = calloc(ps->ps_instances[src],
349 8a35f56c 2022-07-16 thomas sizeof(struct privsep_pipes));
350 8a35f56c 2022-07-16 thomas if (ps->ps_pipes[src] == NULL)
351 8a35f56c 2022-07-16 thomas fatal("%s: calloc", __func__);
352 8a35f56c 2022-07-16 thomas
353 8a35f56c 2022-07-16 thomas for (i = 0; i < ps->ps_instances[src]; i++) {
354 8a35f56c 2022-07-16 thomas pp = &ps->ps_pipes[src][i];
355 8a35f56c 2022-07-16 thomas
356 8a35f56c 2022-07-16 thomas for (dst = 0; dst < PROC_MAX; dst++) {
357 8a35f56c 2022-07-16 thomas /* Allocate maximum fd integers */
358 8a35f56c 2022-07-16 thomas pp->pp_pipes[dst] =
359 8a35f56c 2022-07-16 thomas calloc(ps->ps_instances[dst],
360 8a35f56c 2022-07-16 thomas sizeof(int));
361 8a35f56c 2022-07-16 thomas if (pp->pp_pipes[dst] == NULL)
362 8a35f56c 2022-07-16 thomas fatal("%s: calloc", __func__);
363 8a35f56c 2022-07-16 thomas
364 8a35f56c 2022-07-16 thomas /* Mark fd as unused */
365 8a35f56c 2022-07-16 thomas for (j = 0; j < ps->ps_instances[dst]; j++)
366 8a35f56c 2022-07-16 thomas pp->pp_pipes[dst][j] = -1;
367 8a35f56c 2022-07-16 thomas }
368 8a35f56c 2022-07-16 thomas }
369 8a35f56c 2022-07-16 thomas }
370 8a35f56c 2022-07-16 thomas
371 8a35f56c 2022-07-16 thomas ps->ps_pp = &ps->ps_pipes[privsep_process][ps->ps_instance];
372 8a35f56c 2022-07-16 thomas }
373 8a35f56c 2022-07-16 thomas
374 8a35f56c 2022-07-16 thomas void
375 8a35f56c 2022-07-16 thomas proc_kill(struct privsep *ps)
376 8a35f56c 2022-07-16 thomas {
377 8a35f56c 2022-07-16 thomas char *cause;
378 8a35f56c 2022-07-16 thomas pid_t pid;
379 8a35f56c 2022-07-16 thomas int len, status;
380 8a35f56c 2022-07-16 thomas
381 8a35f56c 2022-07-16 thomas if (privsep_process != PROC_GOTWEBD)
382 8a35f56c 2022-07-16 thomas return;
383 8a35f56c 2022-07-16 thomas
384 8a35f56c 2022-07-16 thomas proc_close(ps);
385 8a35f56c 2022-07-16 thomas
386 8a35f56c 2022-07-16 thomas do {
387 8a35f56c 2022-07-16 thomas pid = waitpid(WAIT_ANY, &status, 0);
388 8a35f56c 2022-07-16 thomas if (pid <= 0)
389 8a35f56c 2022-07-16 thomas continue;
390 8a35f56c 2022-07-16 thomas
391 8a35f56c 2022-07-16 thomas if (WIFSIGNALED(status)) {
392 8a35f56c 2022-07-16 thomas len = asprintf(&cause, "terminated; signal %d",
393 8a35f56c 2022-07-16 thomas WTERMSIG(status));
394 8a35f56c 2022-07-16 thomas } else if (WIFEXITED(status)) {
395 8a35f56c 2022-07-16 thomas if (WEXITSTATUS(status) != 0)
396 8a35f56c 2022-07-16 thomas len = asprintf(&cause, "exited abnormally");
397 8a35f56c 2022-07-16 thomas else
398 8a35f56c 2022-07-16 thomas len = 0;
399 8a35f56c 2022-07-16 thomas } else
400 8a35f56c 2022-07-16 thomas len = -1;
401 8a35f56c 2022-07-16 thomas
402 8a35f56c 2022-07-16 thomas if (len == 0) {
403 8a35f56c 2022-07-16 thomas /* child exited OK, don't print a warning message */
404 8a35f56c 2022-07-16 thomas } else if (len != -1) {
405 8a35f56c 2022-07-16 thomas log_warnx("lost child: pid %u %s", pid, cause);
406 8a35f56c 2022-07-16 thomas free(cause);
407 8a35f56c 2022-07-16 thomas } else
408 8a35f56c 2022-07-16 thomas log_warnx("lost child: pid %u", pid);
409 8a35f56c 2022-07-16 thomas } while (pid != -1 || (pid == -1 && errno == EINTR));
410 8a35f56c 2022-07-16 thomas }
411 8a35f56c 2022-07-16 thomas
412 8a35f56c 2022-07-16 thomas void
413 8a35f56c 2022-07-16 thomas proc_open(struct privsep *ps, int src, int dst)
414 8a35f56c 2022-07-16 thomas {
415 8a35f56c 2022-07-16 thomas struct privsep_pipes *pa, *pb;
416 8a35f56c 2022-07-16 thomas struct privsep_fd pf;
417 8a35f56c 2022-07-16 thomas int fds[2];
418 8a35f56c 2022-07-16 thomas unsigned int i, j;
419 8a35f56c 2022-07-16 thomas
420 8a35f56c 2022-07-16 thomas /* Exchange pipes between process. */
421 8a35f56c 2022-07-16 thomas for (i = 0; i < ps->ps_instances[src]; i++) {
422 8a35f56c 2022-07-16 thomas for (j = 0; j < ps->ps_instances[dst]; j++) {
423 8a35f56c 2022-07-16 thomas /* Don't create sockets for ourself. */
424 8a35f56c 2022-07-16 thomas if (src == dst && i == j)
425 8a35f56c 2022-07-16 thomas continue;
426 8a35f56c 2022-07-16 thomas
427 8a35f56c 2022-07-16 thomas pa = &ps->ps_pipes[src][i];
428 8a35f56c 2022-07-16 thomas pb = &ps->ps_pipes[dst][j];
429 dd7a7d5b 2022-07-16 thomas int sock_flags = SOCK_STREAM | SOCK_NONBLOCK;
430 dd7a7d5b 2022-07-16 thomas #ifdef SOCK_CLOEXEC
431 dd7a7d5b 2022-07-16 thomas sock_flags |= SOCK_CLOEXEC;
432 dd7a7d5b 2022-07-16 thomas #endif
433 dd7a7d5b 2022-07-16 thomas if (socketpair(AF_UNIX, sock_flags,
434 8a35f56c 2022-07-16 thomas PF_UNSPEC, fds) == -1)
435 8a35f56c 2022-07-16 thomas fatal("%s: socketpair", __func__);
436 8a35f56c 2022-07-16 thomas
437 8a35f56c 2022-07-16 thomas pa->pp_pipes[dst][j] = fds[0];
438 8a35f56c 2022-07-16 thomas pb->pp_pipes[src][i] = fds[1];
439 8a35f56c 2022-07-16 thomas
440 8a35f56c 2022-07-16 thomas pf.pf_procid = src;
441 8a35f56c 2022-07-16 thomas pf.pf_instance = i;
442 8a35f56c 2022-07-16 thomas if (proc_compose_imsg(ps, dst, j, IMSG_CTL_PROCFD,
443 8a35f56c 2022-07-16 thomas -1, pb->pp_pipes[src][i], &pf, sizeof(pf)) == -1)
444 8a35f56c 2022-07-16 thomas fatal("%s: proc_compose_imsg", __func__);
445 8a35f56c 2022-07-16 thomas
446 8a35f56c 2022-07-16 thomas pf.pf_procid = dst;
447 8a35f56c 2022-07-16 thomas pf.pf_instance = j;
448 8a35f56c 2022-07-16 thomas if (proc_compose_imsg(ps, src, i, IMSG_CTL_PROCFD,
449 8a35f56c 2022-07-16 thomas -1, pa->pp_pipes[dst][j], &pf, sizeof(pf)) == -1)
450 8a35f56c 2022-07-16 thomas fatal("%s: proc_compose_imsg", __func__);
451 8a35f56c 2022-07-16 thomas
452 8a35f56c 2022-07-16 thomas /*
453 8a35f56c 2022-07-16 thomas * We have to flush to send the descriptors and close
454 8a35f56c 2022-07-16 thomas * them to avoid the fd ramp on startup.
455 8a35f56c 2022-07-16 thomas */
456 8a35f56c 2022-07-16 thomas if (proc_flush_imsg(ps, src, i) == -1 ||
457 8a35f56c 2022-07-16 thomas proc_flush_imsg(ps, dst, j) == -1)
458 8a35f56c 2022-07-16 thomas fatal("%s: imsg_flush", __func__);
459 8a35f56c 2022-07-16 thomas }
460 8a35f56c 2022-07-16 thomas }
461 8a35f56c 2022-07-16 thomas }
462 8a35f56c 2022-07-16 thomas
463 8a35f56c 2022-07-16 thomas void
464 8a35f56c 2022-07-16 thomas proc_close(struct privsep *ps)
465 8a35f56c 2022-07-16 thomas {
466 8a35f56c 2022-07-16 thomas unsigned int dst, n;
467 8a35f56c 2022-07-16 thomas struct privsep_pipes *pp;
468 8a35f56c 2022-07-16 thomas
469 8a35f56c 2022-07-16 thomas if (ps == NULL)
470 8a35f56c 2022-07-16 thomas return;
471 8a35f56c 2022-07-16 thomas
472 8a35f56c 2022-07-16 thomas pp = ps->ps_pp;
473 8a35f56c 2022-07-16 thomas
474 8a35f56c 2022-07-16 thomas for (dst = 0; dst < PROC_MAX; dst++) {
475 8a35f56c 2022-07-16 thomas if (ps->ps_ievs[dst] == NULL)
476 8a35f56c 2022-07-16 thomas continue;
477 8a35f56c 2022-07-16 thomas
478 8a35f56c 2022-07-16 thomas for (n = 0; n < ps->ps_instances[dst]; n++) {
479 8a35f56c 2022-07-16 thomas if (pp->pp_pipes[dst][n] == -1)
480 8a35f56c 2022-07-16 thomas continue;
481 8a35f56c 2022-07-16 thomas
482 8a35f56c 2022-07-16 thomas /* Cancel the fd, close and invalidate the fd */
483 8a35f56c 2022-07-16 thomas event_del(&(ps->ps_ievs[dst][n].ev));
484 8a35f56c 2022-07-16 thomas imsg_clear(&(ps->ps_ievs[dst][n].ibuf));
485 8a35f56c 2022-07-16 thomas close(pp->pp_pipes[dst][n]);
486 8a35f56c 2022-07-16 thomas pp->pp_pipes[dst][n] = -1;
487 8a35f56c 2022-07-16 thomas }
488 8a35f56c 2022-07-16 thomas free(ps->ps_ievs[dst]);
489 8a35f56c 2022-07-16 thomas }
490 8a35f56c 2022-07-16 thomas }
491 8a35f56c 2022-07-16 thomas
492 8a35f56c 2022-07-16 thomas void
493 8a35f56c 2022-07-16 thomas proc_shutdown(struct privsep_proc *p)
494 8a35f56c 2022-07-16 thomas {
495 8a35f56c 2022-07-16 thomas struct privsep *ps = p->p_ps;
496 8a35f56c 2022-07-16 thomas
497 8a35f56c 2022-07-16 thomas if (p->p_shutdown != NULL)
498 8a35f56c 2022-07-16 thomas (*p->p_shutdown)();
499 8a35f56c 2022-07-16 thomas
500 8a35f56c 2022-07-16 thomas proc_close(ps);
501 8a35f56c 2022-07-16 thomas
502 8a35f56c 2022-07-16 thomas log_info("%s, %s exiting, pid %d", getprogname(), p->p_title, getpid());
503 8a35f56c 2022-07-16 thomas
504 8a35f56c 2022-07-16 thomas exit(0);
505 8a35f56c 2022-07-16 thomas }
506 8a35f56c 2022-07-16 thomas
507 8a35f56c 2022-07-16 thomas void
508 8a35f56c 2022-07-16 thomas proc_sig_handler(int sig, short event, void *arg)
509 8a35f56c 2022-07-16 thomas {
510 8a35f56c 2022-07-16 thomas struct privsep_proc *p = arg;
511 8a35f56c 2022-07-16 thomas
512 8a35f56c 2022-07-16 thomas switch (sig) {
513 8a35f56c 2022-07-16 thomas case SIGINT:
514 8a35f56c 2022-07-16 thomas case SIGTERM:
515 8a35f56c 2022-07-16 thomas proc_shutdown(p);
516 8a35f56c 2022-07-16 thomas break;
517 8a35f56c 2022-07-16 thomas case SIGCHLD:
518 8a35f56c 2022-07-16 thomas case SIGHUP:
519 8a35f56c 2022-07-16 thomas case SIGPIPE:
520 8a35f56c 2022-07-16 thomas case SIGUSR1:
521 8a35f56c 2022-07-16 thomas /* ignore */
522 8a35f56c 2022-07-16 thomas break;
523 8a35f56c 2022-07-16 thomas default:
524 8a35f56c 2022-07-16 thomas fatalx("proc_sig_handler: unexpected signal");
525 8a35f56c 2022-07-16 thomas /* NOTREACHED */
526 8a35f56c 2022-07-16 thomas }
527 8a35f56c 2022-07-16 thomas }
528 8a35f56c 2022-07-16 thomas
529 8a35f56c 2022-07-16 thomas void
530 8a35f56c 2022-07-16 thomas proc_run(struct privsep *ps, struct privsep_proc *p,
531 8a35f56c 2022-07-16 thomas struct privsep_proc *procs, unsigned int nproc,
532 8a35f56c 2022-07-16 thomas void (*run)(struct privsep *, struct privsep_proc *, void *), void *arg)
533 8a35f56c 2022-07-16 thomas {
534 8a35f56c 2022-07-16 thomas struct passwd *pw;
535 8a35f56c 2022-07-16 thomas const char *root;
536 8a35f56c 2022-07-16 thomas
537 8a35f56c 2022-07-16 thomas log_procinit(p->p_title);
538 8a35f56c 2022-07-16 thomas
539 8a35f56c 2022-07-16 thomas /* Set the process group of the current process */
540 8a35f56c 2022-07-16 thomas setpgid(0, 0);
541 8a35f56c 2022-07-16 thomas
542 8a35f56c 2022-07-16 thomas /* Use non-standard user */
543 8a35f56c 2022-07-16 thomas if (p->p_pw != NULL)
544 8a35f56c 2022-07-16 thomas pw = p->p_pw;
545 8a35f56c 2022-07-16 thomas else
546 8a35f56c 2022-07-16 thomas pw = ps->ps_pw;
547 8a35f56c 2022-07-16 thomas
548 8a35f56c 2022-07-16 thomas /* Change root directory */
549 8a35f56c 2022-07-16 thomas if (p->p_chroot != NULL)
550 8a35f56c 2022-07-16 thomas root = p->p_chroot;
551 8a35f56c 2022-07-16 thomas else
552 8a35f56c 2022-07-16 thomas root = pw->pw_dir;
553 8a35f56c 2022-07-16 thomas
554 8a35f56c 2022-07-16 thomas if (chroot(root) == -1)
555 8a35f56c 2022-07-16 thomas fatal("proc_run: chroot");
556 8a35f56c 2022-07-16 thomas if (chdir("/") == -1)
557 8a35f56c 2022-07-16 thomas fatal("proc_run: chdir(\"/\")");
558 8a35f56c 2022-07-16 thomas
559 8a35f56c 2022-07-16 thomas privsep_process = p->p_id;
560 8a35f56c 2022-07-16 thomas
561 8a35f56c 2022-07-16 thomas setproctitle("%s", p->p_title);
562 8a35f56c 2022-07-16 thomas
563 8a35f56c 2022-07-16 thomas if (setgroups(1, &pw->pw_gid) ||
564 8a35f56c 2022-07-16 thomas setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
565 8a35f56c 2022-07-16 thomas setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
566 8a35f56c 2022-07-16 thomas fatal("proc_run: cannot drop privileges");
567 8a35f56c 2022-07-16 thomas
568 8a35f56c 2022-07-16 thomas event_init();
569 8a35f56c 2022-07-16 thomas
570 8a35f56c 2022-07-16 thomas signal_set(&ps->ps_evsigint, SIGINT, proc_sig_handler, p);
571 8a35f56c 2022-07-16 thomas signal_set(&ps->ps_evsigterm, SIGTERM, proc_sig_handler, p);
572 8a35f56c 2022-07-16 thomas signal_set(&ps->ps_evsigchld, SIGCHLD, proc_sig_handler, p);
573 8a35f56c 2022-07-16 thomas signal_set(&ps->ps_evsighup, SIGHUP, proc_sig_handler, p);
574 8a35f56c 2022-07-16 thomas signal_set(&ps->ps_evsigpipe, SIGPIPE, proc_sig_handler, p);
575 8a35f56c 2022-07-16 thomas signal_set(&ps->ps_evsigusr1, SIGUSR1, proc_sig_handler, p);
576 8a35f56c 2022-07-16 thomas
577 8a35f56c 2022-07-16 thomas signal_add(&ps->ps_evsigint, NULL);
578 8a35f56c 2022-07-16 thomas signal_add(&ps->ps_evsigterm, NULL);
579 8a35f56c 2022-07-16 thomas signal_add(&ps->ps_evsigchld, NULL);
580 8a35f56c 2022-07-16 thomas signal_add(&ps->ps_evsighup, NULL);
581 8a35f56c 2022-07-16 thomas signal_add(&ps->ps_evsigpipe, NULL);
582 8a35f56c 2022-07-16 thomas signal_add(&ps->ps_evsigusr1, NULL);
583 8a35f56c 2022-07-16 thomas
584 8a35f56c 2022-07-16 thomas proc_setup(ps, procs, nproc);
585 8a35f56c 2022-07-16 thomas proc_accept(ps, PROC_GOTWEBD_SOCK_FILENO, PROC_GOTWEBD, 0);
586 8a35f56c 2022-07-16 thomas
587 8a35f56c 2022-07-16 thomas DPRINTF("%s: %s %d/%d, pid %d", __func__, p->p_title,
588 8a35f56c 2022-07-16 thomas ps->ps_instance + 1, ps->ps_instances[p->p_id], getpid());
589 8a35f56c 2022-07-16 thomas
590 8a35f56c 2022-07-16 thomas if (run != NULL)
591 8a35f56c 2022-07-16 thomas run(ps, p, arg);
592 8a35f56c 2022-07-16 thomas
593 8a35f56c 2022-07-16 thomas event_dispatch();
594 8a35f56c 2022-07-16 thomas
595 8a35f56c 2022-07-16 thomas proc_shutdown(p);
596 8a35f56c 2022-07-16 thomas }
597 8a35f56c 2022-07-16 thomas
598 8a35f56c 2022-07-16 thomas void
599 8a35f56c 2022-07-16 thomas proc_dispatch(int fd, short event, void *arg)
600 8a35f56c 2022-07-16 thomas {
601 8a35f56c 2022-07-16 thomas struct imsgev *iev = arg;
602 8a35f56c 2022-07-16 thomas struct privsep_proc *p = iev->proc;
603 8a35f56c 2022-07-16 thomas struct privsep *ps = p->p_ps;
604 8a35f56c 2022-07-16 thomas struct imsgbuf *ibuf;
605 8a35f56c 2022-07-16 thomas struct imsg imsg;
606 8a35f56c 2022-07-16 thomas ssize_t n;
607 8a35f56c 2022-07-16 thomas int verbose;
608 8a35f56c 2022-07-16 thomas const char *title;
609 8a35f56c 2022-07-16 thomas struct privsep_fd pf;
610 8a35f56c 2022-07-16 thomas
611 8a35f56c 2022-07-16 thomas title = ps->ps_title[privsep_process];
612 8a35f56c 2022-07-16 thomas ibuf = &iev->ibuf;
613 8a35f56c 2022-07-16 thomas
614 8a35f56c 2022-07-16 thomas if (event & EV_READ) {
615 8a35f56c 2022-07-16 thomas n = imsg_read(ibuf);
616 8a35f56c 2022-07-16 thomas if (n == -1 && errno != EAGAIN)
617 8a35f56c 2022-07-16 thomas fatal("%s: imsg_read", __func__);
618 8a35f56c 2022-07-16 thomas if (n == 0) {
619 8a35f56c 2022-07-16 thomas /* this pipe is dead, so remove the event handler */
620 8a35f56c 2022-07-16 thomas event_del(&iev->ev);
621 8a35f56c 2022-07-16 thomas event_loopexit(NULL);
622 8a35f56c 2022-07-16 thomas return;
623 8a35f56c 2022-07-16 thomas }
624 8a35f56c 2022-07-16 thomas }
625 8a35f56c 2022-07-16 thomas
626 8a35f56c 2022-07-16 thomas if (event & EV_WRITE) {
627 8a35f56c 2022-07-16 thomas n = msgbuf_write(&ibuf->w);
628 8a35f56c 2022-07-16 thomas if (n == -1 && errno != EAGAIN)
629 8a35f56c 2022-07-16 thomas fatal("%s: msgbuf_write", __func__);
630 8a35f56c 2022-07-16 thomas if (n == 0) {
631 8a35f56c 2022-07-16 thomas /* this pipe is dead, so remove the event handler */
632 8a35f56c 2022-07-16 thomas event_del(&iev->ev);
633 8a35f56c 2022-07-16 thomas event_loopexit(NULL);
634 8a35f56c 2022-07-16 thomas return;
635 8a35f56c 2022-07-16 thomas }
636 8a35f56c 2022-07-16 thomas }
637 8a35f56c 2022-07-16 thomas
638 8a35f56c 2022-07-16 thomas for (;;) {
639 8a35f56c 2022-07-16 thomas n = imsg_get(ibuf, &imsg);
640 8a35f56c 2022-07-16 thomas if (n == -1)
641 8a35f56c 2022-07-16 thomas fatal("%s: imsg_get", __func__);
642 8a35f56c 2022-07-16 thomas if (n == 0)
643 8a35f56c 2022-07-16 thomas break;
644 8a35f56c 2022-07-16 thomas
645 8a35f56c 2022-07-16 thomas #if DEBUG > 1
646 8a35f56c 2022-07-16 thomas log_debug("%s: %s %d got imsg %d peerid %d from %s %d",
647 8a35f56c 2022-07-16 thomas __func__, title, ps->ps_instance + 1,
648 8a35f56c 2022-07-16 thomas imsg.hdr.type, imsg.hdr.peerid, p->p_title, imsg.hdr.pid);
649 8a35f56c 2022-07-16 thomas #endif
650 8a35f56c 2022-07-16 thomas
651 8a35f56c 2022-07-16 thomas /*
652 8a35f56c 2022-07-16 thomas * Check the message with the program callback
653 8a35f56c 2022-07-16 thomas */
654 8a35f56c 2022-07-16 thomas if ((p->p_cb)(fd, p, &imsg) == 0) {
655 8a35f56c 2022-07-16 thomas /* Message was handled by the callback, continue */
656 8a35f56c 2022-07-16 thomas imsg_free(&imsg);
657 8a35f56c 2022-07-16 thomas continue;
658 8a35f56c 2022-07-16 thomas }
659 8a35f56c 2022-07-16 thomas
660 8a35f56c 2022-07-16 thomas /*
661 8a35f56c 2022-07-16 thomas * Generic message handling
662 8a35f56c 2022-07-16 thomas */
663 8a35f56c 2022-07-16 thomas switch (imsg.hdr.type) {
664 8a35f56c 2022-07-16 thomas case IMSG_CTL_VERBOSE:
665 8a35f56c 2022-07-16 thomas log_info("%s", __func__);
666 8a35f56c 2022-07-16 thomas IMSG_SIZE_CHECK(&imsg, &verbose);
667 8a35f56c 2022-07-16 thomas memcpy(&verbose, imsg.data, sizeof(verbose));
668 8a35f56c 2022-07-16 thomas log_setverbose(verbose);
669 8a35f56c 2022-07-16 thomas break;
670 8a35f56c 2022-07-16 thomas case IMSG_CTL_PROCFD:
671 8a35f56c 2022-07-16 thomas IMSG_SIZE_CHECK(&imsg, &pf);
672 8a35f56c 2022-07-16 thomas memcpy(&pf, imsg.data, sizeof(pf));
673 8a35f56c 2022-07-16 thomas proc_accept(ps, imsg.fd, pf.pf_procid,
674 8a35f56c 2022-07-16 thomas pf.pf_instance);
675 8a35f56c 2022-07-16 thomas break;
676 8a35f56c 2022-07-16 thomas default:
677 8a35f56c 2022-07-16 thomas log_warnx("%s: %s %d got invalid imsg %d peerid %d "
678 8a35f56c 2022-07-16 thomas "from %s %d",
679 8a35f56c 2022-07-16 thomas __func__, title, ps->ps_instance + 1,
680 8a35f56c 2022-07-16 thomas imsg.hdr.type, imsg.hdr.peerid,
681 8a35f56c 2022-07-16 thomas p->p_title, imsg.hdr.pid);
682 8a35f56c 2022-07-16 thomas }
683 8a35f56c 2022-07-16 thomas imsg_free(&imsg);
684 8a35f56c 2022-07-16 thomas }
685 8a35f56c 2022-07-16 thomas imsg_event_add(iev);
686 8a35f56c 2022-07-16 thomas }
687 8a35f56c 2022-07-16 thomas
688 8a35f56c 2022-07-16 thomas int
689 8a35f56c 2022-07-16 thomas proc_dispatch_null(int fd, struct privsep_proc *p, struct imsg *imsg)
690 8a35f56c 2022-07-16 thomas {
691 8a35f56c 2022-07-16 thomas return (-1);
692 8a35f56c 2022-07-16 thomas }
693 8a35f56c 2022-07-16 thomas
694 8a35f56c 2022-07-16 thomas /*
695 8a35f56c 2022-07-16 thomas * imsg helper functions
696 8a35f56c 2022-07-16 thomas */
697 8a35f56c 2022-07-16 thomas
698 8a35f56c 2022-07-16 thomas void
699 8a35f56c 2022-07-16 thomas imsg_event_add(struct imsgev *iev)
700 8a35f56c 2022-07-16 thomas {
701 8a35f56c 2022-07-16 thomas if (iev->handler == NULL) {
702 8a35f56c 2022-07-16 thomas imsg_flush(&iev->ibuf);
703 8a35f56c 2022-07-16 thomas return;
704 8a35f56c 2022-07-16 thomas }
705 8a35f56c 2022-07-16 thomas
706 8a35f56c 2022-07-16 thomas iev->events = EV_READ;
707 8a35f56c 2022-07-16 thomas if (iev->ibuf.w.queued)
708 8a35f56c 2022-07-16 thomas iev->events |= EV_WRITE;
709 8a35f56c 2022-07-16 thomas
710 8a35f56c 2022-07-16 thomas event_del(&iev->ev);
711 8a35f56c 2022-07-16 thomas event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
712 8a35f56c 2022-07-16 thomas event_add(&iev->ev, NULL);
713 8a35f56c 2022-07-16 thomas }
714 8a35f56c 2022-07-16 thomas
715 8a35f56c 2022-07-16 thomas int
716 8a35f56c 2022-07-16 thomas imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
717 8a35f56c 2022-07-16 thomas pid_t pid, int fd, void *data, uint16_t datalen)
718 8a35f56c 2022-07-16 thomas {
719 8a35f56c 2022-07-16 thomas int ret;
720 8a35f56c 2022-07-16 thomas
721 8a35f56c 2022-07-16 thomas ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data, datalen);
722 8a35f56c 2022-07-16 thomas if (ret == -1)
723 8a35f56c 2022-07-16 thomas return (ret);
724 8a35f56c 2022-07-16 thomas imsg_event_add(iev);
725 8a35f56c 2022-07-16 thomas return (ret);
726 8a35f56c 2022-07-16 thomas }
727 8a35f56c 2022-07-16 thomas
728 8a35f56c 2022-07-16 thomas int
729 8a35f56c 2022-07-16 thomas imsg_composev_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
730 8a35f56c 2022-07-16 thomas pid_t pid, int fd, const struct iovec *iov, int iovcnt)
731 8a35f56c 2022-07-16 thomas {
732 8a35f56c 2022-07-16 thomas int ret;
733 8a35f56c 2022-07-16 thomas
734 8a35f56c 2022-07-16 thomas ret = imsg_composev(&iev->ibuf, type, peerid, pid, fd, iov, iovcnt);
735 8a35f56c 2022-07-16 thomas if (ret == -1)
736 8a35f56c 2022-07-16 thomas return (ret);
737 8a35f56c 2022-07-16 thomas imsg_event_add(iev);
738 8a35f56c 2022-07-16 thomas return (ret);
739 8a35f56c 2022-07-16 thomas }
740 8a35f56c 2022-07-16 thomas
741 8a35f56c 2022-07-16 thomas void
742 8a35f56c 2022-07-16 thomas proc_range(struct privsep *ps, enum privsep_procid id, int *n, int *m)
743 8a35f56c 2022-07-16 thomas {
744 8a35f56c 2022-07-16 thomas if (*n == -1) {
745 8a35f56c 2022-07-16 thomas /* Use a range of all target instances */
746 8a35f56c 2022-07-16 thomas *n = 0;
747 8a35f56c 2022-07-16 thomas *m = ps->ps_instances[id];
748 8a35f56c 2022-07-16 thomas } else {
749 8a35f56c 2022-07-16 thomas /* Use only a single slot of the specified peer process */
750 8a35f56c 2022-07-16 thomas *m = *n + 1;
751 8a35f56c 2022-07-16 thomas }
752 8a35f56c 2022-07-16 thomas }
753 8a35f56c 2022-07-16 thomas
754 8a35f56c 2022-07-16 thomas int
755 8a35f56c 2022-07-16 thomas proc_compose_imsg(struct privsep *ps, enum privsep_procid id, int n,
756 8a35f56c 2022-07-16 thomas uint16_t type, uint32_t peerid, int fd, void *data, uint16_t datalen)
757 8a35f56c 2022-07-16 thomas {
758 8a35f56c 2022-07-16 thomas int m;
759 8a35f56c 2022-07-16 thomas
760 8a35f56c 2022-07-16 thomas proc_range(ps, id, &n, &m);
761 8a35f56c 2022-07-16 thomas for (; n < m; n++) {
762 8a35f56c 2022-07-16 thomas if (imsg_compose_event(&ps->ps_ievs[id][n],
763 8a35f56c 2022-07-16 thomas type, peerid, ps->ps_instance + 1, fd, data, datalen) == -1)
764 8a35f56c 2022-07-16 thomas return (-1);
765 8a35f56c 2022-07-16 thomas }
766 8a35f56c 2022-07-16 thomas
767 8a35f56c 2022-07-16 thomas return (0);
768 8a35f56c 2022-07-16 thomas }
769 8a35f56c 2022-07-16 thomas
770 8a35f56c 2022-07-16 thomas int
771 8a35f56c 2022-07-16 thomas proc_compose(struct privsep *ps, enum privsep_procid id,
772 8a35f56c 2022-07-16 thomas uint16_t type, void *data, uint16_t datalen)
773 8a35f56c 2022-07-16 thomas {
774 8a35f56c 2022-07-16 thomas return (proc_compose_imsg(ps, id, -1, type, -1, -1, data, datalen));
775 8a35f56c 2022-07-16 thomas }
776 8a35f56c 2022-07-16 thomas
777 8a35f56c 2022-07-16 thomas int
778 8a35f56c 2022-07-16 thomas proc_composev_imsg(struct privsep *ps, enum privsep_procid id, int n,
779 8a35f56c 2022-07-16 thomas uint16_t type, uint32_t peerid, int fd, const struct iovec *iov, int iovcnt)
780 8a35f56c 2022-07-16 thomas {
781 8a35f56c 2022-07-16 thomas int m;
782 8a35f56c 2022-07-16 thomas
783 8a35f56c 2022-07-16 thomas proc_range(ps, id, &n, &m);
784 8a35f56c 2022-07-16 thomas for (; n < m; n++)
785 8a35f56c 2022-07-16 thomas if (imsg_composev_event(&ps->ps_ievs[id][n],
786 8a35f56c 2022-07-16 thomas type, peerid, ps->ps_instance + 1, fd, iov, iovcnt) == -1)
787 8a35f56c 2022-07-16 thomas return (-1);
788 8a35f56c 2022-07-16 thomas
789 8a35f56c 2022-07-16 thomas return (0);
790 8a35f56c 2022-07-16 thomas }
791 8a35f56c 2022-07-16 thomas
792 8a35f56c 2022-07-16 thomas int
793 8a35f56c 2022-07-16 thomas proc_composev(struct privsep *ps, enum privsep_procid id,
794 8a35f56c 2022-07-16 thomas uint16_t type, const struct iovec *iov, int iovcnt)
795 8a35f56c 2022-07-16 thomas {
796 8a35f56c 2022-07-16 thomas return (proc_composev_imsg(ps, id, -1, type, -1, -1, iov, iovcnt));
797 8a35f56c 2022-07-16 thomas }
798 8a35f56c 2022-07-16 thomas
799 8a35f56c 2022-07-16 thomas int
800 8a35f56c 2022-07-16 thomas proc_forward_imsg(struct privsep *ps, struct imsg *imsg,
801 8a35f56c 2022-07-16 thomas enum privsep_procid id, int n)
802 8a35f56c 2022-07-16 thomas {
803 8a35f56c 2022-07-16 thomas return (proc_compose_imsg(ps, id, n, imsg->hdr.type,
804 8a35f56c 2022-07-16 thomas imsg->hdr.peerid, imsg->fd, imsg->data, IMSG_DATA_SIZE(imsg)));
805 8a35f56c 2022-07-16 thomas }
806 8a35f56c 2022-07-16 thomas
807 8a35f56c 2022-07-16 thomas struct imsgbuf *
808 8a35f56c 2022-07-16 thomas proc_ibuf(struct privsep *ps, enum privsep_procid id, int n)
809 8a35f56c 2022-07-16 thomas {
810 8a35f56c 2022-07-16 thomas int m;
811 8a35f56c 2022-07-16 thomas
812 8a35f56c 2022-07-16 thomas proc_range(ps, id, &n, &m);
813 8a35f56c 2022-07-16 thomas return (&ps->ps_ievs[id][n].ibuf);
814 8a35f56c 2022-07-16 thomas }
815 8a35f56c 2022-07-16 thomas
816 8a35f56c 2022-07-16 thomas struct imsgev *
817 8a35f56c 2022-07-16 thomas proc_iev(struct privsep *ps, enum privsep_procid id, int n)
818 8a35f56c 2022-07-16 thomas {
819 8a35f56c 2022-07-16 thomas int m;
820 8a35f56c 2022-07-16 thomas
821 8a35f56c 2022-07-16 thomas proc_range(ps, id, &n, &m);
822 8a35f56c 2022-07-16 thomas return (&ps->ps_ievs[id][n]);
823 8a35f56c 2022-07-16 thomas }
824 8a35f56c 2022-07-16 thomas
825 8a35f56c 2022-07-16 thomas /* This function should only be called with care as it breaks async I/O */
826 8a35f56c 2022-07-16 thomas int
827 8a35f56c 2022-07-16 thomas proc_flush_imsg(struct privsep *ps, enum privsep_procid id, int n)
828 8a35f56c 2022-07-16 thomas {
829 8a35f56c 2022-07-16 thomas struct imsgbuf *ibuf;
830 8a35f56c 2022-07-16 thomas int m, ret = 0;
831 8a35f56c 2022-07-16 thomas
832 8a35f56c 2022-07-16 thomas proc_range(ps, id, &n, &m);
833 8a35f56c 2022-07-16 thomas for (; n < m; n++) {
834 8a35f56c 2022-07-16 thomas ibuf = proc_ibuf(ps, id, n);
835 8a35f56c 2022-07-16 thomas if (ibuf == NULL)
836 8a35f56c 2022-07-16 thomas return (-1);
837 8a35f56c 2022-07-16 thomas do {
838 8a35f56c 2022-07-16 thomas ret = imsg_flush(ibuf);
839 8a35f56c 2022-07-16 thomas } while (ret == -1 && errno == EAGAIN);
840 8a35f56c 2022-07-16 thomas if (ret == -1)
841 8a35f56c 2022-07-16 thomas break;
842 8a35f56c 2022-07-16 thomas imsg_event_add(&ps->ps_ievs[id][n]);
843 8a35f56c 2022-07-16 thomas }
844 8a35f56c 2022-07-16 thomas
845 8a35f56c 2022-07-16 thomas return (ret);
846 8a35f56c 2022-07-16 thomas }