Blame


1 0126d91d 2022-09-07 op /*
2 eac9287d 2023-06-24 op * Copyright (c) 2022, 2023 Omar Polo <op@omarpolo.com>
3 0126d91d 2022-09-07 op *
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.
7 0126d91d 2022-09-07 op *
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.
15 0126d91d 2022-09-07 op */
16 0126d91d 2022-09-07 op
17 0126d91d 2022-09-07 op #include "gmid.h"
18 0126d91d 2022-09-07 op
19 0126d91d 2022-09-07 op #include <sys/stat.h>
20 0126d91d 2022-09-07 op #include <sys/wait.h>
21 0126d91d 2022-09-07 op
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>
32 0126d91d 2022-09-07 op
33 eae52ad4 2023-06-06 op #include "log.h"
34 df5058c9 2023-06-05 op
35 bab32701 2023-10-18 op static int gen_eckey = 1;
36 bab32701 2023-10-18 op
37 797c4609 2023-06-08 op int privsep_process;
38 0126d91d 2022-09-07 op
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},
43 0126d91d 2022-09-07 op };
44 797c4609 2023-06-08 op
45 797c4609 2023-06-08 op void
46 2c381068 2023-07-01 op log_request(struct client *c, int code, const char *meta)
47 797c4609 2023-06-08 op {
48 ed164e72 2023-06-26 op char b[GEMINI_URL_LEN];
49 ddbcd3c1 2023-08-07 op char cntmp[64], cn[64] = "-";
50 797c4609 2023-06-08 op const char *t;
51 857635a1 2023-07-25 op
52 797c4609 2023-06-08 op if (c->iri.schema != NULL) {
53 797c4609 2023-06-08 op /* serialize the IRI */
54 797c4609 2023-06-08 op strlcpy(b, c->iri.schema, sizeof(b));
55 797c4609 2023-06-08 op strlcat(b, "://", sizeof(b));
56 797c4609 2023-06-08 op
57 797c4609 2023-06-08 op /* log the decoded host name, but if it was invalid
58 797c4609 2023-06-08 op * use the raw one. */
59 797c4609 2023-06-08 op if (*c->domain != '\0')
60 797c4609 2023-06-08 op strlcat(b, c->domain, sizeof(b));
61 797c4609 2023-06-08 op else
62 797c4609 2023-06-08 op strlcat(b, c->iri.host, sizeof(b));
63 797c4609 2023-06-08 op
64 797c4609 2023-06-08 op if (*c->iri.path != '/')
65 797c4609 2023-06-08 op strlcat(b, "/", sizeof(b));
66 797c4609 2023-06-08 op strlcat(b, c->iri.path, sizeof(b)); /* TODO: sanitize UTF8 */
67 797c4609 2023-06-08 op if (*c->iri.query != '\0') { /* TODO: sanitize UTF8 */
68 797c4609 2023-06-08 op strlcat(b, "?", sizeof(b));
69 797c4609 2023-06-08 op strlcat(b, c->iri.query, sizeof(b));
70 797c4609 2023-06-08 op }
71 797c4609 2023-06-08 op } else {
72 797c4609 2023-06-08 op if ((t = c->req) == NULL)
73 797c4609 2023-06-08 op t = "";
74 797c4609 2023-06-08 op strlcpy(b, t, sizeof(b));
75 797c4609 2023-06-08 op }
76 0126d91d 2022-09-07 op
77 ddbcd3c1 2023-08-07 op if (tls_peer_cert_provided(c->ctx)) {
78 ddbcd3c1 2023-08-07 op const char *subj;
79 ddbcd3c1 2023-08-07 op char *n;
80 ddbcd3c1 2023-08-07 op
81 ddbcd3c1 2023-08-07 op subj = tls_peer_cert_subject(c->ctx);
82 ddbcd3c1 2023-08-07 op if ((n = strstr(subj, "/CN=")) != NULL) {
83 ddbcd3c1 2023-08-07 op strlcpy(cntmp, subj + 4, sizeof(cntmp));
84 ddbcd3c1 2023-08-07 op if ((n = strchr(cntmp, '/')) != NULL)
85 ddbcd3c1 2023-08-07 op *n = '\0';
86 40103011 2024-06-08 op strnvis(cn, cntmp, sizeof(cn), VIS_WHITE|VIS_DQ);
87 ddbcd3c1 2023-08-07 op }
88 ddbcd3c1 2023-08-07 op }
89 ddbcd3c1 2023-08-07 op
90 96515efd 2023-08-29 op fprintf(stderr, "%s %s %s %s %d %s\n", c->rhost, cn,
91 ddbcd3c1 2023-08-07 op *c->domain == '\0' ? c->iri.host : c->domain, b, code, meta);
92 797c4609 2023-06-08 op }
93 797c4609 2023-06-08 op
94 345a12c7 2024-06-09 op static void
95 0126d91d 2022-09-07 op load_local_cert(struct vhost *h, const char *hostname, const char *dir)
96 0126d91d 2022-09-07 op {
97 0126d91d 2022-09-07 op char *cert, *key;
98 0126d91d 2022-09-07 op
99 f31289a8 2023-08-29 op if (asprintf(&cert, "%s/%s.pem", dir, hostname) == -1)
100 2dd5994a 2023-06-06 op fatal("asprintf");
101 f31289a8 2023-08-29 op if (asprintf(&key, "%s/%s.key", dir, hostname) == -1)
102 2dd5994a 2023-06-06 op fatal("asprintf");
103 0126d91d 2022-09-07 op
104 0126d91d 2022-09-07 op if (access(cert, R_OK) == -1 || access(key, R_OK) == -1)
105 bab32701 2023-10-18 op gencert(hostname, cert, key, gen_eckey);
106 0126d91d 2022-09-07 op
107 797c4609 2023-06-08 op h->cert = tls_load_file(cert, &h->certlen, NULL);
108 797c4609 2023-06-08 op if (h->cert == NULL)
109 797c4609 2023-06-08 op fatal("can't load %s", cert);
110 797c4609 2023-06-08 op
111 797c4609 2023-06-08 op h->key = tls_load_file(key, &h->keylen, NULL);
112 797c4609 2023-06-08 op if (h->key == NULL)
113 797c4609 2023-06-08 op fatal("can't load %s", key);
114 797c4609 2023-06-08 op
115 68d36b20 2024-06-09 op if (strlcpy(h->domain, hostname, sizeof(h->domain))
116 68d36b20 2024-06-09 op >= sizeof(h->domain))
117 68d36b20 2024-06-09 op fatalx("hostname too long: %s", hostname);
118 0126d91d 2022-09-07 op }
119 0126d91d 2022-09-07 op
120 0126d91d 2022-09-07 op /* wrapper around dirname(3). dn must be PATH_MAX+1 at least. */
121 0126d91d 2022-09-07 op static void
122 0126d91d 2022-09-07 op pdirname(const char *path, char *dn)
123 0126d91d 2022-09-07 op {
124 0126d91d 2022-09-07 op char p[PATH_MAX+1];
125 0126d91d 2022-09-07 op char *t;
126 0126d91d 2022-09-07 op
127 68d36b20 2024-06-09 op if (strlcpy(p, path, sizeof(p)) >= sizeof(p))
128 68d36b20 2024-06-09 op fatalx("%s: path too long: %s", __func__, path);
129 0126d91d 2022-09-07 op t = dirname(p);
130 0126d91d 2022-09-07 op memmove(dn, t, strlen(t)+1);
131 0126d91d 2022-09-07 op }
132 0126d91d 2022-09-07 op
133 0126d91d 2022-09-07 op static void
134 0126d91d 2022-09-07 op mkdirs(const char *path, mode_t mode)
135 0126d91d 2022-09-07 op {
136 0126d91d 2022-09-07 op char dname[PATH_MAX+1];
137 0126d91d 2022-09-07 op
138 0126d91d 2022-09-07 op pdirname(path, dname);
139 0126d91d 2022-09-07 op if (!strcmp(dname, "/"))
140 0126d91d 2022-09-07 op return;
141 0126d91d 2022-09-07 op mkdirs(dname, mode);
142 0126d91d 2022-09-07 op if (mkdir(path, mode) != 0 && errno != EEXIST)
143 df5058c9 2023-06-05 op fatal("can't mkdir %s", path);
144 0126d91d 2022-09-07 op }
145 0126d91d 2022-09-07 op
146 a6d07f09 2023-08-29 op /* $XDG_DATA_HOME/gemexp */
147 345a12c7 2024-06-09 op static char *
148 0126d91d 2022-09-07 op data_dir(void)
149 0126d91d 2022-09-07 op {
150 0126d91d 2022-09-07 op const char *home, *xdg;
151 0126d91d 2022-09-07 op char *t;
152 0126d91d 2022-09-07 op
153 0126d91d 2022-09-07 op if ((xdg = getenv("XDG_DATA_HOME")) == NULL) {
154 0126d91d 2022-09-07 op if ((home = getenv("HOME")) == NULL)
155 2dd5994a 2023-06-06 op fatalx("XDG_DATA_HOME and HOME both empty");
156 a6d07f09 2023-08-29 op if (asprintf(&t, "%s/.local/share/gemexp", home) == -1)
157 2dd5994a 2023-06-06 op fatalx("asprintf");
158 0126d91d 2022-09-07 op } else {
159 a6d07f09 2023-08-29 op if (asprintf(&t, "%s/gemexp", xdg) == -1)
160 2dd5994a 2023-06-06 op fatal("asprintf");
161 0126d91d 2022-09-07 op }
162 0126d91d 2022-09-07 op
163 0126d91d 2022-09-07 op mkdirs(t, 0755);
164 0126d91d 2022-09-07 op return t;
165 0126d91d 2022-09-07 op }
166 0126d91d 2022-09-07 op
167 0126d91d 2022-09-07 op static int
168 af1dab18 2023-06-09 op serve(struct conf *conf, const char *host, int port, const char *dir)
169 0126d91d 2022-09-07 op {
170 0126d91d 2022-09-07 op struct addrinfo hints, *res, *res0;
171 509d0509 2023-06-23 op struct vhost *vh = TAILQ_FIRST(&conf->hosts);
172 509d0509 2023-06-23 op struct address *addr, *acp;
173 4267093e 2023-06-06 op int r, error, saved_errno, sock = -1;
174 0126d91d 2022-09-07 op const char *cause = NULL;
175 0126d91d 2022-09-07 op char service[32];
176 509d0509 2023-06-23 op int any = 0;
177 0126d91d 2022-09-07 op
178 509d0509 2023-06-23 op event_init();
179 509d0509 2023-06-23 op
180 4267093e 2023-06-06 op r = snprintf(service, sizeof(service), "%d", port);
181 4267093e 2023-06-06 op if (r < 0 || (size_t)r >= sizeof(service))
182 0126d91d 2022-09-07 op fatal("snprintf");
183 0126d91d 2022-09-07 op
184 0126d91d 2022-09-07 op memset(&hints, 0, sizeof(hints));
185 0126d91d 2022-09-07 op hints.ai_family = AF_UNSPEC;
186 0126d91d 2022-09-07 op hints.ai_socktype = SOCK_STREAM;
187 0126d91d 2022-09-07 op hints.ai_flags = AI_PASSIVE;
188 0126d91d 2022-09-07 op error = getaddrinfo(host, service, &hints, &res0);
189 0126d91d 2022-09-07 op if (error)
190 df5058c9 2023-06-05 op fatalx("%s", gai_strerror(error));
191 0126d91d 2022-09-07 op for (res = res0; res; res = res->ai_next) {
192 0126d91d 2022-09-07 op sock = socket(res->ai_family, res->ai_socktype,
193 0126d91d 2022-09-07 op res->ai_protocol);
194 0126d91d 2022-09-07 op if (sock == -1) {
195 0126d91d 2022-09-07 op cause = "socket";
196 0126d91d 2022-09-07 op continue;
197 0126d91d 2022-09-07 op }
198 0126d91d 2022-09-07 op
199 0126d91d 2022-09-07 op if (bind(sock, res->ai_addr, res->ai_addrlen) == -1) {
200 0126d91d 2022-09-07 op cause = "bind";
201 0126d91d 2022-09-07 op saved_errno = errno;
202 0126d91d 2022-09-07 op close(sock);
203 0126d91d 2022-09-07 op errno = saved_errno;
204 0126d91d 2022-09-07 op continue;
205 0126d91d 2022-09-07 op }
206 0126d91d 2022-09-07 op
207 0126d91d 2022-09-07 op if (listen(sock, 5) == -1)
208 0126d91d 2022-09-07 op fatal("listen");
209 0126d91d 2022-09-07 op
210 509d0509 2023-06-23 op any = 1;
211 509d0509 2023-06-23 op
212 509d0509 2023-06-23 op addr = xcalloc(1, sizeof(*addr));
213 509d0509 2023-06-23 op addr->ai_flags = res->ai_flags;
214 509d0509 2023-06-23 op addr->ai_family = res->ai_family;
215 509d0509 2023-06-23 op addr->ai_socktype = res->ai_socktype;
216 509d0509 2023-06-23 op addr->ai_protocol = res->ai_protocol;
217 509d0509 2023-06-23 op addr->slen = res->ai_addrlen;
218 509d0509 2023-06-23 op memcpy(&addr->ss, res->ai_addr, res->ai_addrlen);
219 509d0509 2023-06-23 op
220 509d0509 2023-06-23 op addr->conf = conf;
221 509d0509 2023-06-23 op addr->sock = sock;
222 509d0509 2023-06-23 op event_set(&addr->evsock, addr->sock, EV_READ|EV_PERSIST,
223 71b02f63 2023-07-01 op server_accept, addr);
224 509d0509 2023-06-23 op
225 e50f85ad 2023-06-24 op if ((addr->ctx = tls_server()) == NULL)
226 e50f85ad 2023-06-24 op fatal("tls_server failure");
227 e50f85ad 2023-06-24 op
228 509d0509 2023-06-23 op TAILQ_INSERT_HEAD(&conf->addrs, addr, addrs);
229 509d0509 2023-06-23 op
230 509d0509 2023-06-23 op acp = xcalloc(1, sizeof(*acp));
231 509d0509 2023-06-23 op memcpy(acp, addr, sizeof(*acp));
232 509d0509 2023-06-23 op acp->sock = -1;
233 509d0509 2023-06-23 op memset(&acp->evsock, 0, sizeof(acp->evsock));
234 509d0509 2023-06-23 op TAILQ_INSERT_HEAD(&vh->addrs, addr, addrs);
235 0126d91d 2022-09-07 op }
236 0126d91d 2022-09-07 op
237 509d0509 2023-06-23 op if (!any)
238 0126d91d 2022-09-07 op fatal("%s", cause);
239 0126d91d 2022-09-07 op freeaddrinfo(res0);
240 0126d91d 2022-09-07 op
241 797c4609 2023-06-08 op server_init(NULL, NULL, NULL);
242 af1dab18 2023-06-09 op if (server_configure_done(conf) == -1)
243 797c4609 2023-06-08 op fatalx("server configuration failed");
244 797c4609 2023-06-08 op
245 eae52ad4 2023-06-06 op log_info("serving %s on port %d", dir, port);
246 797c4609 2023-06-08 op event_dispatch();
247 797c4609 2023-06-08 op log_info("quitting");
248 797c4609 2023-06-08 op return 0;
249 0126d91d 2022-09-07 op }
250 0126d91d 2022-09-07 op
251 0126d91d 2022-09-07 op static __dead void
252 0126d91d 2022-09-07 op usage(void)
253 0126d91d 2022-09-07 op {
254 0126d91d 2022-09-07 op fprintf(stderr,
255 ac46710a 2024-01-08 op "Version: " GEMEXP_STRING "\n"
256 bab32701 2023-10-18 op "Usage: %s [-hRV] [-d certs-dir] [-H hostname] [-p port] [dir]\n",
257 0126d91d 2022-09-07 op getprogname());
258 0126d91d 2022-09-07 op exit(1);
259 0126d91d 2022-09-07 op }
260 0126d91d 2022-09-07 op
261 0126d91d 2022-09-07 op int
262 0126d91d 2022-09-07 op main(int argc, char **argv)
263 0126d91d 2022-09-07 op {
264 af1dab18 2023-06-09 op struct conf *conf;
265 0126d91d 2022-09-07 op struct vhost *host;
266 0126d91d 2022-09-07 op struct location *loc;
267 0126d91d 2022-09-07 op const char *errstr, *certs_dir = NULL, *hostname = "localhost";
268 0126d91d 2022-09-07 op char path[PATH_MAX];
269 8306bb90 2024-07-02 op int ch, verbose = 0, port = 1965;
270 0126d91d 2022-09-07 op
271 0046c1fe 2023-06-06 op setlocale(LC_CTYPE, "");
272 0046c1fe 2023-06-06 op
273 eae52ad4 2023-06-06 op log_init(1, LOG_DAEMON);
274 af1dab18 2023-06-09 op conf = config_new();
275 0126d91d 2022-09-07 op
276 ba290ef3 2023-06-11 op /* ge doesn't do privsep so no privsep crypto engine. */
277 ba290ef3 2023-06-11 op conf->use_privsep_crypto = 0;
278 ba290ef3 2023-06-11 op
279 8306bb90 2024-07-02 op while ((ch = getopt_long(argc, argv, "d:H:hp:RVv", opts, NULL)) != -1) {
280 0126d91d 2022-09-07 op switch (ch) {
281 0126d91d 2022-09-07 op case 'd':
282 0126d91d 2022-09-07 op certs_dir = optarg;
283 0126d91d 2022-09-07 op break;
284 0126d91d 2022-09-07 op case 'H':
285 0126d91d 2022-09-07 op hostname = optarg;
286 0126d91d 2022-09-07 op break;
287 0126d91d 2022-09-07 op case 'h':
288 0126d91d 2022-09-07 op usage();
289 0126d91d 2022-09-07 op break;
290 0126d91d 2022-09-07 op case 'p':
291 509d0509 2023-06-23 op port = strtonum(optarg, 0, UINT16_MAX, &errstr);
292 0126d91d 2022-09-07 op if (errstr)
293 df5058c9 2023-06-05 op fatalx("port number is %s: %s", errstr,
294 df5058c9 2023-06-05 op optarg);
295 0126d91d 2022-09-07 op break;
296 bab32701 2023-10-18 op case 'R':
297 bab32701 2023-10-18 op gen_eckey = 0;
298 bab32701 2023-10-18 op break;
299 0126d91d 2022-09-07 op case 'V':
300 ac46710a 2024-01-08 op puts("Version: " GEMEXP_STRING);
301 0126d91d 2022-09-07 op return 0;
302 8306bb90 2024-07-02 op case 'v':
303 8306bb90 2024-07-02 op verbose = 1;
304 8306bb90 2024-07-02 op break;
305 0126d91d 2022-09-07 op default:
306 0126d91d 2022-09-07 op usage();
307 0126d91d 2022-09-07 op break;
308 0126d91d 2022-09-07 op }
309 0126d91d 2022-09-07 op }
310 0126d91d 2022-09-07 op argc -= optind;
311 0126d91d 2022-09-07 op argv += optind;
312 0126d91d 2022-09-07 op
313 0126d91d 2022-09-07 op if (argc > 1)
314 0126d91d 2022-09-07 op usage();
315 0126d91d 2022-09-07 op
316 8306bb90 2024-07-02 op log_init(1, LOG_DAEMON);
317 8306bb90 2024-07-02 op log_setverbose(verbose);
318 8306bb90 2024-07-02 op
319 0126d91d 2022-09-07 op /* prepare the configuration */
320 af1dab18 2023-06-09 op init_mime(&conf->mime);
321 0126d91d 2022-09-07 op
322 0126d91d 2022-09-07 op if (certs_dir == NULL)
323 0126d91d 2022-09-07 op certs_dir = data_dir();
324 0126d91d 2022-09-07 op
325 0126d91d 2022-09-07 op /* set up the implicit vhost and location */
326 0126d91d 2022-09-07 op host = xcalloc(1, sizeof(*host));
327 af1dab18 2023-06-09 op TAILQ_INSERT_HEAD(&conf->hosts, host, vhosts);
328 0126d91d 2022-09-07 op
329 0126d91d 2022-09-07 op loc = xcalloc(1, sizeof(*loc));
330 0126d91d 2022-09-07 op loc->fcgi = -1;
331 0126d91d 2022-09-07 op TAILQ_INSERT_HEAD(&host->locations, loc, locations);
332 0126d91d 2022-09-07 op
333 0126d91d 2022-09-07 op load_local_cert(host, hostname, certs_dir);
334 0126d91d 2022-09-07 op
335 534afd0d 2022-10-05 op strlcpy(host->domain, "*", sizeof(host->domain));
336 0126d91d 2022-09-07 op loc->auto_index = 1;
337 534afd0d 2022-10-05 op strlcpy(loc->match, "*", sizeof(loc->match));
338 0126d91d 2022-09-07 op
339 0126d91d 2022-09-07 op if (*argv == NULL) {
340 0126d91d 2022-09-07 op if (getcwd(path, sizeof(path)) == NULL)
341 0126d91d 2022-09-07 op fatal("getcwd");
342 534afd0d 2022-10-05 op strlcpy(loc->dir, path, sizeof(loc->dir));
343 534afd0d 2022-10-05 op } else {
344 534afd0d 2022-10-05 op char *tmp;
345 0126d91d 2022-09-07 op
346 534afd0d 2022-10-05 op tmp = absolutify_path(*argv);
347 534afd0d 2022-10-05 op strlcpy(loc->dir, tmp, sizeof(loc->dir));
348 534afd0d 2022-10-05 op free(tmp);
349 534afd0d 2022-10-05 op }
350 534afd0d 2022-10-05 op
351 0126d91d 2022-09-07 op /* start the server */
352 0126d91d 2022-09-07 op signal(SIGPIPE, SIG_IGN);
353 0126d91d 2022-09-07 op setproctitle("%s", loc->dir);
354 509d0509 2023-06-23 op return serve(conf, hostname, port, loc->dir);
355 0126d91d 2022-09-07 op }