dirserv.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include "or.h"
  5. /* How far in the future do we allow a router to get? (seconds) */
  6. #define ROUTER_ALLOW_SKEW (30*60)
  7. extern or_options_t options; /* command-line and config-file options */
  8. static int the_directory_is_dirty = 1;
  9. static char *the_directory = NULL;
  10. static int the_directory_len = -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. static void
  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. int
  100. dirserv_router_fingerprint_is_known(const routerinfo_t *router)
  101. {
  102. int i;
  103. fingerprint_entry_t *ent =NULL;
  104. char fp[FINGERPRINT_LEN+1];
  105. log_fn(LOG_DEBUG, "%d fingerprints known.", n_fingerprints);
  106. for (i=0;i<n_fingerprints;++i) {
  107. log_fn(LOG_DEBUG,"%s vs %s", router->nickname, fingerprint_list[i].nickname);
  108. if (!strcasecmp(router->nickname,fingerprint_list[i].nickname)) {
  109. ent = &fingerprint_list[i];
  110. break;
  111. }
  112. }
  113. if (!ent) { /* No such server known */
  114. log_fn(LOG_WARN,"no fingerprint found for %s",router->nickname);
  115. return 0;
  116. }
  117. if (crypto_pk_get_fingerprint(router->identity_pkey, fp)) {
  118. log_fn(LOG_WARN,"error computing fingerprint");
  119. return 0;
  120. }
  121. if (0==strcasecmp(ent->fingerprint, fp)) {
  122. log_fn(LOG_DEBUG,"good fingerprint for %s",router->nickname);
  123. return 1; /* Right fingerprint. */
  124. } else {
  125. log_fn(LOG_WARN,"mismatched fingerprint for %s",router->nickname);
  126. return 0; /* Wrong fingerprint. */
  127. }
  128. }
  129. void
  130. dirserv_free_fingerprint_list()
  131. {
  132. int i;
  133. for (i = 0; i < n_fingerprints; ++i) {
  134. free(fingerprint_list[i].nickname);
  135. free(fingerprint_list[i].fingerprint);
  136. }
  137. n_fingerprints = 0;
  138. }
  139. /*
  140. * Descriptor list
  141. */
  142. typedef struct descriptor_entry_t {
  143. char *nickname;
  144. time_t published;
  145. size_t desc_len;
  146. char *descriptor;
  147. } descriptor_entry_t;
  148. static descriptor_entry_t *descriptor_list[MAX_ROUTERS_IN_DIR];
  149. static int n_descriptors = 0;
  150. static void free_descriptor_entry(descriptor_entry_t *desc)
  151. {
  152. tor_free(desc->descriptor);
  153. tor_free(desc->nickname);
  154. free(desc);
  155. }
  156. void
  157. dirserv_free_descriptors()
  158. {
  159. int i;
  160. for (i = 0; i < n_descriptors; ++i) {
  161. free_descriptor_entry(descriptor_list[i]);
  162. }
  163. n_descriptors = 0;
  164. }
  165. /* Return 0 if descriptor added; -1 if descriptor rejected. Updates *desc
  166. * to point after the descriptor if the descriptor is OK.
  167. */
  168. int
  169. dirserv_add_descriptor(const char **desc)
  170. {
  171. descriptor_entry_t **desc_ent_ptr;
  172. routerinfo_t *ri = NULL;
  173. int i;
  174. char *start, *end;
  175. char *desc_tmp = NULL, *cp;
  176. size_t desc_len;
  177. start = strstr(*desc, "router ");
  178. if (!start) {
  179. log(LOG_WARN, "no descriptor found.");
  180. goto err;
  181. }
  182. if ((end = strstr(start+6, "\nrouter "))) {
  183. ++end; /* Include NL. */
  184. } else if ((end = strstr(start+6, "\ndirectory-signature"))) {
  185. ++end;
  186. } else {
  187. end = start+strlen(start);
  188. }
  189. desc_len = end-start;
  190. cp = desc_tmp = tor_malloc(desc_len+1);
  191. strncpy(desc_tmp, start, desc_len);
  192. desc_tmp[desc_len]='\0';
  193. /* Check: is the descriptor syntactically valid? */
  194. ri = router_get_entry_from_string(&cp);
  195. if (!ri) {
  196. log(LOG_WARN, "Couldn't parse descriptor");
  197. goto err;
  198. }
  199. tor_free(desc_tmp);
  200. /* Okay. Now check whether the fingerprint is recognized. */
  201. if (!dirserv_router_fingerprint_is_known(ri)) {
  202. log_fn(LOG_WARN, "Identity is unrecognized for descriptor");
  203. goto err;
  204. }
  205. /* Is there too much clock skew? */
  206. if (ri->published_on > time(NULL)+ROUTER_ALLOW_SKEW) {
  207. log_fn(LOG_WARN, "Publication time for nickname %s is too far in the future; possible clock skew.", ri->nickname);
  208. goto err;
  209. }
  210. /* Do we already have an entry for this router? */
  211. desc_ent_ptr = NULL;
  212. for (i = 0; i < n_descriptors; ++i) {
  213. if (!strcasecmp(ri->nickname, descriptor_list[i]->nickname)) {
  214. desc_ent_ptr = &descriptor_list[i];
  215. break;
  216. }
  217. }
  218. if (desc_ent_ptr) {
  219. /* if so, decide whether to update it. */
  220. if ((*desc_ent_ptr)->published > ri->published_on) {
  221. /* We already have a newer descriptor */
  222. log_fn(LOG_INFO,"We already have a newer desc for nickname %s. Not adding.",ri->nickname);
  223. /* This isn't really an error; return. */
  224. tor_free(desc_tmp);
  225. if (ri) routerinfo_free(ri);
  226. *desc = end;
  227. return 0;
  228. }
  229. /* We don't have a newer one; we'll update this one. */
  230. free_descriptor_entry(*desc_ent_ptr);
  231. } else {
  232. /* Add this at the end. */
  233. desc_ent_ptr = &descriptor_list[n_descriptors++];
  234. }
  235. (*desc_ent_ptr) = tor_malloc(sizeof(descriptor_entry_t));
  236. (*desc_ent_ptr)->nickname = strdup(ri->nickname);
  237. (*desc_ent_ptr)->published = ri->published_on;
  238. (*desc_ent_ptr)->desc_len = desc_len;
  239. (*desc_ent_ptr)->descriptor = tor_malloc(desc_len+1);
  240. strncpy((*desc_ent_ptr)->descriptor, start, desc_len);
  241. (*desc_ent_ptr)->descriptor[desc_len] = '\0';
  242. *desc = end;
  243. the_directory_is_dirty = 1;
  244. routerinfo_free(ri);
  245. return 0;
  246. err:
  247. tor_free(desc_tmp);
  248. if (ri)
  249. routerinfo_free(ri);
  250. return -1;
  251. }
  252. void
  253. directory_set_dirty()
  254. {
  255. the_directory_is_dirty = 1;
  256. }
  257. int
  258. dirserv_init_from_directory_string(const char *dir)
  259. {
  260. const char *cp = dir;
  261. while(1) {
  262. cp = strstr(cp, "\nrouter ");
  263. if (!cp) break;
  264. ++cp;
  265. if (dirserv_add_descriptor(&cp)) {
  266. return -1;
  267. }
  268. --cp; /*Back up to newline.*/
  269. }
  270. return 0;
  271. }
  272. static int
  273. list_running_servers(char **nicknames_out)
  274. {
  275. char *nickname_lst[MAX_ROUTERS_IN_DIR];
  276. connection_t **connection_array;
  277. int n_conns;
  278. connection_t *conn;
  279. char *cp;
  280. int n = 0, i;
  281. int length;
  282. *nicknames_out = NULL;
  283. nickname_lst[n++] = options.Nickname;
  284. get_connection_array(&connection_array, &n_conns);
  285. for (i = 0; i<n_conns; ++i) {
  286. conn = connection_array[i];
  287. if (conn->type != CONN_TYPE_OR || conn->state != OR_CONN_STATE_OPEN)
  288. continue; /* only list successfully handshaked OR's. */
  289. if(!conn->nickname) /* it's an OP, don't list it */
  290. continue;
  291. nickname_lst[n++] = conn->nickname;
  292. }
  293. length = n + 1; /* spaces + EOS + 1. */
  294. for (i = 0; i<n; ++i) {
  295. length += strlen(nickname_lst[i]);
  296. }
  297. *nicknames_out = tor_malloc(length);
  298. cp = *nicknames_out;
  299. memset(cp,0,length);
  300. for (i = 0; i<n; ++i) {
  301. if (i)
  302. strcat(cp, " ");
  303. strcat(cp, nickname_lst[i]);
  304. while (*cp)
  305. ++cp;
  306. }
  307. return 0;
  308. }
  309. int
  310. dirserv_dump_directory_to_string(char *s, int maxlen,
  311. crypto_pk_env_t *private_key)
  312. {
  313. char *cp, *eos;
  314. char digest[20];
  315. char signature[128];
  316. char published[33];
  317. time_t published_on;
  318. int i;
  319. eos = s+maxlen;
  320. if (list_running_servers(&cp))
  321. return -1;
  322. published_on = time(NULL);
  323. strftime(published, 32, "%Y-%m-%d %H:%M:%S", gmtime(&published_on));
  324. snprintf(s, maxlen,
  325. "signed-directory\n"
  326. "published %s\n"
  327. "recommended-software "RECOMMENDED_SOFTWARE_VERSIONS"\n"
  328. "running-routers %s\n\n", published, cp);
  329. free(cp);
  330. i = strlen(s);
  331. cp = s+i;
  332. for (i = 0; i < n_descriptors; ++i) {
  333. strncat(cp, descriptor_list[i]->descriptor, descriptor_list[i]->desc_len);
  334. cp += descriptor_list[i]->desc_len;
  335. assert(!*cp);
  336. }
  337. /* These multiple strlen calls are inefficient, but dwarfed by the RSA
  338. signature.
  339. */
  340. i = strlen(s);
  341. strncat(s, "directory-signature\n", maxlen-i);
  342. i = strlen(s);
  343. cp = s + i;
  344. if (router_get_dir_hash(s,digest)) {
  345. log_fn(LOG_WARN,"couldn't compute digest");
  346. return -1;
  347. }
  348. if (crypto_pk_private_sign(private_key, digest, 20, signature) < 0) {
  349. log_fn(LOG_WARN,"couldn't sign digest");
  350. return -1;
  351. }
  352. log(LOG_DEBUG,"generated directory digest begins with %02x:%02x:%02x:%02x",
  353. ((int)digest[0])&0xff,((int)digest[1])&0xff,
  354. ((int)digest[2])&0xff,((int)digest[3])&0xff);
  355. strncpy(cp,
  356. "-----BEGIN SIGNATURE-----\n", maxlen-i);
  357. i = strlen(s);
  358. cp = s+i;
  359. if (base64_encode(cp, maxlen-i, signature, 128) < 0) {
  360. log_fn(LOG_WARN,"couldn't base64-encode signature");
  361. return -1;
  362. }
  363. i = strlen(s);
  364. cp = s+i;
  365. strncat(cp, "-----END SIGNATURE-----\n", maxlen-i);
  366. i = strlen(s);
  367. if (i == maxlen) {
  368. log_fn(LOG_WARN,"tried to exceed string length.");
  369. return -1;
  370. }
  371. return 0;
  372. }
  373. size_t dirserv_get_directory(const char **directory)
  374. {
  375. char *new_directory;
  376. char filename[512];
  377. if (the_directory_is_dirty) {
  378. new_directory = tor_malloc(MAX_DIR_SIZE);
  379. if (dirserv_dump_directory_to_string(new_directory, MAX_DIR_SIZE,
  380. get_identity_key())) {
  381. log(LOG_WARN, "Error creating directory.");
  382. free(new_directory);
  383. return 0;
  384. }
  385. tor_free(the_directory);
  386. the_directory = new_directory;
  387. the_directory_len = strlen(the_directory);
  388. log_fn(LOG_INFO,"New directory (size %d):\n%s",the_directory_len,
  389. the_directory);
  390. the_directory_is_dirty = 0;
  391. /* Now read the directory we just made in order to update our own
  392. * router lists. This does more signature checking than is strictly
  393. * necessary, but safe is better than sorry. */
  394. new_directory = tor_strdup(the_directory);
  395. /* use a new copy of the dir, since get_dir_from_string scribbles on it */
  396. if (router_get_dir_from_string(new_directory, get_identity_key())) {
  397. log_fn(LOG_ERR, "We just generated a directory we can't parse. Dying.");
  398. exit(0);
  399. }
  400. free(new_directory);
  401. sprintf(filename,"%s/cached-directory", options.DataDirectory);
  402. if(write_str_to_file(filename,the_directory) < 0) {
  403. log_fn(LOG_WARN, "Couldn't write cached directory to disk. Ignoring.");
  404. }
  405. } else {
  406. log(LOG_INFO,"Directory still clean, reusing.");
  407. }
  408. *directory = the_directory;
  409. return the_directory_len;
  410. }