sftpd auto host keys: try to auto-create parent dir if missing

This commit is contained in:
Nicola Murino
2020-10-05 14:16:57 +02:00
parent 3e2afc35ba
commit f9827f958b
2 changed files with 23 additions and 0 deletions

View File

@@ -188,6 +188,9 @@ func DecryptData(data string) (string, error) {
// private key to specified file and the public key to the specified
// file adding the .pub suffix
func GenerateRSAKeys(file string) error {
if err := createDirPathIfMissing(file, 0700); err != nil {
return err
}
key, err := rsa.GenerateKey(rand.Reader, 4096)
if err != nil {
return err
@@ -219,6 +222,9 @@ func GenerateRSAKeys(file string) error {
// private key to specified file and the public key to the specified
// file adding the .pub suffix
func GenerateECDSAKeys(file string) error {
if err := createDirPathIfMissing(file, 0700); err != nil {
return err
}
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return err
@@ -312,3 +318,14 @@ func CleanDirInput(dirInput string) string {
}
return filepath.Clean(dirInput)
}
func createDirPathIfMissing(file string, perm os.FileMode) error {
dirPath := filepath.Dir(file)
if _, err := os.Stat(dirPath); os.IsNotExist(err) {
err = os.MkdirAll(dirPath, perm)
if err != nil {
return err
}
}
return nil
}