From f1d8915d1a512960fc3721be9362d0612cd465ce Mon Sep 17 00:00:00 2001 From: Ben Chalmers Date: Fri, 11 Oct 2013 08:51:13 +0100 Subject: [PATCH] [CP-6629] Copy installer keys from registry to newly installed device Signed-off-by: Ben Chalmers --- src/coinst/coinst.c | 347 ++++++++++++++++++++++++++++---------------- 1 file changed, 225 insertions(+), 122 deletions(-) diff --git a/src/coinst/coinst.c b/src/coinst/coinst.c index 0380fd8..163af13 100644 --- a/src/coinst/coinst.c +++ b/src/coinst/coinst.c @@ -41,6 +41,7 @@ #include #include #include +#include #include #include @@ -1234,9 +1235,6 @@ GetInstallerSettingsKeyName( { PTCHAR Location; HKEY InstallerKey; - DWORD MaxValueLength; - DWORD NameLength; - DWORD Type; HRESULT Error; Log("====>"); @@ -1262,44 +1260,7 @@ GetInstallerSettingsKeyName( goto fail2; } - Error = RegQueryInfoKey(InstallerKey, - NULL, - NULL, - NULL, - NULL, - NULL, - NULL, - NULL, - NULL, - &MaxValueLength, - NULL, - NULL); - if (Error != ERROR_SUCCESS) { - SetLastError(Error); - goto fail3; - } - - NameLength = MaxValueLength + sizeof (TCHAR); - - *Name = calloc(1, NameLength); - if (Name == NULL) - goto fail4; - - Error = RegQueryValueEx(InstallerKey, - Location, - NULL, - &Type, - (LPBYTE)*Name, - &NameLength); - if (Error != ERROR_SUCCESS) { - SetLastError(Error); - goto fail5; - } - - if (Type != REG_SZ) { - SetLastError(ERROR_BAD_FORMAT); - goto fail6; - } + *Name = Location; if (strlen(*Name) == 0) { free(*Name); @@ -1308,8 +1269,6 @@ GetInstallerSettingsKeyName( RegCloseKey(InstallerKey); - free(Location); - done: Log("%s", (*Name == NULL) ? "[NONE]" : *Name); @@ -1317,22 +1276,6 @@ done: return TRUE; -fail6: - Log("fail6"); - -fail5: - Log("fail5"); - - free(*Name); - -fail4: - Log("fail4"); - -fail3: - Log("fail3"); - - RegCloseKey(InstallerKey); - fail2: Log("fail2"); @@ -1353,7 +1296,8 @@ fail1: static HKEY OpenInstallerSettingsKey( - IN PTCHAR Name + IN PTCHAR Name, + IN PTCHAR SubKey ) { HRESULT Result; @@ -1363,9 +1307,10 @@ OpenInstallerSettingsKey( Result = StringCbPrintf(KeyName, MAX_PATH, - "%s\\%s", + "%s\\%s\\%s", INSTALLER_KEY, - Name); + Name, + SubKey); if (!SUCCEEDED(Result)) { SetLastError(ERROR_BUFFER_OVERFLOW); goto fail1; @@ -1469,7 +1414,7 @@ GetInterfaceName( SetLastError(ERROR_BAD_FORMAT); goto fail4; } - + Log("Got interface %s", RootDevice); RegCloseKey(LinkageKey); return RootDevice; @@ -1502,13 +1447,11 @@ fail1: } static BOOLEAN -CopyValues( - IN PTCHAR DestinationKeyName, - IN PTCHAR SourceKeyName +CopyKeyValues( + IN HKEY DestinationKey, + IN HKEY SourceKey ) { - HKEY DestinationKey; - HKEY SourceKey; HRESULT Error; DWORD Values; DWORD MaxNameLength; @@ -1517,33 +1460,6 @@ CopyValues( LPBYTE Value; DWORD Index; - Log("DESTINATION: %s", DestinationKeyName); - Log("SOURCE: %s", SourceKeyName); - - Error = RegCreateKeyEx(HKEY_LOCAL_MACHINE, - DestinationKeyName, - 0, - NULL, - REG_OPTION_NON_VOLATILE, - KEY_ALL_ACCESS, - NULL, - &DestinationKey, - NULL); - if (Error != ERROR_SUCCESS) { - SetLastError(Error); - goto fail1; - } - - Error = RegOpenKeyEx(HKEY_LOCAL_MACHINE, - SourceKeyName, - 0, - KEY_ALL_ACCESS, - &SourceKey); - if (Error != ERROR_SUCCESS) { - SetLastError(Error); - goto fail2; - } - Error = RegQueryInfoKey(SourceKey, NULL, NULL, @@ -1558,7 +1474,7 @@ CopyValues( NULL); if (Error != ERROR_SUCCESS) { SetLastError(Error); - goto fail3; + goto fail1; } if (Values == 0) @@ -1568,11 +1484,11 @@ CopyValues( Name = malloc(MaxNameLength); if (Name == NULL) - goto fail4; + goto fail2; Value = malloc(MaxValueLength); if (Value == NULL) - goto fail5; + goto fail3; for (Index = 0; Index < Values; Index++) { DWORD NameLength; @@ -1595,7 +1511,7 @@ CopyValues( &ValueLength); if (Error != ERROR_SUCCESS) { SetLastError(Error); - goto fail6; + goto fail4; } Error = RegSetValueEx(DestinationKey, @@ -1606,7 +1522,7 @@ CopyValues( ValueLength); if (Error != ERROR_SUCCESS) { SetLastError(Error); - goto fail7; + goto fail5; } Log("COPIED %s", Name); @@ -1621,25 +1537,79 @@ CopyValues( done: return TRUE; -fail7: - Log("fail7"); - -fail6: - Log("fail6"); - - free(Value); - fail5: Log("fail5"); - free(Name); - fail4: Log("fail4"); + free(Value); + fail3: Log("fail3"); + free(Name); + +fail2: + Log("fail2"); + +fail1: + Log("fail1"); + + Error = GetLastError(); + + { + PTCHAR Message; + + Message = __GetErrorMessage(Error); + Log("fail1 (%s)", Message); + LocalFree(Message); + } + + return FALSE; + +} + +static BOOLEAN +CopyValues( + IN PTCHAR DestinationKeyName, + IN PTCHAR SourceKeyName + ) +{ + HRESULT Error; + HKEY DestinationKey; + HKEY SourceKey; + + + Log("DESTINATION: %s", DestinationKeyName); + Log("SOURCE: %s", SourceKeyName); + + Error = RegCreateKeyEx(HKEY_LOCAL_MACHINE, + DestinationKeyName, + 0, + NULL, + REG_OPTION_NON_VOLATILE, + KEY_ALL_ACCESS, + NULL, + &DestinationKey, + NULL); + if (Error != ERROR_SUCCESS) { + SetLastError(Error); + goto fail1; + } + + Error = RegOpenKeyEx(HKEY_LOCAL_MACHINE, + SourceKeyName, + 0, + KEY_ALL_ACCESS, + &SourceKey); + if (Error != ERROR_SUCCESS) { + SetLastError(Error); + goto fail2; + } + + CopyKeyValues(DestinationKey, SourceKey); + RegCloseKey(SourceKey); fail2: @@ -1659,6 +1629,7 @@ fail1: } return FALSE; + } static BOOLEAN @@ -2135,6 +2106,66 @@ fail1: return FALSE; } +static HKEY +CreateFormattedKey( + IN LPCTSTR Format, + IN ... + ) +{ + + HRESULT Result; + TCHAR KeyName[MAX_PATH]; + HKEY Key; + HRESULT Error; + va_list Args; + + va_start(Args, Format); + + Result = StringCbVPrintf(KeyName, + MAX_PATH, + Format, + Args); + if (!SUCCEEDED(Result)) { + SetLastError(ERROR_BUFFER_OVERFLOW); + goto fail1; + } + + Error = RegCreateKeyEx(HKEY_LOCAL_MACHINE, + KeyName, + 0, + NULL, + REG_OPTION_NON_VOLATILE, + KEY_ALL_ACCESS, + NULL, + &Key, + NULL); + if (Error != ERROR_SUCCESS) { + Log("Unable to find key %s",KeyName); + SetLastError(Error); + goto fail2; + } + + va_end(Format); + + return Key; + +fail2: + Log("fail2"); + +fail1: + Error = GetLastError(); + + { + PTCHAR Message; + Message = __GetErrorMessage(Error); + Log("fail1 (%s)", Message); + LocalFree(Message); + } + + va_end(Format); + return NULL; +} + static BOOLEAN CopySettingsFromInstaller( IN HDEVINFO DeviceInfoSet, @@ -2142,36 +2173,108 @@ CopySettingsFromInstaller( IN PTCHAR Name ) { - HKEY Source; HKEY Destination; - BOOLEAN Success; HRESULT Error; - - Source = OpenInstallerSettingsKey(Name); - if (Source == NULL) - goto fail1; + PTCHAR DestinationName; + HKEY Tcpip6Dst; + HKEY TcpipDst; + HKEY NetbtDst; + HKEY Tcpip6Src; + HKEY TcpipSrc; + HKEY NetbtSrc; Destination = OpenSoftwareKey(DeviceInfoSet, DeviceInfoData); if (Destination == NULL) + goto fail1; + + DestinationName = GetInterfaceName(Destination); + if (DestinationName == NULL) goto fail2; - Success = CopySettings(Destination, Source); - if (!Success) + + NetbtSrc = OpenInstallerSettingsKey(Name, "nbt"); + if (NetbtSrc == NULL) goto fail3; + Log("Looking for destination %s", DestinationName); + NetbtDst = CreateFormattedKey(PARAMETERS_KEY(NetBT) "\\Interfaces\\Tcpip_%s", DestinationName); + if (NetbtDst == NULL) + goto fail4; + + if (!CopyKeyValues(NetbtDst, NetbtSrc)) + goto fail5; + + TcpipSrc = OpenInstallerSettingsKey(Name, "tcpip"); + if (TcpipSrc == NULL) + goto fail6; + + TcpipDst = CreateFormattedKey(PARAMETERS_KEY(Tcpip) "\\Interfaces\\%s", DestinationName); + if (TcpipDst == NULL) + goto fail7; + + if (!CopyKeyValues(TcpipDst, TcpipSrc)) + goto fail8; + + Tcpip6Src = OpenInstallerSettingsKey(Name, "tcpip6"); + if (Tcpip6Src == NULL) + goto fail9; + + Tcpip6Dst = CreateFormattedKey(PARAMETERS_KEY(Tcpip6) "\\Interfaces\\%s", DestinationName); + if (Tcpip6Dst == NULL) + goto fail10; + + if (!CopyKeyValues(Tcpip6Dst, Tcpip6Src)) + goto fail11; + + RegCloseKey(Tcpip6Dst); + RegCloseKey(Tcpip6Src); + RegCloseKey(TcpipDst); + RegCloseKey(TcpipSrc); + RegCloseKey(NetbtDst); + RegCloseKey(NetbtSrc); + free(DestinationName); + RegCloseKey(Destination); return TRUE; +fail11: + Log("fail11"); + RegCloseKey(Tcpip6Dst); + +fail10: + Log("fail10"); + RegCloseKey(TcpipSrc); + +fail9: + Log("fail9"); +fail8: + Log("fail8"); + RegCloseKey(TcpipDst); + +fail7: + Log("fail7"); + RegCloseKey(TcpipSrc); + +fail6: + Log("fail6"); +fail5: + Log("fail5"); + RegCloseKey(NetbtDst); + +fail4: + Log("fail4"); + RegCloseKey(NetbtSrc); + fail3: Log("fail3"); - - RegCloseKey(Destination); + free(DestinationName); fail2: Log("fail2"); - - RegCloseKey(Source); + RegCloseKey(Destination); fail1: + Log("fail1"); + Error = GetLastError(); { -- 2.39.5