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 struct parser {
94 char *iri;
95 struct iri *parsed;
96 const char *err;
97 };
99 struct conf;
100 TAILQ_HEAD(addrhead, address);
101 struct address {
102 int ai_flags;
103 int ai_family;
104 int ai_socktype;
105 int ai_protocol;
106 struct sockaddr_storage ss;
107 socklen_t slen;
108 int16_t port;
110 /* used in the server */
111 struct conf *conf;
112 int sock;
113 struct event evsock; /* set if sock != -1 */
114 struct tls *ctx;
116 TAILQ_ENTRY(address) addrs;
117 };
119 TAILQ_HEAD(fcgihead, fcgi);
120 struct fcgi {
121 int id;
122 char path[PATH_MAX];
123 char port[32];
124 TAILQ_ENTRY(fcgi) fcgi;
125 };
127 TAILQ_HEAD(envhead, envlist);
128 struct envlist {
129 char name[FCGI_NAME_MAX];
130 char value[FCGI_VAL_MAX];
131 TAILQ_ENTRY(envlist) envs;
132 };
134 TAILQ_HEAD(aliashead, alist);
135 struct alist {
136 char alias[HOST_NAME_MAX + 1];
137 TAILQ_ENTRY(alist) aliases;
138 };
140 TAILQ_HEAD(proxyhead, proxy);
141 struct proxy {
142 char match_proto[32];
143 char match_host[HOST_NAME_MAX + 1];
144 char match_port[32];
146 char host[HOST_NAME_MAX + 1];
147 char port[32];
148 char sni[HOST_NAME_MAX];
149 int notls;
150 uint32_t protocols;
151 int noverifyname;
152 char *cert_path;
153 uint8_t *cert;
154 size_t certlen;
155 char *key_path;
156 uint8_t *key;
157 size_t keylen;
158 char *reqca_path;
159 X509_STORE *reqca;
161 TAILQ_ENTRY(proxy) proxies;
162 };
164 TAILQ_HEAD(lochead, location);
165 struct location {
166 char match[128];
167 char lang[32];
168 char default_mime[MEDIATYPE_TYPEMAX];
169 char index[PATH_MAX];
170 int auto_index; /* 0 auto, -1 off, 1 on */
171 int block_code;
172 char block_fmt[GEMINI_URL_LEN];
173 int strip;
174 char *reqca_path;
175 X509_STORE *reqca;
176 int disable_log;
177 int fcgi;
178 int nofcgi;
179 struct envhead params;
181 char dir[PATH_MAX];
182 int dirfd;
184 TAILQ_ENTRY(location) locations;
185 };
187 TAILQ_HEAD(vhosthead, vhost);
188 struct vhost {
189 char domain[HOST_NAME_MAX + 1];
190 char *cert_path;
191 char *key_path;
192 char *ocsp_path;
194 uint8_t *cert;
195 size_t certlen;
197 uint8_t *key;
198 size_t keylen;
200 uint8_t *ocsp;
201 size_t ocsplen;
203 TAILQ_ENTRY(vhost) vhosts;
205 struct addrhead addrs;
207 /*
208 * the first location rule is always '*' and holds the default
209 * settings for the vhost, then follows the "real" location
210 * rules as specified in the configuration.
211 */
212 struct lochead locations;
214 struct aliashead aliases;
215 struct proxyhead proxies;
216 };
218 struct etm { /* extension to mime */
219 char mime[MEDIATYPE_TYPEMAX];
220 char ext[MEDIATYPE_NAMEMAX];
221 };
223 struct mime {
224 struct etm *t;
225 size_t len;
226 size_t cap;
227 };
229 TAILQ_HEAD(pkihead, pki);
230 struct pki {
231 char *hash;
232 EVP_PKEY *pkey;
233 TAILQ_ENTRY(pki) pkis;
234 };
236 struct conf {
237 struct privsep *ps;
238 uint32_t protos;
239 struct mime mime;
240 char chroot[PATH_MAX];
241 char user[LOGIN_NAME_MAX];
242 int prefork;
243 int reload;
244 int use_privsep_crypto;
246 struct fcgihead fcgi;
247 struct vhosthead hosts;
248 struct pkihead pkis;
249 struct addrhead addrs;
250 };
252 extern const char *config_path;
254 extern int servpipes[PROC_MAX_INSTANCES];
255 extern int privsep_process;
257 typedef void (imsg_handlerfn)(struct imsgbuf*, struct imsg*, size_t);
259 enum {
260 REQUEST_UNDECIDED,
261 REQUEST_FILE,
262 REQUEST_DIR,
263 REQUEST_FCGI,
264 REQUEST_PROXY,
265 REQUEST_DONE,
266 };
268 struct client {
269 struct conf *conf;
270 struct address *addr;
271 uint32_t id;
272 struct tls *ctx;
273 char *req;
274 size_t reqlen;
275 struct iri iri;
276 char domain[DOMAIN_NAME_LEN];
277 char rhost[NI_MAXHOST];
278 char rserv[NI_MAXSERV];
280 struct bufferevent *bev;
282 int type;
284 struct bufferevent *cgibev;
286 struct proxy *proxy;
287 struct bufferevent *proxybev;
288 struct tls *proxyctx;
289 int proxyevset;
290 struct event proxyev;
292 char *header;
294 int code;
295 const char *meta;
296 int fd, pfd;
297 struct dirent **dir;
298 int dirlen, diroff;
300 /* big enough to store STATUS + SPACE + META + CRLF */
301 char sbuf[1029];
302 size_t soff;
304 struct sockaddr_storage raddr;
305 socklen_t raddrlen;
307 struct vhost *host; /* host they're talking to */
308 size_t loc; /* location matched */
310 SPLAY_ENTRY(client) entry;
311 };
312 SPLAY_HEAD(client_tree_id, client);
313 extern struct client_tree_id clients;
315 struct connreq {
316 char host[NI_MAXHOST];
317 char port[NI_MAXSERV];
318 int flag;
319 };
321 enum imsg_type {
322 IMSG_FCGI_REQ,
323 IMSG_FCGI_FD,
324 IMSG_CONN_REQ,
325 IMSG_CONN_FD,
326 IMSG_LOG,
327 IMSG_LOG_REQUEST,
328 IMSG_LOG_TYPE,
330 IMSG_RECONF_START, /* 7 */
331 IMSG_RECONF_MIME,
332 IMSG_RECONF_PROTOS,
333 IMSG_RECONF_SOCK,
334 IMSG_RECONF_FCGI,
335 IMSG_RECONF_HOST,
336 IMSG_RECONF_CERT,
337 IMSG_RECONF_KEY,
338 IMSG_RECONF_OCSP,
339 IMSG_RECONF_HOST_ADDR,
340 IMSG_RECONF_LOC,
341 IMSG_RECONF_ENV,
342 IMSG_RECONF_ALIAS,
343 IMSG_RECONF_PROXY,
344 IMSG_RECONF_PROXY_CERT,
345 IMSG_RECONF_PROXY_KEY,
346 IMSG_RECONF_END,
347 IMSG_RECONF_DONE,
349 IMSG_CRYPTO_RSA_PRIVENC,
350 IMSG_CRYPTO_RSA_PRIVDEC,
351 IMSG_CRYPTO_ECDSA_SIGN,
353 IMSG_CTL_PROCFD,
354 };
356 /* gmid.c */
357 char *data_dir(void);
358 void load_local_cert(struct vhost*, const char*, const char*);
360 /* gmid.c / ge.c */
361 void log_request(struct client *, int, const char *);
363 /* config.c */
364 struct conf *config_new(void);
365 void config_purge(struct conf *);
366 int config_send(struct conf *);
367 int config_recv(struct conf *, struct imsg *);
369 /* crypto.c */
370 void crypto(struct privsep *, struct privsep_proc *);
371 void crypto_engine_init(struct conf *);
373 /* parse.y */
374 void yyerror(const char*, ...);
375 int parse_conf(struct conf *, const char*);
376 int cmdline_symset(char *);
378 /* mime.c */
379 void init_mime(struct mime*);
380 int add_mime(struct mime*, const char*, const char*);
381 int load_default_mime(struct mime*);
382 void sort_mime(struct mime *);
383 const char *mime(struct conf *, struct vhost*, const char*);
384 void free_mime(struct mime *);
386 /* server.c */
387 extern int shutting_down;
388 const char *vhost_lang(struct vhost*, const char*);
389 const char *vhost_default_mime(struct vhost*, const char*);
390 const char *vhost_index(struct vhost*, const char*);
391 int vhost_auto_index(struct vhost*, const char*);
392 int vhost_block_return(struct vhost*, const char*, int*, const char**);
393 struct location *vhost_fastcgi(struct vhost*, const char*);
394 int vhost_dirfd(struct vhost*, const char*, size_t*);
395 int vhost_strip(struct vhost*, const char*);
396 X509_STORE *vhost_require_ca(struct vhost*, const char*);
397 int vhost_disable_log(struct vhost*, const char*);
399 void mark_nonblock(int);
400 void client_write(struct bufferevent *, void *);
401 void start_reply(struct client*, int, const char*);
402 void client_close(struct client *);
403 struct client *client_by_id(int);
404 void server_accept(int, short, void *);
405 void server_init(struct privsep *, struct privsep_proc *, void *);
406 int server_configure_done(struct conf *);
407 void server(struct privsep *ps, struct privsep_proc *);
409 int client_tree_cmp(struct client *, struct client *);
410 SPLAY_PROTOTYPE(client_tree_id, client, entry, client_tree_cmp);
412 /* dirs.c */
413 int scandir_fd(int, struct dirent***, int(*)(const struct dirent*),
414 int(*)(const struct dirent**, const struct dirent**));
415 int select_non_dot(const struct dirent*);
416 int select_non_dotdot(const struct dirent*);
418 /* fcgi.c */
419 void fcgi_read(struct bufferevent *, void *);
420 void fcgi_write(struct bufferevent *, void *);
421 void fcgi_error(struct bufferevent *, short, void *);
422 void fcgi_req(struct client *, struct location *);
424 /* sandbox.c */
425 void sandbox_main_process(void);
426 void sandbox_server_process(void);
427 void sandbox_crypto_process(void);
428 void sandbox_logger_process(void);
430 /* utf8.c */
431 int valid_multibyte_utf8(struct parser*);
432 char *utf8_nth(char*, size_t);
434 /* logger.c */
435 void logger(struct privsep *, struct privsep_proc *);
437 /* proxy.c */
438 int proxy_init(struct client *);
440 /* puny.c */
441 int puny_decode(const char*, char*, size_t, const char**);
443 /* utils.c */
444 void block_signals(void);
445 void unblock_signals(void);
446 int starts_with(const char*, const char*);
447 int ends_with(const char*, const char*);
448 ssize_t filesize(int);
449 char *absolutify_path(const char*);
450 char *xstrdup(const char*);
451 void *xcalloc(size_t, size_t);
452 void gen_certificate(const char*, const char*, const char*);
453 X509_STORE *load_ca(uint8_t *, size_t);
454 int validate_against_ca(X509_STORE*, const uint8_t*, size_t);
455 void ssl_error(const char *);
456 char *ssl_pubkey_hash(const uint8_t *, size_t);
457 EVP_PKEY *ssl_load_pkey(const uint8_t *, size_t);
458 struct vhost *new_vhost(void);
459 struct location *new_location(void);
460 struct proxy *new_proxy(void);
462 #endif