Blame


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