2 3e4749f7 2020-10-02 op * Copyright (c) 2020 Omar Polo <op@omarpolo.com>
4 3e4749f7 2020-10-02 op * Permission to use, copy, modify, and distribute this software for any
5 3e4749f7 2020-10-02 op * purpose with or without fee is hereby granted, provided that the above
6 3e4749f7 2020-10-02 op * copyright notice and this permission notice appear in all copies.
8 3e4749f7 2020-10-02 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 3e4749f7 2020-10-02 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 3e4749f7 2020-10-02 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 3e4749f7 2020-10-02 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 3e4749f7 2020-10-02 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 3e4749f7 2020-10-02 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 3e4749f7 2020-10-02 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 592fd624 2020-10-07 op #include <errno.h>
18 3e4749f7 2020-10-02 op #include <fcntl.h>
19 231bfcdc 2020-12-21 op #include <limits.h>
20 709d6e5e 2021-01-10 op #include <netdb.h>
21 ae08ec7d 2021-01-25 op #include <pwd.h>
22 0cf902af 2020-11-03 op #include <signal.h>
23 80bbcad5 2021-01-10 op #include <stdarg.h>
24 3e4749f7 2020-10-02 op #include <string.h>
26 488f059a 2020-12-24 op #include "gmid.h"
28 15902770 2021-01-15 op struct vhost hosts[HOSTSLEN];
34 15902770 2021-01-15 op struct conf conf;
36 ae08ec7d 2021-01-25 op struct tls *ctx;
39 80bbcad5 2021-01-10 op fatal(const char *fmt, ...)
43 80bbcad5 2021-01-10 op va_start(ap, fmt);
45 15902770 2021-01-15 op if (conf.foreground) {
46 80bbcad5 2021-01-10 op vfprintf(stderr, fmt, ap);
47 80bbcad5 2021-01-10 op fprintf(stderr, "\n");
49 80bbcad5 2021-01-10 op vsyslog(LOG_DAEMON | LOG_CRIT, fmt, ap);
56 709d6e5e 2021-01-10 op logs(int priority, struct client *c,
57 709d6e5e 2021-01-10 op const char *fmt, ...)
59 709d6e5e 2021-01-10 op char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
60 709d6e5e 2021-01-10 op char *fmted, *s;
65 80bbcad5 2021-01-10 op va_start(ap, fmt);
67 dafb57b8 2021-01-15 op if (c == NULL) {
68 dafb57b8 2021-01-15 op strncpy(hbuf, "<internal>", sizeof(hbuf));
69 dafb57b8 2021-01-15 op sbuf[0] = '\0';
71 dafb57b8 2021-01-15 op len = sizeof(c->addr);
72 dafb57b8 2021-01-15 op ec = getnameinfo((struct sockaddr*)&c->addr, len,
73 f7b816dc 2021-01-15 op hbuf, sizeof(hbuf),
74 f7b816dc 2021-01-15 op sbuf, sizeof(sbuf),
75 f7b816dc 2021-01-15 op NI_NUMERICHOST | NI_NUMERICSERV);
77 dafb57b8 2021-01-15 op fatal("getnameinfo: %s", gai_strerror(ec));
80 709d6e5e 2021-01-10 op if (vasprintf(&fmted, fmt, ap) == -1)
81 709d6e5e 2021-01-10 op fatal("vasprintf: %s", strerror(errno));
83 15902770 2021-01-15 op if (conf.foreground)
84 709d6e5e 2021-01-10 op fprintf(stderr, "%s:%s %s\n", hbuf, sbuf, fmted);
86 709d6e5e 2021-01-10 op if (asprintf(&s, "%s:%s %s", hbuf, sbuf, fmted) == -1)
87 709d6e5e 2021-01-10 op fatal("asprintf: %s", strerror(errno));
88 709d6e5e 2021-01-10 op syslog(priority | LOG_DAEMON, "%s", s);
97 0be51733 2021-01-20 op /* strchr, but with a bound */
99 0be51733 2021-01-20 op gmid_strnchr(char *s, int c, size_t len)
103 0be51733 2021-01-20 op for (i = 0; i < len; ++i)
104 0be51733 2021-01-20 op if (s[i] == c)
105 0be51733 2021-01-20 op return &s[i];
110 0be51733 2021-01-20 op log_request(struct client *c, char *meta, size_t l)
112 0be51733 2021-01-20 op char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV], b[GEMINI_URL_LEN];
117 0be51733 2021-01-20 op len = sizeof(c->addr);
118 0be51733 2021-01-20 op ec = getnameinfo((struct sockaddr*)&c->addr, len,
119 0be51733 2021-01-20 op hbuf, sizeof(hbuf),
120 0be51733 2021-01-20 op sbuf, sizeof(sbuf),
121 0be51733 2021-01-20 op NI_NUMERICHOST | NI_NUMERICSERV);
123 0be51733 2021-01-20 op fatal("getnameinfo: %s", gai_strerror(ec));
125 0ab65593 2021-01-21 op if (c->iri.schema != NULL) {
126 0ab65593 2021-01-21 op /* serialize the IRI */
127 0ab65593 2021-01-21 op strlcpy(b, c->iri.schema, sizeof(b));
128 0ab65593 2021-01-21 op strlcat(b, "://", sizeof(b));
129 0ab65593 2021-01-21 op strlcat(b, c->iri.host, sizeof(b));
130 0ab65593 2021-01-21 op strlcat(b, "/", sizeof(b));
131 0ab65593 2021-01-21 op strlcat(b, c->iri.path, sizeof(b)); /* TODO: sanitize UTF8 */
132 0ab65593 2021-01-21 op if (*c->iri.query != '\0') { /* TODO: sanitize UTF8 */
133 0ab65593 2021-01-21 op strlcat(b, "?", sizeof(b));
134 0ab65593 2021-01-21 op strlcat(b, c->iri.query, sizeof(b));
137 0ab65593 2021-01-21 op strlcpy(b, c->req, sizeof(b));
140 0be51733 2021-01-20 op if ((t = gmid_strnchr(meta, '\r', l)) == NULL)
141 0be51733 2021-01-20 op t = meta + len;
143 0be51733 2021-01-20 op if (conf.foreground)
144 0be51733 2021-01-20 op fprintf(stderr, "%s:%s GET %s %.*s\n", hbuf, sbuf, b,
145 0be51733 2021-01-20 op (int)(t - meta), meta);
147 0be51733 2021-01-20 op syslog(LOG_INFO | LOG_DAEMON, "%s:%s GET %s %.*s",
148 0be51733 2021-01-20 op hbuf, sbuf, b, (int)(t - meta), meta);
152 4a28dd01 2020-12-28 op sig_handler(int sig)
158 72f653b6 2020-11-06 op starts_with(const char *str, const char *prefix)
162 252908e6 2021-01-24 op if (prefix == NULL)
165 72f653b6 2020-11-06 op for (i = 0; prefix[i] != '\0'; ++i)
166 72f653b6 2020-11-06 op if (str[i] != prefix[i])
172 f77a8c86 2021-01-21 op ends_with(const char *str, const char *sufx)
176 f77a8c86 2021-01-21 op i = strlen(str);
177 f77a8c86 2021-01-21 op j = strlen(sufx);
183 f77a8c86 2021-01-21 op for (j = 0; str[i] != '\0'; i++, j++)
184 f77a8c86 2021-01-21 op if (str[i] != sufx[j])
190 f28f9311 2020-10-14 op filesize(int fd)
194 f28f9311 2020-10-14 op if ((len = lseek(fd, 0, SEEK_END)) == -1)
196 f28f9311 2020-10-14 op if (lseek(fd, 0, SEEK_SET) == -1)
202 d3a08f4d 2021-01-17 op absolutify_path(const char *path)
204 d3a08f4d 2021-01-17 op char *wd, *r;
206 d3a08f4d 2021-01-17 op if (*path == '/')
207 d3a08f4d 2021-01-17 op return strdup(path);
209 d3a08f4d 2021-01-17 op wd = getcwd(NULL, 0);
210 d3a08f4d 2021-01-17 op if (asprintf(&r, "%s/%s", wd, path) == -1)
211 132cae8c 2021-01-18 op fatal("asprintf: %s", strerror(errno));
217 d3a08f4d 2021-01-17 op yyerror(const char *msg)
219 d3a08f4d 2021-01-17 op goterror = 1;
220 d3a08f4d 2021-01-17 op fprintf(stderr, "%d: %s\n", yylineno, msg);
224 d3a08f4d 2021-01-17 op parse_portno(const char *p)
226 ea28a449 2021-01-21 op const char *errstr;
229 a5a09e44 2021-01-21 op n = strtonum(p, 0, UINT16_MAX, &errstr);
230 a5a09e44 2021-01-21 op if (errstr != NULL)
231 a5a09e44 2021-01-21 op errx(1, "port number is %s: %s", errstr, p);
236 d3a08f4d 2021-01-17 op parse_conf(const char *path)
238 d3a08f4d 2021-01-17 op if ((yyin = fopen(path, "r")) == NULL)
239 132cae8c 2021-01-18 op fatal("cannot open config %s", path);
241 d3a08f4d 2021-01-17 op fclose(yyin);
243 d3a08f4d 2021-01-17 op if (goterror)
248 ae08ec7d 2021-01-25 op load_vhosts(void)
250 15902770 2021-01-15 op struct vhost *h;
252 15902770 2021-01-15 op for (h = hosts; h->domain != NULL; ++h) {
253 d3a08f4d 2021-01-17 op if ((h->dirfd = open(h->dir, O_RDONLY | O_DIRECTORY)) == -1)
254 132cae8c 2021-01-18 op fatal("open %s for domain %s", h->dir, h->domain);
259 9468027b 2020-10-15 op make_socket(int port, int family)
262 9468027b 2020-10-15 op struct sockaddr_in addr4;
263 9468027b 2020-10-15 op struct sockaddr_in6 addr6;
264 9468027b 2020-10-15 op struct sockaddr *addr;
265 9468027b 2020-10-15 op socklen_t len;
267 9468027b 2020-10-15 op switch (family) {
268 9468027b 2020-10-15 op case AF_INET:
269 9468027b 2020-10-15 op bzero(&addr4, sizeof(addr4));
270 9468027b 2020-10-15 op addr4.sin_family = family;
271 9468027b 2020-10-15 op addr4.sin_port = htons(port);
272 9468027b 2020-10-15 op addr4.sin_addr.s_addr = INADDR_ANY;
273 9468027b 2020-10-15 op addr = (struct sockaddr*)&addr4;
274 9468027b 2020-10-15 op len = sizeof(addr4);
277 9468027b 2020-10-15 op case AF_INET6:
278 9468027b 2020-10-15 op bzero(&addr6, sizeof(addr6));
279 9468027b 2020-10-15 op addr6.sin6_family = AF_INET6;
280 9468027b 2020-10-15 op addr6.sin6_port = htons(port);
281 9468027b 2020-10-15 op addr6.sin6_addr = in6addr_any;
282 9468027b 2020-10-15 op addr = (struct sockaddr*)&addr6;
283 9468027b 2020-10-15 op len = sizeof(addr6);
287 9468027b 2020-10-15 op /* unreachable */
291 9468027b 2020-10-15 op if ((sock = socket(family, SOCK_STREAM, 0)) == -1)
292 80bbcad5 2021-01-10 op fatal("socket: %s", strerror(errno));
295 3e4749f7 2020-10-02 op if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
296 80bbcad5 2021-01-10 op fatal("setsockopt(SO_REUSEADDR): %s", strerror(errno));
299 3e4749f7 2020-10-02 op if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
300 80bbcad5 2021-01-10 op fatal("setsockopt(SO_REUSEPORT): %s", strerror(errno));
302 592fd624 2020-10-07 op mark_nonblock(sock);
304 9468027b 2020-10-15 op if (bind(sock, addr, len) == -1)
305 80bbcad5 2021-01-10 op fatal("bind: %s", strerror(errno));
307 3e4749f7 2020-10-02 op if (listen(sock, 16) == -1)
308 80bbcad5 2021-01-10 op fatal("listen: %s", strerror(errno));
314 ae08ec7d 2021-01-25 op setup_tls(void)
316 881a9dd9 2021-01-16 op struct tls_config *tlsconf;
317 ae08ec7d 2021-01-25 op struct vhost *h;
319 881a9dd9 2021-01-16 op if ((tlsconf = tls_config_new()) == NULL)
320 132cae8c 2021-01-18 op fatal("tls_config_new");
322 881a9dd9 2021-01-16 op /* optionally accept client certs, but don't try to verify them */
323 881a9dd9 2021-01-16 op tls_config_verify_client_optional(tlsconf);
324 881a9dd9 2021-01-16 op tls_config_insecure_noverifycert(tlsconf);
326 881a9dd9 2021-01-16 op if (tls_config_set_protocols(tlsconf, conf.protos) == -1)
327 132cae8c 2021-01-18 op fatal("tls_config_set_protocols");
329 881a9dd9 2021-01-16 op if ((ctx = tls_server()) == NULL)
330 132cae8c 2021-01-18 op fatal("tls_server failure");
332 ae08ec7d 2021-01-25 op /* we need to set something, then we can add how many key we want */
333 ae08ec7d 2021-01-25 op if (tls_config_set_keypair_file(tlsconf, hosts->cert, hosts->key))
334 ae08ec7d 2021-01-25 op fatal("tls_config_set_keypair_file failed");
336 ae08ec7d 2021-01-25 op for (h = hosts; h->domain != NULL; ++h) {
337 ae08ec7d 2021-01-25 op if (tls_config_add_keypair_file(tlsconf, h->cert, h->key) == -1)
338 ae08ec7d 2021-01-25 op fatal("failed to load the keypair (%s, %s)",
339 ae08ec7d 2021-01-25 op h->cert, h->key);
342 881a9dd9 2021-01-16 op if (tls_configure(ctx, tlsconf) == -1)
343 132cae8c 2021-01-18 op fatal("tls_configure: %s", tls_error(ctx));
347 ae08ec7d 2021-01-25 op listener_main(void)
349 ae08ec7d 2021-01-25 op int sock4, sock6;
351 ae08ec7d 2021-01-25 op load_default_mime(&conf.mime);
353 881a9dd9 2021-01-16 op if (!conf.foreground && daemon(0, 1) == -1)
356 881a9dd9 2021-01-16 op sock4 = make_socket(conf.port, AF_INET);
358 881a9dd9 2021-01-16 op if (conf.ipv6)
359 881a9dd9 2021-01-16 op sock6 = make_socket(conf.port, AF_INET6);
361 ae08ec7d 2021-01-25 op load_vhosts();
364 881a9dd9 2021-01-16 op loop(ctx, sock4, sock6);
370 8d6ae384 2021-01-24 op init_config(void)
374 15902770 2021-01-15 op bzero(hosts, sizeof(hosts));
375 15902770 2021-01-15 op for (i = 0; i < HOSTSLEN; ++i)
376 15902770 2021-01-15 op hosts[i].dirfd = -1;
378 15902770 2021-01-15 op conf.foreground = 1;
379 15902770 2021-01-15 op conf.port = 1965;
380 15902770 2021-01-15 op conf.ipv6 = 0;
381 5bc3c98e 2021-01-15 op conf.protos = TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3;
383 b2a6b613 2021-01-21 op init_mime(&conf.mime);
385 ae08ec7d 2021-01-25 op conf.chroot = NULL;
386 ae08ec7d 2021-01-25 op conf.user = NULL;
390 ae08ec7d 2021-01-25 op drop_priv(void)
392 ae08ec7d 2021-01-25 op struct passwd *pw = NULL;
394 ae08ec7d 2021-01-25 op if (conf.chroot != NULL && conf.user == NULL)
395 ae08ec7d 2021-01-25 op fatal("can't chroot without an user to switch to after.");
397 ae08ec7d 2021-01-25 op if (conf.user != NULL) {
398 ae08ec7d 2021-01-25 op if ((pw = getpwnam(conf.user)) == NULL)
399 ae08ec7d 2021-01-25 op fatal("can't find user %s", conf.user);
402 ae08ec7d 2021-01-25 op if (conf.chroot != NULL) {
403 ae08ec7d 2021-01-25 op if (chroot(conf.chroot) != 0 || chdir("/") != 0)
404 ae08ec7d 2021-01-25 op fatal("%s: %s", conf.chroot, strerror(errno));
407 ae08ec7d 2021-01-25 op if (pw != NULL) {
408 ae08ec7d 2021-01-25 op if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) == -1)
409 ae08ec7d 2021-01-25 op fatal("setresuid(%d): %s", pw->pw_uid,
410 ae08ec7d 2021-01-25 op strerror(errno));
413 ae08ec7d 2021-01-25 op if (getuid() == 0)
414 ae08ec7d 2021-01-25 op LOGW(NULL, "%s",
415 ae08ec7d 2021-01-25 op "not a good idea to run a network daemon as root");
419 2030e314 2021-01-25 op usage(const char *me)
421 2030e314 2021-01-25 op fprintf(stderr,
422 2030e314 2021-01-25 op "USAGE: %s [-n] [-c config] | [-6fh] [-C cert] [-d root] [-K key] "
423 2030e314 2021-01-25 op "[-p port] [-x cgi-bin]\n",
428 8d6ae384 2021-01-24 op main(int argc, char **argv)
430 8d6ae384 2021-01-24 op int ch, p[2];
431 8d6ae384 2021-01-24 op const char *config_path = NULL;
432 8d6ae384 2021-01-24 op int conftest = 0;
434 501e489c 2021-01-24 op init_config();
436 15902770 2021-01-15 op while ((ch = getopt(argc, argv, "6C:c:d:fhK:np:x:")) != -1) {
437 3e4749f7 2020-10-02 op switch (ch) {
439 15902770 2021-01-15 op conf.ipv6 = 1;
443 15902770 2021-01-15 op hosts[0].cert = optarg;
447 15902770 2021-01-15 op config_path = optarg;
451 15902770 2021-01-15 op free((char*)hosts[0].dir);
452 15902770 2021-01-15 op if ((hosts[0].dir = absolutify_path(optarg)) == NULL)
453 132cae8c 2021-01-18 op fatal("absolutify_path");
457 15902770 2021-01-15 op conf.foreground = 1;
461 3e4749f7 2020-10-02 op usage(*argv);
465 15902770 2021-01-15 op hosts[0].key = optarg;
469 15902770 2021-01-15 op conftest = 1;
473 15902770 2021-01-15 op conf.port = parse_portno(optarg);
477 15902770 2021-01-15 op /* drop the starting / (if any) */
478 15902770 2021-01-15 op if (*optarg == '/')
480 15902770 2021-01-15 op hosts[0].cgi = optarg;
484 3e4749f7 2020-10-02 op usage(*argv);
489 15902770 2021-01-15 op if (config_path != NULL) {
490 15902770 2021-01-15 op if (hosts[0].cert != NULL || hosts[0].key != NULL ||
491 15902770 2021-01-15 op hosts[0].dir != NULL)
492 132cae8c 2021-01-18 op fatal("can't specify options in conf mode");
493 15902770 2021-01-15 op parse_conf(config_path);
495 15902770 2021-01-15 op if (hosts[0].cert == NULL || hosts[0].key == NULL ||
496 15902770 2021-01-15 op hosts[0].dir == NULL)
497 132cae8c 2021-01-18 op fatal("missing cert, key or root directory to serve");
498 15902770 2021-01-15 op hosts[0].domain = "*";
501 132cae8c 2021-01-18 op if (conftest) {
502 132cae8c 2021-01-18 op puts("config OK");
506 ae08ec7d 2021-01-25 op /* setup tls before dropping privileges: we don't want user
507 ae08ec7d 2021-01-25 op * to put private certs inside the chroot. */
511 4a28dd01 2020-12-28 op signal(SIGPIPE, SIG_IGN);
512 4a28dd01 2020-12-28 op signal(SIGCHLD, SIG_IGN);
514 4a28dd01 2020-12-28 op #ifdef SIGINFO
515 4a28dd01 2020-12-28 op signal(SIGINFO, sig_handler);
517 4a28dd01 2020-12-28 op signal(SIGUSR2, sig_handler);
519 0170ba02 2021-01-17 op if (!conf.foreground) {
520 4a28dd01 2020-12-28 op signal(SIGHUP, SIG_IGN);
521 0170ba02 2021-01-17 op if (daemon(1, 1) == -1)
522 132cae8c 2021-01-18 op fatal("daemon: %s", strerror(errno));
525 881a9dd9 2021-01-16 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, p) == -1)
526 881a9dd9 2021-01-16 op fatal("socketpair: %s", strerror(errno));
528 881a9dd9 2021-01-16 op switch (fork()) {
530 881a9dd9 2021-01-16 op fatal("fork: %s", strerror(errno));
532 881a9dd9 2021-01-16 op case 0: /* child */
535 881a9dd9 2021-01-16 op listener_main();
538 881a9dd9 2021-01-16 op default: /* parent */
540 881a9dd9 2021-01-16 op return executor_main(p[0]);