Blob


1 /*
2 * Copyright (c) 2020, 2021, 2022, 2023 Omar Polo <op@omarpolo.com>
3 *
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.
7 *
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.
15 */
17 #ifndef GMID_H
18 #define GMID_H
20 #include "config.h"
22 #include <sys/socket.h>
23 #include <sys/types.h>
25 #include <arpa/inet.h>
26 #include <netinet/in.h>
28 #include <dirent.h>
29 #include <limits.h>
30 #include <netdb.h>
31 #include <signal.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <time.h>
35 #include <tls.h>
36 #include <unistd.h>
38 #include <openssl/x509.h>
40 #if HAVE_EVENT2
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>
49 #else
50 # include <event.h>
51 #endif
53 #include "iri.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 */
64 #define SUCCESS 20
65 #define TEMP_REDIRECT 30
66 #define TEMP_FAILURE 40
67 #define CGI_ERROR 42
68 #define PROXY_ERROR 43
69 #define NOT_FOUND 51
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 */
90 struct privsep;
91 struct privsep_proc;
93 enum log_format {
94 LOG_FORMAT_CONDENSED,
95 LOG_FORMAT_COMMON,
96 LOG_FORMAT_COMBINED,
97 LOG_FORMAT_LEGACY,
98 };
100 struct parser {
101 char *iri;
102 struct iri *parsed;
103 const char *err;
104 };
106 struct conf;
107 TAILQ_HEAD(addrhead, address);
108 struct address {
109 int ai_flags;
110 int ai_family;
111 int ai_socktype;
112 int ai_protocol;
113 struct sockaddr_storage ss;
114 socklen_t slen;
115 int16_t port;
117 /* used in the server */
118 struct conf *conf;
119 int sock;
120 struct event evsock; /* set if sock != -1 */
121 struct tls *ctx;
123 TAILQ_ENTRY(address) addrs;
124 };
126 TAILQ_HEAD(fcgihead, fcgi);
127 struct fcgi {
128 int id;
129 char path[PATH_MAX];
130 char port[32];
131 TAILQ_ENTRY(fcgi) fcgi;
132 };
134 TAILQ_HEAD(envhead, envlist);
135 struct envlist {
136 char name[FCGI_NAME_MAX];
137 char value[FCGI_VAL_MAX];
138 TAILQ_ENTRY(envlist) envs;
139 };
141 TAILQ_HEAD(aliashead, alist);
142 struct alist {
143 char alias[HOST_NAME_MAX + 1];
144 TAILQ_ENTRY(alist) aliases;
145 };
147 TAILQ_HEAD(proxyhead, proxy);
148 struct proxy {
149 char match_proto[32];
150 char match_host[HOST_NAME_MAX + 1];
151 char match_port[32];
153 char host[HOST_NAME_MAX + 1];
154 char port[32];
155 char sni[HOST_NAME_MAX];
156 int notls;
157 uint32_t protocols;
158 int noverifyname;
159 char *cert_path;
160 uint8_t *cert;
161 size_t certlen;
162 char *key_path;
163 uint8_t *key;
164 size_t keylen;
165 char *reqca_path;
166 X509_STORE *reqca;
168 TAILQ_ENTRY(proxy) proxies;
169 };
171 TAILQ_HEAD(lochead, location);
172 struct location {
173 char match[128];
174 char lang[32];
175 char default_mime[MEDIATYPE_TYPEMAX];
176 char index[PATH_MAX];
177 int auto_index; /* 0 auto, -1 off, 1 on */
178 int block_code;
179 char block_fmt[GEMINI_URL_LEN];
180 int strip;
181 char *reqca_path;
182 X509_STORE *reqca;
183 int disable_log;
184 int fcgi;
185 int nofcgi;
186 struct envhead params;
188 char dir[PATH_MAX];
189 int dirfd;
191 TAILQ_ENTRY(location) locations;
192 };
194 TAILQ_HEAD(vhosthead, vhost);
195 struct vhost {
196 char domain[HOST_NAME_MAX + 1];
197 char *cert_path;
198 char *key_path;
199 char *ocsp_path;
201 uint8_t *cert;
202 size_t certlen;
204 uint8_t *key;
205 size_t keylen;
207 uint8_t *ocsp;
208 size_t ocsplen;
210 TAILQ_ENTRY(vhost) vhosts;
212 struct addrhead addrs;
214 /*
215 * the first location rule is always '*' and holds the default
216 * settings for the vhost, then follows the "real" location
217 * rules as specified in the configuration.
218 */
219 struct lochead locations;
221 struct aliashead aliases;
222 struct proxyhead proxies;
223 };
225 struct etm { /* extension to mime */
226 char mime[MEDIATYPE_TYPEMAX];
227 char ext[MEDIATYPE_NAMEMAX];
228 };
230 struct mime {
231 struct etm *t;
232 size_t len;
233 size_t cap;
234 };
236 TAILQ_HEAD(pkihead, pki);
237 struct pki {
238 char *hash;
239 EVP_PKEY *pkey;
240 TAILQ_ENTRY(pki) pkis;
241 };
243 struct conf {
244 struct privsep *ps;
245 uint32_t protos;
246 struct mime mime;
247 char chroot[PATH_MAX];
248 char user[LOGIN_NAME_MAX];
249 int prefork;
250 int reload;
251 char *log_access;
252 enum log_format log_format;
253 int use_privsep_crypto;
255 struct fcgihead fcgi;
256 struct vhosthead hosts;
257 struct pkihead pkis;
258 struct addrhead addrs;
259 };
261 extern const char *config_path;
263 extern int servpipes[PROC_MAX_INSTANCES];
264 extern int privsep_process;
266 typedef void (imsg_handlerfn)(struct imsgbuf*, struct imsg*, size_t);
268 enum {
269 REQUEST_UNDECIDED,
270 REQUEST_FILE,
271 REQUEST_DIR,
272 REQUEST_FCGI,
273 REQUEST_PROXY,
274 REQUEST_DONE,
275 };
277 struct client {
278 struct conf *conf;
279 struct address *addr;
280 uint32_t id;
281 struct tls *ctx;
282 char *req;
283 size_t reqlen;
284 struct iri iri;
285 char domain[DOMAIN_NAME_LEN];
286 char rhost[NI_MAXHOST];
287 char rserv[NI_MAXSERV];
289 struct bufferevent *bev;
291 int type;
293 struct bufferevent *cgibev;
295 struct proxy *proxy;
296 struct bufferevent *proxybev;
297 struct tls *proxyctx;
298 int proxyevset;
299 struct event proxyev;
301 char *header;
303 int code;
304 const char *meta;
305 int fd, pfd;
306 struct dirent **dir;
307 int dirlen, diroff;
309 /* big enough to store STATUS + SPACE + META + CRLF */
310 char sbuf[1029];
311 size_t soff;
313 struct sockaddr_storage raddr;
314 socklen_t raddrlen;
316 struct vhost *host; /* host they're talking to */
317 size_t loc; /* location matched */
319 SPLAY_ENTRY(client) entry;
320 };
321 SPLAY_HEAD(client_tree_id, client);
322 extern struct client_tree_id clients;
324 struct connreq {
325 char host[NI_MAXHOST];
326 char port[NI_MAXSERV];
327 int flag;
328 };
330 enum imsg_type {
331 IMSG_LOG_REQUEST,
332 IMSG_LOG_TYPE,
334 IMSG_RECONF_START,
335 IMSG_RECONF_MIME,
336 IMSG_RECONF_PROTOS,
337 IMSG_RECONF_SOCK,
338 IMSG_RECONF_FCGI,
339 IMSG_RECONF_HOST,
340 IMSG_RECONF_CERT,
341 IMSG_RECONF_KEY,
342 IMSG_RECONF_OCSP,
343 IMSG_RECONF_HOST_ADDR,
344 IMSG_RECONF_LOC,
345 IMSG_RECONF_ENV,
346 IMSG_RECONF_ALIAS,
347 IMSG_RECONF_PROXY,
348 IMSG_RECONF_PROXY_CERT,
349 IMSG_RECONF_PROXY_KEY,
350 IMSG_RECONF_END,
351 IMSG_RECONF_DONE,
353 IMSG_CRYPTO_RSA_PRIVENC,
354 IMSG_CRYPTO_RSA_PRIVDEC,
355 IMSG_CRYPTO_ECDSA_SIGN,
357 IMSG_CTL_PROCFD,
358 };
360 /* gmid.c */
361 char *data_dir(void);
362 void load_local_cert(struct vhost*, const char*, const char*);
364 /* gmid.c / ge.c */
365 void log_request(struct client *, int, const char *);
367 /* config.c */
368 struct conf *config_new(void);
369 void config_purge(struct conf *);
370 int config_send(struct conf *);
371 int config_recv(struct conf *, struct imsg *);
373 /* crypto.c */
374 void crypto(struct privsep *, struct privsep_proc *);
375 void crypto_engine_init(struct conf *);
377 /* parse.y */
378 void yyerror(const char*, ...);
379 int parse_conf(struct conf *, const char*);
380 int cmdline_symset(char *);
382 /* mime.c */
383 void init_mime(struct mime*);
384 int add_mime(struct mime*, const char*, const char*);
385 int load_default_mime(struct mime*);
386 void sort_mime(struct mime *);
387 const char *mime(struct conf *, struct vhost*, const char*);
388 void free_mime(struct mime *);
390 /* server.c */
391 extern int shutting_down;
392 const char *vhost_lang(struct vhost*, const char*);
393 const char *vhost_default_mime(struct vhost*, const char*);
394 const char *vhost_index(struct vhost*, const char*);
395 int vhost_auto_index(struct vhost*, const char*);
396 int vhost_block_return(struct vhost*, const char*, int*, const char**);
397 struct location *vhost_fastcgi(struct vhost*, const char*);
398 int vhost_dirfd(struct vhost*, const char*, size_t*);
399 int vhost_strip(struct vhost*, const char*);
400 X509_STORE *vhost_require_ca(struct vhost*, const char*);
401 int vhost_disable_log(struct vhost*, const char*);
403 void mark_nonblock(int);
404 void client_write(struct bufferevent *, void *);
405 void start_reply(struct client*, int, const char*);
406 void client_close(struct client *);
407 struct client *client_by_id(int);
408 void server_accept(int, short, void *);
409 void server_init(struct privsep *, struct privsep_proc *, void *);
410 int server_configure_done(struct conf *);
411 void server(struct privsep *ps, struct privsep_proc *);
413 int client_tree_cmp(struct client *, struct client *);
414 SPLAY_PROTOTYPE(client_tree_id, client, entry, client_tree_cmp);
416 /* dirs.c */
417 int scandir_fd(int, struct dirent***, int(*)(const struct dirent*),
418 int(*)(const struct dirent**, const struct dirent**));
419 int select_non_dot(const struct dirent*);
420 int select_non_dotdot(const struct dirent*);
422 /* fcgi.c */
423 void fcgi_read(struct bufferevent *, void *);
424 void fcgi_write(struct bufferevent *, void *);
425 void fcgi_error(struct bufferevent *, short, void *);
426 void fcgi_req(struct client *, struct location *);
428 /* sandbox.c */
429 void sandbox_main_process(void);
430 void sandbox_server_process(void);
431 void sandbox_crypto_process(void);
432 void sandbox_logger_process(void);
434 /* utf8.c */
435 int valid_multibyte_utf8(struct parser*);
436 char *utf8_nth(char*, size_t);
438 /* logger.c */
439 void logger(struct privsep *, struct privsep_proc *);
441 /* proxy.c */
442 int proxy_init(struct client *);
444 /* puny.c */
445 int puny_decode(const char*, char*, size_t, const char**);
447 /* utils.c */
448 void block_signals(void);
449 void unblock_signals(void);
450 int starts_with(const char*, const char*);
451 int ends_with(const char*, const char*);
452 ssize_t filesize(int);
453 char *absolutify_path(const char*);
454 char *xstrdup(const char*);
455 void *xcalloc(size_t, size_t);
456 void gen_certificate(const char*, const char*, const char*);
457 X509_STORE *load_ca(uint8_t *, size_t);
458 int validate_against_ca(X509_STORE*, const uint8_t*, size_t);
459 void ssl_error(const char *);
460 char *ssl_pubkey_hash(const uint8_t *, size_t);
461 EVP_PKEY *ssl_load_pkey(const uint8_t *, size_t);
462 struct vhost *new_vhost(void);
463 struct location *new_location(void);
464 struct proxy *new_proxy(void);
466 #endif