routers.c 34 KB

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