2019-07-30 18:51:29 +00:00
|
|
|
// Package config manages the configuration.
|
|
|
|
// Configuration is loaded from sftpgo.conf file.
|
|
|
|
// If sftpgo.conf is not found or cannot be readed or decoded as json the default configuration is used.
|
|
|
|
// The default configuration an be found inside the source tree:
|
|
|
|
// https://github.com/drakkan/sftpgo/blob/master/sftpgo.conf
|
2019-07-20 10:26:52 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2019-08-04 07:37:58 +00:00
|
|
|
"fmt"
|
2019-07-29 06:53:22 +00:00
|
|
|
"strings"
|
2019-07-20 10:26:52 +00:00
|
|
|
|
|
|
|
"github.com/drakkan/sftpgo/api"
|
|
|
|
"github.com/drakkan/sftpgo/dataprovider"
|
|
|
|
"github.com/drakkan/sftpgo/logger"
|
|
|
|
"github.com/drakkan/sftpgo/sftpd"
|
2019-08-07 20:46:13 +00:00
|
|
|
"github.com/spf13/viper"
|
2019-07-20 10:26:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-07-29 06:53:22 +00:00
|
|
|
logSender = "config"
|
|
|
|
defaultBanner = "SFTPGo"
|
2019-08-07 20:46:13 +00:00
|
|
|
// DefaultConfigName defines the name for the default config file.
|
|
|
|
// This is the file name without extension, we use viper and so we
|
|
|
|
// support all the config files format supported by viper
|
|
|
|
DefaultConfigName = "sftpgo"
|
|
|
|
// ConfigEnvPrefix defines a prefix that ENVIRONMENT variables will use
|
|
|
|
configEnvPrefix = "sftpgo"
|
2019-07-20 10:26:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
globalConf globalConfig
|
|
|
|
)
|
|
|
|
|
|
|
|
type globalConfig struct {
|
2019-08-07 20:46:13 +00:00
|
|
|
SFTPD sftpd.Configuration `json:"sftpd" mapstructure:"sftpd"`
|
|
|
|
ProviderConf dataprovider.Config `json:"data_provider" mapstructure:"data_provider"`
|
|
|
|
HTTPDConfig api.HTTPDConf `json:"httpd" mapstructure:"httpd"`
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// create a default configuration to use if no config file is provided
|
|
|
|
globalConf = globalConfig{
|
|
|
|
SFTPD: sftpd.Configuration{
|
2019-07-29 06:53:22 +00:00
|
|
|
Banner: defaultBanner,
|
2019-07-21 10:02:24 +00:00
|
|
|
BindPort: 2022,
|
|
|
|
BindAddress: "",
|
|
|
|
IdleTimeout: 15,
|
|
|
|
MaxAuthTries: 0,
|
|
|
|
Umask: "0022",
|
2019-08-04 07:37:58 +00:00
|
|
|
UploadMode: 0,
|
2019-07-27 07:38:09 +00:00
|
|
|
Actions: sftpd.Actions{
|
|
|
|
ExecuteOn: []string{},
|
|
|
|
Command: "",
|
|
|
|
HTTPNotificationURL: "",
|
|
|
|
},
|
2019-09-03 10:08:09 +00:00
|
|
|
Keys: []sftpd.Key{},
|
|
|
|
IsSCPEnabled: false,
|
|
|
|
KexAlgorithms: []string{},
|
|
|
|
Ciphers: []string{},
|
|
|
|
MACs: []string{},
|
|
|
|
LoginBannerFile: "",
|
2019-07-20 10:26:52 +00:00
|
|
|
},
|
|
|
|
ProviderConf: dataprovider.Config{
|
|
|
|
Driver: "sqlite",
|
|
|
|
Name: "sftpgo.db",
|
|
|
|
Host: "",
|
|
|
|
Port: 5432,
|
|
|
|
Username: "",
|
|
|
|
Password: "",
|
|
|
|
ConnectionString: "",
|
|
|
|
UsersTable: "users",
|
|
|
|
ManageUsers: 1,
|
|
|
|
SSLMode: 0,
|
|
|
|
TrackQuota: 1,
|
|
|
|
},
|
|
|
|
HTTPDConfig: api.HTTPDConf{
|
|
|
|
BindPort: 8080,
|
|
|
|
BindAddress: "127.0.0.1",
|
|
|
|
},
|
|
|
|
}
|
2019-08-07 20:46:13 +00:00
|
|
|
|
|
|
|
viper.SetEnvPrefix(configEnvPrefix)
|
|
|
|
replacer := strings.NewReplacer(".", "__")
|
|
|
|
viper.SetEnvKeyReplacer(replacer)
|
|
|
|
viper.SetConfigName(DefaultConfigName)
|
2019-08-08 08:01:33 +00:00
|
|
|
setViperAdditionalConfigPaths()
|
2019-08-07 20:46:13 +00:00
|
|
|
viper.AddConfigPath(".")
|
|
|
|
viper.AutomaticEnv()
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 18:51:29 +00:00
|
|
|
// GetSFTPDConfig returns the configuration for the SFTP server
|
2019-07-20 10:26:52 +00:00
|
|
|
func GetSFTPDConfig() sftpd.Configuration {
|
|
|
|
return globalConf.SFTPD
|
|
|
|
}
|
|
|
|
|
2019-07-30 18:51:29 +00:00
|
|
|
// GetHTTPDConfig returns the configuration for the HTTP server
|
2019-07-20 10:26:52 +00:00
|
|
|
func GetHTTPDConfig() api.HTTPDConf {
|
|
|
|
return globalConf.HTTPDConfig
|
|
|
|
}
|
|
|
|
|
2019-07-30 18:51:29 +00:00
|
|
|
//GetProviderConf returns the configuration for the data provider
|
2019-07-20 10:26:52 +00:00
|
|
|
func GetProviderConf() dataprovider.Config {
|
|
|
|
return globalConf.ProviderConf
|
|
|
|
}
|
|
|
|
|
2019-08-07 20:46:13 +00:00
|
|
|
// LoadConfig loads the configuration
|
|
|
|
// configDir will be added to the configuration search paths.
|
|
|
|
// The search path contains by default the current directory and on linux it contains
|
|
|
|
// $HOME/.config/sftpgo and /etc/sftpgo too.
|
|
|
|
// configName is the name of the configuration to search without extension
|
|
|
|
func LoadConfig(configDir, configName string) error {
|
|
|
|
var err error
|
|
|
|
viper.AddConfigPath(configDir)
|
|
|
|
viper.SetConfigName(configName)
|
|
|
|
if err = viper.ReadInConfig(); err != nil {
|
2019-07-21 10:02:24 +00:00
|
|
|
logger.Warn(logSender, "error loading configuration file: %v. Default configuration will be used: %+v", err, globalConf)
|
2019-07-31 06:14:31 +00:00
|
|
|
logger.WarnToConsole("error loading configuration file: %v. Default configuration will be used.", err)
|
2019-07-20 10:26:52 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-08-07 20:46:13 +00:00
|
|
|
err = viper.Unmarshal(&globalConf)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
2019-07-31 06:14:31 +00:00
|
|
|
logger.Warn(logSender, "error parsing configuration file: %v. Default configuration will be used: %+v", err, globalConf)
|
|
|
|
logger.WarnToConsole("error parsing configuration file: %v. Default configuration will be used.", err)
|
2019-07-20 10:26:52 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-07-29 06:53:22 +00:00
|
|
|
if strings.TrimSpace(globalConf.SFTPD.Banner) == "" {
|
|
|
|
globalConf.SFTPD.Banner = defaultBanner
|
|
|
|
}
|
2019-08-04 07:37:58 +00:00
|
|
|
if globalConf.SFTPD.UploadMode < 0 || globalConf.SFTPD.UploadMode > 1 {
|
|
|
|
err = fmt.Errorf("Invalid upload_mode 0 and 1 are supported, configured: %v reset upload_mode to 0",
|
|
|
|
globalConf.SFTPD.UploadMode)
|
|
|
|
globalConf.SFTPD.UploadMode = 0
|
|
|
|
logger.Warn(logSender, "Configuration error: %v", err)
|
|
|
|
logger.WarnToConsole("Configuration error: %v", err)
|
|
|
|
}
|
2019-08-07 20:46:13 +00:00
|
|
|
logger.Debug(logSender, "config file used: '%v', config loaded: %+v", viper.ConfigFileUsed(), globalConf)
|
2019-07-20 10:26:52 +00:00
|
|
|
return err
|
|
|
|
}
|