2 881a9dd9 2021-01-16 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
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.
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.
17 52418c8d 2021-02-12 op #include "gmid.h"
19 8ad1c570 2021-05-09 op #include <sys/un.h>
21 881a9dd9 2021-01-16 op #include <err.h>
22 881a9dd9 2021-01-16 op #include <errno.h>
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>
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*);
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,
43 881a9dd9 2021-01-16 op static inline void
44 881a9dd9 2021-01-16 op safe_setenv(const char *name, const char *val)
46 881a9dd9 2021-01-16 op if (val == NULL)
48 881a9dd9 2021-01-16 op setenv(name, val, 1);
52 2fafa2d2 2021-02-01 op xasprintf(const char *fmt, ...)
57 2fafa2d2 2021-02-01 op va_start(ap, fmt);
58 2fafa2d2 2021-02-01 op if (vasprintf(&s, fmt, ap) == -1)
66 9f006a21 2021-02-07 op do_exec(const char *ex, const char *spath, char *query)
68 9f006a21 2021-02-07 op char **argv, buf[PATH_MAX], *sname, *t;
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);
78 9f006a21 2021-02-07 op strlcpy(buf, spath, sizeof(buf));
79 9f006a21 2021-02-07 op sname = basename(buf);
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]);
91 9f006a21 2021-02-07 op for (t = query ;; t++, n++) {
92 9f006a21 2021-02-07 op if ((t = strchr(t, '+')) == NULL)
96 9f006a21 2021-02-07 op if ((argv = calloc(n+2, sizeof(char*))) == NULL)
97 9f006a21 2021-02-07 op err(1, "calloc");
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)
104 9f006a21 2021-02-07 op argv[i+1] = pct_decode_str(query);
108 9f006a21 2021-02-07 op execvp(ex, argv);
109 9f006a21 2021-02-07 op warn("execvp: %s", argv[0]);
112 b63e30ff 2021-02-07 op static inline void
113 b63e30ff 2021-02-07 op setenv_time(const char *var, time_t t)
115 b63e30ff 2021-02-07 op char timebuf[21];
116 b63e30ff 2021-02-07 op struct tm tminfo;
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);
126 881a9dd9 2021-01-16 op /* fd or -1 on error */
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)
131 ea976e87 2021-07-06 op int p[2], errp[2]; /* read end, write end */
133 403c4220 2021-10-02 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, p) == -1)
135 403c4220 2021-10-02 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, errp) == -1)
138 881a9dd9 2021-01-16 op switch (fork()) {
140 e7c6502b 2021-07-08 op log_err(NULL, "fork failed: %s", strerror(errno));
143 e7c6502b 2021-07-08 op close(errp[0]);
144 e7c6502b 2021-07-08 op close(errp[1]);
147 881a9dd9 2021-01-16 op case 0: { /* child */
148 2fafa2d2 2021-02-01 op char *ex, *pwd;
149 2fafa2d2 2021-02-01 op char iribuf[GEMINI_URL_LEN];
150 2fafa2d2 2021-02-01 op char path[PATH_MAX];
151 9cc630aa 2021-04-28 op struct envlist *e;
154 881a9dd9 2021-01-16 op if (dup2(p[1], 1) == -1)
155 881a9dd9 2021-01-16 op goto childerr;
157 ea976e87 2021-07-06 op close(errp[0]);
158 ea976e87 2021-07-06 op if (dup2(errp[1], 2) == -1)
159 ea976e87 2021-07-06 op goto childerr;
161 fdea6aa0 2021-04-30 op ex = xasprintf("%s/%s", loc->dir, req->spath);
163 2fafa2d2 2021-02-01 op serialize_iri(iri, iribuf, sizeof(iribuf));
165 881a9dd9 2021-01-16 op safe_setenv("GATEWAY_INTERFACE", "CGI/1.1");
166 fdea6aa0 2021-04-30 op safe_setenv("GEMINI_DOCUMENT_ROOT", loc->dir);
167 2fafa2d2 2021-02-01 op safe_setenv("GEMINI_SCRIPT_FILENAME",
168 fdea6aa0 2021-04-30 op xasprintf("%s/%s", loc->dir, req->spath));
169 2fafa2d2 2021-02-01 op safe_setenv("GEMINI_URL", iribuf);
171 2fafa2d2 2021-02-01 op strlcpy(path, "/", sizeof(path));
172 bc99d868 2021-03-19 op strlcat(path, req->spath, sizeof(path));
173 2fafa2d2 2021-02-01 op safe_setenv("GEMINI_URL_PATH", path);
175 bc99d868 2021-03-19 op if (*req->relpath != '\0') {
176 2fafa2d2 2021-02-01 op strlcpy(path, "/", sizeof(path));
177 bc99d868 2021-03-19 op strlcat(path, req->relpath, sizeof(path));
178 2fafa2d2 2021-02-01 op safe_setenv("PATH_INFO", path);
180 fdea6aa0 2021-04-30 op strlcpy(path, loc->dir, sizeof(path));
181 2fafa2d2 2021-02-01 op strlcat(path, "/", sizeof(path));
182 bc99d868 2021-03-19 op strlcat(path, req->relpath, sizeof(path));
183 2fafa2d2 2021-02-01 op safe_setenv("PATH_TRANSLATED", path);
186 2fafa2d2 2021-02-01 op safe_setenv("QUERY_STRING", iri->query);
187 bc99d868 2021-03-19 op safe_setenv("REMOTE_ADDR", req->addr);
188 bc99d868 2021-03-19 op safe_setenv("REMOTE_HOST", req->addr);
189 2fafa2d2 2021-02-01 op safe_setenv("REQUEST_METHOD", "");
191 2fafa2d2 2021-02-01 op strlcpy(path, "/", sizeof(path));
192 bc99d868 2021-03-19 op strlcat(path, req->spath, sizeof(path));
193 2fafa2d2 2021-02-01 op safe_setenv("SCRIPT_NAME", path);
195 2fafa2d2 2021-02-01 op safe_setenv("SERVER_NAME", iri->host);
197 2fafa2d2 2021-02-01 op snprintf(path, sizeof(path), "%d", conf.port);
198 2fafa2d2 2021-02-01 op safe_setenv("SERVER_PORT", path);
200 2fafa2d2 2021-02-01 op safe_setenv("SERVER_PROTOCOL", "GEMINI");
201 ce2c9edb 2021-05-15 op safe_setenv("SERVER_SOFTWARE", GMID_VERSION);
203 bc99d868 2021-03-19 op if (*req->subject != '\0')
204 881a9dd9 2021-01-16 op safe_setenv("AUTH_TYPE", "Certificate");
206 3e541809 2021-02-01 op safe_setenv("AUTH_TYPE", "");
208 bc99d868 2021-03-19 op safe_setenv("REMOTE_USER", req->subject);
209 bc99d868 2021-03-19 op safe_setenv("TLS_CLIENT_ISSUER", req->issuer);
210 bc99d868 2021-03-19 op safe_setenv("TLS_CLIENT_HASH", req->hash);
211 89541eee 2021-04-13 op safe_setenv("TLS_VERSION", req->version);
212 89541eee 2021-04-13 op safe_setenv("TLS_CIPHER", req->cipher);
214 89541eee 2021-04-13 op snprintf(path, sizeof(path), "%d", req->cipher_strength);
215 89541eee 2021-04-13 op safe_setenv("TLS_CIPHER_STRENGTH", path);
217 bc99d868 2021-03-19 op setenv_time("TLS_CLIENT_NOT_AFTER", req->notafter);
218 bc99d868 2021-03-19 op setenv_time("TLS_CLIENT_NOT_BEFORE", req->notbefore);
220 9cc630aa 2021-04-28 op TAILQ_FOREACH(e, &vhost->env, envs) {
221 9cc630aa 2021-04-28 op safe_setenv(e->name, e->value);
224 9f006a21 2021-02-07 op strlcpy(path, ex, sizeof(path));
226 2fafa2d2 2021-02-01 op pwd = dirname(path);
227 2fafa2d2 2021-02-01 op if (chdir(pwd)) {
228 2fafa2d2 2021-02-01 op warn("chdir");
229 2fafa2d2 2021-02-01 op goto childerr;
232 bc99d868 2021-03-19 op do_exec(ex, req->spath, iri->query);
233 881a9dd9 2021-01-16 op goto childerr;
238 e7c6502b 2021-07-08 op close(errp[0]);
239 ea976e87 2021-07-06 op close(errp[1]);
240 52053e1a 2021-02-06 op mark_nonblock(p[0]);
245 881a9dd9 2021-01-16 op dprintf(p[1], "%d internal server error\r\n", TEMP_FAILURE);
249 b8e64ccd 2021-03-31 op static struct vhost *
250 b8e64ccd 2021-03-31 op host_nth(size_t n)
252 b8e64ccd 2021-03-31 op struct vhost *h;
254 b8e64ccd 2021-03-31 op TAILQ_FOREACH(h, &hosts, vhosts) {
263 fdea6aa0 2021-04-30 op static struct location *
264 fdea6aa0 2021-04-30 op loc_nth(struct vhost *vhost, size_t n)
266 fdea6aa0 2021-04-30 op struct location *loc;
268 fdea6aa0 2021-04-30 op TAILQ_FOREACH(loc, &vhost->locations, locations) {
278 bc99d868 2021-03-19 op handle_imsg_cgi_req(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
280 b8e64ccd 2021-03-31 op struct vhost *h;
281 fdea6aa0 2021-04-30 op struct location *l;
282 b8e64ccd 2021-03-31 op struct cgireq req;
283 b8e64ccd 2021-03-31 op struct iri iri;
286 bc99d868 2021-03-19 op if (datalen != sizeof(req))
289 bc99d868 2021-03-19 op memcpy(&req, imsg->data, datalen);
291 bc99d868 2021-03-19 op iri.schema = req.iri_schema_off + req.buf;
292 bc99d868 2021-03-19 op iri.host = req.iri_host_off + req.buf;
293 bc99d868 2021-03-19 op iri.port = req.iri_port_off + req.buf;
294 bc99d868 2021-03-19 op iri.path = req.iri_path_off + req.buf;
295 bc99d868 2021-03-19 op iri.query = req.iri_query_off + req.buf;
296 bc99d868 2021-03-19 op iri.fragment = req.iri_fragment_off + req.buf;
298 bc99d868 2021-03-19 op /* patch the query, otherwise do_exec will always pass "" as
299 bc99d868 2021-03-19 op * first argument to the script. */
300 bc99d868 2021-03-19 op if (*iri.query == '\0')
301 bc99d868 2021-03-19 op iri.query = NULL;
303 b8e64ccd 2021-03-31 op if ((h = host_nth(req.host_off)) == NULL)
306 1feaf2a6 2021-05-15 op if ((l = loc_nth(h, req.loc_off)) == NULL)
309 fdea6aa0 2021-04-30 op fd = launch_cgi(&iri, &req, h, l);
310 bc99d868 2021-03-19 op imsg_compose(ibuf, IMSG_CGI_RES, imsg->hdr.peerid, 0, fd, NULL, 0);
311 bc99d868 2021-03-19 op imsg_flush(ibuf);
315 8ad1c570 2021-05-09 op fcgi_open_prog(struct fcgi *f)
322 2e2e189b 2021-07-08 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, s) == -1)
323 8ad1c570 2021-05-09 op err(1, "socketpair");
325 8ad1c570 2021-05-09 op switch (p = fork()) {
327 8ad1c570 2021-05-09 op err(1, "fork");
330 8ad1c570 2021-05-09 op if (dup2(s[1], 0) == -1)
331 8ad1c570 2021-05-09 op err(1, "dup2");
332 8ad1c570 2021-05-09 op execl(f->prog, f->prog, NULL);
333 8ad1c570 2021-05-09 op err(1, "execl %s", f->prog);
341 8ad1c570 2021-05-09 op fcgi_open_sock(struct fcgi *f)
343 8ad1c570 2021-05-09 op struct sockaddr_un addr;
346 8ad1c570 2021-05-09 op if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
347 8ad1c570 2021-05-09 op log_err(NULL, "socket: %s", strerror(errno));
351 8ad1c570 2021-05-09 op memset(&addr, 0, sizeof(addr));
352 8ad1c570 2021-05-09 op addr.sun_family = AF_UNIX;
353 8ad1c570 2021-05-09 op strlcpy(addr.sun_path, f->path, sizeof(addr.sun_path));
355 8ad1c570 2021-05-09 op if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
356 8ad1c570 2021-05-09 op log_warn(NULL, "failed to connect to %s: %s", f->path,
357 8ad1c570 2021-05-09 op strerror(errno));
366 8ad1c570 2021-05-09 op fcgi_open_conn(struct fcgi *f)
368 8ad1c570 2021-05-09 op struct addrinfo hints, *servinfo, *p;
371 8ad1c570 2021-05-09 op memset(&hints, 0, sizeof(hints));
372 8ad1c570 2021-05-09 op hints.ai_family = AF_UNSPEC;
373 8ad1c570 2021-05-09 op hints.ai_socktype = SOCK_STREAM;
374 8ad1c570 2021-05-09 op hints.ai_flags = AI_ADDRCONFIG;
376 8ad1c570 2021-05-09 op if ((r = getaddrinfo(f->path, f->port, &hints, &servinfo)) != 0) {
377 8ad1c570 2021-05-09 op log_warn(NULL, "getaddrinfo %s:%s: %s", f->path, f->port,
378 8ad1c570 2021-05-09 op gai_strerror(r));
382 8ad1c570 2021-05-09 op for (p = servinfo; p != NULL; p = p->ai_next) {
383 8ad1c570 2021-05-09 op sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
384 8ad1c570 2021-05-09 op if (sock == -1)
386 8ad1c570 2021-05-09 op if (connect(sock, p->ai_addr, p->ai_addrlen) == -1) {
393 8ad1c570 2021-05-09 op if (p == NULL) {
394 8ad1c570 2021-05-09 op log_warn(NULL, "couldn't connect to %s:%s", f->path, f->port);
398 8ad1c570 2021-05-09 op freeaddrinfo(servinfo);
403 8ad1c570 2021-05-09 op handle_imsg_fcgi_req(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
405 8ad1c570 2021-05-09 op struct fcgi *f;
408 8ad1c570 2021-05-09 op if (datalen != sizeof(id))
410 8ad1c570 2021-05-09 op memcpy(&id, imsg->data, datalen);
412 8ad1c570 2021-05-09 op if (id > FCGI_MAX || (fcgi[id].path == NULL && fcgi[id].prog == NULL))
415 8ad1c570 2021-05-09 op f = &fcgi[id];
416 8ad1c570 2021-05-09 op if (f->prog != NULL)
417 8ad1c570 2021-05-09 op fd = fcgi_open_prog(f);
418 8ad1c570 2021-05-09 op else if (f->port != NULL)
419 8ad1c570 2021-05-09 op fd = fcgi_open_conn(f);
421 8ad1c570 2021-05-09 op fd = fcgi_open_sock(f);
423 4cd25209 2021-10-07 op imsg_compose(ibuf, IMSG_FCGI_FD, imsg->hdr.peerid, 0, fd, NULL, 0);
424 8ad1c570 2021-05-09 op imsg_flush(ibuf);
428 bc99d868 2021-03-19 op handle_imsg_quit(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
434 bc99d868 2021-03-19 op (void)datalen;
436 bc99d868 2021-03-19 op for (i = 0; i < conf.prefork; ++i) {
437 bc99d868 2021-03-19 op imsg_compose(&servibuf[i], IMSG_QUIT, 0, 0, -1, NULL, 0);
438 bc99d868 2021-03-19 op imsg_flush(&exibuf);
439 bc99d868 2021-03-19 op close(servibuf[i].fd);
442 bc99d868 2021-03-19 op event_loopbreak();
446 bc99d868 2021-03-19 op handle_dispatch_imsg(int fd, short ev, void *d)
448 bc99d868 2021-03-19 op struct imsgbuf *ibuf = d;
449 bc99d868 2021-03-19 op dispatch_imsg(ibuf, handlers, sizeof(handlers));
453 bc99d868 2021-03-19 op executor_main(struct imsgbuf *ibuf)
455 bc99d868 2021-03-19 op struct event evs[PROC_MAX], imsgev;
458 2c3e53da 2021-03-03 op event_init();
460 bc99d868 2021-03-19 op if (ibuf != NULL) {
461 bc99d868 2021-03-19 op event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST,
462 bc99d868 2021-03-19 op handle_dispatch_imsg, ibuf);
463 bc99d868 2021-03-19 op event_add(&imsgev, NULL);
466 2c3e53da 2021-03-03 op for (i = 0; i < conf.prefork; ++i) {
467 bc99d868 2021-03-19 op event_set(&evs[i], servibuf[i].fd, EV_READ | EV_PERSIST,
468 bc99d868 2021-03-19 op handle_dispatch_imsg, &servibuf[i]);
469 2c3e53da 2021-03-03 op event_add(&evs[i], NULL);
472 62e001b0 2021-03-20 op sandbox_executor_process();
474 2c3e53da 2021-03-03 op event_dispatch();