hs_config.c 25 KB

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