confparse.c 39 KB

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