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 <sys/stat.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <limits.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
33 #include "telescope.h"
34 #include "pages.h"
36 static void die(void) __attribute__((__noreturn__));
37 static void handle_get(struct imsg*, size_t);
38 static void handle_quit(struct imsg*, size_t);
39 static void handle_bookmark_page(struct imsg*, size_t);
40 static void handle_save_cert(struct imsg*, size_t);
41 static void handle_update_cert(struct imsg*, size_t);
42 static void handle_file_open(struct imsg*, size_t);
43 static void handle_session_start(struct imsg*, size_t);
44 static void handle_session_tab(struct imsg*, size_t);
45 static void handle_session_tab_title(struct imsg*, size_t);
46 static void handle_session_end(struct imsg*, size_t);
47 static void handle_dispatch_imsg(int, short, void*);
48 static int fs_send_ui(int, uint32_t, int, const void *, uint16_t);
50 static struct imsgev *iev_ui;
51 static FILE *session;
53 static char base_path[PATH_MAX];
54 static char lockfile_path[PATH_MAX];
55 static char bookmark_file[PATH_MAX];
56 static char known_hosts_file[PATH_MAX], known_hosts_tmp[PATH_MAX];
57 static char crashed_file[PATH_MAX];
59 char session_file[PATH_MAX];
61 static imsg_handlerfn *handlers[] = {
62 [IMSG_GET] = handle_get,
63 [IMSG_QUIT] = handle_quit,
64 [IMSG_BOOKMARK_PAGE] = handle_bookmark_page,
65 [IMSG_SAVE_CERT] = handle_save_cert,
66 [IMSG_UPDATE_CERT] = handle_update_cert,
67 [IMSG_FILE_OPEN] = handle_file_open,
68 [IMSG_SESSION_START] = handle_session_start,
69 [IMSG_SESSION_TAB] = handle_session_tab,
70 [IMSG_SESSION_TAB_TITLE] = handle_session_tab_title,
71 [IMSG_SESSION_END] = handle_session_end,
72 };
74 static void __attribute__((__noreturn__))
75 die(void)
76 {
77 abort(); /* TODO */
78 }
80 static void
81 handle_get(struct imsg *imsg, size_t datalen)
82 {
83 const char *bpath = "bookmarks.gmi";
84 char path[PATH_MAX], buf[BUFSIZ];
85 FILE *f;
86 const char *data, *p;
87 ssize_t r;
88 size_t i;
89 struct page {
90 const char *name;
91 const char *path;
92 const uint8_t *data;
93 size_t len;
94 } pages[] = {
95 {"about", NULL, about_about, about_about_len},
96 {"blank", NULL, about_blank, about_blank_len},
97 {"bookmarks", bpath, bookmarks, bookmarks_len},
98 {"crash", NULL, about_crash, about_crash_len},
99 {"help", NULL, about_help, about_help_len},
100 {"license", NULL, about_license, about_license_len},
101 {"new", NULL, about_new, about_new_len},
102 }, *page = NULL;
104 data = imsg->data;
105 if (data[datalen-1] != '\0') /* make sure it's NUL-terminated */
106 die();
107 if ((data = strchr(data, ':')) == NULL)
108 goto notfound;
109 data++;
111 for (i = 0; i < sizeof(pages)/sizeof(pages[0]); ++i)
112 if (!strcmp(data, pages[i].name)) {
113 page = &pages[i];
114 break;
117 if (page == NULL)
118 goto notfound;
120 strlcpy(path, base_path, sizeof(path));
121 strlcat(path, "/", sizeof(path));
122 if (page->path != NULL)
123 strlcat(path, page->path, sizeof(path));
124 else {
125 strlcat(path, "pages/about_", sizeof(path));
126 strlcat(path, page->name, sizeof(path));
127 strlcat(path, ".gmi", sizeof(path));
130 if ((f = fopen(path, "r")) == NULL) {
131 fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1,
132 page->data, page->len);
133 fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1,
134 NULL, 0);
135 return;
138 for (;;) {
139 r = fread(buf, 1, sizeof(buf), f);
140 fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, buf, r);
141 if (r != sizeof(buf))
142 break;
144 fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
145 fclose(f);
146 return;
148 notfound:
149 p = "# not found!\n";
150 fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, p, strlen(p));
151 fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
154 static void
155 handle_quit(struct imsg *imsg, size_t datalen)
157 event_loopbreak();
160 static void
161 handle_bookmark_page(struct imsg *imsg, size_t datalen)
163 char *data;
164 int res;
165 FILE *f;
167 data = imsg->data;
168 if (data[datalen-1] != '\0')
169 die();
171 if ((f = fopen(bookmark_file, "a")) == NULL) {
172 res = errno;
173 goto end;
175 fprintf(f, "=> %s\n", data);
176 fclose(f);
178 res = 0;
179 end:
180 fs_send_ui(IMSG_BOOKMARK_OK, 0, -1, &res, sizeof(res));
183 static void
184 handle_save_cert(struct imsg *imsg, size_t datalen)
186 struct tofu_entry e;
187 FILE *f;
188 int res;
190 /* TODO: traverse the file to avoid duplications? */
192 if (datalen != sizeof(e))
193 die();
194 memcpy(&e, imsg->data, datalen);
196 if ((f = fopen(known_hosts_file, "a")) == NULL) {
197 res = errno;
198 goto end;
200 fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
201 fclose(f);
203 res = 0;
204 end:
205 fs_send_ui(IMSG_SAVE_CERT_OK, imsg->hdr.peerid, -1,
206 &res, sizeof(res));
209 static void
210 handle_update_cert(struct imsg *imsg, size_t datalen)
212 FILE *tmp, *f;
213 struct tofu_entry entry;
214 char sfn[PATH_MAX], *line = NULL, *t;
215 size_t l, linesize = 0;
216 ssize_t linelen;
217 int fd, e, res = 0;
219 if (datalen != sizeof(entry))
220 die();
221 memcpy(&entry, imsg->data, datalen);
223 strlcpy(sfn, known_hosts_tmp, sizeof(sfn));
224 if ((fd = mkstemp(sfn)) == -1 ||
225 (tmp = fdopen(fd, "w")) == NULL) {
226 if (fd != -1) {
227 unlink(sfn);
228 close(fd);
230 res = 0;
231 goto end;
234 if ((f = fopen(known_hosts_file, "r")) == NULL) {
235 unlink(sfn);
236 fclose(tmp);
237 res = 0;
238 goto end;
241 l = strlen(entry.domain);
242 while ((linelen = getline(&line, &linesize, f)) != -1) {
243 if ((t = strstr(line, entry.domain)) != NULL &&
244 (line[l] == ' ' || line[l] == '\t'))
245 continue;
246 /* line has a trailing \n */
247 fprintf(tmp, "%s", line);
249 fprintf(tmp, "%s %s %d\n", entry.domain, entry.hash, entry.verified);
251 free(line);
252 e = ferror(tmp);
254 fclose(tmp);
255 fclose(f);
257 if (e) {
258 unlink(sfn);
259 res = 0;
260 goto end;
263 res = rename(sfn, known_hosts_file) != -1;
265 end:
266 fs_send_ui(IMSG_UPDATE_CERT_OK, imsg->hdr.peerid, -1,
267 &res, sizeof(res));
270 static void
271 handle_file_open(struct imsg *imsg, size_t datalen)
273 char *path, *e;
274 int fd;
276 path = imsg->data;
277 if (path[datalen-1] != '\0')
278 die();
280 if ((fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644)) == -1) {
281 e = strerror(errno);
282 fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, -1,
283 e, strlen(e)+1);
284 } else
285 fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, fd,
286 NULL, 0);
289 static void
290 handle_session_start(struct imsg *imsg, size_t datalen)
292 if (datalen != 0)
293 die();
295 if ((session = fopen(session_file, "w")) == NULL)
296 die();
299 static void
300 handle_session_tab(struct imsg *imsg, size_t datalen)
302 char *url;
303 uint32_t flags;
305 if (session == NULL)
306 die();
308 flags = imsg->hdr.peerid;
309 url = imsg->data;
310 if (datalen == 0 || url[datalen-1] != '\0')
311 die();
312 fprintf(session, "%s", url);
314 if (flags & TAB_CURRENT)
315 fprintf(session, " current ");
316 else
317 fprintf(session, " - ");
320 static void
321 handle_session_tab_title(struct imsg *imsg, size_t datalen)
323 const char *title;
325 title = imsg->data;
326 if (title == NULL) {
327 datalen = 1;
328 title = "";
331 if (title[datalen-1] != '\0')
332 die();
334 fprintf(session, "%s\n", title);
337 static void
338 handle_session_end(struct imsg *imsg, size_t datalen)
340 if (session == NULL)
341 die();
342 fclose(session);
343 session = NULL;
346 static void
347 handle_dispatch_imsg(int fd, short ev, void *d)
349 struct imsgev *iev = d;
350 int e;
352 if (dispatch_imsg(iev, ev, handlers, sizeof(handlers)) == -1) {
353 /*
354 * This should leave a ~/.telescope/crashed file to
355 * trigger about:crash on next run. Unfortunately, if
356 * the main process dies the fs sticks around and
357 * doesn't notice that the fd was closed. Why EV_READ
358 * is not triggered when a fd is closed on the other end?
359 */
360 e = errno;
361 if ((fd = open(crashed_file, O_CREAT|O_TRUNC|O_WRONLY, 0600))
362 == -1)
363 err(1, "open");
364 close(fd);
365 errx(1, "connection closed: %s", strerror(e));
369 static int
370 fs_send_ui(int type, uint32_t peerid, int fd, const void *data,
371 uint16_t datalen)
373 return imsg_compose_event(iev_ui, type, peerid, 0, fd,
374 data, datalen);
377 int
378 fs_init(void)
380 strlcpy(base_path, getenv("HOME"), sizeof(base_path));
381 strlcat(base_path, "/.telescope", sizeof(base_path));
382 mkdir(base_path, 0700);
384 strlcpy(lockfile_path, base_path, sizeof(lockfile_path));
385 strlcat(lockfile_path, "/lock", sizeof(lockfile_path));
387 strlcpy(bookmark_file, base_path, sizeof(bookmark_file));
388 strlcat(bookmark_file, "/bookmarks.gmi", sizeof(bookmark_file));
390 strlcpy(known_hosts_file, base_path, sizeof(known_hosts_file));
391 strlcat(known_hosts_file, "/known_hosts", sizeof(known_hosts_file));
393 strlcpy(known_hosts_tmp, base_path, sizeof(known_hosts_tmp));
394 strlcat(known_hosts_tmp, "/known_hosts.tmp.XXXXXXXXXX",
395 sizeof(known_hosts_file));
397 strlcpy(session_file, base_path, sizeof(session_file));
398 strlcat(session_file, "/session", sizeof(session_file));
400 strlcpy(crashed_file, base_path, sizeof(crashed_file));
401 strlcat(crashed_file, "/crashed", sizeof(crashed_file));
403 return 1;
406 int
407 fs_main(void)
409 setproctitle("fs");
411 fs_init();
413 event_init();
415 /* Setup pipe and event handler to the main process */
416 if ((iev_ui = malloc(sizeof(*iev_ui))) == NULL)
417 die();
418 imsg_init(&iev_ui->ibuf, 3);
419 iev_ui->handler = handle_dispatch_imsg;
420 iev_ui->events = EV_READ;
421 event_set(&iev_ui->ev, iev_ui->ibuf.fd, iev_ui->events,
422 iev_ui->handler, iev_ui);
423 event_add(&iev_ui->ev, NULL);
425 sandbox_fs_process();
427 event_dispatch();
428 return 0;
433 int
434 last_time_crashed(void)
436 int fd;
438 if ((fd = open(crashed_file, O_RDONLY)) == -1)
439 return 0;
441 close(fd);
442 unlink(crashed_file);
443 return 1;
446 int
447 lock_session(void)
449 struct flock lock;
450 int fd;
452 if ((fd = open(lockfile_path, O_WRONLY|O_CREAT, 0600)) == -1)
453 return -1;
455 lock.l_start = 0;
456 lock.l_len = 0;
457 lock.l_type = F_WRLCK;
458 lock.l_whence = SEEK_SET;
460 if (fcntl(fd, F_SETLK, &lock) == -1) {
461 close(fd);
462 return -1;
465 return fd;
468 static int
469 parse_khost_line(char *line, char *tmp[3])
471 char **ap;
473 for (ap = tmp; ap < &tmp[3] &&
474 (*ap = strsep(&line, " \t\n")) != NULL;) {
475 if (**ap != '\0')
476 ap++;
479 return ap == &tmp[3] && *line == '\0';
482 int
483 load_certs(struct ohash *h)
485 char *tmp[3], *line = NULL;
486 const char *errstr;
487 size_t lineno = 0, linesize = 0;
488 ssize_t linelen;
489 FILE *f;
490 struct tofu_entry *e;
492 if ((f = fopen(known_hosts_file, "r")) == NULL)
493 return 0;
495 while ((linelen = getline(&line, &linesize, f)) != -1) {
496 if ((e = calloc(1, sizeof(*e))) == NULL)
497 abort();
499 lineno++;
501 if (parse_khost_line(line, tmp)) {
502 strlcpy(e->domain, tmp[0], sizeof(e->domain));
503 strlcpy(e->hash, tmp[1], sizeof(e->hash));
505 e->verified = strtonum(tmp[2], 0, 1, &errstr);
506 if (errstr != NULL)
507 errx(1, "%s:%zu verification for %s is %s: %s",
508 known_hosts_file, lineno,
509 e->domain, errstr, tmp[2]);
510 tofu_add(h, e);
511 } else {
512 warnx("%s:%zu invalid entry",
513 known_hosts_file, lineno);
514 free(e);
518 free(line);
519 return ferror(f);