policies.c 22 KB

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