1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
package server
import (
"errors"
"fmt"
iofs "io/fs"
"os"
goUser "os/user"
"path/filepath"
"github.com/mimecast/dtail/internal/config"
"github.com/mimecast/dtail/internal/io/dlog"
"github.com/mimecast/dtail/internal/io/fs"
user "github.com/mimecast/dtail/internal/user/server"
gossh "golang.org/x/crypto/ssh"
)
// NewPublicKeyCallback creates an instance-scoped SSH public key callback.
// It avoids relying on package-level mutable configuration/state.
func NewPublicKeyCallback(authKeyEnabled bool, cacheDir string,
keyStore *AuthKeyStore) func(gossh.ConnMetadata, gossh.PublicKey) (*gossh.Permissions, error) {
if keyStore == nil {
keyStore = authKeyStore
}
return func(c gossh.ConnMetadata, offeredPubKey gossh.PublicKey) (*gossh.Permissions, error) {
return publicKeyCallback(c, offeredPubKey, authKeyEnabled, cacheDir, keyStore)
}
}
func publicKeyCallback(c gossh.ConnMetadata, offeredPubKey gossh.PublicKey,
authKeyEnabled bool, cacheDir string, keyStore *AuthKeyStore) (*gossh.Permissions, error) {
user, err := user.New(c.User(), c.RemoteAddr().String(), nil)
if err != nil {
return nil, err
}
dlog.Server.Info(user, "Incoming authorization")
if authKeyEnabled {
if permissions := authKeyStorePermissions(keyStore, user.Name, offeredPubKey); permissions != nil {
dlog.Server.Info(user, "Authorized by in-memory auth key store")
return permissions, nil
}
}
authorizedKeysPath, err := authorizedKeysPathForUser(user, cacheDir)
if err != nil {
return nil, err
}
dlog.Server.Info(user, "Reading", authorizedKeysPath.Path())
authorizedKeysBytes, err := authorizedKeysPath.ReadFile()
if err != nil {
return nil, fmt.Errorf("Unable to read authorized keys file|%s|%s|%s",
authorizedKeysPath.Path(), user, err.Error())
}
return verifyAuthorizedKeys(user, authorizedKeysBytes, offeredPubKey)
}
func verifyAuthorizedKeys(user *user.User, authorizedKeysBytes []byte,
offeredPubKey gossh.PublicKey) (*gossh.Permissions, error) {
authorizedKeysMap := map[string]bool{}
for len(authorizedKeysBytes) > 0 {
authorizedPubKey, _, _, restBytes, err := gossh.ParseAuthorizedKey(authorizedKeysBytes)
if err != nil {
return nil, fmt.Errorf("unable to parse authorized keys bytes|%s|%s",
user, err.Error())
}
authorizedKeysMap[string(authorizedPubKey.Marshal())] = true
authorizedKeysBytes = restBytes
dlog.Server.Debug(user, "Authorized public key fingerprint",
gossh.FingerprintSHA256(authorizedPubKey))
}
dlog.Server.Debug(user, "Offered public key fingerprint", gossh.FingerprintSHA256(offeredPubKey))
if authorizedKeysMap[string(offeredPubKey.Marshal())] {
return permissionsFromPublicKey(offeredPubKey), nil
}
return nil, fmt.Errorf("%s|public key of user not authorized", user)
}
func authKeyStorePermissions(keyStore *AuthKeyStore, userName string,
offeredPubKey gossh.PublicKey) *gossh.Permissions {
if keyStore == nil || !keyStore.Has(userName, offeredPubKey) {
return nil
}
return permissionsFromPublicKey(offeredPubKey)
}
func permissionsFromPublicKey(offeredPubKey gossh.PublicKey) *gossh.Permissions {
return &gossh.Permissions{
Extensions: map[string]string{"pubkey-fp": gossh.FingerprintSHA256(offeredPubKey)},
}
}
type userLookupFunc func(string) (*goUser.User, error)
func authorizedKeysPathForUser(user *user.User, cacheDir string) (fs.RootedPath, error) {
if config.Env("DTAIL_INTEGRATION_TEST_RUN_MODE") {
// In this case, we expect a pub key in the current directory.
return fs.NewRootedPath("./id_rsa.pub")
}
cwd, err := os.Getwd()
if err != nil {
return fs.RootedPath{}, err
}
return findAuthorizedKeysPath(user, cacheDir, cwd, goUser.Lookup)
}
func findAuthorizedKeysPath(user *user.User, cacheDir, cwd string,
lookupUser userLookupFunc) (fs.RootedPath, error) {
// Check for cached version in the dserver directory.
if cacheDir != "" {
cachePath := filepath.Join(cwd, cacheDir, fmt.Sprintf("%s.authorized_keys", user.Name))
rootedCachePath, err := fs.NewRootedPath(cachePath)
if err != nil {
return fs.RootedPath{}, err
}
if _, err := rootedCachePath.Stat(); err == nil {
return rootedCachePath, nil
}
}
// As the last option, check the regular SSH path.
osUser, err := lookupUser(user.Name)
if err != nil {
return fs.RootedPath{}, err
}
authorizedKeysPath := filepath.Join(osUser.HomeDir, ".ssh", "authorized_keys")
rootedAuthorizedKeysPath, err := fs.NewRootedPath(authorizedKeysPath)
if err != nil {
return fs.RootedPath{}, err
}
if _, err = rootedAuthorizedKeysPath.Stat(); err == nil {
return rootedAuthorizedKeysPath, nil
}
if !errors.Is(err, iofs.ErrNotExist) {
return fs.RootedPath{}, err
}
return fs.RootedPath{}, fmt.Errorf("unable to find any authorized keys file")
}
|