You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

v21.go 1.2 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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/go-xorm/xorm"
  11. )
  12. const (
  13. tplCommentPrefix = `# gitea public key`
  14. tplPublicKey = tplCommentPrefix + "\n" + `command="%s serv key-%d --config='%s'",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty %s` + "\n"
  15. )
  16. func useNewPublickeyFormat(x *xorm.Engine) error {
  17. fpath := filepath.Join(setting.SSH.RootPath, "authorized_keys")
  18. tmpPath := fpath + ".tmp"
  19. f, err := os.OpenFile(tmpPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
  20. if err != nil {
  21. return err
  22. }
  23. defer func() {
  24. f.Close()
  25. os.Remove(tmpPath)
  26. }()
  27. type PublicKey struct {
  28. ID int64
  29. Content string
  30. }
  31. err = x.Iterate(new(PublicKey), func(idx int, bean interface{}) (err error) {
  32. key := bean.(*PublicKey)
  33. _, err = f.WriteString(fmt.Sprintf(tplPublicKey, setting.AppPath, key.ID, setting.CustomConf, key.Content))
  34. return err
  35. })
  36. if err != nil {
  37. return err
  38. }
  39. f.Close()
  40. if err = os.Rename(tmpPath, fpath); err != nil {
  41. return err
  42. }
  43. return nil
  44. }