dirserv.c 13 KB

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