]> git.ipfire.org Git - thirdparty/squid.git/blob - src/cache_manager.cc
Merge from trunk
[thirdparty/squid.git] / src / cache_manager.cc
1
2 /*
3 * $Id$
4 *
5 * DEBUG: section 16 Cache Manager Objects
6 * AUTHOR: Duane Wessels
7 *
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
10 *
11 * Squid is the result of efforts by numerous individuals from
12 * the Internet community; see the CONTRIBUTORS file for full
13 * details. Many organizations have provided support for Squid's
14 * development; see the SPONSORS file for full details. Squid is
15 * Copyrighted (C) 2001 by the Regents of the University of
16 * California; see the COPYRIGHT file for full details. Squid
17 * incorporates software developed and/or copyrighted by other
18 * sources; see the CREDITS file for full details.
19 *
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
33 *
34 */
35
36 #include "config.h"
37 #include "base/TextException.h"
38 #include "CacheManager.h"
39 #include "comm/Connection.h"
40 #include "Debug.h"
41 #include "errorpage.h"
42 #include "fde.h"
43 #include "HttpReply.h"
44 #include "HttpRequest.h"
45 #include "mgr/ActionCreator.h"
46 #include "mgr/Action.h"
47 #include "mgr/ActionProfile.h"
48 #include "mgr/BasicActions.h"
49 #include "mgr/Command.h"
50 #include "mgr/Forwarder.h"
51 #include "mgr/FunAction.h"
52 /* for rotate_logs() */
53 #include "protos.h"
54 #include "SquidTime.h"
55 #include "Store.h"
56 #include "wordlist.h"
57 #include <algorithm>
58
59
60 /// \ingroup CacheManagerInternal
61 #define MGR_PASSWD_SZ 128
62
63 /// creates Action using supplied Action::Create method and command
64 class ClassActionCreator: public Mgr::ActionCreator
65 {
66 public:
67 typedef Mgr::Action::Pointer Handler(const Mgr::Command::Pointer &cmd);
68
69 public:
70 ClassActionCreator(Handler *aHandler): handler(aHandler) {}
71
72 virtual Mgr::Action::Pointer create(const Mgr::Command::Pointer &cmd) const {
73 return handler(cmd);
74 }
75
76 private:
77 Handler *handler;
78 };
79
80
81 /// Registers new profiles, ignoring attempts to register a duplicate
82 void
83 CacheManager::registerProfile(const Mgr::ActionProfile::Pointer &profile)
84 {
85 Must(profile != NULL);
86 if (std::find(menu_.begin(), menu_.end(), profile) == menu_.end()) {
87 menu_.push_back(profile);
88 debugs(16, 3, HERE << "registered profile: " << *profile);
89 } else {
90 debugs(16, 2, HERE << "skipped duplicate profile: " << *profile);
91 }
92 }
93
94 /**
95 \ingroup CacheManagerAPI
96 * Registers a C-style action, which is implemented as a pointer to a function
97 * taking as argument a pointer to a StoreEntry and returning void.
98 * Implemented via CacheManagerActionLegacy.
99 */
100 void
101 CacheManager::registerProfile(char const * action, char const * desc, OBJH * handler, int pw_req_flag, int atomic)
102 {
103 debugs(16, 3, HERE << "registering legacy " << action);
104 const Mgr::ActionProfile::Pointer profile = new Mgr::ActionProfile(action,
105 desc, pw_req_flag, atomic, new Mgr::FunActionCreator(handler));
106 registerProfile(profile);
107 }
108
109 /**
110 * \ingroup CacheManagerAPI
111 * Registers a C++-style action, via a pointer to a subclass of
112 * a CacheManagerAction object, whose run() method will be invoked when
113 * CacheManager identifies that the user has requested the action.
114 */
115 void
116 CacheManager::registerProfile(char const * action, char const * desc,
117 ClassActionCreator::Handler *handler,
118 int pw_req_flag, int atomic)
119 {
120 const Mgr::ActionProfile::Pointer profile = new Mgr::ActionProfile(action,
121 desc, pw_req_flag, atomic, new ClassActionCreator(handler));
122 registerProfile(profile);
123 }
124
125 /**
126 \ingroup CacheManagerInternal
127 * Locates an action in the actions registry ActionsList.
128 \retval NULL if Action not found
129 \retval CacheManagerAction* if the action was found
130 */
131 Mgr::ActionProfile::Pointer
132 CacheManager::findAction(char const * action) const
133 {
134 Must(action != NULL);
135 Menu::const_iterator a;
136
137 debugs(16, 5, "CacheManager::findAction: looking for action " << action);
138 for (a = menu_.begin(); a != menu_.end(); ++a) {
139 if (0 == strcmp((*a)->name, action)) {
140 debugs(16, 6, " found");
141 return *a;
142 }
143 }
144
145 debugs(16, 6, "Action not found.");
146 return Mgr::ActionProfilePointer();
147 }
148
149 Mgr::Action::Pointer
150 CacheManager::createNamedAction(const char *actionName)
151 {
152 Must(actionName);
153
154 Mgr::Command::Pointer cmd = new Mgr::Command;
155 cmd->profile = findAction(actionName);
156 cmd->params.actionName = actionName;
157
158 Must(cmd->profile != NULL);
159 return cmd->profile->creator->create(cmd);
160 }
161
162 Mgr::Action::Pointer
163 CacheManager::createRequestedAction(const Mgr::ActionParams &params)
164 {
165 Mgr::Command::Pointer cmd = new Mgr::Command;
166 cmd->params = params;
167 cmd->profile = findAction(params.actionName.termedBuf());
168 Must(cmd->profile != NULL);
169 return cmd->profile->creator->create(cmd);
170 }
171
172 /**
173 \ingroup CacheManagerInternal
174 * define whether the URL is a cache-manager URL and parse the action
175 * requested by the user. Checks via CacheManager::ActionProtection() that the
176 * item is accessible by the user.
177 \retval CacheManager::cachemgrStateData state object for the following handling
178 \retval NULL if the action can't be found or can't be accessed by the user
179 */
180 Mgr::Command::Pointer
181 CacheManager::ParseUrl(const char *url)
182 {
183 int t;
184 LOCAL_ARRAY(char, host, MAX_URL);
185 LOCAL_ARRAY(char, request, MAX_URL);
186 LOCAL_ARRAY(char, password, MAX_URL);
187 t = sscanf(url, "cache_object://%[^/]/%[^@]@%s", host, request, password);
188
189 if (t < 2)
190 xstrncpy(request, "menu", MAX_URL);
191
192 #ifdef _SQUID_OS2_
193 if (t == 2 && request[0] == '\0') {
194 /*
195 * emx's sscanf insists of returning 2 because it sets request
196 * to null
197 */
198 xstrncpy(request, "menu", MAX_URL);
199 }
200 #endif
201
202 Mgr::ActionProfile::Pointer profile = findAction(request);
203 if (!profile) {
204 debugs(16, DBG_IMPORTANT, "CacheManager::ParseUrl: action '" << request << "' not found");
205 return NULL;
206 }
207
208 const char *prot = ActionProtection(profile);
209 if (!strcmp(prot, "disabled") || !strcmp(prot, "hidden")) {
210 debugs(16, DBG_IMPORTANT, "CacheManager::ParseUrl: action '" << request << "' is " << prot);
211 return NULL;
212 }
213
214 Mgr::Command::Pointer cmd = new Mgr::Command;
215 cmd->profile = profile;
216 cmd->params.httpUri = url;
217 cmd->params.userName = String();
218 cmd->params.password = t == 3 ? String(password) : String();
219 cmd->params.actionName = request;
220 return cmd;
221 }
222
223 /// \ingroup CacheManagerInternal
224 /*
225 \ingroup CacheManagerInternal
226 * Decodes the headers needed to perform user authentication and fills
227 * the details into the cachemgrStateData argument
228 */
229 void
230 CacheManager::ParseHeaders(const HttpRequest * request, Mgr::ActionParams &params)
231 {
232 assert(request);
233
234 params.httpMethod = request->method.id();
235 params.httpFlags = request->flags;
236
237 #if HAVE_AUTH_MODULE_BASIC
238 // TODO: use the authentication system decode to retrieve these details properly.
239
240 /* base 64 _decoded_ user:passwd pair */
241 const char *basic_cookie = request->header.getAuth(HDR_AUTHORIZATION, "Basic");
242
243 if (!basic_cookie)
244 return;
245
246 const char *passwd_del;
247 if (!(passwd_del = strchr(basic_cookie, ':'))) {
248 debugs(16, DBG_IMPORTANT, "CacheManager::ParseHeaders: unknown basic_cookie format '" << basic_cookie << "'");
249 return;
250 }
251
252 /* found user:password pair, reset old values */
253 params.userName.limitInit(basic_cookie, passwd_del - basic_cookie);
254 params.password = passwd_del + 1;
255
256 /* warning: this prints decoded password which maybe not be what you want to do @?@ @?@ */
257 debugs(16, 9, "CacheManager::ParseHeaders: got user: '" <<
258 params.userName << "' passwd: '" << params.password << "'");
259 #endif
260 }
261
262 /**
263 \ingroup CacheManagerInternal
264 *
265 \retval 0 if mgr->password is good or "none"
266 \retval 1 if mgr->password is "disable"
267 \retval !0 if mgr->password does not match configured password
268 */
269 int
270 CacheManager::CheckPassword(const Mgr::Command &cmd)
271 {
272 assert(cmd.profile != NULL);
273 const char *action = cmd.profile->name;
274 char *pwd = PasswdGet(Config.passwd_list, action);
275
276 debugs(16, 4, "CacheManager::CheckPassword for action " << action);
277
278 if (pwd == NULL)
279 return cmd.profile->isPwReq;
280
281 if (strcmp(pwd, "disable") == 0)
282 return 1;
283
284 if (strcmp(pwd, "none") == 0)
285 return 0;
286
287 if (!cmd.params.password.size())
288 return 1;
289
290 return cmd.params.password != pwd;
291 }
292
293 /**
294 \ingroup CacheManagerAPI
295 * Main entry point in the Cache Manager's activity. Gets called as part
296 * of the forward chain if the right URL is detected there. Initiates
297 * all needed internal work and renders the response.
298 */
299 void
300 CacheManager::Start(const Comm::ConnectionPointer &client, HttpRequest * request, StoreEntry * entry)
301 {
302 ErrorState *err = NULL;
303 debugs(16, 3, "CacheManager::Start: '" << entry->url() << "'" );
304
305 Mgr::Command::Pointer cmd = ParseUrl(entry->url());
306 if (!cmd) {
307 err = errorCon(ERR_INVALID_URL, HTTP_NOT_FOUND, request);
308 err->url = xstrdup(entry->url());
309 errorAppendEntry(entry, err);
310 entry->expires = squid_curtime;
311 return;
312 }
313
314 const char *actionName = cmd->profile->name;
315
316 entry->expires = squid_curtime;
317
318 debugs(16, 5, "CacheManager: " << client << " requesting '" << actionName << "'");
319
320 /* get additional info from request headers */
321 ParseHeaders(request, cmd->params);
322
323 const char *userName = cmd->params.userName.size() ?
324 cmd->params.userName.termedBuf() : "unknown";
325
326 /* Check password */
327
328 if (CheckPassword(*cmd) != 0) {
329 /* build error message */
330 ErrorState *errState;
331 HttpReply *rep;
332 errState = errorCon(ERR_CACHE_MGR_ACCESS_DENIED, HTTP_UNAUTHORIZED, request);
333 /* warn if user specified incorrect password */
334
335 if (cmd->params.password.size()) {
336 debugs(16, DBG_IMPORTANT, "CacheManager: " <<
337 userName << "@" <<
338 client << ": incorrect password for '" <<
339 actionName << "'" );
340 } else {
341 debugs(16, DBG_IMPORTANT, "CacheManager: " <<
342 userName << "@" <<
343 client << ": password needed for '" <<
344 actionName << "'" );
345 }
346
347 rep = errState->BuildHttpReply();
348
349 errorStateFree(errState);
350
351 #if HAVE_AUTH_MODULE_BASIC
352 /*
353 * add Authenticate header using action name as a realm because
354 * password depends on the action
355 */
356 rep->header.putAuth("Basic", actionName);
357 #endif
358
359 /* store the reply */
360 entry->replaceHttpReply(rep);
361
362 entry->expires = squid_curtime;
363
364 entry->complete();
365
366 return;
367 }
368
369 debugs(16, 2, "CacheManager: " <<
370 userName << "@" <<
371 client << " requesting '" <<
372 actionName << "'" );
373
374 if (UsingSmp() && IamWorkerProcess()) {
375 AsyncJob::Start(new Mgr::Forwarder(client, cmd->params, request, entry));
376 return;
377 }
378
379 Mgr::Action::Pointer action = cmd->profile->creator->create(cmd);
380 Must(action != NULL);
381 action->run(entry, true);
382 }
383
384 /*
385 \ingroup CacheManagerInternal
386 * Renders the protection level text for an action.
387 * Also doubles as a check for the protection level.
388 */
389 const char *
390 CacheManager::ActionProtection(const Mgr::ActionProfile::Pointer &profile)
391 {
392 assert(profile != NULL);
393 const char *pwd = PasswdGet(Config.passwd_list, profile->name);
394
395 if (!pwd)
396 return profile->isPwReq ? "hidden" : "public";
397
398 if (!strcmp(pwd, "disable"))
399 return "disabled";
400
401 if (strcmp(pwd, "none") == 0)
402 return "public";
403
404 return "protected";
405 }
406
407 /*
408 \ingroup CacheManagerInternal
409 * gets from the global Config the password the user would need to supply
410 * for the action she queried
411 */
412 char *
413 CacheManager::PasswdGet(cachemgr_passwd * a, const char *action)
414 {
415 wordlist *w;
416
417 while (a != NULL) {
418 for (w = a->actions; w != NULL; w = w->next) {
419 if (0 == strcmp(w->key, action))
420 return a->passwd;
421
422 if (0 == strcmp(w->key, "all"))
423 return a->passwd;
424 }
425
426 a = a->next;
427 }
428
429 return NULL;
430 }
431
432 CacheManager* CacheManager::instance=0;
433
434 /**
435 \ingroup CacheManagerAPI
436 * Singleton accessor method.
437 */
438 CacheManager*
439 CacheManager::GetInstance()
440 {
441 if (instance == 0) {
442 debugs(16, 6, "CacheManager::GetInstance: starting cachemanager up");
443 instance = new CacheManager;
444 Mgr::RegisterBasics();
445 }
446 return instance;
447 }