2 * Copyright (c) 2022 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 struct imsgbuf ibuf, logibuf;
33 struct fcgi fcgi[FCGI_MAX]; /* just because it's referenced */
34 struct vhosthead hosts;
37 static const struct option opts[] = {
38 {"help", no_argument, NULL, 'h'},
39 {"version", no_argument, NULL, 'V'},
44 load_local_cert(struct vhost *h, const char *hostname, const char *dir)
48 if (asprintf(&cert, "%s/%s.cert.pem", dir, hostname) == -1)
50 if (asprintf(&key, "%s/%s.key.pem", dir, hostname) == -1)
53 if (access(cert, R_OK) == -1 || access(key, R_OK) == -1)
54 gen_certificate(hostname, cert, key);
61 /* wrapper around dirname(3). dn must be PATH_MAX+1 at least. */
63 pdirname(const char *path, char *dn)
68 strlcpy(p, path, sizeof(p));
70 memmove(dn, t, strlen(t)+1);
74 mkdirs(const char *path, mode_t mode)
76 char dname[PATH_MAX+1];
78 pdirname(path, dname);
79 if (!strcmp(dname, "/"))
82 if (mkdir(path, mode) != 0 && errno != EEXIST)
83 fatal("can't mkdir %s: %s", path, strerror(errno));
86 /* $XDG_DATA_HOME/gmid */
90 const char *home, *xdg;
93 if ((xdg = getenv("XDG_DATA_HOME")) == NULL) {
94 if ((home = getenv("HOME")) == NULL)
95 errx(1, "XDG_DATA_HOME and HOME both empty");
96 if (asprintf(&t, "%s/.local/share/gmid", home) == -1)
99 if (asprintf(&t, "%s/gmid", xdg) == -1)
112 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, p) == -1)
113 err(1, "socketpair");
120 setproctitle("logger");
121 imsg_init(&logibuf, p[1]);
122 _exit(logger_main(p[1], &logibuf));
125 imsg_init(&logibuf, p[0]);
131 serve(const char *host, int port, const char *dir, struct tls *ctx)
133 struct addrinfo hints, *res, *res0;
134 int error, saved_errno, sock = -1;
135 const char *cause = NULL;
138 if (snprintf(service, sizeof(service), "%d", port) < 0)
141 memset(&hints, 0, sizeof(hints));
142 hints.ai_family = AF_UNSPEC;
143 hints.ai_socktype = SOCK_STREAM;
144 hints.ai_flags = AI_PASSIVE;
145 error = getaddrinfo(host, service, &hints, &res0);
147 fatal("%s", gai_strerror(error));
148 for (res = res0; res; res = res->ai_next) {
149 sock = socket(res->ai_family, res->ai_socktype,
156 if (bind(sock, res->ai_addr, res->ai_addrlen) == -1) {
164 if (listen(sock, 5) == -1)
168 * for the time being, we're happy as soon as
178 log_notice(NULL, "serving %s on port %d", dir, port);
179 loop(ctx, sock, -1, NULL);
187 "Version: " GMID_STRING "\n"
188 "Usage: %s [-hVv] [-d certs-dir] [-H hostname] [-p port] [dir]\n",
194 main(int argc, char **argv)
196 struct tls_config *tlsconf;
199 struct location *loc;
200 const char *errstr, *certs_dir = NULL, *hostname = "localhost";
207 while ((ch = getopt_long(argc, argv, "d:H:hp:Vv", opts, NULL)) != -1) {
219 conf.port = strtonum(optarg, 0, UINT16_MAX, &errstr);
221 fatal("port number is %s: %s", errstr, optarg);
224 puts("Version: " GMID_STRING);
237 /* prepare the configuration */
239 init_mime(&conf.mime);
241 if (certs_dir == NULL)
242 certs_dir = data_dir();
244 if (load_default_mime(&conf.mime) == -1)
245 fatal("can't load default mime types");
246 sort_mime(&conf.mime);
248 /* set up the implicit vhost and location */
250 host = xcalloc(1, sizeof(*host));
251 TAILQ_INSERT_HEAD(&hosts, host, vhosts);
253 loc = xcalloc(1, sizeof(*loc));
255 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
257 load_local_cert(host, hostname, certs_dir);
264 if (getcwd(path, sizeof(path)) == NULL)
268 loc->dir = absolutify_path(*argv);
270 if ((loc->dirfd = open(loc->dir, O_RDONLY|O_DIRECTORY)) == -1)
271 fatal("can't open %s", loc->dir);
275 if ((tlsconf = tls_config_new()) == NULL)
276 fatal("tls_config_new"); /* XXX: fatalx */
278 /* optionally accept client certs but don't try to verify them */
279 tls_config_verify_client_optional(tlsconf);
280 tls_config_insecure_noverifycert(tlsconf);
282 if ((ctx = tls_server()) == NULL)
283 fatal("tls_server failure"); /* XXX: fatalx */
285 if (tls_config_set_keypair_file(tlsconf, host->cert, host->key))
286 fatal("can't load the keypair (%s, %s)",
287 host->cert, host->key);
289 if (tls_configure(ctx, tlsconf) == -1)
290 fatal("tls_configure: %s", tls_error(ctx));
292 /* start the server */
293 signal(SIGPIPE, SIG_IGN);
294 setproctitle("%s", loc->dir);
295 return serve(hostname, conf.port, loc->dir, ctx);