summaryrefslogtreecommitdiff
path: root/ssh-keygen.c
diff options
context:
space:
mode:
authordjm@openbsd.org <djm@openbsd.org>2019-09-03 08:27:52 +0000
committerDamien Miller <djm@mindrot.org>2019-09-03 18:39:31 +1000
commit85443f165b4169b2a448b3e24bc1d4dc5b3156a4 (patch)
tree1d28f80daa6ad37e1ecedda5f22c8c7362c7859b /ssh-keygen.c
parent9a396e33685633581c67d5ad9664570ef95281f2 (diff)
upstream: factor out confirm_overwrite(); ok markus@
OpenBSD-Commit-ID: 304e95381b39c774c8fced7e5328b106a3ff0400
Diffstat (limited to 'ssh-keygen.c')
-rw-r--r--ssh-keygen.c38
1 files changed, 27 insertions, 11 deletions
diff --git a/ssh-keygen.c b/ssh-keygen.c
index 6426bb743..3c32513bf 100644
--- a/ssh-keygen.c
+++ b/ssh-keygen.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ssh-keygen.c,v 1.342 2019/09/02 23:46:46 djm Exp $ */ 1/* $OpenBSD: ssh-keygen.c,v 1.343 2019/09/03 08:27:52 djm Exp $ */
2/* 2/*
3 * Author: Tatu Ylonen <ylo@cs.hut.fi> 3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 4 * Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
@@ -222,6 +222,30 @@ type_bits_valid(int type, const char *name, u_int32_t *bitsp)
222#endif 222#endif
223} 223}
224 224
225/*
226 * Checks whether a file exists and, if so, asks the user whether they wish
227 * to overwrite it.
228 * Returns nonzero if the file does not already exist or if the user agrees to
229 * overwrite, or zero otherwise.
230 */
231static int
232confirm_overwrite(const char *filename)
233{
234 char yesno[3];
235 struct stat st;
236
237 if (stat(filename, &st) != 0)
238 return 1;
239 printf("%s already exists.\n", filename);
240 printf("Overwrite (y/n)? ");
241 fflush(stdout);
242 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
243 return 0;
244 if (yesno[0] != 'y' && yesno[0] != 'Y')
245 return 0;
246 return 1;
247}
248
225static void 249static void
226ask_filename(struct passwd *pw, const char *prompt) 250ask_filename(struct passwd *pw, const char *prompt)
227{ 251{
@@ -2881,16 +2905,8 @@ main(int argc, char **argv)
2881 } 2905 }
2882 } 2906 }
2883 /* If the file already exists, ask the user to confirm. */ 2907 /* If the file already exists, ask the user to confirm. */
2884 if (stat(identity_file, &st) >= 0) { 2908 if (!confirm_overwrite(identity_file))
2885 char yesno[3]; 2909 exit(1);
2886 printf("%s already exists.\n", identity_file);
2887 printf("Overwrite (y/n)? ");
2888 fflush(stdout);
2889 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
2890 exit(1);
2891 if (yesno[0] != 'y' && yesno[0] != 'Y')
2892 exit(1);
2893 }
2894 /* Ask for a passphrase (twice). */ 2910 /* Ask for a passphrase (twice). */
2895 if (identity_passphrase) 2911 if (identity_passphrase)
2896 passphrase1 = xstrdup(identity_passphrase); 2912 passphrase1 = xstrdup(identity_passphrase);