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 881a9dd9 2021-01-16 op fprintf(stderr, "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 881a9dd9 2021-01-16 op fprintf(stderr, "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)
233 881a9dd9 2021-01-16 op /* fd or -1 on error */
235 2fafa2d2 2021-02-01 op launch_cgi(struct iri *iri, const char *spath, char *relpath,
236 2fafa2d2 2021-02-01 op const char *addr, const char *ruser, const char *cissuer,
237 2fafa2d2 2021-02-01 op const char *chash, struct vhost *vhost)
239 881a9dd9 2021-01-16 op int p[2]; /* read end, write end */
241 881a9dd9 2021-01-16 op if (pipe2(p, O_NONBLOCK) == -1)
244 881a9dd9 2021-01-16 op switch (fork()) {
248 881a9dd9 2021-01-16 op case 0: { /* child */
249 67528c1f 2021-02-01 op char *argv[] = {NULL, NULL};
250 2fafa2d2 2021-02-01 op char *ex, *pwd;
251 2fafa2d2 2021-02-01 op char iribuf[GEMINI_URL_LEN];
252 2fafa2d2 2021-02-01 op char path[PATH_MAX];
255 881a9dd9 2021-01-16 op if (dup2(p[1], 1) == -1)
256 881a9dd9 2021-01-16 op goto childerr;
258 2fafa2d2 2021-02-01 op ex = xasprintf("%s/%s", vhost->dir, spath);
259 2fafa2d2 2021-02-01 op argv[0] = ex;
261 2fafa2d2 2021-02-01 op serialize_iri(iri, iribuf, sizeof(iribuf));
263 881a9dd9 2021-01-16 op safe_setenv("GATEWAY_INTERFACE", "CGI/1.1");
264 2fafa2d2 2021-02-01 op safe_setenv("GEMINI_DOCUMENT_ROOT", vhost->dir);
265 2fafa2d2 2021-02-01 op safe_setenv("GEMINI_SCRIPT_FILENAME",
266 2fafa2d2 2021-02-01 op xasprintf("%s/%s", vhost->dir, spath));
267 2fafa2d2 2021-02-01 op safe_setenv("GEMINI_URL", iribuf);
269 2fafa2d2 2021-02-01 op strlcpy(path, "/", sizeof(path));
270 2fafa2d2 2021-02-01 op strlcat(path, spath, sizeof(path));
271 2fafa2d2 2021-02-01 op safe_setenv("GEMINI_URL_PATH", path);
273 2fafa2d2 2021-02-01 op if (relpath != NULL) {
274 2fafa2d2 2021-02-01 op strlcpy(path, "/", sizeof(path));
275 2fafa2d2 2021-02-01 op strlcat(path, relpath, sizeof(path));
276 2fafa2d2 2021-02-01 op safe_setenv("PATH_INFO", path);
278 2fafa2d2 2021-02-01 op strlcpy(path, vhost->dir, sizeof(path));
279 2fafa2d2 2021-02-01 op strlcat(path, "/", sizeof(path));
280 2fafa2d2 2021-02-01 op strlcat(path, relpath, sizeof(path));
281 2fafa2d2 2021-02-01 op safe_setenv("PATH_TRANSLATED", path);
284 2fafa2d2 2021-02-01 op safe_setenv("QUERY_STRING", iri->query);
285 881a9dd9 2021-01-16 op safe_setenv("REMOTE_ADDR", addr);
286 2fafa2d2 2021-02-01 op safe_setenv("REMOTE_HOST", addr);
287 2fafa2d2 2021-02-01 op safe_setenv("REQUEST_METHOD", "");
289 2fafa2d2 2021-02-01 op strlcpy(path, "/", sizeof(path));
290 2fafa2d2 2021-02-01 op strlcat(path, spath, sizeof(path));
291 2fafa2d2 2021-02-01 op safe_setenv("SCRIPT_NAME", path);
293 2fafa2d2 2021-02-01 op safe_setenv("SERVER_NAME", iri->host);
295 2fafa2d2 2021-02-01 op snprintf(path, sizeof(path), "%d", conf.port);
296 2fafa2d2 2021-02-01 op safe_setenv("SERVER_PORT", path);
298 2fafa2d2 2021-02-01 op safe_setenv("SERVER_PROTOCOL", "GEMINI");
299 2fafa2d2 2021-02-01 op safe_setenv("SERVER_SOFTWARE", "gmid/1.5");
301 3e541809 2021-02-01 op if (ruser != NULL)
302 881a9dd9 2021-01-16 op safe_setenv("AUTH_TYPE", "Certificate");
304 3e541809 2021-02-01 op safe_setenv("AUTH_TYPE", "");
306 3e541809 2021-02-01 op safe_setenv("REMOTE_USER", ruser);
307 3e541809 2021-02-01 op safe_setenv("TLS_CLIENT_ISSUER", cissuer);
308 3e541809 2021-02-01 op safe_setenv("TLS_CLIENT_HASH", chash);
310 2fafa2d2 2021-02-01 op strlcpy(path, argv[0], sizeof(path));
311 2fafa2d2 2021-02-01 op pwd = dirname(path);
312 2fafa2d2 2021-02-01 op if (chdir(pwd)) {
313 2fafa2d2 2021-02-01 op warn("chdir");
314 2fafa2d2 2021-02-01 op goto childerr;
317 2fafa2d2 2021-02-01 op execvp(argv[0], argv);
318 2fafa2d2 2021-02-01 op warn("execvp: %s", argv[0]);
319 881a9dd9 2021-01-16 op goto childerr;
328 881a9dd9 2021-01-16 op dprintf(p[1], "%d internal server error\r\n", TEMP_FAILURE);
333 4e2e2ab1 2021-02-03 op executor_main()
335 2fafa2d2 2021-02-01 op char *spath, *relpath, *addr, *ruser, *cissuer, *chash;
336 881a9dd9 2021-01-16 op struct vhost *vhost;
337 2fafa2d2 2021-02-01 op struct iri iri;
340 881a9dd9 2021-01-16 op #ifdef __OpenBSD__
341 10782292 2021-01-25 op for (vhost = hosts; vhost->domain != NULL; ++vhost) {
342 2fafa2d2 2021-02-01 op /* r so we can chdir into the correct directory */
343 2fafa2d2 2021-02-01 op if (unveil(vhost->dir, "rx") == -1)
344 10782292 2021-01-25 op err(1, "unveil %s for domain %s",
345 10782292 2021-01-25 op vhost->dir, vhost->domain);
348 2fafa2d2 2021-02-01 op /* rpath to chdir into the correct directory */
349 2fafa2d2 2021-02-01 op if (pledge("stdio rpath sendfd proc exec", NULL))
350 10782292 2021-01-25 op err(1, "pledge");
354 4e2e2ab1 2021-02-03 op if (!recv_iri(exfd, &iri)
355 4e2e2ab1 2021-02-03 op || !recv_string(exfd, &spath)
356 4e2e2ab1 2021-02-03 op || !recv_string(exfd, &relpath)
357 4e2e2ab1 2021-02-03 op || !recv_string(exfd, &addr)
358 4e2e2ab1 2021-02-03 op || !recv_string(exfd, &ruser)
359 4e2e2ab1 2021-02-03 op || !recv_string(exfd, &cissuer)
360 4e2e2ab1 2021-02-03 op || !recv_string(exfd, &chash)
361 4e2e2ab1 2021-02-03 op || !recv_vhost(exfd, &vhost))
364 2fafa2d2 2021-02-01 op d = launch_cgi(&iri, spath, relpath, addr, ruser, cissuer, chash,
366 4e2e2ab1 2021-02-03 op if (!send_fd(exfd, d))
370 2fafa2d2 2021-02-01 op free_recvd_iri(&iri);
372 881a9dd9 2021-01-16 op free(relpath);
375 881a9dd9 2021-01-16 op free(cissuer);
379 881a9dd9 2021-01-16 op /* kill all process in my group. This means the listener and
380 881a9dd9 2021-01-16 op * every pending CGI script. */
381 881a9dd9 2021-01-16 op kill(0, SIGINT);