mirror of
https://github.com/PowerShell/openssh-portable.git
synced 2025-07-25 06:45:39 +02:00
upstream: we have a sshkey_save_public() function to save public keys;
use it and save a bunch of redundant code. Patch from loic AT venez.fr; ok markus@ djm@ OpenBSD-Commit-ID: f93e030a0ebcd0fd9054ab30db501ec63454ea5f
This commit is contained in:
parent
e9dc986372
commit
d25d630d24
67
ssh-keygen.c
67
ssh-keygen.c
@ -1,4 +1,4 @@
|
|||||||
/* $OpenBSD: ssh-keygen.c,v 1.408 2020/05/01 04:23:11 djm Exp $ */
|
/* $OpenBSD: ssh-keygen.c,v 1.409 2020/05/02 07:19:43 djm Exp $ */
|
||||||
/*
|
/*
|
||||||
* Author: Tatu Ylonen <ylo@cs.hut.fi>
|
* Author: Tatu Ylonen <ylo@cs.hut.fi>
|
||||||
* Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
|
* Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
|
||||||
@ -1057,7 +1057,6 @@ do_gen_all_hostkeys(struct passwd *pw)
|
|||||||
struct sshkey *private, *public;
|
struct sshkey *private, *public;
|
||||||
char comment[1024], *prv_tmp, *pub_tmp, *prv_file, *pub_file;
|
char comment[1024], *prv_tmp, *pub_tmp, *prv_file, *pub_file;
|
||||||
int i, type, fd, r;
|
int i, type, fd, r;
|
||||||
FILE *f;
|
|
||||||
|
|
||||||
for (i = 0; key_types[i].key_type; i++) {
|
for (i = 0; key_types[i].key_type; i++) {
|
||||||
public = private = NULL;
|
public = private = NULL;
|
||||||
@ -1095,11 +1094,11 @@ do_gen_all_hostkeys(struct passwd *pw)
|
|||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
type = sshkey_type_from_name(key_types[i].key_type);
|
type = sshkey_type_from_name(key_types[i].key_type);
|
||||||
if ((fd = mkstemp(prv_tmp)) == -1) {
|
if ((fd = mkstemp(prv_tmp)) == -1) {
|
||||||
error("Could not save your public key in %s: %s",
|
error("Could not save your private key in %s: %s",
|
||||||
prv_tmp, strerror(errno));
|
prv_tmp, strerror(errno));
|
||||||
goto failnext;
|
goto failnext;
|
||||||
}
|
}
|
||||||
close(fd); /* just using mkstemp() to generate/reserve a name */
|
(void)close(fd); /* just using mkstemp() to reserve a name */
|
||||||
bits = 0;
|
bits = 0;
|
||||||
type_bits_valid(type, NULL, &bits);
|
type_bits_valid(type, NULL, &bits);
|
||||||
if ((r = sshkey_generate(type, bits, &private)) != 0) {
|
if ((r = sshkey_generate(type, bits, &private)) != 0) {
|
||||||
@ -1123,25 +1122,10 @@ do_gen_all_hostkeys(struct passwd *pw)
|
|||||||
goto failnext;
|
goto failnext;
|
||||||
}
|
}
|
||||||
(void)fchmod(fd, 0644);
|
(void)fchmod(fd, 0644);
|
||||||
f = fdopen(fd, "w");
|
(void)close(fd);
|
||||||
if (f == NULL) {
|
if ((r = sshkey_save_public(public, pub_tmp, comment)) != 0) {
|
||||||
error("fdopen %s failed: %s", pub_tmp, strerror(errno));
|
fatal("Unable to save public key to %s: %s",
|
||||||
close(fd);
|
identity_file, ssh_err(r));
|
||||||
goto failnext;
|
|
||||||
}
|
|
||||||
if ((r = sshkey_write(public, f)) != 0) {
|
|
||||||
error("write key failed: %s", ssh_err(r));
|
|
||||||
fclose(f);
|
|
||||||
goto failnext;
|
|
||||||
}
|
|
||||||
fprintf(f, " %s\n", comment);
|
|
||||||
if (ferror(f) != 0) {
|
|
||||||
error("write key failed: %s", strerror(errno));
|
|
||||||
fclose(f);
|
|
||||||
goto failnext;
|
|
||||||
}
|
|
||||||
if (fclose(f) != 0) {
|
|
||||||
error("key close failed: %s", strerror(errno));
|
|
||||||
goto failnext;
|
goto failnext;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1518,8 +1502,7 @@ do_change_comment(struct passwd *pw, const char *identity_comment)
|
|||||||
struct sshkey *private;
|
struct sshkey *private;
|
||||||
struct sshkey *public;
|
struct sshkey *public;
|
||||||
struct stat st;
|
struct stat st;
|
||||||
FILE *f;
|
int r;
|
||||||
int r, fd;
|
|
||||||
|
|
||||||
if (!have_identity)
|
if (!have_identity)
|
||||||
ask_filename(pw, "Enter file in which the key is");
|
ask_filename(pw, "Enter file in which the key is");
|
||||||
@ -1598,18 +1581,11 @@ do_change_comment(struct passwd *pw, const char *identity_comment)
|
|||||||
sshkey_free(private);
|
sshkey_free(private);
|
||||||
|
|
||||||
strlcat(identity_file, ".pub", sizeof(identity_file));
|
strlcat(identity_file, ".pub", sizeof(identity_file));
|
||||||
fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
if ((r = sshkey_save_public(public, identity_file, new_comment)) != 0) {
|
||||||
if (fd == -1)
|
fatal("Unable to save public key to %s: %s",
|
||||||
fatal("Could not save your public key in %s", identity_file);
|
identity_file, ssh_err(r));
|
||||||
f = fdopen(fd, "w");
|
}
|
||||||
if (f == NULL)
|
|
||||||
fatal("fdopen %s failed: %s", identity_file, strerror(errno));
|
|
||||||
if ((r = sshkey_write(public, f)) != 0)
|
|
||||||
fatal("write key failed: %s", ssh_err(r));
|
|
||||||
sshkey_free(public);
|
sshkey_free(public);
|
||||||
fprintf(f, " %s\n", new_comment);
|
|
||||||
fclose(f);
|
|
||||||
|
|
||||||
free(comment);
|
free(comment);
|
||||||
|
|
||||||
if (strlen(new_comment) > 0)
|
if (strlen(new_comment) > 0)
|
||||||
@ -1741,12 +1717,11 @@ do_ca_sign(struct passwd *pw, const char *ca_key_path, int prefer_agent,
|
|||||||
unsigned long long cert_serial, int cert_serial_autoinc,
|
unsigned long long cert_serial, int cert_serial_autoinc,
|
||||||
int argc, char **argv)
|
int argc, char **argv)
|
||||||
{
|
{
|
||||||
int r, i, fd, found, agent_fd = -1;
|
int r, i, found, agent_fd = -1;
|
||||||
u_int n;
|
u_int n;
|
||||||
struct sshkey *ca, *public;
|
struct sshkey *ca, *public;
|
||||||
char valid[64], *otmp, *tmp, *cp, *out, *comment;
|
char valid[64], *otmp, *tmp, *cp, *out, *comment;
|
||||||
char *ca_fp = NULL, **plist = NULL;
|
char *ca_fp = NULL, **plist = NULL;
|
||||||
FILE *f;
|
|
||||||
struct ssh_identitylist *agent_ids;
|
struct ssh_identitylist *agent_ids;
|
||||||
size_t j;
|
size_t j;
|
||||||
struct notifier_ctx *notifier = NULL;
|
struct notifier_ctx *notifier = NULL;
|
||||||
@ -1869,16 +1844,10 @@ do_ca_sign(struct passwd *pw, const char *ca_key_path, int prefer_agent,
|
|||||||
xasprintf(&out, "%s-cert.pub", tmp);
|
xasprintf(&out, "%s-cert.pub", tmp);
|
||||||
free(tmp);
|
free(tmp);
|
||||||
|
|
||||||
if ((fd = open(out, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
|
if ((r = sshkey_save_public(public, out, comment)) != 0) {
|
||||||
fatal("Could not open \"%s\" for writing: %s", out,
|
fatal("Unable to save public key to %s: %s",
|
||||||
strerror(errno));
|
identity_file, ssh_err(r));
|
||||||
if ((f = fdopen(fd, "w")) == NULL)
|
}
|
||||||
fatal("%s: fdopen: %s", __func__, strerror(errno));
|
|
||||||
if ((r = sshkey_write(public, f)) != 0)
|
|
||||||
fatal("Could not write certified key to %s: %s",
|
|
||||||
out, ssh_err(r));
|
|
||||||
fprintf(f, " %s\n", comment);
|
|
||||||
fclose(f);
|
|
||||||
|
|
||||||
if (!quiet) {
|
if (!quiet) {
|
||||||
sshkey_format_cert_validity(public->cert,
|
sshkey_format_cert_validity(public->cert,
|
||||||
@ -3680,7 +3649,7 @@ main(int argc, char **argv)
|
|||||||
strlcat(identity_file, ".pub", sizeof(identity_file));
|
strlcat(identity_file, ".pub", sizeof(identity_file));
|
||||||
if ((r = sshkey_save_public(public, identity_file, comment)) != 0) {
|
if ((r = sshkey_save_public(public, identity_file, comment)) != 0) {
|
||||||
fatal("Unable to save public key to %s: %s",
|
fatal("Unable to save public key to %s: %s",
|
||||||
identity_file, strerror(errno));
|
identity_file, ssh_err(r));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!quiet) {
|
if (!quiet) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user