Fix a compile warning when using clang
[tor/rransom.git] / src / or / policies.c
blobf8c36c784ba0734cbcb6dfb9e526c249cf864c8b
1 /* Copyright (c) 2001-2004, Roger Dingledine.
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2011, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
6 /**
7 * \file policies.c
8 * \brief Code to parse and use address policies and exit policies.
9 **/
11 #include "or.h"
12 #include "ht.h"
14 /** Policy that addresses for incoming SOCKS connections must match. */
15 static smartlist_t *socks_policy = NULL;
16 /** Policy that addresses for incoming directory connections must match. */
17 static smartlist_t *dir_policy = NULL;
18 /** Policy that addresses for incoming router descriptors must match in order
19 * to be published by us. */
20 static smartlist_t *authdir_reject_policy = NULL;
21 /** Policy that addresses for incoming router descriptors must match in order
22 * to be marked as valid in our networkstatus. */
23 static smartlist_t *authdir_invalid_policy = NULL;
24 /** Policy that addresses for incoming router descriptors must <b>not</b>
25 * match in order to not be marked as BadDirectory. */
26 static smartlist_t *authdir_baddir_policy = NULL;
27 /** Policy that addresses for incoming router descriptors must <b>not</b>
28 * match in order to not be marked as BadExit. */
29 static smartlist_t *authdir_badexit_policy = NULL;
31 /** Parsed addr_policy_t describing which addresses we believe we can start
32 * circuits at. */
33 static smartlist_t *reachable_or_addr_policy = NULL;
34 /** Parsed addr_policy_t describing which addresses we believe we can connect
35 * to directories at. */
36 static smartlist_t *reachable_dir_addr_policy = NULL;
38 /** Element of an exit policy summary */
39 typedef struct policy_summary_item_t {
40 uint16_t prt_min; /**< Lowest port number to accept/reject. */
41 uint16_t prt_max; /**< Highest port number to accept/reject. */
42 uint64_t reject_count; /**< Number of IP-Addresses that are rejected to
43 this port range. */
44 int accepted:1; /** Has this port already been accepted */
45 } policy_summary_item_t;
47 /** Private networks. This list is used in two places, once to expand the
48 * "private" keyword when parsing our own exit policy, secondly to ignore
49 * just such networks when building exit policy summaries. It is important
50 * that all authorities agree on that list when creating summaries, so don't
51 * just change this without a proper migration plan and a proposal and stuff.
53 static const char *private_nets[] = {
54 "0.0.0.0/8", "169.254.0.0/16",
55 "127.0.0.0/8", "192.168.0.0/16", "10.0.0.0/8", "172.16.0.0/12",
56 // "fc00::/7", "fe80::/10", "fec0::/10", "::/127",
57 NULL };
59 /** Replace all "private" entries in *<b>policy</b> with their expanded
60 * equivalents. */
61 void
62 policy_expand_private(smartlist_t **policy)
64 uint16_t port_min, port_max;
66 int i;
67 smartlist_t *tmp;
69 if (!*policy) /*XXXX disallow NULL policies? */
70 return;
72 tmp = smartlist_create();
74 SMARTLIST_FOREACH(*policy, addr_policy_t *, p,
76 if (! p->is_private) {
77 smartlist_add(tmp, p);
78 continue;
80 for (i = 0; private_nets[i]; ++i) {
81 addr_policy_t policy;
82 memcpy(&policy, p, sizeof(addr_policy_t));
83 policy.is_private = 0;
84 policy.is_canonical = 0;
85 if (tor_addr_parse_mask_ports(private_nets[i], &policy.addr,
86 &policy.maskbits, &port_min, &port_max)<0) {
87 tor_assert(0);
89 smartlist_add(tmp, addr_policy_get_canonical_entry(&policy));
91 addr_policy_free(p);
92 });
94 smartlist_free(*policy);
95 *policy = tmp;
98 /**
99 * Given a linked list of config lines containing "allow" and "deny"
100 * tokens, parse them and append the result to <b>dest</b>. Return -1
101 * if any tokens are malformed (and don't append any), else return 0.
103 * If <b>assume_action</b> is nonnegative, then insert its action
104 * (ADDR_POLICY_ACCEPT or ADDR_POLICY_REJECT) for items that specify no
105 * action.
107 static int
108 parse_addr_policy(config_line_t *cfg, smartlist_t **dest,
109 int assume_action)
111 smartlist_t *result;
112 smartlist_t *entries;
113 addr_policy_t *item;
114 int r = 0;
116 if (!cfg)
117 return 0;
119 result = smartlist_create();
120 entries = smartlist_create();
121 for (; cfg; cfg = cfg->next) {
122 smartlist_split_string(entries, cfg->value, ",",
123 SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
124 SMARTLIST_FOREACH(entries, const char *, ent,
126 log_debug(LD_CONFIG,"Adding new entry '%s'",ent);
127 item = router_parse_addr_policy_item_from_string(ent, assume_action);
128 if (item) {
129 smartlist_add(result, item);
130 } else {
131 log_warn(LD_CONFIG,"Malformed policy '%s'.", ent);
132 r = -1;
135 SMARTLIST_FOREACH(entries, char *, ent, tor_free(ent));
136 smartlist_clear(entries);
138 smartlist_free(entries);
139 if (r == -1) {
140 addr_policy_list_free(result);
141 } else {
142 policy_expand_private(&result);
144 if (*dest) {
145 smartlist_add_all(*dest, result);
146 smartlist_free(result);
147 } else {
148 *dest = result;
152 return r;
155 /** Helper: parse the Reachable(Dir|OR)?Addresses fields into
156 * reachable_(or|dir)_addr_policy. The options should already have
157 * been validated by validate_addr_policies.
159 static int
160 parse_reachable_addresses(void)
162 or_options_t *options = get_options();
163 int ret = 0;
165 if (options->ReachableDirAddresses &&
166 options->ReachableORAddresses &&
167 options->ReachableAddresses) {
168 log_warn(LD_CONFIG,
169 "Both ReachableDirAddresses and ReachableORAddresses are set. "
170 "ReachableAddresses setting will be ignored.");
172 addr_policy_list_free(reachable_or_addr_policy);
173 reachable_or_addr_policy = NULL;
174 if (!options->ReachableORAddresses && options->ReachableAddresses)
175 log_info(LD_CONFIG,
176 "Using ReachableAddresses as ReachableORAddresses.");
177 if (parse_addr_policy(options->ReachableORAddresses ?
178 options->ReachableORAddresses :
179 options->ReachableAddresses,
180 &reachable_or_addr_policy, ADDR_POLICY_ACCEPT)) {
181 log_warn(LD_CONFIG,
182 "Error parsing Reachable%sAddresses entry; ignoring.",
183 options->ReachableORAddresses ? "OR" : "");
184 ret = -1;
187 addr_policy_list_free(reachable_dir_addr_policy);
188 reachable_dir_addr_policy = NULL;
189 if (!options->ReachableDirAddresses && options->ReachableAddresses)
190 log_info(LD_CONFIG,
191 "Using ReachableAddresses as ReachableDirAddresses");
192 if (parse_addr_policy(options->ReachableDirAddresses ?
193 options->ReachableDirAddresses :
194 options->ReachableAddresses,
195 &reachable_dir_addr_policy, ADDR_POLICY_ACCEPT)) {
196 if (options->ReachableDirAddresses)
197 log_warn(LD_CONFIG,
198 "Error parsing ReachableDirAddresses entry; ignoring.");
199 ret = -1;
201 return ret;
204 /** Return true iff the firewall options might block any address:port
205 * combination.
208 firewall_is_fascist_or(void)
210 return reachable_or_addr_policy != NULL;
213 /** Return true iff <b>policy</b> (possibly NULL) will allow a
214 * connection to <b>addr</b>:<b>port</b>.
216 static int
217 addr_policy_permits_tor_addr(const tor_addr_t *addr, uint16_t port,
218 smartlist_t *policy)
220 addr_policy_result_t p;
221 p = compare_tor_addr_to_addr_policy(addr, port, policy);
222 switch (p) {
223 case ADDR_POLICY_PROBABLY_ACCEPTED:
224 case ADDR_POLICY_ACCEPTED:
225 return 1;
226 case ADDR_POLICY_PROBABLY_REJECTED:
227 case ADDR_POLICY_REJECTED:
228 return 0;
229 default:
230 log_warn(LD_BUG, "Unexpected result: %d", (int)p);
231 return 0;
235 /** Return true iff <b> policy</b> (possibly NULL) will allow a connection to
236 * <b>addr</b>:<b>port</b>. <b>addr</b> is an IPv4 address given in host
237 * order. */
238 /* XXXX deprecate when possible. */
239 static int
240 addr_policy_permits_address(uint32_t addr, uint16_t port,
241 smartlist_t *policy)
243 tor_addr_t a;
244 tor_addr_from_ipv4h(&a, addr);
245 return addr_policy_permits_tor_addr(&a, port, policy);
248 /** Return true iff we think our firewall will let us make an OR connection to
249 * addr:port. */
251 fascist_firewall_allows_address_or(const tor_addr_t *addr, uint16_t port)
253 return addr_policy_permits_tor_addr(addr, port,
254 reachable_or_addr_policy);
257 /** Return true iff we think our firewall will let us make an OR connection to
258 * <b>ri</b>. */
260 fascist_firewall_allows_or(routerinfo_t *ri)
262 /* XXXX proposal 118 */
263 tor_addr_t addr;
264 tor_addr_from_ipv4h(&addr, ri->addr);
265 return fascist_firewall_allows_address_or(&addr, ri->or_port);
268 /** Return true iff we think our firewall will let us make a directory
269 * connection to addr:port. */
271 fascist_firewall_allows_address_dir(const tor_addr_t *addr, uint16_t port)
273 return addr_policy_permits_tor_addr(addr, port,
274 reachable_dir_addr_policy);
277 /** Return 1 if <b>addr</b> is permitted to connect to our dir port,
278 * based on <b>dir_policy</b>. Else return 0.
281 dir_policy_permits_address(const tor_addr_t *addr)
283 return addr_policy_permits_tor_addr(addr, 1, dir_policy);
286 /** Return 1 if <b>addr</b> is permitted to connect to our socks port,
287 * based on <b>socks_policy</b>. Else return 0.
290 socks_policy_permits_address(const tor_addr_t *addr)
292 return addr_policy_permits_tor_addr(addr, 1, socks_policy);
295 /** Return 1 if <b>addr</b>:<b>port</b> is permitted to publish to our
296 * directory, based on <b>authdir_reject_policy</b>. Else return 0.
299 authdir_policy_permits_address(uint32_t addr, uint16_t port)
301 return addr_policy_permits_address(addr, port, authdir_reject_policy);
304 /** Return 1 if <b>addr</b>:<b>port</b> is considered valid in our
305 * directory, based on <b>authdir_invalid_policy</b>. Else return 0.
308 authdir_policy_valid_address(uint32_t addr, uint16_t port)
310 return addr_policy_permits_address(addr, port, authdir_invalid_policy);
313 /** Return 1 if <b>addr</b>:<b>port</b> should be marked as a bad dir,
314 * based on <b>authdir_baddir_policy</b>. Else return 0.
317 authdir_policy_baddir_address(uint32_t addr, uint16_t port)
319 return ! addr_policy_permits_address(addr, port, authdir_baddir_policy);
322 /** Return 1 if <b>addr</b>:<b>port</b> should be marked as a bad exit,
323 * based on <b>authdir_badexit_policy</b>. Else return 0.
326 authdir_policy_badexit_address(uint32_t addr, uint16_t port)
328 return ! addr_policy_permits_address(addr, port, authdir_badexit_policy);
331 #define REJECT(arg) \
332 STMT_BEGIN *msg = tor_strdup(arg); goto err; STMT_END
334 /** Config helper: If there's any problem with the policy configuration
335 * options in <b>options</b>, return -1 and set <b>msg</b> to a newly
336 * allocated description of the error. Else return 0. */
338 validate_addr_policies(or_options_t *options, char **msg)
340 /* XXXX Maybe merge this into parse_policies_from_options, to make sure
341 * that the two can't go out of sync. */
343 smartlist_t *addr_policy=NULL;
344 *msg = NULL;
346 if (policies_parse_exit_policy(options->ExitPolicy, &addr_policy,
347 options->ExitPolicyRejectPrivate, NULL))
348 REJECT("Error in ExitPolicy entry.");
350 /* The rest of these calls *append* to addr_policy. So don't actually
351 * use the results for anything other than checking if they parse! */
352 if (parse_addr_policy(options->DirPolicy, &addr_policy, -1))
353 REJECT("Error in DirPolicy entry.");
354 if (parse_addr_policy(options->SocksPolicy, &addr_policy, -1))
355 REJECT("Error in SocksPolicy entry.");
356 if (parse_addr_policy(options->AuthDirReject, &addr_policy,
357 ADDR_POLICY_REJECT))
358 REJECT("Error in AuthDirReject entry.");
359 if (parse_addr_policy(options->AuthDirInvalid, &addr_policy,
360 ADDR_POLICY_REJECT))
361 REJECT("Error in AuthDirInvalid entry.");
362 if (parse_addr_policy(options->AuthDirBadDir, &addr_policy,
363 ADDR_POLICY_REJECT))
364 REJECT("Error in AuthDirBadDir entry.");
365 if (parse_addr_policy(options->AuthDirBadExit, &addr_policy,
366 ADDR_POLICY_REJECT))
367 REJECT("Error in AuthDirBadExit entry.");
369 if (parse_addr_policy(options->ReachableAddresses, &addr_policy,
370 ADDR_POLICY_ACCEPT))
371 REJECT("Error in ReachableAddresses entry.");
372 if (parse_addr_policy(options->ReachableORAddresses, &addr_policy,
373 ADDR_POLICY_ACCEPT))
374 REJECT("Error in ReachableORAddresses entry.");
375 if (parse_addr_policy(options->ReachableDirAddresses, &addr_policy,
376 ADDR_POLICY_ACCEPT))
377 REJECT("Error in ReachableDirAddresses entry.");
378 if (parse_addr_policy(options->AuthDirReject, &addr_policy,
379 ADDR_POLICY_REJECT))
380 REJECT("Error in AuthDirReject entry.");
381 if (parse_addr_policy(options->AuthDirInvalid, &addr_policy,
382 ADDR_POLICY_REJECT))
383 REJECT("Error in AuthDirInvalid entry.");
385 err:
386 addr_policy_list_free(addr_policy);
387 return *msg ? -1 : 0;
388 #undef REJECT
391 /** Parse <b>string</b> in the same way that the exit policy
392 * is parsed, and put the processed version in *<b>policy</b>.
393 * Ignore port specifiers.
395 static int
396 load_policy_from_option(config_line_t *config, smartlist_t **policy,
397 int assume_action)
399 int r;
400 addr_policy_list_free(*policy);
401 *policy = NULL;
402 r = parse_addr_policy(config, policy, assume_action);
403 if (r < 0) {
404 return -1;
406 if (*policy) {
407 SMARTLIST_FOREACH_BEGIN(*policy, addr_policy_t *, n) {
408 /* ports aren't used in these. */
409 if (n->prt_min > 1 || n->prt_max != 65535) {
410 addr_policy_t newp, *c;
411 memcpy(&newp, n, sizeof(newp));
412 newp.prt_min = 1;
413 newp.prt_max = 65535;
414 newp.is_canonical = 0;
415 c = addr_policy_get_canonical_entry(&newp);
416 SMARTLIST_REPLACE_CURRENT(*policy, n, c);
417 addr_policy_free(n);
419 } SMARTLIST_FOREACH_END(n);
421 return 0;
424 /** Set all policies based on <b>options</b>, which should have been validated
425 * first by validate_addr_policies. */
427 policies_parse_from_options(or_options_t *options)
429 int ret = 0;
430 if (load_policy_from_option(options->SocksPolicy, &socks_policy, -1) < 0)
431 ret = -1;
432 if (load_policy_from_option(options->DirPolicy, &dir_policy, -1) < 0)
433 ret = -1;
434 if (load_policy_from_option(options->AuthDirReject,
435 &authdir_reject_policy, ADDR_POLICY_REJECT) < 0)
436 ret = -1;
437 if (load_policy_from_option(options->AuthDirInvalid,
438 &authdir_invalid_policy, ADDR_POLICY_REJECT) < 0)
439 ret = -1;
440 if (load_policy_from_option(options->AuthDirBadDir,
441 &authdir_baddir_policy, ADDR_POLICY_REJECT) < 0)
442 ret = -1;
443 if (load_policy_from_option(options->AuthDirBadExit,
444 &authdir_badexit_policy, ADDR_POLICY_REJECT) < 0)
445 ret = -1;
446 if (parse_reachable_addresses() < 0)
447 ret = -1;
448 return ret;
451 /** Compare two provided address policy items, and return -1, 0, or 1
452 * if the first is less than, equal to, or greater than the second. */
453 static int
454 cmp_single_addr_policy(addr_policy_t *a, addr_policy_t *b)
456 int r;
457 if ((r=((int)a->policy_type - (int)b->policy_type)))
458 return r;
459 if ((r=((int)a->is_private - (int)b->is_private)))
460 return r;
461 if ((r=tor_addr_compare(&a->addr, &b->addr, CMP_EXACT)))
462 return r;
463 if ((r=((int)a->maskbits - (int)b->maskbits)))
464 return r;
465 if ((r=((int)a->prt_min - (int)b->prt_min)))
466 return r;
467 if ((r=((int)a->prt_max - (int)b->prt_max)))
468 return r;
469 return 0;
472 /** Like cmp_single_addr_policy() above, but looks at the
473 * whole set of policies in each case. */
475 cmp_addr_policies(smartlist_t *a, smartlist_t *b)
477 int r, i;
478 int len_a = a ? smartlist_len(a) : 0;
479 int len_b = b ? smartlist_len(b) : 0;
481 for (i = 0; i < len_a && i < len_b; ++i) {
482 if ((r = cmp_single_addr_policy(smartlist_get(a, i), smartlist_get(b, i))))
483 return r;
485 if (i == len_a && i == len_b)
486 return 0;
487 if (i < len_a)
488 return -1;
489 else
490 return 1;
493 /** Node in hashtable used to store address policy entries. */
494 typedef struct policy_map_ent_t {
495 HT_ENTRY(policy_map_ent_t) node;
496 addr_policy_t *policy;
497 } policy_map_ent_t;
499 static HT_HEAD(policy_map, policy_map_ent_t) policy_root = HT_INITIALIZER();
501 /** Return true iff a and b are equal. */
502 static INLINE int
503 policy_eq(policy_map_ent_t *a, policy_map_ent_t *b)
505 return cmp_single_addr_policy(a->policy, b->policy) == 0;
508 /** Return a hashcode for <b>ent</b> */
509 static unsigned int
510 policy_hash(policy_map_ent_t *ent)
512 addr_policy_t *a = ent->policy;
513 unsigned int r;
514 if (a->is_private)
515 r = 0x1234abcd;
516 else
517 r = tor_addr_hash(&a->addr);
518 r += a->prt_min << 8;
519 r += a->prt_max << 16;
520 r += a->maskbits;
521 if (a->policy_type == ADDR_POLICY_REJECT)
522 r ^= 0xffffffff;
524 return r;
527 HT_PROTOTYPE(policy_map, policy_map_ent_t, node, policy_hash,
528 policy_eq)
529 HT_GENERATE(policy_map, policy_map_ent_t, node, policy_hash,
530 policy_eq, 0.6, malloc, realloc, free)
532 /** Given a pointer to an addr_policy_t, return a copy of the pointer to the
533 * "canonical" copy of that addr_policy_t; the canonical copy is a single
534 * reference-counted object. */
535 addr_policy_t *
536 addr_policy_get_canonical_entry(addr_policy_t *e)
538 policy_map_ent_t search, *found;
539 if (e->is_canonical)
540 return e;
542 search.policy = e;
543 found = HT_FIND(policy_map, &policy_root, &search);
544 if (!found) {
545 found = tor_malloc_zero(sizeof(policy_map_ent_t));
546 found->policy = tor_memdup(e, sizeof(addr_policy_t));
547 found->policy->is_canonical = 1;
548 found->policy->refcnt = 0;
549 HT_INSERT(policy_map, &policy_root, found);
552 tor_assert(!cmp_single_addr_policy(found->policy, e));
553 ++found->policy->refcnt;
554 return found->policy;
557 /** As compare_tor_addr_to_addr_policy, but instead of a tor_addr_t, takes
558 * in host order. */
559 addr_policy_result_t
560 compare_addr_to_addr_policy(uint32_t addr, uint16_t port,
561 const smartlist_t *policy)
563 /*XXXX deprecate this function when possible. */
564 tor_addr_t a;
565 tor_addr_from_ipv4h(&a, addr);
566 return compare_tor_addr_to_addr_policy(&a, port, policy);
569 /** Helper for compare_tor_addr_to_addr_policy. Implements the case where
570 * addr and port are both known. */
571 static addr_policy_result_t
572 compare_known_tor_addr_to_addr_policy(const tor_addr_t *addr, uint16_t port,
573 const smartlist_t *policy)
575 /* We know the address and port, and we know the policy, so we can just
576 * compute an exact match. */
577 SMARTLIST_FOREACH_BEGIN(policy, addr_policy_t *, tmpe) {
578 /* Address is known */
579 if (!tor_addr_compare_masked(addr, &tmpe->addr, tmpe->maskbits,
580 CMP_EXACT)) {
581 if (port >= tmpe->prt_min && port <= tmpe->prt_max) {
582 /* Exact match for the policy */
583 return tmpe->policy_type == ADDR_POLICY_ACCEPT ?
584 ADDR_POLICY_ACCEPTED : ADDR_POLICY_REJECTED;
587 } SMARTLIST_FOREACH_END(tmpe);
589 /* accept all by default. */
590 return ADDR_POLICY_ACCEPTED;
593 /** Helper for compare_tor_addr_to_addr_policy. Implements the case where
594 * addr is known but port is not. */
595 static addr_policy_result_t
596 compare_known_tor_addr_to_addr_policy_noport(const tor_addr_t *addr,
597 const smartlist_t *policy)
599 /* We look to see if there's a definite match. If so, we return that
600 match's value, unless there's an intervening possible match that says
601 something different. */
602 int maybe_accept = 0, maybe_reject = 0;
604 SMARTLIST_FOREACH_BEGIN(policy, addr_policy_t *, tmpe) {
605 if (!tor_addr_compare_masked(addr, &tmpe->addr, tmpe->maskbits,
606 CMP_EXACT)) {
607 if (tmpe->prt_min <= 1 && tmpe->prt_max >= 65535) {
608 /* Definitely matches, since it covers all ports. */
609 if (tmpe->policy_type == ADDR_POLICY_ACCEPT) {
610 /* If we already hit a clause that might trigger a 'reject', than we
611 * can't be sure of this certain 'accept'.*/
612 return maybe_reject ? ADDR_POLICY_PROBABLY_ACCEPTED :
613 ADDR_POLICY_ACCEPTED;
614 } else {
615 return maybe_accept ? ADDR_POLICY_PROBABLY_REJECTED :
616 ADDR_POLICY_REJECTED;
618 } else {
619 /* Might match. */
620 if (tmpe->policy_type == ADDR_POLICY_REJECT)
621 maybe_reject = 1;
622 else
623 maybe_accept = 1;
626 } SMARTLIST_FOREACH_END(tmpe);
628 /* accept all by default. */
629 return maybe_reject ? ADDR_POLICY_PROBABLY_ACCEPTED : ADDR_POLICY_ACCEPTED;
632 /** Helper for compare_tor_addr_to_addr_policy. Implements the case where
633 * port is known but address is not. */
634 static addr_policy_result_t
635 compare_unknown_tor_addr_to_addr_policy(uint16_t port,
636 const smartlist_t *policy)
638 /* We look to see if there's a definite match. If so, we return that
639 match's value, unless there's an intervening possible match that says
640 something different. */
641 int maybe_accept = 0, maybe_reject = 0;
643 SMARTLIST_FOREACH_BEGIN(policy, addr_policy_t *, tmpe) {
644 if (tmpe->prt_min <= port && port <= tmpe->prt_max) {
645 if (tmpe->maskbits == 0) {
646 /* Definitely matches, since it covers all addresses. */
647 if (tmpe->policy_type == ADDR_POLICY_ACCEPT) {
648 /* If we already hit a clause that might trigger a 'reject', than we
649 * can't be sure of this certain 'accept'.*/
650 return maybe_reject ? ADDR_POLICY_PROBABLY_ACCEPTED :
651 ADDR_POLICY_ACCEPTED;
652 } else {
653 return maybe_accept ? ADDR_POLICY_PROBABLY_REJECTED :
654 ADDR_POLICY_REJECTED;
656 } else {
657 /* Might match. */
658 if (tmpe->policy_type == ADDR_POLICY_REJECT)
659 maybe_reject = 1;
660 else
661 maybe_accept = 1;
664 } SMARTLIST_FOREACH_END(tmpe);
666 /* accept all by default. */
667 return maybe_reject ? ADDR_POLICY_PROBABLY_ACCEPTED : ADDR_POLICY_ACCEPTED;
670 /** Decide whether a given addr:port is definitely accepted,
671 * definitely rejected, probably accepted, or probably rejected by a
672 * given policy. If <b>addr</b> is 0, we don't know the IP of the
673 * target address. If <b>port</b> is 0, we don't know the port of the
674 * target address. (At least one of <b>addr</b> and <b>port</b> must be
675 * provided. If you want to know whether a policy would definitely reject
676 * an unknown address:port, use policy_is_reject_star().)
678 * We could do better by assuming that some ranges never match typical
679 * addresses (127.0.0.1, and so on). But we'll try this for now.
681 addr_policy_result_t
682 compare_tor_addr_to_addr_policy(const tor_addr_t *addr, uint16_t port,
683 const smartlist_t *policy)
685 if (!policy) {
686 /* no policy? accept all. */
687 return ADDR_POLICY_ACCEPTED;
688 } else if (tor_addr_is_null(addr)) {
689 tor_assert(port != 0);
690 return compare_unknown_tor_addr_to_addr_policy(port, policy);
691 } else if (port == 0) {
692 return compare_known_tor_addr_to_addr_policy_noport(addr, policy);
693 } else {
694 return compare_known_tor_addr_to_addr_policy(addr, port, policy);
698 /** Return true iff the address policy <b>a</b> covers every case that
699 * would be covered by <b>b</b>, so that a,b is redundant. */
700 static int
701 addr_policy_covers(addr_policy_t *a, addr_policy_t *b)
703 /* We can ignore accept/reject, since "accept *:80, reject *:80" reduces
704 * to "accept *:80". */
705 if (a->maskbits > b->maskbits) {
706 /* a has more fixed bits than b; it can't possibly cover b. */
707 return 0;
709 if (tor_addr_compare_masked(&a->addr, &b->addr, a->maskbits, CMP_EXACT)) {
710 /* There's a fixed bit in a that's set differently in b. */
711 return 0;
713 return (a->prt_min <= b->prt_min && a->prt_max >= b->prt_max);
716 /** Return true iff the address policies <b>a</b> and <b>b</b> intersect,
717 * that is, there exists an address/port that is covered by <b>a</b> that
718 * is also covered by <b>b</b>.
720 static int
721 addr_policy_intersects(addr_policy_t *a, addr_policy_t *b)
723 maskbits_t minbits;
724 /* All the bits we care about are those that are set in both
725 * netmasks. If they are equal in a and b's networkaddresses
726 * then the networks intersect. If there is a difference,
727 * then they do not. */
728 if (a->maskbits < b->maskbits)
729 minbits = a->maskbits;
730 else
731 minbits = b->maskbits;
732 if (tor_addr_compare_masked(&a->addr, &b->addr, minbits, CMP_EXACT))
733 return 0;
734 if (a->prt_max < b->prt_min || b->prt_max < a->prt_min)
735 return 0;
736 return 1;
739 /** Add the exit policy described by <b>more</b> to <b>policy</b>.
741 static void
742 append_exit_policy_string(smartlist_t **policy, const char *more)
744 config_line_t tmp;
746 tmp.key = NULL;
747 tmp.value = (char*) more;
748 tmp.next = NULL;
749 if (parse_addr_policy(&tmp, policy, -1)<0) {
750 log_warn(LD_BUG, "Unable to parse internally generated policy %s",more);
754 /** Detect and excise "dead code" from the policy *<b>dest</b>. */
755 static void
756 exit_policy_remove_redundancies(smartlist_t *dest)
758 addr_policy_t *ap, *tmp, *victim;
759 int i, j;
761 /* Step one: find a *:* entry and cut off everything after it. */
762 for (i = 0; i < smartlist_len(dest); ++i) {
763 ap = smartlist_get(dest, i);
764 if (ap->maskbits == 0 && ap->prt_min <= 1 && ap->prt_max >= 65535) {
765 /* This is a catch-all line -- later lines are unreachable. */
766 while (i+1 < smartlist_len(dest)) {
767 victim = smartlist_get(dest, i+1);
768 smartlist_del(dest, i+1);
769 addr_policy_free(victim);
771 break;
775 /* Step two: for every entry, see if there's a redundant entry
776 * later on, and remove it. */
777 for (i = 0; i < smartlist_len(dest)-1; ++i) {
778 ap = smartlist_get(dest, i);
779 for (j = i+1; j < smartlist_len(dest); ++j) {
780 tmp = smartlist_get(dest, j);
781 tor_assert(j > i);
782 if (addr_policy_covers(ap, tmp)) {
783 char p1[POLICY_BUF_LEN], p2[POLICY_BUF_LEN];
784 policy_write_item(p1, sizeof(p1), tmp, 0);
785 policy_write_item(p2, sizeof(p2), ap, 0);
786 log(LOG_DEBUG, LD_CONFIG, "Removing exit policy %s (%d). It is made "
787 "redundant by %s (%d).", p1, j, p2, i);
788 smartlist_del_keeporder(dest, j--);
789 addr_policy_free(tmp);
794 /* Step three: for every entry A, see if there's an entry B making this one
795 * redundant later on. This is the case if A and B are of the same type
796 * (accept/reject), A is a subset of B, and there is no other entry of
797 * different type in between those two that intersects with A.
799 * Anybody want to double-check the logic here? XXX
801 for (i = 0; i < smartlist_len(dest)-1; ++i) {
802 ap = smartlist_get(dest, i);
803 for (j = i+1; j < smartlist_len(dest); ++j) {
804 // tor_assert(j > i); // j starts out at i+1; j only increases; i only
805 // // decreases.
806 tmp = smartlist_get(dest, j);
807 if (ap->policy_type != tmp->policy_type) {
808 if (addr_policy_intersects(ap, tmp))
809 break;
810 } else { /* policy_types are equal. */
811 if (addr_policy_covers(tmp, ap)) {
812 char p1[POLICY_BUF_LEN], p2[POLICY_BUF_LEN];
813 policy_write_item(p1, sizeof(p1), ap, 0);
814 policy_write_item(p2, sizeof(p2), tmp, 0);
815 log(LOG_DEBUG, LD_CONFIG, "Removing exit policy %s. It is already "
816 "covered by %s.", p1, p2);
817 smartlist_del_keeporder(dest, i--);
818 addr_policy_free(ap);
819 break;
826 #define DEFAULT_EXIT_POLICY \
827 "reject *:25,reject *:119,reject *:135-139,reject *:445," \
828 "reject *:563,reject *:1214,reject *:4661-4666," \
829 "reject *:6346-6429,reject *:6699,reject *:6881-6999,accept *:*"
831 /** Parse the exit policy <b>cfg</b> into the linked list *<b>dest</b>. If
832 * cfg doesn't end in an absolute accept or reject, add the default exit
833 * policy afterwards. If <b>rejectprivate</b> is true, prepend
834 * "reject private:*" to the policy. Return -1 if we can't parse cfg,
835 * else return 0.
838 policies_parse_exit_policy(config_line_t *cfg, smartlist_t **dest,
839 int rejectprivate, const char *local_address)
841 if (rejectprivate) {
842 append_exit_policy_string(dest, "reject private:*");
843 if (local_address) {
844 char buf[POLICY_BUF_LEN];
845 tor_snprintf(buf, sizeof(buf), "reject %s:*", local_address);
846 append_exit_policy_string(dest, buf);
849 if (parse_addr_policy(cfg, dest, -1))
850 return -1;
851 append_exit_policy_string(dest, DEFAULT_EXIT_POLICY);
853 exit_policy_remove_redundancies(*dest);
855 return 0;
858 /** Replace the exit policy of <b>r</b> with reject *:*. */
859 void
860 policies_set_router_exitpolicy_to_reject_all(routerinfo_t *r)
862 addr_policy_t *item;
863 addr_policy_list_free(r->exit_policy);
864 r->exit_policy = smartlist_create();
865 item = router_parse_addr_policy_item_from_string("reject *:*", -1);
866 smartlist_add(r->exit_policy, item);
869 /** Return true iff <b>ri</b> is "useful as an exit node", meaning
870 * it allows exit to at least one /8 address space for at least
871 * two of ports 80, 443, and 6667. */
873 exit_policy_is_general_exit(smartlist_t *policy)
875 static const int ports[] = { 80, 443, 6667 };
876 int n_allowed = 0;
877 int i;
878 if (!policy) /*XXXX disallow NULL policies? */
879 return 0;
881 for (i = 0; i < 3; ++i) {
882 SMARTLIST_FOREACH(policy, addr_policy_t *, p, {
883 if (tor_addr_family(&p->addr) != AF_INET)
884 continue; /* IPv4 only for now */
885 if (p->prt_min > ports[i] || p->prt_max < ports[i])
886 continue; /* Doesn't cover our port. */
887 if (p->maskbits > 8)
888 continue; /* Narrower than a /8. */
889 if (tor_addr_is_loopback(&p->addr))
890 continue; /* 127.x or ::1. */
891 /* We have a match that is at least a /8. */
892 if (p->policy_type == ADDR_POLICY_ACCEPT) {
893 ++n_allowed;
894 break; /* stop considering this port */
898 return n_allowed >= 2;
901 /** Return false if <b>policy</b> might permit access to some addr:port;
902 * otherwise if we are certain it rejects everything, return true. */
904 policy_is_reject_star(const smartlist_t *policy)
906 if (!policy) /*XXXX disallow NULL policies? */
907 return 1;
908 SMARTLIST_FOREACH(policy, addr_policy_t *, p, {
909 if (p->policy_type == ADDR_POLICY_ACCEPT)
910 return 0;
911 else if (p->policy_type == ADDR_POLICY_REJECT &&
912 p->prt_min <= 1 && p->prt_max == 65535 &&
913 p->maskbits == 0)
914 return 1;
916 return 1;
919 /** Write a single address policy to the buf_len byte buffer at buf. Return
920 * the number of characters written, or -1 on failure. */
922 policy_write_item(char *buf, size_t buflen, addr_policy_t *policy,
923 int format_for_desc)
925 size_t written = 0;
926 char addrbuf[TOR_ADDR_BUF_LEN];
927 const char *addrpart;
928 int result;
929 const int is_accept = policy->policy_type == ADDR_POLICY_ACCEPT;
930 const int is_ip6 = tor_addr_family(&policy->addr) == AF_INET6;
932 tor_addr_to_str(addrbuf, &policy->addr, sizeof(addrbuf), 1);
934 /* write accept/reject 1.2.3.4 */
935 if (policy->is_private)
936 addrpart = "private";
937 else if (policy->maskbits == 0)
938 addrpart = "*";
939 else
940 addrpart = addrbuf;
942 result = tor_snprintf(buf, buflen, "%s%s%s %s",
943 (is_ip6&&format_for_desc)?"opt ":"",
944 is_accept ? "accept" : "reject",
945 (is_ip6&&format_for_desc)?"6":"",
946 addrpart);
947 if (result < 0)
948 return -1;
949 written += strlen(buf);
950 /* If the maskbits is 32 we don't need to give it. If the mask is 0,
951 * we already wrote "*". */
952 if (policy->maskbits < 32 && policy->maskbits > 0) {
953 if (tor_snprintf(buf+written, buflen-written, "/%d", policy->maskbits)<0)
954 return -1;
955 written += strlen(buf+written);
957 if (policy->prt_min <= 1 && policy->prt_max == 65535) {
958 /* There is no port set; write ":*" */
959 if (written+4 > buflen)
960 return -1;
961 strlcat(buf+written, ":*", buflen-written);
962 written += 2;
963 } else if (policy->prt_min == policy->prt_max) {
964 /* There is only one port; write ":80". */
965 result = tor_snprintf(buf+written, buflen-written, ":%d", policy->prt_min);
966 if (result<0)
967 return -1;
968 written += result;
969 } else {
970 /* There is a range of ports; write ":79-80". */
971 result = tor_snprintf(buf+written, buflen-written, ":%d-%d",
972 policy->prt_min, policy->prt_max);
973 if (result<0)
974 return -1;
975 written += result;
977 if (written < buflen)
978 buf[written] = '\0';
979 else
980 return -1;
982 return (int)written;
985 /** Create a new exit policy summary, initially only with a single
986 * port 1-64k item */
987 /* XXXX This entire thing will do most stuff in O(N^2), or worse. Use an
988 * RB-tree if that turns out to matter. */
989 static smartlist_t *
990 policy_summary_create(void)
992 smartlist_t *summary;
993 policy_summary_item_t* item;
995 item = tor_malloc_zero(sizeof(policy_summary_item_t));
996 item->prt_min = 1;
997 item->prt_max = 65535;
998 item->reject_count = 0;
999 item->accepted = 0;
1001 summary = smartlist_create();
1002 smartlist_add(summary, item);
1004 return summary;
1007 /** Split the summary item in <b>item</b> at the port <b>new_starts</b>.
1008 * The current item is changed to end at new-starts - 1, the new item
1009 * copies reject_count and accepted from the old item,
1010 * starts at new_starts and ends at the port where the original item
1011 * previously ended.
1013 static policy_summary_item_t*
1014 policy_summary_item_split(policy_summary_item_t* old, uint16_t new_starts)
1016 policy_summary_item_t* new;
1018 new = tor_malloc_zero(sizeof(policy_summary_item_t));
1019 new->prt_min = new_starts;
1020 new->prt_max = old->prt_max;
1021 new->reject_count = old->reject_count;
1022 new->accepted = old->accepted;
1024 old->prt_max = new_starts-1;
1026 tor_assert(old->prt_min <= old->prt_max);
1027 tor_assert(new->prt_min <= new->prt_max);
1028 return new;
1031 /* XXXX Nick says I'm going to hell for this. If he feels charitably towards
1032 * my immortal soul, he can clean it up himself. */
1033 #define AT(x) ((policy_summary_item_t*)smartlist_get(summary, x))
1035 #define REJECT_CUTOFF_COUNT (1<<25)
1036 /** Split an exit policy summary so that prt_min and prt_max
1037 * fall at exactly the start and end of an item respectively.
1039 static int
1040 policy_summary_split(smartlist_t *summary,
1041 uint16_t prt_min, uint16_t prt_max)
1043 int start_at_index;
1045 int i = 0;
1046 /* XXXX Do a binary search if run time matters */
1047 while (AT(i)->prt_max < prt_min)
1048 i++;
1049 if (AT(i)->prt_min != prt_min) {
1050 policy_summary_item_t* new_item;
1051 new_item = policy_summary_item_split(AT(i), prt_min);
1052 smartlist_insert(summary, i+1, new_item);
1053 i++;
1055 start_at_index = i;
1057 while (AT(i)->prt_max < prt_max)
1058 i++;
1059 if (AT(i)->prt_max != prt_max) {
1060 policy_summary_item_t* new_item;
1061 new_item = policy_summary_item_split(AT(i), prt_max+1);
1062 smartlist_insert(summary, i+1, new_item);
1065 return start_at_index;
1068 /** Mark port ranges as accepted if they are below the reject_count */
1069 static void
1070 policy_summary_accept(smartlist_t *summary,
1071 uint16_t prt_min, uint16_t prt_max)
1073 int i = policy_summary_split(summary, prt_min, prt_max);
1074 while (i < smartlist_len(summary) &&
1075 AT(i)->prt_max <= prt_max) {
1076 if (!AT(i)->accepted &&
1077 AT(i)->reject_count <= REJECT_CUTOFF_COUNT)
1078 AT(i)->accepted = 1;
1079 i++;
1081 tor_assert(i < smartlist_len(summary) || prt_max==65535);
1084 /** Count the number of addresses in a network with prefixlen maskbits
1085 * against the given portrange. */
1086 static void
1087 policy_summary_reject(smartlist_t *summary,
1088 maskbits_t maskbits,
1089 uint16_t prt_min, uint16_t prt_max)
1091 int i = policy_summary_split(summary, prt_min, prt_max);
1092 /* XXX: ipv4 specific */
1093 uint64_t count = (U64_LITERAL(1) << (32-maskbits));
1094 while (i < smartlist_len(summary) &&
1095 AT(i)->prt_max <= prt_max) {
1096 AT(i)->reject_count += count;
1097 i++;
1099 tor_assert(i < smartlist_len(summary) || prt_max==65535);
1102 /** Add a single exit policy item to our summary:
1103 * If it is an accept ignore it unless it is for all IP addresses
1104 * ("*"), i.e. it's prefixlen/maskbits is 0, else call
1105 * policy_summary_accept().
1106 * If it's a reject ignore it if it is about one of the private
1107 * networks, else call policy_summary_reject().
1109 static void
1110 policy_summary_add_item(smartlist_t *summary, addr_policy_t *p)
1112 if (p->policy_type == ADDR_POLICY_ACCEPT) {
1113 if (p->maskbits == 0) {
1114 policy_summary_accept(summary, p->prt_min, p->prt_max);
1116 } else if (p->policy_type == ADDR_POLICY_REJECT) {
1118 int is_private = 0;
1119 int i;
1120 for (i = 0; private_nets[i]; ++i) {
1121 tor_addr_t addr;
1122 maskbits_t maskbits;
1123 if (tor_addr_parse_mask_ports(private_nets[i], &addr,
1124 &maskbits, NULL, NULL)<0) {
1125 tor_assert(0);
1127 if (tor_addr_compare(&p->addr, &addr, CMP_EXACT) == 0 &&
1128 p->maskbits == maskbits) {
1129 is_private = 1;
1130 break;
1134 if (!is_private) {
1135 policy_summary_reject(summary, p->maskbits, p->prt_min, p->prt_max);
1137 } else
1138 tor_assert(0);
1141 /** Create a string representing a summary for an exit policy.
1142 * The summary will either be an "accept" plus a comma-separated list of port
1143 * ranges or a "reject" plus port-ranges, depending on which is shorter.
1145 * If no exits are allowed at all then NULL is returned, if no ports
1146 * are blocked instead of "reject " we return "accept 1-65535" (this
1147 * is an exception to the shorter-representation-wins rule).
1149 char *
1150 policy_summarize(smartlist_t *policy)
1152 smartlist_t *summary = policy_summary_create();
1153 smartlist_t *accepts, *rejects;
1154 int i, last, start_prt;
1155 size_t accepts_len, rejects_len, shorter_len, final_size;
1156 char *accepts_str = NULL, *rejects_str = NULL, *shorter_str, *result;
1157 const char *prefix;
1159 tor_assert(policy);
1161 /* Create the summary list */
1162 SMARTLIST_FOREACH(policy, addr_policy_t *, p, {
1163 policy_summary_add_item(summary, p);
1166 /* Now create two lists of strings, one for accepted and one
1167 * for rejected ports. We take care to merge ranges so that
1168 * we avoid getting stuff like "1-4,5-9,10", instead we want
1169 * "1-10"
1171 i = 0;
1172 start_prt = 1;
1173 accepts = smartlist_create();
1174 rejects = smartlist_create();
1175 while (1) {
1176 last = i == smartlist_len(summary)-1;
1177 if (last ||
1178 AT(i)->accepted != AT(i+1)->accepted) {
1179 char buf[POLICY_BUF_LEN];
1181 if (start_prt == AT(i)->prt_max)
1182 tor_snprintf(buf, sizeof(buf), "%d", start_prt);
1183 else
1184 tor_snprintf(buf, sizeof(buf), "%d-%d", start_prt, AT(i)->prt_max);
1186 if (AT(i)->accepted)
1187 smartlist_add(accepts, tor_strdup(buf));
1188 else
1189 smartlist_add(rejects, tor_strdup(buf));
1191 if (last)
1192 break;
1194 start_prt = AT(i+1)->prt_min;
1196 i++;
1199 /* Figure out which of the two stringlists will be shorter and use
1200 * that to build the result
1202 if (smartlist_len(accepts) == 0) { /* no exits at all */
1203 result = tor_strdup("reject 1-65535");
1204 goto cleanup;
1206 if (smartlist_len(rejects) == 0) { /* no rejects at all */
1207 result = tor_strdup("accept 1-65535");
1208 goto cleanup;
1211 accepts_str = smartlist_join_strings(accepts, ",", 0, &accepts_len);
1212 rejects_str = smartlist_join_strings(rejects, ",", 0, &rejects_len);
1214 if (rejects_len > MAX_EXITPOLICY_SUMMARY_LEN-strlen("reject")-1 &&
1215 accepts_len > MAX_EXITPOLICY_SUMMARY_LEN-strlen("accept")-1) {
1216 char *c;
1217 shorter_str = accepts_str;
1218 prefix = "accept";
1220 c = shorter_str + (MAX_EXITPOLICY_SUMMARY_LEN-strlen(prefix)-1);
1221 while (*c != ',' && c >= shorter_str)
1222 c--;
1223 tor_assert(c >= shorter_str);
1224 tor_assert(*c == ',');
1225 *c = '\0';
1227 shorter_len = strlen(shorter_str);
1228 } else if (rejects_len < accepts_len) {
1229 shorter_str = rejects_str;
1230 shorter_len = rejects_len;
1231 prefix = "reject";
1232 } else {
1233 shorter_str = accepts_str;
1234 shorter_len = accepts_len;
1235 prefix = "accept";
1238 final_size = strlen(prefix)+1+shorter_len+1;
1239 tor_assert(final_size <= MAX_EXITPOLICY_SUMMARY_LEN+1);
1240 result = tor_malloc(final_size);
1241 tor_snprintf(result, final_size, "%s %s", prefix, shorter_str);
1243 cleanup:
1244 /* cleanup */
1245 SMARTLIST_FOREACH(summary, policy_summary_item_t *, s, tor_free(s));
1246 smartlist_free(summary);
1248 tor_free(accepts_str);
1249 SMARTLIST_FOREACH(accepts, char *, s, tor_free(s));
1250 smartlist_free(accepts);
1252 tor_free(rejects_str);
1253 SMARTLIST_FOREACH(rejects, char *, s, tor_free(s));
1254 smartlist_free(rejects);
1256 return result;
1259 /** Implementation for GETINFO control command: knows the answer for questions
1260 * about "exit-policy/..." */
1262 getinfo_helper_policies(control_connection_t *conn,
1263 const char *question, char **answer)
1265 (void) conn;
1266 if (!strcmp(question, "exit-policy/default")) {
1267 *answer = tor_strdup(DEFAULT_EXIT_POLICY);
1269 return 0;
1272 /** Release all storage held by <b>p</b>. */
1273 void
1274 addr_policy_list_free(smartlist_t *lst)
1276 if (!lst) return;
1277 SMARTLIST_FOREACH(lst, addr_policy_t *, policy, addr_policy_free(policy));
1278 smartlist_free(lst);
1281 /** Release all storage held by <b>p</b>. */
1282 void
1283 addr_policy_free(addr_policy_t *p)
1285 if (p) {
1286 if (--p->refcnt <= 0) {
1287 if (p->is_canonical) {
1288 policy_map_ent_t search, *found;
1289 search.policy = p;
1290 found = HT_REMOVE(policy_map, &policy_root, &search);
1291 if (found) {
1292 tor_assert(p == found->policy);
1293 tor_free(found);
1296 tor_free(p);
1301 /** Release all storage held by policy variables. */
1302 void
1303 policies_free_all(void)
1305 addr_policy_list_free(reachable_or_addr_policy);
1306 reachable_or_addr_policy = NULL;
1307 addr_policy_list_free(reachable_dir_addr_policy);
1308 reachable_dir_addr_policy = NULL;
1309 addr_policy_list_free(socks_policy);
1310 socks_policy = NULL;
1311 addr_policy_list_free(dir_policy);
1312 dir_policy = NULL;
1313 addr_policy_list_free(authdir_reject_policy);
1314 authdir_reject_policy = NULL;
1315 addr_policy_list_free(authdir_invalid_policy);
1316 authdir_invalid_policy = NULL;
1317 addr_policy_list_free(authdir_baddir_policy);
1318 authdir_baddir_policy = NULL;
1319 addr_policy_list_free(authdir_badexit_policy);
1320 authdir_badexit_policy = NULL;
1322 if (!HT_EMPTY(&policy_root)) {
1323 policy_map_ent_t **ent;
1324 int n = 0;
1325 char buf[POLICY_BUF_LEN];
1327 log_warn(LD_MM, "Still had %d address policies cached at shutdown.",
1328 (int)HT_SIZE(&policy_root));
1330 /* Note the first 10 cached policies to try to figure out where they
1331 * might be coming from. */
1332 HT_FOREACH(ent, policy_map, &policy_root) {
1333 if (++n > 10)
1334 break;
1335 if (policy_write_item(buf, sizeof(buf), (*ent)->policy, 0) >= 0)
1336 log_warn(LD_MM," %d [%d]: %s", n, (*ent)->policy->refcnt, buf);
1339 HT_CLEAR(policy_map, &policy_root);