From 3a16a58c0c2e42b1a3cc2d594baf7b5682028aff Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= <cmartin@opennebula.org>
Date: Thu, 27 Jun 2013 15:49:23 +0200
Subject: [PATCH] Feature #2054: Add TOKEN_PASSWORD to all users

---
 include/NebulaUtil.h     |  8 +++++++-
 src/common/NebulaUtil.cc | 10 ++++++++++
 src/um/UserPool.cc       |  9 ++++-----
 3 files changed, 21 insertions(+), 6 deletions(-)

diff --git a/include/NebulaUtil.h b/include/NebulaUtil.h
index 4d575e3ba3..b44b51a2bb 100644
--- a/include/NebulaUtil.h
+++ b/include/NebulaUtil.h
@@ -45,7 +45,7 @@ namespace one_util
     std::string * base64_encode(const std::string& in);
 
    /**
-    *  Base 64 deencoding
+    *  Base 64 decoding
     *    @param in the string to decode
     *    @return a pointer to the decoded string (must be freed) or 0 in case of
     *    error
@@ -53,6 +53,12 @@ namespace one_util
     std::string * base64_decode(const std::string& in);
 
     std::string aes256cbc_encrypt(const std::string& in, const std::string password);
+
+    /**
+     *  Creates a random number, using time(0) as seed, and performs an sha1 hash
+     *    @return a new random password
+     */
+    std::string random_password();
 };
 
 #endif /* _NEBULA_UTIL_H_ */
diff --git a/src/common/NebulaUtil.cc b/src/common/NebulaUtil.cc
index bbc63fa033..7fb351718e 100644
--- a/src/common/NebulaUtil.cc
+++ b/src/common/NebulaUtil.cc
@@ -198,3 +198,13 @@ string one_util::aes256cbc_encrypt(const string& in, const string password)
 
 /* -------------------------------------------------------------------------- */
 /* -------------------------------------------------------------------------- */
+
+string one_util::random_password()
+{
+    stringstream  sstr;
+
+    srand(time(0));
+    sstr << rand();
+
+    return sha1_digest(sstr.str());
+}
diff --git a/src/um/UserPool.cc b/src/um/UserPool.cc
index 627bafe4b0..e7429ed26b 100644
--- a/src/um/UserPool.cc
+++ b/src/um/UserPool.cc
@@ -74,7 +74,6 @@ UserPool::UserPool(SqlDB * db,
 
     string        filenames[4];
     string        error_str;
-    stringstream  sstr;
 
     Nebula& nd   = Nebula::instance();
 
@@ -142,10 +141,7 @@ UserPool::UserPool(SqlDB * db,
         goto error_one_name;
     }
 
-    srand(time(0));
-    sstr << rand();
-
-    random = one_util::sha1_digest(sstr.str());
+    random = one_util::random_password();
 
     filenames[0] = nd.get_var_location() + "/.one/sunstone_auth";
     filenames[1] = nd.get_var_location() + "/.one/occi_auth";
@@ -296,6 +292,9 @@ int UserPool::allocate (
     // Build a new User object
     user = new User(-1, gid, uname, gname, upass, auth_driver, enabled);
 
+    // Set a password for the OneGate tokens
+    user->add_template_attribute("TOKEN_PASSWORD", one_util::random_password());
+
     // Insert the Object in the pool
     *oid = PoolSQL::allocate(user, error_str);