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