routers.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228
  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. static int router_add_exit_policy(routerinfo_t *router,
  830. directory_token_t *tok) {
  831. struct exit_policy_t *tmpe, *newe;
  832. char *arg, *colon;
  833. if (tok->val.cmd.n_args != 1)
  834. return -1;
  835. arg = tok->val.cmd.args[0];
  836. newe = tor_malloc(sizeof(struct exit_policy_t));
  837. memset(newe,0,sizeof(struct exit_policy_t));
  838. newe->string = tor_malloc(8+strlen(arg));
  839. if (tok->tp == K_REJECT) {
  840. strcpy(newe->string, "reject ");
  841. newe->policy_type = EXIT_POLICY_REJECT;
  842. } else {
  843. assert(tok->tp == K_ACCEPT);
  844. strcpy(newe->string, "accept ");
  845. newe->policy_type = EXIT_POLICY_ACCEPT;
  846. }
  847. strcat(newe->string, arg);
  848. colon = strchr(arg,':');
  849. if(!colon)
  850. goto policy_read_failed;
  851. *colon = 0;
  852. newe->address = tor_strdup(arg);
  853. newe->port = tor_strdup(colon+1);
  854. log_fn(LOG_DEBUG,"%s %s:%s",
  855. newe->policy_type == EXIT_POLICY_REJECT ? "reject" : "accept",
  856. newe->address, newe->port);
  857. /* now link newe onto the end of exit_policy */
  858. if(!router->exit_policy) {
  859. router->exit_policy = newe;
  860. return 0;
  861. }
  862. for(tmpe=router->exit_policy; tmpe->next; tmpe=tmpe->next) ;
  863. tmpe->next = newe;
  864. return 0;
  865. policy_read_failed:
  866. assert(newe->string);
  867. log_fn(LOG_WARNING,"Couldn't parse line '%s'. Dropping", newe->string);
  868. if(newe->string)
  869. free(newe->string);
  870. if(newe->address)
  871. free(newe->address);
  872. if(newe->port)
  873. free(newe->port);
  874. free(newe);
  875. return -1;
  876. }
  877. /* Return 0 if my exit policy says to allow connection to conn.
  878. * Else return -1.
  879. */
  880. int router_compare_to_exit_policy(connection_t *conn) {
  881. struct exit_policy_t *tmpe;
  882. assert(desc_routerinfo);
  883. for(tmpe=desc_routerinfo->exit_policy; tmpe; tmpe=tmpe->next) {
  884. assert(tmpe->address);
  885. assert(tmpe->port);
  886. /* Totally ignore the address field of the exit policy, for now. */
  887. if(!strcmp(tmpe->port,"*") || atoi(tmpe->port) == conn->port) {
  888. log_fn(LOG_INFO,"Port '%s' matches '%d'. %s.",
  889. tmpe->port, conn->port,
  890. tmpe->policy_type == EXIT_POLICY_ACCEPT ? "Accepting" : "Rejecting");
  891. if(tmpe->policy_type == EXIT_POLICY_ACCEPT)
  892. return 0;
  893. else
  894. return -1;
  895. }
  896. }
  897. return 0; /* accept all by default. */
  898. }
  899. const char *router_get_my_descriptor(void) {
  900. if (!desc_routerinfo) {
  901. if (router_rebuild_descriptor())
  902. return NULL;
  903. }
  904. log_fn(LOG_DEBUG,"my desc is '%s'",descriptor);
  905. return descriptor;
  906. }
  907. const routerinfo_t *router_get_desc_routerinfo(void) {
  908. if (!desc_routerinfo) {
  909. if (router_rebuild_descriptor())
  910. return NULL;
  911. }
  912. return desc_routerinfo;
  913. }
  914. int router_rebuild_descriptor(void) {
  915. routerinfo_t *ri;
  916. char localhostname[256];
  917. char *address = options.Address;
  918. if(!address) { /* if not specified in config, we find a default */
  919. if(gethostname(localhostname,sizeof(localhostname)) < 0) {
  920. log_fn(LOG_WARNING,"Error obtaining local hostname");
  921. return -1;
  922. }
  923. address = localhostname;
  924. }
  925. ri = tor_malloc(sizeof(routerinfo_t));
  926. ri->address = tor_strdup(address);
  927. ri->nickname = tor_strdup(options.Nickname);
  928. /* No need to set addr. */
  929. ri->or_port = options.ORPort;
  930. ri->ap_port = options.APPort;
  931. ri->dir_port = options.DirPort;
  932. ri->published_on = time(NULL);
  933. ri->onion_pkey = crypto_pk_dup_key(get_onion_key());
  934. ri->link_pkey = crypto_pk_dup_key(get_link_key());
  935. ri->identity_pkey = crypto_pk_dup_key(get_identity_key());
  936. ri->bandwidth = options.TotalBandwidth;
  937. ri->exit_policy = NULL; /* XXX implement this. */
  938. if (desc_routerinfo)
  939. routerinfo_free(desc_routerinfo);
  940. desc_routerinfo = ri;
  941. if (router_dump_router_to_string(descriptor, 8192, ri, get_identity_key())<0) {
  942. log_fn(LOG_WARNING, "Couldn't dump router to string.");
  943. return -1;
  944. }
  945. return 0;
  946. }
  947. static void get_platform_str(char *platform, int len)
  948. {
  949. snprintf(platform, len-1, "Tor %s on %s", VERSION, get_uname());
  950. platform[len-1] = '\0';
  951. return;
  952. }
  953. #define DEBUG_ROUTER_DUMP_ROUTER_TO_STRING
  954. int router_dump_router_to_string(char *s, int maxlen, routerinfo_t *router,
  955. crypto_pk_env_t *ident_key) {
  956. char *onion_pkey;
  957. char *link_pkey;
  958. char *identity_pkey;
  959. char platform[256];
  960. char digest[20];
  961. char signature[128];
  962. char published[32];
  963. int onion_pkeylen, link_pkeylen, identity_pkeylen;
  964. int written;
  965. int result=0;
  966. struct exit_policy_t *tmpe;
  967. #ifdef DEBUG_ROUTER_DUMP_ROUTER_TO_STRING
  968. char *s_tmp, *s_dup;
  969. routerinfo_t *ri_tmp;
  970. #endif
  971. get_platform_str(platform, sizeof(platform));
  972. if (crypto_pk_cmp_keys(ident_key, router->identity_pkey)) {
  973. log_fn(LOG_WARNING,"Tried to sign a router with a private key that didn't match router's public key!");
  974. return -1;
  975. }
  976. if(crypto_pk_write_public_key_to_string(router->onion_pkey,
  977. &onion_pkey,&onion_pkeylen)<0) {
  978. log_fn(LOG_WARNING,"write onion_pkey to string failed!");
  979. return -1;
  980. }
  981. if(crypto_pk_write_public_key_to_string(router->identity_pkey,
  982. &identity_pkey,&identity_pkeylen)<0) {
  983. log_fn(LOG_WARNING,"write identity_pkey to string failed!");
  984. return -1;
  985. }
  986. if(crypto_pk_write_public_key_to_string(router->link_pkey,
  987. &link_pkey,&link_pkeylen)<0) {
  988. log_fn(LOG_WARNING,"write link_pkey to string failed!");
  989. return -1;
  990. }
  991. strftime(published, 32, "%Y-%m-%d %H:%M:%S", gmtime(&router->published_on));
  992. result = snprintf(s, maxlen,
  993. "router %s %s %d %d %d %d\n"
  994. "platform %s\n"
  995. "published %s\n"
  996. "onion-key\n%s"
  997. "link-key\n%s"
  998. "signing-key\n%s",
  999. router->nickname,
  1000. router->address,
  1001. router->or_port,
  1002. router->ap_port,
  1003. router->dir_port,
  1004. router->bandwidth,
  1005. platform,
  1006. published,
  1007. onion_pkey, link_pkey, identity_pkey);
  1008. free(onion_pkey);
  1009. free(link_pkey);
  1010. free(identity_pkey);
  1011. if(result < 0 || result >= maxlen) {
  1012. /* apparently different glibcs do different things on snprintf error.. so check both */
  1013. return -1;
  1014. }
  1015. written = result;
  1016. for(tmpe=router->exit_policy; tmpe; tmpe=tmpe->next) {
  1017. result = snprintf(s+written, maxlen-written, "%s %s:%s\n",
  1018. tmpe->policy_type == EXIT_POLICY_ACCEPT ? "accept" : "reject",
  1019. tmpe->address, tmpe->port);
  1020. if(result < 0 || result+written > maxlen) {
  1021. /* apparently different glibcs do different things on snprintf error.. so check both */
  1022. return -1;
  1023. }
  1024. written += result;
  1025. }
  1026. if (written > maxlen-256) /* Not enough room for signature. */
  1027. return -1;
  1028. strcat(s+written, "router-signature\n");
  1029. written += strlen(s+written);
  1030. s[written] = '\0';
  1031. if (router_get_router_hash(s, digest) < 0)
  1032. return -1;
  1033. if (crypto_pk_private_sign(ident_key, digest, 20, signature) < 0) {
  1034. log_fn(LOG_WARNING, "Error signing digest");
  1035. return -1;
  1036. }
  1037. strcat(s+written, "-----BEGIN SIGNATURE-----\n");
  1038. written += strlen(s+written);
  1039. if (base64_encode(s+written, maxlen-written, signature, 128) < 0) {
  1040. log_fn(LOG_WARNING, "Couldn't base64-encode signature");
  1041. return -1;
  1042. }
  1043. written += strlen(s+written);
  1044. strcat(s+written, "-----END SIGNATURE-----\n");
  1045. written += strlen(s+written);
  1046. if (written > maxlen-2)
  1047. return -1;
  1048. /* include a last '\n' */
  1049. s[written] = '\n';
  1050. s[written+1] = 0;
  1051. #ifdef DEBUG_ROUTER_DUMP_ROUTER_TO_STRING
  1052. s_tmp = s_dup = tor_strdup(s);
  1053. ri_tmp = router_get_entry_from_string(&s_tmp);
  1054. if (!ri_tmp) {
  1055. log_fn(LOG_ERR, "We just generated a router descriptor we can't parse: <<%s>>",
  1056. s);
  1057. return -1;
  1058. }
  1059. free(s_dup);
  1060. routerinfo_free(ri_tmp);
  1061. #endif
  1062. return written+1;
  1063. }
  1064. /*
  1065. Local Variables:
  1066. mode:c
  1067. indent-tabs-mode:nil
  1068. c-basic-offset:2
  1069. End:
  1070. */