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