log file: if the path is not absolute make it relative to config dir

Also refuse to join invalid file name such as "."

Fixes #85
This commit is contained in:
Nicola Murino 2020-03-03 00:34:06 +01:00
parent df67f4ef34
commit d0a81cabab
9 changed files with 27 additions and 5 deletions

View file

@ -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())

View file

@ -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)
}

View file

@ -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,

View file

@ -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)
}

View file

@ -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)

View file

@ -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)
}

View file

@ -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

View file

@ -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,

View file

@ -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)