Exemple #1
0
void* Encrypt::unZipForNet(int* dstLength,void* src,int srcLength)
{
	int* rev((int*)src);
	*rev = htonl(*rev);
	void* ret = unZip(dstLength,src,srcLength);
	*rev = htonl(*rev);
	return ret;
}
Exemple #2
0
void* Encrypt::decrypt(int* dstLength,void* src,int srcLength)
{
	int zipLen;
	void* zipData = unXor(&zipLen,src,srcLength);
	if(zipData == NULL)
	{
		return NULL;
	}
	int oriLen;
	void* oriData = unZip(&oriLen,zipData,zipLen);
	delete[]((char *) zipData);
	if(oriData == NULL)
	{
		return NULL;
	}
	*dstLength = oriLen;
	return oriData;
}
UINT CPackage::execute( UINT uCommandTimeOut)
{
	CLog *pLog = getOcsLogger();
	CExecCommand cmProcess;
	CString csBuffer;
	UINT	uTry;

	// Check signature before executing package
	if (!checkSignature())
	{
		pLog->log( LOG_PRIORITY_WARNING, _T( "PACKAGE => Failed verifying signature for Package <%s>"), m_csID);
		setDone( ERR_BAD_DIGEST);
		return FALSE;
	}
	// Check if package not crashing all time
	if (!getExecTry( &uTry))
		// Assuming first execution try
		uTry = 0;
	uTry++;
	if (uTry > MAX_ERROR_COUNT)
	{
		pLog->log( LOG_PRIORITY_WARNING, _T( "PACKAGE => Max try count (%u) reached while executing Package <%s>"), uTry, m_csID);
		setDone( ERR_EXECUTE_TOO_MANY_TRY);
		return FALSE;
	}
	else
		setExecTry( uTry);
	
	if (m_csAction == OCS_DOWNLOAD_ACTION_LAUNCH)
	{
		// We need to wait for all threads/processes started
		// First, extrac build.zip to tmp folder
		if (!directoryCreate( m_csPath) || !unZip())
		{
			pLog->log( LOG_PRIORITY_WARNING, _T( "PACKAGE => Failed to unzip package <%s> to folder <%s>"), m_csID, m_csPath);
			setDone( ERR_UNZIP);
			return FALSE;
		}
		pLog->log( LOG_PRIORITY_DEBUG, _T( "PACKAGE => Launching command <%s> for package <%s> on <%s>"), m_csCommand, m_csID, CTime::GetCurrentTime().Format( _T( "%#c")));
		if (m_csCommand.IsEmpty())
		{
			pLog->log( LOG_PRIORITY_WARNING, _T( "PACKAGE => No command provided for <%s> action of package <%s>"), OCS_DOWNLOAD_ACTION_LAUNCH, m_csID);
			setDone( ERR_BAD_PARAM);
			return FALSE;
		}
		// Set command time out in milliseconds
		cmProcess.setTimeout( uCommandTimeOut * 60 * 1000);
		// Execute command and wait for all childs to terminate
		switch (cmProcess.execWaitForAllChilds( m_csCommand, m_csPath))
		{
		case EXEC_ERROR_START_COMMAND:
			pLog->log( LOG_PRIORITY_WARNING, _T( "PACKAGE => Failed to launch command <%s> for package <%s> (%s)"), m_csCommand, m_csID, cmProcess.getOutput());
			setDone( ERR_EXECUTE);
			return FALSE;
		case EXEC_ERROR_WAIT_COMMAND:
			pLog->log( LOG_PRIORITY_WARNING, _T( "PACKAGE => Failed to get command <%s> result code for package <%s> (%s)"), m_csCommand, m_csID, cmProcess.getOutput());
			csBuffer = ERR_EXECUTE_NO_EXIT_CODE;
			break;
		case EXEC_ERROR_TIMEOUT_COMMAND:
			pLog->log( LOG_PRIORITY_WARNING, _T( "PACKAGE => Command <%s> execution reached timeout on <%s> (%s)"), m_csCommand, CTime::GetCurrentTime().Format( _T( "%#c")), cmProcess.getOutput());
			csBuffer = ERR_EXECUTE_TIMEOUT;
			break;
		default:
			isExecSuccessful( cmProcess.getExitValue(), csBuffer);
			pLog->log( LOG_PRIORITY_DEBUG, _T( "PACKAGE => Package <%s> successfully launched. Command exit code is <%d>. Package return code is <%s>"), m_csID, cmProcess.getExitValue(), csBuffer);
			break;
		}
		setDone( csBuffer);
		return TRUE;
	}
	if (m_csAction == OCS_DOWNLOAD_ACTION_EXECUTE)
	{
		// We only need to wait for cmmand result
		// First, extrac build.zip to tmp folder
		if (!directoryCreate( m_csPath) || !unZip())
		{
			pLog->log( LOG_PRIORITY_WARNING, _T( "PACKAGE => Failed to unzip package <%s> to folder <%s>"), m_csID, m_csPath);
			setDone( ERR_UNZIP);
			return FALSE;
		}
		pLog->log( LOG_PRIORITY_DEBUG, _T( "PACKAGE => Executing command <%s> for package <%s> on <%s>"), m_csCommand, m_csID, CTime::GetCurrentTime().Format( _T( "%#c")));
		if (m_csCommand.IsEmpty())
		{
			pLog->log( LOG_PRIORITY_WARNING, _T( "PACKAGE => No command provided for <%s> action of package <%s>"), OCS_DOWNLOAD_ACTION_EXECUTE, m_csID);
			setDone( ERR_BAD_PARAM);
			return FALSE;
		}
		// Set command time out in milliseconds
		cmProcess.setTimeout( uCommandTimeOut * 60 * 1000);
		// Execute command and wait only for main process to terminate
		switch (cmProcess.execWait( m_csCommand, m_csPath))
		{
		case EXEC_ERROR_START_COMMAND:
			pLog->log( LOG_PRIORITY_WARNING, _T( "PACKAGE => Failed to execute command <%s> for package <%s> (%s)"), m_csCommand, m_csID, cmProcess.getOutput());
			setDone( ERR_EXECUTE);
			return FALSE;
		case EXEC_ERROR_WAIT_COMMAND:
			pLog->log( LOG_PRIORITY_WARNING, _T( "PACKAGE => Failed to get command <%s> result code for package <%s> (%s)"), m_csCommand, m_csID, cmProcess.getOutput());
			csBuffer = ERR_EXECUTE_NO_EXIT_CODE;
			break;
		case EXEC_ERROR_TIMEOUT_COMMAND:
			pLog->log( LOG_PRIORITY_WARNING, _T( "PACKAGE => Command <%s> execution reached timeout on <%s> (%s)"), m_csCommand, CTime::GetCurrentTime().Format( _T( "%#c")), cmProcess.getOutput());
			csBuffer = ERR_EXECUTE_TIMEOUT;
			break;
		default:
			isExecSuccessful( cmProcess.getExitValue(), csBuffer);
			pLog->log( LOG_PRIORITY_DEBUG, _T( "PACKAGE => Package <%s> successfully executed. Command exit code is <%d>. Package return code is <%s>"), m_csID, cmProcess.getExitValue(), csBuffer);
		}
		setDone( csBuffer, GetUnicodeFromAnsi( cmProcess.getOutput()));
		return TRUE;
	}
	if (m_csAction == OCS_DOWNLOAD_ACTION_STORE)
	{
		// We just want to unzip data to specified folder
		pLog->log( LOG_PRIORITY_DEBUG, _T( "PACKAGE => Unzipping data to folder <%s> for package <%s> on <%s>"), m_csPath, m_csID, CTime::GetCurrentTime().Format( _T( "%#c")));
		if (m_csPath.IsEmpty())
		{
			pLog->log( LOG_PRIORITY_WARNING, _T( "PACKAGE => No path provided for <%s> action of package <%s>"), OCS_DOWNLOAD_ACTION_STORE, m_csID);
			setDone( ERR_BAD_PARAM);
			return FALSE;
		}
		if (!directoryCreate( m_csPath) || !unZip())
		{
			pLog->log( LOG_PRIORITY_WARNING, _T( "PACKAGE => Failed to unzip package <%s> to folder <%s>"), m_csID, m_csPath);
			setDone( ERR_UNZIP);
			return FALSE;
		}
		pLog->log( LOG_PRIORITY_DEBUG, _T( "PACKAGE => Package <%s> successfully stored to folder <%s>"), m_csID, m_csPath);
		setDone( CODE_SUCCESS);
		return TRUE;
	}
	// Unknown action
	pLog->log( LOG_PRIORITY_WARNING, _T( "PACKAGE => Unknown action <%s> for package <%s>"), m_csAction, m_csID);
	setDone( ERR_BAD_PARAM);
	return FALSE;
}