dirvote.c 46 KB

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