process_descs.c 29 KB

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