dirserv.c 13 KB

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