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