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