@@ -19,6 +19,8 @@ import (
19
19
"time"
20
20
21
21
"github.com/Unknwon/com"
22
+
23
+ "github.com/gogits/gogs/modules/log"
22
24
)
23
25
24
26
const (
@@ -99,8 +101,8 @@ func AddPublicKey(key *PublicKey) (err error) {
99
101
}
100
102
101
103
// Calculate fingerprint.
102
- tmpPath := filepath .Join (os .TempDir (), fmt .Sprintf ("%d" , time .Now ().Nanosecond ()),
103
- "id_rsa.pub" )
104
+ tmpPath := strings . Replace ( filepath .Join (os .TempDir (), fmt .Sprintf ("%d" , time .Now ().Nanosecond ()),
105
+ "id_rsa.pub" ), " \\ " , "/" , - 1 )
104
106
os .MkdirAll (path .Dir (tmpPath ), os .ModePerm )
105
107
if err = ioutil .WriteFile (tmpPath , []byte (key .Content ), os .ModePerm ); err != nil {
106
108
return err
@@ -127,25 +129,11 @@ func AddPublicKey(key *PublicKey) (err error) {
127
129
return nil
128
130
}
129
131
130
- // DeletePublicKey deletes SSH key information both in database and authorized_keys file.
131
- func DeletePublicKey (key * PublicKey ) (err error ) {
132
- // Delete SSH key in database.
133
- has , err := orm .Id (key .Id ).Get (key )
134
- if err != nil {
135
- return err
136
- } else if ! has {
137
- return errors .New ("Public key does not exist" )
138
- }
139
- if _ , err = orm .Delete (key ); err != nil {
140
- return err
141
- }
142
-
132
+ func rewriteAuthorizedKeys (key * PublicKey , p , tmpP string ) error {
143
133
// Delete SSH key in SSH key file.
144
134
sshOpLocker .Lock ()
145
135
defer sshOpLocker .Unlock ()
146
136
147
- p := filepath .Join (sshPath , "authorized_keys" )
148
- tmpP := filepath .Join (sshPath , "authorized_keys.tmp" )
149
137
fr , err := os .Open (p )
150
138
if err != nil {
151
139
return err
@@ -188,8 +176,29 @@ func DeletePublicKey(key *PublicKey) (err error) {
188
176
break
189
177
}
190
178
}
179
+ return nil
180
+ }
191
181
192
- if err = os .Remove (p ); err != nil {
182
+ // DeletePublicKey deletes SSH key information both in database and authorized_keys file.
183
+ func DeletePublicKey (key * PublicKey ) (err error ) {
184
+ // Delete SSH key in database.
185
+ has , err := orm .Id (key .Id ).Get (key )
186
+ if err != nil {
187
+ return err
188
+ } else if ! has {
189
+ return errors .New ("Public key does not exist" )
190
+ }
191
+ if _ , err = orm .Delete (key ); err != nil {
192
+ return err
193
+ }
194
+
195
+ p := filepath .Join (sshPath , "authorized_keys" )
196
+ tmpP := filepath .Join (sshPath , "authorized_keys.tmp" )
197
+ log .Trace ("ssh.DeletePublicKey(authorized_keys): %s" , p )
198
+
199
+ if err = rewriteAuthorizedKeys (key , p , tmpP ); err != nil {
200
+ return err
201
+ } else if err = os .Remove (p ); err != nil {
193
202
return err
194
203
}
195
204
return os .Rename (tmpP , p )
0 commit comments