confline.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. char *unquoted_path = get_unquoted_path(path);
  249. if (!unquoted_path) {
  250. return -1;
  251. }
  252. smartlist_t *config_files = config_get_file_list(unquoted_path);
  253. if (!config_files) {
  254. tor_free(unquoted_path);
  255. return -1;
  256. }
  257. tor_free(unquoted_path);
  258. SMARTLIST_FOREACH_BEGIN(config_files, char *, config_file) {
  259. config_line_t *included_list = NULL;
  260. if (config_get_included_list(config_file, recursion_level, extended,
  261. &included_list, list_last) < 0) {
  262. SMARTLIST_FOREACH(config_files, char *, f, tor_free(f));
  263. smartlist_free(config_files);
  264. return -1;
  265. }
  266. tor_free(config_file);
  267. **next = included_list;
  268. *next = &(*list_last)->next;
  269. } SMARTLIST_FOREACH_END(config_file);
  270. smartlist_free(config_files);
  271. return 0;
  272. }
  273. /**
  274. * Free all the configuration lines on the linked list <b>front</b>.
  275. */
  276. void
  277. config_free_lines(config_line_t *front)
  278. {
  279. config_line_t *tmp;
  280. while (front) {
  281. tmp = front;
  282. front = tmp->next;
  283. tor_free(tmp->key);
  284. tor_free(tmp->value);
  285. tor_free(tmp);
  286. }
  287. }
  288. /** Return a newly allocated deep copy of the lines in <b>inp</b>. */
  289. config_line_t *
  290. config_lines_dup(const config_line_t *inp)
  291. {
  292. return config_lines_dup_and_filter(inp, NULL);
  293. }
  294. /** Return a newly allocated deep copy of the lines in <b>inp</b>,
  295. * but only the ones that match <b>key</b>. */
  296. config_line_t *
  297. config_lines_dup_and_filter(const config_line_t *inp,
  298. const char *key)
  299. {
  300. config_line_t *result = NULL;
  301. config_line_t **next_out = &result;
  302. while (inp) {
  303. if (key && strcasecmpstart(inp->key, key)) {
  304. inp = inp->next;
  305. continue;
  306. }
  307. *next_out = tor_malloc_zero(sizeof(config_line_t));
  308. (*next_out)->key = tor_strdup(inp->key);
  309. (*next_out)->value = tor_strdup(inp->value);
  310. inp = inp->next;
  311. next_out = &((*next_out)->next);
  312. }
  313. (*next_out) = NULL;
  314. return result;
  315. }
  316. /** Return true iff a and b contain identical keys and values in identical
  317. * order. */
  318. int
  319. config_lines_eq(config_line_t *a, config_line_t *b)
  320. {
  321. while (a && b) {
  322. if (strcasecmp(a->key, b->key) || strcmp(a->value, b->value))
  323. return 0;
  324. a = a->next;
  325. b = b->next;
  326. }
  327. if (a || b)
  328. return 0;
  329. return 1;
  330. }
  331. /** Return the number of lines in <b>a</b> whose key is <b>key</b>. */
  332. int
  333. config_count_key(const config_line_t *a, const char *key)
  334. {
  335. int n = 0;
  336. while (a) {
  337. if (!strcasecmp(a->key, key)) {
  338. ++n;
  339. }
  340. a = a->next;
  341. }
  342. return n;
  343. }
  344. /** Given a string containing part of a configuration file or similar format,
  345. * advance past comments and whitespace and try to parse a single line. If we
  346. * parse a line successfully, set *<b>key_out</b> to a new string holding the
  347. * key portion and *<b>value_out</b> to a new string holding the value portion
  348. * of the line, and return a pointer to the start of the next line. If we run
  349. * out of data, return a pointer to the end of the string. If we encounter an
  350. * error, return NULL and set *<b>err_out</b> (if provided) to an error
  351. * message.
  352. */
  353. const char *
  354. parse_config_line_from_str_verbose(const char *line, char **key_out,
  355. char **value_out,
  356. const char **err_out)
  357. {
  358. /*
  359. See torrc_format.txt for a description of the (silly) format this parses.
  360. */
  361. const char *key, *val, *cp;
  362. int continuation = 0;
  363. tor_assert(key_out);
  364. tor_assert(value_out);
  365. *key_out = *value_out = NULL;
  366. key = val = NULL;
  367. /* Skip until the first keyword. */
  368. while (1) {
  369. while (TOR_ISSPACE(*line))
  370. ++line;
  371. if (*line == '#') {
  372. while (*line && *line != '\n')
  373. ++line;
  374. } else {
  375. break;
  376. }
  377. }
  378. if (!*line) { /* End of string? */
  379. *key_out = *value_out = NULL;
  380. return line;
  381. }
  382. /* Skip until the next space or \ followed by newline. */
  383. key = line;
  384. while (*line && !TOR_ISSPACE(*line) && *line != '#' &&
  385. ! (line[0] == '\\' && line[1] == '\n'))
  386. ++line;
  387. *key_out = tor_strndup(key, line-key);
  388. /* Skip until the value. */
  389. while (*line == ' ' || *line == '\t')
  390. ++line;
  391. val = line;
  392. /* Find the end of the line. */
  393. if (*line == '\"') { // XXX No continuation handling is done here
  394. if (!(line = unescape_string(line, value_out, NULL))) {
  395. if (err_out)
  396. *err_out = "Invalid escape sequence in quoted string";
  397. return NULL;
  398. }
  399. while (*line == ' ' || *line == '\t')
  400. ++line;
  401. if (*line == '\r' && *(++line) == '\n')
  402. ++line;
  403. if (*line && *line != '#' && *line != '\n') {
  404. if (err_out)
  405. *err_out = "Excess data after quoted string";
  406. return NULL;
  407. }
  408. } else {
  409. /* Look for the end of the line. */
  410. while (*line && *line != '\n' && (*line != '#' || continuation)) {
  411. if (*line == '\\' && line[1] == '\n') {
  412. continuation = 1;
  413. line += 2;
  414. } else if (*line == '#') {
  415. do {
  416. ++line;
  417. } while (*line && *line != '\n');
  418. if (*line == '\n')
  419. ++line;
  420. } else {
  421. ++line;
  422. }
  423. }
  424. if (*line == '\n') {
  425. cp = line++;
  426. } else {
  427. cp = line;
  428. }
  429. /* Now back cp up to be the last nonspace character */
  430. while (cp>val && TOR_ISSPACE(*(cp-1)))
  431. --cp;
  432. tor_assert(cp >= val);
  433. /* Now copy out and decode the value. */
  434. *value_out = tor_strndup(val, cp-val);
  435. if (continuation) {
  436. char *v_out, *v_in;
  437. v_out = v_in = *value_out;
  438. while (*v_in) {
  439. if (*v_in == '#') {
  440. do {
  441. ++v_in;
  442. } while (*v_in && *v_in != '\n');
  443. if (*v_in == '\n')
  444. ++v_in;
  445. } else if (v_in[0] == '\\' && v_in[1] == '\n') {
  446. v_in += 2;
  447. } else {
  448. *v_out++ = *v_in++;
  449. }
  450. }
  451. *v_out = '\0';
  452. }
  453. }
  454. if (*line == '#') {
  455. do {
  456. ++line;
  457. } while (*line && *line != '\n');
  458. }
  459. while (TOR_ISSPACE(*line)) ++line;
  460. return line;
  461. }