policies.c 27 KB

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