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"
41 static void die(void) __attribute__((__noreturn__));
42 static void send_file(uint32_t, FILE *);
43 static void handle_get(struct imsg*, size_t);
44 static int select_non_dot(const struct dirent *);
45 static int select_non_dotdot(const struct dirent *);
46 static void handle_get_file(struct imsg*, size_t);
47 static void handle_misc(struct imsg *, size_t);
48 static void handle_bookmark_page(struct imsg*, size_t);
49 static void handle_save_cert(struct imsg*, size_t);
50 static void handle_update_cert(struct imsg*, size_t);
51 static void handle_file_open(struct imsg*, size_t);
52 static void handle_session_start(struct imsg*, size_t);
53 static void handle_session_tab(struct imsg*, size_t);
54 static void handle_session_tab_hist(struct imsg*, size_t);
55 static void handle_session_end(struct imsg*, size_t);
56 static void handle_dispatch_imsg(int, short, void*);
57 static int fs_send_ui(int, uint32_t, int, const void *, uint16_t);
58 static size_t join_path(char*, const char*, const char*, size_t);
59 static void getenv_default(char*, const char*, const char*, size_t);
60 static void mkdirs(const char*, mode_t);
61 static void init_paths(void);
62 static void load_last_session(void);
63 static int last_time_crashed(void);
64 static void load_certs(void);
66 static struct imsgev *iev_ui;
67 static FILE *session;
69 /*
70 * Where to store user data. These are all equal to ~/.telescope if
71 * it exists.
72 */
73 char config_path_base[PATH_MAX];
74 char data_path_base[PATH_MAX];
75 char cache_path_base[PATH_MAX];
77 char config_path[PATH_MAX];
78 char lockfile_path[PATH_MAX];
79 char bookmark_file[PATH_MAX];
80 char known_hosts_file[PATH_MAX], known_hosts_tmp[PATH_MAX];
81 char crashed_file[PATH_MAX];
82 char session_file[PATH_MAX];
84 static imsg_handlerfn *handlers[] = {
85 [IMSG_GET] = handle_get,
86 [IMSG_GET_FILE] = handle_get_file,
87 [IMSG_QUIT] = handle_misc,
88 [IMSG_INIT] = handle_misc,
89 [IMSG_BOOKMARK_PAGE] = handle_bookmark_page,
90 [IMSG_SAVE_CERT] = handle_save_cert,
91 [IMSG_UPDATE_CERT] = handle_update_cert,
92 [IMSG_FILE_OPEN] = handle_file_open,
93 [IMSG_SESSION_START] = handle_session_start,
94 [IMSG_SESSION_TAB] = handle_session_tab,
95 [IMSG_SESSION_TAB_HIST] = handle_session_tab_hist,
96 [IMSG_SESSION_END] = handle_session_end,
97 };
99 static void __attribute__((__noreturn__))
100 die(void)
102 abort(); /* TODO */
105 static void
106 send_file(uint32_t peerid, FILE *f)
108 ssize_t r;
109 char buf[BUFSIZ];
111 for (;;) {
112 r = fread(buf, 1, sizeof(buf), f);
113 if (r != 0)
114 fs_send_ui(IMSG_BUF, peerid, -1, buf, r);
115 if (r != sizeof(buf))
116 break;
118 fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
119 fclose(f);
122 static void
123 handle_get(struct imsg *imsg, size_t datalen)
125 const char *bpath = "bookmarks.gmi";
126 char path[PATH_MAX];
127 FILE *f;
128 const char *data, *p;
129 size_t i;
130 struct page {
131 const char *name;
132 const char *path;
133 const uint8_t *data;
134 size_t len;
135 } pages[] = {
136 {"about", NULL, about_about, about_about_len},
137 {"blank", NULL, about_blank, about_blank_len},
138 {"bookmarks", bpath, bookmarks, bookmarks_len},
139 {"crash", NULL, about_crash, about_crash_len},
140 {"help", NULL, about_help, about_help_len},
141 {"license", NULL, about_license, about_license_len},
142 {"new", NULL, about_new, about_new_len},
143 }, *page = NULL;
145 data = imsg->data;
146 if (data[datalen-1] != '\0') /* make sure it's NUL-terminated */
147 die();
148 if ((data = strchr(data, ':')) == NULL)
149 goto notfound;
150 data++;
152 for (i = 0; i < sizeof(pages)/sizeof(pages[0]); ++i)
153 if (!strcmp(data, pages[i].name)) {
154 page = &pages[i];
155 break;
158 if (page == NULL)
159 goto notfound;
161 strlcpy(path, data_path_base, sizeof(path));
162 strlcat(path, "/", sizeof(path));
163 if (page->path != NULL)
164 strlcat(path, page->path, sizeof(path));
165 else {
166 strlcat(path, "pages/about_", sizeof(path));
167 strlcat(path, page->name, sizeof(path));
168 strlcat(path, ".gmi", sizeof(path));
171 if ((f = fopen(path, "r")) == NULL) {
172 fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1,
173 page->data, page->len);
174 fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1,
175 NULL, 0);
176 return;
179 send_file(imsg->hdr.peerid, f);
180 return;
182 notfound:
183 p = "# not found!\n";
184 fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, p, strlen(p));
185 fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
188 static inline void
189 send_hdr(uint32_t peerid, int code, const char *meta)
191 fs_send_ui(IMSG_GOT_CODE, peerid, -1, &code, sizeof(code));
192 fs_send_ui(IMSG_GOT_META, peerid, -1, meta, strlen(meta)+1);
195 static inline void
196 send_errno(uint32_t peerid, int code, const char *str, int no)
198 char *s;
200 if (asprintf(&s, "%s: %s", str, strerror(no)) == -1)
201 s = NULL;
203 send_hdr(peerid, code, s == NULL ? str : s);
204 free(s);
207 static inline const char *
208 file_type(const char *path)
210 struct mapping {
211 const char *ext;
212 const char *mime;
213 } ms[] = {
214 {"diff", "text/x-patch"},
215 {"gemini", "text/gemini"},
216 {"gmi", "text/gemini"},
217 {"markdown", "text/plain"},
218 {"md", "text/plain"},
219 {"patch", "text/x-patch"},
220 {"txt", "text/plain"},
221 {NULL, NULL},
222 }, *m;
223 char *dot;
225 if ((dot = strrchr(path, '.')) == NULL)
226 return NULL;
228 dot++;
230 for (m = ms; m->ext != NULL; ++m)
231 if (!strcmp(m->ext, dot))
232 return m->mime;
234 return NULL;
237 static int
238 select_non_dot(const struct dirent *d)
240 return strcmp(d->d_name, ".");
243 static int
244 select_non_dotdot(const struct dirent *d)
246 return strcmp(d->d_name, ".") && strcmp(d->d_name, "..");
249 static inline void
250 send_dir(uint32_t peerid, const char *path)
252 struct dirent **names;
253 struct evbuffer *ev;
254 char *s;
255 int (*selector)(const struct dirent *) = select_non_dot;
256 int i, len, no;
258 if (!has_suffix(path, "/")) {
259 if (asprintf(&s, "%s/", path) == -1)
260 die();
261 send_hdr(peerid, 30, s);
262 free(s);
263 return;
266 if (!strcmp(path, "/"))
267 selector = select_non_dotdot;
269 if ((ev = evbuffer_new()) == NULL ||
270 (len = scandir(path, &names, selector, alphasort)) == -1) {
271 no = errno;
272 evbuffer_free(ev);
273 send_errno(peerid, 40, "failure reading the directory", no);
274 return;
277 evbuffer_add_printf(ev, "# Index of %s\n\n", path);
278 for (i = 0; i < len; ++i) {
279 evbuffer_add_printf(ev, "=> %s", names[i]->d_name);
280 if (names[i]->d_type == DT_DIR)
281 evbuffer_add(ev, "/", 1);
282 evbuffer_add(ev, "\n", 1);
285 send_hdr(peerid, 20, "text/gemini");
286 fs_send_ui(IMSG_BUF, peerid, -1,
287 EVBUFFER_DATA(ev), EVBUFFER_LENGTH(ev));
288 fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
290 evbuffer_free(ev);
291 free(names);
294 static void
295 handle_get_file(struct imsg *imsg, size_t datalen)
297 struct stat sb;
298 FILE *f;
299 char *data;
300 const char *meta = NULL;
302 data = imsg->data;
303 data[datalen-1] = '\0';
305 if ((f = fopen(data, "r")) == NULL) {
306 send_errno(imsg->hdr.peerid, 51, "can't open", errno);
307 return;
310 if (fstat(fileno(f), &sb) == -1) {
311 send_errno(imsg->hdr.peerid, 40, "fstat", errno);
312 return;
315 if (S_ISDIR(sb.st_mode)) {
316 fclose(f);
317 send_dir(imsg->hdr.peerid, data);
318 return;
321 if ((meta = file_type(data)) == NULL) {
322 fclose(f);
323 send_hdr(imsg->hdr.peerid, 51,
324 "don't know how to visualize this file");
325 return;
328 send_hdr(imsg->hdr.peerid, 20, meta);
329 send_file(imsg->hdr.peerid, f);
332 static void
333 handle_misc(struct imsg *imsg, size_t datalen)
335 switch (imsg->hdr.type) {
336 case IMSG_INIT:
337 load_certs();
338 load_last_session();
339 break;
341 case IMSG_QUIT:
342 if (!safe_mode)
343 unlink(crashed_file);
344 event_loopbreak();
345 break;
347 default:
348 die();
352 static void
353 handle_bookmark_page(struct imsg *imsg, size_t datalen)
355 char *data;
356 int res;
357 FILE *f;
359 data = imsg->data;
360 if (data[datalen-1] != '\0')
361 die();
363 if ((f = fopen(bookmark_file, "a")) == NULL) {
364 res = errno;
365 goto end;
367 fprintf(f, "=> %s\n", data);
368 fclose(f);
370 res = 0;
371 end:
372 fs_send_ui(IMSG_BOOKMARK_OK, 0, -1, &res, sizeof(res));
375 static void
376 handle_save_cert(struct imsg *imsg, size_t datalen)
378 struct tofu_entry e;
379 FILE *f;
380 int res;
382 /* TODO: traverse the file to avoid duplications? */
384 if (datalen != sizeof(e))
385 die();
386 memcpy(&e, imsg->data, datalen);
388 if ((f = fopen(known_hosts_file, "a")) == NULL) {
389 res = errno;
390 goto end;
392 fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
393 fclose(f);
395 res = 0;
396 end:
397 fs_send_ui(IMSG_SAVE_CERT_OK, imsg->hdr.peerid, -1,
398 &res, sizeof(res));
401 static void
402 handle_update_cert(struct imsg *imsg, size_t datalen)
404 FILE *tmp, *f;
405 struct tofu_entry entry;
406 char sfn[PATH_MAX], *line = NULL, *t;
407 size_t l, linesize = 0;
408 ssize_t linelen;
409 int fd, e, res = 0;
411 if (datalen != sizeof(entry))
412 die();
413 memcpy(&entry, imsg->data, datalen);
415 strlcpy(sfn, known_hosts_tmp, sizeof(sfn));
416 if ((fd = mkstemp(sfn)) == -1 ||
417 (tmp = fdopen(fd, "w")) == NULL) {
418 if (fd != -1) {
419 unlink(sfn);
420 close(fd);
422 res = 0;
423 goto end;
426 if ((f = fopen(known_hosts_file, "r")) == NULL) {
427 unlink(sfn);
428 fclose(tmp);
429 res = 0;
430 goto end;
433 l = strlen(entry.domain);
434 while ((linelen = getline(&line, &linesize, f)) != -1) {
435 if ((t = strstr(line, entry.domain)) != NULL &&
436 (line[l] == ' ' || line[l] == '\t'))
437 continue;
438 /* line has a trailing \n */
439 fprintf(tmp, "%s", line);
441 fprintf(tmp, "%s %s %d\n", entry.domain, entry.hash, entry.verified);
443 free(line);
444 e = ferror(tmp);
446 fclose(tmp);
447 fclose(f);
449 if (e) {
450 unlink(sfn);
451 res = 0;
452 goto end;
455 res = rename(sfn, known_hosts_file) != -1;
457 end:
458 fs_send_ui(IMSG_UPDATE_CERT_OK, imsg->hdr.peerid, -1,
459 &res, sizeof(res));
462 static void
463 handle_file_open(struct imsg *imsg, size_t datalen)
465 char *path, *e;
466 int fd;
468 path = imsg->data;
469 if (path[datalen-1] != '\0')
470 die();
472 if ((fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644)) == -1) {
473 e = strerror(errno);
474 fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, -1,
475 e, strlen(e)+1);
476 } else
477 fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, fd,
478 NULL, 0);
481 static void
482 handle_session_start(struct imsg *imsg, size_t datalen)
484 if (datalen != 0)
485 die();
487 if ((session = fopen(session_file, "w")) == NULL)
488 die();
491 static void
492 handle_session_tab(struct imsg *imsg, size_t datalen)
494 struct session_tab tab;
496 if (session == NULL)
497 die();
499 if (datalen != sizeof(tab))
500 die();
502 memcpy(&tab, imsg->data, sizeof(tab));
503 if (tab.uri[sizeof(tab.uri)-1] != '\0' ||
504 tab.title[sizeof(tab.title)-1] != '\0')
505 die();
507 fprintf(session, "%s", tab.uri);
509 if (tab.flags == 0)
510 fprintf(session, " - ");
511 else {
512 fprintf(session, " ");
513 if (tab.flags & TAB_CURRENT)
514 fprintf(session, "current,");
515 if (tab.flags & TAB_KILLED)
516 fprintf(session, "killed,");
517 fprintf(session, " ");
520 fprintf(session, "%s\n", tab.title);
523 static void
524 handle_session_tab_hist(struct imsg *imsg, size_t datalen)
526 struct session_tab_hist th;
528 if (session == NULL)
529 die();
531 if (datalen != sizeof(th))
532 die();
534 memcpy(&th, imsg->data, sizeof(th));
535 if (th.uri[sizeof(th.uri)-1] != '\0')
536 die();
538 fprintf(session, "%s %s\n", th.future ? ">" : "<", th.uri);
541 static void
542 handle_session_end(struct imsg *imsg, size_t datalen)
544 if (session == NULL)
545 die();
546 fclose(session);
547 session = NULL;
550 static void
551 handle_dispatch_imsg(int fd, short ev, void *d)
553 struct imsgev *iev = d;
554 int e;
556 if (dispatch_imsg(iev, ev, handlers, sizeof(handlers)) == -1) {
557 /*
558 * This should leave a ~/.cache/telescope/crashed file to
559 * trigger about:crash on next run. Unfortunately, if
560 * the main process dies the fs sticks around and
561 * doesn't notice that the fd was closed. Why EV_READ
562 * is not triggered when a fd is closed on the other end?
563 */
564 e = errno;
565 if ((fd = open(crashed_file, O_CREAT|O_TRUNC|O_WRONLY, 0600))
566 == -1)
567 err(1, "open");
568 close(fd);
569 errx(1, "connection closed: %s", strerror(e));
573 static int
574 fs_send_ui(int type, uint32_t peerid, int fd, const void *data,
575 uint16_t datalen)
577 return imsg_compose_event(iev_ui, type, peerid, 0, fd,
578 data, datalen);
581 static size_t
582 join_path(char *buf, const char *lhs, const char *rhs, size_t buflen)
584 strlcpy(buf, lhs, buflen);
585 return strlcat(buf, rhs, buflen);
588 static void
589 getenv_default(char *buf, const char *name, const char *def, size_t buflen)
591 size_t ret;
592 char *home, *env;
594 if ((home = getenv("HOME")) == NULL)
595 errx(1, "HOME is not defined");
597 if ((env = getenv(name)) != NULL)
598 ret = strlcpy(buf, env, buflen);
599 else
600 ret = join_path(buf, home, def, buflen);
602 if (ret >= buflen)
603 errx(1, "buffer too small for %s", name);
606 static void
607 mkdirs(const char *path, mode_t mode)
609 char copy[PATH_MAX+1], orig[PATH_MAX+1], *parent;
611 strlcpy(copy, path, sizeof(copy));
612 strlcpy(orig, path, sizeof(orig));
613 parent = dirname(copy);
614 if (!strcmp(parent, "/"))
615 return;
616 mkdirs(parent, mode);
618 if (mkdir(orig, mode) != 0) {
619 if (errno == EEXIST)
620 return;
621 err(1, "can't mkdir %s", orig);
625 static void
626 init_paths(void)
628 char xdg_config_base[PATH_MAX];
629 char xdg_data_base[PATH_MAX];
630 char xdg_cache_base[PATH_MAX];
631 char old_path[PATH_MAX];
632 char *home;
633 struct stat info;
635 /* old path */
636 if ((home = getenv("HOME")) == NULL)
637 errx(1, "HOME is not defined");
638 join_path(old_path, home, "/.telescope", sizeof(old_path));
640 /* if ~/.telescope exists, use that instead of xdg dirs */
641 if (stat(old_path, &info) == 0 && S_ISDIR(info.st_mode)) {
642 join_path(config_path_base, home, "/.telescope",
643 sizeof(config_path_base));
644 join_path(data_path_base, home, "/.telescope",
645 sizeof(data_path_base));
646 join_path(cache_path_base, home, "/.telescope",
647 sizeof(cache_path_base));
648 return;
651 /* xdg paths */
652 getenv_default(xdg_config_base, "XDG_CONFIG_HOME", "/.config",
653 sizeof(xdg_config_base));
654 getenv_default(xdg_data_base, "XDG_DATA_HOME", "/.local/share",
655 sizeof(xdg_data_base));
656 getenv_default(xdg_cache_base, "XDG_CACHE_HOME", "/.cache",
657 sizeof(xdg_cache_base));
659 join_path(config_path_base, xdg_config_base, "/telescope",
660 sizeof(config_path_base));
661 join_path(data_path_base, xdg_data_base, "/telescope",
662 sizeof(data_path_base));
663 join_path(cache_path_base, xdg_cache_base, "/telescope",
664 sizeof(cache_path_base));
666 mkdirs(xdg_config_base, S_IRWXU);
667 mkdirs(xdg_data_base, S_IRWXU);
668 mkdirs(xdg_cache_base, S_IRWXU);
670 mkdirs(config_path_base, S_IRWXU);
671 mkdirs(data_path_base, S_IRWXU);
672 mkdirs(cache_path_base, S_IRWXU);
675 int
676 fs_init(void)
678 init_paths();
680 join_path(config_path, config_path_base, "/config",
681 sizeof(config_path));
682 join_path(lockfile_path, cache_path_base, "/lock",
683 sizeof(lockfile_path));
684 join_path(bookmark_file, data_path_base, "/bookmarks.gmi",
685 sizeof(bookmark_file));
686 join_path(known_hosts_file, data_path_base, "/known_hosts",
687 sizeof(known_hosts_file));
688 join_path(known_hosts_tmp, cache_path_base,
689 "/known_hosts.tmp.XXXXXXXXXX", sizeof(known_hosts_tmp));
690 join_path(session_file, cache_path_base, "/session",
691 sizeof(session_file));
692 join_path(crashed_file, cache_path_base, "/crashed",
693 sizeof(crashed_file));
695 return 1;
698 /*
699 * Parse a line of the session file. The format is:
701 * URL [flags,...] [title]\n
702 */
703 static void
704 parse_session_line(char *line, const char **title, uint32_t *flags)
706 char *s, *t, *ap;
708 *title = "";
709 *flags = 0;
710 if ((s = strchr(line, ' ')) == NULL)
711 return;
713 *s++ = '\0';
715 if ((t = strchr(s, ' ')) != NULL) {
716 *t++ = '\0';
717 *title = t;
720 while ((ap = strsep(&s, ",")) != NULL) {
721 if (!strcmp(ap, "current"))
722 *flags |= TAB_CURRENT;
723 else if (!strcmp(ap, "killed"))
724 *flags |= TAB_KILLED;
728 static inline void
729 sendtab(uint32_t flags, const char *uri, const char *title)
731 struct session_tab tab;
733 memset(&tab, 0, sizeof(tab));
734 tab.flags = flags;
736 if (strlcpy(tab.uri, uri, sizeof(tab.uri)) >= sizeof(tab.uri))
737 return;
739 /* don't worry about cached title truncation */
740 if (title != NULL)
741 strlcpy(tab.title, title, sizeof(tab.title));
743 fs_send_ui(IMSG_SESSION_TAB, 0, -1, &tab, sizeof(tab));
746 static inline void
747 sendhist(const char *uri, int future)
749 struct session_tab_hist sth;
751 memset(&sth, 0, sizeof(sth));
752 sth.future = future;
754 if (strlcpy(sth.uri, uri, sizeof(sth.uri)) >= sizeof(sth.uri))
755 return;
757 fs_send_ui(IMSG_SESSION_TAB_HIST, 0, -1, &sth, sizeof(sth));
760 static void
761 load_last_session(void)
763 FILE *session;
764 uint32_t flags;
765 size_t linesize = 0;
766 ssize_t linelen;
767 int first_time = 0;
768 int future;
769 const char *title;
770 char *nl, *s, *line = NULL;
772 if ((session = fopen(session_file, "r")) == NULL) {
773 /* first time? */
774 first_time = 1;
775 goto end;
778 while ((linelen = getline(&line, &linesize, session)) != -1) {
779 if ((nl = strchr(line, '\n')) != NULL)
780 *nl = '\0';
782 if (*line == '<' || *line == '>') {
783 future = *line == '>';
784 s = line+1;
785 if (*s != ' ')
786 continue;
787 sendhist(++s, future);
788 } else {
789 parse_session_line(line, &title, &flags);
790 sendtab(flags, line, title);
794 fclose(session);
795 free(line);
797 if (last_time_crashed())
798 sendtab(TAB_CURRENT, "about:crash", NULL);
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;