diff ui/processhelp_win.cpp @ 670:175370634226

Move getProcessOwner to util and use it to skip the current user in locate other hives
author Andre Heinecke <andre.heinecke@intevation.de>
date Fri, 27 Jun 2014 10:27:08 +0200
parents 81a44b93229e
children 20ca94680003
line wrap: on
line diff
--- a/ui/processhelp_win.cpp	Thu Jun 26 17:42:52 2014 +0200
+++ b/ui/processhelp_win.cpp	Fri Jun 27 10:27:08 2014 +0200
@@ -8,6 +8,7 @@
 #ifdef WIN32
 #include "processhelp.h"
 #include "strhelp.h"
+#include "util.h"
 
 #include <windows.h>
 #include <tlhelp32.h>
@@ -22,43 +23,6 @@
     HWND windowId;
 };
 
-PSID copySid(PSID from)
-{
-    if (!from) {
-        return 0;
-    }
-
-    int sidLength = GetLengthSid(from);
-    PSID to = (PSID) xmalloc(sidLength);
-    CopySid(sidLength, to, from);
-    return to;
-}
-
-static PSID getProcessOwner(HANDLE hProcess)
-{
-    HANDLE hToken = NULL;
-    PSID sid;
-
-    OpenProcessToken(hProcess, TOKEN_READ, &hToken);
-    if (hToken) {
-        DWORD size;
-        PTOKEN_USER userStruct;
-
-        // check how much space is needed
-        GetTokenInformation(hToken, TokenUser, NULL, 0, &size);
-        if (ERROR_INSUFFICIENT_BUFFER == GetLastError()) {
-            userStruct = reinterpret_cast<PTOKEN_USER>(new BYTE[size]);
-            GetTokenInformation(hToken, TokenUser, userStruct, size, &size);
-
-            sid = copySid(userStruct->User.Sid);
-            CloseHandle(hToken);
-            delete [] userStruct;
-            return sid;
-        }
-    }
-    return 0;
-}
-
 BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
 {
     if (GetWindowLong(hwnd, GWL_STYLE) & WS_VISIBLE) {
@@ -74,7 +38,6 @@
     return TRUE;
 }
 
-
 static HANDLE getProcessHandle(int processID)
 {
     return OpenProcess(SYNCHRONIZE |
@@ -108,7 +71,7 @@
     do {
         const QString exeFile = QString::fromWCharArray(pe32.szExeFile).toLower();
         if (exeFile == processNameLower || exeFile == processNameExe) {
-            PSID user_sid = getProcessOwner(GetCurrentProcess());
+            PSID user_sid = get_process_owner(GetCurrentProcess());
             if (user_sid) {
                 // Also check that we are the owner of that process
                 HANDLE hProcess = getProcessHandle(pe32.th32ProcessID);
@@ -116,8 +79,8 @@
                     continue;
                 }
 
-                PSID sid = getProcessOwner(hProcess);
-                PSID userSid = getProcessOwner(GetCurrentProcess());
+                PSID sid = get_process_owner(hProcess);
+                PSID userSid = get_process_owner(GetCurrentProcess());
                 if (!sid || (userSid && !EqualSid(userSid, sid))) {
                     free(sid);
                     continue;

http://wald.intevation.org/projects/trustbridge/