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 <fcntl.h>
29 #include <limits.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
35 static void die(void) __attribute__((__noreturn__));
36 static void serve_bookmarks(uint32_t);
37 static void handle_get(struct imsg*, size_t);
38 static void handle_quit(struct imsg*, size_t);
39 static void handle_bookmark_page(struct imsg*, size_t);
40 static void handle_save_cert(struct imsg*, size_t);
41 static void handle_update_cert(struct imsg*, size_t);
42 static void handle_file_open(struct imsg*, size_t);
43 static void handle_session_start(struct imsg*, size_t);
44 static void handle_session_tab(struct imsg*, size_t);
45 static void handle_session_end(struct imsg*, size_t);
46 static void handle_dispatch_imsg(int, short, void*);
48 static struct event imsgev;
49 static struct imsgbuf *ibuf;
51 static FILE *session;
53 static char bookmark_file[PATH_MAX];
54 static char known_hosts_file[PATH_MAX], known_hosts_tmp[PATH_MAX];
55 static char session_file[PATH_MAX];
57 static imsg_handlerfn *handlers[] = {
58 [IMSG_GET] = handle_get,
59 [IMSG_QUIT] = handle_quit,
60 [IMSG_BOOKMARK_PAGE] = handle_bookmark_page,
61 [IMSG_SAVE_CERT] = handle_save_cert,
62 [IMSG_UPDATE_CERT] = handle_update_cert,
63 [IMSG_FILE_OPEN] = handle_file_open,
64 [IMSG_SESSION_START] = handle_session_start,
65 [IMSG_SESSION_TAB] = handle_session_tab,
66 [IMSG_SESSION_END] = handle_session_end,
67 };
69 static void __attribute__((__noreturn__))
70 die(void)
71 {
72 abort(); /* TODO */
73 }
75 static void
76 serve_bookmarks(uint32_t peerid)
77 {
78 const char *t;
79 char buf[BUFSIZ];
80 size_t r;
81 FILE *f;
83 if ((f = fopen(bookmark_file, "r")) == NULL) {
84 t = "# Bookmarks\n\n"
85 "No bookmarks yet!\n"
86 "Create ~/.telescope/bookmarks.gmi or use `bookmark-page'.\n";
87 imsg_compose(ibuf, IMSG_BUF, peerid, 0, -1, t, strlen(t));
88 imsg_compose(ibuf, IMSG_EOF, peerid, 0, -1, NULL, 0);
89 imsg_flush(ibuf);
90 return;
91 }
93 for (;;) {
94 r = fread(buf, 1, sizeof(buf), f);
95 imsg_compose(ibuf, IMSG_BUF, peerid, 0, -1, buf, r);
96 imsg_flush(ibuf);
97 if (r != sizeof(buf))
98 break;
99 }
101 imsg_compose(ibuf, IMSG_EOF, peerid, 0, -1, NULL, 0);
102 imsg_flush(ibuf);
104 fclose(f);
107 static void
108 handle_get(struct imsg *imsg, size_t datalen)
110 char *data;
111 const char *p;
113 data = imsg->data;
115 if (data[datalen-1] != '\0')
116 die();
118 if (!strcmp(data, "about:new")) {
119 imsg_compose(ibuf, IMSG_BUF, imsg->hdr.peerid, 0, -1,
120 about_new, strlen(about_new));
121 imsg_compose(ibuf, IMSG_EOF, imsg->hdr.peerid, 0, -1, NULL, 0);
122 imsg_flush(ibuf);
123 } else if (!strcmp(data, "about:bookmarks")) {
124 serve_bookmarks(imsg->hdr.peerid);
125 } else {
126 p = "# not found!\n";
127 imsg_compose(ibuf, IMSG_BUF, imsg->hdr.peerid, 0, -1, p, strlen(p));
128 imsg_compose(ibuf, IMSG_EOF, imsg->hdr.peerid, 0, -1, NULL, 0);
129 imsg_flush(ibuf);
133 static void
134 handle_quit(struct imsg *imsg, size_t datalen)
136 event_loopbreak();
139 static void
140 handle_bookmark_page(struct imsg *imsg, size_t datalen)
142 char *data;
143 int res;
144 FILE *f;
146 data = imsg->data;
147 if (data[datalen-1] != '\0')
148 die();
150 if ((f = fopen(bookmark_file, "a")) == NULL) {
151 res = errno;
152 goto end;
154 fprintf(f, "=> %s\n", data);
155 fclose(f);
157 res = 0;
158 end:
159 imsg_compose(ibuf, IMSG_BOOKMARK_OK, 0, 0, -1, &res, sizeof(res));
160 imsg_flush(ibuf);
163 static void
164 handle_save_cert(struct imsg *imsg, size_t datalen)
166 struct tofu_entry e;
167 FILE *f;
168 int res;
170 /* TODO: traverse the file to avoid duplications? */
172 if (datalen != sizeof(e))
173 die();
174 memcpy(&e, imsg->data, datalen);
176 if ((f = fopen(known_hosts_file, "a")) == NULL) {
177 res = errno;
178 goto end;
180 fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
181 fclose(f);
183 res = 0;
184 end:
185 imsg_compose(ibuf, IMSG_SAVE_CERT_OK, imsg->hdr.peerid, 0, -1,
186 &res, sizeof(res));
187 imsg_flush(ibuf);
190 static void
191 handle_update_cert(struct imsg *imsg, size_t datalen)
193 FILE *tmp, *f;
194 struct tofu_entry entry;
195 char sfn[PATH_MAX], *line = NULL, *t;
196 size_t l, linesize = 0;
197 ssize_t linelen;
198 int fd, e, res = 0;
200 if (datalen != sizeof(entry))
201 die();
202 memcpy(&entry, imsg->data, datalen);
204 strlcpy(sfn, known_hosts_tmp, sizeof(sfn));
205 if ((fd = mkstemp(sfn)) == -1 ||
206 (tmp = fdopen(fd, "w")) == NULL) {
207 if (fd != -1) {
208 unlink(sfn);
209 close(fd);
211 res = 0;
212 goto end;
215 if ((f = fopen(known_hosts_file, "r")) == NULL) {
216 unlink(sfn);
217 fclose(tmp);
218 res = 0;
219 goto end;
222 l = strlen(entry.domain);
223 while ((linelen = getline(&line, &linesize, f)) != -1) {
224 if ((t = strstr(line, entry.domain)) != NULL &&
225 (line[l] == ' ' || line[l] == '\t'))
226 continue;
227 /* line has a trailing \n */
228 fprintf(tmp, "%s", line);
230 fprintf(tmp, "%s %s %d\n", entry.domain, entry.hash, entry.verified);
232 free(line);
233 e = ferror(tmp);
235 fclose(tmp);
236 fclose(f);
238 if (e) {
239 unlink(sfn);
240 res = 0;
241 goto end;
244 res = rename(sfn, known_hosts_file) != -1;
246 end:
247 imsg_compose(ibuf, IMSG_UPDATE_CERT_OK, imsg->hdr.peerid, 0, -1,
248 &res, sizeof(res));
249 imsg_flush(ibuf);
252 static void
253 handle_file_open(struct imsg *imsg, size_t datalen)
255 char *path, *e;
256 int fd;
258 path = imsg->data;
259 if (path[datalen-1] != '\0')
260 die();
262 if ((fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644)) == -1) {
263 e = strerror(errno);
264 imsg_compose(ibuf, IMSG_FILE_OPENED, imsg->hdr.peerid, 0, -1,
265 e, strlen(e)+1);
266 } else
267 imsg_compose(ibuf, IMSG_FILE_OPENED, imsg->hdr.peerid, 0, fd,
268 NULL, 0);
270 imsg_flush(ibuf);
273 static void
274 handle_session_start(struct imsg *imsg, size_t datalen)
276 if (datalen != 0)
277 die();
279 if ((session = fopen(session_file, "w")) == NULL)
280 die();
283 static void
284 handle_session_tab(struct imsg *imsg, size_t datalen)
286 if (session == NULL)
287 die();
289 if (datalen == 0)
290 die();
292 /* skip the NUL-terminator */
293 fwrite(imsg->data, 1, datalen-1, session);
295 fprintf(session, "\n");
298 static void
299 handle_session_end(struct imsg *imsg, size_t datalen)
301 if (session == NULL)
302 die();
303 fclose(session);
304 session = NULL;
307 static void
308 handle_dispatch_imsg(int fd, short ev, void *d)
310 struct imsgbuf *ibuf = d;
311 dispatch_imsg(ibuf, handlers, sizeof(handlers));
314 int
315 fs_init(void)
317 char dir[PATH_MAX];
319 strlcpy(dir, getenv("HOME"), sizeof(dir));
320 strlcat(dir, "/.telescope", sizeof(dir));
321 mkdir(dir, 0700);
323 strlcpy(bookmark_file, getenv("HOME"), sizeof(bookmark_file));
324 strlcat(bookmark_file, "/.telescope/bookmarks.gmi", sizeof(bookmark_file));
326 strlcpy(known_hosts_file, getenv("HOME"), sizeof(known_hosts_file));
327 strlcat(known_hosts_file, "/.telescope/known_hosts", sizeof(known_hosts_file));
329 strlcpy(known_hosts_tmp, getenv("HOME"), sizeof(known_hosts_tmp));
330 strlcat(known_hosts_tmp, "/.telescope/known_hosts.tmp.XXXXXXXXXX",
331 sizeof(known_hosts_file));
333 strlcpy(session_file, getenv("HOME"), sizeof(session_file));
334 strlcat(session_file, "/.telescope/session", sizeof(session_file));
336 return 1;
339 int
340 fs_main(struct imsgbuf *b)
342 ibuf = b;
344 event_init();
346 event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, ibuf);
347 event_add(&imsgev, NULL);
349 sandbox_fs_process();
351 event_dispatch();
352 return 0;
357 static int
358 parse_khost_line(char *line, char *tmp[3])
360 char **ap;
362 for (ap = tmp; ap < &tmp[3] &&
363 (*ap = strsep(&line, " \t\n")) != NULL;) {
364 if (**ap != '\0')
365 ap++;
368 return ap == &tmp[3] && *line == '\0';
371 int
372 load_certs(struct ohash *h)
374 char *tmp[3], *line = NULL;
375 const char *errstr;
376 size_t lineno = 0, linesize = 0;
377 ssize_t linelen;
378 FILE *f;
379 struct tofu_entry *e;
381 if ((f = fopen(known_hosts_file, "r")) == NULL)
382 return 0;
384 while ((linelen = getline(&line, &linesize, f)) != -1) {
385 if ((e = calloc(1, sizeof(*e))) == NULL)
386 abort();
388 lineno++;
390 if (parse_khost_line(line, tmp)) {
391 strlcpy(e->domain, tmp[0], sizeof(e->domain));
392 strlcpy(e->hash, tmp[1], sizeof(e->hash));
394 e->verified = strtonum(tmp[2], 0, 1, &errstr);
395 if (errstr != NULL)
396 errx(1, "%s:%zu verification for %s is %s: %s",
397 known_hosts_file, lineno,
398 e->domain, errstr, tmp[2]);
399 tofu_add(h, e);
400 } else {
401 warnx("%s:%zu invalid entry",
402 known_hosts_file, lineno);
403 free(e);
407 free(line);
408 return ferror(f);
411 int
412 load_last_session(void (*cb)(const char*))
414 char *nl, *line = NULL;
415 int e;
416 size_t linesize = 0;
417 ssize_t linelen;
418 FILE *session;
420 if ((session = fopen(session_file, "r")) == NULL)
421 return 0;
423 while ((linelen = getline(&line, &linesize, session)) != -1) {
424 if ((nl = strchr(line, '\n')) != NULL)
425 *nl = '\0';
426 cb(line);
429 free(line);
430 e = ferror(session);
431 fclose(session);
433 return !e;