2021-01-17 21:29:08 +00:00
|
|
|
package dataprovider
|
|
|
|
|
|
|
|
import (
|
2021-04-05 16:23:40 +00:00
|
|
|
"crypto/sha256"
|
2021-01-17 21:29:08 +00:00
|
|
|
"encoding/base64"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2021-07-31 08:39:53 +00:00
|
|
|
"os"
|
2021-01-17 21:29:08 +00:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/alexedwards/argon2id"
|
2021-08-06 16:56:07 +00:00
|
|
|
passwordvalidator "github.com/wagslane/go-password-validator"
|
2021-04-20 11:55:09 +00:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
2021-01-17 21:29:08 +00:00
|
|
|
|
2021-07-11 13:26:51 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/util"
|
2021-01-17 21:29:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Available permissions for SFTPGo admins
|
|
|
|
const (
|
|
|
|
PermAdminAny = "*"
|
|
|
|
PermAdminAddUsers = "add_users"
|
|
|
|
PermAdminChangeUsers = "edit_users"
|
|
|
|
PermAdminDeleteUsers = "del_users"
|
|
|
|
PermAdminViewUsers = "view_users"
|
|
|
|
PermAdminViewConnections = "view_conns"
|
|
|
|
PermAdminCloseConnections = "close_conns"
|
|
|
|
PermAdminViewServerStatus = "view_status"
|
|
|
|
PermAdminManageAdmins = "manage_admins"
|
|
|
|
PermAdminQuotaScans = "quota_scans"
|
|
|
|
PermAdminManageSystem = "manage_system"
|
|
|
|
PermAdminManageDefender = "manage_defender"
|
|
|
|
PermAdminViewDefender = "view_defender"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
emailRegex = regexp.MustCompile("^(?:(?:(?:(?:[a-zA-Z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])+(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A-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])))\\.?$")
|
|
|
|
validAdminPerms = []string{PermAdminAny, PermAdminAddUsers, PermAdminChangeUsers, PermAdminDeleteUsers,
|
|
|
|
PermAdminViewUsers, PermAdminViewConnections, PermAdminCloseConnections, PermAdminViewServerStatus,
|
|
|
|
PermAdminManageAdmins, PermAdminQuotaScans, PermAdminManageSystem, PermAdminManageDefender,
|
|
|
|
PermAdminViewDefender}
|
|
|
|
)
|
|
|
|
|
|
|
|
// AdminFilters defines additional restrictions for SFTPGo admins
|
2021-05-27 07:40:46 +00:00
|
|
|
// TODO: rename to AdminOptions in v3
|
2021-01-17 21:29:08 +00:00
|
|
|
type AdminFilters struct {
|
|
|
|
// only clients connecting from these IP/Mask are allowed.
|
|
|
|
// IP/Mask must be in CIDR notation as defined in RFC 4632 and RFC 4291
|
|
|
|
// for example "192.0.2.0/24" or "2001:db8::/32"
|
|
|
|
AllowList []string `json:"allow_list,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Admin defines a SFTPGo admin
|
|
|
|
type Admin struct {
|
|
|
|
// Database unique identifier
|
|
|
|
ID int64 `json:"id"`
|
|
|
|
// 1 enabled, 0 disabled (login is not allowed)
|
|
|
|
Status int `json:"status"`
|
|
|
|
// Username
|
|
|
|
Username string `json:"username"`
|
|
|
|
Password string `json:"password,omitempty"`
|
|
|
|
Email string `json:"email"`
|
|
|
|
Permissions []string `json:"permissions"`
|
|
|
|
Filters AdminFilters `json:"filters,omitempty"`
|
2021-02-24 18:40:29 +00:00
|
|
|
Description string `json:"description,omitempty"`
|
2021-01-17 21:29:08 +00:00
|
|
|
AdditionalInfo string `json:"additional_info,omitempty"`
|
|
|
|
}
|
|
|
|
|
2021-03-04 08:48:53 +00:00
|
|
|
func (a *Admin) checkPassword() error {
|
2021-07-11 13:26:51 +00:00
|
|
|
if a.Password != "" && !util.IsStringPrefixInSlice(a.Password, internalHashPwdPrefixes) {
|
2021-08-06 16:56:07 +00:00
|
|
|
if config.PasswordValidation.Admins.MinEntropy > 0 {
|
|
|
|
if err := passwordvalidator.Validate(a.Password, config.PasswordValidation.Admins.MinEntropy); err != nil {
|
|
|
|
return util.NewValidationError(err.Error())
|
|
|
|
}
|
|
|
|
}
|
2021-04-25 07:38:33 +00:00
|
|
|
if config.PasswordHashing.Algo == HashingAlgoBcrypt {
|
2021-04-20 11:55:09 +00:00
|
|
|
pwd, err := bcrypt.GenerateFromPassword([]byte(a.Password), config.PasswordHashing.BcryptOptions.Cost)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
a.Password = string(pwd)
|
|
|
|
} else {
|
|
|
|
pwd, err := argon2id.CreateHash(a.Password, argon2Params)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
a.Password = pwd
|
2021-03-04 08:48:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (a *Admin) validate() error {
|
|
|
|
if a.Username == "" {
|
2021-07-11 13:26:51 +00:00
|
|
|
return util.NewValidationError("username is mandatory")
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
|
|
|
if a.Password == "" {
|
2021-07-11 13:26:51 +00:00
|
|
|
return util.NewValidationError("please set a password")
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
2021-03-04 08:48:53 +00:00
|
|
|
if !config.SkipNaturalKeysValidation && !usernameRegex.MatchString(a.Username) {
|
2021-07-11 13:26:51 +00:00
|
|
|
return util.NewValidationError(fmt.Sprintf("username %#v is not valid, the following characters are allowed: a-zA-Z0-9-_.~", a.Username))
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
2021-03-04 08:48:53 +00:00
|
|
|
if err := a.checkPassword(); err != nil {
|
|
|
|
return err
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
a.Permissions = util.RemoveDuplicates(a.Permissions)
|
2021-01-17 21:29:08 +00:00
|
|
|
if len(a.Permissions) == 0 {
|
2021-07-11 13:26:51 +00:00
|
|
|
return util.NewValidationError("please grant some permissions to this admin")
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
if util.IsStringInSlice(PermAdminAny, a.Permissions) {
|
2021-01-17 21:29:08 +00:00
|
|
|
a.Permissions = []string{PermAdminAny}
|
|
|
|
}
|
|
|
|
for _, perm := range a.Permissions {
|
2021-07-11 13:26:51 +00:00
|
|
|
if !util.IsStringInSlice(perm, validAdminPerms) {
|
|
|
|
return util.NewValidationError(fmt.Sprintf("invalid permission: %#v", perm))
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if a.Email != "" && !emailRegex.MatchString(a.Email) {
|
2021-07-11 13:26:51 +00:00
|
|
|
return util.NewValidationError(fmt.Sprintf("email %#v is not valid", a.Email))
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
|
|
|
for _, IPMask := range a.Filters.AllowList {
|
|
|
|
_, _, err := net.ParseCIDR(IPMask)
|
|
|
|
if err != nil {
|
2021-07-11 13:26:51 +00:00
|
|
|
return util.NewValidationError(fmt.Sprintf("could not parse allow list entry %#v : %v", IPMask, err))
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckPassword verifies the admin password
|
|
|
|
func (a *Admin) CheckPassword(password string) (bool, error) {
|
2021-04-20 11:55:09 +00:00
|
|
|
if strings.HasPrefix(a.Password, bcryptPwdPrefix) {
|
|
|
|
if err := bcrypt.CompareHashAndPassword([]byte(a.Password), []byte(password)); err != nil {
|
2021-04-26 17:48:21 +00:00
|
|
|
return false, ErrInvalidCredentials
|
2021-04-20 11:55:09 +00:00
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
return argon2id.ComparePasswordAndHash(password, a.Password)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CanLoginFromIP returns true if login from the given IP is allowed
|
|
|
|
func (a *Admin) CanLoginFromIP(ip string) bool {
|
|
|
|
if len(a.Filters.AllowList) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
parsedIP := net.ParseIP(ip)
|
|
|
|
if parsedIP == nil {
|
|
|
|
return len(a.Filters.AllowList) == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ipMask := range a.Filters.AllowList {
|
|
|
|
_, network, err := net.ParseCIDR(ipMask)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if network.Contains(parsedIP) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Admin) checkUserAndPass(password, ip string) error {
|
|
|
|
if a.Status != 1 {
|
|
|
|
return fmt.Errorf("admin %#v is disabled", a.Username)
|
|
|
|
}
|
|
|
|
if a.Password == "" || password == "" {
|
|
|
|
return errors.New("credentials cannot be null or empty")
|
|
|
|
}
|
|
|
|
match, err := a.CheckPassword(password)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !match {
|
|
|
|
return ErrInvalidCredentials
|
|
|
|
}
|
|
|
|
if !a.CanLoginFromIP(ip) {
|
|
|
|
return fmt.Errorf("login from IP %v not allowed", ip)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// HideConfidentialData hides admin confidential data
|
|
|
|
func (a *Admin) HideConfidentialData() {
|
|
|
|
a.Password = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasPermission returns true if the admin has the specified permission
|
|
|
|
func (a *Admin) HasPermission(perm string) bool {
|
2021-07-11 13:26:51 +00:00
|
|
|
if util.IsStringInSlice(PermAdminAny, a.Permissions) {
|
2021-01-17 21:29:08 +00:00
|
|
|
return true
|
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
return util.IsStringInSlice(perm, a.Permissions)
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetPermissionsAsString returns permission as string
|
|
|
|
func (a *Admin) GetPermissionsAsString() string {
|
|
|
|
return strings.Join(a.Permissions, ", ")
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAllowedIPAsString returns the allowed IP as comma separated string
|
|
|
|
func (a *Admin) GetAllowedIPAsString() string {
|
|
|
|
return strings.Join(a.Filters.AllowList, ",")
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetValidPerms returns the allowed admin permissions
|
|
|
|
func (a *Admin) GetValidPerms() []string {
|
|
|
|
return validAdminPerms
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetInfoString returns admin's info as string.
|
|
|
|
func (a *Admin) GetInfoString() string {
|
|
|
|
var result string
|
|
|
|
if a.Email != "" {
|
|
|
|
result = fmt.Sprintf("Email: %v. ", a.Email)
|
|
|
|
}
|
|
|
|
if len(a.Filters.AllowList) > 0 {
|
|
|
|
result += fmt.Sprintf("Allowed IP/Mask: %v. ", len(a.Filters.AllowList))
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSignature returns a signature for this admin.
|
|
|
|
// It could change after an update
|
|
|
|
func (a *Admin) GetSignature() string {
|
|
|
|
data := []byte(a.Username)
|
|
|
|
data = append(data, []byte(a.Password)...)
|
|
|
|
signature := sha256.Sum256(data)
|
|
|
|
return base64.StdEncoding.EncodeToString(signature[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Admin) getACopy() Admin {
|
|
|
|
permissions := make([]string, len(a.Permissions))
|
|
|
|
copy(permissions, a.Permissions)
|
|
|
|
filters := AdminFilters{}
|
|
|
|
filters.AllowList = make([]string, len(a.Filters.AllowList))
|
|
|
|
copy(filters.AllowList, a.Filters.AllowList)
|
|
|
|
|
|
|
|
return Admin{
|
|
|
|
ID: a.ID,
|
|
|
|
Status: a.Status,
|
|
|
|
Username: a.Username,
|
|
|
|
Password: a.Password,
|
|
|
|
Email: a.Email,
|
|
|
|
Permissions: permissions,
|
|
|
|
Filters: filters,
|
|
|
|
AdditionalInfo: a.AdditionalInfo,
|
2021-02-24 18:40:29 +00:00
|
|
|
Description: a.Description,
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// setDefaults sets the appropriate value for the default admin
|
|
|
|
func (a *Admin) setDefaults() {
|
2021-07-31 08:39:53 +00:00
|
|
|
envUsername := strings.TrimSpace(os.Getenv(`SFTPGO_DEFAULT_ADMIN_USERNAME`))
|
|
|
|
envPassword := strings.TrimSpace(os.Getenv(`SFTPGO_DEFAULT_ADMIN_PASSWORD`))
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
a.Username = "admin"
|
2021-07-31 08:39:53 +00:00
|
|
|
if envUsername != "" {
|
|
|
|
a.Username = envUsername
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
a.Password = "password"
|
2021-07-31 08:39:53 +00:00
|
|
|
if envPassword != "" {
|
|
|
|
a.Password = envPassword
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
a.Status = 1
|
|
|
|
a.Permissions = []string{PermAdminAny}
|
|
|
|
}
|