2 * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
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.
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.
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'},
46 log_request(struct client *c, char *meta, size_t l)
48 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV], b[GEMINI_URL_LEN];
53 len = sizeof(c->addr);
54 ec = getnameinfo((struct sockaddr*)&c->addr, len,
57 NI_NUMERICHOST | NI_NUMERICSERV);
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
68 if (*c->domain != '\0')
69 strlcat(b, c->domain, sizeof(b));
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));
81 if ((t = c->req) == NULL)
83 strlcpy(b, t, sizeof(b));
86 if ((t = memchr(meta, '\r', l)) == NULL)
89 fprintf(stderr, "%s:%s GET %s %.*s\n", hbuf, sbuf, b,
94 load_local_cert(struct vhost *h, const char *hostname, const char *dir)
98 if (asprintf(&cert, "%s/%s.cert.pem", dir, hostname) == -1)
100 if (asprintf(&key, "%s/%s.key.pem", dir, hostname) == -1)
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);
108 fatal("can't load %s", cert);
110 h->key = tls_load_file(key, &h->keylen, 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. */
119 pdirname(const char *path, char *dn)
124 strlcpy(p, path, sizeof(p));
126 memmove(dn, t, strlen(t)+1);
130 mkdirs(const char *path, mode_t mode)
132 char dname[PATH_MAX+1];
134 pdirname(path, dname);
135 if (!strcmp(dname, "/"))
138 if (mkdir(path, mode) != 0 && errno != EEXIST)
139 fatal("can't mkdir %s", path);
142 /* $XDG_DATA_HOME/gmid */
146 const char *home, *xdg;
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)
155 if (asprintf(&t, "%s/gmid", xdg) == -1)
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;
171 r = snprintf(service, sizeof(service), "%d", port);
172 if (r < 0 || (size_t)r >= sizeof(service))
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);
181 fatalx("%s", gai_strerror(error));
182 for (res = res0; res; res = res->ai_next) {
183 sock = socket(res->ai_family, res->ai_socktype,
190 if (bind(sock, res->ai_addr, res->ai_addrlen) == -1) {
198 if (listen(sock, 5) == -1)
202 * for the time being, we're happy as soon as
216 event_set(&conf.evsock4, conf.sock4, EV_READ|EV_PERSIST,
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);
225 log_info("quitting");
233 "Version: " GE_STRING "\n"
234 "Usage: %s [-hVv] [-d certs-dir] [-H hostname] [-p port] [dir]\n",
240 main(int argc, char **argv)
243 struct location *loc;
244 const char *errstr, *certs_dir = NULL, *hostname = "localhost";
248 setlocale(LC_CTYPE, "");
250 log_init(1, LOG_DAEMON);
254 while ((ch = getopt_long(argc, argv, "d:H:hp:Vv", opts, NULL)) != -1) {
266 conf.port = strtonum(optarg, 0, UINT16_MAX, &errstr);
268 fatalx("port number is %s: %s", errstr,
272 puts("Version: " GE_STRING);
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));
297 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
299 load_local_cert(host, hostname, certs_dir);
301 strlcpy(host->domain, "*", sizeof(host->domain));
303 strlcpy(loc->match, "*", sizeof(loc->match));
306 if (getcwd(path, sizeof(path)) == NULL)
308 strlcpy(loc->dir, path, sizeof(loc->dir));
312 tmp = absolutify_path(*argv);
313 strlcpy(loc->dir, tmp, sizeof(loc->dir));
317 /* start the server */
318 signal(SIGPIPE, SIG_IGN);
319 setproctitle("%s", loc->dir);
320 return serve(hostname, conf.port, loc->dir);