2 * Copyright (c) 2020, 2021, 2022, 2023 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.
22 #include <sys/socket.h>
23 #include <sys/types.h>
25 #include <arpa/inet.h>
26 #include <netinet/in.h>
38 #include <openssl/x509.h>
41 # include <event2/event.h>
42 # include <event2/event_compat.h>
43 # include <event2/event_struct.h>
44 # include <event2/buffer.h>
45 # include <event2/buffer_compat.h>
46 # include <event2/bufferevent.h>
47 # include <event2/bufferevent_struct.h>
48 # include <event2/bufferevent_compat.h>
55 #define VERSION_STR(n) n " " VERSION
56 #define GE_STRING VERSION_STR("ge")
57 #define GG_STRING VERSION_STR("gg")
58 #define GMID_STRING VERSION_STR("gmid")
60 #define GMID_VERSION "gmid/" VERSION
62 #define GEMINI_URL_LEN (1024+3) /* URL max len + \r\n + \0 */
65 #define TEMP_REDIRECT 30
66 #define TEMP_FAILURE 40
68 #define PROXY_ERROR 43
70 #define PROXY_REFUSED 53
71 #define BAD_REQUEST 59
72 #define CLIENT_CERT_REQ 60
73 #define CERT_NOT_AUTH 61
75 /* maximum hostname and label length, +1 for the NUL-terminator */
76 #define DOMAIN_NAME_LEN (253+1)
77 #define LABEL_LEN (63+1)
79 #define MEDIATYPE_NAMEMAX 128 /* file name extension */
80 #define MEDIATYPE_TYPEMAX 128 /* length of type/subtype */
82 #define FCGI_NAME_MAX 511
83 #define FCGI_VAL_MAX 511
85 #define PROC_MAX_INSTANCES 16
87 #define TLS_CERT_HASH_SIZE 128
89 /* forward declaration */
100 TAILQ_HEAD(addrhead, address);
106 struct sockaddr_storage ss;
110 /* used in the server */
113 struct event evsock; /* set if sock != -1 */
116 TAILQ_ENTRY(address) addrs;
119 TAILQ_HEAD(fcgihead, fcgi);
124 TAILQ_ENTRY(fcgi) fcgi;
127 TAILQ_HEAD(envhead, envlist);
129 char name[FCGI_NAME_MAX];
130 char value[FCGI_VAL_MAX];
131 TAILQ_ENTRY(envlist) envs;
134 TAILQ_HEAD(aliashead, alist);
136 char alias[HOST_NAME_MAX + 1];
137 TAILQ_ENTRY(alist) aliases;
140 TAILQ_HEAD(proxyhead, proxy);
142 char match_proto[32];
143 char match_host[HOST_NAME_MAX + 1];
146 char host[HOST_NAME_MAX + 1];
148 char sni[HOST_NAME_MAX];
161 TAILQ_ENTRY(proxy) proxies;
164 TAILQ_HEAD(lochead, location);
168 char default_mime[MEDIATYPE_TYPEMAX];
169 char index[PATH_MAX];
170 int auto_index; /* 0 auto, -1 off, 1 on */
172 char block_fmt[GEMINI_URL_LEN];
178 struct envhead params;
183 TAILQ_ENTRY(location) locations;
186 TAILQ_HEAD(vhosthead, vhost);
188 char domain[HOST_NAME_MAX + 1];
202 TAILQ_ENTRY(vhost) vhosts;
204 struct addrhead addrs;
207 * the first location rule is always '*' and holds the default
208 * settings for the vhost, then follows the "real" location
209 * rules as specified in the configuration.
211 struct lochead locations;
213 struct aliashead aliases;
214 struct proxyhead proxies;
217 struct etm { /* extension to mime */
218 char mime[MEDIATYPE_TYPEMAX];
219 char ext[MEDIATYPE_NAMEMAX];
228 TAILQ_HEAD(pkihead, pki);
232 TAILQ_ENTRY(pki) pkis;
239 char chroot[PATH_MAX];
240 char user[LOGIN_NAME_MAX];
243 int use_privsep_crypto;
245 struct fcgihead fcgi;
246 struct vhosthead hosts;
248 struct addrhead addrs;
251 extern const char *config_path;
253 extern int servpipes[PROC_MAX_INSTANCES];
254 extern int privsep_process;
256 typedef void (imsg_handlerfn)(struct imsgbuf*, struct imsg*, size_t);
269 struct address *addr;
275 char domain[DOMAIN_NAME_LEN];
276 char rhost[NI_MAXHOST];
277 char rserv[NI_MAXSERV];
279 struct bufferevent *bev;
283 struct bufferevent *cgibev;
286 struct bufferevent *proxybev;
287 struct tls *proxyctx;
289 struct event proxyev;
299 /* big enough to store STATUS + SPACE + META + CRLF */
303 struct sockaddr_storage raddr;
306 struct vhost *host; /* host they're talking to */
307 size_t loc; /* location matched */
309 SPLAY_ENTRY(client) entry;
311 SPLAY_HEAD(client_tree_id, client);
312 extern struct client_tree_id clients;
315 char host[NI_MAXHOST];
316 char port[NI_MAXSERV];
329 IMSG_RECONF_START, /* 7 */
338 IMSG_RECONF_HOST_ADDR,
343 IMSG_RECONF_PROXY_CERT,
344 IMSG_RECONF_PROXY_KEY,
348 IMSG_CRYPTO_RSA_PRIVENC,
349 IMSG_CRYPTO_RSA_PRIVDEC,
350 IMSG_CRYPTO_ECDSA_SIGN,
356 char *data_dir(void);
357 void load_local_cert(struct vhost*, const char*, const char*);
360 void log_request(struct client *, int, const char *);
363 struct conf *config_new(void);
364 void config_purge(struct conf *);
365 int config_send(struct conf *);
366 int config_recv(struct conf *, struct imsg *);
369 void crypto(struct privsep *, struct privsep_proc *);
370 void crypto_engine_init(struct conf *);
373 void yyerror(const char*, ...);
374 int parse_conf(struct conf *, const char*);
375 int cmdline_symset(char *);
378 void init_mime(struct mime*);
379 int add_mime(struct mime*, const char*, const char*);
380 int load_default_mime(struct mime*);
381 void sort_mime(struct mime *);
382 const char *mime(struct conf *, struct vhost*, const char*);
383 void free_mime(struct mime *);
386 extern int shutting_down;
387 const char *vhost_lang(struct vhost*, const char*);
388 const char *vhost_default_mime(struct vhost*, const char*);
389 const char *vhost_index(struct vhost*, const char*);
390 int vhost_auto_index(struct vhost*, const char*);
391 int vhost_block_return(struct vhost*, const char*, int*, const char**);
392 struct location *vhost_fastcgi(struct vhost*, const char*);
393 int vhost_dirfd(struct vhost*, const char*, size_t*);
394 int vhost_strip(struct vhost*, const char*);
395 X509_STORE *vhost_require_ca(struct vhost*, const char*);
396 int vhost_disable_log(struct vhost*, const char*);
398 void mark_nonblock(int);
399 void client_write(struct bufferevent *, void *);
400 void start_reply(struct client*, int, const char*);
401 void client_close(struct client *);
402 struct client *client_by_id(int);
403 void server_accept(int, short, void *);
404 void server_init(struct privsep *, struct privsep_proc *, void *);
405 int server_configure_done(struct conf *);
406 void server(struct privsep *ps, struct privsep_proc *);
408 int client_tree_cmp(struct client *, struct client *);
409 SPLAY_PROTOTYPE(client_tree_id, client, entry, client_tree_cmp);
412 int scandir_fd(int, struct dirent***, int(*)(const struct dirent*),
413 int(*)(const struct dirent**, const struct dirent**));
414 int select_non_dot(const struct dirent*);
415 int select_non_dotdot(const struct dirent*);
418 void fcgi_read(struct bufferevent *, void *);
419 void fcgi_write(struct bufferevent *, void *);
420 void fcgi_error(struct bufferevent *, short, void *);
421 void fcgi_req(struct client *, struct location *);
424 void sandbox_main_process(void);
425 void sandbox_server_process(void);
426 void sandbox_crypto_process(void);
427 void sandbox_logger_process(void);
430 int valid_multibyte_utf8(struct parser*);
431 char *utf8_nth(char*, size_t);
434 void logger(struct privsep *, struct privsep_proc *);
437 int proxy_init(struct client *);
440 int puny_decode(const char*, char*, size_t, const char**);
443 void block_signals(void);
444 void unblock_signals(void);
445 int starts_with(const char*, const char*);
446 int ends_with(const char*, const char*);
447 ssize_t filesize(int);
448 char *absolutify_path(const char*);
449 char *xstrdup(const char*);
450 void *xcalloc(size_t, size_t);
451 void gen_certificate(const char*, const char*, const char*);
452 X509_STORE *load_ca(uint8_t *, size_t);
453 int validate_against_ca(X509_STORE*, const uint8_t*, size_t);
454 void ssl_error(const char *);
455 char *ssl_pubkey_hash(const uint8_t *, size_t);
456 EVP_PKEY *ssl_load_pkey(const uint8_t *, size_t);
457 struct vhost *new_vhost(void);
458 struct location *new_location(void);
459 struct proxy *new_proxy(void);