Debian Codegraph @

client/client.go

raw symbols SCIP
1 // Package client provides a client library and methods for Kerberos 5 authentication.
2 package client
3
4 import (
5 "errors"
6 "fmt"
7
8 "gopkg.in/jcmturner/gokrb5.v5/config"
9 "gopkg.in/jcmturner/gokrb5.v5/credentials"
10 "gopkg.in/jcmturner/gokrb5.v5/crypto"
11 "gopkg.in/jcmturner/gokrb5.v5/crypto/etype"
12 "gopkg.in/jcmturner/gokrb5.v5/iana/errorcode"
13 "gopkg.in/jcmturner/gokrb5.v5/iana/nametype"
14 "gopkg.in/jcmturner/gokrb5.v5/keytab"
15 "gopkg.in/jcmturner/gokrb5.v5/krberror"
16 "gopkg.in/jcmturner/gokrb5.v5/messages"
17 "gopkg.in/jcmturner/gokrb5.v5/types"
18 )
19
20 // Client side configuration and state.
21 type Client struct {
22 Credentials *credentials.Credentials
23 Config *config.Config
24 GoKrb5Conf *Config
25 sessions *sessions
26 Cache *Cache
27 }
28
29 // Config struct holds GoKRB5 specific client configurations.
30 // Set Disable_PA_FX_FAST to true to force this behaviour off.
31 // Set Assume_PA_ENC_TIMESTAMP_Required to send the PA_ENC_TIMESTAMP pro-actively rather than waiting for a KRB_ERROR response from the KDC indicating it is required.
32 type Config struct {
33 DisablePAFXFast bool
34 AssumePAEncTimestampRequired bool
35 }
36
37 // NewClientWithPassword creates a new client from a password credential.
38 // Set the realm to empty string to use the default realm from config.
39 func NewClientWithPassword(username, realm, password string) Client {
40 creds := credentials.NewCredentials(username, realm)
41 return Client{
42 Credentials: creds.WithPassword(password),
43 Config: config.NewConfig(),
44 GoKrb5Conf: &Config{},
45 sessions: &sessions{
46 Entries: make(map[string]*session),
47 },
48 Cache: NewCache(),
49 }
50 }
51
52 // NewClientWithKeytab creates a new client from a keytab credential.
53 func NewClientWithKeytab(username, realm string, kt keytab.Keytab) Client {
54 creds := credentials.NewCredentials(username, realm)
55 return Client{
56 Credentials: creds.WithKeytab(kt),
57 Config: config.NewConfig(),
58 GoKrb5Conf: &Config{},
59 sessions: &sessions{
60 Entries: make(map[string]*session),
61 },
62 Cache: NewCache(),
63 }
64 }
65
66 // NewClientFromCCache create a client from a populated client cache.
67 //
68 // WARNING: A client created from CCache does not automatically renew TGTs and a failure will occur after the TGT expires.
69 func NewClientFromCCache(c credentials.CCache) (Client, error) {
70 cl := Client{
71 Credentials: c.GetClientCredentials(),
72 Config: config.NewConfig(),
73 GoKrb5Conf: &Config{},
74 sessions: &sessions{
75 Entries: make(map[string]*session),
76 },
77 Cache: NewCache(),
78 }
79 spn := types.PrincipalName{
80 NameType: nametype.KRB_NT_SRV_INST,
81 NameString: []string{"krbtgt", c.DefaultPrincipal.Realm},
82 }
83 cred, ok := c.GetEntry(spn)
84 if !ok {
85 return cl, errors.New("TGT not found in CCache")
86 }
87 var tgt messages.Ticket
88 err := tgt.Unmarshal(cred.Ticket)
89 if err != nil {
90 return cl, fmt.Errorf("TGT bytes in cache are not valid: %v", err)
91 }
92 cl.sessions.Entries[c.DefaultPrincipal.Realm] = &session{
93 Realm: c.DefaultPrincipal.Realm,
94 AuthTime: cred.AuthTime,
95 EndTime: cred.EndTime,
96 RenewTill: cred.RenewTill,
97 TGT: tgt,
98 SessionKey: cred.Key,
99 }
100 for _, cred := range c.GetEntries() {
101 var tkt messages.Ticket
102 err = tkt.Unmarshal(cred.Ticket)
103 if err != nil {
104 return cl, fmt.Errorf("cache entry ticket bytes are not valid: %v", err)
105 }
106 cl.Cache.addEntry(
107 tkt,
108 cred.AuthTime,
109 cred.StartTime,
110 cred.EndTime,
111 cred.RenewTill,
112 cred.Key,
113 )
114 }
115 return cl, nil
116 }
117
118 // WithConfig sets the Kerberos configuration for the client.
119 func (cl *Client) WithConfig(cfg *config.Config) *Client {
120 cl.Config = cfg
121 return cl
122 }
123
124 // WithKeytab adds a keytab to the client
125 func (cl *Client) WithKeytab(kt keytab.Keytab) *Client {
126 cl.Credentials.WithKeytab(kt)
127 return cl
128 }
129
130 // WithPassword adds a password to the client
131 func (cl *Client) WithPassword(password string) *Client {
132 cl.Credentials.WithPassword(password)
133 return cl
134 }
135
136 // Key returns a key for the client. Preferably from a keytab and then generated from the password.
137 // The KRBError would have been returned from the KDC and must be of type KDC_ERR_PREAUTH_REQUIRED.
138 // If a KRBError is not available pass messages.KRBError{} and a key will be returned from the credentials keytab.
139 func (cl *Client) Key(etype etype.EType, krberr messages.KRBError) (types.EncryptionKey, error) {
140 if cl.Credentials.HasKeytab() && etype != nil {
141 return cl.Credentials.Keytab.GetEncryptionKey(cl.Credentials.CName.NameString, cl.Credentials.Realm, 0, etype.GetETypeID())
142 } else if cl.Credentials.HasPassword() {
143 if krberr.ErrorCode == errorcode.KDC_ERR_PREAUTH_REQUIRED {
144 var pas types.PADataSequence
145 err := pas.Unmarshal(krberr.EData)
146 if err != nil {
147 return types.EncryptionKey{}, fmt.Errorf("could not get PAData from KRBError to generate key from password: %v", err)
148 }
149 key, _, err := crypto.GetKeyFromPassword(cl.Credentials.Password, krberr.CName, krberr.CRealm, etype.GetETypeID(), pas)
150 return key, err
151 }
152 key, _, err := crypto.GetKeyFromPassword(cl.Credentials.Password, cl.Credentials.CName, cl.Credentials.Realm, etype.GetETypeID(), types.PADataSequence{})
153 return key, err
154 }
155 return types.EncryptionKey{}, errors.New("credential has neither keytab or password to generate key")
156 }
157
158 // LoadConfig loads the Kerberos configuration for the client from file path specified.
159 func (cl *Client) LoadConfig(cfgPath string) (*Client, error) {
160 cfg, err := config.Load(cfgPath)
161 if err != nil {
162 return cl, err
163 }
164 cl.Config = cfg
165 return cl, nil
166 }
167
168 // IsConfigured indicates if the client has the values required set.
169 func (cl *Client) IsConfigured() (bool, error) {
170 if cl.Credentials.Username == "" {
171 return false, errors.New("client does not have a username")
172 }
173 if cl.Credentials.Realm == "" {
174 return false, errors.New("client does not have a define realm")
175 }
176 // Client needs to have either a password, keytab or a session already (later when loading from CCache)
177 if !cl.Credentials.HasPassword() && !cl.Credentials.HasKeytab() {
178 sess, err := cl.GetSessionFromRealm(cl.Credentials.Realm)
179 if err != nil || sess.AuthTime.IsZero() {
180 return false, errors.New("client has neither a keytab nor a password set and no session")
181 }
182 }
183 if !cl.Config.LibDefaults.DNSLookupKDC {
184 for _, r := range cl.Config.Realms {
185 if r.Realm == cl.Credentials.Realm {
186 if len(r.KDC) > 0 {
187 return true, nil
188 }
189 return false, errors.New("client krb5 config does not have any defined KDCs for the default realm")
190 }
191 }
192 }
193 return true, nil
194 }
195
196 // Login the client with the KDC via an AS exchange.
197 func (cl *Client) Login() error {
198 if ok, err := cl.IsConfigured(); !ok {
199 return err
200 }
201 ASReq, err := messages.NewASReqForTGT(cl.Credentials.Realm, cl.Config, cl.Credentials.CName)
202 if err != nil {
203 return krberror.Errorf(err, krberror.KRBMsgError, "error generating new AS_REQ")
204 }
205 err = setPAData(cl, messages.KRBError{}, &ASReq)
206 if err != nil {
207 return krberror.Errorf(err, krberror.KRBMsgError, "failed setting AS_REQ PAData")
208 }
209 ASRep, err := cl.ASExchange(cl.Credentials.Realm, ASReq, 0)
210 if err != nil {
211 return err
212 }
213 cl.AddSession(ASRep.Ticket, ASRep.DecryptedEncPart)
214 return nil
215 }
216
217 // Destroy stops the auto-renewal of all sessions and removes the sessions and cache entries from the client.
218 func (cl *Client) Destroy() {
219 creds := credentials.NewCredentials("", "")
220 cl.sessions.destroy()
221 cl.Cache.clear()
222 cl.Credentials = &creds
223 }