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