commit 3078e1bc2a28ec8a601f3880af32291ca7119434 from: Omar Polo date: Sun Apr 24 11:29:02 2022 UTC merge update_cert and tofu_update into tofu_update_persist commit - 4fa88f57fead5f472c4bbd60c65d70d884ccd6ee commit + 3078e1bc2a28ec8a601f3880af32291ca7119434 blob - cc0587c360ac02f7f6a42d8e81b63c0454deef7f blob + 03f3394a4fa4a1f0d58e5cc0a48bb4152e247c27 --- fs.c +++ fs.c @@ -257,58 +257,7 @@ done: else load_page_from_str(tab, fallback); } - -int -update_cert(const struct tofu_entry *e) -{ - FILE *tmp, *f; - char sfn[PATH_MAX], *line = NULL, *t; - size_t l, linesize = 0; - ssize_t linelen; - int fd, err; - strlcpy(sfn, known_hosts_tmp, sizeof(sfn)); - if ((fd = mkstemp(sfn)) == -1 || - (tmp = fdopen(fd, "w")) == NULL) { - if (fd != -1) { - unlink(sfn); - close(fd); - } - return -1; - } - - if ((f = fopen(known_hosts_file, "r")) == NULL) { - unlink(sfn); - fclose(tmp); - return -1; - } - - l = strlen(e->domain); - while ((linelen = getline(&line, &linesize, f)) != -1) { - if ((t = strstr(line, e->domain)) != NULL && - (line[l] == ' ' || line[l] == '\t')) - continue; - /* line has a trailing \n */ - fprintf(tmp, "%s", line); - } - fprintf(tmp, "%s %s %d\n", e->domain, e->hash, e->verified); - - free(line); - err = ferror(tmp); - - fclose(tmp); - fclose(f); - - if (err) { - unlink(sfn); - return -1; - } - - if (rename(sfn, known_hosts_file)) - return -1; - return 0; -} - static size_t join_path(char *buf, const char *lhs, const char *rhs, size_t buflen) { blob - 3a0e32d0833523ec7274c83695b5a8e6ce62fc12 blob + db32291cf8dce46d1ef193fb319fc6c7a4cd71e7 --- include/fs.h +++ include/fs.h @@ -36,7 +36,6 @@ extern char history_file[PATH_MAX], history_file_tmp[P int fs_init(void); int lock_session(void); void fs_load_url(struct tab *, const char *); -int update_cert(const struct tofu_entry *e); int fs_load_state(struct ohash *); #endif blob - f82e56c6e69ce17b86e314d41b6348f7f3c99087 blob + 800a7f43e7649cf6a81b642cd0bb4108c3f23d21 --- include/telescope.h +++ include/telescope.h @@ -362,6 +362,7 @@ struct tofu_entry *tofu_lookup(struct ohash*, const ch void tofu_add(struct ohash*, struct tofu_entry*); int tofu_save(struct ohash *, struct tofu_entry *); void tofu_update(struct ohash*, struct tofu_entry*); +int tofu_update_persist(struct ohash *, struct tofu_entry *); void tofu_temp_trust(struct ohash *, const char *, const char *, const char *); /* wrap.c */ blob - dee6570ff7ac7ca45ce207b94035af0e8fefdafb blob + c9fe8eb60a4e8b9d201366b70e2687b4dde9a626 --- telescope.c +++ telescope.c @@ -315,8 +315,7 @@ handle_maybe_save_new_cert(int accept, struct tab *tab } strlcpy(e->hash, tab->cert, sizeof(e->hash)); - update_cert(e); - tofu_update(&certs, e); + tofu_update_persist(&certs, e); tab->trust = TS_TRUSTED; blob - 98169515f37cd0899f98bbc088b04a23056051d8 blob + afd390f588ce3fca5237ef0c4d872d81ee9debce --- tofu.c +++ tofu.c @@ -19,6 +19,7 @@ #include #include #include +#include #include "fs.h" #include "telescope.h" @@ -90,6 +91,58 @@ tofu_update(struct ohash *h, struct tofu_entry *e) } } +int +tofu_update_persist(struct ohash *h, struct tofu_entry *e) +{ + FILE *tmp, *fp; + char sfn[PATH_MAX], *line = NULL; + size_t l, linesize = 0; + ssize_t linelen; + int fd, err; + + tofu_update(h, e); + + strlcpy(sfn, known_hosts_tmp, sizeof(sfn)); + if ((fd = mkstemp(sfn)) == -1 || + (tmp = fdopen(fd, "w")) == NULL) { + if (fd != -1) { + unlink(sfn); + close(fd); + } + return -1; + } + + if ((fp = fopen(known_hosts_file, "r")) == NULL) { + unlink(sfn); + fclose(tmp); + return -1; + } + + l = strlen(e->domain); + while ((linelen = getline(&line, &linesize, fp)) != -1) { + if (!strncmp(line, e->domain, l)) + continue; + if (linesize > 0 && line[linesize-1] == '\n') + line[linesize-1] = '\0'; + fprintf(tmp, "%s\n", line); + } + fprintf(tmp, "%s %s %d\n", e->domain, e->hash, e->verified); + + free(line); + err = ferror(tmp); + fclose(tmp); + fclose(fp); + + if (err) { + unlink(sfn); + return -1; + } + + if (rename(sfn, known_hosts_file)) + return -1; + return 0; +} + void tofu_temp_trust(struct ohash *h, const char *host, const char *port, const char *hash)