routers.c 29 KB

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