common.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  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. )
  98. // Initialize sets the common configuration
  99. func Initialize(c Configuration) error {
  100. Config = c
  101. Config.idleLoginTimeout = 2 * time.Minute
  102. Config.idleTimeoutAsDuration = time.Duration(Config.IdleTimeout) * time.Minute
  103. if Config.IdleTimeout > 0 {
  104. startIdleTimeoutTicker(idleTimeoutCheckInterval)
  105. }
  106. Config.defender = nil
  107. if c.DefenderConfig.Enabled {
  108. defender, err := newInMemoryDefender(&c.DefenderConfig)
  109. if err != nil {
  110. return fmt.Errorf("defender initialization error: %v", err)
  111. }
  112. logger.Info(logSender, "", "defender initialized with config %+v", c.DefenderConfig)
  113. Config.defender = defender
  114. }
  115. return nil
  116. }
  117. // IsBanned returns true if the specified IP address is banned
  118. func IsBanned(ip string) bool {
  119. if Config.defender == nil {
  120. return false
  121. }
  122. return Config.defender.IsBanned(ip)
  123. }
  124. // GetDefenderBanTime returns the ban time for the given IP
  125. // or nil if the IP is not banned or the defender is disabled
  126. func GetDefenderBanTime(ip string) *time.Time {
  127. if Config.defender == nil {
  128. return nil
  129. }
  130. return Config.defender.GetBanTime(ip)
  131. }
  132. // Unban removes the specified IP address from the banned ones
  133. func Unban(ip string) bool {
  134. if Config.defender == nil {
  135. return false
  136. }
  137. return Config.defender.Unban(ip)
  138. }
  139. // GetDefenderScore returns the score for the given IP
  140. func GetDefenderScore(ip string) int {
  141. if Config.defender == nil {
  142. return 0
  143. }
  144. return Config.defender.GetScore(ip)
  145. }
  146. // AddDefenderEvent adds the specified defender event for the given IP
  147. func AddDefenderEvent(ip string, event HostEvent) {
  148. if Config.defender == nil {
  149. return
  150. }
  151. Config.defender.AddEvent(ip, event)
  152. }
  153. func startIdleTimeoutTicker(duration time.Duration) {
  154. stopIdleTimeoutTicker()
  155. idleTimeoutTicker = time.NewTicker(duration)
  156. idleTimeoutTickerDone = make(chan bool)
  157. go func() {
  158. for {
  159. select {
  160. case <-idleTimeoutTickerDone:
  161. return
  162. case <-idleTimeoutTicker.C:
  163. Connections.checkIdles()
  164. }
  165. }
  166. }()
  167. }
  168. func stopIdleTimeoutTicker() {
  169. if idleTimeoutTicker != nil {
  170. idleTimeoutTicker.Stop()
  171. idleTimeoutTickerDone <- true
  172. idleTimeoutTicker = nil
  173. }
  174. }
  175. // ActiveTransfer defines the interface for the current active transfers
  176. type ActiveTransfer interface {
  177. GetID() uint64
  178. GetType() int
  179. GetSize() int64
  180. GetVirtualPath() string
  181. GetStartTime() time.Time
  182. SignalClose()
  183. Truncate(fsPath string, size int64) (int64, error)
  184. GetRealFsPath(fsPath string) string
  185. }
  186. // ActiveConnection defines the interface for the current active connections
  187. type ActiveConnection interface {
  188. GetID() string
  189. GetUsername() string
  190. GetRemoteAddress() string
  191. GetClientVersion() string
  192. GetProtocol() string
  193. GetConnectionTime() time.Time
  194. GetLastActivity() time.Time
  195. GetCommand() string
  196. Disconnect() error
  197. AddTransfer(t ActiveTransfer)
  198. RemoveTransfer(t ActiveTransfer)
  199. GetTransfers() []ConnectionTransfer
  200. CloseFS() error
  201. }
  202. // StatAttributes defines the attributes for set stat commands
  203. type StatAttributes struct {
  204. Mode os.FileMode
  205. Atime time.Time
  206. Mtime time.Time
  207. UID int
  208. GID int
  209. Flags int
  210. Size int64
  211. }
  212. // ConnectionTransfer defines the trasfer details to expose
  213. type ConnectionTransfer struct {
  214. ID uint64 `json:"-"`
  215. OperationType string `json:"operation_type"`
  216. StartTime int64 `json:"start_time"`
  217. Size int64 `json:"size"`
  218. VirtualPath string `json:"path"`
  219. }
  220. func (t *ConnectionTransfer) getConnectionTransferAsString() string {
  221. result := ""
  222. switch t.OperationType {
  223. case operationUpload:
  224. result += "UL "
  225. case operationDownload:
  226. result += "DL "
  227. }
  228. result += fmt.Sprintf("%#v ", t.VirtualPath)
  229. if t.Size > 0 {
  230. elapsed := time.Since(utils.GetTimeFromMsecSinceEpoch(t.StartTime))
  231. speed := float64(t.Size) / float64(utils.GetTimeAsMsSinceEpoch(time.Now())-t.StartTime)
  232. result += fmt.Sprintf("Size: %#v Elapsed: %#v Speed: \"%.1f KB/s\"", utils.ByteCountSI(t.Size),
  233. utils.GetDurationAsString(elapsed), speed)
  234. }
  235. return result
  236. }
  237. // Configuration defines configuration parameters common to all supported protocols
  238. type Configuration struct {
  239. // Maximum idle timeout as minutes. If a client is idle for a time that exceeds this setting it will be disconnected.
  240. // 0 means disabled
  241. IdleTimeout int `json:"idle_timeout" mapstructure:"idle_timeout"`
  242. // UploadMode 0 means standard, the files are uploaded directly to the requested path.
  243. // 1 means atomic: the files are uploaded to a temporary path and renamed to the requested path
  244. // when the client ends the upload. Atomic mode avoid problems such as a web server that
  245. // serves partial files when the files are being uploaded.
  246. // In atomic mode if there is an upload error the temporary file is deleted and so the requested
  247. // upload path will not contain a partial file.
  248. // 2 means atomic with resume support: as atomic but if there is an upload error the temporary
  249. // file is renamed to the requested path and not deleted, this way a client can reconnect and resume
  250. // the upload.
  251. UploadMode int `json:"upload_mode" mapstructure:"upload_mode"`
  252. // Actions to execute for SFTP file operations and SSH commands
  253. Actions ProtocolActions `json:"actions" mapstructure:"actions"`
  254. // SetstatMode 0 means "normal mode": requests for changing permissions and owner/group are executed.
  255. // 1 means "ignore mode": requests for changing permissions and owner/group are silently ignored.
  256. // 2 means "ignore mode for cloud fs": requests for changing permissions and owner/group/time are
  257. // silently ignored for cloud based filesystem such as S3, GCS, Azure Blob
  258. SetstatMode int `json:"setstat_mode" mapstructure:"setstat_mode"`
  259. // Support for HAProxy PROXY protocol.
  260. // If you are running SFTPGo behind a proxy server such as HAProxy, AWS ELB or NGNIX, you can enable
  261. // the proxy protocol. It provides a convenient way to safely transport connection information
  262. // such as a client's address across multiple layers of NAT or TCP proxies to get the real
  263. // client IP address instead of the proxy IP. Both protocol versions 1 and 2 are supported.
  264. // - 0 means disabled
  265. // - 1 means proxy protocol enabled. Proxy header will be used and requests without proxy header will be accepted.
  266. // - 2 means proxy protocol required. Proxy header will be used and requests without proxy header will be rejected.
  267. // If the proxy protocol is enabled in SFTPGo then you have to enable the protocol in your proxy configuration too,
  268. // for example for HAProxy add "send-proxy" or "send-proxy-v2" to each server configuration line.
  269. ProxyProtocol int `json:"proxy_protocol" mapstructure:"proxy_protocol"`
  270. // List of IP addresses and IP ranges allowed to send the proxy header.
  271. // If proxy protocol is set to 1 and we receive a proxy header from an IP that is not in the list then the
  272. // connection will be accepted and the header will be ignored.
  273. // If proxy protocol is set to 2 and we receive a proxy header from an IP that is not in the list then the
  274. // connection will be rejected.
  275. ProxyAllowed []string `json:"proxy_allowed" mapstructure:"proxy_allowed"`
  276. // Absolute path to an external program or an HTTP URL to invoke after a user connects
  277. // and before he tries to login. It allows you to reject the connection based on the source
  278. // ip address. Leave empty do disable.
  279. PostConnectHook string `json:"post_connect_hook" mapstructure:"post_connect_hook"`
  280. // Maximum number of concurrent client connections. 0 means unlimited
  281. MaxTotalConnections int `json:"max_total_connections" mapstructure:"max_total_connections"`
  282. // Defender configuration
  283. DefenderConfig DefenderConfig `json:"defender" mapstructure:"defender"`
  284. idleTimeoutAsDuration time.Duration
  285. idleLoginTimeout time.Duration
  286. defender Defender
  287. }
  288. // IsAtomicUploadEnabled returns true if atomic upload is enabled
  289. func (c *Configuration) IsAtomicUploadEnabled() bool {
  290. return c.UploadMode == UploadModeAtomic || c.UploadMode == UploadModeAtomicWithResume
  291. }
  292. // GetProxyListener returns a wrapper for the given listener that supports the
  293. // HAProxy Proxy Protocol or nil if the proxy protocol is not configured
  294. func (c *Configuration) GetProxyListener(listener net.Listener) (*proxyproto.Listener, error) {
  295. var proxyListener *proxyproto.Listener
  296. var err error
  297. if c.ProxyProtocol > 0 {
  298. var policyFunc func(upstream net.Addr) (proxyproto.Policy, error)
  299. if c.ProxyProtocol == 1 && len(c.ProxyAllowed) > 0 {
  300. policyFunc, err = proxyproto.LaxWhiteListPolicy(c.ProxyAllowed)
  301. if err != nil {
  302. return nil, err
  303. }
  304. }
  305. if c.ProxyProtocol == 2 {
  306. if len(c.ProxyAllowed) == 0 {
  307. policyFunc = func(upstream net.Addr) (proxyproto.Policy, error) {
  308. return proxyproto.REQUIRE, nil
  309. }
  310. } else {
  311. policyFunc, err = proxyproto.StrictWhiteListPolicy(c.ProxyAllowed)
  312. if err != nil {
  313. return nil, err
  314. }
  315. }
  316. }
  317. proxyListener = &proxyproto.Listener{
  318. Listener: listener,
  319. Policy: policyFunc,
  320. }
  321. }
  322. return proxyListener, nil
  323. }
  324. // ExecutePostConnectHook executes the post connect hook if defined
  325. func (c *Configuration) ExecutePostConnectHook(ipAddr, protocol string) error {
  326. if len(c.PostConnectHook) == 0 {
  327. return nil
  328. }
  329. if strings.HasPrefix(c.PostConnectHook, "http") {
  330. var url *url.URL
  331. url, err := url.Parse(c.PostConnectHook)
  332. if err != nil {
  333. logger.Warn(protocol, "", "Login from ip %#v denied, invalid post connect hook %#v: %v",
  334. ipAddr, c.PostConnectHook, err)
  335. return err
  336. }
  337. httpClient := httpclient.GetHTTPClient()
  338. q := url.Query()
  339. q.Add("ip", ipAddr)
  340. q.Add("protocol", protocol)
  341. url.RawQuery = q.Encode()
  342. resp, err := httpClient.Get(url.String())
  343. if err != nil {
  344. logger.Warn(protocol, "", "Login from ip %#v denied, error executing post connect hook: %v", ipAddr, err)
  345. return err
  346. }
  347. defer resp.Body.Close()
  348. if resp.StatusCode != http.StatusOK {
  349. logger.Warn(protocol, "", "Login from ip %#v denied, post connect hook response code: %v", ipAddr, resp.StatusCode)
  350. return errUnexpectedHTTResponse
  351. }
  352. return nil
  353. }
  354. if !filepath.IsAbs(c.PostConnectHook) {
  355. err := fmt.Errorf("invalid post connect hook %#v", c.PostConnectHook)
  356. logger.Warn(protocol, "", "Login from ip %#v denied: %v", ipAddr, err)
  357. return err
  358. }
  359. ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
  360. defer cancel()
  361. cmd := exec.CommandContext(ctx, c.PostConnectHook)
  362. cmd.Env = append(os.Environ(),
  363. fmt.Sprintf("SFTPGO_CONNECTION_IP=%v", ipAddr),
  364. fmt.Sprintf("SFTPGO_CONNECTION_PROTOCOL=%v", protocol))
  365. err := cmd.Run()
  366. if err != nil {
  367. logger.Warn(protocol, "", "Login from ip %#v denied, connect hook error: %v", ipAddr, err)
  368. }
  369. return err
  370. }
  371. // SSHConnection defines an ssh connection.
  372. // Each SSH connection can open several channels for SFTP or SSH commands
  373. type SSHConnection struct {
  374. id string
  375. conn net.Conn
  376. lastActivity int64
  377. }
  378. // NewSSHConnection returns a new SSHConnection
  379. func NewSSHConnection(id string, conn net.Conn) *SSHConnection {
  380. return &SSHConnection{
  381. id: id,
  382. conn: conn,
  383. lastActivity: time.Now().UnixNano(),
  384. }
  385. }
  386. // GetID returns the ID for this SSHConnection
  387. func (c *SSHConnection) GetID() string {
  388. return c.id
  389. }
  390. // UpdateLastActivity updates last activity for this connection
  391. func (c *SSHConnection) UpdateLastActivity() {
  392. atomic.StoreInt64(&c.lastActivity, time.Now().UnixNano())
  393. }
  394. // GetLastActivity returns the last connection activity
  395. func (c *SSHConnection) GetLastActivity() time.Time {
  396. return time.Unix(0, atomic.LoadInt64(&c.lastActivity))
  397. }
  398. // Close closes the underlying network connection
  399. func (c *SSHConnection) Close() error {
  400. return c.conn.Close()
  401. }
  402. // ActiveConnections holds the currect active connections with the associated transfers
  403. type ActiveConnections struct {
  404. sync.RWMutex
  405. connections []ActiveConnection
  406. sshConnections []*SSHConnection
  407. }
  408. // GetActiveSessions returns the number of active sessions for the given username.
  409. // We return the open sessions for any protocol
  410. func (conns *ActiveConnections) GetActiveSessions(username string) int {
  411. conns.RLock()
  412. defer conns.RUnlock()
  413. numSessions := 0
  414. for _, c := range conns.connections {
  415. if c.GetUsername() == username {
  416. numSessions++
  417. }
  418. }
  419. return numSessions
  420. }
  421. // Add adds a new connection to the active ones
  422. func (conns *ActiveConnections) Add(c ActiveConnection) {
  423. conns.Lock()
  424. defer conns.Unlock()
  425. conns.connections = append(conns.connections, c)
  426. metrics.UpdateActiveConnectionsSize(len(conns.connections))
  427. logger.Debug(c.GetProtocol(), c.GetID(), "connection added, num open connections: %v", len(conns.connections))
  428. }
  429. // Swap replaces an existing connection with the given one.
  430. // This method is useful if you have to change some connection details
  431. // for example for FTP is used to update the connection once the user
  432. // authenticates
  433. func (conns *ActiveConnections) Swap(c ActiveConnection) error {
  434. conns.Lock()
  435. defer conns.Unlock()
  436. for idx, conn := range conns.connections {
  437. if conn.GetID() == c.GetID() {
  438. conn = nil
  439. conns.connections[idx] = c
  440. return nil
  441. }
  442. }
  443. return errors.New("connection to swap not found")
  444. }
  445. // Remove removes a connection from the active ones
  446. func (conns *ActiveConnections) Remove(connectionID string) {
  447. conns.Lock()
  448. defer conns.Unlock()
  449. for idx, conn := range conns.connections {
  450. if conn.GetID() == connectionID {
  451. err := conn.CloseFS()
  452. lastIdx := len(conns.connections) - 1
  453. conns.connections[idx] = conns.connections[lastIdx]
  454. conns.connections[lastIdx] = nil
  455. conns.connections = conns.connections[:lastIdx]
  456. metrics.UpdateActiveConnectionsSize(lastIdx)
  457. logger.Debug(conn.GetProtocol(), conn.GetID(), "connection removed, close fs error: %v, num open connections: %v",
  458. err, lastIdx)
  459. return
  460. }
  461. }
  462. logger.Warn(logSender, "", "connection id %#v to remove not found!", connectionID)
  463. }
  464. // Close closes an active connection.
  465. // It returns true on success
  466. func (conns *ActiveConnections) Close(connectionID string) bool {
  467. conns.RLock()
  468. result := false
  469. for _, c := range conns.connections {
  470. if c.GetID() == connectionID {
  471. defer func(conn ActiveConnection) {
  472. err := conn.Disconnect()
  473. logger.Debug(conn.GetProtocol(), conn.GetID(), "close connection requested, close err: %v", err)
  474. }(c)
  475. result = true
  476. break
  477. }
  478. }
  479. conns.RUnlock()
  480. return result
  481. }
  482. // AddSSHConnection adds a new ssh connection to the active ones
  483. func (conns *ActiveConnections) AddSSHConnection(c *SSHConnection) {
  484. conns.Lock()
  485. defer conns.Unlock()
  486. conns.sshConnections = append(conns.sshConnections, c)
  487. logger.Debug(logSender, c.GetID(), "ssh connection added, num open connections: %v", len(conns.sshConnections))
  488. }
  489. // RemoveSSHConnection removes a connection from the active ones
  490. func (conns *ActiveConnections) RemoveSSHConnection(connectionID string) {
  491. conns.Lock()
  492. defer conns.Unlock()
  493. for idx, conn := range conns.sshConnections {
  494. if conn.GetID() == connectionID {
  495. lastIdx := len(conns.sshConnections) - 1
  496. conns.sshConnections[idx] = conns.sshConnections[lastIdx]
  497. conns.sshConnections[lastIdx] = nil
  498. conns.sshConnections = conns.sshConnections[:lastIdx]
  499. logger.Debug(logSender, conn.GetID(), "ssh connection removed, num open ssh connections: %v", lastIdx)
  500. return
  501. }
  502. }
  503. logger.Warn(logSender, "", "ssh connection to remove with id %#v not found!", connectionID)
  504. }
  505. func (conns *ActiveConnections) checkIdles() {
  506. conns.RLock()
  507. for _, sshConn := range conns.sshConnections {
  508. idleTime := time.Since(sshConn.GetLastActivity())
  509. if idleTime > Config.idleTimeoutAsDuration {
  510. // we close the an ssh connection if it has no active connections associated
  511. idToMatch := fmt.Sprintf("_%v_", sshConn.GetID())
  512. toClose := true
  513. for _, conn := range conns.connections {
  514. if strings.Contains(conn.GetID(), idToMatch) {
  515. toClose = false
  516. break
  517. }
  518. }
  519. if toClose {
  520. defer func(c *SSHConnection) {
  521. err := c.Close()
  522. logger.Debug(logSender, c.GetID(), "close idle SSH connection, idle time: %v, close err: %v",
  523. time.Since(c.GetLastActivity()), err)
  524. }(sshConn)
  525. }
  526. }
  527. }
  528. for _, c := range conns.connections {
  529. idleTime := time.Since(c.GetLastActivity())
  530. isUnauthenticatedFTPUser := (c.GetProtocol() == ProtocolFTP && len(c.GetUsername()) == 0)
  531. if idleTime > Config.idleTimeoutAsDuration || (isUnauthenticatedFTPUser && idleTime > Config.idleLoginTimeout) {
  532. defer func(conn ActiveConnection, isFTPNoAuth bool) {
  533. err := conn.Disconnect()
  534. logger.Debug(conn.GetProtocol(), conn.GetID(), "close idle connection, idle time: %v, username: %#v close err: %v",
  535. time.Since(conn.GetLastActivity()), conn.GetUsername(), err)
  536. if isFTPNoAuth {
  537. ip := utils.GetIPFromRemoteAddress(c.GetRemoteAddress())
  538. logger.ConnectionFailedLog("", ip, dataprovider.LoginMethodNoAuthTryed, c.GetProtocol(), "client idle")
  539. metrics.AddNoAuthTryed()
  540. AddDefenderEvent(ip, HostEventNoLoginTried)
  541. dataprovider.ExecutePostLoginHook("", dataprovider.LoginMethodNoAuthTryed, ip, c.GetProtocol(),
  542. dataprovider.ErrNoAuthTryed)
  543. }
  544. }(c, isUnauthenticatedFTPUser)
  545. }
  546. }
  547. conns.RUnlock()
  548. }
  549. // IsNewConnectionAllowed returns false if the maximum number of concurrent allowed connections is exceeded
  550. func (conns *ActiveConnections) IsNewConnectionAllowed() bool {
  551. if Config.MaxTotalConnections == 0 {
  552. return true
  553. }
  554. conns.RLock()
  555. defer conns.RUnlock()
  556. return len(conns.connections) < Config.MaxTotalConnections
  557. }
  558. // GetStats returns stats for active connections
  559. func (conns *ActiveConnections) GetStats() []ConnectionStatus {
  560. conns.RLock()
  561. defer conns.RUnlock()
  562. stats := make([]ConnectionStatus, 0, len(conns.connections))
  563. for _, c := range conns.connections {
  564. stat := ConnectionStatus{
  565. Username: c.GetUsername(),
  566. ConnectionID: c.GetID(),
  567. ClientVersion: c.GetClientVersion(),
  568. RemoteAddress: c.GetRemoteAddress(),
  569. ConnectionTime: utils.GetTimeAsMsSinceEpoch(c.GetConnectionTime()),
  570. LastActivity: utils.GetTimeAsMsSinceEpoch(c.GetLastActivity()),
  571. Protocol: c.GetProtocol(),
  572. Command: c.GetCommand(),
  573. Transfers: c.GetTransfers(),
  574. }
  575. stats = append(stats, stat)
  576. }
  577. return stats
  578. }
  579. // ConnectionStatus returns the status for an active connection
  580. type ConnectionStatus struct {
  581. // Logged in username
  582. Username string `json:"username"`
  583. // Unique identifier for the connection
  584. ConnectionID string `json:"connection_id"`
  585. // client's version string
  586. ClientVersion string `json:"client_version,omitempty"`
  587. // Remote address for this connection
  588. RemoteAddress string `json:"remote_address"`
  589. // Connection time as unix timestamp in milliseconds
  590. ConnectionTime int64 `json:"connection_time"`
  591. // Last activity as unix timestamp in milliseconds
  592. LastActivity int64 `json:"last_activity"`
  593. // Protocol for this connection
  594. Protocol string `json:"protocol"`
  595. // active uploads/downloads
  596. Transfers []ConnectionTransfer `json:"active_transfers,omitempty"`
  597. // SSH command or WebDAV method
  598. Command string `json:"command,omitempty"`
  599. }
  600. // GetConnectionDuration returns the connection duration as string
  601. func (c ConnectionStatus) GetConnectionDuration() string {
  602. elapsed := time.Since(utils.GetTimeFromMsecSinceEpoch(c.ConnectionTime))
  603. return utils.GetDurationAsString(elapsed)
  604. }
  605. // GetConnectionInfo returns connection info.
  606. // Protocol,Client Version and RemoteAddress are returned.
  607. func (c ConnectionStatus) GetConnectionInfo() string {
  608. var result strings.Builder
  609. result.WriteString(fmt.Sprintf("%v. Client: %#v From: %#v", c.Protocol, c.ClientVersion, c.RemoteAddress))
  610. if c.Command == "" {
  611. return result.String()
  612. }
  613. switch c.Protocol {
  614. case ProtocolSSH, ProtocolFTP:
  615. result.WriteString(fmt.Sprintf(". Command: %#v", c.Command))
  616. case ProtocolWebDAV:
  617. result.WriteString(fmt.Sprintf(". Method: %#v", c.Command))
  618. }
  619. return result.String()
  620. }
  621. // GetTransfersAsString returns the active transfers as string
  622. func (c ConnectionStatus) GetTransfersAsString() string {
  623. result := ""
  624. for _, t := range c.Transfers {
  625. if len(result) > 0 {
  626. result += ". "
  627. }
  628. result += t.getConnectionTransferAsString()
  629. }
  630. return result
  631. }
  632. // ActiveQuotaScan defines an active quota scan for a user home dir
  633. type ActiveQuotaScan struct {
  634. // Username to which the quota scan refers
  635. Username string `json:"username"`
  636. // quota scan start time as unix timestamp in milliseconds
  637. StartTime int64 `json:"start_time"`
  638. }
  639. // ActiveVirtualFolderQuotaScan defines an active quota scan for a virtual folder
  640. type ActiveVirtualFolderQuotaScan struct {
  641. // folder path to which the quota scan refers
  642. MappedPath string `json:"mapped_path"`
  643. // quota scan start time as unix timestamp in milliseconds
  644. StartTime int64 `json:"start_time"`
  645. }
  646. // ActiveScans holds the active quota scans
  647. type ActiveScans struct {
  648. sync.RWMutex
  649. UserHomeScans []ActiveQuotaScan
  650. FolderScans []ActiveVirtualFolderQuotaScan
  651. }
  652. // GetUsersQuotaScans returns the active quota scans for users home directories
  653. func (s *ActiveScans) GetUsersQuotaScans() []ActiveQuotaScan {
  654. s.RLock()
  655. defer s.RUnlock()
  656. scans := make([]ActiveQuotaScan, len(s.UserHomeScans))
  657. copy(scans, s.UserHomeScans)
  658. return scans
  659. }
  660. // AddUserQuotaScan adds a user to the ones with active quota scans.
  661. // Returns false if the user has a quota scan already running
  662. func (s *ActiveScans) AddUserQuotaScan(username string) bool {
  663. s.Lock()
  664. defer s.Unlock()
  665. for _, scan := range s.UserHomeScans {
  666. if scan.Username == username {
  667. return false
  668. }
  669. }
  670. s.UserHomeScans = append(s.UserHomeScans, ActiveQuotaScan{
  671. Username: username,
  672. StartTime: utils.GetTimeAsMsSinceEpoch(time.Now()),
  673. })
  674. return true
  675. }
  676. // RemoveUserQuotaScan removes a user from the ones with active quota scans.
  677. // Returns false if the user has no active quota scans
  678. func (s *ActiveScans) RemoveUserQuotaScan(username string) bool {
  679. s.Lock()
  680. defer s.Unlock()
  681. indexToRemove := -1
  682. for i, scan := range s.UserHomeScans {
  683. if scan.Username == username {
  684. indexToRemove = i
  685. break
  686. }
  687. }
  688. if indexToRemove >= 0 {
  689. s.UserHomeScans[indexToRemove] = s.UserHomeScans[len(s.UserHomeScans)-1]
  690. s.UserHomeScans = s.UserHomeScans[:len(s.UserHomeScans)-1]
  691. return true
  692. }
  693. return false
  694. }
  695. // GetVFoldersQuotaScans returns the active quota scans for virtual folders
  696. func (s *ActiveScans) GetVFoldersQuotaScans() []ActiveVirtualFolderQuotaScan {
  697. s.RLock()
  698. defer s.RUnlock()
  699. scans := make([]ActiveVirtualFolderQuotaScan, len(s.FolderScans))
  700. copy(scans, s.FolderScans)
  701. return scans
  702. }
  703. // AddVFolderQuotaScan adds a virtual folder to the ones with active quota scans.
  704. // Returns false if the folder has a quota scan already running
  705. func (s *ActiveScans) AddVFolderQuotaScan(folderPath string) bool {
  706. s.Lock()
  707. defer s.Unlock()
  708. for _, scan := range s.FolderScans {
  709. if scan.MappedPath == folderPath {
  710. return false
  711. }
  712. }
  713. s.FolderScans = append(s.FolderScans, ActiveVirtualFolderQuotaScan{
  714. MappedPath: folderPath,
  715. StartTime: utils.GetTimeAsMsSinceEpoch(time.Now()),
  716. })
  717. return true
  718. }
  719. // RemoveVFolderQuotaScan removes a folder from the ones with active quota scans.
  720. // Returns false if the folder has no active quota scans
  721. func (s *ActiveScans) RemoveVFolderQuotaScan(folderPath string) bool {
  722. s.Lock()
  723. defer s.Unlock()
  724. indexToRemove := -1
  725. for i, scan := range s.FolderScans {
  726. if scan.MappedPath == folderPath {
  727. indexToRemove = i
  728. break
  729. }
  730. }
  731. if indexToRemove >= 0 {
  732. s.FolderScans[indexToRemove] = s.FolderScans[len(s.FolderScans)-1]
  733. s.FolderScans = s.FolderScans[:len(s.FolderScans)-1]
  734. return true
  735. }
  736. return false
  737. }