user.go 7.6 KB

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