2008-06-17 20:27:32 +04:00
/* -------------------------------------------------------------------------- */
2020-04-30 16:00:02 +03:00
/* Copyright 2002-2020, OpenNebula Project, OpenNebula Systems */
2008-06-17 20:27:32 +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 POOL_OBJECT_SQL_H_
# define POOL_OBJECT_SQL_H_
# include "ObjectSQL.h"
2011-02-24 20:12:26 +03:00
# include "ObjectXML.h"
2011-04-08 03:02:55 +04:00
# include "Template.h"
2012-01-03 00:17:20 +04:00
2008-06-17 20:27:32 +04:00
# include <pthread.h>
2019-12-10 13:45:15 +03:00
# include <string>
2008-06-17 20:27:32 +04:00
2012-01-03 00:17:20 +04:00
class PoolObjectAuth ;
2012-01-02 22:21:54 +04:00
2008-06-17 20:27:32 +04:00
/**
2010-04-03 18:54:54 +04:00
* PoolObject class . Provides a SQL backend interface for Pool components . Each
2008-06-17 20:27:32 +04:00
* object is identified with and unique OID
2010-04-03 18:54:54 +04:00
*
* Note : The PoolObject provides a synchronization mechanism ( mutex ) . This
2008-06-17 20:27:32 +04:00
* implementation assumes that the mutex IS LOCKED when the class destructor
2010-04-03 18:54:54 +04:00
* is called .
2008-06-17 20:27:32 +04:00
*/
2011-02-24 20:12:26 +03:00
class PoolObjectSQL : public ObjectSQL , public ObjectXML
2008-06-17 20:27:32 +04:00
{
public :
2012-01-03 05:58:23 +04:00
/* ---------------------------------------------------------------------- */
/* Class Constructors & Constants */
/* ---------------------------------------------------------------------- */
/**
* OpenNebula objects . This definitions are used by other core components
* like the AuthZ / AuthN module
*/
enum ObjectType
{
2016-02-02 16:17:20 +03:00
NONE = 0x0000000000000000LL ,
2015-12-10 17:39:23 +03:00
VM = 0x0000001000000000LL ,
HOST = 0x0000002000000000LL ,
NET = 0x0000004000000000LL ,
IMAGE = 0x0000008000000000LL ,
USER = 0x0000010000000000LL ,
TEMPLATE = 0x0000020000000000LL ,
GROUP = 0x0000040000000000LL ,
ACL = 0x0000080000000000LL ,
DATASTORE = 0x0000100000000000LL ,
CLUSTER = 0x0000200000000000LL ,
DOCUMENT = 0x0000400000000000LL ,
ZONE = 0x0000800000000000LL ,
SECGROUP = 0x0001000000000000LL ,
VDC = 0x0002000000000000LL ,
VROUTER = 0x0004000000000000LL ,
MARKETPLACE = 0x0008000000000000LL ,
2017-01-02 18:43:44 +03:00
MARKETPLACEAPP = 0x0010000000000000LL ,
2018-11-20 19:24:59 +03:00
VMGROUP = 0x0020000000000000LL ,
2019-09-09 15:43:51 +03:00
VNTEMPLATE = 0x0040000000000000LL ,
HOOK = 0x0080000000000000LL
2012-01-03 05:58:23 +04:00
} ;
2018-01-24 17:33:12 +03:00
/**
* OpenNebula objects . This definitions are used for define the level of lock
*/
enum LockStates
{
ST_NONE = 0x0LL ,
ST_USE = 0x1LL ,
ST_MANAGE = 0x2LL ,
ST_ADMIN = 0x4LL
} ;
static const long int LockableObject ;
2020-07-02 23:42:10 +03:00
static std : : string type_to_str ( ObjectType ob )
2012-01-03 05:58:23 +04:00
{
switch ( ob )
{
2019-09-03 17:31:51 +03:00
case VM : return " VM " ; break ;
case HOST : return " HOST " ; break ;
case NET : return " NET " ; break ;
case IMAGE : return " IMAGE " ; break ;
case USER : return " USER " ; break ;
case TEMPLATE : return " TEMPLATE " ; break ;
case GROUP : return " GROUP " ; break ;
case ACL : return " ACL " ; break ;
case DATASTORE : return " DATASTORE " ; break ;
case CLUSTER : return " CLUSTER " ; break ;
case DOCUMENT : return " DOCUMENT " ; break ;
case ZONE : return " ZONE " ; break ;
case SECGROUP : return " SECGROUP " ; break ;
case VDC : return " VDC " ; break ;
case VROUTER : return " VROUTER " ; break ;
case MARKETPLACE : return " MARKETPLACE " ; break ;
case MARKETPLACEAPP : return " MARKETPLACEAPP " ; break ;
case VMGROUP : return " VMGROUP " ; break ;
2018-11-20 19:24:59 +03:00
case VNTEMPLATE : return " VNTEMPLATE " ; break ;
2019-09-09 15:43:51 +03:00
case HOOK : return " HOOK " ; break ;
2015-12-10 17:39:23 +03:00
default : return " " ;
2012-01-03 05:58:23 +04:00
}
} ;
2020-07-02 23:42:10 +03:00
static ObjectType str_to_type ( const std : : string & type )
2019-09-03 17:31:51 +03:00
{
if ( type = = " VM " ) return VM ;
else if ( type = = " HOST " ) return HOST ;
else if ( type = = " NET " ) return NET ;
else if ( type = = " IMAGE " ) return IMAGE ;
else if ( type = = " USER " ) return USER ;
else if ( type = = " TEMPLATE " ) return TEMPLATE ;
else if ( type = = " GROUP " ) return GROUP ;
else if ( type = = " ACL " ) return ACL ;
else if ( type = = " DATASTORE " ) return DATASTORE ;
else if ( type = = " CLUSTER " ) return CLUSTER ;
else if ( type = = " DOCUMENT " ) return DOCUMENT ;
else if ( type = = " ZONE " ) return ZONE ;
else if ( type = = " SECGROUP " ) return SECGROUP ;
else if ( type = = " VDC " ) return VDC ;
else if ( type = = " VROUTER " ) return VROUTER ;
else if ( type = = " MARKETPLACE " ) return MARKETPLACE ;
else if ( type = = " MARKETPLACEAPP " ) return MARKETPLACEAPP ;
else if ( type = = " VMGROUP " ) return VMGROUP ;
else if ( type = = " VNTEMPLATE " ) return VNTEMPLATE ;
2019-09-09 15:43:51 +03:00
else if ( type = = " HOOK " ) return HOOK ;
2018-03-28 21:55:17 +03:00
else return NONE ;
} ;
2020-07-02 23:42:10 +03:00
static std : : string lock_state_to_str ( LockStates ob )
2018-01-24 17:33:12 +03:00
{
switch ( ob )
{
2019-09-03 17:31:51 +03:00
case ST_NONE : return " NONE " ; break ;
case ST_USE : return " USE " ; break ;
case ST_MANAGE : return " MANAGE " ; break ;
case ST_ADMIN : return " ADMIN " ; break ;
2018-01-24 17:33:12 +03:00
default : return " " ;
}
} ;
2012-01-03 05:58:23 +04:00
/* ---------------------------------------------------------------------- */
2020-07-02 23:42:10 +03:00
PoolObjectSQL ( int id ,
ObjectType _obj_type ,
const std : : string & _name ,
int _uid ,
int _gid ,
const std : : string & _uname ,
const std : : string & _gname ,
const char * _table )
2011-06-30 13:31:00 +04:00
: ObjectSQL ( ) ,
ObjectXML ( ) ,
oid ( id ) ,
2012-01-03 05:58:23 +04:00
obj_type ( _obj_type ) ,
2011-06-30 13:31:00 +04:00
name ( _name ) ,
uid ( _uid ) ,
gid ( _gid ) ,
uname ( _uname ) ,
gname ( _gname ) ,
2011-12-29 21:14:20 +04:00
owner_u ( 1 ) ,
owner_m ( 1 ) ,
owner_a ( 0 ) ,
group_u ( 0 ) ,
group_m ( 0 ) ,
group_a ( 0 ) ,
other_u ( 0 ) ,
other_m ( 0 ) ,
other_a ( 0 ) ,
2013-01-18 02:14:34 +04:00
obj_template ( 0 ) ,
2018-01-24 17:33:12 +03:00
locked ( LockStates : : ST_NONE ) ,
lock_owner ( - 1 ) ,
lock_req_id ( - 1 ) ,
lock_time ( 0 ) ,
2018-10-09 12:05:08 +03:00
mutex ( 0 ) ,
2011-06-30 13:31:00 +04:00
table ( _table )
2008-06-17 20:27:32 +04:00
{
} ;
virtual ~ PoolObjectSQL ( )
{
2017-10-06 17:19:32 +03:00
delete obj_template ;
2008-06-17 20:27:32 +04:00
} ;
2010-04-03 18:54:54 +04:00
2011-03-05 00:37:21 +03:00
/* --------------------------------------------------------------------- */
2008-06-17 20:27:32 +04:00
int get_oid ( ) const
{
return oid ;
} ;
2012-01-05 04:45:53 +04:00
ObjectType get_type ( ) const
{
return obj_type ;
} ;
2013-04-10 14:56:06 +04:00
/**
* Check if the object name contains invalid characters or exceed the max .
* length . By Default these are not allowed " &|: \ ;/'#{}()$
* @ param obj_name for this object
* @ param extra_chars aditional invalid characters to test
* @ param error_str describing the error
* @ return true if the name is valid
*/
2020-07-02 23:42:10 +03:00
static bool name_is_valid ( const std : : string & obj_name ,
const std : : string & extra_chars ,
std : : string & error_str ) ;
2013-04-10 14:56:06 +04:00
/**
* Check if the object name is valid , no extra characters needed to be
* tested .
*/
2020-07-02 23:42:10 +03:00
static bool name_is_valid ( const std : : string & obj_name ,
std : : string & error_str )
2013-04-10 14:56:06 +04:00
{
return name_is_valid ( obj_name , " " , error_str ) ;
}
2020-07-02 23:42:10 +03:00
const std : : string & get_name ( ) const
2011-03-05 00:37:21 +03:00
{
return name ;
} ;
2013-04-10 17:37:07 +04:00
/**
* Set the name of the object and check if it is valid .
* @ param _name the new name
* @ param error_str describing the error if any
*
* @ return 0 if the name was changed
*/
2020-07-02 23:42:10 +03:00
int set_name ( const std : : string & _name , std : : string & error_str )
2012-12-07 21:32:38 +04:00
{
2013-04-10 17:37:07 +04:00
if ( ! name_is_valid ( _name , error_str ) )
{
return - 1 ;
}
2012-12-07 21:32:38 +04:00
name = _name ;
2013-04-10 17:37:07 +04:00
return 0 ;
2012-12-07 21:32:38 +04:00
} ;
2012-01-05 04:45:53 +04:00
int get_uid ( ) const
2011-06-03 20:26:59 +04:00
{
return uid ;
} ;
2012-01-05 04:45:53 +04:00
int get_gid ( ) const
2011-06-09 02:58:57 +04:00
{
return gid ;
} ;
2020-07-02 23:42:10 +03:00
const std : : string & get_uname ( ) const
2012-08-28 18:52:03 +04:00
{
return uname ;
} ;
2020-07-02 23:42:10 +03:00
const std : : string & get_gname ( ) const
2012-08-28 18:52:03 +04:00
{
return gname ;
} ;
2013-01-18 02:14:34 +04:00
2011-06-07 18:55:23 +04:00
/**
2011-06-30 14:09:25 +04:00
* Changes the object ' s owner
2011-06-07 18:55:23 +04:00
* @ param _uid New User ID
2011-06-30 14:09:25 +04:00
* @ param _uname Name of the new user
2011-06-07 18:55:23 +04:00
*/
2020-07-02 23:42:10 +03:00
void set_user ( int _uid , const std : : string & _uname )
2011-05-17 21:13:59 +04:00
{
2011-06-30 14:09:25 +04:00
uid = _uid ;
uname = _uname ;
2011-05-17 21:13:59 +04:00
}
2011-06-07 18:55:23 +04:00
/**
* Changes the object ' s group id
* @ param _gid New Group ID
2011-06-30 14:09:25 +04:00
* @ param _gname Name of the new group
2011-06-07 18:55:23 +04:00
*/
2020-07-02 23:42:10 +03:00
void set_group ( int _gid , const std : : string & _gname )
Feature #407: Add 'GID' attribute to some pool objects; change *pool.info XML-RPC flag meaning; update onedb migrator; fix tests.
* VM, VMTEMPLATE, VNET & IMAGE objects have a GID attribute, and a table column. The group id is inherited from the user creating the object, except for VMs created from Templates, that inherit the Template's group.
* The new flag meaning has been modified in src/rm sources and CLI commands for one.(vm,template,vnet,image)pool.info . It changes from
-2 all, -1 mine & public, >=0 UID
to
-3 mine, -2 all, -1 mine & group
* USER has a group, but not secondary ones. The user_pool table doesn't have a GID column, we'll deal with it later when the group-users relations are implemented.
* onedb migrator 1.rb: deleted USERNAME, and GID added.
2011-05-16 19:00:27 +04:00
{
2011-06-30 14:09:25 +04:00
gid = _gid ;
gname = _gname ;
Feature #407: Add 'GID' attribute to some pool objects; change *pool.info XML-RPC flag meaning; update onedb migrator; fix tests.
* VM, VMTEMPLATE, VNET & IMAGE objects have a GID attribute, and a table column. The group id is inherited from the user creating the object, except for VMs created from Templates, that inherit the Template's group.
* The new flag meaning has been modified in src/rm sources and CLI commands for one.(vm,template,vnet,image)pool.info . It changes from
-2 all, -1 mine & public, >=0 UID
to
-3 mine, -2 all, -1 mine & group
* USER has a group, but not secondary ones. The user_pool table doesn't have a GID column, we'll deal with it later when the group-users relations are implemented.
* onedb migrator 1.rb: deleted USERNAME, and GID added.
2011-05-16 19:00:27 +04:00
} ;
2012-01-04 23:14:09 +04:00
/**
* Changes the object ' s permissions
*
* @ param _owner_u New permission : 1 allow , 0 deny , - 1 do not change
* @ param _owner_m New permission : 1 allow , 0 deny , - 1 do not change
* @ param _owner_a New permission : 1 allow , 0 deny , - 1 do not change
* @ param _group_u New permission : 1 allow , 0 deny , - 1 do not change
* @ param _group_m New permission : 1 allow , 0 deny , - 1 do not change
* @ param _group_a New permission : 1 allow , 0 deny , - 1 do not change
* @ param _other_u New permission : 1 allow , 0 deny , - 1 do not change
* @ param _other_m New permission : 1 allow , 0 deny , - 1 do not change
* @ param _other_a New permission : 1 allow , 0 deny , - 1 do not change
* @ param error_str Returns the error reason , if any
*
* @ return 0 on success
*/
2012-01-06 04:36:57 +04:00
virtual int set_permissions ( int _owner_u ,
int _owner_m ,
int _owner_a ,
int _group_u ,
int _group_m ,
int _group_a ,
int _other_u ,
int _other_m ,
int _other_a ,
2020-07-02 23:42:10 +03:00
std : : string & error_str ) ;
2012-01-04 23:14:09 +04:00
2011-03-05 00:37:21 +03:00
/* --------------------------------------------------------------------- */
2008-06-17 20:27:32 +04:00
/**
2018-10-09 12:05:08 +03:00
* Function to unlock the object . It also frees associated resources . Object
* cannot be access after unlocking it
2008-06-17 20:27:32 +04:00
*/
void unlock ( )
{
2018-10-09 12:05:08 +03:00
if ( ! ro & & mutex ! = 0 )
{
pthread_mutex_unlock ( mutex ) ;
}
2010-04-03 18:54:54 +04:00
2018-10-09 12:05:08 +03:00
delete this ;
2018-03-18 01:31:52 +03:00
} ;
2011-05-13 02:47:21 +04:00
/**
* Function to print the object into a string in XML format
* base64 encoded
* @ param xml the resulting XML string
* @ return a reference to the generated string
*/
2020-07-02 23:42:10 +03:00
virtual std : : string & to_xml64 ( std : : string & xml64 ) ;
2011-05-13 02:47:21 +04:00
2011-02-24 20:12:26 +03:00
/**
* Function to print the object into a string in XML format
* @ param xml the resulting XML string
* @ return a reference to the generated string
*/
2020-07-02 23:42:10 +03:00
virtual std : : string & to_xml ( std : : string & xml ) const = 0 ;
2011-02-24 20:12:26 +03:00
/**
* Rebuilds the object from an xml formatted string
* @ param xml_str The xml - formatted string
*
* @ return 0 on success , - 1 otherwise
*/
2020-07-02 23:42:10 +03:00
virtual int from_xml ( const std : : string & xml_str ) = 0 ;
2011-02-24 20:12:26 +03:00
2011-04-08 03:02:55 +04:00
// ------------------------------------------------------------------------
// Template
// ------------------------------------------------------------------------
/**
2016-02-04 15:10:42 +03:00
* Gets the first VectorAttribute of the specified type with the given name .
* Const and non - const versions of this method is provided
* @ param name the attribute name .
* @ return true first attribute or 0 if not found or wrong type
2011-04-08 03:02:55 +04:00
*/
2020-07-02 23:42:10 +03:00
const VectorAttribute * get_template_attribute ( const std : : string & s ) const
2011-04-08 03:02:55 +04:00
{
2016-02-04 15:10:42 +03:00
return obj_template - > get ( s ) ;
2011-10-11 14:47:30 +04:00
}
2011-04-08 03:02:55 +04:00
2020-07-02 23:42:10 +03:00
VectorAttribute * get_template_attribute ( const std : : string & s )
2011-04-08 03:02:55 +04:00
{
2016-02-04 15:10:42 +03:00
return obj_template - > get ( s ) ;
2011-04-08 03:02:55 +04:00
}
2013-08-22 17:04:25 +04:00
/**
2016-02-04 15:10:42 +03:00
* Gets the values of a template attribute , as a list of VectorAttributes
2013-08-22 17:04:25 +04:00
* @ param name of the attribute
2016-02-04 15:10:42 +03:00
* @ param values of the attribute
* @ return the number of values
2013-08-22 17:04:25 +04:00
*/
2016-02-04 15:10:42 +03:00
template < typename T >
2020-07-02 23:42:10 +03:00
int get_template_attribute ( const std : : string & name ,
std : : vector < const T * > & values ) const
2013-08-22 17:04:25 +04:00
{
2019-09-03 17:31:51 +03:00
return obj_template - > get ( name , values ) ;
2016-02-04 15:10:42 +03:00
} ;
2013-08-22 17:04:25 +04:00
2013-10-22 19:12:06 +04:00
/**
2016-02-04 15:10:42 +03:00
* These methods gets the value of a SingleAttribute and converts it to the
* target type
2013-10-22 19:12:06 +04:00
* @ param name of the attribute
2016-02-04 15:10:42 +03:00
* @ param value of the attribute , will be " " / 0 / false if not defined or
2013-10-22 19:12:06 +04:00
* not a single attribute
2016-02-04 15:10:42 +03:00
*
* @ return true if the attribute was found and is a valid type for the
* target value
2013-10-22 19:12:06 +04:00
*/
2017-08-29 18:28:48 +03:00
template < typename T >
2020-07-02 23:42:10 +03:00
bool get_template_attribute ( const std : : string & name , T & value ) const
2013-10-22 19:12:06 +04:00
{
2019-09-03 17:31:51 +03:00
return obj_template - > get ( name , value ) ;
2013-10-22 19:12:06 +04:00
}
2013-10-22 19:20:23 +04:00
/**
2016-02-04 15:10:42 +03:00
* These methods get and remove a string based attribute ( single )
2013-10-22 19:20:23 +04:00
* @ param name of the attribute
2016-02-04 15:10:42 +03:00
* @ param value of the attribute ( a string ) , will be " " / 0 / false if not
* defined or not a single attribute , depending on the target value type
2013-10-22 19:20:23 +04:00
* @ return the number of attributes erased
*/
2016-02-04 15:10:42 +03:00
template < typename T >
2020-07-02 23:42:10 +03:00
int erase_template_attribute ( const std : : string & name , T & value )
2013-10-22 19:20:23 +04:00
{
2019-09-03 17:31:51 +03:00
obj_template - > get ( name , value ) ;
2013-10-22 19:20:23 +04:00
return obj_template - > erase ( name ) ;
}
2011-04-08 03:02:55 +04:00
/**
* Adds a new attribute to the template ( replacing it if
* already defined ) , the object ' s mutex SHOULD be locked
* @ param name of the new attribute
* @ param value of the new attribute
* @ return 0 on success
*/
2016-02-04 15:10:42 +03:00
template < typename T >
2020-07-02 23:42:10 +03:00
int replace_template_attribute ( const std : : string & name , const T & value )
2011-04-08 03:02:55 +04:00
{
2012-05-25 14:56:51 +04:00
return obj_template - > replace ( name , value ) ;
2011-04-08 03:02:55 +04:00
}
2014-05-10 03:22:02 +04:00
/**
* Removes an attribute from the template . The attributes are returned , and
* MUST be freed by the calling funtion
* @ param name of the attribute
* @ param values a vector containing a pointer to the attributes
* @ return the number of attributes removed
*/
2016-02-04 15:10:42 +03:00
template < typename T >
2020-07-02 23:42:10 +03:00
int remove_template_attribute ( const std : : string & n , std : : vector < T * > & v )
2014-05-10 03:22:02 +04:00
{
2016-02-04 15:10:42 +03:00
return obj_template - > remove ( n , v ) ;
2014-05-10 03:22:02 +04:00
}
2011-04-08 03:02:55 +04:00
/**
* Generates a XML string for the template of the Object
* @ param xml the string to store the XML description .
*/
2020-07-02 23:42:10 +03:00
std : : string & template_to_xml ( std : : string & xml ) const
2011-04-08 03:02:55 +04:00
{
2011-11-10 14:15:58 +04:00
return obj_template - > to_xml ( xml ) ;
2011-04-08 03:02:55 +04:00
}
/**
2014-11-20 17:14:45 +03:00
* Removes an attribute
2011-04-08 03:02:55 +04:00
* @ param name of the attribute
*/
2020-07-02 23:42:10 +03:00
int remove_template_attribute ( const std : : string & name )
2011-04-08 03:02:55 +04:00
{
return obj_template - > erase ( name ) ;
}
2011-04-15 03:51:38 +04:00
/**
2013-02-13 21:06:20 +04:00
* Sets an error message with timestamp in the template
* @ param message Message string
2011-04-15 03:51:38 +04:00
*/
2020-07-02 23:42:10 +03:00
virtual void set_template_error_message ( const std : : string & message ) ;
2011-04-15 03:51:38 +04:00
2013-02-14 19:55:47 +04:00
/**
* Deletes the error message from the template
*/
2013-04-26 19:26:09 +04:00
virtual void clear_template_error_message ( ) ;
2013-02-14 19:55:47 +04:00
2013-02-13 21:06:20 +04:00
/**
2013-02-16 05:55:17 +04:00
* Adds a string attribute
2013-02-13 21:06:20 +04:00
* @ param att_name Name for the attribute
2013-02-16 05:55:17 +04:00
* @ param att_val Message string
*/
2016-02-04 15:10:42 +03:00
template < typename T >
2020-07-02 23:42:10 +03:00
void add_template_attribute ( const std : : string & name , const T & value )
2013-02-23 22:49:06 +04:00
{
obj_template - > add ( name , value ) ;
}
2016-03-02 01:31:31 +03:00
template < typename T >
2020-07-02 23:42:10 +03:00
void add_template_attribute ( std : : vector < T * > & values )
2016-03-02 01:31:31 +03:00
{
obj_template - > set ( values ) ;
}
2011-06-02 01:53:09 +04:00
/**
* Factory method for templates , it should be implemented
* by classes that uses templates
* @ return a new template
*/
2012-03-14 18:48:06 +04:00
virtual Template * get_new_template ( ) const
2011-06-02 01:53:09 +04:00
{
return 0 ;
}
/**
2011-06-02 02:51:42 +04:00
* Replace template for this object . Object should be updated
* after calling this method
2014-09-03 13:36:46 +04:00
* @ param tmpl_str new contents
* @ param keep_restricted If true , the restricted attributes of the
* current template will override the new template
* @ param error string describing the error if any
* @ return 0 on success
2011-06-02 01:53:09 +04:00
*/
2020-07-02 23:42:10 +03:00
virtual int replace_template ( const std : : string & tmpl_str ,
bool keep_restricted ,
std : : string & error ) ;
2011-06-02 01:53:09 +04:00
2013-06-28 20:00:26 +04:00
/**
* Append new attributes to this object ' s template . Object should be updated
* after calling this method
2014-09-03 13:36:46 +04:00
* @ param tmpl_str new contents
* @ param keep_restricted If true , the restricted attributes of the
* current template will override the new template
* @ param error string describing the error if any
* @ return 0 on success
2013-06-28 20:00:26 +04:00
*/
2020-07-02 23:42:10 +03:00
virtual int append_template ( const std : : string & tmpl_str ,
bool keep_restricted ,
std : : string & error ) ;
2011-12-30 01:05:11 +04:00
2012-01-03 05:58:23 +04:00
/**
* Fills a auth class to perform an authZ / authN request based on the object
* attributes
* @ param auths to be filled
*/
2014-09-17 19:05:01 +04:00
virtual void get_permissions ( PoolObjectAuth & auths ) ;
2011-12-30 01:05:11 +04:00
2015-05-12 17:22:00 +03:00
/**
* Tries to get the DB lock . This is a mutex requested by external
* applications , not related to the internal mutex lock . The object
* must be locked ( internal memory mutex ) before this method is called
*
* @ param owner String to identify who requested the lock
*
* @ return 0 if the lock was granted , - 1 if the object is already locked
*/
2018-05-24 12:43:27 +03:00
int lock_db ( const int owner , const int req_id , const int level ) ;
2015-05-12 17:22:00 +03:00
/**
* Unlocks the DB lock for external applications . The object must be locked
* ( internal memory mutex ) before this method is called
*
2018-05-24 12:43:27 +03:00
* @ param owner String to identify who requested the lock . - 1 to bypass check
* @ return 0 if object was unlocked - 1 otherwise ( owner ! = lock_owner )
2015-05-12 17:22:00 +03:00
*/
2018-05-24 12:43:27 +03:00
int unlock_db ( const int owner , const int req_id ) ;
2018-01-24 17:33:12 +03:00
/**
* Unlocks the DB lock for external applications . The object must be locked
* ( internal memory mutex ) before this method is called
*
* @ param owner String to identify who requested the lock
*/
2018-03-18 01:31:52 +03:00
LockStates get_lock_state ( )
{
2018-01-24 17:33:12 +03:00
return locked ;
}
2015-05-12 17:22:00 +03:00
2019-09-12 17:25:23 +03:00
/**
* Encrypt all secret attributes
*/
virtual void encrypt ( ) ;
2019-09-03 17:31:51 +03:00
/**
* Decrypt all secret attributes
*/
2019-09-12 17:25:23 +03:00
virtual void decrypt ( ) ;
2019-09-03 17:31:51 +03:00
2008-06-17 20:27:32 +04:00
protected :
2010-04-03 18:54:54 +04:00
2008-06-17 20:27:32 +04:00
/**
2011-02-24 20:12:26 +03:00
* Callback function to unmarshall a PoolObjectSQL
* @ param num the number of columns read from the DB
* @ param names the column names
* @ param vaues the column values
* @ return 0 on success
2008-06-17 20:27:32 +04:00
*/
2011-02-24 20:12:26 +03:00
int select_cb ( void * nil , int num , char * * values , char * * names )
{
if ( ( ! values [ 0 ] ) | | ( num ! = 1 ) )
{
return - 1 ;
}
2011-02-25 01:30:39 +03:00
return from_xml ( values [ 0 ] ) ;
2011-02-24 20:12:26 +03:00
} ;
2009-07-13 16:21:14 +04:00
/**
2011-02-24 20:12:26 +03:00
* Reads the PoolObjectSQL ( identified by its OID ) from the database .
* @ param db pointer to the db
* @ return 0 on success
2009-07-13 16:21:14 +04:00
*/
2019-09-03 17:31:51 +03:00
int select ( SqlDB * db ) override ;
2011-02-24 20:12:26 +03:00
2011-03-05 05:24:11 +03:00
/**
* Reads the PoolObjectSQL ( identified by its OID ) from the database .
* @ param db pointer to the db
* @ return 0 on success
*/
2020-07-02 23:42:10 +03:00
virtual int select ( SqlDB * db , const std : : string & _name , int _uid ) ;
2011-03-05 05:24:11 +03:00
2018-03-18 01:31:52 +03:00
/**
* Search oid by its name and owner
* @ param db pointer to the db
* @ param _table for the objects
* @ param _name of the object
* @ param _uid of owner
* @ return - 1 if not found or oid otherwise
*/
2020-07-02 23:42:10 +03:00
static int select_oid ( SqlDB * db , const char * _table , const std : : string & _name ,
2018-03-18 01:31:52 +03:00
int _uid ) ;
/**
* Check if the object exists
* @ param db pointer to the db
* @ param _table for the objects
* @ param _oid of the object
*
* @ return - 1 if not found or oid otherwise
*/
static int exist ( SqlDB * db , const char * _table , int _oid ) ;
2011-02-24 20:12:26 +03:00
/**
* Drops object from the database
* @ param db pointer to the db
* @ return 0 on success
*/
2019-09-03 17:31:51 +03:00
int drop ( SqlDB * db ) override ;
2011-02-24 20:12:26 +03:00
/**
* Function to output a pool object into a stream in XML format
* @ param oss the output stream
* @ param num the number of columns read from the DB
* @ param names the column names
* @ param vaues the column values
* @ return 0 on success
*/
2020-07-02 23:42:10 +03:00
static int dump ( std : : ostringstream & oss , int num , char * * values , char * * names )
2011-02-24 20:12:26 +03:00
{
if ( ( ! values [ 0 ] ) | | ( num ! = 1 ) )
{
return - 1 ;
}
oss < < values [ 0 ] ;
return 0 ;
} ;
2011-12-29 21:14:20 +04:00
/**
* Prints the permissions into a string in XML format
* @ param xml the resulting XML string
* @ return a reference to the generated string
*/
2020-07-02 23:42:10 +03:00
std : : string & perms_to_xml ( std : : string & xml ) const ;
2011-12-29 21:14:20 +04:00
/**
* Rebuilds the object permissions from the xml . ObjectXML : : update_from_str
* must be called before this method
*
* @ return 0 on success , - 1 otherwise
*/
int perms_from_xml ( ) ;
2012-01-04 23:14:09 +04:00
/**
* Sets the permission attribute to the new_perm value , if it is different
* from - 1
*
* @ param perm the permissions attribute , must be - 1 , 0 or 1 , its value
* must be checked before
* @ param new_perm the new value . If it is - 1 , it will be ignored
*/
void set_perm ( int & perm , const int & new_perm )
{
if ( new_perm ! = - 1 )
{
perm = new_perm ;
}
} ;
2013-01-18 21:34:51 +04:00
/**
* Initializes the object ' s permissions , according to the provided umask .
*
* @ param umask Permission mask , similar to unix umask .
* For example a umask of 137 will set the permissions " um- u-- --- "
*/
void set_umask ( int umask ) ;
2013-03-15 20:37:47 +04:00
/**
* Sets an error message with timestamp in the template
* @ param name of the error attribute
* @ param message Message string
*/
2020-07-02 23:42:10 +03:00
virtual void set_template_error_message ( const std : : string & name ,
const std : : string & message ) ;
2013-03-15 20:37:47 +04:00
2014-10-20 18:05:44 +04:00
/**
* Child classes can process the new template set with replace_template or
* append_template with this method
* @ param error string describing the error if any
* @ return 0 on success
*/
2020-07-02 23:42:10 +03:00
virtual int post_update_template ( std : : string & error )
2014-10-20 18:05:44 +04:00
{
return 0 ;
} ;
2015-05-12 13:48:59 +03:00
/**
* Prints the lock info into a string in XML format
* @ param xml the resulting XML string
* @ return a reference to the generated string
*/
2020-07-02 23:42:10 +03:00
std : : string & lock_db_to_xml ( std : : string & xml ) const ;
2015-05-12 13:48:59 +03:00
/**
* Rebuilds the lock info from the xml . ObjectXML : : update_from_str
* must be called before this method
*
* @ return 0 on success , - 1 otherwise
*/
int lock_db_from_xml ( ) ;
2019-09-03 17:31:51 +03:00
2011-02-24 20:12:26 +03:00
/**
* The object ' s unique ID
2008-06-17 20:27:32 +04:00
*/
2011-03-05 00:37:21 +03:00
int oid ;
2012-01-03 05:58:23 +04:00
/**
* The object type
*/
ObjectType obj_type ;
2011-03-05 00:37:21 +03:00
/**
* The object ' s name
*/
2020-07-02 23:42:10 +03:00
std : : string name ;
2011-03-05 00:37:21 +03:00
/**
2011-03-05 05:24:11 +03:00
* Object ' s owner , set it to - 1 if owner is not used
2011-03-05 00:37:21 +03:00
*/
int uid ;
2009-07-13 16:21:14 +04:00
Feature #407: Add 'GID' attribute to some pool objects; change *pool.info XML-RPC flag meaning; update onedb migrator; fix tests.
* VM, VMTEMPLATE, VNET & IMAGE objects have a GID attribute, and a table column. The group id is inherited from the user creating the object, except for VMs created from Templates, that inherit the Template's group.
* The new flag meaning has been modified in src/rm sources and CLI commands for one.(vm,template,vnet,image)pool.info . It changes from
-2 all, -1 mine & public, >=0 UID
to
-3 mine, -2 all, -1 mine & group
* USER has a group, but not secondary ones. The user_pool table doesn't have a GID column, we'll deal with it later when the group-users relations are implemented.
* onedb migrator 1.rb: deleted USERNAME, and GID added.
2011-05-16 19:00:27 +04:00
/**
* Object ' s group , set it to - 1 if group is not used
*/
int gid ;
2011-06-30 13:31:00 +04:00
/**
* Name of the object ' s owner , empty if owner is not used
*/
2020-07-02 23:42:10 +03:00
std : : string uname ;
2011-06-30 13:31:00 +04:00
/**
* Name of the object ' s group , , empty if group is not used
*/
2020-07-02 23:42:10 +03:00
std : : string gname ;
2011-06-30 13:31:00 +04:00
2011-12-29 21:14:20 +04:00
/**
* Permissions for the owner user
*/
int owner_u ;
int owner_m ;
int owner_a ;
/**
* Permissions for users in the object ' s group
*/
int group_u ;
int group_m ;
int group_a ;
/**
* Permissions for the rest
*/
int other_u ;
int other_m ;
int other_a ;
2011-04-08 03:02:55 +04:00
/**
* Template for this object , will be allocated if needed
*/
Template * obj_template ;
2015-05-12 13:48:59 +03:00
/**
* Flag for the DB lock
*/
2018-01-24 17:33:12 +03:00
LockStates locked ;
/**
* Owner of the DB lock
*/
int lock_owner ;
2015-05-12 13:48:59 +03:00
/**
* Owner of the DB lock
*/
2018-01-24 17:33:12 +03:00
int lock_req_id ;
2015-05-12 13:48:59 +03:00
/**
* Expiration time for the DB lock
*/
2018-01-24 17:33:12 +03:00
time_t lock_time ;
2015-05-12 13:48:59 +03:00
2018-10-09 12:05:08 +03:00
/**
* Attribute for check if is a read only object
*/
bool ro ;
2008-06-17 20:27:32 +04:00
private :
2013-04-10 14:56:06 +04:00
/**
* Characters that can not be in a name
*/
2020-07-02 23:42:10 +03:00
static const std : : string INVALID_NAME_CHARS ;
2008-06-17 20:27:32 +04:00
2015-05-12 13:48:59 +03:00
/**
* Expiration time for the lock stored in the DB
*/
static const int LOCK_DB_EXPIRATION ;
2008-06-17 20:27:32 +04:00
/**
* The PoolSQL , friend to easily manipulate its Objects
*/
friend class PoolSQL ;
/**
2010-04-03 18:54:54 +04:00
* The mutex for the PoolObject . This implementation assumes that the mutex
* IS LOCKED when the class destructor is called .
2008-06-17 20:27:32 +04:00
*/
2018-10-09 12:05:08 +03:00
pthread_mutex_t * mutex ;
2011-02-25 01:30:39 +03:00
/**
* Pointer to the SQL table for the PoolObjectSQL
*/
const char * table ;
2008-06-17 20:27:32 +04:00
} ;
# endif /*POOL_OBJECT_SQL_H_*/