fix some codeql warnings

Signed-off-by: Nicola Murino <nicola.murino@gmail.com>
This commit is contained in:
Nicola Murino
2023-03-10 17:30:06 +01:00
parent 14242b59a2
commit 84396343da
5 changed files with 60 additions and 56 deletions

View File

@@ -855,25 +855,25 @@ func getRateLimitersFromEnv(idx int) {
isSet := false isSet := false
average, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_COMMON__RATE_LIMITERS__%v__AVERAGE", idx)) average, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_COMMON__RATE_LIMITERS__%v__AVERAGE", idx), 64)
if ok { if ok {
rtlConfig.Average = average rtlConfig.Average = average
isSet = true isSet = true
} }
period, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_COMMON__RATE_LIMITERS__%v__PERIOD", idx)) period, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_COMMON__RATE_LIMITERS__%v__PERIOD", idx), 64)
if ok { if ok {
rtlConfig.Period = period rtlConfig.Period = period
isSet = true isSet = true
} }
burst, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_COMMON__RATE_LIMITERS__%v__BURST", idx)) burst, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_COMMON__RATE_LIMITERS__%v__BURST", idx), 0)
if ok { if ok {
rtlConfig.Burst = int(burst) rtlConfig.Burst = int(burst)
isSet = true isSet = true
} }
rtlType, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_COMMON__RATE_LIMITERS__%v__TYPE", idx)) rtlType, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_COMMON__RATE_LIMITERS__%v__TYPE", idx), 0)
if ok { if ok {
rtlConfig.Type = int(rtlType) rtlConfig.Type = int(rtlType)
isSet = true isSet = true
@@ -891,13 +891,13 @@ func getRateLimitersFromEnv(idx int) {
isSet = true isSet = true
} }
softLimit, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_COMMON__RATE_LIMITERS__%v__ENTRIES_SOFT_LIMIT", idx)) softLimit, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_COMMON__RATE_LIMITERS__%v__ENTRIES_SOFT_LIMIT", idx), 0)
if ok { if ok {
rtlConfig.EntriesSoftLimit = int(softLimit) rtlConfig.EntriesSoftLimit = int(softLimit)
isSet = true isSet = true
} }
hardLimit, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_COMMON__RATE_LIMITERS__%v__ENTRIES_HARD_LIMIT", idx)) hardLimit, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_COMMON__RATE_LIMITERS__%v__ENTRIES_HARD_LIMIT", idx), 0)
if ok { if ok {
rtlConfig.EntriesHardLimit = int(hardLimit) rtlConfig.EntriesHardLimit = int(hardLimit)
isSet = true isSet = true
@@ -933,7 +933,7 @@ func getKMSPluginFromEnv(idx int, pluginConfig *plugin.Config) bool {
func getAuthPluginFromEnv(idx int, pluginConfig *plugin.Config) bool { func getAuthPluginFromEnv(idx int, pluginConfig *plugin.Config) bool {
isSet := false isSet := false
authScope, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__AUTH_OPTIONS__SCOPE", idx)) authScope, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__AUTH_OPTIONS__SCOPE", idx), 0)
if ok { if ok {
pluginConfig.AuthOptions.Scope = int(authScope) pluginConfig.AuthOptions.Scope = int(authScope)
isSet = true isSet = true
@@ -963,13 +963,13 @@ func getNotifierPluginFromEnv(idx int, pluginConfig *plugin.Config) bool {
isSet = true isSet = true
} }
notifierRetryMaxTime, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__NOTIFIER_OPTIONS__RETRY_MAX_TIME", idx)) notifierRetryMaxTime, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__NOTIFIER_OPTIONS__RETRY_MAX_TIME", idx), 0)
if ok { if ok {
pluginConfig.NotifierOptions.RetryMaxTime = int(notifierRetryMaxTime) pluginConfig.NotifierOptions.RetryMaxTime = int(notifierRetryMaxTime)
isSet = true isSet = true
} }
notifierRetryQueueMaxSize, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__NOTIFIER_OPTIONS__RETRY_QUEUE_MAX_SIZE", idx)) notifierRetryQueueMaxSize, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__NOTIFIER_OPTIONS__RETRY_QUEUE_MAX_SIZE", idx), 0)
if ok { if ok {
pluginConfig.NotifierOptions.RetryQueueMaxSize = int(notifierRetryQueueMaxSize) pluginConfig.NotifierOptions.RetryQueueMaxSize = int(notifierRetryQueueMaxSize)
isSet = true isSet = true
@@ -1045,7 +1045,7 @@ func getSFTPDBindindFromEnv(idx int) {
isSet := false isSet := false
port, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_SFTPD__BINDINGS__%v__PORT", idx)) port, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_SFTPD__BINDINGS__%v__PORT", idx), 0)
if ok { if ok {
binding.Port = int(port) binding.Port = int(port)
isSet = true isSet = true
@@ -1132,13 +1132,13 @@ func getFTPDBindingSecurityFromEnv(idx int, binding *ftpd.Binding) bool {
isSet = true isSet = true
} }
tlsMode, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__TLS_MODE", idx)) tlsMode, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__TLS_MODE", idx), 0)
if ok { if ok {
binding.TLSMode = int(tlsMode) binding.TLSMode = int(tlsMode)
isSet = true isSet = true
} }
tlsVer, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__MIN_TLS_VERSION", idx)) tlsVer, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__MIN_TLS_VERSION", idx), 0)
if ok { if ok {
binding.MinTLSVersion = int(tlsVer) binding.MinTLSVersion = int(tlsVer)
isSet = true isSet = true
@@ -1150,19 +1150,19 @@ func getFTPDBindingSecurityFromEnv(idx int, binding *ftpd.Binding) bool {
isSet = true isSet = true
} }
clientAuthType, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__CLIENT_AUTH_TYPE", idx)) clientAuthType, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__CLIENT_AUTH_TYPE", idx), 0)
if ok { if ok {
binding.ClientAuthType = int(clientAuthType) binding.ClientAuthType = int(clientAuthType)
isSet = true isSet = true
} }
pasvSecurity, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__PASSIVE_CONNECTIONS_SECURITY", idx)) pasvSecurity, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__PASSIVE_CONNECTIONS_SECURITY", idx), 0)
if ok { if ok {
binding.PassiveConnectionsSecurity = int(pasvSecurity) binding.PassiveConnectionsSecurity = int(pasvSecurity)
isSet = true isSet = true
} }
activeSecurity, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__ACTIVE_CONNECTIONS_SECURITY", idx)) activeSecurity, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__ACTIVE_CONNECTIONS_SECURITY", idx), 0)
if ok { if ok {
binding.ActiveConnectionsSecurity = int(activeSecurity) binding.ActiveConnectionsSecurity = int(activeSecurity)
isSet = true isSet = true
@@ -1175,7 +1175,7 @@ func getFTPDBindingFromEnv(idx int) {
binding := getDefaultFTPDBinding(idx) binding := getDefaultFTPDBinding(idx)
isSet := false isSet := false
port, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__PORT", idx)) port, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__PORT", idx), 0)
if ok { if ok {
binding.Port = int(port) binding.Port = int(port)
isSet = true isSet = true
@@ -1249,7 +1249,7 @@ func getWebDAVDBindingProxyConfigsFromEnv(idx int, binding *webdavd.Binding) boo
isSet = true isSet = true
} }
clientIPHeaderDepth, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_WEBDAVD__BINDINGS__%v__CLIENT_IP_HEADER_DEPTH", idx)) clientIPHeaderDepth, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_WEBDAVD__BINDINGS__%v__CLIENT_IP_HEADER_DEPTH", idx), 0)
if ok { if ok {
binding.ClientIPHeaderDepth = int(clientIPHeaderDepth) binding.ClientIPHeaderDepth = int(clientIPHeaderDepth)
isSet = true isSet = true
@@ -1287,7 +1287,7 @@ func getWebDAVDBindingFromEnv(idx int) {
isSet := false isSet := false
port, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_WEBDAVD__BINDINGS__%v__PORT", idx)) port, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_WEBDAVD__BINDINGS__%v__PORT", idx), 0)
if ok { if ok {
binding.Port = int(port) binding.Port = int(port)
isSet = true isSet = true
@@ -1317,13 +1317,13 @@ func getWebDAVDBindingFromEnv(idx int) {
isSet = true isSet = true
} }
tlsVer, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_WEBDAVD__BINDINGS__%v__MIN_TLS_VERSION", idx)) tlsVer, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_WEBDAVD__BINDINGS__%v__MIN_TLS_VERSION", idx), 0)
if ok { if ok {
binding.MinTLSVersion = int(tlsVer) binding.MinTLSVersion = int(tlsVer)
isSet = true isSet = true
} }
clientAuthType, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_WEBDAVD__BINDINGS__%v__CLIENT_AUTH_TYPE", idx)) clientAuthType, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_WEBDAVD__BINDINGS__%v__CLIENT_AUTH_TYPE", idx), 0)
if ok { if ok {
binding.ClientAuthType = int(clientAuthType) binding.ClientAuthType = int(clientAuthType)
isSet = true isSet = true
@@ -1444,7 +1444,7 @@ func getHTTPDSecurityConfFromEnv(idx int) (httpd.SecurityConf, bool) { //nolint:
isSet = true isSet = true
} }
stsSeconds, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__SECURITY__STS_SECONDS", idx)) stsSeconds, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__SECURITY__STS_SECONDS", idx), 64)
if ok { if ok {
result.STSSeconds = stsSeconds result.STSSeconds = stsSeconds
isSet = true isSet = true
@@ -1746,7 +1746,7 @@ func getHTTPDBindingProxyConfigsFromEnv(idx int, binding *httpd.Binding) bool {
isSet = true isSet = true
} }
clientIPHeaderDepth, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__CLIENT_IP_HEADER_DEPTH", idx)) clientIPHeaderDepth, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__CLIENT_IP_HEADER_DEPTH", idx), 0)
if ok { if ok {
binding.ClientIPHeaderDepth = int(clientIPHeaderDepth) binding.ClientIPHeaderDepth = int(clientIPHeaderDepth)
isSet = true isSet = true
@@ -1759,7 +1759,7 @@ func getHTTPDBindingFromEnv(idx int) { //nolint:gocyclo
binding := getDefaultHTTPBinding(idx) binding := getDefaultHTTPBinding(idx)
isSet := false isSet := false
port, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__PORT", idx)) port, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__PORT", idx), 0)
if ok { if ok {
binding.Port = int(port) binding.Port = int(port)
isSet = true isSet = true
@@ -1801,7 +1801,7 @@ func getHTTPDBindingFromEnv(idx int) { //nolint:gocyclo
isSet = true isSet = true
} }
enabledLoginMethods, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__ENABLED_LOGIN_METHODS", idx)) enabledLoginMethods, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__ENABLED_LOGIN_METHODS", idx), 0)
if ok { if ok {
binding.EnabledLoginMethods = int(enabledLoginMethods) binding.EnabledLoginMethods = int(enabledLoginMethods)
isSet = true isSet = true
@@ -1819,13 +1819,13 @@ func getHTTPDBindingFromEnv(idx int) { //nolint:gocyclo
isSet = true isSet = true
} }
tlsVer, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__MIN_TLS_VERSION", idx)) tlsVer, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__MIN_TLS_VERSION", idx), 0)
if ok { if ok {
binding.MinTLSVersion = int(tlsVer) binding.MinTLSVersion = int(tlsVer)
isSet = true isSet = true
} }
clientAuthType, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__CLIENT_AUTH_TYPE", idx)) clientAuthType, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__CLIENT_AUTH_TYPE", idx), 0)
if ok { if ok {
binding.ClientAuthType = int(clientAuthType) binding.ClientAuthType = int(clientAuthType)
isSet = true isSet = true
@@ -1841,7 +1841,7 @@ func getHTTPDBindingFromEnv(idx int) { //nolint:gocyclo
isSet = true isSet = true
} }
hideLoginURL, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__HIDE_LOGIN_URL", idx)) hideLoginURL, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__HIDE_LOGIN_URL", idx), 0)
if ok { if ok {
binding.HideLoginURL = int(hideLoginURL) binding.HideLoginURL = int(hideLoginURL)
isSet = true isSet = true
@@ -1930,7 +1930,7 @@ func getCommandConfigsFromEnv(idx int) {
cfg.Path = path cfg.Path = path
} }
timeout, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_COMMAND__COMMANDS__%v__TIMEOUT", idx)) timeout, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_COMMAND__COMMANDS__%v__TIMEOUT", idx), 0)
if ok { if ok {
cfg.Timeout = int(timeout) cfg.Timeout = int(timeout)
} }
@@ -2148,10 +2148,10 @@ func lookupBoolFromEnv(envName string) (bool, bool) {
return false, false return false, false
} }
func lookupIntFromEnv(envName string) (int64, bool) { func lookupIntFromEnv(envName string, bitSize int) (int64, bool) {
value, ok := os.LookupEnv(envName) value, ok := os.LookupEnv(envName)
if ok { if ok {
converted, err := strconv.ParseInt(strings.TrimSpace(value), 10, 64) converted, err := strconv.ParseInt(strings.TrimSpace(value), 10, bitSize)
if err == nil { if err == nil {
return converted, ok return converted, ok
} }

View File

@@ -98,7 +98,7 @@ func getFsSearchParamsFromRequest(r *http.Request) (eventsearcher.FsEventSearch,
s.Protocols = getCommaSeparatedQueryParam(r, "protocols") s.Protocols = getCommaSeparatedQueryParam(r, "protocols")
statuses := getCommaSeparatedQueryParam(r, "statuses") statuses := getCommaSeparatedQueryParam(r, "statuses")
for _, status := range statuses { for _, status := range statuses {
val, err := strconv.Atoi(status) val, err := strconv.ParseInt(status, 10, 32)
if err != nil { if err != nil {
return s, util.NewValidationError(fmt.Sprintf("invalid status: %v", status)) return s, util.NewValidationError(fmt.Sprintf("invalid status: %v", status))
} }

View File

@@ -1534,15 +1534,15 @@ func getFiltersFromUserPostFields(r *http.Request) (sdk.BaseUserFilters, error)
if err != nil { if err != nil {
return filters, fmt.Errorf("invalid max upload file size: %w", err) return filters, fmt.Errorf("invalid max upload file size: %w", err)
} }
defaultSharesExpiration, err := strconv.ParseInt(r.Form.Get("default_shares_expiration"), 10, 64) defaultSharesExpiration, err := strconv.Atoi(r.Form.Get("default_shares_expiration"))
if err != nil { if err != nil {
return filters, fmt.Errorf("invalid default shares expiration: %w", err) return filters, fmt.Errorf("invalid default shares expiration: %w", err)
} }
passwordExpiration, err := strconv.ParseInt(r.Form.Get("password_expiration"), 10, 64) passwordExpiration, err := strconv.Atoi(r.Form.Get("password_expiration"))
if err != nil { if err != nil {
return filters, fmt.Errorf("invalid password expiration: %w", err) return filters, fmt.Errorf("invalid password expiration: %w", err)
} }
passwordStrength, err := strconv.ParseInt(r.Form.Get("password_strength"), 10, 64) passwordStrength, err := strconv.Atoi(r.Form.Get("password_strength"))
if err != nil { if err != nil {
return filters, fmt.Errorf("invalid password strength: %w", err) return filters, fmt.Errorf("invalid password strength: %w", err)
} }
@@ -1559,9 +1559,9 @@ func getFiltersFromUserPostFields(r *http.Request) (sdk.BaseUserFilters, error)
filters.FilePatterns = getFilePatternsFromPostField(r) filters.FilePatterns = getFilePatternsFromPostField(r)
filters.TLSUsername = sdk.TLSUsername(r.Form.Get("tls_username")) filters.TLSUsername = sdk.TLSUsername(r.Form.Get("tls_username"))
filters.WebClient = r.Form["web_client_options"] filters.WebClient = r.Form["web_client_options"]
filters.DefaultSharesExpiration = int(defaultSharesExpiration) filters.DefaultSharesExpiration = defaultSharesExpiration
filters.PasswordExpiration = int(passwordExpiration) filters.PasswordExpiration = passwordExpiration
filters.PasswordStrength = int(passwordStrength) filters.PasswordStrength = passwordStrength
hooks := r.Form["hooks"] hooks := r.Form["hooks"]
if util.Contains(hooks, "external_auth_disabled") { if util.Contains(hooks, "external_auth_disabled") {
filters.Hooks.ExternalAuthDisabled = true filters.Hooks.ExternalAuthDisabled = true
@@ -1830,11 +1830,11 @@ func getAdminFromPostFields(r *http.Request) (dataprovider.Admin, error) {
admin.Filters.Preferences.HideUserPageSections = getAdminHiddenUserPageSections(r) admin.Filters.Preferences.HideUserPageSections = getAdminHiddenUserPageSections(r)
admin.Filters.Preferences.DefaultUsersExpiration = 0 admin.Filters.Preferences.DefaultUsersExpiration = 0
if val := r.Form.Get("default_users_expiration"); val != "" { if val := r.Form.Get("default_users_expiration"); val != "" {
defaultUsersExpiration, err := strconv.ParseInt(r.Form.Get("default_users_expiration"), 10, 64) defaultUsersExpiration, err := strconv.Atoi(r.Form.Get("default_users_expiration"))
if err != nil { if err != nil {
return admin, fmt.Errorf("invalid default users expiration: %w", err) return admin, fmt.Errorf("invalid default users expiration: %w", err)
} }
admin.Filters.Preferences.DefaultUsersExpiration = int(defaultUsersExpiration) admin.Filters.Preferences.DefaultUsersExpiration = defaultUsersExpiration
} }
for k := range r.Form { for k := range r.Form {
if strings.HasPrefix(k, "group") { if strings.HasPrefix(k, "group") {
@@ -2130,7 +2130,7 @@ func getGroupFromPostFields(r *http.Request) (dataprovider.Group, error) {
if err != nil { if err != nil {
return group, err return group, err
} }
expiresIn, err := strconv.ParseInt(r.Form.Get("expires_in"), 10, 64) expiresIn, err := strconv.Atoi(r.Form.Get("expires_in"))
if err != nil { if err != nil {
return group, fmt.Errorf("invalid expires in: %w", err) return group, fmt.Errorf("invalid expires in: %w", err)
} }
@@ -2159,7 +2159,7 @@ func getGroupFromPostFields(r *http.Request) (dataprovider.Group, error) {
UploadDataTransfer: dataTransferUL, UploadDataTransfer: dataTransferUL,
DownloadDataTransfer: dataTransferDL, DownloadDataTransfer: dataTransferDL,
TotalDataTransfer: dataTransferTotal, TotalDataTransfer: dataTransferTotal,
ExpiresIn: int(expiresIn), ExpiresIn: expiresIn,
Filters: filters, Filters: filters,
}, },
FsConfig: fsConfig, FsConfig: fsConfig,

View File

@@ -1492,7 +1492,7 @@ func (s *httpdServer) handleClientShareLoginPost(w http.ResponseWriter, r *http.
s.renderShareLoginPage(w, r.RequestURI, common.ErrInternalFailure.Error(), ipAddr) s.renderShareLoginPage(w, r.RequestURI, common.ErrInternalFailure.Error(), ipAddr)
return return
} }
next := r.URL.Query().Get("next") next := path.Clean(r.URL.Query().Get("next"))
if strings.HasPrefix(next, path.Join(webClientPubSharesPath, share.ShareID)) { if strings.HasPrefix(next, path.Join(webClientPubSharesPath, share.ShareID)) {
http.Redirect(w, r, next, http.StatusFound) http.Redirect(w, r, next, http.StatusFound)
} }

View File

@@ -29,7 +29,6 @@ import (
"github.com/drakkan/sftpgo/v2/internal/common" "github.com/drakkan/sftpgo/v2/internal/common"
"github.com/drakkan/sftpgo/v2/internal/dataprovider" "github.com/drakkan/sftpgo/v2/internal/dataprovider"
"github.com/drakkan/sftpgo/v2/internal/logger" "github.com/drakkan/sftpgo/v2/internal/logger"
"github.com/drakkan/sftpgo/v2/internal/util"
"github.com/drakkan/sftpgo/v2/internal/vfs" "github.com/drakkan/sftpgo/v2/internal/vfs"
) )
@@ -58,10 +57,9 @@ func (c *scpCommand) handle() (err error) {
defer common.Connections.Remove(c.connection.GetID()) defer common.Connections.Remove(c.connection.GetID())
destPath := c.getDestPath() destPath := c.getDestPath()
commandType := c.getCommandType() c.connection.Log(logger.LevelDebug, "handle scp command, args: %v user: %s, dest path: %q",
c.connection.Log(logger.LevelDebug, "handle scp command, args: %v user: %v command type: %v, dest path: %q", c.args, c.connection.User.Username, destPath)
c.args, c.connection.User.Username, commandType, destPath) if c.hasFlag("t") {
if commandType == "-t" {
// -t means "to", so upload // -t means "to", so upload
err = c.sendConfirmationMessage() err = c.sendConfirmationMessage()
if err != nil { if err != nil {
@@ -71,7 +69,7 @@ func (c *scpCommand) handle() (err error) {
if err != nil { if err != nil {
return err return err
} }
} else if commandType == "-f" { } else if c.hasFlag("f") {
// -f means "from" so download // -f means "from" so download
err = c.readConfirmationMessage() err = c.readConfirmationMessage()
if err != nil { if err != nil {
@@ -351,7 +349,7 @@ func (c *scpCommand) sendDownloadProtocolMessages(virtualDirPath string, stat os
var err error var err error
if c.sendFileTime() { if c.sendFileTime() {
modTime := stat.ModTime().UnixNano() / 1000000000 modTime := stat.ModTime().UnixNano() / 1000000000
tCommand := fmt.Sprintf("T%v 0 %v 0\n", modTime, modTime) tCommand := fmt.Sprintf("T%d 0 %d 0\n", modTime, modTime)
err = c.sendProtocolMessage(tCommand) err = c.sendProtocolMessage(tCommand)
if err != nil { if err != nil {
return err return err
@@ -433,7 +431,7 @@ func (c *scpCommand) sendDownloadFileData(fs vfs.Fs, filePath string, stat os.Fi
var err error var err error
if c.sendFileTime() { if c.sendFileTime() {
modTime := stat.ModTime().UnixNano() / 1000000000 modTime := stat.ModTime().UnixNano() / 1000000000
tCommand := fmt.Sprintf("T%v 0 %v 0\n", modTime, modTime) tCommand := fmt.Sprintf("T%d 0 %d 0\n", modTime, modTime)
err = c.sendProtocolMessage(tCommand) err = c.sendProtocolMessage(tCommand)
if err != nil { if err != nil {
return err return err
@@ -561,16 +559,22 @@ func (c *scpCommand) handleDownload(filePath string) error {
return err return err
} }
func (c *scpCommand) getCommandType() string {
return c.args[len(c.args)-2]
}
func (c *scpCommand) sendFileTime() bool { func (c *scpCommand) sendFileTime() bool {
return util.Contains(c.args, "-p") return c.hasFlag("p")
} }
func (c *scpCommand) isRecursive() bool { func (c *scpCommand) isRecursive() bool {
return util.Contains(c.args, "-r") return c.hasFlag("r")
}
func (c *scpCommand) hasFlag(flag string) bool {
for idx := 0; idx < len(c.args)-1; idx++ {
arg := c.args[idx]
if !strings.HasPrefix(arg, "--") && strings.HasPrefix(arg, "-") && strings.Contains(arg, flag) {
return true
}
}
return false
} }
// read the SCP confirmation message and the optional text message // read the SCP confirmation message and the optional text message