| 1 |
|
| 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 |
|
| 21 |
type Client struct { |
| 22 |
Credentials *credentials.Credentials |
| 23 |
Config *config.Config |
| 24 |
GoKrb5Conf *Config |
| 25 |
sessions *sessions |
| 26 |
Cache *Cache |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
type Config struct { |
| 33 |
DisablePAFXFast bool |
| 34 |
AssumePAEncTimestampRequired bool |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 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 |
|
| 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 |
|
| 67 |
|
| 68 |
|
| 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 |
|
| 119 |
func (cl *Client) WithConfig(cfg *config.Config) *Client { |
| 120 |
cl.Config = cfg |
| 121 |
return cl |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
func (cl *Client) WithKeytab(kt keytab.Keytab) *Client { |
| 126 |
cl.Credentials.WithKeytab(kt) |
| 127 |
return cl |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
func (cl *Client) WithPassword(password string) *Client { |
| 132 |
cl.Credentials.WithPassword(password) |
| 133 |
return cl |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 218 |
func (cl *Client) Destroy() { |
| 219 |
creds := credentials.NewCredentials("", "") |
| 220 |
cl.sessions.destroy() |
| 221 |
cl.Cache.clear() |
| 222 |
cl.Credentials = &creds |
| 223 |
} |