Debian Codegraph @

client/session.go

raw symbols SCIP
1 package client
2
3 import (
4 "fmt"
5 "sync"
6 "time"
7
8 "gopkg.in/jcmturner/gokrb5.v5/iana/nametype"
9 "gopkg.in/jcmturner/gokrb5.v5/krberror"
10 "gopkg.in/jcmturner/gokrb5.v5/messages"
11 "gopkg.in/jcmturner/gokrb5.v5/types"
12 )
13
14 // Sessions keyed on the realm name
15 type sessions struct {
16 Entries map[string]*session
17 mux sync.RWMutex
18 }
19
20 func (s *sessions) destroy() {
21 s.mux.Lock()
22 defer s.mux.Unlock()
23 for k, e := range s.Entries {
24 e.destroy()
25 delete(s.Entries, k)
26 }
27 }
28
29 // Client session struct.
30 type session struct {
31 Realm string
32 AuthTime time.Time
33 EndTime time.Time
34 RenewTill time.Time
35 TGT messages.Ticket
36 SessionKey types.EncryptionKey
37 SessionKeyExpiration time.Time
38 cancel chan bool
39 mux sync.RWMutex
40 }
41
42 func (s *session) update(tkt messages.Ticket, dep messages.EncKDCRepPart) {
43 s.mux.Lock()
44 defer s.mux.Unlock()
45 s.AuthTime = dep.AuthTime
46 s.EndTime = dep.EndTime
47 s.RenewTill = dep.RenewTill
48 s.TGT = tkt
49 s.SessionKey = dep.Key
50 s.SessionKeyExpiration = dep.KeyExpiration
51 }
52
53 func (s *session) destroy() {
54 s.mux.Lock()
55 defer s.mux.Unlock()
56 s.cancel <- true
57 s.EndTime = time.Now().UTC()
58 s.RenewTill = s.EndTime
59 s.SessionKeyExpiration = s.EndTime
60 }
61
62 // AddSession adds a session for a realm with a TGT to the client's session cache.
63 // A goroutine is started to automatically renew the TGT before expiry.
64 func (cl *Client) AddSession(tkt messages.Ticket, dep messages.EncKDCRepPart) {
65 cl.sessions.mux.Lock()
66 defer cl.sessions.mux.Unlock()
67 s := &session{
68 Realm: tkt.SName.NameString[1],
69 AuthTime: dep.AuthTime,
70 EndTime: dep.EndTime,
71 RenewTill: dep.RenewTill,
72 TGT: tkt,
73 SessionKey: dep.Key,
74 SessionKeyExpiration: dep.KeyExpiration,
75 cancel: make(chan bool, 1),
76 }
77 // if a session already exists for this, cancel its auto renew.
78 if i, ok := cl.sessions.Entries[tkt.SName.NameString[1]]; ok {
79 i.cancel <- true
80 }
81 cl.sessions.Entries[tkt.SName.NameString[1]] = s
82 cl.enableAutoSessionRenewal(s)
83 }
84
85 // enableAutoSessionRenewal turns on the automatic renewal for the client's TGT session.
86 func (cl *Client) enableAutoSessionRenewal(s *session) {
87 var timer *time.Timer
88 go func(s *session) {
89 for {
90 s.mux.RLock()
91 w := (s.EndTime.Sub(time.Now().UTC()) * 5) / 6
92 s.mux.RUnlock()
93 if w < 0 {
94 return
95 }
96 timer = time.NewTimer(w)
97 select {
98 case <-timer.C:
99 renewal, err := cl.updateSession(s)
100 if !renewal && err == nil {
101 // end this goroutine as there will have been a new login and new auto renewal goroutine created.
102 return
103 }
104 case <-s.cancel:
105 // cancel has been called. Stop the timer and exit.
106 timer.Stop()
107 return
108 }
109 }
110 }(s)
111 }
112
113 // RenewTGT renews the client's TGT session.
114 func (cl *Client) renewTGT(s *session) error {
115 spn := types.PrincipalName{
116 NameType: nametype.KRB_NT_SRV_INST,
117 NameString: []string{"krbtgt", s.Realm},
118 }
119 _, tgsRep, err := cl.TGSExchange(spn, s.TGT.Realm, s.TGT, s.SessionKey, true, 0)
120 if err != nil {
121 return krberror.Errorf(err, krberror.KRBMsgError, "error renewing TGT")
122 }
123 s.update(tgsRep.Ticket, tgsRep.DecryptedEncPart)
124 return nil
125 }
126
127 // updateSession updates either through renewal or creating a new login.
128 // The boolean indicates if the update was a renewal.
129 func (cl *Client) updateSession(s *session) (bool, error) {
130 if time.Now().UTC().Before(s.RenewTill) {
131 err := cl.renewTGT(s)
132 return true, err
133 }
134 err := cl.Login()
135 return false, err
136 }
137
138 func (cl *Client) getSessionFromRemoteRealm(realm string) (*session, error) {
139 cl.sessions.mux.RLock()
140 sess, ok := cl.sessions.Entries[cl.Credentials.Realm]
141 cl.sessions.mux.RUnlock()
142 if !ok {
143 return nil, fmt.Errorf("client does not have a session for realm %s, login first", cl.Credentials.Realm)
144 }
145
146 spn := types.PrincipalName{
147 NameType: nametype.KRB_NT_SRV_INST,
148 NameString: []string{"krbtgt", realm},
149 }
150
151 _, tgsRep, err := cl.TGSExchange(spn, cl.Credentials.Realm, sess.TGT, sess.SessionKey, false, 0)
152 if err != nil {
153 return nil, err
154 }
155 cl.AddSession(tgsRep.Ticket, tgsRep.DecryptedEncPart)
156
157 cl.sessions.mux.RLock()
158 defer cl.sessions.mux.RUnlock()
159 return cl.sessions.Entries[realm], nil
160 }
161
162 // GetSessionFromRealm returns the session for the realm provided.
163 func (cl *Client) GetSessionFromRealm(realm string) (sess *session, err error) {
164 cl.sessions.mux.RLock()
165 s, ok := cl.sessions.Entries[realm]
166 cl.sessions.mux.RUnlock()
167 if !ok {
168 // Try to request TGT from trusted remote Realm
169 s, err = cl.getSessionFromRemoteRealm(realm)
170 if err != nil {
171 return
172 }
173 }
174 // Create another session to return to prevent race condition.
175 sess = &session{
176 Realm: s.Realm,
177 AuthTime: s.AuthTime,
178 EndTime: s.EndTime,
179 RenewTill: s.RenewTill,
180 TGT: s.TGT,
181 SessionKey: s.SessionKey,
182 SessionKeyExpiration: s.SessionKeyExpiration,
183 }
184 return
185 }
186
187 // GetSessionFromPrincipalName returns the session for the realm of the principal provided.
188 func (cl *Client) GetSessionFromPrincipalName(spn types.PrincipalName) (*session, error) {
189 realm := cl.Config.ResolveRealm(spn.NameString[len(spn.NameString)-1])
190 return cl.GetSessionFromRealm(realm)
191 }