router.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279
  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. Return 0 on success,
  214. * or -1 if Tor should die.
  215. */
  216. int
  217. init_keys(void)
  218. {
  219. char keydir[512];
  220. char keydir2[512];
  221. char fingerprint[FINGERPRINT_LEN+1];
  222. /*nickname<space>fp\n\0 */
  223. char fingerprint_line[MAX_NICKNAME_LEN+FINGERPRINT_LEN+3];
  224. char *cp;
  225. const char *mydesc, *datadir;
  226. crypto_pk_env_t *prkey;
  227. char digest[20];
  228. or_options_t *options = get_options();
  229. if (!key_lock)
  230. key_lock = tor_mutex_new();
  231. /* OP's don't need persistent keys; just make up an identity and
  232. * initialize the TLS context. */
  233. if (!server_mode(options)) {
  234. if (!(prkey = crypto_new_pk_env()))
  235. return -1;
  236. if (crypto_pk_generate_key(prkey))
  237. return -1;
  238. set_identity_key(prkey);
  239. /* Create a TLS context; default the client nickname to "client". */
  240. if (tor_tls_context_new(get_identity_key(),
  241. options->Nickname ? options->Nickname : "client",
  242. MAX_SSL_KEY_LIFETIME) < 0) {
  243. log_err(LD_GENERAL,"Error creating TLS context for OP.");
  244. return -1;
  245. }
  246. return 0;
  247. }
  248. /* Make sure DataDirectory exists, and is private. */
  249. datadir = options->DataDirectory;
  250. if (check_private_dir(datadir, CPD_CREATE)) {
  251. return -1;
  252. }
  253. /* Check the key directory. */
  254. tor_snprintf(keydir,sizeof(keydir),"%s/keys", datadir);
  255. if (check_private_dir(keydir, CPD_CREATE)) {
  256. return -1;
  257. }
  258. cp = keydir + strlen(keydir); /* End of string. */
  259. /* 1. Read identity key. Make it if none is found. */
  260. tor_snprintf(keydir,sizeof(keydir),"%s/keys/identity.key",datadir);
  261. tor_snprintf(keydir2,sizeof(keydir2),"%s/keys/secret_id_key",datadir);
  262. log_info(LD_GENERAL,"Reading/making identity key \"%s\"...",keydir2);
  263. prkey = init_key_from_file_name_changed(keydir,keydir2);
  264. if (!prkey) return -1;
  265. set_identity_key(prkey);
  266. /* 2. Read onion key. Make it if none is found. */
  267. tor_snprintf(keydir,sizeof(keydir),"%s/keys/onion.key",datadir);
  268. tor_snprintf(keydir2,sizeof(keydir2),"%s/keys/secret_onion_key",datadir);
  269. log_info(LD_GENERAL,"Reading/making onion key \"%s\"...",keydir2);
  270. prkey = init_key_from_file_name_changed(keydir,keydir2);
  271. if (!prkey) return -1;
  272. set_onion_key(prkey);
  273. tor_snprintf(keydir,sizeof(keydir),"%s/keys/secret_onion_key.old",datadir);
  274. if (file_status(keydir) == FN_FILE) {
  275. prkey = init_key_from_file(keydir);
  276. if (prkey)
  277. lastonionkey = prkey;
  278. }
  279. /* 3. Initialize link key and TLS context. */
  280. if (tor_tls_context_new(get_identity_key(), options->Nickname,
  281. MAX_SSL_KEY_LIFETIME) < 0) {
  282. log_err(LD_GENERAL,"Error initializing TLS context");
  283. return -1;
  284. }
  285. /* 4. Build our router descriptor. */
  286. /* Must be called after keys are initialized. */
  287. mydesc = router_get_my_descriptor();
  288. if (authdir_mode(options)) {
  289. const char *m;
  290. /* We need to add our own fingerprint so it gets recognized. */
  291. if (dirserv_add_own_fingerprint(options->Nickname, get_identity_key())) {
  292. log_err(LD_GENERAL,"Error adding own fingerprint to approved set");
  293. return -1;
  294. }
  295. if (!mydesc) {
  296. log_err(LD_GENERAL,"Error initializing descriptor.");
  297. return -1;
  298. }
  299. if (dirserv_add_descriptor(mydesc, &m) < 0) {
  300. log_err(LD_GENERAL,"Unable to add own descriptor to directory: %s",
  301. m?m:"<unknown error>");
  302. return -1;
  303. }
  304. }
  305. /* 5. Dump fingerprint to 'fingerprint' */
  306. tor_snprintf(keydir,sizeof(keydir),"%s/fingerprint", datadir);
  307. log_info(LD_GENERAL,"Dumping fingerprint to \"%s\"...",keydir);
  308. if (crypto_pk_get_fingerprint(get_identity_key(), fingerprint, 1)<0) {
  309. log_err(LD_GENERAL,"Error computing fingerprint");
  310. return -1;
  311. }
  312. tor_assert(strlen(options->Nickname) <= MAX_NICKNAME_LEN);
  313. if (tor_snprintf(fingerprint_line, sizeof(fingerprint_line),
  314. "%s %s\n",options->Nickname, fingerprint) < 0) {
  315. log_err(LD_GENERAL,"Error writing fingerprint line");
  316. return -1;
  317. }
  318. if (write_str_to_file(keydir, fingerprint_line, 0)) {
  319. log_err(LD_FS, "Error writing fingerprint line to file");
  320. return -1;
  321. }
  322. log(LOG_NOTICE, LD_GENERAL,
  323. "Your Tor server's identity key fingerprint is '%s %s'",
  324. options->Nickname, fingerprint);
  325. if (!authdir_mode(options))
  326. return 0;
  327. /* 6. [authdirserver only] load approved-routers file */
  328. tor_snprintf(keydir,sizeof(keydir),"%s/approved-routers", datadir);
  329. log_info(LD_DIRSERV,"Loading approved fingerprints from \"%s\"...",keydir);
  330. if (dirserv_parse_fingerprint_file(keydir) < 0) {
  331. log_err(LD_GENERAL,"Error loading fingerprints");
  332. return -1;
  333. }
  334. /* 6b. [authdirserver only] add own key to approved directories. */
  335. crypto_pk_get_digest(get_identity_key(), digest);
  336. if (!router_digest_is_trusted_dir(digest)) {
  337. add_trusted_dir_server(options->Nickname, NULL,
  338. (uint16_t)options->DirPort, digest,
  339. options->V1AuthoritativeDir);
  340. }
  341. return 0; /* success */
  342. }
  343. /* Keep track of whether we should upload our server descriptor,
  344. * and what type of server we are.
  345. */
  346. /** Whether we can reach our ORPort from the outside. */
  347. static int can_reach_or_port = 0;
  348. /** Whether we can reach our DirPort from the outside. */
  349. static int can_reach_dir_port = 0;
  350. /** Return 1 if ORPort is known reachable; else return 0. */
  351. int
  352. check_whether_orport_reachable(void)
  353. {
  354. or_options_t *options = get_options();
  355. return options->AssumeReachable ||
  356. can_reach_or_port;
  357. }
  358. /** Return 1 if we don't have a dirport configured, or if it's reachable. */
  359. int
  360. check_whether_dirport_reachable(void)
  361. {
  362. or_options_t *options = get_options();
  363. return !options->DirPort ||
  364. options->AssumeReachable ||
  365. we_are_hibernating() ||
  366. can_reach_dir_port;
  367. }
  368. /** Look at a variety of factors, and return 0 if we don't want to
  369. * advertise the fact that we have a DirPort open. Else return the
  370. * DirPort we want to advertise. */
  371. static int
  372. decide_to_advertise_dirport(or_options_t *options, routerinfo_t *router)
  373. {
  374. if (!router->dir_port) /* short circuit the rest of the function */
  375. return 0;
  376. if (authdir_mode(options)) /* always publish */
  377. return router->dir_port;
  378. if (we_are_hibernating())
  379. return 0;
  380. if (!check_whether_dirport_reachable())
  381. return 0;
  382. if (router->bandwidthcapacity >= router->bandwidthrate) {
  383. /* check if we might potentially hibernate. */
  384. if (options->AccountingMax != 0)
  385. return 0;
  386. /* also check if we're advertising a small amount, and have
  387. a "boring" DirPort. */
  388. if (router->bandwidthrate < 50000 && router->dir_port > 1024)
  389. return 0;
  390. }
  391. /* Sounds like a great idea. Let's publish it. */
  392. return router->dir_port;
  393. }
  394. /** Some time has passed, or we just got new directory information.
  395. * See if we currently believe our ORPort or DirPort to be
  396. * unreachable. If so, launch a new test for it.
  397. *
  398. * For ORPort, we simply try making a circuit that ends at ourselves.
  399. * Success is noticed in onionskin_answer().
  400. *
  401. * For DirPort, we make a connection via Tor to our DirPort and ask
  402. * for our own server descriptor.
  403. * Success is noticed in connection_dir_client_reached_eof().
  404. */
  405. void
  406. consider_testing_reachability(void)
  407. {
  408. routerinfo_t *me = router_get_my_routerinfo();
  409. if (!me)
  410. return;
  411. if (!check_whether_orport_reachable()) {
  412. log_info(LD_CIRC, "Testing reachability of my ORPort: %s:%d.",
  413. me->address, me->or_port);
  414. circuit_launch_by_router(CIRCUIT_PURPOSE_TESTING, me, 0, 1, 1);
  415. }
  416. if (!check_whether_dirport_reachable()) {
  417. /* ask myself, via tor, for my server descriptor. */
  418. directory_initiate_command_router(me, DIR_PURPOSE_FETCH_SERVERDESC,
  419. 1, "authority", NULL, 0);
  420. }
  421. }
  422. /** Annotate that we found our ORPort reachable. */
  423. void
  424. router_orport_found_reachable(void)
  425. {
  426. if (!can_reach_or_port) {
  427. log_notice(LD_OR,"Self-testing indicates your ORPort is reachable from "
  428. "the outside. Excellent.%s",
  429. get_options()->PublishServerDescriptor ?
  430. " Publishing server descriptor." : "");
  431. can_reach_or_port = 1;
  432. mark_my_descriptor_dirty();
  433. }
  434. }
  435. /** Annotate that we found our DirPort reachable. */
  436. void
  437. router_dirport_found_reachable(void)
  438. {
  439. if (!can_reach_dir_port) {
  440. log_notice(LD_DIRSERV,"Self-testing indicates your DirPort is reachable "
  441. "from the outside. Excellent.");
  442. can_reach_dir_port = 1;
  443. }
  444. }
  445. /** Our router has just moved to a new IP. Reset stats. */
  446. void
  447. server_has_changed_ip(void)
  448. {
  449. stats_n_seconds_working = 0;
  450. can_reach_or_port = 0;
  451. can_reach_dir_port = 0;
  452. mark_my_descriptor_dirty();
  453. }
  454. /** Return true iff we believe ourselves to be an authoritative
  455. * directory server.
  456. */
  457. int
  458. authdir_mode(or_options_t *options)
  459. {
  460. return options->AuthoritativeDir != 0;
  461. }
  462. /** Return true iff we try to stay connected to all ORs at once.
  463. */
  464. int
  465. clique_mode(or_options_t *options)
  466. {
  467. return authdir_mode(options);
  468. }
  469. /** Return true iff we are trying to be a server.
  470. */
  471. int
  472. server_mode(or_options_t *options)
  473. {
  474. if (options->ClientOnly) return 0;
  475. return (options->ORPort != 0 || options->ORListenAddress);
  476. }
  477. /** Remember if we've advertised ourselves to the dirservers. */
  478. static int server_is_advertised=0;
  479. /** Return true iff we have published our descriptor lately.
  480. */
  481. int
  482. advertised_server_mode(void)
  483. {
  484. return server_is_advertised;
  485. }
  486. /**
  487. * Called with a boolean: set whether we have recently published our
  488. * descriptor.
  489. */
  490. static void
  491. set_server_advertised(int s)
  492. {
  493. server_is_advertised = s;
  494. }
  495. /** Return true iff we are trying to be a socks proxy. */
  496. int
  497. proxy_mode(or_options_t *options)
  498. {
  499. return (options->SocksPort != 0 || options->SocksListenAddress);
  500. }
  501. /** Decide if we're a publishable server. We are a publishable server if:
  502. * - We don't have the ClientOnly option set
  503. * and
  504. * - We have the PublishServerDescriptor option set
  505. * and
  506. * - We have ORPort set
  507. * and
  508. * - We believe we are reachable from the outside; or
  509. * - We have the AuthoritativeDirectory option set.
  510. */
  511. static int
  512. decide_if_publishable_server(void)
  513. {
  514. or_options_t *options = get_options();
  515. if (options->ClientOnly)
  516. return 0;
  517. if (!options->PublishServerDescriptor)
  518. return 0;
  519. if (!server_mode(options))
  520. return 0;
  521. if (options->AuthoritativeDir)
  522. return 1;
  523. return check_whether_orport_reachable();
  524. }
  525. /** Initiate server descriptor upload as reasonable (if server is publishable,
  526. * etc). <b>force</b> is as for router_upload_dir_desc_to_dirservers.
  527. *
  528. * We need to rebuild the descriptor if it's dirty even if we're not
  529. * uploading, because our reachability testing *uses* our descriptor to
  530. * determine what IP address and ports to test.
  531. */
  532. void
  533. consider_publishable_server(int force)
  534. {
  535. int rebuilt;
  536. if (!server_mode(get_options()))
  537. return;
  538. rebuilt = router_rebuild_descriptor(0);
  539. if (decide_if_publishable_server()) {
  540. set_server_advertised(1);
  541. if (rebuilt == 0)
  542. router_upload_dir_desc_to_dirservers(force);
  543. } else {
  544. set_server_advertised(0);
  545. }
  546. }
  547. /*
  548. * Clique maintenance -- to be phased out.
  549. */
  550. /** Return true iff this OR should try to keep connections open to all
  551. * other ORs. */
  552. int
  553. router_is_clique_mode(routerinfo_t *router)
  554. {
  555. if (router_digest_is_trusted_dir(router->cache_info.identity_digest))
  556. return 1;
  557. return 0;
  558. }
  559. /*
  560. * OR descriptor generation.
  561. */
  562. /** My routerinfo. */
  563. static routerinfo_t *desc_routerinfo = NULL;
  564. /** Since when has our descriptor been "clean"? 0 if we need to regenerate it
  565. * now. */
  566. static time_t desc_clean_since = 0;
  567. /** Boolean: do we need to regenerate the above? */
  568. static int desc_needs_upload = 0;
  569. /** OR only: If <b>force</b> is true, or we haven't uploaded this
  570. * descriptor successfully yet, try to upload our signed descriptor to
  571. * all the directory servers we know about.
  572. */
  573. void
  574. router_upload_dir_desc_to_dirservers(int force)
  575. {
  576. const char *s;
  577. s = router_get_my_descriptor();
  578. if (!s) {
  579. log_info(LD_GENERAL, "No descriptor; skipping upload");
  580. return;
  581. }
  582. if (!get_options()->PublishServerDescriptor)
  583. return;
  584. if (!force && !desc_needs_upload)
  585. return;
  586. desc_needs_upload = 0;
  587. directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_DIR, s, strlen(s));
  588. }
  589. /** OR only: Check whether my exit policy says to allow connection to
  590. * conn. Return 0 if we accept; non-0 if we reject.
  591. */
  592. int
  593. router_compare_to_my_exit_policy(connection_t *conn)
  594. {
  595. tor_assert(desc_routerinfo);
  596. /* make sure it's resolved to something. this way we can't get a
  597. 'maybe' below. */
  598. if (!conn->addr)
  599. return -1;
  600. return compare_addr_to_addr_policy(conn->addr, conn->port,
  601. desc_routerinfo->exit_policy) != ADDR_POLICY_ACCEPTED;
  602. }
  603. /** Return true iff I'm a server and <b>digest</b> is equal to
  604. * my identity digest. */
  605. int
  606. router_digest_is_me(const char *digest)
  607. {
  608. routerinfo_t *me = router_get_my_routerinfo();
  609. if (!me || memcmp(me->cache_info.identity_digest, digest, DIGEST_LEN))
  610. return 0;
  611. return 1;
  612. }
  613. /** A wrapper around router_digest_is_me(). */
  614. int
  615. router_is_me(routerinfo_t *router)
  616. {
  617. return router_digest_is_me(router->cache_info.identity_digest);
  618. }
  619. /** Return true iff <b>fp</b> is a hex fingerprint of my identity digest. */
  620. int
  621. router_fingerprint_is_me(const char *fp)
  622. {
  623. char digest[DIGEST_LEN];
  624. if (strlen(fp) == HEX_DIGEST_LEN &&
  625. base16_decode(digest, sizeof(digest), fp, HEX_DIGEST_LEN) == 0)
  626. return router_digest_is_me(digest);
  627. return 0;
  628. }
  629. /** Return a routerinfo for this OR, rebuilding a fresh one if
  630. * necessary. Return NULL on error, or if called on an OP. */
  631. routerinfo_t *
  632. router_get_my_routerinfo(void)
  633. {
  634. if (!server_mode(get_options()))
  635. return NULL;
  636. if (!desc_routerinfo || !desc_clean_since)
  637. if (router_rebuild_descriptor(!desc_routerinfo))
  638. return NULL;
  639. return desc_routerinfo;
  640. }
  641. /** OR only: Return a signed server descriptor for this OR, rebuilding a fresh
  642. * one if necessary. Return NULL on error.
  643. */
  644. const char *
  645. router_get_my_descriptor(void)
  646. {
  647. const char *body;
  648. if (!router_get_my_routerinfo())
  649. return NULL;
  650. /* Make sure this is nul-terminated. */
  651. tor_assert(desc_routerinfo->cache_info.saved_location == SAVED_NOWHERE);
  652. body = signed_descriptor_get_body(&desc_routerinfo->cache_info);
  653. tor_assert(!body[desc_routerinfo->cache_info.signed_descriptor_len]);
  654. log_debug(LD_GENERAL,"my desc is '%s'", body);
  655. return body;
  656. }
  657. /*DOCDOC*/
  658. static smartlist_t *warned_nonexistent_family = NULL;
  659. static int router_guess_address_from_dir_headers(uint32_t *guess);
  660. /** If <b>force</b> is true, or our descriptor is out-of-date, rebuild
  661. * a fresh routerinfo and signed server descriptor for this OR.
  662. * Return 0 on success, -1 on temporary error.
  663. */
  664. int
  665. router_rebuild_descriptor(int force)
  666. {
  667. routerinfo_t *ri;
  668. uint32_t addr;
  669. char platform[256];
  670. int hibernating = we_are_hibernating();
  671. or_options_t *options = get_options();
  672. if (desc_clean_since && !force)
  673. return 0;
  674. if (resolve_my_address(LOG_INFO, options, &addr, NULL) < 0) {
  675. log_info(LD_CONFIG, "Could not determine our address locally. "
  676. "Checking if directory headers provide any hints.");
  677. if (router_guess_address_from_dir_headers(&addr) < 0) {
  678. log_info(LD_CONFIG, "No hints from directory headers either. "
  679. "Will try again later.");
  680. /* Stop trying to rebuild our descriptor every second. We'll
  681. * learn that it's time to try again when server_has_changed_ip()
  682. * marks it dirty. */
  683. desc_clean_since = time(NULL);
  684. return -1;
  685. }
  686. }
  687. ri = tor_malloc_zero(sizeof(routerinfo_t));
  688. ri->address = tor_dup_addr(addr);
  689. ri->nickname = tor_strdup(options->Nickname);
  690. ri->addr = addr;
  691. ri->or_port = options->ORPort;
  692. ri->dir_port = options->DirPort;
  693. ri->cache_info.published_on = time(NULL);
  694. ri->onion_pkey = crypto_pk_dup_key(get_onion_key()); /* must invoke from
  695. * main thread */
  696. ri->identity_pkey = crypto_pk_dup_key(get_identity_key());
  697. if (crypto_pk_get_digest(ri->identity_pkey,
  698. ri->cache_info.identity_digest)<0) {
  699. routerinfo_free(ri);
  700. return -1;
  701. }
  702. get_platform_str(platform, sizeof(platform));
  703. ri->platform = tor_strdup(platform);
  704. ri->bandwidthrate = (int)options->BandwidthRate;
  705. ri->bandwidthburst = (int)options->BandwidthBurst;
  706. ri->bandwidthcapacity = hibernating ? 0 : rep_hist_bandwidth_assess();
  707. if (options->BandwidthRate > options->MaxAdvertisedBandwidth)
  708. ri->bandwidthrate = (int)options->MaxAdvertisedBandwidth;
  709. policies_parse_exit_policy(options->ExitPolicy, &ri->exit_policy,
  710. options->ExitPolicyRejectPrivate);
  711. if (desc_routerinfo) { /* inherit values */
  712. ri->is_valid = desc_routerinfo->is_valid;
  713. ri->is_running = desc_routerinfo->is_running;
  714. ri->is_named = desc_routerinfo->is_named;
  715. }
  716. if (authdir_mode(options))
  717. ri->is_valid = ri->is_named = 1; /* believe in yourself */
  718. if (options->MyFamily) {
  719. smartlist_t *family;
  720. if (!warned_nonexistent_family)
  721. warned_nonexistent_family = smartlist_create();
  722. family = smartlist_create();
  723. ri->declared_family = smartlist_create();
  724. smartlist_split_string(family, options->MyFamily, ",",
  725. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  726. SMARTLIST_FOREACH(family, char *, name,
  727. {
  728. routerinfo_t *member;
  729. if (!strcasecmp(name, options->Nickname))
  730. member = ri;
  731. else
  732. member = router_get_by_nickname(name, 1);
  733. if (!member) {
  734. if (!smartlist_string_isin(warned_nonexistent_family, name) &&
  735. !is_legal_hexdigest(name)) {
  736. log_warn(LD_CONFIG,
  737. "I have no descriptor for the router named \"%s\" "
  738. "in my declared family; I'll use the nickname as is, but "
  739. "this may confuse clients.", name);
  740. smartlist_add(warned_nonexistent_family, tor_strdup(name));
  741. }
  742. smartlist_add(ri->declared_family, name);
  743. name = NULL;
  744. } else {
  745. char *fp = tor_malloc(HEX_DIGEST_LEN+2);
  746. fp[0] = '$';
  747. base16_encode(fp+1,HEX_DIGEST_LEN+1,
  748. member->cache_info.identity_digest, DIGEST_LEN);
  749. smartlist_add(ri->declared_family, fp);
  750. if (smartlist_string_isin(warned_nonexistent_family, name))
  751. smartlist_string_remove(warned_nonexistent_family, name);
  752. }
  753. tor_free(name);
  754. });
  755. smartlist_free(family);
  756. }
  757. ri->cache_info.signed_descriptor_body = tor_malloc(8192);
  758. if (router_dump_router_to_string(ri->cache_info.signed_descriptor_body, 8192,
  759. ri, get_identity_key())<0) {
  760. log_warn(LD_BUG, "Couldn't allocate string for descriptor.");
  761. return -1;
  762. }
  763. ri->cache_info.signed_descriptor_len =
  764. strlen(ri->cache_info.signed_descriptor_body);
  765. crypto_digest(ri->cache_info.signed_descriptor_digest,
  766. ri->cache_info.signed_descriptor_body,
  767. ri->cache_info.signed_descriptor_len);
  768. if (desc_routerinfo)
  769. routerinfo_free(desc_routerinfo);
  770. desc_routerinfo = ri;
  771. desc_clean_since = time(NULL);
  772. desc_needs_upload = 1;
  773. return 0;
  774. }
  775. /** Mark descriptor out of date if it's older than <b>when</b> */
  776. void
  777. mark_my_descriptor_dirty_if_older_than(time_t when)
  778. {
  779. if (desc_clean_since < when)
  780. mark_my_descriptor_dirty();
  781. }
  782. /** Call when the current descriptor is out of date. */
  783. void
  784. mark_my_descriptor_dirty(void)
  785. {
  786. desc_clean_since = 0;
  787. }
  788. /** How frequently will we republish our descriptor because of large (factor
  789. * of 2) shifts in estimated bandwidth? */
  790. #define MAX_BANDWIDTH_CHANGE_FREQ (20*60)
  791. /** Check whether bandwidth has changed a lot since the last time we announced
  792. * bandwidth. If so, mark our descriptor dirty. */
  793. void
  794. check_descriptor_bandwidth_changed(time_t now)
  795. {
  796. static time_t last_changed = 0;
  797. uint64_t prev, cur;
  798. if (!desc_routerinfo)
  799. return;
  800. prev = desc_routerinfo->bandwidthcapacity;
  801. cur = we_are_hibernating() ? 0 : rep_hist_bandwidth_assess();
  802. if ((prev != cur && (!prev || !cur)) ||
  803. cur > prev*2 ||
  804. cur < prev/2) {
  805. if (last_changed+MAX_BANDWIDTH_CHANGE_FREQ < now) {
  806. log_info(LD_GENERAL,
  807. "Measured bandwidth has changed; rebuilding descriptor.");
  808. mark_my_descriptor_dirty();
  809. last_changed = now;
  810. }
  811. }
  812. }
  813. static void
  814. log_addr_has_changed(int severity, uint32_t prev, uint32_t cur)
  815. {
  816. char addrbuf_prev[INET_NTOA_BUF_LEN];
  817. char addrbuf_cur[INET_NTOA_BUF_LEN];
  818. struct in_addr in_prev;
  819. struct in_addr in_cur;
  820. in_prev.s_addr = htonl(prev);
  821. tor_inet_ntoa(&in_prev, addrbuf_prev, sizeof(addrbuf_prev));
  822. in_cur.s_addr = htonl(cur);
  823. tor_inet_ntoa(&in_cur, addrbuf_cur, sizeof(addrbuf_cur));
  824. if (prev)
  825. log_fn(severity, LD_GENERAL,
  826. "Our IP Address has changed from %s to %s; "
  827. "rebuilding descriptor.",
  828. addrbuf_prev, addrbuf_cur);
  829. else
  830. log_notice(LD_GENERAL,
  831. "Guessed our IP address as %s.",
  832. addrbuf_cur);
  833. }
  834. /** Check whether our own address as defined by the Address configuration
  835. * has changed. This is for routers that get their address from a service
  836. * like dyndns. If our address has changed, mark our descriptor dirty. */
  837. void
  838. check_descriptor_ipaddress_changed(time_t now)
  839. {
  840. uint32_t prev, cur;
  841. or_options_t *options = get_options();
  842. (void) now;
  843. if (!desc_routerinfo)
  844. return;
  845. prev = desc_routerinfo->addr;
  846. if (resolve_my_address(LOG_INFO, options, &cur, NULL) < 0) {
  847. log_info(LD_CONFIG,"options->Address didn't resolve into an IP.");
  848. return;
  849. }
  850. if (prev != cur) {
  851. log_addr_has_changed(LOG_INFO, prev, cur);
  852. mark_my_descriptor_dirty();
  853. /* the above call is probably redundant, since resolve_my_address()
  854. * probably already noticed and marked it dirty. */
  855. }
  856. }
  857. static uint32_t last_guessed_ip = 0;
  858. /** A directory authority told us our IP address is <b>suggestion</b>.
  859. * If this address is different from the one we think we are now, and
  860. * if our computer doesn't actually know its IP address, then switch. */
  861. void
  862. router_new_address_suggestion(const char *suggestion)
  863. {
  864. uint32_t addr, cur;
  865. struct in_addr in;
  866. /* first, learn what the IP address actually is */
  867. if (!tor_inet_aton(suggestion, &in)) {
  868. log_debug(LD_DIR, "Malformed X-Your-Address-Is header. Ignoring.");
  869. return;
  870. }
  871. addr = ntohl(in.s_addr);
  872. if (resolve_my_address(LOG_INFO, get_options(), &cur, NULL) >= 0) {
  873. /* We're all set -- we already know our address. Great. */
  874. last_guessed_ip = cur; /* store it in case we need it later */
  875. return;
  876. }
  877. if (last_guessed_ip != addr) {
  878. log_addr_has_changed(LOG_NOTICE, last_guessed_ip, addr);
  879. server_has_changed_ip();
  880. last_guessed_ip = addr; /* router_rebuild_descriptor() will fetch it */
  881. }
  882. }
  883. /** We failed to resolve our address locally, but we'd like to build
  884. * a descriptor and publish / test reachability. If we have a guess
  885. * about our address based on directory headers, answer it and return
  886. * 0; else return -1. */
  887. static int
  888. router_guess_address_from_dir_headers(uint32_t *guess)
  889. {
  890. if (last_guessed_ip) {
  891. *guess = last_guessed_ip;
  892. return 0;
  893. }
  894. return -1;
  895. }
  896. /** Set <b>platform</b> (max length <b>len</b>) to a NUL-terminated short
  897. * string describing the version of Tor and the operating system we're
  898. * currently running on.
  899. */
  900. void
  901. get_platform_str(char *platform, size_t len)
  902. {
  903. tor_snprintf(platform, len, "Tor %s on %s",
  904. VERSION, get_uname());
  905. return;
  906. }
  907. /* XXX need to audit this thing and count fenceposts. maybe
  908. * refactor so we don't have to keep asking if we're
  909. * near the end of maxlen?
  910. */
  911. #define DEBUG_ROUTER_DUMP_ROUTER_TO_STRING
  912. /** OR only: Given a routerinfo for this router, and an identity key to sign
  913. * with, encode the routerinfo as a signed server descriptor and write the
  914. * result into <b>s</b>, using at most <b>maxlen</b> bytes. Return -1 on
  915. * failure, and the number of bytes used on success.
  916. */
  917. int
  918. router_dump_router_to_string(char *s, size_t maxlen, routerinfo_t *router,
  919. crypto_pk_env_t *ident_key)
  920. {
  921. char *onion_pkey; /* Onion key, PEM-encoded. */
  922. char *identity_pkey; /* Identity key, PEM-encoded. */
  923. char digest[DIGEST_LEN];
  924. char published[ISO_TIME_LEN+1];
  925. char fingerprint[FINGERPRINT_LEN+1];
  926. struct in_addr in;
  927. char addrbuf[INET_NTOA_BUF_LEN];
  928. size_t onion_pkeylen, identity_pkeylen;
  929. size_t written;
  930. int result=0;
  931. addr_policy_t *tmpe;
  932. char *bandwidth_usage;
  933. char *family_line;
  934. #ifdef DEBUG_ROUTER_DUMP_ROUTER_TO_STRING
  935. char *s_tmp, *s_dup;
  936. const char *cp;
  937. routerinfo_t *ri_tmp;
  938. #endif
  939. or_options_t *options = get_options();
  940. /* Make sure the identity key matches the one in the routerinfo. */
  941. if (crypto_pk_cmp_keys(ident_key, router->identity_pkey)) {
  942. log_warn(LD_BUG,"Tried to sign a router with a private key that didn't "
  943. "match router's public key!");
  944. return -1;
  945. }
  946. /* record our fingerprint, so we can include it in the descriptor */
  947. if (crypto_pk_get_fingerprint(router->identity_pkey, fingerprint, 1)<0) {
  948. log_err(LD_BUG,"Error computing fingerprint");
  949. return -1;
  950. }
  951. /* PEM-encode the onion key */
  952. if (crypto_pk_write_public_key_to_string(router->onion_pkey,
  953. &onion_pkey,&onion_pkeylen)<0) {
  954. log_warn(LD_BUG,"write onion_pkey to string failed!");
  955. return -1;
  956. }
  957. /* PEM-encode the identity key key */
  958. if (crypto_pk_write_public_key_to_string(router->identity_pkey,
  959. &identity_pkey,&identity_pkeylen)<0) {
  960. log_warn(LD_BUG,"write identity_pkey to string failed!");
  961. tor_free(onion_pkey);
  962. return -1;
  963. }
  964. /* Encode the publication time. */
  965. format_iso_time(published, router->cache_info.published_on);
  966. /* How busy have we been? */
  967. bandwidth_usage = rep_hist_get_bandwidth_lines();
  968. if (router->declared_family && smartlist_len(router->declared_family)) {
  969. size_t n;
  970. char *s = smartlist_join_strings(router->declared_family, " ", 0, &n);
  971. n += strlen("family ") + 2; /* 1 for \n, 1 for \0. */
  972. family_line = tor_malloc(n);
  973. tor_snprintf(family_line, n, "family %s\n", s);
  974. tor_free(s);
  975. } else {
  976. family_line = tor_strdup("");
  977. }
  978. /* Generate the easy portion of the router descriptor. */
  979. result = tor_snprintf(s, maxlen,
  980. "router %s %s %d 0 %d\n"
  981. "platform %s\n"
  982. "published %s\n"
  983. "opt fingerprint %s\n"
  984. "uptime %ld\n"
  985. "bandwidth %d %d %d\n"
  986. "onion-key\n%s"
  987. "signing-key\n%s%s%s%s",
  988. router->nickname,
  989. router->address,
  990. router->or_port,
  991. decide_to_advertise_dirport(options, router),
  992. router->platform,
  993. published,
  994. fingerprint,
  995. stats_n_seconds_working,
  996. (int) router->bandwidthrate,
  997. (int) router->bandwidthburst,
  998. (int) router->bandwidthcapacity,
  999. onion_pkey, identity_pkey,
  1000. family_line, bandwidth_usage,
  1001. we_are_hibernating() ? "opt hibernating 1\n" : "");
  1002. tor_free(family_line);
  1003. tor_free(onion_pkey);
  1004. tor_free(identity_pkey);
  1005. tor_free(bandwidth_usage);
  1006. if (result < 0)
  1007. return -1;
  1008. /* From now on, we use 'written' to remember the current length of 's'. */
  1009. written = result;
  1010. if (options->ContactInfo && strlen(options->ContactInfo)) {
  1011. result = tor_snprintf(s+written,maxlen-written, "contact %s\n",
  1012. options->ContactInfo);
  1013. if (result<0)
  1014. return -1;
  1015. written += result;
  1016. }
  1017. /* Write the exit policy to the end of 's'. */
  1018. for (tmpe=router->exit_policy; tmpe; tmpe=tmpe->next) {
  1019. /* Write: "accept 1.2.3.4" */
  1020. in.s_addr = htonl(tmpe->addr);
  1021. tor_inet_ntoa(&in, addrbuf, sizeof(addrbuf));
  1022. result = tor_snprintf(s+written, maxlen-written, "%s %s",
  1023. tmpe->policy_type == ADDR_POLICY_ACCEPT ? "accept" : "reject",
  1024. tmpe->msk == 0 ? "*" : addrbuf);
  1025. if (result < 0)
  1026. return -1;
  1027. written += result;
  1028. if (tmpe->msk != 0xFFFFFFFFu && tmpe->msk != 0) {
  1029. int n_bits = addr_mask_get_bits(tmpe->msk);
  1030. if (n_bits >= 0) {
  1031. if (tor_snprintf(s+written, maxlen-written, "/%d", n_bits)<0)
  1032. return -1;
  1033. } else {
  1034. /* Write "/255.255.0.0" */
  1035. in.s_addr = htonl(tmpe->msk);
  1036. tor_inet_ntoa(&in, addrbuf, sizeof(addrbuf));
  1037. if (tor_snprintf(s+written, maxlen-written, "/%s", addrbuf)<0)
  1038. return -1;
  1039. }
  1040. written += strlen(s+written);
  1041. }
  1042. if (tmpe->prt_min <= 1 && tmpe->prt_max == 65535) {
  1043. /* There is no port set; write ":*" */
  1044. if (written+4 > maxlen)
  1045. return -1;
  1046. strlcat(s+written, ":*\n", maxlen-written);
  1047. written += 3;
  1048. } else if (tmpe->prt_min == tmpe->prt_max) {
  1049. /* There is only one port; write ":80". */
  1050. result = tor_snprintf(s+written, maxlen-written, ":%d\n", tmpe->prt_min);
  1051. if (result<0)
  1052. return -1;
  1053. written += result;
  1054. } else {
  1055. /* There is a range of ports; write ":79-80". */
  1056. result = tor_snprintf(s+written, maxlen-written, ":%d-%d\n",
  1057. tmpe->prt_min, tmpe->prt_max);
  1058. if (result<0)
  1059. return -1;
  1060. written += result;
  1061. }
  1062. } /* end for */
  1063. if (written+256 > maxlen) /* Not enough room for signature. */
  1064. return -1;
  1065. /* Sign the directory */
  1066. strlcat(s+written, "router-signature\n", maxlen-written);
  1067. written += strlen(s+written);
  1068. s[written] = '\0';
  1069. if (router_get_router_hash(s, digest) < 0)
  1070. return -1;
  1071. if (router_append_dirobj_signature(s+written,maxlen-written,
  1072. digest,ident_key)<0) {
  1073. log_warn(LD_BUG, "Couldn't sign router descriptor");
  1074. return -1;
  1075. }
  1076. written += strlen(s+written);
  1077. if (written+2 > maxlen)
  1078. return -1;
  1079. /* include a last '\n' */
  1080. s[written] = '\n';
  1081. s[written+1] = 0;
  1082. #ifdef DEBUG_ROUTER_DUMP_ROUTER_TO_STRING
  1083. cp = s_tmp = s_dup = tor_strdup(s);
  1084. ri_tmp = router_parse_entry_from_string(cp, NULL, 1);
  1085. if (!ri_tmp) {
  1086. log_err(LD_BUG,
  1087. "We just generated a router descriptor we can't parse: <<%s>>",
  1088. s);
  1089. return -1;
  1090. }
  1091. tor_free(s_dup);
  1092. routerinfo_free(ri_tmp);
  1093. #endif
  1094. return written+1;
  1095. }
  1096. /** Return true iff <b>s</b> is a legally valid server nickname. */
  1097. int
  1098. is_legal_nickname(const char *s)
  1099. {
  1100. size_t len;
  1101. tor_assert(s);
  1102. len = strlen(s);
  1103. return len > 0 && len <= MAX_NICKNAME_LEN &&
  1104. strspn(s,LEGAL_NICKNAME_CHARACTERS) == len;
  1105. }
  1106. /** Return true iff <b>s</b> is a legally valid server nickname or
  1107. * hex-encoded identity-key digest. */
  1108. int
  1109. is_legal_nickname_or_hexdigest(const char *s)
  1110. {
  1111. if (*s!='$')
  1112. return is_legal_nickname(s);
  1113. else
  1114. return is_legal_hexdigest(s);
  1115. }
  1116. /** Return true iff <b>s</b> is a legally valid hex-encoded identity-key
  1117. * digest. */
  1118. int
  1119. is_legal_hexdigest(const char *s)
  1120. {
  1121. size_t len;
  1122. tor_assert(s);
  1123. if (s[0] == '$') s++;
  1124. len = strlen(s);
  1125. return (len == HEX_DIGEST_LEN &&
  1126. strspn(s,HEX_CHARACTERS)==len);
  1127. }
  1128. /** Forget that we have issued any router-related warnings, so that we'll
  1129. * warn again if we see the same errors. */
  1130. void
  1131. router_reset_warnings(void)
  1132. {
  1133. if (warned_nonexistent_family) {
  1134. SMARTLIST_FOREACH(warned_nonexistent_family, char *, cp, tor_free(cp));
  1135. smartlist_clear(warned_nonexistent_family);
  1136. }
  1137. }
  1138. /** Release all static resources held in router.c */
  1139. void
  1140. router_free_all(void)
  1141. {
  1142. if (onionkey)
  1143. crypto_free_pk_env(onionkey);
  1144. if (lastonionkey)
  1145. crypto_free_pk_env(lastonionkey);
  1146. if (identitykey)
  1147. crypto_free_pk_env(identitykey);
  1148. if (key_lock)
  1149. tor_mutex_free(key_lock);
  1150. if (desc_routerinfo)
  1151. routerinfo_free(desc_routerinfo);
  1152. if (warned_nonexistent_family) {
  1153. SMARTLIST_FOREACH(warned_nonexistent_family, char *, cp, tor_free(cp));
  1154. smartlist_free(warned_nonexistent_family);
  1155. }
  1156. }