2 * Copyright (c) 2020 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.
17 #include <sys/socket.h>
20 #include <netinet/in.h>
32 # define pledge(a, b) 0
33 # define unveil(a, b) 0
34 #endif /* __OpenBSD__ */
36 #define GEMINI_URL_LEN (1024+3) /* URL max len + \r\n + \0 */
38 /* large enough to hold a copy of a gemini URL and still have extra room */
45 #define BAD_REQUEST 59
50 url_after_proto(char *url)
53 const char *proto = "gemini";
54 const char *marker = "://";
56 if ((s = strstr(url, marker)) == NULL)
59 /* not a gemini:// URL */
61 if (s - strlen(proto) < url)
64 /* if (strcmp(s - strlen(proto), proto)) */
67 /* a valid gemini:// URL */
68 return s + strlen(marker);
72 url_start_of_request(char *url)
76 if ((s = url_after_proto(url)) == NULL)
79 if ((t = strstr(s, "/")) == NULL)
87 const char *e = "\r\n";
90 if ((s = strstr(url, e)) == NULL)
96 fprintf(stderr, "the request was longer than 1024 bytes\n");
104 adjust_path(char *path)
112 if (!strcmp(&path[len-3], "/..")) {
117 /* if the path is only `..` trim out and exit */
118 if (!strcmp(path, "..")) {
123 /* remove every ../ in the path */
125 if ((s = strstr(path, "../")) == NULL)
127 memmove(s, s+3, strlen(s)+1); /* copy also the \0 */
132 path_isdir(char *path)
136 return path[strlen(path)-1] == '/';
140 start_reply(struct tls *ctx, int code, const char *reason)
142 char buf[1030] = {0}; /* status + ' ' + max reply len + \r\n\0 */
145 len = snprintf(buf, sizeof(buf), "%d %s\r\n", code, reason);
146 assert(len < (int)sizeof(buf));
147 tls_write(ctx, buf, len);
155 if (fstat(fd, &sb) == -1) {
157 return 1; /* we'll probably fail later on anyway */
160 return S_ISDIR(sb.st_mode);
163 void send_dir(char*, struct tls*);
166 send_file(char *path, struct tls *ctx)
174 bzero(fpath, sizeof(fpath));
179 strlcat(fpath, path, PATHBUF);
181 if ((fd = openat(dirfd, fpath, O_RDONLY | O_NOFOLLOW)) == -1) {
182 warn("open: %s", fpath);
183 start_reply(ctx, NOT_FOUND, "not found");
188 warnx("%s is a directory, trying %s/index.gmi", fpath, fpath);
190 send_dir(fpath, ctx);
194 /* assume it's a text/gemini file */
195 start_reply(ctx, SUCCESS, "text/gemini");
198 if ((w = read(fd, buf, sizeof(buf))) == -1) {
199 warn("read: %s", fpath);
210 if ((t = tls_write(ctx, buf + i, w)) == -1) {
211 warnx("tls_write (path=%s) : %s",
226 send_dir(char *path, struct tls *ctx)
231 bzero(fpath, PATHBUF);
236 /* this cannot fail since sizeof(fpath) > maxlen of path */
237 strlcat(fpath, path, PATHBUF);
240 /* add a trailing / in case. */
241 if (fpath[len-1] != '/') {
245 strlcat(fpath, "index.gmi", sizeof(fpath));
247 send_file(fpath, ctx);
251 handle(char *url, struct tls *ctx)
255 if (!url_trim(url)) {
256 start_reply(ctx, BAD_REQUEST, "bad request");
260 if ((path = url_start_of_request(url)) == NULL) {
261 start_reply(ctx, BAD_REQUEST, "bad request");
267 fprintf(stderr, "requested path: %s\n", path);
269 if (path_isdir(path))
272 send_file(path, ctx);
276 make_socket(int port)
279 struct sockaddr_in addr;
281 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
285 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
286 err(1, "setsockopt(SO_REUSEADDR)");
289 if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
290 err(1, "setsockopt(SO_REUSEPORT)");
292 bzero(&addr, sizeof(addr));
293 addr.sin_family = AF_INET;
294 addr.sin_port = htons(port);
295 addr.sin_addr.s_addr = INADDR_ANY;
297 if (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
300 if (listen(sock, 16) == -1)
307 loop(struct tls *ctx, int sock)
310 struct sockaddr_in client;
312 struct tls *clientctx;
313 char buf[GEMINI_URL_LEN];
316 len = sizeof(client);
317 if ((fd = accept(sock, (struct sockaddr*)&client, &len)) == -1)
320 if (tls_accept_socket(ctx, &clientctx, fd) == -1) {
321 warnx("tls_accept_socket: %s", tls_error(ctx));
325 bzero(buf, GEMINI_URL_LEN);
326 if (tls_read(clientctx, buf, sizeof(buf)-1) == -1) {
327 warnx("tls_read: %s", tls_error(clientctx));
331 handle(buf, clientctx);
334 if (tls_close(clientctx) == -1)
335 warn("tls_close: client");
342 usage(const char *me)
345 "USAGE: %s [-h] [-c cert.pem] [-d docs] [-k key.pem]\n",
350 main(int argc, char **argv)
352 const char *cert = "cert.pem", *key = "key.pem", *dir = "docs";
353 struct tls *ctx = NULL;
354 struct tls_config *conf;
357 while ((ch = getopt(argc, argv, "c:d:hk:")) != -1) {
381 if ((conf = tls_config_new()) == NULL)
382 err(1, "tls_config_new");
384 if (tls_config_set_protocols(conf,
385 TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3) == -1)
386 err(1, "tls_config_set_protocols");
388 if (tls_config_set_cert_file(conf, cert) == -1)
389 err(1, "tls_config_set_cert_file: %s", cert);
391 if (tls_config_set_key_file(conf, key) == -1)
392 err(1, "tls_config_set_key_file: %s", key);
394 if ((ctx = tls_server()) == NULL)
395 err(1, "tls_server");
397 if (tls_configure(ctx, conf) == -1)
398 errx(1, "tls_configure: %s", tls_error(ctx));
400 sock = make_socket(1965);
402 if ((dirfd = open(dir, O_RDONLY | O_DIRECTORY)) == -1)
403 err(1, "open: %s", dir);
405 if (unveil(dir, "r") == -1)
408 if (pledge("stdio rpath inet", "") == -1)
415 tls_config_free(conf);