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:help", NULL, about_help, about_help_len},
131 {"about:new", NULL, about_new, about_new_len},
132 };
134 data = imsg->data;
136 if (data[datalen-1] != '\0')
137 die();
139 for (i = 0; i < sizeof(pages)/sizeof(pages[0]); ++i) {
140 if (strcmp(data, pages[i].name) != 0)
141 continue;
143 if (pages[i].handle != NULL)
144 pages[i].handle(imsg->hdr.peerid);
145 else
146 send_page(imsg, pages[i].data, pages[i].len);
147 return;
150 p = "# not found!\n";
151 fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, p, strlen(p));
152 fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
155 static void
156 handle_quit(struct imsg *imsg, size_t datalen)
158 event_loopbreak();
161 static void
162 handle_bookmark_page(struct imsg *imsg, size_t datalen)
164 char *data;
165 int res;
166 FILE *f;
168 data = imsg->data;
169 if (data[datalen-1] != '\0')
170 die();
172 if ((f = fopen(bookmark_file, "a")) == NULL) {
173 res = errno;
174 goto end;
176 fprintf(f, "=> %s\n", data);
177 fclose(f);
179 res = 0;
180 end:
181 fs_send_ui(IMSG_BOOKMARK_OK, 0, -1, &res, sizeof(res));
184 static void
185 handle_save_cert(struct imsg *imsg, size_t datalen)
187 struct tofu_entry e;
188 FILE *f;
189 int res;
191 /* TODO: traverse the file to avoid duplications? */
193 if (datalen != sizeof(e))
194 die();
195 memcpy(&e, imsg->data, datalen);
197 if ((f = fopen(known_hosts_file, "a")) == NULL) {
198 res = errno;
199 goto end;
201 fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
202 fclose(f);
204 res = 0;
205 end:
206 fs_send_ui(IMSG_SAVE_CERT_OK, imsg->hdr.peerid, -1,
207 &res, sizeof(res));
210 static void
211 handle_update_cert(struct imsg *imsg, size_t datalen)
213 FILE *tmp, *f;
214 struct tofu_entry entry;
215 char sfn[PATH_MAX], *line = NULL, *t;
216 size_t l, linesize = 0;
217 ssize_t linelen;
218 int fd, e, res = 0;
220 if (datalen != sizeof(entry))
221 die();
222 memcpy(&entry, imsg->data, datalen);
224 strlcpy(sfn, known_hosts_tmp, sizeof(sfn));
225 if ((fd = mkstemp(sfn)) == -1 ||
226 (tmp = fdopen(fd, "w")) == NULL) {
227 if (fd != -1) {
228 unlink(sfn);
229 close(fd);
231 res = 0;
232 goto end;
235 if ((f = fopen(known_hosts_file, "r")) == NULL) {
236 unlink(sfn);
237 fclose(tmp);
238 res = 0;
239 goto end;
242 l = strlen(entry.domain);
243 while ((linelen = getline(&line, &linesize, f)) != -1) {
244 if ((t = strstr(line, entry.domain)) != NULL &&
245 (line[l] == ' ' || line[l] == '\t'))
246 continue;
247 /* line has a trailing \n */
248 fprintf(tmp, "%s", line);
250 fprintf(tmp, "%s %s %d\n", entry.domain, entry.hash, entry.verified);
252 free(line);
253 e = ferror(tmp);
255 fclose(tmp);
256 fclose(f);
258 if (e) {
259 unlink(sfn);
260 res = 0;
261 goto end;
264 res = rename(sfn, known_hosts_file) != -1;
266 end:
267 fs_send_ui(IMSG_UPDATE_CERT_OK, imsg->hdr.peerid, -1,
268 &res, sizeof(res));
271 static void
272 handle_file_open(struct imsg *imsg, size_t datalen)
274 char *path, *e;
275 int fd;
277 path = imsg->data;
278 if (path[datalen-1] != '\0')
279 die();
281 if ((fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644)) == -1) {
282 e = strerror(errno);
283 fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, -1,
284 e, strlen(e)+1);
285 } else
286 fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, fd,
287 NULL, 0);
290 static void
291 handle_session_start(struct imsg *imsg, size_t datalen)
293 if (datalen != 0)
294 die();
296 if ((session = fopen(session_file, "w")) == NULL)
297 die();
300 static void
301 handle_session_tab(struct imsg *imsg, size_t datalen)
303 char *url;
304 uint32_t flags;
306 if (session == NULL)
307 die();
309 flags = imsg->hdr.peerid;
310 url = imsg->data;
311 if (datalen == 0 || url[datalen-1] != '\0')
312 die();
313 fprintf(session, "%s", url);
315 if (flags & TAB_CURRENT)
316 fprintf(session, " current ");
317 else
318 fprintf(session, " - ");
321 static void
322 handle_session_tab_title(struct imsg *imsg, size_t datalen)
324 const char *title;
326 title = imsg->data;
327 if (title == NULL) {
328 datalen = 1;
329 title = "";
332 if (title[datalen-1] != '\0')
333 die();
335 fprintf(session, "%s\n", title);
338 static void
339 handle_session_end(struct imsg *imsg, size_t datalen)
341 if (session == NULL)
342 die();
343 fclose(session);
344 session = NULL;
347 static void
348 handle_dispatch_imsg(int fd, short ev, void *d)
350 struct imsgev *iev = d;
351 dispatch_imsg(iev, ev, handlers, sizeof(handlers));
354 static int
355 fs_send_ui(int type, uint32_t peerid, int fd, const void *data,
356 uint16_t datalen)
358 return imsg_compose_event(iev_ui, type, peerid, 0, fd,
359 data, datalen);
362 int
363 fs_init(void)
365 char dir[PATH_MAX];
367 strlcpy(dir, getenv("HOME"), sizeof(dir));
368 strlcat(dir, "/.telescope", sizeof(dir));
369 mkdir(dir, 0700);
371 strlcpy(lockfile_path, getenv("HOME"), sizeof(lockfile_path));
372 strlcat(lockfile_path, "/.telescope/lock", sizeof(lockfile_path));
374 strlcpy(bookmark_file, getenv("HOME"), sizeof(bookmark_file));
375 strlcat(bookmark_file, "/.telescope/bookmarks.gmi", sizeof(bookmark_file));
377 strlcpy(known_hosts_file, getenv("HOME"), sizeof(known_hosts_file));
378 strlcat(known_hosts_file, "/.telescope/known_hosts", sizeof(known_hosts_file));
380 strlcpy(known_hosts_tmp, getenv("HOME"), sizeof(known_hosts_tmp));
381 strlcat(known_hosts_tmp, "/.telescope/known_hosts.tmp.XXXXXXXXXX",
382 sizeof(known_hosts_file));
384 strlcpy(session_file, getenv("HOME"), sizeof(session_file));
385 strlcat(session_file, "/.telescope/session", sizeof(session_file));
387 return 1;
390 int
391 fs_main(void)
393 setproctitle("fs");
395 fs_init();
397 event_init();
399 /* Setup pipe and event handler to the main process */
400 if ((iev_ui = malloc(sizeof(*iev_ui))) == NULL)
401 die();
402 imsg_init(&iev_ui->ibuf, 3);
403 iev_ui->handler = handle_dispatch_imsg;
404 iev_ui->events = EV_READ;
405 event_set(&iev_ui->ev, iev_ui->ibuf.fd, iev_ui->events,
406 iev_ui->handler, iev_ui);
407 event_add(&iev_ui->ev, NULL);
409 sandbox_fs_process();
411 event_dispatch();
412 return 0;
417 int
418 lock_session(void)
420 struct flock lock;
421 int fd;
423 if ((fd = open(lockfile_path, O_WRONLY|O_CREAT, 0600)) == -1)
424 return -1;
426 lock.l_start = 0;
427 lock.l_len = 0;
428 lock.l_type = F_WRLCK;
429 lock.l_whence = SEEK_SET;
431 if (fcntl(fd, F_SETLK, &lock) == -1) {
432 close(fd);
433 return -1;
436 return fd;
439 static int
440 parse_khost_line(char *line, char *tmp[3])
442 char **ap;
444 for (ap = tmp; ap < &tmp[3] &&
445 (*ap = strsep(&line, " \t\n")) != NULL;) {
446 if (**ap != '\0')
447 ap++;
450 return ap == &tmp[3] && *line == '\0';
453 int
454 load_certs(struct ohash *h)
456 char *tmp[3], *line = NULL;
457 const char *errstr;
458 size_t lineno = 0, linesize = 0;
459 ssize_t linelen;
460 FILE *f;
461 struct tofu_entry *e;
463 if ((f = fopen(known_hosts_file, "r")) == NULL)
464 return 0;
466 while ((linelen = getline(&line, &linesize, f)) != -1) {
467 if ((e = calloc(1, sizeof(*e))) == NULL)
468 abort();
470 lineno++;
472 if (parse_khost_line(line, tmp)) {
473 strlcpy(e->domain, tmp[0], sizeof(e->domain));
474 strlcpy(e->hash, tmp[1], sizeof(e->hash));
476 e->verified = strtonum(tmp[2], 0, 1, &errstr);
477 if (errstr != NULL)
478 errx(1, "%s:%zu verification for %s is %s: %s",
479 known_hosts_file, lineno,
480 e->domain, errstr, tmp[2]);
481 tofu_add(h, e);
482 } else {
483 warnx("%s:%zu invalid entry",
484 known_hosts_file, lineno);
485 free(e);
489 free(line);
490 return ferror(f);