Blame


1 d3a08f4d 2021-01-17 op /*
2 d3a08f4d 2021-01-17 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 d3a08f4d 2021-01-17 op *
4 d3a08f4d 2021-01-17 op * Permission to use, copy, modify, and distribute this software for any
5 d3a08f4d 2021-01-17 op * purpose with or without fee is hereby granted, provided that the above
6 d3a08f4d 2021-01-17 op * copyright notice and this permission notice appear in all copies.
7 d3a08f4d 2021-01-17 op *
8 d3a08f4d 2021-01-17 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 d3a08f4d 2021-01-17 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 d3a08f4d 2021-01-17 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 d3a08f4d 2021-01-17 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 d3a08f4d 2021-01-17 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 d3a08f4d 2021-01-17 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 d3a08f4d 2021-01-17 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 d3a08f4d 2021-01-17 op */
16 d3a08f4d 2021-01-17 op
17 d3a08f4d 2021-01-17 op #include <sys/mman.h>
18 d3a08f4d 2021-01-17 op #include <sys/stat.h>
19 d3a08f4d 2021-01-17 op
20 d3a08f4d 2021-01-17 op #include <netdb.h>
21 d3a08f4d 2021-01-17 op
22 d3a08f4d 2021-01-17 op #include <assert.h>
23 d3a08f4d 2021-01-17 op #include <errno.h>
24 d3a08f4d 2021-01-17 op #include <fcntl.h>
25 b4d409cf 2021-01-21 op #include <fnmatch.h>
26 d3a08f4d 2021-01-17 op #include <string.h>
27 d3a08f4d 2021-01-17 op
28 d3a08f4d 2021-01-17 op #include "gmid.h"
29 d3a08f4d 2021-01-17 op
30 d3a08f4d 2021-01-17 op int connected_clients;
31 d3a08f4d 2021-01-17 op
32 c8b74339 2021-01-24 op const char *
33 c8b74339 2021-01-24 op vhost_lang(struct vhost *v, const char *path)
34 c8b74339 2021-01-24 op {
35 c8b74339 2021-01-24 op struct location *loc;
36 c8b74339 2021-01-24 op const char *lang = NULL;
37 c8b74339 2021-01-24 op
38 caad0308 2021-01-27 op if (v == NULL || path == NULL)
39 8443bff7 2021-01-25 op return lang;
40 8443bff7 2021-01-25 op
41 c8b74339 2021-01-24 op for (loc = v->locations; loc->match != NULL; ++loc) {
42 c8b74339 2021-01-24 op if (!fnmatch(loc->match, path, 0)) {
43 c8b74339 2021-01-24 op if (loc->lang != NULL)
44 c8b74339 2021-01-24 op lang = loc->lang;
45 c8b74339 2021-01-24 op }
46 c8b74339 2021-01-24 op }
47 c8b74339 2021-01-24 op
48 c8b74339 2021-01-24 op return lang;
49 c8b74339 2021-01-24 op }
50 c8b74339 2021-01-24 op
51 c8b74339 2021-01-24 op const char *
52 c8b74339 2021-01-24 op vhost_default_mime(struct vhost *v, const char *path)
53 c8b74339 2021-01-24 op {
54 c8b74339 2021-01-24 op struct location *loc;
55 c8b74339 2021-01-24 op const char *default_mime = "application/octet-stream";
56 caad0308 2021-01-27 op
57 caad0308 2021-01-27 op if (v == NULL || path == NULL)
58 caad0308 2021-01-27 op return default_mime;
59 c8b74339 2021-01-24 op
60 c8b74339 2021-01-24 op for (loc = v->locations; loc->match != NULL; ++loc) {
61 c8b74339 2021-01-24 op if (!fnmatch(loc->match, path, 0)) {
62 c8b74339 2021-01-24 op if (loc->default_mime != NULL)
63 c8b74339 2021-01-24 op default_mime = loc->default_mime;
64 c8b74339 2021-01-24 op }
65 c8b74339 2021-01-24 op }
66 c8b74339 2021-01-24 op
67 c8b74339 2021-01-24 op return default_mime;
68 c8b74339 2021-01-24 op }
69 c8b74339 2021-01-24 op
70 c8b74339 2021-01-24 op const char *
71 c8b74339 2021-01-24 op vhost_index(struct vhost *v, const char *path)
72 c8b74339 2021-01-24 op {
73 c8b74339 2021-01-24 op struct location *loc;
74 c8b74339 2021-01-24 op const char *index = "index.gmi";
75 c8b74339 2021-01-24 op
76 caad0308 2021-01-27 op if (v == NULL || path == NULL)
77 caad0308 2021-01-27 op return index;
78 caad0308 2021-01-27 op
79 c8b74339 2021-01-24 op for (loc = v->locations; loc->match != NULL; ++loc) {
80 c8b74339 2021-01-24 op if (!fnmatch(loc->match, path, 0)) {
81 c8b74339 2021-01-24 op if (loc->index != NULL)
82 c8b74339 2021-01-24 op index = loc->index;
83 c8b74339 2021-01-24 op }
84 c8b74339 2021-01-24 op }
85 c8b74339 2021-01-24 op
86 c8b74339 2021-01-24 op return index;
87 c8b74339 2021-01-24 op }
88 c8b74339 2021-01-24 op
89 d3a08f4d 2021-01-17 op int
90 252908e6 2021-01-24 op vhost_auto_index(struct vhost *v, const char *path)
91 252908e6 2021-01-24 op {
92 252908e6 2021-01-24 op struct location *loc;
93 252908e6 2021-01-24 op int auto_index = 0;
94 252908e6 2021-01-24 op
95 252908e6 2021-01-24 op for (loc = v->locations; loc->match != NULL; ++loc) {
96 252908e6 2021-01-24 op if (!fnmatch(loc->match, path, 0)) {
97 252908e6 2021-01-24 op if (loc->auto_index)
98 252908e6 2021-01-24 op auto_index = loc->auto_index;
99 252908e6 2021-01-24 op }
100 252908e6 2021-01-24 op }
101 252908e6 2021-01-24 op
102 252908e6 2021-01-24 op return auto_index == 1;
103 252908e6 2021-01-24 op }
104 252908e6 2021-01-24 op
105 252908e6 2021-01-24 op int
106 d3a08f4d 2021-01-17 op check_path(struct client *c, const char *path, int *fd)
107 d3a08f4d 2021-01-17 op {
108 d3a08f4d 2021-01-17 op struct stat sb;
109 d1ca3911 2021-01-21 op const char *p;
110 252908e6 2021-01-24 op int flags;
111 d3a08f4d 2021-01-17 op
112 d3a08f4d 2021-01-17 op assert(path != NULL);
113 d1ca3911 2021-01-21 op
114 d1ca3911 2021-01-21 op if (*path == '\0')
115 d1ca3911 2021-01-21 op p = ".";
116 d1ca3911 2021-01-21 op else if (*path == '/')
117 d1ca3911 2021-01-21 op /* in send_dir we add an initial / (to be
118 d1ca3911 2021-01-21 op * redirect-friendly), but here we want to skip it */
119 d1ca3911 2021-01-21 op p = path+1;
120 d1ca3911 2021-01-21 op else
121 d1ca3911 2021-01-21 op p = path;
122 d1ca3911 2021-01-21 op
123 252908e6 2021-01-24 op flags = O_RDONLY | O_NOFOLLOW;
124 252908e6 2021-01-24 op
125 252908e6 2021-01-24 op if (*fd == -1 && (*fd = openat(c->host->dirfd, p, flags)) == -1)
126 d3a08f4d 2021-01-17 op return FILE_MISSING;
127 d3a08f4d 2021-01-17 op
128 d3a08f4d 2021-01-17 op if (fstat(*fd, &sb) == -1) {
129 d3a08f4d 2021-01-17 op LOGN(c, "failed stat for %s: %s", path, strerror(errno));
130 d3a08f4d 2021-01-17 op return FILE_MISSING;
131 d3a08f4d 2021-01-17 op }
132 d3a08f4d 2021-01-17 op
133 d3a08f4d 2021-01-17 op if (S_ISDIR(sb.st_mode))
134 d3a08f4d 2021-01-17 op return FILE_DIRECTORY;
135 d3a08f4d 2021-01-17 op
136 d3a08f4d 2021-01-17 op if (sb.st_mode & S_IXUSR)
137 d3a08f4d 2021-01-17 op return FILE_EXECUTABLE;
138 d3a08f4d 2021-01-17 op
139 d3a08f4d 2021-01-17 op return FILE_EXISTS;
140 d3a08f4d 2021-01-17 op }
141 d3a08f4d 2021-01-17 op
142 07b0a142 2021-01-24 op void
143 0be51733 2021-01-20 op open_file(struct pollfd *fds, struct client *c)
144 d3a08f4d 2021-01-17 op {
145 0be51733 2021-01-20 op switch (check_path(c, c->iri.path, &c->fd)) {
146 d3a08f4d 2021-01-17 op case FILE_EXECUTABLE:
147 252908e6 2021-01-24 op if (starts_with(c->iri.path, c->host->cgi)) {
148 07b0a142 2021-01-24 op start_cgi(c->iri.path, "", c->iri.query, fds, c);
149 07b0a142 2021-01-24 op return;
150 07b0a142 2021-01-24 op }
151 d3a08f4d 2021-01-17 op
152 d3a08f4d 2021-01-17 op /* fallthrough */
153 d3a08f4d 2021-01-17 op
154 d3a08f4d 2021-01-17 op case FILE_EXISTS:
155 252908e6 2021-01-24 op load_file(fds, c);
156 07b0a142 2021-01-24 op return;
157 d3a08f4d 2021-01-17 op
158 d3a08f4d 2021-01-17 op case FILE_DIRECTORY:
159 252908e6 2021-01-24 op open_dir(fds, c);
160 07b0a142 2021-01-24 op return;
161 d3a08f4d 2021-01-17 op
162 d3a08f4d 2021-01-17 op case FILE_MISSING:
163 07b0a142 2021-01-24 op if (c->host->cgi != NULL && starts_with(c->iri.path, c->host->cgi)) {
164 07b0a142 2021-01-24 op check_for_cgi(c->iri.path, c->iri.query, fds, c);
165 07b0a142 2021-01-24 op return;
166 07b0a142 2021-01-24 op }
167 a87f6625 2021-01-24 op start_reply(fds, c, NOT_FOUND, "not found");
168 07b0a142 2021-01-24 op return;
169 d3a08f4d 2021-01-17 op
170 d3a08f4d 2021-01-17 op default:
171 d3a08f4d 2021-01-17 op /* unreachable */
172 d3a08f4d 2021-01-17 op abort();
173 d3a08f4d 2021-01-17 op }
174 d3a08f4d 2021-01-17 op }
175 d3a08f4d 2021-01-17 op
176 252908e6 2021-01-24 op void
177 252908e6 2021-01-24 op load_file(struct pollfd *fds, struct client *c)
178 252908e6 2021-01-24 op {
179 252908e6 2021-01-24 op if ((c->len = filesize(c->fd)) == -1) {
180 252908e6 2021-01-24 op LOGE(c, "failed to get file size for %s", c->iri.path);
181 252908e6 2021-01-24 op start_reply(fds, c, TEMP_FAILURE, "internal server error");
182 252908e6 2021-01-24 op return;
183 252908e6 2021-01-24 op }
184 d3a08f4d 2021-01-17 op
185 252908e6 2021-01-24 op if ((c->buf = mmap(NULL, c->len, PROT_READ, MAP_PRIVATE,
186 252908e6 2021-01-24 op c->fd, 0)) == MAP_FAILED) {
187 252908e6 2021-01-24 op LOGW(c, "mmap: %s: %s", c->iri.path, strerror(errno));
188 252908e6 2021-01-24 op start_reply(fds, c, TEMP_FAILURE, "internal server error");
189 252908e6 2021-01-24 op return;
190 252908e6 2021-01-24 op }
191 252908e6 2021-01-24 op c->i = c->buf;
192 252908e6 2021-01-24 op c->next = S_SENDING_FILE;
193 252908e6 2021-01-24 op start_reply(fds, c, SUCCESS, mime(c->host, c->iri.path));
194 252908e6 2021-01-24 op }
195 252908e6 2021-01-24 op
196 d3a08f4d 2021-01-17 op /*
197 d3a08f4d 2021-01-17 op * the inverse of this algorithm, i.e. starting from the start of the
198 d3a08f4d 2021-01-17 op * path + strlen(cgi), and checking if each component, should be
199 d3a08f4d 2021-01-17 op * faster. But it's tedious to write. This does the opposite: starts
200 d3a08f4d 2021-01-17 op * from the end and strip one component at a time, until either an
201 d3a08f4d 2021-01-17 op * executable is found or we emptied the path.
202 d3a08f4d 2021-01-17 op */
203 07b0a142 2021-01-24 op void
204 d3a08f4d 2021-01-17 op check_for_cgi(char *path, char *query, struct pollfd *fds, struct client *c)
205 d3a08f4d 2021-01-17 op {
206 d3a08f4d 2021-01-17 op char *end;
207 d3a08f4d 2021-01-17 op end = strchr(path, '\0');
208 d3a08f4d 2021-01-17 op
209 d3a08f4d 2021-01-17 op /* NB: assume CGI is enabled and path matches cgi */
210 d3a08f4d 2021-01-17 op
211 d3a08f4d 2021-01-17 op while (end > path) {
212 d3a08f4d 2021-01-17 op /* go up one level. UNIX paths are simple and POSIX
213 d3a08f4d 2021-01-17 op * dirname, with its ambiguities on if the given path
214 d3a08f4d 2021-01-17 op * is changed or not, gives me headaches. */
215 d3a08f4d 2021-01-17 op while (*end != '/')
216 d3a08f4d 2021-01-17 op end--;
217 d3a08f4d 2021-01-17 op *end = '\0';
218 d3a08f4d 2021-01-17 op
219 d3a08f4d 2021-01-17 op switch (check_path(c, path, &c->fd)) {
220 d3a08f4d 2021-01-17 op case FILE_EXECUTABLE:
221 07b0a142 2021-01-24 op start_cgi(path, end+1, query, fds,c);
222 07b0a142 2021-01-24 op return;
223 d3a08f4d 2021-01-17 op case FILE_MISSING:
224 d3a08f4d 2021-01-17 op break;
225 d3a08f4d 2021-01-17 op default:
226 d3a08f4d 2021-01-17 op goto err;
227 d3a08f4d 2021-01-17 op }
228 d3a08f4d 2021-01-17 op
229 d3a08f4d 2021-01-17 op *end = '/';
230 d3a08f4d 2021-01-17 op end--;
231 d3a08f4d 2021-01-17 op }
232 d3a08f4d 2021-01-17 op
233 d3a08f4d 2021-01-17 op err:
234 a87f6625 2021-01-24 op start_reply(fds, c, NOT_FOUND, "not found");
235 07b0a142 2021-01-24 op return;
236 d3a08f4d 2021-01-17 op }
237 d3a08f4d 2021-01-17 op
238 d3a08f4d 2021-01-17 op void
239 d3a08f4d 2021-01-17 op mark_nonblock(int fd)
240 d3a08f4d 2021-01-17 op {
241 d3a08f4d 2021-01-17 op int flags;
242 d3a08f4d 2021-01-17 op
243 d3a08f4d 2021-01-17 op if ((flags = fcntl(fd, F_GETFL)) == -1)
244 d3a08f4d 2021-01-17 op fatal("fcntl(F_GETFL): %s", strerror(errno));
245 d3a08f4d 2021-01-17 op if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
246 d3a08f4d 2021-01-17 op fatal("fcntl(F_SETFL): %s", strerror(errno));
247 d3a08f4d 2021-01-17 op }
248 d3a08f4d 2021-01-17 op
249 d3a08f4d 2021-01-17 op void
250 d3a08f4d 2021-01-17 op handle_handshake(struct pollfd *fds, struct client *c)
251 d3a08f4d 2021-01-17 op {
252 d3a08f4d 2021-01-17 op struct vhost *h;
253 d3a08f4d 2021-01-17 op const char *servname;
254 d3a08f4d 2021-01-17 op
255 d3a08f4d 2021-01-17 op switch (tls_handshake(c->ctx)) {
256 d3a08f4d 2021-01-17 op case 0: /* success */
257 d3a08f4d 2021-01-17 op case -1: /* already handshaked */
258 d3a08f4d 2021-01-17 op break;
259 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
260 d3a08f4d 2021-01-17 op fds->events = POLLIN;
261 d3a08f4d 2021-01-17 op return;
262 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
263 d3a08f4d 2021-01-17 op fds->events = POLLOUT;
264 d3a08f4d 2021-01-17 op return;
265 d3a08f4d 2021-01-17 op default:
266 d3a08f4d 2021-01-17 op /* unreachable */
267 d3a08f4d 2021-01-17 op abort();
268 d3a08f4d 2021-01-17 op }
269 d3a08f4d 2021-01-17 op
270 d3a08f4d 2021-01-17 op servname = tls_conn_servername(c->ctx);
271 3300cbe0 2021-01-27 op puny_decode(servname, c->domain, sizeof(c->domain));
272 d3a08f4d 2021-01-17 op
273 d3a08f4d 2021-01-17 op for (h = hosts; h->domain != NULL; ++h) {
274 3300cbe0 2021-01-27 op if (!fnmatch(h->domain, c->domain, 0))
275 ce79c944 2021-01-21 op break;
276 d3a08f4d 2021-01-17 op }
277 d3a08f4d 2021-01-17 op
278 22c6d633 2021-01-27 op LOGD(c, "handshake: SNI: \"%s\"; decoded: \"%s\"; matched: \"%s\"",
279 22c6d633 2021-01-27 op servname != NULL ? servname : "(null)",
280 22c6d633 2021-01-27 op c->domain,
281 22c6d633 2021-01-27 op h->domain != NULL ? h->domain : "(null)");
282 22c6d633 2021-01-27 op
283 d3a08f4d 2021-01-17 op if (h->domain != NULL) {
284 d3a08f4d 2021-01-17 op c->state = S_OPEN;
285 d3a08f4d 2021-01-17 op c->host = h;
286 d3a08f4d 2021-01-17 op handle_open_conn(fds, c);
287 d3a08f4d 2021-01-17 op return;
288 d3a08f4d 2021-01-17 op }
289 d3a08f4d 2021-01-17 op
290 0ab65593 2021-01-21 op if (servname != NULL)
291 0ab65593 2021-01-21 op strncpy(c->req, servname, sizeof(c->req));
292 0ab65593 2021-01-21 op else
293 0ab65593 2021-01-21 op strncpy(c->req, "null", sizeof(c->req));
294 0ab65593 2021-01-21 op
295 a87f6625 2021-01-24 op start_reply(fds, c, BAD_REQUEST, "Wrong host or missing SNI");
296 d3a08f4d 2021-01-17 op }
297 d3a08f4d 2021-01-17 op
298 d3a08f4d 2021-01-17 op void
299 d3a08f4d 2021-01-17 op handle_open_conn(struct pollfd *fds, struct client *c)
300 d3a08f4d 2021-01-17 op {
301 d3a08f4d 2021-01-17 op const char *parse_err = "invalid request";
302 3300cbe0 2021-01-27 op char decoded[DOMAIN_NAME_LEN];
303 d3a08f4d 2021-01-17 op
304 0be51733 2021-01-20 op bzero(c->req, sizeof(c->req));
305 0be51733 2021-01-20 op bzero(&c->iri, sizeof(c->iri));
306 d3a08f4d 2021-01-17 op
307 0be51733 2021-01-20 op switch (tls_read(c->ctx, c->req, sizeof(c->req)-1)) {
308 d3a08f4d 2021-01-17 op case -1:
309 d3a08f4d 2021-01-17 op LOGE(c, "tls_read: %s", tls_error(c->ctx));
310 36162ed8 2021-01-22 op close_conn(fds, c);
311 d3a08f4d 2021-01-17 op return;
312 d3a08f4d 2021-01-17 op
313 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
314 d3a08f4d 2021-01-17 op fds->events = POLLIN;
315 d3a08f4d 2021-01-17 op return;
316 d3a08f4d 2021-01-17 op
317 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
318 d3a08f4d 2021-01-17 op fds->events = POLLOUT;
319 d3a08f4d 2021-01-17 op return;
320 d3a08f4d 2021-01-17 op }
321 d3a08f4d 2021-01-17 op
322 c4f682f8 2021-01-27 op if (!trim_req_iri(c->req, &parse_err)
323 c4f682f8 2021-01-27 op || !parse_iri(c->req, &c->iri, &parse_err)) {
324 c4f682f8 2021-01-27 op LOGI(c, "iri parse error: %s", parse_err);
325 c4f682f8 2021-01-27 op start_reply(fds, c, BAD_REQUEST, "invalid request");
326 d3a08f4d 2021-01-17 op return;
327 d3a08f4d 2021-01-17 op }
328 d3a08f4d 2021-01-17 op
329 3300cbe0 2021-01-27 op puny_decode(c->iri.host, decoded, sizeof(decoded));
330 3300cbe0 2021-01-27 op
331 3300cbe0 2021-01-27 op if (c->iri.port_no != conf.port
332 3300cbe0 2021-01-27 op || strcmp(c->iri.schema, "gemini")
333 3300cbe0 2021-01-27 op || strcmp(decoded, c->domain)) {
334 a87f6625 2021-01-24 op start_reply(fds, c, PROXY_REFUSED, "won't proxy request");
335 d3a08f4d 2021-01-17 op return;
336 d3a08f4d 2021-01-17 op }
337 d3a08f4d 2021-01-17 op
338 0be51733 2021-01-20 op open_file(fds, c);
339 d3a08f4d 2021-01-17 op }
340 d3a08f4d 2021-01-17 op
341 a87f6625 2021-01-24 op void
342 df79b4c1 2021-01-19 op start_reply(struct pollfd *pfd, struct client *c, int code, const char *meta)
343 d3a08f4d 2021-01-17 op {
344 05c23a54 2021-01-19 op char buf[1030]; /* status + ' ' + max reply len + \r\n\0 */
345 c8b74339 2021-01-24 op const char *lang;
346 0be51733 2021-01-20 op size_t len;
347 d3a08f4d 2021-01-17 op
348 df79b4c1 2021-01-19 op c->code = code;
349 df79b4c1 2021-01-19 op c->meta = meta;
350 df79b4c1 2021-01-19 op c->state = S_INITIALIZING;
351 d3a08f4d 2021-01-17 op
352 c8b74339 2021-01-24 op lang = vhost_lang(c->host, c->iri.path);
353 c8b74339 2021-01-24 op
354 05c23a54 2021-01-19 op snprintf(buf, sizeof(buf), "%d ", code);
355 df79b4c1 2021-01-19 op strlcat(buf, meta, sizeof(buf));
356 c8b74339 2021-01-24 op if (!strcmp(meta, "text/gemini") && lang != NULL) {
357 05c23a54 2021-01-19 op strlcat(buf, "; lang=", sizeof(buf));
358 c8b74339 2021-01-24 op strlcat(buf, lang, sizeof(buf));
359 05c23a54 2021-01-19 op }
360 05c23a54 2021-01-19 op
361 05c23a54 2021-01-19 op len = strlcat(buf, "\r\n", sizeof(buf));
362 0be51733 2021-01-20 op assert(len < sizeof(buf));
363 d3a08f4d 2021-01-17 op
364 df79b4c1 2021-01-19 op switch (tls_write(c->ctx, buf, len)) {
365 a87f6625 2021-01-24 op case -1:
366 a87f6625 2021-01-24 op close_conn(pfd, c);
367 a87f6625 2021-01-24 op return;
368 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
369 d3a08f4d 2021-01-17 op pfd->events = POLLIN;
370 a87f6625 2021-01-24 op return;
371 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
372 d3a08f4d 2021-01-17 op pfd->events = POLLOUT;
373 a87f6625 2021-01-24 op return;
374 d3a08f4d 2021-01-17 op }
375 a87f6625 2021-01-24 op
376 a87f6625 2021-01-24 op log_request(c, buf, sizeof(buf));
377 a87f6625 2021-01-24 op
378 a87f6625 2021-01-24 op /* we don't need a body */
379 a87f6625 2021-01-24 op if (c->code != SUCCESS) {
380 a87f6625 2021-01-24 op close_conn(pfd, c);
381 a87f6625 2021-01-24 op return;
382 a87f6625 2021-01-24 op }
383 a87f6625 2021-01-24 op
384 a87f6625 2021-01-24 op /* advance the state machine */
385 a87f6625 2021-01-24 op c->state = c->next;
386 a87f6625 2021-01-24 op handle(pfd, c);
387 d3a08f4d 2021-01-17 op }
388 d3a08f4d 2021-01-17 op
389 07b0a142 2021-01-24 op void
390 d3a08f4d 2021-01-17 op start_cgi(const char *spath, const char *relpath, const char *query,
391 d3a08f4d 2021-01-17 op struct pollfd *fds, struct client *c)
392 d3a08f4d 2021-01-17 op {
393 d3a08f4d 2021-01-17 op char addr[NI_MAXHOST];
394 d3a08f4d 2021-01-17 op const char *ruser, *cissuer, *chash;
395 d3a08f4d 2021-01-17 op int e;
396 d3a08f4d 2021-01-17 op
397 d3a08f4d 2021-01-17 op e = getnameinfo((struct sockaddr*)&c->addr, sizeof(c->addr),
398 d3a08f4d 2021-01-17 op addr, sizeof(addr),
399 d3a08f4d 2021-01-17 op NULL, 0,
400 d3a08f4d 2021-01-17 op NI_NUMERICHOST);
401 d3a08f4d 2021-01-17 op if (e != 0)
402 d3a08f4d 2021-01-17 op goto err;
403 d3a08f4d 2021-01-17 op
404 d3a08f4d 2021-01-17 op if (tls_peer_cert_provided(c->ctx)) {
405 d3a08f4d 2021-01-17 op ruser = tls_peer_cert_subject(c->ctx);
406 d3a08f4d 2021-01-17 op cissuer = tls_peer_cert_issuer(c->ctx);
407 d3a08f4d 2021-01-17 op chash = tls_peer_cert_hash(c->ctx);
408 d3a08f4d 2021-01-17 op } else {
409 d3a08f4d 2021-01-17 op ruser = NULL;
410 d3a08f4d 2021-01-17 op cissuer = NULL;
411 d3a08f4d 2021-01-17 op chash = NULL;
412 d3a08f4d 2021-01-17 op }
413 d3a08f4d 2021-01-17 op
414 d3a08f4d 2021-01-17 op if (!send_string(exfd, spath)
415 d3a08f4d 2021-01-17 op || !send_string(exfd, relpath)
416 d3a08f4d 2021-01-17 op || !send_string(exfd, query)
417 d3a08f4d 2021-01-17 op || !send_string(exfd, addr)
418 d3a08f4d 2021-01-17 op || !send_string(exfd, ruser)
419 d3a08f4d 2021-01-17 op || !send_string(exfd, cissuer)
420 d3a08f4d 2021-01-17 op || !send_string(exfd, chash)
421 d3a08f4d 2021-01-17 op || !send_vhost(exfd, c->host))
422 d3a08f4d 2021-01-17 op goto err;
423 d3a08f4d 2021-01-17 op
424 d3a08f4d 2021-01-17 op close(c->fd);
425 d3a08f4d 2021-01-17 op if ((c->fd = recv_fd(exfd)) == -1) {
426 a87f6625 2021-01-24 op start_reply(fds, c, TEMP_FAILURE, "internal server error");
427 07b0a142 2021-01-24 op return;
428 d3a08f4d 2021-01-17 op }
429 a87f6625 2021-01-24 op c->state = S_SENDING_CGI;
430 d3a08f4d 2021-01-17 op cgi_poll_on_child(fds, c);
431 0be51733 2021-01-20 op c->code = -1;
432 d3a08f4d 2021-01-17 op /* handle_cgi(fds, c); */
433 07b0a142 2021-01-24 op return;
434 d3a08f4d 2021-01-17 op
435 d3a08f4d 2021-01-17 op err:
436 d3a08f4d 2021-01-17 op /* fatal("cannot talk to the executor process: %s", strerror(errno)); */
437 132cae8c 2021-01-18 op fatal("cannot talk to the executor process");
438 d3a08f4d 2021-01-17 op }
439 d3a08f4d 2021-01-17 op
440 d3a08f4d 2021-01-17 op void
441 0be51733 2021-01-20 op send_file(struct pollfd *fds, struct client *c)
442 d3a08f4d 2021-01-17 op {
443 d3a08f4d 2021-01-17 op ssize_t ret, len;
444 d3a08f4d 2021-01-17 op
445 06f233ad 2021-01-21 op /* ensure the correct state */
446 a87f6625 2021-01-24 op c->state = S_SENDING_FILE;
447 06f233ad 2021-01-21 op
448 d3a08f4d 2021-01-17 op len = (c->buf + c->len) - c->i;
449 d3a08f4d 2021-01-17 op
450 d3a08f4d 2021-01-17 op while (len > 0) {
451 d3a08f4d 2021-01-17 op switch (ret = tls_write(c->ctx, c->i, len)) {
452 d3a08f4d 2021-01-17 op case -1:
453 d3a08f4d 2021-01-17 op LOGE(c, "tls_write: %s", tls_error(c->ctx));
454 36162ed8 2021-01-22 op close_conn(fds, c);
455 d3a08f4d 2021-01-17 op return;
456 d3a08f4d 2021-01-17 op
457 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
458 d3a08f4d 2021-01-17 op fds->events = POLLIN;
459 d3a08f4d 2021-01-17 op return;
460 d3a08f4d 2021-01-17 op
461 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
462 d3a08f4d 2021-01-17 op fds->events = POLLOUT;
463 d3a08f4d 2021-01-17 op return;
464 d3a08f4d 2021-01-17 op
465 d3a08f4d 2021-01-17 op default:
466 d3a08f4d 2021-01-17 op c->i += ret;
467 d3a08f4d 2021-01-17 op len -= ret;
468 d3a08f4d 2021-01-17 op break;
469 d3a08f4d 2021-01-17 op }
470 d3a08f4d 2021-01-17 op }
471 d3a08f4d 2021-01-17 op
472 36162ed8 2021-01-22 op close_conn(fds, c);
473 d3a08f4d 2021-01-17 op }
474 d3a08f4d 2021-01-17 op
475 d3a08f4d 2021-01-17 op void
476 252908e6 2021-01-24 op open_dir(struct pollfd *fds, struct client *c)
477 d3a08f4d 2021-01-17 op {
478 d3a08f4d 2021-01-17 op size_t len;
479 252908e6 2021-01-24 op int dirfd;
480 252908e6 2021-01-24 op char *before_file;
481 d1ca3911 2021-01-21 op
482 0be51733 2021-01-20 op len = strlen(c->iri.path);
483 252908e6 2021-01-24 op if (len > 0 && !ends_with(c->iri.path, "/")) {
484 252908e6 2021-01-24 op redirect_canonical_dir(fds, c);
485 0be51733 2021-01-20 op return;
486 d3a08f4d 2021-01-17 op }
487 d3a08f4d 2021-01-17 op
488 252908e6 2021-01-24 op strlcpy(c->sbuf, "/", sizeof(c->sbuf));
489 d1ca3911 2021-01-21 op strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
490 d1ca3911 2021-01-21 op if (!ends_with(c->sbuf, "/"))
491 0be51733 2021-01-20 op strlcat(c->sbuf, "/", sizeof(c->sbuf));
492 252908e6 2021-01-24 op before_file = strchr(c->sbuf, '\0');
493 c8b74339 2021-01-24 op len = strlcat(c->sbuf, vhost_index(c->host, c->iri.path),
494 c8b74339 2021-01-24 op sizeof(c->sbuf));
495 252908e6 2021-01-24 op if (len >= sizeof(c->sbuf)) {
496 252908e6 2021-01-24 op start_reply(fds, c, TEMP_FAILURE, "internal server error");
497 252908e6 2021-01-24 op return;
498 252908e6 2021-01-24 op }
499 d3a08f4d 2021-01-17 op
500 252908e6 2021-01-24 op c->iri.path = c->sbuf;
501 252908e6 2021-01-24 op
502 252908e6 2021-01-24 op /* close later unless we have to generate the dir listing */
503 252908e6 2021-01-24 op dirfd = c->fd;
504 252908e6 2021-01-24 op c->fd = -1;
505 252908e6 2021-01-24 op
506 252908e6 2021-01-24 op switch (check_path(c, c->iri.path, &c->fd)) {
507 252908e6 2021-01-24 op case FILE_EXECUTABLE:
508 252908e6 2021-01-24 op if (starts_with(c->iri.path, c->host->cgi)) {
509 252908e6 2021-01-24 op start_cgi(c->iri.path, "", c->iri.query, fds, c);
510 252908e6 2021-01-24 op break;
511 252908e6 2021-01-24 op }
512 252908e6 2021-01-24 op
513 252908e6 2021-01-24 op /* fallthrough */
514 252908e6 2021-01-24 op
515 252908e6 2021-01-24 op case FILE_EXISTS:
516 252908e6 2021-01-24 op load_file(fds, c);
517 252908e6 2021-01-24 op break;
518 252908e6 2021-01-24 op
519 252908e6 2021-01-24 op case FILE_DIRECTORY:
520 252908e6 2021-01-24 op start_reply(fds, c, TEMP_REDIRECT, c->sbuf);
521 252908e6 2021-01-24 op break;
522 252908e6 2021-01-24 op
523 252908e6 2021-01-24 op case FILE_MISSING:
524 252908e6 2021-01-24 op *before_file = '\0';
525 252908e6 2021-01-24 op
526 252908e6 2021-01-24 op if (!vhost_auto_index(c->host, c->iri.path)) {
527 252908e6 2021-01-24 op start_reply(fds, c, NOT_FOUND, "not found");
528 252908e6 2021-01-24 op break;
529 252908e6 2021-01-24 op }
530 252908e6 2021-01-24 op
531 252908e6 2021-01-24 op c->fd = dirfd;
532 252908e6 2021-01-24 op c->next = S_SENDING_DIR;
533 252908e6 2021-01-24 op
534 252908e6 2021-01-24 op if ((c->dir = fdopendir(c->fd)) == NULL) {
535 252908e6 2021-01-24 op LOGE(c, "can't fdopendir(%d) (vhost:%s) %s: %s",
536 252908e6 2021-01-24 op c->fd, c->host->domain, c->iri.path, strerror(errno));
537 252908e6 2021-01-24 op start_reply(fds, c, TEMP_FAILURE, "internal server error");
538 252908e6 2021-01-24 op return;
539 252908e6 2021-01-24 op }
540 252908e6 2021-01-24 op c->off = 0;
541 252908e6 2021-01-24 op
542 252908e6 2021-01-24 op start_reply(fds, c, SUCCESS, "text/gemini");
543 252908e6 2021-01-24 op return;
544 252908e6 2021-01-24 op
545 252908e6 2021-01-24 op default:
546 252908e6 2021-01-24 op /* unreachable */
547 252908e6 2021-01-24 op abort();
548 252908e6 2021-01-24 op }
549 252908e6 2021-01-24 op
550 252908e6 2021-01-24 op close(dirfd);
551 252908e6 2021-01-24 op }
552 252908e6 2021-01-24 op
553 252908e6 2021-01-24 op void
554 252908e6 2021-01-24 op redirect_canonical_dir(struct pollfd *fds, struct client *c)
555 252908e6 2021-01-24 op {
556 252908e6 2021-01-24 op size_t len;
557 252908e6 2021-01-24 op
558 252908e6 2021-01-24 op strlcpy(c->sbuf, "/", sizeof(c->sbuf));
559 252908e6 2021-01-24 op strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
560 252908e6 2021-01-24 op len = strlcat(c->sbuf, "/", sizeof(c->sbuf));
561 252908e6 2021-01-24 op
562 0be51733 2021-01-20 op if (len >= sizeof(c->sbuf)) {
563 a87f6625 2021-01-24 op start_reply(fds, c, TEMP_FAILURE, "internal server error");
564 0be51733 2021-01-20 op return;
565 0be51733 2021-01-20 op }
566 0be51733 2021-01-20 op
567 252908e6 2021-01-24 op start_reply(fds, c, TEMP_REDIRECT, c->sbuf);
568 d3a08f4d 2021-01-17 op }
569 d3a08f4d 2021-01-17 op
570 252908e6 2021-01-24 op int
571 252908e6 2021-01-24 op read_next_dir_entry(struct client *c)
572 252908e6 2021-01-24 op {
573 252908e6 2021-01-24 op struct dirent *d;
574 252908e6 2021-01-24 op
575 252908e6 2021-01-24 op do {
576 252908e6 2021-01-24 op errno = 0;
577 252908e6 2021-01-24 op if ((d = readdir(c->dir)) == NULL) {
578 252908e6 2021-01-24 op if (errno != 0)
579 252908e6 2021-01-24 op LOGE(c, "readdir: %s", strerror(errno));
580 252908e6 2021-01-24 op return 0;
581 252908e6 2021-01-24 op }
582 252908e6 2021-01-24 op } while (!strcmp(d->d_name, "."));
583 252908e6 2021-01-24 op
584 252908e6 2021-01-24 op /* XXX: url escape */
585 252908e6 2021-01-24 op snprintf(c->sbuf, sizeof(c->sbuf), "=> %s %s\n",
586 252908e6 2021-01-24 op d->d_name, d->d_name);
587 252908e6 2021-01-24 op c->len = strlen(c->sbuf);
588 252908e6 2021-01-24 op c->off = 0;
589 252908e6 2021-01-24 op
590 252908e6 2021-01-24 op return 1;
591 252908e6 2021-01-24 op }
592 252908e6 2021-01-24 op
593 252908e6 2021-01-24 op void
594 252908e6 2021-01-24 op send_directory_listing(struct pollfd *fds, struct client *c)
595 252908e6 2021-01-24 op {
596 252908e6 2021-01-24 op ssize_t r;
597 252908e6 2021-01-24 op
598 252908e6 2021-01-24 op while (1) {
599 252908e6 2021-01-24 op if (c->len == 0) {
600 252908e6 2021-01-24 op if (!read_next_dir_entry(c))
601 252908e6 2021-01-24 op goto end;
602 252908e6 2021-01-24 op }
603 252908e6 2021-01-24 op
604 252908e6 2021-01-24 op while (c->len > 0) {
605 252908e6 2021-01-24 op switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
606 252908e6 2021-01-24 op case -1:
607 252908e6 2021-01-24 op goto end;
608 252908e6 2021-01-24 op
609 252908e6 2021-01-24 op case TLS_WANT_POLLOUT:
610 252908e6 2021-01-24 op fds->events = POLLOUT;
611 252908e6 2021-01-24 op return;
612 252908e6 2021-01-24 op
613 252908e6 2021-01-24 op case TLS_WANT_POLLIN:
614 252908e6 2021-01-24 op fds->events = POLLIN;
615 252908e6 2021-01-24 op return;
616 252908e6 2021-01-24 op
617 252908e6 2021-01-24 op default:
618 252908e6 2021-01-24 op c->off += r;
619 252908e6 2021-01-24 op c->len -= r;
620 252908e6 2021-01-24 op break;
621 252908e6 2021-01-24 op }
622 252908e6 2021-01-24 op }
623 252908e6 2021-01-24 op }
624 252908e6 2021-01-24 op
625 252908e6 2021-01-24 op end:
626 252908e6 2021-01-24 op close_conn(fds, c);
627 252908e6 2021-01-24 op }
628 252908e6 2021-01-24 op
629 d3a08f4d 2021-01-17 op void
630 d3a08f4d 2021-01-17 op cgi_poll_on_child(struct pollfd *fds, struct client *c)
631 d3a08f4d 2021-01-17 op {
632 d3a08f4d 2021-01-17 op int fd;
633 d3a08f4d 2021-01-17 op
634 d3a08f4d 2021-01-17 op if (c->waiting_on_child)
635 d3a08f4d 2021-01-17 op return;
636 d3a08f4d 2021-01-17 op c->waiting_on_child = 1;
637 d3a08f4d 2021-01-17 op
638 d3a08f4d 2021-01-17 op fds->events = POLLIN;
639 d3a08f4d 2021-01-17 op
640 d3a08f4d 2021-01-17 op fd = fds->fd;
641 d3a08f4d 2021-01-17 op fds->fd = c->fd;
642 d3a08f4d 2021-01-17 op c->fd = fd;
643 d3a08f4d 2021-01-17 op }
644 d3a08f4d 2021-01-17 op
645 d3a08f4d 2021-01-17 op void
646 d3a08f4d 2021-01-17 op cgi_poll_on_client(struct pollfd *fds, struct client *c)
647 d3a08f4d 2021-01-17 op {
648 d3a08f4d 2021-01-17 op int fd;
649 d3a08f4d 2021-01-17 op
650 d3a08f4d 2021-01-17 op if (!c->waiting_on_child)
651 d3a08f4d 2021-01-17 op return;
652 d3a08f4d 2021-01-17 op c->waiting_on_child = 0;
653 d3a08f4d 2021-01-17 op
654 d3a08f4d 2021-01-17 op fd = fds->fd;
655 d3a08f4d 2021-01-17 op fds->fd = c->fd;
656 d3a08f4d 2021-01-17 op c->fd = fd;
657 d3a08f4d 2021-01-17 op }
658 3309ef97 2021-01-23 op
659 3309ef97 2021-01-23 op /* handle the read from the child process. Return like read(2) */
660 3309ef97 2021-01-23 op static ssize_t
661 3309ef97 2021-01-23 op read_from_cgi(struct client *c)
662 3309ef97 2021-01-23 op {
663 3309ef97 2021-01-23 op void *buf;
664 3309ef97 2021-01-23 op size_t len;
665 3309ef97 2021-01-23 op ssize_t r;
666 3309ef97 2021-01-23 op
667 3309ef97 2021-01-23 op /* if we haven't read a whole response line, we want to
668 3309ef97 2021-01-23 op * continue reading. */
669 3309ef97 2021-01-23 op
670 3309ef97 2021-01-23 op if (c->code == -1) {
671 3309ef97 2021-01-23 op buf = c->sbuf + c->len;
672 3309ef97 2021-01-23 op len = sizeof(c->sbuf) - c->len;
673 3309ef97 2021-01-23 op } else {
674 3309ef97 2021-01-23 op buf = c->sbuf;
675 3309ef97 2021-01-23 op len = sizeof(c->sbuf);
676 3309ef97 2021-01-23 op }
677 d3a08f4d 2021-01-17 op
678 3309ef97 2021-01-23 op r = read(c->fd, buf, len);
679 3309ef97 2021-01-23 op if (r == 0 || r == -1)
680 3309ef97 2021-01-23 op return r;
681 3309ef97 2021-01-23 op
682 3309ef97 2021-01-23 op c->len += r;
683 3309ef97 2021-01-23 op c->off = 0;
684 3309ef97 2021-01-23 op
685 3309ef97 2021-01-23 op if (c->code != -1)
686 3309ef97 2021-01-23 op return r;
687 3309ef97 2021-01-23 op
688 3309ef97 2021-01-23 op if (strchr(c->sbuf, '\n') || c->len == sizeof(c->sbuf)) {
689 3309ef97 2021-01-23 op c->code = 0;
690 3309ef97 2021-01-23 op log_request(c, c->sbuf, c->len);
691 3309ef97 2021-01-23 op }
692 3309ef97 2021-01-23 op
693 3309ef97 2021-01-23 op return r;
694 3309ef97 2021-01-23 op }
695 3309ef97 2021-01-23 op
696 d3a08f4d 2021-01-17 op void
697 d3a08f4d 2021-01-17 op handle_cgi(struct pollfd *fds, struct client *c)
698 d3a08f4d 2021-01-17 op {
699 d3a08f4d 2021-01-17 op ssize_t r;
700 d3a08f4d 2021-01-17 op
701 d3a08f4d 2021-01-17 op /* ensure c->fd is the child and fds->fd the client */
702 d3a08f4d 2021-01-17 op cgi_poll_on_client(fds, c);
703 d3a08f4d 2021-01-17 op
704 d3a08f4d 2021-01-17 op while (1) {
705 3309ef97 2021-01-23 op if (c->code == -1 || c->len == 0) {
706 3309ef97 2021-01-23 op switch (r = read_from_cgi(c)) {
707 3309ef97 2021-01-23 op case 0:
708 d3a08f4d 2021-01-17 op goto end;
709 3309ef97 2021-01-23 op
710 3309ef97 2021-01-23 op case -1:
711 d3a08f4d 2021-01-17 op if (errno == EAGAIN || errno == EWOULDBLOCK) {
712 d3a08f4d 2021-01-17 op cgi_poll_on_child(fds, c);
713 d3a08f4d 2021-01-17 op return;
714 d3a08f4d 2021-01-17 op }
715 3309ef97 2021-01-23 op goto end;
716 d3a08f4d 2021-01-17 op }
717 3309ef97 2021-01-23 op }
718 0be51733 2021-01-20 op
719 3309ef97 2021-01-23 op if (c->code == -1) {
720 3309ef97 2021-01-23 op cgi_poll_on_child(fds, c);
721 3309ef97 2021-01-23 op return;
722 d3a08f4d 2021-01-17 op }
723 d3a08f4d 2021-01-17 op
724 d3a08f4d 2021-01-17 op while (c->len > 0) {
725 d3a08f4d 2021-01-17 op switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
726 d3a08f4d 2021-01-17 op case -1:
727 d3a08f4d 2021-01-17 op goto end;
728 d3a08f4d 2021-01-17 op
729 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
730 d3a08f4d 2021-01-17 op fds->events = POLLOUT;
731 d3a08f4d 2021-01-17 op return;
732 d3a08f4d 2021-01-17 op
733 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
734 d3a08f4d 2021-01-17 op fds->events = POLLIN;
735 d3a08f4d 2021-01-17 op return;
736 d3a08f4d 2021-01-17 op
737 d3a08f4d 2021-01-17 op default:
738 d3a08f4d 2021-01-17 op c->off += r;
739 d3a08f4d 2021-01-17 op c->len -= r;
740 d3a08f4d 2021-01-17 op break;
741 d3a08f4d 2021-01-17 op }
742 d3a08f4d 2021-01-17 op }
743 d3a08f4d 2021-01-17 op }
744 d3a08f4d 2021-01-17 op
745 d3a08f4d 2021-01-17 op end:
746 36162ed8 2021-01-22 op close_conn(fds, c);
747 d3a08f4d 2021-01-17 op }
748 d3a08f4d 2021-01-17 op
749 d3a08f4d 2021-01-17 op void
750 36162ed8 2021-01-22 op close_conn(struct pollfd *pfd, struct client *c)
751 d3a08f4d 2021-01-17 op {
752 d3a08f4d 2021-01-17 op c->state = S_CLOSING;
753 d3a08f4d 2021-01-17 op
754 d3a08f4d 2021-01-17 op switch (tls_close(c->ctx)) {
755 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
756 d3a08f4d 2021-01-17 op pfd->events = POLLIN;
757 d3a08f4d 2021-01-17 op return;
758 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
759 d3a08f4d 2021-01-17 op pfd->events = POLLOUT;
760 d3a08f4d 2021-01-17 op return;
761 d3a08f4d 2021-01-17 op }
762 d3a08f4d 2021-01-17 op
763 d3a08f4d 2021-01-17 op connected_clients--;
764 d3a08f4d 2021-01-17 op
765 d3a08f4d 2021-01-17 op tls_free(c->ctx);
766 d3a08f4d 2021-01-17 op c->ctx = NULL;
767 d3a08f4d 2021-01-17 op
768 d3a08f4d 2021-01-17 op if (c->buf != MAP_FAILED)
769 d3a08f4d 2021-01-17 op munmap(c->buf, c->len);
770 d3a08f4d 2021-01-17 op
771 d3a08f4d 2021-01-17 op if (c->fd != -1)
772 d3a08f4d 2021-01-17 op close(c->fd);
773 d3a08f4d 2021-01-17 op
774 252908e6 2021-01-24 op if (c->dir != NULL)
775 252908e6 2021-01-24 op closedir(c->dir);
776 252908e6 2021-01-24 op
777 d3a08f4d 2021-01-17 op close(pfd->fd);
778 d3a08f4d 2021-01-17 op pfd->fd = -1;
779 f890c8c5 2021-01-22 op }
780 f890c8c5 2021-01-22 op
781 f890c8c5 2021-01-22 op void
782 d3a08f4d 2021-01-17 op do_accept(int sock, struct tls *ctx, struct pollfd *fds, struct client *clients)
783 d3a08f4d 2021-01-17 op {
784 d3a08f4d 2021-01-17 op int i, fd;
785 d3a08f4d 2021-01-17 op struct sockaddr_storage addr;
786 d3a08f4d 2021-01-17 op socklen_t len;
787 d3a08f4d 2021-01-17 op
788 d3a08f4d 2021-01-17 op len = sizeof(addr);
789 d3a08f4d 2021-01-17 op if ((fd = accept(sock, (struct sockaddr*)&addr, &len)) == -1) {
790 d3a08f4d 2021-01-17 op if (errno == EWOULDBLOCK)
791 d3a08f4d 2021-01-17 op return;
792 d3a08f4d 2021-01-17 op fatal("accept: %s", strerror(errno));
793 d3a08f4d 2021-01-17 op }
794 d3a08f4d 2021-01-17 op
795 d3a08f4d 2021-01-17 op mark_nonblock(fd);
796 d3a08f4d 2021-01-17 op
797 d3a08f4d 2021-01-17 op for (i = 0; i < MAX_USERS; ++i) {
798 d3a08f4d 2021-01-17 op if (fds[i].fd == -1) {
799 d3a08f4d 2021-01-17 op bzero(&clients[i], sizeof(struct client));
800 d3a08f4d 2021-01-17 op if (tls_accept_socket(ctx, &clients[i].ctx, fd) == -1)
801 d3a08f4d 2021-01-17 op break; /* goodbye fd! */
802 d3a08f4d 2021-01-17 op
803 d3a08f4d 2021-01-17 op fds[i].fd = fd;
804 d3a08f4d 2021-01-17 op fds[i].events = POLLIN;
805 d3a08f4d 2021-01-17 op
806 d3a08f4d 2021-01-17 op clients[i].state = S_HANDSHAKE;
807 a87f6625 2021-01-24 op clients[i].next = S_SENDING_FILE;
808 d3a08f4d 2021-01-17 op clients[i].fd = -1;
809 d3a08f4d 2021-01-17 op clients[i].waiting_on_child = 0;
810 d3a08f4d 2021-01-17 op clients[i].buf = MAP_FAILED;
811 252908e6 2021-01-24 op clients[i].dir = NULL;
812 d3a08f4d 2021-01-17 op clients[i].addr = addr;
813 d3a08f4d 2021-01-17 op
814 d3a08f4d 2021-01-17 op connected_clients++;
815 d3a08f4d 2021-01-17 op return;
816 d3a08f4d 2021-01-17 op }
817 d3a08f4d 2021-01-17 op }
818 d3a08f4d 2021-01-17 op
819 d3a08f4d 2021-01-17 op close(fd);
820 d3a08f4d 2021-01-17 op }
821 d3a08f4d 2021-01-17 op
822 d3a08f4d 2021-01-17 op void
823 d3a08f4d 2021-01-17 op handle(struct pollfd *fds, struct client *client)
824 d3a08f4d 2021-01-17 op {
825 d3a08f4d 2021-01-17 op switch (client->state) {
826 d3a08f4d 2021-01-17 op case S_HANDSHAKE:
827 d3a08f4d 2021-01-17 op handle_handshake(fds, client);
828 d3a08f4d 2021-01-17 op break;
829 d3a08f4d 2021-01-17 op
830 d3a08f4d 2021-01-17 op case S_OPEN:
831 d3a08f4d 2021-01-17 op handle_open_conn(fds, client);
832 d3a08f4d 2021-01-17 op break;
833 d3a08f4d 2021-01-17 op
834 d3a08f4d 2021-01-17 op case S_INITIALIZING:
835 a87f6625 2021-01-24 op start_reply(fds, client, client->code, client->meta);
836 a87f6625 2021-01-24 op break;
837 a87f6625 2021-01-24 op
838 a87f6625 2021-01-24 op case S_SENDING_FILE:
839 a87f6625 2021-01-24 op send_file(fds, client);
840 a87f6625 2021-01-24 op break;
841 d3a08f4d 2021-01-17 op
842 252908e6 2021-01-24 op case S_SENDING_DIR:
843 252908e6 2021-01-24 op send_directory_listing(fds, client);
844 252908e6 2021-01-24 op break;
845 252908e6 2021-01-24 op
846 a87f6625 2021-01-24 op case S_SENDING_CGI:
847 a87f6625 2021-01-24 op handle_cgi(fds, client);
848 d3a08f4d 2021-01-17 op break;
849 d3a08f4d 2021-01-17 op
850 d3a08f4d 2021-01-17 op case S_CLOSING:
851 36162ed8 2021-01-22 op close_conn(fds, client);
852 d3a08f4d 2021-01-17 op break;
853 d3a08f4d 2021-01-17 op
854 d3a08f4d 2021-01-17 op default:
855 d3a08f4d 2021-01-17 op /* unreachable */
856 d3a08f4d 2021-01-17 op abort();
857 d3a08f4d 2021-01-17 op }
858 d3a08f4d 2021-01-17 op }
859 d3a08f4d 2021-01-17 op
860 d3a08f4d 2021-01-17 op void
861 d3a08f4d 2021-01-17 op loop(struct tls *ctx, int sock4, int sock6)
862 d3a08f4d 2021-01-17 op {
863 d3a08f4d 2021-01-17 op int i;
864 d3a08f4d 2021-01-17 op struct client clients[MAX_USERS];
865 d3a08f4d 2021-01-17 op struct pollfd fds[MAX_USERS];
866 d3a08f4d 2021-01-17 op
867 d3a08f4d 2021-01-17 op connected_clients = 0;
868 d3a08f4d 2021-01-17 op
869 d3a08f4d 2021-01-17 op for (i = 0; i < MAX_USERS; ++i) {
870 d3a08f4d 2021-01-17 op fds[i].fd = -1;
871 d3a08f4d 2021-01-17 op fds[i].events = POLLIN;
872 d3a08f4d 2021-01-17 op bzero(&clients[i], sizeof(struct client));
873 d3a08f4d 2021-01-17 op }
874 d3a08f4d 2021-01-17 op
875 d3a08f4d 2021-01-17 op fds[0].fd = sock4;
876 d3a08f4d 2021-01-17 op fds[1].fd = sock6;
877 d3a08f4d 2021-01-17 op
878 d3a08f4d 2021-01-17 op for (;;) {
879 d3a08f4d 2021-01-17 op if (poll(fds, MAX_USERS, INFTIM) == -1) {
880 d3a08f4d 2021-01-17 op if (errno == EINTR) {
881 132cae8c 2021-01-18 op fprintf(stderr, "connected clients: %d\n",
882 d3a08f4d 2021-01-17 op connected_clients);
883 d3a08f4d 2021-01-17 op continue;
884 d3a08f4d 2021-01-17 op }
885 d3a08f4d 2021-01-17 op fatal("poll: %s", strerror(errno));
886 d3a08f4d 2021-01-17 op }
887 d3a08f4d 2021-01-17 op
888 d3a08f4d 2021-01-17 op for (i = 0; i < MAX_USERS; i++) {
889 d3a08f4d 2021-01-17 op if (fds[i].revents == 0)
890 d3a08f4d 2021-01-17 op continue;
891 d3a08f4d 2021-01-17 op
892 d3a08f4d 2021-01-17 op if (fds[i].revents & (POLLERR|POLLNVAL))
893 d3a08f4d 2021-01-17 op fatal("bad fd %d: %s", fds[i].fd,
894 d3a08f4d 2021-01-17 op strerror(errno));
895 d3a08f4d 2021-01-17 op
896 d3a08f4d 2021-01-17 op if (fds[i].revents & POLLHUP) {
897 d3a08f4d 2021-01-17 op /* fds[i] may be the fd of the stdin
898 d3a08f4d 2021-01-17 op * of a cgi script that has exited. */
899 d3a08f4d 2021-01-17 op if (!clients[i].waiting_on_child) {
900 36162ed8 2021-01-22 op close_conn(&fds[i], &clients[i]);
901 d3a08f4d 2021-01-17 op continue;
902 d3a08f4d 2021-01-17 op }
903 d3a08f4d 2021-01-17 op }
904 d3a08f4d 2021-01-17 op
905 d3a08f4d 2021-01-17 op if (fds[i].fd == sock4)
906 d3a08f4d 2021-01-17 op do_accept(sock4, ctx, fds, clients);
907 d3a08f4d 2021-01-17 op else if (fds[i].fd == sock6)
908 d3a08f4d 2021-01-17 op do_accept(sock6, ctx, fds, clients);
909 d3a08f4d 2021-01-17 op else
910 d3a08f4d 2021-01-17 op handle(&fds[i], &clients[i]);
911 d3a08f4d 2021-01-17 op }
912 d3a08f4d 2021-01-17 op }
913 d3a08f4d 2021-01-17 op }