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 */
9 * \brief Header file for config.c.
15 #include "app/config/or_options_st.h"
16 #include "lib/testsupport/testsupport.h"
18 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(DARWIN)
19 #define KERNEL_MAY_SUPPORT_IPFW
22 /** Lowest allowable value for HeartbeatPeriod; if this is too low, we might
23 * expose more information than we're comfortable with. */
24 #define MIN_HEARTBEAT_PERIOD (30*60)
26 /** Maximum default value for MaxMemInQueues, in bytes. */
27 #if SIZEOF_VOID_P >= 8
28 #define MAX_DEFAULT_MEMORY_QUEUE_SIZE (UINT64_C(8) << 30)
30 #define MAX_DEFAULT_MEMORY_QUEUE_SIZE (UINT64_C(2) << 30)
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
);
42 void init_protocol_warning_severity_level(void);
43 int get_protocol_warning_severity_level(void);
45 /** An error from options_trial_assign() or options_init_from_string(). */
46 typedef enum setopt_err_t
{
49 SETOPT_ERR_PARSE
= -2,
50 SETOPT_ERR_TRANSITION
= -3,
51 SETOPT_ERR_SETTING
= -4,
53 setopt_err_t
options_trial_assign(struct config_line_t
*list
, unsigned flags
,
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
,
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
);
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
,
75 int options_save_current(void);
76 const char *get_torrc_fname(int defaults_fname
);
84 options_get_dir_fname2_suffix
,
85 (const or_options_t
*options
,
86 directory_root_t roottype
,
87 const char *sub1
, const char *sub2
,
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)?
95 * Macros that begin with options_ take an options argument; the others
96 * work with respect to the global options.
98 * Each macro works relative to the data directory, the key directory,
99 * or the cache directory, as determined by which one is mentioned.
101 * Macro variants with "2" in their name take two path components; others
104 * Macro variants with "_suffix" at the end take an additional suffix
105 * that gets appended to the end of the file
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))
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)
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))
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)
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))
144 int using_default_dir_authorities(const or_options_t
*options
);
146 int create_keys_directory(const or_options_t
*options
);
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
);
152 int get_num_cpus(const or_options_t
*options
);
154 MOCK_DECL(const smartlist_t
*,get_configured_ports
,(void));
155 int get_first_advertised_port_by_type_af(int listener_type
,
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
,
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
);
168 char *get_first_listener_addrport_string(int listener_type
);
170 int options_need_geoip_info(const or_options_t
*options
,
171 const char **reason_out
);
173 smartlist_t
*get_list_of_ports_to_forward(void);
175 int getinfo_helper_config(control_connection_t
*conn
,
176 const char *question
, char **answer
,
177 const char **errmsg
);
179 uint32_t get_effective_bwrate(const or_options_t
*options
);
180 uint32_t get_effective_bwburst(const or_options_t
*options
);
182 char *get_transport_bindaddr_from_config(const char *transport
);
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
);
188 or_options_t
*options_new(void);
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
);
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
,
198 addressmap_entry_source_t addrmap_source
,
201 int port_cfg_line_extract_addrport(const char *line
,
204 const char **rest_out
);
206 /** Represents the information stored in a torrc Bridge line. */
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
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
);
221 smartlist_t
*get_options_from_transport_options_line(const char *line
,
222 const char *transport
);
223 smartlist_t
*get_options_for_server_transport(const char *transport
);
225 /* Port helper functions. */
226 int options_any_client_port_set(const or_options_t
*options
);
228 #ifdef CONFIG_PRIVATE
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)
239 STATIC
int options_act(const or_options_t
*old_options
);
240 #ifdef TOR_UNIT_TESTS
241 extern struct config_format_t options_format
;
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
,
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
,
260 STATIC
int consider_adding_dir_servers(const or_options_t
*options
,
261 const or_options_t
*old_options
);
262 STATIC
void add_default_trusted_dir_authorities(dirinfo_type_t type
);
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
,
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
,
274 const char *defaultaddr
,
276 const unsigned flags
);
278 STATIC
int check_bridge_distribution_setting(const char *bd
);
280 STATIC
uint64_t compute_real_max_mem_in_queues(const uint64_t val
,
282 STATIC
int open_and_add_file_log(const log_severity_list_t
*severity
,
286 #endif /* defined(CONFIG_PRIVATE) */
288 #endif /* !defined(TOR_CONFIG_H) */