2 5e11c00c 2021-03-02 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 5e11c00c 2021-03-02 op * Permission to use, copy, modify, and distribute this software for any
5 5e11c00c 2021-03-02 op * purpose with or without fee is hereby granted, provided that the above
6 5e11c00c 2021-03-02 op * copyright notice and this permission notice appear in all copies.
8 5e11c00c 2021-03-02 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 5e11c00c 2021-03-02 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 5e11c00c 2021-03-02 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 5e11c00c 2021-03-02 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 5e11c00c 2021-03-02 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 5e11c00c 2021-03-02 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 5e11c00c 2021-03-02 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 5e11c00c 2021-03-02 op * - move the various
20 5e11c00c 2021-03-02 op * imsg_compose(...);
21 5e11c00c 2021-03-02 op * imsg_flush(...);
22 5e11c00c 2021-03-02 op * to something more asynchronous
25 5e11c00c 2021-03-02 op #include <telescope.h>
27 5e11c00c 2021-03-02 op #include <sys/types.h>
28 5e11c00c 2021-03-02 op #include <sys/socket.h>
30 5e11c00c 2021-03-02 op #include <netinet/in.h>
32 5e11c00c 2021-03-02 op #include <assert.h>
33 5e11c00c 2021-03-02 op #include <ctype.h>
34 5e11c00c 2021-03-02 op #include <errno.h>
35 5e11c00c 2021-03-02 op #include <netdb.h>
36 5e11c00c 2021-03-02 op #include <stdarg.h>
37 5e11c00c 2021-03-02 op #include <stdio.h>
38 5e11c00c 2021-03-02 op #include <stdlib.h>
39 5e11c00c 2021-03-02 op #include <string.h>
40 5e11c00c 2021-03-02 op #include <tls.h>
41 5e11c00c 2021-03-02 op #include <unistd.h>
43 35e1f40a 2021-03-14 op static struct event imsgev;
44 5e11c00c 2021-03-02 op static struct tls_config *tlsconf;
45 5e11c00c 2021-03-02 op static struct imsgbuf *ibuf;
49 5e11c00c 2021-03-02 op static void die(void) __attribute__((__noreturn__));
50 5e11c00c 2021-03-02 op static char *xasprintf(const char*, ...);
51 5e11c00c 2021-03-02 op static int conn_towards(struct url*, char**);
53 a237223e 2021-03-16 op static void close_with_err(struct req*, const char*);
54 a237223e 2021-03-16 op static void close_with_errf(struct req*, const char*, ...) __attribute__((format(printf, 2, 3)));
55 5e11c00c 2021-03-02 op static struct req *req_by_id(uint32_t);
56 15354eed 2021-03-10 op static struct req *req_by_id_try(uint32_t);
58 5e11c00c 2021-03-02 op static void do_handshake(int, short, void*);
59 5e11c00c 2021-03-02 op static void write_request(int, short, void*);
60 5e11c00c 2021-03-02 op static void read_reply(int, short, void*);
61 5e11c00c 2021-03-02 op static void parse_reply(struct req*);
62 5e11c00c 2021-03-02 op static void copy_body(int, short, void*);
64 5e11c00c 2021-03-02 op static void handle_get(struct imsg*, size_t);
65 5e11c00c 2021-03-02 op static void handle_cert_status(struct imsg*, size_t);
66 0972d8b2 2021-03-02 op static void handle_proceed(struct imsg*, size_t);
67 5e11c00c 2021-03-02 op static void handle_stop(struct imsg*, size_t);
68 5e11c00c 2021-03-02 op static void handle_quit(struct imsg*, size_t);
69 1304bbdd 2021-03-15 op static void handle_dispatch_imsg(int, short, void*);
71 723bee54 2021-03-09 op /* TODO: making this customizable */
72 723bee54 2021-03-09 op struct timeval timeout_for_handshake = { 5, 0 };
74 5e11c00c 2021-03-02 op static imsg_handlerfn *handlers[] = {
75 5e11c00c 2021-03-02 op [IMSG_GET] = handle_get,
76 5e11c00c 2021-03-02 op [IMSG_CERT_STATUS] = handle_cert_status,
77 0972d8b2 2021-03-02 op [IMSG_PROCEED] = handle_proceed,
78 5e11c00c 2021-03-02 op [IMSG_STOP] = handle_stop,
79 5e11c00c 2021-03-02 op [IMSG_QUIT] = handle_quit,
82 5e11c00c 2021-03-02 op typedef void (*statefn)(int, short, void*);
84 5e11c00c 2021-03-02 op TAILQ_HEAD(, req) reqhead;
85 5e11c00c 2021-03-02 op /* a pending request */
87 5e11c00c 2021-03-02 op struct event ev;
88 5e11c00c 2021-03-02 op struct url url;
91 5e11c00c 2021-03-02 op struct tls *ctx;
92 5e11c00c 2021-03-02 op char buf[1024];
94 5e11c00c 2021-03-02 op TAILQ_ENTRY(req) reqs;
97 5e11c00c 2021-03-02 op static inline void
98 5e11c00c 2021-03-02 op yield_r(struct req *req, statefn fn, struct timeval *tv)
100 5e11c00c 2021-03-02 op event_once(req->fd, EV_READ, fn, req, tv);
103 5e11c00c 2021-03-02 op static inline void
104 5e11c00c 2021-03-02 op yield_w(struct req *req, statefn fn, struct timeval *tv)
106 5e11c00c 2021-03-02 op event_once(req->fd, EV_WRITE, fn, req, tv);
109 5e11c00c 2021-03-02 op static inline void
110 5e11c00c 2021-03-02 op advance_buf(struct req *req, size_t len)
112 5e11c00c 2021-03-02 op assert(len <= req->off);
114 5e11c00c 2021-03-02 op req->off -= len;
115 5e11c00c 2021-03-02 op memmove(req->buf, req->buf + len, req->off);
118 5e11c00c 2021-03-02 op static void __attribute__((__noreturn__))
121 5e11c00c 2021-03-02 op abort(); /* TODO */
124 5e11c00c 2021-03-02 op static char *
125 5e11c00c 2021-03-02 op xasprintf(const char *fmt, ...)
130 5e11c00c 2021-03-02 op va_start(ap, fmt);
131 5e11c00c 2021-03-02 op if (vasprintf(&s, fmt, ap) == -1)
139 5e11c00c 2021-03-02 op conn_towards(struct url *url, char **err)
141 5e11c00c 2021-03-02 op struct addrinfo hints, *servinfo, *p;
142 5e11c00c 2021-03-02 op int status, sock;
143 5e11c00c 2021-03-02 op const char *proto = "1965";
147 5e11c00c 2021-03-02 op if (*url->port != '\0')
148 5e11c00c 2021-03-02 op proto = url->port;
150 5e11c00c 2021-03-02 op memset(&hints, 0, sizeof(hints));
151 5e11c00c 2021-03-02 op hints.ai_family = AF_UNSPEC;
152 5e11c00c 2021-03-02 op hints.ai_socktype = SOCK_STREAM;
154 5e11c00c 2021-03-02 op if ((status = getaddrinfo(url->host, proto, &hints, &servinfo))) {
155 5e11c00c 2021-03-02 op *err = xasprintf("failed to resolve %s: %s",
156 5e11c00c 2021-03-02 op url->host, gai_strerror(status));
161 5e11c00c 2021-03-02 op for (p = servinfo; p != NULL; p = p->ai_next) {
162 5e11c00c 2021-03-02 op if ((sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1)
164 5e11c00c 2021-03-02 op if (connect(sock, p->ai_addr, p->ai_addrlen) != -1)
169 5e11c00c 2021-03-02 op if (sock == -1)
170 5e11c00c 2021-03-02 op *err = xasprintf("couldn't connect to %s", url->host);
172 5e11c00c 2021-03-02 op mark_nonblock(sock);
174 5e11c00c 2021-03-02 op freeaddrinfo(servinfo);
178 5e11c00c 2021-03-02 op static struct req *
179 5e11c00c 2021-03-02 op req_by_id(uint32_t id)
181 15354eed 2021-03-10 op struct req *r;
183 15354eed 2021-03-10 op if ((r = req_by_id_try(id)) == NULL)
188 15354eed 2021-03-10 op static struct req *
189 15354eed 2021-03-10 op req_by_id_try(uint32_t id)
191 5e11c00c 2021-03-02 op struct req *r;
193 5e11c00c 2021-03-02 op TAILQ_FOREACH(r, &reqhead, reqs) {
194 5e11c00c 2021-03-02 op if (r->id == id)
202 5e11c00c 2021-03-02 op close_conn(int fd, short ev, void *d)
204 5e11c00c 2021-03-02 op struct req *req = d;
206 5e11c00c 2021-03-02 op if (req->ctx != NULL) {
207 5e11c00c 2021-03-02 op switch (tls_close(req->ctx)) {
208 5e11c00c 2021-03-02 op case TLS_WANT_POLLIN:
209 5e11c00c 2021-03-02 op yield_r(req, close_conn, NULL);
211 5e11c00c 2021-03-02 op case TLS_WANT_POLLOUT:
212 5e11c00c 2021-03-02 op yield_w(req, close_conn, NULL);
216 5e11c00c 2021-03-02 op tls_free(req->ctx);
219 5e11c00c 2021-03-02 op TAILQ_REMOVE(&reqhead, req, reqs);
220 5e11c00c 2021-03-02 op if (req->fd != -1)
221 5e11c00c 2021-03-02 op close(req->fd);
226 5e11c00c 2021-03-02 op close_with_err(struct req *req, const char *err)
228 5e11c00c 2021-03-02 op imsg_compose(ibuf, IMSG_ERR, req->id, 0, -1, err, strlen(err)+1);
229 5e11c00c 2021-03-02 op imsg_flush(ibuf);
230 5e11c00c 2021-03-02 op close_conn(0, 0, req);
234 a237223e 2021-03-16 op close_with_errf(struct req *req, const char *fmt, ...)
239 a237223e 2021-03-16 op va_start(ap, fmt);
240 a237223e 2021-03-16 op if (vasprintf(&s, fmt, ap) == -1)
244 a237223e 2021-03-16 op close_with_err(req, s);
249 5e11c00c 2021-03-02 op do_handshake(int fd, short ev, void *d)
251 5e11c00c 2021-03-02 op struct req *req = d;
252 5e11c00c 2021-03-02 op const char *hash;
254 723bee54 2021-03-09 op if (ev == EV_TIMEOUT) {
255 723bee54 2021-03-09 op close_with_err(req, "Timeout loading page");
259 5e11c00c 2021-03-02 op switch (tls_handshake(req->ctx)) {
260 5e11c00c 2021-03-02 op case TLS_WANT_POLLIN:
261 5e11c00c 2021-03-02 op yield_r(req, do_handshake, NULL);
263 5e11c00c 2021-03-02 op case TLS_WANT_POLLOUT:
264 5e11c00c 2021-03-02 op yield_w(req, do_handshake, NULL);
268 5e11c00c 2021-03-02 op hash = tls_peer_cert_hash(req->ctx);
269 282a1d0a 2021-03-16 op if (hash == NULL) {
270 a237223e 2021-03-16 op close_with_errf(req, "handshake failed: %s", tls_error(req->ctx));
273 5e11c00c 2021-03-02 op imsg_compose(ibuf, IMSG_CHECK_CERT, req->id, 0, -1, hash, strlen(hash)+1);
274 5e11c00c 2021-03-02 op imsg_flush(ibuf);
278 5e11c00c 2021-03-02 op write_request(int fd, short ev, void *d)
280 5e11c00c 2021-03-02 op struct req *req = d;
283 a237223e 2021-03-16 op char buf[1024];
285 5e11c00c 2021-03-02 op strlcpy(buf, "gemini://", sizeof(buf));
286 5e11c00c 2021-03-02 op strlcat(buf, req->url.host, sizeof(buf));
287 5e11c00c 2021-03-02 op strlcat(buf, "/", sizeof(buf));
288 5e11c00c 2021-03-02 op strlcat(buf, req->url.path, sizeof(buf));
290 5e11c00c 2021-03-02 op if (req->url.query[0] != '\0') {
291 5e11c00c 2021-03-02 op strlcat(buf, "?", sizeof(buf));
292 5e11c00c 2021-03-02 op strlcat(buf, req->url.query, sizeof(buf));
295 5e11c00c 2021-03-02 op len = strlcat(buf, "\r\n", sizeof(buf));
297 5e11c00c 2021-03-02 op assert(len <= sizeof(buf));
299 5e11c00c 2021-03-02 op switch (r = tls_write(req->ctx, buf, len)) {
301 a237223e 2021-03-16 op close_with_errf(req, "tls_write: %s", tls_error(req->ctx));
303 5e11c00c 2021-03-02 op case TLS_WANT_POLLIN:
304 5e11c00c 2021-03-02 op yield_r(req, write_request, NULL);
306 5e11c00c 2021-03-02 op case TLS_WANT_POLLOUT:
307 5e11c00c 2021-03-02 op yield_w(req, write_request, NULL);
310 5e11c00c 2021-03-02 op /* assume r == len */
312 5e11c00c 2021-03-02 op yield_r(req, read_reply, NULL);
318 5e11c00c 2021-03-02 op read_reply(int fd, short ev, void *d)
320 5e11c00c 2021-03-02 op struct req *req = d;
325 5e11c00c 2021-03-02 op buf = req->buf + req->off;
326 5e11c00c 2021-03-02 op len = sizeof(req->buf) - req->off;
328 5e11c00c 2021-03-02 op switch (r = tls_read(req->ctx, buf, len)) {
330 a237223e 2021-03-16 op close_with_errf(req, "tls_read: %s", tls_error(req->ctx));
332 5e11c00c 2021-03-02 op case TLS_WANT_POLLIN:
333 5e11c00c 2021-03-02 op yield_r(req, read_reply, NULL);
335 5e11c00c 2021-03-02 op case TLS_WANT_POLLOUT:
336 5e11c00c 2021-03-02 op yield_w(req, read_reply, NULL);
339 5e11c00c 2021-03-02 op req->off += r;
341 5e11c00c 2021-03-02 op /* TODO: really watch for \r\n not \n alone */
342 e62289e7 2021-03-16 op if (memmem(req->buf, req->off, "\r\n", 2) != NULL)
343 5e11c00c 2021-03-02 op parse_reply(req);
344 5e11c00c 2021-03-02 op else if (req->off == sizeof(req->buf))
345 5e11c00c 2021-03-02 op close_with_err(req, "invalid response");
347 5e11c00c 2021-03-02 op yield_r(req, read_reply, NULL);
353 5e11c00c 2021-03-02 op parse_reply(struct req *req)
359 5e11c00c 2021-03-02 op if (req->off < 4)
362 5e11c00c 2021-03-02 op if (!isdigit(req->buf[0]) || !isdigit(req->buf[1]))
365 5e11c00c 2021-03-02 op code = (req->buf[0] - '0')*10 + (req->buf[1] - '0');
367 5e11c00c 2021-03-02 op if (!isspace(req->buf[2]))
370 5e11c00c 2021-03-02 op advance_buf(req, 3);
371 e62289e7 2021-03-16 op if ((e = memmem(req->buf, req->off, "\r\n", 2)) == NULL)
376 5e11c00c 2021-03-02 op len = e - req->buf;
377 5e11c00c 2021-03-02 op imsg_compose(ibuf, IMSG_GOT_CODE, req->id, 0, -1, &code, sizeof(code));
378 5e11c00c 2021-03-02 op imsg_compose(ibuf, IMSG_GOT_META, req->id, 0, -1,
379 5e11c00c 2021-03-02 op req->buf, len);
380 5e11c00c 2021-03-02 op imsg_flush(ibuf);
382 723bee54 2021-03-09 op if (code != 20)
383 723bee54 2021-03-09 op close_conn(0, 0, req);
384 72a79cfb 2021-03-13 op advance_buf(req, len+1); /* skip \n too */
389 5e11c00c 2021-03-02 op close_with_err(req, "malformed request");
393 5e11c00c 2021-03-02 op copy_body(int fd, short ev, void *d)
395 5e11c00c 2021-03-02 op struct req *req = d;
399 72a79cfb 2021-03-13 op if (req->off != 0) {
400 72a79cfb 2021-03-13 op imsg_compose(ibuf, IMSG_BUF, req->id, 0, -1,
401 72a79cfb 2021-03-13 op req->buf, req->off);
402 72a79cfb 2021-03-13 op imsg_flush(ibuf);
403 1deaf397 2021-03-14 op req->off = 0;
406 72a79cfb 2021-03-13 op switch (r = tls_read(req->ctx, req->buf, sizeof(req->buf))) {
407 5e11c00c 2021-03-02 op case TLS_WANT_POLLIN:
408 5e11c00c 2021-03-02 op yield_r(req, copy_body, NULL);
410 5e11c00c 2021-03-02 op case TLS_WANT_POLLOUT:
411 5e11c00c 2021-03-02 op yield_w(req, copy_body, NULL);
414 5e11c00c 2021-03-02 op imsg_compose(ibuf, IMSG_EOF, req->id, 0, -1, NULL, 0);
415 5e11c00c 2021-03-02 op imsg_flush(ibuf);
416 5e11c00c 2021-03-02 op close_conn(0, 0, req);
419 72a79cfb 2021-03-13 op req->off = r;
425 5e11c00c 2021-03-02 op handle_get(struct imsg *imsg, size_t datalen)
427 5e11c00c 2021-03-02 op struct req *req;
428 5e11c00c 2021-03-02 op const char *e;
429 5e11c00c 2021-03-02 op char *data, *err = NULL;
431 5e11c00c 2021-03-02 op data = imsg->data;
433 5e11c00c 2021-03-02 op if (data[datalen-1] != '\0')
436 5e11c00c 2021-03-02 op if ((req = calloc(1, sizeof(*req))) == NULL)
439 5e11c00c 2021-03-02 op req->id = imsg->hdr.peerid;
440 71a2feab 2021-03-10 op TAILQ_INSERT_HEAD(&reqhead, req, reqs);
442 5e11c00c 2021-03-02 op if (!url_parse(imsg->data, &req->url, &e)) {
443 5e11c00c 2021-03-02 op close_with_err(req, e);
447 5e11c00c 2021-03-02 op if ((req->fd = conn_towards(&req->url, &err)) == -1)
449 5e11c00c 2021-03-02 op if ((req->ctx = tls_client()) == NULL)
451 5e11c00c 2021-03-02 op if (tls_configure(req->ctx, tlsconf) == -1) {
452 5e11c00c 2021-03-02 op err = xasprintf("tls_configure: %s", tls_error(req->ctx));
455 5e11c00c 2021-03-02 op if (tls_connect_socket(req->ctx, req->fd, req->url.host) == -1) {
456 5e11c00c 2021-03-02 op err = xasprintf("tls_connect_socket: %s", tls_error(req->ctx));
460 723bee54 2021-03-09 op yield_w(req, do_handshake, &timeout_for_handshake);
464 5e11c00c 2021-03-02 op close_with_err(req, err);
469 5e11c00c 2021-03-02 op handle_cert_status(struct imsg *imsg, size_t datalen)
471 5e11c00c 2021-03-02 op struct req *req;
474 5e11c00c 2021-03-02 op req = req_by_id(imsg->hdr.peerid);
476 5e11c00c 2021-03-02 op if (datalen < sizeof(is_ok))
478 5e11c00c 2021-03-02 op memcpy(&is_ok, imsg->data, sizeof(is_ok));
481 5e11c00c 2021-03-02 op yield_w(req, write_request, NULL);
483 5e11c00c 2021-03-02 op close_conn(0, 0, req);
487 0972d8b2 2021-03-02 op handle_proceed(struct imsg *imsg, size_t datalen)
489 e4304954 2021-03-14 op yield_r(req_by_id(imsg->hdr.peerid),
490 e4304954 2021-03-14 op copy_body, NULL);
494 5e11c00c 2021-03-02 op handle_stop(struct imsg *imsg, size_t datalen)
496 5e11c00c 2021-03-02 op struct req *req;
498 15354eed 2021-03-10 op if ((req = req_by_id_try(imsg->hdr.peerid)) == NULL)
500 5e11c00c 2021-03-02 op close_conn(0, 0, req);
504 5e11c00c 2021-03-02 op handle_quit(struct imsg *imsg, size_t datalen)
506 5e11c00c 2021-03-02 op event_loopbreak();
510 1304bbdd 2021-03-15 op handle_dispatch_imsg(int fd, short ev, void *d)
512 5e11c00c 2021-03-02 op struct imsgbuf *ibuf = d;
513 1304bbdd 2021-03-15 op dispatch_imsg(ibuf, handlers, sizeof(handlers));
517 5e11c00c 2021-03-02 op client_main(struct imsgbuf *b)
521 5e11c00c 2021-03-02 op TAILQ_INIT(&reqhead);
523 5e11c00c 2021-03-02 op if ((tlsconf = tls_config_new()) == NULL)
525 5e11c00c 2021-03-02 op tls_config_insecure_noverifycert(tlsconf);
527 5e11c00c 2021-03-02 op event_init();
529 1304bbdd 2021-03-15 op event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, ibuf);
530 5e11c00c 2021-03-02 op event_add(&imsgev, NULL);
532 ebdcadbf 2021-03-12 op sandbox_network_process();
534 5e11c00c 2021-03-02 op event_dispatch();