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