dirserv.c 9.8 KB

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