user.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package sdk
  2. import (
  3. "strings"
  4. "github.com/drakkan/sftpgo/v2/util"
  5. )
  6. // Web Client restrictions
  7. const (
  8. WebClientPubKeyChangeDisabled = "publickey-change-disabled"
  9. )
  10. var (
  11. // WebClientOptions defines the available options for the web client interface
  12. WebClientOptions = []string{WebClientPubKeyChangeDisabled}
  13. )
  14. // TLSUsername defines the TLS certificate attribute to use as username
  15. type TLSUsername string
  16. // Supported certificate attributes to use as username
  17. const (
  18. TLSUsernameNone TLSUsername = "None"
  19. TLSUsernameCN TLSUsername = "CommonName"
  20. )
  21. // DirectoryPermissions defines permissions for a directory virtual path
  22. type DirectoryPermissions struct {
  23. Path string
  24. Permissions []string
  25. }
  26. // HasPerm returns true if the directory has the specified permissions
  27. func (d *DirectoryPermissions) HasPerm(perm string) bool {
  28. return util.IsStringInSlice(perm, d.Permissions)
  29. }
  30. // PatternsFilter defines filters based on shell like patterns.
  31. // These restrictions do not apply to files listing for performance reasons, so
  32. // a denied file cannot be downloaded/overwritten/renamed but will still be
  33. // in the list of files.
  34. // System commands such as Git and rsync interacts with the filesystem directly
  35. // and they are not aware about these restrictions so they are not allowed
  36. // inside paths with extensions filters
  37. type PatternsFilter struct {
  38. // Virtual path, if no other specific filter is defined, the filter apply for
  39. // sub directories too.
  40. // For example if filters are defined for the paths "/" and "/sub" then the
  41. // filters for "/" are applied for any file outside the "/sub" directory
  42. Path string `json:"path"`
  43. // files with these, case insensitive, patterns are allowed.
  44. // Denied file patterns are evaluated before the allowed ones
  45. AllowedPatterns []string `json:"allowed_patterns,omitempty"`
  46. // files with these, case insensitive, patterns are not allowed.
  47. // Denied file patterns are evaluated before the allowed ones
  48. DeniedPatterns []string `json:"denied_patterns,omitempty"`
  49. }
  50. // GetCommaSeparatedPatterns returns the first non empty patterns list comma separated
  51. func (p *PatternsFilter) GetCommaSeparatedPatterns() string {
  52. if len(p.DeniedPatterns) > 0 {
  53. return strings.Join(p.DeniedPatterns, ",")
  54. }
  55. return strings.Join(p.AllowedPatterns, ",")
  56. }
  57. // IsDenied returns true if the patterns has one or more denied patterns
  58. func (p *PatternsFilter) IsDenied() bool {
  59. return len(p.DeniedPatterns) > 0
  60. }
  61. // IsAllowed returns true if the patterns has one or more allowed patterns
  62. func (p *PatternsFilter) IsAllowed() bool {
  63. return len(p.AllowedPatterns) > 0
  64. }
  65. // HooksFilter defines user specific overrides for global hooks
  66. type HooksFilter struct {
  67. ExternalAuthDisabled bool `json:"external_auth_disabled"`
  68. PreLoginDisabled bool `json:"pre_login_disabled"`
  69. CheckPasswordDisabled bool `json:"check_password_disabled"`
  70. }
  71. // UserFilters defines additional restrictions for a user
  72. // TODO: rename to UserOptions in v3
  73. type UserFilters struct {
  74. // only clients connecting from these IP/Mask are allowed.
  75. // IP/Mask must be in CIDR notation as defined in RFC 4632 and RFC 4291
  76. // for example "192.0.2.0/24" or "2001:db8::/32"
  77. AllowedIP []string `json:"allowed_ip,omitempty"`
  78. // clients connecting from these IP/Mask are not allowed.
  79. // Denied rules will be evaluated before allowed ones
  80. DeniedIP []string `json:"denied_ip,omitempty"`
  81. // these login methods are not allowed.
  82. // If null or empty any available login method is allowed
  83. DeniedLoginMethods []string `json:"denied_login_methods,omitempty"`
  84. // these protocols are not allowed.
  85. // If null or empty any available protocol is allowed
  86. DeniedProtocols []string `json:"denied_protocols,omitempty"`
  87. // filter based on shell patterns.
  88. // Please note that these restrictions can be easily bypassed.
  89. FilePatterns []PatternsFilter `json:"file_patterns,omitempty"`
  90. // max size allowed for a single upload, 0 means unlimited
  91. MaxUploadFileSize int64 `json:"max_upload_file_size,omitempty"`
  92. // TLS certificate attribute to use as username.
  93. // For FTP clients it must match the name provided using the
  94. // "USER" command
  95. TLSUsername TLSUsername `json:"tls_username,omitempty"`
  96. // user specific hook overrides
  97. Hooks HooksFilter `json:"hooks,omitempty"`
  98. // Disable checks for existence and automatic creation of home directory
  99. // and virtual folders.
  100. // SFTPGo requires that the user's home directory, virtual folder root,
  101. // and intermediate paths to virtual folders exist to work properly.
  102. // If you already know that the required directories exist, disabling
  103. // these checks will speed up login.
  104. // You could, for example, disable these checks after the first login
  105. DisableFsChecks bool `json:"disable_fs_checks,omitempty"`
  106. // WebClient related configuration options
  107. WebClient []string `json:"web_client,omitempty"`
  108. }
  109. type BaseUser struct {
  110. // Data provider unique identifier
  111. ID int64 `json:"id"`
  112. // 1 enabled, 0 disabled (login is not allowed)
  113. Status int `json:"status"`
  114. // Username
  115. Username string `json:"username"`
  116. // Account expiration date as unix timestamp in milliseconds. An expired account cannot login.
  117. // 0 means no expiration
  118. ExpirationDate int64 `json:"expiration_date"`
  119. // Password used for password authentication.
  120. // For users created using SFTPGo REST API the password is be stored using bcrypt or argon2id hashing algo.
  121. // Checking passwords stored with pbkdf2, md5crypt and sha512crypt is supported too.
  122. Password string `json:"password,omitempty"`
  123. // PublicKeys used for public key authentication. At least one between password and a public key is mandatory
  124. PublicKeys []string `json:"public_keys,omitempty"`
  125. // The user cannot upload or download files outside this directory. Must be an absolute path
  126. HomeDir string `json:"home_dir"`
  127. // If sftpgo runs as root system user then the created files and directories will be assigned to this system UID
  128. UID int `json:"uid"`
  129. // If sftpgo runs as root system user then the created files and directories will be assigned to this system GID
  130. GID int `json:"gid"`
  131. // Maximum concurrent sessions. 0 means unlimited
  132. MaxSessions int `json:"max_sessions"`
  133. // Maximum size allowed as bytes. 0 means unlimited
  134. QuotaSize int64 `json:"quota_size"`
  135. // Maximum number of files allowed. 0 means unlimited
  136. QuotaFiles int `json:"quota_files"`
  137. // List of the granted permissions
  138. Permissions map[string][]string `json:"permissions"`
  139. // Used quota as bytes
  140. UsedQuotaSize int64 `json:"used_quota_size"`
  141. // Used quota as number of files
  142. UsedQuotaFiles int `json:"used_quota_files"`
  143. // Last quota update as unix timestamp in milliseconds
  144. LastQuotaUpdate int64 `json:"last_quota_update"`
  145. // Maximum upload bandwidth as KB/s, 0 means unlimited
  146. UploadBandwidth int64 `json:"upload_bandwidth"`
  147. // Maximum download bandwidth as KB/s, 0 means unlimited
  148. DownloadBandwidth int64 `json:"download_bandwidth"`
  149. // Last login as unix timestamp in milliseconds
  150. LastLogin int64 `json:"last_login"`
  151. // Additional restrictions
  152. Filters UserFilters `json:"filters"`
  153. // optional description, for example full name
  154. Description string `json:"description,omitempty"`
  155. // free form text field for external systems
  156. AdditionalInfo string `json:"additional_info,omitempty"`
  157. }
  158. // User defines a SFTPGo user
  159. type User struct {
  160. BaseUser
  161. // Mapping between virtual paths and virtual folders
  162. VirtualFolders []VirtualFolder `json:"virtual_folders,omitempty"`
  163. // Filesystem configuration details
  164. FsConfig Filesystem `json:"filesystem"`
  165. }