[tor-commits] [stem/master] Connect didn't flag controllers as authenticated

atagar at torproject.org atagar at torproject.org
Tue May 6 01:21:12 UTC 2014


commit ccb8a16b9ccb8f11b16d7949d5172bf1f90dc618
Author: Damian Johnson <atagar at torproject.org>
Date:   Tue Apr 15 09:27:54 2014 -0700

    Connect didn't flag controllers as authenticated
    
    Our Controller class tracks if it's been authenticated to Tor or not. When
    users call msg('AUTHENTICATE...') we're authenticated, and when we attach to a
    new socket we're unauthenticated.
    
    This is all well and good, but when a user first constructs us from a
    ControlSocket we have no idea if that socket has been authenticated or not. We
    can't actively check because Tor will disconnect the socket if it doesn't obey
    a very specific authentication dance (... ugg). So we need our caller to tell
    us if it's been authenticated or not.
    
    Previously we just assumed it wasn't authenticated, causing our connect(),
    connect_port(), and connect_socket_file() methods to return a Controller that
    has been authenticated, but didn't know it had. In general this worked, but
    turns out event listening checks the authenticated flag.
---
 docs/change_log.rst |    1 +
 stem/connection.py  |    2 +-
 stem/control.py     |   15 +++++++++++----
 3 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/docs/change_log.rst b/docs/change_log.rst
index 624f730..e7a8ed6 100644
--- a/docs/change_log.rst
+++ b/docs/change_log.rst
@@ -51,6 +51,7 @@ The following are only available within Stem's `git repository
   * Added `support for HS_DESC events <api/response.html#stem.response.events.HSDescEvent>`_ (:spec:`a67ac4d`, :trac:`10807`)
   * Changed :func:`~stem.control.Controller.get_network_status` and :func:`~stem.control.Controller.get_network_statuses` to provide :class:`~stem.descriptor.router_status_entry.RouterStatusEntryMicroV3` if Tor is using microdescriptors (:trac:`7646`)
   * Deprecated :func:`~stem.connection.connect_port` and :func:`~stem.connection.connect_socket_file` in favor of a new, better :func:`~stem.connection.connect` function
+  * The :func:`~stem.connection.connect_port` and :func:`~stem.connection.connect_socket_file` didn't properly mark the Controller it returned as being authenticated, causing event listening among other things to fail.
 
  * **Utilities**
 
diff --git a/stem/connection.py b/stem/connection.py
index c49ff85..b9f6c0d 100644
--- a/stem/connection.py
+++ b/stem/connection.py
@@ -358,7 +358,7 @@ def _connect_auth(control_socket, password, password_prompt, chroot_path, contro
     if controller is None:
       return control_socket
     else:
-      return controller(control_socket)
+      return controller(control_socket, is_authenticated = True)
   except IncorrectSocketType:
     if isinstance(control_socket, stem.socket.ControlPort):
       print CONNECT_MESSAGES['wrong_port_type'].format(port = control_socket.get_port())
diff --git a/stem/control.py b/stem/control.py
index ddcc5df..6630762 100644
--- a/stem/control.py
+++ b/stem/control.py
@@ -335,9 +335,13 @@ class BaseController(object):
   It's highly suggested that you don't interact directly with the
   :class:`~stem.socket.ControlSocket` that we're constructed from - use our
   wrapper methods instead.
+
+  If the **control_socket** is already authenticated to Tor then the caller
+  should provide the **is_authenticated** flag. Otherwise, we will treat the
+  socket as though it hasn't yet been authenticated.
   """
 
-  def __init__(self, control_socket):
+  def __init__(self, control_socket, is_authenticated = False):
     self._socket = control_socket
     self._msg_lock = threading.RLock()
 
@@ -370,6 +374,9 @@ class BaseController(object):
     if self._socket.is_alive():
       self._launch_threads()
 
+    if is_authenticated:
+      self._post_authentication()
+
   def msg(self, message):
     """
     Sends a message to our control socket and provides back its reply.
@@ -770,9 +777,7 @@ class Controller(BaseController):
     control_socket = stem.socket.ControlSocketFile(path)
     return Controller(control_socket)
 
-  def __init__(self, control_socket):
-    super(Controller, self).__init__(control_socket)
-
+  def __init__(self, control_socket, is_authenticated = False):
     self._is_caching_enabled = True
     self._request_cache = {}
     self._last_newnym = 0.0
@@ -789,6 +794,8 @@ class Controller(BaseController):
     self._geoip_failure_count = 0
     self._enabled_features = []
 
+    super(Controller, self).__init__(control_socket, is_authenticated)
+
     def _sighup_listener(event):
       if event.signal == Signal.RELOAD:
         self.clear_cache()





More information about the tor-commits mailing list