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