load/unload registry keys on demand

This commit is contained in:
freedom
2023-08-16 02:00:09 -06:00
parent b9cea5b8e6
commit d41f0b868b

View File

@@ -46,21 +46,25 @@ PWSTR pszSettingsMask = L"SettingsMask";
/* * * *\ /* * * *\
InitializeRegistryKeys - InitializeRegistryKeys -
Takes the relevant registry keys and turns them Creates the necessary registry keys if necessary.
into valid and usable handles.
RETURNS - RETURNS -
TRUE if successful, FALSE if unsuccessful. TRUE if successful, FALSE if unsuccessful.
\* * * */ \* * * */
BOOL InitializeRegistryKeys() BOOL InitializeRegistryKeys()
{ {
if (!RegCreateKeyEx(HKEY_CURRENT_USER, PROGMGR_KEY, 0, szProgMgr, 0, if (RegCreateKeyEx(HKEY_CURRENT_USER, PROGMGR_KEY, 0, szProgMgr, 0,
KEY_READ | KEY_WRITE, NULL, &hKeyProgramManager, NULL)) KEY_READ | KEY_WRITE, NULL, &hKeyProgramManager, NULL) == ERROR_SUCCESS)
{ {
// Create Program Groups and Settings keys // Create Program Groups and Settings keys
RegCreateKeyEx(hKeyProgramManager, pszProgramGroups, 0, szProgMgr, 0, RegCreateKeyEx(hKeyProgramManager, pszProgramGroups, 0, szProgMgr, 0,
KEY_READ | KEY_WRITE, NULL, &hKeyProgramGroups, NULL); KEY_READ | KEY_WRITE, NULL, &hKeyProgramGroups, NULL);
RegCreateKeyEx(hKeyProgramManager, pszSettings, 0, szProgMgr, 0, RegCreateKeyEx(hKeyProgramManager, pszSettings, 0, szProgMgr, 0,
KEY_READ | KEY_WRITE, NULL, &hKeySettings, NULL); KEY_READ | KEY_WRITE, NULL, &hKeySettings, NULL);
// Close the key. We'll open/close the registry as we need to.
RegCloseKey(hKeyProgramGroups);
RegCloseKey(hKeySettings);
RegCloseKey(hKeyProgramManager);
return TRUE; return TRUE;
} }
@@ -87,18 +91,14 @@ BOOL IsProgMgrDefaultShell()
if (RegQueryValueEx(hKeyWinlogon, L"Shell", 0, &dwType, if (RegQueryValueEx(hKeyWinlogon, L"Shell", 0, &dwType,
(LPBYTE)szShell, &dwBufferSize) == ERROR_SUCCESS) (LPBYTE)szShell, &dwBufferSize) == ERROR_SUCCESS)
{ {
RegCloseKey(hKeyWinlogon);
if (StrStr(szShell, szProgMgr)) if (StrStr(szShell, szProgMgr))
{
// ProgMgr detected >:) // ProgMgr detected >:)
RegCloseKey(hKeyWinlogon);
return TRUE; return TRUE;
}
else else
{
// Inferior shell detected. // Inferior shell detected.
RegCloseKey(hKeyWinlogon);
return FALSE; return FALSE;
}
} }
} }
else else
@@ -121,6 +121,12 @@ DWORD SaveGroupToRegistry(_In_ PGROUP pg)
{ {
DWORD dwConfigStatus = RCE_SUCCESS; DWORD dwConfigStatus = RCE_SUCCESS;
// Open the Program Groups key
RegOpenKeyEx(HKEY_LOCAL_MACHINE, PROGMGR_KEY,
0, KEY_READ | KEY_WRITE, &hKeyProgramManager);
RegOpenKeyEx(hKeyProgramManager, pszProgramGroups, 0,
KEY_READ | KEY_WRITE, &hKeyProgramGroups);
// If the pointer is invalid then fail out // If the pointer is invalid then fail out
if (pg == NULL) if (pg == NULL)
return RCE_FAILURE; return RCE_FAILURE;
@@ -130,6 +136,10 @@ DWORD SaveGroupToRegistry(_In_ PGROUP pg)
(const BYTE*)pg, (sizeof(*pg) + sizeof(ITEM) * pg->cItems)) == ERROR_SUCCESS) (const BYTE*)pg, (sizeof(*pg) + sizeof(ITEM) * pg->cItems)) == ERROR_SUCCESS)
dwConfigStatus = dwConfigStatus && RCE_GROUPS; dwConfigStatus = dwConfigStatus && RCE_GROUPS;
// Close the keys
RegCloseKey(hKeyProgramGroups);
RegCloseKey(hKeyProgramManager);
return dwConfigStatus; return dwConfigStatus;
} }
@@ -144,6 +154,12 @@ DWORD LoadGroupFromRegistry(_Inout_ PGROUP pg, _Out_ DWORD dwBufferSize)
DWORD dwConfigStatus = RCE_SUCCESS; DWORD dwConfigStatus = RCE_SUCCESS;
DWORD dwType = REG_BINARY; DWORD dwType = REG_BINARY;
// Open the Program Groups key
RegOpenKeyEx(HKEY_LOCAL_MACHINE, PROGMGR_KEY,
0, KEY_READ | KEY_WRITE, &hKeyProgramManager);
RegOpenKeyEx(hKeyProgramManager, pszProgramGroups, 0,
KEY_READ | KEY_WRITE, &hKeyProgramGroups);
// If the pointer is invalid then fail out // If the pointer is invalid then fail out
if (pg == NULL) if (pg == NULL)
return RCE_FAILURE; return RCE_FAILURE;
@@ -153,6 +169,10 @@ DWORD LoadGroupFromRegistry(_Inout_ PGROUP pg, _Out_ DWORD dwBufferSize)
(LPBYTE)pg, &dwBufferSize) == ERROR_SUCCESS) (LPBYTE)pg, &dwBufferSize) == ERROR_SUCCESS)
dwConfigStatus = dwConfigStatus && RCE_POSITION; dwConfigStatus = dwConfigStatus && RCE_POSITION;
// Close the keys
RegCloseKey(hKeyProgramGroups);
RegCloseKey(hKeyProgramManager);
return dwConfigStatus; return dwConfigStatus;
} }
@@ -167,6 +187,14 @@ DWORD SaveConfig(BOOL bSettings, BOOL bPos, BOOL bGroups)
{ {
DWORD dwConfigStatus = RCE_SUCCESS; DWORD dwConfigStatus = RCE_SUCCESS;
// Open the Program Groups and Settings keys
RegOpenKeyEx(HKEY_LOCAL_MACHINE, PROGMGR_KEY,
0, KEY_READ | KEY_WRITE, &hKeyProgramManager);
RegOpenKeyEx(hKeyProgramManager, pszProgramGroups, 0,
KEY_READ | KEY_WRITE, &hKeyProgramGroups);
RegOpenKeyEx(hKeyProgramManager, pszSettings, 0,
KEY_READ | KEY_WRITE, &hKeySettings);
if (bSettings) if (bSettings)
{ {
// Shrink the settings into the bitmask and save it // Shrink the settings into the bitmask and save it
@@ -207,6 +235,11 @@ DWORD SaveConfig(BOOL bSettings, BOOL bPos, BOOL bGroups)
dwConfigStatus = dwConfigStatus && RCE_GROUPS; dwConfigStatus = dwConfigStatus && RCE_GROUPS;
} }
// Close the keys
RegCloseKey(hKeyProgramGroups);
RegCloseKey(hKeySettings);
RegCloseKey(hKeyProgramManager);
return dwConfigStatus; return dwConfigStatus;
} }
@@ -221,6 +254,14 @@ DWORD LoadConfig(BOOL bSettings, BOOL bPos, BOOL bGroups)
{ {
DWORD dwConfigStatus = RCE_SUCCESS; DWORD dwConfigStatus = RCE_SUCCESS;
// Open the Program Groups and Settings keys
RegOpenKeyEx(HKEY_LOCAL_MACHINE, PROGMGR_KEY,
0, KEY_READ | KEY_WRITE, &hKeyProgramManager);
RegOpenKeyEx(hKeyProgramManager, pszProgramGroups, 0,
KEY_READ | KEY_WRITE, &hKeyProgramGroups);
RegOpenKeyEx(hKeyProgramManager, pszSettings, 0,
KEY_READ | KEY_WRITE, &hKeySettings);
if (bSettings) if (bSettings)
{ {
DWORD dwBufferSize = sizeof(dwSettingsMask); DWORD dwBufferSize = sizeof(dwSettingsMask);
@@ -256,5 +297,10 @@ DWORD LoadConfig(BOOL bSettings, BOOL bPos, BOOL bGroups)
dwConfigStatus = dwConfigStatus && RCE_GROUPS; dwConfigStatus = dwConfigStatus && RCE_GROUPS;
} }
// Close the keys
RegCloseKey(hKeyProgramGroups);
RegCloseKey(hKeySettings);
RegCloseKey(hKeyProgramManager);
return dwConfigStatus; return dwConfigStatus;
} }