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