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

[tor-commits] [torbutton/master] Bug #3928: implement reading password from CookieAuthFile



commit de8c53cece3a8a99a60921fd42364c0faee0d8e7
Author: Jérémy Bobbio <lunar@xxxxxxxxxx>
Date:   Wed Sep 14 12:07:47 2011 +0200

    Bug #3928: implement reading password from CookieAuthFile
    
    Using environment variable TOR_CONTROL_COOKIE_AUTH_FILE, one can now specify
    the path to the Tor CookieAuthFile. The password will be read from here
    if the environment lacks TOR_CONTROL_PASSWD.
---
 src/chrome/content/torbutton.js |   30 ++++++++++++++++++++++++++++++
 1 files changed, 30 insertions(+), 0 deletions(-)

diff --git a/src/chrome/content/torbutton.js b/src/chrome/content/torbutton.js
index 1ebd6bb..5ee1a53 100644
--- a/src/chrome/content/torbutton.js
+++ b/src/chrome/content/torbutton.js
@@ -525,6 +525,15 @@ function torbutton_init() {
         // FIXME: We might want a check to use to set this in the future,
         // but this works fine for now.
         m_tb_tbb = true;
+    } else {
+        var cookie_path = environ.get("TOR_CONTROL_COOKIE_AUTH_FILE");
+        try {
+            if ("" != cookie_path) {
+                m_tb_control_pass = torbutton_read_authentication_cookie(cookie_path);
+            }
+        } catch(e) {
+            torbutton_log(4, 'unable to read authentication cookie');
+        }
     }
 
     if (environ.exists("TOR_CONTROL_PORT")) {
@@ -1304,6 +1313,27 @@ function torbutton_socket_readline(input) {
   return str;
 }
 
+function torbutton_read_authentication_cookie(path) {
+  var file = Components.classes['@mozilla.org/file/local;1']
+             .createInstance(Components.interfaces.nsILocalFile);
+  file.initWithPath(path);
+  var fileStream = Components.classes['@mozilla.org/network/file-input-stream;1']
+                   .createInstance(Components.interfaces.nsIFileInputStream);
+  fileStream.init(file, 1, 0, false);
+  var binaryStream = Components.classes['@mozilla.org/binaryinputstream;1']
+                     .createInstance(Components.interfaces.nsIBinaryInputStream);
+  binaryStream.setInputStream(fileStream);
+  var array = binaryStream.readByteArray(fileStream.available());
+  binaryStream.close();
+  fileStream.close();
+  return torbutton_array_to_hexdigits(array);
+}
+
+function torbutton_array_to_hexdigits(array) {
+  return array.map(function(c) {
+                     return String("0" + c.toString(16)).slice(-2)
+                   }).join('');
+};
 // Executes a command on the control port.
 // Return 0 in error, 1 for success.
 function torbutton_send_ctrl_cmd(command) {



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits