confparse.c 35 KB

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