/* -------------------------------------------------------------------------- */ /* Copyright 2002-2022, OpenNebula Project, OpenNebula Systems */ /* */ /* 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. */ /* -------------------------------------------------------------------------- */ #include "PoolObjectSQL.h" #include "PoolObjectAuth.h" #include "NebulaUtil.h" #include "SSLUtil.h" #include "Nebula.h" #include "Clusterable.h" #include "ClusterableSingle.h" using namespace std; const string PoolObjectSQL::INVALID_NAME_CHARS = "&|:\\\";/'#{}$<>"; const int PoolObjectSQL::LOCK_DB_EXPIRATION = 120; const long int PoolObjectSQL::LockableObject = PoolObjectSQL::ObjectType::VM | PoolObjectSQL::ObjectType::TEMPLATE | PoolObjectSQL::ObjectType::IMAGE | PoolObjectSQL::ObjectType::MARKETPLACEAPP | PoolObjectSQL::ObjectType::NET | PoolObjectSQL::ObjectType::VROUTER | PoolObjectSQL::ObjectType::VMGROUP | PoolObjectSQL::ObjectType::VNTEMPLATE | PoolObjectSQL::ObjectType::DOCUMENT | PoolObjectSQL::ObjectType::HOOK; /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ string& PoolObjectSQL::to_xml64(string &xml64) { to_xml(xml64); ssl_util::base64_encode(xml64, xml64); return xml64; } /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ int PoolObjectSQL::select(SqlDB *db) { ostringstream oss; int rc; int boid; set_callback( static_cast(&PoolObjectSQL::select_cb)); oss << "SELECT body FROM " << table << " WHERE oid = " << oid; boid = oid; oid = -1; rc = db->exec_rd(oss, this); unset_callback(); if ((rc != 0) || (oid != boid )) { return -1; } return 0; } /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ int PoolObjectSQL::select_oid(SqlDB *db, const char * _table, const string& _name, int _uid) { char * sql_name = db->escape_str(_name); if ( sql_name == 0 ) { return -1; } ostringstream oss; oss << "SELECT oid FROM " << _table << " WHERE "; db->add_binary(oss); oss << "name = '" << sql_name << "'"; if ( _uid != -1 ) { oss << " AND uid = " << _uid; } int bd_oid = -1; single_cb oid_cb; oid_cb.set_callback(&bd_oid); int rc = db->exec_rd(oss, &oid_cb); oid_cb.unset_callback(); db->free_str(sql_name); if (rc != 0) { return -1; } return bd_oid; } /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ int PoolObjectSQL::exist(SqlDB *db, const char * _table, int _oid) { if ( _oid < 0 ) { return -1; } ostringstream oss; oss << "SELECT oid FROM " << _table << " WHERE oid = '" << _oid << "'"; int bd_oid = -1; single_cb oid_cb; oid_cb.set_callback(&bd_oid); int rc = db->exec_rd(oss, &oid_cb); oid_cb.unset_callback(); if (rc != 0) { return -1; } return bd_oid; } /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ int PoolObjectSQL::select(SqlDB *db, const string& _name, int _uid) { ostringstream oss; int rc; char * sql_name; sql_name = db->escape_str(_name); if ( sql_name == 0 ) { return -1; } set_callback( static_cast(&PoolObjectSQL::select_cb)); oss << "SELECT body FROM " << table << " WHERE "; db->add_binary(oss); oss << "name = '" << sql_name << "'"; if ( _uid != -1 ) { oss << " AND uid = " << _uid; } name = ""; uid = -1; rc = db->exec_rd(oss, this); unset_callback(); db->free_str(sql_name); if ((rc != 0) || (_name != name) || (_uid != -1 && _uid != uid)) { return -1; } return 0; } /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ int PoolObjectSQL::drop(SqlDB *db) { ostringstream oss; int rc; oss << "DELETE FROM " << table << " WHERE oid=" << oid; rc = db->exec_wr(oss); return rc; } /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ void PoolObjectSQL::set_template_error_message(const string& message) { set_template_error_message("ERROR", message); } /* -------------------------------------------------------------------------- */ void PoolObjectSQL::set_template_error_message(const string& name, const string& message) { SingleAttribute * attr; ostringstream error_value; error_value << one_util::log_time() << " : " << message; attr = new SingleAttribute(name, error_value.str()); obj_template->erase(name); obj_template->set(attr); } /* -------------------------------------------------------------------------- */ void PoolObjectSQL::clear_template_error_message() { remove_template_attribute("ERROR"); } /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ int PoolObjectSQL::replace_template( const string& tmpl_str, bool keep_restricted, string& error) { string ra; unique_ptr