]> git.ipfire.org Git - thirdparty/squid.git/blame - src/cache_manager.cc
Maintenance: rework SASL detection (#1694)
[thirdparty/squid.git] / src / cache_manager.cc
CommitLineData
22f3fd98 1/*
b8ae064d 2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
e25c139f 3 *
bbc27441
AJ
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.
22f3fd98 7 */
8
bbc27441
AJ
9/* DEBUG: section 16 Cache Manager Objects */
10
f7f3304a 11#include "squid.h"
7e6eabbc 12#include "AccessLogEntry.h"
8822ebee 13#include "base/TextException.h"
62ee09ca 14#include "CacheManager.h"
5c336a3b 15#include "comm/Connection.h"
675b8408 16#include "debug/Stream.h"
26e65059 17#include "error/ExceptionErrorDetail.h"
53521734 18#include "errorpage.h"
8822ebee 19#include "fde.h"
92a5adb7 20#include "HttpHdrCc.h"
528b2c61 21#include "HttpReply.h"
22#include "HttpRequest.h"
8822ebee 23#include "mgr/Action.h"
602d9612
A
24#include "mgr/ActionCreator.h"
25#include "mgr/ActionPasswordList.h"
8822ebee
AR
26#include "mgr/ActionProfile.h"
27#include "mgr/BasicActions.h"
28#include "mgr/Command.h"
29#include "mgr/Forwarder.h"
30#include "mgr/FunAction.h"
b8151fa1 31#include "mgr/QueryParams.h"
26e65059 32#include "parser/Tokenizer.h"
592b8687 33#include "protos.h"
26e65059 34#include "sbuf/Stream.h"
2582f64a 35#include "sbuf/StringConvert.h"
4d5904f7 36#include "SquidConfig.h"
8822ebee 37#include "Store.h"
602d9612 38#include "tools.h"
d295d770 39#include "wordlist.h"
5bed43d6 40
8822ebee 41#include <algorithm>
92a5adb7 42#include <memory>
22f3fd98 43
63be0a78 44/// \ingroup CacheManagerInternal
22f3fd98 45#define MGR_PASSWD_SZ 128
46
8822ebee
AR
47/// creates Action using supplied Action::Create method and command
48class ClassActionCreator: public Mgr::ActionCreator
49{
50public:
51 typedef Mgr::Action::Pointer Handler(const Mgr::Command::Pointer &cmd);
c83f0bd5 52
8822ebee
AR
53public:
54 ClassActionCreator(Handler *aHandler): handler(aHandler) {}
55
337b9aa4 56 Mgr::Action::Pointer create(const Mgr::Command::Pointer &cmd) const override {
8822ebee
AR
57 return handler(cmd);
58 }
59
60private:
61 Handler *handler;
62};
63
8822ebee
AR
64/// Registers new profiles, ignoring attempts to register a duplicate
65void
66CacheManager::registerProfile(const Mgr::ActionProfile::Pointer &profile)
62ee09ca 67{
aee3523a 68 Must(profile != nullptr);
e3c2ea01 69 if (!CacheManager::findAction(profile->name)) {
8822ebee 70 menu_.push_back(profile);
bf95c10a 71 debugs(16, 3, "registered profile: " << *profile);
8822ebee 72 } else {
bf95c10a 73 debugs(16, 2, "skipped duplicate profile: " << *profile);
8822ebee 74 }
62ee09ca 75}
22f3fd98 76
50847dca
AJ
77/**
78 \ingroup CacheManagerAPI
79 * Registers a C-style action, which is implemented as a pointer to a function
80 * taking as argument a pointer to a StoreEntry and returning void.
81 * Implemented via CacheManagerActionLegacy.
82 */
22f3fd98 83void
8822ebee 84CacheManager::registerProfile(char const * action, char const * desc, OBJH * handler, int pw_req_flag, int atomic)
22f3fd98 85{
bf95c10a 86 debugs(16, 3, "registering legacy " << action);
8822ebee 87 const Mgr::ActionProfile::Pointer profile = new Mgr::ActionProfile(action,
d9fc6862 88 desc, pw_req_flag, atomic, new Mgr::FunActionCreator(handler));
8822ebee 89 registerProfile(profile);
1d8395bd 90}
62e76326 91
d154d3ec 92/**
e0d28505 93 * \ingroup CacheManagerAPI
a750e510 94 * Registers a C++-style action, via a pointer to a subclass of
d154d3ec
FC
95 * a CacheManagerAction object, whose run() method will be invoked when
96 * CacheManager identifies that the user has requested the action.
97 */
1d8395bd 98void
8822ebee 99CacheManager::registerProfile(char const * action, char const * desc,
d9fc6862
A
100 ClassActionCreator::Handler *handler,
101 int pw_req_flag, int atomic)
1d8395bd 102{
8822ebee 103 const Mgr::ActionProfile::Pointer profile = new Mgr::ActionProfile(action,
d9fc6862 104 desc, pw_req_flag, atomic, new ClassActionCreator(handler));
8822ebee 105 registerProfile(profile);
62ee09ca 106}
107
d154d3ec
FC
108/**
109 \ingroup CacheManagerInternal
110 * Locates an action in the actions registry ActionsList.
111\retval NULL if Action not found
112\retval CacheManagerAction* if the action was found
113 */
8822ebee
AR
114Mgr::ActionProfile::Pointer
115CacheManager::findAction(char const * action) const
22f3fd98 116{
aee3523a 117 Must(action != nullptr);
8822ebee 118 Menu::const_iterator a;
03c4599f
K
119
120 debugs(16, 5, "CacheManager::findAction: looking for action " << action);
8822ebee
AR
121 for (a = menu_.begin(); a != menu_.end(); ++a) {
122 if (0 == strcmp((*a)->name, action)) {
03c4599f
K
123 debugs(16, 6, " found");
124 return *a;
125 }
22f3fd98 126 }
62e76326 127
03c4599f 128 debugs(16, 6, "Action not found.");
8822ebee
AR
129 return Mgr::ActionProfilePointer();
130}
131
132Mgr::Action::Pointer
133CacheManager::createNamedAction(const char *actionName)
134{
135 Must(actionName);
136
137 Mgr::Command::Pointer cmd = new Mgr::Command;
138 cmd->profile = findAction(actionName);
139 cmd->params.actionName = actionName;
140
aee3523a 141 Must(cmd->profile != nullptr);
8822ebee
AR
142 return cmd->profile->creator->create(cmd);
143}
144
145Mgr::Action::Pointer
146CacheManager::createRequestedAction(const Mgr::ActionParams &params)
147{
148 Mgr::Command::Pointer cmd = new Mgr::Command;
149 cmd->params = params;
150 cmd->profile = findAction(params.actionName.termedBuf());
aee3523a 151 Must(cmd->profile != nullptr);
8822ebee 152 return cmd->profile->creator->create(cmd);
22f3fd98 153}
154
3c383cc3
AR
155const SBuf &
156CacheManager::WellKnownUrlPathPrefix()
157{
158 static const SBuf prefix("/squid-internal-mgr/");
159 return prefix;
160}
161
832c08ab 162/**
7902bd5b
EB
163 * Parses the action requested by the user and checks via
164 * CacheManager::ActionProtection() that the item is accessible by the user.
26e65059
AJ
165 *
166 * Syntax:
167 *
7902bd5b 168 * [ scheme "://" authority ] '/squid-internal-mgr' path-absolute [ "?" query ] [ "#" fragment ]
26e65059 169 *
7902bd5b 170 * see RFC 3986 for definitions of scheme, authority, path-absolute, query
26e65059
AJ
171 *
172 * \returns Mgr::Command object with action to perform and parameters it might use
832c08ab 173 */
8822ebee 174Mgr::Command::Pointer
26e65059 175CacheManager::ParseUrl(const AnyP::Uri &uri)
22f3fd98 176{
26e65059 177 Parser::Tokenizer tok(uri.path());
8822ebee 178
3c383cc3 179 Assure(tok.skip(WellKnownUrlPathPrefix()));
62e76326 180
26e65059
AJ
181 Mgr::Command::Pointer cmd = new Mgr::Command();
182 cmd->params.httpUri = SBufToString(uri.absolute());
5366b99b 183
7902bd5b 184 static const auto fieldChars = CharacterSet("mgr-field", "?#").complement();
26e65059
AJ
185
186 SBuf action;
187 if (!tok.prefix(action, fieldChars)) {
7902bd5b
EB
188 static const SBuf indexReport("index");
189 action = indexReport;
22f3fd98 190 }
26e65059
AJ
191 cmd->params.actionName = SBufToString(action);
192
193 const auto profile = findAction(action.c_str());
194 if (!profile)
195 throw TextException(ToSBuf("action '", action, "' not found"), Here());
62e76326 196
8822ebee 197 const char *prot = ActionProtection(profile);
26e65059
AJ
198 if (!strcmp(prot, "disabled") || !strcmp(prot, "hidden"))
199 throw TextException(ToSBuf("action '", action, "' is ", prot), Here());
200 cmd->profile = profile;
201
26e65059
AJ
202 // TODO: fix when AnyP::Uri::parse() separates path?query#fragment
203 SBuf params;
204 if (tok.skip('?')) {
205 params = tok.remaining();
206 Mgr::QueryParams::Parse(tok, cmd->params.queryParams);
207 }
208
209 if (!tok.skip('#') && !tok.atEnd())
210 throw TextException("invalid characters in URL", Here());
211 // else ignore #fragment (if any)
212
7902bd5b 213 debugs(16, 3, "MGR request: host=" << uri.host() << ", action=" << action << ", params=" << params);
26e65059 214
8822ebee 215 return cmd;
22f3fd98 216}
217
63be0a78 218/// \ingroup CacheManagerInternal
832c08ab
FC
219/*
220 \ingroup CacheManagerInternal
221 * Decodes the headers needed to perform user authentication and fills
222 * the details into the cachemgrStateData argument
223 */
c83f0bd5 224void
8822ebee 225CacheManager::ParseHeaders(const HttpRequest * request, Mgr::ActionParams &params)
63259c34 226{
8822ebee
AR
227 assert(request);
228
229 params.httpMethod = request->method.id();
230 params.httpFlags = request->flags;
231
9da6b594
AJ
232#if HAVE_AUTH_MODULE_BASIC
233 // TODO: use the authentication system decode to retrieve these details properly.
234
235 /* base 64 _decoded_ user:passwd pair */
2582f64a 236 const auto basic_cookie(request->header.getAuthToken(Http::HdrType::AUTHORIZATION, "Basic"));
62e76326 237
2582f64a 238 if (basic_cookie.isEmpty())
62e76326 239 return;
240
2582f64a
AJ
241 const auto colonPos = basic_cookie.find(':');
242 if (colonPos == SBuf::npos) {
d816f28d 243 debugs(16, DBG_IMPORTANT, "ERROR: CacheManager::ParseHeaders: unknown basic_cookie format '" << basic_cookie << "'");
62e76326 244 return;
63259c34 245 }
62e76326 246
63259c34 247 /* found user:password pair, reset old values */
2582f64a
AJ
248 params.userName = SBufToString(basic_cookie.substr(0, colonPos));
249 params.password = SBufToString(basic_cookie.substr(colonPos+1));
62e76326 250
9da6b594 251 /* warning: this prints decoded password which maybe not be what you want to do @?@ @?@ */
8822ebee 252 debugs(16, 9, "CacheManager::ParseHeaders: got user: '" <<
d9fc6862 253 params.userName << "' passwd: '" << params.password << "'");
9da6b594 254#endif
63259c34 255}
256
63be0a78 257/**
258 \ingroup CacheManagerInternal
259 *
f53969cc
SM
260 \retval 0 if mgr->password is good or "none"
261 \retval 1 if mgr->password is "disable"
262 \retval !0 if mgr->password does not match configured password
22f3fd98 263 */
c83f0bd5 264int
8822ebee 265CacheManager::CheckPassword(const Mgr::Command &cmd)
22f3fd98 266{
aee3523a 267 assert(cmd.profile != nullptr);
8822ebee
AR
268 const char *action = cmd.profile->name;
269 char *pwd = PasswdGet(Config.passwd_list, action);
03c4599f 270
8822ebee 271 debugs(16, 4, "CacheManager::CheckPassword for action " << action);
62e76326 272
aee3523a 273 if (pwd == nullptr)
8822ebee 274 return cmd.profile->isPwReq;
62e76326 275
22f3fd98 276 if (strcmp(pwd, "disable") == 0)
62e76326 277 return 1;
278
22f3fd98 279 if (strcmp(pwd, "none") == 0)
62e76326 280 return 0;
281
8822ebee 282 if (!cmd.params.password.size())
62e76326 283 return 1;
284
8822ebee 285 return cmd.params.password != pwd;
22f3fd98 286}
287
832c08ab
FC
288/**
289 \ingroup CacheManagerAPI
290 * Main entry point in the Cache Manager's activity. Gets called as part
291 * of the forward chain if the right URL is detected there. Initiates
292 * all needed internal work and renders the response.
293 */
22f3fd98 294void
7e6eabbc 295CacheManager::start(const Comm::ConnectionPointer &client, HttpRequest *request, StoreEntry *entry, const AccessLogEntry::Pointer &ale)
22f3fd98 296{
26e65059 297 debugs(16, 3, "request-url= '" << request->url << "', entry-url='" << entry->url() << "'");
62e76326 298
26e65059
AJ
299 Mgr::Command::Pointer cmd;
300 try {
301 cmd = ParseUrl(request->url);
302
303 } catch (...) {
304 debugs(16, 2, "request URL error: " << CurrentException);
7e6eabbc 305 const auto err = new ErrorState(ERR_INVALID_URL, Http::scNotFound, request, ale);
3900307b 306 err->url = xstrdup(entry->url());
26e65059 307 err->detailError(new ExceptionErrorDetail(Here().id()));
62e76326 308 errorAppendEntry(entry, err);
62e76326 309 return;
22f3fd98 310 }
62e76326 311
8822ebee 312 const char *actionName = cmd->profile->name;
34266cde 313
22f3fd98 314 entry->expires = squid_curtime;
34266cde 315
a750e510 316 debugs(16, 5, "CacheManager: " << client << " requesting '" << actionName << "'");
34266cde 317
63259c34 318 /* get additional info from request headers */
8822ebee
AR
319 ParseHeaders(request, cmd->params);
320
321 const char *userName = cmd->params.userName.size() ?
d9fc6862 322 cmd->params.userName.termedBuf() : "unknown";
34266cde 323
22f3fd98 324 /* Check password */
62e76326 325
8822ebee 326 if (CheckPassword(*cmd) != 0) {
62e76326 327 /* build error message */
7e6eabbc 328 ErrorState errState(ERR_CACHE_MGR_ACCESS_DENIED, Http::scUnauthorized, request, ale);
62e76326 329 /* warn if user specified incorrect password */
330
8822ebee 331 if (cmd->params.password.size()) {
26ac0430 332 debugs(16, DBG_IMPORTANT, "CacheManager: " <<
8822ebee 333 userName << "@" <<
5c336a3b 334 client << ": incorrect password for '" <<
8822ebee
AR
335 actionName << "'" );
336 } else {
26ac0430 337 debugs(16, DBG_IMPORTANT, "CacheManager: " <<
8822ebee 338 userName << "@" <<
5c336a3b 339 client << ": password needed for '" <<
8822ebee
AR
340 actionName << "'" );
341 }
62e76326 342
913524f0 343 HttpReply *rep = errState.BuildHttpReply();
62e76326 344
9da6b594 345#if HAVE_AUTH_MODULE_BASIC
62e76326 346 /*
8822ebee
AR
347 * add Authenticate header using action name as a realm because
348 * password depends on the action
62e76326 349 */
8822ebee 350 rep->header.putAuth("Basic", actionName);
9da6b594 351#endif
92a5adb7
AR
352
353 const auto originOrNil = request->header.getStr(Http::HdrType::ORIGIN);
354 PutCommonResponseHeaders(*rep, originOrNil);
62e76326 355
356 /* store the reply */
db237875 357 entry->replaceHttpReply(rep);
62e76326 358
359 entry->expires = squid_curtime;
360
361 entry->complete();
362
62e76326 363 return;
22f3fd98 364 }
62e76326 365
789217a2
FC
366 if (request->header.has(Http::HdrType::ORIGIN)) {
367 cmd->params.httpOrigin = request->header.getStr(Http::HdrType::ORIGIN);
3865965d
AJ
368 }
369
0be039f4 370 debugs(16, 2, "CacheManager: " <<
8822ebee 371 userName << "@" <<
5c336a3b 372 client << " requesting '" <<
8822ebee 373 actionName << "'" );
62e76326 374
3c383cc3 375 // special case: an index page
b073fc4b 376 if (!strcmp(cmd->profile->name, "index")) {
7e6eabbc 377 ErrorState err(MGR_INDEX, Http::scOkay, request, ale);
b073fc4b
AJ
378 err.url = xstrdup(entry->url());
379 HttpReply *rep = err.BuildHttpReply();
380 if (strncmp(rep->body.content(),"Internal Error:", 15) == 0)
9b769c67 381 rep->sline.set(Http::ProtocolVersion(1,1), Http::scNotFound);
92a5adb7
AR
382
383 const auto originOrNil = request->header.getStr(Http::HdrType::ORIGIN);
384 PutCommonResponseHeaders(*rep, originOrNil);
385
b073fc4b
AJ
386 entry->replaceHttpReply(rep);
387 entry->complete();
388 return;
389 }
390
8822ebee 391 if (UsingSmp() && IamWorkerProcess()) {
1b76e6c1 392 // is client the right connection to pass here?
7e6eabbc 393 AsyncJob::Start(new Mgr::Forwarder(client, cmd->params, request, entry, ale));
8822ebee 394 return;
cb69b4c7 395 }
62e76326 396
8822ebee 397 Mgr::Action::Pointer action = cmd->profile->creator->create(cmd);
aee3523a 398 Must(action != nullptr);
8822ebee 399 action->run(entry, true);
22f3fd98 400}
401
832c08ab
FC
402/*
403 \ingroup CacheManagerInternal
404 * Renders the protection level text for an action.
405 * Also doubles as a check for the protection level.
832c08ab 406 */
c83f0bd5 407const char *
8822ebee 408CacheManager::ActionProtection(const Mgr::ActionProfile::Pointer &profile)
7395afb8 409{
aee3523a 410 assert(profile != nullptr);
8822ebee 411 const char *pwd = PasswdGet(Config.passwd_list, profile->name);
62e76326 412
7395afb8 413 if (!pwd)
8822ebee 414 return profile->isPwReq ? "hidden" : "public";
62e76326 415
7395afb8 416 if (!strcmp(pwd, "disable"))
62e76326 417 return "disabled";
418
7395afb8 419 if (strcmp(pwd, "none") == 0)
62e76326 420 return "public";
421
7395afb8 422 return "protected";
423}
424
832c08ab 425/*
ee82937c 426 * \ingroup CacheManagerInternal
832c08ab
FC
427 * gets from the global Config the password the user would need to supply
428 * for the action she queried
429 */
c83f0bd5 430char *
613924ee 431CacheManager::PasswdGet(Mgr::ActionPasswordList * a, const char *action)
22f3fd98 432{
a5f27c62
AJ
433 while (a) {
434 for (auto &w : a->actions) {
435 if (w.cmp(action) == 0)
62e76326 436 return a->passwd;
437
a5f27c62
AJ
438 static const SBuf allAction("all");
439 if (w == allAction)
62e76326 440 return a->passwd;
441 }
442
443 a = a->next;
22f3fd98 444 }
62e76326 445
aee3523a 446 return nullptr;
22f3fd98 447}
c83f0bd5 448
92a5adb7
AR
449void
450CacheManager::PutCommonResponseHeaders(HttpReply &response, const char *httpOrigin)
451{
452 // Allow cachemgr and other XHR scripts access to our version string
453 if (httpOrigin) {
454 response.header.putExt("Access-Control-Allow-Origin", httpOrigin);
455#if HAVE_AUTH_MODULE_BASIC
456 response.header.putExt("Access-Control-Allow-Credentials", "true");
457#endif
458 response.header.putExt("Access-Control-Expose-Headers", "Server");
459 }
460
182faab8 461 HttpHdrCc cc;
92a5adb7
AR
462 // this is honored by more caches but allows pointless revalidation;
463 // revalidation will always fail because we do not support it (yet?)
182faab8 464 cc.noCache(String());
92a5adb7 465 // this is honored by fewer caches but prohibits pointless revalidation
182faab8
AR
466 cc.noStore(true);
467 response.putCc(cc);
92a5adb7
AR
468}
469
c83f0bd5 470CacheManager*
26ac0430
AJ
471CacheManager::GetInstance()
472{
f0ffd7c3 473 static CacheManager *instance = nullptr;
7658a296
AJ
474 if (!instance) {
475 debugs(16, 6, "starting cachemanager up");
26ac0430 476 instance = new CacheManager;
8822ebee 477 Mgr::RegisterBasics();
26ac0430
AJ
478 }
479 return instance;
c83f0bd5 480}
53521734 481