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