policies.c 30 KB

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