Vidalia
0.2.17
|
00001 /* 00002 ** This file is part of Vidalia, and is subject to the license terms in the 00003 ** LICENSE file, found in the top level directory of this distribution. If you 00004 ** did not receive the LICENSE file with this file, you may obtain it from the 00005 ** Vidalia source package distributed by the Vidalia Project at 00006 ** http://www.torproject.org/projects/vidalia.html. No part of Vidalia, 00007 ** including this file, may be copied, modified, propagated, or distributed 00008 ** except according to the terms described in the LICENSE file. 00009 */ 00010 00011 /* 00012 ** \file ServerSettings.cpp 00013 ** \brief Settings for running a Tor server 00014 */ 00015 00016 #include "config.h" 00017 #include "ServerSettings.h" 00018 #include "TorSettings.h" 00019 #include "TorControl.h" 00020 #ifdef USE_MINIUPNPC 00021 #include "UPNPControl.h" 00022 #endif 00023 00024 #include "net.h" 00025 #include "stringutil.h" 00026 00027 #include <QHostInfo> 00028 00029 /** Define the set of characters that are valid in a nickname. */ 00030 #define VALID_NICKNAME_CHARS \ 00031 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" 00032 /** Define the maximum length of a server's nickname. */ 00033 #define MAX_NICKNAME_LEN 19 00034 00035 /* Server configuration settings */ 00036 #define SETTING_ENABLED "Enabled" 00037 #define SETTING_DIRMIRROR "DirectoryMirror" 00038 #define SETTING_NICKNAME "Nickname" 00039 #define SETTING_ORPORT "ORPort" 00040 #define SETTING_DIRPORT "DirPort" 00041 #define SETTING_CONTACT "ContactInfo" 00042 #define SETTING_EXITPOLICY "ExitPolicy" 00043 #define SETTING_BANDWIDTH_RATE "BandwidthRate" 00044 #define SETTING_BANDWIDTH_BURST "BandwidthBurst" 00045 #define SETTING_BRIDGE_RELAY "BridgeRelay" 00046 #define SETTING_NONEXIT_RELAY "NonExitRelay" 00047 #define SETTING_ENABLE_UPNP "EnableUPnP" 00048 #define SETTING_RELAY_BANDWIDTH_RATE "RelayBandwidthRate" 00049 #define SETTING_RELAY_BANDWIDTH_BURST "RelayBandwidthBurst" 00050 #define SETTING_PUBLISH_SERVER_DESCRIPTOR "PublishServerDescriptor" 00051 00052 00053 /** Constructor. 00054 * \param torControl a TorControl object used to read and apply the server 00055 * configuration settings. 00056 */ 00057 ServerSettings::ServerSettings(TorControl *torControl) 00058 : AbstractTorSettings("Server", torControl) 00059 { 00060 setDefault(SETTING_ENABLED, false); 00061 setDefault(SETTING_DIRMIRROR, true); 00062 #if defined(Q_OS_WIN32) 00063 setDefault(SETTING_ORPORT, 443); 00064 #else 00065 setDefault(SETTING_ORPORT, 9001); 00066 #endif 00067 setDefault(SETTING_DIRPORT, 9030); 00068 setDefault(SETTING_NICKNAME, "Unnamed"); 00069 setDefault(SETTING_CONTACT, "<you@example.com>"); 00070 setDefault(SETTING_BANDWIDTH_RATE, 5242880); 00071 setDefault(SETTING_RELAY_BANDWIDTH_RATE, 5242880); 00072 setDefault(SETTING_BANDWIDTH_BURST, 10485760); 00073 setDefault(SETTING_RELAY_BANDWIDTH_BURST, 10485760); 00074 setDefault(SETTING_EXITPOLICY, 00075 ExitPolicy(ExitPolicy::Middleman).toString()); 00076 setDefault(SETTING_ENABLE_UPNP, false); 00077 setDefault(SETTING_BRIDGE_RELAY, false); 00078 setDefault(SETTING_PUBLISH_SERVER_DESCRIPTOR, "1"); 00079 } 00080 00081 /** Returns a QHash of Tor-recognizable configuratin keys to their current 00082 * values. */ 00083 QHash<QString, QString> 00084 ServerSettings::confValues() 00085 { 00086 QHash<QString, QString> conf; 00087 quint32 torVersion = torControl()->getTorVersion(); 00088 00089 /* Server Nickname */ 00090 conf.insert(SETTING_NICKNAME, 00091 (isServerEnabled() ? localValue(SETTING_NICKNAME).toString() 00092 : "")); 00093 /* Server ORPort */ 00094 conf.insert(SETTING_ORPORT, 00095 (isServerEnabled() ? localValue(SETTING_ORPORT).toString() 00096 : "0")); 00097 /* Server DirPort */ 00098 conf.insert(SETTING_DIRPORT, 00099 (isDirectoryMirror() ? localValue(SETTING_DIRPORT).toString() 00100 : "0")); 00101 /* Server Exit Policy */ 00102 conf.insert(SETTING_EXITPOLICY, 00103 ((isBridgeEnabled() || isNonExitEnabled()) ? "reject *:*" 00104 : localValue(SETTING_EXITPOLICY).toString())); 00105 00106 /* Server bandwidth settings */ 00107 conf.insert((torVersion >= 0x020001 ? SETTING_RELAY_BANDWIDTH_RATE 00108 : SETTING_BANDWIDTH_RATE), 00109 QString::number(localValue(SETTING_BANDWIDTH_RATE).toUInt()) + " bytes"); 00110 conf.insert((torVersion >= 0x020001 ? SETTING_RELAY_BANDWIDTH_BURST 00111 : SETTING_BANDWIDTH_BURST), 00112 QString::number(localValue(SETTING_BANDWIDTH_BURST).toUInt()) + " bytes"); 00113 00114 /* Server Contact Information */ 00115 QString contact = 00116 localValue(SETTING_CONTACT).toString().trimmed(); 00117 QString defaultContact = defaultValue(SETTING_CONTACT).toString(); 00118 if ((contact == defaultContact) || 00119 (contact == scrub_email_addr(defaultContact))) { 00120 /* Only set the contact info if they put something non-default there */ 00121 contact = ""; 00122 } 00123 conf.insert(SETTING_CONTACT, scrub_email_addr(contact)); 00124 00125 /* Set if we're a bridge relay */ 00126 if (isBridgeEnabled()) { 00127 conf.insert(SETTING_BRIDGE_RELAY, "1"); 00128 conf.insert(SETTING_PUBLISH_SERVER_DESCRIPTOR, 00129 publishServerDescriptor() ? "1" : "0"); 00130 } else { 00131 conf.insert(SETTING_BRIDGE_RELAY, "0"); 00132 conf.insert(SETTING_PUBLISH_SERVER_DESCRIPTOR, "1"); 00133 } 00134 return conf; 00135 } 00136 00137 /** Applies the current server configuration settings to Tor. If <b>errmsg</b> 00138 * is specified and an error occurs while applying the settings, it will be 00139 * set to a string describing the error. */ 00140 bool 00141 ServerSettings::apply(QString *errmsg) 00142 { 00143 bool rc; 00144 00145 configurePortForwarding(); 00146 00147 if (isServerEnabled()) { 00148 rc = torControl()->setConf(confValues(), errmsg); 00149 } else { 00150 QStringList resetKeys; 00151 quint32 torVersion = torControl()->getTorVersion(); 00152 resetKeys << SETTING_ORPORT 00153 << SETTING_NICKNAME 00154 << SETTING_DIRPORT 00155 << SETTING_CONTACT 00156 << SETTING_EXITPOLICY 00157 << SETTING_BRIDGE_RELAY 00158 << SETTING_PUBLISH_SERVER_DESCRIPTOR; 00159 if (torVersion >= 0x020001) { 00160 resetKeys << SETTING_RELAY_BANDWIDTH_RATE 00161 << SETTING_RELAY_BANDWIDTH_BURST; 00162 } else { 00163 resetKeys << SETTING_BANDWIDTH_RATE 00164 << SETTING_BANDWIDTH_BURST; 00165 } 00166 rc = torControl()->resetConf(resetKeys, errmsg); 00167 } 00168 return rc; 00169 } 00170 00171 /* TODO: We should call this periodically, in case the router gets rebooted or forgets its UPnP settings */ 00172 /* TODO: Remove port forwarding when Tor is shutdown or the ORPort changes */ 00173 /* TODO: init_upnp() will block for up to 2 seconds. We should fire off a thread */ 00174 00175 /** Configure UPnP device to forward DirPort and ORPort. If enable is 00176 * true, will forward ORPort and DirPort; otherwise will remove exising 00177 * port mappings */ 00178 void 00179 ServerSettings::configurePortForwarding() 00180 { 00181 #ifdef USE_MINIUPNPC 00182 quint16 ORPort, DirPort; 00183 00184 // This is how the tickbox should control UPNP 00185 if (!isUpnpEnabled()) 00186 return; 00187 00188 ORPort = getORPort(); 00189 if (!isServerEnabled()) 00190 ORPort = 0; 00191 00192 DirPort = getDirPort(); 00193 if (!isServerEnabled() || !isDirectoryMirror()) 00194 DirPort = 0; 00195 00196 UPNPControl *control = UPNPControl::instance(); 00197 control->setDesiredState(DirPort, ORPort); 00198 #endif 00199 } 00200 00201 void 00202 ServerSettings::cleanupPortForwarding() 00203 { 00204 #ifdef USE_MINIUPNPC 00205 UPNPControl::cleanup(); 00206 #endif 00207 } 00208 00209 /** Virtual method called when we retrieve a server-related setting from Tor. 00210 * Currently this just translates BandwidthFoo to RelayBandwidthFoo when 00211 * appropriate. */ 00212 QVariant 00213 ServerSettings::torValue(const QString &key) const 00214 { 00215 if (torControl()->getTorVersion() >= 0x020001) { 00216 if (key == SETTING_BANDWIDTH_RATE) 00217 return AbstractTorSettings::torValue(SETTING_RELAY_BANDWIDTH_RATE); 00218 else if (key == SETTING_BANDWIDTH_BURST) 00219 return AbstractTorSettings::torValue(SETTING_RELAY_BANDWIDTH_BURST); 00220 } 00221 return AbstractTorSettings::torValue(key); 00222 } 00223 00224 /** Enables or disables running Tor as a server. 00225 * \param enable Whether to enable or disable the Tor server. 00226 */ 00227 void 00228 ServerSettings::setServerEnabled(bool enable) 00229 { 00230 setValue(SETTING_ENABLED, enable); 00231 } 00232 00233 /** Returns true if Tor is currently configured to run as a Tor server. If Tor 00234 * is running, we will check whether it has an ORPort defined. Otherwise, we 00235 * will use our saved settings. */ 00236 bool 00237 ServerSettings::isServerEnabled() 00238 { 00239 QString orPort; 00240 if (torControl()->isConnected() && !changedSinceLastApply()) { 00241 if (torControl()->getConf(SETTING_ORPORT, orPort)) 00242 return (orPort.toUInt() > 0); 00243 } 00244 return localValue(SETTING_ENABLED).toBool(); 00245 } 00246 00247 /** Sets to <b>enabled</b> whether Tor should be a bridge node when acting as 00248 * a server. */ 00249 void 00250 ServerSettings::setBridgeEnabled(bool enabled) 00251 { 00252 setValue(SETTING_BRIDGE_RELAY, enabled); 00253 } 00254 00255 /** Returns true if Tor is configured to act as a bridge node. */ 00256 bool 00257 ServerSettings::isBridgeEnabled() 00258 { 00259 return value(SETTING_BRIDGE_RELAY).toBool() && isServerEnabled(); 00260 } 00261 00262 /** Sets to <b>enabled</b> whether Tor should be a non-exit node when acting as 00263 * a server. */ 00264 void 00265 ServerSettings::setNonExitEnabled(bool enabled) 00266 { 00267 setValue(SETTING_NONEXIT_RELAY, enabled); 00268 } 00269 00270 /** Returns true if Tor is configured to act as a non-exit node. */ 00271 bool 00272 ServerSettings::isNonExitEnabled() 00273 { 00274 return value(SETTING_NONEXIT_RELAY).toBool() && isServerEnabled(); 00275 } 00276 00277 /** Sets the server's ORPort. */ 00278 void 00279 ServerSettings::setORPort(quint16 orPort) 00280 { 00281 setValue(SETTING_ORPORT, orPort); 00282 } 00283 00284 /** Gets the server's current ORPort setting. */ 00285 quint16 00286 ServerSettings::getORPort() 00287 { 00288 return (quint16)value(SETTING_ORPORT).toUInt(); 00289 } 00290 00291 /** Sets the server's current DirPort. */ 00292 void 00293 ServerSettings::setDirPort(quint16 dirPort) 00294 { 00295 setValue(SETTING_DIRPORT, dirPort); 00296 } 00297 00298 /** Gets the server's current DirPort. */ 00299 quint16 00300 ServerSettings::getDirPort() 00301 { 00302 return (quint16)value(SETTING_DIRPORT).toUInt(); 00303 } 00304 00305 /** Sets the server's nickname. */ 00306 void 00307 ServerSettings::setNickname(QString nickname) 00308 { 00309 setValue(SETTING_NICKNAME, nickname); 00310 } 00311 00312 /** Gets the server's nickname. */ 00313 QString 00314 ServerSettings::getNickname() 00315 { 00316 QString nickname = value(SETTING_NICKNAME).toString(); 00317 /* Ensure the nickname contains only valid characters and is not too long. */ 00318 return ensure_valid_chars(nickname, 00319 VALID_NICKNAME_CHARS).left(MAX_NICKNAME_LEN); 00320 } 00321 00322 /** Sets the server's contact information. */ 00323 void 00324 ServerSettings::setContactInfo(QString contact) 00325 { 00326 setValue(SETTING_CONTACT, contact); 00327 } 00328 00329 /** Gets the server's contact information. */ 00330 QString 00331 ServerSettings::getContactInfo() 00332 { 00333 return value(SETTING_CONTACT).toString(); 00334 } 00335 00336 /** Returns whether this server will act as a directory mirror or not. */ 00337 bool 00338 ServerSettings::isDirectoryMirror() 00339 { 00340 return localValue(SETTING_DIRMIRROR).toBool(); 00341 } 00342 00343 /** Sets whether this server will act as a directory mirror. */ 00344 void 00345 ServerSettings::setDirectoryMirror(bool mirror) 00346 { 00347 setValue(SETTING_DIRMIRROR, mirror); 00348 } 00349 00350 /** Returns the exit policy for this server. */ 00351 ExitPolicy 00352 ServerSettings::getExitPolicy() 00353 { 00354 return ExitPolicy(value(SETTING_EXITPOLICY).toString()); 00355 } 00356 00357 /** Sets the exit policy for this server. */ 00358 void 00359 ServerSettings::setExitPolicy(ExitPolicy &exitPolicy) 00360 { 00361 setValue(SETTING_EXITPOLICY, exitPolicy.toString()); 00362 } 00363 00364 /** Returns the long-term average bandwidth rate (in KB/s) for this server. */ 00365 quint32 00366 ServerSettings::getBandwidthAvgRate() 00367 { 00368 return value(SETTING_BANDWIDTH_RATE).toUInt(); 00369 } 00370 00371 /** Sets the long-term average bandwidth rate (in KB/s) for this server. */ 00372 void 00373 ServerSettings::setBandwidthAvgRate(quint32 rate) 00374 { 00375 setValue(SETTING_BANDWIDTH_RATE, rate); 00376 } 00377 00378 /** Returns the maximum bandwidth burst rate (in KB/s) for this server. */ 00379 quint32 00380 ServerSettings::getBandwidthBurstRate() 00381 { 00382 return value(SETTING_BANDWIDTH_BURST).toUInt(); 00383 } 00384 00385 /** Sets the maximum bandwidth burst rate (in KB/s) for this server. */ 00386 void 00387 ServerSettings::setBandwidthBurstRate(quint32 rate) 00388 { 00389 setValue(SETTING_BANDWIDTH_BURST, rate); 00390 } 00391 00392 /** Sets whether the user's server descriptor will be published or not. 00393 * Currently this only affects publishing of bridge descriptors. If the 00394 * user is running a normal relay, its descriptor will always be 00395 * published regardless of this setting. */ 00396 void 00397 ServerSettings::setPublishServerDescriptor(bool publish) 00398 { 00399 if (publish) 00400 setValue(SETTING_PUBLISH_SERVER_DESCRIPTOR, "1"); 00401 else 00402 setValue(SETTING_PUBLISH_SERVER_DESCRIPTOR, "0"); 00403 } 00404 00405 /** Returns true if the user's server descriptor will be published to the 00406 * appropriate authorities. */ 00407 bool 00408 ServerSettings::publishServerDescriptor() const 00409 { 00410 return (value(SETTING_PUBLISH_SERVER_DESCRIPTOR).toString() != "0"); 00411 } 00412 00413 /** Returns true if UPnP support is available and enabled. */ 00414 bool 00415 ServerSettings::isUpnpEnabled() 00416 { 00417 #if defined(USE_MINIUPNPC) 00418 return localValue(SETTING_ENABLE_UPNP).toBool(); 00419 #else 00420 return false; 00421 #endif 00422 } 00423 00424 /** Sets whether Vidalia should try to configure port forwarding using UPnP. 00425 * If Vidalia was compiled without UPnP support, this method has no effect. */ 00426 void 00427 ServerSettings::setUpnpEnabled(bool enabled) 00428 { 00429 #if defined(USE_MINIUPNPC) 00430 setValue(SETTING_ENABLE_UPNP, enabled); 00431 #endif 00432 } 00433