Tor  0.4.5.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-2020, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
6 
7 /**
8  * \file config.h
9  * \brief Header file for config.c.
10  **/
11 
12 #ifndef TOR_CONFIG_H
13 #define TOR_CONFIG_H
14 
17 #include "app/config/quiet_level.h"
18 
19 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(DARWIN)
20 #define KERNEL_MAY_SUPPORT_IPFW
21 #endif
22 
23 /** Lowest allowable value for HeartbeatPeriod; if this is too low, we might
24  * expose more information than we're comfortable with. */
25 #define MIN_HEARTBEAT_PERIOD (30*60)
26 
27 /** Maximum default value for MaxMemInQueues, in bytes. */
28 #if SIZEOF_VOID_P >= 8
29 #define MAX_DEFAULT_MEMORY_QUEUE_SIZE (UINT64_C(8) << 30)
30 #else
31 #define MAX_DEFAULT_MEMORY_QUEUE_SIZE (UINT64_C(2) << 30)
32 #endif
33 
34 MOCK_DECL(const or_options_t *, get_options, (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 
45 #define LOG_PROTOCOL_WARN (get_protocol_warning_severity_level())
46 
47 /** An error from options_trial_assign() or options_init_from_string(). */
48 typedef enum setopt_err_t {
49  SETOPT_OK = 0,
50  SETOPT_ERR_MISC = -1,
51  SETOPT_ERR_PARSE = -2,
52  SETOPT_ERR_TRANSITION = -3,
53  SETOPT_ERR_SETTING = -4,
54 } setopt_err_t;
55 setopt_err_t options_trial_assign(struct config_line_t *list, unsigned flags,
56  char **msg);
57 
58 void options_init(or_options_t *options);
59 
60 #define OPTIONS_DUMP_MINIMAL 1
61 #define OPTIONS_DUMP_ALL 2
62 char *options_dump(const or_options_t *options, int how_to_dump);
63 int options_init_from_torrc(int argc, char **argv);
64 setopt_err_t options_init_from_string(const char *cf_defaults, const char *cf,
65  int command, const char *command_arg, char **msg);
66 int option_is_recognized(const char *key);
67 const char *option_get_canonical_name(const char *key);
68 struct config_line_t *option_get_assignment(const or_options_t *options,
69  const char *key);
70 int options_save_current(void);
71 const char *get_torrc_fname(int defaults_fname);
72 typedef enum {
73  DIRROOT_DATADIR,
74  DIRROOT_CACHEDIR,
75  DIRROOT_KEYDIR
76 } directory_root_t;
77 
78 MOCK_DECL(char *,
80  (const or_options_t *options,
81  directory_root_t roottype,
82  const char *sub1, const char *sub2,
83  const char *suffix));
84 
85 /* These macros wrap options_get_dir_fname2_suffix to provide a more
86  * convenient API for finding filenames that Tor uses inside its storage
87  * They are named according to a pattern:
88  * (options_)?get_(cache|key|data)dir_fname(2)?(_suffix)?
89  *
90  * Macros that begin with options_ take an options argument; the others
91  * work with respect to the global options.
92  *
93  * Each macro works relative to the data directory, the key directory,
94  * or the cache directory, as determined by which one is mentioned.
95  *
96  * Macro variants with "2" in their name take two path components; others
97  * take one.
98  *
99  * Macro variants with "_suffix" at the end take an additional suffix
100  * that gets appended to the end of the file
101  */
102 #define options_get_datadir_fname2_suffix(options, sub1, sub2, suffix) \
103  options_get_dir_fname2_suffix((options), DIRROOT_DATADIR, \
104  (sub1), (sub2), (suffix))
105 #define options_get_cachedir_fname2_suffix(options, sub1, sub2, suffix) \
106  options_get_dir_fname2_suffix((options), DIRROOT_CACHEDIR, \
107  (sub1), (sub2), (suffix))
108 #define options_get_keydir_fname2_suffix(options, sub1, sub2, suffix) \
109  options_get_dir_fname2_suffix((options), DIRROOT_KEYDIR, \
110  (sub1), (sub2), (suffix))
111 
112 #define options_get_datadir_fname(opts,sub1) \
113  options_get_datadir_fname2_suffix((opts),(sub1), NULL, NULL)
114 #define options_get_datadir_fname2(opts,sub1,sub2) \
115  options_get_datadir_fname2_suffix((opts),(sub1), (sub2), NULL)
116 
117 #define get_datadir_fname2_suffix(sub1, sub2, suffix) \
118  options_get_datadir_fname2_suffix(get_options(), (sub1), (sub2), (suffix))
119 #define get_datadir_fname(sub1) \
120  get_datadir_fname2_suffix((sub1), NULL, NULL)
121 #define get_datadir_fname2(sub1,sub2) \
122  get_datadir_fname2_suffix((sub1), (sub2), NULL)
123 #define get_datadir_fname_suffix(sub1, suffix) \
124  get_datadir_fname2_suffix((sub1), NULL, (suffix))
125 
126 /** DOCDOC */
127 #define options_get_keydir_fname(options, sub1) \
128  options_get_keydir_fname2_suffix((options), (sub1), NULL, NULL)
129 #define get_keydir_fname_suffix(sub1, suffix) \
130  options_get_keydir_fname2_suffix(get_options(), (sub1), NULL, suffix)
131 #define get_keydir_fname(sub1) \
132  options_get_keydir_fname2_suffix(get_options(), (sub1), NULL, NULL)
133 
134 #define get_cachedir_fname(sub1) \
135  options_get_cachedir_fname2_suffix(get_options(), (sub1), NULL, NULL)
136 #define get_cachedir_fname_suffix(sub1, suffix) \
137  options_get_cachedir_fname2_suffix(get_options(), (sub1), NULL, (suffix))
138 
139 #define safe_str_client(address) \
140  safe_str_client_opts(NULL, address)
141 #define safe_str(address) \
142  safe_str_opts(NULL, address)
143 
144 const char * safe_str_client_opts(const or_options_t *options,
145  const char *address);
146 const char * safe_str_opts(const or_options_t *options,
147  const char *address);
148 
149 int using_default_dir_authorities(const or_options_t *options);
150 
151 int create_keys_directory(const or_options_t *options);
152 
153 int check_or_create_data_subdir(const char *subdir);
154 int write_to_data_subdir(const char* subdir, const char* fname,
155  const char* str, const char* descr);
156 
157 int get_num_cpus(const or_options_t *options);
158 
160 int port_binds_ipv4(const port_cfg_t *port);
161 int port_binds_ipv6(const port_cfg_t *port);
162 int portconf_get_first_advertised_port(int listener_type,
163  int address_family);
164 #define portconf_get_primary_dir_port() \
165  (portconf_get_first_advertised_port(CONN_TYPE_DIR_LISTENER, AF_INET))
166 const tor_addr_t *portconf_get_first_advertised_addr(int listener_type,
167  int address_family);
168 int port_exists_by_type_addr_port(int listener_type, const tor_addr_t *addr,
169  int port, int check_wildcard);
170 int port_exists_by_type_addr32h_port(int listener_type, uint32_t addr_ipv4h,
171  int port, int check_wildcard);
172 
173 char *get_first_listener_addrport_string(int listener_type);
174 
175 int options_need_geoip_info(const or_options_t *options,
176  const char **reason_out);
177 
179  const char *question, char **answer,
180  const char **errmsg);
181 
182 int init_cookie_authentication(const char *fname, const char *header,
183  int cookie_len, int group_readable,
184  uint8_t **cookie_out, int *cookie_is_set_out);
185 
187 
188 /** Options settings parsed from the command-line. */
189 typedef struct {
190  /** List of options that can only be set from the command-line */
192  /** List of other options, to be handled by the general Tor configuration
193  system. */
195  /** Subcommand that Tor has been told to run */
197  /** Argument for the command mode, if any. */
198  const char *command_arg;
199  /** How quiet have we been told to be? */
202 
203 parsed_cmdline_t *config_parse_commandline(int argc, char **argv,
204  int ignore_errors);
206 #define parsed_cmdline_free(c) \
207  FREE_AND_NULL(parsed_cmdline_t, parsed_cmdline_free_, (c))
208 
209 void config_register_addressmaps(const or_options_t *options);
210 /* XXXX move to connection_edge.h */
211 int addressmap_register_auto(const char *from, const char *to,
212  time_t expires,
213  addressmap_entry_source_t addrmap_source,
214  const char **msg);
215 
216 int port_cfg_line_extract_addrport(const char *line,
217  char **addrport_out,
218  int *is_unix_out,
219  const char **rest_out);
220 
221 /** Represents the information stored in a torrc Bridge line. */
222 typedef struct bridge_line_t {
223  tor_addr_t addr; /* The IP address of the bridge. */
224  uint16_t port; /* The TCP port of the bridge. */
225  char *transport_name; /* The name of the pluggable transport that
226  should be used to connect to the bridge. */
227  char digest[DIGEST_LEN]; /* The bridge's identity key digest. */
228  smartlist_t *socks_args; /* SOCKS arguments for the pluggable
229  transport proxy. */
230 } bridge_line_t;
231 
232 void bridge_line_free_(bridge_line_t *bridge_line);
233 #define bridge_line_free(line) \
234  FREE_AND_NULL(bridge_line_t, bridge_line_free_, (line))
235 bridge_line_t *parse_bridge_line(const char *line);
236 
237 /* Port helper functions. */
238 int options_any_client_port_set(const or_options_t *options);
240  const struct config_line_t *ports,
241  const char *portname,
242  int listener_type,
243  const char *defaultaddr,
244  int defaultport,
245  const unsigned flags);
246 
247 #define CL_PORT_NO_STREAM_OPTIONS (1u<<0)
248 #define CL_PORT_WARN_NONLOCAL (1u<<1)
249 /* Was CL_PORT_ALLOW_EXTRA_LISTENADDR (1u<<2) */
250 #define CL_PORT_SERVER_OPTIONS (1u<<3)
251 #define CL_PORT_FORBID_NONLOCAL (1u<<4)
252 #define CL_PORT_TAKES_HOSTNAMES (1u<<5)
253 #define CL_PORT_IS_UNIXSOCKET (1u<<6)
254 #define CL_PORT_DFLT_GROUP_WRITABLE (1u<<7)
255 
256 port_cfg_t *port_cfg_new(size_t namelen);
257 #define port_cfg_free(port) \
258  FREE_AND_NULL(port_cfg_t, port_cfg_free_, (port))
259 void port_cfg_free_(port_cfg_t *port);
260 
261 int port_count_real_listeners(const smartlist_t *ports,
262  int listenertype,
263  int count_sockets);
264 int pt_parse_transport_line(const or_options_t *options,
265  const char *line, int validate_only,
266  int server);
267 int config_ensure_bandwidth_cap(uint64_t *value, const char *desc, char **msg);
268 
269 #ifdef CONFIG_PRIVATE
270 
271 MOCK_DECL(STATIC int, options_act,(const or_options_t *old_options));
272 MOCK_DECL(STATIC int, options_act_reversible,(const or_options_t *old_options,
273  char **msg));
274 struct config_mgr_t;
275 STATIC const struct config_mgr_t *get_options_mgr(void);
276 
277 #define or_options_free(opt) \
278  FREE_AND_NULL(or_options_t, or_options_free_, (opt))
279 STATIC void or_options_free_(or_options_t *options);
280 STATIC int options_validate_single_onion(or_options_t *options,
281  char **msg);
282 STATIC int parse_tcp_proxy_line(const char *line, or_options_t *options,
283  char **msg);
285  const or_options_t *old_options);
288 STATIC int parse_dir_authority_line(const char *line,
289  dirinfo_type_t required_type,
290  int validate_only);
291 STATIC int parse_dir_fallback_line(const char *line, int validate_only);
292 
293 STATIC uint64_t compute_real_max_mem_in_queues(const uint64_t val,
294  int log_guess);
296  const char *fname,
297  int truncate_log);
298 STATIC int options_init_logs(const or_options_t *old_options,
299  const or_options_t *options, int validate_only);
300 
301 STATIC int options_create_directories(char **msg_out);
302 struct log_transaction_t;
304  const or_options_t *old_options,
305  char **msg_out);
308 
309 #ifdef TOR_UNIT_TESTS
310 int options_validate(const or_options_t *old_options,
311  or_options_t *options,
312  char **msg);
313 #endif
314 
315 STATIC int parse_ports(or_options_t *options, int validate_only,
316  char **msg, int *n_ports_out,
317  int *world_writable_control_socket);
318 
319 #endif /* defined(CONFIG_PRIVATE) */
320 
321 #endif /* !defined(TOR_CONFIG_H) */
parse_bridge_line
bridge_line_t * parse_bridge_line(const char *line)
Definition: config.c:4999
options_create_directories
STATIC int options_create_directories(char **msg_out)
Definition: config.c:1527
tor_addr_t
Definition: address.h:69
or_options_st.h
The or_options_t structure, which represents Tor's configuration.
addressmap_entry_source_t
addressmap_entry_source_t
Definition: or.h:1035
parsed_cmdline_t::other_opts
struct config_line_t * other_opts
Definition: config.h:194
parse_tcp_proxy_line
STATIC int parse_tcp_proxy_line(const char *line, or_options_t *options, char **msg)
Definition: config.c:5115
options_dump
char * options_dump(const or_options_t *options, int how_to_dump)
Definition: config.c:2770
config_mgr_t
Definition: confmgt.c:107
options_init_from_string
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:4535
log_transaction_t
Definition: config.c:1756
get_configured_ports
const smartlist_t * get_configured_ports(void)
Definition: config.c:6501
add_default_trusted_dir_authorities
STATIC void add_default_trusted_dir_authorities(dirinfo_type_t type)
Definition: config.c:1194
port_count_real_listeners
int port_count_real_listeners(const smartlist_t *ports, int listenertype, int count_sockets)
Definition: config.c:6319
set_options
int set_options(or_options_t *new_val, char **msg)
Definition: config.c:964
options_save_current
int options_save_current(void)
Definition: config.c:6839
write_to_data_subdir
int write_to_data_subdir(const char *subdir, const char *fname, const char *str, const char *descr)
Definition: config.c:6963
options_rollback_log_transaction
STATIC void options_rollback_log_transaction(log_transaction_t *xn)
Definition: config.c:1870
quiet_level.h
Declare the quiet_level enumeration and global.
options_start_log_transaction
STATIC log_transaction_t * options_start_log_transaction(const or_options_t *old_options, char **msg_out)
Definition: config.c:1781
parsed_cmdline_t::command_arg
const char * command_arg
Definition: config.h:198
get_torrc_fname
const char * get_torrc_fname(int defaults_fname)
Definition: config.c:4645
bridge_line_free_
void bridge_line_free_(bridge_line_t *bridge_line)
Definition: config.c:4973
options_init_logs
STATIC int options_init_logs(const or_options_t *old_options, const or_options_t *options, int validate_only)
Definition: config.c:4807
check_or_create_data_subdir
int check_or_create_data_subdir(const char *subdir)
Definition: config.c:6944
DIGEST_LEN
#define DIGEST_LEN
Definition: digest_sizes.h:20
pt_parse_transport_line
int pt_parse_transport_line(const or_options_t *options, const char *line, int validate_only, int server)
Definition: config.c:5178
get_options_mgr
STATIC const config_mgr_t * get_options_mgr(void)
Definition: config.c:902
options_act
STATIC int options_act(const or_options_t *old_options)
Definition: config.c:2044
escaped_safe_str_client
const char * escaped_safe_str_client(const char *address)
Definition: config.c:1119
consider_adding_dir_servers
int consider_adding_dir_servers(const or_options_t *options, const or_options_t *old_options)
Definition: config.c:1276
parse_dir_fallback_line
int parse_dir_fallback_line(const char *line, int validate_only)
Definition: config.c:5538
options_init_from_torrc
int options_init_from_torrc(int argc, char **argv)
Definition: config.c:4293
escaped_safe_str
const char * escaped_safe_str(const char *address)
Definition: config.c:1131
port_cfg_new
port_cfg_t * port_cfg_new(size_t namelen)
Definition: config.c:5648
get_first_listener_addrport_string
char * get_first_listener_addrport_string(int listener_type)
Definition: config.c:6518
getinfo_helper_config
int getinfo_helper_config(control_connection_t *conn, const char *question, char **answer, const char **errmsg)
Definition: config.c:6982
get_protocol_warning_severity_level
int get_protocol_warning_severity_level(void)
Definition: config.c:1152
port_cfg_t
Definition: port_cfg_st.h:19
bridge_line_t
Definition: config.h:222
create_keys_directory
int create_keys_directory(const or_options_t *options)
Definition: config.c:1384
options_trial_assign
setopt_err_t options_trial_assign(struct config_line_t *list, unsigned flags, char **msg)
Definition: config.c:2661
MOCK_DECL
#define MOCK_DECL(rv, funcname, arglist)
Definition: testsupport.h:127
add_default_fallback_dir_servers
void add_default_fallback_dir_servers(void)
Definition: config.c:1208
options_any_client_port_set
int options_any_client_port_set(const or_options_t *options)
Definition: config.c:7300
parsed_cmdline_free_
void parsed_cmdline_free_(parsed_cmdline_t *cmdline)
Definition: config.c:2619
command
tor_cmdline_mode_t command
Definition: config.c:2447
port_exists_by_type_addr_port
int port_exists_by_type_addr_port(int listener_type, const tor_addr_t *addr, int port, int check_wildcard)
Definition: config.c:6620
options_commit_log_transaction
STATIC void options_commit_log_transaction(log_transaction_t *xn)
Definition: config.c:1816
parsed_cmdline_t::quiet_level
quiet_level_t quiet_level
Definition: config.h:200
open_and_add_file_log
STATIC int open_and_add_file_log(const log_severity_list_t *severity, const char *filename, int truncate_log)
Definition: config.c:4749
or_options_free_
STATIC void or_options_free_(or_options_t *options)
Definition: config.c:1040
get_options
const or_options_t * get_options(void)
Definition: config.c:928
options_get_dir_fname2_suffix
char * options_get_dir_fname2_suffix(const or_options_t *options, directory_root_t roottype, const char *sub1, const char *sub2, const char *suffix)
Definition: config.c:6901
parse_dir_authority_line
STATIC int parse_dir_authority_line(const char *line, dirinfo_type_t required_type, int validate_only)
Definition: config.c:5380
tor_cmdline_mode_t
tor_cmdline_mode_t
Definition: tor_cmdline_mode.h:19
config_ensure_bandwidth_cap
int config_ensure_bandwidth_cap(uint64_t *value, const char *desc, char **msg)
Definition: config.c:2819
init_protocol_warning_severity_level
void init_protocol_warning_severity_level(void)
Definition: config.c:1170
config_register_addressmaps
void config_register_addressmaps(const or_options_t *options)
Definition: config.c:4659
config_parse_commandline
parsed_cmdline_t * config_parse_commandline(int argc, char **argv, int ignore_errors)
Definition: config.c:2520
dirinfo_type_t
dirinfo_type_t
Definition: or.h:905
testsupport.h
Macros to implement mocking and selective exposure for the test code.
options_new
or_options_t * options_new(void)
Definition: config.c:2740
option_get_assignment
struct config_line_t * option_get_assignment(const or_options_t *options, const char *key)
Definition: config.c:2646
safe_str_opts
const char * safe_str_opts(const or_options_t *options, const char *address)
Definition: config.c:1102
parsed_cmdline_t::command
tor_cmdline_mode_t command
Definition: config.h:196
control_connection_t
Definition: control_connection_st.h:19
port_cfg_free_
void port_cfg_free_(port_cfg_t *port)
Definition: config.c:5669
portconf_get_first_advertised_addr
const tor_addr_t * portconf_get_first_advertised_addr(int listener_type, int address_family)
Definition: config.c:6605
port_parse_config
int port_parse_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:5845
config_line_t
Definition: confline.h:29
port_cfg_line_extract_addrport
int port_cfg_line_extract_addrport(const char *line, char **addrport_out, int *is_unix_out, const char **rest_out)
Definition: config.c:5755
portconf_get_first_advertised_port
int portconf_get_first_advertised_port(int listener_type, int address_family)
Definition: config.c:6593
options_act_reversible
STATIC int options_act_reversible(const or_options_t *old_options, char **msg)
Definition: config.c:1900
parsed_cmdline_t::cmdline_opts
struct config_line_t * cmdline_opts
Definition: config.h:191
options_need_geoip_info
int options_need_geoip_info(const or_options_t *options, const char **reason_out)
Definition: config.c:1967
log_severity_list_t
Definition: log.h:151
safe_str_client_opts
const char * safe_str_client_opts(const or_options_t *options, const char *address)
Definition: config.c:1081
or_options_t
Definition: or_options_st.h:45
STATIC
#define STATIC
Definition: testsupport.h:32
init_cookie_authentication
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:7241
parsed_cmdline_t
Definition: config.h:189
parse_ports
STATIC int parse_ports(or_options_t *options, int validate_only, char **msg, int *n_ports_out, int *world_writable_control_socket)
Definition: config.c:6344
option_is_recognized
int option_is_recognized(const char *key)
Definition: config.c:2630
setopt_err_t
setopt_err_t
Definition: config.h:48
smartlist_t
Definition: smartlist_core.h:26
get_options_mutable
or_options_t * get_options_mutable(void)
Definition: config.c:919
quiet_level_t
quiet_level_t
Definition: quiet_level.h:16
options_init
void options_init(or_options_t *options)
Definition: config.c:2750
config_free_all
void config_free_all(void)
Definition: config.c:1048
get_num_cpus
int get_num_cpus(const or_options_t *options)
Definition: config.c:6852
addressmap_register_auto
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:4705
option_get_canonical_name
const char * option_get_canonical_name(const char *key)
Definition: config.c:2638