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 740f578b 2021-03-15 op */
20 786e6deb 2021-07-21 op
21 786e6deb 2021-07-21 op #include "compat.h"
22 35e1f40a 2021-03-14 op
23 6cd6a9e1 2021-03-20 op #include <sys/stat.h>
24 65124267 2021-08-13 op #include <sys/types.h>
25 6cd6a9e1 2021-03-20 op
26 65124267 2021-08-13 op #include <dirent.h>
27 35e1f40a 2021-03-14 op #include <errno.h>
28 de2a69bb 2021-05-17 op #include <fcntl.h>
29 35e1f40a 2021-03-14 op #include <limits.h>
30 35e1f40a 2021-03-14 op #include <stdio.h>
31 35e1f40a 2021-03-14 op #include <stdlib.h>
32 35e1f40a 2021-03-14 op #include <string.h>
33 35e1f40a 2021-03-14 op #include <unistd.h>
34 4913b479 2021-07-12 op
35 5a824be4 2021-07-13 op #include "pages.h"
36 328e7915 2021-09-19 op #include "telescope.h"
37 35e1f40a 2021-03-14 op
38 35e1f40a 2021-03-14 op static void die(void) __attribute__((__noreturn__));
39 fb4dc49f 2021-08-13 op static void send_file(uint32_t, FILE *);
40 35e1f40a 2021-03-14 op static void handle_get(struct imsg*, size_t);
41 5cbe1763 2021-08-13 op static int select_non_dot(const struct dirent *);
42 5cbe1763 2021-08-13 op static int select_non_dotdot(const struct dirent *);
43 fb4dc49f 2021-08-13 op static void handle_get_file(struct imsg*, size_t);
44 35e1f40a 2021-03-14 op static void handle_quit(struct imsg*, size_t);
45 740f578b 2021-03-15 op static void handle_bookmark_page(struct imsg*, size_t);
46 3a227e9a 2021-03-18 op static void handle_save_cert(struct imsg*, size_t);
47 288fd238 2021-04-25 op static void handle_update_cert(struct imsg*, size_t);
48 de2a69bb 2021-05-17 op static void handle_file_open(struct imsg*, size_t);
49 c7107cec 2021-04-01 op static void handle_session_start(struct imsg*, size_t);
50 c7107cec 2021-04-01 op static void handle_session_tab(struct imsg*, size_t);
51 2f524b17 2021-07-17 op static void handle_session_tab_title(struct imsg*, size_t);
52 c7107cec 2021-04-01 op static void handle_session_end(struct imsg*, size_t);
53 1304bbdd 2021-03-15 op static void handle_dispatch_imsg(int, short, void*);
54 bc10f6a5 2021-07-12 op static int fs_send_ui(int, uint32_t, int, const void *, uint16_t);
55 35e1f40a 2021-03-14 op
56 bc10f6a5 2021-07-12 op static struct imsgev *iev_ui;
57 c7107cec 2021-04-01 op static FILE *session;
58 c7107cec 2021-04-01 op
59 56e7efb4 2021-07-21 op static char base_path[PATH_MAX];
60 d0fd368a 2021-07-15 op static char lockfile_path[PATH_MAX];
61 740f578b 2021-03-15 op static char bookmark_file[PATH_MAX];
62 288fd238 2021-04-25 op static char known_hosts_file[PATH_MAX], known_hosts_tmp[PATH_MAX];
63 ea080950 2021-07-20 op static char crashed_file[PATH_MAX];
64 87e3e801 2021-07-17 op
65 87e3e801 2021-07-17 op char session_file[PATH_MAX];
66 740f578b 2021-03-15 op
67 35e1f40a 2021-03-14 op static imsg_handlerfn *handlers[] = {
68 35e1f40a 2021-03-14 op [IMSG_GET] = handle_get,
69 fb4dc49f 2021-08-13 op [IMSG_GET_FILE] = handle_get_file,
70 35e1f40a 2021-03-14 op [IMSG_QUIT] = handle_quit,
71 740f578b 2021-03-15 op [IMSG_BOOKMARK_PAGE] = handle_bookmark_page,
72 3a227e9a 2021-03-18 op [IMSG_SAVE_CERT] = handle_save_cert,
73 288fd238 2021-04-25 op [IMSG_UPDATE_CERT] = handle_update_cert,
74 de2a69bb 2021-05-17 op [IMSG_FILE_OPEN] = handle_file_open,
75 c7107cec 2021-04-01 op [IMSG_SESSION_START] = handle_session_start,
76 c7107cec 2021-04-01 op [IMSG_SESSION_TAB] = handle_session_tab,
77 2f524b17 2021-07-17 op [IMSG_SESSION_TAB_TITLE] = handle_session_tab_title,
78 c7107cec 2021-04-01 op [IMSG_SESSION_END] = handle_session_end,
79 35e1f40a 2021-03-14 op };
80 35e1f40a 2021-03-14 op
81 35e1f40a 2021-03-14 op static void __attribute__((__noreturn__))
82 35e1f40a 2021-03-14 op die(void)
83 35e1f40a 2021-03-14 op {
84 35e1f40a 2021-03-14 op abort(); /* TODO */
85 35e1f40a 2021-03-14 op }
86 35e1f40a 2021-03-14 op
87 35e1f40a 2021-03-14 op static void
88 fb4dc49f 2021-08-13 op send_file(uint32_t peerid, FILE *f)
89 fb4dc49f 2021-08-13 op {
90 fb4dc49f 2021-08-13 op ssize_t r;
91 fb4dc49f 2021-08-13 op char buf[BUFSIZ];
92 fb4dc49f 2021-08-13 op
93 fb4dc49f 2021-08-13 op for (;;) {
94 fb4dc49f 2021-08-13 op r = fread(buf, 1, sizeof(buf), f);
95 fb4dc49f 2021-08-13 op if (r != 0)
96 fb4dc49f 2021-08-13 op fs_send_ui(IMSG_BUF, peerid, -1, buf, r);
97 fb4dc49f 2021-08-13 op if (r != sizeof(buf))
98 fb4dc49f 2021-08-13 op break;
99 fb4dc49f 2021-08-13 op }
100 fb4dc49f 2021-08-13 op fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
101 fb4dc49f 2021-08-13 op fclose(f);
102 fb4dc49f 2021-08-13 op }
103 fb4dc49f 2021-08-13 op
104 fb4dc49f 2021-08-13 op static void
105 35e1f40a 2021-03-14 op handle_get(struct imsg *imsg, size_t datalen)
106 35e1f40a 2021-03-14 op {
107 56e7efb4 2021-07-21 op const char *bpath = "bookmarks.gmi";
108 fb4dc49f 2021-08-13 op char path[PATH_MAX];
109 56e7efb4 2021-07-21 op FILE *f;
110 a2728733 2021-07-18 op const char *data, *p;
111 a2728733 2021-07-18 op size_t i;
112 a2728733 2021-07-18 op struct page {
113 a2728733 2021-07-18 op const char *name;
114 56e7efb4 2021-07-21 op const char *path;
115 a2728733 2021-07-18 op const uint8_t *data;
116 a2728733 2021-07-18 op size_t len;
117 a2728733 2021-07-18 op } pages[] = {
118 56e7efb4 2021-07-21 op {"about", NULL, about_about, about_about_len},
119 56e7efb4 2021-07-21 op {"blank", NULL, about_blank, about_blank_len},
120 56e7efb4 2021-07-21 op {"bookmarks", bpath, bookmarks, bookmarks_len},
121 56e7efb4 2021-07-21 op {"crash", NULL, about_crash, about_crash_len},
122 56e7efb4 2021-07-21 op {"help", NULL, about_help, about_help_len},
123 56e7efb4 2021-07-21 op {"license", NULL, about_license, about_license_len},
124 56e7efb4 2021-07-21 op {"new", NULL, about_new, about_new_len},
125 56e7efb4 2021-07-21 op }, *page = NULL;
126 35e1f40a 2021-03-14 op
127 35e1f40a 2021-03-14 op data = imsg->data;
128 56e7efb4 2021-07-21 op if (data[datalen-1] != '\0') /* make sure it's NUL-terminated */
129 35e1f40a 2021-03-14 op die();
130 56e7efb4 2021-07-21 op if ((data = strchr(data, ':')) == NULL)
131 56e7efb4 2021-07-21 op goto notfound;
132 56e7efb4 2021-07-21 op data++;
133 35e1f40a 2021-03-14 op
134 56e7efb4 2021-07-21 op for (i = 0; i < sizeof(pages)/sizeof(pages[0]); ++i)
135 56e7efb4 2021-07-21 op if (!strcmp(data, pages[i].name)) {
136 56e7efb4 2021-07-21 op page = &pages[i];
137 56e7efb4 2021-07-21 op break;
138 56e7efb4 2021-07-21 op }
139 56e7efb4 2021-07-21 op
140 56e7efb4 2021-07-21 op if (page == NULL)
141 56e7efb4 2021-07-21 op goto notfound;
142 56e7efb4 2021-07-21 op
143 56e7efb4 2021-07-21 op strlcpy(path, base_path, sizeof(path));
144 56e7efb4 2021-07-21 op strlcat(path, "/", sizeof(path));
145 56e7efb4 2021-07-21 op if (page->path != NULL)
146 56e7efb4 2021-07-21 op strlcat(path, page->path, sizeof(path));
147 56e7efb4 2021-07-21 op else {
148 56e7efb4 2021-07-21 op strlcat(path, "pages/about_", sizeof(path));
149 56e7efb4 2021-07-21 op strlcat(path, page->name, sizeof(path));
150 56e7efb4 2021-07-21 op strlcat(path, ".gmi", sizeof(path));
151 56e7efb4 2021-07-21 op }
152 56e7efb4 2021-07-21 op
153 56e7efb4 2021-07-21 op if ((f = fopen(path, "r")) == NULL) {
154 56e7efb4 2021-07-21 op fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1,
155 56e7efb4 2021-07-21 op page->data, page->len);
156 56e7efb4 2021-07-21 op fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1,
157 56e7efb4 2021-07-21 op NULL, 0);
158 a2728733 2021-07-18 op return;
159 35e1f40a 2021-03-14 op }
160 a2728733 2021-07-18 op
161 fb4dc49f 2021-08-13 op send_file(imsg->hdr.peerid, f);
162 56e7efb4 2021-07-21 op return;
163 56e7efb4 2021-07-21 op
164 56e7efb4 2021-07-21 op notfound:
165 a2728733 2021-07-18 op p = "# not found!\n";
166 a2728733 2021-07-18 op fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, p, strlen(p));
167 a2728733 2021-07-18 op fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
168 fb4dc49f 2021-08-13 op }
169 fb4dc49f 2021-08-13 op
170 fb4dc49f 2021-08-13 op static inline void
171 fb4dc49f 2021-08-13 op send_hdr(uint32_t peerid, int code, const char *meta)
172 fb4dc49f 2021-08-13 op {
173 fb4dc49f 2021-08-13 op fs_send_ui(IMSG_GOT_CODE, peerid, -1, &code, sizeof(code));
174 fb4dc49f 2021-08-13 op fs_send_ui(IMSG_GOT_META, peerid, -1, meta, strlen(meta)+1);
175 fb4dc49f 2021-08-13 op }
176 fb4dc49f 2021-08-13 op
177 24a68158 2021-08-13 op static inline void
178 24a68158 2021-08-13 op send_errno(uint32_t peerid, int code, const char *str, int no)
179 24a68158 2021-08-13 op {
180 24a68158 2021-08-13 op char *s;
181 24a68158 2021-08-13 op
182 24a68158 2021-08-13 op if (asprintf(&s, "%s: %s", str, strerror(no)) == -1)
183 24a68158 2021-08-13 op s = NULL;
184 24a68158 2021-08-13 op
185 24a68158 2021-08-13 op send_hdr(peerid, code, s == NULL ? str : s);
186 24a68158 2021-08-13 op free(s);
187 24a68158 2021-08-13 op }
188 24a68158 2021-08-13 op
189 65124267 2021-08-13 op static inline const char *
190 65124267 2021-08-13 op file_type(const char *path)
191 fb4dc49f 2021-08-13 op {
192 fb4dc49f 2021-08-13 op struct mapping {
193 fb4dc49f 2021-08-13 op const char *ext;
194 fb4dc49f 2021-08-13 op const char *mime;
195 fb4dc49f 2021-08-13 op } ms[] = {
196 fb4dc49f 2021-08-13 op {"diff", "text/x-patch"},
197 fb4dc49f 2021-08-13 op {"gemini", "text/gemini"},
198 fb4dc49f 2021-08-13 op {"gmi", "text/gemini"},
199 fb4dc49f 2021-08-13 op {"markdown", "text/plain"},
200 fb4dc49f 2021-08-13 op {"md", "text/plain"},
201 fb4dc49f 2021-08-13 op {"patch", "text/x-patch"},
202 fb4dc49f 2021-08-13 op {"txt", "text/plain"},
203 fb4dc49f 2021-08-13 op {NULL, NULL},
204 65124267 2021-08-13 op }, *m;
205 65124267 2021-08-13 op char *dot;
206 65124267 2021-08-13 op
207 65124267 2021-08-13 op if ((dot = strrchr(path, '.')) == NULL)
208 65124267 2021-08-13 op return NULL;
209 65124267 2021-08-13 op
210 65124267 2021-08-13 op dot++;
211 65124267 2021-08-13 op
212 65124267 2021-08-13 op for (m = ms; m->ext != NULL; ++m)
213 65124267 2021-08-13 op if (!strcmp(m->ext, dot))
214 65124267 2021-08-13 op return m->mime;
215 65124267 2021-08-13 op
216 65124267 2021-08-13 op return NULL;
217 5cbe1763 2021-08-13 op }
218 5cbe1763 2021-08-13 op
219 5cbe1763 2021-08-13 op static int
220 5cbe1763 2021-08-13 op select_non_dot(const struct dirent *d)
221 5cbe1763 2021-08-13 op {
222 5cbe1763 2021-08-13 op return strcmp(d->d_name, ".");
223 5cbe1763 2021-08-13 op }
224 5cbe1763 2021-08-13 op
225 5cbe1763 2021-08-13 op static int
226 5cbe1763 2021-08-13 op select_non_dotdot(const struct dirent *d)
227 5cbe1763 2021-08-13 op {
228 5cbe1763 2021-08-13 op return strcmp(d->d_name, ".") && strcmp(d->d_name, "..");
229 65124267 2021-08-13 op }
230 65124267 2021-08-13 op
231 65124267 2021-08-13 op static inline void
232 65124267 2021-08-13 op send_dir(uint32_t peerid, const char *path)
233 65124267 2021-08-13 op {
234 65124267 2021-08-13 op struct dirent **names;
235 65124267 2021-08-13 op struct evbuffer *ev;
236 5d43215b 2021-08-13 op char *s;
237 5cbe1763 2021-08-13 op int (*selector)(const struct dirent *) = select_non_dot;
238 24a68158 2021-08-13 op int i, len, no;
239 65124267 2021-08-13 op
240 5d43215b 2021-08-13 op if (!has_suffix(path, "/")) {
241 5d43215b 2021-08-13 op if (asprintf(&s, "%s/", path) == -1)
242 5d43215b 2021-08-13 op die();
243 5d43215b 2021-08-13 op send_hdr(peerid, 30, s);
244 5d43215b 2021-08-13 op free(s);
245 5d43215b 2021-08-13 op return;
246 5d43215b 2021-08-13 op }
247 5d43215b 2021-08-13 op
248 5cbe1763 2021-08-13 op if (!strcmp(path, "/"))
249 5cbe1763 2021-08-13 op selector = select_non_dotdot;
250 5cbe1763 2021-08-13 op
251 65124267 2021-08-13 op if ((ev = evbuffer_new()) == NULL ||
252 5cbe1763 2021-08-13 op (len = scandir(path, &names, selector, alphasort)) == -1) {
253 24a68158 2021-08-13 op no = errno;
254 65124267 2021-08-13 op evbuffer_free(ev);
255 24a68158 2021-08-13 op send_errno(peerid, 40, "failure reading the directory", no);
256 65124267 2021-08-13 op return;
257 65124267 2021-08-13 op }
258 65124267 2021-08-13 op
259 65124267 2021-08-13 op evbuffer_add_printf(ev, "# Index of %s\n\n", path);
260 65124267 2021-08-13 op for (i = 0; i < len; ++i) {
261 65124267 2021-08-13 op evbuffer_add_printf(ev, "=> %s", names[i]->d_name);
262 65124267 2021-08-13 op if (names[i]->d_type == DT_DIR)
263 65124267 2021-08-13 op evbuffer_add(ev, "/", 1);
264 65124267 2021-08-13 op evbuffer_add(ev, "\n", 1);
265 65124267 2021-08-13 op }
266 65124267 2021-08-13 op
267 65124267 2021-08-13 op send_hdr(peerid, 20, "text/gemini");
268 65124267 2021-08-13 op fs_send_ui(IMSG_BUF, peerid, -1,
269 65124267 2021-08-13 op EVBUFFER_DATA(ev), EVBUFFER_LENGTH(ev));
270 65124267 2021-08-13 op fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
271 65124267 2021-08-13 op
272 65124267 2021-08-13 op evbuffer_free(ev);
273 65124267 2021-08-13 op free(names);
274 65124267 2021-08-13 op }
275 65124267 2021-08-13 op
276 65124267 2021-08-13 op static void
277 65124267 2021-08-13 op handle_get_file(struct imsg *imsg, size_t datalen)
278 65124267 2021-08-13 op {
279 65124267 2021-08-13 op struct stat sb;
280 fb4dc49f 2021-08-13 op FILE *f;
281 65124267 2021-08-13 op char *data;
282 fb4dc49f 2021-08-13 op const char *meta = NULL;
283 fb4dc49f 2021-08-13 op
284 fb4dc49f 2021-08-13 op data = imsg->data;
285 fb4dc49f 2021-08-13 op data[datalen-1] = '\0';
286 fb4dc49f 2021-08-13 op
287 65124267 2021-08-13 op if ((f = fopen(data, "r")) == NULL) {
288 24a68158 2021-08-13 op send_errno(imsg->hdr.peerid, 51, "can't open", errno);
289 fb4dc49f 2021-08-13 op return;
290 fb4dc49f 2021-08-13 op }
291 fb4dc49f 2021-08-13 op
292 65124267 2021-08-13 op if (fstat(fileno(f), &sb) == -1) {
293 24a68158 2021-08-13 op send_errno(imsg->hdr.peerid, 40, "fstat", errno);
294 65124267 2021-08-13 op return;
295 fb4dc49f 2021-08-13 op }
296 fb4dc49f 2021-08-13 op
297 65124267 2021-08-13 op if (S_ISDIR(sb.st_mode)) {
298 65124267 2021-08-13 op fclose(f);
299 65124267 2021-08-13 op send_dir(imsg->hdr.peerid, data);
300 fb4dc49f 2021-08-13 op return;
301 fb4dc49f 2021-08-13 op }
302 fb4dc49f 2021-08-13 op
303 65124267 2021-08-13 op if ((meta = file_type(data)) == NULL) {
304 65124267 2021-08-13 op fclose(f);
305 65124267 2021-08-13 op send_hdr(imsg->hdr.peerid, 51,
306 65124267 2021-08-13 op "don't know how to visualize this file");
307 fb4dc49f 2021-08-13 op return;
308 fb4dc49f 2021-08-13 op }
309 fb4dc49f 2021-08-13 op
310 fb4dc49f 2021-08-13 op send_hdr(imsg->hdr.peerid, 20, meta);
311 fb4dc49f 2021-08-13 op send_file(imsg->hdr.peerid, f);
312 35e1f40a 2021-03-14 op }
313 35e1f40a 2021-03-14 op
314 35e1f40a 2021-03-14 op static void
315 35e1f40a 2021-03-14 op handle_quit(struct imsg *imsg, size_t datalen)
316 35e1f40a 2021-03-14 op {
317 2b409042 2021-09-15 op if (!safe_mode)
318 2b409042 2021-09-15 op unlink(crashed_file);
319 be97d6e6 2021-08-15 op
320 35e1f40a 2021-03-14 op event_loopbreak();
321 35e1f40a 2021-03-14 op }
322 35e1f40a 2021-03-14 op
323 35e1f40a 2021-03-14 op static void
324 740f578b 2021-03-15 op handle_bookmark_page(struct imsg *imsg, size_t datalen)
325 740f578b 2021-03-15 op {
326 740f578b 2021-03-15 op char *data;
327 740f578b 2021-03-15 op int res;
328 740f578b 2021-03-15 op FILE *f;
329 740f578b 2021-03-15 op
330 740f578b 2021-03-15 op data = imsg->data;
331 740f578b 2021-03-15 op if (data[datalen-1] != '\0')
332 740f578b 2021-03-15 op die();
333 740f578b 2021-03-15 op
334 740f578b 2021-03-15 op if ((f = fopen(bookmark_file, "a")) == NULL) {
335 740f578b 2021-03-15 op res = errno;
336 740f578b 2021-03-15 op goto end;
337 740f578b 2021-03-15 op }
338 740f578b 2021-03-15 op fprintf(f, "=> %s\n", data);
339 740f578b 2021-03-15 op fclose(f);
340 740f578b 2021-03-15 op
341 740f578b 2021-03-15 op res = 0;
342 740f578b 2021-03-15 op end:
343 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_BOOKMARK_OK, 0, -1, &res, sizeof(res));
344 740f578b 2021-03-15 op }
345 740f578b 2021-03-15 op
346 740f578b 2021-03-15 op static void
347 3a227e9a 2021-03-18 op handle_save_cert(struct imsg *imsg, size_t datalen)
348 3a227e9a 2021-03-18 op {
349 3a227e9a 2021-03-18 op struct tofu_entry e;
350 3a227e9a 2021-03-18 op FILE *f;
351 3a227e9a 2021-03-18 op int res;
352 3a227e9a 2021-03-18 op
353 3a227e9a 2021-03-18 op /* TODO: traverse the file to avoid duplications? */
354 3a227e9a 2021-03-18 op
355 3a227e9a 2021-03-18 op if (datalen != sizeof(e))
356 3a227e9a 2021-03-18 op die();
357 3a227e9a 2021-03-18 op memcpy(&e, imsg->data, datalen);
358 3a227e9a 2021-03-18 op
359 3a227e9a 2021-03-18 op if ((f = fopen(known_hosts_file, "a")) == NULL) {
360 3a227e9a 2021-03-18 op res = errno;
361 3a227e9a 2021-03-18 op goto end;
362 3a227e9a 2021-03-18 op }
363 3a227e9a 2021-03-18 op fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
364 3a227e9a 2021-03-18 op fclose(f);
365 3a227e9a 2021-03-18 op
366 3a227e9a 2021-03-18 op res = 0;
367 3a227e9a 2021-03-18 op end:
368 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_SAVE_CERT_OK, imsg->hdr.peerid, -1,
369 288fd238 2021-04-25 op &res, sizeof(res));
370 288fd238 2021-04-25 op }
371 288fd238 2021-04-25 op
372 288fd238 2021-04-25 op static void
373 288fd238 2021-04-25 op handle_update_cert(struct imsg *imsg, size_t datalen)
374 288fd238 2021-04-25 op {
375 288fd238 2021-04-25 op FILE *tmp, *f;
376 288fd238 2021-04-25 op struct tofu_entry entry;
377 288fd238 2021-04-25 op char sfn[PATH_MAX], *line = NULL, *t;
378 288fd238 2021-04-25 op size_t l, linesize = 0;
379 288fd238 2021-04-25 op ssize_t linelen;
380 288fd238 2021-04-25 op int fd, e, res = 0;
381 288fd238 2021-04-25 op
382 288fd238 2021-04-25 op if (datalen != sizeof(entry))
383 288fd238 2021-04-25 op die();
384 288fd238 2021-04-25 op memcpy(&entry, imsg->data, datalen);
385 288fd238 2021-04-25 op
386 288fd238 2021-04-25 op strlcpy(sfn, known_hosts_tmp, sizeof(sfn));
387 288fd238 2021-04-25 op if ((fd = mkstemp(sfn)) == -1 ||
388 288fd238 2021-04-25 op (tmp = fdopen(fd, "w")) == NULL) {
389 288fd238 2021-04-25 op if (fd != -1) {
390 288fd238 2021-04-25 op unlink(sfn);
391 288fd238 2021-04-25 op close(fd);
392 288fd238 2021-04-25 op }
393 288fd238 2021-04-25 op res = 0;
394 288fd238 2021-04-25 op goto end;
395 288fd238 2021-04-25 op }
396 288fd238 2021-04-25 op
397 288fd238 2021-04-25 op if ((f = fopen(known_hosts_file, "r")) == NULL) {
398 288fd238 2021-04-25 op unlink(sfn);
399 288fd238 2021-04-25 op fclose(tmp);
400 288fd238 2021-04-25 op res = 0;
401 288fd238 2021-04-25 op goto end;
402 288fd238 2021-04-25 op }
403 288fd238 2021-04-25 op
404 288fd238 2021-04-25 op l = strlen(entry.domain);
405 288fd238 2021-04-25 op while ((linelen = getline(&line, &linesize, f)) != -1) {
406 288fd238 2021-04-25 op if ((t = strstr(line, entry.domain)) != NULL &&
407 288fd238 2021-04-25 op (line[l] == ' ' || line[l] == '\t'))
408 288fd238 2021-04-25 op continue;
409 288fd238 2021-04-25 op /* line has a trailing \n */
410 288fd238 2021-04-25 op fprintf(tmp, "%s", line);
411 288fd238 2021-04-25 op }
412 288fd238 2021-04-25 op fprintf(tmp, "%s %s %d\n", entry.domain, entry.hash, entry.verified);
413 288fd238 2021-04-25 op
414 288fd238 2021-04-25 op free(line);
415 288fd238 2021-04-25 op e = ferror(tmp);
416 288fd238 2021-04-25 op
417 288fd238 2021-04-25 op fclose(tmp);
418 288fd238 2021-04-25 op fclose(f);
419 288fd238 2021-04-25 op
420 288fd238 2021-04-25 op if (e) {
421 288fd238 2021-04-25 op unlink(sfn);
422 288fd238 2021-04-25 op res = 0;
423 288fd238 2021-04-25 op goto end;
424 288fd238 2021-04-25 op }
425 288fd238 2021-04-25 op
426 288fd238 2021-04-25 op res = rename(sfn, known_hosts_file) != -1;
427 288fd238 2021-04-25 op
428 288fd238 2021-04-25 op end:
429 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_UPDATE_CERT_OK, imsg->hdr.peerid, -1,
430 3a227e9a 2021-03-18 op &res, sizeof(res));
431 de2a69bb 2021-05-17 op }
432 de2a69bb 2021-05-17 op
433 de2a69bb 2021-05-17 op static void
434 de2a69bb 2021-05-17 op handle_file_open(struct imsg *imsg, size_t datalen)
435 de2a69bb 2021-05-17 op {
436 de2a69bb 2021-05-17 op char *path, *e;
437 de2a69bb 2021-05-17 op int fd;
438 de2a69bb 2021-05-17 op
439 de2a69bb 2021-05-17 op path = imsg->data;
440 de2a69bb 2021-05-17 op if (path[datalen-1] != '\0')
441 de2a69bb 2021-05-17 op die();
442 de2a69bb 2021-05-17 op
443 de2a69bb 2021-05-17 op if ((fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644)) == -1) {
444 de2a69bb 2021-05-17 op e = strerror(errno);
445 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, -1,
446 de2a69bb 2021-05-17 op e, strlen(e)+1);
447 de2a69bb 2021-05-17 op } else
448 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, fd,
449 de2a69bb 2021-05-17 op NULL, 0);
450 3a227e9a 2021-03-18 op }
451 3a227e9a 2021-03-18 op
452 3a227e9a 2021-03-18 op static void
453 c7107cec 2021-04-01 op handle_session_start(struct imsg *imsg, size_t datalen)
454 c7107cec 2021-04-01 op {
455 c7107cec 2021-04-01 op if (datalen != 0)
456 c7107cec 2021-04-01 op die();
457 c7107cec 2021-04-01 op
458 c7107cec 2021-04-01 op if ((session = fopen(session_file, "w")) == NULL)
459 c7107cec 2021-04-01 op die();
460 c7107cec 2021-04-01 op }
461 c7107cec 2021-04-01 op
462 c7107cec 2021-04-01 op static void
463 c7107cec 2021-04-01 op handle_session_tab(struct imsg *imsg, size_t datalen)
464 c7107cec 2021-04-01 op {
465 87e3e801 2021-07-17 op char *url;
466 87e3e801 2021-07-17 op uint32_t flags;
467 58df4f47 2021-07-17 op
468 c7107cec 2021-04-01 op if (session == NULL)
469 c7107cec 2021-04-01 op die();
470 c7107cec 2021-04-01 op
471 87e3e801 2021-07-17 op flags = imsg->hdr.peerid;
472 58df4f47 2021-07-17 op url = imsg->data;
473 58df4f47 2021-07-17 op if (datalen == 0 || url[datalen-1] != '\0')
474 c7107cec 2021-04-01 op die();
475 58df4f47 2021-07-17 op fprintf(session, "%s", url);
476 87e3e801 2021-07-17 op
477 87e3e801 2021-07-17 op if (flags & TAB_CURRENT)
478 2f524b17 2021-07-17 op fprintf(session, " current ");
479 2f524b17 2021-07-17 op else
480 2f524b17 2021-07-17 op fprintf(session, " - ");
481 2f524b17 2021-07-17 op }
482 87e3e801 2021-07-17 op
483 2f524b17 2021-07-17 op static void
484 2f524b17 2021-07-17 op handle_session_tab_title(struct imsg *imsg, size_t datalen)
485 2f524b17 2021-07-17 op {
486 2f524b17 2021-07-17 op const char *title;
487 2f524b17 2021-07-17 op
488 2f524b17 2021-07-17 op title = imsg->data;
489 2f524b17 2021-07-17 op if (title == NULL) {
490 2f524b17 2021-07-17 op datalen = 1;
491 2f524b17 2021-07-17 op title = "";
492 2f524b17 2021-07-17 op }
493 2f524b17 2021-07-17 op
494 2f524b17 2021-07-17 op if (title[datalen-1] != '\0')
495 2f524b17 2021-07-17 op die();
496 2f524b17 2021-07-17 op
497 2f524b17 2021-07-17 op fprintf(session, "%s\n", title);
498 c7107cec 2021-04-01 op }
499 c7107cec 2021-04-01 op
500 c7107cec 2021-04-01 op static void
501 c7107cec 2021-04-01 op handle_session_end(struct imsg *imsg, size_t datalen)
502 c7107cec 2021-04-01 op {
503 c7107cec 2021-04-01 op if (session == NULL)
504 c7107cec 2021-04-01 op die();
505 c7107cec 2021-04-01 op fclose(session);
506 c7107cec 2021-04-01 op session = NULL;
507 c7107cec 2021-04-01 op }
508 c7107cec 2021-04-01 op
509 c7107cec 2021-04-01 op static void
510 1304bbdd 2021-03-15 op handle_dispatch_imsg(int fd, short ev, void *d)
511 35e1f40a 2021-03-14 op {
512 bc10f6a5 2021-07-12 op struct imsgev *iev = d;
513 ea080950 2021-07-20 op int e;
514 ea080950 2021-07-20 op
515 ea080950 2021-07-20 op if (dispatch_imsg(iev, ev, handlers, sizeof(handlers)) == -1) {
516 ea080950 2021-07-20 op /*
517 ea080950 2021-07-20 op * This should leave a ~/.telescope/crashed file to
518 ea080950 2021-07-20 op * trigger about:crash on next run. Unfortunately, if
519 ea080950 2021-07-20 op * the main process dies the fs sticks around and
520 ea080950 2021-07-20 op * doesn't notice that the fd was closed. Why EV_READ
521 ea080950 2021-07-20 op * is not triggered when a fd is closed on the other end?
522 ea080950 2021-07-20 op */
523 ea080950 2021-07-20 op e = errno;
524 ea080950 2021-07-20 op if ((fd = open(crashed_file, O_CREAT|O_TRUNC|O_WRONLY, 0600))
525 ea080950 2021-07-20 op == -1)
526 ea080950 2021-07-20 op err(1, "open");
527 ea080950 2021-07-20 op close(fd);
528 ea080950 2021-07-20 op errx(1, "connection closed: %s", strerror(e));
529 ea080950 2021-07-20 op }
530 35e1f40a 2021-03-14 op }
531 35e1f40a 2021-03-14 op
532 bc10f6a5 2021-07-12 op static int
533 bc10f6a5 2021-07-12 op fs_send_ui(int type, uint32_t peerid, int fd, const void *data,
534 bc10f6a5 2021-07-12 op uint16_t datalen)
535 bc10f6a5 2021-07-12 op {
536 bc10f6a5 2021-07-12 op return imsg_compose_event(iev_ui, type, peerid, 0, fd,
537 bc10f6a5 2021-07-12 op data, datalen);
538 bc10f6a5 2021-07-12 op }
539 bc10f6a5 2021-07-12 op
540 35e1f40a 2021-03-14 op int
541 3a227e9a 2021-03-18 op fs_init(void)
542 35e1f40a 2021-03-14 op {
543 56e7efb4 2021-07-21 op strlcpy(base_path, getenv("HOME"), sizeof(base_path));
544 56e7efb4 2021-07-21 op strlcat(base_path, "/.telescope", sizeof(base_path));
545 56e7efb4 2021-07-21 op mkdir(base_path, 0700);
546 d0fd368a 2021-07-15 op
547 56e7efb4 2021-07-21 op strlcpy(lockfile_path, base_path, sizeof(lockfile_path));
548 56e7efb4 2021-07-21 op strlcat(lockfile_path, "/lock", sizeof(lockfile_path));
549 35e1f40a 2021-03-14 op
550 56e7efb4 2021-07-21 op strlcpy(bookmark_file, base_path, sizeof(bookmark_file));
551 56e7efb4 2021-07-21 op strlcat(bookmark_file, "/bookmarks.gmi", sizeof(bookmark_file));
552 740f578b 2021-03-15 op
553 56e7efb4 2021-07-21 op strlcpy(known_hosts_file, base_path, sizeof(known_hosts_file));
554 56e7efb4 2021-07-21 op strlcat(known_hosts_file, "/known_hosts", sizeof(known_hosts_file));
555 3a227e9a 2021-03-18 op
556 56e7efb4 2021-07-21 op strlcpy(known_hosts_tmp, base_path, sizeof(known_hosts_tmp));
557 56e7efb4 2021-07-21 op strlcat(known_hosts_tmp, "/known_hosts.tmp.XXXXXXXXXX",
558 288fd238 2021-04-25 op sizeof(known_hosts_file));
559 288fd238 2021-04-25 op
560 56e7efb4 2021-07-21 op strlcpy(session_file, base_path, sizeof(session_file));
561 56e7efb4 2021-07-21 op strlcat(session_file, "/session", sizeof(session_file));
562 c7107cec 2021-04-01 op
563 56e7efb4 2021-07-21 op strlcpy(crashed_file, base_path, sizeof(crashed_file));
564 56e7efb4 2021-07-21 op strlcat(crashed_file, "/crashed", sizeof(crashed_file));
565 ea080950 2021-07-20 op
566 3a227e9a 2021-03-18 op return 1;
567 3a227e9a 2021-03-18 op }
568 3a227e9a 2021-03-18 op
569 3a227e9a 2021-03-18 op int
570 6cc5fcfe 2021-07-08 op fs_main(void)
571 3a227e9a 2021-03-18 op {
572 6cc5fcfe 2021-07-08 op setproctitle("fs");
573 3a227e9a 2021-03-18 op
574 6cc5fcfe 2021-07-08 op fs_init();
575 6cc5fcfe 2021-07-08 op
576 35e1f40a 2021-03-14 op event_init();
577 35e1f40a 2021-03-14 op
578 bc10f6a5 2021-07-12 op /* Setup pipe and event handler to the main process */
579 bc10f6a5 2021-07-12 op if ((iev_ui = malloc(sizeof(*iev_ui))) == NULL)
580 6cc5fcfe 2021-07-08 op die();
581 bc10f6a5 2021-07-12 op imsg_init(&iev_ui->ibuf, 3);
582 bc10f6a5 2021-07-12 op iev_ui->handler = handle_dispatch_imsg;
583 bc10f6a5 2021-07-12 op iev_ui->events = EV_READ;
584 bc10f6a5 2021-07-12 op event_set(&iev_ui->ev, iev_ui->ibuf.fd, iev_ui->events,
585 bc10f6a5 2021-07-12 op iev_ui->handler, iev_ui);
586 bc10f6a5 2021-07-12 op event_add(&iev_ui->ev, NULL);
587 35e1f40a 2021-03-14 op
588 35e1f40a 2021-03-14 op sandbox_fs_process();
589 35e1f40a 2021-03-14 op
590 35e1f40a 2021-03-14 op event_dispatch();
591 35e1f40a 2021-03-14 op return 0;
592 35e1f40a 2021-03-14 op }
593 3a227e9a 2021-03-18 op
594 3a227e9a 2021-03-18 op
595 d0fd368a 2021-07-15 op
596 be97d6e6 2021-08-15 op /*
597 be97d6e6 2021-08-15 op * Check if the last time telescope crashed. The check is done by
598 be97d6e6 2021-08-15 op * looking at `crashed_file': if it exists then last time we crashed.
599 be97d6e6 2021-08-15 op * Then, while here, touch the file too. During IMSG_QUIT we'll
600 be97d6e6 2021-08-15 op * remove it.
601 be97d6e6 2021-08-15 op */
602 d0fd368a 2021-07-15 op int
603 b6171794 2021-07-20 op last_time_crashed(void)
604 b6171794 2021-07-20 op {
605 be97d6e6 2021-08-15 op int fd, crashed = 1;
606 b6171794 2021-07-20 op
607 2b409042 2021-09-15 op if (safe_mode)
608 2b409042 2021-09-15 op return 0;
609 2b409042 2021-09-15 op
610 be97d6e6 2021-08-15 op if (unlink(crashed_file) == -1 && errno == ENOENT)
611 be97d6e6 2021-08-15 op crashed = 0;
612 be97d6e6 2021-08-15 op
613 be97d6e6 2021-08-15 op if ((fd = open(crashed_file, O_CREAT|O_WRONLY, 0600)) == -1)
614 be97d6e6 2021-08-15 op return crashed;
615 b6171794 2021-07-20 op close(fd);
616 be97d6e6 2021-08-15 op
617 be97d6e6 2021-08-15 op return crashed;
618 b6171794 2021-07-20 op }
619 b6171794 2021-07-20 op
620 b6171794 2021-07-20 op int
621 d0fd368a 2021-07-15 op lock_session(void)
622 d0fd368a 2021-07-15 op {
623 d0fd368a 2021-07-15 op struct flock lock;
624 d0fd368a 2021-07-15 op int fd;
625 d0fd368a 2021-07-15 op
626 d0fd368a 2021-07-15 op if ((fd = open(lockfile_path, O_WRONLY|O_CREAT, 0600)) == -1)
627 d0fd368a 2021-07-15 op return -1;
628 d0fd368a 2021-07-15 op
629 d0fd368a 2021-07-15 op lock.l_start = 0;
630 d0fd368a 2021-07-15 op lock.l_len = 0;
631 d0fd368a 2021-07-15 op lock.l_type = F_WRLCK;
632 d0fd368a 2021-07-15 op lock.l_whence = SEEK_SET;
633 c6d03cf5 2021-04-25 op
634 d0fd368a 2021-07-15 op if (fcntl(fd, F_SETLK, &lock) == -1) {
635 d0fd368a 2021-07-15 op close(fd);
636 d0fd368a 2021-07-15 op return -1;
637 d0fd368a 2021-07-15 op }
638 d0fd368a 2021-07-15 op
639 d0fd368a 2021-07-15 op return fd;
640 d0fd368a 2021-07-15 op }
641 d0fd368a 2021-07-15 op
642 c6d03cf5 2021-04-25 op static int
643 c6d03cf5 2021-04-25 op parse_khost_line(char *line, char *tmp[3])
644 c6d03cf5 2021-04-25 op {
645 c6d03cf5 2021-04-25 op char **ap;
646 c6d03cf5 2021-04-25 op
647 c6d03cf5 2021-04-25 op for (ap = tmp; ap < &tmp[3] &&
648 c6d03cf5 2021-04-25 op (*ap = strsep(&line, " \t\n")) != NULL;) {
649 c6d03cf5 2021-04-25 op if (**ap != '\0')
650 c6d03cf5 2021-04-25 op ap++;
651 c6d03cf5 2021-04-25 op }
652 3a227e9a 2021-03-18 op
653 c6d03cf5 2021-04-25 op return ap == &tmp[3] && *line == '\0';
654 c6d03cf5 2021-04-25 op }
655 c6d03cf5 2021-04-25 op
656 3a227e9a 2021-03-18 op int
657 3a227e9a 2021-03-18 op load_certs(struct ohash *h)
658 3a227e9a 2021-03-18 op {
659 c6d03cf5 2021-04-25 op char *tmp[3], *line = NULL;
660 6cd6a9e1 2021-03-20 op const char *errstr;
661 ec1fa0b0 2021-04-25 op size_t lineno = 0, linesize = 0;
662 3a227e9a 2021-03-18 op ssize_t linelen;
663 3a227e9a 2021-03-18 op FILE *f;
664 3a227e9a 2021-03-18 op struct tofu_entry *e;
665 3a227e9a 2021-03-18 op
666 3a227e9a 2021-03-18 op if ((f = fopen(known_hosts_file, "r")) == NULL)
667 3a227e9a 2021-03-18 op return 0;
668 3a227e9a 2021-03-18 op
669 3a227e9a 2021-03-18 op while ((linelen = getline(&line, &linesize, f)) != -1) {
670 3a227e9a 2021-03-18 op if ((e = calloc(1, sizeof(*e))) == NULL)
671 3a227e9a 2021-03-18 op abort();
672 3a227e9a 2021-03-18 op
673 ec1fa0b0 2021-04-25 op lineno++;
674 3a227e9a 2021-03-18 op
675 c6d03cf5 2021-04-25 op if (parse_khost_line(line, tmp)) {
676 c6d03cf5 2021-04-25 op strlcpy(e->domain, tmp[0], sizeof(e->domain));
677 c6d03cf5 2021-04-25 op strlcpy(e->hash, tmp[1], sizeof(e->hash));
678 3a227e9a 2021-03-18 op
679 c6d03cf5 2021-04-25 op e->verified = strtonum(tmp[2], 0, 1, &errstr);
680 c6d03cf5 2021-04-25 op if (errstr != NULL)
681 c6d03cf5 2021-04-25 op errx(1, "%s:%zu verification for %s is %s: %s",
682 c6d03cf5 2021-04-25 op known_hosts_file, lineno,
683 c6d03cf5 2021-04-25 op e->domain, errstr, tmp[2]);
684 c6d03cf5 2021-04-25 op tofu_add(h, e);
685 c6d03cf5 2021-04-25 op } else {
686 ec1fa0b0 2021-04-25 op warnx("%s:%zu invalid entry",
687 ec1fa0b0 2021-04-25 op known_hosts_file, lineno);
688 ec1fa0b0 2021-04-25 op free(e);
689 c6d03cf5 2021-04-25 op }
690 3a227e9a 2021-03-18 op }
691 3a227e9a 2021-03-18 op
692 3a227e9a 2021-03-18 op free(line);
693 3a227e9a 2021-03-18 op return ferror(f);
694 3a227e9a 2021-03-18 op }
695 c7107cec 2021-04-01 op