dirserv.c 14 KB

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