confparse.c 34 KB

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