Debian Codegraph @

service/cache.go

raw symbols SCIP
1 // Package service provides server side integrations for Kerberos authentication.
2 package service
3
4 import (
5 "gopkg.in/jcmturner/gokrb5.v5/types"
6 "sync"
7 "time"
8 )
9
10 // Cache for tickets received from clients keyed by fully qualified client name. Used to track replay of tickets.
11 type Cache struct {
12 Entries map[string]clientEntries
13 mux sync.RWMutex
14 }
15
16 // clientEntries holds entries of client details sent to the service.
17 type clientEntries struct {
18 ReplayMap map[time.Time]replayCacheEntry
19 SeqNumber int64
20 SubKey types.EncryptionKey
21 }
22
23 // Cache entry tracking client time values of tickets sent to the service.
24 type replayCacheEntry struct {
25 PresentedTime time.Time
26 SName types.PrincipalName
27 CTime time.Time // This combines the ticket's CTime and Cusec
28 }
29
30 func (c *Cache) getClientEntries(cname types.PrincipalName) (clientEntries, bool) {
31 c.mux.RLock()
32 defer c.mux.RUnlock()
33 ce, ok := c.Entries[cname.GetPrincipalNameString()]
34 return ce, ok
35 }
36
37 func (c *Cache) getClientEntry(cname types.PrincipalName, t time.Time) (replayCacheEntry, bool) {
38 if ce, ok := c.getClientEntries(cname); ok {
39 c.mux.RLock()
40 defer c.mux.RUnlock()
41 if e, ok := ce.ReplayMap[t]; ok {
42 return e, true
43 }
44 }
45 return replayCacheEntry{}, false
46 }
47
48 // Instance of the ServiceCache. This needs to be a singleton.
49 var replayCache Cache
50 var once sync.Once
51
52 // GetReplayCache returns a pointer to the Cache singleton.
53 func GetReplayCache(d time.Duration) *Cache {
54 // Create a singleton of the ReplayCache and start a background thread to regularly clean out old entries
55 once.Do(func() {
56 replayCache = Cache{
57 Entries: make(map[string]clientEntries),
58 }
59 go func() {
60 for {
61 // TODO consider using a context here.
62 time.Sleep(d)
63 replayCache.ClearOldEntries(d)
64 }
65 }()
66 })
67 return &replayCache
68 }
69
70 // AddEntry adds an entry to the Cache.
71 func (c *Cache) AddEntry(sname types.PrincipalName, a types.Authenticator) {
72 ct := a.CTime.Add(time.Duration(a.Cusec) * time.Microsecond)
73 if ce, ok := c.getClientEntries(a.CName); ok {
74 c.mux.Lock()
75 defer c.mux.Unlock()
76 ce.ReplayMap[ct] = replayCacheEntry{
77 PresentedTime: time.Now().UTC(),
78 SName: sname,
79 CTime: ct,
80 }
81 ce.SeqNumber = a.SeqNumber
82 ce.SubKey = a.SubKey
83 } else {
84 c.mux.Lock()
85 defer c.mux.Unlock()
86 c.Entries[a.CName.GetPrincipalNameString()] = clientEntries{
87 ReplayMap: map[time.Time]replayCacheEntry{
88 ct: {
89 PresentedTime: time.Now().UTC(),
90 SName: sname,
91 CTime: ct,
92 },
93 },
94 SeqNumber: a.SeqNumber,
95 SubKey: a.SubKey,
96 }
97 }
98 }
99
100 // ClearOldEntries clears entries from the Cache that are older than the duration provided.
101 func (c *Cache) ClearOldEntries(d time.Duration) {
102 c.mux.Lock()
103 defer c.mux.Unlock()
104 for ke, ce := range c.Entries {
105 for k, e := range ce.ReplayMap {
106 if time.Now().UTC().Sub(e.PresentedTime) > d {
107 delete(ce.ReplayMap, k)
108 }
109 }
110 if len(ce.ReplayMap) == 0 {
111 delete(c.Entries, ke)
112 }
113 }
114 }
115
116 // IsReplay tests if the Authenticator provided is a replay within the duration defined. If this is not a replay add the entry to the cache for tracking.
117 func (c *Cache) IsReplay(sname types.PrincipalName, a types.Authenticator) bool {
118 ct := a.CTime.Add(time.Duration(a.Cusec) * time.Microsecond)
119 if e, ok := c.getClientEntry(a.CName, ct); ok {
120 if e.SName.Equal(sname) {
121 return true
122 }
123 }
124 c.AddEntry(sname, a)
125 return false
126 }