-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgokrb5.go
238 lines (192 loc) · 5.53 KB
/
gokrb5.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 && !apcera
// +build !windows,!apcera
package sshkrb5
import (
wrapper "github.com/bodgit/gssapi"
"github.com/go-logr/logr"
multierror "github.com/hashicorp/go-multierror"
"github.com/jcmturner/gokrb5/v8/gssapi"
"github.com/jcmturner/gokrb5/v8/iana/nametype"
"github.com/jcmturner/gokrb5/v8/types"
)
// WithConfig sets the configuration in the Client.
func WithConfig[T Client](config string) Option[T] {
return func(a *T) error {
if x, ok := any(a).(*Client); ok {
x.config = config
}
return nil
}
}
// 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
x.keytab = nil
}
return nil
}
}
// WithKeytab sets the keytab path in either a Client or Server.
func WithKeytab[T Client | Server](keytab string) Option[T] {
return func(a *T) error {
switch x := any(a).(type) {
case *Client:
x.keytab = &keytab
x.password = ""
case *Server:
x.keytab = keytab
}
return nil
}
}
// Client implements the ssh.GSSAPIClient interface.
type Client struct {
config string
domain string
username string
password string
keytab *string
initiator *wrapper.Initiator
logger logr.Logger
}
func (c *Client) usePassword() bool {
return c.domain != "" && c.username != "" && c.password != ""
}
func (c *Client) useKeytab() bool {
return c.domain != "" && c.username != "" && c.keytab != nil
}
// 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
}
}
initiatorOptions := []wrapper.Option[wrapper.Initiator]{
wrapper.WithConfig(c.config),
wrapper.WithLogger[wrapper.Initiator](c.logger),
}
switch {
case c.usePassword():
initiatorOptions = append(initiatorOptions,
wrapper.WithDomain(c.domain),
wrapper.WithUsername(c.username),
wrapper.WithPassword(c.password))
case c.useKeytab():
initiatorOptions = append(initiatorOptions,
wrapper.WithDomain(c.domain),
wrapper.WithUsername(c.username),
wrapper.WithKeytab[wrapper.Initiator](*c.keytab))
default:
c.logger.Info("using default session")
}
if c.initiator, err = wrapper.NewInitiator(initiatorOptions...); 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.initiator.Close()).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) {
flags := gssapi.ContextFlagMutual | gssapi.ContextFlagInteg
if isGSSDelegCreds {
flags |= gssapi.ContextFlagDeleg
}
return c.initiator.Initiate(target, flags, token)
}
// 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.initiator.MakeSignature(micField)
}
// DeleteSecContext is called by the ssh.Client to tear down any active
// security context.
func (c *Client) DeleteSecContext() error {
return nil
}
// Server implements the ssh.GSSAPIServer interface.
type Server struct {
strict bool
keytab string
acceptor *wrapper.Acceptor
logger logr.Logger
}
// NewServer returns a new Server.
func NewServer(options ...Option[Server]) (*Server, error) {
s := &Server{
strict: true,
logger: logr.Discard(),
}
for _, option := range options {
if err := option(s); err != nil {
return nil, err
}
}
acceptorOptions := []wrapper.Option[wrapper.Acceptor]{
wrapper.WithLogger[wrapper.Acceptor](s.logger),
wrapper.WithKeytab[wrapper.Acceptor](s.keytab),
}
if s.strict {
hostname, err := osHostname()
if err != nil {
return nil, err
}
principal := types.NewPrincipalName(nametype.KRB_NT_SRV_HST, "host/"+hostname)
acceptorOptions = append(acceptorOptions, wrapper.WithServicePrincipal(&principal))
}
var err error
if s.acceptor, err = wrapper.NewAcceptor(acceptorOptions...); err != nil {
return nil, err
}
return s, nil
}
// Close deletes any active security context and unloads any underlying
// libraries as necessary.
func (s *Server) Close() error {
return s.DeleteSecContext()
}
// AcceptSecContext is called by the ssh.ServerConn to accept and advance the
// security context.
func (s *Server) AcceptSecContext(token []byte) ([]byte, string, bool, error) {
output, cont, err := s.acceptor.Accept(token)
return output, s.acceptor.PeerName(), cont, err
}
// VerifyMIC is called by the ssh.ServerConn to authenticate the user using
// the negotiated security context.
func (s *Server) VerifyMIC(micField, micToken []byte) error {
return s.acceptor.VerifySignature(micField, micToken)
}
// DeleteSecContext is called by the ssh.ServerConn to tear down any active
// security context.
func (s *Server) DeleteSecContext() error {
return nil
}