2021-05-06 19:35:43 +00:00
|
|
|
package httpd
|
|
|
|
|
|
|
|
import (
|
2021-05-30 21:07:46 +00:00
|
|
|
"encoding/json"
|
2021-05-06 19:35:43 +00:00
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"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"
|
|
|
|
|
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"
|
|
|
|
"github.com/drakkan/sftpgo/v2/sdk"
|
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"
|
|
|
|
pageClientFilesTitle = "My Files"
|
2021-09-29 16:46:15 +00:00
|
|
|
pageClientProfileTitle = "My Profile"
|
|
|
|
pageClientChangePwdTitle = "Change password"
|
|
|
|
pageClient2FATitle = "Two-factor auth"
|
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 {
|
2021-09-29 16:46:15 +00:00
|
|
|
Title string
|
|
|
|
CurrentURL string
|
|
|
|
FilesURL string
|
|
|
|
ProfileURL string
|
|
|
|
ChangePwdURL string
|
|
|
|
StaticURL string
|
|
|
|
LogoutURL string
|
|
|
|
MFAURL string
|
|
|
|
MFATitle string
|
|
|
|
FilesTitle string
|
|
|
|
ProfileTitle string
|
|
|
|
Version string
|
|
|
|
CSRFToken string
|
|
|
|
LoggedUser *dataprovider.User
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type dirMapping struct {
|
|
|
|
DirName string
|
|
|
|
Href string
|
|
|
|
}
|
|
|
|
|
|
|
|
type filesPage struct {
|
|
|
|
baseClientPage
|
2021-07-26 18:55:49 +00:00
|
|
|
CurrentDir string
|
|
|
|
DirsURL string
|
|
|
|
DownloadURL string
|
|
|
|
CanAddFiles bool
|
|
|
|
CanCreateDirs bool
|
|
|
|
CanRename bool
|
|
|
|
CanDelete bool
|
2021-08-03 17:58:03 +00:00
|
|
|
CanDownload bool
|
2021-07-26 18:55:49 +00:00
|
|
|
Error string
|
|
|
|
Paths []dirMapping
|
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
|
|
|
|
TOTPConfig sdk.TOTPConfig
|
|
|
|
GenerateTOTPURL string
|
|
|
|
ValidateTOTPURL string
|
|
|
|
SaveTOTPURL string
|
|
|
|
RecCodesURL string
|
|
|
|
Protocols []string
|
|
|
|
}
|
|
|
|
|
2021-05-06 19:35:43 +00:00
|
|
|
func getFileObjectURL(baseDir, name string) string {
|
2021-08-03 17:58:03 +00:00
|
|
|
return fmt.Sprintf("%v?path=%v&_=%v", webClientFilesPath, 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-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-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-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-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getBaseClientPageData(title, currentURL string, r *http.Request) baseClientPage {
|
|
|
|
var csrfToken string
|
|
|
|
if currentURL != "" {
|
|
|
|
csrfToken = createCSRFToken()
|
|
|
|
}
|
|
|
|
v := version.Get()
|
|
|
|
|
|
|
|
return baseClientPage{
|
2021-09-29 16:46:15 +00:00
|
|
|
Title: title,
|
|
|
|
CurrentURL: currentURL,
|
|
|
|
FilesURL: webClientFilesPath,
|
|
|
|
ProfileURL: webClientProfilePath,
|
|
|
|
ChangePwdURL: webChangeClientPwdPath,
|
|
|
|
StaticURL: webStaticFilesPath,
|
|
|
|
LogoutURL: webClientLogoutPath,
|
|
|
|
MFAURL: webClientMFAPath,
|
|
|
|
MFATitle: pageClient2FATitle,
|
|
|
|
FilesTitle: pageClientFilesTitle,
|
|
|
|
ProfileTitle: pageClientProfileTitle,
|
|
|
|
Version: fmt.Sprintf("%v-%v", v.Version, v.CommitHash),
|
|
|
|
CSRFToken: csrfToken,
|
|
|
|
LoggedUser: getUserFromToken(r),
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func renderClientMessagePage(w http.ResponseWriter, r *http.Request, title, body string, statusCode int, err error, message string) {
|
|
|
|
var errorString string
|
|
|
|
if body != "" {
|
|
|
|
errorString = body + " "
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
errorString += err.Error()
|
|
|
|
}
|
|
|
|
data := clientMessagePage{
|
|
|
|
baseClientPage: getBaseClientPageData(title, "", r),
|
|
|
|
Error: errorString,
|
|
|
|
Success: message,
|
|
|
|
}
|
|
|
|
w.WriteHeader(statusCode)
|
|
|
|
renderClientTemplate(w, templateClientMessage, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func renderClientInternalServerErrorPage(w http.ResponseWriter, r *http.Request, err error) {
|
|
|
|
renderClientMessagePage(w, r, page500Title, page500Body, http.StatusInternalServerError, err, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func renderClientBadRequestPage(w http.ResponseWriter, r *http.Request, err error) {
|
|
|
|
renderClientMessagePage(w, r, page400Title, "", http.StatusBadRequest, err, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func renderClientForbiddenPage(w http.ResponseWriter, r *http.Request, body string) {
|
|
|
|
renderClientMessagePage(w, r, page403Title, "", http.StatusForbidden, nil, body)
|
|
|
|
}
|
|
|
|
|
|
|
|
func renderClientNotFoundPage(w http.ResponseWriter, r *http.Request, err error) {
|
|
|
|
renderClientMessagePage(w, r, page404Title, page404Body, http.StatusNotFound, err, "")
|
|
|
|
}
|
|
|
|
|
2021-09-04 10:11:04 +00:00
|
|
|
func renderClientTwoFactorPage(w http.ResponseWriter, error string) {
|
|
|
|
data := twoFactorPage{
|
|
|
|
CurrentURL: webClientTwoFactorPath,
|
|
|
|
Version: version.Get().Version,
|
|
|
|
Error: error,
|
|
|
|
CSRFToken: createCSRFToken(),
|
|
|
|
StaticURL: webStaticFilesPath,
|
|
|
|
RecoveryURL: webClientTwoFactorRecoveryPath,
|
|
|
|
}
|
|
|
|
renderClientTemplate(w, templateTwoFactor, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func renderClientTwoFactorRecoveryPage(w http.ResponseWriter, error string) {
|
|
|
|
data := twoFactorPage{
|
|
|
|
CurrentURL: webClientTwoFactorRecoveryPath,
|
|
|
|
Version: version.Get().Version,
|
|
|
|
Error: error,
|
|
|
|
CSRFToken: createCSRFToken(),
|
|
|
|
StaticURL: webStaticFilesPath,
|
|
|
|
}
|
|
|
|
renderClientTemplate(w, templateTwoFactorRecovery, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func renderClientMFAPage(w http.ResponseWriter, r *http.Request) {
|
|
|
|
data := clientMFAPage{
|
|
|
|
baseClientPage: getBaseClientPageData(pageMFATitle, webClientMFAPath, r),
|
|
|
|
TOTPConfigs: mfa.GetAvailableTOTPConfigNames(),
|
|
|
|
GenerateTOTPURL: webClientTOTPGeneratePath,
|
|
|
|
ValidateTOTPURL: webClientTOTPValidatePath,
|
|
|
|
SaveTOTPURL: webClientTOTPSavePath,
|
|
|
|
RecCodesURL: webClientRecoveryCodesPath,
|
|
|
|
Protocols: dataprovider.MFAProtocols,
|
|
|
|
}
|
|
|
|
user, err := dataprovider.UserExists(data.LoggedUser.Username)
|
|
|
|
if err != nil {
|
|
|
|
renderInternalServerErrorPage(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
data.TOTPConfig = user.Filters.TOTPConfig
|
|
|
|
renderClientTemplate(w, templateClientMFA, data)
|
|
|
|
}
|
|
|
|
|
2021-07-26 18:55:49 +00:00
|
|
|
func renderFilesPage(w http.ResponseWriter, r *http.Request, dirName, error string, user dataprovider.User) {
|
2021-05-06 19:35:43 +00:00
|
|
|
data := filesPage{
|
2021-05-27 07:40:46 +00:00
|
|
|
baseClientPage: getBaseClientPageData(pageClientFilesTitle, webClientFilesPath, r),
|
|
|
|
Error: error,
|
|
|
|
CurrentDir: url.QueryEscape(dirName),
|
2021-06-05 14:07:09 +00:00
|
|
|
DownloadURL: webClientDownloadZipPath,
|
2021-07-26 18:55:49 +00:00
|
|
|
DirsURL: webClientDirsPath,
|
|
|
|
CanAddFiles: user.CanAddFilesFromWeb(dirName),
|
|
|
|
CanCreateDirs: user.CanAddDirsFromWeb(dirName),
|
|
|
|
CanRename: user.CanRenameFromWeb(dirName, dirName),
|
|
|
|
CanDelete: user.CanDeleteFromWeb(dirName),
|
2021-08-03 17:58:03 +00:00
|
|
|
CanDownload: user.HasPerm(dataprovider.PermDownload, dirName),
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
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)},
|
|
|
|
}, paths...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
data.Paths = paths
|
|
|
|
renderClientTemplate(w, templateClientFiles, data)
|
|
|
|
}
|
|
|
|
|
2021-09-29 16:46:15 +00:00
|
|
|
func renderClientProfilePage(w http.ResponseWriter, r *http.Request, error string) {
|
|
|
|
data := clientProfilePage{
|
|
|
|
baseClientPage: getBaseClientPageData(pageClientProfileTitle, webClientProfilePath, r),
|
|
|
|
Error: error,
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
user, err := dataprovider.UserExists(data.LoggedUser.Username)
|
|
|
|
if err != nil {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func renderClientChangePasswordPage(w http.ResponseWriter, r *http.Request, error string) {
|
|
|
|
data := changeClientPasswordPage{
|
|
|
|
baseClientPage: getBaseClientPageData(pageClientChangePwdTitle, webChangeClientPwdPath, r),
|
|
|
|
Error: error,
|
|
|
|
}
|
|
|
|
|
|
|
|
renderClientTemplate(w, templateClientChangePwd, data)
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleWebClientLogout(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxLoginBodySize)
|
2021-05-06 19:35:43 +00:00
|
|
|
c := jwtTokenClaims{}
|
2021-05-16 07:13:00 +00:00
|
|
|
c.removeCookie(w, r, webBaseClientPath)
|
2021-05-06 19:35:43 +00:00
|
|
|
|
|
|
|
http.Redirect(w, r, webClientLoginPath, http.StatusFound)
|
|
|
|
}
|
|
|
|
|
2021-06-05 14:07:09 +00:00
|
|
|
func 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 == "" {
|
|
|
|
renderClientMessagePage(w, r, "Invalid token claims", "", http.StatusForbidden, nil, "")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := dataprovider.UserExists(claims.Username)
|
|
|
|
if err != nil {
|
2021-06-05 14:07:09 +00:00
|
|
|
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()
|
|
|
|
connectionID := fmt.Sprintf("%v_%v", common.ProtocolHTTP, connID)
|
|
|
|
if err := checkHTTPClientUser(&user, r, connectionID); err != nil {
|
|
|
|
renderClientForbiddenPage(w, r, err.Error())
|
|
|
|
return
|
|
|
|
}
|
2021-05-30 21:07:46 +00:00
|
|
|
connection := &Connection{
|
2021-07-24 18:11:17 +00:00
|
|
|
BaseConnection: common.NewBaseConnection(connID, common.ProtocolHTTP, util.GetHTTPLocalAddress(r),
|
|
|
|
r.RemoteAddr, user),
|
|
|
|
request: r,
|
2021-05-30 21:07:46 +00:00
|
|
|
}
|
|
|
|
common.Connections.Add(connection)
|
|
|
|
defer common.Connections.Remove(connection.GetID())
|
|
|
|
|
|
|
|
name := "/"
|
|
|
|
if _, ok := r.URL.Query()["path"]; ok {
|
2021-07-11 13:26:51 +00:00
|
|
|
name = util.CleanPath(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 {
|
|
|
|
renderClientMessagePage(w, r, "Unable to get files list", "", http.StatusInternalServerError, err, "")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Disposition", "attachment; filename=\"sftpgo-download.zip\"")
|
|
|
|
renderCompressedFiles(w, connection, name, filesList)
|
|
|
|
}
|
|
|
|
|
2021-05-27 07:40:46 +00:00
|
|
|
func 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()
|
|
|
|
connectionID := fmt.Sprintf("%v_%v", common.ProtocolHTTP, connID)
|
|
|
|
if err := checkHTTPClientUser(&user, r, connectionID); err != nil {
|
|
|
|
sendAPIResponse(w, r, err, http.StatusText(http.StatusForbidden), http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
2021-05-27 07:40:46 +00:00
|
|
|
connection := &Connection{
|
2021-07-24 18:11:17 +00:00
|
|
|
BaseConnection: common.NewBaseConnection(connID, common.ProtocolHTTP, util.GetHTTPLocalAddress(r),
|
|
|
|
r.RemoteAddr, user),
|
|
|
|
request: r,
|
2021-05-27 07:40:46 +00:00
|
|
|
}
|
|
|
|
common.Connections.Add(connection)
|
|
|
|
defer common.Connections.Remove(connection.GetID())
|
|
|
|
|
|
|
|
name := "/"
|
|
|
|
if _, ok := r.URL.Query()["path"]; ok {
|
2021-07-11 13:26:51 +00:00
|
|
|
name = util.CleanPath(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)
|
|
|
|
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-05-27 07:40:46 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-26 18:55:49 +00:00
|
|
|
res["type_name"] = 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())
|
|
|
|
res["url"] = getFileObjectURL(name, info.Name())
|
|
|
|
results = append(results, res)
|
|
|
|
}
|
|
|
|
|
|
|
|
render.JSON(w, r, results)
|
|
|
|
}
|
|
|
|
|
2021-05-06 19:35:43 +00:00
|
|
|
func 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 == "" {
|
|
|
|
renderClientForbiddenPage(w, r, "Invalid token claims")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := dataprovider.UserExists(claims.Username)
|
|
|
|
if err != nil {
|
2021-06-05 14:07:09 +00:00
|
|
|
renderClientMessagePage(w, r, "Unable to retrieve your user", "", getRespStatus(err), nil, "")
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
connID := xid.New().String()
|
|
|
|
connectionID := fmt.Sprintf("%v_%v", common.ProtocolHTTP, connID)
|
2021-06-05 14:07:09 +00:00
|
|
|
if err := checkHTTPClientUser(&user, r, connectionID); err != nil {
|
2021-05-06 19:35:43 +00:00
|
|
|
renderClientForbiddenPage(w, r, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
connection := &Connection{
|
2021-07-24 18:11:17 +00:00
|
|
|
BaseConnection: common.NewBaseConnection(connID, common.ProtocolHTTP, util.GetHTTPLocalAddress(r),
|
|
|
|
r.RemoteAddr, user),
|
|
|
|
request: r,
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
common.Connections.Add(connection)
|
|
|
|
defer common.Connections.Remove(connection.GetID())
|
|
|
|
|
|
|
|
name := "/"
|
|
|
|
if _, ok := r.URL.Query()["path"]; ok {
|
2021-07-11 13:26:51 +00:00
|
|
|
name = util.CleanPath(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 {
|
2021-07-26 18:55:49 +00:00
|
|
|
renderFilesPage(w, r, path.Dir(name), fmt.Sprintf("unable to stat file %#v: %v", name, err), user)
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if info.IsDir() {
|
2021-07-26 18:55:49 +00:00
|
|
|
renderFilesPage(w, r, name, "", user)
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
2021-06-05 14:07:09 +00:00
|
|
|
if status, err := downloadFile(w, r, connection, name, info); err != nil && status != 0 {
|
|
|
|
if status > 0 {
|
|
|
|
if status == http.StatusRequestedRangeNotSatisfiable {
|
|
|
|
renderClientMessagePage(w, r, http.StatusText(status), "", status, err, "")
|
|
|
|
return
|
|
|
|
}
|
2021-07-26 18:55:49 +00:00
|
|
|
renderFilesPage(w, r, path.Dir(name), err.Error(), user)
|
2021-06-05 14:07:09 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 16:46:15 +00:00
|
|
|
func handleClientGetProfile(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
renderClientProfilePage(w, r, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleWebClientChangePwd(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2021-09-29 16:46:15 +00:00
|
|
|
renderClientChangePasswordPage(w, r, "")
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleWebClientChangePwdPost(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
err := r.ParseForm()
|
|
|
|
if err != nil {
|
2021-09-29 16:46:15 +00:00
|
|
|
renderClientChangePasswordPage(w, r, err.Error())
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := verifyCSRFToken(r.Form.Get(csrfFormToken)); err != nil {
|
|
|
|
renderClientForbiddenPage(w, r, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = doChangeUserPassword(r, r.Form.Get("current_password"), r.Form.Get("new_password1"),
|
|
|
|
r.Form.Get("new_password2"))
|
|
|
|
if err != nil {
|
2021-09-29 16:46:15 +00:00
|
|
|
renderClientChangePasswordPage(w, r, err.Error())
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
handleWebClientLogout(w, r)
|
|
|
|
}
|
|
|
|
|
2021-09-29 16:46:15 +00:00
|
|
|
func 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 {
|
2021-09-29 16:46:15 +00:00
|
|
|
renderClientProfilePage(w, r, err.Error())
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := verifyCSRFToken(r.Form.Get(csrfFormToken)); err != nil {
|
|
|
|
renderClientForbiddenPage(w, r, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
2021-09-29 16:46:15 +00:00
|
|
|
renderClientProfilePage(w, r, "Invalid token claims")
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
user, err := dataprovider.UserExists(claims.Username)
|
|
|
|
if err != nil {
|
2021-09-29 16:46:15 +00:00
|
|
|
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() {
|
|
|
|
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
|
|
|
}
|
|
|
|
err = dataprovider.UpdateUser(&user)
|
|
|
|
if err != nil {
|
2021-09-29 16:46:15 +00:00
|
|
|
renderClientProfilePage(w, r, err.Error())
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
2021-09-29 16:46:15 +00:00
|
|
|
renderClientMessagePage(w, r, "Profile updated", "", http.StatusOK, nil,
|
|
|
|
"Your profile has been successfully updated")
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
2021-09-04 10:11:04 +00:00
|
|
|
|
|
|
|
func handleWebClientMFA(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
renderClientMFAPage(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleWebClientTwoFactor(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
renderClientTwoFactorPage(w, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleWebClientTwoFactorRecovery(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
renderClientTwoFactorRecoveryPage(w, "")
|
|
|
|
}
|