tor  master
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-2017, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
6 
12 #ifndef TOR_CONFIG_H
13 #define TOR_CONFIG_H
14 
15 #include "testsupport.h"
16 
17 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(DARWIN)
18 #define KERNEL_MAY_SUPPORT_IPFW
19 #endif
20 
23 #define MIN_HEARTBEAT_PERIOD (30*60)
24 
26 #if SIZEOF_VOID_P >= 8
27 #define MAX_DEFAULT_MEMORY_QUEUE_SIZE (U64_LITERAL(8) << 30)
28 #else
29 #define MAX_DEFAULT_MEMORY_QUEUE_SIZE (U64_LITERAL(2) << 30)
30 #endif
31 
32 MOCK_DECL(const char*, get_dirportfrontpage, (void));
33 MOCK_DECL(const or_options_t *, get_options, (void));
34 MOCK_DECL(or_options_t *, get_options_mutable, (void));
35 int set_options(or_options_t *new_val, char **msg);
36 void config_free_all(void);
37 const char *safe_str_client(const char *address);
38 const char *safe_str(const char *address);
39 const char *escaped_safe_str_client(const char *address);
40 const char *escaped_safe_str(const char *address);
43 const char *get_version(void);
44 const char *get_short_version(void);
45 setopt_err_t options_trial_assign(config_line_t *list, unsigned flags,
46  char **msg);
47 
48 uint32_t get_last_resolved_addr(void);
49 void reset_last_resolved_addr(void);
50 int resolve_my_address(int warn_severity, const or_options_t *options,
51  uint32_t *addr_out,
52  const char **method_out, char **hostname_out);
53 MOCK_DECL(int, is_local_addr, (const tor_addr_t *addr));
54 void options_init(or_options_t *options);
55 
56 #define OPTIONS_DUMP_MINIMAL 1
57 #define OPTIONS_DUMP_DEFAULTS 2
58 #define OPTIONS_DUMP_ALL 3
59 char *options_dump(const or_options_t *options, int how_to_dump);
60 int options_init_from_torrc(int argc, char **argv);
61 setopt_err_t options_init_from_string(const char *cf_defaults, const char *cf,
62  int command, const char *command_arg, char **msg);
63 int option_is_recognized(const char *key);
64 const char *option_get_canonical_name(const char *key);
66  const char *key);
67 int options_save_current(void);
68 const char *get_torrc_fname(int defaults_fname);
69 typedef enum {
70  DIRROOT_DATADIR,
71  DIRROOT_CACHEDIR,
72  DIRROOT_KEYDIR
73 } directory_root_t;
74 
75 MOCK_DECL(char *,
76  options_get_dir_fname2_suffix,
77  (const or_options_t *options,
78  directory_root_t roottype,
79  const char *sub1, const char *sub2,
80  const char *suffix));
81 
82 /* These macros wrap options_get_dir_fname2_suffix to provide a more
83  * convenient API for finding filenames that Tor uses inside its storage
84  * They are named according to a pattern:
85  * (options_)?get_(cache|key|data)dir_fname(2)?(_suffix)?
86  *
87  * Macros that begin with options_ take an options argument; the others
88  * work with respect to the global options.
89  *
90  * Each macro works relative to the data directory, the key directory,
91  * or the cache directory, as determined by which one is mentioned.
92  *
93  * Macro variants with "2" in their name take two path components; others
94  * take one.
95  *
96  * Macro variants with "_suffix" at the end take an additional suffix
97  * that gets appended to the end of the file
98  */
99 #define options_get_datadir_fname2_suffix(options, sub1, sub2, suffix) \
100  options_get_dir_fname2_suffix((options), DIRROOT_DATADIR, \
101  (sub1), (sub2), (suffix))
102 #define options_get_cachedir_fname2_suffix(options, sub1, sub2, suffix) \
103  options_get_dir_fname2_suffix((options), DIRROOT_CACHEDIR, \
104  (sub1), (sub2), (suffix))
105 #define options_get_keydir_fname2_suffix(options, sub1, sub2, suffix) \
106  options_get_dir_fname2_suffix((options), DIRROOT_KEYDIR, \
107  (sub1), (sub2), (suffix))
108 
109 #define options_get_datadir_fname(opts,sub1) \
110  options_get_datadir_fname2_suffix((opts),(sub1), NULL, NULL)
111 #define options_get_datadir_fname2(opts,sub1,sub2) \
112  options_get_datadir_fname2_suffix((opts),(sub1), (sub2), NULL)
113 
114 #define get_datadir_fname2_suffix(sub1, sub2, suffix) \
115  options_get_datadir_fname2_suffix(get_options(), (sub1), (sub2), (suffix))
116 #define get_datadir_fname(sub1) \
117  get_datadir_fname2_suffix((sub1), NULL, NULL)
118 #define get_datadir_fname2(sub1,sub2) \
119  get_datadir_fname2_suffix((sub1), (sub2), NULL)
120 #define get_datadir_fname_suffix(sub1, suffix) \
121  get_datadir_fname2_suffix((sub1), NULL, (suffix))
122 
124 #define options_get_keydir_fname(options, sub1) \
125  options_get_keydir_fname2_suffix((options), (sub1), NULL, NULL)
126 #define get_keydir_fname_suffix(sub1, suffix) \
127  options_get_keydir_fname2_suffix(get_options(), (sub1), NULL, suffix)
128 #define get_keydir_fname(sub1) \
129  options_get_keydir_fname2_suffix(get_options(), (sub1), NULL, NULL)
130 
131 #define get_cachedir_fname(sub1) \
132  options_get_cachedir_fname2_suffix(get_options(), (sub1), NULL, NULL)
133 #define get_cachedir_fname_suffix(sub1, suffix) \
134  options_get_cachedir_fname2_suffix(get_options(), (sub1), NULL, (suffix))
135 
136 int using_default_dir_authorities(const or_options_t *options);
137 
138 int create_keys_directory(const or_options_t *options);
139 
140 int check_or_create_data_subdir(const char *subdir);
141 int write_to_data_subdir(const char* subdir, const char* fname,
142  const char* str, const char* descr);
143 
144 int get_num_cpus(const or_options_t *options);
145 
146 MOCK_DECL(const smartlist_t *,get_configured_ports,(void));
147 int get_first_advertised_port_by_type_af(int listener_type,
148  int address_family);
149 #define get_primary_or_port() \
150  (get_first_advertised_port_by_type_af(CONN_TYPE_OR_LISTENER, AF_INET))
151 #define get_primary_dir_port() \
152  (get_first_advertised_port_by_type_af(CONN_TYPE_DIR_LISTENER, AF_INET))
153 const tor_addr_t *get_first_advertised_addr_by_type_af(int listener_type,
154  int address_family);
155 int port_exists_by_type_addr_port(int listener_type, const tor_addr_t *addr,
156  int port, int check_wildcard);
157 int port_exists_by_type_addr32h_port(int listener_type, uint32_t addr_ipv4h,
158  int port, int check_wildcard);
159 
160 char *get_first_listener_addrport_string(int listener_type);
161 
162 int options_need_geoip_info(const or_options_t *options,
163  const char **reason_out);
164 
166 
168  const char *question, char **answer,
169  const char **errmsg);
170 
171 uint32_t get_effective_bwrate(const or_options_t *options);
172 uint32_t get_effective_bwburst(const or_options_t *options);
173 
174 char *get_transport_bindaddr_from_config(const char *transport);
175 
176 int init_cookie_authentication(const char *fname, const char *header,
177  int cookie_len, int group_readable,
178  uint8_t **cookie_out, int *cookie_is_set_out);
179 
181 
182 int config_parse_commandline(int argc, char **argv, int ignore_errors,
183  config_line_t **result,
184  config_line_t **cmdline_result);
185 
186 void config_register_addressmaps(const or_options_t *options);
187 /* XXXX move to connection_edge.h */
188 int addressmap_register_auto(const char *from, const char *to,
189  time_t expires,
190  addressmap_entry_source_t addrmap_source,
191  const char **msg);
192 
193 int port_cfg_line_extract_addrport(const char *line,
194  char **addrport_out,
195  int *is_unix_out,
196  const char **rest_out);
197 
199 typedef struct bridge_line_t {
200  tor_addr_t addr; /* The IP address of the bridge. */
201  uint16_t port; /* The TCP port of the bridge. */
202  char *transport_name; /* The name of the pluggable transport that
203  should be used to connect to the bridge. */
204  char digest[DIGEST_LEN]; /* The bridge's identity key digest. */
205  smartlist_t *socks_args; /* SOCKS arguments for the pluggable
206  transport proxy. */
207 } bridge_line_t;
208 
209 void bridge_line_free_(bridge_line_t *bridge_line);
210 #define bridge_line_free(line) \
211  FREE_AND_NULL(bridge_line_t, bridge_line_free_, (line))
212 bridge_line_t *parse_bridge_line(const char *line);
214  const char *transport);
215 smartlist_t *get_options_for_server_transport(const char *transport);
216 
217 /* Port helper functions. */
218 int options_any_client_port_set(const or_options_t *options);
219 
220 #ifdef CONFIG_PRIVATE
221 
222 #define CL_PORT_NO_STREAM_OPTIONS (1u<<0)
223 #define CL_PORT_WARN_NONLOCAL (1u<<1)
224 /* Was CL_PORT_ALLOW_EXTRA_LISTENADDR (1u<<2) */
225 #define CL_PORT_SERVER_OPTIONS (1u<<3)
226 #define CL_PORT_FORBID_NONLOCAL (1u<<4)
227 #define CL_PORT_TAKES_HOSTNAMES (1u<<5)
228 #define CL_PORT_IS_UNIXSOCKET (1u<<6)
229 #define CL_PORT_DFLT_GROUP_WRITABLE (1u<<7)
230 
231 STATIC int options_act(const or_options_t *old_options);
232 #ifdef TOR_UNIT_TESTS
233 extern struct config_format_t options_format;
234 #endif
235 
236 STATIC port_cfg_t *port_cfg_new(size_t namelen);
237 #define port_cfg_free(port) \
238  FREE_AND_NULL(port_cfg_t, port_cfg_free_, (port))
239 STATIC void port_cfg_free_(port_cfg_t *port);
240 #define or_options_free(opt) \
241  FREE_AND_NULL(or_options_t, or_options_free_, (opt))
242 STATIC void or_options_free_(or_options_t *options);
243 STATIC int options_validate_single_onion(or_options_t *options,
244  char **msg);
245 STATIC int options_validate(or_options_t *old_options,
246  or_options_t *options,
247  or_options_t *default_options,
248  int from_setconf, char **msg);
249 STATIC int parse_transport_line(const or_options_t *options,
250  const char *line, int validate_only,
251  int server);
252 STATIC int consider_adding_dir_servers(const or_options_t *options,
253  const or_options_t *old_options);
255 MOCK_DECL(STATIC void, add_default_fallback_dir_servers, (void));
256 STATIC int parse_dir_authority_line(const char *line,
257  dirinfo_type_t required_type,
258  int validate_only);
259 STATIC int parse_dir_fallback_line(const char *line, int validate_only);
260 STATIC int have_enough_mem_for_dircache(const or_options_t *options,
261  size_t total_mem, char **msg);
262 STATIC int parse_port_config(smartlist_t *out,
263  const config_line_t *ports,
264  const char *portname,
265  int listener_type,
266  const char *defaultaddr,
267  int defaultport,
268  const unsigned flags);
269 
270 STATIC int check_bridge_distribution_setting(const char *bd);
271 
272 STATIC uint64_t compute_real_max_mem_in_queues(const uint64_t val,
273  int log_guess);
274 
275 #endif /* defined(CONFIG_PRIVATE) */
276 
277 #endif /* !defined(TOR_CONFIG_H) */
278 
smartlist_t * get_options_for_server_transport(const char *transport)
Definition: config.c:6300
const char * escaped_safe_str_client(const char *address)
Definition: config.c:1065
void bridge_line_free_(bridge_line_t *bridge_line)
Definition: config.c:5827
char * get_transport_bindaddr_from_config(const char *transport)
Definition: config.c:6280
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:2733
Definition: confline.h:23
Definition: or.h:3657
#define DIGEST_LEN
Definition: crypto_digest.h:22
or_options_t * options_new(void)
Definition: config.c:2957
int getinfo_helper_config(control_connection_t *conn, const char *question, char **answer, const char **errmsg)
Definition: config.c:8118
Definition: address.h:56
Definition: confparse.h:153
int options_init_from_torrc(int argc, char **argv)
Definition: config.c:5152
int options_need_geoip_info(const or_options_t *options, const char **reason_out)
Definition: config.c:1664
STATIC void port_cfg_free_(port_cfg_t *port)
Definition: config.c:6593
Definition: config.h:199
setopt_err_t
Definition: or.h:4963
int get_protocol_warning_severity_level(void)
Definition: config.c:1098
Definition: or.h:3624
STATIC int parse_transport_line(const or_options_t *options, const char *line, int validate_only, int server)
Definition: config.c:5971
MOCK_DECL(int, router_have_minimum_dir_info,(void))
const char * safe_str_client(const char *address)
Definition: config.c:1035
addressmap_entry_source_t
Definition: or.h:4974
int create_keys_directory(const or_options_t *options)
Definition: config.c:1340
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:6839
Definition: container.h:18
dirinfo_type_t
Definition: or.h:2879
smartlist_t * get_options_from_transport_options_line(const char *line, const char *transport)
Definition: config.c:6227
void reset_last_resolved_addr(void)
Definition: config.c:2671
STATIC port_cfg_t * port_cfg_new(size_t namelen)
Definition: config.c:6579
const char * option_get_canonical_name(const char *key)
Definition: config.c:2555
char * options_dump(const or_options_t *options, int how_to_dump)
Definition: config.c:2975
const char * safe_str(const char *address)
Definition: config.c:1052
STATIC int check_bridge_distribution_setting(const char *bd)
Definition: config.c:6780
int config_parse_commandline(int argc, char **argv, int ignore_errors, config_line_t **result, config_line_t **cmdline_result)
Definition: config.c:2454
STATIC void add_default_trusted_dir_authorities(dirinfo_type_t type)
Definition: config.c:1151
int port_cfg_line_extract_addrport(const char *line, char **addrport_out, int *is_unix_out, const char **rest_out)
Definition: config.c:6700
uint32_t get_last_resolved_addr(void)
Definition: config.c:2664
uint32_t get_effective_bwrate(const or_options_t *options)
Definition: config.c:1691
STATIC int have_enough_mem_for_dircache(const or_options_t *options, size_t total_mem, char **msg)
Definition: config.c:4651
int get_first_advertised_port_by_type_af(int listener_type, int address_family)
Definition: config.c:7672
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:8385
int option_is_recognized(const char *key)
Definition: config.c:2546
smartlist_t * get_list_of_ports_to_forward(void)
Definition: config.c:8080
void config_free_all(void)
Definition: config.c:995
const char * get_version(void)
Definition: config.c:932
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:5597
setopt_err_t options_trial_assign(config_line_t *list, unsigned flags, char **msg)
Definition: config.c:2579
struct bridge_line_t bridge_line_t
STATIC int options_act(const or_options_t *old_options)
Definition: config.c:1792
Definition: or.h:1885
config_line_t * option_get_assignment(const or_options_t *options, const char *key)
Definition: config.c:2564
bridge_line_t * parse_bridge_line(const char *line)
Definition: config.c:5853
int consider_adding_dir_servers(const or_options_t *options, const or_options_t *old_options)
Definition: config.c:1232
void options_init(or_options_t *options)
Definition: config.c:2965
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:3331
const char * escaped_safe_str(const char *address)
Definition: config.c:1077
int get_num_cpus(const or_options_t *options)
Definition: config.c:7951
char * get_first_listener_addrport_string(int listener_type)
Definition: config.c:7618
const tor_addr_t * get_first_advertised_addr_by_type_af(int listener_type, int address_family)
Definition: config.c:7694
int parse_dir_fallback_line(const char *line, int validate_only)
Definition: config.c:6473
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:5359
int port_exists_by_type_addr_port(int listener_type, const tor_addr_t *addr, int port, int check_wildcard)
Definition: config.c:7719
int options_save_current(void)
Definition: config.c:7938
const char * get_short_version(void)
Definition: config.c:947
uint32_t get_effective_bwburst(const or_options_t *options)
Definition: config.c:1705
int set_options(or_options_t *new_val, char **msg)
Definition: config.c:857
STATIC void or_options_free_(or_options_t *options)
Definition: config.c:963
const char * get_torrc_fname(int defaults_fname)
Definition: config.c:5537
int options_any_client_port_set(const or_options_t *options)
Definition: config.c:8444
STATIC int parse_dir_authority_line(const char *line, dirinfo_type_t required_type, int validate_only)
Definition: config.c:6323
void init_protocol_warning_severity_level(void)
Definition: config.c:1116
int write_to_data_subdir(const char *subdir, const char *fname, const char *str, const char *descr)
Definition: config.c:8062
void config_register_addressmaps(const or_options_t *options)
Definition: config.c:5551
int check_or_create_data_subdir(const char *subdir)
Definition: config.c:8043