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 config_path[PATH_MAX];
79 char lockfile_path[PATH_MAX];
80 char bookmark_file[PATH_MAX];
81 char known_hosts_file[PATH_MAX], known_hosts_tmp[PATH_MAX];
82 char crashed_file[PATH_MAX];
83 char session_file[PATH_MAX];
85 static imsg_handlerfn *handlers[] = {
86 [IMSG_GET] = handle_get,
87 [IMSG_GET_FILE] = handle_get_file,
88 [IMSG_QUIT] = handle_misc,
89 [IMSG_INIT] = handle_misc,
90 [IMSG_BOOKMARK_PAGE] = handle_bookmark_page,
91 [IMSG_SAVE_CERT] = handle_save_cert,
92 [IMSG_UPDATE_CERT] = handle_update_cert,
93 [IMSG_FILE_OPEN] = handle_file_open,
94 [IMSG_SESSION_START] = handle_session_start,
95 [IMSG_SESSION_TAB] = handle_session_tab,
96 [IMSG_SESSION_TAB_HIST] = handle_session_tab_hist,
97 [IMSG_SESSION_END] = handle_session_end,
98 };
100 static void __attribute__((__noreturn__))
101 die(void)
103 abort(); /* TODO */
106 static void
107 send_file(uint32_t peerid, FILE *f)
109 ssize_t r;
110 char buf[BUFSIZ];
112 for (;;) {
113 r = fread(buf, 1, sizeof(buf), f);
114 if (r != 0)
115 fs_send_ui(IMSG_BUF, peerid, -1, buf, r);
116 if (r != sizeof(buf))
117 break;
119 fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
120 fclose(f);
123 static void
124 handle_get(struct imsg *imsg, size_t datalen)
126 const char *bpath = "bookmarks.gmi";
127 char path[PATH_MAX];
128 FILE *f;
129 const char *data, *p;
130 size_t i;
131 struct page {
132 const char *name;
133 const char *path;
134 const uint8_t *data;
135 size_t len;
136 } pages[] = {
137 {"about", NULL, about_about, about_about_len},
138 {"blank", NULL, about_blank, about_blank_len},
139 {"bookmarks", bpath, bookmarks, bookmarks_len},
140 {"crash", NULL, about_crash, about_crash_len},
141 {"help", NULL, about_help, about_help_len},
142 {"license", NULL, about_license, about_license_len},
143 {"new", NULL, about_new, about_new_len},
144 }, *page = NULL;
146 data = imsg->data;
147 if (data[datalen-1] != '\0') /* make sure it's NUL-terminated */
148 die();
149 if ((data = strchr(data, ':')) == NULL)
150 goto notfound;
151 data++;
153 for (i = 0; i < sizeof(pages)/sizeof(pages[0]); ++i)
154 if (!strcmp(data, pages[i].name)) {
155 page = &pages[i];
156 break;
159 if (page == NULL)
160 goto notfound;
162 strlcpy(path, data_path_base, sizeof(path));
163 strlcat(path, "/", sizeof(path));
164 if (page->path != NULL)
165 strlcat(path, page->path, sizeof(path));
166 else {
167 strlcat(path, "pages/about_", sizeof(path));
168 strlcat(path, page->name, sizeof(path));
169 strlcat(path, ".gmi", sizeof(path));
172 if ((f = fopen(path, "r")) == NULL) {
173 fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1,
174 page->data, page->len);
175 fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1,
176 NULL, 0);
177 return;
180 send_file(imsg->hdr.peerid, f);
181 return;
183 notfound:
184 p = "# not found!\n";
185 fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, p, strlen(p));
186 fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
189 static inline void
190 send_hdr(uint32_t peerid, int code, const char *meta)
192 fs_send_ui(IMSG_GOT_CODE, peerid, -1, &code, sizeof(code));
193 fs_send_ui(IMSG_GOT_META, peerid, -1, meta, strlen(meta)+1);
196 static inline void
197 send_errno(uint32_t peerid, int code, const char *str, int no)
199 char *s;
201 if (asprintf(&s, "%s: %s", str, strerror(no)) == -1)
202 s = NULL;
204 send_hdr(peerid, code, s == NULL ? str : s);
205 free(s);
208 static inline const char *
209 file_type(const char *path)
211 struct mapping {
212 const char *ext;
213 const char *mime;
214 } ms[] = {
215 {"diff", "text/x-patch"},
216 {"gemini", "text/gemini"},
217 {"gmi", "text/gemini"},
218 {"markdown", "text/plain"},
219 {"md", "text/plain"},
220 {"patch", "text/x-patch"},
221 {"txt", "text/plain"},
222 {NULL, NULL},
223 }, *m;
224 char *dot;
226 if ((dot = strrchr(path, '.')) == NULL)
227 return NULL;
229 dot++;
231 for (m = ms; m->ext != NULL; ++m)
232 if (!strcmp(m->ext, dot))
233 return m->mime;
235 return NULL;
238 static int
239 select_non_dot(const struct dirent *d)
241 return strcmp(d->d_name, ".");
244 static int
245 select_non_dotdot(const struct dirent *d)
247 return strcmp(d->d_name, ".") && strcmp(d->d_name, "..");
250 static inline void
251 send_dir(uint32_t peerid, const char *path)
253 struct dirent **names;
254 struct evbuffer *ev;
255 char *s;
256 int (*selector)(const struct dirent *) = select_non_dot;
257 int i, len, no;
259 if (!has_suffix(path, "/")) {
260 if (asprintf(&s, "%s/", path) == -1)
261 die();
262 send_hdr(peerid, 30, s);
263 free(s);
264 return;
267 if (!strcmp(path, "/"))
268 selector = select_non_dotdot;
270 if ((ev = evbuffer_new()) == NULL ||
271 (len = scandir(path, &names, selector, alphasort)) == -1) {
272 no = errno;
273 evbuffer_free(ev);
274 send_errno(peerid, 40, "failure reading the directory", no);
275 return;
278 evbuffer_add_printf(ev, "# Index of %s\n\n", path);
279 for (i = 0; i < len; ++i) {
280 evbuffer_add_printf(ev, "=> %s", names[i]->d_name);
281 if (names[i]->d_type == DT_DIR)
282 evbuffer_add(ev, "/", 1);
283 evbuffer_add(ev, "\n", 1);
286 send_hdr(peerid, 20, "text/gemini");
287 fs_send_ui(IMSG_BUF, peerid, -1,
288 EVBUFFER_DATA(ev), EVBUFFER_LENGTH(ev));
289 fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
291 evbuffer_free(ev);
292 free(names);
295 static void
296 handle_get_file(struct imsg *imsg, size_t datalen)
298 struct stat sb;
299 FILE *f;
300 char *data;
301 const char *meta = NULL;
303 data = imsg->data;
304 data[datalen-1] = '\0';
306 if ((f = fopen(data, "r")) == NULL) {
307 send_errno(imsg->hdr.peerid, 51, "can't open", errno);
308 return;
311 if (fstat(fileno(f), &sb) == -1) {
312 send_errno(imsg->hdr.peerid, 40, "fstat", errno);
313 return;
316 if (S_ISDIR(sb.st_mode)) {
317 fclose(f);
318 send_dir(imsg->hdr.peerid, data);
319 return;
322 if ((meta = file_type(data)) == NULL) {
323 fclose(f);
324 send_hdr(imsg->hdr.peerid, 51,
325 "don't know how to visualize this file");
326 return;
329 send_hdr(imsg->hdr.peerid, 20, meta);
330 send_file(imsg->hdr.peerid, f);
333 static void
334 handle_misc(struct imsg *imsg, size_t datalen)
336 switch (imsg->hdr.type) {
337 case IMSG_INIT:
338 load_certs();
339 load_last_session();
340 break;
342 case IMSG_QUIT:
343 if (!safe_mode)
344 unlink(crashed_file);
345 event_loopbreak();
346 break;
348 default:
349 die();
353 static void
354 handle_bookmark_page(struct imsg *imsg, size_t datalen)
356 char *data;
357 int res;
358 FILE *f;
360 data = imsg->data;
361 if (data[datalen-1] != '\0')
362 die();
364 if ((f = fopen(bookmark_file, "a")) == NULL) {
365 res = errno;
366 goto end;
368 fprintf(f, "=> %s\n", data);
369 fclose(f);
371 res = 0;
372 end:
373 fs_send_ui(IMSG_BOOKMARK_OK, 0, -1, &res, sizeof(res));
376 static void
377 handle_save_cert(struct imsg *imsg, size_t datalen)
379 struct tofu_entry e;
380 FILE *f;
381 int res;
383 /* TODO: traverse the file to avoid duplications? */
385 if (datalen != sizeof(e))
386 die();
387 memcpy(&e, imsg->data, datalen);
389 if ((f = fopen(known_hosts_file, "a")) == NULL) {
390 res = errno;
391 goto end;
393 fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
394 fclose(f);
396 res = 0;
397 end:
398 fs_send_ui(IMSG_SAVE_CERT_OK, imsg->hdr.peerid, -1,
399 &res, sizeof(res));
402 static void
403 handle_update_cert(struct imsg *imsg, size_t datalen)
405 FILE *tmp, *f;
406 struct tofu_entry entry;
407 char sfn[PATH_MAX], *line = NULL, *t;
408 size_t l, linesize = 0;
409 ssize_t linelen;
410 int fd, e, res = 0;
412 if (datalen != sizeof(entry))
413 die();
414 memcpy(&entry, imsg->data, datalen);
416 strlcpy(sfn, known_hosts_tmp, sizeof(sfn));
417 if ((fd = mkstemp(sfn)) == -1 ||
418 (tmp = fdopen(fd, "w")) == NULL) {
419 if (fd != -1) {
420 unlink(sfn);
421 close(fd);
423 res = 0;
424 goto end;
427 if ((f = fopen(known_hosts_file, "r")) == NULL) {
428 unlink(sfn);
429 fclose(tmp);
430 res = 0;
431 goto end;
434 l = strlen(entry.domain);
435 while ((linelen = getline(&line, &linesize, f)) != -1) {
436 if ((t = strstr(line, entry.domain)) != NULL &&
437 (line[l] == ' ' || line[l] == '\t'))
438 continue;
439 /* line has a trailing \n */
440 fprintf(tmp, "%s", line);
442 fprintf(tmp, "%s %s %d\n", entry.domain, entry.hash, entry.verified);
444 free(line);
445 e = ferror(tmp);
447 fclose(tmp);
448 fclose(f);
450 if (e) {
451 unlink(sfn);
452 res = 0;
453 goto end;
456 res = rename(sfn, known_hosts_file) != -1;
458 end:
459 fs_send_ui(IMSG_UPDATE_CERT_OK, imsg->hdr.peerid, -1,
460 &res, sizeof(res));
463 static void
464 handle_file_open(struct imsg *imsg, size_t datalen)
466 char *path, *e;
467 int fd;
469 path = imsg->data;
470 if (path[datalen-1] != '\0')
471 die();
473 if ((fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644)) == -1) {
474 e = strerror(errno);
475 fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, -1,
476 e, strlen(e)+1);
477 } else
478 fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, fd,
479 NULL, 0);
482 static void
483 handle_session_start(struct imsg *imsg, size_t datalen)
485 if (datalen != 0)
486 die();
488 if ((session = fopen(session_file, "w")) == NULL)
489 die();
492 static void
493 handle_session_tab(struct imsg *imsg, size_t datalen)
495 struct session_tab tab;
497 if (session == NULL)
498 die();
500 if (datalen != sizeof(tab))
501 die();
503 memcpy(&tab, imsg->data, sizeof(tab));
504 if (tab.uri[sizeof(tab.uri)-1] != '\0' ||
505 tab.title[sizeof(tab.title)-1] != '\0')
506 die();
508 fprintf(session, "%s", tab.uri);
510 if (tab.flags == 0)
511 fprintf(session, " - ");
512 else {
513 fprintf(session, " ");
514 if (tab.flags & TAB_CURRENT)
515 fprintf(session, "current,");
516 if (tab.flags & TAB_KILLED)
517 fprintf(session, "killed,");
518 fprintf(session, " ");
521 fprintf(session, "%s\n", tab.title);
524 static void
525 handle_session_tab_hist(struct imsg *imsg, size_t datalen)
527 struct session_tab_hist th;
529 if (session == NULL)
530 die();
532 if (datalen != sizeof(th))
533 die();
535 memcpy(&th, imsg->data, sizeof(th));
536 if (th.uri[sizeof(th.uri)-1] != '\0')
537 die();
539 fprintf(session, "%s %s\n", th.future ? ">" : "<", th.uri);
542 static void
543 handle_session_end(struct imsg *imsg, size_t datalen)
545 if (session == NULL)
546 die();
547 fclose(session);
548 session = NULL;
551 static void
552 handle_dispatch_imsg(int fd, short ev, void *d)
554 struct imsgev *iev = d;
555 int e;
557 if (dispatch_imsg(iev, ev, handlers, sizeof(handlers)) == -1) {
558 /*
559 * This should leave a ~/.cache/telescope/crashed file to
560 * trigger about:crash on next run. Unfortunately, if
561 * the main process dies the fs sticks around and
562 * doesn't notice that the fd was closed. Why EV_READ
563 * is not triggered when a fd is closed on the other end?
564 */
565 e = errno;
566 if ((fd = open(crashed_file, O_CREAT|O_TRUNC|O_WRONLY, 0600))
567 == -1)
568 err(1, "open");
569 close(fd);
570 errx(1, "connection closed: %s", strerror(e));
574 static int
575 fs_send_ui(int type, uint32_t peerid, int fd, const void *data,
576 uint16_t datalen)
578 return imsg_compose_event(iev_ui, type, peerid, 0, fd,
579 data, datalen);
582 static size_t
583 join_path(char *buf, const char *lhs, const char *rhs, size_t buflen)
585 strlcpy(buf, lhs, buflen);
586 return strlcat(buf, rhs, buflen);
589 static void
590 getenv_default(char *buf, const char *name, const char *def, size_t buflen)
592 size_t ret;
593 char *home, *env;
595 if ((home = getenv("HOME")) == NULL)
596 errx(1, "HOME is not defined");
598 if ((env = getenv(name)) != NULL)
599 ret = strlcpy(buf, env, buflen);
600 else
601 ret = join_path(buf, home, def, buflen);
603 if (ret >= buflen)
604 errx(1, "buffer too small for %s", name);
607 static void
608 mkdirs(const char *path, mode_t mode)
610 char copy[PATH_MAX+1], orig[PATH_MAX+1], *parent;
612 strlcpy(copy, path, sizeof(copy));
613 strlcpy(orig, path, sizeof(orig));
614 parent = dirname(copy);
615 if (!strcmp(parent, "/"))
616 return;
617 mkdirs(parent, mode);
619 if (mkdir(orig, mode) != 0) {
620 if (errno == EEXIST)
621 return;
622 err(1, "can't mkdir %s", orig);
626 static void
627 init_paths(void)
629 char xdg_config_base[PATH_MAX];
630 char xdg_data_base[PATH_MAX];
631 char xdg_cache_base[PATH_MAX];
632 char old_path[PATH_MAX];
633 char *home;
634 struct stat info;
636 /* old path */
637 if ((home = getenv("HOME")) == NULL)
638 errx(1, "HOME is not defined");
639 join_path(old_path, home, "/.telescope", sizeof(old_path));
641 /* if ~/.telescope exists, use that instead of xdg dirs */
642 if (stat(old_path, &info) == 0 && S_ISDIR(info.st_mode)) {
643 join_path(config_path_base, home, "/.telescope",
644 sizeof(config_path_base));
645 join_path(data_path_base, home, "/.telescope",
646 sizeof(data_path_base));
647 join_path(cache_path_base, home, "/.telescope",
648 sizeof(cache_path_base));
649 return;
652 /* xdg paths */
653 getenv_default(xdg_config_base, "XDG_CONFIG_HOME", "/.config",
654 sizeof(xdg_config_base));
655 getenv_default(xdg_data_base, "XDG_DATA_HOME", "/.local/share",
656 sizeof(xdg_data_base));
657 getenv_default(xdg_cache_base, "XDG_CACHE_HOME", "/.cache",
658 sizeof(xdg_cache_base));
660 join_path(config_path_base, xdg_config_base, "/telescope",
661 sizeof(config_path_base));
662 join_path(data_path_base, xdg_data_base, "/telescope",
663 sizeof(data_path_base));
664 join_path(cache_path_base, xdg_cache_base, "/telescope",
665 sizeof(cache_path_base));
667 mkdirs(xdg_config_base, S_IRWXU);
668 mkdirs(xdg_data_base, S_IRWXU);
669 mkdirs(xdg_cache_base, S_IRWXU);
671 mkdirs(config_path_base, S_IRWXU);
672 mkdirs(data_path_base, S_IRWXU);
673 mkdirs(cache_path_base, S_IRWXU);
676 int
677 fs_init(void)
679 init_paths();
681 join_path(config_path, config_path_base, "/config",
682 sizeof(config_path));
683 join_path(lockfile_path, cache_path_base, "/lock",
684 sizeof(lockfile_path));
685 join_path(bookmark_file, data_path_base, "/bookmarks.gmi",
686 sizeof(bookmark_file));
687 join_path(known_hosts_file, data_path_base, "/known_hosts",
688 sizeof(known_hosts_file));
689 join_path(known_hosts_tmp, cache_path_base,
690 "/known_hosts.tmp.XXXXXXXXXX", sizeof(known_hosts_tmp));
691 join_path(session_file, cache_path_base, "/session",
692 sizeof(session_file));
693 join_path(crashed_file, cache_path_base, "/crashed",
694 sizeof(crashed_file));
696 return 1;
699 /*
700 * Parse a line of the session file. The format is:
702 * URL [flags,...] [title]\n
703 */
704 static void
705 parse_session_line(char *line, const char **title, uint32_t *flags)
707 char *s, *t, *ap;
709 *title = "";
710 *flags = 0;
711 if ((s = strchr(line, ' ')) == NULL)
712 return;
714 *s++ = '\0';
716 if ((t = strchr(s, ' ')) != NULL) {
717 *t++ = '\0';
718 *title = t;
721 while ((ap = strsep(&s, ",")) != NULL) {
722 if (!strcmp(ap, "current"))
723 *flags |= TAB_CURRENT;
724 else if (!strcmp(ap, "killed"))
725 *flags |= TAB_KILLED;
729 static inline void
730 sendtab(uint32_t flags, const char *uri, const char *title)
732 struct session_tab tab;
734 memset(&tab, 0, sizeof(tab));
735 tab.flags = flags;
737 if (strlcpy(tab.uri, uri, sizeof(tab.uri)) >= sizeof(tab.uri))
738 return;
740 /* don't worry about cached title truncation */
741 if (title != NULL)
742 strlcpy(tab.title, title, sizeof(tab.title));
744 fs_send_ui(IMSG_SESSION_TAB, 0, -1, &tab, sizeof(tab));
747 static inline void
748 sendhist(const char *uri, int future)
750 struct session_tab_hist sth;
752 memset(&sth, 0, sizeof(sth));
753 sth.future = future;
755 if (strlcpy(sth.uri, uri, sizeof(sth.uri)) >= sizeof(sth.uri))
756 return;
758 fs_send_ui(IMSG_SESSION_TAB_HIST, 0, -1, &sth, sizeof(sth));
761 static void
762 load_last_session(void)
764 FILE *session;
765 uint32_t flags;
766 size_t linesize = 0;
767 ssize_t linelen;
768 int first_time = 0;
769 int future;
770 const char *title;
771 char *nl, *s, *line = NULL;
773 if ((session = fopen(session_file, "r")) == NULL) {
774 /* first time? */
775 first_time = 1;
776 goto end;
779 while ((linelen = getline(&line, &linesize, session)) != -1) {
780 if ((nl = strchr(line, '\n')) != NULL)
781 *nl = '\0';
783 if (*line == '<' || *line == '>') {
784 future = *line == '>';
785 s = line+1;
786 if (*s != ' ')
787 continue;
788 sendhist(++s, future);
789 } else {
790 parse_session_line(line, &title, &flags);
791 sendtab(flags, line, title);
795 fclose(session);
796 free(line);
798 if (last_time_crashed())
799 sendtab(TAB_CURRENT, "about:crash", NULL);
801 end:
802 fs_send_ui(IMSG_SESSION_END, 0, -1, &first_time, sizeof(first_time));
805 int
806 fs_main(void)
808 setproctitle("fs");
810 fs_init();
812 event_init();
814 /* Setup pipe and event handler to the main process */
815 if ((iev_ui = malloc(sizeof(*iev_ui))) == NULL)
816 die();
817 imsg_init(&iev_ui->ibuf, 3);
818 iev_ui->handler = handle_dispatch_imsg;
819 iev_ui->events = EV_READ;
820 event_set(&iev_ui->ev, iev_ui->ibuf.fd, iev_ui->events,
821 iev_ui->handler, iev_ui);
822 event_add(&iev_ui->ev, NULL);
824 sandbox_fs_process();
826 event_dispatch();
827 return 0;
830 /*
831 * Check if the last time telescope crashed. The check is done by
832 * looking at `crashed_file': if it exists then last time we crashed.
833 * Then, while here, touch the file too. During IMSG_QUIT we'll
834 * remove it.
835 */
836 static int
837 last_time_crashed(void)
839 int fd, crashed = 1;
841 if (safe_mode)
842 return 0;
844 if (unlink(crashed_file) == -1 && errno == ENOENT)
845 crashed = 0;
847 if ((fd = open(crashed_file, O_CREAT|O_WRONLY, 0600)) == -1)
848 return crashed;
849 close(fd);
851 return crashed;
854 int
855 lock_session(void)
857 struct flock lock;
858 int fd;
860 if ((fd = open(lockfile_path, O_WRONLY|O_CREAT, 0600)) == -1)
861 return -1;
863 lock.l_start = 0;
864 lock.l_len = 0;
865 lock.l_type = F_WRLCK;
866 lock.l_whence = SEEK_SET;
868 if (fcntl(fd, F_SETLK, &lock) == -1) {
869 close(fd);
870 return -1;
873 return fd;
876 static inline int
877 parse_khost_line(char *line, char *tmp[3])
879 char **ap;
881 for (ap = tmp; ap < &tmp[3] &&
882 (*ap = strsep(&line, " \t\n")) != NULL;) {
883 if (**ap != '\0')
884 ap++;
887 return ap == &tmp[3] && *line == '\0';
890 static void
891 load_certs(void)
893 char *tmp[3], *line = NULL;
894 const char *errstr;
895 size_t lineno = 0, linesize = 0;
896 ssize_t linelen;
897 FILE *f;
898 struct tofu_entry e;
900 if ((f = fopen(known_hosts_file, "r")) == NULL)
901 return;
903 while ((linelen = getline(&line, &linesize, f)) != -1) {
904 lineno++;
906 memset(&e, 0, sizeof(e));
907 if (parse_khost_line(line, tmp)) {
908 strlcpy(e.domain, tmp[0], sizeof(e.domain));
909 strlcpy(e.hash, tmp[1], sizeof(e.hash));
911 e.verified = strtonum(tmp[2], 0, 1, &errstr);
912 if (errstr != NULL)
913 errx(1, "%s:%zu verification for %s is %s: %s",
914 known_hosts_file, lineno,
915 e.domain, errstr, tmp[2]);
917 fs_send_ui(IMSG_TOFU, 0, -1, &e, sizeof(e));
918 } else {
919 warnx("%s:%zu invalid entry",
920 known_hosts_file, lineno);
924 free(line);
925 fclose(f);
926 return;