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_quit(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);
62 static struct imsgev *iev_ui;
63 static FILE *session;
65 /*
66 * Note: these are empty if ~/.telescope exists, use *_path_base
67 * below.
68 */
69 static char xdg_config_base[PATH_MAX];
70 static char xdg_data_base[PATH_MAX];
71 static char xdg_cache_base[PATH_MAX];
73 /*
74 * Where to store user data. These are all equal to ~/.telescope if
75 * it exists.
76 */
77 char config_path_base[PATH_MAX];
78 char data_path_base[PATH_MAX];
79 char cache_path_base[PATH_MAX];
81 char config_path[PATH_MAX];
82 char lockfile_path[PATH_MAX];
83 char bookmark_file[PATH_MAX];
84 char known_hosts_file[PATH_MAX], known_hosts_tmp[PATH_MAX];
85 char crashed_file[PATH_MAX];
86 char session_file[PATH_MAX];
88 static imsg_handlerfn *handlers[] = {
89 [IMSG_GET] = handle_get,
90 [IMSG_GET_FILE] = handle_get_file,
91 [IMSG_QUIT] = handle_quit,
92 [IMSG_BOOKMARK_PAGE] = handle_bookmark_page,
93 [IMSG_SAVE_CERT] = handle_save_cert,
94 [IMSG_UPDATE_CERT] = handle_update_cert,
95 [IMSG_FILE_OPEN] = handle_file_open,
96 [IMSG_SESSION_START] = handle_session_start,
97 [IMSG_SESSION_TAB] = handle_session_tab,
98 [IMSG_SESSION_TAB_HIST] = handle_session_tab_hist,
99 [IMSG_SESSION_END] = handle_session_end,
100 };
102 static void __attribute__((__noreturn__))
103 die(void)
105 abort(); /* TODO */
108 static void
109 send_file(uint32_t peerid, FILE *f)
111 ssize_t r;
112 char buf[BUFSIZ];
114 for (;;) {
115 r = fread(buf, 1, sizeof(buf), f);
116 if (r != 0)
117 fs_send_ui(IMSG_BUF, peerid, -1, buf, r);
118 if (r != sizeof(buf))
119 break;
121 fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
122 fclose(f);
125 static void
126 handle_get(struct imsg *imsg, size_t datalen)
128 const char *bpath = "bookmarks.gmi";
129 char path[PATH_MAX];
130 FILE *f;
131 const char *data, *p;
132 size_t i;
133 struct page {
134 const char *name;
135 const char *path;
136 const uint8_t *data;
137 size_t len;
138 } pages[] = {
139 {"about", NULL, about_about, about_about_len},
140 {"blank", NULL, about_blank, about_blank_len},
141 {"bookmarks", bpath, bookmarks, bookmarks_len},
142 {"crash", NULL, about_crash, about_crash_len},
143 {"help", NULL, about_help, about_help_len},
144 {"license", NULL, about_license, about_license_len},
145 {"new", NULL, about_new, about_new_len},
146 }, *page = NULL;
148 data = imsg->data;
149 if (data[datalen-1] != '\0') /* make sure it's NUL-terminated */
150 die();
151 if ((data = strchr(data, ':')) == NULL)
152 goto notfound;
153 data++;
155 for (i = 0; i < sizeof(pages)/sizeof(pages[0]); ++i)
156 if (!strcmp(data, pages[i].name)) {
157 page = &pages[i];
158 break;
161 if (page == NULL)
162 goto notfound;
164 strlcpy(path, data_path_base, sizeof(path));
165 strlcat(path, "/", sizeof(path));
166 if (page->path != NULL)
167 strlcat(path, page->path, sizeof(path));
168 else {
169 strlcat(path, "pages/about_", sizeof(path));
170 strlcat(path, page->name, sizeof(path));
171 strlcat(path, ".gmi", sizeof(path));
174 if ((f = fopen(path, "r")) == NULL) {
175 fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1,
176 page->data, page->len);
177 fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1,
178 NULL, 0);
179 return;
182 send_file(imsg->hdr.peerid, f);
183 return;
185 notfound:
186 p = "# not found!\n";
187 fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, p, strlen(p));
188 fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
191 static inline void
192 send_hdr(uint32_t peerid, int code, const char *meta)
194 fs_send_ui(IMSG_GOT_CODE, peerid, -1, &code, sizeof(code));
195 fs_send_ui(IMSG_GOT_META, peerid, -1, meta, strlen(meta)+1);
198 static inline void
199 send_errno(uint32_t peerid, int code, const char *str, int no)
201 char *s;
203 if (asprintf(&s, "%s: %s", str, strerror(no)) == -1)
204 s = NULL;
206 send_hdr(peerid, code, s == NULL ? str : s);
207 free(s);
210 static inline const char *
211 file_type(const char *path)
213 struct mapping {
214 const char *ext;
215 const char *mime;
216 } ms[] = {
217 {"diff", "text/x-patch"},
218 {"gemini", "text/gemini"},
219 {"gmi", "text/gemini"},
220 {"markdown", "text/plain"},
221 {"md", "text/plain"},
222 {"patch", "text/x-patch"},
223 {"txt", "text/plain"},
224 {NULL, NULL},
225 }, *m;
226 char *dot;
228 if ((dot = strrchr(path, '.')) == NULL)
229 return NULL;
231 dot++;
233 for (m = ms; m->ext != NULL; ++m)
234 if (!strcmp(m->ext, dot))
235 return m->mime;
237 return NULL;
240 static int
241 select_non_dot(const struct dirent *d)
243 return strcmp(d->d_name, ".");
246 static int
247 select_non_dotdot(const struct dirent *d)
249 return strcmp(d->d_name, ".") && strcmp(d->d_name, "..");
252 static inline void
253 send_dir(uint32_t peerid, const char *path)
255 struct dirent **names;
256 struct evbuffer *ev;
257 char *s;
258 int (*selector)(const struct dirent *) = select_non_dot;
259 int i, len, no;
261 if (!has_suffix(path, "/")) {
262 if (asprintf(&s, "%s/", path) == -1)
263 die();
264 send_hdr(peerid, 30, s);
265 free(s);
266 return;
269 if (!strcmp(path, "/"))
270 selector = select_non_dotdot;
272 if ((ev = evbuffer_new()) == NULL ||
273 (len = scandir(path, &names, selector, alphasort)) == -1) {
274 no = errno;
275 evbuffer_free(ev);
276 send_errno(peerid, 40, "failure reading the directory", no);
277 return;
280 evbuffer_add_printf(ev, "# Index of %s\n\n", path);
281 for (i = 0; i < len; ++i) {
282 evbuffer_add_printf(ev, "=> %s", names[i]->d_name);
283 if (names[i]->d_type == DT_DIR)
284 evbuffer_add(ev, "/", 1);
285 evbuffer_add(ev, "\n", 1);
288 send_hdr(peerid, 20, "text/gemini");
289 fs_send_ui(IMSG_BUF, peerid, -1,
290 EVBUFFER_DATA(ev), EVBUFFER_LENGTH(ev));
291 fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
293 evbuffer_free(ev);
294 free(names);
297 static void
298 handle_get_file(struct imsg *imsg, size_t datalen)
300 struct stat sb;
301 FILE *f;
302 char *data;
303 const char *meta = NULL;
305 data = imsg->data;
306 data[datalen-1] = '\0';
308 if ((f = fopen(data, "r")) == NULL) {
309 send_errno(imsg->hdr.peerid, 51, "can't open", errno);
310 return;
313 if (fstat(fileno(f), &sb) == -1) {
314 send_errno(imsg->hdr.peerid, 40, "fstat", errno);
315 return;
318 if (S_ISDIR(sb.st_mode)) {
319 fclose(f);
320 send_dir(imsg->hdr.peerid, data);
321 return;
324 if ((meta = file_type(data)) == NULL) {
325 fclose(f);
326 send_hdr(imsg->hdr.peerid, 51,
327 "don't know how to visualize this file");
328 return;
331 send_hdr(imsg->hdr.peerid, 20, meta);
332 send_file(imsg->hdr.peerid, f);
335 static void
336 handle_quit(struct imsg *imsg, size_t datalen)
338 if (!safe_mode)
339 unlink(crashed_file);
341 event_loopbreak();
344 static void
345 handle_bookmark_page(struct imsg *imsg, size_t datalen)
347 char *data;
348 int res;
349 FILE *f;
351 data = imsg->data;
352 if (data[datalen-1] != '\0')
353 die();
355 if ((f = fopen(bookmark_file, "a")) == NULL) {
356 res = errno;
357 goto end;
359 fprintf(f, "=> %s\n", data);
360 fclose(f);
362 res = 0;
363 end:
364 fs_send_ui(IMSG_BOOKMARK_OK, 0, -1, &res, sizeof(res));
367 static void
368 handle_save_cert(struct imsg *imsg, size_t datalen)
370 struct tofu_entry e;
371 FILE *f;
372 int res;
374 /* TODO: traverse the file to avoid duplications? */
376 if (datalen != sizeof(e))
377 die();
378 memcpy(&e, imsg->data, datalen);
380 if ((f = fopen(known_hosts_file, "a")) == NULL) {
381 res = errno;
382 goto end;
384 fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
385 fclose(f);
387 res = 0;
388 end:
389 fs_send_ui(IMSG_SAVE_CERT_OK, imsg->hdr.peerid, -1,
390 &res, sizeof(res));
393 static void
394 handle_update_cert(struct imsg *imsg, size_t datalen)
396 FILE *tmp, *f;
397 struct tofu_entry entry;
398 char sfn[PATH_MAX], *line = NULL, *t;
399 size_t l, linesize = 0;
400 ssize_t linelen;
401 int fd, e, res = 0;
403 if (datalen != sizeof(entry))
404 die();
405 memcpy(&entry, imsg->data, datalen);
407 strlcpy(sfn, known_hosts_tmp, sizeof(sfn));
408 if ((fd = mkstemp(sfn)) == -1 ||
409 (tmp = fdopen(fd, "w")) == NULL) {
410 if (fd != -1) {
411 unlink(sfn);
412 close(fd);
414 res = 0;
415 goto end;
418 if ((f = fopen(known_hosts_file, "r")) == NULL) {
419 unlink(sfn);
420 fclose(tmp);
421 res = 0;
422 goto end;
425 l = strlen(entry.domain);
426 while ((linelen = getline(&line, &linesize, f)) != -1) {
427 if ((t = strstr(line, entry.domain)) != NULL &&
428 (line[l] == ' ' || line[l] == '\t'))
429 continue;
430 /* line has a trailing \n */
431 fprintf(tmp, "%s", line);
433 fprintf(tmp, "%s %s %d\n", entry.domain, entry.hash, entry.verified);
435 free(line);
436 e = ferror(tmp);
438 fclose(tmp);
439 fclose(f);
441 if (e) {
442 unlink(sfn);
443 res = 0;
444 goto end;
447 res = rename(sfn, known_hosts_file) != -1;
449 end:
450 fs_send_ui(IMSG_UPDATE_CERT_OK, imsg->hdr.peerid, -1,
451 &res, sizeof(res));
454 static void
455 handle_file_open(struct imsg *imsg, size_t datalen)
457 char *path, *e;
458 int fd;
460 path = imsg->data;
461 if (path[datalen-1] != '\0')
462 die();
464 if ((fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644)) == -1) {
465 e = strerror(errno);
466 fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, -1,
467 e, strlen(e)+1);
468 } else
469 fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, fd,
470 NULL, 0);
473 static void
474 handle_session_start(struct imsg *imsg, size_t datalen)
476 if (datalen != 0)
477 die();
479 if ((session = fopen(session_file, "w")) == NULL)
480 die();
483 static void
484 handle_session_tab(struct imsg *imsg, size_t datalen)
486 struct session_tab tab;
488 if (session == NULL)
489 die();
491 if (datalen != sizeof(tab))
492 die();
494 memcpy(&tab, imsg->data, sizeof(tab));
495 if (tab.uri[sizeof(tab.uri)-1] != '\0' ||
496 tab.title[sizeof(tab.title)-1] != '\0')
497 die();
499 fprintf(session, "%s", tab.uri);
501 if (tab.flags & TAB_CURRENT)
502 fprintf(session, " current ");
503 else
504 fprintf(session, " - ");
506 fprintf(session, "%s\n", tab.title);
509 static void
510 handle_session_tab_hist(struct imsg *imsg, size_t datalen)
512 struct session_tab_hist th;
514 if (session == NULL)
515 die();
517 if (datalen != sizeof(th))
518 die();
520 memcpy(&th, imsg->data, sizeof(th));
521 if (th.uri[sizeof(th.uri)-1] != '\0')
522 die();
524 fprintf(session, "%s %s\n", th.future ? ">" : "<", th.uri);
527 static void
528 handle_session_end(struct imsg *imsg, size_t datalen)
530 if (session == NULL)
531 die();
532 fclose(session);
533 session = NULL;
536 static void
537 handle_dispatch_imsg(int fd, short ev, void *d)
539 struct imsgev *iev = d;
540 int e;
542 if (dispatch_imsg(iev, ev, handlers, sizeof(handlers)) == -1) {
543 /*
544 * This should leave a ~/.cache/telescope/crashed file to
545 * trigger about:crash on next run. Unfortunately, if
546 * the main process dies the fs sticks around and
547 * doesn't notice that the fd was closed. Why EV_READ
548 * is not triggered when a fd is closed on the other end?
549 */
550 e = errno;
551 if ((fd = open(crashed_file, O_CREAT|O_TRUNC|O_WRONLY, 0600))
552 == -1)
553 err(1, "open");
554 close(fd);
555 errx(1, "connection closed: %s", strerror(e));
559 static int
560 fs_send_ui(int type, uint32_t peerid, int fd, const void *data,
561 uint16_t datalen)
563 return imsg_compose_event(iev_ui, type, peerid, 0, fd,
564 data, datalen);
567 static size_t
568 join_path(char *buf, const char *lhs, const char *rhs, size_t buflen)
570 strlcpy(buf, lhs, buflen);
571 return strlcat(buf, rhs, buflen);
574 static void
575 getenv_default(char *buf, const char *name, const char *def, size_t buflen)
577 size_t ret;
578 char *home, *env;
580 if ((home = getenv("HOME")) == NULL)
581 errx(1, "HOME is not defined");
583 if ((env = getenv(name)) != NULL)
584 ret = strlcpy(buf, env, buflen);
585 else
586 ret = join_path(buf, home, def, buflen);
588 if (ret >= buflen)
589 errx(1, "buffer too small for %s", name);
592 static void
593 mkdirs(const char *path, mode_t mode)
595 char copy[PATH_MAX+1], orig[PATH_MAX+1], *parent;
597 strlcpy(copy, path, sizeof(copy));
598 strlcpy(orig, path, sizeof(orig));
599 parent = dirname(copy);
600 if (!strcmp(parent, "/"))
601 return;
602 mkdirs(parent, mode);
604 if (mkdir(orig, mode) != 0) {
605 if (errno == EEXIST)
606 return;
607 err(1, "can't mkdir %s", orig);
611 static void
612 xdg_init(void)
614 char *home, old_path[PATH_MAX];
615 struct stat info;
617 /* old path */
618 if ((home = getenv("HOME")) == NULL)
619 errx(1, "HOME is not defined");
620 join_path(old_path, home, "/.telescope", sizeof(old_path));
622 /* if ~/.telescope exists, use that instead of xdg dirs */
623 if (stat(old_path, &info) == 0 && S_ISDIR(info.st_mode)) {
624 join_path(config_path_base, home, "/.telescope",
625 sizeof(config_path_base));
626 join_path(data_path_base, home, "/.telescope",
627 sizeof(data_path_base));
628 join_path(cache_path_base, home, "/.telescope",
629 sizeof(cache_path_base));
630 return;
633 /* xdg paths */
634 getenv_default(xdg_config_base, "XDG_CONFIG_HOME", "/.config",
635 sizeof(xdg_config_base));
636 getenv_default(xdg_data_base, "XDG_DATA_HOME", "/.local/share",
637 sizeof(xdg_data_base));
638 getenv_default(xdg_cache_base, "XDG_CACHE_HOME", "/.cache",
639 sizeof(xdg_cache_base));
641 join_path(config_path_base, xdg_config_base, "/telescope",
642 sizeof(config_path_base));
643 join_path(data_path_base, xdg_data_base, "/telescope",
644 sizeof(data_path_base));
645 join_path(cache_path_base, xdg_cache_base, "/telescope",
646 sizeof(cache_path_base));
648 mkdirs(xdg_config_base, S_IRWXU);
649 mkdirs(xdg_data_base, S_IRWXU);
650 mkdirs(xdg_cache_base, S_IRWXU);
652 mkdirs(config_path_base, S_IRWXU);
653 mkdirs(data_path_base, S_IRWXU);
654 mkdirs(cache_path_base, S_IRWXU);
657 int
658 fs_init(void)
660 xdg_init();
662 join_path(config_path, config_path_base, "/config",
663 sizeof(config_path));
664 join_path(lockfile_path, cache_path_base, "/lock",
665 sizeof(lockfile_path));
666 join_path(bookmark_file, data_path_base, "/bookmarks.gmi",
667 sizeof(bookmark_file));
668 join_path(known_hosts_file, data_path_base, "/known_hosts",
669 sizeof(known_hosts_file));
670 join_path(known_hosts_tmp, cache_path_base,
671 "/known_hosts.tmp.XXXXXXXXXX", sizeof(known_hosts_tmp));
672 join_path(session_file, cache_path_base, "/session",
673 sizeof(session_file));
674 join_path(crashed_file, cache_path_base, "/crashed",
675 sizeof(crashed_file));
677 return 1;
680 /*
681 * Parse a line of the session file. The format is:
683 * URL [flags,...] [title]\n
684 */
685 static void
686 parse_session_line(char *line, const char **title, uint32_t *flags)
688 char *s, *t, *ap;
690 *title = "";
691 *flags = 0;
692 if ((s = strchr(line, ' ')) == NULL)
693 return;
695 *s++ = '\0';
697 if ((t = strchr(s, ' ')) != NULL) {
698 *t++ = '\0';
699 *title = t;
702 while ((ap = strsep(&s, ",")) != NULL) {
703 if (!strcmp(ap, "current"))
704 *flags |= TAB_CURRENT;
708 static inline void
709 sendtab(uint32_t flags, const char *uri, const char *title)
711 struct session_tab tab;
713 memset(&tab, 0, sizeof(tab));
714 tab.flags = flags;
716 if (strlcpy(tab.uri, uri, sizeof(tab.uri)) >= sizeof(tab.uri))
717 return;
719 /* don't worry about cached title truncation */
720 if (title != NULL)
721 strlcpy(tab.title, title, sizeof(tab.title));
723 fs_send_ui(IMSG_SESSION_TAB, 0, -1, &tab, sizeof(tab));
726 static inline void
727 sendhist(const char *uri, int future)
729 struct session_tab_hist sth;
731 memset(&sth, 0, sizeof(sth));
732 sth.future = future;
734 if (strlcpy(sth.uri, uri, sizeof(sth.uri)) >= sizeof(sth.uri))
735 return;
737 fs_send_ui(IMSG_SESSION_TAB_HIST, 0, -1, &sth, sizeof(sth));
740 static void
741 load_last_session(int fd, short event, void *d)
743 FILE *session;
744 uint32_t flags;
745 size_t linesize = 0;
746 ssize_t linelen;
747 int first_time = 0;
748 int future;
749 const char *title;
750 char *nl, *s, *line = NULL;
752 if ((session = fopen(session_file, "r")) == NULL) {
753 /* first time? */
754 first_time = 1;
755 goto end;
758 while ((linelen = getline(&line, &linesize, session)) != -1) {
759 if ((nl = strchr(line, '\n')) != NULL)
760 *nl = '\0';
762 if (*line == '<' || *line == '>') {
763 future = *line == '>';
764 s = line+1;
765 if (*s != ' ')
766 continue;
767 sendhist(++s, future);
768 } else {
769 parse_session_line(line, &title, &flags);
770 sendtab(flags, line, title);
774 fclose(session);
775 free(line);
777 if (last_time_crashed())
778 sendtab(TAB_CURRENT, "about:crash", NULL);
780 end:
781 fs_send_ui(IMSG_SESSION_END, 0, -1, &first_time, sizeof(first_time));
784 int
785 fs_main(void)
787 struct event ev;
788 struct timeval t = {0, 0};
790 setproctitle("fs");
792 fs_init();
794 event_init();
796 /* Setup pipe and event handler to the main process */
797 if ((iev_ui = malloc(sizeof(*iev_ui))) == NULL)
798 die();
799 imsg_init(&iev_ui->ibuf, 3);
800 iev_ui->handler = handle_dispatch_imsg;
801 iev_ui->events = EV_READ;
802 event_set(&iev_ui->ev, iev_ui->ibuf.fd, iev_ui->events,
803 iev_ui->handler, iev_ui);
804 event_add(&iev_ui->ev, NULL);
806 sandbox_fs_process();
808 /*
809 * Run load_last_session during the event loop, as soon as
810 * possible; it uses fs_send_ui so it can't be ran outside
811 * of event_dispatch.
812 */
813 evtimer_set(&ev, load_last_session, NULL);
814 evtimer_add(&ev, &t);
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 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 int
883 load_certs(struct ohash *h)
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 0;
895 while ((linelen = getline(&line, &linesize, f)) != -1) {
896 if ((e = calloc(1, sizeof(*e))) == NULL)
897 abort();
899 lineno++;
901 if (parse_khost_line(line, tmp)) {
902 strlcpy(e->domain, tmp[0], sizeof(e->domain));
903 strlcpy(e->hash, tmp[1], sizeof(e->hash));
905 e->verified = strtonum(tmp[2], 0, 1, &errstr);
906 if (errstr != NULL)
907 errx(1, "%s:%zu verification for %s is %s: %s",
908 known_hosts_file, lineno,
909 e->domain, errstr, tmp[2]);
910 tofu_add(h, e);
911 } else {
912 warnx("%s:%zu invalid entry",
913 known_hosts_file, lineno);
914 free(e);
918 free(line);
919 return ferror(f);