process_descs.c 29 KB

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