2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
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.
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.
18 * Handles config and runtime files
24 #include <sys/types.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_title(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;
66 * Note: these are empty if ~/.telescope exists, use *_path_base
69 static char xdg_config_base[PATH_MAX];
70 static char xdg_data_base[PATH_MAX];
71 static char xdg_cache_base[PATH_MAX];
74 * Where to store user data. These are all equal to ~/.telescope if
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_TITLE] = handle_session_tab_title,
99 [IMSG_SESSION_END] = handle_session_end,
102 static void __attribute__((__noreturn__))
109 send_file(uint32_t peerid, FILE *f)
115 r = fread(buf, 1, sizeof(buf), f);
117 fs_send_ui(IMSG_BUF, peerid, -1, buf, r);
118 if (r != sizeof(buf))
121 fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
126 handle_get(struct imsg *imsg, size_t datalen)
128 const char *bpath = "bookmarks.gmi";
131 const char *data, *p;
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},
149 if (data[datalen-1] != '\0') /* make sure it's NUL-terminated */
151 if ((data = strchr(data, ':')) == NULL)
155 for (i = 0; i < sizeof(pages)/sizeof(pages[0]); ++i)
156 if (!strcmp(data, pages[i].name)) {
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));
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,
182 send_file(imsg->hdr.peerid, f);
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);
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);
199 send_errno(uint32_t peerid, int code, const char *str, int no)
203 if (asprintf(&s, "%s: %s", str, strerror(no)) == -1)
206 send_hdr(peerid, code, s == NULL ? str : s);
210 static inline const char *
211 file_type(const char *path)
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"},
228 if ((dot = strrchr(path, '.')) == NULL)
233 for (m = ms; m->ext != NULL; ++m)
234 if (!strcmp(m->ext, dot))
241 select_non_dot(const struct dirent *d)
243 return strcmp(d->d_name, ".");
247 select_non_dotdot(const struct dirent *d)
249 return strcmp(d->d_name, ".") && strcmp(d->d_name, "..");
253 send_dir(uint32_t peerid, const char *path)
255 struct dirent **names;
258 int (*selector)(const struct dirent *) = select_non_dot;
261 if (!has_suffix(path, "/")) {
262 if (asprintf(&s, "%s/", path) == -1)
264 send_hdr(peerid, 30, s);
269 if (!strcmp(path, "/"))
270 selector = select_non_dotdot;
272 if ((ev = evbuffer_new()) == NULL ||
273 (len = scandir(path, &names, selector, alphasort)) == -1) {
276 send_errno(peerid, 40, "failure reading the directory", no);
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);
298 handle_get_file(struct imsg *imsg, size_t datalen)
303 const char *meta = NULL;
306 data[datalen-1] = '\0';
308 if ((f = fopen(data, "r")) == NULL) {
309 send_errno(imsg->hdr.peerid, 51, "can't open", errno);
313 if (fstat(fileno(f), &sb) == -1) {
314 send_errno(imsg->hdr.peerid, 40, "fstat", errno);
318 if (S_ISDIR(sb.st_mode)) {
320 send_dir(imsg->hdr.peerid, data);
324 if ((meta = file_type(data)) == NULL) {
326 send_hdr(imsg->hdr.peerid, 51,
327 "don't know how to visualize this file");
331 send_hdr(imsg->hdr.peerid, 20, meta);
332 send_file(imsg->hdr.peerid, f);
336 handle_quit(struct imsg *imsg, size_t datalen)
339 unlink(crashed_file);
345 handle_bookmark_page(struct imsg *imsg, size_t datalen)
352 if (data[datalen-1] != '\0')
355 if ((f = fopen(bookmark_file, "a")) == NULL) {
359 fprintf(f, "=> %s\n", data);
364 fs_send_ui(IMSG_BOOKMARK_OK, 0, -1, &res, sizeof(res));
368 handle_save_cert(struct imsg *imsg, size_t datalen)
374 /* TODO: traverse the file to avoid duplications? */
376 if (datalen != sizeof(e))
378 memcpy(&e, imsg->data, datalen);
380 if ((f = fopen(known_hosts_file, "a")) == NULL) {
384 fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
389 fs_send_ui(IMSG_SAVE_CERT_OK, imsg->hdr.peerid, -1,
394 handle_update_cert(struct imsg *imsg, size_t datalen)
397 struct tofu_entry entry;
398 char sfn[PATH_MAX], *line = NULL, *t;
399 size_t l, linesize = 0;
403 if (datalen != sizeof(entry))
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) {
418 if ((f = fopen(known_hosts_file, "r")) == NULL) {
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'))
430 /* line has a trailing \n */
431 fprintf(tmp, "%s", line);
433 fprintf(tmp, "%s %s %d\n", entry.domain, entry.hash, entry.verified);
447 res = rename(sfn, known_hosts_file) != -1;
450 fs_send_ui(IMSG_UPDATE_CERT_OK, imsg->hdr.peerid, -1,
455 handle_file_open(struct imsg *imsg, size_t datalen)
461 if (path[datalen-1] != '\0')
464 if ((fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644)) == -1) {
466 fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, -1,
469 fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, fd,
474 handle_session_start(struct imsg *imsg, size_t datalen)
479 if ((session = fopen(session_file, "w")) == NULL)
484 handle_session_tab(struct imsg *imsg, size_t datalen)
492 flags = imsg->hdr.peerid;
494 if (datalen == 0 || url[datalen-1] != '\0')
496 fprintf(session, "%s", url);
498 if (flags & TAB_CURRENT)
499 fprintf(session, " current ");
501 fprintf(session, " - ");
505 handle_session_tab_title(struct imsg *imsg, size_t datalen)
515 if (title[datalen-1] != '\0')
518 fprintf(session, "%s\n", title);
522 handle_session_end(struct imsg *imsg, size_t datalen)
531 handle_dispatch_imsg(int fd, short ev, void *d)
533 struct imsgev *iev = d;
536 if (dispatch_imsg(iev, ev, handlers, sizeof(handlers)) == -1) {
538 * This should leave a ~/.cache/telescope/crashed file to
539 * trigger about:crash on next run. Unfortunately, if
540 * the main process dies the fs sticks around and
541 * doesn't notice that the fd was closed. Why EV_READ
542 * is not triggered when a fd is closed on the other end?
545 if ((fd = open(crashed_file, O_CREAT|O_TRUNC|O_WRONLY, 0600))
549 errx(1, "connection closed: %s", strerror(e));
554 fs_send_ui(int type, uint32_t peerid, int fd, const void *data,
557 return imsg_compose_event(iev_ui, type, peerid, 0, fd,
562 join_path(char *buf, const char *lhs, const char *rhs, size_t buflen)
564 strlcpy(buf, lhs, buflen);
565 return strlcat(buf, rhs, buflen);
569 getenv_default(char *buf, const char *name, const char *def, size_t buflen)
574 if ((home = getenv("HOME")) == NULL)
575 errx(1, "HOME is not defined");
577 if ((env = getenv(name)) != NULL)
578 ret = strlcpy(buf, env, buflen);
580 ret = join_path(buf, home, def, buflen);
583 errx(1, "buffer too small for %s", name);
587 mkdirs(const char *path, mode_t mode)
589 char copy[PATH_MAX+1], orig[PATH_MAX+1], *parent;
591 strlcpy(copy, path, sizeof(copy));
592 strlcpy(orig, path, sizeof(orig));
593 parent = dirname(copy);
594 if (!strcmp(parent, "/"))
596 mkdirs(parent, mode);
598 if (mkdir(orig, mode) != 0) {
601 err(1, "can't mkdir %s", orig);
608 char *home, old_path[PATH_MAX];
612 if ((home = getenv("HOME")) == NULL)
613 errx(1, "HOME is not defined");
614 join_path(old_path, home, "/.telescope", sizeof(old_path));
616 /* if ~/.telescope exists, use that instead of xdg dirs */
617 if (stat(old_path, &info) == 0 && S_ISDIR(info.st_mode)) {
618 join_path(config_path_base, home, "/.telescope",
619 sizeof(config_path_base));
620 join_path(data_path_base, home, "/.telescope",
621 sizeof(data_path_base));
622 join_path(cache_path_base, home, "/.telescope",
623 sizeof(cache_path_base));
628 getenv_default(xdg_config_base, "XDG_CONFIG_HOME", "/.config",
629 sizeof(xdg_config_base));
630 getenv_default(xdg_data_base, "XDG_DATA_HOME", "/.local/share",
631 sizeof(xdg_data_base));
632 getenv_default(xdg_cache_base, "XDG_CACHE_HOME", "/.cache",
633 sizeof(xdg_cache_base));
635 join_path(config_path_base, xdg_config_base, "/telescope",
636 sizeof(config_path_base));
637 join_path(data_path_base, xdg_data_base, "/telescope",
638 sizeof(data_path_base));
639 join_path(cache_path_base, xdg_cache_base, "/telescope",
640 sizeof(cache_path_base));
642 mkdirs(xdg_config_base, S_IRWXU);
643 mkdirs(xdg_data_base, S_IRWXU);
644 mkdirs(xdg_cache_base, S_IRWXU);
646 mkdirs(config_path_base, S_IRWXU);
647 mkdirs(data_path_base, S_IRWXU);
648 mkdirs(cache_path_base, S_IRWXU);
656 join_path(config_path, config_path_base, "/config",
657 sizeof(config_path));
658 join_path(lockfile_path, cache_path_base, "/lock",
659 sizeof(lockfile_path));
660 join_path(bookmark_file, data_path_base, "/bookmarks.gmi",
661 sizeof(bookmark_file));
662 join_path(known_hosts_file, data_path_base, "/known_hosts",
663 sizeof(known_hosts_file));
664 join_path(known_hosts_tmp, cache_path_base,
665 "/known_hosts.tmp.XXXXXXXXXX", sizeof(known_hosts_tmp));
666 join_path(session_file, cache_path_base, "/session",
667 sizeof(session_file));
668 join_path(crashed_file, cache_path_base, "/crashed",
669 sizeof(crashed_file));
683 /* Setup pipe and event handler to the main process */
684 if ((iev_ui = malloc(sizeof(*iev_ui))) == NULL)
686 imsg_init(&iev_ui->ibuf, 3);
687 iev_ui->handler = handle_dispatch_imsg;
688 iev_ui->events = EV_READ;
689 event_set(&iev_ui->ev, iev_ui->ibuf.fd, iev_ui->events,
690 iev_ui->handler, iev_ui);
691 event_add(&iev_ui->ev, NULL);
693 sandbox_fs_process();
702 * Check if the last time telescope crashed. The check is done by
703 * looking at `crashed_file': if it exists then last time we crashed.
704 * Then, while here, touch the file too. During IMSG_QUIT we'll
708 last_time_crashed(void)
715 if (unlink(crashed_file) == -1 && errno == ENOENT)
718 if ((fd = open(crashed_file, O_CREAT|O_WRONLY, 0600)) == -1)
731 if ((fd = open(lockfile_path, O_WRONLY|O_CREAT, 0600)) == -1)
736 lock.l_type = F_WRLCK;
737 lock.l_whence = SEEK_SET;
739 if (fcntl(fd, F_SETLK, &lock) == -1) {
748 parse_khost_line(char *line, char *tmp[3])
752 for (ap = tmp; ap < &tmp[3] &&
753 (*ap = strsep(&line, " \t\n")) != NULL;) {
758 return ap == &tmp[3] && *line == '\0';
762 load_certs(struct ohash *h)
764 char *tmp[3], *line = NULL;
766 size_t lineno = 0, linesize = 0;
769 struct tofu_entry *e;
771 if ((f = fopen(known_hosts_file, "r")) == NULL)
774 while ((linelen = getline(&line, &linesize, f)) != -1) {
775 if ((e = calloc(1, sizeof(*e))) == NULL)
780 if (parse_khost_line(line, tmp)) {
781 strlcpy(e->domain, tmp[0], sizeof(e->domain));
782 strlcpy(e->hash, tmp[1], sizeof(e->hash));
784 e->verified = strtonum(tmp[2], 0, 1, &errstr);
786 errx(1, "%s:%zu verification for %s is %s: %s",
787 known_hosts_file, lineno,
788 e->domain, errstr, tmp[2]);
791 warnx("%s:%zu invalid entry",
792 known_hosts_file, lineno);