dirserv.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. static int list_running_servers(char **nicknames_out);
  10. /************** Fingerprint handling code ************/
  11. typedef struct fingerprint_entry_t {
  12. char *nickname;
  13. char *fingerprint;
  14. } fingerprint_entry_t;
  15. static fingerprint_entry_t fingerprint_list[MAX_ROUTERS_IN_DIR];
  16. static int n_fingerprints = 0;
  17. static void
  18. add_fingerprint_to_dir(const char *nickname, const char *fp)
  19. {
  20. int i;
  21. for (i = 0; i < n_fingerprints; ++i) {
  22. if (!strcasecmp(fingerprint_list[i].nickname,nickname)) {
  23. free(fingerprint_list[i].fingerprint);
  24. fingerprint_list[i].fingerprint = tor_strdup(fp);
  25. return;
  26. }
  27. }
  28. fingerprint_list[n_fingerprints].nickname = tor_strdup(nickname);
  29. fingerprint_list[n_fingerprints].fingerprint = tor_strdup(fp);
  30. ++n_fingerprints;
  31. }
  32. int
  33. dirserv_add_own_fingerprint(const char *nickname, crypto_pk_env_t *pk)
  34. {
  35. char fp[FINGERPRINT_LEN+1];
  36. if (crypto_pk_get_fingerprint(pk, fp)<0) {
  37. log_fn(LOG_ERR, "Error computing fingerprint");
  38. return -1;
  39. }
  40. add_fingerprint_to_dir(nickname, fp);
  41. return 0;
  42. }
  43. /* return 0 on success, -1 on failure */
  44. int
  45. dirserv_parse_fingerprint_file(const char *fname)
  46. {
  47. FILE *file;
  48. char line[FINGERPRINT_LEN+MAX_NICKNAME_LEN+20+1];
  49. char *nickname, *fingerprint;
  50. fingerprint_entry_t fingerprint_list_tmp[MAX_ROUTERS_IN_DIR];
  51. int n_fingerprints_tmp = 0;
  52. int i, result;
  53. if(!(file = fopen(fname, "r"))) {
  54. log_fn(LOG_WARNING, "Cannot open fingerprint file %s", fname);
  55. return -1;
  56. }
  57. while( (result=parse_line_from_file(line, sizeof(line),file,&nickname,&fingerprint)) > 0) {
  58. if (strlen(nickname) > MAX_NICKNAME_LEN) {
  59. log(LOG_WARNING, "Nickname %s too long in fingerprint file. Skipping.", nickname);
  60. continue;
  61. }
  62. if(strlen(fingerprint) != FINGERPRINT_LEN ||
  63. !crypto_pk_check_fingerprint_syntax(fingerprint)) {
  64. log_fn(LOG_WARNING, "Invalid fingerprint (nickname %s, fingerprint %s). Skipping.",
  65. nickname, fingerprint);
  66. continue;
  67. }
  68. for (i = 0; i < n_fingerprints_tmp; ++i) {
  69. if (0==strcasecmp(fingerprint_list_tmp[i].nickname, nickname)) {
  70. log(LOG_WARNING, "Duplicate nickname %s. Skipping.",nickname);
  71. break; /* out of the for. the 'if' below means skip to the next line. */
  72. }
  73. }
  74. if(i == n_fingerprints_tmp) { /* not a duplicate */
  75. fingerprint_list_tmp[n_fingerprints_tmp].nickname = tor_strdup(nickname);
  76. fingerprint_list_tmp[n_fingerprints_tmp].fingerprint = tor_strdup(fingerprint);
  77. ++n_fingerprints_tmp;
  78. }
  79. }
  80. fclose(file);
  81. if(result == 0) { /* eof; replace the global fingerprints list. */
  82. dirserv_free_fingerprint_list();
  83. memcpy(fingerprint_list, fingerprint_list_tmp,
  84. sizeof(fingerprint_entry_t)*n_fingerprints_tmp);
  85. n_fingerprints = n_fingerprints_tmp;
  86. return 0;
  87. }
  88. /* error */
  89. log_fn(LOG_WARNING, "Error reading from fingerprint file");
  90. for (i = 0; i < n_fingerprints_tmp; ++i) {
  91. free(fingerprint_list_tmp[i].nickname);
  92. free(fingerprint_list_tmp[i].fingerprint);
  93. }
  94. return -1;
  95. }
  96. /* return 1 if router's identity and nickname match. */
  97. int
  98. dirserv_router_fingerprint_is_known(const routerinfo_t *router)
  99. {
  100. int i;
  101. fingerprint_entry_t *ent =NULL;
  102. char fp[FINGERPRINT_LEN+1];
  103. log_fn(LOG_DEBUG, "%d fingerprints known.", n_fingerprints);
  104. for (i=0;i<n_fingerprints;++i) {
  105. log_fn(LOG_DEBUG,"%s vs %s", router->nickname, fingerprint_list[i].nickname);
  106. if (!strcasecmp(router->nickname,fingerprint_list[i].nickname)) {
  107. ent = &fingerprint_list[i];
  108. break;
  109. }
  110. }
  111. if (!ent) { /* No such server known */
  112. log_fn(LOG_WARNING,"no fingerprint found for %s",router->nickname);
  113. return 0;
  114. }
  115. if (crypto_pk_get_fingerprint(router->identity_pkey, fp)) {
  116. log_fn(LOG_WARNING,"error computing fingerprint");
  117. return 0;
  118. }
  119. if (0==strcasecmp(ent->fingerprint, fp)) {
  120. log_fn(LOG_DEBUG,"good fingerprint for %s",router->nickname);
  121. return 1; /* Right fingerprint. */
  122. } else {
  123. log_fn(LOG_WARNING,"mismatched fingerprint for %s",router->nickname);
  124. return 0; /* Wrong fingerprint. */
  125. }
  126. }
  127. void
  128. dirserv_free_fingerprint_list()
  129. {
  130. int i;
  131. for (i = 0; i < n_fingerprints; ++i) {
  132. free(fingerprint_list[i].nickname);
  133. free(fingerprint_list[i].fingerprint);
  134. }
  135. n_fingerprints = 0;
  136. }
  137. /*
  138. * Descriptor list
  139. */
  140. typedef struct descriptor_entry_t {
  141. char *nickname;
  142. time_t published;
  143. size_t desc_len;
  144. char *descriptor;
  145. } descriptor_entry_t;
  146. static descriptor_entry_t *descriptor_list[MAX_ROUTERS_IN_DIR];
  147. static int n_descriptors = 0;
  148. static void free_descriptor_entry(descriptor_entry_t *desc)
  149. {
  150. if (desc->descriptor)
  151. free(desc->descriptor);
  152. if (desc->nickname)
  153. 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_WARNING, "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_WARNING, "Couldn't parse descriptor");
  197. goto err;
  198. }
  199. free(desc_tmp); desc_tmp = NULL;
  200. /* Okay. Now check whether the fingerprint is recognized. */
  201. if (!dirserv_router_fingerprint_is_known(ri)) {
  202. log(LOG_WARNING, "Identity is unrecognized for descriptor");
  203. goto err;
  204. }
  205. /* Do we already have an entry for this router? */
  206. desc_ent_ptr = NULL;
  207. for (i = 0; i < n_descriptors; ++i) {
  208. if (!strcasecmp(ri->nickname, descriptor_list[i]->nickname)) {
  209. desc_ent_ptr = &descriptor_list[i];
  210. break;
  211. }
  212. }
  213. if (desc_ent_ptr) {
  214. /* if so, decide whether to update it. */
  215. if ((*desc_ent_ptr)->published > ri->published_on) {
  216. /* We already have a newer descriptor */
  217. log_fn(LOG_INFO,"We already have a newer desc for nickname %s. Not adding.",ri->nickname);
  218. /* This isn't really an error; return. */
  219. if (desc_tmp) free(desc_tmp);
  220. if (ri) routerinfo_free(ri);
  221. *desc = end;
  222. return 0;
  223. }
  224. /* We don't have a newer one; we'll update this one. */
  225. free_descriptor_entry(*desc_ent_ptr);
  226. } else {
  227. /* Add this at the end. */
  228. desc_ent_ptr = &descriptor_list[n_descriptors++];
  229. }
  230. (*desc_ent_ptr) = tor_malloc(sizeof(descriptor_entry_t));
  231. (*desc_ent_ptr)->nickname = ri->nickname;
  232. (*desc_ent_ptr)->published = ri->published_on;
  233. (*desc_ent_ptr)->desc_len = desc_len;
  234. (*desc_ent_ptr)->descriptor = tor_malloc(desc_len+1);
  235. strncpy((*desc_ent_ptr)->descriptor, start, desc_len);
  236. (*desc_ent_ptr)->descriptor[desc_len] = '\0';
  237. *desc = end;
  238. the_directory_is_dirty = 1;
  239. routerinfo_free(ri);
  240. return 0;
  241. err:
  242. if (desc_tmp)
  243. free(desc_tmp);
  244. if (ri)
  245. routerinfo_free(ri);
  246. return -1;
  247. }
  248. void
  249. directory_set_dirty()
  250. {
  251. the_directory_is_dirty = 1;
  252. }
  253. int
  254. dirserv_init_from_directory_string(const char *dir)
  255. {
  256. const char *cp = dir;
  257. while(1) {
  258. cp = strstr(cp, "\nrouter ");
  259. if (!cp) break;
  260. ++cp;
  261. if (dirserv_add_descriptor(&cp)) {
  262. return -1;
  263. }
  264. --cp; /*Back up to newline.*/
  265. }
  266. return 0;
  267. }
  268. static int
  269. list_running_servers(char **nicknames_out)
  270. {
  271. char *nickname_lst[MAX_ROUTERS_IN_DIR];
  272. connection_t **connection_array;
  273. int n_conns;
  274. connection_t *conn;
  275. char *cp;
  276. int n = 0, i;
  277. int length;
  278. *nicknames_out = NULL;
  279. nickname_lst[n++] = options.Nickname;
  280. get_connection_array(&connection_array, &n_conns);
  281. for (i = 0; i<n_conns; ++i) {
  282. conn = connection_array[i];
  283. if (conn->type != CONN_TYPE_OR || conn->state != OR_CONN_STATE_OPEN)
  284. continue; /* only list successfully handshaked OR's. */
  285. if(!conn->nickname) /* it's an OP, don't list it */
  286. continue;
  287. nickname_lst[n++] = conn->nickname;
  288. }
  289. length = n + 1; /* spaces + EOS + 1. */
  290. for (i = 0; i<n; ++i) {
  291. length += strlen(nickname_lst[i]);
  292. }
  293. *nicknames_out = tor_malloc(length);
  294. cp = *nicknames_out;
  295. memset(cp,0,length);
  296. for (i = 0; i<n; ++i) {
  297. if (i)
  298. strcat(cp, " ");
  299. strcat(cp, nickname_lst[i]);
  300. while (*cp)
  301. ++cp;
  302. }
  303. return 0;
  304. }
  305. int
  306. dirserv_dump_directory_to_string(char *s, int maxlen,
  307. crypto_pk_env_t *private_key)
  308. {
  309. char *cp, *eos;
  310. char digest[20];
  311. char signature[128];
  312. char published[33];
  313. time_t published_on;
  314. int i;
  315. eos = s+maxlen;
  316. if (list_running_servers(&cp))
  317. return -1;
  318. published_on = time(NULL);
  319. strftime(published, 32, "%Y-%m-%d %H:%M:%S", gmtime(&published_on));
  320. snprintf(s, maxlen,
  321. "signed-directory\n"
  322. "published %s\n"
  323. "recommended-software "RECOMMENDED_SOFTWARE_VERSIONS"\n"
  324. "running-routers %s\n", published, cp);
  325. free(cp);
  326. i = strlen(s);
  327. cp = s+i;
  328. for (i = 0; i < n_descriptors; ++i) {
  329. strncat(cp, descriptor_list[i]->descriptor, descriptor_list[i]->desc_len);
  330. cp += descriptor_list[i]->desc_len;
  331. assert(!*cp);
  332. }
  333. /* These multiple strlen calls are inefficient, but dwarfed by the RSA
  334. signature.
  335. */
  336. i = strlen(s);
  337. strncat(s, "directory-signature\n", maxlen-i);
  338. i = strlen(s);
  339. cp = s + i;
  340. if (router_get_dir_hash(s,digest)) {
  341. log_fn(LOG_WARNING,"couldn't compute digest");
  342. return -1;
  343. }
  344. if (crypto_pk_private_sign(private_key, digest, 20, signature) < 0) {
  345. log_fn(LOG_WARNING,"couldn't sign digest");
  346. return -1;
  347. }
  348. log(LOG_DEBUG,"generated directory digest begins with %02x:%02x:%02x:%02x",
  349. ((int)digest[0])&0xff,((int)digest[1])&0xff,
  350. ((int)digest[2])&0xff,((int)digest[3])&0xff);
  351. strncpy(cp,
  352. "-----BEGIN SIGNATURE-----\n", maxlen-i);
  353. i = strlen(s);
  354. cp = s+i;
  355. if (base64_encode(cp, maxlen-i, signature, 128) < 0) {
  356. log_fn(LOG_WARNING,"couldn't base64-encode signature");
  357. return -1;
  358. }
  359. i = strlen(s);
  360. cp = s+i;
  361. strncat(cp, "-----END SIGNATURE-----\n", maxlen-i);
  362. i = strlen(s);
  363. if (i == maxlen) {
  364. log_fn(LOG_WARNING,"tried to exceed string length.");
  365. return -1;
  366. }
  367. return 0;
  368. }
  369. size_t dirserv_get_directory(const char **directory)
  370. {
  371. char *new_directory;
  372. char filename[512];
  373. if (the_directory_is_dirty) {
  374. new_directory = tor_malloc(MAX_DIR_SIZE);
  375. if (dirserv_dump_directory_to_string(new_directory, MAX_DIR_SIZE,
  376. get_identity_key())) {
  377. log(LOG_WARNING, "Error creating directory.");
  378. free(new_directory);
  379. return 0;
  380. }
  381. if (the_directory)
  382. free(the_directory);
  383. the_directory = new_directory;
  384. the_directory_len = strlen(the_directory);
  385. log_fn(LOG_INFO,"New directory (size %d):\n%s",the_directory_len,
  386. the_directory);
  387. the_directory_is_dirty = 0;
  388. /* Now read the directory we just made in order to update our own
  389. * router lists. This does more signature checking than is strictly
  390. * necessary, but safe is better than sorry. */
  391. new_directory = tor_strdup(the_directory);
  392. /* use a new copy of the dir, since get_dir_from_string scribbles on it */
  393. if (router_get_dir_from_string(new_directory, get_identity_key())) {
  394. log_fn(LOG_ERR, "We just generated a directory we can't parse. Dying.");
  395. exit(0);
  396. }
  397. free(new_directory);
  398. sprintf(filename,"%s/cached-directory", options.DataDirectory);
  399. if(write_str_to_file(filename,the_directory) < 0) {
  400. log_fn(LOG_WARNING, "Couldn't write cached directory to disk. Ignoring.");
  401. }
  402. } else {
  403. log(LOG_INFO,"Directory still clean, reusing.");
  404. }
  405. *directory = the_directory;
  406. return the_directory_len;
  407. }