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