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 4a28dd01 2020-12-28 op sig_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 5a40216f 2020-12-28 op if (cgi != NULL && starts_with(fpath, cgi))
222 a5d310bc 2020-11-10 op return start_cgi(fpath, "", query, fds, c);
223 3e4749f7 2020-10-02 op
224 a5d310bc 2020-11-10 op /* fallthrough */
225 a5d310bc 2020-11-10 op
226 a5d310bc 2020-11-10 op case FILE_EXISTS:
227 a5d310bc 2020-11-10 op if ((c->len = filesize(c->fd)) == -1) {
228 d7802bb4 2020-12-02 op LOGE(c, "failed to get file size for %s", fpath);
229 a5d310bc 2020-11-10 op goodbye(fds, c);
230 72342dc9 2020-11-06 op return 0;
231 a5d310bc 2020-11-10 op }
232 72342dc9 2020-11-06 op
233 a5d310bc 2020-11-10 op if ((c->buf = mmap(NULL, c->len, PROT_READ, MAP_PRIVATE,
234 a5d310bc 2020-11-10 op c->fd, 0)) == MAP_FAILED) {
235 a5d310bc 2020-11-10 op warn("mmap: %s", fpath);
236 a5d310bc 2020-11-10 op goodbye(fds, c);
237 a5d310bc 2020-11-10 op return 0;
238 a5d310bc 2020-11-10 op }
239 a5d310bc 2020-11-10 op c->i = c->buf;
240 a5d310bc 2020-11-10 op return start_reply(fds, c, SUCCESS, mime(fpath));
241 a5d310bc 2020-11-10 op
242 a5d310bc 2020-11-10 op case FILE_DIRECTORY:
243 d7802bb4 2020-12-02 op LOGD(c, "%s is a directory, trying %s/index.gmi", fpath, fpath);
244 f28f9311 2020-10-14 op close(c->fd);
245 f28f9311 2020-10-14 op c->fd = -1;
246 f28f9311 2020-10-14 op send_dir(fpath, fds, c);
247 f28f9311 2020-10-14 op return 0;
248 3e4749f7 2020-10-02 op
249 a5d310bc 2020-11-10 op case FILE_MISSING:
250 5a40216f 2020-12-28 op if (cgi != NULL && starts_with(fpath, cgi))
251 a5d310bc 2020-11-10 op return check_for_cgi(fpath, query, fds, c);
252 72342dc9 2020-11-06 op
253 a5d310bc 2020-11-10 op if (!start_reply(fds, c, NOT_FOUND, "not found"))
254 a5d310bc 2020-11-10 op return 0;
255 f28f9311 2020-10-14 op goodbye(fds, c);
256 f28f9311 2020-10-14 op return 0;
257 f28f9311 2020-10-14 op
258 a5d310bc 2020-11-10 op default:
259 a5d310bc 2020-11-10 op /* unreachable */
260 a5d310bc 2020-11-10 op abort();
261 592fd624 2020-10-07 op }
262 72342dc9 2020-11-06 op }
263 72342dc9 2020-11-06 op
264 a5d310bc 2020-11-10 op int
265 a5d310bc 2020-11-10 op start_cgi(const char *spath, const char *relpath, const char *query,
266 75d233f0 2020-11-06 op struct pollfd *fds, struct client *c)
267 72342dc9 2020-11-06 op {
268 72342dc9 2020-11-06 op pid_t pid;
269 a5d310bc 2020-11-10 op int p[2]; /* read end, write end */
270 72342dc9 2020-11-06 op
271 72342dc9 2020-11-06 op if (pipe(p) == -1)
272 72342dc9 2020-11-06 op goto err;
273 72342dc9 2020-11-06 op
274 72342dc9 2020-11-06 op switch (pid = fork()) {
275 72342dc9 2020-11-06 op case -1:
276 72342dc9 2020-11-06 op goto err;
277 72342dc9 2020-11-06 op
278 a5d310bc 2020-11-10 op case 0: { /* child */
279 721e2325 2020-11-18 op char *ex, *requri, *portno;
280 72342dc9 2020-11-06 op char addr[INET_ADDRSTRLEN];
281 72342dc9 2020-11-06 op char *argv[] = { NULL, NULL, NULL };
282 72342dc9 2020-11-06 op
283 a5d310bc 2020-11-10 op close(p[0]);
284 72342dc9 2020-11-06 op if (dup2(p[1], 1) == -1)
285 72342dc9 2020-11-06 op goto childerr;
286 72342dc9 2020-11-06 op
287 72342dc9 2020-11-06 op if (inet_ntop(c->af, &c->addr, addr, sizeof(addr)) == NULL)
288 a5d310bc 2020-11-10 op goto childerr;
289 72342dc9 2020-11-06 op
290 721e2325 2020-11-18 op if (asprintf(&portno, "%d", port) == -1)
291 721e2325 2020-11-18 op goto childerr;
292 721e2325 2020-11-18 op
293 5a40216f 2020-12-28 op if (asprintf(&ex, "%s/%s", dir, spath) == -1)
294 72342dc9 2020-11-06 op goto childerr;
295 72342dc9 2020-11-06 op
296 a5d310bc 2020-11-10 op if (asprintf(&requri, "%s%s%s", spath,
297 a5d310bc 2020-11-10 op *relpath == '\0' ? "" : "/",
298 a5d310bc 2020-11-10 op relpath) == -1)
299 a5d310bc 2020-11-10 op goto childerr;
300 a5d310bc 2020-11-10 op
301 a5d310bc 2020-11-10 op argv[0] = argv[1] = ex;
302 a5d310bc 2020-11-10 op
303 60ba426e 2020-11-06 op /* fix the env */
304 677afbd3 2020-12-02 op SAFE_SETENV("GATEWAY_INTERFACE", "CGI/1.1");
305 677afbd3 2020-12-02 op SAFE_SETENV("SERVER_SOFTWARE", "gmid");
306 677afbd3 2020-12-02 op SAFE_SETENV("SERVER_PORT", portno);
307 a5d310bc 2020-11-10 op /* setenv("SERVER_NAME", "", 1); */
308 677afbd3 2020-12-02 op SAFE_SETENV("SCRIPT_NAME", spath);
309 677afbd3 2020-12-02 op SAFE_SETENV("SCRIPT_EXECUTABLE", ex);
310 677afbd3 2020-12-02 op SAFE_SETENV("REQUEST_URI", requri);
311 677afbd3 2020-12-02 op SAFE_SETENV("REQUEST_RELATIVE", relpath);
312 677afbd3 2020-12-02 op SAFE_SETENV("QUERY_STRING", query);
313 677afbd3 2020-12-02 op SAFE_SETENV("REMOTE_HOST", addr);
314 677afbd3 2020-12-02 op SAFE_SETENV("REMOTE_ADDR", addr);
315 677afbd3 2020-12-02 op SAFE_SETENV("DOCUMENT_ROOT", dir);
316 72342dc9 2020-11-06 op
317 677afbd3 2020-12-02 op if (tls_peer_cert_provided(c->ctx)) {
318 677afbd3 2020-12-02 op SAFE_SETENV("AUTH_TYPE", "Certificate");
319 677afbd3 2020-12-02 op SAFE_SETENV("REMOTE_USER", tls_peer_cert_subject(c->ctx));
320 677afbd3 2020-12-02 op SAFE_SETENV("TLS_CLIENT_ISSUER", tls_peer_cert_issuer(c->ctx));
321 677afbd3 2020-12-02 op SAFE_SETENV("TLS_CLIENT_HASH", tls_peer_cert_hash(c->ctx));
322 677afbd3 2020-12-02 op }
323 677afbd3 2020-12-02 op
324 a5d310bc 2020-11-10 op execvp(ex, argv);
325 72342dc9 2020-11-06 op goto childerr;
326 72342dc9 2020-11-06 op }
327 72342dc9 2020-11-06 op
328 72342dc9 2020-11-06 op default: /* parent */
329 a5d310bc 2020-11-10 op close(p[1]);
330 72342dc9 2020-11-06 op close(c->fd);
331 72342dc9 2020-11-06 op c->fd = p[0];
332 72342dc9 2020-11-06 op c->child = pid;
333 aff8d190 2020-11-06 op mark_nonblock(c->fd);
334 aff8d190 2020-11-06 op c->state = S_SENDING;
335 72342dc9 2020-11-06 op handle_cgi(fds, c);
336 a5d310bc 2020-11-10 op return 0;
337 72342dc9 2020-11-06 op }
338 72342dc9 2020-11-06 op
339 72342dc9 2020-11-06 op err:
340 72342dc9 2020-11-06 op if (!start_reply(fds, c, TEMP_FAILURE, "internal server error"))
341 a5d310bc 2020-11-10 op return 0;
342 72342dc9 2020-11-06 op goodbye(fds, c);
343 a5d310bc 2020-11-10 op return 0;
344 72342dc9 2020-11-06 op
345 72342dc9 2020-11-06 op childerr:
346 72342dc9 2020-11-06 op dprintf(p[1], "%d internal server error\r\n", TEMP_FAILURE);
347 72342dc9 2020-11-06 op close(p[1]);
348 a5d310bc 2020-11-10 op _exit(1);
349 72342dc9 2020-11-06 op }
350 72342dc9 2020-11-06 op
351 72342dc9 2020-11-06 op void
352 6c6c7a0e 2020-12-28 op cgi_poll_on_child(struct pollfd *fds, struct client *c)
353 72342dc9 2020-11-06 op {
354 aff8d190 2020-11-06 op int fd;
355 72342dc9 2020-11-06 op
356 aff8d190 2020-11-06 op if (c->waiting_on_child)
357 aff8d190 2020-11-06 op return;
358 aff8d190 2020-11-06 op c->waiting_on_child = 1;
359 aff8d190 2020-11-06 op
360 aff8d190 2020-11-06 op fds->events = POLLIN;
361 aff8d190 2020-11-06 op
362 aff8d190 2020-11-06 op fd = fds->fd;
363 aff8d190 2020-11-06 op fds->fd = c->fd;
364 aff8d190 2020-11-06 op c->fd = fd;
365 aff8d190 2020-11-06 op }
366 aff8d190 2020-11-06 op
367 aff8d190 2020-11-06 op void
368 6c6c7a0e 2020-12-28 op cgi_poll_on_client(struct pollfd *fds, struct client *c)
369 aff8d190 2020-11-06 op {
370 aff8d190 2020-11-06 op int fd;
371 aff8d190 2020-11-06 op
372 aff8d190 2020-11-06 op if (!c->waiting_on_child)
373 aff8d190 2020-11-06 op return;
374 aff8d190 2020-11-06 op c->waiting_on_child = 0;
375 aff8d190 2020-11-06 op
376 aff8d190 2020-11-06 op fd = fds->fd;
377 aff8d190 2020-11-06 op fds->fd = c->fd;
378 aff8d190 2020-11-06 op c->fd = fd;
379 aff8d190 2020-11-06 op }
380 aff8d190 2020-11-06 op
381 aff8d190 2020-11-06 op void
382 aff8d190 2020-11-06 op handle_cgi(struct pollfd *fds, struct client *c)
383 aff8d190 2020-11-06 op {
384 aff8d190 2020-11-06 op ssize_t r;
385 aff8d190 2020-11-06 op
386 aff8d190 2020-11-06 op /* ensure c->fd is the child and fds->fd the client */
387 6c6c7a0e 2020-12-28 op cgi_poll_on_client(fds, c);
388 aff8d190 2020-11-06 op
389 72342dc9 2020-11-06 op while (1) {
390 aff8d190 2020-11-06 op if (c->len == 0) {
391 aff8d190 2020-11-06 op if ((r = read(c->fd, c->sbuf, sizeof(c->sbuf))) == 0)
392 aff8d190 2020-11-06 op goto end;
393 aff8d190 2020-11-06 op if (r == -1) {
394 aff8d190 2020-11-06 op if (errno == EAGAIN || errno == EWOULDBLOCK) {
395 6c6c7a0e 2020-12-28 op cgi_poll_on_child(fds, c);
396 aff8d190 2020-11-06 op return;
397 aff8d190 2020-11-06 op }
398 aff8d190 2020-11-06 op goto end;
399 aff8d190 2020-11-06 op }
400 aff8d190 2020-11-06 op c->len = r;
401 aff8d190 2020-11-06 op c->off = 0;
402 aff8d190 2020-11-06 op }
403 aff8d190 2020-11-06 op
404 aff8d190 2020-11-06 op while (c->len > 0) {
405 aff8d190 2020-11-06 op switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
406 72342dc9 2020-11-06 op case -1:
407 72342dc9 2020-11-06 op goto end;
408 72342dc9 2020-11-06 op
409 72342dc9 2020-11-06 op case TLS_WANT_POLLOUT:
410 aff8d190 2020-11-06 op fds->events = POLLOUT;
411 aff8d190 2020-11-06 op return;
412 aff8d190 2020-11-06 op
413 72342dc9 2020-11-06 op case TLS_WANT_POLLIN:
414 aff8d190 2020-11-06 op fds->events = POLLIN;
415 aff8d190 2020-11-06 op return;
416 72342dc9 2020-11-06 op
417 72342dc9 2020-11-06 op default:
418 aff8d190 2020-11-06 op c->off += r;
419 aff8d190 2020-11-06 op c->len -= r;
420 aff8d190 2020-11-06 op break;
421 72342dc9 2020-11-06 op }
422 72342dc9 2020-11-06 op }
423 72342dc9 2020-11-06 op }
424 72342dc9 2020-11-06 op
425 72342dc9 2020-11-06 op end:
426 72342dc9 2020-11-06 op goodbye(fds, c);
427 f28f9311 2020-10-14 op }
428 592fd624 2020-10-07 op
429 f28f9311 2020-10-14 op void
430 75d233f0 2020-11-06 op send_file(char *path, char *query, struct pollfd *fds, struct client *c)
431 f28f9311 2020-10-14 op {
432 f28f9311 2020-10-14 op ssize_t ret, len;
433 592fd624 2020-10-07 op
434 f28f9311 2020-10-14 op if (c->fd == -1) {
435 75d233f0 2020-11-06 op if (!open_file(path, query, fds, c))
436 f28f9311 2020-10-14 op return;
437 f28f9311 2020-10-14 op c->state = S_SENDING;
438 f28f9311 2020-10-14 op }
439 592fd624 2020-10-07 op
440 f28f9311 2020-10-14 op len = (c->buf + c->len) - c->i;
441 f28f9311 2020-10-14 op
442 f28f9311 2020-10-14 op while (len > 0) {
443 f28f9311 2020-10-14 op switch (ret = tls_write(c->ctx, c->i, len)) {
444 f28f9311 2020-10-14 op case -1:
445 d7802bb4 2020-12-02 op LOGE(c, "tls_write: %s", tls_error(c->ctx));
446 f28f9311 2020-10-14 op goodbye(fds, c);
447 f28f9311 2020-10-14 op return;
448 f28f9311 2020-10-14 op
449 f28f9311 2020-10-14 op case TLS_WANT_POLLIN:
450 f28f9311 2020-10-14 op fds->events = POLLIN;
451 f28f9311 2020-10-14 op return;
452 f28f9311 2020-10-14 op
453 f28f9311 2020-10-14 op case TLS_WANT_POLLOUT:
454 f28f9311 2020-10-14 op fds->events = POLLOUT;
455 f28f9311 2020-10-14 op return;
456 f28f9311 2020-10-14 op
457 f28f9311 2020-10-14 op default:
458 f28f9311 2020-10-14 op c->i += ret;
459 f28f9311 2020-10-14 op len -= ret;
460 f28f9311 2020-10-14 op break;
461 3e4749f7 2020-10-02 op }
462 3e4749f7 2020-10-02 op }
463 f28f9311 2020-10-14 op
464 f28f9311 2020-10-14 op goodbye(fds, c);
465 3e4749f7 2020-10-02 op }
466 3e4749f7 2020-10-02 op
467 3e4749f7 2020-10-02 op void
468 592fd624 2020-10-07 op send_dir(char *path, struct pollfd *fds, struct client *client)
469 3e4749f7 2020-10-02 op {
470 3e4749f7 2020-10-02 op char fpath[PATHBUF];
471 3e4749f7 2020-10-02 op size_t len;
472 3e4749f7 2020-10-02 op
473 3e4749f7 2020-10-02 op bzero(fpath, PATHBUF);
474 3e4749f7 2020-10-02 op
475 9c56b0a7 2020-10-14 op if (path[0] != '.')
476 3e4749f7 2020-10-02 op fpath[0] = '.';
477 3e4749f7 2020-10-02 op
478 3e4749f7 2020-10-02 op /* this cannot fail since sizeof(fpath) > maxlen of path */
479 3e4749f7 2020-10-02 op strlcat(fpath, path, PATHBUF);
480 3e4749f7 2020-10-02 op len = strlen(fpath);
481 3e4749f7 2020-10-02 op
482 3e4749f7 2020-10-02 op /* add a trailing / in case. */
483 3e4749f7 2020-10-02 op if (fpath[len-1] != '/') {
484 3e4749f7 2020-10-02 op fpath[len] = '/';
485 3e4749f7 2020-10-02 op }
486 3e4749f7 2020-10-02 op
487 3e4749f7 2020-10-02 op strlcat(fpath, "index.gmi", sizeof(fpath));
488 3e4749f7 2020-10-02 op
489 75d233f0 2020-11-06 op send_file(fpath, NULL, fds, client);
490 3e4749f7 2020-10-02 op }
491 3e4749f7 2020-10-02 op
492 3e4749f7 2020-10-02 op void
493 592fd624 2020-10-07 op handle(struct pollfd *fds, struct client *client)
494 3e4749f7 2020-10-02 op {
495 592fd624 2020-10-07 op char buf[GEMINI_URL_LEN];
496 33d32d1f 2020-12-25 op const char *parse_err;
497 33d32d1f 2020-12-25 op struct uri uri;
498 3e4749f7 2020-10-02 op
499 592fd624 2020-10-07 op switch (client->state) {
500 592fd624 2020-10-07 op case S_OPEN:
501 592fd624 2020-10-07 op bzero(buf, GEMINI_URL_LEN);
502 592fd624 2020-10-07 op switch (tls_read(client->ctx, buf, sizeof(buf)-1)) {
503 592fd624 2020-10-07 op case -1:
504 d7802bb4 2020-12-02 op LOGE(client, "tls_read: %s", tls_error(client->ctx));
505 592fd624 2020-10-07 op goodbye(fds, client);
506 592fd624 2020-10-07 op return;
507 3e4749f7 2020-10-02 op
508 592fd624 2020-10-07 op case TLS_WANT_POLLIN:
509 592fd624 2020-10-07 op fds->events = POLLIN;
510 592fd624 2020-10-07 op return;
511 3e4749f7 2020-10-02 op
512 592fd624 2020-10-07 op case TLS_WANT_POLLOUT:
513 592fd624 2020-10-07 op fds->events = POLLOUT;
514 592fd624 2020-10-07 op return;
515 592fd624 2020-10-07 op }
516 3e4749f7 2020-10-02 op
517 b3deee7b 2020-12-28 op parse_err = "invalid request";
518 33d32d1f 2020-12-25 op if (!trim_req_uri(buf) || !parse_uri(buf, &uri, &parse_err)) {
519 33d32d1f 2020-12-25 op if (!start_reply(fds, client, BAD_REQUEST, parse_err))
520 592fd624 2020-10-07 op return;
521 592fd624 2020-10-07 op goodbye(fds, client);
522 592fd624 2020-10-07 op return;
523 592fd624 2020-10-07 op }
524 592fd624 2020-10-07 op
525 33d32d1f 2020-12-25 op LOGI(client, "GET %s%s%s",
526 33d32d1f 2020-12-25 op *uri.path ? uri.path : "/",
527 33d32d1f 2020-12-25 op *uri.query ? "?" : "",
528 33d32d1f 2020-12-25 op *uri.query ? uri.query : "");
529 592fd624 2020-10-07 op
530 33d32d1f 2020-12-25 op send_file(uri.path, uri.query, fds, client);
531 592fd624 2020-10-07 op break;
532 592fd624 2020-10-07 op
533 592fd624 2020-10-07 op case S_INITIALIZING:
534 592fd624 2020-10-07 op if (!start_reply(fds, client, client->code, client->meta))
535 592fd624 2020-10-07 op return;
536 592fd624 2020-10-07 op
537 592fd624 2020-10-07 op if (client->code != SUCCESS) {
538 592fd624 2020-10-07 op /* we don't need a body */
539 592fd624 2020-10-07 op goodbye(fds, client);
540 592fd624 2020-10-07 op return;
541 592fd624 2020-10-07 op }
542 592fd624 2020-10-07 op
543 592fd624 2020-10-07 op client->state = S_SENDING;
544 592fd624 2020-10-07 op
545 592fd624 2020-10-07 op /* fallthrough */
546 592fd624 2020-10-07 op
547 592fd624 2020-10-07 op case S_SENDING:
548 aff8d190 2020-11-06 op if (client->child != -1)
549 aff8d190 2020-11-06 op handle_cgi(fds, client);
550 aff8d190 2020-11-06 op else
551 aff8d190 2020-11-06 op send_file(NULL, NULL, fds, client);
552 592fd624 2020-10-07 op break;
553 592fd624 2020-10-07 op
554 592fd624 2020-10-07 op case S_CLOSING:
555 592fd624 2020-10-07 op goodbye(fds, client);
556 592fd624 2020-10-07 op break;
557 592fd624 2020-10-07 op
558 592fd624 2020-10-07 op default:
559 592fd624 2020-10-07 op /* unreachable */
560 592fd624 2020-10-07 op abort();
561 592fd624 2020-10-07 op }
562 3e4749f7 2020-10-02 op }
563 3e4749f7 2020-10-02 op
564 592fd624 2020-10-07 op void
565 592fd624 2020-10-07 op mark_nonblock(int fd)
566 592fd624 2020-10-07 op {
567 592fd624 2020-10-07 op int flags;
568 592fd624 2020-10-07 op
569 592fd624 2020-10-07 op if ((flags = fcntl(fd, F_GETFL)) == -1)
570 d7802bb4 2020-12-02 op FATAL("fcntl(F_GETFL): %s", strerror(errno));
571 592fd624 2020-10-07 op if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
572 d7802bb4 2020-12-02 op FATAL("fcntl(F_SETFL): %s", strerror(errno));
573 592fd624 2020-10-07 op }
574 592fd624 2020-10-07 op
575 3e4749f7 2020-10-02 op int
576 9468027b 2020-10-15 op make_socket(int port, int family)
577 3e4749f7 2020-10-02 op {
578 3e4749f7 2020-10-02 op int sock, v;
579 9468027b 2020-10-15 op struct sockaddr_in addr4;
580 9468027b 2020-10-15 op struct sockaddr_in6 addr6;
581 9468027b 2020-10-15 op struct sockaddr *addr;
582 9468027b 2020-10-15 op socklen_t len;
583 3e4749f7 2020-10-02 op
584 9468027b 2020-10-15 op switch (family) {
585 9468027b 2020-10-15 op case AF_INET:
586 9468027b 2020-10-15 op bzero(&addr4, sizeof(addr4));
587 9468027b 2020-10-15 op addr4.sin_family = family;
588 9468027b 2020-10-15 op addr4.sin_port = htons(port);
589 9468027b 2020-10-15 op addr4.sin_addr.s_addr = INADDR_ANY;
590 9468027b 2020-10-15 op addr = (struct sockaddr*)&addr4;
591 9468027b 2020-10-15 op len = sizeof(addr4);
592 9468027b 2020-10-15 op break;
593 9468027b 2020-10-15 op
594 9468027b 2020-10-15 op case AF_INET6:
595 9468027b 2020-10-15 op bzero(&addr6, sizeof(addr6));
596 9468027b 2020-10-15 op addr6.sin6_family = AF_INET6;
597 9468027b 2020-10-15 op addr6.sin6_port = htons(port);
598 9468027b 2020-10-15 op addr6.sin6_addr = in6addr_any;
599 9468027b 2020-10-15 op addr = (struct sockaddr*)&addr6;
600 9468027b 2020-10-15 op len = sizeof(addr6);
601 9468027b 2020-10-15 op break;
602 9468027b 2020-10-15 op
603 9468027b 2020-10-15 op default:
604 9468027b 2020-10-15 op /* unreachable */
605 9468027b 2020-10-15 op abort();
606 9468027b 2020-10-15 op }
607 9468027b 2020-10-15 op
608 9468027b 2020-10-15 op if ((sock = socket(family, SOCK_STREAM, 0)) == -1)
609 d7802bb4 2020-12-02 op FATAL("socket: %s", strerror(errno));
610 3e4749f7 2020-10-02 op
611 3e4749f7 2020-10-02 op v = 1;
612 3e4749f7 2020-10-02 op if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
613 d7802bb4 2020-12-02 op FATAL("setsockopt(SO_REUSEADDR): %s", strerror(errno));
614 3e4749f7 2020-10-02 op
615 3e4749f7 2020-10-02 op v = 1;
616 3e4749f7 2020-10-02 op if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
617 d7802bb4 2020-12-02 op FATAL("setsockopt(SO_REUSEPORT): %s", strerror(errno));
618 3e4749f7 2020-10-02 op
619 592fd624 2020-10-07 op mark_nonblock(sock);
620 592fd624 2020-10-07 op
621 9468027b 2020-10-15 op if (bind(sock, addr, len) == -1)
622 d7802bb4 2020-12-02 op FATAL("bind: %s", strerror(errno));
623 3e4749f7 2020-10-02 op
624 3e4749f7 2020-10-02 op if (listen(sock, 16) == -1)
625 d7802bb4 2020-12-02 op FATAL("listen: %s", strerror(errno));
626 3e4749f7 2020-10-02 op
627 3e4749f7 2020-10-02 op return sock;
628 3e4749f7 2020-10-02 op }
629 3e4749f7 2020-10-02 op
630 3e4749f7 2020-10-02 op void
631 592fd624 2020-10-07 op do_accept(int sock, struct tls *ctx, struct pollfd *fds, struct client *clients)
632 3e4749f7 2020-10-02 op {
633 592fd624 2020-10-07 op int i, fd;
634 592fd624 2020-10-07 op struct sockaddr_in addr;
635 3e4749f7 2020-10-02 op socklen_t len;
636 3e4749f7 2020-10-02 op
637 592fd624 2020-10-07 op len = sizeof(addr);
638 592fd624 2020-10-07 op if ((fd = accept(sock, (struct sockaddr*)&addr, &len)) == -1) {
639 592fd624 2020-10-07 op if (errno == EWOULDBLOCK)
640 592fd624 2020-10-07 op return;
641 d7802bb4 2020-12-02 op FATAL("accept: %s", strerror(errno));
642 592fd624 2020-10-07 op }
643 3e4749f7 2020-10-02 op
644 592fd624 2020-10-07 op mark_nonblock(fd);
645 3e4749f7 2020-10-02 op
646 592fd624 2020-10-07 op for (i = 0; i < MAX_USERS; ++i) {
647 592fd624 2020-10-07 op if (fds[i].fd == -1) {
648 592fd624 2020-10-07 op bzero(&clients[i], sizeof(struct client));
649 592fd624 2020-10-07 op if (tls_accept_socket(ctx, &clients[i].ctx, fd) == -1)
650 592fd624 2020-10-07 op break; /* goodbye fd! */
651 592fd624 2020-10-07 op
652 592fd624 2020-10-07 op fds[i].fd = fd;
653 592fd624 2020-10-07 op fds[i].events = POLLIN;
654 592fd624 2020-10-07 op
655 592fd624 2020-10-07 op clients[i].state = S_OPEN;
656 592fd624 2020-10-07 op clients[i].fd = -1;
657 72342dc9 2020-11-06 op clients[i].child = -1;
658 f28f9311 2020-10-14 op clients[i].buf = MAP_FAILED;
659 2c3a40fa 2020-11-06 op clients[i].af = AF_INET;
660 2c3a40fa 2020-11-06 op clients[i].addr = addr.sin_addr;
661 592fd624 2020-10-07 op
662 120381c9 2020-11-06 op connected_clients++;
663 592fd624 2020-10-07 op return;
664 3e4749f7 2020-10-02 op }
665 592fd624 2020-10-07 op }
666 3e4749f7 2020-10-02 op
667 592fd624 2020-10-07 op close(fd);
668 592fd624 2020-10-07 op }
669 3e4749f7 2020-10-02 op
670 592fd624 2020-10-07 op void
671 592fd624 2020-10-07 op goodbye(struct pollfd *pfd, struct client *c)
672 592fd624 2020-10-07 op {
673 592fd624 2020-10-07 op ssize_t ret;
674 592fd624 2020-10-07 op
675 592fd624 2020-10-07 op c->state = S_CLOSING;
676 592fd624 2020-10-07 op
677 592fd624 2020-10-07 op ret = tls_close(c->ctx);
678 592fd624 2020-10-07 op if (ret == TLS_WANT_POLLIN) {
679 592fd624 2020-10-07 op pfd->events = POLLIN;
680 592fd624 2020-10-07 op return;
681 3e4749f7 2020-10-02 op }
682 592fd624 2020-10-07 op if (ret == TLS_WANT_POLLOUT) {
683 592fd624 2020-10-07 op pfd->events = POLLOUT;
684 592fd624 2020-10-07 op return;
685 592fd624 2020-10-07 op }
686 592fd624 2020-10-07 op
687 120381c9 2020-11-06 op connected_clients--;
688 120381c9 2020-11-06 op
689 592fd624 2020-10-07 op tls_free(c->ctx);
690 592fd624 2020-10-07 op c->ctx = NULL;
691 592fd624 2020-10-07 op
692 f28f9311 2020-10-14 op if (c->buf != MAP_FAILED)
693 f28f9311 2020-10-14 op munmap(c->buf, c->len);
694 f28f9311 2020-10-14 op
695 592fd624 2020-10-07 op if (c->fd != -1)
696 592fd624 2020-10-07 op close(c->fd);
697 592fd624 2020-10-07 op
698 592fd624 2020-10-07 op close(pfd->fd);
699 592fd624 2020-10-07 op pfd->fd = -1;
700 3e4749f7 2020-10-02 op }
701 3e4749f7 2020-10-02 op
702 3e4749f7 2020-10-02 op void
703 592fd624 2020-10-07 op loop(struct tls *ctx, int sock)
704 592fd624 2020-10-07 op {
705 16ace04d 2021-01-08 op int i;
706 592fd624 2020-10-07 op struct client clients[MAX_USERS];
707 592fd624 2020-10-07 op struct pollfd fds[MAX_USERS];
708 592fd624 2020-10-07 op
709 592fd624 2020-10-07 op for (i = 0; i < MAX_USERS; ++i) {
710 592fd624 2020-10-07 op fds[i].fd = -1;
711 592fd624 2020-10-07 op fds[i].events = POLLIN;
712 592fd624 2020-10-07 op bzero(&clients[i], sizeof(struct client));
713 592fd624 2020-10-07 op }
714 592fd624 2020-10-07 op
715 592fd624 2020-10-07 op fds[0].fd = sock;
716 592fd624 2020-10-07 op
717 592fd624 2020-10-07 op for (;;) {
718 16ace04d 2021-01-08 op if (poll(fds, MAX_USERS, INFTIM) == -1) {
719 120381c9 2020-11-06 op if (errno == EINTR) {
720 d7802bb4 2020-12-02 op warnx("connected clients: %d",
721 d7802bb4 2020-12-02 op connected_clients);
722 120381c9 2020-11-06 op continue;
723 120381c9 2020-11-06 op }
724 d7802bb4 2020-12-02 op FATAL("poll: %s", strerror(errno));
725 120381c9 2020-11-06 op }
726 592fd624 2020-10-07 op
727 592fd624 2020-10-07 op for (i = 0; i < MAX_USERS; i++) {
728 592fd624 2020-10-07 op if (fds[i].revents == 0)
729 592fd624 2020-10-07 op continue;
730 592fd624 2020-10-07 op
731 592fd624 2020-10-07 op if (fds[i].revents & (POLLERR|POLLNVAL))
732 d7802bb4 2020-12-02 op FATAL("bad fd %d: %s", fds[i].fd,
733 d7802bb4 2020-12-02 op strerror(errno));
734 592fd624 2020-10-07 op
735 592fd624 2020-10-07 op if (fds[i].revents & POLLHUP) {
736 aff8d190 2020-11-06 op /* fds[i] may be the fd of the stdin
737 aff8d190 2020-11-06 op * of a cgi script that has exited. */
738 aff8d190 2020-11-06 op if (!clients[i].waiting_on_child) {
739 aff8d190 2020-11-06 op goodbye(&fds[i], &clients[i]);
740 aff8d190 2020-11-06 op continue;
741 aff8d190 2020-11-06 op }
742 592fd624 2020-10-07 op }
743 592fd624 2020-10-07 op
744 592fd624 2020-10-07 op if (i == 0) { /* new client */
745 592fd624 2020-10-07 op do_accept(sock, ctx, fds, clients);
746 592fd624 2020-10-07 op continue;
747 592fd624 2020-10-07 op }
748 592fd624 2020-10-07 op
749 592fd624 2020-10-07 op handle(&fds[i], &clients[i]);
750 592fd624 2020-10-07 op }
751 592fd624 2020-10-07 op }
752 231bfcdc 2020-12-21 op }
753 231bfcdc 2020-12-21 op
754 231bfcdc 2020-12-21 op char *
755 231bfcdc 2020-12-21 op absolutify_path(const char *path)
756 231bfcdc 2020-12-21 op {
757 231bfcdc 2020-12-21 op char *wd, *r;
758 231bfcdc 2020-12-21 op
759 231bfcdc 2020-12-21 op if (*path == '/')
760 231bfcdc 2020-12-21 op return strdup(path);
761 231bfcdc 2020-12-21 op
762 231bfcdc 2020-12-21 op wd = getwd(NULL);
763 231bfcdc 2020-12-21 op if (asprintf(&r, "%s/%s", wd, path) == -1)
764 231bfcdc 2020-12-21 op err(1, "asprintf");
765 231bfcdc 2020-12-21 op free(wd);
766 231bfcdc 2020-12-21 op return r;
767 592fd624 2020-10-07 op }
768 592fd624 2020-10-07 op
769 592fd624 2020-10-07 op void
770 3e4749f7 2020-10-02 op usage(const char *me)
771 3e4749f7 2020-10-02 op {
772 3e4749f7 2020-10-02 op fprintf(stderr,
773 72f653b6 2020-11-06 op "USAGE: %s [-h] [-c cert.pem] [-d docs] [-k key.pem] "
774 721e2325 2020-11-18 op "[-l logfile] [-p port] [-x cgi-bin]\n",
775 3e4749f7 2020-10-02 op me);
776 3e4749f7 2020-10-02 op }
777 3e4749f7 2020-10-02 op
778 3e4749f7 2020-10-02 op int
779 3e4749f7 2020-10-02 op main(int argc, char **argv)
780 3e4749f7 2020-10-02 op {
781 72342dc9 2020-11-06 op const char *cert = "cert.pem", *key = "key.pem";
782 3e4749f7 2020-10-02 op struct tls *ctx = NULL;
783 3e4749f7 2020-10-02 op struct tls_config *conf;
784 3e4749f7 2020-10-02 op int sock, ch;
785 120381c9 2020-11-06 op connected_clients = 0;
786 120381c9 2020-11-06 op
787 51d876f0 2020-12-21 op if ((dir = absolutify_path("docs")) == NULL)
788 51d876f0 2020-12-21 op err(1, "absolutify_path");
789 51d876f0 2020-12-21 op
790 72f653b6 2020-11-06 op cgi = NULL;
791 721e2325 2020-11-18 op port = 1965;
792 d7802bb4 2020-12-02 op foreground = 0;
793 2c3a40fa 2020-11-06 op
794 d7802bb4 2020-12-02 op while ((ch = getopt(argc, argv, "c:d:fhk:p:x:")) != -1) {
795 3e4749f7 2020-10-02 op switch (ch) {
796 3e4749f7 2020-10-02 op case 'c':
797 3e4749f7 2020-10-02 op cert = optarg;
798 3e4749f7 2020-10-02 op break;
799 3e4749f7 2020-10-02 op
800 3e4749f7 2020-10-02 op case 'd':
801 231bfcdc 2020-12-21 op free((char*)dir);
802 231bfcdc 2020-12-21 op if ((dir = absolutify_path(optarg)) == NULL)
803 231bfcdc 2020-12-21 op err(1, "absolutify_path");
804 3e4749f7 2020-10-02 op break;
805 3e4749f7 2020-10-02 op
806 d7802bb4 2020-12-02 op case 'f':
807 d7802bb4 2020-12-02 op foreground = 1;
808 d7802bb4 2020-12-02 op break;
809 d7802bb4 2020-12-02 op
810 3e4749f7 2020-10-02 op case 'h':
811 3e4749f7 2020-10-02 op usage(*argv);
812 3e4749f7 2020-10-02 op return 0;
813 3e4749f7 2020-10-02 op
814 3e4749f7 2020-10-02 op case 'k':
815 3e4749f7 2020-10-02 op key = optarg;
816 3e4749f7 2020-10-02 op break;
817 3e4749f7 2020-10-02 op
818 721e2325 2020-11-18 op case 'p': {
819 721e2325 2020-11-18 op char *ep;
820 721e2325 2020-11-18 op long lval;
821 721e2325 2020-11-18 op
822 721e2325 2020-11-18 op errno = 0;
823 721e2325 2020-11-18 op lval = strtol(optarg, &ep, 10);
824 721e2325 2020-11-18 op if (optarg[0] == '\0' || *ep != '\0')
825 721e2325 2020-11-18 op err(1, "not a number: %s", optarg);
826 721e2325 2020-11-18 op if (lval < 0 || lval > UINT16_MAX)
827 721e2325 2020-11-18 op err(1, "port number out of range: %s", optarg);
828 721e2325 2020-11-18 op port = lval;
829 721e2325 2020-11-18 op break;
830 721e2325 2020-11-18 op }
831 721e2325 2020-11-18 op
832 72342dc9 2020-11-06 op case 'x':
833 72f653b6 2020-11-06 op cgi = optarg;
834 72342dc9 2020-11-06 op break;
835 72342dc9 2020-11-06 op
836 3e4749f7 2020-10-02 op default:
837 3e4749f7 2020-10-02 op usage(*argv);
838 3e4749f7 2020-10-02 op return 1;
839 3e4749f7 2020-10-02 op }
840 3e4749f7 2020-10-02 op }
841 3e4749f7 2020-10-02 op
842 4a28dd01 2020-12-28 op signal(SIGPIPE, SIG_IGN);
843 4a28dd01 2020-12-28 op signal(SIGCHLD, SIG_IGN);
844 4a28dd01 2020-12-28 op
845 4a28dd01 2020-12-28 op #ifdef SIGINFO
846 4a28dd01 2020-12-28 op signal(SIGINFO, sig_handler);
847 4a28dd01 2020-12-28 op #endif
848 4a28dd01 2020-12-28 op signal(SIGUSR2, sig_handler);
849 4a28dd01 2020-12-28 op
850 4a28dd01 2020-12-28 op if (!foreground)
851 4a28dd01 2020-12-28 op signal(SIGHUP, SIG_IGN);
852 4a28dd01 2020-12-28 op
853 3e4749f7 2020-10-02 op if ((conf = tls_config_new()) == NULL)
854 3e4749f7 2020-10-02 op err(1, "tls_config_new");
855 0d8ca45a 2020-10-03 op
856 677afbd3 2020-12-02 op /* optionally accept client certs, but don't try to verify them */
857 677afbd3 2020-12-02 op tls_config_verify_client_optional(conf);
858 677afbd3 2020-12-02 op tls_config_insecure_noverifycert(conf);
859 677afbd3 2020-12-02 op
860 0d8ca45a 2020-10-03 op if (tls_config_set_protocols(conf,
861 0d8ca45a 2020-10-03 op TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3) == -1)
862 0d8ca45a 2020-10-03 op err(1, "tls_config_set_protocols");
863 3e4749f7 2020-10-02 op
864 4d4f0e19 2020-10-03 op if (tls_config_set_cert_file(conf, cert) == -1)
865 4d4f0e19 2020-10-03 op err(1, "tls_config_set_cert_file: %s", cert);
866 3e4749f7 2020-10-02 op
867 4d4f0e19 2020-10-03 op if (tls_config_set_key_file(conf, key) == -1)
868 4d4f0e19 2020-10-03 op err(1, "tls_config_set_key_file: %s", key);
869 3e4749f7 2020-10-02 op
870 3e4749f7 2020-10-02 op if ((ctx = tls_server()) == NULL)
871 3e4749f7 2020-10-02 op err(1, "tls_server");
872 3e4749f7 2020-10-02 op
873 3e4749f7 2020-10-02 op if (tls_configure(ctx, conf) == -1)
874 3e4749f7 2020-10-02 op errx(1, "tls_configure: %s", tls_error(ctx));
875 3e4749f7 2020-10-02 op
876 721e2325 2020-11-18 op sock = make_socket(port, AF_INET);
877 3e4749f7 2020-10-02 op
878 3e4749f7 2020-10-02 op if ((dirfd = open(dir, O_RDONLY | O_DIRECTORY)) == -1)
879 3e4749f7 2020-10-02 op err(1, "open: %s", dir);
880 3e4749f7 2020-10-02 op
881 d7802bb4 2020-12-02 op if (!foreground && daemon(0, 1) == -1)
882 d7802bb4 2020-12-02 op exit(1);
883 d7802bb4 2020-12-02 op
884 4c416739 2021-01-01 op if (unveil(dir, "rx") == -1)
885 4c416739 2021-01-01 op err(1, "unveil");
886 3e4749f7 2020-10-02 op
887 4c416739 2021-01-01 op if (pledge("stdio rpath inet proc exec", NULL) == -1)
888 4c416739 2021-01-01 op err(1, "pledge");
889 4c416739 2021-01-01 op
890 4c416739 2021-01-01 op /* drop proc and exec if cgi isn't enabled */
891 4c416739 2021-01-01 op if (cgi == NULL && pledge("stdio rpath inet", NULL) == -1)
892 4c416739 2021-01-01 op err(1, "pledge");
893 4c416739 2021-01-01 op
894 3e4749f7 2020-10-02 op loop(ctx, sock);
895 3e4749f7 2020-10-02 op
896 3e4749f7 2020-10-02 op close(sock);
897 3e4749f7 2020-10-02 op tls_free(ctx);
898 3e4749f7 2020-10-02 op tls_config_free(conf);
899 3e4749f7 2020-10-02 op }