routerlist.c 37 KB

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