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 35e1f40a 2021-03-14 op
23 6cd6a9e1 2021-03-20 op #include <sys/stat.h>
24 6cd6a9e1 2021-03-20 op
25 35e1f40a 2021-03-14 op #include <errno.h>
26 de2a69bb 2021-05-17 op #include <fcntl.h>
27 35e1f40a 2021-03-14 op #include <limits.h>
28 35e1f40a 2021-03-14 op #include <stdio.h>
29 35e1f40a 2021-03-14 op #include <stdlib.h>
30 35e1f40a 2021-03-14 op #include <string.h>
31 35e1f40a 2021-03-14 op #include <unistd.h>
32 4913b479 2021-07-12 op
33 4913b479 2021-07-12 op #include "telescope.h"
34 5a824be4 2021-07-13 op #include "pages.h"
35 35e1f40a 2021-03-14 op
36 35e1f40a 2021-03-14 op static void die(void) __attribute__((__noreturn__));
37 35e1f40a 2021-03-14 op static void serve_bookmarks(uint32_t);
38 a2728733 2021-07-18 op static void send_page(struct imsg *, const uint8_t *, size_t);
39 35e1f40a 2021-03-14 op static void handle_get(struct imsg*, size_t);
40 35e1f40a 2021-03-14 op static void handle_quit(struct imsg*, size_t);
41 740f578b 2021-03-15 op static void handle_bookmark_page(struct imsg*, size_t);
42 3a227e9a 2021-03-18 op static void handle_save_cert(struct imsg*, size_t);
43 288fd238 2021-04-25 op static void handle_update_cert(struct imsg*, size_t);
44 de2a69bb 2021-05-17 op static void handle_file_open(struct imsg*, size_t);
45 c7107cec 2021-04-01 op static void handle_session_start(struct imsg*, size_t);
46 c7107cec 2021-04-01 op static void handle_session_tab(struct imsg*, size_t);
47 2f524b17 2021-07-17 op static void handle_session_tab_title(struct imsg*, size_t);
48 c7107cec 2021-04-01 op static void handle_session_end(struct imsg*, size_t);
49 1304bbdd 2021-03-15 op static void handle_dispatch_imsg(int, short, void*);
50 bc10f6a5 2021-07-12 op static int fs_send_ui(int, uint32_t, int, const void *, uint16_t);
51 35e1f40a 2021-03-14 op
52 bc10f6a5 2021-07-12 op static struct imsgev *iev_ui;
53 c7107cec 2021-04-01 op static FILE *session;
54 c7107cec 2021-04-01 op
55 d0fd368a 2021-07-15 op static char lockfile_path[PATH_MAX];
56 740f578b 2021-03-15 op static char bookmark_file[PATH_MAX];
57 288fd238 2021-04-25 op static char known_hosts_file[PATH_MAX], known_hosts_tmp[PATH_MAX];
58 ea080950 2021-07-20 op static char crashed_file[PATH_MAX];
59 87e3e801 2021-07-17 op
60 87e3e801 2021-07-17 op char session_file[PATH_MAX];
61 740f578b 2021-03-15 op
62 35e1f40a 2021-03-14 op static imsg_handlerfn *handlers[] = {
63 35e1f40a 2021-03-14 op [IMSG_GET] = handle_get,
64 35e1f40a 2021-03-14 op [IMSG_QUIT] = handle_quit,
65 740f578b 2021-03-15 op [IMSG_BOOKMARK_PAGE] = handle_bookmark_page,
66 3a227e9a 2021-03-18 op [IMSG_SAVE_CERT] = handle_save_cert,
67 288fd238 2021-04-25 op [IMSG_UPDATE_CERT] = handle_update_cert,
68 de2a69bb 2021-05-17 op [IMSG_FILE_OPEN] = handle_file_open,
69 c7107cec 2021-04-01 op [IMSG_SESSION_START] = handle_session_start,
70 c7107cec 2021-04-01 op [IMSG_SESSION_TAB] = handle_session_tab,
71 2f524b17 2021-07-17 op [IMSG_SESSION_TAB_TITLE] = handle_session_tab_title,
72 c7107cec 2021-04-01 op [IMSG_SESSION_END] = handle_session_end,
73 35e1f40a 2021-03-14 op };
74 35e1f40a 2021-03-14 op
75 35e1f40a 2021-03-14 op static void __attribute__((__noreturn__))
76 35e1f40a 2021-03-14 op die(void)
77 35e1f40a 2021-03-14 op {
78 35e1f40a 2021-03-14 op abort(); /* TODO */
79 35e1f40a 2021-03-14 op }
80 35e1f40a 2021-03-14 op
81 35e1f40a 2021-03-14 op static void
82 35e1f40a 2021-03-14 op serve_bookmarks(uint32_t peerid)
83 35e1f40a 2021-03-14 op {
84 35e1f40a 2021-03-14 op const char *t;
85 740f578b 2021-03-15 op char buf[BUFSIZ];
86 35e1f40a 2021-03-14 op size_t r;
87 35e1f40a 2021-03-14 op FILE *f;
88 35e1f40a 2021-03-14 op
89 740f578b 2021-03-15 op if ((f = fopen(bookmark_file, "r")) == NULL) {
90 503425db 2021-04-01 op t = "# Bookmarks\n\n"
91 503425db 2021-04-01 op "No bookmarks yet!\n"
92 503425db 2021-04-01 op "Create ~/.telescope/bookmarks.gmi or use `bookmark-page'.\n";
93 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_BUF, peerid, -1, t, strlen(t));
94 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
95 35e1f40a 2021-03-14 op return;
96 35e1f40a 2021-03-14 op }
97 35e1f40a 2021-03-14 op
98 35e1f40a 2021-03-14 op for (;;) {
99 35e1f40a 2021-03-14 op r = fread(buf, 1, sizeof(buf), f);
100 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_BUF, peerid, -1, buf, r);
101 35e1f40a 2021-03-14 op if (r != sizeof(buf))
102 35e1f40a 2021-03-14 op break;
103 35e1f40a 2021-03-14 op }
104 35e1f40a 2021-03-14 op
105 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
106 35e1f40a 2021-03-14 op
107 35e1f40a 2021-03-14 op fclose(f);
108 35e1f40a 2021-03-14 op }
109 35e1f40a 2021-03-14 op
110 35e1f40a 2021-03-14 op static void
111 a2728733 2021-07-18 op send_page(struct imsg *imsg, const uint8_t *page, size_t len)
112 1b412079 2021-06-19 op {
113 a2728733 2021-07-18 op fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, page, len);
114 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
115 1b412079 2021-06-19 op }
116 1b412079 2021-06-19 op
117 1b412079 2021-06-19 op static void
118 35e1f40a 2021-03-14 op handle_get(struct imsg *imsg, size_t datalen)
119 35e1f40a 2021-03-14 op {
120 a2728733 2021-07-18 op const char *data, *p;
121 a2728733 2021-07-18 op size_t i;
122 a2728733 2021-07-18 op struct page {
123 a2728733 2021-07-18 op const char *name;
124 a2728733 2021-07-18 op void (*handle)(uint32_t);
125 a2728733 2021-07-18 op const uint8_t *data;
126 a2728733 2021-07-18 op size_t len;
127 a2728733 2021-07-18 op } pages[] = {
128 a2728733 2021-07-18 op {"about:about", NULL, about_about, about_about_len},
129 a2728733 2021-07-18 op {"about:blank", NULL, about_blank, about_blank_len},
130 a2728733 2021-07-18 op {"about:bookmarks", serve_bookmarks, 0, 0},
131 8064b849 2021-07-20 op {"about:crash", NULL, about_crash, about_crash_len},
132 a2728733 2021-07-18 op {"about:help", NULL, about_help, about_help_len},
133 4745c100 2021-07-18 op {"about:license", NULL, about_license, about_license_len},
134 a2728733 2021-07-18 op {"about:new", NULL, about_new, about_new_len},
135 a2728733 2021-07-18 op };
136 35e1f40a 2021-03-14 op
137 35e1f40a 2021-03-14 op data = imsg->data;
138 35e1f40a 2021-03-14 op
139 35e1f40a 2021-03-14 op if (data[datalen-1] != '\0')
140 35e1f40a 2021-03-14 op die();
141 35e1f40a 2021-03-14 op
142 a2728733 2021-07-18 op for (i = 0; i < sizeof(pages)/sizeof(pages[0]); ++i) {
143 a2728733 2021-07-18 op if (strcmp(data, pages[i].name) != 0)
144 a2728733 2021-07-18 op continue;
145 a2728733 2021-07-18 op
146 a2728733 2021-07-18 op if (pages[i].handle != NULL)
147 a2728733 2021-07-18 op pages[i].handle(imsg->hdr.peerid);
148 a2728733 2021-07-18 op else
149 a2728733 2021-07-18 op send_page(imsg, pages[i].data, pages[i].len);
150 a2728733 2021-07-18 op return;
151 35e1f40a 2021-03-14 op }
152 a2728733 2021-07-18 op
153 a2728733 2021-07-18 op p = "# not found!\n";
154 a2728733 2021-07-18 op fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, p, strlen(p));
155 a2728733 2021-07-18 op fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
156 35e1f40a 2021-03-14 op }
157 35e1f40a 2021-03-14 op
158 35e1f40a 2021-03-14 op static void
159 35e1f40a 2021-03-14 op handle_quit(struct imsg *imsg, size_t datalen)
160 35e1f40a 2021-03-14 op {
161 35e1f40a 2021-03-14 op event_loopbreak();
162 35e1f40a 2021-03-14 op }
163 35e1f40a 2021-03-14 op
164 35e1f40a 2021-03-14 op static void
165 740f578b 2021-03-15 op handle_bookmark_page(struct imsg *imsg, size_t datalen)
166 740f578b 2021-03-15 op {
167 740f578b 2021-03-15 op char *data;
168 740f578b 2021-03-15 op int res;
169 740f578b 2021-03-15 op FILE *f;
170 740f578b 2021-03-15 op
171 740f578b 2021-03-15 op data = imsg->data;
172 740f578b 2021-03-15 op if (data[datalen-1] != '\0')
173 740f578b 2021-03-15 op die();
174 740f578b 2021-03-15 op
175 740f578b 2021-03-15 op if ((f = fopen(bookmark_file, "a")) == NULL) {
176 740f578b 2021-03-15 op res = errno;
177 740f578b 2021-03-15 op goto end;
178 740f578b 2021-03-15 op }
179 740f578b 2021-03-15 op fprintf(f, "=> %s\n", data);
180 740f578b 2021-03-15 op fclose(f);
181 740f578b 2021-03-15 op
182 740f578b 2021-03-15 op res = 0;
183 740f578b 2021-03-15 op end:
184 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_BOOKMARK_OK, 0, -1, &res, sizeof(res));
185 740f578b 2021-03-15 op }
186 740f578b 2021-03-15 op
187 740f578b 2021-03-15 op static void
188 3a227e9a 2021-03-18 op handle_save_cert(struct imsg *imsg, size_t datalen)
189 3a227e9a 2021-03-18 op {
190 3a227e9a 2021-03-18 op struct tofu_entry e;
191 3a227e9a 2021-03-18 op FILE *f;
192 3a227e9a 2021-03-18 op int res;
193 3a227e9a 2021-03-18 op
194 3a227e9a 2021-03-18 op /* TODO: traverse the file to avoid duplications? */
195 3a227e9a 2021-03-18 op
196 3a227e9a 2021-03-18 op if (datalen != sizeof(e))
197 3a227e9a 2021-03-18 op die();
198 3a227e9a 2021-03-18 op memcpy(&e, imsg->data, datalen);
199 3a227e9a 2021-03-18 op
200 3a227e9a 2021-03-18 op if ((f = fopen(known_hosts_file, "a")) == NULL) {
201 3a227e9a 2021-03-18 op res = errno;
202 3a227e9a 2021-03-18 op goto end;
203 3a227e9a 2021-03-18 op }
204 3a227e9a 2021-03-18 op fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
205 3a227e9a 2021-03-18 op fclose(f);
206 3a227e9a 2021-03-18 op
207 3a227e9a 2021-03-18 op res = 0;
208 3a227e9a 2021-03-18 op end:
209 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_SAVE_CERT_OK, imsg->hdr.peerid, -1,
210 288fd238 2021-04-25 op &res, sizeof(res));
211 288fd238 2021-04-25 op }
212 288fd238 2021-04-25 op
213 288fd238 2021-04-25 op static void
214 288fd238 2021-04-25 op handle_update_cert(struct imsg *imsg, size_t datalen)
215 288fd238 2021-04-25 op {
216 288fd238 2021-04-25 op FILE *tmp, *f;
217 288fd238 2021-04-25 op struct tofu_entry entry;
218 288fd238 2021-04-25 op char sfn[PATH_MAX], *line = NULL, *t;
219 288fd238 2021-04-25 op size_t l, linesize = 0;
220 288fd238 2021-04-25 op ssize_t linelen;
221 288fd238 2021-04-25 op int fd, e, res = 0;
222 288fd238 2021-04-25 op
223 288fd238 2021-04-25 op if (datalen != sizeof(entry))
224 288fd238 2021-04-25 op die();
225 288fd238 2021-04-25 op memcpy(&entry, imsg->data, datalen);
226 288fd238 2021-04-25 op
227 288fd238 2021-04-25 op strlcpy(sfn, known_hosts_tmp, sizeof(sfn));
228 288fd238 2021-04-25 op if ((fd = mkstemp(sfn)) == -1 ||
229 288fd238 2021-04-25 op (tmp = fdopen(fd, "w")) == NULL) {
230 288fd238 2021-04-25 op if (fd != -1) {
231 288fd238 2021-04-25 op unlink(sfn);
232 288fd238 2021-04-25 op close(fd);
233 288fd238 2021-04-25 op }
234 288fd238 2021-04-25 op res = 0;
235 288fd238 2021-04-25 op goto end;
236 288fd238 2021-04-25 op }
237 288fd238 2021-04-25 op
238 288fd238 2021-04-25 op if ((f = fopen(known_hosts_file, "r")) == NULL) {
239 288fd238 2021-04-25 op unlink(sfn);
240 288fd238 2021-04-25 op fclose(tmp);
241 288fd238 2021-04-25 op res = 0;
242 288fd238 2021-04-25 op goto end;
243 288fd238 2021-04-25 op }
244 288fd238 2021-04-25 op
245 288fd238 2021-04-25 op l = strlen(entry.domain);
246 288fd238 2021-04-25 op while ((linelen = getline(&line, &linesize, f)) != -1) {
247 288fd238 2021-04-25 op if ((t = strstr(line, entry.domain)) != NULL &&
248 288fd238 2021-04-25 op (line[l] == ' ' || line[l] == '\t'))
249 288fd238 2021-04-25 op continue;
250 288fd238 2021-04-25 op /* line has a trailing \n */
251 288fd238 2021-04-25 op fprintf(tmp, "%s", line);
252 288fd238 2021-04-25 op }
253 288fd238 2021-04-25 op fprintf(tmp, "%s %s %d\n", entry.domain, entry.hash, entry.verified);
254 288fd238 2021-04-25 op
255 288fd238 2021-04-25 op free(line);
256 288fd238 2021-04-25 op e = ferror(tmp);
257 288fd238 2021-04-25 op
258 288fd238 2021-04-25 op fclose(tmp);
259 288fd238 2021-04-25 op fclose(f);
260 288fd238 2021-04-25 op
261 288fd238 2021-04-25 op if (e) {
262 288fd238 2021-04-25 op unlink(sfn);
263 288fd238 2021-04-25 op res = 0;
264 288fd238 2021-04-25 op goto end;
265 288fd238 2021-04-25 op }
266 288fd238 2021-04-25 op
267 288fd238 2021-04-25 op res = rename(sfn, known_hosts_file) != -1;
268 288fd238 2021-04-25 op
269 288fd238 2021-04-25 op end:
270 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_UPDATE_CERT_OK, imsg->hdr.peerid, -1,
271 3a227e9a 2021-03-18 op &res, sizeof(res));
272 de2a69bb 2021-05-17 op }
273 de2a69bb 2021-05-17 op
274 de2a69bb 2021-05-17 op static void
275 de2a69bb 2021-05-17 op handle_file_open(struct imsg *imsg, size_t datalen)
276 de2a69bb 2021-05-17 op {
277 de2a69bb 2021-05-17 op char *path, *e;
278 de2a69bb 2021-05-17 op int fd;
279 de2a69bb 2021-05-17 op
280 de2a69bb 2021-05-17 op path = imsg->data;
281 de2a69bb 2021-05-17 op if (path[datalen-1] != '\0')
282 de2a69bb 2021-05-17 op die();
283 de2a69bb 2021-05-17 op
284 de2a69bb 2021-05-17 op if ((fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644)) == -1) {
285 de2a69bb 2021-05-17 op e = strerror(errno);
286 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, -1,
287 de2a69bb 2021-05-17 op e, strlen(e)+1);
288 de2a69bb 2021-05-17 op } else
289 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, fd,
290 de2a69bb 2021-05-17 op NULL, 0);
291 3a227e9a 2021-03-18 op }
292 3a227e9a 2021-03-18 op
293 3a227e9a 2021-03-18 op static void
294 c7107cec 2021-04-01 op handle_session_start(struct imsg *imsg, size_t datalen)
295 c7107cec 2021-04-01 op {
296 c7107cec 2021-04-01 op if (datalen != 0)
297 c7107cec 2021-04-01 op die();
298 c7107cec 2021-04-01 op
299 c7107cec 2021-04-01 op if ((session = fopen(session_file, "w")) == NULL)
300 c7107cec 2021-04-01 op die();
301 c7107cec 2021-04-01 op }
302 c7107cec 2021-04-01 op
303 c7107cec 2021-04-01 op static void
304 c7107cec 2021-04-01 op handle_session_tab(struct imsg *imsg, size_t datalen)
305 c7107cec 2021-04-01 op {
306 87e3e801 2021-07-17 op char *url;
307 87e3e801 2021-07-17 op uint32_t flags;
308 58df4f47 2021-07-17 op
309 c7107cec 2021-04-01 op if (session == NULL)
310 c7107cec 2021-04-01 op die();
311 c7107cec 2021-04-01 op
312 87e3e801 2021-07-17 op flags = imsg->hdr.peerid;
313 58df4f47 2021-07-17 op url = imsg->data;
314 58df4f47 2021-07-17 op if (datalen == 0 || url[datalen-1] != '\0')
315 c7107cec 2021-04-01 op die();
316 58df4f47 2021-07-17 op fprintf(session, "%s", url);
317 87e3e801 2021-07-17 op
318 87e3e801 2021-07-17 op if (flags & TAB_CURRENT)
319 2f524b17 2021-07-17 op fprintf(session, " current ");
320 2f524b17 2021-07-17 op else
321 2f524b17 2021-07-17 op fprintf(session, " - ");
322 2f524b17 2021-07-17 op }
323 87e3e801 2021-07-17 op
324 2f524b17 2021-07-17 op static void
325 2f524b17 2021-07-17 op handle_session_tab_title(struct imsg *imsg, size_t datalen)
326 2f524b17 2021-07-17 op {
327 2f524b17 2021-07-17 op const char *title;
328 2f524b17 2021-07-17 op
329 2f524b17 2021-07-17 op title = imsg->data;
330 2f524b17 2021-07-17 op if (title == NULL) {
331 2f524b17 2021-07-17 op datalen = 1;
332 2f524b17 2021-07-17 op title = "";
333 2f524b17 2021-07-17 op }
334 2f524b17 2021-07-17 op
335 2f524b17 2021-07-17 op if (title[datalen-1] != '\0')
336 2f524b17 2021-07-17 op die();
337 2f524b17 2021-07-17 op
338 2f524b17 2021-07-17 op fprintf(session, "%s\n", title);
339 c7107cec 2021-04-01 op }
340 c7107cec 2021-04-01 op
341 c7107cec 2021-04-01 op static void
342 c7107cec 2021-04-01 op handle_session_end(struct imsg *imsg, size_t datalen)
343 c7107cec 2021-04-01 op {
344 c7107cec 2021-04-01 op if (session == NULL)
345 c7107cec 2021-04-01 op die();
346 c7107cec 2021-04-01 op fclose(session);
347 c7107cec 2021-04-01 op session = NULL;
348 c7107cec 2021-04-01 op }
349 c7107cec 2021-04-01 op
350 c7107cec 2021-04-01 op static void
351 1304bbdd 2021-03-15 op handle_dispatch_imsg(int fd, short ev, void *d)
352 35e1f40a 2021-03-14 op {
353 bc10f6a5 2021-07-12 op struct imsgev *iev = d;
354 ea080950 2021-07-20 op int e;
355 ea080950 2021-07-20 op
356 ea080950 2021-07-20 op if (dispatch_imsg(iev, ev, handlers, sizeof(handlers)) == -1) {
357 ea080950 2021-07-20 op /*
358 ea080950 2021-07-20 op * This should leave a ~/.telescope/crashed file to
359 ea080950 2021-07-20 op * trigger about:crash on next run. Unfortunately, if
360 ea080950 2021-07-20 op * the main process dies the fs sticks around and
361 ea080950 2021-07-20 op * doesn't notice that the fd was closed. Why EV_READ
362 ea080950 2021-07-20 op * is not triggered when a fd is closed on the other end?
363 ea080950 2021-07-20 op */
364 ea080950 2021-07-20 op e = errno;
365 ea080950 2021-07-20 op if ((fd = open(crashed_file, O_CREAT|O_TRUNC|O_WRONLY, 0600))
366 ea080950 2021-07-20 op == -1)
367 ea080950 2021-07-20 op err(1, "open");
368 ea080950 2021-07-20 op close(fd);
369 ea080950 2021-07-20 op errx(1, "connection closed: %s", strerror(e));
370 ea080950 2021-07-20 op }
371 35e1f40a 2021-03-14 op }
372 35e1f40a 2021-03-14 op
373 bc10f6a5 2021-07-12 op static int
374 bc10f6a5 2021-07-12 op fs_send_ui(int type, uint32_t peerid, int fd, const void *data,
375 bc10f6a5 2021-07-12 op uint16_t datalen)
376 bc10f6a5 2021-07-12 op {
377 bc10f6a5 2021-07-12 op return imsg_compose_event(iev_ui, type, peerid, 0, fd,
378 bc10f6a5 2021-07-12 op data, datalen);
379 bc10f6a5 2021-07-12 op }
380 bc10f6a5 2021-07-12 op
381 35e1f40a 2021-03-14 op int
382 3a227e9a 2021-03-18 op fs_init(void)
383 35e1f40a 2021-03-14 op {
384 63a715ea 2021-03-18 op char dir[PATH_MAX];
385 63a715ea 2021-03-18 op
386 63a715ea 2021-03-18 op strlcpy(dir, getenv("HOME"), sizeof(dir));
387 63a715ea 2021-03-18 op strlcat(dir, "/.telescope", sizeof(dir));
388 63a715ea 2021-03-18 op mkdir(dir, 0700);
389 d0fd368a 2021-07-15 op
390 d0fd368a 2021-07-15 op strlcpy(lockfile_path, getenv("HOME"), sizeof(lockfile_path));
391 d0fd368a 2021-07-15 op strlcat(lockfile_path, "/.telescope/lock", sizeof(lockfile_path));
392 35e1f40a 2021-03-14 op
393 740f578b 2021-03-15 op strlcpy(bookmark_file, getenv("HOME"), sizeof(bookmark_file));
394 740f578b 2021-03-15 op strlcat(bookmark_file, "/.telescope/bookmarks.gmi", sizeof(bookmark_file));
395 740f578b 2021-03-15 op
396 3a227e9a 2021-03-18 op strlcpy(known_hosts_file, getenv("HOME"), sizeof(known_hosts_file));
397 3a227e9a 2021-03-18 op strlcat(known_hosts_file, "/.telescope/known_hosts", sizeof(known_hosts_file));
398 3a227e9a 2021-03-18 op
399 288fd238 2021-04-25 op strlcpy(known_hosts_tmp, getenv("HOME"), sizeof(known_hosts_tmp));
400 288fd238 2021-04-25 op strlcat(known_hosts_tmp, "/.telescope/known_hosts.tmp.XXXXXXXXXX",
401 288fd238 2021-04-25 op sizeof(known_hosts_file));
402 288fd238 2021-04-25 op
403 c7107cec 2021-04-01 op strlcpy(session_file, getenv("HOME"), sizeof(session_file));
404 c7107cec 2021-04-01 op strlcat(session_file, "/.telescope/session", sizeof(session_file));
405 c7107cec 2021-04-01 op
406 ea080950 2021-07-20 op strlcpy(crashed_file, getenv("HOME"), sizeof(crashed_file));
407 ea080950 2021-07-20 op strlcat(crashed_file, "/.telescope/crashed", sizeof(crashed_file));
408 ea080950 2021-07-20 op
409 3a227e9a 2021-03-18 op return 1;
410 3a227e9a 2021-03-18 op }
411 3a227e9a 2021-03-18 op
412 3a227e9a 2021-03-18 op int
413 6cc5fcfe 2021-07-08 op fs_main(void)
414 3a227e9a 2021-03-18 op {
415 6cc5fcfe 2021-07-08 op setproctitle("fs");
416 3a227e9a 2021-03-18 op
417 6cc5fcfe 2021-07-08 op fs_init();
418 6cc5fcfe 2021-07-08 op
419 35e1f40a 2021-03-14 op event_init();
420 35e1f40a 2021-03-14 op
421 bc10f6a5 2021-07-12 op /* Setup pipe and event handler to the main process */
422 bc10f6a5 2021-07-12 op if ((iev_ui = malloc(sizeof(*iev_ui))) == NULL)
423 6cc5fcfe 2021-07-08 op die();
424 bc10f6a5 2021-07-12 op imsg_init(&iev_ui->ibuf, 3);
425 bc10f6a5 2021-07-12 op iev_ui->handler = handle_dispatch_imsg;
426 bc10f6a5 2021-07-12 op iev_ui->events = EV_READ;
427 bc10f6a5 2021-07-12 op event_set(&iev_ui->ev, iev_ui->ibuf.fd, iev_ui->events,
428 bc10f6a5 2021-07-12 op iev_ui->handler, iev_ui);
429 bc10f6a5 2021-07-12 op event_add(&iev_ui->ev, NULL);
430 35e1f40a 2021-03-14 op
431 35e1f40a 2021-03-14 op sandbox_fs_process();
432 35e1f40a 2021-03-14 op
433 35e1f40a 2021-03-14 op event_dispatch();
434 35e1f40a 2021-03-14 op return 0;
435 35e1f40a 2021-03-14 op }
436 3a227e9a 2021-03-18 op
437 3a227e9a 2021-03-18 op
438 d0fd368a 2021-07-15 op
439 d0fd368a 2021-07-15 op int
440 b6171794 2021-07-20 op last_time_crashed(void)
441 b6171794 2021-07-20 op {
442 b6171794 2021-07-20 op int fd;
443 b6171794 2021-07-20 op
444 b6171794 2021-07-20 op if ((fd = open(crashed_file, O_RDONLY)) == -1)
445 b6171794 2021-07-20 op return 0;
446 b6171794 2021-07-20 op
447 b6171794 2021-07-20 op close(fd);
448 b6171794 2021-07-20 op unlink(crashed_file);
449 b6171794 2021-07-20 op return 1;
450 b6171794 2021-07-20 op }
451 b6171794 2021-07-20 op
452 b6171794 2021-07-20 op int
453 d0fd368a 2021-07-15 op lock_session(void)
454 d0fd368a 2021-07-15 op {
455 d0fd368a 2021-07-15 op struct flock lock;
456 d0fd368a 2021-07-15 op int fd;
457 d0fd368a 2021-07-15 op
458 d0fd368a 2021-07-15 op if ((fd = open(lockfile_path, O_WRONLY|O_CREAT, 0600)) == -1)
459 d0fd368a 2021-07-15 op return -1;
460 d0fd368a 2021-07-15 op
461 d0fd368a 2021-07-15 op lock.l_start = 0;
462 d0fd368a 2021-07-15 op lock.l_len = 0;
463 d0fd368a 2021-07-15 op lock.l_type = F_WRLCK;
464 d0fd368a 2021-07-15 op lock.l_whence = SEEK_SET;
465 c6d03cf5 2021-04-25 op
466 d0fd368a 2021-07-15 op if (fcntl(fd, F_SETLK, &lock) == -1) {
467 d0fd368a 2021-07-15 op close(fd);
468 d0fd368a 2021-07-15 op return -1;
469 d0fd368a 2021-07-15 op }
470 d0fd368a 2021-07-15 op
471 d0fd368a 2021-07-15 op return fd;
472 d0fd368a 2021-07-15 op }
473 d0fd368a 2021-07-15 op
474 c6d03cf5 2021-04-25 op static int
475 c6d03cf5 2021-04-25 op parse_khost_line(char *line, char *tmp[3])
476 c6d03cf5 2021-04-25 op {
477 c6d03cf5 2021-04-25 op char **ap;
478 c6d03cf5 2021-04-25 op
479 c6d03cf5 2021-04-25 op for (ap = tmp; ap < &tmp[3] &&
480 c6d03cf5 2021-04-25 op (*ap = strsep(&line, " \t\n")) != NULL;) {
481 c6d03cf5 2021-04-25 op if (**ap != '\0')
482 c6d03cf5 2021-04-25 op ap++;
483 c6d03cf5 2021-04-25 op }
484 3a227e9a 2021-03-18 op
485 c6d03cf5 2021-04-25 op return ap == &tmp[3] && *line == '\0';
486 c6d03cf5 2021-04-25 op }
487 c6d03cf5 2021-04-25 op
488 3a227e9a 2021-03-18 op int
489 3a227e9a 2021-03-18 op load_certs(struct ohash *h)
490 3a227e9a 2021-03-18 op {
491 c6d03cf5 2021-04-25 op char *tmp[3], *line = NULL;
492 6cd6a9e1 2021-03-20 op const char *errstr;
493 ec1fa0b0 2021-04-25 op size_t lineno = 0, linesize = 0;
494 3a227e9a 2021-03-18 op ssize_t linelen;
495 3a227e9a 2021-03-18 op FILE *f;
496 3a227e9a 2021-03-18 op struct tofu_entry *e;
497 3a227e9a 2021-03-18 op
498 3a227e9a 2021-03-18 op if ((f = fopen(known_hosts_file, "r")) == NULL)
499 3a227e9a 2021-03-18 op return 0;
500 3a227e9a 2021-03-18 op
501 3a227e9a 2021-03-18 op while ((linelen = getline(&line, &linesize, f)) != -1) {
502 3a227e9a 2021-03-18 op if ((e = calloc(1, sizeof(*e))) == NULL)
503 3a227e9a 2021-03-18 op abort();
504 3a227e9a 2021-03-18 op
505 ec1fa0b0 2021-04-25 op lineno++;
506 3a227e9a 2021-03-18 op
507 c6d03cf5 2021-04-25 op if (parse_khost_line(line, tmp)) {
508 c6d03cf5 2021-04-25 op strlcpy(e->domain, tmp[0], sizeof(e->domain));
509 c6d03cf5 2021-04-25 op strlcpy(e->hash, tmp[1], sizeof(e->hash));
510 3a227e9a 2021-03-18 op
511 c6d03cf5 2021-04-25 op e->verified = strtonum(tmp[2], 0, 1, &errstr);
512 c6d03cf5 2021-04-25 op if (errstr != NULL)
513 c6d03cf5 2021-04-25 op errx(1, "%s:%zu verification for %s is %s: %s",
514 c6d03cf5 2021-04-25 op known_hosts_file, lineno,
515 c6d03cf5 2021-04-25 op e->domain, errstr, tmp[2]);
516 c6d03cf5 2021-04-25 op tofu_add(h, e);
517 c6d03cf5 2021-04-25 op } else {
518 ec1fa0b0 2021-04-25 op warnx("%s:%zu invalid entry",
519 ec1fa0b0 2021-04-25 op known_hosts_file, lineno);
520 ec1fa0b0 2021-04-25 op free(e);
521 c6d03cf5 2021-04-25 op }
522 3a227e9a 2021-03-18 op }
523 3a227e9a 2021-03-18 op
524 3a227e9a 2021-03-18 op free(line);
525 3a227e9a 2021-03-18 op return ferror(f);
526 3a227e9a 2021-03-18 op }
527 c7107cec 2021-04-01 op