dirvote.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641
  1. /* Copyright 2001-2004 Roger Dingledine.
  2. * Copyright 2004-2007 Roger Dingledine, Nick Mathewson. */
  3. /* See LICENSE for licensing information */
  4. /* $Id$ */
  5. const char dirvote_c_id[] =
  6. "$Id$";
  7. #define DIRVOTE_PRIVATE
  8. #include "or.h"
  9. /**
  10. * \file dirvote.c
  11. * \brief Functions to compute directory consensus, and schedule voting.
  12. **/
  13. static int dirvote_add_signatures_to_pending_consensus(
  14. const char *detached_signatures_body,
  15. const char **msg_out);
  16. static char *list_v3_auth_ids(void); //XXXX020 nuke this.
  17. /* =====
  18. * Voting and consensus generation
  19. * ===== */
  20. /** Clear all storage held in <b>ns</b>. */
  21. void
  22. networkstatus_vote_free(networkstatus_vote_t *ns)
  23. {
  24. if (!ns)
  25. return;
  26. tor_free(ns->client_versions);
  27. tor_free(ns->server_versions);
  28. if (ns->known_flags) {
  29. SMARTLIST_FOREACH(ns->known_flags, char *, c, tor_free(c));
  30. smartlist_free(ns->known_flags);
  31. }
  32. if (ns->voters) {
  33. SMARTLIST_FOREACH(ns->voters, networkstatus_voter_info_t *, voter,
  34. {
  35. tor_free(voter->nickname);
  36. tor_free(voter->address);
  37. tor_free(voter->contact);
  38. });
  39. smartlist_free(ns->voters);
  40. }
  41. if (ns->cert)
  42. authority_cert_free(ns->cert);
  43. if (ns->routerstatus_list) {
  44. if (ns->is_vote) {
  45. SMARTLIST_FOREACH(ns->routerstatus_list, vote_routerstatus_t *, rs,
  46. {
  47. tor_free(rs->version);
  48. tor_free(rs);
  49. });
  50. } else {
  51. SMARTLIST_FOREACH(ns->routerstatus_list, routerstatus_t *, rs,
  52. tor_free(rs));
  53. }
  54. smartlist_free(ns->routerstatus_list);
  55. }
  56. memset(ns, 11, sizeof(*ns));
  57. tor_free(ns);
  58. }
  59. /** Return the voter info from <b>vote</b> for the voter whose identity digest
  60. * is <b>identity</b>, or NULL if no such voter is associated with
  61. * <b>vote</b>. */
  62. networkstatus_voter_info_t *
  63. networkstatus_get_voter_by_id(networkstatus_vote_t *vote,
  64. const char *identity)
  65. {
  66. if (!vote || !vote->voters)
  67. return NULL;
  68. SMARTLIST_FOREACH(vote->voters, networkstatus_voter_info_t *, voter,
  69. if (!memcmp(voter->identity_digest, identity, DIGEST_LEN))
  70. return voter);
  71. return NULL;
  72. }
  73. /** Helper for sorting a list of time_t*. */
  74. static int
  75. _compare_times(const void **_a, const void **_b)
  76. {
  77. const time_t *a = *_a, *b = *_b;
  78. if (*a<*b)
  79. return -1;
  80. else if (*a>*b)
  81. return 1;
  82. else
  83. return 0;
  84. }
  85. /** Helper for sorting a list of int*. */
  86. static int
  87. _compare_ints(const void **_a, const void **_b)
  88. {
  89. const int *a = *_a, *b = *_b;
  90. if (*a<*b)
  91. return -1;
  92. else if (*a>*b)
  93. return 1;
  94. else
  95. return 0;
  96. }
  97. /** Given a list of one or more time_t*, return the (low) median. */
  98. /*static*/ time_t
  99. median_time(smartlist_t *times)
  100. {
  101. int idx;
  102. tor_assert(smartlist_len(times));
  103. smartlist_sort(times, _compare_times);
  104. idx = (smartlist_len(times)-1)/2;
  105. return *(time_t*)smartlist_get(times, idx);
  106. }
  107. /** Given a list of one or more int*, return the (low) median. */
  108. /*static*/ int
  109. median_int(smartlist_t *ints)
  110. {
  111. int idx;
  112. tor_assert(smartlist_len(ints));
  113. smartlist_sort(ints, _compare_ints);
  114. idx = (smartlist_len(ints)-1)/2;
  115. return *(int*)smartlist_get(ints, idx);
  116. }
  117. /** Given a vote <b>vote</b> (not a consensus!), return its associated
  118. * networkstatus_voter_info_t.*/
  119. static networkstatus_voter_info_t *
  120. get_voter(const networkstatus_vote_t *vote)
  121. {
  122. tor_assert(vote);
  123. tor_assert(vote->is_vote);
  124. tor_assert(vote->voters);
  125. tor_assert(smartlist_len(vote->voters) == 1);
  126. return smartlist_get(vote->voters, 0);
  127. }
  128. /** Helper for sorting networkstatus_vote_t votes (not consensuses) by the
  129. * hash of their voters' identity digests. */
  130. static int
  131. _compare_votes_by_authority_id(const void **_a, const void **_b)
  132. {
  133. const networkstatus_vote_t *a = *_a, *b = *_b;
  134. return memcmp(get_voter(a)->identity_digest,
  135. get_voter(b)->identity_digest, DIGEST_LEN);
  136. }
  137. /** Given a sorted list of strings <b>in</b>, add every member to <b>out</b>
  138. * that occurs more than <b>min</b> times. */
  139. static void
  140. get_frequent_members(smartlist_t *out, smartlist_t *in, int min)
  141. {
  142. char *cur = NULL;
  143. int count = 0;
  144. SMARTLIST_FOREACH(in, char *, cp,
  145. {
  146. if (cur && !strcmp(cp, cur)) {
  147. ++count;
  148. } else {
  149. if (count > min)
  150. smartlist_add(out, cur);
  151. cur = cp;
  152. count = 1;
  153. }
  154. });
  155. if (count > min)
  156. smartlist_add(out, cur);
  157. }
  158. /** Given a sorted list of strings <b>lst</b>, return the member that appears
  159. * most. Break ties in favor of later-occurring members. */
  160. static const char *
  161. get_most_frequent_member(smartlist_t *lst)
  162. {
  163. const char *most_frequent = NULL;
  164. int most_frequent_count = 0;
  165. const char *cur = NULL;
  166. int count = 0;
  167. SMARTLIST_FOREACH(lst, const char *, s,
  168. {
  169. if (cur && !strcmp(s, cur)) {
  170. ++count;
  171. } else {
  172. if (count >= most_frequent_count) {
  173. most_frequent = cur;
  174. most_frequent_count = count;
  175. }
  176. cur = s;
  177. count = 1;
  178. }
  179. });
  180. if (count >= most_frequent_count) {
  181. most_frequent = cur;
  182. most_frequent_count = count;
  183. }
  184. return most_frequent;
  185. }
  186. /** Return 0 if and only if <b>a</b> and <b>b</b> are routerstatuses
  187. * that come from the same routerinfo, with the same derived elements.
  188. */
  189. static int
  190. compare_vote_rs(const vote_routerstatus_t *a, const vote_routerstatus_t *b)
  191. {
  192. int r;
  193. if ((r = memcmp(a->status.identity_digest, b->status.identity_digest,
  194. DIGEST_LEN)))
  195. return r;
  196. if ((r = memcmp(a->status.descriptor_digest, b->status.descriptor_digest,
  197. DIGEST_LEN)))
  198. return r;
  199. if ((r = (b->status.published_on - a->status.published_on)))
  200. return r;
  201. if ((r = strcmp(b->status.nickname, a->status.nickname)))
  202. return r;
  203. if ((r = (((int)b->status.addr) - ((int)a->status.addr))))
  204. return r;
  205. if ((r = (((int)b->status.or_port) - ((int)a->status.or_port))))
  206. return r;
  207. if ((r = (((int)b->status.dir_port) - ((int)a->status.dir_port))))
  208. return r;
  209. return 0;
  210. }
  211. /** Helper for sorting routerlists based on compare_vote_rs. */
  212. static int
  213. _compare_vote_rs(const void **_a, const void **_b)
  214. {
  215. const vote_routerstatus_t *a = *_a, *b = *_b;
  216. return compare_vote_rs(a,b);
  217. }
  218. /** Given a list of vote_routerstatus_t, all for the same router identity,
  219. * return whichever is most frequent, breaking ties in favor of more
  220. * recently published vote_routerstatus_t.
  221. */
  222. static vote_routerstatus_t *
  223. compute_routerstatus_consensus(smartlist_t *votes)
  224. {
  225. vote_routerstatus_t *most = NULL, *cur = NULL;
  226. int most_n = 0, cur_n = 0;
  227. time_t most_published = 0;
  228. smartlist_sort(votes, _compare_vote_rs);
  229. SMARTLIST_FOREACH(votes, vote_routerstatus_t *, rs,
  230. {
  231. if (cur && !compare_vote_rs(cur, rs)) {
  232. ++cur_n;
  233. } else {
  234. if (cur_n > most_n ||
  235. (cur && cur_n == most_n &&
  236. cur->status.published_on > most_published)) {
  237. most = cur;
  238. most_n = cur_n;
  239. most_published = cur->status.published_on;
  240. }
  241. cur_n = 1;
  242. cur = rs;
  243. }
  244. });
  245. if (cur_n > most_n ||
  246. (cur && cur_n == most_n && cur->status.published_on > most_published)) {
  247. most = cur;
  248. most_n = cur_n;
  249. most_published = cur->status.published_on;
  250. }
  251. tor_assert(most);
  252. return most;
  253. }
  254. /** Given a list of strings in <b>lst</b>, set the DIGEST_LEN-byte digest at
  255. * <b>digest_out</b> to the hash of the concatenation of those strings. */
  256. static void
  257. hash_list_members(char *digest_out, smartlist_t *lst)
  258. {
  259. crypto_digest_env_t *d = crypto_new_digest_env();
  260. SMARTLIST_FOREACH(lst, const char *, cp,
  261. crypto_digest_add_bytes(d, cp, strlen(cp)));
  262. crypto_digest_get_digest(d, digest_out, DIGEST_LEN);
  263. crypto_free_digest_env(d);
  264. }
  265. /** Given a list of vote networkstatus_vote_t in <b>votes</b>, our public
  266. * authority <b>identity_key</b>, our private authority <b>signing_key</b>,
  267. * and the number of <b>total_authorities</b> that we believe exist in our
  268. * voting quorum, generate the text of a new v3 consensus vote, and return the
  269. * value in a newly allocated string.
  270. *
  271. * Note: this function DOES NOT check whether the votes are from
  272. * recognized authorities. (dirvote_add_vote does that.) */
  273. char *
  274. networkstatus_compute_consensus(smartlist_t *votes,
  275. int total_authorities,
  276. crypto_pk_env_t *identity_key,
  277. crypto_pk_env_t *signing_key)
  278. {
  279. smartlist_t *chunks;
  280. char *result = NULL;
  281. time_t valid_after, fresh_until, valid_until;
  282. int vote_seconds, dist_seconds;
  283. char *client_versions = NULL, *server_versions = NULL;
  284. smartlist_t *flags;
  285. tor_assert(total_authorities >= smartlist_len(votes));
  286. if (!smartlist_len(votes)) {
  287. log_warn(LD_DIR, "Can't compute a consensus from no votes.");
  288. return NULL;
  289. }
  290. flags = smartlist_create();
  291. /* Compute medians of time-related things, and figure out how many
  292. * routers we might need to talk about. */
  293. {
  294. smartlist_t *va_times = smartlist_create();
  295. smartlist_t *fu_times = smartlist_create();
  296. smartlist_t *vu_times = smartlist_create();
  297. smartlist_t *votesec_list = smartlist_create();
  298. smartlist_t *distsec_list = smartlist_create();
  299. int n_versioning_clients = 0, n_versioning_servers = 0;
  300. smartlist_t *combined_client_versions = smartlist_create();
  301. smartlist_t *combined_server_versions = smartlist_create();
  302. int j;
  303. SMARTLIST_FOREACH(votes, networkstatus_vote_t *, v,
  304. {
  305. tor_assert(v->is_vote);
  306. smartlist_add(va_times, &v->valid_after);
  307. smartlist_add(fu_times, &v->fresh_until);
  308. smartlist_add(vu_times, &v->valid_until);
  309. smartlist_add(votesec_list, &v->vote_seconds);
  310. smartlist_add(distsec_list, &v->dist_seconds);
  311. if (v->client_versions) {
  312. smartlist_t *cv = smartlist_create();
  313. ++n_versioning_clients;
  314. smartlist_split_string(cv, v->client_versions, ",",
  315. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  316. sort_version_list(cv, 1);
  317. smartlist_add_all(combined_client_versions, cv);
  318. smartlist_free(cv); /* elements get freed later. */
  319. }
  320. if (v->server_versions) {
  321. smartlist_t *sv = smartlist_create();
  322. ++n_versioning_servers;
  323. smartlist_split_string(sv, v->server_versions, ",",
  324. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  325. sort_version_list(sv, 1);
  326. smartlist_add_all(combined_server_versions, sv);
  327. smartlist_free(sv); /* elements get freed later. */
  328. }
  329. SMARTLIST_FOREACH(v->known_flags, const char *, cp,
  330. smartlist_add(flags, tor_strdup(cp)));
  331. });
  332. valid_after = median_time(va_times);
  333. fresh_until = median_time(fu_times);
  334. valid_until = median_time(vu_times);
  335. vote_seconds = median_int(votesec_list);
  336. dist_seconds = median_int(distsec_list);
  337. /*
  338. SMARTLIST_FOREACH(va_times, int*, i,
  339. printf("VA: %d\n", *i));
  340. SMARTLIST_FOREACH(fu_times, int*, i,
  341. printf("FU: %d\n", *i));
  342. printf("%d..%d\n", (int)valid_after, (int)valid_until);
  343. */
  344. tor_assert(valid_after+MIN_VOTE_INTERVAL <= fresh_until);
  345. tor_assert(fresh_until+MIN_VOTE_INTERVAL <= valid_until);
  346. tor_assert(vote_seconds >= MIN_VOTE_SECONDS);
  347. tor_assert(dist_seconds >= MIN_DIST_SECONDS);
  348. for (j = 0; j < 2; ++j) {
  349. smartlist_t *lst =
  350. j ? combined_server_versions : combined_client_versions;
  351. int min = (j ? n_versioning_servers : n_versioning_clients) / 2;
  352. smartlist_t *good = smartlist_create();
  353. char *res;
  354. sort_version_list(lst, 0);
  355. get_frequent_members(good, lst, min);
  356. res = smartlist_join_strings(good, ",", 0, NULL);
  357. if (j)
  358. server_versions = res;
  359. else
  360. client_versions = res;
  361. SMARTLIST_FOREACH(lst, char *, cp, tor_free(cp));
  362. smartlist_free(good);
  363. smartlist_free(lst);
  364. }
  365. smartlist_sort_strings(flags);
  366. smartlist_uniq_strings(flags);
  367. smartlist_free(va_times);
  368. smartlist_free(fu_times);
  369. smartlist_free(vu_times);
  370. smartlist_free(votesec_list);
  371. smartlist_free(distsec_list);
  372. }
  373. chunks = smartlist_create();
  374. {
  375. char buf[1024];
  376. char va_buf[ISO_TIME_LEN+1], fu_buf[ISO_TIME_LEN+1],
  377. vu_buf[ISO_TIME_LEN+1];
  378. char *flaglist;
  379. format_iso_time(va_buf, valid_after);
  380. format_iso_time(fu_buf, fresh_until);
  381. format_iso_time(vu_buf, valid_until);
  382. flaglist = smartlist_join_strings(flags, " ", 0, NULL);
  383. tor_snprintf(buf, sizeof(buf),
  384. "network-status-version 3\n"
  385. "vote-status consensus\n"
  386. "valid-after %s\n"
  387. "fresh-until %s\n"
  388. "valid-until %s\n"
  389. "voting-delay %d %d\n"
  390. "client-versions %s\n"
  391. "server-versions %s\n"
  392. "known-flags %s\n",
  393. va_buf, fu_buf, vu_buf,
  394. vote_seconds, dist_seconds,
  395. client_versions, server_versions, flaglist);
  396. smartlist_add(chunks, tor_strdup(buf));
  397. tor_free(flaglist);
  398. }
  399. /* Sort the votes. */
  400. smartlist_sort(votes, _compare_votes_by_authority_id);
  401. /* Add the authority sections. */
  402. SMARTLIST_FOREACH(votes, networkstatus_vote_t *, v,
  403. {
  404. char buf[1024];
  405. struct in_addr in;
  406. char ip[INET_NTOA_BUF_LEN];
  407. char fingerprint[HEX_DIGEST_LEN+1];
  408. char votedigest[HEX_DIGEST_LEN+1];
  409. networkstatus_voter_info_t *voter = get_voter(v);
  410. in.s_addr = htonl(voter->addr);
  411. tor_inet_ntoa(&in, ip, sizeof(ip));
  412. base16_encode(fingerprint, sizeof(fingerprint), voter->identity_digest,
  413. DIGEST_LEN);
  414. base16_encode(votedigest, sizeof(votedigest), voter->vote_digest,
  415. DIGEST_LEN);
  416. tor_snprintf(buf, sizeof(buf),
  417. "dir-source %s %s %s %s %d %d\n"
  418. "contact %s\n"
  419. "vote-digest %s\n",
  420. voter->nickname, fingerprint, voter->address, ip,
  421. voter->dir_port,
  422. voter->or_port,
  423. voter->contact,
  424. votedigest);
  425. smartlist_add(chunks, tor_strdup(buf));
  426. });
  427. /* Add the actual router entries. */
  428. {
  429. int *index; /* index[j] is the current index into votes[j]. */
  430. int *size; /* size[j] is the number of routerstatuses in votes[j]. */
  431. int *flag_counts; /* The number of voters that list flag[j] for the
  432. * currently considered router. */
  433. int i;
  434. smartlist_t *matching_descs = smartlist_create();
  435. smartlist_t *chosen_flags = smartlist_create();
  436. smartlist_t *versions = smartlist_create();
  437. int *n_voter_flags; /* n_voter_flags[j] is the number of flags that
  438. * votes[j] knows about. */
  439. int *n_flag_voters; /* n_flag_voters[f] is the number of votes that care
  440. * about flags[f]. */
  441. int **flag_map; /* flag_map[j][b] is an index f such that flag_map[f]
  442. * is the same flag as votes[j]->known_flags[b]. */
  443. int *named_flag; /* Index of the flag "Named" for votes[j] */
  444. index = tor_malloc_zero(sizeof(int)*smartlist_len(votes));
  445. size = tor_malloc_zero(sizeof(int)*smartlist_len(votes));
  446. n_voter_flags = tor_malloc_zero(sizeof(int) * smartlist_len(votes));
  447. n_flag_voters = tor_malloc_zero(sizeof(int) * smartlist_len(flags));
  448. flag_map = tor_malloc_zero(sizeof(int*) * smartlist_len(votes));
  449. named_flag = tor_malloc_zero(sizeof(int*) * smartlist_len(votes));
  450. for (i = 0; i < smartlist_len(votes); ++i)
  451. named_flag[i] = -1;
  452. SMARTLIST_FOREACH(votes, networkstatus_vote_t *, v,
  453. {
  454. flag_map[v_sl_idx] = tor_malloc_zero(
  455. sizeof(int)*smartlist_len(v->known_flags));
  456. SMARTLIST_FOREACH(v->known_flags, const char *, fl,
  457. {
  458. int p = smartlist_string_pos(flags, fl);
  459. tor_assert(p >= 0);
  460. flag_map[v_sl_idx][fl_sl_idx] = p;
  461. ++n_flag_voters[p];
  462. if (!strcmp(fl, "Named"))
  463. named_flag[v_sl_idx] = fl_sl_idx;
  464. });
  465. n_voter_flags[v_sl_idx] = smartlist_len(v->known_flags);
  466. size[v_sl_idx] = smartlist_len(v->routerstatus_list);
  467. });
  468. /* Now go through all the votes */
  469. flag_counts = tor_malloc(sizeof(int) * smartlist_len(flags));
  470. while (1) {
  471. vote_routerstatus_t *rs;
  472. routerstatus_t rs_out;
  473. const char *lowest_id = NULL;
  474. const char *chosen_version;
  475. const char *chosen_name = NULL;
  476. int naming_conflict = 0;
  477. int n_listing = 0;
  478. int i;
  479. char buf[256];
  480. /* Of the next-to-be-considered digest in each voter, which is first? */
  481. SMARTLIST_FOREACH(votes, networkstatus_vote_t *, v, {
  482. if (index[v_sl_idx] < size[v_sl_idx]) {
  483. rs = smartlist_get(v->routerstatus_list, index[v_sl_idx]);
  484. if (!lowest_id ||
  485. memcmp(rs->status.identity_digest, lowest_id, DIGEST_LEN) < 0)
  486. lowest_id = rs->status.identity_digest;
  487. }
  488. });
  489. if (!lowest_id) /* we're out of routers. */
  490. break;
  491. memset(flag_counts, 0, sizeof(int)*smartlist_len(flags));
  492. smartlist_clear(matching_descs);
  493. smartlist_clear(chosen_flags);
  494. smartlist_clear(versions);
  495. /* Okay, go through all the entries for this digest. */
  496. SMARTLIST_FOREACH(votes, networkstatus_vote_t *, v, {
  497. if (index[v_sl_idx] >= size[v_sl_idx])
  498. continue; /* out of entries. */
  499. rs = smartlist_get(v->routerstatus_list, index[v_sl_idx]);
  500. if (memcmp(rs->status.identity_digest, lowest_id, DIGEST_LEN))
  501. continue; /* doesn't include this router. */
  502. /* At this point, we know that we're looking at a routersatus with
  503. * identity "lowest".
  504. */
  505. ++index[v_sl_idx];
  506. ++n_listing;
  507. smartlist_add(matching_descs, rs);
  508. if (rs->version && rs->version[0])
  509. smartlist_add(versions, rs->version);
  510. /* Tally up all the flags. */
  511. for (i = 0; i < n_voter_flags[v_sl_idx]; ++i) {
  512. if (rs->flags & (U64_LITERAL(1) << i))
  513. ++flag_counts[flag_map[v_sl_idx][i]];
  514. }
  515. if (rs->flags & (U64_LITERAL(1) << named_flag[v_sl_idx])) {
  516. if (chosen_name && strcmp(chosen_name, rs->status.nickname)) {
  517. log_notice(LD_DIR, "Conflict on naming for router: %s vs %s",
  518. chosen_name, rs->status.nickname);
  519. naming_conflict = 1;
  520. }
  521. chosen_name = rs->status.nickname;
  522. }
  523. });
  524. /* We don't include this router at all unless more than half of
  525. * the authorities we believe in list it. */
  526. if (n_listing <= total_authorities/2)
  527. continue;
  528. /* Figure out the most popular opinion of what the most recent
  529. * routerinfo and its contents are. */
  530. rs = compute_routerstatus_consensus(matching_descs);
  531. /* Copy bits of that into rs_out. */
  532. tor_assert(!memcmp(lowest_id, rs->status.identity_digest, DIGEST_LEN));
  533. memcpy(rs_out.identity_digest, lowest_id, DIGEST_LEN);
  534. memcpy(rs_out.descriptor_digest, rs->status.descriptor_digest,
  535. DIGEST_LEN);
  536. rs_out.addr = rs->status.addr;
  537. rs_out.published_on = rs->status.published_on;
  538. rs_out.dir_port = rs->status.dir_port;
  539. rs_out.or_port = rs->status.or_port;
  540. if (chosen_name && !naming_conflict) {
  541. strlcpy(rs_out.nickname, chosen_name, sizeof(rs_out.nickname));
  542. } else {
  543. strlcpy(rs_out.nickname, rs->status.nickname, sizeof(rs_out.nickname));
  544. }
  545. /* Set the flags. */
  546. smartlist_add(chosen_flags, (char*)"s"); /* for the start of the line. */
  547. SMARTLIST_FOREACH(flags, const char *, fl,
  548. {
  549. if (strcmp(fl, "Named")) {
  550. if (flag_counts[fl_sl_idx] > n_flag_voters[fl_sl_idx]/2)
  551. smartlist_add(chosen_flags, (char*)fl);
  552. } else {
  553. if (!naming_conflict && flag_counts[fl_sl_idx])
  554. smartlist_add(chosen_flags, (char*)"Named");
  555. }
  556. });
  557. /* Pick the version. */
  558. if (smartlist_len(versions)) {
  559. sort_version_list(versions, 0);
  560. chosen_version = get_most_frequent_member(versions);
  561. } else {
  562. chosen_version = NULL;
  563. }
  564. /* Okay!! Now we can write the descriptor... */
  565. /* First line goes into "buf". */
  566. routerstatus_format_entry(buf, sizeof(buf), &rs_out, NULL, 1);
  567. smartlist_add(chunks, tor_strdup(buf));
  568. /* Second line is all flags. The "\n" is missing. */
  569. smartlist_add(chunks,
  570. smartlist_join_strings(chosen_flags, " ", 0, NULL));
  571. /* Now the version line. */
  572. if (chosen_version) {
  573. smartlist_add(chunks, tor_strdup("\nv "));
  574. smartlist_add(chunks, tor_strdup(chosen_version));
  575. }
  576. smartlist_add(chunks, tor_strdup("\n"));
  577. /* And the loop is over and we move on to the next router */
  578. }
  579. tor_free(index);
  580. tor_free(size);
  581. tor_free(n_voter_flags);
  582. tor_free(n_flag_voters);
  583. for (i = 0; i < smartlist_len(votes); ++i)
  584. tor_free(flag_map[i]);
  585. tor_free(flag_map);
  586. tor_free(flag_counts);
  587. smartlist_free(matching_descs);
  588. smartlist_free(chosen_flags);
  589. smartlist_free(versions);
  590. }
  591. /* Add a signature. */
  592. {
  593. char digest[DIGEST_LEN];
  594. char fingerprint[HEX_DIGEST_LEN+1];
  595. char signing_key_fingerprint[HEX_DIGEST_LEN+1];
  596. char buf[4096];
  597. smartlist_add(chunks, tor_strdup("directory-signature "));
  598. /* Compute the hash of the chunks. */
  599. hash_list_members(digest, chunks);
  600. /* Get the fingerprints */
  601. crypto_pk_get_fingerprint(identity_key, fingerprint, 0);
  602. crypto_pk_get_fingerprint(signing_key, signing_key_fingerprint, 0);
  603. /* add the junk that will go at the end of the line. */
  604. tor_snprintf(buf, sizeof(buf), "%s %s\n", fingerprint,
  605. signing_key_fingerprint);
  606. /* And the signature. */
  607. if (router_append_dirobj_signature(buf, sizeof(buf), digest,
  608. signing_key)) {
  609. log_warn(LD_BUG, "Couldn't sign consensus networkstatus.");
  610. return NULL; /* This leaks, but it should never happen. */
  611. }
  612. smartlist_add(chunks, tor_strdup(buf));
  613. }
  614. result = smartlist_join_strings(chunks, "", 0, NULL);
  615. tor_free(client_versions);
  616. tor_free(server_versions);
  617. smartlist_free(flags);
  618. SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
  619. smartlist_free(chunks);
  620. {
  621. networkstatus_vote_t *c;
  622. if (!(c = networkstatus_parse_vote_from_string(result, 0))) {
  623. log_err(LD_BUG,"Generated a networkstatus consensus we couldn't "
  624. "parse.");
  625. tor_free(result);
  626. return NULL;
  627. }
  628. networkstatus_vote_free(c);
  629. }
  630. return result;
  631. }
  632. /** Check whether the signature on <b>voter</b> is correctly signed by
  633. * the signing key of <b>cert</b>. Return -1 if <b>cert</b> doesn't match the
  634. * signing key; otherwise set the good_signature or bad_signature flag on
  635. * <b>voter</b>, and return 0. */
  636. /* (private; exposed for testing.) */
  637. int
  638. networkstatus_check_voter_signature(networkstatus_vote_t *consensus,
  639. networkstatus_voter_info_t *voter,
  640. authority_cert_t *cert)
  641. {
  642. char d[DIGEST_LEN];
  643. char *signed_digest;
  644. size_t signed_digest_len;
  645. if (crypto_pk_get_digest(cert->signing_key, d)<0)
  646. return -1;
  647. if (memcmp(voter->signing_key_digest, d, DIGEST_LEN))
  648. return -1;
  649. signed_digest_len = crypto_pk_keysize(cert->signing_key);
  650. signed_digest = tor_malloc(signed_digest_len);
  651. if (crypto_pk_public_checksig(cert->signing_key,
  652. signed_digest,
  653. voter->signature,
  654. voter->signature_len) != DIGEST_LEN ||
  655. memcmp(signed_digest, consensus->networkstatus_digest, DIGEST_LEN)) {
  656. log_warn(LD_DIR, "Got a bad signature on a networkstatus vote");
  657. voter->bad_signature = 1;
  658. } else {
  659. voter->good_signature = 1;
  660. }
  661. return 0;
  662. }
  663. /** DOCDOC */
  664. int
  665. networkstatus_check_consensus_signature(networkstatus_vote_t *consensus)
  666. {
  667. int n_good = 0;
  668. int n_missing_key = 0;
  669. int n_bad = 0;
  670. int n_unknown = 0;
  671. int n_no_signature = 0;
  672. int n_required = get_n_authorities(V3_AUTHORITY)/2 + 1;
  673. tor_assert(! consensus->is_vote);
  674. SMARTLIST_FOREACH(consensus->voters, networkstatus_voter_info_t *, voter,
  675. {
  676. if (!voter->good_signature && !voter->bad_signature && voter->signature) {
  677. /* we can try to check the signature. */
  678. authority_cert_t *cert =
  679. authority_cert_get_by_digests(voter->identity_digest,
  680. voter->signing_key_digest);
  681. if (! cert) {
  682. ++n_unknown;
  683. continue;
  684. }
  685. if (networkstatus_check_voter_signature(consensus, voter, cert) < 0) {
  686. ++n_missing_key;
  687. continue;
  688. }
  689. }
  690. if (voter->good_signature)
  691. ++n_good;
  692. else if (voter->bad_signature)
  693. ++n_bad;
  694. else
  695. ++n_no_signature;
  696. });
  697. log_notice(LD_DIR,
  698. "%d unknown, %d missing key, %d good, %d bad, %d no signature, "
  699. "%d required", n_unknown, n_missing_key, n_good, n_bad,
  700. n_no_signature, n_required);
  701. if (n_good >= n_required)
  702. return 0;
  703. else
  704. return -1;
  705. }
  706. /** DOCDOC */
  707. static int
  708. networkstatus_add_signatures_impl(networkstatus_vote_t *target,
  709. smartlist_t *src_voter_list,
  710. char **new_signatures_out,
  711. int *regenerate_out)
  712. {
  713. smartlist_t *added_signatures, *sigs;
  714. int r;
  715. tor_assert(target);
  716. tor_assert(!target->is_vote);
  717. tor_assert(new_signatures_out);
  718. tor_assert(regenerate_out);
  719. added_signatures = smartlist_create();
  720. /* For each voter in src... */
  721. SMARTLIST_FOREACH(src_voter_list, networkstatus_voter_info_t *, src_voter,
  722. {
  723. networkstatus_voter_info_t *target_voter =
  724. networkstatus_get_voter_by_id(target, src_voter->identity_digest);
  725. authority_cert_t *cert;
  726. /* If the target a doesn't know about this voter, then forget it. */
  727. if (!target_voter)
  728. continue;
  729. /* If the target already has a good signature from this voter, then skip
  730. * this one. */
  731. if (target_voter->good_signature)
  732. continue;
  733. /* Try checking the signature if we haven't already. */
  734. if (!src_voter->good_signature && !src_voter->bad_signature) {
  735. cert = authority_cert_get_by_digests(src_voter->identity_digest,
  736. src_voter->signing_key_digest);
  737. if (cert) {
  738. networkstatus_check_voter_signature(target, src_voter, cert);
  739. }
  740. }
  741. /* If this signature is good, or we don't have ay signature yet,
  742. * then add it. */
  743. if (src_voter->good_signature || !target_voter->signature) {
  744. if (target_voter->signature)
  745. *regenerate_out = 1;
  746. tor_free(target_voter->signature);
  747. target_voter->signature =
  748. tor_memdup(src_voter->signature, src_voter->signature_len);
  749. memcpy(target_voter->signing_key_digest, src_voter->signing_key_digest,
  750. DIGEST_LEN);
  751. target_voter->signature_len = src_voter->signature_len;
  752. target_voter->good_signature = 1;
  753. target_voter->bad_signature = 0;
  754. smartlist_add(added_signatures, target_voter);
  755. }
  756. });
  757. sigs = smartlist_create();
  758. SMARTLIST_FOREACH(added_signatures, networkstatus_voter_info_t *, v,
  759. {
  760. char buf[4096];
  761. char sk[HEX_DIGEST_LEN+1];
  762. char ik[HEX_DIGEST_LEN+1];
  763. tor_assert(v->signature);
  764. base16_encode(sk, sizeof(sk), v->signing_key_digest, DIGEST_LEN);
  765. base16_encode(ik, sizeof(ik), v->identity_digest, DIGEST_LEN);
  766. tor_snprintf(buf, sizeof(buf), "directory-signature %s %s\n"
  767. "-----BEGIN SIGNATURE-----\n", ik, sk);
  768. smartlist_add(sigs, tor_strdup(buf));
  769. base64_encode(buf, sizeof(buf), v->signature, v->signature_len);
  770. strlcat(buf, "-----END SIGNATURE-----\n", sizeof(buf));
  771. smartlist_add(sigs, tor_strdup(buf));
  772. });
  773. *new_signatures_out = smartlist_join_strings(sigs, "", 0, NULL);
  774. SMARTLIST_FOREACH(sigs, char *, cp, tor_free(cp));
  775. smartlist_free(sigs);
  776. r = smartlist_len(added_signatures);
  777. smartlist_free(added_signatures);
  778. return r;
  779. }
  780. /** DOCDOC */
  781. int
  782. networkstatus_add_consensus_signatures(networkstatus_vote_t *target,
  783. networkstatus_vote_t *src,
  784. char **new_signatures_out,
  785. int *regenerate_out)
  786. {
  787. tor_assert(src);
  788. tor_assert(! src->is_vote);
  789. *new_signatures_out = NULL;
  790. /* Are they the same consensus? */
  791. if (memcmp(target->networkstatus_digest, src->networkstatus_digest,
  792. DIGEST_LEN))
  793. return -1;
  794. if (target == src)
  795. return 0;
  796. return networkstatus_add_signatures_impl(target, src->voters,
  797. new_signatures_out,
  798. regenerate_out);
  799. }
  800. /** DOCDOC */
  801. int
  802. networkstatus_add_detached_signatures(networkstatus_vote_t *target,
  803. ns_detached_signatures_t *sigs,
  804. char **new_signatures_out,
  805. int *regenerate_out)
  806. {
  807. tor_assert(sigs);
  808. *new_signatures_out = NULL;
  809. /* Are they the same consensus? */
  810. if (memcmp(target->networkstatus_digest, sigs->networkstatus_digest,
  811. DIGEST_LEN))
  812. return -1;
  813. return networkstatus_add_signatures_impl(target, sigs->signatures,
  814. new_signatures_out,
  815. regenerate_out);
  816. }
  817. /** DOCDOC */
  818. char *
  819. networkstatus_get_detached_signatures(networkstatus_vote_t *consensus)
  820. {
  821. smartlist_t *elements;
  822. char buf[4096];
  823. char *result = NULL;
  824. int n_sigs = 0;
  825. tor_assert(consensus);
  826. tor_assert(! consensus->is_vote);
  827. elements = smartlist_create();
  828. {
  829. char va_buf[ISO_TIME_LEN+1], fu_buf[ISO_TIME_LEN+1],
  830. vu_buf[ISO_TIME_LEN+1];
  831. char d[HEX_DIGEST_LEN+1];
  832. base16_encode(d, sizeof(d), consensus->networkstatus_digest, DIGEST_LEN);
  833. format_iso_time(va_buf, consensus->valid_after);
  834. format_iso_time(fu_buf, consensus->fresh_until);
  835. format_iso_time(vu_buf, consensus->valid_until);
  836. tor_snprintf(buf, sizeof(buf),
  837. "consensus-digest %s\n"
  838. "valid-after %s\n"
  839. "fresh-until %s\n"
  840. "valid-until %s\n", d, va_buf, fu_buf, vu_buf);
  841. smartlist_add(elements, tor_strdup(buf));
  842. }
  843. SMARTLIST_FOREACH(consensus->voters, networkstatus_voter_info_t *, v,
  844. {
  845. char sk[HEX_DIGEST_LEN+1];
  846. char id[HEX_DIGEST_LEN+1];
  847. if (!v->signature || v->bad_signature)
  848. continue;
  849. ++n_sigs;
  850. base16_encode(sk, sizeof(sk), v->signing_key_digest, DIGEST_LEN);
  851. base16_encode(id, sizeof(id), v->identity_digest, DIGEST_LEN);
  852. tor_snprintf(buf, sizeof(buf),
  853. "directory-signature %s %s\n-----BEGIN SIGNATURE-----\n",
  854. id, sk);
  855. smartlist_add(elements, tor_strdup(buf));
  856. base64_encode(buf, sizeof(buf), v->signature, v->signature_len);
  857. strlcat(buf, "-----END SIGNATURE-----\n", sizeof(buf));
  858. smartlist_add(elements, tor_strdup(buf));
  859. });
  860. result = smartlist_join_strings(elements, "", 0, NULL);
  861. SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
  862. smartlist_free(elements);
  863. if (!n_sigs)
  864. tor_free(result);
  865. return result;
  866. }
  867. /** DOCDOC */
  868. void
  869. ns_detached_signatures_free(ns_detached_signatures_t *s)
  870. {
  871. if (s->signatures) {
  872. SMARTLIST_FOREACH(s->signatures, networkstatus_voter_info_t *, v,
  873. {
  874. tor_free(v->signature);
  875. tor_free(v);
  876. });
  877. smartlist_free(s->signatures);
  878. }
  879. tor_free(s);
  880. }
  881. /* =====
  882. * Certificate functions
  883. * ===== */
  884. /** Free storage held in <b>cert</b>. */
  885. void
  886. authority_cert_free(authority_cert_t *cert)
  887. {
  888. if (!cert)
  889. return;
  890. tor_free(cert->cache_info.signed_descriptor_body);
  891. if (cert->signing_key)
  892. crypto_free_pk_env(cert->signing_key);
  893. if (cert->identity_key)
  894. crypto_free_pk_env(cert->identity_key);
  895. tor_free(cert);
  896. }
  897. /** Allocate and return a new authority_cert_t with the same contents as
  898. * <b>cert</b>. */
  899. authority_cert_t *
  900. authority_cert_dup(authority_cert_t *cert)
  901. {
  902. authority_cert_t *out = tor_malloc(sizeof(authority_cert_t));
  903. tor_assert(cert);
  904. memcpy(out, cert, sizeof(authority_cert_t));
  905. /* Now copy pointed-to things. */
  906. out->cache_info.signed_descriptor_body =
  907. tor_strndup(cert->cache_info.signed_descriptor_body,
  908. cert->cache_info.signed_descriptor_len);
  909. out->cache_info.saved_location = SAVED_NOWHERE;
  910. out->identity_key = crypto_pk_dup_key(cert->identity_key);
  911. out->signing_key = crypto_pk_dup_key(cert->signing_key);
  912. return out;
  913. }
  914. /* =====
  915. * Vote scheduling
  916. * ===== */
  917. /** DOCDOC */
  918. void
  919. dirvote_get_preferred_voting_intervals(vote_timing_t *timing_out)
  920. {
  921. or_options_t *options = get_options();
  922. tor_assert(timing_out);
  923. timing_out->vote_interval = options->V3AuthVotingInterval;
  924. timing_out->n_intervals_valid = options->V3AuthNIntervalsValid;
  925. timing_out->vote_delay = options->V3AuthVoteDelay;
  926. timing_out->dist_delay = options->V3AuthDistDelay;
  927. }
  928. /** DOCDOC */
  929. time_t
  930. dirvote_get_start_of_next_interval(time_t now, int interval)
  931. {
  932. struct tm tm;
  933. time_t midnight_today;
  934. time_t midnight_tomorrow;
  935. time_t next;
  936. tor_gmtime_r(&now, &tm);
  937. tm.tm_hour = 0;
  938. tm.tm_min = 0;
  939. tm.tm_sec = 0;
  940. midnight_today = tor_timegm(&tm);
  941. midnight_tomorrow = midnight_today + (24*60*60);
  942. next = midnight_today + ((now-midnight_today)/interval + 1)*interval;
  943. /* Intervals never cross midnight. */
  944. if (next > midnight_tomorrow)
  945. next = midnight_tomorrow;
  946. /* If the interval would only last half as long as it's supposed to, then
  947. * skip over to the next day. */
  948. if (next + interval/2 > midnight_tomorrow)
  949. next = midnight_tomorrow;
  950. return next;
  951. }
  952. /** DOCDOC */
  953. static struct {
  954. time_t voting_starts;
  955. time_t voting_ends;
  956. time_t interval_starts;
  957. time_t discard_old_votes;
  958. int have_voted;
  959. int have_built_consensus;
  960. int have_published_consensus;
  961. } voting_schedule = {0,0,0,0,0,0,0};
  962. /** DOCDOC */
  963. void
  964. dirvote_recalculate_timing(time_t now)
  965. {
  966. /* XXXX020 call this when inputs may have changed (i.e., whenver we get a
  967. * fresh consensus.) */
  968. int interval, vote_delay, dist_delay;
  969. time_t start;
  970. time_t end;
  971. networkstatus_vote_t *consensus = networkstatus_get_latest_consensus();
  972. memset(&voting_schedule, 0, sizeof(voting_schedule));
  973. if (consensus) {
  974. interval = consensus->fresh_until - consensus->valid_after;
  975. vote_delay = consensus->vote_seconds;
  976. dist_delay = consensus->dist_seconds;
  977. } else {
  978. /* XXXX020 drop this back down to 30 minutes. */
  979. interval = 1200;
  980. vote_delay = dist_delay = 300;
  981. }
  982. tor_assert(interval > 0);
  983. if (vote_delay + dist_delay > interval/2)
  984. vote_delay = dist_delay = interval / 4;
  985. start = voting_schedule.interval_starts =
  986. dirvote_get_start_of_next_interval(now,interval);
  987. end = dirvote_get_start_of_next_interval(start+1, interval);
  988. tor_assert(end > start);
  989. voting_schedule.voting_ends = start - vote_delay;
  990. voting_schedule.voting_starts = start - vote_delay - dist_delay;
  991. voting_schedule.discard_old_votes = start +
  992. ((end-start) - vote_delay - dist_delay)/2 ;
  993. }
  994. /** DOCDOC */
  995. void
  996. dirvote_act(time_t now)
  997. {
  998. if (!voting_schedule.voting_starts) {
  999. char *keys = list_v3_auth_ids();
  1000. authority_cert_t *c = get_my_v3_authority_cert();
  1001. log_notice(LD_DIR, "Scheduling voting. Known authority IDs are %s."
  1002. "Mine is %s.",
  1003. keys, hex_str(c->cache_info.identity_digest, DIGEST_LEN));
  1004. tor_free(keys);
  1005. dirvote_recalculate_timing(now);
  1006. }
  1007. if (voting_schedule.voting_starts < now && !voting_schedule.have_voted) {
  1008. log_notice(LD_DIR, "Time to vote.");
  1009. dirvote_perform_vote();
  1010. voting_schedule.have_voted = 1;
  1011. }
  1012. /* XXXX020 after a couple minutes here, start trying to fetch votes. */
  1013. if (voting_schedule.voting_ends < now &&
  1014. !voting_schedule.have_built_consensus) {
  1015. log_notice(LD_DIR, "Time to compute a consensus.");
  1016. dirvote_compute_consensus();
  1017. /* XXXX020 we will want to try again later if we haven't got enough
  1018. * votes yet. */
  1019. voting_schedule.have_built_consensus = 1;
  1020. }
  1021. if (voting_schedule.interval_starts < now &&
  1022. !voting_schedule.have_published_consensus) {
  1023. log_notice(LD_DIR, "Time to publish the consensus.");
  1024. dirvote_publish_consensus();
  1025. /* XXXX020 we will want to try again later if we haven't got enough
  1026. * signatures yet. */
  1027. voting_schedule.have_published_consensus = 1;
  1028. }
  1029. if (voting_schedule.discard_old_votes < now) {
  1030. log_notice(LD_DIR, "Time to discard old votes.");
  1031. dirvote_clear_pending_votes();
  1032. dirvote_recalculate_timing(now);
  1033. }
  1034. }
  1035. /** DOCDOC */
  1036. typedef struct pending_vote_t {
  1037. cached_dir_t *vote_body;
  1038. networkstatus_vote_t *vote;
  1039. } pending_vote_t;
  1040. /** DOCDOC */
  1041. static smartlist_t *pending_vote_list = NULL;
  1042. /** DOCDOC */
  1043. static char *pending_consensus_body = NULL;
  1044. /** DOCDOC */
  1045. static char *pending_consensus_signatures = NULL;
  1046. /** DOCDOC */
  1047. static networkstatus_vote_t *pending_consensus = NULL;
  1048. /** DOCDOC */
  1049. static smartlist_t *pending_consensus_signature_list = NULL;
  1050. /** DOCDOC */
  1051. void
  1052. dirvote_perform_vote(void)
  1053. {
  1054. cached_dir_t *new_vote = generate_v3_networkstatus();
  1055. pending_vote_t *pending_vote;
  1056. int status;
  1057. const char *msg = "";
  1058. if (!new_vote)
  1059. return;
  1060. if (!(pending_vote = dirvote_add_vote(new_vote->dir, &msg, &status))) {
  1061. log_warn(LD_DIR, "Couldn't store my own vote! (I told myself, '%s'.)",
  1062. msg);
  1063. return;
  1064. }
  1065. directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_VOTE,
  1066. ROUTER_PURPOSE_GENERAL,
  1067. V3_AUTHORITY,
  1068. pending_vote->vote_body->dir,
  1069. pending_vote->vote_body->dir_len, 0);
  1070. log_notice(LD_DIR, "Vote posted.");
  1071. }
  1072. /** DOCDOC */
  1073. void
  1074. dirvote_clear_pending_votes(void)
  1075. {
  1076. if (pending_vote_list) {
  1077. SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, v, {
  1078. cached_dir_decref(v->vote_body);
  1079. v->vote_body = NULL;
  1080. networkstatus_vote_free(v->vote);
  1081. tor_free(v);
  1082. });
  1083. smartlist_clear(pending_vote_list);
  1084. }
  1085. if (pending_consensus_signature_list) {
  1086. SMARTLIST_FOREACH(pending_consensus_signature_list, char *, cp,
  1087. tor_free(cp));
  1088. smartlist_clear(pending_consensus_signature_list);
  1089. }
  1090. tor_free(pending_consensus_body);
  1091. tor_free(pending_consensus_signatures);
  1092. if (pending_consensus) {
  1093. networkstatus_vote_free(pending_consensus);
  1094. pending_consensus = NULL;
  1095. }
  1096. }
  1097. /* XXXX020 delete me. */
  1098. static char *
  1099. list_v3_auth_ids(void)
  1100. {
  1101. smartlist_t *known_v3_keys = smartlist_create();
  1102. char *keys;
  1103. SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
  1104. trusted_dir_server_t *, ds,
  1105. if (!tor_digest_is_zero(ds->v3_identity_digest))
  1106. smartlist_add(known_v3_keys,
  1107. tor_strdup(hex_str(ds->v3_identity_digest, DIGEST_LEN))));
  1108. keys = smartlist_join_strings(known_v3_keys, ", ", 0, NULL);
  1109. SMARTLIST_FOREACH(known_v3_keys, char *, cp, tor_free(cp));
  1110. smartlist_free(known_v3_keys);
  1111. return keys;
  1112. }
  1113. /** DOCDOC */
  1114. pending_vote_t *
  1115. dirvote_add_vote(const char *vote_body, const char **msg_out, int *status_out)
  1116. {
  1117. networkstatus_vote_t *vote;
  1118. networkstatus_voter_info_t *vi;
  1119. trusted_dir_server_t *ds;
  1120. pending_vote_t *pending_vote = NULL;
  1121. tor_assert(vote_body);
  1122. tor_assert(msg_out);
  1123. tor_assert(status_out);
  1124. *status_out = 0;
  1125. if (!pending_vote_list)
  1126. pending_vote_list = smartlist_create();
  1127. *msg_out = NULL;
  1128. vote = networkstatus_parse_vote_from_string(vote_body, 1);
  1129. if (!vote) {
  1130. *msg_out = "Unable to parse vote";
  1131. goto err;
  1132. }
  1133. tor_assert(smartlist_len(vote->voters) == 1);
  1134. vi = get_voter(vote);
  1135. tor_assert(vi->good_signature == 1);
  1136. ds = trusteddirserver_get_by_v3_auth_digest(vi->identity_digest);
  1137. if (!ds || !(ds->type & V3_AUTHORITY)) {
  1138. char *keys = list_v3_auth_ids();
  1139. log_warn(LD_DIR, "Got a vote from an authority with authority key ID %s. "
  1140. "This authority %s. Known v3 key IDs are: %s",
  1141. hex_str(vi->identity_digest, DIGEST_LEN),
  1142. ds?"is not recognized":"is recognized, but is not listed as v3",
  1143. keys);
  1144. tor_free(keys);
  1145. *msg_out = "Vote not from a recognized v3 authority";
  1146. goto err;
  1147. }
  1148. tor_assert(vote->cert);
  1149. if (!authority_cert_get_by_digests(vote->cert->cache_info.identity_digest,
  1150. vote->cert->signing_key_digest)) {
  1151. /* Hey, it's a new cert! */
  1152. trusted_dirs_load_certs_from_string(
  1153. vote->cert->cache_info.signed_descriptor_body,
  1154. 0 /* from_store */);
  1155. if (!authority_cert_get_by_digests(vote->cert->cache_info.identity_digest,
  1156. vote->cert->signing_key_digest)) {
  1157. log_warn(LD_BUG, "We added a cert, but still couldn't find it.");
  1158. }
  1159. }
  1160. /* XXXX020 check times; make sure epochs match. */
  1161. SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, v, {
  1162. if (! memcmp(v->vote->cert->cache_info.identity_digest,
  1163. vote->cert->cache_info.identity_digest,
  1164. DIGEST_LEN)) {
  1165. networkstatus_voter_info_t *vi_old = get_voter(v->vote);
  1166. if (!memcmp(vi_old->vote_digest, vi->vote_digest, DIGEST_LEN)) {
  1167. /* Ah, it's the same vote. Not a problem. */
  1168. log_info(LD_DIR, "Discarding a vote we already have.");
  1169. *status_out = 200;
  1170. *msg_out = "ok";
  1171. goto err;
  1172. } else if (v->vote->published < vote->published) {
  1173. log_notice(LD_DIR, "Replacing an older pending vote from this "
  1174. "directory.");
  1175. cached_dir_decref(v->vote_body);
  1176. networkstatus_vote_free(v->vote);
  1177. v->vote_body = new_cached_dir(tor_strdup(vote_body),
  1178. vote->published);
  1179. v->vote = vote;
  1180. *msg_out = "ok";
  1181. return v;
  1182. } else {
  1183. *msg_out = "Already have a newer pending vote";
  1184. goto err;
  1185. }
  1186. }
  1187. });
  1188. pending_vote = tor_malloc_zero(sizeof(pending_vote_t));
  1189. pending_vote->vote_body = new_cached_dir(tor_strdup(vote_body),
  1190. vote->published);
  1191. pending_vote->vote = vote;
  1192. smartlist_add(pending_vote_list, pending_vote);
  1193. if (!*status_out)
  1194. *status_out = 200;
  1195. *msg_out = "ok";
  1196. return pending_vote;
  1197. err:
  1198. if (vote)
  1199. networkstatus_vote_free(vote);
  1200. if (!*msg_out)
  1201. *msg_out = "Error adding vote";
  1202. if (!*status_out)
  1203. *status_out = 400;
  1204. return NULL;
  1205. }
  1206. /** DOCDOC */
  1207. int
  1208. dirvote_compute_consensus(void)
  1209. {
  1210. /* Have we got enough votes to try? */
  1211. int n_votes, n_voters;
  1212. smartlist_t *votes = NULL;
  1213. char *consensus_body = NULL, *signatures = NULL;
  1214. networkstatus_vote_t *consensus = NULL;
  1215. authority_cert_t *my_cert;
  1216. if (!pending_vote_list)
  1217. pending_vote_list = smartlist_create();
  1218. n_voters = get_n_authorities(V3_AUTHORITY);
  1219. n_votes = smartlist_len(pending_vote_list);
  1220. if (n_votes <= n_voters/2) {
  1221. log_warn(LD_DIR, "We don't have enough votes to generate a consensus.");
  1222. goto err;
  1223. }
  1224. if (!(my_cert = get_my_v3_authority_cert())) {
  1225. log_warn(LD_DIR, "Can't generate consensus without a certificate.");
  1226. goto err;
  1227. }
  1228. votes = smartlist_create();
  1229. SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, v,
  1230. smartlist_add(votes, v->vote));
  1231. consensus_body = networkstatus_compute_consensus(
  1232. votes, n_voters,
  1233. my_cert->identity_key,
  1234. get_my_v3_authority_signing_key());
  1235. if (!consensus_body) {
  1236. log_warn(LD_DIR, "Couldn't generate a consensus at all!");
  1237. goto err;
  1238. }
  1239. consensus = networkstatus_parse_vote_from_string(consensus_body, 0);
  1240. if (!consensus) {
  1241. log_warn(LD_DIR, "Couldn't parse consensus we generated!");
  1242. goto err;
  1243. }
  1244. /* 'Check' our own signature, to mark it valid. */
  1245. networkstatus_check_consensus_signature(consensus);
  1246. signatures = networkstatus_get_detached_signatures(consensus);
  1247. if (!signatures) {
  1248. log_warn(LD_DIR, "Couldn't extract signatures.");
  1249. goto err;
  1250. }
  1251. tor_free(pending_consensus_body);
  1252. pending_consensus_body = consensus_body;
  1253. tor_free(pending_consensus_signatures);
  1254. pending_consensus_signatures = signatures;
  1255. if (pending_consensus)
  1256. networkstatus_vote_free(pending_consensus);
  1257. pending_consensus = consensus;
  1258. if (pending_consensus_signature_list) {
  1259. int n_sigs = 0;
  1260. /* we may have gotten signatures for this consensus before we built
  1261. * it ourself. Add them now. */
  1262. SMARTLIST_FOREACH(pending_consensus_signature_list, char *, sig,
  1263. {
  1264. const char *msg = NULL;
  1265. n_sigs += dirvote_add_signatures_to_pending_consensus(sig, &msg);
  1266. tor_free(sig);
  1267. });
  1268. if (n_sigs)
  1269. log_notice(LD_DIR, "Added %d pending signatures while building "
  1270. "consensus.", n_sigs);
  1271. smartlist_clear(pending_consensus_signature_list);
  1272. }
  1273. log_notice(LD_DIR, "Consensus computed; uploading signature(s)");
  1274. directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_SIGNATURES,
  1275. ROUTER_PURPOSE_GENERAL,
  1276. V3_AUTHORITY,
  1277. pending_consensus_signatures,
  1278. strlen(pending_consensus_signatures), 0);
  1279. log_notice(LD_DIR, "Signature(s) posted.");
  1280. return 0;
  1281. err:
  1282. if (votes)
  1283. smartlist_free(votes);
  1284. tor_free(consensus_body);
  1285. tor_free(signatures);
  1286. networkstatus_vote_free(consensus);
  1287. return -1;
  1288. }
  1289. /** DOCDOC */
  1290. static int
  1291. dirvote_add_signatures_to_pending_consensus(
  1292. const char *detached_signatures_body,
  1293. const char **msg_out)
  1294. {
  1295. ns_detached_signatures_t *sigs = NULL;
  1296. int r = -1, regenerate=0;
  1297. char *new_signatures = NULL;
  1298. size_t siglen;
  1299. tor_assert(detached_signatures_body);
  1300. tor_assert(msg_out);
  1301. /* Only call if we have a pending consensus right now. */
  1302. tor_assert(pending_consensus);
  1303. tor_assert(pending_consensus_body);
  1304. tor_assert(pending_consensus_signatures);
  1305. *msg_out = NULL;
  1306. if (!(sigs = networkstatus_parse_detached_signatures(
  1307. detached_signatures_body, NULL))) {
  1308. *msg_out = "Couldn't parse detached signatures.";
  1309. goto err;
  1310. }
  1311. r = networkstatus_add_detached_signatures(pending_consensus,
  1312. sigs,
  1313. &new_signatures,
  1314. &regenerate);
  1315. // XXXX020 originally, this test was regenerate && r >= 0). But one
  1316. // code path is simpler than 2.
  1317. if (new_signatures && (siglen = strlen(new_signatures)) && r >= 0) {
  1318. /* XXXX This should really be its own function. */
  1319. char *new_detached =
  1320. networkstatus_get_detached_signatures(pending_consensus);
  1321. const char *src;
  1322. char *dst;
  1323. size_t new_consensus_len =
  1324. strlen(pending_consensus_body) + strlen(new_detached) + 1;
  1325. pending_consensus_body = tor_realloc(pending_consensus_body,
  1326. new_consensus_len);
  1327. dst = strstr(pending_consensus_body, "directory-signature ");
  1328. tor_assert(dst);
  1329. src = strstr(new_detached, "directory-signature ");
  1330. tor_assert(src);
  1331. strlcpy(dst, src, new_consensus_len - (dst-pending_consensus_body));
  1332. /* XXXX020 remove this once it fails to crash. */
  1333. {
  1334. ns_detached_signatures_t *sigs =
  1335. networkstatus_parse_detached_signatures(new_detached, NULL);
  1336. networkstatus_vote_t *v = networkstatus_parse_vote_from_string(
  1337. pending_consensus_body, 0);
  1338. tor_assert(sigs);
  1339. ns_detached_signatures_free(sigs);
  1340. tor_assert(v);
  1341. networkstatus_vote_free(v);
  1342. }
  1343. tor_free(pending_consensus_signatures);
  1344. pending_consensus_signatures = new_detached;
  1345. }
  1346. #if 0
  1347. else if (new_signatures && (siglen = strlen(new_signatures)) && r >= 0) {
  1348. size_t siglen = strlen(new_signatures);
  1349. size_t len = strlen(pending_consensus_body);
  1350. pending_consensus_body = tor_realloc(pending_consensus_body,
  1351. len+siglen+1);
  1352. memcpy(pending_consensus_body+len, new_signatures, siglen+1);
  1353. len = strlen(pending_consensus_signatures);
  1354. pending_consensus_signatures = tor_realloc(pending_consensus_signatures,
  1355. len+siglen+1);
  1356. memcpy(pending_consensus_signatures+len, new_signatures, siglen+1);
  1357. log_info(LD_DIR, "Added %d new signatures to the pending consensus.", r);
  1358. }
  1359. #endif
  1360. *msg_out = "ok";
  1361. goto done;
  1362. err:
  1363. if (!msg_out)
  1364. *msg_out = "Unrecognized error while adding detached signatures.";
  1365. done:
  1366. tor_free(new_signatures);
  1367. if (sigs)
  1368. ns_detached_signatures_free(sigs);
  1369. return r;
  1370. }
  1371. /** DOCDOC */
  1372. int
  1373. dirvote_add_signatures(const char *detached_signatures_body)
  1374. {
  1375. if (pending_consensus) {
  1376. const char *msg=NULL;
  1377. log_notice(LD_DIR, "Got a signature. Adding it to the pending consensus.");
  1378. return dirvote_add_signatures_to_pending_consensus(
  1379. detached_signatures_body, &msg);
  1380. } else {
  1381. log_notice(LD_DIR, "Got a signature. Queueing it for the next consensus.");
  1382. if (!pending_consensus_signature_list)
  1383. pending_consensus_signature_list = smartlist_create();
  1384. smartlist_add(pending_consensus_signature_list,
  1385. tor_strdup(detached_signatures_body));
  1386. return 0;
  1387. }
  1388. }
  1389. /** DOCDOC */
  1390. int
  1391. dirvote_publish_consensus(void)
  1392. {
  1393. /* Can we actually publish it yet? */
  1394. if (!pending_consensus ||
  1395. networkstatus_check_consensus_signature(pending_consensus)<0) {
  1396. log_warn(LD_DIR, "Not enough info to publish pending consensus");
  1397. return -1;
  1398. }
  1399. if (networkstatus_set_current_consensus(pending_consensus_body, 0))
  1400. log_warn(LD_DIR, "Error publishing consensus");
  1401. else
  1402. log_warn(LD_DIR, "Consensus published.");
  1403. return 0;
  1404. }
  1405. /** Release all static storage held in dirvote.c */
  1406. void
  1407. dirvote_free_all(void)
  1408. {
  1409. dirvote_clear_pending_votes();
  1410. if (pending_vote_list) {
  1411. /* now empty as a result of clear_pending_votes. */
  1412. smartlist_free(pending_vote_list);
  1413. pending_vote_list = NULL;
  1414. }
  1415. tor_free(pending_consensus_body);
  1416. tor_free(pending_consensus_signatures);
  1417. if (pending_consensus) {
  1418. networkstatus_vote_free(pending_consensus);
  1419. pending_consensus = NULL;
  1420. }
  1421. if (pending_consensus_signature_list) {
  1422. /* now empty as a result of clear_pending_votes. */
  1423. smartlist_free(pending_consensus_signature_list);
  1424. pending_consensus_signature_list = NULL;
  1425. }
  1426. }
  1427. /* ====
  1428. * Access to pending items.
  1429. * ==== */
  1430. /** DOCDOC */
  1431. const char *
  1432. dirvote_get_pending_consensus(void)
  1433. {
  1434. return pending_consensus_body;
  1435. }
  1436. /** DOCDOC */
  1437. const char *
  1438. dirvote_get_pending_detached_signatures(void)
  1439. {
  1440. return pending_consensus_signatures;
  1441. }
  1442. /** DOCDOC */
  1443. const cached_dir_t *
  1444. dirvote_get_vote(const char *id)
  1445. {
  1446. if (!pending_vote_list)
  1447. return NULL;
  1448. if (id == NULL) {
  1449. authority_cert_t *c = get_my_v3_authority_cert();
  1450. if (c)
  1451. id = c->cache_info.identity_digest;
  1452. else
  1453. return NULL;
  1454. }
  1455. SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, pv,
  1456. if (!memcmp(get_voter(pv->vote)->identity_digest, id, DIGEST_LEN))
  1457. return pv->vote_body);
  1458. return NULL;
  1459. }