]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - src/cache_manager.cc
Docs: Copyright updates for 2018 (#114)
[thirdparty/squid.git] / src / cache_manager.cc
index 7b23c766ceb1d421dd69f37f5f548b1c015deac1..da22f7a84439c83b103c111bdbba9bbe95a6d4b7 100644 (file)
@@ -1,60 +1,39 @@
-
 /*
- * $Id$
- *
- * DEBUG: section 16    Cache Manager Objects
- * AUTHOR: Duane Wessels
- *
- * SQUID Web Proxy Cache          http://www.squid-cache.org/
- * ----------------------------------------------------------
- *
- *  Squid is the result of efforts by numerous individuals from
- *  the Internet community; see the CONTRIBUTORS file for full
- *  details.   Many organizations have provided support for Squid's
- *  development; see the SPONSORS file for full details.  Squid is
- *  Copyrighted (C) 2001 by the Regents of the University of
- *  California; see the COPYRIGHT file for full details.  Squid
- *  incorporates software developed and/or copyrighted by other
- *  sources; see the CREDITS file for full details.
- *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
- *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
+ * Copyright (C) 1996-2018 The Squid Software Foundation and contributors
  *
+ * Squid software is distributed under GPLv2+ license and includes
+ * contributions from numerous individuals and organizations.
+ * Please see the COPYING and CONTRIBUTORS files for details.
  */
 
-#include "config.h"
+/* DEBUG: section 16    Cache Manager Objects */
+
+#include "squid.h"
 #include "base/TextException.h"
 #include "CacheManager.h"
+#include "comm/Connection.h"
 #include "Debug.h"
 #include "errorpage.h"
 #include "fde.h"
 #include "HttpReply.h"
 #include "HttpRequest.h"
-#include "mgr/ActionCreator.h"
 #include "mgr/Action.h"
+#include "mgr/ActionCreator.h"
+#include "mgr/ActionPasswordList.h"
 #include "mgr/ActionProfile.h"
 #include "mgr/BasicActions.h"
 #include "mgr/Command.h"
 #include "mgr/Forwarder.h"
 #include "mgr/FunAction.h"
 #include "mgr/QueryParams.h"
-#include "protos.h" /* rotate_logs() */
+#include "protos.h"
+#include "SquidConfig.h"
 #include "SquidTime.h"
 #include "Store.h"
+#include "tools.h"
 #include "wordlist.h"
-#include <algorithm>
 
+#include <algorithm>
 
 /// \ingroup CacheManagerInternal
 #define MGR_PASSWD_SZ 128
@@ -76,13 +55,12 @@ private:
     Handler *handler;
 };
 
-
 /// Registers new profiles, ignoring attempts to register a duplicate
 void
 CacheManager::registerProfile(const Mgr::ActionProfile::Pointer &profile)
 {
     Must(profile != NULL);
-    if (std::find(menu_.begin(), menu_.end(), profile) == menu_.end()) {
+    if (!CacheManager::findAction(profile->name)) {
         menu_.push_back(profile);
         debugs(16, 3, HERE << "registered profile: " << *profile);
     } else {
@@ -105,6 +83,12 @@ CacheManager::registerProfile(char const * action, char const * desc, OBJH * han
     registerProfile(profile);
 }
 
+/**
+ * \ingroup CacheManagerAPI
+ * Registers a C++-style action, via a pointer to a subclass of
+ * a CacheManagerAction object, whose run() method will be invoked when
+ * CacheManager identifies that the user has requested the action.
+ */
 void
 CacheManager::registerProfile(char const * action, char const * desc,
                               ClassActionCreator::Handler *handler,
@@ -186,26 +170,38 @@ CacheManager::ParseUrl(const char *url)
     int len = strlen(url);
     Must(len > 0);
     t = sscanf(url, "cache_object://%[^/]/%[^@?]%n@%[^?]?%s", host, request, &pos, password, params);
-
-    if (pos >0 && url[pos] == '?') {
-        ++pos;
-        if (pos < len)
-            xstrncpy(params, url + pos, sizeof(params));
+    if (t < 3) {
+        t = sscanf(url, "cache_object://%[^/]/%[^?]%n?%s", host, request, &pos, params);
+    }
+    if (t < 1) {
+        t = sscanf(url, "http://%[^/]/squid-internal-mgr/%[^?]%n?%s", host, request, &pos, params);
+    }
+    if (t < 1) {
+        t = sscanf(url, "https://%[^/]/squid-internal-mgr/%[^?]%n?%s", host, request, &pos, params);
+    }
+    if (t < 2) {
+        if (strncmp("cache_object://",url,15)==0)
+            xstrncpy(request, "menu", MAX_URL);
+        else
+            xstrncpy(request, "index", MAX_URL);
     }
 
-    if (t < 2)
-        xstrncpy(request, "menu", MAX_URL);
-
-#ifdef _SQUID_OS2_
+#if _SQUID_OS2_
     if (t == 2 && request[0] == '\0') {
         /*
          * emx's sscanf insists of returning 2 because it sets request
          * to null
          */
-        xstrncpy(request, "menu", MAX_URL);
+        if (strncmp("cache_object://",url,15)==0)
+            xstrncpy(request, "menu", MAX_URL);
+        else
+            xstrncpy(request, "index", MAX_URL);
     }
 #endif
 
+    debugs(16, 3, HERE << "MGR request: t=" << t << ", host='" << host << "', request='" << request << "', pos=" << pos <<
+           ", password='" << password << "', params='" << params << "'");
+
     Mgr::ActionProfile::Pointer profile = findAction(request);
     if (!profile) {
         debugs(16, DBG_IMPORTANT, "CacheManager::ParseUrl: action '" << request << "' not found");
@@ -247,7 +243,7 @@ CacheManager::ParseHeaders(const HttpRequest * request, Mgr::ActionParams &param
     // TODO: use the authentication system decode to retrieve these details properly.
 
     /* base 64 _decoded_ user:passwd pair */
-    const char *basic_cookie = request->header.getAuth(HDR_AUTHORIZATION, "Basic");
+    const char *basic_cookie = request->header.getAuth(Http::HdrType::AUTHORIZATION, "Basic");
 
     if (!basic_cookie)
         return;
@@ -271,9 +267,9 @@ CacheManager::ParseHeaders(const HttpRequest * request, Mgr::ActionParams &param
 /**
  \ingroup CacheManagerInternal
  *
- \retval 0     if mgr->password is good or "none"
- \retval 1     if mgr->password is "disable"
- \retval !0    if mgr->password does not match configured password
+ \retval 0  if mgr->password is good or "none"
+ \retval 1  if mgr->password is "disable"
+ \retval !0 if mgr->password does not match configured password
  */
 int
 CacheManager::CheckPassword(const Mgr::Command &cmd)
@@ -306,14 +302,13 @@ CacheManager::CheckPassword(const Mgr::Command &cmd)
  * all needed internal work and renders the response.
  */
 void
-CacheManager::Start(int fd, HttpRequest * request, StoreEntry * entry)
+CacheManager::Start(const Comm::ConnectionPointer &client, HttpRequest * request, StoreEntry * entry)
 {
-    ErrorState *err = NULL;
     debugs(16, 3, "CacheManager::Start: '" << entry->url() << "'" );
 
     Mgr::Command::Pointer cmd = ParseUrl(entry->url());
     if (!cmd) {
-        err = errorCon(ERR_INVALID_URL, HTTP_NOT_FOUND, request);
+        ErrorState *err = new ErrorState(ERR_INVALID_URL, Http::scNotFound, request);
         err->url = xstrdup(entry->url());
         errorAppendEntry(entry, err);
         entry->expires = squid_curtime;
@@ -324,7 +319,7 @@ CacheManager::Start(int fd, HttpRequest * request, StoreEntry * entry)
 
     entry->expires = squid_curtime;
 
-    debugs(16, 5, "CacheManager: " << fd_table[fd].ipaddr << " requesting '" << actionName << "'");
+    debugs(16, 5, "CacheManager: " << client << " requesting '" << actionName << "'");
 
     /* get additional info from request headers */
     ParseHeaders(request, cmd->params);
@@ -336,26 +331,22 @@ CacheManager::Start(int fd, HttpRequest * request, StoreEntry * entry)
 
     if (CheckPassword(*cmd) != 0) {
         /* build error message */
-        ErrorState *errState;
-        HttpReply *rep;
-        errState = errorCon(ERR_CACHE_MGR_ACCESS_DENIED, HTTP_UNAUTHORIZED, request);
+        ErrorState errState(ERR_CACHE_MGR_ACCESS_DENIED, Http::scUnauthorized, request);
         /* warn if user specified incorrect password */
 
         if (cmd->params.password.size()) {
             debugs(16, DBG_IMPORTANT, "CacheManager: " <<
                    userName << "@" <<
-                   fd_table[fd].ipaddr << ": incorrect password for '" <<
+                   client << ": incorrect password for '" <<
                    actionName << "'" );
         } else {
             debugs(16, DBG_IMPORTANT, "CacheManager: " <<
                    userName << "@" <<
-                   fd_table[fd].ipaddr << ": password needed for '" <<
+                   client << ": password needed for '" <<
                    actionName << "'" );
         }
 
-        rep = errState->BuildHttpReply();
-
-        errorStateFree(errState);
+        HttpReply *rep = errState.BuildHttpReply();
 
 #if HAVE_AUTH_MODULE_BASIC
         /*
@@ -364,6 +355,14 @@ CacheManager::Start(int fd, HttpRequest * request, StoreEntry * entry)
          */
         rep->header.putAuth("Basic", actionName);
 #endif
+        // Allow cachemgr and other XHR scripts access to our version string
+        if (request->header.has(Http::HdrType::ORIGIN)) {
+            rep->header.putExt("Access-Control-Allow-Origin",request->header.getStr(Http::HdrType::ORIGIN));
+#if HAVE_AUTH_MODULE_BASIC
+            rep->header.putExt("Access-Control-Allow-Credentials","true");
+#endif
+            rep->header.putExt("Access-Control-Expose-Headers","Server");
+        }
 
         /* store the reply */
         entry->replaceHttpReply(rep);
@@ -375,13 +374,38 @@ CacheManager::Start(int fd, HttpRequest * request, StoreEntry * entry)
         return;
     }
 
+    if (request->header.has(Http::HdrType::ORIGIN)) {
+        cmd->params.httpOrigin = request->header.getStr(Http::HdrType::ORIGIN);
+    }
+
     debugs(16, 2, "CacheManager: " <<
            userName << "@" <<
-           fd_table[fd].ipaddr << " requesting '" <<
+           client << " requesting '" <<
            actionName << "'" );
 
+    // special case: /squid-internal-mgr/ index page
+    if (!strcmp(cmd->profile->name, "index")) {
+        ErrorState err(MGR_INDEX, Http::scOkay, request);
+        err.url = xstrdup(entry->url());
+        HttpReply *rep = err.BuildHttpReply();
+        if (strncmp(rep->body.content(),"Internal Error:", 15) == 0)
+            rep->sline.set(Http::ProtocolVersion(1,1), Http::scNotFound);
+        // Allow cachemgr and other XHR scripts access to our version string
+        if (request->header.has(Http::HdrType::ORIGIN)) {
+            rep->header.putExt("Access-Control-Allow-Origin",request->header.getStr(Http::HdrType::ORIGIN));
+#if HAVE_AUTH_MODULE_BASIC
+            rep->header.putExt("Access-Control-Allow-Credentials","true");
+#endif
+            rep->header.putExt("Access-Control-Expose-Headers","Server");
+        }
+        entry->replaceHttpReply(rep);
+        entry->complete();
+        return;
+    }
+
     if (UsingSmp() && IamWorkerProcess()) {
-        AsyncJob::Start(new Mgr::Forwarder(fd, cmd->params, request, entry));
+        // is client the right connection to pass here?
+        AsyncJob::Start(new Mgr::Forwarder(client, cmd->params, request, entry));
         return;
     }
 
@@ -414,21 +438,20 @@ CacheManager::ActionProtection(const Mgr::ActionProfile::Pointer &profile)
 }
 
 /*
- \ingroup CacheManagerInternal
\ingroup CacheManagerInternal
  * gets from the global Config the password the user would need to supply
  * for the action she queried
  */
 char *
-CacheManager::PasswdGet(cachemgr_passwd * a, const char *action)
+CacheManager::PasswdGet(Mgr::ActionPasswordList * a, const char *action)
 {
-    wordlist *w;
-
-    while (a != NULL) {
-        for (w = a->actions; w != NULL; w = w->next) {
-            if (0 == strcmp(w->key, action))
+    while (a) {
+        for (auto &w : a->actions) {
+            if (w.cmp(action) == 0)
                 return a->passwd;
 
-            if (0 == strcmp(w->key, "all"))
+            static const SBuf allAction("all");
+            if (w == allAction)
                 return a->passwd;
         }
 
@@ -438,19 +461,15 @@ CacheManager::PasswdGet(cachemgr_passwd * a, const char *action)
     return NULL;
 }
 
-CacheManager* CacheManager::instance=0;
-
-/**
- \ingroup CacheManagerAPI
- * Singleton accessor method.
- */
 CacheManager*
 CacheManager::GetInstance()
 {
-    if (instance == 0) {
-        debugs(16, 6, "CacheManager::GetInstance: starting cachemanager up");
+    static CacheManager *instance = nullptr;
+    if (!instance) {
+        debugs(16, 6, "starting cachemanager up");
         instance = new CacheManager;
         Mgr::RegisterBasics();
     }
     return instance;
 }
+