torsettings.cpp

Go to the documentation of this file.
00001 /****************************************************************
00002  *  Vidalia is distributed under the following license:
00003  *
00004  *  Copyright (C) 2006,  Matt Edman, Justin Hipple
00005  *
00006  *  This program is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU General Public License
00008  *  as published by the Free Software Foundation; either version 2
00009  *  of the License, or (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, 
00019  *  Boston, MA  02110-1301, USA.
00020  ****************************************************************/
00021 
00022 /** 
00023  * \file torsettings.cpp
00024  * \version $Id: torsettings.cpp 1238 2006-09-25 17:50:57Z edmanm $
00025  * \brief Settings used for starting and running Tor
00026  */
00027 
00028 #include <QDir>
00029 #include <vidalia.h>
00030 
00031 #include "torsettings.h"
00032 
00033 /* Tor Settings */
00034 #define SETTING_TOR_EXECUTABLE  "Tor/TorExecutable"
00035 #define SETTING_TORRC           "Tor/Torrc"
00036 #define SETTING_CONTROL_ADDR    "Tor/ControlAddr"
00037 #define SETTING_CONTROL_PORT    "Tor/ControlPort"
00038 #define SETTING_AUTH_TOKEN      "Tor/AuthToken"
00039 #define SETTING_USE_SERVICE     "Tor/UseService"
00040 #define SETTING_TOR_USER        "Tor/User"
00041 #define SETTING_TOR_GROUP       "Tor/Group"
00042 
00043 /* On win32, we need to add the .exe onto Tor's filename */
00044 #if defined(Q_OS_WIN32)
00045 #include <util/win32.h>
00046 #define DEFAULT_TOR_EXECUTABLE    (win32_program_files_folder() + "\\Tor\\tor.exe")
00047 #else
00048 #define DEFAULT_TOR_EXECUTABLE    "/usr/bin/tor"
00049 #endif
00050 
00051 /* Arguments we can pass to Tor on the command-line */
00052 #define TOR_ARG_CONTROL_PORT    "ControlPort"
00053 #define TOR_ARG_TORRC           "-f"
00054 #define TOR_ARG_USER            "User"
00055 #define TOR_ARG_GROUP           "Group"
00056 
00057 
00058 /** Default constructor */
00059 TorSettings::TorSettings()
00060 {
00061   setDefault(SETTING_TOR_EXECUTABLE, DEFAULT_TOR_EXECUTABLE);
00062   setDefault(SETTING_TORRC,         "/etc/tor/torrc");
00063   setDefault(SETTING_CONTROL_ADDR,  "127.0.0.1");
00064   setDefault(SETTING_CONTROL_PORT,  9051);
00065   setDefault(SETTING_AUTH_TOKEN,    QByteArray(""));
00066   setDefault(SETTING_TOR_USER,      "toruser");
00067   setDefault(SETTING_TOR_GROUP,     "toruser");
00068   setDefault(SETTING_USE_SERVICE,   false);
00069 }
00070 
00071 /** Returns a fully-qualified path to Tor's executable, including the
00072  * executable name. */
00073 QString
00074 TorSettings::getExecutable()
00075 {
00076   return QDir::convertSeparators(
00077             value(SETTING_TOR_EXECUTABLE).toString());
00078 }
00079 
00080 /** Sets the location and name of Tor's executable to the given string. */
00081 void
00082 TorSettings::setExecutable(QString torExecutable)
00083 {
00084   setValue(SETTING_TOR_EXECUTABLE, torExecutable);
00085 }
00086 
00087 /** Formats the argument name <b>name</b> with the given value <b>value</b>.
00088  * If <b>value</b> contains a space, <b>value</b> will be wrapped in quotes. */
00089 QString
00090 TorSettings::formatArgument(QString name, QString value)
00091 {
00092   if (value.indexOf(" ") >= 0) {
00093     value = "\"" + value + "\"";
00094   }
00095   return name + " " + value;
00096 }
00097 
00098 /** Returns a formatted QString of all currently set command-line arguments.
00099  * If an argument's value contains a space, then it will be wrapped in quotes.
00100  * */
00101 QString
00102 TorSettings::getArguments()
00103 {
00104   QString args;
00105 
00106   /* Add the torrc argument (if specified) */
00107   QString torrc = getTorrc();
00108   if (!torrc.isEmpty()) {
00109     args += formatArgument(TOR_ARG_TORRC, torrc) + " ";
00110   }
00111   /* Add the ControlPort value */
00112   quint16 controlPort = getControlPort();
00113   if (controlPort) {
00114     args += formatArgument(TOR_ARG_CONTROL_PORT, 
00115                            QString::number(controlPort)) + " ";
00116   }
00117   /* Add the User argument (if specified) */
00118   QString user = getUser();
00119   if (!user.isEmpty()) {
00120     args += formatArgument(TOR_ARG_USER, user) + " ";
00121   }
00122   /* Add the Group argument (if specified) */
00123   QString group = getGroup();
00124   if (!group.isEmpty()) {
00125     args += formatArgument(TOR_ARG_GROUP, group);
00126   }
00127   return args;
00128 }
00129 
00130 /** Returns the torrc that will be used when starting Tor. */
00131 QString
00132 TorSettings::getTorrc()
00133 {
00134   return value(SETTING_TORRC).toString();
00135 }
00136 
00137 /** Sets the torrc that will be used when starting Tor.
00138  * \param torrc The torrc to use. 
00139  */
00140 void
00141 TorSettings::setTorrc(QString torrc)
00142 {
00143   setValue(SETTING_TORRC, torrc);
00144 }
00145 
00146 /** Returns the user used when running Tor. The user is specified as an
00147  * argument to Tor, which will setuid to this user. */
00148 QString
00149 TorSettings::getUser()
00150 {
00151   return value(SETTING_TOR_USER).toString();
00152 }
00153 
00154 /** Sets the user used when running Tor. The user is specified as an argument
00155  * to Tor, which will setuid to this user. */
00156 void
00157 TorSettings::setUser(QString user)
00158 {
00159   setValue(SETTING_TOR_USER, user);
00160 }
00161 
00162 /** Returns the group used when running Tor. The group is specified as an
00163  * argument to Tor, which will setgid to this group. */
00164 QString
00165 TorSettings::getGroup()
00166 {
00167   return value(SETTING_TOR_GROUP).toString();
00168 }
00169 
00170 /** Sets the group used when running Tor. The group is specified as an
00171  * argument to Tor, which will setgid to this group. */
00172 void
00173 TorSettings::setGroup(QString group)
00174 {
00175   setValue(SETTING_TOR_GROUP, group);
00176 }
00177 
00178 /** Get the address or hostname used to connect to Tor */
00179 QHostAddress
00180 TorSettings::getControlAddress()
00181 {
00182   QString addr = value(SETTING_CONTROL_ADDR).toString();
00183   return QHostAddress(addr);
00184 }
00185 
00186 /** Set the address or hostname used to connect to Tor */
00187 void
00188 TorSettings::setControlAddress(QHostAddress addr)
00189 {
00190   setValue(SETTING_CONTROL_PORT, addr.toString());
00191 }
00192 
00193 /** Get the control port used to connect to Tor */
00194 quint16
00195 TorSettings::getControlPort()
00196 {
00197   return (quint16)value(SETTING_CONTROL_PORT).toInt();
00198 }
00199 
00200 /** Set the control port used to connect to Tor */
00201 void
00202 TorSettings::setControlPort(quint16 port)
00203 {
00204   setValue(SETTING_CONTROL_PORT, port);
00205 }
00206 
00207 /** Get the authentication token sent by the controller to Tor. For now, this
00208 * just sends the default (blank) authentication token. It is implemented as a
00209 * stub here to make it easy to do real authentication in the future. */
00210 QByteArray
00211 TorSettings::getAuthToken()
00212 {
00213   return QByteArray::fromBase64(
00214             value(SETTING_AUTH_TOKEN).toByteArray());
00215 }
00216 
00217 /** Set the authentication token sent by the controller to Tor. */
00218 void
00219 TorSettings::setAuthToken(QByteArray token)
00220 {
00221   setValue(SETTING_AUTH_TOKEN, token.toBase64());
00222 }
00223 
00224 /** Get whether Tor will run as an NT service */
00225 bool
00226 TorSettings::getUseService()
00227 {
00228   return value(SETTING_USE_SERVICE).toBool();
00229 }
00230 
00231 /** Set whether Tor will run as an NT service */
00232 void
00233 TorSettings::setUseService(bool useService)
00234 {
00235   setValue(SETTING_USE_SERVICE, useService);
00236 }
00237 

Generated on Mon Oct 23 20:08:16 2006 for Vidalia by  doxygen 1.5.0