Blob


1 /*
2 * Copyright (c) 2020 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 <errno.h>
18 #include <fcntl.h>
19 #include <limits.h>
20 #include <netdb.h>
21 #include <signal.h>
22 #include <stdarg.h>
23 #include <string.h>
25 #include "gmid.h"
27 struct vhost hosts[HOSTSLEN];
29 int goterror;
31 int exfd;
33 struct conf conf;
35 void
36 fatal(const char *fmt, ...)
37 {
38 va_list ap;
40 va_start(ap, fmt);
42 if (conf.foreground) {
43 vfprintf(stderr, fmt, ap);
44 fprintf(stderr, "\n");
45 } else
46 vsyslog(LOG_DAEMON | LOG_CRIT, fmt, ap);
48 va_end(ap);
49 exit(1);
50 }
52 void
53 logs(int priority, struct client *c,
54 const char *fmt, ...)
55 {
56 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
57 char *fmted, *s;
58 size_t len;
59 int ec;
60 va_list ap;
62 va_start(ap, fmt);
64 if (c == NULL) {
65 strncpy(hbuf, "<internal>", sizeof(hbuf));
66 sbuf[0] = '\0';
67 } else {
68 len = sizeof(c->addr);
69 ec = getnameinfo((struct sockaddr*)&c->addr, len,
70 hbuf, sizeof(hbuf),
71 sbuf, sizeof(sbuf),
72 NI_NUMERICHOST | NI_NUMERICSERV);
73 if (ec != 0)
74 fatal("getnameinfo: %s", gai_strerror(ec));
75 }
77 if (vasprintf(&fmted, fmt, ap) == -1)
78 fatal("vasprintf: %s", strerror(errno));
80 if (conf.foreground)
81 fprintf(stderr, "%s:%s %s\n", hbuf, sbuf, fmted);
82 else {
83 if (asprintf(&s, "%s:%s %s", hbuf, sbuf, fmted) == -1)
84 fatal("asprintf: %s", strerror(errno));
85 syslog(priority | LOG_DAEMON, "%s", s);
86 free(s);
87 }
89 free(fmted);
91 va_end(ap);
92 }
94 void
95 sig_handler(int sig)
96 {
97 (void)sig;
98 }
100 int
101 starts_with(const char *str, const char *prefix)
103 size_t i;
105 for (i = 0; prefix[i] != '\0'; ++i)
106 if (str[i] != prefix[i])
107 return 0;
108 return 1;
111 ssize_t
112 filesize(int fd)
114 ssize_t len;
116 if ((len = lseek(fd, 0, SEEK_END)) == -1)
117 return -1;
118 if (lseek(fd, 0, SEEK_SET) == -1)
119 return -1;
120 return len;
123 char *
124 absolutify_path(const char *path)
126 char *wd, *r;
128 if (*path == '/')
129 return strdup(path);
131 wd = getcwd(NULL, 0);
132 if (asprintf(&r, "%s/%s", wd, path) == -1)
133 fatal("asprintf: %s", strerror(errno));
134 free(wd);
135 return r;
138 void
139 yyerror(const char *msg)
141 goterror = 1;
142 fprintf(stderr, "%d: %s\n", yylineno, msg);
145 int
146 parse_portno(const char *p)
148 char *ep;
149 long lval;
151 errno = 0;
152 lval = strtol(p, &ep, 10);
153 if (p[0] == '\0' || *ep != '\0')
154 fatal("not a number: %s", p);
155 if (lval < 0 || lval > UINT16_MAX)
156 fatal("port number out of range for domain %s: %ld", p, lval);
157 return lval;
160 void
161 parse_conf(const char *path)
163 if ((yyin = fopen(path, "r")) == NULL)
164 fatal("cannot open config %s", path);
165 yyparse();
166 fclose(yyin);
168 if (goterror)
169 exit(1);
172 void
173 load_vhosts(struct tls_config *tlsconf)
175 struct vhost *h;
177 /* we need to set something, then we can add how many key we want */
178 if (tls_config_set_keypair_file(tlsconf, hosts->cert, hosts->key))
179 fatal("tls_config_set_keypair_file failed");
181 for (h = hosts; h->domain != NULL; ++h) {
182 if (tls_config_add_keypair_file(tlsconf, h->cert, h->key) == -1)
183 fatal("failed to load the keypair (%s, %s)",
184 h->cert, h->key);
186 if ((h->dirfd = open(h->dir, O_RDONLY | O_DIRECTORY)) == -1)
187 fatal("open %s for domain %s", h->dir, h->domain);
191 int
192 make_socket(int port, int family)
194 int sock, v;
195 struct sockaddr_in addr4;
196 struct sockaddr_in6 addr6;
197 struct sockaddr *addr;
198 socklen_t len;
200 switch (family) {
201 case AF_INET:
202 bzero(&addr4, sizeof(addr4));
203 addr4.sin_family = family;
204 addr4.sin_port = htons(port);
205 addr4.sin_addr.s_addr = INADDR_ANY;
206 addr = (struct sockaddr*)&addr4;
207 len = sizeof(addr4);
208 break;
210 case AF_INET6:
211 bzero(&addr6, sizeof(addr6));
212 addr6.sin6_family = AF_INET6;
213 addr6.sin6_port = htons(port);
214 addr6.sin6_addr = in6addr_any;
215 addr = (struct sockaddr*)&addr6;
216 len = sizeof(addr6);
217 break;
219 default:
220 /* unreachable */
221 abort();
224 if ((sock = socket(family, SOCK_STREAM, 0)) == -1)
225 fatal("socket: %s", strerror(errno));
227 v = 1;
228 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
229 fatal("setsockopt(SO_REUSEADDR): %s", strerror(errno));
231 v = 1;
232 if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
233 fatal("setsockopt(SO_REUSEPORT): %s", strerror(errno));
235 mark_nonblock(sock);
237 if (bind(sock, addr, len) == -1)
238 fatal("bind: %s", strerror(errno));
240 if (listen(sock, 16) == -1)
241 fatal("listen: %s", strerror(errno));
243 return sock;
246 int
247 listener_main()
249 int sock4, sock6;
250 struct tls *ctx = NULL;
251 struct tls_config *tlsconf;
253 load_default_mime();
255 if ((tlsconf = tls_config_new()) == NULL)
256 fatal("tls_config_new");
258 /* optionally accept client certs, but don't try to verify them */
259 tls_config_verify_client_optional(tlsconf);
260 tls_config_insecure_noverifycert(tlsconf);
262 if (tls_config_set_protocols(tlsconf, conf.protos) == -1)
263 fatal("tls_config_set_protocols");
265 if ((ctx = tls_server()) == NULL)
266 fatal("tls_server failure");
268 load_vhosts(tlsconf);
270 if (tls_configure(ctx, tlsconf) == -1)
271 fatal("tls_configure: %s", tls_error(ctx));
273 if (!conf.foreground && daemon(0, 1) == -1)
274 exit(1);
276 sock4 = make_socket(conf.port, AF_INET);
277 sock6 = -1;
278 if (conf.ipv6)
279 sock6 = make_socket(conf.port, AF_INET6);
281 sandbox();
282 loop(ctx, sock4, sock6);
284 return 0;
287 void
288 usage(const char *me)
290 fprintf(stderr,
291 "USAGE: %s [-n] [-c config] | [-6fh] [-C cert] [-d root] [-K key] "
292 "[-p port] [-x cgi-bin]\n",
293 me);
296 int
297 main(int argc, char **argv)
299 int ch, p[2];
300 const char *config_path = NULL;
301 size_t i;
302 int conftest = 0;
304 bzero(hosts, sizeof(hosts));
305 for (i = 0; i < HOSTSLEN; ++i)
306 hosts[i].dirfd = -1;
308 conf.foreground = 1;
309 conf.port = 1965;
310 conf.ipv6 = 0;
311 conf.protos = TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3;
313 init_mime();
315 while ((ch = getopt(argc, argv, "6C:c:d:fhK:np:x:")) != -1) {
316 switch (ch) {
317 case '6':
318 conf.ipv6 = 1;
319 break;
321 case 'C':
322 hosts[0].cert = optarg;
323 break;
325 case 'c':
326 config_path = optarg;
327 break;
329 case 'd':
330 free((char*)hosts[0].dir);
331 if ((hosts[0].dir = absolutify_path(optarg)) == NULL)
332 fatal("absolutify_path");
333 break;
335 case 'f':
336 conf.foreground = 1;
337 break;
339 case 'h':
340 usage(*argv);
341 return 0;
343 case 'K':
344 hosts[0].key = optarg;
345 break;
347 case 'n':
348 conftest = 1;
349 break;
351 case 'p':
352 conf.port = parse_portno(optarg);
353 break;
355 case 'x':
356 /* drop the starting / (if any) */
357 if (*optarg == '/')
358 optarg++;
359 hosts[0].cgi = optarg;
360 break;
362 default:
363 usage(*argv);
364 return 1;
368 if (config_path != NULL) {
369 if (hosts[0].cert != NULL || hosts[0].key != NULL ||
370 hosts[0].dir != NULL)
371 fatal("can't specify options in conf mode");
372 parse_conf(config_path);
373 } else {
374 if (hosts[0].cert == NULL || hosts[0].key == NULL ||
375 hosts[0].dir == NULL)
376 fatal("missing cert, key or root directory to serve");
377 hosts[0].domain = "*";
380 if (conftest) {
381 puts("config OK");
382 return 0;
385 signal(SIGPIPE, SIG_IGN);
386 signal(SIGCHLD, SIG_IGN);
388 #ifdef SIGINFO
389 signal(SIGINFO, sig_handler);
390 #endif
391 signal(SIGUSR2, sig_handler);
393 if (!conf.foreground) {
394 signal(SIGHUP, SIG_IGN);
395 if (daemon(1, 1) == -1)
396 fatal("daemon: %s", strerror(errno));
399 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, p) == -1)
400 fatal("socketpair: %s", strerror(errno));
402 switch (fork()) {
403 case -1:
404 fatal("fork: %s", strerror(errno));
406 case 0: /* child */
407 close(p[0]);
408 exfd = p[1];
409 listener_main();
410 _exit(0);
412 default: /* parent */
413 close(p[1]);
414 return executor_main(p[0]);