config.c 194 KB
Newer Older
Roger Dingledine's avatar
Roger Dingledine committed
1
2
/* Copyright (c) 2001 Matej Pfajfar.
 * Copyright (c) 2001-2004, Roger Dingledine.
3
 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4
 * Copyright (c) 2007-2012, The Tor Project, Inc. */
5
/* See LICENSE for licensing information */
6

Nick Mathewson's avatar
Nick Mathewson committed
7
/**
8
9
 * \file config.c
 * \brief Code to parse and interpret configuration files.
Nick Mathewson's avatar
Nick Mathewson committed
10
11
 **/

12
13
#define CONFIG_PRIVATE

Roger Dingledine's avatar
Roger Dingledine committed
14
#include "or.h"
15
#include "addressmap.h"
16
#include "channel.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
17
#include "circuitbuild.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
18
#include "circuitlist.h"
19
20
#include "circuitmux.h"
#include "circuitmux_ewma.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
21
#include "config.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
22
#include "connection.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
23
#include "connection_edge.h"
24
#include "connection_or.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
25
#include "control.h"
26
#include "confparse.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
27
#include "cpuworker.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
28
#include "dirserv.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
29
#include "dirvote.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
30
#include "dns.h"
31
#include "entrynodes.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
32
#include "geoip.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
33
#include "hibernate.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
34
#include "main.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
35
#include "networkstatus.h"
36
#include "nodelist.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
37
#include "policies.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
38
#include "relay.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
39
#include "rendclient.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
40
#include "rendservice.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
41
#include "rephist.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
42
#include "router.h"
43
#include "util.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
44
#include "routerlist.h"
45
#include "routerset.h"
46
#include "statefile.h"
47
#include "transports.h"
48
#ifdef _WIN32
49
50
#include <shlobj.h>
#endif
Roger Dingledine's avatar
Roger Dingledine committed
51

52
53
54
55
56
#include "procmon.h"

/* From main.c */
extern int quiet_level;

57
58
/** A list of abbreviations and aliases to map command-line options, obsolete
 * option names, or alternative option names, to their current values. */
59
static config_abbrev_t option_abbrevs_[] = {
60
61
62
63
  PLURAL(AuthDirBadDirCC),
  PLURAL(AuthDirBadExitCC),
  PLURAL(AuthDirInvalidCC),
  PLURAL(AuthDirRejectCC),
64
  PLURAL(ExitNode),
65
  PLURAL(EntryNode),
66
67
  PLURAL(ExcludeNode),
  PLURAL(FirewallPort),
68
  PLURAL(LongLivedPort),
69
70
  PLURAL(HiddenServiceNode),
  PLURAL(HiddenServiceExcludeNode),
71
  PLURAL(NumCPU),
72
73
  PLURAL(RendNode),
  PLURAL(RendExcludeNode),
74
75
  PLURAL(StrictEntryNode),
  PLURAL(StrictExitNode),
76
  PLURAL(StrictNode),
77
  { "l", "Log", 1, 0},
78
  { "AllowUnverifiedNodes", "AllowInvalidNodes", 0, 0},
79
80
  { "AutomapHostSuffixes", "AutomapHostsSuffixes", 0, 0},
  { "AutomapHostOnResolve", "AutomapHostsOnResolve", 0, 0},
81
82
83
84
  { "BandwidthRateBytes", "BandwidthRate", 0, 0},
  { "BandwidthBurstBytes", "BandwidthBurst", 0, 0},
  { "DirFetchPostPeriod", "StatusFetchPeriod", 0, 0},
  { "MaxConn", "ConnLimit", 0, 1},
85
86
87
  { "ORBindAddress", "ORListenAddress", 0, 0},
  { "DirBindAddress", "DirListenAddress", 0, 0},
  { "SocksBindAddress", "SocksListenAddress", 0, 0},
88
89
90
91
  { "UseHelperNodes", "UseEntryGuards", 0, 0},
  { "NumHelperNodes", "NumEntryGuards", 0, 0},
  { "UseEntryNodes", "UseEntryGuards", 0, 0},
  { "NumEntryNodes", "NumEntryGuards", 0, 0},
92
93
  { "ResolvConf", "ServerDNSResolvConfFile", 0, 1},
  { "SearchDomains", "ServerDNSSearchDomains", 0, 1},
94
  { "ServerDNSAllowBrokenResolvConf", "ServerDNSAllowBrokenConfig", 0, 0},
95
  { "PreferTunnelledDirConns", "PreferTunneledDirConns", 0, 0},
96
  { "BridgeAuthoritativeDirectory", "BridgeAuthoritativeDir", 0, 0},
97
  { "HashedControlPassword", "__HashedControlSessionPassword", 1, 0},
98
99
  { "StrictEntryNodes", "StrictNodes", 0, 1},
  { "StrictExitNodes", "StrictNodes", 0, 1},
100
  { "_UseFilteringSSLBufferevents", "UseFilteringSSLBufferevents", 0, 1},
101
102
  { NULL, NULL, 0, 0},
};
103

Nick Mathewson's avatar
Nick Mathewson committed
104
105
106
107
/** An entry for config_vars: "The option <b>name</b> has type
 * CONFIG_TYPE_<b>conftype</b>, and corresponds to
 * or_options_t.<b>member</b>"
 */
108
109
#define VAR(name,conftype,member,initvalue)                             \
  { name, CONFIG_TYPE_ ## conftype, STRUCT_OFFSET(or_options_t, member), \
110
      initvalue }
111
112
113
/** As VAR, but the option name and member name are the same. */
#define V(member,conftype,initvalue)                                    \
  VAR(#member, conftype, member, initvalue)
Nick Mathewson's avatar
Nick Mathewson committed
114
/** An entry for config_vars: "The option <b>name</b> is obsolete." */
115
#define OBSOLETE(name) { name, CONFIG_TYPE_OBSOLETE, 0, NULL }
116

117
118
119
#define VPORT(member,conftype,initvalue)                                    \
  VAR(#member, conftype, member ## _lines, initvalue)

Nick Mathewson's avatar
Nick Mathewson committed
120
121
122
123
/** Array of configuration options.  Until we disallow nonstandard
 * abbreviations, order is significant, since the first matching option will
 * be chosen first.
 */
124
static config_var_t option_vars_[] = {
125
  OBSOLETE("AccountingMaxKB"),
126
127
128
  V(AccountingMax,               MEMUNIT,  "0 bytes"),
  V(AccountingStart,             STRING,   NULL),
  V(Address,                     STRING,   NULL),
129
  V(AllowDotExit,                BOOL,     "0"),
130
131
  V(AllowInvalidNodes,           CSV,      "middle,rendezvous"),
  V(AllowNonRFC953Hostnames,     BOOL,     "0"),
132
133
  V(AllowSingleHopCircuits,      BOOL,     "0"),
  V(AllowSingleHopExits,         BOOL,     "0"),
134
135
136
  V(AlternateBridgeAuthority,    LINELIST, NULL),
  V(AlternateDirAuthority,       LINELIST, NULL),
  V(AlternateHSAuthority,        LINELIST, NULL),
137
  V(AssumeReachable,             BOOL,     "0"),
138
  V(AuthDirBadDir,               LINELIST, NULL),
139
  V(AuthDirBadDirCCs,            CSV,      ""),
140
  V(AuthDirBadExit,              LINELIST, NULL),
141
  V(AuthDirBadExitCCs,           CSV,      ""),
142
  V(AuthDirInvalid,              LINELIST, NULL),
143
  V(AuthDirInvalidCCs,           CSV,      ""),
144
  V(AuthDirFastGuarantee,        MEMUNIT,  "100 KB"),
145
  V(AuthDirGuardBWGuarantee,     MEMUNIT,  "250 KB"),
146
  V(AuthDirReject,               LINELIST, NULL),
147
  V(AuthDirRejectCCs,            CSV,      ""),
148
  V(AuthDirRejectUnlisted,       BOOL,     "0"),
149
  V(AuthDirListBadDirs,          BOOL,     "0"),
150
  V(AuthDirListBadExits,         BOOL,     "0"),
151
152
  V(AuthDirMaxServersPerAddr,    UINT,     "2"),
  V(AuthDirMaxServersPerAuthAddr,UINT,     "5"),
153
  V(AuthDirHasIPv6Connectivity,  BOOL,     "0"),
154
155
156
157
  VAR("AuthoritativeDirectory",  BOOL, AuthoritativeDir,    "0"),
  V(AutomapHostsOnResolve,       BOOL,     "0"),
  V(AutomapHostsSuffixes,        CSV,      ".onion,.exit"),
  V(AvoidDiskWrites,             BOOL,     "0"),
158
159
  V(BandwidthBurst,              MEMUNIT,  "1 GB"),
  V(BandwidthRate,               MEMUNIT,  "1 GB"),
160
161
  V(BridgeAuthoritativeDir,      BOOL,     "0"),
  VAR("Bridge",                  LINELIST, Bridges,    NULL),
162
  V(BridgePassword,              STRING,   NULL),
163
  V(BridgeRecordUsageByCountry,  BOOL,     "1"),
164
  V(BridgeRelay,                 BOOL,     "0"),
165
  V(CellStatistics,              BOOL,     "0"),
166
  V(LearnCircuitBuildTimeout,    BOOL,     "1"),
167
  V(CircuitBuildTimeout,         INTERVAL, "0"),
168
  V(CircuitIdleTimeout,          INTERVAL, "1 hour"),
169
  V(CircuitStreamTimeout,        INTERVAL, "0"),
170
  V(CircuitPriorityHalflife,     DOUBLE,  "-100.0"), /*negative:'Use default'*/
171
172
  V(ClientDNSRejectInternalAddresses, BOOL,"1"),
  V(ClientOnly,                  BOOL,     "0"),
173
  V(ClientPreferIPv6ORPort,      BOOL,     "0"),
174
  V(ClientRejectInternalAddresses, BOOL,   "1"),
175
  V(ClientTransportPlugin,       LINELIST, NULL),
176
  V(ClientUseIPv6,               BOOL,     "0"),
177
  V(ConsensusParams,             STRING,   NULL),
178
  V(ConnLimit,                   UINT,     "1000"),
179
  V(ConnDirectionStatistics,     BOOL,     "0"),
180
181
182
183
  V(ConstrainedSockets,          BOOL,     "0"),
  V(ConstrainedSockSize,         MEMUNIT,  "8192"),
  V(ContactInfo,                 STRING,   NULL),
  V(ControlListenAddress,        LINELIST, NULL),
184
  VPORT(ControlPort,                 LINELIST, NULL),
185
  V(ControlPortFileGroupReadable,BOOL,     "0"),
186
  V(ControlPortWriteToFile,      FILENAME, NULL),
187
  V(ControlSocket,               LINELIST, NULL),
188
  V(ControlSocketsGroupWritable, BOOL,     "0"),
189
190
191
  V(CookieAuthentication,        BOOL,     "0"),
  V(CookieAuthFileGroupReadable, BOOL,     "0"),
  V(CookieAuthFile,              STRING,   NULL),
192
  V(CountPrivateBandwidth,       BOOL,     "0"),
193
  V(DataDirectory,               FILENAME, NULL),
194
  OBSOLETE("DebugLogFile"),
195
  V(DisableNetwork,              BOOL,     "0"),
196
  V(DirAllowPrivateAddresses,    BOOL,     "0"),
197
  V(TestingAuthDirTimeToLearnReachability, INTERVAL, "30 minutes"),
198
  V(DirListenAddress,            LINELIST, NULL),
199
  OBSOLETE("DirFetchPeriod"),
200
  V(DirPolicy,                   LINELIST, NULL),
201
  VPORT(DirPort,                     LINELIST, NULL),
202
  V(DirPortFrontPage,            FILENAME, NULL),
203
  OBSOLETE("DirPostPeriod"),
204
205
206
207
  OBSOLETE("DirRecordUsageByCountry"),
  OBSOLETE("DirRecordUsageGranularity"),
  OBSOLETE("DirRecordUsageRetainIPs"),
  OBSOLETE("DirRecordUsageSaveInterval"),
208
  V(DirReqStatistics,            BOOL,     "1"),
209
  VAR("DirServer",               LINELIST, DirServers, NULL),
210
  V(DisableAllSwap,              BOOL,     "0"),
211
  V(DisableDebuggerAttachment,   BOOL,     "1"),
212
  V(DisableIOCP,                 BOOL,     "1"),
213
  V(DynamicDHGroups,             BOOL,     "0"),
214
  VPORT(DNSPort,                     LINELIST, NULL),
215
216
217
  V(DNSListenAddress,            LINELIST, NULL),
  V(DownloadExtraInfo,           BOOL,     "0"),
  V(EnforceDistinctSubnets,      BOOL,     "1"),
218
  V(EntryNodes,                  ROUTERSET,   NULL),
219
  V(EntryStatistics,             BOOL,     "0"),
220
  V(TestingEstimatedDescriptorPropagationTime, INTERVAL, "10 minutes"),
221
222
  V(ExcludeNodes,                ROUTERSET, NULL),
  V(ExcludeExitNodes,            ROUTERSET, NULL),
223
  V(ExcludeSingleHopRelays,      BOOL,     "1"),
224
  V(ExitNodes,                   ROUTERSET, NULL),
225
226
  V(ExitPolicy,                  LINELIST, NULL),
  V(ExitPolicyRejectPrivate,     BOOL,     "1"),
227
  V(ExitPortStatistics,          BOOL,     "0"),
228
  V(ExtendAllowPrivateAddresses, BOOL,     "0"),
229
  V(ExtraInfoStatistics,         BOOL,     "1"),
230

valerino's avatar
valerino committed
231
232
233
#if defined (WINCE)
  V(FallbackNetworkstatusFile,   FILENAME, "fallback-consensus"),
#else
234
  V(FallbackNetworkstatusFile,   FILENAME,
235
    SHARE_DATADIR PATH_SEPARATOR "tor" PATH_SEPARATOR "fallback-consensus"),
valerino's avatar
valerino committed
236
#endif
237
238
239
  V(FascistFirewall,             BOOL,     "0"),
  V(FirewallPorts,               CSV,      ""),
  V(FastFirstHopPK,              BOOL,     "1"),
240
  V(FetchDirInfoEarly,           BOOL,     "0"),
241
  V(FetchDirInfoExtraEarly,      BOOL,     "0"),
242
243
244
  V(FetchServerDescriptors,      BOOL,     "1"),
  V(FetchHidServDescriptors,     BOOL,     "1"),
  V(FetchUselessDescriptors,     BOOL,     "0"),
245
  V(FetchV2Networkstatus,        BOOL,     "0"),
246
#ifdef _WIN32
247
  V(GeoIPFile,                   FILENAME, "<default>"),
nils's avatar
nils committed
248
  V(GeoIPv6File,                 FILENAME, "<default>"),
249
#else
250
251
  V(GeoIPFile,                   FILENAME,
    SHARE_DATADIR PATH_SEPARATOR "tor" PATH_SEPARATOR "geoip"),
nils's avatar
nils committed
252
253
  V(GeoIPv6File,                 FILENAME,
    SHARE_DATADIR PATH_SEPARATOR "tor" PATH_SEPARATOR "geoip6"),
254
#endif
255
  OBSOLETE("GiveGuardFlagTo_CVE_2011_2768_VulnerableRelays"),
256
  OBSOLETE("Group"),
257
  V(HardwareAccel,               BOOL,     "0"),
258
  V(HeartbeatPeriod,             INTERVAL, "6 hours"),
259
260
  V(AccelName,                   STRING,   NULL),
  V(AccelDir,                    FILENAME, NULL),
261
  V(HashedControlPassword,       LINELIST, NULL),
262
  V(HidServDirectoryV2,          BOOL,     "1"),
Nick Mathewson's avatar
Nick Mathewson committed
263
  VAR("HiddenServiceDir",    LINELIST_S, RendConfigLines,    NULL),
264
265
  OBSOLETE("HiddenServiceExcludeNodes"),
  OBSOLETE("HiddenServiceNodes"),
Nick Mathewson's avatar
Nick Mathewson committed
266
267
  VAR("HiddenServiceOptions",LINELIST_V, RendConfigLines,    NULL),
  VAR("HiddenServicePort",   LINELIST_S, RendConfigLines,    NULL),
268
  VAR("HiddenServiceVersion",LINELIST_S, RendConfigLines,    NULL),
269
  VAR("HiddenServiceAuthorizeClient",LINELIST_S,RendConfigLines, NULL),
270
  V(HidServAuth,                 LINELIST, NULL),
271
  V(HSAuthoritativeDir,          BOOL,     "0"),
272
  OBSOLETE("HSAuthorityRecordStats"),
273
  V(CloseHSClientCircuitsImmediatelyOnTimeout, BOOL, "0"),
274
  V(CloseHSServiceRendCircuitsImmediatelyOnTimeout, BOOL, "0"),
275
276
277
278
  V(HTTPProxy,                   STRING,   NULL),
  V(HTTPProxyAuthenticator,      STRING,   NULL),
  V(HTTPSProxy,                  STRING,   NULL),
  V(HTTPSProxyAuthenticator,     STRING,   NULL),
279
  //  V(IPv6EXit,                    BOOL,     "0"),
280
  VAR("ServerTransportPlugin",   LINELIST, ServerTransportPlugin,  NULL),
281
282
283
284
  V(Socks4Proxy,                 STRING,   NULL),
  V(Socks5Proxy,                 STRING,   NULL),
  V(Socks5ProxyUsername,         STRING,   NULL),
  V(Socks5ProxyPassword,         STRING,   NULL),
285
  OBSOLETE("IgnoreVersion"),
286
287
  V(KeepalivePeriod,             INTERVAL, "5 minutes"),
  VAR("Log",                     LINELIST, Logs,             NULL),
288
  V(LogMessageDomains,           BOOL,     "0"),
289
  OBSOLETE("LinkPadding"),
290
291
  OBSOLETE("LogLevel"),
  OBSOLETE("LogFile"),
292
  V(LogTimeGranularity,          MSEC_INTERVAL, "1 second"),
293
  V(LongLivedPorts,              CSV,
Nick Mathewson's avatar
Nick Mathewson committed
294
        "21,22,706,1863,5050,5190,5222,5223,6523,6667,6697,8300"),
295
296
297
  VAR("MapAddress",              LINELIST, AddressMap,           NULL),
  V(MaxAdvertisedBandwidth,      MEMUNIT,  "1 GB"),
  V(MaxCircuitDirtiness,         INTERVAL, "10 minutes"),
298
  V(MaxClientCircuitsPending,    UINT,     "32"),
299
  V(MaxOnionsPending,            UINT,     "100"),
300
  OBSOLETE("MonthlyAccountingStart"),
301
302
  V(MyFamily,                    STRING,   NULL),
  V(NewCircuitPeriod,            INTERVAL, "30 seconds"),
303
  VAR("NamingAuthoritativeDirectory",BOOL, NamingAuthoritativeDir, "0"),
304
  V(NATDListenAddress,           LINELIST, NULL),
305
  VPORT(NATDPort,                    LINELIST, NULL),
306
  V(Nickname,                    STRING,   NULL),
307
  V(WarnUnsafeSocks,              BOOL,     "1"),
Sebastian Hahn's avatar
Sebastian Hahn committed
308
  OBSOLETE("NoPublish"),
309
  VAR("NodeFamily",              LINELIST, NodeFamilies,         NULL),
310
  V(NumCPUs,                     UINT,     "0"),
311
312
  V(NumEntryGuards,              UINT,     "3"),
  V(ORListenAddress,             LINELIST, NULL),
313
  VPORT(ORPort,                      LINELIST, NULL),
314
  V(OutboundBindAddress,         LINELIST,   NULL),
315
316
317
318
319
320
321

  V(PathBiasCircThreshold,       INT,      "-1"),
  V(PathBiasNoticeRate,          DOUBLE,   "-1"),
  V(PathBiasDisableRate,         DOUBLE,   "-1"),
  V(PathBiasScaleThreshold,      INT,      "-1"),
  V(PathBiasScaleFactor,         INT,      "-1"),

322
  OBSOLETE("PathlenCoinWeight"),
323
324
  V(PerConnBWBurst,              MEMUNIT,  "0"),
  V(PerConnBWRate,               MEMUNIT,  "0"),
325
  V(PidFile,                     STRING,   NULL),
326
  V(TestingTorNetwork,           BOOL,     "0"),
327
  V(OptimisticData,              AUTOBOOL, "auto"),
328
329
  V(PortForwarding,              BOOL,     "0"),
  V(PortForwardingHelper,        FILENAME, "tor-fw-helper"),
Roger Dingledine's avatar
Roger Dingledine committed
330
  V(PreferTunneledDirConns,      BOOL,     "1"),
331
  V(ProtocolWarnings,            BOOL,     "0"),
332
  V(PublishServerDescriptor,     CSV,      "1"),
333
334
335
336
337
338
339
  V(PublishHidServDescriptors,   BOOL,     "1"),
  V(ReachableAddresses,          LINELIST, NULL),
  V(ReachableDirAddresses,       LINELIST, NULL),
  V(ReachableORAddresses,        LINELIST, NULL),
  V(RecommendedVersions,         LINELIST, NULL),
  V(RecommendedClientVersions,   LINELIST, NULL),
  V(RecommendedServerVersions,   LINELIST, NULL),
340
  OBSOLETE("RedirectExit"),
341
  V(RefuseUnknownExits,          AUTOBOOL, "auto"),
342
  V(RejectPlaintextPorts,        CSV,      ""),
343
344
  V(RelayBandwidthBurst,         MEMUNIT,  "0"),
  V(RelayBandwidthRate,          MEMUNIT,  "0"),
345
346
  OBSOLETE("RendExcludeNodes"),
  OBSOLETE("RendNodes"),
347
348
  V(RendPostPeriod,              INTERVAL, "1 hour"),
  V(RephistTrackTime,            INTERVAL, "24 hours"),
349
  OBSOLETE("RouterFile"),
350
  V(RunAsDaemon,                 BOOL,     "0"),
351
352
//  V(RunTesting,                  BOOL,     "0"),
  OBSOLETE("RunTesting"), // currently unused
353
  V(SafeLogging,                 STRING,   "1"),
354
  V(SafeSocks,                   BOOL,     "0"),
355
  V(ServerDNSAllowBrokenConfig,  BOOL,     "1"),
356
357
  V(ServerDNSAllowNonRFC953Hostnames, BOOL,"0"),
  V(ServerDNSDetectHijacking,    BOOL,     "1"),
358
  V(ServerDNSRandomizeCase,      BOOL,     "1"),
359
360
361
  V(ServerDNSResolvConfFile,     STRING,   NULL),
  V(ServerDNSSearchDomains,      BOOL,     "0"),
  V(ServerDNSTestAddresses,      CSV,
362
      "www.google.com,www.mit.edu,www.yahoo.com,www.slashdot.org"),
363
364
365
  V(ShutdownWaitLength,          INTERVAL, "30 seconds"),
  V(SocksListenAddress,          LINELIST, NULL),
  V(SocksPolicy,                 LINELIST, NULL),
366
  VPORT(SocksPort,                   LINELIST, NULL),
367
  V(SocksTimeout,                INTERVAL, "2 minutes"),
368
  OBSOLETE("StatusFetchPeriod"),
369
  V(StrictNodes,                 BOOL,     "0"),
370
  OBSOLETE("SysLog"),
371
  V(TestSocks,                   BOOL,     "0"),
372
  OBSOLETE("TestVia"),
373
  V(TokenBucketRefillInterval,   MSEC_INTERVAL, "100 msec"),
374
  V(Tor2webMode,                 BOOL,     "0"),
375
376
  V(TrackHostExits,              CSV,      NULL),
  V(TrackHostExitsExpire,        INTERVAL, "30 minutes"),
377
  OBSOLETE("TrafficShaping"),
378
  V(TransListenAddress,          LINELIST, NULL),
379
  VPORT(TransPort,                   LINELIST, NULL),
Roger Dingledine's avatar
Roger Dingledine committed
380
  V(TunnelDirConns,              BOOL,     "1"),
381
  V(UpdateBridgesFromAuthority,  BOOL,     "0"),
382
  V(UseBridges,                  BOOL,     "0"),
383
  V(UseEntryGuards,              BOOL,     "1"),
384
  V(UseMicrodescriptors,         AUTOBOOL, "auto"),
385
  V(User,                        STRING,   NULL),
386
  V(UserspaceIOCPBuffers,        BOOL,     "0"),
387
  VAR("V1AuthoritativeDirectory",BOOL, V1AuthoritativeDir,   "0"),
388
  VAR("V2AuthoritativeDirectory",BOOL, V2AuthoritativeDir,   "0"),
389
  VAR("V3AuthoritativeDirectory",BOOL, V3AuthoritativeDir,   "0"),
390
391
392
  V(TestingV3AuthInitialVotingInterval, INTERVAL, "30 minutes"),
  V(TestingV3AuthInitialVoteDelay, INTERVAL, "5 minutes"),
  V(TestingV3AuthInitialDistDelay, INTERVAL, "5 minutes"),
393
394
395
396
  V(V3AuthVotingInterval,        INTERVAL, "1 hour"),
  V(V3AuthVoteDelay,             INTERVAL, "5 minutes"),
  V(V3AuthDistDelay,             INTERVAL, "5 minutes"),
  V(V3AuthNIntervalsValid,       UINT,     "3"),
397
  V(V3AuthUseLegacyKey,          BOOL,     "0"),
398
  V(V3BandwidthsFile,            FILENAME, NULL),
399
  VAR("VersioningAuthoritativeDirectory",BOOL,VersioningAuthoritativeDir, "0"),
400
  V(VirtualAddrNetwork,          STRING,   "127.192.0.0/10"),
401
  V(WarnPlaintextPorts,          CSV,      "23,109,110,143"),
402
  V(UseFilteringSSLBufferevents, BOOL,    "0"),
403
  VAR("__ReloadTorrcOnSIGHUP",   BOOL,  ReloadTorrcOnSIGHUP,      "1"),
404
405
406
  VAR("__AllDirActionsPrivate",  BOOL,  AllDirActionsPrivate,     "0"),
  VAR("__DisablePredictedCircuits",BOOL,DisablePredictedCircuits, "0"),
  VAR("__LeaveStreamsUnattached",BOOL,  LeaveStreamsUnattached,   "0"),
407
408
  VAR("__HashedControlSessionPassword", LINELIST, HashedControlSessionPassword,
      NULL),
409
  VAR("__OwningControllerProcess",STRING,OwningControllerProcess, NULL),
410
  V(MinUptimeHidServDirectoryV2, INTERVAL, "25 hours"),
411
  V(VoteOnHidServDirectoriesV2,  BOOL,     "1"),
412
  VAR("___UsingTestNetworkDefaults", BOOL, UsingTestNetworkDefaults_, "0"),
413

414
  { NULL, CONFIG_TYPE_OBSOLETE, 0, NULL }
415
};
416

417
418
/** Override default values with these if the user sets the TestingTorNetwork
 * option. */
419
static const config_var_t testing_tor_network_defaults[] = {
420
  V(ServerDNSAllowBrokenConfig,  BOOL,     "1"),
421
422
423
424
425
426
  V(DirAllowPrivateAddresses,    BOOL,     "1"),
  V(EnforceDistinctSubnets,      BOOL,     "0"),
  V(AssumeReachable,             BOOL,     "1"),
  V(AuthDirMaxServersPerAddr,    UINT,     "0"),
  V(AuthDirMaxServersPerAuthAddr,UINT,     "0"),
  V(ClientDNSRejectInternalAddresses, BOOL,"0"),
427
  V(ClientRejectInternalAddresses, BOOL,   "0"),
428
  V(CountPrivateBandwidth,       BOOL,     "1"),
429
  V(ExitPolicyRejectPrivate,     BOOL,     "0"),
430
  V(ExtendAllowPrivateAddresses, BOOL,     "1"),
431
432
433
  V(V3AuthVotingInterval,        INTERVAL, "5 minutes"),
  V(V3AuthVoteDelay,             INTERVAL, "20 seconds"),
  V(V3AuthDistDelay,             INTERVAL, "20 seconds"),
434
435
436
437
438
  V(TestingV3AuthInitialVotingInterval, INTERVAL, "5 minutes"),
  V(TestingV3AuthInitialVoteDelay, INTERVAL, "20 seconds"),
  V(TestingV3AuthInitialDistDelay, INTERVAL, "20 seconds"),
  V(TestingAuthDirTimeToLearnReachability, INTERVAL, "0 minutes"),
  V(TestingEstimatedDescriptorPropagationTime, INTERVAL, "0 minutes"),
439
  V(MinUptimeHidServDirectoryV2, INTERVAL, "0 minutes"),
440
  VAR("___UsingTestNetworkDefaults", BOOL, UsingTestNetworkDefaults_, "1"),
441

442
443
  { NULL, CONFIG_TYPE_OBSOLETE, 0, NULL }
};
444

445
#undef VAR
446
#undef V
447
448
#undef OBSOLETE

449
#ifdef _WIN32
450
451
static char *get_windows_conf_root(void);
#endif
452
453
static int options_validate(or_options_t *old_options,
                            or_options_t *options,
454
                            int from_setconf, char **msg);
455
456
457
458
static int options_act_reversible(const or_options_t *old_options, char **msg);
static int options_act(const or_options_t *old_options);
static int options_transition_allowed(const or_options_t *old,
                                      const or_options_t *new,
459
                                      char **msg);
460
461
462
463
static int options_transition_affects_workers(
      const or_options_t *old_options, const or_options_t *new_options);
static int options_transition_affects_descriptor(
      const or_options_t *old_options, const or_options_t *new_options);
464
static int check_nickname_list(const char *lst, const char *name, char **msg);
465

466
static int parse_bridge_line(const char *line, int validate_only);
George Kadianakis's avatar
George Kadianakis committed
467
static int parse_client_transport_line(const char *line, int validate_only);
468
469

static int parse_server_transport_line(const char *line, int validate_only);
470
static int parse_dir_server_line(const char *line,
471
                                 dirinfo_type_t required_type,
472
                                 int validate_only);
473
static void port_cfg_free(port_cfg_t *port);
474
static int parse_ports(or_options_t *options, int validate_only,
475
                              char **msg_out, int *n_ports_out);
476
477
478
static int check_server_ports(const smartlist_t *ports,
                              const or_options_t *options);

479
static int validate_data_directory(or_options_t *options);
480
481
static int write_configuration_file(const char *fname,
                                    const or_options_t *options);
482
static int options_init_logs(or_options_t *options, int validate_only);
483

484
static void init_libevent(const or_options_t *options);
485
static int opt_streq(const char *s1, const char *s2);
486
487
static int parse_outbound_addresses(or_options_t *options, int validate_only,
                                    char **msg);
Linus Nordberg's avatar
Linus Nordberg committed
488
489
static void config_maybe_load_geoip_files_(const or_options_t *options,
                                           const or_options_t *old_options);
490

491
/** Magic value for or_options_t. */
492
493
#define OR_OPTIONS_MAGIC 9090909

494
/** Configuration format for or_options_t. */
495
static config_format_t options_format = {
496
497
  sizeof(or_options_t),
  OR_OPTIONS_MAGIC,
498
499
500
  STRUCT_OFFSET(or_options_t, magic_),
  option_abbrevs_,
  option_vars_,
501
  (validate_fn_t)options_validate,
502
  NULL
503
504
};

505
506
507
508
509
/*
 * Functions to read and write the global options pointer.
 */

/** Command-line and config-file options. */
510
static or_options_t *global_options = NULL;
511
512
/** The fallback options_t object; this is where we look for options not
 * in torrc before we fall back to Tor's defaults. */
513
static or_options_t *global_default_options = NULL;
Roger Dingledine's avatar
Roger Dingledine committed
514
/** Name of most recently read torrc file. */
515
static char *torrc_fname = NULL;
516
/** Name of the most recently read torrc-defaults file.*/
517
static char *torrc_defaults_fname;
518
519
/** Configuration Options set by command line. */
static config_line_t *global_cmdline_options = NULL;
Roger Dingledine's avatar
Roger Dingledine committed
520
/** Contents of most recently read DirPortFrontPage file. */
521
static char *global_dirfrontpagecontents = NULL;
522
523
/** List of port_cfg_t for all configured ports. */
static smartlist_t *configured_ports = NULL;
524
525
526
527
528
529
530

/** Return the contents of our frontpage string, or NULL if not configured. */
const char *
get_dirportfrontpage(void)
{
  return global_dirfrontpagecontents;
}
531

532
533
/** Return the currently configured options. */
or_options_t *
534
get_options_mutable(void)
535
{
536
537
538
  tor_assert(global_options);
  return global_options;
}
539

540
541
542
543
544
545
546
/** Returns the currently configured options */
const or_options_t *
get_options(void)
{
  return get_options_mutable();
}

547
548
/** Change the current global options to contain <b>new_val</b> instead of
 * their current value; take action based on the new value; free the old value
549
 * as necessary.  Returns 0 on success, -1 on failure.
550
 */
551
int
552
set_options(or_options_t *new_val, char **msg)
553
{
554
555
556
  int i;
  smartlist_t *elements;
  config_line_t *line;
557
  or_options_t *old_options = global_options;
558
  global_options = new_val;
559
560
  /* Note that we pass the *old* options below, for comparison. It
   * pulls the new options directly out of global_options. */
561
562
  if (options_act_reversible(old_options, msg)<0) {
    tor_assert(*msg);
563
564
565
    global_options = old_options;
    return -1;
  }
566
  if (options_act(old_options) < 0) { /* acting on the options failed. die. */
567
    log_err(LD_BUG,
Roger Dingledine's avatar
Roger Dingledine committed
568
            "Acting on config options left us in a broken state. Dying.");
569
570
    exit(1);
  }
571
572
  /* Issues a CONF_CHANGED event to notify controller of the change. If Tor is
   * just starting up then the old_options will be undefined. */
573
  if (old_options && old_options != global_options) {
574
    elements = smartlist_new();
575
    for (i=0; options_format.vars[i].name; ++i) {
576
577
      const config_var_t *var = &options_format.vars[i];
      const char *var_name = var->name;
578
579
580
581
      if (var->type == CONFIG_TYPE_LINELIST_S ||
          var->type == CONFIG_TYPE_OBSOLETE) {
        continue;
      }
582
583
584
      if (!config_is_same(&options_format, new_val, old_options, var_name)) {
        line = config_get_assigned_option(&options_format, new_val,
                                          var_name, 1);
585
586
587

        if (line) {
          for (; line; line = line->next) {
588
589
            smartlist_add(elements, line->key);
            smartlist_add(elements, line->value);
590
591
          }
        } else {
592
          smartlist_add(elements, (char*)options_format.vars[i].name);
593
          smartlist_add(elements, NULL);
594
595
596
        }
      }
    }
597
    control_event_conf_changed(elements);
598
599
    smartlist_free(elements);
  }
600
601
602

  if (old_options != global_options)
    config_free(&options_format, old_options);
603
604

  return 0;
605
606
}

607
extern const char tor_git_revision[]; /* from tor_main.c */
608

609
/** The version of this Tor process, as parsed. */
610
static char *the_tor_version = NULL;
Nick Mathewson's avatar
Nick Mathewson committed
611
612
/** A shorter version of this Tor process's version, for export in our router
 *  descriptor.  (Does not include the git version, if any.) */
613
static char *the_short_tor_version = NULL;
614

615
/** Return the current Tor version. */
616
617
618
const char *
get_version(void)
{
619
  if (the_tor_version == NULL) {
620
    if (strlen(tor_git_revision)) {
621
622
      tor_asprintf(&the_tor_version, "%s (git-%s)", get_short_version(),
                   tor_git_revision);
623
    } else {
624
      the_tor_version = tor_strdup(get_short_version());
625
626
    }
  }
627
  return the_tor_version;
628
629
}

630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
/** Return the current Tor version, without any git tag. */
const char *
get_short_version(void)
{

  if (the_short_tor_version == NULL) {
#ifdef TOR_BUILD_TAG
    tor_asprintf(&the_short_tor_version, "%s (%s)", VERSION, TOR_BUILD_TAG);
#else
    the_short_tor_version = tor_strdup(VERSION);
#endif
  }
  return the_short_tor_version;
}

645
646
647
648
649
/** Release additional memory allocated in options
 */
static void
or_options_free(or_options_t *options)
{
650
651
652
  if (!options)
    return;

653
  routerset_free(options->ExcludeExitNodesUnion_);
654
655
656
657
658
  if (options->NodeFamilySets) {
    SMARTLIST_FOREACH(options->NodeFamilySets, routerset_t *,
                      rs, routerset_free(rs));
    smartlist_free(options->NodeFamilySets);
  }
659
  tor_free(options->BridgePassword_AuthDigest_);
660
661
662
  config_free(&options_format, options);
}

663
664
/** Release all memory and resources held by global configuration structures.
 */
665
666
667
void
config_free_all(void)
{
668
669
  or_options_free(global_options);
  global_options = NULL;
670
671
  or_options_free(global_default_options);
  global_default_options = NULL;
672
673
674
675

  config_free_lines(global_cmdline_options);
  global_cmdline_options = NULL;

676
677
  if (configured_ports) {
    SMARTLIST_FOREACH(configured_ports,
678
                      port_cfg_t *, p, tor_free(p));
679
680
    smartlist_free(configured_ports);
    configured_ports = NULL;
681
682
  }

683
  tor_free(torrc_fname);
684
  tor_free(torrc_defaults_fname);
685
  tor_free(the_tor_version);
686
  tor_free(global_dirfrontpagecontents);
687
688
689

  tor_free(the_short_tor_version);
  tor_free(the_tor_version);
690
691
}

692
693
694
695
696
/** Make <b>address</b> -- a piece of information related to our operation as
 * a client -- safe to log according to the settings in options->SafeLogging,
 * and return it.
 *
 * (We return "[scrubbed]" if SafeLogging is "1", and address otherwise.)
697
698
 */
const char *
699
safe_str_client(const char *address)
700
{
701
  tor_assert(address);
702
  if (get_options()->SafeLogging_ == SAFELOG_SCRUB_ALL)
703
704
705
706
707
    return "[scrubbed]";
  else
    return address;
}

708
709
710
711
712
713
/** Make <b>address</b> -- a piece of information of unspecified sensitivity
 * -- safe to log according to the settings in options->SafeLogging, and
 * return it.
 *
 * (We return "[scrubbed]" if SafeLogging is anything besides "0", and address
 * otherwise.)
714
715
 */
const char *
716
717
safe_str(const char *address)
{
718
  tor_assert(address);
719
  if (get_options()->SafeLogging_ != SAFELOG_SCRUB_NONE)
720
721
722
723
724
    return "[scrubbed]";
  else
    return address;
}

725
/** Equivalent to escaped(safe_str_client(address)).  See reentrancy note on
726
727
 * escaped(): don't use this outside the main thread, or twice in the same
 * log statement. */
728
const char *
729
escaped_safe_str_client(const char *address)
730
{
731
  if (get_options()->SafeLogging_ == SAFELOG_SCRUB_ALL)
732
733
734
735
736
    return "[scrubbed]";
  else
    return escaped(address);
}

737
/** Equivalent to escaped(safe_str(address)).  See reentrancy note on
738
739
 * escaped(): don't use this outside the main thread, or twice in the same
 * log statement. */
740
741
742
const char *
escaped_safe_str(const char *address)
{
743
  if (get_options()->SafeLogging_ != SAFELOG_SCRUB_NONE)
744
745
746
747
748
    return "[scrubbed]";
  else
    return escaped(address);
}

749
750
/** Add the default directory authorities directly into the trusted dir list,
 * but only add them insofar as they share bits with <b>type</b>. */
751
static void
752
add_default_trusted_dir_authorities(dirinfo_type_t type)
753
{
754
  int i;
755
  const char *dirservers[] = {
756
757
758
    "moria1 orport=9101 no-v2 "
      "v3ident=D586D18309DED4CD6D57C18FDB97EFA96D330566 "
      "128.31.0.39:9131 9695 DFC3 5FFE B861 329B 9F1A B04C 4639 7020 CE31",
759
    "tor26 v1 orport=443 v3ident=14C131DFC5C6F93646BE72FA1401C02A8DF2E8B4 "