hs_config.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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. /* Return true iff the given options starting at line_ for a hidden service
  106. * contains at least one invalid option. Each hidden service option don't
  107. * apply to all versions so this function can find out. The line_ MUST start
  108. * right after the HiddenServiceDir line of this service.
  109. *
  110. * This is mainly for usability so we can inform the user of any invalid
  111. * option for the hidden service version instead of silently ignoring. */
  112. static int
  113. config_has_invalid_options(const config_line_t *line_,
  114. const hs_service_t *service)
  115. {
  116. int ret = 0;
  117. const char **optlist;
  118. const config_line_t *line;
  119. tor_assert(service);
  120. tor_assert(service->version <= HS_VERSION_MAX);
  121. /* List of options that a v3 service doesn't support thus must exclude from
  122. * its configuration. */
  123. const char *opts_exclude_v3[] = {
  124. "HiddenServiceAuthorizeClient",
  125. NULL /* End marker. */
  126. };
  127. /* Defining the size explicitly allows us to take advantage of the compiler
  128. * which warns us if we ever bump the max version but forget to grow this
  129. * array. The plus one is because we have a version 0 :). */
  130. struct {
  131. const char **list;
  132. } exclude_lists[HS_VERSION_MAX + 1] = {
  133. { NULL }, /* v0. */
  134. { NULL }, /* v1. */
  135. { NULL }, /* v2 */
  136. { opts_exclude_v3 }, /* v3. */
  137. };
  138. optlist = exclude_lists[service->version].list;
  139. if (optlist == NULL) {
  140. /* No exclude options to look at for this version. */
  141. goto end;
  142. }
  143. for (int i = 0; optlist[i]; i++) {
  144. const char *opt = optlist[i];
  145. for (line = line_; line; line = line->next) {
  146. if (!strcasecmp(line->key, "HiddenServiceDir")) {
  147. /* We just hit the next hidden service, stop right now. */
  148. goto end;
  149. }
  150. if (!strcasecmp(line->key, opt)) {
  151. log_warn(LD_CONFIG, "Hidden service option %s is incompatible with "
  152. "version %" PRIu32 " of service in %s",
  153. opt, service->version, service->config.directory_path);
  154. ret = 1;
  155. /* Continue the loop so we can find all possible options. */
  156. continue;
  157. }
  158. }
  159. }
  160. end:
  161. return ret;
  162. }
  163. /* Validate service configuration. This is used when loading the configuration
  164. * and once we've setup a service object, it's config object is passed to this
  165. * function for further validation. This does not validate service key
  166. * material. Return 0 if valid else -1 if invalid. */
  167. static int
  168. config_validate_service(const hs_service_config_t *config)
  169. {
  170. tor_assert(config);
  171. /* Amount of ports validation. */
  172. if (!config->ports || smartlist_len(config->ports) == 0) {
  173. log_warn(LD_CONFIG, "Hidden service (%s) with no ports configured.",
  174. escaped(config->directory_path));
  175. goto invalid;
  176. }
  177. /* Valid. */
  178. return 0;
  179. invalid:
  180. return -1;
  181. }
  182. /* Configuration handler for a version 3 service. The line_ must be pointing
  183. * to the directive directly after a HiddenServiceDir. That way, when hitting
  184. * the next HiddenServiceDir line or reaching the end of the list of lines, we
  185. * know that we have to stop looking for more options. The given service
  186. * object must be already allocated and passed through
  187. * config_generic_service() prior to calling this function.
  188. *
  189. * Return 0 on success else a negative value. */
  190. static int
  191. config_service_v3(const config_line_t *line_,
  192. const or_options_t *options,
  193. hs_service_t *service)
  194. {
  195. (void) options;
  196. const config_line_t *line;
  197. hs_service_config_t *config;
  198. tor_assert(service);
  199. config = &service->config;
  200. for (line = line_; line; line = line->next) {
  201. if (!strcasecmp(line->key, "HiddenServiceDir")) {
  202. /* We just hit the next hidden service, stop right now. */
  203. break;
  204. }
  205. /* Number of introduction points. */
  206. if (!strcasecmp(line->key, "HiddenServiceNumIntroductionPoints")) {
  207. int ok = 0;
  208. config->num_intro_points =
  209. (unsigned int) tor_parse_ulong(line->value, 10,
  210. NUM_INTRO_POINTS_DEFAULT,
  211. HS_CONFIG_V3_MAX_INTRO_POINTS,
  212. &ok, NULL);
  213. if (!ok) {
  214. log_warn(LD_CONFIG, "HiddenServiceNumIntroductionPoints "
  215. "should be between %d and %d, not %s",
  216. NUM_INTRO_POINTS_DEFAULT, HS_CONFIG_V3_MAX_INTRO_POINTS,
  217. line->value);
  218. goto err;
  219. }
  220. log_info(LD_CONFIG, "HiddenServiceNumIntroductionPoints=%d for %s",
  221. config->num_intro_points, escaped(config->directory_path));
  222. continue;
  223. }
  224. }
  225. /* We do not load the key material for the service at this stage. This is
  226. * done later once tor can confirm that it is in a running state. */
  227. /* We are about to return a fully configured service so do one last pass of
  228. * validation at it. */
  229. if (config_validate_service(config) < 0) {
  230. goto err;
  231. }
  232. return 0;
  233. err:
  234. return -1;
  235. }
  236. /* Configure a service using the given options in line_ and options. This is
  237. * called for any service regardless of its version which means that all
  238. * directives in this function are generic to any service version. This
  239. * function will also check the validity of the service directory path.
  240. *
  241. * The line_ must be pointing to the directive directly after a
  242. * HiddenServiceDir. That way, when hitting the next HiddenServiceDir line or
  243. * reaching the end of the list of lines, we know that we have to stop looking
  244. * for more options.
  245. *
  246. * Return 0 on success else -1. */
  247. static int
  248. config_generic_service(const config_line_t *line_,
  249. const or_options_t *options,
  250. hs_service_t *service)
  251. {
  252. int ok, dir_seen = 0;
  253. const config_line_t *line;
  254. hs_service_config_t *config;
  255. tor_assert(line_);
  256. tor_assert(options);
  257. tor_assert(service);
  258. /* Makes thing easier. */
  259. config = &service->config;
  260. memset(config, 0, sizeof(*config));
  261. /* The first line starts with HiddenServiceDir so we consider what's next is
  262. * the configuration of the service. */
  263. for (line = line_; line ; line = line->next) {
  264. /* This indicate that we have a new service to configure. */
  265. if (!strcasecmp(line->key, "HiddenServiceDir")) {
  266. /* This function only configures one service at a time so if we've
  267. * already seen one, stop right now. */
  268. if (dir_seen) {
  269. break;
  270. }
  271. /* Ok, we've seen one and we are about to configure it. */
  272. dir_seen = 1;
  273. config->directory_path = tor_strdup(line->value);
  274. log_info(LD_CONFIG, "HiddenServiceDir=%s. Configuring...",
  275. escaped(config->directory_path));
  276. continue;
  277. }
  278. if (BUG(!dir_seen)) {
  279. goto err;
  280. }
  281. /* Version of the service. */
  282. if (!strcasecmp(line->key, "HiddenServiceVersion")) {
  283. service->version = (uint32_t) tor_parse_ulong(line->value,
  284. 10, HS_VERSION_MIN,
  285. HS_VERSION_MAX,
  286. &ok, NULL);
  287. if (!ok) {
  288. log_warn(LD_CONFIG,
  289. "HiddenServiceVersion be between %u and %u, not %s",
  290. HS_VERSION_TWO, HS_VERSION_MAX, line->value);
  291. goto err;
  292. }
  293. log_info(LD_CONFIG, "HiddenServiceVersion=%" PRIu32 " for %s",
  294. service->version, escaped(config->directory_path));
  295. continue;
  296. }
  297. /* Virtual port. */
  298. if (!strcasecmp(line->key, "HiddenServicePort")) {
  299. char *err_msg = NULL;
  300. /* XXX: Can we rename this? */
  301. rend_service_port_config_t *portcfg =
  302. rend_service_parse_port_config(line->value, " ", &err_msg);
  303. if (!portcfg) {
  304. if (err_msg) {
  305. log_warn(LD_CONFIG, "%s", err_msg);
  306. }
  307. tor_free(err_msg);
  308. goto err;
  309. }
  310. tor_assert(!err_msg);
  311. smartlist_add(config->ports, portcfg);
  312. log_info(LD_CONFIG, "HiddenServicePort=%s for %s",
  313. line->value, escaped(config->directory_path));
  314. continue;
  315. }
  316. /* Do we allow unknown ports. */
  317. if (!strcasecmp(line->key, "HiddenServiceAllowUnknownPorts")) {
  318. config->allow_unknown_ports = (unsigned int) tor_parse_long(line->value,
  319. 10, 0, 1,
  320. &ok, NULL);
  321. if (!ok) {
  322. log_warn(LD_CONFIG,
  323. "HiddenServiceAllowUnknownPorts should be 0 or 1, not %s",
  324. line->value);
  325. goto err;
  326. }
  327. log_info(LD_CONFIG,
  328. "HiddenServiceAllowUnknownPorts=%u for %s",
  329. config->allow_unknown_ports, escaped(config->directory_path));
  330. continue;
  331. }
  332. /* Directory group readable. */
  333. if (!strcasecmp(line->key, "HiddenServiceDirGroupReadable")) {
  334. config->dir_group_readable = (unsigned int) tor_parse_long(line->value,
  335. 10, 0, 1,
  336. &ok, NULL);
  337. if (!ok) {
  338. log_warn(LD_CONFIG,
  339. "HiddenServiceDirGroupReadable should be 0 or 1, not %s",
  340. line->value);
  341. goto err;
  342. }
  343. log_info(LD_CONFIG,
  344. "HiddenServiceDirGroupReadable=%u for %s",
  345. config->dir_group_readable, escaped(config->directory_path));
  346. continue;
  347. }
  348. /* Maximum streams per circuit. */
  349. if (!strcasecmp(line->key, "HiddenServiceMaxStreams")) {
  350. config->max_streams_per_rdv_circuit =
  351. tor_parse_uint64(line->value, 10, 0,
  352. HS_CONFIG_MAX_STREAMS_PER_RDV_CIRCUIT, &ok, NULL);
  353. if (!ok) {
  354. log_warn(LD_CONFIG,
  355. "HiddenServiceMaxStreams should be between 0 and %d, not %s",
  356. HS_CONFIG_MAX_STREAMS_PER_RDV_CIRCUIT, line->value);
  357. goto err;
  358. }
  359. log_info(LD_CONFIG,
  360. "HiddenServiceMaxStreams=%" PRIu64 " for %s",
  361. config->max_streams_per_rdv_circuit,
  362. escaped(config->directory_path));
  363. continue;
  364. }
  365. /* Maximum amount of streams before we close the circuit. */
  366. if (!strcasecmp(line->key, "HiddenServiceMaxStreamsCloseCircuit")) {
  367. config->max_streams_close_circuit =
  368. (unsigned int) tor_parse_long(line->value, 10, 0, 1, &ok, NULL);
  369. if (!ok) {
  370. log_warn(LD_CONFIG,
  371. "HiddenServiceMaxStreamsCloseCircuit should be 0 or 1, "
  372. "not %s", line->value);
  373. goto err;
  374. }
  375. log_info(LD_CONFIG,
  376. "HiddenServiceMaxStreamsCloseCircuit=%u for %s",
  377. config->max_streams_close_circuit,
  378. escaped(config->directory_path));
  379. continue;
  380. }
  381. }
  382. /* Check if we are configured in non anonymous mode and single hop mode
  383. * meaning every service become single onion. */
  384. if (rend_service_allow_non_anonymous_connection(options) &&
  385. rend_service_non_anonymous_mode_enabled(options)) {
  386. config->is_single_onion = 1;
  387. }
  388. /* Success */
  389. return 0;
  390. err:
  391. return -1;
  392. }
  393. /* Configuration handler indexed by version number. */
  394. static int
  395. (*config_service_handlers[])(const config_line_t *line,
  396. const or_options_t *options,
  397. hs_service_t *service) =
  398. {
  399. NULL, /* v0 */
  400. NULL, /* v1 */
  401. rend_config_service, /* v2 */
  402. config_service_v3, /* v3 */
  403. };
  404. /* Configure a service using the given line and options. This function will
  405. * call the corresponding version handler and validate the service against the
  406. * other one. On success, add the service to the given list and return 0. On
  407. * error, nothing is added to the list and a negative value is returned. */
  408. static int
  409. config_service(const config_line_t *line, const or_options_t *options,
  410. smartlist_t *service_list)
  411. {
  412. hs_service_t *service = NULL;
  413. tor_assert(line);
  414. tor_assert(options);
  415. tor_assert(service_list);
  416. /* We have a new hidden service. */
  417. service = hs_service_new(options);
  418. /* We'll configure that service as a generic one and then pass it to the
  419. * specific handler according to the configured version number. */
  420. if (config_generic_service(line, options, service) < 0) {
  421. goto err;
  422. }
  423. tor_assert(service->version <= HS_VERSION_MAX);
  424. /* Before we configure the service with the per-version handler, we'll make
  425. * sure that this set of options for a service are valid that is for
  426. * instance an option only for v2 is not used for v3. */
  427. if (config_has_invalid_options(line->next, service)) {
  428. goto err;
  429. }
  430. /* Check permission on service directory that was just parsed. And this must
  431. * be done regardless of the service version. Do not ask for the directory
  432. * to be created, this is done when the keys are loaded because we could be
  433. * in validation mode right now. */
  434. if (hs_check_service_private_dir(options->User,
  435. service->config.directory_path,
  436. service->config.dir_group_readable,
  437. 0) < 0) {
  438. goto err;
  439. }
  440. /* The handler is in charge of specific options for a version. We start
  441. * after this service directory line so once we hit another directory
  442. * line, the handler knows that it has to stop. */
  443. if (config_service_handlers[service->version](line->next, options,
  444. service) < 0) {
  445. goto err;
  446. }
  447. /* We'll check if this service can be kept depending on the others
  448. * configured previously. */
  449. if (service_is_duplicate_in_list(service_list, service)) {
  450. goto err;
  451. }
  452. /* Passes, add it to the given list. */
  453. smartlist_add(service_list, service);
  454. return 0;
  455. err:
  456. hs_service_free(service);
  457. return -1;
  458. }
  459. /* From a set of <b>options</b>, setup every hidden service found. Return 0 on
  460. * success or -1 on failure. If <b>validate_only</b> is set, parse, warn and
  461. * return as normal, but don't actually change the configured services. */
  462. int
  463. hs_config_service_all(const or_options_t *options, int validate_only)
  464. {
  465. int dir_option_seen = 0, ret = -1;
  466. const config_line_t *line;
  467. smartlist_t *new_service_list = NULL;
  468. tor_assert(options);
  469. /* Newly configured service are put in that list which is then used for
  470. * validation and staging for >= v3. */
  471. new_service_list = smartlist_new();
  472. for (line = options->RendConfigLines; line; line = line->next) {
  473. /* Ignore all directives that aren't the start of a service. */
  474. if (strcasecmp(line->key, "HiddenServiceDir")) {
  475. if (!dir_option_seen) {
  476. log_warn(LD_CONFIG, "%s with no preceding HiddenServiceDir directive",
  477. line->key);
  478. goto err;
  479. }
  480. continue;
  481. }
  482. /* Flag that we've seen a directory directive and we'll use it to make
  483. * sure that the torrc options ordering is actually valid. */
  484. dir_option_seen = 1;
  485. /* Try to configure this service now. On success, it will be added to the
  486. * list and validated against the service in that same list. */
  487. if (config_service(line, options, new_service_list) < 0) {
  488. goto err;
  489. }
  490. }
  491. /* In non validation mode, we'll stage those services we just successfully
  492. * configured. Service ownership is transfered from the list to the global
  493. * state. If any service is invalid, it will be removed from the list and
  494. * freed. All versions are handled in that function. */
  495. if (!validate_only) {
  496. stage_services(new_service_list);
  497. } else {
  498. /* We've just validated that we were able to build a clean working list of
  499. * services. We don't need those objects anymore. */
  500. SMARTLIST_FOREACH(new_service_list, hs_service_t *, s,
  501. hs_service_free(s));
  502. /* For the v2 subsystem, the configuration handler adds the service object
  503. * to the staging list and it is transferred in the main list through the
  504. * prunning process. In validation mode, we thus have to purge the staging
  505. * list so it's not kept in memory as valid service. */
  506. rend_service_free_staging_list();
  507. }
  508. /* Success. Note that the service list has no ownership of its content. */
  509. ret = 0;
  510. goto end;
  511. err:
  512. SMARTLIST_FOREACH(new_service_list, hs_service_t *, s, hs_service_free(s));
  513. end:
  514. smartlist_free(new_service_list);
  515. /* Tor main should call the free all function on error. */
  516. return ret;
  517. }