comparison common/util.c @ 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 c7a35fa302ec
children d4766b4922c9
comparison
equal deleted inserted replaced
669:7147550ee15d 670:175370634226
17 #include <string.h> 17 #include <string.h>
18 #else 18 #else
19 #include <windows.h> 19 #include <windows.h>
20 #endif 20 #endif
21 21
22 static PSID
23 copy_sid(PSID from)
24 {
25 if (!from)
26 {
27 return 0;
28 }
29
30 DWORD sidLength = GetLengthSid(from);
31 PSID to = (PSID) xmalloc(sidLength);
32 CopySid(sidLength, to, from);
33 return to;
34 }
35
36
37 PSID
38 get_process_owner(HANDLE hProcess)
39 {
40 HANDLE hToken = NULL;
41 PSID sid;
42
43 if (hProcess == NULL)
44 {
45 ERRORPRINTF ("invalid call to get_process_owner");
46 return NULL;
47 }
48
49 OpenProcessToken(hProcess, TOKEN_READ, &hToken);
50 if (hToken)
51 {
52 DWORD size = 0;
53 PTOKEN_USER userStruct;
54
55 // check how much space is needed
56 GetTokenInformation(hToken, TokenUser, NULL, 0, &size);
57 if (ERROR_INSUFFICIENT_BUFFER == GetLastError())
58 {
59 userStruct = (PTOKEN_USER) xmalloc (size);
60 GetTokenInformation(hToken, TokenUser, userStruct, size, &size);
61
62 sid = copy_sid(userStruct->User.Sid);
63 CloseHandle(hToken);
64 xfree (userStruct);
65 return sid;
66 }
67 }
68 return NULL;
69 }
70
22 bool 71 bool
23 is_elevated() 72 is_elevated()
24 { 73 {
25 bool ret = false; 74 bool ret = false;
26 #ifndef _WIN32 75 #ifndef _WIN32

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