diff --git a/include/AddressRange.h b/include/AddressRange.h index 2a61774c08..f57b9b073f 100644 --- a/include/AddressRange.h +++ b/include/AddressRange.h @@ -116,8 +116,14 @@ public: /** * Builds an extended XML representation of the AR to send it back to * clients + * @param oss stream to write the XML + * @param vm_ids list of VM the user can access VNET usage info from. + * A vector containing just -1 means all VMs. + * @param vnet_ids list of VNET the user can access reservation info from. + * A vector containing just -1 means all VNETs. */ - void to_xml(ostringstream &oss) const; + void to_xml(ostringstream &oss, const vector& vms, + const vector& vnets) const; // ************************************************************************* // Address allocation functions diff --git a/include/AddressRangePool.h b/include/AddressRangePool.h index 36a87b6045..d971c07466 100644 --- a/include/AddressRangePool.h +++ b/include/AddressRangePool.h @@ -318,9 +318,14 @@ public: * Generate a XML representation of the Address Range Pool * @param sstream where the ARPool is written * @param extended true to include lease information + * @param vm_ids list of VM the user can access VNET usage info from. + * A vector containing just -1 means all VMs. + * @param vnet_ids list of VNET the user can access reservation info from. + * A vector containing just -1 means all VNETs. * @return the string with the XML */ - string& to_xml(string& sstream, bool extended) const; + string& to_xml(string& sstream, bool extended, const vector& vms, + const vector& vnets) const; private: /** diff --git a/include/RequestManagerInfo.h b/include/RequestManagerInfo.h index e3303f17b8..1ce4bbe61c 100644 --- a/include/RequestManagerInfo.h +++ b/include/RequestManagerInfo.h @@ -45,7 +45,8 @@ protected: /* -------------------------------------------------------------------- */ - virtual void to_xml(PoolObjectSQL * object, string& str) + virtual void to_xml(RequestAttributes& att, PoolObjectSQL * object, + string& str) { object->to_xml(str); }; @@ -60,7 +61,7 @@ public: VirtualMachineInfo(): RequestManagerInfo("VirtualMachineInfo", "Returns virtual machine instance information") - { + { Nebula& nd = Nebula::instance(); pool = nd.get_vmpool(); auth_object = PoolObjectSQL::VM; @@ -70,10 +71,9 @@ public: /* -------------------------------------------------------------------- */ - void to_xml(PoolObjectSQL * object, string& str) + void to_xml(RequestAttributes& att, PoolObjectSQL * object, string& str) { - VirtualMachine * vm = static_cast(object); - vm->to_xml_extended(str); + static_cast(object)->to_xml_extended(str); }; }; @@ -86,7 +86,7 @@ public: TemplateInfo(): RequestManagerInfo("TemplateInfo", "Returns virtual machine template information") - { + { Nebula& nd = Nebula::instance(); pool = nd.get_tpool(); auth_object = PoolObjectSQL::TEMPLATE; @@ -105,7 +105,7 @@ public: VirtualNetworkInfo(): RequestManagerInfo("VirtualNetworkInfo", "Returns virtual network information") - { + { Nebula& nd = Nebula::instance(); pool = nd.get_vnpool(); auth_object = PoolObjectSQL::NET; @@ -115,11 +115,7 @@ public: /* -------------------------------------------------------------------- */ - void to_xml(PoolObjectSQL * object, string& str) - { - VirtualNetwork * vn = static_cast(object); - vn->to_xml_extended(str); - }; + void to_xml(RequestAttributes& att, PoolObjectSQL * object, string& str); }; /* ------------------------------------------------------------------------- */ @@ -131,7 +127,7 @@ public: ImageInfo(): RequestManagerInfo("ImageInfo", "Returns image information") - { + { Nebula& nd = Nebula::instance(); pool = nd.get_ipool(); auth_object = PoolObjectSQL::IMAGE; @@ -150,7 +146,7 @@ public: HostInfo(): RequestManagerInfo("HostInfo", "Returns host information") - { + { Nebula& nd = Nebula::instance(); pool = nd.get_hpool(); auth_object = PoolObjectSQL::HOST; @@ -178,10 +174,9 @@ public: /* -------------------------------------------------------------------- */ - void to_xml(PoolObjectSQL * object, string& str) + void to_xml(RequestAttributes& att, PoolObjectSQL * object, string& str) { - Group * group = static_cast(object); - group->to_xml_extended(str); + static_cast(object)->to_xml_extended(str); }; }; @@ -204,10 +199,9 @@ public: /* -------------------------------------------------------------------- */ - void to_xml(PoolObjectSQL * object, string& str) + void to_xml(RequestAttributes& att, PoolObjectSQL * object, string& str) { - User * user = static_cast(object); - user->to_xml_extended(str); + static_cast(object)->to_xml_extended(str); }; }; diff --git a/include/RequestManagerPoolInfoFilter.h b/include/RequestManagerPoolInfoFilter.h index cdc25eac3c..ea94851aa5 100644 --- a/include/RequestManagerPoolInfoFilter.h +++ b/include/RequestManagerPoolInfoFilter.h @@ -39,6 +39,17 @@ public: /** Specify user's + group objects (-1) */ static const int MINE_GROUP; + /** + * Set a where filter to get the oids of objects that a user can "USE" + * @param att the XML-RPC Attributes with user information + * @param auth_object the object type + * @param where_string will store the resulting SQL filter + * @return true if the use_filter is empty and access to all objects + * should be granted. + */ + static bool use_filter(RequestAttributes& att, PoolObjectSQL::ObjectType aobj, + string& where_str); + protected: RequestManagerPoolInfoFilter(const string& method_name, const string& help, @@ -56,12 +67,12 @@ protected: /* -------------------------------------------------------------------- */ void where_filter(RequestAttributes& att, - int filter_flag, - int start_id, - int end_id, - const string& and_clause, - const string& or_clause, - string& where_string); + int filter_flag, + int start_id, + int end_id, + const string& and_clause, + const string& or_clause, + string& where_string); /* -------------------------------------------------------------------- */ @@ -90,7 +101,7 @@ public: RequestManagerPoolInfoFilter("VirtualMachinePoolInfo", "Returns the virtual machine instances pool", "A:siiii") - { + { Nebula& nd = Nebula::instance(); pool = nd.get_vmpool(); auth_object = PoolObjectSQL::VM; @@ -164,7 +175,7 @@ public: RequestManagerPoolInfoFilter("TemplatePoolInfo", "Returns the virtual machine template pool", "A:siii") - { + { Nebula& nd = Nebula::instance(); pool = nd.get_tpool(); auth_object = PoolObjectSQL::TEMPLATE; @@ -183,7 +194,7 @@ public: RequestManagerPoolInfoFilter("VirtualNetworkPoolInfo", "Returns the virtual network pool", "A:siii") - { + { Nebula& nd = Nebula::instance(); pool = nd.get_vnpool(); auth_object = PoolObjectSQL::NET; @@ -202,7 +213,7 @@ public: RequestManagerPoolInfoFilter("ImagePoolInfo", "Returns the image pool", "A:siii") - { + { Nebula& nd = Nebula::instance(); pool = nd.get_ipool(); auth_object = PoolObjectSQL::IMAGE; diff --git a/include/VirtualNetwork.h b/include/VirtualNetwork.h index 1b427dbee8..33216b3443 100644 --- a/include/VirtualNetwork.h +++ b/include/VirtualNetwork.h @@ -343,22 +343,18 @@ public: */ string& to_xml(string& xml) const; - /** - * Function to print the object into a string in XML format - * base64 encoded - * @param xml64 the resulting XML string - * @param extended return the extended template or the simple one - * @return a reference to the generated string - */ - string& to_xml64(string &xml64, bool extended); - /** * Function to print the VirtualNetwork object into a string in * XML format. The extended XML includes the LEASES * @param xml the resulting XML string + * @param vm_ids list of VM the user can access VNET usage info from. + * A vector containing just -1 means all VMs. + * @param vnet_ids list of VNET the user can access reservation info from. + * A vector containing just -1 means all VNETs. * @return a reference to the generated string */ - string& to_xml_extended(string& xml) const; + string& to_xml_extended(string& xml, const vector& vms, + const vector& vnets) const; /** * Replace the template of the virtual network it also updates the BRIDGE, @@ -473,7 +469,8 @@ private: * @param extended If true, leases are included * @return a reference to the generated string */ - string& to_xml_extended(string& xml, bool extended) const; + string& to_xml_extended(string& xml, bool extended, + const vector& vm_ids, const vector& vnet_oids) const; /** * Rebuilds the object from an xml formatted string diff --git a/src/rm/RequestManagerInfo.cc b/src/rm/RequestManagerInfo.cc index 5e344c31a6..ed8e6fbea3 100644 --- a/src/rm/RequestManagerInfo.cc +++ b/src/rm/RequestManagerInfo.cc @@ -15,6 +15,7 @@ /* -------------------------------------------------------------------------- */ #include "RequestManagerInfo.h" +#include "RequestManagerPoolInfoFilter.h" using namespace std; @@ -47,15 +48,15 @@ void RequestManagerInfo::request_execute(xmlrpc_c::paramList const& paramList, object = pool->get(oid,true); - if ( object == 0 ) - { + if ( object == 0 ) + { failure_response(NO_EXISTS, get_error(object_name(auth_object),oid), att); return; - } + } - to_xml(object, str); + to_xml(att, object, str); object->unlock(); @@ -64,3 +65,41 @@ void RequestManagerInfo::request_execute(xmlrpc_c::paramList const& paramList, return; } +/* ------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- */ + +void VirtualNetworkInfo::to_xml(RequestAttributes& att, PoolObjectSQL * object, + string& str) +{ + vector vms; + vector vnets; + + string where_str; + + bool all = RequestManagerPoolInfoFilter::use_filter(att, PoolObjectSQL::NET, + where_str); + + if (all) + { + vnets.push_back(-1); + vms.push_back(-1); + } + else + { + if ( Nebula::instance().get_vnpool()->search(vnets, where_str) != 0 ) + { + //Log warning + } + + where_str = ""; + + RequestManagerPoolInfoFilter::use_filter(att, PoolObjectSQL::VM, where_str); + + if ( Nebula::instance().get_vmpool()->search(vms, where_str) != 0 ) + { + //Log warning + } + } + + static_cast(object)->to_xml_extended(str, vms, vnets); +}; diff --git a/src/rm/RequestManagerPoolInfoFilter.cc b/src/rm/RequestManagerPoolInfoFilter.cc index 7f889b7278..beb248831a 100644 --- a/src/rm/RequestManagerPoolInfoFilter.cc +++ b/src/rm/RequestManagerPoolInfoFilter.cc @@ -23,7 +23,7 @@ using namespace std; const int RequestManagerPoolInfoFilter::ALL = -2; -const int RequestManagerPoolInfoFilter::MINE = -3; +const int RequestManagerPoolInfoFilter::MINE = -3; const int RequestManagerPoolInfoFilter::MINE_GROUP = -1; @@ -31,7 +31,7 @@ const int RequestManagerPoolInfoFilter::MINE_GROUP = -1; const int VirtualMachinePoolInfo::ALL_VM = -2; -const int VirtualMachinePoolInfo::NOT_DONE = -1; +const int VirtualMachinePoolInfo::NOT_DONE = -1; /* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */ @@ -50,6 +50,24 @@ void RequestManagerPoolInfoFilter::request_execute( /* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */ +bool RequestManagerPoolInfoFilter::use_filter(RequestAttributes& att, + PoolObjectSQL::ObjectType aobj, + string& where_str) +{ + bool all; + + string acl_str; + + PoolSQL::acl_filter(att.uid, att.group_ids, aobj, all, acl_str); + + PoolSQL::usr_filter(att.uid, att.group_ids, ALL, all, acl_str, where_str); + + return all; +}; + +/* ------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- */ + void VirtualMachinePoolInfo::request_execute( xmlrpc_c::paramList const& paramList, RequestAttributes& att) @@ -114,8 +132,8 @@ void VirtualMachinePoolAccounting::request_execute( where_filter(att, filter_flag, -1, -1, "", "", where); rc = (static_cast(pool))->dump_acct(oss, - where, - time_start, + where, + time_start, time_end); if ( rc != 0 ) { @@ -295,8 +313,8 @@ void RequestManagerPoolInfoFilter::where_filter( PoolSQL::oid_filter(start_id, end_id, oid_str); - // ------------------------------------------------------------------------- - // Compound WHERE clause + // ------------------------------------------------------------------------- + // Compound WHERE clause // WHERE ( id_str ) AND ( uid_str ) AND ( and_clause ) OR ( or_clause ) // ------------------------------------------------------------------------- diff --git a/src/vm/vm_var_syntax.cc b/src/vm/vm_var_syntax.cc index 2f46a8b51d..3306364a0b 100644 --- a/src/vm/vm_var_syntax.cc +++ b/src/vm/vm_var_syntax.cc @@ -284,7 +284,7 @@ void get_network_attribute(VirtualMachine * vm, if (attr_name == "TEMPLATE") { - attr_value = vn->to_xml64(attr_value, true); + attr_value = vn->to_xml64(attr_value); } else { diff --git a/src/vm/vm_var_syntax.y b/src/vm/vm_var_syntax.y index 1a6bd4ee82..8c593dd65b 100644 --- a/src/vm/vm_var_syntax.y +++ b/src/vm/vm_var_syntax.y @@ -229,7 +229,7 @@ void get_network_attribute(VirtualMachine * vm, if (attr_name == "TEMPLATE") { - attr_value = vn->to_xml64(attr_value, true); + attr_value = vn->to_xml64(attr_value); } else { diff --git a/src/vnm/AddressRange.cc b/src/vnm/AddressRange.cc index 151223b205..65864e01f0 100644 --- a/src/vnm/AddressRange.cc +++ b/src/vnm/AddressRange.cc @@ -20,6 +20,7 @@ #include "NebulaUtil.h" #include +#include using namespace std; @@ -352,11 +353,15 @@ int AddressRange::from_vattr_db(VectorAttribute *vattr) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -void AddressRange::to_xml(ostringstream &oss) const +void AddressRange::to_xml(ostringstream &oss, const vector& vms, + const vector& vns) const { const map& ar_attrs = attr->value(); map::const_iterator it; + bool all_vms = (vms.size() == 1 && vms[0] == -1); + bool all_vns = (vns.size() == 1 && vns[0] == -1); + oss << ""; for (it=ar_attrs.begin(); it != ar_attrs.end(); it++) @@ -379,7 +384,9 @@ void AddressRange::to_xml(ostringstream &oss) const else { map::const_iterator it; + VectorAttribute lease("LEASE"); + bool is_in; oss << ""; @@ -387,6 +394,34 @@ void AddressRange::to_xml(ostringstream &oss) const { lease.clear(); + is_in = false; + + if (it->second & PoolObjectSQL::VM) + { + int vmid = it->second & 0x00000000FFFFFFFFLL; + + if (all_vms || (find(vms.begin(),vms.end(),vmid) != vms.end())) + { + lease.replace("VM", vmid); + is_in = true; + } + } + else if (it->second & PoolObjectSQL::NET) + { + int vnid = it->second & 0x00000000FFFFFFFFLL; + + if (all_vns || (find(vns.begin(),vns.end(),vnid) != vns.end())) + { + lease.replace("VNET", vnid); + is_in = true; + } + } + + if (!is_in) + { + continue; + } + set_mac(it->first, &lease); if (type & 0x00000002 ) @@ -399,19 +434,6 @@ void AddressRange::to_xml(ostringstream &oss) const set_ip6(it->first, &lease); } - if (it->second & PoolObjectSQL::VM) - { - int vmid = it->second & 0x00000000FFFFFFFFLL; - - lease.replace("VM", vmid); - } - else if (it->second & PoolObjectSQL::NET) - { - int vnid = it->second & 0x00000000FFFFFFFFLL; - - lease.replace("VNET", vnid); - } - lease.to_xml(oss); } diff --git a/src/vnm/AddressRangePool.cc b/src/vnm/AddressRangePool.cc index 75cef962f4..99ef34ffb7 100644 --- a/src/vnm/AddressRangePool.cc +++ b/src/vnm/AddressRangePool.cc @@ -228,7 +228,8 @@ int AddressRangePool::rm_ar(unsigned int ar_id, string& error_msg) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -string& AddressRangePool::to_xml(string& sstream, bool extended) const +string& AddressRangePool::to_xml(string& sstream, bool extended, + const vector& vms, const vector& vnets) const { if (extended) { @@ -239,7 +240,7 @@ string& AddressRangePool::to_xml(string& sstream, bool extended) const for (it=ar_pool.begin(); it!=ar_pool.end(); it++) { - it->second->to_xml(oss); + it->second->to_xml(oss, vms, vnets); } oss << ""; diff --git a/src/vnm/VirtualNetwork.cc b/src/vnm/VirtualNetwork.cc index f6715defb3..1e0f4e758c 100644 --- a/src/vnm/VirtualNetwork.cc +++ b/src/vnm/VirtualNetwork.cc @@ -375,39 +375,25 @@ error_common: string& VirtualNetwork::to_xml(string& xml) const { - return to_xml_extended(xml,false); + const vector empty; + + return to_xml_extended(xml,false, empty, empty); } /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -string& VirtualNetwork::to_xml64(string &xml64, bool extended) +string& VirtualNetwork::to_xml_extended(string& xml, const vector& vms, + const vector& vnets) const { - string *str64; - - to_xml_extended(xml64, extended); - - str64 = one_util::base64_encode(xml64); - - xml64 = *str64; - - delete str64; - - return xml64; + return to_xml_extended(xml,true, vms, vnets); } /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -string& VirtualNetwork::to_xml_extended(string& xml) const -{ - return to_xml_extended(xml,true); -} - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -string& VirtualNetwork::to_xml_extended(string& xml, bool extended) const +string& VirtualNetwork::to_xml_extended(string& xml, bool extended, + const vector& vms, const vector& vnets) const { ostringstream os; @@ -459,7 +445,7 @@ string& VirtualNetwork::to_xml_extended(string& xml, bool extended) const os << obj_template->to_xml(template_xml); - os << ar_pool.to_xml(leases_xml, extended); + os << ar_pool.to_xml(leases_xml, extended, vms, vnets); os << "";