dirvote.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467
  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. char *
  270. networkstatus_compute_consensus(smartlist_t *votes,
  271. int total_authorities,
  272. crypto_pk_env_t *identity_key,
  273. crypto_pk_env_t *signing_key)
  274. {
  275. smartlist_t *chunks;
  276. char *result = NULL;
  277. time_t valid_after, fresh_until, valid_until;
  278. int vote_seconds, dist_seconds;
  279. char *client_versions = NULL, *server_versions = NULL;
  280. smartlist_t *flags;
  281. tor_assert(total_authorities >= smartlist_len(votes));
  282. if (!smartlist_len(votes)) {
  283. log_warn(LD_DIR, "Can't compute a consensus from no votes.");
  284. return NULL;
  285. }
  286. /* XXXX020 somebody needs to check vote authority. It could be this
  287. * function, it could be somebody else. */
  288. flags = smartlist_create();
  289. /* Compute medians of time-related things, and figure out how many
  290. * routers we might need to talk about. */
  291. {
  292. smartlist_t *va_times = smartlist_create();
  293. smartlist_t *fu_times = smartlist_create();
  294. smartlist_t *vu_times = smartlist_create();
  295. smartlist_t *votesec_list = smartlist_create();
  296. smartlist_t *distsec_list = smartlist_create();
  297. int n_versioning_clients = 0, n_versioning_servers = 0;
  298. smartlist_t *combined_client_versions = smartlist_create();
  299. smartlist_t *combined_server_versions = smartlist_create();
  300. int j;
  301. SMARTLIST_FOREACH(votes, networkstatus_vote_t *, v,
  302. {
  303. tor_assert(v->is_vote);
  304. smartlist_add(va_times, &v->valid_after);
  305. smartlist_add(fu_times, &v->fresh_until);
  306. smartlist_add(vu_times, &v->valid_until);
  307. smartlist_add(votesec_list, &v->vote_seconds);
  308. smartlist_add(distsec_list, &v->dist_seconds);
  309. if (v->client_versions) {
  310. smartlist_t *cv = smartlist_create();
  311. ++n_versioning_clients;
  312. smartlist_split_string(cv, v->client_versions, ",",
  313. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  314. sort_version_list(cv, 1);
  315. smartlist_add_all(combined_client_versions, cv);
  316. smartlist_free(cv); /* elements get freed later. */
  317. }
  318. if (v->server_versions) {
  319. smartlist_t *sv = smartlist_create();
  320. ++n_versioning_servers;
  321. smartlist_split_string(sv, v->server_versions, ",",
  322. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  323. sort_version_list(sv, 1);
  324. smartlist_add_all(combined_server_versions, sv);
  325. smartlist_free(sv); /* elements get freed later. */
  326. }
  327. SMARTLIST_FOREACH(v->known_flags, const char *, cp,
  328. smartlist_add(flags, tor_strdup(cp)));
  329. });
  330. valid_after = median_time(va_times);
  331. fresh_until = median_time(fu_times);
  332. valid_until = median_time(vu_times);
  333. vote_seconds = median_int(votesec_list);
  334. dist_seconds = median_int(distsec_list);
  335. for (j = 0; j < 2; ++j) {
  336. smartlist_t *lst =
  337. j ? combined_server_versions : combined_client_versions;
  338. int min = (j ? n_versioning_servers : n_versioning_clients) / 2;
  339. smartlist_t *good = smartlist_create();
  340. char *res;
  341. sort_version_list(lst, 0);
  342. get_frequent_members(good, lst, min);
  343. res = smartlist_join_strings(good, ",", 0, NULL);
  344. if (j)
  345. server_versions = res;
  346. else
  347. client_versions = res;
  348. SMARTLIST_FOREACH(lst, char *, cp, tor_free(cp));
  349. smartlist_free(good);
  350. smartlist_free(lst);
  351. }
  352. smartlist_sort_strings(flags);
  353. smartlist_uniq_strings(flags);
  354. smartlist_free(va_times);
  355. smartlist_free(fu_times);
  356. smartlist_free(vu_times);
  357. smartlist_free(votesec_list);
  358. smartlist_free(distsec_list);
  359. }
  360. chunks = smartlist_create();
  361. {
  362. char buf[1024];
  363. char va_buf[ISO_TIME_LEN+1], fu_buf[ISO_TIME_LEN+1],
  364. vu_buf[ISO_TIME_LEN+1];
  365. char *flaglist;
  366. format_iso_time(va_buf, valid_after);
  367. format_iso_time(fu_buf, fresh_until);
  368. format_iso_time(vu_buf, valid_until);
  369. flaglist = smartlist_join_strings(flags, " ", 0, NULL);
  370. tor_snprintf(buf, sizeof(buf),
  371. "network-status-version 3\n"
  372. "vote-status consensus\n"
  373. "valid-after %s\n"
  374. "fresh-until %s\n"
  375. "valid-until %s\n"
  376. "voting-delay %d %d\n"
  377. "client-versions %s\n"
  378. "server-versions %s\n"
  379. "known-flags %s\n",
  380. va_buf, fu_buf, vu_buf,
  381. vote_seconds, dist_seconds,
  382. client_versions, server_versions, flaglist);
  383. smartlist_add(chunks, tor_strdup(buf));
  384. tor_free(flaglist);
  385. }
  386. /* Sort the votes. */
  387. smartlist_sort(votes, _compare_votes_by_authority_id);
  388. /* Add the authority sections. */
  389. SMARTLIST_FOREACH(votes, networkstatus_vote_t *, v,
  390. {
  391. char buf[1024];
  392. struct in_addr in;
  393. char ip[INET_NTOA_BUF_LEN];
  394. char fingerprint[HEX_DIGEST_LEN+1];
  395. char votedigest[HEX_DIGEST_LEN+1];
  396. networkstatus_voter_info_t *voter = get_voter(v);
  397. in.s_addr = htonl(voter->addr);
  398. tor_inet_ntoa(&in, ip, sizeof(ip));
  399. base16_encode(fingerprint, sizeof(fingerprint), voter->identity_digest,
  400. DIGEST_LEN);
  401. base16_encode(votedigest, sizeof(votedigest), voter->vote_digest,
  402. DIGEST_LEN);
  403. tor_snprintf(buf, sizeof(buf),
  404. "dir-source %s %s %s %s %d %d\n"
  405. "contact %s\n"
  406. "vote-digest %s\n",
  407. voter->nickname, fingerprint, voter->address, ip,
  408. voter->dir_port,
  409. voter->or_port,
  410. voter->contact,
  411. votedigest);
  412. smartlist_add(chunks, tor_strdup(buf));
  413. });
  414. /* Add the actual router entries. */
  415. {
  416. /* document these XXXX020 */
  417. int *index;
  418. int *size;
  419. int *flag_counts;
  420. int i;
  421. smartlist_t *matching_descs = smartlist_create();
  422. smartlist_t *chosen_flags = smartlist_create();
  423. smartlist_t *versions = smartlist_create();
  424. int *n_voter_flags; /* n_voter_flags[j] is the number of flags that
  425. * votes[j] knows about. */
  426. int *n_flag_voters; /* n_flag_voters[f] is the number of votes that care
  427. * about flags[f]. */
  428. int **flag_map; /* flag_map[j][b] is an index f such that flag_map[f]
  429. * is the same flag as votes[j]->known_flags[b]. */
  430. int *named_flag;
  431. index = tor_malloc_zero(sizeof(int)*smartlist_len(votes));
  432. size = tor_malloc_zero(sizeof(int)*smartlist_len(votes));
  433. n_voter_flags = tor_malloc_zero(sizeof(int) * smartlist_len(votes));
  434. n_flag_voters = tor_malloc_zero(sizeof(int) * smartlist_len(flags));
  435. flag_map = tor_malloc_zero(sizeof(int*) * smartlist_len(votes));
  436. named_flag = tor_malloc_zero(sizeof(int*) * smartlist_len(votes));
  437. for (i = 0; i < smartlist_len(votes); ++i)
  438. named_flag[i] = -1;
  439. SMARTLIST_FOREACH(votes, networkstatus_vote_t *, v,
  440. {
  441. flag_map[v_sl_idx] = tor_malloc_zero(
  442. sizeof(int)*smartlist_len(v->known_flags));
  443. SMARTLIST_FOREACH(v->known_flags, const char *, fl,
  444. {
  445. int p = smartlist_string_pos(flags, fl);
  446. tor_assert(p >= 0);
  447. flag_map[v_sl_idx][fl_sl_idx] = p;
  448. ++n_flag_voters[p];
  449. if (!strcmp(fl, "Named"))
  450. named_flag[v_sl_idx] = fl_sl_idx;
  451. });
  452. n_voter_flags[v_sl_idx] = smartlist_len(v->known_flags);
  453. size[v_sl_idx] = smartlist_len(v->routerstatus_list);
  454. });
  455. /* Now go through all the votes */
  456. flag_counts = tor_malloc(sizeof(int) * smartlist_len(flags));
  457. while (1) {
  458. vote_routerstatus_t *rs;
  459. routerstatus_t rs_out;
  460. const char *lowest_id = NULL;
  461. const char *chosen_version;
  462. const char *chosen_name = NULL;
  463. int naming_conflict = 0;
  464. int n_listing = 0;
  465. int i;
  466. char buf[256];
  467. SMARTLIST_FOREACH(votes, networkstatus_vote_t *, v, {
  468. if (index[v_sl_idx] < size[v_sl_idx]) {
  469. rs = smartlist_get(v->routerstatus_list, index[v_sl_idx]);
  470. if (!lowest_id ||
  471. memcmp(rs->status.identity_digest, lowest_id, DIGEST_LEN) < 0)
  472. lowest_id = rs->status.identity_digest;
  473. }
  474. });
  475. if (!lowest_id) /* we're out of routers. */
  476. break;
  477. memset(flag_counts, 0, sizeof(int)*smartlist_len(flags));
  478. smartlist_clear(matching_descs);
  479. smartlist_clear(chosen_flags);
  480. smartlist_clear(versions);
  481. /* Okay, go through all the entries for this digest. */
  482. SMARTLIST_FOREACH(votes, networkstatus_vote_t *, v, {
  483. if (index[v_sl_idx] >= size[v_sl_idx])
  484. continue; /* out of entries. */
  485. rs = smartlist_get(v->routerstatus_list, index[v_sl_idx]);
  486. if (memcmp(rs->status.identity_digest, lowest_id, DIGEST_LEN))
  487. continue; /* doesn't include this router. */
  488. /* At this point, we know that we're looking at a routersatus with
  489. * identity "lowest".
  490. */
  491. ++index[v_sl_idx];
  492. ++n_listing;
  493. smartlist_add(matching_descs, rs);
  494. if (rs->version && rs->version[0])
  495. smartlist_add(versions, rs->version);
  496. /* Tally up all the flags. */
  497. for (i = 0; i < n_voter_flags[v_sl_idx]; ++i) {
  498. if (rs->flags & (U64_LITERAL(1) << i))
  499. ++flag_counts[flag_map[v_sl_idx][i]];
  500. }
  501. if (rs->flags & (U64_LITERAL(1) << named_flag[v_sl_idx])) {
  502. if (chosen_name && strcmp(chosen_name, rs->status.nickname))
  503. naming_conflict = 1; /* XXXX020 warn? */
  504. chosen_name = rs->status.nickname;
  505. }
  506. });
  507. /* We don't include this router at all unless more than half of
  508. * the authorities we believe in list it. */
  509. if (n_listing <= total_authorities/2)
  510. continue;
  511. /* Figure out the most popular opinion of what the most recent
  512. * routerinfo and its contents are. */
  513. rs = compute_routerstatus_consensus(matching_descs);
  514. /* Copy bits of that into rs_out. */
  515. tor_assert(!memcmp(lowest_id, rs->status.identity_digest, DIGEST_LEN));
  516. memcpy(rs_out.identity_digest, lowest_id, DIGEST_LEN);
  517. memcpy(rs_out.descriptor_digest, rs->status.descriptor_digest,
  518. DIGEST_LEN);
  519. rs_out.addr = rs->status.addr;
  520. rs_out.published_on = rs->status.published_on;
  521. rs_out.dir_port = rs->status.dir_port;
  522. rs_out.or_port = rs->status.or_port;
  523. if (chosen_name && !naming_conflict) {
  524. strlcpy(rs_out.nickname, chosen_name, sizeof(rs_out.nickname));
  525. } else {
  526. strlcpy(rs_out.nickname, rs->status.nickname, sizeof(rs_out.nickname));
  527. }
  528. /* Set the flags. */
  529. smartlist_add(chosen_flags, (char*)"s"); /* for the start of the line. */
  530. SMARTLIST_FOREACH(flags, const char *, fl,
  531. {
  532. if (strcmp(fl, "Named")) {
  533. if (flag_counts[fl_sl_idx] > n_flag_voters[fl_sl_idx]/2)
  534. smartlist_add(chosen_flags, (char*)fl);
  535. } else {
  536. if (!naming_conflict && flag_counts[fl_sl_idx])
  537. smartlist_add(chosen_flags, (char*)"Named");
  538. }
  539. });
  540. /* Pick the version. */
  541. if (smartlist_len(versions)) {
  542. sort_version_list(versions, 0);
  543. chosen_version = get_most_frequent_member(versions);
  544. } else {
  545. chosen_version = NULL;
  546. }
  547. /* Okay!! Now we can write the descriptor... */
  548. /* First line goes into "buf". */
  549. routerstatus_format_entry(buf, sizeof(buf), &rs_out, NULL, 1);
  550. smartlist_add(chunks, tor_strdup(buf));
  551. /* Second line is all flags. The "\n" is missing. */
  552. smartlist_add(chunks,
  553. smartlist_join_strings(chosen_flags, " ", 0, NULL));
  554. /* Now the version line. */
  555. if (chosen_version) {
  556. /* XXXX020 fails on very long version string */
  557. tor_snprintf(buf, sizeof(buf), "\nv %s\n", chosen_version);
  558. smartlist_add(chunks, tor_strdup(buf));
  559. } else {
  560. smartlist_add(chunks, tor_strdup("\n"));
  561. }
  562. /* And the loop is over and we move on to the next router */
  563. }
  564. tor_free(index);
  565. tor_free(size);
  566. tor_free(n_voter_flags);
  567. tor_free(n_flag_voters);
  568. for (i = 0; i < smartlist_len(votes); ++i)
  569. tor_free(flag_map[i]);
  570. tor_free(flag_map);
  571. tor_free(flag_counts);
  572. smartlist_free(matching_descs);
  573. smartlist_free(chosen_flags);
  574. smartlist_free(versions);
  575. }
  576. /* Add a signature. */
  577. {
  578. char digest[DIGEST_LEN];
  579. char fingerprint[HEX_DIGEST_LEN+1];
  580. char signing_key_fingerprint[HEX_DIGEST_LEN+1];
  581. char buf[4096];
  582. smartlist_add(chunks, tor_strdup("directory-signature "));
  583. /* Compute the hash of the chunks. */
  584. hash_list_members(digest, chunks);
  585. /* Get the fingerprints */
  586. crypto_pk_get_fingerprint(identity_key, fingerprint, 0);
  587. crypto_pk_get_fingerprint(signing_key, signing_key_fingerprint, 0);
  588. /* add the junk that will go at the end of the line. */
  589. tor_snprintf(buf, sizeof(buf), "%s %s\n", fingerprint,
  590. signing_key_fingerprint);
  591. /* And the signature. */
  592. /* XXXX020 check return */
  593. router_append_dirobj_signature(buf, sizeof(buf), digest, signing_key);
  594. smartlist_add(chunks, tor_strdup(buf));
  595. }
  596. result = smartlist_join_strings(chunks, "", 0, NULL);
  597. tor_free(client_versions);
  598. tor_free(server_versions);
  599. smartlist_free(flags);
  600. SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
  601. smartlist_free(chunks);
  602. {
  603. networkstatus_vote_t *c;
  604. if (!(c = networkstatus_parse_vote_from_string(result, 0))) {
  605. log_err(LD_BUG,"Generated a networkstatus consensus we couldn't "
  606. "parse.");
  607. tor_free(result);
  608. return NULL;
  609. }
  610. networkstatus_vote_free(c);
  611. }
  612. return result;
  613. }
  614. /** Check whether the pending_signature on <b>voter</b> is correctly signed by
  615. * the signing key of <b>cert</b>. Return -1 if <b>cert</b> doesn't match the
  616. * signing key; otherwise set the good_signature or bad_signature flag on
  617. * <b>voter</b>, and return 0. */
  618. /* (private; exposed for testing.) */
  619. int
  620. networkstatus_check_voter_signature(networkstatus_vote_t *consensus,
  621. networkstatus_voter_info_t *voter,
  622. authority_cert_t *cert)
  623. {
  624. char d[DIGEST_LEN];
  625. char *signed_digest;
  626. size_t signed_digest_len;
  627. /*XXXX020 check return*/
  628. crypto_pk_get_digest(cert->signing_key, d);
  629. if (memcmp(voter->signing_key_digest, d, DIGEST_LEN)) {
  630. return -1;
  631. }
  632. signed_digest_len = crypto_pk_keysize(cert->signing_key);
  633. signed_digest = tor_malloc(signed_digest_len);
  634. if (crypto_pk_public_checksig(cert->signing_key,
  635. signed_digest,
  636. voter->signature,
  637. voter->signature_len) != DIGEST_LEN ||
  638. memcmp(signed_digest, consensus->networkstatus_digest, DIGEST_LEN)) {
  639. log_warn(LD_DIR, "Got a bad signature."); /*XXXX020 say more*/
  640. voter->bad_signature = 1;
  641. } else {
  642. voter->good_signature = 1;
  643. }
  644. /* XXXX020 did anything rely on this?
  645. * also, rename so it's no longer called "pending". */
  646. // tor_free(voter->signature);
  647. return 0;
  648. }
  649. /** DOCDOC */
  650. int
  651. networkstatus_check_consensus_signature(networkstatus_vote_t *consensus)
  652. {
  653. int n_good = 0;
  654. int n_missing_key = 0;
  655. int n_bad = 0;
  656. int n_unknown = 0;
  657. int n_no_signature = 0;
  658. int n_required = 1; /* XXXX020 This is completely wrong. */
  659. tor_assert(! consensus->is_vote);
  660. SMARTLIST_FOREACH(consensus->voters, networkstatus_voter_info_t *, voter,
  661. {
  662. if (!voter->good_signature && !voter->bad_signature && voter->signature) {
  663. /* we can try to check the signature. */
  664. authority_cert_t *cert =
  665. authority_cert_get_by_digests(voter->identity_digest,
  666. voter->signing_key_digest);
  667. if (! cert) {
  668. ++n_unknown;
  669. continue;
  670. }
  671. if (networkstatus_check_voter_signature(consensus, voter, cert) < 0) {
  672. ++n_missing_key; /* XXXX020 what, really? */
  673. continue;
  674. }
  675. }
  676. if (voter->good_signature)
  677. ++n_good;
  678. else if (voter->bad_signature)
  679. ++n_bad;
  680. else
  681. ++n_no_signature;
  682. });
  683. log_notice(LD_DIR,
  684. "%d unknown, %d missing key, %d good, %d bad, %d no signature,"
  685. "%d required", n_unknown, n_missing_key, n_good, n_bad,
  686. n_no_signature, n_required);
  687. if (n_good >= n_required)
  688. return 0;
  689. else
  690. return -1;
  691. }
  692. /** DOCDOC */
  693. static int
  694. networkstatus_add_signatures_impl(networkstatus_vote_t *target,
  695. smartlist_t *src_voter_list,
  696. char **new_signatures_out)
  697. {
  698. smartlist_t *added_signatures, *sigs;
  699. int r;
  700. tor_assert(target);
  701. tor_assert(!target->is_vote);
  702. tor_assert(new_signatures_out);
  703. added_signatures = smartlist_create();
  704. /* For each voter in src... */
  705. SMARTLIST_FOREACH(src_voter_list, networkstatus_voter_info_t *, src_voter,
  706. {
  707. networkstatus_voter_info_t *target_voter =
  708. networkstatus_get_voter_by_id(target, src_voter->identity_digest);
  709. authority_cert_t *cert;
  710. /* If the target a doesn't know about this voter, then forget it. */
  711. if (!target_voter)
  712. continue;
  713. /* If the target already has a good signature from this voter, then skip
  714. * this one. */
  715. if (target_voter->good_signature)
  716. continue;
  717. /* Try checking the signature if we haven't already. */
  718. if (!src_voter->good_signature && !src_voter->bad_signature) {
  719. cert = authority_cert_get_by_digests(src_voter->identity_digest,
  720. src_voter->signing_key_digest);
  721. if (cert) {
  722. networkstatus_check_voter_signature(target, src_voter, cert);
  723. }
  724. }
  725. /* If this signature is good, then replace and add. */
  726. if (src_voter->good_signature || !target_voter->signature) {
  727. tor_free(target_voter->signature);
  728. target_voter->signature =
  729. tor_memdup(src_voter->signature, src_voter->signature_len);
  730. memcpy(target_voter->signing_key_digest, src_voter->signing_key_digest,
  731. DIGEST_LEN);
  732. target_voter->signature_len = src_voter->signature_len;
  733. target_voter->good_signature = 1;
  734. target_voter->bad_signature = 0;
  735. smartlist_add(added_signatures, target_voter);
  736. }
  737. });
  738. sigs = smartlist_create();
  739. SMARTLIST_FOREACH(added_signatures, networkstatus_voter_info_t *, v,
  740. {
  741. char buf[4096];
  742. char sk[HEX_DIGEST_LEN+1];
  743. char ik[HEX_DIGEST_LEN+1];
  744. tor_assert(v->signature);
  745. base16_encode(sk, sizeof(sk), v->signing_key_digest, DIGEST_LEN);
  746. base16_encode(ik, sizeof(ik), v->identity_digest, DIGEST_LEN);
  747. tor_snprintf(buf, sizeof(buf), "directory-signature %s %s\n"
  748. "-----BEGIN SIGNATURE-----\n", ik, sk);
  749. smartlist_add(sigs, tor_strdup(buf));
  750. base64_encode(buf, sizeof(buf), v->signature, v->signature_len);
  751. strlcat(buf, "-----END SIGNATURE-----\n", sizeof(buf));
  752. smartlist_add(sigs, tor_strdup(buf));
  753. });
  754. *new_signatures_out = smartlist_join_strings(sigs, "", 0, NULL);
  755. SMARTLIST_FOREACH(sigs, char *, cp, tor_free(cp));
  756. smartlist_free(sigs);
  757. r = smartlist_len(added_signatures);
  758. smartlist_free(added_signatures);
  759. return r;
  760. }
  761. /** DOCDOC */
  762. int
  763. networkstatus_add_consensus_signatures(networkstatus_vote_t *target,
  764. networkstatus_vote_t *src,
  765. char **new_signatures_out)
  766. {
  767. tor_assert(src);
  768. tor_assert(! src->is_vote);
  769. *new_signatures_out = NULL;
  770. /* Are they the same consensus? */
  771. if (memcmp(target->networkstatus_digest, src->networkstatus_digest,
  772. DIGEST_LEN))
  773. return -1;
  774. if (target == src)
  775. return 0;
  776. return networkstatus_add_signatures_impl(target, src->voters,
  777. new_signatures_out);
  778. }
  779. /** DOCDOC */
  780. int
  781. networkstatus_add_detached_signatures(networkstatus_vote_t *target,
  782. ns_detached_signatures_t *sigs,
  783. char **new_signatures_out)
  784. {
  785. tor_assert(sigs);
  786. *new_signatures_out = NULL;
  787. /* Are they the same consensus? */
  788. if (memcmp(target->networkstatus_digest, sigs->networkstatus_digest,
  789. DIGEST_LEN))
  790. return -1;
  791. return networkstatus_add_signatures_impl(target, sigs->signatures,
  792. new_signatures_out);
  793. }
  794. /** DOCDOC */
  795. char *
  796. networkstatus_get_detached_signatures(networkstatus_vote_t *consensus)
  797. {
  798. smartlist_t *elements;
  799. char buf[4096];
  800. char *result = NULL;
  801. int n_sigs = 0;
  802. tor_assert(consensus);
  803. tor_assert(! consensus->is_vote);
  804. elements = smartlist_create();
  805. {
  806. char va_buf[ISO_TIME_LEN+1], fu_buf[ISO_TIME_LEN+1],
  807. vu_buf[ISO_TIME_LEN+1];
  808. char d[HEX_DIGEST_LEN+1];
  809. base16_encode(d, sizeof(d), consensus->networkstatus_digest, DIGEST_LEN);
  810. format_iso_time(va_buf, consensus->valid_after);
  811. format_iso_time(fu_buf, consensus->fresh_until);
  812. format_iso_time(vu_buf, consensus->valid_until);
  813. tor_snprintf(buf, sizeof(buf),
  814. "consensus-digest %s\n"
  815. "valid-after %s\n"
  816. "fresh-until %s\n"
  817. "valid-until %s\n", d, va_buf, fu_buf, vu_buf);
  818. smartlist_add(elements, tor_strdup(buf));
  819. }
  820. SMARTLIST_FOREACH(consensus->voters, networkstatus_voter_info_t *, v,
  821. {
  822. char sk[HEX_DIGEST_LEN+1];
  823. char id[HEX_DIGEST_LEN+1];
  824. if (!v->signature) // XXXX020 || !v->good_signature)
  825. continue;
  826. ++n_sigs;
  827. base16_encode(sk, sizeof(sk), v->signing_key_digest, DIGEST_LEN);
  828. base16_encode(id, sizeof(id), v->identity_digest, DIGEST_LEN);
  829. tor_snprintf(buf, sizeof(buf),
  830. "directory-signature %s %s\n-----BEGIN SIGNATURE-----\n",
  831. id, sk);
  832. smartlist_add(elements, tor_strdup(buf));
  833. base64_encode(buf, sizeof(buf), v->signature, v->signature_len);
  834. strlcat(buf, "-----END SIGNATURE-----\n", sizeof(buf));
  835. smartlist_add(elements, tor_strdup(buf));
  836. });
  837. result = smartlist_join_strings(elements, "", 0, NULL);
  838. SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
  839. smartlist_free(elements);
  840. if (!n_sigs)
  841. tor_free(result);
  842. return result;
  843. }
  844. /** DOCDOC */
  845. void
  846. ns_detached_signatures_free(ns_detached_signatures_t *s)
  847. {
  848. if (s->signatures) {
  849. SMARTLIST_FOREACH(s->signatures, networkstatus_voter_info_t *, v,
  850. {
  851. tor_free(v->signature);
  852. tor_free(v);
  853. });
  854. smartlist_free(s->signatures);
  855. }
  856. tor_free(s);
  857. }
  858. /* =====
  859. * Certificate functions
  860. * ===== */
  861. /** Free storage held in <b>cert</b>. */
  862. void
  863. authority_cert_free(authority_cert_t *cert)
  864. {
  865. if (!cert)
  866. return;
  867. tor_free(cert->cache_info.signed_descriptor_body);
  868. if (cert->signing_key)
  869. crypto_free_pk_env(cert->signing_key);
  870. if (cert->identity_key)
  871. crypto_free_pk_env(cert->identity_key);
  872. tor_free(cert);
  873. }
  874. /** Allocate and return a new authority_cert_t with the same contents as
  875. * <b>cert</b>. */
  876. authority_cert_t *
  877. authority_cert_dup(authority_cert_t *cert)
  878. {
  879. authority_cert_t *out = tor_malloc(sizeof(authority_cert_t));
  880. tor_assert(cert);
  881. memcpy(out, cert, sizeof(authority_cert_t));
  882. /* Now copy pointed-to things. */
  883. out->cache_info.signed_descriptor_body =
  884. tor_strndup(cert->cache_info.signed_descriptor_body,
  885. cert->cache_info.signed_descriptor_len);
  886. out->cache_info.saved_location = SAVED_NOWHERE;
  887. out->identity_key = crypto_pk_dup_key(cert->identity_key);
  888. out->signing_key = crypto_pk_dup_key(cert->signing_key);
  889. return out;
  890. }
  891. /* =====
  892. * Vote scheduling
  893. * ===== */
  894. /** DOCDOC */
  895. void
  896. dirvote_get_preferred_voting_intervals(vote_timing_t *timing_out)
  897. {
  898. or_options_t *options = get_options();
  899. tor_assert(timing_out);
  900. timing_out->vote_interval = options->V3AuthVotingInterval;
  901. /* XXXX020 make these configurable. */
  902. timing_out->n_intervals_valid = 3;
  903. timing_out->vote_delay = 300;
  904. timing_out->dist_delay = 300;
  905. }
  906. /** DOCDOC */
  907. time_t
  908. dirvote_get_start_of_next_interval(time_t now, int interval)
  909. {
  910. struct tm tm;
  911. time_t midnight_today;
  912. time_t midnight_tomorrow;
  913. time_t next;
  914. tor_gmtime_r(&now, &tm);
  915. tm.tm_hour = 0;
  916. tm.tm_min = 0;
  917. tm.tm_sec = 0;
  918. midnight_today = tor_timegm(&tm);
  919. midnight_tomorrow = midnight_today + (24*60*60);
  920. next = midnight_today + ((now-midnight_today)/interval + 1)*interval;
  921. if (next > midnight_tomorrow)
  922. next = midnight_tomorrow;
  923. return next;
  924. }
  925. /** DOCDOC */
  926. static struct {
  927. time_t voting_starts;
  928. time_t voting_ends;
  929. time_t interval_starts;
  930. time_t discard_old_votes;
  931. int have_voted;
  932. int have_built_consensus;
  933. int have_published_consensus;
  934. } voting_schedule = {0,0,0,0,0,0,0};
  935. /** DOCDOC */
  936. void
  937. dirvote_recalculate_timing(time_t now)
  938. {
  939. int interval, vote_delay, dist_delay;
  940. time_t start;
  941. networkstatus_vote_t *consensus = networkstatus_get_latest_consensus();
  942. memset(&voting_schedule, 0, sizeof(voting_schedule));
  943. if (consensus) {
  944. /* XXXX020 sanity-check these somewhere! */
  945. interval = consensus->fresh_until - consensus->valid_after;
  946. vote_delay = consensus->vote_seconds;
  947. dist_delay = consensus->dist_seconds;
  948. } else {
  949. /* XXXX020 is this correct according the the spec? */
  950. /* XXXX020 drop this back down to 60 minutes, or whatever the spec says. */
  951. interval = 1200;
  952. vote_delay = dist_delay = 300;
  953. }
  954. start = voting_schedule.interval_starts =
  955. dirvote_get_start_of_next_interval(now,interval);
  956. voting_schedule.voting_ends = start - vote_delay;
  957. voting_schedule.voting_starts = start - vote_delay - dist_delay;
  958. voting_schedule.discard_old_votes = start + 600; /* XXXX020 */
  959. }
  960. /** DOCDOC */
  961. void
  962. dirvote_act(time_t now)
  963. {
  964. if (!voting_schedule.voting_starts)
  965. dirvote_recalculate_timing(now);
  966. if (voting_schedule.voting_starts < now && !voting_schedule.have_voted) {
  967. log_notice(LD_DIR, "Time to vote.");
  968. dirvote_perform_vote();
  969. voting_schedule.have_voted = 1;
  970. }
  971. /* XXXX020 after a couple minutes here, start trying to fetch votes. */
  972. if (voting_schedule.voting_ends < now &&
  973. !voting_schedule.have_built_consensus) {
  974. log_notice(LD_DIR, "Time to compute a consensus.");
  975. dirvote_compute_consensus();
  976. /* XXXX020 we will want to try again later if we haven't got enough
  977. * votes yet. */
  978. voting_schedule.have_built_consensus = 1;
  979. }
  980. if (voting_schedule.interval_starts < now &&
  981. !voting_schedule.have_published_consensus) {
  982. log_notice(LD_DIR, "Time to publish the consensus.");
  983. dirvote_publish_consensus();
  984. /* XXXX020 we will want to try again later if we haven't got enough
  985. * signatures yet. */
  986. voting_schedule.have_published_consensus = 1;
  987. }
  988. if (voting_schedule.discard_old_votes < now) {
  989. log_notice(LD_DIR, "Time to discard old votes consensus.");
  990. dirvote_clear_pending_votes();
  991. dirvote_recalculate_timing(now);
  992. }
  993. }
  994. /** DOCDOC */
  995. typedef struct pending_vote_t {
  996. cached_dir_t *vote_body;
  997. networkstatus_vote_t *vote;
  998. } pending_vote_t;
  999. /** DOCDOC */
  1000. static smartlist_t *pending_vote_list = NULL;
  1001. /** DOCDOC */
  1002. static char *pending_consensus_body = NULL;
  1003. /** DOCDOC */
  1004. static char *pending_consensus_signatures = NULL;
  1005. /** DOCDOC */
  1006. static networkstatus_vote_t *pending_consensus = NULL;
  1007. /** DOCDOC */
  1008. static smartlist_t *pending_consensus_signature_list = NULL;
  1009. /** DOCDOC */
  1010. void
  1011. dirvote_perform_vote(void)
  1012. {
  1013. cached_dir_t *new_vote = generate_v3_networkstatus();
  1014. pending_vote_t *pending_vote;
  1015. int status;
  1016. const char *msg = "";
  1017. if (!new_vote)
  1018. return;
  1019. if (!(pending_vote = dirvote_add_vote(new_vote->dir, &msg, &status))) {
  1020. log_warn(LD_DIR, "Couldn't store my own vote! (I told myself, '%s'.)",
  1021. msg);
  1022. return;
  1023. }
  1024. directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_VOTE,
  1025. ROUTER_PURPOSE_GENERAL,
  1026. V3_AUTHORITY,
  1027. pending_vote->vote_body->dir,
  1028. pending_vote->vote_body->dir_len, 0);
  1029. log_notice(LD_DIR, "Vote posted.");
  1030. }
  1031. /** DOCDOC */
  1032. void
  1033. dirvote_clear_pending_votes(void)
  1034. {
  1035. if (pending_vote_list) {
  1036. SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, v, {
  1037. cached_dir_decref(v->vote_body);
  1038. v->vote_body = NULL;
  1039. networkstatus_vote_free(v->vote);
  1040. tor_free(v);
  1041. });
  1042. smartlist_clear(pending_vote_list);
  1043. }
  1044. if (pending_consensus_signature_list) {
  1045. SMARTLIST_FOREACH(pending_consensus_signature_list, char *, cp,
  1046. tor_free(cp));
  1047. smartlist_clear(pending_consensus_signature_list);
  1048. }
  1049. log_notice(LD_DIR, "Pending votes cleared.");
  1050. }
  1051. /** DOCDOC */
  1052. pending_vote_t *
  1053. dirvote_add_vote(const char *vote_body, const char **msg_out, int *status_out)
  1054. {
  1055. networkstatus_vote_t *vote;
  1056. networkstatus_voter_info_t *vi;
  1057. trusted_dir_server_t *ds;
  1058. pending_vote_t *pending_vote = NULL;
  1059. tor_assert(vote_body);
  1060. tor_assert(msg_out);
  1061. tor_assert(status_out);
  1062. *status_out = 0;
  1063. if (!pending_vote_list)
  1064. pending_vote_list = smartlist_create();
  1065. *msg_out = NULL;
  1066. vote = networkstatus_parse_vote_from_string(vote_body, 1);
  1067. if (!vote) {
  1068. *msg_out = "Unable to parse vote";
  1069. goto err;
  1070. }
  1071. tor_assert(smartlist_len(vote->voters) == 1);
  1072. vi = smartlist_get(vote->voters, 0);
  1073. tor_assert(vi->good_signature == 1);
  1074. ds = trusteddirserver_get_by_v3_auth_digest(vi->identity_digest);
  1075. if (!ds || !(ds->type & V3_AUTHORITY)) {
  1076. *msg_out = "Vote not from a recognized v3 authority";
  1077. goto err;
  1078. }
  1079. tor_assert(vote->cert);
  1080. if (!authority_cert_get_by_digests(vote->cert->cache_info.identity_digest,
  1081. vote->cert->signing_key_digest)) {
  1082. /* Hey, it's a new cert! */
  1083. trusted_dirs_load_certs_from_string(
  1084. vote->cert->cache_info.signed_descriptor_body,
  1085. 0 /* from_store */);
  1086. }
  1087. /* XXXX020 check times; make sure epochs match. */
  1088. SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, v, {
  1089. if (! memcmp(v->vote->cert->cache_info.identity_digest,
  1090. vote->cert->cache_info.identity_digest,
  1091. DIGEST_LEN)) {
  1092. networkstatus_voter_info_t *vi_old = smartlist_get(v->vote->voters, 0);
  1093. if (!memcmp(vi_old->vote_digest, vi->vote_digest, DIGEST_LEN)) {
  1094. /* Ah, it's the same vote. Not a problem. */
  1095. log_info(LD_DIR, "Discarding a vote we already have.");
  1096. *status_out = 200;
  1097. *msg_out = "ok";
  1098. goto err;
  1099. } else if (v->vote->published < vote->published) {
  1100. log_notice(LD_DIR, "Replacing an older pending vote from this "
  1101. "directory.");
  1102. cached_dir_decref(v->vote_body);
  1103. networkstatus_vote_free(v->vote);
  1104. v->vote_body = new_cached_dir(tor_strdup(vote_body),
  1105. vote->published);
  1106. v->vote = vote;
  1107. *msg_out = "ok";
  1108. return v;
  1109. } else {
  1110. *msg_out = "Already have a newer pending vote";
  1111. goto err;
  1112. }
  1113. }
  1114. });
  1115. pending_vote = tor_malloc_zero(sizeof(pending_vote_t));
  1116. pending_vote->vote_body = new_cached_dir(tor_strdup(vote_body),
  1117. vote->published);
  1118. pending_vote->vote = vote;
  1119. smartlist_add(pending_vote_list, pending_vote);
  1120. if (!*status_out)
  1121. *status_out = 200;
  1122. *msg_out = "ok";
  1123. return pending_vote;
  1124. err:
  1125. if (vote)
  1126. networkstatus_vote_free(vote);
  1127. if (!*msg_out)
  1128. *msg_out = "Error adding vote";
  1129. if (!*status_out)
  1130. *status_out = 400;
  1131. /*XXXX020 free other fields */
  1132. return NULL;
  1133. }
  1134. /** DOCDOC */
  1135. int
  1136. dirvote_compute_consensus(void)
  1137. {
  1138. /* Have we got enough votes to try? */
  1139. int n_votes, n_voters;
  1140. smartlist_t *votes = NULL;
  1141. char *consensus_body = NULL, *signatures = NULL;
  1142. networkstatus_vote_t *consensus = NULL;
  1143. authority_cert_t *my_cert;
  1144. if (!pending_vote_list)
  1145. pending_vote_list = smartlist_create();
  1146. n_voters = get_n_authorities(V3_AUTHORITY);
  1147. n_votes = smartlist_len(pending_vote_list);
  1148. /* XXXX020 see if there are enough to go ahead. */
  1149. if (!(my_cert = get_my_v3_authority_cert())) {
  1150. log_warn(LD_DIR, "Can't generate consensus without a certificate.");
  1151. goto err;
  1152. }
  1153. votes = smartlist_create();
  1154. SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, v,
  1155. smartlist_add(votes, v->vote));
  1156. consensus_body = networkstatus_compute_consensus(
  1157. votes, n_voters,
  1158. my_cert->identity_key,
  1159. get_my_v3_authority_signing_key());
  1160. if (!consensus_body) {
  1161. log_warn(LD_DIR, "Couldn't generate a consensus at all!");
  1162. goto err;
  1163. }
  1164. consensus = networkstatus_parse_vote_from_string(consensus_body, 0);
  1165. if (!consensus) {
  1166. log_warn(LD_DIR, "Couldn't parse consensus we generated!");
  1167. goto err;
  1168. }
  1169. signatures = networkstatus_get_detached_signatures(consensus);
  1170. if (!signatures) {
  1171. log_warn(LD_DIR, "Couldn't extract signatures.");
  1172. goto err;
  1173. }
  1174. tor_free(pending_consensus_body);
  1175. pending_consensus_body = consensus_body;
  1176. tor_free(pending_consensus_signatures);
  1177. pending_consensus_signatures = signatures;
  1178. if (pending_consensus)
  1179. networkstatus_vote_free(pending_consensus);
  1180. pending_consensus = consensus;
  1181. if (pending_consensus_signature_list) {
  1182. /* we may have gotten signatures for this consensus before we built
  1183. * it ourself. Add them now. */
  1184. SMARTLIST_FOREACH(pending_consensus_signature_list, char *, sig,
  1185. {
  1186. const char *msg = NULL;
  1187. dirvote_add_signatures_to_pending_consensus(sig, &msg);
  1188. /* XXXX020 log result. */
  1189. tor_free(sig);
  1190. });
  1191. smartlist_clear(pending_consensus_signature_list);
  1192. }
  1193. log_notice(LD_DIR, "Consensus computed.");
  1194. return 0;
  1195. err:
  1196. if (votes)
  1197. smartlist_free(votes);
  1198. tor_free(consensus_body);
  1199. tor_free(signatures);
  1200. networkstatus_vote_free(consensus);
  1201. return -1;
  1202. }
  1203. /** DOCDOC */
  1204. static int
  1205. dirvote_add_signatures_to_pending_consensus(
  1206. const char *detached_signatures_body,
  1207. const char **msg_out)
  1208. {
  1209. ns_detached_signatures_t *sigs = NULL;
  1210. int r = -1;
  1211. char *new_signatures = NULL;
  1212. size_t siglen;
  1213. tor_assert(detached_signatures_body);
  1214. tor_assert(msg_out);
  1215. /* Only call if we have a pending consensus right now. */
  1216. tor_assert(pending_consensus);
  1217. tor_assert(pending_consensus_body);
  1218. tor_assert(pending_consensus_signatures);
  1219. *msg_out = NULL;
  1220. if (!(sigs = networkstatus_parse_detached_signatures(
  1221. detached_signatures_body, NULL))) {
  1222. *msg_out = "Couldn't parse detached signatures.";
  1223. goto err;
  1224. }
  1225. r = networkstatus_add_detached_signatures(pending_consensus,
  1226. sigs,
  1227. &new_signatures);
  1228. if (new_signatures && (siglen = strlen(new_signatures)) && r >= 0) {
  1229. size_t siglen = strlen(new_signatures);
  1230. size_t len = strlen(pending_consensus_body);
  1231. pending_consensus_body = tor_realloc(pending_consensus_body,
  1232. len+siglen+1);
  1233. memcpy(pending_consensus_body+len, new_signatures, siglen+1);
  1234. len = strlen(pending_consensus_signatures);
  1235. pending_consensus_signatures = tor_realloc(pending_consensus_signatures,
  1236. len+siglen+1);
  1237. memcpy(pending_consensus_signatures+len, new_signatures, siglen+1);
  1238. log_info(LD_DIR, "Added %d new signatures to the pending consensus.", r);
  1239. }
  1240. *msg_out = "ok";
  1241. goto done;
  1242. err:
  1243. if (!msg_out)
  1244. *msg_out = "Unrecognized error while adding detached signatures.";
  1245. done:
  1246. tor_free(new_signatures);
  1247. if (sigs)
  1248. ns_detached_signatures_free(sigs);
  1249. return r;
  1250. }
  1251. /** DOCDOC */
  1252. int
  1253. dirvote_add_signatures(const char *detached_signatures_body)
  1254. {
  1255. if (pending_consensus) {
  1256. const char *msg=NULL;
  1257. return dirvote_add_signatures_to_pending_consensus(
  1258. detached_signatures_body, &msg);
  1259. } else {
  1260. if (!pending_consensus_signature_list)
  1261. pending_consensus_signature_list = smartlist_create();
  1262. smartlist_add(pending_consensus_signature_list,
  1263. tor_strdup(detached_signatures_body));
  1264. return 0;
  1265. }
  1266. }
  1267. /** DOCDOC */
  1268. int
  1269. dirvote_publish_consensus(void)
  1270. {
  1271. /* Can we actually publish it yet? */
  1272. if (!pending_consensus ||
  1273. networkstatus_check_consensus_signature(pending_consensus)<0) {
  1274. log_warn(LD_DIR, "Not enough info to publish pending consensus");
  1275. return -1;
  1276. }
  1277. if (networkstatus_set_current_consensus(pending_consensus_body, 0))
  1278. log_warn(LD_DIR, "Error publishing consensus");
  1279. else
  1280. log_warn(LD_DIR, "Consensus published.");
  1281. return 0;
  1282. }
  1283. /** Release all static storage held in dirvote.c */
  1284. void
  1285. dirvote_free_all(void)
  1286. {
  1287. dirvote_clear_pending_votes();
  1288. if (pending_vote_list) {
  1289. /* now empty as a result of clear_pending_votes. */
  1290. smartlist_free(pending_vote_list);
  1291. pending_vote_list = NULL;
  1292. }
  1293. tor_free(pending_consensus_body);
  1294. tor_free(pending_consensus_signatures);
  1295. if (pending_consensus) {
  1296. networkstatus_vote_free(pending_consensus);
  1297. pending_consensus = NULL;
  1298. }
  1299. if (pending_consensus_signature_list) {
  1300. /* now empty as a result of clear_pending_votes. */
  1301. smartlist_free(pending_consensus_signature_list);
  1302. pending_consensus_signature_list = NULL;
  1303. }
  1304. }