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.
30 static void handle_imsg_cgi_req(struct imsgbuf*, struct imsg*, size_t);
31 static void handle_imsg_quit(struct imsgbuf*, struct imsg*, size_t);
32 static void handle_dispatch_imsg(int, short, void*);
34 static imsg_handlerfn *handlers[] = {
35 [IMSG_CGI_REQ] = handle_imsg_cgi_req,
36 [IMSG_QUIT] = handle_imsg_quit,
40 safe_setenv(const char *name, const char *val)
48 xasprintf(const char *fmt, ...)
54 if (vasprintf(&s, fmt, ap) == -1)
62 do_exec(const char *ex, const char *spath, char *query)
64 char **argv, buf[PATH_MAX], *sname, *t;
67 /* restore the default handlers */
68 signal(SIGPIPE, SIG_DFL);
69 signal(SIGCHLD, SIG_DFL);
70 signal(SIGHUP, SIG_DFL);
71 signal(SIGINT, SIG_DFL);
72 signal(SIGTERM, SIG_DFL);
74 strlcpy(buf, spath, sizeof(buf));
75 sname = basename(buf);
77 if (query == NULL || strchr(query, '=') != NULL) {
78 if ((argv = calloc(2, sizeof(char*))) == NULL)
82 warn("execvp: %s", argv[0]);
87 for (t = query ;; t++, n++) {
88 if ((t = strchr(t, '+')) == NULL)
92 if ((argv = calloc(n+2, sizeof(char*))) == NULL)
96 for (i = 0; i < n; ++i) {
97 t = strchr(query, '+');
100 argv[i+1] = pct_decode_str(query);
105 warn("execvp: %s", argv[0]);
109 setenv_time(const char *var, time_t t)
117 strftime(timebuf, sizeof(timebuf), "%FT%TZ",
118 gmtime_r(&t, &tminfo));
119 setenv(var, timebuf, 1);
122 /* fd or -1 on error */
124 launch_cgi(struct iri *iri, struct cgireq *req, struct vhost *vhost)
126 int p[2]; /* read end, write end */
135 case 0: { /* child */
137 char iribuf[GEMINI_URL_LEN];
142 if (dup2(p[1], 1) == -1)
145 ex = xasprintf("%s/%s", vhost->dir, req->spath);
147 serialize_iri(iri, iribuf, sizeof(iribuf));
149 safe_setenv("GATEWAY_INTERFACE", "CGI/1.1");
150 safe_setenv("GEMINI_DOCUMENT_ROOT", vhost->dir);
151 safe_setenv("GEMINI_SCRIPT_FILENAME",
152 xasprintf("%s/%s", vhost->dir, req->spath));
153 safe_setenv("GEMINI_URL", iribuf);
155 strlcpy(path, "/", sizeof(path));
156 strlcat(path, req->spath, sizeof(path));
157 safe_setenv("GEMINI_URL_PATH", path);
159 if (*req->relpath != '\0') {
160 strlcpy(path, "/", sizeof(path));
161 strlcat(path, req->relpath, sizeof(path));
162 safe_setenv("PATH_INFO", path);
164 strlcpy(path, vhost->dir, sizeof(path));
165 strlcat(path, "/", sizeof(path));
166 strlcat(path, req->relpath, sizeof(path));
167 safe_setenv("PATH_TRANSLATED", path);
170 safe_setenv("QUERY_STRING", iri->query);
171 safe_setenv("REMOTE_ADDR", req->addr);
172 safe_setenv("REMOTE_HOST", req->addr);
173 safe_setenv("REQUEST_METHOD", "");
175 strlcpy(path, "/", sizeof(path));
176 strlcat(path, req->spath, sizeof(path));
177 safe_setenv("SCRIPT_NAME", path);
179 safe_setenv("SERVER_NAME", iri->host);
181 snprintf(path, sizeof(path), "%d", conf.port);
182 safe_setenv("SERVER_PORT", path);
184 safe_setenv("SERVER_PROTOCOL", "GEMINI");
185 safe_setenv("SERVER_SOFTWARE", "gmid/1.6");
187 if (*req->subject != '\0')
188 safe_setenv("AUTH_TYPE", "Certificate");
190 safe_setenv("AUTH_TYPE", "");
192 safe_setenv("REMOTE_USER", req->subject);
193 safe_setenv("TLS_CLIENT_ISSUER", req->issuer);
194 safe_setenv("TLS_CLIENT_HASH", req->hash);
195 safe_setenv("TLS_VERSION", req->version);
196 safe_setenv("TLS_CIPHER", req->cipher);
198 snprintf(path, sizeof(path), "%d", req->cipher_strength);
199 safe_setenv("TLS_CIPHER_STRENGTH", path);
201 setenv_time("TLS_CLIENT_NOT_AFTER", req->notafter);
202 setenv_time("TLS_CLIENT_NOT_BEFORE", req->notbefore);
204 TAILQ_FOREACH(e, &vhost->env, envs) {
205 safe_setenv(e->name, e->value);
208 strlcpy(path, ex, sizeof(path));
216 do_exec(ex, req->spath, iri->query);
227 dprintf(p[1], "%d internal server error\r\n", TEMP_FAILURE);
231 static struct vhost *
236 TAILQ_FOREACH(h, &hosts, vhosts) {
246 handle_imsg_cgi_req(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
253 if (datalen != sizeof(req))
256 memcpy(&req, imsg->data, datalen);
258 iri.schema = req.iri_schema_off + req.buf;
259 iri.host = req.iri_host_off + req.buf;
260 iri.port = req.iri_port_off + req.buf;
261 iri.path = req.iri_path_off + req.buf;
262 iri.query = req.iri_query_off + req.buf;
263 iri.fragment = req.iri_fragment_off + req.buf;
265 /* patch the query, otherwise do_exec will always pass "" as
266 * first argument to the script. */
267 if (*iri.query == '\0')
270 if ((h = host_nth(req.host_off)) == NULL)
273 fd = launch_cgi(&iri, &req, h);
274 imsg_compose(ibuf, IMSG_CGI_RES, imsg->hdr.peerid, 0, fd, NULL, 0);
279 handle_imsg_quit(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
287 for (i = 0; i < conf.prefork; ++i) {
288 imsg_compose(&servibuf[i], IMSG_QUIT, 0, 0, -1, NULL, 0);
290 close(servibuf[i].fd);
297 handle_dispatch_imsg(int fd, short ev, void *d)
299 struct imsgbuf *ibuf = d;
300 dispatch_imsg(ibuf, handlers, sizeof(handlers));
304 executor_main(struct imsgbuf *ibuf)
306 struct event evs[PROC_MAX], imsgev;
312 event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST,
313 handle_dispatch_imsg, ibuf);
314 event_add(&imsgev, NULL);
317 for (i = 0; i < conf.prefork; ++i) {
318 event_set(&evs[i], servibuf[i].fd, EV_READ | EV_PERSIST,
319 handle_dispatch_imsg, &servibuf[i]);
320 event_add(&evs[i], NULL);
323 sandbox_executor_process();