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 4.0KB

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