1
0

actions.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. "errors"
  20. "fmt"
  21. "net/http"
  22. "net/url"
  23. "os/exec"
  24. "path"
  25. "path/filepath"
  26. "strings"
  27. "sync/atomic"
  28. "time"
  29. "github.com/sftpgo/sdk"
  30. "github.com/sftpgo/sdk/plugin/notifier"
  31. "github.com/drakkan/sftpgo/v2/internal/command"
  32. "github.com/drakkan/sftpgo/v2/internal/dataprovider"
  33. "github.com/drakkan/sftpgo/v2/internal/httpclient"
  34. "github.com/drakkan/sftpgo/v2/internal/logger"
  35. "github.com/drakkan/sftpgo/v2/internal/plugin"
  36. "github.com/drakkan/sftpgo/v2/internal/util"
  37. )
  38. var (
  39. errUnconfiguredAction = errors.New("no hook is configured for this action")
  40. errNoHook = errors.New("unable to execute action, no hook defined")
  41. errUnexpectedHTTResponse = errors.New("unexpected HTTP hook response code")
  42. hooksConcurrencyGuard = make(chan struct{}, 150)
  43. activeHooks atomic.Int32
  44. )
  45. func startNewHook() {
  46. activeHooks.Add(1)
  47. hooksConcurrencyGuard <- struct{}{}
  48. }
  49. func hookEnded() {
  50. activeHooks.Add(-1)
  51. <-hooksConcurrencyGuard
  52. }
  53. // ProtocolActions defines the action to execute on file operations and SSH commands
  54. type ProtocolActions struct {
  55. // Valid values are download, upload, pre-delete, delete, rename, ssh_cmd. Empty slice to disable
  56. ExecuteOn []string `json:"execute_on" mapstructure:"execute_on"`
  57. // Actions to be performed synchronously.
  58. // The pre-delete action is always executed synchronously while the other ones are asynchronous.
  59. // Executing an action synchronously means that SFTPGo will not return a result code to the client
  60. // (which is waiting for it) until your hook have completed its execution.
  61. ExecuteSync []string `json:"execute_sync" mapstructure:"execute_sync"`
  62. // Absolute path to an external program or an HTTP URL
  63. Hook string `json:"hook" mapstructure:"hook"`
  64. }
  65. var actionHandler ActionHandler = &defaultActionHandler{}
  66. // InitializeActionHandler lets the user choose an action handler implementation.
  67. //
  68. // Do NOT call this function after application initialization.
  69. func InitializeActionHandler(handler ActionHandler) {
  70. actionHandler = handler
  71. }
  72. func handleUnconfiguredPreAction(operation string) error {
  73. // for pre-delete we execute the internal handling on error, so we must return errUnconfiguredAction.
  74. // Other pre action will deny the operation on error so if we have no configuration we must return
  75. // a nil error
  76. if operation == operationPreDelete {
  77. return errUnconfiguredAction
  78. }
  79. return nil
  80. }
  81. // ExecutePreAction executes a pre-* action and returns the result
  82. func ExecutePreAction(conn *BaseConnection, operation, filePath, virtualPath string, fileSize int64, openFlags int) error {
  83. var event *notifier.FsEvent
  84. hasNotifiersPlugin := plugin.Handler.HasNotifiers()
  85. hasHook := util.Contains(Config.Actions.ExecuteOn, operation)
  86. if !hasHook && !hasNotifiersPlugin {
  87. return handleUnconfiguredPreAction(operation)
  88. }
  89. event = newActionNotification(&conn.User, operation, filePath, virtualPath, "", "", "",
  90. conn.protocol, conn.GetRemoteIP(), conn.ID, fileSize, openFlags, nil)
  91. if hasNotifiersPlugin {
  92. plugin.Handler.NotifyFsEvent(event)
  93. }
  94. if !hasHook {
  95. return handleUnconfiguredPreAction(operation)
  96. }
  97. return actionHandler.Handle(event)
  98. }
  99. // ExecuteActionNotification executes the defined hook, if any, for the specified action
  100. func ExecuteActionNotification(conn *BaseConnection, operation, filePath, virtualPath, target, virtualTarget, sshCmd string,
  101. fileSize int64, err error,
  102. ) error {
  103. hasNotifiersPlugin := plugin.Handler.HasNotifiers()
  104. hasHook := util.Contains(Config.Actions.ExecuteOn, operation)
  105. hasRules := eventManager.hasFsRules()
  106. if !hasHook && !hasNotifiersPlugin && !hasRules {
  107. return nil
  108. }
  109. notification := newActionNotification(&conn.User, operation, filePath, virtualPath, target, virtualTarget, sshCmd,
  110. conn.protocol, conn.GetRemoteIP(), conn.ID, fileSize, 0, err)
  111. if hasNotifiersPlugin {
  112. plugin.Handler.NotifyFsEvent(notification)
  113. }
  114. var errRes error
  115. if hasRules {
  116. params := EventParams{
  117. Name: notification.Username,
  118. Groups: conn.User.Groups,
  119. Event: notification.Action,
  120. Status: notification.Status,
  121. VirtualPath: notification.VirtualPath,
  122. FsPath: notification.Path,
  123. VirtualTargetPath: notification.VirtualTargetPath,
  124. FsTargetPath: notification.TargetPath,
  125. ObjectName: path.Base(notification.VirtualPath),
  126. FileSize: notification.FileSize,
  127. Protocol: notification.Protocol,
  128. IP: notification.IP,
  129. Timestamp: notification.Timestamp,
  130. Object: nil,
  131. }
  132. if err != nil {
  133. params.AddError(fmt.Errorf("%q failed: %w", params.Event, err))
  134. }
  135. errRes = eventManager.handleFsEvent(params)
  136. }
  137. if hasHook {
  138. if util.Contains(Config.Actions.ExecuteSync, operation) {
  139. if errHook := actionHandler.Handle(notification); errHook != nil {
  140. errRes = errHook
  141. }
  142. } else {
  143. go func() {
  144. startNewHook()
  145. defer hookEnded()
  146. actionHandler.Handle(notification) //nolint:errcheck
  147. }()
  148. }
  149. }
  150. return errRes
  151. }
  152. // ActionHandler handles a notification for a Protocol Action.
  153. type ActionHandler interface {
  154. Handle(notification *notifier.FsEvent) error
  155. }
  156. func newActionNotification(
  157. user *dataprovider.User,
  158. operation, filePath, virtualPath, target, virtualTarget, sshCmd, protocol, ip, sessionID string,
  159. fileSize int64,
  160. openFlags int,
  161. err error,
  162. ) *notifier.FsEvent {
  163. var bucket, endpoint string
  164. fsConfig := user.GetFsConfigForPath(virtualPath)
  165. switch fsConfig.Provider {
  166. case sdk.S3FilesystemProvider:
  167. bucket = fsConfig.S3Config.Bucket
  168. endpoint = fsConfig.S3Config.Endpoint
  169. case sdk.GCSFilesystemProvider:
  170. bucket = fsConfig.GCSConfig.Bucket
  171. case sdk.AzureBlobFilesystemProvider:
  172. bucket = fsConfig.AzBlobConfig.Container
  173. if fsConfig.AzBlobConfig.Endpoint != "" {
  174. endpoint = fsConfig.AzBlobConfig.Endpoint
  175. }
  176. case sdk.SFTPFilesystemProvider:
  177. endpoint = fsConfig.SFTPConfig.Endpoint
  178. case sdk.HTTPFilesystemProvider:
  179. endpoint = fsConfig.HTTPConfig.Endpoint
  180. }
  181. return &notifier.FsEvent{
  182. Action: operation,
  183. Username: user.Username,
  184. Path: filePath,
  185. TargetPath: target,
  186. VirtualPath: virtualPath,
  187. VirtualTargetPath: virtualTarget,
  188. SSHCmd: sshCmd,
  189. FileSize: fileSize,
  190. FsProvider: int(fsConfig.Provider),
  191. Bucket: bucket,
  192. Endpoint: endpoint,
  193. Status: getNotificationStatus(err),
  194. Protocol: protocol,
  195. IP: ip,
  196. SessionID: sessionID,
  197. OpenFlags: openFlags,
  198. Timestamp: time.Now().UnixNano(),
  199. }
  200. }
  201. type defaultActionHandler struct{}
  202. func (h *defaultActionHandler) Handle(event *notifier.FsEvent) error {
  203. if !util.Contains(Config.Actions.ExecuteOn, event.Action) {
  204. return errUnconfiguredAction
  205. }
  206. if Config.Actions.Hook == "" {
  207. logger.Warn(event.Protocol, "", "Unable to send notification, no hook is defined")
  208. return errNoHook
  209. }
  210. if strings.HasPrefix(Config.Actions.Hook, "http") {
  211. return h.handleHTTP(event)
  212. }
  213. return h.handleCommand(event)
  214. }
  215. func (h *defaultActionHandler) handleHTTP(event *notifier.FsEvent) error {
  216. u, err := url.Parse(Config.Actions.Hook)
  217. if err != nil {
  218. logger.Error(event.Protocol, "", "Invalid hook %#v for operation %#v: %v",
  219. Config.Actions.Hook, event.Action, err)
  220. return err
  221. }
  222. startTime := time.Now()
  223. respCode := 0
  224. var b bytes.Buffer
  225. _ = json.NewEncoder(&b).Encode(event)
  226. resp, err := httpclient.RetryablePost(Config.Actions.Hook, "application/json", &b)
  227. if err == nil {
  228. respCode = resp.StatusCode
  229. resp.Body.Close()
  230. if respCode != http.StatusOK {
  231. err = errUnexpectedHTTResponse
  232. }
  233. }
  234. logger.Debug(event.Protocol, "", "notified operation %q to URL: %s status code: %d, elapsed: %s err: %v",
  235. event.Action, u.Redacted(), respCode, time.Since(startTime), err)
  236. return err
  237. }
  238. func (h *defaultActionHandler) handleCommand(event *notifier.FsEvent) error {
  239. if !filepath.IsAbs(Config.Actions.Hook) {
  240. err := fmt.Errorf("invalid notification command %#v", Config.Actions.Hook)
  241. logger.Warn(event.Protocol, "", "unable to execute notification command: %v", err)
  242. return err
  243. }
  244. timeout, env, args := command.GetConfig(Config.Actions.Hook, command.HookFsActions)
  245. ctx, cancel := context.WithTimeout(context.Background(), timeout)
  246. defer cancel()
  247. cmd := exec.CommandContext(ctx, Config.Actions.Hook, args...)
  248. cmd.Env = append(env, notificationAsEnvVars(event)...)
  249. startTime := time.Now()
  250. err := cmd.Run()
  251. logger.Debug(event.Protocol, "", "executed command %#v, elapsed: %v, error: %v",
  252. Config.Actions.Hook, time.Since(startTime), err)
  253. return err
  254. }
  255. func notificationAsEnvVars(event *notifier.FsEvent) []string {
  256. return []string{
  257. fmt.Sprintf("SFTPGO_ACTION=%s", event.Action),
  258. fmt.Sprintf("SFTPGO_ACTION_USERNAME=%s", event.Username),
  259. fmt.Sprintf("SFTPGO_ACTION_PATH=%s", event.Path),
  260. fmt.Sprintf("SFTPGO_ACTION_TARGET=%s", event.TargetPath),
  261. fmt.Sprintf("SFTPGO_ACTION_VIRTUAL_PATH=%s", event.VirtualPath),
  262. fmt.Sprintf("SFTPGO_ACTION_VIRTUAL_TARGET=%s", event.VirtualTargetPath),
  263. fmt.Sprintf("SFTPGO_ACTION_SSH_CMD=%s", event.SSHCmd),
  264. fmt.Sprintf("SFTPGO_ACTION_FILE_SIZE=%d", event.FileSize),
  265. fmt.Sprintf("SFTPGO_ACTION_FS_PROVIDER=%d", event.FsProvider),
  266. fmt.Sprintf("SFTPGO_ACTION_BUCKET=%s", event.Bucket),
  267. fmt.Sprintf("SFTPGO_ACTION_ENDPOINT=%s", event.Endpoint),
  268. fmt.Sprintf("SFTPGO_ACTION_STATUS=%d", event.Status),
  269. fmt.Sprintf("SFTPGO_ACTION_PROTOCOL=%s", event.Protocol),
  270. fmt.Sprintf("SFTPGO_ACTION_IP=%s", event.IP),
  271. fmt.Sprintf("SFTPGO_ACTION_SESSION_ID=%s", event.SessionID),
  272. fmt.Sprintf("SFTPGO_ACTION_OPEN_FLAGS=%d", event.OpenFlags),
  273. fmt.Sprintf("SFTPGO_ACTION_TIMESTAMP=%d", event.Timestamp),
  274. }
  275. }
  276. func getNotificationStatus(err error) int {
  277. status := 1
  278. if err == ErrQuotaExceeded {
  279. status = 3
  280. } else if err != nil {
  281. status = 2
  282. }
  283. return status
  284. }