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 <fcntl.h>
29 #include <limits.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
35 static void die(void) __attribute__((__noreturn__));
36 static void serve_bookmarks(uint32_t);
37 static void send_page(struct imsg *, const char *);
38 static void handle_get(struct imsg*, size_t);
39 static void handle_quit(struct imsg*, size_t);
40 static void handle_bookmark_page(struct imsg*, size_t);
41 static void handle_save_cert(struct imsg*, size_t);
42 static void handle_update_cert(struct imsg*, size_t);
43 static void handle_file_open(struct imsg*, size_t);
44 static void handle_session_start(struct imsg*, size_t);
45 static void handle_session_tab(struct imsg*, size_t);
46 static void handle_session_end(struct imsg*, size_t);
47 static void handle_dispatch_imsg(int, short, void*);
49 static struct event imsgev;
50 static struct imsgbuf *ibuf;
52 static FILE *session;
54 static char bookmark_file[PATH_MAX];
55 static char known_hosts_file[PATH_MAX], known_hosts_tmp[PATH_MAX];
56 static char session_file[PATH_MAX];
58 static imsg_handlerfn *handlers[] = {
59 [IMSG_GET] = handle_get,
60 [IMSG_QUIT] = handle_quit,
61 [IMSG_BOOKMARK_PAGE] = handle_bookmark_page,
62 [IMSG_SAVE_CERT] = handle_save_cert,
63 [IMSG_UPDATE_CERT] = handle_update_cert,
64 [IMSG_FILE_OPEN] = handle_file_open,
65 [IMSG_SESSION_START] = handle_session_start,
66 [IMSG_SESSION_TAB] = handle_session_tab,
67 [IMSG_SESSION_END] = handle_session_end,
68 };
70 static void __attribute__((__noreturn__))
71 die(void)
72 {
73 abort(); /* TODO */
74 }
76 static void
77 serve_bookmarks(uint32_t peerid)
78 {
79 const char *t;
80 char buf[BUFSIZ];
81 size_t r;
82 FILE *f;
84 if ((f = fopen(bookmark_file, "r")) == NULL) {
85 t = "# Bookmarks\n\n"
86 "No bookmarks yet!\n"
87 "Create ~/.telescope/bookmarks.gmi or use `bookmark-page'.\n";
88 imsg_compose(ibuf, IMSG_BUF, peerid, 0, -1, t, strlen(t));
89 imsg_compose(ibuf, IMSG_EOF, peerid, 0, -1, NULL, 0);
90 imsg_flush(ibuf);
91 return;
92 }
94 for (;;) {
95 r = fread(buf, 1, sizeof(buf), f);
96 imsg_compose(ibuf, IMSG_BUF, peerid, 0, -1, buf, r);
97 imsg_flush(ibuf);
98 if (r != sizeof(buf))
99 break;
102 imsg_compose(ibuf, IMSG_EOF, peerid, 0, -1, NULL, 0);
103 imsg_flush(ibuf);
105 fclose(f);
108 static void
109 send_page(struct imsg *imsg, const char *page)
111 imsg_compose(ibuf, IMSG_BUF, imsg->hdr.peerid, 0, -1,
112 page, strlen(page));
113 imsg_compose(ibuf, IMSG_EOF, imsg->hdr.peerid, 0, -1, NULL, 0);
114 imsg_flush(ibuf);
117 static void
118 handle_get(struct imsg *imsg, size_t datalen)
120 char *data;
121 const char *p;
123 data = imsg->data;
125 if (data[datalen-1] != '\0')
126 die();
128 if (!strcmp(data, "about:about")) {
129 send_page(imsg, about_about);
130 } else if (!strcmp(data, "about:blank")) {
131 send_page(imsg, about_blank);
132 } else if (!strcmp(data, "about:bookmarks")) {
133 serve_bookmarks(imsg->hdr.peerid);
134 } else if (!strcmp(data, "about:help")) {
135 send_page(imsg, about_help);
136 } else if (!strcmp(data, "about:new")) {
137 send_page(imsg, about_new);
138 } else {
139 p = "# not found!\n";
140 imsg_compose(ibuf, IMSG_BUF, imsg->hdr.peerid, 0, -1, p, strlen(p));
141 imsg_compose(ibuf, IMSG_EOF, imsg->hdr.peerid, 0, -1, NULL, 0);
142 imsg_flush(ibuf);
146 static void
147 handle_quit(struct imsg *imsg, size_t datalen)
149 event_loopbreak();
152 static void
153 handle_bookmark_page(struct imsg *imsg, size_t datalen)
155 char *data;
156 int res;
157 FILE *f;
159 data = imsg->data;
160 if (data[datalen-1] != '\0')
161 die();
163 if ((f = fopen(bookmark_file, "a")) == NULL) {
164 res = errno;
165 goto end;
167 fprintf(f, "=> %s\n", data);
168 fclose(f);
170 res = 0;
171 end:
172 imsg_compose(ibuf, IMSG_BOOKMARK_OK, 0, 0, -1, &res, sizeof(res));
173 imsg_flush(ibuf);
176 static void
177 handle_save_cert(struct imsg *imsg, size_t datalen)
179 struct tofu_entry e;
180 FILE *f;
181 int res;
183 /* TODO: traverse the file to avoid duplications? */
185 if (datalen != sizeof(e))
186 die();
187 memcpy(&e, imsg->data, datalen);
189 if ((f = fopen(known_hosts_file, "a")) == NULL) {
190 res = errno;
191 goto end;
193 fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
194 fclose(f);
196 res = 0;
197 end:
198 imsg_compose(ibuf, IMSG_SAVE_CERT_OK, imsg->hdr.peerid, 0, -1,
199 &res, sizeof(res));
200 imsg_flush(ibuf);
203 static void
204 handle_update_cert(struct imsg *imsg, size_t datalen)
206 FILE *tmp, *f;
207 struct tofu_entry entry;
208 char sfn[PATH_MAX], *line = NULL, *t;
209 size_t l, linesize = 0;
210 ssize_t linelen;
211 int fd, e, res = 0;
213 if (datalen != sizeof(entry))
214 die();
215 memcpy(&entry, imsg->data, datalen);
217 strlcpy(sfn, known_hosts_tmp, sizeof(sfn));
218 if ((fd = mkstemp(sfn)) == -1 ||
219 (tmp = fdopen(fd, "w")) == NULL) {
220 if (fd != -1) {
221 unlink(sfn);
222 close(fd);
224 res = 0;
225 goto end;
228 if ((f = fopen(known_hosts_file, "r")) == NULL) {
229 unlink(sfn);
230 fclose(tmp);
231 res = 0;
232 goto end;
235 l = strlen(entry.domain);
236 while ((linelen = getline(&line, &linesize, f)) != -1) {
237 if ((t = strstr(line, entry.domain)) != NULL &&
238 (line[l] == ' ' || line[l] == '\t'))
239 continue;
240 /* line has a trailing \n */
241 fprintf(tmp, "%s", line);
243 fprintf(tmp, "%s %s %d\n", entry.domain, entry.hash, entry.verified);
245 free(line);
246 e = ferror(tmp);
248 fclose(tmp);
249 fclose(f);
251 if (e) {
252 unlink(sfn);
253 res = 0;
254 goto end;
257 res = rename(sfn, known_hosts_file) != -1;
259 end:
260 imsg_compose(ibuf, IMSG_UPDATE_CERT_OK, imsg->hdr.peerid, 0, -1,
261 &res, sizeof(res));
262 imsg_flush(ibuf);
265 static void
266 handle_file_open(struct imsg *imsg, size_t datalen)
268 char *path, *e;
269 int fd;
271 path = imsg->data;
272 if (path[datalen-1] != '\0')
273 die();
275 if ((fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644)) == -1) {
276 e = strerror(errno);
277 imsg_compose(ibuf, IMSG_FILE_OPENED, imsg->hdr.peerid, 0, -1,
278 e, strlen(e)+1);
279 } else
280 imsg_compose(ibuf, IMSG_FILE_OPENED, imsg->hdr.peerid, 0, fd,
281 NULL, 0);
283 imsg_flush(ibuf);
286 static void
287 handle_session_start(struct imsg *imsg, size_t datalen)
289 if (datalen != 0)
290 die();
292 if ((session = fopen(session_file, "w")) == NULL)
293 die();
296 static void
297 handle_session_tab(struct imsg *imsg, size_t datalen)
299 if (session == NULL)
300 die();
302 if (datalen == 0)
303 die();
305 /* skip the NUL-terminator */
306 fwrite(imsg->data, 1, datalen-1, session);
308 fprintf(session, "\n");
311 static void
312 handle_session_end(struct imsg *imsg, size_t datalen)
314 if (session == NULL)
315 die();
316 fclose(session);
317 session = NULL;
320 static void
321 handle_dispatch_imsg(int fd, short ev, void *d)
323 struct imsgbuf *ibuf = d;
324 dispatch_imsg(ibuf, handlers, sizeof(handlers));
327 int
328 fs_init(void)
330 char dir[PATH_MAX];
332 strlcpy(dir, getenv("HOME"), sizeof(dir));
333 strlcat(dir, "/.telescope", sizeof(dir));
334 mkdir(dir, 0700);
336 strlcpy(bookmark_file, getenv("HOME"), sizeof(bookmark_file));
337 strlcat(bookmark_file, "/.telescope/bookmarks.gmi", sizeof(bookmark_file));
339 strlcpy(known_hosts_file, getenv("HOME"), sizeof(known_hosts_file));
340 strlcat(known_hosts_file, "/.telescope/known_hosts", sizeof(known_hosts_file));
342 strlcpy(known_hosts_tmp, getenv("HOME"), sizeof(known_hosts_tmp));
343 strlcat(known_hosts_tmp, "/.telescope/known_hosts.tmp.XXXXXXXXXX",
344 sizeof(known_hosts_file));
346 strlcpy(session_file, getenv("HOME"), sizeof(session_file));
347 strlcat(session_file, "/.telescope/session", sizeof(session_file));
349 return 1;
352 int
353 fs_main(void)
355 setproctitle("fs");
357 fs_init();
359 event_init();
361 if ((ibuf = calloc(1, sizeof(*ibuf))) == NULL)
362 die();
363 imsg_init(ibuf, 3);
364 event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, ibuf);
365 event_add(&imsgev, NULL);
367 sandbox_fs_process();
369 event_dispatch();
370 return 0;
375 static int
376 parse_khost_line(char *line, char *tmp[3])
378 char **ap;
380 for (ap = tmp; ap < &tmp[3] &&
381 (*ap = strsep(&line, " \t\n")) != NULL;) {
382 if (**ap != '\0')
383 ap++;
386 return ap == &tmp[3] && *line == '\0';
389 int
390 load_certs(struct ohash *h)
392 char *tmp[3], *line = NULL;
393 const char *errstr;
394 size_t lineno = 0, linesize = 0;
395 ssize_t linelen;
396 FILE *f;
397 struct tofu_entry *e;
399 if ((f = fopen(known_hosts_file, "r")) == NULL)
400 return 0;
402 while ((linelen = getline(&line, &linesize, f)) != -1) {
403 if ((e = calloc(1, sizeof(*e))) == NULL)
404 abort();
406 lineno++;
408 if (parse_khost_line(line, tmp)) {
409 strlcpy(e->domain, tmp[0], sizeof(e->domain));
410 strlcpy(e->hash, tmp[1], sizeof(e->hash));
412 e->verified = strtonum(tmp[2], 0, 1, &errstr);
413 if (errstr != NULL)
414 errx(1, "%s:%zu verification for %s is %s: %s",
415 known_hosts_file, lineno,
416 e->domain, errstr, tmp[2]);
417 tofu_add(h, e);
418 } else {
419 warnx("%s:%zu invalid entry",
420 known_hosts_file, lineno);
421 free(e);
425 free(line);
426 return ferror(f);
429 int
430 load_last_session(void (*cb)(const char*))
432 char *nl, *line = NULL;
433 int e;
434 size_t linesize = 0;
435 ssize_t linelen;
436 FILE *session;
438 if ((session = fopen(session_file, "r")) == NULL) {
439 /* first time? */
440 cb("about:help");
441 return 0;
444 while ((linelen = getline(&line, &linesize, session)) != -1) {
445 if ((nl = strchr(line, '\n')) != NULL)
446 *nl = '\0';
447 cb(line);
450 free(line);
451 e = ferror(session);
452 fclose(session);
454 return !e;