Blame


1 881a9dd9 2021-01-16 op /*
2 881a9dd9 2021-01-16 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 881a9dd9 2021-01-16 op *
4 881a9dd9 2021-01-16 op * Permission to use, copy, modify, and distribute this software for any
5 881a9dd9 2021-01-16 op * purpose with or without fee is hereby granted, provided that the above
6 881a9dd9 2021-01-16 op * copyright notice and this permission notice appear in all copies.
7 881a9dd9 2021-01-16 op *
8 881a9dd9 2021-01-16 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 881a9dd9 2021-01-16 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 881a9dd9 2021-01-16 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 881a9dd9 2021-01-16 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 881a9dd9 2021-01-16 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 881a9dd9 2021-01-16 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 881a9dd9 2021-01-16 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 881a9dd9 2021-01-16 op */
16 52418c8d 2021-02-12 op
17 52418c8d 2021-02-12 op #include "gmid.h"
18 881a9dd9 2021-01-16 op
19 8ad1c570 2021-05-09 op #include <sys/un.h>
20 8ad1c570 2021-05-09 op
21 881a9dd9 2021-01-16 op #include <err.h>
22 881a9dd9 2021-01-16 op #include <errno.h>
23 881a9dd9 2021-01-16 op
24 2c3e53da 2021-03-03 op #include <event.h>
25 881a9dd9 2021-01-16 op #include <fcntl.h>
26 2fafa2d2 2021-02-01 op #include <libgen.h>
27 2fafa2d2 2021-02-01 op #include <limits.h>
28 881a9dd9 2021-01-16 op #include <signal.h>
29 2fafa2d2 2021-02-01 op #include <stdarg.h>
30 881a9dd9 2021-01-16 op #include <string.h>
31 881a9dd9 2021-01-16 op
32 bc99d868 2021-03-19 op static void handle_imsg_cgi_req(struct imsgbuf*, struct imsg*, size_t);
33 8ad1c570 2021-05-09 op static void handle_imsg_fcgi_req(struct imsgbuf*, struct imsg*, size_t);
34 bc99d868 2021-03-19 op static void handle_imsg_quit(struct imsgbuf*, struct imsg*, size_t);
35 bc99d868 2021-03-19 op static void handle_dispatch_imsg(int, short, void*);
36 881a9dd9 2021-01-16 op
37 bc99d868 2021-03-19 op static imsg_handlerfn *handlers[] = {
38 8ad1c570 2021-05-09 op [IMSG_FCGI_REQ] = handle_imsg_fcgi_req,
39 bc99d868 2021-03-19 op [IMSG_CGI_REQ] = handle_imsg_cgi_req,
40 bc99d868 2021-03-19 op [IMSG_QUIT] = handle_imsg_quit,
41 bc99d868 2021-03-19 op };
42 881a9dd9 2021-01-16 op
43 881a9dd9 2021-01-16 op static inline void
44 881a9dd9 2021-01-16 op safe_setenv(const char *name, const char *val)
45 881a9dd9 2021-01-16 op {
46 881a9dd9 2021-01-16 op if (val == NULL)
47 881a9dd9 2021-01-16 op val = "";
48 881a9dd9 2021-01-16 op setenv(name, val, 1);
49 881a9dd9 2021-01-16 op }
50 881a9dd9 2021-01-16 op
51 2fafa2d2 2021-02-01 op static char *
52 2fafa2d2 2021-02-01 op xasprintf(const char *fmt, ...)
53 2fafa2d2 2021-02-01 op {
54 2fafa2d2 2021-02-01 op va_list ap;
55 2fafa2d2 2021-02-01 op char *s;
56 2fafa2d2 2021-02-01 op
57 2fafa2d2 2021-02-01 op va_start(ap, fmt);
58 2fafa2d2 2021-02-01 op if (vasprintf(&s, fmt, ap) == -1)
59 2fafa2d2 2021-02-01 op s = NULL;
60 2fafa2d2 2021-02-01 op va_end(ap);
61 2fafa2d2 2021-02-01 op
62 2fafa2d2 2021-02-01 op return s;
63 9f006a21 2021-02-07 op }
64 9f006a21 2021-02-07 op
65 9f006a21 2021-02-07 op static void
66 9f006a21 2021-02-07 op do_exec(const char *ex, const char *spath, char *query)
67 9f006a21 2021-02-07 op {
68 9f006a21 2021-02-07 op char **argv, buf[PATH_MAX], *sname, *t;
69 9f006a21 2021-02-07 op size_t i, n;
70 9f006a21 2021-02-07 op
71 3841a369 2021-04-20 op /* restore the default handlers */
72 3841a369 2021-04-20 op signal(SIGPIPE, SIG_DFL);
73 3841a369 2021-04-20 op signal(SIGCHLD, SIG_DFL);
74 3841a369 2021-04-20 op signal(SIGHUP, SIG_DFL);
75 3841a369 2021-04-20 op signal(SIGINT, SIG_DFL);
76 3841a369 2021-04-20 op signal(SIGTERM, SIG_DFL);
77 3841a369 2021-04-20 op
78 9f006a21 2021-02-07 op strlcpy(buf, spath, sizeof(buf));
79 9f006a21 2021-02-07 op sname = basename(buf);
80 9f006a21 2021-02-07 op
81 9f006a21 2021-02-07 op if (query == NULL || strchr(query, '=') != NULL) {
82 9f006a21 2021-02-07 op if ((argv = calloc(2, sizeof(char*))) == NULL)
83 9f006a21 2021-02-07 op err(1, "calloc");
84 9f006a21 2021-02-07 op argv[0] = sname;
85 9f006a21 2021-02-07 op execvp(ex, argv);
86 9f006a21 2021-02-07 op warn("execvp: %s", argv[0]);
87 9f006a21 2021-02-07 op return;
88 9f006a21 2021-02-07 op }
89 9f006a21 2021-02-07 op
90 9f006a21 2021-02-07 op n = 1;
91 9f006a21 2021-02-07 op for (t = query ;; t++, n++) {
92 9f006a21 2021-02-07 op if ((t = strchr(t, '+')) == NULL)
93 9f006a21 2021-02-07 op break;
94 9f006a21 2021-02-07 op }
95 9f006a21 2021-02-07 op
96 9f006a21 2021-02-07 op if ((argv = calloc(n+2, sizeof(char*))) == NULL)
97 9f006a21 2021-02-07 op err(1, "calloc");
98 9f006a21 2021-02-07 op
99 9f006a21 2021-02-07 op argv[0] = sname;
100 9f006a21 2021-02-07 op for (i = 0; i < n; ++i) {
101 9f006a21 2021-02-07 op t = strchr(query, '+');
102 9f006a21 2021-02-07 op if (t != NULL)
103 9f006a21 2021-02-07 op *t = '\0';
104 9f006a21 2021-02-07 op argv[i+1] = pct_decode_str(query);
105 9f006a21 2021-02-07 op query = t+1;
106 9f006a21 2021-02-07 op }
107 9f006a21 2021-02-07 op
108 9f006a21 2021-02-07 op execvp(ex, argv);
109 9f006a21 2021-02-07 op warn("execvp: %s", argv[0]);
110 b63e30ff 2021-02-07 op }
111 b63e30ff 2021-02-07 op
112 b63e30ff 2021-02-07 op static inline void
113 b63e30ff 2021-02-07 op setenv_time(const char *var, time_t t)
114 b63e30ff 2021-02-07 op {
115 b63e30ff 2021-02-07 op char timebuf[21];
116 b63e30ff 2021-02-07 op struct tm tminfo;
117 b63e30ff 2021-02-07 op
118 b63e30ff 2021-02-07 op if (t == -1)
119 b63e30ff 2021-02-07 op return;
120 b63e30ff 2021-02-07 op
121 b63e30ff 2021-02-07 op strftime(timebuf, sizeof(timebuf), "%FT%TZ",
122 b63e30ff 2021-02-07 op gmtime_r(&t, &tminfo));
123 b63e30ff 2021-02-07 op setenv(var, timebuf, 1);
124 2fafa2d2 2021-02-01 op }
125 2fafa2d2 2021-02-01 op
126 881a9dd9 2021-01-16 op /* fd or -1 on error */
127 881a9dd9 2021-01-16 op static int
128 fdea6aa0 2021-04-30 op launch_cgi(struct iri *iri, struct cgireq *req, struct vhost *vhost,
129 fdea6aa0 2021-04-30 op struct location *loc)
130 881a9dd9 2021-01-16 op {
131 ea976e87 2021-07-06 op int p[2], errp[2]; /* read end, write end */
132 881a9dd9 2021-01-16 op
133 52053e1a 2021-02-06 op if (pipe(p) == -1)
134 881a9dd9 2021-01-16 op return -1;
135 ea976e87 2021-07-06 op if (pipe(errp) == -1)
136 ea976e87 2021-07-06 op return -1;
137 881a9dd9 2021-01-16 op
138 881a9dd9 2021-01-16 op switch (fork()) {
139 881a9dd9 2021-01-16 op case -1:
140 881a9dd9 2021-01-16 op return -1;
141 881a9dd9 2021-01-16 op
142 881a9dd9 2021-01-16 op case 0: { /* child */
143 2fafa2d2 2021-02-01 op char *ex, *pwd;
144 2fafa2d2 2021-02-01 op char iribuf[GEMINI_URL_LEN];
145 2fafa2d2 2021-02-01 op char path[PATH_MAX];
146 9cc630aa 2021-04-28 op struct envlist *e;
147 881a9dd9 2021-01-16 op
148 881a9dd9 2021-01-16 op close(p[0]);
149 881a9dd9 2021-01-16 op if (dup2(p[1], 1) == -1)
150 881a9dd9 2021-01-16 op goto childerr;
151 881a9dd9 2021-01-16 op
152 ea976e87 2021-07-06 op close(errp[0]);
153 ea976e87 2021-07-06 op if (dup2(errp[1], 2) == -1)
154 ea976e87 2021-07-06 op goto childerr;
155 ea976e87 2021-07-06 op
156 fdea6aa0 2021-04-30 op ex = xasprintf("%s/%s", loc->dir, req->spath);
157 881a9dd9 2021-01-16 op
158 2fafa2d2 2021-02-01 op serialize_iri(iri, iribuf, sizeof(iribuf));
159 881a9dd9 2021-01-16 op
160 881a9dd9 2021-01-16 op safe_setenv("GATEWAY_INTERFACE", "CGI/1.1");
161 fdea6aa0 2021-04-30 op safe_setenv("GEMINI_DOCUMENT_ROOT", loc->dir);
162 2fafa2d2 2021-02-01 op safe_setenv("GEMINI_SCRIPT_FILENAME",
163 fdea6aa0 2021-04-30 op xasprintf("%s/%s", loc->dir, req->spath));
164 2fafa2d2 2021-02-01 op safe_setenv("GEMINI_URL", iribuf);
165 881a9dd9 2021-01-16 op
166 2fafa2d2 2021-02-01 op strlcpy(path, "/", sizeof(path));
167 bc99d868 2021-03-19 op strlcat(path, req->spath, sizeof(path));
168 2fafa2d2 2021-02-01 op safe_setenv("GEMINI_URL_PATH", path);
169 881a9dd9 2021-01-16 op
170 bc99d868 2021-03-19 op if (*req->relpath != '\0') {
171 2fafa2d2 2021-02-01 op strlcpy(path, "/", sizeof(path));
172 bc99d868 2021-03-19 op strlcat(path, req->relpath, sizeof(path));
173 2fafa2d2 2021-02-01 op safe_setenv("PATH_INFO", path);
174 2fafa2d2 2021-02-01 op
175 fdea6aa0 2021-04-30 op strlcpy(path, loc->dir, sizeof(path));
176 2fafa2d2 2021-02-01 op strlcat(path, "/", sizeof(path));
177 bc99d868 2021-03-19 op strlcat(path, req->relpath, sizeof(path));
178 2fafa2d2 2021-02-01 op safe_setenv("PATH_TRANSLATED", path);
179 2fafa2d2 2021-02-01 op }
180 2fafa2d2 2021-02-01 op
181 2fafa2d2 2021-02-01 op safe_setenv("QUERY_STRING", iri->query);
182 bc99d868 2021-03-19 op safe_setenv("REMOTE_ADDR", req->addr);
183 bc99d868 2021-03-19 op safe_setenv("REMOTE_HOST", req->addr);
184 2fafa2d2 2021-02-01 op safe_setenv("REQUEST_METHOD", "");
185 881a9dd9 2021-01-16 op
186 2fafa2d2 2021-02-01 op strlcpy(path, "/", sizeof(path));
187 bc99d868 2021-03-19 op strlcat(path, req->spath, sizeof(path));
188 2fafa2d2 2021-02-01 op safe_setenv("SCRIPT_NAME", path);
189 2fafa2d2 2021-02-01 op
190 2fafa2d2 2021-02-01 op safe_setenv("SERVER_NAME", iri->host);
191 2fafa2d2 2021-02-01 op
192 2fafa2d2 2021-02-01 op snprintf(path, sizeof(path), "%d", conf.port);
193 2fafa2d2 2021-02-01 op safe_setenv("SERVER_PORT", path);
194 2fafa2d2 2021-02-01 op
195 2fafa2d2 2021-02-01 op safe_setenv("SERVER_PROTOCOL", "GEMINI");
196 ce2c9edb 2021-05-15 op safe_setenv("SERVER_SOFTWARE", GMID_VERSION);
197 2fafa2d2 2021-02-01 op
198 bc99d868 2021-03-19 op if (*req->subject != '\0')
199 881a9dd9 2021-01-16 op safe_setenv("AUTH_TYPE", "Certificate");
200 3e541809 2021-02-01 op else
201 3e541809 2021-02-01 op safe_setenv("AUTH_TYPE", "");
202 05748e49 2021-01-24 op
203 bc99d868 2021-03-19 op safe_setenv("REMOTE_USER", req->subject);
204 bc99d868 2021-03-19 op safe_setenv("TLS_CLIENT_ISSUER", req->issuer);
205 bc99d868 2021-03-19 op safe_setenv("TLS_CLIENT_HASH", req->hash);
206 89541eee 2021-04-13 op safe_setenv("TLS_VERSION", req->version);
207 89541eee 2021-04-13 op safe_setenv("TLS_CIPHER", req->cipher);
208 89541eee 2021-04-13 op
209 89541eee 2021-04-13 op snprintf(path, sizeof(path), "%d", req->cipher_strength);
210 89541eee 2021-04-13 op safe_setenv("TLS_CIPHER_STRENGTH", path);
211 89541eee 2021-04-13 op
212 bc99d868 2021-03-19 op setenv_time("TLS_CLIENT_NOT_AFTER", req->notafter);
213 bc99d868 2021-03-19 op setenv_time("TLS_CLIENT_NOT_BEFORE", req->notbefore);
214 3e541809 2021-02-01 op
215 9cc630aa 2021-04-28 op TAILQ_FOREACH(e, &vhost->env, envs) {
216 9cc630aa 2021-04-28 op safe_setenv(e->name, e->value);
217 9cc630aa 2021-04-28 op }
218 9cc630aa 2021-04-28 op
219 9f006a21 2021-02-07 op strlcpy(path, ex, sizeof(path));
220 9f006a21 2021-02-07 op
221 2fafa2d2 2021-02-01 op pwd = dirname(path);
222 2fafa2d2 2021-02-01 op if (chdir(pwd)) {
223 2fafa2d2 2021-02-01 op warn("chdir");
224 2fafa2d2 2021-02-01 op goto childerr;
225 2fafa2d2 2021-02-01 op }
226 881a9dd9 2021-01-16 op
227 bc99d868 2021-03-19 op do_exec(ex, req->spath, iri->query);
228 881a9dd9 2021-01-16 op goto childerr;
229 881a9dd9 2021-01-16 op }
230 881a9dd9 2021-01-16 op
231 881a9dd9 2021-01-16 op default:
232 881a9dd9 2021-01-16 op close(p[1]);
233 ea976e87 2021-07-06 op close(errp[1]);
234 52053e1a 2021-02-06 op mark_nonblock(p[0]);
235 881a9dd9 2021-01-16 op return p[0];
236 881a9dd9 2021-01-16 op }
237 881a9dd9 2021-01-16 op
238 881a9dd9 2021-01-16 op childerr:
239 881a9dd9 2021-01-16 op dprintf(p[1], "%d internal server error\r\n", TEMP_FAILURE);
240 881a9dd9 2021-01-16 op _exit(1);
241 881a9dd9 2021-01-16 op }
242 881a9dd9 2021-01-16 op
243 b8e64ccd 2021-03-31 op static struct vhost *
244 b8e64ccd 2021-03-31 op host_nth(size_t n)
245 b8e64ccd 2021-03-31 op {
246 b8e64ccd 2021-03-31 op struct vhost *h;
247 b8e64ccd 2021-03-31 op
248 b8e64ccd 2021-03-31 op TAILQ_FOREACH(h, &hosts, vhosts) {
249 b8e64ccd 2021-03-31 op if (n == 0)
250 b8e64ccd 2021-03-31 op return h;
251 b8e64ccd 2021-03-31 op n--;
252 b8e64ccd 2021-03-31 op }
253 b8e64ccd 2021-03-31 op
254 b8e64ccd 2021-03-31 op return NULL;
255 b8e64ccd 2021-03-31 op }
256 b8e64ccd 2021-03-31 op
257 fdea6aa0 2021-04-30 op static struct location *
258 fdea6aa0 2021-04-30 op loc_nth(struct vhost *vhost, size_t n)
259 fdea6aa0 2021-04-30 op {
260 fdea6aa0 2021-04-30 op struct location *loc;
261 fdea6aa0 2021-04-30 op
262 fdea6aa0 2021-04-30 op TAILQ_FOREACH(loc, &vhost->locations, locations) {
263 fdea6aa0 2021-04-30 op if (n == 0)
264 fdea6aa0 2021-04-30 op return loc;
265 fdea6aa0 2021-04-30 op n--;
266 fdea6aa0 2021-04-30 op }
267 fdea6aa0 2021-04-30 op
268 fdea6aa0 2021-04-30 op return NULL;
269 fdea6aa0 2021-04-30 op }
270 fdea6aa0 2021-04-30 op
271 2c3e53da 2021-03-03 op static void
272 bc99d868 2021-03-19 op handle_imsg_cgi_req(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
273 881a9dd9 2021-01-16 op {
274 b8e64ccd 2021-03-31 op struct vhost *h;
275 fdea6aa0 2021-04-30 op struct location *l;
276 b8e64ccd 2021-03-31 op struct cgireq req;
277 b8e64ccd 2021-03-31 op struct iri iri;
278 b8e64ccd 2021-03-31 op int fd;
279 2c3e53da 2021-03-03 op
280 bc99d868 2021-03-19 op if (datalen != sizeof(req))
281 bc99d868 2021-03-19 op abort();
282 bc99d868 2021-03-19 op
283 bc99d868 2021-03-19 op memcpy(&req, imsg->data, datalen);
284 bc99d868 2021-03-19 op
285 bc99d868 2021-03-19 op iri.schema = req.iri_schema_off + req.buf;
286 bc99d868 2021-03-19 op iri.host = req.iri_host_off + req.buf;
287 bc99d868 2021-03-19 op iri.port = req.iri_port_off + req.buf;
288 bc99d868 2021-03-19 op iri.path = req.iri_path_off + req.buf;
289 bc99d868 2021-03-19 op iri.query = req.iri_query_off + req.buf;
290 bc99d868 2021-03-19 op iri.fragment = req.iri_fragment_off + req.buf;
291 bc99d868 2021-03-19 op
292 bc99d868 2021-03-19 op /* patch the query, otherwise do_exec will always pass "" as
293 bc99d868 2021-03-19 op * first argument to the script. */
294 bc99d868 2021-03-19 op if (*iri.query == '\0')
295 bc99d868 2021-03-19 op iri.query = NULL;
296 bc99d868 2021-03-19 op
297 b8e64ccd 2021-03-31 op if ((h = host_nth(req.host_off)) == NULL)
298 bc99d868 2021-03-19 op abort();
299 bc99d868 2021-03-19 op
300 1feaf2a6 2021-05-15 op if ((l = loc_nth(h, req.loc_off)) == NULL)
301 fdea6aa0 2021-04-30 op abort();
302 fdea6aa0 2021-04-30 op
303 fdea6aa0 2021-04-30 op fd = launch_cgi(&iri, &req, h, l);
304 bc99d868 2021-03-19 op imsg_compose(ibuf, IMSG_CGI_RES, imsg->hdr.peerid, 0, fd, NULL, 0);
305 bc99d868 2021-03-19 op imsg_flush(ibuf);
306 8ad1c570 2021-05-09 op }
307 8ad1c570 2021-05-09 op
308 8ad1c570 2021-05-09 op static int
309 8ad1c570 2021-05-09 op fcgi_open_prog(struct fcgi *f)
310 8ad1c570 2021-05-09 op {
311 8ad1c570 2021-05-09 op int s[2];
312 8ad1c570 2021-05-09 op pid_t p;
313 8ad1c570 2021-05-09 op
314 8ad1c570 2021-05-09 op /* XXX! */
315 8ad1c570 2021-05-09 op
316 8ad1c570 2021-05-09 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNIX, s) == -1)
317 8ad1c570 2021-05-09 op err(1, "socketpair");
318 8ad1c570 2021-05-09 op
319 8ad1c570 2021-05-09 op switch (p = fork()) {
320 8ad1c570 2021-05-09 op case -1:
321 8ad1c570 2021-05-09 op err(1, "fork");
322 8ad1c570 2021-05-09 op case 0:
323 8ad1c570 2021-05-09 op close(s[0]);
324 8ad1c570 2021-05-09 op if (dup2(s[1], 0) == -1)
325 8ad1c570 2021-05-09 op err(1, "dup2");
326 8ad1c570 2021-05-09 op execl(f->prog, f->prog, NULL);
327 8ad1c570 2021-05-09 op err(1, "execl %s", f->prog);
328 8ad1c570 2021-05-09 op default:
329 8ad1c570 2021-05-09 op close(s[1]);
330 8ad1c570 2021-05-09 op return s[0];
331 8ad1c570 2021-05-09 op }
332 bc99d868 2021-03-19 op }
333 bc99d868 2021-03-19 op
334 8ad1c570 2021-05-09 op static int
335 8ad1c570 2021-05-09 op fcgi_open_sock(struct fcgi *f)
336 8ad1c570 2021-05-09 op {
337 8ad1c570 2021-05-09 op struct sockaddr_un addr;
338 8ad1c570 2021-05-09 op int fd;
339 8ad1c570 2021-05-09 op
340 8ad1c570 2021-05-09 op if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
341 8ad1c570 2021-05-09 op log_err(NULL, "socket: %s", strerror(errno));
342 8ad1c570 2021-05-09 op return -1;
343 8ad1c570 2021-05-09 op }
344 8ad1c570 2021-05-09 op
345 8ad1c570 2021-05-09 op memset(&addr, 0, sizeof(addr));
346 8ad1c570 2021-05-09 op addr.sun_family = AF_UNIX;
347 8ad1c570 2021-05-09 op strlcpy(addr.sun_path, f->path, sizeof(addr.sun_path));
348 8ad1c570 2021-05-09 op
349 8ad1c570 2021-05-09 op if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
350 8ad1c570 2021-05-09 op log_warn(NULL, "failed to connect to %s: %s", f->path,
351 8ad1c570 2021-05-09 op strerror(errno));
352 8ad1c570 2021-05-09 op close(fd);
353 8ad1c570 2021-05-09 op return -1;
354 8ad1c570 2021-05-09 op }
355 8ad1c570 2021-05-09 op
356 8ad1c570 2021-05-09 op return fd;
357 8ad1c570 2021-05-09 op }
358 8ad1c570 2021-05-09 op
359 8ad1c570 2021-05-09 op static int
360 8ad1c570 2021-05-09 op fcgi_open_conn(struct fcgi *f)
361 8ad1c570 2021-05-09 op {
362 8ad1c570 2021-05-09 op struct addrinfo hints, *servinfo, *p;
363 8ad1c570 2021-05-09 op int r, sock;
364 8ad1c570 2021-05-09 op
365 8ad1c570 2021-05-09 op memset(&hints, 0, sizeof(hints));
366 8ad1c570 2021-05-09 op hints.ai_family = AF_UNSPEC;
367 8ad1c570 2021-05-09 op hints.ai_socktype = SOCK_STREAM;
368 8ad1c570 2021-05-09 op hints.ai_flags = AI_ADDRCONFIG;
369 8ad1c570 2021-05-09 op
370 8ad1c570 2021-05-09 op if ((r = getaddrinfo(f->path, f->port, &hints, &servinfo)) != 0) {
371 8ad1c570 2021-05-09 op log_warn(NULL, "getaddrinfo %s:%s: %s", f->path, f->port,
372 8ad1c570 2021-05-09 op gai_strerror(r));
373 8ad1c570 2021-05-09 op return -1;
374 8ad1c570 2021-05-09 op }
375 8ad1c570 2021-05-09 op
376 8ad1c570 2021-05-09 op for (p = servinfo; p != NULL; p = p->ai_next) {
377 8ad1c570 2021-05-09 op sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
378 8ad1c570 2021-05-09 op if (sock == -1)
379 8ad1c570 2021-05-09 op continue;
380 8ad1c570 2021-05-09 op if (connect(sock, p->ai_addr, p->ai_addrlen) == -1) {
381 8ad1c570 2021-05-09 op close(sock);
382 8ad1c570 2021-05-09 op continue;
383 8ad1c570 2021-05-09 op }
384 8ad1c570 2021-05-09 op break;
385 8ad1c570 2021-05-09 op }
386 8ad1c570 2021-05-09 op
387 8ad1c570 2021-05-09 op if (p == NULL) {
388 8ad1c570 2021-05-09 op log_warn(NULL, "couldn't connect to %s:%s", f->path, f->port);
389 8ad1c570 2021-05-09 op sock = -1;
390 8ad1c570 2021-05-09 op }
391 8ad1c570 2021-05-09 op
392 8ad1c570 2021-05-09 op freeaddrinfo(servinfo);
393 8ad1c570 2021-05-09 op return sock;
394 8ad1c570 2021-05-09 op }
395 8ad1c570 2021-05-09 op
396 bc99d868 2021-03-19 op static void
397 8ad1c570 2021-05-09 op handle_imsg_fcgi_req(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
398 8ad1c570 2021-05-09 op {
399 8ad1c570 2021-05-09 op struct fcgi *f;
400 8ad1c570 2021-05-09 op int id, fd;
401 8ad1c570 2021-05-09 op
402 8ad1c570 2021-05-09 op if (datalen != sizeof(id))
403 8ad1c570 2021-05-09 op abort();
404 8ad1c570 2021-05-09 op memcpy(&id, imsg->data, datalen);
405 8ad1c570 2021-05-09 op
406 8ad1c570 2021-05-09 op if (id > FCGI_MAX || (fcgi[id].path == NULL && fcgi[id].prog == NULL))
407 8ad1c570 2021-05-09 op abort();
408 8ad1c570 2021-05-09 op
409 8ad1c570 2021-05-09 op f = &fcgi[id];
410 8ad1c570 2021-05-09 op if (f->prog != NULL)
411 8ad1c570 2021-05-09 op fd = fcgi_open_prog(f);
412 8ad1c570 2021-05-09 op else if (f->port != NULL)
413 8ad1c570 2021-05-09 op fd = fcgi_open_conn(f);
414 8ad1c570 2021-05-09 op else
415 8ad1c570 2021-05-09 op fd = fcgi_open_sock(f);
416 8ad1c570 2021-05-09 op
417 8ad1c570 2021-05-09 op imsg_compose(ibuf, IMSG_FCGI_FD, id, 0, fd, NULL, 0);
418 8ad1c570 2021-05-09 op imsg_flush(ibuf);
419 8ad1c570 2021-05-09 op }
420 8ad1c570 2021-05-09 op
421 8ad1c570 2021-05-09 op static void
422 bc99d868 2021-03-19 op handle_imsg_quit(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
423 bc99d868 2021-03-19 op {
424 bc99d868 2021-03-19 op int i;
425 bc99d868 2021-03-19 op
426 bc99d868 2021-03-19 op (void)ibuf;
427 bc99d868 2021-03-19 op (void)imsg;
428 bc99d868 2021-03-19 op (void)datalen;
429 bc99d868 2021-03-19 op
430 bc99d868 2021-03-19 op for (i = 0; i < conf.prefork; ++i) {
431 bc99d868 2021-03-19 op imsg_compose(&servibuf[i], IMSG_QUIT, 0, 0, -1, NULL, 0);
432 bc99d868 2021-03-19 op imsg_flush(&exibuf);
433 bc99d868 2021-03-19 op close(servibuf[i].fd);
434 1fbac5ba 2021-03-03 op }
435 2c3e53da 2021-03-03 op
436 bc99d868 2021-03-19 op event_loopbreak();
437 bc99d868 2021-03-19 op }
438 2c3e53da 2021-03-03 op
439 bc99d868 2021-03-19 op static void
440 bc99d868 2021-03-19 op handle_dispatch_imsg(int fd, short ev, void *d)
441 bc99d868 2021-03-19 op {
442 bc99d868 2021-03-19 op struct imsgbuf *ibuf = d;
443 bc99d868 2021-03-19 op dispatch_imsg(ibuf, handlers, sizeof(handlers));
444 2c3e53da 2021-03-03 op }
445 2c3e53da 2021-03-03 op
446 2c3e53da 2021-03-03 op int
447 bc99d868 2021-03-19 op executor_main(struct imsgbuf *ibuf)
448 2c3e53da 2021-03-03 op {
449 bc99d868 2021-03-19 op struct event evs[PROC_MAX], imsgev;
450 2c3e53da 2021-03-03 op int i;
451 10782292 2021-01-25 op
452 2c3e53da 2021-03-03 op event_init();
453 881a9dd9 2021-01-16 op
454 bc99d868 2021-03-19 op if (ibuf != NULL) {
455 bc99d868 2021-03-19 op event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST,
456 bc99d868 2021-03-19 op handle_dispatch_imsg, ibuf);
457 bc99d868 2021-03-19 op event_add(&imsgev, NULL);
458 bc99d868 2021-03-19 op }
459 bc99d868 2021-03-19 op
460 2c3e53da 2021-03-03 op for (i = 0; i < conf.prefork; ++i) {
461 bc99d868 2021-03-19 op event_set(&evs[i], servibuf[i].fd, EV_READ | EV_PERSIST,
462 bc99d868 2021-03-19 op handle_dispatch_imsg, &servibuf[i]);
463 2c3e53da 2021-03-03 op event_add(&evs[i], NULL);
464 881a9dd9 2021-01-16 op }
465 881a9dd9 2021-01-16 op
466 62e001b0 2021-03-20 op sandbox_executor_process();
467 62e001b0 2021-03-20 op
468 2c3e53da 2021-03-03 op event_dispatch();
469 ca21e100 2021-02-04 op
470 881a9dd9 2021-01-16 op return 1;
471 881a9dd9 2021-01-16 op }