confparse.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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 "core/or/or.h"
  24. #include "app/config/confparse.h"
  25. #include "feature/nodelist/routerset.h"
  26. #include "lib/confmgt/unitparse.h"
  27. #include "lib/container/bitarray.h"
  28. #include "lib/encoding/confline.h"
  29. #include "lib/confmgt/structvar.h"
  30. static void config_reset(const config_format_t *fmt, void *options,
  31. const config_var_t *var, int use_defaults);
  32. /** Allocate an empty configuration object of a given format type. */
  33. void *
  34. config_new(const config_format_t *fmt)
  35. {
  36. void *opts = tor_malloc_zero(fmt->size);
  37. struct_set_magic(opts, &fmt->magic);
  38. CONFIG_CHECK(fmt, opts);
  39. return opts;
  40. }
  41. /*
  42. * Functions to parse config options
  43. */
  44. /** If <b>option</b> is an official abbreviation for a longer option,
  45. * return the longer option. Otherwise return <b>option</b>.
  46. * If <b>command_line</b> is set, apply all abbreviations. Otherwise, only
  47. * apply abbreviations that work for the config file and the command line.
  48. * If <b>warn_obsolete</b> is set, warn about deprecated names. */
  49. const char *
  50. config_expand_abbrev(const config_format_t *fmt, const char *option,
  51. int command_line, int warn_obsolete)
  52. {
  53. int i;
  54. if (! fmt->abbrevs)
  55. return option;
  56. for (i=0; fmt->abbrevs[i].abbreviated; ++i) {
  57. /* Abbreviations are case insensitive. */
  58. if (!strcasecmp(option,fmt->abbrevs[i].abbreviated) &&
  59. (command_line || !fmt->abbrevs[i].commandline_only)) {
  60. if (warn_obsolete && fmt->abbrevs[i].warn) {
  61. log_warn(LD_CONFIG,
  62. "The configuration option '%s' is deprecated; "
  63. "use '%s' instead.",
  64. fmt->abbrevs[i].abbreviated,
  65. fmt->abbrevs[i].full);
  66. }
  67. /* Keep going through the list in case we want to rewrite it more.
  68. * (We could imagine recursing here, but I don't want to get the
  69. * user into an infinite loop if we craft our list wrong.) */
  70. option = fmt->abbrevs[i].full;
  71. }
  72. }
  73. return option;
  74. }
  75. /** If <b>key</b> is a deprecated configuration option, return the message
  76. * explaining why it is deprecated (which may be an empty string). Return NULL
  77. * if it is not deprecated. The <b>key</b> field must be fully expanded. */
  78. const char *
  79. config_find_deprecation(const config_format_t *fmt, const char *key)
  80. {
  81. if (BUG(fmt == NULL) || BUG(key == NULL))
  82. return NULL; // LCOV_EXCL_LINE
  83. if (fmt->deprecations == NULL)
  84. return NULL;
  85. const config_deprecation_t *d;
  86. for (d = fmt->deprecations; d->name; ++d) {
  87. if (!strcasecmp(d->name, key)) {
  88. return d->why_deprecated ? d->why_deprecated : "";
  89. }
  90. }
  91. return NULL;
  92. }
  93. /** As config_find_option, but return a non-const pointer. */
  94. config_var_t *
  95. config_find_option_mutable(config_format_t *fmt, const char *key)
  96. {
  97. int i;
  98. size_t keylen = strlen(key);
  99. if (!keylen)
  100. return NULL; /* if they say "--" on the command line, it's not an option */
  101. /* First, check for an exact (case-insensitive) match */
  102. for (i=0; fmt->vars[i].member.name; ++i) {
  103. if (!strcasecmp(key, fmt->vars[i].member.name)) {
  104. return &fmt->vars[i];
  105. }
  106. }
  107. /* If none, check for an abbreviated match */
  108. for (i=0; fmt->vars[i].member.name; ++i) {
  109. if (!strncasecmp(key, fmt->vars[i].member.name, keylen)) {
  110. log_warn(LD_CONFIG, "The abbreviation '%s' is deprecated. "
  111. "Please use '%s' instead",
  112. key, fmt->vars[i].member.name);
  113. return &fmt->vars[i];
  114. }
  115. }
  116. /* Okay, unrecognized option */
  117. return NULL;
  118. }
  119. /** If <b>key</b> is a configuration option, return the corresponding const
  120. * config_var_t. Otherwise, if <b>key</b> is a non-standard abbreviation,
  121. * warn, and return the corresponding const config_var_t. Otherwise return
  122. * NULL.
  123. */
  124. const config_var_t *
  125. config_find_option(const config_format_t *fmt, const char *key)
  126. {
  127. return config_find_option_mutable((config_format_t*)fmt, key);
  128. }
  129. /** Return the number of option entries in <b>fmt</b>. */
  130. static int
  131. config_count_options(const config_format_t *fmt)
  132. {
  133. int i;
  134. for (i=0; fmt->vars[i].member.name; ++i)
  135. ;
  136. return i;
  137. }
  138. /*
  139. * Functions to assign config options.
  140. */
  141. /** <b>c</b>-\>key is known to be a real key. Update <b>options</b>
  142. * with <b>c</b>-\>value and return 0, or return -1 if bad value.
  143. *
  144. * Called from config_assign_line() and option_reset().
  145. */
  146. static int
  147. config_assign_value(const config_format_t *fmt, void *options,
  148. config_line_t *c, char **msg)
  149. {
  150. const config_var_t *var;
  151. CONFIG_CHECK(fmt, options);
  152. var = config_find_option(fmt, c->key);
  153. tor_assert(var);
  154. tor_assert(!strcmp(c->key, var->member.name));
  155. return struct_var_kvassign(options, c, msg, &var->member);
  156. }
  157. /** Mark every linelist in <b>options</b> "fragile", so that fresh assignments
  158. * to it will replace old ones. */
  159. static void
  160. config_mark_lists_fragile(const config_format_t *fmt, void *options)
  161. {
  162. int i;
  163. tor_assert(fmt);
  164. tor_assert(options);
  165. for (i = 0; fmt->vars[i].member.name; ++i) {
  166. const config_var_t *var = &fmt->vars[i];
  167. config_line_t *list;
  168. if (var->member.type != CONFIG_TYPE_LINELIST &&
  169. var->member.type != CONFIG_TYPE_LINELIST_V)
  170. continue;
  171. list = *(config_line_t **)STRUCT_VAR_P(options, var->member.offset);
  172. if (list)
  173. list->fragile = 1;
  174. }
  175. }
  176. void
  177. warn_deprecated_option(const char *what, const char *why)
  178. {
  179. const char *space = (why && strlen(why)) ? " " : "";
  180. log_warn(LD_CONFIG, "The %s option is deprecated, and will most likely "
  181. "be removed in a future version of Tor.%s%s (If you think this is "
  182. "a mistake, please let us know!)",
  183. what, space, why);
  184. }
  185. /** If <b>c</b> is a syntactically valid configuration line, update
  186. * <b>options</b> with its value and return 0. Otherwise return -1 for bad
  187. * key, -2 for bad value.
  188. *
  189. * If <b>clear_first</b> is set, clear the value first. Then if
  190. * <b>use_defaults</b> is set, set the value to the default.
  191. *
  192. * Called from config_assign().
  193. */
  194. static int
  195. config_assign_line(const config_format_t *fmt, void *options,
  196. config_line_t *c, unsigned flags,
  197. bitarray_t *options_seen, char **msg)
  198. {
  199. const unsigned use_defaults = flags & CAL_USE_DEFAULTS;
  200. const unsigned clear_first = flags & CAL_CLEAR_FIRST;
  201. const unsigned warn_deprecations = flags & CAL_WARN_DEPRECATIONS;
  202. const config_var_t *var;
  203. CONFIG_CHECK(fmt, options);
  204. var = config_find_option(fmt, c->key);
  205. if (!var) {
  206. if (fmt->extra) {
  207. void *lvalue = STRUCT_VAR_P(options, fmt->extra->offset);
  208. log_info(LD_CONFIG,
  209. "Found unrecognized option '%s'; saving it.", c->key);
  210. config_line_append((config_line_t**)lvalue, c->key, c->value);
  211. return 0;
  212. } else {
  213. tor_asprintf(msg,
  214. "Unknown option '%s'. Failing.", c->key);
  215. return -1;
  216. }
  217. }
  218. /* Put keyword into canonical case. */
  219. if (strcmp(var->member.name, c->key)) {
  220. tor_free(c->key);
  221. c->key = tor_strdup(var->member.name);
  222. }
  223. const char *deprecation_msg;
  224. if (warn_deprecations &&
  225. (deprecation_msg = config_find_deprecation(fmt, var->member.name))) {
  226. warn_deprecated_option(var->member.name, deprecation_msg);
  227. }
  228. if (!strlen(c->value)) {
  229. /* reset or clear it, then return */
  230. if (!clear_first) {
  231. if ((var->member.type == CONFIG_TYPE_LINELIST ||
  232. var->member.type == CONFIG_TYPE_LINELIST_S) &&
  233. c->command != CONFIG_LINE_CLEAR) {
  234. /* We got an empty linelist from the torrc or command line.
  235. As a special case, call this an error. Warn and ignore. */
  236. log_warn(LD_CONFIG,
  237. "Linelist option '%s' has no value. Skipping.", c->key);
  238. } else { /* not already cleared */
  239. config_reset(fmt, options, var, use_defaults);
  240. }
  241. }
  242. return 0;
  243. } else if (c->command == CONFIG_LINE_CLEAR && !clear_first) {
  244. // XXXX This is unreachable, since a CLEAR line always has an
  245. // XXXX empty value.
  246. config_reset(fmt, options, var, use_defaults); // LCOV_EXCL_LINE
  247. }
  248. if (options_seen && (var->member.type != CONFIG_TYPE_LINELIST &&
  249. var->member.type != CONFIG_TYPE_LINELIST_S)) {
  250. /* We're tracking which options we've seen, and this option is not
  251. * supposed to occur more than once. */
  252. int var_index = (int)(var - fmt->vars);
  253. if (bitarray_is_set(options_seen, var_index)) {
  254. log_warn(LD_CONFIG, "Option '%s' used more than once; all but the last "
  255. "value will be ignored.", var->member.name);
  256. }
  257. bitarray_set(options_seen, var_index);
  258. }
  259. if (config_assign_value(fmt, options, c, msg) < 0)
  260. return -2;
  261. return 0;
  262. }
  263. /** Restore the option named <b>key</b> in options to its default value.
  264. * Called from config_assign(). */
  265. STATIC void
  266. config_reset_line(const config_format_t *fmt, void *options,
  267. const char *key, int use_defaults)
  268. {
  269. const config_var_t *var;
  270. CONFIG_CHECK(fmt, options);
  271. var = config_find_option(fmt, key);
  272. if (!var)
  273. return; /* give error on next pass. */
  274. config_reset(fmt, options, var, use_defaults);
  275. }
  276. /** Return true iff value needs to be quoted and escaped to be used in
  277. * a configuration file. */
  278. static int
  279. config_value_needs_escape(const char *value)
  280. {
  281. if (*value == '\"')
  282. return 1;
  283. while (*value) {
  284. switch (*value)
  285. {
  286. case '\r':
  287. case '\n':
  288. case '#':
  289. /* Note: quotes and backspaces need special handling when we are using
  290. * quotes, not otherwise, so they don't trigger escaping on their
  291. * own. */
  292. return 1;
  293. default:
  294. if (!TOR_ISPRINT(*value))
  295. return 1;
  296. }
  297. ++value;
  298. }
  299. return 0;
  300. }
  301. /** Return newly allocated line or lines corresponding to <b>key</b> in the
  302. * configuration <b>options</b>. If <b>escape_val</b> is true and a
  303. * value needs to be quoted before it's put in a config file, quote and
  304. * escape that value. Return NULL if no such key exists. */
  305. config_line_t *
  306. config_get_assigned_option(const config_format_t *fmt, const void *options,
  307. const char *key, int escape_val)
  308. {
  309. const config_var_t *var;
  310. config_line_t *result;
  311. tor_assert(options && key);
  312. CONFIG_CHECK(fmt, options);
  313. var = config_find_option(fmt, key);
  314. if (!var) {
  315. log_warn(LD_CONFIG, "Unknown option '%s'. Failing.", key);
  316. return NULL;
  317. }
  318. result = struct_var_kvencode(options, &var->member);
  319. if (escape_val) {
  320. config_line_t *line;
  321. for (line = result; line; line = line->next) {
  322. if (line->value && config_value_needs_escape(line->value)) {
  323. char *newval = esc_for_log(line->value);
  324. tor_free(line->value);
  325. line->value = newval;
  326. }
  327. }
  328. }
  329. return result;
  330. }
  331. /** Iterate through the linked list of requested options <b>list</b>.
  332. * For each item, convert as appropriate and assign to <b>options</b>.
  333. * If an item is unrecognized, set *msg and return -1 immediately,
  334. * else return 0 for success.
  335. *
  336. * If <b>clear_first</b>, interpret config options as replacing (not
  337. * extending) their previous values. If <b>clear_first</b> is set,
  338. * then <b>use_defaults</b> to decide if you set to defaults after
  339. * clearing, or make the value 0 or NULL.
  340. *
  341. * Here are the use cases:
  342. * 1. A non-empty AllowInvalid line in your torrc. Appends to current
  343. * if linelist, replaces current if csv.
  344. * 2. An empty AllowInvalid line in your torrc. Should clear it.
  345. * 3. "RESETCONF AllowInvalid" sets it to default.
  346. * 4. "SETCONF AllowInvalid" makes it NULL.
  347. * 5. "SETCONF AllowInvalid=foo" clears it and sets it to "foo".
  348. *
  349. * Use_defaults Clear_first
  350. * 0 0 "append"
  351. * 1 0 undefined, don't use
  352. * 0 1 "set to null first"
  353. * 1 1 "set to defaults first"
  354. * Return 0 on success, -1 on bad key, -2 on bad value.
  355. *
  356. * As an additional special case, if a LINELIST config option has
  357. * no value and clear_first is 0, then warn and ignore it.
  358. */
  359. /*
  360. There are three call cases for config_assign() currently.
  361. Case one: Torrc entry
  362. options_init_from_torrc() calls config_assign(0, 0)
  363. calls config_assign_line(0, 0).
  364. if value is empty, calls config_reset(0) and returns.
  365. calls config_assign_value(), appends.
  366. Case two: setconf
  367. options_trial_assign() calls config_assign(0, 1)
  368. calls config_reset_line(0)
  369. calls config_reset(0)
  370. calls option_clear().
  371. calls config_assign_line(0, 1).
  372. if value is empty, returns.
  373. calls config_assign_value(), appends.
  374. Case three: resetconf
  375. options_trial_assign() calls config_assign(1, 1)
  376. calls config_reset_line(1)
  377. calls config_reset(1)
  378. calls option_clear().
  379. calls config_assign_value(default)
  380. calls config_assign_line(1, 1).
  381. returns.
  382. */
  383. int
  384. config_assign(const config_format_t *fmt, void *options, config_line_t *list,
  385. unsigned config_assign_flags, char **msg)
  386. {
  387. config_line_t *p;
  388. bitarray_t *options_seen;
  389. const int n_options = config_count_options(fmt);
  390. const unsigned clear_first = config_assign_flags & CAL_CLEAR_FIRST;
  391. const unsigned use_defaults = config_assign_flags & CAL_USE_DEFAULTS;
  392. CONFIG_CHECK(fmt, options);
  393. /* pass 1: normalize keys */
  394. for (p = list; p; p = p->next) {
  395. const char *full = config_expand_abbrev(fmt, p->key, 0, 1);
  396. if (strcmp(full,p->key)) {
  397. tor_free(p->key);
  398. p->key = tor_strdup(full);
  399. }
  400. }
  401. /* pass 2: if we're reading from a resetting source, clear all
  402. * mentioned config options, and maybe set to their defaults. */
  403. if (clear_first) {
  404. for (p = list; p; p = p->next)
  405. config_reset_line(fmt, options, p->key, use_defaults);
  406. }
  407. options_seen = bitarray_init_zero(n_options);
  408. /* pass 3: assign. */
  409. while (list) {
  410. int r;
  411. if ((r=config_assign_line(fmt, options, list, config_assign_flags,
  412. options_seen, msg))) {
  413. bitarray_free(options_seen);
  414. return r;
  415. }
  416. list = list->next;
  417. }
  418. bitarray_free(options_seen);
  419. /** Now we're done assigning a group of options to the configuration.
  420. * Subsequent group assignments should _replace_ linelists, not extend
  421. * them. */
  422. config_mark_lists_fragile(fmt, options);
  423. return 0;
  424. }
  425. /** Reset config option <b>var</b> to 0, 0.0, NULL, or the equivalent.
  426. * Called from config_reset() and config_free(). */
  427. static void
  428. config_clear(const config_format_t *fmt, void *options,
  429. const config_var_t *var)
  430. {
  431. (void)fmt; /* unused */
  432. struct_var_free(options, &var->member);
  433. }
  434. /** Clear the option indexed by <b>var</b> in <b>options</b>. Then if
  435. * <b>use_defaults</b>, set it to its default value.
  436. * Called by config_init() and option_reset_line() and option_assign_line(). */
  437. static void
  438. config_reset(const config_format_t *fmt, void *options,
  439. const config_var_t *var, int use_defaults)
  440. {
  441. config_line_t *c;
  442. char *msg = NULL;
  443. CONFIG_CHECK(fmt, options);
  444. config_clear(fmt, options, var); /* clear it first */
  445. if (!use_defaults)
  446. return; /* all done */
  447. if (var->initvalue) {
  448. c = tor_malloc_zero(sizeof(config_line_t));
  449. c->key = tor_strdup(var->member.name);
  450. c->value = tor_strdup(var->initvalue);
  451. if (config_assign_value(fmt, options, c, &msg) < 0) {
  452. // LCOV_EXCL_START
  453. log_warn(LD_BUG, "Failed to assign default: %s", msg);
  454. tor_free(msg); /* if this happens it's a bug */
  455. // LCOV_EXCL_STOP
  456. }
  457. config_free_lines(c);
  458. }
  459. }
  460. /** Release storage held by <b>options</b>. */
  461. void
  462. config_free_(const config_format_t *fmt, void *options)
  463. {
  464. int i;
  465. if (!options)
  466. return;
  467. tor_assert(fmt);
  468. for (i=0; fmt->vars[i].member.name; ++i)
  469. config_clear(fmt, options, &(fmt->vars[i]));
  470. if (fmt->extra) {
  471. config_line_t **linep = STRUCT_VAR_P(options, fmt->extra->offset);
  472. config_free_lines(*linep);
  473. *linep = NULL;
  474. }
  475. tor_free(options);
  476. }
  477. /** Return true iff the option <b>name</b> has the same value in <b>o1</b>
  478. * and <b>o2</b>. Must not be called for LINELIST_S or OBSOLETE options.
  479. */
  480. int
  481. config_is_same(const config_format_t *fmt,
  482. const void *o1, const void *o2,
  483. const char *name)
  484. {
  485. CONFIG_CHECK(fmt, o1);
  486. CONFIG_CHECK(fmt, o2);
  487. const config_var_t *var = config_find_option(fmt, name);
  488. if (!var) {
  489. return true;
  490. }
  491. return struct_var_eq(o1, o2, &var->member);
  492. }
  493. /** Copy storage held by <b>old</b> into a new or_options_t and return it. */
  494. void *
  495. config_dup(const config_format_t *fmt, const void *old)
  496. {
  497. void *newopts;
  498. int i;
  499. newopts = config_new(fmt);
  500. for (i=0; fmt->vars[i].member.name; ++i) {
  501. if (fmt->vars[i].member.type == CONFIG_TYPE_LINELIST_S)
  502. continue;
  503. if (fmt->vars[i].member.type == CONFIG_TYPE_OBSOLETE)
  504. continue;
  505. if (struct_var_copy(newopts, old, &fmt->vars[i].member) < 0) {
  506. // LCOV_EXCL_START
  507. log_err(LD_BUG, "Unable to copy value for %s.",
  508. fmt->vars[i].member.name);
  509. tor_assert_unreached();
  510. // LCOV_EXCL_STOP
  511. }
  512. }
  513. return newopts;
  514. }
  515. /** Set all vars in the configuration object <b>options</b> to their default
  516. * values. */
  517. void
  518. config_init(const config_format_t *fmt, void *options)
  519. {
  520. int i;
  521. const config_var_t *var;
  522. CONFIG_CHECK(fmt, options);
  523. for (i=0; fmt->vars[i].member.name; ++i) {
  524. var = &fmt->vars[i];
  525. if (!var->initvalue)
  526. continue; /* defaults to NULL or 0 */
  527. config_reset(fmt, options, var, 1);
  528. }
  529. }
  530. /** Allocate and return a new string holding the written-out values of the vars
  531. * in 'options'. If 'minimal', do not write out any default-valued vars.
  532. * Else, if comment_defaults, write default values as comments.
  533. */
  534. char *
  535. config_dump(const config_format_t *fmt, const void *default_options,
  536. const void *options, int minimal,
  537. int comment_defaults)
  538. {
  539. smartlist_t *elements;
  540. const void *defaults = default_options;
  541. void *defaults_tmp = NULL;
  542. config_line_t *line, *assigned;
  543. char *result;
  544. int i;
  545. char *msg = NULL;
  546. if (defaults == NULL) {
  547. defaults = defaults_tmp = config_new(fmt);
  548. config_init(fmt, defaults_tmp);
  549. }
  550. /* XXX use a 1 here so we don't add a new log line while dumping */
  551. if (default_options == NULL) {
  552. if (fmt->validate_fn(NULL, defaults_tmp, defaults_tmp, 1, &msg) < 0) {
  553. // LCOV_EXCL_START
  554. log_err(LD_BUG, "Failed to validate default config: %s", msg);
  555. tor_free(msg);
  556. tor_assert(0);
  557. // LCOV_EXCL_STOP
  558. }
  559. }
  560. elements = smartlist_new();
  561. for (i=0; fmt->vars[i].member.name; ++i) {
  562. int comment_option = 0;
  563. if (fmt->vars[i].member.type == CONFIG_TYPE_OBSOLETE ||
  564. fmt->vars[i].member.type == CONFIG_TYPE_LINELIST_S)
  565. continue;
  566. /* Don't save 'hidden' control variables. */
  567. if (!strcmpstart(fmt->vars[i].member.name, "__"))
  568. continue;
  569. if (minimal && config_is_same(fmt, options, defaults,
  570. fmt->vars[i].member.name))
  571. continue;
  572. else if (comment_defaults &&
  573. config_is_same(fmt, options, defaults, fmt->vars[i].member.name))
  574. comment_option = 1;
  575. line = assigned =
  576. config_get_assigned_option(fmt, options, fmt->vars[i].member.name, 1);
  577. for (; line; line = line->next) {
  578. if (!strcmpstart(line->key, "__")) {
  579. /* This check detects "hidden" variables inside LINELIST_V structures.
  580. */
  581. continue;
  582. }
  583. smartlist_add_asprintf(elements, "%s%s %s\n",
  584. comment_option ? "# " : "",
  585. line->key, line->value);
  586. }
  587. config_free_lines(assigned);
  588. }
  589. if (fmt->extra) {
  590. line = *(config_line_t**)STRUCT_VAR_P(options, fmt->extra->offset);
  591. for (; line; line = line->next) {
  592. smartlist_add_asprintf(elements, "%s %s\n", line->key, line->value);
  593. }
  594. }
  595. result = smartlist_join_strings(elements, "", 0, NULL);
  596. SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
  597. smartlist_free(elements);
  598. if (defaults_tmp) {
  599. fmt->free_fn(defaults_tmp);
  600. }
  601. return result;
  602. }
  603. /**
  604. * Return true if every member of <b>options</b> is in-range and well-formed.
  605. * Return false otherwise. Log errors at level <b>severity</b>.
  606. */
  607. bool
  608. config_check_ok(const config_format_t *fmt, const void *options, int severity)
  609. {
  610. bool all_ok = true;
  611. for (int i=0; fmt->vars[i].member.name; ++i) {
  612. if (!struct_var_ok(options, &fmt->vars[i].member)) {
  613. log_fn(severity, LD_BUG, "Invalid value for %s",
  614. fmt->vars[i].member.name);
  615. all_ok = false;
  616. }
  617. }
  618. return all_ok;
  619. }