2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
32 static void handle_imsg_cgi_req(struct imsgbuf*, struct imsg*, size_t);
33 static void handle_imsg_fcgi_req(struct imsgbuf*, struct imsg*, size_t);
34 static void handle_imsg_quit(struct imsgbuf*, struct imsg*, size_t);
35 static void handle_dispatch_imsg(int, short, void*);
37 static imsg_handlerfn *handlers[] = {
38 [IMSG_FCGI_REQ] = handle_imsg_fcgi_req,
39 [IMSG_CGI_REQ] = handle_imsg_cgi_req,
40 [IMSG_QUIT] = handle_imsg_quit,
44 safe_setenv(const char *name, const char *val)
52 xasprintf(const char *fmt, ...)
58 if (vasprintf(&s, fmt, ap) == -1)
66 do_exec(const char *ex, const char *spath, char *query)
68 char **argv, buf[PATH_MAX], *sname, *t;
71 /* restore the default handlers */
72 signal(SIGPIPE, SIG_DFL);
73 signal(SIGCHLD, SIG_DFL);
74 signal(SIGHUP, SIG_DFL);
75 signal(SIGINT, SIG_DFL);
76 signal(SIGTERM, SIG_DFL);
78 strlcpy(buf, spath, sizeof(buf));
79 sname = basename(buf);
81 if (query == NULL || strchr(query, '=') != NULL) {
82 if ((argv = calloc(2, sizeof(char*))) == NULL)
86 warn("execvp: %s", argv[0]);
91 for (t = query ;; t++, n++) {
92 if ((t = strchr(t, '+')) == NULL)
96 if ((argv = calloc(n+2, sizeof(char*))) == NULL)
100 for (i = 0; i < n; ++i) {
101 t = strchr(query, '+');
104 argv[i+1] = pct_decode_str(query);
109 warn("execvp: %s", argv[0]);
113 setenv_time(const char *var, time_t t)
121 strftime(timebuf, sizeof(timebuf), "%FT%TZ",
122 gmtime_r(&t, &tminfo));
123 setenv(var, timebuf, 1);
126 /* fd or -1 on error */
128 launch_cgi(struct iri *iri, struct cgireq *req, struct vhost *vhost,
129 struct location *loc)
131 int p[2]; /* read end, write end */
140 case 0: { /* child */
142 char iribuf[GEMINI_URL_LEN];
147 if (dup2(p[1], 1) == -1)
150 ex = xasprintf("%s/%s", loc->dir, req->spath);
152 serialize_iri(iri, iribuf, sizeof(iribuf));
154 safe_setenv("GATEWAY_INTERFACE", "CGI/1.1");
155 safe_setenv("GEMINI_DOCUMENT_ROOT", loc->dir);
156 safe_setenv("GEMINI_SCRIPT_FILENAME",
157 xasprintf("%s/%s", loc->dir, req->spath));
158 safe_setenv("GEMINI_URL", iribuf);
160 strlcpy(path, "/", sizeof(path));
161 strlcat(path, req->spath, sizeof(path));
162 safe_setenv("GEMINI_URL_PATH", path);
164 if (*req->relpath != '\0') {
165 strlcpy(path, "/", sizeof(path));
166 strlcat(path, req->relpath, sizeof(path));
167 safe_setenv("PATH_INFO", path);
169 strlcpy(path, loc->dir, sizeof(path));
170 strlcat(path, "/", sizeof(path));
171 strlcat(path, req->relpath, sizeof(path));
172 safe_setenv("PATH_TRANSLATED", path);
175 safe_setenv("QUERY_STRING", iri->query);
176 safe_setenv("REMOTE_ADDR", req->addr);
177 safe_setenv("REMOTE_HOST", req->addr);
178 safe_setenv("REQUEST_METHOD", "");
180 strlcpy(path, "/", sizeof(path));
181 strlcat(path, req->spath, sizeof(path));
182 safe_setenv("SCRIPT_NAME", path);
184 safe_setenv("SERVER_NAME", iri->host);
186 snprintf(path, sizeof(path), "%d", conf.port);
187 safe_setenv("SERVER_PORT", path);
189 safe_setenv("SERVER_PROTOCOL", "GEMINI");
190 safe_setenv("SERVER_SOFTWARE", GMID_VERSION);
192 if (*req->subject != '\0')
193 safe_setenv("AUTH_TYPE", "Certificate");
195 safe_setenv("AUTH_TYPE", "");
197 safe_setenv("REMOTE_USER", req->subject);
198 safe_setenv("TLS_CLIENT_ISSUER", req->issuer);
199 safe_setenv("TLS_CLIENT_HASH", req->hash);
200 safe_setenv("TLS_VERSION", req->version);
201 safe_setenv("TLS_CIPHER", req->cipher);
203 snprintf(path, sizeof(path), "%d", req->cipher_strength);
204 safe_setenv("TLS_CIPHER_STRENGTH", path);
206 setenv_time("TLS_CLIENT_NOT_AFTER", req->notafter);
207 setenv_time("TLS_CLIENT_NOT_BEFORE", req->notbefore);
209 TAILQ_FOREACH(e, &vhost->env, envs) {
210 safe_setenv(e->name, e->value);
213 strlcpy(path, ex, sizeof(path));
221 do_exec(ex, req->spath, iri->query);
232 dprintf(p[1], "%d internal server error\r\n", TEMP_FAILURE);
236 static struct vhost *
241 TAILQ_FOREACH(h, &hosts, vhosts) {
250 static struct location *
251 loc_nth(struct vhost *vhost, size_t n)
253 struct location *loc;
255 TAILQ_FOREACH(loc, &vhost->locations, locations) {
265 handle_imsg_cgi_req(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
273 if (datalen != sizeof(req))
276 memcpy(&req, imsg->data, datalen);
278 iri.schema = req.iri_schema_off + req.buf;
279 iri.host = req.iri_host_off + req.buf;
280 iri.port = req.iri_port_off + req.buf;
281 iri.path = req.iri_path_off + req.buf;
282 iri.query = req.iri_query_off + req.buf;
283 iri.fragment = req.iri_fragment_off + req.buf;
285 /* patch the query, otherwise do_exec will always pass "" as
286 * first argument to the script. */
287 if (*iri.query == '\0')
290 if ((h = host_nth(req.host_off)) == NULL)
293 if ((l = loc_nth(h, req.loc_off)) == NULL)
296 fd = launch_cgi(&iri, &req, h, l);
297 imsg_compose(ibuf, IMSG_CGI_RES, imsg->hdr.peerid, 0, fd, NULL, 0);
302 fcgi_open_prog(struct fcgi *f)
309 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNIX, s) == -1)
310 err(1, "socketpair");
312 switch (p = fork()) {
317 if (dup2(s[1], 0) == -1)
319 execl(f->prog, f->prog, NULL);
320 err(1, "execl %s", f->prog);
328 fcgi_open_sock(struct fcgi *f)
330 struct sockaddr_un addr;
333 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
334 log_err(NULL, "socket: %s", strerror(errno));
338 memset(&addr, 0, sizeof(addr));
339 addr.sun_family = AF_UNIX;
340 strlcpy(addr.sun_path, f->path, sizeof(addr.sun_path));
342 if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
343 log_warn(NULL, "failed to connect to %s: %s", f->path,
353 fcgi_open_conn(struct fcgi *f)
355 struct addrinfo hints, *servinfo, *p;
358 memset(&hints, 0, sizeof(hints));
359 hints.ai_family = AF_UNSPEC;
360 hints.ai_socktype = SOCK_STREAM;
361 hints.ai_flags = AI_ADDRCONFIG;
363 if ((r = getaddrinfo(f->path, f->port, &hints, &servinfo)) != 0) {
364 log_warn(NULL, "getaddrinfo %s:%s: %s", f->path, f->port,
369 for (p = servinfo; p != NULL; p = p->ai_next) {
370 sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
373 if (connect(sock, p->ai_addr, p->ai_addrlen) == -1) {
381 log_warn(NULL, "couldn't connect to %s:%s", f->path, f->port);
385 freeaddrinfo(servinfo);
390 handle_imsg_fcgi_req(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
395 if (datalen != sizeof(id))
397 memcpy(&id, imsg->data, datalen);
399 if (id > FCGI_MAX || (fcgi[id].path == NULL && fcgi[id].prog == NULL))
404 fd = fcgi_open_prog(f);
405 else if (f->port != NULL)
406 fd = fcgi_open_conn(f);
408 fd = fcgi_open_sock(f);
410 imsg_compose(ibuf, IMSG_FCGI_FD, id, 0, fd, NULL, 0);
415 handle_imsg_quit(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
423 for (i = 0; i < conf.prefork; ++i) {
424 imsg_compose(&servibuf[i], IMSG_QUIT, 0, 0, -1, NULL, 0);
426 close(servibuf[i].fd);
433 handle_dispatch_imsg(int fd, short ev, void *d)
435 struct imsgbuf *ibuf = d;
436 dispatch_imsg(ibuf, handlers, sizeof(handlers));
440 executor_main(struct imsgbuf *ibuf)
442 struct event evs[PROC_MAX], imsgev;
448 event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST,
449 handle_dispatch_imsg, ibuf);
450 event_add(&imsgev, NULL);
453 for (i = 0; i < conf.prefork; ++i) {
454 event_set(&evs[i], servibuf[i].fd, EV_READ | EV_PERSIST,
455 handle_dispatch_imsg, &servibuf[i]);
456 event_add(&evs[i], NULL);
459 sandbox_executor_process();