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