Vidalia  0.3.1
ServerSettings.cpp
Go to the documentation of this file.
1 /*
2 ** This file is part of Vidalia, and is subject to the license terms in the
3 ** LICENSE file, found in the top level directory of this distribution. If you
4 ** did not receive the LICENSE file with this file, you may obtain it from the
5 ** Vidalia source package distributed by the Vidalia Project at
6 ** http://www.torproject.org/projects/vidalia.html. No part of Vidalia,
7 ** including this file, may be copied, modified, propagated, or distributed
8 ** except according to the terms described in the LICENSE file.
9 */
10 
11 /*
12 ** \file ServerSettings.cpp
13 ** \brief Settings for running a Tor server
14 */
15 
16 #include "config.h"
17 #include "ServerSettings.h"
18 #include "TorSettings.h"
19 #include "TorControl.h"
20 #ifdef USE_MINIUPNPC
21 #include "UPNPControl.h"
22 #endif
23 
24 #include "net.h"
25 #include "stringutil.h"
26 
27 #include <QHostInfo>
28 
29 /** Define the set of characters that are valid in a nickname. */
30 #define VALID_NICKNAME_CHARS \
31  "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
32 /** Define the maximum length of a server's nickname. */
33 #define MAX_NICKNAME_LEN 19
34 
35 /* Server configuration settings */
36 #define SETTING_ENABLED "Enabled"
37 #define SETTING_DIRMIRROR "DirectoryMirror"
38 #define SETTING_NICKNAME "Nickname"
39 #define SETTING_ORPORT "ORPort"
40 #define SETTING_DIRPORT "DirPort"
41 #define SETTING_CONTACT "ContactInfo"
42 #define SETTING_EXITPOLICY "ExitPolicy"
43 #define SETTING_BANDWIDTH_RATE "BandwidthRate"
44 #define SETTING_BANDWIDTH_BURST "BandwidthBurst"
45 #define SETTING_BRIDGE_RELAY "BridgeRelay"
46 #define SETTING_NONEXIT_RELAY "NonExitRelay"
47 #define SETTING_ENABLE_UPNP "EnableUPnP"
48 #define SETTING_RELAY_BANDWIDTH_RATE "RelayBandwidthRate"
49 #define SETTING_RELAY_BANDWIDTH_BURST "RelayBandwidthBurst"
50 #define SETTING_PUBLISH_SERVER_DESCRIPTOR "PublishServerDescriptor"
51 
52 
53 /** Constructor.
54  * \param torControl a TorControl object used to read and apply the server
55  * configuration settings.
56  */
58 : AbstractTorSettings("Server", torControl)
59 {
62 #if defined(Q_OS_WIN32)
64 #else
66 #endif
68  setDefault(SETTING_NICKNAME, "Unnamed");
69  setDefault(SETTING_CONTACT, "<you@example.com>");
75  ExitPolicy(ExitPolicy::Middleman).toString());
79 }
80 
81 /** Returns a QHash of Tor-recognizable configuratin keys to their current
82  * values. */
83 QHash<QString, QString>
85 {
86  QHash<QString, QString> conf;
87  quint32 torVersion = torControl()->getTorVersion();
88 
89  /* Server Nickname */
90  conf.insert(SETTING_NICKNAME,
92  : ""));
93  /* Server ORPort */
94  conf.insert(SETTING_ORPORT,
96  : "0"));
97  /* Server DirPort */
98  conf.insert(SETTING_DIRPORT,
100  : "0"));
101  /* Server Exit Policy */
102  conf.insert(SETTING_EXITPOLICY,
103  ((isBridgeEnabled() || isNonExitEnabled()) ? "reject *:*"
104  : localValue(SETTING_EXITPOLICY).toString()));
105 
106  /* Server bandwidth settings */
107  conf.insert((torVersion >= 0x020001 ? SETTING_RELAY_BANDWIDTH_RATE
109  QString::number(localValue(SETTING_BANDWIDTH_RATE).toUInt()) + " bytes");
110  conf.insert((torVersion >= 0x020001 ? SETTING_RELAY_BANDWIDTH_BURST
112  QString::number(localValue(SETTING_BANDWIDTH_BURST).toUInt()) + " bytes");
113 
114  /* Server Contact Information */
115  QString contact =
116  localValue(SETTING_CONTACT).toString().trimmed();
117  QString defaultContact = defaultValue(SETTING_CONTACT).toString();
118  if ((contact == defaultContact) ||
119  (contact == scrub_email_addr(defaultContact))) {
120  /* Only set the contact info if they put something non-default there */
121  contact = "";
122  }
123  conf.insert(SETTING_CONTACT, scrub_email_addr(contact));
124 
125  /* Set if we're a bridge relay */
126  if (isBridgeEnabled()) {
127  conf.insert(SETTING_BRIDGE_RELAY, "1");
129  publishServerDescriptor() ? "1" : "0");
130  } else {
131  conf.insert(SETTING_BRIDGE_RELAY, "0");
132  conf.insert(SETTING_PUBLISH_SERVER_DESCRIPTOR, "1");
133  }
134  return conf;
135 }
136 
137 /** Applies the current server configuration settings to Tor. If <b>errmsg</b>
138  * is specified and an error occurs while applying the settings, it will be
139  * set to a string describing the error. */
140 bool
141 ServerSettings::apply(QString *errmsg)
142 {
143  bool rc;
144 
146 
147  if (isServerEnabled()) {
148  rc = torControl()->setConf(confValues(), errmsg);
149  } else {
150  QStringList resetKeys;
151  quint32 torVersion = torControl()->getTorVersion();
152  resetKeys << SETTING_ORPORT
153  << SETTING_NICKNAME
154  << SETTING_DIRPORT
155  << SETTING_CONTACT
159  if (torVersion >= 0x020001) {
160  resetKeys << SETTING_RELAY_BANDWIDTH_RATE
162  } else {
163  resetKeys << SETTING_BANDWIDTH_RATE
165  }
166  rc = torControl()->resetConf(resetKeys, errmsg);
167  }
168  return rc;
169 }
170 
171 /* TODO: We should call this periodically, in case the router gets rebooted or forgets its UPnP settings */
172 /* TODO: Remove port forwarding when Tor is shutdown or the ORPort changes */
173 /* TODO: init_upnp() will block for up to 2 seconds. We should fire off a thread */
174 
175 /** Configure UPnP device to forward DirPort and ORPort. If enable is
176  * true, will forward ORPort and DirPort; otherwise will remove exising
177  * port mappings */
178 void
180 {
181 #ifdef USE_MINIUPNPC
182  quint16 ORPort, DirPort;
183 
184  // This is how the tickbox should control UPNP
185  if (!isUpnpEnabled())
186  return;
187 
188  ORPort = getORPort();
189  if (!isServerEnabled())
190  ORPort = 0;
191 
192  DirPort = getDirPort();
193  if (!isServerEnabled() || !isDirectoryMirror())
194  DirPort = 0;
195 
196  UPNPControl *control = UPNPControl::instance();
197  control->setDesiredState(DirPort, ORPort);
198 #endif
199 }
200 
201 void
203 {
204 #ifdef USE_MINIUPNPC
206 #endif
207 }
208 
209 /** Virtual method called when we retrieve a server-related setting from Tor.
210  * Currently this just translates BandwidthFoo to RelayBandwidthFoo when
211  * appropriate. */
212 QVariant
213 ServerSettings::torValue(const QString &key) const
214 {
215  if (torControl()->getTorVersion() >= 0x020001) {
216  if (key == SETTING_BANDWIDTH_RATE)
218  else if (key == SETTING_BANDWIDTH_BURST)
220  }
221  return AbstractTorSettings::torValue(key);
222 }
223 
224 /** Enables or disables running Tor as a server.
225  * \param enable Whether to enable or disable the Tor server.
226  */
227 void
229 {
230  setValue(SETTING_ENABLED, enable);
231 }
232 
233 /** Returns true if Tor is currently configured to run as a Tor server. If Tor
234  * is running, we will check whether it has an ORPort defined. Otherwise, we
235  * will use our saved settings. */
236 bool
238 {
239  QString orPort;
241  if (torControl()->getConf(SETTING_ORPORT, orPort))
242  return (orPort.toUInt() > 0);
243  }
244  return localValue(SETTING_ENABLED).toBool();
245 }
246 
247 /** Sets to <b>enabled</b> whether Tor should be a bridge node when acting as
248  * a server. */
249 void
251 {
252  setValue(SETTING_BRIDGE_RELAY, enabled);
253 }
254 
255 /** Returns true if Tor is configured to act as a bridge node. */
256 bool
258 {
259  return value(SETTING_BRIDGE_RELAY).toBool() && isServerEnabled();
260 }
261 
262 /** Sets to <b>enabled</b> whether Tor should be a non-exit node when acting as
263  * a server. */
264 void
266 {
268 }
269 
270 /** Returns true if Tor is configured to act as a non-exit node. */
271 bool
273 {
274  return value(SETTING_NONEXIT_RELAY).toBool() && isServerEnabled();
275 }
276 
277 /** Sets the server's ORPort. */
278 void
280 {
281  setValue(SETTING_ORPORT, orPort);
282 }
283 
284 /** Gets the server's current ORPort setting. */
285 quint16
287 {
288  return (quint16)value(SETTING_ORPORT).toUInt();
289 }
290 
291 /** Sets the server's current DirPort. */
292 void
294 {
295  setValue(SETTING_DIRPORT, dirPort);
296 }
297 
298 /** Gets the server's current DirPort. */
299 quint16
301 {
302  return (quint16)value(SETTING_DIRPORT).toUInt();
303 }
304 
305 /** Sets the server's nickname. */
306 void
308 {
309  setValue(SETTING_NICKNAME, nickname);
310 }
311 
312 /** Gets the server's nickname. */
313 QString
315 {
316  QString nickname = value(SETTING_NICKNAME).toString();
317  /* Ensure the nickname contains only valid characters and is not too long. */
318  return ensure_valid_chars(nickname,
320 }
321 
322 /** Sets the server's contact information. */
323 void
325 {
326  setValue(SETTING_CONTACT, contact);
327 }
328 
329 /** Gets the server's contact information. */
330 QString
332 {
333  return value(SETTING_CONTACT).toString();
334 }
335 
336 /** Returns whether this server will act as a directory mirror or not. */
337 bool
339 {
340  return localValue(SETTING_DIRMIRROR).toBool();
341 }
342 
343 /** Sets whether this server will act as a directory mirror. */
344 void
346 {
347  setValue(SETTING_DIRMIRROR, mirror);
348 }
349 
350 /** Returns the exit policy for this server. */
353 {
354  return ExitPolicy(value(SETTING_EXITPOLICY).toString());
355 }
356 
357 /** Sets the exit policy for this server. */
358 void
360 {
361  setValue(SETTING_EXITPOLICY, exitPolicy.toString());
362 }
363 
364 /** Returns the long-term average bandwidth rate (in KB/s) for this server. */
365 quint32
367 {
368  return value(SETTING_BANDWIDTH_RATE).toUInt();
369 }
370 
371 /** Sets the long-term average bandwidth rate (in KB/s) for this server. */
372 void
374 {
376 }
377 
378 /** Returns the maximum bandwidth burst rate (in KB/s) for this server. */
379 quint32
381 {
382  return value(SETTING_BANDWIDTH_BURST).toUInt();
383 }
384 
385 /** Sets the maximum bandwidth burst rate (in KB/s) for this server. */
386 void
388 {
390 }
391 
392 /** Sets whether the user's server descriptor will be published or not.
393  * Currently this only affects publishing of bridge descriptors. If the
394  * user is running a normal relay, its descriptor will always be
395  * published regardless of this setting. */
396 void
398 {
399  if (publish)
401  else
403 }
404 
405 /** Returns true if the user's server descriptor will be published to the
406  * appropriate authorities. */
407 bool
409 {
410  return (value(SETTING_PUBLISH_SERVER_DESCRIPTOR).toString() != "0");
411 }
412 
413 /** Returns true if UPnP support is available and enabled. */
414 bool
416 {
417 #if defined(USE_MINIUPNPC)
418  return localValue(SETTING_ENABLE_UPNP).toBool();
419 #else
420  return false;
421 #endif
422 }
423 
424 /** Sets whether Vidalia should try to configure port forwarding using UPnP.
425  * If Vidalia was compiled without UPnP support, this method has no effect. */
426 void
428 {
429 #if defined(USE_MINIUPNPC)
430  setValue(SETTING_ENABLE_UPNP, enabled);
431 #endif
432 }
433 
ServerSettings::getContactInfo
QString getContactInfo()
Definition: ServerSettings.cpp:331
AbstractTorSettings::localValue
virtual QVariant localValue(const QString &key) const
Definition: AbstractTorSettings.cpp:88
ServerSettings::setPublishServerDescriptor
void setPublishServerDescriptor(bool publish)
Definition: ServerSettings.cpp:397
SETTING_CONTACT
#define SETTING_CONTACT
Definition: ServerSettings.cpp:41
ServerSettings::ServerSettings
ServerSettings(TorControl *torControl)
Definition: ServerSettings.cpp:57
UPNPControl::instance
static UPNPControl * instance()
Definition: UPNPControl.cpp:31
ServerSettings::setNonExitEnabled
void setNonExitEnabled(bool enable)
Definition: ServerSettings.cpp:265
ServerSettings::getBandwidthBurstRate
quint32 getBandwidthBurstRate()
Definition: ServerSettings.cpp:380
ServerSettings::cleanupPortForwarding
void cleanupPortForwarding()
Definition: ServerSettings.cpp:202
ServerSettings::getBandwidthAvgRate
quint32 getBandwidthAvgRate()
Definition: ServerSettings.cpp:366
ServerSettings::setExitPolicy
void setExitPolicy(ExitPolicy &policy)
Definition: ServerSettings.cpp:359
ServerSettings::setORPort
void setORPort(quint16 orPort)
Definition: ServerSettings.cpp:279
isConnected
stop errmsg isConnected()
ServerSettings::getExitPolicy
ExitPolicy getExitPolicy()
Definition: ServerSettings.cpp:352
TorControl
Definition: TorControl.h:49
SETTING_DIRPORT
#define SETTING_DIRPORT
Definition: ServerSettings.cpp:40
ServerSettings::isUpnpEnabled
bool isUpnpEnabled()
Definition: ServerSettings.cpp:415
ServerSettings::isBridgeEnabled
bool isBridgeEnabled()
Definition: ServerSettings.cpp:257
ServerSettings::isNonExitEnabled
bool isNonExitEnabled()
Definition: ServerSettings.cpp:272
TorControl::resetConf
bool resetConf(QStringList keys, QString *errmsg=0)
Definition: TorControl.cpp:929
ServerSettings::isDirectoryMirror
bool isDirectoryMirror()
Definition: ServerSettings.cpp:338
QVariant
stop errmsg QVariant
Definition: TorControlPrototype.cpp:180
ServerSettings::setDirPort
void setDirPort(quint16 dirPort)
Definition: ServerSettings.cpp:293
ServerSettings::setServerEnabled
void setServerEnabled(bool enable)
Definition: ServerSettings.cpp:228
VSettings::defaultValue
QVariant defaultValue(const QString &key) const
Definition: VSettings.cpp:79
SETTING_BANDWIDTH_BURST
#define SETTING_BANDWIDTH_BURST
Definition: ServerSettings.cpp:44
SETTING_ORPORT
#define SETTING_ORPORT
Definition: ServerSettings.cpp:39
scrub_email_addr
QString scrub_email_addr(const QString &email)
Definition: stringutil.cpp:63
MAX_NICKNAME_LEN
#define MAX_NICKNAME_LEN
Definition: ServerSettings.cpp:33
ServerSettings::torValue
virtual QVariant torValue(const QString &key) const
Definition: ServerSettings.cpp:213
UPNPControl::cleanup
static void cleanup()
Definition: UPNPControl.cpp:68
TorControl::setConf
bool setConf(QHash< QString, QString > map, QString *errmsg=0)
Definition: TorControl.cpp:722
ServerSettings::isServerEnabled
bool isServerEnabled()
Definition: ServerSettings.cpp:237
AbstractTorSettings::torControl
TorControl * torControl() const
Definition: AbstractTorSettings.h:68
TorControl.h
SETTING_ENABLE_UPNP
#define SETTING_ENABLE_UPNP
Definition: ServerSettings.cpp:47
stringutil.h
ServerSettings::setBridgeEnabled
void setBridgeEnabled(bool enable)
Definition: ServerSettings.cpp:250
SETTING_PUBLISH_SERVER_DESCRIPTOR
#define SETTING_PUBLISH_SERVER_DESCRIPTOR
Definition: ServerSettings.cpp:50
ServerSettings::setDirectoryMirror
void setDirectoryMirror(bool mirror)
Definition: ServerSettings.cpp:345
AbstractTorSettings::changedSinceLastApply
virtual bool changedSinceLastApply() const
Definition: AbstractTorSettings.cpp:49
ServerSettings::setContactInfo
void setContactInfo(QString info)
Definition: ServerSettings.cpp:324
ServerSettings::getNickname
QString getNickname()
Definition: ServerSettings.cpp:314
ExitPolicy::toString
QString toString()
Definition: ExitPolicy.cpp:156
TorControl::getTorVersion
quint32 getTorVersion()
Definition: TorControl.cpp:667
ExitPolicy
Definition: ExitPolicy.h:26
SETTING_DIRMIRROR
#define SETTING_DIRMIRROR
Definition: ServerSettings.cpp:37
ServerSettings::setBandwidthBurstRate
void setBandwidthBurstRate(quint32 rate)
Definition: ServerSettings.cpp:387
ExitPolicy::Middleman
@ Middleman
Definition: ExitPolicy.h:32
getTorVersion
getTorVersion()
SETTING_NICKNAME
#define SETTING_NICKNAME
Definition: ServerSettings.cpp:38
ServerSettings::setUpnpEnabled
void setUpnpEnabled(bool enabled)
Definition: ServerSettings.cpp:427
VSettings::setDefault
void setDefault(const QString &key, const QVariant &val)
Definition: VSettings.cpp:71
ServerSettings::publishServerDescriptor
bool publishServerDescriptor() const
Definition: ServerSettings.cpp:408
SETTING_EXITPOLICY
#define SETTING_EXITPOLICY
Definition: ServerSettings.cpp:42
ServerSettings::configurePortForwarding
void configurePortForwarding()
Definition: ServerSettings.cpp:179
TorSettings.h
SETTING_RELAY_BANDWIDTH_BURST
#define SETTING_RELAY_BANDWIDTH_BURST
Definition: ServerSettings.cpp:49
SETTING_RELAY_BANDWIDTH_RATE
#define SETTING_RELAY_BANDWIDTH_RATE
Definition: ServerSettings.cpp:48
ServerSettings::getORPort
quint16 getORPort()
Definition: ServerSettings.cpp:286
ServerSettings.h
SETTING_NONEXIT_RELAY
#define SETTING_NONEXIT_RELAY
Definition: ServerSettings.cpp:46
config.h
AbstractTorSettings::setValue
virtual void setValue(const QString &key, const QVariant &value)
Definition: AbstractTorSettings.cpp:123
ServerSettings::getDirPort
quint16 getDirPort()
Definition: ServerSettings.cpp:300
ServerSettings::confValues
QHash< QString, QString > confValues()
Definition: ServerSettings.cpp:84
UPNPControl.h
SETTING_BRIDGE_RELAY
#define SETTING_BRIDGE_RELAY
Definition: ServerSettings.cpp:45
SETTING_ENABLED
#define SETTING_ENABLED
Definition: ServerSettings.cpp:36
ensure_valid_chars
QString ensure_valid_chars(const QString &str, const QString &validChars)
Definition: stringutil.cpp:49
ServerSettings::setNickname
void setNickname(QString nickname)
Definition: ServerSettings.cpp:307
AbstractTorSettings::torValue
virtual QVariant torValue(const QString &key) const
Definition: AbstractTorSettings.cpp:96
net.h
VALID_NICKNAME_CHARS
#define VALID_NICKNAME_CHARS
Definition: ServerSettings.cpp:30
UPNPControl::setDesiredState
void setDesiredState(quint16 desiredDirPort, quint16 desiredOrPort)
Definition: UPNPControl.cpp:89
AbstractTorSettings
Definition: AbstractTorSettings.h:22
quint32
quint32
Definition: TorControlPrototype.cpp:222
UPNPControl
Definition: UPNPControl.h:26
AbstractTorSettings::value
virtual QVariant value(const QString &key) const
Definition: AbstractTorSettings.cpp:113
ServerSettings::setBandwidthAvgRate
void setBandwidthAvgRate(quint32 rate)
Definition: ServerSettings.cpp:373
SETTING_BANDWIDTH_RATE
#define SETTING_BANDWIDTH_RATE
Definition: ServerSettings.cpp:43
ServerSettings::apply
bool apply(QString *errmsg=0)
Definition: ServerSettings.cpp:141