UserInfo DLL to get user group and name
git-svn-id: https://svn.code.sf.net/p/nsis/code/NSIS/trunk@1585 212acab6-be3b-0410-9dea-997c60f758d6
This commit is contained in:
parent
ae3de07d72
commit
4541bb7cc8
5 changed files with 259 additions and 0 deletions
108
Contrib/UserInfo/UserInfo.c
Normal file
108
Contrib/UserInfo/UserInfo.c
Normal file
|
@ -0,0 +1,108 @@
|
|||
#include <windows.h>
|
||||
#include "..\exdll\exdll.h"
|
||||
|
||||
void __declspec(dllexport) GetName(HWND hwndParent, int string_size,
|
||||
char *variables, stack_t **stacktop)
|
||||
{
|
||||
EXDLL_INIT();
|
||||
|
||||
{
|
||||
DWORD dwStringSize = g_stringsize;
|
||||
stack_t *th;
|
||||
if (!g_stacktop) return;
|
||||
th = (stack_t*) GlobalAlloc(GPTR, sizeof(stack_t) + g_stringsize);
|
||||
GetUserName(th->text, &dwStringSize);
|
||||
th->next = *g_stacktop;
|
||||
*g_stacktop = th;
|
||||
}
|
||||
}
|
||||
|
||||
void __declspec(dllexport) GetGroup(HWND hwndParent, int string_size,
|
||||
char *variables, stack_t **stacktop)
|
||||
{
|
||||
EXDLL_INIT();
|
||||
|
||||
{
|
||||
HANDLE hThread;
|
||||
TOKEN_GROUPS *ptg = NULL;
|
||||
DWORD cbTokenGroups;
|
||||
DWORD i, j;
|
||||
|
||||
SID_IDENTIFIER_AUTHORITY SystemSidAuthority = SECURITY_NT_AUTHORITY;
|
||||
|
||||
char *group = "";
|
||||
|
||||
// First we must open a handle to the access token for this thread.
|
||||
|
||||
if (OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, FALSE, &hThread) ||
|
||||
OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hThread))
|
||||
{
|
||||
// Then we must query the size of the group information associated with
|
||||
// the token. Note that we expect a FALSE result from GetTokenInformation
|
||||
// because we've given it a NULL buffer. On exit cbTokenGroups will tell
|
||||
// the size of the group information.
|
||||
|
||||
if (!GetTokenInformation (hThread, TokenGroups, NULL, 0, &cbTokenGroups) &&
|
||||
GetLastError() == ERROR_INSUFFICIENT_BUFFER)
|
||||
{
|
||||
|
||||
// Now we allocate a buffer for the group information.
|
||||
// Since _alloca allocates on the stack, we don't have
|
||||
// to explicitly deallocate it. That happens automatically
|
||||
// when we exit this function.
|
||||
|
||||
if (ptg = GlobalAlloc(GPTR, cbTokenGroups))
|
||||
{
|
||||
|
||||
// Now we ask for the group information again.
|
||||
// This may fail if an administrator has added this account
|
||||
// to an additional group between our first call to
|
||||
// GetTokenInformation and this one.
|
||||
|
||||
if (GetTokenInformation(hThread, TokenGroups, ptg, cbTokenGroups, &cbTokenGroups))
|
||||
{
|
||||
|
||||
struct group
|
||||
{
|
||||
DWORD auth_id;
|
||||
char *name;
|
||||
} groups[] = {
|
||||
{DOMAIN_ALIAS_RID_GUESTS, "Guest"},
|
||||
{DOMAIN_ALIAS_RID_USERS, "User"},
|
||||
{DOMAIN_ALIAS_RID_POWER_USERS, "Power"},
|
||||
{DOMAIN_ALIAS_RID_ADMINS, "Admin"}
|
||||
};
|
||||
|
||||
// Finally we'll iterate through the list of groups for this access
|
||||
// token looking for a match against the SID we created above.
|
||||
|
||||
for (i = 0; i < sizeof(groups)/sizeof(struct group); i++)
|
||||
{
|
||||
PSID psid = 0;
|
||||
AllocateAndInitializeSid(
|
||||
&SystemSidAuthority,
|
||||
2,
|
||||
SECURITY_BUILTIN_DOMAIN_RID,
|
||||
groups[i].auth_id,
|
||||
0, 0, 0, 0, 0, 0,
|
||||
&psid
|
||||
);
|
||||
if (psid == 0) continue;
|
||||
for (j = 0; j < ptg->GroupCount; j++)
|
||||
if (EqualSid(ptg->Groups[j].Sid, psid))
|
||||
group = groups[i].name;
|
||||
FreeSid(psid);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pushstring(group);
|
||||
}
|
||||
}
|
||||
|
||||
BOOL WINAPI _DllMainCRTStartup(HANDLE hInst, ULONG ul_reason_for_call, LPVOID lpReserved)
|
||||
{
|
||||
return TRUE;
|
||||
}
|
112
Contrib/UserInfo/UserInfo.dsp
Normal file
112
Contrib/UserInfo/UserInfo.dsp
Normal file
|
@ -0,0 +1,112 @@
|
|||
# Microsoft Developer Studio Project File - Name="UserInfo" - Package Owner=<4>
|
||||
# Microsoft Developer Studio Generated Build File, Format Version 6.00
|
||||
# ** DO NOT EDIT **
|
||||
|
||||
# TARGTYPE "Win32 (x86) Dynamic-Link Library" 0x0102
|
||||
|
||||
CFG=UserInfo - Win32 Debug
|
||||
!MESSAGE This is not a valid makefile. To build this project using NMAKE,
|
||||
!MESSAGE use the Export Makefile command and run
|
||||
!MESSAGE
|
||||
!MESSAGE NMAKE /f "UserInfo.mak".
|
||||
!MESSAGE
|
||||
!MESSAGE You can specify a configuration when running NMAKE
|
||||
!MESSAGE by defining the macro CFG on the command line. For example:
|
||||
!MESSAGE
|
||||
!MESSAGE NMAKE /f "UserInfo.mak" CFG="UserInfo - Win32 Debug"
|
||||
!MESSAGE
|
||||
!MESSAGE Possible choices for configuration are:
|
||||
!MESSAGE
|
||||
!MESSAGE "UserInfo - Win32 Release" (based on "Win32 (x86) Dynamic-Link Library")
|
||||
!MESSAGE "UserInfo - Win32 Debug" (based on "Win32 (x86) Dynamic-Link Library")
|
||||
!MESSAGE
|
||||
|
||||
# Begin Project
|
||||
# PROP AllowPerConfigDependencies 0
|
||||
# PROP Scc_ProjName ""
|
||||
# PROP Scc_LocalPath ""
|
||||
CPP=cl.exe
|
||||
MTL=midl.exe
|
||||
RSC=rc.exe
|
||||
|
||||
!IF "$(CFG)" == "UserInfo - Win32 Release"
|
||||
|
||||
# PROP BASE Use_MFC 0
|
||||
# PROP BASE Use_Debug_Libraries 0
|
||||
# PROP BASE Output_Dir "Release"
|
||||
# PROP BASE Intermediate_Dir "Release"
|
||||
# PROP BASE Target_Dir ""
|
||||
# PROP Use_MFC 0
|
||||
# PROP Use_Debug_Libraries 0
|
||||
# PROP Output_Dir "Release"
|
||||
# PROP Intermediate_Dir "Release"
|
||||
# PROP Ignore_Export_Lib 0
|
||||
# PROP Target_Dir ""
|
||||
# ADD BASE CPP /nologo /MT /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "UserInfo_EXPORTS" /YX /FD /c
|
||||
# ADD CPP /nologo /MT /W3 /GX /O1 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "UserInfo_EXPORTS" /FD /c
|
||||
# SUBTRACT CPP /YX
|
||||
# ADD BASE MTL /nologo /D "NDEBUG" /mktyplib203 /win32
|
||||
# ADD MTL /nologo /D "NDEBUG" /mktyplib203 /win32
|
||||
# ADD BASE RSC /l 0x409 /d "NDEBUG"
|
||||
# ADD RSC /l 0x409 /d "NDEBUG"
|
||||
BSC32=bscmake.exe
|
||||
# ADD BASE BSC32 /nologo
|
||||
# ADD BSC32 /nologo
|
||||
LINK32=link.exe
|
||||
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /machine:I386
|
||||
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /entry:"_DllMainCRTStartup" /dll /machine:I386 /nodefaultlib /out:"../../Plugins/UserInfo.dll" /opt:nowin98
|
||||
# SUBTRACT LINK32 /pdb:none
|
||||
|
||||
!ELSEIF "$(CFG)" == "UserInfo - Win32 Debug"
|
||||
|
||||
# PROP BASE Use_MFC 0
|
||||
# PROP BASE Use_Debug_Libraries 1
|
||||
# PROP BASE Output_Dir "Debug"
|
||||
# PROP BASE Intermediate_Dir "Debug"
|
||||
# PROP BASE Target_Dir ""
|
||||
# PROP Use_MFC 0
|
||||
# PROP Use_Debug_Libraries 1
|
||||
# PROP Output_Dir "Debug"
|
||||
# PROP Intermediate_Dir "Debug"
|
||||
# PROP Target_Dir ""
|
||||
# ADD BASE CPP /nologo /MTd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "UserInfo_EXPORTS" /YX /FD /GZ /c
|
||||
# ADD CPP /nologo /MTd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "UserInfo_EXPORTS" /YX /FD /GZ /c
|
||||
# ADD BASE MTL /nologo /D "_DEBUG" /mktyplib203 /win32
|
||||
# ADD MTL /nologo /D "_DEBUG" /mktyplib203 /win32
|
||||
# ADD BASE RSC /l 0x409 /d "_DEBUG"
|
||||
# ADD RSC /l 0x409 /d "_DEBUG"
|
||||
BSC32=bscmake.exe
|
||||
# ADD BASE BSC32 /nologo
|
||||
# ADD BSC32 /nologo
|
||||
LINK32=link.exe
|
||||
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /debug /machine:I386 /pdbtype:sept
|
||||
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /debug /machine:I386 /pdbtype:sept
|
||||
|
||||
!ENDIF
|
||||
|
||||
# Begin Target
|
||||
|
||||
# Name "UserInfo - Win32 Release"
|
||||
# Name "UserInfo - Win32 Debug"
|
||||
# Begin Group "Source Files"
|
||||
|
||||
# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\UserInfo.c
|
||||
# End Source File
|
||||
# End Group
|
||||
# Begin Group "Header Files"
|
||||
|
||||
# PROP Default_Filter "h;hpp;hxx;hm;inl"
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=..\exdll\exdll.h
|
||||
# End Source File
|
||||
# End Group
|
||||
# Begin Group "Resource Files"
|
||||
|
||||
# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"
|
||||
# End Group
|
||||
# End Target
|
||||
# End Project
|
29
Contrib/UserInfo/UserInfo.dsw
Normal file
29
Contrib/UserInfo/UserInfo.dsw
Normal file
|
@ -0,0 +1,29 @@
|
|||
Microsoft Developer Studio Workspace File, Format Version 6.00
|
||||
# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!
|
||||
|
||||
###############################################################################
|
||||
|
||||
Project: "UserInfo"=.\UserInfo.dsp - Package Owner=<4>
|
||||
|
||||
Package=<5>
|
||||
{{{
|
||||
}}}
|
||||
|
||||
Package=<4>
|
||||
{{{
|
||||
}}}
|
||||
|
||||
###############################################################################
|
||||
|
||||
Global:
|
||||
|
||||
Package=<5>
|
||||
{{{
|
||||
}}}
|
||||
|
||||
Package=<3>
|
||||
{{{
|
||||
}}}
|
||||
|
||||
###############################################################################
|
||||
|
10
Contrib/UserInfo/UserInfo.nsi
Normal file
10
Contrib/UserInfo/UserInfo.nsi
Normal file
|
@ -0,0 +1,10 @@
|
|||
Name "UserInfo.dll test"
|
||||
OutFile UserInfo.exe
|
||||
|
||||
Section
|
||||
UserInfo::GetName
|
||||
Pop $0
|
||||
UserInfo::GetGroup
|
||||
Pop $1
|
||||
MessageBox MB_OK 'User "$0" in group "$1"'
|
||||
SectionEnd
|
BIN
Plugins/UserInfo.dll
Normal file
BIN
Plugins/UserInfo.dll
Normal file
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue