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

ssh_key_authorized_principals.go 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package asymkey
  4. import (
  5. "bufio"
  6. "context"
  7. "fmt"
  8. "io"
  9. "os"
  10. "path/filepath"
  11. "strings"
  12. "time"
  13. asymkey_model "code.gitea.io/gitea/models/asymkey"
  14. "code.gitea.io/gitea/models/db"
  15. "code.gitea.io/gitea/modules/log"
  16. "code.gitea.io/gitea/modules/setting"
  17. "code.gitea.io/gitea/modules/util"
  18. )
  19. // This file contains functions for creating authorized_principals files
  20. //
  21. // There is a dependence on the database within RewriteAllPrincipalKeys & RegeneratePrincipalKeys
  22. // The sshOpLocker is used from ssh_key_authorized_keys.go
  23. const (
  24. authorizedPrincipalsFile = "authorized_principals"
  25. tplCommentPrefix = `# gitea public key`
  26. )
  27. // RewriteAllPrincipalKeys removes any authorized principal and rewrite all keys from database again.
  28. // Note: db.GetEngine(ctx).Iterate does not get latest data after insert/delete, so we have to call this function
  29. // outside any session scope independently.
  30. func RewriteAllPrincipalKeys(ctx context.Context) error {
  31. // Don't rewrite key if internal server
  32. if setting.SSH.StartBuiltinServer || !setting.SSH.CreateAuthorizedPrincipalsFile {
  33. return nil
  34. }
  35. return asymkey_model.WithSSHOpLocker(func() error {
  36. return rewriteAllPrincipalKeys(ctx)
  37. })
  38. }
  39. func rewriteAllPrincipalKeys(ctx context.Context) error {
  40. if setting.SSH.RootPath != "" {
  41. // First of ensure that the RootPath is present, and if not make it with 0700 permissions
  42. // This of course doesn't guarantee that this is the right directory for authorized_keys
  43. // but at least if it's supposed to be this directory and it doesn't exist and we're the
  44. // right user it will at least be created properly.
  45. err := os.MkdirAll(setting.SSH.RootPath, 0o700)
  46. if err != nil {
  47. log.Error("Unable to MkdirAll(%s): %v", setting.SSH.RootPath, err)
  48. return err
  49. }
  50. }
  51. fPath := filepath.Join(setting.SSH.RootPath, authorizedPrincipalsFile)
  52. tmpPath := fPath + ".tmp"
  53. t, err := os.OpenFile(tmpPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o600)
  54. if err != nil {
  55. return err
  56. }
  57. defer func() {
  58. t.Close()
  59. os.Remove(tmpPath)
  60. }()
  61. if setting.SSH.AuthorizedPrincipalsBackup {
  62. isExist, err := util.IsExist(fPath)
  63. if err != nil {
  64. log.Error("Unable to check if %s exists. Error: %v", fPath, err)
  65. return err
  66. }
  67. if isExist {
  68. bakPath := fmt.Sprintf("%s_%d.gitea_bak", fPath, time.Now().Unix())
  69. if err = util.CopyFile(fPath, bakPath); err != nil {
  70. return err
  71. }
  72. }
  73. }
  74. if err := regeneratePrincipalKeys(ctx, t); err != nil {
  75. return err
  76. }
  77. t.Close()
  78. return util.Rename(tmpPath, fPath)
  79. }
  80. func regeneratePrincipalKeys(ctx context.Context, t io.StringWriter) error {
  81. if err := db.GetEngine(ctx).Where("type = ?", asymkey_model.KeyTypePrincipal).Iterate(new(asymkey_model.PublicKey), func(idx int, bean any) (err error) {
  82. _, err = t.WriteString((bean.(*asymkey_model.PublicKey)).AuthorizedString())
  83. return err
  84. }); err != nil {
  85. return err
  86. }
  87. fPath := filepath.Join(setting.SSH.RootPath, authorizedPrincipalsFile)
  88. isExist, err := util.IsExist(fPath)
  89. if err != nil {
  90. log.Error("Unable to check if %s exists. Error: %v", fPath, err)
  91. return err
  92. }
  93. if isExist {
  94. f, err := os.Open(fPath)
  95. if err != nil {
  96. return err
  97. }
  98. scanner := bufio.NewScanner(f)
  99. for scanner.Scan() {
  100. line := scanner.Text()
  101. if strings.HasPrefix(line, tplCommentPrefix) {
  102. scanner.Scan()
  103. continue
  104. }
  105. _, err = t.WriteString(line + "\n")
  106. if err != nil {
  107. f.Close()
  108. return err
  109. }
  110. }
  111. f.Close()
  112. }
  113. return nil
  114. }