summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
author无闻 <u@gogs.io>2015-02-01 16:56:34 -0500
committer无闻 <u@gogs.io>2015-02-01 16:56:34 -0500
commitcd6376f0930d6d0848936756675ffa4c96f6cea8 (patch)
treeef37bf1f30c88717d95838a1cdddce7eb54d71fc /models
parentd4fc8880ebc1b05e19731f377c7b4d7dfc95ed56 (diff)
parent6643647687aa2ba8a41f8f95d404407ff6106d8c (diff)
downloadgitea-cd6376f0930d6d0848936756675ffa4c96f6cea8.tar.gz
gitea-cd6376f0930d6d0848936756675ffa4c96f6cea8.zip
Merge pull request #818 from fzerorubigd/master
add a function to rewrite all public keys on admin request
Diffstat (limited to 'models')
-rw-r--r--models/publickey.go29
1 files changed, 26 insertions, 3 deletions
diff --git a/models/publickey.go b/models/publickey.go
index 566814e841..41233d0c39 100644
--- a/models/publickey.go
+++ b/models/publickey.go
@@ -244,7 +244,7 @@ func CheckPublicKeyString(content string) (bool, error) {
}
// saveAuthorizedKeyFile writes SSH key content to authorized_keys file.
-func saveAuthorizedKeyFile(key *PublicKey) error {
+func saveAuthorizedKeyFile(keys ...*PublicKey) error {
sshOpLocker.Lock()
defer sshOpLocker.Unlock()
@@ -269,8 +269,13 @@ func saveAuthorizedKeyFile(key *PublicKey) error {
}
}
- _, err = f.WriteString(key.GetAuthorizedString())
- return err
+ for _, key := range keys {
+ _, err = f.WriteString(key.GetAuthorizedString())
+ if err != nil {
+ return err
+ }
+ }
+ return nil
}
// AddPublicKey adds new public key to database and authorized_keys file.
@@ -422,3 +427,21 @@ func DeletePublicKey(key *PublicKey) error {
}
return os.Rename(tmpPath, fpath)
}
+
+// RewriteAllPublicKeys remove any authorized key and re-write all key from database again
+func RewriteAllPublicKeys() error {
+ keys := make([]*PublicKey, 0, 5)
+ err := x.Find(&keys)
+ if err != nil {
+ return err
+ }
+
+ fpath := filepath.Join(SshPath, "authorized_keys")
+ if _, err := os.Stat(fpath); os.IsNotExist(err) {
+ return saveAuthorizedKeyFile(keys...)
+ }
+ if err := os.Remove(fpath); err != nil {
+ return err
+ }
+ return saveAuthorizedKeyFile(keys...)
+}