Tor  0.4.7.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-2021, 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 /** Pattern for backing up configuration files */
48 #define CONFIG_BACKUP_PATTERN "%s.orig.1"
49 
50 /** An error from options_trial_assign() or options_init_from_string(). */
51 typedef enum setopt_err_t {
52  SETOPT_OK = 0,
53  SETOPT_ERR_MISC = -1,
54  SETOPT_ERR_PARSE = -2,
55  SETOPT_ERR_TRANSITION = -3,
56  SETOPT_ERR_SETTING = -4,
57 } setopt_err_t;
58 setopt_err_t options_trial_assign(struct config_line_t *list, unsigned flags,
59  char **msg);
60 
61 void options_init(or_options_t *options);
62 
63 #define OPTIONS_DUMP_MINIMAL 1
64 #define OPTIONS_DUMP_ALL 2
65 char *options_dump(const or_options_t *options, int how_to_dump);
66 int options_init_from_torrc(int argc, char **argv);
67 setopt_err_t options_init_from_string(const char *cf_defaults, const char *cf,
68  int command, const char *command_arg, char **msg);
69 int option_is_recognized(const char *key);
70 const char *option_get_canonical_name(const char *key);
71 struct config_line_t *option_get_assignment(const or_options_t *options,
72  const char *key);
73 int options_save_current(void);
74 const char *get_torrc_fname(int defaults_fname);
75 typedef enum {
76  DIRROOT_DATADIR,
77  DIRROOT_CACHEDIR,
78  DIRROOT_KEYDIR
79 } directory_root_t;
80 
81 MOCK_DECL(char *,
83  (const or_options_t *options,
84  directory_root_t roottype,
85  const char *sub1, const char *sub2,
86  const char *suffix));
87 
88 /* These macros wrap options_get_dir_fname2_suffix to provide a more
89  * convenient API for finding filenames that Tor uses inside its storage
90  * They are named according to a pattern:
91  * (options_)?get_(cache|key|data)dir_fname(2)?(_suffix)?
92  *
93  * Macros that begin with options_ take an options argument; the others
94  * work with respect to the global options.
95  *
96  * Each macro works relative to the data directory, the key directory,
97  * or the cache directory, as determined by which one is mentioned.
98  *
99  * Macro variants with "2" in their name take two path components; others
100  * take one.
101  *
102  * Macro variants with "_suffix" at the end take an additional suffix
103  * that gets appended to the end of the file
104  */
105 #define options_get_datadir_fname2_suffix(options, sub1, sub2, suffix) \
106  options_get_dir_fname2_suffix((options), DIRROOT_DATADIR, \
107  (sub1), (sub2), (suffix))
108 #define options_get_cachedir_fname2_suffix(options, sub1, sub2, suffix) \
109  options_get_dir_fname2_suffix((options), DIRROOT_CACHEDIR, \
110  (sub1), (sub2), (suffix))
111 #define options_get_keydir_fname2_suffix(options, sub1, sub2, suffix) \
112  options_get_dir_fname2_suffix((options), DIRROOT_KEYDIR, \
113  (sub1), (sub2), (suffix))
114 
115 #define options_get_datadir_fname(opts,sub1) \
116  options_get_datadir_fname2_suffix((opts),(sub1), NULL, NULL)
117 #define options_get_datadir_fname2(opts,sub1,sub2) \
118  options_get_datadir_fname2_suffix((opts),(sub1), (sub2), NULL)
119 
120 #define get_datadir_fname2_suffix(sub1, sub2, suffix) \
121  options_get_datadir_fname2_suffix(get_options(), (sub1), (sub2), (suffix))
122 #define get_datadir_fname(sub1) \
123  get_datadir_fname2_suffix((sub1), NULL, NULL)
124 #define get_datadir_fname2(sub1,sub2) \
125  get_datadir_fname2_suffix((sub1), (sub2), NULL)
126 #define get_datadir_fname_suffix(sub1, suffix) \
127  get_datadir_fname2_suffix((sub1), NULL, (suffix))
128 
129 /** DOCDOC */
130 #define options_get_keydir_fname(options, sub1) \
131  options_get_keydir_fname2_suffix((options), (sub1), NULL, NULL)
132 #define get_keydir_fname_suffix(sub1, suffix) \
133  options_get_keydir_fname2_suffix(get_options(), (sub1), NULL, suffix)
134 #define get_keydir_fname(sub1) \
135  options_get_keydir_fname2_suffix(get_options(), (sub1), NULL, NULL)
136 
137 #define get_cachedir_fname(sub1) \
138  options_get_cachedir_fname2_suffix(get_options(), (sub1), NULL, NULL)
139 #define get_cachedir_fname_suffix(sub1, suffix) \
140  options_get_cachedir_fname2_suffix(get_options(), (sub1), NULL, (suffix))
141 
142 #define safe_str_client(address) \
143  safe_str_client_opts(NULL, address)
144 #define safe_str(address) \
145  safe_str_opts(NULL, address)
146 
147 const char * safe_str_client_opts(const or_options_t *options,
148  const char *address);
149 const char * safe_str_opts(const or_options_t *options,
150  const char *address);
151 
152 int using_default_dir_authorities(const or_options_t *options);
153 
154 int create_keys_directory(const or_options_t *options);
155 
156 int check_or_create_data_subdir(const char *subdir);
157 int write_to_data_subdir(const char* subdir, const char* fname,
158  const char* str, const char* descr);
159 
160 int get_num_cpus(const or_options_t *options);
161 
163 int port_binds_ipv4(const port_cfg_t *port);
164 int port_binds_ipv6(const port_cfg_t *port);
165 int portconf_get_first_advertised_port(int listener_type,
166  int address_family);
167 #define portconf_get_primary_dir_port() \
168  (portconf_get_first_advertised_port(CONN_TYPE_DIR_LISTENER, AF_INET))
169 const tor_addr_t *portconf_get_first_advertised_addr(int listener_type,
170  int address_family);
171 int port_exists_by_type_addr_port(int listener_type, const tor_addr_t *addr,
172  int port, int check_wildcard);
173 int port_exists_by_type_addr32h_port(int listener_type, uint32_t addr_ipv4h,
174  int port, int check_wildcard);
175 
176 char *get_first_listener_addrport_string(int listener_type);
177 
178 int options_need_geoip_info(const or_options_t *options,
179  const char **reason_out);
180 
182  const char *question, char **answer,
183  const char **errmsg);
184 
185 int init_cookie_authentication(const char *fname, const char *header,
186  int cookie_len, int group_readable,
187  uint8_t **cookie_out, int *cookie_is_set_out);
188 
190 
191 /** Options settings parsed from the command-line. */
192 typedef struct {
193  /** List of options that can only be set from the command-line */
195  /** List of other options, to be handled by the general Tor configuration
196  system. */
198  /** Subcommand that Tor has been told to run */
200  /** Argument for the command mode, if any. */
201  const char *command_arg;
202  /** How quiet have we been told to be? */
205 
206 parsed_cmdline_t *config_parse_commandline(int argc, char **argv,
207  int ignore_errors);
209 #define parsed_cmdline_free(c) \
210  FREE_AND_NULL(parsed_cmdline_t, parsed_cmdline_free_, (c))
211 
212 void config_register_addressmaps(const or_options_t *options);
213 /* XXXX move to connection_edge.h */
214 int addressmap_register_auto(const char *from, const char *to,
215  time_t expires,
216  addressmap_entry_source_t addrmap_source,
217  const char **msg);
218 
219 int port_cfg_line_extract_addrport(const char *line,
220  char **addrport_out,
221  int *is_unix_out,
222  const char **rest_out);
223 
224 /** Represents the information stored in a torrc Bridge line. */
225 typedef struct bridge_line_t {
226  tor_addr_t addr; /* The IP address of the bridge. */
227  uint16_t port; /* The TCP port of the bridge. */
228  char *transport_name; /* The name of the pluggable transport that
229  should be used to connect to the bridge. */
230  char digest[DIGEST_LEN]; /* The bridge's identity key digest. */
231  smartlist_t *socks_args; /* SOCKS arguments for the pluggable
232  transport proxy. */
233 } bridge_line_t;
234 
235 void bridge_line_free_(bridge_line_t *bridge_line);
236 #define bridge_line_free(line) \
237  FREE_AND_NULL(bridge_line_t, bridge_line_free_, (line))
238 bridge_line_t *parse_bridge_line(const char *line);
239 
240 /* Port helper functions. */
241 int options_any_client_port_set(const or_options_t *options);
243  const struct config_line_t *ports,
244  const char *portname,
245  int listener_type,
246  const char *defaultaddr,
247  int defaultport,
248  const unsigned flags);
249 
250 #define CL_PORT_NO_STREAM_OPTIONS (1u<<0)
251 #define CL_PORT_WARN_NONLOCAL (1u<<1)
252 /* Was CL_PORT_ALLOW_EXTRA_LISTENADDR (1u<<2) */
253 #define CL_PORT_SERVER_OPTIONS (1u<<3)
254 #define CL_PORT_FORBID_NONLOCAL (1u<<4)
255 #define CL_PORT_TAKES_HOSTNAMES (1u<<5)
256 #define CL_PORT_IS_UNIXSOCKET (1u<<6)
257 #define CL_PORT_DFLT_GROUP_WRITABLE (1u<<7)
258 
259 port_cfg_t *port_cfg_new(size_t namelen);
260 #define port_cfg_free(port) \
261  FREE_AND_NULL(port_cfg_t, port_cfg_free_, (port))
262 void port_cfg_free_(port_cfg_t *port);
263 
264 int port_count_real_listeners(const smartlist_t *ports,
265  int listenertype,
266  int count_sockets);
267 int pt_parse_transport_line(const or_options_t *options,
268  const char *line, int validate_only,
269  int server);
270 int config_ensure_bandwidth_cap(uint64_t *value, const char *desc, char **msg);
271 
272 #ifdef CONFIG_PRIVATE
273 
274 MOCK_DECL(STATIC int, options_act,(const or_options_t *old_options));
275 MOCK_DECL(STATIC int, options_act_reversible,(const or_options_t *old_options,
276  char **msg));
277 struct config_mgr_t;
278 STATIC const struct config_mgr_t *get_options_mgr(void);
279 
280 #define or_options_free(opt) \
281  FREE_AND_NULL(or_options_t, or_options_free_, (opt))
282 STATIC void or_options_free_(or_options_t *options);
283 STATIC int options_validate_single_onion(or_options_t *options,
284  char **msg);
285 STATIC int parse_tcp_proxy_line(const char *line, or_options_t *options,
286  char **msg);
288  const or_options_t *old_options);
291 STATIC int parse_dir_authority_line(const char *line,
292  dirinfo_type_t required_type,
293  int validate_only);
294 STATIC int parse_dir_fallback_line(const char *line, int validate_only);
295 
296 STATIC uint64_t compute_real_max_mem_in_queues(const uint64_t val,
297  bool is_server);
299  const char *fname,
300  int truncate_log);
301 STATIC int options_init_logs(const or_options_t *old_options,
302  const or_options_t *options, int validate_only);
303 
304 STATIC int options_create_directories(char **msg_out);
305 struct log_transaction_t;
307  const or_options_t *old_options,
308  char **msg_out);
311 
312 #ifdef TOR_UNIT_TESTS
313 int options_validate(const or_options_t *old_options,
314  or_options_t *options,
315  char **msg);
316 #endif
317 
318 STATIC int parse_ports(or_options_t *options, int validate_only,
319  char **msg, int *n_ports_out,
320  int *world_writable_control_socket);
321 
322 #endif /* defined(CONFIG_PRIVATE) */
323 
324 #endif /* !defined(TOR_CONFIG_H) */
STATIC void add_default_trusted_dir_authorities(dirinfo_type_t type)
Definition: config.c:1185
void add_default_fallback_dir_servers(void)
Definition: config.c:1199
STATIC int options_act_reversible(const or_options_t *old_options, char **msg)
Definition: config.c:1891
STATIC void or_options_free_(or_options_t *options)
Definition: config.c:1031
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:6526
int consider_adding_dir_servers(const or_options_t *options, const or_options_t *old_options)
Definition: config.c:1267
STATIC void options_commit_log_transaction(log_transaction_t *xn)
Definition: config.c:1807
STATIC int open_and_add_file_log(const log_severity_list_t *severity, const char *filename, int truncate_log)
Definition: config.c:4842
STATIC int options_create_directories(char **msg_out)
Definition: config.c:1518
STATIC log_transaction_t * options_start_log_transaction(const or_options_t *old_options, char **msg_out)
Definition: config.c:1772
STATIC const config_mgr_t * get_options_mgr(void)
Definition: config.c:893
int parse_dir_fallback_line(const char *line, int validate_only)
Definition: config.c:5727
STATIC int options_act(const or_options_t *old_options)
Definition: config.c:2035
STATIC int parse_dir_authority_line(const char *line, dirinfo_type_t required_type, int validate_only)
Definition: config.c:5548
STATIC int options_init_logs(const or_options_t *old_options, const or_options_t *options, int validate_only)
Definition: config.c:4900
STATIC void options_rollback_log_transaction(log_transaction_t *xn)
Definition: config.c:1861
tor_cmdline_mode_t command
Definition: config.c:2440
STATIC int parse_tcp_proxy_line(const char *line, or_options_t *options, char **msg)
Definition: config.c:5212
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:6034
int config_ensure_bandwidth_cap(uint64_t *value, const char *desc, char **msg)
Definition: config.c:2951
void options_init(or_options_t *options)
Definition: config.c:2882
int options_save_current(void)
Definition: config.c:7034
const smartlist_t * get_configured_ports(void)
Definition: config.c:6687
int pt_parse_transport_line(const or_options_t *options, const char *line, int validate_only, int server)
Definition: config.c:5275
struct config_line_t * option_get_assignment(const or_options_t *options, const char *key)
Definition: config.c:2644
void bridge_line_free_(bridge_line_t *bridge_line)
Definition: config.c:5070
int get_num_cpus(const or_options_t *options)
Definition: config.c:7047
setopt_err_t options_trial_assign(struct config_line_t *list, unsigned flags, char **msg)
Definition: config.c:2659
or_options_t * get_options_mutable(void)
Definition: config.c:910
parsed_cmdline_t * config_parse_commandline(int argc, char **argv, int ignore_errors)
Definition: config.c:2513
or_options_t * options_new(void)
Definition: config.c:2872
int getinfo_helper_config(control_connection_t *conn, const char *question, char **answer, const char **errmsg)
Definition: config.c:7177
char * get_first_listener_addrport_string(int listener_type)
Definition: config.c:6704
port_cfg_t * port_cfg_new(size_t namelen)
Definition: config.c:5837
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:7096
int options_init_from_torrc(int argc, char **argv)
Definition: config.c:4448
void port_cfg_free_(port_cfg_t *port)
Definition: config.c:5858
int portconf_get_first_advertised_port(int listener_type, int address_family)
Definition: config.c:6787
const or_options_t * get_options(void)
Definition: config.c:919
int port_cfg_line_extract_addrport(const char *line, char **addrport_out, int *is_unix_out, const char **rest_out)
Definition: config.c:5944
int check_or_create_data_subdir(const char *subdir)
Definition: config.c:7139
int options_any_client_port_set(const or_options_t *options)
Definition: config.c:7502
int create_keys_directory(const or_options_t *options)
Definition: config.c:1375
void init_protocol_warning_severity_level(void)
Definition: config.c:1161
int get_protocol_warning_severity_level(void)
Definition: config.c:1143
const tor_addr_t * portconf_get_first_advertised_addr(int listener_type, int address_family)
Definition: config.c:6799
char * options_dump(const or_options_t *options, int how_to_dump)
Definition: config.c:2902
int option_is_recognized(const char *key)
Definition: config.c:2628
const char * safe_str_client_opts(const or_options_t *options, const char *address)
Definition: config.c:1072
const char * safe_str_opts(const or_options_t *options, const char *address)
Definition: config.c:1093
void parsed_cmdline_free_(parsed_cmdline_t *cmdline)
Definition: config.c:2617
const char * option_get_canonical_name(const char *key)
Definition: config.c:2636
const char * get_torrc_fname(int defaults_fname)
Definition: config.c:4738
int set_options(or_options_t *new_val, char **msg)
Definition: config.c:955
bridge_line_t * parse_bridge_line(const char *line)
Definition: config.c:5096
const char * escaped_safe_str_client(const char *address)
Definition: config.c:1110
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:4798
int options_need_geoip_info(const or_options_t *options, const char **reason_out)
Definition: config.c:1958
void config_register_addressmaps(const or_options_t *options)
Definition: config.c:4752
void config_free_all(void)
Definition: config.c:1039
int port_count_real_listeners(const smartlist_t *ports, int listenertype, int count_sockets)
Definition: config.c:6501
int port_exists_by_type_addr_port(int listener_type, const tor_addr_t *addr, int port, int check_wildcard)
Definition: config.c:6814
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:7443
setopt_err_t
Definition: config.h:51
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:4628
const char * escaped_safe_str(const char *address)
Definition: config.c:1122
int write_to_data_subdir(const char *subdir, const char *fname, const char *str, const char *descr)
Definition: config.c:7158
#define DIGEST_LEN
Definition: digest_sizes.h:20
addressmap_entry_source_t
Definition: or.h:915
dirinfo_type_t
Definition: or.h:785
The or_options_t structure, which represents Tor's configuration.
Declare the quiet_level enumeration and global.
quiet_level_t
Definition: quiet_level.h:16
const char * command_arg
Definition: config.h:201
quiet_level_t quiet_level
Definition: config.h:203
struct config_line_t * cmdline_opts
Definition: config.h:194
struct config_line_t * other_opts
Definition: config.h:197
tor_cmdline_mode_t command
Definition: config.h:199
Macros to implement mocking and selective exposure for the test code.
#define STATIC
Definition: testsupport.h:32
#define MOCK_DECL(rv, funcname, arglist)
Definition: testsupport.h:127
tor_cmdline_mode_t