policies.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2008, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /* $Id$ */
  6. const char policies_c_id[] = \
  7. "$Id$";
  8. /**
  9. * \file policies.c
  10. * \brief Code to parse and use address policies and exit policies.
  11. **/
  12. #include "or.h"
  13. #include "ht.h"
  14. /** Policy that addresses for incoming SOCKS connections must match. */
  15. static smartlist_t *socks_policy = NULL;
  16. /** Policy that addresses for incoming directory connections must match. */
  17. static smartlist_t *dir_policy = NULL;
  18. /** Policy that addresses for incoming router descriptors must match in order
  19. * to be published by us. */
  20. static smartlist_t *authdir_reject_policy = NULL;
  21. /** Policy that addresses for incoming router descriptors must match in order
  22. * to be marked as valid in our networkstatus. */
  23. static smartlist_t *authdir_invalid_policy = NULL;
  24. /** Policy that addresses for incoming router descriptors must <b>not</b>
  25. * match in order to not be marked as BadDirectory. */
  26. static smartlist_t *authdir_baddir_policy = NULL;
  27. /** Policy that addresses for incoming router descriptors must <b>not</b>
  28. * match in order to not be marked as BadExit. */
  29. static smartlist_t *authdir_badexit_policy = NULL;
  30. /** Parsed addr_policy_t describing which addresses we believe we can start
  31. * circuits at. */
  32. static smartlist_t *reachable_or_addr_policy = NULL;
  33. /** Parsed addr_policy_t describing which addresses we believe we can connect
  34. * to directories at. */
  35. static smartlist_t *reachable_dir_addr_policy = NULL;
  36. /** Element of an exit policy summary */
  37. typedef struct policy_summary_item_t {
  38. uint16_t prt_min; /**< Lowest port number to accept/reject. */
  39. uint16_t prt_max; /**< Highest port number to accept/reject. */
  40. uint64_t reject_count; /**< Number of IP-Addresses that are rejected to
  41. this portrange. */
  42. int accepted:1; /** Has this port already been accepted */
  43. } policy_summary_item_t;
  44. smartlist_t *policy_summary_create(void);
  45. void policy_summary_accept(smartlist_t *summary, uint16_t prt_min, uint16_t prt_max);
  46. void policy_summary_reject(smartlist_t *summary, maskbits_t maskbits, uint16_t prt_min, uint16_t prt_max);
  47. void policy_summary_add_item(smartlist_t *summary, addr_policy_t *p);
  48. int policy_summary_split(smartlist_t *summary, uint16_t prt_min, uint16_t prt_max);
  49. policy_summary_item_t* policy_summary_item_split(policy_summary_item_t* old, uint16_t new_starts);
  50. /** Private networks. This list is used in two places, once to expand the
  51. * "private" keyword when parsing our own exit policy, secondly to ignore
  52. * just such networks when building exit policy summaries. It is important
  53. * that all authorities agree on that list when creating summaries, so don't
  54. * just change this without a proper migration plan and a proposal and stuff.
  55. */
  56. static const char *private_nets[] = {
  57. "0.0.0.0/8", "169.254.0.0/16",
  58. "127.0.0.0/8", "192.168.0.0/16", "10.0.0.0/8", "172.16.0.0/12",
  59. // "fc00::/7", "fe80::/10", "fec0::/10", "::/127",
  60. NULL };
  61. /** Replace all "private" entries in *<b>policy</b> with their expanded
  62. * equivalents. */
  63. void
  64. policy_expand_private(smartlist_t **policy)
  65. {
  66. uint16_t port_min, port_max;
  67. int i;
  68. smartlist_t *tmp;
  69. if (!*policy) /*XXXX021 disallow NULL policies */
  70. return;
  71. tmp = smartlist_create();
  72. SMARTLIST_FOREACH(*policy, addr_policy_t *, p,
  73. {
  74. if (! p->is_private) {
  75. smartlist_add(tmp, p);
  76. continue;
  77. }
  78. for (i = 0; private_nets[i]; ++i) {
  79. addr_policy_t policy;
  80. memcpy(&policy, p, sizeof(addr_policy_t));
  81. policy.is_private = 0;
  82. policy.is_canonical = 0;
  83. if (tor_addr_parse_mask_ports(private_nets[i], &policy.addr,
  84. &policy.maskbits, &port_min, &port_max)<0) {
  85. tor_assert(0);
  86. }
  87. smartlist_add(tmp, addr_policy_get_canonical_entry(&policy));
  88. }
  89. addr_policy_free(p);
  90. });
  91. smartlist_free(*policy);
  92. *policy = tmp;
  93. }
  94. /**
  95. * Given a linked list of config lines containing "allow" and "deny"
  96. * tokens, parse them and append the result to <b>dest</b>. Return -1
  97. * if any tokens are malformed (and don't append any), else return 0.
  98. */
  99. static int
  100. parse_addr_policy(config_line_t *cfg, smartlist_t **dest,
  101. int assume_action)
  102. {
  103. smartlist_t *result;
  104. smartlist_t *entries;
  105. addr_policy_t *item;
  106. int r = 0;
  107. if (!cfg)
  108. return 0;
  109. result = smartlist_create();
  110. entries = smartlist_create();
  111. for (; cfg; cfg = cfg->next) {
  112. smartlist_split_string(entries, cfg->value, ",",
  113. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  114. SMARTLIST_FOREACH(entries, const char *, ent,
  115. {
  116. log_debug(LD_CONFIG,"Adding new entry '%s'",ent);
  117. item = router_parse_addr_policy_item_from_string(ent, assume_action);
  118. if (item) {
  119. smartlist_add(result, item);
  120. } else {
  121. log_warn(LD_CONFIG,"Malformed policy '%s'.", ent);
  122. r = -1;
  123. }
  124. });
  125. SMARTLIST_FOREACH(entries, char *, ent, tor_free(ent));
  126. smartlist_clear(entries);
  127. }
  128. smartlist_free(entries);
  129. if (r == -1) {
  130. addr_policy_list_free(result);
  131. } else {
  132. policy_expand_private(&result);
  133. if (*dest) {
  134. smartlist_add_all(*dest, result);
  135. smartlist_free(result);
  136. } else {
  137. *dest = result;
  138. }
  139. }
  140. return r;
  141. }
  142. /** Helper: parse the Reachable(Dir|OR)?Addresses fields into
  143. * reachable_(or|dir)_addr_policy. The options should already have
  144. * been validated by validate_addr_policies.
  145. */
  146. static int
  147. parse_reachable_addresses(void)
  148. {
  149. or_options_t *options = get_options();
  150. int ret = 0;
  151. if (options->ReachableDirAddresses &&
  152. options->ReachableORAddresses &&
  153. options->ReachableAddresses) {
  154. log_warn(LD_CONFIG,
  155. "Both ReachableDirAddresses and ReachableORAddresses are set. "
  156. "ReachableAddresses setting will be ignored.");
  157. }
  158. addr_policy_list_free(reachable_or_addr_policy);
  159. reachable_or_addr_policy = NULL;
  160. if (!options->ReachableORAddresses && options->ReachableAddresses)
  161. log_info(LD_CONFIG,
  162. "Using ReachableAddresses as ReachableORAddresses.");
  163. if (parse_addr_policy(options->ReachableORAddresses ?
  164. options->ReachableORAddresses :
  165. options->ReachableAddresses,
  166. &reachable_or_addr_policy, ADDR_POLICY_ACCEPT)) {
  167. log_warn(LD_CONFIG,
  168. "Error parsing Reachable%sAddresses entry; ignoring.",
  169. options->ReachableORAddresses ? "OR" : "");
  170. ret = -1;
  171. }
  172. addr_policy_list_free(reachable_dir_addr_policy);
  173. reachable_dir_addr_policy = NULL;
  174. if (!options->ReachableDirAddresses && options->ReachableAddresses)
  175. log_info(LD_CONFIG,
  176. "Using ReachableAddresses as ReachableDirAddresses");
  177. if (parse_addr_policy(options->ReachableDirAddresses ?
  178. options->ReachableDirAddresses :
  179. options->ReachableAddresses,
  180. &reachable_dir_addr_policy, ADDR_POLICY_ACCEPT)) {
  181. if (options->ReachableDirAddresses)
  182. log_warn(LD_CONFIG,
  183. "Error parsing ReachableDirAddresses entry; ignoring.");
  184. ret = -1;
  185. }
  186. return ret;
  187. }
  188. /** Return true iff the firewall options might block any address:port
  189. * combination.
  190. */
  191. int
  192. firewall_is_fascist_or(void)
  193. {
  194. return reachable_or_addr_policy != NULL;
  195. }
  196. /** Return true iff <b>policy</b> (possibly NULL) will allow a
  197. * connection to <b>addr</b>:<b>port</b>.
  198. */
  199. static int
  200. addr_policy_permits_tor_addr(const tor_addr_t *addr, uint16_t port,
  201. smartlist_t *policy)
  202. {
  203. addr_policy_result_t p;
  204. p = compare_tor_addr_to_addr_policy(addr, port, policy);
  205. switch (p) {
  206. case ADDR_POLICY_PROBABLY_ACCEPTED:
  207. case ADDR_POLICY_ACCEPTED:
  208. return 1;
  209. case ADDR_POLICY_PROBABLY_REJECTED:
  210. case ADDR_POLICY_REJECTED:
  211. return 0;
  212. default:
  213. log_warn(LD_BUG, "Unexpected result: %d", (int)p);
  214. return 0;
  215. }
  216. }
  217. /* DOCDOC XXXX021 deprecate? */
  218. static int
  219. addr_policy_permits_address(uint32_t addr, uint16_t port,
  220. smartlist_t *policy)
  221. {
  222. tor_addr_t a;
  223. tor_addr_from_ipv4h(&a, addr);
  224. return addr_policy_permits_tor_addr(&a, port, policy);
  225. }
  226. /** Return true iff we think our firewall will let us make an OR connection to
  227. * addr:port. */
  228. int
  229. fascist_firewall_allows_address_or(const tor_addr_t *addr, uint16_t port)
  230. {
  231. return addr_policy_permits_tor_addr(addr, port,
  232. reachable_or_addr_policy);
  233. }
  234. /** DOCDOC */
  235. int
  236. fascist_firewall_allows_or(routerinfo_t *ri)
  237. {
  238. /* XXXX021 proposal 118 */
  239. tor_addr_t addr;
  240. tor_addr_from_ipv4h(&addr, ri->addr);
  241. return fascist_firewall_allows_address_or(&addr, ri->or_port);
  242. }
  243. /** Return true iff we think our firewall will let us make a directory
  244. * connection to addr:port. */
  245. int
  246. fascist_firewall_allows_address_dir(const tor_addr_t *addr, uint16_t port)
  247. {
  248. return addr_policy_permits_tor_addr(addr, port,
  249. reachable_dir_addr_policy);
  250. }
  251. /** Return 1 if <b>addr</b> is permitted to connect to our dir port,
  252. * based on <b>dir_policy</b>. Else return 0.
  253. */
  254. int
  255. dir_policy_permits_address(const tor_addr_t *addr)
  256. {
  257. return addr_policy_permits_tor_addr(addr, 1, dir_policy);
  258. }
  259. /** Return 1 if <b>addr</b> is permitted to connect to our socks port,
  260. * based on <b>socks_policy</b>. Else return 0.
  261. */
  262. int
  263. socks_policy_permits_address(const tor_addr_t *addr)
  264. {
  265. return addr_policy_permits_tor_addr(addr, 1, socks_policy);
  266. }
  267. /** Return 1 if <b>addr</b>:<b>port</b> is permitted to publish to our
  268. * directory, based on <b>authdir_reject_policy</b>. Else return 0.
  269. */
  270. int
  271. authdir_policy_permits_address(uint32_t addr, uint16_t port)
  272. {
  273. return addr_policy_permits_address(addr, port, authdir_reject_policy);
  274. }
  275. /** Return 1 if <b>addr</b>:<b>port</b> is considered valid in our
  276. * directory, based on <b>authdir_invalid_policy</b>. Else return 0.
  277. */
  278. int
  279. authdir_policy_valid_address(uint32_t addr, uint16_t port)
  280. {
  281. return addr_policy_permits_address(addr, port, authdir_invalid_policy);
  282. }
  283. /** Return 1 if <b>addr</b>:<b>port</b> should be marked as a bad dir,
  284. * based on <b>authdir_baddir_policy</b>. Else return 0.
  285. */
  286. int
  287. authdir_policy_baddir_address(uint32_t addr, uint16_t port)
  288. {
  289. return ! addr_policy_permits_address(addr, port, authdir_baddir_policy);
  290. }
  291. /** Return 1 if <b>addr</b>:<b>port</b> should be marked as a bad exit,
  292. * based on <b>authdir_badexit_policy</b>. Else return 0.
  293. */
  294. int
  295. authdir_policy_badexit_address(uint32_t addr, uint16_t port)
  296. {
  297. return ! addr_policy_permits_address(addr, port, authdir_badexit_policy);
  298. }
  299. #define REJECT(arg) \
  300. STMT_BEGIN *msg = tor_strdup(arg); goto err; STMT_END
  301. /** Config helper: If there's any problem with the policy configuration
  302. * options in <b>options</b>, return -1 and set <b>msg</b> to a newly
  303. * allocated description of the error. Else return 0. */
  304. int
  305. validate_addr_policies(or_options_t *options, char **msg)
  306. {
  307. /* XXXX Maybe merge this into parse_policies_from_options, to make sure
  308. * that the two can't go out of sync. */
  309. smartlist_t *addr_policy=NULL;
  310. *msg = NULL;
  311. if (policies_parse_exit_policy(options->ExitPolicy, &addr_policy,
  312. options->ExitPolicyRejectPrivate, NULL))
  313. REJECT("Error in ExitPolicy entry.");
  314. /* The rest of these calls *append* to addr_policy. So don't actually
  315. * use the results for anything other than checking if they parse! */
  316. if (parse_addr_policy(options->DirPolicy, &addr_policy, -1))
  317. REJECT("Error in DirPolicy entry.");
  318. if (parse_addr_policy(options->SocksPolicy, &addr_policy, -1))
  319. REJECT("Error in SocksPolicy entry.");
  320. if (parse_addr_policy(options->AuthDirReject, &addr_policy,
  321. ADDR_POLICY_REJECT))
  322. REJECT("Error in AuthDirReject entry.");
  323. if (parse_addr_policy(options->AuthDirInvalid, &addr_policy,
  324. ADDR_POLICY_REJECT))
  325. REJECT("Error in AuthDirInvalid entry.");
  326. if (parse_addr_policy(options->AuthDirBadDir, &addr_policy,
  327. ADDR_POLICY_REJECT))
  328. REJECT("Error in AuthDirBadDir entry.");
  329. if (parse_addr_policy(options->AuthDirBadExit, &addr_policy,
  330. ADDR_POLICY_REJECT))
  331. REJECT("Error in AuthDirBadExit entry.");
  332. if (parse_addr_policy(options->ReachableAddresses, &addr_policy,
  333. ADDR_POLICY_ACCEPT))
  334. REJECT("Error in ReachableAddresses entry.");
  335. if (parse_addr_policy(options->ReachableORAddresses, &addr_policy,
  336. ADDR_POLICY_ACCEPT))
  337. REJECT("Error in ReachableORAddresses entry.");
  338. if (parse_addr_policy(options->ReachableDirAddresses, &addr_policy,
  339. ADDR_POLICY_ACCEPT))
  340. REJECT("Error in ReachableDirAddresses entry.");
  341. if (parse_addr_policy(options->AuthDirReject, &addr_policy,
  342. ADDR_POLICY_REJECT))
  343. REJECT("Error in AuthDirReject entry.");
  344. if (parse_addr_policy(options->AuthDirInvalid, &addr_policy,
  345. ADDR_POLICY_REJECT))
  346. REJECT("Error in AuthDirInvalid entry.");
  347. err:
  348. addr_policy_list_free(addr_policy);
  349. return *msg ? -1 : 0;
  350. #undef REJECT
  351. }
  352. /** Parse <b>string</b> in the same way that the exit policy
  353. * is parsed, and put the processed version in *<b>policy</b>.
  354. * Ignore port specifiers.
  355. */
  356. static int
  357. load_policy_from_option(config_line_t *config, smartlist_t **policy,
  358. int assume_action)
  359. {
  360. int r;
  361. addr_policy_list_free(*policy);
  362. *policy = NULL;
  363. r = parse_addr_policy(config, policy, assume_action);
  364. if (r < 0) {
  365. return -1;
  366. }
  367. if (*policy) {
  368. SMARTLIST_FOREACH(*policy, addr_policy_t *, n, {
  369. /* ports aren't used. */
  370. n->prt_min = 1;
  371. n->prt_max = 65535;
  372. });
  373. }
  374. return 0;
  375. }
  376. /** Set all policies based on <b>options</b>, which should have been validated
  377. * first by validate_addr_policies. */
  378. int
  379. policies_parse_from_options(or_options_t *options)
  380. {
  381. int ret = 0;
  382. if (load_policy_from_option(options->SocksPolicy, &socks_policy, -1) < 0)
  383. ret = -1;
  384. if (load_policy_from_option(options->DirPolicy, &dir_policy, -1) < 0)
  385. ret = -1;
  386. if (load_policy_from_option(options->AuthDirReject,
  387. &authdir_reject_policy, ADDR_POLICY_REJECT) < 0)
  388. ret = -1;
  389. if (load_policy_from_option(options->AuthDirInvalid,
  390. &authdir_invalid_policy, ADDR_POLICY_REJECT) < 0)
  391. ret = -1;
  392. if (load_policy_from_option(options->AuthDirBadDir,
  393. &authdir_baddir_policy, ADDR_POLICY_REJECT) < 0)
  394. ret = -1;
  395. if (load_policy_from_option(options->AuthDirBadExit,
  396. &authdir_badexit_policy, ADDR_POLICY_REJECT) < 0)
  397. ret = -1;
  398. if (parse_reachable_addresses() < 0)
  399. ret = -1;
  400. return ret;
  401. }
  402. /** Compare two provided address policy items, and return -1, 0, or 1
  403. * if the first is less than, equal to, or greater than the second. */
  404. static int
  405. cmp_single_addr_policy(addr_policy_t *a, addr_policy_t *b)
  406. {
  407. int r;
  408. if ((r=((int)a->policy_type - (int)b->policy_type)))
  409. return r;
  410. if ((r=((int)a->is_private - (int)b->is_private)))
  411. return r;
  412. if ((r=tor_addr_compare(&a->addr, &b->addr, CMP_EXACT)))
  413. return r;
  414. if ((r=((int)a->maskbits - (int)b->maskbits)))
  415. return r;
  416. if ((r=((int)a->prt_min - (int)b->prt_min)))
  417. return r;
  418. if ((r=((int)a->prt_max - (int)b->prt_max)))
  419. return r;
  420. return 0;
  421. }
  422. /** Like cmp_single_addr_policy() above, but looks at the
  423. * whole set of policies in each case. */
  424. int
  425. cmp_addr_policies(smartlist_t *a, smartlist_t *b)
  426. {
  427. int r, i;
  428. int len_a = a ? smartlist_len(a) : 0;
  429. int len_b = b ? smartlist_len(b) : 0;
  430. for (i = 0; i < len_a && i < len_b; ++i) {
  431. if ((r = cmp_single_addr_policy(smartlist_get(a, i), smartlist_get(b, i))))
  432. return r;
  433. }
  434. if (i == len_a && i == len_b)
  435. return 0;
  436. if (i < len_a)
  437. return -1;
  438. else
  439. return 1;
  440. }
  441. /** Node in hashtable used to store address policy entries. */
  442. typedef struct policy_map_ent_t {
  443. HT_ENTRY(policy_map_ent_t) node;
  444. addr_policy_t *policy;
  445. } policy_map_ent_t;
  446. static HT_HEAD(policy_map, policy_map_ent_t) policy_root = HT_INITIALIZER();
  447. /** Return true iff a and b are equal. */
  448. static INLINE int
  449. policy_eq(policy_map_ent_t *a, policy_map_ent_t *b)
  450. {
  451. return cmp_single_addr_policy(a->policy, b->policy) == 0;
  452. }
  453. /** Return a hashcode for <b>ent</b> */
  454. static unsigned int
  455. policy_hash(policy_map_ent_t *ent)
  456. {
  457. addr_policy_t *a = ent->policy;
  458. unsigned int r;
  459. if (a->is_private)
  460. r = 0x1234abcd;
  461. else
  462. r = tor_addr_hash(&a->addr);
  463. r += a->prt_min << 8;
  464. r += a->prt_max << 16;
  465. r += a->maskbits;
  466. if (a->policy_type == ADDR_POLICY_REJECT)
  467. r ^= 0xffffffff;
  468. return r;
  469. }
  470. HT_PROTOTYPE(policy_map, policy_map_ent_t, node, policy_hash,
  471. policy_eq)
  472. HT_GENERATE(policy_map, policy_map_ent_t, node, policy_hash,
  473. policy_eq, 0.6, malloc, realloc, free)
  474. /** Given a pointer to an addr_policy_t, return a copy of the pointer to the
  475. * "canonical" copy of that addr_policy_t; the canonical copy is a single
  476. * reference-counted object. */
  477. addr_policy_t *
  478. addr_policy_get_canonical_entry(addr_policy_t *e)
  479. {
  480. policy_map_ent_t search, *found;
  481. if (e->is_canonical)
  482. return e;
  483. search.policy = e;
  484. found = HT_FIND(policy_map, &policy_root, &search);
  485. if (!found) {
  486. found = tor_malloc_zero(sizeof(policy_map_ent_t));
  487. found->policy = tor_memdup(e, sizeof(addr_policy_t));
  488. found->policy->is_canonical = 1;
  489. found->policy->refcnt = 0;
  490. HT_INSERT(policy_map, &policy_root, found);
  491. }
  492. tor_assert(!cmp_single_addr_policy(found->policy, e));
  493. ++found->policy->refcnt;
  494. return found->policy;
  495. }
  496. /** DOCDOC */
  497. addr_policy_result_t
  498. compare_addr_to_addr_policy(uint32_t addr, uint16_t port, smartlist_t *policy)
  499. {
  500. /*XXXX021 deprecate this function? */
  501. tor_addr_t a;
  502. tor_addr_from_ipv4h(&a, addr);
  503. return compare_tor_addr_to_addr_policy(&a, port, policy);
  504. }
  505. /** Decide whether a given addr:port is definitely accepted,
  506. * definitely rejected, probably accepted, or probably rejected by a
  507. * given policy. If <b>addr</b> is 0, we don't know the IP of the
  508. * target address. If <b>port</b> is 0, we don't know the port of the
  509. * target address.
  510. *
  511. * For now, the algorithm is pretty simple: we look for definite and
  512. * uncertain matches. The first definite match is what we guess; if
  513. * it was preceded by no uncertain matches of the opposite policy,
  514. * then the guess is definite; otherwise it is probable. (If we
  515. * have a known addr and port, all matches are definite; if we have an
  516. * unknown addr/port, any address/port ranges other than "all" are
  517. * uncertain.)
  518. *
  519. * We could do better by assuming that some ranges never match typical
  520. * addresses (127.0.0.1, and so on). But we'll try this for now.
  521. */
  522. addr_policy_result_t
  523. compare_tor_addr_to_addr_policy(const tor_addr_t *addr, uint16_t port,
  524. smartlist_t *policy)
  525. {
  526. int maybe_reject = 0;
  527. int maybe_accept = 0;
  528. int match = 0;
  529. int maybe = 0;
  530. int i, len;
  531. int addr_is_unknown;
  532. addr_is_unknown = tor_addr_is_null(addr);
  533. len = policy ? smartlist_len(policy) : 0;
  534. for (i = 0; i < len; ++i) {
  535. addr_policy_t *tmpe = smartlist_get(policy, i);
  536. maybe = 0;
  537. if (addr_is_unknown) {
  538. /* Address is unknown. */
  539. if ((port >= tmpe->prt_min && port <= tmpe->prt_max) ||
  540. (!port && tmpe->prt_min<=1 && tmpe->prt_max>=65535)) {
  541. /* The port definitely matches. */
  542. if (tmpe->maskbits == 0) {
  543. match = 1;
  544. } else {
  545. maybe = 1;
  546. }
  547. } else if (!port) {
  548. /* The port maybe matches. */
  549. maybe = 1;
  550. }
  551. } else {
  552. /* Address is known */
  553. if (!tor_addr_compare_masked(addr, &tmpe->addr, tmpe->maskbits,
  554. CMP_SEMANTIC)) {
  555. if (port >= tmpe->prt_min && port <= tmpe->prt_max) {
  556. /* Exact match for the policy */
  557. match = 1;
  558. } else if (!port) {
  559. maybe = 1;
  560. }
  561. }
  562. }
  563. if (maybe) {
  564. if (tmpe->policy_type == ADDR_POLICY_REJECT)
  565. maybe_reject = 1;
  566. else
  567. maybe_accept = 1;
  568. }
  569. if (match) {
  570. if (tmpe->policy_type == ADDR_POLICY_ACCEPT) {
  571. /* If we already hit a clause that might trigger a 'reject', than we
  572. * can't be sure of this certain 'accept'.*/
  573. return maybe_reject ? ADDR_POLICY_PROBABLY_ACCEPTED :
  574. ADDR_POLICY_ACCEPTED;
  575. } else {
  576. return maybe_accept ? ADDR_POLICY_PROBABLY_REJECTED :
  577. ADDR_POLICY_REJECTED;
  578. }
  579. }
  580. }
  581. /* accept all by default. */
  582. return maybe_reject ? ADDR_POLICY_PROBABLY_ACCEPTED : ADDR_POLICY_ACCEPTED;
  583. }
  584. /** Return true iff the address policy <b>a</b> covers every case that
  585. * would be covered by <b>b</b>, so that a,b is redundant. */
  586. static int
  587. addr_policy_covers(addr_policy_t *a, addr_policy_t *b)
  588. {
  589. /* We can ignore accept/reject, since "accept *:80, reject *:80" reduces
  590. * to "accept *:80". */
  591. if (a->maskbits > b->maskbits) {
  592. /* a has more fixed bits than b; it can't possibly cover b. */
  593. return 0;
  594. }
  595. if (tor_addr_compare_masked(&a->addr, &b->addr, a->maskbits, CMP_SEMANTIC)) {
  596. /* There's a fixed bit in a that's set differently in b. */
  597. return 0;
  598. }
  599. return (a->prt_min <= b->prt_min && a->prt_max >= b->prt_max);
  600. }
  601. /** Return true iff the address policies <b>a</b> and <b>b</b> intersect,
  602. * that is, there exists an address/port that is covered by <b>a</b> that
  603. * is also covered by <b>b</b>.
  604. */
  605. static int
  606. addr_policy_intersects(addr_policy_t *a, addr_policy_t *b)
  607. {
  608. maskbits_t minbits;
  609. /* All the bits we care about are those that are set in both
  610. * netmasks. If they are equal in a and b's networkaddresses
  611. * then the networks intersect. If there is a difference,
  612. * then they do not. */
  613. if (a->maskbits < b->maskbits)
  614. minbits = a->maskbits;
  615. else
  616. minbits = b->maskbits;
  617. if (tor_addr_compare_masked(&a->addr, &b->addr, minbits, CMP_SEMANTIC))
  618. return 0;
  619. if (a->prt_max < b->prt_min || b->prt_max < a->prt_min)
  620. return 0;
  621. return 1;
  622. }
  623. /** Add the exit policy described by <b>more</b> to <b>policy</b>.
  624. */
  625. static void
  626. append_exit_policy_string(smartlist_t **policy, const char *more)
  627. {
  628. config_line_t tmp;
  629. tmp.key = NULL;
  630. tmp.value = (char*) more;
  631. tmp.next = NULL;
  632. if (parse_addr_policy(&tmp, policy, -1)<0) {
  633. log_warn(LD_BUG, "Unable to parse internally generated policy %s",more);
  634. }
  635. }
  636. /** Detect and excise "dead code" from the policy *<b>dest</b>. */
  637. static void
  638. exit_policy_remove_redundancies(smartlist_t *dest)
  639. {
  640. addr_policy_t *ap, *tmp, *victim;
  641. int i, j;
  642. /* Step one: find a *:* entry and cut off everything after it. */
  643. for (i = 0; i < smartlist_len(dest); ++i) {
  644. ap = smartlist_get(dest, i);
  645. if (ap->maskbits == 0 && ap->prt_min <= 1 && ap->prt_max >= 65535) {
  646. /* This is a catch-all line -- later lines are unreachable. */
  647. while (i+1 < smartlist_len(dest)) {
  648. victim = smartlist_get(dest, i+1);
  649. smartlist_del(dest, i+1);
  650. addr_policy_free(victim);
  651. }
  652. break;
  653. }
  654. }
  655. /* Step two: for every entry, see if there's a redundant entry
  656. * later on, and remove it. */
  657. for (i = 0; i < smartlist_len(dest)-1; ++i) {
  658. ap = smartlist_get(dest, i);
  659. for (j = i+1; j < smartlist_len(dest); ++j) {
  660. tmp = smartlist_get(dest, j);
  661. tor_assert(j > i);
  662. if (addr_policy_covers(ap, tmp)) {
  663. char p1[POLICY_BUF_LEN], p2[POLICY_BUF_LEN];
  664. policy_write_item(p1, sizeof(p1), tmp, 0);
  665. policy_write_item(p2, sizeof(p2), ap, 0);
  666. log(LOG_DEBUG, LD_CONFIG, "Removing exit policy %s (%d). It is made "
  667. "redundant by %s (%d).", p1, j, p2, i);
  668. smartlist_del_keeporder(dest, j--);
  669. addr_policy_free(tmp);
  670. }
  671. }
  672. }
  673. /* Step three: for every entry A, see if there's an entry B making this one
  674. * redundant later on. This is the case if A and B are of the same type
  675. * (accept/reject), A is a subset of B, and there is no other entry of
  676. * different type in between those two that intersects with A.
  677. *
  678. * Anybody want to doublecheck the logic here? XXX
  679. */
  680. for (i = 0; i < smartlist_len(dest)-1; ++i) {
  681. ap = smartlist_get(dest, i);
  682. for (j = i+1; j < smartlist_len(dest); ++j) {
  683. // tor_assert(j > i); // j starts out at i+1; j only increases; i only
  684. // // decreases.
  685. tmp = smartlist_get(dest, j);
  686. if (ap->policy_type != tmp->policy_type) {
  687. if (addr_policy_intersects(ap, tmp))
  688. break;
  689. } else { /* policy_types are equal. */
  690. if (addr_policy_covers(tmp, ap)) {
  691. char p1[POLICY_BUF_LEN], p2[POLICY_BUF_LEN];
  692. policy_write_item(p1, sizeof(p1), ap, 0);
  693. policy_write_item(p2, sizeof(p2), tmp, 0);
  694. log(LOG_DEBUG, LD_CONFIG, "Removing exit policy %s. It is already "
  695. "covered by %s.", p1, p2);
  696. smartlist_del_keeporder(dest, i--);
  697. addr_policy_free(ap);
  698. break;
  699. }
  700. }
  701. }
  702. }
  703. }
  704. #define DEFAULT_EXIT_POLICY \
  705. "reject *:25,reject *:119,reject *:135-139,reject *:445," \
  706. "reject *:465,reject *:563,reject *:587," \
  707. "reject *:1214,reject *:4661-4666," \
  708. "reject *:6346-6429,reject *:6699,reject *:6881-6999,accept *:*"
  709. /** Parse the exit policy <b>cfg</b> into the linked list *<b>dest</b>. If
  710. * cfg doesn't end in an absolute accept or reject, add the default exit
  711. * policy afterwards. If <b>rejectprivate</b> is true, prepend
  712. * "reject private:*" to the policy. Return -1 if we can't parse cfg,
  713. * else return 0.
  714. */
  715. int
  716. policies_parse_exit_policy(config_line_t *cfg, smartlist_t **dest,
  717. int rejectprivate, const char *local_address)
  718. {
  719. if (rejectprivate) {
  720. append_exit_policy_string(dest, "reject private:*");
  721. if (local_address) {
  722. char buf[POLICY_BUF_LEN];
  723. tor_snprintf(buf, sizeof(buf), "reject %s:*", local_address);
  724. append_exit_policy_string(dest, buf);
  725. }
  726. }
  727. if (parse_addr_policy(cfg, dest, -1))
  728. return -1;
  729. append_exit_policy_string(dest, DEFAULT_EXIT_POLICY);
  730. exit_policy_remove_redundancies(*dest);
  731. return 0;
  732. }
  733. /** Replace the exit policy of <b>r</b> with reject *:*. */
  734. void
  735. policies_set_router_exitpolicy_to_reject_all(routerinfo_t *r)
  736. {
  737. addr_policy_t *item;
  738. addr_policy_list_free(r->exit_policy);
  739. r->exit_policy = smartlist_create();
  740. item = router_parse_addr_policy_item_from_string("reject *:*", -1);
  741. smartlist_add(r->exit_policy, item);
  742. }
  743. /** Return true iff <b>ri</b> is "useful as an exit node", meaning
  744. * it allows exit to at least one /8 address space for at least
  745. * two of ports 80, 443, and 6667. */
  746. int
  747. exit_policy_is_general_exit(smartlist_t *policy)
  748. {
  749. static const int ports[] = { 80, 443, 6667 };
  750. int n_allowed = 0;
  751. int i;
  752. if (!policy) /*XXXX021 disallow NULL policies */
  753. return 0;
  754. for (i = 0; i < 3; ++i) {
  755. SMARTLIST_FOREACH(policy, addr_policy_t *, p, {
  756. if (p->prt_min > ports[i] || p->prt_max < ports[i])
  757. continue; /* Doesn't cover our port. */
  758. if (p->maskbits > 8)
  759. continue; /* Narrower than a /8. */
  760. if (tor_addr_is_loopback(&p->addr))
  761. continue; /* 127.x or ::1. */
  762. /* We have a match that is at least a /8. */
  763. if (p->policy_type == ADDR_POLICY_ACCEPT) {
  764. ++n_allowed;
  765. break; /* stop considering this port */
  766. }
  767. });
  768. }
  769. return n_allowed >= 2;
  770. }
  771. /** Return false if <b>policy</b> might permit access to some addr:port;
  772. * otherwise if we are certain it rejects everything, return true. */
  773. int
  774. policy_is_reject_star(smartlist_t *policy)
  775. {
  776. if (!policy) /*XXXX021 disallow NULL policies */
  777. return 1;
  778. SMARTLIST_FOREACH(policy, addr_policy_t *, p, {
  779. if (p->policy_type == ADDR_POLICY_ACCEPT)
  780. return 0;
  781. else if (p->policy_type == ADDR_POLICY_REJECT &&
  782. p->prt_min <= 1 && p->prt_max == 65535 &&
  783. p->maskbits == 0)
  784. return 1;
  785. });
  786. return 1;
  787. }
  788. /** Write a single address policy to the buf_len byte buffer at buf. Return
  789. * the number of characters written, or -1 on failure. */
  790. int
  791. policy_write_item(char *buf, size_t buflen, addr_policy_t *policy,
  792. int format_for_desc)
  793. {
  794. size_t written = 0;
  795. char addrbuf[TOR_ADDR_BUF_LEN];
  796. const char *addrpart;
  797. int result;
  798. const int is_accept = policy->policy_type == ADDR_POLICY_ACCEPT;
  799. const int is_ip6 = tor_addr_family(&policy->addr) == AF_INET6;
  800. tor_addr_to_str(addrbuf, &policy->addr, sizeof(addrbuf), 1);
  801. /* write accept/reject 1.2.3.4 */
  802. if (policy->is_private)
  803. addrpart = "private";
  804. else if (policy->maskbits == 0)
  805. addrpart = "*";
  806. else
  807. addrpart = addrbuf;
  808. result = tor_snprintf(buf, buflen, "%s%s%s %s",
  809. (is_ip6&&format_for_desc)?"opt ":"",
  810. is_accept ? "accept" : "reject",
  811. (is_ip6&&format_for_desc)?"6":"",
  812. addrpart);
  813. if (result < 0)
  814. return -1;
  815. written += strlen(buf);
  816. /* If the maskbits is 32 we don't need to give it. If the mask is 0,
  817. * we already wrote "*". */
  818. if (policy->maskbits < 32 && policy->maskbits > 0) {
  819. if (tor_snprintf(buf+written, buflen-written, "/%d", policy->maskbits)<0)
  820. return -1;
  821. written += strlen(buf+written);
  822. }
  823. if (policy->prt_min <= 1 && policy->prt_max == 65535) {
  824. /* There is no port set; write ":*" */
  825. if (written+4 > buflen)
  826. return -1;
  827. strlcat(buf+written, ":*", buflen-written);
  828. written += 2;
  829. } else if (policy->prt_min == policy->prt_max) {
  830. /* There is only one port; write ":80". */
  831. result = tor_snprintf(buf+written, buflen-written, ":%d", policy->prt_min);
  832. if (result<0)
  833. return -1;
  834. written += result;
  835. } else {
  836. /* There is a range of ports; write ":79-80". */
  837. result = tor_snprintf(buf+written, buflen-written, ":%d-%d",
  838. policy->prt_min, policy->prt_max);
  839. if (result<0)
  840. return -1;
  841. written += result;
  842. }
  843. if (written < buflen)
  844. buf[written] = '\0';
  845. else
  846. return -1;
  847. return (int)written;
  848. }
  849. /** Create a new exit policy summary, initially only with a single
  850. * port 1-64k item */
  851. /* XXXX This entire thing will do most stuff in O(N^2), or worse. Use an
  852. * RB-tree if that turns out to matter. */
  853. smartlist_t *
  854. policy_summary_create(void)
  855. {
  856. smartlist_t *summary;
  857. policy_summary_item_t* item;
  858. item = tor_malloc_zero(sizeof(policy_summary_item_t));
  859. item->prt_min = 1;
  860. item->prt_max = 65535;
  861. item->reject_count = 0;
  862. item->accepted = 0;
  863. summary = smartlist_create();
  864. smartlist_add(summary, item);
  865. return summary;
  866. }
  867. /** Split the summary item in <b>item</b> at the port <b>new_starts</b>.
  868. * The current item is changed to end at new-starts - 1, the new item
  869. * copies reject_count and accepted from the old item,
  870. * starts at new_starts and ends at the port where the original item
  871. * previously ended.
  872. */
  873. policy_summary_item_t*
  874. policy_summary_item_split(policy_summary_item_t* old, uint16_t new_starts) {
  875. policy_summary_item_t* new;
  876. new = tor_malloc_zero(sizeof(policy_summary_item_t));
  877. new->prt_min = new_starts;
  878. new->prt_max = old->prt_max;
  879. new->reject_count = old->reject_count;
  880. new->accepted = old->accepted;
  881. old->prt_max = new_starts-1;
  882. tor_assert(old->prt_min < old->prt_max);
  883. tor_assert(new->prt_min < new->prt_max);
  884. return new;
  885. }
  886. /* XXXX Nick says I'm going to hell for this. If he feels charitably towards
  887. * my immortal soul, he can clean it up himself. */
  888. #define AT(x) ((policy_summary_item_t*)smartlist_get(summary, x))
  889. #define REJECT_CUTOFF_COUNT (1<<25)
  890. /* Split an exit policy summary so that prt_min and prt_max
  891. * fall at exactly the start and end of an item respectively.
  892. */
  893. int
  894. policy_summary_split(smartlist_t *summary,
  895. uint16_t prt_min, uint16_t prt_max)
  896. {
  897. int start_at_index;
  898. int i = 0;
  899. /* XXXX Do a binary search if run time matters */
  900. while (AT(i)->prt_max < prt_min)
  901. i++;
  902. if (AT(i)->prt_min != prt_min) {
  903. policy_summary_item_t* new_item;
  904. new_item = policy_summary_item_split(AT(i), prt_min);
  905. smartlist_insert(summary, i+1, new_item);
  906. i++;
  907. }
  908. start_at_index = i;
  909. while(AT(i)->prt_max < prt_max)
  910. i++;
  911. if (AT(i)->prt_max != prt_max) {
  912. policy_summary_item_t* new_item;
  913. new_item = policy_summary_item_split(AT(i), prt_max+1);
  914. smartlist_insert(summary, i+1, new_item);
  915. }
  916. return start_at_index;
  917. }
  918. /** Mark port ranges as accepted if they are below the reject_count */
  919. void
  920. policy_summary_accept(smartlist_t *summary,
  921. uint16_t prt_min, uint16_t prt_max)
  922. {
  923. int i = policy_summary_split(summary, prt_min, prt_max);
  924. while (AT(i)->prt_max <= prt_max) {
  925. if (AT(i)->accepted ||
  926. AT(i)->reject_count > REJECT_CUTOFF_COUNT)
  927. continue;
  928. AT(i)->accepted = 1;
  929. i++;
  930. }
  931. }
  932. /** Count the number of addresses in a network with prefixlen maskbits
  933. * against the given portrange. */
  934. void
  935. policy_summary_reject(smartlist_t *summary,
  936. maskbits_t maskbits,
  937. uint16_t prt_min, uint16_t prt_max)
  938. {
  939. int i = policy_summary_split(summary, prt_min, prt_max);
  940. /* XXX: ipv4 specific */
  941. int count = (1 << (32-maskbits));
  942. while (AT(i)->prt_max <= prt_max) {
  943. AT(i)->reject_count += count;
  944. i++;
  945. }
  946. }
  947. /** Add a single exit policy item to our summary:
  948. * If it is an accept ignore it unless it is for all IP addresses
  949. * ("*"), i.e. it's prefixlen/maskbits is 0, else call
  950. * policy_summary_accept().
  951. * If it's a reject ignore it if it is about one of the private
  952. * networks, else call policy_summary_reject().
  953. */
  954. void
  955. policy_summary_add_item(smartlist_t *summary, addr_policy_t *p)
  956. {
  957. if (p->policy_type == ADDR_POLICY_ACCEPT) {
  958. if (p->maskbits != 0) {
  959. policy_summary_accept(summary, p->prt_min, p->prt_max);
  960. }
  961. } else if (p->policy_type == ADDR_POLICY_REJECT) {
  962. int is_private = 0;
  963. int i;
  964. for (i = 0; private_nets[i]; ++i) {
  965. tor_addr_t addr;
  966. maskbits_t maskbits;
  967. if (tor_addr_parse_mask_ports(private_nets[i], &addr,
  968. &maskbits, NULL, NULL)<0) {
  969. tor_assert(0);
  970. }
  971. if (tor_addr_compare(&p->addr, &addr, CMP_EXACT) == 0 &&
  972. p->maskbits == maskbits) {
  973. is_private = 1;
  974. break;
  975. }
  976. }
  977. if (!is_private) {
  978. policy_summary_reject(summary, p->maskbits, p->prt_min, p->prt_max);
  979. }
  980. } else
  981. tor_assert(0);
  982. }
  983. /** Create a string representing a summary for an exit policy.
  984. * The summary will either be an "accept" plus a comma-seperated list of port
  985. * ranges or a "reject" plus portranges, depending on which is shorter.
  986. */
  987. char *
  988. policy_summarize(smartlist_t *policy)
  989. {
  990. smartlist_t *summary = policy_summary_create();
  991. smartlist_t *accepts, *rejects;
  992. int i, last, start_prt;
  993. size_t accepts_len, rejects_len, shorter_len, final_size;
  994. char *accepts_str, *rejects_str, *shorter_str, *result;
  995. const char *prefix;
  996. tor_assert(policy);
  997. /* Create the summary list */
  998. SMARTLIST_FOREACH(policy, addr_policy_t *, p, {
  999. policy_summary_add_item(summary, p);
  1000. });
  1001. /* Now create two lists of strings, one for accepted and one
  1002. * for rejected ports. We take care to merge ranges so that
  1003. * we avoid getting stuff like "1-4,5-9,10", instead we want
  1004. * "1-10"
  1005. */
  1006. i = 0;
  1007. start_prt = 1;
  1008. accepts = smartlist_create();
  1009. rejects = smartlist_create();
  1010. while (1) {
  1011. last = i == smartlist_len(summary)-1;
  1012. if (last ||
  1013. AT(i)->accepted != AT(i+1)->accepted) {
  1014. char buf[POLICY_BUF_LEN];
  1015. if (start_prt == AT(i)->prt_max)
  1016. tor_snprintf(buf, sizeof(buf), "%d", start_prt);
  1017. else
  1018. tor_snprintf(buf, sizeof(buf), "%d-%d", start_prt, AT(i)->prt_max);
  1019. if (AT(i)->accepted)
  1020. smartlist_add(accepts, tor_strdup(buf));
  1021. else
  1022. smartlist_add(rejects, tor_strdup(buf));
  1023. if (last)
  1024. break;
  1025. start_prt = AT(i+1)->prt_min;
  1026. };
  1027. i++;
  1028. };
  1029. /* Figure out which of the two stringlists will be shorter and use
  1030. * that to build the result
  1031. */
  1032. accepts_str = smartlist_join_strings(accepts, ",", 0, &accepts_len);
  1033. rejects_str = smartlist_join_strings(rejects, ",", 0, &rejects_len);
  1034. if (rejects_len < accepts_len) {
  1035. shorter_str = rejects_str;
  1036. shorter_len = rejects_len;
  1037. prefix = "reject";
  1038. } else {
  1039. shorter_str = accepts_str;
  1040. shorter_len = accepts_len;
  1041. prefix = "accept";
  1042. }
  1043. final_size = strlen(prefix)+1+shorter_len+1;
  1044. result = malloc(final_size);
  1045. tor_snprintf(result, final_size, "%s %s", prefix, shorter_str);
  1046. /* cleanup */
  1047. SMARTLIST_FOREACH(summary, policy_summary_item_t *, s, tor_free(s));
  1048. smartlist_clear(summary);
  1049. tor_free(accepts_str);
  1050. SMARTLIST_FOREACH(accepts, char *, s, tor_free(s));
  1051. smartlist_clear(accepts);
  1052. tor_free(rejects_str);
  1053. SMARTLIST_FOREACH(rejects, char *, s, tor_free(s));
  1054. smartlist_clear(rejects);
  1055. return result;
  1056. }
  1057. /** Implementation for GETINFO control command: knows the answer for questions
  1058. * about "exit-policy/..." */
  1059. int
  1060. getinfo_helper_policies(control_connection_t *conn,
  1061. const char *question, char **answer)
  1062. {
  1063. (void) conn;
  1064. if (!strcmp(question, "exit-policy/default")) {
  1065. *answer = tor_strdup(DEFAULT_EXIT_POLICY);
  1066. }
  1067. return 0;
  1068. }
  1069. /** Release all storage held by <b>p</b>. */
  1070. void
  1071. addr_policy_list_free(smartlist_t *lst)
  1072. {
  1073. if (!lst) return;
  1074. SMARTLIST_FOREACH(lst, addr_policy_t *, policy, addr_policy_free(policy));
  1075. smartlist_free(lst);
  1076. }
  1077. /** Release all storage held by <b>p</b>. */
  1078. void
  1079. addr_policy_free(addr_policy_t *p)
  1080. {
  1081. if (p) {
  1082. if (--p->refcnt <= 0) {
  1083. if (p->is_canonical) {
  1084. policy_map_ent_t search, *found;
  1085. search.policy = p;
  1086. found = HT_REMOVE(policy_map, &policy_root, &search);
  1087. if (found) {
  1088. tor_assert(p == found->policy);
  1089. tor_free(found);
  1090. }
  1091. }
  1092. tor_free(p);
  1093. }
  1094. }
  1095. }
  1096. /** Release all storage held by policy variables. */
  1097. void
  1098. policies_free_all(void)
  1099. {
  1100. addr_policy_list_free(reachable_or_addr_policy);
  1101. reachable_or_addr_policy = NULL;
  1102. addr_policy_list_free(reachable_dir_addr_policy);
  1103. reachable_dir_addr_policy = NULL;
  1104. addr_policy_list_free(socks_policy);
  1105. socks_policy = NULL;
  1106. addr_policy_list_free(dir_policy);
  1107. dir_policy = NULL;
  1108. addr_policy_list_free(authdir_reject_policy);
  1109. authdir_reject_policy = NULL;
  1110. addr_policy_list_free(authdir_invalid_policy);
  1111. authdir_invalid_policy = NULL;
  1112. addr_policy_list_free(authdir_baddir_policy);
  1113. authdir_baddir_policy = NULL;
  1114. addr_policy_list_free(authdir_badexit_policy);
  1115. authdir_badexit_policy = NULL;
  1116. if (!HT_EMPTY(&policy_root))
  1117. log_warn(LD_MM, "Still had some address policies cached at shutdown.");
  1118. HT_CLEAR(policy_map, &policy_root);
  1119. }
  1120. /* vim:set et ts=2 shiftwidth=2: */