]> git.ipfire.org Git - thirdparty/squid.git/blob - src/acl/Ip.cc
Merge from trunk
[thirdparty/squid.git] / src / acl / Ip.cc
1 /*
2 * $Id$
3 *
4 * DEBUG: section 28 Access Control
5 * AUTHOR: Duane Wessels
6 *
7 * SQUID Web Proxy Cache http://www.squid-cache.org/
8 * ----------------------------------------------------------
9 *
10 * Squid is the result of efforts by numerous individuals from
11 * the Internet community; see the CONTRIBUTORS file for full
12 * details. Many organizations have provided support for Squid's
13 * development; see the SPONSORS file for full details. Squid is
14 * Copyrighted (C) 2001 by the Regents of the University of
15 * California; see the COPYRIGHT file for full details. Squid
16 * incorporates software developed and/or copyrighted by other
17 * sources; see the CREDITS file for full details.
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
32 *
33 * Copyright (c) 2003, Robert Collins <robertc@squid-cache.org>
34 */
35
36 #include "squid.h"
37 #include "acl/Ip.h"
38 #include "acl/Checklist.h"
39 #include "MemBuf.h"
40 #include "wordlist.h"
41
42 void *
43 ACLIP::operator new (size_t byteCount)
44 {
45 fatal ("ACLIP::operator new: unused");
46 return (void *)1;
47 }
48
49 void
50 ACLIP::operator delete (void *address)
51 {
52 fatal ("ACLIP::operator delete: unused");
53 }
54
55 /**
56 * Writes an IP ACL data into a buffer, then copies the buffer into the wordlist given
57 *
58 \param ip ACL data structure to display
59 \param state wordlist structure which is being generated
60 */
61 void
62 ACLIP::DumpIpListWalkee(acl_ip_data * const & ip, void *state)
63 {
64 char tmpbuf[ ((MAX_IPSTRLEN*2)+6) ]; // space for 2 IPs and a CIDR mask(3) and seperators(3).
65 MemBuf mb;
66 wordlist **W = static_cast<wordlist **>(state);
67 tmpbuf[0] = '\0';
68
69 mb.init();
70 assert(mb.max_capacity > 0 && 1==1 );
71
72 ip->toStr(tmpbuf, sizeof(tmpbuf) );
73 assert(mb.max_capacity > 0 && 2==2 );
74 mb.append(tmpbuf, strlen(tmpbuf) );
75 assert(mb.max_capacity > 0 && 3==3);
76 wordlistAdd(W, mb.buf);
77 mb.clean();
78 }
79
80 /**
81 * print/format an acl_ip_data structure for debugging output.
82 *
83 \param buf string buffer to write to
84 \param len size of the buffer available
85 */
86 void
87 acl_ip_data::toStr(char *buf, int len) const
88 {
89 char *b1 = buf;
90 char *b2 = NULL;
91 char *b3 = NULL;
92 int rlen = 0;
93
94 addr1.NtoA(b1, len - rlen );
95 rlen = strlen(buf);
96 b2 = buf + rlen;
97
98 if (!addr2.IsAnyAddr()) {
99 b2[0] = '-';
100 rlen++;
101 addr2.NtoA(&(b2[1]), len - rlen );
102 rlen = strlen(buf);
103 } else
104 b2[0] = '\0';
105
106 b3 = buf + rlen;
107
108 if (!mask.IsNoAddr()) {
109 b3[0] = '/';
110 rlen++;
111 #if USE_IPV6
112 int cidr = mask.GetCIDR() - (addr1.IsIPv4()?96:0);
113 snprintf(&(b3[1]), (len-rlen), "%u", (unsigned int)(cidr<0?0:cidr) );
114 #else
115 snprintf(&(b3[1]), (len-rlen), "%u", mask.GetCIDR() );
116 #endif
117 } else
118 b3[0] = '\0';
119 }
120
121 /*
122 * aclIpAddrNetworkCompare - The guts of the comparison for IP ACLs
123 * matching checks. The first argument (p) is a "host" address,
124 * i.e. the IP address of a cache client. The second argument (q)
125 * is an entry in some address-based access control element. This
126 * function is called via ACLIP::match() and the splay library.
127 */
128 int
129 aclIpAddrNetworkCompare(acl_ip_data * const &p, acl_ip_data * const &q)
130 {
131 IpAddress A = p->addr1;
132
133 /* apply netmask */
134 A.ApplyMask(q->mask);
135
136 debugs(28,9, "aclIpAddrNetworkCompare: compare: " << p->addr1 << "/" << q->mask << " (" << A << ") vs " <<
137 q->addr1 << "-" << q->addr2 << "/" << q->mask);
138
139 if (q->addr2.IsAnyAddr()) { /* single address check */
140
141 return A.matchIPAddr( q->addr1 );
142
143 } else { /* range address check */
144
145 if ( (A >= q->addr1) && (A <= q->addr2) )
146 return 0; /* valid. inside range. */
147 else
148 return A.matchIPAddr( q->addr1 ); /* outside of range, 'less than' */
149 }
150 }
151
152
153 /*
154 * acl_ip_data::NetworkCompare - Compare two acl_ip_data entries. Strictly
155 * used by the splay insertion routine. It emits a warning if it
156 * detects a "collision" or overlap that would confuse the splay
157 * sorting algorithm. Much like aclDomainCompare.
158 * The first argument (p) is a "host" address, i.e. the IP address of a cache client.
159 * The second argument (b) is a "network" address that might have a subnet and/or range.
160 * We mask the host address bits with the network subnet mask.
161 */
162 int
163 acl_ip_data::NetworkCompare(acl_ip_data * const & a, acl_ip_data * const &b)
164 {
165 int ret;
166 bool bina = true;
167 ret = aclIpAddrNetworkCompare(b, a);
168
169 if (ret != 0) {
170 bina = false;
171 ret = aclIpAddrNetworkCompare(a, b);
172 }
173
174 if (ret == 0) {
175 char buf_n1[3*(MAX_IPSTRLEN+1)];
176 char buf_n2[3*(MAX_IPSTRLEN+1)];
177 if (bina) {
178 b->toStr(buf_n1, 3*(MAX_IPSTRLEN+1));
179 a->toStr(buf_n2, 3*(MAX_IPSTRLEN+1));
180 } else {
181 a->toStr(buf_n1, 3*(MAX_IPSTRLEN+1));
182 b->toStr(buf_n2, 3*(MAX_IPSTRLEN+1));
183 }
184 debugs(28, 0, "WARNING: (" << (bina?'B':'A') << ") '" << buf_n1 << "' is a subnetwork of (" << (bina?'A':'B') << ") '" << buf_n2 << "'");
185 debugs(28, 0, "WARNING: because of this '" << (bina?buf_n2:buf_n1) << "' is ignored to keep splay tree searching predictable");
186 debugs(28, 0, "WARNING: You should probably remove '" << buf_n1 << "' from the ACL named '" << AclMatchedName << "'");
187 }
188
189 return ret;
190 }
191
192 /**
193 * Decode an ascii representation (asc) of a IP netmask address or CIDR,
194 * and place resulting information in mask.
195 * This function should NOT be called if 'asc' is a hostname!
196 */
197 bool
198 acl_ip_data::DecodeMask(const char *asc, IpAddress &mask, int ctype)
199 {
200 char junk;
201 int a1 = 0;
202
203 /* default is a mask that doesn't change any IP */
204 mask.SetNoAddr();
205
206 if (!asc || !*asc) {
207 return true;
208 }
209
210 /* An int mask 128, 32 */
211 if ((sscanf(asc, "%d%c", &a1, &junk)==1) &&
212 (a1 <= 128) && (a1 >= 0)
213 ) {
214 return mask.ApplyMask(a1, ctype);
215 }
216
217 /* dotted notation */
218 /* assignment returns true if asc contained an IP address as text */
219 if ((mask = asc)) {
220 /* HACK: IPv4 netmasks don't cleanly map to IPv6 masks. */
221 debugs(28, DBG_CRITICAL, "WARNING: Netmasks are deprecated. Please use CIDR masks instead.");
222 if (mask.IsIPv4()) {
223 /* locate what CIDR mask was _probably_ meant to be in its native protocol format. */
224 /* this will completely crap out with a security fail-open if the admin is playing mask tricks */
225 /* however, thats their fault, and we do warn. see bug 2601 for the effects if we don't do this. */
226 unsigned int m = mask.GetCIDR();
227 #if USE_IPV6
228 debugs(28, DBG_CRITICAL, "WARNING: IPv4 netmasks are particularly nasty when used to compare IPv6 to IPv4 ranges.");
229 #endif
230 debugs(28, DBG_CRITICAL, "WARNING: For now we will assume you meant to write /" << m);
231 /* reset the mask completely, and crop to the CIDR boundary back properly. */
232 mask.SetNoAddr();
233 return mask.ApplyMask(m,AF_INET);
234 }
235 return true;
236 }
237
238 return false;
239 }
240
241 /* Handle either type of address, IPv6 will be discarded with a warning if disabled */
242 #define SCAN_ACL1_6 "%[0123456789ABCDEFabcdef:]-%[0123456789ABCDEFabcdef:]/%[0123456789]"
243 #define SCAN_ACL2_6 "%[0123456789ABCDEFabcdef:]-%[0123456789ABCDEFabcdef:]%c"
244 #define SCAN_ACL3_6 "%[0123456789ABCDEFabcdef:]/%[0123456789]"
245 #define SCAN_ACL4_6 "%[0123456789ABCDEFabcdef:]/%c"
246 /* We DO need to know which is which though, for proper CIDR masking. */
247 #define SCAN_ACL1_4 "%[0123456789.]-%[0123456789.]/%[0123456789.]"
248 #define SCAN_ACL2_4 "%[0123456789.]-%[0123456789.]%c"
249 #define SCAN_ACL3_4 "%[0123456789.]/%[0123456789.]"
250 #define SCAN_ACL4_4 "%[0123456789.]/%c"
251
252 acl_ip_data *
253 acl_ip_data::FactoryParse(const char *t)
254 {
255 LOCAL_ARRAY(char, addr1, 256);
256 LOCAL_ARRAY(char, addr2, 256);
257 LOCAL_ARRAY(char, mask, 256);
258 acl_ip_data *r = NULL;
259 acl_ip_data **Q = NULL;
260 IpAddress temp;
261 char c;
262 unsigned int changed;
263 acl_ip_data *q = new acl_ip_data;
264 int iptype = AF_UNSPEC;
265
266 debugs(28, 5, "aclIpParseIpData: " << t);
267
268 /* Special ACL RHS "all" matches entire Internet */
269 if (strcasecmp(t, "all") == 0) {
270 debugs(28, 9, "aclIpParseIpData: magic 'all' found.");
271 q->addr1.SetAnyAddr();
272 q->addr2.SetEmpty();
273 q->mask.SetAnyAddr();
274 return q;
275 }
276
277 /* Detect some old broken strings equivalent to 'all'.
278 * treat them nicely. But be loud until its fixed. */
279 if (strcasecmp(t, "0/0") == 0 || strcasecmp(t, "0.0.0.0/0") == 0 || strcasecmp(t, "0.0.0.0/0.0.0.0") == 0 ||
280 strcasecmp(t, "0.0.0.0-255.255.255.255") == 0 || strcasecmp(t, "0.0.0.0-0.0.0.0/0") == 0) {
281
282 debugs(28,DBG_CRITICAL, "ERROR: '" << t << "' needs to be replaced by the term 'all'.");
283 debugs(28,DBG_CRITICAL, "SECURITY NOTICE: Overriding config setting. Using 'all' instead.");
284 q->addr1.SetAnyAddr();
285 q->addr2.SetEmpty();
286 q->mask.SetAnyAddr();
287 return q;
288 }
289
290 #if USE_IPV6
291 /* Special ACL RHS "ipv4" matches IPv4 Internet
292 * A nod to IANA; we include the entire class space in case
293 * they manage to find a way to recover and use it */
294 if (strcasecmp(t, "ipv4") == 0) {
295 q->mask.SetNoAddr();
296 q->mask.ApplyMask(0, AF_INET);
297 return q;
298 }
299
300 /* Special ACL RHS "ipv6" matches IPv6-Unicast Internet */
301 if (strcasecmp(t, "ipv6") == 0) {
302 debugs(28, 9, "aclIpParseIpData: magic 'ipv6' found.");
303 r = q; // save head of the list for result.
304
305 /* 0000::/4 is a mix of localhost and obsolete IPv4-mapping space. Not valid outside this host. */
306
307 /* Future global unicast space: 1000::/4 */
308 q->addr1 = "1000::";
309 q->mask.SetNoAddr();
310 q->mask.ApplyMask(4, AF_INET6);
311
312 /* Current global unicast space: 2000::/4 = (2000::/4 - 3000::/4) */
313 q->next = new acl_ip_data;
314 q = q->next;
315 q->addr1 = "2000::";
316 q->mask.SetNoAddr();
317 q->mask.ApplyMask(3, AF_INET6);
318
319 /* Future global unicast space: 4000::/2 = (4000::/4 - 7000::/4) */
320 q->next = new acl_ip_data;
321 q = q->next;
322 q->addr1 = "4000::";
323 q->mask.SetNoAddr();
324 q->mask.ApplyMask(2, AF_INET6);
325
326 /* Future global unicast space: 8000::/2 = (8000::/4 - B000::/4) */
327 q->next = new acl_ip_data;
328 q = q->next;
329 q->addr1 = "8000::";
330 q->mask.SetNoAddr();
331 q->mask.ApplyMask(2, AF_INET6);
332
333 /* Future global unicast space: C000::/3 = (C000::/4 - D000::/4) */
334 q->next = new acl_ip_data;
335 q = q->next;
336 q->addr1 = "C000::";
337 q->mask.SetNoAddr();
338 q->mask.ApplyMask(3, AF_INET6);
339
340 /* Future global unicast space: E000::/4 */
341 q->next = new acl_ip_data;
342 q = q->next;
343 q->addr1 = "E000::";
344 q->mask.SetNoAddr();
345 q->mask.ApplyMask(4, AF_INET6);
346
347 /* F000::/4 is mostly reserved non-unicast. With some exceptions ... */
348
349 /* RFC 4193 Unique-Local unicast space: FC00::/7 */
350 q->next = new acl_ip_data;
351 q = q->next;
352 q->addr1 = "FC00::";
353 q->mask.SetNoAddr();
354 q->mask.ApplyMask(7, AF_INET6);
355
356 /* Link-Local unicast space: FE80::/10 */
357 q->next = new acl_ip_data;
358 q = q->next;
359 q->addr1 = "FE80::";
360 q->mask.SetNoAddr();
361 q->mask.ApplyMask(10, AF_INET6);
362
363 return r;
364 }
365 #endif
366
367 // IPv4
368 if (sscanf(t, SCAN_ACL1_4, addr1, addr2, mask) == 3) {
369 debugs(28, 9, "aclIpParseIpData: '" << t << "' matched: SCAN1-v4: " << SCAN_ACL1_4);
370 iptype=AF_INET;
371 } else if (sscanf(t, SCAN_ACL2_4, addr1, addr2, &c) >= 2) {
372 debugs(28, 9, "aclIpParseIpData: '" << t << "' matched: SCAN2-v4: " << SCAN_ACL2_4);
373 mask[0] = '\0';
374 iptype=AF_INET;
375 } else if (sscanf(t, SCAN_ACL3_4, addr1, mask) == 2) {
376 debugs(28, 9, "aclIpParseIpData: '" << t << "' matched: SCAN3-v4: " << SCAN_ACL3_4);
377 addr2[0] = '\0';
378 iptype=AF_INET;
379 } else if (sscanf(t, SCAN_ACL4_4, addr1,&c) == 2) {
380 debugs(28, 9, "aclIpParseIpData: '" << t << "' matched: SCAN4-v4: " << SCAN_ACL4_4);
381 addr2[0] = '\0';
382 mask[0] = '\0';
383 iptype=AF_INET;
384
385 // IPv6
386 } else if (sscanf(t, SCAN_ACL1_6, addr1, addr2, mask) == 3) {
387 debugs(28, 9, "aclIpParseIpData: '" << t << "' matched: SCAN1-v6: " << SCAN_ACL1_6);
388 iptype=AF_INET6;
389 } else if (sscanf(t, SCAN_ACL2_6, addr1, addr2, &c) >= 2) {
390 debugs(28, 9, "aclIpParseIpData: '" << t << "' matched: SCAN2-v6: " << SCAN_ACL2_6);
391 mask[0] = '\0';
392 iptype=AF_INET6;
393 } else if (sscanf(t, SCAN_ACL3_6, addr1, mask) == 2) {
394 debugs(28, 9, "aclIpParseIpData: '" << t << "' matched: SCAN3-v6: " << SCAN_ACL3_6);
395 addr2[0] = '\0';
396 iptype=AF_INET6;
397 } else if (sscanf(t, SCAN_ACL4_6, addr1, mask) == 2) {
398 debugs(28, 9, "aclIpParseIpData: '" << t << "' matched: SCAN4-v6: " << SCAN_ACL4_6);
399 addr2[0] = '\0';
400 iptype=AF_INET6;
401
402 // Neither
403 } else if (sscanf(t, "%[^/]/%s", addr1, mask) == 2) {
404 debugs(28, 9, "aclIpParseIpData: '" << t << "' matched: non-IP pattern: %[^/]/%s");
405 addr2[0] = '\0';
406 } else if (sscanf(t, "%s", addr1) == 1) {
407 /*
408 * Note, must use plain xgetaddrinfo() here because at startup
409 * ipcache hasn't been initialized
410 * TODO: offload this to one of the IpAddress lookups.
411 */
412
413 debugs(28, 5, "aclIpParseIpData: Lookup Host/IP " << addr1);
414 struct addrinfo *hp = NULL, *x = NULL;
415 struct addrinfo hints;
416 IpAddress *prev_addr = NULL;
417
418 memset(&hints, 0, sizeof(struct addrinfo));
419
420 if ( iptype != AF_UNSPEC ) {
421 hints.ai_flags |= AI_NUMERICHOST;
422 }
423
424 #if 0 && USE_IPV6 && !IPV6_SPECIAL_SPLITSTACK
425 hints.ai_flags |= AI_V4MAPPED | AI_ALL;
426 #endif
427
428 int errcode = xgetaddrinfo(addr1,NULL,&hints,&hp);
429 if (hp == NULL) {
430 debugs(28, 0, "aclIpParseIpData: Bad host/IP: '" << addr1 <<
431 "' in '" << t << "', flags=" << hints.ai_flags <<
432 " : (" << errcode << ") " << xgai_strerror(errcode) );
433 self_destruct();
434 return NULL;
435 }
436
437 Q = &q;
438
439 for (x = hp; x != NULL;) {
440 if ((r = *Q) == NULL)
441 r = *Q = new acl_ip_data;
442
443 /* getaddrinfo given a host has a nasty tendency to return duplicate addr's */
444 /* BUT sorted fortunately, so we can drop most of them easily */
445 r->addr1 = *x;
446 x = x->ai_next;
447 if ( prev_addr && r->addr1 == *prev_addr) {
448 debugs(28, 3, "aclIpParseIpData: Duplicate host/IP: '" << r->addr1 << "' dropped.");
449 delete r;
450 *Q = NULL;
451 continue;
452 } else
453 prev_addr = &r->addr1;
454
455 debugs(28, 3, "aclIpParseIpData: Located host/IP: '" << r->addr1 << "'");
456
457 r->addr2.SetAnyAddr();
458 r->mask.SetNoAddr();
459
460 Q = &r->next;
461
462 debugs(28, 3, "" << addr1 << " --> " << r->addr1 );
463 }
464
465 if (*Q != NULL) {
466 debugs(28, 0, "aclIpParseIpData: Bad host/IP: '" << t << "'");
467 self_destruct();
468 return NULL;
469 }
470
471 xfreeaddrinfo(hp);
472
473 return q;
474 }
475
476 #if !USE_IPV6
477 /* ignore IPv6 addresses when built with IPv4-only */
478 if ( iptype == AF_INET6 ) {
479 debugs(28, 0, "aclIpParseIpData: IPv6 has not been enabled. build with '--enable-ipv6'");
480 return NULL;
481 }
482 #endif
483
484 /* Decode addr1 */
485 if (!*addr1 || !(q->addr1 = addr1)) {
486 debugs(28, 0, "aclIpParseIpData: unknown first address in '" << t << "'");
487 delete q;
488 self_destruct();
489 return NULL;
490 }
491
492 /* Decode addr2 */
493 if (!*addr2)
494 q->addr2.SetAnyAddr();
495 else if (!(q->addr2=addr2) ) {
496 debugs(28, 0, "aclIpParseIpData: unknown second address in '" << t << "'");
497 delete q;
498 self_destruct();
499 return NULL;
500 }
501
502 /* Decode mask (NULL or empty means a exact host mask) */
503 if (!DecodeMask(mask, q->mask, iptype)) {
504 debugs(28, 0, "aclParseIpData: unknown netmask '" << mask << "' in '" << t << "'");
505 delete q;
506 self_destruct();
507 return NULL;
508 }
509
510 changed = 0;
511 changed += q->addr1.ApplyMask(q->mask);
512 changed += q->addr2.ApplyMask(q->mask);
513
514 if (changed)
515 debugs(28, 0, "aclIpParseIpData: WARNING: Netmask masks away part of the specified IP in '" << t << "'");
516
517 debugs(28,9, HERE << "Parsed: " << q->addr1 << "-" << q->addr2 << "/" << q->mask << "(/" << q->mask.GetCIDR() <<")");
518
519 /* 1.2.3.4/255.255.255.0 --> 1.2.3.0 */
520 /* Same as IPv6 (not so trivial to depict) */
521 return q;
522 }
523
524 void
525 ACLIP::parse()
526 {
527 char *t = NULL;
528
529 while ((t = strtokFile())) {
530 acl_ip_data *q = acl_ip_data::FactoryParse(t);
531
532 while (q != NULL) {
533 /* pop each result off the list and add it to the data tree individually */
534 acl_ip_data *next_node = q->next;
535 q->next = NULL;
536 data = data->insert(q, acl_ip_data::NetworkCompare);
537 q = next_node;
538 }
539 }
540 }
541
542 ACLIP::~ACLIP()
543 {
544 if (data)
545 data->destroy(IPSplay::DefaultFree);
546 }
547
548 wordlist *
549 ACLIP::dump() const
550 {
551 wordlist *w = NULL;
552 data->walk (DumpIpListWalkee, &w);
553 return w;
554 }
555
556 bool
557 ACLIP::empty () const
558 {
559 return data->empty();
560 }
561
562 int
563 ACLIP::match(IpAddress &clientip)
564 {
565 static acl_ip_data ClientAddress;
566 /*
567 * aclIpAddrNetworkCompare() takes two acl_ip_data pointers as
568 * arguments, so we must create a fake one for the client's IP
569 * address. Since we are scanning for a single IP mask and addr2
570 * MUST be set to empty.
571 */
572 ClientAddress.addr1 = clientip;
573 ClientAddress.addr2.SetEmpty();
574 ClientAddress.mask.SetEmpty();
575
576 data = data->splay(&ClientAddress, aclIpAddrNetworkCompare);
577 debugs(28, 3, "aclIpMatchIp: '" << clientip << "' " << (splayLastResult ? "NOT found" : "found"));
578 return !splayLastResult;
579 }
580
581 acl_ip_data::acl_ip_data () :addr1(), addr2(), mask(), next (NULL) {}
582
583 acl_ip_data::acl_ip_data (IpAddress const &anAddress1, IpAddress const &anAddress2, IpAddress const &aMask, acl_ip_data *aNext) : addr1(anAddress1), addr2(anAddress2), mask(aMask), next(aNext) {}