confparse.c 40 KB

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