confparse.c 34 KB

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