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 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. * -1 if they don't match, 0 if the nickname is not known. */
  98. int
  99. dirserv_router_fingerprint_is_known(const routerinfo_t *router)
  100. {
  101. int i;
  102. fingerprint_entry_t *ent =NULL;
  103. char fp[FINGERPRINT_LEN+1];
  104. log_fn(LOG_DEBUG, "%d fingerprints known.", n_fingerprints);
  105. for (i=0;i<n_fingerprints;++i) {
  106. log_fn(LOG_DEBUG,"%s vs %s", router->nickname, fingerprint_list[i].nickname);
  107. if (!strcasecmp(router->nickname,fingerprint_list[i].nickname)) {
  108. ent = &fingerprint_list[i];
  109. break;
  110. }
  111. }
  112. if (!ent) { /* No such server known */
  113. log_fn(LOG_INFO,"no fingerprint found for %s",router->nickname);
  114. return 0;
  115. }
  116. if (crypto_pk_get_fingerprint(router->identity_pkey, fp)) {
  117. log_fn(LOG_WARN,"error computing fingerprint");
  118. return -1;
  119. }
  120. if (0==strcasecmp(ent->fingerprint, fp)) {
  121. log_fn(LOG_DEBUG,"good fingerprint for %s",router->nickname);
  122. return 1; /* Right fingerprint. */
  123. } else {
  124. log_fn(LOG_WARN,"mismatched fingerprint for %s",router->nickname);
  125. return -1; /* Wrong fingerprint. */
  126. }
  127. }
  128. void
  129. dirserv_free_fingerprint_list()
  130. {
  131. int i;
  132. for (i = 0; i < n_fingerprints; ++i) {
  133. free(fingerprint_list[i].nickname);
  134. free(fingerprint_list[i].fingerprint);
  135. }
  136. n_fingerprints = 0;
  137. }
  138. /*
  139. * Descriptor list
  140. */
  141. typedef struct descriptor_entry_t {
  142. char *nickname;
  143. time_t published;
  144. size_t desc_len;
  145. char *descriptor;
  146. } descriptor_entry_t;
  147. static descriptor_entry_t *descriptor_list[MAX_ROUTERS_IN_DIR];
  148. static int n_descriptors = 0;
  149. static void free_descriptor_entry(descriptor_entry_t *desc)
  150. {
  151. tor_free(desc->descriptor);
  152. tor_free(desc->nickname);
  153. free(desc);
  154. }
  155. void
  156. dirserv_free_descriptors()
  157. {
  158. int i;
  159. for (i = 0; i < n_descriptors; ++i) {
  160. free_descriptor_entry(descriptor_list[i]);
  161. }
  162. n_descriptors = 0;
  163. }
  164. /* Return 0 if descriptor is well-formed; -1 if descriptor is not
  165. * well-formed. Update *desc to point after the descriptor if the
  166. * descriptor is well-formed.
  167. */
  168. /* XXX down the road perhaps we should return 1 for accepted, 0 for
  169. * well-formed but rejected, -1 for not-well-formed. So remote servers
  170. * can know if their submission was accepted and not just whether it
  171. * was well-formed. ...Or maybe we shouldn't give them that info?
  172. */
  173. int
  174. dirserv_add_descriptor(const char **desc)
  175. {
  176. descriptor_entry_t **desc_ent_ptr;
  177. routerinfo_t *ri = NULL;
  178. int i, r;
  179. char *start, *end;
  180. char *desc_tmp = NULL;
  181. const char *cp;
  182. size_t desc_len;
  183. start = strstr(*desc, "router ");
  184. if (!start) {
  185. log(LOG_WARN, "no descriptor found.");
  186. goto err;
  187. }
  188. if ((end = strstr(start+6, "\nrouter "))) {
  189. ++end; /* Include NL. */
  190. } else if ((end = strstr(start+6, "\ndirectory-signature"))) {
  191. ++end;
  192. } else {
  193. end = start+strlen(start);
  194. }
  195. desc_len = end-start;
  196. cp = desc_tmp = tor_strndup(start, desc_len);
  197. /* Check: is the descriptor syntactically valid? */
  198. ri = router_get_entry_from_string(&cp);
  199. if (!ri) {
  200. log(LOG_WARN, "Couldn't parse descriptor");
  201. goto err;
  202. }
  203. tor_free(desc_tmp);
  204. /* Okay. Now check whether the fingerprint is recognized. */
  205. r = dirserv_router_fingerprint_is_known(ri);
  206. if(r<1) {
  207. if(r==0) {
  208. log_fn(LOG_WARN, "Unknown nickname %s. Not adding.", ri->nickname);
  209. } else {
  210. log_fn(LOG_WARN, "Known nickname %s, wrong fingerprint. Not adding.", ri->nickname);
  211. }
  212. routerinfo_free(ri);
  213. *desc = end;
  214. return 0;
  215. }
  216. /* Is there too much clock skew? */
  217. if (ri->published_on > time(NULL)+ROUTER_ALLOW_SKEW) {
  218. log_fn(LOG_WARN, "Publication time for nickname %s is too far in the future; possible clock skew. Not adding", ri->nickname);
  219. routerinfo_free(ri);
  220. *desc = end;
  221. return 0;
  222. }
  223. /* Do we already have an entry for this router? */
  224. desc_ent_ptr = NULL;
  225. for (i = 0; i < n_descriptors; ++i) {
  226. if (!strcasecmp(ri->nickname, descriptor_list[i]->nickname)) {
  227. desc_ent_ptr = &descriptor_list[i];
  228. break;
  229. }
  230. }
  231. if (desc_ent_ptr) {
  232. /* if so, decide whether to update it. */
  233. if ((*desc_ent_ptr)->published > ri->published_on) {
  234. /* We already have a newer descriptor */
  235. log_fn(LOG_INFO,"We already have a newer desc for nickname %s. Not adding.",ri->nickname);
  236. /* This isn't really an error; return. */
  237. routerinfo_free(ri);
  238. *desc = end;
  239. return 0;
  240. }
  241. /* We don't have a newer one; we'll update this one. */
  242. free_descriptor_entry(*desc_ent_ptr);
  243. } else {
  244. /* Add this at the end. */
  245. desc_ent_ptr = &descriptor_list[n_descriptors++];
  246. /* XXX check if n_descriptors is too big */
  247. }
  248. (*desc_ent_ptr) = tor_malloc(sizeof(descriptor_entry_t));
  249. (*desc_ent_ptr)->nickname = tor_strdup(ri->nickname);
  250. (*desc_ent_ptr)->published = ri->published_on;
  251. (*desc_ent_ptr)->desc_len = desc_len;
  252. (*desc_ent_ptr)->descriptor = tor_malloc(desc_len+1);
  253. strncpy((*desc_ent_ptr)->descriptor, start, desc_len);
  254. (*desc_ent_ptr)->descriptor[desc_len] = '\0';
  255. *desc = end;
  256. directory_set_dirty();
  257. routerinfo_free(ri);
  258. return 0;
  259. err:
  260. tor_free(desc_tmp);
  261. if (ri)
  262. routerinfo_free(ri);
  263. return -1;
  264. }
  265. void
  266. directory_set_dirty()
  267. {
  268. the_directory_is_dirty = 1;
  269. }
  270. int
  271. dirserv_init_from_directory_string(const char *dir)
  272. {
  273. const char *cp = dir;
  274. while(1) {
  275. cp = strstr(cp, "\nrouter ");
  276. if (!cp) break;
  277. ++cp;
  278. if (dirserv_add_descriptor(&cp)) {
  279. return -1;
  280. }
  281. --cp; /*Back up to newline.*/
  282. }
  283. return 0;
  284. }
  285. static int
  286. list_running_servers(char **nicknames_out)
  287. {
  288. char *nickname_lst[MAX_ROUTERS_IN_DIR];
  289. connection_t **connection_array;
  290. int n_conns;
  291. connection_t *conn;
  292. char *cp;
  293. int n = 0, i;
  294. int length;
  295. *nicknames_out = NULL;
  296. nickname_lst[n++] = options.Nickname;
  297. get_connection_array(&connection_array, &n_conns);
  298. for (i = 0; i<n_conns; ++i) {
  299. conn = connection_array[i];
  300. if (conn->type != CONN_TYPE_OR || conn->state != OR_CONN_STATE_OPEN)
  301. continue; /* only list successfully handshaked OR's. */
  302. if(!conn->nickname) /* it's an OP, don't list it */
  303. continue;
  304. nickname_lst[n++] = conn->nickname;
  305. }
  306. length = n + 1; /* spaces + EOS + 1. */
  307. for (i = 0; i<n; ++i) {
  308. length += strlen(nickname_lst[i]);
  309. }
  310. *nicknames_out = tor_malloc_zero(length);
  311. cp = *nicknames_out;
  312. for (i = 0; i<n; ++i) {
  313. if (i)
  314. strcat(cp, " ");
  315. strcat(cp, nickname_lst[i]);
  316. while (*cp)
  317. ++cp;
  318. }
  319. return 0;
  320. }
  321. int
  322. dirserv_dump_directory_to_string(char *s, int maxlen,
  323. crypto_pk_env_t *private_key)
  324. {
  325. char *cp, *eos;
  326. char digest[20];
  327. char signature[128];
  328. char published[33];
  329. time_t published_on;
  330. int i;
  331. eos = s+maxlen;
  332. if (list_running_servers(&cp))
  333. return -1;
  334. published_on = time(NULL);
  335. strftime(published, 32, "%Y-%m-%d %H:%M:%S", gmtime(&published_on));
  336. snprintf(s, maxlen,
  337. "signed-directory\n"
  338. "published %s\n"
  339. "recommended-software %s\n"
  340. "running-routers %s\n\n", published, options.RecommendedVersions, cp);
  341. free(cp);
  342. i = strlen(s);
  343. cp = s+i;
  344. for (i = 0; i < n_descriptors; ++i) {
  345. strncat(cp, descriptor_list[i]->descriptor, descriptor_list[i]->desc_len);
  346. /* XXX Nick: do strncat and friends null-terminate? man page is ambiguous. */
  347. cp += descriptor_list[i]->desc_len;
  348. assert(!*cp);
  349. }
  350. /* These multiple strlen calls are inefficient, but dwarfed by the RSA
  351. signature.
  352. */
  353. i = strlen(s);
  354. strncat(s, "directory-signature\n", maxlen-i);
  355. i = strlen(s);
  356. cp = s + i;
  357. if (router_get_dir_hash(s,digest)) {
  358. log_fn(LOG_WARN,"couldn't compute digest");
  359. return -1;
  360. }
  361. if (crypto_pk_private_sign(private_key, digest, 20, signature) < 0) {
  362. log_fn(LOG_WARN,"couldn't sign digest");
  363. return -1;
  364. }
  365. log(LOG_DEBUG,"generated directory digest begins with %02x:%02x:%02x:%02x",
  366. ((int)digest[0])&0xff,((int)digest[1])&0xff,
  367. ((int)digest[2])&0xff,((int)digest[3])&0xff);
  368. strncpy(cp, "-----BEGIN SIGNATURE-----\n", maxlen-i);
  369. cp[maxlen-i-1] = 0;
  370. i = strlen(s);
  371. cp = s+i;
  372. if (base64_encode(cp, maxlen-i, signature, 128) < 0) {
  373. log_fn(LOG_WARN,"couldn't base64-encode signature");
  374. return -1;
  375. }
  376. i = strlen(s);
  377. cp = s+i;
  378. strncat(cp, "-----END SIGNATURE-----\n", maxlen-i);
  379. i = strlen(s);
  380. if (i == maxlen) {
  381. log_fn(LOG_WARN,"tried to exceed string length.");
  382. return -1;
  383. }
  384. return 0;
  385. }
  386. static char *the_directory = NULL;
  387. static int the_directory_len = -1;
  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_set_routerlist_from_directory(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. }