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 room */
39 #define PATHBUF (2048)
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, "last byte of request isn't NULL\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 - 3, s, strlen(s)+1); /* copy also the \0 */
132 path_isdir(char *path)
142 if (path[len-1] == '/')
149 start_reply(struct tls *ctx, int code, const char *reason)
151 char buf[1030] = {0}; /* status + ' ' + max reply len + \r\n\0 */
154 len = snprintf(buf, sizeof(buf), "%d %s\r\n", code, reason);
155 assert(len < (int)sizeof(buf));
156 tls_write(ctx, buf, len);
164 if (fstat(fd, &sb) == -1) {
166 return 1; /* we'll fail later on anyway */
169 return S_ISDIR(sb.st_mode);
172 void senddir(char*, struct tls*);
175 sendfile(char *path, struct tls *ctx)
183 bzero(fpath, sizeof(fpath));
186 strlcpy(fpath, ".", PATHBUF);
188 strlcat(fpath, path, PATHBUF);
190 if ((fd = openat(dirfd, fpath, O_RDONLY | O_NOFOLLOW)) == -1) {
191 warn("open: %s", fpath);
192 start_reply(ctx, NOT_FOUND, "not found");
197 warnx("%s is a directory, trying %s/index.gmi", fpath, fpath);
203 /* assume it's a text/gemini file */
204 start_reply(ctx, SUCCESS, "text/gemini");
207 if ((w = read(fd, buf, sizeof(buf))) == -1) {
208 warn("read: %s", fpath);
219 if ((t = tls_write(ctx, buf + i, w)) == -1) {
220 warnx("tls_write (path=%s) : %s",
235 senddir(char *path, struct tls *ctx)
240 bzero(fpath, PATHBUF);
245 /* this cannot fail since sizeof(fpath) > maxlen of path */
246 strlcat(fpath, path, PATHBUF);
249 /* add a trailing / in case. */
250 if (fpath[len-1] != '/') {
254 strlcat(fpath, "index.gmi", sizeof(fpath));
256 sendfile(fpath, ctx);
260 handle(char *url, struct tls *ctx)
264 if (!url_trim(url)) {
265 start_reply(ctx, BAD_REQUEST, "bad request");
269 if ((path = url_start_of_request(url)) == NULL)
274 fprintf(stderr, "requested path: %s\n", path);
276 if (path_isdir(path))
283 make_socket(int port)
286 struct sockaddr_in addr;
288 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
292 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
293 err(1, "setsockopt(SO_REUSEADDR)");
296 if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
297 err(1, "setsockopt(SO_REUSEPORT)");
299 bzero(&addr, sizeof(addr));
300 addr.sin_family = AF_INET;
301 addr.sin_port = htons(port);
302 addr.sin_addr.s_addr = INADDR_ANY;
304 if (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
307 if (listen(sock, 16) == -1)
314 loop(struct tls *ctx, int sock)
317 struct sockaddr_in client;
319 struct tls *clientctx;
320 char buf[GEMINI_URL_LEN];
324 len = sizeof(client);
325 if ((fd = accept(sock, (struct sockaddr*)&client, &len)) == -1)
328 if (tls_accept_socket(ctx, &clientctx, fd) == -1) {
329 warnx("tls_accept_socket: %s", tls_error(ctx));
333 bzero(buf, GEMINI_URL_LEN);
334 if ((r = tls_read(clientctx, buf, sizeof(buf)-1)) == -1) {
335 warnx("tls_read: %s", tls_error(clientctx));
339 handle(buf, clientctx);
342 if (tls_close(clientctx) == -1)
343 warn("tls_close: client");
350 usage(const char *me)
353 "USAGE: %s [-h] [-c cert.pem] [-d docs] [-k key.pem]\n",
358 main(int argc, char **argv)
360 const char *cert = "cert.pem", *key = "key.pem", *dir = "docs";
361 struct tls *ctx = NULL;
362 struct tls_config *conf;
367 while ((ch = getopt(argc, argv, "c:d:hk:")) != -1) {
391 if ((conf = tls_config_new()) == NULL)
392 err(1, "tls_config_new");
394 if ((m = tls_load_file(cert, &mlen, NULL)) == NULL)
395 err(1, "tls_load_file: %s", cert);
396 if (tls_config_set_cert_mem(conf, m, mlen) == -1)
397 err(1, "tls_config_set_cert_mem: server certificate");
399 if ((m = tls_load_file(key, &mlen, NULL)) == NULL)
400 err(1, "tls_load_file: %s", key);
401 if (tls_config_set_key_mem(conf, m, mlen) == -1)
402 err(1, "tls_config_set_cert_mem: server key");
404 if ((ctx = tls_server()) == NULL)
405 err(1, "tls_server");
407 if (tls_configure(ctx, conf) == -1)
408 errx(1, "tls_configure: %s", tls_error(ctx));
410 sock = make_socket(1965);
412 if ((dirfd = open(dir, O_RDONLY | O_DIRECTORY)) == -1)
413 err(1, "open: %s", dir);
415 if (unveil(dir, "r") == -1)
418 if (pledge("stdio rpath inet", "") == -1)
424 printf("why im I here?\n");
430 tls_config_free(conf);