process_descs.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2019, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /**
  6. * \file process_descs.c
  7. * \brief Make decisions about uploaded descriptors
  8. *
  9. * Authorities use the code in this module to decide what to do with just-
  10. * uploaded descriptors, and to manage the fingerprint file that helps
  11. * them make those decisions.
  12. **/
  13. #include "core/or/or.h"
  14. #include "feature/dirauth/process_descs.h"
  15. #include "app/config/config.h"
  16. #include "core/or/policies.h"
  17. #include "core/or/versions.h"
  18. #include "feature/dirauth/keypin.h"
  19. #include "feature/dirauth/reachability.h"
  20. #include "feature/dirclient/dlstatus.h"
  21. #include "feature/dircommon/directory.h"
  22. #include "feature/nodelist/describe.h"
  23. #include "feature/nodelist/networkstatus.h"
  24. #include "feature/nodelist/nodelist.h"
  25. #include "feature/nodelist/routerinfo.h"
  26. #include "feature/nodelist/routerlist.h"
  27. #include "feature/dirparse/routerparse.h"
  28. #include "feature/nodelist/torcert.h"
  29. #include "feature/relay/router.h"
  30. #include "core/or/tor_version_st.h"
  31. #include "feature/nodelist/extrainfo_st.h"
  32. #include "feature/nodelist/node_st.h"
  33. #include "feature/nodelist/routerinfo_st.h"
  34. #include "feature/nodelist/routerstatus_st.h"
  35. #include "lib/encoding/confline.h"
  36. /** How far in the future do we allow a router to get? (seconds) */
  37. #define ROUTER_ALLOW_SKEW (60*60*12)
  38. static void directory_remove_invalid(void);
  39. struct authdir_config_t;
  40. static was_router_added_t dirserv_add_extrainfo(extrainfo_t *ei,
  41. const char **msg);
  42. static uint32_t
  43. dirserv_get_status_impl(const char *fp, const char *nickname,
  44. uint32_t addr, uint16_t or_port,
  45. const char *platform, const char **msg,
  46. int severity);
  47. /* 1 Historically used to indicate Named */
  48. #define FP_INVALID 2 /**< Believed invalid. */
  49. #define FP_REJECT 4 /**< We will not publish this router. */
  50. /* 8 Historically used to avoid using this as a dir. */
  51. #define FP_BADEXIT 16 /**< We'll tell clients not to use this as an exit. */
  52. /* 32 Historically used to indicade Unnamed */
  53. /** Target of status_by_digest map. */
  54. typedef uint32_t router_status_t;
  55. static void add_fingerprint_to_dir(const char *fp,
  56. struct authdir_config_t *list,
  57. router_status_t add_status);
  58. /** List of nickname-\>identity fingerprint mappings for all the routers
  59. * that we name. Used to prevent router impersonation. */
  60. typedef struct authdir_config_t {
  61. strmap_t *fp_by_name; /**< Map from lc nickname to fingerprint. */
  62. digestmap_t *status_by_digest; /**< Map from digest to router_status_t. */
  63. } authdir_config_t;
  64. /** Should be static; exposed for testing. */
  65. static authdir_config_t *fingerprint_list = NULL;
  66. /** Allocate and return a new, empty, authdir_config_t. */
  67. static authdir_config_t *
  68. authdir_config_new(void)
  69. {
  70. authdir_config_t *list = tor_malloc_zero(sizeof(authdir_config_t));
  71. list->fp_by_name = strmap_new();
  72. list->status_by_digest = digestmap_new();
  73. return list;
  74. }
  75. /** Add the fingerprint <b>fp</b> to the smartlist of fingerprint_entry_t's
  76. * <b>list</b>, or-ing the currently set status flags with
  77. * <b>add_status</b>.
  78. */
  79. /* static */ void
  80. add_fingerprint_to_dir(const char *fp, authdir_config_t *list,
  81. router_status_t add_status)
  82. {
  83. char *fingerprint;
  84. char d[DIGEST_LEN];
  85. router_status_t *status;
  86. tor_assert(fp);
  87. tor_assert(list);
  88. fingerprint = tor_strdup(fp);
  89. tor_strstrip(fingerprint, " ");
  90. if (base16_decode(d, DIGEST_LEN,
  91. fingerprint, strlen(fingerprint)) != DIGEST_LEN) {
  92. log_warn(LD_DIRSERV, "Couldn't decode fingerprint \"%s\"",
  93. escaped(fp));
  94. tor_free(fingerprint);
  95. return;
  96. }
  97. status = digestmap_get(list->status_by_digest, d);
  98. if (!status) {
  99. status = tor_malloc_zero(sizeof(router_status_t));
  100. digestmap_set(list->status_by_digest, d, status);
  101. }
  102. tor_free(fingerprint);
  103. *status |= add_status;
  104. return;
  105. }
  106. /** Add the fingerprint for this OR to the global list of recognized
  107. * identity key fingerprints. */
  108. int
  109. dirserv_add_own_fingerprint(crypto_pk_t *pk)
  110. {
  111. char fp[FINGERPRINT_LEN+1];
  112. if (crypto_pk_get_fingerprint(pk, fp, 0)<0) {
  113. log_err(LD_BUG, "Error computing fingerprint");
  114. return -1;
  115. }
  116. if (!fingerprint_list)
  117. fingerprint_list = authdir_config_new();
  118. add_fingerprint_to_dir(fp, fingerprint_list, 0);
  119. return 0;
  120. }
  121. /** Load the nickname-\>fingerprint mappings stored in the approved-routers
  122. * file. The file format is line-based, with each non-blank holding one
  123. * nickname, some space, and a fingerprint for that nickname. On success,
  124. * replace the current fingerprint list with the new list and return 0. On
  125. * failure, leave the current fingerprint list untouched, and return -1. */
  126. int
  127. dirserv_load_fingerprint_file(void)
  128. {
  129. char *fname;
  130. char *cf;
  131. char *nickname, *fingerprint;
  132. authdir_config_t *fingerprint_list_new;
  133. int result;
  134. config_line_t *front=NULL, *list;
  135. fname = get_datadir_fname("approved-routers");
  136. log_info(LD_GENERAL,
  137. "Reloading approved fingerprints from \"%s\"...", fname);
  138. cf = read_file_to_str(fname, RFTS_IGNORE_MISSING, NULL);
  139. if (!cf) {
  140. log_warn(LD_FS, "Cannot open fingerprint file '%s'. That's ok.", fname);
  141. tor_free(fname);
  142. return 0;
  143. }
  144. tor_free(fname);
  145. result = config_get_lines(cf, &front, 0);
  146. tor_free(cf);
  147. if (result < 0) {
  148. log_warn(LD_CONFIG, "Error reading from fingerprint file");
  149. return -1;
  150. }
  151. fingerprint_list_new = authdir_config_new();
  152. for (list=front; list; list=list->next) {
  153. char digest_tmp[DIGEST_LEN];
  154. router_status_t add_status = 0;
  155. nickname = list->key; fingerprint = list->value;
  156. tor_strstrip(fingerprint, " "); /* remove spaces */
  157. if (strlen(fingerprint) != HEX_DIGEST_LEN ||
  158. base16_decode(digest_tmp, sizeof(digest_tmp),
  159. fingerprint, HEX_DIGEST_LEN) != sizeof(digest_tmp)) {
  160. log_notice(LD_CONFIG,
  161. "Invalid fingerprint (nickname '%s', "
  162. "fingerprint %s). Skipping.",
  163. nickname, fingerprint);
  164. continue;
  165. }
  166. if (!strcasecmp(nickname, "!reject")) {
  167. add_status = FP_REJECT;
  168. } else if (!strcasecmp(nickname, "!badexit")) {
  169. add_status = FP_BADEXIT;
  170. } else if (!strcasecmp(nickname, "!invalid")) {
  171. add_status = FP_INVALID;
  172. }
  173. add_fingerprint_to_dir(fingerprint, fingerprint_list_new, add_status);
  174. }
  175. config_free_lines(front);
  176. dirserv_free_fingerprint_list();
  177. fingerprint_list = fingerprint_list_new;
  178. /* Delete any routers whose fingerprints we no longer recognize */
  179. directory_remove_invalid();
  180. return 0;
  181. }
  182. /* If this is set, then we don't allow routers that have advertised an Ed25519
  183. * identity to stop doing so. This is going to be essential for good identity
  184. * security: otherwise anybody who can attack RSA-1024 but not Ed25519 could
  185. * just sign fake descriptors missing the Ed25519 key. But we won't actually
  186. * be able to prevent that kind of thing until we're confident that there isn't
  187. * actually a legit reason to downgrade to 0.2.5. Now we are not recommending
  188. * 0.2.5 anymore so there is no reason to keep the #undef.
  189. */
  190. #define DISABLE_DISABLING_ED25519
  191. /** Check whether <b>router</b> has:
  192. * - a nickname/identity key combination that we recognize from the fingerprint
  193. * list,
  194. * - an IP we automatically act on according to our configuration,
  195. * - an appropriate version, and
  196. * - matching pinned keys.
  197. *
  198. * Return the appropriate router status.
  199. *
  200. * If the status is 'FP_REJECT' and <b>msg</b> is provided, set
  201. * *<b>msg</b> to an explanation of why. */
  202. uint32_t
  203. dirserv_router_get_status(const routerinfo_t *router, const char **msg,
  204. int severity)
  205. {
  206. char d[DIGEST_LEN];
  207. const int key_pinning = get_options()->AuthDirPinKeys;
  208. if (crypto_pk_get_digest(router->identity_pkey, d)) {
  209. log_warn(LD_BUG,"Error computing fingerprint");
  210. if (msg)
  211. *msg = "Bug: Error computing fingerprint";
  212. return FP_REJECT;
  213. }
  214. /* Check for the more common reasons to reject a router first. */
  215. const uint32_t r = dirserv_get_status_impl(d, router->nickname,
  216. router->addr, router->or_port,
  217. router->platform, msg, severity);
  218. if (r)
  219. return r;
  220. /* dirserv_get_status_impl already rejects versions older than 0.2.4.18-rc,
  221. * and onion_curve25519_pkey was introduced in 0.2.4.8-alpha.
  222. * But just in case a relay doesn't provide or lies about its version, or
  223. * doesn't include an ntor key in its descriptor, check that it exists,
  224. * and is non-zero (clients check that it's non-zero before using it). */
  225. if (!routerinfo_has_curve25519_onion_key(router)) {
  226. log_fn(severity, LD_DIR,
  227. "Descriptor from router %s is missing an ntor curve25519 onion "
  228. "key.", router_describe(router));
  229. if (msg)
  230. *msg = "Missing ntor curve25519 onion key. Please upgrade!";
  231. return FP_REJECT;
  232. }
  233. if (router->cache_info.signing_key_cert) {
  234. /* This has an ed25519 identity key. */
  235. if (KEYPIN_MISMATCH ==
  236. keypin_check((const uint8_t*)router->cache_info.identity_digest,
  237. router->cache_info.signing_key_cert->signing_key.pubkey)) {
  238. log_fn(severity, LD_DIR,
  239. "Descriptor from router %s has an Ed25519 key, "
  240. "but the <rsa,ed25519> keys don't match what they were before.",
  241. router_describe(router));
  242. if (key_pinning) {
  243. if (msg) {
  244. *msg = "Ed25519 identity key or RSA identity key has changed.";
  245. }
  246. return FP_REJECT;
  247. }
  248. }
  249. } else {
  250. /* No ed25519 key */
  251. if (KEYPIN_MISMATCH == keypin_check_lone_rsa(
  252. (const uint8_t*)router->cache_info.identity_digest)) {
  253. log_fn(severity, LD_DIR,
  254. "Descriptor from router %s has no Ed25519 key, "
  255. "when we previously knew an Ed25519 for it. Ignoring for now, "
  256. "since Ed25519 keys are fairly new.",
  257. router_describe(router));
  258. #ifdef DISABLE_DISABLING_ED25519
  259. if (key_pinning) {
  260. if (msg) {
  261. *msg = "Ed25519 identity key has disappeared.";
  262. }
  263. return FP_REJECT;
  264. }
  265. #endif /* defined(DISABLE_DISABLING_ED25519) */
  266. }
  267. }
  268. return 0;
  269. }
  270. /** Return true if there is no point in downloading the router described by
  271. * <b>rs</b> because this directory would reject it. */
  272. int
  273. dirserv_would_reject_router(const routerstatus_t *rs)
  274. {
  275. uint32_t res;
  276. res = dirserv_get_status_impl(rs->identity_digest, rs->nickname,
  277. rs->addr, rs->or_port,
  278. NULL, NULL, LOG_DEBUG);
  279. return (res & FP_REJECT) != 0;
  280. }
  281. /** Helper: As dirserv_router_get_status, but takes the router fingerprint
  282. * (hex, no spaces), nickname, address (used for logging only), IP address, OR
  283. * port and platform (logging only) as arguments.
  284. *
  285. * Log messages at 'severity'. (There's not much point in
  286. * logging that we're rejecting servers we'll not download.)
  287. */
  288. static uint32_t
  289. dirserv_get_status_impl(const char *id_digest, const char *nickname,
  290. uint32_t addr, uint16_t or_port,
  291. const char *platform, const char **msg, int severity)
  292. {
  293. uint32_t result = 0;
  294. router_status_t *status_by_digest;
  295. if (!fingerprint_list)
  296. fingerprint_list = authdir_config_new();
  297. log_debug(LD_DIRSERV, "%d fingerprints, %d digests known.",
  298. strmap_size(fingerprint_list->fp_by_name),
  299. digestmap_size(fingerprint_list->status_by_digest));
  300. if (platform) {
  301. tor_version_t ver_tmp;
  302. if (tor_version_parse_platform(platform, &ver_tmp, 1) < 0) {
  303. if (msg) {
  304. *msg = "Malformed platform string.";
  305. }
  306. return FP_REJECT;
  307. }
  308. }
  309. /* Versions before Tor 0.2.4.18-rc are too old to support, and are
  310. * missing some important security fixes too. Disable them. */
  311. if (platform && !tor_version_as_new_as(platform,"0.2.4.18-rc")) {
  312. if (msg)
  313. *msg = "Tor version is insecure or unsupported. Please upgrade!";
  314. return FP_REJECT;
  315. }
  316. /* Tor 0.2.9.x where x<5 suffers from bug #20499, where relays don't
  317. * keep their consensus up to date so they make bad guards.
  318. * The simple fix is to just drop them from the network. */
  319. if (platform &&
  320. tor_version_as_new_as(platform,"0.2.9.0-alpha") &&
  321. !tor_version_as_new_as(platform,"0.2.9.5-alpha")) {
  322. if (msg)
  323. *msg = "Tor version contains bug 20499. Please upgrade!";
  324. return FP_REJECT;
  325. }
  326. status_by_digest = digestmap_get(fingerprint_list->status_by_digest,
  327. id_digest);
  328. if (status_by_digest)
  329. result |= *status_by_digest;
  330. if (result & FP_REJECT) {
  331. if (msg)
  332. *msg = "Fingerprint is marked rejected -- if you think this is a "
  333. "mistake please set a valid email address in ContactInfo and "
  334. "send an email to bad-relays@lists.torproject.org mentioning "
  335. "your fingerprint(s)?";
  336. return FP_REJECT;
  337. } else if (result & FP_INVALID) {
  338. if (msg)
  339. *msg = "Fingerprint is marked invalid";
  340. }
  341. if (authdir_policy_badexit_address(addr, or_port)) {
  342. log_fn(severity, LD_DIRSERV,
  343. "Marking '%s' as bad exit because of address '%s'",
  344. nickname, fmt_addr32(addr));
  345. result |= FP_BADEXIT;
  346. }
  347. if (!authdir_policy_permits_address(addr, or_port)) {
  348. log_fn(severity, LD_DIRSERV, "Rejecting '%s' because of address '%s'",
  349. nickname, fmt_addr32(addr));
  350. if (msg)
  351. *msg = "Suspicious relay address range -- if you think this is a "
  352. "mistake please set a valid email address in ContactInfo and "
  353. "send an email to bad-relays@lists.torproject.org mentioning "
  354. "your address(es) and fingerprint(s)?";
  355. return FP_REJECT;
  356. }
  357. if (!authdir_policy_valid_address(addr, or_port)) {
  358. log_fn(severity, LD_DIRSERV,
  359. "Not marking '%s' valid because of address '%s'",
  360. nickname, fmt_addr32(addr));
  361. result |= FP_INVALID;
  362. }
  363. return result;
  364. }
  365. /** Clear the current fingerprint list. */
  366. void
  367. dirserv_free_fingerprint_list(void)
  368. {
  369. if (!fingerprint_list)
  370. return;
  371. strmap_free(fingerprint_list->fp_by_name, tor_free_);
  372. digestmap_free(fingerprint_list->status_by_digest, tor_free_);
  373. tor_free(fingerprint_list);
  374. }
  375. /*
  376. * Descriptor list
  377. */
  378. /** Return -1 if <b>ri</b> has a private or otherwise bad address,
  379. * unless we're configured to not care. Return 0 if all ok. */
  380. STATIC int
  381. dirserv_router_has_valid_address(routerinfo_t *ri)
  382. {
  383. tor_addr_t addr;
  384. if (get_options()->DirAllowPrivateAddresses)
  385. return 0; /* whatever it is, we're fine with it */
  386. tor_addr_from_ipv4h(&addr, ri->addr);
  387. if (tor_addr_is_internal(&addr, 0) || tor_addr_is_null(&addr)) {
  388. log_info(LD_DIRSERV,
  389. "Router %s published internal IPv4 address. Refusing.",
  390. router_describe(ri));
  391. return -1; /* it's a private IP, we should reject it */
  392. }
  393. /* We only check internal v6 on non-null addresses because we do not require
  394. * IPv6 and null IPv6 is normal. */
  395. if (tor_addr_is_internal(&ri->ipv6_addr, 0) &&
  396. !tor_addr_is_null(&ri->ipv6_addr)) {
  397. log_info(LD_DIRSERV,
  398. "Router %s published internal IPv6 address. Refusing.",
  399. router_describe(ri));
  400. return -1; /* it's a private IP, we should reject it */
  401. }
  402. return 0;
  403. }
  404. /** Check whether we, as a directory server, want to accept <b>ri</b>. If so,
  405. * set its is_valid,running fields and return 0. Otherwise, return -1.
  406. *
  407. * If the router is rejected, set *<b>msg</b> to an explanation of why.
  408. *
  409. * If <b>complain</b> then explain at log-level 'notice' why we refused
  410. * a descriptor; else explain at log-level 'info'.
  411. */
  412. int
  413. authdir_wants_to_reject_router(routerinfo_t *ri, const char **msg,
  414. int complain, int *valid_out)
  415. {
  416. /* Okay. Now check whether the fingerprint is recognized. */
  417. time_t now;
  418. int severity = (complain && ri->contact_info) ? LOG_NOTICE : LOG_INFO;
  419. uint32_t status = dirserv_router_get_status(ri, msg, severity);
  420. tor_assert(msg);
  421. if (status & FP_REJECT)
  422. return -1; /* msg is already set. */
  423. /* Is there too much clock skew? */
  424. now = time(NULL);
  425. if (ri->cache_info.published_on > now+ROUTER_ALLOW_SKEW) {
  426. log_fn(severity, LD_DIRSERV, "Publication time for %s is too "
  427. "far (%d minutes) in the future; possible clock skew. Not adding "
  428. "(%s)",
  429. router_describe(ri),
  430. (int)((ri->cache_info.published_on-now)/60),
  431. esc_router_info(ri));
  432. *msg = "Rejected: Your clock is set too far in the future, or your "
  433. "timezone is not correct.";
  434. return -1;
  435. }
  436. if (ri->cache_info.published_on < now-ROUTER_MAX_AGE_TO_PUBLISH) {
  437. log_fn(severity, LD_DIRSERV,
  438. "Publication time for %s is too far "
  439. "(%d minutes) in the past. Not adding (%s)",
  440. router_describe(ri),
  441. (int)((now-ri->cache_info.published_on)/60),
  442. esc_router_info(ri));
  443. *msg = "Rejected: Server is expired, or your clock is too far in the past,"
  444. " or your timezone is not correct.";
  445. return -1;
  446. }
  447. if (dirserv_router_has_valid_address(ri) < 0) {
  448. log_fn(severity, LD_DIRSERV,
  449. "Router %s has invalid address. Not adding (%s).",
  450. router_describe(ri),
  451. esc_router_info(ri));
  452. *msg = "Rejected: Address is a private address.";
  453. return -1;
  454. }
  455. *valid_out = ! (status & FP_INVALID);
  456. return 0;
  457. }
  458. /** Update the relevant flags of <b>node</b> based on our opinion as a
  459. * directory authority in <b>authstatus</b>, as returned by
  460. * dirserv_router_get_status or equivalent. */
  461. void
  462. dirserv_set_node_flags_from_authoritative_status(node_t *node,
  463. uint32_t authstatus)
  464. {
  465. node->is_valid = (authstatus & FP_INVALID) ? 0 : 1;
  466. node->is_bad_exit = (authstatus & FP_BADEXIT) ? 1 : 0;
  467. }
  468. /** True iff <b>a</b> is more severe than <b>b</b>. */
  469. static int
  470. WRA_MORE_SEVERE(was_router_added_t a, was_router_added_t b)
  471. {
  472. return a < b;
  473. }
  474. /** As for dirserv_add_descriptor(), but accepts multiple documents, and
  475. * returns the most severe error that occurred for any one of them. */
  476. was_router_added_t
  477. dirserv_add_multiple_descriptors(const char *desc, size_t desclen,
  478. uint8_t purpose,
  479. const char *source,
  480. const char **msg)
  481. {
  482. was_router_added_t r, r_tmp;
  483. const char *msg_out;
  484. smartlist_t *list;
  485. const char *s;
  486. int n_parsed = 0;
  487. time_t now = time(NULL);
  488. char annotation_buf[ROUTER_ANNOTATION_BUF_LEN];
  489. char time_buf[ISO_TIME_LEN+1];
  490. int general = purpose == ROUTER_PURPOSE_GENERAL;
  491. tor_assert(msg);
  492. r=ROUTER_ADDED_SUCCESSFULLY; /* Least severe return value. */
  493. if (!string_is_utf8_no_bom(desc, desclen)) {
  494. *msg = "descriptor(s) or extrainfo(s) not valid UTF-8 or had BOM.";
  495. return ROUTER_AUTHDIR_REJECTS;
  496. }
  497. format_iso_time(time_buf, now);
  498. if (tor_snprintf(annotation_buf, sizeof(annotation_buf),
  499. "@uploaded-at %s\n"
  500. "@source %s\n"
  501. "%s%s%s", time_buf, escaped(source),
  502. !general ? "@purpose " : "",
  503. !general ? router_purpose_to_string(purpose) : "",
  504. !general ? "\n" : "")<0) {
  505. *msg = "Couldn't format annotations";
  506. return ROUTER_AUTHDIR_BUG_ANNOTATIONS;
  507. }
  508. s = desc;
  509. list = smartlist_new();
  510. if (!router_parse_list_from_string(&s, s+desclen, list, SAVED_NOWHERE, 0, 0,
  511. annotation_buf, NULL)) {
  512. SMARTLIST_FOREACH(list, routerinfo_t *, ri, {
  513. msg_out = NULL;
  514. tor_assert(ri->purpose == purpose);
  515. r_tmp = dirserv_add_descriptor(ri, &msg_out, source);
  516. if (WRA_MORE_SEVERE(r_tmp, r)) {
  517. r = r_tmp;
  518. *msg = msg_out;
  519. }
  520. });
  521. }
  522. n_parsed += smartlist_len(list);
  523. smartlist_clear(list);
  524. s = desc;
  525. if (!router_parse_list_from_string(&s, s+desclen, list, SAVED_NOWHERE, 1, 0,
  526. NULL, NULL)) {
  527. SMARTLIST_FOREACH(list, extrainfo_t *, ei, {
  528. msg_out = NULL;
  529. r_tmp = dirserv_add_extrainfo(ei, &msg_out);
  530. if (WRA_MORE_SEVERE(r_tmp, r)) {
  531. r = r_tmp;
  532. *msg = msg_out;
  533. }
  534. });
  535. }
  536. n_parsed += smartlist_len(list);
  537. smartlist_free(list);
  538. if (! *msg) {
  539. if (!n_parsed) {
  540. *msg = "No descriptors found in your POST.";
  541. if (WRA_WAS_ADDED(r))
  542. r = ROUTER_IS_ALREADY_KNOWN;
  543. } else {
  544. *msg = "(no message)";
  545. }
  546. }
  547. return r;
  548. }
  549. /** Examine the parsed server descriptor in <b>ri</b> and maybe insert it into
  550. * the list of server descriptors. Set *<b>msg</b> to a message that should be
  551. * passed back to the origin of this descriptor, or NULL if there is no such
  552. * message. Use <b>source</b> to produce better log messages.
  553. *
  554. * If <b>ri</b> is not added to the list of server descriptors, free it.
  555. * That means the caller must not access <b>ri</b> after this function
  556. * returns, since it might have been freed.
  557. *
  558. * Return the status of the operation.
  559. *
  560. * This function is only called when fresh descriptors are posted, not when
  561. * we re-load the cache.
  562. */
  563. was_router_added_t
  564. dirserv_add_descriptor(routerinfo_t *ri, const char **msg, const char *source)
  565. {
  566. was_router_added_t r;
  567. routerinfo_t *ri_old;
  568. char *desc, *nickname;
  569. const size_t desclen = ri->cache_info.signed_descriptor_len +
  570. ri->cache_info.annotations_len;
  571. const int key_pinning = get_options()->AuthDirPinKeys;
  572. *msg = NULL;
  573. /* If it's too big, refuse it now. Otherwise we'll cache it all over the
  574. * network and it'll clog everything up. */
  575. if (ri->cache_info.signed_descriptor_len > MAX_DESCRIPTOR_UPLOAD_SIZE) {
  576. log_notice(LD_DIR, "Somebody attempted to publish a router descriptor '%s'"
  577. " (source: %s) with size %d. Either this is an attack, or the "
  578. "MAX_DESCRIPTOR_UPLOAD_SIZE (%d) constant is too low.",
  579. ri->nickname, source, (int)ri->cache_info.signed_descriptor_len,
  580. MAX_DESCRIPTOR_UPLOAD_SIZE);
  581. *msg = "Router descriptor was too large.";
  582. r = ROUTER_AUTHDIR_REJECTS;
  583. goto fail;
  584. }
  585. /* Check whether this descriptor is semantically identical to the last one
  586. * from this server. (We do this here and not in router_add_to_routerlist
  587. * because we want to be able to accept the newest router descriptor that
  588. * another authority has, so we all converge on the same one.) */
  589. ri_old = router_get_mutable_by_digest(ri->cache_info.identity_digest);
  590. if (ri_old && ri_old->cache_info.published_on < ri->cache_info.published_on
  591. && router_differences_are_cosmetic(ri_old, ri)
  592. && !router_is_me(ri)) {
  593. log_info(LD_DIRSERV,
  594. "Not replacing descriptor from %s (source: %s); "
  595. "differences are cosmetic.",
  596. router_describe(ri), source);
  597. *msg = "Not replacing router descriptor; no information has changed since "
  598. "the last one with this identity.";
  599. r = ROUTER_IS_ALREADY_KNOWN;
  600. goto fail;
  601. }
  602. /* Do keypinning again ... this time, to add the pin if appropriate */
  603. int keypin_status;
  604. if (ri->cache_info.signing_key_cert) {
  605. ed25519_public_key_t *pkey = &ri->cache_info.signing_key_cert->signing_key;
  606. /* First let's validate this pubkey before pinning it */
  607. if (ed25519_validate_pubkey(pkey) < 0) {
  608. log_warn(LD_DIRSERV, "Received bad key from %s (source %s)",
  609. router_describe(ri), source);
  610. routerinfo_free(ri);
  611. return ROUTER_AUTHDIR_REJECTS;
  612. }
  613. /* Now pin it! */
  614. keypin_status = keypin_check_and_add(
  615. (const uint8_t*)ri->cache_info.identity_digest,
  616. pkey->pubkey, ! key_pinning);
  617. } else {
  618. keypin_status = keypin_check_lone_rsa(
  619. (const uint8_t*)ri->cache_info.identity_digest);
  620. #ifndef DISABLE_DISABLING_ED25519
  621. if (keypin_status == KEYPIN_MISMATCH)
  622. keypin_status = KEYPIN_NOT_FOUND;
  623. #endif
  624. }
  625. if (keypin_status == KEYPIN_MISMATCH && key_pinning) {
  626. log_info(LD_DIRSERV, "Dropping descriptor from %s (source: %s) because "
  627. "its key did not match an older RSA/Ed25519 keypair",
  628. router_describe(ri), source);
  629. *msg = "Looks like your keypair has changed? This authority previously "
  630. "recorded a different RSA identity for this Ed25519 identity (or vice "
  631. "versa.) Did you replace or copy some of your key files, but not "
  632. "the others? You should either restore the expected keypair, or "
  633. "delete your keys and restart Tor to start your relay with a new "
  634. "identity.";
  635. r = ROUTER_AUTHDIR_REJECTS;
  636. goto fail;
  637. }
  638. /* Make a copy of desc, since router_add_to_routerlist might free
  639. * ri and its associated signed_descriptor_t. */
  640. desc = tor_strndup(ri->cache_info.signed_descriptor_body, desclen);
  641. nickname = tor_strdup(ri->nickname);
  642. /* Tell if we're about to need to launch a test if we add this. */
  643. ri->needs_retest_if_added =
  644. dirserv_should_launch_reachability_test(ri, ri_old);
  645. r = router_add_to_routerlist(ri, msg, 0, 0);
  646. if (!WRA_WAS_ADDED(r)) {
  647. /* unless the routerinfo was fine, just out-of-date */
  648. log_info(LD_DIRSERV,
  649. "Did not add descriptor from '%s' (source: %s): %s.",
  650. nickname, source, *msg ? *msg : "(no message)");
  651. } else {
  652. smartlist_t *changed;
  653. changed = smartlist_new();
  654. smartlist_add(changed, ri);
  655. routerlist_descriptors_added(changed, 0);
  656. smartlist_free(changed);
  657. if (!*msg) {
  658. *msg = "Descriptor accepted";
  659. }
  660. log_info(LD_DIRSERV,
  661. "Added descriptor from '%s' (source: %s): %s.",
  662. nickname, source, *msg);
  663. }
  664. tor_free(desc);
  665. tor_free(nickname);
  666. return r;
  667. fail:
  668. {
  669. const char *desc_digest = ri->cache_info.signed_descriptor_digest;
  670. download_status_t *dls =
  671. router_get_dl_status_by_descriptor_digest(desc_digest);
  672. if (dls) {
  673. log_info(LD_GENERAL, "Marking router with descriptor %s as rejected, "
  674. "and therefore undownloadable",
  675. hex_str(desc_digest, DIGEST_LEN));
  676. download_status_mark_impossible(dls);
  677. }
  678. routerinfo_free(ri);
  679. }
  680. return r;
  681. }
  682. /** As dirserv_add_descriptor, but for an extrainfo_t <b>ei</b>. */
  683. static was_router_added_t
  684. dirserv_add_extrainfo(extrainfo_t *ei, const char **msg)
  685. {
  686. routerinfo_t *ri;
  687. int r;
  688. was_router_added_t rv;
  689. tor_assert(msg);
  690. *msg = NULL;
  691. /* Needs to be mutable so routerinfo_incompatible_with_extrainfo
  692. * can mess with some of the flags in ri->cache_info. */
  693. ri = router_get_mutable_by_digest(ei->cache_info.identity_digest);
  694. if (!ri) {
  695. *msg = "No corresponding router descriptor for extra-info descriptor";
  696. rv = ROUTER_BAD_EI;
  697. goto fail;
  698. }
  699. /* If it's too big, refuse it now. Otherwise we'll cache it all over the
  700. * network and it'll clog everything up. */
  701. if (ei->cache_info.signed_descriptor_len > MAX_EXTRAINFO_UPLOAD_SIZE) {
  702. log_notice(LD_DIR, "Somebody attempted to publish an extrainfo "
  703. "with size %d. Either this is an attack, or the "
  704. "MAX_EXTRAINFO_UPLOAD_SIZE (%d) constant is too low.",
  705. (int)ei->cache_info.signed_descriptor_len,
  706. MAX_EXTRAINFO_UPLOAD_SIZE);
  707. *msg = "Extrainfo document was too large";
  708. rv = ROUTER_BAD_EI;
  709. goto fail;
  710. }
  711. if ((r = routerinfo_incompatible_with_extrainfo(ri->identity_pkey, ei,
  712. &ri->cache_info, msg))) {
  713. if (r<0) {
  714. extrainfo_free(ei);
  715. return ROUTER_IS_ALREADY_KNOWN;
  716. }
  717. rv = ROUTER_BAD_EI;
  718. goto fail;
  719. }
  720. router_add_extrainfo_to_routerlist(ei, msg, 0, 0);
  721. return ROUTER_ADDED_SUCCESSFULLY;
  722. fail:
  723. {
  724. const char *d = ei->cache_info.signed_descriptor_digest;
  725. signed_descriptor_t *sd = router_get_by_extrainfo_digest((char*)d);
  726. if (sd) {
  727. log_info(LD_GENERAL, "Marking extrainfo with descriptor %s as "
  728. "rejected, and therefore undownloadable",
  729. hex_str((char*)d,DIGEST_LEN));
  730. download_status_mark_impossible(&sd->ei_dl_status);
  731. }
  732. extrainfo_free(ei);
  733. }
  734. return rv;
  735. }
  736. /** Remove all descriptors whose nicknames or fingerprints no longer
  737. * are allowed by our fingerprint list. (Descriptors that used to be
  738. * good can become bad when we reload the fingerprint list.)
  739. */
  740. static void
  741. directory_remove_invalid(void)
  742. {
  743. routerlist_t *rl = router_get_routerlist();
  744. smartlist_t *nodes = smartlist_new();
  745. smartlist_add_all(nodes, nodelist_get_list());
  746. SMARTLIST_FOREACH_BEGIN(nodes, node_t *, node) {
  747. const char *msg = NULL;
  748. const char *description;
  749. routerinfo_t *ent = node->ri;
  750. uint32_t r;
  751. if (!ent)
  752. continue;
  753. r = dirserv_router_get_status(ent, &msg, LOG_INFO);
  754. description = router_describe(ent);
  755. if (r & FP_REJECT) {
  756. log_info(LD_DIRSERV, "Router %s is now rejected: %s",
  757. description, msg?msg:"");
  758. routerlist_remove(rl, ent, 0, time(NULL));
  759. continue;
  760. }
  761. if (bool_neq((r & FP_INVALID), !node->is_valid)) {
  762. log_info(LD_DIRSERV, "Router '%s' is now %svalid.", description,
  763. (r&FP_INVALID) ? "in" : "");
  764. node->is_valid = (r&FP_INVALID)?0:1;
  765. }
  766. if (bool_neq((r & FP_BADEXIT), node->is_bad_exit)) {
  767. log_info(LD_DIRSERV, "Router '%s' is now a %s exit", description,
  768. (r & FP_BADEXIT) ? "bad" : "good");
  769. node->is_bad_exit = (r&FP_BADEXIT) ? 1: 0;
  770. }
  771. } SMARTLIST_FOREACH_END(node);
  772. routerlist_assert_ok(rl);
  773. smartlist_free(nodes);
  774. }