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