user.go 10 KB

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