routers.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  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. e = router->exit_policy;
  123. while (e) {
  124. etmp = e->next;
  125. if (e->string) free(e->string);
  126. if (e->address) free(e->address);
  127. if (e->port) free(e->port);
  128. free(e);
  129. e = etmp;
  130. }
  131. free(router);
  132. }
  133. void directory_free(directory_t *directory)
  134. {
  135. int i;
  136. for (i = 0; i < directory->n_routers; ++i)
  137. routerinfo_free(directory->routers[i]);
  138. free(directory->routers);
  139. free(directory);
  140. }
  141. void router_forget_router(uint32_t addr, uint16_t port) {
  142. int i;
  143. routerinfo_t *router;
  144. router = router_get_by_addr_port(addr,port);
  145. if(!router) /* we don't seem to know about him in the first place */
  146. return;
  147. /* now walk down router_array until we get to router */
  148. for(i=0;i<directory->n_routers;i++)
  149. if(directory->routers[i] == router)
  150. break;
  151. assert(i != directory->n_routers); /* if so then router_get_by_addr_port should have returned null */
  152. // free(router); /* don't actually free; we'll free it when we free the whole thing */
  153. // log(LOG_DEBUG,"router_forget_router(): Forgot about router %d:%d",addr,port);
  154. for(; i<directory->n_routers-1;i++)
  155. directory->routers[i] = directory->routers[i+1];
  156. }
  157. /* load the router list */
  158. int router_get_list_from_file(char *routerfile)
  159. {
  160. int fd; /* router file */
  161. struct stat statbuf;
  162. char *string;
  163. assert(routerfile);
  164. if (strcspn(routerfile,CONFIG_LEGAL_FILENAME_CHARACTERS) != 0) {
  165. log_fn(LOG_ERR,"Filename %s contains illegal characters.",routerfile);
  166. return -1;
  167. }
  168. if(stat(routerfile, &statbuf) < 0) {
  169. log_fn(LOG_ERR,"Could not stat %s.",routerfile);
  170. return -1;
  171. }
  172. /* open the router list */
  173. fd = open(routerfile,O_RDONLY,0);
  174. if (fd<0) {
  175. log_fn(LOG_ERR,"Could not open %s.",routerfile);
  176. return -1;
  177. }
  178. string = tor_malloc(statbuf.st_size+1);
  179. if(read(fd,string,statbuf.st_size) != statbuf.st_size) {
  180. log_fn(LOG_ERR,"Couldn't read all %ld bytes of file '%s'.",
  181. (long)statbuf.st_size,routerfile);
  182. free(string);
  183. close(fd);
  184. return -1;
  185. }
  186. close(fd);
  187. string[statbuf.st_size] = 0; /* null terminate it */
  188. if(router_get_list_from_string(string) < 0) {
  189. log_fn(LOG_ERR,"The routerfile itself was corrupt.");
  190. free(string);
  191. return -1;
  192. }
  193. free(string);
  194. return 0;
  195. }
  196. typedef enum {
  197. K_ACCEPT,
  198. K_DIRECTORY_SIGNATURE,
  199. K_RECOMMENDED_SOFTWARE,
  200. K_REJECT,
  201. K_ROUTER,
  202. K_SIGNED_DIRECTORY,
  203. K_SIGNING_KEY,
  204. _SIGNATURE,
  205. _PUBLIC_KEY,
  206. _ERR,
  207. _EOF
  208. } directory_keyword;
  209. struct token_table_ent { char *t; int v; };
  210. static struct token_table_ent token_table[] = {
  211. { "accept", K_ACCEPT },
  212. { "directory-signature", K_DIRECTORY_SIGNATURE },
  213. { "reject", K_REJECT },
  214. { "router", K_ROUTER },
  215. { "recommended-software", K_RECOMMENDED_SOFTWARE },
  216. { "signed-directory", K_SIGNED_DIRECTORY },
  217. { "signing-key", K_SIGNING_KEY },
  218. { NULL, -1 }
  219. };
  220. #define MAX_ARGS 8
  221. struct directory_token {
  222. directory_keyword tp;
  223. union {
  224. struct {
  225. char *args[MAX_ARGS+1];
  226. int n_args;
  227. } cmd;
  228. char *signature;
  229. char *error;
  230. crypto_pk_env_t *public_key;
  231. } val;
  232. };
  233. static int
  234. _router_get_next_token(char **s, directory_token_t *tok) {
  235. char *next;
  236. crypto_pk_env_t *pkey = NULL;
  237. char *signature = NULL;
  238. int i, done;
  239. tok->tp = _ERR;
  240. tok->val.error = "";
  241. *s = eat_whitespace(*s);
  242. if (!**s) {
  243. tok->tp = _EOF;
  244. return 0;
  245. } else if (**s == '-') {
  246. next = strchr(*s, '\n');
  247. if (! next) { tok->val.error = "No newline at EOF"; return -1; }
  248. ++next;
  249. if (! strncmp(*s, OR_PUBLICKEY_BEGIN_TAG, next-*s)) {
  250. next = strstr(*s, OR_PUBLICKEY_END_TAG);
  251. if (!next) { tok->val.error = "No public key end tag found"; return -1; }
  252. next = strchr(next, '\n'); /* Part of OR_PUBLICKEY_END_TAG; can't fail.*/
  253. ++next;
  254. if (!(pkey = crypto_new_pk_env(CRYPTO_PK_RSA)))
  255. return -1;
  256. if (crypto_pk_read_public_key_from_string(pkey, *s, next-*s)) {
  257. crypto_free_pk_env(pkey);
  258. tok->val.error = "Couldn't parse public key.";
  259. return -1;
  260. }
  261. tok->tp = _PUBLIC_KEY;
  262. tok->val.public_key = pkey;
  263. *s = next;
  264. return 0;
  265. } else if (! strncmp(*s, OR_SIGNATURE_BEGIN_TAG, next-*s)) {
  266. /* Advance past newline; can't fail. */
  267. *s = strchr(*s, '\n');
  268. ++*s;
  269. /* Find end of base64'd data */
  270. next = strstr(*s, OR_SIGNATURE_END_TAG);
  271. if (!next) { tok->val.error = "No signature end tag found"; return -1; }
  272. signature = tor_malloc(256);
  273. i = base64_decode(signature, 256, *s, next-*s);
  274. if (i<0) {
  275. free(signature);
  276. tok->val.error = "Error decoding signature."; return -1;
  277. } else if (i != 128) {
  278. free(signature);
  279. tok->val.error = "Bad length on decoded signature."; return -1;
  280. }
  281. tok->tp = _SIGNATURE;
  282. tok->val.signature = signature;
  283. next = strchr(next, '\n'); /* Part of OR_SIGNATURE_END_TAG; can't fail.*/
  284. *s = next+1;
  285. return 0;
  286. } else {
  287. tok->val.error = "Unrecognized begin line"; return -1;
  288. }
  289. } else {
  290. next = find_whitespace(*s);
  291. if (!next) {
  292. tok->val.error = "Unexpected EOF"; return -1;
  293. }
  294. for (i = 0 ; token_table[i].t ; ++i) {
  295. if (!strncmp(token_table[i].t, *s, next-*s)) {
  296. tok->tp = token_table[i].v;
  297. i = 0;
  298. done = (*next == '\n');
  299. *s = eat_whitespace_no_nl(next);
  300. while (**s != '\n' && i <= MAX_ARGS && !done) {
  301. next = find_whitespace(*s);
  302. if (*next == '\n')
  303. done = 1;
  304. *next = 0;
  305. tok->val.cmd.args[i++] = *s;
  306. *s = eat_whitespace_no_nl(next+1);
  307. };
  308. tok->val.cmd.n_args = i;
  309. if (i > MAX_ARGS) {
  310. tok->tp = _ERR;
  311. tok->val.error = "Too many arguments"; return -1;
  312. }
  313. return 0;
  314. }
  315. }
  316. tok->val.error = "Unrecognized command"; return -1;
  317. }
  318. }
  319. #ifdef DEBUG_ROUTER_TOKENS
  320. static void
  321. router_dump_token(directory_token_t *tok) {
  322. int i;
  323. switch(tok->tp)
  324. {
  325. case _SIGNATURE:
  326. puts("(signature)");
  327. return;
  328. case _PUBLIC_KEY:
  329. puts("(public key)");
  330. return;
  331. case _ERR:
  332. printf("(Error: %s\n)", tok->val.error);
  333. return;
  334. case _EOF:
  335. puts("EOF");
  336. return;
  337. case K_ACCEPT: printf("Accept"); break;
  338. case K_DIRECTORY_SIGNATURE: printf("Directory-Signature"); break;
  339. case K_REJECT: printf("Reject"); break;
  340. case K_RECOMMENDED_SOFTWARE: printf("Server-Software"); break;
  341. case K_ROUTER: printf("Router"); break;
  342. case K_SIGNED_DIRECTORY: printf("Signed-Directory"); break;
  343. case K_SIGNING_KEY: printf("Signing-Key"); break;
  344. default:
  345. printf("?????? %d\n", tok->tp); return;
  346. }
  347. for (i = 0; i < tok->val.cmd.n_args; ++i) {
  348. printf(" \"%s\"", tok->val.cmd.args[i]);
  349. }
  350. printf("\n");
  351. return;
  352. }
  353. static int
  354. router_get_next_token(char **s, directory_token_t *tok) {
  355. int i;
  356. i = _router_get_next_token(s, tok);
  357. router_dump_token(tok);
  358. return i;
  359. }
  360. #else
  361. #define router_get_next_token _router_get_next_token
  362. #endif
  363. /* return the first char of s that is not whitespace and not a comment */
  364. static char *eat_whitespace(char *s) {
  365. assert(s);
  366. while(isspace(*s) || *s == '#') {
  367. while(isspace(*s))
  368. s++;
  369. if(*s == '#') { /* read to a \n or \0 */
  370. while(*s && *s != '\n')
  371. s++;
  372. if(!*s)
  373. return s;
  374. }
  375. }
  376. return s;
  377. }
  378. static char *eat_whitespace_no_nl(char *s) {
  379. while(*s == ' ' || *s == '\t')
  380. ++s;
  381. return s;
  382. }
  383. /* return the first char of s that is whitespace or '#' or '\0 */
  384. static char *find_whitespace(char *s) {
  385. assert(s);
  386. while(*s && !isspace(*s) && *s != '#')
  387. s++;
  388. return s;
  389. }
  390. int router_get_list_from_string(char *s)
  391. {
  392. if (router_get_list_from_string_impl(s, &directory)) {
  393. log(LOG_ERR, "Error parsing router file");
  394. return -1;
  395. }
  396. if (router_resolve_directory(directory)) {
  397. log(LOG_ERR, "Error resolving directory");
  398. return -1;
  399. }
  400. return 0;
  401. }
  402. int router_get_list_from_string_impl(char *s, directory_t **dest) {
  403. directory_token_t tok;
  404. if (router_get_next_token(&s, &tok)) {
  405. log(LOG_ERR, "Error reading routers: %s", tok.val.error);
  406. return -1;
  407. }
  408. return router_get_list_from_string_tok(&s, dest, &tok);
  409. }
  410. static int router_get_dir_hash(char *s, char *digest)
  411. {
  412. char *start, *end;
  413. start = strstr(s, "signed-directory");
  414. if (!start) {
  415. log(LOG_ERR,"router_get_dir_hash(): couldn't find \"signed-directory\"");
  416. return -1;
  417. }
  418. end = strstr(start, "directory-signature");
  419. if (!end) {
  420. log(LOG_ERR,"router_get_dir_hash(): couldn't find \"directory-signature\"");
  421. return -1;
  422. }
  423. end = strchr(end, '\n');
  424. if (!end) {
  425. log(LOG_ERR,"router_get_dir_hash(): couldn't find EOL");
  426. return -1;
  427. }
  428. ++end;
  429. if (crypto_SHA_digest(start, end-start, digest)) {
  430. log(LOG_ERR,"router_get_dir_hash(): couldn't compute digest");
  431. return -1;
  432. }
  433. return 0;
  434. }
  435. int router_get_dir_from_string(char *s, crypto_pk_env_t *pkey)
  436. {
  437. if (router_get_dir_from_string_impl(s, &directory, pkey)) {
  438. log(LOG_ERR, "router_get_dir_from_string: Couldn't parse directory.");
  439. return -1;
  440. }
  441. if (router_resolve_directory(directory)) {
  442. log(LOG_ERR, "Error resolving directory");
  443. return -1;
  444. }
  445. /* XXXX Check version number */
  446. return 0;
  447. }
  448. int router_get_dir_from_string_impl(char *s, directory_t **dest,
  449. crypto_pk_env_t *pkey)
  450. {
  451. directory_token_t tok;
  452. char digest[20];
  453. char signed_digest[128];
  454. directory_t *new_dir = NULL;
  455. char *versions;
  456. #define NEXT_TOK() \
  457. do { \
  458. if (router_get_next_token(&s, &tok)) { \
  459. log(LOG_ERR, "Error reading directory: %s", tok.val.error); \
  460. return -1; \
  461. } } while (0)
  462. #define TOK_IS(type,name) \
  463. do { \
  464. if (tok.tp != type) { \
  465. log(LOG_ERR, "Error reading directory: expected %s", name); \
  466. return -1; \
  467. } } while(0)
  468. if (router_get_dir_hash(s, digest)) {
  469. log(LOG_ERR, "Unable to compute digest of directory");
  470. return -1;
  471. }
  472. NEXT_TOK();
  473. TOK_IS(K_SIGNED_DIRECTORY, "signed-directory");
  474. NEXT_TOK();
  475. TOK_IS(K_RECOMMENDED_SOFTWARE, "recommended-software");
  476. if (tok.val.cmd.n_args != 1) {
  477. log(LOG_ERR, "Invalid recommded-software line");
  478. return -1;
  479. }
  480. versions = strdup(tok.val.cmd.args[0]);
  481. NEXT_TOK();
  482. if (router_get_list_from_string_tok(&s, &new_dir, &tok)) {
  483. log(LOG_ERR, "Error reading routers from directory");
  484. return -1;
  485. }
  486. new_dir->software_versions = versions;
  487. TOK_IS(K_DIRECTORY_SIGNATURE, "directory-signature");
  488. NEXT_TOK();
  489. TOK_IS(_SIGNATURE, "signature");
  490. if (pkey) {
  491. if (crypto_pk_public_checksig(pkey, tok.val.signature, 128, signed_digest)
  492. != 20) {
  493. log(LOG_ERR, "Error reading directory: invalid signature.");
  494. free(tok.val.signature);
  495. return -1;
  496. }
  497. if (memcmp(digest, signed_digest, 20)) {
  498. log(LOG_ERR, "Error reading directory: signature does not match.");
  499. free(tok.val.signature);
  500. return -1;
  501. }
  502. }
  503. free(tok.val.signature);
  504. NEXT_TOK();
  505. TOK_IS(_EOF, "end of directory");
  506. if (*dest)
  507. directory_free(*dest);
  508. *dest = new_dir;
  509. return 0;
  510. #undef NEXT_TOK
  511. #undef TOK_IS
  512. }
  513. static int router_get_list_from_string_tok(char **s, directory_t **dest,
  514. directory_token_t *tok)
  515. {
  516. routerinfo_t *router;
  517. routerinfo_t **rarray;
  518. int rarray_len = 0;
  519. assert(s);
  520. rarray = (routerinfo_t **)tor_malloc((sizeof(routerinfo_t *))*MAX_ROUTERS_IN_DIR);
  521. while (tok->tp == K_ROUTER) {
  522. router = router_get_entry_from_string_tok(s, tok);
  523. if (!router) {
  524. log(LOG_ERR, "Error reading router");
  525. return -1;
  526. }
  527. if (rarray_len >= MAX_ROUTERS_IN_DIR) {
  528. log(LOG_ERR, "router_get_list_from_string_tok(): too many routers");
  529. routerinfo_free(router);
  530. continue;
  531. }
  532. rarray[rarray_len++] = router;
  533. }
  534. if (*dest)
  535. directory_free(*dest);
  536. *dest = (directory_t *)tor_malloc(sizeof(directory_t));
  537. (*dest)->routers = rarray;
  538. (*dest)->n_routers = rarray_len;
  539. return 0;
  540. }
  541. int
  542. router_resolve(routerinfo_t *router)
  543. {
  544. struct hostent *rent;
  545. rent = (struct hostent *)gethostbyname(router->address);
  546. if (!rent) {
  547. log(LOG_ERR,"router_resolve(): Could not get address for router %s.",router->address);
  548. return -1;
  549. }
  550. assert(rent->h_length == 4);
  551. memcpy(&router->addr, rent->h_addr,rent->h_length);
  552. router->addr = ntohl(router->addr); /* get it back into host order */
  553. return 0;
  554. }
  555. int
  556. router_resolve_directory(directory_t *dir)
  557. {
  558. int i, max, remove;
  559. if (!dir)
  560. dir = directory;
  561. max = dir->n_routers;
  562. for (i = 0; i < max; ++i) {
  563. remove = 0;
  564. if (router_resolve(dir->routers[i])) {
  565. log(LOG_INFO, "Couldn't resolve router %s; removing",
  566. dir->routers[i]->address);
  567. remove = 1;
  568. routerinfo_free(dir->routers[i]);
  569. } else if (router_is_me(dir->routers[i]->addr, dir->routers[i]->or_port)) {
  570. my_routerinfo = dir->routers[i];
  571. remove = 1;
  572. }
  573. if (remove) {
  574. dir->routers[i] = dir->routers[--max];
  575. --dir->n_routers;
  576. --i;
  577. }
  578. }
  579. return 0;
  580. }
  581. routerinfo_t *router_get_entry_from_string(char **s) {
  582. directory_token_t tok;
  583. routerinfo_t *router;
  584. if (router_get_next_token(s, &tok)) return NULL;
  585. router = router_get_entry_from_string_tok(s, &tok);
  586. if (tok.tp != _EOF)
  587. return NULL;
  588. return router;
  589. }
  590. /* reads a single router entry from s.
  591. * updates s so it points to after the router it just read.
  592. * mallocs a new router, returns it if all goes well, else returns NULL.
  593. */
  594. static routerinfo_t *router_get_entry_from_string_tok(char**s, directory_token_t *tok) {
  595. routerinfo_t *router = NULL;
  596. #define NEXT_TOKEN() \
  597. do { if (router_get_next_token(s, tok)) { \
  598. log(LOG_ERR, "Error reading directory: %s", tok->val.error); \
  599. goto err; \
  600. } } while(0)
  601. #define ARGS tok->val.cmd.args
  602. if (tok->tp != K_ROUTER) {
  603. log(LOG_ERR,"router_get_entry_from_string(): Entry does not start with \"router\"");
  604. return NULL;
  605. }
  606. router = tor_malloc(sizeof(routerinfo_t));
  607. memset(router,0,sizeof(routerinfo_t)); /* zero it out first */
  608. /* C doesn't guarantee that NULL is represented by 0 bytes. You'll
  609. thank me for this someday. */
  610. router->pkey = router->signing_pkey = NULL;
  611. if (tok->val.cmd.n_args != 6) {
  612. log(LOG_ERR,"router_get_entry_from_string(): Wrong # of arguments to \"router\"");
  613. goto err;
  614. }
  615. /* read router.address */
  616. if (!(router->address = strdup(ARGS[0])))
  617. goto err;
  618. router->addr = 0;
  619. /* Read router->or_port */
  620. router->or_port = atoi(ARGS[1]);
  621. if(!router->or_port) {
  622. log(LOG_ERR,"router_get_entry_from_string(): or_port unreadable or 0. Failing.");
  623. goto err;
  624. }
  625. /* Router->op_port */
  626. router->op_port = atoi(ARGS[2]);
  627. /* Router->ap_port */
  628. router->ap_port = atoi(ARGS[3]);
  629. /* Router->dir_port */
  630. router->dir_port = atoi(ARGS[4]);
  631. /* Router->bandwidth */
  632. router->bandwidth = atoi(ARGS[5]);
  633. if (!router->bandwidth) {
  634. log(LOG_ERR,"router_get_entry_from_string(): bandwidth unreadable or 0. Failing.");
  635. }
  636. log(LOG_DEBUG,"or_port %d, op_port %d, ap_port %d, dir_port %d, bandwidth %d.",
  637. router->or_port, router->op_port, router->ap_port, router->dir_port, router->bandwidth);
  638. NEXT_TOKEN();
  639. if (tok->tp != _PUBLIC_KEY) {
  640. log(LOG_ERR,"router_get_entry_from_string(): Missing public key");
  641. goto err;
  642. } /* Check key length */
  643. router->pkey = tok->val.public_key;
  644. NEXT_TOKEN();
  645. if (tok->tp == K_SIGNING_KEY) {
  646. NEXT_TOKEN();
  647. if (tok->tp != _PUBLIC_KEY) {
  648. log(LOG_ERR,"router_get_entry_from_string(): Missing signing key");
  649. goto err;
  650. }
  651. router->signing_pkey = tok->val.public_key;
  652. NEXT_TOKEN();
  653. }
  654. while (tok->tp == K_ACCEPT || tok->tp == K_REJECT) {
  655. router_add_exit_policy(router, tok);
  656. NEXT_TOKEN();
  657. }
  658. return router;
  659. err:
  660. if(router->address)
  661. free(router->address);
  662. if(router->pkey)
  663. crypto_free_pk_env(router->pkey);
  664. router_free_exit_policy(router);
  665. free(router);
  666. return NULL;
  667. #undef ARGS
  668. #undef NEXT_TOKEN
  669. }
  670. static void router_free_exit_policy(routerinfo_t *router) {
  671. struct exit_policy_t *tmpe;
  672. while(router->exit_policy) {
  673. tmpe = router->exit_policy;
  674. router->exit_policy = tmpe->next;
  675. free(tmpe->string);
  676. free(tmpe->address);
  677. free(tmpe->port);
  678. free(tmpe);
  679. }
  680. }
  681. #if 0
  682. void test_write_pkey(crypto_pk_env_t *pkey) {
  683. char *string;
  684. int len;
  685. log(LOG_DEBUG,"Trying test write.");
  686. if(crypto_pk_write_public_key_to_string(pkey,&string,&len)<0) {
  687. log(LOG_DEBUG,"router_get_entry_from_string(): write pkey to string failed\n");
  688. return;
  689. }
  690. log(LOG_DEBUG,"I did it: len %d, string '%s'.",len,string);
  691. free(string);
  692. }
  693. #endif
  694. static int router_add_exit_policy(routerinfo_t *router,
  695. directory_token_t *tok) {
  696. struct exit_policy_t *tmpe, *newe;
  697. char *arg, *colon;
  698. if (tok->val.cmd.n_args != 1)
  699. return -1;
  700. arg = tok->val.cmd.args[0];
  701. newe = tor_malloc(sizeof(struct exit_policy_t));
  702. memset(newe,0,sizeof(struct exit_policy_t));
  703. newe->string = tor_malloc(8+strlen(arg));
  704. if (tok->tp == K_REJECT) {
  705. strcpy(newe->string, "reject ");
  706. newe->policy_type = EXIT_POLICY_REJECT;
  707. } else {
  708. assert(tok->tp == K_ACCEPT);
  709. strcpy(newe->string, "accept ");
  710. newe->policy_type = EXIT_POLICY_ACCEPT;
  711. }
  712. strcat(newe->string, arg);
  713. colon = strchr(arg,':');
  714. if(!colon)
  715. goto policy_read_failed;
  716. *colon = 0;
  717. newe->address = strdup(arg);
  718. newe->port = strdup(colon+1);
  719. log(LOG_DEBUG,"router_add_exit_policy(): %s %s:%s",
  720. newe->policy_type == EXIT_POLICY_REJECT ? "reject" : "accept",
  721. newe->address, newe->port);
  722. /* now link newe onto the end of exit_policy */
  723. if(!router->exit_policy) {
  724. router->exit_policy = newe;
  725. return 0;
  726. }
  727. for(tmpe=router->exit_policy; tmpe->next; tmpe=tmpe->next) ;
  728. tmpe->next = newe;
  729. return 0;
  730. policy_read_failed:
  731. assert(newe->string);
  732. log(LOG_INFO,"router_add_exit_policy(): Couldn't parse line '%s'. Dropping", newe->string);
  733. if(newe->string)
  734. free(newe->string);
  735. if(newe->address)
  736. free(newe->address);
  737. if(newe->port)
  738. free(newe->port);
  739. free(newe);
  740. return -1;
  741. }
  742. /* Return 0 if my exit policy says to allow connection to conn.
  743. * Else return -1.
  744. */
  745. int router_compare_to_exit_policy(connection_t *conn) {
  746. struct exit_policy_t *tmpe;
  747. if(!my_routerinfo) {
  748. log(LOG_WARNING, "router_compare_to_exit_policy(): my_routerinfo undefined! Rejected.");
  749. return -1;
  750. }
  751. for(tmpe=my_routerinfo->exit_policy; tmpe; tmpe=tmpe->next) {
  752. assert(tmpe->address);
  753. assert(tmpe->port);
  754. /* Totally ignore the address field of the exit policy, for now. */
  755. if(!strcmp(tmpe->port,"*") || atoi(tmpe->port) == conn->port) {
  756. log(LOG_INFO,"router_compare_to_exit_policy(): Port '%s' matches '%d'. %s.",
  757. tmpe->port, conn->port,
  758. tmpe->policy_type == EXIT_POLICY_ACCEPT ? "Accepting" : "Rejecting");
  759. if(tmpe->policy_type == EXIT_POLICY_ACCEPT)
  760. return 0;
  761. else
  762. return -1;
  763. }
  764. }
  765. return 0; /* accept all by default. */
  766. }
  767. /*
  768. Local Variables:
  769. mode:c
  770. indent-tabs-mode:nil
  771. c-basic-offset:2
  772. End:
  773. */