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 <locale.h>
26 #include <libgen.h>
27 #include <signal.h>
28 #include <string.h>
29 #include <syslog.h>
30 #include <unistd.h>
32 #include "log.h"
34 struct conf conf;
35 int privsep_process;
37 struct vhosthead hosts = TAILQ_HEAD_INITIALIZER(hosts);
39 static const struct option opts[] = {
40 {"help", no_argument, NULL, 'h'},
41 {"version", no_argument, NULL, 'V'},
42 {NULL, 0, NULL, 0},
43 };
45 void
46 log_request(struct client *c, char *meta, size_t l)
47 {
48 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV], b[GEMINI_URL_LEN];
49 const char *t;
50 size_t len;
51 int ec;
53 len = sizeof(c->addr);
54 ec = getnameinfo((struct sockaddr*)&c->addr, len,
55 hbuf, sizeof(hbuf),
56 sbuf, sizeof(sbuf),
57 NI_NUMERICHOST | NI_NUMERICSERV);
58 if (ec != 0)
59 fatalx("getnameinfo: %s", gai_strerror(ec));
61 if (c->iri.schema != NULL) {
62 /* serialize the IRI */
63 strlcpy(b, c->iri.schema, sizeof(b));
64 strlcat(b, "://", sizeof(b));
66 /* log the decoded host name, but if it was invalid
67 * use the raw one. */
68 if (*c->domain != '\0')
69 strlcat(b, c->domain, sizeof(b));
70 else
71 strlcat(b, c->iri.host, sizeof(b));
73 if (*c->iri.path != '/')
74 strlcat(b, "/", sizeof(b));
75 strlcat(b, c->iri.path, sizeof(b)); /* TODO: sanitize UTF8 */
76 if (*c->iri.query != '\0') { /* TODO: sanitize UTF8 */
77 strlcat(b, "?", sizeof(b));
78 strlcat(b, c->iri.query, sizeof(b));
79 }
80 } else {
81 if ((t = c->req) == NULL)
82 t = "";
83 strlcpy(b, t, sizeof(b));
84 }
86 if ((t = memchr(meta, '\r', l)) == NULL)
87 t = meta + len;
89 fprintf(stderr, "%s:%s GET %s %.*s\n", hbuf, sbuf, b,
90 (int)(t-meta), meta);
91 }
93 void
94 load_local_cert(struct vhost *h, const char *hostname, const char *dir)
95 {
96 char *cert, *key;
98 if (asprintf(&cert, "%s/%s.cert.pem", dir, hostname) == -1)
99 fatal("asprintf");
100 if (asprintf(&key, "%s/%s.key.pem", dir, hostname) == -1)
101 fatal("asprintf");
103 if (access(cert, R_OK) == -1 || access(key, R_OK) == -1)
104 gen_certificate(hostname, cert, key);
106 h->cert = tls_load_file(cert, &h->certlen, NULL);
107 if (h->cert == NULL)
108 fatal("can't load %s", cert);
110 h->key = tls_load_file(key, &h->keylen, NULL);
111 if (h->key == NULL)
112 fatal("can't load %s", key);
114 strlcpy(h->domain, hostname, sizeof(h->domain));
117 /* wrapper around dirname(3). dn must be PATH_MAX+1 at least. */
118 static void
119 pdirname(const char *path, char *dn)
121 char p[PATH_MAX+1];
122 char *t;
124 strlcpy(p, path, sizeof(p));
125 t = dirname(p);
126 memmove(dn, t, strlen(t)+1);
129 static void
130 mkdirs(const char *path, mode_t mode)
132 char dname[PATH_MAX+1];
134 pdirname(path, dname);
135 if (!strcmp(dname, "/"))
136 return;
137 mkdirs(dname, mode);
138 if (mkdir(path, mode) != 0 && errno != EEXIST)
139 fatal("can't mkdir %s", path);
142 /* $XDG_DATA_HOME/gmid */
143 char *
144 data_dir(void)
146 const char *home, *xdg;
147 char *t;
149 if ((xdg = getenv("XDG_DATA_HOME")) == NULL) {
150 if ((home = getenv("HOME")) == NULL)
151 fatalx("XDG_DATA_HOME and HOME both empty");
152 if (asprintf(&t, "%s/.local/share/gmid", home) == -1)
153 fatalx("asprintf");
154 } else {
155 if (asprintf(&t, "%s/gmid", xdg) == -1)
156 fatal("asprintf");
159 mkdirs(t, 0755);
160 return t;
163 static int
164 serve(const char *host, int port, const char *dir)
166 struct addrinfo hints, *res, *res0;
167 int r, error, saved_errno, sock = -1;
168 const char *cause = NULL;
169 char service[32];
171 r = snprintf(service, sizeof(service), "%d", port);
172 if (r < 0 || (size_t)r >= sizeof(service))
173 fatal("snprintf");
175 memset(&hints, 0, sizeof(hints));
176 hints.ai_family = AF_UNSPEC;
177 hints.ai_socktype = SOCK_STREAM;
178 hints.ai_flags = AI_PASSIVE;
179 error = getaddrinfo(host, service, &hints, &res0);
180 if (error)
181 fatalx("%s", gai_strerror(error));
182 for (res = res0; res; res = res->ai_next) {
183 sock = socket(res->ai_family, res->ai_socktype,
184 res->ai_protocol);
185 if (sock == -1) {
186 cause = "socket";
187 continue;
190 if (bind(sock, res->ai_addr, res->ai_addrlen) == -1) {
191 cause = "bind";
192 saved_errno = errno;
193 close(sock);
194 errno = saved_errno;
195 continue;
198 if (listen(sock, 5) == -1)
199 fatal("listen");
201 /*
202 * for the time being, we're happy as soon as
203 * something binds.
204 */
205 break;
208 if (sock == -1)
209 fatal("%s", cause);
210 freeaddrinfo(res0);
212 event_init();
214 /* cheating */
215 conf.sock4 = sock;
216 event_set(&conf.evsock4, conf.sock4, EV_READ|EV_PERSIST,
217 do_accept, NULL);
219 server_init(NULL, NULL, NULL);
220 if (server_configure_done(&conf) == -1)
221 fatalx("server configuration failed");
223 log_info("serving %s on port %d", dir, port);
224 event_dispatch();
225 log_info("quitting");
226 return 0;
229 static __dead void
230 usage(void)
232 fprintf(stderr,
233 "Version: " GE_STRING "\n"
234 "Usage: %s [-hVv] [-d certs-dir] [-H hostname] [-p port] [dir]\n",
235 getprogname());
236 exit(1);
239 int
240 main(int argc, char **argv)
242 struct vhost *host;
243 struct location *loc;
244 const char *errstr, *certs_dir = NULL, *hostname = "localhost";
245 char path[PATH_MAX];
246 int ch;
248 setlocale(LC_CTYPE, "");
250 log_init(1, LOG_DAEMON);
251 log_setverbose(0);
252 config_init();
254 while ((ch = getopt_long(argc, argv, "d:H:hp:Vv", opts, NULL)) != -1) {
255 switch (ch) {
256 case 'd':
257 certs_dir = optarg;
258 break;
259 case 'H':
260 hostname = optarg;
261 break;
262 case 'h':
263 usage();
264 break;
265 case 'p':
266 conf.port = strtonum(optarg, 0, UINT16_MAX, &errstr);
267 if (errstr)
268 fatalx("port number is %s: %s", errstr,
269 optarg);
270 break;
271 case 'V':
272 puts("Version: " GE_STRING);
273 return 0;
274 default:
275 usage();
276 break;
279 argc -= optind;
280 argv += optind;
282 if (argc > 1)
283 usage();
285 /* prepare the configuration */
286 init_mime(&conf.mime);
288 if (certs_dir == NULL)
289 certs_dir = data_dir();
291 /* set up the implicit vhost and location */
292 host = xcalloc(1, sizeof(*host));
293 TAILQ_INSERT_HEAD(&hosts, host, vhosts);
295 loc = xcalloc(1, sizeof(*loc));
296 loc->fcgi = -1;
297 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
299 load_local_cert(host, hostname, certs_dir);
301 strlcpy(host->domain, "*", sizeof(host->domain));
302 loc->auto_index = 1;
303 strlcpy(loc->match, "*", sizeof(loc->match));
305 if (*argv == NULL) {
306 if (getcwd(path, sizeof(path)) == NULL)
307 fatal("getcwd");
308 strlcpy(loc->dir, path, sizeof(loc->dir));
309 } else {
310 char *tmp;
312 tmp = absolutify_path(*argv);
313 strlcpy(loc->dir, tmp, sizeof(loc->dir));
314 free(tmp);
317 /* start the server */
318 signal(SIGPIPE, SIG_IGN);
319 setproctitle("%s", loc->dir);
320 return serve(hostname, conf.port, loc->dir);