tor  0.4.0.1-alpha
conffile.c
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 
13 #include "lib/fs/conffile.h"
14 
16 #include "lib/encoding/confline.h"
17 #include "lib/fs/dir.h"
18 #include "lib/fs/files.h"
19 #include "lib/fs/path.h"
20 #include "lib/log/log.h"
21 #include "lib/malloc/malloc.h"
22 #include "lib/string/printf.h"
23 
24 static smartlist_t *config_get_file_list(const char *path,
25  smartlist_t *opened_files);
26 static int config_get_included_config(const char *path, int recursion_level,
27  int extended, config_line_t **config,
28  config_line_t **config_last,
29  smartlist_t *opened_lst);
30 static int config_process_include(const char *path, int recursion_level,
31  int extended, config_line_t **list,
32  config_line_t **list_last,
33  smartlist_t *opened_lst);
34 
44 int
45 config_get_lines_include(const char *string, config_line_t **result,
46  int extended, int *has_include,
47  smartlist_t *opened_lst)
48 {
49  return config_get_lines_aux(string, result, extended, 1, has_include,
50  opened_lst, 1, NULL, config_process_include);
51 }
52 
61 static smartlist_t *
62 config_get_file_list(const char *path, smartlist_t *opened_files)
63 {
64  smartlist_t *file_list = smartlist_new();
65 
66  if (opened_files) {
67  smartlist_add_strdup(opened_files, path);
68  }
69 
70  file_status_t file_type = file_status(path);
71  if (file_type == FN_FILE) {
72  smartlist_add_strdup(file_list, path);
73  return file_list;
74  } else if (file_type == FN_DIR) {
75  smartlist_t *all_files = tor_listdir(path);
76  if (!all_files) {
77  smartlist_free(file_list);
78  return NULL;
79  }
80  smartlist_sort_strings(all_files);
81  SMARTLIST_FOREACH_BEGIN(all_files, char *, f) {
82  if (f[0] == '.') {
83  tor_free(f);
84  continue;
85  }
86 
87  char *fullname;
88  tor_asprintf(&fullname, "%s"PATH_SEPARATOR"%s", path, f);
89  tor_free(f);
90 
91  if (opened_files) {
92  smartlist_add_strdup(opened_files, fullname);
93  }
94 
95  if (file_status(fullname) != FN_FILE) {
96  tor_free(fullname);
97  continue;
98  }
99  smartlist_add(file_list, fullname);
100  } SMARTLIST_FOREACH_END(f);
101  smartlist_free(all_files);
102  return file_list;
103  } else if (file_type == FN_EMPTY) {
104  return file_list;
105  } else {
106  smartlist_free(file_list);
107  return NULL;
108  }
109 }
110 
115 static int
116 config_get_included_config(const char *path, int recursion_level, int extended,
117  config_line_t **config, config_line_t **config_last,
118  smartlist_t *opened_lst)
119 {
120  char *included_conf = read_file_to_str(path, 0, NULL);
121  if (!included_conf) {
122  return -1;
123  }
124 
125  if (config_get_lines_aux(included_conf, config, extended, 1, NULL,
126  opened_lst, recursion_level+1, config_last,
127  config_process_include) < 0) {
128  tor_free(included_conf);
129  return -1;
130  }
131 
132  tor_free(included_conf);
133  return 0;
134 }
135 
140 static int
141 config_process_include(const char *path, int recursion_level, int extended,
142  config_line_t **list, config_line_t **list_last,
143  smartlist_t *opened_lst)
144 {
145  config_line_t *ret_list = NULL;
146  config_line_t **next = &ret_list;
147 
148  smartlist_t *config_files = config_get_file_list(path, opened_lst);
149  if (!config_files) {
150  return -1;
151  }
152 
153  int rv = -1;
154  SMARTLIST_FOREACH_BEGIN(config_files, const char *, config_file) {
155  config_line_t *included_config = NULL;
156  if (config_get_included_config(config_file, recursion_level, extended,
157  &included_config, list_last,
158  opened_lst) < 0) {
159  goto done;
160  }
161 
162  *next = included_config;
163  if (*list_last)
164  next = &(*list_last)->next;
165 
166  } SMARTLIST_FOREACH_END(config_file);
167  *list = ret_list;
168  rv = 0;
169 
170  done:
171  SMARTLIST_FOREACH(config_files, char *, f, tor_free(f));
172  smartlist_free(config_files);
173  return rv;
174 }
int config_get_lines_include(const char *string, struct config_line_t **result, int extended, int *has_include, struct smartlist_t *opened_lst)
Definition: conffile.c:45
Header for confline.c.
Header for printf.c.
Header for smartlist.c.
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
void smartlist_add_strdup(struct smartlist_t *sl, const char *string)
void smartlist_add(smartlist_t *sl, void *element)
#define tor_free(p)
Definition: malloc.h:52
Headers for util_malloc.c.
void smartlist_sort_strings(smartlist_t *sl)
Definition: smartlist.c:549
int tor_asprintf(char **strp, const char *fmt,...)
Definition: printf.c:75
int config_get_lines_aux(const char *string, config_line_t **result, int extended, int allow_include, int *has_include, struct smartlist_t *opened_lst, int recursion_level, config_line_t **last, include_handler_fn handle_include)
Definition: confline.c:91
Header for path.c.
#define SMARTLIST_FOREACH(sl, type, var, cmd)
file_status_t
Definition: files.h:55
Wrappers for reading and writing data to files on disk.
Headers for log.c.
file_status_t file_status(const char *filename)
Definition: files.c:212
Read configuration files from disk, with full include support.
Header for dir.c.