process_descs.c 29 KB

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