Blob


1 /*
2 * Copyright (c) 2022 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 "gmid.h"
19 #include <sys/stat.h>
20 #include <sys/wait.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <getopt.h>
25 #include <libgen.h>
26 #include <signal.h>
27 #include <string.h>
28 #include <unistd.h>
30 struct imsgbuf ibuf, logibuf;
31 struct conf conf;
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'},
40 {NULL, 0, NULL, 0},
41 };
43 void
44 load_local_cert(struct vhost *h, const char *hostname, const char *dir)
45 {
46 char *cert, *key;
48 if (asprintf(&cert, "%s/%s.cert.pem", dir, hostname) == -1)
49 errx(1, "asprintf");
50 if (asprintf(&key, "%s/%s.key.pem", dir, hostname) == -1)
51 errx(1, "asprintf");
53 if (access(cert, R_OK) == -1 || access(key, R_OK) == -1)
54 gen_certificate(hostname, cert, key);
56 h->cert = cert;
57 h->key = key;
58 h->domain = hostname;
59 }
61 /* wrapper around dirname(3). dn must be PATH_MAX+1 at least. */
62 static void
63 pdirname(const char *path, char *dn)
64 {
65 char p[PATH_MAX+1];
66 char *t;
68 strlcpy(p, path, sizeof(p));
69 t = dirname(p);
70 memmove(dn, t, strlen(t)+1);
71 }
73 static void
74 mkdirs(const char *path, mode_t mode)
75 {
76 char dname[PATH_MAX+1];
78 pdirname(path, dname);
79 if (!strcmp(dname, "/"))
80 return;
81 mkdirs(dname, mode);
82 if (mkdir(path, mode) != 0 && errno != EEXIST)
83 fatal("can't mkdir %s: %s", path, strerror(errno));
84 }
86 /* $XDG_DATA_HOME/gmid */
87 char *
88 data_dir(void)
89 {
90 const char *home, *xdg;
91 char *t;
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)
97 err(1, "asprintf");
98 } else {
99 if (asprintf(&t, "%s/gmid", xdg) == -1)
100 err(1, "asprintf");
103 mkdirs(t, 0755);
104 return t;
107 static void
108 logger_init(void)
110 int p[2];
112 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, p) == -1)
113 err(1, "socketpair");
115 switch (fork()) {
116 case -1:
117 err(1, "fork");
118 case 0:
119 close(p[0]);
120 setproctitle("logger");
121 imsg_init(&logibuf, p[1]);
122 _exit(logger_main(p[1], &logibuf));
123 default:
124 close(p[1]);
125 imsg_init(&logibuf, p[0]);
126 return;
130 static int
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;
136 char service[32];
138 if (snprintf(service, sizeof(service), "%d", port) < 0)
139 fatal("snprintf");
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);
146 if (error)
147 fatal("%s", gai_strerror(error));
148 for (res = res0; res; res = res->ai_next) {
149 sock = socket(res->ai_family, res->ai_socktype,
150 res->ai_protocol);
151 if (sock == -1) {
152 cause = "socket";
153 continue;
156 if (bind(sock, res->ai_addr, res->ai_addrlen) == -1) {
157 cause = "bind";
158 saved_errno = errno;
159 close(sock);
160 errno = saved_errno;
161 continue;
164 if (listen(sock, 5) == -1)
165 fatal("listen");
167 /*
168 * for the time being, we're happy as soon as
169 * something binds.
170 */
171 break;
174 if (sock == -1)
175 fatal("%s", cause);
176 freeaddrinfo(res0);
178 log_notice(NULL, "serving %s on port %d", dir, port);
179 loop(ctx, sock, -1, NULL);
180 return 0;
183 static __dead void
184 usage(void)
186 fprintf(stderr,
187 "Version: " GMID_STRING "\n"
188 "Usage: %s [-hVv] [-d certs-dir] [-H hostname] [-p port] [dir]\n",
189 getprogname());
190 exit(1);
193 int
194 main(int argc, char **argv)
196 struct tls_config *tlsconf;
197 struct tls *ctx;
198 struct vhost *host;
199 struct location *loc;
200 const char *errstr, *certs_dir = NULL, *hostname = "localhost";
201 char path[PATH_MAX];
202 int ch;
204 logger_init();
205 conf.port = 1965;
207 while ((ch = getopt_long(argc, argv, "d:H:hp:Vv", opts, NULL)) != -1) {
208 switch (ch) {
209 case 'd':
210 certs_dir = optarg;
211 break;
212 case 'H':
213 hostname = optarg;
214 break;
215 case 'h':
216 usage();
217 break;
218 case 'p':
219 conf.port = strtonum(optarg, 0, UINT16_MAX, &errstr);
220 if (errstr)
221 fatal("port number is %s: %s", errstr, optarg);
222 break;
223 case 'V':
224 puts("Version: " GMID_STRING);
225 return 0;
226 default:
227 usage();
228 break;
231 argc -= optind;
232 argv += optind;
234 if (argc > 1)
235 usage();
237 /* prepare the configuration */
238 conf.verbose = 1;
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));
254 loc->fcgi = -1;
255 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
257 load_local_cert(host, hostname, certs_dir);
259 host->domain = "*";
260 loc->auto_index = 1;
261 loc->match = "*";
263 if (*argv == NULL) {
264 if (getcwd(path, sizeof(path)) == NULL)
265 fatal("getcwd");
266 loc->dir = path;
267 } else
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);
273 /* setup tls */
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);