Blob


1 /*
2 * Copyright (c) 2020, 2021, 2022 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 /* forward declaration */
86 struct privsep;
87 struct privsep_proc;
89 struct iri {
90 char *schema;
91 char *host;
92 char *port;
93 uint16_t port_no;
94 char *path;
95 char *query;
96 char *fragment;
97 };
99 struct parser {
100 char *iri;
101 struct iri *parsed;
102 const char *err;
103 };
105 TAILQ_HEAD(fcgihead, fcgi);
106 struct fcgi {
107 int id;
108 char path[PATH_MAX];
109 char port[32];
110 TAILQ_ENTRY(fcgi) fcgi;
111 };
113 TAILQ_HEAD(proxyhead, proxy);
114 struct proxy {
115 char match_proto[32];
116 char match_host[HOST_NAME_MAX + 1];
117 char match_port[32];
119 char host[HOST_NAME_MAX + 1];
120 char port[32];
121 char sni[HOST_NAME_MAX];
122 int notls;
123 uint32_t protocols;
124 int noverifyname;
125 char *cert_path;
126 uint8_t *cert;
127 size_t certlen;
128 char *key_path;
129 uint8_t *key;
130 size_t keylen;
131 char *reqca_path;
132 X509_STORE *reqca;
134 TAILQ_ENTRY(proxy) proxies;
135 };
137 TAILQ_HEAD(lochead, location);
138 struct location {
139 char match[128];
140 char lang[32];
141 char default_mime[MEDIATYPE_TYPEMAX];
142 char index[PATH_MAX];
143 int auto_index; /* 0 auto, -1 off, 1 on */
144 int block_code;
145 char block_fmt[GEMINI_URL_LEN];
146 int strip;
147 char *reqca_path;
148 X509_STORE *reqca;
149 int disable_log;
150 int fcgi;
152 char dir[PATH_MAX];
153 int dirfd;
155 TAILQ_ENTRY(location) locations;
156 };
158 TAILQ_HEAD(envhead, envlist);
159 struct envlist {
160 char name[FCGI_NAME_MAX];
161 char value[FCGI_VAL_MAX];
162 TAILQ_ENTRY(envlist) envs;
163 };
165 TAILQ_HEAD(aliashead, alist);
166 struct alist {
167 char alias[HOST_NAME_MAX + 1];
168 TAILQ_ENTRY(alist) aliases;
169 };
171 TAILQ_HEAD(vhosthead, vhost);
172 struct vhost {
173 char domain[HOST_NAME_MAX + 1];
174 char *cert_path;
175 char *key_path;
176 char *ocsp_path;
178 uint8_t *cert;
179 size_t certlen;
181 uint8_t *key;
182 size_t keylen;
184 uint8_t *ocsp;
185 size_t ocsplen;
187 TAILQ_ENTRY(vhost) vhosts;
189 /*
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.
193 */
194 struct lochead locations;
196 struct envhead params;
197 struct aliashead aliases;
198 struct proxyhead proxies;
199 };
201 struct etm { /* extension to mime */
202 char mime[MEDIATYPE_TYPEMAX];
203 char ext[MEDIATYPE_NAMEMAX];
204 };
206 struct mime {
207 struct etm *t;
208 size_t len;
209 size_t cap;
210 };
212 struct conf {
213 struct privsep *ps;
214 int port;
215 int ipv6;
216 uint32_t protos;
217 struct mime mime;
218 char chroot[PATH_MAX];
219 char user[LOGIN_NAME_MAX];
220 int prefork;
221 int reload;
223 int sock4;
224 struct event evsock4;
225 int sock6;
226 struct event evsock6;
228 struct fcgihead fcgi;
229 struct vhosthead hosts;
230 };
232 extern const char *config_path;
234 extern int servpipes[PROC_MAX_INSTANCES];
235 extern int privsep_process;
237 typedef void (imsg_handlerfn)(struct imsgbuf*, struct imsg*, size_t);
239 enum {
240 REQUEST_UNDECIDED,
241 REQUEST_FILE,
242 REQUEST_DIR,
243 REQUEST_FCGI,
244 REQUEST_PROXY,
245 REQUEST_DONE,
246 };
248 struct client {
249 struct conf *conf;
250 uint32_t id;
251 struct tls *ctx;
252 char *req;
253 size_t reqlen;
254 struct iri iri;
255 char domain[DOMAIN_NAME_LEN];
257 struct bufferevent *bev;
259 int type;
261 struct bufferevent *cgibev;
263 struct proxy *proxy;
264 struct bufferevent *proxybev;
265 struct tls *proxyctx;
266 int proxyevset;
267 struct event proxyev;
269 char *header;
271 int code;
272 const char *meta;
273 int fd, pfd;
274 struct dirent **dir;
275 int dirlen, diroff;
277 /* big enough to store STATUS + SPACE + META + CRLF */
278 char sbuf[1029];
279 ssize_t len, off;
281 struct sockaddr_storage addr;
282 struct vhost *host; /* host they're talking to */
283 size_t loc; /* location matched */
285 SPLAY_ENTRY(client) entry;
286 };
287 SPLAY_HEAD(client_tree_id, client);
288 extern struct client_tree_id clients;
290 struct connreq {
291 char host[NI_MAXHOST];
292 char port[NI_MAXSERV];
293 int flag;
294 };
296 enum {
297 FILE_EXISTS,
298 FILE_DIRECTORY,
299 FILE_MISSING,
300 };
302 enum imsg_type {
303 IMSG_FCGI_REQ,
304 IMSG_FCGI_FD,
305 IMSG_CONN_REQ,
306 IMSG_CONN_FD,
307 IMSG_LOG,
308 IMSG_LOG_REQUEST,
309 IMSG_LOG_TYPE,
311 IMSG_RECONF_START, /* 7 */
312 IMSG_RECONF_MIME,
313 IMSG_RECONF_PROTOS,
314 IMSG_RECONF_PORT,
315 IMSG_RECONF_SOCK4,
316 IMSG_RECONF_SOCK6,
317 IMSG_RECONF_FCGI,
318 IMSG_RECONF_HOST,
319 IMSG_RECONF_CERT,
320 IMSG_RECONF_KEY,
321 IMSG_RECONF_OCSP,
322 IMSG_RECONF_LOC,
323 IMSG_RECONF_ENV,
324 IMSG_RECONF_ALIAS,
325 IMSG_RECONF_PROXY,
326 IMSG_RECONF_PROXY_CERT,
327 IMSG_RECONF_PROXY_KEY,
328 IMSG_RECONF_END,
329 IMSG_RECONF_DONE,
331 IMSG_CTL_PROCFD,
332 };
334 /* gmid.c */
335 char *data_dir(void);
336 void load_local_cert(struct vhost*, const char*, const char*);
338 /* gmid.c / ge.c */
339 void log_request(struct client *, char *, size_t);
341 /* config.c */
342 struct conf *config_new(void);
343 void config_purge(struct conf *);
344 int config_send(struct conf *);
345 int config_recv(struct conf *, struct imsg *);
347 /* parse.y */
348 void yyerror(const char*, ...);
349 int parse_conf(struct conf *, const char*);
350 void print_conf(void);
351 int cmdline_symset(char *);
353 /* mime.c */
354 void init_mime(struct mime*);
355 int add_mime(struct mime*, const char*, const char*);
356 int load_default_mime(struct mime*);
357 void sort_mime(struct mime *);
358 const char *mime(struct conf *, struct vhost*, const char*);
359 void free_mime(struct mime *);
361 /* server.c */
362 extern int shutting_down;
363 const char *vhost_lang(struct vhost*, const char*);
364 const char *vhost_default_mime(struct vhost*, const char*);
365 const char *vhost_index(struct vhost*, const char*);
366 int vhost_auto_index(struct vhost*, const char*);
367 int vhost_block_return(struct vhost*, const char*, int*, const char**);
368 int vhost_fastcgi(struct vhost*, const char*);
369 int vhost_dirfd(struct vhost*, const char*, size_t*);
370 int vhost_strip(struct vhost*, const char*);
371 X509_STORE *vhost_require_ca(struct vhost*, const char*);
372 int vhost_disable_log(struct vhost*, const char*);
374 void mark_nonblock(int);
375 void client_write(struct bufferevent *, void *);
376 void start_reply(struct client*, int, const char*);
377 void client_close(struct client *);
378 struct client *client_by_id(int);
379 void do_accept(int, short, void *);
380 void server_init(struct privsep *, struct privsep_proc *, void *);
381 int server_configure_done(struct conf *);
382 void server(struct privsep *ps, struct privsep_proc *);
384 int client_tree_cmp(struct client *, struct client *);
385 SPLAY_PROTOTYPE(client_tree_id, client, entry, client_tree_cmp);
387 /* dirs.c */
388 int scandir_fd(int, struct dirent***, int(*)(const struct dirent*),
389 int(*)(const struct dirent**, const struct dirent**));
390 int select_non_dot(const struct dirent*);
391 int select_non_dotdot(const struct dirent*);
393 /* fcgi.c */
394 void fcgi_read(struct bufferevent *, void *);
395 void fcgi_write(struct bufferevent *, void *);
396 void fcgi_error(struct bufferevent *, short, void *);
397 void fcgi_req(struct client *);
399 /* sandbox.c */
400 void sandbox_main_process(void);
401 void sandbox_server_process(void);
402 void sandbox_logger_process(void);
404 /* utf8.c */
405 int valid_multibyte_utf8(struct parser*);
406 char *utf8_nth(char*, size_t);
408 /* iri.c */
409 int parse_iri(char*, struct iri*, const char**);
410 int serialize_iri(struct iri*, char*, size_t);
411 int encode_path(char *, size_t, const char *);
412 char *pct_decode_str(char *);
414 /* logger.c */
415 void logger(struct privsep *, struct privsep_proc *);
417 /* proxy.c */
418 int proxy_init(struct client *);
420 /* puny.c */
421 int puny_decode(const char*, char*, size_t, const char**);
423 /* utils.c */
424 void block_signals(void);
425 void unblock_signals(void);
426 int starts_with(const char*, const char*);
427 int ends_with(const char*, const char*);
428 ssize_t filesize(int);
429 char *absolutify_path(const char*);
430 char *xstrdup(const char*);
431 void *xcalloc(size_t, size_t);
432 void gen_certificate(const char*, const char*, const char*);
433 X509_STORE *load_ca(int);
434 int validate_against_ca(X509_STORE*, const uint8_t*, size_t);
435 struct vhost *new_vhost(void);
436 struct location *new_location(void);
437 struct proxy *new_proxy(void);
439 #endif