Blame


1 881a9dd9 2021-01-16 op /*
2 881a9dd9 2021-01-16 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 881a9dd9 2021-01-16 op *
4 881a9dd9 2021-01-16 op * Permission to use, copy, modify, and distribute this software for any
5 881a9dd9 2021-01-16 op * purpose with or without fee is hereby granted, provided that the above
6 881a9dd9 2021-01-16 op * copyright notice and this permission notice appear in all copies.
7 881a9dd9 2021-01-16 op *
8 881a9dd9 2021-01-16 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 881a9dd9 2021-01-16 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 881a9dd9 2021-01-16 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 881a9dd9 2021-01-16 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 881a9dd9 2021-01-16 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 881a9dd9 2021-01-16 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 881a9dd9 2021-01-16 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 881a9dd9 2021-01-16 op */
16 52418c8d 2021-02-12 op
17 52418c8d 2021-02-12 op #include "gmid.h"
18 881a9dd9 2021-01-16 op
19 881a9dd9 2021-01-16 op #include <err.h>
20 881a9dd9 2021-01-16 op #include <errno.h>
21 881a9dd9 2021-01-16 op
22 2c3e53da 2021-03-03 op #include <event.h>
23 881a9dd9 2021-01-16 op #include <fcntl.h>
24 2fafa2d2 2021-02-01 op #include <libgen.h>
25 2fafa2d2 2021-02-01 op #include <limits.h>
26 881a9dd9 2021-01-16 op #include <signal.h>
27 2fafa2d2 2021-02-01 op #include <stdarg.h>
28 881a9dd9 2021-01-16 op #include <string.h>
29 881a9dd9 2021-01-16 op
30 bc99d868 2021-03-19 op static void handle_imsg_cgi_req(struct imsgbuf*, struct imsg*, size_t);
31 bc99d868 2021-03-19 op static void handle_imsg_quit(struct imsgbuf*, struct imsg*, size_t);
32 bc99d868 2021-03-19 op static void handle_dispatch_imsg(int, short, void*);
33 881a9dd9 2021-01-16 op
34 bc99d868 2021-03-19 op static imsg_handlerfn *handlers[] = {
35 bc99d868 2021-03-19 op [IMSG_CGI_REQ] = handle_imsg_cgi_req,
36 bc99d868 2021-03-19 op [IMSG_QUIT] = handle_imsg_quit,
37 bc99d868 2021-03-19 op };
38 881a9dd9 2021-01-16 op
39 881a9dd9 2021-01-16 op static inline void
40 881a9dd9 2021-01-16 op safe_setenv(const char *name, const char *val)
41 881a9dd9 2021-01-16 op {
42 881a9dd9 2021-01-16 op if (val == NULL)
43 881a9dd9 2021-01-16 op val = "";
44 881a9dd9 2021-01-16 op setenv(name, val, 1);
45 881a9dd9 2021-01-16 op }
46 881a9dd9 2021-01-16 op
47 2fafa2d2 2021-02-01 op static char *
48 2fafa2d2 2021-02-01 op xasprintf(const char *fmt, ...)
49 2fafa2d2 2021-02-01 op {
50 2fafa2d2 2021-02-01 op va_list ap;
51 2fafa2d2 2021-02-01 op char *s;
52 2fafa2d2 2021-02-01 op
53 2fafa2d2 2021-02-01 op va_start(ap, fmt);
54 2fafa2d2 2021-02-01 op if (vasprintf(&s, fmt, ap) == -1)
55 2fafa2d2 2021-02-01 op s = NULL;
56 2fafa2d2 2021-02-01 op va_end(ap);
57 2fafa2d2 2021-02-01 op
58 2fafa2d2 2021-02-01 op return s;
59 9f006a21 2021-02-07 op }
60 9f006a21 2021-02-07 op
61 9f006a21 2021-02-07 op static void
62 9f006a21 2021-02-07 op do_exec(const char *ex, const char *spath, char *query)
63 9f006a21 2021-02-07 op {
64 9f006a21 2021-02-07 op char **argv, buf[PATH_MAX], *sname, *t;
65 9f006a21 2021-02-07 op size_t i, n;
66 9f006a21 2021-02-07 op
67 3841a369 2021-04-20 op /* restore the default handlers */
68 3841a369 2021-04-20 op signal(SIGPIPE, SIG_DFL);
69 3841a369 2021-04-20 op signal(SIGCHLD, SIG_DFL);
70 3841a369 2021-04-20 op signal(SIGHUP, SIG_DFL);
71 3841a369 2021-04-20 op signal(SIGINT, SIG_DFL);
72 3841a369 2021-04-20 op signal(SIGTERM, SIG_DFL);
73 3841a369 2021-04-20 op
74 9f006a21 2021-02-07 op strlcpy(buf, spath, sizeof(buf));
75 9f006a21 2021-02-07 op sname = basename(buf);
76 9f006a21 2021-02-07 op
77 9f006a21 2021-02-07 op if (query == NULL || strchr(query, '=') != NULL) {
78 9f006a21 2021-02-07 op if ((argv = calloc(2, sizeof(char*))) == NULL)
79 9f006a21 2021-02-07 op err(1, "calloc");
80 9f006a21 2021-02-07 op argv[0] = sname;
81 9f006a21 2021-02-07 op execvp(ex, argv);
82 9f006a21 2021-02-07 op warn("execvp: %s", argv[0]);
83 9f006a21 2021-02-07 op return;
84 9f006a21 2021-02-07 op }
85 9f006a21 2021-02-07 op
86 9f006a21 2021-02-07 op n = 1;
87 9f006a21 2021-02-07 op for (t = query ;; t++, n++) {
88 9f006a21 2021-02-07 op if ((t = strchr(t, '+')) == NULL)
89 9f006a21 2021-02-07 op break;
90 9f006a21 2021-02-07 op }
91 9f006a21 2021-02-07 op
92 9f006a21 2021-02-07 op if ((argv = calloc(n+2, sizeof(char*))) == NULL)
93 9f006a21 2021-02-07 op err(1, "calloc");
94 9f006a21 2021-02-07 op
95 9f006a21 2021-02-07 op argv[0] = sname;
96 9f006a21 2021-02-07 op for (i = 0; i < n; ++i) {
97 9f006a21 2021-02-07 op t = strchr(query, '+');
98 9f006a21 2021-02-07 op if (t != NULL)
99 9f006a21 2021-02-07 op *t = '\0';
100 9f006a21 2021-02-07 op argv[i+1] = pct_decode_str(query);
101 9f006a21 2021-02-07 op query = t+1;
102 9f006a21 2021-02-07 op }
103 9f006a21 2021-02-07 op
104 9f006a21 2021-02-07 op execvp(ex, argv);
105 9f006a21 2021-02-07 op warn("execvp: %s", argv[0]);
106 b63e30ff 2021-02-07 op }
107 b63e30ff 2021-02-07 op
108 b63e30ff 2021-02-07 op static inline void
109 b63e30ff 2021-02-07 op setenv_time(const char *var, time_t t)
110 b63e30ff 2021-02-07 op {
111 b63e30ff 2021-02-07 op char timebuf[21];
112 b63e30ff 2021-02-07 op struct tm tminfo;
113 b63e30ff 2021-02-07 op
114 b63e30ff 2021-02-07 op if (t == -1)
115 b63e30ff 2021-02-07 op return;
116 b63e30ff 2021-02-07 op
117 b63e30ff 2021-02-07 op strftime(timebuf, sizeof(timebuf), "%FT%TZ",
118 b63e30ff 2021-02-07 op gmtime_r(&t, &tminfo));
119 b63e30ff 2021-02-07 op setenv(var, timebuf, 1);
120 2fafa2d2 2021-02-01 op }
121 2fafa2d2 2021-02-01 op
122 881a9dd9 2021-01-16 op /* fd or -1 on error */
123 881a9dd9 2021-01-16 op static int
124 fdea6aa0 2021-04-30 op launch_cgi(struct iri *iri, struct cgireq *req, struct vhost *vhost,
125 fdea6aa0 2021-04-30 op struct location *loc)
126 881a9dd9 2021-01-16 op {
127 881a9dd9 2021-01-16 op int p[2]; /* read end, write end */
128 881a9dd9 2021-01-16 op
129 52053e1a 2021-02-06 op if (pipe(p) == -1)
130 881a9dd9 2021-01-16 op return -1;
131 881a9dd9 2021-01-16 op
132 881a9dd9 2021-01-16 op switch (fork()) {
133 881a9dd9 2021-01-16 op case -1:
134 881a9dd9 2021-01-16 op return -1;
135 881a9dd9 2021-01-16 op
136 881a9dd9 2021-01-16 op case 0: { /* child */
137 2fafa2d2 2021-02-01 op char *ex, *pwd;
138 2fafa2d2 2021-02-01 op char iribuf[GEMINI_URL_LEN];
139 2fafa2d2 2021-02-01 op char path[PATH_MAX];
140 9cc630aa 2021-04-28 op struct envlist *e;
141 881a9dd9 2021-01-16 op
142 881a9dd9 2021-01-16 op close(p[0]);
143 881a9dd9 2021-01-16 op if (dup2(p[1], 1) == -1)
144 881a9dd9 2021-01-16 op goto childerr;
145 881a9dd9 2021-01-16 op
146 fdea6aa0 2021-04-30 op ex = xasprintf("%s/%s", loc->dir, req->spath);
147 881a9dd9 2021-01-16 op
148 2fafa2d2 2021-02-01 op serialize_iri(iri, iribuf, sizeof(iribuf));
149 881a9dd9 2021-01-16 op
150 881a9dd9 2021-01-16 op safe_setenv("GATEWAY_INTERFACE", "CGI/1.1");
151 fdea6aa0 2021-04-30 op safe_setenv("GEMINI_DOCUMENT_ROOT", loc->dir);
152 2fafa2d2 2021-02-01 op safe_setenv("GEMINI_SCRIPT_FILENAME",
153 fdea6aa0 2021-04-30 op xasprintf("%s/%s", loc->dir, req->spath));
154 2fafa2d2 2021-02-01 op safe_setenv("GEMINI_URL", iribuf);
155 881a9dd9 2021-01-16 op
156 2fafa2d2 2021-02-01 op strlcpy(path, "/", sizeof(path));
157 bc99d868 2021-03-19 op strlcat(path, req->spath, sizeof(path));
158 2fafa2d2 2021-02-01 op safe_setenv("GEMINI_URL_PATH", path);
159 881a9dd9 2021-01-16 op
160 bc99d868 2021-03-19 op if (*req->relpath != '\0') {
161 2fafa2d2 2021-02-01 op strlcpy(path, "/", sizeof(path));
162 bc99d868 2021-03-19 op strlcat(path, req->relpath, sizeof(path));
163 2fafa2d2 2021-02-01 op safe_setenv("PATH_INFO", path);
164 2fafa2d2 2021-02-01 op
165 fdea6aa0 2021-04-30 op strlcpy(path, loc->dir, sizeof(path));
166 2fafa2d2 2021-02-01 op strlcat(path, "/", sizeof(path));
167 bc99d868 2021-03-19 op strlcat(path, req->relpath, sizeof(path));
168 2fafa2d2 2021-02-01 op safe_setenv("PATH_TRANSLATED", path);
169 2fafa2d2 2021-02-01 op }
170 2fafa2d2 2021-02-01 op
171 2fafa2d2 2021-02-01 op safe_setenv("QUERY_STRING", iri->query);
172 bc99d868 2021-03-19 op safe_setenv("REMOTE_ADDR", req->addr);
173 bc99d868 2021-03-19 op safe_setenv("REMOTE_HOST", req->addr);
174 2fafa2d2 2021-02-01 op safe_setenv("REQUEST_METHOD", "");
175 881a9dd9 2021-01-16 op
176 2fafa2d2 2021-02-01 op strlcpy(path, "/", sizeof(path));
177 bc99d868 2021-03-19 op strlcat(path, req->spath, sizeof(path));
178 2fafa2d2 2021-02-01 op safe_setenv("SCRIPT_NAME", path);
179 2fafa2d2 2021-02-01 op
180 2fafa2d2 2021-02-01 op safe_setenv("SERVER_NAME", iri->host);
181 2fafa2d2 2021-02-01 op
182 2fafa2d2 2021-02-01 op snprintf(path, sizeof(path), "%d", conf.port);
183 2fafa2d2 2021-02-01 op safe_setenv("SERVER_PORT", path);
184 2fafa2d2 2021-02-01 op
185 2fafa2d2 2021-02-01 op safe_setenv("SERVER_PROTOCOL", "GEMINI");
186 c8249bad 2021-03-20 op safe_setenv("SERVER_SOFTWARE", "gmid/1.6");
187 2fafa2d2 2021-02-01 op
188 bc99d868 2021-03-19 op if (*req->subject != '\0')
189 881a9dd9 2021-01-16 op safe_setenv("AUTH_TYPE", "Certificate");
190 3e541809 2021-02-01 op else
191 3e541809 2021-02-01 op safe_setenv("AUTH_TYPE", "");
192 05748e49 2021-01-24 op
193 bc99d868 2021-03-19 op safe_setenv("REMOTE_USER", req->subject);
194 bc99d868 2021-03-19 op safe_setenv("TLS_CLIENT_ISSUER", req->issuer);
195 bc99d868 2021-03-19 op safe_setenv("TLS_CLIENT_HASH", req->hash);
196 89541eee 2021-04-13 op safe_setenv("TLS_VERSION", req->version);
197 89541eee 2021-04-13 op safe_setenv("TLS_CIPHER", req->cipher);
198 89541eee 2021-04-13 op
199 89541eee 2021-04-13 op snprintf(path, sizeof(path), "%d", req->cipher_strength);
200 89541eee 2021-04-13 op safe_setenv("TLS_CIPHER_STRENGTH", path);
201 89541eee 2021-04-13 op
202 bc99d868 2021-03-19 op setenv_time("TLS_CLIENT_NOT_AFTER", req->notafter);
203 bc99d868 2021-03-19 op setenv_time("TLS_CLIENT_NOT_BEFORE", req->notbefore);
204 3e541809 2021-02-01 op
205 9cc630aa 2021-04-28 op TAILQ_FOREACH(e, &vhost->env, envs) {
206 9cc630aa 2021-04-28 op safe_setenv(e->name, e->value);
207 9cc630aa 2021-04-28 op }
208 9cc630aa 2021-04-28 op
209 9f006a21 2021-02-07 op strlcpy(path, ex, sizeof(path));
210 9f006a21 2021-02-07 op
211 2fafa2d2 2021-02-01 op pwd = dirname(path);
212 2fafa2d2 2021-02-01 op if (chdir(pwd)) {
213 2fafa2d2 2021-02-01 op warn("chdir");
214 2fafa2d2 2021-02-01 op goto childerr;
215 2fafa2d2 2021-02-01 op }
216 881a9dd9 2021-01-16 op
217 bc99d868 2021-03-19 op do_exec(ex, req->spath, iri->query);
218 881a9dd9 2021-01-16 op goto childerr;
219 881a9dd9 2021-01-16 op }
220 881a9dd9 2021-01-16 op
221 881a9dd9 2021-01-16 op default:
222 881a9dd9 2021-01-16 op close(p[1]);
223 52053e1a 2021-02-06 op mark_nonblock(p[0]);
224 881a9dd9 2021-01-16 op return p[0];
225 881a9dd9 2021-01-16 op }
226 881a9dd9 2021-01-16 op
227 881a9dd9 2021-01-16 op childerr:
228 881a9dd9 2021-01-16 op dprintf(p[1], "%d internal server error\r\n", TEMP_FAILURE);
229 881a9dd9 2021-01-16 op _exit(1);
230 881a9dd9 2021-01-16 op }
231 881a9dd9 2021-01-16 op
232 b8e64ccd 2021-03-31 op static struct vhost *
233 b8e64ccd 2021-03-31 op host_nth(size_t n)
234 b8e64ccd 2021-03-31 op {
235 b8e64ccd 2021-03-31 op struct vhost *h;
236 b8e64ccd 2021-03-31 op
237 b8e64ccd 2021-03-31 op TAILQ_FOREACH(h, &hosts, vhosts) {
238 b8e64ccd 2021-03-31 op if (n == 0)
239 b8e64ccd 2021-03-31 op return h;
240 b8e64ccd 2021-03-31 op n--;
241 b8e64ccd 2021-03-31 op }
242 b8e64ccd 2021-03-31 op
243 b8e64ccd 2021-03-31 op return NULL;
244 b8e64ccd 2021-03-31 op }
245 b8e64ccd 2021-03-31 op
246 fdea6aa0 2021-04-30 op static struct location *
247 fdea6aa0 2021-04-30 op loc_nth(struct vhost *vhost, size_t n)
248 fdea6aa0 2021-04-30 op {
249 fdea6aa0 2021-04-30 op struct location *loc;
250 fdea6aa0 2021-04-30 op
251 fdea6aa0 2021-04-30 op TAILQ_FOREACH(loc, &vhost->locations, locations) {
252 fdea6aa0 2021-04-30 op if (n == 0)
253 fdea6aa0 2021-04-30 op return loc;
254 fdea6aa0 2021-04-30 op n--;
255 fdea6aa0 2021-04-30 op }
256 fdea6aa0 2021-04-30 op
257 fdea6aa0 2021-04-30 op return NULL;
258 fdea6aa0 2021-04-30 op }
259 fdea6aa0 2021-04-30 op
260 2c3e53da 2021-03-03 op static void
261 bc99d868 2021-03-19 op handle_imsg_cgi_req(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
262 881a9dd9 2021-01-16 op {
263 b8e64ccd 2021-03-31 op struct vhost *h;
264 fdea6aa0 2021-04-30 op struct location *l;
265 b8e64ccd 2021-03-31 op struct cgireq req;
266 b8e64ccd 2021-03-31 op struct iri iri;
267 b8e64ccd 2021-03-31 op int fd;
268 2c3e53da 2021-03-03 op
269 bc99d868 2021-03-19 op if (datalen != sizeof(req))
270 bc99d868 2021-03-19 op abort();
271 bc99d868 2021-03-19 op
272 bc99d868 2021-03-19 op memcpy(&req, imsg->data, datalen);
273 bc99d868 2021-03-19 op
274 bc99d868 2021-03-19 op iri.schema = req.iri_schema_off + req.buf;
275 bc99d868 2021-03-19 op iri.host = req.iri_host_off + req.buf;
276 bc99d868 2021-03-19 op iri.port = req.iri_port_off + req.buf;
277 bc99d868 2021-03-19 op iri.path = req.iri_path_off + req.buf;
278 bc99d868 2021-03-19 op iri.query = req.iri_query_off + req.buf;
279 bc99d868 2021-03-19 op iri.fragment = req.iri_fragment_off + req.buf;
280 bc99d868 2021-03-19 op
281 bc99d868 2021-03-19 op /* patch the query, otherwise do_exec will always pass "" as
282 bc99d868 2021-03-19 op * first argument to the script. */
283 bc99d868 2021-03-19 op if (*iri.query == '\0')
284 bc99d868 2021-03-19 op iri.query = NULL;
285 bc99d868 2021-03-19 op
286 b8e64ccd 2021-03-31 op if ((h = host_nth(req.host_off)) == NULL)
287 bc99d868 2021-03-19 op abort();
288 bc99d868 2021-03-19 op
289 fdea6aa0 2021-04-30 op if ((l = loc_nth(h, req.host_off)) == NULL)
290 fdea6aa0 2021-04-30 op abort();
291 fdea6aa0 2021-04-30 op
292 fdea6aa0 2021-04-30 op fd = launch_cgi(&iri, &req, h, l);
293 bc99d868 2021-03-19 op imsg_compose(ibuf, IMSG_CGI_RES, imsg->hdr.peerid, 0, fd, NULL, 0);
294 bc99d868 2021-03-19 op imsg_flush(ibuf);
295 bc99d868 2021-03-19 op }
296 bc99d868 2021-03-19 op
297 bc99d868 2021-03-19 op static void
298 bc99d868 2021-03-19 op handle_imsg_quit(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
299 bc99d868 2021-03-19 op {
300 bc99d868 2021-03-19 op int i;
301 bc99d868 2021-03-19 op
302 bc99d868 2021-03-19 op (void)ibuf;
303 bc99d868 2021-03-19 op (void)imsg;
304 bc99d868 2021-03-19 op (void)datalen;
305 bc99d868 2021-03-19 op
306 bc99d868 2021-03-19 op for (i = 0; i < conf.prefork; ++i) {
307 bc99d868 2021-03-19 op imsg_compose(&servibuf[i], IMSG_QUIT, 0, 0, -1, NULL, 0);
308 bc99d868 2021-03-19 op imsg_flush(&exibuf);
309 bc99d868 2021-03-19 op close(servibuf[i].fd);
310 1fbac5ba 2021-03-03 op }
311 2c3e53da 2021-03-03 op
312 bc99d868 2021-03-19 op event_loopbreak();
313 bc99d868 2021-03-19 op }
314 2c3e53da 2021-03-03 op
315 bc99d868 2021-03-19 op static void
316 bc99d868 2021-03-19 op handle_dispatch_imsg(int fd, short ev, void *d)
317 bc99d868 2021-03-19 op {
318 bc99d868 2021-03-19 op struct imsgbuf *ibuf = d;
319 bc99d868 2021-03-19 op dispatch_imsg(ibuf, handlers, sizeof(handlers));
320 2c3e53da 2021-03-03 op }
321 2c3e53da 2021-03-03 op
322 2c3e53da 2021-03-03 op int
323 bc99d868 2021-03-19 op executor_main(struct imsgbuf *ibuf)
324 2c3e53da 2021-03-03 op {
325 bc99d868 2021-03-19 op struct event evs[PROC_MAX], imsgev;
326 2c3e53da 2021-03-03 op int i;
327 10782292 2021-01-25 op
328 2c3e53da 2021-03-03 op event_init();
329 881a9dd9 2021-01-16 op
330 bc99d868 2021-03-19 op if (ibuf != NULL) {
331 bc99d868 2021-03-19 op event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST,
332 bc99d868 2021-03-19 op handle_dispatch_imsg, ibuf);
333 bc99d868 2021-03-19 op event_add(&imsgev, NULL);
334 bc99d868 2021-03-19 op }
335 bc99d868 2021-03-19 op
336 2c3e53da 2021-03-03 op for (i = 0; i < conf.prefork; ++i) {
337 bc99d868 2021-03-19 op event_set(&evs[i], servibuf[i].fd, EV_READ | EV_PERSIST,
338 bc99d868 2021-03-19 op handle_dispatch_imsg, &servibuf[i]);
339 2c3e53da 2021-03-03 op event_add(&evs[i], NULL);
340 881a9dd9 2021-01-16 op }
341 881a9dd9 2021-01-16 op
342 62e001b0 2021-03-20 op sandbox_executor_process();
343 62e001b0 2021-03-20 op
344 2c3e53da 2021-03-03 op event_dispatch();
345 ca21e100 2021-02-04 op
346 881a9dd9 2021-01-16 op return 1;
347 881a9dd9 2021-01-16 op }