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 void load_certs(void);
65 static struct imsgev *iev_ui;
66 static FILE *session;
68 /*
69 * Where to store user data. These are all equal to ~/.telescope if
70 * it exists.
71 */
72 char config_path_base[PATH_MAX];
73 char data_path_base[PATH_MAX];
74 char cache_path_base[PATH_MAX];
76 char config_path[PATH_MAX];
77 char lockfile_path[PATH_MAX];
78 char bookmark_file[PATH_MAX];
79 char known_hosts_file[PATH_MAX], known_hosts_tmp[PATH_MAX];
80 char crashed_file[PATH_MAX];
81 char session_file[PATH_MAX];
83 static imsg_handlerfn *handlers[] = {
84 [IMSG_GET] = handle_get,
85 [IMSG_GET_FILE] = handle_get_file,
86 [IMSG_QUIT] = handle_misc,
87 [IMSG_INIT] = handle_misc,
88 [IMSG_BOOKMARK_PAGE] = handle_bookmark_page,
89 [IMSG_SAVE_CERT] = handle_save_cert,
90 [IMSG_UPDATE_CERT] = handle_update_cert,
91 [IMSG_FILE_OPEN] = handle_file_open,
92 [IMSG_SESSION_START] = handle_session_start,
93 [IMSG_SESSION_TAB] = handle_session_tab,
94 [IMSG_SESSION_TAB_HIST] = handle_session_tab_hist,
95 [IMSG_SESSION_END] = handle_session_end,
96 };
98 static void __attribute__((__noreturn__))
99 die(void)
101 abort(); /* TODO */
104 static void
105 send_file(uint32_t peerid, FILE *f)
107 ssize_t r;
108 char buf[BUFSIZ];
110 for (;;) {
111 r = fread(buf, 1, sizeof(buf), f);
112 if (r != 0)
113 fs_send_ui(IMSG_BUF, peerid, -1, buf, r);
114 if (r != sizeof(buf))
115 break;
117 fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
118 fclose(f);
121 static void
122 handle_get(struct imsg *imsg, size_t datalen)
124 const char *bpath = "bookmarks.gmi";
125 char path[PATH_MAX];
126 FILE *f;
127 const char *data, *p;
128 size_t i;
129 struct page {
130 const char *name;
131 const char *path;
132 const uint8_t *data;
133 size_t len;
134 } pages[] = {
135 {"about", NULL, about_about, about_about_len},
136 {"blank", NULL, about_blank, about_blank_len},
137 {"bookmarks", bpath, bookmarks, bookmarks_len},
138 {"crash", NULL, about_crash, about_crash_len},
139 {"help", NULL, about_help, about_help_len},
140 {"license", NULL, about_license, about_license_len},
141 {"new", NULL, about_new, about_new_len},
142 }, *page = NULL;
144 data = imsg->data;
145 if (data[datalen-1] != '\0') /* make sure it's NUL-terminated */
146 die();
147 if ((data = strchr(data, ':')) == NULL)
148 goto notfound;
149 data++;
151 for (i = 0; i < sizeof(pages)/sizeof(pages[0]); ++i)
152 if (!strcmp(data, pages[i].name)) {
153 page = &pages[i];
154 break;
157 if (page == NULL)
158 goto notfound;
160 strlcpy(path, data_path_base, sizeof(path));
161 strlcat(path, "/", sizeof(path));
162 if (page->path != NULL)
163 strlcat(path, page->path, sizeof(path));
164 else {
165 strlcat(path, "pages/about_", sizeof(path));
166 strlcat(path, page->name, sizeof(path));
167 strlcat(path, ".gmi", sizeof(path));
170 if ((f = fopen(path, "r")) == NULL) {
171 fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1,
172 page->data, page->len);
173 fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1,
174 NULL, 0);
175 return;
178 send_file(imsg->hdr.peerid, f);
179 return;
181 notfound:
182 p = "# not found!\n";
183 fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, p, strlen(p));
184 fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
187 static inline void
188 send_hdr(uint32_t peerid, int code, const char *meta)
190 fs_send_ui(IMSG_GOT_CODE, peerid, -1, &code, sizeof(code));
191 fs_send_ui(IMSG_GOT_META, peerid, -1, meta, strlen(meta)+1);
194 static inline void
195 send_errno(uint32_t peerid, int code, const char *str, int no)
197 char *s;
199 if (asprintf(&s, "%s: %s", str, strerror(no)) == -1)
200 s = NULL;
202 send_hdr(peerid, code, s == NULL ? str : s);
203 free(s);
206 static inline const char *
207 file_type(const char *path)
209 struct mapping {
210 const char *ext;
211 const char *mime;
212 } ms[] = {
213 {"diff", "text/x-patch"},
214 {"gemini", "text/gemini"},
215 {"gmi", "text/gemini"},
216 {"markdown", "text/plain"},
217 {"md", "text/plain"},
218 {"patch", "text/x-patch"},
219 {"txt", "text/plain"},
220 {NULL, NULL},
221 }, *m;
222 char *dot;
224 if ((dot = strrchr(path, '.')) == NULL)
225 return NULL;
227 dot++;
229 for (m = ms; m->ext != NULL; ++m)
230 if (!strcmp(m->ext, dot))
231 return m->mime;
233 return NULL;
236 static int
237 select_non_dot(const struct dirent *d)
239 return strcmp(d->d_name, ".");
242 static int
243 select_non_dotdot(const struct dirent *d)
245 return strcmp(d->d_name, ".") && strcmp(d->d_name, "..");
248 static inline void
249 send_dir(uint32_t peerid, const char *path)
251 struct dirent **names;
252 struct evbuffer *ev;
253 char *s;
254 int (*selector)(const struct dirent *) = select_non_dot;
255 int i, len, no;
257 if (!has_suffix(path, "/")) {
258 if (asprintf(&s, "%s/", path) == -1)
259 die();
260 send_hdr(peerid, 30, s);
261 free(s);
262 return;
265 if (!strcmp(path, "/"))
266 selector = select_non_dotdot;
268 if ((ev = evbuffer_new()) == NULL ||
269 (len = scandir(path, &names, selector, alphasort)) == -1) {
270 no = errno;
271 evbuffer_free(ev);
272 send_errno(peerid, 40, "failure reading the directory", no);
273 return;
276 evbuffer_add_printf(ev, "# Index of %s\n\n", path);
277 for (i = 0; i < len; ++i) {
278 evbuffer_add_printf(ev, "=> %s", names[i]->d_name);
279 if (names[i]->d_type == DT_DIR)
280 evbuffer_add(ev, "/", 1);
281 evbuffer_add(ev, "\n", 1);
284 send_hdr(peerid, 20, "text/gemini");
285 fs_send_ui(IMSG_BUF, peerid, -1,
286 EVBUFFER_DATA(ev), EVBUFFER_LENGTH(ev));
287 fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
289 evbuffer_free(ev);
290 free(names);
293 static void
294 handle_get_file(struct imsg *imsg, size_t datalen)
296 struct stat sb;
297 FILE *f;
298 char *data;
299 const char *meta = NULL;
301 data = imsg->data;
302 data[datalen-1] = '\0';
304 if ((f = fopen(data, "r")) == NULL) {
305 send_errno(imsg->hdr.peerid, 51, "can't open", errno);
306 return;
309 if (fstat(fileno(f), &sb) == -1) {
310 send_errno(imsg->hdr.peerid, 40, "fstat", errno);
311 return;
314 if (S_ISDIR(sb.st_mode)) {
315 fclose(f);
316 send_dir(imsg->hdr.peerid, data);
317 return;
320 if ((meta = file_type(data)) == NULL) {
321 fclose(f);
322 send_hdr(imsg->hdr.peerid, 51,
323 "don't know how to visualize this file");
324 return;
327 send_hdr(imsg->hdr.peerid, 20, meta);
328 send_file(imsg->hdr.peerid, f);
331 static void
332 handle_misc(struct imsg *imsg, size_t datalen)
334 switch (imsg->hdr.type) {
335 case IMSG_INIT:
336 load_certs();
337 load_last_session();
338 break;
340 case IMSG_QUIT:
341 if (!safe_mode)
342 unlink(crashed_file);
343 event_loopbreak();
344 break;
346 default:
347 die();
351 static void
352 handle_bookmark_page(struct imsg *imsg, size_t datalen)
354 char *data;
355 int res;
356 FILE *f;
358 data = imsg->data;
359 if (data[datalen-1] != '\0')
360 die();
362 if ((f = fopen(bookmark_file, "a")) == NULL) {
363 res = errno;
364 goto end;
366 fprintf(f, "=> %s\n", data);
367 fclose(f);
369 res = 0;
370 end:
371 fs_send_ui(IMSG_BOOKMARK_OK, 0, -1, &res, sizeof(res));
374 static void
375 handle_save_cert(struct imsg *imsg, size_t datalen)
377 struct tofu_entry e;
378 FILE *f;
379 int res;
381 /* TODO: traverse the file to avoid duplications? */
383 if (datalen != sizeof(e))
384 die();
385 memcpy(&e, imsg->data, datalen);
387 if ((f = fopen(known_hosts_file, "a")) == NULL) {
388 res = errno;
389 goto end;
391 fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
392 fclose(f);
394 res = 0;
395 end:
396 fs_send_ui(IMSG_SAVE_CERT_OK, imsg->hdr.peerid, -1,
397 &res, sizeof(res));
400 static void
401 handle_update_cert(struct imsg *imsg, size_t datalen)
403 FILE *tmp, *f;
404 struct tofu_entry entry;
405 char sfn[PATH_MAX], *line = NULL, *t;
406 size_t l, linesize = 0;
407 ssize_t linelen;
408 int fd, e, res = 0;
410 if (datalen != sizeof(entry))
411 die();
412 memcpy(&entry, imsg->data, datalen);
414 strlcpy(sfn, known_hosts_tmp, sizeof(sfn));
415 if ((fd = mkstemp(sfn)) == -1 ||
416 (tmp = fdopen(fd, "w")) == NULL) {
417 if (fd != -1) {
418 unlink(sfn);
419 close(fd);
421 res = 0;
422 goto end;
425 if ((f = fopen(known_hosts_file, "r")) == NULL) {
426 unlink(sfn);
427 fclose(tmp);
428 res = 0;
429 goto end;
432 l = strlen(entry.domain);
433 while ((linelen = getline(&line, &linesize, f)) != -1) {
434 if ((t = strstr(line, entry.domain)) != NULL &&
435 (line[l] == ' ' || line[l] == '\t'))
436 continue;
437 /* line has a trailing \n */
438 fprintf(tmp, "%s", line);
440 fprintf(tmp, "%s %s %d\n", entry.domain, entry.hash, entry.verified);
442 free(line);
443 e = ferror(tmp);
445 fclose(tmp);
446 fclose(f);
448 if (e) {
449 unlink(sfn);
450 res = 0;
451 goto end;
454 res = rename(sfn, known_hosts_file) != -1;
456 end:
457 fs_send_ui(IMSG_UPDATE_CERT_OK, imsg->hdr.peerid, -1,
458 &res, sizeof(res));
461 static void
462 handle_file_open(struct imsg *imsg, size_t datalen)
464 char *path, *e;
465 int fd;
467 path = imsg->data;
468 if (path[datalen-1] != '\0')
469 die();
471 if ((fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644)) == -1) {
472 e = strerror(errno);
473 fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, -1,
474 e, strlen(e)+1);
475 } else
476 fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, fd,
477 NULL, 0);
480 static void
481 handle_session_start(struct imsg *imsg, size_t datalen)
483 if (datalen != 0)
484 die();
486 if ((session = fopen(session_file, "w")) == NULL)
487 die();
490 static void
491 handle_session_tab(struct imsg *imsg, size_t datalen)
493 struct session_tab tab;
495 if (session == NULL)
496 die();
498 if (datalen != sizeof(tab))
499 die();
501 memcpy(&tab, imsg->data, sizeof(tab));
502 if (tab.uri[sizeof(tab.uri)-1] != '\0' ||
503 tab.title[sizeof(tab.title)-1] != '\0')
504 die();
506 fprintf(session, "%s", tab.uri);
508 if (tab.flags & TAB_CURRENT)
509 fprintf(session, " current ");
510 else
511 fprintf(session, " - ");
513 fprintf(session, "%s\n", tab.title);
516 static void
517 handle_session_tab_hist(struct imsg *imsg, size_t datalen)
519 struct session_tab_hist th;
521 if (session == NULL)
522 die();
524 if (datalen != sizeof(th))
525 die();
527 memcpy(&th, imsg->data, sizeof(th));
528 if (th.uri[sizeof(th.uri)-1] != '\0')
529 die();
531 fprintf(session, "%s %s\n", th.future ? ">" : "<", th.uri);
534 static void
535 handle_session_end(struct imsg *imsg, size_t datalen)
537 if (session == NULL)
538 die();
539 fclose(session);
540 session = NULL;
543 static void
544 handle_dispatch_imsg(int fd, short ev, void *d)
546 struct imsgev *iev = d;
547 int e;
549 if (dispatch_imsg(iev, ev, handlers, sizeof(handlers)) == -1) {
550 /*
551 * This should leave a ~/.cache/telescope/crashed file to
552 * trigger about:crash on next run. Unfortunately, if
553 * the main process dies the fs sticks around and
554 * doesn't notice that the fd was closed. Why EV_READ
555 * is not triggered when a fd is closed on the other end?
556 */
557 e = errno;
558 if ((fd = open(crashed_file, O_CREAT|O_TRUNC|O_WRONLY, 0600))
559 == -1)
560 err(1, "open");
561 close(fd);
562 errx(1, "connection closed: %s", strerror(e));
566 static int
567 fs_send_ui(int type, uint32_t peerid, int fd, const void *data,
568 uint16_t datalen)
570 return imsg_compose_event(iev_ui, type, peerid, 0, fd,
571 data, datalen);
574 static size_t
575 join_path(char *buf, const char *lhs, const char *rhs, size_t buflen)
577 strlcpy(buf, lhs, buflen);
578 return strlcat(buf, rhs, buflen);
581 static void
582 getenv_default(char *buf, const char *name, const char *def, size_t buflen)
584 size_t ret;
585 char *home, *env;
587 if ((home = getenv("HOME")) == NULL)
588 errx(1, "HOME is not defined");
590 if ((env = getenv(name)) != NULL)
591 ret = strlcpy(buf, env, buflen);
592 else
593 ret = join_path(buf, home, def, buflen);
595 if (ret >= buflen)
596 errx(1, "buffer too small for %s", name);
599 static void
600 mkdirs(const char *path, mode_t mode)
602 char copy[PATH_MAX+1], orig[PATH_MAX+1], *parent;
604 strlcpy(copy, path, sizeof(copy));
605 strlcpy(orig, path, sizeof(orig));
606 parent = dirname(copy);
607 if (!strcmp(parent, "/"))
608 return;
609 mkdirs(parent, mode);
611 if (mkdir(orig, mode) != 0) {
612 if (errno == EEXIST)
613 return;
614 err(1, "can't mkdir %s", orig);
618 static void
619 init_paths(void)
621 char xdg_config_base[PATH_MAX];
622 char xdg_data_base[PATH_MAX];
623 char xdg_cache_base[PATH_MAX];
624 char old_path[PATH_MAX];
625 char *home;
626 struct stat info;
628 /* old path */
629 if ((home = getenv("HOME")) == NULL)
630 errx(1, "HOME is not defined");
631 join_path(old_path, home, "/.telescope", sizeof(old_path));
633 /* if ~/.telescope exists, use that instead of xdg dirs */
634 if (stat(old_path, &info) == 0 && S_ISDIR(info.st_mode)) {
635 join_path(config_path_base, home, "/.telescope",
636 sizeof(config_path_base));
637 join_path(data_path_base, home, "/.telescope",
638 sizeof(data_path_base));
639 join_path(cache_path_base, home, "/.telescope",
640 sizeof(cache_path_base));
641 return;
644 /* xdg paths */
645 getenv_default(xdg_config_base, "XDG_CONFIG_HOME", "/.config",
646 sizeof(xdg_config_base));
647 getenv_default(xdg_data_base, "XDG_DATA_HOME", "/.local/share",
648 sizeof(xdg_data_base));
649 getenv_default(xdg_cache_base, "XDG_CACHE_HOME", "/.cache",
650 sizeof(xdg_cache_base));
652 join_path(config_path_base, xdg_config_base, "/telescope",
653 sizeof(config_path_base));
654 join_path(data_path_base, xdg_data_base, "/telescope",
655 sizeof(data_path_base));
656 join_path(cache_path_base, xdg_cache_base, "/telescope",
657 sizeof(cache_path_base));
659 mkdirs(xdg_config_base, S_IRWXU);
660 mkdirs(xdg_data_base, S_IRWXU);
661 mkdirs(xdg_cache_base, S_IRWXU);
663 mkdirs(config_path_base, S_IRWXU);
664 mkdirs(data_path_base, S_IRWXU);
665 mkdirs(cache_path_base, S_IRWXU);
668 int
669 fs_init(void)
671 init_paths();
673 join_path(config_path, config_path_base, "/config",
674 sizeof(config_path));
675 join_path(lockfile_path, cache_path_base, "/lock",
676 sizeof(lockfile_path));
677 join_path(bookmark_file, data_path_base, "/bookmarks.gmi",
678 sizeof(bookmark_file));
679 join_path(known_hosts_file, data_path_base, "/known_hosts",
680 sizeof(known_hosts_file));
681 join_path(known_hosts_tmp, cache_path_base,
682 "/known_hosts.tmp.XXXXXXXXXX", sizeof(known_hosts_tmp));
683 join_path(session_file, cache_path_base, "/session",
684 sizeof(session_file));
685 join_path(crashed_file, cache_path_base, "/crashed",
686 sizeof(crashed_file));
688 return 1;
691 /*
692 * Parse a line of the session file. The format is:
694 * URL [flags,...] [title]\n
695 */
696 static void
697 parse_session_line(char *line, const char **title, uint32_t *flags)
699 char *s, *t, *ap;
701 *title = "";
702 *flags = 0;
703 if ((s = strchr(line, ' ')) == NULL)
704 return;
706 *s++ = '\0';
708 if ((t = strchr(s, ' ')) != NULL) {
709 *t++ = '\0';
710 *title = t;
713 while ((ap = strsep(&s, ",")) != NULL) {
714 if (!strcmp(ap, "current"))
715 *flags |= TAB_CURRENT;
719 static inline void
720 sendtab(uint32_t flags, const char *uri, const char *title)
722 struct session_tab tab;
724 memset(&tab, 0, sizeof(tab));
725 tab.flags = flags;
727 if (strlcpy(tab.uri, uri, sizeof(tab.uri)) >= sizeof(tab.uri))
728 return;
730 /* don't worry about cached title truncation */
731 if (title != NULL)
732 strlcpy(tab.title, title, sizeof(tab.title));
734 fs_send_ui(IMSG_SESSION_TAB, 0, -1, &tab, sizeof(tab));
737 static inline void
738 sendhist(const char *uri, int future)
740 struct session_tab_hist sth;
742 memset(&sth, 0, sizeof(sth));
743 sth.future = future;
745 if (strlcpy(sth.uri, uri, sizeof(sth.uri)) >= sizeof(sth.uri))
746 return;
748 fs_send_ui(IMSG_SESSION_TAB_HIST, 0, -1, &sth, sizeof(sth));
751 static void
752 load_last_session(void)
754 FILE *session;
755 uint32_t flags;
756 size_t linesize = 0;
757 ssize_t linelen;
758 int first_time = 0;
759 int future;
760 const char *title;
761 char *nl, *s, *line = NULL;
763 if ((session = fopen(session_file, "r")) == NULL) {
764 /* first time? */
765 first_time = 1;
766 goto end;
769 while ((linelen = getline(&line, &linesize, session)) != -1) {
770 if ((nl = strchr(line, '\n')) != NULL)
771 *nl = '\0';
773 if (*line == '<' || *line == '>') {
774 future = *line == '>';
775 s = line+1;
776 if (*s != ' ')
777 continue;
778 sendhist(++s, future);
779 } else {
780 parse_session_line(line, &title, &flags);
781 sendtab(flags, line, title);
785 fclose(session);
786 free(line);
788 if (last_time_crashed())
789 sendtab(TAB_CURRENT, "about:crash", NULL);
791 end:
792 fs_send_ui(IMSG_SESSION_END, 0, -1, &first_time, sizeof(first_time));
795 int
796 fs_main(void)
798 setproctitle("fs");
800 fs_init();
802 event_init();
804 /* Setup pipe and event handler to the main process */
805 if ((iev_ui = malloc(sizeof(*iev_ui))) == NULL)
806 die();
807 imsg_init(&iev_ui->ibuf, 3);
808 iev_ui->handler = handle_dispatch_imsg;
809 iev_ui->events = EV_READ;
810 event_set(&iev_ui->ev, iev_ui->ibuf.fd, iev_ui->events,
811 iev_ui->handler, iev_ui);
812 event_add(&iev_ui->ev, NULL);
814 sandbox_fs_process();
816 event_dispatch();
817 return 0;
822 /*
823 * Check if the last time telescope crashed. The check is done by
824 * looking at `crashed_file': if it exists then last time we crashed.
825 * Then, while here, touch the file too. During IMSG_QUIT we'll
826 * remove it.
827 */
828 int
829 last_time_crashed(void)
831 int fd, crashed = 1;
833 if (safe_mode)
834 return 0;
836 if (unlink(crashed_file) == -1 && errno == ENOENT)
837 crashed = 0;
839 if ((fd = open(crashed_file, O_CREAT|O_WRONLY, 0600)) == -1)
840 return crashed;
841 close(fd);
843 return crashed;
846 int
847 lock_session(void)
849 struct flock lock;
850 int fd;
852 if ((fd = open(lockfile_path, O_WRONLY|O_CREAT, 0600)) == -1)
853 return -1;
855 lock.l_start = 0;
856 lock.l_len = 0;
857 lock.l_type = F_WRLCK;
858 lock.l_whence = SEEK_SET;
860 if (fcntl(fd, F_SETLK, &lock) == -1) {
861 close(fd);
862 return -1;
865 return fd;
868 static inline int
869 parse_khost_line(char *line, char *tmp[3])
871 char **ap;
873 for (ap = tmp; ap < &tmp[3] &&
874 (*ap = strsep(&line, " \t\n")) != NULL;) {
875 if (**ap != '\0')
876 ap++;
879 return ap == &tmp[3] && *line == '\0';
882 static void
883 load_certs(void)
885 char *tmp[3], *line = NULL;
886 const char *errstr;
887 size_t lineno = 0, linesize = 0;
888 ssize_t linelen;
889 FILE *f;
890 struct tofu_entry e;
892 if ((f = fopen(known_hosts_file, "r")) == NULL)
893 return;
895 while ((linelen = getline(&line, &linesize, f)) != -1) {
896 lineno++;
898 memset(&e, 0, sizeof(e));
899 if (parse_khost_line(line, tmp)) {
900 strlcpy(e.domain, tmp[0], sizeof(e.domain));
901 strlcpy(e.hash, tmp[1], sizeof(e.hash));
903 e.verified = strtonum(tmp[2], 0, 1, &errstr);
904 if (errstr != NULL)
905 errx(1, "%s:%zu verification for %s is %s: %s",
906 known_hosts_file, lineno,
907 e.domain, errstr, tmp[2]);
909 fs_send_ui(IMSG_TOFU, 0, -1, &e, sizeof(e));
910 } else {
911 warnx("%s:%zu invalid entry",
912 known_hosts_file, lineno);
916 free(line);
917 fclose(f);
918 return;