cacheduser.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Copyright (C) 2019-2023 Nicola Murino
  2. //
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU Affero General Public License as published
  5. // by the Free Software Foundation, version 3.
  6. //
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU Affero General Public License for more details.
  11. //
  12. // You should have received a copy of the GNU Affero General Public License
  13. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. package dataprovider
  15. import (
  16. "sync"
  17. "time"
  18. "github.com/drakkan/webdav"
  19. "github.com/drakkan/sftpgo/v2/internal/logger"
  20. "github.com/drakkan/sftpgo/v2/internal/util"
  21. )
  22. var (
  23. webDAVUsersCache *usersCache
  24. )
  25. func init() {
  26. webDAVUsersCache = &usersCache{
  27. users: map[string]CachedUser{},
  28. }
  29. }
  30. // InitializeWebDAVUserCache initializes the cache for webdav users
  31. func InitializeWebDAVUserCache(maxSize int) {
  32. webDAVUsersCache = &usersCache{
  33. users: map[string]CachedUser{},
  34. maxSize: maxSize,
  35. }
  36. }
  37. // CachedUser adds fields useful for caching to a SFTPGo user
  38. type CachedUser struct {
  39. User User
  40. Expiration time.Time
  41. Password string
  42. LockSystem webdav.LockSystem
  43. }
  44. // IsExpired returns true if the cached user is expired
  45. func (c *CachedUser) IsExpired() bool {
  46. if c.Expiration.IsZero() {
  47. return false
  48. }
  49. return c.Expiration.Before(time.Now())
  50. }
  51. type usersCache struct {
  52. sync.RWMutex
  53. users map[string]CachedUser
  54. maxSize int
  55. }
  56. func (cache *usersCache) updateLastLogin(username string) {
  57. cache.Lock()
  58. defer cache.Unlock()
  59. if cachedUser, ok := cache.users[username]; ok {
  60. cachedUser.User.LastLogin = util.GetTimeAsMsSinceEpoch(time.Now())
  61. cache.users[username] = cachedUser
  62. }
  63. }
  64. // swapWebDAVUser updates an existing cached user with the specified one
  65. // preserving the lock fs if possible
  66. // FIXME: this could be racy in rare cases
  67. func (cache *usersCache) swap(userRef *User, plainPassword string) {
  68. user := userRef.getACopy()
  69. err := user.LoadAndApplyGroupSettings()
  70. cache.Lock()
  71. defer cache.Unlock()
  72. if cachedUser, ok := cache.users[user.Username]; ok {
  73. if err != nil {
  74. providerLog(logger.LevelDebug, "unable to load group settings, for user %q, removing from cache, err :%v",
  75. user.Username, err)
  76. delete(cache.users, user.Username)
  77. return
  78. }
  79. if plainPassword != "" {
  80. cachedUser.Password = plainPassword
  81. } else {
  82. if cachedUser.User.Password != user.Password {
  83. providerLog(logger.LevelDebug, "current password different from the cached one for user %q, removing from cache",
  84. user.Username)
  85. // the password changed, the cached user is no longer valid
  86. delete(cache.users, user.Username)
  87. return
  88. }
  89. }
  90. if cachedUser.User.isFsEqual(&user) {
  91. // the updated user has the same fs as the cached one, we can preserve the lock filesystem
  92. providerLog(logger.LevelDebug, "current password and fs unchanged for for user %q, swap cached one",
  93. user.Username)
  94. cachedUser.User = user
  95. cache.users[user.Username] = cachedUser
  96. } else {
  97. // filesystem changed, the cached user is no longer valid
  98. providerLog(logger.LevelDebug, "current fs different from the cached one for user %q, removing from cache",
  99. user.Username)
  100. delete(cache.users, user.Username)
  101. }
  102. }
  103. }
  104. func (cache *usersCache) add(cachedUser *CachedUser) {
  105. cache.Lock()
  106. defer cache.Unlock()
  107. if cache.maxSize > 0 && len(cache.users) >= cache.maxSize {
  108. var userToRemove string
  109. var expirationTime time.Time
  110. for k, v := range cache.users {
  111. if userToRemove == "" {
  112. userToRemove = k
  113. expirationTime = v.Expiration
  114. continue
  115. }
  116. expireTime := v.Expiration
  117. if !expireTime.IsZero() && expireTime.Before(expirationTime) {
  118. userToRemove = k
  119. expirationTime = expireTime
  120. }
  121. }
  122. delete(cache.users, userToRemove)
  123. }
  124. if cachedUser.User.Username != "" {
  125. cache.users[cachedUser.User.Username] = *cachedUser
  126. }
  127. }
  128. func (cache *usersCache) remove(username string) {
  129. cache.Lock()
  130. defer cache.Unlock()
  131. delete(cache.users, username)
  132. }
  133. func (cache *usersCache) get(username string) (*CachedUser, bool) {
  134. cache.RLock()
  135. defer cache.RUnlock()
  136. cachedUser, ok := cache.users[username]
  137. if !ok {
  138. return nil, false
  139. }
  140. return &cachedUser, true
  141. }
  142. // CacheWebDAVUser add a user to the WebDAV cache
  143. func CacheWebDAVUser(cachedUser *CachedUser) {
  144. webDAVUsersCache.add(cachedUser)
  145. }
  146. // GetCachedWebDAVUser returns a previously cached WebDAV user
  147. func GetCachedWebDAVUser(username string) (*CachedUser, bool) {
  148. return webDAVUsersCache.get(username)
  149. }
  150. // RemoveCachedWebDAVUser removes a cached WebDAV user
  151. func RemoveCachedWebDAVUser(username string) {
  152. webDAVUsersCache.remove(username)
  153. }