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 5a824be4 2021-07-13 op #include "pages.h"
37 f63b8f73 2022-04-24 op #include "parser.h"
38 eb9cbcba 2022-01-03 op #include "session.h"
39 f63b8f73 2022-04-24 op #include "telescope.h"
40 9d65b1d9 2022-01-11 op #include "utils.h"
41 35e1f40a 2021-03-14 op
42 f63b8f73 2022-04-24 op #include "fs.h"
43 f63b8f73 2022-04-24 op
44 f63b8f73 2022-04-24 op #ifndef nitems
45 f63b8f73 2022-04-24 op #define nitems(x) (sizeof(x) / sizeof(x[0]))
46 f63b8f73 2022-04-24 op #endif
47 f63b8f73 2022-04-24 op
48 35e1f40a 2021-03-14 op static void die(void) __attribute__((__noreturn__));
49 5cbe1763 2021-08-13 op static int select_non_dot(const struct dirent *);
50 5cbe1763 2021-08-13 op static int select_non_dotdot(const struct dirent *);
51 eb2ed626 2021-10-07 op static size_t join_path(char*, const char*, const char*, size_t);
52 eb2ed626 2021-10-07 op static void getenv_default(char*, const char*, const char*, size_t);
53 eb2ed626 2021-10-07 op static void mkdirs(const char*, mode_t);
54 6c8ddace 2022-01-03 op static void init_paths(void);
55 50f03682 2022-01-03 op static void load_last_session(void);
56 9e97090d 2022-02-26 op static void load_hist(void);
57 83ed72f1 2022-01-03 op static int last_time_crashed(void);
58 f63b8f73 2022-04-24 op static void load_certs(struct ohash *);
59 35e1f40a 2021-03-14 op
60 f88fbabc 2021-11-27 op /*
61 f88fbabc 2021-11-27 op * Where to store user data. These are all equal to ~/.telescope if
62 f88fbabc 2021-11-27 op * it exists.
63 f88fbabc 2021-11-27 op */
64 fb3d08c1 2021-10-07 op char config_path_base[PATH_MAX];
65 fb3d08c1 2021-10-07 op char data_path_base[PATH_MAX];
66 fb3d08c1 2021-10-07 op char cache_path_base[PATH_MAX];
67 eb2ed626 2021-10-07 op
68 4cf6ba13 2022-02-11 op char ctlsock_path[PATH_MAX];
69 eb2ed626 2021-10-07 op char config_path[PATH_MAX];
70 fb3d08c1 2021-10-07 op char lockfile_path[PATH_MAX];
71 fb3d08c1 2021-10-07 op char bookmark_file[PATH_MAX];
72 fb3d08c1 2021-10-07 op char known_hosts_file[PATH_MAX], known_hosts_tmp[PATH_MAX];
73 fb3d08c1 2021-10-07 op char crashed_file[PATH_MAX];
74 de6a6a40 2022-04-24 op char session_file[PATH_MAX], session_file_tmp[PATH_MAX];
75 de6a6a40 2022-04-24 op char history_file[PATH_MAX], history_file_tmp[PATH_MAX];
76 740f578b 2021-03-15 op
77 35e1f40a 2021-03-14 op static void __attribute__((__noreturn__))
78 35e1f40a 2021-03-14 op die(void)
79 35e1f40a 2021-03-14 op {
80 35e1f40a 2021-03-14 op abort(); /* TODO */
81 35e1f40a 2021-03-14 op }
82 35e1f40a 2021-03-14 op
83 f63b8f73 2022-04-24 op static int
84 f63b8f73 2022-04-24 op select_non_dot(const struct dirent *d)
85 fb4dc49f 2021-08-13 op {
86 f63b8f73 2022-04-24 op return strcmp(d->d_name, ".");
87 f63b8f73 2022-04-24 op }
88 fb4dc49f 2021-08-13 op
89 f63b8f73 2022-04-24 op static int
90 f63b8f73 2022-04-24 op select_non_dotdot(const struct dirent *d)
91 f63b8f73 2022-04-24 op {
92 f63b8f73 2022-04-24 op return strcmp(d->d_name, ".") && strcmp(d->d_name, "..");
93 fb4dc49f 2021-08-13 op }
94 fb4dc49f 2021-08-13 op
95 fb4dc49f 2021-08-13 op static void
96 f63b8f73 2022-04-24 op send_dir(struct tab *tab, const char *path)
97 35e1f40a 2021-03-14 op {
98 f63b8f73 2022-04-24 op struct dirent **names;
99 f63b8f73 2022-04-24 op int (*selector)(const struct dirent *) = select_non_dot;
100 f63b8f73 2022-04-24 op int i, len;
101 35e1f40a 2021-03-14 op
102 f63b8f73 2022-04-24 op #if notyet
103 f63b8f73 2022-04-24 op /*
104 f63b8f73 2022-04-24 op * need something to fake a redirect
105 f63b8f73 2022-04-24 op */
106 35e1f40a 2021-03-14 op
107 f63b8f73 2022-04-24 op if (!has_suffix(path, "/")) {
108 f63b8f73 2022-04-24 op if (asprintf(&s, "%s/", path) == -1)
109 f63b8f73 2022-04-24 op die();
110 f63b8f73 2022-04-24 op send_hdr(peerid, 30, s);
111 f63b8f73 2022-04-24 op free(s);
112 f63b8f73 2022-04-24 op return;
113 56e7efb4 2021-07-21 op }
114 f63b8f73 2022-04-24 op #endif
115 56e7efb4 2021-07-21 op
116 f63b8f73 2022-04-24 op if (!strcmp(path, "/"))
117 f63b8f73 2022-04-24 op selector = select_non_dotdot;
118 f63b8f73 2022-04-24 op
119 f63b8f73 2022-04-24 op if ((len = scandir(path, &names, selector, alphasort)) == -1) {
120 f63b8f73 2022-04-24 op load_page_from_str(tab, "# failure reading the directory\n");
121 a2728733 2021-07-18 op return;
122 35e1f40a 2021-03-14 op }
123 a2728733 2021-07-18 op
124 f63b8f73 2022-04-24 op parser_init(tab, gemtext_initparser);
125 f63b8f73 2022-04-24 op parser_parsef(tab, "# Index of %s\n\n", path);
126 56e7efb4 2021-07-21 op
127 f63b8f73 2022-04-24 op for (i = 0; i < len; ++i) {
128 f63b8f73 2022-04-24 op const char *sufx = "";
129 fb4dc49f 2021-08-13 op
130 f63b8f73 2022-04-24 op if (names[i]->d_type == DT_DIR)
131 f63b8f73 2022-04-24 op sufx = "/";
132 f63b8f73 2022-04-24 op
133 f63b8f73 2022-04-24 op parser_parsef(tab, "=> %s%s\n", names[i]->d_name, sufx);
134 f63b8f73 2022-04-24 op }
135 f63b8f73 2022-04-24 op
136 f63b8f73 2022-04-24 op parser_free(tab);
137 f63b8f73 2022-04-24 op free(names);
138 fb4dc49f 2021-08-13 op }
139 fb4dc49f 2021-08-13 op
140 f63b8f73 2022-04-24 op static int
141 f63b8f73 2022-04-24 op is_dir(FILE *fp)
142 24a68158 2021-08-13 op {
143 f63b8f73 2022-04-24 op struct stat sb;
144 24a68158 2021-08-13 op
145 f63b8f73 2022-04-24 op if (fstat(fileno(fp), &sb) == -1)
146 f63b8f73 2022-04-24 op return 0;
147 24a68158 2021-08-13 op
148 f63b8f73 2022-04-24 op return S_ISDIR(sb.st_mode);
149 24a68158 2021-08-13 op }
150 24a68158 2021-08-13 op
151 f63b8f73 2022-04-24 op static parserinit
152 65124267 2021-08-13 op file_type(const char *path)
153 fb4dc49f 2021-08-13 op {
154 21268f10 2022-04-24 op const struct mapping {
155 fb4dc49f 2021-08-13 op const char *ext;
156 f63b8f73 2022-04-24 op parserinit fn;
157 fb4dc49f 2021-08-13 op } ms[] = {
158 f63b8f73 2022-04-24 op {"diff", textpatch_initparser},
159 f63b8f73 2022-04-24 op {"gemini", gemtext_initparser},
160 f63b8f73 2022-04-24 op {"gmi", gemtext_initparser},
161 f63b8f73 2022-04-24 op {"markdown", textplain_initparser},
162 f63b8f73 2022-04-24 op {"md", textplain_initparser},
163 f63b8f73 2022-04-24 op {"patch", gemtext_initparser},
164 fb4dc49f 2021-08-13 op {NULL, NULL},
165 65124267 2021-08-13 op }, *m;
166 21268f10 2022-04-24 op const char *dot;
167 65124267 2021-08-13 op
168 65124267 2021-08-13 op if ((dot = strrchr(path, '.')) == NULL)
169 f63b8f73 2022-04-24 op return textplain_initparser;
170 65124267 2021-08-13 op
171 65124267 2021-08-13 op dot++;
172 65124267 2021-08-13 op
173 65124267 2021-08-13 op for (m = ms; m->ext != NULL; ++m)
174 65124267 2021-08-13 op if (!strcmp(m->ext, dot))
175 f63b8f73 2022-04-24 op return m->fn;
176 65124267 2021-08-13 op
177 f63b8f73 2022-04-24 op return textplain_initparser;
178 5cbe1763 2021-08-13 op }
179 5cbe1763 2021-08-13 op
180 f63b8f73 2022-04-24 op void
181 f63b8f73 2022-04-24 op fs_load_url(struct tab *tab, const char *url)
182 5cbe1763 2021-08-13 op {
183 f63b8f73 2022-04-24 op const char *bpath = "bookmarks.gmi", *fallback = "# Not found\n";
184 f63b8f73 2022-04-24 op parserinit initfn = gemtext_initparser;
185 f63b8f73 2022-04-24 op char path[PATH_MAX];
186 f63b8f73 2022-04-24 op FILE *fp = NULL;
187 f63b8f73 2022-04-24 op size_t i;
188 f63b8f73 2022-04-24 op char buf[BUFSIZ];
189 f63b8f73 2022-04-24 op struct page {
190 f63b8f73 2022-04-24 op const char *name;
191 f63b8f73 2022-04-24 op const char *path;
192 f63b8f73 2022-04-24 op const uint8_t *data;
193 f63b8f73 2022-04-24 op size_t len;
194 f63b8f73 2022-04-24 op } pages[] = {
195 f63b8f73 2022-04-24 op {"about", NULL, about_about, about_about_len},
196 f63b8f73 2022-04-24 op {"blank", NULL, about_blank, about_blank_len},
197 f63b8f73 2022-04-24 op {"bookmarks", bpath, bookmarks, bookmarks_len},
198 f63b8f73 2022-04-24 op {"crash", NULL, about_crash, about_crash_len},
199 f63b8f73 2022-04-24 op {"help", NULL, about_help, about_help_len},
200 f63b8f73 2022-04-24 op {"license", NULL, about_license, about_license_len},
201 f63b8f73 2022-04-24 op {"new", NULL, about_new, about_new_len},
202 f63b8f73 2022-04-24 op }, *page = NULL;
203 65124267 2021-08-13 op
204 f63b8f73 2022-04-24 op if (!strncmp(url, "about:", 6)) {
205 f63b8f73 2022-04-24 op url += 6;
206 65124267 2021-08-13 op
207 f63b8f73 2022-04-24 op for (i = 0; page == NULL && i < nitems(pages); ++i) {
208 f63b8f73 2022-04-24 op if (!strcmp(url, pages[i].name))
209 f63b8f73 2022-04-24 op page = &pages[i];
210 f63b8f73 2022-04-24 op }
211 5d43215b 2021-08-13 op
212 f63b8f73 2022-04-24 op if (page == NULL)
213 f63b8f73 2022-04-24 op goto done;
214 5cbe1763 2021-08-13 op
215 f63b8f73 2022-04-24 op strlcpy(path, data_path_base, sizeof(path));
216 f63b8f73 2022-04-24 op strlcat(path, "/", sizeof(path));
217 f63b8f73 2022-04-24 op if (page->path != NULL)
218 f63b8f73 2022-04-24 op strlcat(path, page->path, sizeof(path));
219 f63b8f73 2022-04-24 op else {
220 f63b8f73 2022-04-24 op strlcat(path, "page/about_", sizeof(path));
221 f63b8f73 2022-04-24 op strlcat(path, page->name, sizeof(path));
222 f63b8f73 2022-04-24 op strlcat(path, ".gmi", sizeof(path));
223 f63b8f73 2022-04-24 op }
224 65124267 2021-08-13 op
225 f63b8f73 2022-04-24 op fallback = page->data;
226 f63b8f73 2022-04-24 op } else if (!strncmp(url, "file://", 7)) {
227 f63b8f73 2022-04-24 op url += 7;
228 f63b8f73 2022-04-24 op strlcpy(path, url, sizeof(path));
229 f63b8f73 2022-04-24 op initfn = file_type(url);
230 f63b8f73 2022-04-24 op } else
231 f63b8f73 2022-04-24 op goto done;
232 65124267 2021-08-13 op
233 f63b8f73 2022-04-24 op if ((fp = fopen(path, "r")) == NULL)
234 f63b8f73 2022-04-24 op goto done;
235 65124267 2021-08-13 op
236 f63b8f73 2022-04-24 op if (is_dir(fp)) {
237 f63b8f73 2022-04-24 op fclose(fp);
238 f63b8f73 2022-04-24 op send_dir(tab, path);
239 f63b8f73 2022-04-24 op goto done;
240 fb4dc49f 2021-08-13 op }
241 fb4dc49f 2021-08-13 op
242 f63b8f73 2022-04-24 op parser_init(tab, initfn);
243 f63b8f73 2022-04-24 op for (;;) {
244 f63b8f73 2022-04-24 op size_t r;
245 fb4dc49f 2021-08-13 op
246 f63b8f73 2022-04-24 op r = fread(buf, 1, sizeof(buf), fp);
247 f63b8f73 2022-04-24 op if (!parser_parse(tab, buf, r))
248 f63b8f73 2022-04-24 op break;
249 f63b8f73 2022-04-24 op if (r != sizeof(buf))
250 f63b8f73 2022-04-24 op break;
251 fb4dc49f 2021-08-13 op }
252 f63b8f73 2022-04-24 op parser_free(tab);
253 fb4dc49f 2021-08-13 op
254 f63b8f73 2022-04-24 op done:
255 f63b8f73 2022-04-24 op if (fp != NULL)
256 f63b8f73 2022-04-24 op fclose(fp);
257 f63b8f73 2022-04-24 op else
258 f63b8f73 2022-04-24 op load_page_from_str(tab, fallback);
259 740f578b 2021-03-15 op }
260 288fd238 2021-04-25 op
261 eb2ed626 2021-10-07 op static size_t
262 eb2ed626 2021-10-07 op join_path(char *buf, const char *lhs, const char *rhs, size_t buflen)
263 35e1f40a 2021-03-14 op {
264 eb2ed626 2021-10-07 op strlcpy(buf, lhs, buflen);
265 eb2ed626 2021-10-07 op return strlcat(buf, rhs, buflen);
266 eb2ed626 2021-10-07 op }
267 d0fd368a 2021-07-15 op
268 eb2ed626 2021-10-07 op static void
269 eb2ed626 2021-10-07 op getenv_default(char *buf, const char *name, const char *def, size_t buflen)
270 eb2ed626 2021-10-07 op {
271 eb2ed626 2021-10-07 op size_t ret;
272 eb2ed626 2021-10-07 op char *home, *env;
273 740f578b 2021-03-15 op
274 eb2ed626 2021-10-07 op if ((home = getenv("HOME")) == NULL)
275 eb2ed626 2021-10-07 op errx(1, "HOME is not defined");
276 3a227e9a 2021-03-18 op
277 eb2ed626 2021-10-07 op if ((env = getenv(name)) != NULL)
278 eb2ed626 2021-10-07 op ret = strlcpy(buf, env, buflen);
279 eb2ed626 2021-10-07 op else
280 eb2ed626 2021-10-07 op ret = join_path(buf, home, def, buflen);
281 288fd238 2021-04-25 op
282 eb2ed626 2021-10-07 op if (ret >= buflen)
283 eb2ed626 2021-10-07 op errx(1, "buffer too small for %s", name);
284 eb2ed626 2021-10-07 op }
285 c7107cec 2021-04-01 op
286 eb2ed626 2021-10-07 op static void
287 eb2ed626 2021-10-07 op mkdirs(const char *path, mode_t mode)
288 eb2ed626 2021-10-07 op {
289 444dad86 2021-10-07 op char copy[PATH_MAX+1], orig[PATH_MAX+1], *parent;
290 ea080950 2021-07-20 op
291 eb2ed626 2021-10-07 op strlcpy(copy, path, sizeof(copy));
292 444dad86 2021-10-07 op strlcpy(orig, path, sizeof(orig));
293 eb2ed626 2021-10-07 op parent = dirname(copy);
294 eb2ed626 2021-10-07 op if (!strcmp(parent, "/"))
295 eb2ed626 2021-10-07 op return;
296 eb2ed626 2021-10-07 op mkdirs(parent, mode);
297 eb2ed626 2021-10-07 op
298 444dad86 2021-10-07 op if (mkdir(orig, mode) != 0) {
299 eb2ed626 2021-10-07 op if (errno == EEXIST)
300 eb2ed626 2021-10-07 op return;
301 444dad86 2021-10-07 op err(1, "can't mkdir %s", orig);
302 eb2ed626 2021-10-07 op }
303 eb2ed626 2021-10-07 op }
304 eb2ed626 2021-10-07 op
305 eb2ed626 2021-10-07 op static void
306 6c8ddace 2022-01-03 op init_paths(void)
307 eb2ed626 2021-10-07 op {
308 7e60a21a 2022-01-03 op char xdg_config_base[PATH_MAX];
309 7e60a21a 2022-01-03 op char xdg_data_base[PATH_MAX];
310 7e60a21a 2022-01-03 op char xdg_cache_base[PATH_MAX];
311 7e60a21a 2022-01-03 op char old_path[PATH_MAX];
312 7e60a21a 2022-01-03 op char *home;
313 7e60a21a 2022-01-03 op struct stat info;
314 eb2ed626 2021-10-07 op
315 eb2ed626 2021-10-07 op /* old path */
316 eb2ed626 2021-10-07 op if ((home = getenv("HOME")) == NULL)
317 eb2ed626 2021-10-07 op errx(1, "HOME is not defined");
318 eb2ed626 2021-10-07 op join_path(old_path, home, "/.telescope", sizeof(old_path));
319 eb2ed626 2021-10-07 op
320 eb2ed626 2021-10-07 op /* if ~/.telescope exists, use that instead of xdg dirs */
321 eb2ed626 2021-10-07 op if (stat(old_path, &info) == 0 && S_ISDIR(info.st_mode)) {
322 eb2ed626 2021-10-07 op join_path(config_path_base, home, "/.telescope",
323 eb2ed626 2021-10-07 op sizeof(config_path_base));
324 eb2ed626 2021-10-07 op join_path(data_path_base, home, "/.telescope",
325 eb2ed626 2021-10-07 op sizeof(data_path_base));
326 eb2ed626 2021-10-07 op join_path(cache_path_base, home, "/.telescope",
327 eb2ed626 2021-10-07 op sizeof(cache_path_base));
328 eb2ed626 2021-10-07 op return;
329 eb2ed626 2021-10-07 op }
330 eb2ed626 2021-10-07 op
331 eb2ed626 2021-10-07 op /* xdg paths */
332 eb2ed626 2021-10-07 op getenv_default(xdg_config_base, "XDG_CONFIG_HOME", "/.config",
333 eb2ed626 2021-10-07 op sizeof(xdg_config_base));
334 eb2ed626 2021-10-07 op getenv_default(xdg_data_base, "XDG_DATA_HOME", "/.local/share",
335 eb2ed626 2021-10-07 op sizeof(xdg_data_base));
336 eb2ed626 2021-10-07 op getenv_default(xdg_cache_base, "XDG_CACHE_HOME", "/.cache",
337 eb2ed626 2021-10-07 op sizeof(xdg_cache_base));
338 eb2ed626 2021-10-07 op
339 eb2ed626 2021-10-07 op join_path(config_path_base, xdg_config_base, "/telescope",
340 eb2ed626 2021-10-07 op sizeof(config_path_base));
341 eb2ed626 2021-10-07 op join_path(data_path_base, xdg_data_base, "/telescope",
342 eb2ed626 2021-10-07 op sizeof(data_path_base));
343 eb2ed626 2021-10-07 op join_path(cache_path_base, xdg_cache_base, "/telescope",
344 eb2ed626 2021-10-07 op sizeof(cache_path_base));
345 eb2ed626 2021-10-07 op
346 eb2ed626 2021-10-07 op mkdirs(xdg_config_base, S_IRWXU);
347 eb2ed626 2021-10-07 op mkdirs(xdg_data_base, S_IRWXU);
348 eb2ed626 2021-10-07 op mkdirs(xdg_cache_base, S_IRWXU);
349 eb2ed626 2021-10-07 op
350 eb2ed626 2021-10-07 op mkdirs(config_path_base, S_IRWXU);
351 eb2ed626 2021-10-07 op mkdirs(data_path_base, S_IRWXU);
352 eb2ed626 2021-10-07 op mkdirs(cache_path_base, S_IRWXU);
353 eb2ed626 2021-10-07 op }
354 eb2ed626 2021-10-07 op
355 eb2ed626 2021-10-07 op int
356 eb2ed626 2021-10-07 op fs_init(void)
357 eb2ed626 2021-10-07 op {
358 6c8ddace 2022-01-03 op init_paths();
359 eb2ed626 2021-10-07 op
360 4cf6ba13 2022-02-11 op join_path(ctlsock_path, cache_path_base, "/ctl",
361 4cf6ba13 2022-02-11 op sizeof(ctlsock_path));
362 eb2ed626 2021-10-07 op join_path(config_path, config_path_base, "/config",
363 eb2ed626 2021-10-07 op sizeof(config_path));
364 eb2ed626 2021-10-07 op join_path(lockfile_path, cache_path_base, "/lock",
365 eb2ed626 2021-10-07 op sizeof(lockfile_path));
366 eb2ed626 2021-10-07 op join_path(bookmark_file, data_path_base, "/bookmarks.gmi",
367 eb2ed626 2021-10-07 op sizeof(bookmark_file));
368 eb2ed626 2021-10-07 op join_path(known_hosts_file, data_path_base, "/known_hosts",
369 eb2ed626 2021-10-07 op sizeof(known_hosts_file));
370 eb2ed626 2021-10-07 op join_path(known_hosts_tmp, cache_path_base,
371 eb2ed626 2021-10-07 op "/known_hosts.tmp.XXXXXXXXXX", sizeof(known_hosts_tmp));
372 eb2ed626 2021-10-07 op join_path(session_file, cache_path_base, "/session",
373 de6a6a40 2022-04-24 op sizeof(session_file));
374 de6a6a40 2022-04-24 op join_path(session_file_tmp, cache_path_base, "/session.XXXXXXXXXX",
375 eb2ed626 2021-10-07 op sizeof(session_file));
376 9e97090d 2022-02-26 op join_path(history_file, cache_path_base, "/history",
377 de6a6a40 2022-04-24 op sizeof(history_file));
378 de6a6a40 2022-04-24 op join_path(history_file_tmp, cache_path_base, "/history.XXXXXXXXXX",
379 9e97090d 2022-02-26 op sizeof(history_file));
380 eb2ed626 2021-10-07 op join_path(crashed_file, cache_path_base, "/crashed",
381 eb2ed626 2021-10-07 op sizeof(crashed_file));
382 eb2ed626 2021-10-07 op
383 3a227e9a 2021-03-18 op return 1;
384 3a227e9a 2021-03-18 op }
385 bb28f1c2 2021-12-30 op
386 bb28f1c2 2021-12-30 op /*
387 f63b8f73 2022-04-24 op * Parse a line of the session file and restores it. The format is:
388 bb28f1c2 2021-12-30 op *
389 bb28f1c2 2021-12-30 op * URL [flags,...] [title]\n
390 bb28f1c2 2021-12-30 op */
391 f63b8f73 2022-04-24 op static inline struct tab *
392 f63b8f73 2022-04-24 op parse_session_line(char *line, struct tab **ct)
393 bb28f1c2 2021-12-30 op {
394 f63b8f73 2022-04-24 op struct tab *tab;
395 bb28f1c2 2021-12-30 op char *s, *t, *ap;
396 f63b8f73 2022-04-24 op const char *uri, *title = "";
397 f63b8f73 2022-04-24 op int current = 0, killed = 0;
398 f63b8f73 2022-04-24 op size_t top_line = 0, current_line = 0;
399 bb28f1c2 2021-12-30 op
400 f63b8f73 2022-04-24 op uri = line;
401 bb28f1c2 2021-12-30 op if ((s = strchr(line, ' ')) == NULL)
402 f63b8f73 2022-04-24 op return NULL;
403 3a227e9a 2021-03-18 op
404 bb28f1c2 2021-12-30 op *s++ = '\0';
405 bb28f1c2 2021-12-30 op
406 bb28f1c2 2021-12-30 op if ((t = strchr(s, ' ')) != NULL) {
407 bb28f1c2 2021-12-30 op *t++ = '\0';
408 f63b8f73 2022-04-24 op title = t;
409 bb28f1c2 2021-12-30 op }
410 bb28f1c2 2021-12-30 op
411 bb28f1c2 2021-12-30 op while ((ap = strsep(&s, ",")) != NULL) {
412 bb28f1c2 2021-12-30 op if (!strcmp(ap, "current"))
413 f63b8f73 2022-04-24 op current = 1;
414 6c74799d 2022-01-05 op else if (!strcmp(ap, "killed"))
415 f63b8f73 2022-04-24 op killed = 1;
416 e795e935 2022-01-18 op else if (has_prefix(ap, "top="))
417 f63b8f73 2022-04-24 op top_line = strtonum(ap+4, 0, UINT32_MAX, NULL);
418 e795e935 2022-01-18 op else if (has_prefix(ap, "cur="))
419 f63b8f73 2022-04-24 op current_line = strtonum(ap+4, 0, UINT32_MAX, NULL);
420 bb28f1c2 2021-12-30 op }
421 bb28f1c2 2021-12-30 op
422 f63b8f73 2022-04-24 op if (top_line > current_line) {
423 f63b8f73 2022-04-24 op top_line = 0;
424 f63b8f73 2022-04-24 op current_line = 0;
425 e795e935 2022-01-18 op }
426 e795e935 2022-01-18 op
427 f63b8f73 2022-04-24 op if ((tab = new_tab(uri, NULL, NULL)) == NULL)
428 f63b8f73 2022-04-24 op die();
429 f63b8f73 2022-04-24 op tab->hist_cur->line_off = top_line;
430 f63b8f73 2022-04-24 op tab->hist_cur->current_off = current_line;
431 f63b8f73 2022-04-24 op strlcpy(tab->buffer.page.title, title, sizeof(tab->buffer.page.title));
432 f63b8f73 2022-04-24 op
433 f63b8f73 2022-04-24 op if (current)
434 f63b8f73 2022-04-24 op *ct = tab;
435 f63b8f73 2022-04-24 op else if (killed)
436 f63b8f73 2022-04-24 op kill_tab(tab, 1);
437 f63b8f73 2022-04-24 op
438 f63b8f73 2022-04-24 op return tab;
439 1040cc7f 2021-01-02 op }
440 1040cc7f 2021-01-02 op
441 1040cc7f 2021-01-02 op static inline void
442 f63b8f73 2022-04-24 op sendhist(struct tab *tab, const char *uri, int future)
443 1040cc7f 2021-01-02 op {
444 f63b8f73 2022-04-24 op struct hist *h;
445 1040cc7f 2021-01-02 op
446 f63b8f73 2022-04-24 op if ((h = calloc(1, sizeof(*h))) == NULL)
447 f63b8f73 2022-04-24 op die();
448 f63b8f73 2022-04-24 op strlcpy(h->h, uri, sizeof(h->h));
449 1040cc7f 2021-01-02 op
450 f63b8f73 2022-04-24 op if (future)
451 f63b8f73 2022-04-24 op hist_push(&tab->hist, h);
452 f63b8f73 2022-04-24 op else
453 f63b8f73 2022-04-24 op hist_add_before(&tab->hist, tab->hist_cur, h);
454 bb28f1c2 2021-12-30 op }
455 bb28f1c2 2021-12-30 op
456 bb28f1c2 2021-12-30 op static void
457 50f03682 2022-01-03 op load_last_session(void)
458 bb28f1c2 2021-12-30 op {
459 f63b8f73 2022-04-24 op struct tab *tab = NULL, *ct = NULL;
460 bb28f1c2 2021-12-30 op FILE *session;
461 bb28f1c2 2021-12-30 op size_t linesize = 0;
462 bb28f1c2 2021-12-30 op ssize_t linelen;
463 1040cc7f 2021-01-02 op int future;
464 1040cc7f 2021-01-02 op char *nl, *s, *line = NULL;
465 bb28f1c2 2021-12-30 op
466 bb28f1c2 2021-12-30 op if ((session = fopen(session_file, "r")) == NULL) {
467 f63b8f73 2022-04-24 op new_tab("about:new", NULL, NULL);
468 f63b8f73 2022-04-24 op switch_to_tab(new_tab("about:help", NULL, NULL));
469 f63b8f73 2022-04-24 op return;
470 bb28f1c2 2021-12-30 op }
471 bb28f1c2 2021-12-30 op
472 bb28f1c2 2021-12-30 op while ((linelen = getline(&line, &linesize, session)) != -1) {
473 bb28f1c2 2021-12-30 op if ((nl = strchr(line, '\n')) != NULL)
474 bb28f1c2 2021-12-30 op *nl = '\0';
475 1040cc7f 2021-01-02 op
476 1040cc7f 2021-01-02 op if (*line == '<' || *line == '>') {
477 1040cc7f 2021-01-02 op future = *line == '>';
478 1040cc7f 2021-01-02 op s = line+1;
479 f63b8f73 2022-04-24 op if (*s != ' ' || tab == NULL)
480 1040cc7f 2021-01-02 op continue;
481 f63b8f73 2022-04-24 op sendhist(tab, ++s, future);
482 1040cc7f 2021-01-02 op } else {
483 f63b8f73 2022-04-24 op tab = parse_session_line(line, &ct);
484 1040cc7f 2021-01-02 op }
485 bb28f1c2 2021-12-30 op }
486 bb28f1c2 2021-12-30 op
487 bb28f1c2 2021-12-30 op fclose(session);
488 bb28f1c2 2021-12-30 op free(line);
489 bb28f1c2 2021-12-30 op
490 f63b8f73 2022-04-24 op if (ct != NULL)
491 f63b8f73 2022-04-24 op switch_to_tab(ct);
492 bb28f1c2 2021-12-30 op
493 f63b8f73 2022-04-24 op if (last_time_crashed())
494 f63b8f73 2022-04-24 op switch_to_tab(new_tab("about:crash", NULL, NULL));
495 bb28f1c2 2021-12-30 op }
496 bb28f1c2 2021-12-30 op
497 9e97090d 2022-02-26 op static void
498 9e97090d 2022-02-26 op load_hist(void)
499 9e97090d 2022-02-26 op {
500 9e97090d 2022-02-26 op FILE *hist;
501 9e97090d 2022-02-26 op size_t linesize = 0;
502 9e97090d 2022-02-26 op ssize_t linelen;
503 9e97090d 2022-02-26 op char *nl, *spc, *line = NULL;
504 9e97090d 2022-02-26 op const char *errstr;
505 9e97090d 2022-02-26 op struct histitem hi;
506 9e97090d 2022-02-26 op
507 9e97090d 2022-02-26 op if ((hist = fopen(history_file, "r")) == NULL)
508 f63b8f73 2022-04-24 op return;
509 9e97090d 2022-02-26 op
510 9e97090d 2022-02-26 op while ((linelen = getline(&line, &linesize, hist)) != -1) {
511 9e97090d 2022-02-26 op if ((nl = strchr(line, '\n')) != NULL)
512 9e97090d 2022-02-26 op *nl = '\0';
513 9e97090d 2022-02-26 op if ((spc = strchr(line, ' ')) == NULL)
514 9e97090d 2022-02-26 op continue;
515 9e97090d 2022-02-26 op *spc = '\0';
516 9e97090d 2022-02-26 op spc++;
517 9e97090d 2022-02-26 op
518 9e97090d 2022-02-26 op memset(&hi, 0, sizeof(hi));
519 9e97090d 2022-02-26 op hi.ts = strtonum(line, INT64_MIN, INT64_MAX, &errstr);
520 9e97090d 2022-02-26 op if (errstr != NULL)
521 9e97090d 2022-02-26 op continue;
522 9e97090d 2022-02-26 op if (strlcpy(hi.uri, spc, sizeof(hi.uri)) >= sizeof(hi.uri))
523 9e97090d 2022-02-26 op continue;
524 9e97090d 2022-02-26 op
525 f63b8f73 2022-04-24 op history_push(&hi);
526 9e97090d 2022-02-26 op }
527 9e97090d 2022-02-26 op
528 9e97090d 2022-02-26 op fclose(hist);
529 9e97090d 2022-02-26 op free(line);
530 f63b8f73 2022-04-24 op
531 f63b8f73 2022-04-24 op history_sort();
532 9e97090d 2022-02-26 op }
533 9e97090d 2022-02-26 op
534 3a227e9a 2021-03-18 op int
535 f63b8f73 2022-04-24 op fs_load_state(struct ohash *certs)
536 3a227e9a 2021-03-18 op {
537 f63b8f73 2022-04-24 op load_certs(certs);
538 f63b8f73 2022-04-24 op load_hist();
539 f63b8f73 2022-04-24 op load_last_session();
540 35e1f40a 2021-03-14 op return 0;
541 35e1f40a 2021-03-14 op }
542 3a227e9a 2021-03-18 op
543 be97d6e6 2021-08-15 op /*
544 be97d6e6 2021-08-15 op * Check if the last time telescope crashed. The check is done by
545 be97d6e6 2021-08-15 op * looking at `crashed_file': if it exists then last time we crashed.
546 be97d6e6 2021-08-15 op * Then, while here, touch the file too. During IMSG_QUIT we'll
547 be97d6e6 2021-08-15 op * remove it.
548 be97d6e6 2021-08-15 op */
549 83ed72f1 2022-01-03 op static int
550 b6171794 2021-07-20 op last_time_crashed(void)
551 b6171794 2021-07-20 op {
552 be97d6e6 2021-08-15 op int fd, crashed = 1;
553 b6171794 2021-07-20 op
554 2b409042 2021-09-15 op if (safe_mode)
555 2b409042 2021-09-15 op return 0;
556 2b409042 2021-09-15 op
557 be97d6e6 2021-08-15 op if (unlink(crashed_file) == -1 && errno == ENOENT)
558 be97d6e6 2021-08-15 op crashed = 0;
559 be97d6e6 2021-08-15 op
560 be97d6e6 2021-08-15 op if ((fd = open(crashed_file, O_CREAT|O_WRONLY, 0600)) == -1)
561 be97d6e6 2021-08-15 op return crashed;
562 b6171794 2021-07-20 op close(fd);
563 be97d6e6 2021-08-15 op
564 be97d6e6 2021-08-15 op return crashed;
565 b6171794 2021-07-20 op }
566 b6171794 2021-07-20 op
567 b6171794 2021-07-20 op int
568 d0fd368a 2021-07-15 op lock_session(void)
569 d0fd368a 2021-07-15 op {
570 d0fd368a 2021-07-15 op struct flock lock;
571 d0fd368a 2021-07-15 op int fd;
572 d0fd368a 2021-07-15 op
573 d0fd368a 2021-07-15 op if ((fd = open(lockfile_path, O_WRONLY|O_CREAT, 0600)) == -1)
574 d0fd368a 2021-07-15 op return -1;
575 d0fd368a 2021-07-15 op
576 d0fd368a 2021-07-15 op lock.l_start = 0;
577 d0fd368a 2021-07-15 op lock.l_len = 0;
578 d0fd368a 2021-07-15 op lock.l_type = F_WRLCK;
579 d0fd368a 2021-07-15 op lock.l_whence = SEEK_SET;
580 c6d03cf5 2021-04-25 op
581 d0fd368a 2021-07-15 op if (fcntl(fd, F_SETLK, &lock) == -1) {
582 d0fd368a 2021-07-15 op close(fd);
583 d0fd368a 2021-07-15 op return -1;
584 d0fd368a 2021-07-15 op }
585 d0fd368a 2021-07-15 op
586 d0fd368a 2021-07-15 op return fd;
587 d0fd368a 2021-07-15 op }
588 d0fd368a 2021-07-15 op
589 6400962b 2022-01-03 op static inline int
590 c6d03cf5 2021-04-25 op parse_khost_line(char *line, char *tmp[3])
591 c6d03cf5 2021-04-25 op {
592 c6d03cf5 2021-04-25 op char **ap;
593 c6d03cf5 2021-04-25 op
594 c6d03cf5 2021-04-25 op for (ap = tmp; ap < &tmp[3] &&
595 c6d03cf5 2021-04-25 op (*ap = strsep(&line, " \t\n")) != NULL;) {
596 c6d03cf5 2021-04-25 op if (**ap != '\0')
597 c6d03cf5 2021-04-25 op ap++;
598 c6d03cf5 2021-04-25 op }
599 3a227e9a 2021-03-18 op
600 c6d03cf5 2021-04-25 op return ap == &tmp[3] && *line == '\0';
601 c6d03cf5 2021-04-25 op }
602 c6d03cf5 2021-04-25 op
603 eb722b50 2022-01-03 op static void
604 f63b8f73 2022-04-24 op load_certs(struct ohash *certs)
605 3a227e9a 2021-03-18 op {
606 c6d03cf5 2021-04-25 op char *tmp[3], *line = NULL;
607 6cd6a9e1 2021-03-20 op const char *errstr;
608 ec1fa0b0 2021-04-25 op size_t lineno = 0, linesize = 0;
609 3a227e9a 2021-03-18 op ssize_t linelen;
610 3a227e9a 2021-03-18 op FILE *f;
611 f63b8f73 2022-04-24 op struct tofu_entry *e;
612 3a227e9a 2021-03-18 op
613 3a227e9a 2021-03-18 op if ((f = fopen(known_hosts_file, "r")) == NULL)
614 eb722b50 2022-01-03 op return;
615 3a227e9a 2021-03-18 op
616 f63b8f73 2022-04-24 op if ((e = calloc(1, sizeof(*e))) == NULL) {
617 f63b8f73 2022-04-24 op fclose(f);
618 f63b8f73 2022-04-24 op return;
619 f63b8f73 2022-04-24 op }
620 f63b8f73 2022-04-24 op
621 3a227e9a 2021-03-18 op while ((linelen = getline(&line, &linesize, f)) != -1) {
622 ec1fa0b0 2021-04-25 op lineno++;
623 3a227e9a 2021-03-18 op
624 eb722b50 2022-01-03 op if (parse_khost_line(line, tmp)) {
625 f63b8f73 2022-04-24 op strlcpy(e->domain, tmp[0], sizeof(e->domain));
626 f63b8f73 2022-04-24 op strlcpy(e->hash, tmp[1], sizeof(e->hash));
627 3a227e9a 2021-03-18 op
628 f63b8f73 2022-04-24 op e->verified = strtonum(tmp[2], 0, 1, &errstr);
629 c6d03cf5 2021-04-25 op if (errstr != NULL)
630 c6d03cf5 2021-04-25 op errx(1, "%s:%zu verification for %s is %s: %s",
631 c6d03cf5 2021-04-25 op known_hosts_file, lineno,
632 f63b8f73 2022-04-24 op e->domain, errstr, tmp[2]);
633 eb722b50 2022-01-03 op
634 f63b8f73 2022-04-24 op tofu_add(certs, e);
635 c6d03cf5 2021-04-25 op } else {
636 ec1fa0b0 2021-04-25 op warnx("%s:%zu invalid entry",
637 ec1fa0b0 2021-04-25 op known_hosts_file, lineno);
638 c6d03cf5 2021-04-25 op }
639 3a227e9a 2021-03-18 op }
640 3a227e9a 2021-03-18 op
641 3a227e9a 2021-03-18 op free(line);
642 eb722b50 2022-01-03 op fclose(f);
643 eb722b50 2022-01-03 op return;
644 3a227e9a 2021-03-18 op }