Blame


1 d5aba4c7 2020-12-24 op /*
2 d5aba4c7 2020-12-24 op * Copyright (c) 2020 Omar Polo <op@omarpolo.com>
3 d5aba4c7 2020-12-24 op *
4 d5aba4c7 2020-12-24 op * Permission to use, copy, modify, and distribute this software for any
5 d5aba4c7 2020-12-24 op * purpose with or without fee is hereby granted, provided that the above
6 d5aba4c7 2020-12-24 op * copyright notice and this permission notice appear in all copies.
7 d5aba4c7 2020-12-24 op *
8 d5aba4c7 2020-12-24 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 d5aba4c7 2020-12-24 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 d5aba4c7 2020-12-24 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 d5aba4c7 2020-12-24 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 d5aba4c7 2020-12-24 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 d5aba4c7 2020-12-24 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 d5aba4c7 2020-12-24 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 d5aba4c7 2020-12-24 op */
16 d5aba4c7 2020-12-24 op
17 488f059a 2020-12-24 op #ifndef GMID_H
18 488f059a 2020-12-24 op #define GMID_H
19 488f059a 2020-12-24 op
20 252908e6 2021-01-24 op #include <sys/socket.h>
21 252908e6 2021-01-24 op #include <sys/types.h>
22 252908e6 2021-01-24 op
23 488f059a 2020-12-24 op #include <arpa/inet.h>
24 488f059a 2020-12-24 op #include <netinet/in.h>
25 488f059a 2020-12-24 op
26 252908e6 2021-01-24 op #include <dirent.h>
27 488f059a 2020-12-24 op #include <poll.h>
28 488f059a 2020-12-24 op #include <stdio.h>
29 488f059a 2020-12-24 op #include <stdlib.h>
30 488f059a 2020-12-24 op #include <syslog.h>
31 488f059a 2020-12-24 op #include <tls.h>
32 488f059a 2020-12-24 op #include <unistd.h>
33 488f059a 2020-12-24 op
34 12042ad7 2021-01-21 op #include "config.h"
35 12042ad7 2021-01-21 op
36 488f059a 2020-12-24 op #ifndef INFTIM
37 488f059a 2020-12-24 op # define INFTIM -1
38 488f059a 2020-12-24 op #endif
39 488f059a 2020-12-24 op
40 488f059a 2020-12-24 op #define GEMINI_URL_LEN (1024+3) /* URL max len + \r\n + \0 */
41 488f059a 2020-12-24 op
42 488f059a 2020-12-24 op #define SUCCESS 20
43 0be51733 2021-01-20 op #define TEMP_REDIRECT 30
44 488f059a 2020-12-24 op #define TEMP_FAILURE 40
45 488f059a 2020-12-24 op #define NOT_FOUND 51
46 7b1d9790 2021-01-11 op #define PROXY_REFUSED 53
47 488f059a 2020-12-24 op #define BAD_REQUEST 59
48 488f059a 2020-12-24 op
49 488f059a 2020-12-24 op #define MAX_USERS 64
50 488f059a 2020-12-24 op
51 15902770 2021-01-15 op #define HOSTSLEN 64
52 c8b74339 2021-01-24 op #define LOCLEN 32
53 15902770 2021-01-15 op
54 35cf19e3 2021-01-28 op /* maximum hostname and label length, +1 for the NUL-terminator */
55 35cf19e3 2021-01-28 op #define DOMAIN_NAME_LEN (253+1)
56 35cf19e3 2021-01-28 op #define LABEL_LEN (63+1)
57 3300cbe0 2021-01-27 op
58 dafb57b8 2021-01-15 op #define LOGE(c, fmt, ...) logs(LOG_ERR, c, fmt, __VA_ARGS__)
59 dafb57b8 2021-01-15 op #define LOGW(c, fmt, ...) logs(LOG_WARNING, c, fmt, __VA_ARGS__)
60 dafb57b8 2021-01-15 op #define LOGN(c, fmt, ...) logs(LOG_NOTICE, c, fmt, __VA_ARGS__)
61 dafb57b8 2021-01-15 op #define LOGI(c, fmt, ...) logs(LOG_INFO, c, fmt, __VA_ARGS__)
62 dafb57b8 2021-01-15 op #define LOGD(c, fmt, ...) logs(LOG_DEBUG, c, fmt, __VA_ARGS__)
63 dafb57b8 2021-01-15 op
64 c8b74339 2021-01-24 op struct location {
65 fe5967cd 2021-01-27 op const char *match;
66 fe5967cd 2021-01-27 op const char *lang;
67 fe5967cd 2021-01-27 op const char *default_mime;
68 fe5967cd 2021-01-27 op const char *index;
69 252908e6 2021-01-24 op int auto_index; /* 0 auto, -1 off, 1 on */
70 c8b74339 2021-01-24 op };
71 c8b74339 2021-01-24 op
72 15902770 2021-01-15 op struct vhost {
73 15902770 2021-01-15 op const char *domain;
74 15902770 2021-01-15 op const char *cert;
75 15902770 2021-01-15 op const char *key;
76 15902770 2021-01-15 op const char *dir;
77 15902770 2021-01-15 op const char *cgi;
78 15902770 2021-01-15 op int dirfd;
79 c8b74339 2021-01-24 op struct location locations[LOCLEN];
80 15902770 2021-01-15 op };
81 15902770 2021-01-15 op
82 15902770 2021-01-15 op extern struct vhost hosts[HOSTSLEN];
83 15902770 2021-01-15 op
84 a010b0dd 2021-01-18 op struct etm { /* extension to mime */
85 a010b0dd 2021-01-18 op const char *mime;
86 a010b0dd 2021-01-18 op const char *ext;
87 a010b0dd 2021-01-18 op };
88 a010b0dd 2021-01-18 op
89 b2a6b613 2021-01-21 op struct mime {
90 a010b0dd 2021-01-18 op struct etm *t;
91 a010b0dd 2021-01-18 op size_t len;
92 a010b0dd 2021-01-18 op size_t cap;
93 a010b0dd 2021-01-18 op };
94 a010b0dd 2021-01-18 op
95 15902770 2021-01-15 op struct conf {
96 ae08ec7d 2021-01-25 op int port;
97 ae08ec7d 2021-01-25 op int ipv6;
98 ae08ec7d 2021-01-25 op uint32_t protos;
99 ae08ec7d 2021-01-25 op struct mime mime;
100 ae08ec7d 2021-01-25 op char *chroot;
101 ae08ec7d 2021-01-25 op char *user;
102 15902770 2021-01-15 op };
103 15902770 2021-01-15 op
104 15902770 2021-01-15 op extern struct conf conf;
105 881a9dd9 2021-01-16 op extern int exfd;
106 15902770 2021-01-15 op
107 0be51733 2021-01-20 op struct iri {
108 0be51733 2021-01-20 op char *schema;
109 0be51733 2021-01-20 op char *host;
110 0be51733 2021-01-20 op char *port;
111 0be51733 2021-01-20 op uint16_t port_no;
112 0be51733 2021-01-20 op char *path;
113 0be51733 2021-01-20 op char *query;
114 0be51733 2021-01-20 op char *fragment;
115 0be51733 2021-01-20 op };
116 0be51733 2021-01-20 op
117 0be51733 2021-01-20 op struct parser {
118 0be51733 2021-01-20 op char *iri;
119 0be51733 2021-01-20 op struct iri *parsed;
120 0be51733 2021-01-20 op const char *err;
121 0be51733 2021-01-20 op };
122 0be51733 2021-01-20 op
123 488f059a 2020-12-24 op enum {
124 9862b637 2021-01-13 op S_HANDSHAKE,
125 488f059a 2020-12-24 op S_OPEN,
126 488f059a 2020-12-24 op S_INITIALIZING,
127 a87f6625 2021-01-24 op S_SENDING_FILE,
128 252908e6 2021-01-24 op S_SENDING_DIR,
129 a87f6625 2021-01-24 op S_SENDING_CGI,
130 488f059a 2020-12-24 op S_CLOSING,
131 488f059a 2020-12-24 op };
132 488f059a 2020-12-24 op
133 488f059a 2020-12-24 op struct client {
134 488f059a 2020-12-24 op struct tls *ctx;
135 0be51733 2021-01-20 op char req[GEMINI_URL_LEN];
136 0be51733 2021-01-20 op struct iri iri;
137 3300cbe0 2021-01-27 op char domain[DOMAIN_NAME_LEN];
138 a87f6625 2021-01-24 op int state, next;
139 488f059a 2020-12-24 op int code;
140 488f059a 2020-12-24 op const char *meta;
141 488f059a 2020-12-24 op int fd, waiting_on_child;
142 488f059a 2020-12-24 op char sbuf[1024]; /* static buffer */
143 488f059a 2020-12-24 op void *buf, *i; /* mmap buffer */
144 488f059a 2020-12-24 op ssize_t len, off; /* mmap/static buffer */
145 252908e6 2021-01-24 op DIR *dir;
146 709d6e5e 2021-01-10 op struct sockaddr_storage addr;
147 f7b816dc 2021-01-15 op struct vhost *host; /* host she's talking to */
148 488f059a 2020-12-24 op };
149 488f059a 2020-12-24 op
150 488f059a 2020-12-24 op enum {
151 488f059a 2020-12-24 op FILE_EXISTS,
152 488f059a 2020-12-24 op FILE_EXECUTABLE,
153 488f059a 2020-12-24 op FILE_DIRECTORY,
154 488f059a 2020-12-24 op FILE_MISSING,
155 488f059a 2020-12-24 op };
156 488f059a 2020-12-24 op
157 33d32d1f 2020-12-25 op /* gmid.c */
158 d3a08f4d 2021-01-17 op __attribute__((format (printf, 1, 2)))
159 d3a08f4d 2021-01-17 op __attribute__((__noreturn__))
160 d3a08f4d 2021-01-17 op void fatal(const char*, ...);
161 d3a08f4d 2021-01-17 op
162 d3a08f4d 2021-01-17 op __attribute__((format (printf, 3, 4)))
163 d3a08f4d 2021-01-17 op void logs(int, struct client*, const char*, ...);
164 0be51733 2021-01-20 op void log_request(struct client*, char*, size_t);
165 d3a08f4d 2021-01-17 op
166 4a28dd01 2020-12-28 op void sig_handler(int);
167 8443bff7 2021-01-25 op void gen_certificate(const char*, const char*, const char*);
168 8443bff7 2021-01-25 op void mkdirs(const char*);
169 8443bff7 2021-01-25 op char *data_dir(void);
170 8443bff7 2021-01-25 op void load_local_cert(const char*, const char*);
171 ae08ec7d 2021-01-25 op void load_vhosts(void);
172 33ac26a0 2021-01-21 op int make_socket(int, int);
173 ae08ec7d 2021-01-25 op void setup_tls(void);
174 33ac26a0 2021-01-21 op int listener_main(void);
175 c8b74339 2021-01-24 op void init_config(void);
176 ae08ec7d 2021-01-25 op void drop_priv(void);
177 488f059a 2020-12-24 op void usage(const char*);
178 488f059a 2020-12-24 op
179 f7b816dc 2021-01-15 op /* provided by lex/yacc */
180 15902770 2021-01-15 op extern FILE *yyin;
181 15902770 2021-01-15 op extern int yylineno;
182 15902770 2021-01-15 op extern int yyparse(void);
183 15902770 2021-01-15 op extern int yylex(void);
184 15902770 2021-01-15 op
185 13ed2fb6 2021-01-27 op void yyerror(const char*);
186 13ed2fb6 2021-01-27 op int parse_portno(const char*);
187 13ed2fb6 2021-01-27 op void parse_conf(const char*);
188 13ed2fb6 2021-01-27 op
189 0fbe79b3 2021-01-18 op /* mime.c */
190 b2a6b613 2021-01-21 op void init_mime(struct mime*);
191 b2a6b613 2021-01-21 op void add_mime(struct mime*, const char*, const char*);
192 b2a6b613 2021-01-21 op void load_default_mime(struct mime*);
193 6119e13e 2021-01-19 op const char *mime(struct vhost*, const char*);
194 0fbe79b3 2021-01-18 op
195 d3a08f4d 2021-01-17 op /* server.c */
196 c8b74339 2021-01-24 op const char *vhost_lang(struct vhost*, const char*);
197 c8b74339 2021-01-24 op const char *vhost_default_mime(struct vhost*, const char*);
198 c8b74339 2021-01-24 op const char *vhost_index(struct vhost*, const char*);
199 252908e6 2021-01-24 op int vhost_auto_index(struct vhost*, const char*);
200 d3a08f4d 2021-01-17 op int check_path(struct client*, const char*, int*);
201 07b0a142 2021-01-24 op void open_file(struct pollfd*, struct client*);
202 252908e6 2021-01-24 op void load_file(struct pollfd*, struct client*);
203 07b0a142 2021-01-24 op void check_for_cgi(char *, char*, struct pollfd*, struct client*);
204 d3a08f4d 2021-01-17 op void mark_nonblock(int);
205 d3a08f4d 2021-01-17 op void handle_handshake(struct pollfd*, struct client*);
206 d3a08f4d 2021-01-17 op void handle_open_conn(struct pollfd*, struct client*);
207 a87f6625 2021-01-24 op void start_reply(struct pollfd*, struct client*, int, const char*);
208 07b0a142 2021-01-24 op void start_cgi(const char*, const char*, const char*, struct pollfd*, struct client*);
209 0be51733 2021-01-20 op void send_file(struct pollfd*, struct client*);
210 252908e6 2021-01-24 op void open_dir(struct pollfd*, struct client*);
211 252908e6 2021-01-24 op void redirect_canonical_dir(struct pollfd*, struct client*);
212 252908e6 2021-01-24 op int read_next_dir_entry(struct client*);
213 252908e6 2021-01-24 op void send_directory_listing(struct pollfd*, struct client*);
214 d3a08f4d 2021-01-17 op void cgi_poll_on_child(struct pollfd*, struct client*);
215 d3a08f4d 2021-01-17 op void cgi_poll_on_client(struct pollfd*, struct client*);
216 d3a08f4d 2021-01-17 op void handle_cgi(struct pollfd*, struct client*);
217 36162ed8 2021-01-22 op void close_conn(struct pollfd*, struct client*);
218 d3a08f4d 2021-01-17 op void do_accept(int, struct tls*, struct pollfd*, struct client*);
219 d3a08f4d 2021-01-17 op void handle(struct pollfd*, struct client*);
220 d3a08f4d 2021-01-17 op void loop(struct tls*, int, int);
221 d3a08f4d 2021-01-17 op
222 881a9dd9 2021-01-16 op /* ex.c */
223 881a9dd9 2021-01-16 op int send_string(int, const char*);
224 881a9dd9 2021-01-16 op int recv_string(int, char**);
225 881a9dd9 2021-01-16 op int send_vhost(int, struct vhost*);
226 881a9dd9 2021-01-16 op int recv_vhost(int, struct vhost**);
227 881a9dd9 2021-01-16 op int send_fd(int, int);
228 881a9dd9 2021-01-16 op int recv_fd(int);
229 881a9dd9 2021-01-16 op int executor_main(int);
230 881a9dd9 2021-01-16 op
231 dafb57b8 2021-01-15 op /* sandbox.c */
232 33ac26a0 2021-01-21 op void sandbox(void);
233 dafb57b8 2021-01-15 op
234 ef04b551 2021-01-09 op /* utf8.c */
235 ef04b551 2021-01-09 op int valid_multibyte_utf8(struct parser*);
236 3300cbe0 2021-01-27 op char *utf8_nth(char*, size_t);
237 ef04b551 2021-01-09 op
238 3c1cf9d0 2021-01-11 op /* iri.c */
239 3c1cf9d0 2021-01-11 op int parse_iri(char*, struct iri*, const char**);
240 c4f682f8 2021-01-27 op int trim_req_iri(char*, const char **);
241 33d32d1f 2020-12-25 op
242 3300cbe0 2021-01-27 op /* puny.c */
243 7957cbd9 2021-01-27 op int puny_decode(const char*, char*, size_t);
244 3300cbe0 2021-01-27 op
245 44ee1bac 2021-01-27 op /* utils.c */
246 44ee1bac 2021-01-27 op int starts_with(const char*, const char*);
247 44ee1bac 2021-01-27 op int ends_with(const char*, const char*);
248 44ee1bac 2021-01-27 op ssize_t filesize(int);
249 44ee1bac 2021-01-27 op
250 488f059a 2020-12-24 op #endif