user.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. }
  110. type BaseUser struct {
  111. // Data provider unique identifier
  112. ID int64 `json:"id"`
  113. // 1 enabled, 0 disabled (login is not allowed)
  114. Status int `json:"status"`
  115. // Username
  116. Username string `json:"username"`
  117. // Account expiration date as unix timestamp in milliseconds. An expired account cannot login.
  118. // 0 means no expiration
  119. ExpirationDate int64 `json:"expiration_date"`
  120. // Password used for password authentication.
  121. // For users created using SFTPGo REST API the password is be stored using bcrypt or argon2id hashing algo.
  122. // Checking passwords stored with pbkdf2, md5crypt and sha512crypt is supported too.
  123. Password string `json:"password,omitempty"`
  124. // PublicKeys used for public key authentication. At least one between password and a public key is mandatory
  125. PublicKeys []string `json:"public_keys,omitempty"`
  126. // The user cannot upload or download files outside this directory. Must be an absolute path
  127. HomeDir string `json:"home_dir"`
  128. // If sftpgo runs as root system user then the created files and directories will be assigned to this system UID
  129. UID int `json:"uid"`
  130. // If sftpgo runs as root system user then the created files and directories will be assigned to this system GID
  131. GID int `json:"gid"`
  132. // Maximum concurrent sessions. 0 means unlimited
  133. MaxSessions int `json:"max_sessions"`
  134. // Maximum size allowed as bytes. 0 means unlimited
  135. QuotaSize int64 `json:"quota_size"`
  136. // Maximum number of files allowed. 0 means unlimited
  137. QuotaFiles int `json:"quota_files"`
  138. // List of the granted permissions
  139. Permissions map[string][]string `json:"permissions"`
  140. // Used quota as bytes
  141. UsedQuotaSize int64 `json:"used_quota_size"`
  142. // Used quota as number of files
  143. UsedQuotaFiles int `json:"used_quota_files"`
  144. // Last quota update as unix timestamp in milliseconds
  145. LastQuotaUpdate int64 `json:"last_quota_update"`
  146. // Maximum upload bandwidth as KB/s, 0 means unlimited
  147. UploadBandwidth int64 `json:"upload_bandwidth"`
  148. // Maximum download bandwidth as KB/s, 0 means unlimited
  149. DownloadBandwidth int64 `json:"download_bandwidth"`
  150. // Last login as unix timestamp in milliseconds
  151. LastLogin int64 `json:"last_login"`
  152. // Additional restrictions
  153. Filters UserFilters `json:"filters"`
  154. // optional description, for example full name
  155. Description string `json:"description,omitempty"`
  156. // free form text field for external systems
  157. AdditionalInfo string `json:"additional_info,omitempty"`
  158. }
  159. // User defines a SFTPGo user
  160. type User struct {
  161. BaseUser
  162. // Mapping between virtual paths and virtual folders
  163. VirtualFolders []VirtualFolder `json:"virtual_folders,omitempty"`
  164. // Filesystem configuration details
  165. FsConfig Filesystem `json:"filesystem"`
  166. }