2 eac9287d 2023-06-24 op * Copyright (c) 2022, 2023 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 eae52ad4 2023-06-06 op #include <syslog.h>
30 0126d91d 2022-09-07 op #include <unistd.h>
31 ddbcd3c1 2023-08-07 op #include <vis.h>
33 eae52ad4 2023-06-06 op #include "log.h"
35 797c4609 2023-06-08 op int privsep_process;
37 0126d91d 2022-09-07 op static const struct option opts[] = {
38 0126d91d 2022-09-07 op {"help", no_argument, NULL, 'h'},
39 0126d91d 2022-09-07 op {"version", no_argument, NULL, 'V'},
40 0126d91d 2022-09-07 op {NULL, 0, NULL, 0},
44 2c381068 2023-07-01 op log_request(struct client *c, int code, const char *meta)
46 ed164e72 2023-06-26 op char b[GEMINI_URL_LEN];
47 ddbcd3c1 2023-08-07 op char cntmp[64], cn[64] = "-";
48 857635a1 2023-07-25 op char rfc3339[32];
49 797c4609 2023-06-08 op const char *t;
50 857635a1 2023-07-25 op struct tm *tm;
53 857635a1 2023-07-25 op if ((now = time(NULL)) == -1)
54 857635a1 2023-07-25 op fatal("time");
55 857635a1 2023-07-25 op if ((tm = localtime(&now)) == NULL)
56 857635a1 2023-07-25 op fatal("localtime");
57 857635a1 2023-07-25 op if (strftime(rfc3339, sizeof(rfc3339), "%FT%T%z", tm) == 0)
58 857635a1 2023-07-25 op fatal("strftime");
60 797c4609 2023-06-08 op if (c->iri.schema != NULL) {
61 797c4609 2023-06-08 op /* serialize the IRI */
62 797c4609 2023-06-08 op strlcpy(b, c->iri.schema, sizeof(b));
63 797c4609 2023-06-08 op strlcat(b, "://", sizeof(b));
65 797c4609 2023-06-08 op /* log the decoded host name, but if it was invalid
66 797c4609 2023-06-08 op * use the raw one. */
67 797c4609 2023-06-08 op if (*c->domain != '\0')
68 797c4609 2023-06-08 op strlcat(b, c->domain, sizeof(b));
70 797c4609 2023-06-08 op strlcat(b, c->iri.host, sizeof(b));
72 797c4609 2023-06-08 op if (*c->iri.path != '/')
73 797c4609 2023-06-08 op strlcat(b, "/", sizeof(b));
74 797c4609 2023-06-08 op strlcat(b, c->iri.path, sizeof(b)); /* TODO: sanitize UTF8 */
75 797c4609 2023-06-08 op if (*c->iri.query != '\0') { /* TODO: sanitize UTF8 */
76 797c4609 2023-06-08 op strlcat(b, "?", sizeof(b));
77 797c4609 2023-06-08 op strlcat(b, c->iri.query, sizeof(b));
80 797c4609 2023-06-08 op if ((t = c->req) == NULL)
82 797c4609 2023-06-08 op strlcpy(b, t, sizeof(b));
85 ddbcd3c1 2023-08-07 op if (tls_peer_cert_provided(c->ctx)) {
86 ddbcd3c1 2023-08-07 op const char *subj;
89 ddbcd3c1 2023-08-07 op subj = tls_peer_cert_subject(c->ctx);
90 ddbcd3c1 2023-08-07 op if ((n = strstr(subj, "/CN=")) != NULL) {
91 ddbcd3c1 2023-08-07 op strlcpy(cntmp, subj + 4, sizeof(cntmp));
92 ddbcd3c1 2023-08-07 op if ((n = strchr(cntmp, '/')) != NULL)
94 ddbcd3c1 2023-08-07 op strnvis(cn, cntmp, sizeof(cn), VIS_WHITE|VIS_DQ);
98 ddbcd3c1 2023-08-07 op fprintf(stderr, "%s %s %s %s %s 0 %d %s\n", rfc3339, c->rhost, cn,
99 ddbcd3c1 2023-08-07 op *c->domain == '\0' ? c->iri.host : c->domain, b, code, meta);
103 0126d91d 2022-09-07 op load_local_cert(struct vhost *h, const char *hostname, const char *dir)
105 0126d91d 2022-09-07 op char *cert, *key;
107 f31289a8 2023-08-29 op if (asprintf(&cert, "%s/%s.pem", dir, hostname) == -1)
108 2dd5994a 2023-06-06 op fatal("asprintf");
109 f31289a8 2023-08-29 op if (asprintf(&key, "%s/%s.key", dir, hostname) == -1)
110 2dd5994a 2023-06-06 op fatal("asprintf");
112 0126d91d 2022-09-07 op if (access(cert, R_OK) == -1 || access(key, R_OK) == -1)
113 0126d91d 2022-09-07 op gen_certificate(hostname, cert, key);
115 797c4609 2023-06-08 op h->cert = tls_load_file(cert, &h->certlen, NULL);
116 797c4609 2023-06-08 op if (h->cert == NULL)
117 797c4609 2023-06-08 op fatal("can't load %s", cert);
119 797c4609 2023-06-08 op h->key = tls_load_file(key, &h->keylen, NULL);
120 797c4609 2023-06-08 op if (h->key == NULL)
121 797c4609 2023-06-08 op fatal("can't load %s", key);
123 534afd0d 2022-10-05 op strlcpy(h->domain, hostname, sizeof(h->domain));
126 0126d91d 2022-09-07 op /* wrapper around dirname(3). dn must be PATH_MAX+1 at least. */
128 0126d91d 2022-09-07 op pdirname(const char *path, char *dn)
130 0126d91d 2022-09-07 op char p[PATH_MAX+1];
133 0126d91d 2022-09-07 op strlcpy(p, path, sizeof(p));
134 0126d91d 2022-09-07 op t = dirname(p);
135 0126d91d 2022-09-07 op memmove(dn, t, strlen(t)+1);
139 0126d91d 2022-09-07 op mkdirs(const char *path, mode_t mode)
141 0126d91d 2022-09-07 op char dname[PATH_MAX+1];
143 0126d91d 2022-09-07 op pdirname(path, dname);
144 0126d91d 2022-09-07 op if (!strcmp(dname, "/"))
146 0126d91d 2022-09-07 op mkdirs(dname, mode);
147 0126d91d 2022-09-07 op if (mkdir(path, mode) != 0 && errno != EEXIST)
148 df5058c9 2023-06-05 op fatal("can't mkdir %s", path);
151 0126d91d 2022-09-07 op /* $XDG_DATA_HOME/gmid */
153 0126d91d 2022-09-07 op data_dir(void)
155 0126d91d 2022-09-07 op const char *home, *xdg;
158 0126d91d 2022-09-07 op if ((xdg = getenv("XDG_DATA_HOME")) == NULL) {
159 0126d91d 2022-09-07 op if ((home = getenv("HOME")) == NULL)
160 2dd5994a 2023-06-06 op fatalx("XDG_DATA_HOME and HOME both empty");
161 0126d91d 2022-09-07 op if (asprintf(&t, "%s/.local/share/gmid", home) == -1)
162 2dd5994a 2023-06-06 op fatalx("asprintf");
164 0126d91d 2022-09-07 op if (asprintf(&t, "%s/gmid", xdg) == -1)
165 2dd5994a 2023-06-06 op fatal("asprintf");
168 0126d91d 2022-09-07 op mkdirs(t, 0755);
173 af1dab18 2023-06-09 op serve(struct conf *conf, const char *host, int port, const char *dir)
175 0126d91d 2022-09-07 op struct addrinfo hints, *res, *res0;
176 509d0509 2023-06-23 op struct vhost *vh = TAILQ_FIRST(&conf->hosts);
177 509d0509 2023-06-23 op struct address *addr, *acp;
178 4267093e 2023-06-06 op int r, error, saved_errno, sock = -1;
179 0126d91d 2022-09-07 op const char *cause = NULL;
180 0126d91d 2022-09-07 op char service[32];
183 509d0509 2023-06-23 op event_init();
185 4267093e 2023-06-06 op r = snprintf(service, sizeof(service), "%d", port);
186 4267093e 2023-06-06 op if (r < 0 || (size_t)r >= sizeof(service))
187 0126d91d 2022-09-07 op fatal("snprintf");
189 0126d91d 2022-09-07 op memset(&hints, 0, sizeof(hints));
190 0126d91d 2022-09-07 op hints.ai_family = AF_UNSPEC;
191 0126d91d 2022-09-07 op hints.ai_socktype = SOCK_STREAM;
192 0126d91d 2022-09-07 op hints.ai_flags = AI_PASSIVE;
193 0126d91d 2022-09-07 op error = getaddrinfo(host, service, &hints, &res0);
195 df5058c9 2023-06-05 op fatalx("%s", gai_strerror(error));
196 0126d91d 2022-09-07 op for (res = res0; res; res = res->ai_next) {
197 0126d91d 2022-09-07 op sock = socket(res->ai_family, res->ai_socktype,
198 0126d91d 2022-09-07 op res->ai_protocol);
199 0126d91d 2022-09-07 op if (sock == -1) {
200 0126d91d 2022-09-07 op cause = "socket";
204 0126d91d 2022-09-07 op if (bind(sock, res->ai_addr, res->ai_addrlen) == -1) {
205 0126d91d 2022-09-07 op cause = "bind";
206 0126d91d 2022-09-07 op saved_errno = errno;
208 0126d91d 2022-09-07 op errno = saved_errno;
212 0126d91d 2022-09-07 op if (listen(sock, 5) == -1)
213 0126d91d 2022-09-07 op fatal("listen");
217 509d0509 2023-06-23 op addr = xcalloc(1, sizeof(*addr));
218 509d0509 2023-06-23 op addr->ai_flags = res->ai_flags;
219 509d0509 2023-06-23 op addr->ai_family = res->ai_family;
220 509d0509 2023-06-23 op addr->ai_socktype = res->ai_socktype;
221 509d0509 2023-06-23 op addr->ai_protocol = res->ai_protocol;
222 509d0509 2023-06-23 op addr->slen = res->ai_addrlen;
223 509d0509 2023-06-23 op memcpy(&addr->ss, res->ai_addr, res->ai_addrlen);
225 509d0509 2023-06-23 op addr->conf = conf;
226 509d0509 2023-06-23 op addr->sock = sock;
227 509d0509 2023-06-23 op event_set(&addr->evsock, addr->sock, EV_READ|EV_PERSIST,
228 71b02f63 2023-07-01 op server_accept, addr);
230 e50f85ad 2023-06-24 op if ((addr->ctx = tls_server()) == NULL)
231 e50f85ad 2023-06-24 op fatal("tls_server failure");
233 509d0509 2023-06-23 op TAILQ_INSERT_HEAD(&conf->addrs, addr, addrs);
235 509d0509 2023-06-23 op acp = xcalloc(1, sizeof(*acp));
236 509d0509 2023-06-23 op memcpy(acp, addr, sizeof(*acp));
237 509d0509 2023-06-23 op acp->sock = -1;
238 509d0509 2023-06-23 op memset(&acp->evsock, 0, sizeof(acp->evsock));
239 509d0509 2023-06-23 op TAILQ_INSERT_HEAD(&vh->addrs, addr, addrs);
243 0126d91d 2022-09-07 op fatal("%s", cause);
244 0126d91d 2022-09-07 op freeaddrinfo(res0);
246 797c4609 2023-06-08 op server_init(NULL, NULL, NULL);
247 af1dab18 2023-06-09 op if (server_configure_done(conf) == -1)
248 797c4609 2023-06-08 op fatalx("server configuration failed");
250 eae52ad4 2023-06-06 op log_info("serving %s on port %d", dir, port);
251 797c4609 2023-06-08 op event_dispatch();
252 797c4609 2023-06-08 op log_info("quitting");
256 0126d91d 2022-09-07 op static __dead void
259 0126d91d 2022-09-07 op fprintf(stderr,
260 c5b4db93 2022-09-10 op "Version: " GE_STRING "\n"
261 0126d91d 2022-09-07 op "Usage: %s [-hVv] [-d certs-dir] [-H hostname] [-p port] [dir]\n",
262 0126d91d 2022-09-07 op getprogname());
267 0126d91d 2022-09-07 op main(int argc, char **argv)
269 af1dab18 2023-06-09 op struct conf *conf;
270 0126d91d 2022-09-07 op struct vhost *host;
271 0126d91d 2022-09-07 op struct location *loc;
272 0126d91d 2022-09-07 op const char *errstr, *certs_dir = NULL, *hostname = "localhost";
273 0126d91d 2022-09-07 op char path[PATH_MAX];
274 509d0509 2023-06-23 op int ch, port = 1965;
276 0046c1fe 2023-06-06 op setlocale(LC_CTYPE, "");
278 eae52ad4 2023-06-06 op log_init(1, LOG_DAEMON);
279 eae52ad4 2023-06-06 op log_setverbose(0);
280 af1dab18 2023-06-09 op conf = config_new();
282 ba290ef3 2023-06-11 op /* ge doesn't do privsep so no privsep crypto engine. */
283 ba290ef3 2023-06-11 op conf->use_privsep_crypto = 0;
285 0126d91d 2022-09-07 op while ((ch = getopt_long(argc, argv, "d:H:hp:Vv", opts, NULL)) != -1) {
286 0126d91d 2022-09-07 op switch (ch) {
288 0126d91d 2022-09-07 op certs_dir = optarg;
291 0126d91d 2022-09-07 op hostname = optarg;
297 509d0509 2023-06-23 op port = strtonum(optarg, 0, UINT16_MAX, &errstr);
299 df5058c9 2023-06-05 op fatalx("port number is %s: %s", errstr,
303 c5b4db93 2022-09-10 op puts("Version: " GE_STRING);
310 0126d91d 2022-09-07 op argc -= optind;
311 0126d91d 2022-09-07 op argv += optind;
313 0126d91d 2022-09-07 op if (argc > 1)
316 0126d91d 2022-09-07 op /* prepare the configuration */
317 af1dab18 2023-06-09 op init_mime(&conf->mime);
319 0126d91d 2022-09-07 op if (certs_dir == NULL)
320 0126d91d 2022-09-07 op certs_dir = data_dir();
322 0126d91d 2022-09-07 op /* set up the implicit vhost and location */
323 0126d91d 2022-09-07 op host = xcalloc(1, sizeof(*host));
324 af1dab18 2023-06-09 op TAILQ_INSERT_HEAD(&conf->hosts, host, vhosts);
326 0126d91d 2022-09-07 op loc = xcalloc(1, sizeof(*loc));
327 0126d91d 2022-09-07 op loc->fcgi = -1;
328 0126d91d 2022-09-07 op TAILQ_INSERT_HEAD(&host->locations, loc, locations);
330 0126d91d 2022-09-07 op load_local_cert(host, hostname, certs_dir);
332 534afd0d 2022-10-05 op strlcpy(host->domain, "*", sizeof(host->domain));
333 0126d91d 2022-09-07 op loc->auto_index = 1;
334 534afd0d 2022-10-05 op strlcpy(loc->match, "*", sizeof(loc->match));
336 0126d91d 2022-09-07 op if (*argv == NULL) {
337 0126d91d 2022-09-07 op if (getcwd(path, sizeof(path)) == NULL)
338 0126d91d 2022-09-07 op fatal("getcwd");
339 534afd0d 2022-10-05 op strlcpy(loc->dir, path, sizeof(loc->dir));
343 534afd0d 2022-10-05 op tmp = absolutify_path(*argv);
344 534afd0d 2022-10-05 op strlcpy(loc->dir, tmp, sizeof(loc->dir));
348 0126d91d 2022-09-07 op /* start the server */
349 0126d91d 2022-09-07 op signal(SIGPIPE, SIG_IGN);
350 0126d91d 2022-09-07 op setproctitle("%s", loc->dir);
351 509d0509 2023-06-23 op return serve(conf, hostname, port, loc->dir);