confparse.c 35 KB

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