2019-10-07 16:19:01 +00:00
|
|
|
package httpd
|
|
|
|
|
|
|
|
import (
|
2020-01-31 18:04:00 +00:00
|
|
|
"errors"
|
2019-10-07 16:19:01 +00:00
|
|
|
"fmt"
|
2019-10-09 09:48:54 +00:00
|
|
|
"html/template"
|
2020-01-31 18:04:00 +00:00
|
|
|
"io/ioutil"
|
2019-10-07 16:19:01 +00:00
|
|
|
"net/http"
|
2021-01-17 21:29:08 +00:00
|
|
|
"net/url"
|
2020-11-16 18:21:50 +00:00
|
|
|
"path"
|
2019-10-07 16:19:01 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2019-11-13 10:36:21 +00:00
|
|
|
"time"
|
2019-10-07 16:19:01 +00:00
|
|
|
|
2021-01-25 20:31:33 +00:00
|
|
|
"github.com/go-chi/render"
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
"github.com/drakkan/sftpgo/common"
|
2019-10-07 16:19:01 +00:00
|
|
|
"github.com/drakkan/sftpgo/dataprovider"
|
2020-11-30 20:46:34 +00:00
|
|
|
"github.com/drakkan/sftpgo/kms"
|
2019-10-19 05:52:58 +00:00
|
|
|
"github.com/drakkan/sftpgo/utils"
|
2020-06-19 15:08:51 +00:00
|
|
|
"github.com/drakkan/sftpgo/version"
|
2020-02-23 10:30:26 +00:00
|
|
|
"github.com/drakkan/sftpgo/vfs"
|
2019-10-07 16:19:01 +00:00
|
|
|
)
|
|
|
|
|
2021-01-25 20:31:33 +00:00
|
|
|
type userPageMode int
|
|
|
|
|
|
|
|
const (
|
|
|
|
userPageModeAdd userPageMode = iota + 1
|
|
|
|
userPageModeUpdate
|
|
|
|
userPageModeTemplate
|
|
|
|
)
|
|
|
|
|
2021-02-01 18:04:15 +00:00
|
|
|
type folderPageMode int
|
|
|
|
|
|
|
|
const (
|
|
|
|
folderPageModeAdd folderPageMode = iota + 1
|
|
|
|
folderPageModeUpdate
|
|
|
|
)
|
|
|
|
|
2019-10-07 16:19:01 +00:00
|
|
|
const (
|
2020-06-07 21:30:18 +00:00
|
|
|
templateBase = "base.html"
|
|
|
|
templateUsers = "users.html"
|
|
|
|
templateUser = "user.html"
|
2021-01-17 21:29:08 +00:00
|
|
|
templateAdmins = "admins.html"
|
|
|
|
templateAdmin = "admin.html"
|
2020-06-07 21:30:18 +00:00
|
|
|
templateConnections = "connections.html"
|
|
|
|
templateFolders = "folders.html"
|
|
|
|
templateFolder = "folder.html"
|
|
|
|
templateMessage = "message.html"
|
2020-12-08 10:18:34 +00:00
|
|
|
templateStatus = "status.html"
|
2021-01-17 21:29:08 +00:00
|
|
|
templateLogin = "login.html"
|
|
|
|
templateChangePwd = "changepwd.html"
|
2021-01-22 18:42:18 +00:00
|
|
|
templateMaintenance = "maintenance.html"
|
2020-06-07 21:30:18 +00:00
|
|
|
pageUsersTitle = "Users"
|
2021-01-17 21:29:08 +00:00
|
|
|
pageAdminsTitle = "Admins"
|
2020-06-07 21:30:18 +00:00
|
|
|
pageConnectionsTitle = "Connections"
|
2020-12-08 10:18:34 +00:00
|
|
|
pageStatusTitle = "Status"
|
2020-06-07 21:30:18 +00:00
|
|
|
pageFoldersTitle = "Folders"
|
2021-01-17 21:29:08 +00:00
|
|
|
pageChangePwdTitle = "Change password"
|
2021-01-22 18:42:18 +00:00
|
|
|
pageMaintenanceTitle = "Maintenance"
|
2020-06-07 21:30:18 +00:00
|
|
|
page400Title = "Bad request"
|
2021-01-17 21:29:08 +00:00
|
|
|
page403Title = "Forbidden"
|
2020-06-07 21:30:18 +00:00
|
|
|
page404Title = "Not found"
|
|
|
|
page404Body = "The page you are looking for does not exist."
|
|
|
|
page500Title = "Internal Server Error"
|
|
|
|
page500Body = "The server is unable to fulfill your request."
|
|
|
|
defaultQueryLimit = 500
|
|
|
|
webDateTimeFormat = "2006-01-02 15:04:05" // YYYY-MM-DD HH:MM:SS
|
2020-11-22 20:53:04 +00:00
|
|
|
redactedSecret = "[**redacted**]"
|
2021-02-03 07:55:28 +00:00
|
|
|
csrfFormToken = "_form_token"
|
|
|
|
csrfHeaderToken = "X-CSRF-TOKEN"
|
2019-10-07 16:19:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
templates = make(map[string]*template.Template)
|
|
|
|
)
|
|
|
|
|
|
|
|
type basePage struct {
|
2021-01-17 21:29:08 +00:00
|
|
|
Title string
|
|
|
|
CurrentURL string
|
|
|
|
UsersURL string
|
|
|
|
UserURL string
|
2021-01-25 20:31:33 +00:00
|
|
|
UserTemplateURL string
|
2021-01-17 21:29:08 +00:00
|
|
|
AdminsURL string
|
|
|
|
AdminURL string
|
|
|
|
QuotaScanURL string
|
|
|
|
ConnectionsURL string
|
|
|
|
FoldersURL string
|
|
|
|
FolderURL string
|
|
|
|
LogoutURL string
|
|
|
|
ChangeAdminPwdURL string
|
|
|
|
FolderQuotaScanURL string
|
|
|
|
StatusURL string
|
2021-01-22 18:42:18 +00:00
|
|
|
MaintenanceURL string
|
2021-01-17 21:29:08 +00:00
|
|
|
UsersTitle string
|
|
|
|
AdminsTitle string
|
|
|
|
ConnectionsTitle string
|
|
|
|
FoldersTitle string
|
|
|
|
StatusTitle string
|
2021-01-22 18:42:18 +00:00
|
|
|
MaintenanceTitle string
|
2021-01-17 21:29:08 +00:00
|
|
|
Version string
|
2021-02-03 07:55:28 +00:00
|
|
|
CSRFToken string
|
2021-01-17 21:29:08 +00:00
|
|
|
LoggedAdmin *dataprovider.Admin
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type usersPage struct {
|
|
|
|
basePage
|
|
|
|
Users []dataprovider.User
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
type adminsPage struct {
|
|
|
|
basePage
|
|
|
|
Admins []dataprovider.Admin
|
|
|
|
}
|
|
|
|
|
2020-06-07 21:30:18 +00:00
|
|
|
type foldersPage struct {
|
|
|
|
basePage
|
|
|
|
Folders []vfs.BaseVirtualFolder
|
|
|
|
}
|
|
|
|
|
2019-10-07 16:19:01 +00:00
|
|
|
type connectionsPage struct {
|
|
|
|
basePage
|
2020-07-24 21:39:38 +00:00
|
|
|
Connections []common.ConnectionStatus
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 10:18:34 +00:00
|
|
|
type statusPage struct {
|
|
|
|
basePage
|
|
|
|
Status ServicesStatus
|
|
|
|
}
|
|
|
|
|
2019-10-07 16:19:01 +00:00
|
|
|
type userPage struct {
|
|
|
|
basePage
|
2021-01-25 20:31:33 +00:00
|
|
|
User *dataprovider.User
|
2020-02-19 21:39:30 +00:00
|
|
|
RootPerms []string
|
|
|
|
Error string
|
|
|
|
ValidPerms []string
|
|
|
|
ValidSSHLoginMethods []string
|
2020-08-17 10:49:20 +00:00
|
|
|
ValidProtocols []string
|
2020-02-19 21:39:30 +00:00
|
|
|
RootDirPerms []string
|
2020-11-22 20:53:04 +00:00
|
|
|
RedactedSecret string
|
2021-01-25 20:31:33 +00:00
|
|
|
Mode userPageMode
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
type adminPage struct {
|
|
|
|
basePage
|
|
|
|
Admin *dataprovider.Admin
|
|
|
|
Error string
|
|
|
|
IsAdd bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type changePwdPage struct {
|
|
|
|
basePage
|
|
|
|
Error string
|
|
|
|
}
|
|
|
|
|
2021-01-22 18:42:18 +00:00
|
|
|
type maintenancePage struct {
|
|
|
|
basePage
|
|
|
|
BackupPath string
|
|
|
|
RestorePath string
|
|
|
|
Error string
|
|
|
|
}
|
|
|
|
|
2020-06-07 21:30:18 +00:00
|
|
|
type folderPage struct {
|
|
|
|
basePage
|
|
|
|
Folder vfs.BaseVirtualFolder
|
|
|
|
Error string
|
2021-02-01 18:04:15 +00:00
|
|
|
Mode folderPageMode
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
|
2019-10-07 16:19:01 +00:00
|
|
|
type messagePage struct {
|
|
|
|
basePage
|
|
|
|
Error string
|
|
|
|
Success string
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
type loginPage struct {
|
|
|
|
CurrentURL string
|
|
|
|
Version string
|
|
|
|
Error string
|
2021-02-03 07:55:28 +00:00
|
|
|
CSRFToken string
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
|
|
|
|
2021-01-25 20:31:33 +00:00
|
|
|
type userTemplateFields struct {
|
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
PublicKey string
|
|
|
|
}
|
|
|
|
|
2019-10-07 16:19:01 +00:00
|
|
|
func loadTemplates(templatesPath string) {
|
|
|
|
usersPaths := []string{
|
|
|
|
filepath.Join(templatesPath, templateBase),
|
|
|
|
filepath.Join(templatesPath, templateUsers),
|
|
|
|
}
|
|
|
|
userPaths := []string{
|
|
|
|
filepath.Join(templatesPath, templateBase),
|
|
|
|
filepath.Join(templatesPath, templateUser),
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
adminsPaths := []string{
|
|
|
|
filepath.Join(templatesPath, templateBase),
|
|
|
|
filepath.Join(templatesPath, templateAdmins),
|
|
|
|
}
|
|
|
|
adminPaths := []string{
|
|
|
|
filepath.Join(templatesPath, templateBase),
|
|
|
|
filepath.Join(templatesPath, templateAdmin),
|
|
|
|
}
|
|
|
|
changePwdPaths := []string{
|
|
|
|
filepath.Join(templatesPath, templateBase),
|
|
|
|
filepath.Join(templatesPath, templateChangePwd),
|
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
connectionsPaths := []string{
|
|
|
|
filepath.Join(templatesPath, templateBase),
|
|
|
|
filepath.Join(templatesPath, templateConnections),
|
|
|
|
}
|
|
|
|
messagePath := []string{
|
|
|
|
filepath.Join(templatesPath, templateBase),
|
|
|
|
filepath.Join(templatesPath, templateMessage),
|
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
foldersPath := []string{
|
|
|
|
filepath.Join(templatesPath, templateBase),
|
|
|
|
filepath.Join(templatesPath, templateFolders),
|
|
|
|
}
|
|
|
|
folderPath := []string{
|
|
|
|
filepath.Join(templatesPath, templateBase),
|
|
|
|
filepath.Join(templatesPath, templateFolder),
|
|
|
|
}
|
2020-12-08 10:18:34 +00:00
|
|
|
statusPath := []string{
|
|
|
|
filepath.Join(templatesPath, templateBase),
|
|
|
|
filepath.Join(templatesPath, templateStatus),
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
loginPath := []string{
|
|
|
|
filepath.Join(templatesPath, templateLogin),
|
|
|
|
}
|
2021-01-22 18:42:18 +00:00
|
|
|
maintenancePath := []string{
|
|
|
|
filepath.Join(templatesPath, templateBase),
|
|
|
|
filepath.Join(templatesPath, templateMaintenance),
|
|
|
|
}
|
2020-03-02 08:34:13 +00:00
|
|
|
usersTmpl := utils.LoadTemplate(template.ParseFiles(usersPaths...))
|
|
|
|
userTmpl := utils.LoadTemplate(template.ParseFiles(userPaths...))
|
2021-01-17 21:29:08 +00:00
|
|
|
adminsTmpl := utils.LoadTemplate(template.ParseFiles(adminsPaths...))
|
|
|
|
adminTmpl := utils.LoadTemplate(template.ParseFiles(adminPaths...))
|
2020-03-02 08:34:13 +00:00
|
|
|
connectionsTmpl := utils.LoadTemplate(template.ParseFiles(connectionsPaths...))
|
|
|
|
messageTmpl := utils.LoadTemplate(template.ParseFiles(messagePath...))
|
2020-06-07 21:30:18 +00:00
|
|
|
foldersTmpl := utils.LoadTemplate(template.ParseFiles(foldersPath...))
|
|
|
|
folderTmpl := utils.LoadTemplate(template.ParseFiles(folderPath...))
|
2020-12-08 10:18:34 +00:00
|
|
|
statusTmpl := utils.LoadTemplate(template.ParseFiles(statusPath...))
|
2021-01-17 21:29:08 +00:00
|
|
|
loginTmpl := utils.LoadTemplate(template.ParseFiles(loginPath...))
|
|
|
|
changePwdTmpl := utils.LoadTemplate(template.ParseFiles(changePwdPaths...))
|
2021-01-22 18:42:18 +00:00
|
|
|
maintenanceTmpl := utils.LoadTemplate(template.ParseFiles(maintenancePath...))
|
2019-10-07 16:19:01 +00:00
|
|
|
|
|
|
|
templates[templateUsers] = usersTmpl
|
|
|
|
templates[templateUser] = userTmpl
|
2021-01-17 21:29:08 +00:00
|
|
|
templates[templateAdmins] = adminsTmpl
|
|
|
|
templates[templateAdmin] = adminTmpl
|
2019-10-07 16:19:01 +00:00
|
|
|
templates[templateConnections] = connectionsTmpl
|
|
|
|
templates[templateMessage] = messageTmpl
|
2020-06-07 21:30:18 +00:00
|
|
|
templates[templateFolders] = foldersTmpl
|
|
|
|
templates[templateFolder] = folderTmpl
|
2020-12-08 10:18:34 +00:00
|
|
|
templates[templateStatus] = statusTmpl
|
2021-01-17 21:29:08 +00:00
|
|
|
templates[templateLogin] = loginTmpl
|
|
|
|
templates[templateChangePwd] = changePwdTmpl
|
2021-01-22 18:42:18 +00:00
|
|
|
templates[templateMaintenance] = maintenanceTmpl
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func getBasePageData(title, currentURL string, r *http.Request) basePage {
|
2021-02-03 07:55:28 +00:00
|
|
|
var csrfToken string
|
|
|
|
if currentURL != "" {
|
|
|
|
csrfToken = createCSRFToken()
|
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
return basePage{
|
2021-01-17 21:29:08 +00:00
|
|
|
Title: title,
|
|
|
|
CurrentURL: currentURL,
|
|
|
|
UsersURL: webUsersPath,
|
|
|
|
UserURL: webUserPath,
|
2021-01-25 20:31:33 +00:00
|
|
|
UserTemplateURL: webTemplateUser,
|
2021-01-17 21:29:08 +00:00
|
|
|
AdminsURL: webAdminsPath,
|
|
|
|
AdminURL: webAdminPath,
|
|
|
|
FoldersURL: webFoldersPath,
|
|
|
|
FolderURL: webFolderPath,
|
|
|
|
LogoutURL: webLogoutPath,
|
|
|
|
ChangeAdminPwdURL: webChangeAdminPwdPath,
|
|
|
|
QuotaScanURL: webQuotaScanPath,
|
|
|
|
ConnectionsURL: webConnectionsPath,
|
|
|
|
StatusURL: webStatusPath,
|
|
|
|
FolderQuotaScanURL: webScanVFolderPath,
|
2021-01-22 18:42:18 +00:00
|
|
|
MaintenanceURL: webMaintenancePath,
|
2021-01-17 21:29:08 +00:00
|
|
|
UsersTitle: pageUsersTitle,
|
|
|
|
AdminsTitle: pageAdminsTitle,
|
|
|
|
ConnectionsTitle: pageConnectionsTitle,
|
|
|
|
FoldersTitle: pageFoldersTitle,
|
|
|
|
StatusTitle: pageStatusTitle,
|
2021-01-22 18:42:18 +00:00
|
|
|
MaintenanceTitle: pageMaintenanceTitle,
|
2021-01-17 21:29:08 +00:00
|
|
|
Version: version.GetAsString(),
|
|
|
|
LoggedAdmin: getAdminFromToken(r),
|
2021-02-03 07:55:28 +00:00
|
|
|
CSRFToken: csrfToken,
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func renderTemplate(w http.ResponseWriter, tmplName string, data interface{}) {
|
|
|
|
err := templates[tmplName].ExecuteTemplate(w, tmplName, data)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func renderMessagePage(w http.ResponseWriter, r *http.Request, title, body string, statusCode int, err error, message string) {
|
2019-10-07 16:19:01 +00:00
|
|
|
var errorString string
|
2021-01-17 21:29:08 +00:00
|
|
|
if body != "" {
|
2019-10-07 16:19:01 +00:00
|
|
|
errorString = body + " "
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
errorString += err.Error()
|
|
|
|
}
|
|
|
|
data := messagePage{
|
2021-01-17 21:29:08 +00:00
|
|
|
basePage: getBasePageData(title, "", r),
|
2019-10-07 16:19:01 +00:00
|
|
|
Error: errorString,
|
|
|
|
Success: message,
|
|
|
|
}
|
|
|
|
w.WriteHeader(statusCode)
|
|
|
|
renderTemplate(w, templateMessage, data)
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func renderInternalServerErrorPage(w http.ResponseWriter, r *http.Request, err error) {
|
|
|
|
renderMessagePage(w, r, page500Title, page500Body, http.StatusInternalServerError, err, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func renderBadRequestPage(w http.ResponseWriter, r *http.Request, err error) {
|
|
|
|
renderMessagePage(w, r, page400Title, "", http.StatusBadRequest, err, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func renderForbiddenPage(w http.ResponseWriter, r *http.Request, body string) {
|
|
|
|
renderMessagePage(w, r, page403Title, "", http.StatusForbidden, nil, body)
|
|
|
|
}
|
|
|
|
|
|
|
|
func renderNotFoundPage(w http.ResponseWriter, r *http.Request, err error) {
|
|
|
|
renderMessagePage(w, r, page404Title, page404Body, http.StatusNotFound, err, "")
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func renderChangePwdPage(w http.ResponseWriter, r *http.Request, error string) {
|
|
|
|
data := changePwdPage{
|
|
|
|
basePage: getBasePageData(pageChangePwdTitle, webChangeAdminPwdPath, r),
|
|
|
|
Error: error,
|
|
|
|
}
|
|
|
|
|
|
|
|
renderTemplate(w, templateChangePwd, data)
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
|
|
|
|
2021-01-22 18:42:18 +00:00
|
|
|
func renderMaintenancePage(w http.ResponseWriter, r *http.Request, error string) {
|
|
|
|
data := maintenancePage{
|
|
|
|
basePage: getBasePageData(pageMaintenanceTitle, webMaintenancePath, r),
|
|
|
|
BackupPath: webBackupPath,
|
|
|
|
RestorePath: webRestorePath,
|
|
|
|
Error: error,
|
|
|
|
}
|
|
|
|
|
|
|
|
renderTemplate(w, templateMaintenance, data)
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func renderAddUpdateAdminPage(w http.ResponseWriter, r *http.Request, admin *dataprovider.Admin,
|
|
|
|
error string, isAdd bool) {
|
|
|
|
currentURL := webAdminPath
|
|
|
|
if !isAdd {
|
|
|
|
currentURL = fmt.Sprintf("%v/%v", webAdminPath, url.PathEscape(admin.Username))
|
|
|
|
}
|
|
|
|
data := adminPage{
|
|
|
|
basePage: getBasePageData("Add a new user", currentURL, r),
|
|
|
|
Admin: admin,
|
|
|
|
Error: error,
|
|
|
|
IsAdd: isAdd,
|
|
|
|
}
|
|
|
|
|
|
|
|
renderTemplate(w, templateAdmin, data)
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
|
|
|
|
2021-01-25 20:31:33 +00:00
|
|
|
func renderUserPage(w http.ResponseWriter, r *http.Request, user *dataprovider.User, mode userPageMode, error string) {
|
2020-11-30 20:46:34 +00:00
|
|
|
user.SetEmptySecretsIfNil()
|
2021-01-25 20:31:33 +00:00
|
|
|
var title, currentURL string
|
|
|
|
switch mode {
|
|
|
|
case userPageModeAdd:
|
|
|
|
title = "Add a new user"
|
|
|
|
currentURL = webUserPath
|
|
|
|
case userPageModeUpdate:
|
|
|
|
title = "Update user"
|
|
|
|
currentURL = fmt.Sprintf("%v/%v", webUserPath, url.PathEscape(user.Username))
|
|
|
|
case userPageModeTemplate:
|
|
|
|
title = "User template"
|
|
|
|
currentURL = webTemplateUser
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
|
|
|
data := userPage{
|
2021-01-25 20:31:33 +00:00
|
|
|
basePage: getBasePageData(title, currentURL, r),
|
|
|
|
Mode: mode,
|
2020-02-19 21:39:30 +00:00
|
|
|
Error: error,
|
|
|
|
User: user,
|
|
|
|
ValidPerms: dataprovider.ValidPerms,
|
|
|
|
ValidSSHLoginMethods: dataprovider.ValidSSHLoginMethods,
|
2020-08-17 10:49:20 +00:00
|
|
|
ValidProtocols: dataprovider.ValidProtocols,
|
2020-02-19 21:39:30 +00:00
|
|
|
RootDirPerms: user.GetPermissionsForPath("/"),
|
2020-11-22 20:53:04 +00:00
|
|
|
RedactedSecret: redactedSecret,
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
|
|
|
renderTemplate(w, templateUser, data)
|
|
|
|
}
|
|
|
|
|
2021-02-01 18:04:15 +00:00
|
|
|
func renderFolderPage(w http.ResponseWriter, r *http.Request, folder vfs.BaseVirtualFolder, mode folderPageMode, error string) {
|
|
|
|
var title, currentURL string
|
|
|
|
switch mode {
|
|
|
|
case folderPageModeAdd:
|
|
|
|
title = "Add a new folder"
|
|
|
|
currentURL = webFolderPath
|
|
|
|
case folderPageModeUpdate:
|
|
|
|
title = "Update folder"
|
|
|
|
currentURL = fmt.Sprintf("%v/%v", webFolderPath, url.PathEscape(folder.Name))
|
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
data := folderPage{
|
2021-02-01 18:04:15 +00:00
|
|
|
basePage: getBasePageData(title, currentURL, r),
|
2020-06-07 21:30:18 +00:00
|
|
|
Error: error,
|
|
|
|
Folder: folder,
|
2021-02-01 18:04:15 +00:00
|
|
|
Mode: mode,
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
renderTemplate(w, templateFolder, data)
|
|
|
|
}
|
|
|
|
|
2021-01-25 20:31:33 +00:00
|
|
|
func getUsersForTemplate(r *http.Request) []userTemplateFields {
|
|
|
|
var res []userTemplateFields
|
|
|
|
formValue := r.Form.Get("users")
|
2021-02-01 18:04:15 +00:00
|
|
|
users := make(map[string]bool)
|
2021-01-25 20:31:33 +00:00
|
|
|
for _, cleaned := range getSliceFromDelimitedValues(formValue, "\n") {
|
|
|
|
if strings.Contains(cleaned, "::") {
|
|
|
|
mapping := strings.Split(cleaned, "::")
|
|
|
|
if len(mapping) > 1 {
|
|
|
|
username := strings.TrimSpace(mapping[0])
|
|
|
|
password := strings.TrimSpace(mapping[1])
|
|
|
|
var publicKey string
|
|
|
|
if len(mapping) > 2 {
|
|
|
|
publicKey = strings.TrimSpace(mapping[2])
|
|
|
|
}
|
|
|
|
if username == "" || (password == "" && publicKey == "") {
|
|
|
|
continue
|
|
|
|
}
|
2021-02-01 18:04:15 +00:00
|
|
|
if _, ok := users[username]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
users[username] = true
|
2021-01-25 20:31:33 +00:00
|
|
|
res = append(res, userTemplateFields{
|
|
|
|
Username: username,
|
|
|
|
Password: password,
|
|
|
|
PublicKey: publicKey,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2020-02-23 10:30:26 +00:00
|
|
|
func getVirtualFoldersFromPostFields(r *http.Request) []vfs.VirtualFolder {
|
|
|
|
var virtualFolders []vfs.VirtualFolder
|
|
|
|
formValue := r.Form.Get("virtual_folders")
|
|
|
|
for _, cleaned := range getSliceFromDelimitedValues(formValue, "\n") {
|
|
|
|
if strings.Contains(cleaned, "::") {
|
|
|
|
mapping := strings.Split(cleaned, "::")
|
|
|
|
if len(mapping) > 1 {
|
2020-05-01 13:27:53 +00:00
|
|
|
vfolder := vfs.VirtualFolder{
|
2020-06-07 21:30:18 +00:00
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
2021-02-01 18:04:15 +00:00
|
|
|
Name: strings.TrimSpace(mapping[1]),
|
2020-06-07 21:30:18 +00:00
|
|
|
},
|
2020-02-23 10:30:26 +00:00
|
|
|
VirtualPath: strings.TrimSpace(mapping[0]),
|
2020-06-07 21:30:18 +00:00
|
|
|
QuotaFiles: -1,
|
|
|
|
QuotaSize: -1,
|
2020-05-01 13:27:53 +00:00
|
|
|
}
|
|
|
|
if len(mapping) > 2 {
|
2020-06-07 21:30:18 +00:00
|
|
|
quotaFiles, err := strconv.Atoi(strings.TrimSpace(mapping[2]))
|
2020-05-01 13:27:53 +00:00
|
|
|
if err == nil {
|
2020-06-07 21:30:18 +00:00
|
|
|
vfolder.QuotaFiles = quotaFiles
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(mapping) > 3 {
|
|
|
|
quotaSize, err := strconv.ParseInt(strings.TrimSpace(mapping[3]), 10, 64)
|
|
|
|
if err == nil {
|
|
|
|
vfolder.QuotaSize = quotaSize
|
2020-05-01 13:27:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
virtualFolders = append(virtualFolders, vfolder)
|
2020-02-23 10:30:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return virtualFolders
|
|
|
|
}
|
|
|
|
|
2019-12-25 17:20:19 +00:00
|
|
|
func getUserPermissionsFromPostFields(r *http.Request) map[string][]string {
|
|
|
|
permissions := make(map[string][]string)
|
|
|
|
permissions["/"] = r.Form["permissions"]
|
|
|
|
subDirsPermsValue := r.Form.Get("sub_dirs_permissions")
|
2019-12-30 17:37:50 +00:00
|
|
|
for _, cleaned := range getSliceFromDelimitedValues(subDirsPermsValue, "\n") {
|
2020-02-23 10:30:26 +00:00
|
|
|
if strings.Contains(cleaned, "::") {
|
|
|
|
dirPerms := strings.Split(cleaned, "::")
|
2019-12-25 17:20:19 +00:00
|
|
|
if len(dirPerms) > 1 {
|
|
|
|
dir := dirPerms[0]
|
2019-12-30 17:37:50 +00:00
|
|
|
dir = strings.TrimSpace(dir)
|
2019-12-25 17:20:19 +00:00
|
|
|
perms := []string{}
|
|
|
|
for _, p := range strings.Split(dirPerms[1], ",") {
|
|
|
|
cleanedPerm := strings.TrimSpace(p)
|
|
|
|
if len(cleanedPerm) > 0 {
|
|
|
|
perms = append(perms, cleanedPerm)
|
|
|
|
}
|
|
|
|
}
|
2021-01-25 20:31:33 +00:00
|
|
|
if dir != "" {
|
2019-12-25 17:20:19 +00:00
|
|
|
permissions[dir] = perms
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return permissions
|
|
|
|
}
|
|
|
|
|
2019-12-30 17:37:50 +00:00
|
|
|
func getSliceFromDelimitedValues(values, delimiter string) []string {
|
|
|
|
result := []string{}
|
|
|
|
for _, v := range strings.Split(values, delimiter) {
|
|
|
|
cleaned := strings.TrimSpace(v)
|
2021-01-25 20:31:33 +00:00
|
|
|
if cleaned != "" {
|
2019-12-30 17:37:50 +00:00
|
|
|
result = append(result, cleaned)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-11-15 21:04:48 +00:00
|
|
|
func getListFromPostFields(value string) map[string][]string {
|
|
|
|
result := make(map[string][]string)
|
2020-03-01 21:10:29 +00:00
|
|
|
for _, cleaned := range getSliceFromDelimitedValues(value, "\n") {
|
|
|
|
if strings.Contains(cleaned, "::") {
|
|
|
|
dirExts := strings.Split(cleaned, "::")
|
|
|
|
if len(dirExts) > 1 {
|
|
|
|
dir := dirExts[0]
|
2020-11-16 18:21:50 +00:00
|
|
|
dir = path.Clean(strings.TrimSpace(dir))
|
2020-03-01 21:10:29 +00:00
|
|
|
exts := []string{}
|
|
|
|
for _, e := range strings.Split(dirExts[1], ",") {
|
|
|
|
cleanedExt := strings.TrimSpace(e)
|
2020-11-15 21:04:48 +00:00
|
|
|
if cleanedExt != "" {
|
2020-03-01 21:10:29 +00:00
|
|
|
exts = append(exts, cleanedExt)
|
|
|
|
}
|
|
|
|
}
|
2020-11-15 21:04:48 +00:00
|
|
|
if dir != "" {
|
|
|
|
if _, ok := result[dir]; ok {
|
|
|
|
result[dir] = append(result[dir], exts...)
|
2020-03-01 21:10:29 +00:00
|
|
|
} else {
|
2020-11-15 21:04:48 +00:00
|
|
|
result[dir] = exts
|
2020-03-01 21:10:29 +00:00
|
|
|
}
|
2020-11-16 18:21:50 +00:00
|
|
|
result[dir] = utils.RemoveDuplicates(result[dir])
|
2020-03-01 21:10:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-11-16 18:21:50 +00:00
|
|
|
func getFilePatternsFromPostField(valueAllowed, valuesDenied string) []dataprovider.PatternsFilter {
|
2020-11-15 21:04:48 +00:00
|
|
|
var result []dataprovider.PatternsFilter
|
2020-11-16 18:21:50 +00:00
|
|
|
allowedPatterns := getListFromPostFields(valueAllowed)
|
|
|
|
deniedPatterns := getListFromPostFields(valuesDenied)
|
|
|
|
|
|
|
|
for dirAllowed, allowPatterns := range allowedPatterns {
|
2020-11-15 21:04:48 +00:00
|
|
|
filter := dataprovider.PatternsFilter{
|
2020-11-16 18:21:50 +00:00
|
|
|
Path: dirAllowed,
|
|
|
|
AllowedPatterns: allowPatterns,
|
2020-11-15 21:04:48 +00:00
|
|
|
}
|
2020-11-16 18:21:50 +00:00
|
|
|
for dirDenied, denPatterns := range deniedPatterns {
|
|
|
|
if dirAllowed == dirDenied {
|
|
|
|
filter.DeniedPatterns = denPatterns
|
|
|
|
break
|
|
|
|
}
|
2020-11-15 21:04:48 +00:00
|
|
|
}
|
|
|
|
result = append(result, filter)
|
|
|
|
}
|
2020-11-16 18:21:50 +00:00
|
|
|
for dirDenied, denPatterns := range deniedPatterns {
|
|
|
|
found := false
|
|
|
|
for _, res := range result {
|
|
|
|
if res.Path == dirDenied {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
result = append(result, dataprovider.PatternsFilter{
|
|
|
|
Path: dirDenied,
|
|
|
|
DeniedPatterns: denPatterns,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-11-15 21:04:48 +00:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-11-16 18:21:50 +00:00
|
|
|
func getFileExtensionsFromPostField(valueAllowed, valuesDenied string) []dataprovider.ExtensionsFilter {
|
2020-11-15 21:04:48 +00:00
|
|
|
var result []dataprovider.ExtensionsFilter
|
2020-11-16 18:21:50 +00:00
|
|
|
allowedExtensions := getListFromPostFields(valueAllowed)
|
|
|
|
deniedExtensions := getListFromPostFields(valuesDenied)
|
|
|
|
|
|
|
|
for dirAllowed, allowedExts := range allowedExtensions {
|
2020-11-15 21:04:48 +00:00
|
|
|
filter := dataprovider.ExtensionsFilter{
|
2020-11-16 18:21:50 +00:00
|
|
|
Path: dirAllowed,
|
|
|
|
AllowedExtensions: allowedExts,
|
2020-11-15 21:04:48 +00:00
|
|
|
}
|
2020-11-16 18:21:50 +00:00
|
|
|
for dirDenied, deniedExts := range deniedExtensions {
|
|
|
|
if dirAllowed == dirDenied {
|
|
|
|
filter.DeniedExtensions = deniedExts
|
|
|
|
break
|
|
|
|
}
|
2020-11-15 21:04:48 +00:00
|
|
|
}
|
|
|
|
result = append(result, filter)
|
|
|
|
}
|
2020-11-16 18:21:50 +00:00
|
|
|
for dirDenied, deniedExts := range deniedExtensions {
|
|
|
|
found := false
|
|
|
|
for _, res := range result {
|
|
|
|
if res.Path == dirDenied {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
result = append(result, dataprovider.ExtensionsFilter{
|
|
|
|
Path: dirDenied,
|
|
|
|
DeniedExtensions: deniedExts,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-11-15 21:04:48 +00:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2019-12-30 17:37:50 +00:00
|
|
|
func getFiltersFromUserPostFields(r *http.Request) dataprovider.UserFilters {
|
|
|
|
var filters dataprovider.UserFilters
|
|
|
|
filters.AllowedIP = getSliceFromDelimitedValues(r.Form.Get("allowed_ip"), ",")
|
|
|
|
filters.DeniedIP = getSliceFromDelimitedValues(r.Form.Get("denied_ip"), ",")
|
2020-02-19 21:39:30 +00:00
|
|
|
filters.DeniedLoginMethods = r.Form["ssh_login_methods"]
|
2020-08-17 10:49:20 +00:00
|
|
|
filters.DeniedProtocols = r.Form["denied_protocols"]
|
2020-11-16 18:21:50 +00:00
|
|
|
filters.FileExtensions = getFileExtensionsFromPostField(r.Form.Get("allowed_extensions"), r.Form.Get("denied_extensions"))
|
|
|
|
filters.FilePatterns = getFilePatternsFromPostField(r.Form.Get("allowed_patterns"), r.Form.Get("denied_patterns"))
|
2019-12-30 17:37:50 +00:00
|
|
|
return filters
|
|
|
|
}
|
|
|
|
|
2020-11-30 20:46:34 +00:00
|
|
|
func getSecretFromFormField(r *http.Request, field string) *kms.Secret {
|
|
|
|
secret := kms.NewPlainSecret(r.Form.Get(field))
|
|
|
|
if strings.TrimSpace(secret.GetPayload()) == redactedSecret {
|
|
|
|
secret.SetStatus(kms.SecretStatusRedacted)
|
2020-11-22 20:53:04 +00:00
|
|
|
}
|
2020-11-30 20:46:34 +00:00
|
|
|
if strings.TrimSpace(secret.GetPayload()) == "" {
|
|
|
|
secret.SetStatus("")
|
2020-11-22 20:53:04 +00:00
|
|
|
}
|
|
|
|
return secret
|
|
|
|
}
|
|
|
|
|
2020-12-05 12:48:13 +00:00
|
|
|
func getS3Config(r *http.Request) (vfs.S3FsConfig, error) {
|
|
|
|
var err error
|
|
|
|
config := vfs.S3FsConfig{}
|
|
|
|
config.Bucket = r.Form.Get("s3_bucket")
|
|
|
|
config.Region = r.Form.Get("s3_region")
|
|
|
|
config.AccessKey = r.Form.Get("s3_access_key")
|
|
|
|
config.AccessSecret = getSecretFromFormField(r, "s3_access_secret")
|
|
|
|
config.Endpoint = r.Form.Get("s3_endpoint")
|
|
|
|
config.StorageClass = r.Form.Get("s3_storage_class")
|
|
|
|
config.KeyPrefix = r.Form.Get("s3_key_prefix")
|
|
|
|
config.UploadPartSize, err = strconv.ParseInt(r.Form.Get("s3_upload_part_size"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return config, err
|
|
|
|
}
|
|
|
|
config.UploadConcurrency, err = strconv.Atoi(r.Form.Get("s3_upload_concurrency"))
|
|
|
|
return config, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func getGCSConfig(r *http.Request) (vfs.GCSFsConfig, error) {
|
|
|
|
var err error
|
|
|
|
config := vfs.GCSFsConfig{}
|
|
|
|
|
|
|
|
config.Bucket = r.Form.Get("gcs_bucket")
|
|
|
|
config.StorageClass = r.Form.Get("gcs_storage_class")
|
|
|
|
config.KeyPrefix = r.Form.Get("gcs_key_prefix")
|
|
|
|
autoCredentials := r.Form.Get("gcs_auto_credentials")
|
|
|
|
if autoCredentials != "" {
|
|
|
|
config.AutomaticCredentials = 1
|
|
|
|
} else {
|
|
|
|
config.AutomaticCredentials = 0
|
|
|
|
}
|
|
|
|
credentials, _, err := r.FormFile("gcs_credential_file")
|
|
|
|
if err == http.ErrMissingFile {
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return config, err
|
|
|
|
}
|
|
|
|
defer credentials.Close()
|
|
|
|
fileBytes, err := ioutil.ReadAll(credentials)
|
|
|
|
if err != nil || len(fileBytes) == 0 {
|
|
|
|
if len(fileBytes) == 0 {
|
|
|
|
err = errors.New("credentials file size must be greater than 0")
|
|
|
|
}
|
|
|
|
return config, err
|
|
|
|
}
|
|
|
|
config.Credentials = kms.NewPlainSecret(string(fileBytes))
|
|
|
|
config.AutomaticCredentials = 0
|
|
|
|
return config, err
|
|
|
|
}
|
|
|
|
|
2020-12-12 09:31:09 +00:00
|
|
|
func getSFTPConfig(r *http.Request) vfs.SFTPFsConfig {
|
|
|
|
config := vfs.SFTPFsConfig{}
|
|
|
|
config.Endpoint = r.Form.Get("sftp_endpoint")
|
|
|
|
config.Username = r.Form.Get("sftp_username")
|
|
|
|
config.Password = getSecretFromFormField(r, "sftp_password")
|
|
|
|
config.PrivateKey = getSecretFromFormField(r, "sftp_private_key")
|
|
|
|
fingerprintsFormValue := r.Form.Get("sftp_fingerprints")
|
|
|
|
config.Fingerprints = getSliceFromDelimitedValues(fingerprintsFormValue, "\n")
|
|
|
|
config.Prefix = r.Form.Get("sftp_prefix")
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
2020-12-05 12:48:13 +00:00
|
|
|
func getAzureConfig(r *http.Request) (vfs.AzBlobFsConfig, error) {
|
|
|
|
var err error
|
|
|
|
config := vfs.AzBlobFsConfig{}
|
|
|
|
config.Container = r.Form.Get("az_container")
|
|
|
|
config.AccountName = r.Form.Get("az_account_name")
|
|
|
|
config.AccountKey = getSecretFromFormField(r, "az_account_key")
|
|
|
|
config.SASURL = r.Form.Get("az_sas_url")
|
|
|
|
config.Endpoint = r.Form.Get("az_endpoint")
|
|
|
|
config.KeyPrefix = r.Form.Get("az_key_prefix")
|
|
|
|
config.AccessTier = r.Form.Get("az_access_tier")
|
|
|
|
config.UseEmulator = len(r.Form.Get("az_use_emulator")) > 0
|
|
|
|
config.UploadPartSize, err = strconv.ParseInt(r.Form.Get("az_upload_part_size"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return config, err
|
|
|
|
}
|
|
|
|
config.UploadConcurrency, err = strconv.Atoi(r.Form.Get("az_upload_concurrency"))
|
|
|
|
return config, err
|
|
|
|
}
|
|
|
|
|
2020-01-31 18:04:00 +00:00
|
|
|
func getFsConfigFromUserPostFields(r *http.Request) (dataprovider.Filesystem, error) {
|
2020-01-19 06:41:05 +00:00
|
|
|
var fs dataprovider.Filesystem
|
|
|
|
provider, err := strconv.Atoi(r.Form.Get("fs_provider"))
|
|
|
|
if err != nil {
|
2020-10-05 18:58:41 +00:00
|
|
|
provider = int(dataprovider.LocalFilesystemProvider)
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
2020-10-05 18:58:41 +00:00
|
|
|
fs.Provider = dataprovider.FilesystemProvider(provider)
|
2020-12-12 09:31:09 +00:00
|
|
|
switch fs.Provider {
|
|
|
|
case dataprovider.S3FilesystemProvider:
|
2020-12-05 12:48:13 +00:00
|
|
|
config, err := getS3Config(r)
|
2020-03-13 18:13:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return fs, err
|
|
|
|
}
|
2020-12-05 12:48:13 +00:00
|
|
|
fs.S3Config = config
|
2020-12-12 09:31:09 +00:00
|
|
|
case dataprovider.AzureBlobFilesystemProvider:
|
|
|
|
config, err := getAzureConfig(r)
|
2020-01-31 18:04:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return fs, err
|
|
|
|
}
|
2020-12-12 09:31:09 +00:00
|
|
|
fs.AzBlobConfig = config
|
|
|
|
case dataprovider.GCSFilesystemProvider:
|
|
|
|
config, err := getGCSConfig(r)
|
2020-10-25 07:18:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return fs, err
|
|
|
|
}
|
2020-12-12 09:31:09 +00:00
|
|
|
fs.GCSConfig = config
|
|
|
|
case dataprovider.CryptedFilesystemProvider:
|
2020-12-05 12:48:13 +00:00
|
|
|
fs.CryptConfig.Passphrase = getSecretFromFormField(r, "crypt_passphrase")
|
2020-12-12 09:31:09 +00:00
|
|
|
case dataprovider.SFTPFilesystemProvider:
|
|
|
|
fs.SFTPConfig = getSFTPConfig(r)
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
2020-01-31 18:04:00 +00:00
|
|
|
return fs, nil
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func getAdminFromPostFields(r *http.Request) (dataprovider.Admin, error) {
|
|
|
|
var admin dataprovider.Admin
|
|
|
|
err := r.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
return admin, err
|
|
|
|
}
|
|
|
|
status, err := strconv.Atoi(r.Form.Get("status"))
|
|
|
|
if err != nil {
|
|
|
|
return admin, err
|
|
|
|
}
|
|
|
|
admin.Username = r.Form.Get("username")
|
|
|
|
admin.Password = r.Form.Get("password")
|
|
|
|
admin.Permissions = r.Form["permissions"]
|
|
|
|
admin.Email = r.Form.Get("email")
|
|
|
|
admin.Status = status
|
|
|
|
admin.Filters.AllowList = getSliceFromDelimitedValues(r.Form.Get("allowed_ip"), ",")
|
|
|
|
admin.AdditionalInfo = r.Form.Get("additional_info")
|
|
|
|
return admin, nil
|
|
|
|
}
|
|
|
|
|
2021-01-25 20:31:33 +00:00
|
|
|
func replacePlaceholders(field string, replacements map[string]string) string {
|
|
|
|
for k, v := range replacements {
|
|
|
|
field = strings.ReplaceAll(field, k, v)
|
|
|
|
}
|
|
|
|
return field
|
|
|
|
}
|
|
|
|
|
|
|
|
func getCryptFsFromTemplate(fsConfig vfs.CryptFsConfig, replacements map[string]string) vfs.CryptFsConfig {
|
|
|
|
if fsConfig.Passphrase != nil {
|
|
|
|
if fsConfig.Passphrase.IsPlain() {
|
|
|
|
payload := replacePlaceholders(fsConfig.Passphrase.GetPayload(), replacements)
|
|
|
|
fsConfig.Passphrase = kms.NewPlainSecret(payload)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fsConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func getS3FsFromTemplate(fsConfig vfs.S3FsConfig, replacements map[string]string) vfs.S3FsConfig {
|
|
|
|
fsConfig.KeyPrefix = replacePlaceholders(fsConfig.KeyPrefix, replacements)
|
|
|
|
fsConfig.AccessKey = replacePlaceholders(fsConfig.AccessKey, replacements)
|
|
|
|
if fsConfig.AccessSecret != nil && fsConfig.AccessSecret.IsPlain() {
|
|
|
|
payload := replacePlaceholders(fsConfig.AccessSecret.GetPayload(), replacements)
|
|
|
|
fsConfig.AccessSecret = kms.NewPlainSecret(payload)
|
|
|
|
}
|
|
|
|
return fsConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func getGCSFsFromTemplate(fsConfig vfs.GCSFsConfig, replacements map[string]string) vfs.GCSFsConfig {
|
|
|
|
fsConfig.KeyPrefix = replacePlaceholders(fsConfig.KeyPrefix, replacements)
|
|
|
|
return fsConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAzBlobFsFromTemplate(fsConfig vfs.AzBlobFsConfig, replacements map[string]string) vfs.AzBlobFsConfig {
|
|
|
|
fsConfig.KeyPrefix = replacePlaceholders(fsConfig.KeyPrefix, replacements)
|
|
|
|
fsConfig.AccountName = replacePlaceholders(fsConfig.AccountName, replacements)
|
|
|
|
if fsConfig.AccountKey != nil && fsConfig.AccountKey.IsPlain() {
|
|
|
|
payload := replacePlaceholders(fsConfig.AccountKey.GetPayload(), replacements)
|
|
|
|
fsConfig.AccountKey = kms.NewPlainSecret(payload)
|
|
|
|
}
|
|
|
|
return fsConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSFTPFsFromTemplate(fsConfig vfs.SFTPFsConfig, replacements map[string]string) vfs.SFTPFsConfig {
|
|
|
|
fsConfig.Prefix = replacePlaceholders(fsConfig.Prefix, replacements)
|
|
|
|
fsConfig.Username = replacePlaceholders(fsConfig.Username, replacements)
|
|
|
|
if fsConfig.Password != nil && fsConfig.Password.IsPlain() {
|
|
|
|
payload := replacePlaceholders(fsConfig.Password.GetPayload(), replacements)
|
|
|
|
fsConfig.Password = kms.NewPlainSecret(payload)
|
|
|
|
}
|
|
|
|
return fsConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func getUserFromTemplate(user dataprovider.User, template userTemplateFields) dataprovider.User {
|
|
|
|
user.Username = template.Username
|
|
|
|
user.Password = template.Password
|
|
|
|
user.PublicKeys = nil
|
|
|
|
if template.PublicKey != "" {
|
|
|
|
user.PublicKeys = append(user.PublicKeys, template.PublicKey)
|
|
|
|
}
|
|
|
|
replacements := make(map[string]string)
|
|
|
|
replacements["%username%"] = user.Username
|
|
|
|
user.Password = replacePlaceholders(user.Password, replacements)
|
|
|
|
replacements["%password%"] = user.Password
|
|
|
|
|
|
|
|
user.HomeDir = replacePlaceholders(user.HomeDir, replacements)
|
|
|
|
var vfolders []vfs.VirtualFolder
|
|
|
|
for _, vfolder := range user.VirtualFolders {
|
2021-02-01 18:04:15 +00:00
|
|
|
vfolder.Name = replacePlaceholders(vfolder.Name, replacements)
|
|
|
|
vfolder.VirtualPath = replacePlaceholders(vfolder.VirtualPath, replacements)
|
2021-01-25 20:31:33 +00:00
|
|
|
vfolders = append(vfolders, vfolder)
|
|
|
|
}
|
|
|
|
user.VirtualFolders = vfolders
|
|
|
|
user.AdditionalInfo = replacePlaceholders(user.AdditionalInfo, replacements)
|
|
|
|
|
|
|
|
switch user.FsConfig.Provider {
|
|
|
|
case dataprovider.CryptedFilesystemProvider:
|
|
|
|
user.FsConfig.CryptConfig = getCryptFsFromTemplate(user.FsConfig.CryptConfig, replacements)
|
|
|
|
case dataprovider.S3FilesystemProvider:
|
|
|
|
user.FsConfig.S3Config = getS3FsFromTemplate(user.FsConfig.S3Config, replacements)
|
|
|
|
case dataprovider.GCSFilesystemProvider:
|
|
|
|
user.FsConfig.GCSConfig = getGCSFsFromTemplate(user.FsConfig.GCSConfig, replacements)
|
|
|
|
case dataprovider.AzureBlobFilesystemProvider:
|
|
|
|
user.FsConfig.AzBlobConfig = getAzBlobFsFromTemplate(user.FsConfig.AzBlobConfig, replacements)
|
|
|
|
case dataprovider.SFTPFilesystemProvider:
|
|
|
|
user.FsConfig.SFTPConfig = getSFTPFsFromTemplate(user.FsConfig.SFTPConfig, replacements)
|
|
|
|
}
|
|
|
|
|
|
|
|
return user
|
|
|
|
}
|
|
|
|
|
2019-10-07 16:19:01 +00:00
|
|
|
func getUserFromPostFields(r *http.Request) (dataprovider.User, error) {
|
|
|
|
var user dataprovider.User
|
2020-01-31 18:04:00 +00:00
|
|
|
err := r.ParseMultipartForm(maxRequestSize)
|
2019-10-07 16:19:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
publicKeysFormValue := r.Form.Get("public_keys")
|
2019-12-30 17:37:50 +00:00
|
|
|
publicKeys := getSliceFromDelimitedValues(publicKeysFormValue, "\n")
|
2019-10-07 16:19:01 +00:00
|
|
|
uid, err := strconv.Atoi(r.Form.Get("uid"))
|
|
|
|
if err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
gid, err := strconv.Atoi(r.Form.Get("gid"))
|
|
|
|
if err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
maxSessions, err := strconv.Atoi(r.Form.Get("max_sessions"))
|
|
|
|
if err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
quotaSize, err := strconv.ParseInt(r.Form.Get("quota_size"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
quotaFiles, err := strconv.Atoi(r.Form.Get("quota_files"))
|
|
|
|
if err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
bandwidthUL, err := strconv.ParseInt(r.Form.Get("upload_bandwidth"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
bandwidthDL, err := strconv.ParseInt(r.Form.Get("download_bandwidth"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
2019-11-13 10:36:21 +00:00
|
|
|
status, err := strconv.Atoi(r.Form.Get("status"))
|
|
|
|
if err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
expirationDateMillis := int64(0)
|
|
|
|
expirationDateString := r.Form.Get("expiration_date")
|
|
|
|
if len(strings.TrimSpace(expirationDateString)) > 0 {
|
|
|
|
expirationDate, err := time.Parse(webDateTimeFormat, expirationDateString)
|
|
|
|
if err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
expirationDateMillis = utils.GetTimeAsMsSinceEpoch(expirationDate)
|
|
|
|
}
|
2020-01-31 18:04:00 +00:00
|
|
|
fsConfig, err := getFsConfigFromUserPostFields(r)
|
|
|
|
if err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
user = dataprovider.User{
|
|
|
|
Username: r.Form.Get("username"),
|
|
|
|
Password: r.Form.Get("password"),
|
|
|
|
PublicKeys: publicKeys,
|
|
|
|
HomeDir: r.Form.Get("home_dir"),
|
2020-02-23 10:30:26 +00:00
|
|
|
VirtualFolders: getVirtualFoldersFromPostFields(r),
|
2019-10-07 16:19:01 +00:00
|
|
|
UID: uid,
|
|
|
|
GID: gid,
|
2019-12-25 17:20:19 +00:00
|
|
|
Permissions: getUserPermissionsFromPostFields(r),
|
2019-10-07 16:19:01 +00:00
|
|
|
MaxSessions: maxSessions,
|
|
|
|
QuotaSize: quotaSize,
|
|
|
|
QuotaFiles: quotaFiles,
|
|
|
|
UploadBandwidth: bandwidthUL,
|
|
|
|
DownloadBandwidth: bandwidthDL,
|
2019-11-13 10:36:21 +00:00
|
|
|
Status: status,
|
|
|
|
ExpirationDate: expirationDateMillis,
|
2019-12-30 17:37:50 +00:00
|
|
|
Filters: getFiltersFromUserPostFields(r),
|
2020-01-31 18:04:00 +00:00
|
|
|
FsConfig: fsConfig,
|
2020-11-25 21:26:34 +00:00
|
|
|
AdditionalInfo: r.Form.Get("additional_info"),
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
2020-08-16 18:17:02 +00:00
|
|
|
maxFileSize, err := strconv.ParseInt(r.Form.Get("max_upload_file_size"), 10, 64)
|
|
|
|
user.Filters.MaxUploadFileSize = maxFileSize
|
2019-10-07 16:19:01 +00:00
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func renderLoginPage(w http.ResponseWriter, error string) {
|
|
|
|
data := loginPage{
|
|
|
|
CurrentURL: webLoginPath,
|
|
|
|
Version: version.Get().Version,
|
|
|
|
Error: error,
|
2021-02-03 07:55:28 +00:00
|
|
|
CSRFToken: createCSRFToken(),
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
|
|
|
renderTemplate(w, templateLogin, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleWebAdminChangePwd(w http.ResponseWriter, r *http.Request) {
|
|
|
|
renderChangePwdPage(w, r, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleWebAdminChangePwdPost(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
err := r.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
renderChangePwdPage(w, r, err.Error())
|
|
|
|
return
|
|
|
|
}
|
2021-02-03 07:55:28 +00:00
|
|
|
if err := verifyCSRFToken(r.Form.Get(csrfFormToken)); err != nil {
|
|
|
|
renderForbiddenPage(w, r, err.Error())
|
|
|
|
return
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
err = doChangeAdminPassword(r, r.Form.Get("current_password"), r.Form.Get("new_password1"),
|
|
|
|
r.Form.Get("new_password2"))
|
|
|
|
if err != nil {
|
|
|
|
renderChangePwdPage(w, r, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
handleWebLogout(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleWebLogout(w http.ResponseWriter, r *http.Request) {
|
|
|
|
c := jwtTokenClaims{}
|
2021-01-26 21:35:36 +00:00
|
|
|
c.removeCookie(w, r)
|
2021-01-17 21:29:08 +00:00
|
|
|
|
|
|
|
http.Redirect(w, r, webLoginPath, http.StatusFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleWebLogin(w http.ResponseWriter, r *http.Request) {
|
|
|
|
renderLoginPage(w, "")
|
|
|
|
}
|
|
|
|
|
2021-01-22 18:42:18 +00:00
|
|
|
func handleWebMaintenance(w http.ResponseWriter, r *http.Request) {
|
|
|
|
renderMaintenancePage(w, r, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleWebRestore(w http.ResponseWriter, r *http.Request) {
|
|
|
|
err := r.ParseMultipartForm(MaxRestoreSize)
|
|
|
|
if err != nil {
|
|
|
|
renderMaintenancePage(w, r, err.Error())
|
|
|
|
return
|
|
|
|
}
|
2021-02-03 07:55:28 +00:00
|
|
|
if err := verifyCSRFToken(r.Form.Get(csrfFormToken)); err != nil {
|
|
|
|
renderForbiddenPage(w, r, err.Error())
|
|
|
|
return
|
|
|
|
}
|
2021-01-22 18:42:18 +00:00
|
|
|
restoreMode, err := strconv.Atoi(r.Form.Get("mode"))
|
|
|
|
if err != nil {
|
|
|
|
renderMaintenancePage(w, r, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
scanQuota, err := strconv.Atoi(r.Form.Get("quota"))
|
|
|
|
if err != nil {
|
|
|
|
renderMaintenancePage(w, r, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
backupFile, _, err := r.FormFile("backup_file")
|
|
|
|
if err != nil {
|
|
|
|
renderMaintenancePage(w, r, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer backupFile.Close()
|
|
|
|
|
|
|
|
backupContent, err := ioutil.ReadAll(backupFile)
|
|
|
|
if err != nil || len(backupContent) == 0 {
|
|
|
|
if len(backupContent) == 0 {
|
|
|
|
err = errors.New("backup file size must be greater than 0")
|
|
|
|
}
|
|
|
|
renderMaintenancePage(w, r, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := restoreBackup(backupContent, "", scanQuota, restoreMode); err != nil {
|
|
|
|
renderMaintenancePage(w, r, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
renderMessagePage(w, r, "Data restored", "", http.StatusOK, nil, "Your backup was successfully restored")
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func handleGetWebAdmins(w http.ResponseWriter, r *http.Request) {
|
|
|
|
limit := defaultQueryLimit
|
|
|
|
if _, ok := r.URL.Query()["qlimit"]; ok {
|
|
|
|
var err error
|
|
|
|
limit, err = strconv.Atoi(r.URL.Query().Get("qlimit"))
|
|
|
|
if err != nil {
|
|
|
|
limit = defaultQueryLimit
|
|
|
|
}
|
|
|
|
}
|
|
|
|
admins := make([]dataprovider.Admin, 0, limit)
|
|
|
|
for {
|
|
|
|
a, err := dataprovider.GetAdmins(limit, len(admins), dataprovider.OrderASC)
|
|
|
|
if err != nil {
|
|
|
|
renderInternalServerErrorPage(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
admins = append(admins, a...)
|
|
|
|
if len(a) < limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
data := adminsPage{
|
|
|
|
basePage: getBasePageData(pageAdminsTitle, webAdminsPath, r),
|
|
|
|
Admins: admins,
|
|
|
|
}
|
|
|
|
renderTemplate(w, templateAdmins, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleWebAddAdminGet(w http.ResponseWriter, r *http.Request) {
|
|
|
|
admin := &dataprovider.Admin{Status: 1}
|
|
|
|
renderAddUpdateAdminPage(w, r, admin, "", true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleWebUpdateAdminGet(w http.ResponseWriter, r *http.Request) {
|
|
|
|
username := getURLParam(r, "username")
|
|
|
|
admin, err := dataprovider.AdminExists(username)
|
|
|
|
if err == nil {
|
|
|
|
renderAddUpdateAdminPage(w, r, &admin, "", false)
|
|
|
|
} else if _, ok := err.(*dataprovider.RecordNotFoundError); ok {
|
|
|
|
renderNotFoundPage(w, r, err)
|
|
|
|
} else {
|
|
|
|
renderInternalServerErrorPage(w, r, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleWebAddAdminPost(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
admin, err := getAdminFromPostFields(r)
|
|
|
|
if err != nil {
|
|
|
|
renderAddUpdateAdminPage(w, r, &admin, err.Error(), true)
|
|
|
|
return
|
|
|
|
}
|
2021-02-03 07:55:28 +00:00
|
|
|
if err := verifyCSRFToken(r.Form.Get(csrfFormToken)); err != nil {
|
|
|
|
renderForbiddenPage(w, r, err.Error())
|
|
|
|
return
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
err = dataprovider.AddAdmin(&admin)
|
|
|
|
if err != nil {
|
|
|
|
renderAddUpdateAdminPage(w, r, &admin, err.Error(), true)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
http.Redirect(w, r, webAdminsPath, http.StatusSeeOther)
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleWebUpdateAdminPost(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
|
|
|
|
username := getURLParam(r, "username")
|
|
|
|
admin, err := dataprovider.AdminExists(username)
|
|
|
|
if _, ok := err.(*dataprovider.RecordNotFoundError); ok {
|
|
|
|
renderNotFoundPage(w, r, err)
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
|
|
|
renderInternalServerErrorPage(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
updatedAdmin, err := getAdminFromPostFields(r)
|
|
|
|
if err != nil {
|
|
|
|
renderAddUpdateAdminPage(w, r, &updatedAdmin, err.Error(), false)
|
|
|
|
return
|
|
|
|
}
|
2021-02-03 07:55:28 +00:00
|
|
|
if err := verifyCSRFToken(r.Form.Get(csrfFormToken)); err != nil {
|
|
|
|
renderForbiddenPage(w, r, err.Error())
|
|
|
|
return
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
updatedAdmin.ID = admin.ID
|
|
|
|
updatedAdmin.Username = admin.Username
|
|
|
|
if updatedAdmin.Password == "" {
|
|
|
|
updatedAdmin.Password = admin.Password
|
|
|
|
}
|
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
|
|
|
renderAddUpdateAdminPage(w, r, &updatedAdmin, fmt.Sprintf("Invalid token claims: %v", err), false)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if username == claims.Username {
|
|
|
|
if claims.isCriticalPermRemoved(updatedAdmin.Permissions) {
|
|
|
|
renderAddUpdateAdminPage(w, r, &updatedAdmin, "You cannot remove these permissions to yourself", false)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if updatedAdmin.Status == 0 {
|
|
|
|
renderAddUpdateAdminPage(w, r, &updatedAdmin, "You cannot disable yourself", false)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = dataprovider.UpdateAdmin(&updatedAdmin)
|
|
|
|
if err != nil {
|
|
|
|
renderAddUpdateAdminPage(w, r, &admin, err.Error(), false)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
http.Redirect(w, r, webAdminsPath, http.StatusSeeOther)
|
|
|
|
}
|
|
|
|
|
2019-10-07 16:19:01 +00:00
|
|
|
func handleGetWebUsers(w http.ResponseWriter, r *http.Request) {
|
2020-06-07 21:30:18 +00:00
|
|
|
limit := defaultQueryLimit
|
2019-10-07 16:19:01 +00:00
|
|
|
if _, ok := r.URL.Query()["qlimit"]; ok {
|
|
|
|
var err error
|
|
|
|
limit, err = strconv.Atoi(r.URL.Query().Get("qlimit"))
|
|
|
|
if err != nil {
|
2020-06-07 21:30:18 +00:00
|
|
|
limit = defaultQueryLimit
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
users := make([]dataprovider.User, 0, limit)
|
|
|
|
for {
|
2021-01-17 21:29:08 +00:00
|
|
|
u, err := dataprovider.GetUsers(limit, len(users), dataprovider.OrderASC)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
2021-01-17 21:29:08 +00:00
|
|
|
renderInternalServerErrorPage(w, r, err)
|
2020-06-07 21:30:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
users = append(users, u...)
|
|
|
|
if len(u) < limit {
|
2019-10-07 16:19:01 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
data := usersPage{
|
2021-01-17 21:29:08 +00:00
|
|
|
basePage: getBasePageData(pageUsersTitle, webUsersPath, r),
|
2019-10-07 16:19:01 +00:00
|
|
|
Users: users,
|
|
|
|
}
|
|
|
|
renderTemplate(w, templateUsers, data)
|
|
|
|
}
|
|
|
|
|
2021-01-25 20:31:33 +00:00
|
|
|
func handleWebTemplateUserGet(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.URL.Query().Get("from") != "" {
|
|
|
|
username := r.URL.Query().Get("from")
|
|
|
|
user, err := dataprovider.UserExists(username)
|
|
|
|
if err == nil {
|
|
|
|
user.SetEmptySecrets()
|
|
|
|
renderUserPage(w, r, &user, userPageModeTemplate, "")
|
|
|
|
} else if _, ok := err.(*dataprovider.RecordNotFoundError); ok {
|
|
|
|
renderNotFoundPage(w, r, err)
|
|
|
|
} else {
|
|
|
|
renderInternalServerErrorPage(w, r, err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
user := dataprovider.User{Status: 1}
|
|
|
|
renderUserPage(w, r, &user, userPageModeTemplate, "")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleWebTemplateUserPost(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
templateUser, err := getUserFromPostFields(r)
|
|
|
|
if err != nil {
|
|
|
|
renderMessagePage(w, r, "Error parsing user fields", "", http.StatusBadRequest, err, "")
|
|
|
|
return
|
|
|
|
}
|
2021-02-03 07:55:28 +00:00
|
|
|
if err := verifyCSRFToken(r.Form.Get(csrfFormToken)); err != nil {
|
|
|
|
renderForbiddenPage(w, r, err.Error())
|
|
|
|
return
|
|
|
|
}
|
2021-01-25 20:31:33 +00:00
|
|
|
|
|
|
|
var dump dataprovider.BackupData
|
|
|
|
dump.Version = dataprovider.DumpVersion
|
|
|
|
|
|
|
|
userTmplFields := getUsersForTemplate(r)
|
|
|
|
for _, tmpl := range userTmplFields {
|
|
|
|
u := getUserFromTemplate(templateUser, tmpl)
|
|
|
|
if err := dataprovider.ValidateUser(&u); err != nil {
|
|
|
|
renderMessagePage(w, r, fmt.Sprintf("Error validating user %#v", u.Username), "", http.StatusBadRequest, err, "")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dump.Users = append(dump.Users, u)
|
2021-02-01 18:04:15 +00:00
|
|
|
for _, folder := range u.VirtualFolders {
|
|
|
|
if !dump.HasFolder(folder.Name) {
|
|
|
|
dump.Folders = append(dump.Folders, folder.BaseVirtualFolder)
|
|
|
|
}
|
|
|
|
}
|
2021-01-25 20:31:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(dump.Users) == 0 {
|
|
|
|
renderMessagePage(w, r, "No users to export", "No valid users found, export is not possible", http.StatusBadRequest, nil, "")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"sftpgo-%v-users-from-template.json\"", len(dump.Users)))
|
|
|
|
render.JSON(w, r, dump)
|
|
|
|
}
|
|
|
|
|
2019-10-07 16:19:01 +00:00
|
|
|
func handleWebAddUserGet(w http.ResponseWriter, r *http.Request) {
|
2021-01-25 20:31:33 +00:00
|
|
|
if r.URL.Query().Get("clone-from") != "" {
|
|
|
|
username := r.URL.Query().Get("clone-from")
|
2021-01-17 21:29:08 +00:00
|
|
|
user, err := dataprovider.UserExists(username)
|
2020-12-26 13:58:59 +00:00
|
|
|
if err == nil {
|
|
|
|
user.ID = 0
|
|
|
|
user.Username = ""
|
2021-01-25 20:31:33 +00:00
|
|
|
user.SetEmptySecrets()
|
|
|
|
renderUserPage(w, r, &user, userPageModeAdd, "")
|
2020-12-26 13:58:59 +00:00
|
|
|
} else if _, ok := err.(*dataprovider.RecordNotFoundError); ok {
|
2021-01-17 21:29:08 +00:00
|
|
|
renderNotFoundPage(w, r, err)
|
2020-12-26 13:58:59 +00:00
|
|
|
} else {
|
2021-01-17 21:29:08 +00:00
|
|
|
renderInternalServerErrorPage(w, r, err)
|
2020-12-26 13:58:59 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
user := dataprovider.User{Status: 1}
|
2021-01-25 20:31:33 +00:00
|
|
|
renderUserPage(w, r, &user, userPageModeAdd, "")
|
2020-12-26 13:58:59 +00:00
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 17:24:38 +00:00
|
|
|
func handleWebUpdateUserGet(w http.ResponseWriter, r *http.Request) {
|
2021-01-17 21:29:08 +00:00
|
|
|
username := getURLParam(r, "username")
|
|
|
|
user, err := dataprovider.UserExists(username)
|
2019-10-07 16:19:01 +00:00
|
|
|
if err == nil {
|
2021-01-25 20:31:33 +00:00
|
|
|
renderUserPage(w, r, &user, userPageModeUpdate, "")
|
2019-10-07 16:19:01 +00:00
|
|
|
} else if _, ok := err.(*dataprovider.RecordNotFoundError); ok {
|
2021-01-17 21:29:08 +00:00
|
|
|
renderNotFoundPage(w, r, err)
|
2019-10-07 16:19:01 +00:00
|
|
|
} else {
|
2021-01-17 21:29:08 +00:00
|
|
|
renderInternalServerErrorPage(w, r, err)
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleWebAddUserPost(w http.ResponseWriter, r *http.Request) {
|
2020-01-31 18:04:00 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2019-10-07 16:19:01 +00:00
|
|
|
user, err := getUserFromPostFields(r)
|
|
|
|
if err != nil {
|
2021-01-25 20:31:33 +00:00
|
|
|
renderUserPage(w, r, &user, userPageModeAdd, err.Error())
|
2019-10-07 16:19:01 +00:00
|
|
|
return
|
|
|
|
}
|
2021-02-03 07:55:28 +00:00
|
|
|
if err := verifyCSRFToken(r.Form.Get(csrfFormToken)); err != nil {
|
|
|
|
renderForbiddenPage(w, r, err.Error())
|
|
|
|
return
|
|
|
|
}
|
2021-01-05 08:50:22 +00:00
|
|
|
err = dataprovider.AddUser(&user)
|
2019-10-07 16:19:01 +00:00
|
|
|
if err == nil {
|
|
|
|
http.Redirect(w, r, webUsersPath, http.StatusSeeOther)
|
|
|
|
} else {
|
2021-01-25 20:31:33 +00:00
|
|
|
renderUserPage(w, r, &user, userPageModeAdd, err.Error())
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 17:24:38 +00:00
|
|
|
func handleWebUpdateUserPost(w http.ResponseWriter, r *http.Request) {
|
2020-01-31 18:04:00 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2021-01-17 21:29:08 +00:00
|
|
|
username := getURLParam(r, "username")
|
|
|
|
user, err := dataprovider.UserExists(username)
|
2019-10-07 16:19:01 +00:00
|
|
|
if _, ok := err.(*dataprovider.RecordNotFoundError); ok {
|
2021-01-17 21:29:08 +00:00
|
|
|
renderNotFoundPage(w, r, err)
|
2019-10-07 16:19:01 +00:00
|
|
|
return
|
|
|
|
} else if err != nil {
|
2021-01-17 21:29:08 +00:00
|
|
|
renderInternalServerErrorPage(w, r, err)
|
2019-10-07 16:19:01 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
updatedUser, err := getUserFromPostFields(r)
|
|
|
|
if err != nil {
|
2021-01-25 20:31:33 +00:00
|
|
|
renderUserPage(w, r, &user, userPageModeUpdate, err.Error())
|
2019-10-07 16:19:01 +00:00
|
|
|
return
|
|
|
|
}
|
2021-02-03 07:55:28 +00:00
|
|
|
if err := verifyCSRFToken(r.Form.Get(csrfFormToken)); err != nil {
|
|
|
|
renderForbiddenPage(w, r, err.Error())
|
|
|
|
return
|
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
updatedUser.ID = user.ID
|
2021-01-17 21:29:08 +00:00
|
|
|
updatedUser.Username = user.Username
|
2020-11-30 20:46:34 +00:00
|
|
|
updatedUser.SetEmptySecretsIfNil()
|
2021-01-17 21:29:08 +00:00
|
|
|
if updatedUser.Password == "" {
|
2019-10-07 16:19:01 +00:00
|
|
|
updatedUser.Password = user.Password
|
|
|
|
}
|
2020-12-12 09:31:09 +00:00
|
|
|
updateEncryptedSecrets(&updatedUser, user.FsConfig.S3Config.AccessSecret, user.FsConfig.AzBlobConfig.AccountKey,
|
|
|
|
user.FsConfig.GCSConfig.Credentials, user.FsConfig.CryptConfig.Passphrase, user.FsConfig.SFTPConfig.Password,
|
|
|
|
user.FsConfig.SFTPConfig.PrivateKey)
|
|
|
|
|
2021-01-05 08:50:22 +00:00
|
|
|
err = dataprovider.UpdateUser(&updatedUser)
|
2019-10-07 16:19:01 +00:00
|
|
|
if err == nil {
|
2020-09-01 14:10:26 +00:00
|
|
|
if len(r.Form.Get("disconnect")) > 0 {
|
|
|
|
disconnectUser(user.Username)
|
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
http.Redirect(w, r, webUsersPath, http.StatusSeeOther)
|
|
|
|
} else {
|
2021-01-25 20:31:33 +00:00
|
|
|
renderUserPage(w, r, &user, userPageModeUpdate, err.Error())
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-08 10:18:34 +00:00
|
|
|
func handleWebGetStatus(w http.ResponseWriter, r *http.Request) {
|
|
|
|
data := statusPage{
|
2021-01-17 21:29:08 +00:00
|
|
|
basePage: getBasePageData(pageStatusTitle, webStatusPath, r),
|
2020-12-08 10:18:34 +00:00
|
|
|
Status: getServicesStatus(),
|
|
|
|
}
|
|
|
|
renderTemplate(w, templateStatus, data)
|
|
|
|
}
|
|
|
|
|
2019-10-07 16:19:01 +00:00
|
|
|
func handleWebGetConnections(w http.ResponseWriter, r *http.Request) {
|
2020-07-24 21:39:38 +00:00
|
|
|
connectionStats := common.Connections.GetStats()
|
2019-10-07 16:19:01 +00:00
|
|
|
data := connectionsPage{
|
2021-01-17 21:29:08 +00:00
|
|
|
basePage: getBasePageData(pageConnectionsTitle, webConnectionsPath, r),
|
2019-10-07 16:19:01 +00:00
|
|
|
Connections: connectionStats,
|
|
|
|
}
|
|
|
|
renderTemplate(w, templateConnections, data)
|
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
|
|
|
|
func handleWebAddFolderGet(w http.ResponseWriter, r *http.Request) {
|
2021-02-01 18:04:15 +00:00
|
|
|
renderFolderPage(w, r, vfs.BaseVirtualFolder{}, folderPageModeAdd, "")
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleWebAddFolderPost(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
folder := vfs.BaseVirtualFolder{}
|
|
|
|
err := r.ParseForm()
|
|
|
|
if err != nil {
|
2021-02-01 18:04:15 +00:00
|
|
|
renderFolderPage(w, r, folder, folderPageModeAdd, err.Error())
|
2020-06-07 21:30:18 +00:00
|
|
|
return
|
|
|
|
}
|
2021-02-03 07:55:28 +00:00
|
|
|
if err := verifyCSRFToken(r.Form.Get(csrfFormToken)); err != nil {
|
|
|
|
renderForbiddenPage(w, r, err.Error())
|
|
|
|
return
|
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
folder.MappedPath = r.Form.Get("mapped_path")
|
2021-02-01 18:04:15 +00:00
|
|
|
folder.Name = r.Form.Get("name")
|
2020-06-07 21:30:18 +00:00
|
|
|
|
2021-01-05 08:50:22 +00:00
|
|
|
err = dataprovider.AddFolder(&folder)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err == nil {
|
|
|
|
http.Redirect(w, r, webFoldersPath, http.StatusSeeOther)
|
|
|
|
} else {
|
2021-02-01 18:04:15 +00:00
|
|
|
renderFolderPage(w, r, folder, folderPageModeAdd, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleWebUpdateFolderGet(w http.ResponseWriter, r *http.Request) {
|
|
|
|
name := getURLParam(r, "name")
|
|
|
|
folder, err := dataprovider.GetFolderByName(name)
|
|
|
|
if err == nil {
|
|
|
|
renderFolderPage(w, r, folder, folderPageModeUpdate, "")
|
|
|
|
} else if _, ok := err.(*dataprovider.RecordNotFoundError); ok {
|
|
|
|
renderNotFoundPage(w, r, err)
|
|
|
|
} else {
|
|
|
|
renderInternalServerErrorPage(w, r, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleWebUpdateFolderPost(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
name := getURLParam(r, "name")
|
|
|
|
folder, err := dataprovider.GetFolderByName(name)
|
|
|
|
if _, ok := err.(*dataprovider.RecordNotFoundError); ok {
|
|
|
|
renderNotFoundPage(w, r, err)
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
|
|
|
renderInternalServerErrorPage(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = r.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
renderFolderPage(w, r, folder, folderPageModeUpdate, err.Error())
|
|
|
|
return
|
|
|
|
}
|
2021-02-03 07:55:28 +00:00
|
|
|
if err := verifyCSRFToken(r.Form.Get(csrfFormToken)); err != nil {
|
|
|
|
renderForbiddenPage(w, r, err.Error())
|
|
|
|
return
|
|
|
|
}
|
2021-02-01 18:04:15 +00:00
|
|
|
folder.MappedPath = r.Form.Get("mapped_path")
|
|
|
|
err = dataprovider.UpdateFolder(&folder)
|
|
|
|
if err != nil {
|
|
|
|
renderFolderPage(w, r, folder, folderPageModeUpdate, err.Error())
|
|
|
|
return
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
2021-02-01 18:04:15 +00:00
|
|
|
http.Redirect(w, r, webFoldersPath, http.StatusSeeOther)
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleWebGetFolders(w http.ResponseWriter, r *http.Request) {
|
|
|
|
limit := defaultQueryLimit
|
|
|
|
if _, ok := r.URL.Query()["qlimit"]; ok {
|
|
|
|
var err error
|
|
|
|
limit, err = strconv.Atoi(r.URL.Query().Get("qlimit"))
|
|
|
|
if err != nil {
|
|
|
|
limit = defaultQueryLimit
|
|
|
|
}
|
|
|
|
}
|
|
|
|
folders := make([]vfs.BaseVirtualFolder, 0, limit)
|
|
|
|
for {
|
2021-02-01 18:04:15 +00:00
|
|
|
f, err := dataprovider.GetFolders(limit, len(folders), dataprovider.OrderASC)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
2021-01-17 21:29:08 +00:00
|
|
|
renderInternalServerErrorPage(w, r, err)
|
2020-06-07 21:30:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
folders = append(folders, f...)
|
|
|
|
if len(f) < limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
data := foldersPage{
|
2021-01-17 21:29:08 +00:00
|
|
|
basePage: getBasePageData(pageFoldersTitle, webFoldersPath, r),
|
2020-06-07 21:30:18 +00:00
|
|
|
Folders: folders,
|
|
|
|
}
|
|
|
|
renderTemplate(w, templateFolders, data)
|
|
|
|
}
|