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 eb2ed626 2021-10-07 op * Handles config and runtime files
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 eb2ed626 2021-10-07 op #include <libgen.h>
31 35e1f40a 2021-03-14 op #include <stdio.h>
32 35e1f40a 2021-03-14 op #include <stdlib.h>
33 35e1f40a 2021-03-14 op #include <string.h>
34 35e1f40a 2021-03-14 op #include <unistd.h>
35 4913b479 2021-07-12 op
36 eb2ed626 2021-10-07 op #include "fs.h"
37 5a824be4 2021-07-13 op #include "pages.h"
38 328e7915 2021-09-19 op #include "telescope.h"
39 eb9cbcba 2022-01-03 op #include "session.h"
40 35e1f40a 2021-03-14 op
41 35e1f40a 2021-03-14 op static void die(void) __attribute__((__noreturn__));
42 fb4dc49f 2021-08-13 op static void send_file(uint32_t, FILE *);
43 35e1f40a 2021-03-14 op static void handle_get(struct imsg*, size_t);
44 5cbe1763 2021-08-13 op static int select_non_dot(const struct dirent *);
45 5cbe1763 2021-08-13 op static int select_non_dotdot(const struct dirent *);
46 fb4dc49f 2021-08-13 op static void handle_get_file(struct imsg*, size_t);
47 50f03682 2022-01-03 op static void handle_misc(struct imsg *, size_t);
48 740f578b 2021-03-15 op static void handle_bookmark_page(struct imsg*, size_t);
49 3a227e9a 2021-03-18 op static void handle_save_cert(struct imsg*, size_t);
50 288fd238 2021-04-25 op static void handle_update_cert(struct imsg*, size_t);
51 de2a69bb 2021-05-17 op static void handle_file_open(struct imsg*, size_t);
52 c7107cec 2021-04-01 op static void handle_session_start(struct imsg*, size_t);
53 c7107cec 2021-04-01 op static void handle_session_tab(struct imsg*, size_t);
54 1040cc7f 2021-01-02 op static void handle_session_tab_hist(struct imsg*, size_t);
55 c7107cec 2021-04-01 op static void handle_session_end(struct imsg*, size_t);
56 1304bbdd 2021-03-15 op static void handle_dispatch_imsg(int, short, void*);
57 bc10f6a5 2021-07-12 op static int fs_send_ui(int, uint32_t, int, const void *, uint16_t);
58 eb2ed626 2021-10-07 op static size_t join_path(char*, const char*, const char*, size_t);
59 eb2ed626 2021-10-07 op static void getenv_default(char*, const char*, const char*, size_t);
60 eb2ed626 2021-10-07 op static void mkdirs(const char*, mode_t);
61 6c8ddace 2022-01-03 op static void init_paths(void);
62 50f03682 2022-01-03 op static void load_last_session(void);
63 83ed72f1 2022-01-03 op static int last_time_crashed(void);
64 eb722b50 2022-01-03 op static void load_certs(void);
65 35e1f40a 2021-03-14 op
66 bc10f6a5 2021-07-12 op static struct imsgev *iev_ui;
67 c7107cec 2021-04-01 op static FILE *session;
68 eb2ed626 2021-10-07 op
69 f88fbabc 2021-11-27 op /*
70 f88fbabc 2021-11-27 op * Where to store user data. These are all equal to ~/.telescope if
71 f88fbabc 2021-11-27 op * it exists.
72 f88fbabc 2021-11-27 op */
73 fb3d08c1 2021-10-07 op char config_path_base[PATH_MAX];
74 fb3d08c1 2021-10-07 op char data_path_base[PATH_MAX];
75 fb3d08c1 2021-10-07 op char cache_path_base[PATH_MAX];
76 eb2ed626 2021-10-07 op
77 eb2ed626 2021-10-07 op char config_path[PATH_MAX];
78 fb3d08c1 2021-10-07 op char lockfile_path[PATH_MAX];
79 fb3d08c1 2021-10-07 op char bookmark_file[PATH_MAX];
80 fb3d08c1 2021-10-07 op char known_hosts_file[PATH_MAX], known_hosts_tmp[PATH_MAX];
81 fb3d08c1 2021-10-07 op char crashed_file[PATH_MAX];
82 fb3d08c1 2021-10-07 op char session_file[PATH_MAX];
83 740f578b 2021-03-15 op
84 35e1f40a 2021-03-14 op static imsg_handlerfn *handlers[] = {
85 35e1f40a 2021-03-14 op [IMSG_GET] = handle_get,
86 fb4dc49f 2021-08-13 op [IMSG_GET_FILE] = handle_get_file,
87 50f03682 2022-01-03 op [IMSG_QUIT] = handle_misc,
88 50f03682 2022-01-03 op [IMSG_INIT] = handle_misc,
89 740f578b 2021-03-15 op [IMSG_BOOKMARK_PAGE] = handle_bookmark_page,
90 3a227e9a 2021-03-18 op [IMSG_SAVE_CERT] = handle_save_cert,
91 288fd238 2021-04-25 op [IMSG_UPDATE_CERT] = handle_update_cert,
92 de2a69bb 2021-05-17 op [IMSG_FILE_OPEN] = handle_file_open,
93 c7107cec 2021-04-01 op [IMSG_SESSION_START] = handle_session_start,
94 c7107cec 2021-04-01 op [IMSG_SESSION_TAB] = handle_session_tab,
95 1040cc7f 2021-01-02 op [IMSG_SESSION_TAB_HIST] = handle_session_tab_hist,
96 c7107cec 2021-04-01 op [IMSG_SESSION_END] = handle_session_end,
97 35e1f40a 2021-03-14 op };
98 35e1f40a 2021-03-14 op
99 35e1f40a 2021-03-14 op static void __attribute__((__noreturn__))
100 35e1f40a 2021-03-14 op die(void)
101 35e1f40a 2021-03-14 op {
102 35e1f40a 2021-03-14 op abort(); /* TODO */
103 35e1f40a 2021-03-14 op }
104 35e1f40a 2021-03-14 op
105 35e1f40a 2021-03-14 op static void
106 fb4dc49f 2021-08-13 op send_file(uint32_t peerid, FILE *f)
107 fb4dc49f 2021-08-13 op {
108 fb4dc49f 2021-08-13 op ssize_t r;
109 fb4dc49f 2021-08-13 op char buf[BUFSIZ];
110 fb4dc49f 2021-08-13 op
111 fb4dc49f 2021-08-13 op for (;;) {
112 fb4dc49f 2021-08-13 op r = fread(buf, 1, sizeof(buf), f);
113 fb4dc49f 2021-08-13 op if (r != 0)
114 fb4dc49f 2021-08-13 op fs_send_ui(IMSG_BUF, peerid, -1, buf, r);
115 fb4dc49f 2021-08-13 op if (r != sizeof(buf))
116 fb4dc49f 2021-08-13 op break;
117 fb4dc49f 2021-08-13 op }
118 fb4dc49f 2021-08-13 op fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
119 fb4dc49f 2021-08-13 op fclose(f);
120 fb4dc49f 2021-08-13 op }
121 fb4dc49f 2021-08-13 op
122 fb4dc49f 2021-08-13 op static void
123 35e1f40a 2021-03-14 op handle_get(struct imsg *imsg, size_t datalen)
124 35e1f40a 2021-03-14 op {
125 56e7efb4 2021-07-21 op const char *bpath = "bookmarks.gmi";
126 fb4dc49f 2021-08-13 op char path[PATH_MAX];
127 56e7efb4 2021-07-21 op FILE *f;
128 a2728733 2021-07-18 op const char *data, *p;
129 a2728733 2021-07-18 op size_t i;
130 a2728733 2021-07-18 op struct page {
131 a2728733 2021-07-18 op const char *name;
132 56e7efb4 2021-07-21 op const char *path;
133 a2728733 2021-07-18 op const uint8_t *data;
134 a2728733 2021-07-18 op size_t len;
135 a2728733 2021-07-18 op } pages[] = {
136 56e7efb4 2021-07-21 op {"about", NULL, about_about, about_about_len},
137 56e7efb4 2021-07-21 op {"blank", NULL, about_blank, about_blank_len},
138 56e7efb4 2021-07-21 op {"bookmarks", bpath, bookmarks, bookmarks_len},
139 56e7efb4 2021-07-21 op {"crash", NULL, about_crash, about_crash_len},
140 56e7efb4 2021-07-21 op {"help", NULL, about_help, about_help_len},
141 56e7efb4 2021-07-21 op {"license", NULL, about_license, about_license_len},
142 56e7efb4 2021-07-21 op {"new", NULL, about_new, about_new_len},
143 56e7efb4 2021-07-21 op }, *page = NULL;
144 35e1f40a 2021-03-14 op
145 35e1f40a 2021-03-14 op data = imsg->data;
146 56e7efb4 2021-07-21 op if (data[datalen-1] != '\0') /* make sure it's NUL-terminated */
147 35e1f40a 2021-03-14 op die();
148 56e7efb4 2021-07-21 op if ((data = strchr(data, ':')) == NULL)
149 56e7efb4 2021-07-21 op goto notfound;
150 56e7efb4 2021-07-21 op data++;
151 35e1f40a 2021-03-14 op
152 56e7efb4 2021-07-21 op for (i = 0; i < sizeof(pages)/sizeof(pages[0]); ++i)
153 56e7efb4 2021-07-21 op if (!strcmp(data, pages[i].name)) {
154 56e7efb4 2021-07-21 op page = &pages[i];
155 56e7efb4 2021-07-21 op break;
156 56e7efb4 2021-07-21 op }
157 56e7efb4 2021-07-21 op
158 56e7efb4 2021-07-21 op if (page == NULL)
159 56e7efb4 2021-07-21 op goto notfound;
160 56e7efb4 2021-07-21 op
161 de04b178 2021-11-26 op strlcpy(path, data_path_base, sizeof(path));
162 56e7efb4 2021-07-21 op strlcat(path, "/", sizeof(path));
163 56e7efb4 2021-07-21 op if (page->path != NULL)
164 56e7efb4 2021-07-21 op strlcat(path, page->path, sizeof(path));
165 56e7efb4 2021-07-21 op else {
166 56e7efb4 2021-07-21 op strlcat(path, "pages/about_", sizeof(path));
167 56e7efb4 2021-07-21 op strlcat(path, page->name, sizeof(path));
168 56e7efb4 2021-07-21 op strlcat(path, ".gmi", sizeof(path));
169 56e7efb4 2021-07-21 op }
170 56e7efb4 2021-07-21 op
171 56e7efb4 2021-07-21 op if ((f = fopen(path, "r")) == NULL) {
172 56e7efb4 2021-07-21 op fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1,
173 56e7efb4 2021-07-21 op page->data, page->len);
174 56e7efb4 2021-07-21 op fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1,
175 56e7efb4 2021-07-21 op NULL, 0);
176 a2728733 2021-07-18 op return;
177 35e1f40a 2021-03-14 op }
178 a2728733 2021-07-18 op
179 fb4dc49f 2021-08-13 op send_file(imsg->hdr.peerid, f);
180 56e7efb4 2021-07-21 op return;
181 56e7efb4 2021-07-21 op
182 56e7efb4 2021-07-21 op notfound:
183 a2728733 2021-07-18 op p = "# not found!\n";
184 a2728733 2021-07-18 op fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, p, strlen(p));
185 a2728733 2021-07-18 op fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
186 fb4dc49f 2021-08-13 op }
187 fb4dc49f 2021-08-13 op
188 fb4dc49f 2021-08-13 op static inline void
189 fb4dc49f 2021-08-13 op send_hdr(uint32_t peerid, int code, const char *meta)
190 fb4dc49f 2021-08-13 op {
191 fb4dc49f 2021-08-13 op fs_send_ui(IMSG_GOT_CODE, peerid, -1, &code, sizeof(code));
192 fb4dc49f 2021-08-13 op fs_send_ui(IMSG_GOT_META, peerid, -1, meta, strlen(meta)+1);
193 fb4dc49f 2021-08-13 op }
194 fb4dc49f 2021-08-13 op
195 24a68158 2021-08-13 op static inline void
196 24a68158 2021-08-13 op send_errno(uint32_t peerid, int code, const char *str, int no)
197 24a68158 2021-08-13 op {
198 24a68158 2021-08-13 op char *s;
199 24a68158 2021-08-13 op
200 24a68158 2021-08-13 op if (asprintf(&s, "%s: %s", str, strerror(no)) == -1)
201 24a68158 2021-08-13 op s = NULL;
202 24a68158 2021-08-13 op
203 24a68158 2021-08-13 op send_hdr(peerid, code, s == NULL ? str : s);
204 24a68158 2021-08-13 op free(s);
205 24a68158 2021-08-13 op }
206 24a68158 2021-08-13 op
207 65124267 2021-08-13 op static inline const char *
208 65124267 2021-08-13 op file_type(const char *path)
209 fb4dc49f 2021-08-13 op {
210 fb4dc49f 2021-08-13 op struct mapping {
211 fb4dc49f 2021-08-13 op const char *ext;
212 fb4dc49f 2021-08-13 op const char *mime;
213 fb4dc49f 2021-08-13 op } ms[] = {
214 fb4dc49f 2021-08-13 op {"diff", "text/x-patch"},
215 fb4dc49f 2021-08-13 op {"gemini", "text/gemini"},
216 fb4dc49f 2021-08-13 op {"gmi", "text/gemini"},
217 fb4dc49f 2021-08-13 op {"markdown", "text/plain"},
218 fb4dc49f 2021-08-13 op {"md", "text/plain"},
219 fb4dc49f 2021-08-13 op {"patch", "text/x-patch"},
220 fb4dc49f 2021-08-13 op {"txt", "text/plain"},
221 fb4dc49f 2021-08-13 op {NULL, NULL},
222 65124267 2021-08-13 op }, *m;
223 65124267 2021-08-13 op char *dot;
224 65124267 2021-08-13 op
225 65124267 2021-08-13 op if ((dot = strrchr(path, '.')) == NULL)
226 65124267 2021-08-13 op return NULL;
227 65124267 2021-08-13 op
228 65124267 2021-08-13 op dot++;
229 65124267 2021-08-13 op
230 65124267 2021-08-13 op for (m = ms; m->ext != NULL; ++m)
231 65124267 2021-08-13 op if (!strcmp(m->ext, dot))
232 65124267 2021-08-13 op return m->mime;
233 65124267 2021-08-13 op
234 65124267 2021-08-13 op return NULL;
235 5cbe1763 2021-08-13 op }
236 5cbe1763 2021-08-13 op
237 5cbe1763 2021-08-13 op static int
238 5cbe1763 2021-08-13 op select_non_dot(const struct dirent *d)
239 5cbe1763 2021-08-13 op {
240 5cbe1763 2021-08-13 op return strcmp(d->d_name, ".");
241 5cbe1763 2021-08-13 op }
242 5cbe1763 2021-08-13 op
243 5cbe1763 2021-08-13 op static int
244 5cbe1763 2021-08-13 op select_non_dotdot(const struct dirent *d)
245 5cbe1763 2021-08-13 op {
246 5cbe1763 2021-08-13 op return strcmp(d->d_name, ".") && strcmp(d->d_name, "..");
247 65124267 2021-08-13 op }
248 65124267 2021-08-13 op
249 65124267 2021-08-13 op static inline void
250 65124267 2021-08-13 op send_dir(uint32_t peerid, const char *path)
251 65124267 2021-08-13 op {
252 65124267 2021-08-13 op struct dirent **names;
253 65124267 2021-08-13 op struct evbuffer *ev;
254 5d43215b 2021-08-13 op char *s;
255 5cbe1763 2021-08-13 op int (*selector)(const struct dirent *) = select_non_dot;
256 24a68158 2021-08-13 op int i, len, no;
257 65124267 2021-08-13 op
258 5d43215b 2021-08-13 op if (!has_suffix(path, "/")) {
259 5d43215b 2021-08-13 op if (asprintf(&s, "%s/", path) == -1)
260 5d43215b 2021-08-13 op die();
261 5d43215b 2021-08-13 op send_hdr(peerid, 30, s);
262 5d43215b 2021-08-13 op free(s);
263 5d43215b 2021-08-13 op return;
264 5d43215b 2021-08-13 op }
265 5d43215b 2021-08-13 op
266 5cbe1763 2021-08-13 op if (!strcmp(path, "/"))
267 5cbe1763 2021-08-13 op selector = select_non_dotdot;
268 5cbe1763 2021-08-13 op
269 65124267 2021-08-13 op if ((ev = evbuffer_new()) == NULL ||
270 5cbe1763 2021-08-13 op (len = scandir(path, &names, selector, alphasort)) == -1) {
271 24a68158 2021-08-13 op no = errno;
272 65124267 2021-08-13 op evbuffer_free(ev);
273 24a68158 2021-08-13 op send_errno(peerid, 40, "failure reading the directory", no);
274 65124267 2021-08-13 op return;
275 65124267 2021-08-13 op }
276 65124267 2021-08-13 op
277 65124267 2021-08-13 op evbuffer_add_printf(ev, "# Index of %s\n\n", path);
278 65124267 2021-08-13 op for (i = 0; i < len; ++i) {
279 65124267 2021-08-13 op evbuffer_add_printf(ev, "=> %s", names[i]->d_name);
280 65124267 2021-08-13 op if (names[i]->d_type == DT_DIR)
281 65124267 2021-08-13 op evbuffer_add(ev, "/", 1);
282 65124267 2021-08-13 op evbuffer_add(ev, "\n", 1);
283 65124267 2021-08-13 op }
284 65124267 2021-08-13 op
285 65124267 2021-08-13 op send_hdr(peerid, 20, "text/gemini");
286 65124267 2021-08-13 op fs_send_ui(IMSG_BUF, peerid, -1,
287 65124267 2021-08-13 op EVBUFFER_DATA(ev), EVBUFFER_LENGTH(ev));
288 65124267 2021-08-13 op fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
289 65124267 2021-08-13 op
290 65124267 2021-08-13 op evbuffer_free(ev);
291 65124267 2021-08-13 op free(names);
292 65124267 2021-08-13 op }
293 65124267 2021-08-13 op
294 65124267 2021-08-13 op static void
295 65124267 2021-08-13 op handle_get_file(struct imsg *imsg, size_t datalen)
296 65124267 2021-08-13 op {
297 65124267 2021-08-13 op struct stat sb;
298 fb4dc49f 2021-08-13 op FILE *f;
299 65124267 2021-08-13 op char *data;
300 fb4dc49f 2021-08-13 op const char *meta = NULL;
301 fb4dc49f 2021-08-13 op
302 fb4dc49f 2021-08-13 op data = imsg->data;
303 fb4dc49f 2021-08-13 op data[datalen-1] = '\0';
304 fb4dc49f 2021-08-13 op
305 65124267 2021-08-13 op if ((f = fopen(data, "r")) == NULL) {
306 24a68158 2021-08-13 op send_errno(imsg->hdr.peerid, 51, "can't open", errno);
307 fb4dc49f 2021-08-13 op return;
308 fb4dc49f 2021-08-13 op }
309 fb4dc49f 2021-08-13 op
310 65124267 2021-08-13 op if (fstat(fileno(f), &sb) == -1) {
311 24a68158 2021-08-13 op send_errno(imsg->hdr.peerid, 40, "fstat", errno);
312 65124267 2021-08-13 op return;
313 fb4dc49f 2021-08-13 op }
314 fb4dc49f 2021-08-13 op
315 65124267 2021-08-13 op if (S_ISDIR(sb.st_mode)) {
316 65124267 2021-08-13 op fclose(f);
317 65124267 2021-08-13 op send_dir(imsg->hdr.peerid, data);
318 fb4dc49f 2021-08-13 op return;
319 fb4dc49f 2021-08-13 op }
320 fb4dc49f 2021-08-13 op
321 65124267 2021-08-13 op if ((meta = file_type(data)) == NULL) {
322 65124267 2021-08-13 op fclose(f);
323 65124267 2021-08-13 op send_hdr(imsg->hdr.peerid, 51,
324 65124267 2021-08-13 op "don't know how to visualize this file");
325 fb4dc49f 2021-08-13 op return;
326 fb4dc49f 2021-08-13 op }
327 fb4dc49f 2021-08-13 op
328 fb4dc49f 2021-08-13 op send_hdr(imsg->hdr.peerid, 20, meta);
329 fb4dc49f 2021-08-13 op send_file(imsg->hdr.peerid, f);
330 35e1f40a 2021-03-14 op }
331 35e1f40a 2021-03-14 op
332 35e1f40a 2021-03-14 op static void
333 50f03682 2022-01-03 op handle_misc(struct imsg *imsg, size_t datalen)
334 50f03682 2022-01-03 op {
335 50f03682 2022-01-03 op switch (imsg->hdr.type) {
336 50f03682 2022-01-03 op case IMSG_INIT:
337 eb722b50 2022-01-03 op load_certs();
338 50f03682 2022-01-03 op load_last_session();
339 50f03682 2022-01-03 op break;
340 be97d6e6 2021-08-15 op
341 50f03682 2022-01-03 op case IMSG_QUIT:
342 50f03682 2022-01-03 op if (!safe_mode)
343 50f03682 2022-01-03 op unlink(crashed_file);
344 50f03682 2022-01-03 op event_loopbreak();
345 50f03682 2022-01-03 op break;
346 50f03682 2022-01-03 op
347 50f03682 2022-01-03 op default:
348 50f03682 2022-01-03 op die();
349 50f03682 2022-01-03 op }
350 35e1f40a 2021-03-14 op }
351 35e1f40a 2021-03-14 op
352 35e1f40a 2021-03-14 op static void
353 740f578b 2021-03-15 op handle_bookmark_page(struct imsg *imsg, size_t datalen)
354 740f578b 2021-03-15 op {
355 740f578b 2021-03-15 op char *data;
356 740f578b 2021-03-15 op int res;
357 740f578b 2021-03-15 op FILE *f;
358 740f578b 2021-03-15 op
359 740f578b 2021-03-15 op data = imsg->data;
360 740f578b 2021-03-15 op if (data[datalen-1] != '\0')
361 740f578b 2021-03-15 op die();
362 740f578b 2021-03-15 op
363 740f578b 2021-03-15 op if ((f = fopen(bookmark_file, "a")) == NULL) {
364 740f578b 2021-03-15 op res = errno;
365 740f578b 2021-03-15 op goto end;
366 740f578b 2021-03-15 op }
367 740f578b 2021-03-15 op fprintf(f, "=> %s\n", data);
368 740f578b 2021-03-15 op fclose(f);
369 740f578b 2021-03-15 op
370 740f578b 2021-03-15 op res = 0;
371 740f578b 2021-03-15 op end:
372 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_BOOKMARK_OK, 0, -1, &res, sizeof(res));
373 740f578b 2021-03-15 op }
374 740f578b 2021-03-15 op
375 740f578b 2021-03-15 op static void
376 3a227e9a 2021-03-18 op handle_save_cert(struct imsg *imsg, size_t datalen)
377 3a227e9a 2021-03-18 op {
378 3a227e9a 2021-03-18 op struct tofu_entry e;
379 3a227e9a 2021-03-18 op FILE *f;
380 3a227e9a 2021-03-18 op int res;
381 3a227e9a 2021-03-18 op
382 3a227e9a 2021-03-18 op /* TODO: traverse the file to avoid duplications? */
383 3a227e9a 2021-03-18 op
384 3a227e9a 2021-03-18 op if (datalen != sizeof(e))
385 3a227e9a 2021-03-18 op die();
386 3a227e9a 2021-03-18 op memcpy(&e, imsg->data, datalen);
387 3a227e9a 2021-03-18 op
388 3a227e9a 2021-03-18 op if ((f = fopen(known_hosts_file, "a")) == NULL) {
389 3a227e9a 2021-03-18 op res = errno;
390 3a227e9a 2021-03-18 op goto end;
391 3a227e9a 2021-03-18 op }
392 3a227e9a 2021-03-18 op fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
393 3a227e9a 2021-03-18 op fclose(f);
394 3a227e9a 2021-03-18 op
395 3a227e9a 2021-03-18 op res = 0;
396 3a227e9a 2021-03-18 op end:
397 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_SAVE_CERT_OK, imsg->hdr.peerid, -1,
398 288fd238 2021-04-25 op &res, sizeof(res));
399 288fd238 2021-04-25 op }
400 288fd238 2021-04-25 op
401 288fd238 2021-04-25 op static void
402 288fd238 2021-04-25 op handle_update_cert(struct imsg *imsg, size_t datalen)
403 288fd238 2021-04-25 op {
404 288fd238 2021-04-25 op FILE *tmp, *f;
405 288fd238 2021-04-25 op struct tofu_entry entry;
406 288fd238 2021-04-25 op char sfn[PATH_MAX], *line = NULL, *t;
407 288fd238 2021-04-25 op size_t l, linesize = 0;
408 288fd238 2021-04-25 op ssize_t linelen;
409 288fd238 2021-04-25 op int fd, e, res = 0;
410 288fd238 2021-04-25 op
411 288fd238 2021-04-25 op if (datalen != sizeof(entry))
412 288fd238 2021-04-25 op die();
413 288fd238 2021-04-25 op memcpy(&entry, imsg->data, datalen);
414 288fd238 2021-04-25 op
415 288fd238 2021-04-25 op strlcpy(sfn, known_hosts_tmp, sizeof(sfn));
416 288fd238 2021-04-25 op if ((fd = mkstemp(sfn)) == -1 ||
417 288fd238 2021-04-25 op (tmp = fdopen(fd, "w")) == NULL) {
418 288fd238 2021-04-25 op if (fd != -1) {
419 288fd238 2021-04-25 op unlink(sfn);
420 288fd238 2021-04-25 op close(fd);
421 288fd238 2021-04-25 op }
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 if ((f = fopen(known_hosts_file, "r")) == NULL) {
427 288fd238 2021-04-25 op unlink(sfn);
428 288fd238 2021-04-25 op fclose(tmp);
429 288fd238 2021-04-25 op res = 0;
430 288fd238 2021-04-25 op goto end;
431 288fd238 2021-04-25 op }
432 288fd238 2021-04-25 op
433 288fd238 2021-04-25 op l = strlen(entry.domain);
434 288fd238 2021-04-25 op while ((linelen = getline(&line, &linesize, f)) != -1) {
435 288fd238 2021-04-25 op if ((t = strstr(line, entry.domain)) != NULL &&
436 288fd238 2021-04-25 op (line[l] == ' ' || line[l] == '\t'))
437 288fd238 2021-04-25 op continue;
438 288fd238 2021-04-25 op /* line has a trailing \n */
439 288fd238 2021-04-25 op fprintf(tmp, "%s", line);
440 288fd238 2021-04-25 op }
441 288fd238 2021-04-25 op fprintf(tmp, "%s %s %d\n", entry.domain, entry.hash, entry.verified);
442 288fd238 2021-04-25 op
443 288fd238 2021-04-25 op free(line);
444 288fd238 2021-04-25 op e = ferror(tmp);
445 288fd238 2021-04-25 op
446 288fd238 2021-04-25 op fclose(tmp);
447 288fd238 2021-04-25 op fclose(f);
448 288fd238 2021-04-25 op
449 288fd238 2021-04-25 op if (e) {
450 288fd238 2021-04-25 op unlink(sfn);
451 288fd238 2021-04-25 op res = 0;
452 288fd238 2021-04-25 op goto end;
453 288fd238 2021-04-25 op }
454 288fd238 2021-04-25 op
455 288fd238 2021-04-25 op res = rename(sfn, known_hosts_file) != -1;
456 288fd238 2021-04-25 op
457 288fd238 2021-04-25 op end:
458 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_UPDATE_CERT_OK, imsg->hdr.peerid, -1,
459 3a227e9a 2021-03-18 op &res, sizeof(res));
460 de2a69bb 2021-05-17 op }
461 de2a69bb 2021-05-17 op
462 de2a69bb 2021-05-17 op static void
463 de2a69bb 2021-05-17 op handle_file_open(struct imsg *imsg, size_t datalen)
464 de2a69bb 2021-05-17 op {
465 de2a69bb 2021-05-17 op char *path, *e;
466 de2a69bb 2021-05-17 op int fd;
467 de2a69bb 2021-05-17 op
468 de2a69bb 2021-05-17 op path = imsg->data;
469 de2a69bb 2021-05-17 op if (path[datalen-1] != '\0')
470 de2a69bb 2021-05-17 op die();
471 de2a69bb 2021-05-17 op
472 de2a69bb 2021-05-17 op if ((fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644)) == -1) {
473 de2a69bb 2021-05-17 op e = strerror(errno);
474 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, -1,
475 de2a69bb 2021-05-17 op e, strlen(e)+1);
476 de2a69bb 2021-05-17 op } else
477 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, fd,
478 de2a69bb 2021-05-17 op NULL, 0);
479 3a227e9a 2021-03-18 op }
480 3a227e9a 2021-03-18 op
481 3a227e9a 2021-03-18 op static void
482 c7107cec 2021-04-01 op handle_session_start(struct imsg *imsg, size_t datalen)
483 c7107cec 2021-04-01 op {
484 c7107cec 2021-04-01 op if (datalen != 0)
485 c7107cec 2021-04-01 op die();
486 c7107cec 2021-04-01 op
487 c7107cec 2021-04-01 op if ((session = fopen(session_file, "w")) == NULL)
488 c7107cec 2021-04-01 op die();
489 c7107cec 2021-04-01 op }
490 c7107cec 2021-04-01 op
491 c7107cec 2021-04-01 op static void
492 c7107cec 2021-04-01 op handle_session_tab(struct imsg *imsg, size_t datalen)
493 c7107cec 2021-04-01 op {
494 f8c6e753 2021-12-30 op struct session_tab tab;
495 58df4f47 2021-07-17 op
496 c7107cec 2021-04-01 op if (session == NULL)
497 c7107cec 2021-04-01 op die();
498 c7107cec 2021-04-01 op
499 f8c6e753 2021-12-30 op if (datalen != sizeof(tab))
500 c7107cec 2021-04-01 op die();
501 87e3e801 2021-07-17 op
502 f8c6e753 2021-12-30 op memcpy(&tab, imsg->data, sizeof(tab));
503 f8c6e753 2021-12-30 op if (tab.uri[sizeof(tab.uri)-1] != '\0' ||
504 f8c6e753 2021-12-30 op tab.title[sizeof(tab.title)-1] != '\0')
505 f8c6e753 2021-12-30 op die();
506 f8c6e753 2021-12-30 op
507 f8c6e753 2021-12-30 op fprintf(session, "%s", tab.uri);
508 f8c6e753 2021-12-30 op
509 6c74799d 2022-01-05 op if (tab.flags == 0)
510 2f524b17 2021-07-17 op fprintf(session, " - ");
511 6c74799d 2022-01-05 op else {
512 6c74799d 2022-01-05 op fprintf(session, " ");
513 6c74799d 2022-01-05 op if (tab.flags & TAB_CURRENT)
514 6c74799d 2022-01-05 op fprintf(session, "current,");
515 6c74799d 2022-01-05 op if (tab.flags & TAB_KILLED)
516 6c74799d 2022-01-05 op fprintf(session, "killed,");
517 6c74799d 2022-01-05 op fprintf(session, " ");
518 6c74799d 2022-01-05 op }
519 87e3e801 2021-07-17 op
520 f8c6e753 2021-12-30 op fprintf(session, "%s\n", tab.title);
521 c7107cec 2021-04-01 op }
522 c7107cec 2021-04-01 op
523 c7107cec 2021-04-01 op static void
524 1040cc7f 2021-01-02 op handle_session_tab_hist(struct imsg *imsg, size_t datalen)
525 1040cc7f 2021-01-02 op {
526 1040cc7f 2021-01-02 op struct session_tab_hist th;
527 1040cc7f 2021-01-02 op
528 1040cc7f 2021-01-02 op if (session == NULL)
529 1040cc7f 2021-01-02 op die();
530 1040cc7f 2021-01-02 op
531 1040cc7f 2021-01-02 op if (datalen != sizeof(th))
532 1040cc7f 2021-01-02 op die();
533 1040cc7f 2021-01-02 op
534 1040cc7f 2021-01-02 op memcpy(&th, imsg->data, sizeof(th));
535 1040cc7f 2021-01-02 op if (th.uri[sizeof(th.uri)-1] != '\0')
536 1040cc7f 2021-01-02 op die();
537 1040cc7f 2021-01-02 op
538 1040cc7f 2021-01-02 op fprintf(session, "%s %s\n", th.future ? ">" : "<", th.uri);
539 1040cc7f 2021-01-02 op }
540 1040cc7f 2021-01-02 op
541 1040cc7f 2021-01-02 op static void
542 c7107cec 2021-04-01 op handle_session_end(struct imsg *imsg, size_t datalen)
543 c7107cec 2021-04-01 op {
544 c7107cec 2021-04-01 op if (session == NULL)
545 c7107cec 2021-04-01 op die();
546 c7107cec 2021-04-01 op fclose(session);
547 c7107cec 2021-04-01 op session = NULL;
548 c7107cec 2021-04-01 op }
549 c7107cec 2021-04-01 op
550 c7107cec 2021-04-01 op static void
551 1304bbdd 2021-03-15 op handle_dispatch_imsg(int fd, short ev, void *d)
552 35e1f40a 2021-03-14 op {
553 bc10f6a5 2021-07-12 op struct imsgev *iev = d;
554 ea080950 2021-07-20 op int e;
555 ea080950 2021-07-20 op
556 ea080950 2021-07-20 op if (dispatch_imsg(iev, ev, handlers, sizeof(handlers)) == -1) {
557 ea080950 2021-07-20 op /*
558 eb2ed626 2021-10-07 op * This should leave a ~/.cache/telescope/crashed file to
559 ea080950 2021-07-20 op * trigger about:crash on next run. Unfortunately, if
560 ea080950 2021-07-20 op * the main process dies the fs sticks around and
561 ea080950 2021-07-20 op * doesn't notice that the fd was closed. Why EV_READ
562 ea080950 2021-07-20 op * is not triggered when a fd is closed on the other end?
563 ea080950 2021-07-20 op */
564 ea080950 2021-07-20 op e = errno;
565 ea080950 2021-07-20 op if ((fd = open(crashed_file, O_CREAT|O_TRUNC|O_WRONLY, 0600))
566 ea080950 2021-07-20 op == -1)
567 ea080950 2021-07-20 op err(1, "open");
568 ea080950 2021-07-20 op close(fd);
569 ea080950 2021-07-20 op errx(1, "connection closed: %s", strerror(e));
570 ea080950 2021-07-20 op }
571 35e1f40a 2021-03-14 op }
572 35e1f40a 2021-03-14 op
573 bc10f6a5 2021-07-12 op static int
574 bc10f6a5 2021-07-12 op fs_send_ui(int type, uint32_t peerid, int fd, const void *data,
575 bc10f6a5 2021-07-12 op uint16_t datalen)
576 bc10f6a5 2021-07-12 op {
577 bc10f6a5 2021-07-12 op return imsg_compose_event(iev_ui, type, peerid, 0, fd,
578 bc10f6a5 2021-07-12 op data, datalen);
579 bc10f6a5 2021-07-12 op }
580 bc10f6a5 2021-07-12 op
581 eb2ed626 2021-10-07 op static size_t
582 eb2ed626 2021-10-07 op join_path(char *buf, const char *lhs, const char *rhs, size_t buflen)
583 35e1f40a 2021-03-14 op {
584 eb2ed626 2021-10-07 op strlcpy(buf, lhs, buflen);
585 eb2ed626 2021-10-07 op return strlcat(buf, rhs, buflen);
586 eb2ed626 2021-10-07 op }
587 d0fd368a 2021-07-15 op
588 eb2ed626 2021-10-07 op static void
589 eb2ed626 2021-10-07 op getenv_default(char *buf, const char *name, const char *def, size_t buflen)
590 eb2ed626 2021-10-07 op {
591 eb2ed626 2021-10-07 op size_t ret;
592 eb2ed626 2021-10-07 op char *home, *env;
593 740f578b 2021-03-15 op
594 eb2ed626 2021-10-07 op if ((home = getenv("HOME")) == NULL)
595 eb2ed626 2021-10-07 op errx(1, "HOME is not defined");
596 3a227e9a 2021-03-18 op
597 eb2ed626 2021-10-07 op if ((env = getenv(name)) != NULL)
598 eb2ed626 2021-10-07 op ret = strlcpy(buf, env, buflen);
599 eb2ed626 2021-10-07 op else
600 eb2ed626 2021-10-07 op ret = join_path(buf, home, def, buflen);
601 288fd238 2021-04-25 op
602 eb2ed626 2021-10-07 op if (ret >= buflen)
603 eb2ed626 2021-10-07 op errx(1, "buffer too small for %s", name);
604 eb2ed626 2021-10-07 op }
605 c7107cec 2021-04-01 op
606 eb2ed626 2021-10-07 op static void
607 eb2ed626 2021-10-07 op mkdirs(const char *path, mode_t mode)
608 eb2ed626 2021-10-07 op {
609 444dad86 2021-10-07 op char copy[PATH_MAX+1], orig[PATH_MAX+1], *parent;
610 ea080950 2021-07-20 op
611 eb2ed626 2021-10-07 op strlcpy(copy, path, sizeof(copy));
612 444dad86 2021-10-07 op strlcpy(orig, path, sizeof(orig));
613 eb2ed626 2021-10-07 op parent = dirname(copy);
614 eb2ed626 2021-10-07 op if (!strcmp(parent, "/"))
615 eb2ed626 2021-10-07 op return;
616 eb2ed626 2021-10-07 op mkdirs(parent, mode);
617 eb2ed626 2021-10-07 op
618 444dad86 2021-10-07 op if (mkdir(orig, mode) != 0) {
619 eb2ed626 2021-10-07 op if (errno == EEXIST)
620 eb2ed626 2021-10-07 op return;
621 444dad86 2021-10-07 op err(1, "can't mkdir %s", orig);
622 eb2ed626 2021-10-07 op }
623 eb2ed626 2021-10-07 op }
624 eb2ed626 2021-10-07 op
625 eb2ed626 2021-10-07 op static void
626 6c8ddace 2022-01-03 op init_paths(void)
627 eb2ed626 2021-10-07 op {
628 7e60a21a 2022-01-03 op char xdg_config_base[PATH_MAX];
629 7e60a21a 2022-01-03 op char xdg_data_base[PATH_MAX];
630 7e60a21a 2022-01-03 op char xdg_cache_base[PATH_MAX];
631 7e60a21a 2022-01-03 op char old_path[PATH_MAX];
632 7e60a21a 2022-01-03 op char *home;
633 7e60a21a 2022-01-03 op struct stat info;
634 eb2ed626 2021-10-07 op
635 eb2ed626 2021-10-07 op /* old path */
636 eb2ed626 2021-10-07 op if ((home = getenv("HOME")) == NULL)
637 eb2ed626 2021-10-07 op errx(1, "HOME is not defined");
638 eb2ed626 2021-10-07 op join_path(old_path, home, "/.telescope", sizeof(old_path));
639 eb2ed626 2021-10-07 op
640 eb2ed626 2021-10-07 op /* if ~/.telescope exists, use that instead of xdg dirs */
641 eb2ed626 2021-10-07 op if (stat(old_path, &info) == 0 && S_ISDIR(info.st_mode)) {
642 eb2ed626 2021-10-07 op join_path(config_path_base, home, "/.telescope",
643 eb2ed626 2021-10-07 op sizeof(config_path_base));
644 eb2ed626 2021-10-07 op join_path(data_path_base, home, "/.telescope",
645 eb2ed626 2021-10-07 op sizeof(data_path_base));
646 eb2ed626 2021-10-07 op join_path(cache_path_base, home, "/.telescope",
647 eb2ed626 2021-10-07 op sizeof(cache_path_base));
648 eb2ed626 2021-10-07 op return;
649 eb2ed626 2021-10-07 op }
650 eb2ed626 2021-10-07 op
651 eb2ed626 2021-10-07 op /* xdg paths */
652 eb2ed626 2021-10-07 op getenv_default(xdg_config_base, "XDG_CONFIG_HOME", "/.config",
653 eb2ed626 2021-10-07 op sizeof(xdg_config_base));
654 eb2ed626 2021-10-07 op getenv_default(xdg_data_base, "XDG_DATA_HOME", "/.local/share",
655 eb2ed626 2021-10-07 op sizeof(xdg_data_base));
656 eb2ed626 2021-10-07 op getenv_default(xdg_cache_base, "XDG_CACHE_HOME", "/.cache",
657 eb2ed626 2021-10-07 op sizeof(xdg_cache_base));
658 eb2ed626 2021-10-07 op
659 eb2ed626 2021-10-07 op join_path(config_path_base, xdg_config_base, "/telescope",
660 eb2ed626 2021-10-07 op sizeof(config_path_base));
661 eb2ed626 2021-10-07 op join_path(data_path_base, xdg_data_base, "/telescope",
662 eb2ed626 2021-10-07 op sizeof(data_path_base));
663 eb2ed626 2021-10-07 op join_path(cache_path_base, xdg_cache_base, "/telescope",
664 eb2ed626 2021-10-07 op sizeof(cache_path_base));
665 eb2ed626 2021-10-07 op
666 eb2ed626 2021-10-07 op mkdirs(xdg_config_base, S_IRWXU);
667 eb2ed626 2021-10-07 op mkdirs(xdg_data_base, S_IRWXU);
668 eb2ed626 2021-10-07 op mkdirs(xdg_cache_base, S_IRWXU);
669 eb2ed626 2021-10-07 op
670 eb2ed626 2021-10-07 op mkdirs(config_path_base, S_IRWXU);
671 eb2ed626 2021-10-07 op mkdirs(data_path_base, S_IRWXU);
672 eb2ed626 2021-10-07 op mkdirs(cache_path_base, S_IRWXU);
673 eb2ed626 2021-10-07 op }
674 eb2ed626 2021-10-07 op
675 eb2ed626 2021-10-07 op int
676 eb2ed626 2021-10-07 op fs_init(void)
677 eb2ed626 2021-10-07 op {
678 6c8ddace 2022-01-03 op init_paths();
679 eb2ed626 2021-10-07 op
680 eb2ed626 2021-10-07 op join_path(config_path, config_path_base, "/config",
681 eb2ed626 2021-10-07 op sizeof(config_path));
682 eb2ed626 2021-10-07 op join_path(lockfile_path, cache_path_base, "/lock",
683 eb2ed626 2021-10-07 op sizeof(lockfile_path));
684 eb2ed626 2021-10-07 op join_path(bookmark_file, data_path_base, "/bookmarks.gmi",
685 eb2ed626 2021-10-07 op sizeof(bookmark_file));
686 eb2ed626 2021-10-07 op join_path(known_hosts_file, data_path_base, "/known_hosts",
687 eb2ed626 2021-10-07 op sizeof(known_hosts_file));
688 eb2ed626 2021-10-07 op join_path(known_hosts_tmp, cache_path_base,
689 eb2ed626 2021-10-07 op "/known_hosts.tmp.XXXXXXXXXX", sizeof(known_hosts_tmp));
690 eb2ed626 2021-10-07 op join_path(session_file, cache_path_base, "/session",
691 eb2ed626 2021-10-07 op sizeof(session_file));
692 eb2ed626 2021-10-07 op join_path(crashed_file, cache_path_base, "/crashed",
693 eb2ed626 2021-10-07 op sizeof(crashed_file));
694 eb2ed626 2021-10-07 op
695 3a227e9a 2021-03-18 op return 1;
696 3a227e9a 2021-03-18 op }
697 bb28f1c2 2021-12-30 op
698 bb28f1c2 2021-12-30 op /*
699 bb28f1c2 2021-12-30 op * Parse a line of the session file. The format is:
700 bb28f1c2 2021-12-30 op *
701 bb28f1c2 2021-12-30 op * URL [flags,...] [title]\n
702 bb28f1c2 2021-12-30 op */
703 bb28f1c2 2021-12-30 op static void
704 bb28f1c2 2021-12-30 op parse_session_line(char *line, const char **title, uint32_t *flags)
705 bb28f1c2 2021-12-30 op {
706 bb28f1c2 2021-12-30 op char *s, *t, *ap;
707 bb28f1c2 2021-12-30 op
708 bb28f1c2 2021-12-30 op *title = "";
709 bb28f1c2 2021-12-30 op *flags = 0;
710 bb28f1c2 2021-12-30 op if ((s = strchr(line, ' ')) == NULL)
711 bb28f1c2 2021-12-30 op return;
712 3a227e9a 2021-03-18 op
713 bb28f1c2 2021-12-30 op *s++ = '\0';
714 bb28f1c2 2021-12-30 op
715 bb28f1c2 2021-12-30 op if ((t = strchr(s, ' ')) != NULL) {
716 bb28f1c2 2021-12-30 op *t++ = '\0';
717 bb28f1c2 2021-12-30 op *title = t;
718 bb28f1c2 2021-12-30 op }
719 bb28f1c2 2021-12-30 op
720 bb28f1c2 2021-12-30 op while ((ap = strsep(&s, ",")) != NULL) {
721 bb28f1c2 2021-12-30 op if (!strcmp(ap, "current"))
722 bb28f1c2 2021-12-30 op *flags |= TAB_CURRENT;
723 6c74799d 2022-01-05 op else if (!strcmp(ap, "killed"))
724 6c74799d 2022-01-05 op *flags |= TAB_KILLED;
725 bb28f1c2 2021-12-30 op }
726 bb28f1c2 2021-12-30 op }
727 bb28f1c2 2021-12-30 op
728 bb28f1c2 2021-12-30 op static inline void
729 bb28f1c2 2021-12-30 op sendtab(uint32_t flags, const char *uri, const char *title)
730 bb28f1c2 2021-12-30 op {
731 bb28f1c2 2021-12-30 op struct session_tab tab;
732 bb28f1c2 2021-12-30 op
733 bb28f1c2 2021-12-30 op memset(&tab, 0, sizeof(tab));
734 bb28f1c2 2021-12-30 op tab.flags = flags;
735 bb28f1c2 2021-12-30 op
736 bb28f1c2 2021-12-30 op if (strlcpy(tab.uri, uri, sizeof(tab.uri)) >= sizeof(tab.uri))
737 bb28f1c2 2021-12-30 op return;
738 bb28f1c2 2021-12-30 op
739 bb28f1c2 2021-12-30 op /* don't worry about cached title truncation */
740 bb28f1c2 2021-12-30 op if (title != NULL)
741 bb28f1c2 2021-12-30 op strlcpy(tab.title, title, sizeof(tab.title));
742 bb28f1c2 2021-12-30 op
743 bb28f1c2 2021-12-30 op fs_send_ui(IMSG_SESSION_TAB, 0, -1, &tab, sizeof(tab));
744 1040cc7f 2021-01-02 op }
745 1040cc7f 2021-01-02 op
746 1040cc7f 2021-01-02 op static inline void
747 1040cc7f 2021-01-02 op sendhist(const char *uri, int future)
748 1040cc7f 2021-01-02 op {
749 1040cc7f 2021-01-02 op struct session_tab_hist sth;
750 1040cc7f 2021-01-02 op
751 1040cc7f 2021-01-02 op memset(&sth, 0, sizeof(sth));
752 1040cc7f 2021-01-02 op sth.future = future;
753 1040cc7f 2021-01-02 op
754 1040cc7f 2021-01-02 op if (strlcpy(sth.uri, uri, sizeof(sth.uri)) >= sizeof(sth.uri))
755 1040cc7f 2021-01-02 op return;
756 1040cc7f 2021-01-02 op
757 1040cc7f 2021-01-02 op fs_send_ui(IMSG_SESSION_TAB_HIST, 0, -1, &sth, sizeof(sth));
758 bb28f1c2 2021-12-30 op }
759 bb28f1c2 2021-12-30 op
760 bb28f1c2 2021-12-30 op static void
761 50f03682 2022-01-03 op load_last_session(void)
762 bb28f1c2 2021-12-30 op {
763 bb28f1c2 2021-12-30 op FILE *session;
764 bb28f1c2 2021-12-30 op uint32_t flags;
765 bb28f1c2 2021-12-30 op size_t linesize = 0;
766 bb28f1c2 2021-12-30 op ssize_t linelen;
767 bb28f1c2 2021-12-30 op int first_time = 0;
768 1040cc7f 2021-01-02 op int future;
769 bb28f1c2 2021-12-30 op const char *title;
770 1040cc7f 2021-01-02 op char *nl, *s, *line = NULL;
771 bb28f1c2 2021-12-30 op
772 bb28f1c2 2021-12-30 op if ((session = fopen(session_file, "r")) == NULL) {
773 bb28f1c2 2021-12-30 op /* first time? */
774 bb28f1c2 2021-12-30 op first_time = 1;
775 bb28f1c2 2021-12-30 op goto end;
776 bb28f1c2 2021-12-30 op }
777 bb28f1c2 2021-12-30 op
778 bb28f1c2 2021-12-30 op while ((linelen = getline(&line, &linesize, session)) != -1) {
779 bb28f1c2 2021-12-30 op if ((nl = strchr(line, '\n')) != NULL)
780 bb28f1c2 2021-12-30 op *nl = '\0';
781 1040cc7f 2021-01-02 op
782 1040cc7f 2021-01-02 op if (*line == '<' || *line == '>') {
783 1040cc7f 2021-01-02 op future = *line == '>';
784 1040cc7f 2021-01-02 op s = line+1;
785 1040cc7f 2021-01-02 op if (*s != ' ')
786 1040cc7f 2021-01-02 op continue;
787 1040cc7f 2021-01-02 op sendhist(++s, future);
788 1040cc7f 2021-01-02 op } else {
789 1040cc7f 2021-01-02 op parse_session_line(line, &title, &flags);
790 1040cc7f 2021-01-02 op sendtab(flags, line, title);
791 1040cc7f 2021-01-02 op }
792 bb28f1c2 2021-12-30 op }
793 bb28f1c2 2021-12-30 op
794 bb28f1c2 2021-12-30 op fclose(session);
795 bb28f1c2 2021-12-30 op free(line);
796 bb28f1c2 2021-12-30 op
797 bb28f1c2 2021-12-30 op if (last_time_crashed())
798 bb28f1c2 2021-12-30 op sendtab(TAB_CURRENT, "about:crash", NULL);
799 bb28f1c2 2021-12-30 op
800 bb28f1c2 2021-12-30 op end:
801 bb28f1c2 2021-12-30 op fs_send_ui(IMSG_SESSION_END, 0, -1, &first_time, sizeof(first_time));
802 bb28f1c2 2021-12-30 op }
803 bb28f1c2 2021-12-30 op
804 3a227e9a 2021-03-18 op int
805 6cc5fcfe 2021-07-08 op fs_main(void)
806 3a227e9a 2021-03-18 op {
807 6cc5fcfe 2021-07-08 op setproctitle("fs");
808 3a227e9a 2021-03-18 op
809 6cc5fcfe 2021-07-08 op fs_init();
810 6cc5fcfe 2021-07-08 op
811 35e1f40a 2021-03-14 op event_init();
812 35e1f40a 2021-03-14 op
813 bc10f6a5 2021-07-12 op /* Setup pipe and event handler to the main process */
814 bc10f6a5 2021-07-12 op if ((iev_ui = malloc(sizeof(*iev_ui))) == NULL)
815 6cc5fcfe 2021-07-08 op die();
816 bc10f6a5 2021-07-12 op imsg_init(&iev_ui->ibuf, 3);
817 bc10f6a5 2021-07-12 op iev_ui->handler = handle_dispatch_imsg;
818 bc10f6a5 2021-07-12 op iev_ui->events = EV_READ;
819 bc10f6a5 2021-07-12 op event_set(&iev_ui->ev, iev_ui->ibuf.fd, iev_ui->events,
820 bc10f6a5 2021-07-12 op iev_ui->handler, iev_ui);
821 bc10f6a5 2021-07-12 op event_add(&iev_ui->ev, NULL);
822 35e1f40a 2021-03-14 op
823 35e1f40a 2021-03-14 op sandbox_fs_process();
824 35e1f40a 2021-03-14 op
825 35e1f40a 2021-03-14 op event_dispatch();
826 35e1f40a 2021-03-14 op return 0;
827 35e1f40a 2021-03-14 op }
828 3a227e9a 2021-03-18 op
829 be97d6e6 2021-08-15 op /*
830 be97d6e6 2021-08-15 op * Check if the last time telescope crashed. The check is done by
831 be97d6e6 2021-08-15 op * looking at `crashed_file': if it exists then last time we crashed.
832 be97d6e6 2021-08-15 op * Then, while here, touch the file too. During IMSG_QUIT we'll
833 be97d6e6 2021-08-15 op * remove it.
834 be97d6e6 2021-08-15 op */
835 83ed72f1 2022-01-03 op static int
836 b6171794 2021-07-20 op last_time_crashed(void)
837 b6171794 2021-07-20 op {
838 be97d6e6 2021-08-15 op int fd, crashed = 1;
839 b6171794 2021-07-20 op
840 2b409042 2021-09-15 op if (safe_mode)
841 2b409042 2021-09-15 op return 0;
842 2b409042 2021-09-15 op
843 be97d6e6 2021-08-15 op if (unlink(crashed_file) == -1 && errno == ENOENT)
844 be97d6e6 2021-08-15 op crashed = 0;
845 be97d6e6 2021-08-15 op
846 be97d6e6 2021-08-15 op if ((fd = open(crashed_file, O_CREAT|O_WRONLY, 0600)) == -1)
847 be97d6e6 2021-08-15 op return crashed;
848 b6171794 2021-07-20 op close(fd);
849 be97d6e6 2021-08-15 op
850 be97d6e6 2021-08-15 op return crashed;
851 b6171794 2021-07-20 op }
852 b6171794 2021-07-20 op
853 b6171794 2021-07-20 op int
854 d0fd368a 2021-07-15 op lock_session(void)
855 d0fd368a 2021-07-15 op {
856 d0fd368a 2021-07-15 op struct flock lock;
857 d0fd368a 2021-07-15 op int fd;
858 d0fd368a 2021-07-15 op
859 d0fd368a 2021-07-15 op if ((fd = open(lockfile_path, O_WRONLY|O_CREAT, 0600)) == -1)
860 d0fd368a 2021-07-15 op return -1;
861 d0fd368a 2021-07-15 op
862 d0fd368a 2021-07-15 op lock.l_start = 0;
863 d0fd368a 2021-07-15 op lock.l_len = 0;
864 d0fd368a 2021-07-15 op lock.l_type = F_WRLCK;
865 d0fd368a 2021-07-15 op lock.l_whence = SEEK_SET;
866 c6d03cf5 2021-04-25 op
867 d0fd368a 2021-07-15 op if (fcntl(fd, F_SETLK, &lock) == -1) {
868 d0fd368a 2021-07-15 op close(fd);
869 d0fd368a 2021-07-15 op return -1;
870 d0fd368a 2021-07-15 op }
871 d0fd368a 2021-07-15 op
872 d0fd368a 2021-07-15 op return fd;
873 d0fd368a 2021-07-15 op }
874 d0fd368a 2021-07-15 op
875 6400962b 2022-01-03 op static inline int
876 c6d03cf5 2021-04-25 op parse_khost_line(char *line, char *tmp[3])
877 c6d03cf5 2021-04-25 op {
878 c6d03cf5 2021-04-25 op char **ap;
879 c6d03cf5 2021-04-25 op
880 c6d03cf5 2021-04-25 op for (ap = tmp; ap < &tmp[3] &&
881 c6d03cf5 2021-04-25 op (*ap = strsep(&line, " \t\n")) != NULL;) {
882 c6d03cf5 2021-04-25 op if (**ap != '\0')
883 c6d03cf5 2021-04-25 op ap++;
884 c6d03cf5 2021-04-25 op }
885 3a227e9a 2021-03-18 op
886 c6d03cf5 2021-04-25 op return ap == &tmp[3] && *line == '\0';
887 c6d03cf5 2021-04-25 op }
888 c6d03cf5 2021-04-25 op
889 eb722b50 2022-01-03 op static void
890 eb722b50 2022-01-03 op load_certs(void)
891 3a227e9a 2021-03-18 op {
892 c6d03cf5 2021-04-25 op char *tmp[3], *line = NULL;
893 6cd6a9e1 2021-03-20 op const char *errstr;
894 ec1fa0b0 2021-04-25 op size_t lineno = 0, linesize = 0;
895 3a227e9a 2021-03-18 op ssize_t linelen;
896 3a227e9a 2021-03-18 op FILE *f;
897 eb722b50 2022-01-03 op struct tofu_entry e;
898 3a227e9a 2021-03-18 op
899 3a227e9a 2021-03-18 op if ((f = fopen(known_hosts_file, "r")) == NULL)
900 eb722b50 2022-01-03 op return;
901 3a227e9a 2021-03-18 op
902 3a227e9a 2021-03-18 op while ((linelen = getline(&line, &linesize, f)) != -1) {
903 ec1fa0b0 2021-04-25 op lineno++;
904 3a227e9a 2021-03-18 op
905 eb722b50 2022-01-03 op memset(&e, 0, sizeof(e));
906 eb722b50 2022-01-03 op if (parse_khost_line(line, tmp)) {
907 eb722b50 2022-01-03 op strlcpy(e.domain, tmp[0], sizeof(e.domain));
908 eb722b50 2022-01-03 op strlcpy(e.hash, tmp[1], sizeof(e.hash));
909 3a227e9a 2021-03-18 op
910 eb722b50 2022-01-03 op e.verified = strtonum(tmp[2], 0, 1, &errstr);
911 c6d03cf5 2021-04-25 op if (errstr != NULL)
912 c6d03cf5 2021-04-25 op errx(1, "%s:%zu verification for %s is %s: %s",
913 c6d03cf5 2021-04-25 op known_hosts_file, lineno,
914 eb722b50 2022-01-03 op e.domain, errstr, tmp[2]);
915 eb722b50 2022-01-03 op
916 eb722b50 2022-01-03 op fs_send_ui(IMSG_TOFU, 0, -1, &e, sizeof(e));
917 c6d03cf5 2021-04-25 op } else {
918 ec1fa0b0 2021-04-25 op warnx("%s:%zu invalid entry",
919 ec1fa0b0 2021-04-25 op known_hosts_file, lineno);
920 c6d03cf5 2021-04-25 op }
921 3a227e9a 2021-03-18 op }
922 3a227e9a 2021-03-18 op
923 3a227e9a 2021-03-18 op free(line);
924 eb722b50 2022-01-03 op fclose(f);
925 eb722b50 2022-01-03 op return;
926 3a227e9a 2021-03-18 op }
927 c7107cec 2021-04-01 op