policies.c 21 KB

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