Skip to content

Commit 300d9a1

Browse files
zeripathlafriks
authored andcommitted
Fixes #8369: Create .ssh dir as necessary (#8486)
* Ensure .ssh dir exists before rewriting public keys * Ensure .ssh dir exists before appending to authorized_keys * Log the error because it would be useful to know where it is trying to MkdirAll * Only try to create RootPath if it's not empty
1 parent f2a3abc commit 300d9a1

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

models/ssh_key.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,18 @@ func appendAuthorizedKeysToFile(keys ...*PublicKey) error {
358358
sshOpLocker.Lock()
359359
defer sshOpLocker.Unlock()
360360

361+
if setting.SSH.RootPath != "" {
362+
// First of ensure that the RootPath is present, and if not make it with 0700 permissions
363+
// This of course doesn't guarantee that this is the right directory for authorized_keys
364+
// but at least if it's supposed to be this directory and it doesn't exist and we're the
365+
// right user it will at least be created properly.
366+
err := os.MkdirAll(setting.SSH.RootPath, 0700)
367+
if err != nil {
368+
log.Error("Unable to MkdirAll(%s): %v", setting.SSH.RootPath, err)
369+
return err
370+
}
371+
}
372+
361373
fPath := filepath.Join(setting.SSH.RootPath, "authorized_keys")
362374
f, err := os.OpenFile(fPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600)
363375
if err != nil {
@@ -645,6 +657,18 @@ func rewriteAllPublicKeys(e Engine) error {
645657
sshOpLocker.Lock()
646658
defer sshOpLocker.Unlock()
647659

660+
if setting.SSH.RootPath != "" {
661+
// First of ensure that the RootPath is present, and if not make it with 0700 permissions
662+
// This of course doesn't guarantee that this is the right directory for authorized_keys
663+
// but at least if it's supposed to be this directory and it doesn't exist and we're the
664+
// right user it will at least be created properly.
665+
err := os.MkdirAll(setting.SSH.RootPath, 0700)
666+
if err != nil {
667+
log.Error("Unable to MkdirAll(%s): %v", setting.SSH.RootPath, err)
668+
return err
669+
}
670+
}
671+
648672
fPath := filepath.Join(setting.SSH.RootPath, "authorized_keys")
649673
tmpPath := fPath + ".tmp"
650674
t, err := os.OpenFile(tmpPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)

0 commit comments

Comments
 (0)