policies.c 39 KB

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