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"
34 #include "pages.h"
36 static void die(void) __attribute__((__noreturn__));
37 static void serve_bookmarks(uint32_t);
38 static void send_page(struct imsg *, const char *);
39 static void handle_get(struct imsg*, size_t);
40 static void handle_quit(struct imsg*, size_t);
41 static void handle_bookmark_page(struct imsg*, size_t);
42 static void handle_save_cert(struct imsg*, size_t);
43 static void handle_update_cert(struct imsg*, size_t);
44 static void handle_file_open(struct imsg*, size_t);
45 static void handle_session_start(struct imsg*, size_t);
46 static void handle_session_tab(struct imsg*, size_t);
47 static void handle_session_end(struct imsg*, size_t);
48 static void handle_dispatch_imsg(int, short, void*);
49 static int fs_send_ui(int, uint32_t, int, const void *, uint16_t);
51 static struct imsgev *iev_ui;
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 fs_send_ui(IMSG_BUF, peerid, -1, t, strlen(t));
89 fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
90 return;
91 }
93 for (;;) {
94 r = fread(buf, 1, sizeof(buf), f);
95 fs_send_ui(IMSG_BUF, peerid, -1, buf, r);
96 if (r != sizeof(buf))
97 break;
98 }
100 fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
102 fclose(f);
105 static void
106 send_page(struct imsg *imsg, const char *page)
108 fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, page, strlen(page));
109 fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
112 static void
113 handle_get(struct imsg *imsg, size_t datalen)
115 char *data;
116 const char *p;
118 data = imsg->data;
120 if (data[datalen-1] != '\0')
121 die();
123 if (!strcmp(data, "about:about")) {
124 send_page(imsg, about_about);
125 } else if (!strcmp(data, "about:blank")) {
126 send_page(imsg, about_blank);
127 } else if (!strcmp(data, "about:bookmarks")) {
128 serve_bookmarks(imsg->hdr.peerid);
129 } else if (!strcmp(data, "about:help")) {
130 send_page(imsg, about_help);
131 } else if (!strcmp(data, "about:new")) {
132 send_page(imsg, about_new);
133 } else {
134 p = "# not found!\n";
135 fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, p, strlen(p));
136 fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
140 static void
141 handle_quit(struct imsg *imsg, size_t datalen)
143 event_loopbreak();
146 static void
147 handle_bookmark_page(struct imsg *imsg, size_t datalen)
149 char *data;
150 int res;
151 FILE *f;
153 data = imsg->data;
154 if (data[datalen-1] != '\0')
155 die();
157 if ((f = fopen(bookmark_file, "a")) == NULL) {
158 res = errno;
159 goto end;
161 fprintf(f, "=> %s\n", data);
162 fclose(f);
164 res = 0;
165 end:
166 fs_send_ui(IMSG_BOOKMARK_OK, 0, -1, &res, sizeof(res));
169 static void
170 handle_save_cert(struct imsg *imsg, size_t datalen)
172 struct tofu_entry e;
173 FILE *f;
174 int res;
176 /* TODO: traverse the file to avoid duplications? */
178 if (datalen != sizeof(e))
179 die();
180 memcpy(&e, imsg->data, datalen);
182 if ((f = fopen(known_hosts_file, "a")) == NULL) {
183 res = errno;
184 goto end;
186 fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
187 fclose(f);
189 res = 0;
190 end:
191 fs_send_ui(IMSG_SAVE_CERT_OK, imsg->hdr.peerid, -1,
192 &res, sizeof(res));
195 static void
196 handle_update_cert(struct imsg *imsg, size_t datalen)
198 FILE *tmp, *f;
199 struct tofu_entry entry;
200 char sfn[PATH_MAX], *line = NULL, *t;
201 size_t l, linesize = 0;
202 ssize_t linelen;
203 int fd, e, res = 0;
205 if (datalen != sizeof(entry))
206 die();
207 memcpy(&entry, imsg->data, datalen);
209 strlcpy(sfn, known_hosts_tmp, sizeof(sfn));
210 if ((fd = mkstemp(sfn)) == -1 ||
211 (tmp = fdopen(fd, "w")) == NULL) {
212 if (fd != -1) {
213 unlink(sfn);
214 close(fd);
216 res = 0;
217 goto end;
220 if ((f = fopen(known_hosts_file, "r")) == NULL) {
221 unlink(sfn);
222 fclose(tmp);
223 res = 0;
224 goto end;
227 l = strlen(entry.domain);
228 while ((linelen = getline(&line, &linesize, f)) != -1) {
229 if ((t = strstr(line, entry.domain)) != NULL &&
230 (line[l] == ' ' || line[l] == '\t'))
231 continue;
232 /* line has a trailing \n */
233 fprintf(tmp, "%s", line);
235 fprintf(tmp, "%s %s %d\n", entry.domain, entry.hash, entry.verified);
237 free(line);
238 e = ferror(tmp);
240 fclose(tmp);
241 fclose(f);
243 if (e) {
244 unlink(sfn);
245 res = 0;
246 goto end;
249 res = rename(sfn, known_hosts_file) != -1;
251 end:
252 fs_send_ui(IMSG_UPDATE_CERT_OK, imsg->hdr.peerid, -1,
253 &res, sizeof(res));
256 static void
257 handle_file_open(struct imsg *imsg, size_t datalen)
259 char *path, *e;
260 int fd;
262 path = imsg->data;
263 if (path[datalen-1] != '\0')
264 die();
266 if ((fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644)) == -1) {
267 e = strerror(errno);
268 fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, -1,
269 e, strlen(e)+1);
270 } else
271 fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, fd,
272 NULL, 0);
275 static void
276 handle_session_start(struct imsg *imsg, size_t datalen)
278 if (datalen != 0)
279 die();
281 if ((session = fopen(session_file, "w")) == NULL)
282 die();
285 static void
286 handle_session_tab(struct imsg *imsg, size_t datalen)
288 if (session == NULL)
289 die();
291 if (datalen == 0)
292 die();
294 /* skip the NUL-terminator */
295 fwrite(imsg->data, 1, datalen-1, session);
297 fprintf(session, "\n");
300 static void
301 handle_session_end(struct imsg *imsg, size_t datalen)
303 if (session == NULL)
304 die();
305 fclose(session);
306 session = NULL;
309 static void
310 handle_dispatch_imsg(int fd, short ev, void *d)
312 struct imsgev *iev = d;
313 dispatch_imsg(iev, ev, handlers, sizeof(handlers));
316 static int
317 fs_send_ui(int type, uint32_t peerid, int fd, const void *data,
318 uint16_t datalen)
320 return imsg_compose_event(iev_ui, type, peerid, 0, fd,
321 data, datalen);
324 int
325 fs_init(void)
327 char dir[PATH_MAX];
329 strlcpy(dir, getenv("HOME"), sizeof(dir));
330 strlcat(dir, "/.telescope", sizeof(dir));
331 mkdir(dir, 0700);
333 strlcpy(bookmark_file, getenv("HOME"), sizeof(bookmark_file));
334 strlcat(bookmark_file, "/.telescope/bookmarks.gmi", sizeof(bookmark_file));
336 strlcpy(known_hosts_file, getenv("HOME"), sizeof(known_hosts_file));
337 strlcat(known_hosts_file, "/.telescope/known_hosts", sizeof(known_hosts_file));
339 strlcpy(known_hosts_tmp, getenv("HOME"), sizeof(known_hosts_tmp));
340 strlcat(known_hosts_tmp, "/.telescope/known_hosts.tmp.XXXXXXXXXX",
341 sizeof(known_hosts_file));
343 strlcpy(session_file, getenv("HOME"), sizeof(session_file));
344 strlcat(session_file, "/.telescope/session", sizeof(session_file));
346 return 1;
349 int
350 fs_main(void)
352 setproctitle("fs");
354 fs_init();
356 event_init();
358 /* Setup pipe and event handler to the main process */
359 if ((iev_ui = malloc(sizeof(*iev_ui))) == NULL)
360 die();
361 imsg_init(&iev_ui->ibuf, 3);
362 iev_ui->handler = handle_dispatch_imsg;
363 iev_ui->events = EV_READ;
364 event_set(&iev_ui->ev, iev_ui->ibuf.fd, iev_ui->events,
365 iev_ui->handler, iev_ui);
366 event_add(&iev_ui->ev, NULL);
368 sandbox_fs_process();
370 event_dispatch();
371 return 0;
376 static int
377 parse_khost_line(char *line, char *tmp[3])
379 char **ap;
381 for (ap = tmp; ap < &tmp[3] &&
382 (*ap = strsep(&line, " \t\n")) != NULL;) {
383 if (**ap != '\0')
384 ap++;
387 return ap == &tmp[3] && *line == '\0';
390 int
391 load_certs(struct ohash *h)
393 char *tmp[3], *line = NULL;
394 const char *errstr;
395 size_t lineno = 0, linesize = 0;
396 ssize_t linelen;
397 FILE *f;
398 struct tofu_entry *e;
400 if ((f = fopen(known_hosts_file, "r")) == NULL)
401 return 0;
403 while ((linelen = getline(&line, &linesize, f)) != -1) {
404 if ((e = calloc(1, sizeof(*e))) == NULL)
405 abort();
407 lineno++;
409 if (parse_khost_line(line, tmp)) {
410 strlcpy(e->domain, tmp[0], sizeof(e->domain));
411 strlcpy(e->hash, tmp[1], sizeof(e->hash));
413 e->verified = strtonum(tmp[2], 0, 1, &errstr);
414 if (errstr != NULL)
415 errx(1, "%s:%zu verification for %s is %s: %s",
416 known_hosts_file, lineno,
417 e->domain, errstr, tmp[2]);
418 tofu_add(h, e);
419 } else {
420 warnx("%s:%zu invalid entry",
421 known_hosts_file, lineno);
422 free(e);
426 free(line);
427 return ferror(f);
430 int
431 load_last_session(void (*cb)(const char*))
433 char *nl, *line = NULL;
434 int e;
435 size_t linesize = 0;
436 ssize_t linelen;
437 FILE *session;
439 if ((session = fopen(session_file, "r")) == NULL) {
440 /* first time? */
441 cb("about:help");
442 return 0;
445 while ((linelen = getline(&line, &linesize, session)) != -1) {
446 if ((nl = strchr(line, '\n')) != NULL)
447 *nl = '\0';
448 cb(line);
451 free(line);
452 e = ferror(session);
453 fclose(session);
455 return !e;