[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[vidalia-svn] r2047: Make TorSettings inherit AbstractTorSettings and support cha (in trunk: . src/config src/gui/config)



Author: edmanm
Date: 2007-10-20 18:37:44 -0400 (Sat, 20 Oct 2007)
New Revision: 2047

Modified:
   trunk/
   trunk/src/config/torsettings.cpp
   trunk/src/config/torsettings.h
   trunk/src/gui/config/advancedpage.cpp
   trunk/src/gui/config/advancedpage.h
Log:
 r2153@lysithea:  edmanm | 2007-10-20 18:37:18 -0400
 Make TorSettings inherit AbstractTorSettings and support changing the control
 port and control port authentication settings while Tor is running.



Property changes on: trunk
___________________________________________________________________
 svk:merge ticket from /local/vidalia/trunk [r2153] on dc66be73-d13e-47ba-a267-8dc7cda68c65

Modified: trunk/src/config/torsettings.cpp
===================================================================
--- trunk/src/config/torsettings.cpp	2007-10-20 22:37:37 UTC (rev 2046)
+++ trunk/src/config/torsettings.cpp	2007-10-20 22:37:44 UTC (rev 2047)
@@ -1,7 +1,7 @@
 /****************************************************************
  *  Vidalia is distributed under the following license:
  *
- *  Copyright (C) 2006,  Matt Edman, Justin Hipple
+ *  Copyright (C) 2006-2007,  Matt Edman, Justin Hipple
  *
  *  This program is free software; you can redistribute it and/or
  *  modify it under the terms of the GNU General Public License
@@ -68,8 +68,8 @@
 
 
 /** Default constructor */
-TorSettings::TorSettings()
-: VSettings("Tor")
+TorSettings::TorSettings(TorControl *torControl)
+: AbstractTorSettings("Tor", torControl)
 {
 #if defined(Q_OS_WIN32)
   QString programFiles = win32_program_files_folder();
@@ -92,6 +92,36 @@
   setDefault(SETTING_USE_RANDOM_PASSWORD, true);
 }
 
+/** Applies any changes to Tor's ccontrol port or authentication settings. */
+bool
+TorSettings::apply(QString *errmsg)
+{
+  QHash<QString, QString> conf;
+
+  conf.insert(SETTING_CONTROL_PORT,
+              localValue(SETTING_CONTROL_PORT).toString());
+  
+  AuthenticationMethod authMethod = 
+    toAuthenticationMethod(localValue(SETTING_AUTH_METHOD).toString());
+  switch (authMethod) {
+    case CookieAuth:
+      conf.insert(TOR_ARG_COOKIE_AUTH,    "1");
+      conf.insert(TOR_ARG_HASHED_PASSWORD, "");
+      break;
+    case PasswordAuth:
+      if (useRandomPassword())
+        setControlPassword(generateRandomPassword());
+      conf.insert(TOR_ARG_COOKIE_AUTH,    "0");
+      conf.insert(TOR_ARG_HASHED_PASSWORD,
+                  hashPassword(getControlPassword()));
+      break;
+    default:
+      conf.insert(TOR_ARG_COOKIE_AUTH,    "0");
+      conf.insert(TOR_ARG_HASHED_PASSWORD, "");
+  }
+  return torControl()->setConf(conf, errmsg);
+}
+
 /** Gets the location of Tor's data directory. */
 QString
 TorSettings::getDataDirectory()
@@ -111,7 +141,7 @@
 QString
 TorSettings::getExecutable()
 {
-  return QDir::convertSeparators(value(SETTING_TOR_EXECUTABLE).toString());
+  return QDir::convertSeparators(localValue(SETTING_TOR_EXECUTABLE).toString());
 }
 
 /** Sets the location and name of Tor's executable to the given string. */
@@ -179,7 +209,11 @@
 QString
 TorSettings::getTorrc()
 {
-  return QDir::convertSeparators(value(SETTING_TORRC).toString());
+  QString torrc;
+  TorControl *tc = torControl();
+  if (tc && tc->isConnected() && tc->getInfo("config-file", torrc))
+    return QDir::convertSeparators(torrc);
+  return QDir::convertSeparators(localValue(SETTING_TORRC).toString());
 }
 
 /** Sets the torrc that will be used when starting Tor.
@@ -227,7 +261,7 @@
 QHostAddress
 TorSettings::getControlAddress()
 {
-  QString addr = value(SETTING_CONTROL_ADDR).toString();
+  QString addr = localValue(SETTING_CONTROL_ADDR).toString();
   return QHostAddress(addr);
 }
 
@@ -257,7 +291,7 @@
 QString
 TorSettings::getControlPassword()
 {
-  return value(SETTING_CONTROL_PASSWORD).toString();
+  return localValue(SETTING_CONTROL_PASSWORD).toString();
 }
 
 /** Sets the control password used when starting Tor with
@@ -273,7 +307,7 @@
 bool
 TorSettings::useRandomPassword()
 {
-  return value(SETTING_USE_RANDOM_PASSWORD).toBool();
+  return localValue(SETTING_USE_RANDOM_PASSWORD).toBool();
 }
 
 /** Sets whether or not to generate and use a random control password each
@@ -288,16 +322,31 @@
 TorSettings::AuthenticationMethod
 TorSettings::getAuthenticationMethod()
 {
-  AuthenticationMethod type;
-  QString str = value(SETTING_AUTH_METHOD).toString();
-  if (str == toString(NullAuth))
-    type = NullAuth;
-  else if (str == toString(PasswordAuth))
-    type = PasswordAuth;
-  else if (str == toString(CookieAuth))
-    type = CookieAuth;
-  else
-    type = UnknownAuth;
+  AuthenticationMethod type = UnknownAuth;
+  TorControl *tc = torControl();
+
+  if (tc && tc->isConnected()) {
+    QHash<QString,QString> conf;
+    conf.insert(TOR_ARG_COOKIE_AUTH, "");
+    conf.insert(TOR_ARG_HASHED_PASSWORD, "");
+    if (tc->getConf(conf)) {
+      if (conf.value(TOR_ARG_COOKIE_AUTH) == "1")
+        type = CookieAuth;
+      else if (!conf.value(TOR_ARG_HASHED_PASSWORD).isEmpty())
+        type = PasswordAuth;
+    }
+  }
+  if (type == UnknownAuth) {
+    QString str = localValue(SETTING_AUTH_METHOD).toString();
+    if (str == toString(NullAuth))
+      type = NullAuth;
+    else if (str == toString(PasswordAuth))
+      type = PasswordAuth;
+    else if (str == toString(CookieAuth))
+      type = CookieAuth;
+    else
+      type = DEFAULT_AUTH_METHOD;
+  }
   return type;
 }
 
@@ -323,6 +372,21 @@
   return "unknown";
 }
 
+/** Returns the AuthenticationMethod enum value for the string
+ * description of the authentication method given in <b>authMethod</b>. */
+TorSettings::AuthenticationMethod
+TorSettings::toAuthenticationMethod(const QString &authMethod)
+{ 
+  QString str = authMethod.toLower();
+  if (str == toString(NullAuth))
+    return NullAuth;
+  else if (str == toString(PasswordAuth))
+    return PasswordAuth;
+  else if (str == toString(CookieAuth))
+    return CookieAuth;
+  return UnknownAuth;
+}
+
 /** Generates a random control password consisting of PASSWORD_LEN characters. */
 QString
 TorSettings::generateRandomPassword()

Modified: trunk/src/config/torsettings.h
===================================================================
--- trunk/src/config/torsettings.h	2007-10-20 22:37:37 UTC (rev 2046)
+++ trunk/src/config/torsettings.h	2007-10-20 22:37:44 UTC (rev 2047)
@@ -1,7 +1,7 @@
 /****************************************************************
  *  Vidalia is distributed under the following license:
  *
- *  Copyright (C) 2006,  Matt Edman, Justin Hipple
+ *  Copyright (C) 2006-2007,  Matt Edman, Justin Hipple
  *
  *  This program is free software; you can redistribute it and/or
  *  modify it under the terms of the GNU General Public License
@@ -30,12 +30,12 @@
 
 #include <QHostAddress>
 
-#include "vsettings.h"
+#include "abstracttorsettings.h"
 
 
 /** Manages Tor-specific settings, such as location, command-line arguments,
  * and control interface information. */
-class TorSettings : public VSettings
+class TorSettings : public AbstractTorSettings
 {
 public:
   /** Available Tor authentication methods. */
@@ -47,8 +47,10 @@
   };
   
   /** Default constructor. */
-  TorSettings();
-  
+  TorSettings(TorControl *torControl = 0);
+  /** Applies any changes to Tor's control port or authentication settings. */ 
+  bool apply(QString *errmsg = 0);
+
   /** Gets the name and path of Tor's executable. */
   QString getExecutable();
   /** Sets the name and path of Tor's executable. */
@@ -107,6 +109,9 @@
   void setGroup(QString group);
 
 private:
+  /** Returns the AuthenticationMethod enum value for the string
+   * description of the authentication method given in <b>authMethod</b>. */
+  AuthenticationMethod toAuthenticationMethod(const QString &authMethod);
   /** Returns the string description of the authentication method specified by
    * <b>method</b>. The authentication method string is stored in  Vidalia's
    * configuration file. */

Modified: trunk/src/gui/config/advancedpage.cpp
===================================================================
--- trunk/src/gui/config/advancedpage.cpp	2007-10-20 22:37:37 UTC (rev 2046)
+++ trunk/src/gui/config/advancedpage.cpp	2007-10-20 22:37:44 UTC (rev 2047)
@@ -1,7 +1,7 @@
 /****************************************************************
  *  Vidalia is distributed under the following license:
  *
- *  Copyright (C) 2006,  Matt Edman, Justin Hipple
+ *  Copyright (C) 2006-2007,  Matt Edman, Justin Hipple
  *
  *  This program is free software; you can redistribute it and/or
  *  modify it under the terms of the GNU General Public License
@@ -48,7 +48,7 @@
   ui.setupUi(this);
 
   /* Create TorSettings object */
-  _settings = new TorSettings();
+  _settings = new TorSettings(Vidalia::torControl());
   
   /* Set validators for the control port and IP address fields */
   ui.lineControlAddress->setValidator(new IPValidator(this));
@@ -76,6 +76,31 @@
   delete _settings;
 }
 
+/** Applies the network configuration settings to Tor. Returns true if the
+ * settings were applied successfully. Otherwise, <b>errmsg</b> is set
+ * and false is returned. */
+bool
+AdvancedPage::apply(QString &errmsg)
+{
+  return _settings->apply(&errmsg);
+}
+
+/** Reverts the Tor configuration settings to their values at the last
+ * time they were successfully applied to Tor. */
+bool
+AdvancedPage::changedSinceLastApply()
+{
+  return _settings->changedSinceLastApply();
+}
+
+/** Returns true if the user has changed their advanced Tor settings since
+ * the last time they were applied to Tor. */
+void
+AdvancedPage::revert()
+{
+  return _settings->revert();
+}
+
 /** Saves all settings for this page. */
 bool
 AdvancedPage::save(QString &errmsg)

Modified: trunk/src/gui/config/advancedpage.h
===================================================================
--- trunk/src/gui/config/advancedpage.h	2007-10-20 22:37:37 UTC (rev 2046)
+++ trunk/src/gui/config/advancedpage.h	2007-10-20 22:37:44 UTC (rev 2047)
@@ -1,7 +1,7 @@
 /****************************************************************
  *  Vidalia is distributed under the following license:
  *
- *  Copyright (C) 2006,  Matt Edman, Justin Hipple
+ *  Copyright (C) 2006-2007,  Matt Edman, Justin Hipple
  *
  *  This program is free software; you can redistribute it and/or
  *  modify it under the terms of the GNU General Public License
@@ -48,6 +48,17 @@
   bool save(QString &errmsg);
   /** Loads the settings for this page */
   void load();
+ 
+  /** Applies the network configuration settings to Tor. Returns true if the
+   * settings were applied successfully. Otherwise, <b>errmsg</b> is set
+   * and false is returned. */
+  bool apply(QString &errmsg);
+  /** Reverts the Tor configuration settings to their values at the last
+   * time they were successfully applied to Tor. */
+  void revert();
+  /** Returns true if the user has changed their advanced Tor settings since
+   * the last time they were applied to Tor. */
+  bool changedSinceLastApply();
 
 private slots:
   /** Called when the user selects a different authentication method from the