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