mirror of
https://github.com/drakkan/sftpgo.git
synced 2025-12-07 23:00:55 +03:00
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:
@@ -1,6 +1,8 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/drakkan/sftpgo/config"
|
"github.com/drakkan/sftpgo/config"
|
||||||
"github.com/drakkan/sftpgo/dataprovider"
|
"github.com/drakkan/sftpgo/dataprovider"
|
||||||
"github.com/drakkan/sftpgo/logger"
|
"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) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
logger.DisableLogger()
|
logger.DisableLogger()
|
||||||
logger.EnableConsoleLogger(zerolog.DebugLevel)
|
logger.EnableConsoleLogger(zerolog.DebugLevel)
|
||||||
|
configDir = filepath.Clean(configDir)
|
||||||
config.LoadConfig(configDir, configFile)
|
config.LoadConfig(configDir, configFile)
|
||||||
providerConf := config.GetProviderConf()
|
providerConf := config.GetProviderConf()
|
||||||
logger.DebugToConsole("Initializing provider: %#v config file: %#v", providerConf.Driver, viper.ConfigFileUsed())
|
logger.DebugToConsole("Initializing provider: %#v config file: %#v", providerConf.Driver, viper.ConfigFileUsed())
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ package cmd
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/drakkan/sftpgo/config"
|
"github.com/drakkan/sftpgo/config"
|
||||||
@@ -140,6 +141,7 @@ func addServeFlags(cmd *cobra.Command) {
|
|||||||
func getCustomServeFlags() []string {
|
func getCustomServeFlags() []string {
|
||||||
result := []string{}
|
result := []string{}
|
||||||
if configDir != defaultConfigDir {
|
if configDir != defaultConfigDir {
|
||||||
|
configDir = filepath.Clean(configDir)
|
||||||
result = append(result, "--"+configDirFlag)
|
result = append(result, "--"+configDirFlag)
|
||||||
result = append(result, configDir)
|
result = append(result, configDir)
|
||||||
}
|
}
|
||||||
@@ -147,7 +149,10 @@ func getCustomServeFlags() []string {
|
|||||||
result = append(result, "--"+configFileFlag)
|
result = append(result, "--"+configFileFlag)
|
||||||
result = append(result, configFile)
|
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, "--"+logFilePathFlag)
|
||||||
result = append(result, logFilePath)
|
result = append(result, logFilePath)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,8 +13,12 @@ var (
|
|||||||
Use: "start",
|
Use: "start",
|
||||||
Short: "Start SFTPGo Windows Service",
|
Short: "Start SFTPGo Windows Service",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
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{
|
s := service.Service{
|
||||||
ConfigDir: filepath.Clean(configDir),
|
ConfigDir: configDir,
|
||||||
ConfigFile: configFile,
|
ConfigFile: configFile,
|
||||||
LogFilePath: logFilePath,
|
LogFilePath: logFilePath,
|
||||||
LogMaxSize: logMaxSize,
|
LogMaxSize: logMaxSize,
|
||||||
|
|||||||
@@ -55,6 +55,9 @@ func initializeBoltProvider(basePath string) error {
|
|||||||
var err error
|
var err error
|
||||||
logSender = BoltDataProviderName
|
logSender = BoltDataProviderName
|
||||||
dbPath := config.Name
|
dbPath := config.Name
|
||||||
|
if dbPath == "." {
|
||||||
|
return fmt.Errorf("Invalid database path: %#v", dbPath)
|
||||||
|
}
|
||||||
if !filepath.IsAbs(dbPath) {
|
if !filepath.IsAbs(dbPath) {
|
||||||
dbPath = filepath.Join(basePath, dbPath)
|
dbPath = filepath.Join(basePath, dbPath)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ type MemoryProvider struct {
|
|||||||
|
|
||||||
func initializeMemoryProvider(basePath string) error {
|
func initializeMemoryProvider(basePath string) error {
|
||||||
configFile := ""
|
configFile := ""
|
||||||
if len(config.Name) > 0 {
|
if len(config.Name) > 0 && config.Name != "." {
|
||||||
configFile = config.Name
|
configFile = config.Name
|
||||||
if !filepath.IsAbs(configFile) {
|
if !filepath.IsAbs(configFile) {
|
||||||
configFile = filepath.Join(basePath, configFile)
|
configFile = filepath.Join(basePath, configFile)
|
||||||
|
|||||||
@@ -32,6 +32,9 @@ func initializeSQLiteProvider(basePath string) error {
|
|||||||
logSender = SQLiteDataProviderName
|
logSender = SQLiteDataProviderName
|
||||||
if len(config.ConnectionString) == 0 {
|
if len(config.ConnectionString) == 0 {
|
||||||
dbPath := config.Name
|
dbPath := config.Name
|
||||||
|
if dbPath == "." {
|
||||||
|
return fmt.Errorf("Invalid database path: %#v", dbPath)
|
||||||
|
}
|
||||||
if !filepath.IsAbs(dbPath) {
|
if !filepath.IsAbs(dbPath) {
|
||||||
dbPath = filepath.Join(basePath, dbPath)
|
dbPath = filepath.Join(basePath, dbPath)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -129,7 +129,7 @@ func ReloadTLSCertificate() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getConfigPath(name, configDir string) string {
|
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 filepath.Join(configDir, name)
|
||||||
}
|
}
|
||||||
return name
|
return name
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ package logger
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
@@ -46,7 +47,7 @@ func GetLogger() *zerolog.Logger {
|
|||||||
// InitLogger configures the logger using the given parameters
|
// InitLogger configures the logger using the given parameters
|
||||||
func InitLogger(logFilePath string, logMaxSize int, logMaxBackups int, logMaxAge int, logCompress bool, level zerolog.Level) {
|
func InitLogger(logFilePath string, logMaxSize int, logMaxBackups int, logMaxAge int, logCompress bool, level zerolog.Level) {
|
||||||
zerolog.TimeFieldFormat = dateFormat
|
zerolog.TimeFieldFormat = dateFormat
|
||||||
if len(logFilePath) > 0 {
|
if len(logFilePath) > 0 && filepath.Clean(logFilePath) != "." {
|
||||||
logger = zerolog.New(&lumberjack.Logger{
|
logger = zerolog.New(&lumberjack.Logger{
|
||||||
Filename: logFilePath,
|
Filename: logFilePath,
|
||||||
MaxSize: logMaxSize,
|
MaxSize: logMaxSize,
|
||||||
|
|||||||
@@ -50,6 +50,9 @@ func (s *Service) Start() error {
|
|||||||
if !s.LogVerbose {
|
if !s.LogVerbose {
|
||||||
logLevel = zerolog.InfoLevel
|
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)
|
logger.InitLogger(s.LogFilePath, s.LogMaxSize, s.LogMaxBackups, s.LogMaxAge, s.LogCompress, logLevel)
|
||||||
if s.PortableMode == 1 {
|
if s.PortableMode == 1 {
|
||||||
logger.EnableConsoleLogger(logLevel)
|
logger.EnableConsoleLogger(logLevel)
|
||||||
|
|||||||
Reference in New Issue
Block a user