routers.c 26 KB

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