2019-10-07 16:19:01 +00:00
|
|
|
package httpd
|
2019-07-20 10:26:52 +00:00
|
|
|
|
|
|
|
import (
|
2020-06-20 10:38:04 +00:00
|
|
|
"errors"
|
2019-07-20 10:26:52 +00:00
|
|
|
"net/http"
|
|
|
|
|
2020-05-06 17:36:34 +00:00
|
|
|
"github.com/go-chi/render"
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
"github.com/drakkan/sftpgo/common"
|
2019-07-20 10:26:52 +00:00
|
|
|
"github.com/drakkan/sftpgo/dataprovider"
|
|
|
|
"github.com/drakkan/sftpgo/logger"
|
2020-06-07 21:30:18 +00:00
|
|
|
"github.com/drakkan/sftpgo/vfs"
|
2019-07-20 10:26:52 +00:00
|
|
|
)
|
|
|
|
|
2020-06-20 10:38:04 +00:00
|
|
|
const (
|
|
|
|
quotaUpdateModeAdd = "add"
|
|
|
|
quotaUpdateModeReset = "reset"
|
|
|
|
)
|
|
|
|
|
2019-07-20 10:26:52 +00:00
|
|
|
func getQuotaScans(w http.ResponseWriter, r *http.Request) {
|
2020-07-24 21:39:38 +00:00
|
|
|
render.JSON(w, r, common.QuotaScans.GetUsersQuotaScans())
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2020-06-07 21:30:18 +00:00
|
|
|
func getVFolderQuotaScans(w http.ResponseWriter, r *http.Request) {
|
2020-07-24 21:39:38 +00:00
|
|
|
render.JSON(w, r, common.QuotaScans.GetVFoldersQuotaScans())
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
|
2020-06-20 10:38:04 +00:00
|
|
|
func updateUserQuotaUsage(w http.ResponseWriter, r *http.Request) {
|
2020-01-31 18:04:00 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2019-07-20 10:26:52 +00:00
|
|
|
var u dataprovider.User
|
|
|
|
err := render.DecodeJSON(r.Body, &u)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2020-06-20 10:38:04 +00:00
|
|
|
if u.UsedQuotaFiles < 0 || u.UsedQuotaSize < 0 {
|
|
|
|
sendAPIResponse(w, r, errors.New("Invalid used quota parameters, negative values are not allowed"),
|
|
|
|
"", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
mode, err := getQuotaUpdateMode(r)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2020-07-08 17:59:31 +00:00
|
|
|
user, err := dataprovider.UserExists(u.Username)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
2020-06-20 10:38:04 +00:00
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if mode == quotaUpdateModeAdd && !user.HasQuotaRestrictions() && dataprovider.GetQuotaTracking() == 2 {
|
|
|
|
sendAPIResponse(w, r, errors.New("this user has no quota restrictions, only reset mode is supported"),
|
|
|
|
"", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
if !common.QuotaScans.AddUserQuotaScan(user.Username) {
|
2020-06-20 10:38:04 +00:00
|
|
|
sendAPIResponse(w, r, err, "A quota scan is in progress for this user", http.StatusConflict)
|
|
|
|
return
|
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
defer common.QuotaScans.RemoveUserQuotaScan(user.Username)
|
2021-02-16 18:11:36 +00:00
|
|
|
err = dataprovider.UpdateUserQuota(&user, u.UsedQuotaFiles, u.UsedQuotaSize, mode == quotaUpdateModeReset)
|
2020-06-20 10:38:04 +00:00
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
} else {
|
|
|
|
sendAPIResponse(w, r, err, "Quota updated", http.StatusOK)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateVFolderQuotaUsage(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
var f vfs.BaseVirtualFolder
|
|
|
|
err := render.DecodeJSON(r.Body, &f)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
2019-07-20 10:26:52 +00:00
|
|
|
return
|
|
|
|
}
|
2020-06-20 10:38:04 +00:00
|
|
|
if f.UsedQuotaFiles < 0 || f.UsedQuotaSize < 0 {
|
|
|
|
sendAPIResponse(w, r, errors.New("Invalid used quota parameters, negative values are not allowed"),
|
|
|
|
"", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
mode, err := getQuotaUpdateMode(r)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2021-02-01 18:04:15 +00:00
|
|
|
folder, err := dataprovider.GetFolderByName(f.Name)
|
2020-06-20 10:38:04 +00:00
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
2021-02-01 18:04:15 +00:00
|
|
|
if !common.QuotaScans.AddVFolderQuotaScan(folder.Name) {
|
2020-06-20 10:38:04 +00:00
|
|
|
sendAPIResponse(w, r, err, "A quota scan is in progress for this folder", http.StatusConflict)
|
|
|
|
return
|
|
|
|
}
|
2021-02-01 18:04:15 +00:00
|
|
|
defer common.QuotaScans.RemoveVFolderQuotaScan(folder.Name)
|
2021-02-16 18:11:36 +00:00
|
|
|
err = dataprovider.UpdateVirtualFolderQuota(&folder, f.UsedQuotaFiles, f.UsedQuotaSize, mode == quotaUpdateModeReset)
|
2020-06-20 10:38:04 +00:00
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
} else {
|
|
|
|
sendAPIResponse(w, r, err, "Quota updated", http.StatusOK)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func startQuotaScan(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2020-06-07 21:30:18 +00:00
|
|
|
if dataprovider.GetQuotaTracking() == 0 {
|
|
|
|
sendAPIResponse(w, r, nil, "Quota tracking is disabled!", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
2020-06-20 10:38:04 +00:00
|
|
|
var u dataprovider.User
|
|
|
|
err := render.DecodeJSON(r.Body, &u)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2020-07-08 17:59:31 +00:00
|
|
|
user, err := dataprovider.UserExists(u.Username)
|
2020-06-20 10:38:04 +00:00
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
if common.QuotaScans.AddUserQuotaScan(user.Username) {
|
2020-04-30 12:23:55 +00:00
|
|
|
go doQuotaScan(user) //nolint:errcheck
|
2020-09-13 17:43:56 +00:00
|
|
|
sendAPIResponse(w, r, err, "Scan started", http.StatusAccepted)
|
2019-12-27 22:12:44 +00:00
|
|
|
} else {
|
|
|
|
sendAPIResponse(w, r, err, "Another scan is already in progress", http.StatusConflict)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-07 21:30:18 +00:00
|
|
|
func startVFolderQuotaScan(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2020-06-20 10:38:04 +00:00
|
|
|
if dataprovider.GetQuotaTracking() == 0 {
|
|
|
|
sendAPIResponse(w, r, nil, "Quota tracking is disabled!", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
var f vfs.BaseVirtualFolder
|
|
|
|
err := render.DecodeJSON(r.Body, &f)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2021-02-01 18:04:15 +00:00
|
|
|
folder, err := dataprovider.GetFolderByName(f.Name)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
2020-06-20 10:38:04 +00:00
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
2020-06-07 21:30:18 +00:00
|
|
|
return
|
|
|
|
}
|
2021-02-01 18:04:15 +00:00
|
|
|
if common.QuotaScans.AddVFolderQuotaScan(folder.Name) {
|
2020-06-07 21:30:18 +00:00
|
|
|
go doFolderQuotaScan(folder) //nolint:errcheck
|
2020-09-13 17:43:56 +00:00
|
|
|
sendAPIResponse(w, r, err, "Scan started", http.StatusAccepted)
|
2020-06-07 21:30:18 +00:00
|
|
|
} else {
|
|
|
|
sendAPIResponse(w, r, err, "Another scan is already in progress", http.StatusConflict)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-19 06:41:05 +00:00
|
|
|
func doQuotaScan(user dataprovider.User) error {
|
2020-07-24 21:39:38 +00:00
|
|
|
defer common.QuotaScans.RemoveUserQuotaScan(user.Username)
|
2020-01-19 06:41:05 +00:00
|
|
|
fs, err := user.GetFilesystem("")
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn(logSender, "", "unable scan quota for user %#v error creating filesystem: %v", user.Username, err)
|
|
|
|
return err
|
|
|
|
}
|
2020-12-12 09:31:09 +00:00
|
|
|
defer fs.Close()
|
2020-01-19 12:58:55 +00:00
|
|
|
numFiles, size, err := fs.ScanRootDirContents()
|
2020-01-19 06:41:05 +00:00
|
|
|
if err != nil {
|
2020-01-22 18:42:23 +00:00
|
|
|
logger.Warn(logSender, "", "error scanning user home dir %#v: %v", user.Username, err)
|
2020-06-07 21:30:18 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-02-16 18:11:36 +00:00
|
|
|
err = dataprovider.UpdateUserQuota(&user, numFiles, size, true)
|
2020-06-07 21:30:18 +00:00
|
|
|
logger.Debug(logSender, "", "user home dir scanned, user: %#v, error: %v", user.Username, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func doFolderQuotaScan(folder vfs.BaseVirtualFolder) error {
|
2021-02-01 18:04:15 +00:00
|
|
|
defer common.QuotaScans.RemoveVFolderQuotaScan(folder.Name)
|
2020-11-02 18:16:12 +00:00
|
|
|
fs := vfs.NewOsFs("", "", nil).(*vfs.OsFs)
|
2020-06-07 21:30:18 +00:00
|
|
|
numFiles, size, err := fs.GetDirSize(folder.MappedPath)
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn(logSender, "", "error scanning folder %#v: %v", folder.MappedPath, err)
|
|
|
|
return err
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2021-02-16 18:11:36 +00:00
|
|
|
err = dataprovider.UpdateVirtualFolderQuota(&folder, numFiles, size, true)
|
2021-02-01 18:04:15 +00:00
|
|
|
logger.Debug(logSender, "", "virtual folder %#v scanned, error: %v", folder.Name, err)
|
2020-01-19 06:41:05 +00:00
|
|
|
return err
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2020-06-20 10:38:04 +00:00
|
|
|
|
|
|
|
func getQuotaUpdateMode(r *http.Request) (string, error) {
|
|
|
|
mode := quotaUpdateModeReset
|
|
|
|
if _, ok := r.URL.Query()["mode"]; ok {
|
|
|
|
mode = r.URL.Query().Get("mode")
|
|
|
|
if mode != quotaUpdateModeReset && mode != quotaUpdateModeAdd {
|
|
|
|
return "", errors.New("Invalid mode")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mode, nil
|
|
|
|
}
|