dataretention.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. // Copyright (C) 2019-2022 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 common
  15. import (
  16. "bytes"
  17. "context"
  18. "encoding/json"
  19. "fmt"
  20. "net/http"
  21. "net/url"
  22. "os"
  23. "os/exec"
  24. "path"
  25. "path/filepath"
  26. "strings"
  27. "sync"
  28. "time"
  29. mail "github.com/xhit/go-simple-mail/v2"
  30. "github.com/drakkan/sftpgo/v2/internal/command"
  31. "github.com/drakkan/sftpgo/v2/internal/dataprovider"
  32. "github.com/drakkan/sftpgo/v2/internal/httpclient"
  33. "github.com/drakkan/sftpgo/v2/internal/logger"
  34. "github.com/drakkan/sftpgo/v2/internal/smtp"
  35. "github.com/drakkan/sftpgo/v2/internal/util"
  36. )
  37. // RetentionCheckNotification defines the supported notification methods for a retention check result
  38. type RetentionCheckNotification = string
  39. // Supported notification methods
  40. const (
  41. // notify results using the defined "data_retention_hook"
  42. RetentionCheckNotificationHook = "Hook"
  43. // notify results by email
  44. RetentionCheckNotificationEmail = "Email"
  45. )
  46. var (
  47. // RetentionChecks is the list of active retention checks
  48. RetentionChecks ActiveRetentionChecks
  49. )
  50. // ActiveRetentionChecks holds the active retention checks
  51. type ActiveRetentionChecks struct {
  52. sync.RWMutex
  53. Checks []RetentionCheck
  54. }
  55. // Get returns the active retention checks
  56. func (c *ActiveRetentionChecks) Get() []RetentionCheck {
  57. c.RLock()
  58. defer c.RUnlock()
  59. checks := make([]RetentionCheck, 0, len(c.Checks))
  60. for _, check := range c.Checks {
  61. foldersCopy := make([]dataprovider.FolderRetention, len(check.Folders))
  62. copy(foldersCopy, check.Folders)
  63. notificationsCopy := make([]string, len(check.Notifications))
  64. copy(notificationsCopy, check.Notifications)
  65. checks = append(checks, RetentionCheck{
  66. Username: check.Username,
  67. StartTime: check.StartTime,
  68. Notifications: notificationsCopy,
  69. Email: check.Email,
  70. Folders: foldersCopy,
  71. })
  72. }
  73. return checks
  74. }
  75. // Add a new retention check, returns nil if a retention check for the given
  76. // username is already active. The returned result can be used to start the check
  77. func (c *ActiveRetentionChecks) Add(check RetentionCheck, user *dataprovider.User) *RetentionCheck {
  78. c.Lock()
  79. defer c.Unlock()
  80. for _, val := range c.Checks {
  81. if val.Username == user.Username {
  82. return nil
  83. }
  84. }
  85. // we silently ignore file patterns
  86. user.Filters.FilePatterns = nil
  87. conn := NewBaseConnection("", "", "", "", *user)
  88. conn.SetProtocol(ProtocolDataRetention)
  89. conn.ID = fmt.Sprintf("data_retention_%v", user.Username)
  90. check.Username = user.Username
  91. check.StartTime = util.GetTimeAsMsSinceEpoch(time.Now())
  92. check.conn = conn
  93. check.updateUserPermissions()
  94. c.Checks = append(c.Checks, check)
  95. return &check
  96. }
  97. // remove a user from the ones with active retention checks
  98. // and returns true if the user is removed
  99. func (c *ActiveRetentionChecks) remove(username string) bool {
  100. c.Lock()
  101. defer c.Unlock()
  102. for idx, check := range c.Checks {
  103. if check.Username == username {
  104. lastIdx := len(c.Checks) - 1
  105. c.Checks[idx] = c.Checks[lastIdx]
  106. c.Checks = c.Checks[:lastIdx]
  107. return true
  108. }
  109. }
  110. return false
  111. }
  112. type folderRetentionCheckResult struct {
  113. Path string `json:"path"`
  114. Retention int `json:"retention"`
  115. DeletedFiles int `json:"deleted_files"`
  116. DeletedSize int64 `json:"deleted_size"`
  117. Elapsed time.Duration `json:"-"`
  118. Info string `json:"info,omitempty"`
  119. Error string `json:"error,omitempty"`
  120. }
  121. // RetentionCheck defines an active retention check
  122. type RetentionCheck struct {
  123. // Username to which the retention check refers
  124. Username string `json:"username"`
  125. // retention check start time as unix timestamp in milliseconds
  126. StartTime int64 `json:"start_time"`
  127. // affected folders
  128. Folders []dataprovider.FolderRetention `json:"folders"`
  129. // how cleanup results will be notified
  130. Notifications []RetentionCheckNotification `json:"notifications,omitempty"`
  131. // email to use if the notification method is set to email
  132. Email string `json:"email,omitempty"`
  133. // Cleanup results
  134. results []folderRetentionCheckResult `json:"-"`
  135. conn *BaseConnection
  136. }
  137. // Validate returns an error if the specified folders are not valid
  138. func (c *RetentionCheck) Validate() error {
  139. folderPaths := make(map[string]bool)
  140. nothingToDo := true
  141. for idx := range c.Folders {
  142. f := &c.Folders[idx]
  143. if err := f.Validate(); err != nil {
  144. return err
  145. }
  146. if f.Retention > 0 {
  147. nothingToDo = false
  148. }
  149. if _, ok := folderPaths[f.Path]; ok {
  150. return util.NewValidationError(fmt.Sprintf("duplicated folder path %#v", f.Path))
  151. }
  152. folderPaths[f.Path] = true
  153. }
  154. if nothingToDo {
  155. return util.NewValidationError("nothing to delete!")
  156. }
  157. for _, notification := range c.Notifications {
  158. switch notification {
  159. case RetentionCheckNotificationEmail:
  160. if !smtp.IsEnabled() {
  161. return util.NewValidationError("in order to notify results via email you must configure an SMTP server")
  162. }
  163. if c.Email == "" {
  164. return util.NewValidationError("in order to notify results via email you must add a valid email address to your profile")
  165. }
  166. case RetentionCheckNotificationHook:
  167. if Config.DataRetentionHook == "" {
  168. return util.NewValidationError("in order to notify results via hook you must define a data_retention_hook")
  169. }
  170. default:
  171. return util.NewValidationError(fmt.Sprintf("invalid notification %#v", notification))
  172. }
  173. }
  174. return nil
  175. }
  176. func (c *RetentionCheck) updateUserPermissions() {
  177. for _, folder := range c.Folders {
  178. if folder.IgnoreUserPermissions {
  179. c.conn.User.Permissions[folder.Path] = []string{dataprovider.PermAny}
  180. }
  181. }
  182. }
  183. func (c *RetentionCheck) getFolderRetention(folderPath string) (dataprovider.FolderRetention, error) {
  184. dirsForPath := util.GetDirsForVirtualPath(folderPath)
  185. for _, dirPath := range dirsForPath {
  186. for _, folder := range c.Folders {
  187. if folder.Path == dirPath {
  188. return folder, nil
  189. }
  190. }
  191. }
  192. return dataprovider.FolderRetention{}, fmt.Errorf("unable to find folder retention for %#v", folderPath)
  193. }
  194. func (c *RetentionCheck) removeFile(virtualPath string, info os.FileInfo) error {
  195. fs, fsPath, err := c.conn.GetFsAndResolvedPath(virtualPath)
  196. if err != nil {
  197. return err
  198. }
  199. return c.conn.RemoveFile(fs, fsPath, virtualPath, info)
  200. }
  201. func (c *RetentionCheck) cleanupFolder(folderPath string) error {
  202. deleteFilesPerms := []string{dataprovider.PermDelete, dataprovider.PermDeleteFiles}
  203. startTime := time.Now()
  204. result := folderRetentionCheckResult{
  205. Path: folderPath,
  206. }
  207. defer func() {
  208. c.results = append(c.results, result)
  209. }()
  210. if !c.conn.User.HasPerm(dataprovider.PermListItems, folderPath) || !c.conn.User.HasAnyPerm(deleteFilesPerms, folderPath) {
  211. result.Elapsed = time.Since(startTime)
  212. result.Info = "data retention check skipped: no permissions"
  213. c.conn.Log(logger.LevelInfo, "user %#v does not have permissions to check retention on %#v, retention check skipped",
  214. c.conn.User.Username, folderPath)
  215. return nil
  216. }
  217. folderRetention, err := c.getFolderRetention(folderPath)
  218. if err != nil {
  219. result.Elapsed = time.Since(startTime)
  220. result.Error = "unable to get folder retention"
  221. c.conn.Log(logger.LevelError, "unable to get folder retention for path %#v", folderPath)
  222. return err
  223. }
  224. result.Retention = folderRetention.Retention
  225. if folderRetention.Retention == 0 {
  226. result.Elapsed = time.Since(startTime)
  227. result.Info = "data retention check skipped: retention is set to 0"
  228. c.conn.Log(logger.LevelDebug, "retention check skipped for folder %#v, retention is set to 0", folderPath)
  229. return nil
  230. }
  231. c.conn.Log(logger.LevelDebug, "start retention check for folder %#v, retention: %v hours, delete empty dirs? %v, ignore user perms? %v",
  232. folderPath, folderRetention.Retention, folderRetention.DeleteEmptyDirs, folderRetention.IgnoreUserPermissions)
  233. files, err := c.conn.ListDir(folderPath)
  234. if err != nil {
  235. result.Elapsed = time.Since(startTime)
  236. if err == c.conn.GetNotExistError() {
  237. result.Info = "data retention check skipped, folder does not exist"
  238. c.conn.Log(logger.LevelDebug, "folder %#v does not exist, retention check skipped", folderPath)
  239. return nil
  240. }
  241. result.Error = fmt.Sprintf("unable to list directory %#v", folderPath)
  242. c.conn.Log(logger.LevelError, result.Error)
  243. return err
  244. }
  245. for _, info := range files {
  246. virtualPath := path.Join(folderPath, info.Name())
  247. if info.IsDir() {
  248. if err := c.cleanupFolder(virtualPath); err != nil {
  249. result.Elapsed = time.Since(startTime)
  250. result.Error = fmt.Sprintf("unable to check folder: %v", err)
  251. c.conn.Log(logger.LevelError, "unable to cleanup folder %#v: %v", virtualPath, err)
  252. return err
  253. }
  254. } else {
  255. retentionTime := info.ModTime().Add(time.Duration(folderRetention.Retention) * time.Hour)
  256. if retentionTime.Before(time.Now()) {
  257. if err := c.removeFile(virtualPath, info); err != nil {
  258. result.Elapsed = time.Since(startTime)
  259. result.Error = fmt.Sprintf("unable to remove file %#v: %v", virtualPath, err)
  260. c.conn.Log(logger.LevelError, "unable to remove file %#v, retention %v: %v",
  261. virtualPath, retentionTime, err)
  262. return err
  263. }
  264. c.conn.Log(logger.LevelDebug, "removed file %#v, modification time: %v, retention: %v hours, retention time: %v",
  265. virtualPath, info.ModTime(), folderRetention.Retention, retentionTime)
  266. result.DeletedFiles++
  267. result.DeletedSize += info.Size()
  268. }
  269. }
  270. }
  271. if folderRetention.DeleteEmptyDirs {
  272. c.checkEmptyDirRemoval(folderPath)
  273. }
  274. result.Elapsed = time.Since(startTime)
  275. c.conn.Log(logger.LevelDebug, "retention check completed for folder %#v, deleted files: %v, deleted size: %v bytes",
  276. folderPath, result.DeletedFiles, result.DeletedSize)
  277. return nil
  278. }
  279. func (c *RetentionCheck) checkEmptyDirRemoval(folderPath string) {
  280. if folderPath == "/" {
  281. return
  282. }
  283. for _, folder := range c.Folders {
  284. if folderPath == folder.Path {
  285. return
  286. }
  287. }
  288. if c.conn.User.HasAnyPerm([]string{
  289. dataprovider.PermDelete,
  290. dataprovider.PermDeleteDirs,
  291. }, path.Dir(folderPath),
  292. ) {
  293. files, err := c.conn.ListDir(folderPath)
  294. if err == nil && len(files) == 0 {
  295. err = c.conn.RemoveDir(folderPath)
  296. c.conn.Log(logger.LevelDebug, "tryed to remove empty dir %#v, error: %v", folderPath, err)
  297. }
  298. }
  299. }
  300. // Start starts the retention check
  301. func (c *RetentionCheck) Start() error {
  302. c.conn.Log(logger.LevelInfo, "retention check started")
  303. defer RetentionChecks.remove(c.conn.User.Username)
  304. defer c.conn.CloseFS() //nolint:errcheck
  305. startTime := time.Now()
  306. for _, folder := range c.Folders {
  307. if folder.Retention > 0 {
  308. if err := c.cleanupFolder(folder.Path); err != nil {
  309. c.conn.Log(logger.LevelError, "retention check failed, unable to cleanup folder %#v", folder.Path)
  310. c.sendNotifications(time.Since(startTime), err)
  311. return err
  312. }
  313. }
  314. }
  315. c.conn.Log(logger.LevelInfo, "retention check completed")
  316. c.sendNotifications(time.Since(startTime), nil)
  317. return nil
  318. }
  319. func (c *RetentionCheck) sendNotifications(elapsed time.Duration, err error) {
  320. for _, notification := range c.Notifications {
  321. switch notification {
  322. case RetentionCheckNotificationEmail:
  323. c.sendEmailNotification(err) //nolint:errcheck
  324. case RetentionCheckNotificationHook:
  325. c.sendHookNotification(elapsed, err) //nolint:errcheck
  326. }
  327. }
  328. }
  329. func (c *RetentionCheck) sendEmailNotification(errCheck error) error {
  330. params := EventParams{}
  331. if len(c.results) > 0 || errCheck != nil {
  332. params.retentionChecks = append(params.retentionChecks, executedRetentionCheck{
  333. Username: c.conn.User.Username,
  334. ActionName: "Retention check",
  335. Results: c.results,
  336. })
  337. }
  338. var files []mail.File
  339. f, err := params.getRetentionReportsAsMailAttachment()
  340. if err != nil {
  341. c.conn.Log(logger.LevelError, "unable to get retention report as mail attachment: %v", err)
  342. return err
  343. }
  344. f.Name = "retention-report.zip"
  345. files = append(files, f)
  346. startTime := time.Now()
  347. var subject string
  348. if errCheck == nil {
  349. subject = fmt.Sprintf("Successful retention check for user %q", c.conn.User.Username)
  350. } else {
  351. subject = fmt.Sprintf("Retention check failed for user %q", c.conn.User.Username)
  352. }
  353. body := "Further details attached."
  354. err = smtp.SendEmail([]string{c.Email}, subject, body, smtp.EmailContentTypeTextPlain, files...)
  355. if err != nil {
  356. c.conn.Log(logger.LevelError, "unable to notify retention check result via email: %v, elapsed: %v", err,
  357. time.Since(startTime))
  358. return err
  359. }
  360. c.conn.Log(logger.LevelInfo, "retention check result successfully notified via email, elapsed: %v", time.Since(startTime))
  361. return nil
  362. }
  363. func (c *RetentionCheck) sendHookNotification(elapsed time.Duration, errCheck error) error {
  364. startNewHook()
  365. defer hookEnded()
  366. data := make(map[string]any)
  367. totalDeletedFiles := 0
  368. totalDeletedSize := int64(0)
  369. for _, result := range c.results {
  370. totalDeletedFiles += result.DeletedFiles
  371. totalDeletedSize += result.DeletedSize
  372. }
  373. data["username"] = c.conn.User.Username
  374. data["start_time"] = c.StartTime
  375. data["elapsed"] = elapsed.Milliseconds()
  376. if errCheck == nil {
  377. data["status"] = 1
  378. } else {
  379. data["status"] = 0
  380. }
  381. data["total_deleted_files"] = totalDeletedFiles
  382. data["total_deleted_size"] = totalDeletedSize
  383. data["details"] = c.results
  384. jsonData, _ := json.Marshal(data)
  385. startTime := time.Now()
  386. if strings.HasPrefix(Config.DataRetentionHook, "http") {
  387. var url *url.URL
  388. url, err := url.Parse(Config.DataRetentionHook)
  389. if err != nil {
  390. c.conn.Log(logger.LevelError, "invalid data retention hook %#v: %v", Config.DataRetentionHook, err)
  391. return err
  392. }
  393. respCode := 0
  394. resp, err := httpclient.RetryablePost(url.String(), "application/json", bytes.NewBuffer(jsonData))
  395. if err == nil {
  396. respCode = resp.StatusCode
  397. resp.Body.Close()
  398. if respCode != http.StatusOK {
  399. err = errUnexpectedHTTResponse
  400. }
  401. }
  402. c.conn.Log(logger.LevelDebug, "notified result to URL: %#v, status code: %v, elapsed: %v err: %v",
  403. url.Redacted(), respCode, time.Since(startTime), err)
  404. return err
  405. }
  406. if !filepath.IsAbs(Config.DataRetentionHook) {
  407. err := fmt.Errorf("invalid data retention hook %#v", Config.DataRetentionHook)
  408. c.conn.Log(logger.LevelError, "%v", err)
  409. return err
  410. }
  411. timeout, env, args := command.GetConfig(Config.DataRetentionHook, command.HookDataRetention)
  412. ctx, cancel := context.WithTimeout(context.Background(), timeout)
  413. defer cancel()
  414. cmd := exec.CommandContext(ctx, Config.DataRetentionHook, args...)
  415. cmd.Env = append(env,
  416. fmt.Sprintf("SFTPGO_DATA_RETENTION_RESULT=%v", string(jsonData)))
  417. err := cmd.Run()
  418. c.conn.Log(logger.LevelDebug, "notified result using command: %v, elapsed: %v err: %v",
  419. Config.DataRetentionHook, time.Since(startTime), err)
  420. return err
  421. }