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 d0fd368a 2021-07-15 op static char lockfile_path[PATH_MAX];
55 740f578b 2021-03-15 op static char bookmark_file[PATH_MAX];
56 288fd238 2021-04-25 op static char known_hosts_file[PATH_MAX], known_hosts_tmp[PATH_MAX];
58 87e3e801 2021-07-17 op char session_file[PATH_MAX];
60 35e1f40a 2021-03-14 op static imsg_handlerfn *handlers[] = {
61 35e1f40a 2021-03-14 op [IMSG_GET] = handle_get,
62 35e1f40a 2021-03-14 op [IMSG_QUIT] = handle_quit,
63 740f578b 2021-03-15 op [IMSG_BOOKMARK_PAGE] = handle_bookmark_page,
64 3a227e9a 2021-03-18 op [IMSG_SAVE_CERT] = handle_save_cert,
65 288fd238 2021-04-25 op [IMSG_UPDATE_CERT] = handle_update_cert,
66 de2a69bb 2021-05-17 op [IMSG_FILE_OPEN] = handle_file_open,
67 c7107cec 2021-04-01 op [IMSG_SESSION_START] = handle_session_start,
68 c7107cec 2021-04-01 op [IMSG_SESSION_TAB] = handle_session_tab,
69 c7107cec 2021-04-01 op [IMSG_SESSION_END] = handle_session_end,
72 35e1f40a 2021-03-14 op static void __attribute__((__noreturn__))
75 35e1f40a 2021-03-14 op abort(); /* TODO */
79 35e1f40a 2021-03-14 op serve_bookmarks(uint32_t peerid)
81 35e1f40a 2021-03-14 op const char *t;
82 740f578b 2021-03-15 op char buf[BUFSIZ];
86 740f578b 2021-03-15 op if ((f = fopen(bookmark_file, "r")) == NULL) {
87 503425db 2021-04-01 op t = "# Bookmarks\n\n"
88 503425db 2021-04-01 op "No bookmarks yet!\n"
89 503425db 2021-04-01 op "Create ~/.telescope/bookmarks.gmi or use `bookmark-page'.\n";
90 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_BUF, peerid, -1, t, strlen(t));
91 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
96 35e1f40a 2021-03-14 op r = fread(buf, 1, sizeof(buf), f);
97 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_BUF, peerid, -1, buf, r);
98 35e1f40a 2021-03-14 op if (r != sizeof(buf))
102 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
108 1b412079 2021-06-19 op send_page(struct imsg *imsg, const char *page)
110 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, page, strlen(page));
111 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
115 35e1f40a 2021-03-14 op handle_get(struct imsg *imsg, size_t datalen)
118 35e1f40a 2021-03-14 op const char *p;
120 35e1f40a 2021-03-14 op data = imsg->data;
122 35e1f40a 2021-03-14 op if (data[datalen-1] != '\0')
125 1b412079 2021-06-19 op if (!strcmp(data, "about:about")) {
126 1b412079 2021-06-19 op send_page(imsg, about_about);
127 1b412079 2021-06-19 op } else if (!strcmp(data, "about:blank")) {
128 1b412079 2021-06-19 op send_page(imsg, about_blank);
129 35e1f40a 2021-03-14 op } else if (!strcmp(data, "about:bookmarks")) {
130 35e1f40a 2021-03-14 op serve_bookmarks(imsg->hdr.peerid);
131 1b412079 2021-06-19 op } else if (!strcmp(data, "about:help")) {
132 1b412079 2021-06-19 op send_page(imsg, about_help);
133 1b412079 2021-06-19 op } else if (!strcmp(data, "about:new")) {
134 1b412079 2021-06-19 op send_page(imsg, about_new);
136 35e1f40a 2021-03-14 op p = "# not found!\n";
137 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, p, strlen(p));
138 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
143 35e1f40a 2021-03-14 op handle_quit(struct imsg *imsg, size_t datalen)
145 35e1f40a 2021-03-14 op event_loopbreak();
149 740f578b 2021-03-15 op handle_bookmark_page(struct imsg *imsg, size_t datalen)
155 740f578b 2021-03-15 op data = imsg->data;
156 740f578b 2021-03-15 op if (data[datalen-1] != '\0')
159 740f578b 2021-03-15 op if ((f = fopen(bookmark_file, "a")) == NULL) {
163 740f578b 2021-03-15 op fprintf(f, "=> %s\n", data);
168 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_BOOKMARK_OK, 0, -1, &res, sizeof(res));
172 3a227e9a 2021-03-18 op handle_save_cert(struct imsg *imsg, size_t datalen)
174 3a227e9a 2021-03-18 op struct tofu_entry e;
178 3a227e9a 2021-03-18 op /* TODO: traverse the file to avoid duplications? */
180 3a227e9a 2021-03-18 op if (datalen != sizeof(e))
182 3a227e9a 2021-03-18 op memcpy(&e, imsg->data, datalen);
184 3a227e9a 2021-03-18 op if ((f = fopen(known_hosts_file, "a")) == NULL) {
188 3a227e9a 2021-03-18 op fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
193 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_SAVE_CERT_OK, imsg->hdr.peerid, -1,
194 288fd238 2021-04-25 op &res, sizeof(res));
198 288fd238 2021-04-25 op handle_update_cert(struct imsg *imsg, size_t datalen)
200 288fd238 2021-04-25 op FILE *tmp, *f;
201 288fd238 2021-04-25 op struct tofu_entry entry;
202 288fd238 2021-04-25 op char sfn[PATH_MAX], *line = NULL, *t;
203 288fd238 2021-04-25 op size_t l, linesize = 0;
204 288fd238 2021-04-25 op ssize_t linelen;
205 288fd238 2021-04-25 op int fd, e, res = 0;
207 288fd238 2021-04-25 op if (datalen != sizeof(entry))
209 288fd238 2021-04-25 op memcpy(&entry, imsg->data, datalen);
211 288fd238 2021-04-25 op strlcpy(sfn, known_hosts_tmp, sizeof(sfn));
212 288fd238 2021-04-25 op if ((fd = mkstemp(sfn)) == -1 ||
213 288fd238 2021-04-25 op (tmp = fdopen(fd, "w")) == NULL) {
214 288fd238 2021-04-25 op if (fd != -1) {
222 288fd238 2021-04-25 op if ((f = fopen(known_hosts_file, "r")) == NULL) {
229 288fd238 2021-04-25 op l = strlen(entry.domain);
230 288fd238 2021-04-25 op while ((linelen = getline(&line, &linesize, f)) != -1) {
231 288fd238 2021-04-25 op if ((t = strstr(line, entry.domain)) != NULL &&
232 288fd238 2021-04-25 op (line[l] == ' ' || line[l] == '\t'))
234 288fd238 2021-04-25 op /* line has a trailing \n */
235 288fd238 2021-04-25 op fprintf(tmp, "%s", line);
237 288fd238 2021-04-25 op fprintf(tmp, "%s %s %d\n", entry.domain, entry.hash, entry.verified);
240 288fd238 2021-04-25 op e = ferror(tmp);
251 288fd238 2021-04-25 op res = rename(sfn, known_hosts_file) != -1;
254 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_UPDATE_CERT_OK, imsg->hdr.peerid, -1,
255 3a227e9a 2021-03-18 op &res, sizeof(res));
259 de2a69bb 2021-05-17 op handle_file_open(struct imsg *imsg, size_t datalen)
261 de2a69bb 2021-05-17 op char *path, *e;
264 de2a69bb 2021-05-17 op path = imsg->data;
265 de2a69bb 2021-05-17 op if (path[datalen-1] != '\0')
268 de2a69bb 2021-05-17 op if ((fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644)) == -1) {
269 de2a69bb 2021-05-17 op e = strerror(errno);
270 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, -1,
271 de2a69bb 2021-05-17 op e, strlen(e)+1);
273 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, fd,
278 c7107cec 2021-04-01 op handle_session_start(struct imsg *imsg, size_t datalen)
280 c7107cec 2021-04-01 op if (datalen != 0)
283 c7107cec 2021-04-01 op if ((session = fopen(session_file, "w")) == NULL)
288 c7107cec 2021-04-01 op handle_session_tab(struct imsg *imsg, size_t datalen)
291 87e3e801 2021-07-17 op uint32_t flags;
293 c7107cec 2021-04-01 op if (session == NULL)
296 87e3e801 2021-07-17 op flags = imsg->hdr.peerid;
297 58df4f47 2021-07-17 op url = imsg->data;
298 58df4f47 2021-07-17 op if (datalen == 0 || url[datalen-1] != '\0')
300 58df4f47 2021-07-17 op fprintf(session, "%s", url);
302 87e3e801 2021-07-17 op if (flags & TAB_CURRENT)
303 87e3e801 2021-07-17 op fprintf(session, " current");
305 c7107cec 2021-04-01 op fprintf(session, "\n");
309 c7107cec 2021-04-01 op handle_session_end(struct imsg *imsg, size_t datalen)
311 c7107cec 2021-04-01 op if (session == NULL)
313 c7107cec 2021-04-01 op fclose(session);
314 c7107cec 2021-04-01 op session = NULL;
318 1304bbdd 2021-03-15 op handle_dispatch_imsg(int fd, short ev, void *d)
320 bc10f6a5 2021-07-12 op struct imsgev *iev = d;
321 bc10f6a5 2021-07-12 op dispatch_imsg(iev, ev, handlers, sizeof(handlers));
325 bc10f6a5 2021-07-12 op fs_send_ui(int type, uint32_t peerid, int fd, const void *data,
326 bc10f6a5 2021-07-12 op uint16_t datalen)
328 bc10f6a5 2021-07-12 op return imsg_compose_event(iev_ui, type, peerid, 0, fd,
329 bc10f6a5 2021-07-12 op data, datalen);
333 3a227e9a 2021-03-18 op fs_init(void)
335 63a715ea 2021-03-18 op char dir[PATH_MAX];
337 63a715ea 2021-03-18 op strlcpy(dir, getenv("HOME"), sizeof(dir));
338 63a715ea 2021-03-18 op strlcat(dir, "/.telescope", sizeof(dir));
339 63a715ea 2021-03-18 op mkdir(dir, 0700);
341 d0fd368a 2021-07-15 op strlcpy(lockfile_path, getenv("HOME"), sizeof(lockfile_path));
342 d0fd368a 2021-07-15 op strlcat(lockfile_path, "/.telescope/lock", sizeof(lockfile_path));
344 740f578b 2021-03-15 op strlcpy(bookmark_file, getenv("HOME"), sizeof(bookmark_file));
345 740f578b 2021-03-15 op strlcat(bookmark_file, "/.telescope/bookmarks.gmi", sizeof(bookmark_file));
347 3a227e9a 2021-03-18 op strlcpy(known_hosts_file, getenv("HOME"), sizeof(known_hosts_file));
348 3a227e9a 2021-03-18 op strlcat(known_hosts_file, "/.telescope/known_hosts", sizeof(known_hosts_file));
350 288fd238 2021-04-25 op strlcpy(known_hosts_tmp, getenv("HOME"), sizeof(known_hosts_tmp));
351 288fd238 2021-04-25 op strlcat(known_hosts_tmp, "/.telescope/known_hosts.tmp.XXXXXXXXXX",
352 288fd238 2021-04-25 op sizeof(known_hosts_file));
354 c7107cec 2021-04-01 op strlcpy(session_file, getenv("HOME"), sizeof(session_file));
355 c7107cec 2021-04-01 op strlcat(session_file, "/.telescope/session", sizeof(session_file));
361 6cc5fcfe 2021-07-08 op fs_main(void)
363 6cc5fcfe 2021-07-08 op setproctitle("fs");
367 35e1f40a 2021-03-14 op event_init();
369 bc10f6a5 2021-07-12 op /* Setup pipe and event handler to the main process */
370 bc10f6a5 2021-07-12 op if ((iev_ui = malloc(sizeof(*iev_ui))) == NULL)
372 bc10f6a5 2021-07-12 op imsg_init(&iev_ui->ibuf, 3);
373 bc10f6a5 2021-07-12 op iev_ui->handler = handle_dispatch_imsg;
374 bc10f6a5 2021-07-12 op iev_ui->events = EV_READ;
375 bc10f6a5 2021-07-12 op event_set(&iev_ui->ev, iev_ui->ibuf.fd, iev_ui->events,
376 bc10f6a5 2021-07-12 op iev_ui->handler, iev_ui);
377 bc10f6a5 2021-07-12 op event_add(&iev_ui->ev, NULL);
379 35e1f40a 2021-03-14 op sandbox_fs_process();
381 35e1f40a 2021-03-14 op event_dispatch();
388 d0fd368a 2021-07-15 op lock_session(void)
390 d0fd368a 2021-07-15 op struct flock lock;
393 d0fd368a 2021-07-15 op if ((fd = open(lockfile_path, O_WRONLY|O_CREAT, 0600)) == -1)
396 d0fd368a 2021-07-15 op lock.l_start = 0;
397 d0fd368a 2021-07-15 op lock.l_len = 0;
398 d0fd368a 2021-07-15 op lock.l_type = F_WRLCK;
399 d0fd368a 2021-07-15 op lock.l_whence = SEEK_SET;
401 d0fd368a 2021-07-15 op if (fcntl(fd, F_SETLK, &lock) == -1) {
410 c6d03cf5 2021-04-25 op parse_khost_line(char *line, char *tmp[3])
414 c6d03cf5 2021-04-25 op for (ap = tmp; ap < &tmp[3] &&
415 c6d03cf5 2021-04-25 op (*ap = strsep(&line, " \t\n")) != NULL;) {
416 c6d03cf5 2021-04-25 op if (**ap != '\0')
420 c6d03cf5 2021-04-25 op return ap == &tmp[3] && *line == '\0';
424 3a227e9a 2021-03-18 op load_certs(struct ohash *h)
426 c6d03cf5 2021-04-25 op char *tmp[3], *line = NULL;
427 6cd6a9e1 2021-03-20 op const char *errstr;
428 ec1fa0b0 2021-04-25 op size_t lineno = 0, linesize = 0;
429 3a227e9a 2021-03-18 op ssize_t linelen;
431 3a227e9a 2021-03-18 op struct tofu_entry *e;
433 3a227e9a 2021-03-18 op if ((f = fopen(known_hosts_file, "r")) == NULL)
436 3a227e9a 2021-03-18 op while ((linelen = getline(&line, &linesize, f)) != -1) {
437 3a227e9a 2021-03-18 op if ((e = calloc(1, sizeof(*e))) == NULL)
442 c6d03cf5 2021-04-25 op if (parse_khost_line(line, tmp)) {
443 c6d03cf5 2021-04-25 op strlcpy(e->domain, tmp[0], sizeof(e->domain));
444 c6d03cf5 2021-04-25 op strlcpy(e->hash, tmp[1], sizeof(e->hash));
446 c6d03cf5 2021-04-25 op e->verified = strtonum(tmp[2], 0, 1, &errstr);
447 c6d03cf5 2021-04-25 op if (errstr != NULL)
448 c6d03cf5 2021-04-25 op errx(1, "%s:%zu verification for %s is %s: %s",
449 c6d03cf5 2021-04-25 op known_hosts_file, lineno,
450 c6d03cf5 2021-04-25 op e->domain, errstr, tmp[2]);
451 c6d03cf5 2021-04-25 op tofu_add(h, e);
453 ec1fa0b0 2021-04-25 op warnx("%s:%zu invalid entry",
454 ec1fa0b0 2021-04-25 op known_hosts_file, lineno);
460 3a227e9a 2021-03-18 op return ferror(f);