confparse.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2019, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file confparse.c
  8. *
  9. * \brief Back-end for parsing and generating key-value files, used to
  10. * implement the torrc file format and the state file.
  11. *
  12. * This module is used by config.c to parse and encode torrc
  13. * configuration files, and by statefile.c to parse and encode the
  14. * $DATADIR/state file.
  15. *
  16. * To use this module, its callers provide an instance of
  17. * config_format_t to describe the mappings from a set of configuration
  18. * options to a number of fields in a C structure. With this mapping,
  19. * the functions here can convert back and forth between the C structure
  20. * specified, and a linked list of key-value pairs.
  21. */
  22. #define CONFPARSE_PRIVATE
  23. #include "orconfig.h"
  24. #include "app/config/confparse.h"
  25. #include "lib/confmgt/structvar.h"
  26. #include "lib/confmgt/unitparse.h"
  27. #include "lib/container/bitarray.h"
  28. #include "lib/container/smartlist.h"
  29. #include "lib/encoding/confline.h"
  30. #include "lib/log/escape.h"
  31. #include "lib/log/log.h"
  32. #include "lib/log/util_bug.h"
  33. #include "lib/string/compat_ctype.h"
  34. #include "lib/string/printf.h"
  35. #include "lib/string/util_string.h"
  36. #include "ext/siphash.h"
  37. /**
  38. * A managed_var_t is an internal wrapper around a config_var_t in
  39. * a config_format_t structure. It is used by config_mgr_t to
  40. * keep track of which option goes with which structure. */
  41. typedef struct managed_var_t {
  42. /**
  43. * A pointer to the config_var_t for this option.
  44. */
  45. const config_var_t *cvar;
  46. /**
  47. * The index of the object in which this option is stored. It is
  48. * IDX_TOPLEVEL to indicate that the object is the top-level object.
  49. **/
  50. int object_idx;
  51. } managed_var_t;
  52. static void config_reset(const config_mgr_t *fmt, void *options,
  53. const managed_var_t *var, int use_defaults);
  54. static void config_mgr_register_fmt(config_mgr_t *mgr,
  55. const config_format_t *fmt,
  56. int object_idx);
  57. /** Release all storage held in a managed_var_t. */
  58. static void
  59. managed_var_free_(managed_var_t *mv)
  60. {
  61. if (!mv)
  62. return;
  63. tor_free(mv);
  64. }
  65. #define managed_var_free(mv) \
  66. FREE_AND_NULL(managed_var_t, managed_var_free_, (mv))
  67. struct config_suite_t {
  68. /** A list of configuration objects managed by a given configuration
  69. * manager. They are stored in the same order as the config_format_t
  70. * objects in the manager's list of subformats. */
  71. smartlist_t *configs;
  72. };
  73. /**
  74. * Allocate a new empty config_suite_t.
  75. **/
  76. static config_suite_t *
  77. config_suite_new(void)
  78. {
  79. config_suite_t *suite = tor_malloc_zero(sizeof(config_suite_t));
  80. suite->configs = smartlist_new();
  81. return suite;
  82. }
  83. /** Release all storage held by a config_suite_t. (Does not free
  84. * any configuration objects it holds; the caller must do that first.) */
  85. static void
  86. config_suite_free_(config_suite_t *suite)
  87. {
  88. if (!suite)
  89. return;
  90. smartlist_free(suite->configs);
  91. tor_free(suite);
  92. }
  93. #define config_suite_free(suite) \
  94. FREE_AND_NULL(config_suite_t, config_suite_free_, (suite))
  95. struct config_mgr_t {
  96. /** The 'top-level' configuration format. This one is used for legacy
  97. * options that have not yet been assigned to different sub-modules.
  98. *
  99. * (NOTE: for now, this is the only config_format_t that a config_mgr_t
  100. * contains. A subsequent commit will add more. XXXX)
  101. */
  102. const config_format_t *toplevel;
  103. /**
  104. * List of second-level configuration format objects that this manager
  105. * also knows about.
  106. */
  107. smartlist_t *subconfigs;
  108. /** A smartlist of managed_var_t objects for all configuration formats. */
  109. smartlist_t *all_vars;
  110. /** A smartlist of config_abbrev_t objects for all configuration
  111. * formats. These objects are used to track synonyms and abbreviations for
  112. * different configuration options. */
  113. smartlist_t *all_abbrevs;
  114. /** A smartlist of config_deprecation_t for all configuration formats. */
  115. smartlist_t *all_deprecations;
  116. /** True if this manager has been frozen and cannot have any more formats
  117. * added to it. A manager must be frozen before it can be used to construct
  118. * or manipulate objects. */
  119. bool frozen;
  120. /** A replacement for the magic number of the toplevel object. We override
  121. * that number to make it unique for this particular config_mgr_t, so that
  122. * an object constructed with one mgr can't be used with another, even if
  123. * those managers' contents are equal.
  124. */
  125. struct_magic_decl_t toplevel_magic;
  126. };
  127. #define IDX_TOPLEVEL (-1)
  128. /** Create a new config_mgr_t to manage a set of configuration objects to be
  129. * wrapped under <b>toplevel_fmt</b>. */
  130. config_mgr_t *
  131. config_mgr_new(const config_format_t *toplevel_fmt)
  132. {
  133. config_mgr_t *mgr = tor_malloc_zero(sizeof(config_mgr_t));
  134. mgr->subconfigs = smartlist_new();
  135. mgr->all_vars = smartlist_new();
  136. mgr->all_abbrevs = smartlist_new();
  137. mgr->all_deprecations = smartlist_new();
  138. config_mgr_register_fmt(mgr, toplevel_fmt, IDX_TOPLEVEL);
  139. mgr->toplevel = toplevel_fmt;
  140. return mgr;
  141. }
  142. /** Add a config_format_t to a manager, with a specified (unique) index. */
  143. static void
  144. config_mgr_register_fmt(config_mgr_t *mgr,
  145. const config_format_t *fmt,
  146. int object_idx)
  147. {
  148. int i;
  149. tor_assertf(!mgr->frozen,
  150. "Tried to add a format to a configuration manager after "
  151. "it had been frozen.");
  152. if (object_idx != IDX_TOPLEVEL) {
  153. tor_assertf(fmt->config_suite_offset < 0,
  154. "Tried to register a toplevel format in a non-toplevel position");
  155. }
  156. tor_assertf(fmt != mgr->toplevel &&
  157. ! smartlist_contains(mgr->subconfigs, fmt),
  158. "Tried to register an already-registered format.");
  159. /* register variables */
  160. for (i = 0; fmt->vars[i].member.name; ++i) {
  161. managed_var_t *mv = tor_malloc_zero(sizeof(managed_var_t));
  162. mv->cvar = &fmt->vars[i];
  163. mv->object_idx = object_idx;
  164. smartlist_add(mgr->all_vars, mv);
  165. }
  166. /* register abbrevs */
  167. if (fmt->abbrevs) {
  168. for (i = 0; fmt->abbrevs[i].abbreviated; ++i) {
  169. smartlist_add(mgr->all_abbrevs, (void*)&fmt->abbrevs[i]);
  170. }
  171. }
  172. /* register deprecations. */
  173. if (fmt->deprecations) {
  174. const config_deprecation_t *d;
  175. for (d = fmt->deprecations; d->name; ++d) {
  176. smartlist_add(mgr->all_deprecations, (void*)d);
  177. }
  178. }
  179. }
  180. /**
  181. * Add a new format to this configuration object. Asserts on failure.
  182. *
  183. **/
  184. int
  185. config_mgr_add_format(config_mgr_t *mgr,
  186. const config_format_t *fmt)
  187. {
  188. tor_assert(mgr);
  189. int idx = smartlist_len(mgr->subconfigs);
  190. config_mgr_register_fmt(mgr, fmt, idx);
  191. smartlist_add(mgr->subconfigs, (void *)fmt);
  192. return idx;
  193. }
  194. /** Return a pointer to the config_suite_t * pointer inside a
  195. * configuration object; returns NULL if there is no such member. */
  196. static inline config_suite_t **
  197. config_mgr_get_suite_ptr(const config_mgr_t *mgr, void *toplevel)
  198. {
  199. if (mgr->toplevel->config_suite_offset < 0)
  200. return NULL;
  201. return STRUCT_VAR_P(toplevel, mgr->toplevel->config_suite_offset);
  202. }
  203. /**
  204. * Return a pointer to the configuration object within <b>toplevel</b> whose
  205. * index is <b>idx</b>.
  206. *
  207. * NOTE: XXXX Eventually, there will be multiple objects supported within the
  208. * toplevel object. For example, the or_options_t will contain pointers
  209. * to configuration objects for other modules. This function gets
  210. * the sub-object for a particular module.
  211. */
  212. STATIC void *
  213. config_mgr_get_obj_mutable(const config_mgr_t *mgr, void *toplevel, int idx)
  214. {
  215. tor_assert(mgr);
  216. tor_assert(toplevel);
  217. if (idx == IDX_TOPLEVEL)
  218. return toplevel;
  219. tor_assertf(idx >= 0 && idx < smartlist_len(mgr->subconfigs),
  220. "Index %d is out of range.", idx);
  221. config_suite_t **suite = config_mgr_get_suite_ptr(mgr, toplevel);
  222. tor_assert(suite);
  223. tor_assert(smartlist_len(mgr->subconfigs) ==
  224. smartlist_len((*suite)->configs));
  225. return smartlist_get((*suite)->configs, idx);
  226. }
  227. /** As config_mgr_get_obj_mutable(), but return a const pointer. */
  228. STATIC const void *
  229. config_mgr_get_obj(const config_mgr_t *mgr, const void *toplevel, int idx)
  230. {
  231. return config_mgr_get_obj_mutable(mgr, (void*)toplevel, idx);
  232. }
  233. /** Sorting helper for smartlist of managed_var_t */
  234. static int
  235. managed_var_cmp(const void **a, const void **b)
  236. {
  237. const managed_var_t *mv1 = *(const managed_var_t**)a;
  238. const managed_var_t *mv2 = *(const managed_var_t**)b;
  239. return strcasecmp(mv1->cvar->member.name, mv2->cvar->member.name);
  240. }
  241. /**
  242. * Mark a configuration manager as "frozen", so that no more formats can be
  243. * added, and so that it can be used for manipulating configuration objects.
  244. **/
  245. void
  246. config_mgr_freeze(config_mgr_t *mgr)
  247. {
  248. static uint64_t mgr_count = 0;
  249. smartlist_sort(mgr->all_vars, managed_var_cmp);
  250. memcpy(&mgr->toplevel_magic, &mgr->toplevel->magic,
  251. sizeof(struct_magic_decl_t));
  252. uint64_t magic_input[3] = { mgr->toplevel_magic.magic_val,
  253. (uint64_t) (uintptr_t) mgr,
  254. ++mgr_count };
  255. mgr->toplevel_magic.magic_val =
  256. (uint32_t)siphash24g(magic_input, sizeof(magic_input));
  257. mgr->frozen = true;
  258. }
  259. /** Release all storage held in <b>mgr</b> */
  260. void
  261. config_mgr_free_(config_mgr_t *mgr)
  262. {
  263. if (!mgr)
  264. return;
  265. SMARTLIST_FOREACH(mgr->all_vars, managed_var_t *, mv, managed_var_free(mv));
  266. smartlist_free(mgr->all_vars);
  267. smartlist_free(mgr->all_abbrevs);
  268. smartlist_free(mgr->all_deprecations);
  269. smartlist_free(mgr->subconfigs);
  270. memset(mgr, 0, sizeof(*mgr));
  271. tor_free(mgr);
  272. }
  273. /** Return a new smartlist_t containing a config_var_t for every variable that
  274. * <b>mgr</b> knows about. The elements of this smartlist do not need
  275. * to be freed; they have the same lifespan as <b>mgr</b>. */
  276. smartlist_t *
  277. config_mgr_list_vars(const config_mgr_t *mgr)
  278. {
  279. smartlist_t *result = smartlist_new();
  280. tor_assert(mgr);
  281. SMARTLIST_FOREACH(mgr->all_vars, managed_var_t *, mv,
  282. smartlist_add(result, (void*) mv->cvar));
  283. return result;
  284. }
  285. /** Return a new smartlist_t containing the names of all deprecated variables.
  286. * The elements of this smartlist do not need to be freed; they have the same
  287. * lifespan as <b>mgr</b>.
  288. */
  289. smartlist_t *
  290. config_mgr_list_deprecated_vars(const config_mgr_t *mgr)
  291. {
  292. smartlist_t *result = smartlist_new();
  293. tor_assert(mgr);
  294. SMARTLIST_FOREACH(mgr->all_deprecations, config_deprecation_t *, d,
  295. smartlist_add(result, (char*)d->name));
  296. return result;
  297. }
  298. /** Assert that the magic fields in <b>options</b> and its subsidiary
  299. * objects are all okay. */
  300. static void
  301. config_mgr_assert_magic_ok(const config_mgr_t *mgr,
  302. const void *options)
  303. {
  304. tor_assert(mgr);
  305. tor_assert(options);
  306. tor_assert(mgr->frozen);
  307. struct_check_magic(options, &mgr->toplevel_magic);
  308. config_suite_t **suitep = config_mgr_get_suite_ptr(mgr, (void*)options);
  309. if (suitep == NULL) {
  310. tor_assert(smartlist_len(mgr->subconfigs) == 0);
  311. return;
  312. }
  313. tor_assert(smartlist_len((*suitep)->configs) ==
  314. smartlist_len(mgr->subconfigs));
  315. SMARTLIST_FOREACH_BEGIN(mgr->subconfigs, const config_format_t *, fmt) {
  316. void *obj = smartlist_get((*suitep)->configs, fmt_sl_idx);
  317. tor_assert(obj);
  318. struct_check_magic(obj, &fmt->magic);
  319. } SMARTLIST_FOREACH_END(fmt);
  320. }
  321. /** Macro: assert that <b>cfg</b> has the right magic field for
  322. * <b>mgr</b>. */
  323. #define CONFIG_CHECK(mgr, cfg) STMT_BEGIN \
  324. config_mgr_assert_magic_ok((mgr), (cfg)); \
  325. STMT_END
  326. /** Allocate an empty configuration object of a given format type. */
  327. void *
  328. config_new(const config_mgr_t *mgr)
  329. {
  330. tor_assert(mgr->frozen);
  331. void *opts = tor_malloc_zero(mgr->toplevel->size);
  332. struct_set_magic(opts, &mgr->toplevel_magic);
  333. config_suite_t **suitep = config_mgr_get_suite_ptr(mgr, opts);
  334. if (suitep) {
  335. *suitep = config_suite_new();
  336. SMARTLIST_FOREACH_BEGIN(mgr->subconfigs, const config_format_t *, fmt) {
  337. void *obj = tor_malloc_zero(fmt->size);
  338. struct_set_magic(obj, &fmt->magic);
  339. smartlist_add((*suitep)->configs, obj);
  340. } SMARTLIST_FOREACH_END(fmt);
  341. }
  342. CONFIG_CHECK(mgr, opts);
  343. return opts;
  344. }
  345. /*
  346. * Functions to parse config options
  347. */
  348. /** If <b>option</b> is an official abbreviation for a longer option,
  349. * return the longer option. Otherwise return <b>option</b>.
  350. * If <b>command_line</b> is set, apply all abbreviations. Otherwise, only
  351. * apply abbreviations that work for the config file and the command line.
  352. * If <b>warn_obsolete</b> is set, warn about deprecated names. */
  353. const char *
  354. config_expand_abbrev(const config_mgr_t *mgr, const char *option,
  355. int command_line, int warn_obsolete)
  356. {
  357. SMARTLIST_FOREACH_BEGIN(mgr->all_abbrevs, const config_abbrev_t *, abbrev) {
  358. /* Abbreviations are case insensitive. */
  359. if (!strcasecmp(option, abbrev->abbreviated) &&
  360. (command_line || !abbrev->commandline_only)) {
  361. if (warn_obsolete && abbrev->warn) {
  362. log_warn(LD_CONFIG,
  363. "The configuration option '%s' is deprecated; "
  364. "use '%s' instead.",
  365. abbrev->abbreviated,
  366. abbrev->full);
  367. }
  368. /* Keep going through the list in case we want to rewrite it more.
  369. * (We could imagine recursing here, but I don't want to get the
  370. * user into an infinite loop if we craft our list wrong.) */
  371. option = abbrev->full;
  372. }
  373. } SMARTLIST_FOREACH_END(abbrev);
  374. return option;
  375. }
  376. /** If <b>key</b> is a deprecated configuration option, return the message
  377. * explaining why it is deprecated (which may be an empty string). Return NULL
  378. * if it is not deprecated. The <b>key</b> field must be fully expanded. */
  379. const char *
  380. config_find_deprecation(const config_mgr_t *mgr, const char *key)
  381. {
  382. if (BUG(mgr == NULL) || BUG(key == NULL))
  383. return NULL; // LCOV_EXCL_LINE
  384. SMARTLIST_FOREACH_BEGIN(mgr->all_deprecations, const config_deprecation_t *,
  385. d) {
  386. if (!strcasecmp(d->name, key)) {
  387. return d->why_deprecated ? d->why_deprecated : "";
  388. }
  389. } SMARTLIST_FOREACH_END(d);
  390. return NULL;
  391. }
  392. /**
  393. * Find the managed_var_t object for a variable whose name is <b>name</b>
  394. * according to <b>mgr</b>. Return that object, or NULL if none exists.
  395. *
  396. * If <b>allow_truncated</b> is true, then accept any variable whose
  397. * name begins with <b>name</b>.
  398. *
  399. * If <b>idx_out</b> is not NULL, set *<b>idx_out</b> to the position of
  400. * that variable within mgr-&gt;all_vars, or to -1 if the variable is
  401. * not found.
  402. */
  403. static const managed_var_t *
  404. config_mgr_find_var(const config_mgr_t *mgr,
  405. const char *key,
  406. bool allow_truncated, int *idx_out)
  407. {
  408. const size_t keylen = strlen(key);
  409. if (idx_out)
  410. *idx_out = -1;
  411. if (!keylen)
  412. return NULL; /* if they say "--" on the command line, it's not an option */
  413. /* First, check for an exact (case-insensitive) match */
  414. SMARTLIST_FOREACH_BEGIN(mgr->all_vars, const managed_var_t *, mv) {
  415. if (!strcasecmp(mv->cvar->member.name, key)) {
  416. if (idx_out)
  417. *idx_out = mv_sl_idx;
  418. return mv;
  419. }
  420. } SMARTLIST_FOREACH_END(mv);
  421. if (!allow_truncated)
  422. return NULL;
  423. /* If none, check for an abbreviated match */
  424. SMARTLIST_FOREACH_BEGIN(mgr->all_vars, const managed_var_t *, mv) {
  425. if (!strncasecmp(key, mv->cvar->member.name, keylen)) {
  426. log_warn(LD_CONFIG, "The abbreviation '%s' is deprecated. "
  427. "Please use '%s' instead",
  428. key, mv->cvar->member.name);
  429. if (idx_out)
  430. *idx_out = mv_sl_idx;
  431. return mv;
  432. }
  433. } SMARTLIST_FOREACH_END(mv);
  434. /* Okay, unrecognized option */
  435. return NULL;
  436. }
  437. /**
  438. * If <b>key</b> is a name or an abbreviation configuration option, return
  439. * the corresponding canonical name for it. Warn if the abbreviation is
  440. * non-standard. Return NULL if the option does not exist.
  441. */
  442. const char *
  443. config_find_option_name(const config_mgr_t *mgr, const char *key)
  444. {
  445. key = config_expand_abbrev(mgr, key, 0, 0);
  446. const managed_var_t *mv = config_mgr_find_var(mgr, key, true, NULL);
  447. if (mv)
  448. return mv->cvar->member.name;
  449. else
  450. return NULL;
  451. }
  452. /** Return the number of option entries in <b>fmt</b>. */
  453. static int
  454. config_count_options(const config_mgr_t *mgr)
  455. {
  456. return smartlist_len(mgr->all_vars);
  457. }
  458. bool
  459. config_var_is_cumulative(const config_var_t *var)
  460. {
  461. return struct_var_is_cumulative(&var->member);
  462. }
  463. bool
  464. config_var_is_settable(const config_var_t *var)
  465. {
  466. if (var->flags & CVFLAG_OBSOLETE)
  467. return false;
  468. return struct_var_is_settable(&var->member);
  469. }
  470. bool
  471. config_var_is_contained(const config_var_t *var)
  472. {
  473. return struct_var_is_contained(&var->member);
  474. }
  475. bool
  476. config_var_is_invisible(const config_var_t *var)
  477. {
  478. return (var->flags & CVFLAG_INVISIBLE) != 0;
  479. }
  480. bool
  481. config_var_is_dumpable(const config_var_t *var)
  482. {
  483. return (var->flags & CVFLAG_NODUMP) == 0;
  484. }
  485. /*
  486. * Functions to assign config options.
  487. */
  488. /** <b>c</b>-\>key is known to be a real key. Update <b>options</b>
  489. * with <b>c</b>-\>value and return 0, or return -1 if bad value.
  490. *
  491. * Called from config_assign_line() and option_reset().
  492. */
  493. static int
  494. config_assign_value(const config_mgr_t *mgr, void *options,
  495. config_line_t *c, char **msg)
  496. {
  497. const managed_var_t *var;
  498. CONFIG_CHECK(mgr, options);
  499. var = config_mgr_find_var(mgr, c->key, true, NULL);
  500. tor_assert(var);
  501. tor_assert(!strcmp(c->key, var->cvar->member.name));
  502. void *object = config_mgr_get_obj_mutable(mgr, options, var->object_idx);
  503. return struct_var_kvassign(object, c, msg, &var->cvar->member);
  504. }
  505. /** Mark every linelist in <b>options</b> "fragile", so that fresh assignments
  506. * to it will replace old ones. */
  507. static void
  508. config_mark_lists_fragile(const config_mgr_t *mgr, void *options)
  509. {
  510. tor_assert(mgr);
  511. tor_assert(options);
  512. SMARTLIST_FOREACH_BEGIN(mgr->all_vars, const managed_var_t *, mv) {
  513. void *object = config_mgr_get_obj_mutable(mgr, options, mv->object_idx);
  514. struct_var_mark_fragile(object, &mv->cvar->member);
  515. } SMARTLIST_FOREACH_END(mv);
  516. }
  517. void
  518. warn_deprecated_option(const char *what, const char *why)
  519. {
  520. const char *space = (why && strlen(why)) ? " " : "";
  521. log_warn(LD_CONFIG, "The %s option is deprecated, and will most likely "
  522. "be removed in a future version of Tor.%s%s (If you think this is "
  523. "a mistake, please let us know!)",
  524. what, space, why);
  525. }
  526. /** If <b>c</b> is a syntactically valid configuration line, update
  527. * <b>options</b> with its value and return 0. Otherwise return -1 for bad
  528. * key, -2 for bad value.
  529. *
  530. * If <b>clear_first</b> is set, clear the value first. Then if
  531. * <b>use_defaults</b> is set, set the value to the default.
  532. *
  533. * Called from config_assign().
  534. */
  535. static int
  536. config_assign_line(const config_mgr_t *mgr, void *options,
  537. config_line_t *c, unsigned flags,
  538. bitarray_t *options_seen, char **msg)
  539. {
  540. const unsigned use_defaults = flags & CAL_USE_DEFAULTS;
  541. const unsigned clear_first = flags & CAL_CLEAR_FIRST;
  542. const unsigned warn_deprecations = flags & CAL_WARN_DEPRECATIONS;
  543. const managed_var_t *mvar;
  544. CONFIG_CHECK(mgr, options);
  545. int var_index = -1;
  546. mvar = config_mgr_find_var(mgr, c->key, true, &var_index);
  547. if (!mvar) {
  548. const config_format_t *fmt = mgr->toplevel;
  549. if (fmt->extra) {
  550. void *lvalue = STRUCT_VAR_P(options, fmt->extra->offset);
  551. log_info(LD_CONFIG,
  552. "Found unrecognized option '%s'; saving it.", c->key);
  553. config_line_append((config_line_t**)lvalue, c->key, c->value);
  554. return 0;
  555. } else {
  556. tor_asprintf(msg,
  557. "Unknown option '%s'. Failing.", c->key);
  558. return -1;
  559. }
  560. }
  561. const config_var_t *cvar = mvar->cvar;
  562. tor_assert(cvar);
  563. /* Put keyword into canonical case. */
  564. if (strcmp(cvar->member.name, c->key)) {
  565. tor_free(c->key);
  566. c->key = tor_strdup(cvar->member.name);
  567. }
  568. const char *deprecation_msg;
  569. if (warn_deprecations &&
  570. (deprecation_msg = config_find_deprecation(mgr, cvar->member.name))) {
  571. warn_deprecated_option(cvar->member.name, deprecation_msg);
  572. }
  573. if (!strlen(c->value)) {
  574. /* reset or clear it, then return */
  575. if (!clear_first) {
  576. if (config_var_is_cumulative(cvar) && c->command != CONFIG_LINE_CLEAR) {
  577. /* We got an empty linelist from the torrc or command line.
  578. As a special case, call this an error. Warn and ignore. */
  579. log_warn(LD_CONFIG,
  580. "Linelist option '%s' has no value. Skipping.", c->key);
  581. } else { /* not already cleared */
  582. config_reset(mgr, options, mvar, use_defaults);
  583. }
  584. }
  585. return 0;
  586. } else if (c->command == CONFIG_LINE_CLEAR && !clear_first) {
  587. // XXXX This is unreachable, since a CLEAR line always has an
  588. // XXXX empty value.
  589. config_reset(mgr, options, mvar, use_defaults); // LCOV_EXCL_LINE
  590. }
  591. if (options_seen && ! config_var_is_cumulative(cvar)) {
  592. /* We're tracking which options we've seen, and this option is not
  593. * supposed to occur more than once. */
  594. tor_assert(var_index >= 0);
  595. if (bitarray_is_set(options_seen, var_index)) {
  596. log_warn(LD_CONFIG, "Option '%s' used more than once; all but the last "
  597. "value will be ignored.", cvar->member.name);
  598. }
  599. bitarray_set(options_seen, var_index);
  600. }
  601. if (config_assign_value(mgr, options, c, msg) < 0)
  602. return -2;
  603. return 0;
  604. }
  605. /** Restore the option named <b>key</b> in options to its default value.
  606. * Called from config_assign(). */
  607. STATIC void
  608. config_reset_line(const config_mgr_t *mgr, void *options,
  609. const char *key, int use_defaults)
  610. {
  611. const managed_var_t *var;
  612. CONFIG_CHECK(mgr, options);
  613. var = config_mgr_find_var(mgr, key, true, NULL);
  614. if (!var)
  615. return; /* give error on next pass. */
  616. config_reset(mgr, options, var, use_defaults);
  617. }
  618. /** Return true iff value needs to be quoted and escaped to be used in
  619. * a configuration file. */
  620. static int
  621. config_value_needs_escape(const char *value)
  622. {
  623. if (*value == '\"')
  624. return 1;
  625. while (*value) {
  626. switch (*value)
  627. {
  628. case '\r':
  629. case '\n':
  630. case '#':
  631. /* Note: quotes and backspaces need special handling when we are using
  632. * quotes, not otherwise, so they don't trigger escaping on their
  633. * own. */
  634. return 1;
  635. default:
  636. if (!TOR_ISPRINT(*value))
  637. return 1;
  638. }
  639. ++value;
  640. }
  641. return 0;
  642. }
  643. /** Return newly allocated line or lines corresponding to <b>key</b> in the
  644. * configuration <b>options</b>. If <b>escape_val</b> is true and a
  645. * value needs to be quoted before it's put in a config file, quote and
  646. * escape that value. Return NULL if no such key exists. */
  647. config_line_t *
  648. config_get_assigned_option(const config_mgr_t *mgr, const void *options,
  649. const char *key, int escape_val)
  650. {
  651. const managed_var_t *var;
  652. config_line_t *result;
  653. tor_assert(options && key);
  654. CONFIG_CHECK(mgr, options);
  655. var = config_mgr_find_var(mgr, key, true, NULL);
  656. if (!var) {
  657. log_warn(LD_CONFIG, "Unknown option '%s'. Failing.", key);
  658. return NULL;
  659. }
  660. const void *object = config_mgr_get_obj(mgr, options, var->object_idx);
  661. result = struct_var_kvencode(object, &var->cvar->member);
  662. if (escape_val) {
  663. config_line_t *line;
  664. for (line = result; line; line = line->next) {
  665. if (line->value && config_value_needs_escape(line->value)) {
  666. char *newval = esc_for_log(line->value);
  667. tor_free(line->value);
  668. line->value = newval;
  669. }
  670. }
  671. }
  672. return result;
  673. }
  674. /** Iterate through the linked list of requested options <b>list</b>.
  675. * For each item, convert as appropriate and assign to <b>options</b>.
  676. * If an item is unrecognized, set *msg and return -1 immediately,
  677. * else return 0 for success.
  678. *
  679. * If <b>clear_first</b>, interpret config options as replacing (not
  680. * extending) their previous values. If <b>clear_first</b> is set,
  681. * then <b>use_defaults</b> to decide if you set to defaults after
  682. * clearing, or make the value 0 or NULL.
  683. *
  684. * Here are the use cases:
  685. * 1. A non-empty AllowInvalid line in your torrc. Appends to current
  686. * if linelist, replaces current if csv.
  687. * 2. An empty AllowInvalid line in your torrc. Should clear it.
  688. * 3. "RESETCONF AllowInvalid" sets it to default.
  689. * 4. "SETCONF AllowInvalid" makes it NULL.
  690. * 5. "SETCONF AllowInvalid=foo" clears it and sets it to "foo".
  691. *
  692. * Use_defaults Clear_first
  693. * 0 0 "append"
  694. * 1 0 undefined, don't use
  695. * 0 1 "set to null first"
  696. * 1 1 "set to defaults first"
  697. * Return 0 on success, -1 on bad key, -2 on bad value.
  698. *
  699. * As an additional special case, if a LINELIST config option has
  700. * no value and clear_first is 0, then warn and ignore it.
  701. */
  702. /*
  703. There are three call cases for config_assign() currently.
  704. Case one: Torrc entry
  705. options_init_from_torrc() calls config_assign(0, 0)
  706. calls config_assign_line(0, 0).
  707. if value is empty, calls config_reset(0) and returns.
  708. calls config_assign_value(), appends.
  709. Case two: setconf
  710. options_trial_assign() calls config_assign(0, 1)
  711. calls config_reset_line(0)
  712. calls config_reset(0)
  713. calls option_clear().
  714. calls config_assign_line(0, 1).
  715. if value is empty, returns.
  716. calls config_assign_value(), appends.
  717. Case three: resetconf
  718. options_trial_assign() calls config_assign(1, 1)
  719. calls config_reset_line(1)
  720. calls config_reset(1)
  721. calls option_clear().
  722. calls config_assign_value(default)
  723. calls config_assign_line(1, 1).
  724. returns.
  725. */
  726. int
  727. config_assign(const config_mgr_t *mgr, void *options, config_line_t *list,
  728. unsigned config_assign_flags, char **msg)
  729. {
  730. config_line_t *p;
  731. bitarray_t *options_seen;
  732. const int n_options = config_count_options(mgr);
  733. const unsigned clear_first = config_assign_flags & CAL_CLEAR_FIRST;
  734. const unsigned use_defaults = config_assign_flags & CAL_USE_DEFAULTS;
  735. CONFIG_CHECK(mgr, options);
  736. /* pass 1: normalize keys */
  737. for (p = list; p; p = p->next) {
  738. const char *full = config_expand_abbrev(mgr, p->key, 0, 1);
  739. if (strcmp(full,p->key)) {
  740. tor_free(p->key);
  741. p->key = tor_strdup(full);
  742. }
  743. }
  744. /* pass 2: if we're reading from a resetting source, clear all
  745. * mentioned config options, and maybe set to their defaults. */
  746. if (clear_first) {
  747. for (p = list; p; p = p->next)
  748. config_reset_line(mgr, options, p->key, use_defaults);
  749. }
  750. options_seen = bitarray_init_zero(n_options);
  751. /* pass 3: assign. */
  752. while (list) {
  753. int r;
  754. if ((r=config_assign_line(mgr, options, list, config_assign_flags,
  755. options_seen, msg))) {
  756. bitarray_free(options_seen);
  757. return r;
  758. }
  759. list = list->next;
  760. }
  761. bitarray_free(options_seen);
  762. /** Now we're done assigning a group of options to the configuration.
  763. * Subsequent group assignments should _replace_ linelists, not extend
  764. * them. */
  765. config_mark_lists_fragile(mgr, options);
  766. return 0;
  767. }
  768. /** Reset config option <b>var</b> to 0, 0.0, NULL, or the equivalent.
  769. * Called from config_reset() and config_free(). */
  770. static void
  771. config_clear(const config_mgr_t *mgr, void *options, const managed_var_t *var)
  772. {
  773. void *object = config_mgr_get_obj_mutable(mgr, options, var->object_idx);
  774. struct_var_free(object, &var->cvar->member);
  775. }
  776. /** Clear the option indexed by <b>var</b> in <b>options</b>. Then if
  777. * <b>use_defaults</b>, set it to its default value.
  778. * Called by config_init() and option_reset_line() and option_assign_line(). */
  779. static void
  780. config_reset(const config_mgr_t *mgr, void *options,
  781. const managed_var_t *var, int use_defaults)
  782. {
  783. config_line_t *c;
  784. char *msg = NULL;
  785. CONFIG_CHECK(mgr, options);
  786. config_clear(mgr, options, var); /* clear it first */
  787. if (!use_defaults)
  788. return; /* all done */
  789. if (var->cvar->initvalue) {
  790. c = tor_malloc_zero(sizeof(config_line_t));
  791. c->key = tor_strdup(var->cvar->member.name);
  792. c->value = tor_strdup(var->cvar->initvalue);
  793. if (config_assign_value(mgr, options, c, &msg) < 0) {
  794. // LCOV_EXCL_START
  795. log_warn(LD_BUG, "Failed to assign default: %s", msg);
  796. tor_free(msg); /* if this happens it's a bug */
  797. // LCOV_EXCL_STOP
  798. }
  799. config_free_lines(c);
  800. }
  801. }
  802. /** Release storage held by <b>options</b>. */
  803. void
  804. config_free_(const config_mgr_t *mgr, void *options)
  805. {
  806. if (!options)
  807. return;
  808. tor_assert(mgr);
  809. if (mgr->toplevel->clear_fn) {
  810. mgr->toplevel->clear_fn(mgr, options);
  811. }
  812. config_suite_t **suitep = config_mgr_get_suite_ptr(mgr, options);
  813. if (suitep) {
  814. tor_assert(smartlist_len((*suitep)->configs) ==
  815. smartlist_len(mgr->subconfigs));
  816. SMARTLIST_FOREACH_BEGIN(mgr->subconfigs, const config_format_t *, fmt) {
  817. void *obj = smartlist_get((*suitep)->configs, fmt_sl_idx);
  818. if (fmt->clear_fn) {
  819. fmt->clear_fn(mgr, obj);
  820. }
  821. } SMARTLIST_FOREACH_END(fmt);
  822. }
  823. SMARTLIST_FOREACH_BEGIN(mgr->all_vars, const managed_var_t *, mv) {
  824. config_clear(mgr, options, mv);
  825. } SMARTLIST_FOREACH_END(mv);
  826. if (mgr->toplevel->extra) {
  827. config_line_t **linep = STRUCT_VAR_P(options,
  828. mgr->toplevel->extra->offset);
  829. config_free_lines(*linep);
  830. *linep = NULL;
  831. }
  832. if (suitep) {
  833. SMARTLIST_FOREACH((*suitep)->configs, void *, obj, tor_free(obj));
  834. config_suite_free(*suitep);
  835. }
  836. tor_free(options);
  837. }
  838. /** Return true iff the option <b>name</b> has the same value in <b>o1</b>
  839. * and <b>o2</b>. Must not be called for LINELIST_S or OBSOLETE options.
  840. */
  841. int
  842. config_is_same(const config_mgr_t *mgr,
  843. const void *o1, const void *o2,
  844. const char *name)
  845. {
  846. CONFIG_CHECK(mgr, o1);
  847. CONFIG_CHECK(mgr, o2);
  848. const managed_var_t *var = config_mgr_find_var(mgr, name, true, NULL);
  849. if (!var) {
  850. return true;
  851. }
  852. const void *obj1 = config_mgr_get_obj(mgr, o1, var->object_idx);
  853. const void *obj2 = config_mgr_get_obj(mgr, o2, var->object_idx);
  854. return struct_var_eq(obj1, obj2, &var->cvar->member);
  855. }
  856. /**
  857. * Return a list of the options which have changed between <b>options1</b> and
  858. * <b>options2</b>. If an option has reverted to its default value, it has a
  859. * value entry of NULL.
  860. *
  861. * <b>options1</b> and <b>options2</b> must be top-level configuration objects
  862. * of the type managed by <b>mgr</b>.
  863. **/
  864. config_line_t *
  865. config_get_changes(const config_mgr_t *mgr,
  866. const void *options1, const void *options2)
  867. {
  868. config_line_t *result = NULL;
  869. config_line_t **next = &result;
  870. SMARTLIST_FOREACH_BEGIN(mgr->all_vars, managed_var_t *, mv) {
  871. if (config_var_is_contained(mv->cvar)) {
  872. /* something else will check this var, or it doesn't need checking */
  873. continue;
  874. }
  875. const void *obj1 = config_mgr_get_obj(mgr, options1, mv->object_idx);
  876. const void *obj2 = config_mgr_get_obj(mgr, options2, mv->object_idx);
  877. if (struct_var_eq(obj1, obj2, &mv->cvar->member)) {
  878. continue;
  879. }
  880. const char *varname = mv->cvar->member.name;
  881. config_line_t *line =
  882. config_get_assigned_option(mgr, options2, varname, 1);
  883. if (line) {
  884. *next = line;
  885. } else {
  886. *next = tor_malloc_zero(sizeof(config_line_t));
  887. (*next)->key = tor_strdup(varname);
  888. }
  889. while (*next)
  890. next = &(*next)->next;
  891. } SMARTLIST_FOREACH_END(mv);
  892. return result;
  893. }
  894. /** Copy storage held by <b>old</b> into a new or_options_t and return it. */
  895. void *
  896. config_dup(const config_mgr_t *mgr, const void *old)
  897. {
  898. void *newopts;
  899. newopts = config_new(mgr);
  900. SMARTLIST_FOREACH_BEGIN(mgr->all_vars, managed_var_t *, mv) {
  901. if (config_var_is_contained(mv->cvar)) {
  902. // Something else will copy this option, or it doesn't need copying.
  903. continue;
  904. }
  905. const void *oldobj = config_mgr_get_obj(mgr, old, mv->object_idx);
  906. void *newobj = config_mgr_get_obj_mutable(mgr, newopts, mv->object_idx);
  907. if (struct_var_copy(newobj, oldobj, &mv->cvar->member) < 0) {
  908. // LCOV_EXCL_START
  909. log_err(LD_BUG, "Unable to copy value for %s.",
  910. mv->cvar->member.name);
  911. tor_assert_unreached();
  912. // LCOV_EXCL_STOP
  913. }
  914. } SMARTLIST_FOREACH_END(mv);
  915. return newopts;
  916. }
  917. /** Set all vars in the configuration object <b>options</b> to their default
  918. * values. */
  919. void
  920. config_init(const config_mgr_t *mgr, void *options)
  921. {
  922. CONFIG_CHECK(mgr, options);
  923. SMARTLIST_FOREACH_BEGIN(mgr->all_vars, const managed_var_t *, mv) {
  924. if (!mv->cvar->initvalue)
  925. continue; /* defaults to NULL or 0 */
  926. config_reset(mgr, options, mv, 1);
  927. } SMARTLIST_FOREACH_END(mv);
  928. }
  929. /** Allocate and return a new string holding the written-out values of the vars
  930. * in 'options'. If 'minimal', do not write out any default-valued vars.
  931. * Else, if comment_defaults, write default values as comments.
  932. */
  933. char *
  934. config_dump(const config_mgr_t *mgr, const void *default_options,
  935. const void *options, int minimal,
  936. int comment_defaults)
  937. {
  938. const config_format_t *fmt = mgr->toplevel;
  939. smartlist_t *elements;
  940. const void *defaults = default_options;
  941. void *defaults_tmp = NULL;
  942. config_line_t *line, *assigned;
  943. char *result;
  944. char *msg = NULL;
  945. if (defaults == NULL) {
  946. defaults = defaults_tmp = config_new(mgr);
  947. config_init(mgr, defaults_tmp);
  948. }
  949. /* XXX use a 1 here so we don't add a new log line while dumping */
  950. if (default_options == NULL) {
  951. if (fmt->validate_fn(NULL, defaults_tmp, defaults_tmp, 1, &msg) < 0) {
  952. // LCOV_EXCL_START
  953. log_err(LD_BUG, "Failed to validate default config: %s", msg);
  954. tor_free(msg);
  955. tor_assert(0);
  956. // LCOV_EXCL_STOP
  957. }
  958. }
  959. elements = smartlist_new();
  960. SMARTLIST_FOREACH_BEGIN(mgr->all_vars, managed_var_t *, mv) {
  961. int comment_option = 0;
  962. if (config_var_is_contained(mv->cvar)) {
  963. // Something else will dump this option, or it doesn't need dumping.
  964. continue;
  965. }
  966. /* Don't save 'hidden' control variables. */
  967. if (! config_var_is_dumpable(mv->cvar))
  968. continue;
  969. const char *name = mv->cvar->member.name;
  970. if (minimal && config_is_same(mgr, options, defaults, name))
  971. continue;
  972. else if (comment_defaults &&
  973. config_is_same(mgr, options, defaults, name))
  974. comment_option = 1;
  975. line = assigned =
  976. config_get_assigned_option(mgr, options, name, 1);
  977. for (; line; line = line->next) {
  978. if (!strcmpstart(line->key, "__")) {
  979. /* This check detects "hidden" variables inside LINELIST_V structures.
  980. */
  981. continue;
  982. }
  983. smartlist_add_asprintf(elements, "%s%s %s\n",
  984. comment_option ? "# " : "",
  985. line->key, line->value);
  986. }
  987. config_free_lines(assigned);
  988. } SMARTLIST_FOREACH_END(mv);
  989. if (fmt->extra) {
  990. line = *(config_line_t**)STRUCT_VAR_P(options, fmt->extra->offset);
  991. for (; line; line = line->next) {
  992. smartlist_add_asprintf(elements, "%s %s\n", line->key, line->value);
  993. }
  994. }
  995. result = smartlist_join_strings(elements, "", 0, NULL);
  996. SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
  997. smartlist_free(elements);
  998. config_free(mgr, defaults_tmp);
  999. return result;
  1000. }
  1001. /**
  1002. * Return true if every member of <b>options</b> is in-range and well-formed.
  1003. * Return false otherwise. Log errors at level <b>severity</b>.
  1004. */
  1005. bool
  1006. config_check_ok(const config_mgr_t *mgr, const void *options, int severity)
  1007. {
  1008. bool all_ok = true;
  1009. SMARTLIST_FOREACH_BEGIN(mgr->all_vars, const managed_var_t *, mv) {
  1010. if (!struct_var_ok(options, &mv->cvar->member)) {
  1011. log_fn(severity, LD_BUG, "Invalid value for %s",
  1012. mv->cvar->member.name);
  1013. all_ok = false;
  1014. }
  1015. } SMARTLIST_FOREACH_END(mv);
  1016. return all_ok;
  1017. }