policies.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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 != NULL;
  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
  239. policies_parse_from_options(or_options_t *options)
  240. {
  241. load_policy_from_option(options->SocksPolicy, &socks_policy, -1);
  242. load_policy_from_option(options->DirPolicy, &dir_policy, -1);
  243. load_policy_from_option(options->AuthDirReject,
  244. &authdir_reject_policy, ADDR_POLICY_REJECT);
  245. load_policy_from_option(options->AuthDirInvalid,
  246. &authdir_invalid_policy, ADDR_POLICY_REJECT);
  247. parse_reachable_addresses();
  248. }
  249. /** Compare two provided address policy items, and return -1, 0, or 1
  250. * if the first is less than, equal to, or greater than the second. */
  251. static int
  252. cmp_single_addr_policy(addr_policy_t *a, addr_policy_t *b)
  253. {
  254. int r;
  255. if ((r=((int)a->policy_type - (int)b->policy_type)))
  256. return r;
  257. if ((r=((int)a->addr - (int)b->addr)))
  258. return r;
  259. if ((r=((int)a->msk - (int)b->msk)))
  260. return r;
  261. if ((r=((int)a->prt_min - (int)b->prt_min)))
  262. return r;
  263. if ((r=((int)a->prt_max - (int)b->prt_max)))
  264. return r;
  265. return 0;
  266. }
  267. /** Like cmp_single_addr_policy() above, but looks at the
  268. * whole set of policies in each case. */
  269. int
  270. cmp_addr_policies(addr_policy_t *a, addr_policy_t *b)
  271. {
  272. int r;
  273. while (a && b) {
  274. if ((r=cmp_single_addr_policy(a,b)))
  275. return r;
  276. a = a->next;
  277. b = b->next;
  278. }
  279. if (!a && !b)
  280. return 0;
  281. if (a)
  282. return -1;
  283. else
  284. return 1;
  285. }
  286. /** Decide whether a given addr:port is definitely accepted,
  287. * definitely rejected, probably accepted, or probably rejected by a
  288. * given policy. If <b>addr</b> is 0, we don't know the IP of the
  289. * target address. If <b>port</b> is 0, we don't know the port of the
  290. * target address.
  291. *
  292. * For now, the algorithm is pretty simple: we look for definite and
  293. * uncertain matches. The first definite match is what we guess; if
  294. * it was preceded by no uncertain matches of the opposite policy,
  295. * then the guess is definite; otherwise it is probable. (If we
  296. * have a known addr and port, all matches are definite; if we have an
  297. * unknown addr/port, any address/port ranges other than "all" are
  298. * uncertain.)
  299. *
  300. * We could do better by assuming that some ranges never match typical
  301. * addresses (127.0.0.1, and so on). But we'll try this for now.
  302. */
  303. addr_policy_result_t
  304. compare_addr_to_addr_policy(uint32_t addr, uint16_t port,
  305. addr_policy_t *policy)
  306. {
  307. int maybe_reject = 0;
  308. int maybe_accept = 0;
  309. int match = 0;
  310. int maybe = 0;
  311. addr_policy_t *tmpe;
  312. for (tmpe=policy; tmpe; tmpe=tmpe->next) {
  313. maybe = 0;
  314. if (!addr) {
  315. /* Address is unknown. */
  316. if ((port >= tmpe->prt_min && port <= tmpe->prt_max) ||
  317. (!port && tmpe->prt_min<=1 && tmpe->prt_max>=65535)) {
  318. /* The port definitely matches. */
  319. if (tmpe->msk == 0) {
  320. match = 1;
  321. } else {
  322. maybe = 1;
  323. }
  324. } else if (!port) {
  325. /* The port maybe matches. */
  326. maybe = 1;
  327. }
  328. } else {
  329. /* Address is known */
  330. if ((addr & tmpe->msk) == (tmpe->addr & tmpe->msk)) {
  331. if (port >= tmpe->prt_min && port <= tmpe->prt_max) {
  332. /* Exact match for the policy */
  333. match = 1;
  334. } else if (!port) {
  335. maybe = 1;
  336. }
  337. }
  338. }
  339. if (maybe) {
  340. if (tmpe->policy_type == ADDR_POLICY_REJECT)
  341. maybe_reject = 1;
  342. else
  343. maybe_accept = 1;
  344. }
  345. if (match) {
  346. if (tmpe->policy_type == ADDR_POLICY_ACCEPT) {
  347. /* If we already hit a clause that might trigger a 'reject', than we
  348. * can't be sure of this certain 'accept'.*/
  349. return maybe_reject ? ADDR_POLICY_PROBABLY_ACCEPTED :
  350. ADDR_POLICY_ACCEPTED;
  351. } else {
  352. return maybe_accept ? ADDR_POLICY_PROBABLY_REJECTED :
  353. ADDR_POLICY_REJECTED;
  354. }
  355. }
  356. }
  357. /* accept all by default. */
  358. return maybe_reject ? ADDR_POLICY_PROBABLY_ACCEPTED : ADDR_POLICY_ACCEPTED;
  359. }
  360. /** Return true iff the address policy <b>a</b> covers every case that
  361. * would be covered by <b>b</b>, so that a,b is redundant. */
  362. static int
  363. addr_policy_covers(addr_policy_t *a, addr_policy_t *b)
  364. {
  365. /* We can ignore accept/reject, since "accept *:80, reject *:80" reduces
  366. * to "accept *:80". */
  367. if (a->msk & ~b->msk) {
  368. /* There's a wildcard bit in b->msk that's not a wildcard in a. */
  369. return 0;
  370. }
  371. if ((a->addr & a->msk) != (b->addr & a->msk)) {
  372. /* There's a fixed bit in a that's set differently in b. */
  373. return 0;
  374. }
  375. return (a->prt_min <= b->prt_min && a->prt_max >= b->prt_max);
  376. }
  377. /** Return true iff the address policies <b>a</b> and <b>b</b> intersect,
  378. * that is, there exists an address/port that is covered by <b>a</b> that
  379. * is also covered by <b>b</b>.
  380. */
  381. static int
  382. addr_policy_intersects(addr_policy_t *a, addr_policy_t *b)
  383. {
  384. /* All the bits we care about are those that are set in both
  385. * netmasks. If they are equal in a and b's networkaddresses
  386. * then the networks intersect. If there is a difference,
  387. * then they do not. */
  388. if (((a->addr ^ b->addr) & a->msk & b->msk) != 0)
  389. return 0;
  390. if (a->prt_max < b->prt_min || b->prt_max < a->prt_min)
  391. return 0;
  392. return 1;
  393. }
  394. /** Add the exit policy described by <b>more</b> to <b>policy</b>.
  395. */
  396. static void
  397. append_exit_policy_string(addr_policy_t **policy, const char *more)
  398. {
  399. config_line_t tmp;
  400. tmp.key = NULL;
  401. tmp.value = (char*) more;
  402. tmp.next = NULL;
  403. parse_addr_policy(&tmp, policy, -1);
  404. }
  405. static int
  406. expand_exit_policy_aliases(smartlist_t *entries, int assume_action)
  407. {
  408. static const char *prefixes[] = {
  409. "0.0.0.0/8", "169.254.0.0/16",
  410. "127.0.0.0/8", "192.168.0.0/16", "10.0.0.0/8", "172.16.0.0/12",NULL };
  411. int i;
  412. char *pre=NULL, *post=NULL;
  413. int expanded_any = 0;
  414. pre = smartlist_join_strings(entries,",",0,NULL);
  415. for (i = 0; i < smartlist_len(entries); ++i) {
  416. char *v = smartlist_get(entries, i);
  417. const char *cp, *ports;
  418. const char *action;
  419. int prefix_idx;
  420. if (!strcasecmpstart(v, "accept")) {
  421. action = "accept ";
  422. cp = v+strlen("accept");
  423. } else if (!strcasecmpstart(v, "reject")) {
  424. action = "reject ";
  425. cp = v+strlen("reject");
  426. } else if (assume_action >= 0) {
  427. action = "";
  428. cp = v;
  429. } else {
  430. log_warn(LD_CONFIG,"Policy '%s' didn't start with accept or reject.", v);
  431. tor_free(pre);
  432. return -1;
  433. }
  434. cp = eat_whitespace(cp);
  435. if (strcmpstart(cp, "private"))
  436. continue; /* No need to expand. */
  437. cp += strlen("private");
  438. cp = eat_whitespace(cp);
  439. if (*cp && *cp != ':')
  440. continue; /* It wasn't "private" after all. */
  441. ports = cp;
  442. /* Okay. We're going to replace entries[i] with a bunch of new entries,
  443. * in order. */
  444. smartlist_del_keeporder(entries, i);
  445. for (prefix_idx = 0; prefixes[prefix_idx]; ++prefix_idx) {
  446. size_t replacement_len = 16+strlen(prefixes[prefix_idx])+strlen(ports);
  447. char *replacement = tor_malloc(replacement_len);
  448. tor_snprintf(replacement, replacement_len, "%s%s%s",
  449. action, prefixes[prefix_idx], ports);
  450. smartlist_insert(entries, i++, replacement);
  451. }
  452. tor_free(v);
  453. expanded_any = 1;
  454. --i;
  455. }
  456. post = smartlist_join_strings(entries,",",0,NULL);
  457. if (expanded_any)
  458. log_info(LD_CONFIG, "Expanded '%s' to '%s'", pre, post);
  459. tor_free(pre);
  460. tor_free(post);
  461. return expanded_any;
  462. }
  463. /** Detect and excise "dead code" from the policy *<b>dest</b>. */
  464. static void
  465. exit_policy_remove_redundancies(addr_policy_t **dest)
  466. {
  467. addr_policy_t *ap, *tmp, *victim, *previous;
  468. /* Step one: find a *:* entry and cut off everything after it. */
  469. for (ap=*dest; ap; ap=ap->next) {
  470. if (ap->msk == 0 && ap->prt_min <= 1 && ap->prt_max >= 65535) {
  471. /* This is a catch-all line -- later lines are unreachable. */
  472. if (ap->next) {
  473. addr_policy_free(ap->next);
  474. ap->next = NULL;
  475. }
  476. }
  477. }
  478. /* Step two: for every entry, see if there's a redundant entry
  479. * later on, and remove it. */
  480. for (ap=*dest; ap; ap=ap->next) {
  481. tmp=ap;
  482. while (tmp) {
  483. if (tmp->next && addr_policy_covers(ap, tmp->next)) {
  484. log(LOG_INFO, LD_CONFIG, "Removing exit policy %s. It is made "
  485. "redundant by %s.", tmp->next->string, ap->string);
  486. victim = tmp->next;
  487. tmp->next = victim->next;
  488. victim->next = NULL;
  489. addr_policy_free(victim);
  490. } else {
  491. tmp=tmp->next;
  492. }
  493. }
  494. }
  495. /* Step three: for every entry A, see if there's an entry B making this one
  496. * redundant later on. This is the case if A and B are of the same type
  497. * (accept/reject), A is a subset of B, and there is no other entry of
  498. * different type in between those two that intersects with A.
  499. *
  500. * Anybody want to doublecheck the logic here? XXX
  501. */
  502. ap = *dest;
  503. previous = NULL;
  504. while (ap) {
  505. for (tmp=ap->next; tmp; tmp=tmp->next) {
  506. if (ap->policy_type != tmp->policy_type &&
  507. addr_policy_intersects(ap, tmp)) {
  508. tmp = NULL; /* so that we advance previous and ap */
  509. break;
  510. }
  511. if (ap->policy_type == tmp->policy_type &&
  512. addr_policy_covers(tmp, ap)) {
  513. log(LOG_INFO, LD_CONFIG, "Removing exit policy %s. It is made "
  514. "redundant by %s.", ap->string, tmp->string);
  515. victim = ap;
  516. ap = ap->next;
  517. if (previous) {
  518. assert(previous->next == victim);
  519. previous->next = victim->next;
  520. } else {
  521. assert(*dest == victim);
  522. *dest = victim->next;
  523. }
  524. victim->next = NULL;
  525. addr_policy_free(victim);
  526. break;
  527. }
  528. }
  529. if (!tmp) {
  530. previous = ap;
  531. ap = ap->next;
  532. }
  533. }
  534. }
  535. #define DEFAULT_EXIT_POLICY \
  536. "reject *:25,reject *:119,reject *:135-139,reject *:445," \
  537. "reject *:465,reject *:587,reject *:1214,reject *:4661-4666," \
  538. "reject *:6346-6429,reject *:6699,reject *:6881-6999,accept *:*"
  539. /** Parse the exit policy <b>cfg</b> into the linked list *<b>dest</b>. If
  540. * cfg doesn't end in an absolute accept or reject, add the default exit
  541. * policy afterwards. If <b>rejectprivate</b> is true, prepend
  542. * "reject private:*" to the policy. Return -1 if we can't parse cfg,
  543. * else return 0.
  544. *
  545. */
  546. int
  547. policies_parse_exit_policy(config_line_t *cfg, addr_policy_t **dest,
  548. int rejectprivate)
  549. {
  550. if (rejectprivate)
  551. append_exit_policy_string(dest, "reject private:*");
  552. if (parse_addr_policy(cfg, dest, -1))
  553. return -1;
  554. append_exit_policy_string(dest, DEFAULT_EXIT_POLICY);
  555. exit_policy_remove_redundancies(dest);
  556. return 0;
  557. }
  558. /** Return true iff <b>ri</b> is "useful as an exit node", meaning
  559. * it allows exit to at least one /8 address space for at least
  560. * two of ports 80, 443, and 6667. */
  561. int
  562. exit_policy_is_general_exit(addr_policy_t *policy)
  563. {
  564. static const int ports[] = { 80, 443, 6667 };
  565. int n_allowed = 0;
  566. int i;
  567. for (i = 0; i < 3; ++i) {
  568. struct addr_policy_t *p = policy;
  569. for ( ; p; p = p->next) {
  570. if (p->prt_min > ports[i] || p->prt_max < ports[i])
  571. continue; /* Doesn't cover our port. */
  572. if ((p->msk & 0x00fffffful) != 0)
  573. continue; /* Narrower than a /8. */
  574. if ((p->addr & 0xff000000ul) == 0x7f000000ul)
  575. continue; /* 127.x */
  576. /* We have a match that is at least a /8. */
  577. if (p->policy_type == ADDR_POLICY_ACCEPT) {
  578. ++n_allowed;
  579. break; /* stop considering this port */
  580. }
  581. }
  582. }
  583. return n_allowed >= 2;
  584. }
  585. /** Return false if <b>policy</b> might permit access to some addr:port;
  586. * otherwise if we are certain it rejects everything, return true. */
  587. int
  588. policy_is_reject_star(addr_policy_t *p)
  589. {
  590. for ( ; p; p = p->next) {
  591. if (p->policy_type == ADDR_POLICY_ACCEPT)
  592. return 0;
  593. else if (p->policy_type == ADDR_POLICY_REJECT &&
  594. p->prt_min <= 1 && p->prt_max == 65535 &&
  595. p->msk == 0)
  596. return 1;
  597. }
  598. return 1;
  599. }
  600. int
  601. policies_getinfo_helper(const char *question, char **answer)
  602. {
  603. if (!strcmp(question, "exit-policy/default")) {
  604. *answer = tor_strdup(DEFAULT_EXIT_POLICY);
  605. // } else if (!strcmp(question, "exit-policy/prepend")) {
  606. } else {
  607. *answer = NULL;
  608. }
  609. return 0;
  610. }
  611. /** Release all storage held by <b>p</b> */
  612. void
  613. addr_policy_free(addr_policy_t *p)
  614. {
  615. addr_policy_t *e;
  616. while (p) {
  617. e = p;
  618. p = p->next;
  619. tor_free(e->string);
  620. tor_free(e);
  621. }
  622. }
  623. void
  624. policies_free_all(void)
  625. {
  626. addr_policy_free(reachable_or_addr_policy);
  627. reachable_or_addr_policy = NULL;
  628. addr_policy_free(reachable_dir_addr_policy);
  629. reachable_dir_addr_policy = NULL;
  630. addr_policy_free(socks_policy);
  631. socks_policy = NULL;
  632. addr_policy_free(dir_policy);
  633. dir_policy = NULL;
  634. addr_policy_free(authdir_reject_policy);
  635. authdir_reject_policy = NULL;
  636. addr_policy_free(authdir_invalid_policy);
  637. authdir_invalid_policy = NULL;
  638. }