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