2 0126d91d 2022-09-07 op * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
4 0126d91d 2022-09-07 op * Permission to use, copy, modify, and distribute this software for any
5 0126d91d 2022-09-07 op * purpose with or without fee is hereby granted, provided that the above
6 0126d91d 2022-09-07 op * copyright notice and this permission notice appear in all copies.
8 0126d91d 2022-09-07 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 0126d91d 2022-09-07 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 0126d91d 2022-09-07 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 0126d91d 2022-09-07 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 0126d91d 2022-09-07 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 0126d91d 2022-09-07 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 0126d91d 2022-09-07 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 0126d91d 2022-09-07 op #include "gmid.h"
19 0126d91d 2022-09-07 op #include <sys/stat.h>
20 0126d91d 2022-09-07 op #include <sys/wait.h>
22 0126d91d 2022-09-07 op #include <errno.h>
23 0126d91d 2022-09-07 op #include <fcntl.h>
24 0126d91d 2022-09-07 op #include <getopt.h>
25 0046c1fe 2023-06-06 op #include <locale.h>
26 0126d91d 2022-09-07 op #include <libgen.h>
27 0126d91d 2022-09-07 op #include <signal.h>
28 0126d91d 2022-09-07 op #include <string.h>
29 0126d91d 2022-09-07 op #include <unistd.h>
31 281a8852 2023-06-06 op #include "logger.h"
33 0126d91d 2022-09-07 op struct imsgbuf ibuf, logibuf;
34 0126d91d 2022-09-07 op struct conf conf;
36 0126d91d 2022-09-07 op struct fcgi fcgi[FCGI_MAX]; /* just because it's referenced */
37 070b3295 2023-06-05 op struct vhosthead hosts = TAILQ_HEAD_INITIALIZER(hosts);
39 0126d91d 2022-09-07 op static const struct option opts[] = {
40 0126d91d 2022-09-07 op {"help", no_argument, NULL, 'h'},
41 0126d91d 2022-09-07 op {"version", no_argument, NULL, 'V'},
42 0126d91d 2022-09-07 op {NULL, 0, NULL, 0},
46 a01a91db 2023-06-05 op drop_priv(void)
52 0126d91d 2022-09-07 op load_local_cert(struct vhost *h, const char *hostname, const char *dir)
54 0126d91d 2022-09-07 op char *cert, *key;
56 0126d91d 2022-09-07 op if (asprintf(&cert, "%s/%s.cert.pem", dir, hostname) == -1)
57 0126d91d 2022-09-07 op errx(1, "asprintf");
58 0126d91d 2022-09-07 op if (asprintf(&key, "%s/%s.key.pem", dir, hostname) == -1)
59 0126d91d 2022-09-07 op errx(1, "asprintf");
61 0126d91d 2022-09-07 op if (access(cert, R_OK) == -1 || access(key, R_OK) == -1)
62 0126d91d 2022-09-07 op gen_certificate(hostname, cert, key);
64 534afd0d 2022-10-05 op strlcpy(h->cert, cert, sizeof(h->cert));
65 534afd0d 2022-10-05 op strlcpy(h->key, key, sizeof(h->key));
66 534afd0d 2022-10-05 op strlcpy(h->domain, hostname, sizeof(h->domain));
69 0126d91d 2022-09-07 op /* wrapper around dirname(3). dn must be PATH_MAX+1 at least. */
71 0126d91d 2022-09-07 op pdirname(const char *path, char *dn)
73 0126d91d 2022-09-07 op char p[PATH_MAX+1];
76 0126d91d 2022-09-07 op strlcpy(p, path, sizeof(p));
77 0126d91d 2022-09-07 op t = dirname(p);
78 0126d91d 2022-09-07 op memmove(dn, t, strlen(t)+1);
82 0126d91d 2022-09-07 op mkdirs(const char *path, mode_t mode)
84 0126d91d 2022-09-07 op char dname[PATH_MAX+1];
86 0126d91d 2022-09-07 op pdirname(path, dname);
87 0126d91d 2022-09-07 op if (!strcmp(dname, "/"))
89 0126d91d 2022-09-07 op mkdirs(dname, mode);
90 0126d91d 2022-09-07 op if (mkdir(path, mode) != 0 && errno != EEXIST)
91 df5058c9 2023-06-05 op fatal("can't mkdir %s", path);
94 0126d91d 2022-09-07 op /* $XDG_DATA_HOME/gmid */
96 0126d91d 2022-09-07 op data_dir(void)
98 0126d91d 2022-09-07 op const char *home, *xdg;
101 0126d91d 2022-09-07 op if ((xdg = getenv("XDG_DATA_HOME")) == NULL) {
102 0126d91d 2022-09-07 op if ((home = getenv("HOME")) == NULL)
103 0126d91d 2022-09-07 op errx(1, "XDG_DATA_HOME and HOME both empty");
104 0126d91d 2022-09-07 op if (asprintf(&t, "%s/.local/share/gmid", home) == -1)
105 0126d91d 2022-09-07 op err(1, "asprintf");
107 0126d91d 2022-09-07 op if (asprintf(&t, "%s/gmid", xdg) == -1)
108 0126d91d 2022-09-07 op err(1, "asprintf");
111 0126d91d 2022-09-07 op mkdirs(t, 0755);
116 0126d91d 2022-09-07 op logger_init(void)
120 0126d91d 2022-09-07 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, p) == -1)
121 0126d91d 2022-09-07 op err(1, "socketpair");
123 0126d91d 2022-09-07 op switch (fork()) {
125 0126d91d 2022-09-07 op err(1, "fork");
128 0126d91d 2022-09-07 op setproctitle("logger");
129 0126d91d 2022-09-07 op imsg_init(&logibuf, p[1]);
130 0126d91d 2022-09-07 op _exit(logger_main(p[1], &logibuf));
133 0126d91d 2022-09-07 op imsg_init(&logibuf, p[0]);
139 070b3295 2023-06-05 op serve(const char *host, int port, const char *dir)
141 0126d91d 2022-09-07 op struct addrinfo hints, *res, *res0;
142 4267093e 2023-06-06 op int r, error, saved_errno, sock = -1;
143 0126d91d 2022-09-07 op const char *cause = NULL;
144 0126d91d 2022-09-07 op char service[32];
146 4267093e 2023-06-06 op r = snprintf(service, sizeof(service), "%d", port);
147 4267093e 2023-06-06 op if (r < 0 || (size_t)r >= sizeof(service))
148 0126d91d 2022-09-07 op fatal("snprintf");
150 0126d91d 2022-09-07 op memset(&hints, 0, sizeof(hints));
151 0126d91d 2022-09-07 op hints.ai_family = AF_UNSPEC;
152 0126d91d 2022-09-07 op hints.ai_socktype = SOCK_STREAM;
153 0126d91d 2022-09-07 op hints.ai_flags = AI_PASSIVE;
154 0126d91d 2022-09-07 op error = getaddrinfo(host, service, &hints, &res0);
156 df5058c9 2023-06-05 op fatalx("%s", gai_strerror(error));
157 0126d91d 2022-09-07 op for (res = res0; res; res = res->ai_next) {
158 0126d91d 2022-09-07 op sock = socket(res->ai_family, res->ai_socktype,
159 0126d91d 2022-09-07 op res->ai_protocol);
160 0126d91d 2022-09-07 op if (sock == -1) {
161 0126d91d 2022-09-07 op cause = "socket";
165 0126d91d 2022-09-07 op if (bind(sock, res->ai_addr, res->ai_addrlen) == -1) {
166 0126d91d 2022-09-07 op cause = "bind";
167 0126d91d 2022-09-07 op saved_errno = errno;
169 0126d91d 2022-09-07 op errno = saved_errno;
173 0126d91d 2022-09-07 op if (listen(sock, 5) == -1)
174 0126d91d 2022-09-07 op fatal("listen");
177 0126d91d 2022-09-07 op * for the time being, we're happy as soon as
178 0126d91d 2022-09-07 op * something binds.
183 0126d91d 2022-09-07 op if (sock == -1)
184 0126d91d 2022-09-07 op fatal("%s", cause);
185 0126d91d 2022-09-07 op freeaddrinfo(res0);
187 0126d91d 2022-09-07 op log_notice(NULL, "serving %s on port %d", dir, port);
188 070b3295 2023-06-05 op return server_main(NULL, sock, -1);
191 0126d91d 2022-09-07 op static __dead void
194 0126d91d 2022-09-07 op fprintf(stderr,
195 c5b4db93 2022-09-10 op "Version: " GE_STRING "\n"
196 0126d91d 2022-09-07 op "Usage: %s [-hVv] [-d certs-dir] [-H hostname] [-p port] [dir]\n",
197 0126d91d 2022-09-07 op getprogname());
202 0126d91d 2022-09-07 op main(int argc, char **argv)
204 0126d91d 2022-09-07 op struct vhost *host;
205 0126d91d 2022-09-07 op struct location *loc;
206 0126d91d 2022-09-07 op const char *errstr, *certs_dir = NULL, *hostname = "localhost";
207 0126d91d 2022-09-07 op char path[PATH_MAX];
210 0046c1fe 2023-06-06 op setlocale(LC_CTYPE, "");
212 0126d91d 2022-09-07 op logger_init();
213 c68baad2 2023-06-06 op config_init();
215 0126d91d 2022-09-07 op while ((ch = getopt_long(argc, argv, "d:H:hp:Vv", opts, NULL)) != -1) {
216 0126d91d 2022-09-07 op switch (ch) {
218 0126d91d 2022-09-07 op certs_dir = optarg;
221 0126d91d 2022-09-07 op hostname = optarg;
227 0126d91d 2022-09-07 op conf.port = strtonum(optarg, 0, UINT16_MAX, &errstr);
229 df5058c9 2023-06-05 op fatalx("port number is %s: %s", errstr,
233 c5b4db93 2022-09-10 op puts("Version: " GE_STRING);
240 0126d91d 2022-09-07 op argc -= optind;
241 0126d91d 2022-09-07 op argv += optind;
243 0126d91d 2022-09-07 op if (argc > 1)
246 0126d91d 2022-09-07 op /* prepare the configuration */
247 0126d91d 2022-09-07 op conf.verbose = 1;
248 0126d91d 2022-09-07 op init_mime(&conf.mime);
250 0126d91d 2022-09-07 op if (certs_dir == NULL)
251 0126d91d 2022-09-07 op certs_dir = data_dir();
253 0126d91d 2022-09-07 op /* set up the implicit vhost and location */
255 0126d91d 2022-09-07 op host = xcalloc(1, sizeof(*host));
256 0126d91d 2022-09-07 op TAILQ_INSERT_HEAD(&hosts, host, vhosts);
258 0126d91d 2022-09-07 op loc = xcalloc(1, sizeof(*loc));
259 0126d91d 2022-09-07 op loc->fcgi = -1;
260 0126d91d 2022-09-07 op TAILQ_INSERT_HEAD(&host->locations, loc, locations);
262 0126d91d 2022-09-07 op load_local_cert(host, hostname, certs_dir);
264 534afd0d 2022-10-05 op strlcpy(host->domain, "*", sizeof(host->domain));
265 0126d91d 2022-09-07 op loc->auto_index = 1;
266 534afd0d 2022-10-05 op strlcpy(loc->match, "*", sizeof(loc->match));
268 0126d91d 2022-09-07 op if (*argv == NULL) {
269 0126d91d 2022-09-07 op if (getcwd(path, sizeof(path)) == NULL)
270 0126d91d 2022-09-07 op fatal("getcwd");
271 534afd0d 2022-10-05 op strlcpy(loc->dir, path, sizeof(loc->dir));
275 534afd0d 2022-10-05 op tmp = absolutify_path(*argv);
276 534afd0d 2022-10-05 op strlcpy(loc->dir, tmp, sizeof(loc->dir));
280 0126d91d 2022-09-07 op /* start the server */
281 0126d91d 2022-09-07 op signal(SIGPIPE, SIG_IGN);
282 0126d91d 2022-09-07 op setproctitle("%s", loc->dir);
283 070b3295 2023-06-05 op return serve(hostname, conf.port, loc->dir);