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"
40 static void die(void) __attribute__((__noreturn__));
41 static void send_file(uint32_t, FILE *);
42 static void handle_get(struct imsg*, size_t);
43 static int select_non_dot(const struct dirent *);
44 static int select_non_dotdot(const struct dirent *);
45 static void handle_get_file(struct imsg*, size_t);
46 static void handle_misc(struct imsg *, size_t);
47 static void handle_bookmark_page(struct imsg*, size_t);
48 static void handle_save_cert(struct imsg*, size_t);
49 static void handle_update_cert(struct imsg*, size_t);
50 static void handle_file_open(struct imsg*, size_t);
51 static void handle_session_start(struct imsg*, size_t);
52 static void handle_session_tab(struct imsg*, size_t);
53 static void handle_session_tab_hist(struct imsg*, size_t);
54 static void handle_session_end(struct imsg*, size_t);
55 static void handle_dispatch_imsg(int, short, void*);
56 static int fs_send_ui(int, uint32_t, int, const void *, uint16_t);
57 static size_t join_path(char*, const char*, const char*, size_t);
58 static void getenv_default(char*, const char*, const char*, size_t);
59 static void mkdirs(const char*, mode_t);
60 static void xdg_init(void);
61 static void load_last_session(void);
62 static void load_certs(void);
64 static struct imsgev *iev_ui;
65 static FILE *session;
67 /*
68 * Note: these are empty if ~/.telescope exists, use *_path_base
69 * below.
70 */
71 static char xdg_config_base[PATH_MAX];
72 static char xdg_data_base[PATH_MAX];
73 static char xdg_cache_base[PATH_MAX];
75 /*
76 * Where to store user data. These are all equal to ~/.telescope if
77 * it exists.
78 */
79 char config_path_base[PATH_MAX];
80 char data_path_base[PATH_MAX];
81 char cache_path_base[PATH_MAX];
83 char config_path[PATH_MAX];
84 char lockfile_path[PATH_MAX];
85 char bookmark_file[PATH_MAX];
86 char known_hosts_file[PATH_MAX], known_hosts_tmp[PATH_MAX];
87 char crashed_file[PATH_MAX];
88 char session_file[PATH_MAX];
90 static imsg_handlerfn *handlers[] = {
91 [IMSG_GET] = handle_get,
92 [IMSG_GET_FILE] = handle_get_file,
93 [IMSG_QUIT] = handle_misc,
94 [IMSG_INIT] = handle_misc,
95 [IMSG_BOOKMARK_PAGE] = handle_bookmark_page,
96 [IMSG_SAVE_CERT] = handle_save_cert,
97 [IMSG_UPDATE_CERT] = handle_update_cert,
98 [IMSG_FILE_OPEN] = handle_file_open,
99 [IMSG_SESSION_START] = handle_session_start,
100 [IMSG_SESSION_TAB] = handle_session_tab,
101 [IMSG_SESSION_TAB_HIST] = handle_session_tab_hist,
102 [IMSG_SESSION_END] = handle_session_end,
103 };
105 static void __attribute__((__noreturn__))
106 die(void)
108 abort(); /* TODO */
111 static void
112 send_file(uint32_t peerid, FILE *f)
114 ssize_t r;
115 char buf[BUFSIZ];
117 for (;;) {
118 r = fread(buf, 1, sizeof(buf), f);
119 if (r != 0)
120 fs_send_ui(IMSG_BUF, peerid, -1, buf, r);
121 if (r != sizeof(buf))
122 break;
124 fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
125 fclose(f);
128 static void
129 handle_get(struct imsg *imsg, size_t datalen)
131 const char *bpath = "bookmarks.gmi";
132 char path[PATH_MAX];
133 FILE *f;
134 const char *data, *p;
135 size_t i;
136 struct page {
137 const char *name;
138 const char *path;
139 const uint8_t *data;
140 size_t len;
141 } pages[] = {
142 {"about", NULL, about_about, about_about_len},
143 {"blank", NULL, about_blank, about_blank_len},
144 {"bookmarks", bpath, bookmarks, bookmarks_len},
145 {"crash", NULL, about_crash, about_crash_len},
146 {"help", NULL, about_help, about_help_len},
147 {"license", NULL, about_license, about_license_len},
148 {"new", NULL, about_new, about_new_len},
149 }, *page = NULL;
151 data = imsg->data;
152 if (data[datalen-1] != '\0') /* make sure it's NUL-terminated */
153 die();
154 if ((data = strchr(data, ':')) == NULL)
155 goto notfound;
156 data++;
158 for (i = 0; i < sizeof(pages)/sizeof(pages[0]); ++i)
159 if (!strcmp(data, pages[i].name)) {
160 page = &pages[i];
161 break;
164 if (page == NULL)
165 goto notfound;
167 strlcpy(path, data_path_base, sizeof(path));
168 strlcat(path, "/", sizeof(path));
169 if (page->path != NULL)
170 strlcat(path, page->path, sizeof(path));
171 else {
172 strlcat(path, "pages/about_", sizeof(path));
173 strlcat(path, page->name, sizeof(path));
174 strlcat(path, ".gmi", sizeof(path));
177 if ((f = fopen(path, "r")) == NULL) {
178 fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1,
179 page->data, page->len);
180 fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1,
181 NULL, 0);
182 return;
185 send_file(imsg->hdr.peerid, f);
186 return;
188 notfound:
189 p = "# not found!\n";
190 fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, p, strlen(p));
191 fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
194 static inline void
195 send_hdr(uint32_t peerid, int code, const char *meta)
197 fs_send_ui(IMSG_GOT_CODE, peerid, -1, &code, sizeof(code));
198 fs_send_ui(IMSG_GOT_META, peerid, -1, meta, strlen(meta)+1);
201 static inline void
202 send_errno(uint32_t peerid, int code, const char *str, int no)
204 char *s;
206 if (asprintf(&s, "%s: %s", str, strerror(no)) == -1)
207 s = NULL;
209 send_hdr(peerid, code, s == NULL ? str : s);
210 free(s);
213 static inline const char *
214 file_type(const char *path)
216 struct mapping {
217 const char *ext;
218 const char *mime;
219 } ms[] = {
220 {"diff", "text/x-patch"},
221 {"gemini", "text/gemini"},
222 {"gmi", "text/gemini"},
223 {"markdown", "text/plain"},
224 {"md", "text/plain"},
225 {"patch", "text/x-patch"},
226 {"txt", "text/plain"},
227 {NULL, NULL},
228 }, *m;
229 char *dot;
231 if ((dot = strrchr(path, '.')) == NULL)
232 return NULL;
234 dot++;
236 for (m = ms; m->ext != NULL; ++m)
237 if (!strcmp(m->ext, dot))
238 return m->mime;
240 return NULL;
243 static int
244 select_non_dot(const struct dirent *d)
246 return strcmp(d->d_name, ".");
249 static int
250 select_non_dotdot(const struct dirent *d)
252 return strcmp(d->d_name, ".") && strcmp(d->d_name, "..");
255 static inline void
256 send_dir(uint32_t peerid, const char *path)
258 struct dirent **names;
259 struct evbuffer *ev;
260 char *s;
261 int (*selector)(const struct dirent *) = select_non_dot;
262 int i, len, no;
264 if (!has_suffix(path, "/")) {
265 if (asprintf(&s, "%s/", path) == -1)
266 die();
267 send_hdr(peerid, 30, s);
268 free(s);
269 return;
272 if (!strcmp(path, "/"))
273 selector = select_non_dotdot;
275 if ((ev = evbuffer_new()) == NULL ||
276 (len = scandir(path, &names, selector, alphasort)) == -1) {
277 no = errno;
278 evbuffer_free(ev);
279 send_errno(peerid, 40, "failure reading the directory", no);
280 return;
283 evbuffer_add_printf(ev, "# Index of %s\n\n", path);
284 for (i = 0; i < len; ++i) {
285 evbuffer_add_printf(ev, "=> %s", names[i]->d_name);
286 if (names[i]->d_type == DT_DIR)
287 evbuffer_add(ev, "/", 1);
288 evbuffer_add(ev, "\n", 1);
291 send_hdr(peerid, 20, "text/gemini");
292 fs_send_ui(IMSG_BUF, peerid, -1,
293 EVBUFFER_DATA(ev), EVBUFFER_LENGTH(ev));
294 fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
296 evbuffer_free(ev);
297 free(names);
300 static void
301 handle_get_file(struct imsg *imsg, size_t datalen)
303 struct stat sb;
304 FILE *f;
305 char *data;
306 const char *meta = NULL;
308 data = imsg->data;
309 data[datalen-1] = '\0';
311 if ((f = fopen(data, "r")) == NULL) {
312 send_errno(imsg->hdr.peerid, 51, "can't open", errno);
313 return;
316 if (fstat(fileno(f), &sb) == -1) {
317 send_errno(imsg->hdr.peerid, 40, "fstat", errno);
318 return;
321 if (S_ISDIR(sb.st_mode)) {
322 fclose(f);
323 send_dir(imsg->hdr.peerid, data);
324 return;
327 if ((meta = file_type(data)) == NULL) {
328 fclose(f);
329 send_hdr(imsg->hdr.peerid, 51,
330 "don't know how to visualize this file");
331 return;
334 send_hdr(imsg->hdr.peerid, 20, meta);
335 send_file(imsg->hdr.peerid, f);
338 static void
339 handle_misc(struct imsg *imsg, size_t datalen)
341 switch (imsg->hdr.type) {
342 case IMSG_INIT:
343 load_certs();
344 load_last_session();
345 break;
347 case IMSG_QUIT:
348 if (!safe_mode)
349 unlink(crashed_file);
350 event_loopbreak();
351 break;
353 default:
354 die();
358 static void
359 handle_bookmark_page(struct imsg *imsg, size_t datalen)
361 char *data;
362 int res;
363 FILE *f;
365 data = imsg->data;
366 if (data[datalen-1] != '\0')
367 die();
369 if ((f = fopen(bookmark_file, "a")) == NULL) {
370 res = errno;
371 goto end;
373 fprintf(f, "=> %s\n", data);
374 fclose(f);
376 res = 0;
377 end:
378 fs_send_ui(IMSG_BOOKMARK_OK, 0, -1, &res, sizeof(res));
381 static void
382 handle_save_cert(struct imsg *imsg, size_t datalen)
384 struct tofu_entry e;
385 FILE *f;
386 int res;
388 /* TODO: traverse the file to avoid duplications? */
390 if (datalen != sizeof(e))
391 die();
392 memcpy(&e, imsg->data, datalen);
394 if ((f = fopen(known_hosts_file, "a")) == NULL) {
395 res = errno;
396 goto end;
398 fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
399 fclose(f);
401 res = 0;
402 end:
403 fs_send_ui(IMSG_SAVE_CERT_OK, imsg->hdr.peerid, -1,
404 &res, sizeof(res));
407 static void
408 handle_update_cert(struct imsg *imsg, size_t datalen)
410 FILE *tmp, *f;
411 struct tofu_entry entry;
412 char sfn[PATH_MAX], *line = NULL, *t;
413 size_t l, linesize = 0;
414 ssize_t linelen;
415 int fd, e, res = 0;
417 if (datalen != sizeof(entry))
418 die();
419 memcpy(&entry, imsg->data, datalen);
421 strlcpy(sfn, known_hosts_tmp, sizeof(sfn));
422 if ((fd = mkstemp(sfn)) == -1 ||
423 (tmp = fdopen(fd, "w")) == NULL) {
424 if (fd != -1) {
425 unlink(sfn);
426 close(fd);
428 res = 0;
429 goto end;
432 if ((f = fopen(known_hosts_file, "r")) == NULL) {
433 unlink(sfn);
434 fclose(tmp);
435 res = 0;
436 goto end;
439 l = strlen(entry.domain);
440 while ((linelen = getline(&line, &linesize, f)) != -1) {
441 if ((t = strstr(line, entry.domain)) != NULL &&
442 (line[l] == ' ' || line[l] == '\t'))
443 continue;
444 /* line has a trailing \n */
445 fprintf(tmp, "%s", line);
447 fprintf(tmp, "%s %s %d\n", entry.domain, entry.hash, entry.verified);
449 free(line);
450 e = ferror(tmp);
452 fclose(tmp);
453 fclose(f);
455 if (e) {
456 unlink(sfn);
457 res = 0;
458 goto end;
461 res = rename(sfn, known_hosts_file) != -1;
463 end:
464 fs_send_ui(IMSG_UPDATE_CERT_OK, imsg->hdr.peerid, -1,
465 &res, sizeof(res));
468 static void
469 handle_file_open(struct imsg *imsg, size_t datalen)
471 char *path, *e;
472 int fd;
474 path = imsg->data;
475 if (path[datalen-1] != '\0')
476 die();
478 if ((fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644)) == -1) {
479 e = strerror(errno);
480 fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, -1,
481 e, strlen(e)+1);
482 } else
483 fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, fd,
484 NULL, 0);
487 static void
488 handle_session_start(struct imsg *imsg, size_t datalen)
490 if (datalen != 0)
491 die();
493 if ((session = fopen(session_file, "w")) == NULL)
494 die();
497 static void
498 handle_session_tab(struct imsg *imsg, size_t datalen)
500 struct session_tab tab;
502 if (session == NULL)
503 die();
505 if (datalen != sizeof(tab))
506 die();
508 memcpy(&tab, imsg->data, sizeof(tab));
509 if (tab.uri[sizeof(tab.uri)-1] != '\0' ||
510 tab.title[sizeof(tab.title)-1] != '\0')
511 die();
513 fprintf(session, "%s", tab.uri);
515 if (tab.flags & TAB_CURRENT)
516 fprintf(session, " current ");
517 else
518 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 xdg_init(void)
628 char *home, old_path[PATH_MAX];
629 struct stat info;
631 /* old path */
632 if ((home = getenv("HOME")) == NULL)
633 errx(1, "HOME is not defined");
634 join_path(old_path, home, "/.telescope", sizeof(old_path));
636 /* if ~/.telescope exists, use that instead of xdg dirs */
637 if (stat(old_path, &info) == 0 && S_ISDIR(info.st_mode)) {
638 join_path(config_path_base, home, "/.telescope",
639 sizeof(config_path_base));
640 join_path(data_path_base, home, "/.telescope",
641 sizeof(data_path_base));
642 join_path(cache_path_base, home, "/.telescope",
643 sizeof(cache_path_base));
644 return;
647 /* xdg paths */
648 getenv_default(xdg_config_base, "XDG_CONFIG_HOME", "/.config",
649 sizeof(xdg_config_base));
650 getenv_default(xdg_data_base, "XDG_DATA_HOME", "/.local/share",
651 sizeof(xdg_data_base));
652 getenv_default(xdg_cache_base, "XDG_CACHE_HOME", "/.cache",
653 sizeof(xdg_cache_base));
655 join_path(config_path_base, xdg_config_base, "/telescope",
656 sizeof(config_path_base));
657 join_path(data_path_base, xdg_data_base, "/telescope",
658 sizeof(data_path_base));
659 join_path(cache_path_base, xdg_cache_base, "/telescope",
660 sizeof(cache_path_base));
662 mkdirs(xdg_config_base, S_IRWXU);
663 mkdirs(xdg_data_base, S_IRWXU);
664 mkdirs(xdg_cache_base, S_IRWXU);
666 mkdirs(config_path_base, S_IRWXU);
667 mkdirs(data_path_base, S_IRWXU);
668 mkdirs(cache_path_base, S_IRWXU);
671 int
672 fs_init(void)
674 xdg_init();
676 join_path(config_path, config_path_base, "/config",
677 sizeof(config_path));
678 join_path(lockfile_path, cache_path_base, "/lock",
679 sizeof(lockfile_path));
680 join_path(bookmark_file, data_path_base, "/bookmarks.gmi",
681 sizeof(bookmark_file));
682 join_path(known_hosts_file, data_path_base, "/known_hosts",
683 sizeof(known_hosts_file));
684 join_path(known_hosts_tmp, cache_path_base,
685 "/known_hosts.tmp.XXXXXXXXXX", sizeof(known_hosts_tmp));
686 join_path(session_file, cache_path_base, "/session",
687 sizeof(session_file));
688 join_path(crashed_file, cache_path_base, "/crashed",
689 sizeof(crashed_file));
691 return 1;
694 /*
695 * Parse a line of the session file. The format is:
697 * URL [flags,...] [title]\n
698 */
699 static void
700 parse_session_line(char *line, const char **title, uint32_t *flags)
702 char *s, *t, *ap;
704 *title = "";
705 *flags = 0;
706 if ((s = strchr(line, ' ')) == NULL)
707 return;
709 *s++ = '\0';
711 if ((t = strchr(s, ' ')) != NULL) {
712 *t++ = '\0';
713 *title = t;
716 while ((ap = strsep(&s, ",")) != NULL) {
717 if (!strcmp(ap, "current"))
718 *flags |= TAB_CURRENT;
722 static inline void
723 sendtab(uint32_t flags, const char *uri, const char *title)
725 struct session_tab tab;
727 memset(&tab, 0, sizeof(tab));
728 tab.flags = flags;
730 if (strlcpy(tab.uri, uri, sizeof(tab.uri)) >= sizeof(tab.uri))
731 return;
733 /* don't worry about cached title truncation */
734 if (title != NULL)
735 strlcpy(tab.title, title, sizeof(tab.title));
737 fs_send_ui(IMSG_SESSION_TAB, 0, -1, &tab, sizeof(tab));
740 static inline void
741 sendhist(const char *uri, int future)
743 struct session_tab_hist sth;
745 memset(&sth, 0, sizeof(sth));
746 sth.future = future;
748 if (strlcpy(sth.uri, uri, sizeof(sth.uri)) >= sizeof(sth.uri))
749 return;
751 fs_send_ui(IMSG_SESSION_TAB_HIST, 0, -1, &sth, sizeof(sth));
754 static void
755 load_last_session(void)
757 FILE *session;
758 uint32_t flags;
759 size_t linesize = 0;
760 ssize_t linelen;
761 int first_time = 0;
762 int future;
763 const char *title;
764 char *nl, *s, *line = NULL;
766 if ((session = fopen(session_file, "r")) == NULL) {
767 /* first time? */
768 first_time = 1;
769 goto end;
772 while ((linelen = getline(&line, &linesize, session)) != -1) {
773 if ((nl = strchr(line, '\n')) != NULL)
774 *nl = '\0';
776 if (*line == '<' || *line == '>') {
777 future = *line == '>';
778 s = line+1;
779 if (*s != ' ')
780 continue;
781 sendhist(++s, future);
782 } else {
783 parse_session_line(line, &title, &flags);
784 sendtab(flags, line, title);
788 fclose(session);
789 free(line);
791 if (last_time_crashed())
792 sendtab(TAB_CURRENT, "about:crash", NULL);
794 end:
795 fs_send_ui(IMSG_SESSION_END, 0, -1, &first_time, sizeof(first_time));
798 int
799 fs_main(void)
801 setproctitle("fs");
803 fs_init();
805 event_init();
807 /* Setup pipe and event handler to the main process */
808 if ((iev_ui = malloc(sizeof(*iev_ui))) == NULL)
809 die();
810 imsg_init(&iev_ui->ibuf, 3);
811 iev_ui->handler = handle_dispatch_imsg;
812 iev_ui->events = EV_READ;
813 event_set(&iev_ui->ev, iev_ui->ibuf.fd, iev_ui->events,
814 iev_ui->handler, iev_ui);
815 event_add(&iev_ui->ev, NULL);
817 sandbox_fs_process();
819 event_dispatch();
820 return 0;
825 /*
826 * Check if the last time telescope crashed. The check is done by
827 * looking at `crashed_file': if it exists then last time we crashed.
828 * Then, while here, touch the file too. During IMSG_QUIT we'll
829 * remove it.
830 */
831 int
832 last_time_crashed(void)
834 int fd, crashed = 1;
836 if (safe_mode)
837 return 0;
839 if (unlink(crashed_file) == -1 && errno == ENOENT)
840 crashed = 0;
842 if ((fd = open(crashed_file, O_CREAT|O_WRONLY, 0600)) == -1)
843 return crashed;
844 close(fd);
846 return crashed;
849 int
850 lock_session(void)
852 struct flock lock;
853 int fd;
855 if ((fd = open(lockfile_path, O_WRONLY|O_CREAT, 0600)) == -1)
856 return -1;
858 lock.l_start = 0;
859 lock.l_len = 0;
860 lock.l_type = F_WRLCK;
861 lock.l_whence = SEEK_SET;
863 if (fcntl(fd, F_SETLK, &lock) == -1) {
864 close(fd);
865 return -1;
868 return fd;
871 static inline int
872 parse_khost_line(char *line, char *tmp[3])
874 char **ap;
876 for (ap = tmp; ap < &tmp[3] &&
877 (*ap = strsep(&line, " \t\n")) != NULL;) {
878 if (**ap != '\0')
879 ap++;
882 return ap == &tmp[3] && *line == '\0';
885 static void
886 load_certs(void)
888 char *tmp[3], *line = NULL;
889 const char *errstr;
890 size_t lineno = 0, linesize = 0;
891 ssize_t linelen;
892 FILE *f;
893 struct tofu_entry e;
895 if ((f = fopen(known_hosts_file, "r")) == NULL)
896 return;
898 while ((linelen = getline(&line, &linesize, f)) != -1) {
899 lineno++;
901 memset(&e, 0, sizeof(e));
902 if (parse_khost_line(line, tmp)) {
903 strlcpy(e.domain, tmp[0], sizeof(e.domain));
904 strlcpy(e.hash, tmp[1], sizeof(e.hash));
906 e.verified = strtonum(tmp[2], 0, 1, &errstr);
907 if (errstr != NULL)
908 errx(1, "%s:%zu verification for %s is %s: %s",
909 known_hosts_file, lineno,
910 e.domain, errstr, tmp[2]);
912 fs_send_ui(IMSG_TOFU, 0, -1, &e, sizeof(e));
913 } else {
914 warnx("%s:%zu invalid entry",
915 known_hosts_file, lineno);
919 free(line);
920 fclose(f);
921 return;