2024-01-01 10:31:45 +00:00
|
|
|
// Copyright (C) 2019 Nicola Murino
|
2022-07-17 18:16:00 +00:00
|
|
|
//
|
|
|
|
// 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
|
2023-01-03 09:18:30 +00:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2022-07-17 18:16:00 +00:00
|
|
|
|
2019-10-07 16:19:01 +00:00
|
|
|
// Package httpd implements REST API and Web interface for SFTPGo.
|
2023-02-09 08:33:33 +00:00
|
|
|
// The OpenAPI 3 schema for the supported API can be found inside the source tree:
|
2021-11-21 08:32:51 +00:00
|
|
|
// https://github.com/drakkan/sftpgo/blob/main/openapi/openapi.yaml
|
2019-10-07 16:19:01 +00:00
|
|
|
package httpd
|
|
|
|
|
|
|
|
import (
|
2021-07-01 18:17:40 +00:00
|
|
|
"crypto/sha256"
|
2022-07-19 20:25:00 +00:00
|
|
|
"errors"
|
2019-10-07 16:19:01 +00:00
|
|
|
"fmt"
|
2021-05-11 04:54:06 +00:00
|
|
|
"net"
|
2019-10-07 16:19:01 +00:00
|
|
|
"net/http"
|
2023-02-23 18:25:20 +00:00
|
|
|
"os"
|
2021-04-09 20:02:48 +00:00
|
|
|
"path"
|
2019-10-07 16:19:01 +00:00
|
|
|
"path/filepath"
|
2020-12-29 18:02:56 +00:00
|
|
|
"runtime"
|
2021-01-17 21:29:08 +00:00
|
|
|
"strings"
|
2021-01-26 21:35:36 +00:00
|
|
|
"time"
|
2019-10-07 16:19:01 +00:00
|
|
|
|
2021-03-01 18:28:11 +00:00
|
|
|
"github.com/go-chi/chi/v5"
|
2020-05-06 17:36:34 +00:00
|
|
|
|
2023-02-24 19:08:14 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/acme"
|
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/ftpd"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/logger"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/mfa"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/sftpd"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/util"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/webdavd"
|
2019-10-07 16:19:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-09-29 16:46:15 +00:00
|
|
|
logSender = "httpd"
|
|
|
|
tokenPath = "/api/v2/token"
|
|
|
|
logoutPath = "/api/v2/logout"
|
|
|
|
userTokenPath = "/api/v2/user/token"
|
|
|
|
userLogoutPath = "/api/v2/user/logout"
|
|
|
|
activeConnectionsPath = "/api/v2/connections"
|
|
|
|
quotasBasePath = "/api/v2/quotas"
|
|
|
|
userPath = "/api/v2/users"
|
|
|
|
versionPath = "/api/v2/version"
|
|
|
|
folderPath = "/api/v2/folders"
|
2022-04-25 13:49:11 +00:00
|
|
|
groupPath = "/api/v2/groups"
|
2021-09-29 16:46:15 +00:00
|
|
|
serverStatusPath = "/api/v2/status"
|
|
|
|
dumpDataPath = "/api/v2/dumpdata"
|
|
|
|
loadDataPath = "/api/v2/loaddata"
|
|
|
|
defenderHosts = "/api/v2/defender/hosts"
|
|
|
|
adminPath = "/api/v2/admins"
|
|
|
|
adminPwdPath = "/api/v2/admin/changepwd"
|
|
|
|
adminProfilePath = "/api/v2/admin/profile"
|
|
|
|
userPwdPath = "/api/v2/user/changepwd"
|
|
|
|
userDirsPath = "/api/v2/user/dirs"
|
|
|
|
userFilesPath = "/api/v2/user/files"
|
2022-12-30 18:30:16 +00:00
|
|
|
userFileActionsPath = "/api/v2/user/file-actions"
|
2021-09-29 16:46:15 +00:00
|
|
|
userStreamZipPath = "/api/v2/user/streamzip"
|
2021-12-08 18:25:22 +00:00
|
|
|
userUploadFilePath = "/api/v2/user/files/upload"
|
|
|
|
userFilesDirsMetadataPath = "/api/v2/user/files/metadata"
|
2021-09-29 16:46:15 +00:00
|
|
|
apiKeysPath = "/api/v2/apikeys"
|
|
|
|
adminTOTPConfigsPath = "/api/v2/admin/totp/configs"
|
|
|
|
adminTOTPGeneratePath = "/api/v2/admin/totp/generate"
|
|
|
|
adminTOTPValidatePath = "/api/v2/admin/totp/validate"
|
|
|
|
adminTOTPSavePath = "/api/v2/admin/totp/save"
|
|
|
|
admin2FARecoveryCodesPath = "/api/v2/admin/2fa/recoverycodes"
|
|
|
|
userTOTPConfigsPath = "/api/v2/user/totp/configs"
|
|
|
|
userTOTPGeneratePath = "/api/v2/user/totp/generate"
|
|
|
|
userTOTPValidatePath = "/api/v2/user/totp/validate"
|
|
|
|
userTOTPSavePath = "/api/v2/user/totp/save"
|
|
|
|
user2FARecoveryCodesPath = "/api/v2/user/2fa/recoverycodes"
|
|
|
|
userProfilePath = "/api/v2/user/profile"
|
2021-11-06 13:13:20 +00:00
|
|
|
userSharesPath = "/api/v2/user/shares"
|
2021-09-29 16:46:15 +00:00
|
|
|
retentionBasePath = "/api/v2/retention/users"
|
|
|
|
retentionChecksPath = "/api/v2/retention/users/checks"
|
2021-10-23 13:47:21 +00:00
|
|
|
fsEventsPath = "/api/v2/events/fs"
|
|
|
|
providerEventsPath = "/api/v2/events/provider"
|
2023-05-12 16:34:59 +00:00
|
|
|
logEventsPath = "/api/v2/events/logs"
|
2021-11-06 13:13:20 +00:00
|
|
|
sharesPath = "/api/v2/shares"
|
2022-07-11 06:17:36 +00:00
|
|
|
eventActionsPath = "/api/v2/eventactions"
|
|
|
|
eventRulesPath = "/api/v2/eventrules"
|
2022-11-16 18:04:50 +00:00
|
|
|
rolesPath = "/api/v2/roles"
|
2023-02-09 08:33:33 +00:00
|
|
|
ipListsPath = "/api/v2/iplists"
|
2021-09-29 16:46:15 +00:00
|
|
|
healthzPath = "/healthz"
|
|
|
|
webRootPathDefault = "/"
|
|
|
|
webBasePathDefault = "/web"
|
|
|
|
webBasePathAdminDefault = "/web/admin"
|
|
|
|
webBasePathClientDefault = "/web/client"
|
|
|
|
webAdminSetupPathDefault = "/web/admin/setup"
|
2022-02-13 13:30:20 +00:00
|
|
|
webAdminLoginPathDefault = "/web/admin/login"
|
|
|
|
webAdminOIDCLoginPathDefault = "/web/admin/oidclogin"
|
|
|
|
webOIDCRedirectPathDefault = "/web/oidc/redirect"
|
2023-06-03 14:17:32 +00:00
|
|
|
webOAuth2RedirectPathDefault = "/web/oauth2/redirect"
|
|
|
|
webOAuth2TokenPathDefault = "/web/admin/oauth2/token"
|
2021-09-29 16:46:15 +00:00
|
|
|
webAdminTwoFactorPathDefault = "/web/admin/twofactor"
|
|
|
|
webAdminTwoFactorRecoveryPathDefault = "/web/admin/twofactor-recovery"
|
|
|
|
webLogoutPathDefault = "/web/admin/logout"
|
|
|
|
webUsersPathDefault = "/web/admin/users"
|
|
|
|
webUserPathDefault = "/web/admin/user"
|
|
|
|
webConnectionsPathDefault = "/web/admin/connections"
|
|
|
|
webFoldersPathDefault = "/web/admin/folders"
|
|
|
|
webFolderPathDefault = "/web/admin/folder"
|
2022-04-25 13:49:11 +00:00
|
|
|
webGroupsPathDefault = "/web/admin/groups"
|
|
|
|
webGroupPathDefault = "/web/admin/group"
|
2021-09-29 16:46:15 +00:00
|
|
|
webStatusPathDefault = "/web/admin/status"
|
|
|
|
webAdminsPathDefault = "/web/admin/managers"
|
|
|
|
webAdminPathDefault = "/web/admin/manager"
|
|
|
|
webMaintenancePathDefault = "/web/admin/maintenance"
|
|
|
|
webBackupPathDefault = "/web/admin/backup"
|
|
|
|
webRestorePathDefault = "/web/admin/restore"
|
|
|
|
webScanVFolderPathDefault = "/web/admin/quotas/scanfolder"
|
|
|
|
webQuotaScanPathDefault = "/web/admin/quotas/scanuser"
|
|
|
|
webChangeAdminPwdPathDefault = "/web/admin/changepwd"
|
2021-11-13 12:25:43 +00:00
|
|
|
webAdminForgotPwdPathDefault = "/web/admin/forgot-password"
|
|
|
|
webAdminResetPwdPathDefault = "/web/admin/reset-password"
|
2021-09-29 16:46:15 +00:00
|
|
|
webAdminProfilePathDefault = "/web/admin/profile"
|
|
|
|
webAdminMFAPathDefault = "/web/admin/mfa"
|
2022-07-11 06:17:36 +00:00
|
|
|
webAdminEventRulesPathDefault = "/web/admin/eventrules"
|
|
|
|
webAdminEventRulePathDefault = "/web/admin/eventrule"
|
|
|
|
webAdminEventActionsPathDefault = "/web/admin/eventactions"
|
|
|
|
webAdminEventActionPathDefault = "/web/admin/eventaction"
|
2022-11-16 18:04:50 +00:00
|
|
|
webAdminRolesPathDefault = "/web/admin/roles"
|
|
|
|
webAdminRolePathDefault = "/web/admin/role"
|
2021-09-29 16:46:15 +00:00
|
|
|
webAdminTOTPGeneratePathDefault = "/web/admin/totp/generate"
|
|
|
|
webAdminTOTPValidatePathDefault = "/web/admin/totp/validate"
|
|
|
|
webAdminTOTPSavePathDefault = "/web/admin/totp/save"
|
|
|
|
webAdminRecoveryCodesPathDefault = "/web/admin/recoverycodes"
|
|
|
|
webTemplateUserDefault = "/web/admin/template/user"
|
|
|
|
webTemplateFolderDefault = "/web/admin/template/folder"
|
|
|
|
webDefenderPathDefault = "/web/admin/defender"
|
2023-02-09 08:33:33 +00:00
|
|
|
webIPListsPathDefault = "/web/admin/ip-lists"
|
|
|
|
webIPListPathDefault = "/web/admin/ip-list"
|
2021-09-29 16:46:15 +00:00
|
|
|
webDefenderHostsPathDefault = "/web/admin/defender/hosts"
|
2022-12-07 17:47:38 +00:00
|
|
|
webEventsPathDefault = "/web/admin/events"
|
|
|
|
webEventsFsSearchPathDefault = "/web/admin/events/fs"
|
|
|
|
webEventsProviderSearchPathDefault = "/web/admin/events/provider"
|
2023-05-12 16:34:59 +00:00
|
|
|
webEventsLogSearchPathDefault = "/web/admin/events/logs"
|
2023-02-19 18:03:45 +00:00
|
|
|
webConfigsPathDefault = "/web/admin/configs"
|
2021-09-29 16:46:15 +00:00
|
|
|
webClientLoginPathDefault = "/web/client/login"
|
2022-02-13 13:30:20 +00:00
|
|
|
webClientOIDCLoginPathDefault = "/web/client/oidclogin"
|
2021-09-29 16:46:15 +00:00
|
|
|
webClientTwoFactorPathDefault = "/web/client/twofactor"
|
|
|
|
webClientTwoFactorRecoveryPathDefault = "/web/client/twofactor-recovery"
|
|
|
|
webClientFilesPathDefault = "/web/client/files"
|
2021-12-08 18:25:22 +00:00
|
|
|
webClientFilePathDefault = "/web/client/file"
|
2022-12-30 18:30:16 +00:00
|
|
|
webClientFileActionsPathDefault = "/web/client/file-actions"
|
2021-11-06 13:13:20 +00:00
|
|
|
webClientSharesPathDefault = "/web/client/shares"
|
|
|
|
webClientSharePathDefault = "/web/client/share"
|
2021-10-09 12:17:28 +00:00
|
|
|
webClientEditFilePathDefault = "/web/client/editfile"
|
2021-09-29 16:46:15 +00:00
|
|
|
webClientDirsPathDefault = "/web/client/dirs"
|
|
|
|
webClientDownloadZipPathDefault = "/web/client/downloadzip"
|
|
|
|
webClientProfilePathDefault = "/web/client/profile"
|
2023-11-06 18:58:39 +00:00
|
|
|
webClientPingPathDefault = "/web/client/ping"
|
2021-09-29 16:46:15 +00:00
|
|
|
webClientMFAPathDefault = "/web/client/mfa"
|
|
|
|
webClientTOTPGeneratePathDefault = "/web/client/totp/generate"
|
|
|
|
webClientTOTPValidatePathDefault = "/web/client/totp/validate"
|
|
|
|
webClientTOTPSavePathDefault = "/web/client/totp/save"
|
|
|
|
webClientRecoveryCodesPathDefault = "/web/client/recoverycodes"
|
|
|
|
webChangeClientPwdPathDefault = "/web/client/changepwd"
|
|
|
|
webClientLogoutPathDefault = "/web/client/logout"
|
2021-11-06 13:13:20 +00:00
|
|
|
webClientPubSharesPathDefault = "/web/client/pubshares"
|
2021-11-13 12:25:43 +00:00
|
|
|
webClientForgotPwdPathDefault = "/web/client/forgot-password"
|
|
|
|
webClientResetPwdPathDefault = "/web/client/reset-password"
|
2021-11-25 18:24:32 +00:00
|
|
|
webClientViewPDFPathDefault = "/web/client/viewpdf"
|
2022-09-22 18:41:28 +00:00
|
|
|
webClientGetPDFPathDefault = "/web/client/getpdf"
|
2023-12-28 17:43:07 +00:00
|
|
|
webClientExistPathDefault = "/web/client/exist"
|
2024-03-11 17:19:57 +00:00
|
|
|
webClientTasksPathDefault = "/web/client/tasks"
|
2021-09-29 16:46:15 +00:00
|
|
|
webStaticFilesPathDefault = "/static"
|
2021-11-21 08:32:51 +00:00
|
|
|
webOpenAPIPathDefault = "/openapi"
|
2020-10-20 16:42:37 +00:00
|
|
|
// MaxRestoreSize defines the max size for the loaddata input file
|
2023-02-09 08:33:33 +00:00
|
|
|
MaxRestoreSize = 20 * 1048576 // 20 MB
|
|
|
|
maxRequestSize = 1048576 // 1MB
|
|
|
|
maxLoginBodySize = 262144 // 256 KB
|
2023-11-18 13:17:28 +00:00
|
|
|
httpdMaxEditFileSize = 2 * 1048576 // 2 MB
|
2023-02-09 08:33:33 +00:00
|
|
|
maxMultipartMem = 10 * 1048576 // 10 MB
|
2021-10-09 12:17:28 +00:00
|
|
|
osWindows = "windows"
|
|
|
|
otpHeaderCode = "X-SFTPGO-OTP"
|
2021-12-08 18:25:22 +00:00
|
|
|
mTimeHeader = "X-SFTPGO-MTIME"
|
2023-02-24 19:08:14 +00:00
|
|
|
acmeChallengeURI = "/.well-known/acme-challenge/"
|
2019-10-07 16:19:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-09-29 16:46:15 +00:00
|
|
|
certMgr *common.CertManager
|
|
|
|
cleanupTicker *time.Ticker
|
|
|
|
cleanupDone chan bool
|
2023-10-31 18:56:59 +00:00
|
|
|
invalidatedJWTTokens tokenManager
|
2021-09-29 16:46:15 +00:00
|
|
|
webRootPath string
|
|
|
|
webBasePath string
|
|
|
|
webBaseAdminPath string
|
|
|
|
webBaseClientPath string
|
2022-02-13 13:30:20 +00:00
|
|
|
webOIDCRedirectPath string
|
2023-06-03 14:17:32 +00:00
|
|
|
webOAuth2RedirectPath string
|
|
|
|
webOAuth2TokenPath string
|
2021-09-29 16:46:15 +00:00
|
|
|
webAdminSetupPath string
|
2022-02-13 13:30:20 +00:00
|
|
|
webAdminOIDCLoginPath string
|
|
|
|
webAdminLoginPath string
|
2021-09-29 16:46:15 +00:00
|
|
|
webAdminTwoFactorPath string
|
|
|
|
webAdminTwoFactorRecoveryPath string
|
|
|
|
webLogoutPath string
|
|
|
|
webUsersPath string
|
|
|
|
webUserPath string
|
|
|
|
webConnectionsPath string
|
|
|
|
webFoldersPath string
|
|
|
|
webFolderPath string
|
2022-04-25 13:49:11 +00:00
|
|
|
webGroupsPath string
|
|
|
|
webGroupPath string
|
2021-09-29 16:46:15 +00:00
|
|
|
webStatusPath string
|
|
|
|
webAdminsPath string
|
|
|
|
webAdminPath string
|
|
|
|
webMaintenancePath string
|
|
|
|
webBackupPath string
|
|
|
|
webRestorePath string
|
|
|
|
webScanVFolderPath string
|
|
|
|
webQuotaScanPath string
|
|
|
|
webAdminProfilePath string
|
|
|
|
webAdminMFAPath string
|
2022-07-11 06:17:36 +00:00
|
|
|
webAdminEventRulesPath string
|
|
|
|
webAdminEventRulePath string
|
|
|
|
webAdminEventActionsPath string
|
|
|
|
webAdminEventActionPath string
|
2022-11-16 18:04:50 +00:00
|
|
|
webAdminRolesPath string
|
|
|
|
webAdminRolePath string
|
2021-09-29 16:46:15 +00:00
|
|
|
webAdminTOTPGeneratePath string
|
|
|
|
webAdminTOTPValidatePath string
|
|
|
|
webAdminTOTPSavePath string
|
|
|
|
webAdminRecoveryCodesPath string
|
|
|
|
webChangeAdminPwdPath string
|
2021-11-13 12:25:43 +00:00
|
|
|
webAdminForgotPwdPath string
|
|
|
|
webAdminResetPwdPath string
|
2021-09-29 16:46:15 +00:00
|
|
|
webTemplateUser string
|
|
|
|
webTemplateFolder string
|
|
|
|
webDefenderPath string
|
2023-02-09 08:33:33 +00:00
|
|
|
webIPListPath string
|
|
|
|
webIPListsPath string
|
2022-12-07 17:47:38 +00:00
|
|
|
webEventsPath string
|
|
|
|
webEventsFsSearchPath string
|
|
|
|
webEventsProviderSearchPath string
|
2023-05-12 16:34:59 +00:00
|
|
|
webEventsLogSearchPath string
|
2023-02-19 18:03:45 +00:00
|
|
|
webConfigsPath string
|
2021-09-29 16:46:15 +00:00
|
|
|
webDefenderHostsPath string
|
|
|
|
webClientLoginPath string
|
2022-02-13 13:30:20 +00:00
|
|
|
webClientOIDCLoginPath string
|
2021-09-29 16:46:15 +00:00
|
|
|
webClientTwoFactorPath string
|
|
|
|
webClientTwoFactorRecoveryPath string
|
|
|
|
webClientFilesPath string
|
2021-12-08 18:25:22 +00:00
|
|
|
webClientFilePath string
|
2022-12-30 18:30:16 +00:00
|
|
|
webClientFileActionsPath string
|
2021-11-06 13:13:20 +00:00
|
|
|
webClientSharesPath string
|
|
|
|
webClientSharePath string
|
2021-10-09 12:17:28 +00:00
|
|
|
webClientEditFilePath string
|
2021-09-29 16:46:15 +00:00
|
|
|
webClientDirsPath string
|
|
|
|
webClientDownloadZipPath string
|
|
|
|
webClientProfilePath string
|
2023-11-06 18:58:39 +00:00
|
|
|
webClientPingPath string
|
2021-09-29 16:46:15 +00:00
|
|
|
webChangeClientPwdPath string
|
|
|
|
webClientMFAPath string
|
|
|
|
webClientTOTPGeneratePath string
|
|
|
|
webClientTOTPValidatePath string
|
|
|
|
webClientTOTPSavePath string
|
|
|
|
webClientRecoveryCodesPath string
|
2021-11-06 13:13:20 +00:00
|
|
|
webClientPubSharesPath string
|
2021-09-29 16:46:15 +00:00
|
|
|
webClientLogoutPath string
|
2021-11-13 12:25:43 +00:00
|
|
|
webClientForgotPwdPath string
|
|
|
|
webClientResetPwdPath string
|
2021-11-25 18:24:32 +00:00
|
|
|
webClientViewPDFPath string
|
2022-09-22 18:41:28 +00:00
|
|
|
webClientGetPDFPath string
|
2023-12-28 17:43:07 +00:00
|
|
|
webClientExistPath string
|
2024-03-11 17:19:57 +00:00
|
|
|
webClientTasksPath string
|
2021-09-29 16:46:15 +00:00
|
|
|
webStaticFilesPath string
|
2021-11-21 08:32:51 +00:00
|
|
|
webOpenAPIPath string
|
2021-07-23 08:19:27 +00:00
|
|
|
// max upload size for http clients, 1GB by default
|
2022-04-03 06:52:36 +00:00
|
|
|
maxUploadFileSize = int64(1048576000)
|
2022-07-11 14:14:44 +00:00
|
|
|
hideSupportLink bool
|
2022-04-03 06:52:36 +00:00
|
|
|
installationCode string
|
|
|
|
installationCodeHint string
|
|
|
|
fnInstallationCodeResolver FnInstallationCodeResolver
|
2023-02-23 18:25:20 +00:00
|
|
|
configurationDir string
|
2019-10-07 16:19:01 +00:00
|
|
|
)
|
|
|
|
|
2021-04-09 20:02:48 +00:00
|
|
|
func init() {
|
|
|
|
updateWebAdminURLs("")
|
2021-05-06 19:35:43 +00:00
|
|
|
updateWebClientURLs("")
|
2023-02-24 19:08:14 +00:00
|
|
|
acme.SetReloadHTTPDCertsFn(ReloadCertificateMgr)
|
2021-04-09 20:02:48 +00:00
|
|
|
}
|
|
|
|
|
2022-04-03 06:52:36 +00:00
|
|
|
// FnInstallationCodeResolver defines a method to get the installation code.
|
|
|
|
// If the installation code cannot be resolved the provided default must be returned
|
|
|
|
type FnInstallationCodeResolver func(defaultInstallationCode string) string
|
|
|
|
|
2022-02-17 17:22:27 +00:00
|
|
|
// HTTPSProxyHeader defines an HTTPS proxy header as key/value.
|
|
|
|
// For example Key could be "X-Forwarded-Proto" and Value "https"
|
|
|
|
type HTTPSProxyHeader struct {
|
|
|
|
Key string
|
|
|
|
Value string
|
|
|
|
}
|
|
|
|
|
|
|
|
// SecurityConf allows to add some security related headers to HTTP responses and to restrict allowed hosts
|
|
|
|
type SecurityConf struct {
|
|
|
|
// Set to true to enable the security configurations
|
|
|
|
Enabled bool `json:"enabled" mapstructure:"enabled"`
|
|
|
|
// AllowedHosts is a list of fully qualified domain names that are allowed.
|
|
|
|
// Default is empty list, which allows any and all host names.
|
|
|
|
AllowedHosts []string `json:"allowed_hosts" mapstructure:"allowed_hosts"`
|
|
|
|
// AllowedHostsAreRegex determines if the provided allowed hosts contains valid regular expressions
|
|
|
|
AllowedHostsAreRegex bool `json:"allowed_hosts_are_regex" mapstructure:"allowed_hosts_are_regex"`
|
|
|
|
// HostsProxyHeaders is a set of header keys that may hold a proxied hostname value for the request.
|
|
|
|
HostsProxyHeaders []string `json:"hosts_proxy_headers" mapstructure:"hosts_proxy_headers"`
|
2022-03-26 09:00:02 +00:00
|
|
|
// Set to true to redirect HTTP requests to HTTPS
|
|
|
|
HTTPSRedirect bool `json:"https_redirect" mapstructure:"https_redirect"`
|
|
|
|
// HTTPSHost defines the host name that is used to redirect HTTP requests to HTTPS.
|
|
|
|
// Default is "", which indicates to use the same host.
|
|
|
|
HTTPSHost string `json:"https_host" mapstructure:"https_host"`
|
2022-02-17 17:22:27 +00:00
|
|
|
// HTTPSProxyHeaders is a list of header keys with associated values that would indicate a valid https request.
|
|
|
|
HTTPSProxyHeaders []HTTPSProxyHeader `json:"https_proxy_headers" mapstructure:"https_proxy_headers"`
|
|
|
|
// STSSeconds is the max-age of the Strict-Transport-Security header.
|
|
|
|
// Default is 0, which would NOT include the header.
|
|
|
|
STSSeconds int64 `json:"sts_seconds" mapstructure:"sts_seconds"`
|
|
|
|
// If STSIncludeSubdomains is set to true, the "includeSubdomains" will be appended to the
|
|
|
|
// Strict-Transport-Security header. Default is false.
|
|
|
|
STSIncludeSubdomains bool `json:"sts_include_subdomains" mapstructure:"sts_include_subdomains"`
|
|
|
|
// If STSPreload is set to true, the `preload` flag will be appended to the
|
|
|
|
// Strict-Transport-Security header. Default is false.
|
|
|
|
STSPreload bool `json:"sts_preload" mapstructure:"sts_preload"`
|
|
|
|
// If ContentTypeNosniff is true, adds the X-Content-Type-Options header with the value "nosniff". Default is false.
|
|
|
|
ContentTypeNosniff bool `json:"content_type_nosniff" mapstructure:"content_type_nosniff"`
|
|
|
|
// ContentSecurityPolicy allows to set the Content-Security-Policy header value. Default is "".
|
|
|
|
ContentSecurityPolicy string `json:"content_security_policy" mapstructure:"content_security_policy"`
|
|
|
|
// PermissionsPolicy allows to set the Permissions-Policy header value. Default is "".
|
|
|
|
PermissionsPolicy string `json:"permissions_policy" mapstructure:"permissions_policy"`
|
|
|
|
// CrossOriginOpenerPolicy allows to set the `Cross-Origin-Opener-Policy` header value. Default is "".
|
|
|
|
CrossOriginOpenerPolicy string `json:"cross_origin_opener_policy" mapstructure:"cross_origin_opener_policy"`
|
2023-12-28 17:43:07 +00:00
|
|
|
proxyHeaders []string
|
2022-02-17 17:22:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SecurityConf) updateProxyHeaders() {
|
|
|
|
if !s.Enabled {
|
|
|
|
s.proxyHeaders = nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s.proxyHeaders = s.HostsProxyHeaders
|
|
|
|
for _, httpsProxyHeader := range s.HTTPSProxyHeaders {
|
|
|
|
s.proxyHeaders = append(s.proxyHeaders, httpsProxyHeader.Key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SecurityConf) getHTTPSProxyHeaders() map[string]string {
|
|
|
|
headers := make(map[string]string)
|
|
|
|
for _, httpsProxyHeader := range s.HTTPSProxyHeaders {
|
|
|
|
headers[httpsProxyHeader.Key] = httpsProxyHeader.Value
|
|
|
|
}
|
|
|
|
return headers
|
|
|
|
}
|
|
|
|
|
2023-02-24 19:08:14 +00:00
|
|
|
func (s *SecurityConf) redirectHandler(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if !isTLS(r) && !strings.HasPrefix(r.RequestURI, acmeChallengeURI) {
|
|
|
|
url := r.URL
|
|
|
|
url.Scheme = "https"
|
|
|
|
if s.HTTPSHost != "" {
|
|
|
|
url.Host = s.HTTPSHost
|
|
|
|
} else {
|
|
|
|
host := r.Host
|
|
|
|
for _, header := range s.HostsProxyHeaders {
|
|
|
|
if h := r.Header.Get(header); h != "" {
|
|
|
|
host = h
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
url.Host = host
|
|
|
|
}
|
|
|
|
http.Redirect(w, r, url.String(), http.StatusTemporaryRedirect)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-05-13 17:40:52 +00:00
|
|
|
// UIBranding defines the supported customizations for the web UIs
|
|
|
|
type UIBranding struct {
|
|
|
|
// Name defines the text to show at the login page and as HTML title
|
|
|
|
Name string `json:"name" mapstructure:"name"`
|
|
|
|
// ShortName defines the name to show next to the logo image
|
|
|
|
ShortName string `json:"short_name" mapstructure:"short_name"`
|
|
|
|
// Path to your logo relative to "static_files_path".
|
|
|
|
// For example, if you create a directory named "branding" inside the static dir and
|
|
|
|
// put the "mylogo.png" file in it, you must set "/branding/mylogo.png" as logo path.
|
|
|
|
LogoPath string `json:"logo_path" mapstructure:"logo_path"`
|
|
|
|
// Path to your favicon relative to "static_files_path"
|
|
|
|
FaviconPath string `json:"favicon_path" mapstructure:"favicon_path"`
|
|
|
|
// DisclaimerName defines the name for the link to your optional disclaimer
|
|
|
|
DisclaimerName string `json:"disclaimer_name" mapstructure:"disclaimer_name"`
|
2023-12-28 18:59:06 +00:00
|
|
|
// Path to the HTML page for your disclaimer relative to "static_files_path"
|
|
|
|
// or an absolute http/https URL.
|
2022-05-13 17:40:52 +00:00
|
|
|
DisclaimerPath string `json:"disclaimer_path" mapstructure:"disclaimer_path"`
|
2023-11-05 11:24:26 +00:00
|
|
|
// Path to custom CSS files, relative to "static_files_path", which replaces
|
|
|
|
// the default CSS files
|
|
|
|
DefaultCSS []string `json:"default_css" mapstructure:"default_css"`
|
2022-05-13 17:40:52 +00:00
|
|
|
// Additional CSS file paths, relative to "static_files_path", to include
|
|
|
|
ExtraCSS []string `json:"extra_css" mapstructure:"extra_css"`
|
|
|
|
}
|
|
|
|
|
2023-12-31 16:35:14 +00:00
|
|
|
func (b *UIBranding) check() {
|
2022-05-13 17:40:52 +00:00
|
|
|
if b.LogoPath != "" {
|
|
|
|
b.LogoPath = util.CleanPath(b.LogoPath)
|
|
|
|
} else {
|
|
|
|
b.LogoPath = "/img/logo.png"
|
|
|
|
}
|
|
|
|
if b.FaviconPath != "" {
|
|
|
|
b.FaviconPath = util.CleanPath(b.FaviconPath)
|
|
|
|
} else {
|
2024-07-20 14:11:21 +00:00
|
|
|
b.FaviconPath = "/favicon.png"
|
2022-05-13 17:40:52 +00:00
|
|
|
}
|
|
|
|
if b.DisclaimerPath != "" {
|
2023-12-28 18:59:06 +00:00
|
|
|
if !strings.HasPrefix(b.DisclaimerPath, "https://") && !strings.HasPrefix(b.DisclaimerPath, "http://") {
|
|
|
|
b.DisclaimerPath = path.Join(webStaticFilesPath, util.CleanPath(b.DisclaimerPath))
|
|
|
|
}
|
2022-05-13 17:40:52 +00:00
|
|
|
}
|
2023-11-05 11:24:26 +00:00
|
|
|
if len(b.DefaultCSS) > 0 {
|
|
|
|
for idx := range b.DefaultCSS {
|
|
|
|
b.DefaultCSS[idx] = util.CleanPath(b.DefaultCSS[idx])
|
|
|
|
}
|
2022-05-21 09:05:58 +00:00
|
|
|
} else {
|
2023-12-30 18:12:22 +00:00
|
|
|
b.DefaultCSS = []string{
|
|
|
|
"/assets/plugins/global/plugins.bundle.css",
|
|
|
|
"/assets/css/style.bundle.css",
|
2023-11-05 11:24:26 +00:00
|
|
|
}
|
2022-05-21 09:05:58 +00:00
|
|
|
}
|
2022-05-13 17:40:52 +00:00
|
|
|
for idx := range b.ExtraCSS {
|
|
|
|
b.ExtraCSS[idx] = util.CleanPath(b.ExtraCSS[idx])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Branding defines the branding-related customizations supported
|
|
|
|
type Branding struct {
|
|
|
|
WebAdmin UIBranding `json:"web_admin" mapstructure:"web_admin"`
|
|
|
|
WebClient UIBranding `json:"web_client" mapstructure:"web_client"`
|
2022-03-19 20:44:27 +00:00
|
|
|
}
|
|
|
|
|
2021-12-03 17:33:08 +00:00
|
|
|
// WebClientIntegration defines the configuration for an external Web Client integration
|
|
|
|
type WebClientIntegration struct {
|
|
|
|
// Files with these extensions can be sent to the configured URL
|
|
|
|
FileExtensions []string `json:"file_extensions" mapstructure:"file_extensions"`
|
|
|
|
// URL that will receive the files
|
|
|
|
URL string `json:"url" mapstructure:"url"`
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
// Binding defines the configuration for a network listener
|
|
|
|
type Binding struct {
|
|
|
|
// The address to listen on. A blank value means listen on all available network interfaces.
|
|
|
|
Address string `json:"address" mapstructure:"address"`
|
|
|
|
// The port used for serving requests
|
|
|
|
Port int `json:"port" mapstructure:"port"`
|
2021-01-19 17:59:41 +00:00
|
|
|
// Enable the built-in admin interface.
|
|
|
|
// You have to define TemplatesPath and StaticFilesPath for this to work
|
|
|
|
EnableWebAdmin bool `json:"enable_web_admin" mapstructure:"enable_web_admin"`
|
2021-05-06 19:35:43 +00:00
|
|
|
// Enable the built-in client interface.
|
|
|
|
// You have to define TemplatesPath and StaticFilesPath for this to work
|
|
|
|
EnableWebClient bool `json:"enable_web_client" mapstructure:"enable_web_client"`
|
2022-09-22 15:27:00 +00:00
|
|
|
// Enable REST API
|
|
|
|
EnableRESTAPI bool `json:"enable_rest_api" mapstructure:"enable_rest_api"`
|
2022-07-19 20:25:00 +00:00
|
|
|
// Defines the login methods available for the WebAdmin and WebClient UIs:
|
|
|
|
//
|
|
|
|
// - 0 means any configured method: username/password login form and OIDC, if enabled
|
2022-07-22 18:55:33 +00:00
|
|
|
// - 1 means OIDC for the WebAdmin UI
|
|
|
|
// - 2 means OIDC for the WebClient UI
|
|
|
|
// - 4 means login form for the WebAdmin UI
|
|
|
|
// - 8 means login form for the WebClient UI
|
2022-07-19 20:25:00 +00:00
|
|
|
//
|
|
|
|
// You can combine the values. For example 3 means that you can only login using OIDC on
|
|
|
|
// both WebClient and WebAdmin UI.
|
|
|
|
EnabledLoginMethods int `json:"enabled_login_methods" mapstructure:"enabled_login_methods"`
|
2021-01-19 17:59:41 +00:00
|
|
|
// you also need to provide a certificate for enabling HTTPS
|
|
|
|
EnableHTTPS bool `json:"enable_https" mapstructure:"enable_https"`
|
2022-05-21 14:34:47 +00:00
|
|
|
// Certificate and matching private key for this specific binding, if empty the global
|
|
|
|
// ones will be used, if any
|
|
|
|
CertificateFile string `json:"certificate_file" mapstructure:"certificate_file"`
|
|
|
|
CertificateKeyFile string `json:"certificate_key_file" mapstructure:"certificate_key_file"`
|
2022-02-13 14:56:07 +00:00
|
|
|
// Defines the minimum TLS version. 13 means TLS 1.3, default is TLS 1.2
|
|
|
|
MinTLSVersion int `json:"min_tls_version" mapstructure:"min_tls_version"`
|
2021-01-19 17:59:41 +00:00
|
|
|
// set to 1 to require client certificate authentication in addition to basic auth.
|
|
|
|
// You need to define at least a certificate authority for this to work
|
|
|
|
ClientAuthType int `json:"client_auth_type" mapstructure:"client_auth_type"`
|
2021-02-18 19:17:16 +00:00
|
|
|
// TLSCipherSuites is a list of supported cipher suites for TLS version 1.2.
|
|
|
|
// If CipherSuites is nil/empty, a default list of secure cipher suites
|
|
|
|
// is used, with a preference order based on hardware performance.
|
|
|
|
// Note that TLS 1.3 ciphersuites are not configurable.
|
|
|
|
// The supported ciphersuites names are defined here:
|
|
|
|
//
|
2023-10-28 10:35:26 +00:00
|
|
|
// https://github.com/golang/go/blob/master/src/crypto/tls/cipher_suites.go#L53
|
2021-02-18 19:17:16 +00:00
|
|
|
//
|
|
|
|
// any invalid name will be silently ignored.
|
|
|
|
// The order matters, the ciphers listed first will be the preferred ones.
|
|
|
|
TLSCipherSuites []string `json:"tls_cipher_suites" mapstructure:"tls_cipher_suites"`
|
2023-10-28 10:35:26 +00:00
|
|
|
// HTTP protocols in preference order. Supported values: http/1.1, h2
|
2023-10-28 10:42:05 +00:00
|
|
|
Protocols []string `json:"tls_protocols" mapstructure:"tls_protocols"`
|
2022-05-28 17:47:23 +00:00
|
|
|
// List of IP addresses and IP ranges allowed to set client IP proxy headers and
|
|
|
|
// X-Forwarded-Proto header.
|
2021-07-27 16:43:00 +00:00
|
|
|
ProxyAllowed []string `json:"proxy_allowed" mapstructure:"proxy_allowed"`
|
2022-05-28 17:47:23 +00:00
|
|
|
// Allowed client IP proxy header such as "X-Forwarded-For", "X-Real-IP"
|
|
|
|
ClientIPProxyHeader string `json:"client_ip_proxy_header" mapstructure:"client_ip_proxy_header"`
|
|
|
|
// Some client IP headers such as "X-Forwarded-For" can contain multiple IP address, this setting
|
|
|
|
// define the position to trust starting from the right. For example if we have:
|
|
|
|
// "10.0.0.1,11.0.0.1,12.0.0.1,13.0.0.1" and the depth is 0, SFTPGo will use "13.0.0.1"
|
|
|
|
// as client IP, if depth is 1, "12.0.0.1" will be used and so on
|
|
|
|
ClientIPHeaderDepth int `json:"client_ip_header_depth" mapstructure:"client_ip_header_depth"`
|
2021-07-27 16:43:00 +00:00
|
|
|
// If both web admin and web client are enabled each login page will show a link
|
|
|
|
// to the other one. This setting allows to hide this link:
|
|
|
|
// - 0 login links are displayed on both admin and client login page. This is the default
|
|
|
|
// - 1 the login link to the web client login page is hidden on admin login page
|
|
|
|
// - 2 the login link to the web admin login page is hidden on client login page
|
|
|
|
// The flags can be combined, for example 3 will disable both login links.
|
2021-11-21 08:32:51 +00:00
|
|
|
HideLoginURL int `json:"hide_login_url" mapstructure:"hide_login_url"`
|
|
|
|
// Enable the built-in OpenAPI renderer
|
2021-12-03 17:33:08 +00:00
|
|
|
RenderOpenAPI bool `json:"render_openapi" mapstructure:"render_openapi"`
|
2022-02-13 13:30:20 +00:00
|
|
|
// Defining an OIDC configuration the web admin and web client UI will use OpenID to authenticate users.
|
2022-02-17 17:22:27 +00:00
|
|
|
OIDC OIDC `json:"oidc" mapstructure:"oidc"`
|
|
|
|
// Security defines security headers to add to HTTP responses and allows to restrict allowed hosts
|
2022-03-19 20:44:27 +00:00
|
|
|
Security SecurityConf `json:"security" mapstructure:"security"`
|
2022-05-13 17:40:52 +00:00
|
|
|
// Branding defines customizations to suit your brand
|
|
|
|
Branding Branding `json:"branding" mapstructure:"branding"`
|
2022-02-13 13:30:20 +00:00
|
|
|
allowHeadersFrom []func(net.IP) bool
|
2021-12-03 17:33:08 +00:00
|
|
|
}
|
|
|
|
|
2022-05-13 17:40:52 +00:00
|
|
|
func (b *Binding) checkBranding() {
|
2023-12-31 16:35:14 +00:00
|
|
|
b.Branding.WebAdmin.check()
|
|
|
|
b.Branding.WebClient.check()
|
2022-05-13 17:40:52 +00:00
|
|
|
if b.Branding.WebAdmin.Name == "" {
|
|
|
|
b.Branding.WebAdmin.Name = "SFTPGo WebAdmin"
|
|
|
|
}
|
|
|
|
if b.Branding.WebAdmin.ShortName == "" {
|
|
|
|
b.Branding.WebAdmin.ShortName = "WebAdmin"
|
|
|
|
}
|
|
|
|
if b.Branding.WebClient.Name == "" {
|
|
|
|
b.Branding.WebClient.Name = "SFTPGo WebClient"
|
|
|
|
}
|
|
|
|
if b.Branding.WebClient.ShortName == "" {
|
|
|
|
b.Branding.WebClient.ShortName = "WebClient"
|
2022-03-19 20:44:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-11 04:54:06 +00:00
|
|
|
func (b *Binding) parseAllowedProxy() error {
|
2022-06-09 06:37:37 +00:00
|
|
|
if filepath.IsAbs(b.Address) && len(b.ProxyAllowed) > 0 {
|
|
|
|
// unix domain socket
|
2024-02-15 20:13:45 +00:00
|
|
|
b.allowHeadersFrom = []func(net.IP) bool{func(_ net.IP) bool { return true }}
|
2022-06-09 06:37:37 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
allowedFuncs, err := util.ParseAllowedIPAndRanges(b.ProxyAllowed)
|
2021-05-11 04:54:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
b.allowHeadersFrom = allowedFuncs
|
|
|
|
return nil
|
2021-01-19 17:59:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetAddress returns the binding address
|
|
|
|
func (b *Binding) GetAddress() string {
|
|
|
|
return fmt.Sprintf("%s:%d", b.Address, b.Port)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsValid returns true if the binding is valid
|
|
|
|
func (b *Binding) IsValid() bool {
|
2022-09-22 15:27:00 +00:00
|
|
|
if !b.EnableRESTAPI && !b.EnableWebAdmin && !b.EnableWebClient {
|
|
|
|
return false
|
|
|
|
}
|
2021-01-19 17:59:41 +00:00
|
|
|
if b.Port > 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if filepath.IsAbs(b.Address) && runtime.GOOS != osWindows {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
|
|
|
|
2022-07-22 18:55:33 +00:00
|
|
|
func (b *Binding) isWebAdminOIDCLoginDisabled() bool {
|
|
|
|
if b.EnableWebAdmin {
|
|
|
|
if b.EnabledLoginMethods == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return b.EnabledLoginMethods&1 == 0
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Binding) isWebClientOIDCLoginDisabled() bool {
|
|
|
|
if b.EnableWebClient {
|
|
|
|
if b.EnabledLoginMethods == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return b.EnabledLoginMethods&2 == 0
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-07-19 20:25:00 +00:00
|
|
|
func (b *Binding) isWebAdminLoginFormDisabled() bool {
|
|
|
|
if b.EnableWebAdmin {
|
|
|
|
if b.EnabledLoginMethods == 0 {
|
|
|
|
return false
|
|
|
|
}
|
2022-07-22 18:55:33 +00:00
|
|
|
return b.EnabledLoginMethods&4 == 0
|
2022-07-19 20:25:00 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Binding) isWebClientLoginFormDisabled() bool {
|
|
|
|
if b.EnableWebClient {
|
|
|
|
if b.EnabledLoginMethods == 0 {
|
|
|
|
return false
|
|
|
|
}
|
2022-07-22 18:55:33 +00:00
|
|
|
return b.EnabledLoginMethods&8 == 0
|
2022-07-19 20:25:00 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Binding) checkLoginMethods() error {
|
2022-07-22 18:55:33 +00:00
|
|
|
if b.isWebAdminLoginFormDisabled() && b.isWebAdminOIDCLoginDisabled() {
|
2022-07-19 20:25:00 +00:00
|
|
|
return errors.New("no login method available for WebAdmin UI")
|
|
|
|
}
|
2022-07-22 18:55:33 +00:00
|
|
|
if !b.isWebAdminOIDCLoginDisabled() {
|
|
|
|
if b.isWebAdminLoginFormDisabled() && !b.OIDC.hasRoles() {
|
|
|
|
return errors.New("no login method available for WebAdmin UI")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if b.isWebClientLoginFormDisabled() && b.isWebClientOIDCLoginDisabled() {
|
2022-07-19 20:25:00 +00:00
|
|
|
return errors.New("no login method available for WebClient UI")
|
|
|
|
}
|
2022-07-22 18:55:33 +00:00
|
|
|
if !b.isWebClientOIDCLoginDisabled() {
|
|
|
|
if b.isWebClientLoginFormDisabled() && !b.OIDC.isEnabled() {
|
|
|
|
return errors.New("no login method available for WebClient UI")
|
|
|
|
}
|
|
|
|
}
|
2022-07-19 20:25:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-27 16:43:00 +00:00
|
|
|
func (b *Binding) showAdminLoginURL() bool {
|
|
|
|
if !b.EnableWebAdmin {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if b.HideLoginURL&2 != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Binding) showClientLoginURL() bool {
|
|
|
|
if !b.EnableWebClient {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if b.HideLoginURL&1 != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2024-06-07 16:19:05 +00:00
|
|
|
func (b *Binding) isMutualTLSEnabled() bool {
|
|
|
|
return b.ClientAuthType == 1
|
|
|
|
}
|
|
|
|
|
2021-01-02 18:33:24 +00:00
|
|
|
type defenderStatus struct {
|
|
|
|
IsActive bool `json:"is_active"`
|
|
|
|
}
|
|
|
|
|
2023-02-09 08:33:33 +00:00
|
|
|
type allowListStatus struct {
|
|
|
|
IsActive bool `json:"is_active"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type rateLimiters struct {
|
|
|
|
IsActive bool `json:"is_active"`
|
|
|
|
Protocols []string `json:"protocols"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetProtocolsAsString returns the enabled protocols as comma separated string
|
|
|
|
func (r *rateLimiters) GetProtocolsAsString() string {
|
|
|
|
return strings.Join(r.Protocols, ", ")
|
|
|
|
}
|
|
|
|
|
2020-12-08 10:18:34 +00:00
|
|
|
// ServicesStatus keep the state of the running services
|
|
|
|
type ServicesStatus struct {
|
|
|
|
SSH sftpd.ServiceStatus `json:"ssh"`
|
|
|
|
FTP ftpd.ServiceStatus `json:"ftp"`
|
|
|
|
WebDAV webdavd.ServiceStatus `json:"webdav"`
|
|
|
|
DataProvider dataprovider.ProviderStatus `json:"data_provider"`
|
2021-01-02 18:33:24 +00:00
|
|
|
Defender defenderStatus `json:"defender"`
|
2021-09-04 10:11:04 +00:00
|
|
|
MFA mfa.ServiceStatus `json:"mfa"`
|
2023-02-09 08:33:33 +00:00
|
|
|
AllowList allowListStatus `json:"allow_list"`
|
|
|
|
RateLimiters rateLimiters `json:"rate_limiters"`
|
2020-12-08 10:18:34 +00:00
|
|
|
}
|
|
|
|
|
2022-02-27 12:08:47 +00:00
|
|
|
// SetupConfig defines the configuration parameters for the initial web admin setup
|
|
|
|
type SetupConfig struct {
|
|
|
|
// Installation code to require when creating the first admin account.
|
|
|
|
// As for the other configurations, this value is read at SFTPGo startup and not at runtime
|
|
|
|
// even if set using an environment variable.
|
|
|
|
// This is not a license key or similar, the purpose here is to prevent anyone who can access
|
|
|
|
// to the initial setup screen from creating an admin user
|
|
|
|
InstallationCode string `json:"installation_code" mapstructure:"installation_code"`
|
|
|
|
// Description for the installation code input field
|
|
|
|
InstallationCodeHint string `json:"installation_code_hint" mapstructure:"installation_code_hint"`
|
|
|
|
}
|
|
|
|
|
2021-11-13 22:14:50 +00:00
|
|
|
// CorsConfig defines the CORS configuration
|
|
|
|
type CorsConfig struct {
|
2022-08-02 16:44:34 +00:00
|
|
|
AllowedOrigins []string `json:"allowed_origins" mapstructure:"allowed_origins"`
|
|
|
|
AllowedMethods []string `json:"allowed_methods" mapstructure:"allowed_methods"`
|
|
|
|
AllowedHeaders []string `json:"allowed_headers" mapstructure:"allowed_headers"`
|
|
|
|
ExposedHeaders []string `json:"exposed_headers" mapstructure:"exposed_headers"`
|
|
|
|
AllowCredentials bool `json:"allow_credentials" mapstructure:"allow_credentials"`
|
|
|
|
Enabled bool `json:"enabled" mapstructure:"enabled"`
|
|
|
|
MaxAge int `json:"max_age" mapstructure:"max_age"`
|
|
|
|
OptionsPassthrough bool `json:"options_passthrough" mapstructure:"options_passthrough"`
|
|
|
|
OptionsSuccessStatus int `json:"options_success_status" mapstructure:"options_success_status"`
|
|
|
|
AllowPrivateNetwork bool `json:"allow_private_network" mapstructure:"allow_private_network"`
|
2021-11-13 22:14:50 +00:00
|
|
|
}
|
|
|
|
|
2019-10-07 16:19:01 +00:00
|
|
|
// Conf httpd daemon configuration
|
|
|
|
type Conf struct {
|
2021-01-19 17:59:41 +00:00
|
|
|
// Addresses and ports to bind to
|
|
|
|
Bindings []Binding `json:"bindings" mapstructure:"bindings"`
|
2019-10-07 16:19:01 +00:00
|
|
|
// Path to the HTML web templates. This can be an absolute path or a path relative to the config dir
|
|
|
|
TemplatesPath string `json:"templates_path" mapstructure:"templates_path"`
|
2020-06-18 21:53:38 +00:00
|
|
|
// Path to the static files for the web interface. This can be an absolute path or a path relative to the config dir.
|
|
|
|
// If both TemplatesPath and StaticFilesPath are empty the built-in web interface will be disabled
|
2019-10-07 16:19:01 +00:00
|
|
|
StaticFilesPath string `json:"static_files_path" mapstructure:"static_files_path"`
|
2019-12-27 22:12:44 +00:00
|
|
|
// Path to the backup directory. This can be an absolute path or a path relative to the config dir
|
2022-03-13 12:45:07 +00:00
|
|
|
//BackupsPath string `json:"backups_path" mapstructure:"backups_path"`
|
2021-11-21 08:32:51 +00:00
|
|
|
// Path to the directory that contains the OpenAPI schema and the default renderer.
|
|
|
|
// This can be an absolute path or a path relative to the config dir
|
|
|
|
OpenAPIPath string `json:"openapi_path" mapstructure:"openapi_path"`
|
2021-05-06 19:35:43 +00:00
|
|
|
// Defines a base URL for the web admin and client interfaces. If empty web admin and client resources will
|
|
|
|
// be available at the root ("/") URI. If defined it must be an absolute URI or it will be ignored.
|
|
|
|
WebRoot string `json:"web_root" mapstructure:"web_root"`
|
2022-05-21 14:34:47 +00:00
|
|
|
// If files containing a certificate and matching private key for the server are provided you can enable
|
|
|
|
// HTTPS connections for the configured bindings.
|
2020-02-04 22:21:33 +00:00
|
|
|
// Certificate and key files can be reloaded on demand sending a "SIGHUP" signal on Unix based systems and a
|
|
|
|
// "paramchange" request to the running service on Windows.
|
2020-02-03 23:08:00 +00:00
|
|
|
CertificateFile string `json:"certificate_file" mapstructure:"certificate_file"`
|
|
|
|
CertificateKeyFile string `json:"certificate_key_file" mapstructure:"certificate_key_file"`
|
2021-01-19 17:59:41 +00:00
|
|
|
// CACertificates defines the set of root certificate authorities to be used to verify client certificates.
|
|
|
|
CACertificates []string `json:"ca_certificates" mapstructure:"ca_certificates"`
|
|
|
|
// CARevocationLists defines a set a revocation lists, one for each root CA, to be used to check
|
|
|
|
// if a client certificate has been revoked
|
|
|
|
CARevocationLists []string `json:"ca_revocation_lists" mapstructure:"ca_revocation_lists"`
|
2021-07-01 18:17:40 +00:00
|
|
|
// SigningPassphrase defines the passphrase to use to derive the signing key for JWT and CSRF tokens.
|
|
|
|
// If empty a random signing key will be generated each time SFTPGo starts. If you set a
|
|
|
|
// signing passphrase you should consider rotating it periodically for added security
|
2023-11-24 19:43:50 +00:00
|
|
|
SigningPassphrase string `json:"signing_passphrase" mapstructure:"signing_passphrase"`
|
|
|
|
SigningPassphraseFile string `json:"signing_passphrase_file" mapstructure:"signing_passphrase_file"`
|
2022-05-28 11:28:50 +00:00
|
|
|
// TokenValidation allows to define how to validate JWT tokens, cookies and CSRF tokens.
|
|
|
|
// By default all the available security checks are enabled. Set to 1 to disable the requirement
|
|
|
|
// that a token must be used by the same IP for which it was issued.
|
|
|
|
TokenValidation int `json:"token_validation" mapstructure:"token_validation"`
|
2021-07-23 08:19:27 +00:00
|
|
|
// MaxUploadFileSize Defines the maximum request body size, in bytes, for Web Client/API HTTP upload requests.
|
|
|
|
// 0 means no limit
|
|
|
|
MaxUploadFileSize int64 `json:"max_upload_file_size" mapstructure:"max_upload_file_size"`
|
2021-11-13 22:14:50 +00:00
|
|
|
// CORS configuration
|
|
|
|
Cors CorsConfig `json:"cors" mapstructure:"cors"`
|
2022-02-27 12:08:47 +00:00
|
|
|
// Initial setup configuration
|
|
|
|
Setup SetupConfig `json:"setup" mapstructure:"setup"`
|
2022-07-11 14:14:44 +00:00
|
|
|
// If enabled, the link to the sponsors section will not appear on the setup screen page
|
|
|
|
HideSupportLink bool `json:"hide_support_link" mapstructure:"hide_support_link"`
|
2023-02-23 18:25:20 +00:00
|
|
|
acmeDomain string
|
2019-12-27 22:12:44 +00:00
|
|
|
}
|
|
|
|
|
2019-10-07 16:19:01 +00:00
|
|
|
type apiResponse struct {
|
2020-09-08 07:45:21 +00:00
|
|
|
Error string `json:"error,omitempty"`
|
|
|
|
Message string `json:"message"`
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
|
|
|
|
2021-01-19 17:59:41 +00:00
|
|
|
// ShouldBind returns true if there is at least a valid binding
|
|
|
|
func (c *Conf) ShouldBind() bool {
|
|
|
|
for _, binding := range c.Bindings {
|
|
|
|
if binding.IsValid() {
|
|
|
|
return true
|
|
|
|
}
|
2020-12-29 18:02:56 +00:00
|
|
|
}
|
2021-01-19 17:59:41 +00:00
|
|
|
|
2020-12-29 18:02:56 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-05-06 19:35:43 +00:00
|
|
|
func (c *Conf) isWebAdminEnabled() bool {
|
|
|
|
for _, binding := range c.Bindings {
|
|
|
|
if binding.EnableWebAdmin {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Conf) isWebClientEnabled() bool {
|
|
|
|
for _, binding := range c.Bindings {
|
|
|
|
if binding.EnableWebClient {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-05-11 04:54:06 +00:00
|
|
|
func (c *Conf) checkRequiredDirs(staticFilesPath, templatesPath string) error {
|
|
|
|
if (c.isWebAdminEnabled() || c.isWebClientEnabled()) && (staticFilesPath == "" || templatesPath == "") {
|
2023-02-27 18:02:43 +00:00
|
|
|
return fmt.Errorf("required directory is invalid, static file path: %q template path: %q",
|
2021-05-11 04:54:06 +00:00
|
|
|
staticFilesPath, templatesPath)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-01 18:17:40 +00:00
|
|
|
func (c *Conf) getRedacted() Conf {
|
2022-02-13 13:30:20 +00:00
|
|
|
redacted := "[redacted]"
|
2021-07-01 18:17:40 +00:00
|
|
|
conf := *c
|
2022-02-27 12:08:47 +00:00
|
|
|
if conf.SigningPassphrase != "" {
|
|
|
|
conf.SigningPassphrase = redacted
|
|
|
|
}
|
|
|
|
if conf.Setup.InstallationCode != "" {
|
|
|
|
conf.Setup.InstallationCode = redacted
|
|
|
|
}
|
2022-02-13 13:30:20 +00:00
|
|
|
conf.Bindings = nil
|
|
|
|
for _, binding := range c.Bindings {
|
|
|
|
if binding.OIDC.ClientID != "" {
|
|
|
|
binding.OIDC.ClientID = redacted
|
|
|
|
}
|
|
|
|
if binding.OIDC.ClientSecret != "" {
|
|
|
|
binding.OIDC.ClientSecret = redacted
|
|
|
|
}
|
|
|
|
conf.Bindings = append(conf.Bindings, binding)
|
|
|
|
}
|
2021-07-01 18:17:40 +00:00
|
|
|
return conf
|
|
|
|
}
|
|
|
|
|
2022-05-21 14:34:47 +00:00
|
|
|
func (c *Conf) getKeyPairs(configDir string) []common.TLSKeyPair {
|
|
|
|
var keyPairs []common.TLSKeyPair
|
|
|
|
|
|
|
|
for _, binding := range c.Bindings {
|
|
|
|
certificateFile := getConfigPath(binding.CertificateFile, configDir)
|
|
|
|
certificateKeyFile := getConfigPath(binding.CertificateKeyFile, configDir)
|
|
|
|
if certificateFile != "" && certificateKeyFile != "" {
|
|
|
|
keyPairs = append(keyPairs, common.TLSKeyPair{
|
|
|
|
Cert: certificateFile,
|
|
|
|
Key: certificateKeyFile,
|
|
|
|
ID: binding.GetAddress(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-02-23 18:25:20 +00:00
|
|
|
var certificateFile, certificateKeyFile string
|
|
|
|
if c.acmeDomain != "" {
|
|
|
|
certificateFile, certificateKeyFile = util.GetACMECertificateKeyPair(c.acmeDomain)
|
|
|
|
} else {
|
|
|
|
certificateFile = getConfigPath(c.CertificateFile, configDir)
|
|
|
|
certificateKeyFile = getConfigPath(c.CertificateKeyFile, configDir)
|
|
|
|
}
|
2022-05-21 14:34:47 +00:00
|
|
|
if certificateFile != "" && certificateKeyFile != "" {
|
|
|
|
keyPairs = append(keyPairs, common.TLSKeyPair{
|
|
|
|
Cert: certificateFile,
|
|
|
|
Key: certificateKeyFile,
|
|
|
|
ID: common.DefaultTLSKeyPaidID,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return keyPairs
|
|
|
|
}
|
|
|
|
|
2022-05-28 11:28:50 +00:00
|
|
|
func (c *Conf) setTokenValidationMode() {
|
|
|
|
if c.TokenValidation == 1 {
|
|
|
|
tokenValidationMode = tokenValidationNoIPMatch
|
|
|
|
} else {
|
|
|
|
tokenValidationMode = tokenValidationFull
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-23 18:25:20 +00:00
|
|
|
func (c *Conf) loadFromProvider() error {
|
|
|
|
configs, err := dataprovider.GetConfigs()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to load config from provider: %w", err)
|
|
|
|
}
|
|
|
|
configs.SetNilsToEmpty()
|
|
|
|
if configs.ACME.Domain == "" || !configs.ACME.HasProtocol(common.ProtocolHTTP) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
crt, key := util.GetACMECertificateKeyPair(configs.ACME.Domain)
|
|
|
|
if crt != "" && key != "" {
|
|
|
|
if _, err := os.Stat(crt); err != nil {
|
|
|
|
logger.Error(logSender, "", "unable to load acme cert file %q: %v", crt, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if _, err := os.Stat(key); err != nil {
|
|
|
|
logger.Error(logSender, "", "unable to load acme key file %q: %v", key, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for idx := range c.Bindings {
|
2023-02-24 19:08:14 +00:00
|
|
|
if c.Bindings[idx].Security.Enabled && c.Bindings[idx].Security.HTTPSRedirect {
|
|
|
|
continue
|
|
|
|
}
|
2023-02-23 18:25:20 +00:00
|
|
|
c.Bindings[idx].EnableHTTPS = true
|
|
|
|
}
|
|
|
|
c.acmeDomain = configs.ACME.Domain
|
|
|
|
logger.Info(logSender, "", "acme domain set to %q", c.acmeDomain)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-24 19:43:50 +00:00
|
|
|
func (c *Conf) loadTemplates(templatesPath string) {
|
|
|
|
if c.isWebAdminEnabled() {
|
|
|
|
updateWebAdminURLs(c.WebRoot)
|
|
|
|
loadAdminTemplates(templatesPath)
|
|
|
|
} else {
|
|
|
|
logger.Info(logSender, "", "built-in web admin interface disabled")
|
|
|
|
}
|
|
|
|
if c.isWebClientEnabled() {
|
|
|
|
updateWebClientURLs(c.WebRoot)
|
|
|
|
loadClientTemplates(templatesPath)
|
|
|
|
} else {
|
|
|
|
logger.Info(logSender, "", "built-in web client interface disabled")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-26 21:29:09 +00:00
|
|
|
// Initialize configures and starts the HTTP server
|
2022-05-19 17:49:51 +00:00
|
|
|
func (c *Conf) Initialize(configDir string, isShared int) error {
|
2023-02-23 18:25:20 +00:00
|
|
|
if err := c.loadFromProvider(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-05 12:22:49 +00:00
|
|
|
logger.Info(logSender, "", "initializing HTTP server with config %+v", c.getRedacted())
|
2023-02-23 18:25:20 +00:00
|
|
|
configurationDir = configDir
|
2023-10-31 18:56:59 +00:00
|
|
|
invalidatedJWTTokens = newTokenManager(isShared)
|
2022-05-19 17:49:51 +00:00
|
|
|
resetCodesMgr = newResetCodeManager(isShared)
|
|
|
|
oidcMgr = newOIDCManager(isShared)
|
2023-06-03 14:17:32 +00:00
|
|
|
oauth2Mgr = newOAuth2Manager(isShared)
|
2024-03-11 17:19:57 +00:00
|
|
|
webTaskMgr = newWebTaskManager(isShared)
|
2022-05-05 09:27:19 +00:00
|
|
|
staticFilesPath := util.FindSharedDataPath(c.StaticFilesPath, configDir)
|
|
|
|
templatesPath := util.FindSharedDataPath(c.TemplatesPath, configDir)
|
|
|
|
openAPIPath := util.FindSharedDataPath(c.OpenAPIPath, configDir)
|
2021-05-11 04:54:06 +00:00
|
|
|
if err := c.checkRequiredDirs(staticFilesPath, templatesPath); err != nil {
|
|
|
|
return err
|
2020-03-03 08:09:58 +00:00
|
|
|
}
|
2023-11-24 19:43:50 +00:00
|
|
|
c.loadTemplates(templatesPath)
|
2022-05-21 14:34:47 +00:00
|
|
|
keyPairs := c.getKeyPairs(configDir)
|
|
|
|
if len(keyPairs) > 0 {
|
|
|
|
mgr, err := common.NewCertManager(keyPairs, configDir, logSender)
|
2020-02-04 22:21:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-19 17:59:41 +00:00
|
|
|
mgr.SetCACertificates(c.CACertificates)
|
|
|
|
if err := mgr.LoadRootCAs(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
mgr.SetCARevocationLists(c.CARevocationLists)
|
|
|
|
if err := mgr.LoadCRLs(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
certMgr = mgr
|
2020-02-03 23:08:00 +00:00
|
|
|
}
|
2021-01-19 17:59:41 +00:00
|
|
|
|
2023-11-24 19:43:50 +00:00
|
|
|
if c.SigningPassphraseFile != "" {
|
|
|
|
passphrase, err := util.ReadConfigFromFile(c.SigningPassphraseFile, configDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.SigningPassphrase = passphrase
|
|
|
|
}
|
|
|
|
|
2022-07-11 14:14:44 +00:00
|
|
|
hideSupportLink = c.HideSupportLink
|
2021-02-03 07:55:28 +00:00
|
|
|
|
2021-01-19 17:59:41 +00:00
|
|
|
exitChannel := make(chan error, 1)
|
|
|
|
|
|
|
|
for _, binding := range c.Bindings {
|
|
|
|
if !binding.IsValid() {
|
|
|
|
continue
|
|
|
|
}
|
2021-05-11 04:54:06 +00:00
|
|
|
if err := binding.parseAllowedProxy(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-13 17:40:52 +00:00
|
|
|
binding.checkBranding()
|
2022-02-17 17:22:27 +00:00
|
|
|
binding.Security.updateProxyHeaders()
|
2021-01-19 17:59:41 +00:00
|
|
|
|
|
|
|
go func(b Binding) {
|
2022-02-13 13:30:20 +00:00
|
|
|
if err := b.OIDC.initialize(); err != nil {
|
|
|
|
exitChannel <- err
|
|
|
|
return
|
|
|
|
}
|
2022-07-19 20:25:00 +00:00
|
|
|
if err := b.checkLoginMethods(); err != nil {
|
|
|
|
exitChannel <- err
|
|
|
|
return
|
|
|
|
}
|
2021-11-21 08:32:51 +00:00
|
|
|
server := newHttpdServer(b, staticFilesPath, c.SigningPassphrase, c.Cors, openAPIPath)
|
2022-07-11 06:17:36 +00:00
|
|
|
server.setShared(isShared)
|
2021-01-19 17:59:41 +00:00
|
|
|
|
|
|
|
exitChannel <- server.listenAndServe()
|
|
|
|
}(binding)
|
|
|
|
}
|
|
|
|
|
2021-07-23 08:19:27 +00:00
|
|
|
maxUploadFileSize = c.MaxUploadFileSize
|
2022-02-27 12:08:47 +00:00
|
|
|
installationCode = c.Setup.InstallationCode
|
|
|
|
installationCodeHint = c.Setup.InstallationCodeHint
|
2021-11-13 12:25:43 +00:00
|
|
|
startCleanupTicker(tokenDuration / 2)
|
2022-05-28 11:28:50 +00:00
|
|
|
c.setTokenValidationMode()
|
2021-01-19 17:59:41 +00:00
|
|
|
return <-exitChannel
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
|
|
|
|
2021-05-06 19:35:43 +00:00
|
|
|
func isWebRequest(r *http.Request) bool {
|
2021-01-17 21:29:08 +00:00
|
|
|
return strings.HasPrefix(r.RequestURI, webBasePath+"/")
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
2020-02-03 23:08:00 +00:00
|
|
|
|
2021-05-06 19:35:43 +00:00
|
|
|
func isWebClientRequest(r *http.Request) bool {
|
|
|
|
return strings.HasPrefix(r.RequestURI, webBaseClientPath+"/")
|
|
|
|
}
|
|
|
|
|
2021-01-03 16:03:04 +00:00
|
|
|
// ReloadCertificateMgr reloads the certificate manager
|
|
|
|
func ReloadCertificateMgr() error {
|
2020-02-04 22:21:33 +00:00
|
|
|
if certMgr != nil {
|
2021-01-03 16:03:04 +00:00
|
|
|
return certMgr.Reload()
|
2020-02-04 22:21:33 +00:00
|
|
|
}
|
2020-04-30 12:23:55 +00:00
|
|
|
return nil
|
2020-02-04 22:21:33 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 23:08:00 +00:00
|
|
|
func getConfigPath(name, configDir string) string {
|
2021-07-11 13:26:51 +00:00
|
|
|
if !util.IsFileInputValid(name) {
|
2020-03-03 08:09:58 +00:00
|
|
|
return ""
|
|
|
|
}
|
2020-12-18 15:04:42 +00:00
|
|
|
if name != "" && !filepath.IsAbs(name) {
|
2020-02-03 23:08:00 +00:00
|
|
|
return filepath.Join(configDir, name)
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|
2020-12-08 10:18:34 +00:00
|
|
|
|
2022-02-26 15:43:29 +00:00
|
|
|
func getServicesStatus() *ServicesStatus {
|
2023-02-09 08:33:33 +00:00
|
|
|
rtlEnabled, rtlProtocols := common.Config.GetRateLimitersStatus()
|
2022-02-26 15:43:29 +00:00
|
|
|
status := &ServicesStatus{
|
2020-12-08 10:18:34 +00:00
|
|
|
SSH: sftpd.GetStatus(),
|
|
|
|
FTP: ftpd.GetStatus(),
|
|
|
|
WebDAV: webdavd.GetStatus(),
|
|
|
|
DataProvider: dataprovider.GetProviderStatus(),
|
2021-01-02 18:33:24 +00:00
|
|
|
Defender: defenderStatus{
|
|
|
|
IsActive: common.Config.DefenderConfig.Enabled,
|
|
|
|
},
|
2021-09-04 10:11:04 +00:00
|
|
|
MFA: mfa.GetStatus(),
|
2023-02-09 08:33:33 +00:00
|
|
|
AllowList: allowListStatus{
|
|
|
|
IsActive: common.Config.IsAllowListEnabled(),
|
|
|
|
},
|
|
|
|
RateLimiters: rateLimiters{
|
|
|
|
IsActive: rtlEnabled,
|
|
|
|
Protocols: rtlProtocols,
|
|
|
|
},
|
2020-12-08 10:18:34 +00:00
|
|
|
}
|
|
|
|
return status
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
|
2023-09-08 17:55:45 +00:00
|
|
|
func fileServer(r chi.Router, path string, root http.FileSystem, disableDirectoryIndex bool) {
|
2021-01-17 21:29:08 +00:00
|
|
|
if path != "/" && path[len(path)-1] != '/' {
|
|
|
|
r.Get(path, http.RedirectHandler(path+"/", http.StatusMovedPermanently).ServeHTTP)
|
|
|
|
path += "/"
|
|
|
|
}
|
|
|
|
path += "*"
|
|
|
|
|
|
|
|
r.Get(path, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
rctx := chi.RouteContext(r.Context())
|
|
|
|
pathPrefix := strings.TrimSuffix(rctx.RoutePattern(), "/*")
|
2023-09-08 17:55:45 +00:00
|
|
|
if disableDirectoryIndex {
|
2023-11-03 16:22:28 +00:00
|
|
|
root = neuteredFileSystem{root}
|
2023-09-08 17:55:45 +00:00
|
|
|
}
|
2023-11-03 16:22:28 +00:00
|
|
|
fs := http.StripPrefix(pathPrefix, http.FileServer(root))
|
2021-01-17 21:29:08 +00:00
|
|
|
fs.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-05-06 19:35:43 +00:00
|
|
|
func updateWebClientURLs(baseURL string) {
|
|
|
|
if !path.IsAbs(baseURL) {
|
|
|
|
baseURL = "/"
|
|
|
|
}
|
|
|
|
webRootPath = path.Join(baseURL, webRootPathDefault)
|
|
|
|
webBasePath = path.Join(baseURL, webBasePathDefault)
|
|
|
|
webBaseClientPath = path.Join(baseURL, webBasePathClientDefault)
|
2022-02-13 13:30:20 +00:00
|
|
|
webOIDCRedirectPath = path.Join(baseURL, webOIDCRedirectPathDefault)
|
2021-05-06 19:35:43 +00:00
|
|
|
webClientLoginPath = path.Join(baseURL, webClientLoginPathDefault)
|
2022-02-13 13:30:20 +00:00
|
|
|
webClientOIDCLoginPath = path.Join(baseURL, webClientOIDCLoginPathDefault)
|
2021-09-04 10:11:04 +00:00
|
|
|
webClientTwoFactorPath = path.Join(baseURL, webClientTwoFactorPathDefault)
|
|
|
|
webClientTwoFactorRecoveryPath = path.Join(baseURL, webClientTwoFactorRecoveryPathDefault)
|
2021-05-06 19:35:43 +00:00
|
|
|
webClientFilesPath = path.Join(baseURL, webClientFilesPathDefault)
|
2021-12-08 18:25:22 +00:00
|
|
|
webClientFilePath = path.Join(baseURL, webClientFilePathDefault)
|
2022-12-30 18:30:16 +00:00
|
|
|
webClientFileActionsPath = path.Join(baseURL, webClientFileActionsPathDefault)
|
2021-11-06 13:13:20 +00:00
|
|
|
webClientSharesPath = path.Join(baseURL, webClientSharesPathDefault)
|
|
|
|
webClientPubSharesPath = path.Join(baseURL, webClientPubSharesPathDefault)
|
|
|
|
webClientSharePath = path.Join(baseURL, webClientSharePathDefault)
|
2021-10-09 12:17:28 +00:00
|
|
|
webClientEditFilePath = path.Join(baseURL, webClientEditFilePathDefault)
|
2021-07-26 18:55:49 +00:00
|
|
|
webClientDirsPath = path.Join(baseURL, webClientDirsPathDefault)
|
2021-06-05 14:07:09 +00:00
|
|
|
webClientDownloadZipPath = path.Join(baseURL, webClientDownloadZipPathDefault)
|
2021-09-29 16:46:15 +00:00
|
|
|
webClientProfilePath = path.Join(baseURL, webClientProfilePathDefault)
|
2023-11-06 18:58:39 +00:00
|
|
|
webClientPingPath = path.Join(baseURL, webClientPingPathDefault)
|
2021-05-06 19:35:43 +00:00
|
|
|
webChangeClientPwdPath = path.Join(baseURL, webChangeClientPwdPathDefault)
|
|
|
|
webClientLogoutPath = path.Join(baseURL, webClientLogoutPathDefault)
|
2021-09-04 10:11:04 +00:00
|
|
|
webClientMFAPath = path.Join(baseURL, webClientMFAPathDefault)
|
|
|
|
webClientTOTPGeneratePath = path.Join(baseURL, webClientTOTPGeneratePathDefault)
|
|
|
|
webClientTOTPValidatePath = path.Join(baseURL, webClientTOTPValidatePathDefault)
|
|
|
|
webClientTOTPSavePath = path.Join(baseURL, webClientTOTPSavePathDefault)
|
|
|
|
webClientRecoveryCodesPath = path.Join(baseURL, webClientRecoveryCodesPathDefault)
|
2021-11-13 12:25:43 +00:00
|
|
|
webClientForgotPwdPath = path.Join(baseURL, webClientForgotPwdPathDefault)
|
|
|
|
webClientResetPwdPath = path.Join(baseURL, webClientResetPwdPathDefault)
|
2021-11-25 18:24:32 +00:00
|
|
|
webClientViewPDFPath = path.Join(baseURL, webClientViewPDFPathDefault)
|
2022-09-22 18:41:28 +00:00
|
|
|
webClientGetPDFPath = path.Join(baseURL, webClientGetPDFPathDefault)
|
2023-12-28 17:43:07 +00:00
|
|
|
webClientExistPath = path.Join(baseURL, webClientExistPathDefault)
|
2024-03-11 17:19:57 +00:00
|
|
|
webClientTasksPath = path.Join(baseURL, webClientTasksPathDefault)
|
2023-12-28 18:59:06 +00:00
|
|
|
webStaticFilesPath = path.Join(baseURL, webStaticFilesPathDefault)
|
|
|
|
webOpenAPIPath = path.Join(baseURL, webOpenAPIPathDefault)
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
2021-04-09 20:02:48 +00:00
|
|
|
func updateWebAdminURLs(baseURL string) {
|
|
|
|
if !path.IsAbs(baseURL) {
|
|
|
|
baseURL = "/"
|
|
|
|
}
|
|
|
|
webRootPath = path.Join(baseURL, webRootPathDefault)
|
|
|
|
webBasePath = path.Join(baseURL, webBasePathDefault)
|
2021-05-06 19:35:43 +00:00
|
|
|
webBaseAdminPath = path.Join(baseURL, webBasePathAdminDefault)
|
2022-02-13 13:30:20 +00:00
|
|
|
webOIDCRedirectPath = path.Join(baseURL, webOIDCRedirectPathDefault)
|
2023-06-03 14:17:32 +00:00
|
|
|
webOAuth2RedirectPath = path.Join(baseURL, webOAuth2RedirectPathDefault)
|
|
|
|
webOAuth2TokenPath = path.Join(baseURL, webOAuth2TokenPathDefault)
|
2021-05-14 17:21:15 +00:00
|
|
|
webAdminSetupPath = path.Join(baseURL, webAdminSetupPathDefault)
|
2022-02-13 13:30:20 +00:00
|
|
|
webAdminLoginPath = path.Join(baseURL, webAdminLoginPathDefault)
|
|
|
|
webAdminOIDCLoginPath = path.Join(baseURL, webAdminOIDCLoginPathDefault)
|
2021-09-04 10:11:04 +00:00
|
|
|
webAdminTwoFactorPath = path.Join(baseURL, webAdminTwoFactorPathDefault)
|
|
|
|
webAdminTwoFactorRecoveryPath = path.Join(baseURL, webAdminTwoFactorRecoveryPathDefault)
|
2021-04-09 20:02:48 +00:00
|
|
|
webLogoutPath = path.Join(baseURL, webLogoutPathDefault)
|
|
|
|
webUsersPath = path.Join(baseURL, webUsersPathDefault)
|
|
|
|
webUserPath = path.Join(baseURL, webUserPathDefault)
|
|
|
|
webConnectionsPath = path.Join(baseURL, webConnectionsPathDefault)
|
|
|
|
webFoldersPath = path.Join(baseURL, webFoldersPathDefault)
|
|
|
|
webFolderPath = path.Join(baseURL, webFolderPathDefault)
|
2022-04-25 13:49:11 +00:00
|
|
|
webGroupsPath = path.Join(baseURL, webGroupsPathDefault)
|
|
|
|
webGroupPath = path.Join(baseURL, webGroupPathDefault)
|
2021-04-09 20:02:48 +00:00
|
|
|
webStatusPath = path.Join(baseURL, webStatusPathDefault)
|
|
|
|
webAdminsPath = path.Join(baseURL, webAdminsPathDefault)
|
|
|
|
webAdminPath = path.Join(baseURL, webAdminPathDefault)
|
|
|
|
webMaintenancePath = path.Join(baseURL, webMaintenancePathDefault)
|
|
|
|
webBackupPath = path.Join(baseURL, webBackupPathDefault)
|
|
|
|
webRestorePath = path.Join(baseURL, webRestorePathDefault)
|
|
|
|
webScanVFolderPath = path.Join(baseURL, webScanVFolderPathDefault)
|
|
|
|
webQuotaScanPath = path.Join(baseURL, webQuotaScanPathDefault)
|
|
|
|
webChangeAdminPwdPath = path.Join(baseURL, webChangeAdminPwdPathDefault)
|
2021-11-13 12:25:43 +00:00
|
|
|
webAdminForgotPwdPath = path.Join(baseURL, webAdminForgotPwdPathDefault)
|
|
|
|
webAdminResetPwdPath = path.Join(baseURL, webAdminResetPwdPathDefault)
|
2021-09-29 16:46:15 +00:00
|
|
|
webAdminProfilePath = path.Join(baseURL, webAdminProfilePathDefault)
|
2021-09-04 10:11:04 +00:00
|
|
|
webAdminMFAPath = path.Join(baseURL, webAdminMFAPathDefault)
|
2022-07-11 06:17:36 +00:00
|
|
|
webAdminEventRulesPath = path.Join(baseURL, webAdminEventRulesPathDefault)
|
|
|
|
webAdminEventRulePath = path.Join(baseURL, webAdminEventRulePathDefault)
|
|
|
|
webAdminEventActionsPath = path.Join(baseURL, webAdminEventActionsPathDefault)
|
|
|
|
webAdminEventActionPath = path.Join(baseURL, webAdminEventActionPathDefault)
|
2022-11-16 18:04:50 +00:00
|
|
|
webAdminRolesPath = path.Join(baseURL, webAdminRolesPathDefault)
|
|
|
|
webAdminRolePath = path.Join(baseURL, webAdminRolePathDefault)
|
2021-09-04 10:11:04 +00:00
|
|
|
webAdminTOTPGeneratePath = path.Join(baseURL, webAdminTOTPGeneratePathDefault)
|
|
|
|
webAdminTOTPValidatePath = path.Join(baseURL, webAdminTOTPValidatePathDefault)
|
|
|
|
webAdminTOTPSavePath = path.Join(baseURL, webAdminTOTPSavePathDefault)
|
|
|
|
webAdminRecoveryCodesPath = path.Join(baseURL, webAdminRecoveryCodesPathDefault)
|
2021-04-09 20:02:48 +00:00
|
|
|
webTemplateUser = path.Join(baseURL, webTemplateUserDefault)
|
|
|
|
webTemplateFolder = path.Join(baseURL, webTemplateFolderDefault)
|
2021-06-08 11:24:28 +00:00
|
|
|
webDefenderHostsPath = path.Join(baseURL, webDefenderHostsPathDefault)
|
|
|
|
webDefenderPath = path.Join(baseURL, webDefenderPathDefault)
|
2023-02-09 08:33:33 +00:00
|
|
|
webIPListPath = path.Join(baseURL, webIPListPathDefault)
|
|
|
|
webIPListsPath = path.Join(baseURL, webIPListsPathDefault)
|
2022-12-07 17:47:38 +00:00
|
|
|
webEventsPath = path.Join(baseURL, webEventsPathDefault)
|
|
|
|
webEventsFsSearchPath = path.Join(baseURL, webEventsFsSearchPathDefault)
|
|
|
|
webEventsProviderSearchPath = path.Join(baseURL, webEventsProviderSearchPathDefault)
|
2023-05-12 16:34:59 +00:00
|
|
|
webEventsLogSearchPath = path.Join(baseURL, webEventsLogSearchPathDefault)
|
2023-02-19 18:03:45 +00:00
|
|
|
webConfigsPath = path.Join(baseURL, webConfigsPathDefault)
|
2021-04-09 20:02:48 +00:00
|
|
|
webStaticFilesPath = path.Join(baseURL, webStaticFilesPathDefault)
|
2021-11-21 08:32:51 +00:00
|
|
|
webOpenAPIPath = path.Join(baseURL, webOpenAPIPathDefault)
|
2021-04-09 20:02:48 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
// GetHTTPRouter returns an HTTP handler suitable to use for test cases
|
2021-12-03 17:33:08 +00:00
|
|
|
func GetHTTPRouter(b Binding) http.Handler {
|
2022-07-24 14:18:54 +00:00
|
|
|
server := newHttpdServer(b, filepath.Join("..", "..", "static"), "", CorsConfig{}, filepath.Join("..", "..", "openapi"))
|
2021-01-17 21:29:08 +00:00
|
|
|
server.initializeRouter()
|
|
|
|
return server.router
|
|
|
|
}
|
2021-01-26 21:35:36 +00:00
|
|
|
|
|
|
|
// the ticker cannot be started/stopped from multiple goroutines
|
2021-09-04 10:11:04 +00:00
|
|
|
func startCleanupTicker(duration time.Duration) {
|
|
|
|
stopCleanupTicker()
|
|
|
|
cleanupTicker = time.NewTicker(duration)
|
|
|
|
cleanupDone = make(chan bool)
|
2021-01-26 21:35:36 +00:00
|
|
|
|
|
|
|
go func() {
|
2022-05-19 17:49:51 +00:00
|
|
|
counter := int64(0)
|
2021-01-26 21:35:36 +00:00
|
|
|
for {
|
|
|
|
select {
|
2021-09-04 10:11:04 +00:00
|
|
|
case <-cleanupDone:
|
2021-01-26 21:35:36 +00:00
|
|
|
return
|
2021-09-04 10:11:04 +00:00
|
|
|
case <-cleanupTicker.C:
|
2022-05-19 17:49:51 +00:00
|
|
|
counter++
|
2023-10-31 18:56:59 +00:00
|
|
|
invalidatedJWTTokens.Cleanup()
|
2022-05-19 17:49:51 +00:00
|
|
|
resetCodesMgr.Cleanup()
|
2024-03-11 17:19:57 +00:00
|
|
|
webTaskMgr.Cleanup()
|
2022-05-19 17:49:51 +00:00
|
|
|
if counter%2 == 0 {
|
|
|
|
oidcMgr.cleanup()
|
2023-06-03 14:17:32 +00:00
|
|
|
oauth2Mgr.cleanup()
|
2022-05-19 17:49:51 +00:00
|
|
|
}
|
2021-01-26 21:35:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2021-09-04 10:11:04 +00:00
|
|
|
func stopCleanupTicker() {
|
|
|
|
if cleanupTicker != nil {
|
|
|
|
cleanupTicker.Stop()
|
|
|
|
cleanupDone <- true
|
|
|
|
cleanupTicker = nil
|
2021-01-26 21:35:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-01 18:17:40 +00:00
|
|
|
func getSigningKey(signingPassphrase string) []byte {
|
|
|
|
if signingPassphrase != "" {
|
|
|
|
sk := sha256.Sum256([]byte(signingPassphrase))
|
|
|
|
return sk[:]
|
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
return util.GenerateRandomBytes(32)
|
2021-07-01 18:17:40 +00:00
|
|
|
}
|
2022-04-03 06:52:36 +00:00
|
|
|
|
|
|
|
// SetInstallationCodeResolver sets a function to call to resolve the installation code
|
|
|
|
func SetInstallationCodeResolver(fn FnInstallationCodeResolver) {
|
|
|
|
fnInstallationCodeResolver = fn
|
|
|
|
}
|
|
|
|
|
|
|
|
func resolveInstallationCode() string {
|
|
|
|
if fnInstallationCodeResolver != nil {
|
|
|
|
return fnInstallationCodeResolver(installationCode)
|
|
|
|
}
|
|
|
|
return installationCode
|
|
|
|
}
|
2023-11-03 16:22:28 +00:00
|
|
|
|
|
|
|
type neuteredFileSystem struct {
|
|
|
|
fs http.FileSystem
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nfs neuteredFileSystem) Open(name string) (http.File, error) {
|
|
|
|
f, err := nfs.fs.Open(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
s, err := f.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.IsDir() {
|
|
|
|
index := path.Join(name, "index.html")
|
|
|
|
if _, err := nfs.fs.Open(index); err != nil {
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return f, nil
|
|
|
|
}
|