policies.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2013, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /**
  6. * \file policies.c
  7. * \brief Code to parse and use address policies and exit policies.
  8. **/
  9. #include "or.h"
  10. #include "config.h"
  11. #include "dirserv.h"
  12. #include "nodelist.h"
  13. #include "policies.h"
  14. #include "router.h"
  15. #include "routerparse.h"
  16. #include "geoip.h"
  17. #include "ht.h"
  18. /** Policy that addresses for incoming SOCKS connections must match. */
  19. static smartlist_t *socks_policy = NULL;
  20. /** Policy that addresses for incoming directory connections must match. */
  21. static smartlist_t *dir_policy = NULL;
  22. /** Policy that addresses for incoming router descriptors must match in order
  23. * to be published by us. */
  24. static smartlist_t *authdir_reject_policy = NULL;
  25. /** Policy that addresses for incoming router descriptors must match in order
  26. * to be marked as valid in our networkstatus. */
  27. static smartlist_t *authdir_invalid_policy = NULL;
  28. /** Policy that addresses for incoming router descriptors must <b>not</b>
  29. * match in order to not be marked as BadDirectory. */
  30. static smartlist_t *authdir_baddir_policy = NULL;
  31. /** Policy that addresses for incoming router descriptors must <b>not</b>
  32. * match in order to not be marked as BadExit. */
  33. static smartlist_t *authdir_badexit_policy = NULL;
  34. /** Parsed addr_policy_t describing which addresses we believe we can start
  35. * circuits at. */
  36. static smartlist_t *reachable_or_addr_policy = NULL;
  37. /** Parsed addr_policy_t describing which addresses we believe we can connect
  38. * to directories at. */
  39. static smartlist_t *reachable_dir_addr_policy = NULL;
  40. /** Element of an exit policy summary */
  41. typedef struct policy_summary_item_t {
  42. uint16_t prt_min; /**< Lowest port number to accept/reject. */
  43. uint16_t prt_max; /**< Highest port number to accept/reject. */
  44. uint64_t reject_count; /**< Number of IP-Addresses that are rejected to
  45. this port range. */
  46. unsigned int accepted:1; /** Has this port already been accepted */
  47. } policy_summary_item_t;
  48. /** Private networks. This list is used in two places, once to expand the
  49. * "private" keyword when parsing our own exit policy, secondly to ignore
  50. * just such networks when building exit policy summaries. It is important
  51. * that all authorities agree on that list when creating summaries, so don't
  52. * just change this without a proper migration plan and a proposal and stuff.
  53. */
  54. static const char *private_nets[] = {
  55. "0.0.0.0/8", "169.254.0.0/16",
  56. "127.0.0.0/8", "192.168.0.0/16", "10.0.0.0/8", "172.16.0.0/12",
  57. "[::]/8",
  58. "[fc00::]/7", "[fe80::]/10", "[fec0::]/10", "[ff00::]/8", "[::]/127",
  59. NULL
  60. };
  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) /*XXXX disallow NULL policies? */
  70. return;
  71. tmp = smartlist_new();
  72. SMARTLIST_FOREACH_BEGIN(*policy, addr_policy_t *, p) {
  73. if (! p->is_private) {
  74. smartlist_add(tmp, p);
  75. continue;
  76. }
  77. for (i = 0; private_nets[i]; ++i) {
  78. addr_policy_t newpolicy;
  79. memcpy(&newpolicy, p, sizeof(addr_policy_t));
  80. newpolicy.is_private = 0;
  81. newpolicy.is_canonical = 0;
  82. if (tor_addr_parse_mask_ports(private_nets[i], 0,
  83. &newpolicy.addr,
  84. &newpolicy.maskbits, &port_min, &port_max)<0) {
  85. tor_assert(0);
  86. }
  87. smartlist_add(tmp, addr_policy_get_canonical_entry(&newpolicy));
  88. }
  89. addr_policy_free(p);
  90. } SMARTLIST_FOREACH_END(p);
  91. smartlist_free(*policy);
  92. *policy = tmp;
  93. }
  94. /** Expand each of the AF_UNSPEC elements in *<b>policy</b> (which indicate
  95. * protocol-neutral wildcards) into a pair of wildcard elements: one IPv4-
  96. * specific and one IPv6-specific. */
  97. void
  98. policy_expand_unspec(smartlist_t **policy)
  99. {
  100. smartlist_t *tmp;
  101. if (!*policy)
  102. return;
  103. tmp = smartlist_new();
  104. SMARTLIST_FOREACH_BEGIN(*policy, addr_policy_t *, p) {
  105. sa_family_t family = tor_addr_family(&p->addr);
  106. if (family == AF_INET6 || family == AF_INET || p->is_private) {
  107. smartlist_add(tmp, p);
  108. } else if (family == AF_UNSPEC) {
  109. addr_policy_t newpolicy_ipv4;
  110. addr_policy_t newpolicy_ipv6;
  111. memcpy(&newpolicy_ipv4, p, sizeof(addr_policy_t));
  112. memcpy(&newpolicy_ipv6, p, sizeof(addr_policy_t));
  113. newpolicy_ipv4.is_canonical = 0;
  114. newpolicy_ipv6.is_canonical = 0;
  115. if (p->maskbits != 0) {
  116. log_warn(LD_BUG, "AF_UNSPEC policy with maskbits==%d", p->maskbits);
  117. newpolicy_ipv4.maskbits = 0;
  118. newpolicy_ipv6.maskbits = 0;
  119. }
  120. tor_addr_from_ipv4h(&newpolicy_ipv4.addr, 0);
  121. tor_addr_from_ipv6_bytes(&newpolicy_ipv6.addr,
  122. "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0");
  123. smartlist_add(tmp, addr_policy_get_canonical_entry(&newpolicy_ipv4));
  124. smartlist_add(tmp, addr_policy_get_canonical_entry(&newpolicy_ipv6));
  125. addr_policy_free(p);
  126. } else {
  127. log_warn(LD_BUG, "Funny-looking address policy with family %d", family);
  128. smartlist_add(tmp, p);
  129. }
  130. } SMARTLIST_FOREACH_END(p);
  131. smartlist_free(*policy);
  132. *policy = tmp;
  133. }
  134. /**
  135. * Given a linked list of config lines containing "allow" and "deny"
  136. * tokens, parse them and append the result to <b>dest</b>. Return -1
  137. * if any tokens are malformed (and don't append any), else return 0.
  138. *
  139. * If <b>assume_action</b> is nonnegative, then insert its action
  140. * (ADDR_POLICY_ACCEPT or ADDR_POLICY_REJECT) for items that specify no
  141. * action.
  142. */
  143. static int
  144. parse_addr_policy(config_line_t *cfg, smartlist_t **dest,
  145. int assume_action)
  146. {
  147. smartlist_t *result;
  148. smartlist_t *entries;
  149. addr_policy_t *item;
  150. int r = 0;
  151. if (!cfg)
  152. return 0;
  153. result = smartlist_new();
  154. entries = smartlist_new();
  155. for (; cfg; cfg = cfg->next) {
  156. smartlist_split_string(entries, cfg->value, ",",
  157. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  158. SMARTLIST_FOREACH_BEGIN(entries, const char *, ent) {
  159. log_debug(LD_CONFIG,"Adding new entry '%s'",ent);
  160. item = router_parse_addr_policy_item_from_string(ent, assume_action);
  161. if (item) {
  162. smartlist_add(result, item);
  163. } else {
  164. log_warn(LD_CONFIG,"Malformed policy '%s'.", ent);
  165. r = -1;
  166. }
  167. } SMARTLIST_FOREACH_END(ent);
  168. SMARTLIST_FOREACH(entries, char *, ent, tor_free(ent));
  169. smartlist_clear(entries);
  170. }
  171. smartlist_free(entries);
  172. if (r == -1) {
  173. addr_policy_list_free(result);
  174. } else {
  175. policy_expand_private(&result);
  176. policy_expand_unspec(&result);
  177. if (*dest) {
  178. smartlist_add_all(*dest, result);
  179. smartlist_free(result);
  180. } else {
  181. *dest = result;
  182. }
  183. }
  184. return r;
  185. }
  186. /** Helper: parse the Reachable(Dir|OR)?Addresses fields into
  187. * reachable_(or|dir)_addr_policy. The options should already have
  188. * been validated by validate_addr_policies.
  189. */
  190. static int
  191. parse_reachable_addresses(void)
  192. {
  193. const or_options_t *options = get_options();
  194. int ret = 0;
  195. if (options->ReachableDirAddresses &&
  196. options->ReachableORAddresses &&
  197. options->ReachableAddresses) {
  198. log_warn(LD_CONFIG,
  199. "Both ReachableDirAddresses and ReachableORAddresses are set. "
  200. "ReachableAddresses setting will be ignored.");
  201. }
  202. addr_policy_list_free(reachable_or_addr_policy);
  203. reachable_or_addr_policy = NULL;
  204. if (!options->ReachableORAddresses && options->ReachableAddresses)
  205. log_info(LD_CONFIG,
  206. "Using ReachableAddresses as ReachableORAddresses.");
  207. if (parse_addr_policy(options->ReachableORAddresses ?
  208. options->ReachableORAddresses :
  209. options->ReachableAddresses,
  210. &reachable_or_addr_policy, ADDR_POLICY_ACCEPT)) {
  211. log_warn(LD_CONFIG,
  212. "Error parsing Reachable%sAddresses entry; ignoring.",
  213. options->ReachableORAddresses ? "OR" : "");
  214. ret = -1;
  215. }
  216. addr_policy_list_free(reachable_dir_addr_policy);
  217. reachable_dir_addr_policy = NULL;
  218. if (!options->ReachableDirAddresses && options->ReachableAddresses)
  219. log_info(LD_CONFIG,
  220. "Using ReachableAddresses as ReachableDirAddresses");
  221. if (parse_addr_policy(options->ReachableDirAddresses ?
  222. options->ReachableDirAddresses :
  223. options->ReachableAddresses,
  224. &reachable_dir_addr_policy, ADDR_POLICY_ACCEPT)) {
  225. if (options->ReachableDirAddresses)
  226. log_warn(LD_CONFIG,
  227. "Error parsing ReachableDirAddresses entry; ignoring.");
  228. ret = -1;
  229. }
  230. return ret;
  231. }
  232. /** Return true iff the firewall options might block any address:port
  233. * combination.
  234. */
  235. int
  236. firewall_is_fascist_or(void)
  237. {
  238. return reachable_or_addr_policy != NULL;
  239. }
  240. /** Return true iff <b>policy</b> (possibly NULL) will allow a
  241. * connection to <b>addr</b>:<b>port</b>.
  242. */
  243. static int
  244. addr_policy_permits_tor_addr(const tor_addr_t *addr, uint16_t port,
  245. smartlist_t *policy)
  246. {
  247. addr_policy_result_t p;
  248. p = compare_tor_addr_to_addr_policy(addr, port, policy);
  249. switch (p) {
  250. case ADDR_POLICY_PROBABLY_ACCEPTED:
  251. case ADDR_POLICY_ACCEPTED:
  252. return 1;
  253. case ADDR_POLICY_PROBABLY_REJECTED:
  254. case ADDR_POLICY_REJECTED:
  255. return 0;
  256. default:
  257. log_warn(LD_BUG, "Unexpected result: %d", (int)p);
  258. return 0;
  259. }
  260. }
  261. /** Return true iff <b> policy</b> (possibly NULL) will allow a connection to
  262. * <b>addr</b>:<b>port</b>. <b>addr</b> is an IPv4 address given in host
  263. * order. */
  264. /* XXXX deprecate when possible. */
  265. static int
  266. addr_policy_permits_address(uint32_t addr, uint16_t port,
  267. smartlist_t *policy)
  268. {
  269. tor_addr_t a;
  270. tor_addr_from_ipv4h(&a, addr);
  271. return addr_policy_permits_tor_addr(&a, port, policy);
  272. }
  273. /** Return true iff we think our firewall will let us make an OR connection to
  274. * addr:port. */
  275. int
  276. fascist_firewall_allows_address_or(const tor_addr_t *addr, uint16_t port)
  277. {
  278. return addr_policy_permits_tor_addr(addr, port,
  279. reachable_or_addr_policy);
  280. }
  281. /** Return true iff we think our firewall will let us make an OR connection to
  282. * <b>ri</b>. */
  283. int
  284. fascist_firewall_allows_or(const routerinfo_t *ri)
  285. {
  286. /* XXXX proposal 118 */
  287. tor_addr_t addr;
  288. tor_addr_from_ipv4h(&addr, ri->addr);
  289. return fascist_firewall_allows_address_or(&addr, ri->or_port);
  290. }
  291. /** Return true iff we think our firewall will let us make an OR connection to
  292. * <b>node</b>. */
  293. int
  294. fascist_firewall_allows_node(const node_t *node)
  295. {
  296. if (node->ri) {
  297. return fascist_firewall_allows_or(node->ri);
  298. } else if (node->rs) {
  299. tor_addr_t addr;
  300. tor_addr_from_ipv4h(&addr, node->rs->addr);
  301. return fascist_firewall_allows_address_or(&addr, node->rs->or_port);
  302. } else {
  303. return 1;
  304. }
  305. }
  306. /** Return true iff we think our firewall will let us make a directory
  307. * connection to addr:port. */
  308. int
  309. fascist_firewall_allows_address_dir(const tor_addr_t *addr, uint16_t port)
  310. {
  311. return addr_policy_permits_tor_addr(addr, port,
  312. reachable_dir_addr_policy);
  313. }
  314. /** Return 1 if <b>addr</b> is permitted to connect to our dir port,
  315. * based on <b>dir_policy</b>. Else return 0.
  316. */
  317. int
  318. dir_policy_permits_address(const tor_addr_t *addr)
  319. {
  320. return addr_policy_permits_tor_addr(addr, 1, dir_policy);
  321. }
  322. /** Return 1 if <b>addr</b> is permitted to connect to our socks port,
  323. * based on <b>socks_policy</b>. Else return 0.
  324. */
  325. int
  326. socks_policy_permits_address(const tor_addr_t *addr)
  327. {
  328. return addr_policy_permits_tor_addr(addr, 1, socks_policy);
  329. }
  330. /** Return true iff the address <b>addr</b> is in a country listed in the
  331. * case-insensitive list of country codes <b>cc_list</b>. */
  332. static int
  333. addr_is_in_cc_list(uint32_t addr, const smartlist_t *cc_list)
  334. {
  335. country_t country;
  336. const char *name;
  337. tor_addr_t tar;
  338. if (!cc_list)
  339. return 0;
  340. /* XXXXipv6 */
  341. tor_addr_from_ipv4h(&tar, addr);
  342. country = geoip_get_country_by_addr(&tar);
  343. name = geoip_get_country_name(country);
  344. return smartlist_contains_string_case(cc_list, name);
  345. }
  346. /** Return 1 if <b>addr</b>:<b>port</b> is permitted to publish to our
  347. * directory, based on <b>authdir_reject_policy</b>. Else return 0.
  348. */
  349. int
  350. authdir_policy_permits_address(uint32_t addr, uint16_t port)
  351. {
  352. if (! addr_policy_permits_address(addr, port, authdir_reject_policy))
  353. return 0;
  354. return !addr_is_in_cc_list(addr, get_options()->AuthDirRejectCCs);
  355. }
  356. /** Return 1 if <b>addr</b>:<b>port</b> is considered valid in our
  357. * directory, based on <b>authdir_invalid_policy</b>. Else return 0.
  358. */
  359. int
  360. authdir_policy_valid_address(uint32_t addr, uint16_t port)
  361. {
  362. if (! addr_policy_permits_address(addr, port, authdir_invalid_policy))
  363. return 0;
  364. return !addr_is_in_cc_list(addr, get_options()->AuthDirInvalidCCs);
  365. }
  366. /** Return 1 if <b>addr</b>:<b>port</b> should be marked as a bad dir,
  367. * based on <b>authdir_baddir_policy</b>. Else return 0.
  368. */
  369. int
  370. authdir_policy_baddir_address(uint32_t addr, uint16_t port)
  371. {
  372. if (! addr_policy_permits_address(addr, port, authdir_baddir_policy))
  373. return 1;
  374. return addr_is_in_cc_list(addr, get_options()->AuthDirBadDirCCs);
  375. }
  376. /** Return 1 if <b>addr</b>:<b>port</b> should be marked as a bad exit,
  377. * based on <b>authdir_badexit_policy</b>. Else return 0.
  378. */
  379. int
  380. authdir_policy_badexit_address(uint32_t addr, uint16_t port)
  381. {
  382. if (! addr_policy_permits_address(addr, port, authdir_badexit_policy))
  383. return 1;
  384. return addr_is_in_cc_list(addr, get_options()->AuthDirBadExitCCs);
  385. }
  386. #define REJECT(arg) \
  387. STMT_BEGIN *msg = tor_strdup(arg); goto err; STMT_END
  388. /** Config helper: If there's any problem with the policy configuration
  389. * options in <b>options</b>, return -1 and set <b>msg</b> to a newly
  390. * allocated description of the error. Else return 0. */
  391. int
  392. validate_addr_policies(const or_options_t *options, char **msg)
  393. {
  394. /* XXXX Maybe merge this into parse_policies_from_options, to make sure
  395. * that the two can't go out of sync. */
  396. smartlist_t *addr_policy=NULL;
  397. *msg = NULL;
  398. if (policies_parse_exit_policy(options->ExitPolicy, &addr_policy,
  399. options->IPv6Exit,
  400. options->ExitPolicyRejectPrivate, NULL,
  401. !options->BridgeRelay))
  402. REJECT("Error in ExitPolicy entry.");
  403. /* The rest of these calls *append* to addr_policy. So don't actually
  404. * use the results for anything other than checking if they parse! */
  405. if (parse_addr_policy(options->DirPolicy, &addr_policy, -1))
  406. REJECT("Error in DirPolicy entry.");
  407. if (parse_addr_policy(options->SocksPolicy, &addr_policy, -1))
  408. REJECT("Error in SocksPolicy entry.");
  409. if (parse_addr_policy(options->AuthDirReject, &addr_policy,
  410. ADDR_POLICY_REJECT))
  411. REJECT("Error in AuthDirReject entry.");
  412. if (parse_addr_policy(options->AuthDirInvalid, &addr_policy,
  413. ADDR_POLICY_REJECT))
  414. REJECT("Error in AuthDirInvalid entry.");
  415. if (parse_addr_policy(options->AuthDirBadDir, &addr_policy,
  416. ADDR_POLICY_REJECT))
  417. REJECT("Error in AuthDirBadDir entry.");
  418. if (parse_addr_policy(options->AuthDirBadExit, &addr_policy,
  419. ADDR_POLICY_REJECT))
  420. REJECT("Error in AuthDirBadExit entry.");
  421. if (parse_addr_policy(options->ReachableAddresses, &addr_policy,
  422. ADDR_POLICY_ACCEPT))
  423. REJECT("Error in ReachableAddresses entry.");
  424. if (parse_addr_policy(options->ReachableORAddresses, &addr_policy,
  425. ADDR_POLICY_ACCEPT))
  426. REJECT("Error in ReachableORAddresses entry.");
  427. if (parse_addr_policy(options->ReachableDirAddresses, &addr_policy,
  428. ADDR_POLICY_ACCEPT))
  429. REJECT("Error in ReachableDirAddresses entry.");
  430. err:
  431. addr_policy_list_free(addr_policy);
  432. return *msg ? -1 : 0;
  433. #undef REJECT
  434. }
  435. /** Parse <b>string</b> in the same way that the exit policy
  436. * is parsed, and put the processed version in *<b>policy</b>.
  437. * Ignore port specifiers.
  438. */
  439. static int
  440. load_policy_from_option(config_line_t *config, smartlist_t **policy,
  441. int assume_action)
  442. {
  443. int r;
  444. addr_policy_list_free(*policy);
  445. *policy = NULL;
  446. r = parse_addr_policy(config, policy, assume_action);
  447. if (r < 0) {
  448. return -1;
  449. }
  450. if (*policy) {
  451. SMARTLIST_FOREACH_BEGIN(*policy, addr_policy_t *, n) {
  452. /* ports aren't used in these. */
  453. if (n->prt_min > 1 || n->prt_max != 65535) {
  454. addr_policy_t newp, *c;
  455. memcpy(&newp, n, sizeof(newp));
  456. newp.prt_min = 1;
  457. newp.prt_max = 65535;
  458. newp.is_canonical = 0;
  459. c = addr_policy_get_canonical_entry(&newp);
  460. SMARTLIST_REPLACE_CURRENT(*policy, n, c);
  461. addr_policy_free(n);
  462. }
  463. } SMARTLIST_FOREACH_END(n);
  464. }
  465. return 0;
  466. }
  467. /** Set all policies based on <b>options</b>, which should have been validated
  468. * first by validate_addr_policies. */
  469. int
  470. policies_parse_from_options(const or_options_t *options)
  471. {
  472. int ret = 0;
  473. if (load_policy_from_option(options->SocksPolicy, &socks_policy, -1) < 0)
  474. ret = -1;
  475. if (load_policy_from_option(options->DirPolicy, &dir_policy, -1) < 0)
  476. ret = -1;
  477. if (load_policy_from_option(options->AuthDirReject,
  478. &authdir_reject_policy, ADDR_POLICY_REJECT) < 0)
  479. ret = -1;
  480. if (load_policy_from_option(options->AuthDirInvalid,
  481. &authdir_invalid_policy, ADDR_POLICY_REJECT) < 0)
  482. ret = -1;
  483. if (load_policy_from_option(options->AuthDirBadDir,
  484. &authdir_baddir_policy, ADDR_POLICY_REJECT) < 0)
  485. ret = -1;
  486. if (load_policy_from_option(options->AuthDirBadExit,
  487. &authdir_badexit_policy, ADDR_POLICY_REJECT) < 0)
  488. ret = -1;
  489. if (parse_reachable_addresses() < 0)
  490. ret = -1;
  491. return ret;
  492. }
  493. /** Compare two provided address policy items, and return -1, 0, or 1
  494. * if the first is less than, equal to, or greater than the second. */
  495. static int
  496. cmp_single_addr_policy(addr_policy_t *a, addr_policy_t *b)
  497. {
  498. int r;
  499. if ((r=((int)a->policy_type - (int)b->policy_type)))
  500. return r;
  501. if ((r=((int)a->is_private - (int)b->is_private)))
  502. return r;
  503. if ((r=tor_addr_compare(&a->addr, &b->addr, CMP_EXACT)))
  504. return r;
  505. if ((r=((int)a->maskbits - (int)b->maskbits)))
  506. return r;
  507. if ((r=((int)a->prt_min - (int)b->prt_min)))
  508. return r;
  509. if ((r=((int)a->prt_max - (int)b->prt_max)))
  510. return r;
  511. return 0;
  512. }
  513. /** Like cmp_single_addr_policy() above, but looks at the
  514. * whole set of policies in each case. */
  515. int
  516. cmp_addr_policies(smartlist_t *a, smartlist_t *b)
  517. {
  518. int r, i;
  519. int len_a = a ? smartlist_len(a) : 0;
  520. int len_b = b ? smartlist_len(b) : 0;
  521. for (i = 0; i < len_a && i < len_b; ++i) {
  522. if ((r = cmp_single_addr_policy(smartlist_get(a, i), smartlist_get(b, i))))
  523. return r;
  524. }
  525. if (i == len_a && i == len_b)
  526. return 0;
  527. if (i < len_a)
  528. return -1;
  529. else
  530. return 1;
  531. }
  532. /** Node in hashtable used to store address policy entries. */
  533. typedef struct policy_map_ent_t {
  534. HT_ENTRY(policy_map_ent_t) node;
  535. addr_policy_t *policy;
  536. } policy_map_ent_t;
  537. /* DOCDOC policy_root */
  538. static HT_HEAD(policy_map, policy_map_ent_t) policy_root = HT_INITIALIZER();
  539. /** Return true iff a and b are equal. */
  540. static INLINE int
  541. policy_eq(policy_map_ent_t *a, policy_map_ent_t *b)
  542. {
  543. return cmp_single_addr_policy(a->policy, b->policy) == 0;
  544. }
  545. /** Return a hashcode for <b>ent</b> */
  546. static unsigned int
  547. policy_hash(policy_map_ent_t *ent)
  548. {
  549. addr_policy_t *a = ent->policy;
  550. unsigned int r;
  551. if (a->is_private)
  552. r = 0x1234abcd;
  553. else
  554. r = tor_addr_hash(&a->addr);
  555. r += a->prt_min << 8;
  556. r += a->prt_max << 16;
  557. r += a->maskbits;
  558. if (a->policy_type == ADDR_POLICY_REJECT)
  559. r ^= 0xffffffff;
  560. return r;
  561. }
  562. HT_PROTOTYPE(policy_map, policy_map_ent_t, node, policy_hash,
  563. policy_eq)
  564. HT_GENERATE(policy_map, policy_map_ent_t, node, policy_hash,
  565. policy_eq, 0.6, malloc, realloc, free)
  566. /** Given a pointer to an addr_policy_t, return a copy of the pointer to the
  567. * "canonical" copy of that addr_policy_t; the canonical copy is a single
  568. * reference-counted object. */
  569. addr_policy_t *
  570. addr_policy_get_canonical_entry(addr_policy_t *e)
  571. {
  572. policy_map_ent_t search, *found;
  573. if (e->is_canonical)
  574. return e;
  575. search.policy = e;
  576. found = HT_FIND(policy_map, &policy_root, &search);
  577. if (!found) {
  578. found = tor_malloc_zero(sizeof(policy_map_ent_t));
  579. found->policy = tor_memdup(e, sizeof(addr_policy_t));
  580. found->policy->is_canonical = 1;
  581. found->policy->refcnt = 0;
  582. HT_INSERT(policy_map, &policy_root, found);
  583. }
  584. tor_assert(!cmp_single_addr_policy(found->policy, e));
  585. ++found->policy->refcnt;
  586. return found->policy;
  587. }
  588. /** Helper for compare_tor_addr_to_addr_policy. Implements the case where
  589. * addr and port are both known. */
  590. static addr_policy_result_t
  591. compare_known_tor_addr_to_addr_policy(const tor_addr_t *addr, uint16_t port,
  592. const smartlist_t *policy)
  593. {
  594. /* We know the address and port, and we know the policy, so we can just
  595. * compute an exact match. */
  596. SMARTLIST_FOREACH_BEGIN(policy, addr_policy_t *, tmpe) {
  597. /* Address is known */
  598. if (!tor_addr_compare_masked(addr, &tmpe->addr, tmpe->maskbits,
  599. CMP_EXACT)) {
  600. if (port >= tmpe->prt_min && port <= tmpe->prt_max) {
  601. /* Exact match for the policy */
  602. return tmpe->policy_type == ADDR_POLICY_ACCEPT ?
  603. ADDR_POLICY_ACCEPTED : ADDR_POLICY_REJECTED;
  604. }
  605. }
  606. } SMARTLIST_FOREACH_END(tmpe);
  607. /* accept all by default. */
  608. return ADDR_POLICY_ACCEPTED;
  609. }
  610. /** Helper for compare_tor_addr_to_addr_policy. Implements the case where
  611. * addr is known but port is not. */
  612. static addr_policy_result_t
  613. compare_known_tor_addr_to_addr_policy_noport(const tor_addr_t *addr,
  614. const smartlist_t *policy)
  615. {
  616. /* We look to see if there's a definite match. If so, we return that
  617. match's value, unless there's an intervening possible match that says
  618. something different. */
  619. int maybe_accept = 0, maybe_reject = 0;
  620. SMARTLIST_FOREACH_BEGIN(policy, addr_policy_t *, tmpe) {
  621. if (!tor_addr_compare_masked(addr, &tmpe->addr, tmpe->maskbits,
  622. CMP_EXACT)) {
  623. if (tmpe->prt_min <= 1 && tmpe->prt_max >= 65535) {
  624. /* Definitely matches, since it covers all ports. */
  625. if (tmpe->policy_type == ADDR_POLICY_ACCEPT) {
  626. /* If we already hit a clause that might trigger a 'reject', than we
  627. * can't be sure of this certain 'accept'.*/
  628. return maybe_reject ? ADDR_POLICY_PROBABLY_ACCEPTED :
  629. ADDR_POLICY_ACCEPTED;
  630. } else {
  631. return maybe_accept ? ADDR_POLICY_PROBABLY_REJECTED :
  632. ADDR_POLICY_REJECTED;
  633. }
  634. } else {
  635. /* Might match. */
  636. if (tmpe->policy_type == ADDR_POLICY_REJECT)
  637. maybe_reject = 1;
  638. else
  639. maybe_accept = 1;
  640. }
  641. }
  642. } SMARTLIST_FOREACH_END(tmpe);
  643. /* accept all by default. */
  644. return maybe_reject ? ADDR_POLICY_PROBABLY_ACCEPTED : ADDR_POLICY_ACCEPTED;
  645. }
  646. /** Helper for compare_tor_addr_to_addr_policy. Implements the case where
  647. * port is known but address is not. */
  648. static addr_policy_result_t
  649. compare_unknown_tor_addr_to_addr_policy(uint16_t port,
  650. const smartlist_t *policy)
  651. {
  652. /* We look to see if there's a definite match. If so, we return that
  653. match's value, unless there's an intervening possible match that says
  654. something different. */
  655. int maybe_accept = 0, maybe_reject = 0;
  656. SMARTLIST_FOREACH_BEGIN(policy, addr_policy_t *, tmpe) {
  657. if (tmpe->prt_min <= port && port <= tmpe->prt_max) {
  658. if (tmpe->maskbits == 0) {
  659. /* Definitely matches, since it covers all addresses. */
  660. if (tmpe->policy_type == ADDR_POLICY_ACCEPT) {
  661. /* If we already hit a clause that might trigger a 'reject', than we
  662. * can't be sure of this certain 'accept'.*/
  663. return maybe_reject ? ADDR_POLICY_PROBABLY_ACCEPTED :
  664. ADDR_POLICY_ACCEPTED;
  665. } else {
  666. return maybe_accept ? ADDR_POLICY_PROBABLY_REJECTED :
  667. ADDR_POLICY_REJECTED;
  668. }
  669. } else {
  670. /* Might match. */
  671. if (tmpe->policy_type == ADDR_POLICY_REJECT)
  672. maybe_reject = 1;
  673. else
  674. maybe_accept = 1;
  675. }
  676. }
  677. } SMARTLIST_FOREACH_END(tmpe);
  678. /* accept all by default. */
  679. return maybe_reject ? ADDR_POLICY_PROBABLY_ACCEPTED : ADDR_POLICY_ACCEPTED;
  680. }
  681. /** Decide whether a given addr:port is definitely accepted,
  682. * definitely rejected, probably accepted, or probably rejected by a
  683. * given policy. If <b>addr</b> is 0, we don't know the IP of the
  684. * target address. If <b>port</b> is 0, we don't know the port of the
  685. * target address. (At least one of <b>addr</b> and <b>port</b> must be
  686. * provided. If you want to know whether a policy would definitely reject
  687. * an unknown address:port, use policy_is_reject_star().)
  688. *
  689. * We could do better by assuming that some ranges never match typical
  690. * addresses (127.0.0.1, and so on). But we'll try this for now.
  691. */
  692. addr_policy_result_t
  693. compare_tor_addr_to_addr_policy(const tor_addr_t *addr, uint16_t port,
  694. const smartlist_t *policy)
  695. {
  696. if (!policy) {
  697. /* no policy? accept all. */
  698. return ADDR_POLICY_ACCEPTED;
  699. } else if (addr == NULL || tor_addr_is_null(addr)) {
  700. if (port == 0) {
  701. log_info(LD_BUG, "Rejecting null address with 0 port (family %d)",
  702. addr ? tor_addr_family(addr) : -1);
  703. return ADDR_POLICY_REJECTED;
  704. }
  705. return compare_unknown_tor_addr_to_addr_policy(port, policy);
  706. } else if (port == 0) {
  707. return compare_known_tor_addr_to_addr_policy_noport(addr, policy);
  708. } else {
  709. return compare_known_tor_addr_to_addr_policy(addr, port, policy);
  710. }
  711. }
  712. /** Return true iff the address policy <b>a</b> covers every case that
  713. * would be covered by <b>b</b>, so that a,b is redundant. */
  714. static int
  715. addr_policy_covers(addr_policy_t *a, addr_policy_t *b)
  716. {
  717. if (tor_addr_family(&a->addr) != tor_addr_family(&b->addr)) {
  718. /* You can't cover a different family. */
  719. return 0;
  720. }
  721. /* We can ignore accept/reject, since "accept *:80, reject *:80" reduces
  722. * to "accept *:80". */
  723. if (a->maskbits > b->maskbits) {
  724. /* a has more fixed bits than b; it can't possibly cover b. */
  725. return 0;
  726. }
  727. if (tor_addr_compare_masked(&a->addr, &b->addr, a->maskbits, CMP_EXACT)) {
  728. /* There's a fixed bit in a that's set differently in b. */
  729. return 0;
  730. }
  731. return (a->prt_min <= b->prt_min && a->prt_max >= b->prt_max);
  732. }
  733. /** Return true iff the address policies <b>a</b> and <b>b</b> intersect,
  734. * that is, there exists an address/port that is covered by <b>a</b> that
  735. * is also covered by <b>b</b>.
  736. */
  737. static int
  738. addr_policy_intersects(addr_policy_t *a, addr_policy_t *b)
  739. {
  740. maskbits_t minbits;
  741. /* All the bits we care about are those that are set in both
  742. * netmasks. If they are equal in a and b's networkaddresses
  743. * then the networks intersect. If there is a difference,
  744. * then they do not. */
  745. if (a->maskbits < b->maskbits)
  746. minbits = a->maskbits;
  747. else
  748. minbits = b->maskbits;
  749. if (tor_addr_compare_masked(&a->addr, &b->addr, minbits, CMP_EXACT))
  750. return 0;
  751. if (a->prt_max < b->prt_min || b->prt_max < a->prt_min)
  752. return 0;
  753. return 1;
  754. }
  755. /** Add the exit policy described by <b>more</b> to <b>policy</b>.
  756. */
  757. static void
  758. append_exit_policy_string(smartlist_t **policy, const char *more)
  759. {
  760. config_line_t tmp;
  761. tmp.key = NULL;
  762. tmp.value = (char*) more;
  763. tmp.next = NULL;
  764. if (parse_addr_policy(&tmp, policy, -1)<0) {
  765. log_warn(LD_BUG, "Unable to parse internally generated policy %s",more);
  766. }
  767. }
  768. /** Add "reject <b>addr</b>:*" to <b>dest</b>, creating the list as needed. */
  769. void
  770. addr_policy_append_reject_addr(smartlist_t **dest, const tor_addr_t *addr)
  771. {
  772. addr_policy_t p, *add;
  773. memset(&p, 0, sizeof(p));
  774. p.policy_type = ADDR_POLICY_REJECT;
  775. p.maskbits = tor_addr_family(addr) == AF_INET6 ? 128 : 32;
  776. tor_addr_copy(&p.addr, addr);
  777. p.prt_min = 1;
  778. p.prt_max = 65535;
  779. add = addr_policy_get_canonical_entry(&p);
  780. if (!*dest)
  781. *dest = smartlist_new();
  782. smartlist_add(*dest, add);
  783. }
  784. /** Detect and excise "dead code" from the policy *<b>dest</b>. */
  785. static void
  786. exit_policy_remove_redundancies(smartlist_t *dest)
  787. {
  788. addr_policy_t *ap, *tmp;
  789. int i, j;
  790. /* Step one: kill every ipv4 thing after *4:*, every IPv6 thing after *6:*
  791. */
  792. {
  793. int kill_v4=0, kill_v6=0;
  794. for (i = 0; i < smartlist_len(dest); ++i) {
  795. sa_family_t family;
  796. ap = smartlist_get(dest, i);
  797. family = tor_addr_family(&ap->addr);
  798. if ((family == AF_INET && kill_v4) ||
  799. (family == AF_INET6 && kill_v6)) {
  800. smartlist_del_keeporder(dest, i--);
  801. addr_policy_free(ap);
  802. continue;
  803. }
  804. if (ap->maskbits == 0 && ap->prt_min <= 1 && ap->prt_max >= 65535) {
  805. /* This is a catch-all line -- later lines are unreachable. */
  806. if (family == AF_INET) {
  807. kill_v4 = 1;
  808. } else if (family == AF_INET6) {
  809. kill_v6 = 1;
  810. }
  811. }
  812. }
  813. }
  814. /* Step two: for every entry, see if there's a redundant entry
  815. * later on, and remove it. */
  816. for (i = 0; i < smartlist_len(dest)-1; ++i) {
  817. ap = smartlist_get(dest, i);
  818. for (j = i+1; j < smartlist_len(dest); ++j) {
  819. tmp = smartlist_get(dest, j);
  820. tor_assert(j > i);
  821. if (addr_policy_covers(ap, tmp)) {
  822. char p1[POLICY_BUF_LEN], p2[POLICY_BUF_LEN];
  823. policy_write_item(p1, sizeof(p1), tmp, 0);
  824. policy_write_item(p2, sizeof(p2), ap, 0);
  825. log_debug(LD_CONFIG, "Removing exit policy %s (%d). It is made "
  826. "redundant by %s (%d).", p1, j, p2, i);
  827. smartlist_del_keeporder(dest, j--);
  828. addr_policy_free(tmp);
  829. }
  830. }
  831. }
  832. /* Step three: for every entry A, see if there's an entry B making this one
  833. * redundant later on. This is the case if A and B are of the same type
  834. * (accept/reject), A is a subset of B, and there is no other entry of
  835. * different type in between those two that intersects with A.
  836. *
  837. * Anybody want to double-check the logic here? XXX
  838. */
  839. for (i = 0; i < smartlist_len(dest)-1; ++i) {
  840. ap = smartlist_get(dest, i);
  841. for (j = i+1; j < smartlist_len(dest); ++j) {
  842. // tor_assert(j > i); // j starts out at i+1; j only increases; i only
  843. // // decreases.
  844. tmp = smartlist_get(dest, j);
  845. if (ap->policy_type != tmp->policy_type) {
  846. if (addr_policy_intersects(ap, tmp))
  847. break;
  848. } else { /* policy_types are equal. */
  849. if (addr_policy_covers(tmp, ap)) {
  850. char p1[POLICY_BUF_LEN], p2[POLICY_BUF_LEN];
  851. policy_write_item(p1, sizeof(p1), ap, 0);
  852. policy_write_item(p2, sizeof(p2), tmp, 0);
  853. log_debug(LD_CONFIG, "Removing exit policy %s. It is already "
  854. "covered by %s.", p1, p2);
  855. smartlist_del_keeporder(dest, i--);
  856. addr_policy_free(ap);
  857. break;
  858. }
  859. }
  860. }
  861. }
  862. }
  863. #define DEFAULT_EXIT_POLICY \
  864. "reject *:25,reject *:119,reject *:135-139,reject *:445," \
  865. "reject *:563,reject *:1214,reject *:4661-4666," \
  866. "reject *:6346-6429,reject *:6699,reject *:6881-6999,accept *:*"
  867. /** Parse the exit policy <b>cfg</b> into the linked list *<b>dest</b>. If
  868. * cfg doesn't end in an absolute accept or reject and if
  869. * <b>add_default_policy</b> is true, add the default exit
  870. * policy afterwards. If <b>rejectprivate</b> is true, prepend
  871. * "reject private:*" to the policy. Return -1 if we can't parse cfg,
  872. * else return 0.
  873. *
  874. * This function is used to parse the exit policy from our torrc. For
  875. * the functions used to parse the exit policy from a router descriptor,
  876. * see router_add_exit_policy.
  877. */
  878. int
  879. policies_parse_exit_policy(config_line_t *cfg, smartlist_t **dest,
  880. int ipv6_exit,
  881. int rejectprivate, const char *local_address,
  882. int add_default_policy)
  883. {
  884. if (!ipv6_exit) {
  885. append_exit_policy_string(dest, "reject *6:*");
  886. }
  887. if (rejectprivate) {
  888. append_exit_policy_string(dest, "reject private:*");
  889. if (local_address) {
  890. char buf[POLICY_BUF_LEN];
  891. tor_snprintf(buf, sizeof(buf), "reject %s:*", local_address);
  892. append_exit_policy_string(dest, buf);
  893. }
  894. }
  895. if (parse_addr_policy(cfg, dest, -1))
  896. return -1;
  897. if (add_default_policy) {
  898. append_exit_policy_string(dest, DEFAULT_EXIT_POLICY);
  899. } else {
  900. append_exit_policy_string(dest, "reject *4:*");
  901. append_exit_policy_string(dest, "reject *6:*");
  902. }
  903. exit_policy_remove_redundancies(*dest);
  904. return 0;
  905. }
  906. /** Add "reject *:*" to the end of the policy in *<b>dest</b>, allocating
  907. * *<b>dest</b> as needed. */
  908. void
  909. policies_exit_policy_append_reject_star(smartlist_t **dest)
  910. {
  911. append_exit_policy_string(dest, "reject *4:*");
  912. append_exit_policy_string(dest, "reject *6:*");
  913. }
  914. /** Replace the exit policy of <b>node</b> with reject *:* */
  915. void
  916. policies_set_node_exitpolicy_to_reject_all(node_t *node)
  917. {
  918. node->rejects_all = 1;
  919. }
  920. /** Return 1 if there is at least one /8 subnet in <b>policy</b> that
  921. * allows exiting to <b>port</b>. Otherwise, return 0. */
  922. static int
  923. exit_policy_is_general_exit_helper(smartlist_t *policy, int port)
  924. {
  925. uint32_t mask, ip, i;
  926. /* Is this /8 rejected (1), or undecided (0)? */
  927. char subnet_status[256];
  928. memset(subnet_status, 0, sizeof(subnet_status));
  929. SMARTLIST_FOREACH_BEGIN(policy, addr_policy_t *, p) {
  930. if (tor_addr_family(&p->addr) != AF_INET)
  931. continue; /* IPv4 only for now */
  932. if (p->prt_min > port || p->prt_max < port)
  933. continue; /* Doesn't cover our port. */
  934. mask = 0;
  935. tor_assert(p->maskbits <= 32);
  936. if (p->maskbits)
  937. mask = UINT32_MAX<<(32-p->maskbits);
  938. ip = tor_addr_to_ipv4h(&p->addr);
  939. /* Calculate the first and last subnet that this exit policy touches
  940. * and set it as loop boundaries. */
  941. for (i = ((mask & ip)>>24); i <= (~((mask & ip) ^ mask)>>24); ++i) {
  942. tor_addr_t addr;
  943. if (subnet_status[i] != 0)
  944. continue; /* We already reject some part of this /8 */
  945. tor_addr_from_ipv4h(&addr, i<<24);
  946. if (tor_addr_is_internal(&addr, 0))
  947. continue; /* Local or non-routable addresses */
  948. if (p->policy_type == ADDR_POLICY_ACCEPT) {
  949. if (p->maskbits > 8)
  950. continue; /* Narrower than a /8. */
  951. /* We found an allowed subnet of at least size /8. Done
  952. * for this port! */
  953. return 1;
  954. } else if (p->policy_type == ADDR_POLICY_REJECT) {
  955. subnet_status[i] = 1;
  956. }
  957. }
  958. } SMARTLIST_FOREACH_END(p);
  959. return 0;
  960. }
  961. /** Return true iff <b>ri</b> is "useful as an exit node", meaning
  962. * it allows exit to at least one /8 address space for at least
  963. * two of ports 80, 443, and 6667. */
  964. int
  965. exit_policy_is_general_exit(smartlist_t *policy)
  966. {
  967. static const int ports[] = { 80, 443, 6667 };
  968. int n_allowed = 0;
  969. int i;
  970. if (!policy) /*XXXX disallow NULL policies? */
  971. return 0;
  972. for (i = 0; i < 3; ++i) {
  973. n_allowed += exit_policy_is_general_exit_helper(policy, ports[i]);
  974. }
  975. return n_allowed >= 2;
  976. }
  977. /** Return false if <b>policy</b> might permit access to some addr:port;
  978. * otherwise if we are certain it rejects everything, return true. */
  979. int
  980. policy_is_reject_star(const smartlist_t *policy, sa_family_t family)
  981. {
  982. if (!policy) /*XXXX disallow NULL policies? */
  983. return 1;
  984. SMARTLIST_FOREACH_BEGIN(policy, addr_policy_t *, p) {
  985. if (p->policy_type == ADDR_POLICY_ACCEPT &&
  986. (tor_addr_family(&p->addr) == family ||
  987. tor_addr_family(&p->addr) == AF_UNSPEC)) {
  988. return 0;
  989. } else if (p->policy_type == ADDR_POLICY_REJECT &&
  990. p->prt_min <= 1 && p->prt_max == 65535 &&
  991. p->maskbits == 0 &&
  992. (tor_addr_family(&p->addr) == family ||
  993. tor_addr_family(&p->addr) == AF_UNSPEC)) {
  994. return 1;
  995. }
  996. } SMARTLIST_FOREACH_END(p);
  997. return 1;
  998. }
  999. /** Write a single address policy to the buf_len byte buffer at buf. Return
  1000. * the number of characters written, or -1 on failure. */
  1001. int
  1002. policy_write_item(char *buf, size_t buflen, addr_policy_t *policy,
  1003. int format_for_desc)
  1004. {
  1005. size_t written = 0;
  1006. char addrbuf[TOR_ADDR_BUF_LEN];
  1007. const char *addrpart;
  1008. int result;
  1009. const int is_accept = policy->policy_type == ADDR_POLICY_ACCEPT;
  1010. const sa_family_t family = tor_addr_family(&policy->addr);
  1011. const int is_ip6 = (family == AF_INET6);
  1012. tor_addr_to_str(addrbuf, &policy->addr, sizeof(addrbuf), 1);
  1013. /* write accept/reject 1.2.3.4 */
  1014. if (policy->is_private) {
  1015. addrpart = "private";
  1016. } else if (policy->maskbits == 0) {
  1017. if (format_for_desc)
  1018. addrpart = "*";
  1019. else if (family == AF_INET6)
  1020. addrpart = "*6";
  1021. else if (family == AF_INET)
  1022. addrpart = "*4";
  1023. else
  1024. addrpart = "*";
  1025. } else {
  1026. addrpart = addrbuf;
  1027. }
  1028. result = tor_snprintf(buf, buflen, "%s%s %s",
  1029. is_accept ? "accept" : "reject",
  1030. (is_ip6&&format_for_desc)?"6":"",
  1031. addrpart);
  1032. if (result < 0)
  1033. return -1;
  1034. written += strlen(buf);
  1035. /* If the maskbits is 32 we don't need to give it. If the mask is 0,
  1036. * we already wrote "*". */
  1037. if (policy->maskbits < 32 && policy->maskbits > 0) {
  1038. if (tor_snprintf(buf+written, buflen-written, "/%d", policy->maskbits)<0)
  1039. return -1;
  1040. written += strlen(buf+written);
  1041. }
  1042. if (policy->prt_min <= 1 && policy->prt_max == 65535) {
  1043. /* There is no port set; write ":*" */
  1044. if (written+4 > buflen)
  1045. return -1;
  1046. strlcat(buf+written, ":*", buflen-written);
  1047. written += 2;
  1048. } else if (policy->prt_min == policy->prt_max) {
  1049. /* There is only one port; write ":80". */
  1050. result = tor_snprintf(buf+written, buflen-written, ":%d", policy->prt_min);
  1051. if (result<0)
  1052. return -1;
  1053. written += result;
  1054. } else {
  1055. /* There is a range of ports; write ":79-80". */
  1056. result = tor_snprintf(buf+written, buflen-written, ":%d-%d",
  1057. policy->prt_min, policy->prt_max);
  1058. if (result<0)
  1059. return -1;
  1060. written += result;
  1061. }
  1062. if (written < buflen)
  1063. buf[written] = '\0';
  1064. else
  1065. return -1;
  1066. return (int)written;
  1067. }
  1068. /** Create a new exit policy summary, initially only with a single
  1069. * port 1-64k item */
  1070. /* XXXX This entire thing will do most stuff in O(N^2), or worse. Use an
  1071. * RB-tree if that turns out to matter. */
  1072. static smartlist_t *
  1073. policy_summary_create(void)
  1074. {
  1075. smartlist_t *summary;
  1076. policy_summary_item_t* item;
  1077. item = tor_malloc_zero(sizeof(policy_summary_item_t));
  1078. item->prt_min = 1;
  1079. item->prt_max = 65535;
  1080. item->reject_count = 0;
  1081. item->accepted = 0;
  1082. summary = smartlist_new();
  1083. smartlist_add(summary, item);
  1084. return summary;
  1085. }
  1086. /** Split the summary item in <b>item</b> at the port <b>new_starts</b>.
  1087. * The current item is changed to end at new-starts - 1, the new item
  1088. * copies reject_count and accepted from the old item,
  1089. * starts at new_starts and ends at the port where the original item
  1090. * previously ended.
  1091. */
  1092. static policy_summary_item_t*
  1093. policy_summary_item_split(policy_summary_item_t* old, uint16_t new_starts)
  1094. {
  1095. policy_summary_item_t* new;
  1096. new = tor_malloc_zero(sizeof(policy_summary_item_t));
  1097. new->prt_min = new_starts;
  1098. new->prt_max = old->prt_max;
  1099. new->reject_count = old->reject_count;
  1100. new->accepted = old->accepted;
  1101. old->prt_max = new_starts-1;
  1102. tor_assert(old->prt_min <= old->prt_max);
  1103. tor_assert(new->prt_min <= new->prt_max);
  1104. return new;
  1105. }
  1106. /* XXXX Nick says I'm going to hell for this. If he feels charitably towards
  1107. * my immortal soul, he can clean it up himself. */
  1108. #define AT(x) ((policy_summary_item_t*)smartlist_get(summary, x))
  1109. #define REJECT_CUTOFF_COUNT (1<<25)
  1110. /** Split an exit policy summary so that prt_min and prt_max
  1111. * fall at exactly the start and end of an item respectively.
  1112. */
  1113. static int
  1114. policy_summary_split(smartlist_t *summary,
  1115. uint16_t prt_min, uint16_t prt_max)
  1116. {
  1117. int start_at_index;
  1118. int i = 0;
  1119. while (AT(i)->prt_max < prt_min)
  1120. i++;
  1121. if (AT(i)->prt_min != prt_min) {
  1122. policy_summary_item_t* new_item;
  1123. new_item = policy_summary_item_split(AT(i), prt_min);
  1124. smartlist_insert(summary, i+1, new_item);
  1125. i++;
  1126. }
  1127. start_at_index = i;
  1128. while (AT(i)->prt_max < prt_max)
  1129. i++;
  1130. if (AT(i)->prt_max != prt_max) {
  1131. policy_summary_item_t* new_item;
  1132. new_item = policy_summary_item_split(AT(i), prt_max+1);
  1133. smartlist_insert(summary, i+1, new_item);
  1134. }
  1135. return start_at_index;
  1136. }
  1137. /** Mark port ranges as accepted if they are below the reject_count */
  1138. static void
  1139. policy_summary_accept(smartlist_t *summary,
  1140. uint16_t prt_min, uint16_t prt_max)
  1141. {
  1142. int i = policy_summary_split(summary, prt_min, prt_max);
  1143. while (i < smartlist_len(summary) &&
  1144. AT(i)->prt_max <= prt_max) {
  1145. if (!AT(i)->accepted &&
  1146. AT(i)->reject_count <= REJECT_CUTOFF_COUNT)
  1147. AT(i)->accepted = 1;
  1148. i++;
  1149. }
  1150. tor_assert(i < smartlist_len(summary) || prt_max==65535);
  1151. }
  1152. /** Count the number of addresses in a network with prefixlen maskbits
  1153. * against the given portrange. */
  1154. static void
  1155. policy_summary_reject(smartlist_t *summary,
  1156. maskbits_t maskbits,
  1157. uint16_t prt_min, uint16_t prt_max)
  1158. {
  1159. int i = policy_summary_split(summary, prt_min, prt_max);
  1160. /* XXX: ipv4 specific */
  1161. uint64_t count = (U64_LITERAL(1) << (32-maskbits));
  1162. while (i < smartlist_len(summary) &&
  1163. AT(i)->prt_max <= prt_max) {
  1164. AT(i)->reject_count += count;
  1165. i++;
  1166. }
  1167. tor_assert(i < smartlist_len(summary) || prt_max==65535);
  1168. }
  1169. /** Add a single exit policy item to our summary:
  1170. * If it is an accept ignore it unless it is for all IP addresses
  1171. * ("*"), i.e. it's prefixlen/maskbits is 0, else call
  1172. * policy_summary_accept().
  1173. * If it's a reject ignore it if it is about one of the private
  1174. * networks, else call policy_summary_reject().
  1175. */
  1176. static void
  1177. policy_summary_add_item(smartlist_t *summary, addr_policy_t *p)
  1178. {
  1179. if (p->policy_type == ADDR_POLICY_ACCEPT) {
  1180. if (p->maskbits == 0) {
  1181. policy_summary_accept(summary, p->prt_min, p->prt_max);
  1182. }
  1183. } else if (p->policy_type == ADDR_POLICY_REJECT) {
  1184. int is_private = 0;
  1185. int i;
  1186. for (i = 0; private_nets[i]; ++i) {
  1187. tor_addr_t addr;
  1188. maskbits_t maskbits;
  1189. if (tor_addr_parse_mask_ports(private_nets[i], 0, &addr,
  1190. &maskbits, NULL, NULL)<0) {
  1191. tor_assert(0);
  1192. }
  1193. if (tor_addr_compare(&p->addr, &addr, CMP_EXACT) == 0 &&
  1194. p->maskbits == maskbits) {
  1195. is_private = 1;
  1196. break;
  1197. }
  1198. }
  1199. if (!is_private) {
  1200. policy_summary_reject(summary, p->maskbits, p->prt_min, p->prt_max);
  1201. }
  1202. } else
  1203. tor_assert(0);
  1204. }
  1205. /** Create a string representing a summary for an exit policy.
  1206. * The summary will either be an "accept" plus a comma-separated list of port
  1207. * ranges or a "reject" plus port-ranges, depending on which is shorter.
  1208. *
  1209. * If no exits are allowed at all then NULL is returned, if no ports
  1210. * are blocked instead of "reject " we return "accept 1-65535" (this
  1211. * is an exception to the shorter-representation-wins rule).
  1212. */
  1213. char *
  1214. policy_summarize(smartlist_t *policy, sa_family_t family)
  1215. {
  1216. smartlist_t *summary = policy_summary_create();
  1217. smartlist_t *accepts, *rejects;
  1218. int i, last, start_prt;
  1219. size_t accepts_len, rejects_len;
  1220. char *accepts_str = NULL, *rejects_str = NULL, *shorter_str, *result;
  1221. const char *prefix;
  1222. tor_assert(policy);
  1223. /* Create the summary list */
  1224. SMARTLIST_FOREACH_BEGIN(policy, addr_policy_t *, p) {
  1225. sa_family_t f = tor_addr_family(&p->addr);
  1226. if (f != AF_INET && f != AF_INET6) {
  1227. log_warn(LD_BUG, "Weird family when summarizing address policy");
  1228. }
  1229. if (f != family)
  1230. continue;
  1231. /* XXXX-ipv6 More family work is needed */
  1232. policy_summary_add_item(summary, p);
  1233. } SMARTLIST_FOREACH_END(p);
  1234. /* Now create two lists of strings, one for accepted and one
  1235. * for rejected ports. We take care to merge ranges so that
  1236. * we avoid getting stuff like "1-4,5-9,10", instead we want
  1237. * "1-10"
  1238. */
  1239. i = 0;
  1240. start_prt = 1;
  1241. accepts = smartlist_new();
  1242. rejects = smartlist_new();
  1243. while (1) {
  1244. last = i == smartlist_len(summary)-1;
  1245. if (last ||
  1246. AT(i)->accepted != AT(i+1)->accepted) {
  1247. char buf[POLICY_BUF_LEN];
  1248. if (start_prt == AT(i)->prt_max)
  1249. tor_snprintf(buf, sizeof(buf), "%d", start_prt);
  1250. else
  1251. tor_snprintf(buf, sizeof(buf), "%d-%d", start_prt, AT(i)->prt_max);
  1252. if (AT(i)->accepted)
  1253. smartlist_add(accepts, tor_strdup(buf));
  1254. else
  1255. smartlist_add(rejects, tor_strdup(buf));
  1256. if (last)
  1257. break;
  1258. start_prt = AT(i+1)->prt_min;
  1259. };
  1260. i++;
  1261. };
  1262. /* Figure out which of the two stringlists will be shorter and use
  1263. * that to build the result
  1264. */
  1265. if (smartlist_len(accepts) == 0) { /* no exits at all */
  1266. result = tor_strdup("reject 1-65535");
  1267. goto cleanup;
  1268. }
  1269. if (smartlist_len(rejects) == 0) { /* no rejects at all */
  1270. result = tor_strdup("accept 1-65535");
  1271. goto cleanup;
  1272. }
  1273. accepts_str = smartlist_join_strings(accepts, ",", 0, &accepts_len);
  1274. rejects_str = smartlist_join_strings(rejects, ",", 0, &rejects_len);
  1275. if (rejects_len > MAX_EXITPOLICY_SUMMARY_LEN-strlen("reject")-1 &&
  1276. accepts_len > MAX_EXITPOLICY_SUMMARY_LEN-strlen("accept")-1) {
  1277. char *c;
  1278. shorter_str = accepts_str;
  1279. prefix = "accept";
  1280. c = shorter_str + (MAX_EXITPOLICY_SUMMARY_LEN-strlen(prefix)-1);
  1281. while (*c != ',' && c >= shorter_str)
  1282. c--;
  1283. tor_assert(c >= shorter_str);
  1284. tor_assert(*c == ',');
  1285. *c = '\0';
  1286. } else if (rejects_len < accepts_len) {
  1287. shorter_str = rejects_str;
  1288. prefix = "reject";
  1289. } else {
  1290. shorter_str = accepts_str;
  1291. prefix = "accept";
  1292. }
  1293. tor_asprintf(&result, "%s %s", prefix, shorter_str);
  1294. cleanup:
  1295. /* cleanup */
  1296. SMARTLIST_FOREACH(summary, policy_summary_item_t *, s, tor_free(s));
  1297. smartlist_free(summary);
  1298. tor_free(accepts_str);
  1299. SMARTLIST_FOREACH(accepts, char *, s, tor_free(s));
  1300. smartlist_free(accepts);
  1301. tor_free(rejects_str);
  1302. SMARTLIST_FOREACH(rejects, char *, s, tor_free(s));
  1303. smartlist_free(rejects);
  1304. return result;
  1305. }
  1306. /** Convert a summarized policy string into a short_policy_t. Return NULL
  1307. * if the string is not well-formed. */
  1308. short_policy_t *
  1309. parse_short_policy(const char *summary)
  1310. {
  1311. const char *orig_summary = summary;
  1312. short_policy_t *result;
  1313. int is_accept;
  1314. int n_entries;
  1315. short_policy_entry_t entries[MAX_EXITPOLICY_SUMMARY_LEN]; /* overkill */
  1316. const char *next;
  1317. if (!strcmpstart(summary, "accept ")) {
  1318. is_accept = 1;
  1319. summary += strlen("accept ");
  1320. } else if (!strcmpstart(summary, "reject ")) {
  1321. is_accept = 0;
  1322. summary += strlen("reject ");
  1323. } else {
  1324. log_fn(LOG_PROTOCOL_WARN, LD_DIR, "Unrecognized policy summary keyword");
  1325. return NULL;
  1326. }
  1327. n_entries = 0;
  1328. for ( ; *summary; summary = next) {
  1329. const char *comma = strchr(summary, ',');
  1330. unsigned low, high;
  1331. char dummy;
  1332. char ent_buf[32];
  1333. size_t len;
  1334. next = comma ? comma+1 : strchr(summary, '\0');
  1335. len = comma ? (size_t)(comma - summary) : strlen(summary);
  1336. if (n_entries == MAX_EXITPOLICY_SUMMARY_LEN) {
  1337. log_fn(LOG_PROTOCOL_WARN, LD_DIR, "Impossibly long policy summary %s",
  1338. escaped(orig_summary));
  1339. return NULL;
  1340. }
  1341. if (! TOR_ISDIGIT(*summary) || len > (sizeof(ent_buf)-1)) {
  1342. /* unrecognized entry format. skip it. */
  1343. continue;
  1344. }
  1345. if (len < 1) {
  1346. /* empty; skip it. */
  1347. /* XXX This happens to be unreachable, since if len==0, then *summary is
  1348. * ',' or '\0', and the TOR_ISDIGIT test above would have failed. */
  1349. continue;
  1350. }
  1351. memcpy(ent_buf, summary, len);
  1352. ent_buf[len] = '\0';
  1353. if (tor_sscanf(ent_buf, "%u-%u%c", &low, &high, &dummy) == 2) {
  1354. if (low<1 || low>65535 || high<1 || high>65535 || low>high) {
  1355. log_fn(LOG_PROTOCOL_WARN, LD_DIR,
  1356. "Found bad entry in policy summary %s", escaped(orig_summary));
  1357. return NULL;
  1358. }
  1359. } else if (tor_sscanf(ent_buf, "%u%c", &low, &dummy) == 1) {
  1360. if (low<1 || low>65535) {
  1361. log_fn(LOG_PROTOCOL_WARN, LD_DIR,
  1362. "Found bad entry in policy summary %s", escaped(orig_summary));
  1363. return NULL;
  1364. }
  1365. high = low;
  1366. } else {
  1367. log_fn(LOG_PROTOCOL_WARN, LD_DIR,"Found bad entry in policy summary %s",
  1368. escaped(orig_summary));
  1369. return NULL;
  1370. }
  1371. entries[n_entries].min_port = low;
  1372. entries[n_entries].max_port = high;
  1373. n_entries++;
  1374. }
  1375. if (n_entries == 0) {
  1376. log_fn(LOG_PROTOCOL_WARN, LD_DIR,
  1377. "Found no port-range entries in summary %s", escaped(orig_summary));
  1378. return NULL;
  1379. }
  1380. {
  1381. size_t size = STRUCT_OFFSET(short_policy_t, entries) +
  1382. sizeof(short_policy_entry_t)*(n_entries);
  1383. result = tor_malloc_zero(size);
  1384. tor_assert( (char*)&result->entries[n_entries-1] < ((char*)result)+size);
  1385. }
  1386. result->is_accept = is_accept;
  1387. result->n_entries = n_entries;
  1388. memcpy(result->entries, entries, sizeof(short_policy_entry_t)*n_entries);
  1389. return result;
  1390. }
  1391. /** Write <b>policy</b> back out into a string. Used only for unit tests
  1392. * currently. */
  1393. char *
  1394. write_short_policy(const short_policy_t *policy)
  1395. {
  1396. int i;
  1397. char *answer;
  1398. smartlist_t *sl = smartlist_new();
  1399. smartlist_add_asprintf(sl, "%s", policy->is_accept ? "accept " : "reject ");
  1400. for (i=0; i < policy->n_entries; i++) {
  1401. const short_policy_entry_t *e = &policy->entries[i];
  1402. if (e->min_port == e->max_port) {
  1403. smartlist_add_asprintf(sl, "%d", e->min_port);
  1404. } else {
  1405. smartlist_add_asprintf(sl, "%d-%d", e->min_port, e->max_port);
  1406. }
  1407. if (i < policy->n_entries-1)
  1408. smartlist_add(sl, tor_strdup(","));
  1409. }
  1410. answer = smartlist_join_strings(sl, "", 0, NULL);
  1411. SMARTLIST_FOREACH(sl, char *, a, tor_free(a));
  1412. smartlist_free(sl);
  1413. return answer;
  1414. }
  1415. /** Release all storage held in <b>policy</b>. */
  1416. void
  1417. short_policy_free(short_policy_t *policy)
  1418. {
  1419. tor_free(policy);
  1420. }
  1421. /** See whether the <b>addr</b>:<b>port</b> address is likely to be accepted
  1422. * or rejected by the summarized policy <b>policy</b>. Return values are as
  1423. * for compare_tor_addr_to_addr_policy. Unlike the regular addr_policy
  1424. * functions, requires the <b>port</b> be specified. */
  1425. addr_policy_result_t
  1426. compare_tor_addr_to_short_policy(const tor_addr_t *addr, uint16_t port,
  1427. const short_policy_t *policy)
  1428. {
  1429. int i;
  1430. int found_match = 0;
  1431. int accept;
  1432. tor_assert(port != 0);
  1433. if (addr && tor_addr_is_null(addr))
  1434. addr = NULL; /* Unspec means 'no address at all,' in this context. */
  1435. if (addr && get_options()->ClientRejectInternalAddresses &&
  1436. (tor_addr_is_internal(addr, 0) || tor_addr_is_loopback(addr)))
  1437. return ADDR_POLICY_REJECTED;
  1438. for (i=0; i < policy->n_entries; ++i) {
  1439. const short_policy_entry_t *e = &policy->entries[i];
  1440. if (e->min_port <= port && port <= e->max_port) {
  1441. found_match = 1;
  1442. break;
  1443. }
  1444. }
  1445. if (found_match)
  1446. accept = policy->is_accept;
  1447. else
  1448. accept = ! policy->is_accept;
  1449. /* ???? are these right? -NM */
  1450. /* We should be sure not to return ADDR_POLICY_ACCEPTED in the accept
  1451. * case here, because it would cause clients to believe that the node
  1452. * allows exit enclaving. Trying it anyway would open up a cool attack
  1453. * where the node refuses due to exitpolicy, the client reacts in
  1454. * surprise by rewriting the node's exitpolicy to reject *:*, and then
  1455. * a bad guy targets users by causing them to attempt such connections
  1456. * to 98% of the exits.
  1457. *
  1458. * Once microdescriptors can handle addresses in special cases (e.g. if
  1459. * we ever solve ticket 1774), we can provide certainty here. -RD */
  1460. if (accept)
  1461. return ADDR_POLICY_PROBABLY_ACCEPTED;
  1462. else
  1463. return ADDR_POLICY_REJECTED;
  1464. }
  1465. /** Return true iff <b>policy</b> seems reject all ports */
  1466. int
  1467. short_policy_is_reject_star(const short_policy_t *policy)
  1468. {
  1469. /* This doesn't need to be as much on the lookout as policy_is_reject_star,
  1470. * since policy summaries are from the consensus or from consensus
  1471. * microdescs.
  1472. */
  1473. tor_assert(policy);
  1474. /* Check for an exact match of "reject 1-65535". */
  1475. return (policy->is_accept == 0 && policy->n_entries == 1 &&
  1476. policy->entries[0].min_port == 1 &&
  1477. policy->entries[0].max_port == 65535);
  1478. }
  1479. /** Decide whether addr:port is probably or definitely accepted or rejected by
  1480. * <b>node</b>. See compare_tor_addr_to_addr_policy for details on addr/port
  1481. * interpretation. */
  1482. addr_policy_result_t
  1483. compare_tor_addr_to_node_policy(const tor_addr_t *addr, uint16_t port,
  1484. const node_t *node)
  1485. {
  1486. if (node->rejects_all)
  1487. return ADDR_POLICY_REJECTED;
  1488. if (addr && tor_addr_family(addr) == AF_INET6) {
  1489. const short_policy_t *p = NULL;
  1490. if (node->ri)
  1491. p = node->ri->ipv6_exit_policy;
  1492. else if (node->md)
  1493. p = node->md->ipv6_exit_policy;
  1494. if (p)
  1495. return compare_tor_addr_to_short_policy(addr, port, p);
  1496. else
  1497. return ADDR_POLICY_REJECTED;
  1498. }
  1499. if (node->ri) {
  1500. return compare_tor_addr_to_addr_policy(addr, port, node->ri->exit_policy);
  1501. } else if (node->md) {
  1502. if (node->md->exit_policy == NULL)
  1503. return ADDR_POLICY_REJECTED;
  1504. else
  1505. return compare_tor_addr_to_short_policy(addr, port,
  1506. node->md->exit_policy);
  1507. } else {
  1508. return ADDR_POLICY_PROBABLY_REJECTED;
  1509. }
  1510. }
  1511. /** Implementation for GETINFO control command: knows the answer for questions
  1512. * about "exit-policy/..." */
  1513. int
  1514. getinfo_helper_policies(control_connection_t *conn,
  1515. const char *question, char **answer,
  1516. const char **errmsg)
  1517. {
  1518. (void) conn;
  1519. (void) errmsg;
  1520. if (!strcmp(question, "exit-policy/default")) {
  1521. *answer = tor_strdup(DEFAULT_EXIT_POLICY);
  1522. } else if (!strcmpstart(question, "exit-policy/")) {
  1523. const routerinfo_t *me = router_get_my_routerinfo();
  1524. int include_ipv4 = 0;
  1525. int include_ipv6 = 0;
  1526. if (!strcmp(question, "exit-policy/ipv4")) {
  1527. include_ipv4 = 1;
  1528. } else if (!strcmp(question, "exit-policy/ipv6")) {
  1529. include_ipv6 = 1;
  1530. } else if (!strcmp(question, "exit-policy/full")) {
  1531. include_ipv4 = include_ipv6 = 1;
  1532. } else {
  1533. return 0; /* No such key. */
  1534. }
  1535. if (!me) {
  1536. *errmsg = "router_get_my_routerinfo returned NULL";
  1537. return -1;
  1538. }
  1539. *answer = router_dump_exit_policy_to_string(me,include_ipv4,include_ipv6);
  1540. }
  1541. return 0;
  1542. }
  1543. /** Release all storage held by <b>p</b>. */
  1544. void
  1545. addr_policy_list_free(smartlist_t *lst)
  1546. {
  1547. if (!lst)
  1548. return;
  1549. SMARTLIST_FOREACH(lst, addr_policy_t *, policy, addr_policy_free(policy));
  1550. smartlist_free(lst);
  1551. }
  1552. /** Release all storage held by <b>p</b>. */
  1553. void
  1554. addr_policy_free(addr_policy_t *p)
  1555. {
  1556. if (!p)
  1557. return;
  1558. if (--p->refcnt <= 0) {
  1559. if (p->is_canonical) {
  1560. policy_map_ent_t search, *found;
  1561. search.policy = p;
  1562. found = HT_REMOVE(policy_map, &policy_root, &search);
  1563. if (found) {
  1564. tor_assert(p == found->policy);
  1565. tor_free(found);
  1566. }
  1567. }
  1568. tor_free(p);
  1569. }
  1570. }
  1571. /** Release all storage held by policy variables. */
  1572. void
  1573. policies_free_all(void)
  1574. {
  1575. addr_policy_list_free(reachable_or_addr_policy);
  1576. reachable_or_addr_policy = NULL;
  1577. addr_policy_list_free(reachable_dir_addr_policy);
  1578. reachable_dir_addr_policy = NULL;
  1579. addr_policy_list_free(socks_policy);
  1580. socks_policy = NULL;
  1581. addr_policy_list_free(dir_policy);
  1582. dir_policy = NULL;
  1583. addr_policy_list_free(authdir_reject_policy);
  1584. authdir_reject_policy = NULL;
  1585. addr_policy_list_free(authdir_invalid_policy);
  1586. authdir_invalid_policy = NULL;
  1587. addr_policy_list_free(authdir_baddir_policy);
  1588. authdir_baddir_policy = NULL;
  1589. addr_policy_list_free(authdir_badexit_policy);
  1590. authdir_badexit_policy = NULL;
  1591. if (!HT_EMPTY(&policy_root)) {
  1592. policy_map_ent_t **ent;
  1593. int n = 0;
  1594. char buf[POLICY_BUF_LEN];
  1595. log_warn(LD_MM, "Still had %d address policies cached at shutdown.",
  1596. (int)HT_SIZE(&policy_root));
  1597. /* Note the first 10 cached policies to try to figure out where they
  1598. * might be coming from. */
  1599. HT_FOREACH(ent, policy_map, &policy_root) {
  1600. if (++n > 10)
  1601. break;
  1602. if (policy_write_item(buf, sizeof(buf), (*ent)->policy, 0) >= 0)
  1603. log_warn(LD_MM," %d [%d]: %s", n, (*ent)->policy->refcnt, buf);
  1604. }
  1605. }
  1606. HT_CLEAR(policy_map, &policy_root);
  1607. }