router.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. */
  4. /* See LICENSE for licensing information */
  5. /* $Id$ */
  6. const char router_c_id[] =
  7. "$Id$";
  8. #include "or.h"
  9. /**
  10. * \file router.c
  11. * \brief OR functionality, including key maintenance, generating
  12. * and uploading server descriptors, retrying OR connections.
  13. **/
  14. extern long stats_n_seconds_working;
  15. /* Exposed for test.c. */ void get_platform_str(char *platform, size_t len);
  16. /************************************************************/
  17. /*****
  18. * Key management: ORs only.
  19. *****/
  20. /** Private keys for this OR. There is also an SSL key managed by tortls.c.
  21. */
  22. static tor_mutex_t *key_lock=NULL;
  23. static time_t onionkey_set_at=0; /* When was onionkey last changed? */
  24. static crypto_pk_env_t *onionkey=NULL;
  25. static crypto_pk_env_t *lastonionkey=NULL;
  26. static crypto_pk_env_t *identitykey=NULL;
  27. /** Replace the current onion key with <b>k</b>. Does not affect lastonionkey;
  28. * to update onionkey correctly, call rotate_onion_key().
  29. */
  30. void
  31. set_onion_key(crypto_pk_env_t *k)
  32. {
  33. tor_mutex_acquire(key_lock);
  34. onionkey = k;
  35. onionkey_set_at = time(NULL);
  36. tor_mutex_release(key_lock);
  37. mark_my_descriptor_dirty();
  38. }
  39. /** Return the current onion key. Requires that the onion key has been
  40. * loaded or generated. */
  41. crypto_pk_env_t *
  42. get_onion_key(void)
  43. {
  44. tor_assert(onionkey);
  45. return onionkey;
  46. }
  47. /** Store a copy of the current onion key into *<b>key</b>, and a copy
  48. * of the most recent onion key into *<b>last</b>.
  49. */
  50. void
  51. dup_onion_keys(crypto_pk_env_t **key, crypto_pk_env_t **last)
  52. {
  53. tor_assert(key);
  54. tor_assert(last);
  55. tor_mutex_acquire(key_lock);
  56. tor_assert(onionkey);
  57. *key = crypto_pk_dup_key(onionkey);
  58. if (lastonionkey)
  59. *last = crypto_pk_dup_key(lastonionkey);
  60. else
  61. *last = NULL;
  62. tor_mutex_release(key_lock);
  63. }
  64. /** Return the time when the onion key was last set. This is either the time
  65. * when the process launched, or the time of the most recent key rotation since
  66. * the process launched.
  67. */
  68. time_t
  69. get_onion_key_set_at(void)
  70. {
  71. return onionkey_set_at;
  72. }
  73. /** Set the current identity key to k.
  74. */
  75. void
  76. set_identity_key(crypto_pk_env_t *k)
  77. {
  78. if (identitykey)
  79. crypto_free_pk_env(identitykey);
  80. identitykey = k;
  81. }
  82. /** Returns the current identity key; requires that the identity key has been
  83. * set.
  84. */
  85. crypto_pk_env_t *
  86. get_identity_key(void)
  87. {
  88. tor_assert(identitykey);
  89. return identitykey;
  90. }
  91. /** Return true iff the identity key has been set. */
  92. int
  93. identity_key_is_set(void)
  94. {
  95. return identitykey != NULL;
  96. }
  97. /** Replace the previous onion key with the current onion key, and generate
  98. * a new previous onion key. Immediately after calling this function,
  99. * the OR should:
  100. * - schedule all previous cpuworkers to shut down _after_ processing
  101. * pending work. (This will cause fresh cpuworkers to be generated.)
  102. * - generate and upload a fresh routerinfo.
  103. */
  104. void
  105. rotate_onion_key(void)
  106. {
  107. char fname[512];
  108. char fname_prev[512];
  109. crypto_pk_env_t *prkey;
  110. tor_snprintf(fname,sizeof(fname),
  111. "%s/keys/secret_onion_key",get_options()->DataDirectory);
  112. tor_snprintf(fname_prev,sizeof(fname_prev),
  113. "%s/keys/secret_onion_key.old",get_options()->DataDirectory);
  114. if (!(prkey = crypto_new_pk_env())) {
  115. log_err(LD_GENERAL,"Error creating crypto environment.");
  116. goto error;
  117. }
  118. if (crypto_pk_generate_key(prkey)) {
  119. log_err(LD_BUG,"Error generating onion key");
  120. goto error;
  121. }
  122. if (file_status(fname) == FN_FILE) {
  123. if (replace_file(fname, fname_prev))
  124. goto error;
  125. }
  126. if (crypto_pk_write_private_key_to_filename(prkey, fname)) {
  127. log_err(LD_FS,"Couldn't write generated key to \"%s\".", fname);
  128. goto error;
  129. }
  130. log_info(LD_GENERAL, "Rotating onion key");
  131. tor_mutex_acquire(key_lock);
  132. if (lastonionkey)
  133. crypto_free_pk_env(lastonionkey);
  134. lastonionkey = onionkey;
  135. onionkey = prkey;
  136. onionkey_set_at = time(NULL);
  137. tor_mutex_release(key_lock);
  138. mark_my_descriptor_dirty();
  139. return;
  140. error:
  141. log_warn(LD_GENERAL, "Couldn't rotate onion key.");
  142. }
  143. /* Read an RSA secret key key from a file that was once named fname_old,
  144. * but is now named fname_new. Rename the file from old to new as needed.
  145. */
  146. static crypto_pk_env_t *
  147. init_key_from_file_name_changed(const char *fname_old,
  148. const char *fname_new)
  149. {
  150. if (file_status(fname_new) == FN_FILE || file_status(fname_old) != FN_FILE)
  151. /* The new filename is there, or both are, or neither is. */
  152. return init_key_from_file(fname_new);
  153. /* The old filename exists, and the new one doesn't. Rename and load. */
  154. if (rename(fname_old, fname_new) < 0) {
  155. log_warn(LD_FS, "Couldn't rename \"%s\" to \"%s\": %s",
  156. fname_old, fname_new, strerror(errno));
  157. return NULL;
  158. }
  159. return init_key_from_file(fname_new);
  160. }
  161. /** Try to read an RSA key from <b>fname</b>. If <b>fname</b> doesn't exist,
  162. * create a new RSA key and save it in <b>fname</b>. Return the read/created
  163. * key, or NULL on error.
  164. */
  165. crypto_pk_env_t *
  166. init_key_from_file(const char *fname)
  167. {
  168. crypto_pk_env_t *prkey = NULL;
  169. FILE *file = NULL;
  170. if (!(prkey = crypto_new_pk_env())) {
  171. log_err(LD_GENERAL,"Error creating crypto environment.");
  172. goto error;
  173. }
  174. switch (file_status(fname)) {
  175. case FN_DIR:
  176. case FN_ERROR:
  177. log_err(LD_FS,"Can't read key from \"%s\"", fname);
  178. goto error;
  179. case FN_NOENT:
  180. log_info(LD_GENERAL, "No key found in \"%s\"; generating fresh key.",
  181. fname);
  182. if (crypto_pk_generate_key(prkey)) {
  183. log_err(LD_GENERAL,"Error generating onion key");
  184. goto error;
  185. }
  186. if (crypto_pk_check_key(prkey) <= 0) {
  187. log_err(LD_GENERAL,"Generated key seems invalid");
  188. goto error;
  189. }
  190. log_info(LD_GENERAL, "Generated key seems valid");
  191. if (crypto_pk_write_private_key_to_filename(prkey, fname)) {
  192. log_err(LD_FS,"Couldn't write generated key to \"%s\".", fname);
  193. goto error;
  194. }
  195. return prkey;
  196. case FN_FILE:
  197. if (crypto_pk_read_private_key_from_filename(prkey, fname)) {
  198. log_err(LD_GENERAL,"Error loading private key.");
  199. goto error;
  200. }
  201. return prkey;
  202. default:
  203. tor_assert(0);
  204. }
  205. error:
  206. if (prkey)
  207. crypto_free_pk_env(prkey);
  208. if (file)
  209. fclose(file);
  210. return NULL;
  211. }
  212. /** Initialize all OR private keys, and the TLS context, as necessary.
  213. * On OPs, this only initializes the tls context.
  214. */
  215. int
  216. init_keys(void)
  217. {
  218. /* XXX009 Two problems with how this is called:
  219. * 1. It should be idempotent for servers, so we can call init_keys
  220. * as much as we need to.
  221. */
  222. char keydir[512];
  223. char keydir2[512];
  224. char fingerprint[FINGERPRINT_LEN+1];
  225. /*nickname<space>fp\n\0 */
  226. char fingerprint_line[MAX_NICKNAME_LEN+FINGERPRINT_LEN+3];
  227. char *cp;
  228. const char *mydesc, *datadir;
  229. crypto_pk_env_t *prkey;
  230. char digest[20];
  231. or_options_t *options = get_options();
  232. if (!key_lock)
  233. key_lock = tor_mutex_new();
  234. /* OP's don't need persistent keys; just make up an identity and
  235. * initialize the TLS context. */
  236. if (!server_mode(options)) {
  237. if (!(prkey = crypto_new_pk_env()))
  238. return -1;
  239. if (crypto_pk_generate_key(prkey))
  240. return -1;
  241. set_identity_key(prkey);
  242. /* Create a TLS context; default the client nickname to "client". */
  243. if (tor_tls_context_new(get_identity_key(), 1,
  244. options->Nickname ? options->Nickname : "client",
  245. MAX_SSL_KEY_LIFETIME) < 0) {
  246. log_err(LD_GENERAL,"Error creating TLS context for OP.");
  247. return -1;
  248. }
  249. return 0;
  250. }
  251. /* Make sure DataDirectory exists, and is private. */
  252. datadir = options->DataDirectory;
  253. if (check_private_dir(datadir, CPD_CREATE)) {
  254. return -1;
  255. }
  256. /* Check the key directory. */
  257. tor_snprintf(keydir,sizeof(keydir),"%s/keys", datadir);
  258. if (check_private_dir(keydir, CPD_CREATE)) {
  259. return -1;
  260. }
  261. cp = keydir + strlen(keydir); /* End of string. */
  262. /* 1. Read identity key. Make it if none is found. */
  263. tor_snprintf(keydir,sizeof(keydir),"%s/keys/identity.key",datadir);
  264. tor_snprintf(keydir2,sizeof(keydir2),"%s/keys/secret_id_key",datadir);
  265. log_info(LD_GENERAL,"Reading/making identity key \"%s\"...",keydir2);
  266. prkey = init_key_from_file_name_changed(keydir,keydir2);
  267. if (!prkey) return -1;
  268. set_identity_key(prkey);
  269. /* 2. Read onion key. Make it if none is found. */
  270. tor_snprintf(keydir,sizeof(keydir),"%s/keys/onion.key",datadir);
  271. tor_snprintf(keydir2,sizeof(keydir2),"%s/keys/secret_onion_key",datadir);
  272. log_info(LD_GENERAL,"Reading/making onion key \"%s\"...",keydir2);
  273. prkey = init_key_from_file_name_changed(keydir,keydir2);
  274. if (!prkey) return -1;
  275. set_onion_key(prkey);
  276. tor_snprintf(keydir,sizeof(keydir),"%s/keys/secret_onion_key.old",datadir);
  277. if (file_status(keydir) == FN_FILE) {
  278. prkey = init_key_from_file(keydir);
  279. if (prkey)
  280. lastonionkey = prkey;
  281. }
  282. /* 3. Initialize link key and TLS context. */
  283. if (tor_tls_context_new(get_identity_key(), 1, options->Nickname,
  284. MAX_SSL_KEY_LIFETIME) < 0) {
  285. log_err(LD_GENERAL,"Error initializing TLS context");
  286. return -1;
  287. }
  288. /* 4. Build our router descriptor. */
  289. /* Must be called after keys are initialized. */
  290. mydesc = router_get_my_descriptor();
  291. if (!mydesc) {
  292. log_err(LD_GENERAL,"Error initializing descriptor.");
  293. return -1;
  294. }
  295. if (authdir_mode(options)) {
  296. const char *m;
  297. /* We need to add our own fingerprint so it gets recognized. */
  298. if (dirserv_add_own_fingerprint(options->Nickname, get_identity_key())) {
  299. log_err(LD_GENERAL,"Error adding own fingerprint to approved set");
  300. return -1;
  301. }
  302. if (dirserv_add_descriptor(mydesc, &m) < 0) {
  303. log_err(LD_GENERAL,"Unable to add own descriptor to directory: %s",
  304. m?m:"<unknown error>");
  305. return -1;
  306. }
  307. }
  308. #if 0
  309. tor_snprintf(keydir,sizeof(keydir),"%s/router.desc", datadir);
  310. log_info(LD_GENERAL,"Dumping descriptor to \"%s\"...",keydir);
  311. if (write_str_to_file(keydir, mydesc,0)) {
  312. return -1;
  313. }
  314. #endif
  315. /* 5. Dump fingerprint to 'fingerprint' */
  316. tor_snprintf(keydir,sizeof(keydir),"%s/fingerprint", datadir);
  317. log_info(LD_GENERAL,"Dumping fingerprint to \"%s\"...",keydir);
  318. if (crypto_pk_get_fingerprint(get_identity_key(), fingerprint, 1)<0) {
  319. log_err(LD_GENERAL,"Error computing fingerprint");
  320. return -1;
  321. }
  322. tor_assert(strlen(options->Nickname) <= MAX_NICKNAME_LEN);
  323. if (tor_snprintf(fingerprint_line, sizeof(fingerprint_line),
  324. "%s %s\n",options->Nickname, fingerprint) < 0) {
  325. log_err(LD_GENERAL,"Error writing fingerprint line");
  326. return -1;
  327. }
  328. if (write_str_to_file(keydir, fingerprint_line, 0)) {
  329. log_err(LD_FS, "Error writing fingerprint line to file");
  330. return -1;
  331. }
  332. log(LOG_NOTICE, LD_GENERAL,
  333. "Your Tor server's identity key fingerprint is '%s %s'.",
  334. options->Nickname, fingerprint);
  335. if (!authdir_mode(options))
  336. return 0;
  337. /* 6. [authdirserver only] load approved-routers file */
  338. tor_snprintf(keydir,sizeof(keydir),"%s/approved-routers", datadir);
  339. log_info(LD_DIRSERV,"Loading approved fingerprints from \"%s\"...",keydir);
  340. if (dirserv_parse_fingerprint_file(keydir) < 0) {
  341. log_err(LD_GENERAL,"Error loading fingerprints");
  342. return -1;
  343. }
  344. /* 6b. [authdirserver only] add own key to approved directories. */
  345. crypto_pk_get_digest(get_identity_key(), digest);
  346. if (!router_digest_is_trusted_dir(digest)) {
  347. add_trusted_dir_server(options->Nickname, NULL,
  348. (uint16_t)options->DirPort, digest,
  349. options->V1AuthoritativeDir);
  350. }
  351. /* success */
  352. return 0;
  353. }
  354. /* Keep track of whether we should upload our server descriptor,
  355. * and what type of server we are.
  356. */
  357. /** Whether we can reach our ORPort from the outside. */
  358. static int can_reach_or_port = 0;
  359. /** Whether we can reach our DirPort from the outside. */
  360. static int can_reach_dir_port = 0;
  361. /** Return 1 if or port is known reachable; else return 0. */
  362. int
  363. check_whether_orport_reachable(void)
  364. {
  365. or_options_t *options = get_options();
  366. return clique_mode(options) ||
  367. options->AssumeReachable ||
  368. can_reach_or_port;
  369. }
  370. /** Return 1 if we don't have a dirport configured, or if it's reachable. */
  371. int
  372. check_whether_dirport_reachable(void)
  373. {
  374. or_options_t *options = get_options();
  375. return !options->DirPort ||
  376. options->AssumeReachable ||
  377. we_are_hibernating() ||
  378. can_reach_dir_port;
  379. }
  380. /** Look at a variety of factors, and return 0 if we don't want to
  381. * advertise the fact that we have a DirPort open. Else return the
  382. * DirPort we want to advertise. */
  383. static int
  384. decide_to_advertise_dirport(or_options_t *options, routerinfo_t *router)
  385. {
  386. if (!router->dir_port) /* short circuit the rest of the function */
  387. return 0;
  388. if (authdir_mode(options)) /* always publish */
  389. return router->dir_port;
  390. if (we_are_hibernating())
  391. return 0;
  392. if (!check_whether_dirport_reachable())
  393. return 0;
  394. if (router->bandwidthcapacity >= router->bandwidthrate) {
  395. /* check if we might potentially hibernate. */
  396. if (options->AccountingMax != 0)
  397. return 0;
  398. /* also check if we're advertising a small amount, and have
  399. a "boring" DirPort. */
  400. if (router->bandwidthrate < 50000 && router->dir_port > 1024)
  401. return 0;
  402. }
  403. /* Sounds like a great idea. Let's publish it. */
  404. return router->dir_port;
  405. }
  406. /** Some time has passed, or we just got new directory information.
  407. * See if we currently believe our ORPort or DirPort to be
  408. * unreachable. If so, launch a new test for it.
  409. *
  410. * For ORPort, we simply try making a circuit that ends at ourselves.
  411. * Success is noticed in onionskin_answer().
  412. *
  413. * For DirPort, we make a connection via Tor to our DirPort and ask
  414. * for our own server descriptor.
  415. * Success is noticed in connection_dir_client_reached_eof().
  416. */
  417. void
  418. consider_testing_reachability(void)
  419. {
  420. routerinfo_t *me = router_get_my_routerinfo();
  421. if (!me) {
  422. log_warn(LD_BUG,
  423. "Bug: router_get_my_routerinfo() did not find my routerinfo?");
  424. return;
  425. }
  426. if (!check_whether_orport_reachable()) {
  427. circuit_launch_by_router(CIRCUIT_PURPOSE_TESTING, me, 0, 1, 1);
  428. }
  429. if (!check_whether_dirport_reachable()) {
  430. /* ask myself, via tor, for my server descriptor. */
  431. directory_initiate_command_router(me, DIR_PURPOSE_FETCH_SERVERDESC,
  432. 1, "authority", NULL, 0);
  433. }
  434. }
  435. /** Annotate that we found our ORPort reachable. */
  436. void
  437. router_orport_found_reachable(void)
  438. {
  439. if (!can_reach_or_port) {
  440. if (!clique_mode(get_options()))
  441. log_notice(LD_OR,"Self-testing indicates your ORPort is reachable from "
  442. "the outside. Excellent.%s",
  443. get_options()->PublishServerDescriptor ?
  444. " Publishing server descriptor." : "");
  445. can_reach_or_port = 1;
  446. mark_my_descriptor_dirty();
  447. consider_publishable_server(time(NULL), 1);
  448. }
  449. }
  450. /** Annotate that we found our DirPort reachable. */
  451. void
  452. router_dirport_found_reachable(void)
  453. {
  454. if (!can_reach_dir_port) {
  455. log_notice(LD_DIRSERV,"Self-testing indicates your DirPort is reachable "
  456. "from the outside. Excellent.");
  457. can_reach_dir_port = 1;
  458. }
  459. }
  460. /** Our router has just moved to a new IP. Reset stats. */
  461. void
  462. server_has_changed_ip(void)
  463. {
  464. stats_n_seconds_working = 0;
  465. can_reach_or_port = 0;
  466. can_reach_dir_port = 0;
  467. mark_my_descriptor_dirty();
  468. }
  469. /** Return true iff we believe ourselves to be an authoritative
  470. * directory server.
  471. */
  472. int
  473. authdir_mode(or_options_t *options)
  474. {
  475. return options->AuthoritativeDir != 0;
  476. }
  477. /** Return true iff we try to stay connected to all ORs at once.
  478. */
  479. int
  480. clique_mode(or_options_t *options)
  481. {
  482. return authdir_mode(options);
  483. }
  484. /** Return true iff we are trying to be a server.
  485. */
  486. int
  487. server_mode(or_options_t *options)
  488. {
  489. if (options->ClientOnly) return 0;
  490. return (options->ORPort != 0 || options->ORListenAddress);
  491. }
  492. /** Remember if we've advertised ourselves to the dirservers. */
  493. static int server_is_advertised=0;
  494. /** Return true iff we have published our descriptor lately.
  495. */
  496. int
  497. advertised_server_mode(void)
  498. {
  499. return server_is_advertised;
  500. }
  501. /**
  502. * Called with a boolean: set whether we have recently published our
  503. * descriptor.
  504. */
  505. static void
  506. set_server_advertised(int s)
  507. {
  508. server_is_advertised = s;
  509. }
  510. /** Return true iff we are trying to be a socks proxy. */
  511. int
  512. proxy_mode(or_options_t *options)
  513. {
  514. return (options->SocksPort != 0 || options->SocksListenAddress);
  515. }
  516. /** Decide if we're a publishable server. We are a publishable server if:
  517. * - We don't have the ClientOnly option set
  518. * and
  519. * - We have the PublishServerDescriptor option set
  520. * and
  521. * - We have ORPort set
  522. * and
  523. * - We believe we are reachable from the outside; or
  524. * - We have the AuthoritativeDirectory option set.
  525. */
  526. static int
  527. decide_if_publishable_server(time_t now)
  528. {
  529. or_options_t *options = get_options();
  530. if (options->ClientOnly)
  531. return 0;
  532. if (!options->PublishServerDescriptor)
  533. return 0;
  534. if (!server_mode(options))
  535. return 0;
  536. if (options->AuthoritativeDir)
  537. return 1;
  538. return check_whether_orport_reachable();
  539. }
  540. /** Initiate server descriptor upload as reasonable (if server is publishable,
  541. * etc). <b>force</b> is as for router_upload_dir_desc_to_dirservers.
  542. *
  543. * We need to rebuild the descriptor if it's dirty even if we're not
  544. * uploading, because our reachability testing *uses* our descriptor to
  545. * determine what IP address and ports to test.
  546. */
  547. void
  548. consider_publishable_server(time_t now, int force)
  549. {
  550. int rebuilt;
  551. if (!server_mode(get_options()))
  552. return;
  553. rebuilt = router_rebuild_descriptor(0);
  554. if (decide_if_publishable_server(now)) {
  555. set_server_advertised(1);
  556. if (rebuilt == 0)
  557. router_upload_dir_desc_to_dirservers(force);
  558. } else {
  559. set_server_advertised(0);
  560. }
  561. }
  562. /*
  563. * Clique maintenance
  564. */
  565. /** OR only: if in clique mode, try to open connections to all of the
  566. * other ORs we know about. Otherwise, open connections to those we
  567. * think are in clique mode.
  568. *
  569. * If <b>testing_reachability</b> is 0, try to open the connections
  570. * but only if we don't already have one. If it's 1, then we're an
  571. * auth dir server, and we should try to connect regardless of
  572. * whether we already have a connection open -- but if <b>try_all</b>
  573. * is 0, we want to load balance such that we only try a few connections
  574. * per call.
  575. *
  576. * The load balancing is such that if we get called once every ten
  577. * seconds, we will cycle through all the tests in 1280 seconds (a
  578. * bit over 20 minutes).
  579. */
  580. void
  581. router_retry_connections(int testing_reachability, int try_all)
  582. {
  583. time_t now = time(NULL);
  584. routerlist_t *rl = router_get_routerlist();
  585. or_options_t *options = get_options();
  586. static char ctr = 0;
  587. tor_assert(server_mode(options));
  588. SMARTLIST_FOREACH(rl->routers, routerinfo_t *, router, {
  589. const char *id_digest = router->cache_info.identity_digest;
  590. if (router_is_me(router))
  591. continue;
  592. if (!clique_mode(options) && !router_is_clique_mode(router))
  593. continue;
  594. if ((testing_reachability &&
  595. (try_all || (((uint8_t)id_digest[0]) % 128) == ctr)) ||
  596. (!testing_reachability &&
  597. !connection_or_get_by_identity_digest(id_digest))) {
  598. log_debug(LD_OR,"%sconnecting to %s at %s:%u.",
  599. clique_mode(options) ? "(forced) " : "",
  600. router->nickname, router->address, router->or_port);
  601. /* Remember when we started trying to determine reachability */
  602. if (!router->testing_since)
  603. router->testing_since = now;
  604. connection_or_connect(router->addr, router->or_port,
  605. id_digest);
  606. }
  607. });
  608. if (testing_reachability && !try_all) /* increment ctr */
  609. ctr = (ctr + 1) % 128;
  610. }
  611. /** Return true iff this OR should try to keep connections open to all
  612. * other ORs. */
  613. int
  614. router_is_clique_mode(routerinfo_t *router)
  615. {
  616. if (router_digest_is_trusted_dir(router->cache_info.identity_digest))
  617. return 1;
  618. return 0;
  619. }
  620. /*
  621. * OR descriptor generation.
  622. */
  623. /** My routerinfo. */
  624. static routerinfo_t *desc_routerinfo = NULL;
  625. /** Since when has our descriptor been "clean"? 0 if we need to regenerate it
  626. * now. */
  627. static time_t desc_clean_since = 0;
  628. /** Boolean: do we need to regenerate the above? */
  629. static int desc_needs_upload = 0;
  630. /** OR only: If <b>force</b> is true, or we haven't uploaded this
  631. * descriptor successfully yet, try to upload our signed descriptor to
  632. * all the directory servers we know about.
  633. */
  634. void
  635. router_upload_dir_desc_to_dirservers(int force)
  636. {
  637. const char *s;
  638. s = router_get_my_descriptor();
  639. if (!s) {
  640. log_warn(LD_GENERAL, "No descriptor; skipping upload");
  641. return;
  642. }
  643. if (!get_options()->PublishServerDescriptor)
  644. return;
  645. if (!force && !desc_needs_upload)
  646. return;
  647. desc_needs_upload = 0;
  648. directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_DIR, s, strlen(s));
  649. }
  650. /** OR only: Check whether my exit policy says to allow connection to
  651. * conn. Return 0 if we accept; non-0 if we reject.
  652. */
  653. int
  654. router_compare_to_my_exit_policy(connection_t *conn)
  655. {
  656. tor_assert(desc_routerinfo);
  657. /* make sure it's resolved to something. this way we can't get a
  658. 'maybe' below. */
  659. if (!conn->addr)
  660. return -1;
  661. return compare_addr_to_addr_policy(conn->addr, conn->port,
  662. desc_routerinfo->exit_policy) != ADDR_POLICY_ACCEPTED;
  663. }
  664. /** Return true iff I'm a server and <b>digest</b> is equal to
  665. * my identity digest. */
  666. int
  667. router_digest_is_me(const char *digest)
  668. {
  669. routerinfo_t *me = router_get_my_routerinfo();
  670. if (!me || memcmp(me->cache_info.identity_digest, digest, DIGEST_LEN))
  671. return 0;
  672. return 1;
  673. }
  674. /** A wrapper around router_digest_is_me(). */
  675. int
  676. router_is_me(routerinfo_t *router)
  677. {
  678. return router_digest_is_me(router->cache_info.identity_digest);
  679. }
  680. /** Return true iff <b>fp</b> is a hex fingerprint of my identity digest. */
  681. int
  682. router_fingerprint_is_me(const char *fp)
  683. {
  684. char digest[DIGEST_LEN];
  685. if (strlen(fp) == HEX_DIGEST_LEN &&
  686. base16_decode(digest, sizeof(digest), fp, HEX_DIGEST_LEN) == 0)
  687. return router_digest_is_me(digest);
  688. return 0;
  689. }
  690. /** Return a routerinfo for this OR, rebuilding a fresh one if
  691. * necessary. Return NULL on error, or if called on an OP. */
  692. routerinfo_t *
  693. router_get_my_routerinfo(void)
  694. {
  695. if (!server_mode(get_options()))
  696. return NULL;
  697. if (!desc_routerinfo) {
  698. if (router_rebuild_descriptor(1))
  699. return NULL;
  700. }
  701. return desc_routerinfo;
  702. }
  703. /** OR only: Return a signed server descriptor for this OR, rebuilding a fresh
  704. * one if necessary. Return NULL on error.
  705. */
  706. const char *
  707. router_get_my_descriptor(void)
  708. {
  709. const char *body;
  710. if (!desc_routerinfo) {
  711. if (router_rebuild_descriptor(1))
  712. return NULL;
  713. }
  714. body = signed_descriptor_get_body(&desc_routerinfo->cache_info);
  715. log_debug(LD_GENERAL,"my desc is '%s'", body);
  716. return body;
  717. }
  718. /*DOCDOC*/
  719. static smartlist_t *warned_nonexistent_family = NULL;
  720. /** If <b>force</b> is true, or our descriptor is out-of-date, rebuild
  721. * a fresh routerinfo and signed server descriptor for this OR.
  722. * Return 0 on success, -1 on error.
  723. */
  724. int
  725. router_rebuild_descriptor(int force)
  726. {
  727. routerinfo_t *ri;
  728. uint32_t addr;
  729. char platform[256];
  730. int hibernating = we_are_hibernating();
  731. or_options_t *options = get_options();
  732. if (desc_clean_since && !force)
  733. return 0;
  734. if (resolve_my_address(options, &addr, NULL) < 0) {
  735. log_warn(LD_CONFIG,"options->Address didn't resolve into an IP.");
  736. return -1;
  737. }
  738. ri = tor_malloc_zero(sizeof(routerinfo_t));
  739. ri->address = tor_dup_addr(addr);
  740. ri->nickname = tor_strdup(options->Nickname);
  741. ri->addr = addr;
  742. ri->or_port = options->ORPort;
  743. ri->dir_port = options->DirPort;
  744. ri->cache_info.published_on = time(NULL);
  745. ri->onion_pkey = crypto_pk_dup_key(get_onion_key()); /* must invoke from
  746. * main thread */
  747. ri->identity_pkey = crypto_pk_dup_key(get_identity_key());
  748. if (crypto_pk_get_digest(ri->identity_pkey,
  749. ri->cache_info.identity_digest)<0) {
  750. routerinfo_free(ri);
  751. return -1;
  752. }
  753. get_platform_str(platform, sizeof(platform));
  754. ri->platform = tor_strdup(platform);
  755. ri->bandwidthrate = (int)options->BandwidthRate;
  756. ri->bandwidthburst = (int)options->BandwidthBurst;
  757. ri->bandwidthcapacity = hibernating ? 0 : rep_hist_bandwidth_assess();
  758. if (options->BandwidthRate > options->MaxAdvertisedBandwidth)
  759. ri->bandwidthrate = (int)options->MaxAdvertisedBandwidth;
  760. policies_parse_exit_policy(options->ExitPolicy, &ri->exit_policy,
  761. options->ExitPolicyRejectPrivate);
  762. if (desc_routerinfo) { /* inherit values */
  763. ri->is_valid = desc_routerinfo->is_valid;
  764. ri->is_running = desc_routerinfo->is_running;
  765. ri->is_named = desc_routerinfo->is_named;
  766. }
  767. if (authdir_mode(options))
  768. ri->is_valid = ri->is_named = 1; /* believe in yourself */
  769. if (options->MyFamily) {
  770. smartlist_t *family;
  771. if (!warned_nonexistent_family)
  772. warned_nonexistent_family = smartlist_create();
  773. family = smartlist_create();
  774. ri->declared_family = smartlist_create();
  775. smartlist_split_string(family, options->MyFamily, ",",
  776. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  777. SMARTLIST_FOREACH(family, char *, name,
  778. {
  779. routerinfo_t *member;
  780. if (!strcasecmp(name, options->Nickname))
  781. member = ri;
  782. else
  783. member = router_get_by_nickname(name, 1);
  784. if (!member) {
  785. if (!smartlist_string_isin(warned_nonexistent_family, name) &&
  786. !is_legal_hexdigest(name)) {
  787. log_warn(LD_CONFIG,
  788. "I have no descriptor for the router named \"%s\" "
  789. "in my declared family; I'll use the nickname as is, but "
  790. "this may confuse clients.", name);
  791. smartlist_add(warned_nonexistent_family, tor_strdup(name));
  792. }
  793. smartlist_add(ri->declared_family, name);
  794. name = NULL;
  795. } else {
  796. char *fp = tor_malloc(HEX_DIGEST_LEN+2);
  797. fp[0] = '$';
  798. base16_encode(fp+1,HEX_DIGEST_LEN+1,
  799. member->cache_info.identity_digest, DIGEST_LEN);
  800. smartlist_add(ri->declared_family, fp);
  801. if (smartlist_string_isin(warned_nonexistent_family, name))
  802. smartlist_string_remove(warned_nonexistent_family, name);
  803. }
  804. tor_free(name);
  805. });
  806. smartlist_free(family);
  807. }
  808. ri->cache_info.signed_descriptor_body = tor_malloc(8192);
  809. if (router_dump_router_to_string(ri->cache_info.signed_descriptor_body, 8192,
  810. ri, get_identity_key())<0) {
  811. log_warn(LD_BUG, "Couldn't allocate string for descriptor.");
  812. return -1;
  813. }
  814. ri->cache_info.signed_descriptor_len =
  815. strlen(ri->cache_info.signed_descriptor_body);
  816. crypto_digest(ri->cache_info.signed_descriptor_digest,
  817. ri->cache_info.signed_descriptor_body,
  818. ri->cache_info.signed_descriptor_len);
  819. if (desc_routerinfo)
  820. routerinfo_free(desc_routerinfo);
  821. desc_routerinfo = ri;
  822. desc_clean_since = time(NULL);
  823. desc_needs_upload = 1;
  824. return 0;
  825. }
  826. /** Mark descriptor out of date if it's older than <b>when</b> */
  827. void
  828. mark_my_descriptor_dirty_if_older_than(time_t when)
  829. {
  830. if (desc_clean_since < when)
  831. mark_my_descriptor_dirty();
  832. }
  833. /** Call when the current descriptor is out of date. */
  834. void
  835. mark_my_descriptor_dirty(void)
  836. {
  837. desc_clean_since = 0;
  838. }
  839. /** How frequently will we republish our descriptor because of large (factor
  840. * of 2) shifts in estimated bandwidth? */
  841. #define MAX_BANDWIDTH_CHANGE_FREQ (20*60)
  842. /** Check whether bandwidth has changed a lot since the last time we announced
  843. * bandwidth. If so, mark our descriptor dirty. */
  844. void
  845. check_descriptor_bandwidth_changed(time_t now)
  846. {
  847. static time_t last_changed = 0;
  848. uint64_t prev, cur;
  849. if (!desc_routerinfo)
  850. return;
  851. prev = desc_routerinfo->bandwidthcapacity;
  852. cur = we_are_hibernating() ? 0 : rep_hist_bandwidth_assess();
  853. if ((prev != cur && (!prev || !cur)) ||
  854. cur > prev*2 ||
  855. cur < prev/2) {
  856. if (last_changed+MAX_BANDWIDTH_CHANGE_FREQ < now) {
  857. log_info(LD_GENERAL,
  858. "Measured bandwidth has changed; rebuilding descriptor.");
  859. mark_my_descriptor_dirty();
  860. last_changed = now;
  861. }
  862. }
  863. }
  864. /** Check whether our own address as defined by the Address configuration
  865. * has changed. This is for routers that get their address from a service
  866. * like dyndns. If our address has changed, mark our descriptor dirty. */
  867. void
  868. check_descriptor_ipaddress_changed(time_t now)
  869. {
  870. uint32_t prev, cur;
  871. or_options_t *options = get_options();
  872. if (!desc_routerinfo)
  873. return;
  874. prev = desc_routerinfo->addr;
  875. if (resolve_my_address(options, &cur, NULL) < 0) {
  876. log_warn(LD_CONFIG,"options->Address didn't resolve into an IP.");
  877. return;
  878. }
  879. if (prev != cur) {
  880. char addrbuf_prev[INET_NTOA_BUF_LEN];
  881. char addrbuf_cur[INET_NTOA_BUF_LEN];
  882. struct in_addr in_prev;
  883. struct in_addr in_cur;
  884. in_prev.s_addr = htonl(prev);
  885. tor_inet_ntoa(&in_prev, addrbuf_prev, sizeof(addrbuf_prev));
  886. in_cur.s_addr = htonl(cur);
  887. tor_inet_ntoa(&in_cur, addrbuf_cur, sizeof(addrbuf_cur));
  888. log_info(LD_GENERAL,
  889. "Our IP Address has changed from %s to %s; "
  890. "rebuilding descriptor.",
  891. addrbuf_prev, addrbuf_cur);
  892. mark_my_descriptor_dirty();
  893. }
  894. }
  895. /** Set <b>platform</b> (max length <b>len</b>) to a NUL-terminated short
  896. * string describing the version of Tor and the operating system we're
  897. * currently running on.
  898. */
  899. void
  900. get_platform_str(char *platform, size_t len)
  901. {
  902. tor_snprintf(platform, len, "Tor %s on %s",
  903. VERSION, get_uname());
  904. return;
  905. }
  906. /* XXX need to audit this thing and count fenceposts. maybe
  907. * refactor so we don't have to keep asking if we're
  908. * near the end of maxlen?
  909. */
  910. #define DEBUG_ROUTER_DUMP_ROUTER_TO_STRING
  911. /** OR only: Given a routerinfo for this router, and an identity key to sign
  912. * with, encode the routerinfo as a signed server descriptor and write the
  913. * result into <b>s</b>, using at most <b>maxlen</b> bytes. Return -1 on
  914. * failure, and the number of bytes used on success.
  915. */
  916. int
  917. router_dump_router_to_string(char *s, size_t maxlen, routerinfo_t *router,
  918. crypto_pk_env_t *ident_key)
  919. {
  920. char *onion_pkey; /* Onion key, PEM-encoded. */
  921. char *identity_pkey; /* Identity key, PEM-encoded. */
  922. char digest[DIGEST_LEN];
  923. char published[ISO_TIME_LEN+1];
  924. char fingerprint[FINGERPRINT_LEN+1];
  925. struct in_addr in;
  926. char addrbuf[INET_NTOA_BUF_LEN];
  927. size_t onion_pkeylen, identity_pkeylen;
  928. size_t written;
  929. int result=0;
  930. addr_policy_t *tmpe;
  931. char *bandwidth_usage;
  932. char *family_line;
  933. #ifdef DEBUG_ROUTER_DUMP_ROUTER_TO_STRING
  934. char *s_tmp, *s_dup;
  935. const char *cp;
  936. routerinfo_t *ri_tmp;
  937. #endif
  938. or_options_t *options = get_options();
  939. /* Make sure the identity key matches the one in the routerinfo. */
  940. if (crypto_pk_cmp_keys(ident_key, router->identity_pkey)) {
  941. log_warn(LD_BUG,"Tried to sign a router with a private key that didn't "
  942. "match router's public key!");
  943. return -1;
  944. }
  945. /* record our fingerprint, so we can include it in the descriptor */
  946. if (crypto_pk_get_fingerprint(router->identity_pkey, fingerprint, 1)<0) {
  947. log_err(LD_BUG,"Error computing fingerprint");
  948. return -1;
  949. }
  950. /* PEM-encode the onion key */
  951. if (crypto_pk_write_public_key_to_string(router->onion_pkey,
  952. &onion_pkey,&onion_pkeylen)<0) {
  953. log_warn(LD_BUG,"write onion_pkey to string failed!");
  954. return -1;
  955. }
  956. /* PEM-encode the identity key key */
  957. if (crypto_pk_write_public_key_to_string(router->identity_pkey,
  958. &identity_pkey,&identity_pkeylen)<0) {
  959. log_warn(LD_BUG,"write identity_pkey to string failed!");
  960. tor_free(onion_pkey);
  961. return -1;
  962. }
  963. /* Encode the publication time. */
  964. format_iso_time(published, router->cache_info.published_on);
  965. /* How busy have we been? */
  966. bandwidth_usage = rep_hist_get_bandwidth_lines();
  967. if (router->declared_family && smartlist_len(router->declared_family)) {
  968. size_t n;
  969. char *s = smartlist_join_strings(router->declared_family, " ", 0, &n);
  970. n += strlen("family ") + 2; /* 1 for \n, 1 for \0. */
  971. family_line = tor_malloc(n);
  972. tor_snprintf(family_line, n, "family %s\n", s);
  973. tor_free(s);
  974. } else {
  975. family_line = tor_strdup("");
  976. }
  977. /* Generate the easy portion of the router descriptor. */
  978. result = tor_snprintf(s, maxlen,
  979. "router %s %s %d 0 %d\n"
  980. "platform %s\n"
  981. "published %s\n"
  982. "opt fingerprint %s\n"
  983. "uptime %ld\n"
  984. "bandwidth %d %d %d\n"
  985. "onion-key\n%s"
  986. "signing-key\n%s%s%s%s",
  987. router->nickname,
  988. router->address,
  989. router->or_port,
  990. decide_to_advertise_dirport(options, router),
  991. router->platform,
  992. published,
  993. fingerprint,
  994. stats_n_seconds_working,
  995. (int) router->bandwidthrate,
  996. (int) router->bandwidthburst,
  997. (int) router->bandwidthcapacity,
  998. onion_pkey, identity_pkey,
  999. family_line, bandwidth_usage,
  1000. we_are_hibernating() ? "opt hibernating 1\n" : "");
  1001. tor_free(family_line);
  1002. tor_free(onion_pkey);
  1003. tor_free(identity_pkey);
  1004. tor_free(bandwidth_usage);
  1005. if (result < 0)
  1006. return -1;
  1007. /* From now on, we use 'written' to remember the current length of 's'. */
  1008. written = result;
  1009. if (options->ContactInfo && strlen(options->ContactInfo)) {
  1010. result = tor_snprintf(s+written,maxlen-written, "contact %s\n",
  1011. options->ContactInfo);
  1012. if (result<0)
  1013. return -1;
  1014. written += result;
  1015. }
  1016. /* Write the exit policy to the end of 's'. */
  1017. for (tmpe=router->exit_policy; tmpe; tmpe=tmpe->next) {
  1018. /* Write: "accept 1.2.3.4" */
  1019. in.s_addr = htonl(tmpe->addr);
  1020. tor_inet_ntoa(&in, addrbuf, sizeof(addrbuf));
  1021. result = tor_snprintf(s+written, maxlen-written, "%s %s",
  1022. tmpe->policy_type == ADDR_POLICY_ACCEPT ? "accept" : "reject",
  1023. tmpe->msk == 0 ? "*" : addrbuf);
  1024. if (result < 0)
  1025. return -1;
  1026. written += result;
  1027. if (tmpe->msk != 0xFFFFFFFFu && tmpe->msk != 0) {
  1028. int n_bits = addr_mask_get_bits(tmpe->msk);
  1029. if (n_bits >= 0) {
  1030. if (tor_snprintf(s+written, maxlen-written, "/%d", n_bits)<0)
  1031. return -1;
  1032. } else {
  1033. /* Write "/255.255.0.0" */
  1034. in.s_addr = htonl(tmpe->msk);
  1035. tor_inet_ntoa(&in, addrbuf, sizeof(addrbuf));
  1036. if (tor_snprintf(s+written, maxlen-written, "/%s", addrbuf)<0)
  1037. return -1;
  1038. }
  1039. written += strlen(s+written);
  1040. }
  1041. if (tmpe->prt_min <= 1 && tmpe->prt_max == 65535) {
  1042. /* There is no port set; write ":*" */
  1043. if (written+4 > maxlen)
  1044. return -1;
  1045. strlcat(s+written, ":*\n", maxlen-written);
  1046. written += 3;
  1047. } else if (tmpe->prt_min == tmpe->prt_max) {
  1048. /* There is only one port; write ":80". */
  1049. result = tor_snprintf(s+written, maxlen-written, ":%d\n", tmpe->prt_min);
  1050. if (result<0)
  1051. return -1;
  1052. written += result;
  1053. } else {
  1054. /* There is a range of ports; write ":79-80". */
  1055. result = tor_snprintf(s+written, maxlen-written, ":%d-%d\n",
  1056. tmpe->prt_min, tmpe->prt_max);
  1057. if (result<0)
  1058. return -1;
  1059. written += result;
  1060. }
  1061. } /* end for */
  1062. if (written+256 > maxlen) /* Not enough room for signature. */
  1063. return -1;
  1064. /* Sign the directory */
  1065. strlcat(s+written, "router-signature\n", maxlen-written);
  1066. written += strlen(s+written);
  1067. s[written] = '\0';
  1068. if (router_get_router_hash(s, digest) < 0)
  1069. return -1;
  1070. if (router_append_dirobj_signature(s+written,maxlen-written,
  1071. digest,ident_key)<0) {
  1072. log_warn(LD_BUG, "Couldn't sign router descriptor");
  1073. return -1;
  1074. }
  1075. written += strlen(s+written);
  1076. if (written+2 > maxlen)
  1077. return -1;
  1078. /* include a last '\n' */
  1079. s[written] = '\n';
  1080. s[written+1] = 0;
  1081. #ifdef DEBUG_ROUTER_DUMP_ROUTER_TO_STRING
  1082. cp = s_tmp = s_dup = tor_strdup(s);
  1083. ri_tmp = router_parse_entry_from_string(cp, NULL);
  1084. if (!ri_tmp) {
  1085. log_err(LD_BUG,
  1086. "We just generated a router descriptor we can't parse: <<%s>>",
  1087. s);
  1088. return -1;
  1089. }
  1090. tor_free(s_dup);
  1091. routerinfo_free(ri_tmp);
  1092. #endif
  1093. return written+1;
  1094. }
  1095. /** Return true iff <b>s</b> is a legally valid server nickname. */
  1096. int
  1097. is_legal_nickname(const char *s)
  1098. {
  1099. size_t len;
  1100. tor_assert(s);
  1101. len = strlen(s);
  1102. return len > 0 && len <= MAX_NICKNAME_LEN &&
  1103. strspn(s,LEGAL_NICKNAME_CHARACTERS) == len;
  1104. }
  1105. /** Return true iff <b>s</b> is a legally valid server nickname or
  1106. * hex-encoded identity-key digest. */
  1107. int
  1108. is_legal_nickname_or_hexdigest(const char *s)
  1109. {
  1110. if (*s!='$')
  1111. return is_legal_nickname(s);
  1112. else
  1113. return is_legal_hexdigest(s);
  1114. }
  1115. /** Return true iff <b>s</b> is a legally valid hex-encoded identity-key
  1116. * digest. */
  1117. int
  1118. is_legal_hexdigest(const char *s)
  1119. {
  1120. size_t len;
  1121. tor_assert(s);
  1122. len = strlen(s);
  1123. return (len == HEX_DIGEST_LEN+1 &&
  1124. s[0] == '$' &&
  1125. strspn(s+1,HEX_CHARACTERS)==len-1);
  1126. }
  1127. /** Forget that we have issued any router-related warnings, so that we'll
  1128. * warn again if we see the same errors. */
  1129. void
  1130. router_reset_warnings(void)
  1131. {
  1132. if (warned_nonexistent_family) {
  1133. SMARTLIST_FOREACH(warned_nonexistent_family, char *, cp, tor_free(cp));
  1134. smartlist_clear(warned_nonexistent_family);
  1135. }
  1136. }
  1137. /** Release all static resources held in router.c */
  1138. void
  1139. router_free_all(void)
  1140. {
  1141. if (onionkey)
  1142. crypto_free_pk_env(onionkey);
  1143. if (lastonionkey)
  1144. crypto_free_pk_env(lastonionkey);
  1145. if (identitykey)
  1146. crypto_free_pk_env(identitykey);
  1147. if (key_lock)
  1148. tor_mutex_free(key_lock);
  1149. if (desc_routerinfo)
  1150. routerinfo_free(desc_routerinfo);
  1151. if (warned_nonexistent_family) {
  1152. SMARTLIST_FOREACH(warned_nonexistent_family, char *, cp, tor_free(cp));
  1153. smartlist_free(warned_nonexistent_family);
  1154. }
  1155. }