Milestone 5: deliver embedded RDP sessions and lifecycle hardening

This commit is contained in:
Keith Smith
2026-03-03 18:59:26 -07:00
parent 230a401386
commit 36006bd4aa
2941 changed files with 724359 additions and 77 deletions

View File

@@ -0,0 +1,18 @@
# WinPR: Windows Portable Runtime
# libwinpr-registry cmake build script
#
# Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
winpr_module_add(registry_reg.c registry_reg.h registry.c)

View File

@@ -0,0 +1,9 @@
set(MINWIN_LAYER "1")
set(MINWIN_GROUP "core")
set(MINWIN_MAJOR_VERSION "1")
set(MINWIN_MINOR_VERSION "0")
set(MINWIN_SHORT_NAME "registry")
set(MINWIN_LONG_NAME "Registry Functions")
set(MODULE_LIBRARY_NAME
"api-ms-win-${MINWIN_GROUP}-${MINWIN_SHORT_NAME}-l${MINWIN_LAYER}-${MINWIN_MAJOR_VERSION}-${MINWIN_MINOR_VERSION}"
)

View File

@@ -0,0 +1,632 @@
/**
* WinPR: Windows Portable Runtime
* Windows Registry
*
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <winpr/config.h>
#include <winpr/registry.h>
/*
* Windows registry MSDN pages:
* Reference: http://msdn.microsoft.com/en-us/library/windows/desktop/ms724880/
* Functions: http://msdn.microsoft.com/en-us/library/windows/desktop/ms724875/
*/
#if !defined(_WIN32) || defined(_UWP)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winpr/crt.h>
#include <winpr/assert.h>
#include <winpr/string.h>
#include "registry_reg.h"
#include "../log.h"
#define TAG WINPR_TAG("registry")
static Reg* instance = nullptr;
static size_t regsz_length(const char* key, const void* value, bool unicode)
{
/* https://learn.microsoft.com/en-us/windows/win32/sysinfo/registry-element-size-limits
*
* while not strictly limited to this size larger values should be stored to a
* file.
*/
const size_t limit = 16383;
size_t length = 0;
if (unicode)
length = _wcsnlen((const WCHAR*)value, limit);
else
length = strnlen((const char*)value, limit);
if (length >= limit)
WLog_WARN(TAG, "REG_SZ[%s] truncated to size %" PRIuz, key, length);
return length;
}
static Reg* RegGetInstance(void)
{
if (!instance)
instance = reg_open(1);
return instance;
}
LONG RegCloseKey(HKEY hKey)
{
WINPR_UNUSED(hKey);
return 0;
}
LONG RegCopyTreeW(WINPR_ATTR_UNUSED HKEY hKeySrc, WINPR_ATTR_UNUSED LPCWSTR lpSubKey,
WINPR_ATTR_UNUSED HKEY hKeyDest)
{
WLog_ERR(TAG, "TODO: Implement");
return -1;
}
LONG RegCopyTreeA(WINPR_ATTR_UNUSED HKEY hKeySrc, WINPR_ATTR_UNUSED LPCSTR lpSubKey,
WINPR_ATTR_UNUSED HKEY hKeyDest)
{
WLog_ERR(TAG, "TODO: Implement");
return -1;
}
LONG RegCreateKeyExW(WINPR_ATTR_UNUSED HKEY hKey, WINPR_ATTR_UNUSED LPCWSTR lpSubKey,
WINPR_ATTR_UNUSED DWORD Reserved, WINPR_ATTR_UNUSED LPWSTR lpClass,
WINPR_ATTR_UNUSED DWORD dwOptions, WINPR_ATTR_UNUSED REGSAM samDesired,
WINPR_ATTR_UNUSED LPSECURITY_ATTRIBUTES lpSecurityAttributes,
WINPR_ATTR_UNUSED PHKEY phkResult, WINPR_ATTR_UNUSED LPDWORD lpdwDisposition)
{
WLog_ERR(TAG, "TODO: Implement");
return -1;
}
LONG RegCreateKeyExA(WINPR_ATTR_UNUSED HKEY hKey, WINPR_ATTR_UNUSED LPCSTR lpSubKey,
WINPR_ATTR_UNUSED DWORD Reserved, WINPR_ATTR_UNUSED LPSTR lpClass,
WINPR_ATTR_UNUSED DWORD dwOptions, WINPR_ATTR_UNUSED REGSAM samDesired,
WINPR_ATTR_UNUSED LPSECURITY_ATTRIBUTES lpSecurityAttributes,
WINPR_ATTR_UNUSED PHKEY phkResult, WINPR_ATTR_UNUSED LPDWORD lpdwDisposition)
{
WLog_ERR(TAG, "TODO: Implement");
return -1;
}
LONG RegDeleteKeyExW(WINPR_ATTR_UNUSED HKEY hKey, WINPR_ATTR_UNUSED LPCWSTR lpSubKey,
WINPR_ATTR_UNUSED REGSAM samDesired, WINPR_ATTR_UNUSED DWORD Reserved)
{
WLog_ERR(TAG, "TODO: Implement");
return -1;
}
LONG RegDeleteKeyExA(WINPR_ATTR_UNUSED HKEY hKey, WINPR_ATTR_UNUSED LPCSTR lpSubKey,
WINPR_ATTR_UNUSED REGSAM samDesired, WINPR_ATTR_UNUSED DWORD Reserved)
{
WLog_ERR(TAG, "TODO: Implement");
return -1;
}
LONG RegDeleteTreeW(WINPR_ATTR_UNUSED HKEY hKey, WINPR_ATTR_UNUSED LPCWSTR lpSubKey)
{
WLog_ERR(TAG, "TODO: Implement");
return -1;
}
LONG RegDeleteTreeA(WINPR_ATTR_UNUSED HKEY hKey, WINPR_ATTR_UNUSED LPCSTR lpSubKey)
{
WLog_ERR(TAG, "TODO: Implement");
return -1;
}
LONG RegDeleteValueW(WINPR_ATTR_UNUSED HKEY hKey, WINPR_ATTR_UNUSED LPCWSTR lpValueName)
{
WLog_ERR(TAG, "TODO: Implement");
return -1;
}
LONG RegDeleteValueA(WINPR_ATTR_UNUSED HKEY hKey, WINPR_ATTR_UNUSED LPCSTR lpValueName)
{
WLog_ERR(TAG, "TODO: Implement");
return -1;
}
LONG RegDisablePredefinedCacheEx(void)
{
WLog_ERR(TAG, "TODO: Implement");
return -1;
}
LONG RegEnumKeyExW(WINPR_ATTR_UNUSED HKEY hKey, WINPR_ATTR_UNUSED DWORD dwIndex,
WINPR_ATTR_UNUSED LPWSTR lpName, WINPR_ATTR_UNUSED LPDWORD lpcName,
WINPR_ATTR_UNUSED LPDWORD lpReserved, WINPR_ATTR_UNUSED LPWSTR lpClass,
WINPR_ATTR_UNUSED LPDWORD lpcClass,
WINPR_ATTR_UNUSED PFILETIME lpftLastWriteTime)
{
WLog_ERR(TAG, "TODO: Implement");
return -1;
}
LONG RegEnumKeyExA(WINPR_ATTR_UNUSED HKEY hKey, WINPR_ATTR_UNUSED DWORD dwIndex,
WINPR_ATTR_UNUSED LPSTR lpName, WINPR_ATTR_UNUSED LPDWORD lpcName,
WINPR_ATTR_UNUSED LPDWORD lpReserved, WINPR_ATTR_UNUSED LPSTR lpClass,
WINPR_ATTR_UNUSED LPDWORD lpcClass,
WINPR_ATTR_UNUSED PFILETIME lpftLastWriteTime)
{
WLog_ERR(TAG, "TODO: Implement");
return -1;
}
LONG RegEnumValueW(WINPR_ATTR_UNUSED HKEY hKey, WINPR_ATTR_UNUSED DWORD dwIndex,
WINPR_ATTR_UNUSED LPWSTR lpValueName, WINPR_ATTR_UNUSED LPDWORD lpcchValueName,
WINPR_ATTR_UNUSED LPDWORD lpReserved, WINPR_ATTR_UNUSED LPDWORD lpType,
WINPR_ATTR_UNUSED LPBYTE lpData, WINPR_ATTR_UNUSED LPDWORD lpcbData)
{
WLog_ERR(TAG, "TODO: Implement");
return -1;
}
LONG RegEnumValueA(WINPR_ATTR_UNUSED HKEY hKey, WINPR_ATTR_UNUSED DWORD dwIndex,
WINPR_ATTR_UNUSED LPSTR lpValueName, WINPR_ATTR_UNUSED LPDWORD lpcchValueName,
WINPR_ATTR_UNUSED LPDWORD lpReserved, WINPR_ATTR_UNUSED LPDWORD lpType,
WINPR_ATTR_UNUSED LPBYTE lpData, WINPR_ATTR_UNUSED LPDWORD lpcbData)
{
WLog_ERR(TAG, "TODO: Implement");
return -1;
}
LONG RegFlushKey(WINPR_ATTR_UNUSED HKEY hKey)
{
WLog_ERR(TAG, "TODO: Implement");
return -1;
}
LONG RegGetKeySecurity(WINPR_ATTR_UNUSED HKEY hKey,
WINPR_ATTR_UNUSED SECURITY_INFORMATION SecurityInformation,
WINPR_ATTR_UNUSED PSECURITY_DESCRIPTOR pSecurityDescriptor,
WINPR_ATTR_UNUSED LPDWORD lpcbSecurityDescriptor)
{
WLog_ERR(TAG, "TODO: Implement");
return -1;
}
LONG RegGetValueW(WINPR_ATTR_UNUSED HKEY hkey, WINPR_ATTR_UNUSED LPCWSTR lpSubKey,
WINPR_ATTR_UNUSED LPCWSTR lpValue, WINPR_ATTR_UNUSED DWORD dwFlags,
WINPR_ATTR_UNUSED LPDWORD pdwType, WINPR_ATTR_UNUSED PVOID pvData,
WINPR_ATTR_UNUSED LPDWORD pcbData)
{
WLog_ERR(TAG, "TODO: Implement");
return -1;
}
LONG RegGetValueA(WINPR_ATTR_UNUSED HKEY hkey, WINPR_ATTR_UNUSED LPCSTR lpSubKey,
WINPR_ATTR_UNUSED LPCSTR lpValue, WINPR_ATTR_UNUSED DWORD dwFlags,
WINPR_ATTR_UNUSED LPDWORD pdwType, WINPR_ATTR_UNUSED PVOID pvData,
WINPR_ATTR_UNUSED LPDWORD pcbData)
{
WLog_ERR(TAG, "TODO: Implement");
return -1;
}
LONG RegLoadAppKeyW(WINPR_ATTR_UNUSED LPCWSTR lpFile, WINPR_ATTR_UNUSED PHKEY phkResult,
WINPR_ATTR_UNUSED REGSAM samDesired, WINPR_ATTR_UNUSED DWORD dwOptions,
WINPR_ATTR_UNUSED DWORD Reserved)
{
WLog_ERR(TAG, "TODO: Implement");
return -1;
}
LONG RegLoadAppKeyA(WINPR_ATTR_UNUSED LPCSTR lpFile, WINPR_ATTR_UNUSED PHKEY phkResult,
WINPR_ATTR_UNUSED REGSAM samDesired, WINPR_ATTR_UNUSED DWORD dwOptions,
WINPR_ATTR_UNUSED DWORD Reserved)
{
WLog_ERR(TAG, "TODO: Implement");
return -1;
}
LONG RegLoadKeyW(WINPR_ATTR_UNUSED HKEY hKey, WINPR_ATTR_UNUSED LPCWSTR lpSubKey,
WINPR_ATTR_UNUSED LPCWSTR lpFile)
{
WLog_ERR(TAG, "TODO: Implement");
return -1;
}
LONG RegLoadKeyA(WINPR_ATTR_UNUSED HKEY hKey, WINPR_ATTR_UNUSED LPCSTR lpSubKey,
WINPR_ATTR_UNUSED LPCSTR lpFile)
{
WLog_ERR(TAG, "TODO: Implement");
return -1;
}
LONG RegLoadMUIStringW(WINPR_ATTR_UNUSED HKEY hKey, WINPR_ATTR_UNUSED LPCWSTR pszValue,
WINPR_ATTR_UNUSED LPWSTR pszOutBuf, WINPR_ATTR_UNUSED DWORD cbOutBuf,
WINPR_ATTR_UNUSED LPDWORD pcbData, WINPR_ATTR_UNUSED DWORD Flags,
WINPR_ATTR_UNUSED LPCWSTR pszDirectory)
{
WLog_ERR(TAG, "TODO: Implement");
return -1;
}
LONG RegLoadMUIStringA(WINPR_ATTR_UNUSED HKEY hKey, WINPR_ATTR_UNUSED LPCSTR pszValue,
WINPR_ATTR_UNUSED LPSTR pszOutBuf, WINPR_ATTR_UNUSED DWORD cbOutBuf,
WINPR_ATTR_UNUSED LPDWORD pcbData, WINPR_ATTR_UNUSED DWORD Flags,
WINPR_ATTR_UNUSED LPCSTR pszDirectory)
{
WLog_ERR(TAG, "TODO: Implement");
return -1;
}
LONG RegNotifyChangeKeyValue(WINPR_ATTR_UNUSED HKEY hKey, WINPR_ATTR_UNUSED BOOL bWatchSubtree,
WINPR_ATTR_UNUSED DWORD dwNotifyFilter,
WINPR_ATTR_UNUSED HANDLE hEvent, WINPR_ATTR_UNUSED BOOL fAsynchronous)
{
WLog_ERR(TAG, "TODO: Implement");
return -1;
}
LONG RegOpenCurrentUser(WINPR_ATTR_UNUSED REGSAM samDesired, WINPR_ATTR_UNUSED PHKEY phkResult)
{
WLog_ERR(TAG, "TODO: Implement");
return -1;
}
LONG RegOpenKeyExW(HKEY hKey, LPCWSTR lpSubKey, DWORD ulOptions, REGSAM samDesired, PHKEY phkResult)
{
LONG rc = 0;
char* str = ConvertWCharToUtf8Alloc(lpSubKey, nullptr);
if (!str)
return ERROR_FILE_NOT_FOUND;
rc = RegOpenKeyExA(hKey, str, ulOptions, samDesired, phkResult);
free(str);
return rc;
}
LONG RegOpenKeyExA(HKEY hKey, LPCSTR lpSubKey, WINPR_ATTR_UNUSED DWORD ulOptions,
WINPR_ATTR_UNUSED REGSAM samDesired, PHKEY phkResult)
{
Reg* reg = RegGetInstance();
if (!reg)
return -1;
if (hKey != HKEY_LOCAL_MACHINE)
{
WLog_WARN(TAG, "Registry emulation only supports HKEY_LOCAL_MACHINE");
return ERROR_FILE_NOT_FOUND;
}
WINPR_ASSERT(reg->root_key);
RegKey* pKey = reg->root_key->subkeys;
while (pKey != nullptr)
{
WINPR_ASSERT(lpSubKey);
if (pKey->subname && (_stricmp(pKey->subname, lpSubKey) == 0))
{
*phkResult = (HKEY)pKey;
return ERROR_SUCCESS;
}
pKey = pKey->next;
}
*phkResult = nullptr;
return ERROR_FILE_NOT_FOUND;
}
LONG RegOpenUserClassesRoot(WINPR_ATTR_UNUSED HANDLE hToken, WINPR_ATTR_UNUSED DWORD dwOptions,
WINPR_ATTR_UNUSED REGSAM samDesired, WINPR_ATTR_UNUSED PHKEY phkResult)
{
WLog_ERR(TAG, "TODO: Implement");
return -1;
}
LONG RegQueryInfoKeyW(WINPR_ATTR_UNUSED HKEY hKey, WINPR_ATTR_UNUSED LPWSTR lpClass,
WINPR_ATTR_UNUSED LPDWORD lpcClass, WINPR_ATTR_UNUSED LPDWORD lpReserved,
WINPR_ATTR_UNUSED LPDWORD lpcSubKeys,
WINPR_ATTR_UNUSED LPDWORD lpcMaxSubKeyLen,
WINPR_ATTR_UNUSED LPDWORD lpcMaxClassLen, WINPR_ATTR_UNUSED LPDWORD lpcValues,
WINPR_ATTR_UNUSED LPDWORD lpcMaxValueNameLen,
WINPR_ATTR_UNUSED LPDWORD lpcMaxValueLen,
WINPR_ATTR_UNUSED LPDWORD lpcbSecurityDescriptor,
WINPR_ATTR_UNUSED PFILETIME lpftLastWriteTime)
{
WLog_ERR(TAG, "TODO: Implement");
return -1;
}
LONG RegQueryInfoKeyA(WINPR_ATTR_UNUSED HKEY hKey, WINPR_ATTR_UNUSED LPSTR lpClass,
WINPR_ATTR_UNUSED LPDWORD lpcClass, WINPR_ATTR_UNUSED LPDWORD lpReserved,
WINPR_ATTR_UNUSED LPDWORD lpcSubKeys,
WINPR_ATTR_UNUSED LPDWORD lpcMaxSubKeyLen,
WINPR_ATTR_UNUSED LPDWORD lpcMaxClassLen, WINPR_ATTR_UNUSED LPDWORD lpcValues,
WINPR_ATTR_UNUSED LPDWORD lpcMaxValueNameLen,
WINPR_ATTR_UNUSED LPDWORD lpcMaxValueLen,
WINPR_ATTR_UNUSED LPDWORD lpcbSecurityDescriptor,
WINPR_ATTR_UNUSED PFILETIME lpftLastWriteTime)
{
WLog_ERR(TAG, "TODO: Implement");
return -1;
}
static LONG reg_read_int(const RegVal* pValue, LPBYTE lpData, LPDWORD lpcbData)
{
const BYTE* ptr = nullptr;
DWORD required = 0;
WINPR_ASSERT(pValue);
switch (pValue->type)
{
case REG_DWORD:
case REG_DWORD_BIG_ENDIAN:
required = sizeof(DWORD);
ptr = (const BYTE*)&pValue->data.dword;
break;
case REG_QWORD:
required = sizeof(UINT64);
ptr = (const BYTE*)&pValue->data.qword;
break;
default:
return ERROR_INTERNAL_ERROR;
}
if (lpcbData)
{
DWORD size = *lpcbData;
*lpcbData = required;
if (lpData)
{
if (size < *lpcbData)
return ERROR_MORE_DATA;
}
}
if (lpData != nullptr)
{
DWORD size = 0;
WINPR_ASSERT(lpcbData);
size = *lpcbData;
*lpcbData = required;
if (size < required)
return ERROR_MORE_DATA;
memcpy(lpData, ptr, required);
}
else if (lpcbData != nullptr)
*lpcbData = required;
return ERROR_SUCCESS;
}
// NOLINTBEGIN(readability-non-const-parameter)
LONG RegQueryValueExW(HKEY hKey, LPCWSTR lpValueName, LPDWORD lpReserved, LPDWORD lpType,
LPBYTE lpData, LPDWORD lpcbData)
// NOLINTEND(readability-non-const-parameter)
{
LONG status = ERROR_FILE_NOT_FOUND;
RegKey* key = nullptr;
RegVal* pValue = nullptr;
char* valueName = nullptr;
WINPR_UNUSED(lpReserved);
key = (RegKey*)hKey;
WINPR_ASSERT(key);
valueName = ConvertWCharToUtf8Alloc(lpValueName, nullptr);
if (!valueName)
goto end;
pValue = key->values;
while (pValue != nullptr)
{
if (strcmp(pValue->name, valueName) == 0)
{
if (lpType)
*lpType = pValue->type;
switch (pValue->type)
{
case REG_DWORD_BIG_ENDIAN:
case REG_QWORD:
case REG_DWORD:
status = reg_read_int(pValue, lpData, lpcbData);
goto end;
case REG_SZ:
{
const size_t length =
regsz_length(pValue->name, pValue->data.string, TRUE) * sizeof(WCHAR);
status = ERROR_SUCCESS;
if (lpData != nullptr)
{
DWORD size = 0;
union
{
WCHAR* wc;
BYTE* b;
} cnv;
WINPR_ASSERT(lpcbData);
cnv.b = lpData;
size = *lpcbData;
*lpcbData = (DWORD)length;
if (size < length)
status = ERROR_MORE_DATA;
if (ConvertUtf8NToWChar(pValue->data.string, length, cnv.wc, length) < 0)
status = ERROR_OUTOFMEMORY;
}
else if (lpcbData)
*lpcbData = (UINT32)length;
goto end;
}
default:
WLog_WARN(TAG,
"Registry emulation does not support value type %s [0x%08" PRIx32 "]",
reg_type_string(pValue->type), pValue->type);
break;
}
}
pValue = pValue->next;
}
end:
free(valueName);
return status;
}
// NOLINTBEGIN(readability-non-const-parameter)
LONG RegQueryValueExA(HKEY hKey, LPCSTR lpValueName, LPDWORD lpReserved, LPDWORD lpType,
LPBYTE lpData, LPDWORD lpcbData)
// NOLINTEND(readability-non-const-parameter)
{
RegKey* key = nullptr;
RegVal* pValue = nullptr;
WINPR_UNUSED(lpReserved);
key = (RegKey*)hKey;
WINPR_ASSERT(key);
pValue = key->values;
while (pValue != nullptr)
{
if (strcmp(pValue->name, lpValueName) == 0)
{
if (lpType)
*lpType = pValue->type;
switch (pValue->type)
{
case REG_DWORD_BIG_ENDIAN:
case REG_QWORD:
case REG_DWORD:
return reg_read_int(pValue, lpData, lpcbData);
case REG_SZ:
{
const size_t length = regsz_length(pValue->name, pValue->data.string, FALSE);
char* pData = (char*)lpData;
if (pData != nullptr)
{
DWORD size = 0;
WINPR_ASSERT(lpcbData);
size = *lpcbData;
*lpcbData = (DWORD)length;
if (size < length)
return ERROR_MORE_DATA;
memcpy(pData, pValue->data.string, length);
pData[length] = '\0';
}
else if (lpcbData)
*lpcbData = (UINT32)length;
return ERROR_SUCCESS;
}
default:
WLog_WARN(TAG,
"Registry emulation does not support value type %s [0x%08" PRIx32 "]",
reg_type_string(pValue->type), pValue->type);
break;
}
}
pValue = pValue->next;
}
return ERROR_FILE_NOT_FOUND;
}
LONG RegRestoreKeyW(WINPR_ATTR_UNUSED HKEY hKey, WINPR_ATTR_UNUSED LPCWSTR lpFile,
WINPR_ATTR_UNUSED DWORD dwFlags)
{
WLog_ERR(TAG, "TODO: Implement");
return -1;
}
LONG RegRestoreKeyA(WINPR_ATTR_UNUSED HKEY hKey, WINPR_ATTR_UNUSED LPCSTR lpFile,
WINPR_ATTR_UNUSED DWORD dwFlags)
{
WLog_ERR(TAG, "TODO: Implement");
return -1;
}
LONG RegSaveKeyExW(WINPR_ATTR_UNUSED HKEY hKey, WINPR_ATTR_UNUSED LPCWSTR lpFile,
WINPR_ATTR_UNUSED LPSECURITY_ATTRIBUTES lpSecurityAttributes,
WINPR_ATTR_UNUSED DWORD Flags)
{
WLog_ERR(TAG, "TODO: Implement");
return -1;
}
LONG RegSaveKeyExA(WINPR_ATTR_UNUSED HKEY hKey, WINPR_ATTR_UNUSED LPCSTR lpFile,
WINPR_ATTR_UNUSED LPSECURITY_ATTRIBUTES lpSecurityAttributes,
WINPR_ATTR_UNUSED DWORD Flags)
{
WLog_ERR(TAG, "TODO: Implement");
return -1;
}
LONG RegSetKeySecurity(WINPR_ATTR_UNUSED HKEY hKey,
WINPR_ATTR_UNUSED SECURITY_INFORMATION SecurityInformation,
WINPR_ATTR_UNUSED PSECURITY_DESCRIPTOR pSecurityDescriptor)
{
WLog_ERR(TAG, "TODO: Implement");
return -1;
}
LONG RegSetValueExW(WINPR_ATTR_UNUSED HKEY hKey, WINPR_ATTR_UNUSED LPCWSTR lpValueName,
WINPR_ATTR_UNUSED DWORD Reserved, WINPR_ATTR_UNUSED DWORD dwType,
WINPR_ATTR_UNUSED const BYTE* lpData, WINPR_ATTR_UNUSED DWORD cbData)
{
WLog_ERR(TAG, "TODO: Implement");
return -1;
}
LONG RegSetValueExA(WINPR_ATTR_UNUSED HKEY hKey, WINPR_ATTR_UNUSED LPCSTR lpValueName,
WINPR_ATTR_UNUSED DWORD Reserved, WINPR_ATTR_UNUSED DWORD dwType,
WINPR_ATTR_UNUSED const BYTE* lpData, WINPR_ATTR_UNUSED DWORD cbData)
{
WLog_ERR(TAG, "TODO: Implement");
return -1;
}
LONG RegUnLoadKeyW(WINPR_ATTR_UNUSED HKEY hKey, WINPR_ATTR_UNUSED LPCWSTR lpSubKey)
{
WLog_ERR(TAG, "TODO: Implement");
return -1;
}
LONG RegUnLoadKeyA(WINPR_ATTR_UNUSED HKEY hKey, WINPR_ATTR_UNUSED LPCSTR lpSubKey)
{
WLog_ERR(TAG, "TODO: Implement");
return -1;
}
#endif

View File

@@ -0,0 +1,572 @@
/**
* WinPR: Windows Portable Runtime
* Windows Registry (.reg file format)
*
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <winpr/config.h>
#include <winpr/path.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winpr/wtypes.h>
#include <winpr/string.h>
#include <winpr/assert.h>
#include <winpr/crt.h>
#include <winpr/file.h>
#include "registry_reg.h"
#include "../log.h"
#define TAG WINPR_TAG("registry")
struct reg_data_type
{
char* tag;
size_t length;
DWORD type;
};
static struct reg_data_type REG_DATA_TYPE_TABLE[] = { { "\"", 1, REG_SZ },
{ "dword:", 6, REG_DWORD },
{ "str:\"", 5, REG_SZ },
{ "str(2):\"", 8, REG_EXPAND_SZ },
{ "str(7):\"", 8, REG_MULTI_SZ },
{ "hex:", 4, REG_BINARY },
{ "hex(2):\"", 8, REG_EXPAND_SZ },
{ "hex(7):\"", 8, REG_MULTI_SZ },
{ "hex(b):\"", 8, REG_QWORD } };
static char* reg_data_type_string(DWORD type)
{
switch (type)
{
case REG_NONE:
return "REG_NONE";
case REG_SZ:
return "REG_SZ";
case REG_EXPAND_SZ:
return "REG_EXPAND_SZ";
case REG_BINARY:
return "REG_BINARY";
case REG_DWORD:
return "REG_DWORD";
case REG_DWORD_BIG_ENDIAN:
return "REG_DWORD_BIG_ENDIAN";
case REG_LINK:
return "REG_LINK";
case REG_MULTI_SZ:
return "REG_MULTI_SZ";
case REG_RESOURCE_LIST:
return "REG_RESOURCE_LIST";
case REG_FULL_RESOURCE_DESCRIPTOR:
return "REG_FULL_RESOURCE_DESCRIPTOR";
case REG_RESOURCE_REQUIREMENTS_LIST:
return "REG_RESOURCE_REQUIREMENTS_LIST";
case REG_QWORD:
return "REG_QWORD";
default:
return "REG_UNKNOWN";
}
}
static BOOL reg_load_start(Reg* reg)
{
char* buffer = nullptr;
INT64 file_size = 0;
WINPR_ASSERT(reg);
WINPR_ASSERT(reg->fp);
if (_fseeki64(reg->fp, 0, SEEK_END) != 0)
return FALSE;
file_size = _ftelli64(reg->fp);
if (_fseeki64(reg->fp, 0, SEEK_SET) != 0)
return FALSE;
reg->line = nullptr;
reg->next_line = nullptr;
if (file_size < 1)
return FALSE;
buffer = (char*)realloc(reg->buffer, (size_t)file_size + 2);
if (!buffer)
return FALSE;
reg->buffer = buffer;
if (fread(reg->buffer, (size_t)file_size, 1, reg->fp) != 1)
return FALSE;
reg->buffer[file_size] = '\n';
reg->buffer[file_size + 1] = '\0';
reg->next_line = strtok_s(reg->buffer, "\n", &reg->saveptr);
return TRUE;
}
static void reg_load_finish(Reg* reg)
{
if (!reg)
return;
if (reg->buffer)
{
free(reg->buffer);
reg->buffer = nullptr;
}
}
static RegVal* reg_load_value(const Reg* reg, RegKey* key)
{
const char* p[5] = WINPR_C_ARRAY_INIT;
size_t length = 0;
char* name = nullptr;
const char* type = nullptr;
const char* data = nullptr;
RegVal* value = nullptr;
WINPR_ASSERT(reg);
WINPR_ASSERT(key);
WINPR_ASSERT(reg->line);
p[0] = reg->line + 1;
p[1] = strstr(p[0], "\"=");
if (!p[1])
return nullptr;
p[2] = p[1] + 2;
type = p[2];
if (p[2][0] == '"')
p[3] = p[2];
else
p[3] = strchr(p[2], ':');
if (!p[3])
return nullptr;
data = p[3] + 1;
length = (size_t)(p[1] - p[0]);
if (length < 1)
goto fail;
name = (char*)calloc(length + 1, sizeof(char));
if (!name)
goto fail;
memcpy(name, p[0], length);
value = (RegVal*)calloc(1, sizeof(RegVal));
if (!value)
goto fail;
value->name = name;
value->type = REG_NONE;
for (size_t index = 0; index < ARRAYSIZE(REG_DATA_TYPE_TABLE); index++)
{
const struct reg_data_type* current = &REG_DATA_TYPE_TABLE[index];
WINPR_ASSERT(current->tag);
WINPR_ASSERT(current->length > 0);
WINPR_ASSERT(current->type != REG_NONE);
if (strncmp(type, current->tag, current->length) == 0)
{
value->type = current->type;
break;
}
}
switch (value->type)
{
case REG_DWORD:
{
unsigned long val = 0;
errno = 0;
val = strtoul(data, nullptr, 0);
if ((errno != 0) || (val > UINT32_MAX))
{
WLog_WARN(TAG, "%s::%s value %s invalid", key->name, value->name, data);
goto fail;
}
value->data.dword = (DWORD)val;
}
break;
case REG_QWORD:
{
unsigned long long val = 0;
errno = 0;
val = strtoull(data, nullptr, 0);
if ((errno != 0) || (val > UINT64_MAX))
{
WLog_WARN(TAG, "%s::%s value %s invalid", key->name, value->name, data);
goto fail;
}
value->data.qword = (UINT64)val;
}
break;
case REG_SZ:
{
char* start = strchr(data, '"');
if (!start)
goto fail;
/* Check for terminating quote, check it is the last symbol */
const size_t len = strlen(start);
char* end = strchr(start + 1, '"');
if (!end)
goto fail;
const intptr_t cmp = end - start + 1;
if ((cmp < 0) || (len != WINPR_ASSERTING_INT_CAST(size_t, cmp)))
goto fail;
if (start[0] == '"')
start++;
if (end[0] == '"')
end[0] = '\0';
value->data.string = _strdup(start);
if (!value->data.string)
goto fail;
}
break;
default:
WLog_ERR(TAG, "[%s] %s unimplemented format: %s", key->name, value->name,
reg_data_type_string(value->type));
break;
}
if (!key->values)
{
key->values = value;
}
else
{
RegVal* pValue = key->values;
while (pValue->next != nullptr)
{
pValue = pValue->next;
}
pValue->next = value;
value->prev = pValue;
}
return value;
fail:
free(value);
free(name);
return nullptr;
}
static BOOL reg_load_has_next_line(Reg* reg)
{
if (!reg)
return 0;
return (reg->next_line != nullptr) ? 1 : 0;
}
static char* reg_load_get_next_line(Reg* reg)
{
if (!reg)
return nullptr;
reg->line = reg->next_line;
reg->next_line = strtok_s(nullptr, "\n", &reg->saveptr);
reg->line_length = strlen(reg->line);
return reg->line;
}
static char* reg_load_peek_next_line(Reg* reg)
{
WINPR_ASSERT(reg);
return reg->next_line;
}
static void reg_insert_key(WINPR_ATTR_UNUSED Reg* reg, RegKey* key, RegKey* subkey)
{
char* name = nullptr;
char* path = nullptr;
char* save = nullptr;
WINPR_ASSERT(reg);
WINPR_ASSERT(key);
WINPR_ASSERT(subkey);
WINPR_ASSERT(subkey->name);
path = _strdup(subkey->name);
if (!path)
return;
name = strtok_s(path, "\\", &save);
while (name != nullptr)
{
if (strcmp(key->name, name) == 0)
{
if (save)
subkey->subname = _strdup(save);
/* TODO: free allocated memory in error case */
if (!subkey->subname)
{
free(path);
return;
}
}
name = strtok_s(nullptr, "\\", &save);
}
free(path);
}
static RegKey* reg_load_key(Reg* reg, RegKey* key)
{
char* p[2];
size_t length = 0;
RegKey* subkey = nullptr;
WINPR_ASSERT(reg);
WINPR_ASSERT(key);
WINPR_ASSERT(reg->line);
p[0] = reg->line + 1;
p[1] = strrchr(p[0], ']');
if (!p[1])
return nullptr;
subkey = (RegKey*)calloc(1, sizeof(RegKey));
if (!subkey)
return nullptr;
length = (size_t)(p[1] - p[0]);
subkey->name = (char*)malloc(length + 1);
if (!subkey->name)
{
free(subkey);
return nullptr;
}
memcpy(subkey->name, p[0], length);
subkey->name[length] = '\0';
while (reg_load_has_next_line(reg))
{
char* line = reg_load_peek_next_line(reg);
if (line[0] == '[')
break;
reg_load_get_next_line(reg);
if (reg->line[0] == '"')
{
reg_load_value(reg, subkey);
}
}
reg_insert_key(reg, key, subkey);
if (!key->subkeys)
{
key->subkeys = subkey;
}
else
{
RegKey* pKey = key->subkeys;
while (pKey->next != nullptr)
{
pKey = pKey->next;
}
pKey->next = subkey;
subkey->prev = pKey;
}
return subkey;
}
static void reg_load(Reg* reg)
{
reg_load_start(reg);
while (reg_load_has_next_line(reg))
{
reg_load_get_next_line(reg);
if (reg->line[0] == '[')
{
reg_load_key(reg, reg->root_key);
}
}
reg_load_finish(reg);
}
static void reg_unload_value(WINPR_ATTR_UNUSED Reg* reg, RegVal* value)
{
WINPR_ASSERT(reg);
WINPR_ASSERT(value);
switch (value->type)
{
case REG_SZ:
free(value->data.string);
break;
default:
break;
}
free(value);
}
static void reg_unload_key(Reg* reg, RegKey* key)
{
RegVal* pValue = nullptr;
WINPR_ASSERT(reg);
WINPR_ASSERT(key);
pValue = key->values;
while (pValue != nullptr)
{
RegVal* pValueNext = pValue->next;
reg_unload_value(reg, pValue);
pValue = pValueNext;
}
free(key->name);
free(key);
}
static void reg_unload(Reg* reg)
{
WINPR_ASSERT(reg);
if (reg->root_key)
{
RegKey* pKey = reg->root_key->subkeys;
while (pKey != nullptr)
{
RegKey* pKeyNext = pKey->next;
reg_unload_key(reg, pKey);
pKey = pKeyNext;
}
free(reg->root_key);
}
}
Reg* reg_open(BOOL read_only)
{
Reg* reg = (Reg*)calloc(1, sizeof(Reg));
if (!reg)
return nullptr;
reg->read_only = read_only;
reg->filename = winpr_GetConfigFilePath(TRUE, "HKLM.reg");
if (!reg->filename)
goto fail;
if (reg->read_only)
reg->fp = winpr_fopen(reg->filename, "r");
else
{
reg->fp = winpr_fopen(reg->filename, "r+");
if (!reg->fp)
reg->fp = winpr_fopen(reg->filename, "w+");
}
if (!reg->fp)
goto fail;
reg->root_key = (RegKey*)calloc(1, sizeof(RegKey));
if (!reg->root_key)
goto fail;
reg->root_key->values = nullptr;
reg->root_key->subkeys = nullptr;
reg->root_key->name = "HKEY_LOCAL_MACHINE";
reg_load(reg);
return reg;
fail:
reg_close(reg);
return nullptr;
}
void reg_close(Reg* reg)
{
if (reg)
{
reg_unload(reg);
if (reg->fp)
(void)fclose(reg->fp);
free(reg->filename);
free(reg);
}
}
const char* reg_type_string(DWORD type)
{
switch (type)
{
case REG_NONE:
return "REG_NONE";
case REG_SZ:
return "REG_SZ";
case REG_EXPAND_SZ:
return "REG_EXPAND_SZ";
case REG_BINARY:
return "REG_BINARY";
case REG_DWORD:
return "REG_DWORD";
case REG_DWORD_BIG_ENDIAN:
return "REG_DWORD_BIG_ENDIAN";
case REG_LINK:
return "REG_LINK";
case REG_MULTI_SZ:
return "REG_MULTI_SZ";
case REG_RESOURCE_LIST:
return "REG_RESOURCE_LIST";
case REG_FULL_RESOURCE_DESCRIPTOR:
return "REG_FULL_RESOURCE_DESCRIPTOR";
case REG_RESOURCE_REQUIREMENTS_LIST:
return "REG_RESOURCE_REQUIREMENTS_LIST";
case REG_QWORD:
return "REG_QWORD";
default:
return "REG_UNKNOWN";
}
}

View File

@@ -0,0 +1,73 @@
/**
* WinPR: Windows Portable Runtime
* Windows Registry (.reg file format)
*
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef REGISTRY_REG_H_
#define REGISTRY_REG_H_
#include <winpr/registry.h>
typedef struct s_reg Reg;
typedef struct s_reg_key RegKey;
typedef struct s_reg_val RegVal;
struct s_reg
{
FILE* fp;
char* line;
char* next_line;
size_t line_length;
char* buffer;
char* filename;
BOOL read_only;
RegKey* root_key;
char* saveptr;
};
struct s_reg_val
{
char* name;
DWORD type;
RegVal* prev;
RegVal* next;
union reg_data
{
DWORD dword;
UINT64 qword;
char* string;
} data;
};
struct s_reg_key
{
char* name;
DWORD type;
RegKey* prev;
RegKey* next;
char* subname;
RegVal* values;
RegKey* subkeys;
};
Reg* reg_open(BOOL read_only);
void reg_close(Reg* reg);
const char* reg_type_string(DWORD type);
#endif