Blob


1 /*
2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 *
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.
7 *
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.
15 */
17 #include "gmid.h"
19 #include <err.h>
20 #include <errno.h>
22 #include <event.h>
23 #include <fcntl.h>
24 #include <libgen.h>
25 #include <limits.h>
26 #include <signal.h>
27 #include <stdarg.h>
28 #include <string.h>
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,
37 };
39 static inline void
40 safe_setenv(const char *name, const char *val)
41 {
42 if (val == NULL)
43 val = "";
44 setenv(name, val, 1);
45 }
47 static char *
48 xasprintf(const char *fmt, ...)
49 {
50 va_list ap;
51 char *s;
53 va_start(ap, fmt);
54 if (vasprintf(&s, fmt, ap) == -1)
55 s = NULL;
56 va_end(ap);
58 return s;
59 }
61 static void
62 do_exec(const char *ex, const char *spath, char *query)
63 {
64 char **argv, buf[PATH_MAX], *sname, *t;
65 size_t i, n;
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)
79 err(1, "calloc");
80 argv[0] = sname;
81 execvp(ex, argv);
82 warn("execvp: %s", argv[0]);
83 return;
84 }
86 n = 1;
87 for (t = query ;; t++, n++) {
88 if ((t = strchr(t, '+')) == NULL)
89 break;
90 }
92 if ((argv = calloc(n+2, sizeof(char*))) == NULL)
93 err(1, "calloc");
95 argv[0] = sname;
96 for (i = 0; i < n; ++i) {
97 t = strchr(query, '+');
98 if (t != NULL)
99 *t = '\0';
100 argv[i+1] = pct_decode_str(query);
101 query = t+1;
104 execvp(ex, argv);
105 warn("execvp: %s", argv[0]);
108 static inline void
109 setenv_time(const char *var, time_t t)
111 char timebuf[21];
112 struct tm tminfo;
114 if (t == -1)
115 return;
117 strftime(timebuf, sizeof(timebuf), "%FT%TZ",
118 gmtime_r(&t, &tminfo));
119 setenv(var, timebuf, 1);
122 /* fd or -1 on error */
123 static int
124 launch_cgi(struct iri *iri, struct cgireq *req, struct vhost *vhost)
126 int p[2]; /* read end, write end */
128 if (pipe(p) == -1)
129 return -1;
131 switch (fork()) {
132 case -1:
133 return -1;
135 case 0: { /* child */
136 char *ex, *pwd;
137 char iribuf[GEMINI_URL_LEN];
138 char path[PATH_MAX];
140 close(p[0]);
141 if (dup2(p[1], 1) == -1)
142 goto childerr;
144 ex = xasprintf("%s/%s", vhost->dir, req->spath);
146 serialize_iri(iri, iribuf, sizeof(iribuf));
148 safe_setenv("GATEWAY_INTERFACE", "CGI/1.1");
149 safe_setenv("GEMINI_DOCUMENT_ROOT", vhost->dir);
150 safe_setenv("GEMINI_SCRIPT_FILENAME",
151 xasprintf("%s/%s", vhost->dir, req->spath));
152 safe_setenv("GEMINI_URL", iribuf);
154 strlcpy(path, "/", sizeof(path));
155 strlcat(path, req->spath, sizeof(path));
156 safe_setenv("GEMINI_URL_PATH", path);
158 if (*req->relpath != '\0') {
159 strlcpy(path, "/", sizeof(path));
160 strlcat(path, req->relpath, sizeof(path));
161 safe_setenv("PATH_INFO", path);
163 strlcpy(path, vhost->dir, sizeof(path));
164 strlcat(path, "/", sizeof(path));
165 strlcat(path, req->relpath, sizeof(path));
166 safe_setenv("PATH_TRANSLATED", path);
169 safe_setenv("QUERY_STRING", iri->query);
170 safe_setenv("REMOTE_ADDR", req->addr);
171 safe_setenv("REMOTE_HOST", req->addr);
172 safe_setenv("REQUEST_METHOD", "");
174 strlcpy(path, "/", sizeof(path));
175 strlcat(path, req->spath, sizeof(path));
176 safe_setenv("SCRIPT_NAME", path);
178 safe_setenv("SERVER_NAME", iri->host);
180 snprintf(path, sizeof(path), "%d", conf.port);
181 safe_setenv("SERVER_PORT", path);
183 safe_setenv("SERVER_PROTOCOL", "GEMINI");
184 safe_setenv("SERVER_SOFTWARE", "gmid/1.6");
186 if (*req->subject != '\0')
187 safe_setenv("AUTH_TYPE", "Certificate");
188 else
189 safe_setenv("AUTH_TYPE", "");
191 safe_setenv("REMOTE_USER", req->subject);
192 safe_setenv("TLS_CLIENT_ISSUER", req->issuer);
193 safe_setenv("TLS_CLIENT_HASH", req->hash);
194 safe_setenv("TLS_VERSION", req->version);
195 safe_setenv("TLS_CIPHER", req->cipher);
197 snprintf(path, sizeof(path), "%d", req->cipher_strength);
198 safe_setenv("TLS_CIPHER_STRENGTH", path);
200 setenv_time("TLS_CLIENT_NOT_AFTER", req->notafter);
201 setenv_time("TLS_CLIENT_NOT_BEFORE", req->notbefore);
203 strlcpy(path, ex, sizeof(path));
205 pwd = dirname(path);
206 if (chdir(pwd)) {
207 warn("chdir");
208 goto childerr;
211 do_exec(ex, req->spath, iri->query);
212 goto childerr;
215 default:
216 close(p[1]);
217 mark_nonblock(p[0]);
218 return p[0];
221 childerr:
222 dprintf(p[1], "%d internal server error\r\n", TEMP_FAILURE);
223 _exit(1);
226 static struct vhost *
227 host_nth(size_t n)
229 struct vhost *h;
231 TAILQ_FOREACH(h, &hosts, vhosts) {
232 if (n == 0)
233 return h;
234 n--;
237 return NULL;
240 static void
241 handle_imsg_cgi_req(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
243 struct vhost *h;
244 struct cgireq req;
245 struct iri iri;
246 int fd;
248 if (datalen != sizeof(req))
249 abort();
251 memcpy(&req, imsg->data, datalen);
253 iri.schema = req.iri_schema_off + req.buf;
254 iri.host = req.iri_host_off + req.buf;
255 iri.port = req.iri_port_off + req.buf;
256 iri.path = req.iri_path_off + req.buf;
257 iri.query = req.iri_query_off + req.buf;
258 iri.fragment = req.iri_fragment_off + req.buf;
260 /* patch the query, otherwise do_exec will always pass "" as
261 * first argument to the script. */
262 if (*iri.query == '\0')
263 iri.query = NULL;
265 if ((h = host_nth(req.host_off)) == NULL)
266 abort();
268 fd = launch_cgi(&iri, &req, h);
269 imsg_compose(ibuf, IMSG_CGI_RES, imsg->hdr.peerid, 0, fd, NULL, 0);
270 imsg_flush(ibuf);
273 static void
274 handle_imsg_quit(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
276 int i;
278 (void)ibuf;
279 (void)imsg;
280 (void)datalen;
282 for (i = 0; i < conf.prefork; ++i) {
283 imsg_compose(&servibuf[i], IMSG_QUIT, 0, 0, -1, NULL, 0);
284 imsg_flush(&exibuf);
285 close(servibuf[i].fd);
288 event_loopbreak();
291 static void
292 handle_dispatch_imsg(int fd, short ev, void *d)
294 struct imsgbuf *ibuf = d;
295 dispatch_imsg(ibuf, handlers, sizeof(handlers));
298 int
299 executor_main(struct imsgbuf *ibuf)
301 struct event evs[PROC_MAX], imsgev;
302 int i;
304 event_init();
306 if (ibuf != NULL) {
307 event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST,
308 handle_dispatch_imsg, ibuf);
309 event_add(&imsgev, NULL);
312 for (i = 0; i < conf.prefork; ++i) {
313 event_set(&evs[i], servibuf[i].fd, EV_READ | EV_PERSIST,
314 handle_dispatch_imsg, &servibuf[i]);
315 event_add(&evs[i], NULL);
318 sandbox_executor_process();
320 event_dispatch();
322 return 1;