diff options
author | djm@openbsd.org <djm@openbsd.org> | 2020-01-02 22:38:33 +0000 |
---|---|---|
committer | Damien Miller <djm@mindrot.org> | 2020-01-03 09:41:20 +1100 |
commit | 878ba4350d57e905d6bb1865d8ff31bdfe5deab4 (patch) | |
tree | 00a528e2edf29a2574cda7e7531376c8d14b3aac | |
parent | 3b1382ffd5e71eff78db8cef0f3cada22ff29409 (diff) |
upstream: add sshkey_save_public(), to save a public key; ok
markus@
OpenBSD-Commit-ID: 5d6f96a966d10d7fa689ff9aa9e1d6767ad5a076
-rw-r--r-- | authfile.c | 33 | ||||
-rw-r--r-- | authfile.h | 4 |
2 files changed, 35 insertions, 2 deletions
diff --git a/authfile.c b/authfile.c index 37341189c..bf22d63e8 100644 --- a/authfile.c +++ b/authfile.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: authfile.c,v 1.135 2019/09/03 08:30:47 djm Exp $ */ | 1 | /* $OpenBSD: authfile.c,v 1.136 2020/01/02 22:38:33 djm Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2000, 2013 Markus Friedl. All rights reserved. | 3 | * Copyright (c) 2000, 2013 Markus Friedl. All rights reserved. |
4 | * | 4 | * |
@@ -550,3 +550,34 @@ sshkey_advance_past_options(char **cpp) | |||
550 | return (*cp == '\0' && quoted) ? -1 : 0; | 550 | return (*cp == '\0' && quoted) ? -1 : 0; |
551 | } | 551 | } |
552 | 552 | ||
553 | /* Save a public key */ | ||
554 | int | ||
555 | sshkey_save_public(const struct sshkey *key, const char *path, | ||
556 | const char *comment) | ||
557 | { | ||
558 | int fd, oerrno; | ||
559 | FILE *f = NULL; | ||
560 | int r = SSH_ERR_INTERNAL_ERROR; | ||
561 | |||
562 | if ((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1) | ||
563 | return SSH_ERR_SYSTEM_ERROR; | ||
564 | if ((f = fdopen(fd, "w")) == NULL) { | ||
565 | r = SSH_ERR_SYSTEM_ERROR; | ||
566 | goto fail; | ||
567 | } | ||
568 | if ((r = sshkey_write(key, f)) != 0) | ||
569 | goto fail; | ||
570 | fprintf(f, " %s\n", comment); | ||
571 | if (ferror(f) || fclose(f) != 0) { | ||
572 | r = SSH_ERR_SYSTEM_ERROR; | ||
573 | fail: | ||
574 | oerrno = errno; | ||
575 | if (f != NULL) | ||
576 | fclose(f); | ||
577 | else | ||
578 | close(fd); | ||
579 | errno = oerrno; | ||
580 | return r; | ||
581 | } | ||
582 | return 0; | ||
583 | } | ||
diff --git a/authfile.h b/authfile.h index 9c8a95a01..a2840cf80 100644 --- a/authfile.h +++ b/authfile.h | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: authfile.h,v 1.23 2019/09/03 08:30:47 djm Exp $ */ | 1 | /* $OpenBSD: authfile.h,v 1.24 2020/01/02 22:38:33 djm Exp $ */ |
2 | 2 | ||
3 | /* | 3 | /* |
4 | * Copyright (c) 2000, 2013 Markus Friedl. All rights reserved. | 4 | * Copyright (c) 2000, 2013 Markus Friedl. All rights reserved. |
@@ -49,5 +49,7 @@ int sshkey_perm_ok(int, const char *); | |||
49 | int sshkey_in_file(struct sshkey *, const char *, int, int); | 49 | int sshkey_in_file(struct sshkey *, const char *, int, int); |
50 | int sshkey_check_revoked(struct sshkey *key, const char *revoked_keys_file); | 50 | int sshkey_check_revoked(struct sshkey *key, const char *revoked_keys_file); |
51 | int sshkey_advance_past_options(char **cpp); | 51 | int sshkey_advance_past_options(char **cpp); |
52 | int sshkey_save_public(const struct sshkey *key, const char *path, | ||
53 | const char *comment); | ||
52 | 54 | ||
53 | #endif | 55 | #endif |