user.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. package sdk
  2. import (
  3. "strings"
  4. "github.com/drakkan/sftpgo/v2/sdk/kms"
  5. )
  6. // Web Client/user REST API restrictions
  7. const (
  8. WebClientPubKeyChangeDisabled = "publickey-change-disabled"
  9. WebClientWriteDisabled = "write-disabled"
  10. WebClientMFADisabled = "mfa-disabled"
  11. WebClientPasswordChangeDisabled = "password-change-disabled"
  12. WebClientAPIKeyAuthChangeDisabled = "api-key-auth-change-disabled"
  13. WebClientInfoChangeDisabled = "info-change-disabled"
  14. WebClientSharesDisabled = "shares-disabled"
  15. WebClientPasswordResetDisabled = "password-reset-disabled"
  16. )
  17. var (
  18. // WebClientOptions defines the available options for the web client interface/user REST API
  19. WebClientOptions = []string{WebClientWriteDisabled, WebClientPasswordChangeDisabled, WebClientPasswordResetDisabled,
  20. WebClientPubKeyChangeDisabled, WebClientMFADisabled, WebClientAPIKeyAuthChangeDisabled, WebClientInfoChangeDisabled,
  21. WebClientSharesDisabled}
  22. // UserTypes defines the supported user type hints for auth plugins
  23. UserTypes = []string{string(UserTypeLDAP), string(UserTypeOS)}
  24. )
  25. // TLSUsername defines the TLS certificate attribute to use as username
  26. type TLSUsername string
  27. // Supported certificate attributes to use as username
  28. const (
  29. TLSUsernameNone TLSUsername = "None"
  30. TLSUsernameCN TLSUsername = "CommonName"
  31. )
  32. // UserType defines the supported user types.
  33. // This is an hint for external auth plugins, is not used in SFTPGo directly
  34. type UserType string
  35. // User types, auth plugins could use this info to choose the correct authentication backend
  36. const (
  37. UserTypeLDAP UserType = "LDAPUser"
  38. UserTypeOS UserType = "OSUser"
  39. )
  40. // DirectoryPermissions defines permissions for a directory virtual path
  41. type DirectoryPermissions struct {
  42. Path string
  43. Permissions []string
  44. }
  45. // PatternsFilter defines filters based on shell like patterns.
  46. // These restrictions do not apply to files listing for performance reasons, so
  47. // a denied file cannot be downloaded/overwritten/renamed but will still be
  48. // in the list of files.
  49. // System commands such as Git and rsync interacts with the filesystem directly
  50. // and they are not aware about these restrictions so they are not allowed
  51. // inside paths with extensions filters
  52. type PatternsFilter struct {
  53. // Virtual path, if no other specific filter is defined, the filter applies for
  54. // sub directories too.
  55. // For example if filters are defined for the paths "/" and "/sub" then the
  56. // filters for "/" are applied for any file outside the "/sub" directory
  57. Path string `json:"path"`
  58. // files with these, case insensitive, patterns are allowed.
  59. // Denied file patterns are evaluated before the allowed ones
  60. AllowedPatterns []string `json:"allowed_patterns,omitempty"`
  61. // files with these, case insensitive, patterns are not allowed.
  62. // Denied file patterns are evaluated before the allowed ones
  63. DeniedPatterns []string `json:"denied_patterns,omitempty"`
  64. }
  65. // GetCommaSeparatedPatterns returns the first non empty patterns list comma separated
  66. func (p *PatternsFilter) GetCommaSeparatedPatterns() string {
  67. if len(p.DeniedPatterns) > 0 {
  68. return strings.Join(p.DeniedPatterns, ",")
  69. }
  70. return strings.Join(p.AllowedPatterns, ",")
  71. }
  72. // IsDenied returns true if the patterns has one or more denied patterns
  73. func (p *PatternsFilter) IsDenied() bool {
  74. return len(p.DeniedPatterns) > 0
  75. }
  76. // IsAllowed returns true if the patterns has one or more allowed patterns
  77. func (p *PatternsFilter) IsAllowed() bool {
  78. return len(p.AllowedPatterns) > 0
  79. }
  80. // HooksFilter defines user specific overrides for global hooks
  81. type HooksFilter struct {
  82. ExternalAuthDisabled bool `json:"external_auth_disabled"`
  83. PreLoginDisabled bool `json:"pre_login_disabled"`
  84. CheckPasswordDisabled bool `json:"check_password_disabled"`
  85. }
  86. // RecoveryCode defines a 2FA recovery code
  87. type RecoveryCode struct {
  88. Secret kms.BaseSecret `json:"secret"`
  89. Used bool `json:"used,omitempty"`
  90. }
  91. // TOTPConfig defines the time-based one time password configuration
  92. type TOTPConfig struct {
  93. Enabled bool `json:"enabled,omitempty"`
  94. ConfigName string `json:"config_name,omitempty"`
  95. Secret kms.BaseSecret `json:"secret,omitempty"`
  96. // TOTP will be required for the specified protocols.
  97. // SSH protocol (SFTP/SCP/SSH commands) will ask for the TOTP passcode if the client uses keyboard interactive
  98. // authentication.
  99. // FTP have no standard way to support two factor authentication, if you
  100. // enable the support for this protocol you have to add the TOTP passcode after the password.
  101. // For example if your password is "password" and your one time passcode is
  102. // "123456" you have to use "password123456" as password.
  103. Protocols []string `json:"protocols,omitempty"`
  104. }
  105. // BandwidthLimit defines a per-source bandwidth limit
  106. type BandwidthLimit struct {
  107. // Source networks in CIDR notation as defined in RFC 4632 and RFC 4291
  108. // for example "192.0.2.0/24" or "2001:db8::/32". The limit applies if the
  109. // defined networks contain the client IP
  110. Sources []string `json:"sources"`
  111. // Maximum upload bandwidth as KB/s
  112. UploadBandwidth int64 `json:"upload_bandwidth,omitempty"`
  113. // Maximum download bandwidth as KB/s
  114. DownloadBandwidth int64 `json:"download_bandwidth,omitempty"`
  115. }
  116. // GetSourcesAsString returns the sources as comma separated string
  117. func (l *BandwidthLimit) GetSourcesAsString() string {
  118. return strings.Join(l.Sources, ",")
  119. }
  120. // BaseUserFilters defines additional restrictions for a user
  121. type BaseUserFilters struct {
  122. // only clients connecting from these IP/Mask are allowed.
  123. // IP/Mask must be in CIDR notation as defined in RFC 4632 and RFC 4291
  124. // for example "192.0.2.0/24" or "2001:db8::/32"
  125. AllowedIP []string `json:"allowed_ip,omitempty"`
  126. // clients connecting from these IP/Mask are not allowed.
  127. // Denied rules will be evaluated before allowed ones
  128. DeniedIP []string `json:"denied_ip,omitempty"`
  129. // these login methods are not allowed.
  130. // If null or empty any available login method is allowed
  131. DeniedLoginMethods []string `json:"denied_login_methods,omitempty"`
  132. // these protocols are not allowed.
  133. // If null or empty any available protocol is allowed
  134. DeniedProtocols []string `json:"denied_protocols,omitempty"`
  135. // filter based on shell patterns.
  136. // Please note that these restrictions can be easily bypassed.
  137. FilePatterns []PatternsFilter `json:"file_patterns,omitempty"`
  138. // max size allowed for a single upload, 0 means unlimited
  139. MaxUploadFileSize int64 `json:"max_upload_file_size,omitempty"`
  140. // TLS certificate attribute to use as username.
  141. // For FTP clients it must match the name provided using the
  142. // "USER" command
  143. TLSUsername TLSUsername `json:"tls_username,omitempty"`
  144. // user specific hook overrides
  145. Hooks HooksFilter `json:"hooks,omitempty"`
  146. // Disable checks for existence and automatic creation of home directory
  147. // and virtual folders.
  148. // SFTPGo requires that the user's home directory, virtual folder root,
  149. // and intermediate paths to virtual folders exist to work properly.
  150. // If you already know that the required directories exist, disabling
  151. // these checks will speed up login.
  152. // You could, for example, disable these checks after the first login
  153. DisableFsChecks bool `json:"disable_fs_checks,omitempty"`
  154. // WebClient related configuration options
  155. WebClient []string `json:"web_client,omitempty"`
  156. // API key auth allows to impersonate this user with an API key
  157. AllowAPIKeyAuth bool `json:"allow_api_key_auth,omitempty"`
  158. // UserType is an hint for authentication plugins.
  159. // It is ignored when using SFTPGo internal authentication
  160. UserType string `json:"user_type,omitempty"`
  161. // Per-source bandwidth limits
  162. BandwidthLimits []BandwidthLimit `json:"bandwidth_limits,omitempty"`
  163. }
  164. // UserFilters defines additional restrictions for a user
  165. // TODO: rename to UserOptions in v3
  166. type UserFilters struct {
  167. BaseUserFilters
  168. // Time-based one time passwords configuration
  169. TOTPConfig TOTPConfig `json:"totp_config,omitempty"`
  170. // Recovery codes to use if the user loses access to their second factor auth device.
  171. // Each code can only be used once, you should use these codes to login and disable or
  172. // reset 2FA for your account
  173. RecoveryCodes []RecoveryCode `json:"recovery_codes,omitempty"`
  174. }
  175. // BaseUser defines the shared user fields
  176. type BaseUser struct {
  177. // Data provider unique identifier
  178. ID int64 `json:"id"`
  179. // 1 enabled, 0 disabled (login is not allowed)
  180. Status int `json:"status"`
  181. // Username
  182. Username string `json:"username"`
  183. // Email
  184. Email string `json:"email,omitempty"`
  185. // Account expiration date as unix timestamp in milliseconds. An expired account cannot login.
  186. // 0 means no expiration
  187. ExpirationDate int64 `json:"expiration_date"`
  188. // Password used for password authentication.
  189. // For users created using SFTPGo REST API the password is be stored using bcrypt or argon2id hashing algo.
  190. // Checking passwords stored with pbkdf2, md5crypt and sha512crypt is supported too.
  191. Password string `json:"password,omitempty"`
  192. // PublicKeys used for public key authentication. At least one between password and a public key is mandatory
  193. PublicKeys []string `json:"public_keys,omitempty"`
  194. // The user cannot upload or download files outside this directory. Must be an absolute path
  195. HomeDir string `json:"home_dir"`
  196. // If sftpgo runs as root system user then the created files and directories will be assigned to this system UID
  197. UID int `json:"uid"`
  198. // If sftpgo runs as root system user then the created files and directories will be assigned to this system GID
  199. GID int `json:"gid"`
  200. // Maximum concurrent sessions. 0 means unlimited
  201. MaxSessions int `json:"max_sessions"`
  202. // Maximum size allowed as bytes. 0 means unlimited
  203. QuotaSize int64 `json:"quota_size"`
  204. // Maximum number of files allowed. 0 means unlimited
  205. QuotaFiles int `json:"quota_files"`
  206. // List of the granted permissions
  207. Permissions map[string][]string `json:"permissions"`
  208. // Used quota as bytes
  209. UsedQuotaSize int64 `json:"used_quota_size,omitempty"`
  210. // Used quota as number of files
  211. UsedQuotaFiles int `json:"used_quota_files,omitempty"`
  212. // Last quota update as unix timestamp in milliseconds
  213. LastQuotaUpdate int64 `json:"last_quota_update,omitempty"`
  214. // Maximum upload bandwidth as KB/s, 0 means unlimited.
  215. // This is the default if no per-source limit match
  216. UploadBandwidth int64 `json:"upload_bandwidth,omitempty"`
  217. // Maximum download bandwidth as KB/s, 0 means unlimited.
  218. // This is the default if no per-source limit match
  219. DownloadBandwidth int64 `json:"download_bandwidth,omitempty"`
  220. // Last login as unix timestamp in milliseconds
  221. LastLogin int64 `json:"last_login,omitempty"`
  222. // Creation time as unix timestamp in milliseconds. It will be 0 for admins created before v2.2.0
  223. CreatedAt int64 `json:"created_at"`
  224. // last update time as unix timestamp in milliseconds
  225. UpdatedAt int64 `json:"updated_at"`
  226. // optional description, for example full name
  227. Description string `json:"description,omitempty"`
  228. // free form text field for external systems
  229. AdditionalInfo string `json:"additional_info,omitempty"`
  230. }
  231. // User defines a SFTPGo user
  232. type User struct {
  233. BaseUser
  234. // Additional restrictions
  235. Filters UserFilters `json:"filters"`
  236. // Mapping between virtual paths and virtual folders
  237. VirtualFolders []VirtualFolder `json:"virtual_folders,omitempty"`
  238. // Filesystem configuration details
  239. FsConfig Filesystem `json:"filesystem"`
  240. }