Skip to content

Commit cb31f88

Browse files
zeripathlafriks
authored andcommitted
Fixes #8369: Create .ssh dir as necessary (#8486) (#8489)
* 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 6cb9ce1 commit cb31f88

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

models/ssh_key.go

+24
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,18 @@ func appendAuthorizedKeysToFile(keys ...*PublicKey) error {
315315
sshOpLocker.Lock()
316316
defer sshOpLocker.Unlock()
317317

318+
if setting.SSH.RootPath != "" {
319+
// First of ensure that the RootPath is present, and if not make it with 0700 permissions
320+
// This of course doesn't guarantee that this is the right directory for authorized_keys
321+
// but at least if it's supposed to be this directory and it doesn't exist and we're the
322+
// right user it will at least be created properly.
323+
err := os.MkdirAll(setting.SSH.RootPath, 0700)
324+
if err != nil {
325+
log.Error("Unable to MkdirAll(%s): %v", setting.SSH.RootPath, err)
326+
return err
327+
}
328+
}
329+
318330
fPath := filepath.Join(setting.SSH.RootPath, "authorized_keys")
319331
f, err := os.OpenFile(fPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600)
320332
if err != nil {
@@ -602,6 +614,18 @@ func rewriteAllPublicKeys(e Engine) error {
602614
sshOpLocker.Lock()
603615
defer sshOpLocker.Unlock()
604616

617+
if setting.SSH.RootPath != "" {
618+
// First of ensure that the RootPath is present, and if not make it with 0700 permissions
619+
// This of course doesn't guarantee that this is the right directory for authorized_keys
620+
// but at least if it's supposed to be this directory and it doesn't exist and we're the
621+
// right user it will at least be created properly.
622+
err := os.MkdirAll(setting.SSH.RootPath, 0700)
623+
if err != nil {
624+
log.Error("Unable to MkdirAll(%s): %v", setting.SSH.RootPath, err)
625+
return err
626+
}
627+
}
628+
605629
fPath := filepath.Join(setting.SSH.RootPath, "authorized_keys")
606630
tmpPath := fPath + ".tmp"
607631
t, err := os.OpenFile(tmpPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)

0 commit comments

Comments
 (0)