hs_config.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. /* Configuration handler for a version 3 service. Return 0 on success else a
  31. * negative value. */
  32. static int
  33. config_service_v3(const config_line_t *line,
  34. const or_options_t *options, int validate_only,
  35. hs_service_t *service)
  36. {
  37. (void) line;
  38. (void) service;
  39. (void) validate_only;
  40. (void) options;
  41. /* XXX: Configure a v3 service with specific options. */
  42. /* XXX: Add service to v3 list and pruning on reload. */
  43. return 0;
  44. }
  45. /* Configure a service using the given options in line_ and options. This is
  46. * called for any service regardless of its version which means that all
  47. * directives in this function are generic to any service version. This
  48. * function will also check the validity of the service directory path.
  49. *
  50. * The line_ must be pointing to the directive directly after a
  51. * HiddenServiceDir. That way, when hitting the next HiddenServiceDir line or
  52. * reaching the end of the list of lines, we know that we have to stop looking
  53. * for more options.
  54. *
  55. * Return 0 on success else -1. */
  56. static int
  57. config_generic_service(const config_line_t *line_,
  58. const or_options_t *options,
  59. hs_service_t *service)
  60. {
  61. int ok, dir_seen = 0;
  62. const config_line_t *line;
  63. hs_service_config_t *config;
  64. tor_assert(line_);
  65. tor_assert(options);
  66. tor_assert(service);
  67. /* Makes thing easier. */
  68. config = &service->config;
  69. memset(config, 0, sizeof(*config));
  70. /* The first line starts with HiddenServiceDir so we consider what's next is
  71. * the configuration of the service. */
  72. for (line = line_; line ; line = line->next) {
  73. /* This indicate that we have a new service to configure. */
  74. if (!strcasecmp(line->key, "HiddenServiceDir")) {
  75. /* This function only configures one service at a time so if we've
  76. * already seen one, stop right now. */
  77. if (dir_seen) {
  78. break;
  79. }
  80. /* Ok, we've seen one and we are about to configure it. */
  81. dir_seen = 1;
  82. config->directory_path = tor_strdup(line->value);
  83. log_info(LD_CONFIG, "HiddenServiceDir=%s. Configuring...",
  84. escaped(config->directory_path));
  85. continue;
  86. }
  87. if (BUG(!dir_seen)) {
  88. goto err;
  89. }
  90. /* Version of the service. */
  91. if (!strcasecmp(line->key, "HiddenServiceVersion")) {
  92. service->version = (uint32_t) tor_parse_ulong(line->value,
  93. 10, HS_VERSION_TWO,
  94. HS_VERSION_MAX,
  95. &ok, NULL);
  96. if (!ok) {
  97. log_warn(LD_CONFIG,
  98. "HiddenServiceVersion be between %u and %u, not %s",
  99. HS_VERSION_TWO, HS_VERSION_MAX, line->value);
  100. goto err;
  101. }
  102. log_info(LD_CONFIG, "HiddenServiceVersion=%" PRIu32 " for %s",
  103. service->version, escaped(config->directory_path));
  104. continue;
  105. }
  106. /* Virtual port. */
  107. if (!strcasecmp(line->key, "HiddenServicePort")) {
  108. char *err_msg = NULL;
  109. /* XXX: Can we rename this? */
  110. rend_service_port_config_t *portcfg =
  111. rend_service_parse_port_config(line->value, " ", &err_msg);
  112. if (!portcfg) {
  113. if (err_msg) {
  114. log_warn(LD_CONFIG, "%s", err_msg);
  115. }
  116. tor_free(err_msg);
  117. goto err;
  118. }
  119. tor_assert(!err_msg);
  120. smartlist_add(config->ports, portcfg);
  121. log_info(LD_CONFIG, "HiddenServicePort=%s for %s",
  122. line->value, escaped(config->directory_path));
  123. continue;
  124. }
  125. /* Do we allow unknown ports. */
  126. if (!strcasecmp(line->key, "HiddenServiceAllowUnknownPorts")) {
  127. config->allow_unknown_ports = (unsigned int) tor_parse_long(line->value,
  128. 10, 0, 1,
  129. &ok, NULL);
  130. if (!ok) {
  131. log_warn(LD_CONFIG,
  132. "HiddenServiceAllowUnknownPorts should be 0 or 1, not %s",
  133. line->value);
  134. goto err;
  135. }
  136. log_info(LD_CONFIG,
  137. "HiddenServiceAllowUnknownPorts=%u for %s",
  138. config->allow_unknown_ports, escaped(config->directory_path));
  139. continue;
  140. }
  141. /* Directory group readable. */
  142. if (!strcasecmp(line->key, "HiddenServiceDirGroupReadable")) {
  143. config->dir_group_readable = (unsigned int) tor_parse_long(line->value,
  144. 10, 0, 1,
  145. &ok, NULL);
  146. if (!ok) {
  147. log_warn(LD_CONFIG,
  148. "HiddenServiceDirGroupReadable should be 0 or 1, not %s",
  149. line->value);
  150. goto err;
  151. }
  152. log_info(LD_CONFIG,
  153. "HiddenServiceDirGroupReadable=%u for %s",
  154. config->dir_group_readable, escaped(config->directory_path));
  155. continue;
  156. }
  157. /* Maximum streams per circuit. */
  158. if (!strcasecmp(line->key, "HiddenServiceMaxStreams")) {
  159. config->max_streams_per_rdv_circuit = tor_parse_uint64(line->value,
  160. 10, 0, 65535,
  161. &ok, NULL);
  162. if (!ok) {
  163. log_warn(LD_CONFIG,
  164. "HiddenServiceMaxStreams should be between 0 and %d, not %s",
  165. 65535, line->value);
  166. goto err;
  167. }
  168. log_info(LD_CONFIG,
  169. "HiddenServiceMaxStreams=%" PRIu64 " for %s",
  170. config->max_streams_per_rdv_circuit,
  171. escaped(config->directory_path));
  172. continue;
  173. }
  174. /* Maximum amount of streams before we close the circuit. */
  175. if (!strcasecmp(line->key, "HiddenServiceMaxStreamsCloseCircuit")) {
  176. config->max_streams_close_circuit =
  177. (unsigned int) tor_parse_long(line->value, 10, 0, 1, &ok, NULL);
  178. if (!ok) {
  179. log_warn(LD_CONFIG,
  180. "HiddenServiceMaxStreamsCloseCircuit should be 0 or 1, "
  181. "not %s", line->value);
  182. goto err;
  183. }
  184. log_info(LD_CONFIG,
  185. "HiddenServiceMaxStreamsCloseCircuit=%u for %s",
  186. config->max_streams_close_circuit,
  187. escaped(config->directory_path));
  188. continue;
  189. }
  190. }
  191. /* Check permission on service directory. */
  192. if (hs_check_service_private_dir(options->User, config->directory_path,
  193. config->dir_group_readable, 0) < 0) {
  194. goto err;
  195. }
  196. /* Check if we are configured in non anonymous mode and single hop mode
  197. * meaning every service become single onion. */
  198. if (rend_service_allow_non_anonymous_connection(options) &&
  199. rend_service_non_anonymous_mode_enabled(options)) {
  200. config->is_single_onion = 1;
  201. }
  202. /* Success */
  203. return 0;
  204. err:
  205. return -1;
  206. }
  207. /* Configuration handler indexed by version number. */
  208. static int
  209. (*config_service_handlers[])(const config_line_t *line,
  210. const or_options_t *options,
  211. int validate_only,
  212. hs_service_t *service) =
  213. {
  214. NULL, /* v0 */
  215. NULL, /* v1 */
  216. rend_config_service, /* v2 */
  217. config_service_v3, /* v3 */
  218. };
  219. /* From a set of <b>options</b>, setup every hidden service found. Return 0 on
  220. * success or -1 on failure. If <b>validate_only</b> is set, parse, warn and
  221. * return as normal, but don't actually change the configured services. */
  222. int
  223. hs_config_service_all(const or_options_t *options, int validate_only)
  224. {
  225. int dir_option_seen = 0;
  226. hs_service_t *service = NULL;
  227. const config_line_t *line;
  228. tor_assert(options);
  229. for (line = options->RendConfigLines; line; line = line->next) {
  230. if (!strcasecmp(line->key, "HiddenServiceDir")) {
  231. /* We have a new hidden service. */
  232. service = hs_service_new(options);
  233. /* We'll configure that service as a generic one and then pass it to the
  234. * specific handler according to the configured version number. */
  235. if (config_generic_service(line, options, service) < 0) {
  236. goto err;
  237. }
  238. tor_assert(service->version <= HS_VERSION_MAX);
  239. /* The handler is in charge of specific options for a version. We start
  240. * after this service directory line so once we hit another directory
  241. * line, the handler knows that it has to stop. */
  242. if (config_service_handlers[service->version](line->next, options,
  243. validate_only,
  244. service) < 0) {
  245. goto err;
  246. }
  247. /* Whatever happens, on success we loose the ownership of the service
  248. * object so we nullify the pointer to be safe. */
  249. service = NULL;
  250. /* Flag that we've seen a directory directive and we'll use that to make
  251. * sure that the torrc options ordering are actually valid. */
  252. dir_option_seen = 1;
  253. continue;
  254. }
  255. /* The first line must be a directory option else tor is misconfigured. */
  256. if (!dir_option_seen) {
  257. log_warn(LD_CONFIG, "%s with no preceding HiddenServiceDir directive",
  258. line->key);
  259. goto err;
  260. }
  261. }
  262. if (!validate_only) {
  263. /* Trigger service pruning which will make sure the just configured
  264. * services end up in the main global list. This is v2 specific. */
  265. rend_service_prune_list();
  266. /* XXX: Need the v3 one. */
  267. }
  268. /* Success. */
  269. return 0;
  270. err:
  271. hs_service_free(service);
  272. /* Tor main should call the free all function. */
  273. return -1;
  274. }