2011-06-22 21:22:52 +04:00
/* -------------------------------------------------------------------------- */
2018-01-02 20:27:37 +03:00
/* Copyright 2002-2018, OpenNebula Project, OpenNebula Systems */
2011-06-22 21:22:52 +04:00
/* */
/* 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 ACL_MANAGER_H_
# define ACL_MANAGER_H_
# include "AuthManager.h"
2012-05-31 18:51:07 +04:00
# include "AuthRequest.h"
2012-01-03 05:58:23 +04:00
# include "PoolObjectSQL.h"
2011-06-22 21:22:52 +04:00
# include "AclRule.h"
2017-02-03 16:19:15 +03:00
# include "NebulaLog.h"
2011-06-22 21:22:52 +04:00
2011-07-03 15:12:00 +04:00
# include "SqlDB.h"
2011-06-22 21:22:52 +04:00
using namespace std ;
2012-01-03 05:58:23 +04:00
class PoolObjectAuth ;
2014-01-27 20:44:27 +04:00
extern " C " void * acl_action_loop ( void * arg ) ;
2011-06-27 20:41:16 +04:00
/**
* This class manages the ACL rules and the authorization engine
*/
2014-01-27 20:44:27 +04:00
class AclManager : public Callbackable , public ActionListener
2011-06-22 21:22:52 +04:00
{
public :
2014-01-30 14:41:56 +04:00
/**
* @ param _db pointer to the DB
* @ param zone_id of the Zone
2014-02-18 21:54:36 +04:00
* @ param is_federation_slave true is this oned is a federation slave . If
* it is true , it will reload periodically rules from the DB
2014-01-30 14:41:56 +04:00
* @ param timer_period period to reload the rules
*/
2014-02-18 21:54:36 +04:00
AclManager ( SqlDB * _db , int zone_id , bool is_federation_slave , time_t timer ) ;
2011-07-05 18:32:18 +04:00
virtual ~ AclManager ( ) ;
2011-06-24 15:22:17 +04:00
2011-06-27 20:41:16 +04:00
/**
2014-01-27 20:44:27 +04:00
* Loads the ACL rule set from the DB , and starts the refresh loop is
* refresh_cache is set
2011-06-27 20:41:16 +04:00
* @ return 0 on success .
*/
int start ( ) ;
2014-01-27 20:44:27 +04:00
void finalize ( ) ;
2017-05-09 00:57:51 +03:00
/**
* Reload the ACL rules from the DB . This function needs to be used when
* a server becomes leader of the zone as the ACL cache maybe out - dated
*/
void reload_rules ( )
{
select ( ) ;
}
2011-06-22 21:22:52 +04:00
/* ---------------------------------------------------------------------- */
/* Rule management */
/* ---------------------------------------------------------------------- */
2011-06-27 20:41:16 +04:00
/**
* Takes an authorization request and checks if any rule in the ACL
* authorizes the operation .
*
* @ param uid The user ID requesting to be authorized
2013-08-23 14:39:14 +04:00
* @ param user_groups Set of group IDs that the user is part of
2011-12-30 01:05:11 +04:00
* @ param obj_perms The object ' s permission attributes
2011-06-27 20:41:16 +04:00
* @ param op The operation to be authorized
* @ return true if the authorization is granted by any rule
*/
2012-01-02 22:21:54 +04:00
const bool authorize ( int uid ,
2013-08-23 14:39:14 +04:00
const set < int > & user_groups ,
2012-01-03 05:58:23 +04:00
const PoolObjectAuth & obj_perms ,
2012-01-02 22:21:54 +04:00
AuthRequest : : Operation op ) ;
2011-12-30 01:05:11 +04:00
2011-06-27 20:41:16 +04:00
/**
* Adds a new rule to the ACL rule set
*
* @ param user 64 bit ID and flags
* @ param resource 64 bit ID and flags
* @ param rights 64 bit flags
2014-01-21 15:52:25 +04:00
* @ param zone 64 bit flags
2011-06-27 20:41:16 +04:00
* @ param error_str Returns the error reason , if any
2011-06-29 20:41:49 +04:00
*
* @ return the oid assigned to the rule on success ,
* - 1 if the rule exists ,
* - 2 if the rule is malformed ,
* - 3 if the DB insert failed
2011-06-27 20:41:16 +04:00
*/
2014-01-30 14:41:56 +04:00
virtual int add_rule ( long long user ,
long long resource ,
2011-07-05 18:32:18 +04:00
long long rights ,
2014-01-21 15:52:25 +04:00
long long zone ,
2011-07-05 18:32:18 +04:00
string & error_str ) ;
2011-06-27 20:41:16 +04:00
/**
* Deletes a rule from the ACL rule set
*
2011-06-29 20:41:49 +04:00
* @ param oid Rule id
2011-06-27 20:41:16 +04:00
* @ param error_str Returns the error reason , if any
* @ return 0 on success
*/
2011-07-05 18:32:18 +04:00
virtual int del_rule ( int oid , string & error_str ) ;
2011-06-22 21:22:52 +04:00
2013-12-13 14:43:34 +04:00
/**
* Deletes a new rule from the ACL rule set
*
* @ param user 64 bit ID and flags
* @ param resource 64 bit ID and flags
* @ param rights 64 bit flags
2014-01-21 15:52:25 +04:00
* @ param zone 64 bit flags
2013-12-13 14:43:34 +04:00
*
* @ param error_str Returns the error reason , if any
* @ return 0 on success
*/
virtual int del_rule ( long long user ,
long long resource ,
long long rights ,
2014-01-21 15:52:25 +04:00
long long zone ,
2013-12-13 14:43:34 +04:00
string & error_str ) ;
2012-04-11 16:45:46 +04:00
/**
* Deletes rules that apply to this user id
*
* @ param uid The user id
*/
void del_uid_rules ( int uid ) ;
/**
* Deletes rules that apply to this group id
*
* @ param gid The group id
*/
void del_gid_rules ( int gid ) ;
2013-01-23 19:46:14 +04:00
/**
* Deletes rules that apply to this cluster id
*
* @ param cid The cluster id
*/
void del_cid_rules ( int cid ) ;
2014-02-21 18:23:24 +04:00
/**
* Deletes rules that apply to this cluster id
*
* @ param zid The zone id
*/
void del_zid_rules ( int zid ) ;
2012-04-16 18:24:42 +04:00
/**
* Deletes all rules that apply to this resource
*
* @ param oid Id of the deleted object
* @ param obj_type Object type
*/
void del_resource_rules ( int oid , PoolObjectSQL : : ObjectType obj_type ) ;
2011-12-01 21:56:29 +04:00
/**
* Searches what resources of type obj_type the ACL rules set allows
* the given user to perform the operation .
*
* @ param uid The user ID
2013-08-23 17:36:43 +04:00
* @ param user_groups Set of group IDs that the user is part of
2011-12-01 21:56:29 +04:00
* @ param obj_type The object over which the search will be performed
* @ param op The operation to be searched
* @ param all True if the user can perform the operation over any object
* @ param oids Set of object IDs over which the user can operate
* @ param gids Set of object group IDs over which the user can operate
2013-01-17 15:33:33 +04:00
* @ param cids Set of object cluster IDs over which the user can operate
2011-12-01 21:56:29 +04:00
*/
2012-01-03 05:58:23 +04:00
void reverse_search ( int uid ,
2013-08-23 17:36:43 +04:00
const set < int > & user_groups ,
2012-01-03 05:58:23 +04:00
PoolObjectSQL : : ObjectType obj_type ,
AuthRequest : : Operation op ,
2014-09-23 22:18:18 +04:00
bool disable_all_acl ,
bool disable_cluster_acl ,
bool disable_group_acl ,
2012-01-03 05:58:23 +04:00
bool & all ,
vector < int > & oids ,
2013-01-17 15:33:33 +04:00
vector < int > & gids ,
vector < int > & cids ) ;
2011-12-01 21:56:29 +04:00
2011-06-22 21:22:52 +04:00
/* ---------------------------------------------------------------------- */
/* DB management */
/* ---------------------------------------------------------------------- */
/**
2011-06-27 20:41:16 +04:00
* Bootstraps the database table ( s ) associated to the ACL Manager
2011-10-10 17:14:46 +04:00
* @ return 0 on success
2011-06-22 21:22:52 +04:00
*/
2011-10-10 17:14:46 +04:00
static int bootstrap ( SqlDB * _db ) ;
2011-06-22 21:22:52 +04:00
/**
2011-06-27 20:41:16 +04:00
* Dumps the rule set in XML format .
* @ param oss The output stream to dump the rule set contents
2011-06-22 21:22:52 +04:00
* @ return 0 on success
*/
2011-07-05 18:32:18 +04:00
virtual int dump ( ostringstream & oss ) ;
2011-06-22 21:22:52 +04:00
2017-05-09 00:57:51 +03:00
// -------------------------------------------------------------------------
2014-01-27 20:44:27 +04:00
// Refresh loop thread
2017-05-09 00:57:51 +03:00
// -------------------------------------------------------------------------
2014-01-27 20:44:27 +04:00
/**
* Gets the AclManager thread identification . The thread is only
* initialized if the refresh_cache flag is true .
* @ return pthread_t for the manager thread ( that in the action loop ) .
*/
pthread_t get_thread_id ( ) const
{
return acl_thread ;
} ;
2011-07-05 18:32:18 +04:00
protected :
2014-01-30 14:41:56 +04:00
/**
* Constructor for derived ACL managers . Classes derived from this one
* will operate in a stand - alone fashion ( i . e . no refresh of ACL rules
* from DB )
*/
AclManager ( int _zone_id )
2017-05-09 00:57:51 +03:00
: zone_id ( _zone_id ) , db ( 0 ) , is_federation_slave ( false )
2014-01-30 14:41:56 +04:00
{
pthread_mutex_init ( & mutex , 0 ) ;
} ;
2017-05-09 00:57:51 +03:00
// -------------------------------------------------------------------------
2011-06-30 13:41:27 +04:00
// ACL rules management
2017-05-09 00:57:51 +03:00
// -------------------------------------------------------------------------
2011-06-29 20:41:49 +04:00
/**
* ACL rules . Each rule is indexed by its ' user ' long long attibute ,
* several rules can apply to the same user
*/
2011-06-27 20:41:16 +04:00
multimap < long long , AclRule * > acl_rules ;
2011-06-22 21:22:52 +04:00
2011-06-29 20:41:49 +04:00
/**
* Rules indexed by oid . Stores the same rules as acl_rules
*/
map < int , AclRule * > acl_rules_oids ;
2011-07-05 18:32:18 +04:00
private :
2011-06-29 14:50:16 +04:00
/**
* Gets all rules that apply to the user_req and , if any of them grants
* permission , returns true .
*
* @ param user_req user / group id and flags
* @ param resource_oid_req 64 bit request , ob . type and individual oid
* @ param resource_gid_req 64 bit request , ob . type and group id
2013-01-16 21:27:36 +04:00
* @ param resource_cid_req 64 bit request , ob . type and cluster id
2011-06-29 14:50:16 +04:00
* @ param resource_all_req 64 bit request , ob . type and all flag
* @ param rights_req Requested rights
* @ param individual_obj_type Mask with ob . type and individual flags
2011-12-30 19:27:42 +04:00
* @ param group_obj_type Mask with ob . type and group flags
2013-01-16 21:27:36 +04:00
* @ param cluster_obj_type Mask with ob . type and cluster flags
2011-12-30 19:27:42 +04:00
* @ param rules ACL rules to match
2011-06-29 14:50:16 +04:00
*
* @ return true if any rule grants permission
*/
bool match_rules (
2016-03-20 01:13:10 +03:00
long long user_req ,
long long resource_oid_req ,
long long resource_gid_req ,
const set < long long > & resource_cid_req ,
long long resource_all_req ,
long long rights_req ,
long long resource_oid_mask ,
long long resource_gid_mask ,
long long resource_cid_mask ,
const multimap < long long , AclRule * > & rules ) ;
2011-12-30 19:27:42 +04:00
/**
* Wrapper for match_rules . It will check if any rules in the temporary
* multimap or in the internal one grants permission .
*
* @ param user_req user / group id and flags
* @ param resource_oid_req 64 bit request , ob . type and individual oid
* @ param resource_gid_req 64 bit request , ob . type and group id
2013-01-16 21:27:36 +04:00
* @ param resource_cid_req 64 bit request , ob . type and cluster id
2011-12-30 19:27:42 +04:00
* @ param resource_all_req 64 bit request , ob . type and all flag
* @ param rights_req Requested rights
* @ param individual_obj_type Mask with ob . type and individual flags
* @ param group_obj_type Mask with ob . type and group flags
2013-01-16 21:27:36 +04:00
* @ param cluster_obj_type Mask with ob . type and cluster flags
2011-12-30 19:27:42 +04:00
* @ param tmp_rules Temporary map group of ACL rules
*
* @ return true if any rule grants permission
*/
bool match_rules_wrapper (
2016-03-20 01:13:10 +03:00
long long user_req ,
long long resource_oid_req ,
long long resource_gid_req ,
const set < long long > & resource_cid_req ,
long long resource_all_req ,
long long rights_req ,
long long individual_obj_type ,
long long group_obj_type ,
long long cluster_obj_type ,
2016-03-11 17:42:17 +03:00
const multimap < long long , AclRule * > & tmp_rules ) ;
2012-04-11 16:45:46 +04:00
/**
* Deletes all rules that match the user mask
*
* @ param user_req Mask to match
*/
void del_user_matching_rules ( long long user_req ) ;
2012-04-16 17:06:06 +04:00
/**
* Deletes all rules that match the resource mask
*
* @ param resource_req 64 bit request , ob . type and group id
* @ param resource_mask Mask with ob . type and group flags
*/
void del_resource_matching_rules (
long long resource_req ,
long long resource_mask ) ;
2014-02-21 18:23:24 +04:00
/**
* Deletes all rules that match the zone mask
*
* @ param zone_req Mask to match
*/
void del_zone_matching_rules ( long long zone_req ) ;
2017-05-09 00:57:51 +03:00
// -------------------------------------------------------------------------
2014-01-24 21:50:09 +04:00
// Local zone
2017-05-09 00:57:51 +03:00
// -------------------------------------------------------------------------
2014-01-24 21:50:09 +04:00
int zone_id ;
2014-01-21 15:52:25 +04:00
2017-05-09 00:57:51 +03:00
// -------------------------------------------------------------------------
2011-06-30 13:41:27 +04:00
// Mutex synchronization
2017-05-09 00:57:51 +03:00
// -------------------------------------------------------------------------
2011-06-30 13:41:27 +04:00
pthread_mutex_t mutex ;
/**
* Function to lock the manager
*/
void lock ( )
{
pthread_mutex_lock ( & mutex ) ;
} ;
/**
* Function to unlock the manager
*/
void unlock ( )
{
pthread_mutex_unlock ( & mutex ) ;
} ;
2017-05-09 00:57:51 +03:00
// -------------------------------------------------------------------------
2011-06-27 20:41:16 +04:00
// DataBase implementation variables
2017-05-09 00:57:51 +03:00
// -------------------------------------------------------------------------
2011-06-27 20:41:16 +04:00
/**
* Pointer to the database .
*/
SqlDB * db ;
2011-06-22 21:22:52 +04:00
2011-06-29 20:41:49 +04:00
/**
* Tablename for the ACL rules
*/
2011-06-27 20:41:16 +04:00
static const char * table ;
2011-06-22 21:22:52 +04:00
2011-06-27 20:41:16 +04:00
static const char * db_names ;
2011-06-22 21:22:52 +04:00
2011-06-27 20:41:16 +04:00
static const char * db_bootstrap ;
/**
* Callback function to unmarshall the ACL rules
* @ param num the number of columns read from the DB
* @ param names the column names
* @ param vaues the column values
* @ return 0 on success
*/
int select_cb ( void * nil , int num , char * * values , char * * names ) ;
/**
* Reads the ACL rule set from the database .
* @ param db pointer to the db
* @ return 0 on success
*/
int select ( ) ;
/**
* Inserts the ACL rule in the database .
* @ param rule to insert
* @ return 0 on success
*/
2011-07-04 21:14:43 +04:00
int insert ( AclRule * rule )
{
return insert ( rule , db ) ;
} ;
/**
* Inserts the ACL rule in the database .
* @ param rule to insert
* @ db db pointer
*
* @ return 0 on success
*/
static int insert ( AclRule * rule , SqlDB * db ) ;
2011-06-27 20:41:16 +04:00
/**
* Drops an ACL rule from the database
2011-06-29 20:41:49 +04:00
*
* @ param oid Rule id
2011-06-27 20:41:16 +04:00
* @ return 0 on success
*/
2011-06-29 20:41:49 +04:00
int drop ( int oid ) ;
2017-05-09 00:57:51 +03:00
// -------------------------------------------------------------------------
2014-01-27 20:44:27 +04:00
// Refresh loop thread
2017-05-09 00:57:51 +03:00
// -------------------------------------------------------------------------
2014-01-27 20:44:27 +04:00
/**
* Flag to refresh the cache periodically
*/
2014-01-30 14:41:56 +04:00
bool is_federation_slave ;
2014-01-27 20:44:27 +04:00
/**
* Timer period for the cache refresh loop .
*/
time_t timer_period ;
/**
* Thread id for the ACL Manager
*/
pthread_t acl_thread ;
/**
* Action engine for the Manager
*/
ActionManager am ;
/**
* Function to execute the Manager action loop method within a new pthread
* ( requires C linkage )
*/
friend void * acl_action_loop ( void * arg ) ;
2017-02-03 16:19:15 +03:00
// -------------------------------------------------------------------------
// Action Listener interface
// -------------------------------------------------------------------------
void timer_action ( const ActionRequest & ar )
{
select ( ) ;
} ;
void finalize_action ( const ActionRequest & ar )
{
NebulaLog : : log ( " ACL " , Log : : INFO , " Stopping ACL Manager... " ) ;
} ;
2011-06-22 21:22:52 +04:00
} ;
# endif /*ACL_MANAGER_H*/