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_dispatch_imsg(int, short, void*);
42 static struct event imsgev;
43 static struct imsgbuf *ibuf;
45 static char bookmark_file[PATH_MAX];
46 static char known_hosts_file[PATH_MAX];
48 static imsg_handlerfn *handlers[] = {
49 [IMSG_GET] = handle_get,
50 [IMSG_QUIT] = handle_quit,
51 [IMSG_BOOKMARK_PAGE] = handle_bookmark_page,
52 [IMSG_SAVE_CERT] = handle_save_cert,
53 };
55 static void __attribute__((__noreturn__))
56 die(void)
57 {
58 abort(); /* TODO */
59 }
61 static void
62 serve_bookmarks(uint32_t peerid)
63 {
64 const char *t;
65 char buf[BUFSIZ];
66 size_t r;
67 FILE *f;
69 if ((f = fopen(bookmark_file, "r")) == NULL) {
70 t = "# error\n\nCan't open bookmarks\n";
71 imsg_compose(ibuf, IMSG_BUF, peerid, 0, -1, t, strlen(t));
72 imsg_compose(ibuf, IMSG_EOF, peerid, 0, -1, NULL, 0);
73 imsg_flush(ibuf);
74 return;
75 }
77 for (;;) {
78 r = fread(buf, 1, sizeof(buf), f);
79 imsg_compose(ibuf, IMSG_BUF, peerid, 0, -1, buf, r);
80 imsg_flush(ibuf);
81 if (r != sizeof(buf))
82 break;
83 }
85 imsg_compose(ibuf, IMSG_EOF, peerid, 0, -1, NULL, 0);
86 imsg_flush(ibuf);
88 fclose(f);
89 }
91 static void
92 handle_get(struct imsg *imsg, size_t datalen)
93 {
94 char *data;
95 const char *p;
97 data = imsg->data;
99 if (data[datalen-1] != '\0')
100 die();
102 if (!strcmp(data, "about:new")) {
103 imsg_compose(ibuf, IMSG_BUF, imsg->hdr.peerid, 0, -1,
104 about_new, strlen(about_new));
105 imsg_compose(ibuf, IMSG_EOF, imsg->hdr.peerid, 0, -1, NULL, 0);
106 imsg_flush(ibuf);
107 } else if (!strcmp(data, "about:bookmarks")) {
108 serve_bookmarks(imsg->hdr.peerid);
109 } else {
110 p = "# not found!\n";
111 imsg_compose(ibuf, IMSG_BUF, imsg->hdr.peerid, 0, -1, p, strlen(p));
112 imsg_compose(ibuf, IMSG_EOF, imsg->hdr.peerid, 0, -1, NULL, 0);
113 imsg_flush(ibuf);
117 static void
118 handle_quit(struct imsg *imsg, size_t datalen)
120 event_loopbreak();
123 static void
124 handle_bookmark_page(struct imsg *imsg, size_t datalen)
126 char *data;
127 int res;
128 FILE *f;
130 data = imsg->data;
131 if (data[datalen-1] != '\0')
132 die();
134 if ((f = fopen(bookmark_file, "a")) == NULL) {
135 res = errno;
136 goto end;
138 fprintf(f, "=> %s\n", data);
139 fclose(f);
141 res = 0;
142 end:
143 imsg_compose(ibuf, IMSG_BOOKMARK_OK, 0, 0, -1, &res, sizeof(res));
144 imsg_flush(ibuf);
147 static void
148 handle_save_cert(struct imsg *imsg, size_t datalen)
150 struct tofu_entry e;
151 FILE *f;
152 int res;
154 /* TODO: traverse the file to avoid duplications? */
156 if (datalen != sizeof(e))
157 die();
158 memcpy(&e, imsg->data, datalen);
160 if ((f = fopen(known_hosts_file, "a")) == NULL) {
161 res = errno;
162 goto end;
164 fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
165 fclose(f);
167 res = 0;
168 end:
169 imsg_compose(ibuf, IMSG_SAVE_CERT_OK, imsg->hdr.peerid, 0, -1,
170 &res, sizeof(res));
171 imsg_flush(ibuf);
174 static void
175 handle_dispatch_imsg(int fd, short ev, void *d)
177 struct imsgbuf *ibuf = d;
178 dispatch_imsg(ibuf, handlers, sizeof(handlers));
181 int
182 fs_init(void)
184 char dir[PATH_MAX];
186 strlcpy(dir, getenv("HOME"), sizeof(dir));
187 strlcat(dir, "/.telescope", sizeof(dir));
188 mkdir(dir, 0700);
190 strlcpy(bookmark_file, getenv("HOME"), sizeof(bookmark_file));
191 strlcat(bookmark_file, "/.telescope/bookmarks.gmi", sizeof(bookmark_file));
193 strlcpy(known_hosts_file, getenv("HOME"), sizeof(known_hosts_file));
194 strlcat(known_hosts_file, "/.telescope/known_hosts", sizeof(known_hosts_file));
196 return 1;
199 int
200 fs_main(struct imsgbuf *b)
202 ibuf = b;
204 event_init();
206 event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, ibuf);
207 event_add(&imsgev, NULL);
209 sandbox_fs_process();
211 event_dispatch();
212 return 0;
217 int
218 load_certs(struct ohash *h)
220 char *p, *last, *el, *line = NULL;
221 const char *errstr;
222 int i;
223 size_t linesize = 0;
224 ssize_t linelen;
225 FILE *f;
226 struct tofu_entry *e;
228 if ((f = fopen(known_hosts_file, "r")) == NULL)
229 return 0;
231 while ((linelen = getline(&line, &linesize, f)) != -1) {
232 if ((e = calloc(1, sizeof(*e))) == NULL)
233 abort();
235 i = 0;
236 for ((p = strtok_r(line, " ", &last)); p;
237 (p = strtok_r(NULL, " ", &last))) {
238 if (*p == '\n') {
239 free(e);
240 break;
243 switch (i) {
244 case 0:
245 strlcpy(e->domain, p, sizeof(e->domain));
246 break;
247 case 1:
248 strlcpy(e->hash, p, sizeof(e->hash));
249 break;
250 case 2:
251 if ((el = strchr(p, '\n')) == NULL)
252 abort();
253 *el = '\0';
255 /* 0 <= verified <= 1 */
256 e->verified = strtonum(p, -1, 2, &errstr);
257 if (errstr != NULL)
258 errx(1, "verification for %s is %s: %s",
259 e->domain, errstr, p);
260 break;
261 default:
262 abort();
264 i++;
267 if (i != 0 && i != 3)
268 abort();
270 if (i != 0)
271 telescope_ohash_insert(h, e);
274 free(line);
275 return ferror(f);