hs_config.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. /* Copyright (c) 2017-2018, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file hs_config.c
  5. * \brief Implement hidden service configuration subsystem.
  6. *
  7. * \details
  8. *
  9. * This file has basically one main entry point: hs_config_service_all(). It
  10. * takes the torrc options and configure hidden service from it. In validate
  11. * mode, nothing is added to the global service list or keys are not generated
  12. * nor loaded.
  13. *
  14. * A service is configured in two steps. It is first created using the tor
  15. * options and then put in a staging list. It will stay there until
  16. * hs_service_load_all_keys() is called. That function is responsible to
  17. * load/generate the keys for the service in the staging list and if
  18. * successful, transfert the service to the main global service list where
  19. * at that point it is ready to be used.
  20. *
  21. * Configuration functions are per-version and there is a main generic one for
  22. * every option that is common to all version (config_generic_service).
  23. **/
  24. #define HS_CONFIG_PRIVATE
  25. #include "feature/hs/hs_common.h"
  26. #include "feature/hs/hs_config.h"
  27. #include "feature/hs/hs_service.h"
  28. #include "feature/rend/rendservice.h"
  29. #include "lib/encoding/confline.h"
  30. #include "app/config/or_options_st.h"
  31. /* Using the given list of services, stage them into our global state. Every
  32. * service version are handled. This function can remove entries in the given
  33. * service_list.
  34. *
  35. * Staging a service means that we take all services in service_list and we
  36. * put them in the staging list (global) which acts as a temporary list that
  37. * is used by the service loading key process. In other words, staging a
  38. * service puts it in a list to be considered when loading the keys and then
  39. * moved to the main global list. */
  40. static void
  41. stage_services(smartlist_t *service_list)
  42. {
  43. tor_assert(service_list);
  44. /* This is v2 specific. Trigger service pruning which will make sure the
  45. * just configured services end up in the main global list. It should only
  46. * be done in non validation mode because v2 subsystem handles service
  47. * object differently. */
  48. rend_service_prune_list();
  49. /* Cleanup v2 service from the list, we don't need those object anymore
  50. * because we validated them all against the others and we want to stage
  51. * only >= v3 service. And remember, v2 has a different object type which is
  52. * shadow copied from an hs_service_t type. */
  53. SMARTLIST_FOREACH_BEGIN(service_list, hs_service_t *, s) {
  54. if (s->config.version == HS_VERSION_TWO) {
  55. SMARTLIST_DEL_CURRENT(service_list, s);
  56. hs_service_free(s);
  57. }
  58. } SMARTLIST_FOREACH_END(s);
  59. /* This is >= v3 specific. Using the newly configured service list, stage
  60. * them into our global state. Every object ownership is lost after. */
  61. hs_service_stage_services(service_list);
  62. }
  63. /* Validate the given service against all service in the given list. If the
  64. * service is ephemeral, this function ignores it. Services with the same
  65. * directory path aren't allowed and will return an error. If a duplicate is
  66. * found, 1 is returned else 0 if none found. */
  67. static int
  68. service_is_duplicate_in_list(const smartlist_t *service_list,
  69. const hs_service_t *service)
  70. {
  71. int ret = 0;
  72. tor_assert(service_list);
  73. tor_assert(service);
  74. /* Ephemeral service don't have a directory configured so no need to check
  75. * for a service in the list having the same path. */
  76. if (service->config.is_ephemeral) {
  77. goto end;
  78. }
  79. /* XXX: Validate if we have any service that has the given service dir path.
  80. * This has two problems:
  81. *
  82. * a) It's O(n^2), but the same comment from the bottom of
  83. * rend_config_services() should apply.
  84. *
  85. * b) We only compare directory paths as strings, so we can't
  86. * detect two distinct paths that specify the same directory
  87. * (which can arise from symlinks, case-insensitivity, bind
  88. * mounts, etc.).
  89. *
  90. * It also can't detect that two separate Tor instances are trying
  91. * to use the same HiddenServiceDir; for that, we would need a
  92. * lock file. But this is enough to detect a simple mistake that
  93. * at least one person has actually made. */
  94. SMARTLIST_FOREACH_BEGIN(service_list, const hs_service_t *, s) {
  95. if (!strcmp(s->config.directory_path, service->config.directory_path)) {
  96. log_warn(LD_REND, "Another hidden service is already configured "
  97. "for directory %s",
  98. escaped(service->config.directory_path));
  99. ret = 1;
  100. goto end;
  101. }
  102. } SMARTLIST_FOREACH_END(s);
  103. end:
  104. return ret;
  105. }
  106. /* Helper function: Given an configuration option name, its value, a minimum
  107. * min and a maxium max, parse the value as a uint64_t. On success, ok is set
  108. * to 1 and ret is the parsed value. On error, ok is set to 0 and ret must be
  109. * ignored. This function logs both on error and success. */
  110. static uint64_t
  111. helper_parse_uint64(const char *opt, const char *value, uint64_t min,
  112. uint64_t max, int *ok)
  113. {
  114. uint64_t ret = 0;
  115. tor_assert(opt);
  116. tor_assert(value);
  117. tor_assert(ok);
  118. *ok = 0;
  119. ret = tor_parse_uint64(value, 10, min, max, ok, NULL);
  120. if (!*ok) {
  121. log_warn(LD_CONFIG, "%s must be between %" PRIu64 " and %"PRIu64
  122. ", not %s.",
  123. opt, min, max, value);
  124. goto err;
  125. }
  126. log_info(LD_CONFIG, "%s was parsed to %" PRIu64, opt, ret);
  127. err:
  128. return ret;
  129. }
  130. /* Return the service version by trying to learn it from the key on disk if
  131. * any. If nothing is found, the current service configured version is
  132. * returned. */
  133. static int
  134. config_learn_service_version(hs_service_t *service)
  135. {
  136. int version;
  137. tor_assert(service);
  138. version = hs_service_get_version_from_key(service);
  139. if (version < 0) {
  140. version = service->config.version;
  141. }
  142. return version;
  143. }
  144. /* Return true iff the given options starting at line_ for a hidden service
  145. * contains at least one invalid option. Each hidden service option don't
  146. * apply to all versions so this function can find out. The line_ MUST start
  147. * right after the HiddenServiceDir line of this service.
  148. *
  149. * This is mainly for usability so we can inform the user of any invalid
  150. * option for the hidden service version instead of silently ignoring. */
  151. static int
  152. config_has_invalid_options(const config_line_t *line_,
  153. const hs_service_t *service)
  154. {
  155. int ret = 0;
  156. const char **optlist;
  157. const config_line_t *line;
  158. tor_assert(service);
  159. tor_assert(service->config.version <= HS_VERSION_MAX);
  160. /* List of options that a v3 service doesn't support thus must exclude from
  161. * its configuration. */
  162. const char *opts_exclude_v3[] = {
  163. "HiddenServiceAuthorizeClient",
  164. NULL /* End marker. */
  165. };
  166. /* Defining the size explicitly allows us to take advantage of the compiler
  167. * which warns us if we ever bump the max version but forget to grow this
  168. * array. The plus one is because we have a version 0 :). */
  169. struct {
  170. const char **list;
  171. } exclude_lists[HS_VERSION_MAX + 1] = {
  172. { NULL }, /* v0. */
  173. { NULL }, /* v1. */
  174. { NULL }, /* v2 */
  175. { opts_exclude_v3 }, /* v3. */
  176. };
  177. optlist = exclude_lists[service->config.version].list;
  178. if (optlist == NULL) {
  179. /* No exclude options to look at for this version. */
  180. goto end;
  181. }
  182. for (int i = 0; optlist[i]; i++) {
  183. const char *opt = optlist[i];
  184. for (line = line_; line; line = line->next) {
  185. if (!strcasecmp(line->key, "HiddenServiceDir")) {
  186. /* We just hit the next hidden service, stop right now. */
  187. goto end;
  188. }
  189. if (!strcasecmp(line->key, opt)) {
  190. log_warn(LD_CONFIG, "Hidden service option %s is incompatible with "
  191. "version %" PRIu32 " of service in %s",
  192. opt, service->config.version,
  193. service->config.directory_path);
  194. ret = 1;
  195. /* Continue the loop so we can find all possible options. */
  196. continue;
  197. }
  198. }
  199. }
  200. end:
  201. return ret;
  202. }
  203. /* Validate service configuration. This is used when loading the configuration
  204. * and once we've setup a service object, it's config object is passed to this
  205. * function for further validation. This does not validate service key
  206. * material. Return 0 if valid else -1 if invalid. */
  207. static int
  208. config_validate_service(const hs_service_config_t *config)
  209. {
  210. tor_assert(config);
  211. /* Amount of ports validation. */
  212. if (!config->ports || smartlist_len(config->ports) == 0) {
  213. log_warn(LD_CONFIG, "Hidden service (%s) with no ports configured.",
  214. escaped(config->directory_path));
  215. goto invalid;
  216. }
  217. /* Valid. */
  218. return 0;
  219. invalid:
  220. return -1;
  221. }
  222. /* Configuration funcion for a version 3 service. The line_ must be pointing
  223. * to the directive directly after a HiddenServiceDir. That way, when hitting
  224. * the next HiddenServiceDir line or reaching the end of the list of lines, we
  225. * know that we have to stop looking for more options. The given service
  226. * object must be already allocated and passed through
  227. * config_generic_service() prior to calling this function.
  228. *
  229. * Return 0 on success else a negative value. */
  230. static int
  231. config_service_v3(const config_line_t *line_,
  232. hs_service_config_t *config)
  233. {
  234. int have_num_ip = 0;
  235. const char *dup_opt_seen = NULL;
  236. const config_line_t *line;
  237. tor_assert(config);
  238. for (line = line_; line; line = line->next) {
  239. int ok = 0;
  240. if (!strcasecmp(line->key, "HiddenServiceDir")) {
  241. /* We just hit the next hidden service, stop right now. */
  242. break;
  243. }
  244. /* Number of introduction points. */
  245. if (!strcasecmp(line->key, "HiddenServiceNumIntroductionPoints")) {
  246. config->num_intro_points =
  247. (unsigned int) helper_parse_uint64(line->key, line->value,
  248. NUM_INTRO_POINTS_DEFAULT,
  249. HS_CONFIG_V3_MAX_INTRO_POINTS,
  250. &ok);
  251. if (!ok || have_num_ip) {
  252. if (have_num_ip)
  253. dup_opt_seen = line->key;
  254. goto err;
  255. }
  256. have_num_ip = 1;
  257. continue;
  258. }
  259. }
  260. /* We do not load the key material for the service at this stage. This is
  261. * done later once tor can confirm that it is in a running state. */
  262. /* We are about to return a fully configured service so do one last pass of
  263. * validation at it. */
  264. if (config_validate_service(config) < 0) {
  265. goto err;
  266. }
  267. return 0;
  268. err:
  269. if (dup_opt_seen) {
  270. log_warn(LD_CONFIG, "Duplicate directive %s.", dup_opt_seen);
  271. }
  272. return -1;
  273. }
  274. /* Configure a service using the given options in line_ and options. This is
  275. * called for any service regardless of its version which means that all
  276. * directives in this function are generic to any service version. This
  277. * function will also check the validity of the service directory path.
  278. *
  279. * The line_ must be pointing to the directive directly after a
  280. * HiddenServiceDir. That way, when hitting the next HiddenServiceDir line or
  281. * reaching the end of the list of lines, we know that we have to stop looking
  282. * for more options.
  283. *
  284. * Return 0 on success else -1. */
  285. static int
  286. config_generic_service(const config_line_t *line_,
  287. const or_options_t *options,
  288. hs_service_t *service)
  289. {
  290. int dir_seen = 0;
  291. const config_line_t *line;
  292. hs_service_config_t *config;
  293. /* If this is set, we've seen a duplicate of this option. Keep the string
  294. * so we can log the directive. */
  295. const char *dup_opt_seen = NULL;
  296. /* These variables will tell us if we ever have duplicate. */
  297. int have_version = 0, have_allow_unknown_ports = 0;
  298. int have_dir_group_read = 0, have_max_streams = 0;
  299. int have_max_streams_close = 0;
  300. tor_assert(line_);
  301. tor_assert(options);
  302. tor_assert(service);
  303. /* Makes thing easier. */
  304. config = &service->config;
  305. /* The first line starts with HiddenServiceDir so we consider what's next is
  306. * the configuration of the service. */
  307. for (line = line_; line ; line = line->next) {
  308. int ok = 0;
  309. /* This indicate that we have a new service to configure. */
  310. if (!strcasecmp(line->key, "HiddenServiceDir")) {
  311. /* This function only configures one service at a time so if we've
  312. * already seen one, stop right now. */
  313. if (dir_seen) {
  314. break;
  315. }
  316. /* Ok, we've seen one and we are about to configure it. */
  317. dir_seen = 1;
  318. config->directory_path = tor_strdup(line->value);
  319. log_info(LD_CONFIG, "HiddenServiceDir=%s. Configuring...",
  320. escaped(config->directory_path));
  321. continue;
  322. }
  323. if (BUG(!dir_seen)) {
  324. goto err;
  325. }
  326. /* Version of the service. */
  327. if (!strcasecmp(line->key, "HiddenServiceVersion")) {
  328. service->config.version =
  329. (uint32_t) helper_parse_uint64(line->key, line->value, HS_VERSION_MIN,
  330. HS_VERSION_MAX, &ok);
  331. if (!ok || have_version) {
  332. if (have_version)
  333. dup_opt_seen = line->key;
  334. goto err;
  335. }
  336. have_version = 1;
  337. continue;
  338. }
  339. /* Virtual port. */
  340. if (!strcasecmp(line->key, "HiddenServicePort")) {
  341. char *err_msg = NULL;
  342. /* XXX: Can we rename this? */
  343. rend_service_port_config_t *portcfg =
  344. rend_service_parse_port_config(line->value, " ", &err_msg);
  345. if (!portcfg) {
  346. if (err_msg) {
  347. log_warn(LD_CONFIG, "%s", err_msg);
  348. }
  349. tor_free(err_msg);
  350. goto err;
  351. }
  352. tor_assert(!err_msg);
  353. smartlist_add(config->ports, portcfg);
  354. log_info(LD_CONFIG, "HiddenServicePort=%s for %s",
  355. line->value, escaped(config->directory_path));
  356. continue;
  357. }
  358. /* Do we allow unknown ports. */
  359. if (!strcasecmp(line->key, "HiddenServiceAllowUnknownPorts")) {
  360. config->allow_unknown_ports =
  361. (unsigned int) helper_parse_uint64(line->key, line->value, 0, 1, &ok);
  362. if (!ok || have_allow_unknown_ports) {
  363. if (have_allow_unknown_ports)
  364. dup_opt_seen = line->key;
  365. goto err;
  366. }
  367. have_allow_unknown_ports = 1;
  368. continue;
  369. }
  370. /* Directory group readable. */
  371. if (!strcasecmp(line->key, "HiddenServiceDirGroupReadable")) {
  372. config->dir_group_readable =
  373. (unsigned int) helper_parse_uint64(line->key, line->value, 0, 1, &ok);
  374. if (!ok || have_dir_group_read) {
  375. if (have_dir_group_read)
  376. dup_opt_seen = line->key;
  377. goto err;
  378. }
  379. have_dir_group_read = 1;
  380. continue;
  381. }
  382. /* Maximum streams per circuit. */
  383. if (!strcasecmp(line->key, "HiddenServiceMaxStreams")) {
  384. config->max_streams_per_rdv_circuit =
  385. helper_parse_uint64(line->key, line->value, 0,
  386. HS_CONFIG_MAX_STREAMS_PER_RDV_CIRCUIT, &ok);
  387. if (!ok || have_max_streams) {
  388. if (have_max_streams)
  389. dup_opt_seen = line->key;
  390. goto err;
  391. }
  392. have_max_streams = 1;
  393. continue;
  394. }
  395. /* Maximum amount of streams before we close the circuit. */
  396. if (!strcasecmp(line->key, "HiddenServiceMaxStreamsCloseCircuit")) {
  397. config->max_streams_close_circuit =
  398. (unsigned int) helper_parse_uint64(line->key, line->value, 0, 1, &ok);
  399. if (!ok || have_max_streams_close) {
  400. if (have_max_streams_close)
  401. dup_opt_seen = line->key;
  402. goto err;
  403. }
  404. have_max_streams_close = 1;
  405. continue;
  406. }
  407. }
  408. /* Check if we are configured in non anonymous mode meaning every service
  409. * becomes a single onion service. */
  410. if (rend_service_non_anonymous_mode_enabled(options)) {
  411. config->is_single_onion = 1;
  412. /* We will add support for IPv6-only v3 single onion services in a future
  413. * Tor version. This won't catch "ReachableAddresses reject *4", but that
  414. * option doesn't work anyway. */
  415. if (options->ClientUseIPv4 == 0 && config->version == HS_VERSION_THREE) {
  416. log_warn(LD_CONFIG, "IPv6-only v3 single onion services are not "
  417. "supported. Set HiddenServiceSingleHopMode 0 and "
  418. "HiddenServiceNonAnonymousMode 0, or set ClientUseIPv4 1.");
  419. goto err;
  420. }
  421. }
  422. /* Success */
  423. return 0;
  424. err:
  425. if (dup_opt_seen) {
  426. log_warn(LD_CONFIG, "Duplicate directive %s.", dup_opt_seen);
  427. }
  428. return -1;
  429. }
  430. /* Configure a service using the given line and options. This function will
  431. * call the corresponding configuration function for a specific service
  432. * version and validate the service against the other ones. On success, add
  433. * the service to the given list and return 0. On error, nothing is added to
  434. * the list and a negative value is returned. */
  435. static int
  436. config_service(const config_line_t *line, const or_options_t *options,
  437. smartlist_t *service_list)
  438. {
  439. int ret;
  440. hs_service_t *service = NULL;
  441. tor_assert(line);
  442. tor_assert(options);
  443. tor_assert(service_list);
  444. /* We have a new hidden service. */
  445. service = hs_service_new(options);
  446. /* We'll configure that service as a generic one and then pass it to a
  447. * specific function according to the configured version number. */
  448. if (config_generic_service(line, options, service) < 0) {
  449. goto err;
  450. }
  451. tor_assert(service->config.version <= HS_VERSION_MAX);
  452. /* Before we configure the service on a per-version basis, we'll make
  453. * sure that this set of options for a service are valid that is for
  454. * instance an option only for v2 is not used for v3. */
  455. if (config_has_invalid_options(line->next, service)) {
  456. goto err;
  457. }
  458. /* Check permission on service directory that was just parsed. And this must
  459. * be done regardless of the service version. Do not ask for the directory
  460. * to be created, this is done when the keys are loaded because we could be
  461. * in validation mode right now. */
  462. if (hs_check_service_private_dir(options->User,
  463. service->config.directory_path,
  464. service->config.dir_group_readable,
  465. 0) < 0) {
  466. goto err;
  467. }
  468. /* We'll try to learn the service version here by loading the key(s) if
  469. * present. Depending on the key format, we can figure out the service
  470. * version. If we can't find a key, the configuration version will be used
  471. * which has been set previously. */
  472. service->config.version = config_learn_service_version(service);
  473. /* Different functions are in charge of specific options for a version. We
  474. * start just after the service directory line so once we hit another
  475. * directory line, the function knows that it has to stop parsing. */
  476. switch (service->config.version) {
  477. case HS_VERSION_TWO:
  478. ret = rend_config_service(line->next, options, &service->config);
  479. break;
  480. case HS_VERSION_THREE:
  481. ret = config_service_v3(line->next, &service->config);
  482. break;
  483. default:
  484. /* We do validate before if we support the parsed version. */
  485. tor_assert_nonfatal_unreached();
  486. goto err;
  487. }
  488. if (ret < 0) {
  489. goto err;
  490. }
  491. /* We'll check if this service can be kept depending on the others
  492. * configured previously. */
  493. if (service_is_duplicate_in_list(service_list, service)) {
  494. goto err;
  495. }
  496. /* Passes, add it to the given list. */
  497. smartlist_add(service_list, service);
  498. return 0;
  499. err:
  500. hs_service_free(service);
  501. return -1;
  502. }
  503. /* From a set of <b>options</b>, setup every hidden service found. Return 0 on
  504. * success or -1 on failure. If <b>validate_only</b> is set, parse, warn and
  505. * return as normal, but don't actually change the configured services. */
  506. int
  507. hs_config_service_all(const or_options_t *options, int validate_only)
  508. {
  509. int dir_option_seen = 0, ret = -1;
  510. const config_line_t *line;
  511. smartlist_t *new_service_list = NULL;
  512. tor_assert(options);
  513. /* Newly configured service are put in that list which is then used for
  514. * validation and staging for >= v3. */
  515. new_service_list = smartlist_new();
  516. for (line = options->RendConfigLines; line; line = line->next) {
  517. /* Ignore all directives that aren't the start of a service. */
  518. if (strcasecmp(line->key, "HiddenServiceDir")) {
  519. if (!dir_option_seen) {
  520. log_warn(LD_CONFIG, "%s with no preceding HiddenServiceDir directive",
  521. line->key);
  522. goto err;
  523. }
  524. continue;
  525. }
  526. /* Flag that we've seen a directory directive and we'll use it to make
  527. * sure that the torrc options ordering is actually valid. */
  528. dir_option_seen = 1;
  529. /* Try to configure this service now. On success, it will be added to the
  530. * list and validated against the service in that same list. */
  531. if (config_service(line, options, new_service_list) < 0) {
  532. goto err;
  533. }
  534. }
  535. /* In non validation mode, we'll stage those services we just successfully
  536. * configured. Service ownership is transferred from the list to the global
  537. * state. If any service is invalid, it will be removed from the list and
  538. * freed. All versions are handled in that function. */
  539. if (!validate_only) {
  540. stage_services(new_service_list);
  541. } else {
  542. /* We've just validated that we were able to build a clean working list of
  543. * services. We don't need those objects anymore. */
  544. SMARTLIST_FOREACH(new_service_list, hs_service_t *, s,
  545. hs_service_free(s));
  546. /* For the v2 subsystem, the configuration function adds the service
  547. * object to the staging list and it is transferred in the main list
  548. * through the prunning process. In validation mode, we thus have to purge
  549. * the staging list so it's not kept in memory as valid service. */
  550. rend_service_free_staging_list();
  551. }
  552. /* Success. Note that the service list has no ownership of its content. */
  553. ret = 0;
  554. goto end;
  555. err:
  556. SMARTLIST_FOREACH(new_service_list, hs_service_t *, s, hs_service_free(s));
  557. end:
  558. smartlist_free(new_service_list);
  559. /* Tor main should call the free all function on error. */
  560. return ret;
  561. }