policies.c 21 KB

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