client.go 6.4 KB

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