confparse.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364
  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. smartlist_add_asprintf(elements, "%s%s %s\n",
  1056. comment_option ? "# " : "",
  1057. line->key, line->value);
  1058. }
  1059. config_free_lines(assigned);
  1060. }
  1061. if (fmt->extra) {
  1062. line = *(config_line_t**)STRUCT_VAR_P(options, fmt->extra->var_offset);
  1063. for (; line; line = line->next) {
  1064. smartlist_add_asprintf(elements, "%s %s\n", line->key, line->value);
  1065. }
  1066. }
  1067. result = smartlist_join_strings(elements, "", 0, NULL);
  1068. SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
  1069. smartlist_free(elements);
  1070. if (defaults_tmp)
  1071. config_free(fmt, defaults_tmp);
  1072. return result;
  1073. }
  1074. /** Mapping from a unit name to a multiplier for converting that unit into a
  1075. * base unit. Used by config_parse_unit. */
  1076. struct unit_table_t {
  1077. const char *unit; /**< The name of the unit */
  1078. uint64_t multiplier; /**< How many of the base unit appear in this unit */
  1079. };
  1080. /** Table to map the names of memory units to the number of bytes they
  1081. * contain. */
  1082. static struct unit_table_t memory_units[] = {
  1083. { "", 1 },
  1084. { "b", 1<< 0 },
  1085. { "byte", 1<< 0 },
  1086. { "bytes", 1<< 0 },
  1087. { "kb", 1<<10 },
  1088. { "kbyte", 1<<10 },
  1089. { "kbytes", 1<<10 },
  1090. { "kilobyte", 1<<10 },
  1091. { "kilobytes", 1<<10 },
  1092. { "kilobits", 1<<7 },
  1093. { "kilobit", 1<<7 },
  1094. { "kbits", 1<<7 },
  1095. { "kbit", 1<<7 },
  1096. { "m", 1<<20 },
  1097. { "mb", 1<<20 },
  1098. { "mbyte", 1<<20 },
  1099. { "mbytes", 1<<20 },
  1100. { "megabyte", 1<<20 },
  1101. { "megabytes", 1<<20 },
  1102. { "megabits", 1<<17 },
  1103. { "megabit", 1<<17 },
  1104. { "mbits", 1<<17 },
  1105. { "mbit", 1<<17 },
  1106. { "gb", 1<<30 },
  1107. { "gbyte", 1<<30 },
  1108. { "gbytes", 1<<30 },
  1109. { "gigabyte", 1<<30 },
  1110. { "gigabytes", 1<<30 },
  1111. { "gigabits", 1<<27 },
  1112. { "gigabit", 1<<27 },
  1113. { "gbits", 1<<27 },
  1114. { "gbit", 1<<27 },
  1115. { "tb", U64_LITERAL(1)<<40 },
  1116. { "terabyte", U64_LITERAL(1)<<40 },
  1117. { "terabytes", U64_LITERAL(1)<<40 },
  1118. { "terabits", U64_LITERAL(1)<<37 },
  1119. { "terabit", U64_LITERAL(1)<<37 },
  1120. { "tbits", U64_LITERAL(1)<<37 },
  1121. { "tbit", U64_LITERAL(1)<<37 },
  1122. { NULL, 0 },
  1123. };
  1124. /** Table to map the names of time units to the number of seconds they
  1125. * contain. */
  1126. static struct unit_table_t time_units[] = {
  1127. { "", 1 },
  1128. { "second", 1 },
  1129. { "seconds", 1 },
  1130. { "minute", 60 },
  1131. { "minutes", 60 },
  1132. { "hour", 60*60 },
  1133. { "hours", 60*60 },
  1134. { "day", 24*60*60 },
  1135. { "days", 24*60*60 },
  1136. { "week", 7*24*60*60 },
  1137. { "weeks", 7*24*60*60 },
  1138. { "month", 2629728, }, /* about 30.437 days */
  1139. { "months", 2629728, },
  1140. { NULL, 0 },
  1141. };
  1142. /** Table to map the names of time units to the number of milliseconds
  1143. * they contain. */
  1144. static struct unit_table_t time_msec_units[] = {
  1145. { "", 1 },
  1146. { "msec", 1 },
  1147. { "millisecond", 1 },
  1148. { "milliseconds", 1 },
  1149. { "second", 1000 },
  1150. { "seconds", 1000 },
  1151. { "minute", 60*1000 },
  1152. { "minutes", 60*1000 },
  1153. { "hour", 60*60*1000 },
  1154. { "hours", 60*60*1000 },
  1155. { "day", 24*60*60*1000 },
  1156. { "days", 24*60*60*1000 },
  1157. { "week", 7*24*60*60*1000 },
  1158. { "weeks", 7*24*60*60*1000 },
  1159. { NULL, 0 },
  1160. };
  1161. /** Parse a string <b>val</b> containing a number, zero or more
  1162. * spaces, and an optional unit string. If the unit appears in the
  1163. * table <b>u</b>, then multiply the number by the unit multiplier.
  1164. * On success, set *<b>ok</b> to 1 and return this product.
  1165. * Otherwise, set *<b>ok</b> to 0.
  1166. */
  1167. static uint64_t
  1168. config_parse_units(const char *val, struct unit_table_t *u, int *ok)
  1169. {
  1170. uint64_t v = 0;
  1171. double d = 0;
  1172. int use_float = 0;
  1173. char *cp;
  1174. tor_assert(ok);
  1175. v = tor_parse_uint64(val, 10, 0, UINT64_MAX, ok, &cp);
  1176. if (!*ok || (cp && *cp == '.')) {
  1177. d = tor_parse_double(val, 0, (double)UINT64_MAX, ok, &cp);
  1178. if (!*ok)
  1179. goto done;
  1180. use_float = 1;
  1181. }
  1182. if (!cp) {
  1183. *ok = 1;
  1184. v = use_float ? DBL_TO_U64(d) : v;
  1185. goto done;
  1186. }
  1187. cp = (char*) eat_whitespace(cp);
  1188. for ( ;u->unit;++u) {
  1189. if (!strcasecmp(u->unit, cp)) {
  1190. if (use_float)
  1191. v = (uint64_t)(u->multiplier * d);
  1192. else
  1193. v *= u->multiplier;
  1194. *ok = 1;
  1195. goto done;
  1196. }
  1197. }
  1198. log_warn(LD_CONFIG, "Unknown unit '%s'.", cp);
  1199. *ok = 0;
  1200. done:
  1201. if (*ok)
  1202. return v;
  1203. else
  1204. return 0;
  1205. }
  1206. /** Parse a string in the format "number unit", where unit is a unit of
  1207. * information (byte, KB, M, etc). On success, set *<b>ok</b> to true
  1208. * and return the number of bytes specified. Otherwise, set
  1209. * *<b>ok</b> to false and return 0. */
  1210. static uint64_t
  1211. config_parse_memunit(const char *s, int *ok)
  1212. {
  1213. uint64_t u = config_parse_units(s, memory_units, ok);
  1214. return u;
  1215. }
  1216. /** Parse a string in the format "number unit", where unit is a unit of
  1217. * time in milliseconds. On success, set *<b>ok</b> to true and return
  1218. * the number of milliseconds in the provided interval. Otherwise, set
  1219. * *<b>ok</b> to 0 and return -1. */
  1220. static int
  1221. config_parse_msec_interval(const char *s, int *ok)
  1222. {
  1223. uint64_t r;
  1224. r = config_parse_units(s, time_msec_units, ok);
  1225. if (!ok)
  1226. return -1;
  1227. if (r > INT_MAX) {
  1228. log_warn(LD_CONFIG, "Msec interval '%s' is too long", s);
  1229. *ok = 0;
  1230. return -1;
  1231. }
  1232. return (int)r;
  1233. }
  1234. /** Parse a string in the format "number unit", where unit is a unit of time.
  1235. * On success, set *<b>ok</b> to true and return the number of seconds in
  1236. * the provided interval. Otherwise, set *<b>ok</b> to 0 and return -1.
  1237. */
  1238. static int
  1239. config_parse_interval(const char *s, int *ok)
  1240. {
  1241. uint64_t r;
  1242. r = config_parse_units(s, time_units, ok);
  1243. if (!ok)
  1244. return -1;
  1245. if (r > INT_MAX) {
  1246. log_warn(LD_CONFIG, "Interval '%s' is too long", s);
  1247. *ok = 0;
  1248. return -1;
  1249. }
  1250. return (int)r;
  1251. }