Blob


1 /*
2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 /*
18 * TODO:
19 * - move the various
20 * imsg_compose(...);
21 * imsg_flush(...);
22 * to something more asynchronous
23 */
25 #include <telescope.h>
27 #include <sys/types.h>
28 #include <sys/socket.h>
30 #include <netinet/in.h>
32 #include <assert.h>
33 #include <ctype.h>
34 #include <errno.h>
35 #include <netdb.h>
36 #include <stdarg.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <tls.h>
41 #include <unistd.h>
43 static struct tls_config *tlsconf;
44 static struct imsgbuf *ibuf;
46 struct req;
48 static void die(void) __attribute__((__noreturn__));
49 static char *xasprintf(const char*, ...);
50 static int conn_towards(struct url*, char**);
52 static void close_with_err(struct req*, const char *err);
53 static struct req *req_by_id(uint32_t);
54 static struct req *req_by_id_try(uint32_t);
56 static void do_handshake(int, short, void*);
57 static void write_request(int, short, void*);
58 static void read_reply(int, short, void*);
59 static void parse_reply(struct req*);
60 static void copy_body(int, short, void*);
62 static void check_special_page(struct req*, const char*);
64 static void handle_get(struct imsg*, size_t);
65 static void handle_cert_status(struct imsg*, size_t);
66 static void handle_proceed(struct imsg*, size_t);
67 static void handle_stop(struct imsg*, size_t);
68 static void handle_quit(struct imsg*, size_t);
70 /* TODO: making this customizable */
71 struct timeval timeout_for_handshake = { 5, 0 };
73 static imsg_handlerfn *handlers[] = {
74 [IMSG_GET] = handle_get,
75 [IMSG_CERT_STATUS] = handle_cert_status,
76 [IMSG_PROCEED] = handle_proceed,
77 [IMSG_STOP] = handle_stop,
78 [IMSG_QUIT] = handle_quit,
79 };
81 typedef void (*statefn)(int, short, void*);
83 TAILQ_HEAD(, req) reqhead;
84 /* a pending request */
85 struct req {
86 struct event ev;
87 struct url url;
88 uint32_t id;
89 int fd;
90 struct tls *ctx;
91 char buf[1024];
92 size_t off;
93 TAILQ_ENTRY(req) reqs;
94 };
96 static inline void
97 yield_r(struct req *req, statefn fn, struct timeval *tv)
98 {
99 event_once(req->fd, EV_READ, fn, req, tv);
102 static inline void
103 yield_w(struct req *req, statefn fn, struct timeval *tv)
105 event_once(req->fd, EV_WRITE, fn, req, tv);
108 static inline void
109 advance_buf(struct req *req, size_t len)
111 assert(len <= req->off);
113 req->off -= len;
114 memmove(req->buf, req->buf + len, req->off);
117 static void __attribute__((__noreturn__))
118 die(void)
120 abort(); /* TODO */
123 static char *
124 xasprintf(const char *fmt, ...)
126 va_list ap;
127 char *s;
129 va_start(ap, fmt);
130 if (vasprintf(&s, fmt, ap) == -1)
131 s = NULL;
132 va_end(ap);
134 return s;
137 static int
138 conn_towards(struct url *url, char **err)
140 struct addrinfo hints, *servinfo, *p;
141 int status, sock;
142 const char *proto = "1965";
144 *err = NULL;
146 if (*url->port != '\0')
147 proto = url->port;
149 memset(&hints, 0, sizeof(hints));
150 hints.ai_family = AF_UNSPEC;
151 hints.ai_socktype = SOCK_STREAM;
153 if ((status = getaddrinfo(url->host, proto, &hints, &servinfo))) {
154 *err = xasprintf("failed to resolve %s: %s",
155 url->host, gai_strerror(status));
156 return -1;
159 sock = -1;
160 for (p = servinfo; p != NULL; p = p->ai_next) {
161 if ((sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1)
162 continue;
163 if (connect(sock, p->ai_addr, p->ai_addrlen) != -1)
164 break;
165 close(sock);
168 if (sock == -1)
169 *err = xasprintf("couldn't connect to %s", url->host);
170 else
171 mark_nonblock(sock);
173 freeaddrinfo(servinfo);
174 return sock;
177 static struct req *
178 req_by_id(uint32_t id)
180 struct req *r;
182 if ((r = req_by_id_try(id)) == NULL)
183 die();
184 return r;
187 static struct req *
188 req_by_id_try(uint32_t id)
190 struct req *r;
192 TAILQ_FOREACH(r, &reqhead, reqs) {
193 if (r->id == id)
194 return r;
197 return NULL;
200 static void
201 close_conn(int fd, short ev, void *d)
203 struct req *req = d;
205 if (req->ctx != NULL) {
206 switch (tls_close(req->ctx)) {
207 case TLS_WANT_POLLIN:
208 yield_r(req, close_conn, NULL);
209 return;
210 case TLS_WANT_POLLOUT:
211 yield_w(req, close_conn, NULL);
212 return;
215 tls_free(req->ctx);
218 TAILQ_REMOVE(&reqhead, req, reqs);
219 if (req->fd != -1)
220 close(req->fd);
221 free(req);
224 static void
225 close_with_err(struct req *req, const char *err)
227 imsg_compose(ibuf, IMSG_ERR, req->id, 0, -1, err, strlen(err)+1);
228 imsg_flush(ibuf);
229 close_conn(0, 0, req);
232 static void
233 do_handshake(int fd, short ev, void *d)
235 struct req *req = d;
236 const char *hash;
238 if (ev == EV_TIMEOUT) {
239 close_with_err(req, "Timeout loading page");
240 return;
243 switch (tls_handshake(req->ctx)) {
244 case TLS_WANT_POLLIN:
245 yield_r(req, do_handshake, NULL);
246 return;
247 case TLS_WANT_POLLOUT:
248 yield_w(req, do_handshake, NULL);
249 return;
252 hash = tls_peer_cert_hash(req->ctx);
253 imsg_compose(ibuf, IMSG_CHECK_CERT, req->id, 0, -1, hash, strlen(hash)+1);
254 imsg_flush(ibuf);
257 static void
258 write_request(int fd, short ev, void *d)
260 struct req *req = d;
261 ssize_t r;
262 size_t len;
263 char buf[1024], *err;
265 strlcpy(buf, "gemini://", sizeof(buf));
266 strlcat(buf, req->url.host, sizeof(buf));
267 strlcat(buf, "/", sizeof(buf));
268 strlcat(buf, req->url.path, sizeof(buf));
270 if (req->url.query[0] != '\0') {
271 strlcat(buf, "?", sizeof(buf));
272 strlcat(buf, req->url.query, sizeof(buf));
275 len = strlcat(buf, "\r\n", sizeof(buf));
277 assert(len <= sizeof(buf));
279 switch (r = tls_write(req->ctx, buf, len)) {
280 case -1:
281 err = xasprintf("tls_write: %s", tls_error(req->ctx));
282 close_with_err(req, err);
283 free(err);
284 break;
285 case TLS_WANT_POLLIN:
286 yield_r(req, write_request, NULL);
287 break;
288 case TLS_WANT_POLLOUT:
289 yield_w(req, write_request, NULL);
290 break;
291 default:
292 /* assume r == len */
293 (void)r;
294 yield_r(req, read_reply, NULL);
295 break;
299 static void
300 read_reply(int fd, short ev, void *d)
302 struct req *req = d;
303 size_t len;
304 ssize_t r;
305 char *buf, *e;
307 buf = req->buf + req->off;
308 len = sizeof(req->buf) - req->off;
310 switch (r = tls_read(req->ctx, buf, len)) {
311 case -1:
312 e = xasprintf("tls_read: %s", tls_error(req->ctx));
313 close_with_err(req, e);
314 free(e);
315 break;
316 case TLS_WANT_POLLIN:
317 yield_r(req, read_reply, NULL);
318 break;
319 case TLS_WANT_POLLOUT:
320 yield_w(req, read_reply, NULL);
321 break;
322 default:
323 req->off += r;
325 /* TODO: really watch for \r\n not \n alone */
326 if ((e = telescope_strnchr(req->buf, '\n', req->off)) != NULL)
327 parse_reply(req);
328 else if (req->off == sizeof(req->buf))
329 close_with_err(req, "invalid response");
330 else
331 yield_r(req, read_reply, NULL);
332 break;
336 static void
337 parse_reply(struct req *req)
339 int code;
340 char *e;
341 size_t len;
343 if (req->off < 4)
344 goto err;
346 if (!isdigit(req->buf[0]) || !isdigit(req->buf[1]))
347 goto err;
349 code = (req->buf[0] - '0')*10 + (req->buf[1] - '0');
351 if (!isspace(req->buf[2]))
352 goto err;
354 advance_buf(req, 3);
355 if ((e = telescope_strnchr(req->buf, '\r', req->off)) == NULL)
356 goto err;
358 *e = '\0';
359 e++;
360 len = e - req->buf;
361 imsg_compose(ibuf, IMSG_GOT_CODE, req->id, 0, -1, &code, sizeof(code));
362 imsg_compose(ibuf, IMSG_GOT_META, req->id, 0, -1,
363 req->buf, len);
364 imsg_flush(ibuf);
366 if (code != 20)
367 close_conn(0, 0, req);
368 advance_buf(req, len+1); /* skip \n too */
370 return;
372 err:
373 close_with_err(req, "malformed request");
376 static void
377 copy_body(int fd, short ev, void *d)
379 struct req *req = d;
380 ssize_t r;
382 for (;;) {
383 if (req->off != 0) {
384 imsg_compose(ibuf, IMSG_BUF, req->id, 0, -1,
385 req->buf, req->off);
386 imsg_flush(ibuf);
387 req->off = 0;
390 switch (r = tls_read(req->ctx, req->buf, sizeof(req->buf))) {
391 case TLS_WANT_POLLIN:
392 yield_r(req, copy_body, NULL);
393 return;
394 case TLS_WANT_POLLOUT:
395 yield_w(req, copy_body, NULL);
396 return;
397 case 0:
398 imsg_compose(ibuf, IMSG_EOF, req->id, 0, -1, NULL, 0);
399 imsg_flush(ibuf);
400 close_conn(0, 0, req);
401 return;
402 default:
403 req->off = r;
408 static void
409 handle_get(struct imsg *imsg, size_t datalen)
411 struct req *req;
412 const char *e;
413 char *data, *err = NULL;
415 data = imsg->data;
417 if (data[datalen-1] != '\0')
418 die();
420 if ((req = calloc(1, sizeof(*req))) == NULL)
421 die();
423 req->id = imsg->hdr.peerid;
424 TAILQ_INSERT_HEAD(&reqhead, req, reqs);
426 if (!url_parse(imsg->data, &req->url, &e)) {
427 fprintf(stderr, "failed to parse url: %s\n", e);
428 close_with_err(req, e);
429 return;
432 if ((req->fd = conn_towards(&req->url, &err)) == -1)
433 goto err;
434 if ((req->ctx = tls_client()) == NULL)
435 goto err;
436 if (tls_configure(req->ctx, tlsconf) == -1) {
437 err = xasprintf("tls_configure: %s", tls_error(req->ctx));
438 goto err;
440 if (tls_connect_socket(req->ctx, req->fd, req->url.host) == -1) {
441 err = xasprintf("tls_connect_socket: %s", tls_error(req->ctx));
442 goto err;
445 yield_w(req, do_handshake, &timeout_for_handshake);
446 return;
448 err:
449 close_with_err(req, err);
450 free(err);
453 static void
454 handle_cert_status(struct imsg *imsg, size_t datalen)
456 struct req *req;
457 int is_ok;
459 req = req_by_id(imsg->hdr.peerid);
461 if (datalen < sizeof(is_ok))
462 die();
463 memcpy(&is_ok, imsg->data, sizeof(is_ok));
465 if (is_ok)
466 yield_w(req, write_request, NULL);
467 else
468 close_conn(0, 0, req);
471 static void
472 handle_proceed(struct imsg *imsg, size_t datalen)
474 yield_r(req_by_id(imsg->hdr.peerid),
475 copy_body, NULL);
478 static void
479 handle_stop(struct imsg *imsg, size_t datalen)
481 struct req *req;
483 if ((req = req_by_id_try(imsg->hdr.peerid)) == NULL)
484 return;
485 close_conn(0, 0, req);
488 static void
489 handle_quit(struct imsg *imsg, size_t datalen)
491 event_loopbreak();
494 static void
495 dispatch_imsg(int fd, short ev, void *d)
497 struct imsgbuf *ibuf = d;
498 struct imsg imsg;
499 ssize_t n;
500 size_t datalen;
502 if ((n = imsg_read(ibuf)) == -1) {
503 if (errno == EAGAIN || errno == EWOULDBLOCK)
504 return;
505 _exit(1);
508 if (n == 0)
509 _exit(1);
511 for (;;) {
512 if ((n = imsg_get(ibuf, &imsg)) == -1)
513 _exit(1);
514 if (n == 0)
515 return;
516 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
517 handlers[imsg.hdr.type](&imsg, datalen);
518 imsg_free(&imsg);
522 int
523 client_main(struct imsgbuf *b)
525 ibuf = b;
527 TAILQ_INIT(&reqhead);
529 if ((tlsconf = tls_config_new()) == NULL)
530 die();
531 tls_config_insecure_noverifycert(tlsconf);
533 event_init();
535 event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST, dispatch_imsg, ibuf);
536 event_add(&imsgev, NULL);
538 sandbox_network_process();
540 event_dispatch();
541 return 0;