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
|
|
|
|
2020-08-11 21:56:10 +00:00
|
|
|
package webdavd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
2021-01-03 16:03:04 +00:00
|
|
|
"crypto/x509"
|
2020-08-11 21:56:10 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-01-03 09:38:28 +00:00
|
|
|
"log"
|
2021-05-11 04:54:06 +00:00
|
|
|
"net"
|
2020-08-11 21:56:10 +00:00
|
|
|
"net/http"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2020-10-31 10:02:04 +00:00
|
|
|
"runtime/debug"
|
2024-07-24 16:27:13 +00:00
|
|
|
"slices"
|
2023-04-09 18:17:37 +00:00
|
|
|
"strings"
|
2020-08-11 21:56:10 +00:00
|
|
|
"time"
|
|
|
|
|
2022-11-03 07:31:40 +00:00
|
|
|
"github.com/drakkan/webdav"
|
2021-03-01 18:28:11 +00:00
|
|
|
"github.com/go-chi/chi/v5/middleware"
|
2020-08-15 13:55:20 +00:00
|
|
|
"github.com/rs/cors"
|
2020-08-11 21:56:10 +00:00
|
|
|
"github.com/rs/xid"
|
2023-05-12 16:34:59 +00:00
|
|
|
"github.com/sftpgo/sdk/plugin/notifier"
|
2020-08-11 21:56:10 +00:00
|
|
|
|
2022-07-24 14:18:54 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/common"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/dataprovider"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/logger"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/metric"
|
2023-05-12 16:34:59 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/plugin"
|
2022-07-24 14:18:54 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/util"
|
2024-05-01 17:42:09 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/version"
|
2020-08-11 21:56:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type webDavServer struct {
|
|
|
|
config *Configuration
|
2021-01-03 08:51:54 +00:00
|
|
|
binding Binding
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
|
|
|
|
2021-02-04 18:09:43 +00:00
|
|
|
func (s *webDavServer) listenAndServe(compressor *middleware.Compressor) error {
|
2021-02-04 08:06:41 +00:00
|
|
|
handler := compressor.Handler(s)
|
2020-08-11 21:56:10 +00:00
|
|
|
httpServer := &http.Server{
|
|
|
|
ReadHeaderTimeout: 30 * time.Second,
|
2021-05-07 18:41:20 +00:00
|
|
|
ReadTimeout: 60 * time.Second,
|
|
|
|
WriteTimeout: 60 * time.Second,
|
|
|
|
IdleTimeout: 60 * time.Second,
|
2020-08-11 21:56:10 +00:00
|
|
|
MaxHeaderBytes: 1 << 16, // 64KB
|
2022-01-05 10:37:45 +00:00
|
|
|
ErrorLog: log.New(&logger.StdLoggerWrapper{Sender: logSender}, "", 0),
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
2020-08-15 13:55:20 +00:00
|
|
|
if s.config.Cors.Enabled {
|
|
|
|
c := cors.New(cors.Options{
|
2022-08-02 16:44:34 +00:00
|
|
|
AllowedOrigins: util.RemoveDuplicates(s.config.Cors.AllowedOrigins, true),
|
|
|
|
AllowedMethods: util.RemoveDuplicates(s.config.Cors.AllowedMethods, true),
|
|
|
|
AllowedHeaders: util.RemoveDuplicates(s.config.Cors.AllowedHeaders, true),
|
|
|
|
ExposedHeaders: util.RemoveDuplicates(s.config.Cors.ExposedHeaders, true),
|
|
|
|
MaxAge: s.config.Cors.MaxAge,
|
|
|
|
AllowCredentials: s.config.Cors.AllowCredentials,
|
|
|
|
OptionsPassthrough: s.config.Cors.OptionsPassthrough,
|
|
|
|
OptionsSuccessStatus: s.config.Cors.OptionsSuccessStatus,
|
|
|
|
AllowPrivateNetwork: s.config.Cors.AllowPrivateNetwork,
|
2020-08-15 13:55:20 +00:00
|
|
|
})
|
2021-02-04 08:06:41 +00:00
|
|
|
handler = c.Handler(handler)
|
2020-08-15 13:55:20 +00:00
|
|
|
}
|
2021-02-04 08:06:41 +00:00
|
|
|
httpServer.Handler = handler
|
2021-01-03 08:51:54 +00:00
|
|
|
if certMgr != nil && s.binding.EnableHTTPS {
|
|
|
|
serviceStatus.Bindings = append(serviceStatus.Bindings, s.binding)
|
2022-05-21 14:34:47 +00:00
|
|
|
certID := common.DefaultTLSKeyPaidID
|
|
|
|
if getConfigPath(s.binding.CertificateFile, "") != "" && getConfigPath(s.binding.CertificateKeyFile, "") != "" {
|
|
|
|
certID = s.binding.GetAddress()
|
|
|
|
}
|
2020-08-11 21:56:10 +00:00
|
|
|
httpServer.TLSConfig = &tls.Config{
|
2023-08-07 17:11:48 +00:00
|
|
|
GetCertificate: certMgr.GetCertificateFunc(certID),
|
|
|
|
MinVersion: util.GetTLSVersion(s.binding.MinTLSVersion),
|
2023-10-28 10:35:26 +00:00
|
|
|
NextProtos: util.GetALPNProtocols(s.binding.Protocols),
|
2023-08-07 17:11:48 +00:00
|
|
|
CipherSuites: util.GetTLSCiphersFromNames(s.binding.TLSCipherSuites),
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
2023-02-27 18:02:43 +00:00
|
|
|
logger.Debug(logSender, "", "configured TLS cipher suites for binding %q: %v, certID: %v",
|
2022-05-21 14:34:47 +00:00
|
|
|
s.binding.GetAddress(), httpServer.TLSConfig.CipherSuites, certID)
|
2021-03-01 18:28:11 +00:00
|
|
|
if s.binding.isMutualTLSEnabled() {
|
2021-01-03 08:51:54 +00:00
|
|
|
httpServer.TLSConfig.ClientCAs = certMgr.GetRootCAs()
|
2021-01-03 16:03:04 +00:00
|
|
|
httpServer.TLSConfig.VerifyConnection = s.verifyTLSConnection
|
2021-03-01 18:28:11 +00:00
|
|
|
switch s.binding.ClientAuthType {
|
|
|
|
case 1:
|
|
|
|
httpServer.TLSConfig.ClientAuth = tls.RequireAndVerifyClientCert
|
|
|
|
case 2:
|
|
|
|
httpServer.TLSConfig.ClientAuth = tls.VerifyClientCertIfGiven
|
|
|
|
}
|
2020-12-28 18:48:23 +00:00
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
return util.HTTPListenAndServe(httpServer, s.binding.Address, s.binding.Port, true, logSender)
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
2021-01-03 08:51:54 +00:00
|
|
|
s.binding.EnableHTTPS = false
|
|
|
|
serviceStatus.Bindings = append(serviceStatus.Bindings, s.binding)
|
2021-07-11 13:26:51 +00:00
|
|
|
return util.HTTPListenAndServe(httpServer, s.binding.Address, s.binding.Port, false, logSender)
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
|
|
|
|
2021-01-03 16:03:04 +00:00
|
|
|
func (s *webDavServer) verifyTLSConnection(state tls.ConnectionState) error {
|
|
|
|
if certMgr != nil {
|
|
|
|
var clientCrt *x509.Certificate
|
|
|
|
var clientCrtName string
|
|
|
|
if len(state.PeerCertificates) > 0 {
|
|
|
|
clientCrt = state.PeerCertificates[0]
|
|
|
|
clientCrtName = clientCrt.Subject.String()
|
|
|
|
}
|
|
|
|
if len(state.VerifiedChains) == 0 {
|
2021-03-01 18:28:11 +00:00
|
|
|
if s.binding.ClientAuthType == 2 {
|
|
|
|
return nil
|
|
|
|
}
|
2021-01-03 16:03:04 +00:00
|
|
|
logger.Warn(logSender, "", "TLS connection cannot be verified: unable to get verification chain")
|
|
|
|
return errors.New("TLS connection cannot be verified: unable to get verification chain")
|
|
|
|
}
|
|
|
|
for _, verifiedChain := range state.VerifiedChains {
|
|
|
|
var caCrt *x509.Certificate
|
|
|
|
if len(verifiedChain) > 0 {
|
|
|
|
caCrt = verifiedChain[len(verifiedChain)-1]
|
|
|
|
}
|
|
|
|
if certMgr.IsRevoked(clientCrt, caCrt) {
|
2023-02-27 18:02:43 +00:00
|
|
|
logger.Debug(logSender, "", "tls handshake error, client certificate %q has been revoked", clientCrtName)
|
2021-01-03 16:03:04 +00:00
|
|
|
return common.ErrCrtRevoked
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-04 06:12:04 +00:00
|
|
|
// returns true if we have to handle a HEAD response, for a directory, ourself
|
|
|
|
func (s *webDavServer) checkRequestMethod(ctx context.Context, r *http.Request, connection *Connection) bool {
|
2020-12-06 07:19:41 +00:00
|
|
|
// see RFC4918, section 9.4
|
2021-02-03 21:36:13 +00:00
|
|
|
if r.Method == http.MethodGet || r.Method == http.MethodHead {
|
2021-02-04 06:12:04 +00:00
|
|
|
p := path.Clean(r.URL.Path)
|
2023-04-09 18:17:37 +00:00
|
|
|
if s.binding.Prefix != "" {
|
|
|
|
p = strings.TrimPrefix(p, s.binding.Prefix)
|
|
|
|
}
|
2020-12-06 07:19:41 +00:00
|
|
|
info, err := connection.Stat(ctx, p)
|
|
|
|
if err == nil && info.IsDir() {
|
2021-02-03 21:36:13 +00:00
|
|
|
if r.Method == http.MethodHead {
|
|
|
|
return true
|
|
|
|
}
|
2020-12-06 07:19:41 +00:00
|
|
|
r.Method = "PROPFIND"
|
|
|
|
if r.Header.Get("Depth") == "" {
|
|
|
|
r.Header.Add("Depth", "1")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-03 21:36:13 +00:00
|
|
|
return false
|
2020-12-06 07:19:41 +00:00
|
|
|
}
|
|
|
|
|
2020-08-11 21:56:10 +00:00
|
|
|
// ServeHTTP implements the http.Handler interface
|
|
|
|
func (s *webDavServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2020-10-31 10:02:04 +00:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
2023-02-27 18:02:43 +00:00
|
|
|
logger.Error(logSender, "", "panic in ServeHTTP: %q stack trace: %v", r, string(debug.Stack()))
|
2020-10-31 10:02:04 +00:00
|
|
|
http.Error(w, common.ErrGenericFailure.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}()
|
2021-04-20 16:12:16 +00:00
|
|
|
|
2024-05-01 17:42:09 +00:00
|
|
|
w.Header().Set("Server", version.GetServerVersion("/", false))
|
2021-05-11 04:54:06 +00:00
|
|
|
ipAddr := s.checkRemoteAddress(r)
|
2021-05-08 17:45:21 +00:00
|
|
|
|
|
|
|
common.Connections.AddClientConnection(ipAddr)
|
|
|
|
defer common.Connections.RemoveClientConnection(ipAddr)
|
|
|
|
|
2023-02-09 08:33:33 +00:00
|
|
|
if err := common.Connections.IsNewConnectionAllowed(ipAddr, common.ProtocolWebDAV); err != nil {
|
2022-10-22 09:56:41 +00:00
|
|
|
logger.Log(logger.LevelDebug, common.ProtocolWebDAV, "", "connection not allowed from ip %q: %v", ipAddr, err)
|
|
|
|
http.Error(w, err.Error(), http.StatusServiceUnavailable)
|
2020-12-15 18:29:30 +00:00
|
|
|
return
|
|
|
|
}
|
2023-02-09 08:33:33 +00:00
|
|
|
if common.IsBanned(ipAddr, common.ProtocolWebDAV) {
|
2020-08-11 21:56:10 +00:00
|
|
|
http.Error(w, common.ErrConnectionDenied.Error(), http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
2021-04-19 06:14:04 +00:00
|
|
|
delay, err := common.LimitRate(common.ProtocolWebDAV, ipAddr)
|
|
|
|
if err != nil {
|
2021-04-19 07:16:27 +00:00
|
|
|
delay += 499999999 * time.Nanosecond
|
2021-04-19 06:14:04 +00:00
|
|
|
w.Header().Set("Retry-After", fmt.Sprintf("%.0f", delay.Seconds()))
|
|
|
|
w.Header().Set("X-Retry-In", delay.String())
|
2021-04-18 10:31:06 +00:00
|
|
|
http.Error(w, err.Error(), http.StatusTooManyRequests)
|
|
|
|
return
|
|
|
|
}
|
2021-01-02 13:05:09 +00:00
|
|
|
if err := common.Config.ExecutePostConnectHook(ipAddr, common.ProtocolWebDAV); err != nil {
|
|
|
|
http.Error(w, common.ErrConnectionDenied.Error(), http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
user, isCached, lockSystem, loginMethod, err := s.authenticate(r, ipAddr)
|
2020-08-11 21:56:10 +00:00
|
|
|
if err != nil {
|
2022-08-02 17:06:49 +00:00
|
|
|
if !s.binding.DisableWWWAuthHeader {
|
2024-05-01 17:42:09 +00:00
|
|
|
w.Header().Set("WWW-Authenticate", fmt.Sprintf("Basic realm=\"%s WebDAV\"", version.GetServerVersion("_", false)))
|
2022-08-02 17:06:49 +00:00
|
|
|
}
|
2021-03-01 18:28:11 +00:00
|
|
|
http.Error(w, fmt.Sprintf("Authentication error: %v", err), http.StatusUnauthorized)
|
2020-08-11 21:56:10 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-01 18:28:11 +00:00
|
|
|
connectionID, err := s.validateUser(&user, r, loginMethod)
|
2020-08-11 21:56:10 +00:00
|
|
|
if err != nil {
|
2021-03-27 18:10:27 +00:00
|
|
|
// remove the cached user, we have not yet validated its filesystem
|
|
|
|
dataprovider.RemoveCachedWebDAVUser(user.Username)
|
2021-03-01 18:28:11 +00:00
|
|
|
updateLoginMetrics(&user, ipAddr, loginMethod, err)
|
2020-08-11 21:56:10 +00:00
|
|
|
http.Error(w, err.Error(), http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
if !isCached {
|
|
|
|
err = user.CheckFsRoot(connectionID)
|
|
|
|
} else {
|
2022-04-02 16:32:46 +00:00
|
|
|
_, err = user.GetFilesystemForPath("/", connectionID)
|
2021-03-21 18:15:47 +00:00
|
|
|
}
|
2020-08-11 21:56:10 +00:00
|
|
|
if err != nil {
|
2021-03-21 18:15:47 +00:00
|
|
|
errClose := user.CloseFs()
|
|
|
|
logger.Warn(logSender, connectionID, "unable to check fs root: %v close fs error: %v", err, errClose)
|
2021-06-05 14:07:09 +00:00
|
|
|
updateLoginMetrics(&user, ipAddr, loginMethod, common.ErrInternalFailure)
|
2020-08-11 21:56:10 +00:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
connection := &Connection{
|
2021-07-24 18:11:17 +00:00
|
|
|
BaseConnection: common.NewBaseConnection(connectionID, common.ProtocolWebDAV, util.GetHTTPLocalAddress(r),
|
|
|
|
r.RemoteAddr, user),
|
|
|
|
request: r,
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
2022-04-14 17:07:41 +00:00
|
|
|
if err = common.Connections.Add(connection); err != nil {
|
|
|
|
errClose := user.CloseFs()
|
|
|
|
logger.Warn(logSender, connectionID, "unable add connection: %v close fs error: %v", err, errClose)
|
|
|
|
updateLoginMetrics(&user, ipAddr, loginMethod, err)
|
|
|
|
http.Error(w, err.Error(), http.StatusTooManyRequests)
|
|
|
|
return
|
|
|
|
}
|
2020-08-11 21:56:10 +00:00
|
|
|
defer common.Connections.Remove(connection.GetID())
|
|
|
|
|
2022-04-14 17:07:41 +00:00
|
|
|
updateLoginMetrics(&user, ipAddr, loginMethod, err)
|
|
|
|
|
|
|
|
ctx := context.WithValue(r.Context(), requestIDKey, connectionID)
|
|
|
|
ctx = context.WithValue(ctx, requestStartKey, time.Now())
|
|
|
|
|
2021-08-19 13:51:43 +00:00
|
|
|
dataprovider.UpdateLastLogin(&user)
|
2020-08-11 21:56:10 +00:00
|
|
|
|
2021-02-04 06:12:04 +00:00
|
|
|
if s.checkRequestMethod(ctx, r, connection) {
|
2021-02-03 21:36:13 +00:00
|
|
|
w.Header().Set("Content-Type", "text/xml; charset=utf-8")
|
|
|
|
w.WriteHeader(http.StatusMultiStatus)
|
|
|
|
w.Write([]byte("")) //nolint:errcheck
|
2021-10-01 18:37:23 +00:00
|
|
|
writeLog(r, http.StatusMultiStatus, nil)
|
2021-02-03 21:36:13 +00:00
|
|
|
return
|
|
|
|
}
|
2020-08-11 21:56:10 +00:00
|
|
|
|
|
|
|
handler := webdav.Handler{
|
2021-03-07 16:10:45 +00:00
|
|
|
Prefix: s.binding.Prefix,
|
2020-08-11 21:56:10 +00:00
|
|
|
FileSystem: connection,
|
2020-11-04 18:11:40 +00:00
|
|
|
LockSystem: lockSystem,
|
2020-08-11 21:56:10 +00:00
|
|
|
Logger: writeLog,
|
|
|
|
}
|
|
|
|
handler.ServeHTTP(w, r.WithContext(ctx))
|
|
|
|
}
|
|
|
|
|
2021-03-01 18:28:11 +00:00
|
|
|
func (s *webDavServer) getCredentialsAndLoginMethod(r *http.Request) (string, string, string, *x509.Certificate, bool) {
|
|
|
|
var tlsCert *x509.Certificate
|
|
|
|
loginMethod := dataprovider.LoginMethodPassword
|
|
|
|
username, password, ok := r.BasicAuth()
|
|
|
|
if s.binding.isMutualTLSEnabled() && r.TLS != nil {
|
|
|
|
if len(r.TLS.PeerCertificates) > 0 {
|
|
|
|
tlsCert = r.TLS.PeerCertificates[0]
|
|
|
|
if ok {
|
|
|
|
loginMethod = dataprovider.LoginMethodTLSCertificateAndPwd
|
|
|
|
} else {
|
|
|
|
loginMethod = dataprovider.LoginMethodTLSCertificate
|
|
|
|
username = tlsCert.Subject.CommonName
|
|
|
|
password = ""
|
|
|
|
}
|
|
|
|
ok = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return username, password, loginMethod, tlsCert, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *webDavServer) authenticate(r *http.Request, ip string) (dataprovider.User, bool, webdav.LockSystem, string, error) {
|
2020-08-11 21:56:10 +00:00
|
|
|
var user dataprovider.User
|
|
|
|
var err error
|
2021-03-01 18:28:11 +00:00
|
|
|
username, password, loginMethod, tlsCert, ok := s.getCredentialsAndLoginMethod(r)
|
2020-08-11 21:56:10 +00:00
|
|
|
if !ok {
|
2021-06-05 14:07:09 +00:00
|
|
|
user.Username = username
|
|
|
|
return user, false, nil, loginMethod, common.ErrNoCredentials
|
2020-08-31 17:25:17 +00:00
|
|
|
}
|
2021-03-27 18:10:27 +00:00
|
|
|
cachedUser, ok := dataprovider.GetCachedWebDAVUser(username)
|
2020-11-04 21:29:25 +00:00
|
|
|
if ok {
|
|
|
|
if cachedUser.IsExpired() {
|
|
|
|
dataprovider.RemoveCachedWebDAVUser(username)
|
|
|
|
} else {
|
2024-01-14 20:36:23 +00:00
|
|
|
if !cachedUser.User.IsTLSVerificationEnabled() {
|
2021-03-01 18:28:11 +00:00
|
|
|
// for backward compatibility with 2.0.x we only check the password
|
|
|
|
tlsCert = nil
|
|
|
|
loginMethod = dataprovider.LoginMethodPassword
|
|
|
|
}
|
2023-05-20 10:39:07 +00:00
|
|
|
cu, u, err := dataprovider.CheckCachedUserCredentials(cachedUser, password, ip, loginMethod, common.ProtocolWebDAV, tlsCert)
|
|
|
|
if err == nil {
|
|
|
|
if cu != nil {
|
|
|
|
return cu.User, true, cu.LockSystem, loginMethod, nil
|
|
|
|
}
|
|
|
|
lockSystem := webdav.NewMemLS()
|
|
|
|
cachedUser = &dataprovider.CachedUser{
|
|
|
|
User: *u,
|
|
|
|
Password: password,
|
|
|
|
LockSystem: lockSystem,
|
|
|
|
Expiration: s.config.Cache.Users.getExpirationTime(),
|
|
|
|
}
|
|
|
|
dataprovider.CacheWebDAVUser(cachedUser)
|
|
|
|
return cachedUser.User, false, cachedUser.LockSystem, loginMethod, nil
|
2020-08-31 17:25:17 +00:00
|
|
|
}
|
2021-03-01 18:28:11 +00:00
|
|
|
updateLoginMetrics(&cachedUser.User, ip, loginMethod, dataprovider.ErrInvalidCredentials)
|
|
|
|
return user, false, nil, loginMethod, dataprovider.ErrInvalidCredentials
|
2020-08-31 17:25:17 +00:00
|
|
|
}
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
2021-03-01 18:28:11 +00:00
|
|
|
user, loginMethod, err = dataprovider.CheckCompositeCredentials(username, password, ip, loginMethod,
|
|
|
|
common.ProtocolWebDAV, tlsCert)
|
2020-08-11 21:56:10 +00:00
|
|
|
if err != nil {
|
2021-01-26 17:05:44 +00:00
|
|
|
user.Username = username
|
2021-03-01 18:28:11 +00:00
|
|
|
updateLoginMetrics(&user, ip, loginMethod, err)
|
2021-05-06 19:35:43 +00:00
|
|
|
return user, false, nil, loginMethod, dataprovider.ErrInvalidCredentials
|
2020-11-04 18:11:40 +00:00
|
|
|
}
|
|
|
|
lockSystem := webdav.NewMemLS()
|
2021-03-27 18:10:27 +00:00
|
|
|
cachedUser = &dataprovider.CachedUser{
|
2021-03-01 18:28:11 +00:00
|
|
|
User: user,
|
|
|
|
Password: password,
|
|
|
|
LockSystem: lockSystem,
|
2023-05-20 10:39:07 +00:00
|
|
|
Expiration: s.config.Cache.Users.getExpirationTime(),
|
2021-03-01 18:28:11 +00:00
|
|
|
}
|
2021-03-27 18:10:27 +00:00
|
|
|
dataprovider.CacheWebDAVUser(cachedUser)
|
2021-03-01 18:28:11 +00:00
|
|
|
return user, false, lockSystem, loginMethod, nil
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 18:28:11 +00:00
|
|
|
func (s *webDavServer) validateUser(user *dataprovider.User, r *http.Request, loginMethod string) (string, error) {
|
2020-08-11 21:56:10 +00:00
|
|
|
connID := xid.New().String()
|
|
|
|
connectionID := fmt.Sprintf("%v_%v", common.ProtocolWebDAV, connID)
|
|
|
|
|
|
|
|
if !filepath.IsAbs(user.HomeDir) {
|
2023-02-27 18:02:43 +00:00
|
|
|
logger.Warn(logSender, connectionID, "user %q has an invalid home dir: %q. Home dir must be an absolute path, login not allowed",
|
2020-08-11 21:56:10 +00:00
|
|
|
user.Username, user.HomeDir)
|
2023-02-27 18:02:43 +00:00
|
|
|
return connID, fmt.Errorf("cannot login user with invalid home dir: %q", user.HomeDir)
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
2024-07-24 16:27:13 +00:00
|
|
|
if slices.Contains(user.Filters.DeniedProtocols, common.ProtocolWebDAV) {
|
2023-02-27 18:02:43 +00:00
|
|
|
logger.Info(logSender, connectionID, "cannot login user %q, protocol DAV is not allowed", user.Username)
|
|
|
|
return connID, fmt.Errorf("protocol DAV is not allowed for user %q", user.Username)
|
2020-08-17 10:49:20 +00:00
|
|
|
}
|
2023-08-04 18:56:23 +00:00
|
|
|
if !user.IsLoginMethodAllowed(loginMethod, common.ProtocolWebDAV) {
|
2023-02-27 18:02:43 +00:00
|
|
|
logger.Info(logSender, connectionID, "cannot login user %q, %v login method is not allowed",
|
2021-12-02 18:36:42 +00:00
|
|
|
user.Username, loginMethod)
|
2023-02-27 18:02:43 +00:00
|
|
|
return connID, fmt.Errorf("login method %v is not allowed for user %q", loginMethod, user.Username)
|
2020-08-19 17:36:12 +00:00
|
|
|
}
|
2020-08-11 21:56:10 +00:00
|
|
|
if !user.IsLoginFromAddrAllowed(r.RemoteAddr) {
|
2023-02-27 18:02:43 +00:00
|
|
|
logger.Info(logSender, connectionID, "cannot login user %q, remote address is not allowed: %v",
|
2021-12-02 18:36:42 +00:00
|
|
|
user.Username, r.RemoteAddr)
|
2023-02-27 18:02:43 +00:00
|
|
|
return connID, fmt.Errorf("login for user %q is not allowed from this address: %v", user.Username, r.RemoteAddr)
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
|
|
|
return connID, nil
|
|
|
|
}
|
|
|
|
|
2021-05-11 04:54:06 +00:00
|
|
|
func (s *webDavServer) checkRemoteAddress(r *http.Request) string {
|
2021-07-11 13:26:51 +00:00
|
|
|
ipAddr := util.GetIPFromRemoteAddress(r.RemoteAddr)
|
2022-06-09 06:37:37 +00:00
|
|
|
var ip net.IP
|
|
|
|
isUnixSocket := filepath.IsAbs(s.binding.Address)
|
|
|
|
if !isUnixSocket {
|
|
|
|
ip = net.ParseIP(ipAddr)
|
|
|
|
}
|
|
|
|
if isUnixSocket || ip != nil {
|
2021-05-11 04:54:06 +00:00
|
|
|
for _, allow := range s.binding.allowHeadersFrom {
|
|
|
|
if allow(ip) {
|
2022-05-28 17:47:23 +00:00
|
|
|
parsedIP := util.GetRealIP(r, s.binding.ClientIPProxyHeader, s.binding.ClientIPHeaderDepth)
|
2021-05-11 04:54:06 +00:00
|
|
|
if parsedIP != "" {
|
|
|
|
ipAddr = parsedIP
|
|
|
|
r.RemoteAddr = ipAddr
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ipAddr
|
|
|
|
}
|
|
|
|
|
2021-10-01 18:37:23 +00:00
|
|
|
func writeLog(r *http.Request, status int, err error) {
|
2020-08-11 21:56:10 +00:00
|
|
|
scheme := "http"
|
|
|
|
if r.TLS != nil {
|
|
|
|
scheme = "https"
|
|
|
|
}
|
2022-05-19 17:49:51 +00:00
|
|
|
fields := map[string]any{
|
2020-08-11 21:56:10 +00:00
|
|
|
"remote_addr": r.RemoteAddr,
|
|
|
|
"proto": r.Proto,
|
|
|
|
"method": r.Method,
|
|
|
|
"user_agent": r.UserAgent(),
|
|
|
|
"uri": fmt.Sprintf("%s://%s%s", scheme, r.Host, r.RequestURI)}
|
|
|
|
if reqID, ok := r.Context().Value(requestIDKey).(string); ok {
|
|
|
|
fields["request_id"] = reqID
|
|
|
|
}
|
|
|
|
if reqStart, ok := r.Context().Value(requestStartKey).(time.Time); ok {
|
|
|
|
fields["elapsed_ms"] = time.Since(reqStart).Nanoseconds() / 1000000
|
|
|
|
}
|
2021-10-01 18:37:23 +00:00
|
|
|
if depth := r.Header.Get("Depth"); depth != "" {
|
|
|
|
fields["depth"] = depth
|
|
|
|
}
|
|
|
|
if contentLength := r.Header.Get("Content-Length"); contentLength != "" {
|
|
|
|
fields["content_length"] = contentLength
|
|
|
|
}
|
2021-10-06 07:11:56 +00:00
|
|
|
if timeout := r.Header.Get("Timeout"); timeout != "" {
|
|
|
|
fields["timeout"] = timeout
|
|
|
|
}
|
2021-10-01 18:37:23 +00:00
|
|
|
if status != 0 {
|
|
|
|
fields["resp_status"] = status
|
|
|
|
}
|
2020-08-11 21:56:10 +00:00
|
|
|
logger.GetLogger().Info().
|
|
|
|
Timestamp().
|
|
|
|
Str("sender", logSender).
|
|
|
|
Fields(fields).
|
|
|
|
Err(err).
|
2020-10-29 18:23:33 +00:00
|
|
|
Send()
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 18:28:11 +00:00
|
|
|
func updateLoginMetrics(user *dataprovider.User, ip, loginMethod string, err error) {
|
2021-07-11 13:26:51 +00:00
|
|
|
metric.AddLoginAttempt(loginMethod)
|
2024-04-10 16:39:08 +00:00
|
|
|
if err == nil {
|
|
|
|
plugin.Handler.NotifyLogEvent(notifier.LogEventTypeLoginOK, common.ProtocolWebDAV, user.Username, ip, "", nil)
|
2024-05-17 18:48:18 +00:00
|
|
|
common.DelayLogin(nil)
|
2024-04-10 16:39:08 +00:00
|
|
|
} else if err != common.ErrInternalFailure && err != common.ErrNoCredentials {
|
2021-03-01 18:28:11 +00:00
|
|
|
logger.ConnectionFailedLog(user.Username, ip, loginMethod, common.ProtocolWebDAV, err.Error())
|
2021-01-02 13:05:09 +00:00
|
|
|
event := common.HostEventLoginFailed
|
2023-05-12 16:34:59 +00:00
|
|
|
logEv := notifier.LogEventTypeLoginFailed
|
2023-01-25 17:49:03 +00:00
|
|
|
if errors.Is(err, util.ErrNotFound) {
|
2021-01-02 13:05:09 +00:00
|
|
|
event = common.HostEventUserNotFound
|
2023-05-12 16:34:59 +00:00
|
|
|
logEv = notifier.LogEventTypeLoginNoUser
|
2021-01-02 13:05:09 +00:00
|
|
|
}
|
2023-02-09 08:33:33 +00:00
|
|
|
common.AddDefenderEvent(ip, common.ProtocolWebDAV, event)
|
2023-05-12 16:34:59 +00:00
|
|
|
plugin.Handler.NotifyLogEvent(logEv, common.ProtocolWebDAV, user.Username, ip, "", err)
|
2024-05-17 18:48:18 +00:00
|
|
|
if loginMethod != dataprovider.LoginMethodTLSCertificate {
|
|
|
|
common.DelayLogin(err)
|
|
|
|
}
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
metric.AddLoginResult(loginMethod, err)
|
2021-03-01 18:28:11 +00:00
|
|
|
dataprovider.ExecutePostLoginHook(user, loginMethod, ip, common.ProtocolWebDAV, err)
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|