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.
36 static const struct option opts[] = {
37 {"help", no_argument, NULL, 'h'},
38 {"version", no_argument, NULL, 'V'},
43 log_request(struct client *c, char *meta, size_t l)
45 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV], b[GEMINI_URL_LEN];
50 len = sizeof(c->addr);
51 ec = getnameinfo((struct sockaddr*)&c->addr, len,
54 NI_NUMERICHOST | NI_NUMERICSERV);
56 fatalx("getnameinfo: %s", gai_strerror(ec));
58 if (c->iri.schema != NULL) {
59 /* serialize the IRI */
60 strlcpy(b, c->iri.schema, sizeof(b));
61 strlcat(b, "://", sizeof(b));
63 /* log the decoded host name, but if it was invalid
65 if (*c->domain != '\0')
66 strlcat(b, c->domain, sizeof(b));
68 strlcat(b, c->iri.host, sizeof(b));
70 if (*c->iri.path != '/')
71 strlcat(b, "/", sizeof(b));
72 strlcat(b, c->iri.path, sizeof(b)); /* TODO: sanitize UTF8 */
73 if (*c->iri.query != '\0') { /* TODO: sanitize UTF8 */
74 strlcat(b, "?", sizeof(b));
75 strlcat(b, c->iri.query, sizeof(b));
78 if ((t = c->req) == NULL)
80 strlcpy(b, t, sizeof(b));
83 if ((t = memchr(meta, '\r', l)) == NULL)
86 fprintf(stderr, "%s:%s GET %s %.*s\n", hbuf, sbuf, b,
91 load_local_cert(struct vhost *h, const char *hostname, const char *dir)
95 if (asprintf(&cert, "%s/%s.cert.pem", dir, hostname) == -1)
97 if (asprintf(&key, "%s/%s.key.pem", dir, hostname) == -1)
100 if (access(cert, R_OK) == -1 || access(key, R_OK) == -1)
101 gen_certificate(hostname, cert, key);
103 h->cert = tls_load_file(cert, &h->certlen, NULL);
105 fatal("can't load %s", cert);
107 h->key = tls_load_file(key, &h->keylen, NULL);
109 fatal("can't load %s", key);
111 strlcpy(h->domain, hostname, sizeof(h->domain));
114 /* wrapper around dirname(3). dn must be PATH_MAX+1 at least. */
116 pdirname(const char *path, char *dn)
121 strlcpy(p, path, sizeof(p));
123 memmove(dn, t, strlen(t)+1);
127 mkdirs(const char *path, mode_t mode)
129 char dname[PATH_MAX+1];
131 pdirname(path, dname);
132 if (!strcmp(dname, "/"))
135 if (mkdir(path, mode) != 0 && errno != EEXIST)
136 fatal("can't mkdir %s", path);
139 /* $XDG_DATA_HOME/gmid */
143 const char *home, *xdg;
146 if ((xdg = getenv("XDG_DATA_HOME")) == NULL) {
147 if ((home = getenv("HOME")) == NULL)
148 fatalx("XDG_DATA_HOME and HOME both empty");
149 if (asprintf(&t, "%s/.local/share/gmid", home) == -1)
152 if (asprintf(&t, "%s/gmid", xdg) == -1)
161 serve(struct conf *conf, const char *host, int port, const char *dir)
163 struct addrinfo hints, *res, *res0;
164 int r, error, saved_errno, sock = -1;
165 const char *cause = NULL;
168 r = snprintf(service, sizeof(service), "%d", port);
169 if (r < 0 || (size_t)r >= sizeof(service))
172 memset(&hints, 0, sizeof(hints));
173 hints.ai_family = AF_UNSPEC;
174 hints.ai_socktype = SOCK_STREAM;
175 hints.ai_flags = AI_PASSIVE;
176 error = getaddrinfo(host, service, &hints, &res0);
178 fatalx("%s", gai_strerror(error));
179 for (res = res0; res; res = res->ai_next) {
180 sock = socket(res->ai_family, res->ai_socktype,
187 if (bind(sock, res->ai_addr, res->ai_addrlen) == -1) {
195 if (listen(sock, 5) == -1)
199 * for the time being, we're happy as soon as
213 event_set(&conf->evsock4, conf->sock4, EV_READ|EV_PERSIST,
216 server_init(NULL, NULL, NULL);
217 if (server_configure_done(conf) == -1)
218 fatalx("server configuration failed");
220 log_info("serving %s on port %d", dir, port);
222 log_info("quitting");
230 "Version: " GE_STRING "\n"
231 "Usage: %s [-hVv] [-d certs-dir] [-H hostname] [-p port] [dir]\n",
237 main(int argc, char **argv)
241 struct location *loc;
242 const char *errstr, *certs_dir = NULL, *hostname = "localhost";
246 setlocale(LC_CTYPE, "");
248 log_init(1, LOG_DAEMON);
252 while ((ch = getopt_long(argc, argv, "d:H:hp:Vv", opts, NULL)) != -1) {
264 conf->port = strtonum(optarg, 0, UINT16_MAX, &errstr);
266 fatalx("port number is %s: %s", errstr,
270 puts("Version: " GE_STRING);
283 /* prepare the configuration */
284 init_mime(&conf->mime);
286 if (certs_dir == NULL)
287 certs_dir = data_dir();
289 /* set up the implicit vhost and location */
290 host = xcalloc(1, sizeof(*host));
291 TAILQ_INSERT_HEAD(&conf->hosts, host, vhosts);
293 loc = xcalloc(1, sizeof(*loc));
295 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
297 load_local_cert(host, hostname, certs_dir);
299 strlcpy(host->domain, "*", sizeof(host->domain));
301 strlcpy(loc->match, "*", sizeof(loc->match));
304 if (getcwd(path, sizeof(path)) == NULL)
306 strlcpy(loc->dir, path, sizeof(loc->dir));
310 tmp = absolutify_path(*argv);
311 strlcpy(loc->dir, tmp, sizeof(loc->dir));
315 /* start the server */
316 signal(SIGPIPE, SIG_IGN);
317 setproctitle("%s", loc->dir);
318 return serve(conf, hostname, conf->port, loc->dir);