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. #include "core/or/or.h"
  23. #include "app/config/confparse.h"
  24. #include "feature/nodelist/routerset.h"
  25. #include "lib/container/bitarray.h"
  26. #include "lib/encoding/confline.h"
  27. static uint64_t config_parse_memunit(const char *s, int *ok);
  28. static int config_parse_msec_interval(const char *s, int *ok);
  29. static int config_parse_interval(const char *s, int *ok);
  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. *(uint32_t*)STRUCT_VAR_P(opts, fmt->magic_offset) = 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;
  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].name; ++i) {
  103. if (!strcasecmp(key, fmt->vars[i].name)) {
  104. return &fmt->vars[i];
  105. }
  106. }
  107. /* If none, check for an abbreviated match */
  108. for (i=0; fmt->vars[i].name; ++i) {
  109. if (!strncasecmp(key, fmt->vars[i].name, keylen)) {
  110. log_warn(LD_CONFIG, "The abbreviation '%s' is deprecated. "
  111. "Please use '%s' instead",
  112. key, fmt->vars[i].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].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. int i, ok;
  151. const config_var_t *var;
  152. void *lvalue;
  153. CONFIG_CHECK(fmt, options);
  154. var = config_find_option(fmt, c->key);
  155. tor_assert(var);
  156. lvalue = STRUCT_VAR_P(options, var->var_offset);
  157. switch (var->type) {
  158. case CONFIG_TYPE_PORT:
  159. if (!strcasecmp(c->value, "auto")) {
  160. *(int *)lvalue = CFG_AUTO_PORT;
  161. break;
  162. }
  163. /* fall through */
  164. case CONFIG_TYPE_INT:
  165. case CONFIG_TYPE_UINT:
  166. i = (int)tor_parse_long(c->value, 10,
  167. var->type==CONFIG_TYPE_INT ? INT_MIN : 0,
  168. var->type==CONFIG_TYPE_PORT ? 65535 : INT_MAX,
  169. &ok, NULL);
  170. if (!ok) {
  171. tor_asprintf(msg,
  172. "Int keyword '%s %s' is malformed or out of bounds.",
  173. c->key, c->value);
  174. return -1;
  175. }
  176. *(int *)lvalue = i;
  177. break;
  178. case CONFIG_TYPE_UINT64: {
  179. uint64_t u64 = tor_parse_uint64(c->value, 10,
  180. 0, UINT64_MAX, &ok, NULL);
  181. if (!ok) {
  182. tor_asprintf(msg,
  183. "uint64 keyword '%s %s' is malformed or out of bounds.",
  184. c->key, c->value);
  185. return -1;
  186. }
  187. *(uint64_t *)lvalue = u64;
  188. break;
  189. }
  190. case CONFIG_TYPE_CSV_INTERVAL: {
  191. /* We used to have entire smartlists here. But now that all of our
  192. * download schedules use exponential backoff, only the first part
  193. * matters. */
  194. const char *comma = strchr(c->value, ',');
  195. const char *val = c->value;
  196. char *tmp = NULL;
  197. if (comma) {
  198. tmp = tor_strndup(c->value, comma - c->value);
  199. val = tmp;
  200. }
  201. i = config_parse_interval(val, &ok);
  202. if (!ok) {
  203. tor_asprintf(msg,
  204. "Interval '%s %s' is malformed or out of bounds.",
  205. c->key, c->value);
  206. return -1;
  207. }
  208. *(int *)lvalue = i;
  209. tor_free(tmp);
  210. break;
  211. }
  212. case CONFIG_TYPE_INTERVAL: {
  213. i = config_parse_interval(c->value, &ok);
  214. if (!ok) {
  215. tor_asprintf(msg,
  216. "Interval '%s %s' is malformed or out of bounds.",
  217. c->key, c->value);
  218. return -1;
  219. }
  220. *(int *)lvalue = i;
  221. break;
  222. }
  223. case CONFIG_TYPE_MSEC_INTERVAL: {
  224. i = config_parse_msec_interval(c->value, &ok);
  225. if (!ok) {
  226. tor_asprintf(msg,
  227. "Msec interval '%s %s' is malformed or out of bounds.",
  228. c->key, c->value);
  229. return -1;
  230. }
  231. *(int *)lvalue = i;
  232. break;
  233. }
  234. case CONFIG_TYPE_MEMUNIT: {
  235. uint64_t u64 = config_parse_memunit(c->value, &ok);
  236. if (!ok) {
  237. tor_asprintf(msg,
  238. "Value '%s %s' is malformed or out of bounds.",
  239. c->key, c->value);
  240. return -1;
  241. }
  242. *(uint64_t *)lvalue = u64;
  243. break;
  244. }
  245. case CONFIG_TYPE_BOOL:
  246. i = (int)tor_parse_long(c->value, 10, 0, 1, &ok, NULL);
  247. if (!ok) {
  248. tor_asprintf(msg,
  249. "Boolean '%s %s' expects 0 or 1.",
  250. c->key, c->value);
  251. return -1;
  252. }
  253. *(int *)lvalue = i;
  254. break;
  255. case CONFIG_TYPE_AUTOBOOL:
  256. if (!strcasecmp(c->value, "auto"))
  257. *(int *)lvalue = -1;
  258. else if (!strcmp(c->value, "0"))
  259. *(int *)lvalue = 0;
  260. else if (!strcmp(c->value, "1"))
  261. *(int *)lvalue = 1;
  262. else {
  263. tor_asprintf(msg, "Boolean '%s %s' expects 0, 1, or 'auto'.",
  264. c->key, c->value);
  265. return -1;
  266. }
  267. break;
  268. case CONFIG_TYPE_STRING:
  269. case CONFIG_TYPE_FILENAME:
  270. tor_free(*(char **)lvalue);
  271. *(char **)lvalue = tor_strdup(c->value);
  272. break;
  273. case CONFIG_TYPE_DOUBLE:
  274. *(double *)lvalue = atof(c->value);
  275. break;
  276. case CONFIG_TYPE_ISOTIME:
  277. if (parse_iso_time(c->value, (time_t *)lvalue)) {
  278. tor_asprintf(msg,
  279. "Invalid time '%s' for keyword '%s'", c->value, c->key);
  280. return -1;
  281. }
  282. break;
  283. case CONFIG_TYPE_ROUTERSET:
  284. if (*(routerset_t**)lvalue) {
  285. routerset_free(*(routerset_t**)lvalue);
  286. }
  287. *(routerset_t**)lvalue = routerset_new();
  288. if (routerset_parse(*(routerset_t**)lvalue, c->value, c->key)<0) {
  289. tor_asprintf(msg, "Invalid exit list '%s' for option '%s'",
  290. c->value, c->key);
  291. return -1;
  292. }
  293. break;
  294. case CONFIG_TYPE_CSV:
  295. if (*(smartlist_t**)lvalue) {
  296. SMARTLIST_FOREACH(*(smartlist_t**)lvalue, char *, cp, tor_free(cp));
  297. smartlist_clear(*(smartlist_t**)lvalue);
  298. } else {
  299. *(smartlist_t**)lvalue = smartlist_new();
  300. }
  301. smartlist_split_string(*(smartlist_t**)lvalue, c->value, ",",
  302. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  303. break;
  304. case CONFIG_TYPE_LINELIST:
  305. case CONFIG_TYPE_LINELIST_S:
  306. {
  307. config_line_t *lastval = *(config_line_t**)lvalue;
  308. if (lastval && lastval->fragile) {
  309. if (c->command != CONFIG_LINE_APPEND) {
  310. config_free_lines(lastval);
  311. *(config_line_t**)lvalue = NULL;
  312. } else {
  313. lastval->fragile = 0;
  314. }
  315. }
  316. config_line_append((config_line_t**)lvalue, c->key, c->value);
  317. }
  318. break;
  319. case CONFIG_TYPE_OBSOLETE:
  320. log_warn(LD_CONFIG, "Skipping obsolete configuration option '%s'", c->key);
  321. break;
  322. case CONFIG_TYPE_LINELIST_V:
  323. tor_asprintf(msg,
  324. "You may not provide a value for virtual option '%s'", c->key);
  325. return -1;
  326. default:
  327. tor_assert(0);
  328. break;
  329. }
  330. return 0;
  331. }
  332. /** Mark every linelist in <b>options</b> "fragile", so that fresh assignments
  333. * to it will replace old ones. */
  334. static void
  335. config_mark_lists_fragile(const config_format_t *fmt, void *options)
  336. {
  337. int i;
  338. tor_assert(fmt);
  339. tor_assert(options);
  340. for (i = 0; fmt->vars[i].name; ++i) {
  341. const config_var_t *var = &fmt->vars[i];
  342. config_line_t *list;
  343. if (var->type != CONFIG_TYPE_LINELIST &&
  344. var->type != CONFIG_TYPE_LINELIST_V)
  345. continue;
  346. list = *(config_line_t **)STRUCT_VAR_P(options, var->var_offset);
  347. if (list)
  348. list->fragile = 1;
  349. }
  350. }
  351. void
  352. warn_deprecated_option(const char *what, const char *why)
  353. {
  354. const char *space = (why && strlen(why)) ? " " : "";
  355. log_warn(LD_CONFIG, "The %s option is deprecated, and will most likely "
  356. "be removed in a future version of Tor.%s%s (If you think this is "
  357. "a mistake, please let us know!)",
  358. what, space, why);
  359. }
  360. /** If <b>c</b> is a syntactically valid configuration line, update
  361. * <b>options</b> with its value and return 0. Otherwise return -1 for bad
  362. * key, -2 for bad value.
  363. *
  364. * If <b>clear_first</b> is set, clear the value first. Then if
  365. * <b>use_defaults</b> is set, set the value to the default.
  366. *
  367. * Called from config_assign().
  368. */
  369. static int
  370. config_assign_line(const config_format_t *fmt, void *options,
  371. config_line_t *c, unsigned flags,
  372. bitarray_t *options_seen, char **msg)
  373. {
  374. const unsigned use_defaults = flags & CAL_USE_DEFAULTS;
  375. const unsigned clear_first = flags & CAL_CLEAR_FIRST;
  376. const unsigned warn_deprecations = flags & CAL_WARN_DEPRECATIONS;
  377. const config_var_t *var;
  378. CONFIG_CHECK(fmt, options);
  379. var = config_find_option(fmt, c->key);
  380. if (!var) {
  381. if (fmt->extra) {
  382. void *lvalue = STRUCT_VAR_P(options, fmt->extra->var_offset);
  383. log_info(LD_CONFIG,
  384. "Found unrecognized option '%s'; saving it.", c->key);
  385. config_line_append((config_line_t**)lvalue, c->key, c->value);
  386. return 0;
  387. } else {
  388. tor_asprintf(msg,
  389. "Unknown option '%s'. Failing.", c->key);
  390. return -1;
  391. }
  392. }
  393. /* Put keyword into canonical case. */
  394. if (strcmp(var->name, c->key)) {
  395. tor_free(c->key);
  396. c->key = tor_strdup(var->name);
  397. }
  398. const char *deprecation_msg;
  399. if (warn_deprecations &&
  400. (deprecation_msg = config_find_deprecation(fmt, var->name))) {
  401. warn_deprecated_option(var->name, deprecation_msg);
  402. }
  403. if (!strlen(c->value)) {
  404. /* reset or clear it, then return */
  405. if (!clear_first) {
  406. if ((var->type == CONFIG_TYPE_LINELIST ||
  407. var->type == CONFIG_TYPE_LINELIST_S) &&
  408. c->command != CONFIG_LINE_CLEAR) {
  409. /* We got an empty linelist from the torrc or command line.
  410. As a special case, call this an error. Warn and ignore. */
  411. log_warn(LD_CONFIG,
  412. "Linelist option '%s' has no value. Skipping.", c->key);
  413. } else { /* not already cleared */
  414. config_reset(fmt, options, var, use_defaults);
  415. }
  416. }
  417. return 0;
  418. } else if (c->command == CONFIG_LINE_CLEAR && !clear_first) {
  419. config_reset(fmt, options, var, use_defaults);
  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_PORT:
  518. if (*(int*)value == CFG_AUTO_PORT) {
  519. result->value = tor_strdup("auto");
  520. escape_val = 0;
  521. break;
  522. }
  523. /* fall through */
  524. case CONFIG_TYPE_CSV_INTERVAL:
  525. case CONFIG_TYPE_INTERVAL:
  526. case CONFIG_TYPE_MSEC_INTERVAL:
  527. case CONFIG_TYPE_UINT:
  528. case CONFIG_TYPE_INT:
  529. /* This means every or_options_t uint or bool element
  530. * needs to be an int. Not, say, a uint16_t or char. */
  531. tor_asprintf(&result->value, "%d", *(int*)value);
  532. escape_val = 0; /* Can't need escape. */
  533. break;
  534. case CONFIG_TYPE_UINT64: /* Fall through */
  535. case CONFIG_TYPE_MEMUNIT:
  536. tor_asprintf(&result->value, "%"PRIu64,
  537. (*(uint64_t*)value));
  538. escape_val = 0; /* Can't need escape. */
  539. break;
  540. case CONFIG_TYPE_DOUBLE:
  541. tor_asprintf(&result->value, "%f", *(double*)value);
  542. escape_val = 0; /* Can't need escape. */
  543. break;
  544. case CONFIG_TYPE_AUTOBOOL:
  545. if (*(int*)value == -1) {
  546. result->value = tor_strdup("auto");
  547. escape_val = 0;
  548. break;
  549. }
  550. /* fall through */
  551. case CONFIG_TYPE_BOOL:
  552. result->value = tor_strdup(*(int*)value ? "1" : "0");
  553. escape_val = 0; /* Can't need escape. */
  554. break;
  555. case CONFIG_TYPE_ROUTERSET:
  556. result->value = routerset_to_string(*(routerset_t**)value);
  557. break;
  558. case CONFIG_TYPE_CSV:
  559. if (*(smartlist_t**)value)
  560. result->value =
  561. smartlist_join_strings(*(smartlist_t**)value, ",", 0, NULL);
  562. else
  563. result->value = tor_strdup("");
  564. break;
  565. case CONFIG_TYPE_OBSOLETE:
  566. log_fn(LOG_INFO, LD_CONFIG,
  567. "You asked me for the value of an obsolete config option '%s'.",
  568. key);
  569. tor_free(result->key);
  570. tor_free(result);
  571. return NULL;
  572. case CONFIG_TYPE_LINELIST_S:
  573. tor_free(result->key);
  574. tor_free(result);
  575. result = config_lines_dup_and_filter(*(const config_line_t **)value,
  576. key);
  577. break;
  578. case CONFIG_TYPE_LINELIST:
  579. case CONFIG_TYPE_LINELIST_V:
  580. tor_free(result->key);
  581. tor_free(result);
  582. result = config_lines_dup(*(const config_line_t**)value);
  583. break;
  584. default:
  585. tor_free(result->key);
  586. tor_free(result);
  587. log_warn(LD_BUG,"Unknown type %d for known key '%s'",
  588. var->type, key);
  589. return NULL;
  590. }
  591. if (escape_val) {
  592. config_line_t *line;
  593. for (line = result; line; line = line->next) {
  594. if (line->value && config_value_needs_escape(line->value)) {
  595. char *newval = esc_for_log(line->value);
  596. tor_free(line->value);
  597. line->value = newval;
  598. }
  599. }
  600. }
  601. return result;
  602. }
  603. /** Iterate through the linked list of requested options <b>list</b>.
  604. * For each item, convert as appropriate and assign to <b>options</b>.
  605. * If an item is unrecognized, set *msg and return -1 immediately,
  606. * else return 0 for success.
  607. *
  608. * If <b>clear_first</b>, interpret config options as replacing (not
  609. * extending) their previous values. If <b>clear_first</b> is set,
  610. * then <b>use_defaults</b> to decide if you set to defaults after
  611. * clearing, or make the value 0 or NULL.
  612. *
  613. * Here are the use cases:
  614. * 1. A non-empty AllowInvalid line in your torrc. Appends to current
  615. * if linelist, replaces current if csv.
  616. * 2. An empty AllowInvalid line in your torrc. Should clear it.
  617. * 3. "RESETCONF AllowInvalid" sets it to default.
  618. * 4. "SETCONF AllowInvalid" makes it NULL.
  619. * 5. "SETCONF AllowInvalid=foo" clears it and sets it to "foo".
  620. *
  621. * Use_defaults Clear_first
  622. * 0 0 "append"
  623. * 1 0 undefined, don't use
  624. * 0 1 "set to null first"
  625. * 1 1 "set to defaults first"
  626. * Return 0 on success, -1 on bad key, -2 on bad value.
  627. *
  628. * As an additional special case, if a LINELIST config option has
  629. * no value and clear_first is 0, then warn and ignore it.
  630. */
  631. /*
  632. There are three call cases for config_assign() currently.
  633. Case one: Torrc entry
  634. options_init_from_torrc() calls config_assign(0, 0)
  635. calls config_assign_line(0, 0).
  636. if value is empty, calls config_reset(0) and returns.
  637. calls config_assign_value(), appends.
  638. Case two: setconf
  639. options_trial_assign() calls config_assign(0, 1)
  640. calls config_reset_line(0)
  641. calls config_reset(0)
  642. calls option_clear().
  643. calls config_assign_line(0, 1).
  644. if value is empty, returns.
  645. calls config_assign_value(), appends.
  646. Case three: resetconf
  647. options_trial_assign() calls config_assign(1, 1)
  648. calls config_reset_line(1)
  649. calls config_reset(1)
  650. calls option_clear().
  651. calls config_assign_value(default)
  652. calls config_assign_line(1, 1).
  653. returns.
  654. */
  655. int
  656. config_assign(const config_format_t *fmt, void *options, config_line_t *list,
  657. unsigned config_assign_flags, char **msg)
  658. {
  659. config_line_t *p;
  660. bitarray_t *options_seen;
  661. const int n_options = config_count_options(fmt);
  662. const unsigned clear_first = config_assign_flags & CAL_CLEAR_FIRST;
  663. const unsigned use_defaults = config_assign_flags & CAL_USE_DEFAULTS;
  664. CONFIG_CHECK(fmt, options);
  665. /* pass 1: normalize keys */
  666. for (p = list; p; p = p->next) {
  667. const char *full = config_expand_abbrev(fmt, p->key, 0, 1);
  668. if (strcmp(full,p->key)) {
  669. tor_free(p->key);
  670. p->key = tor_strdup(full);
  671. }
  672. }
  673. /* pass 2: if we're reading from a resetting source, clear all
  674. * mentioned config options, and maybe set to their defaults. */
  675. if (clear_first) {
  676. for (p = list; p; p = p->next)
  677. config_reset_line(fmt, options, p->key, use_defaults);
  678. }
  679. options_seen = bitarray_init_zero(n_options);
  680. /* pass 3: assign. */
  681. while (list) {
  682. int r;
  683. if ((r=config_assign_line(fmt, options, list, config_assign_flags,
  684. options_seen, msg))) {
  685. bitarray_free(options_seen);
  686. return r;
  687. }
  688. list = list->next;
  689. }
  690. bitarray_free(options_seen);
  691. /** Now we're done assigning a group of options to the configuration.
  692. * Subsequent group assignments should _replace_ linelists, not extend
  693. * them. */
  694. config_mark_lists_fragile(fmt, options);
  695. return 0;
  696. }
  697. /** Reset config option <b>var</b> to 0, 0.0, NULL, or the equivalent.
  698. * Called from config_reset() and config_free(). */
  699. static void
  700. config_clear(const config_format_t *fmt, void *options,
  701. const config_var_t *var)
  702. {
  703. void *lvalue = STRUCT_VAR_P(options, var->var_offset);
  704. (void)fmt; /* unused */
  705. switch (var->type) {
  706. case CONFIG_TYPE_STRING:
  707. case CONFIG_TYPE_FILENAME:
  708. tor_free(*(char**)lvalue);
  709. break;
  710. case CONFIG_TYPE_DOUBLE:
  711. *(double*)lvalue = 0.0;
  712. break;
  713. case CONFIG_TYPE_ISOTIME:
  714. *(time_t*)lvalue = 0;
  715. break;
  716. case CONFIG_TYPE_CSV_INTERVAL:
  717. case CONFIG_TYPE_INTERVAL:
  718. case CONFIG_TYPE_MSEC_INTERVAL:
  719. case CONFIG_TYPE_UINT:
  720. case CONFIG_TYPE_INT:
  721. case CONFIG_TYPE_PORT:
  722. case CONFIG_TYPE_BOOL:
  723. *(int*)lvalue = 0;
  724. break;
  725. case CONFIG_TYPE_AUTOBOOL:
  726. *(int*)lvalue = -1;
  727. break;
  728. case CONFIG_TYPE_UINT64:
  729. case CONFIG_TYPE_MEMUNIT:
  730. *(uint64_t*)lvalue = 0;
  731. break;
  732. case CONFIG_TYPE_ROUTERSET:
  733. if (*(routerset_t**)lvalue) {
  734. routerset_free(*(routerset_t**)lvalue);
  735. *(routerset_t**)lvalue = NULL;
  736. }
  737. break;
  738. case CONFIG_TYPE_CSV:
  739. if (*(smartlist_t**)lvalue) {
  740. SMARTLIST_FOREACH(*(smartlist_t **)lvalue, char *, cp, tor_free(cp));
  741. smartlist_free(*(smartlist_t **)lvalue);
  742. *(smartlist_t **)lvalue = NULL;
  743. }
  744. break;
  745. case CONFIG_TYPE_LINELIST:
  746. case CONFIG_TYPE_LINELIST_S:
  747. config_free_lines(*(config_line_t **)lvalue);
  748. *(config_line_t **)lvalue = NULL;
  749. break;
  750. case CONFIG_TYPE_LINELIST_V:
  751. /* handled by linelist_s. */
  752. break;
  753. case CONFIG_TYPE_OBSOLETE:
  754. break;
  755. }
  756. }
  757. /** Clear the option indexed by <b>var</b> in <b>options</b>. Then if
  758. * <b>use_defaults</b>, set it to its default value.
  759. * Called by config_init() and option_reset_line() and option_assign_line(). */
  760. static void
  761. config_reset(const config_format_t *fmt, void *options,
  762. const config_var_t *var, int use_defaults)
  763. {
  764. config_line_t *c;
  765. char *msg = NULL;
  766. CONFIG_CHECK(fmt, options);
  767. config_clear(fmt, options, var); /* clear it first */
  768. if (!use_defaults)
  769. return; /* all done */
  770. if (var->initvalue) {
  771. c = tor_malloc_zero(sizeof(config_line_t));
  772. c->key = tor_strdup(var->name);
  773. c->value = tor_strdup(var->initvalue);
  774. if (config_assign_value(fmt, options, c, &msg) < 0) {
  775. log_warn(LD_BUG, "Failed to assign default: %s", msg);
  776. tor_free(msg); /* if this happens it's a bug */
  777. }
  778. config_free_lines(c);
  779. }
  780. }
  781. /** Release storage held by <b>options</b>. */
  782. void
  783. config_free_(const config_format_t *fmt, void *options)
  784. {
  785. int i;
  786. if (!options)
  787. return;
  788. tor_assert(fmt);
  789. for (i=0; fmt->vars[i].name; ++i)
  790. config_clear(fmt, options, &(fmt->vars[i]));
  791. if (fmt->extra) {
  792. config_line_t **linep = STRUCT_VAR_P(options, fmt->extra->var_offset);
  793. config_free_lines(*linep);
  794. *linep = NULL;
  795. }
  796. tor_free(options);
  797. }
  798. /** Return true iff the option <b>name</b> has the same value in <b>o1</b>
  799. * and <b>o2</b>. Must not be called for LINELIST_S or OBSOLETE options.
  800. */
  801. int
  802. config_is_same(const config_format_t *fmt,
  803. const void *o1, const void *o2,
  804. const char *name)
  805. {
  806. config_line_t *c1, *c2;
  807. int r = 1;
  808. CONFIG_CHECK(fmt, o1);
  809. CONFIG_CHECK(fmt, o2);
  810. c1 = config_get_assigned_option(fmt, o1, name, 0);
  811. c2 = config_get_assigned_option(fmt, o2, name, 0);
  812. r = config_lines_eq(c1, c2);
  813. config_free_lines(c1);
  814. config_free_lines(c2);
  815. return r;
  816. }
  817. /** Copy storage held by <b>old</b> into a new or_options_t and return it. */
  818. void *
  819. config_dup(const config_format_t *fmt, const void *old)
  820. {
  821. void *newopts;
  822. int i;
  823. config_line_t *line;
  824. newopts = config_new(fmt);
  825. for (i=0; fmt->vars[i].name; ++i) {
  826. if (fmt->vars[i].type == CONFIG_TYPE_LINELIST_S)
  827. continue;
  828. if (fmt->vars[i].type == CONFIG_TYPE_OBSOLETE)
  829. continue;
  830. line = config_get_assigned_option(fmt, old, fmt->vars[i].name, 0);
  831. if (line) {
  832. char *msg = NULL;
  833. if (config_assign(fmt, newopts, line, 0, &msg) < 0) {
  834. log_err(LD_BUG, "config_get_assigned_option() generated "
  835. "something we couldn't config_assign(): %s", msg);
  836. tor_free(msg);
  837. tor_assert(0);
  838. }
  839. }
  840. config_free_lines(line);
  841. }
  842. return newopts;
  843. }
  844. /** Set all vars in the configuration object <b>options</b> to their default
  845. * values. */
  846. void
  847. config_init(const config_format_t *fmt, void *options)
  848. {
  849. int i;
  850. const config_var_t *var;
  851. CONFIG_CHECK(fmt, options);
  852. for (i=0; fmt->vars[i].name; ++i) {
  853. var = &fmt->vars[i];
  854. if (!var->initvalue)
  855. continue; /* defaults to NULL or 0 */
  856. config_reset(fmt, options, var, 1);
  857. }
  858. }
  859. /** Allocate and return a new string holding the written-out values of the vars
  860. * in 'options'. If 'minimal', do not write out any default-valued vars.
  861. * Else, if comment_defaults, write default values as comments.
  862. */
  863. char *
  864. config_dump(const config_format_t *fmt, const void *default_options,
  865. const void *options, int minimal,
  866. int comment_defaults)
  867. {
  868. smartlist_t *elements;
  869. const void *defaults = default_options;
  870. void *defaults_tmp = NULL;
  871. config_line_t *line, *assigned;
  872. char *result;
  873. int i;
  874. char *msg = NULL;
  875. if (defaults == NULL) {
  876. defaults = defaults_tmp = config_new(fmt);
  877. config_init(fmt, defaults_tmp);
  878. }
  879. /* XXX use a 1 here so we don't add a new log line while dumping */
  880. if (default_options == NULL) {
  881. if (fmt->validate_fn(NULL, defaults_tmp, defaults_tmp, 1, &msg) < 0) {
  882. log_err(LD_BUG, "Failed to validate default config: %s", msg);
  883. tor_free(msg);
  884. tor_assert(0);
  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. }