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 config and runtime files
19 */
21 #include "compat.h"
23 #include <sys/stat.h>
24 #include <sys/types.h>
26 #include <dirent.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <limits.h>
30 #include <libgen.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
36 #include "fs.h"
37 #include "pages.h"
38 #include "telescope.h"
39 #include "session.h"
40 #include "utils.h"
42 static void die(void) __attribute__((__noreturn__));
43 static void send_file(uint32_t, FILE *);
44 static void handle_get(struct imsg*, size_t);
45 static int select_non_dot(const struct dirent *);
46 static int select_non_dotdot(const struct dirent *);
47 static void handle_get_file(struct imsg*, size_t);
48 static void handle_misc(struct imsg *, size_t);
49 static void handle_bookmark_page(struct imsg*, size_t);
50 static void handle_save_cert(struct imsg*, size_t);
51 static void handle_update_cert(struct imsg*, size_t);
52 static void handle_file_open(struct imsg*, size_t);
53 static void handle_session_start(struct imsg*, size_t);
54 static void handle_session_tab(struct imsg*, size_t);
55 static void handle_session_tab_hist(struct imsg*, size_t);
56 static void handle_session_end(struct imsg*, size_t);
57 static void handle_dispatch_imsg(int, short, void*);
58 static int fs_send_ui(int, uint32_t, int, const void *, uint16_t);
59 static size_t join_path(char*, const char*, const char*, size_t);
60 static void getenv_default(char*, const char*, const char*, size_t);
61 static void mkdirs(const char*, mode_t);
62 static void init_paths(void);
63 static void load_last_session(void);
64 static int last_time_crashed(void);
65 static void load_certs(void);
67 static struct imsgev *iev_ui;
68 static FILE *session;
70 /*
71 * Where to store user data. These are all equal to ~/.telescope if
72 * it exists.
73 */
74 char config_path_base[PATH_MAX];
75 char data_path_base[PATH_MAX];
76 char cache_path_base[PATH_MAX];
78 char ctlsock_path[PATH_MAX];
79 char config_path[PATH_MAX];
80 char lockfile_path[PATH_MAX];
81 char bookmark_file[PATH_MAX];
82 char known_hosts_file[PATH_MAX], known_hosts_tmp[PATH_MAX];
83 char crashed_file[PATH_MAX];
84 char session_file[PATH_MAX];
86 static imsg_handlerfn *handlers[] = {
87 [IMSG_GET] = handle_get,
88 [IMSG_GET_FILE] = handle_get_file,
89 [IMSG_QUIT] = handle_misc,
90 [IMSG_INIT] = handle_misc,
91 [IMSG_BOOKMARK_PAGE] = handle_bookmark_page,
92 [IMSG_SAVE_CERT] = handle_save_cert,
93 [IMSG_UPDATE_CERT] = handle_update_cert,
94 [IMSG_FILE_OPEN] = handle_file_open,
95 [IMSG_SESSION_START] = handle_session_start,
96 [IMSG_SESSION_TAB] = handle_session_tab,
97 [IMSG_SESSION_TAB_HIST] = handle_session_tab_hist,
98 [IMSG_SESSION_END] = handle_session_end,
99 };
101 static void __attribute__((__noreturn__))
102 die(void)
104 abort(); /* TODO */
107 static void
108 send_file(uint32_t peerid, FILE *f)
110 ssize_t r;
111 char buf[BUFSIZ];
113 for (;;) {
114 r = fread(buf, 1, sizeof(buf), f);
115 if (r != 0)
116 fs_send_ui(IMSG_BUF, peerid, -1, buf, r);
117 if (r != sizeof(buf))
118 break;
120 fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
121 fclose(f);
124 static void
125 handle_get(struct imsg *imsg, size_t datalen)
127 const char *bpath = "bookmarks.gmi";
128 char path[PATH_MAX];
129 FILE *f;
130 const char *data, *p;
131 size_t i;
132 struct page {
133 const char *name;
134 const char *path;
135 const uint8_t *data;
136 size_t len;
137 } pages[] = {
138 {"about", NULL, about_about, about_about_len},
139 {"blank", NULL, about_blank, about_blank_len},
140 {"bookmarks", bpath, bookmarks, bookmarks_len},
141 {"crash", NULL, about_crash, about_crash_len},
142 {"help", NULL, about_help, about_help_len},
143 {"license", NULL, about_license, about_license_len},
144 {"new", NULL, about_new, about_new_len},
145 }, *page = NULL;
147 data = imsg->data;
148 if (data[datalen-1] != '\0') /* make sure it's NUL-terminated */
149 die();
150 if ((data = strchr(data, ':')) == NULL)
151 goto notfound;
152 data++;
154 for (i = 0; i < sizeof(pages)/sizeof(pages[0]); ++i)
155 if (!strcmp(data, pages[i].name)) {
156 page = &pages[i];
157 break;
160 if (page == NULL)
161 goto notfound;
163 strlcpy(path, data_path_base, sizeof(path));
164 strlcat(path, "/", sizeof(path));
165 if (page->path != NULL)
166 strlcat(path, page->path, sizeof(path));
167 else {
168 strlcat(path, "pages/about_", sizeof(path));
169 strlcat(path, page->name, sizeof(path));
170 strlcat(path, ".gmi", sizeof(path));
173 if ((f = fopen(path, "r")) == NULL) {
174 fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1,
175 page->data, page->len);
176 fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1,
177 NULL, 0);
178 return;
181 send_file(imsg->hdr.peerid, f);
182 return;
184 notfound:
185 p = "# not found!\n";
186 fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, p, strlen(p));
187 fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
190 static inline void
191 send_hdr(uint32_t peerid, int code, const char *meta)
193 fs_send_ui(IMSG_GOT_CODE, peerid, -1, &code, sizeof(code));
194 fs_send_ui(IMSG_GOT_META, peerid, -1, meta, strlen(meta)+1);
197 static inline void
198 send_errno(uint32_t peerid, int code, const char *str, int no)
200 char *s;
202 if (asprintf(&s, "%s: %s", str, strerror(no)) == -1)
203 s = NULL;
205 send_hdr(peerid, code, s == NULL ? str : s);
206 free(s);
209 static inline const char *
210 file_type(const char *path)
212 struct mapping {
213 const char *ext;
214 const char *mime;
215 } ms[] = {
216 {"diff", "text/x-patch"},
217 {"gemini", "text/gemini"},
218 {"gmi", "text/gemini"},
219 {"markdown", "text/plain"},
220 {"md", "text/plain"},
221 {"patch", "text/x-patch"},
222 {"txt", "text/plain"},
223 {NULL, NULL},
224 }, *m;
225 char *dot;
227 if ((dot = strrchr(path, '.')) == NULL)
228 return NULL;
230 dot++;
232 for (m = ms; m->ext != NULL; ++m)
233 if (!strcmp(m->ext, dot))
234 return m->mime;
236 return NULL;
239 static int
240 select_non_dot(const struct dirent *d)
242 return strcmp(d->d_name, ".");
245 static int
246 select_non_dotdot(const struct dirent *d)
248 return strcmp(d->d_name, ".") && strcmp(d->d_name, "..");
251 static inline void
252 send_dir(uint32_t peerid, const char *path)
254 struct dirent **names;
255 struct evbuffer *ev;
256 char *s;
257 int (*selector)(const struct dirent *) = select_non_dot;
258 int i, len, no;
260 if (!has_suffix(path, "/")) {
261 if (asprintf(&s, "%s/", path) == -1)
262 die();
263 send_hdr(peerid, 30, s);
264 free(s);
265 return;
268 if (!strcmp(path, "/"))
269 selector = select_non_dotdot;
271 if ((ev = evbuffer_new()) == NULL ||
272 (len = scandir(path, &names, selector, alphasort)) == -1) {
273 no = errno;
274 evbuffer_free(ev);
275 send_errno(peerid, 40, "failure reading the directory", no);
276 return;
279 evbuffer_add_printf(ev, "# Index of %s\n\n", path);
280 for (i = 0; i < len; ++i) {
281 evbuffer_add_printf(ev, "=> %s", names[i]->d_name);
282 if (names[i]->d_type == DT_DIR)
283 evbuffer_add(ev, "/", 1);
284 evbuffer_add(ev, "\n", 1);
287 send_hdr(peerid, 20, "text/gemini");
288 fs_send_ui(IMSG_BUF, peerid, -1,
289 EVBUFFER_DATA(ev), EVBUFFER_LENGTH(ev));
290 fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
292 evbuffer_free(ev);
293 free(names);
296 static void
297 handle_get_file(struct imsg *imsg, size_t datalen)
299 struct stat sb;
300 FILE *f;
301 char *data;
302 const char *meta = NULL;
304 data = imsg->data;
305 data[datalen-1] = '\0';
307 if ((f = fopen(data, "r")) == NULL) {
308 send_errno(imsg->hdr.peerid, 51, "can't open", errno);
309 return;
312 if (fstat(fileno(f), &sb) == -1) {
313 send_errno(imsg->hdr.peerid, 40, "fstat", errno);
314 return;
317 if (S_ISDIR(sb.st_mode)) {
318 fclose(f);
319 send_dir(imsg->hdr.peerid, data);
320 return;
323 if ((meta = file_type(data)) == NULL) {
324 fclose(f);
325 send_hdr(imsg->hdr.peerid, 51,
326 "don't know how to visualize this file");
327 return;
330 send_hdr(imsg->hdr.peerid, 20, meta);
331 send_file(imsg->hdr.peerid, f);
334 static void
335 handle_misc(struct imsg *imsg, size_t datalen)
337 switch (imsg->hdr.type) {
338 case IMSG_INIT:
339 load_certs();
340 load_last_session();
341 break;
343 case IMSG_QUIT:
344 if (!safe_mode)
345 unlink(crashed_file);
346 event_loopbreak();
347 break;
349 default:
350 die();
354 static void
355 handle_bookmark_page(struct imsg *imsg, size_t datalen)
357 char *data;
358 int res;
359 FILE *f;
361 data = imsg->data;
362 if (data[datalen-1] != '\0')
363 die();
365 if ((f = fopen(bookmark_file, "a")) == NULL) {
366 res = errno;
367 goto end;
369 fprintf(f, "=> %s\n", data);
370 fclose(f);
372 res = 0;
373 end:
374 fs_send_ui(IMSG_BOOKMARK_OK, 0, -1, &res, sizeof(res));
377 static void
378 handle_save_cert(struct imsg *imsg, size_t datalen)
380 struct tofu_entry e;
381 FILE *f;
382 int res;
384 /* TODO: traverse the file to avoid duplications? */
386 if (datalen != sizeof(e))
387 die();
388 memcpy(&e, imsg->data, datalen);
390 if ((f = fopen(known_hosts_file, "a")) == NULL) {
391 res = errno;
392 goto end;
394 fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
395 fclose(f);
397 res = 0;
398 end:
399 fs_send_ui(IMSG_SAVE_CERT_OK, imsg->hdr.peerid, -1,
400 &res, sizeof(res));
403 static void
404 handle_update_cert(struct imsg *imsg, size_t datalen)
406 FILE *tmp, *f;
407 struct tofu_entry entry;
408 char sfn[PATH_MAX], *line = NULL, *t;
409 size_t l, linesize = 0;
410 ssize_t linelen;
411 int fd, e, res = 0;
413 if (datalen != sizeof(entry))
414 die();
415 memcpy(&entry, imsg->data, datalen);
417 strlcpy(sfn, known_hosts_tmp, sizeof(sfn));
418 if ((fd = mkstemp(sfn)) == -1 ||
419 (tmp = fdopen(fd, "w")) == NULL) {
420 if (fd != -1) {
421 unlink(sfn);
422 close(fd);
424 res = 0;
425 goto end;
428 if ((f = fopen(known_hosts_file, "r")) == NULL) {
429 unlink(sfn);
430 fclose(tmp);
431 res = 0;
432 goto end;
435 l = strlen(entry.domain);
436 while ((linelen = getline(&line, &linesize, f)) != -1) {
437 if ((t = strstr(line, entry.domain)) != NULL &&
438 (line[l] == ' ' || line[l] == '\t'))
439 continue;
440 /* line has a trailing \n */
441 fprintf(tmp, "%s", line);
443 fprintf(tmp, "%s %s %d\n", entry.domain, entry.hash, entry.verified);
445 free(line);
446 e = ferror(tmp);
448 fclose(tmp);
449 fclose(f);
451 if (e) {
452 unlink(sfn);
453 res = 0;
454 goto end;
457 res = rename(sfn, known_hosts_file) != -1;
459 end:
460 fs_send_ui(IMSG_UPDATE_CERT_OK, imsg->hdr.peerid, -1,
461 &res, sizeof(res));
464 static void
465 handle_file_open(struct imsg *imsg, size_t datalen)
467 char *path, *e;
468 int fd;
470 path = imsg->data;
471 if (path[datalen-1] != '\0')
472 die();
474 if ((fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644)) == -1) {
475 e = strerror(errno);
476 fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, -1,
477 e, strlen(e)+1);
478 } else
479 fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, fd,
480 NULL, 0);
483 static void
484 handle_session_start(struct imsg *imsg, size_t datalen)
486 if (datalen != 0)
487 die();
489 if ((session = fopen(session_file, "w")) == NULL)
490 die();
493 static void
494 handle_session_tab(struct imsg *imsg, size_t datalen)
496 struct session_tab tab;
498 if (session == NULL)
499 die();
501 if (datalen != sizeof(tab))
502 die();
504 memcpy(&tab, imsg->data, sizeof(tab));
505 if (tab.uri[sizeof(tab.uri)-1] != '\0' ||
506 tab.title[sizeof(tab.title)-1] != '\0')
507 die();
509 fprintf(session, "%s ", tab.uri);
511 if (tab.flags & TAB_CURRENT)
512 fprintf(session, "current,");
513 if (tab.flags & TAB_KILLED)
514 fprintf(session, "killed,");
516 fprintf(session, "top=%zu,cur=%zu %s\n", tab.top_line,
517 tab.current_line, tab.title);
520 static void
521 handle_session_tab_hist(struct imsg *imsg, size_t datalen)
523 struct session_tab_hist th;
525 if (session == NULL)
526 die();
528 if (datalen != sizeof(th))
529 die();
531 memcpy(&th, imsg->data, sizeof(th));
532 if (th.uri[sizeof(th.uri)-1] != '\0')
533 die();
535 fprintf(session, "%s %s\n", th.future ? ">" : "<", th.uri);
538 static void
539 handle_session_end(struct imsg *imsg, size_t datalen)
541 if (session == NULL)
542 die();
543 fclose(session);
544 session = NULL;
547 static void
548 handle_dispatch_imsg(int fd, short ev, void *d)
550 struct imsgev *iev = d;
551 int e;
553 if (dispatch_imsg(iev, ev, handlers, sizeof(handlers)) == -1) {
554 /*
555 * This should leave a ~/.cache/telescope/crashed file to
556 * trigger about:crash on next run. Unfortunately, if
557 * the main process dies the fs sticks around and
558 * doesn't notice that the fd was closed. Why EV_READ
559 * is not triggered when a fd is closed on the other end?
560 */
561 e = errno;
562 if ((fd = open(crashed_file, O_CREAT|O_TRUNC|O_WRONLY, 0600))
563 == -1)
564 err(1, "open");
565 close(fd);
566 errx(1, "connection closed: %s", strerror(e));
570 static int
571 fs_send_ui(int type, uint32_t peerid, int fd, const void *data,
572 uint16_t datalen)
574 return imsg_compose_event(iev_ui, type, peerid, 0, fd,
575 data, datalen);
578 static size_t
579 join_path(char *buf, const char *lhs, const char *rhs, size_t buflen)
581 strlcpy(buf, lhs, buflen);
582 return strlcat(buf, rhs, buflen);
585 static void
586 getenv_default(char *buf, const char *name, const char *def, size_t buflen)
588 size_t ret;
589 char *home, *env;
591 if ((home = getenv("HOME")) == NULL)
592 errx(1, "HOME is not defined");
594 if ((env = getenv(name)) != NULL)
595 ret = strlcpy(buf, env, buflen);
596 else
597 ret = join_path(buf, home, def, buflen);
599 if (ret >= buflen)
600 errx(1, "buffer too small for %s", name);
603 static void
604 mkdirs(const char *path, mode_t mode)
606 char copy[PATH_MAX+1], orig[PATH_MAX+1], *parent;
608 strlcpy(copy, path, sizeof(copy));
609 strlcpy(orig, path, sizeof(orig));
610 parent = dirname(copy);
611 if (!strcmp(parent, "/"))
612 return;
613 mkdirs(parent, mode);
615 if (mkdir(orig, mode) != 0) {
616 if (errno == EEXIST)
617 return;
618 err(1, "can't mkdir %s", orig);
622 static void
623 init_paths(void)
625 char xdg_config_base[PATH_MAX];
626 char xdg_data_base[PATH_MAX];
627 char xdg_cache_base[PATH_MAX];
628 char old_path[PATH_MAX];
629 char *home;
630 struct stat info;
632 /* old path */
633 if ((home = getenv("HOME")) == NULL)
634 errx(1, "HOME is not defined");
635 join_path(old_path, home, "/.telescope", sizeof(old_path));
637 /* if ~/.telescope exists, use that instead of xdg dirs */
638 if (stat(old_path, &info) == 0 && S_ISDIR(info.st_mode)) {
639 join_path(config_path_base, home, "/.telescope",
640 sizeof(config_path_base));
641 join_path(data_path_base, home, "/.telescope",
642 sizeof(data_path_base));
643 join_path(cache_path_base, home, "/.telescope",
644 sizeof(cache_path_base));
645 return;
648 /* xdg paths */
649 getenv_default(xdg_config_base, "XDG_CONFIG_HOME", "/.config",
650 sizeof(xdg_config_base));
651 getenv_default(xdg_data_base, "XDG_DATA_HOME", "/.local/share",
652 sizeof(xdg_data_base));
653 getenv_default(xdg_cache_base, "XDG_CACHE_HOME", "/.cache",
654 sizeof(xdg_cache_base));
656 join_path(config_path_base, xdg_config_base, "/telescope",
657 sizeof(config_path_base));
658 join_path(data_path_base, xdg_data_base, "/telescope",
659 sizeof(data_path_base));
660 join_path(cache_path_base, xdg_cache_base, "/telescope",
661 sizeof(cache_path_base));
663 mkdirs(xdg_config_base, S_IRWXU);
664 mkdirs(xdg_data_base, S_IRWXU);
665 mkdirs(xdg_cache_base, S_IRWXU);
667 mkdirs(config_path_base, S_IRWXU);
668 mkdirs(data_path_base, S_IRWXU);
669 mkdirs(cache_path_base, S_IRWXU);
672 int
673 fs_init(void)
675 init_paths();
677 join_path(ctlsock_path, cache_path_base, "/ctl",
678 sizeof(ctlsock_path));
679 join_path(config_path, config_path_base, "/config",
680 sizeof(config_path));
681 join_path(lockfile_path, cache_path_base, "/lock",
682 sizeof(lockfile_path));
683 join_path(bookmark_file, data_path_base, "/bookmarks.gmi",
684 sizeof(bookmark_file));
685 join_path(known_hosts_file, data_path_base, "/known_hosts",
686 sizeof(known_hosts_file));
687 join_path(known_hosts_tmp, cache_path_base,
688 "/known_hosts.tmp.XXXXXXXXXX", sizeof(known_hosts_tmp));
689 join_path(session_file, cache_path_base, "/session",
690 sizeof(session_file));
691 join_path(crashed_file, cache_path_base, "/crashed",
692 sizeof(crashed_file));
694 return 1;
697 /*
698 * Parse a line of the session file. The format is:
700 * URL [flags,...] [title]\n
701 */
702 static void
703 parse_session_line(char *line)
705 struct session_tab tab;
706 char *s, *t, *ap;
708 memset(&tab, 0, sizeof(tab));
710 if ((s = strchr(line, ' ')) == NULL)
711 return;
713 *s++ = '\0';
715 if (strlcpy(tab.uri, line, sizeof(tab.uri)) >= sizeof(tab.uri))
716 return;
718 if ((t = strchr(s, ' ')) != NULL) {
719 *t++ = '\0';
721 /* don't worry about cached title truncation */
722 strlcpy(tab.title, t, sizeof(tab.title));
725 while ((ap = strsep(&s, ",")) != NULL) {
726 if (!strcmp(ap, "current"))
727 tab.flags |= TAB_CURRENT;
728 else if (!strcmp(ap, "killed"))
729 tab.flags |= TAB_KILLED;
730 else if (has_prefix(ap, "top="))
731 tab.top_line = strtonum(ap+4, 0, UINT32_MAX, NULL);
732 else if (has_prefix(ap, "cur="))
733 tab.current_line = strtonum(ap+4, 0, UINT32_MAX, NULL);
736 if (tab.top_line > tab.current_line) {
737 tab.top_line = 0;
738 tab.current_line = 0;
741 fs_send_ui(IMSG_SESSION_TAB, 0, -1, &tab, sizeof(tab));
744 static inline void
745 sendhist(const char *uri, int future)
747 struct session_tab_hist sth;
749 memset(&sth, 0, sizeof(sth));
750 sth.future = future;
752 if (strlcpy(sth.uri, uri, sizeof(sth.uri)) >= sizeof(sth.uri))
753 return;
755 fs_send_ui(IMSG_SESSION_TAB_HIST, 0, -1, &sth, sizeof(sth));
758 static void
759 load_last_session(void)
761 FILE *session;
762 size_t linesize = 0;
763 ssize_t linelen;
764 int first_time = 0;
765 int future;
766 char *nl, *s, *line = NULL;
768 if ((session = fopen(session_file, "r")) == NULL) {
769 /* first time? */
770 first_time = 1;
771 goto end;
774 while ((linelen = getline(&line, &linesize, session)) != -1) {
775 if ((nl = strchr(line, '\n')) != NULL)
776 *nl = '\0';
778 if (*line == '<' || *line == '>') {
779 future = *line == '>';
780 s = line+1;
781 if (*s != ' ')
782 continue;
783 sendhist(++s, future);
784 } else {
785 parse_session_line(line);
789 fclose(session);
790 free(line);
792 if (last_time_crashed()) {
793 struct session_tab tab;
794 memset(&tab, 0, sizeof(tab));
795 tab.flags = TAB_CURRENT;
796 strlcpy(tab.uri, "about:crash", sizeof(tab.uri));
797 fs_send_ui(IMSG_SESSION_TAB, 0, -1, &tab, sizeof(tab));
800 end:
801 fs_send_ui(IMSG_SESSION_END, 0, -1, &first_time, sizeof(first_time));
804 int
805 fs_main(void)
807 setproctitle("fs");
809 fs_init();
811 event_init();
813 /* Setup pipe and event handler to the main process */
814 if ((iev_ui = malloc(sizeof(*iev_ui))) == NULL)
815 die();
816 imsg_init(&iev_ui->ibuf, 3);
817 iev_ui->handler = handle_dispatch_imsg;
818 iev_ui->events = EV_READ;
819 event_set(&iev_ui->ev, iev_ui->ibuf.fd, iev_ui->events,
820 iev_ui->handler, iev_ui);
821 event_add(&iev_ui->ev, NULL);
823 sandbox_fs_process();
825 event_dispatch();
826 return 0;
829 /*
830 * Check if the last time telescope crashed. The check is done by
831 * looking at `crashed_file': if it exists then last time we crashed.
832 * Then, while here, touch the file too. During IMSG_QUIT we'll
833 * remove it.
834 */
835 static int
836 last_time_crashed(void)
838 int fd, crashed = 1;
840 if (safe_mode)
841 return 0;
843 if (unlink(crashed_file) == -1 && errno == ENOENT)
844 crashed = 0;
846 if ((fd = open(crashed_file, O_CREAT|O_WRONLY, 0600)) == -1)
847 return crashed;
848 close(fd);
850 return crashed;
853 int
854 lock_session(void)
856 struct flock lock;
857 int fd;
859 if ((fd = open(lockfile_path, O_WRONLY|O_CREAT, 0600)) == -1)
860 return -1;
862 lock.l_start = 0;
863 lock.l_len = 0;
864 lock.l_type = F_WRLCK;
865 lock.l_whence = SEEK_SET;
867 if (fcntl(fd, F_SETLK, &lock) == -1) {
868 close(fd);
869 return -1;
872 return fd;
875 static inline int
876 parse_khost_line(char *line, char *tmp[3])
878 char **ap;
880 for (ap = tmp; ap < &tmp[3] &&
881 (*ap = strsep(&line, " \t\n")) != NULL;) {
882 if (**ap != '\0')
883 ap++;
886 return ap == &tmp[3] && *line == '\0';
889 static void
890 load_certs(void)
892 char *tmp[3], *line = NULL;
893 const char *errstr;
894 size_t lineno = 0, linesize = 0;
895 ssize_t linelen;
896 FILE *f;
897 struct tofu_entry e;
899 if ((f = fopen(known_hosts_file, "r")) == NULL)
900 return;
902 while ((linelen = getline(&line, &linesize, f)) != -1) {
903 lineno++;
905 memset(&e, 0, sizeof(e));
906 if (parse_khost_line(line, tmp)) {
907 strlcpy(e.domain, tmp[0], sizeof(e.domain));
908 strlcpy(e.hash, tmp[1], sizeof(e.hash));
910 e.verified = strtonum(tmp[2], 0, 1, &errstr);
911 if (errstr != NULL)
912 errx(1, "%s:%zu verification for %s is %s: %s",
913 known_hosts_file, lineno,
914 e.domain, errstr, tmp[2]);
916 fs_send_ui(IMSG_TOFU, 0, -1, &e, sizeof(e));
917 } else {
918 warnx("%s:%zu invalid entry",
919 known_hosts_file, lineno);
923 free(line);
924 fclose(f);
925 return;