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 488f059a 2020-12-24 op #include <arpa/inet.h>
21 488f059a 2020-12-24 op #include <netinet/in.h>
22 28778244 2021-01-11 op #include <sys/socket.h>
23 488f059a 2020-12-24 op
24 488f059a 2020-12-24 op #include <poll.h>
25 488f059a 2020-12-24 op #include <stdio.h>
26 488f059a 2020-12-24 op #include <stdlib.h>
27 488f059a 2020-12-24 op #include <syslog.h>
28 488f059a 2020-12-24 op #include <tls.h>
29 488f059a 2020-12-24 op #include <unistd.h>
30 488f059a 2020-12-24 op
31 12042ad7 2021-01-21 op #include "config.h"
32 12042ad7 2021-01-21 op
33 488f059a 2020-12-24 op #ifndef INFTIM
34 488f059a 2020-12-24 op # define INFTIM -1
35 488f059a 2020-12-24 op #endif
36 488f059a 2020-12-24 op
37 488f059a 2020-12-24 op #define GEMINI_URL_LEN (1024+3) /* URL max len + \r\n + \0 */
38 488f059a 2020-12-24 op
39 488f059a 2020-12-24 op /* large enough to hold a copy of a gemini URL and still have extra room */
40 488f059a 2020-12-24 op #define PATHBUF 2048
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 dafb57b8 2021-01-15 op #define LOGE(c, fmt, ...) logs(LOG_ERR, c, fmt, __VA_ARGS__)
55 dafb57b8 2021-01-15 op #define LOGW(c, fmt, ...) logs(LOG_WARNING, c, fmt, __VA_ARGS__)
56 dafb57b8 2021-01-15 op #define LOGN(c, fmt, ...) logs(LOG_NOTICE, c, fmt, __VA_ARGS__)
57 dafb57b8 2021-01-15 op #define LOGI(c, fmt, ...) logs(LOG_INFO, c, fmt, __VA_ARGS__)
58 dafb57b8 2021-01-15 op #define LOGD(c, fmt, ...) logs(LOG_DEBUG, c, fmt, __VA_ARGS__)
59 dafb57b8 2021-01-15 op
60 c8b74339 2021-01-24 op struct location {
61 c8b74339 2021-01-24 op char *match;
62 c8b74339 2021-01-24 op char *lang;
63 c8b74339 2021-01-24 op char *default_mime;
64 c8b74339 2021-01-24 op char *index;
65 c8b74339 2021-01-24 op };
66 c8b74339 2021-01-24 op
67 15902770 2021-01-15 op struct vhost {
68 15902770 2021-01-15 op const char *domain;
69 15902770 2021-01-15 op const char *cert;
70 15902770 2021-01-15 op const char *key;
71 15902770 2021-01-15 op const char *dir;
72 15902770 2021-01-15 op const char *cgi;
73 15902770 2021-01-15 op int dirfd;
74 c8b74339 2021-01-24 op struct location locations[LOCLEN];
75 15902770 2021-01-15 op };
76 15902770 2021-01-15 op
77 15902770 2021-01-15 op extern struct vhost hosts[HOSTSLEN];
78 15902770 2021-01-15 op
79 a010b0dd 2021-01-18 op struct etm { /* extension to mime */
80 a010b0dd 2021-01-18 op const char *mime;
81 a010b0dd 2021-01-18 op const char *ext;
82 a010b0dd 2021-01-18 op };
83 a010b0dd 2021-01-18 op
84 b2a6b613 2021-01-21 op struct mime {
85 a010b0dd 2021-01-18 op struct etm *t;
86 a010b0dd 2021-01-18 op size_t len;
87 a010b0dd 2021-01-18 op size_t cap;
88 a010b0dd 2021-01-18 op };
89 a010b0dd 2021-01-18 op
90 15902770 2021-01-15 op struct conf {
91 a010b0dd 2021-01-18 op int foreground;
92 a010b0dd 2021-01-18 op int port;
93 a010b0dd 2021-01-18 op int ipv6;
94 a010b0dd 2021-01-18 op uint32_t protos;
95 b2a6b613 2021-01-21 op struct mime mime;
96 15902770 2021-01-15 op };
97 15902770 2021-01-15 op
98 15902770 2021-01-15 op extern struct conf conf;
99 881a9dd9 2021-01-16 op extern int exfd;
100 15902770 2021-01-15 op
101 0be51733 2021-01-20 op struct iri {
102 0be51733 2021-01-20 op char *schema;
103 0be51733 2021-01-20 op char *host;
104 0be51733 2021-01-20 op char *port;
105 0be51733 2021-01-20 op uint16_t port_no;
106 0be51733 2021-01-20 op char *path;
107 0be51733 2021-01-20 op char *query;
108 0be51733 2021-01-20 op char *fragment;
109 0be51733 2021-01-20 op };
110 0be51733 2021-01-20 op
111 0be51733 2021-01-20 op struct parser {
112 0be51733 2021-01-20 op char *iri;
113 0be51733 2021-01-20 op struct iri *parsed;
114 0be51733 2021-01-20 op const char *err;
115 0be51733 2021-01-20 op };
116 0be51733 2021-01-20 op
117 488f059a 2020-12-24 op enum {
118 9862b637 2021-01-13 op S_HANDSHAKE,
119 488f059a 2020-12-24 op S_OPEN,
120 488f059a 2020-12-24 op S_INITIALIZING,
121 a87f6625 2021-01-24 op S_SENDING_FILE,
122 a87f6625 2021-01-24 op S_SENDING_CGI,
123 488f059a 2020-12-24 op S_CLOSING,
124 488f059a 2020-12-24 op };
125 488f059a 2020-12-24 op
126 488f059a 2020-12-24 op struct client {
127 488f059a 2020-12-24 op struct tls *ctx;
128 0be51733 2021-01-20 op char req[GEMINI_URL_LEN];
129 0be51733 2021-01-20 op struct iri iri;
130 a87f6625 2021-01-24 op int state, next;
131 488f059a 2020-12-24 op int code;
132 488f059a 2020-12-24 op const char *meta;
133 488f059a 2020-12-24 op int fd, waiting_on_child;
134 488f059a 2020-12-24 op char sbuf[1024]; /* static buffer */
135 488f059a 2020-12-24 op void *buf, *i; /* mmap buffer */
136 488f059a 2020-12-24 op ssize_t len, off; /* mmap/static buffer */
137 709d6e5e 2021-01-10 op struct sockaddr_storage addr;
138 f7b816dc 2021-01-15 op struct vhost *host; /* host she's talking to */
139 488f059a 2020-12-24 op };
140 488f059a 2020-12-24 op
141 488f059a 2020-12-24 op enum {
142 488f059a 2020-12-24 op FILE_EXISTS,
143 488f059a 2020-12-24 op FILE_EXECUTABLE,
144 488f059a 2020-12-24 op FILE_DIRECTORY,
145 488f059a 2020-12-24 op FILE_MISSING,
146 488f059a 2020-12-24 op };
147 488f059a 2020-12-24 op
148 33d32d1f 2020-12-25 op /* gmid.c */
149 dafb57b8 2021-01-15 op
150 d3a08f4d 2021-01-17 op __attribute__((format (printf, 1, 2)))
151 d3a08f4d 2021-01-17 op __attribute__((__noreturn__))
152 d3a08f4d 2021-01-17 op void fatal(const char*, ...);
153 d3a08f4d 2021-01-17 op
154 d3a08f4d 2021-01-17 op __attribute__((format (printf, 3, 4)))
155 d3a08f4d 2021-01-17 op void logs(int, struct client*, const char*, ...);
156 0be51733 2021-01-20 op void log_request(struct client*, char*, size_t);
157 d3a08f4d 2021-01-17 op
158 4a28dd01 2020-12-28 op void sig_handler(int);
159 488f059a 2020-12-24 op int starts_with(const char*, const char*);
160 f77a8c86 2021-01-21 op int ends_with(const char*, const char*);
161 3d9a1c73 2020-12-28 op ssize_t filesize(int);
162 15902770 2021-01-15 op char *absolutify_path(const char*);
163 15902770 2021-01-15 op void yyerror(const char*);
164 15902770 2021-01-15 op int parse_portno(const char*);
165 15902770 2021-01-15 op void parse_conf(const char*);
166 15902770 2021-01-15 op void load_vhosts(struct tls_config*);
167 33ac26a0 2021-01-21 op int make_socket(int, int);
168 33ac26a0 2021-01-21 op int listener_main(void);
169 c8b74339 2021-01-24 op void init_config(void);
170 488f059a 2020-12-24 op void usage(const char*);
171 488f059a 2020-12-24 op
172 f7b816dc 2021-01-15 op /* provided by lex/yacc */
173 15902770 2021-01-15 op extern FILE *yyin;
174 15902770 2021-01-15 op extern int yylineno;
175 15902770 2021-01-15 op extern int yyparse(void);
176 15902770 2021-01-15 op extern int yylex(void);
177 15902770 2021-01-15 op
178 0fbe79b3 2021-01-18 op /* mime.c */
179 b2a6b613 2021-01-21 op void init_mime(struct mime*);
180 b2a6b613 2021-01-21 op void add_mime(struct mime*, const char*, const char*);
181 b2a6b613 2021-01-21 op void load_default_mime(struct mime*);
182 6119e13e 2021-01-19 op const char *mime(struct vhost*, const char*);
183 0fbe79b3 2021-01-18 op
184 d3a08f4d 2021-01-17 op /* server.c */
185 c8b74339 2021-01-24 op const char *vhost_lang(struct vhost*, const char*);
186 c8b74339 2021-01-24 op const char *vhost_default_mime(struct vhost*, const char*);
187 c8b74339 2021-01-24 op const char *vhost_index(struct vhost*, const char*);
188 d3a08f4d 2021-01-17 op int check_path(struct client*, const char*, int*);
189 07b0a142 2021-01-24 op void open_file(struct pollfd*, struct client*);
190 07b0a142 2021-01-24 op void check_for_cgi(char *, char*, struct pollfd*, struct client*);
191 d3a08f4d 2021-01-17 op void mark_nonblock(int);
192 d3a08f4d 2021-01-17 op void handle_handshake(struct pollfd*, struct client*);
193 d3a08f4d 2021-01-17 op void handle_open_conn(struct pollfd*, struct client*);
194 a87f6625 2021-01-24 op void start_reply(struct pollfd*, struct client*, int, const char*);
195 07b0a142 2021-01-24 op void start_cgi(const char*, const char*, const char*, struct pollfd*, struct client*);
196 0be51733 2021-01-20 op void send_file(struct pollfd*, struct client*);
197 0be51733 2021-01-20 op void send_dir(struct pollfd*, struct client*);
198 d3a08f4d 2021-01-17 op void cgi_poll_on_child(struct pollfd*, struct client*);
199 d3a08f4d 2021-01-17 op void cgi_poll_on_client(struct pollfd*, struct client*);
200 d3a08f4d 2021-01-17 op void handle_cgi(struct pollfd*, struct client*);
201 36162ed8 2021-01-22 op void close_conn(struct pollfd*, struct client*);
202 d3a08f4d 2021-01-17 op void do_accept(int, struct tls*, struct pollfd*, struct client*);
203 d3a08f4d 2021-01-17 op void handle(struct pollfd*, struct client*);
204 d3a08f4d 2021-01-17 op void loop(struct tls*, int, int);
205 d3a08f4d 2021-01-17 op
206 881a9dd9 2021-01-16 op /* ex.c */
207 881a9dd9 2021-01-16 op int send_string(int, const char*);
208 881a9dd9 2021-01-16 op int recv_string(int, char**);
209 881a9dd9 2021-01-16 op int send_vhost(int, struct vhost*);
210 881a9dd9 2021-01-16 op int recv_vhost(int, struct vhost**);
211 881a9dd9 2021-01-16 op int send_fd(int, int);
212 881a9dd9 2021-01-16 op int recv_fd(int);
213 881a9dd9 2021-01-16 op int executor_main(int);
214 881a9dd9 2021-01-16 op
215 dafb57b8 2021-01-15 op /* sandbox.c */
216 33ac26a0 2021-01-21 op void sandbox(void);
217 dafb57b8 2021-01-15 op
218 ef04b551 2021-01-09 op /* utf8.c */
219 ef04b551 2021-01-09 op int valid_multibyte_utf8(struct parser*);
220 ef04b551 2021-01-09 op
221 3c1cf9d0 2021-01-11 op /* iri.c */
222 3c1cf9d0 2021-01-11 op int parse_iri(char*, struct iri*, const char**);
223 3c1cf9d0 2021-01-11 op int trim_req_iri(char*);
224 33d32d1f 2020-12-25 op
225 488f059a 2020-12-24 op #endif