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 <sys/stat.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <limits.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
33 #include "telescope.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*);
48 static int fs_send_ui(int, uint32_t, int, const void *, uint16_t);
50 static struct imsgev *iev_ui;
51 static FILE *session;
53 static char bookmark_file[PATH_MAX];
54 static char known_hosts_file[PATH_MAX], known_hosts_tmp[PATH_MAX];
55 static char session_file[PATH_MAX];
57 static imsg_handlerfn *handlers[] = {
58 [IMSG_GET] = handle_get,
59 [IMSG_QUIT] = handle_quit,
60 [IMSG_BOOKMARK_PAGE] = handle_bookmark_page,
61 [IMSG_SAVE_CERT] = handle_save_cert,
62 [IMSG_UPDATE_CERT] = handle_update_cert,
63 [IMSG_FILE_OPEN] = handle_file_open,
64 [IMSG_SESSION_START] = handle_session_start,
65 [IMSG_SESSION_TAB] = handle_session_tab,
66 [IMSG_SESSION_END] = handle_session_end,
67 };
69 static void __attribute__((__noreturn__))
70 die(void)
71 {
72 abort(); /* TODO */
73 }
75 static void
76 serve_bookmarks(uint32_t peerid)
77 {
78 const char *t;
79 char buf[BUFSIZ];
80 size_t r;
81 FILE *f;
83 if ((f = fopen(bookmark_file, "r")) == NULL) {
84 t = "# Bookmarks\n\n"
85 "No bookmarks yet!\n"
86 "Create ~/.telescope/bookmarks.gmi or use `bookmark-page'.\n";
87 fs_send_ui(IMSG_BUF, peerid, -1, t, strlen(t));
88 fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
89 return;
90 }
92 for (;;) {
93 r = fread(buf, 1, sizeof(buf), f);
94 fs_send_ui(IMSG_BUF, peerid, -1, buf, r);
95 if (r != sizeof(buf))
96 break;
97 }
99 fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
101 fclose(f);
104 static void
105 send_page(struct imsg *imsg, const char *page)
107 fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, page, strlen(page));
108 fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
111 static void
112 handle_get(struct imsg *imsg, size_t datalen)
114 char *data;
115 const char *p;
117 data = imsg->data;
119 if (data[datalen-1] != '\0')
120 die();
122 if (!strcmp(data, "about:about")) {
123 send_page(imsg, about_about);
124 } else if (!strcmp(data, "about:blank")) {
125 send_page(imsg, about_blank);
126 } else if (!strcmp(data, "about:bookmarks")) {
127 serve_bookmarks(imsg->hdr.peerid);
128 } else if (!strcmp(data, "about:help")) {
129 send_page(imsg, about_help);
130 } else if (!strcmp(data, "about:new")) {
131 send_page(imsg, about_new);
132 } else {
133 p = "# not found!\n";
134 fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, p, strlen(p));
135 fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
139 static void
140 handle_quit(struct imsg *imsg, size_t datalen)
142 event_loopbreak();
145 static void
146 handle_bookmark_page(struct imsg *imsg, size_t datalen)
148 char *data;
149 int res;
150 FILE *f;
152 data = imsg->data;
153 if (data[datalen-1] != '\0')
154 die();
156 if ((f = fopen(bookmark_file, "a")) == NULL) {
157 res = errno;
158 goto end;
160 fprintf(f, "=> %s\n", data);
161 fclose(f);
163 res = 0;
164 end:
165 fs_send_ui(IMSG_BOOKMARK_OK, 0, -1, &res, sizeof(res));
168 static void
169 handle_save_cert(struct imsg *imsg, size_t datalen)
171 struct tofu_entry e;
172 FILE *f;
173 int res;
175 /* TODO: traverse the file to avoid duplications? */
177 if (datalen != sizeof(e))
178 die();
179 memcpy(&e, imsg->data, datalen);
181 if ((f = fopen(known_hosts_file, "a")) == NULL) {
182 res = errno;
183 goto end;
185 fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
186 fclose(f);
188 res = 0;
189 end:
190 fs_send_ui(IMSG_SAVE_CERT_OK, imsg->hdr.peerid, -1,
191 &res, sizeof(res));
194 static void
195 handle_update_cert(struct imsg *imsg, size_t datalen)
197 FILE *tmp, *f;
198 struct tofu_entry entry;
199 char sfn[PATH_MAX], *line = NULL, *t;
200 size_t l, linesize = 0;
201 ssize_t linelen;
202 int fd, e, res = 0;
204 if (datalen != sizeof(entry))
205 die();
206 memcpy(&entry, imsg->data, datalen);
208 strlcpy(sfn, known_hosts_tmp, sizeof(sfn));
209 if ((fd = mkstemp(sfn)) == -1 ||
210 (tmp = fdopen(fd, "w")) == NULL) {
211 if (fd != -1) {
212 unlink(sfn);
213 close(fd);
215 res = 0;
216 goto end;
219 if ((f = fopen(known_hosts_file, "r")) == NULL) {
220 unlink(sfn);
221 fclose(tmp);
222 res = 0;
223 goto end;
226 l = strlen(entry.domain);
227 while ((linelen = getline(&line, &linesize, f)) != -1) {
228 if ((t = strstr(line, entry.domain)) != NULL &&
229 (line[l] == ' ' || line[l] == '\t'))
230 continue;
231 /* line has a trailing \n */
232 fprintf(tmp, "%s", line);
234 fprintf(tmp, "%s %s %d\n", entry.domain, entry.hash, entry.verified);
236 free(line);
237 e = ferror(tmp);
239 fclose(tmp);
240 fclose(f);
242 if (e) {
243 unlink(sfn);
244 res = 0;
245 goto end;
248 res = rename(sfn, known_hosts_file) != -1;
250 end:
251 fs_send_ui(IMSG_UPDATE_CERT_OK, imsg->hdr.peerid, -1,
252 &res, sizeof(res));
255 static void
256 handle_file_open(struct imsg *imsg, size_t datalen)
258 char *path, *e;
259 int fd;
261 path = imsg->data;
262 if (path[datalen-1] != '\0')
263 die();
265 if ((fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644)) == -1) {
266 e = strerror(errno);
267 fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, -1,
268 e, strlen(e)+1);
269 } else
270 fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, fd,
271 NULL, 0);
274 static void
275 handle_session_start(struct imsg *imsg, size_t datalen)
277 if (datalen != 0)
278 die();
280 if ((session = fopen(session_file, "w")) == NULL)
281 die();
284 static void
285 handle_session_tab(struct imsg *imsg, size_t datalen)
287 if (session == NULL)
288 die();
290 if (datalen == 0)
291 die();
293 /* skip the NUL-terminator */
294 fwrite(imsg->data, 1, datalen-1, session);
296 fprintf(session, "\n");
299 static void
300 handle_session_end(struct imsg *imsg, size_t datalen)
302 if (session == NULL)
303 die();
304 fclose(session);
305 session = NULL;
308 static void
309 handle_dispatch_imsg(int fd, short ev, void *d)
311 struct imsgev *iev = d;
312 dispatch_imsg(iev, ev, handlers, sizeof(handlers));
315 static int
316 fs_send_ui(int type, uint32_t peerid, int fd, const void *data,
317 uint16_t datalen)
319 return imsg_compose_event(iev_ui, type, peerid, 0, fd,
320 data, datalen);
323 int
324 fs_init(void)
326 char dir[PATH_MAX];
328 strlcpy(dir, getenv("HOME"), sizeof(dir));
329 strlcat(dir, "/.telescope", sizeof(dir));
330 mkdir(dir, 0700);
332 strlcpy(bookmark_file, getenv("HOME"), sizeof(bookmark_file));
333 strlcat(bookmark_file, "/.telescope/bookmarks.gmi", sizeof(bookmark_file));
335 strlcpy(known_hosts_file, getenv("HOME"), sizeof(known_hosts_file));
336 strlcat(known_hosts_file, "/.telescope/known_hosts", sizeof(known_hosts_file));
338 strlcpy(known_hosts_tmp, getenv("HOME"), sizeof(known_hosts_tmp));
339 strlcat(known_hosts_tmp, "/.telescope/known_hosts.tmp.XXXXXXXXXX",
340 sizeof(known_hosts_file));
342 strlcpy(session_file, getenv("HOME"), sizeof(session_file));
343 strlcat(session_file, "/.telescope/session", sizeof(session_file));
345 return 1;
348 int
349 fs_main(void)
351 setproctitle("fs");
353 fs_init();
355 event_init();
357 /* Setup pipe and event handler to the main process */
358 if ((iev_ui = malloc(sizeof(*iev_ui))) == NULL)
359 die();
360 imsg_init(&iev_ui->ibuf, 3);
361 iev_ui->handler = handle_dispatch_imsg;
362 iev_ui->events = EV_READ;
363 event_set(&iev_ui->ev, iev_ui->ibuf.fd, iev_ui->events,
364 iev_ui->handler, iev_ui);
365 event_add(&iev_ui->ev, 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;