2 881a9dd9 2021-01-16 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
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.
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.
17 881a9dd9 2021-01-16 op #include <err.h>
18 881a9dd9 2021-01-16 op #include <errno.h>
20 881a9dd9 2021-01-16 op #include <fcntl.h>
21 2fafa2d2 2021-02-01 op #include <libgen.h>
22 2fafa2d2 2021-02-01 op #include <limits.h>
23 881a9dd9 2021-01-16 op #include <signal.h>
24 2fafa2d2 2021-02-01 op #include <stdarg.h>
25 881a9dd9 2021-01-16 op #include <string.h>
27 881a9dd9 2021-01-16 op #include "gmid.h"
30 881a9dd9 2021-01-16 op send_string(int fd, const char *str)
34 881a9dd9 2021-01-16 op if (str == NULL)
37 881a9dd9 2021-01-16 op len = strlen(str);
39 881a9dd9 2021-01-16 op if (write(fd, &len, sizeof(len)) != sizeof(len))
43 881a9dd9 2021-01-16 op if (write(fd, str, len) != len)
50 881a9dd9 2021-01-16 op recv_string(int fd, char **ret)
54 881a9dd9 2021-01-16 op if (read(fd, &len, sizeof(len)) != sizeof(len))
57 881a9dd9 2021-01-16 op if (len == 0) {
62 881a9dd9 2021-01-16 op if ((*ret = calloc(1, len+1)) == NULL)
65 881a9dd9 2021-01-16 op if (read(fd, *ret, len) != len)
71 2fafa2d2 2021-02-01 op send_iri(int fd, struct iri *i)
73 2fafa2d2 2021-02-01 op return send_string(fd, i->schema)
74 2fafa2d2 2021-02-01 op && send_string(fd, i->host)
75 2fafa2d2 2021-02-01 op && send_string(fd, i->port)
76 2fafa2d2 2021-02-01 op && send_string(fd, i->path)
77 2fafa2d2 2021-02-01 op && send_string(fd, i->query);
81 2fafa2d2 2021-02-01 op recv_iri(int fd, struct iri *i)
83 2fafa2d2 2021-02-01 op memset(i, 0, sizeof(*i));
85 2fafa2d2 2021-02-01 op if (!recv_string(fd, &i->schema)
86 2fafa2d2 2021-02-01 op || !recv_string(fd, &i->host)
87 2fafa2d2 2021-02-01 op || !recv_string(fd, &i->port)
88 2fafa2d2 2021-02-01 op || !recv_string(fd, &i->path)
89 2fafa2d2 2021-02-01 op || !recv_string(fd, &i->query))
96 2fafa2d2 2021-02-01 op free_recvd_iri(struct iri *i)
98 2fafa2d2 2021-02-01 op free(i->schema);
99 2fafa2d2 2021-02-01 op free(i->host);
100 2fafa2d2 2021-02-01 op free(i->port);
101 2fafa2d2 2021-02-01 op free(i->path);
102 2fafa2d2 2021-02-01 op free(i->query);
106 881a9dd9 2021-01-16 op send_vhost(int fd, struct vhost *vhost)
110 881a9dd9 2021-01-16 op if (vhost < hosts || vhost > hosts + HOSTSLEN)
113 78089786 2021-02-01 op n = vhost - hosts;
114 881a9dd9 2021-01-16 op return write(fd, &n, sizeof(n)) == sizeof(n);
118 881a9dd9 2021-01-16 op recv_vhost(int fd, struct vhost **vhost)
122 881a9dd9 2021-01-16 op if (read(fd, &n, sizeof(n)) != sizeof(n))
125 881a9dd9 2021-01-16 op if (n < 0 || n > HOSTSLEN)
128 881a9dd9 2021-01-16 op *vhost = &hosts[n];
129 881a9dd9 2021-01-16 op if ((*vhost)->domain == NULL)
134 881a9dd9 2021-01-16 op /* send d though fd. see /usr/src/usr.sbin/syslogd/privsep_fdpass.c
135 881a9dd9 2021-01-16 op * for an example */
137 881a9dd9 2021-01-16 op send_fd(int fd, int d)
139 881a9dd9 2021-01-16 op struct msghdr msg;
141 881a9dd9 2021-01-16 op struct cmsghdr hdr;
142 881a9dd9 2021-01-16 op unsigned char buf[CMSG_SPACE(sizeof(int))];
144 881a9dd9 2021-01-16 op struct cmsghdr *cmsg;
145 881a9dd9 2021-01-16 op struct iovec vec;
146 881a9dd9 2021-01-16 op int result = 1;
149 881a9dd9 2021-01-16 op memset(&msg, 0, sizeof(msg));
151 881a9dd9 2021-01-16 op if (d >= 0) {
152 881a9dd9 2021-01-16 op msg.msg_control = &cmsgbuf.buf;
153 881a9dd9 2021-01-16 op msg.msg_controllen = sizeof(cmsgbuf.buf);
154 881a9dd9 2021-01-16 op cmsg = CMSG_FIRSTHDR(&msg);
155 881a9dd9 2021-01-16 op cmsg->cmsg_len = CMSG_LEN(sizeof(int));
156 881a9dd9 2021-01-16 op cmsg->cmsg_level = SOL_SOCKET;
157 881a9dd9 2021-01-16 op cmsg->cmsg_type = SCM_RIGHTS;
158 881a9dd9 2021-01-16 op *(int*)CMSG_DATA(cmsg) = d;
162 881a9dd9 2021-01-16 op vec.iov_base = &result;
163 881a9dd9 2021-01-16 op vec.iov_len = sizeof(int);
164 881a9dd9 2021-01-16 op msg.msg_iov = &vec;
165 881a9dd9 2021-01-16 op msg.msg_iovlen = 1;
167 881a9dd9 2021-01-16 op if ((n = sendmsg(fd, &msg, 0)) == -1 || n != sizeof(int)) {
168 a64959c9 2021-02-07 op log_err(NULL, "sendmsg: got %zu but wanted %zu: (errno) %s",
169 881a9dd9 2021-01-16 op n, sizeof(int), strerror(errno));
175 881a9dd9 2021-01-16 op /* receive a descriptor via fd */
177 881a9dd9 2021-01-16 op recv_fd(int fd)
179 881a9dd9 2021-01-16 op struct msghdr msg;
181 881a9dd9 2021-01-16 op struct cmsghdr hdr;
182 881a9dd9 2021-01-16 op char buf[CMSG_SPACE(sizeof(int))];
184 881a9dd9 2021-01-16 op struct cmsghdr *cmsg;
185 881a9dd9 2021-01-16 op struct iovec vec;
189 881a9dd9 2021-01-16 op memset(&msg, 0, sizeof(msg));
190 881a9dd9 2021-01-16 op vec.iov_base = &result;
191 881a9dd9 2021-01-16 op vec.iov_len = sizeof(int);
192 881a9dd9 2021-01-16 op msg.msg_iov = &vec;
193 881a9dd9 2021-01-16 op msg.msg_iovlen = 1;
194 881a9dd9 2021-01-16 op msg.msg_control = &cmsgbuf.buf;
195 881a9dd9 2021-01-16 op msg.msg_controllen = sizeof(cmsgbuf.buf);
197 881a9dd9 2021-01-16 op if ((n = recvmsg(fd, &msg, 0)) != sizeof(int)) {
198 a64959c9 2021-02-07 op log_err(NULL, "read %zu bytes bu wanted %zu\n", n, sizeof(int));
202 881a9dd9 2021-01-16 op if (result) {
203 881a9dd9 2021-01-16 op cmsg = CMSG_FIRSTHDR(&msg);
204 881a9dd9 2021-01-16 op if (cmsg == NULL || cmsg->cmsg_type != SCM_RIGHTS)
206 881a9dd9 2021-01-16 op return (*(int *)CMSG_DATA(cmsg));
211 881a9dd9 2021-01-16 op static inline void
212 881a9dd9 2021-01-16 op safe_setenv(const char *name, const char *val)
214 881a9dd9 2021-01-16 op if (val == NULL)
216 881a9dd9 2021-01-16 op setenv(name, val, 1);
219 2fafa2d2 2021-02-01 op static char *
220 2fafa2d2 2021-02-01 op xasprintf(const char *fmt, ...)
225 2fafa2d2 2021-02-01 op va_start(ap, fmt);
226 2fafa2d2 2021-02-01 op if (vasprintf(&s, fmt, ap) == -1)
234 9f006a21 2021-02-07 op do_exec(const char *ex, const char *spath, char *query)
236 9f006a21 2021-02-07 op char **argv, buf[PATH_MAX], *sname, *t;
239 9f006a21 2021-02-07 op strlcpy(buf, spath, sizeof(buf));
240 9f006a21 2021-02-07 op sname = basename(buf);
242 9f006a21 2021-02-07 op if (query == NULL || strchr(query, '=') != NULL) {
243 9f006a21 2021-02-07 op if ((argv = calloc(2, sizeof(char*))) == NULL)
244 9f006a21 2021-02-07 op err(1, "calloc");
245 9f006a21 2021-02-07 op argv[0] = sname;
246 9f006a21 2021-02-07 op execvp(ex, argv);
247 9f006a21 2021-02-07 op warn("execvp: %s", argv[0]);
252 9f006a21 2021-02-07 op for (t = query ;; t++, n++) {
253 9f006a21 2021-02-07 op if ((t = strchr(t, '+')) == NULL)
257 9f006a21 2021-02-07 op if ((argv = calloc(n+2, sizeof(char*))) == NULL)
258 9f006a21 2021-02-07 op err(1, "calloc");
260 9f006a21 2021-02-07 op argv[0] = sname;
261 9f006a21 2021-02-07 op for (i = 0; i < n; ++i) {
262 9f006a21 2021-02-07 op t = strchr(query, '+');
263 9f006a21 2021-02-07 op if (t != NULL)
265 9f006a21 2021-02-07 op argv[i+1] = pct_decode_str(query);
269 9f006a21 2021-02-07 op execvp(ex, argv);
270 9f006a21 2021-02-07 op warn("execvp: %s", argv[0]);
273 881a9dd9 2021-01-16 op /* fd or -1 on error */
275 2fafa2d2 2021-02-01 op launch_cgi(struct iri *iri, const char *spath, char *relpath,
276 2fafa2d2 2021-02-01 op const char *addr, const char *ruser, const char *cissuer,
277 2fafa2d2 2021-02-01 op const char *chash, struct vhost *vhost)
279 881a9dd9 2021-01-16 op int p[2]; /* read end, write end */
281 52053e1a 2021-02-06 op if (pipe(p) == -1)
284 881a9dd9 2021-01-16 op switch (fork()) {
288 881a9dd9 2021-01-16 op case 0: { /* child */
289 2fafa2d2 2021-02-01 op char *ex, *pwd;
290 2fafa2d2 2021-02-01 op char iribuf[GEMINI_URL_LEN];
291 2fafa2d2 2021-02-01 op char path[PATH_MAX];
294 881a9dd9 2021-01-16 op if (dup2(p[1], 1) == -1)
295 881a9dd9 2021-01-16 op goto childerr;
297 2fafa2d2 2021-02-01 op ex = xasprintf("%s/%s", vhost->dir, spath);
299 2fafa2d2 2021-02-01 op serialize_iri(iri, iribuf, sizeof(iribuf));
301 881a9dd9 2021-01-16 op safe_setenv("GATEWAY_INTERFACE", "CGI/1.1");
302 2fafa2d2 2021-02-01 op safe_setenv("GEMINI_DOCUMENT_ROOT", vhost->dir);
303 2fafa2d2 2021-02-01 op safe_setenv("GEMINI_SCRIPT_FILENAME",
304 2fafa2d2 2021-02-01 op xasprintf("%s/%s", vhost->dir, spath));
305 2fafa2d2 2021-02-01 op safe_setenv("GEMINI_URL", iribuf);
307 2fafa2d2 2021-02-01 op strlcpy(path, "/", sizeof(path));
308 2fafa2d2 2021-02-01 op strlcat(path, spath, sizeof(path));
309 2fafa2d2 2021-02-01 op safe_setenv("GEMINI_URL_PATH", path);
311 2fafa2d2 2021-02-01 op if (relpath != NULL) {
312 2fafa2d2 2021-02-01 op strlcpy(path, "/", sizeof(path));
313 2fafa2d2 2021-02-01 op strlcat(path, relpath, sizeof(path));
314 2fafa2d2 2021-02-01 op safe_setenv("PATH_INFO", path);
316 2fafa2d2 2021-02-01 op strlcpy(path, vhost->dir, sizeof(path));
317 2fafa2d2 2021-02-01 op strlcat(path, "/", sizeof(path));
318 2fafa2d2 2021-02-01 op strlcat(path, relpath, sizeof(path));
319 2fafa2d2 2021-02-01 op safe_setenv("PATH_TRANSLATED", path);
322 2fafa2d2 2021-02-01 op safe_setenv("QUERY_STRING", iri->query);
323 881a9dd9 2021-01-16 op safe_setenv("REMOTE_ADDR", addr);
324 2fafa2d2 2021-02-01 op safe_setenv("REMOTE_HOST", addr);
325 2fafa2d2 2021-02-01 op safe_setenv("REQUEST_METHOD", "");
327 2fafa2d2 2021-02-01 op strlcpy(path, "/", sizeof(path));
328 2fafa2d2 2021-02-01 op strlcat(path, spath, sizeof(path));
329 2fafa2d2 2021-02-01 op safe_setenv("SCRIPT_NAME", path);
331 2fafa2d2 2021-02-01 op safe_setenv("SERVER_NAME", iri->host);
333 2fafa2d2 2021-02-01 op snprintf(path, sizeof(path), "%d", conf.port);
334 2fafa2d2 2021-02-01 op safe_setenv("SERVER_PORT", path);
336 2fafa2d2 2021-02-01 op safe_setenv("SERVER_PROTOCOL", "GEMINI");
337 2fafa2d2 2021-02-01 op safe_setenv("SERVER_SOFTWARE", "gmid/1.5");
339 3e541809 2021-02-01 op if (ruser != NULL)
340 881a9dd9 2021-01-16 op safe_setenv("AUTH_TYPE", "Certificate");
342 3e541809 2021-02-01 op safe_setenv("AUTH_TYPE", "");
344 3e541809 2021-02-01 op safe_setenv("REMOTE_USER", ruser);
345 3e541809 2021-02-01 op safe_setenv("TLS_CLIENT_ISSUER", cissuer);
346 3e541809 2021-02-01 op safe_setenv("TLS_CLIENT_HASH", chash);
348 9f006a21 2021-02-07 op strlcpy(path, ex, sizeof(path));
350 2fafa2d2 2021-02-01 op pwd = dirname(path);
351 2fafa2d2 2021-02-01 op if (chdir(pwd)) {
352 2fafa2d2 2021-02-01 op warn("chdir");
353 2fafa2d2 2021-02-01 op goto childerr;
356 9f006a21 2021-02-07 op do_exec(ex, spath, iri->query);
357 881a9dd9 2021-01-16 op goto childerr;
362 52053e1a 2021-02-06 op mark_nonblock(p[0]);
367 881a9dd9 2021-01-16 op dprintf(p[1], "%d internal server error\r\n", TEMP_FAILURE);
372 4e2e2ab1 2021-02-03 op executor_main()
374 2fafa2d2 2021-02-01 op char *spath, *relpath, *addr, *ruser, *cissuer, *chash;
375 881a9dd9 2021-01-16 op struct vhost *vhost;
376 2fafa2d2 2021-02-01 op struct iri iri;
379 881a9dd9 2021-01-16 op #ifdef __OpenBSD__
380 10782292 2021-01-25 op for (vhost = hosts; vhost->domain != NULL; ++vhost) {
381 2fafa2d2 2021-02-01 op /* r so we can chdir into the correct directory */
382 2fafa2d2 2021-02-01 op if (unveil(vhost->dir, "rx") == -1)
383 10782292 2021-01-25 op err(1, "unveil %s for domain %s",
384 10782292 2021-01-25 op vhost->dir, vhost->domain);
387 2fafa2d2 2021-02-01 op /* rpath to chdir into the correct directory */
388 2fafa2d2 2021-02-01 op if (pledge("stdio rpath sendfd proc exec", NULL))
389 10782292 2021-01-25 op err(1, "pledge");
392 ca21e100 2021-02-04 op while (!hupped) {
393 4e2e2ab1 2021-02-03 op if (!recv_iri(exfd, &iri)
394 4e2e2ab1 2021-02-03 op || !recv_string(exfd, &spath)
395 4e2e2ab1 2021-02-03 op || !recv_string(exfd, &relpath)
396 4e2e2ab1 2021-02-03 op || !recv_string(exfd, &addr)
397 4e2e2ab1 2021-02-03 op || !recv_string(exfd, &ruser)
398 4e2e2ab1 2021-02-03 op || !recv_string(exfd, &cissuer)
399 4e2e2ab1 2021-02-03 op || !recv_string(exfd, &chash)
400 4e2e2ab1 2021-02-03 op || !recv_vhost(exfd, &vhost))
403 2fafa2d2 2021-02-01 op d = launch_cgi(&iri, spath, relpath, addr, ruser, cissuer, chash,
405 4e2e2ab1 2021-02-03 op if (!send_fd(exfd, d))
409 2fafa2d2 2021-02-01 op free_recvd_iri(&iri);
411 881a9dd9 2021-01-16 op free(relpath);
414 881a9dd9 2021-01-16 op free(cissuer);
421 881a9dd9 2021-01-16 op /* kill all process in my group. This means the listener and
422 881a9dd9 2021-01-16 op * every pending CGI script. */
423 881a9dd9 2021-01-16 op kill(0, SIGINT);