diff options
Diffstat (limited to 'ssh-keygen.c')
-rw-r--r-- | ssh-keygen.c | 38 |
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 | */ | ||
231 | static int | ||
232 | confirm_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 | |||
225 | static void | 249 | static void |
226 | ask_filename(struct passwd *pw, const char *prompt) | 250 | ask_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); |