confparse.c 35 KB

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