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.
18 #include <sys/socket.h>
21 #include <arpa/inet.h>
22 #include <netinet/in.h>
38 # define pledge(a, b) 0
39 # define unveil(a, b) 0
40 #endif /* __OpenBSD__ */
46 #define GEMINI_URL_LEN (1024+3) /* URL max len + \r\n + \0 */
48 /* large enough to hold a copy of a gemini URL and still have extra room */
52 #define TEMP_FAILURE 40
54 #define BAD_REQUEST 59
60 #define SAFE_SETENV(var, val) do { \
61 const char *_tmp = (val); \
64 setenv((var), _tmp, 1); \
79 int fd, waiting_on_child;
81 char sbuf[1024]; /* static buffer */
82 void *buf, *i; /* mmap buffer */
83 ssize_t len, off; /* mmap/static buffer */
95 struct etm { /* file extension to mime */
99 {"application/pdf", "pdf"},
101 {"image/gif", "gif"},
102 {"image/jpeg", "jpg"},
103 {"image/jpeg", "jpeg"},
104 {"image/png", "png"},
105 {"image/svg+xml", "svg"},
107 {"text/gemini", "gemini"},
108 {"text/gemini", "gmi"},
109 {"text/markdown", "markdown"},
110 {"text/markdown", "md"},
111 {"text/plain", "txt"},
117 #define LOG(priority, c, fmt, ...) \
119 char buf[INET_ADDRSTRLEN]; \
120 if (inet_ntop((c)->af, &(c)->addr, \
121 buf, sizeof(buf)) == NULL) \
122 FATAL("inet_ntop: %s", strerror(errno)); \
125 "%s " fmt "\n", buf, __VA_ARGS__); \
127 syslog((priority) | LOG_DAEMON, \
128 "%s " fmt, buf, __VA_ARGS__); \
131 #define LOGE(c, fmt, ...) LOG(LOG_ERR, c, fmt, __VA_ARGS__)
132 #define LOGN(c, fmt, ...) LOG(LOG_NOTICE, c, fmt, __VA_ARGS__)
133 #define LOGI(c, fmt, ...) LOG(LOG_INFO, c, fmt, __VA_ARGS__)
134 #define LOGD(c, fmt, ...) LOG(LOG_DEBUG, c, fmt, __VA_ARGS__)
136 #define FATAL(fmt, ...) \
139 fprintf(stderr, fmt "\n", __VA_ARGS__); \
141 syslog(LOG_DAEMON | LOG_CRIT, \
146 const char *dir, *cgi;
150 int connected_clients;
152 void siginfo_handler(int);
153 int starts_with(const char*, const char*);
155 char *url_after_proto(char*);
156 char *url_start_of_request(char*);
157 int url_trim(struct client*, char*);
158 char *adjust_path(char*);
159 ssize_t filesize(int);
161 int start_reply(struct pollfd*, struct client*, int, const char*);
162 const char *path_ext(const char*);
163 const char *mime(const char*);
164 int check_path(struct client*, const char*, int*);
165 int check_for_cgi(char *, char*, struct pollfd*, struct client*);
166 int open_file(char*, char*, struct pollfd*, struct client*);
167 int start_cgi(const char*, const char*, const char*, struct pollfd*, struct client*);
168 void cgi_setpoll_on_child(struct pollfd*, struct client*);
169 void cgi_setpoll_on_client(struct pollfd*, struct client*);
170 void handle_cgi(struct pollfd*, struct client*);
171 void send_file(char*, char*, struct pollfd*, struct client*);
172 void send_dir(char*, struct pollfd*, struct client*);
173 void handle(struct pollfd*, struct client*);
175 void mark_nonblock(int);
177 void do_accept(int, struct tls*, struct pollfd*, struct client*);
178 void goodbye(struct pollfd*, struct client*);
179 void loop(struct tls*, int);
181 void usage(const char*);
184 siginfo_handler(int sig)
190 starts_with(const char *str, const char *prefix)
194 for (i = 0; prefix[i] != '\0'; ++i)
195 if (str[i] != prefix[i])
201 url_after_proto(char *url)
204 const char *proto = "gemini:";
205 const char *marker = "//";
208 if ((s = strstr(url, marker)) == NULL)
212 * if a protocol is not specified, gemini should be implied:
213 * this handles the case of //example.com
216 return s + strlen(marker);
218 if (s - strlen(proto) != url)
221 if (!starts_with(url, proto))
224 return s + strlen(marker);
228 url_start_of_request(char *url)
232 if ((s = url_after_proto(url)) == NULL)
235 /* non-absolute URL */
239 if ((t = strstr(s, "/")) == NULL)
240 return s + strlen(s);
245 url_trim(struct client *c, char *url)
247 const char *e = "\r\n";
250 if ((s = strstr(url, e)) == NULL)
256 LOGE(c, "%s", "request longer than 1024 bytes");
264 adjust_path(char *path)
269 if ((query = strchr(path, '?')) != NULL) {
277 if (!strcmp(&path[len-3], "/..")) {
282 /* if the path is only `..` trim out and exit */
283 if (!strcmp(path, "..")) {
288 /* remove every ../ in the path */
290 if ((s = strstr(path, "../")) == NULL)
292 memmove(s, s+3, strlen(s)+1); /* copy also the \0 */
297 start_reply(struct pollfd *pfd, struct client *client, int code, const char *reason)
299 char buf[1030] = {0}; /* status + ' ' + max reply len + \r\n\0 */
304 client->meta = reason;
305 client->state = S_INITIALIZING;
307 len = snprintf(buf, sizeof(buf), "%d %s\r\n", code, reason);
308 assert(len < (int)sizeof(buf));
309 ret = tls_write(client->ctx, buf, len);
310 if (ret == TLS_WANT_POLLIN) {
311 pfd->events = POLLIN;
315 if (ret == TLS_WANT_POLLOUT) {
316 pfd->events = POLLOUT;
328 if ((len = lseek(fd, 0, SEEK_END)) == -1)
330 if (lseek(fd, 0, SEEK_SET) == -1)
336 path_ext(const char *path)
340 end = path + strlen(path)-1; /* the last byte before the NUL */
341 for (; end != path; --end) {
352 mime(const char *path)
354 const char *ext, *def = "application/octet-stream";
357 if ((ext = path_ext(path)) == NULL)
360 for (t = filetypes; t->mime != NULL; ++t)
361 if (!strcmp(ext, t->ext))
368 check_path(struct client *c, const char *path, int *fd)
372 assert(path != NULL);
373 if ((*fd = openat(dirfd, path,
374 O_RDONLY | O_NOFOLLOW | O_CLOEXEC)) == -1) {
378 if (fstat(*fd, &sb) == -1) {
379 LOGN(c, "failed stat for %s: %s", path, strerror(errno));
383 if (S_ISDIR(sb.st_mode))
384 return FILE_DIRECTORY;
386 if (sb.st_mode & S_IXUSR)
387 return FILE_EXECUTABLE;
393 * the inverse of this algorithm, i.e. starting from the start of the
394 * path + strlen(cgi), and checking if each component, should be
395 * faster. But it's tedious to write. This does the opposite: starts
396 * from the end and strip one component at a time, until either an
397 * executable is found or we emptied the path.
400 check_for_cgi(char *path, char *query, struct pollfd *fds, struct client *c)
403 end = strchr(path, '\0');
405 /* NB: assume CGI is enabled and path matches cgi */
408 /* go up one level. UNIX paths are simple and POSIX
409 * dirname, with its ambiguities on if the given path
410 * is changed or not, gives me headaches. */
415 switch (check_path(c, path, &c->fd)) {
416 case FILE_EXECUTABLE:
417 return start_cgi(path, end+1, query, fds,c);
429 if (!start_reply(fds, c, NOT_FOUND, "not found"))
437 open_file(char *path, char *query, struct pollfd *fds, struct client *c)
441 bzero(fpath, sizeof(fpath));
445 strlcat(fpath, path, PATHBUF);
447 switch (check_path(c, fpath, &c->fd)) {
448 case FILE_EXECUTABLE:
449 /* +2 to skip the ./ */
450 if (cgi != NULL && starts_with(fpath+2, cgi))
451 return start_cgi(fpath, "", query, fds, c);
456 if ((c->len = filesize(c->fd)) == -1) {
457 LOGE(c, "failed to get file size for %s", fpath);
462 if ((c->buf = mmap(NULL, c->len, PROT_READ, MAP_PRIVATE,
463 c->fd, 0)) == MAP_FAILED) {
464 warn("mmap: %s", fpath);
469 return start_reply(fds, c, SUCCESS, mime(fpath));
472 LOGD(c, "%s is a directory, trying %s/index.gmi", fpath, fpath);
475 send_dir(fpath, fds, c);
479 if (cgi != NULL && starts_with(fpath+2, cgi))
480 return check_for_cgi(fpath, query, fds, c);
482 if (!start_reply(fds, c, NOT_FOUND, "not found"))
494 start_cgi(const char *spath, const char *relpath, const char *query,
495 struct pollfd *fds, struct client *c)
498 int p[2]; /* read end, write end */
503 switch (pid = fork()) {
507 case 0: { /* child */
508 char *ex, *requri, *portno;
509 char addr[INET_ADDRSTRLEN];
510 char *argv[] = { NULL, NULL, NULL };
515 if (dup2(p[1], 1) == -1)
518 if (inet_ntop(c->af, &c->addr, addr, sizeof(addr)) == NULL)
521 if (asprintf(&portno, "%d", port) == -1)
524 if (asprintf(&ex, "%s%s", dir, spath+1) == -1)
527 if (asprintf(&requri, "%s%s%s", spath,
528 *relpath == '\0' ? "" : "/",
532 argv[0] = argv[1] = ex;
535 SAFE_SETENV("GATEWAY_INTERFACE", "CGI/1.1");
536 SAFE_SETENV("SERVER_SOFTWARE", "gmid");
537 SAFE_SETENV("SERVER_PORT", portno);
538 /* setenv("SERVER_NAME", "", 1); */
539 SAFE_SETENV("SCRIPT_NAME", spath);
540 SAFE_SETENV("SCRIPT_EXECUTABLE", ex);
541 SAFE_SETENV("REQUEST_URI", requri);
542 SAFE_SETENV("REQUEST_RELATIVE", relpath);
543 SAFE_SETENV("QUERY_STRING", query);
544 SAFE_SETENV("REMOTE_HOST", addr);
545 SAFE_SETENV("REMOTE_ADDR", addr);
546 SAFE_SETENV("DOCUMENT_ROOT", dir);
548 if (tls_peer_cert_provided(c->ctx)) {
549 SAFE_SETENV("AUTH_TYPE", "Certificate");
550 SAFE_SETENV("REMOTE_USER", tls_peer_cert_subject(c->ctx));
551 SAFE_SETENV("TLS_CLIENT_ISSUER", tls_peer_cert_issuer(c->ctx));
552 SAFE_SETENV("TLS_CLIENT_HASH", tls_peer_cert_hash(c->ctx));
559 default: /* parent */
564 mark_nonblock(c->fd);
565 c->state = S_SENDING;
571 if (!start_reply(fds, c, TEMP_FAILURE, "internal server error"))
577 dprintf(p[1], "%d internal server error\r\n", TEMP_FAILURE);
583 cgi_setpoll_on_child(struct pollfd *fds, struct client *c)
587 if (c->waiting_on_child)
589 c->waiting_on_child = 1;
591 fds->events = POLLIN;
599 cgi_setpoll_on_client(struct pollfd *fds, struct client *c)
603 if (!c->waiting_on_child)
605 c->waiting_on_child = 0;
613 handle_cgi(struct pollfd *fds, struct client *c)
617 /* ensure c->fd is the child and fds->fd the client */
618 cgi_setpoll_on_client(fds, c);
622 if ((r = read(c->fd, c->sbuf, sizeof(c->sbuf))) == 0)
625 if (errno == EAGAIN || errno == EWOULDBLOCK) {
626 cgi_setpoll_on_child(fds, c);
636 switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
640 case TLS_WANT_POLLOUT:
641 fds->events = POLLOUT;
644 case TLS_WANT_POLLIN:
645 fds->events = POLLIN;
661 send_file(char *path, char *query, struct pollfd *fds, struct client *c)
666 if (!open_file(path, query, fds, c))
668 c->state = S_SENDING;
671 len = (c->buf + c->len) - c->i;
674 switch (ret = tls_write(c->ctx, c->i, len)) {
676 LOGE(c, "tls_write: %s", tls_error(c->ctx));
680 case TLS_WANT_POLLIN:
681 fds->events = POLLIN;
684 case TLS_WANT_POLLOUT:
685 fds->events = POLLOUT;
699 send_dir(char *path, struct pollfd *fds, struct client *client)
704 bzero(fpath, PATHBUF);
709 /* this cannot fail since sizeof(fpath) > maxlen of path */
710 strlcat(fpath, path, PATHBUF);
713 /* add a trailing / in case. */
714 if (fpath[len-1] != '/') {
718 strlcat(fpath, "index.gmi", sizeof(fpath));
720 send_file(fpath, NULL, fds, client);
724 handle(struct pollfd *fds, struct client *client)
726 char buf[GEMINI_URL_LEN];
730 switch (client->state) {
732 bzero(buf, GEMINI_URL_LEN);
733 switch (tls_read(client->ctx, buf, sizeof(buf)-1)) {
735 LOGE(client, "tls_read: %s", tls_error(client->ctx));
736 goodbye(fds, client);
739 case TLS_WANT_POLLIN:
740 fds->events = POLLIN;
743 case TLS_WANT_POLLOUT:
744 fds->events = POLLOUT;
748 if (!url_trim(client, buf)) {
749 if (!start_reply(fds, client, BAD_REQUEST, "bad request"))
751 goodbye(fds, client);
755 if ((path = url_start_of_request(buf)) == NULL) {
756 if (!start_reply(fds, client, BAD_REQUEST, "bad request"))
758 goodbye(fds, client);
762 query = adjust_path(path);
763 LOGI(client, "GET %s%s%s", path,
767 send_file(path, query, fds, client);
771 if (!start_reply(fds, client, client->code, client->meta))
774 if (client->code != SUCCESS) {
775 /* we don't need a body */
776 goodbye(fds, client);
780 client->state = S_SENDING;
785 if (client->child != -1)
786 handle_cgi(fds, client);
788 send_file(NULL, NULL, fds, client);
792 goodbye(fds, client);
802 mark_nonblock(int fd)
806 if ((flags = fcntl(fd, F_GETFL)) == -1)
807 FATAL("fcntl(F_GETFL): %s", strerror(errno));
808 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
809 FATAL("fcntl(F_SETFL): %s", strerror(errno));
813 make_socket(int port, int family)
816 struct sockaddr_in addr4;
817 struct sockaddr_in6 addr6;
818 struct sockaddr *addr;
823 bzero(&addr4, sizeof(addr4));
824 addr4.sin_family = family;
825 addr4.sin_port = htons(port);
826 addr4.sin_addr.s_addr = INADDR_ANY;
827 addr = (struct sockaddr*)&addr4;
832 bzero(&addr6, sizeof(addr6));
833 addr6.sin6_family = AF_INET6;
834 addr6.sin6_port = htons(port);
835 addr6.sin6_addr = in6addr_any;
836 addr = (struct sockaddr*)&addr6;
845 if ((sock = socket(family, SOCK_STREAM, 0)) == -1)
846 FATAL("socket: %s", strerror(errno));
849 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
850 FATAL("setsockopt(SO_REUSEADDR): %s", strerror(errno));
853 if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
854 FATAL("setsockopt(SO_REUSEPORT): %s", strerror(errno));
858 if (bind(sock, addr, len) == -1)
859 FATAL("bind: %s", strerror(errno));
861 if (listen(sock, 16) == -1)
862 FATAL("listen: %s", strerror(errno));
868 do_accept(int sock, struct tls *ctx, struct pollfd *fds, struct client *clients)
871 struct sockaddr_in addr;
875 if ((fd = accept(sock, (struct sockaddr*)&addr, &len)) == -1) {
876 if (errno == EWOULDBLOCK)
878 FATAL("accept: %s", strerror(errno));
883 for (i = 0; i < MAX_USERS; ++i) {
884 if (fds[i].fd == -1) {
885 bzero(&clients[i], sizeof(struct client));
886 if (tls_accept_socket(ctx, &clients[i].ctx, fd) == -1)
887 break; /* goodbye fd! */
890 fds[i].events = POLLIN;
892 clients[i].state = S_OPEN;
894 clients[i].child = -1;
895 clients[i].buf = MAP_FAILED;
896 clients[i].af = AF_INET;
897 clients[i].addr = addr.sin_addr;
908 goodbye(struct pollfd *pfd, struct client *c)
912 c->state = S_CLOSING;
914 ret = tls_close(c->ctx);
915 if (ret == TLS_WANT_POLLIN) {
916 pfd->events = POLLIN;
919 if (ret == TLS_WANT_POLLOUT) {
920 pfd->events = POLLOUT;
929 if (c->buf != MAP_FAILED)
930 munmap(c->buf, c->len);
940 loop(struct tls *ctx, int sock)
943 struct client clients[MAX_USERS];
944 struct pollfd fds[MAX_USERS];
946 for (i = 0; i < MAX_USERS; ++i) {
948 fds[i].events = POLLIN;
949 bzero(&clients[i], sizeof(struct client));
955 if ((todo = poll(fds, MAX_USERS, INFTIM)) == -1) {
956 if (errno == EINTR) {
957 warnx("connected clients: %d",
961 FATAL("poll: %s", strerror(errno));
964 for (i = 0; i < MAX_USERS; i++) {
965 assert(i < MAX_USERS);
967 if (fds[i].revents == 0)
970 if (fds[i].revents & (POLLERR|POLLNVAL))
971 FATAL("bad fd %d: %s", fds[i].fd,
974 if (fds[i].revents & POLLHUP) {
975 /* fds[i] may be the fd of the stdin
976 * of a cgi script that has exited. */
977 if (!clients[i].waiting_on_child) {
978 goodbye(&fds[i], &clients[i]);
985 if (i == 0) { /* new client */
986 do_accept(sock, ctx, fds, clients);
990 handle(&fds[i], &clients[i]);
996 usage(const char *me)
999 "USAGE: %s [-h] [-c cert.pem] [-d docs] [-k key.pem] "
1000 "[-l logfile] [-p port] [-x cgi-bin]\n",
1005 main(int argc, char **argv)
1007 const char *cert = "cert.pem", *key = "key.pem";
1008 struct tls *ctx = NULL;
1009 struct tls_config *conf;
1012 signal(SIGPIPE, SIG_IGN);
1013 signal(SIGCHLD, SIG_IGN);
1016 signal(SIGINFO, siginfo_handler);
1018 signal(SIGUSR2, siginfo_handler);
1020 connected_clients = 0;
1027 while ((ch = getopt(argc, argv, "c:d:fhk:p:x:")) != -1) {
1054 lval = strtol(optarg, &ep, 10);
1055 if (optarg[0] == '\0' || *ep != '\0')
1056 err(1, "not a number: %s", optarg);
1057 if (lval < 0 || lval > UINT16_MAX)
1058 err(1, "port number out of range: %s", optarg);
1073 if ((conf = tls_config_new()) == NULL)
1074 err(1, "tls_config_new");
1076 /* optionally accept client certs, but don't try to verify them */
1077 tls_config_verify_client_optional(conf);
1078 tls_config_insecure_noverifycert(conf);
1080 if (tls_config_set_protocols(conf,
1081 TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3) == -1)
1082 err(1, "tls_config_set_protocols");
1084 if (tls_config_set_cert_file(conf, cert) == -1)
1085 err(1, "tls_config_set_cert_file: %s", cert);
1087 if (tls_config_set_key_file(conf, key) == -1)
1088 err(1, "tls_config_set_key_file: %s", key);
1090 if ((ctx = tls_server()) == NULL)
1091 err(1, "tls_server");
1093 if (tls_configure(ctx, conf) == -1)
1094 errx(1, "tls_configure: %s", tls_error(ctx));
1096 sock = make_socket(port, AF_INET);
1098 if ((dirfd = open(dir, O_RDONLY | O_DIRECTORY)) == -1)
1099 err(1, "open: %s", dir);
1101 if (!foreground && daemon(0, 1) == -1)
1105 if (unveil(dir, "rx") == -1)
1107 if (pledge("stdio rpath inet proc exec", NULL) == -1)
1110 if (unveil(dir, "r") == -1)
1112 if (pledge("stdio rpath inet", NULL) == -1)
1120 tls_config_free(conf);