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>
53 #define VERSION_STR(n) n " " VERSION
54 #define GE_STRING VERSION_STR("ge")
55 #define GG_STRING VERSION_STR("gg")
56 #define GMID_STRING VERSION_STR("gmid")
58 #define GMID_VERSION "gmid/" VERSION
60 #define GEMINI_URL_LEN (1024+3) /* URL max len + \r\n + \0 */
63 #define TEMP_REDIRECT 30
64 #define TEMP_FAILURE 40
66 #define PROXY_ERROR 43
68 #define PROXY_REFUSED 53
69 #define BAD_REQUEST 59
70 #define CLIENT_CERT_REQ 60
71 #define CERT_NOT_AUTH 61
73 /* maximum hostname and label length, +1 for the NUL-terminator */
74 #define DOMAIN_NAME_LEN (253+1)
75 #define LABEL_LEN (63+1)
77 #define MEDIATYPE_NAMEMAX 128 /* file name extension */
78 #define MEDIATYPE_TYPEMAX 128 /* length of type/subtype */
80 #define FCGI_NAME_MAX 511
81 #define FCGI_VAL_MAX 511
83 #define PROC_MAX_INSTANCES 16
85 #define TLS_CERT_HASH_SIZE 128
87 /* forward declaration */
108 TAILQ_HEAD(addrhead, address);
114 struct sockaddr_storage ss;
118 /* used in the server */
121 struct event evsock; /* set if sock != -1 */
124 TAILQ_ENTRY(address) addrs;
127 TAILQ_HEAD(fcgihead, fcgi);
132 TAILQ_ENTRY(fcgi) fcgi;
135 TAILQ_HEAD(proxyhead, proxy);
137 char match_proto[32];
138 char match_host[HOST_NAME_MAX + 1];
141 char host[HOST_NAME_MAX + 1];
143 char sni[HOST_NAME_MAX];
156 TAILQ_ENTRY(proxy) proxies;
159 TAILQ_HEAD(lochead, location);
163 char default_mime[MEDIATYPE_TYPEMAX];
164 char index[PATH_MAX];
165 int auto_index; /* 0 auto, -1 off, 1 on */
167 char block_fmt[GEMINI_URL_LEN];
177 TAILQ_ENTRY(location) locations;
180 TAILQ_HEAD(envhead, envlist);
182 char name[FCGI_NAME_MAX];
183 char value[FCGI_VAL_MAX];
184 TAILQ_ENTRY(envlist) envs;
187 TAILQ_HEAD(aliashead, alist);
189 char alias[HOST_NAME_MAX + 1];
190 TAILQ_ENTRY(alist) aliases;
193 TAILQ_HEAD(vhosthead, vhost);
195 char domain[HOST_NAME_MAX + 1];
209 TAILQ_ENTRY(vhost) vhosts;
211 struct addrhead addrs;
214 * the first location rule is always '*' and holds the default
215 * settings for the vhost, then follows the "real" location
216 * rules as specified in the configuration.
218 struct lochead locations;
220 struct envhead params;
221 struct aliashead aliases;
222 struct proxyhead proxies;
225 struct etm { /* extension to mime */
226 char mime[MEDIATYPE_TYPEMAX];
227 char ext[MEDIATYPE_NAMEMAX];
236 TAILQ_HEAD(pkihead, pki);
240 TAILQ_ENTRY(pki) pkis;
247 char chroot[PATH_MAX];
248 char user[LOGIN_NAME_MAX];
251 int use_privsep_crypto;
253 struct fcgihead fcgi;
254 struct vhosthead hosts;
256 struct addrhead addrs;
259 extern const char *config_path;
261 extern int servpipes[PROC_MAX_INSTANCES];
262 extern int privsep_process;
264 typedef void (imsg_handlerfn)(struct imsgbuf*, struct imsg*, size_t);
277 struct address *addr;
283 char domain[DOMAIN_NAME_LEN];
284 char rhost[NI_MAXHOST];
285 char rserv[NI_MAXSERV];
287 struct bufferevent *bev;
291 struct bufferevent *cgibev;
294 struct bufferevent *proxybev;
295 struct tls *proxyctx;
297 struct event proxyev;
307 /* big enough to store STATUS + SPACE + META + CRLF */
311 struct sockaddr_storage raddr;
314 struct vhost *host; /* host they're talking to */
315 size_t loc; /* location matched */
317 SPLAY_ENTRY(client) entry;
319 SPLAY_HEAD(client_tree_id, client);
320 extern struct client_tree_id clients;
323 char host[NI_MAXHOST];
324 char port[NI_MAXSERV];
337 IMSG_RECONF_START, /* 7 */
346 IMSG_RECONF_HOST_ADDR,
351 IMSG_RECONF_PROXY_CERT,
352 IMSG_RECONF_PROXY_KEY,
356 IMSG_CRYPTO_RSA_PRIVENC,
357 IMSG_CRYPTO_RSA_PRIVDEC,
358 IMSG_CRYPTO_ECDSA_SIGN,
364 char *data_dir(void);
365 void load_local_cert(struct vhost*, const char*, const char*);
368 void log_request(struct client *, char *, size_t);
371 struct conf *config_new(void);
372 void config_purge(struct conf *);
373 int config_send(struct conf *);
374 int config_recv(struct conf *, struct imsg *);
377 void crypto(struct privsep *, struct privsep_proc *);
378 void crypto_engine_init(struct conf *);
381 void yyerror(const char*, ...);
382 int parse_conf(struct conf *, const char*);
383 int cmdline_symset(char *);
386 void init_mime(struct mime*);
387 int add_mime(struct mime*, const char*, const char*);
388 int load_default_mime(struct mime*);
389 void sort_mime(struct mime *);
390 const char *mime(struct conf *, struct vhost*, const char*);
391 void free_mime(struct mime *);
394 extern int shutting_down;
395 const char *vhost_lang(struct vhost*, const char*);
396 const char *vhost_default_mime(struct vhost*, const char*);
397 const char *vhost_index(struct vhost*, const char*);
398 int vhost_auto_index(struct vhost*, const char*);
399 int vhost_block_return(struct vhost*, const char*, int*, const char**);
400 int vhost_fastcgi(struct vhost*, const char*);
401 int vhost_dirfd(struct vhost*, const char*, size_t*);
402 int vhost_strip(struct vhost*, const char*);
403 X509_STORE *vhost_require_ca(struct vhost*, const char*);
404 int vhost_disable_log(struct vhost*, const char*);
406 void mark_nonblock(int);
407 void client_write(struct bufferevent *, void *);
408 void start_reply(struct client*, int, const char*);
409 void client_close(struct client *);
410 struct client *client_by_id(int);
411 void do_accept(int, short, void *);
412 void server_init(struct privsep *, struct privsep_proc *, void *);
413 int server_configure_done(struct conf *);
414 void server(struct privsep *ps, struct privsep_proc *);
416 int client_tree_cmp(struct client *, struct client *);
417 SPLAY_PROTOTYPE(client_tree_id, client, entry, client_tree_cmp);
420 int scandir_fd(int, struct dirent***, int(*)(const struct dirent*),
421 int(*)(const struct dirent**, const struct dirent**));
422 int select_non_dot(const struct dirent*);
423 int select_non_dotdot(const struct dirent*);
426 void fcgi_read(struct bufferevent *, void *);
427 void fcgi_write(struct bufferevent *, void *);
428 void fcgi_error(struct bufferevent *, short, void *);
429 void fcgi_req(struct client *);
432 void sandbox_main_process(void);
433 void sandbox_server_process(void);
434 void sandbox_crypto_process(void);
435 void sandbox_logger_process(void);
438 int valid_multibyte_utf8(struct parser*);
439 char *utf8_nth(char*, size_t);
442 int parse_iri(char*, struct iri*, const char**);
443 int serialize_iri(struct iri*, char*, size_t);
444 int encode_path(char *, size_t, const char *);
445 char *pct_decode_str(char *);
448 void logger(struct privsep *, struct privsep_proc *);
451 int proxy_init(struct client *);
454 int puny_decode(const char*, char*, size_t, const char**);
457 void block_signals(void);
458 void unblock_signals(void);
459 int starts_with(const char*, const char*);
460 int ends_with(const char*, const char*);
461 ssize_t filesize(int);
462 char *absolutify_path(const char*);
463 char *xstrdup(const char*);
464 void *xcalloc(size_t, size_t);
465 void gen_certificate(const char*, const char*, const char*);
466 X509_STORE *load_ca(uint8_t *, size_t);
467 int validate_against_ca(X509_STORE*, const uint8_t*, size_t);
468 void ssl_error(const char *);
469 char *ssl_pubkey_hash(const uint8_t *, size_t);
470 EVP_PKEY *ssl_load_pkey(const uint8_t *, size_t);
471 struct vhost *new_vhost(void);
472 struct location *new_location(void);
473 struct proxy *new_proxy(void);