Ejemplo n.º 1
0
string LdapTools::readStoragePoolUri(const string& storagePoolName) {
	string retval = "";
	string base("sstStoragePool=");
	base.append(storagePoolName).append(",ou=storage pools,ou=virtualization,ou=services,").append(
			Config::getInstance()->getLdapBaseDn());
	SYSLOGLOGGER(logDEBUG) << "readStoragePool " << base;
	LDAPSearchResults* entries = lc->search(base, LDAPConnection::SEARCH_SUB);
	if (entries != 0) {
		LDAPEntry* entry = entries->getNext();
		if (entry != 0) {
//			SYSLOGLOGGER(logINFO) << "dn: " << entry->getDN() << endl;
			const LDAPAttributeList* attrs = entry->getAttributes();
			LDAPAttributeList::const_iterator it = attrs->begin();
			for (; it != attrs->end(); it++) {
				LDAPAttribute attr = *it;
//				SYSLOGLOGGER(logINFO) << attr.getName() << "(";
//				SYSLOGLOGGER(logINFO) << attr.getNumValues() << "): ";
				if (0 == attr.getName().compare("sstStoragePoolURI")) {
					StringList values = attr.getValues();
					StringList::const_iterator it2 = values.begin();
					if (it2 != values.end()) {
						retval = *it2;
						break;
					}
				}
			}
			delete entry;
		}
	}
	return retval;
}
Ejemplo n.º 2
0
void LdifWriter::writeRecord(const LDAPEntry& le)
{
    std::ostringstream line;

    if ( m_addSeparator )
    {
        m_ldifstream << std::endl;
    } else {
        m_addSeparator = true;
    }

    line << "dn: " << le.getDN();
    this->breakline( line.str(), m_ldifstream );

    const LDAPAttributeList *al = le.getAttributes();
    LDAPAttributeList::const_iterator i = al->begin();
    for ( ; i != al->end(); i++ )
    {
        StringList values = i->getValues();
        StringList::const_iterator j = values.begin();
        for( ; j != values.end(); j++)
        {
            // clear output stream
            line.str("");
            line << i->getName() << ": " << *j;
            this->breakline( line.str(), m_ldifstream );
        }
    }
}
Ejemplo n.º 3
0
void LdapTools::readGlobalBackupConfiguration() {
	VmBackupConfiguration* config = Config::getInstance()->getGlobalBackupConfiguration();
	string base("ou=backup,ou=configuration,ou=virtualization,ou=services,");
	base.append(Config::getInstance()->getLdapBaseDn());
	SYSLOGLOGGER(logDEBUG) << "readGlobalBackupConfiguration " << base;
	LDAPSearchResults* entries = lc->search(base, LDAPConnection::SEARCH_SUB);
	if (entries != 0) {
		LDAPEntry* entry = entries->getNext();
		while (entry != 0) {
//			SYSLOGLOGGER(logINFO) << "dn: " << entry->getDN();
			const LDAPAttributeList* attrs = entry->getAttributes();
			LDAPAttributeList::const_iterator it = attrs->begin();
			for (; it != attrs->end(); it++) {
				LDAPAttribute attr = *it;
//				SYSLOGLOGGER(logINFO) << attr.getName() << "(";
//				SYSLOGLOGGER(logINFO) << attr.getNumValues() << "): ";
				StringList values = attr.getValues();
				StringList::const_iterator it2 = values.begin();
				string value = *it2;
//				for (; it2 != values.end(); it2++) {
//
//					SYSLOGLOGGER(logINFO) << *it2 << "; ";
//				}
//				SYSLOGLOGGER(logINFO) << std::endl;
				//retval->addAttribute(entry->getDN(), attr.getName(), value);
				if (0 == attr.getName().compare("sstBackupNumberOfIterations")) {
					config->setIterations(atoi(value.c_str()));
				}
				else if (0 == attr.getName().compare("sstBackupExcludeFromBackup")) {
					config->setExclude(0 == value.compare("TRUE"));
				}
				else if (0 == attr.getName().compare("sstCronActive")) {
					config->setCronActive(0 == value.compare("TRUE"));
				}
				else if (0 == attr.getName().compare("sstCronDay")) {
					config->setCronDay(value);
				}
				else if (0 == attr.getName().compare("sstCronDayOfWeek")) {
					config->setCronDayOfWeek(value);
				}
				else if (0 == attr.getName().compare("sstCronHour")) {
					config->setCronHour(value);
				}
				else if (0 == attr.getName().compare("sstCronMinute")) {
					config->setCronMinute(value);
				}
				else if (0 == attr.getName().compare("sstCronMonth")) {
					config->setCronMonth(value);
				}
			}
			delete entry;
			entry = entries->getNext();
		}
	}
}
Ejemplo n.º 4
0
int main(){
    LDAPConnection *lc=new LDAPConnection("192.168.3.128",389);
    std::cout << "----------------------doing bind...." <<  std::endl;
    try{
        lc->bind("uid=admin,dc=home,dc=local" , "secret");
        std::cout << lc->getHost() << std::endl;
        StringList tmp;
        tmp.add("subschemasubentry");
        LDAPSearchResults* entries = lc->search("", 
                        LDAPConnection::SEARCH_BASE,
                        "(objectClass=*)",
                        tmp );
        LDAPEntry* rootDse = entries->getNext();
        std::string schemabase="cn=subschema";

        if(rootDse){
            const LDAPAttribute* schemaAttr = rootDse->getAttributes()->getAttributeByName("subschemaSubentry");
            schemabase = *(schemaAttr->getValues().begin());   
        }
        StringList attrs;
        attrs.add("objectClasses");
        attrs.add("attributeTypes");
        entries = lc->search(schemabase, LDAPConnection::SEARCH_BASE, "(objectClass=*)",
                        attrs);
        if (entries != 0){
            LDAPEntry* entry = entries->getNext();
            if(entry != 0){
                const LDAPAttribute* oc = entry->getAttributes()->getAttributeByName("objectClasses");
                LDAPSchema schema;
                schema.setObjectClasses((oc->getValues()));
                LDAPObjClass test = schema.getObjectClassByName("inetOrgPerson");
                std::cout << test.getDesc() << std::endl;
//                StringList mustAttr = test.getMay();
//                for( StringList::const_iterator i = mustAttr.begin(); i != mustAttr.end(); i++ ){
//                    std::cout << *i << std::endl;
//                }
                StringList sup = test.getSup();
                for( StringList::const_iterator i = sup.begin(); i != sup.end(); i++ ){
                    std::cout << *i << std::endl;
                }
            }
        }
        
        lc->unbind();
        delete lc;
   }catch (LDAPException e){
        std::cout << "---------------- caught Exception ---------"<< std::endl;
        std::cout << e << std::endl;
    }

}
Ejemplo n.º 5
0
void LdapTools::readConfigurationSettings() {
	string base("ou=settings,ou=configuration,ou=virtualization,ou=services,");
	base.append(Config::getInstance()->getLdapBaseDn());
	SYSLOGLOGGER(logDEBUG) << "readConfigurationSettings " << base;
	LDAPSearchResults* entries = lc->search(base, LDAPConnection::SEARCH_SUB);
	if (entries != 0) {
		LDAPEntry* entry = entries->getNext();
		while (entry != 0) {
//			SYSLOGLOGGER(logINFO) << "dn: " << entry->getDN();
			const LDAPAttributeList* attrs = entry->getAttributes();
			LDAPAttributeList::const_iterator it = attrs->begin();
			for (; it != attrs->end(); it++) {
				LDAPAttribute attr = *it;
//				SYSLOGLOGGER(logINFO) << attr.getName() << "(";
//				SYSLOGLOGGER(logINFO) << attr.getNumValues() << "): ";
				StringList values = attr.getValues();
				StringList::const_iterator it2 = values.begin();
				string value = *it2;
//				for (; it2 != values.end(); it2++) {
//
//					SYSLOGLOGGER(logINFO) << *it2 << "; ";
//				}
//				SYSLOGLOGGER(logINFO) << std::endl;
				//retval->addAttribute(entry->getDN(), attr.getName(), value);
				if (string::npos != entry->getDN().find("ou=sound")) {
					if (0 == attr.getName().compare("sstAllowSound")) {
						Config::getInstance()->setAllowSound(0 == value.compare("TRUE"));
					}
				}
				else if (string::npos != entry->getDN().find("ou=spice")) {
					if (0 == attr.getName().compare("sstAllowSpice")) {
						Config::getInstance()->setAllowSpice(0 == value.compare("TRUE"));
					}
					else if (0 == attr.getName().compare("sstSpicePortMin")) {
						Config::getInstance()->setSpicePortMin(atoi(value.c_str()));
					}
					else if (0 == attr.getName().compare("sstSpicePortMax")) {
						Config::getInstance()->setSpicePortMax(atoi(value.c_str()));
					}
				}
				else if (string::npos != entry->getDN().find("ou=usb")) {
					if (0 == attr.getName().compare("sstAllowUSB")) {
						Config::getInstance()->setAllowUsb(0 == value.compare("TRUE"));
					}
				}
			}
			delete entry;
			entry = entries->getNext();
		}
	}
}
Ejemplo n.º 6
0
void LdapTools::readVmsByPool(VmPool* vmPool, time_t actTime) {
	string base("ou=virtual machines,ou=virtualization,ou=services,");
	base.append(Config::getInstance()->getLdapBaseDn());
	string filter("(&(objectClass=*)(sstVirtualMachinePool=");
	filter.append(vmPool->getName()).append("))");
	LDAPSearchResults* entries = lc->search(base, LDAPConnection::SEARCH_ONE, filter);
	if (entries != 0) {
		LDAPEntry* entry = entries->getNext();
		while (entry != 0) {
			SYSLOGLOGGER(logINFO) << "readVms dn: " << entry->getDN();
			Vm* vm = readVm(entry->getDN(), true);
			if (NULL != vm) {
				if (vm->isDynVm()) {
					if (vm->isGoldenImage()) {
						SYSLOGLOGGER(logINFO) << "  !! not used for policy and backup; is Golden-Image!";
						delete vm;
/*
						if (!vm->hasOwnBackupConfiguration()) {
							vm->setBackupConfiguration(vmPool->getBackupConfiguration());
							SYSLOGLOGGER(logINFO) << "  use backupconf from vmPool " << vmPool->getName() << "!";
						}
						if (vm->isBackupNeeded()) {
							Config::getInstance()->handleVmForBackup(vm, actTime);
						}
*/
					}
					else {
						vmPool->addVm(vm);
						Config::getInstance()->addVm(vm);
					}
				}
				else {
					if (!vm->hasOwnBackupConfiguration()) {
						vm->setBackupConfiguration(vmPool->getBackupConfiguration());
						SYSLOGLOGGER(logINFO) << "  use backupconf from vmPool " << vmPool->getName() << "!";
					}
					SYSLOGLOGGER(logINFO) << "Vm: " << *vm;
					if (vm->isBackupNeeded()) {
						Config::getInstance()->handleVmForBackup(vm, actTime);
					}
				}
			}

			delete entry;
			entry = entries->getNext();
		}
	}
	//SYSLOGLOGGER(logINFO) << "readVmsByPool finished";
}
Ejemplo n.º 7
0
VmPool* LdapTools::readVmPool(const string poolName, bool complete) {
	VmPool* retval = NULL;
	string base;

	if (!complete) {
		base = string("sstVirtualMachinePool=");
		base.append(poolName).append(",ou=virtual machine pools,ou=virtualization,ou=services,").append(
				Config::getInstance()->getLdapBaseDn());
	}
	else {
		base = string(poolName);
	}
	SYSLOGLOGGER(logINFO) << "readVmPool ";
	LDAPSearchResults* entries = lc->search(base, LDAPConnection::SEARCH_SUB);
	if (entries != 0) {
		LDAPEntry* entry = entries->getNext();
		if (entry != 0) {
			retval = new VmPool(entry->getDN(), this);
		}
		while (entry != 0) {
//			SYSLOGLOGGER(logINFO) << "dn: " << entry->getDN();
			const LDAPAttributeList* attrs = entry->getAttributes();
			LDAPAttributeList::const_iterator it = attrs->begin();
			for (; it != attrs->end(); it++) {
				LDAPAttribute attr = *it;
//				SYSLOGLOGGER(logINFO) << attr.getName() << "(";
//				SYSLOGLOGGER(logINFO) << attr.getNumValues() << "): ";
				StringList values = attr.getValues();
				StringList::const_iterator it2 = values.begin();
				string value = *it2;
//				for (; it2 != values.end(); it2++) {
//
//					SYSLOGLOGGER(logINFO) << *it2 << "; ";
//				}
//				SYSLOGLOGGER(logINFO) << std::endl;
				retval->addAttribute(entry->getDN(), attr.getName(), value);
			}
			delete entry;
			entry = entries->getNext();
		}
	}
	if (!retval->hasOwnBackupConfiguration()) {
		retval->setBackupConfiguration(Config::getInstance()->getGlobalBackupConfiguration());
		SYSLOGLOGGER(logINFO) << "  use global backupconf for vmPool " << retval->getName() << "!";
		SYSLOGLOGGER(logINFO) << "  " << *(Config::getInstance()->getGlobalBackupConfiguration());
	}
	return retval;
}
Ejemplo n.º 8
0
vector<AttributeMap> Ldap::search(string base, int scope, string filter, const StringList& attribs) {

    if(bound == false) {
        return vector<AttributeMap>();
    }

    LDAPSearchResults* lr = lc->search(base, scope, filter,attribs, false);

    LDAPEntry* le;
    const LDAPAttribute* la;
    StringList s;
    vector<AttributeMap> result;
	AttributeMap temp;
	int i = 0;

    while( (le = lr->getNext()) ) {

    	for(StringList::const_iterator
    			it =attribs.begin();
				it!=attribs.end();
				it++)
    	{
    	    //cout << endl << "Name: " << *it << " |";
    		la = le->getAttributeByName(*it);
    		if(la == NULL) continue;
			s = la->getValues();
			for(StringList::const_iterator
					st = s.begin();
					st != s.end();
					st ++)
			{
			    // concatenates multivalues with |
				temp[*it] += (i>0?"|"+*st:*st);
				i++;
			}
			i=0;
    	}
    	//cout << endl;

    	if(temp.size() > 0) {
			result.push_back(temp);
	    	temp.clear();
		}

    }

    return result;
}
Ejemplo n.º 9
0
Vm* LdapTools::readVm(const string vmName, bool complete) {
	Vm* retval = NULL;
	string base;

	if (!complete) {
		base = string("sstVirtualMachine=");
		base.append(vmName).append(",ou=virtual machines,ou=virtualization,ou=services,").append(
				Config::getInstance()->getLdapBaseDn());
	}
	else {
		base = string(vmName);
	}
	SYSLOGLOGGER(logINFO) << "readVm " << base;
	LDAPSearchResults* entries = lc->search(base, LDAPConnection::SEARCH_SUB);
	if (entries != 0) {
		LDAPEntry* entry = entries->getNext();
		if (entry != 0) {
			retval = new Vm(entry->getDN(), this);
		}
		while (entry != 0) {
//			SYSLOGLOGGER(logINFO) << "dn: " << entry->getDN();
			const LDAPAttributeList* attrs = entry->getAttributes();
			LDAPAttributeList::const_iterator it = attrs->begin();
			for (; it != attrs->end(); it++) {
				LDAPAttribute attr = *it;
//				SYSLOGLOGGER(logINFO) << attr.getName() << "(" << attr.getNumValues() << "): ";
				StringList values = attr.getValues();
				StringList::const_iterator it2 = values.begin();
				string value = *it2;
//				for (; it2 != values.end(); it2++) {
//					SYSLOGLOGGER(logINFO) << *it2 << "; ";
//				}
				if (0 == attr.getName().compare("sstFeature")) {
					retval->setFeatures(values);
				}
				else {
					retval->addAttribute(entry->getDN(), attr.getName(), value);
				}
			}
			delete entry;
			entry = entries->getNext();
		}
	}
	//SYSLOGLOGGER(logINFO) << "readVm finished!";
	return retval;
}
Ejemplo n.º 10
0
Node* LdapTools::readNode(const string nodeName) {
	Node* retval = NULL;
	string base("sstNode=");
	base.append(nodeName).append(",ou=nodes,ou=virtualization,ou=services,").append(
			Config::getInstance()->getLdapBaseDn());
	SYSLOGLOGGER(logDEBUG) << "readNode " << base;
	LDAPSearchResults* entries = lc->search(base, LDAPConnection::SEARCH_SUB);
	if (entries != 0) {
		LDAPEntry* entry = entries->getNext();
		if (entry != 0) {
			retval = new Node(entry->getDN(), this);
		}
		while (entry != 0) {
//			SYSLOGLOGGER(logINFO) << "dn: " << entry->getDN();
			const LDAPAttributeList* attrs = entry->getAttributes();
			LDAPAttributeList::const_iterator it = attrs->begin();
			for (; it != attrs->end(); it++) {
				LDAPAttribute attr = *it;
//				SYSLOGLOGGER(logINFO) << attr.getName() << "(";
//				SYSLOGLOGGER(logINFO) << attr.getNumValues() << "): ";
				StringList values = attr.getValues();
				StringList::const_iterator it2 = values.begin();
				string value = *it2;
//				for (; it2 != values.end(); it2++) {
//
//					SYSLOGLOGGER(logINFO) << *it2 << "; ";
//				}
//				SYSLOGLOGGER(logINFO) << std::endl;
				retval->addAttribute(entry->getDN(), attr.getName(), value);
			}
			delete entry;
			entry = entries->getNext();
		}
		if (NULL != retval) {
			NodeType* type = retval->getType(string("VM-Node"));
			string nodestate = type->getState();
			retval->setMaintenance(0 == nodestate.compare("maintenance"));
		}
	}
	return retval;
}
Ejemplo n.º 11
0
void LdapTools::readVmPools(const std::string& poolName, time_t actTime) {
	string base("ou=virtual machine pools,ou=virtualization,ou=services,");
	base.append(Config::getInstance()->getLdapBaseDn());
	LDAPSearchResults* entries = lc->search(base.c_str(), LDAPConnection::SEARCH_ONE);
	if (entries != 0) {
		LDAPEntry* entry = entries->getNext();
		while (entry != 0) {
//			SYSLOGLOGGER(logINFO) << "readVmPools dn: " << entry->getDN();
			VmPool* vmPool = readVmPool(entry->getDN(), true);
			SYSLOGLOGGER(logINFO) << "VmPool.type: " << (vmPool->getType());
			// Todo: check if there min. 2 nodes in this pool
			if (0 == poolName.length() || 0 == poolName.compare(vmPool->getName())) {
				if (vmPool->isDynamicType()) {
					if (vmPool->hasActiveGoldenImage()) {
						readVmsByPool(vmPool, actTime);
						Config::getInstance()->addVmPool(vmPool);
					}
					else {
						SYSLOGLOGGER(logINFO) << "  !! not used (VmPool has no active golden image)";
						delete vmPool;
					}
				}
				else if (vmPool->isStaticType() || vmPool->isTemplateType()) {
					// readVmsByPool decides what to do
					readVmsByPool(vmPool, actTime);
				}
				else {
					SYSLOGLOGGER(logINFO) << "  !! not used (unknown type)";
					delete vmPool;
				}
			}
			else {
				SYSLOGLOGGER(logINFO) << "  !! not used (wrong pool)";
				delete vmPool;
			}
			delete entry;
			entry = entries->getNext();
		}
	}
}
Ejemplo n.º 12
0
string LdapTools::getNetworkRangeDn(const string& range) {
	string retval = "";
	string base("ou=dhcp,ou=networks,ou=virtualization,ou=services,");
	base.append(Config::getInstance()->getLdapBaseDn());
	SYSLOGLOGGER(logDEBUG) << "find NetworkRange " << range;
	string filter = "(&(objectClass=sstVirtualizationNetworkRange)(cn=";
	filter.append(range).append("))");
	//SYSLOGLOGGER(logINFO) << "dhcp base: " << base << "; filter " << filter;
	StringList attrs = StringList();
	attrs.add("cn");
	LDAPSearchResults* entries = lc->search(base, LDAPConnection::SEARCH_SUB, filter, attrs);
	if (entries != 0) {
		LDAPEntry* entry = entries->getNext();
		if (entry != 0) {
			retval = string(entry->getDN());
		}
		delete entry;
	}
	//SYSLOGLOGGER(logINFO) << "dn: " << retval;

	return retval;
}
Ejemplo n.º 13
0
void LdapTools::removeEntry(const std::string& dn_, bool recursive, bool keepStart, std::string prefix) {
	SYSLOGLOGGER(logDEBUG) << prefix << "removeEntry " << dn_;
	if (!recursive) {
		lc->del(dn_);
	}
	else {
		LDAPSearchResults* entries = lc->search(dn_, LDAPConnection::SEARCH_ONE);
		if (entries != 0) {
			LDAPEntry* entry = entries->getNext();
			while (entry != 0) {
				//if (0 != dn_.compare(entry->getDN())) {
				removeEntry(entry->getDN(), recursive, false, prefix + "   ");
				//}
				delete entry;
				entry = entries->getNext();
			}
			if (!keepStart) {
				SYSLOGLOGGER(logDEBUG) << prefix << "   remove " << dn_;
				lc->del(dn_);
			}
		}
	}
}
Ejemplo n.º 14
0
const string LdapTools::nextSpicePort(const Node* node) {
	int port = 0;
	int portMin = Config::getInstance()->getSpicePortMin();
	int portMax = Config::getInstance()->getSpicePortMax();
	int size = portMax - portMin + 1;
	//bool* portsUsed = new bool[size];
	bool* portsUsed = (bool *) malloc(size * sizeof(bool));
	for (int i = 0; i < size; i++) {
		portsUsed[i] = false;
	}
	string base("ou=virtual machines,ou=virtualization,ou=services,");
	base.append(Config::getInstance()->getLdapBaseDn());
//	string filter = "(&(objectClass=sstSpice))";
	string filter = "(&(objectClass=sstSpice)(sstNode=";
	filter.append(node->getName()).append("))");
	StringList attrs = StringList();
	attrs.add("sstVirtualMachine");
	attrs.add("sstSpicePort");
	LDAPSearchResults* entries = lc->search(base, LDAPConnection::SEARCH_SUB, filter, attrs);
	LDAPEntry* entry = entries->getNext();
	while (entry != 0) {
		string vmName = "";
		const LDAPAttribute* attribute = entry->getAttributeByName("sstVirtualMachine");
		const StringList values = attribute->getValues();
		StringList::const_iterator it = values.begin();
		if (it != values.end()) {
			vmName = it->c_str();
		}
		const LDAPAttribute* attribute2 = entry->getAttributeByName("sstSpicePort");
		const StringList values2 = attribute2->getValues();
		StringList::const_iterator it2 = values2.begin();
		if (it2 != values2.end()) {
			port = atoi(it2->c_str());
			SYSLOGLOGGER(logDEBUG) << "  " << port << " in use " << port - portMin << " (" << vmName << ")";
			portsUsed[port - portMin] = true;
		}
		delete entry;
		entry = entries->getNext();
	}

	filter = "(&(objectClass=sstSpice)(sstMigrationNode=";
	filter.append(node->getName()).append("))");
	attrs = StringList();
	attrs.add("sstMigrationSpicePort");
	entries = lc->search(base, LDAPConnection::SEARCH_SUB, filter, attrs);
	entry = entries->getNext();
	while (entry != 0) {
		const LDAPAttribute* attribute = entry->getAttributeByName("sstMigrationSpicePort");
		const StringList values = attribute->getValues();
		StringList::const_iterator it = values.begin();
		if (it != values.end()) {
			port = atoi(it->c_str());
			SYSLOGLOGGER(logDEBUG) << "M " << port << " in use " << port - portMin;
			portsUsed[port - portMin] = true;
		}
		delete entry;
		entry = entries->getNext();
	}

	port = 0;
	for (int i = 0; i < size; i++) {
		if (!portsUsed[i]) {
			port = portMin + i;
			break;
		}
	}

	//delete[] portsUsed;
	free(portsUsed);

	SYSLOGLOGGER(logDEBUG) << "nextSpicePort: " << base << "; " << filter << "; port: " << port;
	char buffer[10];
	sprintf(buffer, "%d", port);
	return string(buffer);
}
Ejemplo n.º 15
0
Vm* LdapTools::cloneVm(const Vm* vm, const Node* targetNode, VirtTools* vt, string newUuid) {
	Vm* retval = NULL;
	size_t pos;
	const VmPool* vmPool = vm->getVmPool();

	SYSLOGLOGGER(logDEBUG) << "cloneVm DN: " << (vm->getDn());
	LDAPSearchResults* entries = lc->search(vm->getDn(), LDAPConnection::SEARCH_SUB);
	if (entries != 0) {
		string newVmDn;

		LDAPEntry* entry = entries->getNext();
		const string oldVmDn = entry->getDN();
		string uuid;
		vm->getDnPart(oldVmDn, "sstVirtualMachine", uuid);
		SYSLOGLOGGER(logDEBUG) << "old DN: " << oldVmDn << "; uuid: " << uuid;
		string oldVm = "sstVirtualMachine=";
		oldVm.append(uuid);

		string newVm = "sstVirtualMachine=" + newUuid;
		pos = oldVmDn.find(oldVm);
		newVmDn = newVm;
		newVmDn.append(oldVmDn.substr(oldVm.length()));
		SYSLOGLOGGER(logDEBUG) << "new DN: " << newVmDn;

		string firstMac = "";
		bool diskSet = false;

		while (entry != 0) {
			string dn = entry->getDN();
			pos = dn.find(oldVm);
			string newDn = dn.substr(0, pos);
			newDn.append(newVm).append(dn.substr(pos + oldVm.length()));
			//SYSLOGLOGGER(logDEBUG) << "oldDn: " << dn;
			//SYSLOGLOGGER(logDEBUG) << "newDn: " << newDn;

			LDAPEntry* newEntry = new LDAPEntry(newDn, entry->getAttributes());
			if (0 == newDn.find("sstVirtualMachine")) {
				// DN starts with sstVirtualMachine
				newEntry->delAttribute("sstVirtualMachine");
				const LDAPAttribute* attribute = entry->getAttributeByName("sstDisplayName");
				StringList values = attribute->getValues();
				StringList::const_iterator it = values.begin();
				string displayName = *it;
				displayName.append(" clone");
				newEntry->replaceAttribute(LDAPAttribute("sstDisplayName", displayName));
				newEntry->replaceAttribute(LDAPAttribute("sstNode", targetNode->getName()));
				newEntry->replaceAttribute(LDAPAttribute("sstVirtualMachineType", "dynamic"));
				newEntry->replaceAttribute(LDAPAttribute("sstVirtualMachineSubType", "Desktop"));
				newEntry->replaceAttribute(LDAPAttribute("sstOsBootDevice", "hd"));
				newEntry->replaceAttribute(LDAPAttribute("sstSpicePort", nextSpicePort(targetNode)));
				newEntry->replaceAttribute(LDAPAttribute("sstSpicePassword", newUuid));
			}
			else if (0 == newDn.find("sstDisk") && !diskSet) {
				// DN starts with sstDisk
				const LDAPAttribute* attribute = entry->getAttributeByName("sstDevice");
				StringList values = attribute->getValues();
				StringList::const_iterator it = values.begin();
				string value = *it;
				if (0 == value.compare("disk")) {
					const string volumeName = vt->generateUUID();
					string sourceFile = vmPool->getStoragePoolDir();
					sourceFile.append("/").append(volumeName).append(".qcow2");
					SYSLOGLOGGER(logINFO) << "volumeName: " << volumeName;
					SYSLOGLOGGER(logINFO) << "sourceFile: " << sourceFile;
					try {
						vt->createBackingStoreVolumeFile(vm, vmPool->getStoragePoolName(), volumeName);
					}
					catch (VirtException& e) {
						SYSLOGLOGGER(logINFO) << "-------------- caught Exception ---------";
						SYSLOGLOGGER(logINFO) << e;
						lc->del(newDn);
						delete entry;
						return NULL;
					}

					newEntry->replaceAttribute(LDAPAttribute("sstVolumeName", volumeName));
					newEntry->replaceAttribute(LDAPAttribute("sstSourceFile", sourceFile));
					diskSet = true;
				}
			}
			else if (0 == newDn.find("sstInterface") && 0 == firstMac.size()) {
				// DN start with sstInterface
				firstMac = vt->generateMacAddress();
				newEntry->replaceAttribute(LDAPAttribute("sstMacAddress", firstMac));
			}
			lc->add(newEntry);

			delete entry;
			entry = entries->getNext();
		}
		string peopleDn = "ou=people,";
		peopleDn.append(newVmDn);

		LDAPEntry* peopleEntry = new LDAPEntry(peopleDn);
		StringList values;
		values.add("top");
		values.add("organizationalUnit");
		values.add("sstRelationship");

		peopleEntry->addAttribute(LDAPAttribute("objectClass", values));
		peopleEntry->addAttribute(LDAPAttribute("ou", "people"));
		peopleEntry->addAttribute(LDAPAttribute("description", "This is the assigned people subtree."));
		peopleEntry->addAttribute(LDAPAttribute("sstBelongsToCustomerUID", vm->getCustomerUID()));
		peopleEntry->addAttribute(LDAPAttribute("sstBelongsToResellerUID", vm->getResellerUID()));
		lc->add(peopleEntry);
		delete peopleEntry;

		const NetworkRange* range = vmPool->getRange();
		string base = "ou=dhcp,ou=networks,ou=virtualization,ou=services,";
		base.append(Config::getInstance()->getLdapBaseDn());
		string filter = "(&(objectClass=sstVirtualizationNetworkRange)(cn=";
		filter.append(range->getRange()).append("))");
		SYSLOGLOGGER(logDEBUG) << "dhcp base: " << base << "; filter " << filter;
		StringList attrs = StringList();
		attrs.add("cn");
		LDAPSearchResults* entries2 = lc->search(base, LDAPConnection::SEARCH_SUB, filter, attrs);
		if (NULL != entries2) {
			LDAPEntry* entry2 = entries2->getNext();
			if (NULL != entry2) {
				string dn = "cn=";
				dn.append(newUuid).append(",ou=virtual machines,");
				string entryDn = entry2->getDN();
				delete entry2;
				SYSLOGLOGGER(logDEBUG) << "rangeDN: " << entryDn;
				pos = entryDn.find("ou=ranges,");
				dn.append(entryDn.substr(pos + 10));
				SYSLOGLOGGER(logDEBUG) << "dhcp dn:" << dn;
				LDAPEntry* dhcpEntry = new LDAPEntry(dn);
				StringList vals;
				vals.add("top");
				vals.add("dhcpHost");
				vals.add("sstVirtualizationNetwork");

				dhcpEntry->addAttribute(LDAPAttribute("objectClass", vals));
				dhcpEntry->addAttribute(LDAPAttribute("cn", newUuid));
				dhcpEntry->addAttribute(LDAPAttribute("sstBelongsToCustomerUID", vm->getCustomerUID()));
				dhcpEntry->addAttribute(LDAPAttribute("sstBelongsToResellerUID", vm->getResellerUID()));
				dhcpEntry->addAttribute(LDAPAttribute("dhcpHWAddress", "ethernet " + firstMac));
				dhcpEntry->addAttribute(LDAPAttribute("dhcpStatements", "fixed-address " + getFreeIp(range)));
				lc->add(dhcpEntry);
				delete dhcpEntry;
			}
		}

		retval = readVm(newVmDn, true);
	}

	return retval;
}
Ejemplo n.º 16
0
void Vm::handleBackupWorkflow(VirtTools* vt) {
	string newMode = "";
	string newState = "0";
	if (0 == activeBackupMode.compare("initialize")) {
		//20121002T010000Z
		string backupDn = "ou=backup,";
		backupDn.append(getDn());
		if (!lt->hasDn(backupDn)) {
			LDAPEntry* backupEntry = new LDAPEntry(backupDn);
			StringList values;
			values.add("top");
			values.add("organizationalUnit");

			backupEntry->addAttribute(LDAPAttribute("objectClass", values));
			backupEntry->addAttribute(LDAPAttribute("ou", "backup"));
			lt->addEntry(backupEntry);
			delete backupEntry;
		}
		char buffer[18];

		struct tm * timeinfo;
		time_t rawtime = backupConfiguration.getNextTime();
		timeinfo = localtime(&rawtime);

		strftime(buffer, 18, "%Y%m%dT%H%M00Z", timeinfo);

		activeBackupDn = "ou=";
		activeBackupDn.append(buffer).append(",").append(backupDn);
		if (!lt->hasDn(activeBackupDn)) {
			LDAPEntry* singelBackupEntry = new LDAPEntry(activeBackupDn);
			StringList values;
			values.add("top");
			values.add("organizationalUnit");
			values.add("sstProvisioning");

			time(&rawtime);
			timeinfo = localtime(&rawtime);
			strftime(buffer, 18, "%Y%m%dT%H%M00Z", timeinfo);

			singelBackupEntry->addAttribute(LDAPAttribute("objectClass", values));
			singelBackupEntry->addAttribute(LDAPAttribute("ou", buffer));
			singelBackupEntry->addAttribute(LDAPAttribute("sstProvisioningExecutionDate", "0"));
			singelBackupEntry->addAttribute(LDAPAttribute("sstProvisioningMode", "initialized"));
			singelBackupEntry->addAttribute(LDAPAttribute("sstProvisioningState", "0"));
			lt->addEntry(singelBackupEntry);
			delete singelBackupEntry;
		}
		SYSLOGLOGGER(logDEBUG) << (getDn()) << ": change sstProvisioningMode from initialize to initialized";

		newMode = "snapshot";
		newState = "0";
	}
	else if (0 == activeBackupMode.compare("initialized")) {
		// This attribute is written by the fc-brokerd and used internally by the fc-brokerd.

		newMode = "snapshot";
		newState = "0";
	}
	else if (0 == activeBackupMode.compare("snapshotted") && 0 == activeBackupReturnValue) {
		//  The attribute is changed by the Backup-Daemon from snapshotting to snapshotted when the snapshot process has finished.

		newMode = "merge";
		newState = "0";
	}
	else if (0 == activeBackupMode.compare("merged") && 0 == activeBackupReturnValue) {
		// The attribute is changed by the Backup-Daemon from merging to merged when the merge process has finished.

		newMode = "retain";
		newState = "0";
	}
	else if (0 == activeBackupMode.compare("retained") && 0 == activeBackupReturnValue) {
		// The attribute is changed by the Backup-Daemon from retaining to retained when the retain process has finished.

		newMode = "finished";
		time_t rawtime;
		struct tm * timeinfo;
		char buffer[18];

		time(&rawtime);
		timeinfo = localtime(&rawtime);

		strftime(buffer, 18, "%Y%m%dT%H%M00Z", timeinfo);
		newState = buffer;
	}
	else if (0 == activeBackupMode.compare("deleted") && 0 == activeBackupReturnValue) {
		lt->removeEntry(activeBackupDn, true);
	}
	else if (0 == activeBackupMode.compare("unretainedLargeFiles") && 0 == activeBackupReturnValue) {
		// The attribute is changed by the Backup-Daemon from unretaining to unretained when the unretain process has finished.

		newMode = "restore";
		newState = "0";

		vt->stopVmForRestore(this);
		// Merge ldif
	}
	else if (0 == activeBackupMode.compare("restored") && 0 == activeBackupReturnValue) {
		// The attribute is changed by the Backup-Daemon from restoring to restored when the restore process has finished.

		newMode = "finished";
		time_t rawtime;
		struct tm * timeinfo;
		char buffer[18];

		time(&rawtime);
		timeinfo = localtime(&rawtime);

		strftime(buffer, 18, "%Y%m%dT%H%M00Z", timeinfo);
		newState = buffer;
	}
	if (0 != newMode.length()) {
		LDAPModList* modlist = new LDAPModList();
		const string value = newMode;
		LDAPAttribute attr = LDAPAttribute("sstProvisioningMode", value);
		LDAPModification modification = LDAPModification(attr, LDAPModification::OP_REPLACE);
		modlist->addModification(modification);
		const string value2 = newState;
		attr = LDAPAttribute("sstProvisioningState", value2);
		modification = LDAPModification(attr, LDAPModification::OP_REPLACE);
		modlist->addModification(modification);
		SYSLOGLOGGER(logDEBUG) << (getDn()) << ": change sstProvisioningMode from " << activeBackupMode << " to " << value;
		lt->modifyEntry(activeBackupDn, modlist);
		delete modlist;
	}
	if (0 == newMode.compare("finished") && 0 == activeBackupMode.compare("retained") && singleBackupCount > backupConfiguration.getIterations()) {
		// check for deletion of an older backup
		if (0 < finishedBackups.size()) {
			string oldBackupDn = *(finishedBackups.begin());

			LDAPModList* modlist = new LDAPModList();
			LDAPAttribute attr = LDAPAttribute("sstProvisioningMode", "delete");
			LDAPModification modification = LDAPModification(attr, LDAPModification::OP_REPLACE);
			modlist->addModification(modification);
			attr = LDAPAttribute("sstProvisioningState", "0");
			modification = LDAPModification(attr, LDAPModification::OP_REPLACE);
			modlist->addModification(modification);
			SYSLOGLOGGER(logDEBUG) << (getDn()) << ": change sstProvisioningMode from finished to delete";
			lt->modifyEntry(oldBackupDn, modlist);
			delete modlist;
		}
	}
}
Ejemplo n.º 17
0
const string LdapTools::getFreeIp(const NetworkRange* range) {
	string retval = "";
	set<long> ips;

	string base = "ou=dhcp,ou=networks,ou=virtualization,ou=services,";
	base.append(Config::getInstance()->getLdapBaseDn());
	string filter = "(&(objectClass=sstVirtualizationNetworkRange)(cn=";
	filter.append(range->getRange()).append("))");
	StringList attrs = StringList();
	attrs.add("cn");
	SYSLOGLOGGER(logDEBUG) << "getFreeIp: " << base << "; " << filter;
	LDAPSearchResults* entries = lc->search(base, LDAPConnection::SEARCH_SUB, filter, attrs);
	if (NULL != entries) {
		LDAPEntry* entry = entries->getNext();
		if (NULL != entry) {
			string dn = entry->getDN();
			SYSLOGLOGGER(logDEBUG) << "rangeDN: " << dn;
			size_t pos = dn.find("ou=ranges,");
			dn = dn.substr(pos + 10);
			SYSLOGLOGGER(logDEBUG) << "subnetDN: " << dn;

			attrs.clear();
			attrs.add("dhcpOption");
			LDAPSearchResults* entries2 = lc->search(dn, LDAPConnection::SEARCH_SUB, "objectClass=dhcpOptions", attrs);
			if (NULL != entries2) {
				LDAPEntry* entry2 = entries2->getNext();
//				while (entry2 != 0) {
//					std::cout << "dn: " << entry2->getDN() << endl;
//					const LDAPAttributeList* attrs = entry2->getAttributes();
//					LDAPAttributeList::const_iterator it = attrs->begin();
//					for (; it != attrs->end(); it++) {
//						LDAPAttribute attr = *it;
//						std::cout << attr.getName() << "(";
//						std::cout << attr.getNumValues() << "): ";
//						StringList values = attr.getValues();
//						StringList::const_iterator it2 = values.begin();
//						for (; it2 != values.end(); it2++) {
//
//							std::cout << *it2 << "; ";
//						}
//						std::cout << std::endl;
//					}
//					std::cout << endl;
//					delete entry2;
//					entry2 = entries->getNext();
//				}

				if (NULL != entry2) {
					const LDAPAttribute* attribute = entry2->getAttributeByName("dhcpOption");
					if (NULL != attribute) {
						StringList values = attribute->getValues();
						for (StringList::const_iterator it = values.begin(); it != values.end(); it++) {
							string value = *it;
							if (0 == value.find("routers ")) {
								SYSLOGLOGGER(logDEBUG) << value;
								ips.insert(NetworkRange::ip2long(value.substr(8)));
								break;
							}
						}
					}
					delete entry2;
				}
			}

			dn = "ou=virtual machines," + dn;
			SYSLOGLOGGER(logDEBUG) << "search IPs DN: " << dn;

			filter = "(objectClass=dhcpHost)";
			attrs.clear();
			attrs.add("dhcpStatements");
			LDAPSearchResults* entries3 = lc->search(dn, LDAPConnection::SEARCH_SUB, filter, attrs);
			if (NULL != entries3) {
				LDAPEntry* entry3 = entries3->getNext();
				while (NULL != entry3) {
					StringList values = entry3->getAttributeByName("dhcpStatements")->getValues();
					for (StringList::const_iterator it = values.begin(); it != values.end(); it++) {
						string value = *it;
						if (0 == value.find("fixed-address ")) {
							SYSLOGLOGGER(logDEBUG) << value << "; " << (NetworkRange::ip2long(value.substr(14)));
							ips.insert(NetworkRange::ip2long(value.substr(14)));
						}
					}

					delete entry3;
					entry3 = entries3->getNext();
				}
			}
			delete entry;
		}
	}
	long hostmin = NetworkRange::ip2long(range->getHostMin());
	long hostmax = NetworkRange::ip2long(range->getHostMax());
	for (long i = hostmin; i <= hostmax; i++) {
		SYSLOGLOGGER(logDEBUG) << i << ": " << (NetworkRange::long2ip(i));
		if (ips.end() == ips.find(i)) {
			retval = NetworkRange::long2ip(i);
			break;
		}
	}

	return retval;
}