]> source.dussan.org Git - gitea.git/commitdiff
Fixes #8369: Create .ssh dir as necessary (#8486)
authorzeripath <art27@cantab.net>
Sun, 13 Oct 2019 14:35:19 +0000 (15:35 +0100)
committerLauris BH <lauris@nix.lv>
Sun, 13 Oct 2019 14:35:19 +0000 (17:35 +0300)
* 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

models/ssh_key.go

index b7c5b4fe6e587cbb60526aea1e66fb077137fcf5..d1132bf0c61a0aa8d183e8cecae96fb171d1b350 100644 (file)
@@ -358,6 +358,18 @@ func appendAuthorizedKeysToFile(keys ...*PublicKey) error {
        sshOpLocker.Lock()
        defer sshOpLocker.Unlock()
 
+       if setting.SSH.RootPath != "" {
+               // First of ensure that the RootPath is present, and if not make it with 0700 permissions
+               // This of course doesn't guarantee that this is the right directory for authorized_keys
+               // but at least if it's supposed to be this directory and it doesn't exist and we're the
+               // right user it will at least be created properly.
+               err := os.MkdirAll(setting.SSH.RootPath, 0700)
+               if err != nil {
+                       log.Error("Unable to MkdirAll(%s): %v", setting.SSH.RootPath, err)
+                       return err
+               }
+       }
+
        fPath := filepath.Join(setting.SSH.RootPath, "authorized_keys")
        f, err := os.OpenFile(fPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600)
        if err != nil {
@@ -645,6 +657,18 @@ func rewriteAllPublicKeys(e Engine) error {
        sshOpLocker.Lock()
        defer sshOpLocker.Unlock()
 
+       if setting.SSH.RootPath != "" {
+               // First of ensure that the RootPath is present, and if not make it with 0700 permissions
+               // This of course doesn't guarantee that this is the right directory for authorized_keys
+               // but at least if it's supposed to be this directory and it doesn't exist and we're the
+               // right user it will at least be created properly.
+               err := os.MkdirAll(setting.SSH.RootPath, 0700)
+               if err != nil {
+                       log.Error("Unable to MkdirAll(%s): %v", setting.SSH.RootPath, err)
+                       return err
+               }
+       }
+
        fPath := filepath.Join(setting.SSH.RootPath, "authorized_keys")
        tmpPath := fPath + ".tmp"
        t, err := os.OpenFile(tmpPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)