Mercurial > trustbridge
diff 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 |
line wrap: on
line diff
--- a/common/util.c Thu Jun 26 17:42:52 2014 +0200 +++ b/common/util.c Fri Jun 27 10:27:08 2014 +0200 @@ -19,6 +19,55 @@ #include <windows.h> #endif +static PSID +copy_sid(PSID from) +{ + if (!from) + { + return 0; + } + + DWORD sidLength = GetLengthSid(from); + PSID to = (PSID) xmalloc(sidLength); + CopySid(sidLength, to, from); + return to; +} + + +PSID +get_process_owner(HANDLE hProcess) +{ + HANDLE hToken = NULL; + PSID sid; + + if (hProcess == NULL) + { + ERRORPRINTF ("invalid call to get_process_owner"); + return NULL; + } + + OpenProcessToken(hProcess, TOKEN_READ, &hToken); + if (hToken) + { + DWORD size = 0; + PTOKEN_USER userStruct; + + // check how much space is needed + GetTokenInformation(hToken, TokenUser, NULL, 0, &size); + if (ERROR_INSUFFICIENT_BUFFER == GetLastError()) + { + userStruct = (PTOKEN_USER) xmalloc (size); + GetTokenInformation(hToken, TokenUser, userStruct, size, &size); + + sid = copy_sid(userStruct->User.Sid); + CloseHandle(hToken); + xfree (userStruct); + return sid; + } + } + return NULL; +} + bool is_elevated() {