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.
31 send_string(int fd, const char *str)
40 if (write(fd, &len, sizeof(len)) != sizeof(len))
44 if (write(fd, str, len) != len)
51 recv_string(int fd, char **ret)
55 if (read(fd, &len, sizeof(len)) != sizeof(len))
63 if ((*ret = calloc(1, len+1)) == NULL)
66 if (read(fd, *ret, len) != len)
72 send_iri(int fd, struct iri *i)
74 return send_string(fd, i->schema)
75 && send_string(fd, i->host)
76 && send_string(fd, i->port)
77 && send_string(fd, i->path)
78 && send_string(fd, i->query);
82 recv_iri(int fd, struct iri *i)
84 memset(i, 0, sizeof(*i));
86 if (!recv_string(fd, &i->schema)
87 || !recv_string(fd, &i->host)
88 || !recv_string(fd, &i->port)
89 || !recv_string(fd, &i->path)
90 || !recv_string(fd, &i->query))
97 free_recvd_iri(struct iri *i)
107 send_vhost(int fd, struct vhost *vhost)
111 if (vhost < hosts || vhost > hosts + HOSTSLEN)
115 return write(fd, &n, sizeof(n)) == sizeof(n);
119 recv_vhost(int fd, struct vhost **vhost)
123 if (read(fd, &n, sizeof(n)) != sizeof(n))
126 if (n < 0 || n > HOSTSLEN)
130 if ((*vhost)->domain == NULL)
136 send_time(int fd, time_t t)
138 return write(fd, &t, sizeof(t)) == sizeof(t);
142 recv_time(int fd, time_t *t)
144 return read(fd, t, sizeof(*t)) == sizeof(*t);
147 /* send d though fd. see /usr/src/usr.sbin/syslogd/privsep_fdpass.c
150 send_fd(int fd, int d)
155 unsigned char buf[CMSG_SPACE(sizeof(int))];
157 struct cmsghdr *cmsg;
162 memset(&msg, 0, sizeof(msg));
165 msg.msg_control = &cmsgbuf.buf;
166 msg.msg_controllen = sizeof(cmsgbuf.buf);
167 cmsg = CMSG_FIRSTHDR(&msg);
168 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
169 cmsg->cmsg_level = SOL_SOCKET;
170 cmsg->cmsg_type = SCM_RIGHTS;
171 *(int*)CMSG_DATA(cmsg) = d;
175 vec.iov_base = &result;
176 vec.iov_len = sizeof(int);
180 if ((n = sendmsg(fd, &msg, 0)) == -1 || n != sizeof(int)) {
181 log_err(NULL, "sendmsg: got %zu but wanted %zu: (errno) %s",
182 n, sizeof(int), strerror(errno));
188 /* receive a descriptor via fd */
195 char buf[CMSG_SPACE(sizeof(int))];
197 struct cmsghdr *cmsg;
202 memset(&msg, 0, sizeof(msg));
203 vec.iov_base = &result;
204 vec.iov_len = sizeof(int);
207 msg.msg_control = &cmsgbuf.buf;
208 msg.msg_controllen = sizeof(cmsgbuf.buf);
210 if ((n = recvmsg(fd, &msg, 0)) != sizeof(int)) {
211 log_err(NULL, "read %zu bytes bu wanted %zu\n", n, sizeof(int));
216 cmsg = CMSG_FIRSTHDR(&msg);
217 if (cmsg == NULL || cmsg->cmsg_type != SCM_RIGHTS)
219 return (*(int *)CMSG_DATA(cmsg));
225 safe_setenv(const char *name, const char *val)
229 setenv(name, val, 1);
233 xasprintf(const char *fmt, ...)
239 if (vasprintf(&s, fmt, ap) == -1)
247 do_exec(const char *ex, const char *spath, char *query)
249 char **argv, buf[PATH_MAX], *sname, *t;
252 strlcpy(buf, spath, sizeof(buf));
253 sname = basename(buf);
255 if (query == NULL || strchr(query, '=') != NULL) {
256 if ((argv = calloc(2, sizeof(char*))) == NULL)
260 warn("execvp: %s", argv[0]);
265 for (t = query ;; t++, n++) {
266 if ((t = strchr(t, '+')) == NULL)
270 if ((argv = calloc(n+2, sizeof(char*))) == NULL)
274 for (i = 0; i < n; ++i) {
275 t = strchr(query, '+');
278 argv[i+1] = pct_decode_str(query);
283 warn("execvp: %s", argv[0]);
287 setenv_time(const char *var, time_t t)
295 strftime(timebuf, sizeof(timebuf), "%FT%TZ",
296 gmtime_r(&t, &tminfo));
297 setenv(var, timebuf, 1);
300 /* fd or -1 on error */
302 launch_cgi(struct iri *iri, const char *spath, char *relpath,
303 const char *addr, const char *ruser, const char *cissuer,
304 const char *chash, time_t notbefore, time_t notafter,
307 int p[2]; /* read end, write end */
316 case 0: { /* child */
318 char iribuf[GEMINI_URL_LEN];
322 if (dup2(p[1], 1) == -1)
325 ex = xasprintf("%s/%s", vhost->dir, spath);
327 serialize_iri(iri, iribuf, sizeof(iribuf));
329 safe_setenv("GATEWAY_INTERFACE", "CGI/1.1");
330 safe_setenv("GEMINI_DOCUMENT_ROOT", vhost->dir);
331 safe_setenv("GEMINI_SCRIPT_FILENAME",
332 xasprintf("%s/%s", vhost->dir, spath));
333 safe_setenv("GEMINI_URL", iribuf);
335 strlcpy(path, "/", sizeof(path));
336 strlcat(path, spath, sizeof(path));
337 safe_setenv("GEMINI_URL_PATH", path);
339 if (relpath != NULL) {
340 strlcpy(path, "/", sizeof(path));
341 strlcat(path, relpath, sizeof(path));
342 safe_setenv("PATH_INFO", path);
344 strlcpy(path, vhost->dir, sizeof(path));
345 strlcat(path, "/", sizeof(path));
346 strlcat(path, relpath, sizeof(path));
347 safe_setenv("PATH_TRANSLATED", path);
350 safe_setenv("QUERY_STRING", iri->query);
351 safe_setenv("REMOTE_ADDR", addr);
352 safe_setenv("REMOTE_HOST", addr);
353 safe_setenv("REQUEST_METHOD", "");
355 strlcpy(path, "/", sizeof(path));
356 strlcat(path, spath, sizeof(path));
357 safe_setenv("SCRIPT_NAME", path);
359 safe_setenv("SERVER_NAME", iri->host);
361 snprintf(path, sizeof(path), "%d", conf.port);
362 safe_setenv("SERVER_PORT", path);
364 safe_setenv("SERVER_PROTOCOL", "GEMINI");
365 safe_setenv("SERVER_SOFTWARE", "gmid/1.5");
368 safe_setenv("AUTH_TYPE", "Certificate");
370 safe_setenv("AUTH_TYPE", "");
372 safe_setenv("REMOTE_USER", ruser);
373 safe_setenv("TLS_CLIENT_ISSUER", cissuer);
374 safe_setenv("TLS_CLIENT_HASH", chash);
375 setenv_time("TLS_CLIENT_NOT_AFTER", notafter);
376 setenv_time("TLS_CLIENT_NOT_BEFORE", notbefore);
378 strlcpy(path, ex, sizeof(path));
386 do_exec(ex, spath, iri->query);
397 dprintf(p[1], "%d internal server error\r\n", TEMP_FAILURE);
402 handle_fork_req(int fd, short ev, void *data)
404 char *spath, *relpath, *addr, *ruser, *cissuer, *chash;
407 time_t notbefore, notafter;
410 if (!recv_iri(fd, &iri)
411 || !recv_string(fd, &spath)
412 || !recv_string(fd, &relpath)
413 || !recv_string(fd, &addr)
414 || !recv_string(fd, &ruser)
415 || !recv_string(fd, &cissuer)
416 || !recv_string(fd, &chash)
417 || !recv_time(fd, ¬before)
418 || !recv_time(fd, ¬after)
419 || !recv_vhost(fd, &vhost))
420 fatal("failure in handling fork request");
422 d = launch_cgi(&iri, spath, relpath, addr, ruser, cissuer, chash,
423 notbefore, notafter, vhost);
425 fatal("failure in sending the fd to the server: %s",
429 free_recvd_iri(&iri);
442 struct event evs[PROC_MAX];
446 for (vhost = hosts; vhost->domain != NULL; ++vhost) {
447 /* r so we can chdir into the correct directory */
448 if (unveil(vhost->dir, "rx") == -1)
449 err(1, "unveil %s for domain %s",
450 vhost->dir, vhost->domain);
453 /* rpath to chdir into the correct directory */
454 if (pledge("stdio rpath sendfd proc exec", NULL))
460 for (i = 0; i < conf.prefork; ++i) {
461 event_set(&evs[i], servpipes[i], EV_READ | EV_PERSIST,
462 handle_fork_req, NULL);
463 event_add(&evs[i], NULL);