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 6cd6a9e1 2021-03-20 op #include <sys/stat.h>
27 35e1f40a 2021-03-14 op #include <errno.h>
28 35e1f40a 2021-03-14 op #include <limits.h>
29 35e1f40a 2021-03-14 op #include <stdio.h>
30 35e1f40a 2021-03-14 op #include <stdlib.h>
31 35e1f40a 2021-03-14 op #include <string.h>
32 35e1f40a 2021-03-14 op #include <unistd.h>
34 35e1f40a 2021-03-14 op static void die(void) __attribute__((__noreturn__));
35 35e1f40a 2021-03-14 op static void serve_bookmarks(uint32_t);
36 35e1f40a 2021-03-14 op static void handle_get(struct imsg*, size_t);
37 35e1f40a 2021-03-14 op static void handle_quit(struct imsg*, size_t);
38 740f578b 2021-03-15 op static void handle_bookmark_page(struct imsg*, size_t);
39 3a227e9a 2021-03-18 op static void handle_save_cert(struct imsg*, size_t);
40 c7107cec 2021-04-01 op static void handle_session_start(struct imsg*, size_t);
41 c7107cec 2021-04-01 op static void handle_session_tab(struct imsg*, size_t);
42 c7107cec 2021-04-01 op static void handle_session_end(struct imsg*, size_t);
43 1304bbdd 2021-03-15 op static void handle_dispatch_imsg(int, short, void*);
45 35e1f40a 2021-03-14 op static struct event imsgev;
46 35e1f40a 2021-03-14 op static struct imsgbuf *ibuf;
48 c7107cec 2021-04-01 op static FILE *session;
50 740f578b 2021-03-15 op static char bookmark_file[PATH_MAX];
51 3a227e9a 2021-03-18 op static char known_hosts_file[PATH_MAX];
52 c7107cec 2021-04-01 op static char session_file[PATH_MAX];
54 35e1f40a 2021-03-14 op static imsg_handlerfn *handlers[] = {
55 35e1f40a 2021-03-14 op [IMSG_GET] = handle_get,
56 35e1f40a 2021-03-14 op [IMSG_QUIT] = handle_quit,
57 740f578b 2021-03-15 op [IMSG_BOOKMARK_PAGE] = handle_bookmark_page,
58 3a227e9a 2021-03-18 op [IMSG_SAVE_CERT] = handle_save_cert,
59 c7107cec 2021-04-01 op [IMSG_SESSION_START] = handle_session_start,
60 c7107cec 2021-04-01 op [IMSG_SESSION_TAB] = handle_session_tab,
61 c7107cec 2021-04-01 op [IMSG_SESSION_END] = handle_session_end,
64 35e1f40a 2021-03-14 op static void __attribute__((__noreturn__))
67 35e1f40a 2021-03-14 op abort(); /* TODO */
71 35e1f40a 2021-03-14 op serve_bookmarks(uint32_t peerid)
73 35e1f40a 2021-03-14 op const char *t;
74 740f578b 2021-03-15 op char buf[BUFSIZ];
78 740f578b 2021-03-15 op if ((f = fopen(bookmark_file, "r")) == NULL) {
79 503425db 2021-04-01 op t = "# Bookmarks\n\n"
80 503425db 2021-04-01 op "No bookmarks yet!\n"
81 503425db 2021-04-01 op "Create ~/.telescope/bookmarks.gmi or use `bookmark-page'.\n";
82 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_BUF, peerid, 0, -1, t, strlen(t));
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);
89 35e1f40a 2021-03-14 op r = fread(buf, 1, sizeof(buf), f);
90 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_BUF, peerid, 0, -1, buf, r);
91 35e1f40a 2021-03-14 op imsg_flush(ibuf);
92 35e1f40a 2021-03-14 op if (r != sizeof(buf))
96 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_EOF, peerid, 0, -1, NULL, 0);
97 35e1f40a 2021-03-14 op imsg_flush(ibuf);
103 35e1f40a 2021-03-14 op handle_get(struct imsg *imsg, size_t datalen)
106 35e1f40a 2021-03-14 op const char *p;
108 35e1f40a 2021-03-14 op data = imsg->data;
110 35e1f40a 2021-03-14 op if (data[datalen-1] != '\0')
113 35e1f40a 2021-03-14 op if (!strcmp(data, "about:new")) {
114 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_BUF, imsg->hdr.peerid, 0, -1,
115 35e1f40a 2021-03-14 op about_new, strlen(about_new));
116 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_EOF, imsg->hdr.peerid, 0, -1, NULL, 0);
117 35e1f40a 2021-03-14 op imsg_flush(ibuf);
118 35e1f40a 2021-03-14 op } else if (!strcmp(data, "about:bookmarks")) {
119 35e1f40a 2021-03-14 op serve_bookmarks(imsg->hdr.peerid);
121 35e1f40a 2021-03-14 op p = "# not found!\n";
122 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_BUF, imsg->hdr.peerid, 0, -1, p, strlen(p));
123 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_EOF, imsg->hdr.peerid, 0, -1, NULL, 0);
124 35e1f40a 2021-03-14 op imsg_flush(ibuf);
129 35e1f40a 2021-03-14 op handle_quit(struct imsg *imsg, size_t datalen)
131 35e1f40a 2021-03-14 op event_loopbreak();
135 740f578b 2021-03-15 op handle_bookmark_page(struct imsg *imsg, size_t datalen)
141 740f578b 2021-03-15 op data = imsg->data;
142 740f578b 2021-03-15 op if (data[datalen-1] != '\0')
145 740f578b 2021-03-15 op if ((f = fopen(bookmark_file, "a")) == NULL) {
149 740f578b 2021-03-15 op fprintf(f, "=> %s\n", data);
154 740f578b 2021-03-15 op imsg_compose(ibuf, IMSG_BOOKMARK_OK, 0, 0, -1, &res, sizeof(res));
155 740f578b 2021-03-15 op imsg_flush(ibuf);
159 3a227e9a 2021-03-18 op handle_save_cert(struct imsg *imsg, size_t datalen)
161 3a227e9a 2021-03-18 op struct tofu_entry e;
165 3a227e9a 2021-03-18 op /* TODO: traverse the file to avoid duplications? */
167 3a227e9a 2021-03-18 op if (datalen != sizeof(e))
169 3a227e9a 2021-03-18 op memcpy(&e, imsg->data, datalen);
171 3a227e9a 2021-03-18 op if ((f = fopen(known_hosts_file, "a")) == NULL) {
175 3a227e9a 2021-03-18 op fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
180 3a227e9a 2021-03-18 op imsg_compose(ibuf, IMSG_SAVE_CERT_OK, imsg->hdr.peerid, 0, -1,
181 3a227e9a 2021-03-18 op &res, sizeof(res));
182 3a227e9a 2021-03-18 op imsg_flush(ibuf);
186 c7107cec 2021-04-01 op handle_session_start(struct imsg *imsg, size_t datalen)
188 c7107cec 2021-04-01 op if (datalen != 0)
191 c7107cec 2021-04-01 op if ((session = fopen(session_file, "w")) == NULL)
196 c7107cec 2021-04-01 op handle_session_tab(struct imsg *imsg, size_t datalen)
198 c7107cec 2021-04-01 op if (session == NULL)
201 c7107cec 2021-04-01 op if (datalen == 0)
204 c7107cec 2021-04-01 op /* skip the NUL-terminator */
205 c7107cec 2021-04-01 op fwrite(imsg->data, 1, datalen-1, session);
207 c7107cec 2021-04-01 op fprintf(session, "\n");
211 c7107cec 2021-04-01 op handle_session_end(struct imsg *imsg, size_t datalen)
213 c7107cec 2021-04-01 op if (session == NULL)
215 c7107cec 2021-04-01 op fclose(session);
216 c7107cec 2021-04-01 op session = NULL;
220 1304bbdd 2021-03-15 op handle_dispatch_imsg(int fd, short ev, void *d)
222 35e1f40a 2021-03-14 op struct imsgbuf *ibuf = d;
223 1304bbdd 2021-03-15 op dispatch_imsg(ibuf, handlers, sizeof(handlers));
227 3a227e9a 2021-03-18 op fs_init(void)
229 63a715ea 2021-03-18 op char dir[PATH_MAX];
231 63a715ea 2021-03-18 op strlcpy(dir, getenv("HOME"), sizeof(dir));
232 63a715ea 2021-03-18 op strlcat(dir, "/.telescope", sizeof(dir));
233 63a715ea 2021-03-18 op mkdir(dir, 0700);
235 740f578b 2021-03-15 op strlcpy(bookmark_file, getenv("HOME"), sizeof(bookmark_file));
236 740f578b 2021-03-15 op strlcat(bookmark_file, "/.telescope/bookmarks.gmi", sizeof(bookmark_file));
238 3a227e9a 2021-03-18 op strlcpy(known_hosts_file, getenv("HOME"), sizeof(known_hosts_file));
239 3a227e9a 2021-03-18 op strlcat(known_hosts_file, "/.telescope/known_hosts", sizeof(known_hosts_file));
241 c7107cec 2021-04-01 op strlcpy(session_file, getenv("HOME"), sizeof(session_file));
242 c7107cec 2021-04-01 op strlcat(session_file, "/.telescope/session", sizeof(session_file));
248 3a227e9a 2021-03-18 op fs_main(struct imsgbuf *b)
252 35e1f40a 2021-03-14 op event_init();
254 1304bbdd 2021-03-15 op event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, ibuf);
255 35e1f40a 2021-03-14 op event_add(&imsgev, NULL);
257 35e1f40a 2021-03-14 op sandbox_fs_process();
259 35e1f40a 2021-03-14 op event_dispatch();
266 3a227e9a 2021-03-18 op load_certs(struct ohash *h)
268 6cd6a9e1 2021-03-20 op char *p, *last, *el, *line = NULL;
269 6cd6a9e1 2021-03-20 op const char *errstr;
271 3a227e9a 2021-03-18 op size_t linesize = 0;
272 3a227e9a 2021-03-18 op ssize_t linelen;
274 3a227e9a 2021-03-18 op struct tofu_entry *e;
276 3a227e9a 2021-03-18 op if ((f = fopen(known_hosts_file, "r")) == NULL)
279 3a227e9a 2021-03-18 op while ((linelen = getline(&line, &linesize, f)) != -1) {
280 3a227e9a 2021-03-18 op if ((e = calloc(1, sizeof(*e))) == NULL)
284 3a227e9a 2021-03-18 op for ((p = strtok_r(line, " ", &last)); p;
285 3a227e9a 2021-03-18 op (p = strtok_r(NULL, " ", &last))) {
286 3a227e9a 2021-03-18 op if (*p == '\n') {
293 3a227e9a 2021-03-18 op strlcpy(e->domain, p, sizeof(e->domain));
296 3a227e9a 2021-03-18 op strlcpy(e->hash, p, sizeof(e->hash));
299 3a227e9a 2021-03-18 op if ((el = strchr(p, '\n')) == NULL)
303 3a227e9a 2021-03-18 op /* 0 <= verified <= 1 */
304 3a227e9a 2021-03-18 op e->verified = strtonum(p, -1, 2, &errstr);
305 3a227e9a 2021-03-18 op if (errstr != NULL)
306 3a227e9a 2021-03-18 op errx(1, "verification for %s is %s: %s",
307 3a227e9a 2021-03-18 op e->domain, errstr, p);
315 3a227e9a 2021-03-18 op if (i != 0 && i != 3)
319 3a227e9a 2021-03-18 op telescope_ohash_insert(h, e);
323 3a227e9a 2021-03-18 op return ferror(f);
327 c7107cec 2021-04-01 op load_last_session(void (*cb)(const char*))
329 c7107cec 2021-04-01 op char *nl, *line = NULL;
331 c7107cec 2021-04-01 op size_t linesize = 0;
332 c7107cec 2021-04-01 op ssize_t linelen;
333 c7107cec 2021-04-01 op FILE *session;
335 c7107cec 2021-04-01 op if ((session = fopen(session_file, "r")) == NULL)
338 c7107cec 2021-04-01 op while ((linelen = getline(&line, &linesize, session)) != -1) {
339 c7107cec 2021-04-01 op if ((nl = strchr(line, '\n')) != NULL)
345 c7107cec 2021-04-01 op e = ferror(session);
346 c7107cec 2021-04-01 op fclose(session);