confparse.c 34 KB

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