confparse.c 31 KB

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