process_descs.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  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 a nickname/identity key combination that
  192. * we recognize from the fingerprint list, or an IP we automatically act on
  193. * according to our configuration. Return the appropriate router status.
  194. *
  195. * If the status is 'FP_REJECT' and <b>msg</b> is provided, set
  196. * *<b>msg</b> to an explanation of why. */
  197. uint32_t
  198. dirserv_router_get_status(const routerinfo_t *router, const char **msg,
  199. int severity)
  200. {
  201. char d[DIGEST_LEN];
  202. const int key_pinning = get_options()->AuthDirPinKeys;
  203. if (crypto_pk_get_digest(router->identity_pkey, d)) {
  204. log_warn(LD_BUG,"Error computing fingerprint");
  205. if (msg)
  206. *msg = "Bug: Error computing fingerprint";
  207. return FP_REJECT;
  208. }
  209. /* Check for the more usual versions to reject a router first. */
  210. const uint32_t r = dirserv_get_status_impl(d, router->nickname,
  211. router->addr, router->or_port,
  212. router->platform, msg, severity);
  213. if (r)
  214. return r;
  215. /* dirserv_get_status_impl already rejects versions older than 0.2.4.18-rc,
  216. * and onion_curve25519_pkey was introduced in 0.2.4.8-alpha.
  217. * But just in case a relay doesn't provide or lies about its version, or
  218. * doesn't include an ntor key in its descriptor, check that it exists,
  219. * and is non-zero (clients check that it's non-zero before using it). */
  220. if (!routerinfo_has_curve25519_onion_key(router)) {
  221. log_fn(severity, LD_DIR,
  222. "Descriptor from router %s is missing an ntor curve25519 onion "
  223. "key.", router_describe(router));
  224. if (msg)
  225. *msg = "Missing ntor curve25519 onion key. Please upgrade!";
  226. return FP_REJECT;
  227. }
  228. if (router->cache_info.signing_key_cert) {
  229. /* This has an ed25519 identity key. */
  230. if (KEYPIN_MISMATCH ==
  231. keypin_check((const uint8_t*)router->cache_info.identity_digest,
  232. router->cache_info.signing_key_cert->signing_key.pubkey)) {
  233. log_fn(severity, LD_DIR,
  234. "Descriptor from router %s has an Ed25519 key, "
  235. "but the <rsa,ed25519> keys don't match what they were before.",
  236. router_describe(router));
  237. if (key_pinning) {
  238. if (msg) {
  239. *msg = "Ed25519 identity key or RSA identity key has changed.";
  240. }
  241. return FP_REJECT;
  242. }
  243. }
  244. } else {
  245. /* No ed25519 key */
  246. if (KEYPIN_MISMATCH == keypin_check_lone_rsa(
  247. (const uint8_t*)router->cache_info.identity_digest)) {
  248. log_fn(severity, LD_DIR,
  249. "Descriptor from router %s has no Ed25519 key, "
  250. "when we previously knew an Ed25519 for it. Ignoring for now, "
  251. "since Ed25519 keys are fairly new.",
  252. router_describe(router));
  253. #ifdef DISABLE_DISABLING_ED25519
  254. if (key_pinning) {
  255. if (msg) {
  256. *msg = "Ed25519 identity key has disappeared.";
  257. }
  258. return FP_REJECT;
  259. }
  260. #endif /* defined(DISABLE_DISABLING_ED25519) */
  261. }
  262. }
  263. return 0;
  264. }
  265. /** Return true if there is no point in downloading the router described by
  266. * <b>rs</b> because this directory would reject it. */
  267. int
  268. dirserv_would_reject_router(const routerstatus_t *rs)
  269. {
  270. uint32_t res;
  271. res = dirserv_get_status_impl(rs->identity_digest, rs->nickname,
  272. rs->addr, rs->or_port,
  273. NULL, NULL, LOG_DEBUG);
  274. return (res & FP_REJECT) != 0;
  275. }
  276. /** Helper: As dirserv_router_get_status, but takes the router fingerprint
  277. * (hex, no spaces), nickname, address (used for logging only), IP address, OR
  278. * port and platform (logging only) as arguments.
  279. *
  280. * Log messages at 'severity'. (There's not much point in
  281. * logging that we're rejecting servers we'll not download.)
  282. */
  283. static uint32_t
  284. dirserv_get_status_impl(const char *id_digest, const char *nickname,
  285. uint32_t addr, uint16_t or_port,
  286. const char *platform, const char **msg, int severity)
  287. {
  288. uint32_t result = 0;
  289. router_status_t *status_by_digest;
  290. if (!fingerprint_list)
  291. fingerprint_list = authdir_config_new();
  292. log_debug(LD_DIRSERV, "%d fingerprints, %d digests known.",
  293. strmap_size(fingerprint_list->fp_by_name),
  294. digestmap_size(fingerprint_list->status_by_digest));
  295. if (platform) {
  296. tor_version_t ver_tmp;
  297. if (tor_version_parse_platform(platform, &ver_tmp, 1) < 0) {
  298. if (msg) {
  299. *msg = "Malformed platform string.";
  300. }
  301. return FP_REJECT;
  302. }
  303. }
  304. /* Versions before Tor 0.2.4.18-rc are too old to support, and are
  305. * missing some important security fixes too. Disable them. */
  306. if (platform && !tor_version_as_new_as(platform,"0.2.4.18-rc")) {
  307. if (msg)
  308. *msg = "Tor version is insecure or unsupported. Please upgrade!";
  309. return FP_REJECT;
  310. }
  311. /* Tor 0.2.9.x where x<5 suffers from bug #20499, where relays don't
  312. * keep their consensus up to date so they make bad guards.
  313. * The simple fix is to just drop them from the network. */
  314. if (platform &&
  315. tor_version_as_new_as(platform,"0.2.9.0-alpha") &&
  316. !tor_version_as_new_as(platform,"0.2.9.5-alpha")) {
  317. if (msg)
  318. *msg = "Tor version contains bug 20499. Please upgrade!";
  319. return FP_REJECT;
  320. }
  321. status_by_digest = digestmap_get(fingerprint_list->status_by_digest,
  322. id_digest);
  323. if (status_by_digest)
  324. result |= *status_by_digest;
  325. if (result & FP_REJECT) {
  326. if (msg)
  327. *msg = "Fingerprint is marked rejected -- if you think this is a "
  328. "mistake please set a valid email address in ContactInfo and "
  329. "send an email to bad-relays@lists.torproject.org mentioning "
  330. "your fingerprint(s)?";
  331. return FP_REJECT;
  332. } else if (result & FP_INVALID) {
  333. if (msg)
  334. *msg = "Fingerprint is marked invalid";
  335. }
  336. if (authdir_policy_badexit_address(addr, or_port)) {
  337. log_fn(severity, LD_DIRSERV,
  338. "Marking '%s' as bad exit because of address '%s'",
  339. nickname, fmt_addr32(addr));
  340. result |= FP_BADEXIT;
  341. }
  342. if (!authdir_policy_permits_address(addr, or_port)) {
  343. log_fn(severity, LD_DIRSERV, "Rejecting '%s' because of address '%s'",
  344. nickname, fmt_addr32(addr));
  345. if (msg)
  346. *msg = "Suspicious relay address range -- if you think this is a "
  347. "mistake please set a valid email address in ContactInfo and "
  348. "send an email to bad-relays@lists.torproject.org mentioning "
  349. "your address(es) and fingerprint(s)?";
  350. return FP_REJECT;
  351. }
  352. if (!authdir_policy_valid_address(addr, or_port)) {
  353. log_fn(severity, LD_DIRSERV,
  354. "Not marking '%s' valid because of address '%s'",
  355. nickname, fmt_addr32(addr));
  356. result |= FP_INVALID;
  357. }
  358. return result;
  359. }
  360. /** Clear the current fingerprint list. */
  361. void
  362. dirserv_free_fingerprint_list(void)
  363. {
  364. if (!fingerprint_list)
  365. return;
  366. strmap_free(fingerprint_list->fp_by_name, tor_free_);
  367. digestmap_free(fingerprint_list->status_by_digest, tor_free_);
  368. tor_free(fingerprint_list);
  369. }
  370. /*
  371. * Descriptor list
  372. */
  373. /** Return -1 if <b>ri</b> has a private or otherwise bad address,
  374. * unless we're configured to not care. Return 0 if all ok. */
  375. static int
  376. dirserv_router_has_valid_address(routerinfo_t *ri)
  377. {
  378. tor_addr_t addr;
  379. if (get_options()->DirAllowPrivateAddresses)
  380. return 0; /* whatever it is, we're fine with it */
  381. tor_addr_from_ipv4h(&addr, ri->addr);
  382. if (tor_addr_is_internal(&addr, 0)) {
  383. log_info(LD_DIRSERV,
  384. "Router %s published internal IP address. Refusing.",
  385. router_describe(ri));
  386. return -1; /* it's a private IP, we should reject it */
  387. }
  388. return 0;
  389. }
  390. /** Check whether we, as a directory server, want to accept <b>ri</b>. If so,
  391. * set its is_valid,running fields and return 0. Otherwise, return -1.
  392. *
  393. * If the router is rejected, set *<b>msg</b> to an explanation of why.
  394. *
  395. * If <b>complain</b> then explain at log-level 'notice' why we refused
  396. * a descriptor; else explain at log-level 'info'.
  397. */
  398. int
  399. authdir_wants_to_reject_router(routerinfo_t *ri, const char **msg,
  400. int complain, int *valid_out)
  401. {
  402. /* Okay. Now check whether the fingerprint is recognized. */
  403. time_t now;
  404. int severity = (complain && ri->contact_info) ? LOG_NOTICE : LOG_INFO;
  405. uint32_t status = dirserv_router_get_status(ri, msg, severity);
  406. tor_assert(msg);
  407. if (status & FP_REJECT)
  408. return -1; /* msg is already set. */
  409. /* Is there too much clock skew? */
  410. now = time(NULL);
  411. if (ri->cache_info.published_on > now+ROUTER_ALLOW_SKEW) {
  412. log_fn(severity, LD_DIRSERV, "Publication time for %s is too "
  413. "far (%d minutes) in the future; possible clock skew. Not adding "
  414. "(%s)",
  415. router_describe(ri),
  416. (int)((ri->cache_info.published_on-now)/60),
  417. esc_router_info(ri));
  418. *msg = "Rejected: Your clock is set too far in the future, or your "
  419. "timezone is not correct.";
  420. return -1;
  421. }
  422. if (ri->cache_info.published_on < now-ROUTER_MAX_AGE_TO_PUBLISH) {
  423. log_fn(severity, LD_DIRSERV,
  424. "Publication time for %s is too far "
  425. "(%d minutes) in the past. Not adding (%s)",
  426. router_describe(ri),
  427. (int)((now-ri->cache_info.published_on)/60),
  428. esc_router_info(ri));
  429. *msg = "Rejected: Server is expired, or your clock is too far in the past,"
  430. " or your timezone is not correct.";
  431. return -1;
  432. }
  433. if (dirserv_router_has_valid_address(ri) < 0) {
  434. log_fn(severity, LD_DIRSERV,
  435. "Router %s has invalid address. Not adding (%s).",
  436. router_describe(ri),
  437. esc_router_info(ri));
  438. *msg = "Rejected: Address is a private address.";
  439. return -1;
  440. }
  441. *valid_out = ! (status & FP_INVALID);
  442. return 0;
  443. }
  444. /** Update the relevant flags of <b>node</b> based on our opinion as a
  445. * directory authority in <b>authstatus</b>, as returned by
  446. * dirserv_router_get_status or equivalent. */
  447. void
  448. dirserv_set_node_flags_from_authoritative_status(node_t *node,
  449. uint32_t authstatus)
  450. {
  451. node->is_valid = (authstatus & FP_INVALID) ? 0 : 1;
  452. node->is_bad_exit = (authstatus & FP_BADEXIT) ? 1 : 0;
  453. }
  454. /** True iff <b>a</b> is more severe than <b>b</b>. */
  455. static int
  456. WRA_MORE_SEVERE(was_router_added_t a, was_router_added_t b)
  457. {
  458. return a < b;
  459. }
  460. /** As for dirserv_add_descriptor(), but accepts multiple documents, and
  461. * returns the most severe error that occurred for any one of them. */
  462. was_router_added_t
  463. dirserv_add_multiple_descriptors(const char *desc, uint8_t purpose,
  464. const char *source,
  465. const char **msg)
  466. {
  467. was_router_added_t r, r_tmp;
  468. const char *msg_out;
  469. smartlist_t *list;
  470. const char *s;
  471. int n_parsed = 0;
  472. time_t now = time(NULL);
  473. char annotation_buf[ROUTER_ANNOTATION_BUF_LEN];
  474. char time_buf[ISO_TIME_LEN+1];
  475. int general = purpose == ROUTER_PURPOSE_GENERAL;
  476. tor_assert(msg);
  477. r=ROUTER_ADDED_SUCCESSFULLY; /*Least severe return value. */
  478. format_iso_time(time_buf, now);
  479. if (tor_snprintf(annotation_buf, sizeof(annotation_buf),
  480. "@uploaded-at %s\n"
  481. "@source %s\n"
  482. "%s%s%s", time_buf, escaped(source),
  483. !general ? "@purpose " : "",
  484. !general ? router_purpose_to_string(purpose) : "",
  485. !general ? "\n" : "")<0) {
  486. *msg = "Couldn't format annotations";
  487. /* XXX Not cool: we return -1 below, but (was_router_added_t)-1 is
  488. * ROUTER_BAD_EI, which isn't what's gone wrong here. :( */
  489. return -1;
  490. }
  491. s = desc;
  492. list = smartlist_new();
  493. if (!router_parse_list_from_string(&s, NULL, list, SAVED_NOWHERE, 0, 0,
  494. annotation_buf, NULL)) {
  495. SMARTLIST_FOREACH(list, routerinfo_t *, ri, {
  496. msg_out = NULL;
  497. tor_assert(ri->purpose == purpose);
  498. r_tmp = dirserv_add_descriptor(ri, &msg_out, source);
  499. if (WRA_MORE_SEVERE(r_tmp, r)) {
  500. r = r_tmp;
  501. *msg = msg_out;
  502. }
  503. });
  504. }
  505. n_parsed += smartlist_len(list);
  506. smartlist_clear(list);
  507. s = desc;
  508. if (!router_parse_list_from_string(&s, NULL, list, SAVED_NOWHERE, 1, 0,
  509. NULL, NULL)) {
  510. SMARTLIST_FOREACH(list, extrainfo_t *, ei, {
  511. msg_out = NULL;
  512. r_tmp = dirserv_add_extrainfo(ei, &msg_out);
  513. if (WRA_MORE_SEVERE(r_tmp, r)) {
  514. r = r_tmp;
  515. *msg = msg_out;
  516. }
  517. });
  518. }
  519. n_parsed += smartlist_len(list);
  520. smartlist_free(list);
  521. if (! *msg) {
  522. if (!n_parsed) {
  523. *msg = "No descriptors found in your POST.";
  524. if (WRA_WAS_ADDED(r))
  525. r = ROUTER_IS_ALREADY_KNOWN;
  526. } else {
  527. *msg = "(no message)";
  528. }
  529. }
  530. return r;
  531. }
  532. /** Examine the parsed server descriptor in <b>ri</b> and maybe insert it into
  533. * the list of server descriptors. Set *<b>msg</b> to a message that should be
  534. * passed back to the origin of this descriptor, or NULL if there is no such
  535. * message. Use <b>source</b> to produce better log messages.
  536. *
  537. * If <b>ri</b> is not added to the list of server descriptors, free it.
  538. * That means the caller must not access <b>ri</b> after this function
  539. * returns, since it might have been freed.
  540. *
  541. * Return the status of the operation.
  542. *
  543. * This function is only called when fresh descriptors are posted, not when
  544. * we re-load the cache.
  545. */
  546. was_router_added_t
  547. dirserv_add_descriptor(routerinfo_t *ri, const char **msg, const char *source)
  548. {
  549. was_router_added_t r;
  550. routerinfo_t *ri_old;
  551. char *desc, *nickname;
  552. const size_t desclen = ri->cache_info.signed_descriptor_len +
  553. ri->cache_info.annotations_len;
  554. const int key_pinning = get_options()->AuthDirPinKeys;
  555. *msg = NULL;
  556. /* If it's too big, refuse it now. Otherwise we'll cache it all over the
  557. * network and it'll clog everything up. */
  558. if (ri->cache_info.signed_descriptor_len > MAX_DESCRIPTOR_UPLOAD_SIZE) {
  559. log_notice(LD_DIR, "Somebody attempted to publish a router descriptor '%s'"
  560. " (source: %s) with size %d. Either this is an attack, or the "
  561. "MAX_DESCRIPTOR_UPLOAD_SIZE (%d) constant is too low.",
  562. ri->nickname, source, (int)ri->cache_info.signed_descriptor_len,
  563. MAX_DESCRIPTOR_UPLOAD_SIZE);
  564. *msg = "Router descriptor was too large.";
  565. r = ROUTER_AUTHDIR_REJECTS;
  566. goto fail;
  567. }
  568. /* Check whether this descriptor is semantically identical to the last one
  569. * from this server. (We do this here and not in router_add_to_routerlist
  570. * because we want to be able to accept the newest router descriptor that
  571. * another authority has, so we all converge on the same one.) */
  572. ri_old = router_get_mutable_by_digest(ri->cache_info.identity_digest);
  573. if (ri_old && ri_old->cache_info.published_on < ri->cache_info.published_on
  574. && router_differences_are_cosmetic(ri_old, ri)
  575. && !router_is_me(ri)) {
  576. log_info(LD_DIRSERV,
  577. "Not replacing descriptor from %s (source: %s); "
  578. "differences are cosmetic.",
  579. router_describe(ri), source);
  580. *msg = "Not replacing router descriptor; no information has changed since "
  581. "the last one with this identity.";
  582. r = ROUTER_IS_ALREADY_KNOWN;
  583. goto fail;
  584. }
  585. /* Do keypinning again ... this time, to add the pin if appropriate */
  586. int keypin_status;
  587. if (ri->cache_info.signing_key_cert) {
  588. ed25519_public_key_t *pkey = &ri->cache_info.signing_key_cert->signing_key;
  589. /* First let's validate this pubkey before pinning it */
  590. if (ed25519_validate_pubkey(pkey) < 0) {
  591. log_warn(LD_DIRSERV, "Received bad key from %s (source %s)",
  592. router_describe(ri), source);
  593. routerinfo_free(ri);
  594. return ROUTER_AUTHDIR_REJECTS;
  595. }
  596. /* Now pin it! */
  597. keypin_status = keypin_check_and_add(
  598. (const uint8_t*)ri->cache_info.identity_digest,
  599. pkey->pubkey, ! key_pinning);
  600. } else {
  601. keypin_status = keypin_check_lone_rsa(
  602. (const uint8_t*)ri->cache_info.identity_digest);
  603. #ifndef DISABLE_DISABLING_ED25519
  604. if (keypin_status == KEYPIN_MISMATCH)
  605. keypin_status = KEYPIN_NOT_FOUND;
  606. #endif
  607. }
  608. if (keypin_status == KEYPIN_MISMATCH && key_pinning) {
  609. log_info(LD_DIRSERV, "Dropping descriptor from %s (source: %s) because "
  610. "its key did not match an older RSA/Ed25519 keypair",
  611. router_describe(ri), source);
  612. *msg = "Looks like your keypair has changed? This authority previously "
  613. "recorded a different RSA identity for this Ed25519 identity (or vice "
  614. "versa.) Did you replace or copy some of your key files, but not "
  615. "the others? You should either restore the expected keypair, or "
  616. "delete your keys and restart Tor to start your relay with a new "
  617. "identity.";
  618. r = ROUTER_AUTHDIR_REJECTS;
  619. goto fail;
  620. }
  621. /* Make a copy of desc, since router_add_to_routerlist might free
  622. * ri and its associated signed_descriptor_t. */
  623. desc = tor_strndup(ri->cache_info.signed_descriptor_body, desclen);
  624. nickname = tor_strdup(ri->nickname);
  625. /* Tell if we're about to need to launch a test if we add this. */
  626. ri->needs_retest_if_added =
  627. dirserv_should_launch_reachability_test(ri, ri_old);
  628. r = router_add_to_routerlist(ri, msg, 0, 0);
  629. if (!WRA_WAS_ADDED(r)) {
  630. /* unless the routerinfo was fine, just out-of-date */
  631. log_info(LD_DIRSERV,
  632. "Did not add descriptor from '%s' (source: %s): %s.",
  633. nickname, source, *msg ? *msg : "(no message)");
  634. } else {
  635. smartlist_t *changed;
  636. changed = smartlist_new();
  637. smartlist_add(changed, ri);
  638. routerlist_descriptors_added(changed, 0);
  639. smartlist_free(changed);
  640. if (!*msg) {
  641. *msg = "Descriptor accepted";
  642. }
  643. log_info(LD_DIRSERV,
  644. "Added descriptor from '%s' (source: %s): %s.",
  645. nickname, source, *msg);
  646. }
  647. tor_free(desc);
  648. tor_free(nickname);
  649. return r;
  650. fail:
  651. {
  652. const char *desc_digest = ri->cache_info.signed_descriptor_digest;
  653. download_status_t *dls =
  654. router_get_dl_status_by_descriptor_digest(desc_digest);
  655. if (dls) {
  656. log_info(LD_GENERAL, "Marking router with descriptor %s as rejected, "
  657. "and therefore undownloadable",
  658. hex_str(desc_digest, DIGEST_LEN));
  659. download_status_mark_impossible(dls);
  660. }
  661. routerinfo_free(ri);
  662. }
  663. return r;
  664. }
  665. /** As dirserv_add_descriptor, but for an extrainfo_t <b>ei</b>. */
  666. static was_router_added_t
  667. dirserv_add_extrainfo(extrainfo_t *ei, const char **msg)
  668. {
  669. routerinfo_t *ri;
  670. int r;
  671. was_router_added_t rv;
  672. tor_assert(msg);
  673. *msg = NULL;
  674. /* Needs to be mutable so routerinfo_incompatible_with_extrainfo
  675. * can mess with some of the flags in ri->cache_info. */
  676. ri = router_get_mutable_by_digest(ei->cache_info.identity_digest);
  677. if (!ri) {
  678. *msg = "No corresponding router descriptor for extra-info descriptor";
  679. rv = ROUTER_BAD_EI;
  680. goto fail;
  681. }
  682. /* If it's too big, refuse it now. Otherwise we'll cache it all over the
  683. * network and it'll clog everything up. */
  684. if (ei->cache_info.signed_descriptor_len > MAX_EXTRAINFO_UPLOAD_SIZE) {
  685. log_notice(LD_DIR, "Somebody attempted to publish an extrainfo "
  686. "with size %d. Either this is an attack, or the "
  687. "MAX_EXTRAINFO_UPLOAD_SIZE (%d) constant is too low.",
  688. (int)ei->cache_info.signed_descriptor_len,
  689. MAX_EXTRAINFO_UPLOAD_SIZE);
  690. *msg = "Extrainfo document was too large";
  691. rv = ROUTER_BAD_EI;
  692. goto fail;
  693. }
  694. if ((r = routerinfo_incompatible_with_extrainfo(ri->identity_pkey, ei,
  695. &ri->cache_info, msg))) {
  696. if (r<0) {
  697. extrainfo_free(ei);
  698. return ROUTER_IS_ALREADY_KNOWN;
  699. }
  700. rv = ROUTER_BAD_EI;
  701. goto fail;
  702. }
  703. router_add_extrainfo_to_routerlist(ei, msg, 0, 0);
  704. return ROUTER_ADDED_SUCCESSFULLY;
  705. fail:
  706. {
  707. const char *d = ei->cache_info.signed_descriptor_digest;
  708. signed_descriptor_t *sd = router_get_by_extrainfo_digest((char*)d);
  709. if (sd) {
  710. log_info(LD_GENERAL, "Marking extrainfo with descriptor %s as "
  711. "rejected, and therefore undownloadable",
  712. hex_str((char*)d,DIGEST_LEN));
  713. download_status_mark_impossible(&sd->ei_dl_status);
  714. }
  715. extrainfo_free(ei);
  716. }
  717. return rv;
  718. }
  719. /** Remove all descriptors whose nicknames or fingerprints no longer
  720. * are allowed by our fingerprint list. (Descriptors that used to be
  721. * good can become bad when we reload the fingerprint list.)
  722. */
  723. static void
  724. directory_remove_invalid(void)
  725. {
  726. routerlist_t *rl = router_get_routerlist();
  727. smartlist_t *nodes = smartlist_new();
  728. smartlist_add_all(nodes, nodelist_get_list());
  729. SMARTLIST_FOREACH_BEGIN(nodes, node_t *, node) {
  730. const char *msg = NULL;
  731. const char *description;
  732. routerinfo_t *ent = node->ri;
  733. uint32_t r;
  734. if (!ent)
  735. continue;
  736. r = dirserv_router_get_status(ent, &msg, LOG_INFO);
  737. description = router_describe(ent);
  738. if (r & FP_REJECT) {
  739. log_info(LD_DIRSERV, "Router %s is now rejected: %s",
  740. description, msg?msg:"");
  741. routerlist_remove(rl, ent, 0, time(NULL));
  742. continue;
  743. }
  744. if (bool_neq((r & FP_INVALID), !node->is_valid)) {
  745. log_info(LD_DIRSERV, "Router '%s' is now %svalid.", description,
  746. (r&FP_INVALID) ? "in" : "");
  747. node->is_valid = (r&FP_INVALID)?0:1;
  748. }
  749. if (bool_neq((r & FP_BADEXIT), node->is_bad_exit)) {
  750. log_info(LD_DIRSERV, "Router '%s' is now a %s exit", description,
  751. (r & FP_BADEXIT) ? "bad" : "good");
  752. node->is_bad_exit = (r&FP_BADEXIT) ? 1: 0;
  753. }
  754. } SMARTLIST_FOREACH_END(node);
  755. routerlist_assert_ok(rl);
  756. smartlist_free(nodes);
  757. }