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 3e4749f7 2020-10-02 op #include <sys/socket.h>
18 3e4749f7 2020-10-02 op #include <sys/stat.h>
19 3e4749f7 2020-10-02 op
20 3e4749f7 2020-10-02 op #include <netinet/in.h>
21 3e4749f7 2020-10-02 op
22 3e4749f7 2020-10-02 op #include <assert.h>
23 3e4749f7 2020-10-02 op #include <err.h>
24 592fd624 2020-10-07 op #include <errno.h>
25 3e4749f7 2020-10-02 op #include <fcntl.h>
26 592fd624 2020-10-07 op #include <poll.h>
27 3e4749f7 2020-10-02 op #include <stdio.h>
28 3e4749f7 2020-10-02 op #include <stdlib.h>
29 3e4749f7 2020-10-02 op #include <string.h>
30 3e4749f7 2020-10-02 op #include <tls.h>
31 3e4749f7 2020-10-02 op #include <unistd.h>
32 3e4749f7 2020-10-02 op
33 3e4749f7 2020-10-02 op #ifndef __OpenBSD__
34 3e4749f7 2020-10-02 op # define pledge(a, b) 0
35 3e4749f7 2020-10-02 op # define unveil(a, b) 0
36 3e4749f7 2020-10-02 op #endif /* __OpenBSD__ */
37 3e4749f7 2020-10-02 op
38 592fd624 2020-10-07 op #ifndef INFTIM
39 592fd624 2020-10-07 op # define INFTIM -1
40 592fd624 2020-10-07 op #endif /* INFTIM */
41 592fd624 2020-10-07 op
42 3e4749f7 2020-10-02 op #define GEMINI_URL_LEN (1024+3) /* URL max len + \r\n + \0 */
43 3e4749f7 2020-10-02 op
44 4d4f0e19 2020-10-03 op /* large enough to hold a copy of a gemini URL and still have extra room */
45 d95b77a0 2020-10-07 op #define PATHBUF 2048
46 3e4749f7 2020-10-02 op
47 d95b77a0 2020-10-07 op #define FILEBUF 1024
48 3e4749f7 2020-10-02 op
49 3e4749f7 2020-10-02 op #define SUCCESS 20
50 3e4749f7 2020-10-02 op #define NOT_FOUND 51
51 3e4749f7 2020-10-02 op #define BAD_REQUEST 59
52 3e4749f7 2020-10-02 op
53 592fd624 2020-10-07 op #ifndef MAX_USERS
54 592fd624 2020-10-07 op #define MAX_USERS 64
55 592fd624 2020-10-07 op #endif
56 592fd624 2020-10-07 op
57 592fd624 2020-10-07 op enum {
58 592fd624 2020-10-07 op S_OPEN,
59 592fd624 2020-10-07 op S_INITIALIZING,
60 592fd624 2020-10-07 op S_SENDING,
61 592fd624 2020-10-07 op S_CLOSING,
62 592fd624 2020-10-07 op };
63 592fd624 2020-10-07 op
64 592fd624 2020-10-07 op struct client {
65 592fd624 2020-10-07 op struct tls *ctx;
66 592fd624 2020-10-07 op int state;
67 592fd624 2020-10-07 op int code;
68 592fd624 2020-10-07 op const char *meta;
69 592fd624 2020-10-07 op int fd;
70 592fd624 2020-10-07 op };
71 592fd624 2020-10-07 op
72 cc68fe70 2020-10-07 op struct etm { /* file extension to mime */
73 cc68fe70 2020-10-07 op const char *mime;
74 cc68fe70 2020-10-07 op const char *ext;
75 cc68fe70 2020-10-07 op } filetypes[] = {
76 cc68fe70 2020-10-07 op {"application/pdf", "pdf"},
77 cc68fe70 2020-10-07 op
78 cc68fe70 2020-10-07 op {"image/gif", "gif"},
79 cc68fe70 2020-10-07 op {"image/jpeg", "jpg"},
80 cc68fe70 2020-10-07 op {"image/jpeg", "jpeg"},
81 cc68fe70 2020-10-07 op {"image/png", "png"},
82 cc68fe70 2020-10-07 op {"image/svg+xml", "svg"},
83 cc68fe70 2020-10-07 op
84 cc68fe70 2020-10-07 op {"text/gemini", "gemini"},
85 cc68fe70 2020-10-07 op {"text/gemini", "gmi"},
86 cc68fe70 2020-10-07 op {"text/markdown", "markdown"},
87 cc68fe70 2020-10-07 op {"text/markdown", "md"},
88 cc68fe70 2020-10-07 op {"text/plain", "txt"},
89 cc68fe70 2020-10-07 op
90 cc68fe70 2020-10-07 op {NULL, NULL}
91 cc68fe70 2020-10-07 op };
92 cc68fe70 2020-10-07 op
93 3e4749f7 2020-10-02 op int dirfd;
94 3e4749f7 2020-10-02 op
95 592fd624 2020-10-07 op char *url_after_proto(char*);
96 592fd624 2020-10-07 op char *url_start_of_request(char*);
97 592fd624 2020-10-07 op int url_trim(char*);
98 592fd624 2020-10-07 op void adjust_path(char*);
99 592fd624 2020-10-07 op int path_isdir(char*);
100 592fd624 2020-10-07 op
101 592fd624 2020-10-07 op int start_reply(struct pollfd*, struct client*, int, const char*);
102 592fd624 2020-10-07 op int isdir(int);
103 cc68fe70 2020-10-07 op const char *path_ext(const char*);
104 cc68fe70 2020-10-07 op const char *mime(const char*);
105 592fd624 2020-10-07 op void send_file(char*, struct pollfd*, struct client*);
106 592fd624 2020-10-07 op void send_dir(char*, struct pollfd*, struct client*);
107 592fd624 2020-10-07 op void handle(struct pollfd*, struct client*);
108 592fd624 2020-10-07 op
109 592fd624 2020-10-07 op void mark_nonblock(int);
110 592fd624 2020-10-07 op int make_soket(int);
111 592fd624 2020-10-07 op void do_accept(int, struct tls*, struct pollfd*, struct client*);
112 592fd624 2020-10-07 op void goodbye(struct pollfd*, struct client*);
113 592fd624 2020-10-07 op void loop(struct tls*, int);
114 592fd624 2020-10-07 op
115 592fd624 2020-10-07 op void usage(const char*);
116 592fd624 2020-10-07 op
117 3e4749f7 2020-10-02 op char *
118 3e4749f7 2020-10-02 op url_after_proto(char *url)
119 3e4749f7 2020-10-02 op {
120 3e4749f7 2020-10-02 op char *s;
121 3e4749f7 2020-10-02 op const char *proto = "gemini";
122 3e4749f7 2020-10-02 op const char *marker = "://";
123 3e4749f7 2020-10-02 op
124 3e4749f7 2020-10-02 op if ((s = strstr(url, marker)) == NULL)
125 3e4749f7 2020-10-02 op return url;
126 3e4749f7 2020-10-02 op
127 3e4749f7 2020-10-02 op /* not a gemini:// URL */
128 3e4749f7 2020-10-02 op
129 3e4749f7 2020-10-02 op if (s - strlen(proto) < url)
130 3e4749f7 2020-10-02 op return NULL;
131 3e4749f7 2020-10-02 op /* TODO: */
132 3e4749f7 2020-10-02 op /* if (strcmp(s - strlen(proto), proto)) */
133 3e4749f7 2020-10-02 op /* return NULL; */
134 3e4749f7 2020-10-02 op
135 3e4749f7 2020-10-02 op /* a valid gemini:// URL */
136 3e4749f7 2020-10-02 op return s + strlen(marker);
137 3e4749f7 2020-10-02 op }
138 3e4749f7 2020-10-02 op
139 3e4749f7 2020-10-02 op char *
140 3e4749f7 2020-10-02 op url_start_of_request(char *url)
141 3e4749f7 2020-10-02 op {
142 3e4749f7 2020-10-02 op char *s, *t;
143 3e4749f7 2020-10-02 op
144 3e4749f7 2020-10-02 op if ((s = url_after_proto(url)) == NULL)
145 3e4749f7 2020-10-02 op return NULL;
146 3e4749f7 2020-10-02 op
147 3e4749f7 2020-10-02 op if ((t = strstr(s, "/")) == NULL)
148 3e4749f7 2020-10-02 op return s + strlen(s);
149 3e4749f7 2020-10-02 op return t;
150 3e4749f7 2020-10-02 op }
151 3e4749f7 2020-10-02 op
152 3e4749f7 2020-10-02 op int
153 3e4749f7 2020-10-02 op url_trim(char *url)
154 3e4749f7 2020-10-02 op {
155 3e4749f7 2020-10-02 op const char *e = "\r\n";
156 3e4749f7 2020-10-02 op char *s;
157 3e4749f7 2020-10-02 op
158 3e4749f7 2020-10-02 op if ((s = strstr(url, e)) == NULL)
159 3e4749f7 2020-10-02 op return 0;
160 3e4749f7 2020-10-02 op s[0] = '\0';
161 3e4749f7 2020-10-02 op s[1] = '\0';
162 3e4749f7 2020-10-02 op
163 3e4749f7 2020-10-02 op if (s[2] != '\0') {
164 4d4f0e19 2020-10-03 op fprintf(stderr, "the request was longer than 1024 bytes\n");
165 3e4749f7 2020-10-02 op return 0;
166 3e4749f7 2020-10-02 op }
167 3e4749f7 2020-10-02 op
168 3e4749f7 2020-10-02 op return 1;
169 3e4749f7 2020-10-02 op }
170 3e4749f7 2020-10-02 op
171 3e4749f7 2020-10-02 op void
172 3e4749f7 2020-10-02 op adjust_path(char *path)
173 3e4749f7 2020-10-02 op {
174 3e4749f7 2020-10-02 op char *s;
175 3e4749f7 2020-10-02 op size_t len;
176 3e4749f7 2020-10-02 op
177 3e4749f7 2020-10-02 op /* /.. -> / */
178 3e4749f7 2020-10-02 op len = strlen(path);
179 3e4749f7 2020-10-02 op if (len >= 3) {
180 3e4749f7 2020-10-02 op if (!strcmp(&path[len-3], "/..")) {
181 3e4749f7 2020-10-02 op path[len-2] = '\0';
182 3e4749f7 2020-10-02 op }
183 3e4749f7 2020-10-02 op }
184 3e4749f7 2020-10-02 op
185 3e4749f7 2020-10-02 op /* if the path is only `..` trim out and exit */
186 3e4749f7 2020-10-02 op if (!strcmp(path, "..")) {
187 3e4749f7 2020-10-02 op path[0] = '\0';
188 3e4749f7 2020-10-02 op return;
189 3e4749f7 2020-10-02 op }
190 3e4749f7 2020-10-02 op
191 3e4749f7 2020-10-02 op /* remove every ../ in the path */
192 3e4749f7 2020-10-02 op while (1) {
193 3e4749f7 2020-10-02 op if ((s = strstr(path, "../")) == NULL)
194 3e4749f7 2020-10-02 op return;
195 67328d23 2020-10-03 op memmove(s, s+3, strlen(s)+1); /* copy also the \0 */
196 3e4749f7 2020-10-02 op }
197 3e4749f7 2020-10-02 op }
198 3e4749f7 2020-10-02 op
199 3e4749f7 2020-10-02 op int
200 3e4749f7 2020-10-02 op path_isdir(char *path)
201 3e4749f7 2020-10-02 op {
202 3e4749f7 2020-10-02 op if (*path == '\0')
203 3e4749f7 2020-10-02 op return 1;
204 4d4f0e19 2020-10-03 op return path[strlen(path)-1] == '/';
205 3e4749f7 2020-10-02 op }
206 3e4749f7 2020-10-02 op
207 592fd624 2020-10-07 op int
208 592fd624 2020-10-07 op start_reply(struct pollfd *pfd, struct client *client, int code, const char *reason)
209 3e4749f7 2020-10-02 op {
210 3e4749f7 2020-10-02 op char buf[1030] = {0}; /* status + ' ' + max reply len + \r\n\0 */
211 3e4749f7 2020-10-02 op int len;
212 592fd624 2020-10-07 op int ret;
213 3e4749f7 2020-10-02 op
214 592fd624 2020-10-07 op client->code = code;
215 592fd624 2020-10-07 op client->meta = reason;
216 592fd624 2020-10-07 op client->state = S_INITIALIZING;
217 592fd624 2020-10-07 op
218 3e4749f7 2020-10-02 op len = snprintf(buf, sizeof(buf), "%d %s\r\n", code, reason);
219 3e4749f7 2020-10-02 op assert(len < (int)sizeof(buf));
220 592fd624 2020-10-07 op ret = tls_write(client->ctx, buf, len);
221 592fd624 2020-10-07 op if (ret == TLS_WANT_POLLIN) {
222 592fd624 2020-10-07 op pfd->events = POLLIN;
223 592fd624 2020-10-07 op return 0;
224 592fd624 2020-10-07 op }
225 592fd624 2020-10-07 op
226 592fd624 2020-10-07 op if (ret == TLS_WANT_POLLOUT) {
227 592fd624 2020-10-07 op pfd->events = POLLOUT;
228 592fd624 2020-10-07 op return 0;
229 592fd624 2020-10-07 op }
230 592fd624 2020-10-07 op
231 592fd624 2020-10-07 op return 1;
232 3e4749f7 2020-10-02 op }
233 3e4749f7 2020-10-02 op
234 3e4749f7 2020-10-02 op int
235 3e4749f7 2020-10-02 op isdir(int fd)
236 3e4749f7 2020-10-02 op {
237 3e4749f7 2020-10-02 op struct stat sb;
238 3e4749f7 2020-10-02 op
239 3e4749f7 2020-10-02 op if (fstat(fd, &sb) == -1) {
240 3e4749f7 2020-10-02 op warn("fstat");
241 4d4f0e19 2020-10-03 op return 1; /* we'll probably fail later on anyway */
242 3e4749f7 2020-10-02 op }
243 3e4749f7 2020-10-02 op
244 3e4749f7 2020-10-02 op return S_ISDIR(sb.st_mode);
245 cc68fe70 2020-10-07 op }
246 cc68fe70 2020-10-07 op
247 cc68fe70 2020-10-07 op const char *
248 cc68fe70 2020-10-07 op path_ext(const char *path)
249 cc68fe70 2020-10-07 op {
250 cc68fe70 2020-10-07 op const char *end;
251 cc68fe70 2020-10-07 op
252 cc68fe70 2020-10-07 op end = path + strlen(path)-1; /* the last byte before the NUL */
253 cc68fe70 2020-10-07 op for (; end != path; --end) {
254 cc68fe70 2020-10-07 op if (*end == '.')
255 cc68fe70 2020-10-07 op return end+1;
256 cc68fe70 2020-10-07 op if (*end == '/')
257 cc68fe70 2020-10-07 op break;
258 cc68fe70 2020-10-07 op }
259 cc68fe70 2020-10-07 op
260 cc68fe70 2020-10-07 op return NULL;
261 cc68fe70 2020-10-07 op }
262 cc68fe70 2020-10-07 op
263 cc68fe70 2020-10-07 op const char *
264 cc68fe70 2020-10-07 op mime(const char *path)
265 cc68fe70 2020-10-07 op {
266 cc68fe70 2020-10-07 op const char *ext, *def = "application/octet-stream";
267 cc68fe70 2020-10-07 op struct etm *t;
268 cc68fe70 2020-10-07 op
269 cc68fe70 2020-10-07 op if ((ext = path_ext(path)) == NULL)
270 cc68fe70 2020-10-07 op return def;
271 cc68fe70 2020-10-07 op
272 cc68fe70 2020-10-07 op for (t = filetypes; t->mime != NULL; ++t)
273 cc68fe70 2020-10-07 op if (!strcmp(ext, t->ext))
274 cc68fe70 2020-10-07 op return t->mime;
275 cc68fe70 2020-10-07 op
276 cc68fe70 2020-10-07 op return def;
277 3e4749f7 2020-10-02 op }
278 3e4749f7 2020-10-02 op
279 3e4749f7 2020-10-02 op void
280 592fd624 2020-10-07 op send_file(char *path, struct pollfd *fds, struct client *client)
281 3e4749f7 2020-10-02 op {
282 3e4749f7 2020-10-02 op char fpath[PATHBUF];
283 3e4749f7 2020-10-02 op char buf[FILEBUF];
284 3e4749f7 2020-10-02 op size_t i;
285 3e4749f7 2020-10-02 op ssize_t t, w;
286 3e4749f7 2020-10-02 op
287 592fd624 2020-10-07 op if (client->fd == -1) {
288 592fd624 2020-10-07 op assert(path != NULL);
289 3e4749f7 2020-10-02 op
290 592fd624 2020-10-07 op bzero(fpath, sizeof(fpath));
291 3e4749f7 2020-10-02 op
292 592fd624 2020-10-07 op if (*path != '.')
293 592fd624 2020-10-07 op fpath[0] = '.';
294 592fd624 2020-10-07 op strlcat(fpath, path, PATHBUF);
295 3e4749f7 2020-10-02 op
296 592fd624 2020-10-07 op if ((client->fd = openat(dirfd, fpath, O_RDONLY | O_NOFOLLOW)) == -1) {
297 592fd624 2020-10-07 op warn("open: %s", fpath);
298 592fd624 2020-10-07 op if (!start_reply(fds, client, NOT_FOUND, "not found"))
299 592fd624 2020-10-07 op return;
300 592fd624 2020-10-07 op goodbye(fds, client);
301 592fd624 2020-10-07 op return;
302 592fd624 2020-10-07 op }
303 3e4749f7 2020-10-02 op
304 592fd624 2020-10-07 op if (isdir(client->fd)) {
305 592fd624 2020-10-07 op warnx("%s is a directory, trying %s/index.gmi", fpath, fpath);
306 592fd624 2020-10-07 op close(client->fd);
307 592fd624 2020-10-07 op client->fd = -1;
308 592fd624 2020-10-07 op send_dir(fpath, fds, client);
309 592fd624 2020-10-07 op return;
310 3e4749f7 2020-10-02 op }
311 3e4749f7 2020-10-02 op
312 cc68fe70 2020-10-07 op if (!start_reply(fds, client, SUCCESS, mime(fpath)))
313 592fd624 2020-10-07 op return;
314 592fd624 2020-10-07 op }
315 3e4749f7 2020-10-02 op
316 592fd624 2020-10-07 op while (1) {
317 592fd624 2020-10-07 op w = read(client->fd, buf, sizeof(buf));
318 592fd624 2020-10-07 op if (w == -1)
319 592fd624 2020-10-07 op warn("read");
320 592fd624 2020-10-07 op if (w == 0 || w == -1) {
321 592fd624 2020-10-07 op goodbye(fds, client);
322 592fd624 2020-10-07 op return;
323 592fd624 2020-10-07 op }
324 592fd624 2020-10-07 op
325 3e4749f7 2020-10-02 op t = w;
326 3e4749f7 2020-10-02 op i = 0;
327 3e4749f7 2020-10-02 op
328 3e4749f7 2020-10-02 op while (w > 0) {
329 592fd624 2020-10-07 op t = tls_write(client->ctx, buf+i, w);
330 592fd624 2020-10-07 op switch (t) {
331 592fd624 2020-10-07 op case -1:
332 592fd624 2020-10-07 op warnx("tls_write: %s", tls_error(client->ctx));
333 592fd624 2020-10-07 op goodbye(fds, client);
334 592fd624 2020-10-07 op return;
335 592fd624 2020-10-07 op
336 592fd624 2020-10-07 op case TLS_WANT_POLLIN:
337 592fd624 2020-10-07 op case TLS_WANT_POLLOUT:
338 592fd624 2020-10-07 op fds->events = (t == TLS_WANT_POLLIN)
339 592fd624 2020-10-07 op ? POLLIN
340 592fd624 2020-10-07 op : POLLOUT;
341 592fd624 2020-10-07 op return;
342 592fd624 2020-10-07 op
343 592fd624 2020-10-07 op default:
344 592fd624 2020-10-07 op w -= t;
345 592fd624 2020-10-07 op i += t;
346 592fd624 2020-10-07 op break;
347 3e4749f7 2020-10-02 op }
348 3e4749f7 2020-10-02 op }
349 3e4749f7 2020-10-02 op }
350 3e4749f7 2020-10-02 op }
351 3e4749f7 2020-10-02 op
352 3e4749f7 2020-10-02 op void
353 592fd624 2020-10-07 op send_dir(char *path, struct pollfd *fds, struct client *client)
354 3e4749f7 2020-10-02 op {
355 3e4749f7 2020-10-02 op char fpath[PATHBUF];
356 3e4749f7 2020-10-02 op size_t len;
357 3e4749f7 2020-10-02 op
358 3e4749f7 2020-10-02 op bzero(fpath, PATHBUF);
359 3e4749f7 2020-10-02 op
360 3e4749f7 2020-10-02 op if (*path != '.')
361 3e4749f7 2020-10-02 op fpath[0] = '.';
362 3e4749f7 2020-10-02 op
363 3e4749f7 2020-10-02 op /* this cannot fail since sizeof(fpath) > maxlen of path */
364 3e4749f7 2020-10-02 op strlcat(fpath, path, PATHBUF);
365 3e4749f7 2020-10-02 op len = strlen(fpath);
366 3e4749f7 2020-10-02 op
367 3e4749f7 2020-10-02 op /* add a trailing / in case. */
368 3e4749f7 2020-10-02 op if (fpath[len-1] != '/') {
369 3e4749f7 2020-10-02 op fpath[len] = '/';
370 3e4749f7 2020-10-02 op }
371 3e4749f7 2020-10-02 op
372 3e4749f7 2020-10-02 op strlcat(fpath, "index.gmi", sizeof(fpath));
373 3e4749f7 2020-10-02 op
374 592fd624 2020-10-07 op send_file(fpath, fds, client);
375 3e4749f7 2020-10-02 op }
376 3e4749f7 2020-10-02 op
377 3e4749f7 2020-10-02 op void
378 592fd624 2020-10-07 op handle(struct pollfd *fds, struct client *client)
379 3e4749f7 2020-10-02 op {
380 592fd624 2020-10-07 op char buf[GEMINI_URL_LEN];
381 3e4749f7 2020-10-02 op char *path;
382 3e4749f7 2020-10-02 op
383 592fd624 2020-10-07 op switch (client->state) {
384 592fd624 2020-10-07 op case S_OPEN:
385 592fd624 2020-10-07 op bzero(buf, GEMINI_URL_LEN);
386 592fd624 2020-10-07 op switch (tls_read(client->ctx, buf, sizeof(buf)-1)) {
387 592fd624 2020-10-07 op case -1:
388 592fd624 2020-10-07 op warnx("tls_read: %s", tls_error(client->ctx));
389 592fd624 2020-10-07 op goodbye(fds, client);
390 592fd624 2020-10-07 op return;
391 3e4749f7 2020-10-02 op
392 592fd624 2020-10-07 op case TLS_WANT_POLLIN:
393 592fd624 2020-10-07 op fds->events = POLLIN;
394 592fd624 2020-10-07 op return;
395 3e4749f7 2020-10-02 op
396 592fd624 2020-10-07 op case TLS_WANT_POLLOUT:
397 592fd624 2020-10-07 op fds->events = POLLOUT;
398 592fd624 2020-10-07 op return;
399 592fd624 2020-10-07 op }
400 3e4749f7 2020-10-02 op
401 592fd624 2020-10-07 op if (!url_trim(buf)) {
402 592fd624 2020-10-07 op if (!start_reply(fds, client, BAD_REQUEST, "bad request"))
403 592fd624 2020-10-07 op return;
404 592fd624 2020-10-07 op goodbye(fds, client);
405 592fd624 2020-10-07 op return;
406 592fd624 2020-10-07 op }
407 3e4749f7 2020-10-02 op
408 592fd624 2020-10-07 op if ((path = url_start_of_request(buf)) == NULL) {
409 592fd624 2020-10-07 op if (!start_reply(fds, client, BAD_REQUEST, "bad request"))
410 592fd624 2020-10-07 op return;
411 592fd624 2020-10-07 op goodbye(fds, client);
412 592fd624 2020-10-07 op return;
413 592fd624 2020-10-07 op }
414 592fd624 2020-10-07 op
415 592fd624 2020-10-07 op adjust_path(path);
416 592fd624 2020-10-07 op fprintf(stderr, "requested path: %s\n", path);
417 592fd624 2020-10-07 op
418 592fd624 2020-10-07 op if (path_isdir(path))
419 592fd624 2020-10-07 op send_dir(path, fds, client);
420 592fd624 2020-10-07 op else
421 592fd624 2020-10-07 op send_file(path, fds, client);
422 592fd624 2020-10-07 op break;
423 592fd624 2020-10-07 op
424 592fd624 2020-10-07 op case S_INITIALIZING:
425 592fd624 2020-10-07 op if (!start_reply(fds, client, client->code, client->meta))
426 592fd624 2020-10-07 op return;
427 592fd624 2020-10-07 op
428 592fd624 2020-10-07 op if (client->code != SUCCESS) {
429 592fd624 2020-10-07 op /* we don't need a body */
430 592fd624 2020-10-07 op goodbye(fds, client);
431 592fd624 2020-10-07 op return;
432 592fd624 2020-10-07 op }
433 592fd624 2020-10-07 op
434 592fd624 2020-10-07 op client->state = S_SENDING;
435 592fd624 2020-10-07 op
436 592fd624 2020-10-07 op /* fallthrough */
437 592fd624 2020-10-07 op
438 592fd624 2020-10-07 op case S_SENDING:
439 592fd624 2020-10-07 op send_file(NULL, fds, client);
440 592fd624 2020-10-07 op break;
441 592fd624 2020-10-07 op
442 592fd624 2020-10-07 op case S_CLOSING:
443 592fd624 2020-10-07 op goodbye(fds, client);
444 592fd624 2020-10-07 op break;
445 592fd624 2020-10-07 op
446 592fd624 2020-10-07 op default:
447 592fd624 2020-10-07 op /* unreachable */
448 592fd624 2020-10-07 op abort();
449 592fd624 2020-10-07 op }
450 3e4749f7 2020-10-02 op }
451 3e4749f7 2020-10-02 op
452 592fd624 2020-10-07 op void
453 592fd624 2020-10-07 op mark_nonblock(int fd)
454 592fd624 2020-10-07 op {
455 592fd624 2020-10-07 op int flags;
456 592fd624 2020-10-07 op
457 592fd624 2020-10-07 op if ((flags = fcntl(fd, F_GETFL)) == -1)
458 592fd624 2020-10-07 op err(1, "fcntl(F_GETFL)");
459 592fd624 2020-10-07 op if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
460 592fd624 2020-10-07 op err(1, "fcntl(F_SETFL)");
461 592fd624 2020-10-07 op }
462 592fd624 2020-10-07 op
463 3e4749f7 2020-10-02 op int
464 3e4749f7 2020-10-02 op make_socket(int port)
465 3e4749f7 2020-10-02 op {
466 3e4749f7 2020-10-02 op int sock, v;
467 3e4749f7 2020-10-02 op struct sockaddr_in addr;
468 3e4749f7 2020-10-02 op
469 3e4749f7 2020-10-02 op if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
470 3e4749f7 2020-10-02 op err(1, "socket");
471 3e4749f7 2020-10-02 op
472 3e4749f7 2020-10-02 op v = 1;
473 3e4749f7 2020-10-02 op if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
474 3e4749f7 2020-10-02 op err(1, "setsockopt(SO_REUSEADDR)");
475 3e4749f7 2020-10-02 op
476 3e4749f7 2020-10-02 op v = 1;
477 3e4749f7 2020-10-02 op if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
478 3e4749f7 2020-10-02 op err(1, "setsockopt(SO_REUSEPORT)");
479 3e4749f7 2020-10-02 op
480 592fd624 2020-10-07 op mark_nonblock(sock);
481 592fd624 2020-10-07 op
482 3e4749f7 2020-10-02 op bzero(&addr, sizeof(addr));
483 3e4749f7 2020-10-02 op addr.sin_family = AF_INET;
484 3e4749f7 2020-10-02 op addr.sin_port = htons(port);
485 3e4749f7 2020-10-02 op addr.sin_addr.s_addr = INADDR_ANY;
486 3e4749f7 2020-10-02 op
487 3e4749f7 2020-10-02 op if (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
488 3e4749f7 2020-10-02 op err(1, "bind");
489 3e4749f7 2020-10-02 op
490 3e4749f7 2020-10-02 op if (listen(sock, 16) == -1)
491 3e4749f7 2020-10-02 op err(1, "listen");
492 3e4749f7 2020-10-02 op
493 3e4749f7 2020-10-02 op return sock;
494 3e4749f7 2020-10-02 op }
495 3e4749f7 2020-10-02 op
496 3e4749f7 2020-10-02 op void
497 592fd624 2020-10-07 op do_accept(int sock, struct tls *ctx, struct pollfd *fds, struct client *clients)
498 3e4749f7 2020-10-02 op {
499 592fd624 2020-10-07 op int i, fd;
500 592fd624 2020-10-07 op struct sockaddr_in addr;
501 3e4749f7 2020-10-02 op socklen_t len;
502 3e4749f7 2020-10-02 op
503 592fd624 2020-10-07 op len = sizeof(addr);
504 592fd624 2020-10-07 op if ((fd = accept(sock, (struct sockaddr*)&addr, &len)) == -1) {
505 592fd624 2020-10-07 op if (errno == EWOULDBLOCK)
506 592fd624 2020-10-07 op return;
507 592fd624 2020-10-07 op err(1, "accept");
508 592fd624 2020-10-07 op }
509 3e4749f7 2020-10-02 op
510 592fd624 2020-10-07 op mark_nonblock(fd);
511 3e4749f7 2020-10-02 op
512 592fd624 2020-10-07 op for (i = 0; i < MAX_USERS; ++i) {
513 592fd624 2020-10-07 op if (fds[i].fd == -1) {
514 592fd624 2020-10-07 op bzero(&clients[i], sizeof(struct client));
515 592fd624 2020-10-07 op if (tls_accept_socket(ctx, &clients[i].ctx, fd) == -1)
516 592fd624 2020-10-07 op break; /* goodbye fd! */
517 592fd624 2020-10-07 op
518 592fd624 2020-10-07 op fds[i].fd = fd;
519 592fd624 2020-10-07 op fds[i].events = POLLIN;
520 592fd624 2020-10-07 op
521 592fd624 2020-10-07 op clients[i].state = S_OPEN;
522 592fd624 2020-10-07 op clients[i].fd = -1;
523 592fd624 2020-10-07 op
524 592fd624 2020-10-07 op return;
525 3e4749f7 2020-10-02 op }
526 592fd624 2020-10-07 op }
527 3e4749f7 2020-10-02 op
528 592fd624 2020-10-07 op printf("too much clients. goodbye bro!\n");
529 592fd624 2020-10-07 op close(fd);
530 592fd624 2020-10-07 op }
531 3e4749f7 2020-10-02 op
532 592fd624 2020-10-07 op void
533 592fd624 2020-10-07 op goodbye(struct pollfd *pfd, struct client *c)
534 592fd624 2020-10-07 op {
535 592fd624 2020-10-07 op ssize_t ret;
536 592fd624 2020-10-07 op
537 592fd624 2020-10-07 op c->state = S_CLOSING;
538 592fd624 2020-10-07 op
539 592fd624 2020-10-07 op ret = tls_close(c->ctx);
540 592fd624 2020-10-07 op if (ret == TLS_WANT_POLLIN) {
541 592fd624 2020-10-07 op pfd->events = POLLIN;
542 592fd624 2020-10-07 op return;
543 3e4749f7 2020-10-02 op }
544 592fd624 2020-10-07 op if (ret == TLS_WANT_POLLOUT) {
545 592fd624 2020-10-07 op pfd->events = POLLOUT;
546 592fd624 2020-10-07 op return;
547 592fd624 2020-10-07 op }
548 592fd624 2020-10-07 op
549 592fd624 2020-10-07 op tls_free(c->ctx);
550 592fd624 2020-10-07 op c->ctx = NULL;
551 592fd624 2020-10-07 op
552 592fd624 2020-10-07 op if (c->fd != -1)
553 592fd624 2020-10-07 op close(c->fd);
554 592fd624 2020-10-07 op
555 592fd624 2020-10-07 op close(pfd->fd);
556 592fd624 2020-10-07 op pfd->fd = -1;
557 3e4749f7 2020-10-02 op }
558 3e4749f7 2020-10-02 op
559 3e4749f7 2020-10-02 op void
560 592fd624 2020-10-07 op loop(struct tls *ctx, int sock)
561 592fd624 2020-10-07 op {
562 592fd624 2020-10-07 op int i, todo;
563 592fd624 2020-10-07 op struct client clients[MAX_USERS];
564 592fd624 2020-10-07 op struct pollfd fds[MAX_USERS];
565 592fd624 2020-10-07 op
566 592fd624 2020-10-07 op for (i = 0; i < MAX_USERS; ++i) {
567 592fd624 2020-10-07 op fds[i].fd = -1;
568 592fd624 2020-10-07 op fds[i].events = POLLIN;
569 592fd624 2020-10-07 op bzero(&clients[i], sizeof(struct client));
570 592fd624 2020-10-07 op }
571 592fd624 2020-10-07 op
572 592fd624 2020-10-07 op fds[0].fd = sock;
573 592fd624 2020-10-07 op
574 592fd624 2020-10-07 op for (;;) {
575 592fd624 2020-10-07 op if ((todo = poll(fds, MAX_USERS, INFTIM)) == -1)
576 592fd624 2020-10-07 op err(1, "poll");
577 592fd624 2020-10-07 op
578 592fd624 2020-10-07 op for (i = 0; i < MAX_USERS; i++) {
579 592fd624 2020-10-07 op assert(i < MAX_USERS);
580 592fd624 2020-10-07 op
581 592fd624 2020-10-07 op if (fds[i].revents == 0)
582 592fd624 2020-10-07 op continue;
583 592fd624 2020-10-07 op
584 592fd624 2020-10-07 op if (fds[i].revents & (POLLERR|POLLNVAL))
585 592fd624 2020-10-07 op err(1, "bad fd %d", fds[i].fd);
586 592fd624 2020-10-07 op
587 592fd624 2020-10-07 op if (fds[i].revents & POLLHUP) {
588 592fd624 2020-10-07 op goodbye(&fds[i], &clients[i]);
589 592fd624 2020-10-07 op continue;
590 592fd624 2020-10-07 op }
591 592fd624 2020-10-07 op
592 592fd624 2020-10-07 op todo--;
593 592fd624 2020-10-07 op
594 592fd624 2020-10-07 op if (i == 0) { /* new client */
595 592fd624 2020-10-07 op do_accept(sock, ctx, fds, clients);
596 592fd624 2020-10-07 op continue;
597 592fd624 2020-10-07 op }
598 592fd624 2020-10-07 op
599 592fd624 2020-10-07 op handle(&fds[i], &clients[i]);
600 592fd624 2020-10-07 op
601 592fd624 2020-10-07 op }
602 592fd624 2020-10-07 op }
603 592fd624 2020-10-07 op }
604 592fd624 2020-10-07 op
605 592fd624 2020-10-07 op void
606 3e4749f7 2020-10-02 op usage(const char *me)
607 3e4749f7 2020-10-02 op {
608 3e4749f7 2020-10-02 op fprintf(stderr,
609 3e4749f7 2020-10-02 op "USAGE: %s [-h] [-c cert.pem] [-d docs] [-k key.pem]\n",
610 3e4749f7 2020-10-02 op me);
611 3e4749f7 2020-10-02 op }
612 3e4749f7 2020-10-02 op
613 3e4749f7 2020-10-02 op int
614 3e4749f7 2020-10-02 op main(int argc, char **argv)
615 3e4749f7 2020-10-02 op {
616 3e4749f7 2020-10-02 op const char *cert = "cert.pem", *key = "key.pem", *dir = "docs";
617 3e4749f7 2020-10-02 op struct tls *ctx = NULL;
618 3e4749f7 2020-10-02 op struct tls_config *conf;
619 3e4749f7 2020-10-02 op int sock, ch;
620 3e4749f7 2020-10-02 op
621 3e4749f7 2020-10-02 op while ((ch = getopt(argc, argv, "c:d:hk:")) != -1) {
622 3e4749f7 2020-10-02 op switch (ch) {
623 3e4749f7 2020-10-02 op case 'c':
624 3e4749f7 2020-10-02 op cert = optarg;
625 3e4749f7 2020-10-02 op break;
626 3e4749f7 2020-10-02 op
627 3e4749f7 2020-10-02 op case 'd':
628 3e4749f7 2020-10-02 op dir = optarg;
629 3e4749f7 2020-10-02 op break;
630 3e4749f7 2020-10-02 op
631 3e4749f7 2020-10-02 op case 'h':
632 3e4749f7 2020-10-02 op usage(*argv);
633 3e4749f7 2020-10-02 op return 0;
634 3e4749f7 2020-10-02 op
635 3e4749f7 2020-10-02 op case 'k':
636 3e4749f7 2020-10-02 op key = optarg;
637 3e4749f7 2020-10-02 op break;
638 3e4749f7 2020-10-02 op
639 3e4749f7 2020-10-02 op default:
640 3e4749f7 2020-10-02 op usage(*argv);
641 3e4749f7 2020-10-02 op return 1;
642 3e4749f7 2020-10-02 op }
643 3e4749f7 2020-10-02 op }
644 3e4749f7 2020-10-02 op
645 3e4749f7 2020-10-02 op if ((conf = tls_config_new()) == NULL)
646 3e4749f7 2020-10-02 op err(1, "tls_config_new");
647 0d8ca45a 2020-10-03 op
648 0d8ca45a 2020-10-03 op if (tls_config_set_protocols(conf,
649 0d8ca45a 2020-10-03 op TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3) == -1)
650 0d8ca45a 2020-10-03 op err(1, "tls_config_set_protocols");
651 3e4749f7 2020-10-02 op
652 4d4f0e19 2020-10-03 op if (tls_config_set_cert_file(conf, cert) == -1)
653 4d4f0e19 2020-10-03 op err(1, "tls_config_set_cert_file: %s", cert);
654 3e4749f7 2020-10-02 op
655 4d4f0e19 2020-10-03 op if (tls_config_set_key_file(conf, key) == -1)
656 4d4f0e19 2020-10-03 op err(1, "tls_config_set_key_file: %s", key);
657 3e4749f7 2020-10-02 op
658 3e4749f7 2020-10-02 op if ((ctx = tls_server()) == NULL)
659 3e4749f7 2020-10-02 op err(1, "tls_server");
660 3e4749f7 2020-10-02 op
661 3e4749f7 2020-10-02 op if (tls_configure(ctx, conf) == -1)
662 3e4749f7 2020-10-02 op errx(1, "tls_configure: %s", tls_error(ctx));
663 3e4749f7 2020-10-02 op
664 3e4749f7 2020-10-02 op sock = make_socket(1965);
665 3e4749f7 2020-10-02 op
666 3e4749f7 2020-10-02 op if ((dirfd = open(dir, O_RDONLY | O_DIRECTORY)) == -1)
667 3e4749f7 2020-10-02 op err(1, "open: %s", dir);
668 3e4749f7 2020-10-02 op
669 3e4749f7 2020-10-02 op if (unveil(dir, "r") == -1)
670 3e4749f7 2020-10-02 op err(1, "unveil");
671 3e4749f7 2020-10-02 op
672 3e4749f7 2020-10-02 op if (pledge("stdio rpath inet", "") == -1)
673 3e4749f7 2020-10-02 op err(1, "pledge");
674 3e4749f7 2020-10-02 op
675 3e4749f7 2020-10-02 op loop(ctx, sock);
676 3e4749f7 2020-10-02 op
677 3e4749f7 2020-10-02 op close(sock);
678 3e4749f7 2020-10-02 op tls_free(ctx);
679 3e4749f7 2020-10-02 op tls_config_free(conf);
680 3e4749f7 2020-10-02 op }