router.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. /* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include "or.h"
  5. /**
  6. * \file router.c
  7. * \brief OR functionality, including key maintenance, generating
  8. * and uploading server descriptors, retrying OR connections.
  9. **/
  10. extern or_options_t options; /* command-line and config-file options */
  11. /** Exposed for test.c. */ void get_platform_str(char *platform, int len);
  12. /************************************************************/
  13. /*****
  14. * Key management: ORs only.
  15. *****/
  16. /** Private keys for this OR. There is also an SSL key managed by tortls.c.
  17. */
  18. static tor_mutex_t *key_lock=NULL;
  19. static time_t onionkey_set_at=0; /* When was onionkey last changed? */
  20. static crypto_pk_env_t *onionkey=NULL;
  21. static crypto_pk_env_t *lastonionkey=NULL;
  22. static crypto_pk_env_t *identitykey=NULL;
  23. /** Replace the current onion key with <b>k</b>. Does not affect lastonionkey;
  24. * to update onionkey correctly, call rotate_onion_key().
  25. */
  26. void set_onion_key(crypto_pk_env_t *k) {
  27. tor_mutex_acquire(key_lock);
  28. onionkey = k;
  29. onionkey_set_at = time(NULL);
  30. tor_mutex_release(key_lock);
  31. }
  32. /** Return the current onion key. Requires that the onion key has been
  33. * loaded or generated. */
  34. crypto_pk_env_t *get_onion_key(void) {
  35. tor_assert(onionkey);
  36. return onionkey;
  37. }
  38. /** Return the onion key that was current before the most recent onion
  39. * key rotation. If no rotation has been performed since this process
  40. * started, return NULL.
  41. */
  42. crypto_pk_env_t *get_previous_onion_key(void) {
  43. return lastonionkey;
  44. }
  45. void dup_onion_keys(crypto_pk_env_t **key, crypto_pk_env_t **last)
  46. {
  47. tor_assert(key && last);
  48. tor_mutex_acquire(key_lock);
  49. *key = crypto_pk_dup_key(onionkey);
  50. if (lastonionkey)
  51. *last = crypto_pk_dup_key(lastonionkey);
  52. else
  53. *last = NULL;
  54. tor_mutex_release(key_lock);
  55. }
  56. /** Return the time when the onion key was last set. This is either the time
  57. * when the process launched, or the time of the most recent key rotation since
  58. * the process launched.
  59. */
  60. time_t get_onion_key_set_at(void) {
  61. return onionkey_set_at;
  62. }
  63. /** Set the current identity key to k.
  64. */
  65. void set_identity_key(crypto_pk_env_t *k) {
  66. identitykey = k;
  67. }
  68. /** Returns the current identity key; requires that the identity key has been
  69. * set.
  70. */
  71. crypto_pk_env_t *get_identity_key(void) {
  72. tor_assert(identitykey);
  73. return identitykey;
  74. }
  75. /** Replace the previous onion key with the current onion key, and generate
  76. * a new previous onion key. Immediately after calling this function,
  77. * the OR should:
  78. * - schedule all previous cpuworkers to shut down _after_ processing
  79. * pending work. (This will cause fresh cpuworkers to be generated.)
  80. * - generate and upload a fresh routerinfo.
  81. */
  82. void rotate_onion_key(void)
  83. {
  84. char fname[512];
  85. crypto_pk_env_t *prkey;
  86. sprintf(fname,"%s/keys/onion.key",get_data_directory(&options));
  87. if (!(prkey = crypto_new_pk_env())) {
  88. log(LOG_ERR, "Error creating crypto environment.");
  89. goto error;
  90. }
  91. if (crypto_pk_generate_key(prkey)) {
  92. log(LOG_ERR, "Error generating onion key");
  93. goto error;
  94. }
  95. if (crypto_pk_write_private_key_to_filename(prkey, fname)) {
  96. log(LOG_ERR, "Couldn't write generated key to %s.", fname);
  97. goto error;
  98. }
  99. tor_mutex_acquire(key_lock);
  100. if (lastonionkey)
  101. crypto_free_pk_env(lastonionkey);
  102. log_fn(LOG_INFO, "Rotating onion key");
  103. lastonionkey = onionkey;
  104. set_onion_key(prkey);
  105. tor_mutex_release(key_lock);
  106. return;
  107. error:
  108. log_fn(LOG_WARN, "Couldn't rotate onion key.");
  109. }
  110. /** Try to read an RSA key from <b>fname</b>. If <b>fname</b> doesn't exist,
  111. * create a new RSA key and save it in <b>fname</b>. Return the read/created
  112. * key, or NULL on error.
  113. */
  114. crypto_pk_env_t *init_key_from_file(const char *fname)
  115. {
  116. crypto_pk_env_t *prkey = NULL;
  117. FILE *file = NULL;
  118. if (!(prkey = crypto_new_pk_env())) {
  119. log(LOG_ERR, "Error creating crypto environment.");
  120. goto error;
  121. }
  122. switch(file_status(fname)) {
  123. case FN_DIR:
  124. case FN_ERROR:
  125. log(LOG_ERR, "Can't read key from %s", fname);
  126. goto error;
  127. case FN_NOENT:
  128. log(LOG_INFO, "No key found in %s; generating fresh key.", fname);
  129. if (crypto_pk_generate_key(prkey)) {
  130. log(LOG_ERR, "Error generating onion key");
  131. goto error;
  132. }
  133. if (crypto_pk_check_key(prkey) <= 0) {
  134. log(LOG_ERR, "Generated key seems invalid");
  135. goto error;
  136. }
  137. log(LOG_INFO, "Generated key seems valid");
  138. if (crypto_pk_write_private_key_to_filename(prkey, fname)) {
  139. log(LOG_ERR, "Couldn't write generated key to %s.", fname);
  140. goto error;
  141. }
  142. return prkey;
  143. case FN_FILE:
  144. if (crypto_pk_read_private_key_from_filename(prkey, fname)) {
  145. log(LOG_ERR, "Error loading private key.");
  146. goto error;
  147. }
  148. return prkey;
  149. default:
  150. tor_assert(0);
  151. }
  152. error:
  153. if (prkey)
  154. crypto_free_pk_env(prkey);
  155. if (file)
  156. fclose(file);
  157. return NULL;
  158. }
  159. /** Initialize all OR private keys, and the TLS context, as necessary.
  160. * On OPs, this only initializes the tls context.
  161. */
  162. int init_keys(void) {
  163. char keydir[512];
  164. char fingerprint[FINGERPRINT_LEN+MAX_NICKNAME_LEN+3];
  165. char *cp;
  166. const char *tmp, *mydesc, *datadir;
  167. crypto_pk_env_t *prkey;
  168. if (!key_lock)
  169. key_lock = tor_mutex_new();
  170. /* OP's don't need keys. Just initialize the TLS context.*/
  171. if (!options.ORPort) {
  172. tor_assert(!options.DirPort);
  173. if (tor_tls_context_new(NULL, 0, NULL, 0)<0) {
  174. log_fn(LOG_ERR, "Error creating TLS context for OP.");
  175. return -1;
  176. }
  177. return 0;
  178. }
  179. /* Make sure DataDirectory exists, and is private. */
  180. datadir = get_data_directory(&options);
  181. tor_assert(datadir);
  182. if (strlen(datadir) > (512-128)) {
  183. log_fn(LOG_ERR, "DataDirectory is too long.");
  184. return -1;
  185. }
  186. if (check_private_dir(datadir, 1)) {
  187. return -1;
  188. }
  189. /* Check the key directory. */
  190. sprintf(keydir,"%s/keys", datadir);
  191. if (check_private_dir(keydir, 1)) {
  192. return -1;
  193. }
  194. cp = keydir + strlen(keydir); /* End of string. */
  195. /* 1. Read identity key. Make it if none is found. */
  196. strcpy(cp, "/identity.key");
  197. log_fn(LOG_INFO,"Reading/making identity key %s...",keydir);
  198. prkey = init_key_from_file(keydir);
  199. if (!prkey) return -1;
  200. set_identity_key(prkey);
  201. /* 2. Read onion key. Make it if none is found. */
  202. strcpy(cp, "/onion.key");
  203. log_fn(LOG_INFO,"Reading/making onion key %s...",keydir);
  204. prkey = init_key_from_file(keydir);
  205. if (!prkey) return -1;
  206. set_onion_key(prkey);
  207. /* 3. Initialize link key and TLS context. */
  208. if (tor_tls_context_new(get_identity_key(), 1, options.Nickname,
  209. MAX_SSL_KEY_LIFETIME) < 0) {
  210. log_fn(LOG_ERR, "Error initializing TLS context");
  211. return -1;
  212. }
  213. /* 4. Dump router descriptor to 'router.desc' */
  214. /* Must be called after keys are initialized. */
  215. if (!(router_get_my_descriptor())) {
  216. log_fn(LOG_ERR, "Error initializing descriptor.");
  217. return -1;
  218. }
  219. /* We need to add our own fingerprint so it gets recognized. */
  220. if (dirserv_add_own_fingerprint(options.Nickname, get_identity_key())) {
  221. log_fn(LOG_ERR, "Error adding own fingerprint to approved set");
  222. return -1;
  223. }
  224. tmp = mydesc = router_get_my_descriptor();
  225. if (dirserv_add_descriptor(&tmp) != 1) {
  226. log(LOG_ERR, "Unable to add own descriptor to directory.");
  227. return -1;
  228. }
  229. sprintf(keydir,"%s/router.desc", datadir);
  230. log_fn(LOG_INFO,"Dumping descriptor to %s...",keydir);
  231. if (write_str_to_file(keydir, mydesc)) {
  232. return -1;
  233. }
  234. /* 5. Dump fingerprint to 'fingerprint' */
  235. sprintf(keydir,"%s/fingerprint", datadir);
  236. log_fn(LOG_INFO,"Dumping fingerprint to %s...",keydir);
  237. tor_assert(strlen(options.Nickname) <= MAX_NICKNAME_LEN);
  238. strcpy(fingerprint, options.Nickname);
  239. strcat(fingerprint, " ");
  240. if (crypto_pk_get_fingerprint(get_identity_key(),
  241. fingerprint+strlen(fingerprint))<0) {
  242. log_fn(LOG_ERR, "Error computing fingerprint");
  243. return -1;
  244. }
  245. strcat(fingerprint, "\n");
  246. if (write_str_to_file(keydir, fingerprint))
  247. return -1;
  248. if(!options.DirPort)
  249. return 0;
  250. /* 6. [dirserver only] load approved-routers file */
  251. sprintf(keydir,"%s/approved-routers", datadir);
  252. log_fn(LOG_INFO,"Loading approved fingerprints from %s...",keydir);
  253. if(dirserv_parse_fingerprint_file(keydir) < 0) {
  254. log_fn(LOG_ERR, "Error loading fingerprints");
  255. return -1;
  256. }
  257. /* 7. [dirserver only] load old directory, if it's there */
  258. sprintf(keydir,"%s/cached-directory", datadir);
  259. log_fn(LOG_INFO,"Loading cached directory from %s...",keydir);
  260. cp = read_file_to_str(keydir);
  261. if(!cp) {
  262. log_fn(LOG_INFO,"Cached directory %s not present. Ok.",keydir);
  263. } else {
  264. if(options.AuthoritativeDir) {
  265. if(dirserv_load_from_directory_string(cp) < 0){
  266. log_fn(LOG_ERR, "Cached directory %s is corrupt", keydir);
  267. tor_free(cp);
  268. return -1;
  269. }
  270. } else {
  271. /* set time to 1 so it will be replaced on first download. */
  272. dirserv_set_cached_directory(cp, 1);
  273. }
  274. tor_free(cp);
  275. }
  276. /* success */
  277. return 0;
  278. }
  279. /*
  280. * Clique maintenance
  281. */
  282. /** OR only: try to open connections to all of the other ORs we know about.
  283. */
  284. void router_retry_connections(void) {
  285. int i;
  286. routerinfo_t *router;
  287. routerlist_t *rl;
  288. router_get_routerlist(&rl);
  289. for (i=0;i < smartlist_len(rl->routers);i++) {
  290. router = smartlist_get(rl->routers, i);
  291. if(router_is_me(router))
  292. continue;
  293. if(!connection_exact_get_by_addr_port(router->addr,router->or_port)) {
  294. /* not in the list */
  295. log_fn(LOG_DEBUG,"connecting to OR %s:%u.",router->address,router->or_port);
  296. connection_or_connect(router);
  297. }
  298. }
  299. }
  300. /*
  301. * OR descriptor generation.
  302. */
  303. /** My routerinfo. */
  304. static routerinfo_t *desc_routerinfo = NULL;
  305. /** String representation of my descriptor, signed by me. */
  306. static char descriptor[8192];
  307. /** OR only: try to upload our signed descriptor to all the directory servers
  308. * we know about.
  309. */
  310. void router_upload_dir_desc_to_dirservers(void) {
  311. const char *s;
  312. s = router_get_my_descriptor();
  313. if (!s) {
  314. log_fn(LOG_WARN, "No descriptor; skipping upload");
  315. return;
  316. }
  317. directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_DIR, s, strlen(s));
  318. }
  319. #define DEFAULT_EXIT_POLICY "reject 0.0.0.0/8,reject 169.254.0.0/16,reject 127.0.0.0/8,reject 192.168.0.0/16,reject 10.0.0.0/8,reject 172.16.0.0/12,accept *:20-22,accept *:53,accept *:79-81,accept *:110,accept *:143,accept *:443,accept *:873,accept *:993,accept *:995,accept *:1024-65535,reject *:*"
  320. /** Set the exit policy on <b>router</b> to match the exit policy in the
  321. * current configuration file. If the exit policy doesn't have a catch-all
  322. * rule, then append the default exit policy as well.
  323. */
  324. static void router_add_exit_policy_from_config(routerinfo_t *router) {
  325. struct exit_policy_t *ep;
  326. struct config_line_t default_policy;
  327. config_parse_exit_policy(options.ExitPolicy, &router->exit_policy);
  328. for (ep = router->exit_policy; ep; ep = ep->next) {
  329. if (ep->msk == 0 && ep->prt_min <= 1 && ep->prt_max >= 65535) {
  330. /* if exitpolicy includes a *:* line, then we're done. */
  331. return;
  332. }
  333. }
  334. /* Else, append the default exitpolicy. */
  335. default_policy.key = NULL;
  336. default_policy.value = DEFAULT_EXIT_POLICY;
  337. default_policy.next = NULL;
  338. config_parse_exit_policy(&default_policy, &router->exit_policy);
  339. }
  340. /** OR only: Return false if my exit policy says to allow connection to
  341. * conn. Else return true.
  342. */
  343. int router_compare_to_my_exit_policy(connection_t *conn)
  344. {
  345. tor_assert(desc_routerinfo);
  346. tor_assert(conn->addr); /* make sure it's resolved to something. this
  347. way we can't get a 'maybe' below. */
  348. return router_compare_addr_to_exit_policy(conn->addr, conn->port,
  349. desc_routerinfo->exit_policy);
  350. }
  351. /** Return true iff <b>router</b> has the same nickname as this OR. (For an
  352. * OP, always returns false.)
  353. */
  354. int router_is_me(routerinfo_t *router)
  355. {
  356. tor_assert(router);
  357. return options.Nickname && !strcasecmp(router->nickname, options.Nickname);
  358. }
  359. /** Return a routerinfo for this OR, rebuilding a fresh one if
  360. * necessary. Return NULL on error, or if called on an OP. */
  361. routerinfo_t *router_get_my_routerinfo(void)
  362. {
  363. if (!options.ORPort)
  364. return NULL;
  365. if (!desc_routerinfo) {
  366. if (router_rebuild_descriptor())
  367. return NULL;
  368. }
  369. return desc_routerinfo;
  370. }
  371. /** OR only: Return a signed server descriptor for this OR, rebuilding a fresh
  372. * one if necessary. Return NULL on error.
  373. */
  374. const char *router_get_my_descriptor(void) {
  375. if (!desc_routerinfo) {
  376. if (router_rebuild_descriptor())
  377. return NULL;
  378. }
  379. log_fn(LOG_DEBUG,"my desc is '%s'",descriptor);
  380. return descriptor;
  381. }
  382. /** Rebuild a fresh routerinfo and signed server descriptor for this
  383. * OR. Return 0 on success, -1 on error.
  384. */
  385. int router_rebuild_descriptor(void) {
  386. routerinfo_t *ri;
  387. struct in_addr addr;
  388. char platform[256];
  389. if (!tor_inet_aton(options.Address, &addr)) {
  390. log_fn(LOG_ERR, "options.Address didn't hold an IP.");
  391. return -1;
  392. }
  393. ri = tor_malloc_zero(sizeof(routerinfo_t));
  394. ri->address = tor_strdup(options.Address);
  395. ri->nickname = tor_strdup(options.Nickname);
  396. ri->addr = (uint32_t) ntohl(addr.s_addr);
  397. ri->or_port = options.ORPort;
  398. ri->socks_port = options.SocksPort;
  399. ri->dir_port = options.DirPort;
  400. ri->published_on = time(NULL);
  401. ri->onion_pkey = crypto_pk_dup_key(get_onion_key()); /* must invoke from main thread */
  402. ri->identity_pkey = crypto_pk_dup_key(get_identity_key());
  403. if (crypto_pk_get_digest(ri->identity_pkey, ri->identity_digest)<0) {
  404. routerinfo_free(ri);
  405. return -1;
  406. }
  407. get_platform_str(platform, sizeof(platform));
  408. ri->platform = tor_strdup(platform);
  409. ri->bandwidthrate = options.BandwidthRate;
  410. ri->bandwidthburst = options.BandwidthBurst;
  411. ri->exit_policy = NULL; /* zero it out first */
  412. router_add_exit_policy_from_config(ri);
  413. if (desc_routerinfo)
  414. routerinfo_free(desc_routerinfo);
  415. desc_routerinfo = ri;
  416. if (router_dump_router_to_string(descriptor, 8192, ri, get_identity_key())<0) {
  417. log_fn(LOG_WARN, "Couldn't dump router to string.");
  418. return -1;
  419. }
  420. if (ri->dir_port)
  421. ri->is_trusted_dir = 1;
  422. return 0;
  423. }
  424. /** Set <b>platform</b> (max length <b>len</b>) to a NUL-terminated short
  425. * string describing the version of Tor and the operating system we're
  426. * currently running on.
  427. */
  428. void get_platform_str(char *platform, int len)
  429. {
  430. snprintf(platform, len-1, "Tor %s on %s", VERSION, get_uname());
  431. platform[len-1] = '\0';
  432. return;
  433. }
  434. /* XXX need to audit this thing and count fenceposts. maybe
  435. * refactor so we don't have to keep asking if we're
  436. * near the end of maxlen?
  437. */
  438. #define DEBUG_ROUTER_DUMP_ROUTER_TO_STRING
  439. /** OR only: Given a routerinfo for this router, and an identity key to sign
  440. * with, encode the routerinfo as a signed server descriptor and write the
  441. * result into <b>s</b>, using at most <b>maxlen</b> bytes. Return -1 on
  442. * failure, and the number of bytes used on success.
  443. */
  444. int router_dump_router_to_string(char *s, int maxlen, routerinfo_t *router,
  445. crypto_pk_env_t *ident_key) {
  446. char *onion_pkey; /* Onion key, PEM-encoded. */
  447. char *identity_pkey; /* Identity key, PEM-encoded. */
  448. char digest[20];
  449. char signature[128];
  450. char published[32];
  451. struct in_addr in;
  452. int onion_pkeylen, identity_pkeylen;
  453. int written;
  454. int result=0;
  455. struct exit_policy_t *tmpe;
  456. #ifdef DEBUG_ROUTER_DUMP_ROUTER_TO_STRING
  457. char *s_tmp, *s_dup;
  458. const char *cp;
  459. routerinfo_t *ri_tmp;
  460. #endif
  461. /* Make sure the identity key matches the one in the routerinfo. */
  462. if (crypto_pk_cmp_keys(ident_key, router->identity_pkey)) {
  463. log_fn(LOG_WARN,"Tried to sign a router with a private key that didn't match router's public key!");
  464. return -1;
  465. }
  466. /* PEM-encode the onion key */
  467. if(crypto_pk_write_public_key_to_string(router->onion_pkey,
  468. &onion_pkey,&onion_pkeylen)<0) {
  469. log_fn(LOG_WARN,"write onion_pkey to string failed!");
  470. return -1;
  471. }
  472. /* PEM-encode the identity key key */
  473. if(crypto_pk_write_public_key_to_string(router->identity_pkey,
  474. &identity_pkey,&identity_pkeylen)<0) {
  475. log_fn(LOG_WARN,"write identity_pkey to string failed!");
  476. tor_free(onion_pkey);
  477. return -1;
  478. }
  479. /* Encode the publication time. */
  480. strftime(published, 32, "%Y-%m-%d %H:%M:%S", gmtime(&router->published_on));
  481. /* Generate the easy portion of the router descriptor. */
  482. result = snprintf(s, maxlen,
  483. "router %s %s %d %d %d\n"
  484. "platform %s\n"
  485. "published %s\n"
  486. "bandwidth %d %d\n"
  487. "onion-key\n%s"
  488. "signing-key\n%s",
  489. router->nickname,
  490. router->address,
  491. router->or_port,
  492. router->socks_port,
  493. /* Due to an 0.0.7 bug, we can't actually say that we have a dirport unles
  494. * we're an authoritative directory.
  495. */
  496. router->is_trusted_dir ? router->dir_port : 0,
  497. router->platform,
  498. published,
  499. (int) router->bandwidthrate,
  500. (int) router->bandwidthburst,
  501. onion_pkey, identity_pkey);
  502. tor_free(onion_pkey);
  503. tor_free(identity_pkey);
  504. if(result < 0 || result >= maxlen) {
  505. /* apparently different glibcs do different things on snprintf error.. so check both */
  506. return -1;
  507. }
  508. /* From now on, we use 'written' to remember the current length of 's'. */
  509. written = result;
  510. if (router->dir_port && !router->is_trusted_dir) {
  511. /* dircacheport wasn't recognized before 0.0.8pre. (When 0.0.7 is gone,
  512. * we can fold this back into dirport anyway.) */
  513. result = snprintf(s+written,maxlen-written, "opt dircacheport %d\n",
  514. router->dir_port);
  515. if (result<0 || result+written > maxlen)
  516. return -1;
  517. written += result;
  518. }
  519. if (options.ContactInfo && strlen(options.ContactInfo)) {
  520. result = snprintf(s+written,maxlen-written, "opt contact %s\n",
  521. options.ContactInfo);
  522. if (result<0 || result+written > maxlen)
  523. return -1;
  524. written += result;
  525. }
  526. /* Write the exit policy to the end of 's'. */
  527. for(tmpe=router->exit_policy; tmpe; tmpe=tmpe->next) {
  528. in.s_addr = htonl(tmpe->addr);
  529. /* Write: "accept 1.2.3.4" */
  530. result = snprintf(s+written, maxlen-written, "%s %s",
  531. tmpe->policy_type == EXIT_POLICY_ACCEPT ? "accept" : "reject",
  532. tmpe->msk == 0 ? "*" : inet_ntoa(in));
  533. if(result < 0 || result+written > maxlen) {
  534. /* apparently different glibcs do different things on snprintf error.. so check both */
  535. return -1;
  536. }
  537. written += result;
  538. if (tmpe->msk != 0xFFFFFFFFu && tmpe->msk != 0) {
  539. /* Write "/255.255.0.0" */
  540. in.s_addr = htonl(tmpe->msk);
  541. result = snprintf(s+written, maxlen-written, "/%s", inet_ntoa(in));
  542. if (result<0 || result+written > maxlen)
  543. return -1;
  544. written += result;
  545. }
  546. if (tmpe->prt_min == 0 && tmpe->prt_max == 65535) {
  547. /* There is no port set; write ":*" */
  548. if (written > maxlen-4)
  549. return -1;
  550. strcat(s+written, ":*\n");
  551. written += 3;
  552. } else if (tmpe->prt_min == tmpe->prt_max) {
  553. /* There is only one port; write ":80". */
  554. result = snprintf(s+written, maxlen-written, ":%d\n", tmpe->prt_min);
  555. if (result<0 || result+written > maxlen)
  556. return -1;
  557. written += result;
  558. } else {
  559. /* There is a range of ports; write ":79-80". */
  560. result = snprintf(s+written, maxlen-written, ":%d-%d\n", tmpe->prt_min,
  561. tmpe->prt_max);
  562. if (result<0 || result+written > maxlen)
  563. return -1;
  564. written += result;
  565. }
  566. } /* end for */
  567. if (written > maxlen-256) /* Not enough room for signature. */
  568. return -1;
  569. /* Sign the directory */
  570. strcat(s+written, "router-signature\n");
  571. written += strlen(s+written);
  572. s[written] = '\0';
  573. if (router_get_router_hash(s, digest) < 0)
  574. return -1;
  575. if (crypto_pk_private_sign(ident_key, digest, 20, signature) < 0) {
  576. log_fn(LOG_WARN, "Error signing digest");
  577. return -1;
  578. }
  579. strcat(s+written, "-----BEGIN SIGNATURE-----\n");
  580. written += strlen(s+written);
  581. if (base64_encode(s+written, maxlen-written, signature, 128) < 0) {
  582. log_fn(LOG_WARN, "Couldn't base64-encode signature");
  583. return -1;
  584. }
  585. written += strlen(s+written);
  586. strcat(s+written, "-----END SIGNATURE-----\n");
  587. written += strlen(s+written);
  588. if (written > maxlen-2)
  589. return -1;
  590. /* include a last '\n' */
  591. s[written] = '\n';
  592. s[written+1] = 0;
  593. #ifdef DEBUG_ROUTER_DUMP_ROUTER_TO_STRING
  594. cp = s_tmp = s_dup = tor_strdup(s);
  595. ri_tmp = router_parse_entry_from_string(cp, NULL);
  596. if (!ri_tmp) {
  597. log_fn(LOG_ERR, "We just generated a router descriptor we can't parse: <<%s>>",
  598. s);
  599. return -1;
  600. }
  601. free(s_dup);
  602. routerinfo_free(ri_tmp);
  603. #endif
  604. return written+1;
  605. }
  606. /*
  607. Local Variables:
  608. mode:c
  609. indent-tabs-mode:nil
  610. c-basic-offset:2
  611. End:
  612. */