process_descs.c 30 KB

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