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];
58 char session_file[PATH_MAX];
60 static imsg_handlerfn *handlers[] = {
61 [IMSG_GET] = handle_get,
62 [IMSG_QUIT] = handle_quit,
63 [IMSG_BOOKMARK_PAGE] = handle_bookmark_page,
64 [IMSG_SAVE_CERT] = handle_save_cert,
65 [IMSG_UPDATE_CERT] = handle_update_cert,
66 [IMSG_FILE_OPEN] = handle_file_open,
67 [IMSG_SESSION_START] = handle_session_start,
68 [IMSG_SESSION_TAB] = handle_session_tab,
69 [IMSG_SESSION_END] = handle_session_end,
70 };
72 static void __attribute__((__noreturn__))
73 die(void)
74 {
75 abort(); /* TODO */
76 }
78 static void
79 serve_bookmarks(uint32_t peerid)
80 {
81 const char *t;
82 char buf[BUFSIZ];
83 size_t r;
84 FILE *f;
86 if ((f = fopen(bookmark_file, "r")) == NULL) {
87 t = "# Bookmarks\n\n"
88 "No bookmarks yet!\n"
89 "Create ~/.telescope/bookmarks.gmi or use `bookmark-page'.\n";
90 fs_send_ui(IMSG_BUF, peerid, -1, t, strlen(t));
91 fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
92 return;
93 }
95 for (;;) {
96 r = fread(buf, 1, sizeof(buf), f);
97 fs_send_ui(IMSG_BUF, peerid, -1, buf, r);
98 if (r != sizeof(buf))
99 break;
102 fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
104 fclose(f);
107 static void
108 send_page(struct imsg *imsg, const char *page)
110 fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, page, strlen(page));
111 fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
114 static void
115 handle_get(struct imsg *imsg, size_t datalen)
117 char *data;
118 const char *p;
120 data = imsg->data;
122 if (data[datalen-1] != '\0')
123 die();
125 if (!strcmp(data, "about:about")) {
126 send_page(imsg, about_about);
127 } else if (!strcmp(data, "about:blank")) {
128 send_page(imsg, about_blank);
129 } else if (!strcmp(data, "about:bookmarks")) {
130 serve_bookmarks(imsg->hdr.peerid);
131 } else if (!strcmp(data, "about:help")) {
132 send_page(imsg, about_help);
133 } else if (!strcmp(data, "about:new")) {
134 send_page(imsg, about_new);
135 } else {
136 p = "# not found!\n";
137 fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, p, strlen(p));
138 fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
142 static void
143 handle_quit(struct imsg *imsg, size_t datalen)
145 event_loopbreak();
148 static void
149 handle_bookmark_page(struct imsg *imsg, size_t datalen)
151 char *data;
152 int res;
153 FILE *f;
155 data = imsg->data;
156 if (data[datalen-1] != '\0')
157 die();
159 if ((f = fopen(bookmark_file, "a")) == NULL) {
160 res = errno;
161 goto end;
163 fprintf(f, "=> %s\n", data);
164 fclose(f);
166 res = 0;
167 end:
168 fs_send_ui(IMSG_BOOKMARK_OK, 0, -1, &res, sizeof(res));
171 static void
172 handle_save_cert(struct imsg *imsg, size_t datalen)
174 struct tofu_entry e;
175 FILE *f;
176 int res;
178 /* TODO: traverse the file to avoid duplications? */
180 if (datalen != sizeof(e))
181 die();
182 memcpy(&e, imsg->data, datalen);
184 if ((f = fopen(known_hosts_file, "a")) == NULL) {
185 res = errno;
186 goto end;
188 fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
189 fclose(f);
191 res = 0;
192 end:
193 fs_send_ui(IMSG_SAVE_CERT_OK, imsg->hdr.peerid, -1,
194 &res, sizeof(res));
197 static void
198 handle_update_cert(struct imsg *imsg, size_t datalen)
200 FILE *tmp, *f;
201 struct tofu_entry entry;
202 char sfn[PATH_MAX], *line = NULL, *t;
203 size_t l, linesize = 0;
204 ssize_t linelen;
205 int fd, e, res = 0;
207 if (datalen != sizeof(entry))
208 die();
209 memcpy(&entry, imsg->data, datalen);
211 strlcpy(sfn, known_hosts_tmp, sizeof(sfn));
212 if ((fd = mkstemp(sfn)) == -1 ||
213 (tmp = fdopen(fd, "w")) == NULL) {
214 if (fd != -1) {
215 unlink(sfn);
216 close(fd);
218 res = 0;
219 goto end;
222 if ((f = fopen(known_hosts_file, "r")) == NULL) {
223 unlink(sfn);
224 fclose(tmp);
225 res = 0;
226 goto end;
229 l = strlen(entry.domain);
230 while ((linelen = getline(&line, &linesize, f)) != -1) {
231 if ((t = strstr(line, entry.domain)) != NULL &&
232 (line[l] == ' ' || line[l] == '\t'))
233 continue;
234 /* line has a trailing \n */
235 fprintf(tmp, "%s", line);
237 fprintf(tmp, "%s %s %d\n", entry.domain, entry.hash, entry.verified);
239 free(line);
240 e = ferror(tmp);
242 fclose(tmp);
243 fclose(f);
245 if (e) {
246 unlink(sfn);
247 res = 0;
248 goto end;
251 res = rename(sfn, known_hosts_file) != -1;
253 end:
254 fs_send_ui(IMSG_UPDATE_CERT_OK, imsg->hdr.peerid, -1,
255 &res, sizeof(res));
258 static void
259 handle_file_open(struct imsg *imsg, size_t datalen)
261 char *path, *e;
262 int fd;
264 path = imsg->data;
265 if (path[datalen-1] != '\0')
266 die();
268 if ((fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644)) == -1) {
269 e = strerror(errno);
270 fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, -1,
271 e, strlen(e)+1);
272 } else
273 fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, fd,
274 NULL, 0);
277 static void
278 handle_session_start(struct imsg *imsg, size_t datalen)
280 if (datalen != 0)
281 die();
283 if ((session = fopen(session_file, "w")) == NULL)
284 die();
287 static void
288 handle_session_tab(struct imsg *imsg, size_t datalen)
290 char *url;
291 uint32_t flags;
293 if (session == NULL)
294 die();
296 flags = imsg->hdr.peerid;
297 url = imsg->data;
298 if (datalen == 0 || url[datalen-1] != '\0')
299 die();
300 fprintf(session, "%s", url);
302 if (flags & TAB_CURRENT)
303 fprintf(session, " current");
305 fprintf(session, "\n");
308 static void
309 handle_session_end(struct imsg *imsg, size_t datalen)
311 if (session == NULL)
312 die();
313 fclose(session);
314 session = NULL;
317 static void
318 handle_dispatch_imsg(int fd, short ev, void *d)
320 struct imsgev *iev = d;
321 dispatch_imsg(iev, ev, handlers, sizeof(handlers));
324 static int
325 fs_send_ui(int type, uint32_t peerid, int fd, const void *data,
326 uint16_t datalen)
328 return imsg_compose_event(iev_ui, type, peerid, 0, fd,
329 data, datalen);
332 int
333 fs_init(void)
335 char dir[PATH_MAX];
337 strlcpy(dir, getenv("HOME"), sizeof(dir));
338 strlcat(dir, "/.telescope", sizeof(dir));
339 mkdir(dir, 0700);
341 strlcpy(lockfile_path, getenv("HOME"), sizeof(lockfile_path));
342 strlcat(lockfile_path, "/.telescope/lock", sizeof(lockfile_path));
344 strlcpy(bookmark_file, getenv("HOME"), sizeof(bookmark_file));
345 strlcat(bookmark_file, "/.telescope/bookmarks.gmi", sizeof(bookmark_file));
347 strlcpy(known_hosts_file, getenv("HOME"), sizeof(known_hosts_file));
348 strlcat(known_hosts_file, "/.telescope/known_hosts", sizeof(known_hosts_file));
350 strlcpy(known_hosts_tmp, getenv("HOME"), sizeof(known_hosts_tmp));
351 strlcat(known_hosts_tmp, "/.telescope/known_hosts.tmp.XXXXXXXXXX",
352 sizeof(known_hosts_file));
354 strlcpy(session_file, getenv("HOME"), sizeof(session_file));
355 strlcat(session_file, "/.telescope/session", sizeof(session_file));
357 return 1;
360 int
361 fs_main(void)
363 setproctitle("fs");
365 fs_init();
367 event_init();
369 /* Setup pipe and event handler to the main process */
370 if ((iev_ui = malloc(sizeof(*iev_ui))) == NULL)
371 die();
372 imsg_init(&iev_ui->ibuf, 3);
373 iev_ui->handler = handle_dispatch_imsg;
374 iev_ui->events = EV_READ;
375 event_set(&iev_ui->ev, iev_ui->ibuf.fd, iev_ui->events,
376 iev_ui->handler, iev_ui);
377 event_add(&iev_ui->ev, NULL);
379 sandbox_fs_process();
381 event_dispatch();
382 return 0;
387 int
388 lock_session(void)
390 struct flock lock;
391 int fd;
393 if ((fd = open(lockfile_path, O_WRONLY|O_CREAT, 0600)) == -1)
394 return -1;
396 lock.l_start = 0;
397 lock.l_len = 0;
398 lock.l_type = F_WRLCK;
399 lock.l_whence = SEEK_SET;
401 if (fcntl(fd, F_SETLK, &lock) == -1) {
402 close(fd);
403 return -1;
406 return fd;
409 static int
410 parse_khost_line(char *line, char *tmp[3])
412 char **ap;
414 for (ap = tmp; ap < &tmp[3] &&
415 (*ap = strsep(&line, " \t\n")) != NULL;) {
416 if (**ap != '\0')
417 ap++;
420 return ap == &tmp[3] && *line == '\0';
423 int
424 load_certs(struct ohash *h)
426 char *tmp[3], *line = NULL;
427 const char *errstr;
428 size_t lineno = 0, linesize = 0;
429 ssize_t linelen;
430 FILE *f;
431 struct tofu_entry *e;
433 if ((f = fopen(known_hosts_file, "r")) == NULL)
434 return 0;
436 while ((linelen = getline(&line, &linesize, f)) != -1) {
437 if ((e = calloc(1, sizeof(*e))) == NULL)
438 abort();
440 lineno++;
442 if (parse_khost_line(line, tmp)) {
443 strlcpy(e->domain, tmp[0], sizeof(e->domain));
444 strlcpy(e->hash, tmp[1], sizeof(e->hash));
446 e->verified = strtonum(tmp[2], 0, 1, &errstr);
447 if (errstr != NULL)
448 errx(1, "%s:%zu verification for %s is %s: %s",
449 known_hosts_file, lineno,
450 e->domain, errstr, tmp[2]);
451 tofu_add(h, e);
452 } else {
453 warnx("%s:%zu invalid entry",
454 known_hosts_file, lineno);
455 free(e);
459 free(line);
460 return ferror(f);