diff --git a/include/AuthRequest.h b/include/AuthRequest.h index b0b4a49e61..6844b34f82 100644 --- a/include/AuthRequest.h +++ b/include/AuthRequest.h @@ -21,8 +21,8 @@ #include "ActionManager.h" #include "PoolObjectAuth.h" -#include "SSLTools.h" #include "AuthManager.h" +#include "Util.h" #include "SyncRequest.h" @@ -75,22 +75,22 @@ public: username = _username; password = _password; session = _session; - + driver = _driver; } /** * Adds a CREATE authorization request. - * + * * OBJECT:<-1|OBJECT_TMPL_XML64>:CREATE:UID:AUTH - * + * * @param type of the object to be created * @param template (base64 encoded) of the new object */ void add_create_auth(PoolObjectSQL::ObjectType type, const string& txml_64) { PoolObjectAuth perms; //oid & gid set to -1 - + perms.uid = uid; perms.obj_type = type; @@ -143,20 +143,20 @@ public: bool core_authenticate() { - string sha1_session = SSLTools::sha1_digest(session); + string sha1_session = one_util::sha1_digest(session); return (password == sha1_session); } - -private: - + +private: + friend class AuthManager; - + /** * The user id for this request */ int uid; - + /** * The user group ID */ diff --git a/include/SSLTools.h b/include/SSLTools.h deleted file mode 100644 index 12b8275716..0000000000 --- a/include/SSLTools.h +++ /dev/null @@ -1,51 +0,0 @@ -/* ------------------------------------------------------------------------ */ -/* Copyright 2002-2013, OpenNebula Project (OpenNebula.org), C12G Labs */ -/* */ -/* Licensed under the Apache License, Version 2.0 (the "License"); you may */ -/* not use this file except in compliance with the License. You may obtain */ -/* a copy of the License at */ -/* */ -/* http://www.apache.org/licenses/LICENSE-2.0 */ -/* */ -/* Unless required by applicable law or agreed to in writing, software */ -/* distributed under the License is distributed on an "AS IS" BASIS, */ -/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ -/* See the License for the specific language governing permissions and */ -/* limitations under the License. */ -/* -------------------------------------------------------------------------*/ - -#ifndef SSL_TOOLS_H_ -#define SSL_TOOLS_H_ - -#include - -using namespace std; - -/** - * The SSLTools class provides a simple interface to common SSL utils used - * in OpenNebula - */ -class SSLTools -{ -public: - /** - * sha1 digest - * @param in the string to be hashed - * @return sha1 hash of str - */ - static string sha1_digest(const string& in); - - /** - * Base 64 encoding - * @param in the string to encoded - * @return a pointer to the encoded string (must be freed) or 0 in case of - * error - */ - static string * base64_encode(const string& in); - -private: - SSLTools(){}; - ~SSLTools(){}; -}; - -#endif /*SSL_TOOLS_H_*/ diff --git a/include/Util.h b/include/Util.h index be18222f1a..d125e76893 100644 --- a/include/Util.h +++ b/include/Util.h @@ -28,6 +28,30 @@ namespace one_util std::string log_time(time_t the_time); std::string log_time(); + + /** + * sha1 digest + * @param in the string to be hashed + * @return sha1 hash of str + */ + std::string sha1_digest(const std::string& in); + + /** + * Base 64 encoding + * @param in the string to encoded + * @return a pointer to the encoded string (must be freed) or 0 in case of + * error + */ + std::string * base64_encode(const std::string& in); + + /** + * Base 64 deencoding + * @param in the string to decode + * @return a pointer to the decoded string (must be freed) or 0 in case of + * error + */ + std::string * base64_decode(const std::string& in); + }; #endif /* UTIL_H_ */ diff --git a/src/authm/AuthManager.cc b/src/authm/AuthManager.cc index 2eaa4dc507..cb3753b03e 100644 --- a/src/authm/AuthManager.cc +++ b/src/authm/AuthManager.cc @@ -17,7 +17,7 @@ #include "AuthManager.h" #include "AuthRequest.h" #include "NebulaLog.h" -#include "SSLTools.h" +#include "Util.h" #include "PoolObjectAuth.h" #include "Nebula.h" @@ -40,7 +40,7 @@ void AuthRequest::add_auth(Operation op, if ( !ob_template.empty() ) { - string * encoded_id = SSLTools::base64_encode(ob_template); + string * encoded_id = one_util::base64_encode(ob_template); if (encoded_id != 0) { diff --git a/src/common/SConstruct b/src/common/SConstruct index 5f3867988a..741e2bdbc3 100644 --- a/src/common/SConstruct +++ b/src/common/SConstruct @@ -25,7 +25,6 @@ source_files=[ 'ActionManager.cc', 'Attribute.cc', 'mem_collector.c', - 'SSLTools.cc', 'Util.cc' ] diff --git a/src/common/SSLTools.cc b/src/common/SSLTools.cc deleted file mode 100644 index 469f26127b..0000000000 --- a/src/common/SSLTools.cc +++ /dev/null @@ -1,98 +0,0 @@ -/* -------------------------------------------------------------------------- */ -/* Copyright 2002-2013, OpenNebula Project (OpenNebula.org), C12G Labs */ -/* */ -/* Licensed under the Apache License, Version 2.0 (the "License"); you may */ -/* not use this file except in compliance with the License. You may obtain */ -/* a copy of the License at */ -/* */ -/* http://www.apache.org/licenses/LICENSE-2.0 */ -/* */ -/* Unless required by applicable law or agreed to in writing, software */ -/* distributed under the License is distributed on an "AS IS" BASIS, */ -/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ -/* See the License for the specific language governing permissions and */ -/* limitations under the License. */ -/* -------------------------------------------------------------------------- */ - -#include -#include -#include -#include -#include - -#include "SSLTools.h" -#include -#include -#include - - -//#include - - -//#include -//#include -//#include - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -string * SSLTools::base64_encode(const string& in) -{ - BIO * bio_mem; - BIO * bio_64; - - char * encoded_c; - long int size; - - bio_64 = BIO_new(BIO_f_base64()); - bio_mem = BIO_new(BIO_s_mem()); - - BIO_push(bio_64, bio_mem); - - BIO_set_flags(bio_64,BIO_FLAGS_BASE64_NO_NL); - - BIO_write(bio_64, in.c_str(), in.length()); - - if (BIO_flush(bio_64) != 1) - { - return 0; - } - - size = BIO_get_mem_data(bio_mem,&encoded_c); - - string * encoded = new string(encoded_c,size); - - BIO_free_all(bio_64); - - return encoded; -} - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -string SSLTools::sha1_digest(const string& in) -{ - EVP_MD_CTX mdctx; - unsigned char md_value[EVP_MAX_MD_SIZE]; - unsigned int md_len; - ostringstream oss; - - EVP_MD_CTX_init(&mdctx); - EVP_DigestInit_ex(&mdctx, EVP_sha1(), NULL); - - EVP_DigestUpdate(&mdctx, in.c_str(), in.length()); - - EVP_DigestFinal_ex(&mdctx,md_value,&md_len); - EVP_MD_CTX_cleanup(&mdctx); - - for(unsigned int i = 0; i +#include +#include +#include +#include + +#include +#include +#include #include using namespace std; @@ -25,12 +35,18 @@ string& one_util::toupper(string& st) return st; }; +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + string& one_util::tolower(string& st) { transform(st.begin(),st.end(),st.begin(),(int(*)(int))tolower); return st; }; +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + string one_util::log_time(time_t the_time) { char time_str[26]; @@ -46,8 +62,115 @@ string one_util::log_time(time_t the_time) return string(time_str); }; +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + string one_util::log_time() { return log_time( time(0) ); }; +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +string * one_util::base64_encode(const string& in) +{ + BIO * bio_mem; + BIO * bio_64; + + char * encoded_c; + long int size; + + bio_64 = BIO_new(BIO_f_base64()); + bio_mem = BIO_new(BIO_s_mem()); + + BIO_push(bio_64, bio_mem); + + BIO_set_flags(bio_64, BIO_FLAGS_BASE64_NO_NL); + + BIO_write(bio_64, in.c_str(), in.length()); + + if (BIO_flush(bio_64) != 1) + { + return 0; + } + + size = BIO_get_mem_data(bio_mem,&encoded_c); + + string * encoded = new string(encoded_c,size); + + BIO_free_all(bio_64); + + return encoded; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +string * one_util::base64_decode(const string& in) +{ + BIO * bio_mem_in; + BIO * bio_mem_out; + BIO * bio_64; + + char inbuf[512]; + int inlen; + + char * decoded_c; + long int size; + + bio_64 = BIO_new(BIO_f_base64()); + + bio_mem_in = BIO_new(BIO_s_mem()); + bio_mem_out = BIO_new(BIO_s_mem()); + + bio_64 = BIO_push(bio_64, bio_mem_in); + + BIO_set_flags(bio_64, BIO_FLAGS_BASE64_NO_NL); + + BIO_write(bio_mem_in, in.c_str(), in.length()); + + while((inlen = BIO_read(bio_64, inbuf, 512)) > 0) + { + BIO_write(bio_mem_out, inbuf, inlen); + } + + size = BIO_get_mem_data(bio_mem_out, &decoded_c); + + string * decoded = new string(decoded_c, size); + + BIO_free_all(bio_64); + BIO_free_all(bio_mem_out); + + return decoded; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +string one_util::sha1_digest(const string& in) +{ + EVP_MD_CTX mdctx; + unsigned char md_value[EVP_MAX_MD_SIZE]; + unsigned int md_len; + ostringstream oss; + + EVP_MD_CTX_init(&mdctx); + EVP_DigestInit_ex(&mdctx, EVP_sha1(), NULL); + + EVP_DigestUpdate(&mdctx, in.c_str(), in.length()); + + EVP_DigestFinal_ex(&mdctx,md_value,&md_len); + EVP_MD_CTX_cleanup(&mdctx); + + for(unsigned int i = 0; i @@ -49,7 +50,7 @@ void InformationManagerDriver::protocol( int id; ostringstream ess; - string hinfo; + Host * host; // Parse the driver message @@ -98,27 +99,29 @@ void InformationManagerDriver::protocol( { size_t pos; int rc; + string hinfo64; + string* hinfo; ostringstream oss; - getline (is,hinfo); + getline (is, hinfo64); - for (pos=hinfo.find(',');pos!=string::npos;pos=hinfo.find(',')) - { - hinfo.replace(pos,1,"\n"); - } + hinfo = one_util::base64_decode(hinfo64); - hinfo += "\n"; + oss << "Host " << id << " successfully monitored."; + NebulaLog::log("InM", Log::DEBUG, oss); - oss << "Host " << id << " successfully monitored."; - NebulaLog::log("InM",Log::DEBUG,oss); - - rc = host->update_info(hinfo); + rc = host->update_info(*hinfo); if (rc != 0) { - goto error_parse_info; + ess << "Error parsing host information: " << *hinfo; + delete hinfo; + + goto error_common_info; } + + delete hinfo; } else { @@ -146,10 +149,6 @@ error_driver_info: ess << "Error monitoring host " << id << " : " << is.str(); goto error_common_info; -error_parse_info: - ess << "Error parsing host information: " << hinfo; - goto error_common_info; - error_common_info: NebulaLog::log("InM",Log::ERROR,ess); diff --git a/src/image/ImageManagerActions.cc b/src/image/ImageManagerActions.cc index 9a1a17a245..470f04896d 100644 --- a/src/image/ImageManagerActions.cc +++ b/src/image/ImageManagerActions.cc @@ -17,7 +17,6 @@ #include "ImageManager.h" #include "NebulaLog.h" #include "ImagePool.h" -#include "SSLTools.h" #include "SyncRequest.h" #include "Template.h" #include "Nebula.h" @@ -812,7 +811,7 @@ string * ImageManager::format_message( << ds_data << ""; - return SSLTools::base64_encode(oss.str()); + return one_util::base64_encode(oss.str()); } /* -------------------------------------------------------------------------- */ diff --git a/src/pool/PoolObjectSQL.cc b/src/pool/PoolObjectSQL.cc index 76ace6a665..b4bc95f356 100644 --- a/src/pool/PoolObjectSQL.cc +++ b/src/pool/PoolObjectSQL.cc @@ -16,7 +16,7 @@ #include "PoolObjectSQL.h" #include "PoolObjectAuth.h" -#include "SSLTools.h" +#include "Util.h" #include "Nebula.h" #include "Clusterable.h" @@ -29,7 +29,7 @@ string& PoolObjectSQL::to_xml64(string &xml64) to_xml(xml64); - str64 = SSLTools::base64_encode(xml64); + str64 = one_util::base64_encode(xml64); xml64 = *str64; diff --git a/src/rm/RequestManagerUser.cc b/src/rm/RequestManagerUser.cc index 464f3415ec..2907eb2069 100644 --- a/src/rm/RequestManagerUser.cc +++ b/src/rm/RequestManagerUser.cc @@ -15,6 +15,7 @@ /* -------------------------------------------------------------------------- */ #include "RequestManagerUser.h" +#include "Util.h" using namespace std; @@ -47,14 +48,14 @@ void RequestManagerUser:: failure_response(ACTION, request_error(error_str,""), att); return; } - + success_response(id, att); } /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int UserChangePassword::user_action(int user_id, +int UserChangePassword::user_action(int user_id, xmlrpc_c::paramList const& paramList, string& error_str) { @@ -71,7 +72,7 @@ int UserChangePassword::user_action(int user_id, if (user->get_auth_driver() == UserPool::CORE_AUTH) { - new_pass = SSLTools::sha1_digest(new_pass); + new_pass = one_util::sha1_digest(new_pass); } int rc = user->set_password(new_pass, error_str); @@ -128,7 +129,7 @@ int UserChangeAuth::user_action(int user_id, { if ( new_auth == UserPool::CORE_AUTH) { - new_pass = SSLTools::sha1_digest(new_pass); + new_pass = one_util::sha1_digest(new_pass); } // The password may be invalid, try to change it first @@ -170,7 +171,7 @@ void UserChangeAuth::log_xmlrpc_param( /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int UserSetQuota::user_action(int user_id, +int UserSetQuota::user_action(int user_id, xmlrpc_c::paramList const& paramList, string& error_str) { diff --git a/src/scheduler/src/client/Client.cc b/src/scheduler/src/client/Client.cc index 5e647d3e0c..0c3e0327f5 100644 --- a/src/scheduler/src/client/Client.cc +++ b/src/scheduler/src/client/Client.cc @@ -15,7 +15,6 @@ /* -------------------------------------------------------------------------- */ #include "Client.h" -#include "SSLTools.h" #include #include diff --git a/src/um/UserPool.cc b/src/um/UserPool.cc index c24b5f0cb4..d5560c834b 100644 --- a/src/um/UserPool.cc +++ b/src/um/UserPool.cc @@ -22,7 +22,7 @@ #include "NebulaLog.h" #include "Nebula.h" #include "AuthManager.h" -#include "SSLTools.h" +#include "Util.h" #include #include @@ -145,7 +145,7 @@ UserPool::UserPool(SqlDB * db, srand(time(0)); sstr << rand(); - random = SSLTools::sha1_digest( sstr.str() ); + random = one_util::sha1_digest(sstr.str()); filenames[0] = nd.get_var_location() + "/.one/sunstone_auth"; filenames[1] = nd.get_var_location() + "/.one/occi_auth"; @@ -188,7 +188,7 @@ UserPool::UserPool(SqlDB * db, GroupPool::ONEADMIN_ID, SERVER_NAME, GroupPool::ONEADMIN_NAME, - SSLTools::sha1_digest(random), + one_util::sha1_digest(random), "server_cipher", true, error_str); @@ -289,7 +289,7 @@ int UserPool::allocate ( if (auth_driver == UserPool::CORE_AUTH) { - upass = SSLTools::sha1_digest(password); + upass = one_util::sha1_digest(password); } // Build a new User object diff --git a/src/vmm/VirtualMachineManager.cc b/src/vmm/VirtualMachineManager.cc index 933a2bd911..b3bd9e0bd7 100644 --- a/src/vmm/VirtualMachineManager.cc +++ b/src/vmm/VirtualMachineManager.cc @@ -19,6 +19,7 @@ #include "XenDriver.h" #include "XMLDriver.h" #include "LibVirtDriver.h" +#include "Util.h" #include "Nebula.h" @@ -380,7 +381,7 @@ string * VirtualMachineManager::format_message( oss << tmpl << ""; - return SSLTools::base64_encode(oss.str()); + return one_util::base64_encode(oss.str()); } /* -------------------------------------------------------------------------- */