summaryrefslogtreecommitdiffstats
path: root/authfile.c
diff options
context:
space:
mode:
authordjm@openbsd.org <djm@openbsd.org>2020-01-02 22:38:33 +0000
committerDamien Miller <djm@mindrot.org>2020-01-03 09:41:20 +1100
commit878ba4350d57e905d6bb1865d8ff31bdfe5deab4 (patch)
tree00a528e2edf29a2574cda7e7531376c8d14b3aac /authfile.c
parent3b1382ffd5e71eff78db8cef0f3cada22ff29409 (diff)
upstream: add sshkey_save_public(), to save a public key; ok
markus@ OpenBSD-Commit-ID: 5d6f96a966d10d7fa689ff9aa9e1d6767ad5a076
Diffstat (limited to 'authfile.c')
-rw-r--r--authfile.c33
1 files changed, 32 insertions, 1 deletions
diff --git a/authfile.c b/authfile.c
index 37341189..bf22d63e 100644
--- a/authfile.c
+++ b/authfile.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: authfile.c,v 1.135 2019/09/03 08:30:47 djm Exp $ */
+/* $OpenBSD: authfile.c,v 1.136 2020/01/02 22:38:33 djm Exp $ */
/*
* Copyright (c) 2000, 2013 Markus Friedl. All rights reserved.
*
@@ -550,3 +550,34 @@ sshkey_advance_past_options(char **cpp)
return (*cp == '\0' && quoted) ? -1 : 0;
}
+/* Save a public key */
+int
+sshkey_save_public(const struct sshkey *key, const char *path,
+ const char *comment)
+{
+ int fd, oerrno;
+ FILE *f = NULL;
+ int r = SSH_ERR_INTERNAL_ERROR;
+
+ if ((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
+ return SSH_ERR_SYSTEM_ERROR;
+ if ((f = fdopen(fd, "w")) == NULL) {
+ r = SSH_ERR_SYSTEM_ERROR;
+ goto fail;
+ }
+ if ((r = sshkey_write(key, f)) != 0)
+ goto fail;
+ fprintf(f, " %s\n", comment);
+ if (ferror(f) || fclose(f) != 0) {
+ r = SSH_ERR_SYSTEM_ERROR;
+ fail:
+ oerrno = errno;
+ if (f != NULL)
+ fclose(f);
+ else
+ close(fd);
+ errno = oerrno;
+ return r;
+ }
+ return 0;
+}