routers.c 34 KB

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