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