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 * Handles the data in ~/.telescope
19 *
20 * TODO: add some form of locking on the files
21 */
23 #include "telescope.h"
25 #include <sys/stat.h>
27 #include <errno.h>
28 #include <limits.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
34 static void die(void) __attribute__((__noreturn__));
35 static void serve_bookmarks(uint32_t);
36 static void handle_get(struct imsg*, size_t);
37 static void handle_quit(struct imsg*, size_t);
38 static void handle_bookmark_page(struct imsg*, size_t);
39 static void handle_save_cert(struct imsg*, size_t);
40 static void handle_session_start(struct imsg*, size_t);
41 static void handle_session_tab(struct imsg*, size_t);
42 static void handle_session_end(struct imsg*, size_t);
43 static void handle_dispatch_imsg(int, short, void*);
45 static struct event imsgev;
46 static struct imsgbuf *ibuf;
48 static FILE *session;
50 static char bookmark_file[PATH_MAX];
51 static char known_hosts_file[PATH_MAX];
52 static char session_file[PATH_MAX];
54 static imsg_handlerfn *handlers[] = {
55 [IMSG_GET] = handle_get,
56 [IMSG_QUIT] = handle_quit,
57 [IMSG_BOOKMARK_PAGE] = handle_bookmark_page,
58 [IMSG_SAVE_CERT] = handle_save_cert,
59 [IMSG_SESSION_START] = handle_session_start,
60 [IMSG_SESSION_TAB] = handle_session_tab,
61 [IMSG_SESSION_END] = handle_session_end,
62 };
64 static void __attribute__((__noreturn__))
65 die(void)
66 {
67 abort(); /* TODO */
68 }
70 static void
71 serve_bookmarks(uint32_t peerid)
72 {
73 const char *t;
74 char buf[BUFSIZ];
75 size_t r;
76 FILE *f;
78 if ((f = fopen(bookmark_file, "r")) == NULL) {
79 t = "# Bookmarks\n\n"
80 "No bookmarks yet!\n"
81 "Create ~/.telescope/bookmarks.gmi or use `bookmark-page'.\n";
82 imsg_compose(ibuf, IMSG_BUF, peerid, 0, -1, t, strlen(t));
83 imsg_compose(ibuf, IMSG_EOF, peerid, 0, -1, NULL, 0);
84 imsg_flush(ibuf);
85 return;
86 }
88 for (;;) {
89 r = fread(buf, 1, sizeof(buf), f);
90 imsg_compose(ibuf, IMSG_BUF, peerid, 0, -1, buf, r);
91 imsg_flush(ibuf);
92 if (r != sizeof(buf))
93 break;
94 }
96 imsg_compose(ibuf, IMSG_EOF, peerid, 0, -1, NULL, 0);
97 imsg_flush(ibuf);
99 fclose(f);
102 static void
103 handle_get(struct imsg *imsg, size_t datalen)
105 char *data;
106 const char *p;
108 data = imsg->data;
110 if (data[datalen-1] != '\0')
111 die();
113 if (!strcmp(data, "about:new")) {
114 imsg_compose(ibuf, IMSG_BUF, imsg->hdr.peerid, 0, -1,
115 about_new, strlen(about_new));
116 imsg_compose(ibuf, IMSG_EOF, imsg->hdr.peerid, 0, -1, NULL, 0);
117 imsg_flush(ibuf);
118 } else if (!strcmp(data, "about:bookmarks")) {
119 serve_bookmarks(imsg->hdr.peerid);
120 } else {
121 p = "# not found!\n";
122 imsg_compose(ibuf, IMSG_BUF, imsg->hdr.peerid, 0, -1, p, strlen(p));
123 imsg_compose(ibuf, IMSG_EOF, imsg->hdr.peerid, 0, -1, NULL, 0);
124 imsg_flush(ibuf);
128 static void
129 handle_quit(struct imsg *imsg, size_t datalen)
131 event_loopbreak();
134 static void
135 handle_bookmark_page(struct imsg *imsg, size_t datalen)
137 char *data;
138 int res;
139 FILE *f;
141 data = imsg->data;
142 if (data[datalen-1] != '\0')
143 die();
145 if ((f = fopen(bookmark_file, "a")) == NULL) {
146 res = errno;
147 goto end;
149 fprintf(f, "=> %s\n", data);
150 fclose(f);
152 res = 0;
153 end:
154 imsg_compose(ibuf, IMSG_BOOKMARK_OK, 0, 0, -1, &res, sizeof(res));
155 imsg_flush(ibuf);
158 static void
159 handle_save_cert(struct imsg *imsg, size_t datalen)
161 struct tofu_entry e;
162 FILE *f;
163 int res;
165 /* TODO: traverse the file to avoid duplications? */
167 if (datalen != sizeof(e))
168 die();
169 memcpy(&e, imsg->data, datalen);
171 if ((f = fopen(known_hosts_file, "a")) == NULL) {
172 res = errno;
173 goto end;
175 fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
176 fclose(f);
178 res = 0;
179 end:
180 imsg_compose(ibuf, IMSG_SAVE_CERT_OK, imsg->hdr.peerid, 0, -1,
181 &res, sizeof(res));
182 imsg_flush(ibuf);
185 static void
186 handle_session_start(struct imsg *imsg, size_t datalen)
188 if (datalen != 0)
189 die();
191 if ((session = fopen(session_file, "w")) == NULL)
192 die();
195 static void
196 handle_session_tab(struct imsg *imsg, size_t datalen)
198 if (session == NULL)
199 die();
201 if (datalen == 0)
202 die();
204 /* skip the NUL-terminator */
205 fwrite(imsg->data, 1, datalen-1, session);
207 fprintf(session, "\n");
210 static void
211 handle_session_end(struct imsg *imsg, size_t datalen)
213 if (session == NULL)
214 die();
215 fclose(session);
216 session = NULL;
219 static void
220 handle_dispatch_imsg(int fd, short ev, void *d)
222 struct imsgbuf *ibuf = d;
223 dispatch_imsg(ibuf, handlers, sizeof(handlers));
226 int
227 fs_init(void)
229 char dir[PATH_MAX];
231 strlcpy(dir, getenv("HOME"), sizeof(dir));
232 strlcat(dir, "/.telescope", sizeof(dir));
233 mkdir(dir, 0700);
235 strlcpy(bookmark_file, getenv("HOME"), sizeof(bookmark_file));
236 strlcat(bookmark_file, "/.telescope/bookmarks.gmi", sizeof(bookmark_file));
238 strlcpy(known_hosts_file, getenv("HOME"), sizeof(known_hosts_file));
239 strlcat(known_hosts_file, "/.telescope/known_hosts", sizeof(known_hosts_file));
241 strlcpy(session_file, getenv("HOME"), sizeof(session_file));
242 strlcat(session_file, "/.telescope/session", sizeof(session_file));
244 return 1;
247 int
248 fs_main(struct imsgbuf *b)
250 ibuf = b;
252 event_init();
254 event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, ibuf);
255 event_add(&imsgev, NULL);
257 sandbox_fs_process();
259 event_dispatch();
260 return 0;
265 int
266 load_certs(struct ohash *h)
268 char *p, *last, *el, *line = NULL;
269 const char *errstr;
270 int i;
271 size_t linesize = 0;
272 ssize_t linelen;
273 FILE *f;
274 struct tofu_entry *e;
276 if ((f = fopen(known_hosts_file, "r")) == NULL)
277 return 0;
279 while ((linelen = getline(&line, &linesize, f)) != -1) {
280 if ((e = calloc(1, sizeof(*e))) == NULL)
281 abort();
283 i = 0;
284 for ((p = strtok_r(line, " ", &last)); p;
285 (p = strtok_r(NULL, " ", &last))) {
286 if (*p == '\n') {
287 free(e);
288 break;
291 switch (i) {
292 case 0:
293 strlcpy(e->domain, p, sizeof(e->domain));
294 break;
295 case 1:
296 strlcpy(e->hash, p, sizeof(e->hash));
297 break;
298 case 2:
299 if ((el = strchr(p, '\n')) == NULL)
300 abort();
301 *el = '\0';
303 /* 0 <= verified <= 1 */
304 e->verified = strtonum(p, -1, 2, &errstr);
305 if (errstr != NULL)
306 errx(1, "verification for %s is %s: %s",
307 e->domain, errstr, p);
308 break;
309 default:
310 abort();
312 i++;
315 if (i != 0 && i != 3)
316 abort();
318 if (i != 0)
319 telescope_ohash_insert(h, e);
322 free(line);
323 return ferror(f);
326 int
327 load_last_session(void (*cb)(const char*))
329 char *nl, *line = NULL;
330 int e;
331 size_t linesize = 0;
332 ssize_t linelen;
333 FILE *session;
335 if ((session = fopen(session_file, "r")) == NULL)
336 return 0;
338 while ((linelen = getline(&line, &linesize, session)) != -1) {
339 if ((nl = strchr(line, '\n')) != NULL)
340 *nl = '\0';
341 cb(line);
344 free(line);
345 e = ferror(session);
346 fclose(session);
348 return !e;