2021-05-06 19:35:43 +00:00
|
|
|
package httpd
|
|
|
|
|
|
|
|
import (
|
2021-10-09 12:17:28 +00:00
|
|
|
"bytes"
|
2021-05-30 21:07:46 +00:00
|
|
|
"encoding/json"
|
2021-11-13 12:25:43 +00:00
|
|
|
"errors"
|
2021-05-06 19:35:43 +00:00
|
|
|
"fmt"
|
|
|
|
"html/template"
|
2021-10-09 12:17:28 +00:00
|
|
|
"io"
|
2021-05-06 19:35:43 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2021-11-06 13:13:20 +00:00
|
|
|
"strconv"
|
2021-10-09 12:17:28 +00:00
|
|
|
"strings"
|
2021-05-06 19:35:43 +00:00
|
|
|
"time"
|
|
|
|
|
2021-05-27 07:40:46 +00:00
|
|
|
"github.com/go-chi/render"
|
2021-05-06 19:35:43 +00:00
|
|
|
"github.com/rs/xid"
|
2022-01-06 10:54:43 +00:00
|
|
|
"github.com/sftpgo/sdk"
|
2021-05-06 19:35:43 +00:00
|
|
|
|
2021-06-26 05:31:41 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/common"
|
|
|
|
"github.com/drakkan/sftpgo/v2/dataprovider"
|
2021-09-04 10:11:04 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/mfa"
|
2021-11-13 12:25:43 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/smtp"
|
2021-07-11 13:26:51 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/util"
|
2021-06-26 05:31:41 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/version"
|
|
|
|
"github.com/drakkan/sftpgo/v2/vfs"
|
2021-05-06 19:35:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-09-04 10:11:04 +00:00
|
|
|
templateClientDir = "webclient"
|
|
|
|
templateClientBase = "base.html"
|
|
|
|
templateClientBaseLogin = "baselogin.html"
|
|
|
|
templateClientLogin = "login.html"
|
|
|
|
templateClientFiles = "files.html"
|
|
|
|
templateClientMessage = "message.html"
|
2021-09-29 16:46:15 +00:00
|
|
|
templateClientProfile = "profile.html"
|
|
|
|
templateClientChangePwd = "changepassword.html"
|
2021-09-04 10:11:04 +00:00
|
|
|
templateClientTwoFactor = "twofactor.html"
|
|
|
|
templateClientTwoFactorRecovery = "twofactor-recovery.html"
|
|
|
|
templateClientMFA = "mfa.html"
|
2021-10-09 12:17:28 +00:00
|
|
|
templateClientEditFile = "editfile.html"
|
2021-11-06 13:13:20 +00:00
|
|
|
templateClientShare = "share.html"
|
|
|
|
templateClientShares = "shares.html"
|
2021-11-25 18:24:32 +00:00
|
|
|
templateClientViewPDF = "viewpdf.html"
|
2022-02-06 15:46:43 +00:00
|
|
|
templateShareFiles = "sharefiles.html"
|
2022-02-15 18:19:25 +00:00
|
|
|
templateUploadToShare = "shareupload.html"
|
2021-09-04 10:11:04 +00:00
|
|
|
pageClientFilesTitle = "My Files"
|
2021-11-06 13:13:20 +00:00
|
|
|
pageClientSharesTitle = "Shares"
|
2021-09-29 16:46:15 +00:00
|
|
|
pageClientProfileTitle = "My Profile"
|
|
|
|
pageClientChangePwdTitle = "Change password"
|
|
|
|
pageClient2FATitle = "Two-factor auth"
|
2021-10-09 12:17:28 +00:00
|
|
|
pageClientEditFileTitle = "Edit file"
|
2021-11-13 12:25:43 +00:00
|
|
|
pageClientForgotPwdTitle = "SFTPGo WebClient - Forgot password"
|
|
|
|
pageClientResetPwdTitle = "SFTPGo WebClient - Reset password"
|
2022-02-06 15:46:43 +00:00
|
|
|
pageExtShareTitle = "Shared files"
|
2022-02-15 18:19:25 +00:00
|
|
|
pageUploadToShareTitle = "Upload to share"
|
2021-05-06 19:35:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// condResult is the result of an HTTP request precondition check.
|
|
|
|
// See https://tools.ietf.org/html/rfc7232 section 3.
|
|
|
|
type condResult int
|
|
|
|
|
|
|
|
const (
|
|
|
|
condNone condResult = iota
|
|
|
|
condTrue
|
|
|
|
condFalse
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
clientTemplates = make(map[string]*template.Template)
|
|
|
|
unixEpochTime = time.Unix(0, 0)
|
|
|
|
)
|
|
|
|
|
|
|
|
// isZeroTime reports whether t is obviously unspecified (either zero or Unix()=0).
|
|
|
|
func isZeroTime(t time.Time) bool {
|
|
|
|
return t.IsZero() || t.Equal(unixEpochTime)
|
|
|
|
}
|
|
|
|
|
|
|
|
type baseClientPage struct {
|
2022-02-13 13:30:20 +00:00
|
|
|
Title string
|
|
|
|
CurrentURL string
|
|
|
|
FilesURL string
|
|
|
|
SharesURL string
|
|
|
|
ShareURL string
|
|
|
|
ProfileURL string
|
|
|
|
ChangePwdURL string
|
|
|
|
StaticURL string
|
|
|
|
LogoutURL string
|
|
|
|
MFAURL string
|
|
|
|
MFATitle string
|
|
|
|
FilesTitle string
|
|
|
|
SharesTitle string
|
|
|
|
ProfileTitle string
|
|
|
|
Version string
|
|
|
|
CSRFToken string
|
|
|
|
HasExternalLogin bool
|
|
|
|
LoggedUser *dataprovider.User
|
2022-03-19 20:44:27 +00:00
|
|
|
ExtraCSS []CustomCSS
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type dirMapping struct {
|
|
|
|
DirName string
|
|
|
|
Href string
|
|
|
|
}
|
|
|
|
|
2021-11-25 18:24:32 +00:00
|
|
|
type viewPDFPage struct {
|
|
|
|
Title string
|
|
|
|
URL string
|
|
|
|
StaticURL string
|
2022-03-19 20:44:27 +00:00
|
|
|
ExtraCSS []CustomCSS
|
2021-11-25 18:24:32 +00:00
|
|
|
}
|
|
|
|
|
2021-10-09 12:17:28 +00:00
|
|
|
type editFilePage struct {
|
|
|
|
baseClientPage
|
|
|
|
CurrentDir string
|
2021-12-08 18:25:22 +00:00
|
|
|
FileURL string
|
2021-10-09 12:17:28 +00:00
|
|
|
Path string
|
|
|
|
Name string
|
2021-11-25 18:24:32 +00:00
|
|
|
ReadOnly bool
|
2021-10-09 12:17:28 +00:00
|
|
|
Data string
|
|
|
|
}
|
|
|
|
|
2021-05-06 19:35:43 +00:00
|
|
|
type filesPage struct {
|
|
|
|
baseClientPage
|
2021-12-03 17:33:08 +00:00
|
|
|
CurrentDir string
|
|
|
|
DirsURL string
|
|
|
|
DownloadURL string
|
|
|
|
ViewPDFURL string
|
2021-12-08 18:25:22 +00:00
|
|
|
FileURL string
|
2021-12-03 17:33:08 +00:00
|
|
|
CanAddFiles bool
|
|
|
|
CanCreateDirs bool
|
|
|
|
CanRename bool
|
|
|
|
CanDelete bool
|
|
|
|
CanDownload bool
|
|
|
|
CanShare bool
|
|
|
|
Error string
|
|
|
|
Paths []dirMapping
|
|
|
|
HasIntegrations bool
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
2022-02-06 15:46:43 +00:00
|
|
|
type shareFilesPage struct {
|
|
|
|
baseClientPage
|
|
|
|
CurrentDir string
|
|
|
|
DirsURL string
|
|
|
|
FilesURL string
|
|
|
|
DownloadURL string
|
|
|
|
Error string
|
|
|
|
Paths []dirMapping
|
|
|
|
}
|
|
|
|
|
2022-02-15 18:19:25 +00:00
|
|
|
type shareUploadPage struct {
|
|
|
|
baseClientPage
|
|
|
|
Share *dataprovider.Share
|
|
|
|
UploadBasePath string
|
|
|
|
}
|
|
|
|
|
2021-05-06 19:35:43 +00:00
|
|
|
type clientMessagePage struct {
|
|
|
|
baseClientPage
|
|
|
|
Error string
|
|
|
|
Success string
|
|
|
|
}
|
|
|
|
|
2021-09-29 16:46:15 +00:00
|
|
|
type clientProfilePage struct {
|
2021-05-06 19:35:43 +00:00
|
|
|
baseClientPage
|
2021-08-17 16:08:32 +00:00
|
|
|
PublicKeys []string
|
2021-09-29 16:46:15 +00:00
|
|
|
CanSubmit bool
|
2021-08-17 16:08:32 +00:00
|
|
|
AllowAPIKeyAuth bool
|
2021-09-29 16:46:15 +00:00
|
|
|
Email string
|
|
|
|
Description string
|
|
|
|
Error string
|
|
|
|
}
|
|
|
|
|
|
|
|
type changeClientPasswordPage struct {
|
|
|
|
baseClientPage
|
|
|
|
Error string
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
2021-09-04 10:11:04 +00:00
|
|
|
type clientMFAPage struct {
|
|
|
|
baseClientPage
|
|
|
|
TOTPConfigs []string
|
2022-01-06 09:11:47 +00:00
|
|
|
TOTPConfig dataprovider.UserTOTPConfig
|
2021-09-04 10:11:04 +00:00
|
|
|
GenerateTOTPURL string
|
|
|
|
ValidateTOTPURL string
|
|
|
|
SaveTOTPURL string
|
|
|
|
RecCodesURL string
|
|
|
|
Protocols []string
|
|
|
|
}
|
|
|
|
|
2021-11-06 13:13:20 +00:00
|
|
|
type clientSharesPage struct {
|
|
|
|
baseClientPage
|
|
|
|
Shares []dataprovider.Share
|
|
|
|
BasePublicSharesURL string
|
|
|
|
}
|
|
|
|
|
|
|
|
type clientSharePage struct {
|
|
|
|
baseClientPage
|
|
|
|
Share *dataprovider.Share
|
|
|
|
Error string
|
|
|
|
IsAdd bool
|
|
|
|
}
|
|
|
|
|
2022-02-06 15:46:43 +00:00
|
|
|
func getFileObjectURL(baseDir, name, baseWebPath string) string {
|
|
|
|
return fmt.Sprintf("%v?path=%v&_=%v", baseWebPath, url.QueryEscape(path.Join(baseDir, name)), time.Now().UTC().Unix())
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getFileObjectModTime(t time.Time) string {
|
|
|
|
if isZeroTime(t) {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return t.Format("2006-01-02 15:04")
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadClientTemplates(templatesPath string) {
|
|
|
|
filesPaths := []string{
|
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientBase),
|
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientFiles),
|
|
|
|
}
|
2021-10-09 12:17:28 +00:00
|
|
|
editFilePath := []string{
|
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientBase),
|
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientEditFile),
|
|
|
|
}
|
2021-11-06 13:13:20 +00:00
|
|
|
sharesPaths := []string{
|
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientBase),
|
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientShares),
|
|
|
|
}
|
|
|
|
sharePaths := []string{
|
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientBase),
|
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientShare),
|
|
|
|
}
|
2021-09-29 16:46:15 +00:00
|
|
|
profilePaths := []string{
|
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientBase),
|
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientProfile),
|
|
|
|
}
|
|
|
|
changePwdPaths := []string{
|
2021-05-06 19:35:43 +00:00
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientBase),
|
2021-09-29 16:46:15 +00:00
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientChangePwd),
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
loginPath := []string{
|
2021-09-04 10:11:04 +00:00
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientBaseLogin),
|
2021-05-06 19:35:43 +00:00
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientLogin),
|
|
|
|
}
|
|
|
|
messagePath := []string{
|
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientBase),
|
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientMessage),
|
|
|
|
}
|
2021-09-04 10:11:04 +00:00
|
|
|
mfaPath := []string{
|
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientBase),
|
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientMFA),
|
|
|
|
}
|
|
|
|
twoFactorPath := []string{
|
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientBaseLogin),
|
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientTwoFactor),
|
|
|
|
}
|
|
|
|
twoFactorRecoveryPath := []string{
|
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientBaseLogin),
|
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientTwoFactorRecovery),
|
|
|
|
}
|
2021-11-13 12:25:43 +00:00
|
|
|
forgotPwdPaths := []string{
|
|
|
|
filepath.Join(templatesPath, templateCommonDir, templateForgotPassword),
|
|
|
|
}
|
|
|
|
resetPwdPaths := []string{
|
|
|
|
filepath.Join(templatesPath, templateCommonDir, templateResetPassword),
|
|
|
|
}
|
2021-11-25 18:24:32 +00:00
|
|
|
viewPDFPaths := []string{
|
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientViewPDF),
|
|
|
|
}
|
2022-02-06 15:46:43 +00:00
|
|
|
shareFilesPath := []string{
|
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientBase),
|
|
|
|
filepath.Join(templatesPath, templateClientDir, templateShareFiles),
|
|
|
|
}
|
2022-02-15 18:19:25 +00:00
|
|
|
shareUploadPath := []string{
|
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientBase),
|
|
|
|
filepath.Join(templatesPath, templateClientDir, templateUploadToShare),
|
|
|
|
}
|
2021-05-06 19:35:43 +00:00
|
|
|
|
2021-07-11 13:26:51 +00:00
|
|
|
filesTmpl := util.LoadTemplate(nil, filesPaths...)
|
2021-09-29 16:46:15 +00:00
|
|
|
profileTmpl := util.LoadTemplate(nil, profilePaths...)
|
|
|
|
changePwdTmpl := util.LoadTemplate(nil, changePwdPaths...)
|
2021-07-11 13:26:51 +00:00
|
|
|
loginTmpl := util.LoadTemplate(nil, loginPath...)
|
|
|
|
messageTmpl := util.LoadTemplate(nil, messagePath...)
|
2021-09-04 10:11:04 +00:00
|
|
|
mfaTmpl := util.LoadTemplate(nil, mfaPath...)
|
|
|
|
twoFactorTmpl := util.LoadTemplate(nil, twoFactorPath...)
|
|
|
|
twoFactorRecoveryTmpl := util.LoadTemplate(nil, twoFactorRecoveryPath...)
|
2021-10-09 12:17:28 +00:00
|
|
|
editFileTmpl := util.LoadTemplate(nil, editFilePath...)
|
2021-11-06 13:13:20 +00:00
|
|
|
sharesTmpl := util.LoadTemplate(nil, sharesPaths...)
|
|
|
|
shareTmpl := util.LoadTemplate(nil, sharePaths...)
|
2021-11-13 12:25:43 +00:00
|
|
|
forgotPwdTmpl := util.LoadTemplate(nil, forgotPwdPaths...)
|
|
|
|
resetPwdTmpl := util.LoadTemplate(nil, resetPwdPaths...)
|
2021-11-25 18:24:32 +00:00
|
|
|
viewPDFTmpl := util.LoadTemplate(nil, viewPDFPaths...)
|
2022-02-06 15:46:43 +00:00
|
|
|
shareFilesTmpl := util.LoadTemplate(nil, shareFilesPath...)
|
2022-02-15 18:19:25 +00:00
|
|
|
shareUploadTmpl := util.LoadTemplate(nil, shareUploadPath...)
|
2021-05-06 19:35:43 +00:00
|
|
|
|
|
|
|
clientTemplates[templateClientFiles] = filesTmpl
|
2021-09-29 16:46:15 +00:00
|
|
|
clientTemplates[templateClientProfile] = profileTmpl
|
|
|
|
clientTemplates[templateClientChangePwd] = changePwdTmpl
|
2021-05-06 19:35:43 +00:00
|
|
|
clientTemplates[templateClientLogin] = loginTmpl
|
|
|
|
clientTemplates[templateClientMessage] = messageTmpl
|
2021-09-04 10:11:04 +00:00
|
|
|
clientTemplates[templateClientMFA] = mfaTmpl
|
|
|
|
clientTemplates[templateClientTwoFactor] = twoFactorTmpl
|
|
|
|
clientTemplates[templateClientTwoFactorRecovery] = twoFactorRecoveryTmpl
|
2021-10-09 12:17:28 +00:00
|
|
|
clientTemplates[templateClientEditFile] = editFileTmpl
|
2021-11-06 13:13:20 +00:00
|
|
|
clientTemplates[templateClientShares] = sharesTmpl
|
|
|
|
clientTemplates[templateClientShare] = shareTmpl
|
2021-11-13 12:25:43 +00:00
|
|
|
clientTemplates[templateForgotPassword] = forgotPwdTmpl
|
|
|
|
clientTemplates[templateResetPassword] = resetPwdTmpl
|
2021-11-25 18:24:32 +00:00
|
|
|
clientTemplates[templateClientViewPDF] = viewPDFTmpl
|
2022-02-06 15:46:43 +00:00
|
|
|
clientTemplates[templateShareFiles] = shareFilesTmpl
|
2022-02-15 18:19:25 +00:00
|
|
|
clientTemplates[templateUploadToShare] = shareUploadTmpl
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) getBaseClientPageData(title, currentURL string, r *http.Request) baseClientPage {
|
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
|
|
|
}
|
|
|
|
v := version.Get()
|
|
|
|
|
|
|
|
return baseClientPage{
|
2022-02-13 13:30:20 +00:00
|
|
|
Title: title,
|
|
|
|
CurrentURL: currentURL,
|
|
|
|
FilesURL: webClientFilesPath,
|
|
|
|
SharesURL: webClientSharesPath,
|
|
|
|
ShareURL: webClientSharePath,
|
|
|
|
ProfileURL: webClientProfilePath,
|
|
|
|
ChangePwdURL: webChangeClientPwdPath,
|
|
|
|
StaticURL: webStaticFilesPath,
|
|
|
|
LogoutURL: webClientLogoutPath,
|
|
|
|
MFAURL: webClientMFAPath,
|
|
|
|
MFATitle: pageClient2FATitle,
|
|
|
|
FilesTitle: pageClientFilesTitle,
|
|
|
|
SharesTitle: pageClientSharesTitle,
|
|
|
|
ProfileTitle: pageClientProfileTitle,
|
|
|
|
Version: fmt.Sprintf("%v-%v", v.Version, v.CommitHash),
|
|
|
|
CSRFToken: csrfToken,
|
|
|
|
HasExternalLogin: isLoggedInWithOIDC(r),
|
|
|
|
LoggedUser: getUserFromToken(r),
|
2022-03-19 20:44:27 +00:00
|
|
|
ExtraCSS: s.binding.ExtraCSS,
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-26 07:41:50 +00:00
|
|
|
func (s *httpdServer) renderClientForgotPwdPage(w http.ResponseWriter, error, ip string) {
|
2021-11-13 12:25:43 +00:00
|
|
|
data := forgotPwdPage{
|
|
|
|
CurrentURL: webClientForgotPwdPath,
|
|
|
|
Error: error,
|
2022-03-26 07:41:50 +00:00
|
|
|
CSRFToken: createCSRFToken(ip),
|
2021-11-13 12:25:43 +00:00
|
|
|
StaticURL: webStaticFilesPath,
|
|
|
|
Title: pageClientForgotPwdTitle,
|
2022-03-19 20:44:27 +00:00
|
|
|
ExtraCSS: s.binding.ExtraCSS,
|
2021-11-13 12:25:43 +00:00
|
|
|
}
|
|
|
|
renderClientTemplate(w, templateForgotPassword, data)
|
|
|
|
}
|
|
|
|
|
2022-03-26 07:41:50 +00:00
|
|
|
func (s *httpdServer) renderClientResetPwdPage(w http.ResponseWriter, error, ip string) {
|
2021-11-13 12:25:43 +00:00
|
|
|
data := resetPwdPage{
|
|
|
|
CurrentURL: webClientResetPwdPath,
|
|
|
|
Error: error,
|
2022-03-26 07:41:50 +00:00
|
|
|
CSRFToken: createCSRFToken(ip),
|
2021-11-13 12:25:43 +00:00
|
|
|
StaticURL: webStaticFilesPath,
|
|
|
|
Title: pageClientResetPwdTitle,
|
2022-03-19 20:44:27 +00:00
|
|
|
ExtraCSS: s.binding.ExtraCSS,
|
2021-11-13 12:25:43 +00:00
|
|
|
}
|
|
|
|
renderClientTemplate(w, templateResetPassword, data)
|
|
|
|
}
|
|
|
|
|
2021-05-06 19:35:43 +00:00
|
|
|
func renderClientTemplate(w http.ResponseWriter, tmplName string, data interface{}) {
|
|
|
|
err := clientTemplates[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) renderClientMessagePage(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 := clientMessagePage{
|
2022-03-19 20:44:27 +00:00
|
|
|
baseClientPage: s.getBaseClientPageData(title, "", r),
|
2021-05-06 19:35:43 +00:00
|
|
|
Error: errorString,
|
|
|
|
Success: message,
|
|
|
|
}
|
|
|
|
w.WriteHeader(statusCode)
|
|
|
|
renderClientTemplate(w, templateClientMessage, data)
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) renderClientInternalServerErrorPage(w http.ResponseWriter, r *http.Request, err error) {
|
|
|
|
s.renderClientMessagePage(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) renderClientBadRequestPage(w http.ResponseWriter, r *http.Request, err error) {
|
|
|
|
s.renderClientMessagePage(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) renderClientForbiddenPage(w http.ResponseWriter, r *http.Request, body string) {
|
|
|
|
s.renderClientMessagePage(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) renderClientNotFoundPage(w http.ResponseWriter, r *http.Request, err error) {
|
|
|
|
s.renderClientMessagePage(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) renderClientTwoFactorPage(w http.ResponseWriter, error, ip string) {
|
2021-09-04 10:11:04 +00:00
|
|
|
data := twoFactorPage{
|
|
|
|
CurrentURL: webClientTwoFactorPath,
|
|
|
|
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: webClientTwoFactorRecoveryPath,
|
2022-03-19 20:44:27 +00:00
|
|
|
ExtraCSS: s.binding.ExtraCSS,
|
2021-09-04 10:11:04 +00:00
|
|
|
}
|
|
|
|
renderClientTemplate(w, templateTwoFactor, data)
|
|
|
|
}
|
|
|
|
|
2022-03-26 07:41:50 +00:00
|
|
|
func (s *httpdServer) renderClientTwoFactorRecoveryPage(w http.ResponseWriter, error, ip string) {
|
2021-09-04 10:11:04 +00:00
|
|
|
data := twoFactorPage{
|
|
|
|
CurrentURL: webClientTwoFactorRecoveryPath,
|
|
|
|
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-03-19 20:44:27 +00:00
|
|
|
ExtraCSS: s.binding.ExtraCSS,
|
2021-09-04 10:11:04 +00:00
|
|
|
}
|
|
|
|
renderClientTemplate(w, templateTwoFactorRecovery, data)
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) renderClientMFAPage(w http.ResponseWriter, r *http.Request) {
|
2021-09-04 10:11:04 +00:00
|
|
|
data := clientMFAPage{
|
2022-03-19 20:44:27 +00:00
|
|
|
baseClientPage: s.getBaseClientPageData(pageMFATitle, webClientMFAPath, r),
|
2021-09-04 10:11:04 +00:00
|
|
|
TOTPConfigs: mfa.GetAvailableTOTPConfigNames(),
|
|
|
|
GenerateTOTPURL: webClientTOTPGeneratePath,
|
|
|
|
ValidateTOTPURL: webClientTOTPValidatePath,
|
|
|
|
SaveTOTPURL: webClientTOTPSavePath,
|
|
|
|
RecCodesURL: webClientRecoveryCodesPath,
|
|
|
|
Protocols: dataprovider.MFAProtocols,
|
|
|
|
}
|
|
|
|
user, err := dataprovider.UserExists(data.LoggedUser.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 = user.Filters.TOTPConfig
|
|
|
|
renderClientTemplate(w, templateClientMFA, data)
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) renderEditFilePage(w http.ResponseWriter, r *http.Request, fileName, fileData string, readOnly bool) {
|
2021-10-09 12:17:28 +00:00
|
|
|
data := editFilePage{
|
2022-03-19 20:44:27 +00:00
|
|
|
baseClientPage: s.getBaseClientPageData(pageClientEditFileTitle, webClientEditFilePath, r),
|
2021-10-09 12:17:28 +00:00
|
|
|
Path: fileName,
|
|
|
|
Name: path.Base(fileName),
|
|
|
|
CurrentDir: path.Dir(fileName),
|
2021-12-08 18:25:22 +00:00
|
|
|
FileURL: webClientFilePath,
|
2021-11-25 18:24:32 +00:00
|
|
|
ReadOnly: readOnly,
|
2021-10-09 12:17:28 +00:00
|
|
|
Data: fileData,
|
|
|
|
}
|
|
|
|
|
|
|
|
renderClientTemplate(w, templateClientEditFile, data)
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) renderAddUpdateSharePage(w http.ResponseWriter, r *http.Request, share *dataprovider.Share,
|
2021-11-06 13:13:20 +00:00
|
|
|
error string, isAdd bool) {
|
|
|
|
currentURL := webClientSharePath
|
|
|
|
title := "Add a new share"
|
|
|
|
if !isAdd {
|
|
|
|
currentURL = fmt.Sprintf("%v/%v", webClientSharePath, url.PathEscape(share.ShareID))
|
|
|
|
title = "Update share"
|
|
|
|
}
|
|
|
|
data := clientSharePage{
|
2022-03-19 20:44:27 +00:00
|
|
|
baseClientPage: s.getBaseClientPageData(title, currentURL, r),
|
2021-11-06 13:13:20 +00:00
|
|
|
Share: share,
|
|
|
|
Error: error,
|
|
|
|
IsAdd: isAdd,
|
|
|
|
}
|
|
|
|
|
|
|
|
renderClientTemplate(w, templateClientShare, data)
|
|
|
|
}
|
|
|
|
|
2022-02-06 15:46:43 +00:00
|
|
|
func getDirMapping(dirName, baseWebPath string) []dirMapping {
|
|
|
|
paths := []dirMapping{}
|
|
|
|
if dirName != "/" {
|
|
|
|
paths = append(paths, dirMapping{
|
|
|
|
DirName: path.Base(dirName),
|
|
|
|
Href: "",
|
|
|
|
})
|
|
|
|
for {
|
|
|
|
dirName = path.Dir(dirName)
|
|
|
|
if dirName == "/" || dirName == "." {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
paths = append([]dirMapping{{
|
|
|
|
DirName: path.Base(dirName),
|
|
|
|
Href: getFileObjectURL("/", dirName, baseWebPath)},
|
|
|
|
}, paths...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return paths
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) renderSharedFilesPage(w http.ResponseWriter, r *http.Request, dirName, error string,
|
|
|
|
share dataprovider.Share,
|
|
|
|
) {
|
2022-02-06 15:46:43 +00:00
|
|
|
currentURL := path.Join(webClientPubSharesPath, share.ShareID, "browse")
|
|
|
|
data := shareFilesPage{
|
2022-03-19 20:44:27 +00:00
|
|
|
baseClientPage: s.getBaseClientPageData(pageExtShareTitle, currentURL, r),
|
2022-02-06 15:46:43 +00:00
|
|
|
CurrentDir: url.QueryEscape(dirName),
|
|
|
|
DirsURL: path.Join(webClientPubSharesPath, share.ShareID, "dirs"),
|
|
|
|
FilesURL: currentURL,
|
|
|
|
DownloadURL: path.Join(webClientPubSharesPath, share.ShareID),
|
|
|
|
Error: error,
|
|
|
|
Paths: getDirMapping(dirName, currentURL),
|
|
|
|
}
|
|
|
|
renderClientTemplate(w, templateShareFiles, data)
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) renderUploadToSharePage(w http.ResponseWriter, r *http.Request, share dataprovider.Share) {
|
2022-02-15 18:19:25 +00:00
|
|
|
currentURL := path.Join(webClientPubSharesPath, share.ShareID, "upload")
|
|
|
|
data := shareUploadPage{
|
2022-03-19 20:44:27 +00:00
|
|
|
baseClientPage: s.getBaseClientPageData(pageUploadToShareTitle, currentURL, r),
|
2022-02-15 18:19:25 +00:00
|
|
|
Share: &share,
|
|
|
|
UploadBasePath: path.Join(webClientPubSharesPath, share.ShareID),
|
|
|
|
}
|
|
|
|
renderClientTemplate(w, templateUploadToShare, data)
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) renderFilesPage(w http.ResponseWriter, r *http.Request, dirName, error string, user dataprovider.User,
|
2021-12-03 17:33:08 +00:00
|
|
|
hasIntegrations bool,
|
|
|
|
) {
|
2021-05-06 19:35:43 +00:00
|
|
|
data := filesPage{
|
2022-03-19 20:44:27 +00:00
|
|
|
baseClientPage: s.getBaseClientPageData(pageClientFilesTitle, webClientFilesPath, r),
|
2021-12-03 17:33:08 +00:00
|
|
|
Error: error,
|
|
|
|
CurrentDir: url.QueryEscape(dirName),
|
|
|
|
DownloadURL: webClientDownloadZipPath,
|
|
|
|
ViewPDFURL: webClientViewPDFPath,
|
|
|
|
DirsURL: webClientDirsPath,
|
2021-12-08 18:25:22 +00:00
|
|
|
FileURL: webClientFilePath,
|
2021-12-03 17:33:08 +00:00
|
|
|
CanAddFiles: user.CanAddFilesFromWeb(dirName),
|
|
|
|
CanCreateDirs: user.CanAddDirsFromWeb(dirName),
|
|
|
|
CanRename: user.CanRenameFromWeb(dirName, dirName),
|
|
|
|
CanDelete: user.CanDeleteFromWeb(dirName),
|
|
|
|
CanDownload: user.HasPerm(dataprovider.PermDownload, dirName),
|
|
|
|
CanShare: user.CanManageShares(),
|
|
|
|
HasIntegrations: hasIntegrations,
|
2022-02-06 15:46:43 +00:00
|
|
|
Paths: getDirMapping(dirName, webClientFilesPath),
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
renderClientTemplate(w, templateClientFiles, data)
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) renderClientProfilePage(w http.ResponseWriter, r *http.Request, error string) {
|
2021-09-29 16:46:15 +00:00
|
|
|
data := clientProfilePage{
|
2022-03-19 20:44:27 +00:00
|
|
|
baseClientPage: s.getBaseClientPageData(pageClientProfileTitle, webClientProfilePath, r),
|
2021-09-29 16:46:15 +00:00
|
|
|
Error: error,
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
user, err := dataprovider.UserExists(data.LoggedUser.Username)
|
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientInternalServerErrorPage(w, r, err)
|
2021-08-17 16:08:32 +00:00
|
|
|
return
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
data.PublicKeys = user.PublicKeys
|
2021-08-17 16:08:32 +00:00
|
|
|
data.AllowAPIKeyAuth = user.Filters.AllowAPIKeyAuth
|
2021-09-29 16:46:15 +00:00
|
|
|
data.Email = user.Email
|
|
|
|
data.Description = user.Description
|
|
|
|
data.CanSubmit = user.CanChangeAPIKeyAuth() || user.CanManagePublicKeys() || user.CanChangeInfo()
|
|
|
|
renderClientTemplate(w, templateClientProfile, data)
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) renderClientChangePasswordPage(w http.ResponseWriter, r *http.Request, error string) {
|
2021-09-29 16:46:15 +00:00
|
|
|
data := changeClientPasswordPage{
|
2022-03-19 20:44:27 +00:00
|
|
|
baseClientPage: s.getBaseClientPageData(pageClientChangePwdTitle, webChangeClientPwdPath, r),
|
2021-09-29 16:46:15 +00:00
|
|
|
Error: error,
|
|
|
|
}
|
|
|
|
|
|
|
|
renderClientTemplate(w, templateClientChangePwd, data)
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebClientDownloadZip(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2021-05-30 21:07:46 +00:00
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientMessagePage(w, r, "Invalid token claims", "", http.StatusForbidden, nil, "")
|
2021-05-30 21:07:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := dataprovider.UserExists(claims.Username)
|
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientMessagePage(w, r, "Unable to retrieve your user", "", getRespStatus(err), nil, "")
|
2021-05-30 21:07:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-05 14:07:09 +00:00
|
|
|
connID := xid.New().String()
|
2022-02-19 09:53:35 +00:00
|
|
|
protocol := getProtocolFromRequest(r)
|
|
|
|
connectionID := fmt.Sprintf("%v_%v", protocol, connID)
|
2022-04-14 17:07:41 +00:00
|
|
|
if err := checkHTTPClientUser(&user, r, connectionID, false); err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientForbiddenPage(w, r, err.Error())
|
2021-06-05 14:07:09 +00:00
|
|
|
return
|
|
|
|
}
|
2021-05-30 21:07:46 +00:00
|
|
|
connection := &Connection{
|
2022-02-19 09:53:35 +00:00
|
|
|
BaseConnection: common.NewBaseConnection(connID, protocol, util.GetHTTPLocalAddress(r),
|
2021-07-24 18:11:17 +00:00
|
|
|
r.RemoteAddr, user),
|
|
|
|
request: r,
|
2021-05-30 21:07:46 +00:00
|
|
|
}
|
2022-04-14 17:07:41 +00:00
|
|
|
if err = common.Connections.Add(connection); err != nil {
|
|
|
|
s.renderClientMessagePage(w, r, "Unable to add connection", "", http.StatusTooManyRequests, err, "")
|
|
|
|
return
|
|
|
|
}
|
2021-05-30 21:07:46 +00:00
|
|
|
defer common.Connections.Remove(connection.GetID())
|
|
|
|
|
2022-03-03 11:44:56 +00:00
|
|
|
name := connection.User.GetCleanedPath(r.URL.Query().Get("path"))
|
2021-05-30 21:07:46 +00:00
|
|
|
files := r.URL.Query().Get("files")
|
|
|
|
var filesList []string
|
|
|
|
err = json.Unmarshal([]byte(files), &filesList)
|
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientMessagePage(w, r, "Unable to get files list", "", http.StatusInternalServerError, err, "")
|
2021-05-30 21:07:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Disposition", "attachment; filename=\"sftpgo-download.zip\"")
|
2021-11-06 13:13:20 +00:00
|
|
|
renderCompressedFiles(w, connection, name, filesList, nil)
|
2021-05-30 21:07:46 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleShareGetDirContents(w http.ResponseWriter, r *http.Request) {
|
2022-02-06 15:46:43 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2022-03-19 20:44:27 +00:00
|
|
|
share, connection, err := s.checkPublicShare(w, r, dataprovider.ShareScopeRead, true)
|
2022-02-06 15:46:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := validateBrowsableShare(share, connection); err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientMessagePage(w, r, "Unable to validate share", "", getRespStatus(err), err, "")
|
2022-02-06 15:46:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
name, err := getBrowsableSharedPath(share, r)
|
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientMessagePage(w, r, "Invalid share path", "", getRespStatus(err), err, "")
|
2022-02-06 15:46:43 +00:00
|
|
|
return
|
|
|
|
}
|
2022-04-14 17:07:41 +00:00
|
|
|
if err = common.Connections.Add(connection); err != nil {
|
|
|
|
s.renderClientMessagePage(w, r, "Unable to add connection", "", http.StatusTooManyRequests, err, "")
|
|
|
|
return
|
|
|
|
}
|
2022-02-06 15:46:43 +00:00
|
|
|
defer common.Connections.Remove(connection.GetID())
|
|
|
|
|
|
|
|
contents, err := connection.ReadDir(name)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "Unable to get directory contents", getMappedStatusCode(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
results := make([]map[string]string, 0, len(contents))
|
|
|
|
for _, info := range contents {
|
|
|
|
if !info.Mode().IsDir() && !info.Mode().IsRegular() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
res := make(map[string]string)
|
|
|
|
if info.IsDir() {
|
|
|
|
res["type"] = "1"
|
|
|
|
res["size"] = ""
|
|
|
|
} else {
|
|
|
|
res["type"] = "2"
|
|
|
|
res["size"] = util.ByteCountIEC(info.Size())
|
|
|
|
}
|
|
|
|
res["name"] = info.Name()
|
|
|
|
res["url"] = getFileObjectURL(share.GetRelativePath(name), info.Name(),
|
|
|
|
path.Join(webClientPubSharesPath, share.ShareID, "browse"))
|
|
|
|
res["last_modified"] = getFileObjectModTime(info.ModTime())
|
|
|
|
results = append(results, res)
|
|
|
|
}
|
|
|
|
|
|
|
|
render.JSON(w, r, results)
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleClientUploadToShare(w http.ResponseWriter, r *http.Request) {
|
2022-02-15 18:19:25 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2022-03-19 20:44:27 +00:00
|
|
|
share, _, err := s.checkPublicShare(w, r, dataprovider.ShareScopeWrite, true)
|
2022-02-15 18:19:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderUploadToSharePage(w, r, share)
|
2022-02-15 18:19:25 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleShareGetFiles(w http.ResponseWriter, r *http.Request) {
|
2022-02-06 15:46:43 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2022-03-19 20:44:27 +00:00
|
|
|
share, connection, err := s.checkPublicShare(w, r, dataprovider.ShareScopeRead, true)
|
2022-02-06 15:46:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := validateBrowsableShare(share, connection); err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientMessagePage(w, r, "Unable to validate share", "", getRespStatus(err), err, "")
|
2022-02-06 15:46:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
name, err := getBrowsableSharedPath(share, r)
|
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientMessagePage(w, r, "Invalid share path", "", getRespStatus(err), err, "")
|
2022-02-06 15:46:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-14 17:07:41 +00:00
|
|
|
if err = common.Connections.Add(connection); err != nil {
|
|
|
|
s.renderClientMessagePage(w, r, "Unable to add connection", "", http.StatusTooManyRequests, err, "")
|
|
|
|
return
|
|
|
|
}
|
2022-02-06 15:46:43 +00:00
|
|
|
defer common.Connections.Remove(connection.GetID())
|
|
|
|
|
|
|
|
var info os.FileInfo
|
|
|
|
if name == "/" {
|
|
|
|
info = vfs.NewFileInfo(name, true, 0, time.Now(), false)
|
|
|
|
} else {
|
|
|
|
info, err = connection.Stat(name, 1)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderSharedFilesPage(w, r, path.Dir(share.GetRelativePath(name)), err.Error(), share)
|
2022-02-06 15:46:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if info.IsDir() {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderSharedFilesPage(w, r, share.GetRelativePath(name), "", share)
|
2022-02-06 15:46:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
inline := r.URL.Query().Get("inline") != ""
|
|
|
|
dataprovider.UpdateShareLastUse(&share, 1) //nolint:errcheck
|
|
|
|
if status, err := downloadFile(w, r, connection, name, info, inline, &share); err != nil {
|
|
|
|
dataprovider.UpdateShareLastUse(&share, -1) //nolint:errcheck
|
|
|
|
if status > 0 {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderSharedFilesPage(w, r, path.Dir(share.GetRelativePath(name)), err.Error(), share)
|
2022-02-06 15:46:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-03 17:33:08 +00:00
|
|
|
func (s *httpdServer) handleClientGetDirContents(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2021-05-27 07:40:46 +00:00
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
|
|
|
sendAPIResponse(w, r, nil, "invalid token claims", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := dataprovider.UserExists(claims.Username)
|
|
|
|
if err != nil {
|
2021-06-05 14:07:09 +00:00
|
|
|
sendAPIResponse(w, r, nil, "Unable to retrieve your user", getRespStatus(err))
|
2021-05-27 07:40:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-05 14:07:09 +00:00
|
|
|
connID := xid.New().String()
|
2022-02-19 09:53:35 +00:00
|
|
|
protocol := getProtocolFromRequest(r)
|
|
|
|
connectionID := fmt.Sprintf("%v_%v", protocol, connID)
|
2022-04-14 17:07:41 +00:00
|
|
|
if err := checkHTTPClientUser(&user, r, connectionID, false); err != nil {
|
2021-06-05 14:07:09 +00:00
|
|
|
sendAPIResponse(w, r, err, http.StatusText(http.StatusForbidden), http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
2021-05-27 07:40:46 +00:00
|
|
|
connection := &Connection{
|
2022-02-19 09:53:35 +00:00
|
|
|
BaseConnection: common.NewBaseConnection(connID, protocol, util.GetHTTPLocalAddress(r),
|
2021-07-24 18:11:17 +00:00
|
|
|
r.RemoteAddr, user),
|
|
|
|
request: r,
|
2021-05-27 07:40:46 +00:00
|
|
|
}
|
2022-04-14 17:07:41 +00:00
|
|
|
if err = common.Connections.Add(connection); err != nil {
|
|
|
|
s.renderClientMessagePage(w, r, "Unable to add connection", "", http.StatusTooManyRequests, err, "")
|
|
|
|
return
|
|
|
|
}
|
2021-05-27 07:40:46 +00:00
|
|
|
defer common.Connections.Remove(connection.GetID())
|
|
|
|
|
2022-03-03 11:44:56 +00:00
|
|
|
name := connection.User.GetCleanedPath(r.URL.Query().Get("path"))
|
2021-05-27 07:40:46 +00:00
|
|
|
contents, err := connection.ReadDir(name)
|
|
|
|
if err != nil {
|
2021-06-05 14:07:09 +00:00
|
|
|
sendAPIResponse(w, r, err, "Unable to get directory contents", getMappedStatusCode(err))
|
2021-05-27 07:40:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
results := make([]map[string]string, 0, len(contents))
|
|
|
|
for _, info := range contents {
|
|
|
|
res := make(map[string]string)
|
2022-02-06 15:46:43 +00:00
|
|
|
res["url"] = getFileObjectURL(name, info.Name(), webClientFilesPath)
|
2021-05-27 07:40:46 +00:00
|
|
|
if info.IsDir() {
|
|
|
|
res["type"] = "1"
|
|
|
|
res["size"] = ""
|
|
|
|
} else {
|
|
|
|
res["type"] = "2"
|
|
|
|
if info.Mode()&os.ModeSymlink != 0 {
|
|
|
|
res["size"] = ""
|
|
|
|
} else {
|
2021-07-11 13:26:51 +00:00
|
|
|
res["size"] = util.ByteCountIEC(info.Size())
|
2021-10-09 12:17:28 +00:00
|
|
|
if info.Size() < httpdMaxEditFileSize {
|
2021-12-03 17:33:08 +00:00
|
|
|
res["edit_url"] = strings.Replace(res["url"], webClientFilesPath, webClientEditFilePath, 1)
|
|
|
|
}
|
|
|
|
if len(s.binding.WebClientIntegrations) > 0 {
|
|
|
|
extension := path.Ext(info.Name())
|
|
|
|
for idx := range s.binding.WebClientIntegrations {
|
|
|
|
if util.IsStringInSlice(extension, s.binding.WebClientIntegrations[idx].FileExtensions) {
|
|
|
|
res["ext_url"] = s.binding.WebClientIntegrations[idx].URL
|
2021-12-08 18:25:22 +00:00
|
|
|
res["ext_link"] = fmt.Sprintf("%v?path=%v&_=%v", webClientFilePath,
|
|
|
|
url.QueryEscape(path.Join(name, info.Name())), time.Now().UTC().Unix())
|
2021-12-03 17:33:08 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-10-09 12:17:28 +00:00
|
|
|
}
|
2021-05-27 07:40:46 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-09 12:17:28 +00:00
|
|
|
res["meta"] = fmt.Sprintf("%v_%v", res["type"], info.Name())
|
2021-05-27 07:40:46 +00:00
|
|
|
res["name"] = info.Name()
|
|
|
|
res["last_modified"] = getFileObjectModTime(info.ModTime())
|
|
|
|
results = append(results, res)
|
|
|
|
}
|
|
|
|
|
|
|
|
render.JSON(w, r, results)
|
|
|
|
}
|
|
|
|
|
2021-12-03 17:33:08 +00:00
|
|
|
func (s *httpdServer) handleClientGetFiles(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
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientForbiddenPage(w, r, "Invalid token claims")
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := dataprovider.UserExists(claims.Username)
|
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientMessagePage(w, r, "Unable to retrieve your user", "", getRespStatus(err), nil, "")
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
connID := xid.New().String()
|
2022-02-19 09:53:35 +00:00
|
|
|
protocol := getProtocolFromRequest(r)
|
|
|
|
connectionID := fmt.Sprintf("%v_%v", protocol, connID)
|
2022-04-14 17:07:41 +00:00
|
|
|
if err := checkHTTPClientUser(&user, r, connectionID, false); err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientForbiddenPage(w, r, err.Error())
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
connection := &Connection{
|
2022-02-19 09:53:35 +00:00
|
|
|
BaseConnection: common.NewBaseConnection(connID, protocol, util.GetHTTPLocalAddress(r),
|
2021-07-24 18:11:17 +00:00
|
|
|
r.RemoteAddr, user),
|
|
|
|
request: r,
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
2022-04-14 17:07:41 +00:00
|
|
|
if err = common.Connections.Add(connection); err != nil {
|
|
|
|
s.renderClientMessagePage(w, r, "Unable to add connection", "", http.StatusTooManyRequests, err, "")
|
|
|
|
return
|
|
|
|
}
|
2021-05-06 19:35:43 +00:00
|
|
|
defer common.Connections.Remove(connection.GetID())
|
|
|
|
|
2022-03-03 11:44:56 +00:00
|
|
|
name := connection.User.GetCleanedPath(r.URL.Query().Get("path"))
|
2021-05-06 19:35:43 +00:00
|
|
|
var info os.FileInfo
|
|
|
|
if name == "/" {
|
|
|
|
info = vfs.NewFileInfo(name, true, 0, time.Now(), false)
|
|
|
|
} else {
|
|
|
|
info, err = connection.Stat(name, 0)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderFilesPage(w, r, path.Dir(name), fmt.Sprintf("unable to stat file %#v: %v", name, err),
|
2021-12-03 17:33:08 +00:00
|
|
|
user, len(s.binding.WebClientIntegrations) > 0)
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if info.IsDir() {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderFilesPage(w, r, name, "", user, len(s.binding.WebClientIntegrations) > 0)
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
2021-11-25 18:24:32 +00:00
|
|
|
inline := r.URL.Query().Get("inline") != ""
|
2022-02-06 15:46:43 +00:00
|
|
|
if status, err := downloadFile(w, r, connection, name, info, inline, nil); err != nil && status != 0 {
|
2021-06-05 14:07:09 +00:00
|
|
|
if status > 0 {
|
|
|
|
if status == http.StatusRequestedRangeNotSatisfiable {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientMessagePage(w, r, http.StatusText(status), "", status, err, "")
|
2021-06-05 14:07:09 +00:00
|
|
|
return
|
|
|
|
}
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderFilesPage(w, r, path.Dir(name), err.Error(), user, len(s.binding.WebClientIntegrations) > 0)
|
2021-06-05 14:07:09 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleClientEditFile(w http.ResponseWriter, r *http.Request) {
|
2021-10-09 12:17:28 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientForbiddenPage(w, r, "Invalid token claims")
|
2021-10-09 12:17:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := dataprovider.UserExists(claims.Username)
|
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientMessagePage(w, r, "Unable to retrieve your user", "", getRespStatus(err), nil, "")
|
2021-10-09 12:17:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
connID := xid.New().String()
|
2022-02-19 09:53:35 +00:00
|
|
|
protocol := getProtocolFromRequest(r)
|
|
|
|
connectionID := fmt.Sprintf("%v_%v", protocol, connID)
|
2022-04-14 17:07:41 +00:00
|
|
|
if err := checkHTTPClientUser(&user, r, connectionID, false); err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientForbiddenPage(w, r, err.Error())
|
2021-10-09 12:17:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
connection := &Connection{
|
2022-02-19 09:53:35 +00:00
|
|
|
BaseConnection: common.NewBaseConnection(connID, protocol, util.GetHTTPLocalAddress(r),
|
2021-10-09 12:17:28 +00:00
|
|
|
r.RemoteAddr, user),
|
|
|
|
request: r,
|
|
|
|
}
|
2022-04-14 17:07:41 +00:00
|
|
|
if err = common.Connections.Add(connection); err != nil {
|
|
|
|
s.renderClientMessagePage(w, r, "Unable to add connection", "", http.StatusTooManyRequests, err, "")
|
|
|
|
return
|
|
|
|
}
|
2021-10-09 12:17:28 +00:00
|
|
|
defer common.Connections.Remove(connection.GetID())
|
|
|
|
|
2022-03-03 11:44:56 +00:00
|
|
|
name := connection.User.GetCleanedPath(r.URL.Query().Get("path"))
|
2021-10-09 12:17:28 +00:00
|
|
|
info, err := connection.Stat(name, 0)
|
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientMessagePage(w, r, fmt.Sprintf("Unable to stat file %#v", name), "",
|
2021-10-09 12:17:28 +00:00
|
|
|
getRespStatus(err), nil, "")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if info.IsDir() {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientMessagePage(w, r, fmt.Sprintf("The path %#v does not point to a file", name), "",
|
2021-10-09 12:17:28 +00:00
|
|
|
http.StatusBadRequest, nil, "")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if info.Size() > httpdMaxEditFileSize {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientMessagePage(w, r, fmt.Sprintf("The file size %v for %#v exceeds the maximum allowed size",
|
2021-10-09 12:17:28 +00:00
|
|
|
util.ByteCountIEC(info.Size()), name), "", http.StatusBadRequest, nil, "")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
reader, err := connection.getFileReader(name, 0, r.Method)
|
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientMessagePage(w, r, fmt.Sprintf("Unable to get a reader for the file %#v", name), "",
|
2021-10-09 12:17:28 +00:00
|
|
|
getRespStatus(err), nil, "")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer reader.Close()
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
_, err = io.Copy(&b, reader)
|
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientMessagePage(w, r, fmt.Sprintf("Unable to read the file %#v", name), "", http.StatusInternalServerError,
|
2021-10-09 12:17:28 +00:00
|
|
|
nil, "")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderEditFilePage(w, r, name, b.String(), util.IsStringInSlice(sdk.WebClientWriteDisabled, user.Filters.WebClient))
|
2021-10-09 12:17:28 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleClientAddShareGet(w http.ResponseWriter, r *http.Request) {
|
2021-11-06 13:13:20 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
share := &dataprovider.Share{Scope: dataprovider.ShareScopeRead}
|
|
|
|
dirName := "/"
|
|
|
|
if _, ok := r.URL.Query()["path"]; ok {
|
|
|
|
dirName = util.CleanPath(r.URL.Query().Get("path"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := r.URL.Query()["files"]; ok {
|
|
|
|
files := r.URL.Query().Get("files")
|
|
|
|
var filesList []string
|
|
|
|
err := json.Unmarshal([]byte(files), &filesList)
|
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientMessagePage(w, r, "Invalid share list", "", http.StatusBadRequest, err, "")
|
2021-11-06 13:13:20 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, f := range filesList {
|
|
|
|
if f != "" {
|
|
|
|
share.Paths = append(share.Paths, path.Join(dirName, f))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderAddUpdateSharePage(w, r, share, "", true)
|
2021-11-06 13:13:20 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleClientUpdateShareGet(w http.ResponseWriter, r *http.Request) {
|
2021-11-06 13:13:20 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientForbiddenPage(w, r, "Invalid token claims")
|
2021-11-06 13:13:20 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
shareID := getURLParam(r, "id")
|
|
|
|
share, err := dataprovider.ShareExists(shareID, claims.Username)
|
|
|
|
if err == nil {
|
|
|
|
share.HideConfidentialData()
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderAddUpdateSharePage(w, r, &share, "", false)
|
2021-11-06 13:13:20 +00:00
|
|
|
} else if _, ok := err.(*util.RecordNotFoundError); ok {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientNotFoundPage(w, r, err)
|
2021-11-06 13:13:20 +00:00
|
|
|
} else {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientInternalServerErrorPage(w, r, err)
|
2021-11-06 13:13:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleClientAddSharePost(w http.ResponseWriter, r *http.Request) {
|
2021-11-06 13:13:20 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientForbiddenPage(w, r, "Invalid token claims")
|
2021-11-06 13:13:20 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
share, err := getShareFromPostFields(r)
|
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderAddUpdateSharePage(w, r, share, err.Error(), true)
|
2021-11-06 13:13:20 +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.renderClientForbiddenPage(w, r, err.Error())
|
2021-11-06 13:13:20 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
share.ID = 0
|
|
|
|
share.ShareID = util.GenerateUniqueID()
|
|
|
|
share.LastUseAt = 0
|
|
|
|
share.Username = claims.Username
|
2022-02-19 12:31:58 +00:00
|
|
|
if share.Password == "" {
|
|
|
|
if util.IsStringInSlice(sdk.WebClientShareNoPasswordDisabled, claims.Permissions) {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientForbiddenPage(w, r, "You are not authorized to share files/folders without a password")
|
2022-02-19 12:31:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2022-03-26 07:41:50 +00:00
|
|
|
err = dataprovider.AddShare(share, claims.Username, ipAddr)
|
2021-11-06 13:13:20 +00:00
|
|
|
if err == nil {
|
|
|
|
http.Redirect(w, r, webClientSharesPath, http.StatusSeeOther)
|
|
|
|
} else {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderAddUpdateSharePage(w, r, share, err.Error(), true)
|
2021-11-06 13:13:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleClientUpdateSharePost(w http.ResponseWriter, r *http.Request) {
|
2021-11-06 13:13:20 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientForbiddenPage(w, r, "Invalid token claims")
|
2021-11-06 13:13:20 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
shareID := getURLParam(r, "id")
|
|
|
|
share, err := dataprovider.ShareExists(shareID, claims.Username)
|
|
|
|
if _, ok := err.(*util.RecordNotFoundError); ok {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientNotFoundPage(w, r, err)
|
2021-11-06 13:13:20 +00:00
|
|
|
return
|
|
|
|
} else if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientInternalServerErrorPage(w, r, err)
|
2021-11-06 13:13:20 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
updatedShare, err := getShareFromPostFields(r)
|
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderAddUpdateSharePage(w, r, updatedShare, err.Error(), false)
|
2021-11-06 13:13:20 +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.renderClientForbiddenPage(w, r, err.Error())
|
2021-11-06 13:13:20 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
updatedShare.ShareID = shareID
|
|
|
|
updatedShare.Username = claims.Username
|
|
|
|
if updatedShare.Password == redactedSecret {
|
|
|
|
updatedShare.Password = share.Password
|
|
|
|
}
|
2022-02-19 12:31:58 +00:00
|
|
|
if updatedShare.Password == "" {
|
|
|
|
if util.IsStringInSlice(sdk.WebClientShareNoPasswordDisabled, claims.Permissions) {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientForbiddenPage(w, r, "You are not authorized to share files/folders without a password")
|
2022-02-19 12:31:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2022-03-26 07:41:50 +00:00
|
|
|
err = dataprovider.UpdateShare(updatedShare, claims.Username, ipAddr)
|
2021-11-06 13:13:20 +00:00
|
|
|
if err == nil {
|
|
|
|
http.Redirect(w, r, webClientSharesPath, http.StatusSeeOther)
|
|
|
|
} else {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderAddUpdateSharePage(w, r, updatedShare, err.Error(), false)
|
2021-11-06 13:13:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleClientGetShares(w http.ResponseWriter, r *http.Request) {
|
2021-11-06 13:13:20 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientForbiddenPage(w, r, "Invalid token claims")
|
2021-11-06 13:13:20 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
shares := make([]dataprovider.Share, 0, limit)
|
|
|
|
for {
|
2022-03-19 20:44:27 +00:00
|
|
|
sh, err := dataprovider.GetShares(limit, len(shares), dataprovider.OrderASC, claims.Username)
|
2021-11-06 13:13:20 +00:00
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderInternalServerErrorPage(w, r, err)
|
2021-11-06 13:13:20 +00:00
|
|
|
return
|
|
|
|
}
|
2022-03-19 20:44:27 +00:00
|
|
|
shares = append(shares, sh...)
|
|
|
|
if len(sh) < limit {
|
2021-11-06 13:13:20 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
data := clientSharesPage{
|
2022-03-19 20:44:27 +00:00
|
|
|
baseClientPage: s.getBaseClientPageData(pageClientSharesTitle, webClientSharesPath, r),
|
2021-11-06 13:13:20 +00:00
|
|
|
Shares: shares,
|
|
|
|
BasePublicSharesURL: webClientPubSharesPath,
|
|
|
|
}
|
|
|
|
renderClientTemplate(w, templateClientShares, data)
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleClientGetProfile(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.renderClientProfilePage(w, r, "")
|
2021-09-29 16:46:15 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebClientChangePwd(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.renderClientChangePasswordPage(w, r, "")
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebClientProfilePost(w http.ResponseWriter, r *http.Request) {
|
2021-05-06 19:35:43 +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.renderClientProfilePage(w, r, 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.renderClientForbiddenPage(w, r, err.Error())
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientForbiddenPage(w, r, "Invalid token claims")
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
user, err := dataprovider.UserExists(claims.Username)
|
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientProfilePage(w, r, err.Error())
|
2021-08-17 16:08:32 +00:00
|
|
|
return
|
|
|
|
}
|
2021-09-29 16:46:15 +00:00
|
|
|
if !user.CanManagePublicKeys() && !user.CanChangeAPIKeyAuth() && !user.CanChangeInfo() {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientForbiddenPage(w, r, "You are not allowed to change anything")
|
2021-08-17 16:08:32 +00:00
|
|
|
return
|
|
|
|
}
|
2021-09-29 16:46:15 +00:00
|
|
|
if user.CanManagePublicKeys() {
|
|
|
|
user.PublicKeys = r.Form["public_keys"]
|
2021-08-17 16:08:32 +00:00
|
|
|
}
|
2021-09-29 16:46:15 +00:00
|
|
|
if user.CanChangeAPIKeyAuth() {
|
|
|
|
user.Filters.AllowAPIKeyAuth = len(r.Form.Get("allow_api_key_auth")) > 0
|
2021-08-17 16:08:32 +00:00
|
|
|
}
|
2021-09-29 16:46:15 +00:00
|
|
|
if user.CanChangeInfo() {
|
|
|
|
user.Email = r.Form.Get("email")
|
|
|
|
user.Description = r.Form.Get("description")
|
2021-08-17 16:08:32 +00:00
|
|
|
}
|
2022-03-26 07:41:50 +00:00
|
|
|
err = dataprovider.UpdateUser(&user, dataprovider.ActionExecutorSelf, ipAddr)
|
2021-08-17 16:08:32 +00:00
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientProfilePage(w, r, err.Error())
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientMessagePage(w, r, "Profile updated", "", http.StatusOK, nil,
|
2021-09-29 16:46:15 +00:00
|
|
|
"Your profile has been successfully updated")
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
2021-09-04 10:11:04 +00:00
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebClientMFA(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.renderClientMFAPage(w, r)
|
2021-09-04 10:11:04 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebClientTwoFactor(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.renderClientTwoFactorPage(w, "", util.GetIPFromRemoteAddress(r.RemoteAddr))
|
2021-09-04 10:11:04 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebClientTwoFactorRecovery(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.renderClientTwoFactorRecoveryPage(w, "", util.GetIPFromRemoteAddress(r.RemoteAddr))
|
2021-09-04 10:11:04 +00:00
|
|
|
}
|
2021-11-06 13:13:20 +00:00
|
|
|
|
|
|
|
func getShareFromPostFields(r *http.Request) (*dataprovider.Share, error) {
|
|
|
|
share := &dataprovider.Share{}
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
return share, err
|
|
|
|
}
|
|
|
|
share.Name = r.Form.Get("name")
|
|
|
|
share.Description = r.Form.Get("description")
|
|
|
|
share.Paths = r.Form["paths"]
|
|
|
|
share.Password = r.Form.Get("password")
|
|
|
|
share.AllowFrom = getSliceFromDelimitedValues(r.Form.Get("allowed_ip"), ",")
|
|
|
|
scope, err := strconv.Atoi(r.Form.Get("scope"))
|
|
|
|
if err != nil {
|
|
|
|
return share, err
|
|
|
|
}
|
|
|
|
share.Scope = dataprovider.ShareScope(scope)
|
|
|
|
maxTokens, err := strconv.Atoi(r.Form.Get("max_tokens"))
|
|
|
|
if err != nil {
|
|
|
|
return share, err
|
|
|
|
}
|
|
|
|
share.MaxTokens = maxTokens
|
|
|
|
expirationDateMillis := int64(0)
|
|
|
|
expirationDateString := r.Form.Get("expiration_date")
|
|
|
|
if strings.TrimSpace(expirationDateString) != "" {
|
|
|
|
expirationDate, err := time.Parse(webDateTimeFormat, expirationDateString)
|
|
|
|
if err != nil {
|
|
|
|
return share, err
|
|
|
|
}
|
|
|
|
expirationDateMillis = util.GetTimeAsMsSinceEpoch(expirationDate)
|
|
|
|
}
|
|
|
|
share.ExpiresAt = expirationDateMillis
|
|
|
|
return share, nil
|
|
|
|
}
|
2021-11-13 12:25:43 +00:00
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebClientForgotPwd(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.renderClientNotFoundPage(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.renderClientForgotPwdPage(w, "", util.GetIPFromRemoteAddress(r.RemoteAddr))
|
2021-11-13 12:25:43 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebClientForgotPwdPost(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.renderClientForgotPwdPage(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.renderClientForbiddenPage(w, r, err.Error())
|
2021-11-13 12:25:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
username := r.Form.Get("username")
|
|
|
|
err = handleForgotPassword(r, username, false)
|
|
|
|
if err != nil {
|
|
|
|
if e, ok := err.(*util.ValidationError); ok {
|
2022-03-26 07:41:50 +00:00
|
|
|
s.renderClientForgotPwdPage(w, e.GetErrorString(), ipAddr)
|
2021-11-13 12:25:43 +00:00
|
|
|
return
|
|
|
|
}
|
2022-03-26 07:41:50 +00:00
|
|
|
s.renderClientForgotPwdPage(w, err.Error(), ipAddr)
|
2021-11-13 12:25:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
http.Redirect(w, r, webClientResetPwdPath, http.StatusFound)
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebClientPasswordReset(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.renderClientNotFoundPage(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.renderClientResetPwdPage(w, "", util.GetIPFromRemoteAddress(r.RemoteAddr))
|
2021-11-13 12:25:43 +00:00
|
|
|
}
|
2021-11-25 18:24:32 +00:00
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleClientViewPDF(w http.ResponseWriter, r *http.Request) {
|
2021-11-25 18:24:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxLoginBodySize)
|
|
|
|
name := r.URL.Query().Get("path")
|
|
|
|
if name == "" {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientBadRequestPage(w, r, errors.New("no file specified"))
|
2021-11-25 18:24:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
name = util.CleanPath(name)
|
|
|
|
data := viewPDFPage{
|
|
|
|
Title: path.Base(name),
|
|
|
|
URL: fmt.Sprintf("%v?path=%v&inline=1", webClientFilesPath, url.QueryEscape(name)),
|
|
|
|
StaticURL: webStaticFilesPath,
|
2022-03-19 20:44:27 +00:00
|
|
|
ExtraCSS: s.binding.ExtraCSS,
|
2021-11-25 18:24:32 +00:00
|
|
|
}
|
|
|
|
renderClientTemplate(w, templateClientViewPDF, data)
|
|
|
|
}
|