version 1.86, 2020/10/18 11:32:01 |
version 1.87, 2020/12/20 23:36:51 |
|
|
hostkeys->entries[hostkeys->num_entries].key = l->key; |
hostkeys->entries[hostkeys->num_entries].key = l->key; |
l->key = NULL; /* steal it */ |
l->key = NULL; /* steal it */ |
hostkeys->entries[hostkeys->num_entries].marker = l->marker; |
hostkeys->entries[hostkeys->num_entries].marker = l->marker; |
|
hostkeys->entries[hostkeys->num_entries].note = l->note; |
hostkeys->num_entries++; |
hostkeys->num_entries++; |
ctx->num_loaded++; |
ctx->num_loaded++; |
|
|
|
|
} |
} |
|
|
void |
void |
load_hostkeys(struct hostkeys *hostkeys, const char *host, const char *path) |
load_hostkeys_file(struct hostkeys *hostkeys, const char *host, |
|
const char *path, FILE *f, u_int note) |
{ |
{ |
int r; |
int r; |
struct load_callback_ctx ctx; |
struct load_callback_ctx ctx; |
|
|
ctx.num_loaded = 0; |
ctx.num_loaded = 0; |
ctx.hostkeys = hostkeys; |
ctx.hostkeys = hostkeys; |
|
|
if ((r = hostkeys_foreach(path, record_hostkey, &ctx, host, NULL, |
if ((r = hostkeys_foreach_file(path, f, record_hostkey, &ctx, host, |
HKF_WANT_MATCH|HKF_WANT_PARSE_KEY)) != 0) { |
NULL, HKF_WANT_MATCH|HKF_WANT_PARSE_KEY, note)) != 0) { |
if (r != SSH_ERR_SYSTEM_ERROR && errno != ENOENT) |
if (r != SSH_ERR_SYSTEM_ERROR && errno != ENOENT) |
debug_fr(r, "hostkeys_foreach failed for %s", path); |
debug_fr(r, "hostkeys_foreach failed for %s", path); |
} |
} |
|
|
} |
} |
|
|
void |
void |
|
load_hostkeys(struct hostkeys *hostkeys, const char *host, const char *path, |
|
u_int note) |
|
{ |
|
FILE *f; |
|
|
|
if ((f = fopen(path, "r")) == NULL) { |
|
debug_f("fopen %s: %s", path, strerror(errno)); |
|
return; |
|
} |
|
|
|
load_hostkeys_file(hostkeys, host, path, f, note); |
|
fclose(f); |
|
} |
|
|
|
void |
free_hostkeys(struct hostkeys *hostkeys) |
free_hostkeys(struct hostkeys *hostkeys) |
{ |
{ |
u_int i; |
u_int i; |
|
|
|
|
/* Remove stale/mismatching entries for the specified host */ |
/* Remove stale/mismatching entries for the specified host */ |
if ((r = hostkeys_foreach(filename, host_delete, &ctx, host, ip, |
if ((r = hostkeys_foreach(filename, host_delete, &ctx, host, ip, |
HKF_WANT_PARSE_KEY)) != 0) { |
HKF_WANT_PARSE_KEY, 0)) != 0) { |
oerrno = errno; |
oerrno = errno; |
error_fr(r, "hostkeys_foreach"); |
error_fr(r, "hostkeys_foreach"); |
goto fail; |
goto fail; |
|
|
} |
} |
|
|
int |
int |
hostkeys_foreach(const char *path, hostkeys_foreach_fn *callback, void *ctx, |
hostkeys_foreach_file(const char *path, FILE *f, hostkeys_foreach_fn *callback, |
const char *host, const char *ip, u_int options) |
void *ctx, const char *host, const char *ip, u_int options, u_int note) |
{ |
{ |
FILE *f; |
|
char *line = NULL, ktype[128]; |
char *line = NULL, ktype[128]; |
u_long linenum = 0; |
u_long linenum = 0; |
char *cp, *cp2; |
char *cp, *cp2; |
|
|
memset(&lineinfo, 0, sizeof(lineinfo)); |
memset(&lineinfo, 0, sizeof(lineinfo)); |
if (host == NULL && (options & HKF_WANT_MATCH) != 0) |
if (host == NULL && (options & HKF_WANT_MATCH) != 0) |
return SSH_ERR_INVALID_ARGUMENT; |
return SSH_ERR_INVALID_ARGUMENT; |
if ((f = fopen(path, "r")) == NULL) |
|
return SSH_ERR_SYSTEM_ERROR; |
|
|
|
debug3_f("reading file \"%s\"", path); |
|
while (getline(&line, &linesize, f) != -1) { |
while (getline(&line, &linesize, f) != -1) { |
linenum++; |
linenum++; |
line[strcspn(line, "\n")] = '\0'; |
line[strcspn(line, "\n")] = '\0'; |
|
|
lineinfo.marker = MRK_NONE; |
lineinfo.marker = MRK_NONE; |
lineinfo.status = HKF_STATUS_OK; |
lineinfo.status = HKF_STATUS_OK; |
lineinfo.keytype = KEY_UNSPEC; |
lineinfo.keytype = KEY_UNSPEC; |
|
lineinfo.note = note; |
|
|
/* Skip any leading whitespace, comments and empty lines. */ |
/* Skip any leading whitespace, comments and empty lines. */ |
for (cp = line; *cp == ' ' || *cp == '\t'; cp++) |
for (cp = line; *cp == ' ' || *cp == '\t'; cp++) |
|
|
sshkey_free(lineinfo.key); |
sshkey_free(lineinfo.key); |
free(lineinfo.line); |
free(lineinfo.line); |
free(line); |
free(line); |
|
return r; |
|
} |
|
|
|
int |
|
hostkeys_foreach(const char *path, hostkeys_foreach_fn *callback, void *ctx, |
|
const char *host, const char *ip, u_int options, u_int note) |
|
{ |
|
FILE *f; |
|
int r, oerrno; |
|
|
|
if ((f = fopen(path, "r")) == NULL) |
|
return SSH_ERR_SYSTEM_ERROR; |
|
|
|
debug3_f("reading file \"%s\"", path); |
|
r = hostkeys_foreach_file(path, f, callback, ctx, host, ip, |
|
options, note); |
|
oerrno = errno; |
fclose(f); |
fclose(f); |
|
errno = oerrno; |
return r; |
return r; |
} |
} |