policies.c 23 KB

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