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