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