ssh-keygen: error if file is directory

Salar Nosrati-Ershad snosratiershad at gmail.com
Mon Jan 6 20:37:31 AEDT 2025


On several tasks if you pass a directory as a file to ssh-keygen, the
program wouldn't check the file mode to check if it's a directory like:
`Saving key "./test/" failed: Is a directory`
After asking the user to overwrite or not.

The file mode is already readed when getting `stat`
Do you think checking it is a good idea?

I've done something like:
>From 2794c45c84c06999d977d44b69e9fc34e93c8336 Mon Sep 17 00:00:00 2001
From: Salar Nosrati-Ershad <snosratiershad at gmail.com>
Date: Mon, 6 Jan 2025 12:59:22 +0330
Subject: [PATCH] ssh-keygen: feat: raise error on confirm_overwrite if file
is
directory

---
ssh-keygen.c | 4 ++++
1 file changed, 4 insertions(+)

diff --git a/ssh-keygen.c b/ssh-keygen.c
index 89c3ed287..94665b0ab 100644
--- a/ssh-keygen.c
+++ b/ssh-keygen.c
@@ -242,6 +242,10 @@ confirm_overwrite(const char *filename)

       if (stat(filename, &st) != 0)
               return 1;
+       if (S_ISDIR(st.st_mode)) {
+               error("%s is a directory.", filename);
+               return 0;
+       }
       printf("%s already exists.\n", filename);
       printf("Overwrite (y/n)? ");
       fflush(stdout);
--
2.43.0


More information about the openssh-unix-dev mailing list