-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsspi.go
238 lines (190 loc) · 5.12 KB
/
sspi.go
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
//go:build windows
// +build windows
package sshkrb5
import (
"strings"
"github.com/alexbrainman/sspi"
"github.com/alexbrainman/sspi/kerberos"
"github.com/go-logr/logr"
multierror "github.com/hashicorp/go-multierror"
)
// WithConfig sets the configuration in the Client.
func WithConfig[T Client](_ string) Option[T] {
return unsupportedOption[T]
}
// WithDomain sets the Kerberos domain in the Client.
func WithDomain[T Client](domain string) Option[T] {
return func(a *T) error {
if x, ok := any(a).(*Client); ok {
x.domain = domain
}
return nil
}
}
// WithUsername sets the username in the Client.
func WithUsername[T Client](username string) Option[T] {
return func(a *T) error {
if x, ok := any(a).(*Client); ok {
x.username = username
}
return nil
}
}
// WithPassword sets the password in the Client.
func WithPassword[T Client](password string) Option[T] {
return func(a *T) error {
if x, ok := any(a).(*Client); ok {
x.password = password
}
return nil
}
}
// WithKeytab sets the keytab path in either a Client or Server.
func WithKeytab[T Client | Server](_ string) Option[T] {
return unsupportedOption[T]
}
// Client implements the ssh.GSSAPIClient interface.
type Client struct {
domain string
username string
password string
creds *sspi.Credentials
ctx *kerberos.ClientContext
logger logr.Logger
}
func (c *Client) usePassword() bool {
return c.domain != "" && c.username != "" && c.password != ""
}
// NewClient returns a new Client using the current user.
func NewClient(options ...Option[Client]) (*Client, error) {
c := &Client{
logger: logr.Discard(),
}
var err error
for _, option := range options {
if err = option(c); err != nil {
return nil, err
}
}
if c.usePassword() {
c.creds, err = kerberos.AcquireUserCredentials(c.domain, c.username, c.password)
} else {
c.creds, err = kerberos.AcquireCurrentUserCredentials()
}
if err != nil {
return nil, err
}
return c, nil
}
// Close deletes any active security context and unloads any underlying
// libraries as necessary.
func (c *Client) Close() error {
return multierror.Append(c.DeleteSecContext(), c.creds.Release()).ErrorOrNil()
}
// InitSecContext is called by the ssh.Client to initialise or advance the
// security context.
func (c *Client) InitSecContext(target string, token []byte, isGSSDelegCreds bool) ([]byte, bool, error) {
var (
completed bool
output []byte
err error
)
if len(token) == 0 {
sspiFlags := uint32(sspi.ISC_REQ_MUTUAL_AUTH | sspi.ISC_REQ_CONNECTION | sspi.ISC_REQ_INTEGRITY)
if isGSSDelegCreds {
sspiFlags |= sspi.ISC_REQ_DELEGATE
}
//nolint:lll
c.ctx, completed, output, err = kerberos.NewClientContextWithFlags(c.creds, strings.ReplaceAll(target, "@", "/"), sspiFlags)
if err != nil {
return nil, false, err
}
} else {
completed, output, err = c.ctx.Update(token)
}
if err != nil {
return nil, false, err
}
return output, !completed, nil
}
// GetMIC is called by the ssh.Client to authenticate the user using the
// negotiated security context.
func (c *Client) GetMIC(micField []byte) ([]byte, error) {
return c.ctx.MakeSignature(micField, 0, 0)
}
// DeleteSecContext is called by the ssh.Client to tear down any active
// security context.
func (c *Client) DeleteSecContext() (err error) {
if c.ctx != nil {
err = c.ctx.Release()
c.ctx = nil
}
return
}
// Server implements the ssh.GSSAPIServer interface.
type Server struct {
creds *sspi.Credentials
ctx *kerberos.ServerContext
logger logr.Logger
}
// NewServer returns a new Server.
func NewServer(options ...Option[Server]) (*Server, error) {
s := &Server{
logger: logr.Discard(),
}
for _, option := range options {
if err := option(s); err != nil {
return nil, err
}
}
creds, err := kerberos.AcquireServerCredentials("")
if err != nil {
return nil, err
}
s.creds = creds
return s, nil
}
// Close deletes any active security context and unloads any underlying
// libraries as necessary.
func (s *Server) Close() error {
return multierror.Append(s.DeleteSecContext(), s.creds.Release()).ErrorOrNil()
}
// AcceptSecContext is called by the ssh.ServerConn to accept and advance the
// security context.
func (s *Server) AcceptSecContext(token []byte) ([]byte, string, bool, error) {
var (
completed bool
output []byte
err error
)
if s.ctx == nil {
s.ctx, completed, output, err = kerberos.NewServerContext(s.creds, token)
} else {
completed, output, err = s.ctx.Update(token)
}
if err != nil {
return nil, "", false, err
}
var username string
if completed {
if username, err = s.ctx.GetUsername(); err != nil {
return nil, "", false, err
}
}
return output, username, !completed, nil
}
// VerifyMIC is called by the ssh.ServerConn to authenticate the user using
// the negotiated security context.
func (s *Server) VerifyMIC(micField, micToken []byte) error {
_, err := s.ctx.VerifySignature(micField, micToken, 0)
return err
}
// DeleteSecContext is called by the ssh.ServerConn to tear down any active
// security context.
func (s *Server) DeleteSecContext() (err error) {
if s.ctx != nil {
err = s.ctx.Release()
s.ctx = nil
}
return
}