Blame


1 35e1f40a 2021-03-14 op /*
2 35e1f40a 2021-03-14 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 35e1f40a 2021-03-14 op *
4 35e1f40a 2021-03-14 op * Permission to use, copy, modify, and distribute this software for any
5 35e1f40a 2021-03-14 op * purpose with or without fee is hereby granted, provided that the above
6 35e1f40a 2021-03-14 op * copyright notice and this permission notice appear in all copies.
7 35e1f40a 2021-03-14 op *
8 35e1f40a 2021-03-14 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 35e1f40a 2021-03-14 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 35e1f40a 2021-03-14 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 35e1f40a 2021-03-14 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 35e1f40a 2021-03-14 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 35e1f40a 2021-03-14 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 35e1f40a 2021-03-14 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 35e1f40a 2021-03-14 op */
16 35e1f40a 2021-03-14 op
17 740f578b 2021-03-15 op /*
18 740f578b 2021-03-15 op * Handles the data in ~/.telescope
19 3a227e9a 2021-03-18 op *
20 3a227e9a 2021-03-18 op * TODO: add some form of locking on the files
21 740f578b 2021-03-15 op */
22 740f578b 2021-03-15 op
23 35e1f40a 2021-03-14 op #include "telescope.h"
24 35e1f40a 2021-03-14 op
25 6cd6a9e1 2021-03-20 op #include <sys/stat.h>
26 6cd6a9e1 2021-03-20 op
27 35e1f40a 2021-03-14 op #include <errno.h>
28 35e1f40a 2021-03-14 op #include <limits.h>
29 35e1f40a 2021-03-14 op #include <stdio.h>
30 35e1f40a 2021-03-14 op #include <stdlib.h>
31 35e1f40a 2021-03-14 op #include <string.h>
32 35e1f40a 2021-03-14 op #include <unistd.h>
33 35e1f40a 2021-03-14 op
34 35e1f40a 2021-03-14 op static void die(void) __attribute__((__noreturn__));
35 35e1f40a 2021-03-14 op static void serve_bookmarks(uint32_t);
36 35e1f40a 2021-03-14 op static void handle_get(struct imsg*, size_t);
37 35e1f40a 2021-03-14 op static void handle_quit(struct imsg*, size_t);
38 740f578b 2021-03-15 op static void handle_bookmark_page(struct imsg*, size_t);
39 3a227e9a 2021-03-18 op static void handle_save_cert(struct imsg*, size_t);
40 1304bbdd 2021-03-15 op static void handle_dispatch_imsg(int, short, void*);
41 35e1f40a 2021-03-14 op
42 35e1f40a 2021-03-14 op static struct event imsgev;
43 35e1f40a 2021-03-14 op static struct imsgbuf *ibuf;
44 35e1f40a 2021-03-14 op
45 740f578b 2021-03-15 op static char bookmark_file[PATH_MAX];
46 3a227e9a 2021-03-18 op static char known_hosts_file[PATH_MAX];
47 740f578b 2021-03-15 op
48 35e1f40a 2021-03-14 op static imsg_handlerfn *handlers[] = {
49 35e1f40a 2021-03-14 op [IMSG_GET] = handle_get,
50 35e1f40a 2021-03-14 op [IMSG_QUIT] = handle_quit,
51 740f578b 2021-03-15 op [IMSG_BOOKMARK_PAGE] = handle_bookmark_page,
52 3a227e9a 2021-03-18 op [IMSG_SAVE_CERT] = handle_save_cert,
53 35e1f40a 2021-03-14 op };
54 35e1f40a 2021-03-14 op
55 35e1f40a 2021-03-14 op static void __attribute__((__noreturn__))
56 35e1f40a 2021-03-14 op die(void)
57 35e1f40a 2021-03-14 op {
58 35e1f40a 2021-03-14 op abort(); /* TODO */
59 35e1f40a 2021-03-14 op }
60 35e1f40a 2021-03-14 op
61 35e1f40a 2021-03-14 op static void
62 35e1f40a 2021-03-14 op serve_bookmarks(uint32_t peerid)
63 35e1f40a 2021-03-14 op {
64 35e1f40a 2021-03-14 op const char *t;
65 740f578b 2021-03-15 op char buf[BUFSIZ];
66 35e1f40a 2021-03-14 op size_t r;
67 35e1f40a 2021-03-14 op FILE *f;
68 35e1f40a 2021-03-14 op
69 740f578b 2021-03-15 op if ((f = fopen(bookmark_file, "r")) == NULL) {
70 740f578b 2021-03-15 op t = "# error\n\nCan't open bookmarks\n";
71 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_BUF, peerid, 0, -1, t, strlen(t));
72 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_EOF, peerid, 0, -1, NULL, 0);
73 35e1f40a 2021-03-14 op imsg_flush(ibuf);
74 35e1f40a 2021-03-14 op return;
75 35e1f40a 2021-03-14 op }
76 35e1f40a 2021-03-14 op
77 35e1f40a 2021-03-14 op for (;;) {
78 35e1f40a 2021-03-14 op r = fread(buf, 1, sizeof(buf), f);
79 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_BUF, peerid, 0, -1, buf, r);
80 35e1f40a 2021-03-14 op imsg_flush(ibuf);
81 35e1f40a 2021-03-14 op if (r != sizeof(buf))
82 35e1f40a 2021-03-14 op break;
83 35e1f40a 2021-03-14 op }
84 35e1f40a 2021-03-14 op
85 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_EOF, peerid, 0, -1, NULL, 0);
86 35e1f40a 2021-03-14 op imsg_flush(ibuf);
87 35e1f40a 2021-03-14 op
88 35e1f40a 2021-03-14 op fclose(f);
89 35e1f40a 2021-03-14 op }
90 35e1f40a 2021-03-14 op
91 35e1f40a 2021-03-14 op static void
92 35e1f40a 2021-03-14 op handle_get(struct imsg *imsg, size_t datalen)
93 35e1f40a 2021-03-14 op {
94 35e1f40a 2021-03-14 op char *data;
95 35e1f40a 2021-03-14 op const char *p;
96 35e1f40a 2021-03-14 op
97 35e1f40a 2021-03-14 op data = imsg->data;
98 35e1f40a 2021-03-14 op
99 35e1f40a 2021-03-14 op if (data[datalen-1] != '\0')
100 35e1f40a 2021-03-14 op die();
101 35e1f40a 2021-03-14 op
102 35e1f40a 2021-03-14 op if (!strcmp(data, "about:new")) {
103 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_BUF, imsg->hdr.peerid, 0, -1,
104 35e1f40a 2021-03-14 op about_new, strlen(about_new));
105 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_EOF, imsg->hdr.peerid, 0, -1, NULL, 0);
106 35e1f40a 2021-03-14 op imsg_flush(ibuf);
107 35e1f40a 2021-03-14 op } else if (!strcmp(data, "about:bookmarks")) {
108 35e1f40a 2021-03-14 op serve_bookmarks(imsg->hdr.peerid);
109 35e1f40a 2021-03-14 op } else {
110 35e1f40a 2021-03-14 op p = "# not found!\n";
111 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_BUF, imsg->hdr.peerid, 0, -1, p, strlen(p));
112 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_EOF, imsg->hdr.peerid, 0, -1, NULL, 0);
113 35e1f40a 2021-03-14 op imsg_flush(ibuf);
114 35e1f40a 2021-03-14 op }
115 35e1f40a 2021-03-14 op }
116 35e1f40a 2021-03-14 op
117 35e1f40a 2021-03-14 op static void
118 35e1f40a 2021-03-14 op handle_quit(struct imsg *imsg, size_t datalen)
119 35e1f40a 2021-03-14 op {
120 35e1f40a 2021-03-14 op event_loopbreak();
121 35e1f40a 2021-03-14 op }
122 35e1f40a 2021-03-14 op
123 35e1f40a 2021-03-14 op static void
124 740f578b 2021-03-15 op handle_bookmark_page(struct imsg *imsg, size_t datalen)
125 740f578b 2021-03-15 op {
126 740f578b 2021-03-15 op char *data;
127 740f578b 2021-03-15 op int res;
128 740f578b 2021-03-15 op FILE *f;
129 740f578b 2021-03-15 op
130 740f578b 2021-03-15 op data = imsg->data;
131 740f578b 2021-03-15 op if (data[datalen-1] != '\0')
132 740f578b 2021-03-15 op die();
133 740f578b 2021-03-15 op
134 740f578b 2021-03-15 op if ((f = fopen(bookmark_file, "a")) == NULL) {
135 740f578b 2021-03-15 op res = errno;
136 740f578b 2021-03-15 op goto end;
137 740f578b 2021-03-15 op }
138 740f578b 2021-03-15 op fprintf(f, "=> %s\n", data);
139 740f578b 2021-03-15 op fclose(f);
140 740f578b 2021-03-15 op
141 740f578b 2021-03-15 op res = 0;
142 740f578b 2021-03-15 op end:
143 740f578b 2021-03-15 op imsg_compose(ibuf, IMSG_BOOKMARK_OK, 0, 0, -1, &res, sizeof(res));
144 740f578b 2021-03-15 op imsg_flush(ibuf);
145 740f578b 2021-03-15 op }
146 740f578b 2021-03-15 op
147 740f578b 2021-03-15 op static void
148 3a227e9a 2021-03-18 op handle_save_cert(struct imsg *imsg, size_t datalen)
149 3a227e9a 2021-03-18 op {
150 3a227e9a 2021-03-18 op struct tofu_entry e;
151 3a227e9a 2021-03-18 op FILE *f;
152 3a227e9a 2021-03-18 op int res;
153 3a227e9a 2021-03-18 op
154 3a227e9a 2021-03-18 op /* TODO: traverse the file to avoid duplications? */
155 3a227e9a 2021-03-18 op
156 3a227e9a 2021-03-18 op if (datalen != sizeof(e))
157 3a227e9a 2021-03-18 op die();
158 3a227e9a 2021-03-18 op memcpy(&e, imsg->data, datalen);
159 3a227e9a 2021-03-18 op
160 3a227e9a 2021-03-18 op if ((f = fopen(known_hosts_file, "a")) == NULL) {
161 3a227e9a 2021-03-18 op res = errno;
162 3a227e9a 2021-03-18 op goto end;
163 3a227e9a 2021-03-18 op }
164 3a227e9a 2021-03-18 op fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
165 3a227e9a 2021-03-18 op fclose(f);
166 3a227e9a 2021-03-18 op
167 3a227e9a 2021-03-18 op res = 0;
168 3a227e9a 2021-03-18 op end:
169 3a227e9a 2021-03-18 op imsg_compose(ibuf, IMSG_SAVE_CERT_OK, imsg->hdr.peerid, 0, -1,
170 3a227e9a 2021-03-18 op &res, sizeof(res));
171 3a227e9a 2021-03-18 op imsg_flush(ibuf);
172 3a227e9a 2021-03-18 op }
173 3a227e9a 2021-03-18 op
174 3a227e9a 2021-03-18 op static void
175 1304bbdd 2021-03-15 op handle_dispatch_imsg(int fd, short ev, void *d)
176 35e1f40a 2021-03-14 op {
177 35e1f40a 2021-03-14 op struct imsgbuf *ibuf = d;
178 1304bbdd 2021-03-15 op dispatch_imsg(ibuf, handlers, sizeof(handlers));
179 35e1f40a 2021-03-14 op }
180 35e1f40a 2021-03-14 op
181 35e1f40a 2021-03-14 op int
182 3a227e9a 2021-03-18 op fs_init(void)
183 35e1f40a 2021-03-14 op {
184 63a715ea 2021-03-18 op char dir[PATH_MAX];
185 63a715ea 2021-03-18 op
186 63a715ea 2021-03-18 op strlcpy(dir, getenv("HOME"), sizeof(dir));
187 63a715ea 2021-03-18 op strlcat(dir, "/.telescope", sizeof(dir));
188 63a715ea 2021-03-18 op mkdir(dir, 0700);
189 35e1f40a 2021-03-14 op
190 740f578b 2021-03-15 op strlcpy(bookmark_file, getenv("HOME"), sizeof(bookmark_file));
191 740f578b 2021-03-15 op strlcat(bookmark_file, "/.telescope/bookmarks.gmi", sizeof(bookmark_file));
192 740f578b 2021-03-15 op
193 3a227e9a 2021-03-18 op strlcpy(known_hosts_file, getenv("HOME"), sizeof(known_hosts_file));
194 3a227e9a 2021-03-18 op strlcat(known_hosts_file, "/.telescope/known_hosts", sizeof(known_hosts_file));
195 3a227e9a 2021-03-18 op
196 3a227e9a 2021-03-18 op return 1;
197 3a227e9a 2021-03-18 op }
198 3a227e9a 2021-03-18 op
199 3a227e9a 2021-03-18 op int
200 3a227e9a 2021-03-18 op fs_main(struct imsgbuf *b)
201 3a227e9a 2021-03-18 op {
202 3a227e9a 2021-03-18 op ibuf = b;
203 3a227e9a 2021-03-18 op
204 35e1f40a 2021-03-14 op event_init();
205 35e1f40a 2021-03-14 op
206 1304bbdd 2021-03-15 op event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, ibuf);
207 35e1f40a 2021-03-14 op event_add(&imsgev, NULL);
208 35e1f40a 2021-03-14 op
209 35e1f40a 2021-03-14 op sandbox_fs_process();
210 35e1f40a 2021-03-14 op
211 35e1f40a 2021-03-14 op event_dispatch();
212 35e1f40a 2021-03-14 op return 0;
213 35e1f40a 2021-03-14 op }
214 3a227e9a 2021-03-18 op
215 3a227e9a 2021-03-18 op
216 3a227e9a 2021-03-18 op
217 3a227e9a 2021-03-18 op int
218 3a227e9a 2021-03-18 op load_certs(struct ohash *h)
219 3a227e9a 2021-03-18 op {
220 6cd6a9e1 2021-03-20 op char *p, *last, *el, *line = NULL;
221 6cd6a9e1 2021-03-20 op const char *errstr;
222 3a227e9a 2021-03-18 op int i;
223 3a227e9a 2021-03-18 op size_t linesize = 0;
224 3a227e9a 2021-03-18 op ssize_t linelen;
225 3a227e9a 2021-03-18 op FILE *f;
226 3a227e9a 2021-03-18 op struct tofu_entry *e;
227 3a227e9a 2021-03-18 op
228 3a227e9a 2021-03-18 op if ((f = fopen(known_hosts_file, "r")) == NULL)
229 3a227e9a 2021-03-18 op return 0;
230 3a227e9a 2021-03-18 op
231 3a227e9a 2021-03-18 op while ((linelen = getline(&line, &linesize, f)) != -1) {
232 3a227e9a 2021-03-18 op if ((e = calloc(1, sizeof(*e))) == NULL)
233 3a227e9a 2021-03-18 op abort();
234 3a227e9a 2021-03-18 op
235 3a227e9a 2021-03-18 op i = 0;
236 3a227e9a 2021-03-18 op for ((p = strtok_r(line, " ", &last)); p;
237 3a227e9a 2021-03-18 op (p = strtok_r(NULL, " ", &last))) {
238 3a227e9a 2021-03-18 op if (*p == '\n') {
239 3a227e9a 2021-03-18 op free(e);
240 3a227e9a 2021-03-18 op break;
241 3a227e9a 2021-03-18 op }
242 3a227e9a 2021-03-18 op
243 3a227e9a 2021-03-18 op switch (i) {
244 3a227e9a 2021-03-18 op case 0:
245 3a227e9a 2021-03-18 op strlcpy(e->domain, p, sizeof(e->domain));
246 3a227e9a 2021-03-18 op break;
247 3a227e9a 2021-03-18 op case 1:
248 3a227e9a 2021-03-18 op strlcpy(e->hash, p, sizeof(e->hash));
249 3a227e9a 2021-03-18 op break;
250 3a227e9a 2021-03-18 op case 2:
251 3a227e9a 2021-03-18 op if ((el = strchr(p, '\n')) == NULL)
252 3a227e9a 2021-03-18 op abort();
253 3a227e9a 2021-03-18 op *el = '\0';
254 3a227e9a 2021-03-18 op
255 3a227e9a 2021-03-18 op /* 0 <= verified <= 1 */
256 3a227e9a 2021-03-18 op e->verified = strtonum(p, -1, 2, &errstr);
257 3a227e9a 2021-03-18 op if (errstr != NULL)
258 3a227e9a 2021-03-18 op errx(1, "verification for %s is %s: %s",
259 3a227e9a 2021-03-18 op e->domain, errstr, p);
260 3a227e9a 2021-03-18 op break;
261 3a227e9a 2021-03-18 op default:
262 3a227e9a 2021-03-18 op abort();
263 3a227e9a 2021-03-18 op }
264 3a227e9a 2021-03-18 op i++;
265 3a227e9a 2021-03-18 op }
266 3a227e9a 2021-03-18 op
267 3a227e9a 2021-03-18 op if (i != 0 && i != 3)
268 3a227e9a 2021-03-18 op abort();
269 3a227e9a 2021-03-18 op
270 3a227e9a 2021-03-18 op if (i != 0)
271 3a227e9a 2021-03-18 op telescope_ohash_insert(h, e);
272 3a227e9a 2021-03-18 op }
273 3a227e9a 2021-03-18 op
274 3a227e9a 2021-03-18 op free(line);
275 3a227e9a 2021-03-18 op return ferror(f);
276 3a227e9a 2021-03-18 op }