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 xdg_init(void);
62 static void load_last_session(void);
63 static void load_certs(void);
65 static struct imsgev *iev_ui;
66 static FILE *session;
68 /*
69 * Note: these are empty if ~/.telescope exists, use *_path_base
70 * below.
71 */
72 static char xdg_config_base[PATH_MAX];
73 static char xdg_data_base[PATH_MAX];
74 static char xdg_cache_base[PATH_MAX];
76 /*
77 * Where to store user data. These are all equal to ~/.telescope if
78 * it exists.
79 */
80 char config_path_base[PATH_MAX];
81 char data_path_base[PATH_MAX];
82 char cache_path_base[PATH_MAX];
84 char config_path[PATH_MAX];
85 char lockfile_path[PATH_MAX];
86 char bookmark_file[PATH_MAX];
87 char known_hosts_file[PATH_MAX], known_hosts_tmp[PATH_MAX];
88 char crashed_file[PATH_MAX];
89 char session_file[PATH_MAX];
91 static imsg_handlerfn *handlers[] = {
92 [IMSG_GET] = handle_get,
93 [IMSG_GET_FILE] = handle_get_file,
94 [IMSG_QUIT] = handle_misc,
95 [IMSG_INIT] = handle_misc,
96 [IMSG_BOOKMARK_PAGE] = handle_bookmark_page,
97 [IMSG_SAVE_CERT] = handle_save_cert,
98 [IMSG_UPDATE_CERT] = handle_update_cert,
99 [IMSG_FILE_OPEN] = handle_file_open,
100 [IMSG_SESSION_START] = handle_session_start,
101 [IMSG_SESSION_TAB] = handle_session_tab,
102 [IMSG_SESSION_TAB_HIST] = handle_session_tab_hist,
103 [IMSG_SESSION_END] = handle_session_end,
104 };
106 static void __attribute__((__noreturn__))
107 die(void)
109 abort(); /* TODO */
112 static void
113 send_file(uint32_t peerid, FILE *f)
115 ssize_t r;
116 char buf[BUFSIZ];
118 for (;;) {
119 r = fread(buf, 1, sizeof(buf), f);
120 if (r != 0)
121 fs_send_ui(IMSG_BUF, peerid, -1, buf, r);
122 if (r != sizeof(buf))
123 break;
125 fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
126 fclose(f);
129 static void
130 handle_get(struct imsg *imsg, size_t datalen)
132 const char *bpath = "bookmarks.gmi";
133 char path[PATH_MAX];
134 FILE *f;
135 const char *data, *p;
136 size_t i;
137 struct page {
138 const char *name;
139 const char *path;
140 const uint8_t *data;
141 size_t len;
142 } pages[] = {
143 {"about", NULL, about_about, about_about_len},
144 {"blank", NULL, about_blank, about_blank_len},
145 {"bookmarks", bpath, bookmarks, bookmarks_len},
146 {"crash", NULL, about_crash, about_crash_len},
147 {"help", NULL, about_help, about_help_len},
148 {"license", NULL, about_license, about_license_len},
149 {"new", NULL, about_new, about_new_len},
150 }, *page = NULL;
152 data = imsg->data;
153 if (data[datalen-1] != '\0') /* make sure it's NUL-terminated */
154 die();
155 if ((data = strchr(data, ':')) == NULL)
156 goto notfound;
157 data++;
159 for (i = 0; i < sizeof(pages)/sizeof(pages[0]); ++i)
160 if (!strcmp(data, pages[i].name)) {
161 page = &pages[i];
162 break;
165 if (page == NULL)
166 goto notfound;
168 strlcpy(path, data_path_base, sizeof(path));
169 strlcat(path, "/", sizeof(path));
170 if (page->path != NULL)
171 strlcat(path, page->path, sizeof(path));
172 else {
173 strlcat(path, "pages/about_", sizeof(path));
174 strlcat(path, page->name, sizeof(path));
175 strlcat(path, ".gmi", sizeof(path));
178 if ((f = fopen(path, "r")) == NULL) {
179 fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1,
180 page->data, page->len);
181 fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1,
182 NULL, 0);
183 return;
186 send_file(imsg->hdr.peerid, f);
187 return;
189 notfound:
190 p = "# not found!\n";
191 fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, p, strlen(p));
192 fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
195 static inline void
196 send_hdr(uint32_t peerid, int code, const char *meta)
198 fs_send_ui(IMSG_GOT_CODE, peerid, -1, &code, sizeof(code));
199 fs_send_ui(IMSG_GOT_META, peerid, -1, meta, strlen(meta)+1);
202 static inline void
203 send_errno(uint32_t peerid, int code, const char *str, int no)
205 char *s;
207 if (asprintf(&s, "%s: %s", str, strerror(no)) == -1)
208 s = NULL;
210 send_hdr(peerid, code, s == NULL ? str : s);
211 free(s);
214 static inline const char *
215 file_type(const char *path)
217 struct mapping {
218 const char *ext;
219 const char *mime;
220 } ms[] = {
221 {"diff", "text/x-patch"},
222 {"gemini", "text/gemini"},
223 {"gmi", "text/gemini"},
224 {"markdown", "text/plain"},
225 {"md", "text/plain"},
226 {"patch", "text/x-patch"},
227 {"txt", "text/plain"},
228 {NULL, NULL},
229 }, *m;
230 char *dot;
232 if ((dot = strrchr(path, '.')) == NULL)
233 return NULL;
235 dot++;
237 for (m = ms; m->ext != NULL; ++m)
238 if (!strcmp(m->ext, dot))
239 return m->mime;
241 return NULL;
244 static int
245 select_non_dot(const struct dirent *d)
247 return strcmp(d->d_name, ".");
250 static int
251 select_non_dotdot(const struct dirent *d)
253 return strcmp(d->d_name, ".") && strcmp(d->d_name, "..");
256 static inline void
257 send_dir(uint32_t peerid, const char *path)
259 struct dirent **names;
260 struct evbuffer *ev;
261 char *s;
262 int (*selector)(const struct dirent *) = select_non_dot;
263 int i, len, no;
265 if (!has_suffix(path, "/")) {
266 if (asprintf(&s, "%s/", path) == -1)
267 die();
268 send_hdr(peerid, 30, s);
269 free(s);
270 return;
273 if (!strcmp(path, "/"))
274 selector = select_non_dotdot;
276 if ((ev = evbuffer_new()) == NULL ||
277 (len = scandir(path, &names, selector, alphasort)) == -1) {
278 no = errno;
279 evbuffer_free(ev);
280 send_errno(peerid, 40, "failure reading the directory", no);
281 return;
284 evbuffer_add_printf(ev, "# Index of %s\n\n", path);
285 for (i = 0; i < len; ++i) {
286 evbuffer_add_printf(ev, "=> %s", names[i]->d_name);
287 if (names[i]->d_type == DT_DIR)
288 evbuffer_add(ev, "/", 1);
289 evbuffer_add(ev, "\n", 1);
292 send_hdr(peerid, 20, "text/gemini");
293 fs_send_ui(IMSG_BUF, peerid, -1,
294 EVBUFFER_DATA(ev), EVBUFFER_LENGTH(ev));
295 fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
297 evbuffer_free(ev);
298 free(names);
301 static void
302 handle_get_file(struct imsg *imsg, size_t datalen)
304 struct stat sb;
305 FILE *f;
306 char *data;
307 const char *meta = NULL;
309 data = imsg->data;
310 data[datalen-1] = '\0';
312 if ((f = fopen(data, "r")) == NULL) {
313 send_errno(imsg->hdr.peerid, 51, "can't open", errno);
314 return;
317 if (fstat(fileno(f), &sb) == -1) {
318 send_errno(imsg->hdr.peerid, 40, "fstat", errno);
319 return;
322 if (S_ISDIR(sb.st_mode)) {
323 fclose(f);
324 send_dir(imsg->hdr.peerid, data);
325 return;
328 if ((meta = file_type(data)) == NULL) {
329 fclose(f);
330 send_hdr(imsg->hdr.peerid, 51,
331 "don't know how to visualize this file");
332 return;
335 send_hdr(imsg->hdr.peerid, 20, meta);
336 send_file(imsg->hdr.peerid, f);
339 static void
340 handle_misc(struct imsg *imsg, size_t datalen)
342 switch (imsg->hdr.type) {
343 case IMSG_INIT:
344 load_certs();
345 load_last_session();
346 break;
348 case IMSG_QUIT:
349 if (!safe_mode)
350 unlink(crashed_file);
351 event_loopbreak();
352 break;
354 default:
355 die();
359 static void
360 handle_bookmark_page(struct imsg *imsg, size_t datalen)
362 char *data;
363 int res;
364 FILE *f;
366 data = imsg->data;
367 if (data[datalen-1] != '\0')
368 die();
370 if ((f = fopen(bookmark_file, "a")) == NULL) {
371 res = errno;
372 goto end;
374 fprintf(f, "=> %s\n", data);
375 fclose(f);
377 res = 0;
378 end:
379 fs_send_ui(IMSG_BOOKMARK_OK, 0, -1, &res, sizeof(res));
382 static void
383 handle_save_cert(struct imsg *imsg, size_t datalen)
385 struct tofu_entry e;
386 FILE *f;
387 int res;
389 /* TODO: traverse the file to avoid duplications? */
391 if (datalen != sizeof(e))
392 die();
393 memcpy(&e, imsg->data, datalen);
395 if ((f = fopen(known_hosts_file, "a")) == NULL) {
396 res = errno;
397 goto end;
399 fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
400 fclose(f);
402 res = 0;
403 end:
404 fs_send_ui(IMSG_SAVE_CERT_OK, imsg->hdr.peerid, -1,
405 &res, sizeof(res));
408 static void
409 handle_update_cert(struct imsg *imsg, size_t datalen)
411 FILE *tmp, *f;
412 struct tofu_entry entry;
413 char sfn[PATH_MAX], *line = NULL, *t;
414 size_t l, linesize = 0;
415 ssize_t linelen;
416 int fd, e, res = 0;
418 if (datalen != sizeof(entry))
419 die();
420 memcpy(&entry, imsg->data, datalen);
422 strlcpy(sfn, known_hosts_tmp, sizeof(sfn));
423 if ((fd = mkstemp(sfn)) == -1 ||
424 (tmp = fdopen(fd, "w")) == NULL) {
425 if (fd != -1) {
426 unlink(sfn);
427 close(fd);
429 res = 0;
430 goto end;
433 if ((f = fopen(known_hosts_file, "r")) == NULL) {
434 unlink(sfn);
435 fclose(tmp);
436 res = 0;
437 goto end;
440 l = strlen(entry.domain);
441 while ((linelen = getline(&line, &linesize, f)) != -1) {
442 if ((t = strstr(line, entry.domain)) != NULL &&
443 (line[l] == ' ' || line[l] == '\t'))
444 continue;
445 /* line has a trailing \n */
446 fprintf(tmp, "%s", line);
448 fprintf(tmp, "%s %s %d\n", entry.domain, entry.hash, entry.verified);
450 free(line);
451 e = ferror(tmp);
453 fclose(tmp);
454 fclose(f);
456 if (e) {
457 unlink(sfn);
458 res = 0;
459 goto end;
462 res = rename(sfn, known_hosts_file) != -1;
464 end:
465 fs_send_ui(IMSG_UPDATE_CERT_OK, imsg->hdr.peerid, -1,
466 &res, sizeof(res));
469 static void
470 handle_file_open(struct imsg *imsg, size_t datalen)
472 char *path, *e;
473 int fd;
475 path = imsg->data;
476 if (path[datalen-1] != '\0')
477 die();
479 if ((fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644)) == -1) {
480 e = strerror(errno);
481 fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, -1,
482 e, strlen(e)+1);
483 } else
484 fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, fd,
485 NULL, 0);
488 static void
489 handle_session_start(struct imsg *imsg, size_t datalen)
491 if (datalen != 0)
492 die();
494 if ((session = fopen(session_file, "w")) == NULL)
495 die();
498 static void
499 handle_session_tab(struct imsg *imsg, size_t datalen)
501 struct session_tab tab;
503 if (session == NULL)
504 die();
506 if (datalen != sizeof(tab))
507 die();
509 memcpy(&tab, imsg->data, sizeof(tab));
510 if (tab.uri[sizeof(tab.uri)-1] != '\0' ||
511 tab.title[sizeof(tab.title)-1] != '\0')
512 die();
514 fprintf(session, "%s", tab.uri);
516 if (tab.flags & TAB_CURRENT)
517 fprintf(session, " current ");
518 else
519 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 xdg_init(void)
629 char *home, old_path[PATH_MAX];
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 xdg_init();
677 join_path(config_path, config_path_base, "/config",
678 sizeof(config_path));
679 join_path(lockfile_path, cache_path_base, "/lock",
680 sizeof(lockfile_path));
681 join_path(bookmark_file, data_path_base, "/bookmarks.gmi",
682 sizeof(bookmark_file));
683 join_path(known_hosts_file, data_path_base, "/known_hosts",
684 sizeof(known_hosts_file));
685 join_path(known_hosts_tmp, cache_path_base,
686 "/known_hosts.tmp.XXXXXXXXXX", sizeof(known_hosts_tmp));
687 join_path(session_file, cache_path_base, "/session",
688 sizeof(session_file));
689 join_path(crashed_file, cache_path_base, "/crashed",
690 sizeof(crashed_file));
692 return 1;
695 /*
696 * Parse a line of the session file. The format is:
698 * URL [flags,...] [title]\n
699 */
700 static void
701 parse_session_line(char *line, const char **title, uint32_t *flags)
703 char *s, *t, *ap;
705 *title = "";
706 *flags = 0;
707 if ((s = strchr(line, ' ')) == NULL)
708 return;
710 *s++ = '\0';
712 if ((t = strchr(s, ' ')) != NULL) {
713 *t++ = '\0';
714 *title = t;
717 while ((ap = strsep(&s, ",")) != NULL) {
718 if (!strcmp(ap, "current"))
719 *flags |= TAB_CURRENT;
723 static inline void
724 sendtab(uint32_t flags, const char *uri, const char *title)
726 struct session_tab tab;
728 memset(&tab, 0, sizeof(tab));
729 tab.flags = flags;
731 if (strlcpy(tab.uri, uri, sizeof(tab.uri)) >= sizeof(tab.uri))
732 return;
734 /* don't worry about cached title truncation */
735 if (title != NULL)
736 strlcpy(tab.title, title, sizeof(tab.title));
738 fs_send_ui(IMSG_SESSION_TAB, 0, -1, &tab, sizeof(tab));
741 static inline void
742 sendhist(const char *uri, int future)
744 struct session_tab_hist sth;
746 memset(&sth, 0, sizeof(sth));
747 sth.future = future;
749 if (strlcpy(sth.uri, uri, sizeof(sth.uri)) >= sizeof(sth.uri))
750 return;
752 fs_send_ui(IMSG_SESSION_TAB_HIST, 0, -1, &sth, sizeof(sth));
755 static void
756 load_last_session(void)
758 FILE *session;
759 uint32_t flags;
760 size_t linesize = 0;
761 ssize_t linelen;
762 int first_time = 0;
763 int future;
764 const char *title;
765 char *nl, *s, *line = NULL;
767 if ((session = fopen(session_file, "r")) == NULL) {
768 /* first time? */
769 first_time = 1;
770 goto end;
773 while ((linelen = getline(&line, &linesize, session)) != -1) {
774 if ((nl = strchr(line, '\n')) != NULL)
775 *nl = '\0';
777 if (*line == '<' || *line == '>') {
778 future = *line == '>';
779 s = line+1;
780 if (*s != ' ')
781 continue;
782 sendhist(++s, future);
783 } else {
784 parse_session_line(line, &title, &flags);
785 sendtab(flags, line, title);
789 fclose(session);
790 free(line);
792 if (last_time_crashed())
793 sendtab(TAB_CURRENT, "about:crash", NULL);
795 end:
796 fs_send_ui(IMSG_SESSION_END, 0, -1, &first_time, sizeof(first_time));
799 int
800 fs_main(void)
802 setproctitle("fs");
804 fs_init();
806 event_init();
808 /* Setup pipe and event handler to the main process */
809 if ((iev_ui = malloc(sizeof(*iev_ui))) == NULL)
810 die();
811 imsg_init(&iev_ui->ibuf, 3);
812 iev_ui->handler = handle_dispatch_imsg;
813 iev_ui->events = EV_READ;
814 event_set(&iev_ui->ev, iev_ui->ibuf.fd, iev_ui->events,
815 iev_ui->handler, iev_ui);
816 event_add(&iev_ui->ev, NULL);
818 sandbox_fs_process();
820 event_dispatch();
821 return 0;
826 /*
827 * Check if the last time telescope crashed. The check is done by
828 * looking at `crashed_file': if it exists then last time we crashed.
829 * Then, while here, touch the file too. During IMSG_QUIT we'll
830 * remove it.
831 */
832 int
833 last_time_crashed(void)
835 int fd, crashed = 1;
837 if (safe_mode)
838 return 0;
840 if (unlink(crashed_file) == -1 && errno == ENOENT)
841 crashed = 0;
843 if ((fd = open(crashed_file, O_CREAT|O_WRONLY, 0600)) == -1)
844 return crashed;
845 close(fd);
847 return crashed;
850 int
851 lock_session(void)
853 struct flock lock;
854 int fd;
856 if ((fd = open(lockfile_path, O_WRONLY|O_CREAT, 0600)) == -1)
857 return -1;
859 lock.l_start = 0;
860 lock.l_len = 0;
861 lock.l_type = F_WRLCK;
862 lock.l_whence = SEEK_SET;
864 if (fcntl(fd, F_SETLK, &lock) == -1) {
865 close(fd);
866 return -1;
869 return fd;
872 static inline int
873 parse_khost_line(char *line, char *tmp[3])
875 char **ap;
877 for (ap = tmp; ap < &tmp[3] &&
878 (*ap = strsep(&line, " \t\n")) != NULL;) {
879 if (**ap != '\0')
880 ap++;
883 return ap == &tmp[3] && *line == '\0';
886 static void
887 load_certs(void)
889 char *tmp[3], *line = NULL;
890 const char *errstr;
891 size_t lineno = 0, linesize = 0;
892 ssize_t linelen;
893 FILE *f;
894 struct tofu_entry e;
896 if ((f = fopen(known_hosts_file, "r")) == NULL)
897 return;
899 while ((linelen = getline(&line, &linesize, f)) != -1) {
900 lineno++;
902 memset(&e, 0, sizeof(e));
903 if (parse_khost_line(line, tmp)) {
904 strlcpy(e.domain, tmp[0], sizeof(e.domain));
905 strlcpy(e.hash, tmp[1], sizeof(e.hash));
907 e.verified = strtonum(tmp[2], 0, 1, &errstr);
908 if (errstr != NULL)
909 errx(1, "%s:%zu verification for %s is %s: %s",
910 known_hosts_file, lineno,
911 e.domain, errstr, tmp[2]);
913 fs_send_ui(IMSG_TOFU, 0, -1, &e, sizeof(e));
914 } else {
915 warnx("%s:%zu invalid entry",
916 known_hosts_file, lineno);
920 free(line);
921 fclose(f);
922 return;