drivers.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package ftp
  2. import (
  3. "crypto/tls"
  4. "errors"
  5. "log"
  6. "os"
  7. ftp "github.com/fclairamb/ftpserverlib"
  8. )
  9. func (m mainDriver) GetSettings() (*ftp.Settings, error) {
  10. return &m.setting, nil
  11. }
  12. func (m mainDriver) ClientConnected(cc ftp.ClientContext) (string, error) {
  13. //log.Println("Client Connected: ", cc.ID(), cc.RemoteAddr())
  14. m.connectedUserList.Store(cc.ID(), "")
  15. return "arozos FTP Endpoint", nil
  16. }
  17. func (m mainDriver) ClientDisconnected(cc ftp.ClientContext) {
  18. //log.Println("Client Disconencted: ", cc.ID(), cc.RemoteAddr())
  19. ////Recalculate user quota if logged in
  20. val, ok := m.connectedUserList.Load(cc.ID())
  21. if ok {
  22. if val != "" {
  23. //Recalculate user storage quota
  24. if m.userHandler.GetAuthAgent().UserExists(val.(string)) {
  25. userinfo, err := m.userHandler.GetUserInfoFromUsername(val.(string))
  26. if err == nil {
  27. //Update the user storage quota
  28. userinfo.StorageQuota.CalculateQuotaUsage()
  29. log.Println("FTP storage quota updated: ", val.(string))
  30. }
  31. } else {
  32. //This user is being delete during his connection to FTP???
  33. }
  34. }
  35. m.connectedUserList.Delete(cc.ID())
  36. }
  37. }
  38. //Authenicate user using arozos authAgent
  39. func (m mainDriver) AuthUser(cc ftp.ClientContext, user string, pass string) (ftp.ClientDriver, error) {
  40. authAgent := m.userHandler.GetAuthAgent()
  41. if authAgent.ValidateUsernameAndPassword(user, pass) {
  42. //OK
  43. userinfo, _ := m.userHandler.GetUserInfoFromUsername(user)
  44. //Check user permission to access ftp endpoint
  45. db := m.userHandler.GetDatabase()
  46. allowedPgs := []string{}
  47. err := db.Read("ftp", "groups", &allowedPgs)
  48. if err != nil {
  49. allowedPgs = []string{}
  50. }
  51. accessOK := userinfo.UserIsInOneOfTheGroupOf(allowedPgs)
  52. if !accessOK {
  53. //Disconnect this user as he is not in the group that is allowed to access ftp
  54. log.Println(userinfo.Username + " tries to access FTP endpoint with invalid permission settings.")
  55. return nil, errors.New("User " + userinfo.Username + " has no permission to access FTP endpoint")
  56. }
  57. //Create tmp buffer for this user
  58. tmpFolder := m.tmpFolder + "users/" + userinfo.Username + "/ftpbuf/"
  59. os.MkdirAll(tmpFolder, 0755)
  60. //Record username into connected user list
  61. m.connectedUserList.Store(cc.ID(), userinfo.Username)
  62. //Return the aofs object
  63. return aofs{
  64. userinfo: userinfo,
  65. tmpFolder: tmpFolder,
  66. }, nil
  67. } else {
  68. return nil, errors.New("Invalid username or password")
  69. }
  70. }
  71. func (m mainDriver) GetTLSConfig() (*tls.Config, error) {
  72. return nil, errors.New("Not Supported")
  73. }