routers.c 34 KB

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