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