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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright 2021 The Gitea Authors. 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 models
  5. import (
  6. "bufio"
  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() error {
  40. return rewriteAllPrincipalKeys(db.GetEngine(db.DefaultContext))
  41. }
  42. func rewriteAllPrincipalKeys(e db.Engine) error {
  43. // Don't rewrite key if internal server
  44. if setting.SSH.StartBuiltinServer || !setting.SSH.CreateAuthorizedPrincipalsFile {
  45. return nil
  46. }
  47. sshOpLocker.Lock()
  48. defer sshOpLocker.Unlock()
  49. if setting.SSH.RootPath != "" {
  50. // First of ensure that the RootPath is present, and if not make it with 0700 permissions
  51. // This of course doesn't guarantee that this is the right directory for authorized_keys
  52. // but at least if it's supposed to be this directory and it doesn't exist and we're the
  53. // right user it will at least be created properly.
  54. err := os.MkdirAll(setting.SSH.RootPath, 0o700)
  55. if err != nil {
  56. log.Error("Unable to MkdirAll(%s): %v", setting.SSH.RootPath, err)
  57. return err
  58. }
  59. }
  60. fPath := filepath.Join(setting.SSH.RootPath, authorizedPrincipalsFile)
  61. tmpPath := fPath + ".tmp"
  62. t, err := os.OpenFile(tmpPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o600)
  63. if err != nil {
  64. return err
  65. }
  66. defer func() {
  67. t.Close()
  68. os.Remove(tmpPath)
  69. }()
  70. if setting.SSH.AuthorizedPrincipalsBackup {
  71. isExist, err := util.IsExist(fPath)
  72. if err != nil {
  73. log.Error("Unable to check if %s exists. Error: %v", fPath, err)
  74. return err
  75. }
  76. if isExist {
  77. bakPath := fmt.Sprintf("%s_%d.gitea_bak", fPath, time.Now().Unix())
  78. if err = util.CopyFile(fPath, bakPath); err != nil {
  79. return err
  80. }
  81. }
  82. }
  83. if err := regeneratePrincipalKeys(e, t); err != nil {
  84. return err
  85. }
  86. t.Close()
  87. return util.Rename(tmpPath, fPath)
  88. }
  89. // RegeneratePrincipalKeys regenerates the authorized_principals file
  90. func RegeneratePrincipalKeys(t io.StringWriter) error {
  91. return regeneratePrincipalKeys(db.GetEngine(db.DefaultContext), t)
  92. }
  93. func regeneratePrincipalKeys(e db.Engine, t io.StringWriter) error {
  94. if err := e.Where("type = ?", KeyTypePrincipal).Iterate(new(PublicKey), func(idx int, bean interface{}) (err error) {
  95. _, err = t.WriteString((bean.(*PublicKey)).AuthorizedString())
  96. return err
  97. }); err != nil {
  98. return err
  99. }
  100. fPath := filepath.Join(setting.SSH.RootPath, authorizedPrincipalsFile)
  101. isExist, err := util.IsExist(fPath)
  102. if err != nil {
  103. log.Error("Unable to check if %s exists. Error: %v", fPath, err)
  104. return err
  105. }
  106. if isExist {
  107. f, err := os.Open(fPath)
  108. if err != nil {
  109. return err
  110. }
  111. scanner := bufio.NewScanner(f)
  112. for scanner.Scan() {
  113. line := scanner.Text()
  114. if strings.HasPrefix(line, tplCommentPrefix) {
  115. scanner.Scan()
  116. continue
  117. }
  118. _, err = t.WriteString(line + "\n")
  119. if err != nil {
  120. f.Close()
  121. return err
  122. }
  123. }
  124. f.Close()
  125. }
  126. return nil
  127. }