common.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. // Package common defines code shared among file transfer packages and protocols
  2. package common
  3. import (
  4. "context"
  5. "errors"
  6. "fmt"
  7. "net"
  8. "net/http"
  9. "net/url"
  10. "os"
  11. "os/exec"
  12. "path/filepath"
  13. "strings"
  14. "sync"
  15. "sync/atomic"
  16. "time"
  17. "github.com/pires/go-proxyproto"
  18. "github.com/drakkan/sftpgo/dataprovider"
  19. "github.com/drakkan/sftpgo/httpclient"
  20. "github.com/drakkan/sftpgo/logger"
  21. "github.com/drakkan/sftpgo/metrics"
  22. "github.com/drakkan/sftpgo/utils"
  23. )
  24. // constants
  25. const (
  26. logSender = "common"
  27. uploadLogSender = "Upload"
  28. downloadLogSender = "Download"
  29. renameLogSender = "Rename"
  30. rmdirLogSender = "Rmdir"
  31. mkdirLogSender = "Mkdir"
  32. symlinkLogSender = "Symlink"
  33. removeLogSender = "Remove"
  34. chownLogSender = "Chown"
  35. chmodLogSender = "Chmod"
  36. chtimesLogSender = "Chtimes"
  37. truncateLogSender = "Truncate"
  38. operationDownload = "download"
  39. operationUpload = "upload"
  40. operationDelete = "delete"
  41. operationPreDelete = "pre-delete"
  42. operationRename = "rename"
  43. operationSSHCmd = "ssh_cmd"
  44. chtimesFormat = "2006-01-02T15:04:05" // YYYY-MM-DDTHH:MM:SS
  45. idleTimeoutCheckInterval = 3 * time.Minute
  46. )
  47. // Stat flags
  48. const (
  49. StatAttrUIDGID = 1
  50. StatAttrPerms = 2
  51. StatAttrTimes = 4
  52. StatAttrSize = 8
  53. )
  54. // Transfer types
  55. const (
  56. TransferUpload = iota
  57. TransferDownload
  58. )
  59. // Supported protocols
  60. const (
  61. ProtocolSFTP = "SFTP"
  62. ProtocolSCP = "SCP"
  63. ProtocolSSH = "SSH"
  64. ProtocolFTP = "FTP"
  65. ProtocolWebDAV = "DAV"
  66. )
  67. // Upload modes
  68. const (
  69. UploadModeStandard = iota
  70. UploadModeAtomic
  71. UploadModeAtomicWithResume
  72. )
  73. // errors definitions
  74. var (
  75. ErrPermissionDenied = errors.New("permission denied")
  76. ErrNotExist = errors.New("no such file or directory")
  77. ErrOpUnsupported = errors.New("operation unsupported")
  78. ErrGenericFailure = errors.New("failure")
  79. ErrQuotaExceeded = errors.New("denying write due to space limit")
  80. ErrSkipPermissionsCheck = errors.New("permission check skipped")
  81. ErrConnectionDenied = errors.New("you are not allowed to connect")
  82. ErrNoBinding = errors.New("no binding configured")
  83. ErrCrtRevoked = errors.New("your certificate has been revoked")
  84. errNoTransfer = errors.New("requested transfer not found")
  85. errTransferMismatch = errors.New("transfer mismatch")
  86. )
  87. var (
  88. // Config is the configuration for the supported protocols
  89. Config Configuration
  90. // Connections is the list of active connections
  91. Connections ActiveConnections
  92. // QuotaScans is the list of active quota scans
  93. QuotaScans ActiveScans
  94. idleTimeoutTicker *time.Ticker
  95. idleTimeoutTickerDone chan bool
  96. supportedProtocols = []string{ProtocolSFTP, ProtocolSCP, ProtocolSSH, ProtocolFTP, ProtocolWebDAV}
  97. // the map key is the protocol, for each protocol we can have multiple rate limiters
  98. rateLimiters map[string][]*rateLimiter
  99. )
  100. // Initialize sets the common configuration
  101. func Initialize(c Configuration) error {
  102. Config = c
  103. Config.idleLoginTimeout = 2 * time.Minute
  104. Config.idleTimeoutAsDuration = time.Duration(Config.IdleTimeout) * time.Minute
  105. if Config.IdleTimeout > 0 {
  106. startIdleTimeoutTicker(idleTimeoutCheckInterval)
  107. }
  108. Config.defender = nil
  109. if c.DefenderConfig.Enabled {
  110. defender, err := newInMemoryDefender(&c.DefenderConfig)
  111. if err != nil {
  112. return fmt.Errorf("defender initialization error: %v", err)
  113. }
  114. logger.Info(logSender, "", "defender initialized with config %+v", c.DefenderConfig)
  115. Config.defender = defender
  116. }
  117. rateLimiters = make(map[string][]*rateLimiter)
  118. for _, rlCfg := range c.RateLimitersConfig {
  119. if rlCfg.isEnabled() {
  120. if err := rlCfg.validate(); err != nil {
  121. return fmt.Errorf("rate limiters initialization error: %v", err)
  122. }
  123. rateLimiter := rlCfg.getLimiter()
  124. for _, protocol := range rlCfg.Protocols {
  125. rateLimiters[protocol] = append(rateLimiters[protocol], rateLimiter)
  126. }
  127. }
  128. }
  129. return nil
  130. }
  131. // LimitRate blocks until all the configured rate limiters
  132. // allow one event to happen.
  133. // It returns an error if the time to wait exceeds the max
  134. // allowed delay
  135. func LimitRate(protocol, ip string) error {
  136. for _, limiter := range rateLimiters[protocol] {
  137. if err := limiter.Wait(ip); err != nil {
  138. logger.Debug(logSender, "", "protocol %v ip %v: %v", protocol, ip, err)
  139. return err
  140. }
  141. }
  142. return nil
  143. }
  144. // ReloadDefender reloads the defender's block and safe lists
  145. func ReloadDefender() error {
  146. if Config.defender == nil {
  147. return nil
  148. }
  149. return Config.defender.Reload()
  150. }
  151. // IsBanned returns true if the specified IP address is banned
  152. func IsBanned(ip string) bool {
  153. if Config.defender == nil {
  154. return false
  155. }
  156. return Config.defender.IsBanned(ip)
  157. }
  158. // GetDefenderBanTime returns the ban time for the given IP
  159. // or nil if the IP is not banned or the defender is disabled
  160. func GetDefenderBanTime(ip string) *time.Time {
  161. if Config.defender == nil {
  162. return nil
  163. }
  164. return Config.defender.GetBanTime(ip)
  165. }
  166. // Unban removes the specified IP address from the banned ones
  167. func Unban(ip string) bool {
  168. if Config.defender == nil {
  169. return false
  170. }
  171. return Config.defender.Unban(ip)
  172. }
  173. // GetDefenderScore returns the score for the given IP
  174. func GetDefenderScore(ip string) int {
  175. if Config.defender == nil {
  176. return 0
  177. }
  178. return Config.defender.GetScore(ip)
  179. }
  180. // AddDefenderEvent adds the specified defender event for the given IP
  181. func AddDefenderEvent(ip string, event HostEvent) {
  182. if Config.defender == nil {
  183. return
  184. }
  185. Config.defender.AddEvent(ip, event)
  186. }
  187. // the ticker cannot be started/stopped from multiple goroutines
  188. func startIdleTimeoutTicker(duration time.Duration) {
  189. stopIdleTimeoutTicker()
  190. idleTimeoutTicker = time.NewTicker(duration)
  191. idleTimeoutTickerDone = make(chan bool)
  192. go func() {
  193. for {
  194. select {
  195. case <-idleTimeoutTickerDone:
  196. return
  197. case <-idleTimeoutTicker.C:
  198. Connections.checkIdles()
  199. }
  200. }
  201. }()
  202. }
  203. func stopIdleTimeoutTicker() {
  204. if idleTimeoutTicker != nil {
  205. idleTimeoutTicker.Stop()
  206. idleTimeoutTickerDone <- true
  207. idleTimeoutTicker = nil
  208. }
  209. }
  210. // ActiveTransfer defines the interface for the current active transfers
  211. type ActiveTransfer interface {
  212. GetID() uint64
  213. GetType() int
  214. GetSize() int64
  215. GetVirtualPath() string
  216. GetStartTime() time.Time
  217. SignalClose()
  218. Truncate(fsPath string, size int64) (int64, error)
  219. GetRealFsPath(fsPath string) string
  220. }
  221. // ActiveConnection defines the interface for the current active connections
  222. type ActiveConnection interface {
  223. GetID() string
  224. GetUsername() string
  225. GetRemoteAddress() string
  226. GetClientVersion() string
  227. GetProtocol() string
  228. GetConnectionTime() time.Time
  229. GetLastActivity() time.Time
  230. GetCommand() string
  231. Disconnect() error
  232. AddTransfer(t ActiveTransfer)
  233. RemoveTransfer(t ActiveTransfer)
  234. GetTransfers() []ConnectionTransfer
  235. CloseFS() error
  236. }
  237. // StatAttributes defines the attributes for set stat commands
  238. type StatAttributes struct {
  239. Mode os.FileMode
  240. Atime time.Time
  241. Mtime time.Time
  242. UID int
  243. GID int
  244. Flags int
  245. Size int64
  246. }
  247. // ConnectionTransfer defines the trasfer details to expose
  248. type ConnectionTransfer struct {
  249. ID uint64 `json:"-"`
  250. OperationType string `json:"operation_type"`
  251. StartTime int64 `json:"start_time"`
  252. Size int64 `json:"size"`
  253. VirtualPath string `json:"path"`
  254. }
  255. func (t *ConnectionTransfer) getConnectionTransferAsString() string {
  256. result := ""
  257. switch t.OperationType {
  258. case operationUpload:
  259. result += "UL "
  260. case operationDownload:
  261. result += "DL "
  262. }
  263. result += fmt.Sprintf("%#v ", t.VirtualPath)
  264. if t.Size > 0 {
  265. elapsed := time.Since(utils.GetTimeFromMsecSinceEpoch(t.StartTime))
  266. speed := float64(t.Size) / float64(utils.GetTimeAsMsSinceEpoch(time.Now())-t.StartTime)
  267. result += fmt.Sprintf("Size: %#v Elapsed: %#v Speed: \"%.1f KB/s\"", utils.ByteCountIEC(t.Size),
  268. utils.GetDurationAsString(elapsed), speed)
  269. }
  270. return result
  271. }
  272. // Configuration defines configuration parameters common to all supported protocols
  273. type Configuration struct {
  274. // Maximum idle timeout as minutes. If a client is idle for a time that exceeds this setting it will be disconnected.
  275. // 0 means disabled
  276. IdleTimeout int `json:"idle_timeout" mapstructure:"idle_timeout"`
  277. // UploadMode 0 means standard, the files are uploaded directly to the requested path.
  278. // 1 means atomic: the files are uploaded to a temporary path and renamed to the requested path
  279. // when the client ends the upload. Atomic mode avoid problems such as a web server that
  280. // serves partial files when the files are being uploaded.
  281. // In atomic mode if there is an upload error the temporary file is deleted and so the requested
  282. // upload path will not contain a partial file.
  283. // 2 means atomic with resume support: as atomic but if there is an upload error the temporary
  284. // file is renamed to the requested path and not deleted, this way a client can reconnect and resume
  285. // the upload.
  286. UploadMode int `json:"upload_mode" mapstructure:"upload_mode"`
  287. // Actions to execute for SFTP file operations and SSH commands
  288. Actions ProtocolActions `json:"actions" mapstructure:"actions"`
  289. // SetstatMode 0 means "normal mode": requests for changing permissions and owner/group are executed.
  290. // 1 means "ignore mode": requests for changing permissions and owner/group are silently ignored.
  291. // 2 means "ignore mode for cloud fs": requests for changing permissions and owner/group/time are
  292. // silently ignored for cloud based filesystem such as S3, GCS, Azure Blob
  293. SetstatMode int `json:"setstat_mode" mapstructure:"setstat_mode"`
  294. // Support for HAProxy PROXY protocol.
  295. // If you are running SFTPGo behind a proxy server such as HAProxy, AWS ELB or NGNIX, you can enable
  296. // the proxy protocol. It provides a convenient way to safely transport connection information
  297. // such as a client's address across multiple layers of NAT or TCP proxies to get the real
  298. // client IP address instead of the proxy IP. Both protocol versions 1 and 2 are supported.
  299. // - 0 means disabled
  300. // - 1 means proxy protocol enabled. Proxy header will be used and requests without proxy header will be accepted.
  301. // - 2 means proxy protocol required. Proxy header will be used and requests without proxy header will be rejected.
  302. // If the proxy protocol is enabled in SFTPGo then you have to enable the protocol in your proxy configuration too,
  303. // for example for HAProxy add "send-proxy" or "send-proxy-v2" to each server configuration line.
  304. ProxyProtocol int `json:"proxy_protocol" mapstructure:"proxy_protocol"`
  305. // List of IP addresses and IP ranges allowed to send the proxy header.
  306. // If proxy protocol is set to 1 and we receive a proxy header from an IP that is not in the list then the
  307. // connection will be accepted and the header will be ignored.
  308. // If proxy protocol is set to 2 and we receive a proxy header from an IP that is not in the list then the
  309. // connection will be rejected.
  310. ProxyAllowed []string `json:"proxy_allowed" mapstructure:"proxy_allowed"`
  311. // Absolute path to an external program or an HTTP URL to invoke as soon as SFTPGo starts.
  312. // If you define an HTTP URL it will be invoked using a `GET` request.
  313. // Please note that SFTPGo services may not yet be available when this hook is run.
  314. // Leave empty do disable.
  315. StartupHook string `json:"startup_hook" mapstructure:"startup_hook"`
  316. // Absolute path to an external program or an HTTP URL to invoke after a user connects
  317. // and before he tries to login. It allows you to reject the connection based on the source
  318. // ip address. Leave empty do disable.
  319. PostConnectHook string `json:"post_connect_hook" mapstructure:"post_connect_hook"`
  320. // Maximum number of concurrent client connections. 0 means unlimited
  321. MaxTotalConnections int `json:"max_total_connections" mapstructure:"max_total_connections"`
  322. // Defender configuration
  323. DefenderConfig DefenderConfig `json:"defender" mapstructure:"defender"`
  324. // Rate limiter configurations
  325. RateLimitersConfig []RateLimiterConfig `json:"rate_limiters" mapstructure:"rate_limiters"`
  326. idleTimeoutAsDuration time.Duration
  327. idleLoginTimeout time.Duration
  328. defender Defender
  329. }
  330. // IsAtomicUploadEnabled returns true if atomic upload is enabled
  331. func (c *Configuration) IsAtomicUploadEnabled() bool {
  332. return c.UploadMode == UploadModeAtomic || c.UploadMode == UploadModeAtomicWithResume
  333. }
  334. // GetProxyListener returns a wrapper for the given listener that supports the
  335. // HAProxy Proxy Protocol or nil if the proxy protocol is not configured
  336. func (c *Configuration) GetProxyListener(listener net.Listener) (*proxyproto.Listener, error) {
  337. var proxyListener *proxyproto.Listener
  338. var err error
  339. if c.ProxyProtocol > 0 {
  340. var policyFunc func(upstream net.Addr) (proxyproto.Policy, error)
  341. if c.ProxyProtocol == 1 && len(c.ProxyAllowed) > 0 {
  342. policyFunc, err = proxyproto.LaxWhiteListPolicy(c.ProxyAllowed)
  343. if err != nil {
  344. return nil, err
  345. }
  346. }
  347. if c.ProxyProtocol == 2 {
  348. if len(c.ProxyAllowed) == 0 {
  349. policyFunc = func(upstream net.Addr) (proxyproto.Policy, error) {
  350. return proxyproto.REQUIRE, nil
  351. }
  352. } else {
  353. policyFunc, err = proxyproto.StrictWhiteListPolicy(c.ProxyAllowed)
  354. if err != nil {
  355. return nil, err
  356. }
  357. }
  358. }
  359. proxyListener = &proxyproto.Listener{
  360. Listener: listener,
  361. Policy: policyFunc,
  362. }
  363. }
  364. return proxyListener, nil
  365. }
  366. // ExecuteStartupHook runs the startup hook if defined
  367. func (c *Configuration) ExecuteStartupHook() error {
  368. if c.StartupHook == "" {
  369. return nil
  370. }
  371. if strings.HasPrefix(c.StartupHook, "http") {
  372. var url *url.URL
  373. url, err := url.Parse(c.StartupHook)
  374. if err != nil {
  375. logger.Warn(logSender, "", "Invalid startup hook %#v: %v", c.StartupHook, err)
  376. return err
  377. }
  378. startTime := time.Now()
  379. httpClient := httpclient.GetRetraybleHTTPClient()
  380. resp, err := httpClient.Get(url.String())
  381. if err != nil {
  382. logger.Warn(logSender, "", "Error executing startup hook: %v", err)
  383. return err
  384. }
  385. defer resp.Body.Close()
  386. logger.Debug(logSender, "", "Startup hook executed, elapsed: %v, response code: %v", time.Since(startTime), resp.StatusCode)
  387. return nil
  388. }
  389. if !filepath.IsAbs(c.StartupHook) {
  390. err := fmt.Errorf("invalid startup hook %#v", c.StartupHook)
  391. logger.Warn(logSender, "", "Invalid startup hook %#v", c.StartupHook)
  392. return err
  393. }
  394. startTime := time.Now()
  395. ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
  396. defer cancel()
  397. cmd := exec.CommandContext(ctx, c.StartupHook)
  398. err := cmd.Run()
  399. logger.Debug(logSender, "", "Startup hook executed, elapsed: %v, error: %v", time.Since(startTime), err)
  400. return nil
  401. }
  402. // ExecutePostConnectHook executes the post connect hook if defined
  403. func (c *Configuration) ExecutePostConnectHook(ipAddr, protocol string) error {
  404. if c.PostConnectHook == "" {
  405. return nil
  406. }
  407. if strings.HasPrefix(c.PostConnectHook, "http") {
  408. var url *url.URL
  409. url, err := url.Parse(c.PostConnectHook)
  410. if err != nil {
  411. logger.Warn(protocol, "", "Login from ip %#v denied, invalid post connect hook %#v: %v",
  412. ipAddr, c.PostConnectHook, err)
  413. return err
  414. }
  415. httpClient := httpclient.GetRetraybleHTTPClient()
  416. q := url.Query()
  417. q.Add("ip", ipAddr)
  418. q.Add("protocol", protocol)
  419. url.RawQuery = q.Encode()
  420. resp, err := httpClient.Get(url.String())
  421. if err != nil {
  422. logger.Warn(protocol, "", "Login from ip %#v denied, error executing post connect hook: %v", ipAddr, err)
  423. return err
  424. }
  425. defer resp.Body.Close()
  426. if resp.StatusCode != http.StatusOK {
  427. logger.Warn(protocol, "", "Login from ip %#v denied, post connect hook response code: %v", ipAddr, resp.StatusCode)
  428. return errUnexpectedHTTResponse
  429. }
  430. return nil
  431. }
  432. if !filepath.IsAbs(c.PostConnectHook) {
  433. err := fmt.Errorf("invalid post connect hook %#v", c.PostConnectHook)
  434. logger.Warn(protocol, "", "Login from ip %#v denied: %v", ipAddr, err)
  435. return err
  436. }
  437. ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
  438. defer cancel()
  439. cmd := exec.CommandContext(ctx, c.PostConnectHook)
  440. cmd.Env = append(os.Environ(),
  441. fmt.Sprintf("SFTPGO_CONNECTION_IP=%v", ipAddr),
  442. fmt.Sprintf("SFTPGO_CONNECTION_PROTOCOL=%v", protocol))
  443. err := cmd.Run()
  444. if err != nil {
  445. logger.Warn(protocol, "", "Login from ip %#v denied, connect hook error: %v", ipAddr, err)
  446. }
  447. return err
  448. }
  449. // SSHConnection defines an ssh connection.
  450. // Each SSH connection can open several channels for SFTP or SSH commands
  451. type SSHConnection struct {
  452. id string
  453. conn net.Conn
  454. lastActivity int64
  455. }
  456. // NewSSHConnection returns a new SSHConnection
  457. func NewSSHConnection(id string, conn net.Conn) *SSHConnection {
  458. return &SSHConnection{
  459. id: id,
  460. conn: conn,
  461. lastActivity: time.Now().UnixNano(),
  462. }
  463. }
  464. // GetID returns the ID for this SSHConnection
  465. func (c *SSHConnection) GetID() string {
  466. return c.id
  467. }
  468. // UpdateLastActivity updates last activity for this connection
  469. func (c *SSHConnection) UpdateLastActivity() {
  470. atomic.StoreInt64(&c.lastActivity, time.Now().UnixNano())
  471. }
  472. // GetLastActivity returns the last connection activity
  473. func (c *SSHConnection) GetLastActivity() time.Time {
  474. return time.Unix(0, atomic.LoadInt64(&c.lastActivity))
  475. }
  476. // Close closes the underlying network connection
  477. func (c *SSHConnection) Close() error {
  478. return c.conn.Close()
  479. }
  480. // ActiveConnections holds the currect active connections with the associated transfers
  481. type ActiveConnections struct {
  482. sync.RWMutex
  483. connections []ActiveConnection
  484. sshConnections []*SSHConnection
  485. }
  486. // GetActiveSessions returns the number of active sessions for the given username.
  487. // We return the open sessions for any protocol
  488. func (conns *ActiveConnections) GetActiveSessions(username string) int {
  489. conns.RLock()
  490. defer conns.RUnlock()
  491. numSessions := 0
  492. for _, c := range conns.connections {
  493. if c.GetUsername() == username {
  494. numSessions++
  495. }
  496. }
  497. return numSessions
  498. }
  499. // Add adds a new connection to the active ones
  500. func (conns *ActiveConnections) Add(c ActiveConnection) {
  501. conns.Lock()
  502. defer conns.Unlock()
  503. conns.connections = append(conns.connections, c)
  504. metrics.UpdateActiveConnectionsSize(len(conns.connections))
  505. logger.Debug(c.GetProtocol(), c.GetID(), "connection added, num open connections: %v", len(conns.connections))
  506. }
  507. // Swap replaces an existing connection with the given one.
  508. // This method is useful if you have to change some connection details
  509. // for example for FTP is used to update the connection once the user
  510. // authenticates
  511. func (conns *ActiveConnections) Swap(c ActiveConnection) error {
  512. conns.Lock()
  513. defer conns.Unlock()
  514. for idx, conn := range conns.connections {
  515. if conn.GetID() == c.GetID() {
  516. conn = nil
  517. conns.connections[idx] = c
  518. return nil
  519. }
  520. }
  521. return errors.New("connection to swap not found")
  522. }
  523. // Remove removes a connection from the active ones
  524. func (conns *ActiveConnections) Remove(connectionID string) {
  525. conns.Lock()
  526. defer conns.Unlock()
  527. for idx, conn := range conns.connections {
  528. if conn.GetID() == connectionID {
  529. err := conn.CloseFS()
  530. lastIdx := len(conns.connections) - 1
  531. conns.connections[idx] = conns.connections[lastIdx]
  532. conns.connections[lastIdx] = nil
  533. conns.connections = conns.connections[:lastIdx]
  534. metrics.UpdateActiveConnectionsSize(lastIdx)
  535. logger.Debug(conn.GetProtocol(), conn.GetID(), "connection removed, close fs error: %v, num open connections: %v",
  536. err, lastIdx)
  537. return
  538. }
  539. }
  540. logger.Warn(logSender, "", "connection id %#v to remove not found!", connectionID)
  541. }
  542. // Close closes an active connection.
  543. // It returns true on success
  544. func (conns *ActiveConnections) Close(connectionID string) bool {
  545. conns.RLock()
  546. result := false
  547. for _, c := range conns.connections {
  548. if c.GetID() == connectionID {
  549. defer func(conn ActiveConnection) {
  550. err := conn.Disconnect()
  551. logger.Debug(conn.GetProtocol(), conn.GetID(), "close connection requested, close err: %v", err)
  552. }(c)
  553. result = true
  554. break
  555. }
  556. }
  557. conns.RUnlock()
  558. return result
  559. }
  560. // AddSSHConnection adds a new ssh connection to the active ones
  561. func (conns *ActiveConnections) AddSSHConnection(c *SSHConnection) {
  562. conns.Lock()
  563. defer conns.Unlock()
  564. conns.sshConnections = append(conns.sshConnections, c)
  565. logger.Debug(logSender, c.GetID(), "ssh connection added, num open connections: %v", len(conns.sshConnections))
  566. }
  567. // RemoveSSHConnection removes a connection from the active ones
  568. func (conns *ActiveConnections) RemoveSSHConnection(connectionID string) {
  569. conns.Lock()
  570. defer conns.Unlock()
  571. for idx, conn := range conns.sshConnections {
  572. if conn.GetID() == connectionID {
  573. lastIdx := len(conns.sshConnections) - 1
  574. conns.sshConnections[idx] = conns.sshConnections[lastIdx]
  575. conns.sshConnections[lastIdx] = nil
  576. conns.sshConnections = conns.sshConnections[:lastIdx]
  577. logger.Debug(logSender, conn.GetID(), "ssh connection removed, num open ssh connections: %v", lastIdx)
  578. return
  579. }
  580. }
  581. logger.Warn(logSender, "", "ssh connection to remove with id %#v not found!", connectionID)
  582. }
  583. func (conns *ActiveConnections) checkIdles() {
  584. conns.RLock()
  585. for _, sshConn := range conns.sshConnections {
  586. idleTime := time.Since(sshConn.GetLastActivity())
  587. if idleTime > Config.idleTimeoutAsDuration {
  588. // we close the an ssh connection if it has no active connections associated
  589. idToMatch := fmt.Sprintf("_%v_", sshConn.GetID())
  590. toClose := true
  591. for _, conn := range conns.connections {
  592. if strings.Contains(conn.GetID(), idToMatch) {
  593. toClose = false
  594. break
  595. }
  596. }
  597. if toClose {
  598. defer func(c *SSHConnection) {
  599. err := c.Close()
  600. logger.Debug(logSender, c.GetID(), "close idle SSH connection, idle time: %v, close err: %v",
  601. time.Since(c.GetLastActivity()), err)
  602. }(sshConn)
  603. }
  604. }
  605. }
  606. for _, c := range conns.connections {
  607. idleTime := time.Since(c.GetLastActivity())
  608. isUnauthenticatedFTPUser := (c.GetProtocol() == ProtocolFTP && c.GetUsername() == "")
  609. if idleTime > Config.idleTimeoutAsDuration || (isUnauthenticatedFTPUser && idleTime > Config.idleLoginTimeout) {
  610. defer func(conn ActiveConnection, isFTPNoAuth bool) {
  611. err := conn.Disconnect()
  612. logger.Debug(conn.GetProtocol(), conn.GetID(), "close idle connection, idle time: %v, username: %#v close err: %v",
  613. time.Since(conn.GetLastActivity()), conn.GetUsername(), err)
  614. if isFTPNoAuth {
  615. ip := utils.GetIPFromRemoteAddress(c.GetRemoteAddress())
  616. logger.ConnectionFailedLog("", ip, dataprovider.LoginMethodNoAuthTryed, c.GetProtocol(), "client idle")
  617. metrics.AddNoAuthTryed()
  618. AddDefenderEvent(ip, HostEventNoLoginTried)
  619. dataprovider.ExecutePostLoginHook(&dataprovider.User{}, dataprovider.LoginMethodNoAuthTryed, ip, c.GetProtocol(),
  620. dataprovider.ErrNoAuthTryed)
  621. }
  622. }(c, isUnauthenticatedFTPUser)
  623. }
  624. }
  625. conns.RUnlock()
  626. }
  627. // IsNewConnectionAllowed returns false if the maximum number of concurrent allowed connections is exceeded
  628. func (conns *ActiveConnections) IsNewConnectionAllowed() bool {
  629. if Config.MaxTotalConnections == 0 {
  630. return true
  631. }
  632. conns.RLock()
  633. defer conns.RUnlock()
  634. return len(conns.connections) < Config.MaxTotalConnections
  635. }
  636. // GetStats returns stats for active connections
  637. func (conns *ActiveConnections) GetStats() []*ConnectionStatus {
  638. conns.RLock()
  639. defer conns.RUnlock()
  640. stats := make([]*ConnectionStatus, 0, len(conns.connections))
  641. for _, c := range conns.connections {
  642. stat := &ConnectionStatus{
  643. Username: c.GetUsername(),
  644. ConnectionID: c.GetID(),
  645. ClientVersion: c.GetClientVersion(),
  646. RemoteAddress: c.GetRemoteAddress(),
  647. ConnectionTime: utils.GetTimeAsMsSinceEpoch(c.GetConnectionTime()),
  648. LastActivity: utils.GetTimeAsMsSinceEpoch(c.GetLastActivity()),
  649. Protocol: c.GetProtocol(),
  650. Command: c.GetCommand(),
  651. Transfers: c.GetTransfers(),
  652. }
  653. stats = append(stats, stat)
  654. }
  655. return stats
  656. }
  657. // ConnectionStatus returns the status for an active connection
  658. type ConnectionStatus struct {
  659. // Logged in username
  660. Username string `json:"username"`
  661. // Unique identifier for the connection
  662. ConnectionID string `json:"connection_id"`
  663. // client's version string
  664. ClientVersion string `json:"client_version,omitempty"`
  665. // Remote address for this connection
  666. RemoteAddress string `json:"remote_address"`
  667. // Connection time as unix timestamp in milliseconds
  668. ConnectionTime int64 `json:"connection_time"`
  669. // Last activity as unix timestamp in milliseconds
  670. LastActivity int64 `json:"last_activity"`
  671. // Protocol for this connection
  672. Protocol string `json:"protocol"`
  673. // active uploads/downloads
  674. Transfers []ConnectionTransfer `json:"active_transfers,omitempty"`
  675. // SSH command or WebDAV method
  676. Command string `json:"command,omitempty"`
  677. }
  678. // GetConnectionDuration returns the connection duration as string
  679. func (c *ConnectionStatus) GetConnectionDuration() string {
  680. elapsed := time.Since(utils.GetTimeFromMsecSinceEpoch(c.ConnectionTime))
  681. return utils.GetDurationAsString(elapsed)
  682. }
  683. // GetConnectionInfo returns connection info.
  684. // Protocol,Client Version and RemoteAddress are returned.
  685. func (c *ConnectionStatus) GetConnectionInfo() string {
  686. var result strings.Builder
  687. result.WriteString(fmt.Sprintf("%v. Client: %#v From: %#v", c.Protocol, c.ClientVersion, c.RemoteAddress))
  688. if c.Command == "" {
  689. return result.String()
  690. }
  691. switch c.Protocol {
  692. case ProtocolSSH, ProtocolFTP:
  693. result.WriteString(fmt.Sprintf(". Command: %#v", c.Command))
  694. case ProtocolWebDAV:
  695. result.WriteString(fmt.Sprintf(". Method: %#v", c.Command))
  696. }
  697. return result.String()
  698. }
  699. // GetTransfersAsString returns the active transfers as string
  700. func (c *ConnectionStatus) GetTransfersAsString() string {
  701. result := ""
  702. for _, t := range c.Transfers {
  703. if result != "" {
  704. result += ". "
  705. }
  706. result += t.getConnectionTransferAsString()
  707. }
  708. return result
  709. }
  710. // ActiveQuotaScan defines an active quota scan for a user home dir
  711. type ActiveQuotaScan struct {
  712. // Username to which the quota scan refers
  713. Username string `json:"username"`
  714. // quota scan start time as unix timestamp in milliseconds
  715. StartTime int64 `json:"start_time"`
  716. }
  717. // ActiveVirtualFolderQuotaScan defines an active quota scan for a virtual folder
  718. type ActiveVirtualFolderQuotaScan struct {
  719. // folder name to which the quota scan refers
  720. Name string `json:"name"`
  721. // quota scan start time as unix timestamp in milliseconds
  722. StartTime int64 `json:"start_time"`
  723. }
  724. // ActiveScans holds the active quota scans
  725. type ActiveScans struct {
  726. sync.RWMutex
  727. UserHomeScans []ActiveQuotaScan
  728. FolderScans []ActiveVirtualFolderQuotaScan
  729. }
  730. // GetUsersQuotaScans returns the active quota scans for users home directories
  731. func (s *ActiveScans) GetUsersQuotaScans() []ActiveQuotaScan {
  732. s.RLock()
  733. defer s.RUnlock()
  734. scans := make([]ActiveQuotaScan, len(s.UserHomeScans))
  735. copy(scans, s.UserHomeScans)
  736. return scans
  737. }
  738. // AddUserQuotaScan adds a user to the ones with active quota scans.
  739. // Returns false if the user has a quota scan already running
  740. func (s *ActiveScans) AddUserQuotaScan(username string) bool {
  741. s.Lock()
  742. defer s.Unlock()
  743. for _, scan := range s.UserHomeScans {
  744. if scan.Username == username {
  745. return false
  746. }
  747. }
  748. s.UserHomeScans = append(s.UserHomeScans, ActiveQuotaScan{
  749. Username: username,
  750. StartTime: utils.GetTimeAsMsSinceEpoch(time.Now()),
  751. })
  752. return true
  753. }
  754. // RemoveUserQuotaScan removes a user from the ones with active quota scans.
  755. // Returns false if the user has no active quota scans
  756. func (s *ActiveScans) RemoveUserQuotaScan(username string) bool {
  757. s.Lock()
  758. defer s.Unlock()
  759. indexToRemove := -1
  760. for i, scan := range s.UserHomeScans {
  761. if scan.Username == username {
  762. indexToRemove = i
  763. break
  764. }
  765. }
  766. if indexToRemove >= 0 {
  767. s.UserHomeScans[indexToRemove] = s.UserHomeScans[len(s.UserHomeScans)-1]
  768. s.UserHomeScans = s.UserHomeScans[:len(s.UserHomeScans)-1]
  769. return true
  770. }
  771. return false
  772. }
  773. // GetVFoldersQuotaScans returns the active quota scans for virtual folders
  774. func (s *ActiveScans) GetVFoldersQuotaScans() []ActiveVirtualFolderQuotaScan {
  775. s.RLock()
  776. defer s.RUnlock()
  777. scans := make([]ActiveVirtualFolderQuotaScan, len(s.FolderScans))
  778. copy(scans, s.FolderScans)
  779. return scans
  780. }
  781. // AddVFolderQuotaScan adds a virtual folder to the ones with active quota scans.
  782. // Returns false if the folder has a quota scan already running
  783. func (s *ActiveScans) AddVFolderQuotaScan(folderName string) bool {
  784. s.Lock()
  785. defer s.Unlock()
  786. for _, scan := range s.FolderScans {
  787. if scan.Name == folderName {
  788. return false
  789. }
  790. }
  791. s.FolderScans = append(s.FolderScans, ActiveVirtualFolderQuotaScan{
  792. Name: folderName,
  793. StartTime: utils.GetTimeAsMsSinceEpoch(time.Now()),
  794. })
  795. return true
  796. }
  797. // RemoveVFolderQuotaScan removes a folder from the ones with active quota scans.
  798. // Returns false if the folder has no active quota scans
  799. func (s *ActiveScans) RemoveVFolderQuotaScan(folderName string) bool {
  800. s.Lock()
  801. defer s.Unlock()
  802. indexToRemove := -1
  803. for i, scan := range s.FolderScans {
  804. if scan.Name == folderName {
  805. indexToRemove = i
  806. break
  807. }
  808. }
  809. if indexToRemove >= 0 {
  810. s.FolderScans[indexToRemove] = s.FolderScans[len(s.FolderScans)-1]
  811. s.FolderScans = s.FolderScans[:len(s.FolderScans)-1]
  812. return true
  813. }
  814. return false
  815. }