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 72b033ef 2021-12-29 op static void handle_imsg_conn_req(struct imsgbuf *, struct imsg *, size_t);
35 bc99d868 2021-03-19 op static void handle_imsg_quit(struct imsgbuf*, struct imsg*, size_t);
36 bc99d868 2021-03-19 op static void handle_dispatch_imsg(int, short, void*);
37 881a9dd9 2021-01-16 op
38 bc99d868 2021-03-19 op static imsg_handlerfn *handlers[] = {
39 8ad1c570 2021-05-09 op [IMSG_FCGI_REQ] = handle_imsg_fcgi_req,
40 bc99d868 2021-03-19 op [IMSG_CGI_REQ] = handle_imsg_cgi_req,
41 72b033ef 2021-12-29 op [IMSG_CONN_REQ] = handle_imsg_conn_req,
42 bc99d868 2021-03-19 op [IMSG_QUIT] = handle_imsg_quit,
43 bc99d868 2021-03-19 op };
44 881a9dd9 2021-01-16 op
45 881a9dd9 2021-01-16 op static inline void
46 881a9dd9 2021-01-16 op safe_setenv(const char *name, const char *val)
47 881a9dd9 2021-01-16 op {
48 881a9dd9 2021-01-16 op if (val == NULL)
49 881a9dd9 2021-01-16 op val = "";
50 881a9dd9 2021-01-16 op setenv(name, val, 1);
51 881a9dd9 2021-01-16 op }
52 881a9dd9 2021-01-16 op
53 2fafa2d2 2021-02-01 op static char *
54 2fafa2d2 2021-02-01 op xasprintf(const char *fmt, ...)
55 2fafa2d2 2021-02-01 op {
56 2fafa2d2 2021-02-01 op va_list ap;
57 2fafa2d2 2021-02-01 op char *s;
58 2fafa2d2 2021-02-01 op
59 2fafa2d2 2021-02-01 op va_start(ap, fmt);
60 2fafa2d2 2021-02-01 op if (vasprintf(&s, fmt, ap) == -1)
61 2fafa2d2 2021-02-01 op s = NULL;
62 2fafa2d2 2021-02-01 op va_end(ap);
63 2fafa2d2 2021-02-01 op
64 2fafa2d2 2021-02-01 op return s;
65 9f006a21 2021-02-07 op }
66 9f006a21 2021-02-07 op
67 9f006a21 2021-02-07 op static void
68 9f006a21 2021-02-07 op do_exec(const char *ex, const char *spath, char *query)
69 9f006a21 2021-02-07 op {
70 9f006a21 2021-02-07 op char **argv, buf[PATH_MAX], *sname, *t;
71 9f006a21 2021-02-07 op size_t i, n;
72 9f006a21 2021-02-07 op
73 3841a369 2021-04-20 op /* restore the default handlers */
74 3841a369 2021-04-20 op signal(SIGPIPE, SIG_DFL);
75 3841a369 2021-04-20 op signal(SIGCHLD, SIG_DFL);
76 3841a369 2021-04-20 op signal(SIGHUP, SIG_DFL);
77 3841a369 2021-04-20 op signal(SIGINT, SIG_DFL);
78 3841a369 2021-04-20 op signal(SIGTERM, SIG_DFL);
79 3841a369 2021-04-20 op
80 9f006a21 2021-02-07 op strlcpy(buf, spath, sizeof(buf));
81 9f006a21 2021-02-07 op sname = basename(buf);
82 9f006a21 2021-02-07 op
83 9f006a21 2021-02-07 op if (query == NULL || strchr(query, '=') != NULL) {
84 9f006a21 2021-02-07 op if ((argv = calloc(2, sizeof(char*))) == NULL)
85 9f006a21 2021-02-07 op err(1, "calloc");
86 9f006a21 2021-02-07 op argv[0] = sname;
87 9f006a21 2021-02-07 op execvp(ex, argv);
88 9f006a21 2021-02-07 op warn("execvp: %s", argv[0]);
89 9f006a21 2021-02-07 op return;
90 9f006a21 2021-02-07 op }
91 9f006a21 2021-02-07 op
92 9f006a21 2021-02-07 op n = 1;
93 9f006a21 2021-02-07 op for (t = query ;; t++, n++) {
94 9f006a21 2021-02-07 op if ((t = strchr(t, '+')) == NULL)
95 9f006a21 2021-02-07 op break;
96 9f006a21 2021-02-07 op }
97 9f006a21 2021-02-07 op
98 9f006a21 2021-02-07 op if ((argv = calloc(n+2, sizeof(char*))) == NULL)
99 9f006a21 2021-02-07 op err(1, "calloc");
100 9f006a21 2021-02-07 op
101 9f006a21 2021-02-07 op argv[0] = sname;
102 9f006a21 2021-02-07 op for (i = 0; i < n; ++i) {
103 9f006a21 2021-02-07 op t = strchr(query, '+');
104 9f006a21 2021-02-07 op if (t != NULL)
105 9f006a21 2021-02-07 op *t = '\0';
106 9f006a21 2021-02-07 op argv[i+1] = pct_decode_str(query);
107 9f006a21 2021-02-07 op query = t+1;
108 9f006a21 2021-02-07 op }
109 9f006a21 2021-02-07 op
110 9f006a21 2021-02-07 op execvp(ex, argv);
111 9f006a21 2021-02-07 op warn("execvp: %s", argv[0]);
112 b63e30ff 2021-02-07 op }
113 b63e30ff 2021-02-07 op
114 b63e30ff 2021-02-07 op static inline void
115 b63e30ff 2021-02-07 op setenv_time(const char *var, time_t t)
116 b63e30ff 2021-02-07 op {
117 b63e30ff 2021-02-07 op char timebuf[21];
118 b63e30ff 2021-02-07 op struct tm tminfo;
119 b63e30ff 2021-02-07 op
120 b63e30ff 2021-02-07 op if (t == -1)
121 b63e30ff 2021-02-07 op return;
122 b63e30ff 2021-02-07 op
123 b63e30ff 2021-02-07 op strftime(timebuf, sizeof(timebuf), "%FT%TZ",
124 b63e30ff 2021-02-07 op gmtime_r(&t, &tminfo));
125 b63e30ff 2021-02-07 op setenv(var, timebuf, 1);
126 2fafa2d2 2021-02-01 op }
127 2fafa2d2 2021-02-01 op
128 881a9dd9 2021-01-16 op /* fd or -1 on error */
129 881a9dd9 2021-01-16 op static int
130 fdea6aa0 2021-04-30 op launch_cgi(struct iri *iri, struct cgireq *req, struct vhost *vhost,
131 fdea6aa0 2021-04-30 op struct location *loc)
132 881a9dd9 2021-01-16 op {
133 ea976e87 2021-07-06 op int p[2], errp[2]; /* read end, write end */
134 881a9dd9 2021-01-16 op
135 403c4220 2021-10-02 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, p) == -1)
136 881a9dd9 2021-01-16 op return -1;
137 403c4220 2021-10-02 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, errp) == -1)
138 ea976e87 2021-07-06 op return -1;
139 881a9dd9 2021-01-16 op
140 881a9dd9 2021-01-16 op switch (fork()) {
141 881a9dd9 2021-01-16 op case -1:
142 e7c6502b 2021-07-08 op log_err(NULL, "fork failed: %s", strerror(errno));
143 e7c6502b 2021-07-08 op close(p[0]);
144 e7c6502b 2021-07-08 op close(p[1]);
145 e7c6502b 2021-07-08 op close(errp[0]);
146 e7c6502b 2021-07-08 op close(errp[1]);
147 881a9dd9 2021-01-16 op return -1;
148 881a9dd9 2021-01-16 op
149 881a9dd9 2021-01-16 op case 0: { /* child */
150 4fbd4dcc 2022-11-29 op char *ex, *pwd, *qs;
151 2fafa2d2 2021-02-01 op char iribuf[GEMINI_URL_LEN];
152 2fafa2d2 2021-02-01 op char path[PATH_MAX];
153 9cc630aa 2021-04-28 op struct envlist *e;
154 881a9dd9 2021-01-16 op
155 881a9dd9 2021-01-16 op close(p[0]);
156 881a9dd9 2021-01-16 op if (dup2(p[1], 1) == -1)
157 881a9dd9 2021-01-16 op goto childerr;
158 881a9dd9 2021-01-16 op
159 ea976e87 2021-07-06 op close(errp[0]);
160 ea976e87 2021-07-06 op if (dup2(errp[1], 2) == -1)
161 ea976e87 2021-07-06 op goto childerr;
162 ea976e87 2021-07-06 op
163 fdea6aa0 2021-04-30 op ex = xasprintf("%s/%s", loc->dir, req->spath);
164 881a9dd9 2021-01-16 op
165 2fafa2d2 2021-02-01 op serialize_iri(iri, iribuf, sizeof(iribuf));
166 881a9dd9 2021-01-16 op
167 881a9dd9 2021-01-16 op safe_setenv("GATEWAY_INTERFACE", "CGI/1.1");
168 fdea6aa0 2021-04-30 op safe_setenv("GEMINI_DOCUMENT_ROOT", loc->dir);
169 2fafa2d2 2021-02-01 op safe_setenv("GEMINI_SCRIPT_FILENAME",
170 fdea6aa0 2021-04-30 op xasprintf("%s/%s", loc->dir, req->spath));
171 2fafa2d2 2021-02-01 op safe_setenv("GEMINI_URL", iribuf);
172 881a9dd9 2021-01-16 op
173 2fafa2d2 2021-02-01 op strlcpy(path, "/", sizeof(path));
174 bc99d868 2021-03-19 op strlcat(path, req->spath, sizeof(path));
175 2fafa2d2 2021-02-01 op safe_setenv("GEMINI_URL_PATH", path);
176 881a9dd9 2021-01-16 op
177 bc99d868 2021-03-19 op if (*req->relpath != '\0') {
178 2fafa2d2 2021-02-01 op strlcpy(path, "/", sizeof(path));
179 bc99d868 2021-03-19 op strlcat(path, req->relpath, sizeof(path));
180 2fafa2d2 2021-02-01 op safe_setenv("PATH_INFO", path);
181 2fafa2d2 2021-02-01 op
182 fdea6aa0 2021-04-30 op strlcpy(path, loc->dir, sizeof(path));
183 2fafa2d2 2021-02-01 op strlcat(path, "/", sizeof(path));
184 bc99d868 2021-03-19 op strlcat(path, req->relpath, sizeof(path));
185 2fafa2d2 2021-02-01 op safe_setenv("PATH_TRANSLATED", path);
186 4fbd4dcc 2022-11-29 op }
187 4fbd4dcc 2022-11-29 op
188 4fbd4dcc 2022-11-29 op if (iri->query != NULL &&
189 4fbd4dcc 2022-11-29 op strchr(iri->query, '=') == NULL &&
190 4fbd4dcc 2022-11-29 op (qs = strdup(iri->query)) != NULL) {
191 4fbd4dcc 2022-11-29 op pct_decode_str(qs);
192 4fbd4dcc 2022-11-29 op safe_setenv("GEMINI_SEARCH_STRING", qs);
193 4fbd4dcc 2022-11-29 op free(qs);
194 2fafa2d2 2021-02-01 op }
195 2fafa2d2 2021-02-01 op
196 2fafa2d2 2021-02-01 op safe_setenv("QUERY_STRING", iri->query);
197 bc99d868 2021-03-19 op safe_setenv("REMOTE_ADDR", req->addr);
198 bc99d868 2021-03-19 op safe_setenv("REMOTE_HOST", req->addr);
199 2fafa2d2 2021-02-01 op safe_setenv("REQUEST_METHOD", "");
200 881a9dd9 2021-01-16 op
201 2fafa2d2 2021-02-01 op strlcpy(path, "/", sizeof(path));
202 bc99d868 2021-03-19 op strlcat(path, req->spath, sizeof(path));
203 2fafa2d2 2021-02-01 op safe_setenv("SCRIPT_NAME", path);
204 2fafa2d2 2021-02-01 op
205 2fafa2d2 2021-02-01 op safe_setenv("SERVER_NAME", iri->host);
206 2fafa2d2 2021-02-01 op
207 2fafa2d2 2021-02-01 op snprintf(path, sizeof(path), "%d", conf.port);
208 2fafa2d2 2021-02-01 op safe_setenv("SERVER_PORT", path);
209 2fafa2d2 2021-02-01 op
210 2fafa2d2 2021-02-01 op safe_setenv("SERVER_PROTOCOL", "GEMINI");
211 ce2c9edb 2021-05-15 op safe_setenv("SERVER_SOFTWARE", GMID_VERSION);
212 2fafa2d2 2021-02-01 op
213 bc99d868 2021-03-19 op if (*req->subject != '\0')
214 881a9dd9 2021-01-16 op safe_setenv("AUTH_TYPE", "Certificate");
215 3e541809 2021-02-01 op else
216 3e541809 2021-02-01 op safe_setenv("AUTH_TYPE", "");
217 05748e49 2021-01-24 op
218 bc99d868 2021-03-19 op safe_setenv("REMOTE_USER", req->subject);
219 bc99d868 2021-03-19 op safe_setenv("TLS_CLIENT_ISSUER", req->issuer);
220 bc99d868 2021-03-19 op safe_setenv("TLS_CLIENT_HASH", req->hash);
221 89541eee 2021-04-13 op safe_setenv("TLS_VERSION", req->version);
222 89541eee 2021-04-13 op safe_setenv("TLS_CIPHER", req->cipher);
223 89541eee 2021-04-13 op
224 89541eee 2021-04-13 op snprintf(path, sizeof(path), "%d", req->cipher_strength);
225 89541eee 2021-04-13 op safe_setenv("TLS_CIPHER_STRENGTH", path);
226 89541eee 2021-04-13 op
227 bc99d868 2021-03-19 op setenv_time("TLS_CLIENT_NOT_AFTER", req->notafter);
228 bc99d868 2021-03-19 op setenv_time("TLS_CLIENT_NOT_BEFORE", req->notbefore);
229 3e541809 2021-02-01 op
230 9cc630aa 2021-04-28 op TAILQ_FOREACH(e, &vhost->env, envs) {
231 9cc630aa 2021-04-28 op safe_setenv(e->name, e->value);
232 9cc630aa 2021-04-28 op }
233 9cc630aa 2021-04-28 op
234 9f006a21 2021-02-07 op strlcpy(path, ex, sizeof(path));
235 9f006a21 2021-02-07 op
236 2fafa2d2 2021-02-01 op pwd = dirname(path);
237 2fafa2d2 2021-02-01 op if (chdir(pwd)) {
238 2fafa2d2 2021-02-01 op warn("chdir");
239 2fafa2d2 2021-02-01 op goto childerr;
240 2fafa2d2 2021-02-01 op }
241 881a9dd9 2021-01-16 op
242 bc99d868 2021-03-19 op do_exec(ex, req->spath, iri->query);
243 881a9dd9 2021-01-16 op goto childerr;
244 881a9dd9 2021-01-16 op }
245 881a9dd9 2021-01-16 op
246 881a9dd9 2021-01-16 op default:
247 881a9dd9 2021-01-16 op close(p[1]);
248 e7c6502b 2021-07-08 op close(errp[0]);
249 ea976e87 2021-07-06 op close(errp[1]);
250 52053e1a 2021-02-06 op mark_nonblock(p[0]);
251 881a9dd9 2021-01-16 op return p[0];
252 881a9dd9 2021-01-16 op }
253 881a9dd9 2021-01-16 op
254 881a9dd9 2021-01-16 op childerr:
255 881a9dd9 2021-01-16 op dprintf(p[1], "%d internal server error\r\n", TEMP_FAILURE);
256 881a9dd9 2021-01-16 op _exit(1);
257 881a9dd9 2021-01-16 op }
258 881a9dd9 2021-01-16 op
259 b8e64ccd 2021-03-31 op static struct vhost *
260 b8e64ccd 2021-03-31 op host_nth(size_t n)
261 b8e64ccd 2021-03-31 op {
262 b8e64ccd 2021-03-31 op struct vhost *h;
263 b8e64ccd 2021-03-31 op
264 b8e64ccd 2021-03-31 op TAILQ_FOREACH(h, &hosts, vhosts) {
265 b8e64ccd 2021-03-31 op if (n == 0)
266 b8e64ccd 2021-03-31 op return h;
267 b8e64ccd 2021-03-31 op n--;
268 b8e64ccd 2021-03-31 op }
269 b8e64ccd 2021-03-31 op
270 b8e64ccd 2021-03-31 op return NULL;
271 b8e64ccd 2021-03-31 op }
272 b8e64ccd 2021-03-31 op
273 fdea6aa0 2021-04-30 op static struct location *
274 fdea6aa0 2021-04-30 op loc_nth(struct vhost *vhost, size_t n)
275 fdea6aa0 2021-04-30 op {
276 fdea6aa0 2021-04-30 op struct location *loc;
277 fdea6aa0 2021-04-30 op
278 fdea6aa0 2021-04-30 op TAILQ_FOREACH(loc, &vhost->locations, locations) {
279 fdea6aa0 2021-04-30 op if (n == 0)
280 fdea6aa0 2021-04-30 op return loc;
281 fdea6aa0 2021-04-30 op n--;
282 fdea6aa0 2021-04-30 op }
283 fdea6aa0 2021-04-30 op
284 fdea6aa0 2021-04-30 op return NULL;
285 fdea6aa0 2021-04-30 op }
286 fdea6aa0 2021-04-30 op
287 2c3e53da 2021-03-03 op static void
288 bc99d868 2021-03-19 op handle_imsg_cgi_req(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
289 881a9dd9 2021-01-16 op {
290 b8e64ccd 2021-03-31 op struct vhost *h;
291 fdea6aa0 2021-04-30 op struct location *l;
292 b8e64ccd 2021-03-31 op struct cgireq req;
293 b8e64ccd 2021-03-31 op struct iri iri;
294 b8e64ccd 2021-03-31 op int fd;
295 2c3e53da 2021-03-03 op
296 bc99d868 2021-03-19 op if (datalen != sizeof(req))
297 bc99d868 2021-03-19 op abort();
298 bc99d868 2021-03-19 op
299 6084a9a5 2022-03-27 op memcpy(&req, imsg->data, sizeof(req));
300 bc99d868 2021-03-19 op
301 bc99d868 2021-03-19 op iri.schema = req.iri_schema_off + req.buf;
302 bc99d868 2021-03-19 op iri.host = req.iri_host_off + req.buf;
303 bc99d868 2021-03-19 op iri.port = req.iri_port_off + req.buf;
304 bc99d868 2021-03-19 op iri.path = req.iri_path_off + req.buf;
305 bc99d868 2021-03-19 op iri.query = req.iri_query_off + req.buf;
306 bc99d868 2021-03-19 op iri.fragment = req.iri_fragment_off + req.buf;
307 bc99d868 2021-03-19 op
308 bc99d868 2021-03-19 op /* patch the query, otherwise do_exec will always pass "" as
309 bc99d868 2021-03-19 op * first argument to the script. */
310 bc99d868 2021-03-19 op if (*iri.query == '\0')
311 bc99d868 2021-03-19 op iri.query = NULL;
312 bc99d868 2021-03-19 op
313 b8e64ccd 2021-03-31 op if ((h = host_nth(req.host_off)) == NULL)
314 bc99d868 2021-03-19 op abort();
315 bc99d868 2021-03-19 op
316 1feaf2a6 2021-05-15 op if ((l = loc_nth(h, req.loc_off)) == NULL)
317 fdea6aa0 2021-04-30 op abort();
318 fdea6aa0 2021-04-30 op
319 fdea6aa0 2021-04-30 op fd = launch_cgi(&iri, &req, h, l);
320 bc99d868 2021-03-19 op imsg_compose(ibuf, IMSG_CGI_RES, imsg->hdr.peerid, 0, fd, NULL, 0);
321 bc99d868 2021-03-19 op imsg_flush(ibuf);
322 8ad1c570 2021-05-09 op }
323 8ad1c570 2021-05-09 op
324 8ad1c570 2021-05-09 op static int
325 8ad1c570 2021-05-09 op fcgi_open_prog(struct fcgi *f)
326 8ad1c570 2021-05-09 op {
327 8ad1c570 2021-05-09 op int s[2];
328 8ad1c570 2021-05-09 op pid_t p;
329 8ad1c570 2021-05-09 op
330 8ad1c570 2021-05-09 op /* XXX! */
331 8ad1c570 2021-05-09 op
332 2e2e189b 2021-07-08 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, s) == -1)
333 8ad1c570 2021-05-09 op err(1, "socketpair");
334 8ad1c570 2021-05-09 op
335 8ad1c570 2021-05-09 op switch (p = fork()) {
336 8ad1c570 2021-05-09 op case -1:
337 8ad1c570 2021-05-09 op err(1, "fork");
338 8ad1c570 2021-05-09 op case 0:
339 8ad1c570 2021-05-09 op close(s[0]);
340 8ad1c570 2021-05-09 op if (dup2(s[1], 0) == -1)
341 8ad1c570 2021-05-09 op err(1, "dup2");
342 8ad1c570 2021-05-09 op execl(f->prog, f->prog, NULL);
343 8ad1c570 2021-05-09 op err(1, "execl %s", f->prog);
344 8ad1c570 2021-05-09 op default:
345 8ad1c570 2021-05-09 op close(s[1]);
346 8ad1c570 2021-05-09 op return s[0];
347 8ad1c570 2021-05-09 op }
348 bc99d868 2021-03-19 op }
349 bc99d868 2021-03-19 op
350 8ad1c570 2021-05-09 op static int
351 8ad1c570 2021-05-09 op fcgi_open_sock(struct fcgi *f)
352 8ad1c570 2021-05-09 op {
353 8ad1c570 2021-05-09 op struct sockaddr_un addr;
354 8ad1c570 2021-05-09 op int fd;
355 8ad1c570 2021-05-09 op
356 8ad1c570 2021-05-09 op if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
357 8ad1c570 2021-05-09 op log_err(NULL, "socket: %s", strerror(errno));
358 8ad1c570 2021-05-09 op return -1;
359 8ad1c570 2021-05-09 op }
360 8ad1c570 2021-05-09 op
361 8ad1c570 2021-05-09 op memset(&addr, 0, sizeof(addr));
362 8ad1c570 2021-05-09 op addr.sun_family = AF_UNIX;
363 8ad1c570 2021-05-09 op strlcpy(addr.sun_path, f->path, sizeof(addr.sun_path));
364 8ad1c570 2021-05-09 op
365 8ad1c570 2021-05-09 op if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
366 8ad1c570 2021-05-09 op log_warn(NULL, "failed to connect to %s: %s", f->path,
367 8ad1c570 2021-05-09 op strerror(errno));
368 8ad1c570 2021-05-09 op close(fd);
369 8ad1c570 2021-05-09 op return -1;
370 8ad1c570 2021-05-09 op }
371 8ad1c570 2021-05-09 op
372 8ad1c570 2021-05-09 op return fd;
373 8ad1c570 2021-05-09 op }
374 8ad1c570 2021-05-09 op
375 8ad1c570 2021-05-09 op static int
376 8ad1c570 2021-05-09 op fcgi_open_conn(struct fcgi *f)
377 8ad1c570 2021-05-09 op {
378 8ad1c570 2021-05-09 op struct addrinfo hints, *servinfo, *p;
379 8ad1c570 2021-05-09 op int r, sock;
380 8ad1c570 2021-05-09 op
381 8ad1c570 2021-05-09 op memset(&hints, 0, sizeof(hints));
382 8ad1c570 2021-05-09 op hints.ai_family = AF_UNSPEC;
383 8ad1c570 2021-05-09 op hints.ai_socktype = SOCK_STREAM;
384 8ad1c570 2021-05-09 op hints.ai_flags = AI_ADDRCONFIG;
385 8ad1c570 2021-05-09 op
386 8ad1c570 2021-05-09 op if ((r = getaddrinfo(f->path, f->port, &hints, &servinfo)) != 0) {
387 8ad1c570 2021-05-09 op log_warn(NULL, "getaddrinfo %s:%s: %s", f->path, f->port,
388 8ad1c570 2021-05-09 op gai_strerror(r));
389 8ad1c570 2021-05-09 op return -1;
390 8ad1c570 2021-05-09 op }
391 8ad1c570 2021-05-09 op
392 8ad1c570 2021-05-09 op for (p = servinfo; p != NULL; p = p->ai_next) {
393 8ad1c570 2021-05-09 op sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
394 8ad1c570 2021-05-09 op if (sock == -1)
395 8ad1c570 2021-05-09 op continue;
396 8ad1c570 2021-05-09 op if (connect(sock, p->ai_addr, p->ai_addrlen) == -1) {
397 8ad1c570 2021-05-09 op close(sock);
398 8ad1c570 2021-05-09 op continue;
399 8ad1c570 2021-05-09 op }
400 8ad1c570 2021-05-09 op break;
401 8ad1c570 2021-05-09 op }
402 8ad1c570 2021-05-09 op
403 8ad1c570 2021-05-09 op if (p == NULL) {
404 8ad1c570 2021-05-09 op log_warn(NULL, "couldn't connect to %s:%s", f->path, f->port);
405 8ad1c570 2021-05-09 op sock = -1;
406 8ad1c570 2021-05-09 op }
407 8ad1c570 2021-05-09 op
408 8ad1c570 2021-05-09 op freeaddrinfo(servinfo);
409 8ad1c570 2021-05-09 op return sock;
410 8ad1c570 2021-05-09 op }
411 8ad1c570 2021-05-09 op
412 bc99d868 2021-03-19 op static void
413 8ad1c570 2021-05-09 op handle_imsg_fcgi_req(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
414 8ad1c570 2021-05-09 op {
415 8ad1c570 2021-05-09 op struct fcgi *f;
416 8ad1c570 2021-05-09 op int id, fd;
417 8ad1c570 2021-05-09 op
418 8ad1c570 2021-05-09 op if (datalen != sizeof(id))
419 8ad1c570 2021-05-09 op abort();
420 6084a9a5 2022-03-27 op memcpy(&id, imsg->data, sizeof(id));
421 8ad1c570 2021-05-09 op
422 8ad1c570 2021-05-09 op if (id > FCGI_MAX || (fcgi[id].path == NULL && fcgi[id].prog == NULL))
423 8ad1c570 2021-05-09 op abort();
424 8ad1c570 2021-05-09 op
425 8ad1c570 2021-05-09 op f = &fcgi[id];
426 8ad1c570 2021-05-09 op if (f->prog != NULL)
427 8ad1c570 2021-05-09 op fd = fcgi_open_prog(f);
428 8ad1c570 2021-05-09 op else if (f->port != NULL)
429 8ad1c570 2021-05-09 op fd = fcgi_open_conn(f);
430 8ad1c570 2021-05-09 op else
431 8ad1c570 2021-05-09 op fd = fcgi_open_sock(f);
432 8ad1c570 2021-05-09 op
433 4cd25209 2021-10-07 op imsg_compose(ibuf, IMSG_FCGI_FD, imsg->hdr.peerid, 0, fd, NULL, 0);
434 72b033ef 2021-12-29 op imsg_flush(ibuf);
435 72b033ef 2021-12-29 op }
436 72b033ef 2021-12-29 op
437 72b033ef 2021-12-29 op static void
438 72b033ef 2021-12-29 op handle_imsg_conn_req(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
439 72b033ef 2021-12-29 op {
440 72b033ef 2021-12-29 op struct addrinfo hints, *res, *res0;
441 72b033ef 2021-12-29 op struct connreq req;
442 72b033ef 2021-12-29 op int r, sock;
443 72b033ef 2021-12-29 op
444 72b033ef 2021-12-29 op if (datalen != sizeof(req))
445 72b033ef 2021-12-29 op abort();
446 72b033ef 2021-12-29 op memcpy(&req, imsg->data, sizeof(req));
447 72b033ef 2021-12-29 op req.flag = 0;
448 72b033ef 2021-12-29 op
449 72b033ef 2021-12-29 op memset(&hints, 0, sizeof(hints));
450 72b033ef 2021-12-29 op hints.ai_family = AF_UNSPEC;
451 72b033ef 2021-12-29 op hints.ai_socktype = SOCK_STREAM;
452 72b033ef 2021-12-29 op
453 72b033ef 2021-12-29 op /* XXX: do this asynchronously if possible */
454 72b033ef 2021-12-29 op r = getaddrinfo(req.host, req.port, &hints, &res0);
455 72b033ef 2021-12-29 op if (r != 0) {
456 72b033ef 2021-12-29 op log_warn(NULL, "getaddrinfo(\"%s\", \"%s\"): %s",
457 72b033ef 2021-12-29 op req.host, req.port, gai_strerror(r));
458 72b033ef 2021-12-29 op goto err;
459 72b033ef 2021-12-29 op }
460 72b033ef 2021-12-29 op
461 72b033ef 2021-12-29 op for (res = res0; res; res = res->ai_next) {
462 72b033ef 2021-12-29 op sock = socket(res->ai_family, res->ai_socktype,
463 72b033ef 2021-12-29 op res->ai_protocol);
464 72b033ef 2021-12-29 op if (sock == -1)
465 72b033ef 2021-12-29 op continue;
466 72b033ef 2021-12-29 op
467 72b033ef 2021-12-29 op if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
468 72b033ef 2021-12-29 op close(sock);
469 72b033ef 2021-12-29 op sock = -1;
470 72b033ef 2021-12-29 op continue;
471 72b033ef 2021-12-29 op }
472 72b033ef 2021-12-29 op
473 72b033ef 2021-12-29 op break;
474 72b033ef 2021-12-29 op }
475 72b033ef 2021-12-29 op
476 72b033ef 2021-12-29 op freeaddrinfo(res0);
477 72b033ef 2021-12-29 op
478 72b033ef 2021-12-29 op if (sock == -1) {
479 72b033ef 2021-12-29 op log_warn(NULL, "can't connect to %s:%s", req.host,
480 72b033ef 2021-12-29 op req.port);
481 72b033ef 2021-12-29 op goto err;
482 72b033ef 2021-12-29 op }
483 72b033ef 2021-12-29 op
484 72b033ef 2021-12-29 op imsg_compose(ibuf, IMSG_CONN_FD, imsg->hdr.peerid, 0, sock, NULL, 0);
485 8ad1c570 2021-05-09 op imsg_flush(ibuf);
486 72b033ef 2021-12-29 op return;
487 72b033ef 2021-12-29 op
488 72b033ef 2021-12-29 op err:
489 72b033ef 2021-12-29 op imsg_compose(ibuf, IMSG_CONN_FD, imsg->hdr.peerid, 0, -1, NULL, 0);
490 72b033ef 2021-12-29 op imsg_flush(ibuf);
491 8ad1c570 2021-05-09 op }
492 8ad1c570 2021-05-09 op
493 8ad1c570 2021-05-09 op static void
494 bc99d868 2021-03-19 op handle_imsg_quit(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
495 bc99d868 2021-03-19 op {
496 bc99d868 2021-03-19 op int i;
497 bc99d868 2021-03-19 op
498 bc99d868 2021-03-19 op for (i = 0; i < conf.prefork; ++i) {
499 bc99d868 2021-03-19 op imsg_compose(&servibuf[i], IMSG_QUIT, 0, 0, -1, NULL, 0);
500 bc99d868 2021-03-19 op imsg_flush(&exibuf);
501 bc99d868 2021-03-19 op close(servibuf[i].fd);
502 1fbac5ba 2021-03-03 op }
503 2c3e53da 2021-03-03 op
504 bc99d868 2021-03-19 op event_loopbreak();
505 bc99d868 2021-03-19 op }
506 2c3e53da 2021-03-03 op
507 bc99d868 2021-03-19 op static void
508 bc99d868 2021-03-19 op handle_dispatch_imsg(int fd, short ev, void *d)
509 bc99d868 2021-03-19 op {
510 bc99d868 2021-03-19 op struct imsgbuf *ibuf = d;
511 bc99d868 2021-03-19 op dispatch_imsg(ibuf, handlers, sizeof(handlers));
512 2c3e53da 2021-03-03 op }
513 2c3e53da 2021-03-03 op
514 2c3e53da 2021-03-03 op int
515 bc99d868 2021-03-19 op executor_main(struct imsgbuf *ibuf)
516 2c3e53da 2021-03-03 op {
517 bc99d868 2021-03-19 op struct event evs[PROC_MAX], imsgev;
518 2c3e53da 2021-03-03 op int i;
519 10782292 2021-01-25 op
520 2c3e53da 2021-03-03 op event_init();
521 881a9dd9 2021-01-16 op
522 bc99d868 2021-03-19 op if (ibuf != NULL) {
523 bc99d868 2021-03-19 op event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST,
524 bc99d868 2021-03-19 op handle_dispatch_imsg, ibuf);
525 bc99d868 2021-03-19 op event_add(&imsgev, NULL);
526 bc99d868 2021-03-19 op }
527 bc99d868 2021-03-19 op
528 2c3e53da 2021-03-03 op for (i = 0; i < conf.prefork; ++i) {
529 bc99d868 2021-03-19 op event_set(&evs[i], servibuf[i].fd, EV_READ | EV_PERSIST,
530 bc99d868 2021-03-19 op handle_dispatch_imsg, &servibuf[i]);
531 2c3e53da 2021-03-03 op event_add(&evs[i], NULL);
532 881a9dd9 2021-01-16 op }
533 881a9dd9 2021-01-16 op
534 62e001b0 2021-03-20 op sandbox_executor_process();
535 62e001b0 2021-03-20 op
536 2c3e53da 2021-03-03 op event_dispatch();
537 ca21e100 2021-02-04 op
538 881a9dd9 2021-01-16 op return 1;
539 881a9dd9 2021-01-16 op }