tor  0.4.0.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-2018, 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 int using_default_dir_authorities(const or_options_t *options);
145 
146 int create_keys_directory(const or_options_t *options);
147 
148 int check_or_create_data_subdir(const char *subdir);
149 int write_to_data_subdir(const char* subdir, const char* fname,
150  const char* str, const char* descr);
151 
152 int get_num_cpus(const or_options_t *options);
153 
154 MOCK_DECL(const smartlist_t *,get_configured_ports,(void));
155 int get_first_advertised_port_by_type_af(int listener_type,
156  int address_family);
157 #define get_primary_or_port() \
158  (get_first_advertised_port_by_type_af(CONN_TYPE_OR_LISTENER, AF_INET))
159 #define get_primary_dir_port() \
160  (get_first_advertised_port_by_type_af(CONN_TYPE_DIR_LISTENER, AF_INET))
161 const tor_addr_t *get_first_advertised_addr_by_type_af(int listener_type,
162  int address_family);
163 int port_exists_by_type_addr_port(int listener_type, const tor_addr_t *addr,
164  int port, int check_wildcard);
165 int port_exists_by_type_addr32h_port(int listener_type, uint32_t addr_ipv4h,
166  int port, int check_wildcard);
167 
168 char *get_first_listener_addrport_string(int listener_type);
169 
170 int options_need_geoip_info(const or_options_t *options,
171  const char **reason_out);
172 
174 
176  const char *question, char **answer,
177  const char **errmsg);
178 
179 uint32_t get_effective_bwrate(const or_options_t *options);
180 uint32_t get_effective_bwburst(const or_options_t *options);
181 
182 char *get_transport_bindaddr_from_config(const char *transport);
183 
184 int init_cookie_authentication(const char *fname, const char *header,
185  int cookie_len, int group_readable,
186  uint8_t **cookie_out, int *cookie_is_set_out);
187 
189 
190 int config_parse_commandline(int argc, char **argv, int ignore_errors,
191  struct config_line_t **result,
192  struct config_line_t **cmdline_result);
193 
194 void config_register_addressmaps(const or_options_t *options);
195 /* XXXX move to connection_edge.h */
196 int addressmap_register_auto(const char *from, const char *to,
197  time_t expires,
198  addressmap_entry_source_t addrmap_source,
199  const char **msg);
200 
201 int port_cfg_line_extract_addrport(const char *line,
202  char **addrport_out,
203  int *is_unix_out,
204  const char **rest_out);
205 
207 typedef struct bridge_line_t {
208  tor_addr_t addr; /* The IP address of the bridge. */
209  uint16_t port; /* The TCP port of the bridge. */
210  char *transport_name; /* The name of the pluggable transport that
211  should be used to connect to the bridge. */
212  char digest[DIGEST_LEN]; /* The bridge's identity key digest. */
213  smartlist_t *socks_args; /* SOCKS arguments for the pluggable
214  transport proxy. */
215 } bridge_line_t;
216 
217 void bridge_line_free_(bridge_line_t *bridge_line);
218 #define bridge_line_free(line) \
219  FREE_AND_NULL(bridge_line_t, bridge_line_free_, (line))
220 bridge_line_t *parse_bridge_line(const char *line);
222  const char *transport);
223 smartlist_t *get_options_for_server_transport(const char *transport);
224 
225 /* Port helper functions. */
226 int options_any_client_port_set(const or_options_t *options);
227 
228 #ifdef CONFIG_PRIVATE
229 
230 #define CL_PORT_NO_STREAM_OPTIONS (1u<<0)
231 #define CL_PORT_WARN_NONLOCAL (1u<<1)
232 /* Was CL_PORT_ALLOW_EXTRA_LISTENADDR (1u<<2) */
233 #define CL_PORT_SERVER_OPTIONS (1u<<3)
234 #define CL_PORT_FORBID_NONLOCAL (1u<<4)
235 #define CL_PORT_TAKES_HOSTNAMES (1u<<5)
236 #define CL_PORT_IS_UNIXSOCKET (1u<<6)
237 #define CL_PORT_DFLT_GROUP_WRITABLE (1u<<7)
238 
239 STATIC int options_act(const or_options_t *old_options);
240 #ifdef TOR_UNIT_TESTS
241 extern struct config_format_t options_format;
242 #endif
243 
244 STATIC port_cfg_t *port_cfg_new(size_t namelen);
245 #define port_cfg_free(port) \
246  FREE_AND_NULL(port_cfg_t, port_cfg_free_, (port))
247 STATIC void port_cfg_free_(port_cfg_t *port);
248 #define or_options_free(opt) \
249  FREE_AND_NULL(or_options_t, or_options_free_, (opt))
250 STATIC void or_options_free_(or_options_t *options);
251 STATIC int options_validate_single_onion(or_options_t *options,
252  char **msg);
253 STATIC int options_validate(or_options_t *old_options,
254  or_options_t *options,
255  or_options_t *default_options,
256  int from_setconf, char **msg);
257 STATIC int parse_transport_line(const or_options_t *options,
258  const char *line, int validate_only,
259  int server);
260 STATIC int consider_adding_dir_servers(const or_options_t *options,
261  const or_options_t *old_options);
263 MOCK_DECL(STATIC void, add_default_fallback_dir_servers, (void));
264 STATIC int parse_dir_authority_line(const char *line,
265  dirinfo_type_t required_type,
266  int validate_only);
267 STATIC int parse_dir_fallback_line(const char *line, int validate_only);
268 STATIC int have_enough_mem_for_dircache(const or_options_t *options,
269  size_t total_mem, char **msg);
270 STATIC int parse_port_config(smartlist_t *out,
271  const struct config_line_t *ports,
272  const char *portname,
273  int listener_type,
274  const char *defaultaddr,
275  int defaultport,
276  const unsigned flags);
277 
278 STATIC int check_bridge_distribution_setting(const char *bd);
279 
280 STATIC uint64_t compute_real_max_mem_in_queues(const uint64_t val,
281  int log_guess);
282 STATIC int open_and_add_file_log(const log_severity_list_t *severity,
283  const char *fname,
284  int truncate_log);
285 
286 #endif /* defined(CONFIG_PRIVATE) */
287 
288 #endif /* !defined(TOR_CONFIG_H) */
smartlist_t * get_options_for_server_transport(const char *transport)
Definition: config.c:6298
const char * escaped_safe_str_client(const char *address)
Definition: config.c:1089
void bridge_line_free_(bridge_line_t *bridge_line)
Definition: config.c:5825
char * get_transport_bindaddr_from_config(const char *transport)
Definition: config.c:6278
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
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:2454
or_options_t * options_new(void)
Definition: config.c:2957
struct config_line_t * option_get_assignment(const or_options_t *options, const char *key)
Definition: config.c:2564
int getinfo_helper_config(control_connection_t *conn, const char *question, char **answer, const char **errmsg)
Definition: config.c:8126
int options_init_from_torrc(int argc, char **argv)
Definition: config.c:5126
int options_need_geoip_info(const or_options_t *options, const char **reason_out)
Definition: config.c:1694
STATIC void port_cfg_free_(port_cfg_t *port)
Definition: config.c:6599
int get_protocol_warning_severity_level(void)
Definition: config.c:1122
STATIC int parse_transport_line(const or_options_t *options, const char *line, int validate_only, int server)
Definition: config.c:5969
The or_options_t structure, which represents Tor&#39;s configuration.
setopt_err_t options_trial_assign(struct config_line_t *list, unsigned flags, char **msg)
Definition: config.c:2579
const char * safe_str_client(const char *address)
Definition: config.c:1059
addressmap_entry_source_t
Definition: or.h:1019
int create_keys_directory(const or_options_t *options)
Definition: config.c:1364
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:6845
STATIC config_format_t options_format
Definition: config.c:850
dirinfo_type_t
Definition: or.h:887
smartlist_t * get_options_from_transport_options_line(const char *line, const char *transport)
Definition: config.c:6225
void reset_last_resolved_addr(void)
Definition: config.c:2671
STATIC port_cfg_t * port_cfg_new(size_t namelen)
Definition: config.c:6585
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:1076
STATIC int check_bridge_distribution_setting(const char *bd)
Definition: config.c:6786
STATIC void add_default_trusted_dir_authorities(dirinfo_type_t type)
Definition: config.c:1175
int port_cfg_line_extract_addrport(const char *line, char **addrport_out, int *is_unix_out, const char **rest_out)
Definition: config.c:6706
#define DIGEST_LEN
Definition: digest_sizes.h:20
uint32_t get_last_resolved_addr(void)
Definition: config.c:2664
uint32_t get_effective_bwrate(const or_options_t *options)
Definition: config.c:1721
STATIC int have_enough_mem_for_dircache(const or_options_t *options, size_t total_mem, char **msg)
Definition: config.c:4626
int get_first_advertised_port_by_type_af(int listener_type, int address_family)
Definition: config.c:7680
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:8409
int option_is_recognized(const char *key)
Definition: config.c:2546
setopt_err_t
Definition: config.h:46
smartlist_t * get_list_of_ports_to_forward(void)
Definition: config.c:8088
void config_free_all(void)
Definition: config.c:1022
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:5578
struct bridge_line_t bridge_line_t
STATIC int options_act(const or_options_t *old_options)
Definition: config.c:1822
bridge_line_t * parse_bridge_line(const char *line)
Definition: config.c:5851
int consider_adding_dir_servers(const or_options_t *options, const or_options_t *old_options)
Definition: config.c:1256
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:3329
const char * escaped_safe_str(const char *address)
Definition: config.c:1101
int get_num_cpus(const or_options_t *options)
Definition: config.c:7959
char * get_first_listener_addrport_string(int listener_type)
Definition: config.c:7626
const tor_addr_t * get_first_advertised_addr_by_type_af(int listener_type, int address_family)
Definition: config.c:7702
int parse_dir_fallback_line(const char *line, int validate_only)
Definition: config.c:6479
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:5340
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:7727
int options_save_current(void)
Definition: config.c:7946
uint32_t get_effective_bwburst(const or_options_t *options)
Definition: config.c:1735
int set_options(or_options_t *new_val, char **msg)
Definition: config.c:919
STATIC int open_and_add_file_log(const log_severity_list_t *severity, const char *filename, int truncate_log)
Definition: config.c:5622
STATIC void or_options_free_(or_options_t *options)
Definition: config.c:990
const char * get_torrc_fname(int defaults_fname)
Definition: config.c:5518
int options_any_client_port_set(const or_options_t *options)
Definition: config.c:8468
STATIC int parse_dir_authority_line(const char *line, dirinfo_type_t required_type, int validate_only)
Definition: config.c:6321
void init_protocol_warning_severity_level(void)
Definition: config.c:1140
int write_to_data_subdir(const char *subdir, const char *fname, const char *str, const char *descr)
Definition: config.c:8070
void config_register_addressmaps(const or_options_t *options)
Definition: config.c:5532
int check_or_create_data_subdir(const char *subdir)
Definition: config.c:8051