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 2f524b17 2021-07-17 op static void handle_session_tab_title(struct imsg*, size_t);
48 c7107cec 2021-04-01 op static void handle_session_end(struct imsg*, size_t);
49 1304bbdd 2021-03-15 op static void handle_dispatch_imsg(int, short, void*);
50 bc10f6a5 2021-07-12 op static int fs_send_ui(int, uint32_t, int, const void *, uint16_t);
52 bc10f6a5 2021-07-12 op static struct imsgev *iev_ui;
53 c7107cec 2021-04-01 op static FILE *session;
55 d0fd368a 2021-07-15 op static char lockfile_path[PATH_MAX];
56 740f578b 2021-03-15 op static char bookmark_file[PATH_MAX];
57 288fd238 2021-04-25 op static char known_hosts_file[PATH_MAX], known_hosts_tmp[PATH_MAX];
59 87e3e801 2021-07-17 op char session_file[PATH_MAX];
61 35e1f40a 2021-03-14 op static imsg_handlerfn *handlers[] = {
62 35e1f40a 2021-03-14 op [IMSG_GET] = handle_get,
63 35e1f40a 2021-03-14 op [IMSG_QUIT] = handle_quit,
64 740f578b 2021-03-15 op [IMSG_BOOKMARK_PAGE] = handle_bookmark_page,
65 3a227e9a 2021-03-18 op [IMSG_SAVE_CERT] = handle_save_cert,
66 288fd238 2021-04-25 op [IMSG_UPDATE_CERT] = handle_update_cert,
67 de2a69bb 2021-05-17 op [IMSG_FILE_OPEN] = handle_file_open,
68 c7107cec 2021-04-01 op [IMSG_SESSION_START] = handle_session_start,
69 c7107cec 2021-04-01 op [IMSG_SESSION_TAB] = handle_session_tab,
70 2f524b17 2021-07-17 op [IMSG_SESSION_TAB_TITLE] = handle_session_tab_title,
71 c7107cec 2021-04-01 op [IMSG_SESSION_END] = handle_session_end,
74 35e1f40a 2021-03-14 op static void __attribute__((__noreturn__))
77 35e1f40a 2021-03-14 op abort(); /* TODO */
81 35e1f40a 2021-03-14 op serve_bookmarks(uint32_t peerid)
83 35e1f40a 2021-03-14 op const char *t;
84 740f578b 2021-03-15 op char buf[BUFSIZ];
88 740f578b 2021-03-15 op if ((f = fopen(bookmark_file, "r")) == NULL) {
89 503425db 2021-04-01 op t = "# Bookmarks\n\n"
90 503425db 2021-04-01 op "No bookmarks yet!\n"
91 503425db 2021-04-01 op "Create ~/.telescope/bookmarks.gmi or use `bookmark-page'.\n";
92 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_BUF, peerid, -1, t, strlen(t));
93 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
98 35e1f40a 2021-03-14 op r = fread(buf, 1, sizeof(buf), f);
99 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_BUF, peerid, -1, buf, r);
100 35e1f40a 2021-03-14 op if (r != sizeof(buf))
104 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
110 1b412079 2021-06-19 op send_page(struct imsg *imsg, const char *page)
112 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, page, strlen(page));
113 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
117 35e1f40a 2021-03-14 op handle_get(struct imsg *imsg, size_t datalen)
120 35e1f40a 2021-03-14 op const char *p;
122 35e1f40a 2021-03-14 op data = imsg->data;
124 35e1f40a 2021-03-14 op if (data[datalen-1] != '\0')
127 1b412079 2021-06-19 op if (!strcmp(data, "about:about")) {
128 1b412079 2021-06-19 op send_page(imsg, about_about);
129 1b412079 2021-06-19 op } else if (!strcmp(data, "about:blank")) {
130 1b412079 2021-06-19 op send_page(imsg, about_blank);
131 35e1f40a 2021-03-14 op } else if (!strcmp(data, "about:bookmarks")) {
132 35e1f40a 2021-03-14 op serve_bookmarks(imsg->hdr.peerid);
133 1b412079 2021-06-19 op } else if (!strcmp(data, "about:help")) {
134 1b412079 2021-06-19 op send_page(imsg, about_help);
135 1b412079 2021-06-19 op } else if (!strcmp(data, "about:new")) {
136 1b412079 2021-06-19 op send_page(imsg, about_new);
138 35e1f40a 2021-03-14 op p = "# not found!\n";
139 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, p, strlen(p));
140 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
145 35e1f40a 2021-03-14 op handle_quit(struct imsg *imsg, size_t datalen)
147 35e1f40a 2021-03-14 op event_loopbreak();
151 740f578b 2021-03-15 op handle_bookmark_page(struct imsg *imsg, size_t datalen)
157 740f578b 2021-03-15 op data = imsg->data;
158 740f578b 2021-03-15 op if (data[datalen-1] != '\0')
161 740f578b 2021-03-15 op if ((f = fopen(bookmark_file, "a")) == NULL) {
165 740f578b 2021-03-15 op fprintf(f, "=> %s\n", data);
170 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_BOOKMARK_OK, 0, -1, &res, sizeof(res));
174 3a227e9a 2021-03-18 op handle_save_cert(struct imsg *imsg, size_t datalen)
176 3a227e9a 2021-03-18 op struct tofu_entry e;
180 3a227e9a 2021-03-18 op /* TODO: traverse the file to avoid duplications? */
182 3a227e9a 2021-03-18 op if (datalen != sizeof(e))
184 3a227e9a 2021-03-18 op memcpy(&e, imsg->data, datalen);
186 3a227e9a 2021-03-18 op if ((f = fopen(known_hosts_file, "a")) == NULL) {
190 3a227e9a 2021-03-18 op fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
195 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_SAVE_CERT_OK, imsg->hdr.peerid, -1,
196 288fd238 2021-04-25 op &res, sizeof(res));
200 288fd238 2021-04-25 op handle_update_cert(struct imsg *imsg, size_t datalen)
202 288fd238 2021-04-25 op FILE *tmp, *f;
203 288fd238 2021-04-25 op struct tofu_entry entry;
204 288fd238 2021-04-25 op char sfn[PATH_MAX], *line = NULL, *t;
205 288fd238 2021-04-25 op size_t l, linesize = 0;
206 288fd238 2021-04-25 op ssize_t linelen;
207 288fd238 2021-04-25 op int fd, e, res = 0;
209 288fd238 2021-04-25 op if (datalen != sizeof(entry))
211 288fd238 2021-04-25 op memcpy(&entry, imsg->data, datalen);
213 288fd238 2021-04-25 op strlcpy(sfn, known_hosts_tmp, sizeof(sfn));
214 288fd238 2021-04-25 op if ((fd = mkstemp(sfn)) == -1 ||
215 288fd238 2021-04-25 op (tmp = fdopen(fd, "w")) == NULL) {
216 288fd238 2021-04-25 op if (fd != -1) {
224 288fd238 2021-04-25 op if ((f = fopen(known_hosts_file, "r")) == NULL) {
231 288fd238 2021-04-25 op l = strlen(entry.domain);
232 288fd238 2021-04-25 op while ((linelen = getline(&line, &linesize, f)) != -1) {
233 288fd238 2021-04-25 op if ((t = strstr(line, entry.domain)) != NULL &&
234 288fd238 2021-04-25 op (line[l] == ' ' || line[l] == '\t'))
236 288fd238 2021-04-25 op /* line has a trailing \n */
237 288fd238 2021-04-25 op fprintf(tmp, "%s", line);
239 288fd238 2021-04-25 op fprintf(tmp, "%s %s %d\n", entry.domain, entry.hash, entry.verified);
242 288fd238 2021-04-25 op e = ferror(tmp);
253 288fd238 2021-04-25 op res = rename(sfn, known_hosts_file) != -1;
256 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_UPDATE_CERT_OK, imsg->hdr.peerid, -1,
257 3a227e9a 2021-03-18 op &res, sizeof(res));
261 de2a69bb 2021-05-17 op handle_file_open(struct imsg *imsg, size_t datalen)
263 de2a69bb 2021-05-17 op char *path, *e;
266 de2a69bb 2021-05-17 op path = imsg->data;
267 de2a69bb 2021-05-17 op if (path[datalen-1] != '\0')
270 de2a69bb 2021-05-17 op if ((fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644)) == -1) {
271 de2a69bb 2021-05-17 op e = strerror(errno);
272 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, -1,
273 de2a69bb 2021-05-17 op e, strlen(e)+1);
275 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, fd,
280 c7107cec 2021-04-01 op handle_session_start(struct imsg *imsg, size_t datalen)
282 c7107cec 2021-04-01 op if (datalen != 0)
285 c7107cec 2021-04-01 op if ((session = fopen(session_file, "w")) == NULL)
290 c7107cec 2021-04-01 op handle_session_tab(struct imsg *imsg, size_t datalen)
293 87e3e801 2021-07-17 op uint32_t flags;
295 c7107cec 2021-04-01 op if (session == NULL)
298 87e3e801 2021-07-17 op flags = imsg->hdr.peerid;
299 58df4f47 2021-07-17 op url = imsg->data;
300 58df4f47 2021-07-17 op if (datalen == 0 || url[datalen-1] != '\0')
302 58df4f47 2021-07-17 op fprintf(session, "%s", url);
304 87e3e801 2021-07-17 op if (flags & TAB_CURRENT)
305 2f524b17 2021-07-17 op fprintf(session, " current ");
307 2f524b17 2021-07-17 op fprintf(session, " - ");
311 2f524b17 2021-07-17 op handle_session_tab_title(struct imsg *imsg, size_t datalen)
313 2f524b17 2021-07-17 op const char *title;
315 2f524b17 2021-07-17 op title = imsg->data;
316 2f524b17 2021-07-17 op if (title == NULL) {
321 2f524b17 2021-07-17 op if (title[datalen-1] != '\0')
324 2f524b17 2021-07-17 op fprintf(session, "%s\n", title);
328 c7107cec 2021-04-01 op handle_session_end(struct imsg *imsg, size_t datalen)
330 c7107cec 2021-04-01 op if (session == NULL)
332 c7107cec 2021-04-01 op fclose(session);
333 c7107cec 2021-04-01 op session = NULL;
337 1304bbdd 2021-03-15 op handle_dispatch_imsg(int fd, short ev, void *d)
339 bc10f6a5 2021-07-12 op struct imsgev *iev = d;
340 bc10f6a5 2021-07-12 op dispatch_imsg(iev, ev, handlers, sizeof(handlers));
344 bc10f6a5 2021-07-12 op fs_send_ui(int type, uint32_t peerid, int fd, const void *data,
345 bc10f6a5 2021-07-12 op uint16_t datalen)
347 bc10f6a5 2021-07-12 op return imsg_compose_event(iev_ui, type, peerid, 0, fd,
348 bc10f6a5 2021-07-12 op data, datalen);
352 3a227e9a 2021-03-18 op fs_init(void)
354 63a715ea 2021-03-18 op char dir[PATH_MAX];
356 63a715ea 2021-03-18 op strlcpy(dir, getenv("HOME"), sizeof(dir));
357 63a715ea 2021-03-18 op strlcat(dir, "/.telescope", sizeof(dir));
358 63a715ea 2021-03-18 op mkdir(dir, 0700);
360 d0fd368a 2021-07-15 op strlcpy(lockfile_path, getenv("HOME"), sizeof(lockfile_path));
361 d0fd368a 2021-07-15 op strlcat(lockfile_path, "/.telescope/lock", sizeof(lockfile_path));
363 740f578b 2021-03-15 op strlcpy(bookmark_file, getenv("HOME"), sizeof(bookmark_file));
364 740f578b 2021-03-15 op strlcat(bookmark_file, "/.telescope/bookmarks.gmi", sizeof(bookmark_file));
366 3a227e9a 2021-03-18 op strlcpy(known_hosts_file, getenv("HOME"), sizeof(known_hosts_file));
367 3a227e9a 2021-03-18 op strlcat(known_hosts_file, "/.telescope/known_hosts", sizeof(known_hosts_file));
369 288fd238 2021-04-25 op strlcpy(known_hosts_tmp, getenv("HOME"), sizeof(known_hosts_tmp));
370 288fd238 2021-04-25 op strlcat(known_hosts_tmp, "/.telescope/known_hosts.tmp.XXXXXXXXXX",
371 288fd238 2021-04-25 op sizeof(known_hosts_file));
373 c7107cec 2021-04-01 op strlcpy(session_file, getenv("HOME"), sizeof(session_file));
374 c7107cec 2021-04-01 op strlcat(session_file, "/.telescope/session", sizeof(session_file));
380 6cc5fcfe 2021-07-08 op fs_main(void)
382 6cc5fcfe 2021-07-08 op setproctitle("fs");
386 35e1f40a 2021-03-14 op event_init();
388 bc10f6a5 2021-07-12 op /* Setup pipe and event handler to the main process */
389 bc10f6a5 2021-07-12 op if ((iev_ui = malloc(sizeof(*iev_ui))) == NULL)
391 bc10f6a5 2021-07-12 op imsg_init(&iev_ui->ibuf, 3);
392 bc10f6a5 2021-07-12 op iev_ui->handler = handle_dispatch_imsg;
393 bc10f6a5 2021-07-12 op iev_ui->events = EV_READ;
394 bc10f6a5 2021-07-12 op event_set(&iev_ui->ev, iev_ui->ibuf.fd, iev_ui->events,
395 bc10f6a5 2021-07-12 op iev_ui->handler, iev_ui);
396 bc10f6a5 2021-07-12 op event_add(&iev_ui->ev, NULL);
398 35e1f40a 2021-03-14 op sandbox_fs_process();
400 35e1f40a 2021-03-14 op event_dispatch();
407 d0fd368a 2021-07-15 op lock_session(void)
409 d0fd368a 2021-07-15 op struct flock lock;
412 d0fd368a 2021-07-15 op if ((fd = open(lockfile_path, O_WRONLY|O_CREAT, 0600)) == -1)
415 d0fd368a 2021-07-15 op lock.l_start = 0;
416 d0fd368a 2021-07-15 op lock.l_len = 0;
417 d0fd368a 2021-07-15 op lock.l_type = F_WRLCK;
418 d0fd368a 2021-07-15 op lock.l_whence = SEEK_SET;
420 d0fd368a 2021-07-15 op if (fcntl(fd, F_SETLK, &lock) == -1) {
429 c6d03cf5 2021-04-25 op parse_khost_line(char *line, char *tmp[3])
433 c6d03cf5 2021-04-25 op for (ap = tmp; ap < &tmp[3] &&
434 c6d03cf5 2021-04-25 op (*ap = strsep(&line, " \t\n")) != NULL;) {
435 c6d03cf5 2021-04-25 op if (**ap != '\0')
439 c6d03cf5 2021-04-25 op return ap == &tmp[3] && *line == '\0';
443 3a227e9a 2021-03-18 op load_certs(struct ohash *h)
445 c6d03cf5 2021-04-25 op char *tmp[3], *line = NULL;
446 6cd6a9e1 2021-03-20 op const char *errstr;
447 ec1fa0b0 2021-04-25 op size_t lineno = 0, linesize = 0;
448 3a227e9a 2021-03-18 op ssize_t linelen;
450 3a227e9a 2021-03-18 op struct tofu_entry *e;
452 3a227e9a 2021-03-18 op if ((f = fopen(known_hosts_file, "r")) == NULL)
455 3a227e9a 2021-03-18 op while ((linelen = getline(&line, &linesize, f)) != -1) {
456 3a227e9a 2021-03-18 op if ((e = calloc(1, sizeof(*e))) == NULL)
461 c6d03cf5 2021-04-25 op if (parse_khost_line(line, tmp)) {
462 c6d03cf5 2021-04-25 op strlcpy(e->domain, tmp[0], sizeof(e->domain));
463 c6d03cf5 2021-04-25 op strlcpy(e->hash, tmp[1], sizeof(e->hash));
465 c6d03cf5 2021-04-25 op e->verified = strtonum(tmp[2], 0, 1, &errstr);
466 c6d03cf5 2021-04-25 op if (errstr != NULL)
467 c6d03cf5 2021-04-25 op errx(1, "%s:%zu verification for %s is %s: %s",
468 c6d03cf5 2021-04-25 op known_hosts_file, lineno,
469 c6d03cf5 2021-04-25 op e->domain, errstr, tmp[2]);
470 c6d03cf5 2021-04-25 op tofu_add(h, e);
472 ec1fa0b0 2021-04-25 op warnx("%s:%zu invalid entry",
473 ec1fa0b0 2021-04-25 op known_hosts_file, lineno);
479 3a227e9a 2021-03-18 op return ferror(f);