dirserv.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include "or.h"
  5. /* How old do we allow a router to get before removing it? (seconds) */
  6. #define ROUTER_MAX_AGE (60*60*24)
  7. /* How far in the future do we allow a router to get? (seconds) */
  8. #define ROUTER_ALLOW_SKEW (30*60)
  9. extern or_options_t options; /* command-line and config-file options */
  10. static int the_directory_is_dirty = 1;
  11. static int list_running_servers(char **nicknames_out);
  12. static void directory_remove_unrecognized(void);
  13. /************** Fingerprint handling code ************/
  14. typedef struct fingerprint_entry_t {
  15. char *nickname;
  16. char *fingerprint;
  17. } fingerprint_entry_t;
  18. static fingerprint_entry_t fingerprint_list[MAX_ROUTERS_IN_DIR];
  19. static int n_fingerprints = 0;
  20. void /* Should be static; exposed for testing */
  21. add_fingerprint_to_dir(const char *nickname, const char *fp)
  22. {
  23. int i;
  24. for (i = 0; i < n_fingerprints; ++i) {
  25. if (!strcasecmp(fingerprint_list[i].nickname,nickname)) {
  26. free(fingerprint_list[i].fingerprint);
  27. fingerprint_list[i].fingerprint = tor_strdup(fp);
  28. return;
  29. }
  30. }
  31. fingerprint_list[n_fingerprints].nickname = tor_strdup(nickname);
  32. fingerprint_list[n_fingerprints].fingerprint = tor_strdup(fp);
  33. ++n_fingerprints;
  34. }
  35. int
  36. dirserv_add_own_fingerprint(const char *nickname, crypto_pk_env_t *pk)
  37. {
  38. char fp[FINGERPRINT_LEN+1];
  39. if (crypto_pk_get_fingerprint(pk, fp)<0) {
  40. log_fn(LOG_ERR, "Error computing fingerprint");
  41. return -1;
  42. }
  43. add_fingerprint_to_dir(nickname, fp);
  44. return 0;
  45. }
  46. /* return 0 on success, -1 on failure */
  47. int
  48. dirserv_parse_fingerprint_file(const char *fname)
  49. {
  50. FILE *file;
  51. char line[FINGERPRINT_LEN+MAX_NICKNAME_LEN+20+1];
  52. char *nickname, *fingerprint;
  53. fingerprint_entry_t fingerprint_list_tmp[MAX_ROUTERS_IN_DIR];
  54. int n_fingerprints_tmp = 0;
  55. int i, result;
  56. if(!(file = fopen(fname, "r"))) {
  57. log_fn(LOG_WARN, "Cannot open fingerprint file %s", fname);
  58. return -1;
  59. }
  60. while( (result=parse_line_from_file(line, sizeof(line),file,&nickname,&fingerprint)) > 0) {
  61. if (strlen(nickname) > MAX_NICKNAME_LEN) {
  62. log(LOG_WARN, "Nickname %s too long in fingerprint file. Skipping.", nickname);
  63. continue;
  64. }
  65. if(strlen(fingerprint) != FINGERPRINT_LEN ||
  66. !crypto_pk_check_fingerprint_syntax(fingerprint)) {
  67. log_fn(LOG_WARN, "Invalid fingerprint (nickname %s, fingerprint %s). Skipping.",
  68. nickname, fingerprint);
  69. continue;
  70. }
  71. for (i = 0; i < n_fingerprints_tmp; ++i) {
  72. if (0==strcasecmp(fingerprint_list_tmp[i].nickname, nickname)) {
  73. log(LOG_WARN, "Duplicate nickname %s. Skipping.",nickname);
  74. break; /* out of the for. the 'if' below means skip to the next line. */
  75. }
  76. }
  77. if(i == n_fingerprints_tmp) { /* not a duplicate */
  78. fingerprint_list_tmp[n_fingerprints_tmp].nickname = tor_strdup(nickname);
  79. fingerprint_list_tmp[n_fingerprints_tmp].fingerprint = tor_strdup(fingerprint);
  80. ++n_fingerprints_tmp;
  81. }
  82. }
  83. fclose(file);
  84. if(result == 0) { /* eof; replace the global fingerprints list. */
  85. dirserv_free_fingerprint_list();
  86. memcpy(fingerprint_list, fingerprint_list_tmp,
  87. sizeof(fingerprint_entry_t)*n_fingerprints_tmp);
  88. n_fingerprints = n_fingerprints_tmp;
  89. /* Delete any routers whose fingerprints we no longer recognize */
  90. directory_remove_unrecognized();
  91. return 0;
  92. }
  93. /* error */
  94. log_fn(LOG_WARN, "Error reading from fingerprint file");
  95. for (i = 0; i < n_fingerprints_tmp; ++i) {
  96. free(fingerprint_list_tmp[i].nickname);
  97. free(fingerprint_list_tmp[i].fingerprint);
  98. }
  99. return -1;
  100. }
  101. /* return 1 if router's identity and nickname match,
  102. * -1 if they don't match, 0 if the nickname is not known. */
  103. int
  104. dirserv_router_fingerprint_is_known(const routerinfo_t *router)
  105. {
  106. int i;
  107. fingerprint_entry_t *ent =NULL;
  108. char fp[FINGERPRINT_LEN+1];
  109. log_fn(LOG_DEBUG, "%d fingerprints known.", n_fingerprints);
  110. for (i=0;i<n_fingerprints;++i) {
  111. log_fn(LOG_DEBUG,"%s vs %s", router->nickname, fingerprint_list[i].nickname);
  112. if (!strcasecmp(router->nickname,fingerprint_list[i].nickname)) {
  113. ent = &fingerprint_list[i];
  114. break;
  115. }
  116. }
  117. if (!ent) { /* No such server known */
  118. log_fn(LOG_INFO,"no fingerprint found for %s",router->nickname);
  119. return 0;
  120. }
  121. if (crypto_pk_get_fingerprint(router->identity_pkey, fp)) {
  122. log_fn(LOG_WARN,"error computing fingerprint");
  123. return -1;
  124. }
  125. if (0==strcasecmp(ent->fingerprint, fp)) {
  126. log_fn(LOG_DEBUG,"good fingerprint for %s",router->nickname);
  127. return 1; /* Right fingerprint. */
  128. } else {
  129. log_fn(LOG_WARN,"mismatched fingerprint for %s",router->nickname);
  130. return -1; /* Wrong fingerprint. */
  131. }
  132. }
  133. void
  134. dirserv_free_fingerprint_list()
  135. {
  136. int i;
  137. for (i = 0; i < n_fingerprints; ++i) {
  138. free(fingerprint_list[i].nickname);
  139. free(fingerprint_list[i].fingerprint);
  140. }
  141. n_fingerprints = 0;
  142. }
  143. /*
  144. * Descriptor list
  145. */
  146. typedef struct descriptor_entry_t {
  147. char *nickname;
  148. time_t published;
  149. size_t desc_len;
  150. char *descriptor;
  151. routerinfo_t *router;
  152. } descriptor_entry_t;
  153. static descriptor_entry_t *descriptor_list[MAX_ROUTERS_IN_DIR];
  154. static int n_descriptors = 0;
  155. static void free_descriptor_entry(descriptor_entry_t *desc)
  156. {
  157. tor_free(desc->descriptor);
  158. tor_free(desc->nickname);
  159. routerinfo_free(desc->router);
  160. free(desc);
  161. }
  162. void
  163. dirserv_free_descriptors()
  164. {
  165. int i;
  166. for (i = 0; i < n_descriptors; ++i) {
  167. free_descriptor_entry(descriptor_list[i]);
  168. }
  169. n_descriptors = 0;
  170. }
  171. /* Return 1 if descriptor is well-formed and accepted;
  172. * 0 if well-formed and server or descriptor is unapproved;
  173. * -1 if not well-formed or other error.
  174. *
  175. * Update *desc to point after the descriptor if the
  176. * descriptor is well-formed.
  177. */
  178. int
  179. dirserv_add_descriptor(const char **desc)
  180. {
  181. descriptor_entry_t **desc_ent_ptr;
  182. routerinfo_t *ri = NULL;
  183. int i, r;
  184. char *start, *end;
  185. char *desc_tmp = NULL;
  186. const char *cp;
  187. size_t desc_len;
  188. time_t now;
  189. start = strstr(*desc, "router ");
  190. if (!start) {
  191. log_fn(LOG_WARN, "no 'router' line found. This is not a descriptor.");
  192. return -1;
  193. }
  194. if ((end = strstr(start+6, "\nrouter "))) {
  195. ++end; /* Include NL. */
  196. } else if ((end = strstr(start+6, "\ndirectory-signature"))) {
  197. ++end;
  198. } else {
  199. end = start+strlen(start);
  200. }
  201. desc_len = end-start;
  202. cp = desc_tmp = tor_strndup(start, desc_len);
  203. /* Check: is the descriptor syntactically valid? */
  204. ri = router_get_entry_from_string(cp, NULL);
  205. tor_free(desc_tmp);
  206. if (!ri) {
  207. log(LOG_WARN, "Couldn't parse descriptor");
  208. return -1;
  209. }
  210. /* Okay. Now check whether the fingerprint is recognized. */
  211. r = dirserv_router_fingerprint_is_known(ri);
  212. if(r<1) {
  213. if(r==0) {
  214. char fp[FINGERPRINT_LEN+1];
  215. log_fn(LOG_WARN, "Unknown nickname %s (%s:%d). Not adding.",
  216. ri->nickname, ri->address, ri->or_port);
  217. if (crypto_pk_get_fingerprint(ri->identity_pkey, fp) < 0) {
  218. log_fn(LOG_WARN, "Error computing fingerprint for %s", ri->nickname);
  219. } else {
  220. log_fn(LOG_WARN, "Fingerprint line: %s %s", ri->nickname, fp);
  221. }
  222. } else {
  223. log_fn(LOG_WARN, "Known nickname %s, wrong fingerprint. Not adding.", ri->nickname);
  224. }
  225. routerinfo_free(ri);
  226. *desc = end;
  227. return 0;
  228. }
  229. /* Is there too much clock skew? */
  230. now = time(NULL);
  231. if (ri->published_on > now+ROUTER_ALLOW_SKEW) {
  232. log_fn(LOG_WARN, "Publication time for nickname %s is too far in the future; possible clock skew. Not adding.", ri->nickname);
  233. routerinfo_free(ri);
  234. *desc = end;
  235. return 0;
  236. }
  237. if (ri->published_on < now-ROUTER_MAX_AGE) {
  238. log_fn(LOG_WARN, "Publication time for router with nickname %s is too far in the past. Not adding.", ri->nickname);
  239. routerinfo_free(ri);
  240. *desc = end;
  241. return 0;
  242. }
  243. /* Do we already have an entry for this router? */
  244. desc_ent_ptr = NULL;
  245. for (i = 0; i < n_descriptors; ++i) {
  246. if (!strcasecmp(ri->nickname, descriptor_list[i]->nickname)) {
  247. desc_ent_ptr = &descriptor_list[i];
  248. break;
  249. }
  250. }
  251. if (desc_ent_ptr) {
  252. /* if so, decide whether to update it. */
  253. if ((*desc_ent_ptr)->published > ri->published_on) {
  254. /* We already have a newer descriptor */
  255. log_fn(LOG_INFO,"We already have a newer desc for nickname %s. Not adding.",ri->nickname);
  256. /* This isn't really an error; return success. */
  257. routerinfo_free(ri);
  258. *desc = end;
  259. return 1;
  260. }
  261. /* We don't have a newer one; we'll update this one. */
  262. log_fn(LOG_INFO,"Dirserv updating desc for nickname %s",ri->nickname);
  263. free_descriptor_entry(*desc_ent_ptr);
  264. } else {
  265. /* Add this at the end. */
  266. log_fn(LOG_INFO,"Dirserv adding desc for nickname %s",ri->nickname);
  267. desc_ent_ptr = &descriptor_list[n_descriptors++];
  268. /* XXX check if n_descriptors is too big */
  269. }
  270. (*desc_ent_ptr) = tor_malloc(sizeof(descriptor_entry_t));
  271. (*desc_ent_ptr)->nickname = tor_strdup(ri->nickname);
  272. (*desc_ent_ptr)->published = ri->published_on;
  273. (*desc_ent_ptr)->desc_len = desc_len;
  274. (*desc_ent_ptr)->descriptor = tor_malloc(desc_len+1);
  275. strncpy((*desc_ent_ptr)->descriptor, start, desc_len);
  276. (*desc_ent_ptr)->descriptor[desc_len] = '\0';
  277. (*desc_ent_ptr)->router = ri;
  278. *desc = end;
  279. directory_set_dirty();
  280. return 1;
  281. }
  282. static void
  283. directory_remove_unrecognized(void)
  284. {
  285. int i;
  286. for (i = 0; i < n_descriptors; ++i) {
  287. if (dirserv_router_fingerprint_is_known(descriptor_list[i]->router)<=0) {
  288. log(LOG_INFO, "Router %s is no longer recognized",
  289. descriptor_list[i]->nickname);
  290. free_descriptor_entry(descriptor_list[i]);
  291. descriptor_list[i--] = descriptor_list[--n_descriptors];
  292. }
  293. }
  294. }
  295. void
  296. directory_set_dirty()
  297. {
  298. the_directory_is_dirty = 1;
  299. }
  300. int
  301. dirserv_init_from_directory_string(const char *dir)
  302. {
  303. const char *cp = dir;
  304. while(1) {
  305. cp = strstr(cp, "\nrouter ");
  306. if (!cp) break;
  307. ++cp;
  308. if (dirserv_add_descriptor(&cp) < 0) {
  309. return -1;
  310. }
  311. --cp; /*Back up to newline.*/
  312. }
  313. return 0;
  314. }
  315. static int
  316. list_running_servers(char **nicknames_out)
  317. {
  318. char *nickname_lst[MAX_ROUTERS_IN_DIR];
  319. connection_t **connection_array;
  320. int n_conns;
  321. connection_t *conn;
  322. char *cp;
  323. int n = 0, i;
  324. int length;
  325. *nicknames_out = NULL;
  326. nickname_lst[n++] = options.Nickname;
  327. get_connection_array(&connection_array, &n_conns);
  328. for (i = 0; i<n_conns; ++i) {
  329. conn = connection_array[i];
  330. if (conn->type != CONN_TYPE_OR || conn->state != OR_CONN_STATE_OPEN)
  331. continue; /* only list successfully handshaked OR's. */
  332. if(!conn->nickname) /* it's an OP, don't list it */
  333. continue;
  334. nickname_lst[n++] = conn->nickname;
  335. }
  336. length = n + 1; /* spaces + EOS + 1. */
  337. for (i = 0; i<n; ++i) {
  338. length += strlen(nickname_lst[i]);
  339. }
  340. *nicknames_out = tor_malloc_zero(length);
  341. cp = *nicknames_out;
  342. for (i = 0; i<n; ++i) {
  343. if (i)
  344. strcat(cp, " ");
  345. strcat(cp, nickname_lst[i]); /* can't overflow */
  346. while (*cp)
  347. ++cp;
  348. }
  349. return 0;
  350. }
  351. /* Remove any descriptors from the directory that are more than ROUTER_MAX_AGE
  352. * seconds old.
  353. */
  354. void
  355. dirserv_remove_old_servers(void)
  356. {
  357. int i;
  358. time_t cutoff;
  359. cutoff = time(NULL) - ROUTER_MAX_AGE;
  360. for (i = 0; i < n_descriptors; ++i) {
  361. if (descriptor_list[i]->published < cutoff) {
  362. /* descriptor_list[i] is too old. Remove it. */
  363. free_descriptor_entry(descriptor_list[i]);
  364. descriptor_list[i] = descriptor_list[n_descriptors-1];
  365. --n_descriptors;
  366. directory_set_dirty();
  367. --i; /* Don't advance the index; consider the new value now at i. */
  368. }
  369. }
  370. }
  371. /* Dump all routers currently in the directory into the string <s>, using
  372. * at most <maxlen> characters, and signing the directory with <private_key>.
  373. * Return 0 on success, -1 on failure.
  374. */
  375. int
  376. dirserv_dump_directory_to_string(char *s, unsigned int maxlen,
  377. crypto_pk_env_t *private_key)
  378. {
  379. char *cp, *eos;
  380. char digest[20];
  381. char signature[128];
  382. char published[33];
  383. time_t published_on;
  384. int i;
  385. eos = s+maxlen;
  386. if (list_running_servers(&cp))
  387. return -1;
  388. dirserv_remove_old_servers();
  389. published_on = time(NULL);
  390. strftime(published, 32, "%Y-%m-%d %H:%M:%S", gmtime(&published_on));
  391. snprintf(s, maxlen,
  392. "signed-directory\n"
  393. "published %s\n"
  394. "recommended-software %s\n"
  395. "running-routers %s\n\n", published, options.RecommendedVersions, cp);
  396. free(cp);
  397. i = strlen(s);
  398. cp = s+i;
  399. for (i = 0; i < n_descriptors; ++i) {
  400. if (strlcat(s, descriptor_list[i]->descriptor, maxlen) >= maxlen)
  401. goto truncated;
  402. }
  403. /* These multiple strlcat calls are inefficient, but dwarfed by the RSA
  404. signature.
  405. */
  406. if (strlcat(s, "directory-signature ", maxlen) >= maxlen)
  407. goto truncated;
  408. if (strlcat(s, options.Nickname, maxlen) >= maxlen)
  409. goto truncated;
  410. if (strlcat(s, "\n", maxlen) >= maxlen)
  411. goto truncated;
  412. if (router_get_dir_hash(s,digest)) {
  413. log_fn(LOG_WARN,"couldn't compute digest");
  414. return -1;
  415. }
  416. if (crypto_pk_private_sign(private_key, digest, 20, signature) < 0) {
  417. log_fn(LOG_WARN,"couldn't sign digest");
  418. return -1;
  419. }
  420. log(LOG_DEBUG,"generated directory digest begins with %s",hex_str(digest,4));
  421. if (strlcat(cp, "-----BEGIN SIGNATURE-----\n", maxlen) >= maxlen)
  422. goto truncated;
  423. i = strlen(s);
  424. cp = s+i;
  425. if (base64_encode(cp, maxlen-i, signature, 128) < 0) {
  426. log_fn(LOG_WARN,"couldn't base64-encode signature");
  427. return -1;
  428. }
  429. if (strlcat(s, "-----END SIGNATURE-----\n", maxlen) >= maxlen)
  430. goto truncated;
  431. return 0;
  432. truncated:
  433. log_fn(LOG_WARN,"tried to exceed string length.");
  434. return -1;
  435. }
  436. static char *the_directory = NULL;
  437. static int the_directory_len = -1;
  438. size_t dirserv_get_directory(const char **directory)
  439. {
  440. char *new_directory;
  441. char filename[512];
  442. if (the_directory_is_dirty) {
  443. new_directory = tor_malloc(MAX_DIR_SIZE);
  444. if (dirserv_dump_directory_to_string(new_directory, MAX_DIR_SIZE,
  445. get_identity_key())) {
  446. log(LOG_WARN, "Error creating directory.");
  447. free(new_directory);
  448. return 0;
  449. }
  450. tor_free(the_directory);
  451. the_directory = new_directory;
  452. the_directory_len = strlen(the_directory);
  453. log_fn(LOG_INFO,"New directory (size %d):\n%s",the_directory_len,
  454. the_directory);
  455. the_directory_is_dirty = 0;
  456. /* Now read the directory we just made in order to update our own
  457. * router lists. This does more signature checking than is strictly
  458. * necessary, but safe is better than sorry. */
  459. new_directory = tor_strdup(the_directory);
  460. /* use a new copy of the dir, since get_dir_from_string scribbles on it */
  461. if (router_set_routerlist_from_directory(new_directory, get_identity_key())) {
  462. log_fn(LOG_ERR, "We just generated a directory we can't parse. Dying.");
  463. exit(0);
  464. }
  465. free(new_directory);
  466. sprintf(filename,"%s/cached-directory", options.DataDirectory);
  467. if(write_str_to_file(filename,the_directory) < 0) {
  468. log_fn(LOG_WARN, "Couldn't write cached directory to disk. Ignoring.");
  469. }
  470. } else {
  471. log(LOG_INFO,"Directory still clean, reusing.");
  472. }
  473. *directory = the_directory;
  474. return the_directory_len;
  475. }
  476. /*
  477. Local Variables:
  478. mode:c
  479. indent-tabs-mode:nil
  480. c-basic-offset:2
  481. End:
  482. */