2024-01-01 10:31:45 +00:00
|
|
|
// Copyright (C) 2019 Nicola Murino
|
2022-07-17 18:16:00 +00:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published
|
|
|
|
// by the Free Software Foundation, version 3.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
2023-01-03 09:18:30 +00:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2022-07-17 18:16:00 +00:00
|
|
|
|
2020-10-29 18:23:33 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"os/user"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/rs/xid"
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
2022-07-24 14:18:54 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/common"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/config"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/dataprovider"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/logger"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/plugin"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/sftpd"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/version"
|
2020-10-29 18:23:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
logJournalD = false
|
|
|
|
preserveHomeDir = false
|
2020-11-05 11:12:11 +00:00
|
|
|
baseHomeDir = ""
|
2020-10-29 18:23:33 +00:00
|
|
|
subsystemCmd = &cobra.Command{
|
|
|
|
Use: "startsubsys",
|
2021-10-03 06:14:57 +00:00
|
|
|
Short: "Use sftpgo as SFTP file transfer subsystem",
|
2020-10-29 18:23:33 +00:00
|
|
|
Long: `In this mode SFTPGo speaks the server side of SFTP protocol to stdout and
|
|
|
|
expects client requests from stdin.
|
|
|
|
This mode is not intended to be called directly, but from sshd using the
|
|
|
|
Subsystem option.
|
|
|
|
For example adding a line like this one in "/etc/ssh/sshd_config":
|
|
|
|
|
|
|
|
Subsystem sftp sftpgo startsubsys
|
|
|
|
|
|
|
|
Command-line flags should be specified in the Subsystem declaration.
|
|
|
|
`,
|
2022-07-19 21:28:33 +00:00
|
|
|
Run: func(_ *cobra.Command, _ []string) {
|
2020-10-29 18:23:33 +00:00
|
|
|
logSender := "startsubsys"
|
|
|
|
connectionID := xid.New().String()
|
2022-07-13 08:40:24 +00:00
|
|
|
var zeroLogLevel zerolog.Level
|
|
|
|
switch logLevel {
|
|
|
|
case "info":
|
|
|
|
zeroLogLevel = zerolog.InfoLevel
|
|
|
|
case "warn":
|
|
|
|
zeroLogLevel = zerolog.WarnLevel
|
|
|
|
case "error":
|
|
|
|
zeroLogLevel = zerolog.ErrorLevel
|
|
|
|
default:
|
|
|
|
zeroLogLevel = zerolog.DebugLevel
|
2020-10-29 18:23:33 +00:00
|
|
|
}
|
2021-11-06 14:18:16 +00:00
|
|
|
logger.SetLogTime(logUTCTime)
|
2020-10-29 18:23:33 +00:00
|
|
|
if logJournalD {
|
2022-07-13 08:40:24 +00:00
|
|
|
logger.InitJournalDLogger(zeroLogLevel)
|
2020-10-29 18:23:33 +00:00
|
|
|
} else {
|
2022-07-13 08:40:24 +00:00
|
|
|
logger.InitStdErrLogger(zeroLogLevel)
|
2020-10-29 18:23:33 +00:00
|
|
|
}
|
|
|
|
osUser, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(logSender, connectionID, "unable to get the current user: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
username := osUser.Username
|
|
|
|
homedir := osUser.HomeDir
|
2023-02-27 18:02:43 +00:00
|
|
|
logger.Info(logSender, connectionID, "starting SFTPGo %v as subsystem, user %q home dir %q config dir %q base home dir %q",
|
2020-11-05 11:12:11 +00:00
|
|
|
version.Get(), username, homedir, configDir, baseHomeDir)
|
2020-10-29 18:23:33 +00:00
|
|
|
err = config.LoadConfig(configDir, configFile)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(logSender, connectionID, "unable to load configuration: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2020-12-06 09:36:48 +00:00
|
|
|
kmsConfig := config.GetKMSConfig()
|
|
|
|
if err := kmsConfig.Initialize(); err != nil {
|
|
|
|
logger.Error(logSender, connectionID, "unable to initialize KMS: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2024-10-02 16:14:05 +00:00
|
|
|
if err := plugin.Initialize(config.GetPluginsConfig(), logLevel); err != nil {
|
|
|
|
logger.Error(logSender, connectionID, "unable to initialize plugin system: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2021-09-04 10:11:04 +00:00
|
|
|
mfaConfig := config.GetMFAConfig()
|
|
|
|
err = mfaConfig.Initialize()
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(logSender, "", "unable to initialize MFA: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2023-02-09 08:33:33 +00:00
|
|
|
dataProviderConf := config.GetProviderConf()
|
2020-10-29 18:23:33 +00:00
|
|
|
if dataProviderConf.Driver == dataprovider.SQLiteDataProviderName || dataProviderConf.Driver == dataprovider.BoltDataProviderName {
|
2023-02-09 08:33:33 +00:00
|
|
|
logger.Debug(logSender, connectionID, "data provider %q not supported in subsystem mode, using %q provider",
|
2020-10-29 18:23:33 +00:00
|
|
|
dataProviderConf.Driver, dataprovider.MemoryDataProviderName)
|
|
|
|
dataProviderConf.Driver = dataprovider.MemoryDataProviderName
|
|
|
|
dataProviderConf.Name = ""
|
|
|
|
}
|
|
|
|
config.SetProviderConf(dataProviderConf)
|
2021-01-17 21:29:08 +00:00
|
|
|
err = dataprovider.Initialize(dataProviderConf, configDir, false)
|
2020-10-29 18:23:33 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Error(logSender, connectionID, "unable to initialize the data provider: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2023-02-19 18:03:45 +00:00
|
|
|
smtpConfig := config.GetSMTPConfig()
|
2023-03-01 17:31:02 +00:00
|
|
|
err = smtpConfig.Initialize(configDir, false)
|
2023-02-19 18:03:45 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Error(logSender, connectionID, "unable to initialize SMTP configuration: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2023-02-09 08:33:33 +00:00
|
|
|
commonConfig := config.GetCommonConfig()
|
|
|
|
// idle connection are managed externally
|
|
|
|
commonConfig.IdleTimeout = 0
|
|
|
|
config.SetCommonConfig(commonConfig)
|
|
|
|
if err := common.Initialize(config.GetCommonConfig(), dataProviderConf.GetShared()); err != nil {
|
|
|
|
logger.Error(logSender, connectionID, "%v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2020-10-29 18:23:33 +00:00
|
|
|
httpConfig := config.GetHTTPConfig()
|
2021-02-13 13:41:37 +00:00
|
|
|
if err := httpConfig.Initialize(configDir); err != nil {
|
|
|
|
logger.Error(logSender, connectionID, "unable to initialize http client: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2022-05-20 17:30:54 +00:00
|
|
|
commandConfig := config.GetCommandConfig()
|
|
|
|
if err := commandConfig.Initialize(); err != nil {
|
|
|
|
logger.Error(logSender, connectionID, "unable to initialize commands configuration: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2022-11-16 18:04:50 +00:00
|
|
|
user, err := dataprovider.UserExists(username, "")
|
2020-10-29 18:23:33 +00:00
|
|
|
if err == nil {
|
|
|
|
if user.HomeDir != filepath.Clean(homedir) && !preserveHomeDir {
|
|
|
|
// update the user
|
|
|
|
user.HomeDir = filepath.Clean(homedir)
|
2022-12-03 10:45:27 +00:00
|
|
|
err = dataprovider.UpdateUser(&user, dataprovider.ActionExecutorSystem, "", "")
|
2020-10-29 18:23:33 +00:00
|
|
|
if err != nil {
|
2023-02-27 18:02:43 +00:00
|
|
|
logger.Error(logSender, connectionID, "unable to update user %q: %v", username, err)
|
2020-10-29 18:23:33 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
user.Username = username
|
2020-11-05 11:12:11 +00:00
|
|
|
if baseHomeDir != "" && filepath.IsAbs(baseHomeDir) {
|
|
|
|
user.HomeDir = filepath.Join(baseHomeDir, username)
|
|
|
|
} else {
|
|
|
|
user.HomeDir = filepath.Clean(homedir)
|
|
|
|
}
|
2023-02-27 18:02:43 +00:00
|
|
|
logger.Debug(logSender, connectionID, "home dir for new user %q", user.HomeDir)
|
2020-10-29 18:23:33 +00:00
|
|
|
user.Password = connectionID
|
|
|
|
user.Permissions = make(map[string][]string)
|
|
|
|
user.Permissions["/"] = []string{dataprovider.PermAny}
|
2022-12-03 10:45:27 +00:00
|
|
|
err = dataprovider.AddUser(&user, dataprovider.ActionExecutorSystem, "", "")
|
2020-10-29 18:23:33 +00:00
|
|
|
if err != nil {
|
2023-02-27 18:02:43 +00:00
|
|
|
logger.Error(logSender, connectionID, "unable to add user %q: %v", username, err)
|
2020-10-29 18:23:33 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
2022-04-25 13:49:11 +00:00
|
|
|
err = user.LoadAndApplyGroupSettings()
|
|
|
|
if err != nil {
|
2023-02-27 18:02:43 +00:00
|
|
|
logger.Error(logSender, connectionID, "unable to apply group settings for user %q: %v", username, err)
|
2022-04-25 13:49:11 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2021-02-16 18:11:36 +00:00
|
|
|
err = sftpd.ServeSubSystemConnection(&user, connectionID, os.Stdin, os.Stdout)
|
2020-10-29 18:23:33 +00:00
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
logger.Warn(logSender, connectionID, "serving subsystem finished with error: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
logger.Info(logSender, connectionID, "serving subsystem finished")
|
2021-07-31 17:09:09 +00:00
|
|
|
plugin.Handler.Cleanup()
|
2020-10-29 18:23:33 +00:00
|
|
|
os.Exit(0)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
subsystemCmd.Flags().BoolVarP(&preserveHomeDir, "preserve-home", "p", false, `If the user already exists, the existing home
|
|
|
|
directory will not be changed`)
|
2020-11-05 11:12:11 +00:00
|
|
|
subsystemCmd.Flags().StringVarP(&baseHomeDir, "base-home-dir", "d", "", `If the user does not exist specify an alternate
|
|
|
|
starting directory. The home directory for a new
|
|
|
|
user will be:
|
|
|
|
|
2020-11-07 17:05:47 +00:00
|
|
|
[base-home-dir]/[username]
|
2020-11-05 11:12:11 +00:00
|
|
|
|
|
|
|
base-home-dir must be an absolute path.`)
|
2020-10-29 18:23:33 +00:00
|
|
|
subsystemCmd.Flags().BoolVarP(&logJournalD, "log-to-journald", "j", false, `Send logs to journald. Only available on Linux.
|
|
|
|
Use:
|
|
|
|
|
|
|
|
$ journalctl -o verbose -f
|
|
|
|
|
|
|
|
To see full logs.
|
|
|
|
If not set, the logs will be sent to the standard
|
|
|
|
error`)
|
|
|
|
|
2020-12-03 15:23:33 +00:00
|
|
|
addConfigFlags(subsystemCmd)
|
2020-10-29 18:23:33 +00:00
|
|
|
|
2022-07-13 08:40:24 +00:00
|
|
|
viper.SetDefault(logLevelKey, defaultLogLevel)
|
|
|
|
viper.BindEnv(logLevelKey, "SFTPGO_LOG_LEVEL") //nolint:errcheck
|
|
|
|
subsystemCmd.Flags().StringVar(&logLevel, logLevelFlag, viper.GetString(logLevelKey),
|
|
|
|
`Set the log level. Supported values:
|
|
|
|
|
|
|
|
debug, info, warn, error.
|
|
|
|
|
|
|
|
This flag can be set
|
|
|
|
using SFTPGO_LOG_LEVEL env var too.
|
2020-10-29 18:23:33 +00:00
|
|
|
`)
|
2022-07-13 08:40:24 +00:00
|
|
|
viper.BindPFlag(logLevelKey, subsystemCmd.Flags().Lookup(logLevelFlag)) //nolint:errcheck
|
2020-10-29 18:23:33 +00:00
|
|
|
|
2021-11-06 14:18:16 +00:00
|
|
|
viper.SetDefault(logUTCTimeKey, defaultLogUTCTime)
|
|
|
|
viper.BindEnv(logUTCTimeKey, "SFTPGO_LOG_UTC_TIME") //nolint:errcheck
|
|
|
|
subsystemCmd.Flags().BoolVar(&logUTCTime, logUTCTimeFlag, viper.GetBool(logUTCTimeKey),
|
|
|
|
`Use UTC time for logging. This flag can be set
|
|
|
|
using SFTPGO_LOG_UTC_TIME env var too.
|
|
|
|
`)
|
|
|
|
viper.BindPFlag(logUTCTimeKey, subsystemCmd.Flags().Lookup(logUTCTimeFlag)) //nolint:errcheck
|
|
|
|
|
2020-10-29 18:23:33 +00:00
|
|
|
rootCmd.AddCommand(subsystemCmd)
|
|
|
|
}
|