📄 security.c
字号:
{
return E_OUTOFMEMORY;
}
ErrorCode = GetInheritanceSource(obj->szRegKey,
SE_REGISTRY_KEY,
si,
(obj->ObjectInfo.dwFlags & SI_CONTAINER) != 0,
NULL,
0,
pACL,
NULL,
&RegAccessMasks,
pif);
if (ErrorCode == ERROR_SUCCESS)
{
/* Calculate the size of the buffer to return */
for (i = 0;
i < pACL->AceCount;
i++)
{
if (pif[i].AncestorName != NULL)
{
pifSize += (_tcslen(pif[i].AncestorName) + 1) * sizeof(TCHAR);
}
}
/* Allocate enough space for the array and the strings */
pif2 = (PINHERITED_FROM)LocalAlloc(LMEM_FIXED,
pifSize);
if (pif2 == NULL)
{
ErrorCode = GetLastError();
goto Cleanup;
}
/* copy the array and strings to the buffer */
lpBuf = (LPTSTR)((ULONG_PTR)pif2 + (pACL->AceCount * sizeof(INHERITED_FROM)));
for (i = 0;
i < pACL->AceCount;
i++)
{
pif2[i].GenerationGap = pif[i].GenerationGap;
if (pif[i].AncestorName != NULL)
{
pif2[i].AncestorName = lpBuf;
_tcscpy(lpBuf,
pif[i].AncestorName);
lpBuf += _tcslen(pif[i].AncestorName) + 1;
}
else
pif2[i].AncestorName = NULL;
}
/* return the newly allocated array */
*ppInheritArray = pif2;
}
Cleanup:
FreeInheritedFromArray(pif,
pACL->AceCount,
NULL);
HeapFree(GetProcessHeap(),
0,
pif);
return HRESULT_FROM_WIN32(ErrorCode);
}
/******************************************************************************
Implementation of the CRegKeySecurity constructor
******************************************************************************/
static PCRegKeySecurity
CRegKeySecurity_fnConstructor(LPTSTR lpRegKey,
HKEY hRootKey,
SI_OBJECT_INFO *ObjectInfo,
BOOL *Btn)
{
PCRegKeySecurity obj;
obj = (PCRegKeySecurity)HeapAlloc(GetProcessHeap(),
HEAP_ZERO_MEMORY,
FIELD_OFFSET(CRegKeySecurity,
szRegKey[_tcslen(lpRegKey) + 1]));
if (obj != NULL)
{
obj->ref = 1;
obj->lpISecurityInformationVtbl = &vtblISecurityInformation;
#if REGEDIT_IMPLEMENT_ISECURITYINFORMATION2
obj->lpISecurityInformation2Vtbl = &vtblISecurityInformation2;
#endif
obj->lpIEffectivePermissionVtbl = &vtblIEffectivePermission;
obj->lpISecurityObjectTypeInfoVtbl = &vtblISecurityObjectTypeInfo;
obj->ObjectInfo = *ObjectInfo;
obj->Btn = Btn;
obj->hRootKey = hRootKey;
_tcscpy(obj->szRegKey,
lpRegKey);
}
else
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
return obj;
}
/******************************************************************************/
/******************************************************************************/
/******************************************************************************/
typedef struct _CHANGE_CONTEXT
{
HKEY hKey;
LPTSTR KeyString;
} CHANGE_CONTEXT, *PCHANGE_CONTEXT;
typedef BOOL (WINAPI *PEDITSECURITY)(HWND hwndOwner,
struct ISecurityInformation *psi);
static PEDITSECURITY pfnEditSecurity;
static HMODULE hAclUiDll;
BOOL
InitializeAclUiDll(VOID)
{
if (!(hAclUiDll = LoadLibrary(_T("aclui.dll"))))
{
return FALSE;
}
if (!(pfnEditSecurity = (PEDITSECURITY)GetProcAddress(hAclUiDll,
"EditSecurity")))
{
FreeLibrary(hAclUiDll);
hAclUiDll = NULL;
return FALSE;
}
return TRUE;
}
VOID
UnloadAclUiDll(VOID)
{
if (hAclUiDll != NULL)
{
FreeLibrary(hAclUiDll);
}
}
BOOL
RegKeyEditPermissions(HWND hWndOwner,
HKEY hKey,
LPCTSTR lpMachine,
LPCTSTR lpKeyName)
{
BOOL Result = FALSE;
LPWSTR Machine = NULL, KeyName = NULL;
LPCTSTR lphKey = NULL;
LPTSTR lpKeyPath = NULL;
PCRegKeySecurity RegKeySecurity;
SI_OBJECT_INFO ObjectInfo;
size_t lnMachine = 0, lnKeyName = 0;
if (pfnEditSecurity == NULL)
{
return FALSE;
}
#ifndef UNICODE
/* aclui.dll only accepts unicode strings, convert them */
if (lpMachine != NULL)
{
lnMachine = lstrlen(lpMachine);
if (!(Machine = HeapAlloc(GetProcessHeap(),
0,
(lnMachine + 1) * sizeof(WCHAR))))
{
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
goto Cleanup;
}
if (lnMachine > 0)
{
MultiByteToWideChar(CP_ACP,
0,
lpMachine,
-1,
Machine,
lnMachine + 1);
}
else
*Machine = L'\0';
}
else
Machine = NULL;
if (lpKeyName != NULL)
{
lnKeyName = lstrlen(lpKeyName);
if (!(KeyName = HeapAlloc(GetProcessHeap(),
0,
(lnKeyName + 1) * sizeof(WCHAR))))
{
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
goto Cleanup;
}
if (lnKeyName > 0)
{
MultiByteToWideChar(CP_ACP,
0,
lpKeyName,
-1,
KeyName,
lnKeyName + 1);
}
else
*KeyName = L'\0';
}
else
KeyName = NULL;
#else
Machine = (LPWSTR)lpMachine;
KeyName = (LPWSTR)lpKeyName;
if (Machine != NULL)
lnMachine = wcslen(lpMachine);
if (KeyName != NULL)
lnKeyName = wcslen(KeyName);
#endif
/* build registry path */
if (lpMachine != NULL &&
(lpMachine[0] == _T('\0') ||
(lpMachine[0] == _T('.') && lpMachine[1] == _T('.'))))
{
lnMachine = 0;
}
if (hKey == HKEY_CLASSES_ROOT)
lphKey = TEXT("CLASSES_ROOT");
else if (hKey == HKEY_CURRENT_USER)
lphKey = TEXT("CURRENT_USER");
else if (hKey == HKEY_LOCAL_MACHINE)
lphKey = TEXT("MACHINE");
else if (hKey == HKEY_USERS)
lphKey = TEXT("USERS");
else if (hKey == HKEY_CURRENT_CONFIG)
lphKey = TEXT("CONFIG");
else
goto Cleanup;
lpKeyPath = HeapAlloc(GetProcessHeap(),
0,
(2 + lnMachine + 1 + _tcslen(lphKey) + 1 + lnKeyName) * sizeof(TCHAR));
if (lpKeyPath == NULL)
{
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
goto Cleanup;
}
lpKeyPath[0] = _T('\0');
if (lnMachine != 0)
{
_tcscat(lpKeyPath,
_T("\\\\"));
_tcscat(lpKeyPath,
lpMachine);
_tcscat(lpKeyPath,
_T("\\"));
}
_tcscat(lpKeyPath,
lphKey);
if (lpKeyName != NULL && lpKeyName[0] != _T('\0'))
{
if (lpKeyName[0] != _T('\\'))
{
_tcscat(lpKeyPath,
_T("\\"));
}
_tcscat(lpKeyPath,
lpKeyName);
}
ObjectInfo.dwFlags = SI_EDIT_ALL | SI_ADVANCED | SI_CONTAINER | SI_EDIT_EFFECTIVE | SI_EDIT_PERMS |
SI_OWNER_RECURSE | SI_RESET_DACL_TREE | SI_RESET_SACL_TREE;
ObjectInfo.hInstance = hInst;
ObjectInfo.pszServerName = Machine;
ObjectInfo.pszObjectName = KeyName; /* FIXME */
ObjectInfo.pszPageTitle = KeyName; /* FIXME */
if (!(RegKeySecurity = CRegKeySecurity_fnConstructor(lpKeyPath,
hKey,
&ObjectInfo,
&Result)))
{
goto Cleanup;
}
/* display the security editor dialog */
pfnEditSecurity(hWndOwner,
impl_to_interface(RegKeySecurity,
ISecurityInformation));
/* dereference the interface, it should be destroyed here */
CRegKeySecurity_fnRelease(RegKeySecurity);
Cleanup:
#ifndef UNICODE
if (Machine != NULL)
{
HeapFree(GetProcessHeap(),
0,
Machine);
}
if (KeyName != NULL)
{
HeapFree(GetProcessHeap(),
0,
KeyName);
}
#endif
if (lpKeyPath != NULL)
{
HeapFree(GetProcessHeap(),
0,
lpKeyPath);
}
return Result;
}
/* EOF */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -