router.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280
  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. consider_publishable_server(1);
  434. }
  435. }
  436. /** Annotate that we found our DirPort reachable. */
  437. void
  438. router_dirport_found_reachable(void)
  439. {
  440. if (!can_reach_dir_port) {
  441. log_notice(LD_DIRSERV,"Self-testing indicates your DirPort is reachable "
  442. "from the outside. Excellent.");
  443. can_reach_dir_port = 1;
  444. }
  445. }
  446. /** Our router has just moved to a new IP. Reset stats. */
  447. void
  448. server_has_changed_ip(void)
  449. {
  450. stats_n_seconds_working = 0;
  451. can_reach_or_port = 0;
  452. can_reach_dir_port = 0;
  453. mark_my_descriptor_dirty();
  454. }
  455. /** Return true iff we believe ourselves to be an authoritative
  456. * directory server.
  457. */
  458. int
  459. authdir_mode(or_options_t *options)
  460. {
  461. return options->AuthoritativeDir != 0;
  462. }
  463. /** Return true iff we try to stay connected to all ORs at once.
  464. */
  465. int
  466. clique_mode(or_options_t *options)
  467. {
  468. return authdir_mode(options);
  469. }
  470. /** Return true iff we are trying to be a server.
  471. */
  472. int
  473. server_mode(or_options_t *options)
  474. {
  475. if (options->ClientOnly) return 0;
  476. return (options->ORPort != 0 || options->ORListenAddress);
  477. }
  478. /** Remember if we've advertised ourselves to the dirservers. */
  479. static int server_is_advertised=0;
  480. /** Return true iff we have published our descriptor lately.
  481. */
  482. int
  483. advertised_server_mode(void)
  484. {
  485. return server_is_advertised;
  486. }
  487. /**
  488. * Called with a boolean: set whether we have recently published our
  489. * descriptor.
  490. */
  491. static void
  492. set_server_advertised(int s)
  493. {
  494. server_is_advertised = s;
  495. }
  496. /** Return true iff we are trying to be a socks proxy. */
  497. int
  498. proxy_mode(or_options_t *options)
  499. {
  500. return (options->SocksPort != 0 || options->SocksListenAddress);
  501. }
  502. /** Decide if we're a publishable server. We are a publishable server if:
  503. * - We don't have the ClientOnly option set
  504. * and
  505. * - We have the PublishServerDescriptor option set
  506. * and
  507. * - We have ORPort set
  508. * and
  509. * - We believe we are reachable from the outside; or
  510. * - We have the AuthoritativeDirectory option set.
  511. */
  512. static int
  513. decide_if_publishable_server(void)
  514. {
  515. or_options_t *options = get_options();
  516. if (options->ClientOnly)
  517. return 0;
  518. if (!options->PublishServerDescriptor)
  519. return 0;
  520. if (!server_mode(options))
  521. return 0;
  522. if (options->AuthoritativeDir)
  523. return 1;
  524. return check_whether_orport_reachable();
  525. }
  526. /** Initiate server descriptor upload as reasonable (if server is publishable,
  527. * etc). <b>force</b> is as for router_upload_dir_desc_to_dirservers.
  528. *
  529. * We need to rebuild the descriptor if it's dirty even if we're not
  530. * uploading, because our reachability testing *uses* our descriptor to
  531. * determine what IP address and ports to test.
  532. */
  533. void
  534. consider_publishable_server(int force)
  535. {
  536. int rebuilt;
  537. if (!server_mode(get_options()))
  538. return;
  539. rebuilt = router_rebuild_descriptor(0);
  540. if (decide_if_publishable_server()) {
  541. set_server_advertised(1);
  542. if (rebuilt == 0)
  543. router_upload_dir_desc_to_dirservers(force);
  544. } else {
  545. set_server_advertised(0);
  546. }
  547. }
  548. /*
  549. * Clique maintenance -- to be phased out.
  550. */
  551. /** Return true iff this OR should try to keep connections open to all
  552. * other ORs. */
  553. int
  554. router_is_clique_mode(routerinfo_t *router)
  555. {
  556. if (router_digest_is_trusted_dir(router->cache_info.identity_digest))
  557. return 1;
  558. return 0;
  559. }
  560. /*
  561. * OR descriptor generation.
  562. */
  563. /** My routerinfo. */
  564. static routerinfo_t *desc_routerinfo = NULL;
  565. /** Since when has our descriptor been "clean"? 0 if we need to regenerate it
  566. * now. */
  567. static time_t desc_clean_since = 0;
  568. /** Boolean: do we need to regenerate the above? */
  569. static int desc_needs_upload = 0;
  570. /** OR only: If <b>force</b> is true, or we haven't uploaded this
  571. * descriptor successfully yet, try to upload our signed descriptor to
  572. * all the directory servers we know about.
  573. */
  574. void
  575. router_upload_dir_desc_to_dirservers(int force)
  576. {
  577. const char *s;
  578. s = router_get_my_descriptor();
  579. if (!s) {
  580. log_info(LD_GENERAL, "No descriptor; skipping upload");
  581. return;
  582. }
  583. if (!get_options()->PublishServerDescriptor)
  584. return;
  585. if (!force && !desc_needs_upload)
  586. return;
  587. desc_needs_upload = 0;
  588. directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_DIR, s, strlen(s));
  589. }
  590. /** OR only: Check whether my exit policy says to allow connection to
  591. * conn. Return 0 if we accept; non-0 if we reject.
  592. */
  593. int
  594. router_compare_to_my_exit_policy(connection_t *conn)
  595. {
  596. tor_assert(desc_routerinfo);
  597. /* make sure it's resolved to something. this way we can't get a
  598. 'maybe' below. */
  599. if (!conn->addr)
  600. return -1;
  601. return compare_addr_to_addr_policy(conn->addr, conn->port,
  602. desc_routerinfo->exit_policy) != ADDR_POLICY_ACCEPTED;
  603. }
  604. /** Return true iff I'm a server and <b>digest</b> is equal to
  605. * my identity digest. */
  606. int
  607. router_digest_is_me(const char *digest)
  608. {
  609. routerinfo_t *me = router_get_my_routerinfo();
  610. if (!me || memcmp(me->cache_info.identity_digest, digest, DIGEST_LEN))
  611. return 0;
  612. return 1;
  613. }
  614. /** A wrapper around router_digest_is_me(). */
  615. int
  616. router_is_me(routerinfo_t *router)
  617. {
  618. return router_digest_is_me(router->cache_info.identity_digest);
  619. }
  620. /** Return true iff <b>fp</b> is a hex fingerprint of my identity digest. */
  621. int
  622. router_fingerprint_is_me(const char *fp)
  623. {
  624. char digest[DIGEST_LEN];
  625. if (strlen(fp) == HEX_DIGEST_LEN &&
  626. base16_decode(digest, sizeof(digest), fp, HEX_DIGEST_LEN) == 0)
  627. return router_digest_is_me(digest);
  628. return 0;
  629. }
  630. /** Return a routerinfo for this OR, rebuilding a fresh one if
  631. * necessary. Return NULL on error, or if called on an OP. */
  632. routerinfo_t *
  633. router_get_my_routerinfo(void)
  634. {
  635. if (!server_mode(get_options()))
  636. return NULL;
  637. if (!desc_routerinfo || !desc_clean_since)
  638. if (router_rebuild_descriptor(!desc_routerinfo))
  639. return NULL;
  640. return desc_routerinfo;
  641. }
  642. /** OR only: Return a signed server descriptor for this OR, rebuilding a fresh
  643. * one if necessary. Return NULL on error.
  644. */
  645. const char *
  646. router_get_my_descriptor(void)
  647. {
  648. const char *body;
  649. if (!router_get_my_routerinfo())
  650. return NULL;
  651. /* Make sure this is nul-terminated. */
  652. tor_assert(desc_routerinfo->cache_info.saved_location == SAVED_NOWHERE);
  653. body = signed_descriptor_get_body(&desc_routerinfo->cache_info);
  654. tor_assert(!body[desc_routerinfo->cache_info.signed_descriptor_len]);
  655. log_debug(LD_GENERAL,"my desc is '%s'", body);
  656. return body;
  657. }
  658. /*DOCDOC*/
  659. static smartlist_t *warned_nonexistent_family = NULL;
  660. static int router_guess_address_from_dir_headers(uint32_t *guess);
  661. /** If <b>force</b> is true, or our descriptor is out-of-date, rebuild
  662. * a fresh routerinfo and signed server descriptor for this OR.
  663. * Return 0 on success, -1 on temporary error.
  664. */
  665. int
  666. router_rebuild_descriptor(int force)
  667. {
  668. routerinfo_t *ri;
  669. uint32_t addr;
  670. char platform[256];
  671. int hibernating = we_are_hibernating();
  672. or_options_t *options = get_options();
  673. if (desc_clean_since && !force)
  674. return 0;
  675. if (resolve_my_address(LOG_INFO, options, &addr, NULL) < 0) {
  676. log_info(LD_CONFIG, "Could not determine our address locally. "
  677. "Checking if directory headers provide any hints.");
  678. if (router_guess_address_from_dir_headers(&addr) < 0) {
  679. log_info(LD_CONFIG, "No hints from directory headers either. "
  680. "Will try again later.");
  681. /* Stop trying to rebuild our descriptor every second. We'll
  682. * learn that it's time to try again when server_has_changed_ip()
  683. * marks it dirty. */
  684. desc_clean_since = time(NULL);
  685. return -1;
  686. }
  687. }
  688. ri = tor_malloc_zero(sizeof(routerinfo_t));
  689. ri->address = tor_dup_addr(addr);
  690. ri->nickname = tor_strdup(options->Nickname);
  691. ri->addr = addr;
  692. ri->or_port = options->ORPort;
  693. ri->dir_port = options->DirPort;
  694. ri->cache_info.published_on = time(NULL);
  695. ri->onion_pkey = crypto_pk_dup_key(get_onion_key()); /* must invoke from
  696. * main thread */
  697. ri->identity_pkey = crypto_pk_dup_key(get_identity_key());
  698. if (crypto_pk_get_digest(ri->identity_pkey,
  699. ri->cache_info.identity_digest)<0) {
  700. routerinfo_free(ri);
  701. return -1;
  702. }
  703. get_platform_str(platform, sizeof(platform));
  704. ri->platform = tor_strdup(platform);
  705. ri->bandwidthrate = (int)options->BandwidthRate;
  706. ri->bandwidthburst = (int)options->BandwidthBurst;
  707. ri->bandwidthcapacity = hibernating ? 0 : rep_hist_bandwidth_assess();
  708. if (options->BandwidthRate > options->MaxAdvertisedBandwidth)
  709. ri->bandwidthrate = (int)options->MaxAdvertisedBandwidth;
  710. policies_parse_exit_policy(options->ExitPolicy, &ri->exit_policy,
  711. options->ExitPolicyRejectPrivate);
  712. if (desc_routerinfo) { /* inherit values */
  713. ri->is_valid = desc_routerinfo->is_valid;
  714. ri->is_running = desc_routerinfo->is_running;
  715. ri->is_named = desc_routerinfo->is_named;
  716. }
  717. if (authdir_mode(options))
  718. ri->is_valid = ri->is_named = 1; /* believe in yourself */
  719. if (options->MyFamily) {
  720. smartlist_t *family;
  721. if (!warned_nonexistent_family)
  722. warned_nonexistent_family = smartlist_create();
  723. family = smartlist_create();
  724. ri->declared_family = smartlist_create();
  725. smartlist_split_string(family, options->MyFamily, ",",
  726. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  727. SMARTLIST_FOREACH(family, char *, name,
  728. {
  729. routerinfo_t *member;
  730. if (!strcasecmp(name, options->Nickname))
  731. member = ri;
  732. else
  733. member = router_get_by_nickname(name, 1);
  734. if (!member) {
  735. if (!smartlist_string_isin(warned_nonexistent_family, name) &&
  736. !is_legal_hexdigest(name)) {
  737. log_warn(LD_CONFIG,
  738. "I have no descriptor for the router named \"%s\" "
  739. "in my declared family; I'll use the nickname as is, but "
  740. "this may confuse clients.", name);
  741. smartlist_add(warned_nonexistent_family, tor_strdup(name));
  742. }
  743. smartlist_add(ri->declared_family, name);
  744. name = NULL;
  745. } else {
  746. char *fp = tor_malloc(HEX_DIGEST_LEN+2);
  747. fp[0] = '$';
  748. base16_encode(fp+1,HEX_DIGEST_LEN+1,
  749. member->cache_info.identity_digest, DIGEST_LEN);
  750. smartlist_add(ri->declared_family, fp);
  751. if (smartlist_string_isin(warned_nonexistent_family, name))
  752. smartlist_string_remove(warned_nonexistent_family, name);
  753. }
  754. tor_free(name);
  755. });
  756. smartlist_free(family);
  757. }
  758. ri->cache_info.signed_descriptor_body = tor_malloc(8192);
  759. if (router_dump_router_to_string(ri->cache_info.signed_descriptor_body, 8192,
  760. ri, get_identity_key())<0) {
  761. log_warn(LD_BUG, "Couldn't allocate string for descriptor.");
  762. return -1;
  763. }
  764. ri->cache_info.signed_descriptor_len =
  765. strlen(ri->cache_info.signed_descriptor_body);
  766. crypto_digest(ri->cache_info.signed_descriptor_digest,
  767. ri->cache_info.signed_descriptor_body,
  768. ri->cache_info.signed_descriptor_len);
  769. if (desc_routerinfo)
  770. routerinfo_free(desc_routerinfo);
  771. desc_routerinfo = ri;
  772. desc_clean_since = time(NULL);
  773. desc_needs_upload = 1;
  774. return 0;
  775. }
  776. /** Mark descriptor out of date if it's older than <b>when</b> */
  777. void
  778. mark_my_descriptor_dirty_if_older_than(time_t when)
  779. {
  780. if (desc_clean_since < when)
  781. mark_my_descriptor_dirty();
  782. }
  783. /** Call when the current descriptor is out of date. */
  784. void
  785. mark_my_descriptor_dirty(void)
  786. {
  787. desc_clean_since = 0;
  788. }
  789. /** How frequently will we republish our descriptor because of large (factor
  790. * of 2) shifts in estimated bandwidth? */
  791. #define MAX_BANDWIDTH_CHANGE_FREQ (20*60)
  792. /** Check whether bandwidth has changed a lot since the last time we announced
  793. * bandwidth. If so, mark our descriptor dirty. */
  794. void
  795. check_descriptor_bandwidth_changed(time_t now)
  796. {
  797. static time_t last_changed = 0;
  798. uint64_t prev, cur;
  799. if (!desc_routerinfo)
  800. return;
  801. prev = desc_routerinfo->bandwidthcapacity;
  802. cur = we_are_hibernating() ? 0 : rep_hist_bandwidth_assess();
  803. if ((prev != cur && (!prev || !cur)) ||
  804. cur > prev*2 ||
  805. cur < prev/2) {
  806. if (last_changed+MAX_BANDWIDTH_CHANGE_FREQ < now) {
  807. log_info(LD_GENERAL,
  808. "Measured bandwidth has changed; rebuilding descriptor.");
  809. mark_my_descriptor_dirty();
  810. last_changed = now;
  811. }
  812. }
  813. }
  814. static void
  815. log_addr_has_changed(uint32_t prev, uint32_t cur)
  816. {
  817. char addrbuf_prev[INET_NTOA_BUF_LEN];
  818. char addrbuf_cur[INET_NTOA_BUF_LEN];
  819. struct in_addr in_prev;
  820. struct in_addr in_cur;
  821. in_prev.s_addr = htonl(prev);
  822. tor_inet_ntoa(&in_prev, addrbuf_prev, sizeof(addrbuf_prev));
  823. in_cur.s_addr = htonl(cur);
  824. tor_inet_ntoa(&in_cur, addrbuf_cur, sizeof(addrbuf_cur));
  825. if (prev)
  826. log_info(LD_GENERAL,
  827. "Our IP Address has changed from %s to %s; "
  828. "rebuilding descriptor.",
  829. addrbuf_prev, addrbuf_cur);
  830. else
  831. log_notice(LD_GENERAL,
  832. "Guessed our IP address as %s.",
  833. addrbuf_cur);
  834. }
  835. /** Check whether our own address as defined by the Address configuration
  836. * has changed. This is for routers that get their address from a service
  837. * like dyndns. If our address has changed, mark our descriptor dirty. */
  838. void
  839. check_descriptor_ipaddress_changed(time_t now)
  840. {
  841. uint32_t prev, cur;
  842. or_options_t *options = get_options();
  843. (void) now;
  844. if (!desc_routerinfo)
  845. return;
  846. prev = desc_routerinfo->addr;
  847. if (resolve_my_address(LOG_INFO, options, &cur, NULL) < 0) {
  848. log_info(LD_CONFIG,"options->Address didn't resolve into an IP.");
  849. return;
  850. }
  851. if (prev != cur) {
  852. log_addr_has_changed(prev, cur);
  853. mark_my_descriptor_dirty();
  854. /* the above call is probably redundant, since resolve_my_address()
  855. * probably already noticed and marked it dirty. */
  856. }
  857. }
  858. static uint32_t last_guessed_ip = 0;
  859. /** A directory authority told us our IP address is <b>suggestion</b>.
  860. * If this address is different from the one we think we are now, and
  861. * if our computer doesn't actually know its IP address, then switch. */
  862. void
  863. router_new_address_suggestion(const char *suggestion)
  864. {
  865. uint32_t addr, cur;
  866. struct in_addr in;
  867. /* first, learn what the IP address actually is */
  868. if (!tor_inet_aton(suggestion, &in)) {
  869. log_debug(LD_DIR, "Malformed X-Your-Address-Is header. Ignoring.");
  870. return;
  871. }
  872. addr = ntohl(in.s_addr);
  873. if (resolve_my_address(LOG_INFO, get_options(), &cur, NULL) >= 0) {
  874. /* We're all set -- we already know our address. Great. */
  875. last_guessed_ip = cur; /* store it in case we need it later */
  876. return;
  877. }
  878. if (last_guessed_ip != addr) {
  879. log_addr_has_changed(last_guessed_ip, addr);
  880. server_has_changed_ip();
  881. last_guessed_ip = addr; /* router_rebuild_descriptor() will fetch it */
  882. }
  883. }
  884. /** We failed to resolve our address locally, but we'd like to build
  885. * a descriptor and publish / test reachability. If we have a guess
  886. * about our address based on directory headers, answer it and return
  887. * 0; else return -1. */
  888. static int
  889. router_guess_address_from_dir_headers(uint32_t *guess)
  890. {
  891. if (last_guessed_ip) {
  892. *guess = last_guessed_ip;
  893. return 0;
  894. }
  895. return -1;
  896. }
  897. /** Set <b>platform</b> (max length <b>len</b>) to a NUL-terminated short
  898. * string describing the version of Tor and the operating system we're
  899. * currently running on.
  900. */
  901. void
  902. get_platform_str(char *platform, size_t len)
  903. {
  904. tor_snprintf(platform, len, "Tor %s on %s",
  905. VERSION, get_uname());
  906. return;
  907. }
  908. /* XXX need to audit this thing and count fenceposts. maybe
  909. * refactor so we don't have to keep asking if we're
  910. * near the end of maxlen?
  911. */
  912. #define DEBUG_ROUTER_DUMP_ROUTER_TO_STRING
  913. /** OR only: Given a routerinfo for this router, and an identity key to sign
  914. * with, encode the routerinfo as a signed server descriptor and write the
  915. * result into <b>s</b>, using at most <b>maxlen</b> bytes. Return -1 on
  916. * failure, and the number of bytes used on success.
  917. */
  918. int
  919. router_dump_router_to_string(char *s, size_t maxlen, routerinfo_t *router,
  920. crypto_pk_env_t *ident_key)
  921. {
  922. char *onion_pkey; /* Onion key, PEM-encoded. */
  923. char *identity_pkey; /* Identity key, PEM-encoded. */
  924. char digest[DIGEST_LEN];
  925. char published[ISO_TIME_LEN+1];
  926. char fingerprint[FINGERPRINT_LEN+1];
  927. struct in_addr in;
  928. char addrbuf[INET_NTOA_BUF_LEN];
  929. size_t onion_pkeylen, identity_pkeylen;
  930. size_t written;
  931. int result=0;
  932. addr_policy_t *tmpe;
  933. char *bandwidth_usage;
  934. char *family_line;
  935. #ifdef DEBUG_ROUTER_DUMP_ROUTER_TO_STRING
  936. char *s_tmp, *s_dup;
  937. const char *cp;
  938. routerinfo_t *ri_tmp;
  939. #endif
  940. or_options_t *options = get_options();
  941. /* Make sure the identity key matches the one in the routerinfo. */
  942. if (crypto_pk_cmp_keys(ident_key, router->identity_pkey)) {
  943. log_warn(LD_BUG,"Tried to sign a router with a private key that didn't "
  944. "match router's public key!");
  945. return -1;
  946. }
  947. /* record our fingerprint, so we can include it in the descriptor */
  948. if (crypto_pk_get_fingerprint(router->identity_pkey, fingerprint, 1)<0) {
  949. log_err(LD_BUG,"Error computing fingerprint");
  950. return -1;
  951. }
  952. /* PEM-encode the onion key */
  953. if (crypto_pk_write_public_key_to_string(router->onion_pkey,
  954. &onion_pkey,&onion_pkeylen)<0) {
  955. log_warn(LD_BUG,"write onion_pkey to string failed!");
  956. return -1;
  957. }
  958. /* PEM-encode the identity key key */
  959. if (crypto_pk_write_public_key_to_string(router->identity_pkey,
  960. &identity_pkey,&identity_pkeylen)<0) {
  961. log_warn(LD_BUG,"write identity_pkey to string failed!");
  962. tor_free(onion_pkey);
  963. return -1;
  964. }
  965. /* Encode the publication time. */
  966. format_iso_time(published, router->cache_info.published_on);
  967. /* How busy have we been? */
  968. bandwidth_usage = rep_hist_get_bandwidth_lines();
  969. if (router->declared_family && smartlist_len(router->declared_family)) {
  970. size_t n;
  971. char *s = smartlist_join_strings(router->declared_family, " ", 0, &n);
  972. n += strlen("family ") + 2; /* 1 for \n, 1 for \0. */
  973. family_line = tor_malloc(n);
  974. tor_snprintf(family_line, n, "family %s\n", s);
  975. tor_free(s);
  976. } else {
  977. family_line = tor_strdup("");
  978. }
  979. /* Generate the easy portion of the router descriptor. */
  980. result = tor_snprintf(s, maxlen,
  981. "router %s %s %d 0 %d\n"
  982. "platform %s\n"
  983. "published %s\n"
  984. "opt fingerprint %s\n"
  985. "uptime %ld\n"
  986. "bandwidth %d %d %d\n"
  987. "onion-key\n%s"
  988. "signing-key\n%s%s%s%s",
  989. router->nickname,
  990. router->address,
  991. router->or_port,
  992. decide_to_advertise_dirport(options, router),
  993. router->platform,
  994. published,
  995. fingerprint,
  996. stats_n_seconds_working,
  997. (int) router->bandwidthrate,
  998. (int) router->bandwidthburst,
  999. (int) router->bandwidthcapacity,
  1000. onion_pkey, identity_pkey,
  1001. family_line, bandwidth_usage,
  1002. we_are_hibernating() ? "opt hibernating 1\n" : "");
  1003. tor_free(family_line);
  1004. tor_free(onion_pkey);
  1005. tor_free(identity_pkey);
  1006. tor_free(bandwidth_usage);
  1007. if (result < 0)
  1008. return -1;
  1009. /* From now on, we use 'written' to remember the current length of 's'. */
  1010. written = result;
  1011. if (options->ContactInfo && strlen(options->ContactInfo)) {
  1012. result = tor_snprintf(s+written,maxlen-written, "contact %s\n",
  1013. options->ContactInfo);
  1014. if (result<0)
  1015. return -1;
  1016. written += result;
  1017. }
  1018. /* Write the exit policy to the end of 's'. */
  1019. for (tmpe=router->exit_policy; tmpe; tmpe=tmpe->next) {
  1020. /* Write: "accept 1.2.3.4" */
  1021. in.s_addr = htonl(tmpe->addr);
  1022. tor_inet_ntoa(&in, addrbuf, sizeof(addrbuf));
  1023. result = tor_snprintf(s+written, maxlen-written, "%s %s",
  1024. tmpe->policy_type == ADDR_POLICY_ACCEPT ? "accept" : "reject",
  1025. tmpe->msk == 0 ? "*" : addrbuf);
  1026. if (result < 0)
  1027. return -1;
  1028. written += result;
  1029. if (tmpe->msk != 0xFFFFFFFFu && tmpe->msk != 0) {
  1030. int n_bits = addr_mask_get_bits(tmpe->msk);
  1031. if (n_bits >= 0) {
  1032. if (tor_snprintf(s+written, maxlen-written, "/%d", n_bits)<0)
  1033. return -1;
  1034. } else {
  1035. /* Write "/255.255.0.0" */
  1036. in.s_addr = htonl(tmpe->msk);
  1037. tor_inet_ntoa(&in, addrbuf, sizeof(addrbuf));
  1038. if (tor_snprintf(s+written, maxlen-written, "/%s", addrbuf)<0)
  1039. return -1;
  1040. }
  1041. written += strlen(s+written);
  1042. }
  1043. if (tmpe->prt_min <= 1 && tmpe->prt_max == 65535) {
  1044. /* There is no port set; write ":*" */
  1045. if (written+4 > maxlen)
  1046. return -1;
  1047. strlcat(s+written, ":*\n", maxlen-written);
  1048. written += 3;
  1049. } else if (tmpe->prt_min == tmpe->prt_max) {
  1050. /* There is only one port; write ":80". */
  1051. result = tor_snprintf(s+written, maxlen-written, ":%d\n", tmpe->prt_min);
  1052. if (result<0)
  1053. return -1;
  1054. written += result;
  1055. } else {
  1056. /* There is a range of ports; write ":79-80". */
  1057. result = tor_snprintf(s+written, maxlen-written, ":%d-%d\n",
  1058. tmpe->prt_min, tmpe->prt_max);
  1059. if (result<0)
  1060. return -1;
  1061. written += result;
  1062. }
  1063. } /* end for */
  1064. if (written+256 > maxlen) /* Not enough room for signature. */
  1065. return -1;
  1066. /* Sign the directory */
  1067. strlcat(s+written, "router-signature\n", maxlen-written);
  1068. written += strlen(s+written);
  1069. s[written] = '\0';
  1070. if (router_get_router_hash(s, digest) < 0)
  1071. return -1;
  1072. if (router_append_dirobj_signature(s+written,maxlen-written,
  1073. digest,ident_key)<0) {
  1074. log_warn(LD_BUG, "Couldn't sign router descriptor");
  1075. return -1;
  1076. }
  1077. written += strlen(s+written);
  1078. if (written+2 > maxlen)
  1079. return -1;
  1080. /* include a last '\n' */
  1081. s[written] = '\n';
  1082. s[written+1] = 0;
  1083. #ifdef DEBUG_ROUTER_DUMP_ROUTER_TO_STRING
  1084. cp = s_tmp = s_dup = tor_strdup(s);
  1085. ri_tmp = router_parse_entry_from_string(cp, NULL, 1);
  1086. if (!ri_tmp) {
  1087. log_err(LD_BUG,
  1088. "We just generated a router descriptor we can't parse: <<%s>>",
  1089. s);
  1090. return -1;
  1091. }
  1092. tor_free(s_dup);
  1093. routerinfo_free(ri_tmp);
  1094. #endif
  1095. return written+1;
  1096. }
  1097. /** Return true iff <b>s</b> is a legally valid server nickname. */
  1098. int
  1099. is_legal_nickname(const char *s)
  1100. {
  1101. size_t len;
  1102. tor_assert(s);
  1103. len = strlen(s);
  1104. return len > 0 && len <= MAX_NICKNAME_LEN &&
  1105. strspn(s,LEGAL_NICKNAME_CHARACTERS) == len;
  1106. }
  1107. /** Return true iff <b>s</b> is a legally valid server nickname or
  1108. * hex-encoded identity-key digest. */
  1109. int
  1110. is_legal_nickname_or_hexdigest(const char *s)
  1111. {
  1112. if (*s!='$')
  1113. return is_legal_nickname(s);
  1114. else
  1115. return is_legal_hexdigest(s);
  1116. }
  1117. /** Return true iff <b>s</b> is a legally valid hex-encoded identity-key
  1118. * digest. */
  1119. int
  1120. is_legal_hexdigest(const char *s)
  1121. {
  1122. size_t len;
  1123. tor_assert(s);
  1124. if (s[0] == '$') s++;
  1125. len = strlen(s);
  1126. return (len == HEX_DIGEST_LEN &&
  1127. strspn(s,HEX_CHARACTERS)==len);
  1128. }
  1129. /** Forget that we have issued any router-related warnings, so that we'll
  1130. * warn again if we see the same errors. */
  1131. void
  1132. router_reset_warnings(void)
  1133. {
  1134. if (warned_nonexistent_family) {
  1135. SMARTLIST_FOREACH(warned_nonexistent_family, char *, cp, tor_free(cp));
  1136. smartlist_clear(warned_nonexistent_family);
  1137. }
  1138. }
  1139. /** Release all static resources held in router.c */
  1140. void
  1141. router_free_all(void)
  1142. {
  1143. if (onionkey)
  1144. crypto_free_pk_env(onionkey);
  1145. if (lastonionkey)
  1146. crypto_free_pk_env(lastonionkey);
  1147. if (identitykey)
  1148. crypto_free_pk_env(identitykey);
  1149. if (key_lock)
  1150. tor_mutex_free(key_lock);
  1151. if (desc_routerinfo)
  1152. routerinfo_free(desc_routerinfo);
  1153. if (warned_nonexistent_family) {
  1154. SMARTLIST_FOREACH(warned_nonexistent_family, char *, cp, tor_free(cp));
  1155. smartlist_free(warned_nonexistent_family);
  1156. }
  1157. }