confparse.c 34 KB

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