confparse.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  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/container/bitarray.h"
  27. #include "lib/encoding/confline.h"
  28. static uint64_t config_parse_memunit(const char *s, int *ok);
  29. static int config_parse_msec_interval(const char *s, int *ok);
  30. static int config_parse_interval(const char *s, int *ok);
  31. static void config_reset(const config_format_t *fmt, void *options,
  32. const config_var_t *var, int use_defaults);
  33. /** Allocate an empty configuration object of a given format type. */
  34. void *
  35. config_new(const config_format_t *fmt)
  36. {
  37. void *opts = tor_malloc_zero(fmt->size);
  38. *(uint32_t*)STRUCT_VAR_P(opts, fmt->magic_offset) = fmt->magic;
  39. CONFIG_CHECK(fmt, opts);
  40. return opts;
  41. }
  42. /*
  43. * Functions to parse config options
  44. */
  45. /** If <b>option</b> is an official abbreviation for a longer option,
  46. * return the longer option. Otherwise return <b>option</b>.
  47. * If <b>command_line</b> is set, apply all abbreviations. Otherwise, only
  48. * apply abbreviations that work for the config file and the command line.
  49. * If <b>warn_obsolete</b> is set, warn about deprecated names. */
  50. const char *
  51. config_expand_abbrev(const config_format_t *fmt, const char *option,
  52. int command_line, int warn_obsolete)
  53. {
  54. int i;
  55. if (! fmt->abbrevs)
  56. return option;
  57. for (i=0; fmt->abbrevs[i].abbreviated; ++i) {
  58. /* Abbreviations are case insensitive. */
  59. if (!strcasecmp(option,fmt->abbrevs[i].abbreviated) &&
  60. (command_line || !fmt->abbrevs[i].commandline_only)) {
  61. if (warn_obsolete && fmt->abbrevs[i].warn) {
  62. log_warn(LD_CONFIG,
  63. "The configuration option '%s' is deprecated; "
  64. "use '%s' instead.",
  65. fmt->abbrevs[i].abbreviated,
  66. fmt->abbrevs[i].full);
  67. }
  68. /* Keep going through the list in case we want to rewrite it more.
  69. * (We could imagine recursing here, but I don't want to get the
  70. * user into an infinite loop if we craft our list wrong.) */
  71. option = fmt->abbrevs[i].full;
  72. }
  73. }
  74. return option;
  75. }
  76. /** If <b>key</b> is a deprecated configuration option, return the message
  77. * explaining why it is deprecated (which may be an empty string). Return NULL
  78. * if it is not deprecated. The <b>key</b> field must be fully expanded. */
  79. const char *
  80. config_find_deprecation(const config_format_t *fmt, const char *key)
  81. {
  82. if (BUG(fmt == NULL) || BUG(key == NULL))
  83. return NULL; // LCOV_EXCL_LINE
  84. if (fmt->deprecations == NULL)
  85. return NULL;
  86. const config_deprecation_t *d;
  87. for (d = fmt->deprecations; d->name; ++d) {
  88. if (!strcasecmp(d->name, key)) {
  89. return d->why_deprecated ? d->why_deprecated : "";
  90. }
  91. }
  92. return NULL;
  93. }
  94. /** As config_find_option, but return a non-const pointer. */
  95. config_var_t *
  96. config_find_option_mutable(config_format_t *fmt, const char *key)
  97. {
  98. int i;
  99. size_t keylen = strlen(key);
  100. if (!keylen)
  101. return NULL; /* if they say "--" on the command line, it's not an option */
  102. /* First, check for an exact (case-insensitive) match */
  103. for (i=0; fmt->vars[i].name; ++i) {
  104. if (!strcasecmp(key, fmt->vars[i].name)) {
  105. return &fmt->vars[i];
  106. }
  107. }
  108. /* If none, check for an abbreviated match */
  109. for (i=0; fmt->vars[i].name; ++i) {
  110. if (!strncasecmp(key, fmt->vars[i].name, keylen)) {
  111. log_warn(LD_CONFIG, "The abbreviation '%s' is deprecated. "
  112. "Please use '%s' instead",
  113. key, fmt->vars[i].name);
  114. return &fmt->vars[i];
  115. }
  116. }
  117. /* Okay, unrecognized option */
  118. return NULL;
  119. }
  120. /** If <b>key</b> is a configuration option, return the corresponding const
  121. * config_var_t. Otherwise, if <b>key</b> is a non-standard abbreviation,
  122. * warn, and return the corresponding const config_var_t. Otherwise return
  123. * NULL.
  124. */
  125. const config_var_t *
  126. config_find_option(const config_format_t *fmt, const char *key)
  127. {
  128. return config_find_option_mutable((config_format_t*)fmt, key);
  129. }
  130. /** Return the number of option entries in <b>fmt</b>. */
  131. static int
  132. config_count_options(const config_format_t *fmt)
  133. {
  134. int i;
  135. for (i=0; fmt->vars[i].name; ++i)
  136. ;
  137. return i;
  138. }
  139. /*
  140. * Functions to assign config options.
  141. */
  142. /** <b>c</b>-\>key is known to be a real key. Update <b>options</b>
  143. * with <b>c</b>-\>value and return 0, or return -1 if bad value.
  144. *
  145. * Called from config_assign_line() and option_reset().
  146. */
  147. static int
  148. config_assign_value(const config_format_t *fmt, void *options,
  149. config_line_t *c, char **msg)
  150. {
  151. int i, ok;
  152. const config_var_t *var;
  153. void *lvalue;
  154. CONFIG_CHECK(fmt, options);
  155. var = config_find_option(fmt, c->key);
  156. tor_assert(var);
  157. lvalue = STRUCT_VAR_P(options, var->var_offset);
  158. switch (var->type) {
  159. case CONFIG_TYPE_INT:
  160. case CONFIG_TYPE_POSINT:
  161. i = (int)tor_parse_long(c->value, 10,
  162. var->type==CONFIG_TYPE_INT ? INT_MIN : 0,
  163. INT_MAX,
  164. &ok, NULL);
  165. if (!ok) {
  166. tor_asprintf(msg,
  167. "Int keyword '%s %s' is malformed or out of bounds.",
  168. c->key, c->value);
  169. return -1;
  170. }
  171. *(int *)lvalue = i;
  172. break;
  173. case CONFIG_TYPE_UINT64: {
  174. uint64_t u64 = tor_parse_uint64(c->value, 10,
  175. 0, UINT64_MAX, &ok, NULL);
  176. if (!ok) {
  177. tor_asprintf(msg,
  178. "uint64 keyword '%s %s' is malformed or out of bounds.",
  179. c->key, c->value);
  180. return -1;
  181. }
  182. *(uint64_t *)lvalue = u64;
  183. break;
  184. }
  185. case CONFIG_TYPE_CSV_INTERVAL: {
  186. /* We used to have entire smartlists here. But now that all of our
  187. * download schedules use exponential backoff, only the first part
  188. * matters. */
  189. const char *comma = strchr(c->value, ',');
  190. const char *val = c->value;
  191. char *tmp = NULL;
  192. if (comma) {
  193. tmp = tor_strndup(c->value, comma - c->value);
  194. val = tmp;
  195. }
  196. i = config_parse_interval(val, &ok);
  197. if (!ok) {
  198. tor_asprintf(msg,
  199. "Interval '%s %s' is malformed or out of bounds.",
  200. c->key, c->value);
  201. tor_free(tmp);
  202. return -1;
  203. }
  204. *(int *)lvalue = i;
  205. tor_free(tmp);
  206. break;
  207. }
  208. case CONFIG_TYPE_INTERVAL: {
  209. i = config_parse_interval(c->value, &ok);
  210. if (!ok) {
  211. tor_asprintf(msg,
  212. "Interval '%s %s' is malformed or out of bounds.",
  213. c->key, c->value);
  214. return -1;
  215. }
  216. *(int *)lvalue = i;
  217. break;
  218. }
  219. case CONFIG_TYPE_MSEC_INTERVAL: {
  220. i = config_parse_msec_interval(c->value, &ok);
  221. if (!ok) {
  222. tor_asprintf(msg,
  223. "Msec interval '%s %s' is malformed or out of bounds.",
  224. c->key, c->value);
  225. return -1;
  226. }
  227. *(int *)lvalue = i;
  228. break;
  229. }
  230. case CONFIG_TYPE_MEMUNIT: {
  231. uint64_t u64 = config_parse_memunit(c->value, &ok);
  232. if (!ok) {
  233. tor_asprintf(msg,
  234. "Value '%s %s' is malformed or out of bounds.",
  235. c->key, c->value);
  236. return -1;
  237. }
  238. *(uint64_t *)lvalue = u64;
  239. break;
  240. }
  241. case CONFIG_TYPE_BOOL:
  242. i = (int)tor_parse_long(c->value, 10, 0, 1, &ok, NULL);
  243. if (!ok) {
  244. tor_asprintf(msg,
  245. "Boolean '%s %s' expects 0 or 1.",
  246. c->key, c->value);
  247. return -1;
  248. }
  249. *(int *)lvalue = i;
  250. break;
  251. case CONFIG_TYPE_AUTOBOOL:
  252. if (!strcasecmp(c->value, "auto"))
  253. *(int *)lvalue = -1;
  254. else if (!strcmp(c->value, "0"))
  255. *(int *)lvalue = 0;
  256. else if (!strcmp(c->value, "1"))
  257. *(int *)lvalue = 1;
  258. else {
  259. tor_asprintf(msg, "Boolean '%s %s' expects 0, 1, or 'auto'.",
  260. c->key, c->value);
  261. return -1;
  262. }
  263. break;
  264. case CONFIG_TYPE_STRING:
  265. case CONFIG_TYPE_FILENAME:
  266. tor_free(*(char **)lvalue);
  267. *(char **)lvalue = tor_strdup(c->value);
  268. break;
  269. case CONFIG_TYPE_DOUBLE:
  270. *(double *)lvalue = atof(c->value);
  271. break;
  272. case CONFIG_TYPE_ISOTIME:
  273. if (parse_iso_time(c->value, (time_t *)lvalue)) {
  274. tor_asprintf(msg,
  275. "Invalid time '%s' for keyword '%s'", c->value, c->key);
  276. return -1;
  277. }
  278. break;
  279. case CONFIG_TYPE_ROUTERSET:
  280. if (*(routerset_t**)lvalue) {
  281. routerset_free(*(routerset_t**)lvalue);
  282. }
  283. *(routerset_t**)lvalue = routerset_new();
  284. if (routerset_parse(*(routerset_t**)lvalue, c->value, c->key)<0) {
  285. tor_asprintf(msg, "Invalid exit list '%s' for option '%s'",
  286. c->value, c->key);
  287. return -1;
  288. }
  289. break;
  290. case CONFIG_TYPE_CSV:
  291. if (*(smartlist_t**)lvalue) {
  292. SMARTLIST_FOREACH(*(smartlist_t**)lvalue, char *, cp, tor_free(cp));
  293. smartlist_clear(*(smartlist_t**)lvalue);
  294. } else {
  295. *(smartlist_t**)lvalue = smartlist_new();
  296. }
  297. smartlist_split_string(*(smartlist_t**)lvalue, c->value, ",",
  298. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  299. break;
  300. case CONFIG_TYPE_LINELIST:
  301. case CONFIG_TYPE_LINELIST_S:
  302. {
  303. config_line_t *lastval = *(config_line_t**)lvalue;
  304. if (lastval && lastval->fragile) {
  305. if (c->command != CONFIG_LINE_APPEND) {
  306. config_free_lines(lastval);
  307. *(config_line_t**)lvalue = NULL;
  308. } else {
  309. lastval->fragile = 0;
  310. }
  311. }
  312. config_line_append((config_line_t**)lvalue, c->key, c->value);
  313. }
  314. break;
  315. case CONFIG_TYPE_OBSOLETE:
  316. log_warn(LD_CONFIG, "Skipping obsolete configuration option '%s'", c->key);
  317. break;
  318. case CONFIG_TYPE_LINELIST_V:
  319. tor_asprintf(msg,
  320. "You may not provide a value for virtual option '%s'", c->key);
  321. return -1;
  322. // LCOV_EXCL_START
  323. default:
  324. tor_assert_unreached();
  325. break;
  326. // LCOV_EXCL_STOP
  327. }
  328. return 0;
  329. }
  330. /** Mark every linelist in <b>options</b> "fragile", so that fresh assignments
  331. * to it will replace old ones. */
  332. static void
  333. config_mark_lists_fragile(const config_format_t *fmt, void *options)
  334. {
  335. int i;
  336. tor_assert(fmt);
  337. tor_assert(options);
  338. for (i = 0; fmt->vars[i].name; ++i) {
  339. const config_var_t *var = &fmt->vars[i];
  340. config_line_t *list;
  341. if (var->type != CONFIG_TYPE_LINELIST &&
  342. var->type != CONFIG_TYPE_LINELIST_V)
  343. continue;
  344. list = *(config_line_t **)STRUCT_VAR_P(options, var->var_offset);
  345. if (list)
  346. list->fragile = 1;
  347. }
  348. }
  349. void
  350. warn_deprecated_option(const char *what, const char *why)
  351. {
  352. const char *space = (why && strlen(why)) ? " " : "";
  353. log_warn(LD_CONFIG, "The %s option is deprecated, and will most likely "
  354. "be removed in a future version of Tor.%s%s (If you think this is "
  355. "a mistake, please let us know!)",
  356. what, space, why);
  357. }
  358. /** If <b>c</b> is a syntactically valid configuration line, update
  359. * <b>options</b> with its value and return 0. Otherwise return -1 for bad
  360. * key, -2 for bad value.
  361. *
  362. * If <b>clear_first</b> is set, clear the value first. Then if
  363. * <b>use_defaults</b> is set, set the value to the default.
  364. *
  365. * Called from config_assign().
  366. */
  367. static int
  368. config_assign_line(const config_format_t *fmt, void *options,
  369. config_line_t *c, unsigned flags,
  370. bitarray_t *options_seen, char **msg)
  371. {
  372. const unsigned use_defaults = flags & CAL_USE_DEFAULTS;
  373. const unsigned clear_first = flags & CAL_CLEAR_FIRST;
  374. const unsigned warn_deprecations = flags & CAL_WARN_DEPRECATIONS;
  375. const config_var_t *var;
  376. CONFIG_CHECK(fmt, options);
  377. var = config_find_option(fmt, c->key);
  378. if (!var) {
  379. if (fmt->extra) {
  380. void *lvalue = STRUCT_VAR_P(options, fmt->extra->var_offset);
  381. log_info(LD_CONFIG,
  382. "Found unrecognized option '%s'; saving it.", c->key);
  383. config_line_append((config_line_t**)lvalue, c->key, c->value);
  384. return 0;
  385. } else {
  386. tor_asprintf(msg,
  387. "Unknown option '%s'. Failing.", c->key);
  388. return -1;
  389. }
  390. }
  391. /* Put keyword into canonical case. */
  392. if (strcmp(var->name, c->key)) {
  393. tor_free(c->key);
  394. c->key = tor_strdup(var->name);
  395. }
  396. const char *deprecation_msg;
  397. if (warn_deprecations &&
  398. (deprecation_msg = config_find_deprecation(fmt, var->name))) {
  399. warn_deprecated_option(var->name, deprecation_msg);
  400. }
  401. if (!strlen(c->value)) {
  402. /* reset or clear it, then return */
  403. if (!clear_first) {
  404. if ((var->type == CONFIG_TYPE_LINELIST ||
  405. var->type == CONFIG_TYPE_LINELIST_S) &&
  406. c->command != CONFIG_LINE_CLEAR) {
  407. /* We got an empty linelist from the torrc or command line.
  408. As a special case, call this an error. Warn and ignore. */
  409. log_warn(LD_CONFIG,
  410. "Linelist option '%s' has no value. Skipping.", c->key);
  411. } else { /* not already cleared */
  412. config_reset(fmt, options, var, use_defaults);
  413. }
  414. }
  415. return 0;
  416. } else if (c->command == CONFIG_LINE_CLEAR && !clear_first) {
  417. // XXXX This is unreachable, since a CLEAR line always has an
  418. // XXXX empty value.
  419. config_reset(fmt, options, var, use_defaults); // LCOV_EXCL_LINE
  420. }
  421. if (options_seen && (var->type != CONFIG_TYPE_LINELIST &&
  422. var->type != CONFIG_TYPE_LINELIST_S)) {
  423. /* We're tracking which options we've seen, and this option is not
  424. * supposed to occur more than once. */
  425. int var_index = (int)(var - fmt->vars);
  426. if (bitarray_is_set(options_seen, var_index)) {
  427. log_warn(LD_CONFIG, "Option '%s' used more than once; all but the last "
  428. "value will be ignored.", var->name);
  429. }
  430. bitarray_set(options_seen, var_index);
  431. }
  432. if (config_assign_value(fmt, options, c, msg) < 0)
  433. return -2;
  434. return 0;
  435. }
  436. /** Restore the option named <b>key</b> in options to its default value.
  437. * Called from config_assign(). */
  438. STATIC void
  439. config_reset_line(const config_format_t *fmt, void *options,
  440. const char *key, int use_defaults)
  441. {
  442. const config_var_t *var;
  443. CONFIG_CHECK(fmt, options);
  444. var = config_find_option(fmt, key);
  445. if (!var)
  446. return; /* give error on next pass. */
  447. config_reset(fmt, options, var, use_defaults);
  448. }
  449. /** Return true iff value needs to be quoted and escaped to be used in
  450. * a configuration file. */
  451. static int
  452. config_value_needs_escape(const char *value)
  453. {
  454. if (*value == '\"')
  455. return 1;
  456. while (*value) {
  457. switch (*value)
  458. {
  459. case '\r':
  460. case '\n':
  461. case '#':
  462. /* Note: quotes and backspaces need special handling when we are using
  463. * quotes, not otherwise, so they don't trigger escaping on their
  464. * own. */
  465. return 1;
  466. default:
  467. if (!TOR_ISPRINT(*value))
  468. return 1;
  469. }
  470. ++value;
  471. }
  472. return 0;
  473. }
  474. /** Return newly allocated line or lines corresponding to <b>key</b> in the
  475. * configuration <b>options</b>. If <b>escape_val</b> is true and a
  476. * value needs to be quoted before it's put in a config file, quote and
  477. * escape that value. Return NULL if no such key exists. */
  478. config_line_t *
  479. config_get_assigned_option(const config_format_t *fmt, const void *options,
  480. const char *key, int escape_val)
  481. {
  482. const config_var_t *var;
  483. const void *value;
  484. config_line_t *result;
  485. tor_assert(options && key);
  486. CONFIG_CHECK(fmt, options);
  487. var = config_find_option(fmt, key);
  488. if (!var) {
  489. log_warn(LD_CONFIG, "Unknown option '%s'. Failing.", key);
  490. return NULL;
  491. }
  492. value = STRUCT_VAR_P(options, var->var_offset);
  493. result = tor_malloc_zero(sizeof(config_line_t));
  494. result->key = tor_strdup(var->name);
  495. switch (var->type)
  496. {
  497. case CONFIG_TYPE_STRING:
  498. case CONFIG_TYPE_FILENAME:
  499. if (*(char**)value) {
  500. result->value = tor_strdup(*(char**)value);
  501. } else {
  502. tor_free(result->key);
  503. tor_free(result);
  504. return NULL;
  505. }
  506. break;
  507. case CONFIG_TYPE_ISOTIME:
  508. if (*(time_t*)value) {
  509. result->value = tor_malloc(ISO_TIME_LEN+1);
  510. format_iso_time(result->value, *(time_t*)value);
  511. } else {
  512. tor_free(result->key);
  513. tor_free(result);
  514. }
  515. escape_val = 0; /* Can't need escape. */
  516. break;
  517. case CONFIG_TYPE_CSV_INTERVAL:
  518. case CONFIG_TYPE_INTERVAL:
  519. case CONFIG_TYPE_MSEC_INTERVAL:
  520. case CONFIG_TYPE_POSINT:
  521. case CONFIG_TYPE_INT:
  522. /* This means every or_options_t uint or bool element
  523. * needs to be an int. Not, say, a uint16_t or char. */
  524. tor_asprintf(&result->value, "%d", *(int*)value);
  525. escape_val = 0; /* Can't need escape. */
  526. break;
  527. case CONFIG_TYPE_UINT64: /* Fall through */
  528. case CONFIG_TYPE_MEMUNIT:
  529. tor_asprintf(&result->value, "%"PRIu64,
  530. (*(uint64_t*)value));
  531. escape_val = 0; /* Can't need escape. */
  532. break;
  533. case CONFIG_TYPE_DOUBLE:
  534. tor_asprintf(&result->value, "%f", *(double*)value);
  535. escape_val = 0; /* Can't need escape. */
  536. break;
  537. case CONFIG_TYPE_AUTOBOOL:
  538. if (*(int*)value == -1) {
  539. result->value = tor_strdup("auto");
  540. escape_val = 0;
  541. break;
  542. }
  543. /* fall through */
  544. case CONFIG_TYPE_BOOL:
  545. result->value = tor_strdup(*(int*)value ? "1" : "0");
  546. escape_val = 0; /* Can't need escape. */
  547. break;
  548. case CONFIG_TYPE_ROUTERSET:
  549. result->value = routerset_to_string(*(routerset_t**)value);
  550. break;
  551. case CONFIG_TYPE_CSV:
  552. if (*(smartlist_t**)value)
  553. result->value =
  554. smartlist_join_strings(*(smartlist_t**)value, ",", 0, NULL);
  555. else
  556. result->value = tor_strdup("");
  557. break;
  558. case CONFIG_TYPE_OBSOLETE:
  559. log_fn(LOG_INFO, LD_CONFIG,
  560. "You asked me for the value of an obsolete config option '%s'.",
  561. key);
  562. tor_free(result->key);
  563. tor_free(result);
  564. return NULL;
  565. case CONFIG_TYPE_LINELIST_S:
  566. tor_free(result->key);
  567. tor_free(result);
  568. result = config_lines_dup_and_filter(*(const config_line_t **)value,
  569. key);
  570. break;
  571. case CONFIG_TYPE_LINELIST:
  572. case CONFIG_TYPE_LINELIST_V:
  573. tor_free(result->key);
  574. tor_free(result);
  575. result = config_lines_dup(*(const config_line_t**)value);
  576. break;
  577. // LCOV_EXCL_START
  578. default:
  579. tor_free(result->key);
  580. tor_free(result);
  581. log_warn(LD_BUG,"Unknown type %d for known key '%s'",
  582. var->type, key);
  583. return NULL;
  584. // LCOV_EXCL_STOP
  585. }
  586. if (escape_val) {
  587. config_line_t *line;
  588. for (line = result; line; line = line->next) {
  589. if (line->value && config_value_needs_escape(line->value)) {
  590. char *newval = esc_for_log(line->value);
  591. tor_free(line->value);
  592. line->value = newval;
  593. }
  594. }
  595. }
  596. return result;
  597. }
  598. /** Iterate through the linked list of requested options <b>list</b>.
  599. * For each item, convert as appropriate and assign to <b>options</b>.
  600. * If an item is unrecognized, set *msg and return -1 immediately,
  601. * else return 0 for success.
  602. *
  603. * If <b>clear_first</b>, interpret config options as replacing (not
  604. * extending) their previous values. If <b>clear_first</b> is set,
  605. * then <b>use_defaults</b> to decide if you set to defaults after
  606. * clearing, or make the value 0 or NULL.
  607. *
  608. * Here are the use cases:
  609. * 1. A non-empty AllowInvalid line in your torrc. Appends to current
  610. * if linelist, replaces current if csv.
  611. * 2. An empty AllowInvalid line in your torrc. Should clear it.
  612. * 3. "RESETCONF AllowInvalid" sets it to default.
  613. * 4. "SETCONF AllowInvalid" makes it NULL.
  614. * 5. "SETCONF AllowInvalid=foo" clears it and sets it to "foo".
  615. *
  616. * Use_defaults Clear_first
  617. * 0 0 "append"
  618. * 1 0 undefined, don't use
  619. * 0 1 "set to null first"
  620. * 1 1 "set to defaults first"
  621. * Return 0 on success, -1 on bad key, -2 on bad value.
  622. *
  623. * As an additional special case, if a LINELIST config option has
  624. * no value and clear_first is 0, then warn and ignore it.
  625. */
  626. /*
  627. There are three call cases for config_assign() currently.
  628. Case one: Torrc entry
  629. options_init_from_torrc() calls config_assign(0, 0)
  630. calls config_assign_line(0, 0).
  631. if value is empty, calls config_reset(0) and returns.
  632. calls config_assign_value(), appends.
  633. Case two: setconf
  634. options_trial_assign() calls config_assign(0, 1)
  635. calls config_reset_line(0)
  636. calls config_reset(0)
  637. calls option_clear().
  638. calls config_assign_line(0, 1).
  639. if value is empty, returns.
  640. calls config_assign_value(), appends.
  641. Case three: resetconf
  642. options_trial_assign() calls config_assign(1, 1)
  643. calls config_reset_line(1)
  644. calls config_reset(1)
  645. calls option_clear().
  646. calls config_assign_value(default)
  647. calls config_assign_line(1, 1).
  648. returns.
  649. */
  650. int
  651. config_assign(const config_format_t *fmt, void *options, config_line_t *list,
  652. unsigned config_assign_flags, char **msg)
  653. {
  654. config_line_t *p;
  655. bitarray_t *options_seen;
  656. const int n_options = config_count_options(fmt);
  657. const unsigned clear_first = config_assign_flags & CAL_CLEAR_FIRST;
  658. const unsigned use_defaults = config_assign_flags & CAL_USE_DEFAULTS;
  659. CONFIG_CHECK(fmt, options);
  660. /* pass 1: normalize keys */
  661. for (p = list; p; p = p->next) {
  662. const char *full = config_expand_abbrev(fmt, p->key, 0, 1);
  663. if (strcmp(full,p->key)) {
  664. tor_free(p->key);
  665. p->key = tor_strdup(full);
  666. }
  667. }
  668. /* pass 2: if we're reading from a resetting source, clear all
  669. * mentioned config options, and maybe set to their defaults. */
  670. if (clear_first) {
  671. for (p = list; p; p = p->next)
  672. config_reset_line(fmt, options, p->key, use_defaults);
  673. }
  674. options_seen = bitarray_init_zero(n_options);
  675. /* pass 3: assign. */
  676. while (list) {
  677. int r;
  678. if ((r=config_assign_line(fmt, options, list, config_assign_flags,
  679. options_seen, msg))) {
  680. bitarray_free(options_seen);
  681. return r;
  682. }
  683. list = list->next;
  684. }
  685. bitarray_free(options_seen);
  686. /** Now we're done assigning a group of options to the configuration.
  687. * Subsequent group assignments should _replace_ linelists, not extend
  688. * them. */
  689. config_mark_lists_fragile(fmt, options);
  690. return 0;
  691. }
  692. /** Reset config option <b>var</b> to 0, 0.0, NULL, or the equivalent.
  693. * Called from config_reset() and config_free(). */
  694. static void
  695. config_clear(const config_format_t *fmt, void *options,
  696. const config_var_t *var)
  697. {
  698. void *lvalue = STRUCT_VAR_P(options, var->var_offset);
  699. (void)fmt; /* unused */
  700. switch (var->type) {
  701. case CONFIG_TYPE_STRING:
  702. case CONFIG_TYPE_FILENAME:
  703. tor_free(*(char**)lvalue);
  704. break;
  705. case CONFIG_TYPE_DOUBLE:
  706. *(double*)lvalue = 0.0;
  707. break;
  708. case CONFIG_TYPE_ISOTIME:
  709. *(time_t*)lvalue = 0;
  710. break;
  711. case CONFIG_TYPE_CSV_INTERVAL:
  712. case CONFIG_TYPE_INTERVAL:
  713. case CONFIG_TYPE_MSEC_INTERVAL:
  714. case CONFIG_TYPE_POSINT:
  715. case CONFIG_TYPE_INT:
  716. case CONFIG_TYPE_BOOL:
  717. *(int*)lvalue = 0;
  718. break;
  719. case CONFIG_TYPE_AUTOBOOL:
  720. *(int*)lvalue = -1;
  721. break;
  722. case CONFIG_TYPE_UINT64:
  723. case CONFIG_TYPE_MEMUNIT:
  724. *(uint64_t*)lvalue = 0;
  725. break;
  726. case CONFIG_TYPE_ROUTERSET:
  727. if (*(routerset_t**)lvalue) {
  728. routerset_free(*(routerset_t**)lvalue);
  729. *(routerset_t**)lvalue = NULL;
  730. }
  731. break;
  732. case CONFIG_TYPE_CSV:
  733. if (*(smartlist_t**)lvalue) {
  734. SMARTLIST_FOREACH(*(smartlist_t **)lvalue, char *, cp, tor_free(cp));
  735. smartlist_free(*(smartlist_t **)lvalue);
  736. *(smartlist_t **)lvalue = NULL;
  737. }
  738. break;
  739. case CONFIG_TYPE_LINELIST:
  740. case CONFIG_TYPE_LINELIST_S:
  741. config_free_lines(*(config_line_t **)lvalue);
  742. *(config_line_t **)lvalue = NULL;
  743. break;
  744. case CONFIG_TYPE_LINELIST_V:
  745. /* handled by linelist_s. */
  746. break;
  747. case CONFIG_TYPE_OBSOLETE:
  748. break;
  749. }
  750. }
  751. /** Clear the option indexed by <b>var</b> in <b>options</b>. Then if
  752. * <b>use_defaults</b>, set it to its default value.
  753. * Called by config_init() and option_reset_line() and option_assign_line(). */
  754. static void
  755. config_reset(const config_format_t *fmt, void *options,
  756. const config_var_t *var, int use_defaults)
  757. {
  758. config_line_t *c;
  759. char *msg = NULL;
  760. CONFIG_CHECK(fmt, options);
  761. config_clear(fmt, options, var); /* clear it first */
  762. if (!use_defaults)
  763. return; /* all done */
  764. if (var->initvalue) {
  765. c = tor_malloc_zero(sizeof(config_line_t));
  766. c->key = tor_strdup(var->name);
  767. c->value = tor_strdup(var->initvalue);
  768. if (config_assign_value(fmt, options, c, &msg) < 0) {
  769. // LCOV_EXCL_START
  770. log_warn(LD_BUG, "Failed to assign default: %s", msg);
  771. tor_free(msg); /* if this happens it's a bug */
  772. // LCOV_EXCL_STOP
  773. }
  774. config_free_lines(c);
  775. }
  776. }
  777. /** Release storage held by <b>options</b>. */
  778. void
  779. config_free_(const config_format_t *fmt, void *options)
  780. {
  781. int i;
  782. if (!options)
  783. return;
  784. tor_assert(fmt);
  785. for (i=0; fmt->vars[i].name; ++i)
  786. config_clear(fmt, options, &(fmt->vars[i]));
  787. if (fmt->extra) {
  788. config_line_t **linep = STRUCT_VAR_P(options, fmt->extra->var_offset);
  789. config_free_lines(*linep);
  790. *linep = NULL;
  791. }
  792. tor_free(options);
  793. }
  794. /** Return true iff the option <b>name</b> has the same value in <b>o1</b>
  795. * and <b>o2</b>. Must not be called for LINELIST_S or OBSOLETE options.
  796. */
  797. int
  798. config_is_same(const config_format_t *fmt,
  799. const void *o1, const void *o2,
  800. const char *name)
  801. {
  802. config_line_t *c1, *c2;
  803. int r = 1;
  804. CONFIG_CHECK(fmt, o1);
  805. CONFIG_CHECK(fmt, o2);
  806. c1 = config_get_assigned_option(fmt, o1, name, 0);
  807. c2 = config_get_assigned_option(fmt, o2, name, 0);
  808. r = config_lines_eq(c1, c2);
  809. config_free_lines(c1);
  810. config_free_lines(c2);
  811. return r;
  812. }
  813. /** Copy storage held by <b>old</b> into a new or_options_t and return it. */
  814. void *
  815. config_dup(const config_format_t *fmt, const void *old)
  816. {
  817. void *newopts;
  818. int i;
  819. config_line_t *line;
  820. newopts = config_new(fmt);
  821. for (i=0; fmt->vars[i].name; ++i) {
  822. if (fmt->vars[i].type == CONFIG_TYPE_LINELIST_S)
  823. continue;
  824. if (fmt->vars[i].type == CONFIG_TYPE_OBSOLETE)
  825. continue;
  826. line = config_get_assigned_option(fmt, old, fmt->vars[i].name, 0);
  827. if (line) {
  828. char *msg = NULL;
  829. if (config_assign(fmt, newopts, line, 0, &msg) < 0) {
  830. // LCOV_EXCL_START
  831. log_err(LD_BUG, "config_get_assigned_option() generated "
  832. "something we couldn't config_assign(): %s", msg);
  833. tor_free(msg);
  834. tor_assert(0);
  835. // LCOV_EXCL_STOP
  836. }
  837. }
  838. config_free_lines(line);
  839. }
  840. return newopts;
  841. }
  842. /** Set all vars in the configuration object <b>options</b> to their default
  843. * values. */
  844. void
  845. config_init(const config_format_t *fmt, void *options)
  846. {
  847. int i;
  848. const config_var_t *var;
  849. CONFIG_CHECK(fmt, options);
  850. for (i=0; fmt->vars[i].name; ++i) {
  851. var = &fmt->vars[i];
  852. if (!var->initvalue)
  853. continue; /* defaults to NULL or 0 */
  854. config_reset(fmt, options, var, 1);
  855. }
  856. }
  857. /** Allocate and return a new string holding the written-out values of the vars
  858. * in 'options'. If 'minimal', do not write out any default-valued vars.
  859. * Else, if comment_defaults, write default values as comments.
  860. */
  861. char *
  862. config_dump(const config_format_t *fmt, const void *default_options,
  863. const void *options, int minimal,
  864. int comment_defaults)
  865. {
  866. smartlist_t *elements;
  867. const void *defaults = default_options;
  868. void *defaults_tmp = NULL;
  869. config_line_t *line, *assigned;
  870. char *result;
  871. int i;
  872. char *msg = NULL;
  873. if (defaults == NULL) {
  874. defaults = defaults_tmp = config_new(fmt);
  875. config_init(fmt, defaults_tmp);
  876. }
  877. /* XXX use a 1 here so we don't add a new log line while dumping */
  878. if (default_options == NULL) {
  879. if (fmt->validate_fn(NULL, defaults_tmp, defaults_tmp, 1, &msg) < 0) {
  880. // LCOV_EXCL_START
  881. log_err(LD_BUG, "Failed to validate default config: %s", msg);
  882. tor_free(msg);
  883. tor_assert(0);
  884. // LCOV_EXCL_STOP
  885. }
  886. }
  887. elements = smartlist_new();
  888. for (i=0; fmt->vars[i].name; ++i) {
  889. int comment_option = 0;
  890. if (fmt->vars[i].type == CONFIG_TYPE_OBSOLETE ||
  891. fmt->vars[i].type == CONFIG_TYPE_LINELIST_S)
  892. continue;
  893. /* Don't save 'hidden' control variables. */
  894. if (!strcmpstart(fmt->vars[i].name, "__"))
  895. continue;
  896. if (minimal && config_is_same(fmt, options, defaults, fmt->vars[i].name))
  897. continue;
  898. else if (comment_defaults &&
  899. config_is_same(fmt, options, defaults, fmt->vars[i].name))
  900. comment_option = 1;
  901. line = assigned =
  902. config_get_assigned_option(fmt, options, fmt->vars[i].name, 1);
  903. for (; line; line = line->next) {
  904. if (!strcmpstart(line->key, "__")) {
  905. /* This check detects "hidden" variables inside LINELIST_V structures.
  906. */
  907. continue;
  908. }
  909. smartlist_add_asprintf(elements, "%s%s %s\n",
  910. comment_option ? "# " : "",
  911. line->key, line->value);
  912. }
  913. config_free_lines(assigned);
  914. }
  915. if (fmt->extra) {
  916. line = *(config_line_t**)STRUCT_VAR_P(options, fmt->extra->var_offset);
  917. for (; line; line = line->next) {
  918. smartlist_add_asprintf(elements, "%s %s\n", line->key, line->value);
  919. }
  920. }
  921. result = smartlist_join_strings(elements, "", 0, NULL);
  922. SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
  923. smartlist_free(elements);
  924. if (defaults_tmp) {
  925. fmt->free_fn(defaults_tmp);
  926. }
  927. return result;
  928. }
  929. /** Mapping from a unit name to a multiplier for converting that unit into a
  930. * base unit. Used by config_parse_unit. */
  931. struct unit_table_t {
  932. const char *unit; /**< The name of the unit */
  933. uint64_t multiplier; /**< How many of the base unit appear in this unit */
  934. };
  935. /** Table to map the names of memory units to the number of bytes they
  936. * contain. */
  937. static struct unit_table_t memory_units[] = {
  938. { "", 1 },
  939. { "b", 1<< 0 },
  940. { "byte", 1<< 0 },
  941. { "bytes", 1<< 0 },
  942. { "kb", 1<<10 },
  943. { "kbyte", 1<<10 },
  944. { "kbytes", 1<<10 },
  945. { "kilobyte", 1<<10 },
  946. { "kilobytes", 1<<10 },
  947. { "kilobits", 1<<7 },
  948. { "kilobit", 1<<7 },
  949. { "kbits", 1<<7 },
  950. { "kbit", 1<<7 },
  951. { "m", 1<<20 },
  952. { "mb", 1<<20 },
  953. { "mbyte", 1<<20 },
  954. { "mbytes", 1<<20 },
  955. { "megabyte", 1<<20 },
  956. { "megabytes", 1<<20 },
  957. { "megabits", 1<<17 },
  958. { "megabit", 1<<17 },
  959. { "mbits", 1<<17 },
  960. { "mbit", 1<<17 },
  961. { "gb", 1<<30 },
  962. { "gbyte", 1<<30 },
  963. { "gbytes", 1<<30 },
  964. { "gigabyte", 1<<30 },
  965. { "gigabytes", 1<<30 },
  966. { "gigabits", 1<<27 },
  967. { "gigabit", 1<<27 },
  968. { "gbits", 1<<27 },
  969. { "gbit", 1<<27 },
  970. { "tb", UINT64_C(1)<<40 },
  971. { "tbyte", UINT64_C(1)<<40 },
  972. { "tbytes", UINT64_C(1)<<40 },
  973. { "terabyte", UINT64_C(1)<<40 },
  974. { "terabytes", UINT64_C(1)<<40 },
  975. { "terabits", UINT64_C(1)<<37 },
  976. { "terabit", UINT64_C(1)<<37 },
  977. { "tbits", UINT64_C(1)<<37 },
  978. { "tbit", UINT64_C(1)<<37 },
  979. { NULL, 0 },
  980. };
  981. /** Table to map the names of time units to the number of seconds they
  982. * contain. */
  983. static struct unit_table_t time_units[] = {
  984. { "", 1 },
  985. { "second", 1 },
  986. { "seconds", 1 },
  987. { "minute", 60 },
  988. { "minutes", 60 },
  989. { "hour", 60*60 },
  990. { "hours", 60*60 },
  991. { "day", 24*60*60 },
  992. { "days", 24*60*60 },
  993. { "week", 7*24*60*60 },
  994. { "weeks", 7*24*60*60 },
  995. { "month", 2629728, }, /* about 30.437 days */
  996. { "months", 2629728, },
  997. { NULL, 0 },
  998. };
  999. /** Table to map the names of time units to the number of milliseconds
  1000. * they contain. */
  1001. static struct unit_table_t time_msec_units[] = {
  1002. { "", 1 },
  1003. { "msec", 1 },
  1004. { "millisecond", 1 },
  1005. { "milliseconds", 1 },
  1006. { "second", 1000 },
  1007. { "seconds", 1000 },
  1008. { "minute", 60*1000 },
  1009. { "minutes", 60*1000 },
  1010. { "hour", 60*60*1000 },
  1011. { "hours", 60*60*1000 },
  1012. { "day", 24*60*60*1000 },
  1013. { "days", 24*60*60*1000 },
  1014. { "week", 7*24*60*60*1000 },
  1015. { "weeks", 7*24*60*60*1000 },
  1016. { NULL, 0 },
  1017. };
  1018. /** Parse a string <b>val</b> containing a number, zero or more
  1019. * spaces, and an optional unit string. If the unit appears in the
  1020. * table <b>u</b>, then multiply the number by the unit multiplier.
  1021. * On success, set *<b>ok</b> to 1 and return this product.
  1022. * Otherwise, set *<b>ok</b> to 0.
  1023. */
  1024. static uint64_t
  1025. config_parse_units(const char *val, struct unit_table_t *u, int *ok)
  1026. {
  1027. uint64_t v = 0;
  1028. double d = 0;
  1029. int use_float = 0;
  1030. char *cp;
  1031. tor_assert(ok);
  1032. v = tor_parse_uint64(val, 10, 0, UINT64_MAX, ok, &cp);
  1033. if (!*ok || (cp && *cp == '.')) {
  1034. d = tor_parse_double(val, 0, (double)UINT64_MAX, ok, &cp);
  1035. if (!*ok)
  1036. goto done;
  1037. use_float = 1;
  1038. }
  1039. if (!cp) {
  1040. *ok = 1;
  1041. v = use_float ? ((uint64_t)d) : v;
  1042. goto done;
  1043. }
  1044. cp = (char*) eat_whitespace(cp);
  1045. for ( ;u->unit;++u) {
  1046. if (!strcasecmp(u->unit, cp)) {
  1047. if (use_float)
  1048. v = (uint64_t)(u->multiplier * d);
  1049. else
  1050. v *= u->multiplier;
  1051. *ok = 1;
  1052. goto done;
  1053. }
  1054. }
  1055. log_warn(LD_CONFIG, "Unknown unit '%s'.", cp);
  1056. *ok = 0;
  1057. done:
  1058. if (*ok)
  1059. return v;
  1060. else
  1061. return 0;
  1062. }
  1063. /** Parse a string in the format "number unit", where unit is a unit of
  1064. * information (byte, KB, M, etc). On success, set *<b>ok</b> to true
  1065. * and return the number of bytes specified. Otherwise, set
  1066. * *<b>ok</b> to false and return 0. */
  1067. static uint64_t
  1068. config_parse_memunit(const char *s, int *ok)
  1069. {
  1070. uint64_t u = config_parse_units(s, memory_units, ok);
  1071. return u;
  1072. }
  1073. /** Parse a string in the format "number unit", where unit is a unit of
  1074. * time in milliseconds. On success, set *<b>ok</b> to true and return
  1075. * the number of milliseconds in the provided interval. Otherwise, set
  1076. * *<b>ok</b> to 0 and return -1. */
  1077. static int
  1078. config_parse_msec_interval(const char *s, int *ok)
  1079. {
  1080. uint64_t r;
  1081. r = config_parse_units(s, time_msec_units, ok);
  1082. if (r > INT_MAX) {
  1083. log_warn(LD_CONFIG, "Msec interval '%s' is too long", s);
  1084. *ok = 0;
  1085. return -1;
  1086. }
  1087. return (int)r;
  1088. }
  1089. /** Parse a string in the format "number unit", where unit is a unit of time.
  1090. * On success, set *<b>ok</b> to true and return the number of seconds in
  1091. * the provided interval. Otherwise, set *<b>ok</b> to 0 and return -1.
  1092. */
  1093. static int
  1094. config_parse_interval(const char *s, int *ok)
  1095. {
  1096. uint64_t r;
  1097. r = config_parse_units(s, time_units, ok);
  1098. if (r > INT_MAX) {
  1099. log_warn(LD_CONFIG, "Interval '%s' is too long", s);
  1100. *ok = 0;
  1101. return -1;
  1102. }
  1103. return (int)r;
  1104. }