CredentialsCache.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9/* DEBUG: section 29 Authenticator */
10
11#include "squid.h"
12#include "acl/Gadgets.h"
13#include "auth/Config.h"
16#include "debug/Stream.h"
17#include "event.h"
18
19namespace Auth {
20
22{
23public:
24 explicit CredentialCacheRr(const char *n, CredentialsCache * const c) :
25 name(n),
26 whichCache(c)
27 {}
28
29 ~CredentialCacheRr() override {
30 debugs(29, 5, "Terminating Auth credentials cache: " << name);
31 // invalidate the CBDATA reference.
32 // causes Auth::*::User::Cache() to produce nil / invalid pointer
33 delete whichCache.get();
34 }
35
36 void endingShutdown() override {
37 debugs(29, 5, "Clearing Auth credentials cache: " << name);
38 whichCache->reset();
39 }
40
41 void syncConfig() override {
42 debugs(29, 5, "Reconfiguring Auth credentials cache: " << name);
43 whichCache->doConfigChangeCleanup();
44 }
45
46private:
48 const char *name;
49
52};
53
55
56CredentialsCache::CredentialsCache(const char *name, const char * const prettyEvName) :
57 gcScheduled_(false),
58 cacheCleanupEventName(prettyEvName)
59{
60 debugs(29, 5, "initializing " << name << " credentials cache");
62}
63
65CredentialsCache::lookup(const SBuf &userKey) const
66{
67 debugs(29, 6, "lookup for " << userKey);
68 auto p = store_.find(userKey);
69 if (p == store_.end())
70 return User::Pointer(nullptr);
71 return p->second;
72}
73
74void
76{
77 debugs(29, 5, "checkpoint");
78 // data is this in disguise
79 CredentialsCache *self = static_cast<CredentialsCache *>(data);
80 self->cleanup();
81}
82
83void
85{
86 // cache entries with expiretime <= expirationTime are to be evicted
87 const time_t expirationTime = current_time.tv_sec - Auth::TheConfig.credentialsTtl;
88
89 const auto end = store_.end();
90 for (auto i = store_.begin(); i != end;) {
91 debugs(29, 6, "considering " << i->first << "(expires in " <<
92 (expirationTime - i->second->expiretime) << " sec)");
93 if (i->second->expiretime <= expirationTime) {
94 debugs(29, 6, "evicting " << i->first);
95 i = store_.erase(i); //erase advances i
96 } else {
97 ++i;
98 }
99 }
100 gcScheduled_ = false;
102}
103
104void
106{
107 debugs(29, 6, "adding " << userKey << " (" << anAuth_user->username() << ")");
108 store_[userKey] = anAuth_user;
110}
111
112// generates the list of cached usernames in a format that is convenient
113// to merge with equivalent lists obtained from other CredentialsCaches.
114std::vector<Auth::User::Pointer>
116{
117 std::vector<Auth::User::Pointer> rv(size(), nullptr);
118 std::transform(store_.begin(), store_.end(), rv.begin(),
119 [](StoreType::value_type v) { return v.second; }
120 );
121 std::sort(rv.begin(), rv.end(),
122 [](const Auth::User::Pointer &lhs, const Auth::User::Pointer &rhs) {
123 return strcmp(lhs->username(), rhs->username()) < 0;
124 }
125 );
126 return rv;
127}
128
129void
131{
132 if (!gcScheduled_ && store_.size()) {
133 gcScheduled_ = true;
135 this, Auth::TheConfig.garbageCollectInterval, 1);
136 }
137}
138
139void
141{
142 // purge expired entries entirely
143 cleanup();
144 // purge the ACL match data stored in the credentials
145 for (auto i : store_) {
146 aclCacheMatchFlush(&i.second->proxy_match_cache);
147 }
148}
149
150} /* namespace Auth */
151
bool RegisterRunner(RegisteredRunner *rr)
registers a given runner with the given registry and returns true on success
time_t credentialsTtl
the authenticate_ttl
Definition: Config.h:43
void endingShutdown() override
CbcPointer< CredentialsCache > whichCache
reference to the scheme cache which is being managed
CredentialCacheRr(const char *n, CredentialsCache *const c)
const char * name
name of the cache being managed, for logs
void syncConfig() override
Cache of Auth::User credentials, keyed by Auth::User::userKey.
Auth::User::Pointer lookup(const SBuf &userKey) const
bool gcScheduled_
whether a cleanup (garbage collection) event has been scheduled
void cleanup()
cache garbage collection, removes timed-out entries
std::vector< Auth::User::Pointer > sortedUsersList() const
static void Cleanup(void *)
CredentialsCache(const char *name, const char *const eventName)
void insert(const SBuf &userKey, Auth::User::Pointer anAuth_user)
add an user to the cache with the provided key
const char *const cacheCleanupEventName
RefCount< User > Pointer
Definition: User.h:39
Definition: SBuf.h:94
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Stream.h:194
void eventAdd(const char *name, EVH *func, void *arg, double when, int weight, bool cbdata)
Definition: event.cc:107
void aclCacheMatchFlush(dlink_list *cache)
Definition: Acl.cc:391
HTTP Authentication.
Definition: Config.h:19
CBDATA_CLASS_INIT(CredentialsCache)
Auth::Config TheConfig
Definition: Config.cc:15
struct timeval current_time
the current UNIX time in timeval {seconds, microseconds} format
Definition: gadgets.cc:17

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors