2 35e1f40a 2021-03-14 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 35e1f40a 2021-03-14 op * Permission to use, copy, modify, and distribute this software for any
5 35e1f40a 2021-03-14 op * purpose with or without fee is hereby granted, provided that the above
6 35e1f40a 2021-03-14 op * copyright notice and this permission notice appear in all copies.
8 35e1f40a 2021-03-14 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 35e1f40a 2021-03-14 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 35e1f40a 2021-03-14 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 35e1f40a 2021-03-14 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 35e1f40a 2021-03-14 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 35e1f40a 2021-03-14 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 35e1f40a 2021-03-14 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 740f578b 2021-03-15 op * Handles the data in ~/.telescope
20 3a227e9a 2021-03-18 op * TODO: add some form of locking on the files
23 6cd6a9e1 2021-03-20 op #include <sys/stat.h>
25 35e1f40a 2021-03-14 op #include <errno.h>
26 de2a69bb 2021-05-17 op #include <fcntl.h>
27 35e1f40a 2021-03-14 op #include <limits.h>
28 35e1f40a 2021-03-14 op #include <stdio.h>
29 35e1f40a 2021-03-14 op #include <stdlib.h>
30 35e1f40a 2021-03-14 op #include <string.h>
31 35e1f40a 2021-03-14 op #include <unistd.h>
33 4913b479 2021-07-12 op #include "telescope.h"
34 5a824be4 2021-07-13 op #include "pages.h"
36 35e1f40a 2021-03-14 op static void die(void) __attribute__((__noreturn__));
37 35e1f40a 2021-03-14 op static void serve_bookmarks(uint32_t);
38 1b412079 2021-06-19 op static void send_page(struct imsg *, const char *);
39 35e1f40a 2021-03-14 op static void handle_get(struct imsg*, size_t);
40 35e1f40a 2021-03-14 op static void handle_quit(struct imsg*, size_t);
41 740f578b 2021-03-15 op static void handle_bookmark_page(struct imsg*, size_t);
42 3a227e9a 2021-03-18 op static void handle_save_cert(struct imsg*, size_t);
43 288fd238 2021-04-25 op static void handle_update_cert(struct imsg*, size_t);
44 de2a69bb 2021-05-17 op static void handle_file_open(struct imsg*, size_t);
45 c7107cec 2021-04-01 op static void handle_session_start(struct imsg*, size_t);
46 c7107cec 2021-04-01 op static void handle_session_tab(struct imsg*, size_t);
47 c7107cec 2021-04-01 op static void handle_session_end(struct imsg*, size_t);
48 1304bbdd 2021-03-15 op static void handle_dispatch_imsg(int, short, void*);
49 bc10f6a5 2021-07-12 op static int fs_send_ui(int, uint32_t, int, const void *, uint16_t);
51 bc10f6a5 2021-07-12 op static struct imsgev *iev_ui;
52 c7107cec 2021-04-01 op static FILE *session;
54 740f578b 2021-03-15 op static char bookmark_file[PATH_MAX];
55 288fd238 2021-04-25 op static char known_hosts_file[PATH_MAX], known_hosts_tmp[PATH_MAX];
56 c7107cec 2021-04-01 op static char session_file[PATH_MAX];
58 35e1f40a 2021-03-14 op static imsg_handlerfn *handlers[] = {
59 35e1f40a 2021-03-14 op [IMSG_GET] = handle_get,
60 35e1f40a 2021-03-14 op [IMSG_QUIT] = handle_quit,
61 740f578b 2021-03-15 op [IMSG_BOOKMARK_PAGE] = handle_bookmark_page,
62 3a227e9a 2021-03-18 op [IMSG_SAVE_CERT] = handle_save_cert,
63 288fd238 2021-04-25 op [IMSG_UPDATE_CERT] = handle_update_cert,
64 de2a69bb 2021-05-17 op [IMSG_FILE_OPEN] = handle_file_open,
65 c7107cec 2021-04-01 op [IMSG_SESSION_START] = handle_session_start,
66 c7107cec 2021-04-01 op [IMSG_SESSION_TAB] = handle_session_tab,
67 c7107cec 2021-04-01 op [IMSG_SESSION_END] = handle_session_end,
70 35e1f40a 2021-03-14 op static void __attribute__((__noreturn__))
73 35e1f40a 2021-03-14 op abort(); /* TODO */
77 35e1f40a 2021-03-14 op serve_bookmarks(uint32_t peerid)
79 35e1f40a 2021-03-14 op const char *t;
80 740f578b 2021-03-15 op char buf[BUFSIZ];
84 740f578b 2021-03-15 op if ((f = fopen(bookmark_file, "r")) == NULL) {
85 503425db 2021-04-01 op t = "# Bookmarks\n\n"
86 503425db 2021-04-01 op "No bookmarks yet!\n"
87 503425db 2021-04-01 op "Create ~/.telescope/bookmarks.gmi or use `bookmark-page'.\n";
88 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_BUF, peerid, -1, t, strlen(t));
89 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
94 35e1f40a 2021-03-14 op r = fread(buf, 1, sizeof(buf), f);
95 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_BUF, peerid, -1, buf, r);
96 35e1f40a 2021-03-14 op if (r != sizeof(buf))
100 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
106 1b412079 2021-06-19 op send_page(struct imsg *imsg, const char *page)
108 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, page, strlen(page));
109 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
113 35e1f40a 2021-03-14 op handle_get(struct imsg *imsg, size_t datalen)
116 35e1f40a 2021-03-14 op const char *p;
118 35e1f40a 2021-03-14 op data = imsg->data;
120 35e1f40a 2021-03-14 op if (data[datalen-1] != '\0')
123 1b412079 2021-06-19 op if (!strcmp(data, "about:about")) {
124 1b412079 2021-06-19 op send_page(imsg, about_about);
125 1b412079 2021-06-19 op } else if (!strcmp(data, "about:blank")) {
126 1b412079 2021-06-19 op send_page(imsg, about_blank);
127 35e1f40a 2021-03-14 op } else if (!strcmp(data, "about:bookmarks")) {
128 35e1f40a 2021-03-14 op serve_bookmarks(imsg->hdr.peerid);
129 1b412079 2021-06-19 op } else if (!strcmp(data, "about:help")) {
130 1b412079 2021-06-19 op send_page(imsg, about_help);
131 1b412079 2021-06-19 op } else if (!strcmp(data, "about:new")) {
132 1b412079 2021-06-19 op send_page(imsg, about_new);
134 35e1f40a 2021-03-14 op p = "# not found!\n";
135 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, p, strlen(p));
136 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
141 35e1f40a 2021-03-14 op handle_quit(struct imsg *imsg, size_t datalen)
143 35e1f40a 2021-03-14 op event_loopbreak();
147 740f578b 2021-03-15 op handle_bookmark_page(struct imsg *imsg, size_t datalen)
153 740f578b 2021-03-15 op data = imsg->data;
154 740f578b 2021-03-15 op if (data[datalen-1] != '\0')
157 740f578b 2021-03-15 op if ((f = fopen(bookmark_file, "a")) == NULL) {
161 740f578b 2021-03-15 op fprintf(f, "=> %s\n", data);
166 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_BOOKMARK_OK, 0, -1, &res, sizeof(res));
170 3a227e9a 2021-03-18 op handle_save_cert(struct imsg *imsg, size_t datalen)
172 3a227e9a 2021-03-18 op struct tofu_entry e;
176 3a227e9a 2021-03-18 op /* TODO: traverse the file to avoid duplications? */
178 3a227e9a 2021-03-18 op if (datalen != sizeof(e))
180 3a227e9a 2021-03-18 op memcpy(&e, imsg->data, datalen);
182 3a227e9a 2021-03-18 op if ((f = fopen(known_hosts_file, "a")) == NULL) {
186 3a227e9a 2021-03-18 op fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
191 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_SAVE_CERT_OK, imsg->hdr.peerid, -1,
192 288fd238 2021-04-25 op &res, sizeof(res));
196 288fd238 2021-04-25 op handle_update_cert(struct imsg *imsg, size_t datalen)
198 288fd238 2021-04-25 op FILE *tmp, *f;
199 288fd238 2021-04-25 op struct tofu_entry entry;
200 288fd238 2021-04-25 op char sfn[PATH_MAX], *line = NULL, *t;
201 288fd238 2021-04-25 op size_t l, linesize = 0;
202 288fd238 2021-04-25 op ssize_t linelen;
203 288fd238 2021-04-25 op int fd, e, res = 0;
205 288fd238 2021-04-25 op if (datalen != sizeof(entry))
207 288fd238 2021-04-25 op memcpy(&entry, imsg->data, datalen);
209 288fd238 2021-04-25 op strlcpy(sfn, known_hosts_tmp, sizeof(sfn));
210 288fd238 2021-04-25 op if ((fd = mkstemp(sfn)) == -1 ||
211 288fd238 2021-04-25 op (tmp = fdopen(fd, "w")) == NULL) {
212 288fd238 2021-04-25 op if (fd != -1) {
220 288fd238 2021-04-25 op if ((f = fopen(known_hosts_file, "r")) == NULL) {
227 288fd238 2021-04-25 op l = strlen(entry.domain);
228 288fd238 2021-04-25 op while ((linelen = getline(&line, &linesize, f)) != -1) {
229 288fd238 2021-04-25 op if ((t = strstr(line, entry.domain)) != NULL &&
230 288fd238 2021-04-25 op (line[l] == ' ' || line[l] == '\t'))
232 288fd238 2021-04-25 op /* line has a trailing \n */
233 288fd238 2021-04-25 op fprintf(tmp, "%s", line);
235 288fd238 2021-04-25 op fprintf(tmp, "%s %s %d\n", entry.domain, entry.hash, entry.verified);
238 288fd238 2021-04-25 op e = ferror(tmp);
249 288fd238 2021-04-25 op res = rename(sfn, known_hosts_file) != -1;
252 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_UPDATE_CERT_OK, imsg->hdr.peerid, -1,
253 3a227e9a 2021-03-18 op &res, sizeof(res));
257 de2a69bb 2021-05-17 op handle_file_open(struct imsg *imsg, size_t datalen)
259 de2a69bb 2021-05-17 op char *path, *e;
262 de2a69bb 2021-05-17 op path = imsg->data;
263 de2a69bb 2021-05-17 op if (path[datalen-1] != '\0')
266 de2a69bb 2021-05-17 op if ((fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644)) == -1) {
267 de2a69bb 2021-05-17 op e = strerror(errno);
268 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, -1,
269 de2a69bb 2021-05-17 op e, strlen(e)+1);
271 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, fd,
276 c7107cec 2021-04-01 op handle_session_start(struct imsg *imsg, size_t datalen)
278 c7107cec 2021-04-01 op if (datalen != 0)
281 c7107cec 2021-04-01 op if ((session = fopen(session_file, "w")) == NULL)
286 c7107cec 2021-04-01 op handle_session_tab(struct imsg *imsg, size_t datalen)
288 c7107cec 2021-04-01 op if (session == NULL)
291 c7107cec 2021-04-01 op if (datalen == 0)
294 c7107cec 2021-04-01 op /* skip the NUL-terminator */
295 c7107cec 2021-04-01 op fwrite(imsg->data, 1, datalen-1, session);
297 c7107cec 2021-04-01 op fprintf(session, "\n");
301 c7107cec 2021-04-01 op handle_session_end(struct imsg *imsg, size_t datalen)
303 c7107cec 2021-04-01 op if (session == NULL)
305 c7107cec 2021-04-01 op fclose(session);
306 c7107cec 2021-04-01 op session = NULL;
310 1304bbdd 2021-03-15 op handle_dispatch_imsg(int fd, short ev, void *d)
312 bc10f6a5 2021-07-12 op struct imsgev *iev = d;
313 bc10f6a5 2021-07-12 op dispatch_imsg(iev, ev, handlers, sizeof(handlers));
317 bc10f6a5 2021-07-12 op fs_send_ui(int type, uint32_t peerid, int fd, const void *data,
318 bc10f6a5 2021-07-12 op uint16_t datalen)
320 bc10f6a5 2021-07-12 op return imsg_compose_event(iev_ui, type, peerid, 0, fd,
321 bc10f6a5 2021-07-12 op data, datalen);
325 3a227e9a 2021-03-18 op fs_init(void)
327 63a715ea 2021-03-18 op char dir[PATH_MAX];
329 63a715ea 2021-03-18 op strlcpy(dir, getenv("HOME"), sizeof(dir));
330 63a715ea 2021-03-18 op strlcat(dir, "/.telescope", sizeof(dir));
331 63a715ea 2021-03-18 op mkdir(dir, 0700);
333 740f578b 2021-03-15 op strlcpy(bookmark_file, getenv("HOME"), sizeof(bookmark_file));
334 740f578b 2021-03-15 op strlcat(bookmark_file, "/.telescope/bookmarks.gmi", sizeof(bookmark_file));
336 3a227e9a 2021-03-18 op strlcpy(known_hosts_file, getenv("HOME"), sizeof(known_hosts_file));
337 3a227e9a 2021-03-18 op strlcat(known_hosts_file, "/.telescope/known_hosts", sizeof(known_hosts_file));
339 288fd238 2021-04-25 op strlcpy(known_hosts_tmp, getenv("HOME"), sizeof(known_hosts_tmp));
340 288fd238 2021-04-25 op strlcat(known_hosts_tmp, "/.telescope/known_hosts.tmp.XXXXXXXXXX",
341 288fd238 2021-04-25 op sizeof(known_hosts_file));
343 c7107cec 2021-04-01 op strlcpy(session_file, getenv("HOME"), sizeof(session_file));
344 c7107cec 2021-04-01 op strlcat(session_file, "/.telescope/session", sizeof(session_file));
350 6cc5fcfe 2021-07-08 op fs_main(void)
352 6cc5fcfe 2021-07-08 op setproctitle("fs");
356 35e1f40a 2021-03-14 op event_init();
358 bc10f6a5 2021-07-12 op /* Setup pipe and event handler to the main process */
359 bc10f6a5 2021-07-12 op if ((iev_ui = malloc(sizeof(*iev_ui))) == NULL)
361 bc10f6a5 2021-07-12 op imsg_init(&iev_ui->ibuf, 3);
362 bc10f6a5 2021-07-12 op iev_ui->handler = handle_dispatch_imsg;
363 bc10f6a5 2021-07-12 op iev_ui->events = EV_READ;
364 bc10f6a5 2021-07-12 op event_set(&iev_ui->ev, iev_ui->ibuf.fd, iev_ui->events,
365 bc10f6a5 2021-07-12 op iev_ui->handler, iev_ui);
366 bc10f6a5 2021-07-12 op event_add(&iev_ui->ev, NULL);
368 35e1f40a 2021-03-14 op sandbox_fs_process();
370 35e1f40a 2021-03-14 op event_dispatch();
377 c6d03cf5 2021-04-25 op parse_khost_line(char *line, char *tmp[3])
381 c6d03cf5 2021-04-25 op for (ap = tmp; ap < &tmp[3] &&
382 c6d03cf5 2021-04-25 op (*ap = strsep(&line, " \t\n")) != NULL;) {
383 c6d03cf5 2021-04-25 op if (**ap != '\0')
387 c6d03cf5 2021-04-25 op return ap == &tmp[3] && *line == '\0';
391 3a227e9a 2021-03-18 op load_certs(struct ohash *h)
393 c6d03cf5 2021-04-25 op char *tmp[3], *line = NULL;
394 6cd6a9e1 2021-03-20 op const char *errstr;
395 ec1fa0b0 2021-04-25 op size_t lineno = 0, linesize = 0;
396 3a227e9a 2021-03-18 op ssize_t linelen;
398 3a227e9a 2021-03-18 op struct tofu_entry *e;
400 3a227e9a 2021-03-18 op if ((f = fopen(known_hosts_file, "r")) == NULL)
403 3a227e9a 2021-03-18 op while ((linelen = getline(&line, &linesize, f)) != -1) {
404 3a227e9a 2021-03-18 op if ((e = calloc(1, sizeof(*e))) == NULL)
409 c6d03cf5 2021-04-25 op if (parse_khost_line(line, tmp)) {
410 c6d03cf5 2021-04-25 op strlcpy(e->domain, tmp[0], sizeof(e->domain));
411 c6d03cf5 2021-04-25 op strlcpy(e->hash, tmp[1], sizeof(e->hash));
413 c6d03cf5 2021-04-25 op e->verified = strtonum(tmp[2], 0, 1, &errstr);
414 c6d03cf5 2021-04-25 op if (errstr != NULL)
415 c6d03cf5 2021-04-25 op errx(1, "%s:%zu verification for %s is %s: %s",
416 c6d03cf5 2021-04-25 op known_hosts_file, lineno,
417 c6d03cf5 2021-04-25 op e->domain, errstr, tmp[2]);
418 c6d03cf5 2021-04-25 op tofu_add(h, e);
420 ec1fa0b0 2021-04-25 op warnx("%s:%zu invalid entry",
421 ec1fa0b0 2021-04-25 op known_hosts_file, lineno);
427 3a227e9a 2021-03-18 op return ferror(f);
431 c7107cec 2021-04-01 op load_last_session(void (*cb)(const char*))
433 c7107cec 2021-04-01 op char *nl, *line = NULL;
435 c7107cec 2021-04-01 op size_t linesize = 0;
436 c7107cec 2021-04-01 op ssize_t linelen;
437 c7107cec 2021-04-01 op FILE *session;
439 51dc2fa4 2021-06-19 op if ((session = fopen(session_file, "r")) == NULL) {
440 51dc2fa4 2021-06-19 op /* first time? */
441 51dc2fa4 2021-06-19 op cb("about:help");
445 c7107cec 2021-04-01 op while ((linelen = getline(&line, &linesize, session)) != -1) {
446 c7107cec 2021-04-01 op if ((nl = strchr(line, '\n')) != NULL)
452 c7107cec 2021-04-01 op e = ferror(session);
453 c7107cec 2021-04-01 op fclose(session);