2 * Copyright (c) 2020, 2021, 2022 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 /* forward declaration */
105 TAILQ_HEAD(fcgihead, fcgi);
110 TAILQ_ENTRY(fcgi) fcgi;
113 TAILQ_HEAD(proxyhead, proxy);
115 char match_proto[32];
116 char match_host[HOST_NAME_MAX + 1];
119 char host[HOST_NAME_MAX + 1];
121 char sni[HOST_NAME_MAX];
134 TAILQ_ENTRY(proxy) proxies;
137 TAILQ_HEAD(lochead, location);
141 char default_mime[MEDIATYPE_TYPEMAX];
142 char index[PATH_MAX];
143 int auto_index; /* 0 auto, -1 off, 1 on */
145 char block_fmt[GEMINI_URL_LEN];
155 TAILQ_ENTRY(location) locations;
158 TAILQ_HEAD(envhead, envlist);
160 char name[FCGI_NAME_MAX];
161 char value[FCGI_VAL_MAX];
162 TAILQ_ENTRY(envlist) envs;
165 TAILQ_HEAD(aliashead, alist);
167 char alias[HOST_NAME_MAX + 1];
168 TAILQ_ENTRY(alist) aliases;
171 extern TAILQ_HEAD(vhosthead, vhost) hosts;
173 char domain[HOST_NAME_MAX + 1];
187 TAILQ_ENTRY(vhost) vhosts;
190 * the first location rule is always '*' and holds the default
191 * settings for the vhost, then follows the "real" location
192 * rules as specified in the configuration.
194 struct lochead locations;
196 struct envhead params;
197 struct aliashead aliases;
198 struct proxyhead proxies;
201 struct etm { /* extension to mime */
202 char mime[MEDIATYPE_TYPEMAX];
203 char ext[MEDIATYPE_NAMEMAX];
218 char chroot[PATH_MAX];
219 char user[LOGIN_NAME_MAX];
224 struct event evsock4;
226 struct event evsock6;
228 struct fcgihead fcgi;
231 extern const char *config_path;
232 extern struct conf conf;
234 extern int servpipes[PROC_MAX_INSTANCES];
235 extern int privsep_process;
237 typedef void (imsg_handlerfn)(struct imsgbuf*, struct imsg*, size_t);
254 char domain[DOMAIN_NAME_LEN];
256 struct bufferevent *bev;
260 struct bufferevent *cgibev;
263 struct bufferevent *proxybev;
264 struct tls *proxyctx;
266 struct event proxyev;
276 /* big enough to store STATUS + SPACE + META + CRLF */
280 struct sockaddr_storage addr;
281 struct vhost *host; /* host they're talking to */
282 size_t loc; /* location matched */
284 SPLAY_ENTRY(client) entry;
286 SPLAY_HEAD(client_tree_id, client);
287 extern struct client_tree_id clients;
290 char host[NI_MAXHOST];
291 char port[NI_MAXSERV];
310 IMSG_RECONF_START, /* 7 */
325 IMSG_RECONF_PROXY_CERT,
326 IMSG_RECONF_PROXY_KEY,
334 char *data_dir(void);
335 void load_local_cert(struct vhost*, const char*, const char*);
338 void log_request(struct client *, char *, size_t);
341 void config_init(void);
342 void config_free(void);
343 int config_send(struct conf *, struct vhosthead *);
344 int config_recv(struct conf *, struct imsg *);
347 void yyerror(const char*, ...);
348 void parse_conf(const char*);
349 void print_conf(void);
350 int cmdline_symset(char *);
353 void init_mime(struct mime*);
354 int add_mime(struct mime*, const char*, const char*);
355 int load_default_mime(struct mime*);
356 void sort_mime(struct mime *);
357 const char *mime(struct vhost*, const char*);
358 void free_mime(struct mime *);
361 extern int shutting_down;
362 const char *vhost_lang(struct vhost*, const char*);
363 const char *vhost_default_mime(struct vhost*, const char*);
364 const char *vhost_index(struct vhost*, const char*);
365 int vhost_auto_index(struct vhost*, const char*);
366 int vhost_block_return(struct vhost*, const char*, int*, const char**);
367 int vhost_fastcgi(struct vhost*, const char*);
368 int vhost_dirfd(struct vhost*, const char*, size_t*);
369 int vhost_strip(struct vhost*, const char*);
370 X509_STORE *vhost_require_ca(struct vhost*, const char*);
371 int vhost_disable_log(struct vhost*, const char*);
373 void mark_nonblock(int);
374 void client_write(struct bufferevent *, void *);
375 void start_reply(struct client*, int, const char*);
376 void client_close(struct client *);
377 struct client *client_by_id(int);
378 void do_accept(int, short, void *);
379 void server_init(struct privsep *, struct privsep_proc *, void *);
380 int server_configure_done(struct conf *);
381 void server(struct privsep *ps, struct privsep_proc *);
383 int client_tree_cmp(struct client *, struct client *);
384 SPLAY_PROTOTYPE(client_tree_id, client, entry, client_tree_cmp);
387 int scandir_fd(int, struct dirent***, int(*)(const struct dirent*),
388 int(*)(const struct dirent**, const struct dirent**));
389 int select_non_dot(const struct dirent*);
390 int select_non_dotdot(const struct dirent*);
393 void fcgi_read(struct bufferevent *, void *);
394 void fcgi_write(struct bufferevent *, void *);
395 void fcgi_error(struct bufferevent *, short, void *);
396 void fcgi_req(struct client *);
399 void sandbox_main_process(void);
400 void sandbox_server_process(void);
401 void sandbox_logger_process(void);
404 int valid_multibyte_utf8(struct parser*);
405 char *utf8_nth(char*, size_t);
408 int parse_iri(char*, struct iri*, const char**);
409 int serialize_iri(struct iri*, char*, size_t);
410 int encode_path(char *, size_t, const char *);
411 char *pct_decode_str(char *);
414 void logger(struct privsep *, struct privsep_proc *);
417 int proxy_init(struct client *);
420 int puny_decode(const char*, char*, size_t, const char**);
423 void block_signals(void);
424 void unblock_signals(void);
425 int starts_with(const char*, const char*);
426 int ends_with(const char*, const char*);
427 ssize_t filesize(int);
428 char *absolutify_path(const char*);
429 char *xstrdup(const char*);
430 void *xcalloc(size_t, size_t);
431 void gen_certificate(const char*, const char*, const char*);
432 X509_STORE *load_ca(int);
433 int validate_against_ca(X509_STORE*, const uint8_t*, size_t);
434 struct vhost *new_vhost(void);
435 struct location *new_location(void);
436 struct proxy *new_proxy(void);