]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - src/acl/Acl.cc
Upgrade Acl::Node::name to SBuf; remove AclMatchedName global (#1766)
[thirdparty/squid.git] / src / acl / Acl.cc
index d2e9c5faec40e74528230945470954e3a85221a1..b08ab442e5db6765532e5f57dc1df0151e81fee9 100644 (file)
@@ -27,8 +27,6 @@
 #include <algorithm>
 #include <map>
 
-const char *AclMatchedName = nullptr;
-
 namespace Acl {
 
 /// Acl::Node type name comparison functor
@@ -115,6 +113,14 @@ Acl::SetKey(SBuf &keyStorage, const char *keyParameterName, const char *newKey)
                         Here());
 }
 
+const SBuf &
+Acl::Answer::lastCheckDescription() const
+{
+    static const auto none = new SBuf("[no-ACL]");
+    // no value_or() because it would create a new SBuf object here
+    return lastCheckedName ? *lastCheckedName : *none;
+}
+
 /* Acl::ParsingContext */
 
 ScopedId
@@ -143,27 +149,39 @@ void Acl::Node::operator delete(void *)
     fatal ("unusable Acl::Node::delete");
 }
 
-Acl::Node *
-Acl::Node::FindByName(const char *name)
+/// implements both Acl::Node::FindByName() variations
+template <typename SBufOrCString>
+static Acl::Node *
+FindByNameT(const SBufOrCString name)
 {
-    debugs(28, 9, "name=" << name);
-
-    for (auto *a = Config.aclList; a; a = a->next)
-        if (!strcasecmp(a->name, name))
+    for (auto a = Config.aclList; a; a = a->next) {
+        if (a->name.caseCmp(name) == 0) {
+            debugs(28, 8, "found " << a->name);
             return a;
+        }
+    }
 
-    debugs(28, 9, "found no match");
-
+    debugs(28, 8, "cannot find " << name);
     return nullptr;
 }
 
+Acl::Node *
+Acl::Node::FindByName(const SBuf &name)
+{
+    return FindByNameT(name);
+}
+
+Acl::Node *
+Acl::Node::FindByName(const char *name)
+{
+    return FindByNameT(name);
+}
+
 Acl::Node::Node() :
     cfgline(nullptr),
     next(nullptr),
     registered(false)
-{
-    *name = 0;
-}
+{}
 
 bool
 Acl::Node::valid() const
@@ -176,10 +194,7 @@ Acl::Node::matches(ACLChecklist *checklist) const
 {
     debugs(28, 5, "checking " << name);
 
-    // XXX: AclMatchedName does not contain a matched ACL name when the acl
-    // does not match. It contains the last (usually leaf) ACL name checked
-    // (or is NULL if no ACLs were checked).
-    AclMatchedName = name;
+    checklist->setLastCheckedName(name);
 
     int result = 0;
     if (!checklist->hasAle() && requiresAle()) {
@@ -206,11 +221,9 @@ Acl::Node::matches(ACLChecklist *checklist) const
 }
 
 void
-Acl::Node::context(const char *aName, const char *aCfgLine)
+Acl::Node::context(const SBuf &aName, const char *aCfgLine)
 {
-    name[0] = '\0';
-    if (aName)
-        xstrncpy(name, aName, ACL_NAME_SZ-1);
+    name = aName;
     safe_free(cfgline);
     if (aCfgLine)
         cfgline = xstrdup(aCfgLine);
@@ -230,22 +243,15 @@ Acl::Node::ParseAclLine(ConfigParser &parser, Node ** head)
         return;
     }
 
-    if (strlen(t) >= ACL_NAME_SZ) {
-        debugs(28, DBG_CRITICAL, "aclParseAclLine: aclParseAclLine: ACL name '" << t <<
-               "' too long, max " << ACL_NAME_SZ - 1 << " characters supported");
-        parser.destruct();
-        return;
-    }
-
     SBuf aclname(t);
     CallParser(ParsingContext::Pointer::Make(aclname), [&] {
-        ParseNamed(parser, head, aclname.c_str()); // TODO: Convert Node::name to SBuf
+        ParseNamed(parser, head, aclname);
     });
 }
 
 /// parses acl directive parts that follow aclname
 void
-Acl::Node::ParseNamed(ConfigParser &parser, Node ** const head, const char * const aclname)
+Acl::Node::ParseNamed(ConfigParser &parser, Node ** const head, const SBuf &aclname)
 {
     /* snarf the ACL type */
     const char *theType;
@@ -278,7 +284,7 @@ Acl::Node::ParseNamed(ConfigParser &parser, Node ** const head, const char * con
         }
         theType = "localport";
         debugs(28, DBG_IMPORTANT, "WARNING: UPGRADE: ACL 'myport' type has been renamed to 'localport' and matches the port the client connected to.");
-    } else if (strcmp(theType, "proto") == 0 && strcmp(aclname, "manager") == 0) {
+    } else if (strcmp(theType, "proto") == 0 && aclname.cmp("manager") == 0) {
         // ACL manager is now a built-in and has a different type.
         debugs(28, DBG_PARSE_NOTE(DBG_IMPORTANT), "WARNING: UPGRADE: ACL 'manager' is now a built-in ACL. Remove it from your config file.");
         return; // ignore the line
@@ -446,7 +452,6 @@ Acl::Node::~Node()
 {
     debugs(28, 3, "freeing ACL " << name);
     safe_free(cfgline);
-    AclMatchedName = nullptr; // in case it was pointing to our name
 }
 
 void