routers.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381
  1. /* Copyright 2001-2003 Roger Dingledine, Matej Pfajfar. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #define OR_PUBLICKEY_BEGIN_TAG "-----BEGIN RSA PUBLIC KEY-----\n"
  5. #define OR_PUBLICKEY_END_TAG "-----END RSA PUBLIC KEY-----\n"
  6. #define OR_SIGNATURE_BEGIN_TAG "-----BEGIN SIGNATURE-----\n"
  7. #define OR_SIGNATURE_END_TAG "-----END SIGNATURE-----\n"
  8. #define _GNU_SOURCE
  9. /* XXX this is required on rh7 to make strptime not complain. how bad
  10. * is this for portability?
  11. */
  12. #include "or.h"
  13. /****************************************************************************/
  14. static directory_t *directory = NULL; /* router array */
  15. static routerinfo_t *desc_routerinfo = NULL; /* my descriptor */
  16. static char descriptor[8192]; /* string representation of my descriptor */
  17. extern or_options_t options; /* command-line and config-file options */
  18. /****************************************************************************/
  19. struct directory_token;
  20. typedef struct directory_token directory_token_t;
  21. /* static function prototypes */
  22. void routerlist_free(routerinfo_t *list);
  23. static int router_add_exit_policy_from_string(routerinfo_t *router, char *s);
  24. static int router_add_exit_policy(routerinfo_t *router,
  25. directory_token_t *tok);
  26. static int router_resolve_directory(directory_t *dir);
  27. /****************************************************************************/
  28. void router_retry_connections(void) {
  29. int i;
  30. routerinfo_t *router;
  31. for (i=0;i<directory->n_routers;i++) {
  32. router = directory->routers[i];
  33. if(!connection_exact_get_by_addr_port(router->addr,router->or_port)) { /* not in the list */
  34. log_fn(LOG_DEBUG,"connecting to OR %s:%u.",router->address,router->or_port);
  35. connection_or_connect(router);
  36. }
  37. }
  38. }
  39. routerinfo_t *router_pick_directory_server(void) {
  40. /* pick the first running router with a positive dir_port */
  41. int i;
  42. routerinfo_t *router, *dirserver=NULL;
  43. if(!directory)
  44. return NULL;
  45. for(i=0;i<directory->n_routers;i++) {
  46. router = directory->routers[i];
  47. if(router->dir_port > 0 && router->is_running)
  48. return router;
  49. }
  50. log_fn(LOG_INFO,"No dirservers are reachable. Trying them all again.");
  51. /* no running dir servers found? go through and mark them all as up,
  52. * and we'll cycle through the list again. */
  53. for(i=0;i<directory->n_routers;i++) {
  54. router = directory->routers[i];
  55. if(router->dir_port > 0) {
  56. router->is_running = 1;
  57. dirserver = router;
  58. }
  59. }
  60. return dirserver;
  61. }
  62. void router_upload_desc_to_dirservers(void) {
  63. int i;
  64. routerinfo_t *router;
  65. if(!directory)
  66. return;
  67. if (!router_get_my_descriptor()) {
  68. log_fn(LOG_WARN, "No descriptor; skipping upload");
  69. return;
  70. }
  71. for(i=0;i<directory->n_routers;i++) {
  72. router = directory->routers[i];
  73. if(router->dir_port > 0)
  74. directory_initiate_command(router, DIR_CONN_STATE_CONNECTING_UPLOAD);
  75. }
  76. }
  77. routerinfo_t *router_get_by_addr_port(uint32_t addr, uint16_t port) {
  78. int i;
  79. routerinfo_t *router;
  80. assert(directory);
  81. for(i=0;i<directory->n_routers;i++) {
  82. router = directory->routers[i];
  83. if ((router->addr == addr) && (router->or_port == port))
  84. return router;
  85. }
  86. return NULL;
  87. }
  88. routerinfo_t *router_get_by_link_pk(crypto_pk_env_t *pk)
  89. {
  90. int i;
  91. routerinfo_t *router;
  92. assert(directory);
  93. for(i=0;i<directory->n_routers;i++) {
  94. router = directory->routers[i];
  95. if (0 == crypto_pk_cmp_keys(router->link_pkey, pk))
  96. return router;
  97. }
  98. return NULL;
  99. }
  100. routerinfo_t *router_get_by_nickname(char *nickname)
  101. {
  102. int i;
  103. routerinfo_t *router;
  104. assert(directory);
  105. for(i=0;i<directory->n_routers;i++) {
  106. router = directory->routers[i];
  107. if (0 == strcmp(router->nickname, nickname))
  108. return router;
  109. }
  110. return NULL;
  111. }
  112. void router_get_directory(directory_t **pdirectory) {
  113. *pdirectory = directory;
  114. }
  115. /* delete a router from memory */
  116. void routerinfo_free(routerinfo_t *router)
  117. {
  118. struct exit_policy_t *e;
  119. if (!router)
  120. return;
  121. tor_free(router->address);
  122. tor_free(router->nickname);
  123. if (router->onion_pkey)
  124. crypto_free_pk_env(router->onion_pkey);
  125. if (router->link_pkey)
  126. crypto_free_pk_env(router->link_pkey);
  127. if (router->identity_pkey)
  128. crypto_free_pk_env(router->identity_pkey);
  129. while (router->exit_policy) {
  130. e = router->exit_policy;
  131. router->exit_policy = e->next;
  132. tor_free(e->string);
  133. free(e);
  134. }
  135. free(router);
  136. }
  137. void directory_free(directory_t *dir)
  138. {
  139. int i;
  140. for (i = 0; i < dir->n_routers; ++i)
  141. routerinfo_free(dir->routers[i]);
  142. tor_free(dir->routers);
  143. tor_free(dir->software_versions);
  144. free(dir);
  145. }
  146. void router_mark_as_down(char *nickname) {
  147. routerinfo_t *router = router_get_by_nickname(nickname);
  148. if(!router) /* we don't seem to know about him in the first place */
  149. return;
  150. log_fn(LOG_DEBUG,"Marking %s as down.",router->nickname);
  151. router->is_running = 0;
  152. }
  153. /* load the router list */
  154. int router_get_list_from_file(char *routerfile)
  155. {
  156. char *string;
  157. string = read_file_to_str(routerfile);
  158. if(!string) {
  159. log_fn(LOG_WARN,"Failed to load routerfile %s.",routerfile);
  160. return -1;
  161. }
  162. if(router_get_list_from_string(string) < 0) {
  163. log_fn(LOG_WARN,"The routerfile itself was corrupt.");
  164. free(string);
  165. return -1;
  166. }
  167. free(string);
  168. return 0;
  169. }
  170. typedef enum {
  171. K_ACCEPT,
  172. K_DIRECTORY_SIGNATURE,
  173. K_RECOMMENDED_SOFTWARE,
  174. K_REJECT,
  175. K_ROUTER,
  176. K_SIGNED_DIRECTORY,
  177. K_SIGNING_KEY,
  178. K_ONION_KEY,
  179. K_LINK_KEY,
  180. K_ROUTER_SIGNATURE,
  181. K_PUBLISHED,
  182. K_RUNNING_ROUTERS,
  183. K_PLATFORM,
  184. _SIGNATURE,
  185. _PUBLIC_KEY,
  186. _ERR,
  187. _EOF
  188. } directory_keyword;
  189. struct token_table_ent { char *t; int v; };
  190. static struct token_table_ent token_table[] = {
  191. { "accept", K_ACCEPT },
  192. { "directory-signature", K_DIRECTORY_SIGNATURE },
  193. { "reject", K_REJECT },
  194. { "router", K_ROUTER },
  195. { "recommended-software", K_RECOMMENDED_SOFTWARE },
  196. { "signed-directory", K_SIGNED_DIRECTORY },
  197. { "signing-key", K_SIGNING_KEY },
  198. { "onion-key", K_ONION_KEY },
  199. { "link-key", K_LINK_KEY },
  200. { "router-signature", K_ROUTER_SIGNATURE },
  201. { "published", K_PUBLISHED },
  202. { "running-routers", K_RUNNING_ROUTERS },
  203. { "platform", K_PLATFORM },
  204. { NULL, -1 }
  205. };
  206. #define MAX_ARGS 1024
  207. struct directory_token {
  208. directory_keyword tp;
  209. union {
  210. struct {
  211. char *args[MAX_ARGS+1];
  212. int n_args;
  213. } cmd;
  214. char *signature;
  215. char *error;
  216. crypto_pk_env_t *public_key;
  217. } val;
  218. };
  219. /* Free any malloced resources allocated for a token. Don't call this if
  220. you inherit the reference to those resources.
  221. */
  222. static void
  223. router_release_token(directory_token_t *tok)
  224. {
  225. switch (tok->tp)
  226. {
  227. case _SIGNATURE:
  228. free(tok->val.signature);
  229. break;
  230. case _PUBLIC_KEY:
  231. crypto_free_pk_env(tok->val.public_key);
  232. break;
  233. default:
  234. break;
  235. }
  236. }
  237. static int
  238. _router_get_next_token(char **s, directory_token_t *tok) {
  239. char *next;
  240. crypto_pk_env_t *pkey = NULL;
  241. char *signature = NULL;
  242. int i, done;
  243. tok->tp = _ERR;
  244. tok->val.error = "";
  245. *s = eat_whitespace(*s);
  246. if (!**s) {
  247. tok->tp = _EOF;
  248. return 0;
  249. } else if (**s == '-') {
  250. next = strchr(*s, '\n');
  251. if (! next) { tok->val.error = "No newline at EOF"; return -1; }
  252. ++next;
  253. if (! strncmp(*s, OR_PUBLICKEY_BEGIN_TAG, next-*s)) {
  254. next = strstr(*s, OR_PUBLICKEY_END_TAG);
  255. if (!next) { tok->val.error = "No public key end tag found"; return -1; }
  256. next = strchr(next, '\n'); /* Part of OR_PUBLICKEY_END_TAG; can't fail.*/
  257. ++next;
  258. if (!(pkey = crypto_new_pk_env(CRYPTO_PK_RSA)))
  259. return -1;
  260. if (crypto_pk_read_public_key_from_string(pkey, *s, next-*s)) {
  261. crypto_free_pk_env(pkey);
  262. tok->val.error = "Couldn't parse public key.";
  263. return -1;
  264. }
  265. tok->tp = _PUBLIC_KEY;
  266. tok->val.public_key = pkey;
  267. *s = next;
  268. return 0;
  269. } else if (! strncmp(*s, OR_SIGNATURE_BEGIN_TAG, next-*s)) {
  270. /* Advance past newline; can't fail. */
  271. *s = strchr(*s, '\n');
  272. ++*s;
  273. /* Find end of base64'd data */
  274. next = strstr(*s, OR_SIGNATURE_END_TAG);
  275. if (!next) { tok->val.error = "No signature end tag found"; return -1; }
  276. signature = tor_malloc(256);
  277. i = base64_decode(signature, 256, *s, next-*s);
  278. if (i<0) {
  279. free(signature);
  280. tok->val.error = "Error decoding signature."; return -1;
  281. } else if (i != 128) {
  282. free(signature);
  283. tok->val.error = "Bad length on decoded signature."; return -1;
  284. }
  285. tok->tp = _SIGNATURE;
  286. tok->val.signature = signature;
  287. next = strchr(next, '\n'); /* Part of OR_SIGNATURE_END_TAG; can't fail.*/
  288. *s = next+1;
  289. return 0;
  290. } else {
  291. tok->val.error = "Unrecognized begin line"; return -1;
  292. }
  293. } else {
  294. next = find_whitespace(*s);
  295. if (!next) {
  296. tok->val.error = "Unexpected EOF"; return -1;
  297. }
  298. for (i = 0 ; token_table[i].t ; ++i) {
  299. if (!strncmp(token_table[i].t, *s, next-*s)) {
  300. tok->tp = token_table[i].v;
  301. i = 0;
  302. done = (*next == '\n');
  303. *s = eat_whitespace_no_nl(next);
  304. while (**s != '\n' && i <= MAX_ARGS && !done) {
  305. next = find_whitespace(*s);
  306. if (*next == '\n')
  307. done = 1;
  308. *next = 0;
  309. tok->val.cmd.args[i++] = *s;
  310. *s = eat_whitespace_no_nl(next+1);
  311. };
  312. tok->val.cmd.n_args = i;
  313. if (i > MAX_ARGS) {
  314. tok->tp = _ERR;
  315. tok->val.error = "Too many arguments"; return -1;
  316. }
  317. return 0;
  318. }
  319. }
  320. tok->val.error = "Unrecognized command"; return -1;
  321. }
  322. }
  323. #ifdef DEBUG_ROUTER_TOKENS
  324. static void
  325. router_dump_token(directory_token_t *tok) {
  326. int i;
  327. switch(tok->tp)
  328. {
  329. case _SIGNATURE:
  330. puts("(signature)");
  331. return;
  332. case _PUBLIC_KEY:
  333. puts("(public key)");
  334. return;
  335. case _ERR:
  336. printf("(Error: %s\n)", tok->val.error);
  337. return;
  338. case _EOF:
  339. puts("EOF");
  340. return;
  341. case K_ACCEPT: printf("Accept"); break;
  342. case K_DIRECTORY_SIGNATURE: printf("Directory-Signature"); break;
  343. case K_REJECT: printf("Reject"); break;
  344. case K_RECOMMENDED_SOFTWARE: printf("Server-Software"); break;
  345. case K_ROUTER: printf("Router"); break;
  346. case K_SIGNED_DIRECTORY: printf("Signed-Directory"); break;
  347. case K_SIGNING_KEY: printf("Signing-Key"); break;
  348. case K_ONION_KEY: printf("Onion-key"); break;
  349. case K_LINK_KEY: printf("Link-key"); break;
  350. case K_ROUTER_SIGNATURE: printf("Router-signature"); break;
  351. case K_PUBLISHED: printf("Published"); break;
  352. case K_RUNNING_ROUTERS: printf("Running-routers"); break;
  353. case K_PLATFORM: printf("Platform"); break;
  354. default:
  355. printf("?????? %d\n", tok->tp); return;
  356. }
  357. for (i = 0; i < tok->val.cmd.n_args; ++i) {
  358. printf(" \"%s\"", tok->val.cmd.args[i]);
  359. }
  360. printf("\n");
  361. return;
  362. }
  363. static int
  364. router_get_next_token(char **s, directory_token_t *tok) {
  365. int i;
  366. i = _router_get_next_token(s, tok);
  367. router_dump_token(tok);
  368. return i;
  369. }
  370. #else
  371. #define router_get_next_token _router_get_next_token
  372. #endif
  373. int router_get_list_from_string(char *s)
  374. {
  375. if (router_get_list_from_string_impl(&s, &directory, -1, NULL)) {
  376. log(LOG_WARN, "Error parsing router file");
  377. return -1;
  378. }
  379. if (router_resolve_directory(directory)) {
  380. log(LOG_WARN, "Error resolving directory");
  381. return -1;
  382. }
  383. return 0;
  384. }
  385. static int router_get_hash_impl(char *s, char *digest, const char *start_str,
  386. const char *end_str)
  387. {
  388. char *start, *end;
  389. start = strstr(s, start_str);
  390. if (!start) {
  391. log_fn(LOG_WARN,"couldn't find \"%s\"",start_str);
  392. return -1;
  393. }
  394. end = strstr(start+strlen(start_str), end_str);
  395. if (!end) {
  396. log_fn(LOG_WARN,"couldn't find \"%s\"",end_str);
  397. return -1;
  398. }
  399. end = strchr(end, '\n');
  400. if (!end) {
  401. log_fn(LOG_WARN,"couldn't find EOL");
  402. return -1;
  403. }
  404. ++end;
  405. if (crypto_SHA_digest(start, end-start, digest)) {
  406. log_fn(LOG_WARN,"couldn't compute digest");
  407. return -1;
  408. }
  409. return 0;
  410. }
  411. int router_get_dir_hash(char *s, char *digest)
  412. {
  413. return router_get_hash_impl(s,digest,
  414. "signed-directory","directory-signature");
  415. }
  416. int router_get_router_hash(char *s, char *digest)
  417. {
  418. return router_get_hash_impl(s,digest,
  419. "router ","router-signature");
  420. }
  421. /* return 0 if myversion is in start. Else return -1. */
  422. int compare_recommended_versions(char *myversion, char *start) {
  423. int len_myversion = strlen(myversion);
  424. char *comma;
  425. char *end = start + strlen(start);
  426. log_fn(LOG_DEBUG,"checking '%s' in '%s'.", myversion, start);
  427. for(;;) {
  428. comma = strchr(start, ',');
  429. if( ((comma ? comma : end) - start == len_myversion) &&
  430. !strncmp(start, myversion, len_myversion)) /* only do strncmp if the length matches */
  431. return 0; /* success, it's there */
  432. if(!comma)
  433. return -1; /* nope */
  434. start = comma+1;
  435. }
  436. }
  437. int router_get_dir_from_string(char *s, crypto_pk_env_t *pkey)
  438. {
  439. if (router_get_dir_from_string_impl(s, &directory, pkey)) {
  440. log_fn(LOG_WARN, "Couldn't parse directory.");
  441. return -1;
  442. }
  443. if (router_resolve_directory(directory)) {
  444. log_fn(LOG_WARN, "Error resolving directory");
  445. return -1;
  446. }
  447. if (compare_recommended_versions(VERSION, directory->software_versions) < 0) {
  448. log(options.IgnoreVersion ? LOG_WARN : LOG_ERR,
  449. "You are running Tor version %s, which is not recommended.\n"
  450. "Please upgrade to one of %s.",
  451. VERSION, directory->software_versions);
  452. if(options.IgnoreVersion) {
  453. log(LOG_WARN, "IgnoreVersion is set. If it breaks, we told you so.");
  454. } else {
  455. fflush(0);
  456. exit(0);
  457. }
  458. }
  459. return 0;
  460. }
  461. int router_get_dir_from_string_impl(char *s, directory_t **dest,
  462. crypto_pk_env_t *pkey)
  463. {
  464. directory_token_t tok;
  465. char digest[20];
  466. char signed_digest[128];
  467. directory_t *new_dir = NULL;
  468. char *versions;
  469. struct tm published;
  470. time_t published_on;
  471. const char *good_nickname_lst[1024];
  472. int n_good_nicknames;
  473. #define NEXT_TOK() \
  474. do { \
  475. if (router_get_next_token(&s, &tok)) { \
  476. log_fn(LOG_WARN, "Error reading directory: %s", tok.val.error);\
  477. return -1; \
  478. } } while (0)
  479. #define TOK_IS(type,name) \
  480. do { \
  481. if (tok.tp != type) { \
  482. router_release_token(&tok); \
  483. log_fn(LOG_WARN, "Error reading directory: expected %s", name);\
  484. return -1; \
  485. } } while(0)
  486. if (router_get_dir_hash(s, digest)) {
  487. log_fn(LOG_WARN, "Unable to compute digest of directory");
  488. goto err;
  489. }
  490. log(LOG_DEBUG,"Received directory hashes to %02x:%02x:%02x:%02x",
  491. ((int)digest[0])&0xff,((int)digest[1])&0xff,
  492. ((int)digest[2])&0xff,((int)digest[3])&0xff);
  493. NEXT_TOK();
  494. TOK_IS(K_SIGNED_DIRECTORY, "signed-directory");
  495. NEXT_TOK();
  496. TOK_IS(K_PUBLISHED, "published");
  497. if (tok.val.cmd.n_args != 2) {
  498. log_fn(LOG_WARN, "Invalid published line");
  499. goto err;
  500. }
  501. tok.val.cmd.args[1][-1] = ' ';
  502. if (!strptime(tok.val.cmd.args[0], "%Y-%m-%d %H:%M:%S", &published)) {
  503. log_fn(LOG_WARN, "Published time was unparseable"); goto err;
  504. }
  505. published_on = tor_timegm(&published);
  506. NEXT_TOK();
  507. TOK_IS(K_RECOMMENDED_SOFTWARE, "recommended-software");
  508. if (tok.val.cmd.n_args != 1) {
  509. log_fn(LOG_WARN, "Invalid recommended-software line");
  510. goto err;
  511. }
  512. versions = tor_strdup(tok.val.cmd.args[0]);
  513. NEXT_TOK();
  514. TOK_IS(K_RUNNING_ROUTERS, "running-routers");
  515. n_good_nicknames = tok.val.cmd.n_args;
  516. memcpy(good_nickname_lst, tok.val.cmd.args, n_good_nicknames*sizeof(char *));
  517. if (router_get_list_from_string_impl(&s, &new_dir,
  518. n_good_nicknames, good_nickname_lst)) {
  519. log_fn(LOG_WARN, "Error reading routers from directory");
  520. goto err;
  521. }
  522. new_dir->software_versions = versions;
  523. new_dir->published_on = published_on;
  524. NEXT_TOK();
  525. TOK_IS(K_DIRECTORY_SIGNATURE, "directory-signature");
  526. NEXT_TOK();
  527. TOK_IS(_SIGNATURE, "signature");
  528. if (pkey) {
  529. if (crypto_pk_public_checksig(pkey, tok.val.signature, 128, signed_digest)
  530. != 20) {
  531. log_fn(LOG_WARN, "Error reading directory: invalid signature.");
  532. free(tok.val.signature);
  533. goto err;
  534. }
  535. log(LOG_DEBUG,"Signed directory hash starts %02x:%02x:%02x:%02x",
  536. ((int)signed_digest[0])&0xff,((int)signed_digest[1])&0xff,
  537. ((int)signed_digest[2])&0xff,((int)signed_digest[3])&0xff);
  538. if (memcmp(digest, signed_digest, 20)) {
  539. log_fn(LOG_WARN, "Error reading directory: signature does not match.");
  540. free(tok.val.signature);
  541. goto err;
  542. }
  543. }
  544. free(tok.val.signature);
  545. NEXT_TOK();
  546. TOK_IS(_EOF, "end of directory");
  547. if (*dest)
  548. directory_free(*dest);
  549. *dest = new_dir;
  550. return 0;
  551. err:
  552. if (new_dir)
  553. directory_free(new_dir);
  554. return -1;
  555. #undef NEXT_TOK
  556. #undef TOK_IS
  557. }
  558. int router_get_list_from_string_impl(char **s, directory_t **dest,
  559. int n_good_nicknames,
  560. const char **good_nickname_lst)
  561. {
  562. routerinfo_t *router;
  563. routerinfo_t **rarray;
  564. int rarray_len = 0;
  565. int i;
  566. assert(s && *s);
  567. rarray = (routerinfo_t **)tor_malloc((sizeof(routerinfo_t *))*MAX_ROUTERS_IN_DIR);
  568. while (1) {
  569. *s = eat_whitespace(*s);
  570. if (strncmp(*s, "router ", 7)!=0)
  571. break;
  572. router = router_get_entry_from_string(s);
  573. if (!router) {
  574. log_fn(LOG_WARN, "Error reading router");
  575. for(i=0;i<rarray_len;i++)
  576. routerinfo_free(rarray[i]);
  577. free(rarray);
  578. return -1;
  579. }
  580. if (rarray_len >= MAX_ROUTERS_IN_DIR) {
  581. log_fn(LOG_WARN, "too many routers");
  582. routerinfo_free(router);
  583. continue;
  584. }
  585. if (n_good_nicknames>=0) {
  586. router->is_running = 0;
  587. for (i = 0; i < n_good_nicknames; ++i) {
  588. if (0==strcasecmp(good_nickname_lst[i], router->nickname)) {
  589. router->is_running = 1;
  590. break;
  591. }
  592. }
  593. } else {
  594. router->is_running = 1; /* start out assuming all dirservers are up */
  595. }
  596. rarray[rarray_len++] = router;
  597. log_fn(LOG_DEBUG,"just added router #%d.",rarray_len);
  598. }
  599. if (*dest)
  600. directory_free(*dest);
  601. *dest = (directory_t *)tor_malloc(sizeof(directory_t));
  602. (*dest)->routers = rarray;
  603. (*dest)->n_routers = rarray_len;
  604. (*dest)->software_versions = NULL;
  605. return 0;
  606. }
  607. static int
  608. router_resolve(routerinfo_t *router)
  609. {
  610. struct hostent *rent;
  611. rent = (struct hostent *)gethostbyname(router->address);
  612. if (!rent) {
  613. log_fn(LOG_WARN,"Could not get address for router %s.",router->address);
  614. return -1;
  615. }
  616. assert(rent->h_length == 4);
  617. memcpy(&router->addr, rent->h_addr,rent->h_length);
  618. router->addr = ntohl(router->addr); /* get it back into host order */
  619. return 0;
  620. }
  621. static int
  622. router_resolve_directory(directory_t *dir)
  623. {
  624. int i, max, remove;
  625. if (!dir)
  626. dir = directory;
  627. max = dir->n_routers;
  628. for (i = 0; i < max; ++i) {
  629. remove = 0;
  630. if (router_resolve(dir->routers[i])) {
  631. log_fn(LOG_WARN, "Couldn't resolve router %s; not using",
  632. dir->routers[i]->address);
  633. remove = 1;
  634. } else if (options.Nickname &&
  635. !strcmp(dir->routers[i]->nickname, options.Nickname)) {
  636. remove = 1;
  637. }
  638. if (remove) {
  639. routerinfo_free(dir->routers[i]);
  640. dir->routers[i] = dir->routers[--max];
  641. --dir->n_routers;
  642. --i;
  643. }
  644. }
  645. return 0;
  646. }
  647. /* reads a single router entry from s.
  648. * updates s so it points to after the router it just read.
  649. * mallocs a new router, returns it if all goes well, else returns NULL.
  650. */
  651. routerinfo_t *router_get_entry_from_string(char**s) {
  652. routerinfo_t *router = NULL;
  653. char signed_digest[128];
  654. char digest[128];
  655. directory_token_t _tok;
  656. directory_token_t *tok = &_tok;
  657. struct tm published;
  658. int t;
  659. #define NEXT_TOKEN() \
  660. do { if (router_get_next_token(s, tok)) { \
  661. log_fn(LOG_WARN, "Error reading directory: %s", tok->val.error);\
  662. goto err; \
  663. } } while(0)
  664. #define ARGS tok->val.cmd.args
  665. if (router_get_router_hash(*s, digest) < 0) {
  666. log_fn(LOG_WARN, "Couldn't compute router hash.");
  667. return NULL;
  668. }
  669. NEXT_TOKEN();
  670. if (tok->tp != K_ROUTER) {
  671. router_release_token(tok);
  672. log_fn(LOG_WARN,"Entry does not start with \"router\"");
  673. return NULL;
  674. }
  675. router = tor_malloc_zero(sizeof(routerinfo_t));
  676. router->onion_pkey = router->identity_pkey = router->link_pkey = NULL;
  677. if (tok->val.cmd.n_args != 6) {
  678. log_fn(LOG_WARN,"Wrong # of arguments to \"router\"");
  679. goto err;
  680. }
  681. router->nickname = tor_strdup(ARGS[0]);
  682. if (strlen(router->nickname) > MAX_NICKNAME_LEN) {
  683. log_fn(LOG_WARN,"Router nickname too long.");
  684. goto err;
  685. }
  686. if (strspn(router->nickname, LEGAL_NICKNAME_CHARACTERS) !=
  687. strlen(router->nickname)) {
  688. log_fn(LOG_WARN, "Router nickname contains illegal characters.");
  689. goto err;
  690. }
  691. /* read router.address */
  692. router->address = tor_strdup(ARGS[1]);
  693. router->addr = 0;
  694. /* Read router->or_port */
  695. router->or_port = atoi(ARGS[2]);
  696. if(!router->or_port) {
  697. log_fn(LOG_WARN,"or_port unreadable or 0. Failing.");
  698. goto err;
  699. }
  700. /* Router->socks_port */
  701. router->socks_port = atoi(ARGS[3]);
  702. /* Router->dir_port */
  703. router->dir_port = atoi(ARGS[4]);
  704. /* Router->bandwidth */
  705. router->bandwidth = atoi(ARGS[5]);
  706. if (!router->bandwidth) {
  707. log_fn(LOG_WARN,"bandwidth unreadable or 0. Failing.");
  708. goto err;
  709. }
  710. log_fn(LOG_DEBUG,"or_port %d, socks_port %d, dir_port %d, bandwidth %d.",
  711. router->or_port, router->socks_port, router->dir_port, router->bandwidth);
  712. /* XXX Later, require platform before published. */
  713. NEXT_TOKEN();
  714. if (tok->tp == K_PLATFORM) {
  715. NEXT_TOKEN();
  716. }
  717. if (tok->tp != K_PUBLISHED) {
  718. log_fn(LOG_WARN, "Missing published time"); goto err;
  719. }
  720. if (tok->val.cmd.n_args != 2) {
  721. log_fn(LOG_WARN, "Wrong number of arguments to published"); goto err;
  722. }
  723. ARGS[1][-1] = ' '; /* Re-insert space. */
  724. if (!strptime(ARGS[0], "%Y-%m-%d %H:%M:%S", &published)) {
  725. log_fn(LOG_WARN, "Published time was unparseable"); goto err;
  726. }
  727. router->published_on = tor_timegm(&published);
  728. NEXT_TOKEN();
  729. if (tok->tp != K_ONION_KEY) {
  730. log_fn(LOG_WARN, "Missing onion-key"); goto err;
  731. }
  732. NEXT_TOKEN();
  733. if (tok->tp != _PUBLIC_KEY) {
  734. log_fn(LOG_WARN, "Missing onion key"); goto err;
  735. } /* XXX Check key length */
  736. router->onion_pkey = tok->val.public_key;
  737. NEXT_TOKEN();
  738. if (tok->tp != K_LINK_KEY) {
  739. log_fn(LOG_WARN, "Missing link-key"); goto err;
  740. }
  741. NEXT_TOKEN();
  742. if (tok->tp != _PUBLIC_KEY) {
  743. log_fn(LOG_WARN, "Missing link key"); goto err;
  744. } /* XXX Check key length */
  745. router->link_pkey = tok->val.public_key;
  746. NEXT_TOKEN();
  747. if (tok->tp != K_SIGNING_KEY) {
  748. log_fn(LOG_WARN, "Missing signing-key"); goto err;
  749. }
  750. NEXT_TOKEN();
  751. if (tok->tp != _PUBLIC_KEY) {
  752. log_fn(LOG_WARN, "Missing signing key"); goto err;
  753. }
  754. router->identity_pkey = tok->val.public_key;
  755. NEXT_TOKEN();
  756. while (tok->tp == K_ACCEPT || tok->tp == K_REJECT) {
  757. router_add_exit_policy(router, tok);
  758. NEXT_TOKEN();
  759. }
  760. if (tok->tp != K_ROUTER_SIGNATURE) {
  761. log_fn(LOG_WARN,"Missing router signature");
  762. goto err;
  763. }
  764. NEXT_TOKEN();
  765. if (tok->tp != _SIGNATURE) {
  766. log_fn(LOG_WARN,"Missing router signature");
  767. goto err;
  768. }
  769. assert (router->identity_pkey);
  770. if ((t=crypto_pk_public_checksig(router->identity_pkey, tok->val.signature,
  771. 128, signed_digest)) != 20) {
  772. log_fn(LOG_WARN, "Invalid signature %d",t);
  773. goto err;
  774. }
  775. if (memcmp(digest, signed_digest, 20)) {
  776. log_fn(LOG_WARN, "Mismatched signature");
  777. goto err;
  778. }
  779. router_release_token(tok); /* free the signature */
  780. return router;
  781. err:
  782. router_release_token(tok);
  783. routerinfo_free(router);
  784. return NULL;
  785. #undef ARGS
  786. #undef NEXT_TOKEN
  787. }
  788. void router_add_exit_policy_from_config(routerinfo_t *router) {
  789. char *s = options.ExitPolicy, *e;
  790. int last=0;
  791. char line[1024];
  792. if(!s) {
  793. log_fn(LOG_INFO,"No exit policy configured. Ok.");
  794. return; /* nothing to see here */
  795. }
  796. if(!*s) {
  797. log_fn(LOG_INFO,"Exit policy is empty. Ok.");
  798. return; /* nothing to see here */
  799. }
  800. for(;;) {
  801. e = strchr(s,',');
  802. if(!e) {
  803. last = 1;
  804. strncpy(line,s,1023);
  805. } else {
  806. memcpy(line,s, ((e-s)<1023)?(e-s):1023);
  807. line[e-s] = 0;
  808. }
  809. line[1023]=0;
  810. log_fn(LOG_DEBUG,"Adding new entry '%s'",line);
  811. if(router_add_exit_policy_from_string(router,line) < 0)
  812. log_fn(LOG_WARN,"Malformed exit policy %s; skipping.", line);
  813. if(last)
  814. return;
  815. s = e+1;
  816. }
  817. }
  818. static int
  819. router_add_exit_policy_from_string(routerinfo_t *router,
  820. char *s)
  821. {
  822. directory_token_t tok;
  823. char *tmp, *cp;
  824. int r;
  825. int len, idx;
  826. len = strlen(s);
  827. tmp = cp = tor_malloc(len+2);
  828. for (idx = 0; idx < len; ++idx) {
  829. tmp[idx] = tolower(s[idx]);
  830. }
  831. tmp[len]='\n';
  832. tmp[len+1]='\0';
  833. if (router_get_next_token(&cp, &tok)) {
  834. log_fn(LOG_WARN, "Error reading exit policy: %s", tok.val.error);
  835. free(tmp);
  836. return -1;
  837. }
  838. if (tok.tp != K_ACCEPT && tok.tp != K_REJECT) {
  839. log_fn(LOG_WARN, "Expected 'accept' or 'reject'.");
  840. free(tmp);
  841. return -1;
  842. }
  843. r = router_add_exit_policy(router, &tok);
  844. free(tmp);
  845. return r;
  846. }
  847. static int router_add_exit_policy(routerinfo_t *router,
  848. directory_token_t *tok) {
  849. struct exit_policy_t *tmpe, *newe;
  850. struct in_addr in;
  851. char *arg, *address, *mask, *port, *endptr;
  852. int bits;
  853. if (tok->val.cmd.n_args != 1)
  854. return -1;
  855. arg = tok->val.cmd.args[0];
  856. newe = tor_malloc_zero(sizeof(struct exit_policy_t));
  857. newe->string = tor_malloc(8+strlen(arg));
  858. if (tok->tp == K_REJECT) {
  859. strcpy(newe->string, "reject ");
  860. newe->policy_type = EXIT_POLICY_REJECT;
  861. } else {
  862. assert(tok->tp == K_ACCEPT);
  863. strcpy(newe->string, "accept ");
  864. newe->policy_type = EXIT_POLICY_ACCEPT;
  865. }
  866. strcat(newe->string, arg);
  867. address = arg;
  868. mask = strchr(arg,'/');
  869. port = strchr(mask?mask:arg,':');
  870. if(!port)
  871. goto policy_read_failed;
  872. if (mask)
  873. *mask++ = 0;
  874. *port++ = 0;
  875. if (strcmp(address, "*") == 0) {
  876. newe->addr = 0;
  877. } else if (inet_aton(address, &in) != 0) {
  878. newe->addr = ntohl(in.s_addr);
  879. } else {
  880. log_fn(LOG_WARN, "Malformed IP %s in exit policy; rejecting.",
  881. address);
  882. goto policy_read_failed;
  883. }
  884. if (!mask) {
  885. if (strcmp(address, "*") == 0)
  886. newe->msk = 0;
  887. else
  888. newe->msk = 0xFFFFFFFFu;
  889. } else {
  890. endptr = NULL;
  891. bits = (int) strtol(mask, &endptr, 10);
  892. if (!*endptr) {
  893. /* strtol handled the whole mask. */
  894. newe->msk = ~((1<<(32-bits))-1);
  895. } else if (inet_aton(mask, &in) != 0) {
  896. newe->msk = ntohl(in.s_addr);
  897. } else {
  898. log_fn(LOG_WARN, "Malformed mask %s on exit policy; rejecting.",
  899. mask);
  900. goto policy_read_failed;
  901. }
  902. }
  903. if (strcmp(port, "*") == 0) {
  904. newe->prt = 0;
  905. } else {
  906. endptr = NULL;
  907. newe->prt = strtol(port, &endptr, 10);
  908. if (*endptr) {
  909. log_fn(LOG_WARN, "Malformed port %s on exit policy; rejecting.",
  910. port);
  911. goto policy_read_failed;
  912. }
  913. }
  914. in.s_addr = htonl(newe->addr);
  915. address = tor_strdup(inet_ntoa(in));
  916. in.s_addr = htonl(newe->msk);
  917. log_fn(LOG_DEBUG,"%s %s/%s:%d",
  918. newe->policy_type == EXIT_POLICY_REJECT ? "reject" : "accept",
  919. address, inet_ntoa(in), newe->prt);
  920. tor_free(address);
  921. /* now link newe onto the end of exit_policy */
  922. if(!router->exit_policy) {
  923. router->exit_policy = newe;
  924. return 0;
  925. }
  926. for(tmpe=router->exit_policy; tmpe->next; tmpe=tmpe->next) ;
  927. tmpe->next = newe;
  928. return 0;
  929. policy_read_failed:
  930. assert(newe->string);
  931. log_fn(LOG_WARN,"Couldn't parse line '%s'. Dropping", newe->string);
  932. tor_free(newe->string);
  933. free(newe);
  934. return -1;
  935. }
  936. /* Addr is 0 for "IP unknown".
  937. *
  938. * Returns -1 for 'rejected', 0 for accepted, 1 for 'maybe' (since IP is
  939. * unknown.
  940. */
  941. int router_supports_exit_address(uint32_t addr, uint16_t port,
  942. routerinfo_t *router)
  943. {
  944. return router_compare_addr_to_exit_policy(addr, port, router->exit_policy);
  945. }
  946. /* Addr is 0 for "IP unknown".
  947. *
  948. * Returns -1 for 'rejected', 0 for accepted, 1 for 'maybe' (since IP is
  949. * unknown.
  950. */
  951. int router_compare_addr_to_exit_policy(uint32_t addr, uint16_t port,
  952. struct exit_policy_t *policy)
  953. {
  954. int maybe_reject = 0;
  955. int match = 0;
  956. struct in_addr in;
  957. struct exit_policy_t *tmpe;
  958. for(tmpe=policy; tmpe; tmpe=tmpe->next) {
  959. log_fn(LOG_DEBUG,"Considering exit policy %s", tmpe->string);
  960. if (!addr) {
  961. /* Address is unknown. */
  962. if (tmpe->msk == 0 && (!tmpe || port == tmpe->prt)) {
  963. /* The exit policy is accept/reject *:port */
  964. match = 1;
  965. } else if ((!tmpe->prt || port == tmpe->prt) &&
  966. tmpe->policy_type == EXIT_POLICY_REJECT) {
  967. /* The exit policy is reject ???:port */
  968. maybe_reject = 1;
  969. }
  970. } else {
  971. /* Address is known */
  972. if ( (addr & tmpe->msk) == (tmpe->addr & tmpe->msk) &&
  973. (!tmpe->prt || port == tmpe->prt) ) {
  974. /* Exact match for the policy */
  975. match = 1;
  976. }
  977. }
  978. if (match) {
  979. in.s_addr = htonl(addr);
  980. log_fn(LOG_INFO,"Address %s:%d matches exit policy '%s'",
  981. inet_ntoa(in), port, tmpe->string);
  982. if(tmpe->policy_type == EXIT_POLICY_ACCEPT)
  983. return 0;
  984. else
  985. return -1;
  986. }
  987. }
  988. if (maybe_reject)
  989. return 1;
  990. else
  991. return 0; /* accept all by default. */
  992. }
  993. /* Return 0 if my exit policy says to allow connection to conn.
  994. * Else return -1.
  995. */
  996. int router_compare_to_my_exit_policy(connection_t *conn) {
  997. assert(desc_routerinfo);
  998. assert(conn->addr); /* make sure it's resolved to something. this
  999. way we can't get a 'maybe' below. */
  1000. if (router_compare_addr_to_exit_policy(conn->addr, conn->port,
  1001. desc_routerinfo->exit_policy) == 0)
  1002. return 0;
  1003. else
  1004. return -1;
  1005. }
  1006. /* return 1 if all running routers will reject addr:port, return 0 if
  1007. any might accept it. */
  1008. int router_exit_policy_all_routers_reject(uint32_t addr, uint16_t port) {
  1009. int i;
  1010. routerinfo_t *router;
  1011. for (i=0;i<directory->n_routers;i++) {
  1012. router = directory->routers[i];
  1013. if (router->is_running && router_compare_addr_to_exit_policy(addr,
  1014. port, router->exit_policy) >= 0)
  1015. return 0; /* this one could be ok. good enough. */
  1016. }
  1017. return 1; /* all will reject. */
  1018. }
  1019. int router_exit_policy_rejects_all(routerinfo_t *router) {
  1020. if (router_compare_addr_to_exit_policy(0, 0, router->exit_policy) < 0)
  1021. return 1; /* yes, rejects all */
  1022. else
  1023. return 0; /* no, might accept some */
  1024. }
  1025. const char *router_get_my_descriptor(void) {
  1026. if (!desc_routerinfo) {
  1027. if (router_rebuild_descriptor())
  1028. return NULL;
  1029. }
  1030. log_fn(LOG_DEBUG,"my desc is '%s'",descriptor);
  1031. return descriptor;
  1032. }
  1033. const routerinfo_t *router_get_desc_routerinfo(void) {
  1034. if (!desc_routerinfo) {
  1035. if (router_rebuild_descriptor())
  1036. return NULL;
  1037. }
  1038. return desc_routerinfo;
  1039. }
  1040. int router_rebuild_descriptor(void) {
  1041. routerinfo_t *ri;
  1042. char localhostname[256];
  1043. char *address = options.Address;
  1044. if(!address) { /* if not specified in config, we find a default */
  1045. if(gethostname(localhostname,sizeof(localhostname)) < 0) {
  1046. log_fn(LOG_WARN,"Error obtaining local hostname");
  1047. return -1;
  1048. }
  1049. address = localhostname;
  1050. if(!strchr(address,'.')) {
  1051. log_fn(LOG_WARN,"fqdn '%s' has only one element. Misconfigured machine?",address);
  1052. log_fn(LOG_WARN,"Try setting the Address line in your config file.");
  1053. return -1;
  1054. }
  1055. }
  1056. ri = tor_malloc(sizeof(routerinfo_t));
  1057. ri->address = tor_strdup(address);
  1058. ri->nickname = tor_strdup(options.Nickname);
  1059. /* No need to set addr. */
  1060. ri->or_port = options.ORPort;
  1061. ri->socks_port = options.SocksPort;
  1062. ri->dir_port = options.DirPort;
  1063. ri->published_on = time(NULL);
  1064. ri->onion_pkey = crypto_pk_dup_key(get_onion_key());
  1065. ri->link_pkey = crypto_pk_dup_key(get_link_key());
  1066. ri->identity_pkey = crypto_pk_dup_key(get_identity_key());
  1067. ri->bandwidth = options.TotalBandwidth;
  1068. ri->exit_policy = NULL; /* zero it out first */
  1069. router_add_exit_policy_from_config(ri);
  1070. if (desc_routerinfo)
  1071. routerinfo_free(desc_routerinfo);
  1072. desc_routerinfo = ri;
  1073. if (router_dump_router_to_string(descriptor, 8192, ri, get_identity_key())<0) {
  1074. log_fn(LOG_WARN, "Couldn't dump router to string.");
  1075. return -1;
  1076. }
  1077. return 0;
  1078. }
  1079. static void get_platform_str(char *platform, int len)
  1080. {
  1081. snprintf(platform, len-1, "Tor %s on %s", VERSION, get_uname());
  1082. platform[len-1] = '\0';
  1083. return;
  1084. }
  1085. /* XXX need to audit this thing and count fenceposts. maybe
  1086. * refactor so we don't have to keep asking if we're
  1087. * near the end of maxlen?
  1088. */
  1089. #define DEBUG_ROUTER_DUMP_ROUTER_TO_STRING
  1090. int router_dump_router_to_string(char *s, int maxlen, routerinfo_t *router,
  1091. crypto_pk_env_t *ident_key) {
  1092. char *onion_pkey;
  1093. char *link_pkey;
  1094. char *identity_pkey;
  1095. struct in_addr in;
  1096. char platform[256];
  1097. char digest[20];
  1098. char signature[128];
  1099. char published[32];
  1100. int onion_pkeylen, link_pkeylen, identity_pkeylen;
  1101. int written;
  1102. int result=0;
  1103. struct exit_policy_t *tmpe;
  1104. #ifdef DEBUG_ROUTER_DUMP_ROUTER_TO_STRING
  1105. char *s_tmp, *s_dup;
  1106. routerinfo_t *ri_tmp;
  1107. #endif
  1108. get_platform_str(platform, sizeof(platform));
  1109. if (crypto_pk_cmp_keys(ident_key, router->identity_pkey)) {
  1110. log_fn(LOG_WARN,"Tried to sign a router with a private key that didn't match router's public key!");
  1111. return -1;
  1112. }
  1113. if(crypto_pk_write_public_key_to_string(router->onion_pkey,
  1114. &onion_pkey,&onion_pkeylen)<0) {
  1115. log_fn(LOG_WARN,"write onion_pkey to string failed!");
  1116. return -1;
  1117. }
  1118. if(crypto_pk_write_public_key_to_string(router->identity_pkey,
  1119. &identity_pkey,&identity_pkeylen)<0) {
  1120. log_fn(LOG_WARN,"write identity_pkey to string failed!");
  1121. return -1;
  1122. }
  1123. if(crypto_pk_write_public_key_to_string(router->link_pkey,
  1124. &link_pkey,&link_pkeylen)<0) {
  1125. log_fn(LOG_WARN,"write link_pkey to string failed!");
  1126. return -1;
  1127. }
  1128. strftime(published, 32, "%Y-%m-%d %H:%M:%S", gmtime(&router->published_on));
  1129. result = snprintf(s, maxlen,
  1130. "router %s %s %d %d %d %d\n"
  1131. "platform %s\n"
  1132. "published %s\n"
  1133. "onion-key\n%s"
  1134. "link-key\n%s"
  1135. "signing-key\n%s",
  1136. router->nickname,
  1137. router->address,
  1138. router->or_port,
  1139. router->socks_port,
  1140. router->dir_port,
  1141. router->bandwidth,
  1142. platform,
  1143. published,
  1144. onion_pkey, link_pkey, identity_pkey);
  1145. free(onion_pkey);
  1146. free(link_pkey);
  1147. free(identity_pkey);
  1148. if(result < 0 || result >= maxlen) {
  1149. /* apparently different glibcs do different things on snprintf error.. so check both */
  1150. return -1;
  1151. }
  1152. written = result;
  1153. for(tmpe=router->exit_policy; tmpe; tmpe=tmpe->next) {
  1154. in.s_addr = htonl(tmpe->addr);
  1155. result = snprintf(s+written, maxlen-written, "%s %s",
  1156. tmpe->policy_type == EXIT_POLICY_ACCEPT ? "accept" : "reject",
  1157. tmpe->msk == 0 ? "*" : inet_ntoa(in));
  1158. if(result < 0 || result+written > maxlen) {
  1159. /* apparently different glibcs do different things on snprintf error.. so check both */
  1160. return -1;
  1161. }
  1162. written += result;
  1163. if (tmpe->msk != 0xFFFFFFFFu && tmpe->msk != 0) {
  1164. in.s_addr = htonl(tmpe->msk);
  1165. result = snprintf(s+written, maxlen-written, "/%s", inet_ntoa(in));
  1166. if (result<0 || result+written > maxlen)
  1167. return -1;
  1168. written += result;
  1169. }
  1170. if (tmpe->prt) {
  1171. result = snprintf(s+written, maxlen-written, ":%d\n", tmpe->prt);
  1172. if (result<0 || result+written > maxlen)
  1173. return -1;
  1174. written += result;
  1175. } else {
  1176. if (written > maxlen-4)
  1177. return -1;
  1178. strcat(s+written, ":*\n");
  1179. written += 3;
  1180. }
  1181. } /* end for */
  1182. if (written > maxlen-256) /* Not enough room for signature. */
  1183. return -1;
  1184. strcat(s+written, "router-signature\n");
  1185. written += strlen(s+written);
  1186. s[written] = '\0';
  1187. if (router_get_router_hash(s, digest) < 0)
  1188. return -1;
  1189. if (crypto_pk_private_sign(ident_key, digest, 20, signature) < 0) {
  1190. log_fn(LOG_WARN, "Error signing digest");
  1191. return -1;
  1192. }
  1193. strcat(s+written, "-----BEGIN SIGNATURE-----\n");
  1194. written += strlen(s+written);
  1195. if (base64_encode(s+written, maxlen-written, signature, 128) < 0) {
  1196. log_fn(LOG_WARN, "Couldn't base64-encode signature");
  1197. return -1;
  1198. }
  1199. written += strlen(s+written);
  1200. strcat(s+written, "-----END SIGNATURE-----\n");
  1201. written += strlen(s+written);
  1202. if (written > maxlen-2)
  1203. return -1;
  1204. /* include a last '\n' */
  1205. s[written] = '\n';
  1206. s[written+1] = 0;
  1207. #ifdef DEBUG_ROUTER_DUMP_ROUTER_TO_STRING
  1208. s_tmp = s_dup = tor_strdup(s);
  1209. ri_tmp = router_get_entry_from_string(&s_tmp);
  1210. if (!ri_tmp) {
  1211. log_fn(LOG_ERR, "We just generated a router descriptor we can't parse: <<%s>>",
  1212. s);
  1213. return -1;
  1214. }
  1215. free(s_dup);
  1216. routerinfo_free(ri_tmp);
  1217. #endif
  1218. return written+1;
  1219. }
  1220. /*
  1221. Local Variables:
  1222. mode:c
  1223. indent-tabs-mode:nil
  1224. c-basic-offset:2
  1225. End:
  1226. */