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 #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 */
62 #define SUCCESS 20
63 #define TEMP_REDIRECT 30
64 #define TEMP_FAILURE 40
65 #define CGI_ERROR 42
66 #define PROXY_ERROR 43
67 #define NOT_FOUND 51
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 */
88 struct privsep;
89 struct privsep_proc;
91 struct iri {
92 char *schema;
93 char *host;
94 char *port;
95 uint16_t port_no;
96 char *path;
97 char *query;
98 char *fragment;
99 };
101 struct parser {
102 char *iri;
103 struct iri *parsed;
104 const char *err;
105 };
107 struct conf;
108 TAILQ_HEAD(addrhead, address);
109 struct address {
110 int ai_flags;
111 int ai_family;
112 int ai_socktype;
113 int ai_protocol;
114 struct sockaddr_storage ss;
115 socklen_t slen;
116 int16_t port;
118 /* used in the server */
119 struct conf *conf;
120 int sock;
121 struct event evsock; /* set if sock != -1 */
122 struct tls *ctx;
124 TAILQ_ENTRY(address) addrs;
125 };
127 TAILQ_HEAD(fcgihead, fcgi);
128 struct fcgi {
129 int id;
130 char path[PATH_MAX];
131 char port[32];
132 TAILQ_ENTRY(fcgi) fcgi;
133 };
135 TAILQ_HEAD(proxyhead, proxy);
136 struct proxy {
137 char match_proto[32];
138 char match_host[HOST_NAME_MAX + 1];
139 char match_port[32];
141 char host[HOST_NAME_MAX + 1];
142 char port[32];
143 char sni[HOST_NAME_MAX];
144 int notls;
145 uint32_t protocols;
146 int noverifyname;
147 char *cert_path;
148 uint8_t *cert;
149 size_t certlen;
150 char *key_path;
151 uint8_t *key;
152 size_t keylen;
153 char *reqca_path;
154 X509_STORE *reqca;
156 TAILQ_ENTRY(proxy) proxies;
157 };
159 TAILQ_HEAD(lochead, location);
160 struct location {
161 char match[128];
162 char lang[32];
163 char default_mime[MEDIATYPE_TYPEMAX];
164 char index[PATH_MAX];
165 int auto_index; /* 0 auto, -1 off, 1 on */
166 int block_code;
167 char block_fmt[GEMINI_URL_LEN];
168 int strip;
169 char *reqca_path;
170 X509_STORE *reqca;
171 int disable_log;
172 int fcgi;
174 char dir[PATH_MAX];
175 int dirfd;
177 TAILQ_ENTRY(location) locations;
178 };
180 TAILQ_HEAD(envhead, envlist);
181 struct envlist {
182 char name[FCGI_NAME_MAX];
183 char value[FCGI_VAL_MAX];
184 TAILQ_ENTRY(envlist) envs;
185 };
187 TAILQ_HEAD(aliashead, alist);
188 struct alist {
189 char alias[HOST_NAME_MAX + 1];
190 TAILQ_ENTRY(alist) aliases;
191 };
193 TAILQ_HEAD(vhosthead, vhost);
194 struct vhost {
195 char domain[HOST_NAME_MAX + 1];
196 char *cert_path;
197 char *key_path;
198 char *ocsp_path;
200 uint8_t *cert;
201 size_t certlen;
203 uint8_t *key;
204 size_t keylen;
206 uint8_t *ocsp;
207 size_t ocsplen;
209 TAILQ_ENTRY(vhost) vhosts;
211 struct addrhead addrs;
213 /*
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.
217 */
218 struct lochead locations;
220 struct envhead params;
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 int use_privsep_crypto;
253 struct fcgihead fcgi;
254 struct vhosthead hosts;
255 struct pkihead pkis;
256 struct addrhead addrs;
257 };
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);
266 enum {
267 REQUEST_UNDECIDED,
268 REQUEST_FILE,
269 REQUEST_DIR,
270 REQUEST_FCGI,
271 REQUEST_PROXY,
272 REQUEST_DONE,
273 };
275 struct client {
276 struct conf *conf;
277 struct address *addr;
278 uint32_t id;
279 struct tls *ctx;
280 char *req;
281 size_t reqlen;
282 struct iri iri;
283 char domain[DOMAIN_NAME_LEN];
285 struct bufferevent *bev;
287 int type;
289 struct bufferevent *cgibev;
291 struct proxy *proxy;
292 struct bufferevent *proxybev;
293 struct tls *proxyctx;
294 int proxyevset;
295 struct event proxyev;
297 char *header;
299 int code;
300 const char *meta;
301 int fd, pfd;
302 struct dirent **dir;
303 int dirlen, diroff;
305 /* big enough to store STATUS + SPACE + META + CRLF */
306 char sbuf[1029];
307 ssize_t len, off;
309 struct sockaddr_storage raddr;
310 socklen_t raddrlen;
312 struct vhost *host; /* host they're talking to */
313 size_t loc; /* location matched */
315 SPLAY_ENTRY(client) entry;
316 };
317 SPLAY_HEAD(client_tree_id, client);
318 extern struct client_tree_id clients;
320 struct connreq {
321 char host[NI_MAXHOST];
322 char port[NI_MAXSERV];
323 int flag;
324 };
326 enum {
327 FILE_EXISTS,
328 FILE_DIRECTORY,
329 FILE_MISSING,
330 };
332 enum imsg_type {
333 IMSG_FCGI_REQ,
334 IMSG_FCGI_FD,
335 IMSG_CONN_REQ,
336 IMSG_CONN_FD,
337 IMSG_LOG,
338 IMSG_LOG_REQUEST,
339 IMSG_LOG_TYPE,
341 IMSG_RECONF_START, /* 7 */
342 IMSG_RECONF_MIME,
343 IMSG_RECONF_PROTOS,
344 IMSG_RECONF_SOCK,
345 IMSG_RECONF_FCGI,
346 IMSG_RECONF_HOST,
347 IMSG_RECONF_CERT,
348 IMSG_RECONF_KEY,
349 IMSG_RECONF_OCSP,
350 IMSG_RECONF_HOST_ADDR,
351 IMSG_RECONF_LOC,
352 IMSG_RECONF_ENV,
353 IMSG_RECONF_ALIAS,
354 IMSG_RECONF_PROXY,
355 IMSG_RECONF_PROXY_CERT,
356 IMSG_RECONF_PROXY_KEY,
357 IMSG_RECONF_END,
358 IMSG_RECONF_DONE,
360 IMSG_CRYPTO_RSA_PRIVENC,
361 IMSG_CRYPTO_RSA_PRIVDEC,
362 IMSG_CRYPTO_ECDSA_SIGN,
364 IMSG_CTL_PROCFD,
365 };
367 /* gmid.c */
368 char *data_dir(void);
369 void load_local_cert(struct vhost*, const char*, const char*);
371 /* gmid.c / ge.c */
372 void log_request(struct client *, char *, size_t);
374 /* config.c */
375 struct conf *config_new(void);
376 void config_purge(struct conf *);
377 int config_send(struct conf *);
378 int config_recv(struct conf *, struct imsg *);
380 /* crypto.c */
381 void crypto(struct privsep *, struct privsep_proc *);
382 void crypto_engine_init(struct conf *);
384 /* parse.y */
385 void yyerror(const char*, ...);
386 int parse_conf(struct conf *, const char*);
387 int cmdline_symset(char *);
389 /* mime.c */
390 void init_mime(struct mime*);
391 int add_mime(struct mime*, const char*, const char*);
392 int load_default_mime(struct mime*);
393 void sort_mime(struct mime *);
394 const char *mime(struct conf *, struct vhost*, const char*);
395 void free_mime(struct mime *);
397 /* server.c */
398 extern int shutting_down;
399 const char *vhost_lang(struct vhost*, const char*);
400 const char *vhost_default_mime(struct vhost*, const char*);
401 const char *vhost_index(struct vhost*, const char*);
402 int vhost_auto_index(struct vhost*, const char*);
403 int vhost_block_return(struct vhost*, const char*, int*, const char**);
404 int vhost_fastcgi(struct vhost*, const char*);
405 int vhost_dirfd(struct vhost*, const char*, size_t*);
406 int vhost_strip(struct vhost*, const char*);
407 X509_STORE *vhost_require_ca(struct vhost*, const char*);
408 int vhost_disable_log(struct vhost*, const char*);
410 void mark_nonblock(int);
411 void client_write(struct bufferevent *, void *);
412 void start_reply(struct client*, int, const char*);
413 void client_close(struct client *);
414 struct client *client_by_id(int);
415 void do_accept(int, short, void *);
416 void server_init(struct privsep *, struct privsep_proc *, void *);
417 int server_configure_done(struct conf *);
418 void server(struct privsep *ps, struct privsep_proc *);
420 int client_tree_cmp(struct client *, struct client *);
421 SPLAY_PROTOTYPE(client_tree_id, client, entry, client_tree_cmp);
423 /* dirs.c */
424 int scandir_fd(int, struct dirent***, int(*)(const struct dirent*),
425 int(*)(const struct dirent**, const struct dirent**));
426 int select_non_dot(const struct dirent*);
427 int select_non_dotdot(const struct dirent*);
429 /* fcgi.c */
430 void fcgi_read(struct bufferevent *, void *);
431 void fcgi_write(struct bufferevent *, void *);
432 void fcgi_error(struct bufferevent *, short, void *);
433 void fcgi_req(struct client *);
435 /* sandbox.c */
436 void sandbox_main_process(void);
437 void sandbox_server_process(void);
438 void sandbox_crypto_process(void);
439 void sandbox_logger_process(void);
441 /* utf8.c */
442 int valid_multibyte_utf8(struct parser*);
443 char *utf8_nth(char*, size_t);
445 /* iri.c */
446 int parse_iri(char*, struct iri*, const char**);
447 int serialize_iri(struct iri*, char*, size_t);
448 int encode_path(char *, size_t, const char *);
449 char *pct_decode_str(char *);
451 /* logger.c */
452 void logger(struct privsep *, struct privsep_proc *);
454 /* proxy.c */
455 int proxy_init(struct client *);
457 /* puny.c */
458 int puny_decode(const char*, char*, size_t, const char**);
460 /* utils.c */
461 void block_signals(void);
462 void unblock_signals(void);
463 int starts_with(const char*, const char*);
464 int ends_with(const char*, const char*);
465 ssize_t filesize(int);
466 char *absolutify_path(const char*);
467 char *xstrdup(const char*);
468 void *xcalloc(size_t, size_t);
469 void gen_certificate(const char*, const char*, const char*);
470 X509_STORE *load_ca(uint8_t *, size_t);
471 int validate_against_ca(X509_STORE*, const uint8_t*, size_t);
472 void ssl_error(const char *);
473 char *ssl_pubkey_hash(const uint8_t *, size_t);
474 EVP_PKEY *ssl_load_pkey(const uint8_t *, size_t);
475 struct vhost *new_vhost(void);
476 struct location *new_location(void);
477 struct proxy *new_proxy(void);
479 #endif