DomainData.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9/* DEBUG: section 28 Access Control */
10
11#include "squid.h"
12#include "acl/Checklist.h"
13#include "acl/DomainData.h"
14#include "anyp/Uri.h"
15#include "cache_cf.h"
16#include "ConfigParser.h"
17#include "debug/Stream.h"
18#include "util.h"
19
20template<class T>
21inline void
22xRefFree(T &thing)
23{
24 xfree (thing);
25}
26
28{
29 if (domains) {
31 delete domains;
32 }
33}
34
35template<class T>
36inline int
38{
39 return strcasecmp ((char *)l,(char *)r);
40}
41
42template<class T>
43inline int
44splaystrcmp (T&l, T&r)
45{
46 return strcmp ((char *)l,(char *)r);
47}
48
49/* general compare functions, these are used for tree search algorithms
50 * so they return <0, 0 or >0 */
51
52/* compare a host and a domain */
53
54static int
55aclHostDomainCompare( char *const &a, char * const &b)
56{
57 const char *h = static_cast<const char *>(a);
58 const char *d = static_cast<const char *>(b);
59 return matchDomainName(h, d);
60}
61
62/* compare two domains */
63
64template<class T>
65int
66aclDomainCompare(T const &a, T const &b)
67{
68 char * const d1 = static_cast<char *>(b);
69 char * const d2 = static_cast<char *>(a);
70 int ret;
71 ret = aclHostDomainCompare(d1, d2);
72
73 if (ret != 0) {
74 char *const d3 = d2;
75 char *const d4 = d1;
76 ret = aclHostDomainCompare(d3, d4);
77 if (ret == 0) {
78 // When a.example.com comes after .example.com in an ACL
79 // sub-domain is ignored. That is okay. Just important
80 bool d3big = (strlen(d3) > strlen(d4)); // Always suggest removing the longer one.
81 debugs(28, DBG_IMPORTANT, "WARNING: '" << (d3big?d3:d4) << "' is a subdomain of '" << (d3big?d4:d3) << "'");
82 debugs(28, DBG_IMPORTANT, "WARNING: You should remove '" << (d3big?d3:d4) << "' from the ACL named '" << AclMatchedName << "'");
83 debugs(28, 2, "Ignore '" << d3 << "' to keep splay tree searching predictable");
84 }
85 } else if (ret == 0) {
86 // It may be an exact duplicate. No problem. Just drop.
87 if (strcmp(d1,d2)==0) {
88 debugs(28, 2, "WARNING: '" << d2 << "' is duplicated in the list.");
89 debugs(28, 2, "WARNING: You should remove one '" << d2 << "' from the ACL named '" << AclMatchedName << "'");
90 return ret;
91 }
92 // When a.example.com comes before .example.com in an ACL
93 // discarding the wildcard is critically bad.
94 // or Maybe even both are wildcards. Things are very weird in those cases.
95 bool d1big = (strlen(d1) > strlen(d2)); // Always suggest removing the longer one.
96 debugs(28, DBG_CRITICAL, "ERROR: '" << (d1big?d1:d2) << "' is a subdomain of '" << (d1big?d2:d1) << "'");
97 debugs(28, DBG_CRITICAL, "ERROR: You need to remove '" << (d1big?d1:d2) << "' from the ACL named '" << AclMatchedName << "'");
99 }
100
101 return ret;
102}
103
104bool
105ACLDomainData::match(char const *host)
106{
107 if (host == nullptr)
108 return 0;
109
110 debugs(28, 3, "aclMatchDomainList: checking '" << host << "'");
111
112 char *h = const_cast<char *>(host);
113 char const * const * result = domains->find(h, aclHostDomainCompare);
114
115 debugs(28, 3, "aclMatchDomainList: '" << host << "' " << (result ? "found" : "NOT found"));
116
117 return (result != nullptr);
118}
119
122 void operator() (char * const & node_data) {
123 contents.push_back(SBuf(node_data));
124 }
125};
126
129{
131 domains->visit(visitor);
132 return visitor.contents;
133}
134
135void
137{
138 if (!domains)
139 domains = new Splay<char *>();
140
141 while (char *t = ConfigParser::strtokFile()) {
142 Tolower(t);
144 }
145}
146
147bool
149{
150 return domains->empty();
151}
152
int splaystrcmp(T &l, T &r)
Definition: DomainData.cc:44
static int aclHostDomainCompare(char *const &a, char *const &b)
Definition: DomainData.cc:55
int splaystrcasecmp(T &l, T &r)
Definition: DomainData.cc:37
void xRefFree(T &thing)
Definition: DomainData.cc:22
int aclDomainCompare(T const &a, T const &b)
Definition: DomainData.cc:66
int matchDomainName(const char *h, const char *d, MatchDomainNameFlags flags)
Definition: Uri.cc:820
void self_destruct(void)
Definition: cache_cf.cc:277
~ACLDomainData() override
Definition: DomainData.cc:27
void parse() override
Definition: DomainData.cc:136
SBufList dump() const override
Definition: DomainData.cc:128
bool match(char const *) override
Definition: DomainData.cc:105
Splay< char * > * domains
Definition: DomainData.h:28
bool empty() const override
Definition: DomainData.cc:148
static char * strtokFile()
Definition: ConfigParser.cc:65
Definition: SBuf.h:94
Value const * find(FindValue const &, int(*compare)(FindValue const &a, Value const &b)) const
bool empty() const
Definition: splay.h:76
void insert(Value const &, SPLAYCMP *compare)
Definition: splay.h:318
void visit(ValueVisitor &) const
left-to-right visit of all stored Values
void destroy(SPLAYFREE *=DefaultFree)
Definition: splay.h:365
#define DBG_IMPORTANT
Definition: Stream.h:38
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Stream.h:194
#define DBG_CRITICAL
Definition: Stream.h:37
const char * AclMatchedName
Definition: Acl.cc:29
#define xfree
#define xstrdup
std::list< SBuf > SBufList
Definition: forward.h:23
void operator()(char *const &node_data)
Definition: DomainData.cc:122
SQUIDCEXTERN void Tolower(char *)
Definition: util.c:28

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors