]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - src/cache_manager.cc
Renamed squid.h to squid-old.h and config.h to squid.h
[thirdparty/squid.git] / src / cache_manager.cc
index 38ee8cddfe90053442c3764dcd29284f993cd6be..b45cf50c2e2371ffc075ba737d1eb85594c6a5b6 100644 (file)
@@ -33,7 +33,7 @@
  *
  */
 
-#include "config.h"
+#include "squid.h"
 #include "base/TextException.h"
 #include "CacheManager.h"
 #include "comm/Connection.h"
@@ -49,8 +49,8 @@
 #include "mgr/Command.h"
 #include "mgr/Forwarder.h"
 #include "mgr/FunAction.h"
-/* for rotate_logs() */
-#include "protos.h"
+#include "mgr/QueryParams.h"
+#include "protos.h" /* rotate_logs() */
 #include "SquidTime.h"
 #include "Store.h"
 #include "wordlist.h"
@@ -83,7 +83,7 @@ void
 CacheManager::registerProfile(const Mgr::ActionProfile::Pointer &profile)
 {
     Must(profile != NULL);
-    if (find(menu_.begin(), menu_.end(), profile) == menu_.end()) {
+    if (std::find(menu_.begin(), menu_.end(), profile) == menu_.end()) {
         menu_.push_back(profile);
         debugs(16, 3, HERE << "registered profile: " << *profile);
     } else {
@@ -184,21 +184,40 @@ CacheManager::ParseUrl(const char *url)
     LOCAL_ARRAY(char, host, MAX_URL);
     LOCAL_ARRAY(char, request, MAX_URL);
     LOCAL_ARRAY(char, password, MAX_URL);
-    t = sscanf(url, "cache_object://%[^/]/%[^@]@%s", host, request, password);
-
+    LOCAL_ARRAY(char, params, MAX_URL);
+    host[0] = 0;
+    request[0] = 0;
+    password[0] = 0;
+    params[0] = 0;
+    int pos = -1;
+    int len = strlen(url);
+    Must(len > 0);
+    t = sscanf(url, "cache_object://%[^/]/%[^@?]%n@%[^?]?%s", host, request, &pos, password, 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)
-        xstrncpy(request, "menu", MAX_URL);
+        xstrncpy(request, "index", 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);
+        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");
@@ -212,10 +231,12 @@ CacheManager::ParseUrl(const char *url)
     }
 
     Mgr::Command::Pointer cmd = new Mgr::Command;
+    if (!Mgr::QueryParams::Parse(params, cmd->params.queryParams))
+        return NULL;
     cmd->profile = profile;
     cmd->params.httpUri = url;
     cmd->params.userName = String();
-    cmd->params.password = t == 3 ? String(password) : String();
+    cmd->params.password = password;
     cmd->params.actionName = request;
     return cmd;
 }
@@ -229,18 +250,21 @@ CacheManager::ParseUrl(const char *url)
 void
 CacheManager::ParseHeaders(const HttpRequest * request, Mgr::ActionParams &params)
 {
-    const char *basic_cookie;  /* base 64 _decoded_ user:passwd pair */
-    const char *passwd_del;
     assert(request);
 
     params.httpMethod = request->method.id();
     params.httpFlags = request->flags;
 
-    basic_cookie = request->header.getAuth(HDR_AUTHORIZATION, "Basic");
+#if HAVE_AUTH_MODULE_BASIC
+    // 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");
 
     if (!basic_cookie)
         return;
 
+    const char *passwd_del;
     if (!(passwd_del = strchr(basic_cookie, ':'))) {
         debugs(16, DBG_IMPORTANT, "CacheManager::ParseHeaders: unknown basic_cookie format '" << basic_cookie << "'");
         return;
@@ -250,9 +274,10 @@ CacheManager::ParseHeaders(const HttpRequest * request, Mgr::ActionParams &param
     params.userName.limitInit(basic_cookie, passwd_del - basic_cookie);
     params.password = passwd_del + 1;
 
-    /* warning: this prints decoded password which maybe not what you want to do @?@ @?@ */
+    /* warning: this prints decoded password which maybe not be what you want to do @?@ @?@ */
     debugs(16, 9, "CacheManager::ParseHeaders: got user: '" <<
            params.userName << "' passwd: '" << params.password << "'");
+#endif
 }
 
 /**
@@ -295,12 +320,11 @@ CacheManager::CheckPassword(const Mgr::Command &cmd)
 void
 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_NOT_FOUND, request);
         err->url = xstrdup(entry->url());
         errorAppendEntry(entry, err);
         entry->expires = squid_curtime;
@@ -323,9 +347,7 @@ CacheManager::Start(const Comm::ConnectionPointer &client, HttpRequest * request
 
     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_UNAUTHORIZED, request);
         /* warn if user specified incorrect password */
 
         if (cmd->params.password.size()) {
@@ -340,15 +362,23 @@ CacheManager::Start(const Comm::ConnectionPointer &client, HttpRequest * request
                    actionName << "'" );
         }
 
-        rep = errState->BuildHttpReply();
-
-        errorStateFree(errState);
+        HttpReply *rep = errState.BuildHttpReply();
 
+#if HAVE_AUTH_MODULE_BASIC
         /*
          * add Authenticate header using action name as a realm because
          * password depends on the action
          */
         rep->header.putAuth("Basic", actionName);
+#endif
+        // Allow cachemgr and other XHR scripts access to our version string
+        if (request->header.has(HDR_ORIGIN)) {
+            rep->header.putExt("Access-Control-Allow-Origin",request->header.getStr(HDR_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);
@@ -360,12 +390,37 @@ CacheManager::Start(const Comm::ConnectionPointer &client, HttpRequest * request
         return;
     }
 
+    if (request->header.has(HDR_ORIGIN)) {
+        cmd->params.httpOrigin = request->header.getStr(HDR_ORIGIN);
+    }
+
     debugs(16, 2, "CacheManager: " <<
            userName << "@" <<
            client << " requesting '" <<
            actionName << "'" );
 
+    // special case: /squid-internal-mgr/ index page
+    if (!strcmp(cmd->profile->name, "index")) {
+        ErrorState err(MGR_INDEX, HTTP_OK, request);
+        err.url = xstrdup(entry->url());
+        HttpReply *rep = err.BuildHttpReply();
+        if (strncmp(rep->body.content(),"Internal Error:", 15) == 0)
+            rep->sline.status = HTTP_NOT_FOUND;
+        // Allow cachemgr and other XHR scripts access to our version string
+        if (request->header.has(HDR_ORIGIN)) {
+            rep->header.putExt("Access-Control-Allow-Origin",request->header.getStr(HDR_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()) {
+        // is client the right connection to pass here?
         AsyncJob::Start(new Mgr::Forwarder(client, cmd->params, request, entry));
         return;
     }