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"
35 35e1f40a 2021-03-14 op static void die(void) __attribute__((__noreturn__));
36 35e1f40a 2021-03-14 op static void serve_bookmarks(uint32_t);
37 1b412079 2021-06-19 op static void send_page(struct imsg *, const char *);
38 35e1f40a 2021-03-14 op static void handle_get(struct imsg*, size_t);
39 35e1f40a 2021-03-14 op static void handle_quit(struct imsg*, size_t);
40 740f578b 2021-03-15 op static void handle_bookmark_page(struct imsg*, size_t);
41 3a227e9a 2021-03-18 op static void handle_save_cert(struct imsg*, size_t);
42 288fd238 2021-04-25 op static void handle_update_cert(struct imsg*, size_t);
43 de2a69bb 2021-05-17 op static void handle_file_open(struct imsg*, size_t);
44 c7107cec 2021-04-01 op static void handle_session_start(struct imsg*, size_t);
45 c7107cec 2021-04-01 op static void handle_session_tab(struct imsg*, size_t);
46 c7107cec 2021-04-01 op static void handle_session_end(struct imsg*, size_t);
47 1304bbdd 2021-03-15 op static void handle_dispatch_imsg(int, short, void*);
48 bc10f6a5 2021-07-12 op static int fs_send_ui(int, uint32_t, int, const void *, uint16_t);
50 bc10f6a5 2021-07-12 op static struct imsgev *iev_ui;
51 c7107cec 2021-04-01 op static FILE *session;
53 740f578b 2021-03-15 op static char bookmark_file[PATH_MAX];
54 288fd238 2021-04-25 op static char known_hosts_file[PATH_MAX], known_hosts_tmp[PATH_MAX];
55 c7107cec 2021-04-01 op static char session_file[PATH_MAX];
57 35e1f40a 2021-03-14 op static imsg_handlerfn *handlers[] = {
58 35e1f40a 2021-03-14 op [IMSG_GET] = handle_get,
59 35e1f40a 2021-03-14 op [IMSG_QUIT] = handle_quit,
60 740f578b 2021-03-15 op [IMSG_BOOKMARK_PAGE] = handle_bookmark_page,
61 3a227e9a 2021-03-18 op [IMSG_SAVE_CERT] = handle_save_cert,
62 288fd238 2021-04-25 op [IMSG_UPDATE_CERT] = handle_update_cert,
63 de2a69bb 2021-05-17 op [IMSG_FILE_OPEN] = handle_file_open,
64 c7107cec 2021-04-01 op [IMSG_SESSION_START] = handle_session_start,
65 c7107cec 2021-04-01 op [IMSG_SESSION_TAB] = handle_session_tab,
66 c7107cec 2021-04-01 op [IMSG_SESSION_END] = handle_session_end,
69 35e1f40a 2021-03-14 op static void __attribute__((__noreturn__))
72 35e1f40a 2021-03-14 op abort(); /* TODO */
76 35e1f40a 2021-03-14 op serve_bookmarks(uint32_t peerid)
78 35e1f40a 2021-03-14 op const char *t;
79 740f578b 2021-03-15 op char buf[BUFSIZ];
83 740f578b 2021-03-15 op if ((f = fopen(bookmark_file, "r")) == NULL) {
84 503425db 2021-04-01 op t = "# Bookmarks\n\n"
85 503425db 2021-04-01 op "No bookmarks yet!\n"
86 503425db 2021-04-01 op "Create ~/.telescope/bookmarks.gmi or use `bookmark-page'.\n";
87 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_BUF, peerid, -1, t, strlen(t));
88 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
93 35e1f40a 2021-03-14 op r = fread(buf, 1, sizeof(buf), f);
94 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_BUF, peerid, -1, buf, r);
95 35e1f40a 2021-03-14 op if (r != sizeof(buf))
99 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
105 1b412079 2021-06-19 op send_page(struct imsg *imsg, const char *page)
107 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, page, strlen(page));
108 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
112 35e1f40a 2021-03-14 op handle_get(struct imsg *imsg, size_t datalen)
115 35e1f40a 2021-03-14 op const char *p;
117 35e1f40a 2021-03-14 op data = imsg->data;
119 35e1f40a 2021-03-14 op if (data[datalen-1] != '\0')
122 1b412079 2021-06-19 op if (!strcmp(data, "about:about")) {
123 1b412079 2021-06-19 op send_page(imsg, about_about);
124 1b412079 2021-06-19 op } else if (!strcmp(data, "about:blank")) {
125 1b412079 2021-06-19 op send_page(imsg, about_blank);
126 35e1f40a 2021-03-14 op } else if (!strcmp(data, "about:bookmarks")) {
127 35e1f40a 2021-03-14 op serve_bookmarks(imsg->hdr.peerid);
128 1b412079 2021-06-19 op } else if (!strcmp(data, "about:help")) {
129 1b412079 2021-06-19 op send_page(imsg, about_help);
130 1b412079 2021-06-19 op } else if (!strcmp(data, "about:new")) {
131 1b412079 2021-06-19 op send_page(imsg, about_new);
133 35e1f40a 2021-03-14 op p = "# not found!\n";
134 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, p, strlen(p));
135 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
140 35e1f40a 2021-03-14 op handle_quit(struct imsg *imsg, size_t datalen)
142 35e1f40a 2021-03-14 op event_loopbreak();
146 740f578b 2021-03-15 op handle_bookmark_page(struct imsg *imsg, size_t datalen)
152 740f578b 2021-03-15 op data = imsg->data;
153 740f578b 2021-03-15 op if (data[datalen-1] != '\0')
156 740f578b 2021-03-15 op if ((f = fopen(bookmark_file, "a")) == NULL) {
160 740f578b 2021-03-15 op fprintf(f, "=> %s\n", data);
165 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_BOOKMARK_OK, 0, -1, &res, sizeof(res));
169 3a227e9a 2021-03-18 op handle_save_cert(struct imsg *imsg, size_t datalen)
171 3a227e9a 2021-03-18 op struct tofu_entry e;
175 3a227e9a 2021-03-18 op /* TODO: traverse the file to avoid duplications? */
177 3a227e9a 2021-03-18 op if (datalen != sizeof(e))
179 3a227e9a 2021-03-18 op memcpy(&e, imsg->data, datalen);
181 3a227e9a 2021-03-18 op if ((f = fopen(known_hosts_file, "a")) == NULL) {
185 3a227e9a 2021-03-18 op fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
190 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_SAVE_CERT_OK, imsg->hdr.peerid, -1,
191 288fd238 2021-04-25 op &res, sizeof(res));
195 288fd238 2021-04-25 op handle_update_cert(struct imsg *imsg, size_t datalen)
197 288fd238 2021-04-25 op FILE *tmp, *f;
198 288fd238 2021-04-25 op struct tofu_entry entry;
199 288fd238 2021-04-25 op char sfn[PATH_MAX], *line = NULL, *t;
200 288fd238 2021-04-25 op size_t l, linesize = 0;
201 288fd238 2021-04-25 op ssize_t linelen;
202 288fd238 2021-04-25 op int fd, e, res = 0;
204 288fd238 2021-04-25 op if (datalen != sizeof(entry))
206 288fd238 2021-04-25 op memcpy(&entry, imsg->data, datalen);
208 288fd238 2021-04-25 op strlcpy(sfn, known_hosts_tmp, sizeof(sfn));
209 288fd238 2021-04-25 op if ((fd = mkstemp(sfn)) == -1 ||
210 288fd238 2021-04-25 op (tmp = fdopen(fd, "w")) == NULL) {
211 288fd238 2021-04-25 op if (fd != -1) {
219 288fd238 2021-04-25 op if ((f = fopen(known_hosts_file, "r")) == NULL) {
226 288fd238 2021-04-25 op l = strlen(entry.domain);
227 288fd238 2021-04-25 op while ((linelen = getline(&line, &linesize, f)) != -1) {
228 288fd238 2021-04-25 op if ((t = strstr(line, entry.domain)) != NULL &&
229 288fd238 2021-04-25 op (line[l] == ' ' || line[l] == '\t'))
231 288fd238 2021-04-25 op /* line has a trailing \n */
232 288fd238 2021-04-25 op fprintf(tmp, "%s", line);
234 288fd238 2021-04-25 op fprintf(tmp, "%s %s %d\n", entry.domain, entry.hash, entry.verified);
237 288fd238 2021-04-25 op e = ferror(tmp);
248 288fd238 2021-04-25 op res = rename(sfn, known_hosts_file) != -1;
251 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_UPDATE_CERT_OK, imsg->hdr.peerid, -1,
252 3a227e9a 2021-03-18 op &res, sizeof(res));
256 de2a69bb 2021-05-17 op handle_file_open(struct imsg *imsg, size_t datalen)
258 de2a69bb 2021-05-17 op char *path, *e;
261 de2a69bb 2021-05-17 op path = imsg->data;
262 de2a69bb 2021-05-17 op if (path[datalen-1] != '\0')
265 de2a69bb 2021-05-17 op if ((fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644)) == -1) {
266 de2a69bb 2021-05-17 op e = strerror(errno);
267 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, -1,
268 de2a69bb 2021-05-17 op e, strlen(e)+1);
270 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, fd,
275 c7107cec 2021-04-01 op handle_session_start(struct imsg *imsg, size_t datalen)
277 c7107cec 2021-04-01 op if (datalen != 0)
280 c7107cec 2021-04-01 op if ((session = fopen(session_file, "w")) == NULL)
285 c7107cec 2021-04-01 op handle_session_tab(struct imsg *imsg, size_t datalen)
287 c7107cec 2021-04-01 op if (session == NULL)
290 c7107cec 2021-04-01 op if (datalen == 0)
293 c7107cec 2021-04-01 op /* skip the NUL-terminator */
294 c7107cec 2021-04-01 op fwrite(imsg->data, 1, datalen-1, session);
296 c7107cec 2021-04-01 op fprintf(session, "\n");
300 c7107cec 2021-04-01 op handle_session_end(struct imsg *imsg, size_t datalen)
302 c7107cec 2021-04-01 op if (session == NULL)
304 c7107cec 2021-04-01 op fclose(session);
305 c7107cec 2021-04-01 op session = NULL;
309 1304bbdd 2021-03-15 op handle_dispatch_imsg(int fd, short ev, void *d)
311 bc10f6a5 2021-07-12 op struct imsgev *iev = d;
312 bc10f6a5 2021-07-12 op dispatch_imsg(iev, ev, handlers, sizeof(handlers));
316 bc10f6a5 2021-07-12 op fs_send_ui(int type, uint32_t peerid, int fd, const void *data,
317 bc10f6a5 2021-07-12 op uint16_t datalen)
319 bc10f6a5 2021-07-12 op return imsg_compose_event(iev_ui, type, peerid, 0, fd,
320 bc10f6a5 2021-07-12 op data, datalen);
324 3a227e9a 2021-03-18 op fs_init(void)
326 63a715ea 2021-03-18 op char dir[PATH_MAX];
328 63a715ea 2021-03-18 op strlcpy(dir, getenv("HOME"), sizeof(dir));
329 63a715ea 2021-03-18 op strlcat(dir, "/.telescope", sizeof(dir));
330 63a715ea 2021-03-18 op mkdir(dir, 0700);
332 740f578b 2021-03-15 op strlcpy(bookmark_file, getenv("HOME"), sizeof(bookmark_file));
333 740f578b 2021-03-15 op strlcat(bookmark_file, "/.telescope/bookmarks.gmi", sizeof(bookmark_file));
335 3a227e9a 2021-03-18 op strlcpy(known_hosts_file, getenv("HOME"), sizeof(known_hosts_file));
336 3a227e9a 2021-03-18 op strlcat(known_hosts_file, "/.telescope/known_hosts", sizeof(known_hosts_file));
338 288fd238 2021-04-25 op strlcpy(known_hosts_tmp, getenv("HOME"), sizeof(known_hosts_tmp));
339 288fd238 2021-04-25 op strlcat(known_hosts_tmp, "/.telescope/known_hosts.tmp.XXXXXXXXXX",
340 288fd238 2021-04-25 op sizeof(known_hosts_file));
342 c7107cec 2021-04-01 op strlcpy(session_file, getenv("HOME"), sizeof(session_file));
343 c7107cec 2021-04-01 op strlcat(session_file, "/.telescope/session", sizeof(session_file));
349 6cc5fcfe 2021-07-08 op fs_main(void)
351 6cc5fcfe 2021-07-08 op setproctitle("fs");
355 35e1f40a 2021-03-14 op event_init();
357 bc10f6a5 2021-07-12 op /* Setup pipe and event handler to the main process */
358 bc10f6a5 2021-07-12 op if ((iev_ui = malloc(sizeof(*iev_ui))) == NULL)
360 bc10f6a5 2021-07-12 op imsg_init(&iev_ui->ibuf, 3);
361 bc10f6a5 2021-07-12 op iev_ui->handler = handle_dispatch_imsg;
362 bc10f6a5 2021-07-12 op iev_ui->events = EV_READ;
363 bc10f6a5 2021-07-12 op event_set(&iev_ui->ev, iev_ui->ibuf.fd, iev_ui->events,
364 bc10f6a5 2021-07-12 op iev_ui->handler, iev_ui);
365 bc10f6a5 2021-07-12 op event_add(&iev_ui->ev, NULL);
367 35e1f40a 2021-03-14 op sandbox_fs_process();
369 35e1f40a 2021-03-14 op event_dispatch();
376 c6d03cf5 2021-04-25 op parse_khost_line(char *line, char *tmp[3])
380 c6d03cf5 2021-04-25 op for (ap = tmp; ap < &tmp[3] &&
381 c6d03cf5 2021-04-25 op (*ap = strsep(&line, " \t\n")) != NULL;) {
382 c6d03cf5 2021-04-25 op if (**ap != '\0')
386 c6d03cf5 2021-04-25 op return ap == &tmp[3] && *line == '\0';
390 3a227e9a 2021-03-18 op load_certs(struct ohash *h)
392 c6d03cf5 2021-04-25 op char *tmp[3], *line = NULL;
393 6cd6a9e1 2021-03-20 op const char *errstr;
394 ec1fa0b0 2021-04-25 op size_t lineno = 0, linesize = 0;
395 3a227e9a 2021-03-18 op ssize_t linelen;
397 3a227e9a 2021-03-18 op struct tofu_entry *e;
399 3a227e9a 2021-03-18 op if ((f = fopen(known_hosts_file, "r")) == NULL)
402 3a227e9a 2021-03-18 op while ((linelen = getline(&line, &linesize, f)) != -1) {
403 3a227e9a 2021-03-18 op if ((e = calloc(1, sizeof(*e))) == NULL)
408 c6d03cf5 2021-04-25 op if (parse_khost_line(line, tmp)) {
409 c6d03cf5 2021-04-25 op strlcpy(e->domain, tmp[0], sizeof(e->domain));
410 c6d03cf5 2021-04-25 op strlcpy(e->hash, tmp[1], sizeof(e->hash));
412 c6d03cf5 2021-04-25 op e->verified = strtonum(tmp[2], 0, 1, &errstr);
413 c6d03cf5 2021-04-25 op if (errstr != NULL)
414 c6d03cf5 2021-04-25 op errx(1, "%s:%zu verification for %s is %s: %s",
415 c6d03cf5 2021-04-25 op known_hosts_file, lineno,
416 c6d03cf5 2021-04-25 op e->domain, errstr, tmp[2]);
417 c6d03cf5 2021-04-25 op tofu_add(h, e);
419 ec1fa0b0 2021-04-25 op warnx("%s:%zu invalid entry",
420 ec1fa0b0 2021-04-25 op known_hosts_file, lineno);
426 3a227e9a 2021-03-18 op return ferror(f);
430 c7107cec 2021-04-01 op load_last_session(void (*cb)(const char*))
432 c7107cec 2021-04-01 op char *nl, *line = NULL;
434 c7107cec 2021-04-01 op size_t linesize = 0;
435 c7107cec 2021-04-01 op ssize_t linelen;
436 c7107cec 2021-04-01 op FILE *session;
438 51dc2fa4 2021-06-19 op if ((session = fopen(session_file, "r")) == NULL) {
439 51dc2fa4 2021-06-19 op /* first time? */
440 51dc2fa4 2021-06-19 op cb("about:help");
444 c7107cec 2021-04-01 op while ((linelen = getline(&line, &linesize, session)) != -1) {
445 c7107cec 2021-04-01 op if ((nl = strchr(line, '\n')) != NULL)
451 c7107cec 2021-04-01 op e = ferror(session);
452 c7107cec 2021-04-01 op fclose(session);