2 d5aba4c7 2020-12-24 op * Copyright (c) 2020 Omar Polo <op@omarpolo.com>
4 d5aba4c7 2020-12-24 op * Permission to use, copy, modify, and distribute this software for any
5 d5aba4c7 2020-12-24 op * purpose with or without fee is hereby granted, provided that the above
6 d5aba4c7 2020-12-24 op * copyright notice and this permission notice appear in all copies.
8 d5aba4c7 2020-12-24 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 d5aba4c7 2020-12-24 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 d5aba4c7 2020-12-24 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 d5aba4c7 2020-12-24 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 d5aba4c7 2020-12-24 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 d5aba4c7 2020-12-24 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 d5aba4c7 2020-12-24 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 488f059a 2020-12-24 op #ifndef GMID_H
18 488f059a 2020-12-24 op #define GMID_H
20 252908e6 2021-01-24 op #include <sys/socket.h>
21 252908e6 2021-01-24 op #include <sys/types.h>
23 488f059a 2020-12-24 op #include <arpa/inet.h>
24 488f059a 2020-12-24 op #include <netinet/in.h>
26 252908e6 2021-01-24 op #include <dirent.h>
27 488f059a 2020-12-24 op #include <poll.h>
28 488f059a 2020-12-24 op #include <stdio.h>
29 488f059a 2020-12-24 op #include <stdlib.h>
30 488f059a 2020-12-24 op #include <syslog.h>
31 488f059a 2020-12-24 op #include <tls.h>
32 488f059a 2020-12-24 op #include <unistd.h>
34 12042ad7 2021-01-21 op #include "config.h"
36 488f059a 2020-12-24 op #ifndef INFTIM
37 488f059a 2020-12-24 op # define INFTIM -1
40 488f059a 2020-12-24 op #define GEMINI_URL_LEN (1024+3) /* URL max len + \r\n + \0 */
42 488f059a 2020-12-24 op /* large enough to hold a copy of a gemini URL and still have extra room */
43 488f059a 2020-12-24 op #define PATHBUF 2048
45 488f059a 2020-12-24 op #define SUCCESS 20
46 0be51733 2021-01-20 op #define TEMP_REDIRECT 30
47 488f059a 2020-12-24 op #define TEMP_FAILURE 40
48 488f059a 2020-12-24 op #define NOT_FOUND 51
49 7b1d9790 2021-01-11 op #define PROXY_REFUSED 53
50 488f059a 2020-12-24 op #define BAD_REQUEST 59
52 488f059a 2020-12-24 op #define MAX_USERS 64
54 15902770 2021-01-15 op #define HOSTSLEN 64
55 c8b74339 2021-01-24 op #define LOCLEN 32
57 3300cbe0 2021-01-27 op /* RFC1034 imposes this limit. 63+1 for the NUL-terminator */
58 3300cbe0 2021-01-27 op #define DOMAIN_NAME_LEN (63+1)
60 dafb57b8 2021-01-15 op #define LOGE(c, fmt, ...) logs(LOG_ERR, c, fmt, __VA_ARGS__)
61 dafb57b8 2021-01-15 op #define LOGW(c, fmt, ...) logs(LOG_WARNING, c, fmt, __VA_ARGS__)
62 dafb57b8 2021-01-15 op #define LOGN(c, fmt, ...) logs(LOG_NOTICE, c, fmt, __VA_ARGS__)
63 dafb57b8 2021-01-15 op #define LOGI(c, fmt, ...) logs(LOG_INFO, c, fmt, __VA_ARGS__)
64 dafb57b8 2021-01-15 op #define LOGD(c, fmt, ...) logs(LOG_DEBUG, c, fmt, __VA_ARGS__)
66 c8b74339 2021-01-24 op struct location {
69 c8b74339 2021-01-24 op char *default_mime;
71 252908e6 2021-01-24 op int auto_index; /* 0 auto, -1 off, 1 on */
74 15902770 2021-01-15 op struct vhost {
75 15902770 2021-01-15 op const char *domain;
76 15902770 2021-01-15 op const char *cert;
77 15902770 2021-01-15 op const char *key;
78 15902770 2021-01-15 op const char *dir;
79 15902770 2021-01-15 op const char *cgi;
81 c8b74339 2021-01-24 op struct location locations[LOCLEN];
84 15902770 2021-01-15 op extern struct vhost hosts[HOSTSLEN];
86 a010b0dd 2021-01-18 op struct etm { /* extension to mime */
87 a010b0dd 2021-01-18 op const char *mime;
88 a010b0dd 2021-01-18 op const char *ext;
92 a010b0dd 2021-01-18 op struct etm *t;
100 ae08ec7d 2021-01-25 op uint32_t protos;
101 ae08ec7d 2021-01-25 op struct mime mime;
102 ae08ec7d 2021-01-25 op char *chroot;
106 15902770 2021-01-15 op extern struct conf conf;
107 881a9dd9 2021-01-16 op extern int exfd;
110 0be51733 2021-01-20 op char *schema;
113 0be51733 2021-01-20 op uint16_t port_no;
116 0be51733 2021-01-20 op char *fragment;
119 0be51733 2021-01-20 op struct parser {
121 0be51733 2021-01-20 op struct iri *parsed;
122 0be51733 2021-01-20 op const char *err;
128 488f059a 2020-12-24 op S_INITIALIZING,
129 a87f6625 2021-01-24 op S_SENDING_FILE,
130 252908e6 2021-01-24 op S_SENDING_DIR,
131 a87f6625 2021-01-24 op S_SENDING_CGI,
135 488f059a 2020-12-24 op struct client {
136 488f059a 2020-12-24 op struct tls *ctx;
137 0be51733 2021-01-20 op char req[GEMINI_URL_LEN];
138 0be51733 2021-01-20 op struct iri iri;
139 3300cbe0 2021-01-27 op char domain[DOMAIN_NAME_LEN];
140 a87f6625 2021-01-24 op int state, next;
142 488f059a 2020-12-24 op const char *meta;
143 488f059a 2020-12-24 op int fd, waiting_on_child;
144 488f059a 2020-12-24 op char sbuf[1024]; /* static buffer */
145 488f059a 2020-12-24 op void *buf, *i; /* mmap buffer */
146 488f059a 2020-12-24 op ssize_t len, off; /* mmap/static buffer */
148 709d6e5e 2021-01-10 op struct sockaddr_storage addr;
149 f7b816dc 2021-01-15 op struct vhost *host; /* host she's talking to */
154 488f059a 2020-12-24 op FILE_EXECUTABLE,
155 488f059a 2020-12-24 op FILE_DIRECTORY,
156 488f059a 2020-12-24 op FILE_MISSING,
161 d3a08f4d 2021-01-17 op __attribute__((format (printf, 1, 2)))
162 d3a08f4d 2021-01-17 op __attribute__((__noreturn__))
163 d3a08f4d 2021-01-17 op void fatal(const char*, ...);
165 d3a08f4d 2021-01-17 op __attribute__((format (printf, 3, 4)))
166 d3a08f4d 2021-01-17 op void logs(int, struct client*, const char*, ...);
167 0be51733 2021-01-20 op void log_request(struct client*, char*, size_t);
169 4a28dd01 2020-12-28 op void sig_handler(int);
170 488f059a 2020-12-24 op int starts_with(const char*, const char*);
171 f77a8c86 2021-01-21 op int ends_with(const char*, const char*);
172 3d9a1c73 2020-12-28 op ssize_t filesize(int);
173 15902770 2021-01-15 op char *absolutify_path(const char*);
174 8443bff7 2021-01-25 op void gen_certificate(const char*, const char*, const char*);
175 8443bff7 2021-01-25 op void mkdirs(const char*);
176 8443bff7 2021-01-25 op char *data_dir(void);
177 8443bff7 2021-01-25 op void load_local_cert(const char*, const char*);
178 15902770 2021-01-15 op void yyerror(const char*);
179 15902770 2021-01-15 op int parse_portno(const char*);
180 15902770 2021-01-15 op void parse_conf(const char*);
181 ae08ec7d 2021-01-25 op void load_vhosts(void);
182 33ac26a0 2021-01-21 op int make_socket(int, int);
183 ae08ec7d 2021-01-25 op void setup_tls(void);
184 33ac26a0 2021-01-21 op int listener_main(void);
185 c8b74339 2021-01-24 op void init_config(void);
186 ae08ec7d 2021-01-25 op void drop_priv(void);
187 488f059a 2020-12-24 op void usage(const char*);
189 f7b816dc 2021-01-15 op /* provided by lex/yacc */
190 15902770 2021-01-15 op extern FILE *yyin;
191 15902770 2021-01-15 op extern int yylineno;
192 15902770 2021-01-15 op extern int yyparse(void);
193 15902770 2021-01-15 op extern int yylex(void);
196 b2a6b613 2021-01-21 op void init_mime(struct mime*);
197 b2a6b613 2021-01-21 op void add_mime(struct mime*, const char*, const char*);
198 b2a6b613 2021-01-21 op void load_default_mime(struct mime*);
199 6119e13e 2021-01-19 op const char *mime(struct vhost*, const char*);
201 d3a08f4d 2021-01-17 op /* server.c */
202 c8b74339 2021-01-24 op const char *vhost_lang(struct vhost*, const char*);
203 c8b74339 2021-01-24 op const char *vhost_default_mime(struct vhost*, const char*);
204 c8b74339 2021-01-24 op const char *vhost_index(struct vhost*, const char*);
205 252908e6 2021-01-24 op int vhost_auto_index(struct vhost*, const char*);
206 d3a08f4d 2021-01-17 op int check_path(struct client*, const char*, int*);
207 07b0a142 2021-01-24 op void open_file(struct pollfd*, struct client*);
208 252908e6 2021-01-24 op void load_file(struct pollfd*, struct client*);
209 07b0a142 2021-01-24 op void check_for_cgi(char *, char*, struct pollfd*, struct client*);
210 d3a08f4d 2021-01-17 op void mark_nonblock(int);
211 d3a08f4d 2021-01-17 op void handle_handshake(struct pollfd*, struct client*);
212 d3a08f4d 2021-01-17 op void handle_open_conn(struct pollfd*, struct client*);
213 a87f6625 2021-01-24 op void start_reply(struct pollfd*, struct client*, int, const char*);
214 07b0a142 2021-01-24 op void start_cgi(const char*, const char*, const char*, struct pollfd*, struct client*);
215 0be51733 2021-01-20 op void send_file(struct pollfd*, struct client*);
216 252908e6 2021-01-24 op void open_dir(struct pollfd*, struct client*);
217 252908e6 2021-01-24 op void redirect_canonical_dir(struct pollfd*, struct client*);
218 252908e6 2021-01-24 op int read_next_dir_entry(struct client*);
219 252908e6 2021-01-24 op void send_directory_listing(struct pollfd*, struct client*);
220 d3a08f4d 2021-01-17 op void cgi_poll_on_child(struct pollfd*, struct client*);
221 d3a08f4d 2021-01-17 op void cgi_poll_on_client(struct pollfd*, struct client*);
222 d3a08f4d 2021-01-17 op void handle_cgi(struct pollfd*, struct client*);
223 36162ed8 2021-01-22 op void close_conn(struct pollfd*, struct client*);
224 d3a08f4d 2021-01-17 op void do_accept(int, struct tls*, struct pollfd*, struct client*);
225 d3a08f4d 2021-01-17 op void handle(struct pollfd*, struct client*);
226 d3a08f4d 2021-01-17 op void loop(struct tls*, int, int);
229 881a9dd9 2021-01-16 op int send_string(int, const char*);
230 881a9dd9 2021-01-16 op int recv_string(int, char**);
231 881a9dd9 2021-01-16 op int send_vhost(int, struct vhost*);
232 881a9dd9 2021-01-16 op int recv_vhost(int, struct vhost**);
233 881a9dd9 2021-01-16 op int send_fd(int, int);
234 881a9dd9 2021-01-16 op int recv_fd(int);
235 881a9dd9 2021-01-16 op int executor_main(int);
237 dafb57b8 2021-01-15 op /* sandbox.c */
238 33ac26a0 2021-01-21 op void sandbox(void);
241 ef04b551 2021-01-09 op int valid_multibyte_utf8(struct parser*);
242 3300cbe0 2021-01-27 op char *utf8_nth(char*, size_t);
245 3c1cf9d0 2021-01-11 op int parse_iri(char*, struct iri*, const char**);
246 3c1cf9d0 2021-01-11 op int trim_req_iri(char*);
249 7957cbd9 2021-01-27 op int puny_decode(const char*, char*, size_t);