policies.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. */
  3. /* See LICENSE for licensing information */
  4. /* $Id$ */
  5. const char policies_c_id[] = \
  6. "$Id$";
  7. /**
  8. * \file policies.c
  9. * \brief Code to parse and use address policies and exit policies.
  10. **/
  11. #include "or.h"
  12. static int expand_exit_policy_aliases(smartlist_t *entries, int assume_action);
  13. static addr_policy_t *socks_policy = NULL;
  14. static addr_policy_t *dir_policy = NULL;
  15. static addr_policy_t *authdir_reject_policy = NULL;
  16. static addr_policy_t *authdir_invalid_policy = NULL;
  17. /** Parsed addr_policy_t describing which addresses we believe we can start
  18. * circuits at. */
  19. static addr_policy_t *reachable_or_addr_policy = NULL;
  20. /** Parsed addr_policy_t describing which addresses we believe we can connect
  21. * to directories at. */
  22. static addr_policy_t *reachable_dir_addr_policy = NULL;
  23. /**
  24. * Given a linked list of config lines containing "allow" and "deny"
  25. * tokens, parse them and append the result to <b>dest</b>. Return -1
  26. * if any tokens are malformed, else return 0.
  27. */
  28. static int
  29. parse_addr_policy(config_line_t *cfg, addr_policy_t **dest,
  30. int assume_action)
  31. {
  32. addr_policy_t **nextp;
  33. smartlist_t *entries;
  34. int r = 0;
  35. if (!cfg)
  36. return 0;
  37. nextp = dest;
  38. while (*nextp)
  39. nextp = &((*nextp)->next);
  40. entries = smartlist_create();
  41. for (; cfg; cfg = cfg->next) {
  42. smartlist_split_string(entries, cfg->value, ",",
  43. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  44. if (expand_exit_policy_aliases(entries,assume_action)<0) {
  45. r = -1;
  46. continue;
  47. }
  48. SMARTLIST_FOREACH(entries, const char *, ent,
  49. {
  50. log_debug(LD_CONFIG,"Adding new entry '%s'",ent);
  51. *nextp = router_parse_addr_policy_from_string(ent, assume_action);
  52. if (*nextp) {
  53. if (addr_mask_get_bits((*nextp)->msk)<0) {
  54. log_warn(LD_CONFIG, "Address policy element '%s' can't be expressed "
  55. "as a bit prefix.", ent);
  56. }
  57. /* Advance nextp to the end of the policy. */
  58. while (*nextp)
  59. nextp = &((*nextp)->next);
  60. } else {
  61. log_warn(LD_CONFIG,"Malformed policy '%s'.", ent);
  62. r = -1;
  63. }
  64. });
  65. SMARTLIST_FOREACH(entries, char *, ent, tor_free(ent));
  66. smartlist_clear(entries);
  67. }
  68. smartlist_free(entries);
  69. return r;
  70. }
  71. /** Helper: parse the Reachable(Dir|OR)?Addresses fields into
  72. * reachable_(or|dir)_addr_policy. */
  73. static void
  74. parse_reachable_addresses(void)
  75. {
  76. or_options_t *options = get_options();
  77. if (options->ReachableDirAddresses &&
  78. options->ReachableORAddresses &&
  79. options->ReachableAddresses) {
  80. log_warn(LD_CONFIG,
  81. "Both ReachableDirAddresses and ReachableORAddresses are set. "
  82. "ReachableAddresses setting will be ignored.");
  83. }
  84. addr_policy_free(reachable_or_addr_policy);
  85. reachable_or_addr_policy = NULL;
  86. if (!options->ReachableORAddresses && options->ReachableAddresses)
  87. log_info(LD_CONFIG,
  88. "Using ReachableAddresses as ReachableORAddresses.");
  89. if (parse_addr_policy(options->ReachableORAddresses ?
  90. options->ReachableORAddresses :
  91. options->ReachableAddresses,
  92. &reachable_or_addr_policy, ADDR_POLICY_ACCEPT)) {
  93. log_warn(LD_CONFIG,
  94. "Error parsing Reachable%sAddresses entry; ignoring.",
  95. options->ReachableORAddresses ? "OR" : "");
  96. }
  97. addr_policy_free(reachable_dir_addr_policy);
  98. reachable_dir_addr_policy = NULL;
  99. if (!options->ReachableDirAddresses && options->ReachableAddresses)
  100. log_info(LD_CONFIG,
  101. "Using ReachableAddresses as ReachableDirAddresses");
  102. if (parse_addr_policy(options->ReachableDirAddresses ?
  103. options->ReachableDirAddresses :
  104. options->ReachableAddresses,
  105. &reachable_dir_addr_policy, ADDR_POLICY_ACCEPT)) {
  106. if (options->ReachableDirAddresses)
  107. log_warn(LD_CONFIG,
  108. "Error parsing ReachableDirAddresses entry; ignoring.");
  109. }
  110. }
  111. /** Return true iff the firewall options might block any address:port
  112. * combination.
  113. */
  114. int
  115. firewall_is_fascist_or(void)
  116. {
  117. return !!reachable_or_addr_policy;
  118. }
  119. /** Return true iff <b>policy</b> (possibly NULL) will allow a
  120. * connection to <b>addr</b>:<b>port</b>.
  121. */
  122. static int
  123. addr_policy_permits_address(uint32_t addr, uint16_t port,
  124. addr_policy_t *policy)
  125. {
  126. addr_policy_result_t p;
  127. p = compare_addr_to_addr_policy(addr, port, policy);
  128. switch (p) {
  129. case ADDR_POLICY_PROBABLY_ACCEPTED:
  130. case ADDR_POLICY_ACCEPTED:
  131. return 1;
  132. case ADDR_POLICY_PROBABLY_REJECTED:
  133. case ADDR_POLICY_REJECTED:
  134. return 0;
  135. default:
  136. log_warn(LD_BUG, "Unexpected result: %d", (int)p);
  137. return 0;
  138. }
  139. }
  140. int
  141. fascist_firewall_allows_address_or(uint32_t addr, uint16_t port)
  142. {
  143. return addr_policy_permits_address(addr, port,
  144. reachable_or_addr_policy);
  145. }
  146. int
  147. fascist_firewall_allows_address_dir(uint32_t addr, uint16_t port)
  148. {
  149. return addr_policy_permits_address(addr, port,
  150. reachable_dir_addr_policy);
  151. }
  152. /** Return 1 if <b>addr</b> is permitted to connect to our dir port,
  153. * based on <b>dir_policy</b>. Else return 0.
  154. */
  155. int
  156. dir_policy_permits_address(uint32_t addr)
  157. {
  158. return addr_policy_permits_address(addr, 1, dir_policy);
  159. }
  160. /** Return 1 if <b>addr</b> is permitted to connect to our socks port,
  161. * based on <b>socks_policy</b>. Else return 0.
  162. */
  163. int
  164. socks_policy_permits_address(uint32_t addr)
  165. {
  166. return addr_policy_permits_address(addr, 1, socks_policy);
  167. }
  168. /** Return 1 if <b>addr</b>:<b>port</b> is permitted to publish to our
  169. * directory, based on <b>authdir_reject_policy</b>. Else return 0.
  170. */
  171. int
  172. authdir_policy_permits_address(uint32_t addr, uint16_t port)
  173. {
  174. return addr_policy_permits_address(addr, port, authdir_reject_policy);
  175. }
  176. /** Return 1 if <b>addr</b>:<b>port</b> is considered valid in our
  177. * directory, based on <b>authdir_invalid_policy</b>. Else return 0.
  178. */
  179. int
  180. authdir_policy_valid_address(uint32_t addr, uint16_t port)
  181. {
  182. return addr_policy_permits_address(addr, port, authdir_invalid_policy);
  183. }
  184. #define REJECT(arg) \
  185. do { *msg = tor_strdup(arg); goto err; } while (0)
  186. int
  187. validate_addr_policies(or_options_t *options, char **msg)
  188. {
  189. addr_policy_t *addr_policy=NULL;
  190. *msg = NULL;
  191. if (policies_parse_exit_policy(options->ExitPolicy, &addr_policy,
  192. options->ExitPolicyRejectPrivate))
  193. REJECT("Error in ExitPolicy entry.");
  194. /* The rest of these calls *append* to addr_policy. So don't actually
  195. * use the results for anything other than checking if they parse! */
  196. if (parse_addr_policy(options->DirPolicy, &addr_policy, -1))
  197. REJECT("Error in DirPolicy entry.");
  198. if (parse_addr_policy(options->SocksPolicy, &addr_policy, -1))
  199. REJECT("Error in SocksPolicy entry.");
  200. if (parse_addr_policy(options->ReachableAddresses, &addr_policy,
  201. ADDR_POLICY_ACCEPT))
  202. REJECT("Error in ReachableAddresses entry.");
  203. if (parse_addr_policy(options->ReachableORAddresses, &addr_policy,
  204. ADDR_POLICY_ACCEPT))
  205. REJECT("Error in ReachableORAddresses entry.");
  206. if (parse_addr_policy(options->ReachableDirAddresses, &addr_policy,
  207. ADDR_POLICY_ACCEPT))
  208. REJECT("Error in ReachableDirAddresses entry.");
  209. if (parse_addr_policy(options->AuthDirReject, &addr_policy,
  210. ADDR_POLICY_REJECT))
  211. REJECT("Error in AuthDirReject entry.");
  212. if (parse_addr_policy(options->AuthDirInvalid, &addr_policy,
  213. ADDR_POLICY_REJECT))
  214. REJECT("Error in AuthDirInvalid entry.");
  215. err:
  216. addr_policy_free(addr_policy);
  217. return *msg ? -1 : 0;
  218. #undef REJECT
  219. }
  220. /* Parse <b>string</b> in the same way that the exit policy
  221. * is parsed, and put the processed version in *<b>policy</b>.
  222. * Ignore port specifiers.
  223. */
  224. static void
  225. load_policy_from_option(config_line_t *config, addr_policy_t **policy,
  226. int assume_action)
  227. {
  228. addr_policy_t *n;
  229. addr_policy_free(*policy);
  230. *policy = NULL;
  231. parse_addr_policy(config, policy, assume_action);
  232. /* ports aren't used. */
  233. for (n=*policy; n; n = n->next) {
  234. n->prt_min = 1;
  235. n->prt_max = 65535;
  236. }
  237. }
  238. void policies_parse_from_options(or_options_t *options)
  239. {
  240. load_policy_from_option(options->SocksPolicy, &socks_policy, -1);
  241. load_policy_from_option(options->DirPolicy, &dir_policy, -1);
  242. load_policy_from_option(options->AuthDirReject,
  243. &authdir_reject_policy, ADDR_POLICY_REJECT);
  244. load_policy_from_option(options->AuthDirInvalid,
  245. &authdir_invalid_policy, ADDR_POLICY_REJECT);
  246. parse_reachable_addresses();
  247. }
  248. /** Compare two provided address policy items, and return -1, 0, or 1
  249. * if the first is less than, equal to, or greater than the second. */
  250. static int
  251. cmp_single_addr_policy(addr_policy_t *a, addr_policy_t *b)
  252. {
  253. int r;
  254. if ((r=((int)a->policy_type - (int)b->policy_type)))
  255. return r;
  256. if ((r=((int)a->addr - (int)b->addr)))
  257. return r;
  258. if ((r=((int)a->msk - (int)b->msk)))
  259. return r;
  260. if ((r=((int)a->prt_min - (int)b->prt_min)))
  261. return r;
  262. if ((r=((int)a->prt_max - (int)b->prt_max)))
  263. return r;
  264. return 0;
  265. }
  266. /** Like cmp_single_addr_policy() above, but looks at the
  267. * whole set of policies in each case. */
  268. int
  269. cmp_addr_policies(addr_policy_t *a, addr_policy_t *b)
  270. {
  271. int r;
  272. while (a && b) {
  273. if ((r=cmp_single_addr_policy(a,b)))
  274. return r;
  275. a = a->next;
  276. b = b->next;
  277. }
  278. if (!a && !b)
  279. return 0;
  280. if (a)
  281. return -1;
  282. else
  283. return 1;
  284. }
  285. /** Decide whether a given addr:port is definitely accepted,
  286. * definitely rejected, probably accepted, or probably rejected by a
  287. * given policy. If <b>addr</b> is 0, we don't know the IP of the
  288. * target address. If <b>port</b> is 0, we don't know the port of the
  289. * target address.
  290. *
  291. * For now, the algorithm is pretty simple: we look for definite and
  292. * uncertain matches. The first definite match is what we guess; if
  293. * it was preceded by no uncertain matches of the opposite policy,
  294. * then the guess is definite; otherwise it is probable. (If we
  295. * have a known addr and port, all matches are definite; if we have an
  296. * unknown addr/port, any address/port ranges other than "all" are
  297. * uncertain.)
  298. *
  299. * We could do better by assuming that some ranges never match typical
  300. * addresses (127.0.0.1, and so on). But we'll try this for now.
  301. */
  302. addr_policy_result_t
  303. compare_addr_to_addr_policy(uint32_t addr, uint16_t port,
  304. addr_policy_t *policy)
  305. {
  306. int maybe_reject = 0;
  307. int maybe_accept = 0;
  308. int match = 0;
  309. int maybe = 0;
  310. addr_policy_t *tmpe;
  311. for (tmpe=policy; tmpe; tmpe=tmpe->next) {
  312. maybe = 0;
  313. if (!addr) {
  314. /* Address is unknown. */
  315. if ((port >= tmpe->prt_min && port <= tmpe->prt_max) ||
  316. (!port && tmpe->prt_min<=1 && tmpe->prt_max>=65535)) {
  317. /* The port definitely matches. */
  318. if (tmpe->msk == 0) {
  319. match = 1;
  320. } else {
  321. maybe = 1;
  322. }
  323. } else if (!port) {
  324. /* The port maybe matches. */
  325. maybe = 1;
  326. }
  327. } else {
  328. /* Address is known */
  329. if ((addr & tmpe->msk) == (tmpe->addr & tmpe->msk)) {
  330. if (port >= tmpe->prt_min && port <= tmpe->prt_max) {
  331. /* Exact match for the policy */
  332. match = 1;
  333. } else if (!port) {
  334. maybe = 1;
  335. }
  336. }
  337. }
  338. if (maybe) {
  339. if (tmpe->policy_type == ADDR_POLICY_REJECT)
  340. maybe_reject = 1;
  341. else
  342. maybe_accept = 1;
  343. }
  344. if (match) {
  345. if (tmpe->policy_type == ADDR_POLICY_ACCEPT) {
  346. /* If we already hit a clause that might trigger a 'reject', than we
  347. * can't be sure of this certain 'accept'.*/
  348. return maybe_reject ? ADDR_POLICY_PROBABLY_ACCEPTED :
  349. ADDR_POLICY_ACCEPTED;
  350. } else {
  351. return maybe_accept ? ADDR_POLICY_PROBABLY_REJECTED :
  352. ADDR_POLICY_REJECTED;
  353. }
  354. }
  355. }
  356. /* accept all by default. */
  357. return maybe_reject ? ADDR_POLICY_PROBABLY_ACCEPTED : ADDR_POLICY_ACCEPTED;
  358. }
  359. /** Return true iff the address policy <b>a</b> covers every case that
  360. * would be covered by <b>b</b>, so that a,b is redundant. */
  361. static int
  362. addr_policy_covers(addr_policy_t *a, addr_policy_t *b)
  363. {
  364. /* We can ignore accept/reject, since "accept *:80, reject *:80" reduces
  365. * to "accept *:80". */
  366. if (a->msk & ~b->msk) {
  367. /* There's a wildcard bit in b->msk that's not a wildcard in a. */
  368. return 0;
  369. }
  370. if ((a->addr & a->msk) != (b->addr & a->msk)) {
  371. /* There's a fixed bit in a that's set differently in b. */
  372. return 0;
  373. }
  374. return (a->prt_min <= b->prt_min && a->prt_max >= b->prt_max);
  375. }
  376. /** Return true iff the address policies <b>a</b> and <b>b</b> intersect,
  377. * that is, there exists an address/port that is covered by <b>a</b> that
  378. * is also covered by <b>b</b>.
  379. */
  380. static int
  381. addr_policy_intersects(addr_policy_t *a, addr_policy_t *b)
  382. {
  383. /* All the bits we care about are those that are set in both
  384. * netmasks. If they are equal in a and b's networkaddresses
  385. * then the networks intersect. If there is a difference,
  386. * then they do not. */
  387. if (((a->addr ^ b->addr) & a->msk & b->msk) != 0)
  388. return 0;
  389. if (a->prt_max < b->prt_min || b->prt_max < a->prt_min)
  390. return 0;
  391. return 1;
  392. }
  393. /** Add the exit policy described by <b>more</b> to <b>policy</b>.
  394. */
  395. static void
  396. append_exit_policy_string(addr_policy_t **policy, const char *more)
  397. {
  398. config_line_t tmp;
  399. tmp.key = NULL;
  400. tmp.value = (char*) more;
  401. tmp.next = NULL;
  402. parse_addr_policy(&tmp, policy, -1);
  403. }
  404. static int
  405. expand_exit_policy_aliases(smartlist_t *entries, int assume_action)
  406. {
  407. static const char *prefixes[] = {
  408. "0.0.0.0/8", "169.254.0.0/16",
  409. "127.0.0.0/8", "192.168.0.0/16", "10.0.0.0/8", "172.16.0.0/12",NULL };
  410. int i;
  411. char *pre=NULL, *post=NULL;
  412. int expanded_any = 0;
  413. pre = smartlist_join_strings(entries,",",0,NULL);
  414. for (i = 0; i < smartlist_len(entries); ++i) {
  415. char *v = smartlist_get(entries, i);
  416. const char *cp, *ports;
  417. const char *action;
  418. int prefix_idx;
  419. if (!strcasecmpstart(v, "accept")) {
  420. action = "accept ";
  421. cp = v+strlen("accept");
  422. } else if (!strcasecmpstart(v, "reject")) {
  423. action = "reject ";
  424. cp = v+strlen("reject");
  425. } else if (assume_action >= 0) {
  426. action = "";
  427. cp = v;
  428. } else {
  429. log_warn(LD_CONFIG,"Policy '%s' didn't start with accept or reject.", v);
  430. tor_free(pre);
  431. return -1;
  432. }
  433. cp = eat_whitespace(cp);
  434. if (strcmpstart(cp, "private"))
  435. continue; /* No need to expand. */
  436. cp += strlen("private");
  437. cp = eat_whitespace(cp);
  438. if (*cp && *cp != ':')
  439. continue; /* It wasn't "private" after all. */
  440. ports = cp;
  441. /* Okay. We're going to replace entries[i] with a bunch of new entries,
  442. * in order. */
  443. smartlist_del_keeporder(entries, i);
  444. for (prefix_idx = 0; prefixes[prefix_idx]; ++prefix_idx) {
  445. size_t replacement_len = 16+strlen(prefixes[prefix_idx])+strlen(ports);
  446. char *replacement = tor_malloc(replacement_len);
  447. tor_snprintf(replacement, replacement_len, "%s%s%s",
  448. action, prefixes[prefix_idx], ports);
  449. smartlist_insert(entries, i++, replacement);
  450. }
  451. tor_free(v);
  452. expanded_any = 1;
  453. --i;
  454. }
  455. post = smartlist_join_strings(entries,",",0,NULL);
  456. if (expanded_any)
  457. log_info(LD_CONFIG, "Expanded '%s' to '%s'", pre, post);
  458. tor_free(pre);
  459. tor_free(post);
  460. return expanded_any;
  461. }
  462. /** Detect and excise "dead code" from the policy *<b>dest</b>. */
  463. static void
  464. exit_policy_remove_redundancies(addr_policy_t **dest)
  465. {
  466. addr_policy_t *ap, *tmp, *victim, *previous;
  467. /* Step one: find a *:* entry and cut off everything after it. */
  468. for (ap=*dest; ap; ap=ap->next) {
  469. if (ap->msk == 0 && ap->prt_min <= 1 && ap->prt_max >= 65535) {
  470. /* This is a catch-all line -- later lines are unreachable. */
  471. if (ap->next) {
  472. addr_policy_free(ap->next);
  473. ap->next = NULL;
  474. }
  475. }
  476. }
  477. /* Step two: for every entry, see if there's a redundant entry
  478. * later on, and remove it. */
  479. for (ap=*dest; ap; ap=ap->next) {
  480. tmp=ap;
  481. while (tmp) {
  482. if (tmp->next && addr_policy_covers(ap, tmp->next)) {
  483. log(LOG_INFO, LD_CONFIG, "Removing exit policy %s. It is made "
  484. "redundant by %s.", tmp->next->string, ap->string);
  485. victim = tmp->next;
  486. tmp->next = victim->next;
  487. victim->next = NULL;
  488. addr_policy_free(victim);
  489. } else {
  490. tmp=tmp->next;
  491. }
  492. }
  493. }
  494. /* Step three: for every entry A, see if there's an entry B making this one
  495. * redundant later on. This is the case if A and B are of the same type
  496. * (accept/reject), A is a subset of B, and there is no other entry of
  497. * different type in between those two that intersects with A.
  498. *
  499. * Anybody want to doublecheck the logic here? XXX
  500. */
  501. ap = *dest;
  502. previous = NULL;
  503. while (ap) {
  504. for (tmp=ap->next; tmp; tmp=tmp->next) {
  505. if (ap->policy_type != tmp->policy_type &&
  506. addr_policy_intersects(ap, tmp)) {
  507. tmp = NULL; /* so that we advance previous and ap */
  508. break;
  509. }
  510. if (ap->policy_type == tmp->policy_type &&
  511. addr_policy_covers(tmp, ap)) {
  512. log(LOG_INFO, LD_CONFIG, "Removing exit policy %s. It is made "
  513. "redundant by %s.", ap->string, tmp->string);
  514. victim = ap;
  515. ap = ap->next;
  516. if (previous) {
  517. assert(previous->next == victim);
  518. previous->next = victim->next;
  519. } else {
  520. assert(*dest == victim);
  521. *dest = victim->next;
  522. }
  523. victim->next = NULL;
  524. addr_policy_free(victim);
  525. break;
  526. }
  527. }
  528. if (!tmp) {
  529. previous = ap;
  530. ap = ap->next;
  531. }
  532. }
  533. }
  534. #define DEFAULT_EXIT_POLICY \
  535. "reject *:25,reject *:119,reject *:135-139,reject *:445," \
  536. "reject *:465,reject *:587,reject *:1214,reject *:4661-4666," \
  537. "reject *:6346-6429,reject *:6699,reject *:6881-6999,accept *:*"
  538. /** Parse the exit policy <b>cfg</b> into the linked list *<b>dest</b>. If
  539. * cfg doesn't end in an absolute accept or reject, add the default exit
  540. * policy afterwards. If <b>rejectprivate</b> is true, prepend
  541. * "reject private:*" to the policy. Return -1 if we can't parse cfg,
  542. * else return 0.
  543. *
  544. */
  545. int
  546. policies_parse_exit_policy(config_line_t *cfg, addr_policy_t **dest,
  547. int rejectprivate)
  548. {
  549. if (rejectprivate)
  550. append_exit_policy_string(dest, "reject private:*");
  551. if (parse_addr_policy(cfg, dest, -1))
  552. return -1;
  553. append_exit_policy_string(dest, DEFAULT_EXIT_POLICY);
  554. exit_policy_remove_redundancies(dest);
  555. return 0;
  556. }
  557. /** Return true iff <b>ri</b> is "useful as an exit node", meaning
  558. * it allows exit to at least one /8 address space for at least
  559. * one of ports 80, 443, and 6667. */
  560. int
  561. exit_policy_is_general_exit(addr_policy_t *policy)
  562. {
  563. static const int ports[] = { 80, 443, 6667 };
  564. int n_allowed = 0;
  565. int i;
  566. for (i = 0; i < 3; ++i) {
  567. struct addr_policy_t *p = policy;
  568. for ( ; p; p = p->next) {
  569. if (p->prt_min > ports[i] || p->prt_max < ports[i])
  570. continue; /* Doesn't cover our port. */
  571. if ((p->msk & 0x00fffffful) != 0)
  572. continue; /* Narrower than a /8. */
  573. if ((p->addr & 0xff000000ul) == 0x7f000000ul)
  574. continue; /* 127.x */
  575. /* We have a match that is at least a /8. */
  576. if (p->policy_type == ADDR_POLICY_ACCEPT)
  577. ++n_allowed;
  578. break;
  579. }
  580. }
  581. return n_allowed > 0;
  582. }
  583. /** Release all storage held by <b>p</b> */
  584. void
  585. addr_policy_free(addr_policy_t *p)
  586. {
  587. addr_policy_t *e;
  588. while (p) {
  589. e = p;
  590. p = p->next;
  591. tor_free(e->string);
  592. tor_free(e);
  593. }
  594. }
  595. void
  596. policies_free_all(void)
  597. {
  598. addr_policy_free(reachable_or_addr_policy);
  599. reachable_or_addr_policy = NULL;
  600. addr_policy_free(reachable_dir_addr_policy);
  601. reachable_dir_addr_policy = NULL;
  602. addr_policy_free(socks_policy);
  603. socks_policy = NULL;
  604. addr_policy_free(dir_policy);
  605. dir_policy = NULL;
  606. addr_policy_free(authdir_reject_policy);
  607. authdir_reject_policy = NULL;
  608. addr_policy_free(authdir_invalid_policy);
  609. authdir_invalid_policy = NULL;
  610. }