/
gss.go
171 lines (135 loc) · 4.3 KB
/
gss.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
/*
Package gss implements RFC 3645 GSS-TSIG functions. This permits sending
signed dynamic DNS update messages to Windows servers that have the zone
require "Secure only" updates.
Example client:
import (
"fmt"
"time"
"github.com/bodgit/tsig"
"github.com/bodgit/tsig/gss"
"github.com/miekg/dns"
)
func main() {
dnsClient := new(dns.Client)
dnsClient.Net = "tcp"
gssClient, err := gss.NewClient(dnsClient)
if err != nil {
panic(err)
}
defer gssClient.Close()
host := "ns.example.com:53"
// Negotiate a context with the chosen server using the
// current user. See also
// gssClient.NegotiateContextWithCredentials() and
// gssClient.NegotiateContextWithKeytab() for alternatives
keyname, _, err := gssClient.NegotiateContext(host)
if err != nil {
panic(err)
}
dnsClient.TsigProvider = gssClient
// Use the DNS client as normal
msg := new(dns.Msg)
msg.SetUpdate(dns.Fqdn("example.com"))
insert, err := dns.NewRR("test.example.com. 300 A 192.0.2.1")
if err != nil {
panic(err)
}
msg.Insert([]dns.RR{insert})
msg.SetTsig(keyname, tsig.GSS, 300, time.Now().Unix())
rr, _, err := dnsClient.Exchange(msg, host)
if err != nil {
panic(err)
}
if rr.Rcode != dns.RcodeSuccess {
fmt.Printf("DNS error: %s (%d)\n", dns.RcodeToString[rr.Rcode], rr.Rcode)
}
// Cleanup the context
err = gssClient.DeleteContext(keyname)
if err != nil {
panic(err)
}
}
Under the hood, GSSAPI is used on platforms other than Windows whilst Windows
uses native SSPI which has a similar API.
*/
package gss
import (
"errors"
"fmt"
"math/rand"
"time"
"github.com/bodgit/tsig"
"github.com/go-logr/logr"
multierror "github.com/hashicorp/go-multierror"
"github.com/miekg/dns"
)
var (
errNotSupported = errors.New("not supported")
)
// gssNoVerify is a dns.TsigProvider that skips any GSS-TSIG verification.
//
// BIND doesn't sign TKEY responses but Windows does, using the key you're
// currently negotiating so it creates a chicken & egg problem. According
// to the RFC, verification isn't needed as the TKEY response should be
// cryptographically secure anyway.
type gssNoVerify struct{}
func (*gssNoVerify) Generate(_ []byte, t *dns.TSIG) ([]byte, error) {
if dns.CanonicalName(t.Algorithm) != tsig.GSS {
return nil, dns.ErrKeyAlg
}
return nil, dns.ErrSecret
}
func (*gssNoVerify) Verify(_ []byte, t *dns.TSIG) error {
if dns.CanonicalName(t.Algorithm) != tsig.GSS {
return dns.ErrKeyAlg
}
return nil
}
func generateTKEYName(host string) string {
seed := rand.NewSource(time.Now().UnixNano())
rng := rand.New(seed)
return dns.Fqdn(fmt.Sprintf("%d.sig-%s", rng.Int31(), host))
}
func generateSPN(host string) string {
if dns.IsFqdn(host) {
return fmt.Sprintf("DNS/%s", host[:len(host)-1])
}
return fmt.Sprintf("DNS/%s", host)
}
func (c *Client) close() error {
c.m.RLock()
keys := make([]string, 0, len(c.ctx))
for k := range c.ctx {
keys = append(keys, k)
}
c.m.RUnlock()
var errs error
for _, k := range keys {
errs = multierror.Append(errs, c.DeleteContext(k))
}
return errs
}
func (c *Client) setOption(options ...func(*Client) error) error {
for _, option := range options {
if err := option(c); err != nil {
return err
}
}
return nil
}
// SetConfig sets the Kerberos configuration used by c
func (c *Client) SetConfig(config string) error {
return c.setOption(WithConfig(config))
}
// WithLogger sets the logger used
func WithLogger(logger logr.Logger) func(*Client) error {
return func(c *Client) error {
c.logger = logger
return nil
}
}
// SetLogger sets the logger used by c
func (c *Client) SetLogger(logger logr.Logger) error {
return c.setOption(WithLogger(logger))
}