routerlist.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329
  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 routerlist_t *routerlist = NULL; /* router array */
  15. extern or_options_t options; /* command-line and config-file options */
  16. /****************************************************************************/
  17. /* Enumeration of possible token types. The ones starting with K_ correspond
  18. * to directory 'keywords'. _SIGNATURE and _PUBLIC_KEY are self-explanatory.
  19. * _ERR is an error in the tokenizing process, _EOF is an end-of-file marker,
  20. * and _NIL is used to encode not-a-token.
  21. */
  22. typedef enum {
  23. K_ACCEPT,
  24. K_DIRECTORY_SIGNATURE,
  25. K_RECOMMENDED_SOFTWARE,
  26. K_REJECT,
  27. K_ROUTER,
  28. K_SIGNED_DIRECTORY,
  29. K_SIGNING_KEY,
  30. K_ONION_KEY,
  31. K_LINK_KEY,
  32. K_ROUTER_SIGNATURE,
  33. K_PUBLISHED,
  34. K_RUNNING_ROUTERS,
  35. K_PLATFORM,
  36. _SIGNATURE,
  37. _PUBLIC_KEY,
  38. _ERR,
  39. _EOF,
  40. _NIL
  41. } directory_keyword;
  42. /* Struct containing a directory token. */
  43. #define MAX_ARGS 1024
  44. typedef struct directory_token_t {
  45. directory_keyword tp; /* Type of the token. */
  46. union {
  47. struct {
  48. char *args[MAX_ARGS+1]; /* For K_xxx tokens only: an array and count */
  49. int n_args; /* of arguments provided on the same line */
  50. } cmd;
  51. char *signature; /* For _SIGNATURE tokens only. */
  52. char *error; /* For _ERR tokens only. */
  53. crypto_pk_env_t *public_key; /* For _PUBLIC_KEY tokens only. */
  54. } val;
  55. } directory_token_t;
  56. /****************************************************************************/
  57. /* static function prototypes */
  58. static routerinfo_t *
  59. router_pick_directory_server_impl(void);
  60. static int
  61. router_get_list_from_string_impl(const char **s, routerlist_t **dest,
  62. int n_good_nicknames,
  63. const char **good_nickname_lst);
  64. static int
  65. router_get_routerlist_from_directory_impl(const char *s, routerlist_t **dest,
  66. crypto_pk_env_t *pkey);
  67. static int
  68. router_add_exit_policy(routerinfo_t *router, directory_token_t *tok);
  69. static int
  70. router_resolve_routerlist(routerlist_t *dir);
  71. static int
  72. _router_get_next_token(const char **s, directory_token_t *tok);
  73. #ifdef DEBUG_ROUTER_TOKENS
  74. static int
  75. router_get_next_token(const char **s, directory_token_t *tok);
  76. #else
  77. #define router_get_next_token _router_get_next_token
  78. #endif
  79. static int
  80. router_get_hash_impl(const char *s, char *digest,
  81. const char *start_str, const char *end_str);
  82. static void
  83. router_release_token(directory_token_t *tok);
  84. /****************************************************************************/
  85. extern int has_fetched_directory;
  86. /* try to find a running dirserver. if there are no dirservers
  87. * in our routerlist, reload the routerlist and try again. */
  88. routerinfo_t *router_pick_directory_server(void) {
  89. routerinfo_t *choice;
  90. choice = router_pick_directory_server_impl();
  91. if(!choice) {
  92. log_fn(LOG_WARN,"No dirservers known. Reloading and trying again.");
  93. has_fetched_directory=0; /* reset it */
  94. if(options.RouterFile) {
  95. if(router_set_routerlist_from_file(options.RouterFile) < 0)
  96. return NULL;
  97. } else {
  98. if(config_assign_default_dirservers() < 0)
  99. return NULL;
  100. }
  101. /* give it another try */
  102. choice = router_pick_directory_server_impl();
  103. }
  104. return choice;
  105. }
  106. /* pick a random running router with a positive dir_port */
  107. static routerinfo_t *router_pick_directory_server_impl(void) {
  108. int i;
  109. routerinfo_t *router, *dirserver=NULL;
  110. smartlist_t *sl;
  111. if(!routerlist)
  112. return NULL;
  113. sl = smartlist_create(MAX_ROUTERS_IN_DIR);
  114. for(i=0;i<routerlist->n_routers;i++) {
  115. router = routerlist->routers[i];
  116. if(router->dir_port > 0 && router->is_running)
  117. smartlist_add(sl, router);
  118. }
  119. router = smartlist_choose(sl);
  120. smartlist_free(sl);
  121. if(router)
  122. return router;
  123. log_fn(LOG_INFO,"No dirservers are reachable. Trying them all again.");
  124. /* no running dir servers found? go through and mark them all as up,
  125. * and we'll cycle through the list again. */
  126. for(i=0;i<routerlist->n_routers;i++) {
  127. router = routerlist->routers[i];
  128. if(router->dir_port > 0) {
  129. router->is_running = 1;
  130. dirserver = router;
  131. }
  132. }
  133. if(!dirserver)
  134. log_fn(LOG_WARN,"No dirservers in directory! Returning NULL.");
  135. return dirserver;
  136. }
  137. void router_add_running_routers_to_smartlist(smartlist_t *sl) {
  138. routerinfo_t *router;
  139. int i;
  140. if(!routerlist)
  141. return;
  142. for(i=0;i<routerlist->n_routers;i++) {
  143. router = routerlist->routers[i];
  144. if(router->is_running &&
  145. (!options.ORPort ||
  146. connection_twin_get_by_addr_port(router->addr, router->or_port) ))
  147. smartlist_add(sl, router);
  148. }
  149. }
  150. routerinfo_t *router_get_by_addr_port(uint32_t addr, uint16_t port) {
  151. int i;
  152. routerinfo_t *router;
  153. assert(routerlist);
  154. for(i=0;i<routerlist->n_routers;i++) {
  155. router = routerlist->routers[i];
  156. if ((router->addr == addr) && (router->or_port == port))
  157. return router;
  158. }
  159. return NULL;
  160. }
  161. routerinfo_t *router_get_by_link_pk(crypto_pk_env_t *pk)
  162. {
  163. int i;
  164. routerinfo_t *router;
  165. assert(routerlist);
  166. for(i=0;i<routerlist->n_routers;i++) {
  167. router = routerlist->routers[i];
  168. if (0 == crypto_pk_cmp_keys(router->link_pkey, pk))
  169. return router;
  170. }
  171. return NULL;
  172. }
  173. routerinfo_t *router_get_by_nickname(char *nickname)
  174. {
  175. int i;
  176. routerinfo_t *router;
  177. assert(routerlist);
  178. for(i=0;i<routerlist->n_routers;i++) {
  179. router = routerlist->routers[i];
  180. if (0 == strcmp(router->nickname, nickname))
  181. return router;
  182. }
  183. return NULL;
  184. }
  185. /* a way to access routerlist outside this file */
  186. void router_get_routerlist(routerlist_t **prouterlist) {
  187. *prouterlist = routerlist;
  188. }
  189. /* delete a router from memory */
  190. void routerinfo_free(routerinfo_t *router)
  191. {
  192. struct exit_policy_t *e;
  193. if (!router)
  194. return;
  195. tor_free(router->address);
  196. tor_free(router->nickname);
  197. if (router->onion_pkey)
  198. crypto_free_pk_env(router->onion_pkey);
  199. if (router->link_pkey)
  200. crypto_free_pk_env(router->link_pkey);
  201. if (router->identity_pkey)
  202. crypto_free_pk_env(router->identity_pkey);
  203. while (router->exit_policy) {
  204. e = router->exit_policy;
  205. router->exit_policy = e->next;
  206. tor_free(e->string);
  207. free(e);
  208. }
  209. free(router);
  210. }
  211. static void routerlist_free(routerlist_t *rl)
  212. {
  213. int i;
  214. for (i = 0; i < rl->n_routers; ++i)
  215. routerinfo_free(rl->routers[i]);
  216. tor_free(rl->routers);
  217. tor_free(rl->software_versions);
  218. free(rl);
  219. }
  220. void router_mark_as_down(char *nickname) {
  221. routerinfo_t *router = router_get_by_nickname(nickname);
  222. if(!router) /* we don't seem to know about him in the first place */
  223. return;
  224. log_fn(LOG_DEBUG,"Marking %s as down.",router->nickname);
  225. router->is_running = 0;
  226. }
  227. /* ------------------------------------------------------------ */
  228. /* Replace the current router list with the one stored in 'routerfile'. */
  229. int router_set_routerlist_from_file(char *routerfile)
  230. {
  231. char *string;
  232. string = read_file_to_str(routerfile);
  233. if(!string) {
  234. log_fn(LOG_WARN,"Failed to load routerfile %s.",routerfile);
  235. return -1;
  236. }
  237. if(router_set_routerlist_from_string(string) < 0) {
  238. log_fn(LOG_WARN,"The routerfile itself was corrupt.");
  239. free(string);
  240. return -1;
  241. }
  242. free(string);
  243. return 0;
  244. }
  245. /* Helper function: read routerinfo elements from s, and throw out the
  246. * ones that don't parse and resolve. Replace the current
  247. * routerlist. */
  248. int router_set_routerlist_from_string(const char *s)
  249. {
  250. if (router_get_list_from_string_impl(&s, &routerlist, -1, NULL)) {
  251. log(LOG_WARN, "Error parsing router file");
  252. return -1;
  253. }
  254. if (router_resolve_routerlist(routerlist)) {
  255. log(LOG_WARN, "Error resolving routerlist");
  256. return -1;
  257. }
  258. return 0;
  259. }
  260. /* Set 'digest' to the SHA-1 digest of the hash of the directory in 's'.
  261. * Return 0 on success, nonzero on failure.
  262. */
  263. int router_get_dir_hash(const char *s, char *digest)
  264. {
  265. return router_get_hash_impl(s,digest,
  266. "signed-directory","directory-signature");
  267. }
  268. /* Set 'digest' to the SHA-1 digest of the hash of the first router in 's'.
  269. * Return 0 on success, nonzero on failure.
  270. */
  271. int router_get_router_hash(const char *s, char *digest)
  272. {
  273. return router_get_hash_impl(s,digest,
  274. "router ","router-signature");
  275. }
  276. /* return 0 if myversion is in versionlist. Else return -1.
  277. * (versionlist contains a comma-separated list of versions.) */
  278. int compare_recommended_versions(const char *myversion,
  279. const char *versionlist) {
  280. int len_myversion = strlen(myversion);
  281. char *comma;
  282. const char *end = versionlist + strlen(versionlist);
  283. log_fn(LOG_DEBUG,"checking '%s' in '%s'.", myversion, versionlist);
  284. for(;;) {
  285. comma = strchr(versionlist, ',');
  286. if( ((comma ? comma : end) - versionlist == len_myversion) &&
  287. !strncmp(versionlist, myversion, len_myversion))
  288. /* only do strncmp if the length matches */
  289. return 0; /* success, it's there */
  290. if(!comma)
  291. return -1; /* nope */
  292. versionlist = comma+1;
  293. }
  294. }
  295. /* Replace the current routerlist with the routers stored in the directory
  296. * 's'. If pkey is provided, make sure that 's' is signed with pkey. */
  297. int router_set_routerlist_from_directory(const char *s, crypto_pk_env_t *pkey)
  298. {
  299. if (router_get_routerlist_from_directory_impl(s, &routerlist, pkey)) {
  300. log_fn(LOG_WARN, "Couldn't parse directory.");
  301. return -1;
  302. }
  303. if (router_resolve_routerlist(routerlist)) {
  304. log_fn(LOG_WARN, "Error resolving routerlist");
  305. return -1;
  306. }
  307. if (compare_recommended_versions(VERSION, routerlist->software_versions) < 0) {
  308. log(options.IgnoreVersion ? LOG_WARN : LOG_ERR,
  309. "You are running Tor version %s, which will not work with this network.\n"
  310. "Please use %s%s.",
  311. VERSION, strchr(routerlist->software_versions,',') ? "one of " : "",
  312. routerlist->software_versions);
  313. if(options.IgnoreVersion) {
  314. log(LOG_WARN, "IgnoreVersion is set. If it breaks, we told you so.");
  315. } else {
  316. fflush(0);
  317. exit(0);
  318. }
  319. }
  320. return 0;
  321. }
  322. /* Helper function: resolve the hostname for 'router' */
  323. static int
  324. router_resolve(routerinfo_t *router)
  325. {
  326. struct hostent *rent;
  327. rent = (struct hostent *)gethostbyname(router->address);
  328. if (!rent) {
  329. log_fn(LOG_WARN,"Could not get address for router %s (%s).",
  330. router->address, router->nickname);
  331. return -1;
  332. }
  333. assert(rent->h_length == 4);
  334. memcpy(&router->addr, rent->h_addr,rent->h_length);
  335. router->addr = ntohl(router->addr); /* get it back into host order */
  336. return 0;
  337. }
  338. /* Helper function: resolve every router in rl. */
  339. static int
  340. router_resolve_routerlist(routerlist_t *rl)
  341. {
  342. int i, max, remove;
  343. if (!rl)
  344. rl = routerlist;
  345. max = rl->n_routers;
  346. for (i = 0; i < max; ++i) {
  347. remove = 0;
  348. if (router_resolve(rl->routers[i])) {
  349. log_fn(LOG_WARN, "Couldn't resolve router %s; not using",
  350. rl->routers[i]->address);
  351. remove = 1;
  352. } else if (options.Nickname &&
  353. !strcmp(rl->routers[i]->nickname, options.Nickname)) {
  354. remove = 1;
  355. }
  356. if (remove) {
  357. routerinfo_free(rl->routers[i]);
  358. rl->routers[i] = rl->routers[--max];
  359. --rl->n_routers;
  360. --i;
  361. }
  362. }
  363. return 0;
  364. }
  365. /* Addr is 0 for "IP unknown".
  366. *
  367. * Returns -1 for 'rejected', 0 for accepted, 1 for 'maybe' (since IP is
  368. * unknown.
  369. */
  370. int router_compare_addr_to_exit_policy(uint32_t addr, uint16_t port,
  371. struct exit_policy_t *policy)
  372. {
  373. int maybe_reject = 0;
  374. int maybe_accept = 0;
  375. int match = 0;
  376. int maybe = 0;
  377. struct in_addr in;
  378. struct exit_policy_t *tmpe;
  379. for(tmpe=policy; tmpe; tmpe=tmpe->next) {
  380. log_fn(LOG_DEBUG,"Considering exit policy %s", tmpe->string);
  381. maybe = 0;
  382. if (!addr) {
  383. /* Address is unknown. */
  384. if (port >= tmpe->prt_min && port <= tmpe->prt_max) {
  385. /* The port definitely matches. */
  386. if (tmpe->msk == 0) {
  387. match = 1;
  388. } else {
  389. maybe = 1;
  390. }
  391. } else if (!port) {
  392. /* The port maybe matches. */
  393. maybe = 1;
  394. }
  395. } else {
  396. /* Address is known */
  397. if ((addr & tmpe->msk) == (tmpe->addr & tmpe->msk)) {
  398. if (port >= tmpe->prt_min && port <= tmpe->prt_max) {
  399. /* Exact match for the policy */
  400. match = 1;
  401. } else if (!port) {
  402. maybe = 1;
  403. }
  404. }
  405. }
  406. if (maybe) {
  407. if (tmpe->policy_type == EXIT_POLICY_REJECT)
  408. maybe_reject = 1;
  409. else
  410. maybe_accept = 1;
  411. }
  412. if (match) {
  413. in.s_addr = htonl(addr);
  414. log_fn(LOG_INFO,"Address %s:%d matches exit policy '%s'",
  415. inet_ntoa(in), port, tmpe->string);
  416. if(tmpe->policy_type == EXIT_POLICY_ACCEPT) {
  417. /* If we already hit a clause that might trigger a 'reject', than we
  418. * can't be sure of this certain 'accept'.*/
  419. return maybe_reject ? ADDR_POLICY_UNKNOWN : ADDR_POLICY_ACCEPTED;
  420. } else {
  421. return maybe_accept ? ADDR_POLICY_UNKNOWN : ADDR_POLICY_REJECTED;
  422. }
  423. }
  424. }
  425. /* accept all by default. */
  426. return maybe_reject ? ADDR_POLICY_UNKNOWN : ADDR_POLICY_ACCEPTED;
  427. }
  428. /* return 1 if all running routers will reject addr:port, return 0 if
  429. any might accept it. */
  430. int router_exit_policy_all_routers_reject(uint32_t addr, uint16_t port) {
  431. int i;
  432. routerinfo_t *router;
  433. for (i=0;i<routerlist->n_routers;i++) {
  434. router = routerlist->routers[i];
  435. if (router->is_running && router_compare_addr_to_exit_policy(
  436. addr, port, router->exit_policy) != ADDR_POLICY_REJECTED)
  437. return 0; /* this one could be ok. good enough. */
  438. }
  439. return 1; /* all will reject. */
  440. }
  441. int router_exit_policy_rejects_all(routerinfo_t *router) {
  442. return router_compare_addr_to_exit_policy(0, 0, router->exit_policy)
  443. == ADDR_POLICY_REJECTED;
  444. }
  445. /* Helper function: parse a directory from 's' and, when done, store the
  446. * resulting routerlist in *dest, freeing the old value if necessary.
  447. * If pkey is provided, we check the directory signature with pkey.
  448. */
  449. static int
  450. router_get_routerlist_from_directory_impl(const char *s, routerlist_t **dest,
  451. crypto_pk_env_t *pkey)
  452. {
  453. directory_token_t tok;
  454. char digest[20];
  455. char signed_digest[128];
  456. routerlist_t *new_dir = NULL;
  457. char *versions = NULL;
  458. struct tm published;
  459. time_t published_on;
  460. char *good_nickname_lst[1024];
  461. int n_good_nicknames = 0;
  462. int i;
  463. /* Local helper macro: get the next token from s (advancing s) and
  464. * bail on failure. */
  465. #define NEXT_TOK() \
  466. do { \
  467. if (router_get_next_token(&s, &tok)) { \
  468. log_fn(LOG_WARN, "Error reading directory: %s", tok.val.error); \
  469. goto err; \
  470. } } while (0)
  471. /* Local helper macro: bail if the most recently read token is not of
  472. * the given type. */
  473. #define TOK_IS(type,name) \
  474. do { \
  475. if (tok.tp != type) { \
  476. router_release_token(&tok); \
  477. log_fn(LOG_WARN, "Error reading directory: expected %s", name); \
  478. goto err; \
  479. } } while(0)
  480. /* Local helper macro: Number of args in most recent token. */
  481. #define N_ARGS tok.val.cmd.n_args
  482. /* Local helper macro: Array of args to most recent token. */
  483. #define ARGS tok.val.cmd.args
  484. tok.tp = _NIL;
  485. if (router_get_dir_hash(s, digest)) {
  486. log_fn(LOG_WARN, "Unable to compute digest of directory");
  487. goto err;
  488. }
  489. log(LOG_DEBUG,"Received directory hashes to %02x:%02x:%02x:%02x",
  490. ((int)digest[0])&0xff,((int)digest[1])&0xff,
  491. ((int)digest[2])&0xff,((int)digest[3])&0xff);
  492. NEXT_TOK();
  493. TOK_IS(K_SIGNED_DIRECTORY, "signed-directory");
  494. NEXT_TOK();
  495. TOK_IS(K_PUBLISHED, "published");
  496. assert(N_ARGS == 1);
  497. if (!strptime(ARGS[0], "%Y-%m-%d %H:%M:%S", &published)) {
  498. log_fn(LOG_WARN, "Published time was unparseable"); goto err;
  499. }
  500. published_on = tor_timegm(&published);
  501. NEXT_TOK();
  502. TOK_IS(K_RECOMMENDED_SOFTWARE, "recommended-software");
  503. if (N_ARGS != 1) {
  504. log_fn(LOG_WARN, "Invalid recommended-software line");
  505. goto err;
  506. }
  507. versions = ARGS[0];
  508. tok.val.cmd.n_args = 0; /* Don't let the versions string get freed. */
  509. NEXT_TOK();
  510. TOK_IS(K_RUNNING_ROUTERS, "running-routers");
  511. n_good_nicknames = N_ARGS;
  512. memcpy(good_nickname_lst, ARGS, n_good_nicknames*sizeof(char *));
  513. N_ARGS = 0; /* Don't free the strings in good_nickname_lst yet. */
  514. /* Read the router list from s, advancing s up past the end of the last
  515. * router. */
  516. if (router_get_list_from_string_impl(&s, &new_dir,
  517. n_good_nicknames,
  518. (const char**)good_nickname_lst)) {
  519. log_fn(LOG_WARN, "Error reading routers from directory");
  520. goto err;
  521. }
  522. for (i = 0; i < n_good_nicknames; ++i) {
  523. tor_free(good_nickname_lst[i]); /* now free them */
  524. }
  525. new_dir->software_versions = versions; versions = NULL;
  526. new_dir->published_on = published_on;
  527. NEXT_TOK();
  528. TOK_IS(K_DIRECTORY_SIGNATURE, "directory-signature");
  529. NEXT_TOK();
  530. TOK_IS(_SIGNATURE, "signature");
  531. if (pkey) {
  532. if (crypto_pk_public_checksig(pkey, tok.val.signature, 128, signed_digest)
  533. != 20) {
  534. log_fn(LOG_WARN, "Error reading directory: invalid signature.");
  535. goto err;
  536. }
  537. log(LOG_DEBUG,"Signed directory hash starts %02x:%02x:%02x:%02x",
  538. ((int)signed_digest[0])&0xff,((int)signed_digest[1])&0xff,
  539. ((int)signed_digest[2])&0xff,((int)signed_digest[3])&0xff);
  540. if (memcmp(digest, signed_digest, 20)) {
  541. log_fn(LOG_WARN, "Error reading directory: signature does not match.");
  542. goto err;
  543. }
  544. }
  545. NEXT_TOK();
  546. TOK_IS(_EOF, "end of directory");
  547. if (*dest)
  548. routerlist_free(*dest);
  549. *dest = new_dir;
  550. return 0;
  551. err:
  552. router_release_token(&tok);
  553. if (new_dir)
  554. routerlist_free(new_dir);
  555. tor_free(versions);
  556. for (i = 0; i < n_good_nicknames; ++i) {
  557. tor_free(good_nickname_lst[i]);
  558. }
  559. return -1;
  560. #undef NEXT_TOK
  561. #undef TOK_IS
  562. #undef ARGS
  563. #undef N_ARGS
  564. }
  565. /* Helper function: Given a string *s containing a concatenated
  566. * sequence of router descriptors, parses them and stores the result
  567. * in *dest. If good_nickname_lst is provided, then routers whose
  568. * nicknames are not listed are marked as nonrunning. Advances *s to
  569. * a point immediately following the last router entry. Returns 0 on
  570. * success and -1 on failure.
  571. */
  572. static int
  573. router_get_list_from_string_impl(const char **s, routerlist_t **dest,
  574. int n_good_nicknames,
  575. const char **good_nickname_lst)
  576. {
  577. routerinfo_t *router;
  578. routerinfo_t **rarray;
  579. int rarray_len = 0;
  580. int i;
  581. assert(s && *s);
  582. rarray = (routerinfo_t **)
  583. tor_malloc((sizeof(routerinfo_t *))*MAX_ROUTERS_IN_DIR);
  584. while (1) {
  585. *s = eat_whitespace(*s);
  586. /* Don't start parsing the rest of *s unless it contains a router. */
  587. if (strncmp(*s, "router ", 7)!=0)
  588. break;
  589. router = router_get_entry_from_string(s);
  590. if (!router) {
  591. log_fn(LOG_WARN, "Error reading router");
  592. for(i=0;i<rarray_len;i++)
  593. routerinfo_free(rarray[i]);
  594. free(rarray);
  595. return -1;
  596. }
  597. if (rarray_len >= MAX_ROUTERS_IN_DIR) {
  598. log_fn(LOG_WARN, "too many routers");
  599. routerinfo_free(router);
  600. continue;
  601. }
  602. if (n_good_nicknames>=0) {
  603. router->is_running = 0;
  604. for (i = 0; i < n_good_nicknames; ++i) {
  605. if (0==strcasecmp(good_nickname_lst[i], router->nickname)) {
  606. router->is_running = 1;
  607. break;
  608. }
  609. }
  610. } else {
  611. router->is_running = 1; /* start out assuming all dirservers are up */
  612. }
  613. rarray[rarray_len++] = router;
  614. log_fn(LOG_DEBUG,"just added router #%d.",rarray_len);
  615. }
  616. if (*dest)
  617. routerlist_free(*dest);
  618. *dest = (routerlist_t *)tor_malloc(sizeof(routerlist_t));
  619. (*dest)->routers = rarray;
  620. (*dest)->n_routers = rarray_len;
  621. (*dest)->software_versions = NULL;
  622. return 0;
  623. }
  624. /* Helper function: reads a single router entry from *s, and advances
  625. * *s so it points to just after the router it just read.
  626. * mallocs a new router and returns it if all goes well, else returns
  627. * NULL.
  628. */
  629. routerinfo_t *router_get_entry_from_string(const char**s) {
  630. routerinfo_t *router = NULL;
  631. char signed_digest[128];
  632. char digest[128];
  633. directory_token_t _tok;
  634. directory_token_t *tok = &_tok;
  635. struct tm published;
  636. int t;
  637. /* Helper macro: read the next token from *s, advance *s, and bail
  638. if there's an error */
  639. #define NEXT_TOKEN() \
  640. do { if (router_get_next_token(s, tok)) { \
  641. log_fn(LOG_WARN, "Error reading directory: %s", tok->val.error);\
  642. goto err; \
  643. } } while(0)
  644. #define ARGS tok->val.cmd.args
  645. #define N_ARGS tok->val.cmd.n_args
  646. _tok.tp = _NIL;
  647. if (router_get_router_hash(*s, digest) < 0) {
  648. log_fn(LOG_WARN, "Couldn't compute router hash.");
  649. return NULL;
  650. }
  651. NEXT_TOKEN(); /* XXX This leaks some arguments. */
  652. if (tok->tp != K_ROUTER) {
  653. log_fn(LOG_WARN,"Entry does not start with \"router\"");
  654. goto err;
  655. }
  656. router = tor_malloc_zero(sizeof(routerinfo_t));
  657. router->onion_pkey = router->identity_pkey = router->link_pkey = NULL;
  658. /* XXXBC move to <7 once we require bandwidthburst */
  659. if (N_ARGS < 6) {
  660. log_fn(LOG_WARN,"Wrong # of arguments to \"router\"");
  661. goto err;
  662. }
  663. router->nickname = tor_strdup(ARGS[0]);
  664. if (strlen(router->nickname) > MAX_NICKNAME_LEN) {
  665. log_fn(LOG_WARN,"Router nickname too long.");
  666. goto err;
  667. }
  668. if (strspn(router->nickname, LEGAL_NICKNAME_CHARACTERS) !=
  669. strlen(router->nickname)) {
  670. log_fn(LOG_WARN, "Router nickname contains illegal characters.");
  671. goto err;
  672. }
  673. /* read router.address */
  674. router->address = tor_strdup(ARGS[1]);
  675. router->addr = 0;
  676. /* Read router->or_port */
  677. router->or_port = atoi(ARGS[2]);
  678. if(!router->or_port) {
  679. log_fn(LOG_WARN,"or_port unreadable or 0. Failing.");
  680. goto err;
  681. }
  682. /* Router->socks_port */
  683. router->socks_port = atoi(ARGS[3]);
  684. /* Router->dir_port */
  685. router->dir_port = atoi(ARGS[4]);
  686. /* Router->bandwidth */
  687. router->bandwidthrate = atoi(ARGS[5]);
  688. if (!router->bandwidthrate) {
  689. log_fn(LOG_WARN,"bandwidthrate unreadable or 0. Failing.");
  690. goto err;
  691. }
  692. #if XXXBC
  693. router->bandwidthburst = atoi(ARGS[6]);
  694. if (!router->bandwidthburst) {
  695. log_fn(LOG_WARN,"bandwidthburst unreadable or 0. Failing.");
  696. goto err;
  697. }
  698. #else
  699. router->bandwidthburst = 10*router->bandwidthrate;
  700. #endif
  701. log_fn(LOG_DEBUG,"or_port %d, socks_port %d, dir_port %d, bandwidthrate %u, bandwidthburst %u.",
  702. router->or_port, router->socks_port, router->dir_port,
  703. (unsigned) router->bandwidthrate, (unsigned) router->bandwidthburst);
  704. /* XXX Later, require platform before published. */
  705. NEXT_TOKEN();
  706. if (tok->tp == K_PLATFORM) {
  707. NEXT_TOKEN();
  708. }
  709. if (tok->tp != K_PUBLISHED) {
  710. log_fn(LOG_WARN, "Missing published time"); goto err;
  711. }
  712. assert(N_ARGS == 1);
  713. if (!strptime(ARGS[0], "%Y-%m-%d %H:%M:%S", &published)) {
  714. log_fn(LOG_WARN, "Published time was unparseable"); goto err;
  715. }
  716. router->published_on = tor_timegm(&published);
  717. NEXT_TOKEN();
  718. if (tok->tp != K_ONION_KEY) {
  719. log_fn(LOG_WARN, "Missing onion-key"); goto err;
  720. }
  721. NEXT_TOKEN();
  722. if (tok->tp != _PUBLIC_KEY) {
  723. log_fn(LOG_WARN, "Missing onion key"); goto err;
  724. } /* XXX Check key length */
  725. router->onion_pkey = tok->val.public_key;
  726. tok->val.public_key = NULL; /* Prevent free */
  727. NEXT_TOKEN();
  728. if (tok->tp != K_LINK_KEY) {
  729. log_fn(LOG_WARN, "Missing link-key"); goto err;
  730. }
  731. NEXT_TOKEN();
  732. if (tok->tp != _PUBLIC_KEY) {
  733. log_fn(LOG_WARN, "Missing link key"); goto err;
  734. } /* XXX Check key length */
  735. router->link_pkey = tok->val.public_key;
  736. tok->val.public_key = NULL; /* Prevent free */
  737. NEXT_TOKEN();
  738. if (tok->tp != K_SIGNING_KEY) {
  739. log_fn(LOG_WARN, "Missing signing-key"); goto err;
  740. }
  741. NEXT_TOKEN();
  742. if (tok->tp != _PUBLIC_KEY) {
  743. log_fn(LOG_WARN, "Missing signing key"); goto err;
  744. }
  745. router->identity_pkey = tok->val.public_key;
  746. tok->val.public_key = NULL; /* Prevent free */
  747. NEXT_TOKEN();
  748. while (tok->tp == K_ACCEPT || tok->tp == K_REJECT) {
  749. router_add_exit_policy(router, tok);
  750. NEXT_TOKEN(); /* This also leaks some args. XXX */
  751. }
  752. if (tok->tp != K_ROUTER_SIGNATURE) {
  753. log_fn(LOG_WARN,"Missing router signature");
  754. goto err;
  755. }
  756. NEXT_TOKEN();
  757. if (tok->tp != _SIGNATURE) {
  758. log_fn(LOG_WARN,"Missing router signature");
  759. goto err;
  760. }
  761. assert (router->identity_pkey);
  762. if ((t=crypto_pk_public_checksig(router->identity_pkey, tok->val.signature,
  763. 128, signed_digest)) != 20) {
  764. log_fn(LOG_WARN, "Invalid signature %d",t);
  765. goto err;
  766. }
  767. if (memcmp(digest, signed_digest, 20)) {
  768. log_fn(LOG_WARN, "Mismatched signature");
  769. goto err;
  770. }
  771. router_release_token(tok); /* free the signature */
  772. return router;
  773. err:
  774. router_release_token(tok);
  775. routerinfo_free(router);
  776. return NULL;
  777. #undef ARGS
  778. #undef N_ARGS
  779. #undef NEXT_TOKEN
  780. }
  781. /* Parse the exit policy in the string 's' and add it to 'router'.
  782. */
  783. int
  784. router_add_exit_policy_from_string(routerinfo_t *router, const char *s)
  785. {
  786. directory_token_t tok;
  787. const char *cp;
  788. char *tmp;
  789. int r;
  790. int len, idx;
  791. tok.tp = _NIL;
  792. /* *s might not end with \n, so we need to extend it with one. */
  793. len = strlen(s);
  794. cp = tmp = tor_malloc(len+2);
  795. for (idx = 0; idx < len; ++idx) {
  796. tmp[idx] = tolower(s[idx]);
  797. }
  798. tmp[len]='\n';
  799. tmp[len+1]='\0';
  800. if (router_get_next_token(&cp, &tok)) {
  801. log_fn(LOG_WARN, "Error reading exit policy: %s", tok.val.error);
  802. goto err;
  803. }
  804. if (tok.tp != K_ACCEPT && tok.tp != K_REJECT) {
  805. log_fn(LOG_WARN, "Expected 'accept' or 'reject'.");
  806. goto err;
  807. }
  808. /* Now that we've gotten an exit policy, add it to the router. */
  809. r = router_add_exit_policy(router, &tok);
  810. goto done;
  811. err:
  812. r = -1;
  813. done:
  814. free(tmp);
  815. router_release_token(&tok);
  816. return r;
  817. }
  818. /* Given a K_ACCEPT or K_REJECT token and a router, create a new exit_policy_t
  819. * corresponding to the token, and add it to 'router' */
  820. static int router_add_exit_policy(routerinfo_t *router,
  821. directory_token_t *tok) {
  822. struct exit_policy_t *tmpe, *newe;
  823. struct in_addr in;
  824. char *arg, *address, *mask, *port, *endptr;
  825. int bits;
  826. assert(tok->tp == K_REJECT || tok->tp == K_ACCEPT);
  827. if (tok->val.cmd.n_args != 1)
  828. return -1;
  829. arg = tok->val.cmd.args[0];
  830. newe = tor_malloc_zero(sizeof(struct exit_policy_t));
  831. newe->string = tor_malloc(8+strlen(arg));
  832. if (tok->tp == K_REJECT) {
  833. strcpy(newe->string, "reject ");
  834. newe->policy_type = EXIT_POLICY_REJECT;
  835. } else {
  836. strcpy(newe->string, "accept ");
  837. newe->policy_type = EXIT_POLICY_ACCEPT;
  838. }
  839. strcat(newe->string, arg);
  840. address = arg;
  841. mask = strchr(arg,'/');
  842. port = strchr(mask?mask:arg,':');
  843. /* Break 'arg' into separate strings. 'arg' was already strdup'd by
  844. * _router_get_next_token, so it's safe to modify.
  845. */
  846. if (mask)
  847. *mask++ = 0;
  848. if (port)
  849. *port++ = 0;
  850. if (strcmp(address, "*") == 0) {
  851. newe->addr = 0;
  852. } else if (inet_aton(address, &in) != 0) {
  853. newe->addr = ntohl(in.s_addr);
  854. } else {
  855. log_fn(LOG_WARN, "Malformed IP %s in exit policy; rejecting.",
  856. address);
  857. goto policy_read_failed;
  858. }
  859. if (!mask) {
  860. if (strcmp(address, "*") == 0)
  861. newe->msk = 0;
  862. else
  863. newe->msk = 0xFFFFFFFFu;
  864. } else {
  865. endptr = NULL;
  866. bits = (int) strtol(mask, &endptr, 10);
  867. if (!*endptr) {
  868. /* strtol handled the whole mask. */
  869. newe->msk = ~((1<<(32-bits))-1);
  870. } else if (inet_aton(mask, &in) != 0) {
  871. newe->msk = ntohl(in.s_addr);
  872. } else {
  873. log_fn(LOG_WARN, "Malformed mask %s on exit policy; rejecting.",
  874. mask);
  875. goto policy_read_failed;
  876. }
  877. }
  878. if (!port || strcmp(port, "*") == 0) {
  879. newe->prt_min = 0;
  880. newe->prt_max = 65535;
  881. } else {
  882. endptr = NULL;
  883. newe->prt_min = strtol(port, &endptr, 10);
  884. if (*endptr == '-') {
  885. port = endptr+1;
  886. endptr = NULL;
  887. newe->prt_max = strtol(port, &endptr, 10);
  888. if (*endptr) {
  889. log_fn(LOG_WARN, "Malformed port %s on exit policy; rejecting.",
  890. port);
  891. }
  892. } else if (*endptr) {
  893. log_fn(LOG_WARN, "Malformed port %s on exit policy; rejecting.",
  894. port);
  895. goto policy_read_failed;
  896. } else {
  897. newe->prt_max = newe->prt_min;
  898. }
  899. }
  900. in.s_addr = htonl(newe->addr);
  901. address = tor_strdup(inet_ntoa(in));
  902. in.s_addr = htonl(newe->msk);
  903. log_fn(LOG_DEBUG,"%s %s/%s:%d-%d",
  904. newe->policy_type == EXIT_POLICY_REJECT ? "reject" : "accept",
  905. address, inet_ntoa(in), newe->prt_min, newe->prt_max);
  906. tor_free(address);
  907. /* now link newe onto the end of exit_policy */
  908. if(!router->exit_policy) {
  909. router->exit_policy = newe;
  910. return 0;
  911. }
  912. for(tmpe=router->exit_policy; tmpe->next; tmpe=tmpe->next) ;
  913. tmpe->next = newe;
  914. return 0;
  915. policy_read_failed:
  916. assert(newe->string);
  917. log_fn(LOG_WARN,"Couldn't parse line '%s'. Dropping", newe->string);
  918. tor_free(newe->string);
  919. free(newe);
  920. return -1;
  921. }
  922. /* ------------------------------------------------------------ */
  923. /* Tokenizer for router descriptors and directories. */
  924. /* Every keyword takes either... */
  925. typedef enum {
  926. NO_ARGS, /* (1) no arguments, ever */
  927. ARGS, /* (2) a list of arguments separated by spaces */
  928. CONCAT_ARGS, /* or (3) the rest of the line, treated as a single argument. */
  929. } arg_syntax;
  930. /* Table mapping keywods to token value and to argument rules. */
  931. static struct { char *t; int v; arg_syntax s; } token_table[] = {
  932. { "accept", K_ACCEPT, ARGS },
  933. { "directory-signature", K_DIRECTORY_SIGNATURE, NO_ARGS },
  934. { "reject", K_REJECT, ARGS },
  935. { "router", K_ROUTER, ARGS },
  936. { "recommended-software", K_RECOMMENDED_SOFTWARE, ARGS },
  937. { "signed-directory", K_SIGNED_DIRECTORY, NO_ARGS },
  938. { "signing-key", K_SIGNING_KEY, NO_ARGS },
  939. { "onion-key", K_ONION_KEY, NO_ARGS },
  940. { "link-key", K_LINK_KEY, NO_ARGS },
  941. { "router-signature", K_ROUTER_SIGNATURE, NO_ARGS },
  942. { "published", K_PUBLISHED, CONCAT_ARGS },
  943. { "running-routers", K_RUNNING_ROUTERS, ARGS },
  944. { "platform", K_PLATFORM, ARGS },
  945. { NULL, -1 }
  946. };
  947. /* Free any malloced resources allocated for a token. Does not free
  948. * the token itself.
  949. */
  950. static void
  951. router_release_token(directory_token_t *tok)
  952. {
  953. int i;
  954. switch (tok->tp)
  955. {
  956. case _SIGNATURE:
  957. if (tok->val.signature)
  958. free(tok->val.signature);
  959. break;
  960. case _PUBLIC_KEY:
  961. if (tok->val.public_key)
  962. crypto_free_pk_env(tok->val.public_key);
  963. break;
  964. case _ERR:
  965. case _EOF:
  966. case _NIL:
  967. break;
  968. default:
  969. for (i = 0; i < tok->val.cmd.n_args; ++i) {
  970. tor_free(tok->val.cmd.args[i]);
  971. }
  972. }
  973. tok->tp = _NIL;
  974. }
  975. /* Helper function: read the next token from *s, and stores it into *tok.
  976. * If *tok already contains a token (tok->tp != _NIL), free the resources
  977. * held by *tok. Advance *s to a point immediately after the token.
  978. *
  979. *
  980. */
  981. static int
  982. _router_get_next_token(const char **s, directory_token_t *tok) {
  983. const char *next;
  984. crypto_pk_env_t *pkey = NULL;
  985. char *signature = NULL;
  986. int i, done;
  987. /* Clear the token _first_, so that we can clear it safely. */
  988. router_release_token(tok);
  989. tok->tp = _ERR;
  990. tok->val.error = "";
  991. *s = eat_whitespace(*s);
  992. if (!**s) {
  993. tok->tp = _EOF;
  994. return 0;
  995. } else if (**s == '-') {
  996. next = strchr(*s, '\n');
  997. if (! next) { tok->val.error = "No newline at EOF"; return -1; }
  998. ++next;
  999. if (! strncmp(*s, OR_PUBLICKEY_BEGIN_TAG, next-*s)) {
  1000. /* We have a ----BEGIN PUBLIC KEY----- */
  1001. next = strstr(*s, OR_PUBLICKEY_END_TAG);
  1002. if (!next) { tok->val.error = "No public key end tag found"; return -1; }
  1003. next = strchr(next, '\n'); /* Part of OR_PUBLICKEY_END_TAG; can't fail.*/
  1004. ++next;
  1005. if (!(pkey = crypto_new_pk_env(CRYPTO_PK_RSA)))
  1006. return -1;
  1007. if (crypto_pk_read_public_key_from_string(pkey, *s, next-*s)) {
  1008. crypto_free_pk_env(pkey);
  1009. tok->val.error = "Couldn't parse public key.";
  1010. return -1;
  1011. }
  1012. tok->tp = _PUBLIC_KEY;
  1013. tok->val.public_key = pkey;
  1014. *s = next;
  1015. return 0;
  1016. } else if (! strncmp(*s, OR_SIGNATURE_BEGIN_TAG, next-*s)) {
  1017. /* We have a -----BEGIN SIGNATURE----- */
  1018. /* Advance past newline; can't fail. */
  1019. *s = strchr(*s, '\n');
  1020. ++*s;
  1021. /* Find end of base64'd data */
  1022. next = strstr(*s, OR_SIGNATURE_END_TAG);
  1023. if (!next) { tok->val.error = "No signature end tag found"; return -1; }
  1024. signature = tor_malloc(256);
  1025. i = base64_decode(signature, 256, *s, next-*s);
  1026. if (i<0) {
  1027. free(signature);
  1028. tok->val.error = "Error decoding signature."; return -1;
  1029. } else if (i != 128) {
  1030. free(signature);
  1031. tok->val.error = "Bad length on decoded signature."; return -1;
  1032. }
  1033. tok->tp = _SIGNATURE;
  1034. tok->val.signature = signature;
  1035. next = strchr(next, '\n'); /* Part of OR_SIGNATURE_END_TAG; can't fail.*/
  1036. *s = next+1;
  1037. return 0;
  1038. } else {
  1039. tok->val.error = "Unrecognized begin line"; return -1;
  1040. }
  1041. } else {
  1042. next = find_whitespace(*s);
  1043. if (!next) {
  1044. tok->val.error = "Unexpected EOF"; return -1;
  1045. }
  1046. /* It's a keyword... but which one? */
  1047. for (i = 0 ; token_table[i].t ; ++i) {
  1048. if (!strncmp(token_table[i].t, *s, next-*s)) {
  1049. /* We've found the keyword. */
  1050. tok->tp = token_table[i].v;
  1051. if (token_table[i].s == ARGS) {
  1052. /* This keyword takes multiple arguments. */
  1053. i = 0;
  1054. done = (*next == '\n');
  1055. *s = eat_whitespace_no_nl(next);
  1056. while (**s != '\n' && i < MAX_ARGS && !done) {
  1057. next = find_whitespace(*s);
  1058. if (*next == '\n')
  1059. done = 1;
  1060. tok->val.cmd.args[i++] = tor_strndup(*s,next-*s);
  1061. /* XXX this line (the strndup) is the memory leak. */
  1062. *s = eat_whitespace_no_nl(next+1);
  1063. }
  1064. tok->val.cmd.n_args = i;
  1065. if (i >= MAX_ARGS) {
  1066. router_release_token(tok);
  1067. tok->tp = _ERR;
  1068. tok->val.error = "Too many arguments"; return -1;
  1069. }
  1070. } else if (token_table[i].s == CONCAT_ARGS) {
  1071. /* The keyword takes the line as a single argument */
  1072. *s = eat_whitespace_no_nl(next);
  1073. next = strchr(*s, '\n');
  1074. if (!next) {
  1075. tok->tp = _ERR;
  1076. tok->val.error = "Unexpected EOF"; return -1;
  1077. }
  1078. tok->val.cmd.args[0] = tor_strndup(*s,next-*s);
  1079. tok->val.cmd.n_args = 1;
  1080. *s = eat_whitespace_no_nl(next+1);
  1081. } else {
  1082. /* The keyword takes no arguments. */
  1083. *s = eat_whitespace_no_nl(next);
  1084. if (**s != '\n') {
  1085. tok->tp = _ERR;
  1086. tok->val.error = "Unexpected arguments"; return -1;
  1087. }
  1088. tok->val.cmd.n_args = 0;
  1089. *s = eat_whitespace_no_nl(*s+1);
  1090. }
  1091. return 0;
  1092. }
  1093. }
  1094. tok->val.error = "Unrecognized command"; return -1;
  1095. }
  1096. }
  1097. #ifdef DEBUG_ROUTER_TOKENS
  1098. static void
  1099. router_dump_token(directory_token_t *tok) {
  1100. int i;
  1101. switch(tok->tp)
  1102. {
  1103. case _SIGNATURE:
  1104. puts("(signature)");
  1105. return;
  1106. case _PUBLIC_KEY:
  1107. puts("(public key)");
  1108. return;
  1109. case _ERR:
  1110. printf("(Error: %s\n)", tok->val.error);
  1111. return;
  1112. case _EOF:
  1113. puts("EOF");
  1114. return;
  1115. case K_ACCEPT: printf("Accept"); break;
  1116. case K_DIRECTORY_SIGNATURE: printf("Directory-Signature"); break;
  1117. case K_REJECT: printf("Reject"); break;
  1118. case K_RECOMMENDED_SOFTWARE: printf("Server-Software"); break;
  1119. case K_ROUTER: printf("Router"); break;
  1120. case K_SIGNED_DIRECTORY: printf("Signed-Directory"); break;
  1121. case K_SIGNING_KEY: printf("Signing-Key"); break;
  1122. case K_ONION_KEY: printf("Onion-key"); break;
  1123. case K_LINK_KEY: printf("Link-key"); break;
  1124. case K_ROUTER_SIGNATURE: printf("Router-signature"); break;
  1125. case K_PUBLISHED: printf("Published"); break;
  1126. case K_RUNNING_ROUTERS: printf("Running-routers"); break;
  1127. case K_PLATFORM: printf("Platform"); break;
  1128. default:
  1129. printf("?????? %d\n", tok->tp); return;
  1130. }
  1131. for (i = 0; i < tok->val.cmd.n_args; ++i) {
  1132. printf(" \"%s\"", tok->val.cmd.args[i]);
  1133. }
  1134. printf("\n");
  1135. return;
  1136. }
  1137. static int
  1138. router_get_next_token(const char **s, directory_token_t *tok) {
  1139. int i;
  1140. i = _router_get_next_token(s, tok);
  1141. router_dump_token(tok);
  1142. return i;
  1143. }
  1144. #else
  1145. #define router_get_next_token _router_get_next_token
  1146. #endif
  1147. /* Compute the SHA digest of the substring of s taken from the first
  1148. * occurrence of start_str through the first newline after the first
  1149. * subsequent occurrence of end_str; store the 20-byte result in 'digest';
  1150. * return 0 on success.
  1151. *
  1152. * If no such substring exists, return -1.
  1153. */
  1154. static int router_get_hash_impl(const char *s, char *digest,
  1155. const char *start_str,
  1156. const char *end_str)
  1157. {
  1158. char *start, *end;
  1159. start = strstr(s, start_str);
  1160. if (!start) {
  1161. log_fn(LOG_WARN,"couldn't find \"%s\"",start_str);
  1162. return -1;
  1163. }
  1164. end = strstr(start+strlen(start_str), end_str);
  1165. if (!end) {
  1166. log_fn(LOG_WARN,"couldn't find \"%s\"",end_str);
  1167. return -1;
  1168. }
  1169. end = strchr(end, '\n');
  1170. if (!end) {
  1171. log_fn(LOG_WARN,"couldn't find EOL");
  1172. return -1;
  1173. }
  1174. ++end;
  1175. if (crypto_SHA_digest(start, end-start, digest)) {
  1176. log_fn(LOG_WARN,"couldn't compute digest");
  1177. return -1;
  1178. }
  1179. return 0;
  1180. }
  1181. /*
  1182. Local Variables:
  1183. mode:c
  1184. indent-tabs-mode:nil
  1185. c-basic-offset:2
  1186. End:
  1187. */