Blob


1 /*
2 * Copyright (c) 2020 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 <event.h>
30 #include <limits.h>
31 #include <netdb.h>
32 #include <signal.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <time.h>
36 #include <tls.h>
37 #include <unistd.h>
39 #include <openssl/x509.h>
41 #define GMID_VERSION "gmid/1.7"
43 #define GEMINI_URL_LEN (1024+3) /* URL max len + \r\n + \0 */
45 #define SUCCESS 20
46 #define TEMP_REDIRECT 30
47 #define TEMP_FAILURE 40
48 #define CGI_ERROR 42
49 #define NOT_FOUND 51
50 #define PROXY_REFUSED 53
51 #define BAD_REQUEST 59
52 #define CLIENT_CERT_REQ 60
53 #define CERT_NOT_AUTH 61
55 #define MAX_USERS 64
57 /* maximum hostname and label length, +1 for the NUL-terminator */
58 #define DOMAIN_NAME_LEN (253+1)
59 #define LABEL_LEN (63+1)
61 #define FCGI_MAX 32
62 #define PROC_MAX 16
64 struct fcgi {
65 int id;
66 char *path;
67 char *port;
68 char *prog;
69 int fd;
70 struct event e;
72 #define FCGI_OFF 0
73 #define FCGI_INFLIGHT 1
74 #define FCGI_READY 2
75 int s;
76 };
77 extern struct fcgi fcgi[FCGI_MAX];
79 TAILQ_HEAD(lochead, location);
80 struct location {
81 const char *match;
82 const char *lang;
83 const char *default_mime;
84 const char *index;
85 int auto_index; /* 0 auto, -1 off, 1 on */
86 int block_code;
87 const char *block_fmt;
88 int strip;
89 X509_STORE *reqca;
90 int disable_log;
91 int fcgi;
93 const char *dir;
94 int dirfd;
96 TAILQ_ENTRY(location) locations;
97 };
99 TAILQ_HEAD(envhead, envlist);
100 struct envlist {
101 char *name;
102 char *value;
103 TAILQ_ENTRY(envlist) envs;
104 };
106 TAILQ_HEAD(aliashead, alist);
107 struct alist {
108 char *alias;
109 TAILQ_ENTRY(alist) aliases;
110 };
112 extern TAILQ_HEAD(vhosthead, vhost) hosts;
113 struct vhost {
114 const char *domain;
115 const char *cert;
116 const char *key;
117 const char *cgi;
118 const char *entrypoint;
120 TAILQ_ENTRY(vhost) vhosts;
122 /* the first location rule is always '*' and holds the default
123 * settings for the vhost, then follows the "real" location
124 * rules as specified in the configuration. */
125 struct lochead locations;
127 struct envhead env;
128 struct envhead params;
129 struct aliashead aliases;
130 };
132 struct etm { /* extension to mime */
133 const char *mime;
134 const char *ext;
135 };
137 struct mime {
138 struct etm *t;
139 size_t len;
140 size_t cap;
141 };
143 struct conf {
144 /* from command line */
145 int foreground;
146 int verbose;
148 /* in the config */
149 int port;
150 int ipv6;
151 uint32_t protos;
152 struct mime mime;
153 char *chroot;
154 char *user;
155 int prefork;
156 };
158 extern const char *config_path;
159 extern struct conf conf;
161 extern struct imsgbuf logibuf, exibuf, servibuf[PROC_MAX];
163 extern int servpipes[PROC_MAX];
165 struct iri {
166 char *schema;
167 char *host;
168 char *port;
169 uint16_t port_no;
170 char *path;
171 char *query;
172 char *fragment;
173 };
175 struct parser {
176 char *iri;
177 struct iri *parsed;
178 const char *err;
179 };
181 struct mbuf {
182 size_t len;
183 size_t off;
184 TAILQ_ENTRY(mbuf) mbufs;
185 char data[];
186 };
187 TAILQ_HEAD(mbufhead, mbuf);
189 typedef void (imsg_handlerfn)(struct imsgbuf*, struct imsg*, size_t);
191 typedef void (*statefn)(int, short, void*);
193 /*
194 * DFA: handle_handshake is the initial state, close_conn the final.
195 * Sometimes we have an enter_* function to handle the state switch.
197 * handle_handshake -> handle_open_conn
198 * handle_handshake -> close_conn // on err
200 * handle_open_conn -> handle_cgi_reply // via open_file/dir/...
201 * handle_open_conn -> send_fcgi_req // via apply_fastcgi, IMSG_FCGI_FD
202 * handle_open_conn -> handle_dirlist // ...same
203 * handle_open_conn -> send_file // ...same
204 * handle_open_conn -> start_reply // on error
206 * handle_cgi_reply -> handle_cgi // after logging the CGI reply
207 * handle_cgi_reply -> start_reply // on error
209 * handle_cgi -> close_conn
211 * send_fcgi_req -> copy_mbuf // via handle_fcgi
212 * handle_fcgi -> close_all // on error
213 * copy_mbuf -> close_conn // on success/error
215 * handle_dirlist -> send_directory_listing
216 * handle_dirlist -> close_conn // on error
218 * send_directory_listing -> close_conn
220 * send_file -> close_conn
221 */
222 struct client {
223 int id;
224 struct tls *ctx;
225 char req[GEMINI_URL_LEN];
226 struct iri iri;
227 char domain[DOMAIN_NAME_LEN];
229 /*
230 * start_reply uses this to know what function call after the
231 * reply. It's also used as sentinel value in fastcgi to know
232 * if the server has closed the request.
233 */
234 statefn next;
236 int code;
237 const char *meta;
238 int fd, pfd;
239 struct dirent **dir;
240 int dirlen, diroff;
241 int fcgi;
243 /* big enough to store STATUS + SPACE + META + CRLF */
244 char sbuf[1029];
245 ssize_t len, off;
247 struct mbufhead mbufhead;
249 struct sockaddr_storage addr;
250 struct vhost *host; /* host they're talking to */
251 size_t loc; /* location matched */
252 };
254 extern struct client clients[MAX_USERS];
256 struct cgireq {
257 char buf[GEMINI_URL_LEN];
259 size_t iri_schema_off;
260 size_t iri_host_off;
261 size_t iri_port_off;
262 size_t iri_path_off;
263 size_t iri_query_off;
264 size_t iri_fragment_off;
265 int iri_portno;
267 char spath[PATH_MAX+1];
268 char relpath[PATH_MAX+1];
269 char addr[NI_MAXHOST+1];
271 /* AFAIK there isn't an upper limit for these two fields. */
272 char subject[64+1];
273 char issuer[64+1];
275 char hash[128+1];
276 char version[8];
277 char cipher[32];
278 int cipher_strength;
279 time_t notbefore;
280 time_t notafter;
282 size_t host_off;
283 size_t loc_off;
284 };
286 enum {
287 FILE_EXISTS,
288 FILE_EXECUTABLE,
289 FILE_DIRECTORY,
290 FILE_MISSING,
291 };
293 enum imsg_type {
294 IMSG_CGI_REQ,
295 IMSG_CGI_RES,
296 IMSG_FCGI_REQ,
297 IMSG_FCGI_FD,
298 IMSG_LOG,
299 IMSG_LOG_TYPE,
300 IMSG_QUIT,
301 };
303 /* gmid.c */
304 char *data_dir(void);
305 void load_local_cert(const char*, const char*);
306 void load_vhosts(void);
307 int make_socket(int, int);
308 void setup_tls(void);
309 void init_config(void);
310 void free_config(void);
311 void drop_priv(void);
313 void yyerror(const char*, ...);
314 int parse_portno(const char*);
315 void parse_conf(const char*);
317 /* log.c */
318 void fatal(const char*, ...)
319 __attribute__((format (printf, 1, 2)))
320 __attribute__((__noreturn__));
322 #define LOG_ATTR_FMT __attribute__((format (printf, 2, 3)))
323 void log_err(struct client*, const char*, ...) LOG_ATTR_FMT;
324 void log_warn(struct client*, const char*, ...) LOG_ATTR_FMT;
325 void log_notice(struct client*, const char*, ...) LOG_ATTR_FMT;
326 void log_info(struct client*, const char*, ...) LOG_ATTR_FMT;
327 void log_debug(struct client*, const char*, ...) LOG_ATTR_FMT;
328 void log_request(struct client*, char*, size_t);
329 int logger_main(int, struct imsgbuf*);
331 /* mime.c */
332 void init_mime(struct mime*);
333 void add_mime(struct mime*, const char*, const char*);
334 void load_default_mime(struct mime*);
335 const char *mime(struct vhost*, const char*);
337 /* server.c */
338 const char *vhost_lang(struct vhost*, const char*);
339 const char *vhost_default_mime(struct vhost*, const char*);
340 const char *vhost_index(struct vhost*, const char*);
341 int vhost_auto_index(struct vhost*, const char*);
342 int vhost_block_return(struct vhost*, const char*, int*, const char**);
343 int vhost_fastcgi(struct vhost*, const char*);
344 int vhost_dirfd(struct vhost*, const char*, size_t*);
345 int vhost_strip(struct vhost*, const char*);
346 X509_STORE *vhost_require_ca(struct vhost*, const char*);
347 int vhost_disable_log(struct vhost*, const char*);
349 void mark_nonblock(int);
350 void start_reply(struct client*, int, const char*);
351 void close_conn(int, short, void*);
352 struct client *try_client_by_id(int);
353 void loop(struct tls*, int, int, struct imsgbuf*);
355 /* dirs.c */
356 int scandir_fd(int, struct dirent***, int(*)(const struct dirent*),
357 int(*)(const struct dirent**, const struct dirent**));
358 int select_non_dot(const struct dirent*);
359 int select_non_dotdot(const struct dirent*);
361 /* ex.c */
362 int send_string(int, const char*);
363 int recv_string(int, char**);
364 int send_iri(int, struct iri*);
365 int recv_iri(int, struct iri*);
366 void free_recvd_iri(struct iri*);
367 int send_vhost(int, struct vhost*);
368 int recv_vhost(int, struct vhost**);
369 int send_time(int, time_t);
370 int recv_time(int, time_t*);
371 int send_fd(int, int);
372 int recv_fd(int);
373 int executor_main(struct imsgbuf*);
375 /* fcgi.c */
376 void handle_fcgi(int, short, void*);
377 void send_fcgi_req(struct fcgi*, struct client*);
379 /* sandbox.c */
380 void sandbox_server_process(void);
381 void sandbox_executor_process(void);
382 void sandbox_logger_process(void);
384 /* utf8.c */
385 int valid_multibyte_utf8(struct parser*);
386 char *utf8_nth(char*, size_t);
388 /* iri.c */
389 int parse_iri(char*, struct iri*, const char**);
390 int trim_req_iri(char*, const char **);
391 int serialize_iri(struct iri*, char*, size_t);
392 char *pct_decode_str(char *);
394 /* puny.c */
395 int puny_decode(const char*, char*, size_t, const char**);
397 /* utils.c */
398 void block_signals(void);
399 void unblock_signals(void);
400 int starts_with(const char*, const char*);
401 int ends_with(const char*, const char*);
402 ssize_t filesize(int);
403 char *absolutify_path(const char*);
404 char *xstrdup(const char*);
405 void *xcalloc(size_t, size_t);
406 void gen_certificate(const char*, const char*, const char*);
407 X509_STORE *load_ca(const char*);
408 int validate_against_ca(X509_STORE*, const uint8_t*, size_t);
409 void dispatch_imsg(struct imsgbuf*, imsg_handlerfn**, size_t);
411 #endif