client.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Package client provides a client library and methods for Kerberos 5 authentication.
  2. package client
  3. import (
  4. "errors"
  5. "fmt"
  6. "gopkg.in/jcmturner/gokrb5.v1/config"
  7. "gopkg.in/jcmturner/gokrb5.v1/credentials"
  8. "gopkg.in/jcmturner/gokrb5.v1/crypto"
  9. "gopkg.in/jcmturner/gokrb5.v1/crypto/etype"
  10. "gopkg.in/jcmturner/gokrb5.v1/iana/errorcode"
  11. "gopkg.in/jcmturner/gokrb5.v1/iana/nametype"
  12. "gopkg.in/jcmturner/gokrb5.v1/keytab"
  13. "gopkg.in/jcmturner/gokrb5.v1/messages"
  14. "gopkg.in/jcmturner/gokrb5.v1/types"
  15. )
  16. // Client side configuration and state.
  17. type Client struct {
  18. Credentials *credentials.Credentials
  19. Config *config.Config
  20. GoKrb5Conf *Config
  21. sessions sessions
  22. Cache *Cache
  23. }
  24. // Config struct holds GoKRB5 specific client configurations.
  25. // Set Disable_PA_FX_FAST to true to force this behaviour off.
  26. // 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.
  27. type Config struct {
  28. DisablePAFXFast bool
  29. AssumePAEncTimestampRequired bool
  30. }
  31. // NewClientWithPassword creates a new client from a password credential.
  32. func NewClientWithPassword(username, realm, password string) Client {
  33. creds := credentials.NewCredentials(username, realm)
  34. return Client{
  35. Credentials: creds.WithPassword(password),
  36. Config: config.NewConfig(),
  37. GoKrb5Conf: &Config{},
  38. sessions: make(sessions),
  39. Cache: NewCache(),
  40. }
  41. }
  42. // NewClientWithKeytab creates a new client from a keytab credential.
  43. func NewClientWithKeytab(username, realm string, kt keytab.Keytab) Client {
  44. creds := credentials.NewCredentials(username, realm)
  45. return Client{
  46. Credentials: creds.WithKeytab(kt),
  47. Config: config.NewConfig(),
  48. GoKrb5Conf: &Config{},
  49. sessions: make(sessions),
  50. Cache: NewCache(),
  51. }
  52. }
  53. // NewClientFromCCache create a client from a populated client cache.
  54. //
  55. // WARNING: If you do not add a keytab or password to the client then the TGT cannot be renewed and a failure will occur after the TGT expires.
  56. func NewClientFromCCache(c credentials.CCache) (Client, error) {
  57. cl := Client{
  58. Credentials: c.GetClientCredentials(),
  59. Config: config.NewConfig(),
  60. GoKrb5Conf: &Config{},
  61. sessions: make(sessions),
  62. Cache: NewCache(),
  63. }
  64. spn := types.PrincipalName{
  65. NameType: nametype.KRB_NT_SRV_INST,
  66. NameString: []string{"krbtgt", c.DefaultPrincipal.Realm},
  67. }
  68. cred, ok := c.GetEntry(spn)
  69. if !ok {
  70. return cl, errors.New("TGT not found in CCache")
  71. }
  72. var tgt messages.Ticket
  73. err := tgt.Unmarshal(cred.Ticket)
  74. if err != nil {
  75. return cl, fmt.Errorf("TGT bytes in cache are not valid: %v", err)
  76. }
  77. cl.sessions[c.DefaultPrincipal.Realm] = &session{
  78. Realm: c.DefaultPrincipal.Realm,
  79. AuthTime: cred.AuthTime,
  80. EndTime: cred.EndTime,
  81. RenewTill: cred.RenewTill,
  82. TGT: tgt,
  83. SessionKey: cred.Key,
  84. }
  85. for _, cred := range c.GetEntries() {
  86. var tkt messages.Ticket
  87. err = tkt.Unmarshal(cred.Ticket)
  88. if err != nil {
  89. return cl, fmt.Errorf("Cache entry ticket bytes are not valid: %v", err)
  90. }
  91. cl.Cache.addEntry(
  92. tkt,
  93. cred.AuthTime,
  94. cred.StartTime,
  95. cred.EndTime,
  96. cred.RenewTill,
  97. cred.Key,
  98. )
  99. }
  100. return cl, nil
  101. }
  102. // WithConfig sets the Kerberos configuration for the client.
  103. func (cl *Client) WithConfig(cfg *config.Config) *Client {
  104. cl.Config = cfg
  105. return cl
  106. }
  107. // WithKeytab adds a keytab to the client
  108. func (cl *Client) WithKeytab(kt keytab.Keytab) *Client {
  109. cl.Credentials.WithKeytab(kt)
  110. return cl
  111. }
  112. // WithPassword adds a password to the client
  113. func (cl *Client) WithPassword(password string) *Client {
  114. cl.Credentials.WithPassword(password)
  115. return cl
  116. }
  117. // Key returns a key for the client. Preferably from a keytab and then generated from the password.
  118. // The KRBError would have been returned from the KDC and must be of type KDC_ERR_PREAUTH_REQUIRED.
  119. // If a KRBError is not available pass nil and a key will be returned from the credentials keytab.
  120. func (cl *Client) Key(etype etype.EType, krberr messages.KRBError) (types.EncryptionKey, error) {
  121. if cl.Credentials.HasKeytab() && etype != nil {
  122. return cl.Credentials.Keytab.GetEncryptionKey(cl.Credentials.CName.NameString, cl.Credentials.Realm, 0, etype.GetETypeID())
  123. } else if cl.Credentials.HasPassword() {
  124. if krberr.ErrorCode == errorcode.KDC_ERR_PREAUTH_REQUIRED {
  125. var pas types.PADataSequence
  126. err := pas.Unmarshal(krberr.EData)
  127. if err != nil {
  128. return types.EncryptionKey{}, fmt.Errorf("Could not get PAData from KRBError to generate key from password: %v", err)
  129. }
  130. key, _, err := crypto.GetKeyFromPassword(cl.Credentials.Password, krberr.CName, krberr.CRealm, etype.GetETypeID(), pas)
  131. return key, err
  132. }
  133. key, _, err := crypto.GetKeyFromPassword(cl.Credentials.Password, cl.Credentials.CName, cl.Credentials.Realm, etype.GetETypeID(), types.PADataSequence{})
  134. return key, err
  135. }
  136. return types.EncryptionKey{}, errors.New("Credential has neither keytab or password to generate key.")
  137. }
  138. // LoadConfig loads the Kerberos configuration for the client from file path specified.
  139. func (cl *Client) LoadConfig(cfgPath string) (*Client, error) {
  140. cfg, err := config.Load(cfgPath)
  141. if err != nil {
  142. return cl, err
  143. }
  144. cl.Config = cfg
  145. return cl, nil
  146. }
  147. // IsConfigured indicates if the client has the values required set.
  148. func (cl *Client) IsConfigured() (bool, error) {
  149. // Client needs to have either a password, keytab or a session already (later when loading from CCache)
  150. if !cl.Credentials.HasPassword() && !cl.Credentials.HasKeytab() && cl.sessions[cl.Config.LibDefaults.DefaultRealm].AuthTime.IsZero() {
  151. return false, errors.New("client has neither a keytab nor a password set and no session")
  152. }
  153. if cl.Credentials.Username == "" {
  154. return false, errors.New("client does not have a username")
  155. }
  156. if cl.Config.LibDefaults.DefaultRealm == "" {
  157. return false, errors.New("client krb5 config does not have a default realm")
  158. }
  159. for _, r := range cl.Config.Realms {
  160. if r.Realm == cl.Config.LibDefaults.DefaultRealm {
  161. if len(r.KDC) > 0 {
  162. return true, nil
  163. }
  164. return false, errors.New("client krb5 config does not have any defined KDCs for the default realm")
  165. }
  166. }
  167. return false, errors.New("client does not have KDCs configured for the default realm")
  168. }
  169. // Login the client with the KDC via an AS exchange.
  170. func (cl *Client) Login() error {
  171. return cl.ASExchange(cl.Config.LibDefaults.DefaultRealm)
  172. }