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 c7107cec 2021-04-01 op static void handle_session_start(struct imsg*, size_t);
41 c7107cec 2021-04-01 op static void handle_session_tab(struct imsg*, size_t);
42 c7107cec 2021-04-01 op static void handle_session_end(struct imsg*, size_t);
43 1304bbdd 2021-03-15 op static void handle_dispatch_imsg(int, short, void*);
44 35e1f40a 2021-03-14 op
45 35e1f40a 2021-03-14 op static struct event imsgev;
46 35e1f40a 2021-03-14 op static struct imsgbuf *ibuf;
47 35e1f40a 2021-03-14 op
48 c7107cec 2021-04-01 op static FILE *session;
49 c7107cec 2021-04-01 op
50 740f578b 2021-03-15 op static char bookmark_file[PATH_MAX];
51 3a227e9a 2021-03-18 op static char known_hosts_file[PATH_MAX];
52 c7107cec 2021-04-01 op static char session_file[PATH_MAX];
53 740f578b 2021-03-15 op
54 35e1f40a 2021-03-14 op static imsg_handlerfn *handlers[] = {
55 35e1f40a 2021-03-14 op [IMSG_GET] = handle_get,
56 35e1f40a 2021-03-14 op [IMSG_QUIT] = handle_quit,
57 740f578b 2021-03-15 op [IMSG_BOOKMARK_PAGE] = handle_bookmark_page,
58 3a227e9a 2021-03-18 op [IMSG_SAVE_CERT] = handle_save_cert,
59 c7107cec 2021-04-01 op [IMSG_SESSION_START] = handle_session_start,
60 c7107cec 2021-04-01 op [IMSG_SESSION_TAB] = handle_session_tab,
61 c7107cec 2021-04-01 op [IMSG_SESSION_END] = handle_session_end,
62 35e1f40a 2021-03-14 op };
63 35e1f40a 2021-03-14 op
64 35e1f40a 2021-03-14 op static void __attribute__((__noreturn__))
65 35e1f40a 2021-03-14 op die(void)
66 35e1f40a 2021-03-14 op {
67 35e1f40a 2021-03-14 op abort(); /* TODO */
68 35e1f40a 2021-03-14 op }
69 35e1f40a 2021-03-14 op
70 35e1f40a 2021-03-14 op static void
71 35e1f40a 2021-03-14 op serve_bookmarks(uint32_t peerid)
72 35e1f40a 2021-03-14 op {
73 35e1f40a 2021-03-14 op const char *t;
74 740f578b 2021-03-15 op char buf[BUFSIZ];
75 35e1f40a 2021-03-14 op size_t r;
76 35e1f40a 2021-03-14 op FILE *f;
77 35e1f40a 2021-03-14 op
78 740f578b 2021-03-15 op if ((f = fopen(bookmark_file, "r")) == NULL) {
79 503425db 2021-04-01 op t = "# Bookmarks\n\n"
80 503425db 2021-04-01 op "No bookmarks yet!\n"
81 503425db 2021-04-01 op "Create ~/.telescope/bookmarks.gmi or use `bookmark-page'.\n";
82 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_BUF, peerid, 0, -1, t, strlen(t));
83 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_EOF, peerid, 0, -1, NULL, 0);
84 35e1f40a 2021-03-14 op imsg_flush(ibuf);
85 35e1f40a 2021-03-14 op return;
86 35e1f40a 2021-03-14 op }
87 35e1f40a 2021-03-14 op
88 35e1f40a 2021-03-14 op for (;;) {
89 35e1f40a 2021-03-14 op r = fread(buf, 1, sizeof(buf), f);
90 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_BUF, peerid, 0, -1, buf, r);
91 35e1f40a 2021-03-14 op imsg_flush(ibuf);
92 35e1f40a 2021-03-14 op if (r != sizeof(buf))
93 35e1f40a 2021-03-14 op break;
94 35e1f40a 2021-03-14 op }
95 35e1f40a 2021-03-14 op
96 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_EOF, peerid, 0, -1, NULL, 0);
97 35e1f40a 2021-03-14 op imsg_flush(ibuf);
98 35e1f40a 2021-03-14 op
99 35e1f40a 2021-03-14 op fclose(f);
100 35e1f40a 2021-03-14 op }
101 35e1f40a 2021-03-14 op
102 35e1f40a 2021-03-14 op static void
103 35e1f40a 2021-03-14 op handle_get(struct imsg *imsg, size_t datalen)
104 35e1f40a 2021-03-14 op {
105 35e1f40a 2021-03-14 op char *data;
106 35e1f40a 2021-03-14 op const char *p;
107 35e1f40a 2021-03-14 op
108 35e1f40a 2021-03-14 op data = imsg->data;
109 35e1f40a 2021-03-14 op
110 35e1f40a 2021-03-14 op if (data[datalen-1] != '\0')
111 35e1f40a 2021-03-14 op die();
112 35e1f40a 2021-03-14 op
113 35e1f40a 2021-03-14 op if (!strcmp(data, "about:new")) {
114 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_BUF, imsg->hdr.peerid, 0, -1,
115 35e1f40a 2021-03-14 op about_new, strlen(about_new));
116 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_EOF, imsg->hdr.peerid, 0, -1, NULL, 0);
117 35e1f40a 2021-03-14 op imsg_flush(ibuf);
118 35e1f40a 2021-03-14 op } else if (!strcmp(data, "about:bookmarks")) {
119 35e1f40a 2021-03-14 op serve_bookmarks(imsg->hdr.peerid);
120 35e1f40a 2021-03-14 op } else {
121 35e1f40a 2021-03-14 op p = "# not found!\n";
122 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_BUF, imsg->hdr.peerid, 0, -1, p, strlen(p));
123 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_EOF, imsg->hdr.peerid, 0, -1, NULL, 0);
124 35e1f40a 2021-03-14 op imsg_flush(ibuf);
125 35e1f40a 2021-03-14 op }
126 35e1f40a 2021-03-14 op }
127 35e1f40a 2021-03-14 op
128 35e1f40a 2021-03-14 op static void
129 35e1f40a 2021-03-14 op handle_quit(struct imsg *imsg, size_t datalen)
130 35e1f40a 2021-03-14 op {
131 35e1f40a 2021-03-14 op event_loopbreak();
132 35e1f40a 2021-03-14 op }
133 35e1f40a 2021-03-14 op
134 35e1f40a 2021-03-14 op static void
135 740f578b 2021-03-15 op handle_bookmark_page(struct imsg *imsg, size_t datalen)
136 740f578b 2021-03-15 op {
137 740f578b 2021-03-15 op char *data;
138 740f578b 2021-03-15 op int res;
139 740f578b 2021-03-15 op FILE *f;
140 740f578b 2021-03-15 op
141 740f578b 2021-03-15 op data = imsg->data;
142 740f578b 2021-03-15 op if (data[datalen-1] != '\0')
143 740f578b 2021-03-15 op die();
144 740f578b 2021-03-15 op
145 740f578b 2021-03-15 op if ((f = fopen(bookmark_file, "a")) == NULL) {
146 740f578b 2021-03-15 op res = errno;
147 740f578b 2021-03-15 op goto end;
148 740f578b 2021-03-15 op }
149 740f578b 2021-03-15 op fprintf(f, "=> %s\n", data);
150 740f578b 2021-03-15 op fclose(f);
151 740f578b 2021-03-15 op
152 740f578b 2021-03-15 op res = 0;
153 740f578b 2021-03-15 op end:
154 740f578b 2021-03-15 op imsg_compose(ibuf, IMSG_BOOKMARK_OK, 0, 0, -1, &res, sizeof(res));
155 740f578b 2021-03-15 op imsg_flush(ibuf);
156 740f578b 2021-03-15 op }
157 740f578b 2021-03-15 op
158 740f578b 2021-03-15 op static void
159 3a227e9a 2021-03-18 op handle_save_cert(struct imsg *imsg, size_t datalen)
160 3a227e9a 2021-03-18 op {
161 3a227e9a 2021-03-18 op struct tofu_entry e;
162 3a227e9a 2021-03-18 op FILE *f;
163 3a227e9a 2021-03-18 op int res;
164 3a227e9a 2021-03-18 op
165 3a227e9a 2021-03-18 op /* TODO: traverse the file to avoid duplications? */
166 3a227e9a 2021-03-18 op
167 3a227e9a 2021-03-18 op if (datalen != sizeof(e))
168 3a227e9a 2021-03-18 op die();
169 3a227e9a 2021-03-18 op memcpy(&e, imsg->data, datalen);
170 3a227e9a 2021-03-18 op
171 3a227e9a 2021-03-18 op if ((f = fopen(known_hosts_file, "a")) == NULL) {
172 3a227e9a 2021-03-18 op res = errno;
173 3a227e9a 2021-03-18 op goto end;
174 3a227e9a 2021-03-18 op }
175 3a227e9a 2021-03-18 op fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
176 3a227e9a 2021-03-18 op fclose(f);
177 3a227e9a 2021-03-18 op
178 3a227e9a 2021-03-18 op res = 0;
179 3a227e9a 2021-03-18 op end:
180 3a227e9a 2021-03-18 op imsg_compose(ibuf, IMSG_SAVE_CERT_OK, imsg->hdr.peerid, 0, -1,
181 3a227e9a 2021-03-18 op &res, sizeof(res));
182 3a227e9a 2021-03-18 op imsg_flush(ibuf);
183 3a227e9a 2021-03-18 op }
184 3a227e9a 2021-03-18 op
185 3a227e9a 2021-03-18 op static void
186 c7107cec 2021-04-01 op handle_session_start(struct imsg *imsg, size_t datalen)
187 c7107cec 2021-04-01 op {
188 c7107cec 2021-04-01 op if (datalen != 0)
189 c7107cec 2021-04-01 op die();
190 c7107cec 2021-04-01 op
191 c7107cec 2021-04-01 op if ((session = fopen(session_file, "w")) == NULL)
192 c7107cec 2021-04-01 op die();
193 c7107cec 2021-04-01 op }
194 c7107cec 2021-04-01 op
195 c7107cec 2021-04-01 op static void
196 c7107cec 2021-04-01 op handle_session_tab(struct imsg *imsg, size_t datalen)
197 c7107cec 2021-04-01 op {
198 c7107cec 2021-04-01 op if (session == NULL)
199 c7107cec 2021-04-01 op die();
200 c7107cec 2021-04-01 op
201 c7107cec 2021-04-01 op if (datalen == 0)
202 c7107cec 2021-04-01 op die();
203 c7107cec 2021-04-01 op
204 c7107cec 2021-04-01 op /* skip the NUL-terminator */
205 c7107cec 2021-04-01 op fwrite(imsg->data, 1, datalen-1, session);
206 c7107cec 2021-04-01 op
207 c7107cec 2021-04-01 op fprintf(session, "\n");
208 c7107cec 2021-04-01 op }
209 c7107cec 2021-04-01 op
210 c7107cec 2021-04-01 op static void
211 c7107cec 2021-04-01 op handle_session_end(struct imsg *imsg, size_t datalen)
212 c7107cec 2021-04-01 op {
213 c7107cec 2021-04-01 op if (session == NULL)
214 c7107cec 2021-04-01 op die();
215 c7107cec 2021-04-01 op fclose(session);
216 c7107cec 2021-04-01 op session = NULL;
217 c7107cec 2021-04-01 op }
218 c7107cec 2021-04-01 op
219 c7107cec 2021-04-01 op static void
220 1304bbdd 2021-03-15 op handle_dispatch_imsg(int fd, short ev, void *d)
221 35e1f40a 2021-03-14 op {
222 35e1f40a 2021-03-14 op struct imsgbuf *ibuf = d;
223 1304bbdd 2021-03-15 op dispatch_imsg(ibuf, handlers, sizeof(handlers));
224 35e1f40a 2021-03-14 op }
225 35e1f40a 2021-03-14 op
226 35e1f40a 2021-03-14 op int
227 3a227e9a 2021-03-18 op fs_init(void)
228 35e1f40a 2021-03-14 op {
229 63a715ea 2021-03-18 op char dir[PATH_MAX];
230 63a715ea 2021-03-18 op
231 63a715ea 2021-03-18 op strlcpy(dir, getenv("HOME"), sizeof(dir));
232 63a715ea 2021-03-18 op strlcat(dir, "/.telescope", sizeof(dir));
233 63a715ea 2021-03-18 op mkdir(dir, 0700);
234 35e1f40a 2021-03-14 op
235 740f578b 2021-03-15 op strlcpy(bookmark_file, getenv("HOME"), sizeof(bookmark_file));
236 740f578b 2021-03-15 op strlcat(bookmark_file, "/.telescope/bookmarks.gmi", sizeof(bookmark_file));
237 740f578b 2021-03-15 op
238 3a227e9a 2021-03-18 op strlcpy(known_hosts_file, getenv("HOME"), sizeof(known_hosts_file));
239 3a227e9a 2021-03-18 op strlcat(known_hosts_file, "/.telescope/known_hosts", sizeof(known_hosts_file));
240 3a227e9a 2021-03-18 op
241 c7107cec 2021-04-01 op strlcpy(session_file, getenv("HOME"), sizeof(session_file));
242 c7107cec 2021-04-01 op strlcat(session_file, "/.telescope/session", sizeof(session_file));
243 c7107cec 2021-04-01 op
244 3a227e9a 2021-03-18 op return 1;
245 3a227e9a 2021-03-18 op }
246 3a227e9a 2021-03-18 op
247 3a227e9a 2021-03-18 op int
248 3a227e9a 2021-03-18 op fs_main(struct imsgbuf *b)
249 3a227e9a 2021-03-18 op {
250 3a227e9a 2021-03-18 op ibuf = b;
251 3a227e9a 2021-03-18 op
252 35e1f40a 2021-03-14 op event_init();
253 35e1f40a 2021-03-14 op
254 1304bbdd 2021-03-15 op event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, ibuf);
255 35e1f40a 2021-03-14 op event_add(&imsgev, NULL);
256 35e1f40a 2021-03-14 op
257 35e1f40a 2021-03-14 op sandbox_fs_process();
258 35e1f40a 2021-03-14 op
259 35e1f40a 2021-03-14 op event_dispatch();
260 35e1f40a 2021-03-14 op return 0;
261 35e1f40a 2021-03-14 op }
262 3a227e9a 2021-03-18 op
263 3a227e9a 2021-03-18 op
264 3a227e9a 2021-03-18 op
265 3a227e9a 2021-03-18 op int
266 3a227e9a 2021-03-18 op load_certs(struct ohash *h)
267 3a227e9a 2021-03-18 op {
268 6cd6a9e1 2021-03-20 op char *p, *last, *el, *line = NULL;
269 6cd6a9e1 2021-03-20 op const char *errstr;
270 3a227e9a 2021-03-18 op int i;
271 3a227e9a 2021-03-18 op size_t linesize = 0;
272 3a227e9a 2021-03-18 op ssize_t linelen;
273 3a227e9a 2021-03-18 op FILE *f;
274 3a227e9a 2021-03-18 op struct tofu_entry *e;
275 3a227e9a 2021-03-18 op
276 3a227e9a 2021-03-18 op if ((f = fopen(known_hosts_file, "r")) == NULL)
277 3a227e9a 2021-03-18 op return 0;
278 3a227e9a 2021-03-18 op
279 3a227e9a 2021-03-18 op while ((linelen = getline(&line, &linesize, f)) != -1) {
280 3a227e9a 2021-03-18 op if ((e = calloc(1, sizeof(*e))) == NULL)
281 3a227e9a 2021-03-18 op abort();
282 3a227e9a 2021-03-18 op
283 3a227e9a 2021-03-18 op i = 0;
284 3a227e9a 2021-03-18 op for ((p = strtok_r(line, " ", &last)); p;
285 3a227e9a 2021-03-18 op (p = strtok_r(NULL, " ", &last))) {
286 3a227e9a 2021-03-18 op if (*p == '\n') {
287 3a227e9a 2021-03-18 op free(e);
288 3a227e9a 2021-03-18 op break;
289 3a227e9a 2021-03-18 op }
290 3a227e9a 2021-03-18 op
291 3a227e9a 2021-03-18 op switch (i) {
292 3a227e9a 2021-03-18 op case 0:
293 3a227e9a 2021-03-18 op strlcpy(e->domain, p, sizeof(e->domain));
294 3a227e9a 2021-03-18 op break;
295 3a227e9a 2021-03-18 op case 1:
296 3a227e9a 2021-03-18 op strlcpy(e->hash, p, sizeof(e->hash));
297 3a227e9a 2021-03-18 op break;
298 3a227e9a 2021-03-18 op case 2:
299 3a227e9a 2021-03-18 op if ((el = strchr(p, '\n')) == NULL)
300 3a227e9a 2021-03-18 op abort();
301 3a227e9a 2021-03-18 op *el = '\0';
302 3a227e9a 2021-03-18 op
303 3a227e9a 2021-03-18 op /* 0 <= verified <= 1 */
304 3a227e9a 2021-03-18 op e->verified = strtonum(p, -1, 2, &errstr);
305 3a227e9a 2021-03-18 op if (errstr != NULL)
306 3a227e9a 2021-03-18 op errx(1, "verification for %s is %s: %s",
307 3a227e9a 2021-03-18 op e->domain, errstr, p);
308 3a227e9a 2021-03-18 op break;
309 3a227e9a 2021-03-18 op default:
310 3a227e9a 2021-03-18 op abort();
311 3a227e9a 2021-03-18 op }
312 3a227e9a 2021-03-18 op i++;
313 3a227e9a 2021-03-18 op }
314 3a227e9a 2021-03-18 op
315 3a227e9a 2021-03-18 op if (i != 0 && i != 3)
316 3a227e9a 2021-03-18 op abort();
317 3a227e9a 2021-03-18 op
318 3a227e9a 2021-03-18 op if (i != 0)
319 3a227e9a 2021-03-18 op telescope_ohash_insert(h, e);
320 3a227e9a 2021-03-18 op }
321 3a227e9a 2021-03-18 op
322 3a227e9a 2021-03-18 op free(line);
323 3a227e9a 2021-03-18 op return ferror(f);
324 3a227e9a 2021-03-18 op }
325 c7107cec 2021-04-01 op
326 c7107cec 2021-04-01 op int
327 c7107cec 2021-04-01 op load_last_session(void (*cb)(const char*))
328 c7107cec 2021-04-01 op {
329 c7107cec 2021-04-01 op char *nl, *line = NULL;
330 c7107cec 2021-04-01 op int e;
331 c7107cec 2021-04-01 op size_t linesize = 0;
332 c7107cec 2021-04-01 op ssize_t linelen;
333 c7107cec 2021-04-01 op FILE *session;
334 c7107cec 2021-04-01 op
335 c7107cec 2021-04-01 op if ((session = fopen(session_file, "r")) == NULL)
336 c7107cec 2021-04-01 op return 0;
337 c7107cec 2021-04-01 op
338 c7107cec 2021-04-01 op while ((linelen = getline(&line, &linesize, session)) != -1) {
339 c7107cec 2021-04-01 op if ((nl = strchr(line, '\n')) != NULL)
340 c7107cec 2021-04-01 op *nl = '\0';
341 c7107cec 2021-04-01 op cb(line);
342 c7107cec 2021-04-01 op }
343 c7107cec 2021-04-01 op
344 c7107cec 2021-04-01 op free(line);
345 c7107cec 2021-04-01 op e = ferror(session);
346 c7107cec 2021-04-01 op fclose(session);
347 c7107cec 2021-04-01 op
348 c7107cec 2021-04-01 op return !e;
349 c7107cec 2021-04-01 op }