diff --git a/cmd/initprovider.go b/cmd/initprovider.go index af2389fa..3a690fc3 100644 --- a/cmd/initprovider.go +++ b/cmd/initprovider.go @@ -1,6 +1,8 @@ package cmd import ( + "path/filepath" + "github.com/drakkan/sftpgo/config" "github.com/drakkan/sftpgo/dataprovider" "github.com/drakkan/sftpgo/logger" @@ -29,6 +31,7 @@ Please take a look at the usage below to customize the options.`, Run: func(cmd *cobra.Command, args []string) { logger.DisableLogger() logger.EnableConsoleLogger(zerolog.DebugLevel) + configDir = filepath.Clean(configDir) config.LoadConfig(configDir, configFile) providerConf := config.GetProviderConf() logger.DebugToConsole("Initializing provider: %#v config file: %#v", providerConf.Driver, viper.ConfigFileUsed()) diff --git a/cmd/root.go b/cmd/root.go index 1221c8f6..f53a4b97 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -4,6 +4,7 @@ package cmd import ( "fmt" "os" + "path/filepath" "strconv" "github.com/drakkan/sftpgo/config" @@ -140,6 +141,7 @@ func addServeFlags(cmd *cobra.Command) { func getCustomServeFlags() []string { result := []string{} if configDir != defaultConfigDir { + configDir = filepath.Clean(configDir) result = append(result, "--"+configDirFlag) result = append(result, configDir) } @@ -147,7 +149,10 @@ func getCustomServeFlags() []string { result = append(result, "--"+configFileFlag) result = append(result, configFile) } - if logFilePath != defaultLogFile { + if logFilePath != defaultLogFile && len(logFilePath) > 0 && logFilePath != "." { + if !filepath.IsAbs(logFilePath) { + logFilePath = filepath.Join(configDir, logFilePath) + } result = append(result, "--"+logFilePathFlag) result = append(result, logFilePath) } diff --git a/cmd/start_windows.go b/cmd/start_windows.go index d1ce8a8e..74d3768e 100644 --- a/cmd/start_windows.go +++ b/cmd/start_windows.go @@ -13,8 +13,12 @@ var ( Use: "start", Short: "Start SFTPGo Windows Service", Run: func(cmd *cobra.Command, args []string) { + configDir = filepath.Clean(configDir) + if !filepath.IsAbs(logFilePath) && len(logFilePath) > 0 && logFilePath != "." { + logFilePath = filepath.Join(configDir, logFilePath) + } s := service.Service{ - ConfigDir: filepath.Clean(configDir), + ConfigDir: configDir, ConfigFile: configFile, LogFilePath: logFilePath, LogMaxSize: logMaxSize, diff --git a/dataprovider/bolt.go b/dataprovider/bolt.go index 780210f6..e70e6fd4 100644 --- a/dataprovider/bolt.go +++ b/dataprovider/bolt.go @@ -55,6 +55,9 @@ func initializeBoltProvider(basePath string) error { var err error logSender = BoltDataProviderName dbPath := config.Name + if dbPath == "." { + return fmt.Errorf("Invalid database path: %#v", dbPath) + } if !filepath.IsAbs(dbPath) { dbPath = filepath.Join(basePath, dbPath) } diff --git a/dataprovider/memory.go b/dataprovider/memory.go index 51a98e14..a517d668 100644 --- a/dataprovider/memory.go +++ b/dataprovider/memory.go @@ -39,7 +39,7 @@ type MemoryProvider struct { func initializeMemoryProvider(basePath string) error { configFile := "" - if len(config.Name) > 0 { + if len(config.Name) > 0 && config.Name != "." { configFile = config.Name if !filepath.IsAbs(configFile) { configFile = filepath.Join(basePath, configFile) diff --git a/dataprovider/sqlite.go b/dataprovider/sqlite.go index d606dbec..8d8043fb 100644 --- a/dataprovider/sqlite.go +++ b/dataprovider/sqlite.go @@ -32,6 +32,9 @@ func initializeSQLiteProvider(basePath string) error { logSender = SQLiteDataProviderName if len(config.ConnectionString) == 0 { dbPath := config.Name + if dbPath == "." { + return fmt.Errorf("Invalid database path: %#v", dbPath) + } if !filepath.IsAbs(dbPath) { dbPath = filepath.Join(basePath, dbPath) } diff --git a/httpd/httpd.go b/httpd/httpd.go index 3a18729e..1d863d53 100644 --- a/httpd/httpd.go +++ b/httpd/httpd.go @@ -129,7 +129,7 @@ func ReloadTLSCertificate() { } func getConfigPath(name, configDir string) string { - if len(name) > 0 && !filepath.IsAbs(name) { + if len(name) > 0 && !filepath.IsAbs(name) && name != "." { return filepath.Join(configDir, name) } return name diff --git a/logger/logger.go b/logger/logger.go index afc2b0af..03af6ec7 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -11,6 +11,7 @@ package logger import ( "fmt" "os" + "path/filepath" "runtime" "sync" @@ -46,7 +47,7 @@ func GetLogger() *zerolog.Logger { // InitLogger configures the logger using the given parameters func InitLogger(logFilePath string, logMaxSize int, logMaxBackups int, logMaxAge int, logCompress bool, level zerolog.Level) { zerolog.TimeFieldFormat = dateFormat - if len(logFilePath) > 0 { + if len(logFilePath) > 0 && filepath.Clean(logFilePath) != "." { logger = zerolog.New(&lumberjack.Logger{ Filename: logFilePath, MaxSize: logMaxSize, diff --git a/service/service.go b/service/service.go index 7008eadd..8ee3a0e2 100644 --- a/service/service.go +++ b/service/service.go @@ -50,6 +50,9 @@ func (s *Service) Start() error { if !s.LogVerbose { logLevel = zerolog.InfoLevel } + if !filepath.IsAbs(s.LogFilePath) && len(s.LogFilePath) > 0 && s.LogFilePath != "." { + s.LogFilePath = filepath.Join(s.ConfigDir, s.LogFilePath) + } logger.InitLogger(s.LogFilePath, s.LogMaxSize, s.LogMaxBackups, s.LogMaxAge, s.LogCompress, logLevel) if s.PortableMode == 1 { logger.EnableConsoleLogger(logLevel)