type_defs.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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-2019, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * @file type_defs.c
  8. * @brief Definitions for various low-level configuration types.
  9. *
  10. * This module creates a number of var_type_def_t objects, to be used by
  11. * typedvar.c in manipulating variables.
  12. *
  13. * The types here are common types that can be implemented with Tor's
  14. * low-level functionality. To define new types, see var_type_def_st.h.
  15. **/
  16. #include "orconfig.h"
  17. #include "lib/conf/conftypes.h"
  18. #include "lib/confmgt/typedvar.h"
  19. #include "lib/confmgt/type_defs.h"
  20. #include "lib/confmgt/unitparse.h"
  21. #include "lib/cc/compat_compiler.h"
  22. #include "lib/conf/conftypes.h"
  23. #include "lib/container/smartlist.h"
  24. #include "lib/encoding/confline.h"
  25. #include "lib/encoding/time_fmt.h"
  26. #include "lib/log/escape.h"
  27. #include "lib/log/log.h"
  28. #include "lib/log/util_bug.h"
  29. #include "lib/malloc/malloc.h"
  30. #include "lib/string/parse_int.h"
  31. #include "lib/string/printf.h"
  32. #include "lib/confmgt/var_type_def_st.h"
  33. #include <stddef.h>
  34. #include <string.h>
  35. //////
  36. // CONFIG_TYPE_STRING
  37. // CONFIG_TYPE_FILENAME
  38. //
  39. // These two types are the same for now, but they have different names.
  40. //////
  41. static int
  42. string_parse(void *target, const char *value, char **errmsg,
  43. const void *params)
  44. {
  45. (void)params;
  46. (void)errmsg;
  47. char **p = (char**)target;
  48. *p = tor_strdup(value);
  49. return 0;
  50. }
  51. static char *
  52. string_encode(const void *value, const void *params)
  53. {
  54. (void)params;
  55. const char **p = (const char**)value;
  56. return *p ? tor_strdup(*p) : NULL;
  57. }
  58. static void
  59. string_clear(void *value, const void *params)
  60. {
  61. (void)params;
  62. char **p = (char**)value;
  63. tor_free(*p); // sets *p to NULL.
  64. }
  65. static const var_type_fns_t string_fns = {
  66. .parse = string_parse,
  67. .encode = string_encode,
  68. .clear = string_clear,
  69. };
  70. /////
  71. // CONFIG_TYPE_INT
  72. // CONFIG_TYPE_POSINT
  73. //
  74. // These types are implemented as int, possibly with a restricted range.
  75. /////
  76. typedef struct int_type_params_t {
  77. int minval;
  78. int maxval;
  79. } int_parse_params_t;
  80. static const int_parse_params_t INT_PARSE_UNRESTRICTED = {
  81. .minval = INT_MIN,
  82. .maxval = INT_MAX,
  83. };
  84. static const int_parse_params_t INT_PARSE_POSINT = {
  85. .minval = 0,
  86. .maxval = INT_MAX,
  87. };
  88. static int
  89. int_parse(void *target, const char *value, char **errmsg, const void *params)
  90. {
  91. const int_parse_params_t *pp;
  92. if (params) {
  93. pp = params;
  94. } else {
  95. pp = &INT_PARSE_UNRESTRICTED;
  96. }
  97. int *p = target;
  98. int ok=0;
  99. *p = (int)tor_parse_long(value, 10, pp->minval, pp->maxval, &ok, NULL);
  100. if (!ok) {
  101. tor_asprintf(errmsg, "Integer %s is malformed or out of bounds.",
  102. value);
  103. return -1;
  104. }
  105. return 0;
  106. }
  107. static char *
  108. int_encode(const void *value, const void *params)
  109. {
  110. (void)params;
  111. int v = *(int*)value;
  112. char *result;
  113. tor_asprintf(&result, "%d", v);
  114. return result;
  115. }
  116. static void
  117. int_clear(void *value, const void *params)
  118. {
  119. (void)params;
  120. *(int*)value = 0;
  121. }
  122. static bool
  123. int_ok(const void *value, const void *params)
  124. {
  125. const int_parse_params_t *pp = params;
  126. if (pp) {
  127. int v = *(int*)value;
  128. return pp->minval <= v && v <= pp->maxval;
  129. } else {
  130. return true;
  131. }
  132. }
  133. static const var_type_fns_t int_fns = {
  134. .parse = int_parse,
  135. .encode = int_encode,
  136. .clear = int_clear,
  137. .ok = int_ok,
  138. };
  139. /////
  140. // CONFIG_TYPE_UINT64
  141. //
  142. // This type is an unrestricted u64.
  143. /////
  144. static int
  145. uint64_parse(void *target, const char *value, char **errmsg,
  146. const void *params)
  147. {
  148. (void)params;
  149. (void)errmsg;
  150. uint64_t *p = target;
  151. int ok=0;
  152. *p = tor_parse_uint64(value, 10, 0, UINT64_MAX, &ok, NULL);
  153. if (!ok) {
  154. tor_asprintf(errmsg, "Integer %s is malformed or out of bounds.",
  155. value);
  156. return -1;
  157. }
  158. return 0;
  159. }
  160. static char *
  161. uint64_encode(const void *value, const void *params)
  162. {
  163. (void)params;
  164. uint64_t v = *(uint64_t*)value;
  165. char *result;
  166. tor_asprintf(&result, "%"PRIu64, v);
  167. return result;
  168. }
  169. static void
  170. uint64_clear(void *value, const void *params)
  171. {
  172. (void)params;
  173. *(uint64_t*)value = 0;
  174. }
  175. static const var_type_fns_t uint64_fns = {
  176. .parse = uint64_parse,
  177. .encode = uint64_encode,
  178. .clear = uint64_clear,
  179. };
  180. /////
  181. // CONFIG_TYPE_INTERVAL
  182. // CONFIG_TYPE_MSEC_INTERVAL
  183. // CONFIG_TYPE_MEMUNIT
  184. //
  185. // These types are implemented using the config_parse_units() function.
  186. // The intervals are stored as ints, whereas memory units are stored as
  187. // uint64_ts.
  188. /////
  189. static int
  190. units_parse_u64(void *target, const char *value, char **errmsg,
  191. const void *params)
  192. {
  193. const unit_table_t *table = params;
  194. tor_assert(table);
  195. uint64_t *v = (uint64_t*)target;
  196. int ok=1;
  197. *v = config_parse_units(value, table, &ok);
  198. if (!ok) {
  199. *errmsg = tor_strdup("Provided value is malformed or out of bounds.");
  200. return -1;
  201. }
  202. return 0;
  203. }
  204. static int
  205. units_parse_int(void *target, const char *value, char **errmsg,
  206. const void *params)
  207. {
  208. const unit_table_t *table = params;
  209. tor_assert(table);
  210. int *v = (int*)target;
  211. int ok=1;
  212. uint64_t u64 = config_parse_units(value, table, &ok);
  213. if (!ok) {
  214. *errmsg = tor_strdup("Provided value is malformed or out of bounds.");
  215. return -1;
  216. }
  217. if (u64 > INT_MAX) {
  218. tor_asprintf(errmsg, "Provided value %s is too large", value);
  219. return -1;
  220. }
  221. *v = (int) u64;
  222. return 0;
  223. }
  224. static bool
  225. units_ok_int(const void *value, const void *params)
  226. {
  227. (void)params;
  228. int v = *(int*)value;
  229. return v >= 0;
  230. }
  231. static const var_type_fns_t memunit_fns = {
  232. .parse = units_parse_u64,
  233. .encode = uint64_encode, // doesn't use params
  234. .clear = uint64_clear, // doesn't use params
  235. };
  236. static const var_type_fns_t interval_fns = {
  237. .parse = units_parse_int,
  238. .encode = int_encode, // doesn't use params
  239. .clear = int_clear, // doesn't use params,
  240. .ok = units_ok_int // can't use int_ok, since that expects int params.
  241. };
  242. /////
  243. // CONFIG_TYPE_DOUBLE
  244. //
  245. // This is a nice simple double.
  246. /////
  247. static int
  248. double_parse(void *target, const char *value, char **errmsg,
  249. const void *params)
  250. {
  251. (void)params;
  252. (void)errmsg;
  253. double *v = (double*)target;
  254. // XXXX This is the preexisting behavior, but we should detect errors here.
  255. *v = atof(value);
  256. return 0;
  257. }
  258. static char *
  259. double_encode(const void *value, const void *params)
  260. {
  261. (void)params;
  262. double v = *(double*)value;
  263. char *result;
  264. tor_asprintf(&result, "%f", v);
  265. return result;
  266. }
  267. static void
  268. double_clear(void *value, const void *params)
  269. {
  270. (void)params;
  271. double *v = (double *)value;
  272. *v = 0.0;
  273. }
  274. static const var_type_fns_t double_fns = {
  275. .parse = double_parse,
  276. .encode = double_encode,
  277. .clear = double_clear,
  278. };
  279. /////
  280. // CONFIG_TYPE_BOOL
  281. // CONFIG_TYPE_AUTOBOOL
  282. //
  283. // These types are implemented as a case-insensitive string-to-integer
  284. // mapping.
  285. /////
  286. typedef struct enumeration_table_t {
  287. const char *name;
  288. int value;
  289. } enumeration_table_t;
  290. static int
  291. enum_parse(void *target, const char *value, char **errmsg,
  292. const void *params)
  293. {
  294. const enumeration_table_t *table = params;
  295. int *p = (int *)target;
  296. for (; table->name; ++table) {
  297. if (!strcasecmp(value, table->name)) {
  298. *p = table->value;
  299. return 0;
  300. }
  301. }
  302. tor_asprintf(errmsg, "Unrecognized value %s.", value);
  303. return -1;
  304. }
  305. static char *
  306. enum_encode(const void *value, const void *params)
  307. {
  308. int v = *(const int*)value;
  309. const enumeration_table_t *table = params;
  310. for (; table->name; ++table) {
  311. if (v == table->value)
  312. return tor_strdup(table->name);
  313. }
  314. return NULL; // error.
  315. }
  316. static void
  317. enum_clear(void *value, const void *params)
  318. {
  319. int *p = (int*)value;
  320. const enumeration_table_t *table = params;
  321. tor_assert(table->name);
  322. *p = table->value;
  323. }
  324. static bool
  325. enum_ok(const void *value, const void *params)
  326. {
  327. int v = *(const int*)value;
  328. const enumeration_table_t *table = params;
  329. for (; table->name; ++table) {
  330. if (v == table->value)
  331. return true;
  332. }
  333. return false;
  334. }
  335. static const enumeration_table_t enum_table_bool[] = {
  336. { "0", 0 },
  337. { "1", 1 },
  338. { NULL, 0 },
  339. };
  340. static const enumeration_table_t enum_table_autobool[] = {
  341. { "0", 0 },
  342. { "1", 1 },
  343. { "auto", -1 },
  344. { NULL, 0 },
  345. };
  346. static const var_type_fns_t enum_fns = {
  347. .parse = enum_parse,
  348. .encode = enum_encode,
  349. .clear = enum_clear,
  350. .ok = enum_ok,
  351. };
  352. /////
  353. // CONFIG_TYPE_ISOTIME
  354. //
  355. // This is a time_t, encoded in ISO8601 format.
  356. /////
  357. static int
  358. time_parse(void *target, const char *value, char **errmsg,
  359. const void *params)
  360. {
  361. (void) params;
  362. time_t *p = target;
  363. if (parse_iso_time(value, p) < 0) {
  364. tor_asprintf(errmsg, "Invalid time %s", escaped(value));
  365. return -1;
  366. }
  367. return 0;
  368. }
  369. static char *
  370. time_encode(const void *value, const void *params)
  371. {
  372. (void)params;
  373. time_t v = *(const time_t *)value;
  374. char *result = tor_malloc(ISO_TIME_LEN+1);
  375. format_iso_time(result, v);
  376. return result;
  377. }
  378. static void
  379. time_clear(void *value, const void *params)
  380. {
  381. (void)params;
  382. time_t *t = value;
  383. *t = 0;
  384. }
  385. static const var_type_fns_t time_fns = {
  386. .parse = time_parse,
  387. .encode = time_encode,
  388. .clear = time_clear,
  389. };
  390. /////
  391. // CONFIG_TYPE_CSV
  392. //
  393. // This type is a comma-separated list of strings, stored in a smartlist_t.
  394. // An empty list may be encoded either as an empty smartlist, or as NULL.
  395. /////
  396. static int
  397. csv_parse(void *target, const char *value, char **errmsg,
  398. const void *params)
  399. {
  400. (void)params;
  401. (void)errmsg;
  402. smartlist_t **sl = (smartlist_t**)target;
  403. *sl = smartlist_new();
  404. smartlist_split_string(*sl, value, ",",
  405. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  406. return 0;
  407. }
  408. static char *
  409. csv_encode(const void *value, const void *params)
  410. {
  411. (void)params;
  412. const smartlist_t *sl = *(const smartlist_t **)value;
  413. if (! sl)
  414. return tor_strdup("");
  415. return smartlist_join_strings(*(smartlist_t**)value, ",", 0, NULL);
  416. }
  417. static void
  418. csv_clear(void *value, const void *params)
  419. {
  420. (void)params;
  421. smartlist_t **sl = (smartlist_t**)value;
  422. if (!*sl)
  423. return;
  424. SMARTLIST_FOREACH(*sl, char *, cp, tor_free(cp));
  425. smartlist_free(*sl); // clears pointer.
  426. }
  427. static const var_type_fns_t csv_fns = {
  428. .parse = csv_parse,
  429. .encode = csv_encode,
  430. .clear = csv_clear,
  431. };
  432. /////
  433. // CONFIG_TYPE_CSV_INTERVAL
  434. //
  435. // This type used to be a list of time intervals, used to determine a download
  436. // schedule. Now, only the first interval counts: everything after the first
  437. // comma is discarded.
  438. /////
  439. static int
  440. legacy_csv_interval_parse(void *target, const char *value, char **errmsg,
  441. const void *params)
  442. {
  443. (void)params;
  444. /* We used to have entire smartlists here. But now that all of our
  445. * download schedules use exponential backoff, only the first part
  446. * matters. */
  447. const char *comma = strchr(value, ',');
  448. const char *val = value;
  449. char *tmp = NULL;
  450. if (comma) {
  451. tmp = tor_strndup(val, comma - val);
  452. val = tmp;
  453. }
  454. int rv = units_parse_int(target, val, errmsg, &time_units);
  455. tor_free(tmp);
  456. return rv;
  457. }
  458. static const var_type_fns_t legacy_csv_interval_fns = {
  459. .parse = legacy_csv_interval_parse,
  460. .encode = int_encode,
  461. .clear = int_clear,
  462. };
  463. /////
  464. // CONFIG_TYPE_LINELIST
  465. // CONFIG_TYPE_LINELIST_S
  466. // CONFIG_TYPE_LINELIST_V
  467. //
  468. // A linelist is a raw config_line_t list. Order is preserved.
  469. //
  470. // The LINELIST type is used for homogeneous lists, where all the lines
  471. // have the same key.
  472. //
  473. // The LINELIST_S and LINELIST_V types are used for the case where multiple
  474. // lines of different keys are kept in a single list, to preserve their
  475. // relative order. The unified list is stored as a "virtual" variable whose
  476. // type is LINELIST_V; the individual sublists are treated as variables of
  477. // type LINELIST_S.
  478. //
  479. // A linelist may be fragile or non-fragile. Assigning a line to a fragile
  480. // linelist replaces the list with the line. If the line has the "APPEND"
  481. // command set on it, or if the list is non-fragile, the line is appended.
  482. // Either way, the new list is non-fragile.
  483. /////
  484. static int
  485. linelist_kv_parse(void *target, const struct config_line_t *line,
  486. char **errmsg, const void *params)
  487. {
  488. (void)params;
  489. (void)errmsg;
  490. config_line_t **lines = target;
  491. if (*lines && (*lines)->fragile) {
  492. if (line->command == CONFIG_LINE_APPEND) {
  493. (*lines)->fragile = 0;
  494. } else {
  495. config_free_lines(*lines); // sets it to NULL
  496. }
  497. }
  498. config_line_append(lines, line->key, line->value);
  499. return 0;
  500. }
  501. static int
  502. linelist_kv_virt_noparse(void *target, const struct config_line_t *line,
  503. char **errmsg, const void *params)
  504. {
  505. (void)target;
  506. (void)line;
  507. (void)params;
  508. *errmsg = tor_strdup("Cannot assign directly to virtual option.");
  509. return -1;
  510. }
  511. static struct config_line_t *
  512. linelist_kv_encode(const char *key, const void *value,
  513. const void *params)
  514. {
  515. (void)key;
  516. (void)params;
  517. config_line_t *lines = *(config_line_t **)value;
  518. return config_lines_dup(lines);
  519. }
  520. static struct config_line_t *
  521. linelist_s_kv_encode(const char *key, const void *value,
  522. const void *params)
  523. {
  524. (void)params;
  525. config_line_t *lines = *(config_line_t **)value;
  526. return config_lines_dup_and_filter(lines, key);
  527. }
  528. static void
  529. linelist_clear(void *target, const void *params)
  530. {
  531. (void)params;
  532. config_line_t **lines = target;
  533. config_free_lines(*lines); // sets it to NULL
  534. }
  535. static bool
  536. linelist_eq(const void *a, const void *b, const void *params)
  537. {
  538. (void)params;
  539. const config_line_t *lines_a = *(const config_line_t **)a;
  540. const config_line_t *lines_b = *(const config_line_t **)b;
  541. return config_lines_eq(lines_a, lines_b);
  542. }
  543. static int
  544. linelist_copy(void *target, const void *value, const void *params)
  545. {
  546. (void)params;
  547. config_line_t **ptr = (config_line_t **)target;
  548. const config_line_t *val = *(const config_line_t **)value;
  549. config_free_lines(*ptr);
  550. *ptr = config_lines_dup(val);
  551. return 0;
  552. }
  553. static const var_type_fns_t linelist_fns = {
  554. .kv_parse = linelist_kv_parse,
  555. .kv_encode = linelist_kv_encode,
  556. .clear = linelist_clear,
  557. .eq = linelist_eq,
  558. .copy = linelist_copy,
  559. };
  560. static const var_type_fns_t linelist_v_fns = {
  561. .kv_parse = linelist_kv_virt_noparse,
  562. .kv_encode = linelist_kv_encode,
  563. .clear = linelist_clear,
  564. .eq = linelist_eq,
  565. .copy = linelist_copy,
  566. };
  567. static const var_type_fns_t linelist_s_fns = {
  568. .kv_parse = linelist_kv_parse,
  569. .kv_encode = linelist_s_kv_encode,
  570. .clear = linelist_clear,
  571. .eq = linelist_eq,
  572. .copy = linelist_copy,
  573. };
  574. /////
  575. // CONFIG_TYPE_ROUTERSET
  576. //
  577. // XXXX This type is not implemented here, since routerset_t is not available
  578. // XXXX to this module.
  579. /////
  580. /////
  581. // CONFIG_TYPE_OBSOLETE
  582. //
  583. // Used to indicate an obsolete option.
  584. //
  585. // XXXX This is not a type, and should be handled at a higher level of
  586. // XXXX abstraction.
  587. /////
  588. static int
  589. ignore_parse(void *target, const char *value, char **errmsg,
  590. const void *params)
  591. {
  592. (void)target;
  593. (void)value;
  594. (void)errmsg;
  595. (void)params;
  596. // XXXX move this to a higher level, once such a level exists.
  597. log_warn(LD_GENERAL, "Skipping obsolete configuration option.");
  598. return 0;
  599. }
  600. static char *
  601. ignore_encode(const void *value, const void *params)
  602. {
  603. (void)value;
  604. (void)params;
  605. return NULL;
  606. }
  607. static const var_type_fns_t ignore_fns = {
  608. .parse = ignore_parse,
  609. .encode = ignore_encode,
  610. };
  611. /**
  612. * Table mapping conf_type_t values to var_type_def_t objects.
  613. **/
  614. static const var_type_def_t type_definitions_table[] = {
  615. [CONFIG_TYPE_STRING] = { "String", &string_fns, NULL },
  616. [CONFIG_TYPE_FILENAME] = { "Filename", &string_fns, NULL },
  617. [CONFIG_TYPE_INT] = { "SignedInteger", &int_fns, &INT_PARSE_UNRESTRICTED },
  618. [CONFIG_TYPE_POSINT] = { "Integer", &int_fns, &INT_PARSE_POSINT },
  619. [CONFIG_TYPE_UINT64] = { "Integer", &uint64_fns, NULL, },
  620. [CONFIG_TYPE_MEMUNIT] = { "DataSize", &memunit_fns, &memory_units },
  621. [CONFIG_TYPE_INTERVAL] = { "TimeInterval", &interval_fns, &time_units },
  622. [CONFIG_TYPE_MSEC_INTERVAL] = { "TimeMsecInterval", &interval_fns,
  623. &time_msec_units },
  624. [CONFIG_TYPE_DOUBLE] = { "Float", &double_fns, NULL },
  625. [CONFIG_TYPE_BOOL] = { "Boolean", &enum_fns, &enum_table_bool },
  626. [CONFIG_TYPE_AUTOBOOL] = { "Boolean+Auto", &enum_fns, &enum_table_autobool },
  627. [CONFIG_TYPE_ISOTIME] = { "Time", &time_fns, NULL },
  628. [CONFIG_TYPE_CSV] = { "CommaList", &csv_fns, NULL },
  629. [CONFIG_TYPE_CSV_INTERVAL] = { "TimeInterval", &legacy_csv_interval_fns,
  630. NULL },
  631. [CONFIG_TYPE_LINELIST] = { "LineList", &linelist_fns, NULL },
  632. [CONFIG_TYPE_LINELIST_S] = { "Dependent", &linelist_s_fns, NULL },
  633. [CONFIG_TYPE_LINELIST_V] = { "Virtual", &linelist_v_fns, NULL },
  634. [CONFIG_TYPE_OBSOLETE] = { "Obsolete", &ignore_fns, NULL }
  635. };
  636. /**
  637. * Return a pointer to the var_type_def_t object for the given
  638. * config_type_t value, or NULL if no such type definition exists.
  639. **/
  640. const var_type_def_t *
  641. lookup_type_def(config_type_t type)
  642. {
  643. int t = type;
  644. tor_assert(t >= 0);
  645. if (t >= (int)ARRAY_LENGTH(type_definitions_table))
  646. return NULL;
  647. return &type_definitions_table[t];
  648. }