tor  0.4.2.0-alpha-dev
config.h
Go to the documentation of this file.
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 
12 #ifndef TOR_CONFIG_H
13 #define TOR_CONFIG_H
14 
17 
18 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(DARWIN)
19 #define KERNEL_MAY_SUPPORT_IPFW
20 #endif
21 
24 #define MIN_HEARTBEAT_PERIOD (30*60)
25 
27 #if SIZEOF_VOID_P >= 8
28 #define MAX_DEFAULT_MEMORY_QUEUE_SIZE (UINT64_C(8) << 30)
29 #else
30 #define MAX_DEFAULT_MEMORY_QUEUE_SIZE (UINT64_C(2) << 30)
31 #endif
32 
33 MOCK_DECL(const char*, get_dirportfrontpage, (void));
34 MOCK_DECL(const or_options_t *, get_options, (void));
35 MOCK_DECL(or_options_t *, get_options_mutable, (void));
36 int set_options(or_options_t *new_val, char **msg);
37 void config_free_all(void);
38 const char *safe_str_client(const char *address);
39 const char *safe_str(const char *address);
40 const char *escaped_safe_str_client(const char *address);
41 const char *escaped_safe_str(const char *address);
44 
46 typedef enum setopt_err_t {
47  SETOPT_OK = 0,
48  SETOPT_ERR_MISC = -1,
49  SETOPT_ERR_PARSE = -2,
50  SETOPT_ERR_TRANSITION = -3,
51  SETOPT_ERR_SETTING = -4,
52 } setopt_err_t;
53 setopt_err_t options_trial_assign(struct config_line_t *list, unsigned flags,
54  char **msg);
55 
56 uint32_t get_last_resolved_addr(void);
57 void reset_last_resolved_addr(void);
58 int resolve_my_address(int warn_severity, const or_options_t *options,
59  uint32_t *addr_out,
60  const char **method_out, char **hostname_out);
61 MOCK_DECL(int, is_local_addr, (const tor_addr_t *addr));
62 void options_init(or_options_t *options);
63 
64 #define OPTIONS_DUMP_MINIMAL 1
65 #define OPTIONS_DUMP_DEFAULTS 2
66 #define OPTIONS_DUMP_ALL 3
67 char *options_dump(const or_options_t *options, int how_to_dump);
68 int options_init_from_torrc(int argc, char **argv);
69 setopt_err_t options_init_from_string(const char *cf_defaults, const char *cf,
70  int command, const char *command_arg, char **msg);
71 int option_is_recognized(const char *key);
72 const char *option_get_canonical_name(const char *key);
73 struct config_line_t *option_get_assignment(const or_options_t *options,
74  const char *key);
75 int options_save_current(void);
76 const char *get_torrc_fname(int defaults_fname);
77 typedef enum {
78  DIRROOT_DATADIR,
79  DIRROOT_CACHEDIR,
80  DIRROOT_KEYDIR
81 } directory_root_t;
82 
83 MOCK_DECL(char *,
84  options_get_dir_fname2_suffix,
85  (const or_options_t *options,
86  directory_root_t roottype,
87  const char *sub1, const char *sub2,
88  const char *suffix));
89 
90 /* These macros wrap options_get_dir_fname2_suffix to provide a more
91  * convenient API for finding filenames that Tor uses inside its storage
92  * They are named according to a pattern:
93  * (options_)?get_(cache|key|data)dir_fname(2)?(_suffix)?
94  *
95  * Macros that begin with options_ take an options argument; the others
96  * work with respect to the global options.
97  *
98  * Each macro works relative to the data directory, the key directory,
99  * or the cache directory, as determined by which one is mentioned.
100  *
101  * Macro variants with "2" in their name take two path components; others
102  * take one.
103  *
104  * Macro variants with "_suffix" at the end take an additional suffix
105  * that gets appended to the end of the file
106  */
107 #define options_get_datadir_fname2_suffix(options, sub1, sub2, suffix) \
108  options_get_dir_fname2_suffix((options), DIRROOT_DATADIR, \
109  (sub1), (sub2), (suffix))
110 #define options_get_cachedir_fname2_suffix(options, sub1, sub2, suffix) \
111  options_get_dir_fname2_suffix((options), DIRROOT_CACHEDIR, \
112  (sub1), (sub2), (suffix))
113 #define options_get_keydir_fname2_suffix(options, sub1, sub2, suffix) \
114  options_get_dir_fname2_suffix((options), DIRROOT_KEYDIR, \
115  (sub1), (sub2), (suffix))
116 
117 #define options_get_datadir_fname(opts,sub1) \
118  options_get_datadir_fname2_suffix((opts),(sub1), NULL, NULL)
119 #define options_get_datadir_fname2(opts,sub1,sub2) \
120  options_get_datadir_fname2_suffix((opts),(sub1), (sub2), NULL)
121 
122 #define get_datadir_fname2_suffix(sub1, sub2, suffix) \
123  options_get_datadir_fname2_suffix(get_options(), (sub1), (sub2), (suffix))
124 #define get_datadir_fname(sub1) \
125  get_datadir_fname2_suffix((sub1), NULL, NULL)
126 #define get_datadir_fname2(sub1,sub2) \
127  get_datadir_fname2_suffix((sub1), (sub2), NULL)
128 #define get_datadir_fname_suffix(sub1, suffix) \
129  get_datadir_fname2_suffix((sub1), NULL, (suffix))
130 
132 #define options_get_keydir_fname(options, sub1) \
133  options_get_keydir_fname2_suffix((options), (sub1), NULL, NULL)
134 #define get_keydir_fname_suffix(sub1, suffix) \
135  options_get_keydir_fname2_suffix(get_options(), (sub1), NULL, suffix)
136 #define get_keydir_fname(sub1) \
137  options_get_keydir_fname2_suffix(get_options(), (sub1), NULL, NULL)
138 
139 #define get_cachedir_fname(sub1) \
140  options_get_cachedir_fname2_suffix(get_options(), (sub1), NULL, NULL)
141 #define get_cachedir_fname_suffix(sub1, suffix) \
142  options_get_cachedir_fname2_suffix(get_options(), (sub1), NULL, (suffix))
143 
144 #define safe_str_client(address) \
145  safe_str_client_opts(NULL, address)
146 #define safe_str(address) \
147  safe_str_opts(NULL, address)
148 
149 const char * safe_str_client_opts(const or_options_t *options,
150  const char *address);
151 const char * safe_str_opts(const or_options_t *options,
152  const char *address);
153 
154 int using_default_dir_authorities(const or_options_t *options);
155 
156 int create_keys_directory(const or_options_t *options);
157 
158 int check_or_create_data_subdir(const char *subdir);
159 int write_to_data_subdir(const char* subdir, const char* fname,
160  const char* str, const char* descr);
161 
162 int get_num_cpus(const or_options_t *options);
163 
164 MOCK_DECL(const smartlist_t *,get_configured_ports,(void));
165 int get_first_advertised_port_by_type_af(int listener_type,
166  int address_family);
167 #define get_primary_or_port() \
168  (get_first_advertised_port_by_type_af(CONN_TYPE_OR_LISTENER, AF_INET))
169 #define get_primary_dir_port() \
170  (get_first_advertised_port_by_type_af(CONN_TYPE_DIR_LISTENER, AF_INET))
171 const tor_addr_t *get_first_advertised_addr_by_type_af(int listener_type,
172  int address_family);
173 int port_exists_by_type_addr_port(int listener_type, const tor_addr_t *addr,
174  int port, int check_wildcard);
175 int port_exists_by_type_addr32h_port(int listener_type, uint32_t addr_ipv4h,
176  int port, int check_wildcard);
177 
178 char *get_first_listener_addrport_string(int listener_type);
179 
180 int options_need_geoip_info(const or_options_t *options,
181  const char **reason_out);
182 
184 
186  const char *question, char **answer,
187  const char **errmsg);
188 
189 uint32_t get_effective_bwrate(const or_options_t *options);
190 uint32_t get_effective_bwburst(const or_options_t *options);
191 
192 char *get_transport_bindaddr_from_config(const char *transport);
193 
194 int init_cookie_authentication(const char *fname, const char *header,
195  int cookie_len, int group_readable,
196  uint8_t **cookie_out, int *cookie_is_set_out);
197 
199 
200 int config_parse_commandline(int argc, char **argv, int ignore_errors,
201  struct config_line_t **result,
202  struct config_line_t **cmdline_result);
203 
204 void config_register_addressmaps(const or_options_t *options);
205 /* XXXX move to connection_edge.h */
206 int addressmap_register_auto(const char *from, const char *to,
207  time_t expires,
208  addressmap_entry_source_t addrmap_source,
209  const char **msg);
210 
211 int port_cfg_line_extract_addrport(const char *line,
212  char **addrport_out,
213  int *is_unix_out,
214  const char **rest_out);
215 
217 typedef struct bridge_line_t {
218  tor_addr_t addr; /* The IP address of the bridge. */
219  uint16_t port; /* The TCP port of the bridge. */
220  char *transport_name; /* The name of the pluggable transport that
221  should be used to connect to the bridge. */
222  char digest[DIGEST_LEN]; /* The bridge's identity key digest. */
223  smartlist_t *socks_args; /* SOCKS arguments for the pluggable
224  transport proxy. */
225 } bridge_line_t;
226 
227 void bridge_line_free_(bridge_line_t *bridge_line);
228 #define bridge_line_free(line) \
229  FREE_AND_NULL(bridge_line_t, bridge_line_free_, (line))
230 bridge_line_t *parse_bridge_line(const char *line);
232  const char *transport);
233 smartlist_t *get_options_for_server_transport(const char *transport);
234 
235 /* Port helper functions. */
236 int options_any_client_port_set(const or_options_t *options);
237 
238 #ifdef CONFIG_PRIVATE
239 
240 #define CL_PORT_NO_STREAM_OPTIONS (1u<<0)
241 #define CL_PORT_WARN_NONLOCAL (1u<<1)
242 /* Was CL_PORT_ALLOW_EXTRA_LISTENADDR (1u<<2) */
243 #define CL_PORT_SERVER_OPTIONS (1u<<3)
244 #define CL_PORT_FORBID_NONLOCAL (1u<<4)
245 #define CL_PORT_TAKES_HOSTNAMES (1u<<5)
246 #define CL_PORT_IS_UNIXSOCKET (1u<<6)
247 #define CL_PORT_DFLT_GROUP_WRITABLE (1u<<7)
248 
249 STATIC int options_act(const or_options_t *old_options);
250 #ifdef TOR_UNIT_TESTS
251 extern struct config_format_t options_format;
252 #endif
253 
254 STATIC port_cfg_t *port_cfg_new(size_t namelen);
255 #define port_cfg_free(port) \
256  FREE_AND_NULL(port_cfg_t, port_cfg_free_, (port))
257 STATIC void port_cfg_free_(port_cfg_t *port);
258 #define or_options_free(opt) \
259  FREE_AND_NULL(or_options_t, or_options_free_, (opt))
260 STATIC void or_options_free_(or_options_t *options);
261 STATIC int options_validate_single_onion(or_options_t *options,
262  char **msg);
263 STATIC int options_validate(or_options_t *old_options,
264  or_options_t *options,
265  or_options_t *default_options,
266  int from_setconf, char **msg);
267 STATIC int parse_transport_line(const or_options_t *options,
268  const char *line, int validate_only,
269  int server);
270 STATIC int consider_adding_dir_servers(const or_options_t *options,
271  const or_options_t *old_options);
273 MOCK_DECL(STATIC void, add_default_fallback_dir_servers, (void));
274 STATIC int parse_dir_authority_line(const char *line,
275  dirinfo_type_t required_type,
276  int validate_only);
277 STATIC int parse_dir_fallback_line(const char *line, int validate_only);
278 STATIC int have_enough_mem_for_dircache(const or_options_t *options,
279  size_t total_mem, char **msg);
280 STATIC int parse_port_config(smartlist_t *out,
281  const struct config_line_t *ports,
282  const char *portname,
283  int listener_type,
284  const char *defaultaddr,
285  int defaultport,
286  const unsigned flags);
287 
288 STATIC int check_bridge_distribution_setting(const char *bd);
289 
290 STATIC uint64_t compute_real_max_mem_in_queues(const uint64_t val,
291  int log_guess);
292 STATIC int open_and_add_file_log(const log_severity_list_t *severity,
293  const char *fname,
294  int truncate_log);
295 
296 #endif /* defined(CONFIG_PRIVATE) */
297 
298 #endif /* !defined(TOR_CONFIG_H) */
const char * safe_str_opts(const or_options_t *options, const char *address)
Definition: config.c:1086
smartlist_t * get_options_for_server_transport(const char *transport)
Definition: config.c:6334
const char * escaped_safe_str_client(const char *address)
Definition: config.c:1103
void bridge_line_free_(bridge_line_t *bridge_line)
Definition: config.c:5861
char * get_transport_bindaddr_from_config(const char *transport)
Definition: config.c:6314
int resolve_my_address(int warn_severity, const or_options_t *options, uint32_t *addr_out, const char **method_out, char **hostname_out)
Definition: config.c:2756
int config_parse_commandline(int argc, char **argv, int ignore_errors, struct config_line_t **result, struct config_line_t **cmdline_result)
Definition: config.c:2470
or_options_t * options_new(void)
Definition: config.c:2980
struct config_line_t * option_get_assignment(const or_options_t *options, const char *key)
Definition: config.c:2580
int getinfo_helper_config(control_connection_t *conn, const char *question, char **answer, const char **errmsg)
Definition: config.c:8162
int options_init_from_torrc(int argc, char **argv)
Definition: config.c:5157
int options_need_geoip_info(const or_options_t *options, const char **reason_out)
Definition: config.c:1708
STATIC void port_cfg_free_(port_cfg_t *port)
Definition: config.c:6635
int get_protocol_warning_severity_level(void)
Definition: config.c:1136
STATIC int parse_transport_line(const or_options_t *options, const char *line, int validate_only, int server)
Definition: config.c:6005
The or_options_t structure, which represents Tor's configuration.
setopt_err_t options_trial_assign(struct config_line_t *list, unsigned flags, char **msg)
Definition: config.c:2595
addressmap_entry_source_t
Definition: or.h:1019
int create_keys_directory(const or_options_t *options)
Definition: config.c:1378
STATIC int parse_port_config(smartlist_t *out, const config_line_t *ports, const char *portname, int listener_type, const char *defaultaddr, int defaultport, const unsigned flags)
Definition: config.c:6881
STATIC config_format_t options_format
Definition: config.c:856
dirinfo_type_t
Definition: or.h:887
smartlist_t * get_options_from_transport_options_line(const char *line, const char *transport)
Definition: config.c:6261
void reset_last_resolved_addr(void)
Definition: config.c:2694
STATIC port_cfg_t * port_cfg_new(size_t namelen)
Definition: config.c:6621
const char * option_get_canonical_name(const char *key)
Definition: config.c:2571
char * options_dump(const or_options_t *options, int how_to_dump)
Definition: config.c:2998
STATIC int check_bridge_distribution_setting(const char *bd)
Definition: config.c:6822
STATIC void add_default_trusted_dir_authorities(dirinfo_type_t type)
Definition: config.c:1189
int port_cfg_line_extract_addrport(const char *line, char **addrport_out, int *is_unix_out, const char **rest_out)
Definition: config.c:6742
#define DIGEST_LEN
Definition: digest_sizes.h:20
uint32_t get_last_resolved_addr(void)
Definition: config.c:2687
uint32_t get_effective_bwrate(const or_options_t *options)
Definition: config.c:1735
STATIC int have_enough_mem_for_dircache(const or_options_t *options, size_t total_mem, char **msg)
Definition: config.c:4657
int get_first_advertised_port_by_type_af(int listener_type, int address_family)
Definition: config.c:7716
int init_cookie_authentication(const char *fname, const char *header, int cookie_len, int group_readable, uint8_t **cookie_out, int *cookie_is_set_out)
Definition: config.c:8445
int option_is_recognized(const char *key)
Definition: config.c:2562
setopt_err_t
Definition: config.h:46
smartlist_t * get_list_of_ports_to_forward(void)
Definition: config.c:8124
void config_free_all(void)
Definition: config.c:1028
int addressmap_register_auto(const char *from, const char *to, time_t expires, addressmap_entry_source_t addrmap_source, const char **msg)
Definition: config.c:5614
const char * safe_str_client_opts(const or_options_t *options, const char *address)
Definition: config.c:1065
struct bridge_line_t bridge_line_t
STATIC int options_act(const or_options_t *old_options)
Definition: config.c:1836
bridge_line_t * parse_bridge_line(const char *line)
Definition: config.c:5887
int consider_adding_dir_servers(const or_options_t *options, const or_options_t *old_options)
Definition: config.c:1270
void options_init(or_options_t *options)
Definition: config.c:2988
STATIC int options_validate(or_options_t *old_options, or_options_t *options, or_options_t *default_options, int from_setconf, char **msg)
Definition: config.c:3352
const char * escaped_safe_str(const char *address)
Definition: config.c:1115
int get_num_cpus(const or_options_t *options)
Definition: config.c:7995
char * get_first_listener_addrport_string(int listener_type)
Definition: config.c:7662
const tor_addr_t * get_first_advertised_addr_by_type_af(int listener_type, int address_family)
Definition: config.c:7738
int parse_dir_fallback_line(const char *line, int validate_only)
Definition: config.c:6515
setopt_err_t options_init_from_string(const char *cf_defaults, const char *cf, int command, const char *command_arg, char **msg)
Definition: config.c:5376
Macros to implement mocking and selective exposure for the test code.
#define MOCK_DECL(rv, funcname, arglist)
Definition: testsupport.h:94
int port_exists_by_type_addr_port(int listener_type, const tor_addr_t *addr, int port, int check_wildcard)
Definition: config.c:7763
int options_save_current(void)
Definition: config.c:7982
uint32_t get_effective_bwburst(const or_options_t *options)
Definition: config.c:1749
int set_options(or_options_t *new_val, char **msg)
Definition: config.c:925
STATIC int open_and_add_file_log(const log_severity_list_t *severity, const char *filename, int truncate_log)
Definition: config.c:5658
STATIC void or_options_free_(or_options_t *options)
Definition: config.c:996
const char * get_torrc_fname(int defaults_fname)
Definition: config.c:5554
int options_any_client_port_set(const or_options_t *options)
Definition: config.c:8504
STATIC int parse_dir_authority_line(const char *line, dirinfo_type_t required_type, int validate_only)
Definition: config.c:6357
void init_protocol_warning_severity_level(void)
Definition: config.c:1154
int write_to_data_subdir(const char *subdir, const char *fname, const char *str, const char *descr)
Definition: config.c:8106
void config_register_addressmaps(const or_options_t *options)
Definition: config.c:5568
int check_or_create_data_subdir(const char *subdir)
Definition: config.c:8087