confparse.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396
  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);
  716. result = config_lines_dup_and_filter(*(const config_line_t **)value,
  717. key);
  718. break;
  719. case CONFIG_TYPE_LINELIST:
  720. case CONFIG_TYPE_LINELIST_V:
  721. tor_free(result->key);
  722. tor_free(result);
  723. result = config_lines_dup(*(const config_line_t**)value);
  724. break;
  725. default:
  726. tor_free(result->key);
  727. tor_free(result);
  728. log_warn(LD_BUG,"Unknown type %d for known key '%s'",
  729. var->type, key);
  730. return NULL;
  731. }
  732. if (escape_val) {
  733. config_line_t *line;
  734. for (line = result; line; line = line->next) {
  735. if (line->value && config_value_needs_escape(line->value)) {
  736. char *newval = esc_for_log(line->value);
  737. tor_free(line->value);
  738. line->value = newval;
  739. }
  740. }
  741. }
  742. return result;
  743. }
  744. /** Iterate through the linked list of requested options <b>list</b>.
  745. * For each item, convert as appropriate and assign to <b>options</b>.
  746. * If an item is unrecognized, set *msg and return -1 immediately,
  747. * else return 0 for success.
  748. *
  749. * If <b>clear_first</b>, interpret config options as replacing (not
  750. * extending) their previous values. If <b>clear_first</b> is set,
  751. * then <b>use_defaults</b> to decide if you set to defaults after
  752. * clearing, or make the value 0 or NULL.
  753. *
  754. * Here are the use cases:
  755. * 1. A non-empty AllowInvalid line in your torrc. Appends to current
  756. * if linelist, replaces current if csv.
  757. * 2. An empty AllowInvalid line in your torrc. Should clear it.
  758. * 3. "RESETCONF AllowInvalid" sets it to default.
  759. * 4. "SETCONF AllowInvalid" makes it NULL.
  760. * 5. "SETCONF AllowInvalid=foo" clears it and sets it to "foo".
  761. *
  762. * Use_defaults Clear_first
  763. * 0 0 "append"
  764. * 1 0 undefined, don't use
  765. * 0 1 "set to null first"
  766. * 1 1 "set to defaults first"
  767. * Return 0 on success, -1 on bad key, -2 on bad value.
  768. *
  769. * As an additional special case, if a LINELIST config option has
  770. * no value and clear_first is 0, then warn and ignore it.
  771. */
  772. /*
  773. There are three call cases for config_assign() currently.
  774. Case one: Torrc entry
  775. options_init_from_torrc() calls config_assign(0, 0)
  776. calls config_assign_line(0, 0).
  777. if value is empty, calls config_reset(0) and returns.
  778. calls config_assign_value(), appends.
  779. Case two: setconf
  780. options_trial_assign() calls config_assign(0, 1)
  781. calls config_reset_line(0)
  782. calls config_reset(0)
  783. calls option_clear().
  784. calls config_assign_line(0, 1).
  785. if value is empty, returns.
  786. calls config_assign_value(), appends.
  787. Case three: resetconf
  788. options_trial_assign() calls config_assign(1, 1)
  789. calls config_reset_line(1)
  790. calls config_reset(1)
  791. calls option_clear().
  792. calls config_assign_value(default)
  793. calls config_assign_line(1, 1).
  794. returns.
  795. */
  796. int
  797. config_assign(const config_format_t *fmt, void *options, config_line_t *list,
  798. unsigned config_assign_flags, char **msg)
  799. {
  800. config_line_t *p;
  801. bitarray_t *options_seen;
  802. const int n_options = config_count_options(fmt);
  803. const unsigned clear_first = config_assign_flags & CAL_CLEAR_FIRST;
  804. const unsigned use_defaults = config_assign_flags & CAL_USE_DEFAULTS;
  805. CONFIG_CHECK(fmt, options);
  806. /* pass 1: normalize keys */
  807. for (p = list; p; p = p->next) {
  808. const char *full = config_expand_abbrev(fmt, p->key, 0, 1);
  809. if (strcmp(full,p->key)) {
  810. tor_free(p->key);
  811. p->key = tor_strdup(full);
  812. }
  813. }
  814. /* pass 2: if we're reading from a resetting source, clear all
  815. * mentioned config options, and maybe set to their defaults. */
  816. if (clear_first) {
  817. for (p = list; p; p = p->next)
  818. config_reset_line(fmt, options, p->key, use_defaults);
  819. }
  820. options_seen = bitarray_init_zero(n_options);
  821. /* pass 3: assign. */
  822. while (list) {
  823. int r;
  824. if ((r=config_assign_line(fmt, options, list, config_assign_flags,
  825. options_seen, msg))) {
  826. bitarray_free(options_seen);
  827. return r;
  828. }
  829. list = list->next;
  830. }
  831. bitarray_free(options_seen);
  832. /** Now we're done assigning a group of options to the configuration.
  833. * Subsequent group assignments should _replace_ linelists, not extend
  834. * them. */
  835. config_mark_lists_fragile(fmt, options);
  836. return 0;
  837. }
  838. /** Reset config option <b>var</b> to 0, 0.0, NULL, or the equivalent.
  839. * Called from config_reset() and config_free(). */
  840. static void
  841. config_clear(const config_format_t *fmt, void *options,
  842. const config_var_t *var)
  843. {
  844. void *lvalue = STRUCT_VAR_P(options, var->var_offset);
  845. (void)fmt; /* unused */
  846. switch (var->type) {
  847. case CONFIG_TYPE_STRING:
  848. case CONFIG_TYPE_FILENAME:
  849. tor_free(*(char**)lvalue);
  850. break;
  851. case CONFIG_TYPE_DOUBLE:
  852. *(double*)lvalue = 0.0;
  853. break;
  854. case CONFIG_TYPE_ISOTIME:
  855. *(time_t*)lvalue = 0;
  856. break;
  857. case CONFIG_TYPE_INTERVAL:
  858. case CONFIG_TYPE_MSEC_INTERVAL:
  859. case CONFIG_TYPE_UINT:
  860. case CONFIG_TYPE_INT:
  861. case CONFIG_TYPE_PORT:
  862. case CONFIG_TYPE_BOOL:
  863. *(int*)lvalue = 0;
  864. break;
  865. case CONFIG_TYPE_AUTOBOOL:
  866. *(int*)lvalue = -1;
  867. break;
  868. case CONFIG_TYPE_MEMUNIT:
  869. *(uint64_t*)lvalue = 0;
  870. break;
  871. case CONFIG_TYPE_ROUTERSET:
  872. if (*(routerset_t**)lvalue) {
  873. routerset_free(*(routerset_t**)lvalue);
  874. *(routerset_t**)lvalue = NULL;
  875. }
  876. break;
  877. case CONFIG_TYPE_CSV:
  878. if (*(smartlist_t**)lvalue) {
  879. SMARTLIST_FOREACH(*(smartlist_t **)lvalue, char *, cp, tor_free(cp));
  880. smartlist_free(*(smartlist_t **)lvalue);
  881. *(smartlist_t **)lvalue = NULL;
  882. }
  883. break;
  884. case CONFIG_TYPE_CSV_INTERVAL:
  885. if (*(smartlist_t**)lvalue) {
  886. SMARTLIST_FOREACH(*(smartlist_t **)lvalue, int *, cp, tor_free(cp));
  887. smartlist_free(*(smartlist_t **)lvalue);
  888. *(smartlist_t **)lvalue = NULL;
  889. }
  890. break;
  891. case CONFIG_TYPE_LINELIST:
  892. case CONFIG_TYPE_LINELIST_S:
  893. config_free_lines(*(config_line_t **)lvalue);
  894. *(config_line_t **)lvalue = NULL;
  895. break;
  896. case CONFIG_TYPE_LINELIST_V:
  897. /* handled by linelist_s. */
  898. break;
  899. case CONFIG_TYPE_OBSOLETE:
  900. break;
  901. }
  902. }
  903. /** Clear the option indexed by <b>var</b> in <b>options</b>. Then if
  904. * <b>use_defaults</b>, set it to its default value.
  905. * Called by config_init() and option_reset_line() and option_assign_line(). */
  906. static void
  907. config_reset(const config_format_t *fmt, void *options,
  908. const config_var_t *var, int use_defaults)
  909. {
  910. config_line_t *c;
  911. char *msg = NULL;
  912. CONFIG_CHECK(fmt, options);
  913. config_clear(fmt, options, var); /* clear it first */
  914. if (!use_defaults)
  915. return; /* all done */
  916. if (var->initvalue) {
  917. c = tor_malloc_zero(sizeof(config_line_t));
  918. c->key = tor_strdup(var->name);
  919. c->value = tor_strdup(var->initvalue);
  920. if (config_assign_value(fmt, options, c, &msg) < 0) {
  921. log_warn(LD_BUG, "Failed to assign default: %s", msg);
  922. tor_free(msg); /* if this happens it's a bug */
  923. }
  924. config_free_lines(c);
  925. }
  926. }
  927. /** Release storage held by <b>options</b>. */
  928. void
  929. config_free(const config_format_t *fmt, void *options)
  930. {
  931. int i;
  932. if (!options)
  933. return;
  934. tor_assert(fmt);
  935. for (i=0; fmt->vars[i].name; ++i)
  936. config_clear(fmt, options, &(fmt->vars[i]));
  937. if (fmt->extra) {
  938. config_line_t **linep = STRUCT_VAR_P(options, fmt->extra->var_offset);
  939. config_free_lines(*linep);
  940. *linep = NULL;
  941. }
  942. tor_free(options);
  943. }
  944. /** Return true iff a and b contain identical keys and values in identical
  945. * order. */
  946. int
  947. config_lines_eq(config_line_t *a, config_line_t *b)
  948. {
  949. while (a && b) {
  950. if (strcasecmp(a->key, b->key) || strcmp(a->value, b->value))
  951. return 0;
  952. a = a->next;
  953. b = b->next;
  954. }
  955. if (a || b)
  956. return 0;
  957. return 1;
  958. }
  959. /** Return the number of lines in <b>a</b> whose key is <b>key</b>. */
  960. int
  961. config_count_key(const config_line_t *a, const char *key)
  962. {
  963. int n = 0;
  964. while (a) {
  965. if (!strcasecmp(a->key, key)) {
  966. ++n;
  967. }
  968. a = a->next;
  969. }
  970. return n;
  971. }
  972. /** Return true iff the option <b>name</b> has the same value in <b>o1</b>
  973. * and <b>o2</b>. Must not be called for LINELIST_S or OBSOLETE options.
  974. */
  975. int
  976. config_is_same(const config_format_t *fmt,
  977. const void *o1, const void *o2,
  978. const char *name)
  979. {
  980. config_line_t *c1, *c2;
  981. int r = 1;
  982. CONFIG_CHECK(fmt, o1);
  983. CONFIG_CHECK(fmt, o2);
  984. c1 = config_get_assigned_option(fmt, o1, name, 0);
  985. c2 = config_get_assigned_option(fmt, o2, name, 0);
  986. r = config_lines_eq(c1, c2);
  987. config_free_lines(c1);
  988. config_free_lines(c2);
  989. return r;
  990. }
  991. /** Copy storage held by <b>old</b> into a new or_options_t and return it. */
  992. void *
  993. config_dup(const config_format_t *fmt, const void *old)
  994. {
  995. void *newopts;
  996. int i;
  997. config_line_t *line;
  998. newopts = config_new(fmt);
  999. for (i=0; fmt->vars[i].name; ++i) {
  1000. if (fmt->vars[i].type == CONFIG_TYPE_LINELIST_S)
  1001. continue;
  1002. if (fmt->vars[i].type == CONFIG_TYPE_OBSOLETE)
  1003. continue;
  1004. line = config_get_assigned_option(fmt, old, fmt->vars[i].name, 0);
  1005. if (line) {
  1006. char *msg = NULL;
  1007. if (config_assign(fmt, newopts, line, 0, &msg) < 0) {
  1008. log_err(LD_BUG, "config_get_assigned_option() generated "
  1009. "something we couldn't config_assign(): %s", msg);
  1010. tor_free(msg);
  1011. tor_assert(0);
  1012. }
  1013. }
  1014. config_free_lines(line);
  1015. }
  1016. return newopts;
  1017. }
  1018. /** Set all vars in the configuration object <b>options</b> to their default
  1019. * values. */
  1020. void
  1021. config_init(const config_format_t *fmt, void *options)
  1022. {
  1023. int i;
  1024. const config_var_t *var;
  1025. CONFIG_CHECK(fmt, options);
  1026. for (i=0; fmt->vars[i].name; ++i) {
  1027. var = &fmt->vars[i];
  1028. if (!var->initvalue)
  1029. continue; /* defaults to NULL or 0 */
  1030. config_reset(fmt, options, var, 1);
  1031. }
  1032. }
  1033. /** Allocate and return a new string holding the written-out values of the vars
  1034. * in 'options'. If 'minimal', do not write out any default-valued vars.
  1035. * Else, if comment_defaults, write default values as comments.
  1036. */
  1037. char *
  1038. config_dump(const config_format_t *fmt, const void *default_options,
  1039. const void *options, int minimal,
  1040. int comment_defaults)
  1041. {
  1042. smartlist_t *elements;
  1043. const void *defaults = default_options;
  1044. void *defaults_tmp = NULL;
  1045. config_line_t *line, *assigned;
  1046. char *result;
  1047. int i;
  1048. char *msg = NULL;
  1049. if (defaults == NULL) {
  1050. defaults = defaults_tmp = config_new(fmt);
  1051. config_init(fmt, defaults_tmp);
  1052. }
  1053. /* XXX use a 1 here so we don't add a new log line while dumping */
  1054. if (default_options == NULL) {
  1055. if (fmt->validate_fn(NULL, defaults_tmp, defaults_tmp, 1, &msg) < 0) {
  1056. log_err(LD_BUG, "Failed to validate default config: %s", msg);
  1057. tor_free(msg);
  1058. tor_assert(0);
  1059. }
  1060. }
  1061. elements = smartlist_new();
  1062. for (i=0; fmt->vars[i].name; ++i) {
  1063. int comment_option = 0;
  1064. if (fmt->vars[i].type == CONFIG_TYPE_OBSOLETE ||
  1065. fmt->vars[i].type == CONFIG_TYPE_LINELIST_S)
  1066. continue;
  1067. /* Don't save 'hidden' control variables. */
  1068. if (!strcmpstart(fmt->vars[i].name, "__"))
  1069. continue;
  1070. if (minimal && config_is_same(fmt, options, defaults, fmt->vars[i].name))
  1071. continue;
  1072. else if (comment_defaults &&
  1073. config_is_same(fmt, options, defaults, fmt->vars[i].name))
  1074. comment_option = 1;
  1075. line = assigned =
  1076. config_get_assigned_option(fmt, options, fmt->vars[i].name, 1);
  1077. for (; line; line = line->next) {
  1078. if (!strcmpstart(line->key, "__")) {
  1079. /* This check detects "hidden" variables inside LINELIST_V structures.
  1080. */
  1081. continue;
  1082. }
  1083. smartlist_add_asprintf(elements, "%s%s %s\n",
  1084. comment_option ? "# " : "",
  1085. line->key, line->value);
  1086. }
  1087. config_free_lines(assigned);
  1088. }
  1089. if (fmt->extra) {
  1090. line = *(config_line_t**)STRUCT_VAR_P(options, fmt->extra->var_offset);
  1091. for (; line; line = line->next) {
  1092. smartlist_add_asprintf(elements, "%s %s\n", line->key, line->value);
  1093. }
  1094. }
  1095. result = smartlist_join_strings(elements, "", 0, NULL);
  1096. SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
  1097. smartlist_free(elements);
  1098. if (defaults_tmp)
  1099. config_free(fmt, defaults_tmp);
  1100. return result;
  1101. }
  1102. /** Mapping from a unit name to a multiplier for converting that unit into a
  1103. * base unit. Used by config_parse_unit. */
  1104. struct unit_table_t {
  1105. const char *unit; /**< The name of the unit */
  1106. uint64_t multiplier; /**< How many of the base unit appear in this unit */
  1107. };
  1108. /** Table to map the names of memory units to the number of bytes they
  1109. * contain. */
  1110. static struct unit_table_t memory_units[] = {
  1111. { "", 1 },
  1112. { "b", 1<< 0 },
  1113. { "byte", 1<< 0 },
  1114. { "bytes", 1<< 0 },
  1115. { "kb", 1<<10 },
  1116. { "kbyte", 1<<10 },
  1117. { "kbytes", 1<<10 },
  1118. { "kilobyte", 1<<10 },
  1119. { "kilobytes", 1<<10 },
  1120. { "kilobits", 1<<7 },
  1121. { "kilobit", 1<<7 },
  1122. { "kbits", 1<<7 },
  1123. { "kbit", 1<<7 },
  1124. { "m", 1<<20 },
  1125. { "mb", 1<<20 },
  1126. { "mbyte", 1<<20 },
  1127. { "mbytes", 1<<20 },
  1128. { "megabyte", 1<<20 },
  1129. { "megabytes", 1<<20 },
  1130. { "megabits", 1<<17 },
  1131. { "megabit", 1<<17 },
  1132. { "mbits", 1<<17 },
  1133. { "mbit", 1<<17 },
  1134. { "gb", 1<<30 },
  1135. { "gbyte", 1<<30 },
  1136. { "gbytes", 1<<30 },
  1137. { "gigabyte", 1<<30 },
  1138. { "gigabytes", 1<<30 },
  1139. { "gigabits", 1<<27 },
  1140. { "gigabit", 1<<27 },
  1141. { "gbits", 1<<27 },
  1142. { "gbit", 1<<27 },
  1143. { "tb", U64_LITERAL(1)<<40 },
  1144. { "tbyte", U64_LITERAL(1)<<40 },
  1145. { "tbytes", U64_LITERAL(1)<<40 },
  1146. { "terabyte", U64_LITERAL(1)<<40 },
  1147. { "terabytes", U64_LITERAL(1)<<40 },
  1148. { "terabits", U64_LITERAL(1)<<37 },
  1149. { "terabit", U64_LITERAL(1)<<37 },
  1150. { "tbits", U64_LITERAL(1)<<37 },
  1151. { "tbit", U64_LITERAL(1)<<37 },
  1152. { NULL, 0 },
  1153. };
  1154. /** Table to map the names of time units to the number of seconds they
  1155. * contain. */
  1156. static struct unit_table_t time_units[] = {
  1157. { "", 1 },
  1158. { "second", 1 },
  1159. { "seconds", 1 },
  1160. { "minute", 60 },
  1161. { "minutes", 60 },
  1162. { "hour", 60*60 },
  1163. { "hours", 60*60 },
  1164. { "day", 24*60*60 },
  1165. { "days", 24*60*60 },
  1166. { "week", 7*24*60*60 },
  1167. { "weeks", 7*24*60*60 },
  1168. { "month", 2629728, }, /* about 30.437 days */
  1169. { "months", 2629728, },
  1170. { NULL, 0 },
  1171. };
  1172. /** Table to map the names of time units to the number of milliseconds
  1173. * they contain. */
  1174. static struct unit_table_t time_msec_units[] = {
  1175. { "", 1 },
  1176. { "msec", 1 },
  1177. { "millisecond", 1 },
  1178. { "milliseconds", 1 },
  1179. { "second", 1000 },
  1180. { "seconds", 1000 },
  1181. { "minute", 60*1000 },
  1182. { "minutes", 60*1000 },
  1183. { "hour", 60*60*1000 },
  1184. { "hours", 60*60*1000 },
  1185. { "day", 24*60*60*1000 },
  1186. { "days", 24*60*60*1000 },
  1187. { "week", 7*24*60*60*1000 },
  1188. { "weeks", 7*24*60*60*1000 },
  1189. { NULL, 0 },
  1190. };
  1191. /** Parse a string <b>val</b> containing a number, zero or more
  1192. * spaces, and an optional unit string. If the unit appears in the
  1193. * table <b>u</b>, then multiply the number by the unit multiplier.
  1194. * On success, set *<b>ok</b> to 1 and return this product.
  1195. * Otherwise, set *<b>ok</b> to 0.
  1196. */
  1197. static uint64_t
  1198. config_parse_units(const char *val, struct unit_table_t *u, int *ok)
  1199. {
  1200. uint64_t v = 0;
  1201. double d = 0;
  1202. int use_float = 0;
  1203. char *cp;
  1204. tor_assert(ok);
  1205. v = tor_parse_uint64(val, 10, 0, UINT64_MAX, ok, &cp);
  1206. if (!*ok || (cp && *cp == '.')) {
  1207. d = tor_parse_double(val, 0, (double)UINT64_MAX, ok, &cp);
  1208. if (!*ok)
  1209. goto done;
  1210. use_float = 1;
  1211. }
  1212. if (!cp) {
  1213. *ok = 1;
  1214. v = use_float ? DBL_TO_U64(d) : v;
  1215. goto done;
  1216. }
  1217. cp = (char*) eat_whitespace(cp);
  1218. for ( ;u->unit;++u) {
  1219. if (!strcasecmp(u->unit, cp)) {
  1220. if (use_float)
  1221. v = (uint64_t)(u->multiplier * d);
  1222. else
  1223. v *= u->multiplier;
  1224. *ok = 1;
  1225. goto done;
  1226. }
  1227. }
  1228. log_warn(LD_CONFIG, "Unknown unit '%s'.", cp);
  1229. *ok = 0;
  1230. done:
  1231. if (*ok)
  1232. return v;
  1233. else
  1234. return 0;
  1235. }
  1236. /** Parse a string in the format "number unit", where unit is a unit of
  1237. * information (byte, KB, M, etc). On success, set *<b>ok</b> to true
  1238. * and return the number of bytes specified. Otherwise, set
  1239. * *<b>ok</b> to false and return 0. */
  1240. static uint64_t
  1241. config_parse_memunit(const char *s, int *ok)
  1242. {
  1243. uint64_t u = config_parse_units(s, memory_units, ok);
  1244. return u;
  1245. }
  1246. /** Parse a string in the format "number unit", where unit is a unit of
  1247. * time in milliseconds. On success, set *<b>ok</b> to true and return
  1248. * the number of milliseconds in the provided interval. Otherwise, set
  1249. * *<b>ok</b> to 0 and return -1. */
  1250. static int
  1251. config_parse_msec_interval(const char *s, int *ok)
  1252. {
  1253. uint64_t r;
  1254. r = config_parse_units(s, time_msec_units, ok);
  1255. if (!ok)
  1256. return -1;
  1257. if (r > INT_MAX) {
  1258. log_warn(LD_CONFIG, "Msec interval '%s' is too long", s);
  1259. *ok = 0;
  1260. return -1;
  1261. }
  1262. return (int)r;
  1263. }
  1264. /** Parse a string in the format "number unit", where unit is a unit of time.
  1265. * On success, set *<b>ok</b> to true and return the number of seconds in
  1266. * the provided interval. Otherwise, set *<b>ok</b> to 0 and return -1.
  1267. */
  1268. static int
  1269. config_parse_interval(const char *s, int *ok)
  1270. {
  1271. uint64_t r;
  1272. r = config_parse_units(s, time_units, ok);
  1273. if (!ok)
  1274. return -1;
  1275. if (r > INT_MAX) {
  1276. log_warn(LD_CONFIG, "Interval '%s' is too long", s);
  1277. *ok = 0;
  1278. return -1;
  1279. }
  1280. return (int)r;
  1281. }