2 35e1f40a 2021-03-14 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 35e1f40a 2021-03-14 op * Permission to use, copy, modify, and distribute this software for any
5 35e1f40a 2021-03-14 op * purpose with or without fee is hereby granted, provided that the above
6 35e1f40a 2021-03-14 op * copyright notice and this permission notice appear in all copies.
8 35e1f40a 2021-03-14 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 35e1f40a 2021-03-14 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 35e1f40a 2021-03-14 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 35e1f40a 2021-03-14 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 35e1f40a 2021-03-14 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 35e1f40a 2021-03-14 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 35e1f40a 2021-03-14 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 740f578b 2021-03-15 op * Handles the data in ~/.telescope
20 3a227e9a 2021-03-18 op * TODO: add some form of locking on the files
23 35e1f40a 2021-03-14 op #include "telescope.h"
25 35e1f40a 2021-03-14 op #include <errno.h>
26 35e1f40a 2021-03-14 op #include <limits.h>
27 35e1f40a 2021-03-14 op #include <stdio.h>
28 35e1f40a 2021-03-14 op #include <stdlib.h>
29 35e1f40a 2021-03-14 op #include <string.h>
30 35e1f40a 2021-03-14 op #include <unistd.h>
32 35e1f40a 2021-03-14 op static void die(void) __attribute__((__noreturn__));
33 35e1f40a 2021-03-14 op static void serve_bookmarks(uint32_t);
34 35e1f40a 2021-03-14 op static void handle_get(struct imsg*, size_t);
35 35e1f40a 2021-03-14 op static void handle_quit(struct imsg*, size_t);
36 740f578b 2021-03-15 op static void handle_bookmark_page(struct imsg*, size_t);
37 3a227e9a 2021-03-18 op static void handle_save_cert(struct imsg*, size_t);
38 1304bbdd 2021-03-15 op static void handle_dispatch_imsg(int, short, void*);
40 35e1f40a 2021-03-14 op static struct event imsgev;
41 35e1f40a 2021-03-14 op static struct imsgbuf *ibuf;
43 740f578b 2021-03-15 op static char bookmark_file[PATH_MAX];
44 3a227e9a 2021-03-18 op static char known_hosts_file[PATH_MAX];
46 35e1f40a 2021-03-14 op static imsg_handlerfn *handlers[] = {
47 35e1f40a 2021-03-14 op [IMSG_GET] = handle_get,
48 35e1f40a 2021-03-14 op [IMSG_QUIT] = handle_quit,
49 740f578b 2021-03-15 op [IMSG_BOOKMARK_PAGE] = handle_bookmark_page,
50 3a227e9a 2021-03-18 op [IMSG_SAVE_CERT] = handle_save_cert,
53 35e1f40a 2021-03-14 op static void __attribute__((__noreturn__))
56 35e1f40a 2021-03-14 op abort(); /* TODO */
60 35e1f40a 2021-03-14 op serve_bookmarks(uint32_t peerid)
62 35e1f40a 2021-03-14 op const char *t;
63 740f578b 2021-03-15 op char buf[BUFSIZ];
67 740f578b 2021-03-15 op if ((f = fopen(bookmark_file, "r")) == NULL) {
68 740f578b 2021-03-15 op t = "# error\n\nCan't open bookmarks\n";
69 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_BUF, peerid, 0, -1, t, strlen(t));
70 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_EOF, peerid, 0, -1, NULL, 0);
71 35e1f40a 2021-03-14 op imsg_flush(ibuf);
76 35e1f40a 2021-03-14 op r = fread(buf, 1, sizeof(buf), f);
77 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_BUF, peerid, 0, -1, buf, r);
78 35e1f40a 2021-03-14 op imsg_flush(ibuf);
79 35e1f40a 2021-03-14 op if (r != sizeof(buf))
83 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_EOF, peerid, 0, -1, NULL, 0);
84 35e1f40a 2021-03-14 op imsg_flush(ibuf);
90 35e1f40a 2021-03-14 op handle_get(struct imsg *imsg, size_t datalen)
93 35e1f40a 2021-03-14 op const char *p;
95 35e1f40a 2021-03-14 op data = imsg->data;
97 35e1f40a 2021-03-14 op if (data[datalen-1] != '\0')
100 35e1f40a 2021-03-14 op if (!strcmp(data, "about:new")) {
101 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_BUF, imsg->hdr.peerid, 0, -1,
102 35e1f40a 2021-03-14 op about_new, strlen(about_new));
103 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_EOF, imsg->hdr.peerid, 0, -1, NULL, 0);
104 35e1f40a 2021-03-14 op imsg_flush(ibuf);
105 35e1f40a 2021-03-14 op } else if (!strcmp(data, "about:bookmarks")) {
106 35e1f40a 2021-03-14 op serve_bookmarks(imsg->hdr.peerid);
108 35e1f40a 2021-03-14 op p = "# not found!\n";
109 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_BUF, imsg->hdr.peerid, 0, -1, p, strlen(p));
110 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_EOF, imsg->hdr.peerid, 0, -1, NULL, 0);
111 35e1f40a 2021-03-14 op imsg_flush(ibuf);
116 35e1f40a 2021-03-14 op handle_quit(struct imsg *imsg, size_t datalen)
118 35e1f40a 2021-03-14 op event_loopbreak();
122 740f578b 2021-03-15 op handle_bookmark_page(struct imsg *imsg, size_t datalen)
128 740f578b 2021-03-15 op data = imsg->data;
129 740f578b 2021-03-15 op if (data[datalen-1] != '\0')
132 740f578b 2021-03-15 op if ((f = fopen(bookmark_file, "a")) == NULL) {
136 740f578b 2021-03-15 op fprintf(f, "=> %s\n", data);
141 740f578b 2021-03-15 op imsg_compose(ibuf, IMSG_BOOKMARK_OK, 0, 0, -1, &res, sizeof(res));
142 740f578b 2021-03-15 op imsg_flush(ibuf);
146 3a227e9a 2021-03-18 op handle_save_cert(struct imsg *imsg, size_t datalen)
148 3a227e9a 2021-03-18 op struct tofu_entry e;
152 3a227e9a 2021-03-18 op /* TODO: traverse the file to avoid duplications? */
154 3a227e9a 2021-03-18 op if (datalen != sizeof(e))
156 3a227e9a 2021-03-18 op memcpy(&e, imsg->data, datalen);
158 3a227e9a 2021-03-18 op if ((f = fopen(known_hosts_file, "a")) == NULL) {
162 3a227e9a 2021-03-18 op fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
167 3a227e9a 2021-03-18 op imsg_compose(ibuf, IMSG_SAVE_CERT_OK, imsg->hdr.peerid, 0, -1,
168 3a227e9a 2021-03-18 op &res, sizeof(res));
169 3a227e9a 2021-03-18 op imsg_flush(ibuf);
173 1304bbdd 2021-03-15 op handle_dispatch_imsg(int fd, short ev, void *d)
175 35e1f40a 2021-03-14 op struct imsgbuf *ibuf = d;
176 1304bbdd 2021-03-15 op dispatch_imsg(ibuf, handlers, sizeof(handlers));
180 3a227e9a 2021-03-18 op fs_init(void)
182 63a715ea 2021-03-18 op char dir[PATH_MAX];
184 63a715ea 2021-03-18 op strlcpy(dir, getenv("HOME"), sizeof(dir));
185 63a715ea 2021-03-18 op strlcat(dir, "/.telescope", sizeof(dir));
186 63a715ea 2021-03-18 op mkdir(dir, 0700);
188 740f578b 2021-03-15 op strlcpy(bookmark_file, getenv("HOME"), sizeof(bookmark_file));
189 740f578b 2021-03-15 op strlcat(bookmark_file, "/.telescope/bookmarks.gmi", sizeof(bookmark_file));
191 3a227e9a 2021-03-18 op strlcpy(known_hosts_file, getenv("HOME"), sizeof(known_hosts_file));
192 3a227e9a 2021-03-18 op strlcat(known_hosts_file, "/.telescope/known_hosts", sizeof(known_hosts_file));
198 3a227e9a 2021-03-18 op fs_main(struct imsgbuf *b)
202 35e1f40a 2021-03-14 op event_init();
204 1304bbdd 2021-03-15 op event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, ibuf);
205 35e1f40a 2021-03-14 op event_add(&imsgev, NULL);
207 35e1f40a 2021-03-14 op sandbox_fs_process();
209 35e1f40a 2021-03-14 op event_dispatch();
216 3a227e9a 2021-03-18 op load_certs(struct ohash *h)
218 3a227e9a 2021-03-18 op char *p, *last, *errstr, *el, *line = NULL;
220 3a227e9a 2021-03-18 op size_t linesize = 0;
221 3a227e9a 2021-03-18 op ssize_t linelen;
223 3a227e9a 2021-03-18 op struct tofu_entry *e;
225 3a227e9a 2021-03-18 op if ((f = fopen(known_hosts_file, "r")) == NULL)
228 3a227e9a 2021-03-18 op while ((linelen = getline(&line, &linesize, f)) != -1) {
229 3a227e9a 2021-03-18 op if ((e = calloc(1, sizeof(*e))) == NULL)
233 3a227e9a 2021-03-18 op for ((p = strtok_r(line, " ", &last)); p;
234 3a227e9a 2021-03-18 op (p = strtok_r(NULL, " ", &last))) {
235 3a227e9a 2021-03-18 op if (*p == '\n') {
242 3a227e9a 2021-03-18 op strlcpy(e->domain, p, sizeof(e->domain));
245 3a227e9a 2021-03-18 op strlcpy(e->hash, p, sizeof(e->hash));
248 3a227e9a 2021-03-18 op if ((el = strchr(p, '\n')) == NULL)
252 3a227e9a 2021-03-18 op /* 0 <= verified <= 1 */
253 3a227e9a 2021-03-18 op e->verified = strtonum(p, -1, 2, &errstr);
254 3a227e9a 2021-03-18 op if (errstr != NULL)
255 3a227e9a 2021-03-18 op errx(1, "verification for %s is %s: %s",
256 3a227e9a 2021-03-18 op e->domain, errstr, p);
264 3a227e9a 2021-03-18 op if (i != 0 && i != 3)
268 3a227e9a 2021-03-18 op telescope_ohash_insert(h, e);
272 3a227e9a 2021-03-18 op return ferror(f);