2 * Copyright (c) 2020 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.
27 struct vhost hosts[HOSTSLEN];
36 fatal(const char *fmt, ...)
42 if (conf.foreground) {
43 vfprintf(stderr, fmt, ap);
44 fprintf(stderr, "\n");
46 vsyslog(LOG_DAEMON | LOG_CRIT, fmt, ap);
53 logs(int priority, struct client *c,
56 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
65 strncpy(hbuf, "<internal>", sizeof(hbuf));
68 len = sizeof(c->addr);
69 ec = getnameinfo((struct sockaddr*)&c->addr, len,
72 NI_NUMERICHOST | NI_NUMERICSERV);
74 fatal("getnameinfo: %s", gai_strerror(ec));
77 if (vasprintf(&fmted, fmt, ap) == -1)
78 fatal("vasprintf: %s", strerror(errno));
81 fprintf(stderr, "%s:%s %s\n", hbuf, sbuf, fmted);
83 if (asprintf(&s, "%s:%s %s", hbuf, sbuf, fmted) == -1)
84 fatal("asprintf: %s", strerror(errno));
85 syslog(priority | LOG_DAEMON, "%s", s);
94 /* strchr, but with a bound */
96 gmid_strnchr(char *s, int c, size_t len)
100 for (i = 0; i < len; ++i)
107 log_request(struct client *c, char *meta, size_t l)
109 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV], b[GEMINI_URL_LEN];
114 len = sizeof(c->addr);
115 ec = getnameinfo((struct sockaddr*)&c->addr, len,
118 NI_NUMERICHOST | NI_NUMERICSERV);
120 fatal("getnameinfo: %s", gai_strerror(ec));
122 if (c->iri.schema != NULL) {
123 /* serialize the IRI */
124 strlcpy(b, c->iri.schema, sizeof(b));
125 strlcat(b, "://", sizeof(b));
126 strlcat(b, c->iri.host, sizeof(b));
127 strlcat(b, "/", sizeof(b));
128 strlcat(b, c->iri.path, sizeof(b)); /* TODO: sanitize UTF8 */
129 if (*c->iri.query != '\0') { /* TODO: sanitize UTF8 */
130 strlcat(b, "?", sizeof(b));
131 strlcat(b, c->iri.query, sizeof(b));
134 strlcpy(b, c->req, sizeof(b));
137 if ((t = gmid_strnchr(meta, '\r', l)) == NULL)
141 fprintf(stderr, "%s:%s GET %s %.*s\n", hbuf, sbuf, b,
142 (int)(t - meta), meta);
144 syslog(LOG_INFO | LOG_DAEMON, "%s:%s GET %s %.*s",
145 hbuf, sbuf, b, (int)(t - meta), meta);
155 starts_with(const char *str, const char *prefix)
162 for (i = 0; prefix[i] != '\0'; ++i)
163 if (str[i] != prefix[i])
169 ends_with(const char *str, const char *sufx)
180 for (j = 0; str[i] != '\0'; i++, j++)
181 if (str[i] != sufx[j])
191 if ((len = lseek(fd, 0, SEEK_END)) == -1)
193 if (lseek(fd, 0, SEEK_SET) == -1)
199 absolutify_path(const char *path)
206 wd = getcwd(NULL, 0);
207 if (asprintf(&r, "%s/%s", wd, path) == -1)
208 fatal("asprintf: %s", strerror(errno));
214 yyerror(const char *msg)
217 fprintf(stderr, "%d: %s\n", yylineno, msg);
221 parse_portno(const char *p)
226 n = strtonum(p, 0, UINT16_MAX, &errstr);
228 errx(1, "port number is %s: %s", errstr, p);
233 parse_conf(const char *path)
235 if ((yyin = fopen(path, "r")) == NULL)
236 fatal("cannot open config %s", path);
245 load_vhosts(struct tls_config *tlsconf)
249 /* we need to set something, then we can add how many key we want */
250 if (tls_config_set_keypair_file(tlsconf, hosts->cert, hosts->key))
251 fatal("tls_config_set_keypair_file failed");
253 for (h = hosts; h->domain != NULL; ++h) {
254 if (tls_config_add_keypair_file(tlsconf, h->cert, h->key) == -1)
255 fatal("failed to load the keypair (%s, %s)",
258 if ((h->dirfd = open(h->dir, O_RDONLY | O_DIRECTORY)) == -1)
259 fatal("open %s for domain %s", h->dir, h->domain);
264 make_socket(int port, int family)
267 struct sockaddr_in addr4;
268 struct sockaddr_in6 addr6;
269 struct sockaddr *addr;
274 bzero(&addr4, sizeof(addr4));
275 addr4.sin_family = family;
276 addr4.sin_port = htons(port);
277 addr4.sin_addr.s_addr = INADDR_ANY;
278 addr = (struct sockaddr*)&addr4;
283 bzero(&addr6, sizeof(addr6));
284 addr6.sin6_family = AF_INET6;
285 addr6.sin6_port = htons(port);
286 addr6.sin6_addr = in6addr_any;
287 addr = (struct sockaddr*)&addr6;
296 if ((sock = socket(family, SOCK_STREAM, 0)) == -1)
297 fatal("socket: %s", strerror(errno));
300 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
301 fatal("setsockopt(SO_REUSEADDR): %s", strerror(errno));
304 if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
305 fatal("setsockopt(SO_REUSEPORT): %s", strerror(errno));
309 if (bind(sock, addr, len) == -1)
310 fatal("bind: %s", strerror(errno));
312 if (listen(sock, 16) == -1)
313 fatal("listen: %s", strerror(errno));
322 struct tls *ctx = NULL;
323 struct tls_config *tlsconf;
325 load_default_mime(&conf.mime);
327 if ((tlsconf = tls_config_new()) == NULL)
328 fatal("tls_config_new");
330 /* optionally accept client certs, but don't try to verify them */
331 tls_config_verify_client_optional(tlsconf);
332 tls_config_insecure_noverifycert(tlsconf);
334 if (tls_config_set_protocols(tlsconf, conf.protos) == -1)
335 fatal("tls_config_set_protocols");
337 if ((ctx = tls_server()) == NULL)
338 fatal("tls_server failure");
340 load_vhosts(tlsconf);
342 if (tls_configure(ctx, tlsconf) == -1)
343 fatal("tls_configure: %s", tls_error(ctx));
345 if (!conf.foreground && daemon(0, 1) == -1)
348 sock4 = make_socket(conf.port, AF_INET);
351 sock6 = make_socket(conf.port, AF_INET6);
354 loop(ctx, sock4, sock6);
360 usage(const char *me)
363 "USAGE: %s [-n] [-c config] | [-6fh] [-C cert] [-d root] [-K key] "
364 "[-p port] [-x cgi-bin]\n",
373 bzero(hosts, sizeof(hosts));
374 for (i = 0; i < HOSTSLEN; ++i)
380 conf.protos = TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3;
382 init_mime(&conf.mime);
386 main(int argc, char **argv)
389 const char *config_path = NULL;
394 while ((ch = getopt(argc, argv, "6C:c:d:fhK:np:x:")) != -1) {
401 hosts[0].cert = optarg;
405 config_path = optarg;
409 free((char*)hosts[0].dir);
410 if ((hosts[0].dir = absolutify_path(optarg)) == NULL)
411 fatal("absolutify_path");
423 hosts[0].key = optarg;
431 conf.port = parse_portno(optarg);
435 /* drop the starting / (if any) */
438 hosts[0].cgi = optarg;
447 if (config_path != NULL) {
448 if (hosts[0].cert != NULL || hosts[0].key != NULL ||
449 hosts[0].dir != NULL)
450 fatal("can't specify options in conf mode");
451 parse_conf(config_path);
453 if (hosts[0].cert == NULL || hosts[0].key == NULL ||
454 hosts[0].dir == NULL)
455 fatal("missing cert, key or root directory to serve");
456 hosts[0].domain = "*";
464 signal(SIGPIPE, SIG_IGN);
465 signal(SIGCHLD, SIG_IGN);
468 signal(SIGINFO, sig_handler);
470 signal(SIGUSR2, sig_handler);
472 if (!conf.foreground) {
473 signal(SIGHUP, SIG_IGN);
474 if (daemon(1, 1) == -1)
475 fatal("daemon: %s", strerror(errno));
478 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, p) == -1)
479 fatal("socketpair: %s", strerror(errno));
483 fatal("fork: %s", strerror(errno));
491 default: /* parent */
493 return executor_main(p[0]);