confline.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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-2017, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. #include "compat.h"
  7. #include "confline.h"
  8. #include "torlog.h"
  9. #include "util.h"
  10. #include "container.h"
  11. static int config_get_lines_aux(const char *string, config_line_t **result,
  12. int extended, int allow_include,
  13. int *has_include, int recursion_level,
  14. config_line_t **last);
  15. static smartlist_t *config_get_file_list(const char *path);
  16. static int config_get_included_list(const char *path, int recursion_level,
  17. int extended, config_line_t **list,
  18. config_line_t **list_last);
  19. static int config_process_include(const char *path, int recursion_level,
  20. int extended, config_line_t ***next,
  21. config_line_t **list_last);
  22. /** Helper: allocate a new configuration option mapping 'key' to 'val',
  23. * append it to *<b>lst</b>. */
  24. void
  25. config_line_append(config_line_t **lst,
  26. const char *key,
  27. const char *val)
  28. {
  29. tor_assert(lst);
  30. config_line_t *newline;
  31. newline = tor_malloc_zero(sizeof(config_line_t));
  32. newline->key = tor_strdup(key);
  33. newline->value = tor_strdup(val);
  34. newline->next = NULL;
  35. while (*lst)
  36. lst = &((*lst)->next);
  37. (*lst) = newline;
  38. }
  39. /** Helper: allocate a new configuration option mapping 'key' to 'val',
  40. * and prepend it to *<b>lst</b> */
  41. void
  42. config_line_prepend(config_line_t **lst,
  43. const char *key,
  44. const char *val)
  45. {
  46. tor_assert(lst);
  47. config_line_t *newline;
  48. newline = tor_malloc_zero(sizeof(config_line_t));
  49. newline->key = tor_strdup(key);
  50. newline->value = tor_strdup(val);
  51. newline->next = *lst;
  52. *lst = newline;
  53. }
  54. /** Return the first line in <b>lines</b> whose key is exactly <b>key</b>, or
  55. * NULL if no such key exists.
  56. *
  57. * (In options parsing, this is for handling commandline-only options only;
  58. * other options should be looked up in the appropriate data structure.) */
  59. const config_line_t *
  60. config_line_find(const config_line_t *lines,
  61. const char *key)
  62. {
  63. const config_line_t *cl;
  64. for (cl = lines; cl; cl = cl->next) {
  65. if (!strcmp(cl->key, key))
  66. return cl;
  67. }
  68. return NULL;
  69. }
  70. /** Auxiliary function that does all the work of config_get_lines.
  71. * <b>recursion_level</b> is the count of how many nested %includes we have.
  72. * Returns the a pointer to the last element of the <b>result</b> in
  73. * <b>last</b>. */
  74. static int
  75. config_get_lines_aux(const char *string, config_line_t **result, int extended,
  76. int allow_include, int *has_include, int recursion_level,
  77. config_line_t **last)
  78. {
  79. config_line_t *list = NULL, **next, *list_last = NULL;
  80. char *k, *v;
  81. const char *parse_err;
  82. int include_used = 0;
  83. if (recursion_level > MAX_INCLUDE_RECURSION_LEVEL) {
  84. log_warn(LD_CONFIG, "Error while parsing configuration: more than %d "
  85. "nested %%includes.", MAX_INCLUDE_RECURSION_LEVEL);
  86. return -1;
  87. }
  88. next = &list;
  89. do {
  90. k = v = NULL;
  91. string = parse_config_line_from_str_verbose(string, &k, &v, &parse_err);
  92. if (!string) {
  93. log_warn(LD_CONFIG, "Error while parsing configuration: %s",
  94. parse_err?parse_err:"<unknown>");
  95. config_free_lines(list);
  96. tor_free(k);
  97. tor_free(v);
  98. return -1;
  99. }
  100. if (k && v) {
  101. unsigned command = CONFIG_LINE_NORMAL;
  102. if (extended) {
  103. if (k[0] == '+') {
  104. char *k_new = tor_strdup(k+1);
  105. tor_free(k);
  106. k = k_new;
  107. command = CONFIG_LINE_APPEND;
  108. } else if (k[0] == '/') {
  109. char *k_new = tor_strdup(k+1);
  110. tor_free(k);
  111. k = k_new;
  112. tor_free(v);
  113. v = tor_strdup("");
  114. command = CONFIG_LINE_CLEAR;
  115. }
  116. }
  117. if (allow_include && !strcmp(k, "%include")) {
  118. tor_free(k);
  119. include_used = 1;
  120. if (config_process_include(v, recursion_level, extended, &next,
  121. &list_last) < 0) {
  122. log_warn(LD_CONFIG, "Error reading included configuration "
  123. "file or directory: \"%s\".", v);
  124. config_free_lines(list);
  125. tor_free(v);
  126. return -1;
  127. }
  128. tor_free(v);
  129. } else {
  130. /* This list can get long, so we keep a pointer to the end of it
  131. * rather than using config_line_append over and over and getting
  132. * n^2 performance. */
  133. *next = tor_malloc_zero(sizeof(**next));
  134. (*next)->key = k;
  135. (*next)->value = v;
  136. (*next)->next = NULL;
  137. (*next)->command = command;
  138. list_last = *next;
  139. next = &((*next)->next);
  140. }
  141. } else {
  142. tor_free(k);
  143. tor_free(v);
  144. }
  145. } while (*string);
  146. if (last) {
  147. *last = list_last;
  148. }
  149. if (has_include) {
  150. *has_include = include_used;
  151. }
  152. *result = list;
  153. return 0;
  154. }
  155. /** Helper: parse the config string and strdup into key/value
  156. * strings. Set *result to the list, or NULL if parsing the string
  157. * failed. Set *has_include to 1 if <b>result</b> has values from
  158. * %included files. Return 0 on success, -1 on failure. Warn and ignore any
  159. * misformatted lines.
  160. *
  161. * If <b>extended</b> is set, then treat keys beginning with / and with + as
  162. * indicating "clear" and "append" respectively. */
  163. int
  164. config_get_lines_include(const char *string, config_line_t **result,
  165. int extended, int *has_include)
  166. {
  167. return config_get_lines_aux(string, result, extended, 1, has_include, 1,
  168. NULL);
  169. }
  170. /** Same as config_get_lines_include but does not allow %include */
  171. int
  172. config_get_lines(const char *string, config_line_t **result, int extended)
  173. {
  174. return config_get_lines_aux(string, result, extended, 0, NULL, 1, NULL);
  175. }
  176. /** Adds a list of configuration files present on <b>path</b> to
  177. * <b>file_list</b>. <b>path</b> can be a file or a directory. If it is a file,
  178. * only that file will be added to <b>file_list</b>. If it is a directory,
  179. * all paths for files on that directory root (no recursion) except for files
  180. * whose name starts with a dot will be added to <b>file_list</b>.
  181. * Return 0 on success, -1 on failure. Ignores empty files.
  182. */
  183. static smartlist_t *
  184. config_get_file_list(const char *path)
  185. {
  186. smartlist_t *file_list = smartlist_new();
  187. file_status_t file_type = file_status(path);
  188. if (file_type == FN_FILE) {
  189. smartlist_add_strdup(file_list, path);
  190. return file_list;
  191. } else if (file_type == FN_DIR) {
  192. smartlist_t *all_files = tor_listdir(path);
  193. if (!all_files) {
  194. smartlist_free(file_list);
  195. return NULL;
  196. }
  197. smartlist_sort_strings(all_files);
  198. SMARTLIST_FOREACH_BEGIN(all_files, char *, f) {
  199. if (f[0] == '.') {
  200. tor_free(f);
  201. continue;
  202. }
  203. char *fullname;
  204. tor_asprintf(&fullname, "%s"PATH_SEPARATOR"%s", path, f);
  205. tor_free(f);
  206. if (file_status(fullname) != FN_FILE) {
  207. tor_free(fullname);
  208. continue;
  209. }
  210. smartlist_add(file_list, fullname);
  211. } SMARTLIST_FOREACH_END(f);
  212. smartlist_free(all_files);
  213. return file_list;
  214. } else if (file_type == FN_EMPTY) {
  215. return file_list;
  216. } else {
  217. smartlist_free(file_list);
  218. return NULL;
  219. }
  220. }
  221. /** Creates a list of config lines present on included <b>path</b>.
  222. * Set <b>list</b> to the list and <b>list_last</b> to the last element of
  223. * <b>list</b>. Return 0 on success, -1 on failure. */
  224. static int
  225. config_get_included_list(const char *path, int recursion_level, int extended,
  226. config_line_t **list, config_line_t **list_last)
  227. {
  228. char *included_conf = read_file_to_str(path, 0, NULL);
  229. if (!included_conf) {
  230. return -1;
  231. }
  232. if (config_get_lines_aux(included_conf, list, extended, 1, NULL,
  233. recursion_level+1, list_last) < 0) {
  234. tor_free(included_conf);
  235. return -1;
  236. }
  237. tor_free(included_conf);
  238. return 0;
  239. }
  240. /** Process an %include <b>path</b> in a config file. Set <b>next</b> to a
  241. * pointer to the next pointer of the last element of the config_line_t list
  242. * obtained from the config file and <b>list_last</b> to the last element of
  243. * the same list. Return 0 on success, -1 on failure. */
  244. static int
  245. config_process_include(const char *path, int recursion_level, int extended,
  246. config_line_t ***next, config_line_t **list_last)
  247. {
  248. #if 0
  249. // Disabled -- we already unescape_string() on the result. */
  250. char *unquoted_path = get_unquoted_path(path);
  251. if (!unquoted_path) {
  252. return -1;
  253. }
  254. smartlist_t *config_files = config_get_file_list(unquoted_path);
  255. if (!config_files) {
  256. tor_free(unquoted_path);
  257. return -1;
  258. }
  259. tor_free(unquoted_path);
  260. #endif
  261. smartlist_t *config_files = config_get_file_list(path);
  262. if (!config_files) {
  263. return -1;
  264. }
  265. SMARTLIST_FOREACH_BEGIN(config_files, char *, config_file) {
  266. config_line_t *included_list = NULL;
  267. if (config_get_included_list(config_file, recursion_level, extended,
  268. &included_list, list_last) < 0) {
  269. SMARTLIST_FOREACH(config_files, char *, f, tor_free(f));
  270. smartlist_free(config_files);
  271. return -1;
  272. }
  273. tor_free(config_file);
  274. **next = included_list;
  275. *next = &(*list_last)->next;
  276. } SMARTLIST_FOREACH_END(config_file);
  277. smartlist_free(config_files);
  278. return 0;
  279. }
  280. /**
  281. * Free all the configuration lines on the linked list <b>front</b>.
  282. */
  283. void
  284. config_free_lines(config_line_t *front)
  285. {
  286. config_line_t *tmp;
  287. while (front) {
  288. tmp = front;
  289. front = tmp->next;
  290. tor_free(tmp->key);
  291. tor_free(tmp->value);
  292. tor_free(tmp);
  293. }
  294. }
  295. /** Return a newly allocated deep copy of the lines in <b>inp</b>. */
  296. config_line_t *
  297. config_lines_dup(const config_line_t *inp)
  298. {
  299. return config_lines_dup_and_filter(inp, NULL);
  300. }
  301. /** Return a newly allocated deep copy of the lines in <b>inp</b>,
  302. * but only the ones that match <b>key</b>. */
  303. config_line_t *
  304. config_lines_dup_and_filter(const config_line_t *inp,
  305. const char *key)
  306. {
  307. config_line_t *result = NULL;
  308. config_line_t **next_out = &result;
  309. while (inp) {
  310. if (key && strcasecmpstart(inp->key, key)) {
  311. inp = inp->next;
  312. continue;
  313. }
  314. *next_out = tor_malloc_zero(sizeof(config_line_t));
  315. (*next_out)->key = tor_strdup(inp->key);
  316. (*next_out)->value = tor_strdup(inp->value);
  317. inp = inp->next;
  318. next_out = &((*next_out)->next);
  319. }
  320. (*next_out) = NULL;
  321. return result;
  322. }
  323. /** Return true iff a and b contain identical keys and values in identical
  324. * order. */
  325. int
  326. config_lines_eq(config_line_t *a, config_line_t *b)
  327. {
  328. while (a && b) {
  329. if (strcasecmp(a->key, b->key) || strcmp(a->value, b->value))
  330. return 0;
  331. a = a->next;
  332. b = b->next;
  333. }
  334. if (a || b)
  335. return 0;
  336. return 1;
  337. }
  338. /** Return the number of lines in <b>a</b> whose key is <b>key</b>. */
  339. int
  340. config_count_key(const config_line_t *a, const char *key)
  341. {
  342. int n = 0;
  343. while (a) {
  344. if (!strcasecmp(a->key, key)) {
  345. ++n;
  346. }
  347. a = a->next;
  348. }
  349. return n;
  350. }
  351. /** Given a string containing part of a configuration file or similar format,
  352. * advance past comments and whitespace and try to parse a single line. If we
  353. * parse a line successfully, set *<b>key_out</b> to a new string holding the
  354. * key portion and *<b>value_out</b> to a new string holding the value portion
  355. * of the line, and return a pointer to the start of the next line. If we run
  356. * out of data, return a pointer to the end of the string. If we encounter an
  357. * error, return NULL and set *<b>err_out</b> (if provided) to an error
  358. * message.
  359. */
  360. const char *
  361. parse_config_line_from_str_verbose(const char *line, char **key_out,
  362. char **value_out,
  363. const char **err_out)
  364. {
  365. /*
  366. See torrc_format.txt for a description of the (silly) format this parses.
  367. */
  368. const char *key, *val, *cp;
  369. int continuation = 0;
  370. tor_assert(key_out);
  371. tor_assert(value_out);
  372. *key_out = *value_out = NULL;
  373. key = val = NULL;
  374. /* Skip until the first keyword. */
  375. while (1) {
  376. while (TOR_ISSPACE(*line))
  377. ++line;
  378. if (*line == '#') {
  379. while (*line && *line != '\n')
  380. ++line;
  381. } else {
  382. break;
  383. }
  384. }
  385. if (!*line) { /* End of string? */
  386. *key_out = *value_out = NULL;
  387. return line;
  388. }
  389. /* Skip until the next space or \ followed by newline. */
  390. key = line;
  391. while (*line && !TOR_ISSPACE(*line) && *line != '#' &&
  392. ! (line[0] == '\\' && line[1] == '\n'))
  393. ++line;
  394. *key_out = tor_strndup(key, line-key);
  395. /* Skip until the value. */
  396. while (*line == ' ' || *line == '\t')
  397. ++line;
  398. val = line;
  399. /* Find the end of the line. */
  400. if (*line == '\"') { // XXX No continuation handling is done here
  401. if (!(line = unescape_string(line, value_out, NULL))) {
  402. if (err_out)
  403. *err_out = "Invalid escape sequence in quoted string";
  404. return NULL;
  405. }
  406. while (*line == ' ' || *line == '\t')
  407. ++line;
  408. if (*line == '\r' && *(++line) == '\n')
  409. ++line;
  410. if (*line && *line != '#' && *line != '\n') {
  411. if (err_out)
  412. *err_out = "Excess data after quoted string";
  413. return NULL;
  414. }
  415. } else {
  416. /* Look for the end of the line. */
  417. while (*line && *line != '\n' && (*line != '#' || continuation)) {
  418. if (*line == '\\' && line[1] == '\n') {
  419. continuation = 1;
  420. line += 2;
  421. } else if (*line == '#') {
  422. do {
  423. ++line;
  424. } while (*line && *line != '\n');
  425. if (*line == '\n')
  426. ++line;
  427. } else {
  428. ++line;
  429. }
  430. }
  431. if (*line == '\n') {
  432. cp = line++;
  433. } else {
  434. cp = line;
  435. }
  436. /* Now back cp up to be the last nonspace character */
  437. while (cp>val && TOR_ISSPACE(*(cp-1)))
  438. --cp;
  439. tor_assert(cp >= val);
  440. /* Now copy out and decode the value. */
  441. *value_out = tor_strndup(val, cp-val);
  442. if (continuation) {
  443. char *v_out, *v_in;
  444. v_out = v_in = *value_out;
  445. while (*v_in) {
  446. if (*v_in == '#') {
  447. do {
  448. ++v_in;
  449. } while (*v_in && *v_in != '\n');
  450. if (*v_in == '\n')
  451. ++v_in;
  452. } else if (v_in[0] == '\\' && v_in[1] == '\n') {
  453. v_in += 2;
  454. } else {
  455. *v_out++ = *v_in++;
  456. }
  457. }
  458. *v_out = '\0';
  459. }
  460. }
  461. if (*line == '#') {
  462. do {
  463. ++line;
  464. } while (*line && *line != '\n');
  465. }
  466. while (TOR_ISSPACE(*line)) ++line;
  467. return line;
  468. }