Blob


1 /*
2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 *
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.
7 *
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.
15 */
17 #include "gmid.h"
19 #include <err.h>
20 #include <errno.h>
22 #include <fcntl.h>
23 #include <libgen.h>
24 #include <limits.h>
25 #include <signal.h>
26 #include <stdarg.h>
27 #include <string.h>
29 int
30 send_string(int fd, const char *str)
31 {
32 ssize_t len;
34 if (str == NULL)
35 len = 0;
36 else
37 len = strlen(str);
39 if (write(fd, &len, sizeof(len)) != sizeof(len))
40 return 0;
42 if (len != 0)
43 if (write(fd, str, len) != len)
44 return 0;
46 return 1;
47 }
49 int
50 recv_string(int fd, char **ret)
51 {
52 ssize_t len;
54 if (read(fd, &len, sizeof(len)) != sizeof(len))
55 return 0;
57 if (len == 0) {
58 *ret = NULL;
59 return 1;
60 }
62 if ((*ret = calloc(1, len+1)) == NULL)
63 return 0;
65 if (read(fd, *ret, len) != len)
66 return 0;
67 return 1;
68 }
70 int
71 send_iri(int fd, struct iri *i)
72 {
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);
78 }
80 int
81 recv_iri(int fd, struct iri *i)
82 {
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))
90 return 0;
92 return 1;
93 }
95 void
96 free_recvd_iri(struct iri *i)
97 {
98 free(i->schema);
99 free(i->host);
100 free(i->port);
101 free(i->path);
102 free(i->query);
105 int
106 send_vhost(int fd, struct vhost *vhost)
108 ssize_t n;
110 if (vhost < hosts || vhost > hosts + HOSTSLEN)
111 return 0;
113 n = vhost - hosts;
114 return write(fd, &n, sizeof(n)) == sizeof(n);
117 int
118 recv_vhost(int fd, struct vhost **vhost)
120 ssize_t n;
122 if (read(fd, &n, sizeof(n)) != sizeof(n))
123 return 0;
125 if (n < 0 || n > HOSTSLEN)
126 return 0;
128 *vhost = &hosts[n];
129 if ((*vhost)->domain == NULL)
130 return 0;
131 return 1;
134 int
135 send_time(int fd, time_t t)
137 return write(fd, &t, sizeof(t)) == sizeof(t);
140 int
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
147 * for an example */
148 int
149 send_fd(int fd, int d)
151 struct msghdr msg;
152 union {
153 struct cmsghdr hdr;
154 unsigned char buf[CMSG_SPACE(sizeof(int))];
155 } cmsgbuf;
156 struct cmsghdr *cmsg;
157 struct iovec vec;
158 int result = 1;
159 ssize_t n;
161 memset(&msg, 0, sizeof(msg));
163 if (d >= 0) {
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;
171 } else
172 result = 0;
174 vec.iov_base = &result;
175 vec.iov_len = sizeof(int);
176 msg.msg_iov = &vec;
177 msg.msg_iovlen = 1;
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));
182 return 0;
184 return 1;
187 /* receive a descriptor via fd */
188 int
189 recv_fd(int fd)
191 struct msghdr msg;
192 union {
193 struct cmsghdr hdr;
194 char buf[CMSG_SPACE(sizeof(int))];
195 } cmsgbuf;
196 struct cmsghdr *cmsg;
197 struct iovec vec;
198 ssize_t n;
199 int result;
201 memset(&msg, 0, sizeof(msg));
202 vec.iov_base = &result;
203 vec.iov_len = sizeof(int);
204 msg.msg_iov = &vec;
205 msg.msg_iovlen = 1;
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));
211 return -1;
214 if (result) {
215 cmsg = CMSG_FIRSTHDR(&msg);
216 if (cmsg == NULL || cmsg->cmsg_type != SCM_RIGHTS)
217 return -1;
218 return (*(int *)CMSG_DATA(cmsg));
219 } else
220 return -1;
223 static inline void
224 safe_setenv(const char *name, const char *val)
226 if (val == NULL)
227 val = "";
228 setenv(name, val, 1);
231 static char *
232 xasprintf(const char *fmt, ...)
234 va_list ap;
235 char *s;
237 va_start(ap, fmt);
238 if (vasprintf(&s, fmt, ap) == -1)
239 s = NULL;
240 va_end(ap);
242 return s;
245 static void
246 do_exec(const char *ex, const char *spath, char *query)
248 char **argv, buf[PATH_MAX], *sname, *t;
249 size_t i, n;
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)
256 err(1, "calloc");
257 argv[0] = sname;
258 execvp(ex, argv);
259 warn("execvp: %s", argv[0]);
260 return;
263 n = 1;
264 for (t = query ;; t++, n++) {
265 if ((t = strchr(t, '+')) == NULL)
266 break;
269 if ((argv = calloc(n+2, sizeof(char*))) == NULL)
270 err(1, "calloc");
272 argv[0] = sname;
273 for (i = 0; i < n; ++i) {
274 t = strchr(query, '+');
275 if (t != NULL)
276 *t = '\0';
277 argv[i+1] = pct_decode_str(query);
278 query = t+1;
281 execvp(ex, argv);
282 warn("execvp: %s", argv[0]);
285 static inline void
286 setenv_time(const char *var, time_t t)
288 char timebuf[21];
289 struct tm tminfo;
291 if (t == -1)
292 return;
294 strftime(timebuf, sizeof(timebuf), "%FT%TZ",
295 gmtime_r(&t, &tminfo));
296 setenv(var, timebuf, 1);
299 /* fd or -1 on error */
300 static int
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,
304 struct vhost *vhost)
306 int p[2]; /* read end, write end */
308 if (pipe(p) == -1)
309 return -1;
311 switch (fork()) {
312 case -1:
313 return -1;
315 case 0: { /* child */
316 char *ex, *pwd;
317 char iribuf[GEMINI_URL_LEN];
318 char path[PATH_MAX];
320 close(p[0]);
321 if (dup2(p[1], 1) == -1)
322 goto childerr;
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");
366 if (ruser != NULL)
367 safe_setenv("AUTH_TYPE", "Certificate");
368 else
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));
379 pwd = dirname(path);
380 if (chdir(pwd)) {
381 warn("chdir");
382 goto childerr;
385 do_exec(ex, spath, iri->query);
386 goto childerr;
389 default:
390 close(p[1]);
391 mark_nonblock(p[0]);
392 return p[0];
395 childerr:
396 dprintf(p[1], "%d internal server error\r\n", TEMP_FAILURE);
397 _exit(1);
400 int
401 executor_main()
403 char *spath, *relpath, *addr, *ruser, *cissuer, *chash;
404 struct vhost *vhost;
405 struct iri iri;
406 time_t notbefore, notafter;
407 int d;
409 #ifdef __OpenBSD__
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))
419 err(1, "pledge");
420 #endif
422 while (!hupped) {
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, &notbefore)
431 || !recv_time(exfd, &notafter)
432 || !recv_vhost(exfd, &vhost))
433 break;
435 d = launch_cgi(&iri, spath, relpath, addr, ruser, cissuer, chash,
436 notbefore, notafter, vhost);
437 if (!send_fd(exfd, d))
438 break;
439 close(d);
441 free_recvd_iri(&iri);
442 free(spath);
443 free(relpath);
444 free(addr);
445 free(ruser);
446 free(cissuer);
447 free(chash);
450 if (hupped)
451 _exit(0);
453 /* kill all process in my group. This means the listener and
454 * every pending CGI script. */
455 kill(0, SIGINT);
456 return 1;