Blame


1 3e4749f7 2020-10-02 op /*
2 3e4749f7 2020-10-02 op * Copyright (c) 2020 Omar Polo <op@omarpolo.com>
3 3e4749f7 2020-10-02 op *
4 3e4749f7 2020-10-02 op * Permission to use, copy, modify, and distribute this software for any
5 3e4749f7 2020-10-02 op * purpose with or without fee is hereby granted, provided that the above
6 3e4749f7 2020-10-02 op * copyright notice and this permission notice appear in all copies.
7 3e4749f7 2020-10-02 op *
8 3e4749f7 2020-10-02 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 3e4749f7 2020-10-02 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 3e4749f7 2020-10-02 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 3e4749f7 2020-10-02 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 3e4749f7 2020-10-02 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 3e4749f7 2020-10-02 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 3e4749f7 2020-10-02 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 3e4749f7 2020-10-02 op */
16 3e4749f7 2020-10-02 op
17 f28f9311 2020-10-14 op #include <sys/mman.h>
18 3e4749f7 2020-10-02 op #include <sys/socket.h>
19 3e4749f7 2020-10-02 op #include <sys/stat.h>
20 3e4749f7 2020-10-02 op
21 2c3a40fa 2020-11-06 op #include <arpa/inet.h>
22 3e4749f7 2020-10-02 op #include <netinet/in.h>
23 3e4749f7 2020-10-02 op
24 3e4749f7 2020-10-02 op #include <assert.h>
25 3e4749f7 2020-10-02 op #include <err.h>
26 592fd624 2020-10-07 op #include <errno.h>
27 3e4749f7 2020-10-02 op #include <fcntl.h>
28 592fd624 2020-10-07 op #include <poll.h>
29 0cf902af 2020-11-03 op #include <signal.h>
30 3e4749f7 2020-10-02 op #include <stdio.h>
31 3e4749f7 2020-10-02 op #include <stdlib.h>
32 3e4749f7 2020-10-02 op #include <string.h>
33 3e4749f7 2020-10-02 op #include <tls.h>
34 3e4749f7 2020-10-02 op #include <unistd.h>
35 3e4749f7 2020-10-02 op
36 3e4749f7 2020-10-02 op #ifndef __OpenBSD__
37 3e4749f7 2020-10-02 op # define pledge(a, b) 0
38 3e4749f7 2020-10-02 op # define unveil(a, b) 0
39 3e4749f7 2020-10-02 op #endif /* __OpenBSD__ */
40 3e4749f7 2020-10-02 op
41 592fd624 2020-10-07 op #ifndef INFTIM
42 592fd624 2020-10-07 op # define INFTIM -1
43 592fd624 2020-10-07 op #endif /* INFTIM */
44 592fd624 2020-10-07 op
45 3e4749f7 2020-10-02 op #define GEMINI_URL_LEN (1024+3) /* URL max len + \r\n + \0 */
46 3e4749f7 2020-10-02 op
47 4d4f0e19 2020-10-03 op /* large enough to hold a copy of a gemini URL and still have extra room */
48 d95b77a0 2020-10-07 op #define PATHBUF 2048
49 3e4749f7 2020-10-02 op
50 3e4749f7 2020-10-02 op #define SUCCESS 20
51 72342dc9 2020-11-06 op #define TEMP_FAILURE 40
52 3e4749f7 2020-10-02 op #define NOT_FOUND 51
53 3e4749f7 2020-10-02 op #define BAD_REQUEST 59
54 3e4749f7 2020-10-02 op
55 592fd624 2020-10-07 op #ifndef MAX_USERS
56 592fd624 2020-10-07 op #define MAX_USERS 64
57 592fd624 2020-10-07 op #endif
58 592fd624 2020-10-07 op
59 677afbd3 2020-12-02 op #define SAFE_SETENV(var, val) do { \
60 677afbd3 2020-12-02 op const char *_tmp = (val); \
61 677afbd3 2020-12-02 op if (_tmp == NULL) \
62 677afbd3 2020-12-02 op _tmp = ""; \
63 677afbd3 2020-12-02 op setenv((var), _tmp, 1); \
64 677afbd3 2020-12-02 op } while(0)
65 677afbd3 2020-12-02 op
66 592fd624 2020-10-07 op enum {
67 592fd624 2020-10-07 op S_OPEN,
68 592fd624 2020-10-07 op S_INITIALIZING,
69 592fd624 2020-10-07 op S_SENDING,
70 592fd624 2020-10-07 op S_CLOSING,
71 592fd624 2020-10-07 op };
72 592fd624 2020-10-07 op
73 592fd624 2020-10-07 op struct client {
74 592fd624 2020-10-07 op struct tls *ctx;
75 f28f9311 2020-10-14 op int state;
76 f28f9311 2020-10-14 op int code;
77 592fd624 2020-10-07 op const char *meta;
78 aff8d190 2020-11-06 op int fd, waiting_on_child;
79 72342dc9 2020-11-06 op pid_t child;
80 aff8d190 2020-11-06 op char sbuf[1024]; /* static buffer */
81 aff8d190 2020-11-06 op void *buf, *i; /* mmap buffer */
82 aff8d190 2020-11-06 op ssize_t len, off; /* mmap/static buffer */
83 2c3a40fa 2020-11-06 op int af;
84 2c3a40fa 2020-11-06 op struct in_addr addr;
85 592fd624 2020-10-07 op };
86 592fd624 2020-10-07 op
87 a5d310bc 2020-11-10 op enum {
88 a5d310bc 2020-11-10 op FILE_EXISTS,
89 a5d310bc 2020-11-10 op FILE_EXECUTABLE,
90 a5d310bc 2020-11-10 op FILE_DIRECTORY,
91 a5d310bc 2020-11-10 op FILE_MISSING,
92 a5d310bc 2020-11-10 op };
93 a5d310bc 2020-11-10 op
94 cc68fe70 2020-10-07 op struct etm { /* file extension to mime */
95 cc68fe70 2020-10-07 op const char *mime;
96 cc68fe70 2020-10-07 op const char *ext;
97 cc68fe70 2020-10-07 op } filetypes[] = {
98 cc68fe70 2020-10-07 op {"application/pdf", "pdf"},
99 cc68fe70 2020-10-07 op
100 cc68fe70 2020-10-07 op {"image/gif", "gif"},
101 cc68fe70 2020-10-07 op {"image/jpeg", "jpg"},
102 cc68fe70 2020-10-07 op {"image/jpeg", "jpeg"},
103 cc68fe70 2020-10-07 op {"image/png", "png"},
104 cc68fe70 2020-10-07 op {"image/svg+xml", "svg"},
105 cc68fe70 2020-10-07 op
106 cc68fe70 2020-10-07 op {"text/gemini", "gemini"},
107 cc68fe70 2020-10-07 op {"text/gemini", "gmi"},
108 cc68fe70 2020-10-07 op {"text/markdown", "markdown"},
109 cc68fe70 2020-10-07 op {"text/markdown", "md"},
110 cc68fe70 2020-10-07 op {"text/plain", "txt"},
111 932b001a 2020-11-06 op {"text/xml", "xml"},
112 cc68fe70 2020-10-07 op
113 cc68fe70 2020-10-07 op {NULL, NULL}
114 cc68fe70 2020-10-07 op };
115 cc68fe70 2020-10-07 op
116 2c3a40fa 2020-11-06 op #define LOG(c, fmt, ...) \
117 2c3a40fa 2020-11-06 op do { \
118 2c3a40fa 2020-11-06 op char buf[INET_ADDRSTRLEN]; \
119 2c3a40fa 2020-11-06 op if (inet_ntop((c)->af, &(c)->addr, buf, sizeof(buf)) == NULL) \
120 2c3a40fa 2020-11-06 op err(1, "inet_ntop"); \
121 2c3a40fa 2020-11-06 op dprintf(logfd, "[%s] " fmt "\n", buf, __VA_ARGS__); \
122 2c3a40fa 2020-11-06 op } while (0)
123 3e4749f7 2020-10-02 op
124 72f653b6 2020-11-06 op const char *dir, *cgi;
125 2c3a40fa 2020-11-06 op int dirfd, logfd;
126 721e2325 2020-11-18 op int port;
127 120381c9 2020-11-06 op int connected_clients;
128 2c3a40fa 2020-11-06 op
129 120381c9 2020-11-06 op void siginfo_handler(int);
130 72f653b6 2020-11-06 op int starts_with(const char*, const char*);
131 120381c9 2020-11-06 op
132 592fd624 2020-10-07 op char *url_after_proto(char*);
133 592fd624 2020-10-07 op char *url_start_of_request(char*);
134 2c3a40fa 2020-11-06 op int url_trim(struct client*, char*);
135 aa0fe0cf 2020-11-06 op char *adjust_path(char*);
136 f28f9311 2020-10-14 op ssize_t filesize(int);
137 592fd624 2020-10-07 op
138 592fd624 2020-10-07 op int start_reply(struct pollfd*, struct client*, int, const char*);
139 cc68fe70 2020-10-07 op const char *path_ext(const char*);
140 cc68fe70 2020-10-07 op const char *mime(const char*);
141 a5d310bc 2020-11-10 op int check_path(const char*, int*);
142 a5d310bc 2020-11-10 op int check_for_cgi(char *, char*, struct pollfd*, struct client*);
143 75d233f0 2020-11-06 op int open_file(char*, char*, struct pollfd*, struct client*);
144 a5d310bc 2020-11-10 op int start_cgi(const char*, const char*, const char*, struct pollfd*, struct client*);
145 aff8d190 2020-11-06 op void cgi_setpoll_on_child(struct pollfd*, struct client*);
146 aff8d190 2020-11-06 op void cgi_setpoll_on_client(struct pollfd*, struct client*);
147 72342dc9 2020-11-06 op void handle_cgi(struct pollfd*, struct client*);
148 75d233f0 2020-11-06 op void send_file(char*, char*, struct pollfd*, struct client*);
149 592fd624 2020-10-07 op void send_dir(char*, struct pollfd*, struct client*);
150 592fd624 2020-10-07 op void handle(struct pollfd*, struct client*);
151 592fd624 2020-10-07 op
152 592fd624 2020-10-07 op void mark_nonblock(int);
153 592fd624 2020-10-07 op int make_soket(int);
154 592fd624 2020-10-07 op void do_accept(int, struct tls*, struct pollfd*, struct client*);
155 592fd624 2020-10-07 op void goodbye(struct pollfd*, struct client*);
156 592fd624 2020-10-07 op void loop(struct tls*, int);
157 592fd624 2020-10-07 op
158 592fd624 2020-10-07 op void usage(const char*);
159 592fd624 2020-10-07 op
160 120381c9 2020-11-06 op void
161 120381c9 2020-11-06 op siginfo_handler(int sig)
162 120381c9 2020-11-06 op {
163 120381c9 2020-11-06 op (void)sig;
164 120381c9 2020-11-06 op }
165 120381c9 2020-11-06 op
166 72f653b6 2020-11-06 op int
167 72f653b6 2020-11-06 op starts_with(const char *str, const char *prefix)
168 72f653b6 2020-11-06 op {
169 72f653b6 2020-11-06 op size_t i;
170 72f653b6 2020-11-06 op
171 72f653b6 2020-11-06 op for (i = 0; prefix[i] != '\0'; ++i)
172 72f653b6 2020-11-06 op if (str[i] != prefix[i])
173 72f653b6 2020-11-06 op return 0;
174 72f653b6 2020-11-06 op return 1;
175 72f653b6 2020-11-06 op }
176 72f653b6 2020-11-06 op
177 3e4749f7 2020-10-02 op char *
178 3e4749f7 2020-10-02 op url_after_proto(char *url)
179 3e4749f7 2020-10-02 op {
180 3e4749f7 2020-10-02 op char *s;
181 c603f126 2020-11-18 op const char *proto = "gemini:";
182 ae2ad03e 2020-11-18 op const char *marker = "//";
183 3e4749f7 2020-10-02 op
184 3c19febb 2020-11-06 op /* a relative URL */
185 3e4749f7 2020-10-02 op if ((s = strstr(url, marker)) == NULL)
186 3e4749f7 2020-10-02 op return url;
187 ae2ad03e 2020-11-18 op
188 ae2ad03e 2020-11-18 op /*
189 ae2ad03e 2020-11-18 op * if a protocol is not specified, gemini should be implied:
190 ae2ad03e 2020-11-18 op * this handles the case of //example.com
191 ae2ad03e 2020-11-18 op */
192 ae2ad03e 2020-11-18 op if (s == url)
193 ae2ad03e 2020-11-18 op return s + strlen(marker);
194 3e4749f7 2020-10-02 op
195 3c19febb 2020-11-06 op if (s - strlen(proto) != url)
196 3e4749f7 2020-10-02 op return NULL;
197 3c19febb 2020-11-06 op
198 72f653b6 2020-11-06 op if (!starts_with(url, proto))
199 72f653b6 2020-11-06 op return NULL;
200 3e4749f7 2020-10-02 op
201 3e4749f7 2020-10-02 op return s + strlen(marker);
202 3e4749f7 2020-10-02 op }
203 3e4749f7 2020-10-02 op
204 3e4749f7 2020-10-02 op char *
205 3e4749f7 2020-10-02 op url_start_of_request(char *url)
206 3e4749f7 2020-10-02 op {
207 3e4749f7 2020-10-02 op char *s, *t;
208 3e4749f7 2020-10-02 op
209 3e4749f7 2020-10-02 op if ((s = url_after_proto(url)) == NULL)
210 3e4749f7 2020-10-02 op return NULL;
211 7cd6880b 2020-11-18 op
212 7cd6880b 2020-11-18 op /* non-absolute URL */
213 7cd6880b 2020-11-18 op if (s == url)
214 7cd6880b 2020-11-18 op return s;
215 3e4749f7 2020-10-02 op
216 3e4749f7 2020-10-02 op if ((t = strstr(s, "/")) == NULL)
217 3e4749f7 2020-10-02 op return s + strlen(s);
218 3e4749f7 2020-10-02 op return t;
219 3e4749f7 2020-10-02 op }
220 3e4749f7 2020-10-02 op
221 3e4749f7 2020-10-02 op int
222 2c3a40fa 2020-11-06 op url_trim(struct client *c, char *url)
223 3e4749f7 2020-10-02 op {
224 3e4749f7 2020-10-02 op const char *e = "\r\n";
225 3e4749f7 2020-10-02 op char *s;
226 3e4749f7 2020-10-02 op
227 3e4749f7 2020-10-02 op if ((s = strstr(url, e)) == NULL)
228 3e4749f7 2020-10-02 op return 0;
229 3e4749f7 2020-10-02 op s[0] = '\0';
230 3e4749f7 2020-10-02 op s[1] = '\0';
231 3e4749f7 2020-10-02 op
232 3e4749f7 2020-10-02 op if (s[2] != '\0') {
233 2c3a40fa 2020-11-06 op LOG(c, "%s", "request longer than 1024 bytes\n");
234 3e4749f7 2020-10-02 op return 0;
235 3e4749f7 2020-10-02 op }
236 3e4749f7 2020-10-02 op
237 3e4749f7 2020-10-02 op return 1;
238 3e4749f7 2020-10-02 op }
239 3e4749f7 2020-10-02 op
240 aa0fe0cf 2020-11-06 op char *
241 3e4749f7 2020-10-02 op adjust_path(char *path)
242 3e4749f7 2020-10-02 op {
243 aa0fe0cf 2020-11-06 op char *s, *query;
244 3e4749f7 2020-10-02 op size_t len;
245 aa0fe0cf 2020-11-06 op
246 aa0fe0cf 2020-11-06 op if ((query = strchr(path, '?')) != NULL) {
247 aa0fe0cf 2020-11-06 op *query = '\0';
248 aa0fe0cf 2020-11-06 op query++;
249 aa0fe0cf 2020-11-06 op }
250 3e4749f7 2020-10-02 op
251 f28f9311 2020-10-14 op /* /.. -> / */
252 3e4749f7 2020-10-02 op len = strlen(path);
253 3e4749f7 2020-10-02 op if (len >= 3) {
254 3e4749f7 2020-10-02 op if (!strcmp(&path[len-3], "/..")) {
255 3e4749f7 2020-10-02 op path[len-2] = '\0';
256 3e4749f7 2020-10-02 op }
257 3e4749f7 2020-10-02 op }
258 3e4749f7 2020-10-02 op
259 3e4749f7 2020-10-02 op /* if the path is only `..` trim out and exit */
260 3e4749f7 2020-10-02 op if (!strcmp(path, "..")) {
261 3e4749f7 2020-10-02 op path[0] = '\0';
262 aa0fe0cf 2020-11-06 op return query;
263 3e4749f7 2020-10-02 op }
264 3e4749f7 2020-10-02 op
265 3e4749f7 2020-10-02 op /* remove every ../ in the path */
266 3e4749f7 2020-10-02 op while (1) {
267 3e4749f7 2020-10-02 op if ((s = strstr(path, "../")) == NULL)
268 aa0fe0cf 2020-11-06 op return query;
269 67328d23 2020-10-03 op memmove(s, s+3, strlen(s)+1); /* copy also the \0 */
270 3e4749f7 2020-10-02 op }
271 3e4749f7 2020-10-02 op }
272 3e4749f7 2020-10-02 op
273 3e4749f7 2020-10-02 op int
274 592fd624 2020-10-07 op start_reply(struct pollfd *pfd, struct client *client, int code, const char *reason)
275 3e4749f7 2020-10-02 op {
276 3e4749f7 2020-10-02 op char buf[1030] = {0}; /* status + ' ' + max reply len + \r\n\0 */
277 3e4749f7 2020-10-02 op int len;
278 592fd624 2020-10-07 op int ret;
279 3e4749f7 2020-10-02 op
280 592fd624 2020-10-07 op client->code = code;
281 592fd624 2020-10-07 op client->meta = reason;
282 592fd624 2020-10-07 op client->state = S_INITIALIZING;
283 592fd624 2020-10-07 op
284 3e4749f7 2020-10-02 op len = snprintf(buf, sizeof(buf), "%d %s\r\n", code, reason);
285 3e4749f7 2020-10-02 op assert(len < (int)sizeof(buf));
286 592fd624 2020-10-07 op ret = tls_write(client->ctx, buf, len);
287 592fd624 2020-10-07 op if (ret == TLS_WANT_POLLIN) {
288 592fd624 2020-10-07 op pfd->events = POLLIN;
289 592fd624 2020-10-07 op return 0;
290 592fd624 2020-10-07 op }
291 592fd624 2020-10-07 op
292 592fd624 2020-10-07 op if (ret == TLS_WANT_POLLOUT) {
293 592fd624 2020-10-07 op pfd->events = POLLOUT;
294 592fd624 2020-10-07 op return 0;
295 592fd624 2020-10-07 op }
296 592fd624 2020-10-07 op
297 592fd624 2020-10-07 op return 1;
298 3e4749f7 2020-10-02 op }
299 3e4749f7 2020-10-02 op
300 f28f9311 2020-10-14 op ssize_t
301 f28f9311 2020-10-14 op filesize(int fd)
302 f28f9311 2020-10-14 op {
303 f28f9311 2020-10-14 op ssize_t len;
304 f28f9311 2020-10-14 op
305 f28f9311 2020-10-14 op if ((len = lseek(fd, 0, SEEK_END)) == -1)
306 f28f9311 2020-10-14 op return -1;
307 f28f9311 2020-10-14 op if (lseek(fd, 0, SEEK_SET) == -1)
308 f28f9311 2020-10-14 op return -1;
309 f28f9311 2020-10-14 op return len;
310 f28f9311 2020-10-14 op }
311 f28f9311 2020-10-14 op
312 cc68fe70 2020-10-07 op const char *
313 cc68fe70 2020-10-07 op path_ext(const char *path)
314 cc68fe70 2020-10-07 op {
315 cc68fe70 2020-10-07 op const char *end;
316 cc68fe70 2020-10-07 op
317 cc68fe70 2020-10-07 op end = path + strlen(path)-1; /* the last byte before the NUL */
318 cc68fe70 2020-10-07 op for (; end != path; --end) {
319 cc68fe70 2020-10-07 op if (*end == '.')
320 cc68fe70 2020-10-07 op return end+1;
321 cc68fe70 2020-10-07 op if (*end == '/')
322 cc68fe70 2020-10-07 op break;
323 cc68fe70 2020-10-07 op }
324 cc68fe70 2020-10-07 op
325 cc68fe70 2020-10-07 op return NULL;
326 cc68fe70 2020-10-07 op }
327 cc68fe70 2020-10-07 op
328 cc68fe70 2020-10-07 op const char *
329 cc68fe70 2020-10-07 op mime(const char *path)
330 cc68fe70 2020-10-07 op {
331 cc68fe70 2020-10-07 op const char *ext, *def = "application/octet-stream";
332 cc68fe70 2020-10-07 op struct etm *t;
333 cc68fe70 2020-10-07 op
334 cc68fe70 2020-10-07 op if ((ext = path_ext(path)) == NULL)
335 cc68fe70 2020-10-07 op return def;
336 cc68fe70 2020-10-07 op
337 cc68fe70 2020-10-07 op for (t = filetypes; t->mime != NULL; ++t)
338 cc68fe70 2020-10-07 op if (!strcmp(ext, t->ext))
339 cc68fe70 2020-10-07 op return t->mime;
340 cc68fe70 2020-10-07 op
341 cc68fe70 2020-10-07 op return def;
342 3e4749f7 2020-10-02 op }
343 3e4749f7 2020-10-02 op
344 f28f9311 2020-10-14 op int
345 a5d310bc 2020-11-10 op check_path(const char *path, int *fd)
346 3e4749f7 2020-10-02 op {
347 72342dc9 2020-11-06 op struct stat sb;
348 3e4749f7 2020-10-02 op
349 f28f9311 2020-10-14 op assert(path != NULL);
350 a5d310bc 2020-11-10 op if ((*fd = openat(dirfd, path,
351 a5d310bc 2020-11-10 op O_RDONLY | O_NOFOLLOW | O_CLOEXEC)) == -1) {
352 a5d310bc 2020-11-10 op return FILE_MISSING;
353 a5d310bc 2020-11-10 op }
354 a5d310bc 2020-11-10 op
355 a5d310bc 2020-11-10 op if (fstat(*fd, &sb) == -1) {
356 a5d310bc 2020-11-10 op dprintf(logfd, "failed stat for %s\n", path);
357 a5d310bc 2020-11-10 op return FILE_MISSING;
358 a5d310bc 2020-11-10 op }
359 a5d310bc 2020-11-10 op
360 a5d310bc 2020-11-10 op if (S_ISDIR(sb.st_mode))
361 a5d310bc 2020-11-10 op return FILE_DIRECTORY;
362 a5d310bc 2020-11-10 op
363 a5d310bc 2020-11-10 op if (sb.st_mode & S_IXUSR)
364 a5d310bc 2020-11-10 op return FILE_EXECUTABLE;
365 a5d310bc 2020-11-10 op
366 a5d310bc 2020-11-10 op return FILE_EXISTS;
367 a5d310bc 2020-11-10 op }
368 a5d310bc 2020-11-10 op
369 a5d310bc 2020-11-10 op /*
370 a5d310bc 2020-11-10 op * the inverse of this algorithm, i.e. starting from the start of the
371 a5d310bc 2020-11-10 op * path + strlen(cgi), and checking if each component, should be
372 a5d310bc 2020-11-10 op * faster. But it's tedious to write. This does the opposite: starts
373 a5d310bc 2020-11-10 op * from the end and strip one component at a time, until either an
374 a5d310bc 2020-11-10 op * executable is found or we emptied the path.
375 a5d310bc 2020-11-10 op */
376 a5d310bc 2020-11-10 op int
377 a5d310bc 2020-11-10 op check_for_cgi(char *path, char *query, struct pollfd *fds, struct client *c)
378 a5d310bc 2020-11-10 op {
379 a5d310bc 2020-11-10 op char *end;
380 a5d310bc 2020-11-10 op end = strchr(path, '\0');
381 a5d310bc 2020-11-10 op
382 a5d310bc 2020-11-10 op /* NB: assume CGI is enabled and path matches cgi */
383 a5d310bc 2020-11-10 op
384 a5d310bc 2020-11-10 op while (end > path) {
385 a5d310bc 2020-11-10 op /* go up one level. UNIX paths are simple and POSIX
386 a5d310bc 2020-11-10 op * dirname, with its ambiguities on if the given path
387 a5d310bc 2020-11-10 op * is changed or not, gives me headaches. */
388 a5d310bc 2020-11-10 op while (*end != '/')
389 a5d310bc 2020-11-10 op end--;
390 a5d310bc 2020-11-10 op *end = '\0';
391 3e4749f7 2020-10-02 op
392 a5d310bc 2020-11-10 op switch (check_path(path, &c->fd)) {
393 a5d310bc 2020-11-10 op case FILE_EXECUTABLE:
394 a5d310bc 2020-11-10 op return start_cgi(path, end+1, query, fds,c);
395 a5d310bc 2020-11-10 op case FILE_MISSING:
396 a5d310bc 2020-11-10 op break;
397 a5d310bc 2020-11-10 op default:
398 a5d310bc 2020-11-10 op goto err;
399 a5d310bc 2020-11-10 op }
400 a5d310bc 2020-11-10 op
401 a5d310bc 2020-11-10 op *end = '/';
402 a5d310bc 2020-11-10 op end--;
403 a5d310bc 2020-11-10 op }
404 a5d310bc 2020-11-10 op
405 a5d310bc 2020-11-10 op err:
406 a5d310bc 2020-11-10 op if (!start_reply(fds, c, NOT_FOUND, "not found"))
407 a5d310bc 2020-11-10 op return 0;
408 a5d310bc 2020-11-10 op goodbye(fds, c);
409 a5d310bc 2020-11-10 op return 0;
410 a5d310bc 2020-11-10 op }
411 a5d310bc 2020-11-10 op
412 a5d310bc 2020-11-10 op
413 a5d310bc 2020-11-10 op int
414 a5d310bc 2020-11-10 op open_file(char *path, char *query, struct pollfd *fds, struct client *c)
415 a5d310bc 2020-11-10 op {
416 a5d310bc 2020-11-10 op char fpath[PATHBUF];
417 a5d310bc 2020-11-10 op
418 f28f9311 2020-10-14 op bzero(fpath, sizeof(fpath));
419 3e4749f7 2020-10-02 op
420 f28f9311 2020-10-14 op if (*path != '.')
421 f28f9311 2020-10-14 op fpath[0] = '.';
422 f28f9311 2020-10-14 op strlcat(fpath, path, PATHBUF);
423 3e4749f7 2020-10-02 op
424 a5d310bc 2020-11-10 op switch (check_path(fpath, &c->fd)) {
425 a5d310bc 2020-11-10 op case FILE_EXECUTABLE:
426 a5d310bc 2020-11-10 op /* +2 to skip the ./ */
427 a5d310bc 2020-11-10 op if (cgi != NULL && starts_with(fpath+2, cgi))
428 a5d310bc 2020-11-10 op return start_cgi(fpath, "", query, fds, c);
429 3e4749f7 2020-10-02 op
430 a5d310bc 2020-11-10 op /* fallthrough */
431 a5d310bc 2020-11-10 op
432 a5d310bc 2020-11-10 op case FILE_EXISTS:
433 a5d310bc 2020-11-10 op if ((c->len = filesize(c->fd)) == -1) {
434 a5d310bc 2020-11-10 op LOG(c, "failed to get file size for %s", fpath);
435 a5d310bc 2020-11-10 op goodbye(fds, c);
436 72342dc9 2020-11-06 op return 0;
437 a5d310bc 2020-11-10 op }
438 72342dc9 2020-11-06 op
439 a5d310bc 2020-11-10 op if ((c->buf = mmap(NULL, c->len, PROT_READ, MAP_PRIVATE,
440 a5d310bc 2020-11-10 op c->fd, 0)) == MAP_FAILED) {
441 a5d310bc 2020-11-10 op warn("mmap: %s", fpath);
442 a5d310bc 2020-11-10 op goodbye(fds, c);
443 a5d310bc 2020-11-10 op return 0;
444 a5d310bc 2020-11-10 op }
445 a5d310bc 2020-11-10 op c->i = c->buf;
446 a5d310bc 2020-11-10 op return start_reply(fds, c, SUCCESS, mime(fpath));
447 a5d310bc 2020-11-10 op
448 a5d310bc 2020-11-10 op case FILE_DIRECTORY:
449 2c3a40fa 2020-11-06 op LOG(c, "%s is a directory, trying %s/index.gmi", fpath, fpath);
450 f28f9311 2020-10-14 op close(c->fd);
451 f28f9311 2020-10-14 op c->fd = -1;
452 f28f9311 2020-10-14 op send_dir(fpath, fds, c);
453 f28f9311 2020-10-14 op return 0;
454 3e4749f7 2020-10-02 op
455 a5d310bc 2020-11-10 op case FILE_MISSING:
456 a5d310bc 2020-11-10 op if (cgi != NULL && starts_with(fpath+2, cgi))
457 a5d310bc 2020-11-10 op return check_for_cgi(fpath, query, fds, c);
458 72342dc9 2020-11-06 op
459 a5d310bc 2020-11-10 op if (!start_reply(fds, c, NOT_FOUND, "not found"))
460 a5d310bc 2020-11-10 op return 0;
461 f28f9311 2020-10-14 op goodbye(fds, c);
462 f28f9311 2020-10-14 op return 0;
463 f28f9311 2020-10-14 op
464 a5d310bc 2020-11-10 op default:
465 a5d310bc 2020-11-10 op /* unreachable */
466 a5d310bc 2020-11-10 op abort();
467 592fd624 2020-10-07 op }
468 72342dc9 2020-11-06 op }
469 72342dc9 2020-11-06 op
470 a5d310bc 2020-11-10 op int
471 a5d310bc 2020-11-10 op start_cgi(const char *spath, const char *relpath, const char *query,
472 75d233f0 2020-11-06 op struct pollfd *fds, struct client *c)
473 72342dc9 2020-11-06 op {
474 72342dc9 2020-11-06 op pid_t pid;
475 a5d310bc 2020-11-10 op int p[2]; /* read end, write end */
476 72342dc9 2020-11-06 op
477 72342dc9 2020-11-06 op if (pipe(p) == -1)
478 72342dc9 2020-11-06 op goto err;
479 72342dc9 2020-11-06 op
480 72342dc9 2020-11-06 op switch (pid = fork()) {
481 72342dc9 2020-11-06 op case -1:
482 72342dc9 2020-11-06 op goto err;
483 72342dc9 2020-11-06 op
484 a5d310bc 2020-11-10 op case 0: { /* child */
485 721e2325 2020-11-18 op char *ex, *requri, *portno;
486 72342dc9 2020-11-06 op char addr[INET_ADDRSTRLEN];
487 72342dc9 2020-11-06 op char *argv[] = { NULL, NULL, NULL };
488 72342dc9 2020-11-06 op
489 a5d310bc 2020-11-10 op spath++;
490 72342dc9 2020-11-06 op
491 a5d310bc 2020-11-10 op close(p[0]);
492 72342dc9 2020-11-06 op if (dup2(p[1], 1) == -1)
493 72342dc9 2020-11-06 op goto childerr;
494 72342dc9 2020-11-06 op
495 72342dc9 2020-11-06 op if (inet_ntop(c->af, &c->addr, addr, sizeof(addr)) == NULL)
496 a5d310bc 2020-11-10 op goto childerr;
497 72342dc9 2020-11-06 op
498 721e2325 2020-11-18 op if (asprintf(&portno, "%d", port) == -1)
499 721e2325 2020-11-18 op goto childerr;
500 721e2325 2020-11-18 op
501 a5d310bc 2020-11-10 op if (asprintf(&ex, "%s%s", dir, spath+1) == -1)
502 72342dc9 2020-11-06 op goto childerr;
503 72342dc9 2020-11-06 op
504 a5d310bc 2020-11-10 op if (asprintf(&requri, "%s%s%s", spath,
505 a5d310bc 2020-11-10 op *relpath == '\0' ? "" : "/",
506 a5d310bc 2020-11-10 op relpath) == -1)
507 a5d310bc 2020-11-10 op goto childerr;
508 a5d310bc 2020-11-10 op
509 a5d310bc 2020-11-10 op argv[0] = argv[1] = ex;
510 a5d310bc 2020-11-10 op
511 60ba426e 2020-11-06 op /* fix the env */
512 677afbd3 2020-12-02 op SAFE_SETENV("GATEWAY_INTERFACE", "CGI/1.1");
513 677afbd3 2020-12-02 op SAFE_SETENV("SERVER_SOFTWARE", "gmid");
514 677afbd3 2020-12-02 op SAFE_SETENV("SERVER_PORT", portno);
515 a5d310bc 2020-11-10 op /* setenv("SERVER_NAME", "", 1); */
516 677afbd3 2020-12-02 op SAFE_SETENV("SCRIPT_NAME", spath);
517 677afbd3 2020-12-02 op SAFE_SETENV("SCRIPT_EXECUTABLE", ex);
518 677afbd3 2020-12-02 op SAFE_SETENV("REQUEST_URI", requri);
519 677afbd3 2020-12-02 op SAFE_SETENV("REQUEST_RELATIVE", relpath);
520 677afbd3 2020-12-02 op SAFE_SETENV("QUERY_STRING", query);
521 677afbd3 2020-12-02 op SAFE_SETENV("REMOTE_HOST", addr);
522 677afbd3 2020-12-02 op SAFE_SETENV("REMOTE_ADDR", addr);
523 677afbd3 2020-12-02 op SAFE_SETENV("DOCUMENT_ROOT", dir);
524 72342dc9 2020-11-06 op
525 677afbd3 2020-12-02 op if (tls_peer_cert_provided(c->ctx)) {
526 677afbd3 2020-12-02 op SAFE_SETENV("AUTH_TYPE", "Certificate");
527 677afbd3 2020-12-02 op SAFE_SETENV("REMOTE_USER", tls_peer_cert_subject(c->ctx));
528 677afbd3 2020-12-02 op SAFE_SETENV("TLS_CLIENT_ISSUER", tls_peer_cert_issuer(c->ctx));
529 677afbd3 2020-12-02 op SAFE_SETENV("TLS_CLIENT_HASH", tls_peer_cert_hash(c->ctx));
530 677afbd3 2020-12-02 op }
531 677afbd3 2020-12-02 op
532 a5d310bc 2020-11-10 op execvp(ex, argv);
533 72342dc9 2020-11-06 op goto childerr;
534 72342dc9 2020-11-06 op }
535 72342dc9 2020-11-06 op
536 72342dc9 2020-11-06 op default: /* parent */
537 a5d310bc 2020-11-10 op close(p[1]);
538 72342dc9 2020-11-06 op close(c->fd);
539 72342dc9 2020-11-06 op c->fd = p[0];
540 72342dc9 2020-11-06 op c->child = pid;
541 aff8d190 2020-11-06 op mark_nonblock(c->fd);
542 aff8d190 2020-11-06 op c->state = S_SENDING;
543 72342dc9 2020-11-06 op handle_cgi(fds, c);
544 a5d310bc 2020-11-10 op return 0;
545 72342dc9 2020-11-06 op }
546 72342dc9 2020-11-06 op
547 72342dc9 2020-11-06 op err:
548 72342dc9 2020-11-06 op if (!start_reply(fds, c, TEMP_FAILURE, "internal server error"))
549 a5d310bc 2020-11-10 op return 0;
550 72342dc9 2020-11-06 op goodbye(fds, c);
551 a5d310bc 2020-11-10 op return 0;
552 72342dc9 2020-11-06 op
553 72342dc9 2020-11-06 op childerr:
554 72342dc9 2020-11-06 op dprintf(p[1], "%d internal server error\r\n", TEMP_FAILURE);
555 72342dc9 2020-11-06 op close(p[1]);
556 a5d310bc 2020-11-10 op _exit(1);
557 72342dc9 2020-11-06 op }
558 72342dc9 2020-11-06 op
559 72342dc9 2020-11-06 op void
560 aff8d190 2020-11-06 op cgi_setpoll_on_child(struct pollfd *fds, struct client *c)
561 72342dc9 2020-11-06 op {
562 aff8d190 2020-11-06 op int fd;
563 72342dc9 2020-11-06 op
564 aff8d190 2020-11-06 op if (c->waiting_on_child)
565 aff8d190 2020-11-06 op return;
566 aff8d190 2020-11-06 op c->waiting_on_child = 1;
567 aff8d190 2020-11-06 op
568 aff8d190 2020-11-06 op fds->events = POLLIN;
569 aff8d190 2020-11-06 op
570 aff8d190 2020-11-06 op fd = fds->fd;
571 aff8d190 2020-11-06 op fds->fd = c->fd;
572 aff8d190 2020-11-06 op c->fd = fd;
573 aff8d190 2020-11-06 op }
574 aff8d190 2020-11-06 op
575 aff8d190 2020-11-06 op void
576 aff8d190 2020-11-06 op cgi_setpoll_on_client(struct pollfd *fds, struct client *c)
577 aff8d190 2020-11-06 op {
578 aff8d190 2020-11-06 op int fd;
579 aff8d190 2020-11-06 op
580 aff8d190 2020-11-06 op if (!c->waiting_on_child)
581 aff8d190 2020-11-06 op return;
582 aff8d190 2020-11-06 op c->waiting_on_child = 0;
583 aff8d190 2020-11-06 op
584 aff8d190 2020-11-06 op fd = fds->fd;
585 aff8d190 2020-11-06 op fds->fd = c->fd;
586 aff8d190 2020-11-06 op c->fd = fd;
587 aff8d190 2020-11-06 op }
588 aff8d190 2020-11-06 op
589 aff8d190 2020-11-06 op void
590 aff8d190 2020-11-06 op handle_cgi(struct pollfd *fds, struct client *c)
591 aff8d190 2020-11-06 op {
592 aff8d190 2020-11-06 op ssize_t r;
593 aff8d190 2020-11-06 op
594 aff8d190 2020-11-06 op /* ensure c->fd is the child and fds->fd the client */
595 aff8d190 2020-11-06 op cgi_setpoll_on_client(fds, c);
596 aff8d190 2020-11-06 op
597 72342dc9 2020-11-06 op while (1) {
598 aff8d190 2020-11-06 op if (c->len == 0) {
599 aff8d190 2020-11-06 op if ((r = read(c->fd, c->sbuf, sizeof(c->sbuf))) == 0)
600 aff8d190 2020-11-06 op goto end;
601 aff8d190 2020-11-06 op if (r == -1) {
602 aff8d190 2020-11-06 op if (errno == EAGAIN || errno == EWOULDBLOCK) {
603 aff8d190 2020-11-06 op cgi_setpoll_on_child(fds, c);
604 aff8d190 2020-11-06 op return;
605 aff8d190 2020-11-06 op }
606 aff8d190 2020-11-06 op goto end;
607 aff8d190 2020-11-06 op }
608 aff8d190 2020-11-06 op c->len = r;
609 aff8d190 2020-11-06 op c->off = 0;
610 aff8d190 2020-11-06 op }
611 aff8d190 2020-11-06 op
612 aff8d190 2020-11-06 op while (c->len > 0) {
613 aff8d190 2020-11-06 op switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
614 72342dc9 2020-11-06 op case -1:
615 72342dc9 2020-11-06 op goto end;
616 72342dc9 2020-11-06 op
617 72342dc9 2020-11-06 op case TLS_WANT_POLLOUT:
618 aff8d190 2020-11-06 op fds->events = POLLOUT;
619 aff8d190 2020-11-06 op return;
620 aff8d190 2020-11-06 op
621 72342dc9 2020-11-06 op case TLS_WANT_POLLIN:
622 aff8d190 2020-11-06 op fds->events = POLLIN;
623 aff8d190 2020-11-06 op return;
624 72342dc9 2020-11-06 op
625 72342dc9 2020-11-06 op default:
626 aff8d190 2020-11-06 op c->off += r;
627 aff8d190 2020-11-06 op c->len -= r;
628 aff8d190 2020-11-06 op break;
629 72342dc9 2020-11-06 op }
630 72342dc9 2020-11-06 op }
631 72342dc9 2020-11-06 op }
632 72342dc9 2020-11-06 op
633 72342dc9 2020-11-06 op end:
634 72342dc9 2020-11-06 op goodbye(fds, c);
635 f28f9311 2020-10-14 op }
636 592fd624 2020-10-07 op
637 f28f9311 2020-10-14 op void
638 75d233f0 2020-11-06 op send_file(char *path, char *query, struct pollfd *fds, struct client *c)
639 f28f9311 2020-10-14 op {
640 f28f9311 2020-10-14 op ssize_t ret, len;
641 592fd624 2020-10-07 op
642 f28f9311 2020-10-14 op if (c->fd == -1) {
643 75d233f0 2020-11-06 op if (!open_file(path, query, fds, c))
644 f28f9311 2020-10-14 op return;
645 f28f9311 2020-10-14 op c->state = S_SENDING;
646 f28f9311 2020-10-14 op }
647 592fd624 2020-10-07 op
648 f28f9311 2020-10-14 op len = (c->buf + c->len) - c->i;
649 f28f9311 2020-10-14 op
650 f28f9311 2020-10-14 op while (len > 0) {
651 f28f9311 2020-10-14 op switch (ret = tls_write(c->ctx, c->i, len)) {
652 f28f9311 2020-10-14 op case -1:
653 2c3a40fa 2020-11-06 op LOG(c, "tls_write: %s", tls_error(c->ctx));
654 f28f9311 2020-10-14 op goodbye(fds, c);
655 f28f9311 2020-10-14 op return;
656 f28f9311 2020-10-14 op
657 f28f9311 2020-10-14 op case TLS_WANT_POLLIN:
658 f28f9311 2020-10-14 op fds->events = POLLIN;
659 f28f9311 2020-10-14 op return;
660 f28f9311 2020-10-14 op
661 f28f9311 2020-10-14 op case TLS_WANT_POLLOUT:
662 f28f9311 2020-10-14 op fds->events = POLLOUT;
663 f28f9311 2020-10-14 op return;
664 f28f9311 2020-10-14 op
665 f28f9311 2020-10-14 op default:
666 f28f9311 2020-10-14 op c->i += ret;
667 f28f9311 2020-10-14 op len -= ret;
668 f28f9311 2020-10-14 op break;
669 3e4749f7 2020-10-02 op }
670 3e4749f7 2020-10-02 op }
671 f28f9311 2020-10-14 op
672 f28f9311 2020-10-14 op goodbye(fds, c);
673 3e4749f7 2020-10-02 op }
674 3e4749f7 2020-10-02 op
675 3e4749f7 2020-10-02 op void
676 592fd624 2020-10-07 op send_dir(char *path, struct pollfd *fds, struct client *client)
677 3e4749f7 2020-10-02 op {
678 3e4749f7 2020-10-02 op char fpath[PATHBUF];
679 3e4749f7 2020-10-02 op size_t len;
680 3e4749f7 2020-10-02 op
681 3e4749f7 2020-10-02 op bzero(fpath, PATHBUF);
682 3e4749f7 2020-10-02 op
683 9c56b0a7 2020-10-14 op if (path[0] != '.')
684 3e4749f7 2020-10-02 op fpath[0] = '.';
685 3e4749f7 2020-10-02 op
686 3e4749f7 2020-10-02 op /* this cannot fail since sizeof(fpath) > maxlen of path */
687 3e4749f7 2020-10-02 op strlcat(fpath, path, PATHBUF);
688 3e4749f7 2020-10-02 op len = strlen(fpath);
689 3e4749f7 2020-10-02 op
690 3e4749f7 2020-10-02 op /* add a trailing / in case. */
691 3e4749f7 2020-10-02 op if (fpath[len-1] != '/') {
692 3e4749f7 2020-10-02 op fpath[len] = '/';
693 3e4749f7 2020-10-02 op }
694 3e4749f7 2020-10-02 op
695 3e4749f7 2020-10-02 op strlcat(fpath, "index.gmi", sizeof(fpath));
696 3e4749f7 2020-10-02 op
697 75d233f0 2020-11-06 op send_file(fpath, NULL, fds, client);
698 3e4749f7 2020-10-02 op }
699 3e4749f7 2020-10-02 op
700 3e4749f7 2020-10-02 op void
701 592fd624 2020-10-07 op handle(struct pollfd *fds, struct client *client)
702 3e4749f7 2020-10-02 op {
703 592fd624 2020-10-07 op char buf[GEMINI_URL_LEN];
704 3e4749f7 2020-10-02 op char *path;
705 aa0fe0cf 2020-11-06 op char *query;
706 3e4749f7 2020-10-02 op
707 592fd624 2020-10-07 op switch (client->state) {
708 592fd624 2020-10-07 op case S_OPEN:
709 592fd624 2020-10-07 op bzero(buf, GEMINI_URL_LEN);
710 592fd624 2020-10-07 op switch (tls_read(client->ctx, buf, sizeof(buf)-1)) {
711 592fd624 2020-10-07 op case -1:
712 2c3a40fa 2020-11-06 op LOG(client, "tls_read: %s", tls_error(client->ctx));
713 592fd624 2020-10-07 op goodbye(fds, client);
714 592fd624 2020-10-07 op return;
715 3e4749f7 2020-10-02 op
716 592fd624 2020-10-07 op case TLS_WANT_POLLIN:
717 592fd624 2020-10-07 op fds->events = POLLIN;
718 592fd624 2020-10-07 op return;
719 3e4749f7 2020-10-02 op
720 592fd624 2020-10-07 op case TLS_WANT_POLLOUT:
721 592fd624 2020-10-07 op fds->events = POLLOUT;
722 592fd624 2020-10-07 op return;
723 592fd624 2020-10-07 op }
724 3e4749f7 2020-10-02 op
725 2c3a40fa 2020-11-06 op if (!url_trim(client, buf)) {
726 592fd624 2020-10-07 op if (!start_reply(fds, client, BAD_REQUEST, "bad request"))
727 592fd624 2020-10-07 op return;
728 592fd624 2020-10-07 op goodbye(fds, client);
729 592fd624 2020-10-07 op return;
730 592fd624 2020-10-07 op }
731 3e4749f7 2020-10-02 op
732 592fd624 2020-10-07 op if ((path = url_start_of_request(buf)) == NULL) {
733 592fd624 2020-10-07 op if (!start_reply(fds, client, BAD_REQUEST, "bad request"))
734 592fd624 2020-10-07 op return;
735 592fd624 2020-10-07 op goodbye(fds, client);
736 592fd624 2020-10-07 op return;
737 592fd624 2020-10-07 op }
738 592fd624 2020-10-07 op
739 aa0fe0cf 2020-11-06 op query = adjust_path(path);
740 aa0fe0cf 2020-11-06 op LOG(client, "get %s%s%s", path,
741 aa0fe0cf 2020-11-06 op query ? "?" : "",
742 aa0fe0cf 2020-11-06 op query ? query : "");
743 592fd624 2020-10-07 op
744 a5d310bc 2020-11-10 op send_file(path, query, fds, client);
745 592fd624 2020-10-07 op break;
746 592fd624 2020-10-07 op
747 592fd624 2020-10-07 op case S_INITIALIZING:
748 592fd624 2020-10-07 op if (!start_reply(fds, client, client->code, client->meta))
749 592fd624 2020-10-07 op return;
750 592fd624 2020-10-07 op
751 592fd624 2020-10-07 op if (client->code != SUCCESS) {
752 592fd624 2020-10-07 op /* we don't need a body */
753 592fd624 2020-10-07 op goodbye(fds, client);
754 592fd624 2020-10-07 op return;
755 592fd624 2020-10-07 op }
756 592fd624 2020-10-07 op
757 592fd624 2020-10-07 op client->state = S_SENDING;
758 592fd624 2020-10-07 op
759 592fd624 2020-10-07 op /* fallthrough */
760 592fd624 2020-10-07 op
761 592fd624 2020-10-07 op case S_SENDING:
762 aff8d190 2020-11-06 op if (client->child != -1)
763 aff8d190 2020-11-06 op handle_cgi(fds, client);
764 aff8d190 2020-11-06 op else
765 aff8d190 2020-11-06 op send_file(NULL, NULL, fds, client);
766 592fd624 2020-10-07 op break;
767 592fd624 2020-10-07 op
768 592fd624 2020-10-07 op case S_CLOSING:
769 592fd624 2020-10-07 op goodbye(fds, client);
770 592fd624 2020-10-07 op break;
771 592fd624 2020-10-07 op
772 592fd624 2020-10-07 op default:
773 592fd624 2020-10-07 op /* unreachable */
774 592fd624 2020-10-07 op abort();
775 592fd624 2020-10-07 op }
776 3e4749f7 2020-10-02 op }
777 3e4749f7 2020-10-02 op
778 592fd624 2020-10-07 op void
779 592fd624 2020-10-07 op mark_nonblock(int fd)
780 592fd624 2020-10-07 op {
781 592fd624 2020-10-07 op int flags;
782 592fd624 2020-10-07 op
783 592fd624 2020-10-07 op if ((flags = fcntl(fd, F_GETFL)) == -1)
784 592fd624 2020-10-07 op err(1, "fcntl(F_GETFL)");
785 592fd624 2020-10-07 op if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
786 592fd624 2020-10-07 op err(1, "fcntl(F_SETFL)");
787 592fd624 2020-10-07 op }
788 592fd624 2020-10-07 op
789 3e4749f7 2020-10-02 op int
790 9468027b 2020-10-15 op make_socket(int port, int family)
791 3e4749f7 2020-10-02 op {
792 3e4749f7 2020-10-02 op int sock, v;
793 9468027b 2020-10-15 op struct sockaddr_in addr4;
794 9468027b 2020-10-15 op struct sockaddr_in6 addr6;
795 9468027b 2020-10-15 op struct sockaddr *addr;
796 9468027b 2020-10-15 op socklen_t len;
797 3e4749f7 2020-10-02 op
798 9468027b 2020-10-15 op switch (family) {
799 9468027b 2020-10-15 op case AF_INET:
800 9468027b 2020-10-15 op bzero(&addr4, sizeof(addr4));
801 9468027b 2020-10-15 op addr4.sin_family = family;
802 9468027b 2020-10-15 op addr4.sin_port = htons(port);
803 9468027b 2020-10-15 op addr4.sin_addr.s_addr = INADDR_ANY;
804 9468027b 2020-10-15 op addr = (struct sockaddr*)&addr4;
805 9468027b 2020-10-15 op len = sizeof(addr4);
806 9468027b 2020-10-15 op break;
807 9468027b 2020-10-15 op
808 9468027b 2020-10-15 op case AF_INET6:
809 9468027b 2020-10-15 op bzero(&addr6, sizeof(addr6));
810 9468027b 2020-10-15 op addr6.sin6_family = AF_INET6;
811 9468027b 2020-10-15 op addr6.sin6_port = htons(port);
812 9468027b 2020-10-15 op addr6.sin6_addr = in6addr_any;
813 9468027b 2020-10-15 op addr = (struct sockaddr*)&addr6;
814 9468027b 2020-10-15 op len = sizeof(addr6);
815 9468027b 2020-10-15 op break;
816 9468027b 2020-10-15 op
817 9468027b 2020-10-15 op default:
818 9468027b 2020-10-15 op /* unreachable */
819 9468027b 2020-10-15 op abort();
820 9468027b 2020-10-15 op }
821 9468027b 2020-10-15 op
822 9468027b 2020-10-15 op if ((sock = socket(family, SOCK_STREAM, 0)) == -1)
823 f28f9311 2020-10-14 op err(1, "socket");
824 3e4749f7 2020-10-02 op
825 3e4749f7 2020-10-02 op v = 1;
826 3e4749f7 2020-10-02 op if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
827 3e4749f7 2020-10-02 op err(1, "setsockopt(SO_REUSEADDR)");
828 3e4749f7 2020-10-02 op
829 3e4749f7 2020-10-02 op v = 1;
830 3e4749f7 2020-10-02 op if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
831 3e4749f7 2020-10-02 op err(1, "setsockopt(SO_REUSEPORT)");
832 3e4749f7 2020-10-02 op
833 592fd624 2020-10-07 op mark_nonblock(sock);
834 592fd624 2020-10-07 op
835 9468027b 2020-10-15 op if (bind(sock, addr, len) == -1)
836 f28f9311 2020-10-14 op err(1, "bind");
837 3e4749f7 2020-10-02 op
838 3e4749f7 2020-10-02 op if (listen(sock, 16) == -1)
839 f28f9311 2020-10-14 op err(1, "listen");
840 3e4749f7 2020-10-02 op
841 3e4749f7 2020-10-02 op return sock;
842 3e4749f7 2020-10-02 op }
843 3e4749f7 2020-10-02 op
844 3e4749f7 2020-10-02 op void
845 592fd624 2020-10-07 op do_accept(int sock, struct tls *ctx, struct pollfd *fds, struct client *clients)
846 3e4749f7 2020-10-02 op {
847 592fd624 2020-10-07 op int i, fd;
848 592fd624 2020-10-07 op struct sockaddr_in addr;
849 3e4749f7 2020-10-02 op socklen_t len;
850 3e4749f7 2020-10-02 op
851 592fd624 2020-10-07 op len = sizeof(addr);
852 592fd624 2020-10-07 op if ((fd = accept(sock, (struct sockaddr*)&addr, &len)) == -1) {
853 592fd624 2020-10-07 op if (errno == EWOULDBLOCK)
854 592fd624 2020-10-07 op return;
855 592fd624 2020-10-07 op err(1, "accept");
856 592fd624 2020-10-07 op }
857 3e4749f7 2020-10-02 op
858 592fd624 2020-10-07 op mark_nonblock(fd);
859 3e4749f7 2020-10-02 op
860 592fd624 2020-10-07 op for (i = 0; i < MAX_USERS; ++i) {
861 592fd624 2020-10-07 op if (fds[i].fd == -1) {
862 592fd624 2020-10-07 op bzero(&clients[i], sizeof(struct client));
863 592fd624 2020-10-07 op if (tls_accept_socket(ctx, &clients[i].ctx, fd) == -1)
864 592fd624 2020-10-07 op break; /* goodbye fd! */
865 592fd624 2020-10-07 op
866 592fd624 2020-10-07 op fds[i].fd = fd;
867 592fd624 2020-10-07 op fds[i].events = POLLIN;
868 592fd624 2020-10-07 op
869 592fd624 2020-10-07 op clients[i].state = S_OPEN;
870 592fd624 2020-10-07 op clients[i].fd = -1;
871 72342dc9 2020-11-06 op clients[i].child = -1;
872 f28f9311 2020-10-14 op clients[i].buf = MAP_FAILED;
873 2c3a40fa 2020-11-06 op clients[i].af = AF_INET;
874 2c3a40fa 2020-11-06 op clients[i].addr = addr.sin_addr;
875 592fd624 2020-10-07 op
876 120381c9 2020-11-06 op connected_clients++;
877 592fd624 2020-10-07 op return;
878 3e4749f7 2020-10-02 op }
879 592fd624 2020-10-07 op }
880 3e4749f7 2020-10-02 op
881 592fd624 2020-10-07 op close(fd);
882 592fd624 2020-10-07 op }
883 3e4749f7 2020-10-02 op
884 592fd624 2020-10-07 op void
885 592fd624 2020-10-07 op goodbye(struct pollfd *pfd, struct client *c)
886 592fd624 2020-10-07 op {
887 592fd624 2020-10-07 op ssize_t ret;
888 592fd624 2020-10-07 op
889 592fd624 2020-10-07 op c->state = S_CLOSING;
890 592fd624 2020-10-07 op
891 592fd624 2020-10-07 op ret = tls_close(c->ctx);
892 592fd624 2020-10-07 op if (ret == TLS_WANT_POLLIN) {
893 592fd624 2020-10-07 op pfd->events = POLLIN;
894 592fd624 2020-10-07 op return;
895 3e4749f7 2020-10-02 op }
896 592fd624 2020-10-07 op if (ret == TLS_WANT_POLLOUT) {
897 592fd624 2020-10-07 op pfd->events = POLLOUT;
898 592fd624 2020-10-07 op return;
899 592fd624 2020-10-07 op }
900 592fd624 2020-10-07 op
901 120381c9 2020-11-06 op connected_clients--;
902 120381c9 2020-11-06 op
903 592fd624 2020-10-07 op tls_free(c->ctx);
904 592fd624 2020-10-07 op c->ctx = NULL;
905 592fd624 2020-10-07 op
906 f28f9311 2020-10-14 op if (c->buf != MAP_FAILED)
907 f28f9311 2020-10-14 op munmap(c->buf, c->len);
908 f28f9311 2020-10-14 op
909 592fd624 2020-10-07 op if (c->fd != -1)
910 592fd624 2020-10-07 op close(c->fd);
911 592fd624 2020-10-07 op
912 592fd624 2020-10-07 op close(pfd->fd);
913 592fd624 2020-10-07 op pfd->fd = -1;
914 3e4749f7 2020-10-02 op }
915 3e4749f7 2020-10-02 op
916 3e4749f7 2020-10-02 op void
917 592fd624 2020-10-07 op loop(struct tls *ctx, int sock)
918 592fd624 2020-10-07 op {
919 592fd624 2020-10-07 op int i, todo;
920 592fd624 2020-10-07 op struct client clients[MAX_USERS];
921 592fd624 2020-10-07 op struct pollfd fds[MAX_USERS];
922 592fd624 2020-10-07 op
923 592fd624 2020-10-07 op for (i = 0; i < MAX_USERS; ++i) {
924 592fd624 2020-10-07 op fds[i].fd = -1;
925 592fd624 2020-10-07 op fds[i].events = POLLIN;
926 592fd624 2020-10-07 op bzero(&clients[i], sizeof(struct client));
927 592fd624 2020-10-07 op }
928 592fd624 2020-10-07 op
929 592fd624 2020-10-07 op fds[0].fd = sock;
930 592fd624 2020-10-07 op
931 592fd624 2020-10-07 op for (;;) {
932 120381c9 2020-11-06 op if ((todo = poll(fds, MAX_USERS, INFTIM)) == -1) {
933 120381c9 2020-11-06 op if (errno == EINTR) {
934 120381c9 2020-11-06 op warnx("connected clients: %d", connected_clients);
935 120381c9 2020-11-06 op continue;
936 120381c9 2020-11-06 op }
937 592fd624 2020-10-07 op err(1, "poll");
938 120381c9 2020-11-06 op }
939 592fd624 2020-10-07 op
940 592fd624 2020-10-07 op for (i = 0; i < MAX_USERS; i++) {
941 592fd624 2020-10-07 op assert(i < MAX_USERS);
942 592fd624 2020-10-07 op
943 592fd624 2020-10-07 op if (fds[i].revents == 0)
944 592fd624 2020-10-07 op continue;
945 592fd624 2020-10-07 op
946 592fd624 2020-10-07 op if (fds[i].revents & (POLLERR|POLLNVAL))
947 592fd624 2020-10-07 op err(1, "bad fd %d", fds[i].fd);
948 592fd624 2020-10-07 op
949 592fd624 2020-10-07 op if (fds[i].revents & POLLHUP) {
950 aff8d190 2020-11-06 op /* fds[i] may be the fd of the stdin
951 aff8d190 2020-11-06 op * of a cgi script that has exited. */
952 aff8d190 2020-11-06 op if (!clients[i].waiting_on_child) {
953 aff8d190 2020-11-06 op goodbye(&fds[i], &clients[i]);
954 aff8d190 2020-11-06 op continue;
955 aff8d190 2020-11-06 op }
956 592fd624 2020-10-07 op }
957 592fd624 2020-10-07 op
958 592fd624 2020-10-07 op todo--;
959 592fd624 2020-10-07 op
960 592fd624 2020-10-07 op if (i == 0) { /* new client */
961 592fd624 2020-10-07 op do_accept(sock, ctx, fds, clients);
962 592fd624 2020-10-07 op continue;
963 592fd624 2020-10-07 op }
964 592fd624 2020-10-07 op
965 592fd624 2020-10-07 op handle(&fds[i], &clients[i]);
966 592fd624 2020-10-07 op }
967 592fd624 2020-10-07 op }
968 592fd624 2020-10-07 op }
969 592fd624 2020-10-07 op
970 592fd624 2020-10-07 op void
971 3e4749f7 2020-10-02 op usage(const char *me)
972 3e4749f7 2020-10-02 op {
973 3e4749f7 2020-10-02 op fprintf(stderr,
974 72f653b6 2020-11-06 op "USAGE: %s [-h] [-c cert.pem] [-d docs] [-k key.pem] "
975 721e2325 2020-11-18 op "[-l logfile] [-p port] [-x cgi-bin]\n",
976 3e4749f7 2020-10-02 op me);
977 3e4749f7 2020-10-02 op }
978 3e4749f7 2020-10-02 op
979 3e4749f7 2020-10-02 op int
980 3e4749f7 2020-10-02 op main(int argc, char **argv)
981 3e4749f7 2020-10-02 op {
982 72342dc9 2020-11-06 op const char *cert = "cert.pem", *key = "key.pem";
983 3e4749f7 2020-10-02 op struct tls *ctx = NULL;
984 3e4749f7 2020-10-02 op struct tls_config *conf;
985 3e4749f7 2020-10-02 op int sock, ch;
986 3e4749f7 2020-10-02 op
987 0cf902af 2020-11-03 op signal(SIGPIPE, SIG_IGN);
988 72342dc9 2020-11-06 op signal(SIGCHLD, SIG_IGN);
989 0cf902af 2020-11-03 op
990 120381c9 2020-11-06 op #ifdef SIGINFO
991 120381c9 2020-11-06 op signal(SIGINFO, siginfo_handler);
992 120381c9 2020-11-06 op #endif
993 120381c9 2020-11-06 op signal(SIGUSR2, siginfo_handler);
994 120381c9 2020-11-06 op
995 120381c9 2020-11-06 op connected_clients = 0;
996 120381c9 2020-11-06 op
997 72342dc9 2020-11-06 op dir = "docs/";
998 2c3a40fa 2020-11-06 op logfd = 2; /* stderr */
999 72f653b6 2020-11-06 op cgi = NULL;
1000 721e2325 2020-11-18 op port = 1965;
1001 2c3a40fa 2020-11-06 op
1002 721e2325 2020-11-18 op while ((ch = getopt(argc, argv, "c:d:hk:l:p:x:")) != -1) {
1003 3e4749f7 2020-10-02 op switch (ch) {
1004 3e4749f7 2020-10-02 op case 'c':
1005 3e4749f7 2020-10-02 op cert = optarg;
1006 3e4749f7 2020-10-02 op break;
1007 3e4749f7 2020-10-02 op
1008 3e4749f7 2020-10-02 op case 'd':
1009 3e4749f7 2020-10-02 op dir = optarg;
1010 3e4749f7 2020-10-02 op break;
1011 3e4749f7 2020-10-02 op
1012 3e4749f7 2020-10-02 op case 'h':
1013 3e4749f7 2020-10-02 op usage(*argv);
1014 3e4749f7 2020-10-02 op return 0;
1015 3e4749f7 2020-10-02 op
1016 3e4749f7 2020-10-02 op case 'k':
1017 3e4749f7 2020-10-02 op key = optarg;
1018 3e4749f7 2020-10-02 op break;
1019 3e4749f7 2020-10-02 op
1020 2c3a40fa 2020-11-06 op case 'l':
1021 2c3a40fa 2020-11-06 op /* open log file or create it with 644 */
1022 e8cac16e 2020-11-06 op if ((logfd = open(optarg, O_WRONLY | O_CREAT | O_CLOEXEC,
1023 2c3a40fa 2020-11-06 op S_IRUSR | S_IWUSR | S_IRGRP | S_IWOTH)) == -1)
1024 2c3a40fa 2020-11-06 op err(1, "%s", optarg);
1025 2c3a40fa 2020-11-06 op break;
1026 2c3a40fa 2020-11-06 op
1027 721e2325 2020-11-18 op case 'p': {
1028 721e2325 2020-11-18 op char *ep;
1029 721e2325 2020-11-18 op long lval;
1030 721e2325 2020-11-18 op
1031 721e2325 2020-11-18 op errno = 0;
1032 721e2325 2020-11-18 op lval = strtol(optarg, &ep, 10);
1033 721e2325 2020-11-18 op if (optarg[0] == '\0' || *ep != '\0')
1034 721e2325 2020-11-18 op err(1, "not a number: %s", optarg);
1035 721e2325 2020-11-18 op if (lval < 0 || lval > UINT16_MAX)
1036 721e2325 2020-11-18 op err(1, "port number out of range: %s", optarg);
1037 721e2325 2020-11-18 op port = lval;
1038 721e2325 2020-11-18 op break;
1039 721e2325 2020-11-18 op }
1040 721e2325 2020-11-18 op
1041 72342dc9 2020-11-06 op case 'x':
1042 72f653b6 2020-11-06 op cgi = optarg;
1043 72342dc9 2020-11-06 op break;
1044 72342dc9 2020-11-06 op
1045 3e4749f7 2020-10-02 op default:
1046 3e4749f7 2020-10-02 op usage(*argv);
1047 3e4749f7 2020-10-02 op return 1;
1048 3e4749f7 2020-10-02 op }
1049 3e4749f7 2020-10-02 op }
1050 3e4749f7 2020-10-02 op
1051 3e4749f7 2020-10-02 op if ((conf = tls_config_new()) == NULL)
1052 3e4749f7 2020-10-02 op err(1, "tls_config_new");
1053 0d8ca45a 2020-10-03 op
1054 677afbd3 2020-12-02 op /* optionally accept client certs, but don't try to verify them */
1055 677afbd3 2020-12-02 op tls_config_verify_client_optional(conf);
1056 677afbd3 2020-12-02 op tls_config_insecure_noverifycert(conf);
1057 677afbd3 2020-12-02 op
1058 0d8ca45a 2020-10-03 op if (tls_config_set_protocols(conf,
1059 0d8ca45a 2020-10-03 op TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3) == -1)
1060 0d8ca45a 2020-10-03 op err(1, "tls_config_set_protocols");
1061 3e4749f7 2020-10-02 op
1062 4d4f0e19 2020-10-03 op if (tls_config_set_cert_file(conf, cert) == -1)
1063 4d4f0e19 2020-10-03 op err(1, "tls_config_set_cert_file: %s", cert);
1064 3e4749f7 2020-10-02 op
1065 4d4f0e19 2020-10-03 op if (tls_config_set_key_file(conf, key) == -1)
1066 4d4f0e19 2020-10-03 op err(1, "tls_config_set_key_file: %s", key);
1067 3e4749f7 2020-10-02 op
1068 3e4749f7 2020-10-02 op if ((ctx = tls_server()) == NULL)
1069 3e4749f7 2020-10-02 op err(1, "tls_server");
1070 3e4749f7 2020-10-02 op
1071 3e4749f7 2020-10-02 op if (tls_configure(ctx, conf) == -1)
1072 3e4749f7 2020-10-02 op errx(1, "tls_configure: %s", tls_error(ctx));
1073 3e4749f7 2020-10-02 op
1074 721e2325 2020-11-18 op sock = make_socket(port, AF_INET);
1075 3e4749f7 2020-10-02 op
1076 3e4749f7 2020-10-02 op if ((dirfd = open(dir, O_RDONLY | O_DIRECTORY)) == -1)
1077 3e4749f7 2020-10-02 op err(1, "open: %s", dir);
1078 3e4749f7 2020-10-02 op
1079 72f653b6 2020-11-06 op if (cgi != NULL) {
1080 72f653b6 2020-11-06 op if (unveil(dir, "rx") == -1)
1081 72f653b6 2020-11-06 op err(1, "unveil");
1082 72f653b6 2020-11-06 op if (pledge("stdio rpath inet proc exec", NULL) == -1)
1083 72f653b6 2020-11-06 op err(1, "pledge");
1084 72f653b6 2020-11-06 op } else {
1085 72f653b6 2020-11-06 op if (unveil(dir, "r") == -1)
1086 72f653b6 2020-11-06 op err(1, "unveil");
1087 72f653b6 2020-11-06 op if (pledge("stdio rpath inet", NULL) == -1)
1088 72f653b6 2020-11-06 op err(1, "pledge");
1089 72f653b6 2020-11-06 op }
1090 3e4749f7 2020-10-02 op
1091 3e4749f7 2020-10-02 op loop(ctx, sock);
1092 3e4749f7 2020-10-02 op
1093 3e4749f7 2020-10-02 op close(sock);
1094 3e4749f7 2020-10-02 op tls_free(ctx);
1095 3e4749f7 2020-10-02 op tls_config_free(conf);
1096 3e4749f7 2020-10-02 op }