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