2 3e4749f7 2020-10-02 op * Copyright (c) 2020 Omar Polo <op@omarpolo.com>
4 3e4749f7 2020-10-02 op * Permission to use, copy, modify, and distribute this software for any
5 3e4749f7 2020-10-02 op * purpose with or without fee is hereby granted, provided that the above
6 3e4749f7 2020-10-02 op * copyright notice and this permission notice appear in all copies.
8 3e4749f7 2020-10-02 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 3e4749f7 2020-10-02 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 3e4749f7 2020-10-02 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 3e4749f7 2020-10-02 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 3e4749f7 2020-10-02 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 3e4749f7 2020-10-02 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 3e4749f7 2020-10-02 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 3e4749f7 2020-10-02 op #include <sys/socket.h>
18 3e4749f7 2020-10-02 op #include <sys/stat.h>
20 3e4749f7 2020-10-02 op #include <netinet/in.h>
22 3e4749f7 2020-10-02 op #include <assert.h>
23 3e4749f7 2020-10-02 op #include <err.h>
24 3e4749f7 2020-10-02 op #include <fcntl.h>
25 3e4749f7 2020-10-02 op #include <stdio.h>
26 3e4749f7 2020-10-02 op #include <stdlib.h>
27 3e4749f7 2020-10-02 op #include <string.h>
28 3e4749f7 2020-10-02 op #include <tls.h>
29 3e4749f7 2020-10-02 op #include <unistd.h>
31 3e4749f7 2020-10-02 op #ifndef __OpenBSD__
32 3e4749f7 2020-10-02 op # define pledge(a, b) 0
33 3e4749f7 2020-10-02 op # define unveil(a, b) 0
34 3e4749f7 2020-10-02 op #endif /* __OpenBSD__ */
36 3e4749f7 2020-10-02 op #define GEMINI_URL_LEN (1024+3) /* URL max len + \r\n + \0 */
38 3e4749f7 2020-10-02 op /* large enough to hold a copy of a gemini URL and still have room */
39 3e4749f7 2020-10-02 op #define PATHBUF (2048)
41 3e4749f7 2020-10-02 op #define FILEBUF 1024
43 3e4749f7 2020-10-02 op #define SUCCESS 20
44 3e4749f7 2020-10-02 op #define NOT_FOUND 51
45 3e4749f7 2020-10-02 op #define BAD_REQUEST 59
50 3e4749f7 2020-10-02 op url_after_proto(char *url)
53 3e4749f7 2020-10-02 op const char *proto = "gemini";
54 3e4749f7 2020-10-02 op const char *marker = "://";
56 3e4749f7 2020-10-02 op if ((s = strstr(url, marker)) == NULL)
59 3e4749f7 2020-10-02 op /* not a gemini:// URL */
61 3e4749f7 2020-10-02 op if (s - strlen(proto) < url)
64 3e4749f7 2020-10-02 op /* if (strcmp(s - strlen(proto), proto)) */
65 3e4749f7 2020-10-02 op /* return NULL; */
67 3e4749f7 2020-10-02 op /* a valid gemini:// URL */
68 3e4749f7 2020-10-02 op return s + strlen(marker);
72 3e4749f7 2020-10-02 op url_start_of_request(char *url)
76 3e4749f7 2020-10-02 op if ((s = url_after_proto(url)) == NULL)
79 3e4749f7 2020-10-02 op if ((t = strstr(s, "/")) == NULL)
80 3e4749f7 2020-10-02 op return s + strlen(s);
85 3e4749f7 2020-10-02 op url_trim(char *url)
87 3e4749f7 2020-10-02 op const char *e = "\r\n";
90 3e4749f7 2020-10-02 op if ((s = strstr(url, e)) == NULL)
95 3e4749f7 2020-10-02 op if (s[2] != '\0') {
96 3e4749f7 2020-10-02 op fprintf(stderr, "last byte of request isn't NULL\n");
104 3e4749f7 2020-10-02 op adjust_path(char *path)
109 3e4749f7 2020-10-02 op /* /.. -> / */
110 3e4749f7 2020-10-02 op len = strlen(path);
111 3e4749f7 2020-10-02 op if (len >= 3) {
112 3e4749f7 2020-10-02 op if (!strcmp(&path[len-3], "/..")) {
113 3e4749f7 2020-10-02 op path[len-2] = '\0';
117 3e4749f7 2020-10-02 op /* if the path is only `..` trim out and exit */
118 3e4749f7 2020-10-02 op if (!strcmp(path, "..")) {
119 3e4749f7 2020-10-02 op path[0] = '\0';
123 3e4749f7 2020-10-02 op /* remove every ../ in the path */
125 3e4749f7 2020-10-02 op if ((s = strstr(path, "../")) == NULL)
127 3e4749f7 2020-10-02 op memmove(s - 3, s, strlen(s)+1); /* copy also the \0 */
132 3e4749f7 2020-10-02 op path_isdir(char *path)
136 3e4749f7 2020-10-02 op if (*path == '\0')
139 3e4749f7 2020-10-02 op len = strlen(path);
141 3e4749f7 2020-10-02 op /* len >= 1 */
142 3e4749f7 2020-10-02 op if (path[len-1] == '/')
149 3e4749f7 2020-10-02 op start_reply(struct tls *ctx, int code, const char *reason)
151 3e4749f7 2020-10-02 op char buf[1030] = {0}; /* status + ' ' + max reply len + \r\n\0 */
154 3e4749f7 2020-10-02 op len = snprintf(buf, sizeof(buf), "%d %s\r\n", code, reason);
155 3e4749f7 2020-10-02 op assert(len < (int)sizeof(buf));
156 3e4749f7 2020-10-02 op tls_write(ctx, buf, len);
160 3e4749f7 2020-10-02 op isdir(int fd)
162 3e4749f7 2020-10-02 op struct stat sb;
164 3e4749f7 2020-10-02 op if (fstat(fd, &sb) == -1) {
165 3e4749f7 2020-10-02 op warn("fstat");
166 3e4749f7 2020-10-02 op return 1; /* we'll fail later on anyway */
169 3e4749f7 2020-10-02 op return S_ISDIR(sb.st_mode);
172 3e4749f7 2020-10-02 op void senddir(char*, struct tls*);
175 3e4749f7 2020-10-02 op sendfile(char *path, struct tls *ctx)
178 3e4749f7 2020-10-02 op char fpath[PATHBUF];
179 3e4749f7 2020-10-02 op char buf[FILEBUF];
181 3e4749f7 2020-10-02 op ssize_t t, w;
183 3e4749f7 2020-10-02 op bzero(fpath, sizeof(fpath));
185 3e4749f7 2020-10-02 op if (*path != '.')
186 3e4749f7 2020-10-02 op strlcpy(fpath, ".", PATHBUF);
188 3e4749f7 2020-10-02 op strlcat(fpath, path, PATHBUF);
190 3e4749f7 2020-10-02 op if ((fd = openat(dirfd, fpath, O_RDONLY | O_NOFOLLOW)) == -1) {
191 3e4749f7 2020-10-02 op warn("open: %s", fpath);
192 3e4749f7 2020-10-02 op start_reply(ctx, NOT_FOUND, "not found");
196 3e4749f7 2020-10-02 op if (isdir(fd)) {
197 3e4749f7 2020-10-02 op warnx("%s is a directory, trying %s/index.gmi", fpath, fpath);
199 3e4749f7 2020-10-02 op senddir(fpath, ctx);
203 3e4749f7 2020-10-02 op /* assume it's a text/gemini file */
204 3e4749f7 2020-10-02 op start_reply(ctx, SUCCESS, "text/gemini");
207 3e4749f7 2020-10-02 op if ((w = read(fd, buf, sizeof(buf))) == -1) {
208 3e4749f7 2020-10-02 op warn("read: %s", fpath);
218 3e4749f7 2020-10-02 op while (w > 0) {
219 3e4749f7 2020-10-02 op if ((t = tls_write(ctx, buf + i, w)) == -1) {
220 3e4749f7 2020-10-02 op warnx("tls_write (path=%s) : %s",
222 3e4749f7 2020-10-02 op tls_error(ctx));
235 3e4749f7 2020-10-02 op senddir(char *path, struct tls *ctx)
237 3e4749f7 2020-10-02 op char fpath[PATHBUF];
240 3e4749f7 2020-10-02 op bzero(fpath, PATHBUF);
242 3e4749f7 2020-10-02 op if (*path != '.')
243 3e4749f7 2020-10-02 op fpath[0] = '.';
245 3e4749f7 2020-10-02 op /* this cannot fail since sizeof(fpath) > maxlen of path */
246 3e4749f7 2020-10-02 op strlcat(fpath, path, PATHBUF);
247 3e4749f7 2020-10-02 op len = strlen(fpath);
249 3e4749f7 2020-10-02 op /* add a trailing / in case. */
250 3e4749f7 2020-10-02 op if (fpath[len-1] != '/') {
251 3e4749f7 2020-10-02 op fpath[len] = '/';
254 3e4749f7 2020-10-02 op strlcat(fpath, "index.gmi", sizeof(fpath));
256 3e4749f7 2020-10-02 op sendfile(fpath, ctx);
260 3e4749f7 2020-10-02 op handle(char *url, struct tls *ctx)
264 3e4749f7 2020-10-02 op if (!url_trim(url)) {
265 3e4749f7 2020-10-02 op start_reply(ctx, BAD_REQUEST, "bad request");
269 3e4749f7 2020-10-02 op if ((path = url_start_of_request(url)) == NULL)
272 3e4749f7 2020-10-02 op adjust_path(path);
274 3e4749f7 2020-10-02 op fprintf(stderr, "requested path: %s\n", path);
276 3e4749f7 2020-10-02 op if (path_isdir(path))
277 3e4749f7 2020-10-02 op senddir(path, ctx);
279 3e4749f7 2020-10-02 op sendfile(path, ctx);
283 3e4749f7 2020-10-02 op make_socket(int port)
286 3e4749f7 2020-10-02 op struct sockaddr_in addr;
288 3e4749f7 2020-10-02 op if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
289 3e4749f7 2020-10-02 op err(1, "socket");
292 3e4749f7 2020-10-02 op if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
293 3e4749f7 2020-10-02 op err(1, "setsockopt(SO_REUSEADDR)");
296 3e4749f7 2020-10-02 op if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
297 3e4749f7 2020-10-02 op err(1, "setsockopt(SO_REUSEPORT)");
299 3e4749f7 2020-10-02 op bzero(&addr, sizeof(addr));
300 3e4749f7 2020-10-02 op addr.sin_family = AF_INET;
301 3e4749f7 2020-10-02 op addr.sin_port = htons(port);
302 3e4749f7 2020-10-02 op addr.sin_addr.s_addr = INADDR_ANY;
304 3e4749f7 2020-10-02 op if (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
305 3e4749f7 2020-10-02 op err(1, "bind");
307 3e4749f7 2020-10-02 op if (listen(sock, 16) == -1)
308 3e4749f7 2020-10-02 op err(1, "listen");
314 3e4749f7 2020-10-02 op loop(struct tls *ctx, int sock)
317 3e4749f7 2020-10-02 op struct sockaddr_in client;
318 3e4749f7 2020-10-02 op socklen_t len;
319 3e4749f7 2020-10-02 op struct tls *clientctx;
320 3e4749f7 2020-10-02 op char buf[GEMINI_URL_LEN];
324 3e4749f7 2020-10-02 op len = sizeof(client);
325 3e4749f7 2020-10-02 op if ((fd = accept(sock, (struct sockaddr*)&client, &len)) == -1)
326 3e4749f7 2020-10-02 op err(1, "accept");
328 3e4749f7 2020-10-02 op if (tls_accept_socket(ctx, &clientctx, fd) == -1) {
329 3e4749f7 2020-10-02 op warnx("tls_accept_socket: %s", tls_error(ctx));
333 3e4749f7 2020-10-02 op bzero(buf, GEMINI_URL_LEN);
334 3e4749f7 2020-10-02 op if ((r = tls_read(clientctx, buf, sizeof(buf)-1)) == -1) {
335 3e4749f7 2020-10-02 op warnx("tls_read: %s", tls_error(clientctx));
336 3e4749f7 2020-10-02 op goto clienterr;
339 3e4749f7 2020-10-02 op handle(buf, clientctx);
342 3e4749f7 2020-10-02 op if (tls_close(clientctx) == -1)
343 3e4749f7 2020-10-02 op warn("tls_close: client");
344 3e4749f7 2020-10-02 op tls_free(clientctx);
350 3e4749f7 2020-10-02 op usage(const char *me)
352 3e4749f7 2020-10-02 op fprintf(stderr,
353 3e4749f7 2020-10-02 op "USAGE: %s [-h] [-c cert.pem] [-d docs] [-k key.pem]\n",
358 3e4749f7 2020-10-02 op main(int argc, char **argv)
360 3e4749f7 2020-10-02 op const char *cert = "cert.pem", *key = "key.pem", *dir = "docs";
361 3e4749f7 2020-10-02 op struct tls *ctx = NULL;
362 3e4749f7 2020-10-02 op struct tls_config *conf;
365 3e4749f7 2020-10-02 op int sock, ch;
367 3e4749f7 2020-10-02 op while ((ch = getopt(argc, argv, "c:d:hk:")) != -1) {
368 3e4749f7 2020-10-02 op switch (ch) {
370 3e4749f7 2020-10-02 op cert = optarg;
374 3e4749f7 2020-10-02 op dir = optarg;
378 3e4749f7 2020-10-02 op usage(*argv);
382 3e4749f7 2020-10-02 op key = optarg;
386 3e4749f7 2020-10-02 op usage(*argv);
391 3e4749f7 2020-10-02 op if ((conf = tls_config_new()) == NULL)
392 3e4749f7 2020-10-02 op err(1, "tls_config_new");
394 3e4749f7 2020-10-02 op if ((m = tls_load_file(cert, &mlen, NULL)) == NULL)
395 3e4749f7 2020-10-02 op err(1, "tls_load_file: %s", cert);
396 3e4749f7 2020-10-02 op if (tls_config_set_cert_mem(conf, m, mlen) == -1)
397 3e4749f7 2020-10-02 op err(1, "tls_config_set_cert_mem: server certificate");
399 3e4749f7 2020-10-02 op if ((m = tls_load_file(key, &mlen, NULL)) == NULL)
400 3e4749f7 2020-10-02 op err(1, "tls_load_file: %s", key);
401 3e4749f7 2020-10-02 op if (tls_config_set_key_mem(conf, m, mlen) == -1)
402 3e4749f7 2020-10-02 op err(1, "tls_config_set_cert_mem: server key");
404 3e4749f7 2020-10-02 op if ((ctx = tls_server()) == NULL)
405 3e4749f7 2020-10-02 op err(1, "tls_server");
407 3e4749f7 2020-10-02 op if (tls_configure(ctx, conf) == -1)
408 3e4749f7 2020-10-02 op errx(1, "tls_configure: %s", tls_error(ctx));
410 3e4749f7 2020-10-02 op sock = make_socket(1965);
412 3e4749f7 2020-10-02 op if ((dirfd = open(dir, O_RDONLY | O_DIRECTORY)) == -1)
413 3e4749f7 2020-10-02 op err(1, "open: %s", dir);
415 3e4749f7 2020-10-02 op if (unveil(dir, "r") == -1)
416 3e4749f7 2020-10-02 op err(1, "unveil");
418 3e4749f7 2020-10-02 op if (pledge("stdio rpath inet", "") == -1)
419 3e4749f7 2020-10-02 op err(1, "pledge");
421 3e4749f7 2020-10-02 op loop(ctx, sock);
424 3e4749f7 2020-10-02 op printf("why im I here?\n");
429 3e4749f7 2020-10-02 op tls_free(ctx);
430 3e4749f7 2020-10-02 op tls_config_free(conf);