Blame


1 3e4749f7 2020-10-02 op /*
2 3e4749f7 2020-10-02 op * Copyright (c) 2020 Omar Polo <op@omarpolo.com>
3 3e4749f7 2020-10-02 op *
4 3e4749f7 2020-10-02 op * Permission to use, copy, modify, and distribute this software for any
5 3e4749f7 2020-10-02 op * purpose with or without fee is hereby granted, provided that the above
6 3e4749f7 2020-10-02 op * copyright notice and this permission notice appear in all copies.
7 3e4749f7 2020-10-02 op *
8 3e4749f7 2020-10-02 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 3e4749f7 2020-10-02 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 3e4749f7 2020-10-02 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 3e4749f7 2020-10-02 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 3e4749f7 2020-10-02 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 3e4749f7 2020-10-02 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 3e4749f7 2020-10-02 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 3e4749f7 2020-10-02 op */
16 3e4749f7 2020-10-02 op
17 f28f9311 2020-10-14 op #include <sys/mman.h>
18 3e4749f7 2020-10-02 op #include <sys/socket.h>
19 3e4749f7 2020-10-02 op #include <sys/stat.h>
20 3e4749f7 2020-10-02 op
21 3e4749f7 2020-10-02 op #include <assert.h>
22 3e4749f7 2020-10-02 op #include <err.h>
23 592fd624 2020-10-07 op #include <errno.h>
24 3e4749f7 2020-10-02 op #include <fcntl.h>
25 231bfcdc 2020-12-21 op #include <limits.h>
26 0cf902af 2020-11-03 op #include <signal.h>
27 3e4749f7 2020-10-02 op #include <string.h>
28 3e4749f7 2020-10-02 op
29 488f059a 2020-12-24 op #include "gmid.h"
30 cc68fe70 2020-10-07 op
31 d7802bb4 2020-12-02 op const char *dir, *cgi;
32 d7802bb4 2020-12-02 op int dirfd;
33 721e2325 2020-11-18 op int port;
34 d7802bb4 2020-12-02 op int foreground;
35 120381c9 2020-11-06 op int connected_clients;
36 592fd624 2020-10-07 op
37 120381c9 2020-11-06 op void
38 120381c9 2020-11-06 op siginfo_handler(int sig)
39 120381c9 2020-11-06 op {
40 120381c9 2020-11-06 op (void)sig;
41 120381c9 2020-11-06 op }
42 120381c9 2020-11-06 op
43 72f653b6 2020-11-06 op int
44 72f653b6 2020-11-06 op starts_with(const char *str, const char *prefix)
45 72f653b6 2020-11-06 op {
46 72f653b6 2020-11-06 op size_t i;
47 72f653b6 2020-11-06 op
48 72f653b6 2020-11-06 op for (i = 0; prefix[i] != '\0'; ++i)
49 72f653b6 2020-11-06 op if (str[i] != prefix[i])
50 72f653b6 2020-11-06 op return 0;
51 72f653b6 2020-11-06 op return 1;
52 72f653b6 2020-11-06 op }
53 72f653b6 2020-11-06 op
54 3e4749f7 2020-10-02 op char *
55 3e4749f7 2020-10-02 op url_after_proto(char *url)
56 3e4749f7 2020-10-02 op {
57 3e4749f7 2020-10-02 op char *s;
58 c603f126 2020-11-18 op const char *proto = "gemini:";
59 ae2ad03e 2020-11-18 op const char *marker = "//";
60 3e4749f7 2020-10-02 op
61 3c19febb 2020-11-06 op /* a relative URL */
62 3e4749f7 2020-10-02 op if ((s = strstr(url, marker)) == NULL)
63 3e4749f7 2020-10-02 op return url;
64 ae2ad03e 2020-11-18 op
65 ae2ad03e 2020-11-18 op /*
66 ae2ad03e 2020-11-18 op * if a protocol is not specified, gemini should be implied:
67 ae2ad03e 2020-11-18 op * this handles the case of //example.com
68 ae2ad03e 2020-11-18 op */
69 ae2ad03e 2020-11-18 op if (s == url)
70 ae2ad03e 2020-11-18 op return s + strlen(marker);
71 3e4749f7 2020-10-02 op
72 3c19febb 2020-11-06 op if (s - strlen(proto) != url)
73 3e4749f7 2020-10-02 op return NULL;
74 3c19febb 2020-11-06 op
75 72f653b6 2020-11-06 op if (!starts_with(url, proto))
76 72f653b6 2020-11-06 op return NULL;
77 3e4749f7 2020-10-02 op
78 3e4749f7 2020-10-02 op return s + strlen(marker);
79 3e4749f7 2020-10-02 op }
80 3e4749f7 2020-10-02 op
81 3e4749f7 2020-10-02 op char *
82 3e4749f7 2020-10-02 op url_start_of_request(char *url)
83 3e4749f7 2020-10-02 op {
84 3e4749f7 2020-10-02 op char *s, *t;
85 3e4749f7 2020-10-02 op
86 3e4749f7 2020-10-02 op if ((s = url_after_proto(url)) == NULL)
87 3e4749f7 2020-10-02 op return NULL;
88 7cd6880b 2020-11-18 op
89 7cd6880b 2020-11-18 op /* non-absolute URL */
90 7cd6880b 2020-11-18 op if (s == url)
91 7cd6880b 2020-11-18 op return s;
92 3e4749f7 2020-10-02 op
93 3e4749f7 2020-10-02 op if ((t = strstr(s, "/")) == NULL)
94 3e4749f7 2020-10-02 op return s + strlen(s);
95 3e4749f7 2020-10-02 op return t;
96 3e4749f7 2020-10-02 op }
97 3e4749f7 2020-10-02 op
98 3e4749f7 2020-10-02 op int
99 2c3a40fa 2020-11-06 op url_trim(struct client *c, char *url)
100 3e4749f7 2020-10-02 op {
101 3e4749f7 2020-10-02 op const char *e = "\r\n";
102 3e4749f7 2020-10-02 op char *s;
103 3e4749f7 2020-10-02 op
104 3e4749f7 2020-10-02 op if ((s = strstr(url, e)) == NULL)
105 3e4749f7 2020-10-02 op return 0;
106 3e4749f7 2020-10-02 op s[0] = '\0';
107 3e4749f7 2020-10-02 op s[1] = '\0';
108 3e4749f7 2020-10-02 op
109 3e4749f7 2020-10-02 op if (s[2] != '\0') {
110 d7802bb4 2020-12-02 op LOGE(c, "%s", "request longer than 1024 bytes");
111 3e4749f7 2020-10-02 op return 0;
112 3e4749f7 2020-10-02 op }
113 3e4749f7 2020-10-02 op
114 3e4749f7 2020-10-02 op return 1;
115 3e4749f7 2020-10-02 op }
116 3e4749f7 2020-10-02 op
117 aa0fe0cf 2020-11-06 op char *
118 3e4749f7 2020-10-02 op adjust_path(char *path)
119 3e4749f7 2020-10-02 op {
120 aa0fe0cf 2020-11-06 op char *s, *query;
121 3e4749f7 2020-10-02 op size_t len;
122 aa0fe0cf 2020-11-06 op
123 aa0fe0cf 2020-11-06 op if ((query = strchr(path, '?')) != NULL) {
124 aa0fe0cf 2020-11-06 op *query = '\0';
125 aa0fe0cf 2020-11-06 op query++;
126 aa0fe0cf 2020-11-06 op }
127 3e4749f7 2020-10-02 op
128 f28f9311 2020-10-14 op /* /.. -> / */
129 3e4749f7 2020-10-02 op len = strlen(path);
130 3e4749f7 2020-10-02 op if (len >= 3) {
131 3e4749f7 2020-10-02 op if (!strcmp(&path[len-3], "/..")) {
132 3e4749f7 2020-10-02 op path[len-2] = '\0';
133 3e4749f7 2020-10-02 op }
134 3e4749f7 2020-10-02 op }
135 3e4749f7 2020-10-02 op
136 3e4749f7 2020-10-02 op /* if the path is only `..` trim out and exit */
137 3e4749f7 2020-10-02 op if (!strcmp(path, "..")) {
138 3e4749f7 2020-10-02 op path[0] = '\0';
139 aa0fe0cf 2020-11-06 op return query;
140 3e4749f7 2020-10-02 op }
141 3e4749f7 2020-10-02 op
142 3e4749f7 2020-10-02 op /* remove every ../ in the path */
143 3e4749f7 2020-10-02 op while (1) {
144 3e4749f7 2020-10-02 op if ((s = strstr(path, "../")) == NULL)
145 aa0fe0cf 2020-11-06 op return query;
146 67328d23 2020-10-03 op memmove(s, s+3, strlen(s)+1); /* copy also the \0 */
147 3e4749f7 2020-10-02 op }
148 3e4749f7 2020-10-02 op }
149 3e4749f7 2020-10-02 op
150 3e4749f7 2020-10-02 op int
151 592fd624 2020-10-07 op start_reply(struct pollfd *pfd, struct client *client, int code, const char *reason)
152 3e4749f7 2020-10-02 op {
153 3e4749f7 2020-10-02 op char buf[1030] = {0}; /* status + ' ' + max reply len + \r\n\0 */
154 3e4749f7 2020-10-02 op int len;
155 592fd624 2020-10-07 op int ret;
156 3e4749f7 2020-10-02 op
157 592fd624 2020-10-07 op client->code = code;
158 592fd624 2020-10-07 op client->meta = reason;
159 592fd624 2020-10-07 op client->state = S_INITIALIZING;
160 592fd624 2020-10-07 op
161 3e4749f7 2020-10-02 op len = snprintf(buf, sizeof(buf), "%d %s\r\n", code, reason);
162 3e4749f7 2020-10-02 op assert(len < (int)sizeof(buf));
163 592fd624 2020-10-07 op ret = tls_write(client->ctx, buf, len);
164 592fd624 2020-10-07 op if (ret == TLS_WANT_POLLIN) {
165 592fd624 2020-10-07 op pfd->events = POLLIN;
166 592fd624 2020-10-07 op return 0;
167 592fd624 2020-10-07 op }
168 592fd624 2020-10-07 op
169 592fd624 2020-10-07 op if (ret == TLS_WANT_POLLOUT) {
170 592fd624 2020-10-07 op pfd->events = POLLOUT;
171 592fd624 2020-10-07 op return 0;
172 592fd624 2020-10-07 op }
173 592fd624 2020-10-07 op
174 592fd624 2020-10-07 op return 1;
175 3e4749f7 2020-10-02 op }
176 3e4749f7 2020-10-02 op
177 f28f9311 2020-10-14 op ssize_t
178 f28f9311 2020-10-14 op filesize(int fd)
179 f28f9311 2020-10-14 op {
180 f28f9311 2020-10-14 op ssize_t len;
181 f28f9311 2020-10-14 op
182 f28f9311 2020-10-14 op if ((len = lseek(fd, 0, SEEK_END)) == -1)
183 f28f9311 2020-10-14 op return -1;
184 f28f9311 2020-10-14 op if (lseek(fd, 0, SEEK_SET) == -1)
185 f28f9311 2020-10-14 op return -1;
186 f28f9311 2020-10-14 op return len;
187 f28f9311 2020-10-14 op }
188 f28f9311 2020-10-14 op
189 cc68fe70 2020-10-07 op const char *
190 cc68fe70 2020-10-07 op path_ext(const char *path)
191 cc68fe70 2020-10-07 op {
192 cc68fe70 2020-10-07 op const char *end;
193 cc68fe70 2020-10-07 op
194 cc68fe70 2020-10-07 op end = path + strlen(path)-1; /* the last byte before the NUL */
195 cc68fe70 2020-10-07 op for (; end != path; --end) {
196 cc68fe70 2020-10-07 op if (*end == '.')
197 cc68fe70 2020-10-07 op return end+1;
198 cc68fe70 2020-10-07 op if (*end == '/')
199 cc68fe70 2020-10-07 op break;
200 cc68fe70 2020-10-07 op }
201 cc68fe70 2020-10-07 op
202 cc68fe70 2020-10-07 op return NULL;
203 cc68fe70 2020-10-07 op }
204 cc68fe70 2020-10-07 op
205 cc68fe70 2020-10-07 op const char *
206 cc68fe70 2020-10-07 op mime(const char *path)
207 cc68fe70 2020-10-07 op {
208 cc68fe70 2020-10-07 op const char *ext, *def = "application/octet-stream";
209 cc68fe70 2020-10-07 op struct etm *t;
210 cc68fe70 2020-10-07 op
211 cc68fe70 2020-10-07 op if ((ext = path_ext(path)) == NULL)
212 cc68fe70 2020-10-07 op return def;
213 cc68fe70 2020-10-07 op
214 cc68fe70 2020-10-07 op for (t = filetypes; t->mime != NULL; ++t)
215 cc68fe70 2020-10-07 op if (!strcmp(ext, t->ext))
216 cc68fe70 2020-10-07 op return t->mime;
217 cc68fe70 2020-10-07 op
218 cc68fe70 2020-10-07 op return def;
219 3e4749f7 2020-10-02 op }
220 3e4749f7 2020-10-02 op
221 f28f9311 2020-10-14 op int
222 d7802bb4 2020-12-02 op check_path(struct client *c, const char *path, int *fd)
223 3e4749f7 2020-10-02 op {
224 72342dc9 2020-11-06 op struct stat sb;
225 3e4749f7 2020-10-02 op
226 f28f9311 2020-10-14 op assert(path != NULL);
227 a5d310bc 2020-11-10 op if ((*fd = openat(dirfd, path,
228 a5d310bc 2020-11-10 op O_RDONLY | O_NOFOLLOW | O_CLOEXEC)) == -1) {
229 a5d310bc 2020-11-10 op return FILE_MISSING;
230 a5d310bc 2020-11-10 op }
231 a5d310bc 2020-11-10 op
232 a5d310bc 2020-11-10 op if (fstat(*fd, &sb) == -1) {
233 d7802bb4 2020-12-02 op LOGN(c, "failed stat for %s: %s", path, strerror(errno));
234 a5d310bc 2020-11-10 op return FILE_MISSING;
235 a5d310bc 2020-11-10 op }
236 a5d310bc 2020-11-10 op
237 a5d310bc 2020-11-10 op if (S_ISDIR(sb.st_mode))
238 a5d310bc 2020-11-10 op return FILE_DIRECTORY;
239 a5d310bc 2020-11-10 op
240 a5d310bc 2020-11-10 op if (sb.st_mode & S_IXUSR)
241 a5d310bc 2020-11-10 op return FILE_EXECUTABLE;
242 a5d310bc 2020-11-10 op
243 a5d310bc 2020-11-10 op return FILE_EXISTS;
244 a5d310bc 2020-11-10 op }
245 a5d310bc 2020-11-10 op
246 a5d310bc 2020-11-10 op /*
247 a5d310bc 2020-11-10 op * the inverse of this algorithm, i.e. starting from the start of the
248 a5d310bc 2020-11-10 op * path + strlen(cgi), and checking if each component, should be
249 a5d310bc 2020-11-10 op * faster. But it's tedious to write. This does the opposite: starts
250 a5d310bc 2020-11-10 op * from the end and strip one component at a time, until either an
251 a5d310bc 2020-11-10 op * executable is found or we emptied the path.
252 a5d310bc 2020-11-10 op */
253 a5d310bc 2020-11-10 op int
254 a5d310bc 2020-11-10 op check_for_cgi(char *path, char *query, struct pollfd *fds, struct client *c)
255 a5d310bc 2020-11-10 op {
256 a5d310bc 2020-11-10 op char *end;
257 a5d310bc 2020-11-10 op end = strchr(path, '\0');
258 a5d310bc 2020-11-10 op
259 a5d310bc 2020-11-10 op /* NB: assume CGI is enabled and path matches cgi */
260 a5d310bc 2020-11-10 op
261 a5d310bc 2020-11-10 op while (end > path) {
262 a5d310bc 2020-11-10 op /* go up one level. UNIX paths are simple and POSIX
263 a5d310bc 2020-11-10 op * dirname, with its ambiguities on if the given path
264 a5d310bc 2020-11-10 op * is changed or not, gives me headaches. */
265 a5d310bc 2020-11-10 op while (*end != '/')
266 a5d310bc 2020-11-10 op end--;
267 a5d310bc 2020-11-10 op *end = '\0';
268 3e4749f7 2020-10-02 op
269 d7802bb4 2020-12-02 op switch (check_path(c, path, &c->fd)) {
270 a5d310bc 2020-11-10 op case FILE_EXECUTABLE:
271 a5d310bc 2020-11-10 op return start_cgi(path, end+1, query, fds,c);
272 a5d310bc 2020-11-10 op case FILE_MISSING:
273 a5d310bc 2020-11-10 op break;
274 a5d310bc 2020-11-10 op default:
275 a5d310bc 2020-11-10 op goto err;
276 a5d310bc 2020-11-10 op }
277 a5d310bc 2020-11-10 op
278 a5d310bc 2020-11-10 op *end = '/';
279 a5d310bc 2020-11-10 op end--;
280 a5d310bc 2020-11-10 op }
281 a5d310bc 2020-11-10 op
282 a5d310bc 2020-11-10 op err:
283 a5d310bc 2020-11-10 op if (!start_reply(fds, c, NOT_FOUND, "not found"))
284 a5d310bc 2020-11-10 op return 0;
285 a5d310bc 2020-11-10 op goodbye(fds, c);
286 a5d310bc 2020-11-10 op return 0;
287 a5d310bc 2020-11-10 op }
288 a5d310bc 2020-11-10 op
289 a5d310bc 2020-11-10 op
290 a5d310bc 2020-11-10 op int
291 a5d310bc 2020-11-10 op open_file(char *path, char *query, struct pollfd *fds, struct client *c)
292 a5d310bc 2020-11-10 op {
293 a5d310bc 2020-11-10 op char fpath[PATHBUF];
294 a5d310bc 2020-11-10 op
295 f28f9311 2020-10-14 op bzero(fpath, sizeof(fpath));
296 3e4749f7 2020-10-02 op
297 f28f9311 2020-10-14 op if (*path != '.')
298 f28f9311 2020-10-14 op fpath[0] = '.';
299 f28f9311 2020-10-14 op strlcat(fpath, path, PATHBUF);
300 3e4749f7 2020-10-02 op
301 d7802bb4 2020-12-02 op switch (check_path(c, fpath, &c->fd)) {
302 a5d310bc 2020-11-10 op case FILE_EXECUTABLE:
303 a5d310bc 2020-11-10 op /* +2 to skip the ./ */
304 a5d310bc 2020-11-10 op if (cgi != NULL && starts_with(fpath+2, cgi))
305 a5d310bc 2020-11-10 op return start_cgi(fpath, "", query, fds, c);
306 3e4749f7 2020-10-02 op
307 a5d310bc 2020-11-10 op /* fallthrough */
308 a5d310bc 2020-11-10 op
309 a5d310bc 2020-11-10 op case FILE_EXISTS:
310 a5d310bc 2020-11-10 op if ((c->len = filesize(c->fd)) == -1) {
311 d7802bb4 2020-12-02 op LOGE(c, "failed to get file size for %s", fpath);
312 a5d310bc 2020-11-10 op goodbye(fds, c);
313 72342dc9 2020-11-06 op return 0;
314 a5d310bc 2020-11-10 op }
315 72342dc9 2020-11-06 op
316 a5d310bc 2020-11-10 op if ((c->buf = mmap(NULL, c->len, PROT_READ, MAP_PRIVATE,
317 a5d310bc 2020-11-10 op c->fd, 0)) == MAP_FAILED) {
318 a5d310bc 2020-11-10 op warn("mmap: %s", fpath);
319 a5d310bc 2020-11-10 op goodbye(fds, c);
320 a5d310bc 2020-11-10 op return 0;
321 a5d310bc 2020-11-10 op }
322 a5d310bc 2020-11-10 op c->i = c->buf;
323 a5d310bc 2020-11-10 op return start_reply(fds, c, SUCCESS, mime(fpath));
324 a5d310bc 2020-11-10 op
325 a5d310bc 2020-11-10 op case FILE_DIRECTORY:
326 d7802bb4 2020-12-02 op LOGD(c, "%s is a directory, trying %s/index.gmi", fpath, fpath);
327 f28f9311 2020-10-14 op close(c->fd);
328 f28f9311 2020-10-14 op c->fd = -1;
329 f28f9311 2020-10-14 op send_dir(fpath, fds, c);
330 f28f9311 2020-10-14 op return 0;
331 3e4749f7 2020-10-02 op
332 a5d310bc 2020-11-10 op case FILE_MISSING:
333 a5d310bc 2020-11-10 op if (cgi != NULL && starts_with(fpath+2, cgi))
334 a5d310bc 2020-11-10 op return check_for_cgi(fpath, query, fds, c);
335 72342dc9 2020-11-06 op
336 a5d310bc 2020-11-10 op if (!start_reply(fds, c, NOT_FOUND, "not found"))
337 a5d310bc 2020-11-10 op return 0;
338 f28f9311 2020-10-14 op goodbye(fds, c);
339 f28f9311 2020-10-14 op return 0;
340 f28f9311 2020-10-14 op
341 a5d310bc 2020-11-10 op default:
342 a5d310bc 2020-11-10 op /* unreachable */
343 a5d310bc 2020-11-10 op abort();
344 592fd624 2020-10-07 op }
345 72342dc9 2020-11-06 op }
346 72342dc9 2020-11-06 op
347 a5d310bc 2020-11-10 op int
348 a5d310bc 2020-11-10 op start_cgi(const char *spath, const char *relpath, const char *query,
349 75d233f0 2020-11-06 op struct pollfd *fds, struct client *c)
350 72342dc9 2020-11-06 op {
351 72342dc9 2020-11-06 op pid_t pid;
352 a5d310bc 2020-11-10 op int p[2]; /* read end, write end */
353 72342dc9 2020-11-06 op
354 72342dc9 2020-11-06 op if (pipe(p) == -1)
355 72342dc9 2020-11-06 op goto err;
356 72342dc9 2020-11-06 op
357 72342dc9 2020-11-06 op switch (pid = fork()) {
358 72342dc9 2020-11-06 op case -1:
359 72342dc9 2020-11-06 op goto err;
360 72342dc9 2020-11-06 op
361 a5d310bc 2020-11-10 op case 0: { /* child */
362 721e2325 2020-11-18 op char *ex, *requri, *portno;
363 72342dc9 2020-11-06 op char addr[INET_ADDRSTRLEN];
364 72342dc9 2020-11-06 op char *argv[] = { NULL, NULL, NULL };
365 72342dc9 2020-11-06 op
366 a5d310bc 2020-11-10 op spath++;
367 72342dc9 2020-11-06 op
368 a5d310bc 2020-11-10 op close(p[0]);
369 72342dc9 2020-11-06 op if (dup2(p[1], 1) == -1)
370 72342dc9 2020-11-06 op goto childerr;
371 72342dc9 2020-11-06 op
372 72342dc9 2020-11-06 op if (inet_ntop(c->af, &c->addr, addr, sizeof(addr)) == NULL)
373 a5d310bc 2020-11-10 op goto childerr;
374 72342dc9 2020-11-06 op
375 721e2325 2020-11-18 op if (asprintf(&portno, "%d", port) == -1)
376 721e2325 2020-11-18 op goto childerr;
377 721e2325 2020-11-18 op
378 a5d310bc 2020-11-10 op if (asprintf(&ex, "%s%s", dir, spath+1) == -1)
379 72342dc9 2020-11-06 op goto childerr;
380 72342dc9 2020-11-06 op
381 a5d310bc 2020-11-10 op if (asprintf(&requri, "%s%s%s", spath,
382 a5d310bc 2020-11-10 op *relpath == '\0' ? "" : "/",
383 a5d310bc 2020-11-10 op relpath) == -1)
384 a5d310bc 2020-11-10 op goto childerr;
385 a5d310bc 2020-11-10 op
386 a5d310bc 2020-11-10 op argv[0] = argv[1] = ex;
387 a5d310bc 2020-11-10 op
388 60ba426e 2020-11-06 op /* fix the env */
389 677afbd3 2020-12-02 op SAFE_SETENV("GATEWAY_INTERFACE", "CGI/1.1");
390 677afbd3 2020-12-02 op SAFE_SETENV("SERVER_SOFTWARE", "gmid");
391 677afbd3 2020-12-02 op SAFE_SETENV("SERVER_PORT", portno);
392 a5d310bc 2020-11-10 op /* setenv("SERVER_NAME", "", 1); */
393 677afbd3 2020-12-02 op SAFE_SETENV("SCRIPT_NAME", spath);
394 677afbd3 2020-12-02 op SAFE_SETENV("SCRIPT_EXECUTABLE", ex);
395 677afbd3 2020-12-02 op SAFE_SETENV("REQUEST_URI", requri);
396 677afbd3 2020-12-02 op SAFE_SETENV("REQUEST_RELATIVE", relpath);
397 677afbd3 2020-12-02 op SAFE_SETENV("QUERY_STRING", query);
398 677afbd3 2020-12-02 op SAFE_SETENV("REMOTE_HOST", addr);
399 677afbd3 2020-12-02 op SAFE_SETENV("REMOTE_ADDR", addr);
400 677afbd3 2020-12-02 op SAFE_SETENV("DOCUMENT_ROOT", dir);
401 72342dc9 2020-11-06 op
402 677afbd3 2020-12-02 op if (tls_peer_cert_provided(c->ctx)) {
403 677afbd3 2020-12-02 op SAFE_SETENV("AUTH_TYPE", "Certificate");
404 677afbd3 2020-12-02 op SAFE_SETENV("REMOTE_USER", tls_peer_cert_subject(c->ctx));
405 677afbd3 2020-12-02 op SAFE_SETENV("TLS_CLIENT_ISSUER", tls_peer_cert_issuer(c->ctx));
406 677afbd3 2020-12-02 op SAFE_SETENV("TLS_CLIENT_HASH", tls_peer_cert_hash(c->ctx));
407 677afbd3 2020-12-02 op }
408 677afbd3 2020-12-02 op
409 a5d310bc 2020-11-10 op execvp(ex, argv);
410 72342dc9 2020-11-06 op goto childerr;
411 72342dc9 2020-11-06 op }
412 72342dc9 2020-11-06 op
413 72342dc9 2020-11-06 op default: /* parent */
414 a5d310bc 2020-11-10 op close(p[1]);
415 72342dc9 2020-11-06 op close(c->fd);
416 72342dc9 2020-11-06 op c->fd = p[0];
417 72342dc9 2020-11-06 op c->child = pid;
418 aff8d190 2020-11-06 op mark_nonblock(c->fd);
419 aff8d190 2020-11-06 op c->state = S_SENDING;
420 72342dc9 2020-11-06 op handle_cgi(fds, c);
421 a5d310bc 2020-11-10 op return 0;
422 72342dc9 2020-11-06 op }
423 72342dc9 2020-11-06 op
424 72342dc9 2020-11-06 op err:
425 72342dc9 2020-11-06 op if (!start_reply(fds, c, TEMP_FAILURE, "internal server error"))
426 a5d310bc 2020-11-10 op return 0;
427 72342dc9 2020-11-06 op goodbye(fds, c);
428 a5d310bc 2020-11-10 op return 0;
429 72342dc9 2020-11-06 op
430 72342dc9 2020-11-06 op childerr:
431 72342dc9 2020-11-06 op dprintf(p[1], "%d internal server error\r\n", TEMP_FAILURE);
432 72342dc9 2020-11-06 op close(p[1]);
433 a5d310bc 2020-11-10 op _exit(1);
434 72342dc9 2020-11-06 op }
435 72342dc9 2020-11-06 op
436 72342dc9 2020-11-06 op void
437 aff8d190 2020-11-06 op cgi_setpoll_on_child(struct pollfd *fds, struct client *c)
438 72342dc9 2020-11-06 op {
439 aff8d190 2020-11-06 op int fd;
440 72342dc9 2020-11-06 op
441 aff8d190 2020-11-06 op if (c->waiting_on_child)
442 aff8d190 2020-11-06 op return;
443 aff8d190 2020-11-06 op c->waiting_on_child = 1;
444 aff8d190 2020-11-06 op
445 aff8d190 2020-11-06 op fds->events = POLLIN;
446 aff8d190 2020-11-06 op
447 aff8d190 2020-11-06 op fd = fds->fd;
448 aff8d190 2020-11-06 op fds->fd = c->fd;
449 aff8d190 2020-11-06 op c->fd = fd;
450 aff8d190 2020-11-06 op }
451 aff8d190 2020-11-06 op
452 aff8d190 2020-11-06 op void
453 aff8d190 2020-11-06 op cgi_setpoll_on_client(struct pollfd *fds, struct client *c)
454 aff8d190 2020-11-06 op {
455 aff8d190 2020-11-06 op int fd;
456 aff8d190 2020-11-06 op
457 aff8d190 2020-11-06 op if (!c->waiting_on_child)
458 aff8d190 2020-11-06 op return;
459 aff8d190 2020-11-06 op c->waiting_on_child = 0;
460 aff8d190 2020-11-06 op
461 aff8d190 2020-11-06 op fd = fds->fd;
462 aff8d190 2020-11-06 op fds->fd = c->fd;
463 aff8d190 2020-11-06 op c->fd = fd;
464 aff8d190 2020-11-06 op }
465 aff8d190 2020-11-06 op
466 aff8d190 2020-11-06 op void
467 aff8d190 2020-11-06 op handle_cgi(struct pollfd *fds, struct client *c)
468 aff8d190 2020-11-06 op {
469 aff8d190 2020-11-06 op ssize_t r;
470 aff8d190 2020-11-06 op
471 aff8d190 2020-11-06 op /* ensure c->fd is the child and fds->fd the client */
472 aff8d190 2020-11-06 op cgi_setpoll_on_client(fds, c);
473 aff8d190 2020-11-06 op
474 72342dc9 2020-11-06 op while (1) {
475 aff8d190 2020-11-06 op if (c->len == 0) {
476 aff8d190 2020-11-06 op if ((r = read(c->fd, c->sbuf, sizeof(c->sbuf))) == 0)
477 aff8d190 2020-11-06 op goto end;
478 aff8d190 2020-11-06 op if (r == -1) {
479 aff8d190 2020-11-06 op if (errno == EAGAIN || errno == EWOULDBLOCK) {
480 aff8d190 2020-11-06 op cgi_setpoll_on_child(fds, c);
481 aff8d190 2020-11-06 op return;
482 aff8d190 2020-11-06 op }
483 aff8d190 2020-11-06 op goto end;
484 aff8d190 2020-11-06 op }
485 aff8d190 2020-11-06 op c->len = r;
486 aff8d190 2020-11-06 op c->off = 0;
487 aff8d190 2020-11-06 op }
488 aff8d190 2020-11-06 op
489 aff8d190 2020-11-06 op while (c->len > 0) {
490 aff8d190 2020-11-06 op switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
491 72342dc9 2020-11-06 op case -1:
492 72342dc9 2020-11-06 op goto end;
493 72342dc9 2020-11-06 op
494 72342dc9 2020-11-06 op case TLS_WANT_POLLOUT:
495 aff8d190 2020-11-06 op fds->events = POLLOUT;
496 aff8d190 2020-11-06 op return;
497 aff8d190 2020-11-06 op
498 72342dc9 2020-11-06 op case TLS_WANT_POLLIN:
499 aff8d190 2020-11-06 op fds->events = POLLIN;
500 aff8d190 2020-11-06 op return;
501 72342dc9 2020-11-06 op
502 72342dc9 2020-11-06 op default:
503 aff8d190 2020-11-06 op c->off += r;
504 aff8d190 2020-11-06 op c->len -= r;
505 aff8d190 2020-11-06 op break;
506 72342dc9 2020-11-06 op }
507 72342dc9 2020-11-06 op }
508 72342dc9 2020-11-06 op }
509 72342dc9 2020-11-06 op
510 72342dc9 2020-11-06 op end:
511 72342dc9 2020-11-06 op goodbye(fds, c);
512 f28f9311 2020-10-14 op }
513 592fd624 2020-10-07 op
514 f28f9311 2020-10-14 op void
515 75d233f0 2020-11-06 op send_file(char *path, char *query, struct pollfd *fds, struct client *c)
516 f28f9311 2020-10-14 op {
517 f28f9311 2020-10-14 op ssize_t ret, len;
518 592fd624 2020-10-07 op
519 f28f9311 2020-10-14 op if (c->fd == -1) {
520 75d233f0 2020-11-06 op if (!open_file(path, query, fds, c))
521 f28f9311 2020-10-14 op return;
522 f28f9311 2020-10-14 op c->state = S_SENDING;
523 f28f9311 2020-10-14 op }
524 592fd624 2020-10-07 op
525 f28f9311 2020-10-14 op len = (c->buf + c->len) - c->i;
526 f28f9311 2020-10-14 op
527 f28f9311 2020-10-14 op while (len > 0) {
528 f28f9311 2020-10-14 op switch (ret = tls_write(c->ctx, c->i, len)) {
529 f28f9311 2020-10-14 op case -1:
530 d7802bb4 2020-12-02 op LOGE(c, "tls_write: %s", tls_error(c->ctx));
531 f28f9311 2020-10-14 op goodbye(fds, c);
532 f28f9311 2020-10-14 op return;
533 f28f9311 2020-10-14 op
534 f28f9311 2020-10-14 op case TLS_WANT_POLLIN:
535 f28f9311 2020-10-14 op fds->events = POLLIN;
536 f28f9311 2020-10-14 op return;
537 f28f9311 2020-10-14 op
538 f28f9311 2020-10-14 op case TLS_WANT_POLLOUT:
539 f28f9311 2020-10-14 op fds->events = POLLOUT;
540 f28f9311 2020-10-14 op return;
541 f28f9311 2020-10-14 op
542 f28f9311 2020-10-14 op default:
543 f28f9311 2020-10-14 op c->i += ret;
544 f28f9311 2020-10-14 op len -= ret;
545 f28f9311 2020-10-14 op break;
546 3e4749f7 2020-10-02 op }
547 3e4749f7 2020-10-02 op }
548 f28f9311 2020-10-14 op
549 f28f9311 2020-10-14 op goodbye(fds, c);
550 3e4749f7 2020-10-02 op }
551 3e4749f7 2020-10-02 op
552 3e4749f7 2020-10-02 op void
553 592fd624 2020-10-07 op send_dir(char *path, struct pollfd *fds, struct client *client)
554 3e4749f7 2020-10-02 op {
555 3e4749f7 2020-10-02 op char fpath[PATHBUF];
556 3e4749f7 2020-10-02 op size_t len;
557 3e4749f7 2020-10-02 op
558 3e4749f7 2020-10-02 op bzero(fpath, PATHBUF);
559 3e4749f7 2020-10-02 op
560 9c56b0a7 2020-10-14 op if (path[0] != '.')
561 3e4749f7 2020-10-02 op fpath[0] = '.';
562 3e4749f7 2020-10-02 op
563 3e4749f7 2020-10-02 op /* this cannot fail since sizeof(fpath) > maxlen of path */
564 3e4749f7 2020-10-02 op strlcat(fpath, path, PATHBUF);
565 3e4749f7 2020-10-02 op len = strlen(fpath);
566 3e4749f7 2020-10-02 op
567 3e4749f7 2020-10-02 op /* add a trailing / in case. */
568 3e4749f7 2020-10-02 op if (fpath[len-1] != '/') {
569 3e4749f7 2020-10-02 op fpath[len] = '/';
570 3e4749f7 2020-10-02 op }
571 3e4749f7 2020-10-02 op
572 3e4749f7 2020-10-02 op strlcat(fpath, "index.gmi", sizeof(fpath));
573 3e4749f7 2020-10-02 op
574 75d233f0 2020-11-06 op send_file(fpath, NULL, fds, client);
575 3e4749f7 2020-10-02 op }
576 3e4749f7 2020-10-02 op
577 3e4749f7 2020-10-02 op void
578 592fd624 2020-10-07 op handle(struct pollfd *fds, struct client *client)
579 3e4749f7 2020-10-02 op {
580 592fd624 2020-10-07 op char buf[GEMINI_URL_LEN];
581 3e4749f7 2020-10-02 op char *path;
582 aa0fe0cf 2020-11-06 op char *query;
583 3e4749f7 2020-10-02 op
584 592fd624 2020-10-07 op switch (client->state) {
585 592fd624 2020-10-07 op case S_OPEN:
586 592fd624 2020-10-07 op bzero(buf, GEMINI_URL_LEN);
587 592fd624 2020-10-07 op switch (tls_read(client->ctx, buf, sizeof(buf)-1)) {
588 592fd624 2020-10-07 op case -1:
589 d7802bb4 2020-12-02 op LOGE(client, "tls_read: %s", tls_error(client->ctx));
590 592fd624 2020-10-07 op goodbye(fds, client);
591 592fd624 2020-10-07 op return;
592 3e4749f7 2020-10-02 op
593 592fd624 2020-10-07 op case TLS_WANT_POLLIN:
594 592fd624 2020-10-07 op fds->events = POLLIN;
595 592fd624 2020-10-07 op return;
596 3e4749f7 2020-10-02 op
597 592fd624 2020-10-07 op case TLS_WANT_POLLOUT:
598 592fd624 2020-10-07 op fds->events = POLLOUT;
599 592fd624 2020-10-07 op return;
600 592fd624 2020-10-07 op }
601 3e4749f7 2020-10-02 op
602 2c3a40fa 2020-11-06 op if (!url_trim(client, buf)) {
603 592fd624 2020-10-07 op if (!start_reply(fds, client, BAD_REQUEST, "bad request"))
604 592fd624 2020-10-07 op return;
605 592fd624 2020-10-07 op goodbye(fds, client);
606 592fd624 2020-10-07 op return;
607 592fd624 2020-10-07 op }
608 3e4749f7 2020-10-02 op
609 592fd624 2020-10-07 op if ((path = url_start_of_request(buf)) == NULL) {
610 592fd624 2020-10-07 op if (!start_reply(fds, client, BAD_REQUEST, "bad request"))
611 592fd624 2020-10-07 op return;
612 592fd624 2020-10-07 op goodbye(fds, client);
613 592fd624 2020-10-07 op return;
614 592fd624 2020-10-07 op }
615 592fd624 2020-10-07 op
616 aa0fe0cf 2020-11-06 op query = adjust_path(path);
617 d7802bb4 2020-12-02 op LOGI(client, "GET %s%s%s", path,
618 aa0fe0cf 2020-11-06 op query ? "?" : "",
619 aa0fe0cf 2020-11-06 op query ? query : "");
620 592fd624 2020-10-07 op
621 a5d310bc 2020-11-10 op send_file(path, query, fds, client);
622 592fd624 2020-10-07 op break;
623 592fd624 2020-10-07 op
624 592fd624 2020-10-07 op case S_INITIALIZING:
625 592fd624 2020-10-07 op if (!start_reply(fds, client, client->code, client->meta))
626 592fd624 2020-10-07 op return;
627 592fd624 2020-10-07 op
628 592fd624 2020-10-07 op if (client->code != SUCCESS) {
629 592fd624 2020-10-07 op /* we don't need a body */
630 592fd624 2020-10-07 op goodbye(fds, client);
631 592fd624 2020-10-07 op return;
632 592fd624 2020-10-07 op }
633 592fd624 2020-10-07 op
634 592fd624 2020-10-07 op client->state = S_SENDING;
635 592fd624 2020-10-07 op
636 592fd624 2020-10-07 op /* fallthrough */
637 592fd624 2020-10-07 op
638 592fd624 2020-10-07 op case S_SENDING:
639 aff8d190 2020-11-06 op if (client->child != -1)
640 aff8d190 2020-11-06 op handle_cgi(fds, client);
641 aff8d190 2020-11-06 op else
642 aff8d190 2020-11-06 op send_file(NULL, NULL, fds, client);
643 592fd624 2020-10-07 op break;
644 592fd624 2020-10-07 op
645 592fd624 2020-10-07 op case S_CLOSING:
646 592fd624 2020-10-07 op goodbye(fds, client);
647 592fd624 2020-10-07 op break;
648 592fd624 2020-10-07 op
649 592fd624 2020-10-07 op default:
650 592fd624 2020-10-07 op /* unreachable */
651 592fd624 2020-10-07 op abort();
652 592fd624 2020-10-07 op }
653 3e4749f7 2020-10-02 op }
654 3e4749f7 2020-10-02 op
655 592fd624 2020-10-07 op void
656 592fd624 2020-10-07 op mark_nonblock(int fd)
657 592fd624 2020-10-07 op {
658 592fd624 2020-10-07 op int flags;
659 592fd624 2020-10-07 op
660 592fd624 2020-10-07 op if ((flags = fcntl(fd, F_GETFL)) == -1)
661 d7802bb4 2020-12-02 op FATAL("fcntl(F_GETFL): %s", strerror(errno));
662 592fd624 2020-10-07 op if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
663 d7802bb4 2020-12-02 op FATAL("fcntl(F_SETFL): %s", strerror(errno));
664 592fd624 2020-10-07 op }
665 592fd624 2020-10-07 op
666 3e4749f7 2020-10-02 op int
667 9468027b 2020-10-15 op make_socket(int port, int family)
668 3e4749f7 2020-10-02 op {
669 3e4749f7 2020-10-02 op int sock, v;
670 9468027b 2020-10-15 op struct sockaddr_in addr4;
671 9468027b 2020-10-15 op struct sockaddr_in6 addr6;
672 9468027b 2020-10-15 op struct sockaddr *addr;
673 9468027b 2020-10-15 op socklen_t len;
674 3e4749f7 2020-10-02 op
675 9468027b 2020-10-15 op switch (family) {
676 9468027b 2020-10-15 op case AF_INET:
677 9468027b 2020-10-15 op bzero(&addr4, sizeof(addr4));
678 9468027b 2020-10-15 op addr4.sin_family = family;
679 9468027b 2020-10-15 op addr4.sin_port = htons(port);
680 9468027b 2020-10-15 op addr4.sin_addr.s_addr = INADDR_ANY;
681 9468027b 2020-10-15 op addr = (struct sockaddr*)&addr4;
682 9468027b 2020-10-15 op len = sizeof(addr4);
683 9468027b 2020-10-15 op break;
684 9468027b 2020-10-15 op
685 9468027b 2020-10-15 op case AF_INET6:
686 9468027b 2020-10-15 op bzero(&addr6, sizeof(addr6));
687 9468027b 2020-10-15 op addr6.sin6_family = AF_INET6;
688 9468027b 2020-10-15 op addr6.sin6_port = htons(port);
689 9468027b 2020-10-15 op addr6.sin6_addr = in6addr_any;
690 9468027b 2020-10-15 op addr = (struct sockaddr*)&addr6;
691 9468027b 2020-10-15 op len = sizeof(addr6);
692 9468027b 2020-10-15 op break;
693 9468027b 2020-10-15 op
694 9468027b 2020-10-15 op default:
695 9468027b 2020-10-15 op /* unreachable */
696 9468027b 2020-10-15 op abort();
697 9468027b 2020-10-15 op }
698 9468027b 2020-10-15 op
699 9468027b 2020-10-15 op if ((sock = socket(family, SOCK_STREAM, 0)) == -1)
700 d7802bb4 2020-12-02 op FATAL("socket: %s", strerror(errno));
701 3e4749f7 2020-10-02 op
702 3e4749f7 2020-10-02 op v = 1;
703 3e4749f7 2020-10-02 op if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
704 d7802bb4 2020-12-02 op FATAL("setsockopt(SO_REUSEADDR): %s", strerror(errno));
705 3e4749f7 2020-10-02 op
706 3e4749f7 2020-10-02 op v = 1;
707 3e4749f7 2020-10-02 op if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
708 d7802bb4 2020-12-02 op FATAL("setsockopt(SO_REUSEPORT): %s", strerror(errno));
709 3e4749f7 2020-10-02 op
710 592fd624 2020-10-07 op mark_nonblock(sock);
711 592fd624 2020-10-07 op
712 9468027b 2020-10-15 op if (bind(sock, addr, len) == -1)
713 d7802bb4 2020-12-02 op FATAL("bind: %s", strerror(errno));
714 3e4749f7 2020-10-02 op
715 3e4749f7 2020-10-02 op if (listen(sock, 16) == -1)
716 d7802bb4 2020-12-02 op FATAL("listen: %s", strerror(errno));
717 3e4749f7 2020-10-02 op
718 3e4749f7 2020-10-02 op return sock;
719 3e4749f7 2020-10-02 op }
720 3e4749f7 2020-10-02 op
721 3e4749f7 2020-10-02 op void
722 592fd624 2020-10-07 op do_accept(int sock, struct tls *ctx, struct pollfd *fds, struct client *clients)
723 3e4749f7 2020-10-02 op {
724 592fd624 2020-10-07 op int i, fd;
725 592fd624 2020-10-07 op struct sockaddr_in addr;
726 3e4749f7 2020-10-02 op socklen_t len;
727 3e4749f7 2020-10-02 op
728 592fd624 2020-10-07 op len = sizeof(addr);
729 592fd624 2020-10-07 op if ((fd = accept(sock, (struct sockaddr*)&addr, &len)) == -1) {
730 592fd624 2020-10-07 op if (errno == EWOULDBLOCK)
731 592fd624 2020-10-07 op return;
732 d7802bb4 2020-12-02 op FATAL("accept: %s", strerror(errno));
733 592fd624 2020-10-07 op }
734 3e4749f7 2020-10-02 op
735 592fd624 2020-10-07 op mark_nonblock(fd);
736 3e4749f7 2020-10-02 op
737 592fd624 2020-10-07 op for (i = 0; i < MAX_USERS; ++i) {
738 592fd624 2020-10-07 op if (fds[i].fd == -1) {
739 592fd624 2020-10-07 op bzero(&clients[i], sizeof(struct client));
740 592fd624 2020-10-07 op if (tls_accept_socket(ctx, &clients[i].ctx, fd) == -1)
741 592fd624 2020-10-07 op break; /* goodbye fd! */
742 592fd624 2020-10-07 op
743 592fd624 2020-10-07 op fds[i].fd = fd;
744 592fd624 2020-10-07 op fds[i].events = POLLIN;
745 592fd624 2020-10-07 op
746 592fd624 2020-10-07 op clients[i].state = S_OPEN;
747 592fd624 2020-10-07 op clients[i].fd = -1;
748 72342dc9 2020-11-06 op clients[i].child = -1;
749 f28f9311 2020-10-14 op clients[i].buf = MAP_FAILED;
750 2c3a40fa 2020-11-06 op clients[i].af = AF_INET;
751 2c3a40fa 2020-11-06 op clients[i].addr = addr.sin_addr;
752 592fd624 2020-10-07 op
753 120381c9 2020-11-06 op connected_clients++;
754 592fd624 2020-10-07 op return;
755 3e4749f7 2020-10-02 op }
756 592fd624 2020-10-07 op }
757 3e4749f7 2020-10-02 op
758 592fd624 2020-10-07 op close(fd);
759 592fd624 2020-10-07 op }
760 3e4749f7 2020-10-02 op
761 592fd624 2020-10-07 op void
762 592fd624 2020-10-07 op goodbye(struct pollfd *pfd, struct client *c)
763 592fd624 2020-10-07 op {
764 592fd624 2020-10-07 op ssize_t ret;
765 592fd624 2020-10-07 op
766 592fd624 2020-10-07 op c->state = S_CLOSING;
767 592fd624 2020-10-07 op
768 592fd624 2020-10-07 op ret = tls_close(c->ctx);
769 592fd624 2020-10-07 op if (ret == TLS_WANT_POLLIN) {
770 592fd624 2020-10-07 op pfd->events = POLLIN;
771 592fd624 2020-10-07 op return;
772 3e4749f7 2020-10-02 op }
773 592fd624 2020-10-07 op if (ret == TLS_WANT_POLLOUT) {
774 592fd624 2020-10-07 op pfd->events = POLLOUT;
775 592fd624 2020-10-07 op return;
776 592fd624 2020-10-07 op }
777 592fd624 2020-10-07 op
778 120381c9 2020-11-06 op connected_clients--;
779 120381c9 2020-11-06 op
780 592fd624 2020-10-07 op tls_free(c->ctx);
781 592fd624 2020-10-07 op c->ctx = NULL;
782 592fd624 2020-10-07 op
783 f28f9311 2020-10-14 op if (c->buf != MAP_FAILED)
784 f28f9311 2020-10-14 op munmap(c->buf, c->len);
785 f28f9311 2020-10-14 op
786 592fd624 2020-10-07 op if (c->fd != -1)
787 592fd624 2020-10-07 op close(c->fd);
788 592fd624 2020-10-07 op
789 592fd624 2020-10-07 op close(pfd->fd);
790 592fd624 2020-10-07 op pfd->fd = -1;
791 3e4749f7 2020-10-02 op }
792 3e4749f7 2020-10-02 op
793 3e4749f7 2020-10-02 op void
794 592fd624 2020-10-07 op loop(struct tls *ctx, int sock)
795 592fd624 2020-10-07 op {
796 592fd624 2020-10-07 op int i, todo;
797 592fd624 2020-10-07 op struct client clients[MAX_USERS];
798 592fd624 2020-10-07 op struct pollfd fds[MAX_USERS];
799 592fd624 2020-10-07 op
800 592fd624 2020-10-07 op for (i = 0; i < MAX_USERS; ++i) {
801 592fd624 2020-10-07 op fds[i].fd = -1;
802 592fd624 2020-10-07 op fds[i].events = POLLIN;
803 592fd624 2020-10-07 op bzero(&clients[i], sizeof(struct client));
804 592fd624 2020-10-07 op }
805 592fd624 2020-10-07 op
806 592fd624 2020-10-07 op fds[0].fd = sock;
807 592fd624 2020-10-07 op
808 592fd624 2020-10-07 op for (;;) {
809 120381c9 2020-11-06 op if ((todo = poll(fds, MAX_USERS, INFTIM)) == -1) {
810 120381c9 2020-11-06 op if (errno == EINTR) {
811 d7802bb4 2020-12-02 op warnx("connected clients: %d",
812 d7802bb4 2020-12-02 op connected_clients);
813 120381c9 2020-11-06 op continue;
814 120381c9 2020-11-06 op }
815 d7802bb4 2020-12-02 op FATAL("poll: %s", strerror(errno));
816 120381c9 2020-11-06 op }
817 592fd624 2020-10-07 op
818 592fd624 2020-10-07 op for (i = 0; i < MAX_USERS; i++) {
819 592fd624 2020-10-07 op assert(i < MAX_USERS);
820 592fd624 2020-10-07 op
821 592fd624 2020-10-07 op if (fds[i].revents == 0)
822 592fd624 2020-10-07 op continue;
823 592fd624 2020-10-07 op
824 592fd624 2020-10-07 op if (fds[i].revents & (POLLERR|POLLNVAL))
825 d7802bb4 2020-12-02 op FATAL("bad fd %d: %s", fds[i].fd,
826 d7802bb4 2020-12-02 op strerror(errno));
827 592fd624 2020-10-07 op
828 592fd624 2020-10-07 op if (fds[i].revents & POLLHUP) {
829 aff8d190 2020-11-06 op /* fds[i] may be the fd of the stdin
830 aff8d190 2020-11-06 op * of a cgi script that has exited. */
831 aff8d190 2020-11-06 op if (!clients[i].waiting_on_child) {
832 aff8d190 2020-11-06 op goodbye(&fds[i], &clients[i]);
833 aff8d190 2020-11-06 op continue;
834 aff8d190 2020-11-06 op }
835 592fd624 2020-10-07 op }
836 592fd624 2020-10-07 op
837 592fd624 2020-10-07 op todo--;
838 592fd624 2020-10-07 op
839 592fd624 2020-10-07 op if (i == 0) { /* new client */
840 592fd624 2020-10-07 op do_accept(sock, ctx, fds, clients);
841 592fd624 2020-10-07 op continue;
842 592fd624 2020-10-07 op }
843 592fd624 2020-10-07 op
844 592fd624 2020-10-07 op handle(&fds[i], &clients[i]);
845 592fd624 2020-10-07 op }
846 592fd624 2020-10-07 op }
847 231bfcdc 2020-12-21 op }
848 231bfcdc 2020-12-21 op
849 231bfcdc 2020-12-21 op char *
850 231bfcdc 2020-12-21 op absolutify_path(const char *path)
851 231bfcdc 2020-12-21 op {
852 231bfcdc 2020-12-21 op char *wd, *r;
853 231bfcdc 2020-12-21 op
854 231bfcdc 2020-12-21 op if (*path == '/')
855 231bfcdc 2020-12-21 op return strdup(path);
856 231bfcdc 2020-12-21 op
857 231bfcdc 2020-12-21 op wd = getwd(NULL);
858 231bfcdc 2020-12-21 op if (asprintf(&r, "%s/%s", wd, path) == -1)
859 231bfcdc 2020-12-21 op err(1, "asprintf");
860 231bfcdc 2020-12-21 op free(wd);
861 231bfcdc 2020-12-21 op return r;
862 592fd624 2020-10-07 op }
863 592fd624 2020-10-07 op
864 592fd624 2020-10-07 op void
865 3e4749f7 2020-10-02 op usage(const char *me)
866 3e4749f7 2020-10-02 op {
867 3e4749f7 2020-10-02 op fprintf(stderr,
868 72f653b6 2020-11-06 op "USAGE: %s [-h] [-c cert.pem] [-d docs] [-k key.pem] "
869 721e2325 2020-11-18 op "[-l logfile] [-p port] [-x cgi-bin]\n",
870 3e4749f7 2020-10-02 op me);
871 3e4749f7 2020-10-02 op }
872 3e4749f7 2020-10-02 op
873 3e4749f7 2020-10-02 op int
874 3e4749f7 2020-10-02 op main(int argc, char **argv)
875 3e4749f7 2020-10-02 op {
876 72342dc9 2020-11-06 op const char *cert = "cert.pem", *key = "key.pem";
877 3e4749f7 2020-10-02 op struct tls *ctx = NULL;
878 3e4749f7 2020-10-02 op struct tls_config *conf;
879 3e4749f7 2020-10-02 op int sock, ch;
880 3e4749f7 2020-10-02 op
881 0cf902af 2020-11-03 op signal(SIGPIPE, SIG_IGN);
882 72342dc9 2020-11-06 op signal(SIGCHLD, SIG_IGN);
883 0cf902af 2020-11-03 op
884 120381c9 2020-11-06 op #ifdef SIGINFO
885 120381c9 2020-11-06 op signal(SIGINFO, siginfo_handler);
886 120381c9 2020-11-06 op #endif
887 120381c9 2020-11-06 op signal(SIGUSR2, siginfo_handler);
888 120381c9 2020-11-06 op
889 120381c9 2020-11-06 op connected_clients = 0;
890 120381c9 2020-11-06 op
891 51d876f0 2020-12-21 op if ((dir = absolutify_path("docs")) == NULL)
892 51d876f0 2020-12-21 op err(1, "absolutify_path");
893 51d876f0 2020-12-21 op
894 72f653b6 2020-11-06 op cgi = NULL;
895 721e2325 2020-11-18 op port = 1965;
896 d7802bb4 2020-12-02 op foreground = 0;
897 2c3a40fa 2020-11-06 op
898 d7802bb4 2020-12-02 op while ((ch = getopt(argc, argv, "c:d:fhk:p:x:")) != -1) {
899 3e4749f7 2020-10-02 op switch (ch) {
900 3e4749f7 2020-10-02 op case 'c':
901 3e4749f7 2020-10-02 op cert = optarg;
902 3e4749f7 2020-10-02 op break;
903 3e4749f7 2020-10-02 op
904 3e4749f7 2020-10-02 op case 'd':
905 231bfcdc 2020-12-21 op free((char*)dir);
906 231bfcdc 2020-12-21 op if ((dir = absolutify_path(optarg)) == NULL)
907 231bfcdc 2020-12-21 op err(1, "absolutify_path");
908 3e4749f7 2020-10-02 op break;
909 3e4749f7 2020-10-02 op
910 d7802bb4 2020-12-02 op case 'f':
911 d7802bb4 2020-12-02 op foreground = 1;
912 d7802bb4 2020-12-02 op break;
913 d7802bb4 2020-12-02 op
914 3e4749f7 2020-10-02 op case 'h':
915 3e4749f7 2020-10-02 op usage(*argv);
916 3e4749f7 2020-10-02 op return 0;
917 3e4749f7 2020-10-02 op
918 3e4749f7 2020-10-02 op case 'k':
919 3e4749f7 2020-10-02 op key = optarg;
920 3e4749f7 2020-10-02 op break;
921 3e4749f7 2020-10-02 op
922 721e2325 2020-11-18 op case 'p': {
923 721e2325 2020-11-18 op char *ep;
924 721e2325 2020-11-18 op long lval;
925 721e2325 2020-11-18 op
926 721e2325 2020-11-18 op errno = 0;
927 721e2325 2020-11-18 op lval = strtol(optarg, &ep, 10);
928 721e2325 2020-11-18 op if (optarg[0] == '\0' || *ep != '\0')
929 721e2325 2020-11-18 op err(1, "not a number: %s", optarg);
930 721e2325 2020-11-18 op if (lval < 0 || lval > UINT16_MAX)
931 721e2325 2020-11-18 op err(1, "port number out of range: %s", optarg);
932 721e2325 2020-11-18 op port = lval;
933 721e2325 2020-11-18 op break;
934 721e2325 2020-11-18 op }
935 721e2325 2020-11-18 op
936 72342dc9 2020-11-06 op case 'x':
937 72f653b6 2020-11-06 op cgi = optarg;
938 72342dc9 2020-11-06 op break;
939 72342dc9 2020-11-06 op
940 3e4749f7 2020-10-02 op default:
941 3e4749f7 2020-10-02 op usage(*argv);
942 3e4749f7 2020-10-02 op return 1;
943 3e4749f7 2020-10-02 op }
944 3e4749f7 2020-10-02 op }
945 3e4749f7 2020-10-02 op
946 3e4749f7 2020-10-02 op if ((conf = tls_config_new()) == NULL)
947 3e4749f7 2020-10-02 op err(1, "tls_config_new");
948 0d8ca45a 2020-10-03 op
949 677afbd3 2020-12-02 op /* optionally accept client certs, but don't try to verify them */
950 677afbd3 2020-12-02 op tls_config_verify_client_optional(conf);
951 677afbd3 2020-12-02 op tls_config_insecure_noverifycert(conf);
952 677afbd3 2020-12-02 op
953 0d8ca45a 2020-10-03 op if (tls_config_set_protocols(conf,
954 0d8ca45a 2020-10-03 op TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3) == -1)
955 0d8ca45a 2020-10-03 op err(1, "tls_config_set_protocols");
956 3e4749f7 2020-10-02 op
957 4d4f0e19 2020-10-03 op if (tls_config_set_cert_file(conf, cert) == -1)
958 4d4f0e19 2020-10-03 op err(1, "tls_config_set_cert_file: %s", cert);
959 3e4749f7 2020-10-02 op
960 4d4f0e19 2020-10-03 op if (tls_config_set_key_file(conf, key) == -1)
961 4d4f0e19 2020-10-03 op err(1, "tls_config_set_key_file: %s", key);
962 3e4749f7 2020-10-02 op
963 3e4749f7 2020-10-02 op if ((ctx = tls_server()) == NULL)
964 3e4749f7 2020-10-02 op err(1, "tls_server");
965 3e4749f7 2020-10-02 op
966 3e4749f7 2020-10-02 op if (tls_configure(ctx, conf) == -1)
967 3e4749f7 2020-10-02 op errx(1, "tls_configure: %s", tls_error(ctx));
968 3e4749f7 2020-10-02 op
969 721e2325 2020-11-18 op sock = make_socket(port, AF_INET);
970 3e4749f7 2020-10-02 op
971 3e4749f7 2020-10-02 op if ((dirfd = open(dir, O_RDONLY | O_DIRECTORY)) == -1)
972 3e4749f7 2020-10-02 op err(1, "open: %s", dir);
973 3e4749f7 2020-10-02 op
974 d7802bb4 2020-12-02 op if (!foreground && daemon(0, 1) == -1)
975 d7802bb4 2020-12-02 op exit(1);
976 d7802bb4 2020-12-02 op
977 72f653b6 2020-11-06 op if (cgi != NULL) {
978 72f653b6 2020-11-06 op if (unveil(dir, "rx") == -1)
979 72f653b6 2020-11-06 op err(1, "unveil");
980 72f653b6 2020-11-06 op if (pledge("stdio rpath inet proc exec", NULL) == -1)
981 72f653b6 2020-11-06 op err(1, "pledge");
982 72f653b6 2020-11-06 op } else {
983 72f653b6 2020-11-06 op if (unveil(dir, "r") == -1)
984 72f653b6 2020-11-06 op err(1, "unveil");
985 72f653b6 2020-11-06 op if (pledge("stdio rpath inet", NULL) == -1)
986 72f653b6 2020-11-06 op err(1, "pledge");
987 72f653b6 2020-11-06 op }
988 3e4749f7 2020-10-02 op
989 3e4749f7 2020-10-02 op loop(ctx, sock);
990 3e4749f7 2020-10-02 op
991 3e4749f7 2020-10-02 op close(sock);
992 3e4749f7 2020-10-02 op tls_free(ctx);
993 3e4749f7 2020-10-02 op tls_config_free(conf);
994 3e4749f7 2020-10-02 op }