routers.c 32 KB

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