hs_config.c 20 KB

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