int add_host_to_hostfile(const char *filename, const char *host, const Key *key, int store_hash) { FILE *f; int success = 0; char *hashed_host = NULL; if (key == NULL) return 1; /* XXX ? */ f = fopen(filename, "a"); if (!f) return 0; if (store_hash) { if ((hashed_host = host_hash(host, NULL, 0)) == NULL) { error("add_host_to_hostfile: host_hash failed"); fclose(f); return 0; } } fprintf(f, "%s ", store_hash ? hashed_host : host); if (key_write(key, f)) { success = 1; } else { error("add_host_to_hostfile: saving key in %s failed", filename); } fprintf(f, "\n"); fclose(f); return success; }
static int write_host_entry(FILE *f, const char *host, const char *ip, const struct sshkey *key, int store_hash) { int r, success = 0; char *hashed_host = NULL; if (store_hash) { if ((hashed_host = host_hash(host, NULL, 0)) == NULL) { error("%s: host_hash failed", __func__); return 0; } fprintf(f, "%s ", hashed_host); } else if (ip != NULL) fprintf(f, "%s,%s ", host, ip); else fprintf(f, "%s ", host); if ((r = sshkey_write(key, f)) == 0) success = 1; else error("%s: sshkey_write failed: %s", __func__, ssh_err(r)); fputc('\n', f); return success; }
int add_host_to_hostfile(const char *filename, const char *host, const struct sshkey *key, int store_hash) { FILE *f; int r, success = 0; char *hashed_host = NULL; if (key == NULL) return 1; /* XXX ? */ f = fopen(filename, "a"); if (!f) return 0; if (store_hash) { if ((hashed_host = host_hash(host, NULL, 0)) == NULL) { error("add_host_to_hostfile: host_hash failed"); fclose(f); return 0; } } fprintf(f, "%s ", store_hash ? hashed_host : host); if ((r = sshkey_write(key, f)) != 0) { error("%s: saving key in %s failed: %s", __func__, filename, ssh_err(r)); } else success = 1; fputs("\n", f); fclose(f); return success; }
static void keyprint(con *c, Key *key) { char *host = c->c_output_name ? c->c_output_name : c->c_name; if (!key) return; if (hash_hosts && (host = host_hash(host, NULL, 0)) == NULL) fatal("host_hash failed"); fprintf(stdout, "%s ", host); key_write(key, stdout); fputs("\n", stdout); }
static void keyprint_one(char *host, struct sshkey *key) { char *hostport; if (hash_hosts && (host = host_hash(host, NULL, 0)) == NULL) fatal("host_hash failed"); hostport = put_host_port(host, ssh_port); if (!get_cert) fprintf(stdout, "%s ", hostport); sshkey_write(key, stdout); fputs("\n", stdout); free(hostport); }
static int match_maybe_hashed(const char *host, const char *names, int *was_hashed) { int hashed = *names == HASH_DELIM; const char *hashed_host; size_t nlen = strlen(names); if (was_hashed != NULL) *was_hashed = hashed; if (hashed) { if ((hashed_host = host_hash(host, names, nlen)) == NULL) return -1; return nlen == strlen(hashed_host) && strncmp(hashed_host, names, nlen) == 0; } return match_hostname(host, names) == 1; }
static void keyprint(con *c, struct sshkey *key) { char *host = c->c_output_name ? c->c_output_name : c->c_name; int r; if (!key) return; if (hash_hosts && (host = host_hash(host, NULL, 0)) == NULL) fatal("host_hash failed"); fprintf(stdout, "%s ", host); if ((r = sshkey_write(key, stdout)) != 0) fprintf(stderr, "key_write failed: %s", ssh_err(r)); fputs("\n", stdout); }
static void keyprint_one(const char *host, struct sshkey *key) { char *hostport; const char *known_host, *hashed; found_one = 1; if (print_sshfp) { export_dns_rr(host, key, stdout, 0); return; } hostport = put_host_port(host, ssh_port); lowercase(hostport); if (hash_hosts && (hashed = host_hash(host, NULL, 0)) == NULL) fatal("host_hash failed"); known_host = hash_hosts ? hashed : hostport; if (!get_cert) fprintf(stdout, "%s ", known_host); sshkey_write(key, stdout); fputs("\n", stdout); free(hostport); }
void load_hostkeys(struct hostkeys *hostkeys, const char *host, const char *path) { FILE *f; char line[8192]; u_long linenum = 0, num_loaded = 0; char *cp, *cp2, *hashed_host; HostkeyMarker marker; Key *key; int kbits; if ((f = fopen(path, "r")) == NULL) return; debug3("%s: loading entries for host \"%.100s\" from file \"%s\"", __func__, host, path); while (read_keyfile_line(f, path, line, sizeof(line), &linenum) == 0) { cp = line; /* Skip any leading whitespace, comments and empty lines. */ for (; *cp == ' ' || *cp == '\t'; cp++) ; if (!*cp || *cp == '#' || *cp == '\n') continue; if ((marker = check_markers(&cp)) == MRK_ERROR) { verbose("%s: invalid marker at %s:%lu", __func__, path, linenum); continue; } /* Find the end of the host name portion. */ for (cp2 = cp; *cp2 && *cp2 != ' ' && *cp2 != '\t'; cp2++) ; /* Check if the host name matches. */ if (match_hostname(host, cp, (u_int) (cp2 - cp)) != 1) { if (*cp != HASH_DELIM) continue; hashed_host = host_hash(host, cp, (u_int) (cp2 - cp)); if (hashed_host == NULL) { debug("Invalid hashed host line %lu of %s", linenum, path); continue; } if (strncmp(hashed_host, cp, (u_int) (cp2 - cp)) != 0) continue; } /* Got a match. Skip host name. */ cp = cp2; /* * Extract the key from the line. This will skip any leading * whitespace. Ignore badly formatted lines. */ key = key_new(KEY_UNSPEC); if (!hostfile_read_key(&cp, &kbits, key)) { key_free(key); key = key_new(KEY_RSA1); if (!hostfile_read_key(&cp, &kbits, key)) { key_free(key); continue; } } if (!hostfile_check_key(kbits, key, host, path, linenum)) continue; debug3("%s: found %skey type %s in file %s:%lu", __func__, marker == MRK_NONE ? "" : (marker == MRK_CA ? "ca " : "revoked "), key_type(key), path, linenum); hostkeys->entries = xrealloc(hostkeys->entries, hostkeys->num_entries + 1, sizeof(*hostkeys->entries)); hostkeys->entries[hostkeys->num_entries].host = xstrdup(host); hostkeys->entries[hostkeys->num_entries].file = xstrdup(path); hostkeys->entries[hostkeys->num_entries].line = linenum; hostkeys->entries[hostkeys->num_entries].key = key; hostkeys->entries[hostkeys->num_entries].marker = marker; hostkeys->num_entries++; num_loaded++; } debug3("%s: loaded %lu keys", __func__, num_loaded); fclose(f); return; }
static HostStatus check_host_in_hostfile_by_key_or_type(const char *filename, const char *host, const Key *key, int keytype, Key *found, int *numret) { FILE *f; char line[8192]; int linenum = 0; u_int kbits; char *cp, *cp2, *hashed_host; HostStatus end_return; debug3("check_host_in_hostfile: filename %s", filename); /* Open the file containing the list of known hosts. */ f = fopen(filename, "r"); if (!f) return HOST_NEW; /* * Return value when the loop terminates. This is set to * HOST_CHANGED if we have seen a different key for the host and have * not found the proper one. */ end_return = HOST_NEW; /* Go through the file. */ while (fgets(line, sizeof(line), f)) { cp = line; linenum++; /* Skip any leading whitespace, comments and empty lines. */ for (; *cp == ' ' || *cp == '\t'; cp++) ; if (!*cp || *cp == '#' || *cp == '\n') continue; /* Find the end of the host name portion. */ for (cp2 = cp; *cp2 && *cp2 != ' ' && *cp2 != '\t'; cp2++) ; /* Check if the host name matches. */ if (match_hostname(host, cp, (u_int) (cp2 - cp)) != 1) { if (*cp != HASH_DELIM) continue; hashed_host = host_hash(host, cp, (u_int) (cp2 - cp)); if (hashed_host == NULL) { debug("Invalid hashed host line %d of %s", linenum, filename); continue; } if (strncmp(hashed_host, cp, (u_int) (cp2 - cp)) != 0) continue; } /* Got a match. Skip host name. */ cp = cp2; /* * Extract the key from the line. This will skip any leading * whitespace. Ignore badly formatted lines. */ if (!hostfile_read_key(&cp, &kbits, found)) continue; if (numret != NULL) *numret = linenum; if (key == NULL) { /* we found a key of the requested type */ if (found->type == keytype) return HOST_FOUND; continue; } if (!hostfile_check_key(kbits, found, host, filename, linenum)) continue; /* Check if the current key is the same as the given key. */ if (key_equal(key, found)) { /* Ok, they match. */ debug3("check_host_in_hostfile: match line %d", linenum); fclose(f); return HOST_OK; } /* * They do not match. We will continue to go through the * file; however, we note that we will not return that it is * new. */ end_return = HOST_CHANGED; } /* Clear variables and close the file. */ fclose(f); /* * Return either HOST_NEW or HOST_CHANGED, depending on whether we * saw a different key for the host. */ return end_return; }