2022-07-17 18:16:00 +00:00
|
|
|
// Copyright (C) 2019-2022 Nicola Murino
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published
|
|
|
|
// by the Free Software Foundation, version 3.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2021-05-06 19:35:43 +00:00
|
|
|
package httpd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2022-01-10 18:44:16 +00:00
|
|
|
"os"
|
2021-05-06 19:35:43 +00:00
|
|
|
"path/filepath"
|
2022-09-03 14:29:07 +00:00
|
|
|
"sort"
|
2021-05-06 19:35:43 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-chi/render"
|
2022-01-06 10:54:43 +00:00
|
|
|
"github.com/sftpgo/sdk"
|
|
|
|
sdkkms "github.com/sftpgo/sdk/kms"
|
2021-05-06 19:35:43 +00:00
|
|
|
|
2022-07-24 14:18:54 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/common"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/dataprovider"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/kms"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/mfa"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/smtp"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/util"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/version"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/vfs"
|
2021-05-06 19:35:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type userPageMode int
|
|
|
|
|
|
|
|
const (
|
|
|
|
userPageModeAdd userPageMode = iota + 1
|
|
|
|
userPageModeUpdate
|
|
|
|
userPageModeTemplate
|
|
|
|
)
|
|
|
|
|
|
|
|
type folderPageMode int
|
|
|
|
|
|
|
|
const (
|
|
|
|
folderPageModeAdd folderPageMode = iota + 1
|
|
|
|
folderPageModeUpdate
|
|
|
|
folderPageModeTemplate
|
|
|
|
)
|
|
|
|
|
2022-07-11 06:17:36 +00:00
|
|
|
type genericPageMode int
|
2022-04-25 13:49:11 +00:00
|
|
|
|
|
|
|
const (
|
2022-07-11 06:17:36 +00:00
|
|
|
genericPageModeAdd genericPageMode = iota + 1
|
|
|
|
genericPageModeUpdate
|
2022-04-25 13:49:11 +00:00
|
|
|
)
|
|
|
|
|
2021-05-06 19:35:43 +00:00
|
|
|
const (
|
2022-04-25 13:49:11 +00:00
|
|
|
templateAdminDir = "webadmin"
|
|
|
|
templateBase = "base.html"
|
|
|
|
templateBaseLogin = "baselogin.html"
|
|
|
|
templateFsConfig = "fsconfig.html"
|
|
|
|
templateSharedComponents = "sharedcomponents.html"
|
|
|
|
templateUsers = "users.html"
|
|
|
|
templateUser = "user.html"
|
|
|
|
templateAdmins = "admins.html"
|
|
|
|
templateAdmin = "admin.html"
|
|
|
|
templateConnections = "connections.html"
|
|
|
|
templateGroups = "groups.html"
|
|
|
|
templateGroup = "group.html"
|
|
|
|
templateFolders = "folders.html"
|
|
|
|
templateFolder = "folder.html"
|
2022-07-11 06:17:36 +00:00
|
|
|
templateEventRules = "eventrules.html"
|
|
|
|
templateEventRule = "eventrule.html"
|
|
|
|
templateEventActions = "eventactions.html"
|
|
|
|
templateEventAction = "eventaction.html"
|
2022-04-25 13:49:11 +00:00
|
|
|
templateMessage = "message.html"
|
|
|
|
templateStatus = "status.html"
|
|
|
|
templateLogin = "login.html"
|
|
|
|
templateDefender = "defender.html"
|
|
|
|
templateProfile = "profile.html"
|
|
|
|
templateChangePwd = "changepassword.html"
|
|
|
|
templateMaintenance = "maintenance.html"
|
|
|
|
templateMFA = "mfa.html"
|
|
|
|
templateSetup = "adminsetup.html"
|
|
|
|
pageUsersTitle = "Users"
|
|
|
|
pageAdminsTitle = "Admins"
|
|
|
|
pageConnectionsTitle = "Connections"
|
|
|
|
pageStatusTitle = "Status"
|
|
|
|
pageFoldersTitle = "Folders"
|
|
|
|
pageGroupsTitle = "Groups"
|
2022-07-11 06:17:36 +00:00
|
|
|
pageEventRulesTitle = "Event rules"
|
|
|
|
pageEventActionsTitle = "Event actions"
|
2022-04-25 13:49:11 +00:00
|
|
|
pageProfileTitle = "My profile"
|
|
|
|
pageChangePwdTitle = "Change password"
|
|
|
|
pageMaintenanceTitle = "Maintenance"
|
|
|
|
pageDefenderTitle = "Defender"
|
|
|
|
pageForgotPwdTitle = "SFTPGo Admin - Forgot password"
|
|
|
|
pageResetPwdTitle = "SFTPGo Admin - Reset password"
|
|
|
|
pageSetupTitle = "Create first admin user"
|
|
|
|
defaultQueryLimit = 500
|
2022-09-04 15:48:09 +00:00
|
|
|
inversePatternType = "inverse"
|
2021-05-06 19:35:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
adminTemplates = make(map[string]*template.Template)
|
|
|
|
)
|
|
|
|
|
|
|
|
type basePage struct {
|
|
|
|
Title string
|
|
|
|
CurrentURL string
|
|
|
|
UsersURL string
|
|
|
|
UserURL string
|
|
|
|
UserTemplateURL string
|
|
|
|
AdminsURL string
|
|
|
|
AdminURL string
|
|
|
|
QuotaScanURL string
|
|
|
|
ConnectionsURL string
|
2022-04-25 13:49:11 +00:00
|
|
|
GroupsURL string
|
|
|
|
GroupURL string
|
2021-05-06 19:35:43 +00:00
|
|
|
FoldersURL string
|
|
|
|
FolderURL string
|
|
|
|
FolderTemplateURL string
|
2021-06-08 11:24:28 +00:00
|
|
|
DefenderURL string
|
2021-05-06 19:35:43 +00:00
|
|
|
LogoutURL string
|
2021-09-29 16:46:15 +00:00
|
|
|
ProfileURL string
|
|
|
|
ChangePwdURL string
|
2021-09-04 10:11:04 +00:00
|
|
|
MFAURL string
|
2022-07-11 06:17:36 +00:00
|
|
|
EventRulesURL string
|
|
|
|
EventRuleURL string
|
|
|
|
EventActionsURL string
|
|
|
|
EventActionURL string
|
2021-05-06 19:35:43 +00:00
|
|
|
FolderQuotaScanURL string
|
|
|
|
StatusURL string
|
|
|
|
MaintenanceURL string
|
|
|
|
StaticURL string
|
|
|
|
UsersTitle string
|
|
|
|
AdminsTitle string
|
|
|
|
ConnectionsTitle string
|
|
|
|
FoldersTitle string
|
2022-04-25 13:49:11 +00:00
|
|
|
GroupsTitle string
|
2022-07-11 06:17:36 +00:00
|
|
|
EventRulesTitle string
|
|
|
|
EventActionsTitle string
|
2021-05-06 19:35:43 +00:00
|
|
|
StatusTitle string
|
|
|
|
MaintenanceTitle string
|
2021-06-08 11:24:28 +00:00
|
|
|
DefenderTitle string
|
2021-05-06 19:35:43 +00:00
|
|
|
Version string
|
|
|
|
CSRFToken string
|
2022-07-11 06:17:36 +00:00
|
|
|
IsEventManagerPage bool
|
2021-06-08 11:24:28 +00:00
|
|
|
HasDefender bool
|
2022-02-13 13:30:20 +00:00
|
|
|
HasExternalLogin bool
|
2021-05-06 19:35:43 +00:00
|
|
|
LoggedAdmin *dataprovider.Admin
|
2022-05-13 17:40:52 +00:00
|
|
|
Branding UIBranding
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type usersPage struct {
|
|
|
|
basePage
|
|
|
|
Users []dataprovider.User
|
|
|
|
}
|
|
|
|
|
|
|
|
type adminsPage struct {
|
|
|
|
basePage
|
|
|
|
Admins []dataprovider.Admin
|
|
|
|
}
|
|
|
|
|
|
|
|
type foldersPage struct {
|
|
|
|
basePage
|
|
|
|
Folders []vfs.BaseVirtualFolder
|
|
|
|
}
|
|
|
|
|
2022-04-25 13:49:11 +00:00
|
|
|
type groupsPage struct {
|
|
|
|
basePage
|
|
|
|
Groups []dataprovider.Group
|
|
|
|
}
|
|
|
|
|
2022-07-11 06:17:36 +00:00
|
|
|
type eventRulesPage struct {
|
|
|
|
basePage
|
|
|
|
Rules []dataprovider.EventRule
|
|
|
|
}
|
|
|
|
|
|
|
|
type eventActionsPage struct {
|
|
|
|
basePage
|
|
|
|
Actions []dataprovider.BaseEventAction
|
|
|
|
}
|
|
|
|
|
2021-05-06 19:35:43 +00:00
|
|
|
type connectionsPage struct {
|
|
|
|
basePage
|
2022-01-16 11:09:17 +00:00
|
|
|
Connections []common.ConnectionStatus
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type statusPage struct {
|
|
|
|
basePage
|
2022-02-26 15:43:29 +00:00
|
|
|
Status *ServicesStatus
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
2022-01-10 18:44:16 +00:00
|
|
|
type fsWrapper struct {
|
|
|
|
vfs.Filesystem
|
|
|
|
IsUserPage bool
|
2022-04-25 13:49:11 +00:00
|
|
|
IsGroupPage bool
|
2022-09-21 17:36:08 +00:00
|
|
|
IsHidden bool
|
2022-01-10 18:44:16 +00:00
|
|
|
HasUsersBaseDir bool
|
|
|
|
DirPath string
|
|
|
|
}
|
|
|
|
|
2021-05-06 19:35:43 +00:00
|
|
|
type userPage struct {
|
|
|
|
basePage
|
2022-03-06 15:57:13 +00:00
|
|
|
User *dataprovider.User
|
|
|
|
RootPerms []string
|
|
|
|
Error string
|
|
|
|
ValidPerms []string
|
|
|
|
ValidLoginMethods []string
|
|
|
|
ValidProtocols []string
|
|
|
|
TwoFactorProtocols []string
|
|
|
|
WebClientOptions []string
|
|
|
|
RootDirPerms []string
|
|
|
|
Mode userPageMode
|
|
|
|
VirtualFolders []vfs.BaseVirtualFolder
|
2022-04-25 13:49:11 +00:00
|
|
|
Groups []dataprovider.Group
|
2022-03-06 15:57:13 +00:00
|
|
|
CanImpersonate bool
|
|
|
|
FsWrapper fsWrapper
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type adminPage struct {
|
|
|
|
basePage
|
2022-09-13 16:04:27 +00:00
|
|
|
Admin *dataprovider.Admin
|
|
|
|
Groups []dataprovider.Group
|
|
|
|
Error string
|
|
|
|
IsAdd bool
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 16:46:15 +00:00
|
|
|
type profilePage struct {
|
2021-05-06 19:35:43 +00:00
|
|
|
basePage
|
2021-08-17 16:08:32 +00:00
|
|
|
Error string
|
|
|
|
AllowAPIKeyAuth bool
|
2021-09-29 16:46:15 +00:00
|
|
|
Email string
|
|
|
|
Description string
|
|
|
|
}
|
|
|
|
|
|
|
|
type changePasswordPage struct {
|
|
|
|
basePage
|
|
|
|
Error string
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
2021-09-04 10:11:04 +00:00
|
|
|
type mfaPage struct {
|
|
|
|
basePage
|
|
|
|
TOTPConfigs []string
|
2022-01-06 09:11:47 +00:00
|
|
|
TOTPConfig dataprovider.AdminTOTPConfig
|
2021-09-04 10:11:04 +00:00
|
|
|
GenerateTOTPURL string
|
|
|
|
ValidateTOTPURL string
|
|
|
|
SaveTOTPURL string
|
|
|
|
RecCodesURL string
|
|
|
|
}
|
|
|
|
|
2021-05-06 19:35:43 +00:00
|
|
|
type maintenancePage struct {
|
|
|
|
basePage
|
|
|
|
BackupPath string
|
|
|
|
RestorePath string
|
|
|
|
Error string
|
|
|
|
}
|
|
|
|
|
2021-06-08 11:24:28 +00:00
|
|
|
type defenderHostsPage struct {
|
|
|
|
basePage
|
|
|
|
DefenderHostsURL string
|
|
|
|
}
|
|
|
|
|
2021-05-14 17:21:15 +00:00
|
|
|
type setupPage struct {
|
|
|
|
basePage
|
2022-02-27 12:08:47 +00:00
|
|
|
Username string
|
|
|
|
HasInstallationCode bool
|
|
|
|
InstallationCodeHint string
|
2022-07-11 14:14:44 +00:00
|
|
|
HideSupportLink bool
|
2022-02-27 12:08:47 +00:00
|
|
|
Error string
|
2021-05-14 17:21:15 +00:00
|
|
|
}
|
|
|
|
|
2021-05-06 19:35:43 +00:00
|
|
|
type folderPage struct {
|
|
|
|
basePage
|
2022-01-10 18:44:16 +00:00
|
|
|
Folder vfs.BaseVirtualFolder
|
|
|
|
Error string
|
|
|
|
Mode folderPageMode
|
|
|
|
FsWrapper fsWrapper
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
2022-04-25 13:49:11 +00:00
|
|
|
type groupPage struct {
|
|
|
|
basePage
|
|
|
|
Group *dataprovider.Group
|
|
|
|
Error string
|
2022-07-11 06:17:36 +00:00
|
|
|
Mode genericPageMode
|
2022-04-25 13:49:11 +00:00
|
|
|
ValidPerms []string
|
|
|
|
ValidLoginMethods []string
|
|
|
|
ValidProtocols []string
|
|
|
|
TwoFactorProtocols []string
|
|
|
|
WebClientOptions []string
|
|
|
|
VirtualFolders []vfs.BaseVirtualFolder
|
|
|
|
FsWrapper fsWrapper
|
|
|
|
}
|
|
|
|
|
2022-07-11 06:17:36 +00:00
|
|
|
type eventActionPage struct {
|
|
|
|
basePage
|
|
|
|
Action dataprovider.BaseEventAction
|
|
|
|
ActionTypes []dataprovider.EnumMapping
|
2022-08-10 16:41:59 +00:00
|
|
|
FsActions []dataprovider.EnumMapping
|
2022-07-11 06:17:36 +00:00
|
|
|
HTTPMethods []string
|
|
|
|
RedactedSecret string
|
|
|
|
Error string
|
|
|
|
Mode genericPageMode
|
|
|
|
}
|
|
|
|
|
|
|
|
type eventRulePage struct {
|
|
|
|
basePage
|
|
|
|
Rule dataprovider.EventRule
|
|
|
|
TriggerTypes []dataprovider.EnumMapping
|
|
|
|
Actions []dataprovider.BaseEventAction
|
|
|
|
FsEvents []string
|
|
|
|
Protocols []string
|
|
|
|
ProviderEvents []string
|
|
|
|
ProviderObjects []string
|
|
|
|
Error string
|
|
|
|
Mode genericPageMode
|
|
|
|
IsShared bool
|
|
|
|
}
|
|
|
|
|
2021-05-06 19:35:43 +00:00
|
|
|
type messagePage struct {
|
|
|
|
basePage
|
|
|
|
Error string
|
|
|
|
Success string
|
|
|
|
}
|
|
|
|
|
|
|
|
type userTemplateFields struct {
|
2022-03-27 14:32:21 +00:00
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
PublicKeys []string
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func loadAdminTemplates(templatesPath string) {
|
|
|
|
usersPaths := []string{
|
2022-05-14 09:54:55 +00:00
|
|
|
filepath.Join(templatesPath, templateCommonDir, templateCommonCSS),
|
2021-05-06 19:35:43 +00:00
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateBase),
|
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateUsers),
|
|
|
|
}
|
|
|
|
userPaths := []string{
|
2022-05-14 09:54:55 +00:00
|
|
|
filepath.Join(templatesPath, templateCommonDir, templateCommonCSS),
|
2021-05-06 19:35:43 +00:00
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateBase),
|
2022-04-25 13:49:11 +00:00
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateSharedComponents),
|
2021-05-06 19:35:43 +00:00
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateFsConfig),
|
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateUser),
|
|
|
|
}
|
|
|
|
adminsPaths := []string{
|
2022-05-14 09:54:55 +00:00
|
|
|
filepath.Join(templatesPath, templateCommonDir, templateCommonCSS),
|
2021-05-06 19:35:43 +00:00
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateBase),
|
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateAdmins),
|
|
|
|
}
|
|
|
|
adminPaths := []string{
|
2022-05-14 09:54:55 +00:00
|
|
|
filepath.Join(templatesPath, templateCommonDir, templateCommonCSS),
|
2021-05-06 19:35:43 +00:00
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateBase),
|
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateAdmin),
|
|
|
|
}
|
2021-09-29 16:46:15 +00:00
|
|
|
profilePaths := []string{
|
2022-05-14 09:54:55 +00:00
|
|
|
filepath.Join(templatesPath, templateCommonDir, templateCommonCSS),
|
2021-05-06 19:35:43 +00:00
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateBase),
|
2021-09-29 16:46:15 +00:00
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateProfile),
|
|
|
|
}
|
|
|
|
changePwdPaths := []string{
|
2022-05-14 09:54:55 +00:00
|
|
|
filepath.Join(templatesPath, templateCommonDir, templateCommonCSS),
|
2021-09-29 16:46:15 +00:00
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateBase),
|
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateChangePwd),
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
connectionsPaths := []string{
|
2022-05-14 09:54:55 +00:00
|
|
|
filepath.Join(templatesPath, templateCommonDir, templateCommonCSS),
|
2021-05-06 19:35:43 +00:00
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateBase),
|
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateConnections),
|
|
|
|
}
|
2021-11-13 12:25:43 +00:00
|
|
|
messagePaths := []string{
|
2022-05-14 09:54:55 +00:00
|
|
|
filepath.Join(templatesPath, templateCommonDir, templateCommonCSS),
|
2021-05-06 19:35:43 +00:00
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateBase),
|
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateMessage),
|
|
|
|
}
|
2021-11-13 12:25:43 +00:00
|
|
|
foldersPaths := []string{
|
2022-05-14 09:54:55 +00:00
|
|
|
filepath.Join(templatesPath, templateCommonDir, templateCommonCSS),
|
2021-05-06 19:35:43 +00:00
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateBase),
|
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateFolders),
|
|
|
|
}
|
2021-11-13 12:25:43 +00:00
|
|
|
folderPaths := []string{
|
2022-05-14 09:54:55 +00:00
|
|
|
filepath.Join(templatesPath, templateCommonDir, templateCommonCSS),
|
2021-05-06 19:35:43 +00:00
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateBase),
|
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateFsConfig),
|
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateFolder),
|
|
|
|
}
|
2022-04-25 13:49:11 +00:00
|
|
|
groupsPaths := []string{
|
2022-05-14 09:54:55 +00:00
|
|
|
filepath.Join(templatesPath, templateCommonDir, templateCommonCSS),
|
2022-04-25 13:49:11 +00:00
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateBase),
|
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateGroups),
|
|
|
|
}
|
|
|
|
groupPaths := []string{
|
2022-05-14 09:54:55 +00:00
|
|
|
filepath.Join(templatesPath, templateCommonDir, templateCommonCSS),
|
2022-04-25 13:49:11 +00:00
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateBase),
|
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateFsConfig),
|
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateSharedComponents),
|
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateGroup),
|
|
|
|
}
|
2022-07-11 06:17:36 +00:00
|
|
|
eventRulesPaths := []string{
|
|
|
|
filepath.Join(templatesPath, templateCommonDir, templateCommonCSS),
|
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateBase),
|
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateEventRules),
|
|
|
|
}
|
|
|
|
eventRulePaths := []string{
|
|
|
|
filepath.Join(templatesPath, templateCommonDir, templateCommonCSS),
|
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateBase),
|
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateEventRule),
|
|
|
|
}
|
|
|
|
eventActionsPaths := []string{
|
|
|
|
filepath.Join(templatesPath, templateCommonDir, templateCommonCSS),
|
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateBase),
|
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateEventActions),
|
|
|
|
}
|
|
|
|
eventActionPaths := []string{
|
|
|
|
filepath.Join(templatesPath, templateCommonDir, templateCommonCSS),
|
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateBase),
|
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateEventAction),
|
|
|
|
}
|
2021-11-13 12:25:43 +00:00
|
|
|
statusPaths := []string{
|
2022-05-14 09:54:55 +00:00
|
|
|
filepath.Join(templatesPath, templateCommonDir, templateCommonCSS),
|
2021-05-06 19:35:43 +00:00
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateBase),
|
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateStatus),
|
|
|
|
}
|
2021-11-13 12:25:43 +00:00
|
|
|
loginPaths := []string{
|
2022-05-14 09:54:55 +00:00
|
|
|
filepath.Join(templatesPath, templateCommonDir, templateCommonCSS),
|
2021-09-04 10:11:04 +00:00
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateBaseLogin),
|
2021-05-06 19:35:43 +00:00
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateLogin),
|
|
|
|
}
|
2021-11-13 12:25:43 +00:00
|
|
|
maintenancePaths := []string{
|
2022-05-14 09:54:55 +00:00
|
|
|
filepath.Join(templatesPath, templateCommonDir, templateCommonCSS),
|
2021-05-06 19:35:43 +00:00
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateBase),
|
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateMaintenance),
|
|
|
|
}
|
2021-11-13 12:25:43 +00:00
|
|
|
defenderPaths := []string{
|
2022-05-14 09:54:55 +00:00
|
|
|
filepath.Join(templatesPath, templateCommonDir, templateCommonCSS),
|
2021-06-08 11:24:28 +00:00
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateBase),
|
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateDefender),
|
|
|
|
}
|
2021-11-13 12:25:43 +00:00
|
|
|
mfaPaths := []string{
|
2022-05-14 09:54:55 +00:00
|
|
|
filepath.Join(templatesPath, templateCommonDir, templateCommonCSS),
|
2021-09-04 10:11:04 +00:00
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateBase),
|
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateMFA),
|
|
|
|
}
|
2021-11-13 12:25:43 +00:00
|
|
|
twoFactorPaths := []string{
|
2022-05-14 09:54:55 +00:00
|
|
|
filepath.Join(templatesPath, templateCommonDir, templateCommonCSS),
|
2021-09-04 10:11:04 +00:00
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateBaseLogin),
|
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateTwoFactor),
|
|
|
|
}
|
2021-11-13 12:25:43 +00:00
|
|
|
twoFactorRecoveryPaths := []string{
|
2022-05-14 09:54:55 +00:00
|
|
|
filepath.Join(templatesPath, templateCommonDir, templateCommonCSS),
|
2021-09-04 10:11:04 +00:00
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateBaseLogin),
|
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateTwoFactorRecovery),
|
|
|
|
}
|
2021-11-13 12:25:43 +00:00
|
|
|
setupPaths := []string{
|
2022-05-14 09:54:55 +00:00
|
|
|
filepath.Join(templatesPath, templateCommonDir, templateCommonCSS),
|
2021-09-04 10:11:04 +00:00
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateBaseLogin),
|
2021-05-14 17:21:15 +00:00
|
|
|
filepath.Join(templatesPath, templateAdminDir, templateSetup),
|
|
|
|
}
|
2021-11-13 12:25:43 +00:00
|
|
|
forgotPwdPaths := []string{
|
2022-05-14 09:54:55 +00:00
|
|
|
filepath.Join(templatesPath, templateCommonDir, templateCommonCSS),
|
2021-11-13 12:25:43 +00:00
|
|
|
filepath.Join(templatesPath, templateCommonDir, templateForgotPassword),
|
|
|
|
}
|
|
|
|
resetPwdPaths := []string{
|
2022-05-14 09:54:55 +00:00
|
|
|
filepath.Join(templatesPath, templateCommonDir, templateCommonCSS),
|
2021-11-13 12:25:43 +00:00
|
|
|
filepath.Join(templatesPath, templateCommonDir, templateResetPassword),
|
|
|
|
}
|
2021-06-05 16:06:53 +00:00
|
|
|
|
2021-10-02 20:25:41 +00:00
|
|
|
fsBaseTpl := template.New("fsBaseTemplate").Funcs(template.FuncMap{
|
2022-01-10 18:44:16 +00:00
|
|
|
"ListFSProviders": func() []sdk.FilesystemProvider {
|
|
|
|
return []sdk.FilesystemProvider{sdk.LocalFilesystemProvider, sdk.CryptedFilesystemProvider,
|
2022-06-11 08:41:34 +00:00
|
|
|
sdk.S3FilesystemProvider, sdk.GCSFilesystemProvider, sdk.AzureBlobFilesystemProvider,
|
|
|
|
sdk.SFTPFilesystemProvider, sdk.HTTPFilesystemProvider,
|
2022-01-10 18:44:16 +00:00
|
|
|
}
|
|
|
|
},
|
2022-09-14 19:18:32 +00:00
|
|
|
"HumanizeBytes": util.ByteCountSI,
|
2021-06-19 12:06:56 +00:00
|
|
|
})
|
2021-09-30 08:23:25 +00:00
|
|
|
usersTmpl := util.LoadTemplate(nil, usersPaths...)
|
|
|
|
userTmpl := util.LoadTemplate(fsBaseTpl, userPaths...)
|
|
|
|
adminsTmpl := util.LoadTemplate(nil, adminsPaths...)
|
|
|
|
adminTmpl := util.LoadTemplate(nil, adminPaths...)
|
|
|
|
connectionsTmpl := util.LoadTemplate(nil, connectionsPaths...)
|
2021-11-13 12:25:43 +00:00
|
|
|
messageTmpl := util.LoadTemplate(nil, messagePaths...)
|
2022-04-25 13:49:11 +00:00
|
|
|
groupsTmpl := util.LoadTemplate(nil, groupsPaths...)
|
|
|
|
groupTmpl := util.LoadTemplate(fsBaseTpl, groupPaths...)
|
2021-11-13 12:25:43 +00:00
|
|
|
foldersTmpl := util.LoadTemplate(nil, foldersPaths...)
|
|
|
|
folderTmpl := util.LoadTemplate(fsBaseTpl, folderPaths...)
|
2022-07-11 06:17:36 +00:00
|
|
|
eventRulesTmpl := util.LoadTemplate(nil, eventRulesPaths...)
|
|
|
|
eventRuleTmpl := util.LoadTemplate(nil, eventRulePaths...)
|
|
|
|
eventActionsTmpl := util.LoadTemplate(nil, eventActionsPaths...)
|
|
|
|
eventActionTmpl := util.LoadTemplate(nil, eventActionPaths...)
|
2021-11-13 12:25:43 +00:00
|
|
|
statusTmpl := util.LoadTemplate(nil, statusPaths...)
|
|
|
|
loginTmpl := util.LoadTemplate(nil, loginPaths...)
|
2021-09-30 08:23:25 +00:00
|
|
|
profileTmpl := util.LoadTemplate(nil, profilePaths...)
|
|
|
|
changePwdTmpl := util.LoadTemplate(nil, changePwdPaths...)
|
2021-11-13 12:25:43 +00:00
|
|
|
maintenanceTmpl := util.LoadTemplate(nil, maintenancePaths...)
|
|
|
|
defenderTmpl := util.LoadTemplate(nil, defenderPaths...)
|
|
|
|
mfaTmpl := util.LoadTemplate(nil, mfaPaths...)
|
|
|
|
twoFactorTmpl := util.LoadTemplate(nil, twoFactorPaths...)
|
|
|
|
twoFactorRecoveryTmpl := util.LoadTemplate(nil, twoFactorRecoveryPaths...)
|
|
|
|
setupTmpl := util.LoadTemplate(nil, setupPaths...)
|
|
|
|
forgotPwdTmpl := util.LoadTemplate(nil, forgotPwdPaths...)
|
|
|
|
resetPwdTmpl := util.LoadTemplate(nil, resetPwdPaths...)
|
2021-05-06 19:35:43 +00:00
|
|
|
|
|
|
|
adminTemplates[templateUsers] = usersTmpl
|
|
|
|
adminTemplates[templateUser] = userTmpl
|
|
|
|
adminTemplates[templateAdmins] = adminsTmpl
|
|
|
|
adminTemplates[templateAdmin] = adminTmpl
|
|
|
|
adminTemplates[templateConnections] = connectionsTmpl
|
|
|
|
adminTemplates[templateMessage] = messageTmpl
|
2022-04-25 13:49:11 +00:00
|
|
|
adminTemplates[templateGroups] = groupsTmpl
|
|
|
|
adminTemplates[templateGroup] = groupTmpl
|
2021-05-06 19:35:43 +00:00
|
|
|
adminTemplates[templateFolders] = foldersTmpl
|
|
|
|
adminTemplates[templateFolder] = folderTmpl
|
2022-07-11 06:17:36 +00:00
|
|
|
adminTemplates[templateEventRules] = eventRulesTmpl
|
|
|
|
adminTemplates[templateEventRule] = eventRuleTmpl
|
|
|
|
adminTemplates[templateEventActions] = eventActionsTmpl
|
|
|
|
adminTemplates[templateEventAction] = eventActionTmpl
|
2021-05-06 19:35:43 +00:00
|
|
|
adminTemplates[templateStatus] = statusTmpl
|
|
|
|
adminTemplates[templateLogin] = loginTmpl
|
2021-09-29 16:46:15 +00:00
|
|
|
adminTemplates[templateProfile] = profileTmpl
|
|
|
|
adminTemplates[templateChangePwd] = changePwdTmpl
|
2021-05-06 19:35:43 +00:00
|
|
|
adminTemplates[templateMaintenance] = maintenanceTmpl
|
2021-06-08 11:24:28 +00:00
|
|
|
adminTemplates[templateDefender] = defenderTmpl
|
2021-09-04 10:11:04 +00:00
|
|
|
adminTemplates[templateMFA] = mfaTmpl
|
|
|
|
adminTemplates[templateTwoFactor] = twoFactorTmpl
|
|
|
|
adminTemplates[templateTwoFactorRecovery] = twoFactorRecoveryTmpl
|
2021-05-14 17:21:15 +00:00
|
|
|
adminTemplates[templateSetup] = setupTmpl
|
2021-11-13 12:25:43 +00:00
|
|
|
adminTemplates[templateForgotPassword] = forgotPwdTmpl
|
|
|
|
adminTemplates[templateResetPassword] = resetPwdTmpl
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 06:17:36 +00:00
|
|
|
func isEventManagerResource(currentURL string) bool {
|
|
|
|
if currentURL == webAdminEventRulesPath {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if currentURL == webAdminEventActionsPath {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if currentURL == webAdminEventRulePath || strings.HasPrefix(currentURL, webAdminEventRulePath+"/") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if currentURL == webAdminEventActionPath || strings.HasPrefix(currentURL, webAdminEventActionPath+"/") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) getBasePageData(title, currentURL string, r *http.Request) basePage {
|
2021-05-06 19:35:43 +00:00
|
|
|
var csrfToken string
|
|
|
|
if currentURL != "" {
|
2022-03-26 07:41:50 +00:00
|
|
|
csrfToken = createCSRFToken(util.GetIPFromRemoteAddress(r.RemoteAddr))
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
return basePage{
|
|
|
|
Title: title,
|
|
|
|
CurrentURL: currentURL,
|
|
|
|
UsersURL: webUsersPath,
|
|
|
|
UserURL: webUserPath,
|
|
|
|
UserTemplateURL: webTemplateUser,
|
|
|
|
AdminsURL: webAdminsPath,
|
|
|
|
AdminURL: webAdminPath,
|
2022-04-25 13:49:11 +00:00
|
|
|
GroupsURL: webGroupsPath,
|
|
|
|
GroupURL: webGroupPath,
|
2021-05-06 19:35:43 +00:00
|
|
|
FoldersURL: webFoldersPath,
|
|
|
|
FolderURL: webFolderPath,
|
|
|
|
FolderTemplateURL: webTemplateFolder,
|
2021-06-08 11:24:28 +00:00
|
|
|
DefenderURL: webDefenderPath,
|
2021-05-06 19:35:43 +00:00
|
|
|
LogoutURL: webLogoutPath,
|
2021-09-29 16:46:15 +00:00
|
|
|
ProfileURL: webAdminProfilePath,
|
|
|
|
ChangePwdURL: webChangeAdminPwdPath,
|
2021-09-04 10:11:04 +00:00
|
|
|
MFAURL: webAdminMFAPath,
|
2022-07-11 06:17:36 +00:00
|
|
|
EventRulesURL: webAdminEventRulesPath,
|
|
|
|
EventRuleURL: webAdminEventRulePath,
|
|
|
|
EventActionsURL: webAdminEventActionsPath,
|
|
|
|
EventActionURL: webAdminEventActionPath,
|
2021-05-06 19:35:43 +00:00
|
|
|
QuotaScanURL: webQuotaScanPath,
|
|
|
|
ConnectionsURL: webConnectionsPath,
|
|
|
|
StatusURL: webStatusPath,
|
|
|
|
FolderQuotaScanURL: webScanVFolderPath,
|
|
|
|
MaintenanceURL: webMaintenancePath,
|
|
|
|
StaticURL: webStaticFilesPath,
|
|
|
|
UsersTitle: pageUsersTitle,
|
|
|
|
AdminsTitle: pageAdminsTitle,
|
|
|
|
ConnectionsTitle: pageConnectionsTitle,
|
|
|
|
FoldersTitle: pageFoldersTitle,
|
2022-04-25 13:49:11 +00:00
|
|
|
GroupsTitle: pageGroupsTitle,
|
2022-07-11 06:17:36 +00:00
|
|
|
EventRulesTitle: pageEventRulesTitle,
|
|
|
|
EventActionsTitle: pageEventActionsTitle,
|
2021-05-06 19:35:43 +00:00
|
|
|
StatusTitle: pageStatusTitle,
|
|
|
|
MaintenanceTitle: pageMaintenanceTitle,
|
2021-06-08 11:24:28 +00:00
|
|
|
DefenderTitle: pageDefenderTitle,
|
2021-05-06 19:35:43 +00:00
|
|
|
Version: version.GetAsString(),
|
|
|
|
LoggedAdmin: getAdminFromToken(r),
|
2022-07-11 06:17:36 +00:00
|
|
|
IsEventManagerPage: isEventManagerResource(currentURL),
|
2021-06-08 11:24:28 +00:00
|
|
|
HasDefender: common.Config.DefenderConfig.Enabled,
|
2022-02-13 13:30:20 +00:00
|
|
|
HasExternalLogin: isLoggedInWithOIDC(r),
|
2021-05-06 19:35:43 +00:00
|
|
|
CSRFToken: csrfToken,
|
2022-05-13 17:40:52 +00:00
|
|
|
Branding: s.binding.Branding.WebAdmin,
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-19 17:49:51 +00:00
|
|
|
func renderAdminTemplate(w http.ResponseWriter, tmplName string, data any) {
|
2021-05-06 19:35:43 +00:00
|
|
|
err := adminTemplates[tmplName].ExecuteTemplate(w, tmplName, data)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) renderMessagePage(w http.ResponseWriter, r *http.Request, title, body string, statusCode int,
|
|
|
|
err error, message string,
|
|
|
|
) {
|
2021-05-06 19:35:43 +00:00
|
|
|
var errorString string
|
|
|
|
if body != "" {
|
|
|
|
errorString = body + " "
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
errorString += err.Error()
|
|
|
|
}
|
|
|
|
data := messagePage{
|
2022-03-19 20:44:27 +00:00
|
|
|
basePage: s.getBasePageData(title, "", r),
|
2021-05-06 19:35:43 +00:00
|
|
|
Error: errorString,
|
|
|
|
Success: message,
|
|
|
|
}
|
|
|
|
w.WriteHeader(statusCode)
|
|
|
|
renderAdminTemplate(w, templateMessage, data)
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) renderInternalServerErrorPage(w http.ResponseWriter, r *http.Request, err error) {
|
|
|
|
s.renderMessagePage(w, r, page500Title, page500Body, http.StatusInternalServerError, err, "")
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) renderBadRequestPage(w http.ResponseWriter, r *http.Request, err error) {
|
|
|
|
s.renderMessagePage(w, r, page400Title, "", http.StatusBadRequest, err, "")
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) renderForbiddenPage(w http.ResponseWriter, r *http.Request, body string) {
|
|
|
|
s.renderMessagePage(w, r, page403Title, "", http.StatusForbidden, nil, body)
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) renderNotFoundPage(w http.ResponseWriter, r *http.Request, err error) {
|
|
|
|
s.renderMessagePage(w, r, page404Title, page404Body, http.StatusNotFound, err, "")
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
2022-03-26 07:41:50 +00:00
|
|
|
func (s *httpdServer) renderForgotPwdPage(w http.ResponseWriter, error, ip string) {
|
2021-11-13 12:25:43 +00:00
|
|
|
data := forgotPwdPage{
|
|
|
|
CurrentURL: webAdminForgotPwdPath,
|
|
|
|
Error: error,
|
2022-03-26 07:41:50 +00:00
|
|
|
CSRFToken: createCSRFToken(ip),
|
2021-11-13 12:25:43 +00:00
|
|
|
StaticURL: webStaticFilesPath,
|
|
|
|
Title: pageForgotPwdTitle,
|
2022-05-13 17:40:52 +00:00
|
|
|
Branding: s.binding.Branding.WebAdmin,
|
2021-11-13 12:25:43 +00:00
|
|
|
}
|
|
|
|
renderAdminTemplate(w, templateForgotPassword, data)
|
|
|
|
}
|
|
|
|
|
2022-03-26 07:41:50 +00:00
|
|
|
func (s *httpdServer) renderResetPwdPage(w http.ResponseWriter, error, ip string) {
|
2021-11-13 12:25:43 +00:00
|
|
|
data := resetPwdPage{
|
|
|
|
CurrentURL: webAdminResetPwdPath,
|
|
|
|
Error: error,
|
2022-03-26 07:41:50 +00:00
|
|
|
CSRFToken: createCSRFToken(ip),
|
2021-11-13 12:25:43 +00:00
|
|
|
StaticURL: webStaticFilesPath,
|
|
|
|
Title: pageResetPwdTitle,
|
2022-05-13 17:40:52 +00:00
|
|
|
Branding: s.binding.Branding.WebAdmin,
|
2021-11-13 12:25:43 +00:00
|
|
|
}
|
|
|
|
renderAdminTemplate(w, templateResetPassword, data)
|
|
|
|
}
|
|
|
|
|
2022-03-26 07:41:50 +00:00
|
|
|
func (s *httpdServer) renderTwoFactorPage(w http.ResponseWriter, error, ip string) {
|
2021-09-04 10:11:04 +00:00
|
|
|
data := twoFactorPage{
|
|
|
|
CurrentURL: webAdminTwoFactorPath,
|
|
|
|
Version: version.Get().Version,
|
|
|
|
Error: error,
|
2022-03-26 07:41:50 +00:00
|
|
|
CSRFToken: createCSRFToken(ip),
|
2021-09-04 10:11:04 +00:00
|
|
|
StaticURL: webStaticFilesPath,
|
|
|
|
RecoveryURL: webAdminTwoFactorRecoveryPath,
|
2022-05-13 17:40:52 +00:00
|
|
|
Branding: s.binding.Branding.WebAdmin,
|
2021-09-04 10:11:04 +00:00
|
|
|
}
|
|
|
|
renderAdminTemplate(w, templateTwoFactor, data)
|
|
|
|
}
|
|
|
|
|
2022-03-26 07:41:50 +00:00
|
|
|
func (s *httpdServer) renderTwoFactorRecoveryPage(w http.ResponseWriter, error, ip string) {
|
2021-09-04 10:11:04 +00:00
|
|
|
data := twoFactorPage{
|
|
|
|
CurrentURL: webAdminTwoFactorRecoveryPath,
|
|
|
|
Version: version.Get().Version,
|
|
|
|
Error: error,
|
2022-03-26 07:41:50 +00:00
|
|
|
CSRFToken: createCSRFToken(ip),
|
2021-09-04 10:11:04 +00:00
|
|
|
StaticURL: webStaticFilesPath,
|
2022-05-13 17:40:52 +00:00
|
|
|
Branding: s.binding.Branding.WebAdmin,
|
2021-09-04 10:11:04 +00:00
|
|
|
}
|
|
|
|
renderAdminTemplate(w, templateTwoFactorRecovery, data)
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) renderMFAPage(w http.ResponseWriter, r *http.Request) {
|
2021-09-04 10:11:04 +00:00
|
|
|
data := mfaPage{
|
2022-03-19 20:44:27 +00:00
|
|
|
basePage: s.getBasePageData(pageMFATitle, webAdminMFAPath, r),
|
2021-09-04 10:11:04 +00:00
|
|
|
TOTPConfigs: mfa.GetAvailableTOTPConfigNames(),
|
|
|
|
GenerateTOTPURL: webAdminTOTPGeneratePath,
|
|
|
|
ValidateTOTPURL: webAdminTOTPValidatePath,
|
|
|
|
SaveTOTPURL: webAdminTOTPSavePath,
|
|
|
|
RecCodesURL: webAdminRecoveryCodesPath,
|
|
|
|
}
|
|
|
|
admin, err := dataprovider.AdminExists(data.LoggedAdmin.Username)
|
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderInternalServerErrorPage(w, r, err)
|
2021-09-04 10:11:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
data.TOTPConfig = admin.Filters.TOTPConfig
|
|
|
|
renderAdminTemplate(w, templateMFA, data)
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) renderProfilePage(w http.ResponseWriter, r *http.Request, error string) {
|
2021-09-29 16:46:15 +00:00
|
|
|
data := profilePage{
|
2022-03-19 20:44:27 +00:00
|
|
|
basePage: s.getBasePageData(pageProfileTitle, webAdminProfilePath, r),
|
2021-09-29 16:46:15 +00:00
|
|
|
Error: error,
|
2021-08-17 16:08:32 +00:00
|
|
|
}
|
|
|
|
admin, err := dataprovider.AdminExists(data.LoggedAdmin.Username)
|
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderInternalServerErrorPage(w, r, err)
|
2021-08-17 16:08:32 +00:00
|
|
|
return
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
2021-08-17 16:08:32 +00:00
|
|
|
data.AllowAPIKeyAuth = admin.Filters.AllowAPIKeyAuth
|
2021-09-29 16:46:15 +00:00
|
|
|
data.Email = admin.Email
|
|
|
|
data.Description = admin.Description
|
2021-05-06 19:35:43 +00:00
|
|
|
|
2021-09-29 16:46:15 +00:00
|
|
|
renderAdminTemplate(w, templateProfile, data)
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) renderChangePasswordPage(w http.ResponseWriter, r *http.Request, error string) {
|
2021-09-29 16:46:15 +00:00
|
|
|
data := changePasswordPage{
|
2022-03-19 20:44:27 +00:00
|
|
|
basePage: s.getBasePageData(pageChangePwdTitle, webChangeAdminPwdPath, r),
|
2021-09-29 16:46:15 +00:00
|
|
|
Error: error,
|
|
|
|
}
|
|
|
|
|
|
|
|
renderAdminTemplate(w, templateChangePwd, data)
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) renderMaintenancePage(w http.ResponseWriter, r *http.Request, error string) {
|
2021-05-06 19:35:43 +00:00
|
|
|
data := maintenancePage{
|
2022-03-19 20:44:27 +00:00
|
|
|
basePage: s.getBasePageData(pageMaintenanceTitle, webMaintenancePath, r),
|
2021-05-06 19:35:43 +00:00
|
|
|
BackupPath: webBackupPath,
|
|
|
|
RestorePath: webRestorePath,
|
|
|
|
Error: error,
|
|
|
|
}
|
|
|
|
|
|
|
|
renderAdminTemplate(w, templateMaintenance, data)
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) renderAdminSetupPage(w http.ResponseWriter, r *http.Request, username, error string) {
|
2021-05-14 17:21:15 +00:00
|
|
|
data := setupPage{
|
2022-03-19 20:44:27 +00:00
|
|
|
basePage: s.getBasePageData(pageSetupTitle, webAdminSetupPath, r),
|
2022-02-27 12:08:47 +00:00
|
|
|
Username: username,
|
|
|
|
HasInstallationCode: installationCode != "",
|
|
|
|
InstallationCodeHint: installationCodeHint,
|
2022-07-11 14:14:44 +00:00
|
|
|
HideSupportLink: hideSupportLink,
|
2022-02-27 12:08:47 +00:00
|
|
|
Error: error,
|
2021-05-14 17:21:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
renderAdminTemplate(w, templateSetup, data)
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) renderAddUpdateAdminPage(w http.ResponseWriter, r *http.Request, admin *dataprovider.Admin,
|
2021-05-06 19:35:43 +00:00
|
|
|
error string, isAdd bool) {
|
2022-09-13 16:04:27 +00:00
|
|
|
groups, err := s.getWebGroups(w, r, defaultQueryLimit, true)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-05-06 19:35:43 +00:00
|
|
|
currentURL := webAdminPath
|
2021-11-06 13:13:20 +00:00
|
|
|
title := "Add a new admin"
|
2021-05-06 19:35:43 +00:00
|
|
|
if !isAdd {
|
|
|
|
currentURL = fmt.Sprintf("%v/%v", webAdminPath, url.PathEscape(admin.Username))
|
2021-11-06 13:13:20 +00:00
|
|
|
title = "Update admin"
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
data := adminPage{
|
2022-03-19 20:44:27 +00:00
|
|
|
basePage: s.getBasePageData(title, currentURL, r),
|
2021-05-06 19:35:43 +00:00
|
|
|
Admin: admin,
|
2022-09-13 16:04:27 +00:00
|
|
|
Groups: groups,
|
2021-05-06 19:35:43 +00:00
|
|
|
Error: error,
|
|
|
|
IsAdd: isAdd,
|
|
|
|
}
|
|
|
|
|
|
|
|
renderAdminTemplate(w, templateAdmin, data)
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) renderUserPage(w http.ResponseWriter, r *http.Request, user *dataprovider.User,
|
|
|
|
mode userPageMode, error string,
|
|
|
|
) {
|
2022-04-25 13:49:11 +00:00
|
|
|
folders, err := s.getWebVirtualFolders(w, r, defaultQueryLimit, true)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
groups, err := s.getWebGroups(w, r, defaultQueryLimit, true)
|
2021-05-23 20:02:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-05-06 19:35:43 +00:00
|
|
|
user.SetEmptySecretsIfNil()
|
|
|
|
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
|
|
|
|
}
|
2022-07-17 16:48:41 +00:00
|
|
|
if user.Password != "" && user.IsPasswordHashed() {
|
|
|
|
switch mode {
|
|
|
|
case userPageModeUpdate:
|
|
|
|
user.Password = redactedSecret
|
|
|
|
default:
|
|
|
|
user.Password = ""
|
|
|
|
}
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
user.FsConfig.RedactedSecret = redactedSecret
|
2022-09-13 16:04:27 +00:00
|
|
|
basePage := s.getBasePageData(title, currentURL, r)
|
|
|
|
if (mode == userPageModeAdd || mode == userPageModeTemplate) && len(user.Groups) == 0 {
|
|
|
|
admin, err := dataprovider.AdminExists(basePage.LoggedAdmin.Username)
|
|
|
|
if err != nil {
|
|
|
|
s.renderInternalServerErrorPage(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, group := range admin.Groups {
|
|
|
|
user.Groups = append(user.Groups, sdk.GroupMapping{
|
|
|
|
Name: group.Name,
|
|
|
|
Type: group.Options.GetUserGroupType(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-05-06 19:35:43 +00:00
|
|
|
data := userPage{
|
2022-09-13 16:04:27 +00:00
|
|
|
basePage: basePage,
|
2022-03-06 15:57:13 +00:00
|
|
|
Mode: mode,
|
|
|
|
Error: error,
|
|
|
|
User: user,
|
|
|
|
ValidPerms: dataprovider.ValidPerms,
|
|
|
|
ValidLoginMethods: dataprovider.ValidLoginMethods,
|
|
|
|
ValidProtocols: dataprovider.ValidProtocols,
|
|
|
|
TwoFactorProtocols: dataprovider.MFAProtocols,
|
|
|
|
WebClientOptions: sdk.WebClientOptions,
|
|
|
|
RootDirPerms: user.GetPermissionsForPath("/"),
|
|
|
|
VirtualFolders: folders,
|
2022-04-25 13:49:11 +00:00
|
|
|
Groups: groups,
|
2022-03-06 15:57:13 +00:00
|
|
|
CanImpersonate: os.Getuid() == 0,
|
2022-01-10 18:44:16 +00:00
|
|
|
FsWrapper: fsWrapper{
|
|
|
|
Filesystem: user.FsConfig,
|
|
|
|
IsUserPage: true,
|
2022-04-25 13:49:11 +00:00
|
|
|
IsGroupPage: false,
|
2022-09-21 17:36:08 +00:00
|
|
|
IsHidden: basePage.LoggedAdmin.Filters.Preferences.HideFilesystem(),
|
2022-01-10 18:44:16 +00:00
|
|
|
HasUsersBaseDir: dataprovider.HasUsersBaseDir(),
|
|
|
|
DirPath: user.HomeDir,
|
|
|
|
},
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
renderAdminTemplate(w, templateUser, data)
|
|
|
|
}
|
|
|
|
|
2022-04-25 13:49:11 +00:00
|
|
|
func (s *httpdServer) renderGroupPage(w http.ResponseWriter, r *http.Request, group dataprovider.Group,
|
2022-07-11 06:17:36 +00:00
|
|
|
mode genericPageMode, error string,
|
2022-04-25 13:49:11 +00:00
|
|
|
) {
|
|
|
|
folders, err := s.getWebVirtualFolders(w, r, defaultQueryLimit, true)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
group.SetEmptySecretsIfNil()
|
|
|
|
group.UserSettings.FsConfig.RedactedSecret = redactedSecret
|
|
|
|
var title, currentURL string
|
|
|
|
switch mode {
|
2022-07-11 06:17:36 +00:00
|
|
|
case genericPageModeAdd:
|
2022-04-25 13:49:11 +00:00
|
|
|
title = "Add a new group"
|
|
|
|
currentURL = webGroupPath
|
2022-07-11 06:17:36 +00:00
|
|
|
case genericPageModeUpdate:
|
2022-04-25 13:49:11 +00:00
|
|
|
title = "Update group"
|
|
|
|
currentURL = fmt.Sprintf("%v/%v", webGroupPath, url.PathEscape(group.Name))
|
|
|
|
}
|
|
|
|
group.UserSettings.FsConfig.RedactedSecret = redactedSecret
|
|
|
|
group.UserSettings.FsConfig.SetEmptySecretsIfNil()
|
|
|
|
|
|
|
|
data := groupPage{
|
|
|
|
basePage: s.getBasePageData(title, currentURL, r),
|
|
|
|
Error: error,
|
|
|
|
Group: &group,
|
|
|
|
Mode: mode,
|
|
|
|
ValidPerms: dataprovider.ValidPerms,
|
|
|
|
ValidLoginMethods: dataprovider.ValidLoginMethods,
|
|
|
|
ValidProtocols: dataprovider.ValidProtocols,
|
|
|
|
TwoFactorProtocols: dataprovider.MFAProtocols,
|
|
|
|
WebClientOptions: sdk.WebClientOptions,
|
|
|
|
VirtualFolders: folders,
|
|
|
|
FsWrapper: fsWrapper{
|
|
|
|
Filesystem: group.UserSettings.FsConfig,
|
|
|
|
IsUserPage: false,
|
|
|
|
IsGroupPage: true,
|
|
|
|
HasUsersBaseDir: false,
|
|
|
|
DirPath: group.UserSettings.HomeDir,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
renderAdminTemplate(w, templateGroup, data)
|
|
|
|
}
|
|
|
|
|
2022-07-11 06:17:36 +00:00
|
|
|
func (s *httpdServer) renderEventActionPage(w http.ResponseWriter, r *http.Request, action dataprovider.BaseEventAction,
|
|
|
|
mode genericPageMode, error string,
|
|
|
|
) {
|
|
|
|
action.Options.SetEmptySecretsIfNil()
|
|
|
|
var title, currentURL string
|
|
|
|
switch mode {
|
|
|
|
case genericPageModeAdd:
|
|
|
|
title = "Add a new event action"
|
|
|
|
currentURL = webAdminEventActionPath
|
|
|
|
case genericPageModeUpdate:
|
|
|
|
title = "Update event action"
|
|
|
|
currentURL = fmt.Sprintf("%v/%v", webAdminEventActionPath, url.PathEscape(action.Name))
|
|
|
|
}
|
|
|
|
if action.Options.HTTPConfig.Timeout == 0 {
|
|
|
|
action.Options.HTTPConfig.Timeout = 20
|
|
|
|
}
|
|
|
|
if action.Options.CmdConfig.Timeout == 0 {
|
|
|
|
action.Options.CmdConfig.Timeout = 20
|
|
|
|
}
|
|
|
|
|
|
|
|
data := eventActionPage{
|
|
|
|
basePage: s.getBasePageData(title, currentURL, r),
|
|
|
|
Action: action,
|
|
|
|
ActionTypes: dataprovider.EventActionTypes,
|
2022-08-10 16:41:59 +00:00
|
|
|
FsActions: dataprovider.FsActionTypes,
|
2022-07-11 06:17:36 +00:00
|
|
|
HTTPMethods: dataprovider.SupportedHTTPActionMethods,
|
|
|
|
RedactedSecret: redactedSecret,
|
|
|
|
Error: error,
|
|
|
|
Mode: mode,
|
|
|
|
}
|
|
|
|
renderAdminTemplate(w, templateEventAction, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpdServer) renderEventRulePage(w http.ResponseWriter, r *http.Request, rule dataprovider.EventRule,
|
|
|
|
mode genericPageMode, error string,
|
|
|
|
) {
|
|
|
|
actions, err := s.getWebEventActions(w, r, defaultQueryLimit, true)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var title, currentURL string
|
|
|
|
switch mode {
|
|
|
|
case genericPageModeAdd:
|
|
|
|
title = "Add new event rules"
|
|
|
|
currentURL = webAdminEventRulePath
|
|
|
|
case genericPageModeUpdate:
|
|
|
|
title = "Update event rules"
|
|
|
|
currentURL = fmt.Sprintf("%v/%v", webAdminEventRulePath, url.PathEscape(rule.Name))
|
|
|
|
}
|
|
|
|
|
|
|
|
data := eventRulePage{
|
|
|
|
basePage: s.getBasePageData(title, currentURL, r),
|
|
|
|
Rule: rule,
|
|
|
|
TriggerTypes: dataprovider.EventTriggerTypes,
|
|
|
|
Actions: actions,
|
|
|
|
FsEvents: dataprovider.SupportedFsEvents,
|
|
|
|
Protocols: dataprovider.SupportedRuleConditionProtocols,
|
|
|
|
ProviderEvents: dataprovider.SupportedProviderEvents,
|
|
|
|
ProviderObjects: dataprovider.SupporteRuleConditionProviderObjects,
|
|
|
|
Error: error,
|
|
|
|
Mode: mode,
|
|
|
|
IsShared: s.isShared > 0,
|
|
|
|
}
|
|
|
|
renderAdminTemplate(w, templateEventRule, data)
|
|
|
|
}
|
|
|
|
|
2022-04-25 13:49:11 +00:00
|
|
|
func (s *httpdServer) renderFolderPage(w http.ResponseWriter, r *http.Request, folder vfs.BaseVirtualFolder,
|
|
|
|
mode folderPageMode, error string,
|
|
|
|
) {
|
2021-05-06 19:35:43 +00:00
|
|
|
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))
|
|
|
|
case folderPageModeTemplate:
|
|
|
|
title = "Folder template"
|
|
|
|
currentURL = webTemplateFolder
|
|
|
|
}
|
|
|
|
folder.FsConfig.RedactedSecret = redactedSecret
|
|
|
|
folder.FsConfig.SetEmptySecretsIfNil()
|
|
|
|
|
|
|
|
data := folderPage{
|
2022-03-19 20:44:27 +00:00
|
|
|
basePage: s.getBasePageData(title, currentURL, r),
|
2021-05-06 19:35:43 +00:00
|
|
|
Error: error,
|
|
|
|
Folder: folder,
|
|
|
|
Mode: mode,
|
2022-01-10 18:44:16 +00:00
|
|
|
FsWrapper: fsWrapper{
|
|
|
|
Filesystem: folder.FsConfig,
|
|
|
|
IsUserPage: false,
|
2022-04-25 13:49:11 +00:00
|
|
|
IsGroupPage: false,
|
2022-01-10 18:44:16 +00:00
|
|
|
HasUsersBaseDir: false,
|
|
|
|
DirPath: folder.MappedPath,
|
|
|
|
},
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
renderAdminTemplate(w, templateFolder, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getFoldersForTemplate(r *http.Request) []string {
|
|
|
|
var res []string
|
2021-05-23 20:02:01 +00:00
|
|
|
folderNames := r.Form["tpl_foldername"]
|
2021-05-06 19:35:43 +00:00
|
|
|
folders := make(map[string]bool)
|
2021-05-23 20:02:01 +00:00
|
|
|
for _, name := range folderNames {
|
|
|
|
name = strings.TrimSpace(name)
|
|
|
|
if name == "" {
|
|
|
|
continue
|
|
|
|
}
|
2021-05-06 19:35:43 +00:00
|
|
|
if _, ok := folders[name]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
folders[name] = true
|
|
|
|
res = append(res, name)
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
func getUsersForTemplate(r *http.Request) []userTemplateFields {
|
|
|
|
var res []userTemplateFields
|
2021-05-23 20:02:01 +00:00
|
|
|
tplUsernames := r.Form["tpl_username"]
|
|
|
|
tplPasswords := r.Form["tpl_password"]
|
|
|
|
tplPublicKeys := r.Form["tpl_public_keys"]
|
2021-05-06 19:35:43 +00:00
|
|
|
|
2021-05-23 20:02:01 +00:00
|
|
|
users := make(map[string]bool)
|
|
|
|
for idx, username := range tplUsernames {
|
|
|
|
username = strings.TrimSpace(username)
|
|
|
|
password := ""
|
|
|
|
publicKey := ""
|
|
|
|
if len(tplPasswords) > idx {
|
|
|
|
password = strings.TrimSpace(tplPasswords[idx])
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
2021-05-23 20:02:01 +00:00
|
|
|
if len(tplPublicKeys) > idx {
|
|
|
|
publicKey = strings.TrimSpace(tplPublicKeys[idx])
|
|
|
|
}
|
2022-04-01 07:47:54 +00:00
|
|
|
if username == "" {
|
2021-05-23 20:02:01 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if _, ok := users[username]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
users[username] = true
|
|
|
|
res = append(res, userTemplateFields{
|
2022-03-27 14:32:21 +00:00
|
|
|
Username: username,
|
|
|
|
Password: password,
|
|
|
|
PublicKeys: []string{publicKey},
|
2021-05-23 20:02:01 +00:00
|
|
|
})
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
2021-05-23 20:02:01 +00:00
|
|
|
|
2021-05-06 19:35:43 +00:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
func getVirtualFoldersFromPostFields(r *http.Request) []vfs.VirtualFolder {
|
|
|
|
var virtualFolders []vfs.VirtualFolder
|
2021-05-23 20:02:01 +00:00
|
|
|
folderPaths := r.Form["vfolder_path"]
|
|
|
|
folderNames := r.Form["vfolder_name"]
|
|
|
|
folderQuotaSizes := r.Form["vfolder_quota_size"]
|
|
|
|
folderQuotaFiles := r.Form["vfolder_quota_files"]
|
|
|
|
for idx, p := range folderPaths {
|
|
|
|
p = strings.TrimSpace(p)
|
|
|
|
name := ""
|
|
|
|
if len(folderNames) > idx {
|
|
|
|
name = folderNames[idx]
|
|
|
|
}
|
|
|
|
if p != "" && name != "" {
|
|
|
|
vfolder := vfs.VirtualFolder{
|
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
|
|
|
Name: name,
|
|
|
|
},
|
|
|
|
VirtualPath: p,
|
|
|
|
QuotaFiles: -1,
|
|
|
|
QuotaSize: -1,
|
|
|
|
}
|
|
|
|
if len(folderQuotaSizes) > idx {
|
2022-09-14 19:18:32 +00:00
|
|
|
quotaSize, err := util.ParseBytes(folderQuotaSizes[idx])
|
2021-05-23 20:02:01 +00:00
|
|
|
if err == nil {
|
|
|
|
vfolder.QuotaSize = quotaSize
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(folderQuotaFiles) > idx {
|
|
|
|
quotaFiles, err := strconv.Atoi(strings.TrimSpace(folderQuotaFiles[idx]))
|
|
|
|
if err == nil {
|
|
|
|
vfolder.QuotaFiles = quotaFiles
|
|
|
|
}
|
|
|
|
}
|
|
|
|
virtualFolders = append(virtualFolders, vfolder)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-06 19:35:43 +00:00
|
|
|
return virtualFolders
|
|
|
|
}
|
|
|
|
|
2022-04-25 13:49:11 +00:00
|
|
|
func getSubDirPermissionsFromPostFields(r *http.Request) map[string][]string {
|
2021-05-06 19:35:43 +00:00
|
|
|
permissions := make(map[string][]string)
|
2021-05-23 20:02:01 +00:00
|
|
|
|
|
|
|
for k := range r.Form {
|
|
|
|
if strings.HasPrefix(k, "sub_perm_path") {
|
|
|
|
p := strings.TrimSpace(r.Form.Get(k))
|
|
|
|
if p != "" {
|
|
|
|
idx := strings.TrimPrefix(k, "sub_perm_path")
|
|
|
|
permissions[p] = r.Form[fmt.Sprintf("sub_perm_permissions%v", idx)]
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-23 20:02:01 +00:00
|
|
|
|
2021-05-06 19:35:43 +00:00
|
|
|
return permissions
|
|
|
|
}
|
|
|
|
|
2022-04-25 13:49:11 +00:00
|
|
|
func getUserPermissionsFromPostFields(r *http.Request) map[string][]string {
|
|
|
|
permissions := getSubDirPermissionsFromPostFields(r)
|
|
|
|
permissions["/"] = r.Form["permissions"]
|
|
|
|
|
|
|
|
return permissions
|
|
|
|
}
|
|
|
|
|
2022-01-30 10:42:36 +00:00
|
|
|
func getDataTransferLimitsFromPostFields(r *http.Request) ([]sdk.DataTransferLimit, error) {
|
|
|
|
var result []sdk.DataTransferLimit
|
|
|
|
|
|
|
|
for k := range r.Form {
|
|
|
|
if strings.HasPrefix(k, "data_transfer_limit_sources") {
|
|
|
|
sources := getSliceFromDelimitedValues(r.Form.Get(k), ",")
|
|
|
|
if len(sources) > 0 {
|
|
|
|
dtLimit := sdk.DataTransferLimit{
|
|
|
|
Sources: sources,
|
|
|
|
}
|
|
|
|
idx := strings.TrimPrefix(k, "data_transfer_limit_sources")
|
|
|
|
ul := r.Form.Get(fmt.Sprintf("upload_data_transfer_source%v", idx))
|
|
|
|
dl := r.Form.Get(fmt.Sprintf("download_data_transfer_source%v", idx))
|
|
|
|
total := r.Form.Get(fmt.Sprintf("total_data_transfer_source%v", idx))
|
|
|
|
if ul != "" {
|
|
|
|
dataUL, err := strconv.ParseInt(ul, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return result, fmt.Errorf("invalid upload_data_transfer_source%v %#v: %w", idx, ul, err)
|
|
|
|
}
|
|
|
|
dtLimit.UploadDataTransfer = dataUL
|
|
|
|
}
|
|
|
|
if dl != "" {
|
|
|
|
dataDL, err := strconv.ParseInt(dl, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return result, fmt.Errorf("invalid download_data_transfer_source%v %#v: %w", idx, dl, err)
|
|
|
|
}
|
|
|
|
dtLimit.DownloadDataTransfer = dataDL
|
|
|
|
}
|
|
|
|
if total != "" {
|
|
|
|
dataTotal, err := strconv.ParseInt(total, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return result, fmt.Errorf("invalid total_data_transfer_source%v %#v: %w", idx, total, err)
|
|
|
|
}
|
|
|
|
dtLimit.TotalDataTransfer = dataTotal
|
|
|
|
}
|
|
|
|
|
|
|
|
result = append(result, dtLimit)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2021-12-10 17:43:26 +00:00
|
|
|
func getBandwidthLimitsFromPostFields(r *http.Request) ([]sdk.BandwidthLimit, error) {
|
|
|
|
var result []sdk.BandwidthLimit
|
|
|
|
|
|
|
|
for k := range r.Form {
|
|
|
|
if strings.HasPrefix(k, "bandwidth_limit_sources") {
|
|
|
|
sources := getSliceFromDelimitedValues(r.Form.Get(k), ",")
|
|
|
|
if len(sources) > 0 {
|
|
|
|
bwLimit := sdk.BandwidthLimit{
|
|
|
|
Sources: sources,
|
|
|
|
}
|
|
|
|
idx := strings.TrimPrefix(k, "bandwidth_limit_sources")
|
|
|
|
ul := r.Form.Get(fmt.Sprintf("upload_bandwidth_source%v", idx))
|
|
|
|
dl := r.Form.Get(fmt.Sprintf("download_bandwidth_source%v", idx))
|
|
|
|
if ul != "" {
|
|
|
|
bandwidthUL, err := strconv.ParseInt(ul, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return result, fmt.Errorf("invalid upload_bandwidth_source%v %#v: %w", idx, ul, err)
|
|
|
|
}
|
|
|
|
bwLimit.UploadBandwidth = bandwidthUL
|
|
|
|
}
|
|
|
|
if dl != "" {
|
|
|
|
bandwidthDL, err := strconv.ParseInt(dl, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return result, fmt.Errorf("invalid download_bandwidth_source%v %#v: %w", idx, ul, err)
|
|
|
|
}
|
|
|
|
bwLimit.DownloadBandwidth = bandwidthDL
|
|
|
|
}
|
|
|
|
result = append(result, bwLimit)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2022-01-15 16:16:49 +00:00
|
|
|
func getPatterDenyPolicyFromString(policy string) int {
|
|
|
|
denyPolicy := sdk.DenyPolicyDefault
|
|
|
|
if policy == "1" {
|
|
|
|
denyPolicy = sdk.DenyPolicyHide
|
|
|
|
}
|
|
|
|
return denyPolicy
|
|
|
|
}
|
|
|
|
|
2021-07-11 13:26:51 +00:00
|
|
|
func getFilePatternsFromPostField(r *http.Request) []sdk.PatternsFilter {
|
|
|
|
var result []sdk.PatternsFilter
|
2021-05-23 20:02:01 +00:00
|
|
|
|
|
|
|
allowedPatterns := make(map[string][]string)
|
|
|
|
deniedPatterns := make(map[string][]string)
|
2022-01-15 16:16:49 +00:00
|
|
|
patternPolicies := make(map[string]string)
|
2021-05-23 20:02:01 +00:00
|
|
|
|
|
|
|
for k := range r.Form {
|
|
|
|
if strings.HasPrefix(k, "pattern_path") {
|
|
|
|
p := strings.TrimSpace(r.Form.Get(k))
|
|
|
|
idx := strings.TrimPrefix(k, "pattern_path")
|
|
|
|
filters := strings.TrimSpace(r.Form.Get(fmt.Sprintf("patterns%v", idx)))
|
|
|
|
filters = strings.ReplaceAll(filters, " ", "")
|
|
|
|
patternType := r.Form.Get(fmt.Sprintf("pattern_type%v", idx))
|
2022-01-15 16:16:49 +00:00
|
|
|
patternPolicy := r.Form.Get(fmt.Sprintf("pattern_policy%v", idx))
|
2021-05-23 20:02:01 +00:00
|
|
|
if p != "" && filters != "" {
|
|
|
|
if patternType == "allowed" {
|
|
|
|
allowedPatterns[p] = append(allowedPatterns[p], strings.Split(filters, ",")...)
|
|
|
|
} else {
|
|
|
|
deniedPatterns[p] = append(deniedPatterns[p], strings.Split(filters, ",")...)
|
|
|
|
}
|
2022-01-15 16:16:49 +00:00
|
|
|
if patternPolicy != "" && patternPolicy != "0" {
|
|
|
|
patternPolicies[p] = patternPolicy
|
|
|
|
}
|
2021-05-23 20:02:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-06 19:35:43 +00:00
|
|
|
|
|
|
|
for dirAllowed, allowPatterns := range allowedPatterns {
|
2021-07-11 13:26:51 +00:00
|
|
|
filter := sdk.PatternsFilter{
|
2021-05-06 19:35:43 +00:00
|
|
|
Path: dirAllowed,
|
2022-01-15 16:16:49 +00:00
|
|
|
AllowedPatterns: allowPatterns,
|
|
|
|
DenyPolicy: getPatterDenyPolicyFromString(patternPolicies[dirAllowed]),
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
for dirDenied, denPatterns := range deniedPatterns {
|
|
|
|
if dirAllowed == dirDenied {
|
2022-01-15 16:16:49 +00:00
|
|
|
filter.DeniedPatterns = denPatterns
|
2021-05-06 19:35:43 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result = append(result, filter)
|
|
|
|
}
|
|
|
|
for dirDenied, denPatterns := range deniedPatterns {
|
|
|
|
found := false
|
|
|
|
for _, res := range result {
|
|
|
|
if res.Path == dirDenied {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
2021-07-11 13:26:51 +00:00
|
|
|
result = append(result, sdk.PatternsFilter{
|
2021-05-06 19:35:43 +00:00
|
|
|
Path: dirDenied,
|
|
|
|
DeniedPatterns: denPatterns,
|
2022-01-15 16:16:49 +00:00
|
|
|
DenyPolicy: getPatterDenyPolicyFromString(patternPolicies[dirDenied]),
|
2021-05-06 19:35:43 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2022-04-25 13:49:11 +00:00
|
|
|
func getGroupsFromUserPostFields(r *http.Request) []sdk.GroupMapping {
|
|
|
|
var groups []sdk.GroupMapping
|
|
|
|
|
|
|
|
primaryGroup := r.Form.Get("primary_group")
|
|
|
|
if primaryGroup != "" {
|
|
|
|
groups = append(groups, sdk.GroupMapping{
|
|
|
|
Name: primaryGroup,
|
|
|
|
Type: sdk.GroupTypePrimary,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
secondaryGroups := r.Form["secondary_groups"]
|
|
|
|
for _, name := range secondaryGroups {
|
|
|
|
groups = append(groups, sdk.GroupMapping{
|
|
|
|
Name: name,
|
|
|
|
Type: sdk.GroupTypeSecondary,
|
|
|
|
})
|
|
|
|
}
|
2022-09-13 16:04:27 +00:00
|
|
|
membershipGroups := r.Form["membership_groups"]
|
|
|
|
for _, name := range membershipGroups {
|
|
|
|
groups = append(groups, sdk.GroupMapping{
|
|
|
|
Name: name,
|
|
|
|
Type: sdk.GroupTypeMembership,
|
|
|
|
})
|
|
|
|
}
|
2022-04-25 13:49:11 +00:00
|
|
|
return groups
|
|
|
|
}
|
|
|
|
|
2022-01-06 09:11:47 +00:00
|
|
|
func getFiltersFromUserPostFields(r *http.Request) (sdk.BaseUserFilters, error) {
|
|
|
|
var filters sdk.BaseUserFilters
|
2021-12-10 17:43:26 +00:00
|
|
|
bwLimits, err := getBandwidthLimitsFromPostFields(r)
|
|
|
|
if err != nil {
|
|
|
|
return filters, err
|
|
|
|
}
|
2022-01-30 10:42:36 +00:00
|
|
|
dtLimits, err := getDataTransferLimitsFromPostFields(r)
|
|
|
|
if err != nil {
|
|
|
|
return filters, err
|
|
|
|
}
|
2022-09-14 19:18:32 +00:00
|
|
|
maxFileSize, err := util.ParseBytes(r.Form.Get("max_upload_file_size"))
|
2022-04-25 13:49:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return filters, fmt.Errorf("invalid max upload file size: %w", err)
|
|
|
|
}
|
2022-08-28 12:41:42 +00:00
|
|
|
defaultSharesExpiration, err := strconv.ParseInt(r.Form.Get("default_shares_expiration"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return filters, fmt.Errorf("invalid default shares expiration: %w", err)
|
|
|
|
}
|
2022-07-26 16:51:39 +00:00
|
|
|
if r.Form.Get("ftp_security") == "1" {
|
|
|
|
filters.FTPSecurity = 1
|
|
|
|
}
|
2021-12-10 17:43:26 +00:00
|
|
|
filters.BandwidthLimits = bwLimits
|
2022-01-30 10:42:36 +00:00
|
|
|
filters.DataTransferLimits = dtLimits
|
2021-05-06 19:35:43 +00:00
|
|
|
filters.AllowedIP = getSliceFromDelimitedValues(r.Form.Get("allowed_ip"), ",")
|
|
|
|
filters.DeniedIP = getSliceFromDelimitedValues(r.Form.Get("denied_ip"), ",")
|
2022-03-06 15:57:13 +00:00
|
|
|
filters.DeniedLoginMethods = r.Form["denied_login_methods"]
|
2021-05-06 19:35:43 +00:00
|
|
|
filters.DeniedProtocols = r.Form["denied_protocols"]
|
2022-03-06 15:57:13 +00:00
|
|
|
filters.TwoFactorAuthProtocols = r.Form["required_two_factor_protocols"]
|
2021-05-23 20:02:01 +00:00
|
|
|
filters.FilePatterns = getFilePatternsFromPostField(r)
|
2021-07-11 13:26:51 +00:00
|
|
|
filters.TLSUsername = sdk.TLSUsername(r.Form.Get("tls_username"))
|
2021-05-06 19:35:43 +00:00
|
|
|
filters.WebClient = r.Form["web_client_options"]
|
2022-08-28 12:41:42 +00:00
|
|
|
filters.DefaultSharesExpiration = int(defaultSharesExpiration)
|
2021-05-06 19:35:43 +00:00
|
|
|
hooks := r.Form["hooks"]
|
2022-05-19 17:49:51 +00:00
|
|
|
if util.Contains(hooks, "external_auth_disabled") {
|
2021-05-06 19:35:43 +00:00
|
|
|
filters.Hooks.ExternalAuthDisabled = true
|
|
|
|
}
|
2022-05-19 17:49:51 +00:00
|
|
|
if util.Contains(hooks, "pre_login_disabled") {
|
2021-05-06 19:35:43 +00:00
|
|
|
filters.Hooks.PreLoginDisabled = true
|
|
|
|
}
|
2022-05-19 17:49:51 +00:00
|
|
|
if util.Contains(hooks, "check_password_disabled") {
|
2021-05-06 19:35:43 +00:00
|
|
|
filters.Hooks.CheckPasswordDisabled = true
|
|
|
|
}
|
2022-07-27 16:54:25 +00:00
|
|
|
filters.IsAnonymous = r.Form.Get("is_anonymous") != ""
|
2022-06-11 08:41:34 +00:00
|
|
|
filters.DisableFsChecks = r.Form.Get("disable_fs_checks") != ""
|
|
|
|
filters.AllowAPIKeyAuth = r.Form.Get("allow_api_key_auth") != ""
|
2022-03-03 11:44:56 +00:00
|
|
|
filters.StartDirectory = r.Form.Get("start_directory")
|
2022-04-25 13:49:11 +00:00
|
|
|
filters.MaxUploadFileSize = maxFileSize
|
|
|
|
filters.ExternalAuthCacheTime, err = strconv.ParseInt(r.Form.Get("external_auth_cache_time"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return filters, fmt.Errorf("invalid external auth cache time: %w", err)
|
|
|
|
}
|
|
|
|
return filters, nil
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getSecretFromFormField(r *http.Request, field string) *kms.Secret {
|
|
|
|
secret := kms.NewPlainSecret(r.Form.Get(field))
|
|
|
|
if strings.TrimSpace(secret.GetPayload()) == redactedSecret {
|
2022-01-06 09:11:47 +00:00
|
|
|
secret.SetStatus(sdkkms.SecretStatusRedacted)
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
if strings.TrimSpace(secret.GetPayload()) == "" {
|
|
|
|
secret.SetStatus("")
|
|
|
|
}
|
|
|
|
return secret
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
2022-02-28 19:19:13 +00:00
|
|
|
config.RoleARN = r.Form.Get("s3_role_arn")
|
2021-05-06 19:35:43 +00:00
|
|
|
config.AccessSecret = getSecretFromFormField(r, "s3_access_secret")
|
|
|
|
config.Endpoint = r.Form.Get("s3_endpoint")
|
|
|
|
config.StorageClass = r.Form.Get("s3_storage_class")
|
2021-11-13 15:05:40 +00:00
|
|
|
config.ACL = r.Form.Get("s3_acl")
|
2021-05-06 19:35:43 +00:00
|
|
|
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 {
|
2022-04-25 13:49:11 +00:00
|
|
|
return config, fmt.Errorf("invalid s3 upload part size: %w", err)
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
2021-07-23 14:56:48 +00:00
|
|
|
config.UploadConcurrency, err = strconv.Atoi(r.Form.Get("s3_upload_concurrency"))
|
2021-07-11 16:39:45 +00:00
|
|
|
if err != nil {
|
2022-04-25 13:49:11 +00:00
|
|
|
return config, fmt.Errorf("invalid s3 upload concurrency: %w", err)
|
2021-07-11 16:39:45 +00:00
|
|
|
}
|
2021-07-23 14:56:48 +00:00
|
|
|
config.DownloadPartSize, err = strconv.ParseInt(r.Form.Get("s3_download_part_size"), 10, 64)
|
|
|
|
if err != nil {
|
2022-04-25 13:49:11 +00:00
|
|
|
return config, fmt.Errorf("invalid s3 download part size: %w", err)
|
2021-07-23 14:56:48 +00:00
|
|
|
}
|
|
|
|
config.DownloadConcurrency, err = strconv.Atoi(r.Form.Get("s3_download_concurrency"))
|
|
|
|
if err != nil {
|
2022-04-25 13:49:11 +00:00
|
|
|
return config, fmt.Errorf("invalid s3 download concurrency: %w", err)
|
2021-07-23 14:56:48 +00:00
|
|
|
}
|
|
|
|
config.ForcePathStyle = r.Form.Get("s3_force_path_style") != ""
|
|
|
|
config.DownloadPartMaxTime, err = strconv.Atoi(r.Form.Get("s3_download_part_max_time"))
|
2022-02-01 11:15:56 +00:00
|
|
|
if err != nil {
|
2022-04-25 13:49:11 +00:00
|
|
|
return config, fmt.Errorf("invalid s3 download part max time: %w", err)
|
2022-02-01 11:15:56 +00:00
|
|
|
}
|
|
|
|
config.UploadPartMaxTime, err = strconv.Atoi(r.Form.Get("s3_upload_part_max_time"))
|
2022-04-25 13:49:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return config, fmt.Errorf("invalid s3 upload part max time: %w", err)
|
|
|
|
}
|
|
|
|
return config, nil
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
2021-11-15 20:57:41 +00:00
|
|
|
config.ACL = r.Form.Get("gcs_acl")
|
2021-05-06 19:35:43 +00:00
|
|
|
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 := io.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
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSFTPConfig(r *http.Request) (vfs.SFTPFsConfig, error) {
|
|
|
|
var err error
|
|
|
|
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")
|
2022-05-30 17:01:12 +00:00
|
|
|
config.KeyPassphrase = getSecretFromFormField(r, "sftp_key_passphrase")
|
2021-05-06 19:35:43 +00:00
|
|
|
fingerprintsFormValue := r.Form.Get("sftp_fingerprints")
|
|
|
|
config.Fingerprints = getSliceFromDelimitedValues(fingerprintsFormValue, "\n")
|
|
|
|
config.Prefix = r.Form.Get("sftp_prefix")
|
2022-06-11 08:41:34 +00:00
|
|
|
config.DisableCouncurrentReads = r.Form.Get("sftp_disable_concurrent_reads") != ""
|
2021-05-06 19:35:43 +00:00
|
|
|
config.BufferSize, err = strconv.ParseInt(r.Form.Get("sftp_buffer_size"), 10, 64)
|
2022-08-15 19:39:04 +00:00
|
|
|
if r.Form.Get("sftp_equality_check_mode") != "" {
|
|
|
|
config.EqualityCheckMode = 1
|
|
|
|
} else {
|
|
|
|
config.EqualityCheckMode = 0
|
|
|
|
}
|
2022-04-25 13:49:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return config, fmt.Errorf("invalid SFTP buffer size: %w", err)
|
|
|
|
}
|
|
|
|
return config, nil
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
2022-06-11 08:41:34 +00:00
|
|
|
func getHTTPFsConfig(r *http.Request) vfs.HTTPFsConfig {
|
|
|
|
config := vfs.HTTPFsConfig{}
|
|
|
|
config.Endpoint = r.Form.Get("http_endpoint")
|
|
|
|
config.Username = r.Form.Get("http_username")
|
|
|
|
config.SkipTLSVerify = r.Form.Get("http_skip_tls_verify") != ""
|
|
|
|
config.Password = getSecretFromFormField(r, "http_password")
|
|
|
|
config.APIKey = getSecretFromFormField(r, "http_api_key")
|
2022-08-15 19:39:04 +00:00
|
|
|
if r.Form.Get("http_equality_check_mode") != "" {
|
|
|
|
config.EqualityCheckMode = 1
|
|
|
|
} else {
|
|
|
|
config.EqualityCheckMode = 0
|
|
|
|
}
|
2022-06-11 08:41:34 +00:00
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
2021-05-06 19:35:43 +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")
|
2021-06-11 20:27:36 +00:00
|
|
|
config.SASURL = getSecretFromFormField(r, "az_sas_url")
|
2021-05-06 19:35:43 +00:00
|
|
|
config.Endpoint = r.Form.Get("az_endpoint")
|
|
|
|
config.KeyPrefix = r.Form.Get("az_key_prefix")
|
|
|
|
config.AccessTier = r.Form.Get("az_access_tier")
|
2022-06-11 08:41:34 +00:00
|
|
|
config.UseEmulator = r.Form.Get("az_use_emulator") != ""
|
2021-05-06 19:35:43 +00:00
|
|
|
config.UploadPartSize, err = strconv.ParseInt(r.Form.Get("az_upload_part_size"), 10, 64)
|
|
|
|
if err != nil {
|
2022-04-25 13:49:11 +00:00
|
|
|
return config, fmt.Errorf("invalid azure upload part size: %w", err)
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
config.UploadConcurrency, err = strconv.Atoi(r.Form.Get("az_upload_concurrency"))
|
2022-02-21 18:01:31 +00:00
|
|
|
if err != nil {
|
2022-04-25 13:49:11 +00:00
|
|
|
return config, fmt.Errorf("invalid azure upload concurrency: %w", err)
|
2022-02-21 18:01:31 +00:00
|
|
|
}
|
|
|
|
config.DownloadPartSize, err = strconv.ParseInt(r.Form.Get("az_download_part_size"), 10, 64)
|
|
|
|
if err != nil {
|
2022-04-25 13:49:11 +00:00
|
|
|
return config, fmt.Errorf("invalid azure download part size: %w", err)
|
2022-02-21 18:01:31 +00:00
|
|
|
}
|
|
|
|
config.DownloadConcurrency, err = strconv.Atoi(r.Form.Get("az_download_concurrency"))
|
2022-04-25 13:49:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return config, fmt.Errorf("invalid azure download concurrency: %w", err)
|
|
|
|
}
|
|
|
|
return config, nil
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getFsConfigFromPostFields(r *http.Request) (vfs.Filesystem, error) {
|
|
|
|
var fs vfs.Filesystem
|
2021-07-11 13:26:51 +00:00
|
|
|
fs.Provider = sdk.GetProviderByName(r.Form.Get("fs_provider"))
|
2021-05-06 19:35:43 +00:00
|
|
|
switch fs.Provider {
|
2021-07-11 13:26:51 +00:00
|
|
|
case sdk.S3FilesystemProvider:
|
2021-05-06 19:35:43 +00:00
|
|
|
config, err := getS3Config(r)
|
|
|
|
if err != nil {
|
|
|
|
return fs, err
|
|
|
|
}
|
|
|
|
fs.S3Config = config
|
2021-07-11 13:26:51 +00:00
|
|
|
case sdk.AzureBlobFilesystemProvider:
|
2021-05-06 19:35:43 +00:00
|
|
|
config, err := getAzureConfig(r)
|
|
|
|
if err != nil {
|
|
|
|
return fs, err
|
|
|
|
}
|
|
|
|
fs.AzBlobConfig = config
|
2021-07-11 13:26:51 +00:00
|
|
|
case sdk.GCSFilesystemProvider:
|
2021-05-06 19:35:43 +00:00
|
|
|
config, err := getGCSConfig(r)
|
|
|
|
if err != nil {
|
|
|
|
return fs, err
|
|
|
|
}
|
|
|
|
fs.GCSConfig = config
|
2021-07-11 13:26:51 +00:00
|
|
|
case sdk.CryptedFilesystemProvider:
|
2021-05-06 19:35:43 +00:00
|
|
|
fs.CryptConfig.Passphrase = getSecretFromFormField(r, "crypt_passphrase")
|
2021-07-11 13:26:51 +00:00
|
|
|
case sdk.SFTPFilesystemProvider:
|
2021-05-06 19:35:43 +00:00
|
|
|
config, err := getSFTPConfig(r)
|
|
|
|
if err != nil {
|
|
|
|
return fs, err
|
|
|
|
}
|
|
|
|
fs.SFTPConfig = config
|
2022-06-11 08:41:34 +00:00
|
|
|
case sdk.HTTPFilesystemProvider:
|
|
|
|
fs.HTTPConfig = getHTTPFsConfig(r)
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
return fs, nil
|
|
|
|
}
|
|
|
|
|
2022-09-21 17:36:08 +00:00
|
|
|
func getAdminHiddenUserPageSections(r *http.Request) int {
|
|
|
|
var result int
|
|
|
|
|
|
|
|
for _, val := range r.Form["user_page_hidden_sections"] {
|
|
|
|
switch val {
|
|
|
|
case "1":
|
|
|
|
result++
|
|
|
|
case "2":
|
|
|
|
result += 2
|
|
|
|
case "3":
|
|
|
|
result += 4
|
|
|
|
case "4":
|
|
|
|
result += 8
|
|
|
|
case "5":
|
|
|
|
result += 16
|
|
|
|
case "6":
|
|
|
|
result += 32
|
|
|
|
case "7":
|
|
|
|
result += 64
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2021-05-06 19:35:43 +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 {
|
2022-07-19 20:25:00 +00:00
|
|
|
return admin, fmt.Errorf("invalid status: %w", err)
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
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"), ",")
|
2022-06-11 08:41:34 +00:00
|
|
|
admin.Filters.AllowAPIKeyAuth = r.Form.Get("allow_api_key_auth") != ""
|
2021-05-06 19:35:43 +00:00
|
|
|
admin.AdditionalInfo = r.Form.Get("additional_info")
|
|
|
|
admin.Description = r.Form.Get("description")
|
2022-09-21 17:36:08 +00:00
|
|
|
admin.Filters.Preferences.HideUserPageSections = getAdminHiddenUserPageSections(r)
|
2022-09-13 16:04:27 +00:00
|
|
|
for k := range r.Form {
|
|
|
|
if strings.HasPrefix(k, "group") {
|
|
|
|
groupName := strings.TrimSpace(r.Form.Get(k))
|
|
|
|
if groupName != "" {
|
|
|
|
idx := strings.TrimPrefix(k, "group")
|
|
|
|
addAsGroupType := r.Form.Get(fmt.Sprintf("add_as_group_type%s", idx))
|
|
|
|
group := dataprovider.AdminGroupMapping{
|
|
|
|
Name: groupName,
|
|
|
|
}
|
|
|
|
switch addAsGroupType {
|
|
|
|
case "1":
|
|
|
|
group.Options.AddToUsersAs = dataprovider.GroupAddToUsersAsPrimary
|
|
|
|
case "2":
|
|
|
|
group.Options.AddToUsersAs = dataprovider.GroupAddToUsersAsSecondary
|
|
|
|
default:
|
|
|
|
group.Options.AddToUsersAs = dataprovider.GroupAddToUsersAsMembership
|
|
|
|
}
|
|
|
|
admin.Groups = append(admin.Groups, group)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-06 19:35:43 +00:00
|
|
|
return admin, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func replacePlaceholders(field string, replacements map[string]string) string {
|
|
|
|
for k, v := range replacements {
|
|
|
|
field = strings.ReplaceAll(field, k, v)
|
|
|
|
}
|
|
|
|
return field
|
|
|
|
}
|
|
|
|
|
|
|
|
func getFolderFromTemplate(folder vfs.BaseVirtualFolder, name string) vfs.BaseVirtualFolder {
|
|
|
|
folder.Name = name
|
|
|
|
replacements := make(map[string]string)
|
|
|
|
replacements["%name%"] = folder.Name
|
|
|
|
|
|
|
|
folder.MappedPath = replacePlaceholders(folder.MappedPath, replacements)
|
|
|
|
folder.Description = replacePlaceholders(folder.Description, replacements)
|
|
|
|
switch folder.FsConfig.Provider {
|
2021-07-11 13:26:51 +00:00
|
|
|
case sdk.CryptedFilesystemProvider:
|
2021-05-06 19:35:43 +00:00
|
|
|
folder.FsConfig.CryptConfig = getCryptFsFromTemplate(folder.FsConfig.CryptConfig, replacements)
|
2021-07-11 13:26:51 +00:00
|
|
|
case sdk.S3FilesystemProvider:
|
2021-05-06 19:35:43 +00:00
|
|
|
folder.FsConfig.S3Config = getS3FsFromTemplate(folder.FsConfig.S3Config, replacements)
|
2021-07-11 13:26:51 +00:00
|
|
|
case sdk.GCSFilesystemProvider:
|
2021-05-06 19:35:43 +00:00
|
|
|
folder.FsConfig.GCSConfig = getGCSFsFromTemplate(folder.FsConfig.GCSConfig, replacements)
|
2021-07-11 13:26:51 +00:00
|
|
|
case sdk.AzureBlobFilesystemProvider:
|
2021-05-06 19:35:43 +00:00
|
|
|
folder.FsConfig.AzBlobConfig = getAzBlobFsFromTemplate(folder.FsConfig.AzBlobConfig, replacements)
|
2021-07-11 13:26:51 +00:00
|
|
|
case sdk.SFTPFilesystemProvider:
|
2021-05-06 19:35:43 +00:00
|
|
|
folder.FsConfig.SFTPConfig = getSFTPFsFromTemplate(folder.FsConfig.SFTPConfig, replacements)
|
2022-06-11 08:41:34 +00:00
|
|
|
case sdk.HTTPFilesystemProvider:
|
|
|
|
folder.FsConfig.HTTPConfig = getHTTPFsFromTemplate(folder.FsConfig.HTTPConfig, replacements)
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return folder
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-06-11 08:41:34 +00:00
|
|
|
func getHTTPFsFromTemplate(fsConfig vfs.HTTPFsConfig, replacements map[string]string) vfs.HTTPFsConfig {
|
|
|
|
fsConfig.Username = replacePlaceholders(fsConfig.Username, replacements)
|
|
|
|
return fsConfig
|
|
|
|
}
|
|
|
|
|
2021-05-06 19:35:43 +00:00
|
|
|
func getUserFromTemplate(user dataprovider.User, template userTemplateFields) dataprovider.User {
|
|
|
|
user.Username = template.Username
|
|
|
|
user.Password = template.Password
|
2022-03-27 14:32:21 +00:00
|
|
|
user.PublicKeys = template.PublicKeys
|
2021-05-06 19:35:43 +00:00
|
|
|
replacements := make(map[string]string)
|
|
|
|
replacements["%username%"] = user.Username
|
2022-03-27 14:32:21 +00:00
|
|
|
if user.Password != "" && !user.IsPasswordHashed() {
|
|
|
|
user.Password = replacePlaceholders(user.Password, replacements)
|
|
|
|
replacements["%password%"] = user.Password
|
|
|
|
}
|
2021-05-06 19:35:43 +00:00
|
|
|
|
|
|
|
user.HomeDir = replacePlaceholders(user.HomeDir, replacements)
|
|
|
|
var vfolders []vfs.VirtualFolder
|
|
|
|
for _, vfolder := range user.VirtualFolders {
|
|
|
|
vfolder.Name = replacePlaceholders(vfolder.Name, replacements)
|
|
|
|
vfolder.VirtualPath = replacePlaceholders(vfolder.VirtualPath, replacements)
|
|
|
|
vfolders = append(vfolders, vfolder)
|
|
|
|
}
|
|
|
|
user.VirtualFolders = vfolders
|
|
|
|
user.Description = replacePlaceholders(user.Description, replacements)
|
|
|
|
user.AdditionalInfo = replacePlaceholders(user.AdditionalInfo, replacements)
|
2022-07-27 16:54:25 +00:00
|
|
|
user.Filters.StartDirectory = replacePlaceholders(user.Filters.StartDirectory, replacements)
|
2021-05-06 19:35:43 +00:00
|
|
|
|
|
|
|
switch user.FsConfig.Provider {
|
2021-07-11 13:26:51 +00:00
|
|
|
case sdk.CryptedFilesystemProvider:
|
2021-05-06 19:35:43 +00:00
|
|
|
user.FsConfig.CryptConfig = getCryptFsFromTemplate(user.FsConfig.CryptConfig, replacements)
|
2021-07-11 13:26:51 +00:00
|
|
|
case sdk.S3FilesystemProvider:
|
2021-05-06 19:35:43 +00:00
|
|
|
user.FsConfig.S3Config = getS3FsFromTemplate(user.FsConfig.S3Config, replacements)
|
2021-07-11 13:26:51 +00:00
|
|
|
case sdk.GCSFilesystemProvider:
|
2021-05-06 19:35:43 +00:00
|
|
|
user.FsConfig.GCSConfig = getGCSFsFromTemplate(user.FsConfig.GCSConfig, replacements)
|
2021-07-11 13:26:51 +00:00
|
|
|
case sdk.AzureBlobFilesystemProvider:
|
2021-05-06 19:35:43 +00:00
|
|
|
user.FsConfig.AzBlobConfig = getAzBlobFsFromTemplate(user.FsConfig.AzBlobConfig, replacements)
|
2021-07-11 13:26:51 +00:00
|
|
|
case sdk.SFTPFilesystemProvider:
|
2021-05-06 19:35:43 +00:00
|
|
|
user.FsConfig.SFTPConfig = getSFTPFsFromTemplate(user.FsConfig.SFTPConfig, replacements)
|
2022-06-11 08:41:34 +00:00
|
|
|
case sdk.HTTPFilesystemProvider:
|
|
|
|
user.FsConfig.HTTPConfig = getHTTPFsFromTemplate(user.FsConfig.HTTPConfig, replacements)
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return user
|
|
|
|
}
|
|
|
|
|
2022-01-30 10:42:36 +00:00
|
|
|
func getTransferLimits(r *http.Request) (int64, int64, int64, error) {
|
|
|
|
dataTransferUL, err := strconv.ParseInt(r.Form.Get("upload_data_transfer"), 10, 64)
|
|
|
|
if err != nil {
|
2022-03-27 14:32:21 +00:00
|
|
|
return 0, 0, 0, fmt.Errorf("invalid upload data transfer: %w", err)
|
2022-01-30 10:42:36 +00:00
|
|
|
}
|
|
|
|
dataTransferDL, err := strconv.ParseInt(r.Form.Get("download_data_transfer"), 10, 64)
|
|
|
|
if err != nil {
|
2022-03-27 14:32:21 +00:00
|
|
|
return 0, 0, 0, fmt.Errorf("invalid download data transfer: %w", err)
|
2022-01-30 10:42:36 +00:00
|
|
|
}
|
|
|
|
dataTransferTotal, err := strconv.ParseInt(r.Form.Get("total_data_transfer"), 10, 64)
|
|
|
|
if err != nil {
|
2022-03-27 14:32:21 +00:00
|
|
|
return 0, 0, 0, fmt.Errorf("invalid total data transfer: %w", err)
|
2022-01-30 10:42:36 +00:00
|
|
|
}
|
|
|
|
return dataTransferUL, dataTransferDL, dataTransferTotal, nil
|
|
|
|
}
|
|
|
|
|
2022-03-27 14:32:21 +00:00
|
|
|
func getQuotaLimits(r *http.Request) (int64, int, error) {
|
2022-09-14 19:18:32 +00:00
|
|
|
quotaSize, err := util.ParseBytes(r.Form.Get("quota_size"))
|
2022-03-27 14:32:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, 0, fmt.Errorf("invalid quota size: %w", err)
|
|
|
|
}
|
|
|
|
quotaFiles, err := strconv.Atoi(r.Form.Get("quota_files"))
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, fmt.Errorf("invalid quota files: %w", err)
|
|
|
|
}
|
|
|
|
return quotaSize, quotaFiles, nil
|
|
|
|
}
|
|
|
|
|
2021-05-06 19:35:43 +00:00
|
|
|
func getUserFromPostFields(r *http.Request) (dataprovider.User, error) {
|
2022-04-25 13:49:11 +00:00
|
|
|
user := dataprovider.User{}
|
2021-05-06 19:35:43 +00:00
|
|
|
err := r.ParseMultipartForm(maxRequestSize)
|
|
|
|
if err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
2021-10-13 07:15:04 +00:00
|
|
|
defer r.MultipartForm.RemoveAll() //nolint:errcheck
|
2021-05-06 19:35:43 +00:00
|
|
|
uid, err := strconv.Atoi(r.Form.Get("uid"))
|
|
|
|
if err != nil {
|
2022-03-27 14:32:21 +00:00
|
|
|
return user, fmt.Errorf("invalid uid: %w", err)
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
gid, err := strconv.Atoi(r.Form.Get("gid"))
|
|
|
|
if err != nil {
|
2022-03-27 14:32:21 +00:00
|
|
|
return user, fmt.Errorf("invalid uid: %w", err)
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
maxSessions, err := strconv.Atoi(r.Form.Get("max_sessions"))
|
|
|
|
if err != nil {
|
2022-03-27 14:32:21 +00:00
|
|
|
return user, fmt.Errorf("invalid max sessions: %w", err)
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
2022-03-27 14:32:21 +00:00
|
|
|
quotaSize, quotaFiles, err := getQuotaLimits(r)
|
2021-05-06 19:35:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
bandwidthUL, err := strconv.ParseInt(r.Form.Get("upload_bandwidth"), 10, 64)
|
|
|
|
if err != nil {
|
2022-03-27 14:32:21 +00:00
|
|
|
return user, fmt.Errorf("invalid upload bandwidth: %w", err)
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
bandwidthDL, err := strconv.ParseInt(r.Form.Get("download_bandwidth"), 10, 64)
|
|
|
|
if err != nil {
|
2022-03-27 14:32:21 +00:00
|
|
|
return user, fmt.Errorf("invalid download bandwidth: %w", err)
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
2022-01-30 10:42:36 +00:00
|
|
|
dataTransferUL, dataTransferDL, dataTransferTotal, err := getTransferLimits(r)
|
|
|
|
if err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
2021-05-06 19:35:43 +00:00
|
|
|
status, err := strconv.Atoi(r.Form.Get("status"))
|
|
|
|
if err != nil {
|
2022-03-27 14:32:21 +00:00
|
|
|
return user, fmt.Errorf("invalid status: %w", err)
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
expirationDateMillis := int64(0)
|
|
|
|
expirationDateString := r.Form.Get("expiration_date")
|
2021-11-06 13:13:20 +00:00
|
|
|
if strings.TrimSpace(expirationDateString) != "" {
|
2021-05-06 19:35:43 +00:00
|
|
|
expirationDate, err := time.Parse(webDateTimeFormat, expirationDateString)
|
|
|
|
if err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
expirationDateMillis = util.GetTimeAsMsSinceEpoch(expirationDate)
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
fsConfig, err := getFsConfigFromPostFields(r)
|
|
|
|
if err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
2021-12-10 17:43:26 +00:00
|
|
|
filters, err := getFiltersFromUserPostFields(r)
|
|
|
|
if err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
2021-05-06 19:35:43 +00:00
|
|
|
user = dataprovider.User{
|
2021-07-11 13:26:51 +00:00
|
|
|
BaseUser: sdk.BaseUser{
|
2022-01-30 10:42:36 +00:00
|
|
|
Username: r.Form.Get("username"),
|
|
|
|
Email: r.Form.Get("email"),
|
|
|
|
Password: r.Form.Get("password"),
|
|
|
|
PublicKeys: r.Form["public_keys"],
|
|
|
|
HomeDir: r.Form.Get("home_dir"),
|
|
|
|
UID: uid,
|
|
|
|
GID: gid,
|
|
|
|
Permissions: getUserPermissionsFromPostFields(r),
|
|
|
|
MaxSessions: maxSessions,
|
|
|
|
QuotaSize: quotaSize,
|
|
|
|
QuotaFiles: quotaFiles,
|
|
|
|
UploadBandwidth: bandwidthUL,
|
|
|
|
DownloadBandwidth: bandwidthDL,
|
|
|
|
UploadDataTransfer: dataTransferUL,
|
|
|
|
DownloadDataTransfer: dataTransferDL,
|
|
|
|
TotalDataTransfer: dataTransferTotal,
|
|
|
|
Status: status,
|
|
|
|
ExpirationDate: expirationDateMillis,
|
|
|
|
AdditionalInfo: r.Form.Get("additional_info"),
|
|
|
|
Description: r.Form.Get("description"),
|
2021-07-11 13:26:51 +00:00
|
|
|
},
|
2022-01-06 09:11:47 +00:00
|
|
|
Filters: dataprovider.UserFilters{
|
|
|
|
BaseUserFilters: filters,
|
|
|
|
},
|
2021-07-11 13:26:51 +00:00
|
|
|
VirtualFolders: getVirtualFoldersFromPostFields(r),
|
|
|
|
FsConfig: fsConfig,
|
2022-04-25 13:49:11 +00:00
|
|
|
Groups: getGroupsFromUserPostFields(r),
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
2022-04-25 13:49:11 +00:00
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getGroupFromPostFields(r *http.Request) (dataprovider.Group, error) {
|
|
|
|
group := dataprovider.Group{}
|
|
|
|
err := r.ParseMultipartForm(maxRequestSize)
|
|
|
|
if err != nil {
|
|
|
|
return group, err
|
|
|
|
}
|
|
|
|
defer r.MultipartForm.RemoveAll() //nolint:errcheck
|
|
|
|
|
|
|
|
maxSessions, err := strconv.Atoi(r.Form.Get("max_sessions"))
|
|
|
|
if err != nil {
|
|
|
|
return group, fmt.Errorf("invalid max sessions: %w", err)
|
|
|
|
}
|
|
|
|
quotaSize, quotaFiles, err := getQuotaLimits(r)
|
|
|
|
if err != nil {
|
|
|
|
return group, err
|
|
|
|
}
|
|
|
|
bandwidthUL, err := strconv.ParseInt(r.Form.Get("upload_bandwidth"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return group, fmt.Errorf("invalid upload bandwidth: %w", err)
|
|
|
|
}
|
|
|
|
bandwidthDL, err := strconv.ParseInt(r.Form.Get("download_bandwidth"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return group, fmt.Errorf("invalid download bandwidth: %w", err)
|
|
|
|
}
|
|
|
|
dataTransferUL, dataTransferDL, dataTransferTotal, err := getTransferLimits(r)
|
2022-03-27 14:32:21 +00:00
|
|
|
if err != nil {
|
2022-04-25 13:49:11 +00:00
|
|
|
return group, err
|
2022-03-27 14:32:21 +00:00
|
|
|
}
|
2022-04-25 13:49:11 +00:00
|
|
|
fsConfig, err := getFsConfigFromPostFields(r)
|
|
|
|
if err != nil {
|
|
|
|
return group, err
|
|
|
|
}
|
|
|
|
filters, err := getFiltersFromUserPostFields(r)
|
|
|
|
if err != nil {
|
|
|
|
return group, err
|
|
|
|
}
|
|
|
|
group = dataprovider.Group{
|
|
|
|
BaseGroup: sdk.BaseGroup{
|
|
|
|
Name: r.Form.Get("name"),
|
|
|
|
Description: r.Form.Get("description"),
|
|
|
|
},
|
|
|
|
UserSettings: dataprovider.GroupUserSettings{
|
|
|
|
BaseGroupUserSettings: sdk.BaseGroupUserSettings{
|
|
|
|
HomeDir: r.Form.Get("home_dir"),
|
|
|
|
MaxSessions: maxSessions,
|
|
|
|
QuotaSize: quotaSize,
|
|
|
|
QuotaFiles: quotaFiles,
|
|
|
|
Permissions: getSubDirPermissionsFromPostFields(r),
|
|
|
|
UploadBandwidth: bandwidthUL,
|
|
|
|
DownloadBandwidth: bandwidthDL,
|
|
|
|
UploadDataTransfer: dataTransferUL,
|
|
|
|
DownloadDataTransfer: dataTransferDL,
|
|
|
|
TotalDataTransfer: dataTransferTotal,
|
|
|
|
Filters: filters,
|
|
|
|
},
|
|
|
|
FsConfig: fsConfig,
|
|
|
|
},
|
|
|
|
VirtualFolders: getVirtualFoldersFromPostFields(r),
|
|
|
|
}
|
|
|
|
return group, nil
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 06:17:36 +00:00
|
|
|
func getKeyValsFromPostFields(r *http.Request, key, val string) []dataprovider.KeyValue {
|
|
|
|
var res []dataprovider.KeyValue
|
|
|
|
for k := range r.Form {
|
|
|
|
if strings.HasPrefix(k, key) {
|
|
|
|
formKey := r.Form.Get(k)
|
|
|
|
idx := strings.TrimPrefix(k, key)
|
|
|
|
formVal := r.Form.Get(fmt.Sprintf("%s%s", val, idx))
|
|
|
|
if formKey != "" && formVal != "" {
|
|
|
|
res = append(res, dataprovider.KeyValue{
|
|
|
|
Key: formKey,
|
|
|
|
Value: formVal,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2022-08-04 19:50:38 +00:00
|
|
|
func getFoldersRetentionFromPostFields(r *http.Request) ([]dataprovider.FolderRetention, error) {
|
|
|
|
var res []dataprovider.FolderRetention
|
|
|
|
for k := range r.Form {
|
|
|
|
if strings.HasPrefix(k, "folder_retention_path") {
|
|
|
|
folderPath := r.Form.Get(k)
|
|
|
|
if folderPath != "" {
|
|
|
|
idx := strings.TrimPrefix(k, "folder_retention_path")
|
|
|
|
retention, err := strconv.Atoi(r.Form.Get(fmt.Sprintf("folder_retention_val%s", idx)))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid retention for path %q: %w", folderPath, err)
|
|
|
|
}
|
|
|
|
options := r.Form[fmt.Sprintf("folder_retention_options%s", idx)]
|
|
|
|
res = append(res, dataprovider.FolderRetention{
|
|
|
|
Path: folderPath,
|
|
|
|
Retention: retention,
|
|
|
|
DeleteEmptyDirs: util.Contains(options, "1"),
|
|
|
|
IgnoreUserPermissions: util.Contains(options, "2"),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2022-09-03 14:29:07 +00:00
|
|
|
func getHTTPPartsFromPostFields(r *http.Request) []dataprovider.HTTPPart {
|
|
|
|
var result []dataprovider.HTTPPart
|
|
|
|
for k := range r.Form {
|
|
|
|
if strings.HasPrefix(k, "http_part_name") {
|
|
|
|
partName := r.Form.Get(k)
|
|
|
|
if partName != "" {
|
|
|
|
idx := strings.TrimPrefix(k, "http_part_name")
|
|
|
|
order, err := strconv.Atoi(idx)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
filePath := r.Form.Get(fmt.Sprintf("http_part_file%s", idx))
|
|
|
|
body := r.Form.Get(fmt.Sprintf("http_part_body%s", idx))
|
|
|
|
concatHeaders := getSliceFromDelimitedValues(r.Form.Get(fmt.Sprintf("http_part_headers%s", idx)), "\n")
|
|
|
|
var headers []dataprovider.KeyValue
|
|
|
|
for _, h := range concatHeaders {
|
|
|
|
values := strings.SplitN(h, ":", 2)
|
|
|
|
if len(values) > 1 {
|
|
|
|
headers = append(headers, dataprovider.KeyValue{
|
|
|
|
Key: strings.TrimSpace(values[0]),
|
|
|
|
Value: strings.TrimSpace(values[1]),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result = append(result, dataprovider.HTTPPart{
|
|
|
|
Name: partName,
|
|
|
|
Filepath: filePath,
|
|
|
|
Headers: headers,
|
|
|
|
Body: body,
|
|
|
|
Order: order,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Slice(result, func(i, j int) bool {
|
|
|
|
return result[i].Order < result[j].Order
|
|
|
|
})
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2022-07-11 06:17:36 +00:00
|
|
|
func getEventActionOptionsFromPostFields(r *http.Request) (dataprovider.BaseEventActionOptions, error) {
|
|
|
|
httpTimeout, err := strconv.Atoi(r.Form.Get("http_timeout"))
|
|
|
|
if err != nil {
|
|
|
|
return dataprovider.BaseEventActionOptions{}, fmt.Errorf("invalid http timeout: %w", err)
|
|
|
|
}
|
|
|
|
cmdTimeout, err := strconv.Atoi(r.Form.Get("cmd_timeout"))
|
|
|
|
if err != nil {
|
|
|
|
return dataprovider.BaseEventActionOptions{}, fmt.Errorf("invalid command timeout: %w", err)
|
|
|
|
}
|
2022-08-04 19:50:38 +00:00
|
|
|
foldersRetention, err := getFoldersRetentionFromPostFields(r)
|
|
|
|
if err != nil {
|
|
|
|
return dataprovider.BaseEventActionOptions{}, err
|
|
|
|
}
|
2022-08-10 16:41:59 +00:00
|
|
|
fsActionType, err := strconv.Atoi(r.Form.Get("fs_action_type"))
|
|
|
|
if err != nil {
|
|
|
|
return dataprovider.BaseEventActionOptions{}, fmt.Errorf("invalid fs action type: %w", err)
|
|
|
|
}
|
2022-08-22 18:17:45 +00:00
|
|
|
var emailAttachments []string
|
|
|
|
if r.Form.Get("email_attachments") != "" {
|
|
|
|
emailAttachments = strings.Split(strings.ReplaceAll(r.Form.Get("email_attachments"), " ", ""), ",")
|
|
|
|
}
|
2022-08-30 10:37:18 +00:00
|
|
|
var cmdArgs []string
|
|
|
|
if r.Form.Get("cmd_arguments") != "" {
|
|
|
|
cmdArgs = strings.Split(strings.ReplaceAll(r.Form.Get("cmd_arguments"), " ", ""), ",")
|
|
|
|
}
|
2022-07-11 06:17:36 +00:00
|
|
|
options := dataprovider.BaseEventActionOptions{
|
|
|
|
HTTPConfig: dataprovider.EventActionHTTPConfig{
|
|
|
|
Endpoint: r.Form.Get("http_endpoint"),
|
|
|
|
Username: r.Form.Get("http_username"),
|
|
|
|
Password: getSecretFromFormField(r, "http_password"),
|
|
|
|
Headers: getKeyValsFromPostFields(r, "http_header_key", "http_header_val"),
|
|
|
|
Timeout: httpTimeout,
|
|
|
|
SkipTLSVerify: r.Form.Get("http_skip_tls_verify") != "",
|
|
|
|
Method: r.Form.Get("http_method"),
|
|
|
|
QueryParameters: getKeyValsFromPostFields(r, "http_query_key", "http_query_val"),
|
|
|
|
Body: r.Form.Get("http_body"),
|
2022-09-03 14:29:07 +00:00
|
|
|
Parts: getHTTPPartsFromPostFields(r),
|
2022-07-11 06:17:36 +00:00
|
|
|
},
|
|
|
|
CmdConfig: dataprovider.EventActionCommandConfig{
|
|
|
|
Cmd: r.Form.Get("cmd_path"),
|
2022-08-30 10:37:18 +00:00
|
|
|
Args: cmdArgs,
|
2022-07-11 06:17:36 +00:00
|
|
|
Timeout: cmdTimeout,
|
|
|
|
EnvVars: getKeyValsFromPostFields(r, "cmd_env_key", "cmd_env_val"),
|
|
|
|
},
|
|
|
|
EmailConfig: dataprovider.EventActionEmailConfig{
|
2022-08-22 17:04:17 +00:00
|
|
|
Recipients: strings.Split(strings.ReplaceAll(r.Form.Get("email_recipients"), " ", ""), ","),
|
|
|
|
Subject: r.Form.Get("email_subject"),
|
|
|
|
Body: r.Form.Get("email_body"),
|
2022-08-22 18:17:45 +00:00
|
|
|
Attachments: emailAttachments,
|
2022-07-11 06:17:36 +00:00
|
|
|
},
|
2022-08-04 19:50:38 +00:00
|
|
|
RetentionConfig: dataprovider.EventActionDataRetentionConfig{
|
|
|
|
Folders: foldersRetention,
|
|
|
|
},
|
2022-08-10 16:41:59 +00:00
|
|
|
FsConfig: dataprovider.EventActionFilesystemConfig{
|
|
|
|
Type: fsActionType,
|
|
|
|
Renames: getKeyValsFromPostFields(r, "fs_rename_source", "fs_rename_target"),
|
|
|
|
Deletes: strings.Split(strings.ReplaceAll(r.Form.Get("fs_delete_paths"), " ", ""), ","),
|
|
|
|
MkDirs: strings.Split(strings.ReplaceAll(r.Form.Get("fs_mkdir_paths"), " ", ""), ","),
|
2022-08-20 12:13:43 +00:00
|
|
|
Exist: strings.Split(strings.ReplaceAll(r.Form.Get("fs_exist_paths"), " ", ""), ","),
|
2022-10-10 16:53:58 +00:00
|
|
|
Compress: dataprovider.EventActionFsCompress{
|
|
|
|
Name: r.Form.Get("fs_compress_name"),
|
|
|
|
Paths: strings.Split(strings.ReplaceAll(r.Form.Get("fs_compress_paths"), " ", ""), ","),
|
|
|
|
},
|
2022-08-10 16:41:59 +00:00
|
|
|
},
|
2022-07-11 06:17:36 +00:00
|
|
|
}
|
|
|
|
return options, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getEventActionFromPostFields(r *http.Request) (dataprovider.BaseEventAction, error) {
|
|
|
|
err := r.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
return dataprovider.BaseEventAction{}, err
|
|
|
|
}
|
|
|
|
actionType, err := strconv.Atoi(r.Form.Get("type"))
|
|
|
|
if err != nil {
|
|
|
|
return dataprovider.BaseEventAction{}, fmt.Errorf("invalid action type: %w", err)
|
|
|
|
}
|
|
|
|
options, err := getEventActionOptionsFromPostFields(r)
|
|
|
|
if err != nil {
|
|
|
|
return dataprovider.BaseEventAction{}, err
|
|
|
|
}
|
|
|
|
action := dataprovider.BaseEventAction{
|
|
|
|
Name: r.Form.Get("name"),
|
|
|
|
Description: r.Form.Get("description"),
|
|
|
|
Type: actionType,
|
|
|
|
Options: options,
|
|
|
|
}
|
|
|
|
return action, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getEventRuleConditionsFromPostFields(r *http.Request) (dataprovider.EventConditions, error) {
|
|
|
|
var schedules []dataprovider.Schedule
|
2022-09-04 15:48:09 +00:00
|
|
|
var names, groupNames, fsPaths []dataprovider.ConditionPattern
|
2022-07-11 06:17:36 +00:00
|
|
|
for k := range r.Form {
|
|
|
|
if strings.HasPrefix(k, "schedule_hour") {
|
|
|
|
hour := r.Form.Get(k)
|
|
|
|
if hour != "" {
|
|
|
|
idx := strings.TrimPrefix(k, "schedule_hour")
|
|
|
|
dayOfWeek := r.Form.Get(fmt.Sprintf("schedule_day_of_week%s", idx))
|
|
|
|
dayOfMonth := r.Form.Get(fmt.Sprintf("schedule_day_of_month%s", idx))
|
|
|
|
month := r.Form.Get(fmt.Sprintf("schedule_month%s", idx))
|
|
|
|
schedules = append(schedules, dataprovider.Schedule{
|
|
|
|
Hours: hour,
|
|
|
|
DayOfWeek: dayOfWeek,
|
|
|
|
DayOfMonth: dayOfMonth,
|
|
|
|
Month: month,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(k, "name_pattern") {
|
|
|
|
pattern := r.Form.Get(k)
|
|
|
|
if pattern != "" {
|
|
|
|
idx := strings.TrimPrefix(k, "name_pattern")
|
|
|
|
patternType := r.Form.Get(fmt.Sprintf("type_name_pattern%s", idx))
|
|
|
|
names = append(names, dataprovider.ConditionPattern{
|
|
|
|
Pattern: pattern,
|
2022-09-04 15:48:09 +00:00
|
|
|
InverseMatch: patternType == inversePatternType,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(k, "group_name_pattern") {
|
|
|
|
pattern := r.Form.Get(k)
|
|
|
|
if pattern != "" {
|
|
|
|
idx := strings.TrimPrefix(k, "group_name_pattern")
|
|
|
|
patternType := r.Form.Get(fmt.Sprintf("type_group_name_pattern%s", idx))
|
|
|
|
groupNames = append(groupNames, dataprovider.ConditionPattern{
|
|
|
|
Pattern: pattern,
|
|
|
|
InverseMatch: patternType == inversePatternType,
|
2022-07-11 06:17:36 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(k, "fs_path_pattern") {
|
|
|
|
pattern := r.Form.Get(k)
|
|
|
|
if pattern != "" {
|
|
|
|
idx := strings.TrimPrefix(k, "fs_path_pattern")
|
|
|
|
patternType := r.Form.Get(fmt.Sprintf("type_fs_path_pattern%s", idx))
|
|
|
|
fsPaths = append(fsPaths, dataprovider.ConditionPattern{
|
|
|
|
Pattern: pattern,
|
2022-09-04 15:48:09 +00:00
|
|
|
InverseMatch: patternType == inversePatternType,
|
2022-07-11 06:17:36 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
minFileSize, err := strconv.ParseInt(r.Form.Get("fs_min_size"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return dataprovider.EventConditions{}, fmt.Errorf("invalid min file size: %w", err)
|
|
|
|
}
|
|
|
|
maxFileSize, err := strconv.ParseInt(r.Form.Get("fs_max_size"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return dataprovider.EventConditions{}, fmt.Errorf("invalid max file size: %w", err)
|
|
|
|
}
|
|
|
|
conditions := dataprovider.EventConditions{
|
|
|
|
FsEvents: r.Form["fs_events"],
|
|
|
|
ProviderEvents: r.Form["provider_events"],
|
|
|
|
Schedules: schedules,
|
|
|
|
Options: dataprovider.ConditionOptions{
|
|
|
|
Names: names,
|
2022-09-04 15:48:09 +00:00
|
|
|
GroupNames: groupNames,
|
2022-07-11 06:17:36 +00:00
|
|
|
FsPaths: fsPaths,
|
|
|
|
Protocols: r.Form["fs_protocols"],
|
|
|
|
ProviderObjects: r.Form["provider_objects"],
|
|
|
|
MinFileSize: minFileSize,
|
|
|
|
MaxFileSize: maxFileSize,
|
|
|
|
ConcurrentExecution: r.Form.Get("concurrent_execution") != "",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return conditions, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getEventRuleActionsFromPostFields(r *http.Request) ([]dataprovider.EventAction, error) {
|
|
|
|
var actions []dataprovider.EventAction
|
|
|
|
for k := range r.Form {
|
|
|
|
if strings.HasPrefix(k, "action_name") {
|
|
|
|
name := r.Form.Get(k)
|
|
|
|
if name != "" {
|
|
|
|
idx := strings.TrimPrefix(k, "action_name")
|
|
|
|
order, err := strconv.Atoi(r.Form.Get(fmt.Sprintf("action_order%s", idx)))
|
|
|
|
if err != nil {
|
|
|
|
return actions, fmt.Errorf("invalid order: %w", err)
|
|
|
|
}
|
|
|
|
options := r.Form[fmt.Sprintf("action_options%s", idx)]
|
|
|
|
actions = append(actions, dataprovider.EventAction{
|
|
|
|
BaseEventAction: dataprovider.BaseEventAction{
|
|
|
|
Name: name,
|
|
|
|
},
|
|
|
|
Order: order + 1,
|
|
|
|
Options: dataprovider.EventActionOptions{
|
|
|
|
IsFailureAction: util.Contains(options, "1"),
|
|
|
|
StopOnFailure: util.Contains(options, "2"),
|
|
|
|
ExecuteSync: util.Contains(options, "3"),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return actions, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getEventRuleFromPostFields(r *http.Request) (dataprovider.EventRule, error) {
|
|
|
|
err := r.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
return dataprovider.EventRule{}, err
|
|
|
|
}
|
|
|
|
trigger, err := strconv.Atoi(r.Form.Get("trigger"))
|
|
|
|
if err != nil {
|
|
|
|
return dataprovider.EventRule{}, fmt.Errorf("invalid trigger: %w", err)
|
|
|
|
}
|
|
|
|
conditions, err := getEventRuleConditionsFromPostFields(r)
|
|
|
|
if err != nil {
|
|
|
|
return dataprovider.EventRule{}, err
|
|
|
|
}
|
|
|
|
actions, err := getEventRuleActionsFromPostFields(r)
|
|
|
|
if err != nil {
|
|
|
|
return dataprovider.EventRule{}, err
|
|
|
|
}
|
|
|
|
rule := dataprovider.EventRule{
|
|
|
|
Name: r.Form.Get("name"),
|
|
|
|
Description: r.Form.Get("description"),
|
|
|
|
Trigger: trigger,
|
|
|
|
Conditions: conditions,
|
|
|
|
Actions: actions,
|
|
|
|
}
|
|
|
|
return rule, nil
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebAdminForgotPwd(w http.ResponseWriter, r *http.Request) {
|
2021-11-13 12:25:43 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
if !smtp.IsEnabled() {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderNotFoundPage(w, r, errors.New("this page does not exist"))
|
2021-11-13 12:25:43 +00:00
|
|
|
return
|
|
|
|
}
|
2022-03-26 07:41:50 +00:00
|
|
|
s.renderForgotPwdPage(w, "", util.GetIPFromRemoteAddress(r.RemoteAddr))
|
2021-11-13 12:25:43 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebAdminForgotPwdPost(w http.ResponseWriter, r *http.Request) {
|
2021-11-13 12:25:43 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2022-03-26 07:41:50 +00:00
|
|
|
|
|
|
|
ipAddr := util.GetIPFromRemoteAddress(r.RemoteAddr)
|
2021-11-13 12:25:43 +00:00
|
|
|
err := r.ParseForm()
|
|
|
|
if err != nil {
|
2022-03-26 07:41:50 +00:00
|
|
|
s.renderForgotPwdPage(w, err.Error(), ipAddr)
|
2021-11-13 12:25:43 +00:00
|
|
|
return
|
|
|
|
}
|
2022-03-26 07:41:50 +00:00
|
|
|
if err := verifyCSRFToken(r.Form.Get(csrfFormToken), ipAddr); err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderForbiddenPage(w, r, err.Error())
|
2021-11-13 12:25:43 +00:00
|
|
|
return
|
|
|
|
}
|
2022-03-19 20:44:27 +00:00
|
|
|
err = handleForgotPassword(r, r.Form.Get("username"), true)
|
2021-11-13 12:25:43 +00:00
|
|
|
if err != nil {
|
|
|
|
if e, ok := err.(*util.ValidationError); ok {
|
2022-03-26 07:41:50 +00:00
|
|
|
s.renderForgotPwdPage(w, e.GetErrorString(), ipAddr)
|
2021-11-13 12:25:43 +00:00
|
|
|
return
|
|
|
|
}
|
2022-03-26 07:41:50 +00:00
|
|
|
s.renderForgotPwdPage(w, err.Error(), ipAddr)
|
2021-11-13 12:25:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
http.Redirect(w, r, webAdminResetPwdPath, http.StatusFound)
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebAdminPasswordReset(w http.ResponseWriter, r *http.Request) {
|
2021-11-13 12:25:43 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxLoginBodySize)
|
|
|
|
if !smtp.IsEnabled() {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderNotFoundPage(w, r, errors.New("this page does not exist"))
|
2021-11-13 12:25:43 +00:00
|
|
|
return
|
|
|
|
}
|
2022-03-26 07:41:50 +00:00
|
|
|
s.renderResetPwdPage(w, "", util.GetIPFromRemoteAddress(r.RemoteAddr))
|
2021-11-13 12:25:43 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebAdminTwoFactor(w http.ResponseWriter, r *http.Request) {
|
2021-09-04 10:11:04 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2022-03-26 07:41:50 +00:00
|
|
|
s.renderTwoFactorPage(w, "", util.GetIPFromRemoteAddress(r.RemoteAddr))
|
2021-09-04 10:11:04 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebAdminTwoFactorRecovery(w http.ResponseWriter, r *http.Request) {
|
2021-09-04 10:11:04 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2022-03-26 07:41:50 +00:00
|
|
|
s.renderTwoFactorRecoveryPage(w, "", util.GetIPFromRemoteAddress(r.RemoteAddr))
|
2021-09-04 10:11:04 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebAdminMFA(w http.ResponseWriter, r *http.Request) {
|
2021-09-04 10:11:04 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderMFAPage(w, r)
|
2021-09-04 10:11:04 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebAdminProfile(w http.ResponseWriter, r *http.Request) {
|
2021-09-29 16:46:15 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderProfilePage(w, r, "")
|
2021-09-29 16:46:15 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebAdminChangePwd(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderChangePasswordPage(w, r, "")
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebAdminProfilePost(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
err := r.ParseForm()
|
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderProfilePage(w, r, err.Error())
|
2021-08-17 16:08:32 +00:00
|
|
|
return
|
|
|
|
}
|
2022-03-26 07:41:50 +00:00
|
|
|
ipAddr := util.GetIPFromRemoteAddress(r.RemoteAddr)
|
|
|
|
if err := verifyCSRFToken(r.Form.Get(csrfFormToken), ipAddr); err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderForbiddenPage(w, r, err.Error())
|
2021-08-17 16:08:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderProfilePage(w, r, "Invalid token claims")
|
2021-08-17 16:08:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
admin, err := dataprovider.AdminExists(claims.Username)
|
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderProfilePage(w, r, err.Error())
|
2021-08-17 16:08:32 +00:00
|
|
|
return
|
|
|
|
}
|
2022-06-11 08:41:34 +00:00
|
|
|
admin.Filters.AllowAPIKeyAuth = r.Form.Get("allow_api_key_auth") != ""
|
2021-09-29 16:46:15 +00:00
|
|
|
admin.Email = r.Form.Get("email")
|
|
|
|
admin.Description = r.Form.Get("description")
|
2022-03-26 07:41:50 +00:00
|
|
|
err = dataprovider.UpdateAdmin(&admin, dataprovider.ActionExecutorSelf, ipAddr)
|
2021-08-17 16:08:32 +00:00
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderProfilePage(w, r, err.Error())
|
2021-08-17 16:08:32 +00:00
|
|
|
return
|
|
|
|
}
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderMessagePage(w, r, "Profile updated", "", http.StatusOK, nil,
|
2021-09-29 16:46:15 +00:00
|
|
|
"Your profile has been successfully updated")
|
2021-08-17 16:08:32 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebMaintenance(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderMaintenancePage(w, r, "")
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebRestore(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, MaxRestoreSize)
|
2021-10-10 11:08:05 +00:00
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderBadRequestPage(w, r, errors.New("invalid token claims"))
|
2021-10-10 11:08:05 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
err = r.ParseMultipartForm(MaxRestoreSize)
|
2021-05-06 19:35:43 +00:00
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderMaintenancePage(w, r, err.Error())
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
2021-10-13 07:15:04 +00:00
|
|
|
defer r.MultipartForm.RemoveAll() //nolint:errcheck
|
2022-03-26 07:41:50 +00:00
|
|
|
|
|
|
|
ipAddr := util.GetIPFromRemoteAddress(r.RemoteAddr)
|
|
|
|
if err := verifyCSRFToken(r.Form.Get(csrfFormToken), ipAddr); err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderForbiddenPage(w, r, err.Error())
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
restoreMode, err := strconv.Atoi(r.Form.Get("mode"))
|
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderMaintenancePage(w, r, err.Error())
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
scanQuota, err := strconv.Atoi(r.Form.Get("quota"))
|
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderMaintenancePage(w, r, err.Error())
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
backupFile, _, err := r.FormFile("backup_file")
|
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderMaintenancePage(w, r, err.Error())
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer backupFile.Close()
|
|
|
|
|
|
|
|
backupContent, err := io.ReadAll(backupFile)
|
|
|
|
if err != nil || len(backupContent) == 0 {
|
|
|
|
if len(backupContent) == 0 {
|
|
|
|
err = errors.New("backup file size must be greater than 0")
|
|
|
|
}
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderMaintenancePage(w, r, err.Error())
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-26 07:41:50 +00:00
|
|
|
if err := restoreBackup(backupContent, "", scanQuota, restoreMode, claims.Username, ipAddr); err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderMaintenancePage(w, r, err.Error())
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderMessagePage(w, r, "Data restored", "", http.StatusOK, nil, "Your backup was successfully restored")
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleGetWebAdmins(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2021-05-06 19:35:43 +00:00
|
|
|
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 {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderInternalServerErrorPage(w, r, err)
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
admins = append(admins, a...)
|
|
|
|
if len(a) < limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
data := adminsPage{
|
2022-03-19 20:44:27 +00:00
|
|
|
basePage: s.getBasePageData(pageAdminsTitle, webAdminsPath, r),
|
2021-05-06 19:35:43 +00:00
|
|
|
Admins: admins,
|
|
|
|
}
|
|
|
|
renderAdminTemplate(w, templateAdmins, data)
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebAdminSetupGet(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxLoginBodySize)
|
2021-05-14 17:21:15 +00:00
|
|
|
if dataprovider.HasAdmin() {
|
2022-02-13 13:30:20 +00:00
|
|
|
http.Redirect(w, r, webAdminLoginPath, http.StatusFound)
|
2021-05-14 17:21:15 +00:00
|
|
|
return
|
|
|
|
}
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderAdminSetupPage(w, r, "", "")
|
2021-05-14 17:21:15 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebAddAdminGet(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2022-05-22 17:19:14 +00:00
|
|
|
admin := &dataprovider.Admin{
|
|
|
|
Status: 1,
|
|
|
|
Permissions: []string{dataprovider.PermAdminAny},
|
|
|
|
}
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderAddUpdateAdminPage(w, r, admin, "", true)
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebUpdateAdminGet(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2021-05-06 19:35:43 +00:00
|
|
|
username := getURLParam(r, "username")
|
|
|
|
admin, err := dataprovider.AdminExists(username)
|
|
|
|
if err == nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderAddUpdateAdminPage(w, r, &admin, "", false)
|
2021-07-11 13:26:51 +00:00
|
|
|
} else if _, ok := err.(*util.RecordNotFoundError); ok {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderNotFoundPage(w, r, err)
|
2021-05-06 19:35:43 +00:00
|
|
|
} else {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderInternalServerErrorPage(w, r, err)
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebAddAdminPost(w http.ResponseWriter, r *http.Request) {
|
2021-05-06 19:35:43 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2021-10-10 11:08:05 +00:00
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderBadRequestPage(w, r, errors.New("invalid token claims"))
|
2021-10-10 11:08:05 +00:00
|
|
|
return
|
|
|
|
}
|
2021-05-06 19:35:43 +00:00
|
|
|
admin, err := getAdminFromPostFields(r)
|
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderAddUpdateAdminPage(w, r, &admin, err.Error(), true)
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
2022-07-19 20:25:00 +00:00
|
|
|
if admin.Password == "" && s.binding.isWebAdminLoginFormDisabled() {
|
|
|
|
admin.Password = util.GenerateUniqueID()
|
|
|
|
}
|
2022-03-26 07:41:50 +00:00
|
|
|
ipAddr := util.GetIPFromRemoteAddress(r.RemoteAddr)
|
|
|
|
if err := verifyCSRFToken(r.Form.Get(csrfFormToken), ipAddr); err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderForbiddenPage(w, r, err.Error())
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
2022-03-26 07:41:50 +00:00
|
|
|
err = dataprovider.AddAdmin(&admin, claims.Username, ipAddr)
|
2021-05-06 19:35:43 +00:00
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderAddUpdateAdminPage(w, r, &admin, err.Error(), true)
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
http.Redirect(w, r, webAdminsPath, http.StatusSeeOther)
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebUpdateAdminPost(w http.ResponseWriter, r *http.Request) {
|
2021-05-06 19:35:43 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
|
|
|
|
username := getURLParam(r, "username")
|
|
|
|
admin, err := dataprovider.AdminExists(username)
|
2021-07-11 13:26:51 +00:00
|
|
|
if _, ok := err.(*util.RecordNotFoundError); ok {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderNotFoundPage(w, r, err)
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
} else if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderInternalServerErrorPage(w, r, err)
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
updatedAdmin, err := getAdminFromPostFields(r)
|
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderAddUpdateAdminPage(w, r, &updatedAdmin, err.Error(), false)
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
2022-03-26 07:41:50 +00:00
|
|
|
ipAddr := util.GetIPFromRemoteAddress(r.RemoteAddr)
|
|
|
|
if err := verifyCSRFToken(r.Form.Get(csrfFormToken), ipAddr); err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderForbiddenPage(w, r, err.Error())
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
updatedAdmin.ID = admin.ID
|
|
|
|
updatedAdmin.Username = admin.Username
|
|
|
|
if updatedAdmin.Password == "" {
|
|
|
|
updatedAdmin.Password = admin.Password
|
|
|
|
}
|
2021-09-04 10:11:04 +00:00
|
|
|
updatedAdmin.Filters.TOTPConfig = admin.Filters.TOTPConfig
|
|
|
|
updatedAdmin.Filters.RecoveryCodes = admin.Filters.RecoveryCodes
|
2021-05-06 19:35:43 +00:00
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderAddUpdateAdminPage(w, r, &updatedAdmin, "Invalid token claims", false)
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if username == claims.Username {
|
|
|
|
if claims.isCriticalPermRemoved(updatedAdmin.Permissions) {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderAddUpdateAdminPage(w, r, &updatedAdmin, "You cannot remove these permissions to yourself", false)
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if updatedAdmin.Status == 0 {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderAddUpdateAdminPage(w, r, &updatedAdmin, "You cannot disable yourself", false)
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2022-03-26 07:41:50 +00:00
|
|
|
err = dataprovider.UpdateAdmin(&updatedAdmin, claims.Username, ipAddr)
|
2021-05-06 19:35:43 +00:00
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderAddUpdateAdminPage(w, r, &admin, err.Error(), false)
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
http.Redirect(w, r, webAdminsPath, http.StatusSeeOther)
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebDefenderPage(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2021-06-08 11:24:28 +00:00
|
|
|
data := defenderHostsPage{
|
2022-03-19 20:44:27 +00:00
|
|
|
basePage: s.getBasePageData(pageDefenderTitle, webDefenderPath, r),
|
2021-06-08 11:24:28 +00:00
|
|
|
DefenderHostsURL: webDefenderHostsPath,
|
|
|
|
}
|
|
|
|
|
|
|
|
renderAdminTemplate(w, templateDefender, data)
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleGetWebUsers(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2022-03-19 20:44:27 +00:00
|
|
|
var limit int
|
2021-05-06 19:35:43 +00:00
|
|
|
if _, ok := r.URL.Query()["qlimit"]; ok {
|
|
|
|
var err error
|
|
|
|
limit, err = strconv.Atoi(r.URL.Query().Get("qlimit"))
|
|
|
|
if err != nil {
|
|
|
|
limit = defaultQueryLimit
|
|
|
|
}
|
2022-03-19 20:44:27 +00:00
|
|
|
} else {
|
|
|
|
limit = defaultQueryLimit
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
users := make([]dataprovider.User, 0, limit)
|
|
|
|
for {
|
|
|
|
u, err := dataprovider.GetUsers(limit, len(users), dataprovider.OrderASC)
|
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderInternalServerErrorPage(w, r, err)
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
users = append(users, u...)
|
|
|
|
if len(u) < limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
data := usersPage{
|
2022-03-19 20:44:27 +00:00
|
|
|
basePage: s.getBasePageData(pageUsersTitle, webUsersPath, r),
|
2021-05-06 19:35:43 +00:00
|
|
|
Users: users,
|
|
|
|
}
|
|
|
|
renderAdminTemplate(w, templateUsers, data)
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebTemplateFolderGet(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2021-05-06 19:35:43 +00:00
|
|
|
if r.URL.Query().Get("from") != "" {
|
|
|
|
name := r.URL.Query().Get("from")
|
|
|
|
folder, err := dataprovider.GetFolderByName(name)
|
|
|
|
if err == nil {
|
2022-01-12 18:01:19 +00:00
|
|
|
folder.FsConfig.SetEmptySecrets()
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderFolderPage(w, r, folder, folderPageModeTemplate, "")
|
2021-07-11 13:26:51 +00:00
|
|
|
} else if _, ok := err.(*util.RecordNotFoundError); ok {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderNotFoundPage(w, r, err)
|
2021-05-06 19:35:43 +00:00
|
|
|
} else {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderInternalServerErrorPage(w, r, err)
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
folder := vfs.BaseVirtualFolder{}
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderFolderPage(w, r, folder, folderPageModeTemplate, "")
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebTemplateFolderPost(w http.ResponseWriter, r *http.Request) {
|
2021-05-06 19:35:43 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2022-01-12 18:01:19 +00:00
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderBadRequestPage(w, r, errors.New("invalid token claims"))
|
2022-01-12 18:01:19 +00:00
|
|
|
return
|
|
|
|
}
|
2021-05-06 19:35:43 +00:00
|
|
|
templateFolder := vfs.BaseVirtualFolder{}
|
2022-01-12 18:01:19 +00:00
|
|
|
err = r.ParseMultipartForm(maxRequestSize)
|
2021-05-06 19:35:43 +00:00
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderMessagePage(w, r, "Error parsing folders fields", "", http.StatusBadRequest, err, "")
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
2021-10-13 07:15:04 +00:00
|
|
|
defer r.MultipartForm.RemoveAll() //nolint:errcheck
|
2021-05-06 19:35:43 +00:00
|
|
|
|
2022-03-26 07:41:50 +00:00
|
|
|
ipAddr := util.GetIPFromRemoteAddress(r.RemoteAddr)
|
|
|
|
if err := verifyCSRFToken(r.Form.Get(csrfFormToken), ipAddr); err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderForbiddenPage(w, r, err.Error())
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
templateFolder.MappedPath = r.Form.Get("mapped_path")
|
|
|
|
templateFolder.Description = r.Form.Get("description")
|
|
|
|
fsConfig, err := getFsConfigFromPostFields(r)
|
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderMessagePage(w, r, "Error parsing folders fields", "", http.StatusBadRequest, err, "")
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
templateFolder.FsConfig = fsConfig
|
|
|
|
|
|
|
|
var dump dataprovider.BackupData
|
|
|
|
dump.Version = dataprovider.DumpVersion
|
|
|
|
|
|
|
|
foldersFields := getFoldersForTemplate(r)
|
|
|
|
for _, tmpl := range foldersFields {
|
|
|
|
f := getFolderFromTemplate(templateFolder, tmpl)
|
|
|
|
if err := dataprovider.ValidateFolder(&f); err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderMessagePage(w, r, "Folder validation error", fmt.Sprintf("Error validating folder %#v", f.Name),
|
2022-01-12 18:01:19 +00:00
|
|
|
http.StatusBadRequest, err, "")
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
dump.Folders = append(dump.Folders, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(dump.Folders) == 0 {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderMessagePage(w, r, "No folders defined", "No valid folders defined, unable to complete the requested action",
|
2022-01-12 18:01:19 +00:00
|
|
|
http.StatusBadRequest, nil, "")
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
2022-01-12 18:01:19 +00:00
|
|
|
if r.Form.Get("form_action") == "export_from_template" {
|
|
|
|
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"sftpgo-%v-folders-from-template.json\"",
|
|
|
|
len(dump.Folders)))
|
|
|
|
render.JSON(w, r, dump)
|
|
|
|
return
|
|
|
|
}
|
2022-03-26 07:41:50 +00:00
|
|
|
if err = RestoreFolders(dump.Folders, "", 1, 0, claims.Username, ipAddr); err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderMessagePage(w, r, "Unable to save folders", "Cannot save the defined folders:",
|
2022-01-12 18:01:19 +00:00
|
|
|
getRespStatus(err), err, "")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
http.Redirect(w, r, webFoldersPath, http.StatusSeeOther)
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebTemplateUserGet(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2021-05-06 19:35:43 +00:00
|
|
|
if r.URL.Query().Get("from") != "" {
|
|
|
|
username := r.URL.Query().Get("from")
|
|
|
|
user, err := dataprovider.UserExists(username)
|
|
|
|
if err == nil {
|
|
|
|
user.SetEmptySecrets()
|
2022-01-12 18:01:19 +00:00
|
|
|
user.PublicKeys = nil
|
2021-09-25 17:06:13 +00:00
|
|
|
user.Email = ""
|
|
|
|
user.Description = ""
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderUserPage(w, r, &user, userPageModeTemplate, "")
|
2021-07-11 13:26:51 +00:00
|
|
|
} else if _, ok := err.(*util.RecordNotFoundError); ok {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderNotFoundPage(w, r, err)
|
2021-05-06 19:35:43 +00:00
|
|
|
} else {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderInternalServerErrorPage(w, r, err)
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-01-12 18:01:19 +00:00
|
|
|
user := dataprovider.User{BaseUser: sdk.BaseUser{
|
|
|
|
Status: 1,
|
|
|
|
Permissions: map[string][]string{
|
|
|
|
"/": {dataprovider.PermAny},
|
|
|
|
},
|
|
|
|
}}
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderUserPage(w, r, &user, userPageModeTemplate, "")
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebTemplateUserPost(w http.ResponseWriter, r *http.Request) {
|
2021-05-06 19:35:43 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2022-01-12 18:01:19 +00:00
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderBadRequestPage(w, r, errors.New("invalid token claims"))
|
2022-01-12 18:01:19 +00:00
|
|
|
return
|
|
|
|
}
|
2021-05-06 19:35:43 +00:00
|
|
|
templateUser, err := getUserFromPostFields(r)
|
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderMessagePage(w, r, "Error parsing user fields", "", http.StatusBadRequest, err, "")
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
2022-03-26 07:41:50 +00:00
|
|
|
ipAddr := util.GetIPFromRemoteAddress(r.RemoteAddr)
|
|
|
|
if err := verifyCSRFToken(r.Form.Get(csrfFormToken), ipAddr); err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderForbiddenPage(w, r, err.Error())
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderMessagePage(w, r, "User validation error", fmt.Sprintf("Error validating user %#v", u.Username),
|
2022-01-12 18:01:19 +00:00
|
|
|
http.StatusBadRequest, err, "")
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
dump.Users = append(dump.Users, u)
|
|
|
|
for _, folder := range u.VirtualFolders {
|
|
|
|
if !dump.HasFolder(folder.Name) {
|
|
|
|
dump.Folders = append(dump.Folders, folder.BaseVirtualFolder)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(dump.Users) == 0 {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderMessagePage(w, r, "No users defined", "No valid users defined, unable to complete the requested action",
|
2022-01-12 18:01:19 +00:00
|
|
|
http.StatusBadRequest, nil, "")
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
2022-01-12 18:01:19 +00:00
|
|
|
if r.Form.Get("form_action") == "export_from_template" {
|
|
|
|
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"sftpgo-%v-users-from-template.json\"",
|
|
|
|
len(dump.Users)))
|
|
|
|
render.JSON(w, r, dump)
|
|
|
|
return
|
|
|
|
}
|
2022-03-26 07:41:50 +00:00
|
|
|
if err = RestoreUsers(dump.Users, "", 1, 0, claims.Username, ipAddr); err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderMessagePage(w, r, "Unable to save users", "Cannot save the defined users:",
|
2022-01-12 18:01:19 +00:00
|
|
|
getRespStatus(err), err, "")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
http.Redirect(w, r, webUsersPath, http.StatusSeeOther)
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebAddUserGet(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2022-01-12 18:01:19 +00:00
|
|
|
user := dataprovider.User{BaseUser: sdk.BaseUser{
|
|
|
|
Status: 1,
|
|
|
|
Permissions: map[string][]string{
|
|
|
|
"/": {dataprovider.PermAny},
|
2022-08-28 12:41:42 +00:00
|
|
|
}},
|
|
|
|
}
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderUserPage(w, r, &user, userPageModeAdd, "")
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebUpdateUserGet(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2021-05-06 19:35:43 +00:00
|
|
|
username := getURLParam(r, "username")
|
|
|
|
user, err := dataprovider.UserExists(username)
|
|
|
|
if err == nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderUserPage(w, r, &user, userPageModeUpdate, "")
|
2021-07-11 13:26:51 +00:00
|
|
|
} else if _, ok := err.(*util.RecordNotFoundError); ok {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderNotFoundPage(w, r, err)
|
2021-05-06 19:35:43 +00:00
|
|
|
} else {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderInternalServerErrorPage(w, r, err)
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebAddUserPost(w http.ResponseWriter, r *http.Request) {
|
2021-05-06 19:35:43 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2021-10-10 11:08:05 +00:00
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderBadRequestPage(w, r, errors.New("invalid token claims"))
|
2021-10-10 11:08:05 +00:00
|
|
|
return
|
|
|
|
}
|
2021-05-06 19:35:43 +00:00
|
|
|
user, err := getUserFromPostFields(r)
|
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderUserPage(w, r, &user, userPageModeAdd, err.Error())
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
2022-03-26 07:41:50 +00:00
|
|
|
ipAddr := util.GetIPFromRemoteAddress(r.RemoteAddr)
|
|
|
|
if err := verifyCSRFToken(r.Form.Get(csrfFormToken), ipAddr); err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderForbiddenPage(w, r, err.Error())
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
2022-03-27 14:32:21 +00:00
|
|
|
user = getUserFromTemplate(user, userTemplateFields{
|
|
|
|
Username: user.Username,
|
|
|
|
Password: user.Password,
|
|
|
|
PublicKeys: user.PublicKeys,
|
|
|
|
})
|
2022-03-26 07:41:50 +00:00
|
|
|
err = dataprovider.AddUser(&user, claims.Username, ipAddr)
|
2022-04-25 13:49:11 +00:00
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderUserPage(w, r, &user, userPageModeAdd, err.Error())
|
2022-04-25 13:49:11 +00:00
|
|
|
return
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
2022-04-25 13:49:11 +00:00
|
|
|
http.Redirect(w, r, webUsersPath, http.StatusSeeOther)
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebUpdateUserPost(w http.ResponseWriter, r *http.Request) {
|
2021-05-06 19:35:43 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2021-10-10 11:08:05 +00:00
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderBadRequestPage(w, r, errors.New("invalid token claims"))
|
2021-10-10 11:08:05 +00:00
|
|
|
return
|
|
|
|
}
|
2021-05-06 19:35:43 +00:00
|
|
|
username := getURLParam(r, "username")
|
|
|
|
user, err := dataprovider.UserExists(username)
|
2021-07-11 13:26:51 +00:00
|
|
|
if _, ok := err.(*util.RecordNotFoundError); ok {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderNotFoundPage(w, r, err)
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
} else if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderInternalServerErrorPage(w, r, err)
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
updatedUser, err := getUserFromPostFields(r)
|
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderUserPage(w, r, &user, userPageModeUpdate, err.Error())
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
2022-03-26 07:41:50 +00:00
|
|
|
ipAddr := util.GetIPFromRemoteAddress(r.RemoteAddr)
|
|
|
|
if err := verifyCSRFToken(r.Form.Get(csrfFormToken), ipAddr); err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderForbiddenPage(w, r, err.Error())
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
updatedUser.ID = user.ID
|
|
|
|
updatedUser.Username = user.Username
|
2021-09-04 10:11:04 +00:00
|
|
|
updatedUser.Filters.RecoveryCodes = user.Filters.RecoveryCodes
|
|
|
|
updatedUser.Filters.TOTPConfig = user.Filters.TOTPConfig
|
2021-05-06 19:35:43 +00:00
|
|
|
updatedUser.SetEmptySecretsIfNil()
|
|
|
|
if updatedUser.Password == redactedSecret {
|
|
|
|
updatedUser.Password = user.Password
|
|
|
|
}
|
|
|
|
updateEncryptedSecrets(&updatedUser.FsConfig, user.FsConfig.S3Config.AccessSecret, user.FsConfig.AzBlobConfig.AccountKey,
|
2021-06-11 20:27:36 +00:00
|
|
|
user.FsConfig.AzBlobConfig.SASURL, user.FsConfig.GCSConfig.Credentials, user.FsConfig.CryptConfig.Passphrase,
|
2022-06-11 08:41:34 +00:00
|
|
|
user.FsConfig.SFTPConfig.Password, user.FsConfig.SFTPConfig.PrivateKey, user.FsConfig.SFTPConfig.KeyPassphrase,
|
|
|
|
user.FsConfig.HTTPConfig.Password, user.FsConfig.HTTPConfig.APIKey)
|
2021-05-06 19:35:43 +00:00
|
|
|
|
2022-03-27 14:32:21 +00:00
|
|
|
updatedUser = getUserFromTemplate(updatedUser, userTemplateFields{
|
|
|
|
Username: updatedUser.Username,
|
|
|
|
Password: updatedUser.Password,
|
|
|
|
PublicKeys: updatedUser.PublicKeys,
|
|
|
|
})
|
|
|
|
|
2022-03-26 07:41:50 +00:00
|
|
|
err = dataprovider.UpdateUser(&updatedUser, claims.Username, ipAddr)
|
2022-04-25 13:49:11 +00:00
|
|
|
if err != nil {
|
|
|
|
s.renderUserPage(w, r, &updatedUser, userPageModeUpdate, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if r.Form.Get("disconnect") != "" {
|
|
|
|
disconnectUser(user.Username)
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
2022-04-25 13:49:11 +00:00
|
|
|
http.Redirect(w, r, webUsersPath, http.StatusSeeOther)
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebGetStatus(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2021-05-06 19:35:43 +00:00
|
|
|
data := statusPage{
|
2022-03-19 20:44:27 +00:00
|
|
|
basePage: s.getBasePageData(pageStatusTitle, webStatusPath, r),
|
2021-05-06 19:35:43 +00:00
|
|
|
Status: getServicesStatus(),
|
|
|
|
}
|
|
|
|
renderAdminTemplate(w, templateStatus, data)
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebGetConnections(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2022-10-02 07:51:47 +00:00
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
|
|
|
s.renderBadRequestPage(w, r, errors.New("invalid token claims"))
|
|
|
|
return
|
|
|
|
}
|
2021-05-06 19:35:43 +00:00
|
|
|
connectionStats := common.Connections.GetStats()
|
2022-10-02 07:51:47 +00:00
|
|
|
connectionStats = append(connectionStats, getNodesConnections(claims.Username)...)
|
2021-05-06 19:35:43 +00:00
|
|
|
data := connectionsPage{
|
2022-03-19 20:44:27 +00:00
|
|
|
basePage: s.getBasePageData(pageConnectionsTitle, webConnectionsPath, r),
|
2021-05-06 19:35:43 +00:00
|
|
|
Connections: connectionStats,
|
|
|
|
}
|
|
|
|
renderAdminTemplate(w, templateConnections, data)
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebAddFolderGet(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderFolderPage(w, r, vfs.BaseVirtualFolder{}, folderPageModeAdd, "")
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebAddFolderPost(w http.ResponseWriter, r *http.Request) {
|
2021-05-06 19:35:43 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2022-07-24 06:10:23 +00:00
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
|
|
|
s.renderBadRequestPage(w, r, errors.New("invalid token claims"))
|
|
|
|
return
|
|
|
|
}
|
2021-05-06 19:35:43 +00:00
|
|
|
folder := vfs.BaseVirtualFolder{}
|
2022-07-24 06:10:23 +00:00
|
|
|
err = r.ParseMultipartForm(maxRequestSize)
|
2021-05-06 19:35:43 +00:00
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderFolderPage(w, r, folder, folderPageModeAdd, err.Error())
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
2021-10-13 07:15:04 +00:00
|
|
|
defer r.MultipartForm.RemoveAll() //nolint:errcheck
|
|
|
|
|
2022-03-26 07:41:50 +00:00
|
|
|
ipAddr := util.GetIPFromRemoteAddress(r.RemoteAddr)
|
|
|
|
if err := verifyCSRFToken(r.Form.Get(csrfFormToken), ipAddr); err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderForbiddenPage(w, r, err.Error())
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
folder.MappedPath = r.Form.Get("mapped_path")
|
|
|
|
folder.Name = r.Form.Get("name")
|
|
|
|
folder.Description = r.Form.Get("description")
|
|
|
|
fsConfig, err := getFsConfigFromPostFields(r)
|
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderFolderPage(w, r, folder, folderPageModeAdd, err.Error())
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
folder.FsConfig = fsConfig
|
2022-03-27 14:32:21 +00:00
|
|
|
folder = getFolderFromTemplate(folder, folder.Name)
|
2021-05-06 19:35:43 +00:00
|
|
|
|
2022-07-24 06:10:23 +00:00
|
|
|
err = dataprovider.AddFolder(&folder, claims.Username, ipAddr)
|
2021-05-06 19:35:43 +00:00
|
|
|
if err == nil {
|
|
|
|
http.Redirect(w, r, webFoldersPath, http.StatusSeeOther)
|
|
|
|
} else {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderFolderPage(w, r, folder, folderPageModeAdd, err.Error())
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebUpdateFolderGet(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2021-05-06 19:35:43 +00:00
|
|
|
name := getURLParam(r, "name")
|
|
|
|
folder, err := dataprovider.GetFolderByName(name)
|
|
|
|
if err == nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderFolderPage(w, r, folder, folderPageModeUpdate, "")
|
2021-07-11 13:26:51 +00:00
|
|
|
} else if _, ok := err.(*util.RecordNotFoundError); ok {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderNotFoundPage(w, r, err)
|
2021-05-06 19:35:43 +00:00
|
|
|
} else {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderInternalServerErrorPage(w, r, err)
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebUpdateFolderPost(w http.ResponseWriter, r *http.Request) {
|
2021-05-06 19:35:43 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2021-10-10 11:08:05 +00:00
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderBadRequestPage(w, r, errors.New("invalid token claims"))
|
2021-10-10 11:08:05 +00:00
|
|
|
return
|
|
|
|
}
|
2021-05-06 19:35:43 +00:00
|
|
|
name := getURLParam(r, "name")
|
|
|
|
folder, err := dataprovider.GetFolderByName(name)
|
2021-07-11 13:26:51 +00:00
|
|
|
if _, ok := err.(*util.RecordNotFoundError); ok {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderNotFoundPage(w, r, err)
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
} else if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderInternalServerErrorPage(w, r, err)
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = r.ParseMultipartForm(maxRequestSize)
|
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderFolderPage(w, r, folder, folderPageModeUpdate, err.Error())
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
2021-10-13 07:15:04 +00:00
|
|
|
defer r.MultipartForm.RemoveAll() //nolint:errcheck
|
|
|
|
|
2022-03-26 07:41:50 +00:00
|
|
|
ipAddr := util.GetIPFromRemoteAddress(r.RemoteAddr)
|
|
|
|
if err := verifyCSRFToken(r.Form.Get(csrfFormToken), ipAddr); err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderForbiddenPage(w, r, err.Error())
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
fsConfig, err := getFsConfigFromPostFields(r)
|
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderFolderPage(w, r, folder, folderPageModeUpdate, err.Error())
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
2022-03-27 14:32:21 +00:00
|
|
|
updatedFolder := vfs.BaseVirtualFolder{
|
2021-05-06 19:35:43 +00:00
|
|
|
MappedPath: r.Form.Get("mapped_path"),
|
|
|
|
Description: r.Form.Get("description"),
|
|
|
|
}
|
|
|
|
updatedFolder.ID = folder.ID
|
|
|
|
updatedFolder.Name = folder.Name
|
|
|
|
updatedFolder.FsConfig = fsConfig
|
|
|
|
updatedFolder.FsConfig.SetEmptySecretsIfNil()
|
|
|
|
updateEncryptedSecrets(&updatedFolder.FsConfig, folder.FsConfig.S3Config.AccessSecret, folder.FsConfig.AzBlobConfig.AccountKey,
|
2021-06-11 20:27:36 +00:00
|
|
|
folder.FsConfig.AzBlobConfig.SASURL, folder.FsConfig.GCSConfig.Credentials, folder.FsConfig.CryptConfig.Passphrase,
|
2022-06-11 08:41:34 +00:00
|
|
|
folder.FsConfig.SFTPConfig.Password, folder.FsConfig.SFTPConfig.PrivateKey, folder.FsConfig.SFTPConfig.KeyPassphrase,
|
|
|
|
folder.FsConfig.HTTPConfig.Password, folder.FsConfig.HTTPConfig.APIKey)
|
2021-05-06 19:35:43 +00:00
|
|
|
|
2022-03-27 14:32:21 +00:00
|
|
|
updatedFolder = getFolderFromTemplate(updatedFolder, updatedFolder.Name)
|
|
|
|
|
2022-04-25 13:49:11 +00:00
|
|
|
err = dataprovider.UpdateFolder(&updatedFolder, folder.Users, folder.Groups, claims.Username, ipAddr)
|
2021-05-06 19:35:43 +00:00
|
|
|
if err != nil {
|
2022-04-25 13:49:11 +00:00
|
|
|
s.renderFolderPage(w, r, updatedFolder, folderPageModeUpdate, err.Error())
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
http.Redirect(w, r, webFoldersPath, http.StatusSeeOther)
|
|
|
|
}
|
|
|
|
|
2022-04-25 13:49:11 +00:00
|
|
|
func (s *httpdServer) getWebVirtualFolders(w http.ResponseWriter, r *http.Request, limit int, minimal bool) ([]vfs.BaseVirtualFolder, error) {
|
2021-05-06 19:35:43 +00:00
|
|
|
folders := make([]vfs.BaseVirtualFolder, 0, limit)
|
|
|
|
for {
|
2022-04-25 13:49:11 +00:00
|
|
|
f, err := dataprovider.GetFolders(limit, len(folders), dataprovider.OrderASC, minimal)
|
2021-05-06 19:35:43 +00:00
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderInternalServerErrorPage(w, r, err)
|
2021-05-23 20:02:01 +00:00
|
|
|
return folders, err
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
folders = append(folders, f...)
|
|
|
|
if len(f) < limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-05-23 20:02:01 +00:00
|
|
|
return folders, nil
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebGetFolders(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2021-05-23 20:02:01 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2022-04-25 13:49:11 +00:00
|
|
|
folders, err := s.getWebVirtualFolders(w, r, limit, false)
|
2021-05-23 20:02:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-05-06 19:35:43 +00:00
|
|
|
|
|
|
|
data := foldersPage{
|
2022-03-19 20:44:27 +00:00
|
|
|
basePage: s.getBasePageData(pageFoldersTitle, webFoldersPath, r),
|
2021-05-06 19:35:43 +00:00
|
|
|
Folders: folders,
|
|
|
|
}
|
|
|
|
renderAdminTemplate(w, templateFolders, data)
|
|
|
|
}
|
2022-04-25 13:49:11 +00:00
|
|
|
|
|
|
|
func (s *httpdServer) getWebGroups(w http.ResponseWriter, r *http.Request, limit int, minimal bool) ([]dataprovider.Group, error) {
|
|
|
|
groups := make([]dataprovider.Group, 0, limit)
|
|
|
|
for {
|
|
|
|
f, err := dataprovider.GetGroups(limit, len(groups), dataprovider.OrderASC, minimal)
|
|
|
|
if err != nil {
|
|
|
|
s.renderInternalServerErrorPage(w, r, err)
|
|
|
|
return groups, err
|
|
|
|
}
|
|
|
|
groups = append(groups, f...)
|
|
|
|
if len(f) < limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return groups, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpdServer) handleWebGetGroups(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
groups, err := s.getWebGroups(w, r, limit, false)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data := groupsPage{
|
|
|
|
basePage: s.getBasePageData(pageGroupsTitle, webGroupsPath, r),
|
|
|
|
Groups: groups,
|
|
|
|
}
|
|
|
|
renderAdminTemplate(w, templateGroups, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpdServer) handleWebAddGroupGet(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2022-07-11 06:17:36 +00:00
|
|
|
s.renderGroupPage(w, r, dataprovider.Group{}, genericPageModeAdd, "")
|
2022-04-25 13:49:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpdServer) handleWebAddGroupPost(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
|
|
|
s.renderBadRequestPage(w, r, errors.New("invalid token claims"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
group, err := getGroupFromPostFields(r)
|
|
|
|
if err != nil {
|
2022-07-11 06:17:36 +00:00
|
|
|
s.renderGroupPage(w, r, group, genericPageModeAdd, err.Error())
|
2022-04-25 13:49:11 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
ipAddr := util.GetIPFromRemoteAddress(r.RemoteAddr)
|
|
|
|
if err := verifyCSRFToken(r.Form.Get(csrfFormToken), ipAddr); err != nil {
|
|
|
|
s.renderForbiddenPage(w, r, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = dataprovider.AddGroup(&group, claims.Username, ipAddr)
|
|
|
|
if err != nil {
|
2022-07-11 06:17:36 +00:00
|
|
|
s.renderGroupPage(w, r, group, genericPageModeAdd, err.Error())
|
2022-04-25 13:49:11 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
http.Redirect(w, r, webGroupsPath, http.StatusSeeOther)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpdServer) handleWebUpdateGroupGet(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
name := getURLParam(r, "name")
|
|
|
|
group, err := dataprovider.GroupExists(name)
|
|
|
|
if err == nil {
|
2022-07-11 06:17:36 +00:00
|
|
|
s.renderGroupPage(w, r, group, genericPageModeUpdate, "")
|
2022-04-25 13:49:11 +00:00
|
|
|
} else if _, ok := err.(*util.RecordNotFoundError); ok {
|
|
|
|
s.renderNotFoundPage(w, r, err)
|
|
|
|
} else {
|
|
|
|
s.renderInternalServerErrorPage(w, r, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpdServer) handleWebUpdateGroupPost(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
|
|
|
s.renderBadRequestPage(w, r, errors.New("invalid token claims"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
name := getURLParam(r, "name")
|
|
|
|
group, err := dataprovider.GroupExists(name)
|
|
|
|
if _, ok := err.(*util.RecordNotFoundError); ok {
|
|
|
|
s.renderNotFoundPage(w, r, err)
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
|
|
|
s.renderInternalServerErrorPage(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
updatedGroup, err := getGroupFromPostFields(r)
|
|
|
|
if err != nil {
|
2022-07-11 06:17:36 +00:00
|
|
|
s.renderGroupPage(w, r, group, genericPageModeUpdate, err.Error())
|
2022-04-25 13:49:11 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
ipAddr := util.GetIPFromRemoteAddress(r.RemoteAddr)
|
|
|
|
if err := verifyCSRFToken(r.Form.Get(csrfFormToken), ipAddr); err != nil {
|
|
|
|
s.renderForbiddenPage(w, r, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
updatedGroup.ID = group.ID
|
|
|
|
updatedGroup.Name = group.Name
|
|
|
|
updatedGroup.SetEmptySecretsIfNil()
|
|
|
|
|
|
|
|
updateEncryptedSecrets(&updatedGroup.UserSettings.FsConfig, group.UserSettings.FsConfig.S3Config.AccessSecret,
|
|
|
|
group.UserSettings.FsConfig.AzBlobConfig.AccountKey, group.UserSettings.FsConfig.AzBlobConfig.SASURL,
|
|
|
|
group.UserSettings.FsConfig.GCSConfig.Credentials, group.UserSettings.FsConfig.CryptConfig.Passphrase,
|
2022-05-30 17:01:12 +00:00
|
|
|
group.UserSettings.FsConfig.SFTPConfig.Password, group.UserSettings.FsConfig.SFTPConfig.PrivateKey,
|
2022-06-11 08:41:34 +00:00
|
|
|
group.UserSettings.FsConfig.SFTPConfig.KeyPassphrase, group.UserSettings.FsConfig.HTTPConfig.Password,
|
|
|
|
group.UserSettings.FsConfig.HTTPConfig.APIKey)
|
2022-04-25 13:49:11 +00:00
|
|
|
|
|
|
|
err = dataprovider.UpdateGroup(&updatedGroup, group.Users, claims.Username, ipAddr)
|
|
|
|
if err != nil {
|
2022-07-11 06:17:36 +00:00
|
|
|
s.renderGroupPage(w, r, updatedGroup, genericPageModeUpdate, err.Error())
|
2022-04-25 13:49:11 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
http.Redirect(w, r, webGroupsPath, http.StatusSeeOther)
|
|
|
|
}
|
2022-07-11 06:17:36 +00:00
|
|
|
|
|
|
|
func (s *httpdServer) getWebEventActions(w http.ResponseWriter, r *http.Request, limit int, minimal bool,
|
|
|
|
) ([]dataprovider.BaseEventAction, error) {
|
|
|
|
actions := make([]dataprovider.BaseEventAction, 0, limit)
|
|
|
|
for {
|
|
|
|
res, err := dataprovider.GetEventActions(limit, len(actions), dataprovider.OrderASC, minimal)
|
|
|
|
if err != nil {
|
|
|
|
s.renderInternalServerErrorPage(w, r, err)
|
|
|
|
return actions, err
|
|
|
|
}
|
|
|
|
actions = append(actions, res...)
|
|
|
|
if len(res) < limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return actions, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpdServer) handleWebGetEventActions(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
actions, err := s.getWebEventActions(w, r, limit, false)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data := eventActionsPage{
|
|
|
|
basePage: s.getBasePageData(pageEventActionsTitle, webAdminEventActionsPath, r),
|
|
|
|
Actions: actions,
|
|
|
|
}
|
|
|
|
renderAdminTemplate(w, templateEventActions, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpdServer) handleWebAddEventActionGet(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
action := dataprovider.BaseEventAction{
|
|
|
|
Type: dataprovider.ActionTypeHTTP,
|
|
|
|
}
|
|
|
|
s.renderEventActionPage(w, r, action, genericPageModeAdd, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpdServer) handleWebAddEventActionPost(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
|
|
|
s.renderBadRequestPage(w, r, errors.New("invalid token claims"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
action, err := getEventActionFromPostFields(r)
|
|
|
|
if err != nil {
|
|
|
|
s.renderEventActionPage(w, r, action, genericPageModeAdd, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ipAddr := util.GetIPFromRemoteAddress(r.RemoteAddr)
|
|
|
|
if err := verifyCSRFToken(r.Form.Get(csrfFormToken), ipAddr); err != nil {
|
|
|
|
s.renderForbiddenPage(w, r, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = dataprovider.AddEventAction(&action, claims.Username, ipAddr); err != nil {
|
|
|
|
s.renderEventActionPage(w, r, action, genericPageModeAdd, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
http.Redirect(w, r, webAdminEventActionsPath, http.StatusSeeOther)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpdServer) handleWebUpdateEventActionGet(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
name := getURLParam(r, "name")
|
|
|
|
action, err := dataprovider.EventActionExists(name)
|
|
|
|
if err == nil {
|
|
|
|
s.renderEventActionPage(w, r, action, genericPageModeUpdate, "")
|
|
|
|
} else if _, ok := err.(*util.RecordNotFoundError); ok {
|
|
|
|
s.renderNotFoundPage(w, r, err)
|
|
|
|
} else {
|
|
|
|
s.renderInternalServerErrorPage(w, r, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpdServer) handleWebUpdateEventActionPost(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
|
|
|
s.renderBadRequestPage(w, r, errors.New("invalid token claims"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
name := getURLParam(r, "name")
|
|
|
|
action, err := dataprovider.EventActionExists(name)
|
|
|
|
if _, ok := err.(*util.RecordNotFoundError); ok {
|
|
|
|
s.renderNotFoundPage(w, r, err)
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
|
|
|
s.renderInternalServerErrorPage(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
updatedAction, err := getEventActionFromPostFields(r)
|
|
|
|
if err != nil {
|
|
|
|
s.renderEventActionPage(w, r, updatedAction, genericPageModeUpdate, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ipAddr := util.GetIPFromRemoteAddress(r.RemoteAddr)
|
|
|
|
if err := verifyCSRFToken(r.Form.Get(csrfFormToken), ipAddr); err != nil {
|
|
|
|
s.renderForbiddenPage(w, r, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
updatedAction.ID = action.ID
|
|
|
|
updatedAction.Name = action.Name
|
|
|
|
updatedAction.Options.SetEmptySecretsIfNil()
|
|
|
|
switch updatedAction.Type {
|
|
|
|
case dataprovider.ActionTypeHTTP:
|
|
|
|
if updatedAction.Options.HTTPConfig.Password.IsNotPlainAndNotEmpty() {
|
|
|
|
updatedAction.Options.HTTPConfig.Password = action.Options.HTTPConfig.Password
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = dataprovider.UpdateEventAction(&updatedAction, claims.Username, ipAddr)
|
|
|
|
if err != nil {
|
|
|
|
s.renderEventActionPage(w, r, updatedAction, genericPageModeUpdate, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
http.Redirect(w, r, webAdminEventActionsPath, http.StatusSeeOther)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpdServer) handleWebGetEventRules(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
limit := defaultQueryLimit
|
|
|
|
if _, ok := r.URL.Query()["qlimit"]; ok {
|
|
|
|
if lim, err := strconv.Atoi(r.URL.Query().Get("qlimit")); err == nil {
|
|
|
|
limit = lim
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rules := make([]dataprovider.EventRule, 0, limit)
|
|
|
|
for {
|
|
|
|
res, err := dataprovider.GetEventRules(limit, len(rules), dataprovider.OrderASC)
|
|
|
|
if err != nil {
|
|
|
|
s.renderInternalServerErrorPage(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rules = append(rules, res...)
|
|
|
|
if len(res) < limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
data := eventRulesPage{
|
|
|
|
basePage: s.getBasePageData(pageEventRulesTitle, webAdminEventRulesPath, r),
|
|
|
|
Rules: rules,
|
|
|
|
}
|
|
|
|
renderAdminTemplate(w, templateEventRules, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpdServer) handleWebAddEventRuleGet(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
rule := dataprovider.EventRule{
|
|
|
|
Trigger: dataprovider.EventTriggerFsEvent,
|
|
|
|
}
|
|
|
|
s.renderEventRulePage(w, r, rule, genericPageModeAdd, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpdServer) handleWebAddEventRulePost(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
|
|
|
s.renderBadRequestPage(w, r, errors.New("invalid token claims"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rule, err := getEventRuleFromPostFields(r)
|
|
|
|
if err != nil {
|
|
|
|
s.renderEventRulePage(w, r, rule, genericPageModeAdd, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ipAddr := util.GetIPFromRemoteAddress(r.RemoteAddr)
|
|
|
|
err = verifyCSRFToken(r.Form.Get(csrfFormToken), ipAddr)
|
|
|
|
if err != nil {
|
|
|
|
s.renderForbiddenPage(w, r, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = dataprovider.AddEventRule(&rule, claims.Username, ipAddr); err != nil {
|
|
|
|
s.renderEventRulePage(w, r, rule, genericPageModeAdd, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
http.Redirect(w, r, webAdminEventRulesPath, http.StatusSeeOther)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpdServer) handleWebUpdateEventRuleGet(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
name := getURLParam(r, "name")
|
|
|
|
rule, err := dataprovider.EventRuleExists(name)
|
|
|
|
if err == nil {
|
|
|
|
s.renderEventRulePage(w, r, rule, genericPageModeUpdate, "")
|
|
|
|
} else if _, ok := err.(*util.RecordNotFoundError); ok {
|
|
|
|
s.renderNotFoundPage(w, r, err)
|
|
|
|
} else {
|
|
|
|
s.renderInternalServerErrorPage(w, r, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpdServer) handleWebUpdateEventRulePost(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
|
|
|
s.renderBadRequestPage(w, r, errors.New("invalid token claims"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
name := getURLParam(r, "name")
|
|
|
|
rule, err := dataprovider.EventRuleExists(name)
|
|
|
|
if _, ok := err.(*util.RecordNotFoundError); ok {
|
|
|
|
s.renderNotFoundPage(w, r, err)
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
|
|
|
s.renderInternalServerErrorPage(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
updatedRule, err := getEventRuleFromPostFields(r)
|
|
|
|
if err != nil {
|
|
|
|
s.renderEventRulePage(w, r, updatedRule, genericPageModeUpdate, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ipAddr := util.GetIPFromRemoteAddress(r.RemoteAddr)
|
|
|
|
if err := verifyCSRFToken(r.Form.Get(csrfFormToken), ipAddr); err != nil {
|
|
|
|
s.renderForbiddenPage(w, r, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
updatedRule.ID = rule.ID
|
|
|
|
updatedRule.Name = rule.Name
|
|
|
|
err = dataprovider.UpdateEventRule(&updatedRule, claims.Username, ipAddr)
|
|
|
|
if err != nil {
|
|
|
|
s.renderEventRulePage(w, r, updatedRule, genericPageModeUpdate, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
http.Redirect(w, r, webAdminEventRulesPath, http.StatusSeeOther)
|
|
|
|
}
|