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)
134 /* send d though fd. see /usr/src/usr.sbin/syslogd/privsep_fdpass.c
137 send_fd(int fd, int d)
142 unsigned char buf[CMSG_SPACE(sizeof(int))];
144 struct cmsghdr *cmsg;
149 memset(&msg, 0, sizeof(msg));
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;
162 vec.iov_base = &result;
163 vec.iov_len = sizeof(int);
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));
175 /* receive a descriptor via fd */
182 char buf[CMSG_SPACE(sizeof(int))];
184 struct cmsghdr *cmsg;
189 memset(&msg, 0, sizeof(msg));
190 vec.iov_base = &result;
191 vec.iov_len = sizeof(int);
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));
203 cmsg = CMSG_FIRSTHDR(&msg);
204 if (cmsg == NULL || cmsg->cmsg_type != SCM_RIGHTS)
206 return (*(int *)CMSG_DATA(cmsg));
212 safe_setenv(const char *name, const char *val)
216 setenv(name, val, 1);
220 xasprintf(const char *fmt, ...)
226 if (vasprintf(&s, fmt, ap) == -1)
233 /* fd or -1 on error */
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 (pipe2(p, O_NONBLOCK) == -1)
248 case 0: { /* child */
249 char *argv[] = {NULL, NULL};
251 char iribuf[GEMINI_URL_LEN];
255 if (dup2(p[1], 1) == -1)
258 ex = xasprintf("%s/%s", vhost->dir, spath);
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");
302 safe_setenv("AUTH_TYPE", "Certificate");
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));
317 execvp(argv[0], argv);
318 warn("execvp: %s", argv[0]);
328 dprintf(p[1], "%d internal server error\r\n", TEMP_FAILURE);
333 executor_main(int fd)
335 char *spath, *relpath, *addr, *ruser, *cissuer, *chash;
341 for (vhost = hosts; vhost->domain != NULL; ++vhost) {
342 /* r so we can chdir into the correct directory */
343 if (unveil(vhost->dir, "rx") == -1)
344 err(1, "unveil %s for domain %s",
345 vhost->dir, vhost->domain);
348 /* rpath to chdir into the correct directory */
349 if (pledge("stdio rpath sendfd proc exec", NULL))
354 if (!recv_iri(fd, &iri)
355 || !recv_string(fd, &spath)
356 || !recv_string(fd, &relpath)
357 || !recv_string(fd, &addr)
358 || !recv_string(fd, &ruser)
359 || !recv_string(fd, &cissuer)
360 || !recv_string(fd, &chash)
361 || !recv_vhost(fd, &vhost))
364 d = launch_cgi(&iri, spath, relpath, addr, ruser, cissuer, chash,
370 free_recvd_iri(&iri);
379 /* kill all process in my group. This means the listener and
380 * every pending CGI script. */