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,
125 struct location *loc)
127 int p[2]; /* read end, write end */
136 case 0: { /* child */
138 char iribuf[GEMINI_URL_LEN];
143 if (dup2(p[1], 1) == -1)
146 ex = xasprintf("%s/%s", loc->dir, req->spath);
148 serialize_iri(iri, iribuf, sizeof(iribuf));
150 safe_setenv("GATEWAY_INTERFACE", "CGI/1.1");
151 safe_setenv("GEMINI_DOCUMENT_ROOT", loc->dir);
152 safe_setenv("GEMINI_SCRIPT_FILENAME",
153 xasprintf("%s/%s", loc->dir, req->spath));
154 safe_setenv("GEMINI_URL", iribuf);
156 strlcpy(path, "/", sizeof(path));
157 strlcat(path, req->spath, sizeof(path));
158 safe_setenv("GEMINI_URL_PATH", path);
160 if (*req->relpath != '\0') {
161 strlcpy(path, "/", sizeof(path));
162 strlcat(path, req->relpath, sizeof(path));
163 safe_setenv("PATH_INFO", path);
165 strlcpy(path, loc->dir, sizeof(path));
166 strlcat(path, "/", sizeof(path));
167 strlcat(path, req->relpath, sizeof(path));
168 safe_setenv("PATH_TRANSLATED", path);
171 safe_setenv("QUERY_STRING", iri->query);
172 safe_setenv("REMOTE_ADDR", req->addr);
173 safe_setenv("REMOTE_HOST", req->addr);
174 safe_setenv("REQUEST_METHOD", "");
176 strlcpy(path, "/", sizeof(path));
177 strlcat(path, req->spath, sizeof(path));
178 safe_setenv("SCRIPT_NAME", path);
180 safe_setenv("SERVER_NAME", iri->host);
182 snprintf(path, sizeof(path), "%d", conf.port);
183 safe_setenv("SERVER_PORT", path);
185 safe_setenv("SERVER_PROTOCOL", "GEMINI");
186 safe_setenv("SERVER_SOFTWARE", "gmid/1.6");
188 if (*req->subject != '\0')
189 safe_setenv("AUTH_TYPE", "Certificate");
191 safe_setenv("AUTH_TYPE", "");
193 safe_setenv("REMOTE_USER", req->subject);
194 safe_setenv("TLS_CLIENT_ISSUER", req->issuer);
195 safe_setenv("TLS_CLIENT_HASH", req->hash);
196 safe_setenv("TLS_VERSION", req->version);
197 safe_setenv("TLS_CIPHER", req->cipher);
199 snprintf(path, sizeof(path), "%d", req->cipher_strength);
200 safe_setenv("TLS_CIPHER_STRENGTH", path);
202 setenv_time("TLS_CLIENT_NOT_AFTER", req->notafter);
203 setenv_time("TLS_CLIENT_NOT_BEFORE", req->notbefore);
205 TAILQ_FOREACH(e, &vhost->env, envs) {
206 safe_setenv(e->name, e->value);
209 strlcpy(path, ex, sizeof(path));
217 do_exec(ex, req->spath, iri->query);
228 dprintf(p[1], "%d internal server error\r\n", TEMP_FAILURE);
232 static struct vhost *
237 TAILQ_FOREACH(h, &hosts, vhosts) {
246 static struct location *
247 loc_nth(struct vhost *vhost, size_t n)
249 struct location *loc;
251 TAILQ_FOREACH(loc, &vhost->locations, locations) {
261 handle_imsg_cgi_req(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
269 if (datalen != sizeof(req))
272 memcpy(&req, imsg->data, datalen);
274 iri.schema = req.iri_schema_off + req.buf;
275 iri.host = req.iri_host_off + req.buf;
276 iri.port = req.iri_port_off + req.buf;
277 iri.path = req.iri_path_off + req.buf;
278 iri.query = req.iri_query_off + req.buf;
279 iri.fragment = req.iri_fragment_off + req.buf;
281 /* patch the query, otherwise do_exec will always pass "" as
282 * first argument to the script. */
283 if (*iri.query == '\0')
286 if ((h = host_nth(req.host_off)) == NULL)
289 if ((l = loc_nth(h, req.host_off)) == NULL)
292 fd = launch_cgi(&iri, &req, h, l);
293 imsg_compose(ibuf, IMSG_CGI_RES, imsg->hdr.peerid, 0, fd, NULL, 0);
298 handle_imsg_quit(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
306 for (i = 0; i < conf.prefork; ++i) {
307 imsg_compose(&servibuf[i], IMSG_QUIT, 0, 0, -1, NULL, 0);
309 close(servibuf[i].fd);
316 handle_dispatch_imsg(int fd, short ev, void *d)
318 struct imsgbuf *ibuf = d;
319 dispatch_imsg(ibuf, handlers, sizeof(handlers));
323 executor_main(struct imsgbuf *ibuf)
325 struct event evs[PROC_MAX], imsgev;
331 event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST,
332 handle_dispatch_imsg, ibuf);
333 event_add(&imsgev, NULL);
336 for (i = 0; i < conf.prefork; ++i) {
337 event_set(&evs[i], servibuf[i].fd, EV_READ | EV_PERSIST,
338 handle_dispatch_imsg, &servibuf[i]);
339 event_add(&evs[i], NULL);
342 sandbox_executor_process();