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>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <limits.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
35 #include "telescope.h"
36 #include "pages.h"
38 static void die(void) __attribute__((__noreturn__));
39 static void send_file(uint32_t, FILE *);
40 static void handle_get(struct imsg*, size_t);
41 static void handle_get_file(struct imsg*, size_t);
42 static void handle_quit(struct imsg*, size_t);
43 static void handle_bookmark_page(struct imsg*, size_t);
44 static void handle_save_cert(struct imsg*, size_t);
45 static void handle_update_cert(struct imsg*, size_t);
46 static void handle_file_open(struct imsg*, size_t);
47 static void handle_session_start(struct imsg*, size_t);
48 static void handle_session_tab(struct imsg*, size_t);
49 static void handle_session_tab_title(struct imsg*, size_t);
50 static void handle_session_end(struct imsg*, size_t);
51 static void handle_dispatch_imsg(int, short, void*);
52 static int fs_send_ui(int, uint32_t, int, const void *, uint16_t);
54 static struct imsgev *iev_ui;
55 static FILE *session;
57 static char base_path[PATH_MAX];
58 static char lockfile_path[PATH_MAX];
59 static char bookmark_file[PATH_MAX];
60 static char known_hosts_file[PATH_MAX], known_hosts_tmp[PATH_MAX];
61 static char crashed_file[PATH_MAX];
63 char session_file[PATH_MAX];
65 static imsg_handlerfn *handlers[] = {
66 [IMSG_GET] = handle_get,
67 [IMSG_GET_FILE] = handle_get_file,
68 [IMSG_QUIT] = handle_quit,
69 [IMSG_BOOKMARK_PAGE] = handle_bookmark_page,
70 [IMSG_SAVE_CERT] = handle_save_cert,
71 [IMSG_UPDATE_CERT] = handle_update_cert,
72 [IMSG_FILE_OPEN] = handle_file_open,
73 [IMSG_SESSION_START] = handle_session_start,
74 [IMSG_SESSION_TAB] = handle_session_tab,
75 [IMSG_SESSION_TAB_TITLE] = handle_session_tab_title,
76 [IMSG_SESSION_END] = handle_session_end,
77 };
79 static void __attribute__((__noreturn__))
80 die(void)
81 {
82 abort(); /* TODO */
83 }
85 static void
86 send_file(uint32_t peerid, FILE *f)
87 {
88 ssize_t r;
89 char buf[BUFSIZ];
91 for (;;) {
92 r = fread(buf, 1, sizeof(buf), f);
93 if (r != 0)
94 fs_send_ui(IMSG_BUF, peerid, -1, buf, r);
95 if (r != sizeof(buf))
96 break;
97 }
98 fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
99 fclose(f);
102 static void
103 handle_get(struct imsg *imsg, size_t datalen)
105 const char *bpath = "bookmarks.gmi";
106 char path[PATH_MAX];
107 FILE *f;
108 const char *data, *p;
109 size_t i;
110 struct page {
111 const char *name;
112 const char *path;
113 const uint8_t *data;
114 size_t len;
115 } pages[] = {
116 {"about", NULL, about_about, about_about_len},
117 {"blank", NULL, about_blank, about_blank_len},
118 {"bookmarks", bpath, bookmarks, bookmarks_len},
119 {"crash", NULL, about_crash, about_crash_len},
120 {"help", NULL, about_help, about_help_len},
121 {"license", NULL, about_license, about_license_len},
122 {"new", NULL, about_new, about_new_len},
123 }, *page = NULL;
125 data = imsg->data;
126 if (data[datalen-1] != '\0') /* make sure it's NUL-terminated */
127 die();
128 if ((data = strchr(data, ':')) == NULL)
129 goto notfound;
130 data++;
132 for (i = 0; i < sizeof(pages)/sizeof(pages[0]); ++i)
133 if (!strcmp(data, pages[i].name)) {
134 page = &pages[i];
135 break;
138 if (page == NULL)
139 goto notfound;
141 strlcpy(path, base_path, sizeof(path));
142 strlcat(path, "/", sizeof(path));
143 if (page->path != NULL)
144 strlcat(path, page->path, sizeof(path));
145 else {
146 strlcat(path, "pages/about_", sizeof(path));
147 strlcat(path, page->name, sizeof(path));
148 strlcat(path, ".gmi", sizeof(path));
151 if ((f = fopen(path, "r")) == NULL) {
152 fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1,
153 page->data, page->len);
154 fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1,
155 NULL, 0);
156 return;
159 send_file(imsg->hdr.peerid, f);
160 return;
162 notfound:
163 p = "# not found!\n";
164 fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, p, strlen(p));
165 fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
168 static inline void
169 send_hdr(uint32_t peerid, int code, const char *meta)
171 fs_send_ui(IMSG_GOT_CODE, peerid, -1, &code, sizeof(code));
172 fs_send_ui(IMSG_GOT_META, peerid, -1, meta, strlen(meta)+1);
175 static void
176 handle_get_file(struct imsg *imsg, size_t datalen)
178 struct mapping {
179 const char *ext;
180 const char *mime;
181 } ms[] = {
182 {"diff", "text/x-patch"},
183 {"gemini", "text/gemini"},
184 {"gmi", "text/gemini"},
185 {"markdown", "text/plain"},
186 {"md", "text/plain"},
187 {"patch", "text/x-patch"},
188 {"txt", "text/plain"},
189 {NULL, NULL},
190 }, *m;
191 FILE *f;
192 char *data, *dot;
193 const char *meta = NULL;
194 int code;
196 data = imsg->data;
197 data[datalen-1] = '\0';
199 if ((dot = strrchr(data, '.')) == NULL) {
200 send_hdr(imsg->hdr.peerid, 51,
201 "don't know how to visualize this file");
202 return;
204 dot++;
206 for (m = ms; m->ext != NULL; ++m) {
207 if (!strcmp(m->ext, dot)) {
208 meta = m->mime;
209 break;
213 if (meta == NULL) {
214 send_hdr(imsg->hdr.peerid, 51,
215 "don't know how to visualize this file");
216 return;
219 if ((f = fopen(data, "r")) == NULL) {
220 /*
221 * If errno is EDIR it would be nice to serve the
222 * directory listing. See how it's done in gmid.
223 */
224 send_hdr(imsg->hdr.peerid, 51, "can't open the file");
225 return;
228 send_hdr(imsg->hdr.peerid, 20, meta);
229 send_file(imsg->hdr.peerid, f);
232 static void
233 handle_quit(struct imsg *imsg, size_t datalen)
235 event_loopbreak();
238 static void
239 handle_bookmark_page(struct imsg *imsg, size_t datalen)
241 char *data;
242 int res;
243 FILE *f;
245 data = imsg->data;
246 if (data[datalen-1] != '\0')
247 die();
249 if ((f = fopen(bookmark_file, "a")) == NULL) {
250 res = errno;
251 goto end;
253 fprintf(f, "=> %s\n", data);
254 fclose(f);
256 res = 0;
257 end:
258 fs_send_ui(IMSG_BOOKMARK_OK, 0, -1, &res, sizeof(res));
261 static void
262 handle_save_cert(struct imsg *imsg, size_t datalen)
264 struct tofu_entry e;
265 FILE *f;
266 int res;
268 /* TODO: traverse the file to avoid duplications? */
270 if (datalen != sizeof(e))
271 die();
272 memcpy(&e, imsg->data, datalen);
274 if ((f = fopen(known_hosts_file, "a")) == NULL) {
275 res = errno;
276 goto end;
278 fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
279 fclose(f);
281 res = 0;
282 end:
283 fs_send_ui(IMSG_SAVE_CERT_OK, imsg->hdr.peerid, -1,
284 &res, sizeof(res));
287 static void
288 handle_update_cert(struct imsg *imsg, size_t datalen)
290 FILE *tmp, *f;
291 struct tofu_entry entry;
292 char sfn[PATH_MAX], *line = NULL, *t;
293 size_t l, linesize = 0;
294 ssize_t linelen;
295 int fd, e, res = 0;
297 if (datalen != sizeof(entry))
298 die();
299 memcpy(&entry, imsg->data, datalen);
301 strlcpy(sfn, known_hosts_tmp, sizeof(sfn));
302 if ((fd = mkstemp(sfn)) == -1 ||
303 (tmp = fdopen(fd, "w")) == NULL) {
304 if (fd != -1) {
305 unlink(sfn);
306 close(fd);
308 res = 0;
309 goto end;
312 if ((f = fopen(known_hosts_file, "r")) == NULL) {
313 unlink(sfn);
314 fclose(tmp);
315 res = 0;
316 goto end;
319 l = strlen(entry.domain);
320 while ((linelen = getline(&line, &linesize, f)) != -1) {
321 if ((t = strstr(line, entry.domain)) != NULL &&
322 (line[l] == ' ' || line[l] == '\t'))
323 continue;
324 /* line has a trailing \n */
325 fprintf(tmp, "%s", line);
327 fprintf(tmp, "%s %s %d\n", entry.domain, entry.hash, entry.verified);
329 free(line);
330 e = ferror(tmp);
332 fclose(tmp);
333 fclose(f);
335 if (e) {
336 unlink(sfn);
337 res = 0;
338 goto end;
341 res = rename(sfn, known_hosts_file) != -1;
343 end:
344 fs_send_ui(IMSG_UPDATE_CERT_OK, imsg->hdr.peerid, -1,
345 &res, sizeof(res));
348 static void
349 handle_file_open(struct imsg *imsg, size_t datalen)
351 char *path, *e;
352 int fd;
354 path = imsg->data;
355 if (path[datalen-1] != '\0')
356 die();
358 if ((fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644)) == -1) {
359 e = strerror(errno);
360 fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, -1,
361 e, strlen(e)+1);
362 } else
363 fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, fd,
364 NULL, 0);
367 static void
368 handle_session_start(struct imsg *imsg, size_t datalen)
370 if (datalen != 0)
371 die();
373 if ((session = fopen(session_file, "w")) == NULL)
374 die();
377 static void
378 handle_session_tab(struct imsg *imsg, size_t datalen)
380 char *url;
381 uint32_t flags;
383 if (session == NULL)
384 die();
386 flags = imsg->hdr.peerid;
387 url = imsg->data;
388 if (datalen == 0 || url[datalen-1] != '\0')
389 die();
390 fprintf(session, "%s", url);
392 if (flags & TAB_CURRENT)
393 fprintf(session, " current ");
394 else
395 fprintf(session, " - ");
398 static void
399 handle_session_tab_title(struct imsg *imsg, size_t datalen)
401 const char *title;
403 title = imsg->data;
404 if (title == NULL) {
405 datalen = 1;
406 title = "";
409 if (title[datalen-1] != '\0')
410 die();
412 fprintf(session, "%s\n", title);
415 static void
416 handle_session_end(struct imsg *imsg, size_t datalen)
418 if (session == NULL)
419 die();
420 fclose(session);
421 session = NULL;
424 static void
425 handle_dispatch_imsg(int fd, short ev, void *d)
427 struct imsgev *iev = d;
428 int e;
430 if (dispatch_imsg(iev, ev, handlers, sizeof(handlers)) == -1) {
431 /*
432 * This should leave a ~/.telescope/crashed file to
433 * trigger about:crash on next run. Unfortunately, if
434 * the main process dies the fs sticks around and
435 * doesn't notice that the fd was closed. Why EV_READ
436 * is not triggered when a fd is closed on the other end?
437 */
438 e = errno;
439 if ((fd = open(crashed_file, O_CREAT|O_TRUNC|O_WRONLY, 0600))
440 == -1)
441 err(1, "open");
442 close(fd);
443 errx(1, "connection closed: %s", strerror(e));
447 static int
448 fs_send_ui(int type, uint32_t peerid, int fd, const void *data,
449 uint16_t datalen)
451 return imsg_compose_event(iev_ui, type, peerid, 0, fd,
452 data, datalen);
455 int
456 fs_init(void)
458 strlcpy(base_path, getenv("HOME"), sizeof(base_path));
459 strlcat(base_path, "/.telescope", sizeof(base_path));
460 mkdir(base_path, 0700);
462 strlcpy(lockfile_path, base_path, sizeof(lockfile_path));
463 strlcat(lockfile_path, "/lock", sizeof(lockfile_path));
465 strlcpy(bookmark_file, base_path, sizeof(bookmark_file));
466 strlcat(bookmark_file, "/bookmarks.gmi", sizeof(bookmark_file));
468 strlcpy(known_hosts_file, base_path, sizeof(known_hosts_file));
469 strlcat(known_hosts_file, "/known_hosts", sizeof(known_hosts_file));
471 strlcpy(known_hosts_tmp, base_path, sizeof(known_hosts_tmp));
472 strlcat(known_hosts_tmp, "/known_hosts.tmp.XXXXXXXXXX",
473 sizeof(known_hosts_file));
475 strlcpy(session_file, base_path, sizeof(session_file));
476 strlcat(session_file, "/session", sizeof(session_file));
478 strlcpy(crashed_file, base_path, sizeof(crashed_file));
479 strlcat(crashed_file, "/crashed", sizeof(crashed_file));
481 return 1;
484 int
485 fs_main(void)
487 setproctitle("fs");
489 fs_init();
491 event_init();
493 /* Setup pipe and event handler to the main process */
494 if ((iev_ui = malloc(sizeof(*iev_ui))) == NULL)
495 die();
496 imsg_init(&iev_ui->ibuf, 3);
497 iev_ui->handler = handle_dispatch_imsg;
498 iev_ui->events = EV_READ;
499 event_set(&iev_ui->ev, iev_ui->ibuf.fd, iev_ui->events,
500 iev_ui->handler, iev_ui);
501 event_add(&iev_ui->ev, NULL);
503 sandbox_fs_process();
505 event_dispatch();
506 return 0;
511 int
512 last_time_crashed(void)
514 int fd;
516 if ((fd = open(crashed_file, O_RDONLY)) == -1)
517 return 0;
519 close(fd);
520 unlink(crashed_file);
521 return 1;
524 int
525 lock_session(void)
527 struct flock lock;
528 int fd;
530 if ((fd = open(lockfile_path, O_WRONLY|O_CREAT, 0600)) == -1)
531 return -1;
533 lock.l_start = 0;
534 lock.l_len = 0;
535 lock.l_type = F_WRLCK;
536 lock.l_whence = SEEK_SET;
538 if (fcntl(fd, F_SETLK, &lock) == -1) {
539 close(fd);
540 return -1;
543 return fd;
546 static int
547 parse_khost_line(char *line, char *tmp[3])
549 char **ap;
551 for (ap = tmp; ap < &tmp[3] &&
552 (*ap = strsep(&line, " \t\n")) != NULL;) {
553 if (**ap != '\0')
554 ap++;
557 return ap == &tmp[3] && *line == '\0';
560 int
561 load_certs(struct ohash *h)
563 char *tmp[3], *line = NULL;
564 const char *errstr;
565 size_t lineno = 0, linesize = 0;
566 ssize_t linelen;
567 FILE *f;
568 struct tofu_entry *e;
570 if ((f = fopen(known_hosts_file, "r")) == NULL)
571 return 0;
573 while ((linelen = getline(&line, &linesize, f)) != -1) {
574 if ((e = calloc(1, sizeof(*e))) == NULL)
575 abort();
577 lineno++;
579 if (parse_khost_line(line, tmp)) {
580 strlcpy(e->domain, tmp[0], sizeof(e->domain));
581 strlcpy(e->hash, tmp[1], sizeof(e->hash));
583 e->verified = strtonum(tmp[2], 0, 1, &errstr);
584 if (errstr != NULL)
585 errx(1, "%s:%zu verification for %s is %s: %s",
586 known_hosts_file, lineno,
587 e->domain, errstr, tmp[2]);
588 tofu_add(h, e);
589 } else {
590 warnx("%s:%zu invalid entry",
591 known_hosts_file, lineno);
592 free(e);
596 free(line);
597 return ferror(f);