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 serve_bookmarks(uint32_t);
38 static void send_page(struct imsg *, const uint8_t *, size_t);
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 lockfile_path[PATH_MAX];
56 static char bookmark_file[PATH_MAX];
57 static char known_hosts_file[PATH_MAX], known_hosts_tmp[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 serve_bookmarks(uint32_t peerid)
82 {
83 const char *t;
84 char buf[BUFSIZ];
85 size_t r;
86 FILE *f;
88 if ((f = fopen(bookmark_file, "r")) == NULL) {
89 t = "# Bookmarks\n\n"
90 "No bookmarks yet!\n"
91 "Create ~/.telescope/bookmarks.gmi or use `bookmark-page'.\n";
92 fs_send_ui(IMSG_BUF, peerid, -1, t, strlen(t));
93 fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
94 return;
95 }
97 for (;;) {
98 r = fread(buf, 1, sizeof(buf), f);
99 fs_send_ui(IMSG_BUF, peerid, -1, buf, r);
100 if (r != sizeof(buf))
101 break;
104 fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
106 fclose(f);
109 static void
110 send_page(struct imsg *imsg, const uint8_t *page, size_t len)
112 fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, page, len);
113 fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
116 static void
117 handle_get(struct imsg *imsg, size_t datalen)
119 const char *data, *p;
120 size_t i;
121 struct page {
122 const char *name;
123 void (*handle)(uint32_t);
124 const uint8_t *data;
125 size_t len;
126 } pages[] = {
127 {"about:about", NULL, about_about, about_about_len},
128 {"about:blank", NULL, about_blank, about_blank_len},
129 {"about:bookmarks", serve_bookmarks, 0, 0},
130 {"about:crash", NULL, about_crash, about_crash_len},
131 {"about:help", NULL, about_help, about_help_len},
132 {"about:license", NULL, about_license, about_license_len},
133 {"about:new", NULL, about_new, about_new_len},
134 };
136 data = imsg->data;
138 if (data[datalen-1] != '\0')
139 die();
141 for (i = 0; i < sizeof(pages)/sizeof(pages[0]); ++i) {
142 if (strcmp(data, pages[i].name) != 0)
143 continue;
145 if (pages[i].handle != NULL)
146 pages[i].handle(imsg->hdr.peerid);
147 else
148 send_page(imsg, pages[i].data, pages[i].len);
149 return;
152 p = "# not found!\n";
153 fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, p, strlen(p));
154 fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
157 static void
158 handle_quit(struct imsg *imsg, size_t datalen)
160 event_loopbreak();
163 static void
164 handle_bookmark_page(struct imsg *imsg, size_t datalen)
166 char *data;
167 int res;
168 FILE *f;
170 data = imsg->data;
171 if (data[datalen-1] != '\0')
172 die();
174 if ((f = fopen(bookmark_file, "a")) == NULL) {
175 res = errno;
176 goto end;
178 fprintf(f, "=> %s\n", data);
179 fclose(f);
181 res = 0;
182 end:
183 fs_send_ui(IMSG_BOOKMARK_OK, 0, -1, &res, sizeof(res));
186 static void
187 handle_save_cert(struct imsg *imsg, size_t datalen)
189 struct tofu_entry e;
190 FILE *f;
191 int res;
193 /* TODO: traverse the file to avoid duplications? */
195 if (datalen != sizeof(e))
196 die();
197 memcpy(&e, imsg->data, datalen);
199 if ((f = fopen(known_hosts_file, "a")) == NULL) {
200 res = errno;
201 goto end;
203 fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
204 fclose(f);
206 res = 0;
207 end:
208 fs_send_ui(IMSG_SAVE_CERT_OK, imsg->hdr.peerid, -1,
209 &res, sizeof(res));
212 static void
213 handle_update_cert(struct imsg *imsg, size_t datalen)
215 FILE *tmp, *f;
216 struct tofu_entry entry;
217 char sfn[PATH_MAX], *line = NULL, *t;
218 size_t l, linesize = 0;
219 ssize_t linelen;
220 int fd, e, res = 0;
222 if (datalen != sizeof(entry))
223 die();
224 memcpy(&entry, imsg->data, datalen);
226 strlcpy(sfn, known_hosts_tmp, sizeof(sfn));
227 if ((fd = mkstemp(sfn)) == -1 ||
228 (tmp = fdopen(fd, "w")) == NULL) {
229 if (fd != -1) {
230 unlink(sfn);
231 close(fd);
233 res = 0;
234 goto end;
237 if ((f = fopen(known_hosts_file, "r")) == NULL) {
238 unlink(sfn);
239 fclose(tmp);
240 res = 0;
241 goto end;
244 l = strlen(entry.domain);
245 while ((linelen = getline(&line, &linesize, f)) != -1) {
246 if ((t = strstr(line, entry.domain)) != NULL &&
247 (line[l] == ' ' || line[l] == '\t'))
248 continue;
249 /* line has a trailing \n */
250 fprintf(tmp, "%s", line);
252 fprintf(tmp, "%s %s %d\n", entry.domain, entry.hash, entry.verified);
254 free(line);
255 e = ferror(tmp);
257 fclose(tmp);
258 fclose(f);
260 if (e) {
261 unlink(sfn);
262 res = 0;
263 goto end;
266 res = rename(sfn, known_hosts_file) != -1;
268 end:
269 fs_send_ui(IMSG_UPDATE_CERT_OK, imsg->hdr.peerid, -1,
270 &res, sizeof(res));
273 static void
274 handle_file_open(struct imsg *imsg, size_t datalen)
276 char *path, *e;
277 int fd;
279 path = imsg->data;
280 if (path[datalen-1] != '\0')
281 die();
283 if ((fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644)) == -1) {
284 e = strerror(errno);
285 fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, -1,
286 e, strlen(e)+1);
287 } else
288 fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, fd,
289 NULL, 0);
292 static void
293 handle_session_start(struct imsg *imsg, size_t datalen)
295 if (datalen != 0)
296 die();
298 if ((session = fopen(session_file, "w")) == NULL)
299 die();
302 static void
303 handle_session_tab(struct imsg *imsg, size_t datalen)
305 char *url;
306 uint32_t flags;
308 if (session == NULL)
309 die();
311 flags = imsg->hdr.peerid;
312 url = imsg->data;
313 if (datalen == 0 || url[datalen-1] != '\0')
314 die();
315 fprintf(session, "%s", url);
317 if (flags & TAB_CURRENT)
318 fprintf(session, " current ");
319 else
320 fprintf(session, " - ");
323 static void
324 handle_session_tab_title(struct imsg *imsg, size_t datalen)
326 const char *title;
328 title = imsg->data;
329 if (title == NULL) {
330 datalen = 1;
331 title = "";
334 if (title[datalen-1] != '\0')
335 die();
337 fprintf(session, "%s\n", title);
340 static void
341 handle_session_end(struct imsg *imsg, size_t datalen)
343 if (session == NULL)
344 die();
345 fclose(session);
346 session = NULL;
349 static void
350 handle_dispatch_imsg(int fd, short ev, void *d)
352 struct imsgev *iev = d;
353 dispatch_imsg(iev, ev, handlers, sizeof(handlers));
356 static int
357 fs_send_ui(int type, uint32_t peerid, int fd, const void *data,
358 uint16_t datalen)
360 return imsg_compose_event(iev_ui, type, peerid, 0, fd,
361 data, datalen);
364 int
365 fs_init(void)
367 char dir[PATH_MAX];
369 strlcpy(dir, getenv("HOME"), sizeof(dir));
370 strlcat(dir, "/.telescope", sizeof(dir));
371 mkdir(dir, 0700);
373 strlcpy(lockfile_path, getenv("HOME"), sizeof(lockfile_path));
374 strlcat(lockfile_path, "/.telescope/lock", sizeof(lockfile_path));
376 strlcpy(bookmark_file, getenv("HOME"), sizeof(bookmark_file));
377 strlcat(bookmark_file, "/.telescope/bookmarks.gmi", sizeof(bookmark_file));
379 strlcpy(known_hosts_file, getenv("HOME"), sizeof(known_hosts_file));
380 strlcat(known_hosts_file, "/.telescope/known_hosts", sizeof(known_hosts_file));
382 strlcpy(known_hosts_tmp, getenv("HOME"), sizeof(known_hosts_tmp));
383 strlcat(known_hosts_tmp, "/.telescope/known_hosts.tmp.XXXXXXXXXX",
384 sizeof(known_hosts_file));
386 strlcpy(session_file, getenv("HOME"), sizeof(session_file));
387 strlcat(session_file, "/.telescope/session", sizeof(session_file));
389 return 1;
392 int
393 fs_main(void)
395 setproctitle("fs");
397 fs_init();
399 event_init();
401 /* Setup pipe and event handler to the main process */
402 if ((iev_ui = malloc(sizeof(*iev_ui))) == NULL)
403 die();
404 imsg_init(&iev_ui->ibuf, 3);
405 iev_ui->handler = handle_dispatch_imsg;
406 iev_ui->events = EV_READ;
407 event_set(&iev_ui->ev, iev_ui->ibuf.fd, iev_ui->events,
408 iev_ui->handler, iev_ui);
409 event_add(&iev_ui->ev, NULL);
411 sandbox_fs_process();
413 event_dispatch();
414 return 0;
419 int
420 lock_session(void)
422 struct flock lock;
423 int fd;
425 if ((fd = open(lockfile_path, O_WRONLY|O_CREAT, 0600)) == -1)
426 return -1;
428 lock.l_start = 0;
429 lock.l_len = 0;
430 lock.l_type = F_WRLCK;
431 lock.l_whence = SEEK_SET;
433 if (fcntl(fd, F_SETLK, &lock) == -1) {
434 close(fd);
435 return -1;
438 return fd;
441 static int
442 parse_khost_line(char *line, char *tmp[3])
444 char **ap;
446 for (ap = tmp; ap < &tmp[3] &&
447 (*ap = strsep(&line, " \t\n")) != NULL;) {
448 if (**ap != '\0')
449 ap++;
452 return ap == &tmp[3] && *line == '\0';
455 int
456 load_certs(struct ohash *h)
458 char *tmp[3], *line = NULL;
459 const char *errstr;
460 size_t lineno = 0, linesize = 0;
461 ssize_t linelen;
462 FILE *f;
463 struct tofu_entry *e;
465 if ((f = fopen(known_hosts_file, "r")) == NULL)
466 return 0;
468 while ((linelen = getline(&line, &linesize, f)) != -1) {
469 if ((e = calloc(1, sizeof(*e))) == NULL)
470 abort();
472 lineno++;
474 if (parse_khost_line(line, tmp)) {
475 strlcpy(e->domain, tmp[0], sizeof(e->domain));
476 strlcpy(e->hash, tmp[1], sizeof(e->hash));
478 e->verified = strtonum(tmp[2], 0, 1, &errstr);
479 if (errstr != NULL)
480 errx(1, "%s:%zu verification for %s is %s: %s",
481 known_hosts_file, lineno,
482 e->domain, errstr, tmp[2]);
483 tofu_add(h, e);
484 } else {
485 warnx("%s:%zu invalid entry",
486 known_hosts_file, lineno);
487 free(e);
491 free(line);
492 return ferror(f);