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 fcgi fcgi[FCGI_MAX]; /* just because it's referenced */
38 struct vhosthead hosts = TAILQ_HEAD_INITIALIZER(hosts);
40 static const struct option opts[] = {
41 {"help", no_argument, NULL, 'h'},
42 {"version", no_argument, NULL, 'V'},
47 log_request(struct client *c, char *meta, size_t l)
49 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV], b[GEMINI_URL_LEN];
54 len = sizeof(c->addr);
55 ec = getnameinfo((struct sockaddr*)&c->addr, len,
58 NI_NUMERICHOST | NI_NUMERICSERV);
60 fatalx("getnameinfo: %s", gai_strerror(ec));
62 if (c->iri.schema != NULL) {
63 /* serialize the IRI */
64 strlcpy(b, c->iri.schema, sizeof(b));
65 strlcat(b, "://", sizeof(b));
67 /* log the decoded host name, but if it was invalid
69 if (*c->domain != '\0')
70 strlcat(b, c->domain, sizeof(b));
72 strlcat(b, c->iri.host, sizeof(b));
74 if (*c->iri.path != '/')
75 strlcat(b, "/", sizeof(b));
76 strlcat(b, c->iri.path, sizeof(b)); /* TODO: sanitize UTF8 */
77 if (*c->iri.query != '\0') { /* TODO: sanitize UTF8 */
78 strlcat(b, "?", sizeof(b));
79 strlcat(b, c->iri.query, sizeof(b));
82 if ((t = c->req) == NULL)
84 strlcpy(b, t, sizeof(b));
87 if ((t = memchr(meta, '\r', l)) == NULL)
90 fprintf(stderr, "%s:%s GET %s %.*s\n", hbuf, sbuf, b,
95 load_local_cert(struct vhost *h, const char *hostname, const char *dir)
99 if (asprintf(&cert, "%s/%s.cert.pem", dir, hostname) == -1)
101 if (asprintf(&key, "%s/%s.key.pem", dir, hostname) == -1)
104 if (access(cert, R_OK) == -1 || access(key, R_OK) == -1)
105 gen_certificate(hostname, cert, key);
107 h->cert = tls_load_file(cert, &h->certlen, NULL);
109 fatal("can't load %s", cert);
111 h->key = tls_load_file(key, &h->keylen, NULL);
113 fatal("can't load %s", key);
115 strlcpy(h->domain, hostname, sizeof(h->domain));
118 /* wrapper around dirname(3). dn must be PATH_MAX+1 at least. */
120 pdirname(const char *path, char *dn)
125 strlcpy(p, path, sizeof(p));
127 memmove(dn, t, strlen(t)+1);
131 mkdirs(const char *path, mode_t mode)
133 char dname[PATH_MAX+1];
135 pdirname(path, dname);
136 if (!strcmp(dname, "/"))
139 if (mkdir(path, mode) != 0 && errno != EEXIST)
140 fatal("can't mkdir %s", path);
143 /* $XDG_DATA_HOME/gmid */
147 const char *home, *xdg;
150 if ((xdg = getenv("XDG_DATA_HOME")) == NULL) {
151 if ((home = getenv("HOME")) == NULL)
152 fatalx("XDG_DATA_HOME and HOME both empty");
153 if (asprintf(&t, "%s/.local/share/gmid", home) == -1)
156 if (asprintf(&t, "%s/gmid", xdg) == -1)
165 serve(const char *host, int port, const char *dir)
167 struct addrinfo hints, *res, *res0;
168 int r, error, saved_errno, sock = -1;
169 const char *cause = NULL;
172 r = snprintf(service, sizeof(service), "%d", port);
173 if (r < 0 || (size_t)r >= sizeof(service))
176 memset(&hints, 0, sizeof(hints));
177 hints.ai_family = AF_UNSPEC;
178 hints.ai_socktype = SOCK_STREAM;
179 hints.ai_flags = AI_PASSIVE;
180 error = getaddrinfo(host, service, &hints, &res0);
182 fatalx("%s", gai_strerror(error));
183 for (res = res0; res; res = res->ai_next) {
184 sock = socket(res->ai_family, res->ai_socktype,
191 if (bind(sock, res->ai_addr, res->ai_addrlen) == -1) {
199 if (listen(sock, 5) == -1)
203 * for the time being, we're happy as soon as
217 event_set(&conf.evsock4, conf.sock4, EV_READ|EV_PERSIST,
220 server_init(NULL, NULL, NULL);
221 if (server_configure_done(&conf) == -1)
222 fatalx("server configuration failed");
224 log_info("serving %s on port %d", dir, port);
226 log_info("quitting");
234 "Version: " GE_STRING "\n"
235 "Usage: %s [-hVv] [-d certs-dir] [-H hostname] [-p port] [dir]\n",
241 main(int argc, char **argv)
244 struct location *loc;
245 const char *errstr, *certs_dir = NULL, *hostname = "localhost";
249 setlocale(LC_CTYPE, "");
251 log_init(1, LOG_DAEMON);
255 while ((ch = getopt_long(argc, argv, "d:H:hp:Vv", opts, NULL)) != -1) {
267 conf.port = strtonum(optarg, 0, UINT16_MAX, &errstr);
269 fatalx("port number is %s: %s", errstr,
273 puts("Version: " GE_STRING);
286 /* prepare the configuration */
287 init_mime(&conf.mime);
289 if (certs_dir == NULL)
290 certs_dir = data_dir();
292 /* set up the implicit vhost and location */
293 host = xcalloc(1, sizeof(*host));
294 TAILQ_INSERT_HEAD(&hosts, host, vhosts);
296 loc = xcalloc(1, sizeof(*loc));
298 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
300 load_local_cert(host, hostname, certs_dir);
302 strlcpy(host->domain, "*", sizeof(host->domain));
304 strlcpy(loc->match, "*", sizeof(loc->match));
307 if (getcwd(path, sizeof(path)) == NULL)
309 strlcpy(loc->dir, path, sizeof(loc->dir));
313 tmp = absolutify_path(*argv);
314 strlcpy(loc->dir, tmp, sizeof(loc->dir));
318 /* start the server */
319 signal(SIGPIPE, SIG_IGN);
320 setproctitle("%s", loc->dir);
321 return serve(hostname, conf.port, loc->dir);