policies.c 29 KB

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