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 <err.h>
18 #include <errno.h>
20 #include <fcntl.h>
21 #include <libgen.h>
22 #include <limits.h>
23 #include <signal.h>
24 #include <stdarg.h>
25 #include <string.h>
27 #include "gmid.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 /* send d though fd. see /usr/src/usr.sbin/syslogd/privsep_fdpass.c
135 * for an example */
136 int
137 send_fd(int fd, int d)
139 struct msghdr msg;
140 union {
141 struct cmsghdr hdr;
142 unsigned char buf[CMSG_SPACE(sizeof(int))];
143 } cmsgbuf;
144 struct cmsghdr *cmsg;
145 struct iovec vec;
146 int result = 1;
147 ssize_t n;
149 memset(&msg, 0, sizeof(msg));
151 if (d >= 0) {
152 msg.msg_control = &cmsgbuf.buf;
153 msg.msg_controllen = sizeof(cmsgbuf.buf);
154 cmsg = CMSG_FIRSTHDR(&msg);
155 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
156 cmsg->cmsg_level = SOL_SOCKET;
157 cmsg->cmsg_type = SCM_RIGHTS;
158 *(int*)CMSG_DATA(cmsg) = d;
159 } else
160 result = 0;
162 vec.iov_base = &result;
163 vec.iov_len = sizeof(int);
164 msg.msg_iov = &vec;
165 msg.msg_iovlen = 1;
167 if ((n = sendmsg(fd, &msg, 0)) == -1 || n != sizeof(int)) {
168 fprintf(stderr, "sendmsg: got %zu but wanted %zu: (errno) %s",
169 n, sizeof(int), strerror(errno));
170 return 0;
172 return 1;
175 /* receive a descriptor via fd */
176 int
177 recv_fd(int fd)
179 struct msghdr msg;
180 union {
181 struct cmsghdr hdr;
182 char buf[CMSG_SPACE(sizeof(int))];
183 } cmsgbuf;
184 struct cmsghdr *cmsg;
185 struct iovec vec;
186 ssize_t n;
187 int result;
189 memset(&msg, 0, sizeof(msg));
190 vec.iov_base = &result;
191 vec.iov_len = sizeof(int);
192 msg.msg_iov = &vec;
193 msg.msg_iovlen = 1;
194 msg.msg_control = &cmsgbuf.buf;
195 msg.msg_controllen = sizeof(cmsgbuf.buf);
197 if ((n = recvmsg(fd, &msg, 0)) != sizeof(int)) {
198 fprintf(stderr, "read %zu bytes bu wanted %zu\n", n, sizeof(int));
199 return -1;
202 if (result) {
203 cmsg = CMSG_FIRSTHDR(&msg);
204 if (cmsg == NULL || cmsg->cmsg_type != SCM_RIGHTS)
205 return -1;
206 return (*(int *)CMSG_DATA(cmsg));
207 } else
208 return -1;
211 static inline void
212 safe_setenv(const char *name, const char *val)
214 if (val == NULL)
215 val = "";
216 setenv(name, val, 1);
219 static char *
220 xasprintf(const char *fmt, ...)
222 va_list ap;
223 char *s;
225 va_start(ap, fmt);
226 if (vasprintf(&s, fmt, ap) == -1)
227 s = NULL;
228 va_end(ap);
230 return s;
233 /* fd or -1 on error */
234 static int
235 launch_cgi(struct iri *iri, const char *spath, char *relpath,
236 const char *addr, const char *ruser, const char *cissuer,
237 const char *chash, struct vhost *vhost)
239 int p[2]; /* read end, write end */
241 if (pipe(p) == -1)
242 return -1;
244 switch (fork()) {
245 case -1:
246 return -1;
248 case 0: { /* child */
249 char *argv[] = {NULL, NULL};
250 char *ex, *pwd;
251 char iribuf[GEMINI_URL_LEN];
252 char path[PATH_MAX];
254 close(p[0]);
255 if (dup2(p[1], 1) == -1)
256 goto childerr;
258 ex = xasprintf("%s/%s", vhost->dir, spath);
259 argv[0] = ex;
261 serialize_iri(iri, iribuf, sizeof(iribuf));
263 safe_setenv("GATEWAY_INTERFACE", "CGI/1.1");
264 safe_setenv("GEMINI_DOCUMENT_ROOT", vhost->dir);
265 safe_setenv("GEMINI_SCRIPT_FILENAME",
266 xasprintf("%s/%s", vhost->dir, spath));
267 safe_setenv("GEMINI_URL", iribuf);
269 strlcpy(path, "/", sizeof(path));
270 strlcat(path, spath, sizeof(path));
271 safe_setenv("GEMINI_URL_PATH", path);
273 if (relpath != NULL) {
274 strlcpy(path, "/", sizeof(path));
275 strlcat(path, relpath, sizeof(path));
276 safe_setenv("PATH_INFO", path);
278 strlcpy(path, vhost->dir, sizeof(path));
279 strlcat(path, "/", sizeof(path));
280 strlcat(path, relpath, sizeof(path));
281 safe_setenv("PATH_TRANSLATED", path);
284 safe_setenv("QUERY_STRING", iri->query);
285 safe_setenv("REMOTE_ADDR", addr);
286 safe_setenv("REMOTE_HOST", addr);
287 safe_setenv("REQUEST_METHOD", "");
289 strlcpy(path, "/", sizeof(path));
290 strlcat(path, spath, sizeof(path));
291 safe_setenv("SCRIPT_NAME", path);
293 safe_setenv("SERVER_NAME", iri->host);
295 snprintf(path, sizeof(path), "%d", conf.port);
296 safe_setenv("SERVER_PORT", path);
298 safe_setenv("SERVER_PROTOCOL", "GEMINI");
299 safe_setenv("SERVER_SOFTWARE", "gmid/1.5");
301 if (ruser != NULL)
302 safe_setenv("AUTH_TYPE", "Certificate");
303 else
304 safe_setenv("AUTH_TYPE", "");
306 safe_setenv("REMOTE_USER", ruser);
307 safe_setenv("TLS_CLIENT_ISSUER", cissuer);
308 safe_setenv("TLS_CLIENT_HASH", chash);
310 strlcpy(path, argv[0], sizeof(path));
311 pwd = dirname(path);
312 if (chdir(pwd)) {
313 warn("chdir");
314 goto childerr;
317 execvp(argv[0], argv);
318 warn("execvp: %s", argv[0]);
319 goto childerr;
322 default:
323 close(p[1]);
324 mark_nonblock(p[0]);
325 return p[0];
328 childerr:
329 dprintf(p[1], "%d internal server error\r\n", TEMP_FAILURE);
330 _exit(1);
333 int
334 executor_main()
336 char *spath, *relpath, *addr, *ruser, *cissuer, *chash;
337 struct vhost *vhost;
338 struct iri iri;
339 int d;
341 #ifdef __OpenBSD__
342 for (vhost = hosts; vhost->domain != NULL; ++vhost) {
343 /* r so we can chdir into the correct directory */
344 if (unveil(vhost->dir, "rx") == -1)
345 err(1, "unveil %s for domain %s",
346 vhost->dir, vhost->domain);
349 /* rpath to chdir into the correct directory */
350 if (pledge("stdio rpath sendfd proc exec", NULL))
351 err(1, "pledge");
352 #endif
354 while (!hupped) {
355 if (!recv_iri(exfd, &iri)
356 || !recv_string(exfd, &spath)
357 || !recv_string(exfd, &relpath)
358 || !recv_string(exfd, &addr)
359 || !recv_string(exfd, &ruser)
360 || !recv_string(exfd, &cissuer)
361 || !recv_string(exfd, &chash)
362 || !recv_vhost(exfd, &vhost))
363 break;
365 d = launch_cgi(&iri, spath, relpath, addr, ruser, cissuer, chash,
366 vhost);
367 if (!send_fd(exfd, d))
368 break;
369 close(d);
371 free_recvd_iri(&iri);
372 free(spath);
373 free(relpath);
374 free(addr);
375 free(ruser);
376 free(cissuer);
377 free(chash);
380 if (hupped)
381 _exit(0);
383 /* kill all process in my group. This means the listener and
384 * every pending CGI script. */
385 kill(0, SIGINT);
386 return 1;