hs_config.c 18 KB

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