diff --git a/include/AuthManager.h b/include/AuthManager.h index 8d4e49990e..693f53ea7f 100644 --- a/include/AuthManager.h +++ b/include/AuthManager.h @@ -428,12 +428,12 @@ public: am.loop(0,0); }; - bool plain_authorize() + bool core_authorize() { return ( uid == 0 || self_authorize ); } - bool plain_authenticate() + bool core_authenticate() { return (password == session); } diff --git a/include/User.h b/include/User.h index ad589e183a..66fd26ba3b 100644 --- a/include/User.h +++ b/include/User.h @@ -164,6 +164,11 @@ private: */ string password; + /** + * Authentication driver for this user + */ + string auth_driver; + /** * Flag marking user enabled/disabled */ @@ -210,10 +215,12 @@ protected: int _gid, const string& _uname, const string& _gname, - const string& _password, + const string& _password, + const string& _auth_driver, bool _enabled): PoolObjectSQL(id,_uname,-1,_gid,"",_gname,table), password(_password), + auth_driver(_auth_driver), enabled(_enabled) { obj_template = new UserTemplate; diff --git a/include/UserPool.h b/include/UserPool.h index d077f3906c..140ea72124 100644 --- a/include/UserPool.h +++ b/include/UserPool.h @@ -54,6 +54,7 @@ public: const string& uname, const string& gname, const string& password, + const string& auth, bool enabled, string& error_str); @@ -133,6 +134,11 @@ public: return PoolSQL::dump(oss, "USER_POOL", User::table, where); }; + /** + * Name for the OpenNebula core authentication process + */ + static const char * CORE_AUTH; + private: /** * Factory method to produce User objects @@ -140,7 +146,7 @@ private: */ PoolObjectSQL * create() { - return new User(-1,-1,"","","",true); + return new User(-1,-1,"","","",UserPool::CORE_AUTH,true); }; }; diff --git a/src/authm/test/AuthManagerTest.cc b/src/authm/test/AuthManagerTest.cc index f23358069b..965ce1c142 100644 --- a/src/authm/test/AuthManagerTest.cc +++ b/src/authm/test/AuthManagerTest.cc @@ -297,28 +297,28 @@ public: ar.add_auth(AuthRequest::NET,2,1,AuthRequest::USE,2,false); ar.add_auth(AuthRequest::IMAGE,3,1,AuthRequest::USE,4,true); - CPPUNIT_ASSERT(ar.plain_authorize() == true); + CPPUNIT_ASSERT(ar.core_authorize() == true); ar1.add_auth(AuthRequest::VM,"dGhpcy",-1,AuthRequest::CREATE,2,false); ar1.add_auth(AuthRequest::NET,2,1,AuthRequest::USE,2,false); ar1.add_auth(AuthRequest::IMAGE,3,1,AuthRequest::USE,4,false); - CPPUNIT_ASSERT(ar1.plain_authorize() == false); + CPPUNIT_ASSERT(ar1.core_authorize() == false); ar2.add_auth(AuthRequest::HOST,"dGhpcy",-1,AuthRequest::CREATE,0,false); - CPPUNIT_ASSERT(ar2.plain_authorize() == false); + CPPUNIT_ASSERT(ar2.core_authorize() == false); ar3.add_auth(AuthRequest::VM,5,1,AuthRequest::MANAGE,2,false); - CPPUNIT_ASSERT(ar3.plain_authorize() == false); + CPPUNIT_ASSERT(ar3.core_authorize() == false); ar4.add_auth(AuthRequest::VM,4,1,AuthRequest::MANAGE,2,false); - CPPUNIT_ASSERT(ar4.plain_authorize() == true); + CPPUNIT_ASSERT(ar4.core_authorize() == true); ar5.add_auth(AuthRequest::HOST,4,-1,AuthRequest::MANAGE,0,false); - CPPUNIT_ASSERT(ar5.plain_authorize() == true); + CPPUNIT_ASSERT(ar5.core_authorize() == true); ar6.add_auth(AuthRequest::HOST,4,-1,AuthRequest::CREATE,0,false); - CPPUNIT_ASSERT(ar6.plain_authorize() == true); + CPPUNIT_ASSERT(ar6.core_authorize() == true); } void self_authenticate() @@ -327,10 +327,10 @@ public: AuthRequest ar1(2,2); ar.add_authenticate("the_user","the_pass","the_secret"); - CPPUNIT_ASSERT(ar.plain_authenticate() == false); + CPPUNIT_ASSERT(ar.core_authenticate() == false); ar1.add_authenticate("the_user","the_pass","the_pass"); - CPPUNIT_ASSERT(ar1.plain_authenticate() == true); + CPPUNIT_ASSERT(ar1.core_authenticate() == true); } }; diff --git a/src/authm_mad/remotes/dummy/authenticate b/src/authm_mad/remotes/dummy/authenticate index 2e28d18be9..d0261ff292 100755 --- a/src/authm_mad/remotes/dummy/authenticate +++ b/src/authm_mad/remotes/dummy/authenticate @@ -19,7 +19,8 @@ # $1 = username # $2 = "-" if user is not registered in opennebula # $3 = password -echo $1 $3 +echo core $1 $3 + diff --git a/src/cli/one_helper/oneuser_helper.rb b/src/cli/one_helper/oneuser_helper.rb index 71ff93b27b..0cce29c43f 100644 --- a/src/cli/one_helper/oneuser_helper.rb +++ b/src/cli/one_helper/oneuser_helper.rb @@ -161,10 +161,12 @@ class OneUserHelper < OpenNebulaHelper::OneHelper str_h1="%-80s" CLIHelper.print_header(str_h1 % "USER #{user['ID']} INFORMATION") - puts str % ["ID", user.id.to_s] - puts str % ["NAME", user.name] - puts str % ["GROUP", user.gid] - puts str % ["PASSWORD", user['PASSWORD']] + puts str % ["ID", user.id.to_s] + puts str % ["NAME", user.name] + puts str % ["GROUP", user.gid] + puts str % ["PASSWORD", user['PASSWORD']] + puts str % ["AUTH_DRIVER", user['AUTH_DRIVER']] + puts str % ["ENABLED", OpenNebulaHelper.boolean_to_str(user['ENABLED'])] diff --git a/src/cli/oneuser b/src/cli/oneuser index 6774684370..d97b9491cc 100755 --- a/src/cli/oneuser +++ b/src/cli/oneuser @@ -105,7 +105,15 @@ cmd=CommandParser::CmdParser.new(ARGV) do :description => "Token duration in seconds, defaults to 3600 (1 h)" } - create_options = [READ_FILE, PLAIN, SSH, X509, KEY, CERT] + DRIVER={ + :name => "driver", + :short => "-d driver_name", + :large => "--driver driver_name", + :format => String, + :description => "Authentication driver to be used with this user" + } + + create_options = [READ_FILE, PLAIN, SSH, X509, KEY, CERT, DRIVER] login_options = [SSH, X509, X509_PROXY, KEY, CERT, PROXY, TIME] ######################################################################## @@ -159,7 +167,8 @@ cmd=CommandParser::CmdParser.new(ARGV) do end helper.create_resource(options) do |user| - user.allocate(args[0], pass) + driver = options[:driver] || OpenNebula::User::CORE_AUTH + user.allocate(args[0], pass, driver) end end diff --git a/src/oca/ruby/OpenNebula/User.rb b/src/oca/ruby/OpenNebula/User.rb index 9415ed6e91..2f0673e50c 100644 --- a/src/oca/ruby/OpenNebula/User.rb +++ b/src/oca/ruby/OpenNebula/User.rb @@ -32,7 +32,10 @@ module OpenNebula :update => "user.update" } - SELF = -1 + SELF = -1 + + #Selects core authentication driver + CORE_AUTH = "core" # Creates a User description with just its identifier # this method should be used to create plain User objects. @@ -72,8 +75,8 @@ module OpenNebula # +username+ Name of the new user. # # +password+ Password for the new user - def allocate(username, password) - super(USER_METHODS[:allocate], username, password) + def allocate(username, password, driver=CORE_AUTH) + super(USER_METHODS[:allocate], username, password, driver) end # Replaces the template contents diff --git a/src/rm/RequestManagerAllocate.cc b/src/rm/RequestManagerAllocate.cc index abaa83ff36..eb60f27e08 100644 --- a/src/rm/RequestManagerAllocate.cc +++ b/src/rm/RequestManagerAllocate.cc @@ -230,6 +230,7 @@ int UserAllocate::pool_allocate(xmlrpc_c::paramList const& paramList, { string uname = xmlrpc_c::value_string(paramList.getString(1)); string passwd = xmlrpc_c::value_string(paramList.getString(2)); + string driver = xmlrpc_c::value_string(paramList.getString(3)); UserPool * upool = static_cast(pool); @@ -242,7 +243,12 @@ int UserAllocate::pool_allocate(xmlrpc_c::paramList const& paramList, ugname = GroupPool::USERS_NAME; } - return upool->allocate(&id,ugid,uname,ugname,passwd,true,error_str); + if (driver.empty()) + { + driver = UserPool::CORE_AUTH; + } + + return upool->allocate(&id,ugid,uname,ugname,passwd,driver,true,error_str); } /* -------------------------------------------------------------------------- */ diff --git a/src/um/User.cc b/src/um/User.cc index 0d92f26c65..628e3f6f59 100644 --- a/src/um/User.cc +++ b/src/um/User.cc @@ -128,13 +128,14 @@ string& User::to_xml(string& xml) const oss << "" - "" << oid <<"" << - "" << gid <<"" << - "" << gname <<"" << - "" << name <<"" << - "" << password <<""<< - "" << enabled_int <<"" << - obj_template->to_xml(template_xml) << + "" << oid <<"" << + "" << gid <<"" << + "" << gname <<"" << + "" << name <<"" << + "" << password <<"" << + "" << auth_driver <<""<< + "" << enabled_int <<"" << + obj_template->to_xml(template_xml) << ""; xml = oss.str(); @@ -154,12 +155,13 @@ int User::from_xml(const string& xml) // Initialize the internal XML object update_from_str(xml); - rc += xpath(oid, "/USER/ID", -1); - rc += xpath(gid, "/USER/GID", -1); - rc += xpath(gname, "/USER/GNAME", "not_found"); - rc += xpath(name, "/USER/NAME", "not_found"); - rc += xpath(password, "/USER/PASSWORD", "not_found"); - rc += xpath(int_enabled, "/USER/ENABLED", 0); + rc += xpath(oid, "/USER/ID", -1); + rc += xpath(gid, "/USER/GID", -1); + rc += xpath(gname, "/USER/GNAME", "not_found"); + rc += xpath(name, "/USER/NAME", "not_found"); + rc += xpath(password, "/USER/PASSWORD", "not_found"); + rc += xpath(auth_driver,"/USER/AUTH_DRIVER", UserPool::CORE_AUTH); + rc += xpath(int_enabled,"/USER/ENABLED", 0); enabled = int_enabled; diff --git a/src/um/UserPool.cc b/src/um/UserPool.cc index f2ba70a7a7..b2bbef07a7 100644 --- a/src/um/UserPool.cc +++ b/src/um/UserPool.cc @@ -29,6 +29,8 @@ #include #include +const char * UserPool::CORE_AUTH = "core"; + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -93,6 +95,7 @@ UserPool::UserPool(SqlDB * db):PoolSQL(db,User::table) one_name, GroupPool::ONEADMIN_NAME, sha1_pass, + UserPool::CORE_AUTH, true, error_str); } @@ -125,6 +128,7 @@ int UserPool::allocate ( const string& uname, const string& gname, const string& password, + const string& auth, bool enabled, string& error_str) { @@ -134,6 +138,8 @@ int UserPool::allocate ( GroupPool * gpool; Group * group; + string auth_driver = auth; + ostringstream oss; if ( !User::is_valid(password, error_str) ) @@ -153,8 +159,13 @@ int UserPool::allocate ( goto error_duplicated; } + if (auth_driver.empty()) + { + auth_driver = UserPool::CORE_AUTH; + } + // Build a new User object - user = new User(-1, gid, uname, gname, password, enabled); + user = new User(-1, gid, uname, gname, password, auth_driver, enabled); // Insert the Object in the pool *oid = PoolSQL::allocate(user, error_str); @@ -213,7 +224,8 @@ bool UserPool::authenticate(const string& session, User * user = 0; string username; - string secret, u_pass; + string secret, u_secret, u_pass; + string auth_driver; string tuname; string tgname; @@ -231,13 +243,14 @@ bool UserPool::authenticate(const string& session, gname = ""; result = false; - rc = User::split_secret(session,username,secret); + rc = User::split_secret(session,username,u_secret); if ( rc != 0 ) { return -1; } + user = get(username,true); if (user != 0) //User known to OpenNebula @@ -249,38 +262,57 @@ bool UserPool::authenticate(const string& session, tuname = user->name; tgname = user->gname; + auth_driver = user->auth_driver; + user->unlock(); } else //External User { - u_pass = "-"; + u_pass = "-"; + auth_driver = ""; + uid = -1; gid = -1; } AuthRequest ar(uid, gid); - ar.add_authenticate(username,u_pass,secret); - - if (authm == 0) //plain auth + if ( auth_driver == UserPool::CORE_AUTH ) { - if ( user != 0 && ar.plain_authenticate()) //no plain for external users + if (user != 0) //no core auth for external users { - user_id = uid; - group_id = gid; + ar.add_authenticate(username,u_pass,u_secret); - uname = tuname; - gname = tgname; + if (ar.core_authenticate()) + { + user_id = uid; + group_id = gid; - result = true; + uname = tuname; + gname = tgname; + + result = true; + } } } - else //use the driver + else if ( authm != 0 ) //use auth driver if it was loaded { + //Compose secret for the user driver + if (!auth_driver.empty()) + { + secret = auth_driver; + secret += ":"; + } + + secret += u_secret; + + //Initialize authentication request and call the driver + ar.add_authenticate(username,u_pass,secret); + authm->trigger(AuthManager::AUTHENTICATE,&ar); ar.wait(); - if (ar.result==true) + if (ar.result==true) //User was authenticated { if ( user != 0 ) //knwon user_id { @@ -294,13 +326,20 @@ bool UserPool::authenticate(const string& session, } else //External user, username & pass in driver message { + string driver_name; string mad_name; string mad_pass; + string error_str; istringstream is(ar.message); if ( is.good() ) + { + is >> driver_name >> ws; + } + + if ( !is.fail() ) { is >> mad_name >> ws; } @@ -317,6 +356,7 @@ bool UserPool::authenticate(const string& session, mad_name, GroupPool::USERS_NAME, mad_pass, + driver_name, true, error_str); } @@ -349,6 +389,12 @@ bool UserPool::authenticate(const string& session, NebulaLog::log("AuM",Log::ERROR,oss); } } + else + { + NebulaLog::log("AuM",Log::ERROR, + "Auth Error: Authentication driver not enabled. " + "Check AUTH_MAD in oned.conf"); + } return result; } @@ -364,7 +410,7 @@ int UserPool::authorize(AuthRequest& ar) if (authm == 0) { - if (ar.plain_authorize()) + if (ar.core_authorize()) { rc = 0; } diff --git a/src/um/test/UserPoolTest.cc b/src/um/test/UserPoolTest.cc index 09be4b1588..3da94b80d6 100644 --- a/src/um/test/UserPoolTest.cc +++ b/src/um/test/UserPoolTest.cc @@ -98,7 +98,7 @@ protected: string err; return ((UserPool*)pool)->allocate(&oid, 0, usernames[index],"oneadmin", - passwords[index], true, err); + passwords[index], UserPool::CORE_AUTH, true, err); }; void check(int index, PoolObjectSQL* obj) @@ -296,17 +296,17 @@ public: UserPool * up = static_cast(pool); // Allocate a user. - rc = up->allocate(&oid, 0,usernames[0], "oneadmin",passwords[0], true, err); + rc = up->allocate(&oid, 0,usernames[0], "oneadmin",passwords[0], UserPool::CORE_AUTH,true, err); CPPUNIT_ASSERT( oid == 1 ); CPPUNIT_ASSERT( oid == rc ); // Try to allocate twice the same user, should fail - rc = up->allocate(&oid, 0,usernames[0], "oneadmin", passwords[0], true, err); + rc = up->allocate(&oid, 0,usernames[0], "oneadmin", passwords[0],UserPool::CORE_AUTH,true, err); CPPUNIT_ASSERT( rc == -1 ); CPPUNIT_ASSERT( oid == rc ); // Try again, with different password - rc = up->allocate(&oid, 0, usernames[0], "oneadmin", passwords[1], true, err); + rc = up->allocate(&oid, 0, usernames[0], "oneadmin", passwords[1],UserPool::CORE_AUTH,true, err); CPPUNIT_ASSERT( rc == -1 ); CPPUNIT_ASSERT( oid == rc ); } @@ -321,7 +321,7 @@ public: for(int i=0; i<5; i++) { - ((UserPool*)pool)->allocate(&oid, 0, d_names[i], "oneadmin", d_pass[i], true, err); + ((UserPool*)pool)->allocate(&oid, 0, d_names[i], "oneadmin", d_pass[i],UserPool::CORE_AUTH, true, err); } ostringstream oss; @@ -348,7 +348,7 @@ public: for(int i=0; i<5; i++) { - ((UserPool*)pool)->allocate(&oid, 0, d_names[i], "oneadmin",d_pass[i], true, err); + ((UserPool*)pool)->allocate(&oid, 0, d_names[i], "oneadmin",d_pass[i], UserPool::CORE_AUTH,true, err); } // Note: second parameter of dump is the WHERE constraint. The "order