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 handle_get(struct imsg*, size_t);
40 static void handle_quit(struct imsg*, size_t);
41 static void handle_bookmark_page(struct imsg*, size_t);
42 static void handle_save_cert(struct imsg*, size_t);
43 static void handle_update_cert(struct imsg*, size_t);
44 static void handle_file_open(struct imsg*, size_t);
45 static void handle_session_start(struct imsg*, size_t);
46 static void handle_session_tab(struct imsg*, size_t);
47 static void handle_session_tab_title(struct imsg*, size_t);
48 static void handle_session_end(struct imsg*, size_t);
49 static void handle_dispatch_imsg(int, short, void*);
50 static int fs_send_ui(int, uint32_t, int, const void *, uint16_t);
52 static struct imsgev *iev_ui;
53 static FILE *session;
55 static char base_path[PATH_MAX];
56 static char lockfile_path[PATH_MAX];
57 static char bookmark_file[PATH_MAX];
58 static char known_hosts_file[PATH_MAX], known_hosts_tmp[PATH_MAX];
59 static char crashed_file[PATH_MAX];
61 char session_file[PATH_MAX];
63 static imsg_handlerfn *handlers[] = {
64 [IMSG_GET] = handle_get,
65 [IMSG_QUIT] = handle_quit,
66 [IMSG_BOOKMARK_PAGE] = handle_bookmark_page,
67 [IMSG_SAVE_CERT] = handle_save_cert,
68 [IMSG_UPDATE_CERT] = handle_update_cert,
69 [IMSG_FILE_OPEN] = handle_file_open,
70 [IMSG_SESSION_START] = handle_session_start,
71 [IMSG_SESSION_TAB] = handle_session_tab,
72 [IMSG_SESSION_TAB_TITLE] = handle_session_tab_title,
73 [IMSG_SESSION_END] = handle_session_end,
74 };
76 static void __attribute__((__noreturn__))
77 die(void)
78 {
79 abort(); /* TODO */
80 }
82 static void
83 handle_get(struct imsg *imsg, size_t datalen)
84 {
85 const char *bpath = "bookmarks.gmi";
86 char path[PATH_MAX], buf[BUFSIZ];
87 FILE *f;
88 const char *data, *p;
89 ssize_t r;
90 size_t i;
91 struct page {
92 const char *name;
93 const char *path;
94 const uint8_t *data;
95 size_t len;
96 } pages[] = {
97 {"about", NULL, about_about, about_about_len},
98 {"blank", NULL, about_blank, about_blank_len},
99 {"bookmarks", bpath, bookmarks, bookmarks_len},
100 {"crash", NULL, about_crash, about_crash_len},
101 {"help", NULL, about_help, about_help_len},
102 {"license", NULL, about_license, about_license_len},
103 {"new", NULL, about_new, about_new_len},
104 }, *page = NULL;
106 data = imsg->data;
107 if (data[datalen-1] != '\0') /* make sure it's NUL-terminated */
108 die();
109 if ((data = strchr(data, ':')) == NULL)
110 goto notfound;
111 data++;
113 for (i = 0; i < sizeof(pages)/sizeof(pages[0]); ++i)
114 if (!strcmp(data, pages[i].name)) {
115 page = &pages[i];
116 break;
119 if (page == NULL)
120 goto notfound;
122 strlcpy(path, base_path, sizeof(path));
123 strlcat(path, "/", sizeof(path));
124 if (page->path != NULL)
125 strlcat(path, page->path, sizeof(path));
126 else {
127 strlcat(path, "pages/about_", sizeof(path));
128 strlcat(path, page->name, sizeof(path));
129 strlcat(path, ".gmi", sizeof(path));
132 if ((f = fopen(path, "r")) == NULL) {
133 fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1,
134 page->data, page->len);
135 fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1,
136 NULL, 0);
137 return;
140 for (;;) {
141 r = fread(buf, 1, sizeof(buf), f);
142 fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, buf, r);
143 if (r != sizeof(buf))
144 break;
146 fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
147 fclose(f);
148 return;
150 notfound:
151 p = "# not found!\n";
152 fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, p, strlen(p));
153 fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
156 static void
157 handle_quit(struct imsg *imsg, size_t datalen)
159 event_loopbreak();
162 static void
163 handle_bookmark_page(struct imsg *imsg, size_t datalen)
165 char *data;
166 int res;
167 FILE *f;
169 data = imsg->data;
170 if (data[datalen-1] != '\0')
171 die();
173 if ((f = fopen(bookmark_file, "a")) == NULL) {
174 res = errno;
175 goto end;
177 fprintf(f, "=> %s\n", data);
178 fclose(f);
180 res = 0;
181 end:
182 fs_send_ui(IMSG_BOOKMARK_OK, 0, -1, &res, sizeof(res));
185 static void
186 handle_save_cert(struct imsg *imsg, size_t datalen)
188 struct tofu_entry e;
189 FILE *f;
190 int res;
192 /* TODO: traverse the file to avoid duplications? */
194 if (datalen != sizeof(e))
195 die();
196 memcpy(&e, imsg->data, datalen);
198 if ((f = fopen(known_hosts_file, "a")) == NULL) {
199 res = errno;
200 goto end;
202 fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
203 fclose(f);
205 res = 0;
206 end:
207 fs_send_ui(IMSG_SAVE_CERT_OK, imsg->hdr.peerid, -1,
208 &res, sizeof(res));
211 static void
212 handle_update_cert(struct imsg *imsg, size_t datalen)
214 FILE *tmp, *f;
215 struct tofu_entry entry;
216 char sfn[PATH_MAX], *line = NULL, *t;
217 size_t l, linesize = 0;
218 ssize_t linelen;
219 int fd, e, res = 0;
221 if (datalen != sizeof(entry))
222 die();
223 memcpy(&entry, imsg->data, datalen);
225 strlcpy(sfn, known_hosts_tmp, sizeof(sfn));
226 if ((fd = mkstemp(sfn)) == -1 ||
227 (tmp = fdopen(fd, "w")) == NULL) {
228 if (fd != -1) {
229 unlink(sfn);
230 close(fd);
232 res = 0;
233 goto end;
236 if ((f = fopen(known_hosts_file, "r")) == NULL) {
237 unlink(sfn);
238 fclose(tmp);
239 res = 0;
240 goto end;
243 l = strlen(entry.domain);
244 while ((linelen = getline(&line, &linesize, f)) != -1) {
245 if ((t = strstr(line, entry.domain)) != NULL &&
246 (line[l] == ' ' || line[l] == '\t'))
247 continue;
248 /* line has a trailing \n */
249 fprintf(tmp, "%s", line);
251 fprintf(tmp, "%s %s %d\n", entry.domain, entry.hash, entry.verified);
253 free(line);
254 e = ferror(tmp);
256 fclose(tmp);
257 fclose(f);
259 if (e) {
260 unlink(sfn);
261 res = 0;
262 goto end;
265 res = rename(sfn, known_hosts_file) != -1;
267 end:
268 fs_send_ui(IMSG_UPDATE_CERT_OK, imsg->hdr.peerid, -1,
269 &res, sizeof(res));
272 static void
273 handle_file_open(struct imsg *imsg, size_t datalen)
275 char *path, *e;
276 int fd;
278 path = imsg->data;
279 if (path[datalen-1] != '\0')
280 die();
282 if ((fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644)) == -1) {
283 e = strerror(errno);
284 fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, -1,
285 e, strlen(e)+1);
286 } else
287 fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, fd,
288 NULL, 0);
291 static void
292 handle_session_start(struct imsg *imsg, size_t datalen)
294 if (datalen != 0)
295 die();
297 if ((session = fopen(session_file, "w")) == NULL)
298 die();
301 static void
302 handle_session_tab(struct imsg *imsg, size_t datalen)
304 char *url;
305 uint32_t flags;
307 if (session == NULL)
308 die();
310 flags = imsg->hdr.peerid;
311 url = imsg->data;
312 if (datalen == 0 || url[datalen-1] != '\0')
313 die();
314 fprintf(session, "%s", url);
316 if (flags & TAB_CURRENT)
317 fprintf(session, " current ");
318 else
319 fprintf(session, " - ");
322 static void
323 handle_session_tab_title(struct imsg *imsg, size_t datalen)
325 const char *title;
327 title = imsg->data;
328 if (title == NULL) {
329 datalen = 1;
330 title = "";
333 if (title[datalen-1] != '\0')
334 die();
336 fprintf(session, "%s\n", title);
339 static void
340 handle_session_end(struct imsg *imsg, size_t datalen)
342 if (session == NULL)
343 die();
344 fclose(session);
345 session = NULL;
348 static void
349 handle_dispatch_imsg(int fd, short ev, void *d)
351 struct imsgev *iev = d;
352 int e;
354 if (dispatch_imsg(iev, ev, handlers, sizeof(handlers)) == -1) {
355 /*
356 * This should leave a ~/.telescope/crashed file to
357 * trigger about:crash on next run. Unfortunately, if
358 * the main process dies the fs sticks around and
359 * doesn't notice that the fd was closed. Why EV_READ
360 * is not triggered when a fd is closed on the other end?
361 */
362 e = errno;
363 if ((fd = open(crashed_file, O_CREAT|O_TRUNC|O_WRONLY, 0600))
364 == -1)
365 err(1, "open");
366 close(fd);
367 errx(1, "connection closed: %s", strerror(e));
371 static int
372 fs_send_ui(int type, uint32_t peerid, int fd, const void *data,
373 uint16_t datalen)
375 return imsg_compose_event(iev_ui, type, peerid, 0, fd,
376 data, datalen);
379 int
380 fs_init(void)
382 strlcpy(base_path, getenv("HOME"), sizeof(base_path));
383 strlcat(base_path, "/.telescope", sizeof(base_path));
384 mkdir(base_path, 0700);
386 strlcpy(lockfile_path, base_path, sizeof(lockfile_path));
387 strlcat(lockfile_path, "/lock", sizeof(lockfile_path));
389 strlcpy(bookmark_file, base_path, sizeof(bookmark_file));
390 strlcat(bookmark_file, "/bookmarks.gmi", sizeof(bookmark_file));
392 strlcpy(known_hosts_file, base_path, sizeof(known_hosts_file));
393 strlcat(known_hosts_file, "/known_hosts", sizeof(known_hosts_file));
395 strlcpy(known_hosts_tmp, base_path, sizeof(known_hosts_tmp));
396 strlcat(known_hosts_tmp, "/known_hosts.tmp.XXXXXXXXXX",
397 sizeof(known_hosts_file));
399 strlcpy(session_file, base_path, sizeof(session_file));
400 strlcat(session_file, "/session", sizeof(session_file));
402 strlcpy(crashed_file, base_path, sizeof(crashed_file));
403 strlcat(crashed_file, "/crashed", sizeof(crashed_file));
405 return 1;
408 int
409 fs_main(void)
411 setproctitle("fs");
413 fs_init();
415 event_init();
417 /* Setup pipe and event handler to the main process */
418 if ((iev_ui = malloc(sizeof(*iev_ui))) == NULL)
419 die();
420 imsg_init(&iev_ui->ibuf, 3);
421 iev_ui->handler = handle_dispatch_imsg;
422 iev_ui->events = EV_READ;
423 event_set(&iev_ui->ev, iev_ui->ibuf.fd, iev_ui->events,
424 iev_ui->handler, iev_ui);
425 event_add(&iev_ui->ev, NULL);
427 sandbox_fs_process();
429 event_dispatch();
430 return 0;
435 int
436 last_time_crashed(void)
438 int fd;
440 if ((fd = open(crashed_file, O_RDONLY)) == -1)
441 return 0;
443 close(fd);
444 unlink(crashed_file);
445 return 1;
448 int
449 lock_session(void)
451 struct flock lock;
452 int fd;
454 if ((fd = open(lockfile_path, O_WRONLY|O_CREAT, 0600)) == -1)
455 return -1;
457 lock.l_start = 0;
458 lock.l_len = 0;
459 lock.l_type = F_WRLCK;
460 lock.l_whence = SEEK_SET;
462 if (fcntl(fd, F_SETLK, &lock) == -1) {
463 close(fd);
464 return -1;
467 return fd;
470 static int
471 parse_khost_line(char *line, char *tmp[3])
473 char **ap;
475 for (ap = tmp; ap < &tmp[3] &&
476 (*ap = strsep(&line, " \t\n")) != NULL;) {
477 if (**ap != '\0')
478 ap++;
481 return ap == &tmp[3] && *line == '\0';
484 int
485 load_certs(struct ohash *h)
487 char *tmp[3], *line = NULL;
488 const char *errstr;
489 size_t lineno = 0, linesize = 0;
490 ssize_t linelen;
491 FILE *f;
492 struct tofu_entry *e;
494 if ((f = fopen(known_hosts_file, "r")) == NULL)
495 return 0;
497 while ((linelen = getline(&line, &linesize, f)) != -1) {
498 if ((e = calloc(1, sizeof(*e))) == NULL)
499 abort();
501 lineno++;
503 if (parse_khost_line(line, tmp)) {
504 strlcpy(e->domain, tmp[0], sizeof(e->domain));
505 strlcpy(e->hash, tmp[1], sizeof(e->hash));
507 e->verified = strtonum(tmp[2], 0, 1, &errstr);
508 if (errstr != NULL)
509 errx(1, "%s:%zu verification for %s is %s: %s",
510 known_hosts_file, lineno,
511 e->domain, errstr, tmp[2]);
512 tofu_add(h, e);
513 } else {
514 warnx("%s:%zu invalid entry",
515 known_hosts_file, lineno);
516 free(e);
520 free(line);
521 return ferror(f);