2019-08-12 16:31:31 +00:00
|
|
|
package dataprovider
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
2020-05-06 17:36:34 +00:00
|
|
|
bolt "go.etcd.io/bbolt"
|
|
|
|
|
2019-09-06 13:19:01 +00:00
|
|
|
"github.com/drakkan/sftpgo/logger"
|
2019-08-12 16:31:31 +00:00
|
|
|
"github.com/drakkan/sftpgo/utils"
|
|
|
|
)
|
|
|
|
|
2019-11-13 10:36:21 +00:00
|
|
|
const (
|
2020-02-08 13:44:25 +00:00
|
|
|
boltDatabaseVersion = 3
|
2019-11-13 10:36:21 +00:00
|
|
|
)
|
|
|
|
|
2019-08-12 16:31:31 +00:00
|
|
|
var (
|
|
|
|
usersBucket = []byte("users")
|
|
|
|
usersIDIdxBucket = []byte("users_id_idx")
|
2019-11-13 10:36:21 +00:00
|
|
|
dbVersionBucket = []byte("db_version")
|
|
|
|
dbVersionKey = []byte("version")
|
2019-08-12 16:31:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// BoltProvider auth provider for bolt key/value store
|
|
|
|
type BoltProvider struct {
|
|
|
|
dbHandle *bolt.DB
|
|
|
|
}
|
|
|
|
|
2019-12-25 17:20:19 +00:00
|
|
|
type compatUserV2 struct {
|
|
|
|
ID int64 `json:"id"`
|
|
|
|
Username string `json:"username"`
|
|
|
|
Password string `json:"password,omitempty"`
|
|
|
|
PublicKeys []string `json:"public_keys,omitempty"`
|
|
|
|
HomeDir string `json:"home_dir"`
|
|
|
|
UID int `json:"uid"`
|
|
|
|
GID int `json:"gid"`
|
|
|
|
MaxSessions int `json:"max_sessions"`
|
|
|
|
QuotaSize int64 `json:"quota_size"`
|
|
|
|
QuotaFiles int `json:"quota_files"`
|
|
|
|
Permissions []string `json:"permissions"`
|
|
|
|
UsedQuotaSize int64 `json:"used_quota_size"`
|
|
|
|
UsedQuotaFiles int `json:"used_quota_files"`
|
|
|
|
LastQuotaUpdate int64 `json:"last_quota_update"`
|
|
|
|
UploadBandwidth int64 `json:"upload_bandwidth"`
|
|
|
|
DownloadBandwidth int64 `json:"download_bandwidth"`
|
|
|
|
ExpirationDate int64 `json:"expiration_date"`
|
|
|
|
LastLogin int64 `json:"last_login"`
|
|
|
|
Status int `json:"status"`
|
|
|
|
}
|
|
|
|
|
2019-08-12 16:31:31 +00:00
|
|
|
func initializeBoltProvider(basePath string) error {
|
|
|
|
var err error
|
2020-04-01 21:25:23 +00:00
|
|
|
logSender = fmt.Sprintf("dataprovider_%v", BoltDataProviderName)
|
2019-08-12 16:31:31 +00:00
|
|
|
dbPath := config.Name
|
2020-03-03 08:09:58 +00:00
|
|
|
if !utils.IsFileInputValid(dbPath) {
|
2020-03-02 23:34:06 +00:00
|
|
|
return fmt.Errorf("Invalid database path: %#v", dbPath)
|
|
|
|
}
|
2019-08-12 16:31:31 +00:00
|
|
|
if !filepath.IsAbs(dbPath) {
|
|
|
|
dbPath = filepath.Join(basePath, dbPath)
|
|
|
|
}
|
|
|
|
dbHandle, err := bolt.Open(dbPath, 0600, &bolt.Options{
|
|
|
|
NoGrowSync: false,
|
|
|
|
FreelistType: bolt.FreelistArrayType,
|
|
|
|
Timeout: 5 * time.Second})
|
|
|
|
if err == nil {
|
2019-09-06 13:19:01 +00:00
|
|
|
providerLog(logger.LevelDebug, "bolt key store handle created")
|
2019-08-12 16:31:31 +00:00
|
|
|
err = dbHandle.Update(func(tx *bolt.Tx) error {
|
|
|
|
_, e := tx.CreateBucketIfNotExists(usersBucket)
|
|
|
|
return e
|
|
|
|
})
|
|
|
|
if err != nil {
|
2019-09-06 13:19:01 +00:00
|
|
|
providerLog(logger.LevelWarn, "error creating users bucket: %v", err)
|
2019-08-12 16:31:31 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = dbHandle.Update(func(tx *bolt.Tx) error {
|
|
|
|
_, e := tx.CreateBucketIfNotExists(usersIDIdxBucket)
|
|
|
|
return e
|
|
|
|
})
|
|
|
|
if err != nil {
|
2019-09-06 13:19:01 +00:00
|
|
|
providerLog(logger.LevelWarn, "error creating username idx bucket: %v", err)
|
2019-08-12 16:31:31 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-11-13 10:36:21 +00:00
|
|
|
err = dbHandle.Update(func(tx *bolt.Tx) error {
|
|
|
|
_, e := tx.CreateBucketIfNotExists(dbVersionBucket)
|
|
|
|
return e
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelWarn, "error creating database version bucket: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2019-08-12 16:31:31 +00:00
|
|
|
provider = BoltProvider{dbHandle: dbHandle}
|
|
|
|
} else {
|
2019-09-06 13:19:01 +00:00
|
|
|
providerLog(logger.LevelWarn, "error creating bolt key/value store handler: %v", err)
|
2019-08-12 16:31:31 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-09-13 16:45:36 +00:00
|
|
|
func (p BoltProvider) checkAvailability() error {
|
|
|
|
_, err := p.getUsers(1, 0, "ASC", "")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-12 16:31:31 +00:00
|
|
|
func (p BoltProvider) validateUserAndPass(username string, password string) (User, error) {
|
|
|
|
var user User
|
|
|
|
if len(password) == 0 {
|
|
|
|
return user, errors.New("Credentials cannot be null or empty")
|
|
|
|
}
|
|
|
|
user, err := p.userExists(username)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
return checkUserAndPass(user, password)
|
|
|
|
}
|
|
|
|
|
2020-04-09 21:32:42 +00:00
|
|
|
func (p BoltProvider) validateUserAndPubKey(username string, pubKey []byte) (User, string, error) {
|
2019-08-12 16:31:31 +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-08-12 16:31:31 +00:00
|
|
|
}
|
|
|
|
user, err := p.userExists(username)
|
|
|
|
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-08-12 16:31:31 +00:00
|
|
|
}
|
|
|
|
return checkUserAndPubKey(user, pubKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p BoltProvider) getUserByID(ID int64) (User, error) {
|
|
|
|
var user User
|
|
|
|
err := p.dbHandle.View(func(tx *bolt.Tx) error {
|
|
|
|
bucket, idxBucket, err := getBuckets(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
userIDAsBytes := itob(ID)
|
|
|
|
username := idxBucket.Get(userIDAsBytes)
|
|
|
|
if username == nil {
|
|
|
|
return &RecordNotFoundError{err: fmt.Sprintf("user with ID %v does not exist", ID)}
|
|
|
|
}
|
|
|
|
u := bucket.Get(username)
|
|
|
|
if u == nil {
|
2019-11-13 10:36:21 +00:00
|
|
|
return &RecordNotFoundError{err: fmt.Sprintf("username %#v and ID: %v does not exist", string(username), ID)}
|
2019-08-12 16:31:31 +00:00
|
|
|
}
|
|
|
|
return json.Unmarshal(u, &user)
|
|
|
|
})
|
|
|
|
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
|
2019-11-13 10:36:21 +00:00
|
|
|
func (p BoltProvider) updateLastLogin(username string) error {
|
|
|
|
return p.dbHandle.Update(func(tx *bolt.Tx) error {
|
|
|
|
bucket, _, err := getBuckets(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var u []byte
|
|
|
|
if u = bucket.Get([]byte(username)); u == nil {
|
|
|
|
return &RecordNotFoundError{err: fmt.Sprintf("username %#v does not exist, unable to update last login", username)}
|
|
|
|
}
|
|
|
|
var user User
|
|
|
|
err = json.Unmarshal(u, &user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
user.LastLogin = utils.GetTimeAsMsSinceEpoch(time.Now())
|
|
|
|
buf, err := json.Marshal(user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-30 12:23:55 +00:00
|
|
|
err = bucket.Put([]byte(username), buf)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
return err
|
2019-11-13 10:36:21 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-12 16:31:31 +00:00
|
|
|
func (p BoltProvider) updateQuota(username string, filesAdd int, sizeAdd int64, reset bool) error {
|
|
|
|
return p.dbHandle.Update(func(tx *bolt.Tx) error {
|
|
|
|
bucket, _, err := getBuckets(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var u []byte
|
|
|
|
if u = bucket.Get([]byte(username)); u == nil {
|
2019-11-13 10:36:21 +00:00
|
|
|
return &RecordNotFoundError{err: fmt.Sprintf("username %#v does not exist, unable to update quota", username)}
|
2019-08-12 16:31:31 +00:00
|
|
|
}
|
|
|
|
var user User
|
|
|
|
err = json.Unmarshal(u, &user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if reset {
|
|
|
|
user.UsedQuotaSize = sizeAdd
|
|
|
|
user.UsedQuotaFiles = filesAdd
|
|
|
|
} else {
|
|
|
|
user.UsedQuotaSize += sizeAdd
|
|
|
|
user.UsedQuotaFiles += filesAdd
|
|
|
|
}
|
|
|
|
user.LastQuotaUpdate = utils.GetTimeAsMsSinceEpoch(time.Now())
|
|
|
|
buf, err := json.Marshal(user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return bucket.Put([]byte(username), buf)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p BoltProvider) getUsedQuota(username string) (int, int64, error) {
|
|
|
|
user, err := p.userExists(username)
|
|
|
|
if err != nil {
|
2019-09-06 13:19:01 +00:00
|
|
|
providerLog(logger.LevelWarn, "unable to get quota for user %v error: %v", username, err)
|
2019-08-12 16:31:31 +00:00
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
return user.UsedQuotaFiles, user.UsedQuotaSize, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p BoltProvider) userExists(username string) (User, error) {
|
|
|
|
var user User
|
|
|
|
err := p.dbHandle.View(func(tx *bolt.Tx) error {
|
|
|
|
bucket, _, err := getBuckets(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
u := bucket.Get([]byte(username))
|
|
|
|
if u == nil {
|
2019-10-25 16:37:12 +00:00
|
|
|
return &RecordNotFoundError{err: fmt.Sprintf("username %v does not exist", username)}
|
2019-08-12 16:31:31 +00:00
|
|
|
}
|
|
|
|
return json.Unmarshal(u, &user)
|
|
|
|
})
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p BoltProvider) addUser(user User) error {
|
|
|
|
err := validateUser(&user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return p.dbHandle.Update(func(tx *bolt.Tx) error {
|
|
|
|
bucket, idxBucket, err := getBuckets(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if u := bucket.Get([]byte(user.Username)); u != nil {
|
2019-09-05 21:42:00 +00:00
|
|
|
return fmt.Errorf("username %v already exists", user.Username)
|
2019-08-12 16:31:31 +00:00
|
|
|
}
|
|
|
|
id, err := bucket.NextSequence()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
user.ID = int64(id)
|
|
|
|
buf, err := json.Marshal(user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
userIDAsBytes := itob(user.ID)
|
|
|
|
err = bucket.Put([]byte(user.Username), buf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return idxBucket.Put(userIDAsBytes, []byte(user.Username))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p BoltProvider) updateUser(user User) error {
|
|
|
|
err := validateUser(&user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return p.dbHandle.Update(func(tx *bolt.Tx) error {
|
|
|
|
bucket, _, err := getBuckets(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if u := bucket.Get([]byte(user.Username)); u == nil {
|
2019-09-05 21:42:00 +00:00
|
|
|
return &RecordNotFoundError{err: fmt.Sprintf("username %v does not exist", user.Username)}
|
2019-08-12 16:31:31 +00:00
|
|
|
}
|
|
|
|
buf, err := json.Marshal(user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return bucket.Put([]byte(user.Username), buf)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p BoltProvider) deleteUser(user User) error {
|
|
|
|
return p.dbHandle.Update(func(tx *bolt.Tx) error {
|
|
|
|
bucket, idxBucket, err := getBuckets(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
userIDAsBytes := itob(user.ID)
|
|
|
|
userName := idxBucket.Get(userIDAsBytes)
|
|
|
|
if userName == nil {
|
|
|
|
return &RecordNotFoundError{err: fmt.Sprintf("user with id %v does not exist", user.ID)}
|
|
|
|
}
|
|
|
|
err = bucket.Delete(userName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return idxBucket.Delete(userIDAsBytes)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-27 22:12:44 +00:00
|
|
|
func (p BoltProvider) dumpUsers() ([]User, error) {
|
|
|
|
users := []User{}
|
2020-04-30 12:23:55 +00:00
|
|
|
err := p.dbHandle.View(func(tx *bolt.Tx) error {
|
2019-12-27 22:12:44 +00:00
|
|
|
bucket, _, err := getBuckets(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cursor := bucket.Cursor()
|
|
|
|
for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
|
|
|
|
var user User
|
|
|
|
err = json.Unmarshal(v, &user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-31 18:04:00 +00:00
|
|
|
err = addCredentialsToUser(&user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-27 22:12:44 +00:00
|
|
|
users = append(users, user)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
return users, err
|
|
|
|
}
|
|
|
|
|
2020-03-27 22:26:22 +00:00
|
|
|
func (p BoltProvider) getUserWithUsername(username string) ([]User, error) {
|
|
|
|
users := []User{}
|
|
|
|
var user User
|
|
|
|
user, err := p.userExists(username)
|
|
|
|
if err == nil {
|
|
|
|
users = append(users, HideUserSensitiveData(&user))
|
|
|
|
return users, nil
|
|
|
|
}
|
|
|
|
if _, ok := err.(*RecordNotFoundError); ok {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
return users, err
|
|
|
|
}
|
|
|
|
|
2019-08-12 16:31:31 +00:00
|
|
|
func (p BoltProvider) getUsers(limit int, offset int, order string, username string) ([]User, error) {
|
|
|
|
users := []User{}
|
|
|
|
var err error
|
2019-10-25 16:37:12 +00:00
|
|
|
if limit <= 0 {
|
|
|
|
return users, err
|
|
|
|
}
|
2019-08-12 16:31:31 +00:00
|
|
|
if len(username) > 0 {
|
|
|
|
if offset == 0 {
|
2020-03-27 22:26:22 +00:00
|
|
|
return p.getUserWithUsername(username)
|
2019-08-12 16:31:31 +00:00
|
|
|
}
|
|
|
|
return users, err
|
|
|
|
}
|
|
|
|
err = p.dbHandle.View(func(tx *bolt.Tx) error {
|
|
|
|
bucket, _, err := getBuckets(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cursor := bucket.Cursor()
|
|
|
|
itNum := 0
|
|
|
|
if order == "ASC" {
|
|
|
|
for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
|
|
|
|
itNum++
|
|
|
|
if itNum <= offset {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var user User
|
|
|
|
err = json.Unmarshal(v, &user)
|
|
|
|
if err == nil {
|
2020-01-19 06:41:05 +00:00
|
|
|
users = append(users, HideUserSensitiveData(&user))
|
2019-08-12 16:31:31 +00:00
|
|
|
}
|
|
|
|
if len(users) >= limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for k, v := cursor.Last(); k != nil; k, v = cursor.Prev() {
|
|
|
|
itNum++
|
|
|
|
if itNum <= offset {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var user User
|
|
|
|
err = json.Unmarshal(v, &user)
|
|
|
|
if err == nil {
|
2020-01-19 06:41:05 +00:00
|
|
|
users = append(users, HideUserSensitiveData(&user))
|
2019-08-12 16:31:31 +00:00
|
|
|
}
|
|
|
|
if len(users) >= limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
return users, err
|
|
|
|
}
|
|
|
|
|
2019-09-28 20:48:52 +00:00
|
|
|
func (p BoltProvider) close() error {
|
|
|
|
return p.dbHandle.Close()
|
|
|
|
}
|
|
|
|
|
2020-02-02 21:20:39 +00:00
|
|
|
func (p BoltProvider) reloadConfig() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-08 13:44:25 +00:00
|
|
|
// initializeDatabase does nothing, no initilization is needed for bolt provider
|
|
|
|
func (p BoltProvider) initializeDatabase() error {
|
|
|
|
return errNoInitRequired
|
2019-08-12 16:31:31 +00:00
|
|
|
}
|
2019-11-13 10:36:21 +00:00
|
|
|
|
2020-02-08 13:44:25 +00:00
|
|
|
func (p BoltProvider) migrateDatabase() error {
|
|
|
|
dbVersion, err := getBoltDatabaseVersion(p.dbHandle)
|
2019-11-13 10:36:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-08 13:44:25 +00:00
|
|
|
if dbVersion.Version == boltDatabaseVersion {
|
|
|
|
providerLog(logger.LevelDebug, "bolt database is updated, current version: %v", dbVersion.Version)
|
2019-11-13 10:36:21 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-04-11 09:17:40 +00:00
|
|
|
switch dbVersion.Version {
|
|
|
|
case 1:
|
2020-02-08 13:44:25 +00:00
|
|
|
err = updateDatabaseFrom1To2(p.dbHandle)
|
2019-11-13 10:36:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-08 13:44:25 +00:00
|
|
|
return updateDatabaseFrom2To3(p.dbHandle)
|
2020-04-11 09:17:40 +00:00
|
|
|
case 2:
|
2020-02-08 13:44:25 +00:00
|
|
|
return updateDatabaseFrom2To3(p.dbHandle)
|
2020-04-11 09:17:40 +00:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("Database version not handled: %v", dbVersion.Version)
|
2019-12-25 17:20:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-08 13:44:25 +00:00
|
|
|
// itob returns an 8-byte big endian representation of v.
|
|
|
|
func itob(v int64) []byte {
|
|
|
|
b := make([]byte, 8)
|
|
|
|
binary.BigEndian.PutUint64(b, uint64(v))
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func getBuckets(tx *bolt.Tx) (*bolt.Bucket, *bolt.Bucket, error) {
|
|
|
|
var err error
|
|
|
|
bucket := tx.Bucket(usersBucket)
|
|
|
|
idxBucket := tx.Bucket(usersIDIdxBucket)
|
|
|
|
if bucket == nil || idxBucket == nil {
|
|
|
|
err = fmt.Errorf("unable to find required buckets, bolt database structure not correcly defined")
|
|
|
|
}
|
|
|
|
return bucket, idxBucket, err
|
|
|
|
}
|
|
|
|
|
2019-12-25 17:20:19 +00:00
|
|
|
func updateDatabaseFrom1To2(dbHandle *bolt.DB) error {
|
|
|
|
providerLog(logger.LevelInfo, "updating bolt database version: 1 -> 2")
|
|
|
|
usernames, err := getBoltAvailableUsernames(dbHandle)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, u := range usernames {
|
|
|
|
user, err := provider.userExists(u)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
user.Status = 1
|
|
|
|
err = provider.updateUser(user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
providerLog(logger.LevelInfo, "user %#v updated, \"status\" setted to 1", user.Username)
|
|
|
|
}
|
|
|
|
return updateBoltDatabaseVersion(dbHandle, 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateDatabaseFrom2To3(dbHandle *bolt.DB) error {
|
|
|
|
providerLog(logger.LevelInfo, "updating bolt database version: 2 -> 3")
|
|
|
|
users := []User{}
|
|
|
|
err := dbHandle.View(func(tx *bolt.Tx) error {
|
|
|
|
bucket, _, err := getBuckets(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cursor := bucket.Cursor()
|
|
|
|
for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
|
|
|
|
var compatUser compatUserV2
|
|
|
|
err = json.Unmarshal(v, &compatUser)
|
|
|
|
if err == nil {
|
|
|
|
user := User{}
|
|
|
|
user.ID = compatUser.ID
|
|
|
|
user.Username = compatUser.Username
|
|
|
|
user.Password = compatUser.Password
|
|
|
|
user.PublicKeys = compatUser.PublicKeys
|
|
|
|
user.HomeDir = compatUser.HomeDir
|
|
|
|
user.UID = compatUser.UID
|
|
|
|
user.GID = compatUser.GID
|
|
|
|
user.MaxSessions = compatUser.MaxSessions
|
|
|
|
user.QuotaSize = compatUser.QuotaSize
|
|
|
|
user.QuotaFiles = compatUser.QuotaFiles
|
|
|
|
user.Permissions = make(map[string][]string)
|
|
|
|
user.Permissions["/"] = compatUser.Permissions
|
|
|
|
user.UsedQuotaSize = compatUser.UsedQuotaSize
|
|
|
|
user.UsedQuotaFiles = compatUser.UsedQuotaFiles
|
|
|
|
user.LastQuotaUpdate = compatUser.LastQuotaUpdate
|
|
|
|
user.UploadBandwidth = compatUser.UploadBandwidth
|
|
|
|
user.DownloadBandwidth = compatUser.DownloadBandwidth
|
|
|
|
user.ExpirationDate = compatUser.ExpirationDate
|
|
|
|
user.LastLogin = compatUser.LastLogin
|
|
|
|
user.Status = compatUser.Status
|
|
|
|
users = append(users, user)
|
2019-11-13 10:36:21 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-25 17:20:19 +00:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-11-13 10:36:21 +00:00
|
|
|
}
|
|
|
|
|
2019-12-25 17:20:19 +00:00
|
|
|
for _, user := range users {
|
|
|
|
err = provider.updateUser(user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
providerLog(logger.LevelInfo, "user %#v updated, \"permissions\" setted to %+v", user.Username, user.Permissions)
|
|
|
|
}
|
|
|
|
|
|
|
|
return updateBoltDatabaseVersion(dbHandle, 3)
|
2019-11-13 10:36:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getBoltAvailableUsernames(dbHandle *bolt.DB) ([]string, error) {
|
|
|
|
usernames := []string{}
|
|
|
|
err := dbHandle.View(func(tx *bolt.Tx) error {
|
|
|
|
_, idxBucket, err := getBuckets(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cursor := idxBucket.Cursor()
|
|
|
|
for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
|
|
|
|
usernames = append(usernames, string(v))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return usernames, err
|
|
|
|
}
|
|
|
|
|
2020-02-08 13:44:25 +00:00
|
|
|
func getBoltDatabaseVersion(dbHandle *bolt.DB) (schemaVersion, error) {
|
|
|
|
var dbVersion schemaVersion
|
2019-11-13 10:36:21 +00:00
|
|
|
err := dbHandle.View(func(tx *bolt.Tx) error {
|
|
|
|
bucket := tx.Bucket(dbVersionBucket)
|
|
|
|
if bucket == nil {
|
|
|
|
return fmt.Errorf("unable to find database version bucket")
|
|
|
|
}
|
|
|
|
v := bucket.Get(dbVersionKey)
|
|
|
|
if v == nil {
|
2020-02-08 13:44:25 +00:00
|
|
|
dbVersion = schemaVersion{
|
2019-11-13 10:36:21 +00:00
|
|
|
Version: 1,
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return json.Unmarshal(v, &dbVersion)
|
|
|
|
})
|
|
|
|
return dbVersion, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateBoltDatabaseVersion(dbHandle *bolt.DB, version int) error {
|
|
|
|
err := dbHandle.Update(func(tx *bolt.Tx) error {
|
|
|
|
bucket := tx.Bucket(dbVersionBucket)
|
|
|
|
if bucket == nil {
|
|
|
|
return fmt.Errorf("unable to find database version bucket")
|
|
|
|
}
|
2020-02-08 13:44:25 +00:00
|
|
|
newDbVersion := schemaVersion{
|
2019-11-13 10:36:21 +00:00
|
|
|
Version: version,
|
|
|
|
}
|
|
|
|
buf, err := json.Marshal(newDbVersion)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return bucket.Put(dbVersionKey, buf)
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|