sftpd: simplify initialization code

now gocyclo is happy again
This commit is contained in:
Nicola Murino 2019-08-02 11:17:23 +02:00
parent ba3f9d891a
commit 8c40684989

View file

@ -86,19 +86,9 @@ func (c Configuration) Initialize(configDir string) error {
ServerVersion: "SSH-2.0-" + c.Banner,
}
if len(c.Keys) == 0 {
autoFile := filepath.Join(configDir, defaultPrivateKeyName)
if _, err := os.Stat(autoFile); os.IsNotExist(err) {
logger.Info(logSender, "No host keys configured and %s does not exist; creating new private key for server", autoFile)
logger.InfoToConsole("No host keys configured and %s does not exist; creating new private key for server", autoFile)
if err := c.generatePrivateKey(autoFile); err != nil {
return err
}
} else if err != nil {
return err
}
c.Keys = append(c.Keys, Key{PrivateKey: defaultPrivateKeyName})
err = c.checkHostKeys(configDir)
if err != nil {
return err
}
for _, k := range c.Keys {
@ -275,6 +265,22 @@ func loginUser(user dataprovider.User) (*ssh.Permissions, error) {
return p, nil
}
// If no host keys are defined we try to use or generate the default one.
func (c *Configuration) checkHostKeys(configDir string) error {
var err error
if len(c.Keys) == 0 {
autoFile := filepath.Join(configDir, defaultPrivateKeyName)
if _, err = os.Stat(autoFile); os.IsNotExist(err) {
logger.Info(logSender, "No host keys configured and %s does not exist; creating new private key for server", autoFile)
logger.InfoToConsole("No host keys configured and %s does not exist; creating new private key for server", autoFile)
err = c.generatePrivateKey(autoFile)
}
c.Keys = append(c.Keys, Key{PrivateKey: defaultPrivateKeyName})
}
return err
}
func (c Configuration) validatePublicKeyCredentials(conn ssh.ConnMetadata, pubKey string) (*ssh.Permissions, error) {
var err error
var user dataprovider.User