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