policies.c 23 KB

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