📄 utils.cpp
字号:
goto Done;
} else {
//TRACEMSG(ZONE_PRINTQUEUE, (L"SMB-MEMMAP: Got file mapping at address 0x%x for %d bytes", (UINT)pRet, m_uiInc));
//
// Touch the memory just to verify that its okay
__try {
memset(pRet, 0, m_uiInc);
} __except(1) {
TRACEMSG(ZONE_PRINTQUEUE, (L"SMB-MEMMAP: touching memory failed: %d used -- inc %d", m_uiUsed, m_uiInc));
ASSERT(FALSE);
pRet = NULL;
goto Done;
}
}
m_uiUsed += m_uiInc;
}
Done:
//
// If we succeed, inc the outstanding count
if(pRet) {
m_uiBlocksOutstanding ++;
*pBlockSize = m_uiInc;
} else {
TRACEMSG(ZONE_ERROR, (L"SMB-MEMMAP: Out of memory in spool swap file. failing request"));
}
ASSERT((m_uiBlocksOutstanding * m_uiInc) <= m_uiMaxSize);
LeaveCriticalSection(&myLock);
return pRet;
}
VOID
MemMappedBuffer::Return(VOID *pRet)
{
CCritSection csLock(&myLock);
csLock.Lock();
//
// If we havent been inited we shouldnd be called! error out!
if(FALSE == m_fInited) {
ASSERT(FALSE);
goto Done;
}
//
// Handle the base case of returning (rechaining)
*((UINT *)pRet) = (UINT)m_pNext;
m_pNext = pRet;
m_uiBlocksOutstanding --;
//
// If there are no blocks outstanding, we need to truncate the file
if(0 == m_uiBlocksOutstanding) {
VOID *pToFree = m_pNext;
//
// Unmap everything
while(pToFree) {
VOID *pNext = (VOID *)(*(UINT *)pToFree);
//
// Unmap the file view for this one node
if(0 == UnmapViewOfFile(pToFree)) {
TRACEMSG(ZONE_PRINTQUEUE, (L"SMB-MEMMAP: Couldnt unmap a file view(0x%x)... this is bad and is an internal error: %d", (UINT)pToFree, GetLastError()));
ASSERT(FALSE);
} else {
TRACEMSG(ZONE_PRINTQUEUE, (L"SMB-MEMMAP: unmapped a file view(0x%x)...", (UINT)pToFree));
}
//
// Get the next pointer
pToFree = pNext;
}
m_pNext = NULL;
//
// Set the end of the file to be the beginning.
/*if(0xFFFFFFFF == ::SetFilePointer(m_hFile, 0, NULL, FILE_BEGIN)) {
if(ERROR_SUCCESS != GetLastError()) {
RETAILMSG(1, (L"SMB-MEMMAP: Setting file pointer failed while truncating file-- GetLastError(%d)", GetLastError()));
ASSERT(FALSE);
goto Done;
}
}
//
// Do the set file operation to truncate
if(0 == SetEndOfFile(m_hFile)) {
RETAILMSG(1, (L"SMB-MEMMAP: Setting end of file failed while truncating file -- GetLastError(%d)", GetLastError()));
ASSERT(FALSE);
goto Done;
}*/
m_uiUsed = 0;
ASSERT(NULL == m_pNext);
}
Done:
;
}
BOOL VerifyPassword(ce::smart_ptr<ActiveConnection> pMyConnection, BYTE *pBlob, UINT uiBlobLen)
{
char p21[21];
char p24[24];
CReg RegPassword;
WCHAR wcPassPath[MAX_PATH];
DWORD dwRegType;
DWORD dwSizeNeeded = 0;
BYTE TempBlob[1024];
BYTE *pBlob2 = TempBlob;
WCHAR *pCryptString = NULL;
BYTE *pChallenge = NULL;
DATA_BLOB dataIn = {0, NULL};
DATA_BLOB dataOut = {0, NULL};
BOOL fAllowAccess = FALSE;
if(sizeof(p24) != uiBlobLen) {
TRACEMSG(ZONE_SECURITY, (L"SMBSRV Verify password: Error -- invalid password len given (len %d)!!", uiBlobLen));
goto Done;
}
swprintf(wcPassPath, L"Comm\\Security\\UserAccounts\\%s", pMyConnection->UserName());
if(FALSE == RegPassword.Open(HKEY_LOCAL_MACHINE, wcPassPath)) {
RETAILMSG(1, (L"SMBSRV Verify password: Error -- invalid user name %s!!", pMyConnection->UserName()));
goto Done;
}
//
// See how much data we need for the blob
if(ERROR_SUCCESS != RegQueryValueEx(RegPassword, L"LM", NULL, &dwRegType, NULL, &dwSizeNeeded)) {
RETAILMSG(1, (L"SMBSRV Verify password: Error -- invalid user name %s!!", pMyConnection->UserName()));
goto Done;
}
//
// If we need more than we've got, request it
if(dwSizeNeeded > sizeof(TempBlob)) {
pBlob2 = new BYTE[dwSizeNeeded];
if(NULL == pBlob2) {
goto Done;
}
}
//
// Fetch our blob
if(ERROR_SUCCESS != RegQueryValueEx(RegPassword, L"LM", NULL, &dwRegType, pBlob2, &dwSizeNeeded)) {
RETAILMSG(1, (L"SMBSRV Verify password: Error -- invalid user name %s!!", pMyConnection->UserName()));
goto Done;
}
//
// Decrypt it
dataIn.cbData = dwSizeNeeded;
dataIn.pbData = pBlob2;
if(FALSE == CryptUnprotectData(&dataIn, &pCryptString, NULL, NULL, NULL, CRYPTPROTECT_SYSTEM, &dataOut)) {
RETAILMSG(1, (L"SMBSRV Verify password: Error, cant unprotect hash -- invalid user name %s!!", pMyConnection->UserName()));
goto Done;
}
//
// Verify the blobs are okay
if(0 != _wcsicmp(pCryptString, pMyConnection->UserName())) {
RETAILMSG(1, (L"SMBSRV Verify password: Error, cant protected names differ! %s %s!!", pMyConnection->UserName(), pCryptString));
goto Done;
}
//
// Fetch our challenge from the connection manager
if(FAILED(SMB_Globals::g_pConnectionManager->FindChallenge(pMyConnection->ConnectionID(), &pChallenge))) {
TRACEMSG(ZONE_SECURITY, (L"SMBSRV couldnt find challeinge for our connection! CID: %d!!", pMyConnection->ConnectionID()));
ASSERT(FALSE);
goto Done;
}
//
// Verify the security here. -- make sure their password aligns with
// what we expect
memset(p21, 0, sizeof(p21));
memcpy(p21, dataOut.pbData, dataOut.cbData);
Encrypt(p21, (CHAR *)pChallenge, p24, sizeof(p24));
if(0 == memcmp(p24, pBlob, sizeof(p24))) {
fAllowAccess = TRUE;
}
Done:
if(pBlob2 != TempBlob) {
delete [] pBlob2;
}
if(NULL != pCryptString) {
LocalFree(pCryptString);
}
if(NULL != dataOut.pbData) {
LocalFree(dataOut.pbData);
}
return fAllowAccess;
}
HRESULT ConvertWildCard(const WCHAR *pWild, StringConverter *pNew, BOOL fUnicodeRules)
{
PREFAST_ASSERT(NULL != pWild);
PREFAST_ASSERT(NULL != pNew);
UINT uiWildLen = wcslen(pWild);
WCHAR *pWildTmp = NULL;
HRESULT hr = E_FAIL;
//
// We cant be 0 (that would mean there is nothing here !?!?)
if(0 == uiWildLen) {
ASSERT(FALSE);
hr = E_FAIL;
goto Done;
}
//
// Copy the passed in string
if(FAILED(pNew->Clear()) || FAILED(pNew->append(pWild))) {
ASSERT(FALSE);
hr = E_FAIL;
goto Done;
}
//
// First pass on unicode rules, do conversion (per spec in 3.4 of cifs9f.doc)
// ? translates to >
// , translates to " if not followed by ? or *
// * translates to < if not followed by .
pWildTmp = pNew->GetUnsafeString() + uiWildLen - 1;
if(TRUE == fUnicodeRules) {
while(pWildTmp > pNew->GetUnsafeString()) {
WCHAR wThis = *pWildTmp;
WCHAR wNext = *(pWildTmp + 1); //this is safe b/c we are null terminated
if(wThis == '>') {
*pWildTmp = '?';
}
else if (wThis == '"' && (wNext == '?' || wNext == '*')) {
*pWildTmp = '.';
}
else if (wThis == '<' && wNext == '.') {
*pWildTmp = '*';
}
pWildTmp --;
}
}
//
// Success
hr = S_OK;
Done:
return hr;
}
HRESULT FileTimeToSMBTime(const FILETIME *pFT, SMB_TIME *pSMBTime, SMB_DATE *pSMBDate)
{
SYSTEMTIME st;
HRESULT hr;
if(0 == pFT->dwHighDateTime && 0 == pFT->dwLowDateTime) {
pSMBTime->Hours = 0;
pSMBTime->Minutes = 0;
pSMBTime->TwoSeconds = 0;
pSMBDate->Day = 0;
pSMBDate->Month = 0;
pSMBDate->Year = 0;
hr = S_OK;
goto Done;
}
if(0 == FileTimeToSystemTime(pFT, &st)) {
ASSERT(FALSE);
hr = E_FAIL;
goto Done;
}
//
// Do the time
pSMBTime->Hours = st.wHour;
pSMBTime->Minutes = st.wMinute;
pSMBTime->TwoSeconds = (st.wSecond / 2);
//
// Do the DATE
ASSERT(st.wYear >= 1980);
pSMBDate->Day = st.wDay;
pSMBDate->Month = st.wMonth;
pSMBDate->Year = st.wYear - 1980;
//
// Success
hr = S_OK;
Done:
return hr;
}
BOOL MatchesWildcard(DWORD len, LPCWSTR lpWild, DWORD len2, LPCWSTR lpFile)
{
while (len && len2) {
if (*lpWild == L'*') {
lpWild++;
len--;
if ((len >= 2) && (lpWild[0] == L'.') && (lpWild[1] == L'*')) {
len -= 2;
lpWild += 2;
}
if (len) {
while (len2) {
if (MatchesWildcard(len, lpWild, len2--, lpFile++))
return TRUE;
}
return FALSE;
}
return TRUE;
} else if ((*lpWild != L'?') && _wcsnicmp(lpWild, lpFile, 1)) {
return FALSE;
}
len--;
lpWild++;
len2--;
lpFile++;
}
if (!len && !len2)
return TRUE;
if (!len)
return FALSE;
while (len--) {
if (*lpWild++ != L'*')
return FALSE;
if ((len >= 2) && (lpWild[0] == L'.') && (lpWild[1] == L'*')) {
len -= 2;
lpWild += 2;
}
}
return TRUE;
}
//
//
// WakeUpOnEvent -- you set a handle, when that handle is Signeled, you get called
//
//
WakeUpOnEvent::WakeUpOnEvent()
{
InitializeCriticalSection(&m_csLock);
m_hWakeUpEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
m_hWakeUpThread = NULL;
m_fStop = FALSE;
}
WakeUpOnEvent::~WakeUpOnEvent()
{
this->m_fStop = TRUE;
WakeUp();
WaitForSingleObject(m_hWakeUpThread, INFINITE);
DeleteCriticalSection(&m_csLock);
CloseHandle(m_hWakeUpEvent);
CloseHandle(m_hWakeUpThread);
ASSERT(0 == m_HandList.size());
}
HRESULT
WakeUpOnEvent::AddEvent(HANDLE h, WakeUpNode *pNode, USHORT *pID)
{
HRESULT hr = E_FAIL;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -