dirserv.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include "or.h"
  5. extern or_options_t options; /* command-line and config-file options */
  6. static int the_directory_is_dirty = 1;
  7. static char *the_directory = NULL;
  8. static int the_directory_len = -1;
  9. /************** Fingerprint handling code ************/
  10. typedef struct fingerprint_entry_t {
  11. char *nickname;
  12. char *fingerprint;
  13. } fingerprint_entry_t;
  14. static fingerprint_entry_t fingerprint_list[MAX_ROUTERS_IN_DIR];
  15. static int n_fingerprints = 0;
  16. static void
  17. add_fingerprint_to_dir(const char *nickname, const char *fp)
  18. {
  19. int i;
  20. for (i = 0; i < n_fingerprints; ++i) {
  21. if (!strcasecmp(fingerprint_list[i].nickname,nickname)) {
  22. free(fingerprint_list[i].fingerprint);
  23. fingerprint_list[i].fingerprint = strdup(fp);
  24. return;
  25. }
  26. }
  27. fingerprint_list[n_fingerprints].nickname = strdup(nickname);
  28. fingerprint_list[n_fingerprints].fingerprint = strdup(fp);
  29. ++n_fingerprints;
  30. }
  31. int
  32. dirserv_add_own_fingerprint(const char *nickname, crypto_pk_env_t *pk)
  33. {
  34. char fp[FINGERPRINT_LEN+1];
  35. if (crypto_pk_get_fingerprint(pk, fp)<0) {
  36. log_fn(LOG_ERR, "Error computing fingerprint");
  37. return -1;
  38. }
  39. add_fingerprint_to_dir(nickname, fp);
  40. return 0;
  41. }
  42. /* return 0 on success, -1 on failure */
  43. int
  44. dirserv_parse_fingerprint_file(const char *fname)
  45. {
  46. FILE *file;
  47. char line[FINGERPRINT_LEN+MAX_NICKNAME_LEN+20+1];
  48. char *nickname, *fingerprint;
  49. fingerprint_entry_t fingerprint_list_tmp[MAX_ROUTERS_IN_DIR];
  50. int n_fingerprints_tmp = 0;
  51. int i, result;
  52. if(!(file = fopen(fname, "r"))) {
  53. log_fn(LOG_WARNING, "Cannot open fingerprint file %s", fname);
  54. return -1;
  55. }
  56. while( (result=parse_line_from_file(line, sizeof(line),file,&nickname,&fingerprint)) > 0) {
  57. if (strlen(nickname) > MAX_NICKNAME_LEN) {
  58. log(LOG_WARNING, "Nickname %s too long in fingerprint file. Skipping.", nickname);
  59. continue;
  60. }
  61. if(strlen(fingerprint) != FINGERPRINT_LEN ||
  62. !crypto_pk_check_fingerprint_syntax(fingerprint)) {
  63. log_fn(LOG_WARNING, "Invalid fingerprint (nickname %s, fingerprint %s). Skipping.",
  64. nickname, fingerprint);
  65. continue;
  66. }
  67. for (i = 0; i < n_fingerprints_tmp; ++i) {
  68. if (0==strcasecmp(fingerprint_list_tmp[i].nickname, nickname)) {
  69. log(LOG_WARNING, "Duplicate nickname %s. Skipping.",nickname);
  70. break; /* out of the for. the 'if' below means skip to the next line. */
  71. }
  72. }
  73. if(i == n_fingerprints_tmp) { /* not a duplicate */
  74. fingerprint_list_tmp[n_fingerprints_tmp].nickname = strdup(nickname);
  75. fingerprint_list_tmp[n_fingerprints_tmp].fingerprint = strdup(fingerprint);
  76. ++n_fingerprints_tmp;
  77. }
  78. }
  79. fclose(file);
  80. if(result == 0) { /* eof; replace the global fingerprints list. */
  81. dirserv_free_fingerprint_list();
  82. memcpy(fingerprint_list, fingerprint_list_tmp,
  83. sizeof(fingerprint_entry_t)*n_fingerprints_tmp);
  84. n_fingerprints = n_fingerprints_tmp;
  85. return 0;
  86. }
  87. /* error */
  88. log_fn(LOG_WARNING, "Error reading from fingerprint file");
  89. for (i = 0; i < n_fingerprints_tmp; ++i) {
  90. free(fingerprint_list_tmp[i].nickname);
  91. free(fingerprint_list_tmp[i].fingerprint);
  92. }
  93. return -1;
  94. }
  95. /* return 1 if router's identity and nickname match. */
  96. int
  97. dirserv_router_fingerprint_is_known(const routerinfo_t *router)
  98. {
  99. int i;
  100. fingerprint_entry_t *ent =NULL;
  101. char fp[FINGERPRINT_LEN+1];
  102. log_fn(LOG_DEBUG, "%d fingerprints known.", n_fingerprints);
  103. for (i=0;i<n_fingerprints;++i) {
  104. log_fn(LOG_DEBUG,"%s vs %s", router->nickname, fingerprint_list[i].nickname);
  105. if (!strcasecmp(router->nickname,fingerprint_list[i].nickname)) {
  106. ent = &fingerprint_list[i];
  107. break;
  108. }
  109. }
  110. if (!ent) { /* No such server known */
  111. log_fn(LOG_WARNING,"no fingerprint found for %s",router->nickname);
  112. return 0;
  113. }
  114. if (crypto_pk_get_fingerprint(router->identity_pkey, fp)) {
  115. log_fn(LOG_WARNING,"error computing fingerprint");
  116. return 0;
  117. }
  118. if (0==strcasecmp(ent->fingerprint, fp)) {
  119. log_fn(LOG_DEBUG,"good fingerprint for %s",router->nickname);
  120. return 1; /* Right fingerprint. */
  121. } else {
  122. log_fn(LOG_WARNING,"mismatched fingerprint for %s",router->nickname);
  123. return 0; /* Wrong fingerprint. */
  124. }
  125. }
  126. void
  127. dirserv_free_fingerprint_list()
  128. {
  129. int i;
  130. for (i = 0; i < n_fingerprints; ++i) {
  131. free(fingerprint_list[i].nickname);
  132. free(fingerprint_list[i].fingerprint);
  133. }
  134. n_fingerprints = 0;
  135. }
  136. /*
  137. * Descriptor list
  138. */
  139. typedef struct descriptor_entry_t {
  140. char *nickname;
  141. time_t published;
  142. size_t desc_len;
  143. char *descriptor;
  144. } descriptor_entry_t;
  145. static descriptor_entry_t *descriptor_list[MAX_ROUTERS_IN_DIR];
  146. static int n_descriptors = 0;
  147. static void free_descriptor_entry(descriptor_entry_t *desc)
  148. {
  149. if (desc->descriptor)
  150. free(desc->descriptor);
  151. if (desc->nickname)
  152. free(desc->nickname);
  153. free(desc);
  154. }
  155. void
  156. dirserv_free_descriptors()
  157. {
  158. int i;
  159. for (i = 0; i < n_descriptors; ++i) {
  160. free_descriptor_entry(descriptor_list[i]);
  161. }
  162. n_descriptors = 0;
  163. }
  164. /* Return 0 if descriptor added; -1 if descriptor rejected. Updates *desc
  165. * to point after the descriptor if the descriptor is OK.
  166. */
  167. int
  168. dirserv_add_descriptor(const char **desc)
  169. {
  170. descriptor_entry_t **desc_ent_ptr;
  171. routerinfo_t *ri = NULL;
  172. int i;
  173. char *start, *end;
  174. char *desc_tmp = NULL, *cp;
  175. size_t desc_len;
  176. start = strstr(*desc, "router ");
  177. if (!start) {
  178. log(LOG_WARNING, "no descriptor found.");
  179. goto err;
  180. }
  181. end = strstr(start+6, "\nrouter ");
  182. if (end) {
  183. ++end; /* Include NL. */
  184. } else {
  185. end = start+strlen(start);
  186. }
  187. desc_len = end-start;
  188. cp = desc_tmp = tor_malloc(desc_len+1);
  189. strncpy(desc_tmp, start, desc_len);
  190. desc_tmp[desc_len]='\0';
  191. /* Check: is the descriptor syntactically valid? */
  192. ri = router_get_entry_from_string(&cp);
  193. if (!ri) {
  194. log(LOG_WARNING, "Couldn't parse descriptor");
  195. goto err;
  196. }
  197. free(desc_tmp); desc_tmp = NULL;
  198. /* Okay. Now check whether the fingerprint is recognized. */
  199. if (!dirserv_router_fingerprint_is_known(ri)) {
  200. log(LOG_WARNING, "Identity is unrecognized for descriptor");
  201. goto err;
  202. }
  203. /* Do we already have an entry for this router? */
  204. desc_ent_ptr = NULL;
  205. for (i = 0; i < n_descriptors; ++i) {
  206. if (!strcasecmp(ri->nickname, descriptor_list[i]->nickname)) {
  207. desc_ent_ptr = &descriptor_list[i];
  208. break;
  209. }
  210. }
  211. if (desc_ent_ptr) {
  212. /* if so, decide whether to update it. */
  213. if ((*desc_ent_ptr)->published > ri->published_on) {
  214. /* We already have a newer descriptor */
  215. log_fn(LOG_INFO,"We already have a newer desc for nickname %s. Not adding.",ri->nickname);
  216. /* This isn't really an error; return. */
  217. if (desc_tmp) free(desc_tmp);
  218. if (ri) routerinfo_free(ri);
  219. *desc = end;
  220. return 0;
  221. }
  222. /* We don't have a newer one; we'll update this one. */
  223. free_descriptor_entry(*desc_ent_ptr);
  224. } else {
  225. /* Add this at the end. */
  226. desc_ent_ptr = &descriptor_list[n_descriptors++];
  227. }
  228. (*desc_ent_ptr) = tor_malloc(sizeof(descriptor_entry_t));
  229. (*desc_ent_ptr)->nickname = ri->nickname;
  230. (*desc_ent_ptr)->published = ri->published_on;
  231. (*desc_ent_ptr)->desc_len = desc_len;
  232. (*desc_ent_ptr)->descriptor = tor_malloc(desc_len+1);
  233. strncpy((*desc_ent_ptr)->descriptor, start, desc_len);
  234. (*desc_ent_ptr)->descriptor[desc_len] = '\0';
  235. *desc = end;
  236. the_directory_is_dirty = 1;
  237. routerinfo_free(ri);
  238. return 0;
  239. err:
  240. if (desc_tmp)
  241. free(desc_tmp);
  242. if (ri)
  243. routerinfo_free(ri);
  244. return -1;
  245. }
  246. void
  247. directory_set_dirty()
  248. {
  249. the_directory_is_dirty = 1;
  250. }
  251. int
  252. dirserv_init_from_directory_string(const char *dir)
  253. {
  254. const char *cp = dir;
  255. while(1) {
  256. cp = strstr(cp, "\nrouter ");
  257. if (!cp) break;
  258. ++cp;
  259. if (dirserv_add_descriptor(&cp)) {
  260. return -1;
  261. }
  262. --cp; /*Back up to newline.*/
  263. }
  264. return 0;
  265. }
  266. int
  267. dirserv_dump_directory_to_string(char *s, int maxlen,
  268. crypto_pk_env_t *private_key)
  269. {
  270. char *cp, *eos;
  271. char digest[20];
  272. char signature[128];
  273. char published[33];
  274. time_t published_on;
  275. int i;
  276. eos = s+maxlen;
  277. if (list_running_servers(&cp))
  278. return -1;
  279. published_on = time(NULL);
  280. strftime(published, 32, "%Y-%m-%d %H:%M:%S", gmtime(&published_on));
  281. snprintf(s, maxlen,
  282. "signed-directory\n"
  283. "published %s\n"
  284. "recommended-software "RECOMMENDED_SOFTWARE_VERSIONS"\n"
  285. "running-routers %s\n", published, cp);
  286. free(cp);
  287. i = strlen(s);
  288. cp = s+i;
  289. for (i = 0; i < n_descriptors; ++i) {
  290. strncat(cp, descriptor_list[i]->descriptor, descriptor_list[i]->desc_len);
  291. cp += descriptor_list[i]->desc_len;
  292. assert(!*cp);
  293. }
  294. /* These multiple strlen calls are inefficient, but dwarfed by the RSA
  295. signature.
  296. */
  297. i = strlen(s);
  298. strncat(s, "directory-signature\n", maxlen-i);
  299. i = strlen(s);
  300. cp = s + i;
  301. if (router_get_dir_hash(s,digest)) {
  302. log_fn(LOG_WARNING,"couldn't compute digest");
  303. return -1;
  304. }
  305. if (crypto_pk_private_sign(private_key, digest, 20, signature) < 0) {
  306. log_fn(LOG_WARNING,"couldn't sign digest");
  307. return -1;
  308. }
  309. strncpy(cp,
  310. "-----BEGIN SIGNATURE-----\n", maxlen-i);
  311. i = strlen(s);
  312. cp = s+i;
  313. if (base64_encode(cp, maxlen-i, signature, 128) < 0) {
  314. log_fn(LOG_WARNING,"couldn't base64-encode signature");
  315. return -1;
  316. }
  317. i = strlen(s);
  318. cp = s+i;
  319. strncat(cp, "-----END SIGNATURE-----\n", maxlen-i);
  320. i = strlen(s);
  321. if (i == maxlen) {
  322. log_fn(LOG_WARNING,"tried to exceed string length.");
  323. return -1;
  324. }
  325. return 0;
  326. }
  327. size_t dirserv_get_directory(const char **directory)
  328. {
  329. char *new_directory;
  330. char filename[512];
  331. if (the_directory_is_dirty) {
  332. new_directory = tor_malloc(MAX_DIR_SIZE);
  333. if (dirserv_dump_directory_to_string(new_directory, MAX_DIR_SIZE,
  334. get_identity_key())) {
  335. log(LOG_WARNING, "Error creating directory.");
  336. free(new_directory);
  337. return 0;
  338. }
  339. if (the_directory)
  340. free(the_directory);
  341. the_directory = new_directory;
  342. the_directory_len = strlen(the_directory);
  343. log_fn(LOG_INFO,"New directory (size %d):\n%s",the_directory_len,
  344. the_directory);
  345. the_directory_is_dirty = 0;
  346. /* Now read the directory we just made in order to update our own
  347. * router lists. This does more signature checking than is strictly
  348. * necessary, but safe is better than sorry. */
  349. new_directory = strdup(the_directory);
  350. /* use a new copy of the dir, since get_dir_from_string scribbles on it */
  351. if (router_get_dir_from_string(new_directory, get_identity_key())) {
  352. log_fn(LOG_ERR, "We just generated a directory we can't parse. Dying.");
  353. exit(0);
  354. }
  355. free(new_directory);
  356. sprintf(filename,"%s/cached-directory", options.DataDirectory);
  357. if(write_str_to_file(filename,the_directory) < 0) {
  358. log_fn(LOG_WARNING, "Couldn't write cached directory to disk. Ignoring.");
  359. }
  360. } else {
  361. log(LOG_INFO,"Directory still clean, reusing.");
  362. }
  363. *directory = the_directory;
  364. return the_directory_len;
  365. }