routerlist.c 36 KB

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