2011-05-24 16:32:39 +04:00
/* -------------------------------------------------------------------------- */
2014-01-09 14:51:20 +04:00
/* Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs */
2011-05-24 16:32:39 +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 REQUEST_H_
# define REQUEST_H_
# include <xmlrpc-c/base.hpp>
# include <xmlrpc-c/registry.hpp>
# include "RequestManager.h"
2012-05-31 18:51:07 +04:00
# include "AuthRequest.h"
2012-01-03 05:58:23 +04:00
# include "PoolObjectSQL.h"
2012-06-16 01:33:50 +04:00
# include "Quotas.h"
2011-05-24 16:32:39 +04:00
using namespace std ;
2011-05-24 17:23:07 +04:00
/**
* The Request Class represents the basic abstraction for the OpenNebula
* XML - RPC API . This interface must be implemented by any XML - RPC API call
*/
2011-05-24 16:32:39 +04:00
class Request : public xmlrpc_c : : method
{
public :
/**
* Wraps the actual execution function by authorizing the user
* and calling the request_execute virtual function
* @ param _paramlist list of XML parameters
* @ param _retval value to be returned to the client
*/
virtual void execute (
xmlrpc_c : : paramList const & _paramList ,
xmlrpc_c : : value * const _retval ) ;
2011-05-24 17:23:07 +04:00
/**
* Error codes for the XML - RPC API
*/
enum ErrorCode {
2011-05-24 19:31:27 +04:00
SUCCESS = 0x0000 ,
2011-05-24 17:23:07 +04:00
AUTHENTICATION = 0x0100 ,
AUTHORIZATION = 0x0200 ,
2011-05-24 19:31:27 +04:00
NO_EXISTS = 0x0400 ,
ACTION = 0x0800 ,
XML_RPC_API = 0x1000 ,
INTERNAL = 0x2000 ,
2011-05-24 17:23:07 +04:00
} ;
2011-05-24 16:32:39 +04:00
protected :
2011-07-07 21:01:04 +04:00
/* ---------------------------------------------------------------------*/
/* Attributes of the Request */
/* ---------------------------------------------------------------------*/
/* -------- Dynamic (specific for a request of the same method) -------- */
2011-05-26 02:59:50 +04:00
2011-07-07 14:45:13 +04:00
struct RequestAttributes
{
2011-07-07 21:01:04 +04:00
int uid ; /**< id of the user */
int gid ; /**< id of the user's group */
2011-06-30 13:31:00 +04:00
2011-07-07 21:01:04 +04:00
string uname ; /**< name of the user */
string gname ; /**< name of the user's group */
2011-06-29 14:50:16 +04:00
2013-08-23 14:39:14 +04:00
set < int > group_ids ; /**< set of user's group ids */
2011-07-07 21:01:04 +04:00
string session ; /**< Session from ONE XML-RPC API */
2012-10-02 18:25:57 +04:00
int req_id ; /**< Request ID for log messages */
2011-07-07 14:45:13 +04:00
2011-07-07 21:01:04 +04:00
xmlrpc_c : : value * retval ; /**< Return value from libxmlrpc-c */
2012-06-08 03:50:15 +04:00
RequestAttributes ( ) { } ;
RequestAttributes ( const RequestAttributes & ra )
{
uid = ra . uid ;
gid = ra . gid ;
uname = ra . uname ;
gname = ra . gname ;
session = ra . session ;
retval = ra . retval ;
} ;
RequestAttributes ( int _uid , int _gid , const RequestAttributes & ra )
{
uid = _uid ;
gid = _gid ;
uname = " " ;
gname = " " ;
session = ra . session ;
retval = ra . retval ;
} ;
2011-07-07 14:45:13 +04:00
} ;
2011-05-26 02:59:50 +04:00
2011-07-07 21:01:04 +04:00
/* -------- Static (shared among request of the same method) -------- */
2012-01-03 05:58:23 +04:00
PoolSQL * pool ; /**< Pool of objects */
string method_name ; /**< The name of the XML-RPC method */
2011-05-28 06:03:09 +04:00
2012-01-03 05:58:23 +04:00
PoolObjectSQL : : ObjectType auth_object ; /**< Auth object for the request */
AuthRequest : : Operation auth_op ; /**< Auth operation for the request */
2011-05-26 02:59:50 +04:00
/* -------------------- Constructors ---------------------------------- */
2014-03-08 03:02:38 +04:00
Request ( const string & mn ,
const string & signature ,
2011-07-07 14:45:13 +04:00
const string & help ) : pool ( 0 ) , method_name ( mn )
2011-05-24 16:32:39 +04:00
{
_signature = signature ;
_help = help ;
} ;
virtual ~ Request ( ) { } ;
2011-05-26 02:59:50 +04:00
/* -------------------------------------------------------------------- */
/* -------------------------------------------------------------------- */
/**
2011-06-22 21:22:52 +04:00
* Performs a basic authorization for this request using the uid / gid
2014-03-08 03:02:38 +04:00
* from the request . The function gets the object from the pool to get
* the public attribute and its owner . The authorization is based on
2011-05-26 02:59:50 +04:00
* object and type of operation for the request .
2011-07-01 19:33:37 +04:00
* @ param oid of the object , can be - 1 for objects to be created , or
* pools .
2011-07-07 14:45:13 +04:00
* @ param att the specific request attributes
2011-06-02 21:17:22 +04:00
*
* @ return true if the user is authorized .
2011-05-26 02:59:50 +04:00
*/
2011-07-07 14:45:13 +04:00
bool basic_authorization ( int oid , RequestAttributes & att )
2011-07-01 19:33:37 +04:00
{
2011-07-07 14:45:13 +04:00
return basic_authorization ( oid , auth_op , att ) ;
2011-07-01 19:33:37 +04:00
} ;
/**
* Performs a basic authorization for this request using the uid / gid
* from the request . The function gets the object from the pool to get
* the public attribute and its owner . The authorization is based on
* object and type of operation for the request .
* @ param oid of the object , can be - 1 for objects to be created , or
* pools .
* @ param op operation of the request .
2011-07-07 14:45:13 +04:00
* @ param att the specific request attributes
2011-07-01 19:33:37 +04:00
*
* @ return true if the user is authorized .
*/
2011-07-07 14:45:13 +04:00
bool basic_authorization ( int oid , AuthRequest : : Operation op ,
RequestAttributes & att ) ;
2012-05-30 04:20:16 +04:00
/**
* Performs a basic quota check for this request using the uid / gid
2012-06-28 15:21:19 +04:00
* from the request . Usage counters are updated for the user / group .
* On case of error , the failure_response return values are set
*
2012-05-30 04:20:16 +04:00
* @ param tmpl describing the object
2012-06-06 19:52:44 +04:00
* @ param object type of the object
2012-05-30 04:20:16 +04:00
* @ param att the specific request attributes
*
* @ return true if the user is authorized .
*/
2012-06-28 15:21:19 +04:00
bool quota_authorization (
Template * tmpl ,
Quotas : : QuotaType qtype ,
RequestAttributes & att ) ;
/**
* Performs a basic quota check for this request using the uid / gid
* from the request . Usage counters are updated for the user / group .
* On case of error , the failure_response return values is not set , instead
* the error reason is returned in error_str
*
* @ param tmpl describing the object
* @ param object type of the object
* @ param att the specific request attributes
*
* @ param error_str Error reason , if any
* @ return true if the user is authorized .
*/
bool quota_authorization (
Template * tmpl ,
Quotas : : QuotaType qtype ,
RequestAttributes & att ,
string & error_str ) ;
2012-06-06 19:52:44 +04:00
/**
* Performs rollback on usage counters for a previous quota check operation
* for the request .
* @ param tmpl describing the object
* @ param att the specific request attributes
*/
2012-06-16 01:33:50 +04:00
void quota_rollback ( Template * tmpl ,
Quotas : : QuotaType qtype ,
2012-06-06 19:52:44 +04:00
RequestAttributes & att ) ;
2012-05-30 04:20:16 +04:00
2011-05-24 16:32:39 +04:00
/**
* Actual Execution method for the request . Must be implemented by the
* XML - RPC requests
* @ param _paramlist of the XML - RPC call ( complete list )
2011-07-07 14:45:13 +04:00
* @ param att the specific request attributes
2011-05-24 16:32:39 +04:00
*/
2011-07-07 14:45:13 +04:00
virtual void request_execute ( xmlrpc_c : : paramList const & _paramList ,
RequestAttributes & att ) = 0 ;
2011-05-24 16:32:39 +04:00
/**
* Builds an XML - RPC response updating retval . After calling this function
* the xml - rpc excute method should return
* @ param val to be returned to the client
2011-07-07 14:45:13 +04:00
* @ param att the specific request attributes
2011-05-24 16:32:39 +04:00
*/
2011-07-07 14:45:13 +04:00
void success_response ( int val , RequestAttributes & att ) ;
2011-05-24 16:32:39 +04:00
/**
* Builds an XML - RPC response updating retval . After calling this function
* the xml - rpc excute method should return
* @ param val string to be returned to the client
2011-07-07 14:45:13 +04:00
* @ param att the specific request attributes
2011-05-24 16:32:39 +04:00
*/
2011-07-07 14:45:13 +04:00
void success_response ( const string & val , RequestAttributes & att ) ;
2011-05-24 16:32:39 +04:00
/**
* Builds an XML - RPC response updating retval . After calling this function
* the xml - rpc excute method should return
* @ param ec error code for this call
* @ param val string representation of the error
2011-07-07 14:45:13 +04:00
* @ param att the specific request attributes
2011-05-24 16:32:39 +04:00
*/
2011-07-07 14:45:13 +04:00
void failure_response ( ErrorCode ec ,
const string & val ,
RequestAttributes & att ) ;
2011-05-24 16:32:39 +04:00
2011-05-28 13:36:24 +04:00
/**
* Gets a string representation for the Auth object in the
* request .
* @ param ob object for the auth operation
2011-06-02 21:17:22 +04:00
* @ return string equivalent of the object
2011-05-28 13:36:24 +04:00
*/
2012-01-03 05:58:23 +04:00
static string object_name ( PoolObjectSQL : : ObjectType ob ) ;
2011-05-28 13:36:24 +04:00
2011-05-24 16:32:39 +04:00
/**
* Logs authorization errors
2011-06-04 04:51:50 +04:00
* @ param message with the authorization error details
2011-06-02 21:17:22 +04:00
* @ return string for logging
2011-07-07 14:45:13 +04:00
* @ param att the specific request attributes
2011-05-24 16:32:39 +04:00
*/
2011-07-07 14:45:13 +04:00
string authorization_error ( const string & message , RequestAttributes & att ) ;
2011-05-24 16:32:39 +04:00
/**
* Logs authenticate errors
2011-06-02 21:17:22 +04:00
* @ return string for logging
2011-05-24 16:32:39 +04:00
*/
string authenticate_error ( ) ;
/**
* Logs get object errors
* @ param object over which the get failed
* @ param id of the object over which the get failed
2011-06-02 21:17:22 +04:00
* @ return string for logging
2011-05-24 16:32:39 +04:00
*/
2011-06-04 06:02:19 +04:00
string get_error ( const string & object , int id ) ;
2011-05-24 16:32:39 +04:00
/**
* Logs action errors
2011-06-04 06:02:19 +04:00
* @ param err_desc brief description of the error
* @ param err_detail additional error details from Managers & Pools
2011-06-02 21:17:22 +04:00
* @ return string for logging
2011-05-24 16:32:39 +04:00
*/
2011-06-04 06:02:19 +04:00
string request_error ( const string & err_desc , const string & err_detail ) ;
2011-06-04 04:51:50 +04:00
/**
* Logs allocate errors
* @ param message with the allocate error details
* @ return string for logging
*/
string allocate_error ( const string & error ) ;
2011-06-04 06:02:19 +04:00
/**
* Logs allocate errors for a given resource
* @ param obj the resource
* @ param message with the allocate error details
* @ return string for logging
*/
2012-01-03 05:58:23 +04:00
string allocate_error ( PoolObjectSQL : : ObjectType obj , const string & error ) ;
2012-02-28 20:59:03 +04:00
/**
* Locks the requested object , gets information , and unlocks it
*
* @ param pool object pool
* @ param id of the object
* @ param type of the object
* @ param att the specific request attributes
*
* @ param perms returns the object ' s permissions
* @ param name returns the object ' s name
2014-03-08 03:02:38 +04:00
* @ param throw_error send error response to client if object not found
2012-02-28 20:59:03 +04:00
*
* @ return 0 on success , - 1 otherwise
*/
int get_info ( PoolSQL * pool ,
int id ,
PoolObjectSQL : : ObjectType type ,
RequestAttributes & att ,
PoolObjectAuth & perms ,
2014-03-08 03:02:38 +04:00
string & name ,
bool throw_error ) ;
2012-10-02 18:25:57 +04:00
/**
* Logs the method invocation , including the arguments
*
* @ param att the specific request attributes
* @ param paramList list of XML parameters
*/
virtual void log_method_invoked (
const RequestAttributes & att ,
const xmlrpc_c : : paramList & paramList ) ;
/**
* Logs the method result , including the output data or error message
*
* @ param att the specific request attributes
*/
virtual void log_result (
const RequestAttributes & att ) ;
/**
* Formats and adds a xmlrpc_c : : value input parameter to oss .
*
* @ param v value to format
* @ param oss stream to write v
* @ param index parameter index
*/
virtual void log_xmlrpc_param (
const xmlrpc_c : : value & v ,
ostringstream & oss ,
const int & index ) ;
/**
* Formats and adds a xmlrpc_c : : value to oss .
*
* @ param v value to format
* @ param oss stream to write v
*/
virtual void log_xmlrpc_value (
const xmlrpc_c : : value & v ,
ostringstream & oss ) ;
2012-06-09 00:14:40 +04:00
private :
/* ------------- Functions to manage user and group quotas -------------- */
bool user_quota_authorization ( Template * tmpl ,
2012-06-16 01:33:50 +04:00
Quotas : : QuotaType qtype ,
2012-06-09 00:14:40 +04:00
RequestAttributes & att ,
string & error_str ) ;
bool group_quota_authorization ( Template * tmpl ,
2012-06-16 01:33:50 +04:00
Quotas : : QuotaType qtype ,
2012-06-09 00:14:40 +04:00
RequestAttributes & att ,
string & error_str ) ;
void user_quota_rollback ( Template * tmpl ,
2012-06-16 01:33:50 +04:00
Quotas : : QuotaType qtype ,
2012-06-09 00:14:40 +04:00
RequestAttributes & att ) ;
void group_quota_rollback ( Template * tmpl ,
2012-06-16 01:33:50 +04:00
Quotas : : QuotaType qtype ,
2012-06-09 00:14:40 +04:00
RequestAttributes & att ) ;
2011-05-24 16:32:39 +04:00
} ;
2011-05-26 02:59:50 +04:00
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
2011-05-24 16:32:39 +04:00
# endif //REQUEST_H_