dirserv.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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_WARN, "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_WARN, "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_WARN, "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_WARN, "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_WARN, "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_WARN,"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_WARN,"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_WARN,"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. tor_free(desc->descriptor);
  151. tor_free(desc->nickname);
  152. free(desc);
  153. }
  154. void
  155. dirserv_free_descriptors()
  156. {
  157. int i;
  158. for (i = 0; i < n_descriptors; ++i) {
  159. free_descriptor_entry(descriptor_list[i]);
  160. }
  161. n_descriptors = 0;
  162. }
  163. /* Return 0 if descriptor added; -1 if descriptor rejected. Updates *desc
  164. * to point after the descriptor if the descriptor is OK.
  165. */
  166. int
  167. dirserv_add_descriptor(const char **desc)
  168. {
  169. descriptor_entry_t **desc_ent_ptr;
  170. routerinfo_t *ri = NULL;
  171. int i;
  172. char *start, *end;
  173. char *desc_tmp = NULL, *cp;
  174. size_t desc_len;
  175. start = strstr(*desc, "router ");
  176. if (!start) {
  177. log(LOG_WARN, "no descriptor found.");
  178. goto err;
  179. }
  180. if ((end = strstr(start+6, "\nrouter "))) {
  181. ++end; /* Include NL. */
  182. } else if ((end = strstr(start+6, "\ndirectory-signature"))) {
  183. ++end;
  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_WARN, "Couldn't parse descriptor");
  195. goto err;
  196. }
  197. tor_free(desc_tmp);
  198. /* Okay. Now check whether the fingerprint is recognized. */
  199. if (!dirserv_router_fingerprint_is_known(ri)) {
  200. log(LOG_WARN, "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. tor_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 = strdup(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. tor_free(desc_tmp);
  241. if (ri)
  242. routerinfo_free(ri);
  243. return -1;
  244. }
  245. void
  246. directory_set_dirty()
  247. {
  248. the_directory_is_dirty = 1;
  249. }
  250. int
  251. dirserv_init_from_directory_string(const char *dir)
  252. {
  253. const char *cp = dir;
  254. while(1) {
  255. cp = strstr(cp, "\nrouter ");
  256. if (!cp) break;
  257. ++cp;
  258. if (dirserv_add_descriptor(&cp)) {
  259. return -1;
  260. }
  261. --cp; /*Back up to newline.*/
  262. }
  263. return 0;
  264. }
  265. static int
  266. list_running_servers(char **nicknames_out)
  267. {
  268. char *nickname_lst[MAX_ROUTERS_IN_DIR];
  269. connection_t **connection_array;
  270. int n_conns;
  271. connection_t *conn;
  272. char *cp;
  273. int n = 0, i;
  274. int length;
  275. *nicknames_out = NULL;
  276. nickname_lst[n++] = options.Nickname;
  277. get_connection_array(&connection_array, &n_conns);
  278. for (i = 0; i<n_conns; ++i) {
  279. conn = connection_array[i];
  280. if (conn->type != CONN_TYPE_OR || conn->state != OR_CONN_STATE_OPEN)
  281. continue; /* only list successfully handshaked OR's. */
  282. if(!conn->nickname) /* it's an OP, don't list it */
  283. continue;
  284. nickname_lst[n++] = conn->nickname;
  285. }
  286. length = n + 1; /* spaces + EOS + 1. */
  287. for (i = 0; i<n; ++i) {
  288. length += strlen(nickname_lst[i]);
  289. }
  290. *nicknames_out = tor_malloc(length);
  291. cp = *nicknames_out;
  292. memset(cp,0,length);
  293. for (i = 0; i<n; ++i) {
  294. if (i)
  295. strcat(cp, " ");
  296. strcat(cp, nickname_lst[i]);
  297. while (*cp)
  298. ++cp;
  299. }
  300. return 0;
  301. }
  302. int
  303. dirserv_dump_directory_to_string(char *s, int maxlen,
  304. crypto_pk_env_t *private_key)
  305. {
  306. char *cp, *eos;
  307. char digest[20];
  308. char signature[128];
  309. char published[33];
  310. time_t published_on;
  311. int i;
  312. eos = s+maxlen;
  313. if (list_running_servers(&cp))
  314. return -1;
  315. published_on = time(NULL);
  316. strftime(published, 32, "%Y-%m-%d %H:%M:%S", gmtime(&published_on));
  317. snprintf(s, maxlen,
  318. "signed-directory\n"
  319. "published %s\n"
  320. "recommended-software "RECOMMENDED_SOFTWARE_VERSIONS"\n"
  321. "running-routers %s\n\n", published, cp);
  322. free(cp);
  323. i = strlen(s);
  324. cp = s+i;
  325. for (i = 0; i < n_descriptors; ++i) {
  326. strncat(cp, descriptor_list[i]->descriptor, descriptor_list[i]->desc_len);
  327. cp += descriptor_list[i]->desc_len;
  328. assert(!*cp);
  329. }
  330. /* These multiple strlen calls are inefficient, but dwarfed by the RSA
  331. signature.
  332. */
  333. i = strlen(s);
  334. strncat(s, "directory-signature\n", maxlen-i);
  335. i = strlen(s);
  336. cp = s + i;
  337. if (router_get_dir_hash(s,digest)) {
  338. log_fn(LOG_WARN,"couldn't compute digest");
  339. return -1;
  340. }
  341. if (crypto_pk_private_sign(private_key, digest, 20, signature) < 0) {
  342. log_fn(LOG_WARN,"couldn't sign digest");
  343. return -1;
  344. }
  345. log(LOG_DEBUG,"generated directory digest begins with %02x:%02x:%02x:%02x",
  346. ((int)digest[0])&0xff,((int)digest[1])&0xff,
  347. ((int)digest[2])&0xff,((int)digest[3])&0xff);
  348. strncpy(cp,
  349. "-----BEGIN SIGNATURE-----\n", maxlen-i);
  350. i = strlen(s);
  351. cp = s+i;
  352. if (base64_encode(cp, maxlen-i, signature, 128) < 0) {
  353. log_fn(LOG_WARN,"couldn't base64-encode signature");
  354. return -1;
  355. }
  356. i = strlen(s);
  357. cp = s+i;
  358. strncat(cp, "-----END SIGNATURE-----\n", maxlen-i);
  359. i = strlen(s);
  360. if (i == maxlen) {
  361. log_fn(LOG_WARN,"tried to exceed string length.");
  362. return -1;
  363. }
  364. return 0;
  365. }
  366. size_t dirserv_get_directory(const char **directory)
  367. {
  368. char *new_directory;
  369. char filename[512];
  370. if (the_directory_is_dirty) {
  371. new_directory = tor_malloc(MAX_DIR_SIZE);
  372. if (dirserv_dump_directory_to_string(new_directory, MAX_DIR_SIZE,
  373. get_identity_key())) {
  374. log(LOG_WARN, "Error creating directory.");
  375. free(new_directory);
  376. return 0;
  377. }
  378. tor_free(the_directory);
  379. the_directory = new_directory;
  380. the_directory_len = strlen(the_directory);
  381. log_fn(LOG_INFO,"New directory (size %d):\n%s",the_directory_len,
  382. the_directory);
  383. the_directory_is_dirty = 0;
  384. /* Now read the directory we just made in order to update our own
  385. * router lists. This does more signature checking than is strictly
  386. * necessary, but safe is better than sorry. */
  387. new_directory = tor_strdup(the_directory);
  388. /* use a new copy of the dir, since get_dir_from_string scribbles on it */
  389. if (router_get_dir_from_string(new_directory, get_identity_key())) {
  390. log_fn(LOG_ERR, "We just generated a directory we can't parse. Dying.");
  391. exit(0);
  392. }
  393. free(new_directory);
  394. sprintf(filename,"%s/cached-directory", options.DataDirectory);
  395. if(write_str_to_file(filename,the_directory) < 0) {
  396. log_fn(LOG_WARN, "Couldn't write cached directory to disk. Ignoring.");
  397. }
  398. } else {
  399. log(LOG_INFO,"Directory still clean, reusing.");
  400. }
  401. *directory = the_directory;
  402. return the_directory_len;
  403. }