routerlist.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301
  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. int maybe = 0;
  355. struct in_addr in;
  356. struct exit_policy_t *tmpe;
  357. for(tmpe=policy; tmpe; tmpe=tmpe->next) {
  358. log_fn(LOG_DEBUG,"Considering exit policy %s", tmpe->string);
  359. maybe = 0;
  360. if (!addr) {
  361. /* Address is unknown. */
  362. if (port >= tmpe->prt_min && port <= tmpe->prt_max) {
  363. /* The port definitely matches. */
  364. if (tmpe->msk == 0) {
  365. match = 1;
  366. } else {
  367. maybe = 1;
  368. }
  369. } else if (!port) {
  370. /* The port maybe matches. */
  371. maybe = 1;
  372. }
  373. } else {
  374. /* Address is known */
  375. if ( (addr & tmpe->msk) == (tmpe->addr & tmpe->msk) &&
  376. (port >= tmpe->prt_min && port <= tmpe->prt_max) ) {
  377. /* Exact match for the policy */
  378. match = 1;
  379. }
  380. }
  381. if (maybe) {
  382. if (tmpe->policy_type == EXIT_POLICY_REJECT)
  383. maybe_reject = 1;
  384. else
  385. maybe_accept = 1;
  386. }
  387. if (match) {
  388. in.s_addr = htonl(addr);
  389. log_fn(LOG_INFO,"Address %s:%d matches exit policy '%s'",
  390. inet_ntoa(in), port, tmpe->string);
  391. if(tmpe->policy_type == EXIT_POLICY_ACCEPT) {
  392. /* If we already hit a clause that might trigger a 'reject', than we
  393. * can't be sure of this certain 'accept'.*/
  394. return maybe_reject ? ADDR_POLICY_UNKNOWN : ADDR_POLICY_ACCEPTED;
  395. } else {
  396. return maybe_accept ? ADDR_POLICY_UNKNOWN : ADDR_POLICY_REJECTED;
  397. }
  398. }
  399. }
  400. /* accept all by default. */
  401. return maybe_reject ? ADDR_POLICY_UNKNOWN : ADDR_POLICY_ACCEPTED;
  402. }
  403. /* return 1 if all running routers will reject addr:port, return 0 if
  404. any might accept it. */
  405. int router_exit_policy_all_routers_reject(uint32_t addr, uint16_t port) {
  406. int i;
  407. routerinfo_t *router;
  408. for (i=0;i<routerlist->n_routers;i++) {
  409. router = routerlist->routers[i];
  410. if (router->is_running && router_compare_addr_to_exit_policy(
  411. addr, port, router->exit_policy) != ADDR_POLICY_REJECTED)
  412. return 0; /* this one could be ok. good enough. */
  413. }
  414. return 1; /* all will reject. */
  415. }
  416. int router_exit_policy_rejects_all(routerinfo_t *router) {
  417. return router_compare_addr_to_exit_policy(0, 0, router->exit_policy)
  418. == ADDR_POLICY_REJECTED;
  419. }
  420. /* Helper function: parse a directory from 's' and, when done, store the
  421. * resulting routerlist in *dest, freeing the old value if necessary.
  422. * If pkey is provided, we check the directory signature with pkey.
  423. */
  424. static int
  425. router_get_routerlist_from_directory_impl(const char *s, routerlist_t **dest,
  426. crypto_pk_env_t *pkey)
  427. {
  428. directory_token_t tok;
  429. char digest[20];
  430. char signed_digest[128];
  431. routerlist_t *new_dir = NULL;
  432. char *versions = NULL;
  433. struct tm published;
  434. time_t published_on;
  435. char *good_nickname_lst[1024];
  436. int n_good_nicknames = 0;
  437. int i;
  438. /* Local helper macro: get the next token from s (advancing s) and
  439. * bail on failure. */
  440. #define NEXT_TOK() \
  441. do { \
  442. if (router_get_next_token(&s, &tok)) { \
  443. log_fn(LOG_WARN, "Error reading directory: %s", tok.val.error); \
  444. goto err; \
  445. } } while (0)
  446. /* Local helper macro: bail if the most recently read token is not of
  447. * the given type. */
  448. #define TOK_IS(type,name) \
  449. do { \
  450. if (tok.tp != type) { \
  451. router_release_token(&tok); \
  452. log_fn(LOG_WARN, "Error reading directory: expected %s", name); \
  453. goto err; \
  454. } } while(0)
  455. /* Local helper macro: Number of args in most recent token. */
  456. #define N_ARGS tok.val.cmd.n_args
  457. /* Local helper macro: Array of args to most recent token. */
  458. #define ARGS tok.val.cmd.args
  459. tok.tp = _NIL;
  460. if (router_get_dir_hash(s, digest)) {
  461. log_fn(LOG_WARN, "Unable to compute digest of directory");
  462. goto err;
  463. }
  464. log(LOG_DEBUG,"Received directory hashes to %02x:%02x:%02x:%02x",
  465. ((int)digest[0])&0xff,((int)digest[1])&0xff,
  466. ((int)digest[2])&0xff,((int)digest[3])&0xff);
  467. NEXT_TOK();
  468. TOK_IS(K_SIGNED_DIRECTORY, "signed-directory");
  469. NEXT_TOK();
  470. TOK_IS(K_PUBLISHED, "published");
  471. assert(N_ARGS == 1);
  472. if (!strptime(ARGS[0], "%Y-%m-%d %H:%M:%S", &published)) {
  473. log_fn(LOG_WARN, "Published time was unparseable"); goto err;
  474. }
  475. published_on = tor_timegm(&published);
  476. NEXT_TOK();
  477. TOK_IS(K_RECOMMENDED_SOFTWARE, "recommended-software");
  478. if (N_ARGS != 1) {
  479. log_fn(LOG_WARN, "Invalid recommended-software line");
  480. goto err;
  481. }
  482. versions = ARGS[0];
  483. tok.val.cmd.n_args = 0; /* Don't let the versions string get freed. */
  484. NEXT_TOK();
  485. TOK_IS(K_RUNNING_ROUTERS, "running-routers");
  486. n_good_nicknames = N_ARGS;
  487. memcpy(good_nickname_lst, ARGS, n_good_nicknames*sizeof(char *));
  488. N_ARGS = 0; /* Don't free the strings in good_nickname_lst yet. */
  489. /* Read the router list from s, advancing s up past the end of the last
  490. * router. */
  491. if (router_get_list_from_string_impl(&s, &new_dir,
  492. n_good_nicknames,
  493. (const char**)good_nickname_lst)) {
  494. log_fn(LOG_WARN, "Error reading routers from directory");
  495. goto err;
  496. }
  497. for (i = 0; i < n_good_nicknames; ++i) {
  498. tor_free(good_nickname_lst[i]); /* now free them */
  499. }
  500. new_dir->software_versions = versions; versions = NULL;
  501. new_dir->published_on = published_on;
  502. NEXT_TOK();
  503. TOK_IS(K_DIRECTORY_SIGNATURE, "directory-signature");
  504. NEXT_TOK();
  505. TOK_IS(_SIGNATURE, "signature");
  506. if (pkey) {
  507. if (crypto_pk_public_checksig(pkey, tok.val.signature, 128, signed_digest)
  508. != 20) {
  509. log_fn(LOG_WARN, "Error reading directory: invalid signature.");
  510. goto err;
  511. }
  512. log(LOG_DEBUG,"Signed directory hash starts %02x:%02x:%02x:%02x",
  513. ((int)signed_digest[0])&0xff,((int)signed_digest[1])&0xff,
  514. ((int)signed_digest[2])&0xff,((int)signed_digest[3])&0xff);
  515. if (memcmp(digest, signed_digest, 20)) {
  516. log_fn(LOG_WARN, "Error reading directory: signature does not match.");
  517. goto err;
  518. }
  519. }
  520. NEXT_TOK();
  521. TOK_IS(_EOF, "end of directory");
  522. if (*dest)
  523. routerlist_free(*dest);
  524. *dest = new_dir;
  525. return 0;
  526. err:
  527. router_release_token(&tok);
  528. if (new_dir)
  529. routerlist_free(new_dir);
  530. tor_free(versions);
  531. for (i = 0; i < n_good_nicknames; ++i) {
  532. tor_free(good_nickname_lst[i]);
  533. }
  534. return -1;
  535. #undef NEXT_TOK
  536. #undef TOK_IS
  537. #undef ARGS
  538. #undef N_ARGS
  539. }
  540. /* Helper function: Given a string *s containing a concatenated
  541. * sequence of router descriptors, parses them and stores the result
  542. * in *dest. If good_nickname_lst is provided, then routers whose
  543. * nicknames are not listed are marked as nonrunning. Advances *s to
  544. * a point immediately following the last router entry. Returns 0 on
  545. * success and -1 on failure.
  546. */
  547. static int
  548. router_get_list_from_string_impl(const char **s, routerlist_t **dest,
  549. int n_good_nicknames,
  550. const char **good_nickname_lst)
  551. {
  552. routerinfo_t *router;
  553. routerinfo_t **rarray;
  554. int rarray_len = 0;
  555. int i;
  556. assert(s && *s);
  557. rarray = (routerinfo_t **)
  558. tor_malloc((sizeof(routerinfo_t *))*MAX_ROUTERS_IN_DIR);
  559. while (1) {
  560. *s = eat_whitespace(*s);
  561. /* Don't start parsing the rest of *s unless it contains a router. */
  562. if (strncmp(*s, "router ", 7)!=0)
  563. break;
  564. router = router_get_entry_from_string(s);
  565. if (!router) {
  566. log_fn(LOG_WARN, "Error reading router");
  567. for(i=0;i<rarray_len;i++)
  568. routerinfo_free(rarray[i]);
  569. free(rarray);
  570. return -1;
  571. }
  572. if (rarray_len >= MAX_ROUTERS_IN_DIR) {
  573. log_fn(LOG_WARN, "too many routers");
  574. routerinfo_free(router);
  575. continue;
  576. }
  577. if (n_good_nicknames>=0) {
  578. router->is_running = 0;
  579. for (i = 0; i < n_good_nicknames; ++i) {
  580. if (0==strcasecmp(good_nickname_lst[i], router->nickname)) {
  581. router->is_running = 1;
  582. break;
  583. }
  584. }
  585. } else {
  586. router->is_running = 1; /* start out assuming all dirservers are up */
  587. }
  588. rarray[rarray_len++] = router;
  589. log_fn(LOG_DEBUG,"just added router #%d.",rarray_len);
  590. }
  591. if (*dest)
  592. routerlist_free(*dest);
  593. *dest = (routerlist_t *)tor_malloc(sizeof(routerlist_t));
  594. (*dest)->routers = rarray;
  595. (*dest)->n_routers = rarray_len;
  596. (*dest)->software_versions = NULL;
  597. return 0;
  598. }
  599. /* Helper function: reads a single router entry from *s, and advances
  600. * *s so it points to just after the router it just read.
  601. * mallocs a new router and returns it if all goes well, else returns
  602. * NULL.
  603. */
  604. routerinfo_t *router_get_entry_from_string(const char**s) {
  605. routerinfo_t *router = NULL;
  606. char signed_digest[128];
  607. char digest[128];
  608. directory_token_t _tok;
  609. directory_token_t *tok = &_tok;
  610. struct tm published;
  611. int t;
  612. /* Helper macro: read the next token from *s, advance *s, and bail
  613. if there's an error */
  614. #define NEXT_TOKEN() \
  615. do { if (router_get_next_token(s, tok)) { \
  616. log_fn(LOG_WARN, "Error reading directory: %s", tok->val.error);\
  617. goto err; \
  618. } } while(0)
  619. #define ARGS tok->val.cmd.args
  620. #define N_ARGS tok->val.cmd.n_args
  621. _tok.tp = _NIL;
  622. if (router_get_router_hash(*s, digest) < 0) {
  623. log_fn(LOG_WARN, "Couldn't compute router hash.");
  624. return NULL;
  625. }
  626. NEXT_TOKEN(); /* XXX This leaks some arguments. */
  627. if (tok->tp != K_ROUTER) {
  628. log_fn(LOG_WARN,"Entry does not start with \"router\"");
  629. goto err;
  630. }
  631. router = tor_malloc_zero(sizeof(routerinfo_t));
  632. router->onion_pkey = router->identity_pkey = router->link_pkey = NULL;
  633. /* XXXBC move to <7 once we require bandwidthburst */
  634. if (N_ARGS < 6) {
  635. log_fn(LOG_WARN,"Wrong # of arguments to \"router\"");
  636. goto err;
  637. }
  638. router->nickname = tor_strdup(ARGS[0]);
  639. if (strlen(router->nickname) > MAX_NICKNAME_LEN) {
  640. log_fn(LOG_WARN,"Router nickname too long.");
  641. goto err;
  642. }
  643. if (strspn(router->nickname, LEGAL_NICKNAME_CHARACTERS) !=
  644. strlen(router->nickname)) {
  645. log_fn(LOG_WARN, "Router nickname contains illegal characters.");
  646. goto err;
  647. }
  648. /* read router.address */
  649. router->address = tor_strdup(ARGS[1]);
  650. router->addr = 0;
  651. /* Read router->or_port */
  652. router->or_port = atoi(ARGS[2]);
  653. if(!router->or_port) {
  654. log_fn(LOG_WARN,"or_port unreadable or 0. Failing.");
  655. goto err;
  656. }
  657. /* Router->socks_port */
  658. router->socks_port = atoi(ARGS[3]);
  659. /* Router->dir_port */
  660. router->dir_port = atoi(ARGS[4]);
  661. /* Router->bandwidth */
  662. router->bandwidthrate = atoi(ARGS[5]);
  663. if (!router->bandwidthrate) {
  664. log_fn(LOG_WARN,"bandwidthrate unreadable or 0. Failing.");
  665. goto err;
  666. }
  667. #if XXXBC
  668. router->bandwidthburst = atoi(ARGS[6]);
  669. if (!router->bandwidthburst) {
  670. log_fn(LOG_WARN,"bandwidthburst unreadable or 0. Failing.");
  671. goto err;
  672. }
  673. #else
  674. router->bandwidthburst = 10*router->bandwidthrate;
  675. #endif
  676. log_fn(LOG_DEBUG,"or_port %d, socks_port %d, dir_port %d, bandwidthrate %u, bandwidthburst %u.",
  677. router->or_port, router->socks_port, router->dir_port,
  678. (unsigned) router->bandwidthrate, (unsigned) router->bandwidthburst);
  679. /* XXX Later, require platform before published. */
  680. NEXT_TOKEN();
  681. if (tok->tp == K_PLATFORM) {
  682. NEXT_TOKEN();
  683. }
  684. if (tok->tp != K_PUBLISHED) {
  685. log_fn(LOG_WARN, "Missing published time"); goto err;
  686. }
  687. assert(N_ARGS == 1);
  688. if (!strptime(ARGS[0], "%Y-%m-%d %H:%M:%S", &published)) {
  689. log_fn(LOG_WARN, "Published time was unparseable"); goto err;
  690. }
  691. router->published_on = tor_timegm(&published);
  692. NEXT_TOKEN();
  693. if (tok->tp != K_ONION_KEY) {
  694. log_fn(LOG_WARN, "Missing onion-key"); goto err;
  695. }
  696. NEXT_TOKEN();
  697. if (tok->tp != _PUBLIC_KEY) {
  698. log_fn(LOG_WARN, "Missing onion key"); goto err;
  699. } /* XXX Check key length */
  700. router->onion_pkey = tok->val.public_key;
  701. tok->val.public_key = NULL; /* Prevent free */
  702. NEXT_TOKEN();
  703. if (tok->tp != K_LINK_KEY) {
  704. log_fn(LOG_WARN, "Missing link-key"); goto err;
  705. }
  706. NEXT_TOKEN();
  707. if (tok->tp != _PUBLIC_KEY) {
  708. log_fn(LOG_WARN, "Missing link key"); goto err;
  709. } /* XXX Check key length */
  710. router->link_pkey = tok->val.public_key;
  711. tok->val.public_key = NULL; /* Prevent free */
  712. NEXT_TOKEN();
  713. if (tok->tp != K_SIGNING_KEY) {
  714. log_fn(LOG_WARN, "Missing signing-key"); goto err;
  715. }
  716. NEXT_TOKEN();
  717. if (tok->tp != _PUBLIC_KEY) {
  718. log_fn(LOG_WARN, "Missing signing key"); goto err;
  719. }
  720. router->identity_pkey = tok->val.public_key;
  721. tok->val.public_key = NULL; /* Prevent free */
  722. NEXT_TOKEN();
  723. while (tok->tp == K_ACCEPT || tok->tp == K_REJECT) {
  724. router_add_exit_policy(router, tok);
  725. NEXT_TOKEN(); /* This also leaks some args. XXX */
  726. }
  727. if (tok->tp != K_ROUTER_SIGNATURE) {
  728. log_fn(LOG_WARN,"Missing router signature");
  729. goto err;
  730. }
  731. NEXT_TOKEN();
  732. if (tok->tp != _SIGNATURE) {
  733. log_fn(LOG_WARN,"Missing router signature");
  734. goto err;
  735. }
  736. assert (router->identity_pkey);
  737. if ((t=crypto_pk_public_checksig(router->identity_pkey, tok->val.signature,
  738. 128, signed_digest)) != 20) {
  739. log_fn(LOG_WARN, "Invalid signature %d",t);
  740. goto err;
  741. }
  742. if (memcmp(digest, signed_digest, 20)) {
  743. log_fn(LOG_WARN, "Mismatched signature");
  744. goto err;
  745. }
  746. router_release_token(tok); /* free the signature */
  747. return router;
  748. err:
  749. router_release_token(tok);
  750. routerinfo_free(router);
  751. return NULL;
  752. #undef ARGS
  753. #undef N_ARGS
  754. #undef NEXT_TOKEN
  755. }
  756. /* Parse the exit policy in the string 's' and add it to 'router'.
  757. */
  758. int
  759. router_add_exit_policy_from_string(routerinfo_t *router, const char *s)
  760. {
  761. directory_token_t tok;
  762. const char *cp;
  763. char *tmp;
  764. int r;
  765. int len, idx;
  766. tok.tp = _NIL;
  767. /* *s might not end with \n, so we need to extend it with one. */
  768. len = strlen(s);
  769. cp = tmp = tor_malloc(len+2);
  770. for (idx = 0; idx < len; ++idx) {
  771. tmp[idx] = tolower(s[idx]);
  772. }
  773. tmp[len]='\n';
  774. tmp[len+1]='\0';
  775. if (router_get_next_token(&cp, &tok)) {
  776. log_fn(LOG_WARN, "Error reading exit policy: %s", tok.val.error);
  777. goto err;
  778. }
  779. if (tok.tp != K_ACCEPT && tok.tp != K_REJECT) {
  780. log_fn(LOG_WARN, "Expected 'accept' or 'reject'.");
  781. goto err;
  782. }
  783. /* Now that we've gotten an exit policy, add it to the router. */
  784. r = router_add_exit_policy(router, &tok);
  785. goto done;
  786. err:
  787. r = -1;
  788. done:
  789. free(tmp);
  790. router_release_token(&tok);
  791. return r;
  792. }
  793. /* Given a K_ACCEPT or K_REJECT token and a router, create a new exit_policy_t
  794. * corresponding to the token, and add it to 'router' */
  795. static int router_add_exit_policy(routerinfo_t *router,
  796. directory_token_t *tok) {
  797. struct exit_policy_t *tmpe, *newe;
  798. struct in_addr in;
  799. char *arg, *address, *mask, *port, *endptr;
  800. int bits;
  801. assert(tok->tp == K_REJECT || tok->tp == K_ACCEPT);
  802. if (tok->val.cmd.n_args != 1)
  803. return -1;
  804. arg = tok->val.cmd.args[0];
  805. newe = tor_malloc_zero(sizeof(struct exit_policy_t));
  806. newe->string = tor_malloc(8+strlen(arg));
  807. if (tok->tp == K_REJECT) {
  808. strcpy(newe->string, "reject ");
  809. newe->policy_type = EXIT_POLICY_REJECT;
  810. } else {
  811. strcpy(newe->string, "accept ");
  812. newe->policy_type = EXIT_POLICY_ACCEPT;
  813. }
  814. strcat(newe->string, arg);
  815. address = arg;
  816. mask = strchr(arg,'/');
  817. port = strchr(mask?mask:arg,':');
  818. /* Break 'arg' into separate strings. 'arg' was already strdup'd by
  819. * _router_get_next_token, so it's safe to modify.
  820. */
  821. if (mask)
  822. *mask++ = 0;
  823. if (port)
  824. *port++ = 0;
  825. if (strcmp(address, "*") == 0) {
  826. newe->addr = 0;
  827. } else if (inet_aton(address, &in) != 0) {
  828. newe->addr = ntohl(in.s_addr);
  829. } else {
  830. log_fn(LOG_WARN, "Malformed IP %s in exit policy; rejecting.",
  831. address);
  832. goto policy_read_failed;
  833. }
  834. if (!mask) {
  835. if (strcmp(address, "*") == 0)
  836. newe->msk = 0;
  837. else
  838. newe->msk = 0xFFFFFFFFu;
  839. } else {
  840. endptr = NULL;
  841. bits = (int) strtol(mask, &endptr, 10);
  842. if (!*endptr) {
  843. /* strtol handled the whole mask. */
  844. newe->msk = ~((1<<(32-bits))-1);
  845. } else if (inet_aton(mask, &in) != 0) {
  846. newe->msk = ntohl(in.s_addr);
  847. } else {
  848. log_fn(LOG_WARN, "Malformed mask %s on exit policy; rejecting.",
  849. mask);
  850. goto policy_read_failed;
  851. }
  852. }
  853. if (!port || strcmp(port, "*") == 0) {
  854. newe->prt_min = 0;
  855. newe->prt_max = 65535;
  856. } else {
  857. endptr = NULL;
  858. newe->prt_min = strtol(port, &endptr, 10);
  859. if (*endptr == '-') {
  860. port = endptr+1;
  861. endptr = NULL;
  862. newe->prt_max = strtol(port, &endptr, 10);
  863. if (*endptr) {
  864. log_fn(LOG_WARN, "Malformed port %s on exit policy; rejecting.",
  865. port);
  866. }
  867. } else if (*endptr) {
  868. log_fn(LOG_WARN, "Malformed port %s on exit policy; rejecting.",
  869. port);
  870. goto policy_read_failed;
  871. } else {
  872. newe->prt_max = newe->prt_min;
  873. }
  874. }
  875. in.s_addr = htonl(newe->addr);
  876. address = tor_strdup(inet_ntoa(in));
  877. in.s_addr = htonl(newe->msk);
  878. log_fn(LOG_DEBUG,"%s %s/%s:%d-%d",
  879. newe->policy_type == EXIT_POLICY_REJECT ? "reject" : "accept",
  880. address, inet_ntoa(in), newe->prt_min, newe->prt_max);
  881. tor_free(address);
  882. /* now link newe onto the end of exit_policy */
  883. if(!router->exit_policy) {
  884. router->exit_policy = newe;
  885. return 0;
  886. }
  887. for(tmpe=router->exit_policy; tmpe->next; tmpe=tmpe->next) ;
  888. tmpe->next = newe;
  889. return 0;
  890. policy_read_failed:
  891. assert(newe->string);
  892. log_fn(LOG_WARN,"Couldn't parse line '%s'. Dropping", newe->string);
  893. tor_free(newe->string);
  894. free(newe);
  895. return -1;
  896. }
  897. /* ------------------------------------------------------------ */
  898. /* Tokenizer for router descriptors and directories. */
  899. /* Every keyword takes either... */
  900. typedef enum {
  901. NO_ARGS, /* (1) no arguments, ever */
  902. ARGS, /* (2) a list of arguments separated by spaces */
  903. CONCAT_ARGS, /* or (3) the rest of the line, treated as a single argument. */
  904. } arg_syntax;
  905. /* Table mapping keywods to token value and to argument rules. */
  906. static struct { char *t; int v; arg_syntax s; } token_table[] = {
  907. { "accept", K_ACCEPT, ARGS },
  908. { "directory-signature", K_DIRECTORY_SIGNATURE, NO_ARGS },
  909. { "reject", K_REJECT, ARGS },
  910. { "router", K_ROUTER, ARGS },
  911. { "recommended-software", K_RECOMMENDED_SOFTWARE, ARGS },
  912. { "signed-directory", K_SIGNED_DIRECTORY, NO_ARGS },
  913. { "signing-key", K_SIGNING_KEY, NO_ARGS },
  914. { "onion-key", K_ONION_KEY, NO_ARGS },
  915. { "link-key", K_LINK_KEY, NO_ARGS },
  916. { "router-signature", K_ROUTER_SIGNATURE, NO_ARGS },
  917. { "published", K_PUBLISHED, CONCAT_ARGS },
  918. { "running-routers", K_RUNNING_ROUTERS, ARGS },
  919. { "platform", K_PLATFORM, ARGS },
  920. { NULL, -1 }
  921. };
  922. /* Free any malloced resources allocated for a token. Does not free
  923. * the token itself.
  924. */
  925. static void
  926. router_release_token(directory_token_t *tok)
  927. {
  928. int i;
  929. switch (tok->tp)
  930. {
  931. case _SIGNATURE:
  932. if (tok->val.signature)
  933. free(tok->val.signature);
  934. break;
  935. case _PUBLIC_KEY:
  936. if (tok->val.public_key)
  937. crypto_free_pk_env(tok->val.public_key);
  938. break;
  939. case _ERR:
  940. case _EOF:
  941. case _NIL:
  942. break;
  943. default:
  944. for (i = 0; i < tok->val.cmd.n_args; ++i) {
  945. tor_free(tok->val.cmd.args[i]);
  946. }
  947. }
  948. tok->tp = _NIL;
  949. }
  950. /* Helper function: read the next token from *s, and stores it into *tok.
  951. * If *tok already contains a token (tok->tp != _NIL), free the resources
  952. * held by *tok. Advance *s to a point immediately after the token.
  953. *
  954. *
  955. */
  956. static int
  957. _router_get_next_token(const char **s, directory_token_t *tok) {
  958. const char *next;
  959. crypto_pk_env_t *pkey = NULL;
  960. char *signature = NULL;
  961. int i, done;
  962. /* Clear the token _first_, so that we can clear it safely. */
  963. router_release_token(tok);
  964. tok->tp = _ERR;
  965. tok->val.error = "";
  966. *s = eat_whitespace(*s);
  967. if (!**s) {
  968. tok->tp = _EOF;
  969. return 0;
  970. } else if (**s == '-') {
  971. next = strchr(*s, '\n');
  972. if (! next) { tok->val.error = "No newline at EOF"; return -1; }
  973. ++next;
  974. if (! strncmp(*s, OR_PUBLICKEY_BEGIN_TAG, next-*s)) {
  975. /* We have a ----BEGIN PUBLIC KEY----- */
  976. next = strstr(*s, OR_PUBLICKEY_END_TAG);
  977. if (!next) { tok->val.error = "No public key end tag found"; return -1; }
  978. next = strchr(next, '\n'); /* Part of OR_PUBLICKEY_END_TAG; can't fail.*/
  979. ++next;
  980. if (!(pkey = crypto_new_pk_env(CRYPTO_PK_RSA)))
  981. return -1;
  982. if (crypto_pk_read_public_key_from_string(pkey, *s, next-*s)) {
  983. crypto_free_pk_env(pkey);
  984. tok->val.error = "Couldn't parse public key.";
  985. return -1;
  986. }
  987. tok->tp = _PUBLIC_KEY;
  988. tok->val.public_key = pkey;
  989. *s = next;
  990. return 0;
  991. } else if (! strncmp(*s, OR_SIGNATURE_BEGIN_TAG, next-*s)) {
  992. /* We have a -----BEGIN SIGNATURE----- */
  993. /* Advance past newline; can't fail. */
  994. *s = strchr(*s, '\n');
  995. ++*s;
  996. /* Find end of base64'd data */
  997. next = strstr(*s, OR_SIGNATURE_END_TAG);
  998. if (!next) { tok->val.error = "No signature end tag found"; return -1; }
  999. signature = tor_malloc(256);
  1000. i = base64_decode(signature, 256, *s, next-*s);
  1001. if (i<0) {
  1002. free(signature);
  1003. tok->val.error = "Error decoding signature."; return -1;
  1004. } else if (i != 128) {
  1005. free(signature);
  1006. tok->val.error = "Bad length on decoded signature."; return -1;
  1007. }
  1008. tok->tp = _SIGNATURE;
  1009. tok->val.signature = signature;
  1010. next = strchr(next, '\n'); /* Part of OR_SIGNATURE_END_TAG; can't fail.*/
  1011. *s = next+1;
  1012. return 0;
  1013. } else {
  1014. tok->val.error = "Unrecognized begin line"; return -1;
  1015. }
  1016. } else {
  1017. next = find_whitespace(*s);
  1018. if (!next) {
  1019. tok->val.error = "Unexpected EOF"; return -1;
  1020. }
  1021. /* It's a keyword... but which one? */
  1022. for (i = 0 ; token_table[i].t ; ++i) {
  1023. if (!strncmp(token_table[i].t, *s, next-*s)) {
  1024. /* We've found the keyword. */
  1025. tok->tp = token_table[i].v;
  1026. if (token_table[i].s == ARGS) {
  1027. /* This keyword takes multiple arguments. */
  1028. i = 0;
  1029. done = (*next == '\n');
  1030. *s = eat_whitespace_no_nl(next);
  1031. while (**s != '\n' && i < MAX_ARGS && !done) {
  1032. next = find_whitespace(*s);
  1033. if (*next == '\n')
  1034. done = 1;
  1035. tok->val.cmd.args[i++] = tor_strndup(*s,next-*s);
  1036. /* XXX this line (the strndup) is the memory leak. */
  1037. *s = eat_whitespace_no_nl(next+1);
  1038. }
  1039. tok->val.cmd.n_args = i;
  1040. if (i >= MAX_ARGS) {
  1041. router_release_token(tok);
  1042. tok->tp = _ERR;
  1043. tok->val.error = "Too many arguments"; return -1;
  1044. }
  1045. } else if (token_table[i].s == CONCAT_ARGS) {
  1046. /* The keyword takes the line as a single argument */
  1047. *s = eat_whitespace_no_nl(next);
  1048. next = strchr(*s, '\n');
  1049. if (!next) {
  1050. tok->tp = _ERR;
  1051. tok->val.error = "Unexpected EOF"; return -1;
  1052. }
  1053. tok->val.cmd.args[0] = tor_strndup(*s,next-*s);
  1054. tok->val.cmd.n_args = 1;
  1055. *s = eat_whitespace_no_nl(next+1);
  1056. } else {
  1057. /* The keyword takes no arguments. */
  1058. *s = eat_whitespace_no_nl(next);
  1059. if (**s != '\n') {
  1060. tok->tp = _ERR;
  1061. tok->val.error = "Unexpected arguments"; return -1;
  1062. }
  1063. tok->val.cmd.n_args = 0;
  1064. *s = eat_whitespace_no_nl(*s+1);
  1065. }
  1066. return 0;
  1067. }
  1068. }
  1069. tok->val.error = "Unrecognized command"; return -1;
  1070. }
  1071. }
  1072. #ifdef DEBUG_ROUTER_TOKENS
  1073. static void
  1074. router_dump_token(directory_token_t *tok) {
  1075. int i;
  1076. switch(tok->tp)
  1077. {
  1078. case _SIGNATURE:
  1079. puts("(signature)");
  1080. return;
  1081. case _PUBLIC_KEY:
  1082. puts("(public key)");
  1083. return;
  1084. case _ERR:
  1085. printf("(Error: %s\n)", tok->val.error);
  1086. return;
  1087. case _EOF:
  1088. puts("EOF");
  1089. return;
  1090. case K_ACCEPT: printf("Accept"); break;
  1091. case K_DIRECTORY_SIGNATURE: printf("Directory-Signature"); break;
  1092. case K_REJECT: printf("Reject"); break;
  1093. case K_RECOMMENDED_SOFTWARE: printf("Server-Software"); break;
  1094. case K_ROUTER: printf("Router"); break;
  1095. case K_SIGNED_DIRECTORY: printf("Signed-Directory"); break;
  1096. case K_SIGNING_KEY: printf("Signing-Key"); break;
  1097. case K_ONION_KEY: printf("Onion-key"); break;
  1098. case K_LINK_KEY: printf("Link-key"); break;
  1099. case K_ROUTER_SIGNATURE: printf("Router-signature"); break;
  1100. case K_PUBLISHED: printf("Published"); break;
  1101. case K_RUNNING_ROUTERS: printf("Running-routers"); break;
  1102. case K_PLATFORM: printf("Platform"); break;
  1103. default:
  1104. printf("?????? %d\n", tok->tp); return;
  1105. }
  1106. for (i = 0; i < tok->val.cmd.n_args; ++i) {
  1107. printf(" \"%s\"", tok->val.cmd.args[i]);
  1108. }
  1109. printf("\n");
  1110. return;
  1111. }
  1112. static int
  1113. router_get_next_token(const char **s, directory_token_t *tok) {
  1114. int i;
  1115. i = _router_get_next_token(s, tok);
  1116. router_dump_token(tok);
  1117. return i;
  1118. }
  1119. #else
  1120. #define router_get_next_token _router_get_next_token
  1121. #endif
  1122. /* Compute the SHA digest of the substring of s taken from the first
  1123. * occurrence of start_str through the first newline after the first
  1124. * subsequent occurrence of end_str; store the 20-byte result in 'digest';
  1125. * return 0 on success.
  1126. *
  1127. * If no such substring exists, return -1.
  1128. */
  1129. static int router_get_hash_impl(const char *s, char *digest,
  1130. const char *start_str,
  1131. const char *end_str)
  1132. {
  1133. char *start, *end;
  1134. start = strstr(s, start_str);
  1135. if (!start) {
  1136. log_fn(LOG_WARN,"couldn't find \"%s\"",start_str);
  1137. return -1;
  1138. }
  1139. end = strstr(start+strlen(start_str), end_str);
  1140. if (!end) {
  1141. log_fn(LOG_WARN,"couldn't find \"%s\"",end_str);
  1142. return -1;
  1143. }
  1144. end = strchr(end, '\n');
  1145. if (!end) {
  1146. log_fn(LOG_WARN,"couldn't find EOL");
  1147. return -1;
  1148. }
  1149. ++end;
  1150. if (crypto_SHA_digest(start, end-start, digest)) {
  1151. log_fn(LOG_WARN,"couldn't compute digest");
  1152. return -1;
  1153. }
  1154. return 0;
  1155. }
  1156. /*
  1157. Local Variables:
  1158. mode:c
  1159. indent-tabs-mode:nil
  1160. c-basic-offset:2
  1161. End:
  1162. */