routers.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  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(LOG_ERR,"Error obtaining local hostname.");
  37. return -1;
  38. }
  39. log(LOG_DEBUG,"learn_my_address(): localhostname is '%s'.",localhostname);
  40. localhost = gethostbyname(localhostname);
  41. if (!localhost) {
  42. log(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(options.ORPort);
  49. log(LOG_DEBUG,"learn_my_address(): chose address as '%s'.",inet_ntoa(me->sin_addr));
  50. return 0;
  51. }
  52. void router_retry_connections(void) {
  53. int i;
  54. routerinfo_t *router;
  55. for (i=0;i<directory->n_routers;i++) {
  56. router = directory->routers[i];
  57. if(!connection_exact_get_by_addr_port(router->addr,router->or_port)) { /* not in the list */
  58. log(LOG_DEBUG,"retry_all_connections(): connecting to OR %s:%u.",router->address,router->or_port);
  59. connection_or_connect_as_or(router);
  60. }
  61. }
  62. }
  63. routerinfo_t *router_pick_directory_server(void) {
  64. /* currently, pick the first router with a positive dir_port */
  65. int i;
  66. routerinfo_t *router;
  67. if(!directory)
  68. return NULL;
  69. for(i=0;i<directory->n_routers;i++) {
  70. router = directory->routers[i];
  71. if(router->dir_port > 0)
  72. return router;
  73. }
  74. return NULL;
  75. }
  76. routerinfo_t *router_get_by_addr_port(uint32_t addr, uint16_t port) {
  77. int i;
  78. routerinfo_t *router;
  79. assert(directory);
  80. for(i=0;i<directory->n_routers;i++) {
  81. router = directory->routers[i];
  82. if ((router->addr == addr) && (router->or_port == port))
  83. return router;
  84. }
  85. return NULL;
  86. }
  87. void router_get_directory(directory_t **pdirectory) {
  88. *pdirectory = directory;
  89. }
  90. /* return 1 if addr and port corresponds to my addr and my or_listenport. else 0,
  91. * or -1 for failure.
  92. */
  93. int router_is_me(uint32_t addr, uint16_t port)
  94. {
  95. /* XXXX Should this check the key too? */
  96. struct sockaddr_in me; /* my router identity */
  97. if(!options.ORPort) {
  98. /* we're not an OR. This obviously isn't us. */
  99. return 0;
  100. }
  101. if(learn_my_address(&me) < 0)
  102. return -1;
  103. if(ntohl(me.sin_addr.s_addr) == addr && ntohs(me.sin_port) == port)
  104. return 1;
  105. return 0;
  106. }
  107. /* delete a list of routers from memory */
  108. void routerinfo_free(routerinfo_t *router)
  109. {
  110. struct exit_policy_t *e = NULL, *etmp = NULL;
  111. if (!router)
  112. return;
  113. if (router->address)
  114. free(router->address);
  115. if (router->pkey)
  116. crypto_free_pk_env(router->pkey);
  117. e = router->exit_policy;
  118. while (e) {
  119. etmp = e->next;
  120. if (e->string) free(e->string);
  121. if (e->address) free(e->address);
  122. if (e->port) free(e->port);
  123. free(e);
  124. e = etmp;
  125. }
  126. free(router);
  127. }
  128. void directory_free(directory_t *directory)
  129. {
  130. int i;
  131. for (i = 0; i < directory->n_routers; ++i)
  132. routerinfo_free(directory->routers[i]);
  133. free(directory->routers);
  134. free(directory);
  135. }
  136. void router_forget_router(uint32_t addr, uint16_t port) {
  137. int i;
  138. routerinfo_t *router;
  139. router = router_get_by_addr_port(addr,port);
  140. if(!router) /* we don't seem to know about him in the first place */
  141. return;
  142. /* now walk down router_array until we get to router */
  143. for(i=0;i<directory->n_routers;i++)
  144. if(directory->routers[i] == router)
  145. break;
  146. assert(i != directory->n_routers); /* if so then router_get_by_addr_port should have returned null */
  147. // free(router); /* don't actually free; we'll free it when we free the whole thing */
  148. // log(LOG_DEBUG,"router_forget_router(): Forgot about router %d:%d",addr,port);
  149. for(; i<directory->n_routers-1;i++)
  150. directory->routers[i] = directory->routers[i+1];
  151. }
  152. /* load the router list */
  153. int router_get_list_from_file(char *routerfile)
  154. {
  155. int fd; /* router file */
  156. struct stat statbuf;
  157. char *string;
  158. assert(routerfile);
  159. if (strcspn(routerfile,CONFIG_LEGAL_FILENAME_CHARACTERS) != 0) {
  160. log(LOG_ERR,"router_get_list_from_file(): Filename %s contains illegal characters.",routerfile);
  161. return -1;
  162. }
  163. if(stat(routerfile, &statbuf) < 0) {
  164. log(LOG_ERR,"router_get_list_from_file(): Could not stat %s.",routerfile);
  165. return -1;
  166. }
  167. /* open the router list */
  168. fd = open(routerfile,O_RDONLY,0);
  169. if (fd<0) {
  170. log(LOG_ERR,"router_get_list_from_file(): Could not open %s.",routerfile);
  171. return -1;
  172. }
  173. string = malloc(statbuf.st_size+1);
  174. if(!string) {
  175. log(LOG_ERR,"router_get_list_from_file(): Out of memory.");
  176. return -1;
  177. }
  178. if(read(fd,string,statbuf.st_size) != statbuf.st_size) {
  179. log(LOG_ERR,"router_get_list_from_file(): Couldn't read all %d bytes of file '%s'.",statbuf.st_size,routerfile);
  180. free(string);
  181. close(fd);
  182. return -1;
  183. }
  184. close(fd);
  185. string[statbuf.st_size] = 0; /* null terminate it */
  186. if(router_get_list_from_string(string) < 0) {
  187. log(LOG_ERR,"router_get_list_from_file(): The routerfile itself was corrupt.");
  188. free(string);
  189. return -1;
  190. }
  191. free(string);
  192. return 0;
  193. }
  194. typedef enum {
  195. K_ACCEPT,
  196. K_CLIENT_SOFTWARE,
  197. K_DIRECTORY_SIGNATURE,
  198. K_REJECT,
  199. K_ROUTER,
  200. K_SERVER_SOFTWARE,
  201. K_SIGNED_DIRECTORY,
  202. K_SIGNING_KEY,
  203. _SIGNATURE,
  204. _PUBLIC_KEY,
  205. _ERR,
  206. _EOF
  207. } directory_keyword;
  208. struct token_table_ent { char *t; int v; };
  209. static struct token_table_ent token_table[] = {
  210. { "accept", K_ACCEPT },
  211. { "client-software", K_CLIENT_SOFTWARE },
  212. { "directory-signature", K_DIRECTORY_SIGNATURE },
  213. { "reject", K_REJECT },
  214. { "router", K_ROUTER },
  215. { "server-software", K_SERVER_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 = 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. static void
  320. router_dump_token(directory_token_t *tok) {
  321. int i;
  322. switch(tok->tp)
  323. {
  324. case _SIGNATURE:
  325. puts("(signature)");
  326. return;
  327. case _PUBLIC_KEY:
  328. puts("(public key)");
  329. return;
  330. case _ERR:
  331. printf("(Error: %s\n)", tok->val.error);
  332. return;
  333. case _EOF:
  334. puts("EOF");
  335. return;
  336. case K_ACCEPT: printf("Accept"); break;
  337. case K_CLIENT_SOFTWARE: printf("Client-Software"); break;
  338. case K_DIRECTORY_SIGNATURE: printf("Directory-Signature"); break;
  339. case K_REJECT: printf("Reject"); break;
  340. case K_ROUTER: printf("Router"); break;
  341. case K_SERVER_SOFTWARE: printf("Server-Software"); 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. #ifdef DEBUG_ROUTER_TOKENS
  354. static int
  355. router_get_next_token(char **s, directory_token_t *tok) {
  356. int i;
  357. i = _router_get_next_token(s, tok);
  358. router_dump_token(tok);
  359. return i;
  360. }
  361. #else
  362. #define router_get_next_token _router_get_next_token
  363. #endif
  364. /* return the first char of s that is not whitespace and not a comment */
  365. static char *eat_whitespace(char *s) {
  366. assert(s);
  367. while(isspace(*s) || *s == '#') {
  368. while(isspace(*s))
  369. s++;
  370. if(*s == '#') { /* read to a \n or \0 */
  371. while(*s && *s != '\n')
  372. s++;
  373. if(!*s)
  374. return s;
  375. }
  376. }
  377. return s;
  378. }
  379. static char *eat_whitespace_no_nl(char *s) {
  380. while(*s == ' ' || *s == '\t')
  381. ++s;
  382. return s;
  383. }
  384. /* return the first char of s that is whitespace or '#' or '\0 */
  385. static char *find_whitespace(char *s) {
  386. assert(s);
  387. while(*s && !isspace(*s) && *s != '#')
  388. s++;
  389. return s;
  390. }
  391. int router_get_list_from_string(char *s)
  392. {
  393. if (router_get_list_from_string_impl(s, &directory)) {
  394. log(LOG_ERR, "Error parsing router file");
  395. return -1;
  396. }
  397. if (router_resolve_directory(directory)) {
  398. log(LOG_ERR, "Error resolving directory");
  399. return -1;
  400. }
  401. return 0;
  402. }
  403. int router_get_list_from_string_impl(char *s, directory_t **dest) {
  404. directory_token_t tok;
  405. if (router_get_next_token(&s, &tok)) {
  406. log(LOG_ERR, "Error reading routers: %s", tok.val.error);
  407. return -1;
  408. }
  409. return router_get_list_from_string_tok(&s, dest, &tok);
  410. }
  411. static int router_get_dir_hash(char *s, char *digest)
  412. {
  413. char *start, *end;
  414. start = strstr(s, "signed-directory");
  415. if (!start) {
  416. log(LOG_ERR,"router_get_dir_hash(): couldn't find \"signed-directory\"");
  417. return -1;
  418. }
  419. end = strstr(start, "directory-signature");
  420. if (!end) {
  421. log(LOG_ERR,"router_get_dir_hash(): couldn't find \"directory-signature\"");
  422. return -1;
  423. }
  424. end = strchr(end, '\n');
  425. if (!end) {
  426. log(LOG_ERR,"router_get_dir_hash(): couldn't find EOL");
  427. return -1;
  428. }
  429. ++end;
  430. if (crypto_SHA_digest(start, end-start, digest)) {
  431. log(LOG_ERR,"router_get_dir_hash(): couldn't compute digest");
  432. return -1;
  433. }
  434. return 0;
  435. }
  436. int router_get_dir_from_string(char *s, crypto_pk_env_t *pkey)
  437. {
  438. if (router_get_dir_from_string_impl(s, &directory, pkey)) {
  439. log(LOG_ERR, "router_get_dir_from_string: Couldn\'t parse directory.");
  440. return -1;
  441. }
  442. if (router_resolve_directory(directory)) {
  443. log(LOG_ERR, "Error resolving directory");
  444. return -1;
  445. }
  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. #define NEXT_TOK() \
  456. do { \
  457. if (router_get_next_token(&s, &tok)) { \
  458. log(LOG_ERR, "Error reading directory: %s", tok.val.error); \
  459. return -1; \
  460. } } while (0)
  461. #define TOK_IS(type,name) \
  462. do { \
  463. if (tok.tp != type) { \
  464. log(LOG_ERR, "Error reading directory: expected %s", name); \
  465. return -1; \
  466. } } while(0)
  467. if (router_get_dir_hash(s, digest)) {
  468. log(LOG_ERR, "Unable to compute digest of directory");
  469. return -1;
  470. }
  471. NEXT_TOK();
  472. TOK_IS(K_SIGNED_DIRECTORY, "signed-directory");
  473. NEXT_TOK();
  474. TOK_IS(K_CLIENT_SOFTWARE, "client-software");
  475. NEXT_TOK();
  476. TOK_IS(K_SERVER_SOFTWARE, "server-software");
  477. NEXT_TOK();
  478. if (router_get_list_from_string_tok(&s, &new_dir, &tok)) {
  479. log(LOG_ERR, "Error reading routers from directory");
  480. return -1;
  481. }
  482. TOK_IS(K_DIRECTORY_SIGNATURE, "directory-signature");
  483. NEXT_TOK();
  484. TOK_IS(_SIGNATURE, "signature");
  485. if (pkey) {
  486. if (crypto_pk_public_checksig(pkey, tok.val.signature, 128, signed_digest)
  487. != 20) {
  488. log(LOG_ERR, "Error reading directory: invalid signature.");
  489. free(tok.val.signature);
  490. return -1;
  491. }
  492. if (memcmp(digest, signed_digest, 20)) {
  493. log(LOG_ERR, "Error reading directory: signature does not match.");
  494. free(tok.val.signature);
  495. return -1;
  496. }
  497. }
  498. free(tok.val.signature);
  499. NEXT_TOK();
  500. TOK_IS(_EOF, "end of directory");
  501. if (*dest)
  502. directory_free(*dest);
  503. *dest = new_dir;
  504. return 0;
  505. #undef NEXT_TOK
  506. #undef TOK_IS
  507. }
  508. static int router_get_list_from_string_tok(char **s, directory_t **dest,
  509. directory_token_t *tok)
  510. {
  511. routerinfo_t *router;
  512. routerinfo_t **rarray;
  513. int rarray_len = 0;
  514. assert(s);
  515. if (!(rarray = malloc((sizeof(routerinfo_t *))*MAX_ROUTERS_IN_DIR))) {
  516. log(LOG_ERR, "router_get_list_from_string_tok(): malloc failed");
  517. return -1;
  518. }
  519. while (tok->tp == K_ROUTER) {
  520. router = router_get_entry_from_string_tok(s, tok);
  521. if (!router) {
  522. log(LOG_ERR, "Error reading router");
  523. return -1;
  524. }
  525. if (rarray_len >= MAX_ROUTERS_IN_DIR) {
  526. log(LOG_ERR, "router_get_list_from_string_tok(): too many routers");
  527. routerinfo_free(router);
  528. continue;
  529. }
  530. rarray[rarray_len++] = router;
  531. }
  532. if (*dest)
  533. directory_free(*dest);
  534. if (!(*dest = (directory_t*) malloc(sizeof(directory_t)))) {
  535. log(LOG_ERR, "router_get_list_from_string_tok(): malloc failed");
  536. return -1;
  537. }
  538. (*dest)->routers = rarray;
  539. (*dest)->n_routers = rarray_len;
  540. return 0;
  541. }
  542. int
  543. router_resolve(routerinfo_t *router)
  544. {
  545. struct hostent *rent;
  546. rent = (struct hostent *)gethostbyname(router->address);
  547. if (!rent) {
  548. log(LOG_ERR,"router_resolve(): Could not get address for router %s.",router->address);
  549. return -1;
  550. }
  551. assert(rent->h_length == 4);
  552. memcpy(&router->addr, rent->h_addr,rent->h_length);
  553. router->addr = ntohl(router->addr); /* get it back into host order */
  554. return 0;
  555. }
  556. int
  557. router_resolve_directory(directory_t *dir)
  558. {
  559. int i, max, remove;
  560. if (!dir)
  561. dir = directory;
  562. max = dir->n_routers;
  563. for (i = 0; i < max; ++i) {
  564. remove = 0;
  565. if (router_resolve(dir->routers[i])) {
  566. log(LOG_INFO, "Couldn\'t resolve router %s; removing",
  567. dir->routers[i]->address);
  568. remove = 1;
  569. routerinfo_free(dir->routers[i]);
  570. } else if (router_is_me(dir->routers[i]->addr, dir->routers[i]->or_port)) {
  571. my_routerinfo = dir->routers[i];
  572. remove = 1;
  573. }
  574. if (remove) {
  575. dir->routers[i] = dir->routers[--max];
  576. --dir->n_routers;
  577. --i;
  578. }
  579. }
  580. return 0;
  581. }
  582. routerinfo_t *router_get_entry_from_string(char **s) {
  583. directory_token_t tok;
  584. routerinfo_t *router;
  585. if (router_get_next_token(s, &tok)) return NULL;
  586. router = router_get_entry_from_string_tok(s, &tok);
  587. if (tok.tp != _EOF)
  588. return NULL;
  589. return router;
  590. }
  591. /* reads a single router entry from s.
  592. * updates s so it points to after the router it just read.
  593. * mallocs a new router, returns it if all goes well, else returns NULL.
  594. */
  595. static routerinfo_t *router_get_entry_from_string_tok(char**s, directory_token_t *tok) {
  596. routerinfo_t *router = NULL;
  597. #define NEXT_TOKEN() \
  598. do { if (router_get_next_token(s, tok)) { \
  599. log(LOG_ERR, "Error reading directory: %s", tok->val.error); \
  600. goto err; \
  601. } } while(0)
  602. #define ARGS tok->val.cmd.args
  603. if (tok->tp != K_ROUTER) {
  604. log(LOG_ERR,"router_get_entry_from_string(): Entry does not start with \"router\"");
  605. return NULL;
  606. }
  607. if (!(router = malloc(sizeof(routerinfo_t)))) {
  608. log(LOG_ERR,"router_get_entry_from_string(): Could not allocate memory.");
  609. return NULL;
  610. }
  611. memset(router,0,sizeof(routerinfo_t)); /* zero it out first */
  612. /* C doesn't guarantee that NULL is represented by 0 bytes. You'll
  613. thank me for this someday. */
  614. router->pkey = router->signing_pkey = NULL;
  615. if (tok->val.cmd.n_args != 6) {
  616. log(LOG_ERR,"router_get_entry_from_string(): Wrong # of arguments to \"router\"");
  617. goto err;
  618. }
  619. /* read router.address */
  620. if (!(router->address = strdup(ARGS[0])))
  621. goto err;
  622. router->addr = 0;
  623. /* Read router->or_port */
  624. router->or_port = atoi(ARGS[1]);
  625. if(!router->or_port) {
  626. log(LOG_ERR,"router_get_entry_from_string(): or_port unreadable or 0. Failing.");
  627. goto err;
  628. }
  629. /* Router->op_port */
  630. router->op_port = atoi(ARGS[2]);
  631. /* Router->ap_port */
  632. router->ap_port = atoi(ARGS[3]);
  633. /* Router->dir_port */
  634. router->dir_port = atoi(ARGS[4]);
  635. /* Router->bandwidth */
  636. router->bandwidth = atoi(ARGS[5]);
  637. if (!router->bandwidth) {
  638. log(LOG_ERR,"router_get_entry_from_string(): bandwidth unreadable or 0. Failing.");
  639. }
  640. log(LOG_DEBUG,"or_port %d, op_port %d, ap_port %d, dir_port %d, bandwidth %d.",
  641. router->or_port, router->op_port, router->ap_port, router->dir_port, router->bandwidth);
  642. NEXT_TOKEN();
  643. if (tok->tp != _PUBLIC_KEY) {
  644. log(LOG_ERR,"router_get_entry_from_string(): Missing public key");
  645. goto err;
  646. } /* Check key length */
  647. router->pkey = tok->val.public_key;
  648. NEXT_TOKEN();
  649. if (tok->tp == K_SIGNING_KEY) {
  650. NEXT_TOKEN();
  651. if (tok->tp != _PUBLIC_KEY) {
  652. log(LOG_ERR,"router_get_entry_from_string(): Missing signing key");
  653. goto err;
  654. }
  655. router->signing_pkey = tok->val.public_key;
  656. NEXT_TOKEN();
  657. }
  658. while (tok->tp == K_ACCEPT || tok->tp == K_REJECT) {
  659. router_add_exit_policy(router, tok);
  660. NEXT_TOKEN();
  661. }
  662. return router;
  663. err:
  664. if(router->address)
  665. free(router->address);
  666. if(router->pkey)
  667. crypto_free_pk_env(router->pkey);
  668. router_free_exit_policy(router);
  669. free(router);
  670. return NULL;
  671. #undef ARGS
  672. #undef NEXT_TOKEN
  673. }
  674. static void router_free_exit_policy(routerinfo_t *router) {
  675. struct exit_policy_t *tmpe;
  676. while(router->exit_policy) {
  677. tmpe = router->exit_policy;
  678. router->exit_policy = tmpe->next;
  679. free(tmpe->string);
  680. free(tmpe->address);
  681. free(tmpe->port);
  682. free(tmpe);
  683. }
  684. }
  685. #if 0
  686. void test_write_pkey(crypto_pk_env_t *pkey) {
  687. char *string;
  688. int len;
  689. log(LOG_DEBUG,"Trying test write.");
  690. if(crypto_pk_write_public_key_to_string(pkey,&string,&len)<0) {
  691. log(LOG_DEBUG,"router_get_entry_from_string(): write pkey to string failed\n");
  692. return;
  693. }
  694. log(LOG_DEBUG,"I did it: len %d, string '%s'.",len,string);
  695. free(string);
  696. }
  697. #endif
  698. static int router_add_exit_policy(routerinfo_t *router,
  699. directory_token_t *tok) {
  700. struct exit_policy_t *tmpe, *newe;
  701. char *arg, *colon;
  702. if (tok->val.cmd.n_args != 1)
  703. return -1;
  704. arg = tok->val.cmd.args[0];
  705. newe = malloc(sizeof(struct exit_policy_t));
  706. memset(newe,0,sizeof(struct exit_policy_t));
  707. newe->string = malloc(8+strlen(arg));
  708. if (tok->tp == K_REJECT) {
  709. strcpy(newe->string, "reject ");
  710. newe->policy_type = EXIT_POLICY_REJECT;
  711. } else {
  712. assert(tok->tp == K_ACCEPT);
  713. strcpy(newe->string, "accept ");
  714. newe->policy_type = EXIT_POLICY_ACCEPT;
  715. }
  716. strcat(newe->string, arg);
  717. colon = strchr(arg,':');
  718. if(!colon)
  719. goto policy_read_failed;
  720. *colon = 0;
  721. newe->address = strdup(arg);
  722. newe->port = strdup(colon+1);
  723. log(LOG_DEBUG,"router_add_exit_policy(): type %d, address '%s', port '%s'.",
  724. newe->policy_type, newe->address, newe->port);
  725. /* now link newe onto the end of exit_policy */
  726. if(!router->exit_policy) {
  727. router->exit_policy = newe;
  728. return 0;
  729. }
  730. for(tmpe=router->exit_policy; tmpe->next; tmpe=tmpe->next) ;
  731. tmpe->next = newe;
  732. return 0;
  733. policy_read_failed:
  734. assert(newe->string);
  735. log(LOG_INFO,"router_add_exit_policy(): Couldn't parse line '%s'. Dropping", newe->string);
  736. if(newe->string)
  737. free(newe->string);
  738. if(newe->address)
  739. free(newe->address);
  740. if(newe->port)
  741. free(newe->port);
  742. free(newe);
  743. return -1;
  744. }
  745. /* Return 0 if my exit policy says to allow connection to conn.
  746. * Else return -1.
  747. */
  748. int router_compare_to_exit_policy(connection_t *conn) {
  749. struct exit_policy_t *tmpe;
  750. if(!my_routerinfo) {
  751. log(LOG_WARNING, "router_compare_to_exit_policy(): my_routerinfo undefined! Rejected.");
  752. return -1;
  753. }
  754. for(tmpe=my_routerinfo->exit_policy; tmpe; tmpe=tmpe->next) {
  755. assert(tmpe->address);
  756. assert(tmpe->port);
  757. /* Totally ignore the address field of the exit policy, for now. */
  758. if(!strcmp(tmpe->port,"*") || atoi(tmpe->port) == conn->port) {
  759. log(LOG_INFO,"router_compare_to_exit_policy(): Port '%s' matches '%d'. %s.",
  760. tmpe->port, conn->port,
  761. tmpe->policy_type == EXIT_POLICY_ACCEPT ? "Accepting" : "Rejecting");
  762. if(tmpe->policy_type == EXIT_POLICY_ACCEPT)
  763. return 0;
  764. else
  765. return -1;
  766. }
  767. }
  768. return 0; /* accept all by default. */
  769. }
  770. /*
  771. Local Variables:
  772. mode:c
  773. indent-tabs-mode:nil
  774. c-basic-offset:2
  775. End:
  776. */