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
|
|
|
|
2020-05-06 17:36:34 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
2019-07-20 10:26:52 +00:00
|
|
|
"github.com/drakkan/sftpgo/dataprovider"
|
2020-04-26 21:29:09 +00:00
|
|
|
"github.com/drakkan/sftpgo/httpclient"
|
2019-10-07 16:19:01 +00:00
|
|
|
"github.com/drakkan/sftpgo/httpd"
|
2019-07-20 10:26:52 +00:00
|
|
|
"github.com/drakkan/sftpgo/logger"
|
|
|
|
"github.com/drakkan/sftpgo/sftpd"
|
2019-09-12 07:21:58 +00:00
|
|
|
"github.com/drakkan/sftpgo/utils"
|
2019-07-20 10:26:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-09-12 07:21:58 +00:00
|
|
|
logSender = "config"
|
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 (
|
2019-09-12 07:21:58 +00:00
|
|
|
globalConf globalConfig
|
|
|
|
defaultBanner = fmt.Sprintf("SFTPGo_%v", utils.GetAppVersion().Version)
|
2019-07-20 10:26:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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"`
|
2019-10-07 16:19:01 +00:00
|
|
|
HTTPDConfig httpd.Conf `json:"httpd" mapstructure:"httpd"`
|
2020-04-26 21:29:09 +00:00
|
|
|
HTTPConfig httpclient.Config `json:"http" mapstructure:"http"`
|
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{
|
2020-05-24 13:29:39 +00:00
|
|
|
ExecuteOn: []string{},
|
|
|
|
Hook: "",
|
2019-07-27 07:38:09 +00:00
|
|
|
},
|
2020-05-16 21:26:44 +00:00
|
|
|
HostKeys: []string{},
|
2020-04-01 21:25:23 +00:00
|
|
|
KexAlgorithms: []string{},
|
|
|
|
Ciphers: []string{},
|
|
|
|
MACs: []string{},
|
2020-05-15 18:08:53 +00:00
|
|
|
TrustedUserCAKeys: []string{},
|
2020-04-01 21:25:23 +00:00
|
|
|
LoginBannerFile: "",
|
|
|
|
EnabledSSHCommands: sftpd.GetDefaultSSHCommands(),
|
|
|
|
KeyboardInteractiveHook: "",
|
|
|
|
ProxyProtocol: 0,
|
|
|
|
ProxyAllowed: []string{},
|
2019-07-20 10:26:52 +00:00
|
|
|
},
|
|
|
|
ProviderConf: dataprovider.Config{
|
|
|
|
Driver: "sqlite",
|
|
|
|
Name: "sftpgo.db",
|
|
|
|
Host: "",
|
|
|
|
Port: 5432,
|
|
|
|
Username: "",
|
|
|
|
Password: "",
|
|
|
|
ConnectionString: "",
|
2020-06-07 21:30:18 +00:00
|
|
|
SQLTablesPrefix: "",
|
2019-07-20 10:26:52 +00:00
|
|
|
ManageUsers: 1,
|
|
|
|
SSLMode: 0,
|
|
|
|
TrackQuota: 1,
|
2019-09-13 06:14:07 +00:00
|
|
|
PoolSize: 0,
|
2019-09-28 20:48:52 +00:00
|
|
|
UsersBaseDir: "",
|
2019-11-14 10:06:03 +00:00
|
|
|
Actions: dataprovider.Actions{
|
2020-05-24 13:29:39 +00:00
|
|
|
ExecuteOn: []string{},
|
|
|
|
Hook: "",
|
2019-11-14 10:06:03 +00:00
|
|
|
},
|
2020-04-01 21:25:23 +00:00
|
|
|
ExternalAuthHook: "",
|
|
|
|
ExternalAuthScope: 0,
|
|
|
|
CredentialsPath: "credentials",
|
|
|
|
PreLoginHook: "",
|
2019-07-20 10:26:52 +00:00
|
|
|
},
|
2019-10-07 16:19:01 +00:00
|
|
|
HTTPDConfig: httpd.Conf{
|
2020-02-03 23:08:00 +00:00
|
|
|
BindPort: 8080,
|
|
|
|
BindAddress: "127.0.0.1",
|
|
|
|
TemplatesPath: "templates",
|
|
|
|
StaticFilesPath: "static",
|
|
|
|
BackupsPath: "backups",
|
|
|
|
AuthUserFile: "",
|
|
|
|
CertificateFile: "",
|
|
|
|
CertificateKeyFile: "",
|
2019-07-20 10:26:52 +00:00
|
|
|
},
|
2020-04-26 21:29:09 +00:00
|
|
|
HTTPConfig: httpclient.Config{
|
|
|
|
Timeout: 20,
|
|
|
|
CACertificates: nil,
|
2020-05-03 09:37:50 +00:00
|
|
|
SkipTLSVerify: false,
|
2020-04-26 21:29:09 +00:00
|
|
|
},
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2019-08-07 20:46:13 +00:00
|
|
|
|
|
|
|
viper.SetEnvPrefix(configEnvPrefix)
|
|
|
|
replacer := strings.NewReplacer(".", "__")
|
|
|
|
viper.SetEnvKeyReplacer(replacer)
|
|
|
|
viper.SetConfigName(DefaultConfigName)
|
|
|
|
viper.AutomaticEnv()
|
2019-09-03 21:13:33 +00:00
|
|
|
viper.AllowEmptyEnv(true)
|
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-10-24 16:50:35 +00:00
|
|
|
// SetSFTPDConfig sets the configuration for the SFTP server
|
|
|
|
func SetSFTPDConfig(config sftpd.Configuration) {
|
|
|
|
globalConf.SFTPD = config
|
|
|
|
}
|
|
|
|
|
2019-07-30 18:51:29 +00:00
|
|
|
// GetHTTPDConfig returns the configuration for the HTTP server
|
2019-10-07 16:19:01 +00:00
|
|
|
func GetHTTPDConfig() httpd.Conf {
|
2019-07-20 10:26:52 +00:00
|
|
|
return globalConf.HTTPDConfig
|
|
|
|
}
|
|
|
|
|
2019-10-24 16:50:35 +00:00
|
|
|
// SetHTTPDConfig sets the configuration for the HTTP server
|
|
|
|
func SetHTTPDConfig(config httpd.Conf) {
|
|
|
|
globalConf.HTTPDConfig = config
|
|
|
|
}
|
|
|
|
|
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-10-24 16:50:35 +00:00
|
|
|
//SetProviderConf sets the configuration for the data provider
|
|
|
|
func SetProviderConf(config dataprovider.Config) {
|
|
|
|
globalConf.ProviderConf = config
|
|
|
|
}
|
|
|
|
|
2020-04-26 21:29:09 +00:00
|
|
|
// GetHTTPConfig returns the configuration for HTTP clients
|
|
|
|
func GetHTTPConfig() httpclient.Config {
|
|
|
|
return globalConf.HTTPConfig
|
|
|
|
}
|
|
|
|
|
2019-09-13 19:57:52 +00:00
|
|
|
func getRedactedGlobalConf() globalConfig {
|
|
|
|
conf := globalConf
|
|
|
|
conf.ProviderConf.Password = "[redacted]"
|
|
|
|
return conf
|
|
|
|
}
|
|
|
|
|
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)
|
2019-09-05 20:07:59 +00:00
|
|
|
setViperAdditionalConfigPaths()
|
|
|
|
viper.AddConfigPath(".")
|
2019-08-07 20:46:13 +00:00
|
|
|
viper.SetConfigName(configName)
|
|
|
|
if err = viper.ReadInConfig(); err != nil {
|
2019-09-13 19:57:52 +00:00
|
|
|
logger.Warn(logSender, "", "error loading configuration file: %v. Default configuration will be used: %+v",
|
|
|
|
err, getRedactedGlobalConf())
|
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-09-13 19:57:52 +00:00
|
|
|
logger.Warn(logSender, "", "error parsing configuration file: %v. Default configuration will be used: %+v",
|
|
|
|
err, getRedactedGlobalConf())
|
2019-07-31 06:14:31 +00:00
|
|
|
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
|
|
|
|
}
|
2020-03-03 08:09:58 +00:00
|
|
|
if len(globalConf.ProviderConf.UsersBaseDir) > 0 && !utils.IsFileInputValid(globalConf.ProviderConf.UsersBaseDir) {
|
|
|
|
err = fmt.Errorf("invalid users base dir %#v will be ignored", globalConf.ProviderConf.UsersBaseDir)
|
|
|
|
globalConf.ProviderConf.UsersBaseDir = ""
|
|
|
|
logger.Warn(logSender, "", "Configuration error: %v", err)
|
|
|
|
logger.WarnToConsole("Configuration error: %v", err)
|
|
|
|
}
|
2019-10-09 15:33:30 +00:00
|
|
|
if globalConf.SFTPD.UploadMode < 0 || globalConf.SFTPD.UploadMode > 2 {
|
2020-02-28 23:02:06 +00:00
|
|
|
err = fmt.Errorf("invalid upload_mode 0, 1 and 2 are supported, configured: %v reset upload_mode to 0",
|
2019-08-04 07:37:58 +00:00
|
|
|
globalConf.SFTPD.UploadMode)
|
|
|
|
globalConf.SFTPD.UploadMode = 0
|
2019-09-05 14:21:35 +00:00
|
|
|
logger.Warn(logSender, "", "Configuration error: %v", err)
|
2019-08-04 07:37:58 +00:00
|
|
|
logger.WarnToConsole("Configuration error: %v", err)
|
|
|
|
}
|
2020-02-28 23:02:06 +00:00
|
|
|
if globalConf.SFTPD.ProxyProtocol < 0 || globalConf.SFTPD.ProxyProtocol > 2 {
|
|
|
|
err = fmt.Errorf("invalid proxy_protocol 0, 1 and 2 are supported, configured: %v reset proxy_protocol to 0",
|
|
|
|
globalConf.SFTPD.ProxyProtocol)
|
|
|
|
globalConf.SFTPD.ProxyProtocol = 0
|
|
|
|
logger.Warn(logSender, "", "Configuration error: %v", err)
|
|
|
|
logger.WarnToConsole("Configuration error: %v", err)
|
|
|
|
}
|
2020-01-21 09:54:05 +00:00
|
|
|
if globalConf.ProviderConf.ExternalAuthScope < 0 || globalConf.ProviderConf.ExternalAuthScope > 7 {
|
2020-01-06 20:42:41 +00:00
|
|
|
err = fmt.Errorf("invalid external_auth_scope: %v reset to 0", globalConf.ProviderConf.ExternalAuthScope)
|
|
|
|
globalConf.ProviderConf.ExternalAuthScope = 0
|
|
|
|
logger.Warn(logSender, "", "Configuration error: %v", err)
|
|
|
|
logger.WarnToConsole("Configuration error: %v", err)
|
|
|
|
}
|
2020-01-31 18:04:00 +00:00
|
|
|
if len(globalConf.ProviderConf.CredentialsPath) == 0 {
|
|
|
|
err = fmt.Errorf("invalid credentials path, reset to \"credentials\"")
|
|
|
|
globalConf.ProviderConf.CredentialsPath = "credentials"
|
|
|
|
logger.Warn(logSender, "", "Configuration error: %v", err)
|
|
|
|
logger.WarnToConsole("Configuration error: %v", err)
|
|
|
|
}
|
2020-04-01 21:25:23 +00:00
|
|
|
checkHooksCompatibility()
|
2020-05-16 21:26:44 +00:00
|
|
|
checkHostKeyCompatibility()
|
2020-03-03 08:09:58 +00:00
|
|
|
logger.Debug(logSender, "", "config file used: '%#v', config loaded: %+v", viper.ConfigFileUsed(), getRedactedGlobalConf())
|
2019-07-20 10:26:52 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-04-01 21:25:23 +00:00
|
|
|
|
|
|
|
func checkHooksCompatibility() {
|
2020-04-30 12:23:55 +00:00
|
|
|
// we copy deprecated fields to new ones to keep backward compatibility so lint is disabled
|
2020-05-06 17:36:34 +00:00
|
|
|
if len(globalConf.ProviderConf.ExternalAuthProgram) > 0 && len(globalConf.ProviderConf.ExternalAuthHook) == 0 { //nolint:staticcheck
|
2020-04-01 21:25:23 +00:00
|
|
|
logger.Warn(logSender, "", "external_auth_program is deprecated, please use external_auth_hook")
|
|
|
|
logger.WarnToConsole("external_auth_program is deprecated, please use external_auth_hook")
|
2020-05-06 17:36:34 +00:00
|
|
|
globalConf.ProviderConf.ExternalAuthHook = globalConf.ProviderConf.ExternalAuthProgram //nolint:staticcheck
|
2020-04-01 21:25:23 +00:00
|
|
|
}
|
2020-05-06 17:36:34 +00:00
|
|
|
if len(globalConf.ProviderConf.PreLoginProgram) > 0 && len(globalConf.ProviderConf.PreLoginHook) == 0 { //nolint:staticcheck
|
2020-04-01 21:25:23 +00:00
|
|
|
logger.Warn(logSender, "", "pre_login_program is deprecated, please use pre_login_hook")
|
|
|
|
logger.WarnToConsole("pre_login_program is deprecated, please use pre_login_hook")
|
2020-05-06 17:36:34 +00:00
|
|
|
globalConf.ProviderConf.PreLoginHook = globalConf.ProviderConf.PreLoginProgram //nolint:staticcheck
|
2020-04-01 21:25:23 +00:00
|
|
|
}
|
2020-05-06 17:36:34 +00:00
|
|
|
if len(globalConf.SFTPD.KeyboardInteractiveProgram) > 0 && len(globalConf.SFTPD.KeyboardInteractiveHook) == 0 { //nolint:staticcheck
|
2020-04-01 21:25:23 +00:00
|
|
|
logger.Warn(logSender, "", "keyboard_interactive_auth_program is deprecated, please use keyboard_interactive_auth_hook")
|
|
|
|
logger.WarnToConsole("keyboard_interactive_auth_program is deprecated, please use keyboard_interactive_auth_hook")
|
2020-05-06 17:36:34 +00:00
|
|
|
globalConf.SFTPD.KeyboardInteractiveHook = globalConf.SFTPD.KeyboardInteractiveProgram //nolint:staticcheck
|
2020-04-01 21:25:23 +00:00
|
|
|
}
|
2020-05-24 13:29:39 +00:00
|
|
|
if len(globalConf.SFTPD.Actions.Hook) == 0 {
|
|
|
|
if len(globalConf.SFTPD.Actions.HTTPNotificationURL) > 0 { //nolint:staticcheck
|
|
|
|
logger.Warn(logSender, "", "http_notification_url is deprecated, please use hook")
|
|
|
|
logger.WarnToConsole("http_notification_url is deprecated, please use hook")
|
|
|
|
globalConf.SFTPD.Actions.Hook = globalConf.SFTPD.Actions.HTTPNotificationURL //nolint:staticcheck
|
|
|
|
} else if len(globalConf.SFTPD.Actions.Command) > 0 { //nolint:staticcheck
|
|
|
|
logger.Warn(logSender, "", "command is deprecated, please use hook")
|
|
|
|
logger.WarnToConsole("command is deprecated, please use hook")
|
|
|
|
globalConf.SFTPD.Actions.Hook = globalConf.SFTPD.Actions.Command //nolint:staticcheck
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(globalConf.ProviderConf.Actions.Hook) == 0 {
|
|
|
|
if len(globalConf.ProviderConf.Actions.HTTPNotificationURL) > 0 { //nolint:staticcheck
|
|
|
|
logger.Warn(logSender, "", "http_notification_url is deprecated, please use hook")
|
|
|
|
logger.WarnToConsole("http_notification_url is deprecated, please use hook")
|
|
|
|
globalConf.ProviderConf.Actions.Hook = globalConf.ProviderConf.Actions.HTTPNotificationURL //nolint:staticcheck
|
|
|
|
} else if len(globalConf.ProviderConf.Actions.Command) > 0 { //nolint:staticcheck
|
|
|
|
logger.Warn(logSender, "", "command is deprecated, please use hook")
|
|
|
|
logger.WarnToConsole("command is deprecated, please use hook")
|
|
|
|
globalConf.ProviderConf.Actions.Hook = globalConf.ProviderConf.Actions.Command //nolint:staticcheck
|
|
|
|
}
|
|
|
|
}
|
2020-04-01 21:25:23 +00:00
|
|
|
}
|
2020-05-16 21:26:44 +00:00
|
|
|
|
|
|
|
func checkHostKeyCompatibility() {
|
|
|
|
// we copy deprecated fields to new ones to keep backward compatibility so lint is disabled
|
|
|
|
if len(globalConf.SFTPD.Keys) > 0 && len(globalConf.SFTPD.HostKeys) == 0 { //nolint:staticcheck
|
|
|
|
logger.Warn(logSender, "", "keys is deprecated, please use host_keys")
|
|
|
|
logger.WarnToConsole("keys is deprecated, please use host_keys")
|
|
|
|
for _, k := range globalConf.SFTPD.Keys { //nolint:staticcheck
|
|
|
|
globalConf.SFTPD.HostKeys = append(globalConf.SFTPD.HostKeys, k.PrivateKey)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|