diff options
Diffstat (limited to 'src/Common/Dlgcode.c')
-rw-r--r-- | src/Common/Dlgcode.c | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/src/Common/Dlgcode.c b/src/Common/Dlgcode.c index 7b3d2d45..2c707f5d 100644 --- a/src/Common/Dlgcode.c +++ b/src/Common/Dlgcode.c @@ -14515,71 +14515,74 @@ static bool RunAsDesktopUser( { HANDLE hThreadToken = NULL, hShellProcess = NULL, hShellProcessToken = NULL, hPrimaryToken = NULL; HWND hwnd = NULL; DWORD dwPID = 0; BOOL ret; DWORD dwLastErr; STARTUPINFOW si; PROCESS_INFORMATION pi; bool retval = false; SecureZeroMemory(&si, sizeof(si)); SecureZeroMemory(&pi, sizeof(pi)); si.cb = sizeof(si); // locate CreateProcessWithTokenW in Advapi32.dll if (!CreateProcessWithTokenWPtr) { return false; } if (!ImpersonateSelf (SecurityImpersonation)) { return false; } if (!OpenThreadToken (GetCurrentThread(), TOKEN_ADJUST_PRIVILEGES, TRUE, &hThreadToken)) { return false; } else { TOKEN_PRIVILEGES tkp; tkp.PrivilegeCount = 1; LookupPrivilegeValueW(NULL, SE_INCREASE_QUOTA_NAME, &tkp.Privileges[0].Luid); tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; - SetThreadToken (NULL, NULL); + if (!SetThreadToken(NULL, NULL)) + { + goto cleanup; + } AdjustTokenPrivileges(hThreadToken, FALSE, &tkp, 0, NULL, NULL); dwLastErr = GetLastError(); if (ERROR_SUCCESS != dwLastErr) { goto cleanup; } } // From this point down, we have handles to close, so make sure to clean up. // Get an HWND representing the desktop shell. // CAVEATS: This will fail if the shell is not running (crashed or terminated), or the default shell has been // replaced with a custom shell. This also won't return what you probably want if Explorer has been terminated and // restarted elevated. hwnd = GetShellWindow(); if (NULL == hwnd) { dwLastErr = GetLastError(); goto cleanup; } // Get the PID of the desktop shell process. GetWindowThreadProcessId(hwnd, &dwPID); if (0 == dwPID) { dwLastErr = GetLastError(); goto cleanup; } // Open the desktop shell process in order to query it (get the token) hShellProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, dwPID); if (!hShellProcess) { dwLastErr = GetLastError(); @@ -14601,71 +14604,74 @@ static bool RunAsDesktopUser( if (!ret) { dwLastErr = GetLastError(); goto cleanup; } // Start the target process with the new token. ret = CreateProcessWithTokenWPtr( hPrimaryToken, 0, szApp, szCmdLine, 0, NULL, NULL, &si, &pi); if (!ret) { dwLastErr = GetLastError(); goto cleanup; } // Make sure to close HANDLEs return in the PROCESS_INFORMATION. CloseHandle(pi.hProcess); CloseHandle(pi.hThread); retval = true; cleanup: // Clean up resources if (hShellProcessToken) CloseHandle(hShellProcessToken); if (hPrimaryToken) CloseHandle(hPrimaryToken); if (hShellProcess) CloseHandle(hShellProcess); if (hThreadToken) CloseHandle(hThreadToken); - RevertToSelf (); + + if (!RevertToSelf()) + return false; + if (!retval) SetLastError (dwLastErr); return retval; } // This function checks if the process is running with elevated privileges or not BOOL IsElevated() { DWORD dwSize = 0; HANDLE hToken = NULL; TOKEN_ELEVATION tokenInformation; BOOL bReturn = FALSE; if(OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken)) { if(GetTokenInformation(hToken, TokenElevation, &tokenInformation, sizeof(TOKEN_ELEVATION), &dwSize)) { if (tokenInformation.TokenIsElevated) bReturn = TRUE; } CloseHandle(hToken); } return bReturn; } // Based on code from: // https://github.com/microsoft/Windows-classic-samples/blob/main/Samples/Win7Samples/winui/shell/appplatform/ExecInExplorer/ExecInExplorer.cpp HRESULT GetShellViewForDesktop(REFIID riid, void **ppv) { *ppv = NULL; IShellWindows *psw; HRESULT hr = CoCreateInstance(CLSID_ShellWindows, NULL, CLSCTX_LOCAL_SERVER, IID_PPV_ARGS(&psw)); if (SUCCEEDED(hr)) @@ -15009,71 +15015,71 @@ HRESULT GenericDropTarget::QueryInterface(REFIID iid, void **ppvObject) } // compare guids fast and dirty if (IsEqualGUID (iid, IID_IDropTarget)) { AddRef(); (*ppvObject) = this; return S_OK; } return E_FAIL; } ULONG GenericDropTarget::AddRef(void) { return (ULONG) InterlockedIncrement (&m_dwRefCount); } ULONG GenericDropTarget::Release(void) { if (InterlockedDecrement (&m_dwRefCount) == 0) { delete this; return 0; } else return (ULONG) m_dwRefCount; } //************************************************************* // Register // Called by whom implements us so we can serve //************************************************************* BOOL GenericDropTarget::Register(HWND hWnd) { if(NULL == hWnd) - return E_FAIL; + return FALSE; OleInitialize(NULL); // required: these MUST be strong locked CoLockObjectExternal(this, TRUE, 0); // this is ok, we have it DWORD hRes = ::RegisterDragDrop(hWnd, this); if(SUCCEEDED(hRes)) { // keep m_DropTargetWnd = hWnd; return TRUE; } // unlock CoLockObjectExternal(this, FALSE, 0); // bye bye COM OleUninitialize(); // wont accept data now return FALSE; } //************************************************************* // Revoke // Unregister us as a target //************************************************************* void GenericDropTarget::Revoke() { if(NULL == m_DropTargetWnd) return; RevokeDragDrop(m_DropTargetWnd); @@ -15230,96 +15236,96 @@ DWORD GenericDropTarget::GotEnter(void) // PasswordEditDropTarget // Constructor // ************************************************************ PasswordEditDropTarget::PasswordEditDropTarget() : GenericDropTarget (g_supportedFormats, ARRAYSIZE (g_supportedFormats)) { } // ************************************************************ // GotDrag // ************************************************************ DWORD PasswordEditDropTarget::GotDrag(void) { return GotEnter(); } // ************************************************************ // GotLeave // ************************************************************ void PasswordEditDropTarget::GotLeave(void) { } // ************************************************************ // GotEnter // ************************************************************ DWORD PasswordEditDropTarget::GotEnter(void) { TCHAR szClassName[64]; DWORD dwStyles; int maxLen; HWND hChild = WindowFromPoint (m_DropPoint); // check that we are on password edit control (we use maximum length to correctly identify password fields since they don't always have ES_PASSWORD style (if the the user checked show password) if (hChild && GetClassName (hChild, szClassName, ARRAYSIZE (szClassName)) && (0 == _tcsicmp (szClassName, _T("EDIT"))) - && (dwStyles = GetWindowLong (hChild, GWL_STYLE)) && !(dwStyles & ES_NUMBER) + && (dwStyles = GetWindowLongPtr (hChild, GWL_STYLE)) && !(dwStyles & ES_NUMBER) && (maxLen = (int) SendMessage (hChild, EM_GETLIMITTEXT, 0, 0)) && (maxLen == MAX_PASSWORD || maxLen == MAX_LEGACY_PASSWORD) ) { return DROPEFFECT_COPY; } return DROPEFFECT_LINK; } // ************************************************************ // GotDrop // Called if we have a drop text drop here. // // ************************************************************ void PasswordEditDropTarget::GotDrop(CLIPFORMAT format) { // value contains the material itself if(m_Data) { TCHAR szClassName[64]; DWORD dwStyles; int maxLen; HWND hChild = WindowFromPoint (m_DropPoint); if (hChild && GetClassName (hChild, szClassName, ARRAYSIZE (szClassName)) && (0 == _tcsicmp (szClassName, _T("EDIT"))) - && (dwStyles = GetWindowLong (hChild, GWL_STYLE)) && !(dwStyles & ES_NUMBER) + && (dwStyles = GetWindowLongPtr (hChild, GWL_STYLE)) && !(dwStyles & ES_NUMBER) && (maxLen = (int) SendMessage (hChild, EM_GETLIMITTEXT, 0, 0)) && (maxLen == MAX_PASSWORD || maxLen == MAX_LEGACY_PASSWORD) ) { WCHAR* wszText; int wlen; bool bFree = false; // get the text if (format == CF_UNICODETEXT) { wszText = (WCHAR *)m_Data; } else { char *iText = (char *)m_Data; wlen = MultiByteToWideChar ((format == CF_OEMTEXT)? CP_OEMCP : CP_ACP, 0, iText, -1, NULL, 0); wszText = new WCHAR[wlen]; if (wszText) { wlen = MultiByteToWideChar (CP_ACP, 0, iText, -1, wszText, wlen); bFree = true; } } WCHAR* pchData = wszText; int txtlen = 0; bool bTruncated = false; // remove any appended \r or \n while (*pchData) { if (*pchData == '\r' || *pchData == '\n') break; else { txtlen++; |