]> git.ipfire.org Git - thirdparty/squid.git/blame - src/acl/Asn.cc
renamed cachemgr_passwd to CacheMgrPasswd and moved to own header.
[thirdparty/squid.git] / src / acl / Asn.cc
CommitLineData
445d18a4 1
3841dd46 2/*
445d18a4
AJ
3 * DEBUG: section 53 AS Number handling
4 * AUTHOR: Duane Wessels, Kostas Anagnostakis
3841dd46 5 *
6 * SQUID Web Proxy Cache http://www.squid-cache.org/
7 * ----------------------------------------------------------
8 *
9 * Squid is the result of efforts by numerous individuals from
10 * the Internet community; see the CONTRIBUTORS file for full
11 * details. Many organizations have provided support for Squid's
12 * development; see the SPONSORS file for full details. Squid is
13 * Copyrighted (C) 2001 by the Regents of the University of
14 * California; see the COPYRIGHT file for full details. Squid
15 * incorporates software developed and/or copyrighted by other
16 * sources; see the CREDITS file for full details.
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
26ac0430 22 *
3841dd46 23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
26ac0430 27 *
3841dd46 28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
31 *
3841dd46 32 */
33
582c2af2 34#include "squid.h"
8822ebee 35#include "mgr/Registration.h"
445d18a4
AJ
36#include "radix.h"
37#include "HttpRequest.h"
38#include "StoreClient.h"
39#include "Store.h"
40#include "acl/Acl.h"
3ad63615
AR
41#include "acl/Asn.h"
42#include "acl/Checklist.h"
445d18a4
AJ
43#include "acl/SourceAsn.h"
44#include "acl/DestinationAsn.h"
45#include "acl/DestinationIp.h"
06390e4b 46#include "cache_cf.h"
445d18a4 47#include "HttpReply.h"
714e68b7 48#include "ipcache.h"
445d18a4 49#include "forward.h"
4d5904f7 50#include "SquidConfig.h"
e87137f1 51#include "StoreClient.h"
445d18a4
AJ
52#include "wordlist.h"
53
54#define WHOIS_PORT 43
55#define AS_REQBUF_SZ 4096
56
57/* BEGIN of definitions for radix tree entries */
58
445d18a4
AJ
59/* 32/128 bits address in memory with length */
60class m_ADDR
61{
62public:
63 uint8_t len;
b7ac5457 64 Ip::Address addr;
445d18a4 65
b7ac5457 66 m_ADDR() : len(sizeof(Ip::Address)) {};
445d18a4
AJ
67};
68
69/* END of definitions for radix tree entries */
70
71/* Head for ip to asn radix tree */
72
73struct squid_radix_node_head *AS_tree_head;
74
75/* explicit instantiation required for some systems */
76
77/// \cond AUTODOCS-IGNORE
78template cbdata_type CbDataList<int>::CBDATA_CbDataList;
79/// \endcond
80
81/**
82 * Structure for as number information. it could be simply
83 * a list but it's coded as a structure for future
84 * enhancements (e.g. expires)
85 */
86struct as_info {
87 CbDataList<int> *as_number;
88 time_t expires; /* NOTUSED */
89};
90
91struct ASState {
92 StoreEntry *entry;
93 store_client *sc;
94 HttpRequest *request;
95 int as_number;
96 int64_t offset;
97 int reqofs;
98 char reqbuf[AS_REQBUF_SZ];
99 bool dataRead;
100};
101
102/** entry into the radix tree */
103struct rtentry_t {
104 struct squid_radix_node e_nodes[2];
105 as_info *e_info;
106 m_ADDR e_addr;
107 m_ADDR e_mask;
108};
109
110static int asnAddNet(char *, int);
111
112static void asnCacheStart(int as);
113
114static STCB asHandleReply;
115
062e0b16
FC
116#if defined(__cplusplus)
117extern "C" {
118#endif
119
182fbea5
A
120 static int destroyRadixNode(struct squid_radix_node *rn, void *w);
121 static int printRadixNode(struct squid_radix_node *rn, void *sentry);
445d18a4 122
062e0b16
FC
123#if defined(__cplusplus)
124}
125#endif
126
445d18a4
AJ
127void asnAclInitialize(ACL * acls);
128
129static void asStateFree(void *data);
130
131static void destroyRadixNodeInfo(as_info *);
132
133static OBJH asnStats;
134
135/* PUBLIC */
136
137int
b7ac5457 138asnMatchIp(CbDataList<int> *data, Ip::Address &addr)
445d18a4
AJ
139{
140 struct squid_radix_node *rn;
141 as_info *e;
142 m_ADDR m_addr;
143 CbDataList<int> *a = NULL;
144 CbDataList<int> *b = NULL;
145
146 debugs(53, 3, "asnMatchIp: Called for " << addr );
147
148 if (AS_tree_head == NULL)
149 return 0;
150
151 if (addr.IsNoAddr())
152 return 0;
153
154 if (addr.IsAnyAddr())
155 return 0;
156
157 m_addr.addr = addr;
158
159 rn = squid_rn_match(&m_addr, AS_tree_head);
160
161 if (rn == NULL) {
162 debugs(53, 3, "asnMatchIp: Address not in as db.");
163 return 0;
164 }
165
166 debugs(53, 3, "asnMatchIp: Found in db!");
167 e = ((rtentry_t *) rn)->e_info;
168 assert(e);
169
170 for (a = data; a; a = a->next)
171 for (b = e->as_number; b; b = b->next)
172 if (a->element == b->element) {
173 debugs(53, 5, "asnMatchIp: Found a match!");
174 return 1;
175 }
176
177 debugs(53, 5, "asnMatchIp: AS not in as db.");
178 return 0;
179}
180
181void
182ACLASN::prepareForUse()
183{
184 for (CbDataList<int> *i = data; i; i = i->
185 next)
186 asnCacheStart(i->element);
187}
188
189static void
190asnRegisterWithCacheManager(void)
191{
8822ebee 192 Mgr::RegisterAction("asndb", "AS Number Database", asnStats, 0, 1);
445d18a4
AJ
193}
194
195/* initialize the radix tree structure */
196
197SQUIDCEXTERN int squid_max_keylen; /* yuck.. this is in lib/radix.c */
198
199CBDATA_TYPE(ASState);
200void
201asnInit(void)
202{
742a021b 203 static bool inited = false;
445d18a4
AJ
204 squid_max_keylen = 40;
205 CBDATA_INIT_TYPE(ASState);
206
742a021b
FC
207 if (!inited) {
208 inited = true;
445d18a4 209 squid_rn_init();
742a021b 210 }
445d18a4
AJ
211
212 squid_rn_inithead(&AS_tree_head, 8);
213
214 asnRegisterWithCacheManager();
215}
216
217void
218asnFreeMemory(void)
219{
220 squid_rn_walktree(AS_tree_head, destroyRadixNode, AS_tree_head);
221
222 destroyRadixNode((struct squid_radix_node *) 0, (void *) AS_tree_head);
223}
224
225static void
226asnStats(StoreEntry * sentry)
227{
228 storeAppendPrintf(sentry, "Address \tAS Numbers\n");
229 squid_rn_walktree(AS_tree_head, printRadixNode, sentry);
230}
231
232/* PRIVATE */
233
445d18a4
AJ
234static void
235asnCacheStart(int as)
236{
237 LOCAL_ARRAY(char, asres, 4096);
238 StoreEntry *e;
239 HttpRequest *req;
240 ASState *asState;
241 asState = cbdataAlloc(ASState);
242 asState->dataRead = 0;
243 debugs(53, 3, "asnCacheStart: AS " << as);
244 snprintf(asres, 4096, "whois://%s/!gAS%d", Config.as_whois_server, as);
245 asState->as_number = as;
246 req = HttpRequest::CreateFromUrl(asres);
247 assert(NULL != req);
248 asState->request = HTTPMSGLOCK(req);
249
250 if ((e = storeGetPublic(asres, METHOD_GET)) == NULL) {
251 e = storeCreateEntry(asres, asres, request_flags(), METHOD_GET);
252 asState->sc = storeClientListAdd(e, asState);
e83cc785 253 FwdState::fwdStart(Comm::ConnectionPointer(), e, asState->request);
445d18a4
AJ
254 } else {
255
256 e->lock();
257 asState->sc = storeClientListAdd(e, asState);
258 }
259
260 asState->entry = e;
261 asState->offset = 0;
262 asState->reqofs = 0;
263 StoreIOBuffer readBuffer (AS_REQBUF_SZ, asState->offset, asState->reqbuf);
264 storeClientCopy(asState->sc,
265 e,
266 readBuffer,
267 asHandleReply,
268 asState);
269}
270
271static void
272asHandleReply(void *data, StoreIOBuffer result)
273{
274 ASState *asState = (ASState *)data;
275 StoreEntry *e = asState->entry;
276 char *s;
277 char *t;
278 char *buf = asState->reqbuf;
279 int leftoversz = -1;
280
281 debugs(53, 3, "asHandleReply: Called with size=" << (unsigned int)result.length);
282 debugs(53, 3, "asHandleReply: buffer='" << buf << "'");
283
284 /* First figure out whether we should abort the request */
285
286 if (EBIT_TEST(e->flags, ENTRY_ABORTED)) {
287 asStateFree(asState);
288 return;
289 }
290
291 if (result.length == 0 && asState->dataRead) {
292 debugs(53, 3, "asHandleReply: Done: " << e->url() );
293 asStateFree(asState);
294 return;
295 } else if (result.flags.error) {
e0236918 296 debugs(53, DBG_IMPORTANT, "asHandleReply: Called with Error set and size=" << (unsigned int) result.length);
445d18a4
AJ
297 asStateFree(asState);
298 return;
299 } else if (HTTP_OK != e->getReply()->sline.status) {
e0236918 300 debugs(53, DBG_IMPORTANT, "WARNING: AS " << asState->as_number << " whois request failed");
445d18a4
AJ
301 asStateFree(asState);
302 return;
303 }
304
305 /*
306 * Next, attempt to parse our request
307 * Remembering that the actual buffer size is retsize + reqofs!
308 */
309 s = buf;
310
311 while ((size_t)(s - buf) < result.length + asState->reqofs && *s != '\0') {
312 while (*s && xisspace(*s))
742a021b 313 ++s;
445d18a4 314
742a021b 315 for (t = s; *t; ++t) {
445d18a4
AJ
316 if (xisspace(*t))
317 break;
318 }
319
320 if (*t == '\0') {
321 /* oof, word should continue on next block */
322 break;
323 }
324
325 *t = '\0';
326 debugs(53, 3, "asHandleReply: AS# " << s << " (" << asState->as_number << ")");
327 asnAddNet(s, asState->as_number);
328 s = t + 1;
329 asState->dataRead = 1;
330 }
331
332 /*
333 * Next, grab the end of the 'valid data' in the buffer, and figure
334 * out how much data is left in our buffer, which we need to keep
335 * around for the next request
336 */
337 leftoversz = (asState->reqofs + result.length) - (s - buf);
338
339 assert(leftoversz >= 0);
340
341 /*
342 * Next, copy the left over data, from s to s + leftoversz to the
343 * beginning of the buffer
344 */
41d00cd3 345 memmove(buf, s, leftoversz);
445d18a4
AJ
346
347 /*
348 * Next, update our offset and reqofs, and kick off a copy if required
349 */
350 asState->offset += result.length;
351
352 asState->reqofs = leftoversz;
353
354 debugs(53, 3, "asState->offset = " << asState->offset);
355
356 if (e->store_status == STORE_PENDING) {
357 debugs(53, 3, "asHandleReply: store_status == STORE_PENDING: " << e->url() );
358 StoreIOBuffer tempBuffer (AS_REQBUF_SZ - asState->reqofs,
359 asState->offset,
360 asState->reqbuf + asState->reqofs);
361 storeClientCopy(asState->sc,
362 e,
363 tempBuffer,
364 asHandleReply,
365 asState);
366 } else {
367 StoreIOBuffer tempBuffer;
368 debugs(53, 3, "asHandleReply: store complete, but data received " << e->url() );
369 tempBuffer.offset = asState->offset;
370 tempBuffer.length = AS_REQBUF_SZ - asState->reqofs;
371 tempBuffer.data = asState->reqbuf + asState->reqofs;
372 storeClientCopy(asState->sc,
373 e,
374 tempBuffer,
375 asHandleReply,
376 asState);
377 }
378}
379
380static void
381asStateFree(void *data)
382{
383 ASState *asState = (ASState *)data;
384 debugs(53, 3, "asnStateFree: " << asState->entry->url() );
385 storeUnregister(asState->sc, asState->entry, asState);
386 asState->entry->unlock();
387 HTTPMSGUNLOCK(asState->request);
388 cbdataFree(asState);
389}
390
445d18a4
AJ
391/**
392 * add a network (addr, mask) to the radix tree, with matching AS number
393 */
394static int
395asnAddNet(char *as_string, int as_number)
396{
397 rtentry_t *e;
398
399 struct squid_radix_node *rn;
400 CbDataList<int> **Tail = NULL;
401 CbDataList<int> *q = NULL;
402 as_info *asinfo = NULL;
403
b7ac5457
AJ
404 Ip::Address mask;
405 Ip::Address addr;
445d18a4
AJ
406 char *t;
407 int bitl;
408
409 t = strchr(as_string, '/');
410
411 if (t == NULL) {
412 debugs(53, 3, "asnAddNet: failed, invalid response from whois server.");
413 return 0;
414 }
415
416 *t = '\0';
417 addr = as_string;
418 bitl = atoi(t + 1);
419
420 if (bitl < 0)
421 bitl = 0;
422
423 // INET6 TODO : find a better way of identifying the base IPA family for mask than this.
424 t = strchr(as_string, '.');
425
426 // generate Netbits Format Mask
427 mask.SetNoAddr();
428 mask.ApplyMask(bitl, (t!=NULL?AF_INET:AF_INET6) );
429
430 debugs(53, 3, "asnAddNet: called for " << addr << "/" << mask );
431
432 e = (rtentry_t *)xmalloc(sizeof(rtentry_t));
433
434 memset(e, '\0', sizeof(rtentry_t));
435
436 e->e_addr.addr = addr;
437
438 e->e_mask.addr = mask;
439
440 rn = squid_rn_lookup(&e->e_addr, &e->e_mask, AS_tree_head);
441
442 if (rn != NULL) {
443 asinfo = ((rtentry_t *) rn)->e_info;
444
445 if (asinfo->as_number->find(as_number)) {
446 debugs(53, 3, "asnAddNet: Ignoring repeated network '" << addr << "/" << bitl << "' for AS " << as_number);
447 } else {
448 debugs(53, 3, "asnAddNet: Warning: Found a network with multiple AS numbers!");
449
450 for (Tail = &asinfo->as_number; *Tail; Tail = &(*Tail)->next);
451 q = new CbDataList<int> (as_number);
452
453 *(Tail) = q;
454
455 e->e_info = asinfo;
456 }
457 } else {
458 q = new CbDataList<int> (as_number);
459 asinfo = (as_info *)xmalloc(sizeof(as_info));
460 asinfo->as_number = q;
461 rn = squid_rn_addroute(&e->e_addr, &e->e_mask, AS_tree_head, e->e_nodes);
462 rn = squid_rn_match(&e->e_addr, AS_tree_head);
463 assert(rn != NULL);
464 e->e_info = asinfo;
465 }
466
467 if (rn == 0) { /* assert might expand to nothing */
468 xfree(asinfo);
469 delete q;
470 xfree(e);
471 debugs(53, 3, "asnAddNet: Could not add entry.");
472 return 0;
473 }
474
475 e->e_info = asinfo;
476 return 1;
477}
478
479static int
480destroyRadixNode(struct squid_radix_node *rn, void *w)
481{
482
483 struct squid_radix_node_head *rnh = (struct squid_radix_node_head *) w;
484
485 if (rn && !(rn->rn_flags & RNF_ROOT)) {
486 rtentry_t *e = (rtentry_t *) rn;
487 rn = squid_rn_delete(rn->rn_key, rn->rn_mask, rnh);
488
489 if (rn == 0)
490 debugs(53, 3, "destroyRadixNode: internal screwup");
491
492 destroyRadixNodeInfo(e->e_info);
493
494 xfree(rn);
495 }
496
497 return 1;
498}
499
500static void
501destroyRadixNodeInfo(as_info * e_info)
502{
503 CbDataList<int> *prev = NULL;
504 CbDataList<int> *data = e_info->as_number;
505
506 while (data) {
507 prev = data;
508 data = data->next;
509 delete prev;
510 }
511
512 delete data;
513}
514
515static int
516printRadixNode(struct squid_radix_node *rn, void *_sentry)
517{
518 StoreEntry *sentry = (StoreEntry *)_sentry;
519 rtentry_t *e = (rtentry_t *) rn;
520 CbDataList<int> *q;
521 as_info *asinfo;
522 char buf[MAX_IPSTRLEN];
b7ac5457
AJ
523 Ip::Address addr;
524 Ip::Address mask;
445d18a4
AJ
525
526 assert(e);
527 assert(e->e_info);
528 addr = e->e_addr.addr;
529 mask = e->e_mask.addr;
530 storeAppendPrintf(sentry, "%s/%d\t",
531 addr.NtoA(buf, MAX_IPSTRLEN),
532 mask.GetCIDR() );
533 asinfo = e->e_info;
534 assert(asinfo->as_number);
535
536 for (q = asinfo->as_number; q; q = q->next)
537 storeAppendPrintf(sentry, " %d", q->element);
538
539 storeAppendPrintf(sentry, "\n");
540
541 return 0;
542}
543
544ACLASN::~ACLASN()
545{
546 if (data)
547 delete data;
548}
549
550bool
551
b7ac5457 552ACLASN::match(Ip::Address toMatch)
445d18a4
AJ
553{
554 return asnMatchIp(data, toMatch);
555}
556
557wordlist *
558ACLASN::dump()
559{
560 wordlist *W = NULL;
561 char buf[32];
562 CbDataList<int> *ldata = data;
563
564 while (ldata != NULL) {
565 snprintf(buf, sizeof(buf), "%d", ldata->element);
566 wordlistAdd(&W, buf);
567 ldata = ldata->next;
568 }
569
570 return W;
571}
572
573bool
574ACLASN::empty () const
575{
576 return data == NULL;
577}
578
579void
580ACLASN::parse()
581{
582 CbDataList<int> **curlist = &data;
583 CbDataList<int> **Tail;
584 CbDataList<int> *q = NULL;
585 char *t = NULL;
586
587 for (Tail = curlist; *Tail; Tail = &((*Tail)->next));
588 while ((t = strtokFile())) {
589 q = new CbDataList<int> (atoi(t));
590 *(Tail) = q;
591 Tail = &q->next;
592 }
593}
594
b7ac5457 595ACLData<Ip::Address> *
445d18a4
AJ
596ACLASN::clone() const
597{
598 if (data)
599 fatal ("cloning of ACLASN not implemented");
600
601 return new ACLASN(*this);
602}
603
604/* explicit template instantiation required for some systems */
605
b7ac5457 606template class ACLStrategised<Ip::Address>;
445d18a4
AJ
607
608ACL::Prototype ACLASN::SourceRegistryProtoype(&ACLASN::SourceRegistryEntry_, "src_as");
609
b7ac5457 610ACLStrategised<Ip::Address> ACLASN::SourceRegistryEntry_(new ACLASN, ACLSourceASNStrategy::Instance(), "src_as");
445d18a4
AJ
611
612ACL::Prototype ACLASN::DestinationRegistryProtoype(&ACLASN::DestinationRegistryEntry_, "dst_as");
613
b7ac5457 614ACLStrategised<Ip::Address> ACLASN::DestinationRegistryEntry_(new ACLASN, ACLDestinationASNStrategy::Instance(), "dst_as");
445d18a4
AJ
615
616int
b7ac5457 617ACLSourceASNStrategy::match (ACLData<Ip::Address> * &data, ACLFilledChecklist *checklist)
445d18a4
AJ
618{
619 return data->match(checklist->src_addr);
620}
621
622ACLSourceASNStrategy *
623ACLSourceASNStrategy::Instance()
624{
625 return &Instance_;
626}
627
628ACLSourceASNStrategy ACLSourceASNStrategy::Instance_;
629
445d18a4
AJ
630int
631ACLDestinationASNStrategy::match (ACLData<MatchType> * &data, ACLFilledChecklist *checklist)
632{
633 const ipcache_addrs *ia = ipcache_gethostbyname(checklist->request->GetHost(), IP_LOOKUP_IF_MISS);
634
635 if (ia) {
742a021b 636 for (int k = 0; k < (int) ia->count; ++k) {
445d18a4
AJ
637 if (data->match(ia->in_addrs[k]))
638 return 1;
639 }
640
641 return 0;
642
643 } else if (!checklist->request->flags.destinationIPLookedUp()) {
644 /* No entry in cache, lookup not attempted */
645 /* XXX FIXME: allow accessing the acl name here */
646 debugs(28, 3, "asnMatchAcl: Can't yet compare '" << "unknown" /*name*/ << "' ACL for '" << checklist->request->GetHost() << "'");
647 checklist->changeState (DestinationIPLookup::Instance());
648 } else {
b7ac5457 649 Ip::Address noaddr;
445d18a4
AJ
650 noaddr.SetNoAddr();
651 return data->match(noaddr);
652 }
653
654 return 0;
655}
656
657ACLDestinationASNStrategy *
658ACLDestinationASNStrategy::Instance()
659{
660 return &Instance_;
661}
3841dd46 662
445d18a4 663ACLDestinationASNStrategy ACLDestinationASNStrategy::Instance_;