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"
|
2020-08-11 21:56:10 +00:00
|
|
|
"time"
|
|
|
|
|
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"
|
|
|
|
"golang.org/x/net/webdav"
|
|
|
|
|
2021-06-26 05:31:41 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/common"
|
|
|
|
"github.com/drakkan/sftpgo/v2/dataprovider"
|
|
|
|
"github.com/drakkan/sftpgo/v2/logger"
|
2021-07-11 13:26:51 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/metric"
|
|
|
|
"github.com/drakkan/sftpgo/v2/util"
|
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{
|
|
|
|
AllowedOrigins: s.config.Cors.AllowedOrigins,
|
|
|
|
AllowedMethods: s.config.Cors.AllowedMethods,
|
|
|
|
AllowedHeaders: s.config.Cors.AllowedHeaders,
|
|
|
|
ExposedHeaders: s.config.Cors.ExposedHeaders,
|
|
|
|
MaxAge: s.config.Cors.MaxAge,
|
|
|
|
AllowCredentials: s.config.Cors.AllowCredentials,
|
|
|
|
OptionsPassthrough: true,
|
|
|
|
})
|
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)
|
2020-08-11 21:56:10 +00:00
|
|
|
httpServer.TLSConfig = &tls.Config{
|
2021-02-18 19:17:16 +00:00
|
|
|
GetCertificate: certMgr.GetCertificateFunc(),
|
2022-02-13 14:56:07 +00:00
|
|
|
MinVersion: util.GetTLSVersion(s.binding.MinTLSVersion),
|
2021-12-08 18:25:22 +00:00
|
|
|
NextProtos: []string{"http/1.1", "h2"},
|
2021-07-11 13:26:51 +00:00
|
|
|
CipherSuites: util.GetTLSCiphersFromNames(s.binding.TLSCipherSuites),
|
2021-02-18 19:17:16 +00:00
|
|
|
PreferServerCipherSuites: true,
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
2021-02-18 19:17:16 +00:00
|
|
|
logger.Debug(logSender, "", "configured TLS cipher suites for binding %#v: %v", s.binding.GetAddress(),
|
|
|
|
httpServer.TLSConfig.CipherSuites)
|
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) {
|
|
|
|
logger.Debug(logSender, "", "tls handshake error, client certificate %#v has been revoked", clientCrtName)
|
|
|
|
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)
|
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 {
|
2022-04-28 10:55:01 +00:00
|
|
|
logger.Error(logSender, "", "panic in ServeHTTP: %#v 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
|
|
|
|
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)
|
|
|
|
|
|
|
|
if !common.Connections.IsNewConnectionAllowed(ipAddr) {
|
2022-03-17 21:10:52 +00:00
|
|
|
logger.Log(logger.LevelDebug, common.ProtocolWebDAV, "", fmt.Sprintf("connection not allowed from ip %#v", ipAddr))
|
2020-12-15 18:29:30 +00:00
|
|
|
http.Error(w, common.ErrConnectionDenied.Error(), http.StatusServiceUnavailable)
|
|
|
|
return
|
|
|
|
}
|
2021-01-02 13:05:09 +00:00
|
|
|
if common.IsBanned(ipAddr) {
|
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 {
|
2021-06-05 14:07:09 +00:00
|
|
|
updateLoginMetrics(&user, ipAddr, loginMethod, err)
|
2020-08-11 21:56:10 +00:00
|
|
|
w.Header().Set("WWW-Authenticate", "Basic realm=\"SFTPGo WebDAV\"")
|
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 {
|
2021-03-01 18:28:11 +00:00
|
|
|
if !cachedUser.User.IsTLSUsernameVerificationEnabled() {
|
|
|
|
// for backward compatibility with 2.0.x we only check the password
|
|
|
|
tlsCert = nil
|
|
|
|
loginMethod = dataprovider.LoginMethodPassword
|
|
|
|
}
|
|
|
|
if err := dataprovider.CheckCachedUserCredentials(cachedUser, password, loginMethod, common.ProtocolWebDAV, tlsCert); err == nil {
|
|
|
|
return cachedUser.User, true, 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,
|
|
|
|
}
|
|
|
|
if s.config.Cache.Users.ExpirationTime > 0 {
|
|
|
|
cachedUser.Expiration = time.Now().Add(time.Duration(s.config.Cache.Users.ExpirationTime) * time.Minute)
|
|
|
|
}
|
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) {
|
|
|
|
logger.Warn(logSender, connectionID, "user %#v has an invalid home dir: %#v. Home dir must be an absolute path, login not allowed",
|
|
|
|
user.Username, user.HomeDir)
|
|
|
|
return connID, fmt.Errorf("cannot login user with invalid home dir: %#v", user.HomeDir)
|
|
|
|
}
|
2022-05-19 17:49:51 +00:00
|
|
|
if util.Contains(user.Filters.DeniedProtocols, common.ProtocolWebDAV) {
|
2021-12-02 18:36:42 +00:00
|
|
|
logger.Info(logSender, connectionID, "cannot login user %#v, protocol DAV is not allowed", user.Username)
|
2021-03-21 18:15:47 +00:00
|
|
|
return connID, fmt.Errorf("protocol DAV is not allowed for user %#v", user.Username)
|
2020-08-17 10:49:20 +00:00
|
|
|
}
|
2022-03-06 15:57:13 +00:00
|
|
|
if !user.IsLoginMethodAllowed(loginMethod, common.ProtocolWebDAV, nil) {
|
2021-12-02 18:36:42 +00:00
|
|
|
logger.Info(logSender, connectionID, "cannot login user %#v, %v login method is not allowed",
|
|
|
|
user.Username, loginMethod)
|
2021-03-21 18:15:47 +00:00
|
|
|
return connID, fmt.Errorf("login method %v is not allowed for user %#v", loginMethod, user.Username)
|
2020-08-19 17:36:12 +00:00
|
|
|
}
|
2020-08-11 21:56:10 +00:00
|
|
|
if !user.IsLoginFromAddrAllowed(r.RemoteAddr) {
|
2021-12-02 18:36:42 +00:00
|
|
|
logger.Info(logSender, connectionID, "cannot login user %#v, remote address is not allowed: %v",
|
|
|
|
user.Username, r.RemoteAddr)
|
2021-03-21 18:15:47 +00:00
|
|
|
return connID, fmt.Errorf("login for user %#v 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)
|
2021-05-11 04:54:06 +00:00
|
|
|
ip := net.ParseIP(ipAddr)
|
|
|
|
if ip != nil {
|
|
|
|
for _, allow := range s.binding.allowHeadersFrom {
|
|
|
|
if allow(ip) {
|
2021-07-11 13:26:51 +00:00
|
|
|
parsedIP := util.GetRealIP(r)
|
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)
|
2021-09-11 16:23:11 +00:00
|
|
|
if err != nil && 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
|
2021-07-11 13:26:51 +00:00
|
|
|
if _, ok := err.(*util.RecordNotFoundError); ok {
|
2021-01-02 13:05:09 +00:00
|
|
|
event = common.HostEventUserNotFound
|
|
|
|
}
|
|
|
|
common.AddDefenderEvent(ip, event)
|
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
|
|
|
}
|