summaryrefslogtreecommitdiffstats
path: root/usr.bin/ssh/ssh-keygen.c
diff options
context:
space:
mode:
authordjm <djm@openbsd.org>2019-09-03 08:27:52 +0000
committerdjm <djm@openbsd.org>2019-09-03 08:27:52 +0000
commitf1eaa432116ffeeba7deed5ebff238fac7e7bca6 (patch)
treefaa75b402d645d2a8586716977bbe35af0dd45f5 /usr.bin/ssh/ssh-keygen.c
parentIt's perfectly normal for the SIOCGIFFLAGS and SIOCGIFXFLAGS to fail (diff)
downloadwireguard-openbsd-f1eaa432116ffeeba7deed5ebff238fac7e7bca6.tar.xz
wireguard-openbsd-f1eaa432116ffeeba7deed5ebff238fac7e7bca6.zip
factor out confirm_overwrite(); ok markus@
Diffstat (limited to 'usr.bin/ssh/ssh-keygen.c')
-rw-r--r--usr.bin/ssh/ssh-keygen.c38
1 files changed, 27 insertions, 11 deletions
diff --git a/usr.bin/ssh/ssh-keygen.c b/usr.bin/ssh/ssh-keygen.c
index e82c2320e63..431f22a6305 100644
--- a/usr.bin/ssh/ssh-keygen.c
+++ b/usr.bin/ssh/ssh-keygen.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: ssh-keygen.c,v 1.342 2019/09/02 23:46:46 djm Exp $ */
+/* $OpenBSD: ssh-keygen.c,v 1.343 2019/09/03 08:27:52 djm Exp $ */
/*
* Author: Tatu Ylonen <ylo@cs.hut.fi>
* Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
@@ -211,6 +211,30 @@ type_bits_valid(int type, const char *name, u_int32_t *bitsp)
#endif
}
+/*
+ * Checks whether a file exists and, if so, asks the user whether they wish
+ * to overwrite it.
+ * Returns nonzero if the file does not already exist or if the user agrees to
+ * overwrite, or zero otherwise.
+ */
+static int
+confirm_overwrite(const char *filename)
+{
+ char yesno[3];
+ struct stat st;
+
+ if (stat(filename, &st) != 0)
+ return 1;
+ printf("%s already exists.\n", filename);
+ printf("Overwrite (y/n)? ");
+ fflush(stdout);
+ if (fgets(yesno, sizeof(yesno), stdin) == NULL)
+ return 0;
+ if (yesno[0] != 'y' && yesno[0] != 'Y')
+ return 0;
+ return 1;
+}
+
static void
ask_filename(struct passwd *pw, const char *prompt)
{
@@ -2861,16 +2885,8 @@ main(int argc, char **argv)
}
}
/* If the file already exists, ask the user to confirm. */
- if (stat(identity_file, &st) >= 0) {
- char yesno[3];
- printf("%s already exists.\n", identity_file);
- printf("Overwrite (y/n)? ");
- fflush(stdout);
- if (fgets(yesno, sizeof(yesno), stdin) == NULL)
- exit(1);
- if (yesno[0] != 'y' && yesno[0] != 'Y')
- exit(1);
- }
+ if (!confirm_overwrite(identity_file))
+ exit(1);
/* Ask for a passphrase (twice). */
if (identity_passphrase)
passphrase1 = xstrdup(identity_passphrase);