Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
3.5 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996 - 1999
  5. //
  6. // File: reg.cpp
  7. //
  8. // Contents: certsrv setup reg apis
  9. //
  10. //---------------------------------------------------------------------------
  11. #include <pch.cpp>
  12. #pragma hdrstop
  13. #include <assert.h>
  14. #include <shlwapi.h>
  15. #define __dwFILE__ __dwFILE_OCMSETUP_REG_CPP__
  16. DWORD
  17. mySHCopyKey(
  18. IN HKEY hkeySrc,
  19. IN LPCWSTR wszSrcSubKey,
  20. IN HKEY hkeyDest,
  21. IN DWORD fReserved)
  22. {
  23. DWORD err;
  24. err = S_OK;
  25. __try
  26. {
  27. err = SHCopyKey(hkeySrc, wszSrcSubKey, hkeyDest, fReserved);
  28. _LeaveIfError(err, "SHCopyKey");
  29. }
  30. __except(err = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  31. {
  32. }
  33. return(err);
  34. }
  35. DWORD
  36. mySHDeleteKey(
  37. IN HKEY hkey,
  38. IN LPCWSTR pszSubKey)
  39. {
  40. DWORD err;
  41. err = S_OK;
  42. __try
  43. {
  44. err = SHDeleteKey(hkey, pszSubKey);
  45. _LeaveIfError(err, "SHDeleteKey");
  46. }
  47. __except(err = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  48. {
  49. }
  50. return(err);
  51. }
  52. LONG
  53. myRegRenameKey(
  54. HKEY hKey, // handle to an open key
  55. LPCTSTR lpSrcKey, // address of old name of subkey
  56. LPCTSTR lpDesKey, // address of new name of subkey
  57. PHKEY phkResult) // address of buffer for opened handle of new subkey
  58. {
  59. LONG lerr;
  60. HKEY hDesKey = NULL;
  61. if (NULL == lpSrcKey || NULL == lpDesKey)
  62. {
  63. lerr = ERROR_INVALID_PARAMETER;
  64. goto error;
  65. }
  66. // open destination key sure it doesn't exist
  67. lerr = RegOpenKeyEx(
  68. hKey,
  69. lpDesKey,
  70. 0,
  71. KEY_ALL_ACCESS,
  72. &hDesKey);
  73. if (ERROR_SUCCESS == lerr)
  74. {
  75. // destination exists, stop
  76. lerr = ERROR_FILE_EXISTS;
  77. goto error;
  78. }
  79. else if (ERROR_FILE_NOT_FOUND != lerr)
  80. {
  81. goto error;
  82. }
  83. assert(NULL == hDesKey);
  84. lerr = RegCreateKeyEx(
  85. hKey,
  86. lpDesKey,
  87. 0,
  88. NULL,
  89. REG_OPTION_NON_VOLATILE,
  90. KEY_ALL_ACCESS,
  91. NULL,
  92. &hDesKey,
  93. NULL);
  94. if (ERROR_SUCCESS != lerr)
  95. {
  96. goto error;
  97. }
  98. lerr = mySHCopyKey(hKey, lpSrcKey, hDesKey, 0);
  99. if (ERROR_SUCCESS != lerr)
  100. {
  101. goto error;
  102. }
  103. lerr = mySHDeleteKey(hKey, lpSrcKey);
  104. if (ERROR_SUCCESS != lerr)
  105. {
  106. goto error;
  107. }
  108. if (NULL != phkResult)
  109. {
  110. *phkResult = hDesKey;
  111. hDesKey = NULL;
  112. }
  113. // done
  114. lerr = ERROR_SUCCESS;
  115. error:
  116. if (NULL != hDesKey)
  117. {
  118. RegCloseKey(hDesKey);
  119. }
  120. return lerr;
  121. }
  122. HRESULT
  123. myRenameCertRegKey(
  124. IN WCHAR const *pwszSrcCAName,
  125. IN WCHAR const *pwszDesCAName)
  126. {
  127. HRESULT hr;
  128. WCHAR *pwszSrcPath = NULL;
  129. WCHAR *pwszDesPath = NULL;
  130. if (0 == mylstrcmpiL(pwszSrcCAName, pwszDesCAName))
  131. {
  132. // destination is the same as source, done
  133. goto done;
  134. }
  135. hr = myFormCertRegPath(pwszSrcCAName, NULL, NULL, TRUE, &pwszSrcPath);
  136. _JumpIfError(hr, error, "formCertRegPath");
  137. hr = myFormCertRegPath(pwszDesCAName, NULL, NULL, TRUE, &pwszDesPath);
  138. _JumpIfError(hr, error, "formCertRegPath");
  139. hr = myRegRenameKey(
  140. HKEY_LOCAL_MACHINE,
  141. pwszSrcPath,
  142. pwszDesPath,
  143. NULL);
  144. if ((HRESULT) ERROR_SUCCESS != hr)
  145. {
  146. hr = HRESULT_FROM_WIN32(hr);
  147. _JumpError(hr, error, "myRegMoveKey");
  148. }
  149. done:
  150. hr = S_OK;
  151. error:
  152. if (NULL != pwszSrcPath)
  153. {
  154. LocalFree(pwszSrcPath);
  155. }
  156. if (NULL != pwszDesPath)
  157. {
  158. LocalFree(pwszDesPath);
  159. }
  160. return hr;
  161. }