]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - src/adaptation/ServiceConfig.cc
Docs: Copyright updates for 2018 (#114)
[thirdparty/squid.git] / src / adaptation / ServiceConfig.cc
index 4e4dd5d52939c413d37bf4d375c98a2d65f5de94..3c868f824057bba4fcf5c0d546e22d724e755d35 100644 (file)
@@ -1,14 +1,25 @@
 /*
- * DEBUG: section XXX
+ * 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.
  */
 
+/* DEBUG: section 93    Adaptation */
+
 #include "squid.h"
-#include "ConfigParser.h"
 #include "adaptation/ServiceConfig.h"
+#include "ConfigParser.h"
+#include "Debug.h"
+#include "globals.h"
+#include "ip/tools.h"
+#include <set>
 
 Adaptation::ServiceConfig::ServiceConfig():
-        port(-1), method(methodNone), point(pointNone),
-        bypass(false), routing(false)
+    port(-1), method(methodNone), point(pointNone),
+    bypass(false), maxConn(-1), onOverload(srvWait),
+    routing(false), ipv6(false)
 {}
 
 const char *
@@ -44,10 +55,10 @@ Adaptation::ServiceConfig::parseVectPoint(const char *service_configConfig) cons
     if (q)
         t = q + 1;
 
-    if (!strcasecmp(t, "precache"))
+    if (!strcmp(t, "precache"))
         return Adaptation::pointPreCache;
 
-    if (!strcasecmp(t, "postcache"))
+    if (!strcmp(t, "postcache"))
         return Adaptation::pointPostCache;
 
     return Adaptation::pointNone;
@@ -56,59 +67,112 @@ Adaptation::ServiceConfig::parseVectPoint(const char *service_configConfig) cons
 bool
 Adaptation::ServiceConfig::parse()
 {
-    char *method_point = NULL;
+    key = ConfigParser::NextToken();
+    String method_point = ConfigParser::NextToken();
+    if (!method_point.size()) {
+        debugs(3, DBG_CRITICAL, "ERROR: " << cfg_filename << ':' << config_lineno << ": " <<
+               "Missing vectoring point in adaptation service definition");
+        return false;
+    }
 
-    ConfigParser::ParseString(&key);
-    ConfigParser::ParseString(&method_point);
-    method = parseMethod(method_point);
-    point = parseVectPoint(method_point);
+    method = parseMethod(method_point.termedBuf());
+    point = parseVectPoint(method_point.termedBuf());
+    if (method == Adaptation::methodNone && point == Adaptation::pointNone) {
+        debugs(3, DBG_CRITICAL, "ERROR: " << cfg_filename << ':' << config_lineno << ": " <<
+               "Unknown vectoring point '" << method_point << "' in adaptation service definition");
+        return false;
+    }
 
     // reset optional parameters in case we are reconfiguring
     bypass = routing = false;
 
     // handle optional service name=value parameters
-    const char *lastOption = NULL;
-    while (char *option = strtok(NULL, w_space)) {
+    bool grokkedUri = false;
+    bool onOverloadSet = false;
+    std::set<std::string> options;
+
+    while (char *option = ConfigParser::NextToken()) {
+        const char *name = option;
+        const char *value = "";
         if (strcmp(option, "0") == 0) { // backward compatibility
-            bypass = false;
-            continue;
-        }
-        if (strcmp(option, "1") == 0) { // backward compatibility
-            bypass = true;
-            continue;
+            name = "bypass";
+            value = "off";
+            debugs(3, DBG_PARSE_NOTE(DBG_IMPORTANT), "UPGRADE: Please use 'bypass=off' option to disable service bypass");
+        }  else if (strcmp(option, "1") == 0) { // backward compatibility
+            name = "bypass";
+            value = "on";
+            debugs(3, DBG_PARSE_NOTE(DBG_IMPORTANT), "UPGRADE: Please use 'bypass=on' option to enable service bypass");
+        } else {
+            char *eq = strstr(option, "=");
+            const char *sffx = strstr(option, "://");
+            if (!eq || (sffx && sffx < eq)) { //no "=" or has the form "icap://host?arg=val"
+                name = "uri";
+                value = option;
+            }  else { // a normal name=value option
+                *eq = '\0'; // terminate option name
+                value = eq + 1; // skip '='
+            }
         }
 
-        const char *name = option;
-        char *value = strstr(option, "=");
-        if (!value) {
-            lastOption = option;
-            break;
+        // Check if option is set twice
+        if (options.find(name) != options.end()) {
+            debugs(3, DBG_CRITICAL, "ERROR: " << cfg_filename << ':' << config_lineno << ": " <<
+                   "Duplicate option \"" << name << "\" in adaptation service definition");
+            return false;
         }
-        *value = '\0'; // terminate option name
-        ++value; // skip '='
+        options.insert(name);
 
-        // TODO: warn if option is set twice?
         bool grokked = false;
-        if (strcmp(name, "bypass") == 0)
+        if (strcmp(name, "bypass") == 0) {
             grokked = grokBool(bypass, name, value);
-        else if (strcmp(name, "routing") == 0)
+        else if (strcmp(name, "routing") == 0)
             grokked = grokBool(routing, name, value);
-        else {
-            debugs(3, 0, cfg_filename << ':' << config_lineno << ": " <<
-                   "unknown adaptation service option: " << name << '=' << value);
-        }
+        else if (strcmp(name, "uri") == 0)
+            grokked = grokkedUri = grokUri(value);
+        else if (strcmp(name, "ipv6") == 0) {
+            grokked = grokBool(ipv6, name, value);
+            if (grokked && ipv6 && !Ip::EnableIpv6)
+                debugs(3, DBG_PARSE_NOTE(DBG_IMPORTANT), "WARNING: IPv6 is disabled. ICAP service option ignored.");
+        } else if (strcmp(name, "max-conn") == 0)
+            grokked = grokLong(maxConn, name, value);
+        else if (strcmp(name, "on-overload") == 0) {
+            grokked = grokOnOverload(onOverload, value);
+            onOverloadSet = true;
+        } else if (strcmp(name, "connection-encryption") == 0) {
+            bool encrypt = false;
+            grokked = grokBool(encrypt, name, value);
+            connectionEncryption.configure(encrypt);
+        } else if (strncmp(name, "ssl", 3) == 0 || strncmp(name, "tls-", 4) == 0) {
+#if !USE_OPENSSL
+            debugs(3, DBG_PARSE_NOTE(DBG_IMPORTANT), "WARNING: adaptation option '" << name << "' requires --with-openssl. ICAP service option ignored.");
+#else
+            // name prefix is "ssl" or "tls-"
+            std::string tmp = name + (name[0] == 's' ? 3 : 4);
+            tmp += "=";
+            tmp += value;
+            secure.parse(tmp.c_str());
+            grokked = true;
+#endif
+        } else
+            grokked = grokExtension(name, value);
+
         if (!grokked)
             return false;
     }
 
-    // what is left must be the service URI
-    if (!grokUri(lastOption))
-        return false;
+    // set default on-overload value if needed
+    if (!onOverloadSet)
+        onOverload = bypass ? srvBypass : srvWait;
+
+    // disable the TLS NPN extension if encrypted.
+    // Squid advertises "http/1.1", which is wrong for ICAPS.
+    if (secure.encryptTransport)
+        secure.parse("no-npn");
 
-    // there should be nothing else left
-    if (const char *tail = strtok(NULL, w_space)) {
-        debugs(3, 0, cfg_filename << ':' << config_lineno << ": " <<
-               "garbage after adaptation service URI: " << tail);
+    // is the service URI set?
+    if (!grokkedUri) {
+        debugs(3, DBG_CRITICAL, "ERROR: " << cfg_filename << ':' << config_lineno << ": " <<
+               "No \"uri\" option in adaptation service definition");
         return false;
     }
 
@@ -125,10 +189,10 @@ bool
 Adaptation::ServiceConfig::grokUri(const char *value)
 {
     // TODO: find core code that parses URLs and extracts various parts
-    // AYJ: most of this is duplicate of urlParse() in src/url.cc
+    // AYJ: most of this is duplicate of URL::parse() in src/url.cc
 
     if (!value || !*value) {
-        debugs(3, 0, HERE << cfg_filename << ':' << config_lineno << ": " <<
+        debugs(3, DBG_CRITICAL, HERE << cfg_filename << ':' << config_lineno << ": " <<
                "empty adaptation service URI");
         return false;
     }
@@ -160,7 +224,7 @@ Adaptation::ServiceConfig::grokUri(const char *value)
         if ((t = strchr(s, ']')) == NULL)
             return false;
 
-        s++;
+        ++s;
         len = t - s;
         if ((e = strchr(t, ':')) != NULL) {
             have_port = true;
@@ -169,8 +233,7 @@ Adaptation::ServiceConfig::grokUri(const char *value)
         } else {
             return false;
         }
-    }
-    else {
+    } else {
         if ((e = strchr(s, ':')) != NULL) {
             have_port = true;
         } else if ((e = strchr(s, '/')) != NULL) {
@@ -182,11 +245,15 @@ Adaptation::ServiceConfig::grokUri(const char *value)
     }
 
     host.limitInit(s, len);
+#if USE_OPENSSL
+    if (secure.sslDomain.isEmpty())
+        secure.sslDomain.assign(host.rawBuf(), host.size());
+#endif
     s = e;
 
     port = -1;
     if (have_port) {
-        s++;
+        ++s;
 
         if ((e = strchr(s, '/')) != NULL) {
             char *t;
@@ -209,12 +276,12 @@ Adaptation::ServiceConfig::grokUri(const char *value)
 
     // if no port, the caller may use service_configConfigs or supply the default if neeeded
 
-    s++;
+    ++s;
     e = strchr(s, '\0');
     len = e - s;
 
     if (len > 1024) {
-        debugs(3, 0, HERE << cfg_filename << ':' << config_lineno << ": " <<
+        debugs(3, DBG_CRITICAL, HERE << cfg_filename << ':' << config_lineno << ": " <<
                "long resource name (>1024), probably wrong");
     }
 
@@ -222,7 +289,6 @@ Adaptation::ServiceConfig::grokUri(const char *value)
     return true;
 }
 
-
 bool
 Adaptation::ServiceConfig::grokBool(bool &var, const char *name, const char *value)
 {
@@ -231,7 +297,7 @@ Adaptation::ServiceConfig::grokBool(bool &var, const char *name, const char *val
     else if (!strcmp(value, "1") || !strcmp(value, "on"))
         var = true;
     else {
-        debugs(3, 0, HERE << cfg_filename << ':' << config_lineno << ": " <<
+        debugs(3, DBG_CRITICAL, HERE << cfg_filename << ':' << config_lineno << ": " <<
                "wrong value for boolean " << name << "; " <<
                "'0', '1', 'on', or 'off' expected but got: " << value);
         return false;
@@ -239,3 +305,49 @@ Adaptation::ServiceConfig::grokBool(bool &var, const char *name, const char *val
 
     return true;
 }
+
+bool
+Adaptation::ServiceConfig::grokLong(long &var, const char *name, const char *value)
+{
+    char *bad = NULL;
+    const long p = strtol(value, &bad, 0);
+    if (p < 0 || bad == value) {
+        debugs(3, DBG_CRITICAL, "ERROR: " << cfg_filename << ':' <<
+               config_lineno << ": " << "wrong value for " << name << "; " <<
+               "a non-negative integer expected but got: " << value);
+        return false;
+    }
+    var = p;
+    return true;
+}
+
+bool
+Adaptation::ServiceConfig::grokOnOverload(SrvBehaviour &var, const char *value)
+{
+    if (strcmp(value, "block") == 0)
+        var = srvBlock;
+    else if (strcmp(value, "bypass") == 0)
+        var = srvBypass;
+    else if (strcmp(value, "wait") == 0)
+        var = srvWait;
+    else if (strcmp(value, "force") == 0)
+        var = srvForce;
+    else {
+        debugs(3, DBG_CRITICAL, "ERROR: " << cfg_filename << ':' <<
+               config_lineno << ": " << "wrong value for on-overload; " <<
+               "'block', 'bypass', 'wait' or 'force' expected but got: " << value);
+        return false;
+    }
+    return true;
+}
+
+bool
+Adaptation::ServiceConfig::grokExtension(const char *name, const char *value)
+{
+    // we do not accept extensions by default
+    debugs(3, DBG_CRITICAL, cfg_filename << ':' << config_lineno << ": " <<
+           "ERROR: unknown adaptation service option: " <<
+           name << '=' << value);
+    return false;
+}
+