Esempio n. 1
0
BOOL CPackage::checkSignature()
{
	CString csFile,
			csDigest,
			csRegDigest;
	BOOL	bRes;

	// Read meta data digest in the registry and verify it
	if (!regReadPackageDigest( csRegDigest))
		// No digest registered in Registry
		return FALSE;
	csFile.Format( _T( "%s\\%s\\%s"), getDownloadFolder(), m_csID, OCS_DOWNLOAD_METADATA);
	if (!fileDigest( csFile, csDigest))
		// Unable to compute digest on meta data
		return FALSE;
	if (csRegDigest.Compare( csDigest) != 0)
		// Digest registered in Registry is not the same
		return FALSE;
	// Now, check ZIP digest
	if (m_uFrags > 0)
	{
		// Compute digest on build.zip file
		csFile.Format( _T( "%s\\%s\\%s"), getDownloadFolder(), m_csID, OCS_DOWNLOAD_BUILD);
		if (isBase64())
			bRes = fileDigest( csFile, csDigest, m_csDigestAlgo);
		else if (isHexadecimal())
			bRes = fileDigest( csFile, csDigest, m_csDigestAlgo, FALSE);
		else
			// Unsupported encoding
			return FALSE;
		// Digest compute successfully
		if (bRes && (m_csDigest.Compare( csDigest) == 0))
			return TRUE;
		else
			return FALSE;
	}
	// No ZIP file
	return TRUE;
}
int COptDownloadPackage::downloadInfoFile()
{
	CConfig				*pAgentConfig = getAgentConfig();
	CComProvider		*pProvider = getComServerProvider();
	CServerConfig		*pServerConfig = pProvider->newConfig();
	CConnexionAbstract	*pConnexion = NULL;
	TCHAR				szDummy[_MAX_PATH + 1];

	ASSERT(pAgentConfig);
	ASSERT(pProvider);
	ASSERT(pServerConfig);

	// Create provider connection object using default Provider configuration
	if ((pConnexion = pProvider->newConnexion(pServerConfig)) == NULL)
	{
		m_pLogger->log(LOG_PRIORITY_ERROR, _T("DOWNLOAD => Failed creating connection for Communication Provider <%s>"), pAgentConfig->getCommunicationProvider());
		pProvider->deleteConfig(pServerConfig);
		return FALSE;
	}
	// Download metadata info file
	m_pLogger->log(LOG_PRIORITY_DEBUG, _T("DOWNLOAD => Metadata file <info> for package <%s> is located at <%s>"), m_csId, getRemoteMetadataURL());
	if (!pConnexion->getFile(getRemoteMetadataURL(), getLocalMetadataFilename()))
	{
		m_pLogger->log(LOG_PRIORITY_ERROR, _T("DOWNLOAD => Failed to download Metadata file <%s> to <%s>"), getRemoteMetadataURL(), getLocalMetadataFilename());
		pProvider->deleteConnexion(pConnexion);
		pProvider->deleteConfig(pServerConfig);
		return FALSE;
	}
	m_pLogger->log(LOG_PRIORITY_DEBUG, _T("DOWNLOAD => Unloading communication provider"));
	// Use provider to delete connexion and server config
	pProvider->deleteConnexion(pConnexion);
	pProvider->deleteConfig(pServerConfig);

	// Open metadata file to add fragment location
	CString csBuffer;
	CMarkup xml;
	if (!xml.LoadFile(getLocalMetadataFilename()))
	{
		m_pLogger->log(LOG_PRIORITY_ERROR, _T("DOWNLOAD => Cannot read or parse Metadata file <%s>"), getLocalMetadataFilename());
		return FALSE;
	}
	// Add fragment location to meta data
	xml.FindFirstElem(_T("DOWNLOAD"));
	xml.SetAttrib(_T("LOC"), m_csRemotePackLoc);
	// Add package schedule to meta data
	xml.SetAttrib(_T("SCHEDULE"), m_csSchedule);
	// Add post execution command to meta data
	xml.SetAttrib(_T("POSTCMD"), m_csPostCmd);
	// Write meta data file
	if (!xml.SaveFile(getLocalMetadataFilename()))
	{
		m_pLogger->log(LOG_PRIORITY_ERROR, _T("DOWNLOAD => Can't update Metadata file <%s>"), getLocalMetadataFilename());
		return FALSE;
	}
	// Compute digest on meta data and add it to Registry
	if (!fileDigest(getLocalMetadataFilename(), csBuffer))
	{
		m_pLogger->log(LOG_PRIORITY_ERROR, _T("DOWNLOAD => Can't register package <%s> into Registry"), m_csId);
		DeleteFile(getLocalMetadataFilename());
		return FALSE;
	}
	if (!regAddPackageDigest(m_csId, csBuffer))
	{
		m_pLogger->log(LOG_PRIORITY_ERROR, _T("DOWNLOAD => Can't register package <%s> into Registry"), m_csId);
		DeleteFile(getLocalMetadataFilename());
		return FALSE;
	}
	// Now create a timestamp 
	csBuffer.Format(_T("%s\\%s\\%s"), getDownloadFolder(), m_csId, OCS_DOWNLOAD_TIMESTAMP);
	if (!fileExists(csBuffer))
	{
		_ltot(time(NULL), szDummy, 10);
		if (!WriteTextToFile(szDummy, csBuffer))
			m_pLogger->log(LOG_PRIORITY_ERROR, _T("DOWNLOAD => Can't create timestamp file <%s>"), csBuffer);
	}
	else
		m_pLogger->log(LOG_PRIORITY_DEBUG, _T("DOWNLOAD => Timestamp file <%s> already exists"), csBuffer);
	m_pLogger->log(LOG_PRIORITY_DEBUG, _T("DOWNLOAD => Retrieve info file...OK (pack %s)"), m_csId);
	return TRUE;
}