Squid Web Cache master
Loading...
Searching...
No Matches
Config.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2025 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#include "squid.h"
10#include "acl/FilledChecklist.h"
11#include "acl/Gadgets.h"
13#include "adaptation/Config.h"
14#include "adaptation/History.h"
15#include "adaptation/Service.h"
17#include "cache_cf.h"
18#include "ConfigParser.h"
19#include "debug/Messages.h"
20#include "globals.h"
21#include "HttpReply.h"
22#include "HttpRequest.h"
23#include "Store.h"
24
25#include <algorithm>
26
33
34Notes&
36{
37 static const auto protectedFieldNamesRaw = {
38 "Allow",
39 "Date",
40 "Encapsulated",
41 "ISTag",
42 "Max-Connections",
43 "Methods",
44 "Opt-body-type",
45 "Options-TTL",
46 "Preview",
47 "Service",
48 "Service-ID",
49 "Transfer-Complete",
50 "Transfer-Ignore",
51 "Transfer-Preview"
52 };
53 static const Notes::Keys protectedFieldNames(std::begin(protectedFieldNamesRaw), std::end(protectedFieldNamesRaw));
54 static const auto metaHeaders = new Notes("ICAP header", &protectedFieldNames);
55 return *metaHeaders;
56}
58
61{
62 return new ServiceConfig();
63}
64
65void
67{
68 removeRule(service);
69 const Groups& groups = AllGroups();
70 for (unsigned int i = 0; i < groups.size(); ) {
71 const ServiceGroupPointer group = groups[i];
72 const ServiceGroup::Store& services = group->services;
73 typedef ServiceGroup::Store::const_iterator SGSI;
74 for (SGSI it = services.begin(); it != services.end(); ++it) {
75 if (*it == service) {
76 group->removedServices.push_back(service);
77 ServiceGroup::Store::iterator newend;
78 newend = std::remove(group->services.begin(), group->services.end(), service);
79 group->services.resize(newend-group->services.begin());
80 debugs(93, 5, "adaptation service " << service <<
81 " removed from group " << group->id);
82 break;
83 }
84 }
85 if (services.empty()) {
86 removeRule(group->id);
87 Groups::iterator newend;
88 newend = std::remove(AllGroups().begin(), AllGroups().end(), group);
89 AllGroups().resize(newend-AllGroups().begin());
90 } else {
91 ++i;
92 }
93 }
94}
95
98{
99 typedef ServiceConfigs::const_iterator SCI;
100 const ServiceConfigs& configs = serviceConfigs;
101 for (SCI cfg = configs.begin(); cfg != configs.end(); ++cfg) {
102 if ((*cfg)->key == service)
103 return *cfg;
104 }
105 return nullptr;
106}
107
108void
110{
111 typedef AccessRules::const_iterator ARI;
112 const AccessRules& rules = AllRules();
113 for (ARI it = rules.begin(); it != rules.end(); ++it) {
114 AccessRule* rule = *it;
115 if (rule->groupId == id) {
116 debugs(93, 5, "removing access rules for:" << id);
117 AccessRules::iterator newend;
118 newend = std::remove(AllRules().begin(), AllRules().end(), rule);
119 AllRules().resize(newend-AllRules().begin());
120 delete (rule);
121 break;
122 }
123 }
124}
125
126void
128{
129 debugs(93, 3, "rules: " << AllRules().size() << ", groups: " <<
130 AllGroups().size() << ", services: " << serviceConfigs.size());
131 typedef ServiceConfigs::const_iterator SCI;
132 const ServiceConfigs& configs = serviceConfigs;
133 for (SCI cfg = configs.begin(); cfg != configs.end(); ++cfg)
134 removeService((*cfg)->key);
135 serviceConfigs.clear();
136 debugs(93, 3, "rules: " << AllRules().size() << ", groups: " <<
137 AllGroups().size() << ", services: " << serviceConfigs.size());
138}
139
140void
142{
143 ServiceConfigPointer cfg = newServiceConfig();
144 if (!cfg->parse()) {
145 fatalf("%s:%d: malformed adaptation service configuration",
147 }
148 serviceConfigs.push_back(cfg);
149}
150
151void
153{
154 FreeAccess();
155 FreeServiceGroups();
156
158
159 serviceConfigs.clear();
160}
161
162void
163Adaptation::Config::dumpService(StoreEntry *entry, const char *name) const
164{
165 typedef Services::iterator SCI;
166 for (SCI i = AllServices().begin(); i != AllServices().end(); ++i) {
167 const ServiceConfig &cfg = (*i)->cfg();
168 bool isEcap = cfg.protocol.caseCmp("ecap") == 0;
169 bool isIcap = !isEcap;
170 const char *optConnectionEncryption = "";
171 // Print connections_encrypted option if no default value is used
173 optConnectionEncryption = " connection-encryption=off";
174 else if (isEcap && !cfg.connectionEncryption)
175 optConnectionEncryption = " connection-encryption=off";
176 else if (isIcap && !cfg.secure.encryptTransport && cfg.connectionEncryption)
177 optConnectionEncryption = " connection-encryption=on";
178
179 storeAppendPrintf(entry, "%s " SQUIDSTRINGPH " %s_%s %d " SQUIDSTRINGPH "%s\n",
180 name,
182 cfg.methodStr(), cfg.vectPointStr(), cfg.bypass,
184
185 optConnectionEncryption);
186 }
187}
188
189bool
191{
192 if (!onoff) {
193 clear();
194 return false;
195 }
196
197 // create service reps from service configs
198 int created = 0;
199
200 typedef ServiceConfigs::const_iterator VISCI;
201 const ServiceConfigs &configs = serviceConfigs;
202 for (VISCI i = configs.begin(); i != configs.end(); ++i) {
203 const ServiceConfigPointer cfg = *i;
204 if (FindService(cfg->key) != nullptr) {
205 debugs(93, DBG_CRITICAL, "ERROR: Duplicate adaptation service name: " <<
206 cfg->key);
207 continue; // TODO: make fatal
208 }
209 ServicePointer s = createService(cfg);
210 if (s != nullptr) {
211 AllServices().push_back(s);
212 ++created;
213 }
214 }
215
216 debugs(93,3, "Created " << created << " adaptation services");
217
218 // services remember their configs; we do not have to
219 serviceConfigs.clear();
220 return true;
221}
222
223// poor man for_each
224template <class Collection>
225static void
226FinalizeEach(Collection &collection, const char *label)
227{
228 typedef typename Collection::iterator CI;
229 for (CI i = collection.begin(); i != collection.end(); ++i)
230 (*i)->finalize();
231
232 debugs(93,2, "Initialized " << collection.size() << ' ' << label);
233}
234
235void
237{
238 Enabled = enabled;
239 debugs(93, Important(11), "Adaptation support is " << (Enabled ? "on" : "off."));
240
241 FinalizeEach(AllServices(), "message adaptation services");
242 FinalizeEach(AllGroups(), "message adaptation service groups");
243 FinalizeEach(AllRules(), "message adaptation access rules");
244}
245
246void
251
252void
257
258void
260{
261 assert(g != nullptr);
262 g->parse();
263 AllGroups().push_back(g);
264}
265
266void
268{
269 while (!AllGroups().empty()) {
270 // groups are refcounted so we do not explicitly delete them
271 AllGroups().pop_back();
272 }
273}
274
275void
277{
278 typedef Groups::iterator GI;
279 for (GI i = AllGroups().begin(); i != AllGroups().end(); ++i)
280 storeAppendPrintf(entry, "%s " SQUIDSTRINGPH "\n", name, SQUIDSTRINGPRINT((*i)->id));
281}
282
283void
285{
287 AccessRule *r;
288 if (!(r=FindRuleByGroupId(groupId))) {
289 r = new AccessRule(groupId);
290 AllRules().push_back(r);
291 }
292 r->parse(parser);
293}
294
295void
297{
298 while (!AllRules().empty()) {
299 delete AllRules().back();
300 AllRules().pop_back();
301 }
302}
303
304void
306{
307 LOCAL_ARRAY(char, nom, 64);
308
309 typedef AccessRules::iterator CI;
310 for (CI i = AllRules().begin(); i != AllRules().end(); ++i) {
311 snprintf(nom, 64, "%s " SQUIDSTRINGPH, name, SQUIDSTRINGPRINT((*i)->groupId));
312 dump_acl_access(entry, nom, (*i)->acl);
313 }
314}
315
317 onoff(0), service_failure_limit(0), oldest_service_failure(0),
318 service_revival_delay(0)
319{}
320
321// XXX: this is called for ICAP and eCAP configs, but deals mostly
322// with global arrays shared by those individual configs
324{
325 freeService();
326}
327
int size
Definition ModDevPoll.cc:70
#define SQUIDSTRINGPH
Definition SquidString.h:22
#define SQUIDSTRINGPRINT(s)
Definition SquidString.h:23
static void FinalizeEach(Collection &collection, const char *label)
Definition Config.cc:226
#define assert(EX)
Definition assert.h:17
const char * cfg_filename
Definition cache_cf.cc:270
int config_lineno
Definition cache_cf.cc:271
void parse(ConfigParser &parser)
Definition AccessRule.cc:30
static void DumpServiceGroups(StoreEntry *, const char *)
Definition Config.cc:276
void parseService(void)
Definition Config.cc:141
static int send_client_ip
Definition Config.h:47
void removeService(const String &service)
Removes the given service from all service groups.
Definition Config.cc:66
virtual bool finalize()
Definition Config.cc:190
virtual void clear()
Removes any reference to the services from configuration.
Definition Config.cc:127
ServiceConfigPointer findServiceConfig(const String &)
Definition Config.cc:97
static void ParseServiceSet(void)
Definition Config.cc:247
static void Finalize(bool enable)
Definition Config.cc:236
static void FreeServiceGroups(void)
Definition Config.cc:267
static int use_indirect_client
Definition Config.h:49
static void ParseAccess(ConfigParser &parser)
Definition Config.cc:284
static void ParseServiceGroup(ServiceGroupPointer group)
Definition Config.cc:259
void removeRule(const String &id)
Removes access rules of the given service or group.
Definition Config.cc:109
static bool needHistory
HttpRequest adaptation history should recorded.
Definition Config.h:60
static void FreeAccess(void)
Definition Config.cc:296
static int send_username
Definition Config.h:48
void dumpService(StoreEntry *, const char *) const
Definition Config.cc:163
std::vector< ServiceConfigPointer > ServiceConfigs
Definition Config.h:62
static char * masterx_shared_name
Definition Config.h:45
static bool Enabled
Definition Config.h:42
static int service_iteration_limit
Definition Config.h:46
virtual ~Config()
Definition Config.cc:323
void freeService(void)
Definition Config.cc:152
static Notes & metaHeaders()
The list of configured meta headers.
Definition Config.cc:35
virtual ServiceConfig * newServiceConfig() const
creates service configuration object that will parse and keep cfg info
Definition Config.cc:60
static void ParseServiceChain(void)
Definition Config.cc:253
static void DumpAccess(StoreEntry *, const char *)
Definition Config.cc:305
a group of services that must be used one after another
Security::PeerOptions secure
const char * vectPointStr() const
YesNoNone connectionEncryption
whether this service uses only secure connections
const char * methodStr() const
std::vector< String > Store
Store removedServices
the disabled services in the case ecap or icap is disabled
static char * NextToken()
Definition Notes.h:114
std::vector< SBuf > Keys
unordered annotation names
Definition Notes.h:117
bool encryptTransport
whether transport encryption (TLS/SSL) is to be used on connections to the peer
int caseCmp(char const *) const
Definition String.cc:273
#define Important(id)
Definition Messages.h:93
#define debugs(SECTION, LEVEL, CONTENT)
Definition Stream.h:192
#define DBG_CRITICAL
Definition Stream.h:37
void fatalf(const char *fmt,...)
Definition fatal.cc:68
void dump_acl_access(StoreEntry *entry, const char *name, acl_access *head)
Definition cache_cf.cc:1499
std::vector< ServiceGroupPointer > Groups
Services & AllServices()
Definition Service.cc:61
AccessRules & AllRules()
Definition AccessRule.cc:61
void DetachServices()
detach all adaptation services from current configuration
Definition Service.cc:78
Groups & AllGroups()
std::vector< Adaptation::AccessRule * > AccessRules
Definition AccessRule.h:47
ServicePointer FindService(const Service::Id &key)
Definition Service.cc:68
AccessRule * FindRuleByGroupId(const String &groupId)
Definition AccessRule.cc:81
#define LOCAL_ARRAY(type, name, size)
Definition squid.h:62
void storeAppendPrintf(StoreEntry *e, const char *fmt,...)
Definition store.cc:855