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 <sys/stat.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <limits.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
33 #include "telescope.h"
34 #include "pages.h"
36 static void die(void) __attribute__((__noreturn__));
37 static void serve_bookmarks(uint32_t);
38 static void send_page(struct imsg *, const uint8_t *, size_t);
39 static void handle_get(struct imsg*, size_t);
40 static void handle_quit(struct imsg*, size_t);
41 static void handle_bookmark_page(struct imsg*, size_t);
42 static void handle_save_cert(struct imsg*, size_t);
43 static void handle_update_cert(struct imsg*, size_t);
44 static void handle_file_open(struct imsg*, size_t);
45 static void handle_session_start(struct imsg*, size_t);
46 static void handle_session_tab(struct imsg*, size_t);
47 static void handle_session_tab_title(struct imsg*, size_t);
48 static void handle_session_end(struct imsg*, size_t);
49 static void handle_dispatch_imsg(int, short, void*);
50 static int fs_send_ui(int, uint32_t, int, const void *, uint16_t);
52 static struct imsgev *iev_ui;
53 static FILE *session;
55 static char lockfile_path[PATH_MAX];
56 static char bookmark_file[PATH_MAX];
57 static char known_hosts_file[PATH_MAX], known_hosts_tmp[PATH_MAX];
58 static char crashed_file[PATH_MAX];
60 char session_file[PATH_MAX];
62 static imsg_handlerfn *handlers[] = {
63 [IMSG_GET] = handle_get,
64 [IMSG_QUIT] = handle_quit,
65 [IMSG_BOOKMARK_PAGE] = handle_bookmark_page,
66 [IMSG_SAVE_CERT] = handle_save_cert,
67 [IMSG_UPDATE_CERT] = handle_update_cert,
68 [IMSG_FILE_OPEN] = handle_file_open,
69 [IMSG_SESSION_START] = handle_session_start,
70 [IMSG_SESSION_TAB] = handle_session_tab,
71 [IMSG_SESSION_TAB_TITLE] = handle_session_tab_title,
72 [IMSG_SESSION_END] = handle_session_end,
73 };
75 static void __attribute__((__noreturn__))
76 die(void)
77 {
78 abort(); /* TODO */
79 }
81 static void
82 serve_bookmarks(uint32_t peerid)
83 {
84 const char *t;
85 char buf[BUFSIZ];
86 size_t r;
87 FILE *f;
89 if ((f = fopen(bookmark_file, "r")) == NULL) {
90 t = "# Bookmarks\n\n"
91 "No bookmarks yet!\n"
92 "Create ~/.telescope/bookmarks.gmi or use `bookmark-page'.\n";
93 fs_send_ui(IMSG_BUF, peerid, -1, t, strlen(t));
94 fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
95 return;
96 }
98 for (;;) {
99 r = fread(buf, 1, sizeof(buf), f);
100 fs_send_ui(IMSG_BUF, peerid, -1, buf, r);
101 if (r != sizeof(buf))
102 break;
105 fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
107 fclose(f);
110 static void
111 send_page(struct imsg *imsg, const uint8_t *page, size_t len)
113 fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, page, len);
114 fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
117 static void
118 handle_get(struct imsg *imsg, size_t datalen)
120 const char *data, *p;
121 size_t i;
122 struct page {
123 const char *name;
124 void (*handle)(uint32_t);
125 const uint8_t *data;
126 size_t len;
127 } pages[] = {
128 {"about:about", NULL, about_about, about_about_len},
129 {"about:blank", NULL, about_blank, about_blank_len},
130 {"about:bookmarks", serve_bookmarks, 0, 0},
131 {"about:crash", NULL, about_crash, about_crash_len},
132 {"about:help", NULL, about_help, about_help_len},
133 {"about:license", NULL, about_license, about_license_len},
134 {"about:new", NULL, about_new, about_new_len},
135 };
137 data = imsg->data;
139 if (data[datalen-1] != '\0')
140 die();
142 for (i = 0; i < sizeof(pages)/sizeof(pages[0]); ++i) {
143 if (strcmp(data, pages[i].name) != 0)
144 continue;
146 if (pages[i].handle != NULL)
147 pages[i].handle(imsg->hdr.peerid);
148 else
149 send_page(imsg, pages[i].data, pages[i].len);
150 return;
153 p = "# not found!\n";
154 fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, p, strlen(p));
155 fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
158 static void
159 handle_quit(struct imsg *imsg, size_t datalen)
161 event_loopbreak();
164 static void
165 handle_bookmark_page(struct imsg *imsg, size_t datalen)
167 char *data;
168 int res;
169 FILE *f;
171 data = imsg->data;
172 if (data[datalen-1] != '\0')
173 die();
175 if ((f = fopen(bookmark_file, "a")) == NULL) {
176 res = errno;
177 goto end;
179 fprintf(f, "=> %s\n", data);
180 fclose(f);
182 res = 0;
183 end:
184 fs_send_ui(IMSG_BOOKMARK_OK, 0, -1, &res, sizeof(res));
187 static void
188 handle_save_cert(struct imsg *imsg, size_t datalen)
190 struct tofu_entry e;
191 FILE *f;
192 int res;
194 /* TODO: traverse the file to avoid duplications? */
196 if (datalen != sizeof(e))
197 die();
198 memcpy(&e, imsg->data, datalen);
200 if ((f = fopen(known_hosts_file, "a")) == NULL) {
201 res = errno;
202 goto end;
204 fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
205 fclose(f);
207 res = 0;
208 end:
209 fs_send_ui(IMSG_SAVE_CERT_OK, imsg->hdr.peerid, -1,
210 &res, sizeof(res));
213 static void
214 handle_update_cert(struct imsg *imsg, size_t datalen)
216 FILE *tmp, *f;
217 struct tofu_entry entry;
218 char sfn[PATH_MAX], *line = NULL, *t;
219 size_t l, linesize = 0;
220 ssize_t linelen;
221 int fd, e, res = 0;
223 if (datalen != sizeof(entry))
224 die();
225 memcpy(&entry, imsg->data, datalen);
227 strlcpy(sfn, known_hosts_tmp, sizeof(sfn));
228 if ((fd = mkstemp(sfn)) == -1 ||
229 (tmp = fdopen(fd, "w")) == NULL) {
230 if (fd != -1) {
231 unlink(sfn);
232 close(fd);
234 res = 0;
235 goto end;
238 if ((f = fopen(known_hosts_file, "r")) == NULL) {
239 unlink(sfn);
240 fclose(tmp);
241 res = 0;
242 goto end;
245 l = strlen(entry.domain);
246 while ((linelen = getline(&line, &linesize, f)) != -1) {
247 if ((t = strstr(line, entry.domain)) != NULL &&
248 (line[l] == ' ' || line[l] == '\t'))
249 continue;
250 /* line has a trailing \n */
251 fprintf(tmp, "%s", line);
253 fprintf(tmp, "%s %s %d\n", entry.domain, entry.hash, entry.verified);
255 free(line);
256 e = ferror(tmp);
258 fclose(tmp);
259 fclose(f);
261 if (e) {
262 unlink(sfn);
263 res = 0;
264 goto end;
267 res = rename(sfn, known_hosts_file) != -1;
269 end:
270 fs_send_ui(IMSG_UPDATE_CERT_OK, imsg->hdr.peerid, -1,
271 &res, sizeof(res));
274 static void
275 handle_file_open(struct imsg *imsg, size_t datalen)
277 char *path, *e;
278 int fd;
280 path = imsg->data;
281 if (path[datalen-1] != '\0')
282 die();
284 if ((fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644)) == -1) {
285 e = strerror(errno);
286 fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, -1,
287 e, strlen(e)+1);
288 } else
289 fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, fd,
290 NULL, 0);
293 static void
294 handle_session_start(struct imsg *imsg, size_t datalen)
296 if (datalen != 0)
297 die();
299 if ((session = fopen(session_file, "w")) == NULL)
300 die();
303 static void
304 handle_session_tab(struct imsg *imsg, size_t datalen)
306 char *url;
307 uint32_t flags;
309 if (session == NULL)
310 die();
312 flags = imsg->hdr.peerid;
313 url = imsg->data;
314 if (datalen == 0 || url[datalen-1] != '\0')
315 die();
316 fprintf(session, "%s", url);
318 if (flags & TAB_CURRENT)
319 fprintf(session, " current ");
320 else
321 fprintf(session, " - ");
324 static void
325 handle_session_tab_title(struct imsg *imsg, size_t datalen)
327 const char *title;
329 title = imsg->data;
330 if (title == NULL) {
331 datalen = 1;
332 title = "";
335 if (title[datalen-1] != '\0')
336 die();
338 fprintf(session, "%s\n", title);
341 static void
342 handle_session_end(struct imsg *imsg, size_t datalen)
344 if (session == NULL)
345 die();
346 fclose(session);
347 session = NULL;
350 static void
351 handle_dispatch_imsg(int fd, short ev, void *d)
353 struct imsgev *iev = d;
354 int e;
356 if (dispatch_imsg(iev, ev, handlers, sizeof(handlers)) == -1) {
357 /*
358 * This should leave a ~/.telescope/crashed file to
359 * trigger about:crash on next run. Unfortunately, if
360 * the main process dies the fs sticks around and
361 * doesn't notice that the fd was closed. Why EV_READ
362 * is not triggered when a fd is closed on the other end?
363 */
364 e = errno;
365 if ((fd = open(crashed_file, O_CREAT|O_TRUNC|O_WRONLY, 0600))
366 == -1)
367 err(1, "open");
368 close(fd);
369 errx(1, "connection closed: %s", strerror(e));
373 static int
374 fs_send_ui(int type, uint32_t peerid, int fd, const void *data,
375 uint16_t datalen)
377 return imsg_compose_event(iev_ui, type, peerid, 0, fd,
378 data, datalen);
381 int
382 fs_init(void)
384 char dir[PATH_MAX];
386 strlcpy(dir, getenv("HOME"), sizeof(dir));
387 strlcat(dir, "/.telescope", sizeof(dir));
388 mkdir(dir, 0700);
390 strlcpy(lockfile_path, getenv("HOME"), sizeof(lockfile_path));
391 strlcat(lockfile_path, "/.telescope/lock", sizeof(lockfile_path));
393 strlcpy(bookmark_file, getenv("HOME"), sizeof(bookmark_file));
394 strlcat(bookmark_file, "/.telescope/bookmarks.gmi", sizeof(bookmark_file));
396 strlcpy(known_hosts_file, getenv("HOME"), sizeof(known_hosts_file));
397 strlcat(known_hosts_file, "/.telescope/known_hosts", sizeof(known_hosts_file));
399 strlcpy(known_hosts_tmp, getenv("HOME"), sizeof(known_hosts_tmp));
400 strlcat(known_hosts_tmp, "/.telescope/known_hosts.tmp.XXXXXXXXXX",
401 sizeof(known_hosts_file));
403 strlcpy(session_file, getenv("HOME"), sizeof(session_file));
404 strlcat(session_file, "/.telescope/session", sizeof(session_file));
406 strlcpy(crashed_file, getenv("HOME"), sizeof(crashed_file));
407 strlcat(crashed_file, "/.telescope/crashed", sizeof(crashed_file));
409 return 1;
412 int
413 fs_main(void)
415 setproctitle("fs");
417 fs_init();
419 event_init();
421 /* Setup pipe and event handler to the main process */
422 if ((iev_ui = malloc(sizeof(*iev_ui))) == NULL)
423 die();
424 imsg_init(&iev_ui->ibuf, 3);
425 iev_ui->handler = handle_dispatch_imsg;
426 iev_ui->events = EV_READ;
427 event_set(&iev_ui->ev, iev_ui->ibuf.fd, iev_ui->events,
428 iev_ui->handler, iev_ui);
429 event_add(&iev_ui->ev, NULL);
431 sandbox_fs_process();
433 event_dispatch();
434 return 0;
439 int
440 last_time_crashed(void)
442 int fd;
444 if ((fd = open(crashed_file, O_RDONLY)) == -1)
445 return 0;
447 close(fd);
448 unlink(crashed_file);
449 return 1;
452 int
453 lock_session(void)
455 struct flock lock;
456 int fd;
458 if ((fd = open(lockfile_path, O_WRONLY|O_CREAT, 0600)) == -1)
459 return -1;
461 lock.l_start = 0;
462 lock.l_len = 0;
463 lock.l_type = F_WRLCK;
464 lock.l_whence = SEEK_SET;
466 if (fcntl(fd, F_SETLK, &lock) == -1) {
467 close(fd);
468 return -1;
471 return fd;
474 static int
475 parse_khost_line(char *line, char *tmp[3])
477 char **ap;
479 for (ap = tmp; ap < &tmp[3] &&
480 (*ap = strsep(&line, " \t\n")) != NULL;) {
481 if (**ap != '\0')
482 ap++;
485 return ap == &tmp[3] && *line == '\0';
488 int
489 load_certs(struct ohash *h)
491 char *tmp[3], *line = NULL;
492 const char *errstr;
493 size_t lineno = 0, linesize = 0;
494 ssize_t linelen;
495 FILE *f;
496 struct tofu_entry *e;
498 if ((f = fopen(known_hosts_file, "r")) == NULL)
499 return 0;
501 while ((linelen = getline(&line, &linesize, f)) != -1) {
502 if ((e = calloc(1, sizeof(*e))) == NULL)
503 abort();
505 lineno++;
507 if (parse_khost_line(line, tmp)) {
508 strlcpy(e->domain, tmp[0], sizeof(e->domain));
509 strlcpy(e->hash, tmp[1], sizeof(e->hash));
511 e->verified = strtonum(tmp[2], 0, 1, &errstr);
512 if (errstr != NULL)
513 errx(1, "%s:%zu verification for %s is %s: %s",
514 known_hosts_file, lineno,
515 e->domain, errstr, tmp[2]);
516 tofu_add(h, e);
517 } else {
518 warnx("%s:%zu invalid entry",
519 known_hosts_file, lineno);
520 free(e);
524 free(line);
525 return ferror(f);