Blob


1 /*
2 * Copyright (c) 2022, 2023 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 int privsep_process;
36 static const struct option opts[] = {
37 {"help", no_argument, NULL, 'h'},
38 {"version", no_argument, NULL, 'V'},
39 {NULL, 0, NULL, 0},
40 };
42 void
43 log_request(struct client *c, char *meta, size_t l)
44 {
45 char b[GEMINI_URL_LEN];
46 const char *t;
48 if (c->iri.schema != NULL) {
49 /* serialize the IRI */
50 strlcpy(b, c->iri.schema, sizeof(b));
51 strlcat(b, "://", sizeof(b));
53 /* log the decoded host name, but if it was invalid
54 * use the raw one. */
55 if (*c->domain != '\0')
56 strlcat(b, c->domain, sizeof(b));
57 else
58 strlcat(b, c->iri.host, sizeof(b));
60 if (*c->iri.path != '/')
61 strlcat(b, "/", sizeof(b));
62 strlcat(b, c->iri.path, sizeof(b)); /* TODO: sanitize UTF8 */
63 if (*c->iri.query != '\0') { /* TODO: sanitize UTF8 */
64 strlcat(b, "?", sizeof(b));
65 strlcat(b, c->iri.query, sizeof(b));
66 }
67 } else {
68 if ((t = c->req) == NULL)
69 t = "";
70 strlcpy(b, t, sizeof(b));
71 }
73 if ((t = memchr(meta, '\r', l)) == NULL)
74 t = meta + l;
76 fprintf(stderr, "%s:%s GET %s %.*s\n", c->rhost, c->rserv, b,
77 (int)(t-meta), meta);
78 }
80 void
81 load_local_cert(struct vhost *h, const char *hostname, const char *dir)
82 {
83 char *cert, *key;
85 if (asprintf(&cert, "%s/%s.cert.pem", dir, hostname) == -1)
86 fatal("asprintf");
87 if (asprintf(&key, "%s/%s.key.pem", dir, hostname) == -1)
88 fatal("asprintf");
90 if (access(cert, R_OK) == -1 || access(key, R_OK) == -1)
91 gen_certificate(hostname, cert, key);
93 h->cert = tls_load_file(cert, &h->certlen, NULL);
94 if (h->cert == NULL)
95 fatal("can't load %s", cert);
97 h->key = tls_load_file(key, &h->keylen, NULL);
98 if (h->key == NULL)
99 fatal("can't load %s", key);
101 strlcpy(h->domain, hostname, sizeof(h->domain));
104 /* wrapper around dirname(3). dn must be PATH_MAX+1 at least. */
105 static void
106 pdirname(const char *path, char *dn)
108 char p[PATH_MAX+1];
109 char *t;
111 strlcpy(p, path, sizeof(p));
112 t = dirname(p);
113 memmove(dn, t, strlen(t)+1);
116 static void
117 mkdirs(const char *path, mode_t mode)
119 char dname[PATH_MAX+1];
121 pdirname(path, dname);
122 if (!strcmp(dname, "/"))
123 return;
124 mkdirs(dname, mode);
125 if (mkdir(path, mode) != 0 && errno != EEXIST)
126 fatal("can't mkdir %s", path);
129 /* $XDG_DATA_HOME/gmid */
130 char *
131 data_dir(void)
133 const char *home, *xdg;
134 char *t;
136 if ((xdg = getenv("XDG_DATA_HOME")) == NULL) {
137 if ((home = getenv("HOME")) == NULL)
138 fatalx("XDG_DATA_HOME and HOME both empty");
139 if (asprintf(&t, "%s/.local/share/gmid", home) == -1)
140 fatalx("asprintf");
141 } else {
142 if (asprintf(&t, "%s/gmid", xdg) == -1)
143 fatal("asprintf");
146 mkdirs(t, 0755);
147 return t;
150 static int
151 serve(struct conf *conf, const char *host, int port, const char *dir)
153 struct addrinfo hints, *res, *res0;
154 struct vhost *vh = TAILQ_FIRST(&conf->hosts);
155 struct address *addr, *acp;
156 int r, error, saved_errno, sock = -1;
157 const char *cause = NULL;
158 char service[32];
159 int any = 0;
161 event_init();
163 r = snprintf(service, sizeof(service), "%d", port);
164 if (r < 0 || (size_t)r >= sizeof(service))
165 fatal("snprintf");
167 memset(&hints, 0, sizeof(hints));
168 hints.ai_family = AF_UNSPEC;
169 hints.ai_socktype = SOCK_STREAM;
170 hints.ai_flags = AI_PASSIVE;
171 error = getaddrinfo(host, service, &hints, &res0);
172 if (error)
173 fatalx("%s", gai_strerror(error));
174 for (res = res0; res; res = res->ai_next) {
175 sock = socket(res->ai_family, res->ai_socktype,
176 res->ai_protocol);
177 if (sock == -1) {
178 cause = "socket";
179 continue;
182 if (bind(sock, res->ai_addr, res->ai_addrlen) == -1) {
183 cause = "bind";
184 saved_errno = errno;
185 close(sock);
186 errno = saved_errno;
187 continue;
190 if (listen(sock, 5) == -1)
191 fatal("listen");
193 any = 1;
195 addr = xcalloc(1, sizeof(*addr));
196 addr->ai_flags = res->ai_flags;
197 addr->ai_family = res->ai_family;
198 addr->ai_socktype = res->ai_socktype;
199 addr->ai_protocol = res->ai_protocol;
200 addr->slen = res->ai_addrlen;
201 memcpy(&addr->ss, res->ai_addr, res->ai_addrlen);
203 addr->conf = conf;
204 addr->sock = sock;
205 event_set(&addr->evsock, addr->sock, EV_READ|EV_PERSIST,
206 do_accept, addr);
208 if ((addr->ctx = tls_server()) == NULL)
209 fatal("tls_server failure");
211 TAILQ_INSERT_HEAD(&conf->addrs, addr, addrs);
213 acp = xcalloc(1, sizeof(*acp));
214 memcpy(acp, addr, sizeof(*acp));
215 acp->sock = -1;
216 memset(&acp->evsock, 0, sizeof(acp->evsock));
217 TAILQ_INSERT_HEAD(&vh->addrs, addr, addrs);
220 if (!any)
221 fatal("%s", cause);
222 freeaddrinfo(res0);
224 server_init(NULL, NULL, NULL);
225 if (server_configure_done(conf) == -1)
226 fatalx("server configuration failed");
228 log_info("serving %s on port %d", dir, port);
229 event_dispatch();
230 log_info("quitting");
231 return 0;
234 static __dead void
235 usage(void)
237 fprintf(stderr,
238 "Version: " GE_STRING "\n"
239 "Usage: %s [-hVv] [-d certs-dir] [-H hostname] [-p port] [dir]\n",
240 getprogname());
241 exit(1);
244 int
245 main(int argc, char **argv)
247 struct conf *conf;
248 struct vhost *host;
249 struct location *loc;
250 const char *errstr, *certs_dir = NULL, *hostname = "localhost";
251 char path[PATH_MAX];
252 int ch, port = 1965;
254 setlocale(LC_CTYPE, "");
256 log_init(1, LOG_DAEMON);
257 log_setverbose(0);
258 conf = config_new();
260 /* ge doesn't do privsep so no privsep crypto engine. */
261 conf->use_privsep_crypto = 0;
263 while ((ch = getopt_long(argc, argv, "d:H:hp:Vv", opts, NULL)) != -1) {
264 switch (ch) {
265 case 'd':
266 certs_dir = optarg;
267 break;
268 case 'H':
269 hostname = optarg;
270 break;
271 case 'h':
272 usage();
273 break;
274 case 'p':
275 port = strtonum(optarg, 0, UINT16_MAX, &errstr);
276 if (errstr)
277 fatalx("port number is %s: %s", errstr,
278 optarg);
279 break;
280 case 'V':
281 puts("Version: " GE_STRING);
282 return 0;
283 default:
284 usage();
285 break;
288 argc -= optind;
289 argv += optind;
291 if (argc > 1)
292 usage();
294 /* prepare the configuration */
295 init_mime(&conf->mime);
297 if (certs_dir == NULL)
298 certs_dir = data_dir();
300 /* set up the implicit vhost and location */
301 host = xcalloc(1, sizeof(*host));
302 TAILQ_INSERT_HEAD(&conf->hosts, host, vhosts);
304 loc = xcalloc(1, sizeof(*loc));
305 loc->fcgi = -1;
306 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
308 load_local_cert(host, hostname, certs_dir);
310 strlcpy(host->domain, "*", sizeof(host->domain));
311 loc->auto_index = 1;
312 strlcpy(loc->match, "*", sizeof(loc->match));
314 if (*argv == NULL) {
315 if (getcwd(path, sizeof(path)) == NULL)
316 fatal("getcwd");
317 strlcpy(loc->dir, path, sizeof(loc->dir));
318 } else {
319 char *tmp;
321 tmp = absolutify_path(*argv);
322 strlcpy(loc->dir, tmp, sizeof(loc->dir));
323 free(tmp);
326 /* start the server */
327 signal(SIGPIPE, SIG_IGN);
328 setproctitle("%s", loc->dir);
329 return serve(conf, hostname, port, loc->dir);