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.
30 send_string(int fd, const char *str)
39 if (write(fd, &len, sizeof(len)) != sizeof(len))
43 if (write(fd, str, len) != len)
50 recv_string(int fd, char **ret)
54 if (read(fd, &len, sizeof(len)) != sizeof(len))
62 if ((*ret = calloc(1, len+1)) == NULL)
65 if (read(fd, *ret, len) != len)
71 send_iri(int fd, struct iri *i)
73 return send_string(fd, i->schema)
74 && send_string(fd, i->host)
75 && send_string(fd, i->port)
76 && send_string(fd, i->path)
77 && send_string(fd, i->query);
81 recv_iri(int fd, struct iri *i)
83 memset(i, 0, sizeof(*i));
85 if (!recv_string(fd, &i->schema)
86 || !recv_string(fd, &i->host)
87 || !recv_string(fd, &i->port)
88 || !recv_string(fd, &i->path)
89 || !recv_string(fd, &i->query))
96 free_recvd_iri(struct iri *i)
106 send_vhost(int fd, struct vhost *vhost)
110 if (vhost < hosts || vhost > hosts + HOSTSLEN)
114 return write(fd, &n, sizeof(n)) == sizeof(n);
118 recv_vhost(int fd, struct vhost **vhost)
122 if (read(fd, &n, sizeof(n)) != sizeof(n))
125 if (n < 0 || n > HOSTSLEN)
129 if ((*vhost)->domain == NULL)
135 send_time(int fd, time_t t)
137 return write(fd, &t, sizeof(t)) == sizeof(t);
141 recv_time(int fd, time_t *t)
143 return read(fd, t, sizeof(*t)) == sizeof(*t);
146 /* send d though fd. see /usr/src/usr.sbin/syslogd/privsep_fdpass.c
149 send_fd(int fd, int d)
154 unsigned char buf[CMSG_SPACE(sizeof(int))];
156 struct cmsghdr *cmsg;
161 memset(&msg, 0, sizeof(msg));
164 msg.msg_control = &cmsgbuf.buf;
165 msg.msg_controllen = sizeof(cmsgbuf.buf);
166 cmsg = CMSG_FIRSTHDR(&msg);
167 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
168 cmsg->cmsg_level = SOL_SOCKET;
169 cmsg->cmsg_type = SCM_RIGHTS;
170 *(int*)CMSG_DATA(cmsg) = d;
174 vec.iov_base = &result;
175 vec.iov_len = sizeof(int);
179 if ((n = sendmsg(fd, &msg, 0)) == -1 || n != sizeof(int)) {
180 log_err(NULL, "sendmsg: got %zu but wanted %zu: (errno) %s",
181 n, sizeof(int), strerror(errno));
187 /* receive a descriptor via fd */
194 char buf[CMSG_SPACE(sizeof(int))];
196 struct cmsghdr *cmsg;
201 memset(&msg, 0, sizeof(msg));
202 vec.iov_base = &result;
203 vec.iov_len = sizeof(int);
206 msg.msg_control = &cmsgbuf.buf;
207 msg.msg_controllen = sizeof(cmsgbuf.buf);
209 if ((n = recvmsg(fd, &msg, 0)) != sizeof(int)) {
210 log_err(NULL, "read %zu bytes bu wanted %zu\n", n, sizeof(int));
215 cmsg = CMSG_FIRSTHDR(&msg);
216 if (cmsg == NULL || cmsg->cmsg_type != SCM_RIGHTS)
218 return (*(int *)CMSG_DATA(cmsg));
224 safe_setenv(const char *name, const char *val)
228 setenv(name, val, 1);
232 xasprintf(const char *fmt, ...)
238 if (vasprintf(&s, fmt, ap) == -1)
246 do_exec(const char *ex, const char *spath, char *query)
248 char **argv, buf[PATH_MAX], *sname, *t;
251 strlcpy(buf, spath, sizeof(buf));
252 sname = basename(buf);
254 if (query == NULL || strchr(query, '=') != NULL) {
255 if ((argv = calloc(2, sizeof(char*))) == NULL)
259 warn("execvp: %s", argv[0]);
264 for (t = query ;; t++, n++) {
265 if ((t = strchr(t, '+')) == NULL)
269 if ((argv = calloc(n+2, sizeof(char*))) == NULL)
273 for (i = 0; i < n; ++i) {
274 t = strchr(query, '+');
277 argv[i+1] = pct_decode_str(query);
282 warn("execvp: %s", argv[0]);
286 setenv_time(const char *var, time_t t)
294 strftime(timebuf, sizeof(timebuf), "%FT%TZ",
295 gmtime_r(&t, &tminfo));
296 setenv(var, timebuf, 1);
299 /* fd or -1 on error */
301 launch_cgi(struct iri *iri, const char *spath, char *relpath,
302 const char *addr, const char *ruser, const char *cissuer,
303 const char *chash, time_t notbefore, time_t notafter,
306 int p[2]; /* read end, write end */
315 case 0: { /* child */
317 char iribuf[GEMINI_URL_LEN];
321 if (dup2(p[1], 1) == -1)
324 ex = xasprintf("%s/%s", vhost->dir, spath);
326 serialize_iri(iri, iribuf, sizeof(iribuf));
328 safe_setenv("GATEWAY_INTERFACE", "CGI/1.1");
329 safe_setenv("GEMINI_DOCUMENT_ROOT", vhost->dir);
330 safe_setenv("GEMINI_SCRIPT_FILENAME",
331 xasprintf("%s/%s", vhost->dir, spath));
332 safe_setenv("GEMINI_URL", iribuf);
334 strlcpy(path, "/", sizeof(path));
335 strlcat(path, spath, sizeof(path));
336 safe_setenv("GEMINI_URL_PATH", path);
338 if (relpath != NULL) {
339 strlcpy(path, "/", sizeof(path));
340 strlcat(path, relpath, sizeof(path));
341 safe_setenv("PATH_INFO", path);
343 strlcpy(path, vhost->dir, sizeof(path));
344 strlcat(path, "/", sizeof(path));
345 strlcat(path, relpath, sizeof(path));
346 safe_setenv("PATH_TRANSLATED", path);
349 safe_setenv("QUERY_STRING", iri->query);
350 safe_setenv("REMOTE_ADDR", addr);
351 safe_setenv("REMOTE_HOST", addr);
352 safe_setenv("REQUEST_METHOD", "");
354 strlcpy(path, "/", sizeof(path));
355 strlcat(path, spath, sizeof(path));
356 safe_setenv("SCRIPT_NAME", path);
358 safe_setenv("SERVER_NAME", iri->host);
360 snprintf(path, sizeof(path), "%d", conf.port);
361 safe_setenv("SERVER_PORT", path);
363 safe_setenv("SERVER_PROTOCOL", "GEMINI");
364 safe_setenv("SERVER_SOFTWARE", "gmid/1.5");
367 safe_setenv("AUTH_TYPE", "Certificate");
369 safe_setenv("AUTH_TYPE", "");
371 safe_setenv("REMOTE_USER", ruser);
372 safe_setenv("TLS_CLIENT_ISSUER", cissuer);
373 safe_setenv("TLS_CLIENT_HASH", chash);
374 setenv_time("TLS_CLIENT_NOT_AFTER", notafter);
375 setenv_time("TLS_CLIENT_NOT_BEFORE", notbefore);
377 strlcpy(path, ex, sizeof(path));
385 do_exec(ex, spath, iri->query);
396 dprintf(p[1], "%d internal server error\r\n", TEMP_FAILURE);
403 char *spath, *relpath, *addr, *ruser, *cissuer, *chash;
406 time_t notbefore, notafter;
410 for (vhost = hosts; vhost->domain != NULL; ++vhost) {
411 /* r so we can chdir into the correct directory */
412 if (unveil(vhost->dir, "rx") == -1)
413 err(1, "unveil %s for domain %s",
414 vhost->dir, vhost->domain);
417 /* rpath to chdir into the correct directory */
418 if (pledge("stdio rpath sendfd proc exec", NULL))
423 if (!recv_iri(exfd, &iri)
424 || !recv_string(exfd, &spath)
425 || !recv_string(exfd, &relpath)
426 || !recv_string(exfd, &addr)
427 || !recv_string(exfd, &ruser)
428 || !recv_string(exfd, &cissuer)
429 || !recv_string(exfd, &chash)
430 || !recv_time(exfd, ¬before)
431 || !recv_time(exfd, ¬after)
432 || !recv_vhost(exfd, &vhost))
435 d = launch_cgi(&iri, spath, relpath, addr, ruser, cissuer, chash,
436 notbefore, notafter, vhost);
437 if (!send_fd(exfd, d))
441 free_recvd_iri(&iri);
453 /* kill all process in my group. This means the listener and
454 * every pending CGI script. */