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 <errno.h>
26 #include <limits.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
32 static void die(void) __attribute__((__noreturn__));
33 static void serve_bookmarks(uint32_t);
34 static void handle_get(struct imsg*, size_t);
35 static void handle_quit(struct imsg*, size_t);
36 static void handle_bookmark_page(struct imsg*, size_t);
37 static void handle_save_cert(struct imsg*, size_t);
38 static void handle_dispatch_imsg(int, short, void*);
40 static struct event imsgev;
41 static struct imsgbuf *ibuf;
43 static char bookmark_file[PATH_MAX];
44 static char known_hosts_file[PATH_MAX];
46 static imsg_handlerfn *handlers[] = {
47 [IMSG_GET] = handle_get,
48 [IMSG_QUIT] = handle_quit,
49 [IMSG_BOOKMARK_PAGE] = handle_bookmark_page,
50 [IMSG_SAVE_CERT] = handle_save_cert,
51 };
53 static void __attribute__((__noreturn__))
54 die(void)
55 {
56 abort(); /* TODO */
57 }
59 static void
60 serve_bookmarks(uint32_t peerid)
61 {
62 const char *t;
63 char buf[BUFSIZ];
64 size_t r;
65 FILE *f;
67 if ((f = fopen(bookmark_file, "r")) == NULL) {
68 t = "# error\n\nCan't open bookmarks\n";
69 imsg_compose(ibuf, IMSG_BUF, peerid, 0, -1, t, strlen(t));
70 imsg_compose(ibuf, IMSG_EOF, peerid, 0, -1, NULL, 0);
71 imsg_flush(ibuf);
72 return;
73 }
75 for (;;) {
76 r = fread(buf, 1, sizeof(buf), f);
77 imsg_compose(ibuf, IMSG_BUF, peerid, 0, -1, buf, r);
78 imsg_flush(ibuf);
79 if (r != sizeof(buf))
80 break;
81 }
83 imsg_compose(ibuf, IMSG_EOF, peerid, 0, -1, NULL, 0);
84 imsg_flush(ibuf);
86 fclose(f);
87 }
89 static void
90 handle_get(struct imsg *imsg, size_t datalen)
91 {
92 char *data;
93 const char *p;
95 data = imsg->data;
97 if (data[datalen-1] != '\0')
98 die();
100 if (!strcmp(data, "about:new")) {
101 imsg_compose(ibuf, IMSG_BUF, imsg->hdr.peerid, 0, -1,
102 about_new, strlen(about_new));
103 imsg_compose(ibuf, IMSG_EOF, imsg->hdr.peerid, 0, -1, NULL, 0);
104 imsg_flush(ibuf);
105 } else if (!strcmp(data, "about:bookmarks")) {
106 serve_bookmarks(imsg->hdr.peerid);
107 } else {
108 p = "# not found!\n";
109 imsg_compose(ibuf, IMSG_BUF, imsg->hdr.peerid, 0, -1, p, strlen(p));
110 imsg_compose(ibuf, IMSG_EOF, imsg->hdr.peerid, 0, -1, NULL, 0);
111 imsg_flush(ibuf);
115 static void
116 handle_quit(struct imsg *imsg, size_t datalen)
118 event_loopbreak();
121 static void
122 handle_bookmark_page(struct imsg *imsg, size_t datalen)
124 char *data;
125 int res;
126 FILE *f;
128 data = imsg->data;
129 if (data[datalen-1] != '\0')
130 die();
132 if ((f = fopen(bookmark_file, "a")) == NULL) {
133 res = errno;
134 goto end;
136 fprintf(f, "=> %s\n", data);
137 fclose(f);
139 res = 0;
140 end:
141 imsg_compose(ibuf, IMSG_BOOKMARK_OK, 0, 0, -1, &res, sizeof(res));
142 imsg_flush(ibuf);
145 static void
146 handle_save_cert(struct imsg *imsg, size_t datalen)
148 struct tofu_entry e;
149 FILE *f;
150 int res;
152 /* TODO: traverse the file to avoid duplications? */
154 if (datalen != sizeof(e))
155 die();
156 memcpy(&e, imsg->data, datalen);
158 if ((f = fopen(known_hosts_file, "a")) == NULL) {
159 res = errno;
160 goto end;
162 fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
163 fclose(f);
165 res = 0;
166 end:
167 imsg_compose(ibuf, IMSG_SAVE_CERT_OK, imsg->hdr.peerid, 0, -1,
168 &res, sizeof(res));
169 imsg_flush(ibuf);
172 static void
173 handle_dispatch_imsg(int fd, short ev, void *d)
175 struct imsgbuf *ibuf = d;
176 dispatch_imsg(ibuf, handlers, sizeof(handlers));
179 int
180 fs_init(void)
182 char dir[PATH_MAX];
184 strlcpy(dir, getenv("HOME"), sizeof(dir));
185 strlcat(dir, "/.telescope", sizeof(dir));
186 mkdir(dir, 0700);
188 strlcpy(bookmark_file, getenv("HOME"), sizeof(bookmark_file));
189 strlcat(bookmark_file, "/.telescope/bookmarks.gmi", sizeof(bookmark_file));
191 strlcpy(known_hosts_file, getenv("HOME"), sizeof(known_hosts_file));
192 strlcat(known_hosts_file, "/.telescope/known_hosts", sizeof(known_hosts_file));
194 return 1;
197 int
198 fs_main(struct imsgbuf *b)
200 ibuf = b;
202 event_init();
204 event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, ibuf);
205 event_add(&imsgev, NULL);
207 sandbox_fs_process();
209 event_dispatch();
210 return 0;
215 int
216 load_certs(struct ohash *h)
218 char *p, *last, *errstr, *el, *line = NULL;
219 int i;
220 size_t linesize = 0;
221 ssize_t linelen;
222 FILE *f;
223 struct tofu_entry *e;
225 if ((f = fopen(known_hosts_file, "r")) == NULL)
226 return 0;
228 while ((linelen = getline(&line, &linesize, f)) != -1) {
229 if ((e = calloc(1, sizeof(*e))) == NULL)
230 abort();
232 i = 0;
233 for ((p = strtok_r(line, " ", &last)); p;
234 (p = strtok_r(NULL, " ", &last))) {
235 if (*p == '\n') {
236 free(e);
237 break;
240 switch (i) {
241 case 0:
242 strlcpy(e->domain, p, sizeof(e->domain));
243 break;
244 case 1:
245 strlcpy(e->hash, p, sizeof(e->hash));
246 break;
247 case 2:
248 if ((el = strchr(p, '\n')) == NULL)
249 abort();
250 *el = '\0';
252 /* 0 <= verified <= 1 */
253 e->verified = strtonum(p, -1, 2, &errstr);
254 if (errstr != NULL)
255 errx(1, "verification for %s is %s: %s",
256 e->domain, errstr, p);
257 break;
258 default:
259 abort();
261 i++;
264 if (i != 0 && i != 3)
265 abort();
267 if (i != 0)
268 telescope_ohash_insert(h, e);
271 free(line);
272 return ferror(f);