2019-12-27 22:12:44 +00:00
|
|
|
package httpd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
"github.com/drakkan/sftpgo/common"
|
2019-12-27 22:12:44 +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-12-27 22:12:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func dumpData(w http.ResponseWriter, r *http.Request) {
|
2020-02-02 21:20:39 +00:00
|
|
|
var outputFile, indent string
|
2019-12-27 22:12:44 +00:00
|
|
|
if _, ok := r.URL.Query()["output_file"]; ok {
|
|
|
|
outputFile = strings.TrimSpace(r.URL.Query().Get("output_file"))
|
|
|
|
}
|
2020-02-02 21:20:39 +00:00
|
|
|
if _, ok := r.URL.Query()["indent"]; ok {
|
|
|
|
indent = strings.TrimSpace(r.URL.Query().Get("indent"))
|
|
|
|
}
|
2021-01-05 08:50:22 +00:00
|
|
|
if outputFile == "" {
|
2019-12-27 22:12:44 +00:00
|
|
|
sendAPIResponse(w, r, errors.New("Invalid or missing output_file"), "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if filepath.IsAbs(outputFile) {
|
|
|
|
sendAPIResponse(w, r, fmt.Errorf("Invalid output_file %#v: it must be a relative path", outputFile), "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if strings.Contains(outputFile, "..") {
|
|
|
|
sendAPIResponse(w, r, fmt.Errorf("Invalid output_file %#v", outputFile), "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
outputFile = filepath.Join(backupsPath, outputFile)
|
2020-04-30 12:23:55 +00:00
|
|
|
err := os.MkdirAll(filepath.Dir(outputFile), 0700)
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn(logSender, "", "dumping data error: %v, output file: %#v", err, outputFile)
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
2019-12-27 22:12:44 +00:00
|
|
|
logger.Debug(logSender, "", "dumping data to: %#v", outputFile)
|
|
|
|
|
2020-07-08 17:59:31 +00:00
|
|
|
backup, err := dataprovider.DumpData()
|
2019-12-27 22:12:44 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Warn(logSender, "", "dumping data error: %v, output file: %#v", err, outputFile)
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
2020-02-02 21:20:39 +00:00
|
|
|
var dump []byte
|
|
|
|
if indent == "1" {
|
2020-06-07 21:30:18 +00:00
|
|
|
dump, err = json.MarshalIndent(backup, "", " ")
|
2020-02-02 21:20:39 +00:00
|
|
|
} else {
|
2020-06-07 21:30:18 +00:00
|
|
|
dump, err = json.Marshal(backup)
|
2020-02-02 21:20:39 +00:00
|
|
|
}
|
2019-12-27 22:12:44 +00:00
|
|
|
if err == nil {
|
2020-02-02 21:20:39 +00:00
|
|
|
err = ioutil.WriteFile(outputFile, dump, 0600)
|
2019-12-27 22:12:44 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn(logSender, "", "dumping data error: %v, output file: %#v", err, outputFile)
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
logger.Debug(logSender, "", "dumping data completed, output file: %#v, error: %v", outputFile, err)
|
|
|
|
sendAPIResponse(w, r, err, "Data saved", http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadData(w http.ResponseWriter, r *http.Request) {
|
2020-01-31 18:10:45 +00:00
|
|
|
inputFile, scanQuota, mode, err := getLoaddataOptions(r)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return
|
2019-12-27 22:12:44 +00:00
|
|
|
}
|
|
|
|
if !filepath.IsAbs(inputFile) {
|
|
|
|
sendAPIResponse(w, r, fmt.Errorf("Invalid input_file %#v: it must be an absolute path", inputFile), "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fi, err := os.Stat(inputFile)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
2020-10-20 16:42:37 +00:00
|
|
|
if fi.Size() > MaxRestoreSize {
|
2020-02-02 21:20:39 +00:00
|
|
|
sendAPIResponse(w, r, err, fmt.Sprintf("Unable to restore input file: %#v size too big: %v/%v bytes",
|
2020-10-20 16:42:37 +00:00
|
|
|
inputFile, fi.Size(), MaxRestoreSize), http.StatusBadRequest)
|
2019-12-27 22:12:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
content, err := ioutil.ReadFile(inputFile)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
2020-11-22 20:53:04 +00:00
|
|
|
dump, err := dataprovider.ParseDumpData(content)
|
2019-12-27 22:12:44 +00:00
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, fmt.Sprintf("Unable to parse input file: %#v", inputFile), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-20 16:42:37 +00:00
|
|
|
if err = RestoreFolders(dump.Folders, inputFile, scanQuota); err != nil {
|
2020-06-07 21:30:18 +00:00
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
2019-12-27 22:12:44 +00:00
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
|
2020-10-20 16:42:37 +00:00
|
|
|
if err = RestoreUsers(dump.Users, inputFile, mode, scanQuota); err != nil {
|
2020-06-07 21:30:18 +00:00
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-27 22:12:44 +00:00
|
|
|
logger.Debug(logSender, "", "backup restored, users: %v", len(dump.Users))
|
|
|
|
sendAPIResponse(w, r, err, "Data restored", http.StatusOK)
|
|
|
|
}
|
2020-01-19 06:41:05 +00:00
|
|
|
|
2020-01-31 18:10:45 +00:00
|
|
|
func getLoaddataOptions(r *http.Request) (string, int, int, error) {
|
|
|
|
var inputFile string
|
|
|
|
var err error
|
|
|
|
scanQuota := 0
|
|
|
|
restoreMode := 0
|
|
|
|
if _, ok := r.URL.Query()["input_file"]; ok {
|
|
|
|
inputFile = strings.TrimSpace(r.URL.Query().Get("input_file"))
|
|
|
|
}
|
|
|
|
if _, ok := r.URL.Query()["scan_quota"]; ok {
|
|
|
|
scanQuota, err = strconv.Atoi(r.URL.Query().Get("scan_quota"))
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("invalid scan_quota: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, ok := r.URL.Query()["mode"]; ok {
|
|
|
|
restoreMode, err = strconv.Atoi(r.URL.Query().Get("mode"))
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("invalid mode: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return inputFile, scanQuota, restoreMode, err
|
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
|
2020-10-20 16:42:37 +00:00
|
|
|
// RestoreFolders restores the specified folders
|
|
|
|
func RestoreFolders(folders []vfs.BaseVirtualFolder, inputFile string, scanQuota int) error {
|
2020-06-07 21:30:18 +00:00
|
|
|
for _, folder := range folders {
|
2020-07-08 17:59:31 +00:00
|
|
|
_, err := dataprovider.GetFolderByPath(folder.MappedPath)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err == nil {
|
|
|
|
logger.Debug(logSender, "", "folder %#v already exists, restore not needed", folder.MappedPath)
|
|
|
|
continue
|
|
|
|
}
|
2021-01-05 08:50:22 +00:00
|
|
|
folder := folder // pin
|
2020-06-07 21:30:18 +00:00
|
|
|
folder.Users = nil
|
2021-01-05 08:50:22 +00:00
|
|
|
err = dataprovider.AddFolder(&folder)
|
2020-06-07 21:30:18 +00:00
|
|
|
logger.Debug(logSender, "", "adding new folder: %+v, dump file: %#v, error: %v", folder, inputFile, err)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if scanQuota >= 1 {
|
2020-07-24 21:39:38 +00:00
|
|
|
if common.QuotaScans.AddVFolderQuotaScan(folder.MappedPath) {
|
2020-06-07 21:30:18 +00:00
|
|
|
logger.Debug(logSender, "", "starting quota scan for restored folder: %#v", folder.MappedPath)
|
|
|
|
go doFolderQuotaScan(folder) //nolint:errcheck
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-20 16:42:37 +00:00
|
|
|
// RestoreUsers restores the specified users
|
|
|
|
func RestoreUsers(users []dataprovider.User, inputFile string, mode, scanQuota int) error {
|
2020-06-07 21:30:18 +00:00
|
|
|
for _, user := range users {
|
2021-01-05 08:50:22 +00:00
|
|
|
user := user // pin
|
2020-07-08 17:59:31 +00:00
|
|
|
u, err := dataprovider.UserExists(user.Username)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err == nil {
|
|
|
|
if mode == 1 {
|
|
|
|
logger.Debug(logSender, "", "loaddata mode 1, existing user %#v not updated", u.Username)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
user.ID = u.ID
|
2021-01-05 08:50:22 +00:00
|
|
|
err = dataprovider.UpdateUser(&user)
|
2020-06-07 21:30:18 +00:00
|
|
|
user.Password = "[redacted]"
|
|
|
|
logger.Debug(logSender, "", "restoring existing user: %+v, dump file: %#v, error: %v", user, inputFile, err)
|
2020-09-01 14:10:26 +00:00
|
|
|
if mode == 2 && err == nil {
|
|
|
|
disconnectUser(user.Username)
|
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
} else {
|
2021-01-05 08:50:22 +00:00
|
|
|
err = dataprovider.AddUser(&user)
|
2020-06-07 21:30:18 +00:00
|
|
|
user.Password = "[redacted]"
|
|
|
|
logger.Debug(logSender, "", "adding new user: %+v, dump file: %#v, error: %v", user, inputFile, err)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if scanQuota == 1 || (scanQuota == 2 && user.HasQuotaRestrictions()) {
|
2020-07-24 21:39:38 +00:00
|
|
|
if common.QuotaScans.AddUserQuotaScan(user.Username) {
|
2020-06-07 21:30:18 +00:00
|
|
|
logger.Debug(logSender, "", "starting quota scan for restored user: %#v", user.Username)
|
|
|
|
go doQuotaScan(user) //nolint:errcheck
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|