Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

v21.go 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright 2017 Gitea. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package migrations
  5. import (
  6. "fmt"
  7. "os"
  8. "path/filepath"
  9. "code.gitea.io/gitea/modules/setting"
  10. "github.com/Unknwon/com"
  11. "github.com/go-xorm/xorm"
  12. )
  13. const (
  14. tplCommentPrefix = `# gitea public key`
  15. tplPublicKey = tplCommentPrefix + "\n" + `command="%s serv key-%d --config='%s'",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty %s` + "\n"
  16. )
  17. func useNewPublickeyFormat(x *xorm.Engine) error {
  18. fpath := filepath.Join(setting.SSH.RootPath, "authorized_keys")
  19. if !com.IsExist(fpath) {
  20. return nil
  21. }
  22. tmpPath := fpath + ".tmp"
  23. f, err := os.OpenFile(tmpPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
  24. if err != nil {
  25. return err
  26. }
  27. defer func() {
  28. f.Close()
  29. os.Remove(tmpPath)
  30. }()
  31. type PublicKey struct {
  32. ID int64
  33. Content string
  34. }
  35. err = x.Iterate(new(PublicKey), func(idx int, bean interface{}) (err error) {
  36. key := bean.(*PublicKey)
  37. _, err = f.WriteString(fmt.Sprintf(tplPublicKey, setting.AppPath, key.ID, setting.CustomConf, key.Content))
  38. return err
  39. })
  40. if err != nil {
  41. return err
  42. }
  43. f.Close()
  44. return os.Rename(tmpPath, fpath)
  45. }