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 #include "compat.h"
19 #include <sys/types.h>
20 #include <sys/socket.h>
22 #include <netinet/in.h>
24 #include <assert.h>
25 #include <ctype.h>
26 #include <errno.h>
27 #include <netdb.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <tls.h>
33 #include <unistd.h>
35 #if HAVE_ASR_RUN
36 # include <asr.h>
37 #endif
39 #include "telescope.h"
41 static struct imsgev *iev_ui;
42 static struct tls_config *tlsconf;
44 /* a pending request */
45 struct req {
46 struct phos_uri url;
47 uint32_t id;
48 int proto;
49 int fd;
50 struct tls *ctx;
51 char req[1024];
52 size_t len;
53 int done_header;
54 struct bufferevent *bev;
56 struct addrinfo *servinfo, *p;
57 #if HAVE_ASR_RUN
58 struct addrinfo hints;
59 struct event_asr *asrev;
60 #endif
62 TAILQ_ENTRY(req) reqs;
63 };
65 static struct req *req_by_id(uint32_t);
67 static void die(void) __attribute__((__noreturn__));
69 static void try_to_connect(int, short, void*);
71 #if HAVE_ASR_RUN
72 static void query_done(struct asr_result*, void*);
73 static void async_conn_towards(struct req*);
74 #else
75 static void blocking_conn_towards(struct req*);
76 #endif
78 static void close_with_err(struct req*, const char*);
79 static void close_with_errf(struct req*, const char*, ...)
80 __attribute__((format(printf, 2, 3)));
82 static void net_tls_handshake(int, short, void *);
83 static void net_tls_readcb(int, short, void *);
84 static void net_tls_writecb(int, short, void *);
86 static int gemini_parse_reply(struct req *, const char *, size_t);
88 static void net_ready(struct req *req);
89 static void net_read(struct bufferevent *, void *);
90 static void net_write(struct bufferevent *, void *);
91 static void net_error(struct bufferevent *, short, void *);
93 static void handle_get_raw(struct imsg *, size_t);
94 static void handle_cert_status(struct imsg*, size_t);
95 static void handle_proceed(struct imsg*, size_t);
96 static void handle_stop(struct imsg*, size_t);
97 static void handle_quit(struct imsg*, size_t);
98 static void handle_dispatch_imsg(int, short, void*);
100 static int net_send_ui(int, uint32_t, const void *, uint16_t);
102 /* TODO: making this customizable */
103 struct timeval timeout_for_handshake = { 5, 0 };
105 static imsg_handlerfn *handlers[] = {
106 [IMSG_GET_RAW] = handle_get_raw,
107 [IMSG_CERT_STATUS] = handle_cert_status,
108 [IMSG_PROCEED] = handle_proceed,
109 [IMSG_STOP] = handle_stop,
110 [IMSG_QUIT] = handle_quit,
111 };
113 typedef void (*statefn)(int, short, void*);
115 TAILQ_HEAD(, req) reqhead;
117 static inline void
118 yield_r(struct req *req, statefn fn, struct timeval *tv)
120 event_once(req->fd, EV_READ, fn, req, tv);
123 static inline void
124 yield_w(struct req *req, statefn fn, struct timeval *tv)
126 event_once(req->fd, EV_WRITE, fn, req, tv);
129 static struct req *
130 req_by_id(uint32_t id)
132 struct req *r;
134 TAILQ_FOREACH(r, &reqhead, reqs) {
135 if (r->id == id)
136 return r;
139 return NULL;
142 static void __attribute__((__noreturn__))
143 die(void)
145 abort(); /* TODO */
148 static void
149 try_to_connect(int fd, short ev, void *d)
151 struct req *req = d;
152 int error = 0;
153 socklen_t len = sizeof(error);
155 again:
156 if (req->p == NULL)
157 goto err;
159 if (req->fd != -1) {
160 if (getsockopt(req->fd, SOL_SOCKET, SO_ERROR, &error,
161 &len) == -1)
162 goto err;
163 if (error != 0) {
164 errno = error;
165 goto err;
167 goto done;
170 req->fd = socket(req->p->ai_family, req->p->ai_socktype,
171 req->p->ai_protocol);
172 if (req->fd == -1) {
173 req->p = req->p->ai_next;
174 goto again;
175 } else {
176 mark_nonblock(req->fd);
177 if (connect(req->fd, req->p->ai_addr, req->p->ai_addrlen) == 0)
178 goto done;
179 yield_w(req, try_to_connect, NULL);
181 return;
183 err:
184 freeaddrinfo(req->servinfo);
185 close_with_errf(req, "failed to connect to %s",
186 req->url.host);
187 return;
189 done:
190 freeaddrinfo(req->servinfo);
192 switch (req->proto) {
193 case PROTO_FINGER:
194 case PROTO_GOPHER:
195 /* finger and gopher don't have a header nor TLS */
196 req->done_header = 1;
197 net_ready(req);
198 break;
200 case PROTO_GEMINI:
201 /* prepare tls */
202 if ((req->ctx = tls_client()) == NULL) {
203 close_with_errf(req, "tls_client: %s",
204 strerror(errno));
205 return;
207 if (tls_configure(req->ctx, tlsconf) == -1) {
208 close_with_errf(req, "tls_configure: %s",
209 tls_error(req->ctx));
210 return;
212 if (tls_connect_socket(req->ctx, req->fd, req->url.host)
213 == -1) {
214 close_with_errf(req, "tls_connect_socket: %s",
215 tls_error(req->ctx));
216 return;
218 yield_w(req, net_tls_handshake, &timeout_for_handshake);
219 break;
221 default:
222 die();
226 #if HAVE_ASR_RUN
227 static void
228 query_done(struct asr_result *res, void *d)
230 struct req *req = d;
232 req->asrev = NULL;
233 if (res->ar_gai_errno != 0) {
234 close_with_errf(req, "failed to resolve %s: %s",
235 req->url.host, gai_strerror(res->ar_gai_errno));
236 return;
239 req->fd = -1;
240 req->servinfo = res->ar_addrinfo;
241 req->p = res->ar_addrinfo;
242 try_to_connect(0, 0, req);
245 static void
246 async_conn_towards(struct req *req)
248 struct asr_query *q;
249 const char *proto = "1965";
251 if (*req->url.port != '\0')
252 proto = req->url.port;
254 req->hints.ai_family = AF_UNSPEC;
255 req->hints.ai_socktype = SOCK_STREAM;
256 q = getaddrinfo_async(req->url.host, proto, &req->hints, NULL);
257 req->asrev = event_asr_run(q, query_done, req);
259 #else
260 static void
261 blocking_conn_towards(struct req *req)
263 struct addrinfo hints;
264 struct phos_uri *url = &req->url;
265 int status;
266 const char *proto = "1965";
268 if (*url->port != '\0')
269 proto = url->port;
271 memset(&hints, 0, sizeof(hints));
272 hints.ai_family = AF_UNSPEC;
273 hints.ai_socktype = SOCK_STREAM;
275 if ((status = getaddrinfo(url->host, proto, &hints, &req->servinfo))) {
276 close_with_errf(req, "failed to resolve %s: %s",
277 url->host, gai_strerror(status));
278 return;
281 req->fd = -1;
282 req->p = req->servinfo;
283 try_to_connect(0, 0, req);
285 #endif
287 static void
288 close_conn(int fd, short ev, void *d)
290 struct req *req = d;
292 #if HAVE_ASR_RUN
293 if (req->asrev != NULL)
294 event_asr_abort(req->asrev);
295 #endif
297 if (req->bev != NULL) {
298 bufferevent_free(req->bev);
299 req->bev = NULL;
302 if (req->ctx != NULL) {
303 switch (tls_close(req->ctx)) {
304 case TLS_WANT_POLLIN:
305 yield_r(req, close_conn, NULL);
306 return;
307 case TLS_WANT_POLLOUT:
308 yield_w(req, close_conn, NULL);
309 return;
312 tls_free(req->ctx);
313 req->ctx = NULL;
316 TAILQ_REMOVE(&reqhead, req, reqs);
317 if (req->fd != -1)
318 close(req->fd);
319 free(req);
322 static void
323 close_with_err(struct req *req, const char *err)
325 net_send_ui(IMSG_ERR, req->id, err, strlen(err)+1);
326 close_conn(0, 0, req);
329 static void
330 close_with_errf(struct req *req, const char *fmt, ...)
332 va_list ap;
333 char *s;
335 va_start(ap, fmt);
336 if (vasprintf(&s, fmt, ap) == -1)
337 abort();
338 va_end(ap);
340 close_with_err(req, s);
341 free(s);
344 static void
345 net_tls_handshake(int fd, short event, void *d)
347 struct req *req = d;
348 const char *hash;
350 if (event == EV_TIMEOUT) {
351 close_with_err(req, "Timeout loading page");
352 return;
355 switch (tls_handshake(req->ctx)) {
356 case TLS_WANT_POLLIN:
357 yield_r(req, net_tls_handshake, NULL);
358 return;
359 case TLS_WANT_POLLOUT:
360 yield_w(req, net_tls_handshake, NULL);
361 return;
364 hash = tls_peer_cert_hash(req->ctx);
365 if (hash == NULL) {
366 close_with_errf(req, "handshake failed: %s",
367 tls_error(req->ctx));
368 return;
370 net_send_ui(IMSG_CHECK_CERT, req->id, hash, strlen(hash)+1);
373 static void
374 net_tls_readcb(int fd, short event, void *d)
376 struct bufferevent *bufev = d;
377 struct req *req = bufev->cbarg;
378 char buf[IBUF_READ_SIZE];
379 int what = EVBUFFER_READ;
380 int howmuch = IBUF_READ_SIZE;
381 int res;
382 ssize_t ret;
383 size_t len;
385 if (event == EV_TIMEOUT) {
386 what |= EVBUFFER_TIMEOUT;
387 goto err;
390 if (bufev->wm_read.high != 0)
391 howmuch = MIN(sizeof(buf), bufev->wm_read.high);
393 switch (ret = tls_read(req->ctx, buf, howmuch)) {
394 case TLS_WANT_POLLIN:
395 case TLS_WANT_POLLOUT:
396 goto retry;
397 case -1:
398 what |= EVBUFFER_ERROR;
399 goto err;
401 len = ret;
403 if (len == 0) {
404 what |= EVBUFFER_EOF;
405 goto err;
408 evbuffer_unfreeze(bufev->input, 0);
409 res = evbuffer_add(bufev->input, buf, len);
410 evbuffer_freeze(bufev->input, 0);
411 if (res == -1) {
412 what |= EVBUFFER_ERROR;
413 goto err;
416 event_add(&bufev->ev_read, NULL);
418 len = EVBUFFER_LENGTH(bufev->input);
419 if (bufev->wm_read.low != 0 && len < bufev->wm_read.low)
420 return;
422 if (bufev->readcb != NULL)
423 (*bufev->readcb)(bufev, bufev->cbarg);
424 return;
426 retry:
427 event_add(&bufev->ev_read, NULL);
428 return;
430 err:
431 (*bufev->errorcb)(bufev, what, bufev->cbarg);
434 static void
435 net_tls_writecb(int fd, short event, void *d)
437 struct bufferevent *bufev = d;
438 struct req *req = bufev->cbarg;
439 ssize_t ret;
440 size_t len;
441 short what = EVBUFFER_WRITE;
443 if (event & EV_TIMEOUT) {
444 what |= EVBUFFER_TIMEOUT;
445 goto err;
448 if (EVBUFFER_LENGTH(bufev->output) != 0) {
449 ret = tls_write(req->ctx, EVBUFFER_DATA(bufev->output),
450 EVBUFFER_LENGTH(bufev->output));
451 switch (ret) {
452 case TLS_WANT_POLLIN:
453 case TLS_WANT_POLLOUT:
454 goto retry;
455 case -1:
456 what |= EVBUFFER_ERROR;
457 goto err;
459 len = ret;
461 evbuffer_unfreeze(bufev->output, 1);
462 evbuffer_drain(bufev->output, len);
463 evbuffer_freeze(bufev->output, 1);
466 if (EVBUFFER_LENGTH(bufev->output) != 0)
467 event_add(&bufev->ev_write, NULL);
469 if (bufev->writecb != NULL &&
470 EVBUFFER_LENGTH(bufev->output) <= bufev->wm_write.low)
471 (*bufev->writecb)(bufev, bufev->cbarg);
472 return;
474 retry:
475 event_add(&bufev->ev_write, NULL);
476 return;
478 err:
479 (*bufev->errorcb)(bufev, what, bufev->cbarg);
482 static int
483 gemini_parse_reply(struct req *req, const char *header, size_t len)
485 int code;
486 const char *t;
488 if (len < 4)
489 return 0;
491 if (!isdigit(header[0]) || !isdigit(header[1]))
492 return 0;
494 code = (header[0] - '0')*10 + (header[1] - '0');
495 if (header[2] != ' ')
496 return 0;
498 t = header + 3;
500 net_send_ui(IMSG_GOT_CODE, req->id, &code, sizeof(code));
501 net_send_ui(IMSG_GOT_META, req->id, t, strlen(t)+1);
503 bufferevent_disable(req->bev, EV_READ|EV_WRITE);
505 if (code < 20 || code >= 30)
506 close_conn(0, 0, req);
507 return 1;
510 /* called when we're ready to read/write */
511 static void
512 net_ready(struct req *req)
514 req->bev = bufferevent_new(req->fd, net_read, net_write, net_error,
515 req);
516 if (req->bev == NULL)
517 die();
519 /* setup tls i/o layer */
520 if (req->ctx != NULL) {
521 event_set(&req->bev->ev_read, req->fd, EV_READ,
522 net_tls_readcb, req->bev);
523 event_set(&req->bev->ev_write, req->fd, EV_WRITE,
524 net_tls_writecb, req->bev);
527 /* TODO: adjust watermarks */
528 bufferevent_setwatermark(req->bev, EV_WRITE, 1, 0);
529 bufferevent_setwatermark(req->bev, EV_READ, 1, 0);
531 bufferevent_enable(req->bev, EV_READ|EV_WRITE);
533 bufferevent_write(req->bev, req->req, req->len);
536 /* called after a read has been done */
537 static void
538 net_read(struct bufferevent *bev, void *d)
540 struct req *req = d;
541 struct evbuffer *src = EVBUFFER_INPUT(bev);
542 void *data;
543 size_t len;
544 int r;
545 char *header;
547 if (!req->done_header) {
548 header = evbuffer_readln(src, &len, EVBUFFER_EOL_CRLF_STRICT);
549 if (header == NULL && EVBUFFER_LENGTH(src) >= 1024)
550 goto err;
551 if (header == NULL)
552 return;
553 r = gemini_parse_reply(req, header, len);
554 free(header);
555 if (!r)
556 goto err;
557 req->done_header = 1;
558 return;
561 if ((len = EVBUFFER_LENGTH(src)) == 0)
562 return;
563 data = EVBUFFER_DATA(src);
564 net_send_ui(IMSG_BUF, req->id, data, len);
565 evbuffer_drain(src, len);
566 return;
568 err:
569 (*bev->errorcb)(bev, EVBUFFER_READ, bev->cbarg);
572 /* called after a write has been done */
573 static void
574 net_write(struct bufferevent *bev, void *d)
576 struct evbuffer *dst = EVBUFFER_OUTPUT(bev);
578 if (EVBUFFER_LENGTH(dst) == 0)
579 (*bev->errorcb)(bev, EVBUFFER_WRITE, bev->cbarg);
582 static void
583 net_error(struct bufferevent *bev, short error, void *d)
585 struct req *req = d;
586 struct evbuffer *src;
588 if (error & EVBUFFER_TIMEOUT) {
589 close_with_err(req, "Timeout loading page");
590 return;
593 if (error & EVBUFFER_ERROR) {
594 close_with_err(req, "buffer event error");
595 return;
598 if (error & EVBUFFER_EOF) {
599 src = EVBUFFER_INPUT(req->bev);
600 if (EVBUFFER_LENGTH(src) != 0)
601 net_send_ui(IMSG_BUF, req->id, EVBUFFER_DATA(src),
602 EVBUFFER_LENGTH(src));
603 net_send_ui(IMSG_EOF, req->id, NULL, 0);
604 close_conn(0, 0, req);
605 return;
608 if (error & EVBUFFER_WRITE) {
609 /* finished sending request */
610 bufferevent_disable(bev, EV_WRITE);
611 return;
614 if (error & EVBUFFER_READ) {
615 close_with_err(req, "protocol error");
616 return;
619 close_with_errf(req, "unknown event error %x", error);
622 static void
623 handle_get_raw(struct imsg *imsg, size_t datalen)
625 struct req *req;
626 struct get_req *r;
628 r = imsg->data;
630 if (datalen != sizeof(*r))
631 die();
633 if ((req = calloc(1, sizeof(*req))) == NULL)
634 die();
636 req->id = imsg->hdr.peerid;
637 TAILQ_INSERT_HEAD(&reqhead, req, reqs);
639 strlcpy(req->url.host, r->host, sizeof(req->url.host));
640 strlcpy(req->url.port, r->port, sizeof(req->url.port));
642 strlcpy(req->req, r->req, sizeof(req->req));
643 req->len = strlen(r->req);
645 req->proto = r->proto;
647 #if HAVE_ASR_RUN
648 async_conn_towards(req);
649 #else
650 blocking_conn_towards(req);
651 #endif
654 static void
655 handle_cert_status(struct imsg *imsg, size_t datalen)
657 struct req *req;
658 int is_ok;
660 req = req_by_id(imsg->hdr.peerid);
662 if (datalen < sizeof(is_ok))
663 die();
664 memcpy(&is_ok, imsg->data, sizeof(is_ok));
666 if (is_ok)
667 net_ready(req);
668 else
669 close_conn(0, 0, req);
672 static void
673 handle_proceed(struct imsg *imsg, size_t datalen)
675 struct req *req;
677 if ((req = req_by_id(imsg->hdr.peerid)) == NULL)
678 return;
680 bufferevent_enable(req->bev, EV_READ);
683 static void
684 handle_stop(struct imsg *imsg, size_t datalen)
686 struct req *req;
688 if ((req = req_by_id(imsg->hdr.peerid)) == NULL)
689 return;
690 close_conn(0, 0, req);
693 static void
694 handle_quit(struct imsg *imsg, size_t datalen)
696 event_loopbreak();
699 static void
700 handle_dispatch_imsg(int fd, short ev, void *d)
702 struct imsgev *iev = d;
704 if (dispatch_imsg(iev, ev, handlers, sizeof(handlers)) == -1)
705 err(1, "connection closed");
708 static int
709 net_send_ui(int type, uint32_t peerid, const void *data,
710 uint16_t datalen)
712 return imsg_compose_event(iev_ui, type, peerid, 0, -1,
713 data, datalen);
716 int
717 net_main(void)
719 setproctitle("net");
721 TAILQ_INIT(&reqhead);
723 if ((tlsconf = tls_config_new()) == NULL)
724 die();
725 tls_config_insecure_noverifycert(tlsconf);
726 tls_config_insecure_noverifyname(tlsconf);
728 event_init();
730 /* Setup pipe and event handler to the main process */
731 if ((iev_ui = malloc(sizeof(*iev_ui))) == NULL)
732 die();
733 imsg_init(&iev_ui->ibuf, 3);
734 iev_ui->handler = handle_dispatch_imsg;
735 iev_ui->events = EV_READ;
736 event_set(&iev_ui->ev, iev_ui->ibuf.fd, iev_ui->events,
737 iev_ui->handler, iev_ui);
738 event_add(&iev_ui->ev, NULL);
740 sandbox_net_process();
742 event_dispatch();
744 tls_config_free(tlsconf);
745 msgbuf_clear(&iev_ui->ibuf.w);
746 close(iev_ui->ibuf.fd);
747 free(iev_ui);
749 return 0;