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 char *);
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 char *page)
112 fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, page, strlen(page));
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 char *data;
120 const char *p;
122 data = imsg->data;
124 if (data[datalen-1] != '\0')
125 die();
127 if (!strcmp(data, "about:about")) {
128 send_page(imsg, about_about);
129 } else if (!strcmp(data, "about:blank")) {
130 send_page(imsg, about_blank);
131 } else if (!strcmp(data, "about:bookmarks")) {
132 serve_bookmarks(imsg->hdr.peerid);
133 } else if (!strcmp(data, "about:help")) {
134 send_page(imsg, about_help);
135 } else if (!strcmp(data, "about:new")) {
136 send_page(imsg, about_new);
137 } else {
138 p = "# not found!\n";
139 fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, p, strlen(p));
140 fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
144 static void
145 handle_quit(struct imsg *imsg, size_t datalen)
147 event_loopbreak();
150 static void
151 handle_bookmark_page(struct imsg *imsg, size_t datalen)
153 char *data;
154 int res;
155 FILE *f;
157 data = imsg->data;
158 if (data[datalen-1] != '\0')
159 die();
161 if ((f = fopen(bookmark_file, "a")) == NULL) {
162 res = errno;
163 goto end;
165 fprintf(f, "=> %s\n", data);
166 fclose(f);
168 res = 0;
169 end:
170 fs_send_ui(IMSG_BOOKMARK_OK, 0, -1, &res, sizeof(res));
173 static void
174 handle_save_cert(struct imsg *imsg, size_t datalen)
176 struct tofu_entry e;
177 FILE *f;
178 int res;
180 /* TODO: traverse the file to avoid duplications? */
182 if (datalen != sizeof(e))
183 die();
184 memcpy(&e, imsg->data, datalen);
186 if ((f = fopen(known_hosts_file, "a")) == NULL) {
187 res = errno;
188 goto end;
190 fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
191 fclose(f);
193 res = 0;
194 end:
195 fs_send_ui(IMSG_SAVE_CERT_OK, imsg->hdr.peerid, -1,
196 &res, sizeof(res));
199 static void
200 handle_update_cert(struct imsg *imsg, size_t datalen)
202 FILE *tmp, *f;
203 struct tofu_entry entry;
204 char sfn[PATH_MAX], *line = NULL, *t;
205 size_t l, linesize = 0;
206 ssize_t linelen;
207 int fd, e, res = 0;
209 if (datalen != sizeof(entry))
210 die();
211 memcpy(&entry, imsg->data, datalen);
213 strlcpy(sfn, known_hosts_tmp, sizeof(sfn));
214 if ((fd = mkstemp(sfn)) == -1 ||
215 (tmp = fdopen(fd, "w")) == NULL) {
216 if (fd != -1) {
217 unlink(sfn);
218 close(fd);
220 res = 0;
221 goto end;
224 if ((f = fopen(known_hosts_file, "r")) == NULL) {
225 unlink(sfn);
226 fclose(tmp);
227 res = 0;
228 goto end;
231 l = strlen(entry.domain);
232 while ((linelen = getline(&line, &linesize, f)) != -1) {
233 if ((t = strstr(line, entry.domain)) != NULL &&
234 (line[l] == ' ' || line[l] == '\t'))
235 continue;
236 /* line has a trailing \n */
237 fprintf(tmp, "%s", line);
239 fprintf(tmp, "%s %s %d\n", entry.domain, entry.hash, entry.verified);
241 free(line);
242 e = ferror(tmp);
244 fclose(tmp);
245 fclose(f);
247 if (e) {
248 unlink(sfn);
249 res = 0;
250 goto end;
253 res = rename(sfn, known_hosts_file) != -1;
255 end:
256 fs_send_ui(IMSG_UPDATE_CERT_OK, imsg->hdr.peerid, -1,
257 &res, sizeof(res));
260 static void
261 handle_file_open(struct imsg *imsg, size_t datalen)
263 char *path, *e;
264 int fd;
266 path = imsg->data;
267 if (path[datalen-1] != '\0')
268 die();
270 if ((fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644)) == -1) {
271 e = strerror(errno);
272 fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, -1,
273 e, strlen(e)+1);
274 } else
275 fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, fd,
276 NULL, 0);
279 static void
280 handle_session_start(struct imsg *imsg, size_t datalen)
282 if (datalen != 0)
283 die();
285 if ((session = fopen(session_file, "w")) == NULL)
286 die();
289 static void
290 handle_session_tab(struct imsg *imsg, size_t datalen)
292 char *url;
293 uint32_t flags;
295 if (session == NULL)
296 die();
298 flags = imsg->hdr.peerid;
299 url = imsg->data;
300 if (datalen == 0 || url[datalen-1] != '\0')
301 die();
302 fprintf(session, "%s", url);
304 if (flags & TAB_CURRENT)
305 fprintf(session, " current ");
306 else
307 fprintf(session, " - ");
310 static void
311 handle_session_tab_title(struct imsg *imsg, size_t datalen)
313 const char *title;
315 title = imsg->data;
316 if (title == NULL) {
317 datalen = 1;
318 title = "";
321 if (title[datalen-1] != '\0')
322 die();
324 fprintf(session, "%s\n", title);
327 static void
328 handle_session_end(struct imsg *imsg, size_t datalen)
330 if (session == NULL)
331 die();
332 fclose(session);
333 session = NULL;
336 static void
337 handle_dispatch_imsg(int fd, short ev, void *d)
339 struct imsgev *iev = d;
340 dispatch_imsg(iev, ev, handlers, sizeof(handlers));
343 static int
344 fs_send_ui(int type, uint32_t peerid, int fd, const void *data,
345 uint16_t datalen)
347 return imsg_compose_event(iev_ui, type, peerid, 0, fd,
348 data, datalen);
351 int
352 fs_init(void)
354 char dir[PATH_MAX];
356 strlcpy(dir, getenv("HOME"), sizeof(dir));
357 strlcat(dir, "/.telescope", sizeof(dir));
358 mkdir(dir, 0700);
360 strlcpy(lockfile_path, getenv("HOME"), sizeof(lockfile_path));
361 strlcat(lockfile_path, "/.telescope/lock", sizeof(lockfile_path));
363 strlcpy(bookmark_file, getenv("HOME"), sizeof(bookmark_file));
364 strlcat(bookmark_file, "/.telescope/bookmarks.gmi", sizeof(bookmark_file));
366 strlcpy(known_hosts_file, getenv("HOME"), sizeof(known_hosts_file));
367 strlcat(known_hosts_file, "/.telescope/known_hosts", sizeof(known_hosts_file));
369 strlcpy(known_hosts_tmp, getenv("HOME"), sizeof(known_hosts_tmp));
370 strlcat(known_hosts_tmp, "/.telescope/known_hosts.tmp.XXXXXXXXXX",
371 sizeof(known_hosts_file));
373 strlcpy(session_file, getenv("HOME"), sizeof(session_file));
374 strlcat(session_file, "/.telescope/session", sizeof(session_file));
376 return 1;
379 int
380 fs_main(void)
382 setproctitle("fs");
384 fs_init();
386 event_init();
388 /* Setup pipe and event handler to the main process */
389 if ((iev_ui = malloc(sizeof(*iev_ui))) == NULL)
390 die();
391 imsg_init(&iev_ui->ibuf, 3);
392 iev_ui->handler = handle_dispatch_imsg;
393 iev_ui->events = EV_READ;
394 event_set(&iev_ui->ev, iev_ui->ibuf.fd, iev_ui->events,
395 iev_ui->handler, iev_ui);
396 event_add(&iev_ui->ev, NULL);
398 sandbox_fs_process();
400 event_dispatch();
401 return 0;
406 int
407 lock_session(void)
409 struct flock lock;
410 int fd;
412 if ((fd = open(lockfile_path, O_WRONLY|O_CREAT, 0600)) == -1)
413 return -1;
415 lock.l_start = 0;
416 lock.l_len = 0;
417 lock.l_type = F_WRLCK;
418 lock.l_whence = SEEK_SET;
420 if (fcntl(fd, F_SETLK, &lock) == -1) {
421 close(fd);
422 return -1;
425 return fd;
428 static int
429 parse_khost_line(char *line, char *tmp[3])
431 char **ap;
433 for (ap = tmp; ap < &tmp[3] &&
434 (*ap = strsep(&line, " \t\n")) != NULL;) {
435 if (**ap != '\0')
436 ap++;
439 return ap == &tmp[3] && *line == '\0';
442 int
443 load_certs(struct ohash *h)
445 char *tmp[3], *line = NULL;
446 const char *errstr;
447 size_t lineno = 0, linesize = 0;
448 ssize_t linelen;
449 FILE *f;
450 struct tofu_entry *e;
452 if ((f = fopen(known_hosts_file, "r")) == NULL)
453 return 0;
455 while ((linelen = getline(&line, &linesize, f)) != -1) {
456 if ((e = calloc(1, sizeof(*e))) == NULL)
457 abort();
459 lineno++;
461 if (parse_khost_line(line, tmp)) {
462 strlcpy(e->domain, tmp[0], sizeof(e->domain));
463 strlcpy(e->hash, tmp[1], sizeof(e->hash));
465 e->verified = strtonum(tmp[2], 0, 1, &errstr);
466 if (errstr != NULL)
467 errx(1, "%s:%zu verification for %s is %s: %s",
468 known_hosts_file, lineno,
469 e->domain, errstr, tmp[2]);
470 tofu_add(h, e);
471 } else {
472 warnx("%s:%zu invalid entry",
473 known_hosts_file, lineno);
474 free(e);
478 free(line);
479 return ferror(f);