2 * DEBUG: section 28 Access Control
3 * AUTHOR: Duane Wessels
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
31 * Copyright (c) 2003, Robert Collins <robertc@squid-cache.org>
35 #include "acl/Checklist.h"
44 ACLIP::operator new (size_t byteCount
)
46 fatal ("ACLIP::operator new: unused");
51 ACLIP::operator delete (void *address
)
53 fatal ("ACLIP::operator delete: unused");
57 * Writes an IP ACL data into a buffer, then copies the buffer into the wordlist given
59 \param ip ACL data structure to display
60 \param state wordlist structure which is being generated
63 ACLIP::DumpIpListWalkee(acl_ip_data
* const & ip
, void *state
)
65 static_cast<SBufList
*>(state
)->push_back(ip
->toSBuf());
69 * print/format an acl_ip_data structure for debugging output.
71 \param buf string buffer to write to
72 \param len size of the buffer available
75 acl_ip_data::toStr(char *buf
, int len
) const
82 addr1
.toStr(b1
, len
- rlen
);
86 if (!addr2
.isAnyAddr()) {
89 addr2
.toStr(&(b2
[1]), len
- rlen
);
96 if (!mask
.isNoAddr()) {
99 int cidr
= mask
.cidr() - (addr1
.isIPv4()?96:0);
100 snprintf(&(b3
[1]), (len
-rlen
), "%u", (unsigned int)(cidr
<0?0:cidr
) );
106 acl_ip_data::toSBuf() const
108 const int bufsz
= MAX_IPSTRLEN
*2+6;
109 static char tmpbuf
[ bufsz
];
115 * aclIpAddrNetworkCompare - The guts of the comparison for IP ACLs
116 * matching checks. The first argument (p) is a "host" address,
117 * i.e. the IP address of a cache client. The second argument (q)
118 * is an entry in some address-based access control element. This
119 * function is called via ACLIP::match() and the splay library.
122 aclIpAddrNetworkCompare(acl_ip_data
* const &p
, acl_ip_data
* const &q
)
124 Ip::Address A
= p
->addr1
;
127 A
.applyMask(q
->mask
);
129 debugs(28,9, "aclIpAddrNetworkCompare: compare: " << p
->addr1
<< "/" << q
->mask
<< " (" << A
<< ") vs " <<
130 q
->addr1
<< "-" << q
->addr2
<< "/" << q
->mask
);
132 if (q
->addr2
.isAnyAddr()) { /* single address check */
134 return A
.matchIPAddr( q
->addr1
);
136 } else { /* range address check */
138 if ( (A
>= q
->addr1
) && (A
<= q
->addr2
) )
139 return 0; /* valid. inside range. */
141 return A
.matchIPAddr( q
->addr1
); /* outside of range, 'less than' */
146 * acl_ip_data::NetworkCompare - Compare two acl_ip_data entries. Strictly
147 * used by the splay insertion routine. It emits a warning if it
148 * detects a "collision" or overlap that would confuse the splay
149 * sorting algorithm. Much like aclDomainCompare.
150 * The first argument (p) is a "host" address, i.e. the IP address of a cache client.
151 * The second argument (b) is a "network" address that might have a subnet and/or range.
152 * We mask the host address bits with the network subnet mask.
155 acl_ip_data::NetworkCompare(acl_ip_data
* const & a
, acl_ip_data
* const &b
)
159 ret
= aclIpAddrNetworkCompare(b
, a
);
163 ret
= aclIpAddrNetworkCompare(a
, b
);
167 char buf_n1
[3*(MAX_IPSTRLEN
+1)];
168 char buf_n2
[3*(MAX_IPSTRLEN
+1)];
170 b
->toStr(buf_n1
, 3*(MAX_IPSTRLEN
+1));
171 a
->toStr(buf_n2
, 3*(MAX_IPSTRLEN
+1));
173 a
->toStr(buf_n1
, 3*(MAX_IPSTRLEN
+1));
174 b
->toStr(buf_n2
, 3*(MAX_IPSTRLEN
+1));
176 debugs(28, DBG_CRITICAL
, "WARNING: (" << (bina
?'B':'A') << ") '" << buf_n1
<< "' is a subnetwork of (" << (bina
?'A':'B') << ") '" << buf_n2
<< "'");
177 debugs(28, DBG_CRITICAL
, "WARNING: because of this '" << (bina
?buf_n2
:buf_n1
) << "' is ignored to keep splay tree searching predictable");
178 debugs(28, DBG_CRITICAL
, "WARNING: You should probably remove '" << buf_n1
<< "' from the ACL named '" << AclMatchedName
<< "'");
185 * Decode an ascii representation (asc) of a IP netmask address or CIDR,
186 * and place resulting information in mask.
187 * This function should NOT be called if 'asc' is a hostname!
190 acl_ip_data::DecodeMask(const char *asc
, Ip::Address
&mask
, int ctype
)
195 /* default is a mask that doesn't change any IP */
202 /* An int mask 128, 32 */
203 if ((sscanf(asc
, "%d%c", &a1
, &junk
)==1) &&
204 (a1
<= 128) && (a1
>= 0)
206 return mask
.applyMask(a1
, ctype
);
209 /* dotted notation */
210 /* assignment returns true if asc contained an IP address as text */
212 /* HACK: IPv4 netmasks don't cleanly map to IPv6 masks. */
213 debugs(28, DBG_CRITICAL
, "WARNING: Netmasks are deprecated. Please use CIDR masks instead.");
215 /* locate what CIDR mask was _probably_ meant to be in its native protocol format. */
216 /* this will completely crap out with a security fail-open if the admin is playing mask tricks */
217 /* however, thats their fault, and we do warn. see bug 2601 for the effects if we don't do this. */
218 unsigned int m
= mask
.cidr();
219 debugs(28, DBG_CRITICAL
, "WARNING: IPv4 netmasks are particularly nasty when used to compare IPv6 to IPv4 ranges.");
220 debugs(28, DBG_CRITICAL
, "WARNING: For now we will assume you meant to write /" << m
);
221 /* reset the mask completely, and crop to the CIDR boundary back properly. */
223 return mask
.applyMask(m
,AF_INET
);
231 /* Handle either type of address, IPv6 will be discarded with a warning if disabled */
232 #define SCAN_ACL1_6 "%[0123456789ABCDEFabcdef:]-%[0123456789ABCDEFabcdef:]/%[0123456789]"
233 #define SCAN_ACL2_6 "%[0123456789ABCDEFabcdef:]-%[0123456789ABCDEFabcdef:]%c"
234 #define SCAN_ACL3_6 "%[0123456789ABCDEFabcdef:]/%[0123456789]"
235 #define SCAN_ACL4_6 "%[0123456789ABCDEFabcdef:]/%c"
236 /* We DO need to know which is which though, for proper CIDR masking. */
237 #define SCAN_ACL1_4 "%[0123456789.]-%[0123456789.]/%[0123456789.]"
238 #define SCAN_ACL2_4 "%[0123456789.]-%[0123456789.]%c"
239 #define SCAN_ACL3_4 "%[0123456789.]/%[0123456789.]"
240 #define SCAN_ACL4_4 "%[0123456789.]/%c"
243 acl_ip_data::FactoryParse(const char *t
)
245 LOCAL_ARRAY(char, addr1
, 256);
246 LOCAL_ARRAY(char, addr2
, 256);
247 LOCAL_ARRAY(char, mask
, 256);
248 acl_ip_data
*r
= NULL
;
249 acl_ip_data
**Q
= NULL
;
252 unsigned int changed
;
253 acl_ip_data
*q
= new acl_ip_data
;
254 int iptype
= AF_UNSPEC
;
256 debugs(28, 5, "aclIpParseIpData: " << t
);
258 /* Special ACL RHS "all" matches entire Internet */
259 if (strcmp(t
, "all") == 0) {
260 debugs(28, 9, "aclIpParseIpData: magic 'all' found.");
261 q
->addr1
.setAnyAddr();
263 q
->mask
.setAnyAddr();
267 /* Detect some old broken strings equivalent to 'all'.
268 * treat them nicely. But be loud until its fixed. */
269 if (strcmp(t
, "0/0") == 0 || strcmp(t
, "0.0.0.0/0") == 0 || strcmp(t
, "0.0.0.0/0.0.0.0") == 0 ||
270 strcmp(t
, "0.0.0.0-255.255.255.255") == 0 || strcmp(t
, "0.0.0.0-0.0.0.0/0") == 0) {
272 debugs(28,DBG_CRITICAL
, "ERROR: '" << t
<< "' needs to be replaced by the term 'all'.");
273 debugs(28,DBG_CRITICAL
, "SECURITY NOTICE: Overriding config setting. Using 'all' instead.");
274 q
->addr1
.setAnyAddr();
276 q
->mask
.setAnyAddr();
280 /* Special ACL RHS "ipv4" matches IPv4 Internet
281 * A nod to IANA; we include the entire class space in case
282 * they manage to find a way to recover and use it */
283 if (strcmp(t
, "ipv4") == 0) {
285 q
->mask
.applyMask(0, AF_INET
);
289 /* Special ACL RHS "ipv6" matches IPv6-Unicast Internet */
290 if (strcmp(t
, "ipv6") == 0) {
291 debugs(28, 9, "aclIpParseIpData: magic 'ipv6' found.");
292 r
= q
; // save head of the list for result.
294 /* 0000::/4 is a mix of localhost and obsolete IPv4-mapping space. Not valid outside this host. */
296 /* Future global unicast space: 1000::/4 */
299 q
->mask
.applyMask(4, AF_INET6
);
301 /* Current global unicast space: 2000::/4 = (2000::/4 - 3000::/4) */
302 q
->next
= new acl_ip_data
;
306 q
->mask
.applyMask(3, AF_INET6
);
308 /* Future global unicast space: 4000::/2 = (4000::/4 - 7000::/4) */
309 q
->next
= new acl_ip_data
;
313 q
->mask
.applyMask(2, AF_INET6
);
315 /* Future global unicast space: 8000::/2 = (8000::/4 - B000::/4) */
316 q
->next
= new acl_ip_data
;
320 q
->mask
.applyMask(2, AF_INET6
);
322 /* Future global unicast space: C000::/3 = (C000::/4 - D000::/4) */
323 q
->next
= new acl_ip_data
;
327 q
->mask
.applyMask(3, AF_INET6
);
329 /* Future global unicast space: E000::/4 */
330 q
->next
= new acl_ip_data
;
334 q
->mask
.applyMask(4, AF_INET6
);
336 /* F000::/4 is mostly reserved non-unicast. With some exceptions ... */
338 /* RFC 4193 Unique-Local unicast space: FC00::/7 */
339 q
->next
= new acl_ip_data
;
343 q
->mask
.applyMask(7, AF_INET6
);
345 /* Link-Local unicast space: FE80::/10 */
346 q
->next
= new acl_ip_data
;
350 q
->mask
.applyMask(10, AF_INET6
);
356 if (sscanf(t
, SCAN_ACL1_4
, addr1
, addr2
, mask
) == 3) {
357 debugs(28, 9, "aclIpParseIpData: '" << t
<< "' matched: SCAN1-v4: " << SCAN_ACL1_4
);
359 } else if (sscanf(t
, SCAN_ACL2_4
, addr1
, addr2
, &c
) >= 2) {
360 debugs(28, 9, "aclIpParseIpData: '" << t
<< "' matched: SCAN2-v4: " << SCAN_ACL2_4
);
363 } else if (sscanf(t
, SCAN_ACL3_4
, addr1
, mask
) == 2) {
364 debugs(28, 9, "aclIpParseIpData: '" << t
<< "' matched: SCAN3-v4: " << SCAN_ACL3_4
);
367 } else if (sscanf(t
, SCAN_ACL4_4
, addr1
,&c
) == 2) {
368 debugs(28, 9, "aclIpParseIpData: '" << t
<< "' matched: SCAN4-v4: " << SCAN_ACL4_4
);
374 } else if (sscanf(t
, SCAN_ACL1_6
, addr1
, addr2
, mask
) == 3) {
375 debugs(28, 9, "aclIpParseIpData: '" << t
<< "' matched: SCAN1-v6: " << SCAN_ACL1_6
);
377 } else if (sscanf(t
, SCAN_ACL2_6
, addr1
, addr2
, &c
) >= 2) {
378 debugs(28, 9, "aclIpParseIpData: '" << t
<< "' matched: SCAN2-v6: " << SCAN_ACL2_6
);
381 } else if (sscanf(t
, SCAN_ACL3_6
, addr1
, mask
) == 2) {
382 debugs(28, 9, "aclIpParseIpData: '" << t
<< "' matched: SCAN3-v6: " << SCAN_ACL3_6
);
385 } else if (sscanf(t
, SCAN_ACL4_6
, addr1
, mask
) == 2) {
386 debugs(28, 9, "aclIpParseIpData: '" << t
<< "' matched: SCAN4-v6: " << SCAN_ACL4_6
);
391 } else if (sscanf(t
, "%[^/]/%s", addr1
, mask
) == 2) {
392 debugs(28, 9, "aclIpParseIpData: '" << t
<< "' matched: non-IP pattern: %[^/]/%s");
394 } else if (sscanf(t
, "%s", addr1
) == 1) {
396 * Note, must use plain getaddrinfo() here because at startup
397 * ipcache hasn't been initialized
398 * TODO: offload this to one of the Ip::Address lookups.
401 debugs(28, 5, "aclIpParseIpData: Lookup Host/IP " << addr1
);
402 struct addrinfo
*hp
= NULL
, *x
= NULL
;
403 struct addrinfo hints
;
404 Ip::Address
*prev_addr
= NULL
;
406 memset(&hints
, 0, sizeof(struct addrinfo
));
408 int errcode
= getaddrinfo(addr1
,NULL
,&hints
,&hp
);
410 debugs(28, DBG_CRITICAL
, "aclIpParseIpData: Bad host/IP: '" << addr1
<<
411 "' in '" << t
<< "', flags=" << hints
.ai_flags
<<
412 " : (" << errcode
<< ") " << gai_strerror(errcode
) );
419 for (x
= hp
; x
!= NULL
;) {
420 if ((r
= *Q
) == NULL
)
421 r
= *Q
= new acl_ip_data
;
423 /* getaddrinfo given a host has a nasty tendency to return duplicate addr's */
424 /* BUT sorted fortunately, so we can drop most of them easily */
427 if ( prev_addr
&& r
->addr1
== *prev_addr
) {
428 debugs(28, 3, "aclIpParseIpData: Duplicate host/IP: '" << r
->addr1
<< "' dropped.");
433 prev_addr
= &r
->addr1
;
435 debugs(28, 3, "aclIpParseIpData: Located host/IP: '" << r
->addr1
<< "'");
437 r
->addr2
.setAnyAddr();
442 debugs(28, 3, "" << addr1
<< " --> " << r
->addr1
);
446 debugs(28, DBG_CRITICAL
, "aclIpParseIpData: Bad host/IP: '" << t
<< "'");
456 /* ignore IPv6 addresses when built with IPv4-only */
457 if ( iptype
== AF_INET6
&& !Ip::EnableIpv6
) {
458 debugs(28, DBG_IMPORTANT
, "aclIpParseIpData: IPv6 has not been enabled.");
464 if (!*addr1
|| !(q
->addr1
= addr1
)) {
465 debugs(28, DBG_CRITICAL
, "aclIpParseIpData: unknown first address in '" << t
<< "'");
473 q
->addr2
.setAnyAddr();
474 else if (!(q
->addr2
=addr2
) ) {
475 debugs(28, DBG_CRITICAL
, "aclIpParseIpData: unknown second address in '" << t
<< "'");
481 /* Decode mask (NULL or empty means a exact host mask) */
482 if (!DecodeMask(mask
, q
->mask
, iptype
)) {
483 debugs(28, DBG_CRITICAL
, "aclParseIpData: unknown netmask '" << mask
<< "' in '" << t
<< "'");
490 changed
+= q
->addr1
.applyMask(q
->mask
);
491 changed
+= q
->addr2
.applyMask(q
->mask
);
494 debugs(28, DBG_CRITICAL
, "aclIpParseIpData: WARNING: Netmask masks away part of the specified IP in '" << t
<< "'");
496 debugs(28,9, HERE
<< "Parsed: " << q
->addr1
<< "-" << q
->addr2
<< "/" << q
->mask
<< "(/" << q
->mask
.cidr() <<")");
498 /* 1.2.3.4/255.255.255.0 --> 1.2.3.0 */
499 /* Same as IPv6 (not so trivial to depict) */
508 while (char *t
= strtokFile()) {
509 acl_ip_data
*q
= acl_ip_data::FactoryParse(t
);
512 /* pop each result off the list and add it to the data tree individually */
513 acl_ip_data
*next_node
= q
->next
;
515 data
= data
->insert(q
, acl_ip_data::NetworkCompare
);
524 data
->destroy(IPSplay::DefaultFree
);
531 data
->walk(DumpIpListWalkee
, &sl
);
538 return data
->empty();
542 ACLIP::match(Ip::Address
&clientip
)
544 static acl_ip_data ClientAddress
;
546 * aclIpAddrNetworkCompare() takes two acl_ip_data pointers as
547 * arguments, so we must create a fake one for the client's IP
548 * address. Since we are scanning for a single IP mask and addr2
549 * MUST be set to empty.
551 ClientAddress
.addr1
= clientip
;
552 ClientAddress
.addr2
.setEmpty();
553 ClientAddress
.mask
.setEmpty();
555 data
= data
->splay(&ClientAddress
, aclIpAddrNetworkCompare
);
556 debugs(28, 3, "aclIpMatchIp: '" << clientip
<< "' " << (splayLastResult
? "NOT found" : "found"));
557 return !splayLastResult
;
560 acl_ip_data::acl_ip_data() :addr1(), addr2(), mask(), next (NULL
) {}
562 acl_ip_data::acl_ip_data(Ip::Address
const &anAddress1
, Ip::Address
const &anAddress2
, Ip::Address
const &aMask
, acl_ip_data
*aNext
) : addr1(anAddress1
), addr2(anAddress2
), mask(aMask
), next(aNext
) {}