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];
49 ec = getnameinfo((struct sockaddr*)&c->raddr, c->raddrlen,
52 NI_NUMERICHOST | NI_NUMERICSERV);
54 fatalx("getnameinfo: %s", gai_strerror(ec));
56 if (c->iri.schema != NULL) {
57 /* serialize the IRI */
58 strlcpy(b, c->iri.schema, sizeof(b));
59 strlcat(b, "://", sizeof(b));
61 /* log the decoded host name, but if it was invalid
63 if (*c->domain != '\0')
64 strlcat(b, c->domain, sizeof(b));
66 strlcat(b, c->iri.host, sizeof(b));
68 if (*c->iri.path != '/')
69 strlcat(b, "/", sizeof(b));
70 strlcat(b, c->iri.path, sizeof(b)); /* TODO: sanitize UTF8 */
71 if (*c->iri.query != '\0') { /* TODO: sanitize UTF8 */
72 strlcat(b, "?", sizeof(b));
73 strlcat(b, c->iri.query, sizeof(b));
76 if ((t = c->req) == NULL)
78 strlcpy(b, t, sizeof(b));
81 if ((t = memchr(meta, '\r', l)) == NULL)
84 fprintf(stderr, "%s:%s GET %s %.*s\n", hbuf, sbuf, b,
89 load_local_cert(struct vhost *h, const char *hostname, const char *dir)
93 if (asprintf(&cert, "%s/%s.cert.pem", dir, hostname) == -1)
95 if (asprintf(&key, "%s/%s.key.pem", dir, hostname) == -1)
98 if (access(cert, R_OK) == -1 || access(key, R_OK) == -1)
99 gen_certificate(hostname, cert, key);
101 h->cert = tls_load_file(cert, &h->certlen, NULL);
103 fatal("can't load %s", cert);
105 h->key = tls_load_file(key, &h->keylen, NULL);
107 fatal("can't load %s", key);
109 strlcpy(h->domain, hostname, sizeof(h->domain));
112 /* wrapper around dirname(3). dn must be PATH_MAX+1 at least. */
114 pdirname(const char *path, char *dn)
119 strlcpy(p, path, sizeof(p));
121 memmove(dn, t, strlen(t)+1);
125 mkdirs(const char *path, mode_t mode)
127 char dname[PATH_MAX+1];
129 pdirname(path, dname);
130 if (!strcmp(dname, "/"))
133 if (mkdir(path, mode) != 0 && errno != EEXIST)
134 fatal("can't mkdir %s", path);
137 /* $XDG_DATA_HOME/gmid */
141 const char *home, *xdg;
144 if ((xdg = getenv("XDG_DATA_HOME")) == NULL) {
145 if ((home = getenv("HOME")) == NULL)
146 fatalx("XDG_DATA_HOME and HOME both empty");
147 if (asprintf(&t, "%s/.local/share/gmid", home) == -1)
150 if (asprintf(&t, "%s/gmid", xdg) == -1)
159 serve(struct conf *conf, const char *host, int port, const char *dir)
161 struct addrinfo hints, *res, *res0;
162 struct vhost *vh = TAILQ_FIRST(&conf->hosts);
163 struct address *addr, *acp;
164 int r, error, saved_errno, sock = -1;
165 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)
203 addr = xcalloc(1, sizeof(*addr));
204 addr->ai_flags = res->ai_flags;
205 addr->ai_family = res->ai_family;
206 addr->ai_socktype = res->ai_socktype;
207 addr->ai_protocol = res->ai_protocol;
208 addr->slen = res->ai_addrlen;
209 memcpy(&addr->ss, res->ai_addr, res->ai_addrlen);
213 event_set(&addr->evsock, addr->sock, EV_READ|EV_PERSIST,
216 TAILQ_INSERT_HEAD(&conf->addrs, addr, addrs);
218 acp = xcalloc(1, sizeof(*acp));
219 memcpy(acp, addr, sizeof(*acp));
221 memset(&acp->evsock, 0, sizeof(acp->evsock));
222 TAILQ_INSERT_HEAD(&vh->addrs, addr, addrs);
229 server_init(NULL, NULL, NULL);
230 if (server_configure_done(conf) == -1)
231 fatalx("server configuration failed");
233 log_info("serving %s on port %d", dir, port);
235 log_info("quitting");
243 "Version: " GE_STRING "\n"
244 "Usage: %s [-hVv] [-d certs-dir] [-H hostname] [-p port] [dir]\n",
250 main(int argc, char **argv)
254 struct location *loc;
255 const char *errstr, *certs_dir = NULL, *hostname = "localhost";
259 setlocale(LC_CTYPE, "");
261 log_init(1, LOG_DAEMON);
265 /* ge doesn't do privsep so no privsep crypto engine. */
266 conf->use_privsep_crypto = 0;
268 while ((ch = getopt_long(argc, argv, "d:H:hp:Vv", opts, NULL)) != -1) {
280 port = strtonum(optarg, 0, UINT16_MAX, &errstr);
282 fatalx("port number is %s: %s", errstr,
286 puts("Version: " GE_STRING);
299 /* prepare the configuration */
300 init_mime(&conf->mime);
302 if (certs_dir == NULL)
303 certs_dir = data_dir();
305 /* set up the implicit vhost and location */
306 host = xcalloc(1, sizeof(*host));
307 TAILQ_INSERT_HEAD(&conf->hosts, host, vhosts);
309 loc = xcalloc(1, sizeof(*loc));
311 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
313 load_local_cert(host, hostname, certs_dir);
315 strlcpy(host->domain, "*", sizeof(host->domain));
317 strlcpy(loc->match, "*", sizeof(loc->match));
320 if (getcwd(path, sizeof(path)) == NULL)
322 strlcpy(loc->dir, path, sizeof(loc->dir));
326 tmp = absolutify_path(*argv);
327 strlcpy(loc->dir, tmp, sizeof(loc->dir));
331 /* start the server */
332 signal(SIGPIPE, SIG_IGN);
333 setproctitle("%s", loc->dir);
334 return serve(conf, hostname, port, loc->dir);