client.go 7.3 KB

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