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, int code, const char *meta)
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 fprintf(stderr, "%s:%s GET %s %d %s\n", c->rhost, c->rserv, b,
74 code, meta);
75 }
77 void
78 load_local_cert(struct vhost *h, const char *hostname, const char *dir)
79 {
80 char *cert, *key;
82 if (asprintf(&cert, "%s/%s.cert.pem", dir, hostname) == -1)
83 fatal("asprintf");
84 if (asprintf(&key, "%s/%s.key.pem", dir, hostname) == -1)
85 fatal("asprintf");
87 if (access(cert, R_OK) == -1 || access(key, R_OK) == -1)
88 gen_certificate(hostname, cert, key);
90 h->cert = tls_load_file(cert, &h->certlen, NULL);
91 if (h->cert == NULL)
92 fatal("can't load %s", cert);
94 h->key = tls_load_file(key, &h->keylen, NULL);
95 if (h->key == NULL)
96 fatal("can't load %s", key);
98 strlcpy(h->domain, hostname, sizeof(h->domain));
99 }
101 /* wrapper around dirname(3). dn must be PATH_MAX+1 at least. */
102 static void
103 pdirname(const char *path, char *dn)
105 char p[PATH_MAX+1];
106 char *t;
108 strlcpy(p, path, sizeof(p));
109 t = dirname(p);
110 memmove(dn, t, strlen(t)+1);
113 static void
114 mkdirs(const char *path, mode_t mode)
116 char dname[PATH_MAX+1];
118 pdirname(path, dname);
119 if (!strcmp(dname, "/"))
120 return;
121 mkdirs(dname, mode);
122 if (mkdir(path, mode) != 0 && errno != EEXIST)
123 fatal("can't mkdir %s", path);
126 /* $XDG_DATA_HOME/gmid */
127 char *
128 data_dir(void)
130 const char *home, *xdg;
131 char *t;
133 if ((xdg = getenv("XDG_DATA_HOME")) == NULL) {
134 if ((home = getenv("HOME")) == NULL)
135 fatalx("XDG_DATA_HOME and HOME both empty");
136 if (asprintf(&t, "%s/.local/share/gmid", home) == -1)
137 fatalx("asprintf");
138 } else {
139 if (asprintf(&t, "%s/gmid", xdg) == -1)
140 fatal("asprintf");
143 mkdirs(t, 0755);
144 return t;
147 static int
148 serve(struct conf *conf, const char *host, int port, const char *dir)
150 struct addrinfo hints, *res, *res0;
151 struct vhost *vh = TAILQ_FIRST(&conf->hosts);
152 struct address *addr, *acp;
153 int r, error, saved_errno, sock = -1;
154 const char *cause = NULL;
155 char service[32];
156 int any = 0;
158 event_init();
160 r = snprintf(service, sizeof(service), "%d", port);
161 if (r < 0 || (size_t)r >= sizeof(service))
162 fatal("snprintf");
164 memset(&hints, 0, sizeof(hints));
165 hints.ai_family = AF_UNSPEC;
166 hints.ai_socktype = SOCK_STREAM;
167 hints.ai_flags = AI_PASSIVE;
168 error = getaddrinfo(host, service, &hints, &res0);
169 if (error)
170 fatalx("%s", gai_strerror(error));
171 for (res = res0; res; res = res->ai_next) {
172 sock = socket(res->ai_family, res->ai_socktype,
173 res->ai_protocol);
174 if (sock == -1) {
175 cause = "socket";
176 continue;
179 if (bind(sock, res->ai_addr, res->ai_addrlen) == -1) {
180 cause = "bind";
181 saved_errno = errno;
182 close(sock);
183 errno = saved_errno;
184 continue;
187 if (listen(sock, 5) == -1)
188 fatal("listen");
190 any = 1;
192 addr = xcalloc(1, sizeof(*addr));
193 addr->ai_flags = res->ai_flags;
194 addr->ai_family = res->ai_family;
195 addr->ai_socktype = res->ai_socktype;
196 addr->ai_protocol = res->ai_protocol;
197 addr->slen = res->ai_addrlen;
198 memcpy(&addr->ss, res->ai_addr, res->ai_addrlen);
200 addr->conf = conf;
201 addr->sock = sock;
202 event_set(&addr->evsock, addr->sock, EV_READ|EV_PERSIST,
203 server_accept, addr);
205 if ((addr->ctx = tls_server()) == NULL)
206 fatal("tls_server failure");
208 TAILQ_INSERT_HEAD(&conf->addrs, addr, addrs);
210 acp = xcalloc(1, sizeof(*acp));
211 memcpy(acp, addr, sizeof(*acp));
212 acp->sock = -1;
213 memset(&acp->evsock, 0, sizeof(acp->evsock));
214 TAILQ_INSERT_HEAD(&vh->addrs, addr, addrs);
217 if (!any)
218 fatal("%s", cause);
219 freeaddrinfo(res0);
221 server_init(NULL, NULL, NULL);
222 if (server_configure_done(conf) == -1)
223 fatalx("server configuration failed");
225 log_info("serving %s on port %d", dir, port);
226 event_dispatch();
227 log_info("quitting");
228 return 0;
231 static __dead void
232 usage(void)
234 fprintf(stderr,
235 "Version: " GE_STRING "\n"
236 "Usage: %s [-hVv] [-d certs-dir] [-H hostname] [-p port] [dir]\n",
237 getprogname());
238 exit(1);
241 int
242 main(int argc, char **argv)
244 struct conf *conf;
245 struct vhost *host;
246 struct location *loc;
247 const char *errstr, *certs_dir = NULL, *hostname = "localhost";
248 char path[PATH_MAX];
249 int ch, port = 1965;
251 setlocale(LC_CTYPE, "");
253 log_init(1, LOG_DAEMON);
254 log_setverbose(0);
255 conf = config_new();
257 /* ge doesn't do privsep so no privsep crypto engine. */
258 conf->use_privsep_crypto = 0;
260 while ((ch = getopt_long(argc, argv, "d:H:hp:Vv", opts, NULL)) != -1) {
261 switch (ch) {
262 case 'd':
263 certs_dir = optarg;
264 break;
265 case 'H':
266 hostname = optarg;
267 break;
268 case 'h':
269 usage();
270 break;
271 case 'p':
272 port = strtonum(optarg, 0, UINT16_MAX, &errstr);
273 if (errstr)
274 fatalx("port number is %s: %s", errstr,
275 optarg);
276 break;
277 case 'V':
278 puts("Version: " GE_STRING);
279 return 0;
280 default:
281 usage();
282 break;
285 argc -= optind;
286 argv += optind;
288 if (argc > 1)
289 usage();
291 /* prepare the configuration */
292 init_mime(&conf->mime);
294 if (certs_dir == NULL)
295 certs_dir = data_dir();
297 /* set up the implicit vhost and location */
298 host = xcalloc(1, sizeof(*host));
299 TAILQ_INSERT_HEAD(&conf->hosts, host, vhosts);
301 loc = xcalloc(1, sizeof(*loc));
302 loc->fcgi = -1;
303 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
305 load_local_cert(host, hostname, certs_dir);
307 strlcpy(host->domain, "*", sizeof(host->domain));
308 loc->auto_index = 1;
309 strlcpy(loc->match, "*", sizeof(loc->match));
311 if (*argv == NULL) {
312 if (getcwd(path, sizeof(path)) == NULL)
313 fatal("getcwd");
314 strlcpy(loc->dir, path, sizeof(loc->dir));
315 } else {
316 char *tmp;
318 tmp = absolutify_path(*argv);
319 strlcpy(loc->dir, tmp, sizeof(loc->dir));
320 free(tmp);
323 /* start the server */
324 signal(SIGPIPE, SIG_IGN);
325 setproctitle("%s", loc->dir);
326 return serve(conf, hostname, port, loc->dir);