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