sshpoke/internal/server/driver/ssh/sshtun/auth.go

92 lines
1.8 KiB
Go
Raw Normal View History

2023-11-18 21:23:29 +03:00
package sshtun
2023-11-18 17:51:04 +03:00
import (
"errors"
"fmt"
"os"
"path"
"strings"
2023-11-18 21:51:44 +03:00
"github.com/Neur0toxine/sshpoke/pkg/proto/ssh"
"github.com/Neur0toxine/sshpoke/pkg/smarttypes"
2023-11-18 17:51:04 +03:00
)
func AuthKeyFile(keyFile smarttypes.Path) (ssh.AuthMethod, error) {
2023-11-18 17:51:04 +03:00
key, err := readKey(keyFile)
if err != nil {
return nil, err
}
return ssh.PublicKeys(key), nil
}
func AuthKeyDir(keyDir smarttypes.Path) (ssh.AuthMethod, error) {
2023-11-18 17:51:04 +03:00
keys, err := readKeys(keyDir)
if err != nil {
return nil, err
}
return ssh.PublicKeys(keys...), nil
}
func AuthPassword(password string) ssh.AuthMethod {
return ssh.Password(password)
}
func readKeys(keyDir smarttypes.Path) ([]ssh.Signer, error) {
dir, err := keyDir.Directory()
2023-11-18 17:51:04 +03:00
if err != nil {
return nil, fmt.Errorf("cannot parse keys: %s", err)
}
entries, err := os.ReadDir(dir)
if err != nil {
return nil, fmt.Errorf("cannot read key directory: %s", err)
}
keys := []ssh.Signer{}
for _, entry := range entries {
if entry.IsDir() {
continue
}
info, err := entry.Info()
if err != nil {
continue
}
if strings.HasSuffix(entry.Name(), ".pub") {
continue
}
if entry.Name() == "config" {
continue
}
if entry.Name() == "known_hosts" {
continue
}
// this file is too small to be a private key
if info.Size() < 256 {
continue
}
key, err := readKey(smarttypes.Path(path.Join(dir, entry.Name())))
2023-11-18 17:51:04 +03:00
if err != nil {
continue
}
keys = append(keys, key)
}
if len(keys) == 0 {
return nil, errors.New("no keys in the provided directory")
}
return keys, nil
}
func readKey(keyFile smarttypes.Path) (ssh.Signer, error) {
fileName, err := keyFile.File()
2023-11-18 17:51:04 +03:00
if err != nil {
return nil, err
}
keyData, err := os.ReadFile(fileName)
if err != nil {
return nil, err
}
key, err := ssh.ParsePrivateKey(keyData)
if err != nil {
return nil, err
}
return key, nil
}