2019-07-20 10:26:52 +00:00
|
|
|
package dataprovider
|
|
|
|
|
|
|
|
import (
|
2019-09-14 14:18:31 +00:00
|
|
|
"context"
|
2019-07-20 10:26:52 +00:00
|
|
|
"database/sql"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/drakkan/sftpgo/logger"
|
|
|
|
"github.com/drakkan/sftpgo/utils"
|
|
|
|
)
|
|
|
|
|
2020-02-08 13:44:25 +00:00
|
|
|
const (
|
|
|
|
sqlDatabaseVersion = 1
|
|
|
|
initialDBVersionSQL = "INSERT INTO schema_version (version) VALUES (1);"
|
|
|
|
)
|
|
|
|
|
2019-08-11 12:53:37 +00:00
|
|
|
func getUserByUsername(username string, dbHandle *sql.DB) (User, error) {
|
2019-07-20 10:26:52 +00:00
|
|
|
var user User
|
|
|
|
q := getUserByUsernameQuery()
|
|
|
|
stmt, err := dbHandle.Prepare(q)
|
|
|
|
if err != nil {
|
2019-09-06 13:19:01 +00:00
|
|
|
providerLog(logger.LevelWarn, "error preparing database query %#v: %v", q, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
|
|
|
|
row := stmt.QueryRow(username)
|
|
|
|
return getUserFromDbRow(row, nil)
|
|
|
|
}
|
|
|
|
|
2019-08-11 12:53:37 +00:00
|
|
|
func sqlCommonValidateUserAndPass(username string, password string, dbHandle *sql.DB) (User, error) {
|
2019-07-20 10:26:52 +00:00
|
|
|
var user User
|
|
|
|
if len(password) == 0 {
|
|
|
|
return user, errors.New("Credentials cannot be null or empty")
|
|
|
|
}
|
2019-08-11 12:53:37 +00:00
|
|
|
user, err := getUserByUsername(username, dbHandle)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
2019-09-06 13:19:01 +00:00
|
|
|
providerLog(logger.LevelWarn, "error authenticating user: %v, error: %v", username, err)
|
2019-08-12 16:31:31 +00:00
|
|
|
return user, err
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2019-08-12 16:31:31 +00:00
|
|
|
return checkUserAndPass(user, password)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2019-09-05 19:35:53 +00:00
|
|
|
func sqlCommonValidateUserAndPubKey(username string, pubKey string, dbHandle *sql.DB) (User, string, error) {
|
2019-07-20 10:26:52 +00:00
|
|
|
var user User
|
|
|
|
if len(pubKey) == 0 {
|
2019-09-05 19:35:53 +00:00
|
|
|
return user, "", errors.New("Credentials cannot be null or empty")
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2019-08-11 12:53:37 +00:00
|
|
|
user, err := getUserByUsername(username, dbHandle)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
2019-09-06 13:19:01 +00:00
|
|
|
providerLog(logger.LevelWarn, "error authenticating user: %v, error: %v", username, err)
|
2019-09-05 19:35:53 +00:00
|
|
|
return user, "", err
|
2019-07-20 19:17:53 +00:00
|
|
|
}
|
2019-08-12 16:31:31 +00:00
|
|
|
return checkUserAndPubKey(user, pubKey)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2019-09-13 16:45:36 +00:00
|
|
|
func sqlCommonCheckAvailability(dbHandle *sql.DB) error {
|
2019-09-14 14:18:31 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
return dbHandle.PingContext(ctx)
|
2019-09-13 16:45:36 +00:00
|
|
|
}
|
|
|
|
|
2019-08-11 12:53:37 +00:00
|
|
|
func sqlCommonGetUserByID(ID int64, dbHandle *sql.DB) (User, error) {
|
2019-07-20 10:26:52 +00:00
|
|
|
var user User
|
|
|
|
q := getUserByIDQuery()
|
|
|
|
stmt, err := dbHandle.Prepare(q)
|
|
|
|
if err != nil {
|
2019-09-06 13:19:01 +00:00
|
|
|
providerLog(logger.LevelWarn, "error preparing database query %#v: %v", q, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
|
|
|
|
row := stmt.QueryRow(ID)
|
|
|
|
return getUserFromDbRow(row, nil)
|
|
|
|
}
|
|
|
|
|
2019-08-11 12:53:37 +00:00
|
|
|
func sqlCommonUpdateQuota(username string, filesAdd int, sizeAdd int64, reset bool, dbHandle *sql.DB) error {
|
2019-07-28 11:24:46 +00:00
|
|
|
q := getUpdateQuotaQuery(reset)
|
2019-07-20 10:26:52 +00:00
|
|
|
stmt, err := dbHandle.Prepare(q)
|
|
|
|
if err != nil {
|
2019-09-06 13:19:01 +00:00
|
|
|
providerLog(logger.LevelWarn, "error preparing database query %#v: %v", q, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
2019-07-28 11:24:46 +00:00
|
|
|
_, err = stmt.Exec(sizeAdd, filesAdd, utils.GetTimeAsMsSinceEpoch(time.Now()), username)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err == nil {
|
2019-11-13 10:36:21 +00:00
|
|
|
providerLog(logger.LevelDebug, "quota updated for user %#v, files increment: %v size increment: %v is reset? %v",
|
2019-07-28 11:24:46 +00:00
|
|
|
username, filesAdd, sizeAdd, reset)
|
2019-07-20 10:26:52 +00:00
|
|
|
} else {
|
2019-11-13 10:36:21 +00:00
|
|
|
providerLog(logger.LevelWarn, "error updating quota for user %#v: %v", username, err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func sqlCommonUpdateLastLogin(username string, dbHandle *sql.DB) error {
|
|
|
|
q := getUpdateLastLoginQuery()
|
|
|
|
stmt, err := dbHandle.Prepare(q)
|
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelWarn, "error preparing database query %#v: %v", q, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
_, err = stmt.Exec(utils.GetTimeAsMsSinceEpoch(time.Now()), username)
|
|
|
|
if err == nil {
|
|
|
|
providerLog(logger.LevelDebug, "last login updated for user %#v", username)
|
|
|
|
} else {
|
|
|
|
providerLog(logger.LevelWarn, "error updating last login for user %#v: %v", username, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-11 12:53:37 +00:00
|
|
|
func sqlCommonGetUsedQuota(username string, dbHandle *sql.DB) (int, int64, error) {
|
2019-07-20 10:26:52 +00:00
|
|
|
q := getQuotaQuery()
|
|
|
|
stmt, err := dbHandle.Prepare(q)
|
|
|
|
if err != nil {
|
2019-09-06 13:19:01 +00:00
|
|
|
providerLog(logger.LevelWarn, "error preparing database query %#v: %v", q, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
|
|
|
|
var usedFiles int
|
|
|
|
var usedSize int64
|
|
|
|
err = stmt.QueryRow(username).Scan(&usedSize, &usedFiles)
|
|
|
|
if err != nil {
|
2019-09-06 13:19:01 +00:00
|
|
|
providerLog(logger.LevelWarn, "error getting quota for user: %v, error: %v", username, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
return usedFiles, usedSize, err
|
|
|
|
}
|
|
|
|
|
2019-08-11 12:53:37 +00:00
|
|
|
func sqlCommonCheckUserExists(username string, dbHandle *sql.DB) (User, error) {
|
2019-07-20 10:26:52 +00:00
|
|
|
var user User
|
|
|
|
q := getUserByUsernameQuery()
|
|
|
|
stmt, err := dbHandle.Prepare(q)
|
|
|
|
if err != nil {
|
2019-09-06 13:19:01 +00:00
|
|
|
providerLog(logger.LevelWarn, "error preparing database query %#v: %v", q, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
row := stmt.QueryRow(username)
|
|
|
|
return getUserFromDbRow(row, nil)
|
|
|
|
}
|
|
|
|
|
2019-08-11 12:53:37 +00:00
|
|
|
func sqlCommonAddUser(user User, dbHandle *sql.DB) error {
|
2019-07-20 10:26:52 +00:00
|
|
|
err := validateUser(&user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
q := getAddUserQuery()
|
|
|
|
stmt, err := dbHandle.Prepare(q)
|
|
|
|
if err != nil {
|
2019-09-06 13:19:01 +00:00
|
|
|
providerLog(logger.LevelWarn, "error preparing database query %#v: %v", q, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
permissions, err := user.GetPermissionsAsJSON()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-08-01 20:42:46 +00:00
|
|
|
publicKeys, err := user.GetPublicKeysAsJSON()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-30 17:37:50 +00:00
|
|
|
filters, err := user.GetFiltersAsJSON()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-19 06:41:05 +00:00
|
|
|
fsConfig, err := user.GetFsConfigAsJSON()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-08-01 20:42:46 +00:00
|
|
|
_, err = stmt.Exec(user.Username, user.Password, string(publicKeys), user.HomeDir, user.UID, user.GID, user.MaxSessions, user.QuotaSize,
|
2020-01-19 06:41:05 +00:00
|
|
|
user.QuotaFiles, string(permissions), user.UploadBandwidth, user.DownloadBandwidth, user.Status, user.ExpirationDate, string(filters),
|
|
|
|
string(fsConfig))
|
2019-07-20 10:26:52 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-11 12:53:37 +00:00
|
|
|
func sqlCommonUpdateUser(user User, dbHandle *sql.DB) error {
|
2019-07-20 10:26:52 +00:00
|
|
|
err := validateUser(&user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
q := getUpdateUserQuery()
|
|
|
|
stmt, err := dbHandle.Prepare(q)
|
|
|
|
if err != nil {
|
2019-09-06 13:19:01 +00:00
|
|
|
providerLog(logger.LevelWarn, "error preparing database query %#v: %v", q, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
permissions, err := user.GetPermissionsAsJSON()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-08-01 20:42:46 +00:00
|
|
|
publicKeys, err := user.GetPublicKeysAsJSON()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-30 17:37:50 +00:00
|
|
|
filters, err := user.GetFiltersAsJSON()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-19 06:41:05 +00:00
|
|
|
fsConfig, err := user.GetFsConfigAsJSON()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-08-01 20:42:46 +00:00
|
|
|
_, err = stmt.Exec(user.Password, string(publicKeys), user.HomeDir, user.UID, user.GID, user.MaxSessions, user.QuotaSize,
|
2019-12-30 17:37:50 +00:00
|
|
|
user.QuotaFiles, string(permissions), user.UploadBandwidth, user.DownloadBandwidth, user.Status, user.ExpirationDate,
|
2020-01-19 06:41:05 +00:00
|
|
|
string(filters), string(fsConfig), user.ID)
|
2019-07-20 10:26:52 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-11 12:53:37 +00:00
|
|
|
func sqlCommonDeleteUser(user User, dbHandle *sql.DB) error {
|
2019-07-20 10:26:52 +00:00
|
|
|
q := getDeleteUserQuery()
|
|
|
|
stmt, err := dbHandle.Prepare(q)
|
|
|
|
if err != nil {
|
2019-09-06 13:19:01 +00:00
|
|
|
providerLog(logger.LevelWarn, "error preparing database query %#v: %v", q, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
_, err = stmt.Exec(user.ID)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-27 22:12:44 +00:00
|
|
|
func sqlCommonDumpUsers(dbHandle *sql.DB) ([]User, error) {
|
|
|
|
users := []User{}
|
|
|
|
q := getDumpUsersQuery()
|
|
|
|
stmt, err := dbHandle.Prepare(q)
|
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelWarn, "error preparing database query %#v: %v", q, err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
rows, err := stmt.Query()
|
|
|
|
if err == nil {
|
|
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
|
|
u, err := getUserFromDbRow(nil, rows)
|
2020-01-31 18:04:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return users, err
|
|
|
|
}
|
|
|
|
err = addCredentialsToUser(&u)
|
|
|
|
if err != nil {
|
|
|
|
return users, err
|
2019-12-27 22:12:44 +00:00
|
|
|
}
|
2020-01-31 18:04:00 +00:00
|
|
|
users = append(users, u)
|
2019-12-27 22:12:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return users, err
|
|
|
|
}
|
|
|
|
|
2019-08-11 12:53:37 +00:00
|
|
|
func sqlCommonGetUsers(limit int, offset int, order string, username string, dbHandle *sql.DB) ([]User, error) {
|
2019-07-20 10:26:52 +00:00
|
|
|
users := []User{}
|
|
|
|
q := getUsersQuery(order, username)
|
|
|
|
stmt, err := dbHandle.Prepare(q)
|
|
|
|
if err != nil {
|
2019-09-06 13:19:01 +00:00
|
|
|
providerLog(logger.LevelWarn, "error preparing database query %#v: %v", q, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
var rows *sql.Rows
|
|
|
|
if len(username) > 0 {
|
|
|
|
rows, err = stmt.Query(username, limit, offset)
|
|
|
|
} else {
|
|
|
|
rows, err = stmt.Query(limit, offset)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
|
|
u, err := getUserFromDbRow(nil, rows)
|
|
|
|
if err == nil {
|
2020-01-19 06:41:05 +00:00
|
|
|
users = append(users, HideUserSensitiveData(&u))
|
2019-07-20 10:26:52 +00:00
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return users, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func getUserFromDbRow(row *sql.Row, rows *sql.Rows) (User, error) {
|
|
|
|
var user User
|
|
|
|
var permissions sql.NullString
|
|
|
|
var password sql.NullString
|
|
|
|
var publicKey sql.NullString
|
2019-12-30 17:37:50 +00:00
|
|
|
var filters sql.NullString
|
2020-01-19 06:41:05 +00:00
|
|
|
var fsConfig sql.NullString
|
2019-07-20 10:26:52 +00:00
|
|
|
var err error
|
|
|
|
if row != nil {
|
|
|
|
err = row.Scan(&user.ID, &user.Username, &password, &publicKey, &user.HomeDir, &user.UID, &user.GID, &user.MaxSessions,
|
2019-07-28 17:29:32 +00:00
|
|
|
&user.QuotaSize, &user.QuotaFiles, &permissions, &user.UsedQuotaSize, &user.UsedQuotaFiles, &user.LastQuotaUpdate,
|
2020-01-19 06:41:05 +00:00
|
|
|
&user.UploadBandwidth, &user.DownloadBandwidth, &user.ExpirationDate, &user.LastLogin, &user.Status, &filters, &fsConfig)
|
2019-07-20 10:26:52 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
err = rows.Scan(&user.ID, &user.Username, &password, &publicKey, &user.HomeDir, &user.UID, &user.GID, &user.MaxSessions,
|
2019-07-28 17:29:32 +00:00
|
|
|
&user.QuotaSize, &user.QuotaFiles, &permissions, &user.UsedQuotaSize, &user.UsedQuotaFiles, &user.LastQuotaUpdate,
|
2020-01-19 06:41:05 +00:00
|
|
|
&user.UploadBandwidth, &user.DownloadBandwidth, &user.ExpirationDate, &user.LastLogin, &user.Status, &filters, &fsConfig)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
2019-08-12 16:31:31 +00:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return user, &RecordNotFoundError{err: err.Error()}
|
|
|
|
}
|
2019-07-20 10:26:52 +00:00
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
if password.Valid {
|
|
|
|
user.Password = password.String
|
|
|
|
}
|
|
|
|
if publicKey.Valid {
|
2019-08-01 20:42:46 +00:00
|
|
|
var list []string
|
|
|
|
err = json.Unmarshal([]byte(publicKey.String), &list)
|
|
|
|
if err == nil {
|
2019-08-07 21:41:10 +00:00
|
|
|
user.PublicKeys = list
|
2019-08-01 20:42:46 +00:00
|
|
|
}
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
if permissions.Valid {
|
2019-12-25 17:20:19 +00:00
|
|
|
perms := make(map[string][]string)
|
|
|
|
err = json.Unmarshal([]byte(permissions.String), &perms)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err == nil {
|
2019-12-25 17:20:19 +00:00
|
|
|
user.Permissions = perms
|
|
|
|
} else {
|
|
|
|
// compatibility layer: until version 0.9.4 permissions were a string list
|
|
|
|
var list []string
|
|
|
|
err = json.Unmarshal([]byte(permissions.String), &list)
|
|
|
|
if err == nil {
|
|
|
|
perms["/"] = list
|
|
|
|
user.Permissions = perms
|
|
|
|
}
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-30 17:37:50 +00:00
|
|
|
if filters.Valid {
|
|
|
|
var userFilters UserFilters
|
|
|
|
err = json.Unmarshal([]byte(filters.String), &userFilters)
|
|
|
|
if err == nil {
|
|
|
|
user.Filters = userFilters
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
user.Filters = UserFilters{
|
|
|
|
AllowedIP: []string{},
|
|
|
|
DeniedIP: []string{},
|
|
|
|
}
|
|
|
|
}
|
2020-01-19 06:41:05 +00:00
|
|
|
if fsConfig.Valid {
|
|
|
|
var fs Filesystem
|
|
|
|
err = json.Unmarshal([]byte(fsConfig.String), &fs)
|
|
|
|
if err == nil {
|
|
|
|
user.FsConfig = fs
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
user.FsConfig = Filesystem{
|
|
|
|
Provider: 0,
|
|
|
|
}
|
|
|
|
}
|
2019-07-20 10:26:52 +00:00
|
|
|
return user, err
|
|
|
|
}
|
2020-02-08 13:44:25 +00:00
|
|
|
|
|
|
|
func sqlCommonMigrateDatabase(dbHandle *sql.DB) error {
|
|
|
|
dbVersion, err := sqlCommonGetDatabaseVersion(dbHandle)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if dbVersion.Version == sqlDatabaseVersion {
|
|
|
|
providerLog(logger.LevelDebug, "sql database is updated, current version: %v", dbVersion.Version)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func sqlCommonGetDatabaseVersion(dbHandle *sql.DB) (schemaVersion, error) {
|
|
|
|
var result schemaVersion
|
|
|
|
q := getDatabaseVersionQuery()
|
|
|
|
stmt, err := dbHandle.Prepare(q)
|
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelWarn, "error preparing database query %#v: %v", q, err)
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
row := stmt.QueryRow()
|
|
|
|
err = row.Scan(&result.Version)
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func sqlCommonUpdateDatabaseVersion(dbHandle *sql.DB) error {
|
|
|
|
q := getUpdateDBVersionQuery()
|
|
|
|
stmt, err := dbHandle.Prepare(q)
|
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelWarn, "error preparing database query %#v: %v", q, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
_, err = stmt.Exec(sqlDatabaseVersion)
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|