@@ -12,7 +12,6 @@ import (
1212 "path/filepath"
1313 "strings"
1414 "sync"
15- "time"
1615
1716 "code.gitea.io/gitea/models/db"
1817 "code.gitea.io/gitea/modules/log"
@@ -44,6 +43,12 @@ const (
4443
4544var sshOpLocker sync.Mutex
4645
46+ func WithSSHOpLocker (f func () error ) error {
47+ sshOpLocker .Lock ()
48+ defer sshOpLocker .Unlock ()
49+ return f ()
50+ }
51+
4752// AuthorizedStringForKey creates the authorized keys string appropriate for the provided key
4853func AuthorizedStringForKey (key * PublicKey ) string {
4954 sb := & strings.Builder {}
@@ -114,65 +119,6 @@ func appendAuthorizedKeysToFile(keys ...*PublicKey) error {
114119 return nil
115120}
116121
117- // RewriteAllPublicKeys removes any authorized key and rewrite all keys from database again.
118- // Note: db.GetEngine(ctx).Iterate does not get latest data after insert/delete, so we have to call this function
119- // outside any session scope independently.
120- func RewriteAllPublicKeys (ctx context.Context ) error {
121- // Don't rewrite key if internal server
122- if setting .SSH .StartBuiltinServer || ! setting .SSH .CreateAuthorizedKeysFile {
123- return nil
124- }
125-
126- sshOpLocker .Lock ()
127- defer sshOpLocker .Unlock ()
128-
129- if setting .SSH .RootPath != "" {
130- // First of ensure that the RootPath is present, and if not make it with 0700 permissions
131- // This of course doesn't guarantee that this is the right directory for authorized_keys
132- // but at least if it's supposed to be this directory and it doesn't exist and we're the
133- // right user it will at least be created properly.
134- err := os .MkdirAll (setting .SSH .RootPath , 0o700 )
135- if err != nil {
136- log .Error ("Unable to MkdirAll(%s): %v" , setting .SSH .RootPath , err )
137- return err
138- }
139- }
140-
141- fPath := filepath .Join (setting .SSH .RootPath , "authorized_keys" )
142- tmpPath := fPath + ".tmp"
143- t , err := os .OpenFile (tmpPath , os .O_RDWR | os .O_CREATE | os .O_TRUNC , 0o600 )
144- if err != nil {
145- return err
146- }
147- defer func () {
148- t .Close ()
149- if err := util .Remove (tmpPath ); err != nil {
150- log .Warn ("Unable to remove temporary authorized keys file: %s: Error: %v" , tmpPath , err )
151- }
152- }()
153-
154- if setting .SSH .AuthorizedKeysBackup {
155- isExist , err := util .IsExist (fPath )
156- if err != nil {
157- log .Error ("Unable to check if %s exists. Error: %v" , fPath , err )
158- return err
159- }
160- if isExist {
161- bakPath := fmt .Sprintf ("%s_%d.gitea_bak" , fPath , time .Now ().Unix ())
162- if err = util .CopyFile (fPath , bakPath ); err != nil {
163- return err
164- }
165- }
166- }
167-
168- if err := RegeneratePublicKeys (ctx , t ); err != nil {
169- return err
170- }
171-
172- t .Close ()
173- return util .Rename (tmpPath , fPath )
174- }
175-
176122// RegeneratePublicKeys regenerates the authorized_keys file
177123func RegeneratePublicKeys (ctx context.Context , t io.StringWriter ) error {
178124 if err := db .GetEngine (ctx ).Where ("type != ?" , KeyTypePrincipal ).Iterate (new (PublicKey ), func (idx int , bean any ) (err error ) {
0 commit comments