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 the data in ~/.telescope
19 *
20 * TODO: add some form of locking on the files
21 */
23 #include "compat.h"
25 #include <sys/stat.h>
26 #include <sys/types.h>
28 #include <dirent.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <limits.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
37 #include "telescope.h"
38 #include "pages.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 void handle_get_file(struct imsg*, size_t);
44 static void handle_quit(struct imsg*, size_t);
45 static void handle_bookmark_page(struct imsg*, size_t);
46 static void handle_save_cert(struct imsg*, size_t);
47 static void handle_update_cert(struct imsg*, size_t);
48 static void handle_file_open(struct imsg*, size_t);
49 static void handle_session_start(struct imsg*, size_t);
50 static void handle_session_tab(struct imsg*, size_t);
51 static void handle_session_tab_title(struct imsg*, size_t);
52 static void handle_session_end(struct imsg*, size_t);
53 static void handle_dispatch_imsg(int, short, void*);
54 static int fs_send_ui(int, uint32_t, int, const void *, uint16_t);
56 static struct imsgev *iev_ui;
57 static FILE *session;
59 static char base_path[PATH_MAX];
60 static char lockfile_path[PATH_MAX];
61 static char bookmark_file[PATH_MAX];
62 static char known_hosts_file[PATH_MAX], known_hosts_tmp[PATH_MAX];
63 static char crashed_file[PATH_MAX];
65 char session_file[PATH_MAX];
67 static imsg_handlerfn *handlers[] = {
68 [IMSG_GET] = handle_get,
69 [IMSG_GET_FILE] = handle_get_file,
70 [IMSG_QUIT] = handle_quit,
71 [IMSG_BOOKMARK_PAGE] = handle_bookmark_page,
72 [IMSG_SAVE_CERT] = handle_save_cert,
73 [IMSG_UPDATE_CERT] = handle_update_cert,
74 [IMSG_FILE_OPEN] = handle_file_open,
75 [IMSG_SESSION_START] = handle_session_start,
76 [IMSG_SESSION_TAB] = handle_session_tab,
77 [IMSG_SESSION_TAB_TITLE] = handle_session_tab_title,
78 [IMSG_SESSION_END] = handle_session_end,
79 };
81 static void __attribute__((__noreturn__))
82 die(void)
83 {
84 abort(); /* TODO */
85 }
87 static void
88 send_file(uint32_t peerid, FILE *f)
89 {
90 ssize_t r;
91 char buf[BUFSIZ];
93 for (;;) {
94 r = fread(buf, 1, sizeof(buf), f);
95 if (r != 0)
96 fs_send_ui(IMSG_BUF, peerid, -1, buf, r);
97 if (r != sizeof(buf))
98 break;
99 }
100 fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
101 fclose(f);
104 static void
105 handle_get(struct imsg *imsg, size_t datalen)
107 const char *bpath = "bookmarks.gmi";
108 char path[PATH_MAX];
109 FILE *f;
110 const char *data, *p;
111 size_t i;
112 struct page {
113 const char *name;
114 const char *path;
115 const uint8_t *data;
116 size_t len;
117 } pages[] = {
118 {"about", NULL, about_about, about_about_len},
119 {"blank", NULL, about_blank, about_blank_len},
120 {"bookmarks", bpath, bookmarks, bookmarks_len},
121 {"crash", NULL, about_crash, about_crash_len},
122 {"help", NULL, about_help, about_help_len},
123 {"license", NULL, about_license, about_license_len},
124 {"new", NULL, about_new, about_new_len},
125 }, *page = NULL;
127 data = imsg->data;
128 if (data[datalen-1] != '\0') /* make sure it's NUL-terminated */
129 die();
130 if ((data = strchr(data, ':')) == NULL)
131 goto notfound;
132 data++;
134 for (i = 0; i < sizeof(pages)/sizeof(pages[0]); ++i)
135 if (!strcmp(data, pages[i].name)) {
136 page = &pages[i];
137 break;
140 if (page == NULL)
141 goto notfound;
143 strlcpy(path, base_path, sizeof(path));
144 strlcat(path, "/", sizeof(path));
145 if (page->path != NULL)
146 strlcat(path, page->path, sizeof(path));
147 else {
148 strlcat(path, "pages/about_", sizeof(path));
149 strlcat(path, page->name, sizeof(path));
150 strlcat(path, ".gmi", sizeof(path));
153 if ((f = fopen(path, "r")) == NULL) {
154 fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1,
155 page->data, page->len);
156 fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1,
157 NULL, 0);
158 return;
161 send_file(imsg->hdr.peerid, f);
162 return;
164 notfound:
165 p = "# not found!\n";
166 fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, p, strlen(p));
167 fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
170 static inline void
171 send_hdr(uint32_t peerid, int code, const char *meta)
173 fs_send_ui(IMSG_GOT_CODE, peerid, -1, &code, sizeof(code));
174 fs_send_ui(IMSG_GOT_META, peerid, -1, meta, strlen(meta)+1);
177 static inline const char *
178 file_type(const char *path)
180 struct mapping {
181 const char *ext;
182 const char *mime;
183 } ms[] = {
184 {"diff", "text/x-patch"},
185 {"gemini", "text/gemini"},
186 {"gmi", "text/gemini"},
187 {"markdown", "text/plain"},
188 {"md", "text/plain"},
189 {"patch", "text/x-patch"},
190 {"txt", "text/plain"},
191 {NULL, NULL},
192 }, *m;
193 char *dot;
195 if ((dot = strrchr(path, '.')) == NULL)
196 return NULL;
198 dot++;
200 for (m = ms; m->ext != NULL; ++m)
201 if (!strcmp(m->ext, dot))
202 return m->mime;
204 return NULL;
207 static inline void
208 send_dir(uint32_t peerid, const char *path)
210 struct dirent **names;
211 struct evbuffer *ev;
212 int i, len;
214 if ((ev = evbuffer_new()) == NULL ||
215 (len = scandir(path, &names, NULL, alphasort)) == -1) {
216 evbuffer_free(ev);
217 send_hdr(peerid, 40, "failure reading the directory");
218 return;
221 evbuffer_add_printf(ev, "# Index of %s\n\n", path);
222 for (i = 0; i < len; ++i) {
223 evbuffer_add_printf(ev, "=> %s", names[i]->d_name);
224 if (names[i]->d_type == DT_DIR)
225 evbuffer_add(ev, "/", 1);
226 evbuffer_add(ev, "\n", 1);
229 send_hdr(peerid, 20, "text/gemini");
230 fs_send_ui(IMSG_BUF, peerid, -1,
231 EVBUFFER_DATA(ev), EVBUFFER_LENGTH(ev));
232 fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
234 evbuffer_free(ev);
235 free(names);
238 static void
239 handle_get_file(struct imsg *imsg, size_t datalen)
241 struct stat sb;
242 FILE *f;
243 char *data;
244 const char *meta = NULL;
246 data = imsg->data;
247 data[datalen-1] = '\0';
249 if ((f = fopen(data, "r")) == NULL) {
250 send_hdr(imsg->hdr.peerid, 51, "can't open the file");
251 return;
254 if (fstat(fileno(f), &sb) == -1) {
255 send_hdr(imsg->hdr.peerid, 40, "fstat failed");
256 return;
259 if (S_ISDIR(sb.st_mode)) {
260 fclose(f);
261 send_dir(imsg->hdr.peerid, data);
262 return;
265 if ((meta = file_type(data)) == NULL) {
266 fclose(f);
267 send_hdr(imsg->hdr.peerid, 51,
268 "don't know how to visualize this file");
269 return;
272 send_hdr(imsg->hdr.peerid, 20, meta);
273 send_file(imsg->hdr.peerid, f);
276 static void
277 handle_quit(struct imsg *imsg, size_t datalen)
279 event_loopbreak();
282 static void
283 handle_bookmark_page(struct imsg *imsg, size_t datalen)
285 char *data;
286 int res;
287 FILE *f;
289 data = imsg->data;
290 if (data[datalen-1] != '\0')
291 die();
293 if ((f = fopen(bookmark_file, "a")) == NULL) {
294 res = errno;
295 goto end;
297 fprintf(f, "=> %s\n", data);
298 fclose(f);
300 res = 0;
301 end:
302 fs_send_ui(IMSG_BOOKMARK_OK, 0, -1, &res, sizeof(res));
305 static void
306 handle_save_cert(struct imsg *imsg, size_t datalen)
308 struct tofu_entry e;
309 FILE *f;
310 int res;
312 /* TODO: traverse the file to avoid duplications? */
314 if (datalen != sizeof(e))
315 die();
316 memcpy(&e, imsg->data, datalen);
318 if ((f = fopen(known_hosts_file, "a")) == NULL) {
319 res = errno;
320 goto end;
322 fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
323 fclose(f);
325 res = 0;
326 end:
327 fs_send_ui(IMSG_SAVE_CERT_OK, imsg->hdr.peerid, -1,
328 &res, sizeof(res));
331 static void
332 handle_update_cert(struct imsg *imsg, size_t datalen)
334 FILE *tmp, *f;
335 struct tofu_entry entry;
336 char sfn[PATH_MAX], *line = NULL, *t;
337 size_t l, linesize = 0;
338 ssize_t linelen;
339 int fd, e, res = 0;
341 if (datalen != sizeof(entry))
342 die();
343 memcpy(&entry, imsg->data, datalen);
345 strlcpy(sfn, known_hosts_tmp, sizeof(sfn));
346 if ((fd = mkstemp(sfn)) == -1 ||
347 (tmp = fdopen(fd, "w")) == NULL) {
348 if (fd != -1) {
349 unlink(sfn);
350 close(fd);
352 res = 0;
353 goto end;
356 if ((f = fopen(known_hosts_file, "r")) == NULL) {
357 unlink(sfn);
358 fclose(tmp);
359 res = 0;
360 goto end;
363 l = strlen(entry.domain);
364 while ((linelen = getline(&line, &linesize, f)) != -1) {
365 if ((t = strstr(line, entry.domain)) != NULL &&
366 (line[l] == ' ' || line[l] == '\t'))
367 continue;
368 /* line has a trailing \n */
369 fprintf(tmp, "%s", line);
371 fprintf(tmp, "%s %s %d\n", entry.domain, entry.hash, entry.verified);
373 free(line);
374 e = ferror(tmp);
376 fclose(tmp);
377 fclose(f);
379 if (e) {
380 unlink(sfn);
381 res = 0;
382 goto end;
385 res = rename(sfn, known_hosts_file) != -1;
387 end:
388 fs_send_ui(IMSG_UPDATE_CERT_OK, imsg->hdr.peerid, -1,
389 &res, sizeof(res));
392 static void
393 handle_file_open(struct imsg *imsg, size_t datalen)
395 char *path, *e;
396 int fd;
398 path = imsg->data;
399 if (path[datalen-1] != '\0')
400 die();
402 if ((fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644)) == -1) {
403 e = strerror(errno);
404 fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, -1,
405 e, strlen(e)+1);
406 } else
407 fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, fd,
408 NULL, 0);
411 static void
412 handle_session_start(struct imsg *imsg, size_t datalen)
414 if (datalen != 0)
415 die();
417 if ((session = fopen(session_file, "w")) == NULL)
418 die();
421 static void
422 handle_session_tab(struct imsg *imsg, size_t datalen)
424 char *url;
425 uint32_t flags;
427 if (session == NULL)
428 die();
430 flags = imsg->hdr.peerid;
431 url = imsg->data;
432 if (datalen == 0 || url[datalen-1] != '\0')
433 die();
434 fprintf(session, "%s", url);
436 if (flags & TAB_CURRENT)
437 fprintf(session, " current ");
438 else
439 fprintf(session, " - ");
442 static void
443 handle_session_tab_title(struct imsg *imsg, size_t datalen)
445 const char *title;
447 title = imsg->data;
448 if (title == NULL) {
449 datalen = 1;
450 title = "";
453 if (title[datalen-1] != '\0')
454 die();
456 fprintf(session, "%s\n", title);
459 static void
460 handle_session_end(struct imsg *imsg, size_t datalen)
462 if (session == NULL)
463 die();
464 fclose(session);
465 session = NULL;
468 static void
469 handle_dispatch_imsg(int fd, short ev, void *d)
471 struct imsgev *iev = d;
472 int e;
474 if (dispatch_imsg(iev, ev, handlers, sizeof(handlers)) == -1) {
475 /*
476 * This should leave a ~/.telescope/crashed file to
477 * trigger about:crash on next run. Unfortunately, if
478 * the main process dies the fs sticks around and
479 * doesn't notice that the fd was closed. Why EV_READ
480 * is not triggered when a fd is closed on the other end?
481 */
482 e = errno;
483 if ((fd = open(crashed_file, O_CREAT|O_TRUNC|O_WRONLY, 0600))
484 == -1)
485 err(1, "open");
486 close(fd);
487 errx(1, "connection closed: %s", strerror(e));
491 static int
492 fs_send_ui(int type, uint32_t peerid, int fd, const void *data,
493 uint16_t datalen)
495 return imsg_compose_event(iev_ui, type, peerid, 0, fd,
496 data, datalen);
499 int
500 fs_init(void)
502 strlcpy(base_path, getenv("HOME"), sizeof(base_path));
503 strlcat(base_path, "/.telescope", sizeof(base_path));
504 mkdir(base_path, 0700);
506 strlcpy(lockfile_path, base_path, sizeof(lockfile_path));
507 strlcat(lockfile_path, "/lock", sizeof(lockfile_path));
509 strlcpy(bookmark_file, base_path, sizeof(bookmark_file));
510 strlcat(bookmark_file, "/bookmarks.gmi", sizeof(bookmark_file));
512 strlcpy(known_hosts_file, base_path, sizeof(known_hosts_file));
513 strlcat(known_hosts_file, "/known_hosts", sizeof(known_hosts_file));
515 strlcpy(known_hosts_tmp, base_path, sizeof(known_hosts_tmp));
516 strlcat(known_hosts_tmp, "/known_hosts.tmp.XXXXXXXXXX",
517 sizeof(known_hosts_file));
519 strlcpy(session_file, base_path, sizeof(session_file));
520 strlcat(session_file, "/session", sizeof(session_file));
522 strlcpy(crashed_file, base_path, sizeof(crashed_file));
523 strlcat(crashed_file, "/crashed", sizeof(crashed_file));
525 return 1;
528 int
529 fs_main(void)
531 setproctitle("fs");
533 fs_init();
535 event_init();
537 /* Setup pipe and event handler to the main process */
538 if ((iev_ui = malloc(sizeof(*iev_ui))) == NULL)
539 die();
540 imsg_init(&iev_ui->ibuf, 3);
541 iev_ui->handler = handle_dispatch_imsg;
542 iev_ui->events = EV_READ;
543 event_set(&iev_ui->ev, iev_ui->ibuf.fd, iev_ui->events,
544 iev_ui->handler, iev_ui);
545 event_add(&iev_ui->ev, NULL);
547 sandbox_fs_process();
549 event_dispatch();
550 return 0;
555 int
556 last_time_crashed(void)
558 int fd;
560 if ((fd = open(crashed_file, O_RDONLY)) == -1)
561 return 0;
563 close(fd);
564 unlink(crashed_file);
565 return 1;
568 int
569 lock_session(void)
571 struct flock lock;
572 int fd;
574 if ((fd = open(lockfile_path, O_WRONLY|O_CREAT, 0600)) == -1)
575 return -1;
577 lock.l_start = 0;
578 lock.l_len = 0;
579 lock.l_type = F_WRLCK;
580 lock.l_whence = SEEK_SET;
582 if (fcntl(fd, F_SETLK, &lock) == -1) {
583 close(fd);
584 return -1;
587 return fd;
590 static int
591 parse_khost_line(char *line, char *tmp[3])
593 char **ap;
595 for (ap = tmp; ap < &tmp[3] &&
596 (*ap = strsep(&line, " \t\n")) != NULL;) {
597 if (**ap != '\0')
598 ap++;
601 return ap == &tmp[3] && *line == '\0';
604 int
605 load_certs(struct ohash *h)
607 char *tmp[3], *line = NULL;
608 const char *errstr;
609 size_t lineno = 0, linesize = 0;
610 ssize_t linelen;
611 FILE *f;
612 struct tofu_entry *e;
614 if ((f = fopen(known_hosts_file, "r")) == NULL)
615 return 0;
617 while ((linelen = getline(&line, &linesize, f)) != -1) {
618 if ((e = calloc(1, sizeof(*e))) == NULL)
619 abort();
621 lineno++;
623 if (parse_khost_line(line, tmp)) {
624 strlcpy(e->domain, tmp[0], sizeof(e->domain));
625 strlcpy(e->hash, tmp[1], sizeof(e->hash));
627 e->verified = strtonum(tmp[2], 0, 1, &errstr);
628 if (errstr != NULL)
629 errx(1, "%s:%zu verification for %s is %s: %s",
630 known_hosts_file, lineno,
631 e->domain, errstr, tmp[2]);
632 tofu_add(h, e);
633 } else {
634 warnx("%s:%zu invalid entry",
635 known_hosts_file, lineno);
636 free(e);
640 free(line);
641 return ferror(f);