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 strlcpy(buf, spath, sizeof(buf));
68 sname = basename(buf);
70 if (query == NULL || strchr(query, '=') != NULL) {
71 if ((argv = calloc(2, sizeof(char*))) == NULL)
75 warn("execvp: %s", argv[0]);
80 for (t = query ;; t++, n++) {
81 if ((t = strchr(t, '+')) == NULL)
85 if ((argv = calloc(n+2, sizeof(char*))) == NULL)
89 for (i = 0; i < n; ++i) {
90 t = strchr(query, '+');
93 argv[i+1] = pct_decode_str(query);
98 warn("execvp: %s", argv[0]);
102 setenv_time(const char *var, time_t t)
110 strftime(timebuf, sizeof(timebuf), "%FT%TZ",
111 gmtime_r(&t, &tminfo));
112 setenv(var, timebuf, 1);
115 /* fd or -1 on error */
117 launch_cgi(struct iri *iri, struct cgireq *req, struct vhost *vhost)
119 int p[2]; /* read end, write end */
128 case 0: { /* child */
130 char iribuf[GEMINI_URL_LEN];
134 if (dup2(p[1], 1) == -1)
137 ex = xasprintf("%s/%s", vhost->dir, req->spath);
139 serialize_iri(iri, iribuf, sizeof(iribuf));
141 safe_setenv("GATEWAY_INTERFACE", "CGI/1.1");
142 safe_setenv("GEMINI_DOCUMENT_ROOT", vhost->dir);
143 safe_setenv("GEMINI_SCRIPT_FILENAME",
144 xasprintf("%s/%s", vhost->dir, req->spath));
145 safe_setenv("GEMINI_URL", iribuf);
147 strlcpy(path, "/", sizeof(path));
148 strlcat(path, req->spath, sizeof(path));
149 safe_setenv("GEMINI_URL_PATH", path);
151 if (*req->relpath != '\0') {
152 strlcpy(path, "/", sizeof(path));
153 strlcat(path, req->relpath, sizeof(path));
154 safe_setenv("PATH_INFO", path);
156 strlcpy(path, vhost->dir, sizeof(path));
157 strlcat(path, "/", sizeof(path));
158 strlcat(path, req->relpath, sizeof(path));
159 safe_setenv("PATH_TRANSLATED", path);
162 safe_setenv("QUERY_STRING", iri->query);
163 safe_setenv("REMOTE_ADDR", req->addr);
164 safe_setenv("REMOTE_HOST", req->addr);
165 safe_setenv("REQUEST_METHOD", "");
167 strlcpy(path, "/", sizeof(path));
168 strlcat(path, req->spath, sizeof(path));
169 safe_setenv("SCRIPT_NAME", path);
171 safe_setenv("SERVER_NAME", iri->host);
173 snprintf(path, sizeof(path), "%d", conf.port);
174 safe_setenv("SERVER_PORT", path);
176 safe_setenv("SERVER_PROTOCOL", "GEMINI");
177 safe_setenv("SERVER_SOFTWARE", "gmid/1.6");
179 if (*req->subject != '\0')
180 safe_setenv("AUTH_TYPE", "Certificate");
182 safe_setenv("AUTH_TYPE", "");
184 safe_setenv("REMOTE_USER", req->subject);
185 safe_setenv("TLS_CLIENT_ISSUER", req->issuer);
186 safe_setenv("TLS_CLIENT_HASH", req->hash);
187 safe_setenv("TLS_VERSION", req->version);
188 safe_setenv("TLS_CIPHER", req->cipher);
190 snprintf(path, sizeof(path), "%d", req->cipher_strength);
191 safe_setenv("TLS_CIPHER_STRENGTH", path);
193 setenv_time("TLS_CLIENT_NOT_AFTER", req->notafter);
194 setenv_time("TLS_CLIENT_NOT_BEFORE", req->notbefore);
196 strlcpy(path, ex, sizeof(path));
204 do_exec(ex, req->spath, iri->query);
215 dprintf(p[1], "%d internal server error\r\n", TEMP_FAILURE);
219 static struct vhost *
224 TAILQ_FOREACH(h, &hosts, vhosts) {
234 handle_imsg_cgi_req(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
241 if (datalen != sizeof(req))
244 memcpy(&req, imsg->data, datalen);
246 iri.schema = req.iri_schema_off + req.buf;
247 iri.host = req.iri_host_off + req.buf;
248 iri.port = req.iri_port_off + req.buf;
249 iri.path = req.iri_path_off + req.buf;
250 iri.query = req.iri_query_off + req.buf;
251 iri.fragment = req.iri_fragment_off + req.buf;
253 /* patch the query, otherwise do_exec will always pass "" as
254 * first argument to the script. */
255 if (*iri.query == '\0')
258 if ((h = host_nth(req.host_off)) == NULL)
261 fd = launch_cgi(&iri, &req, h);
262 imsg_compose(ibuf, IMSG_CGI_RES, imsg->hdr.peerid, 0, fd, NULL, 0);
267 handle_imsg_quit(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
275 for (i = 0; i < conf.prefork; ++i) {
276 imsg_compose(&servibuf[i], IMSG_QUIT, 0, 0, -1, NULL, 0);
278 close(servibuf[i].fd);
285 handle_dispatch_imsg(int fd, short ev, void *d)
287 struct imsgbuf *ibuf = d;
288 dispatch_imsg(ibuf, handlers, sizeof(handlers));
292 executor_main(struct imsgbuf *ibuf)
294 struct event evs[PROC_MAX], imsgev;
300 event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST,
301 handle_dispatch_imsg, ibuf);
302 event_add(&imsgev, NULL);
305 for (i = 0; i < conf.prefork; ++i) {
306 event_set(&evs[i], servibuf[i].fd, EV_READ | EV_PERSIST,
307 handle_dispatch_imsg, &servibuf[i]);
308 event_add(&evs[i], NULL);
311 sandbox_executor_process();