Esempio n. 1
0
HRESULT RunStoreValidation(char* strHost, char* strUser, char* strPass, char *strAltUser, bool bPublic, CHECKMAP checkmap)
{
	HRESULT hr = hrSuccess;
	LPMAPISESSION lpSession = NULL;
	LPMDB lpStore = NULL;
	LPMDB lpAltStore = NULL;
	LPMDB lpReadStore = NULL;
	LPMAPIFOLDER lpRootFolder = NULL;
	LPMAPITABLE lpHierarchyTable = NULL;
	LPSRowSet lpRows = NULL;
	ULONG ulObjectType;
	ULONG ulCount;
    LPEXCHANGEMANAGESTORE lpIEMS = NULL;
    // user
    ULONG			cbUserStoreEntryID = 0;
    LPENTRYID		lpUserStoreEntryID = NULL;
	wstring strwUsername;
	wstring strwAltUsername;
	wstring strwPassword;
	std::set<std::string> setFolderIgnore;
	LPSPropValue lpAddRenProp = NULL;
	ULONG cbEntryIDSrc = 0;
	LPENTRYID lpEntryIDSrc = NULL;
	ECLogger *const lpLogger = new ECLogger_File(EC_LOGLEVEL_FATAL, 0, "-");

	hr = MAPIInitialize(NULL);
	if (hr != hrSuccess) {
		cout << "Unable to initialize session" << endl;
		goto exit;
	}

	// input from commandline is current locale
	if (strUser)
		strwUsername = convert_to<wstring>(strUser);
	if (strPass)
		strwPassword = convert_to<wstring>(strPass);
	if (strAltUser)
		strwAltUsername = convert_to<wstring>(strAltUser);

	hr = HrOpenECSession(lpLogger, &lpSession, "zarafa-fsck", PROJECT_SVN_REV_STR, strwUsername.c_str(), strwPassword.c_str(), (const char *)strHost, 0, NULL, NULL);
	lpLogger->Release();
	if(hr != hrSuccess) {
		cout << "Wrong username or password." << endl;
		goto exit;
	}
	
	if (bPublic) {
		hr = HrOpenECPublicStore(lpSession, &lpStore);
		if (hr != hrSuccess) {
			cout << "Failed to open public store." << endl;
			goto exit;
		}
	} else {
		hr = HrOpenDefaultStore(lpSession, &lpStore);
		if (hr != hrSuccess) {
			cout << "Failed to open default store." << endl;
			goto exit;
		}
	}

	if (!strwAltUsername.empty()) {
        hr = lpStore->QueryInterface(IID_IExchangeManageStore, (void **)&lpIEMS);
        if (hr != hrSuccess) {
            cout << "Cannot open ExchangeManageStore object" << endl;
            goto exit;
        }

        hr = lpIEMS->CreateStoreEntryID(L"", (LPTSTR)strwAltUsername.c_str(), MAPI_UNICODE | OPENSTORE_HOME_LOGON, &cbUserStoreEntryID, &lpUserStoreEntryID);
        if (hr != hrSuccess) {
            cout << "Cannot get user store id for user" << endl;
            goto exit;
        }

        hr = lpSession->OpenMsgStore(0, cbUserStoreEntryID, lpUserStoreEntryID, NULL, MDB_WRITE | MDB_NO_DIALOG | MDB_NO_MAIL | MDB_TEMPORARY, &lpAltStore);
        if (hr != hrSuccess) {
            cout << "Cannot open user store of user" << endl;
            goto exit;
        }
        
        lpReadStore = lpAltStore;
	} else {
	    lpReadStore = lpStore;
    }

	hr = lpReadStore->OpenEntry(0, NULL, &IID_IMAPIFolder, 0, &ulObjectType, (IUnknown **)&lpRootFolder);
	if(hr != hrSuccess) {
		cout << "Failed to open root folder." << endl;
		goto exit;
	}

	if (HrGetOneProp(lpRootFolder, PR_IPM_OL2007_ENTRYIDS /*PR_ADDITIONAL_REN_ENTRYIDS_EX*/, &lpAddRenProp) == hrSuccess &&
		Util::ExtractSuggestedContactsEntryID(lpAddRenProp, &cbEntryIDSrc, &lpEntryIDSrc) == hrSuccess) {
		setFolderIgnore.insert(string((const char*)lpEntryIDSrc, cbEntryIDSrc));
	}

	hr = lpRootFolder->GetHierarchyTable(CONVENIENT_DEPTH, &lpHierarchyTable);
	if (hr != hrSuccess) {
		cout << "Failed to open hierarchy." << endl;
		goto exit;
	}

	/*
	 * Check if we have found at least *something*.
	 */
	hr = lpHierarchyTable->GetRowCount(0, &ulCount);
	if(hr != hrSuccess) {
		cout << "Failed to count number of rows." << endl;
		goto exit;
	} else if (!ulCount) {
		cout << "No entries inside Calendar." << endl;
		goto exit;
	}

	/*
	 * Loop through each row/entry and validate.
	 */
	while (true) {
		hr = lpHierarchyTable->QueryRows(20, 0, &lpRows);
		if (hr != hrSuccess)
			break;

		if (lpRows->cRows == 0)
			break;

		for (ULONG i = 0; i < lpRows->cRows; i++)
			RunFolderValidation(setFolderIgnore, lpRootFolder, &lpRows->aRow[i], checkmap);

		if (lpRows) {
			FreeProws(lpRows);
			lpRows = NULL;
		}
	}

exit:
    if (lpUserStoreEntryID)
        MAPIFreeBuffer(lpUserStoreEntryID);

    if (lpIEMS)
        lpIEMS->Release();
        
	if (lpRows) {
		FreeProws(lpRows);
		lpRows = NULL;
	}

	if (lpEntryIDSrc)
		MAPIFreeBuffer(lpEntryIDSrc);

	if (lpAddRenProp)
		MAPIFreeBuffer(lpAddRenProp);

	if(lpHierarchyTable)
		lpHierarchyTable->Release();

	if (lpRootFolder)
		lpRootFolder->Release();

    if (lpAltStore)
        lpAltStore->Release();

	if (lpStore)
		lpStore->Release();

	if (lpSession)
		lpSession->Release();

	MAPIUninitialize();

	return hr;
}
/// <summary>
/// <para name='Name'>GetEID</para>
/// <para name='Purpose'>Resolve the EID from the email address</para>
/// </summary>
/// <param name='szEmailAddress'>Email address to resolve</param>
/// <param name='sbEID'>[out]SBinary EID to return</param>
/// <returns>HRESULT</returns>
/// <remarks>
/// <para name='Notes'></para>
/// <para name='Author'>Kenn Guilstorf</para>
/// <para name='LastModified'>28Jan2016</para>
/// </remarks>
STDMETHODIMP ZybraxiSoft::CMailbox::GetEID(const TSTRING sEmailAddress, SBinary &sbEID)
{
	FBEG;
	HRESULT hr = S_OK;
	LPMDB lpDefaultMDB = NULL;
	LPEXCHANGEMANAGESTORE lpExStore = NULL;
	TSTRING sServerName;
	TSTRING sServerPre = TSTRING(SERVER_PRE);
	TSTRING sServerPost = TSTRING(SERVER_POST);
	TSTRING sServerDN = wstring();
	std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
	string sTempEmailAddress;
	string sTempServerDN;


	// Clean our sbEID
	sbEID.cb = 0;

	// Get a pointer to the default message store
	if (FAILED(hr = OpenDefaultMessageStore(
		&lpDefaultMDB)))
	{
		ERROR_MSG_W_HR("OpenDefaultMessageStore failed", hr);
		goto CLEANUP;
	}

	// Use the pointer to the default message store to
	// Get a pointer to the Manage Store interface
	if (FAILED(hr = lpDefaultMDB->QueryInterface(
		IID_IExchangeManageStore,
		(LPVOID*)&lpExStore)))
	{
		ERROR_MSG_W_HR("Getting the Exchange store manager failed", hr);
		goto CLEANUP;
	}

	// Get our Server Name
	if (FAILED(hr = this->GetServerNameFromProfile(sServerName)))
	{
		ERROR_MSG_W_HR("Getting the server name failed", hr);
		goto CLEANUP;
	}

	// Go ahead and build our server dn
	sServerDN.append(sServerPre.c_str());
	sServerDN.append(sServerName.c_str());
	sServerDN.append(sServerPost.c_str());

	// if we're in UNICODE, we need to convert; we can only pass ASCII
#if defined(UNICODE) || defined(_UNICODE)
	sTempServerDN = converter.to_bytes(sServerDN);
	sTempEmailAddress = converter.to_bytes(sEmailAddress);
#else
	sTempServerDN = sServerDN;
	sTempEmailAddress = sEmailAddress;
#endif

	// Get the EID
	if (FAILED(hr = lpExStore->CreateStoreEntryID(
		(LPSTR)sTempServerDN.c_str(),
		(LPSTR)sTempEmailAddress.c_str(),
		OPENSTORE_TAKE_OWNERSHIP,
		&sbEID.cb,
		(LPENTRYID*)&sbEID.lpb)))
	{
		ERROR_MSG_W_HR("CreateStoreEntryID failed", hr);
		goto CLEANUP;
	}

	// Check to make sure we got an actual EID
	if (sbEID.cb == 0)
	{
		hr = MAPI_E_NOT_FOUND;
		ERROR_MSG_W_HR("Failed to resolve the mailbox", hr);
		goto CLEANUP;
	}
	
CLEANUP:
	if (lpExStore)
	{
		lpExStore->Release();
		lpExStore = NULL;
	}

	if (lpDefaultMDB)
	{
		lpDefaultMDB->Release();
		lpDefaultMDB = NULL;
	}

	FEND;
	return hr;
}