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 "telescope.h"
25 #include <sys/stat.h>
27 #include <errno.h>
28 #include <limits.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
34 static void die(void) __attribute__((__noreturn__));
35 static void serve_bookmarks(uint32_t);
36 static void handle_get(struct imsg*, size_t);
37 static void handle_quit(struct imsg*, size_t);
38 static void handle_bookmark_page(struct imsg*, size_t);
39 static void handle_save_cert(struct imsg*, size_t);
40 static void handle_update_cert(struct imsg*, size_t);
41 static void handle_session_start(struct imsg*, size_t);
42 static void handle_session_tab(struct imsg*, size_t);
43 static void handle_session_end(struct imsg*, size_t);
44 static void handle_dispatch_imsg(int, short, void*);
46 static struct event imsgev;
47 static struct imsgbuf *ibuf;
49 static FILE *session;
51 static char bookmark_file[PATH_MAX];
52 static char known_hosts_file[PATH_MAX], known_hosts_tmp[PATH_MAX];
53 static char session_file[PATH_MAX];
55 static imsg_handlerfn *handlers[] = {
56 [IMSG_GET] = handle_get,
57 [IMSG_QUIT] = handle_quit,
58 [IMSG_BOOKMARK_PAGE] = handle_bookmark_page,
59 [IMSG_SAVE_CERT] = handle_save_cert,
60 [IMSG_UPDATE_CERT] = handle_update_cert,
61 [IMSG_SESSION_START] = handle_session_start,
62 [IMSG_SESSION_TAB] = handle_session_tab,
63 [IMSG_SESSION_END] = handle_session_end,
64 };
66 static void __attribute__((__noreturn__))
67 die(void)
68 {
69 abort(); /* TODO */
70 }
72 static void
73 serve_bookmarks(uint32_t peerid)
74 {
75 const char *t;
76 char buf[BUFSIZ];
77 size_t r;
78 FILE *f;
80 if ((f = fopen(bookmark_file, "r")) == NULL) {
81 t = "# Bookmarks\n\n"
82 "No bookmarks yet!\n"
83 "Create ~/.telescope/bookmarks.gmi or use `bookmark-page'.\n";
84 imsg_compose(ibuf, IMSG_BUF, peerid, 0, -1, t, strlen(t));
85 imsg_compose(ibuf, IMSG_EOF, peerid, 0, -1, NULL, 0);
86 imsg_flush(ibuf);
87 return;
88 }
90 for (;;) {
91 r = fread(buf, 1, sizeof(buf), f);
92 imsg_compose(ibuf, IMSG_BUF, peerid, 0, -1, buf, r);
93 imsg_flush(ibuf);
94 if (r != sizeof(buf))
95 break;
96 }
98 imsg_compose(ibuf, IMSG_EOF, peerid, 0, -1, NULL, 0);
99 imsg_flush(ibuf);
101 fclose(f);
104 static void
105 handle_get(struct imsg *imsg, size_t datalen)
107 char *data;
108 const char *p;
110 data = imsg->data;
112 if (data[datalen-1] != '\0')
113 die();
115 if (!strcmp(data, "about:new")) {
116 imsg_compose(ibuf, IMSG_BUF, imsg->hdr.peerid, 0, -1,
117 about_new, strlen(about_new));
118 imsg_compose(ibuf, IMSG_EOF, imsg->hdr.peerid, 0, -1, NULL, 0);
119 imsg_flush(ibuf);
120 } else if (!strcmp(data, "about:bookmarks")) {
121 serve_bookmarks(imsg->hdr.peerid);
122 } else {
123 p = "# not found!\n";
124 imsg_compose(ibuf, IMSG_BUF, imsg->hdr.peerid, 0, -1, p, strlen(p));
125 imsg_compose(ibuf, IMSG_EOF, imsg->hdr.peerid, 0, -1, NULL, 0);
126 imsg_flush(ibuf);
130 static void
131 handle_quit(struct imsg *imsg, size_t datalen)
133 event_loopbreak();
136 static void
137 handle_bookmark_page(struct imsg *imsg, size_t datalen)
139 char *data;
140 int res;
141 FILE *f;
143 data = imsg->data;
144 if (data[datalen-1] != '\0')
145 die();
147 if ((f = fopen(bookmark_file, "a")) == NULL) {
148 res = errno;
149 goto end;
151 fprintf(f, "=> %s\n", data);
152 fclose(f);
154 res = 0;
155 end:
156 imsg_compose(ibuf, IMSG_BOOKMARK_OK, 0, 0, -1, &res, sizeof(res));
157 imsg_flush(ibuf);
160 static void
161 handle_save_cert(struct imsg *imsg, size_t datalen)
163 struct tofu_entry e;
164 FILE *f;
165 int res;
167 /* TODO: traverse the file to avoid duplications? */
169 if (datalen != sizeof(e))
170 die();
171 memcpy(&e, imsg->data, datalen);
173 if ((f = fopen(known_hosts_file, "a")) == NULL) {
174 res = errno;
175 goto end;
177 fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
178 fclose(f);
180 res = 0;
181 end:
182 imsg_compose(ibuf, IMSG_SAVE_CERT_OK, imsg->hdr.peerid, 0, -1,
183 &res, sizeof(res));
184 imsg_flush(ibuf);
187 static void
188 handle_update_cert(struct imsg *imsg, size_t datalen)
190 FILE *tmp, *f;
191 struct tofu_entry entry;
192 char sfn[PATH_MAX], *line = NULL, *t;
193 size_t l, linesize = 0;
194 ssize_t linelen;
195 int fd, e, res = 0;
197 if (datalen != sizeof(entry))
198 die();
199 memcpy(&entry, imsg->data, datalen);
201 strlcpy(sfn, known_hosts_tmp, sizeof(sfn));
202 if ((fd = mkstemp(sfn)) == -1 ||
203 (tmp = fdopen(fd, "w")) == NULL) {
204 if (fd != -1) {
205 unlink(sfn);
206 close(fd);
208 res = 0;
209 goto end;
212 if ((f = fopen(known_hosts_file, "r")) == NULL) {
213 unlink(sfn);
214 fclose(tmp);
215 res = 0;
216 goto end;
219 l = strlen(entry.domain);
220 while ((linelen = getline(&line, &linesize, f)) != -1) {
221 if ((t = strstr(line, entry.domain)) != NULL &&
222 (line[l] == ' ' || line[l] == '\t'))
223 continue;
224 /* line has a trailing \n */
225 fprintf(tmp, "%s", line);
227 fprintf(tmp, "%s %s %d\n", entry.domain, entry.hash, entry.verified);
229 free(line);
230 e = ferror(tmp);
232 fclose(tmp);
233 fclose(f);
235 if (e) {
236 unlink(sfn);
237 res = 0;
238 goto end;
241 res = rename(sfn, known_hosts_file) != -1;
243 end:
244 imsg_compose(ibuf, IMSG_UPDATE_CERT_OK, imsg->hdr.peerid, 0, -1,
245 &res, sizeof(res));
246 imsg_flush(ibuf);
249 static void
250 handle_session_start(struct imsg *imsg, size_t datalen)
252 if (datalen != 0)
253 die();
255 if ((session = fopen(session_file, "w")) == NULL)
256 die();
259 static void
260 handle_session_tab(struct imsg *imsg, size_t datalen)
262 if (session == NULL)
263 die();
265 if (datalen == 0)
266 die();
268 /* skip the NUL-terminator */
269 fwrite(imsg->data, 1, datalen-1, session);
271 fprintf(session, "\n");
274 static void
275 handle_session_end(struct imsg *imsg, size_t datalen)
277 if (session == NULL)
278 die();
279 fclose(session);
280 session = NULL;
283 static void
284 handle_dispatch_imsg(int fd, short ev, void *d)
286 struct imsgbuf *ibuf = d;
287 dispatch_imsg(ibuf, handlers, sizeof(handlers));
290 int
291 fs_init(void)
293 char dir[PATH_MAX];
295 strlcpy(dir, getenv("HOME"), sizeof(dir));
296 strlcat(dir, "/.telescope", sizeof(dir));
297 mkdir(dir, 0700);
299 strlcpy(bookmark_file, getenv("HOME"), sizeof(bookmark_file));
300 strlcat(bookmark_file, "/.telescope/bookmarks.gmi", sizeof(bookmark_file));
302 strlcpy(known_hosts_file, getenv("HOME"), sizeof(known_hosts_file));
303 strlcat(known_hosts_file, "/.telescope/known_hosts", sizeof(known_hosts_file));
305 strlcpy(known_hosts_tmp, getenv("HOME"), sizeof(known_hosts_tmp));
306 strlcat(known_hosts_tmp, "/.telescope/known_hosts.tmp.XXXXXXXXXX",
307 sizeof(known_hosts_file));
309 strlcpy(session_file, getenv("HOME"), sizeof(session_file));
310 strlcat(session_file, "/.telescope/session", sizeof(session_file));
312 return 1;
315 int
316 fs_main(struct imsgbuf *b)
318 ibuf = b;
320 event_init();
322 event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, ibuf);
323 event_add(&imsgev, NULL);
325 sandbox_fs_process();
327 event_dispatch();
328 return 0;
333 static int
334 parse_khost_line(char *line, char *tmp[3])
336 char **ap;
338 for (ap = tmp; ap < &tmp[3] &&
339 (*ap = strsep(&line, " \t\n")) != NULL;) {
340 if (**ap != '\0')
341 ap++;
344 return ap == &tmp[3] && *line == '\0';
347 int
348 load_certs(struct ohash *h)
350 char *tmp[3], *line = NULL;
351 const char *errstr;
352 size_t lineno = 0, linesize = 0;
353 ssize_t linelen;
354 FILE *f;
355 struct tofu_entry *e;
357 if ((f = fopen(known_hosts_file, "r")) == NULL)
358 return 0;
360 while ((linelen = getline(&line, &linesize, f)) != -1) {
361 if ((e = calloc(1, sizeof(*e))) == NULL)
362 abort();
364 lineno++;
366 if (parse_khost_line(line, tmp)) {
367 strlcpy(e->domain, tmp[0], sizeof(e->domain));
368 strlcpy(e->hash, tmp[1], sizeof(e->hash));
370 e->verified = strtonum(tmp[2], 0, 1, &errstr);
371 if (errstr != NULL)
372 errx(1, "%s:%zu verification for %s is %s: %s",
373 known_hosts_file, lineno,
374 e->domain, errstr, tmp[2]);
375 tofu_add(h, e);
376 } else {
377 warnx("%s:%zu invalid entry",
378 known_hosts_file, lineno);
379 free(e);
383 free(line);
384 return ferror(f);
387 int
388 load_last_session(void (*cb)(const char*))
390 char *nl, *line = NULL;
391 int e;
392 size_t linesize = 0;
393 ssize_t linelen;
394 FILE *session;
396 if ((session = fopen(session_file, "r")) == NULL)
397 return 0;
399 while ((linelen = getline(&line, &linesize, session)) != -1) {
400 if ((nl = strchr(line, '\n')) != NULL)
401 *nl = '\0';
402 cb(line);
405 free(line);
406 e = ferror(session);
407 fclose(session);
409 return !e;