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_end(struct imsg*, size_t);
48 static void handle_dispatch_imsg(int, short, void*);
49 static int fs_send_ui(int, uint32_t, int, const void *, uint16_t);
51 static struct imsgev *iev_ui;
52 static FILE *session;
54 static char lockfile_path[PATH_MAX];
55 static char bookmark_file[PATH_MAX];
56 static char known_hosts_file[PATH_MAX], known_hosts_tmp[PATH_MAX];
57 static char session_file[PATH_MAX];
59 static imsg_handlerfn *handlers[] = {
60 [IMSG_GET] = handle_get,
61 [IMSG_QUIT] = handle_quit,
62 [IMSG_BOOKMARK_PAGE] = handle_bookmark_page,
63 [IMSG_SAVE_CERT] = handle_save_cert,
64 [IMSG_UPDATE_CERT] = handle_update_cert,
65 [IMSG_FILE_OPEN] = handle_file_open,
66 [IMSG_SESSION_START] = handle_session_start,
67 [IMSG_SESSION_TAB] = handle_session_tab,
68 [IMSG_SESSION_END] = handle_session_end,
69 };
71 static void __attribute__((__noreturn__))
72 die(void)
73 {
74 abort(); /* TODO */
75 }
77 static void
78 serve_bookmarks(uint32_t peerid)
79 {
80 const char *t;
81 char buf[BUFSIZ];
82 size_t r;
83 FILE *f;
85 if ((f = fopen(bookmark_file, "r")) == NULL) {
86 t = "# Bookmarks\n\n"
87 "No bookmarks yet!\n"
88 "Create ~/.telescope/bookmarks.gmi or use `bookmark-page'.\n";
89 fs_send_ui(IMSG_BUF, peerid, -1, t, strlen(t));
90 fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
91 return;
92 }
94 for (;;) {
95 r = fread(buf, 1, sizeof(buf), f);
96 fs_send_ui(IMSG_BUF, peerid, -1, buf, r);
97 if (r != sizeof(buf))
98 break;
99 }
101 fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
103 fclose(f);
106 static void
107 send_page(struct imsg *imsg, const char *page)
109 fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, page, strlen(page));
110 fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
113 static void
114 handle_get(struct imsg *imsg, size_t datalen)
116 char *data;
117 const char *p;
119 data = imsg->data;
121 if (data[datalen-1] != '\0')
122 die();
124 if (!strcmp(data, "about:about")) {
125 send_page(imsg, about_about);
126 } else if (!strcmp(data, "about:blank")) {
127 send_page(imsg, about_blank);
128 } else if (!strcmp(data, "about:bookmarks")) {
129 serve_bookmarks(imsg->hdr.peerid);
130 } else if (!strcmp(data, "about:help")) {
131 send_page(imsg, about_help);
132 } else if (!strcmp(data, "about:new")) {
133 send_page(imsg, about_new);
134 } else {
135 p = "# not found!\n";
136 fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, p, strlen(p));
137 fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
141 static void
142 handle_quit(struct imsg *imsg, size_t datalen)
144 event_loopbreak();
147 static void
148 handle_bookmark_page(struct imsg *imsg, size_t datalen)
150 char *data;
151 int res;
152 FILE *f;
154 data = imsg->data;
155 if (data[datalen-1] != '\0')
156 die();
158 if ((f = fopen(bookmark_file, "a")) == NULL) {
159 res = errno;
160 goto end;
162 fprintf(f, "=> %s\n", data);
163 fclose(f);
165 res = 0;
166 end:
167 fs_send_ui(IMSG_BOOKMARK_OK, 0, -1, &res, sizeof(res));
170 static void
171 handle_save_cert(struct imsg *imsg, size_t datalen)
173 struct tofu_entry e;
174 FILE *f;
175 int res;
177 /* TODO: traverse the file to avoid duplications? */
179 if (datalen != sizeof(e))
180 die();
181 memcpy(&e, imsg->data, datalen);
183 if ((f = fopen(known_hosts_file, "a")) == NULL) {
184 res = errno;
185 goto end;
187 fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
188 fclose(f);
190 res = 0;
191 end:
192 fs_send_ui(IMSG_SAVE_CERT_OK, imsg->hdr.peerid, -1,
193 &res, sizeof(res));
196 static void
197 handle_update_cert(struct imsg *imsg, size_t datalen)
199 FILE *tmp, *f;
200 struct tofu_entry entry;
201 char sfn[PATH_MAX], *line = NULL, *t;
202 size_t l, linesize = 0;
203 ssize_t linelen;
204 int fd, e, res = 0;
206 if (datalen != sizeof(entry))
207 die();
208 memcpy(&entry, imsg->data, datalen);
210 strlcpy(sfn, known_hosts_tmp, sizeof(sfn));
211 if ((fd = mkstemp(sfn)) == -1 ||
212 (tmp = fdopen(fd, "w")) == NULL) {
213 if (fd != -1) {
214 unlink(sfn);
215 close(fd);
217 res = 0;
218 goto end;
221 if ((f = fopen(known_hosts_file, "r")) == NULL) {
222 unlink(sfn);
223 fclose(tmp);
224 res = 0;
225 goto end;
228 l = strlen(entry.domain);
229 while ((linelen = getline(&line, &linesize, f)) != -1) {
230 if ((t = strstr(line, entry.domain)) != NULL &&
231 (line[l] == ' ' || line[l] == '\t'))
232 continue;
233 /* line has a trailing \n */
234 fprintf(tmp, "%s", line);
236 fprintf(tmp, "%s %s %d\n", entry.domain, entry.hash, entry.verified);
238 free(line);
239 e = ferror(tmp);
241 fclose(tmp);
242 fclose(f);
244 if (e) {
245 unlink(sfn);
246 res = 0;
247 goto end;
250 res = rename(sfn, known_hosts_file) != -1;
252 end:
253 fs_send_ui(IMSG_UPDATE_CERT_OK, imsg->hdr.peerid, -1,
254 &res, sizeof(res));
257 static void
258 handle_file_open(struct imsg *imsg, size_t datalen)
260 char *path, *e;
261 int fd;
263 path = imsg->data;
264 if (path[datalen-1] != '\0')
265 die();
267 if ((fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644)) == -1) {
268 e = strerror(errno);
269 fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, -1,
270 e, strlen(e)+1);
271 } else
272 fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, fd,
273 NULL, 0);
276 static void
277 handle_session_start(struct imsg *imsg, size_t datalen)
279 if (datalen != 0)
280 die();
282 if ((session = fopen(session_file, "w")) == NULL)
283 die();
286 static void
287 handle_session_tab(struct imsg *imsg, size_t datalen)
289 if (session == NULL)
290 die();
292 if (datalen == 0)
293 die();
295 /* skip the NUL-terminator */
296 fwrite(imsg->data, 1, datalen-1, session);
298 fprintf(session, "\n");
301 static void
302 handle_session_end(struct imsg *imsg, size_t datalen)
304 if (session == NULL)
305 die();
306 fclose(session);
307 session = NULL;
310 static void
311 handle_dispatch_imsg(int fd, short ev, void *d)
313 struct imsgev *iev = d;
314 dispatch_imsg(iev, ev, handlers, sizeof(handlers));
317 static int
318 fs_send_ui(int type, uint32_t peerid, int fd, const void *data,
319 uint16_t datalen)
321 return imsg_compose_event(iev_ui, type, peerid, 0, fd,
322 data, datalen);
325 int
326 fs_init(void)
328 char dir[PATH_MAX];
330 strlcpy(dir, getenv("HOME"), sizeof(dir));
331 strlcat(dir, "/.telescope", sizeof(dir));
332 mkdir(dir, 0700);
334 strlcpy(lockfile_path, getenv("HOME"), sizeof(lockfile_path));
335 strlcat(lockfile_path, "/.telescope/lock", sizeof(lockfile_path));
337 strlcpy(bookmark_file, getenv("HOME"), sizeof(bookmark_file));
338 strlcat(bookmark_file, "/.telescope/bookmarks.gmi", sizeof(bookmark_file));
340 strlcpy(known_hosts_file, getenv("HOME"), sizeof(known_hosts_file));
341 strlcat(known_hosts_file, "/.telescope/known_hosts", sizeof(known_hosts_file));
343 strlcpy(known_hosts_tmp, getenv("HOME"), sizeof(known_hosts_tmp));
344 strlcat(known_hosts_tmp, "/.telescope/known_hosts.tmp.XXXXXXXXXX",
345 sizeof(known_hosts_file));
347 strlcpy(session_file, getenv("HOME"), sizeof(session_file));
348 strlcat(session_file, "/.telescope/session", sizeof(session_file));
350 return 1;
353 int
354 fs_main(void)
356 setproctitle("fs");
358 fs_init();
360 event_init();
362 /* Setup pipe and event handler to the main process */
363 if ((iev_ui = malloc(sizeof(*iev_ui))) == NULL)
364 die();
365 imsg_init(&iev_ui->ibuf, 3);
366 iev_ui->handler = handle_dispatch_imsg;
367 iev_ui->events = EV_READ;
368 event_set(&iev_ui->ev, iev_ui->ibuf.fd, iev_ui->events,
369 iev_ui->handler, iev_ui);
370 event_add(&iev_ui->ev, NULL);
372 sandbox_fs_process();
374 event_dispatch();
375 return 0;
380 int
381 lock_session(void)
383 struct flock lock;
384 int fd;
386 if ((fd = open(lockfile_path, O_WRONLY|O_CREAT, 0600)) == -1)
387 return -1;
389 lock.l_start = 0;
390 lock.l_len = 0;
391 lock.l_type = F_WRLCK;
392 lock.l_whence = SEEK_SET;
394 if (fcntl(fd, F_SETLK, &lock) == -1) {
395 close(fd);
396 return -1;
399 return fd;
402 static int
403 parse_khost_line(char *line, char *tmp[3])
405 char **ap;
407 for (ap = tmp; ap < &tmp[3] &&
408 (*ap = strsep(&line, " \t\n")) != NULL;) {
409 if (**ap != '\0')
410 ap++;
413 return ap == &tmp[3] && *line == '\0';
416 int
417 load_certs(struct ohash *h)
419 char *tmp[3], *line = NULL;
420 const char *errstr;
421 size_t lineno = 0, linesize = 0;
422 ssize_t linelen;
423 FILE *f;
424 struct tofu_entry *e;
426 if ((f = fopen(known_hosts_file, "r")) == NULL)
427 return 0;
429 while ((linelen = getline(&line, &linesize, f)) != -1) {
430 if ((e = calloc(1, sizeof(*e))) == NULL)
431 abort();
433 lineno++;
435 if (parse_khost_line(line, tmp)) {
436 strlcpy(e->domain, tmp[0], sizeof(e->domain));
437 strlcpy(e->hash, tmp[1], sizeof(e->hash));
439 e->verified = strtonum(tmp[2], 0, 1, &errstr);
440 if (errstr != NULL)
441 errx(1, "%s:%zu verification for %s is %s: %s",
442 known_hosts_file, lineno,
443 e->domain, errstr, tmp[2]);
444 tofu_add(h, e);
445 } else {
446 warnx("%s:%zu invalid entry",
447 known_hosts_file, lineno);
448 free(e);
452 free(line);
453 return ferror(f);
456 int
457 load_last_session(void (*cb)(const char*))
459 char *nl, *line = NULL;
460 int e;
461 size_t linesize = 0;
462 ssize_t linelen;
463 FILE *session;
465 if ((session = fopen(session_file, "r")) == NULL) {
466 /* first time? */
467 cb("about:help");
468 return 0;
471 while ((linelen = getline(&line, &linesize, session)) != -1) {
472 if ((nl = strchr(line, '\n')) != NULL)
473 *nl = '\0';
474 cb(line);
477 free(line);
478 e = ferror(session);
479 fclose(session);
481 return !e;