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 52418c8d 2021-02-12 op #include "config.h"
22 252908e6 2021-01-24 op #include <sys/socket.h>
23 252908e6 2021-01-24 op #include <sys/types.h>
25 488f059a 2020-12-24 op #include <arpa/inet.h>
26 488f059a 2020-12-24 op #include <netinet/in.h>
28 252908e6 2021-01-24 op #include <dirent.h>
29 8ad1c570 2021-05-09 op #include <event.h>
30 bc99d868 2021-03-19 op #include <limits.h>
31 bc99d868 2021-03-19 op #include <netdb.h>
32 ca21e100 2021-02-04 op #include <signal.h>
33 488f059a 2020-12-24 op #include <stdio.h>
34 488f059a 2020-12-24 op #include <stdlib.h>
35 b63e30ff 2021-02-07 op #include <time.h>
36 488f059a 2020-12-24 op #include <tls.h>
37 488f059a 2020-12-24 op #include <unistd.h>
39 02be96c6 2021-02-09 op #include <openssl/x509.h>
41 ce2c9edb 2021-05-15 op #define GMID_VERSION "gmid/1.7"
43 488f059a 2020-12-24 op #define GEMINI_URL_LEN (1024+3) /* URL max len + \r\n + \0 */
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 35744950 2021-02-01 op #define CGI_ERROR 42
49 488f059a 2020-12-24 op #define NOT_FOUND 51
50 7b1d9790 2021-01-11 op #define PROXY_REFUSED 53
51 488f059a 2020-12-24 op #define BAD_REQUEST 59
52 02be96c6 2021-02-09 op #define CLIENT_CERT_REQ 60
53 02be96c6 2021-02-09 op #define CERT_NOT_AUTH 61
55 488f059a 2020-12-24 op #define MAX_USERS 64
57 35cf19e3 2021-01-28 op /* maximum hostname and label length, +1 for the NUL-terminator */
58 35cf19e3 2021-01-28 op #define DOMAIN_NAME_LEN (253+1)
59 35cf19e3 2021-01-28 op #define LABEL_LEN (63+1)
61 8ad1c570 2021-05-09 op #define FCGI_MAX 32
62 2c3e53da 2021-03-03 op #define PROC_MAX 16
70 8ad1c570 2021-05-09 op struct event e;
72 8ad1c570 2021-05-09 op #define FCGI_OFF 0
73 8ad1c570 2021-05-09 op #define FCGI_INFLIGHT 1
74 8ad1c570 2021-05-09 op #define FCGI_READY 2
77 8ad1c570 2021-05-09 op extern struct fcgi fcgi[FCGI_MAX];
79 b8e64ccd 2021-03-31 op TAILQ_HEAD(lochead, location);
80 c8b74339 2021-01-24 op struct location {
81 fe5967cd 2021-01-27 op const char *match;
82 fe5967cd 2021-01-27 op const char *lang;
83 fe5967cd 2021-01-27 op const char *default_mime;
84 fe5967cd 2021-01-27 op const char *index;
85 252908e6 2021-01-24 op int auto_index; /* 0 auto, -1 off, 1 on */
86 6abda252 2021-02-06 op int block_code;
87 6abda252 2021-02-06 op const char *block_fmt;
89 02be96c6 2021-02-09 op X509_STORE *reqca;
90 793835cb 2021-02-23 op int disable_log;
93 fdea6aa0 2021-04-30 op const char *dir;
96 b8e64ccd 2021-03-31 op TAILQ_ENTRY(location) locations;
99 9cc630aa 2021-04-28 op TAILQ_HEAD(envhead, envlist);
100 9cc630aa 2021-04-28 op struct envlist {
103 9cc630aa 2021-04-28 op TAILQ_ENTRY(envlist) envs;
106 cc8c2901 2021-04-29 op TAILQ_HEAD(aliashead, alist);
107 cc8c2901 2021-04-29 op struct alist {
109 cc8c2901 2021-04-29 op TAILQ_ENTRY(alist) aliases;
112 b8e64ccd 2021-03-31 op extern TAILQ_HEAD(vhosthead, vhost) hosts;
113 15902770 2021-01-15 op struct vhost {
114 15902770 2021-01-15 op const char *domain;
115 15902770 2021-01-15 op const char *cert;
116 15902770 2021-01-15 op const char *key;
117 15902770 2021-01-15 op const char *cgi;
118 e3ddf390 2021-02-06 op const char *entrypoint;
120 b8e64ccd 2021-03-31 op TAILQ_ENTRY(vhost) vhosts;
122 6016a593 2021-01-30 op /* the first location rule is always '*' and holds the default
123 b8e64ccd 2021-03-31 op * settings for the vhost, then follows the "real" location
124 b8e64ccd 2021-03-31 op * rules as specified in the configuration. */
125 b8e64ccd 2021-03-31 op struct lochead locations;
127 9cc630aa 2021-04-28 op struct envhead env;
128 f740b61b 2021-06-11 op struct envhead params;
129 cc8c2901 2021-04-29 op struct aliashead aliases;
132 a010b0dd 2021-01-18 op struct etm { /* extension to mime */
133 a010b0dd 2021-01-18 op const char *mime;
134 a010b0dd 2021-01-18 op const char *ext;
137 b2a6b613 2021-01-21 op struct mime {
138 a010b0dd 2021-01-18 op struct etm *t;
143 15902770 2021-01-15 op struct conf {
144 3abf91b0 2021-02-07 op /* from command line */
145 3abf91b0 2021-02-07 op int foreground;
148 3abf91b0 2021-02-07 op /* in the config */
151 ae08ec7d 2021-01-25 op uint32_t protos;
152 ae08ec7d 2021-01-25 op struct mime mime;
153 ae08ec7d 2021-01-25 op char *chroot;
158 d090dc84 2021-02-08 op extern const char *config_path;
159 15902770 2021-01-15 op extern struct conf conf;
161 bc99d868 2021-03-19 op extern struct imsgbuf logibuf, exibuf, servibuf[PROC_MAX];
163 2c3e53da 2021-03-03 op extern int servpipes[PROC_MAX];
166 0be51733 2021-01-20 op char *schema;
169 0be51733 2021-01-20 op uint16_t port_no;
172 0be51733 2021-01-20 op char *fragment;
175 0be51733 2021-01-20 op struct parser {
177 0be51733 2021-01-20 op struct iri *parsed;
178 0be51733 2021-01-20 op const char *err;
181 8ad1c570 2021-05-09 op struct mbuf {
184 8ad1c570 2021-05-09 op TAILQ_ENTRY(mbuf) mbufs;
187 8ad1c570 2021-05-09 op TAILQ_HEAD(mbufhead, mbuf);
189 bc99d868 2021-03-19 op typedef void (imsg_handlerfn)(struct imsgbuf*, struct imsg*, size_t);
191 abc007d2 2021-02-08 op typedef void (*statefn)(int, short, void*);
194 92da8285 2021-02-01 op * DFA: handle_handshake is the initial state, close_conn the final.
195 5f715ce4 2021-02-02 op * Sometimes we have an enter_* function to handle the state switch.
197 92da8285 2021-02-01 op * handle_handshake -> handle_open_conn
198 92da8285 2021-02-01 op * handle_handshake -> close_conn // on err
200 35744950 2021-02-01 op * handle_open_conn -> handle_cgi_reply // via open_file/dir/...
201 8ad1c570 2021-05-09 op * handle_open_conn -> send_fcgi_req // via apply_fastcgi, IMSG_FCGI_FD
202 5f715ce4 2021-02-02 op * handle_open_conn -> handle_dirlist // ...same
203 92da8285 2021-02-01 op * handle_open_conn -> send_file // ...same
204 35744950 2021-02-01 op * handle_open_conn -> start_reply // on error
206 35744950 2021-02-01 op * handle_cgi_reply -> handle_cgi // after logging the CGI reply
207 35744950 2021-02-01 op * handle_cgi_reply -> start_reply // on error
209 92da8285 2021-02-01 op * handle_cgi -> close_conn
211 8ad1c570 2021-05-09 op * send_fcgi_req -> copy_mbuf // via handle_fcgi
212 8ad1c570 2021-05-09 op * handle_fcgi -> close_all // on error
213 8ad1c570 2021-05-09 op * copy_mbuf -> close_conn // on success/error
215 5f715ce4 2021-02-02 op * handle_dirlist -> send_directory_listing
216 5f715ce4 2021-02-02 op * handle_dirlist -> close_conn // on error
218 92da8285 2021-02-01 op * send_directory_listing -> close_conn
220 92da8285 2021-02-01 op * send_file -> close_conn
222 488f059a 2020-12-24 op struct client {
224 488f059a 2020-12-24 op struct tls *ctx;
225 0be51733 2021-01-20 op char req[GEMINI_URL_LEN];
226 0be51733 2021-01-20 op struct iri iri;
227 3300cbe0 2021-01-27 op char domain[DOMAIN_NAME_LEN];
230 8ad1c570 2021-05-09 op * start_reply uses this to know what function call after the
231 8ad1c570 2021-05-09 op * reply. It's also used as sentinel value in fastcgi to know
232 8ad1c570 2021-05-09 op * if the server has closed the request.
234 abc007d2 2021-02-08 op statefn next;
237 488f059a 2020-12-24 op const char *meta;
239 11c98667 2021-04-25 op struct dirent **dir;
240 11c98667 2021-04-25 op int dirlen, diroff;
243 c836cdfa 2021-03-29 op /* big enough to store STATUS + SPACE + META + CRLF */
244 c836cdfa 2021-03-29 op char sbuf[1029];
245 27b2fa9a 2021-02-12 op ssize_t len, off;
247 8ad1c570 2021-05-09 op struct mbufhead mbufhead;
249 709d6e5e 2021-01-10 op struct sockaddr_storage addr;
250 bc99d868 2021-03-19 op struct vhost *host; /* host they're talking to */
251 1feaf2a6 2021-05-15 op size_t loc; /* location matched */
254 8ad1c570 2021-05-09 op extern struct client clients[MAX_USERS];
256 bc99d868 2021-03-19 op struct cgireq {
257 bc99d868 2021-03-19 op char buf[GEMINI_URL_LEN];
259 bc99d868 2021-03-19 op size_t iri_schema_off;
260 bc99d868 2021-03-19 op size_t iri_host_off;
261 bc99d868 2021-03-19 op size_t iri_port_off;
262 bc99d868 2021-03-19 op size_t iri_path_off;
263 bc99d868 2021-03-19 op size_t iri_query_off;
264 bc99d868 2021-03-19 op size_t iri_fragment_off;
265 bc99d868 2021-03-19 op int iri_portno;
267 bc99d868 2021-03-19 op char spath[PATH_MAX+1];
268 bc99d868 2021-03-19 op char relpath[PATH_MAX+1];
269 bc99d868 2021-03-19 op char addr[NI_MAXHOST+1];
271 bc99d868 2021-03-19 op /* AFAIK there isn't an upper limit for these two fields. */
272 bc99d868 2021-03-19 op char subject[64+1];
273 bc99d868 2021-03-19 op char issuer[64+1];
275 bc99d868 2021-03-19 op char hash[128+1];
276 89541eee 2021-04-13 op char version[8];
277 89541eee 2021-04-13 op char cipher[32];
278 89541eee 2021-04-13 op int cipher_strength;
279 bc99d868 2021-03-19 op time_t notbefore;
280 bc99d868 2021-03-19 op time_t notafter;
282 bc99d868 2021-03-19 op size_t host_off;
283 fdea6aa0 2021-04-30 op size_t loc_off;
288 488f059a 2020-12-24 op FILE_EXECUTABLE,
289 488f059a 2020-12-24 op FILE_DIRECTORY,
290 488f059a 2020-12-24 op FILE_MISSING,
293 bc99d868 2021-03-19 op enum imsg_type {
294 bc99d868 2021-03-19 op IMSG_CGI_REQ,
295 bc99d868 2021-03-19 op IMSG_CGI_RES,
296 8ad1c570 2021-05-09 op IMSG_FCGI_REQ,
297 8ad1c570 2021-05-09 op IMSG_FCGI_FD,
303 8443bff7 2021-01-25 op char *data_dir(void);
304 8443bff7 2021-01-25 op void load_local_cert(const char*, const char*);
305 ae08ec7d 2021-01-25 op void load_vhosts(void);
306 33ac26a0 2021-01-21 op int make_socket(int, int);
307 ae08ec7d 2021-01-25 op void setup_tls(void);
308 c8b74339 2021-01-24 op void init_config(void);
309 ca21e100 2021-02-04 op void free_config(void);
310 ae08ec7d 2021-01-25 op void drop_priv(void);
312 f7b816dc 2021-01-15 op /* provided by lex/yacc */
313 15902770 2021-01-15 op extern FILE *yyin;
314 15902770 2021-01-15 op extern int yylineno;
315 15902770 2021-01-15 op extern int yyparse(void);
316 15902770 2021-01-15 op extern int yylex(void);
318 6abda252 2021-02-06 op void yyerror(const char*, ...);
319 13ed2fb6 2021-01-27 op int parse_portno(const char*);
320 13ed2fb6 2021-01-27 op void parse_conf(const char*);
323 3abf91b0 2021-02-07 op void fatal(const char*, ...)
324 3abf91b0 2021-02-07 op __attribute__((format (printf, 1, 2)))
325 3abf91b0 2021-02-07 op __attribute__((__noreturn__));
327 3abf91b0 2021-02-07 op #define LOG_ATTR_FMT __attribute__((format (printf, 2, 3)))
328 3abf91b0 2021-02-07 op void log_err(struct client*, const char*, ...) LOG_ATTR_FMT;
329 3abf91b0 2021-02-07 op void log_warn(struct client*, const char*, ...) LOG_ATTR_FMT;
330 3abf91b0 2021-02-07 op void log_notice(struct client*, const char*, ...) LOG_ATTR_FMT;
331 3abf91b0 2021-02-07 op void log_info(struct client*, const char*, ...) LOG_ATTR_FMT;
332 3abf91b0 2021-02-07 op void log_debug(struct client*, const char*, ...) LOG_ATTR_FMT;
333 3abf91b0 2021-02-07 op void log_request(struct client*, char*, size_t);
334 376a5407 2021-02-23 op int logger_main(int, struct imsgbuf*);
337 b2a6b613 2021-01-21 op void init_mime(struct mime*);
338 b2a6b613 2021-01-21 op void add_mime(struct mime*, const char*, const char*);
339 b2a6b613 2021-01-21 op void load_default_mime(struct mime*);
340 6119e13e 2021-01-19 op const char *mime(struct vhost*, const char*);
342 d3a08f4d 2021-01-17 op /* server.c */
343 c8b74339 2021-01-24 op const char *vhost_lang(struct vhost*, const char*);
344 c8b74339 2021-01-24 op const char *vhost_default_mime(struct vhost*, const char*);
345 c8b74339 2021-01-24 op const char *vhost_index(struct vhost*, const char*);
346 252908e6 2021-01-24 op int vhost_auto_index(struct vhost*, const char*);
347 6abda252 2021-02-06 op int vhost_block_return(struct vhost*, const char*, int*, const char**);
348 8ad1c570 2021-05-09 op int vhost_fastcgi(struct vhost*, const char*);
349 1feaf2a6 2021-05-15 op int vhost_dirfd(struct vhost*, const char*, size_t*);
350 6abda252 2021-02-06 op int vhost_strip(struct vhost*, const char*);
351 02be96c6 2021-02-09 op X509_STORE *vhost_require_ca(struct vhost*, const char*);
352 793835cb 2021-02-23 op int vhost_disable_log(struct vhost*, const char*);
354 9b8f5ed2 2021-02-03 op void mark_nonblock(int);
355 8ad1c570 2021-05-09 op void start_reply(struct client*, int, const char*);
356 8ad1c570 2021-05-09 op void close_conn(int, short, void*);
357 8ad1c570 2021-05-09 op struct client *try_client_by_id(int);
358 bc99d868 2021-03-19 op void loop(struct tls*, int, int, struct imsgbuf*);
361 11c98667 2021-04-25 op int scandir_fd(int, struct dirent***, int(*)(const struct dirent*),
362 11c98667 2021-04-25 op int(*)(const struct dirent**, const struct dirent**));
363 11c98667 2021-04-25 op int select_non_dot(const struct dirent*);
364 11c98667 2021-04-25 op int select_non_dotdot(const struct dirent*);
367 881a9dd9 2021-01-16 op int send_string(int, const char*);
368 881a9dd9 2021-01-16 op int recv_string(int, char**);
369 2fafa2d2 2021-02-01 op int send_iri(int, struct iri*);
370 2fafa2d2 2021-02-01 op int recv_iri(int, struct iri*);
371 2fafa2d2 2021-02-01 op void free_recvd_iri(struct iri*);
372 881a9dd9 2021-01-16 op int send_vhost(int, struct vhost*);
373 881a9dd9 2021-01-16 op int recv_vhost(int, struct vhost**);
374 b63e30ff 2021-02-07 op int send_time(int, time_t);
375 b63e30ff 2021-02-07 op int recv_time(int, time_t*);
376 881a9dd9 2021-01-16 op int send_fd(int, int);
377 881a9dd9 2021-01-16 op int recv_fd(int);
378 bc99d868 2021-03-19 op int executor_main(struct imsgbuf*);
381 8ad1c570 2021-05-09 op void handle_fcgi(int, short, void*);
382 8ad1c570 2021-05-09 op void send_fcgi_req(struct fcgi*, struct client*);
384 dafb57b8 2021-01-15 op /* sandbox.c */
385 62e001b0 2021-03-20 op void sandbox_server_process(void);
386 62e001b0 2021-03-20 op void sandbox_executor_process(void);
387 62e001b0 2021-03-20 op void sandbox_logger_process(void);
390 ef04b551 2021-01-09 op int valid_multibyte_utf8(struct parser*);
391 3300cbe0 2021-01-27 op char *utf8_nth(char*, size_t);
394 3c1cf9d0 2021-01-11 op int parse_iri(char*, struct iri*, const char**);
395 c4f682f8 2021-01-27 op int trim_req_iri(char*, const char **);
396 2fafa2d2 2021-02-01 op int serialize_iri(struct iri*, char*, size_t);
397 9f006a21 2021-02-07 op char *pct_decode_str(char *);
400 a2fd8013 2021-01-29 op int puny_decode(const char*, char*, size_t, const char**);
402 44ee1bac 2021-01-27 op /* utils.c */
403 ca21e100 2021-02-04 op void block_signals(void);
404 ca21e100 2021-02-04 op void unblock_signals(void);
405 44ee1bac 2021-01-27 op int starts_with(const char*, const char*);
406 44ee1bac 2021-01-27 op int ends_with(const char*, const char*);
407 44ee1bac 2021-01-27 op ssize_t filesize(int);
408 2fafa2d2 2021-02-01 op char *absolutify_path(const char*);
409 ca21e100 2021-02-04 op char *xstrdup(const char*);
410 b8e64ccd 2021-03-31 op void *xcalloc(size_t, size_t);
411 3abf91b0 2021-02-07 op void gen_certificate(const char*, const char*, const char*);
412 02be96c6 2021-02-09 op X509_STORE *load_ca(const char*);
413 02be96c6 2021-02-09 op int validate_against_ca(X509_STORE*, const uint8_t*, size_t);
414 bc99d868 2021-03-19 op void dispatch_imsg(struct imsgbuf*, imsg_handlerfn**, size_t);