-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathkrb_unix.go
196 lines (156 loc) · 4.22 KB
/
krb_unix.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
//go:build !windows
// +build !windows
package gopgkrb5
import (
"fmt"
"github.com/jcmturner/gokrb5/v8/client"
"github.com/jcmturner/gokrb5/v8/config"
"github.com/jcmturner/gokrb5/v8/credentials"
"github.com/jcmturner/gokrb5/v8/keytab"
"github.com/jcmturner/gokrb5/v8/spnego"
"os"
"os/user"
"strings"
)
/*
* UNIX Kerberos support, using jcmturner's pure-go
* implementation
*
* Keytab support is available only on unix systems
*/
// GSS implements the pq.GSS interface and the pgconn.GSS interface.
type GSS struct {
cli *client.Client
ktPath string
realm string
username string
password string
settings []func(settings *client.Settings)
}
// NewGSS creates a new GSS provider.
func NewGSS(settings ...func(*client.Settings)) (*GSS, error) {
g := &GSS{}
g.settings = settings
err := g.init()
if err != nil {
return nil, err
}
return g, nil
}
func NewGSSWithKeytab(username string, realm string, ktPath string, settings ...func(settings *client.Settings)) (*GSS, error) {
g := &GSS{}
g.ktPath = ktPath
g.username = username
g.realm = realm
g.settings = settings
err := g.init()
if err != nil {
return nil, err
}
return g, nil
}
func NewGSSWithPassword(username string, realm string, password string, settings ...func(settings *client.Settings)) (*GSS, error) {
g := &GSS{}
g.password = password
g.username = username
g.realm = realm
g.settings = settings
err := g.init()
if err != nil {
return nil, err
}
return g, nil
}
func (g *GSS) init() error {
cfgPath, ok := os.LookupEnv("KRB5_CONFIG")
if !ok {
cfgPath = "/etc/krb5.conf"
}
cfg, err := config.Load(cfgPath)
if err != nil {
return err
}
u, err := user.Current()
if err != nil {
return err
}
var cl *client.Client
// By default, we disable encryption match check
// Settings can be overwritten when creating GSS provider instance
settings := []func(settings *client.Settings){client.DisablePAFXFAST(true)}
if len(g.settings) != 0 {
settings = g.settings
}
// If we have keytab path set, we create client from keytab
// Or if we have password set, we log in by password
// Otherwise, we use ccache file
if g.ktPath != "" {
kt, err := keytab.Load(g.ktPath)
if err != nil {
return err
}
cl = client.NewWithKeytab(g.username, g.realm, kt, cfg, settings...)
} else if g.password != "" {
cl = client.NewWithPassword(g.username, g.realm, g.password, cfg, settings...)
} else {
ccpath := "/tmp/krb5cc_" + u.Uid
ccname := os.Getenv("KRB5CCNAME")
if strings.HasPrefix(ccname, "FILE:") {
ccpath = strings.SplitN(ccname, ":", 2)[1]
}
ccache, err := credentials.LoadCCache(ccpath)
if err != nil {
return err
}
cl, err = client.NewFromCCache(ccache, cfg, settings...)
if err != nil {
return err
}
}
cl.Login()
g.cli = cl
return nil
}
// GetInitToken implements the GSS interface.
func (g *GSS) GetInitToken(host string, service string) ([]byte, error) {
// Resolve the hostname down to an 'A' record, if required (usually, it is)
if g.cli.Config.LibDefaults.DNSCanonicalizeHostname {
var err error
host, err = canonicalizeHostname(host)
if err != nil {
return nil, err
}
}
spn := service + "/" + host
return g.GetInitTokenFromSPN(spn)
}
// GetInitTokenFromSPN implements the GSS interface.
func (g *GSS) GetInitTokenFromSPN(spn string) ([]byte, error) {
s := spnego.SPNEGOClient(g.cli, spn)
st, err := s.InitSecContext()
if err != nil {
return nil, fmt.Errorf("kerberos error (InitSecContext): %s", err.Error())
}
b, err := st.Marshal()
if err != nil {
return nil, fmt.Errorf("kerberos error (Marshaling token): %s", err.Error())
}
return b, nil
}
// GetInitTokenFromSpn implements the GSS interface.
func (g *GSS) GetInitTokenFromSpn(spn string) ([]byte, error) {
return g.GetInitTokenFromSPN(spn)
}
// Continue implements the GSS interface.
func (g *GSS) Continue(inToken []byte) (done bool, outToken []byte, err error) {
t := &spnego.SPNEGOToken{}
err = t.Unmarshal(inToken)
if err != nil {
return true, nil, fmt.Errorf("kerberos error (Unmarshaling token): %s", err.Error())
}
state := t.NegTokenResp.State()
if state != spnego.NegStateAcceptCompleted {
return true, nil, fmt.Errorf("kerberos: expected state 'Completed' - got %d", state)
}
return true, nil, nil
}