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.

450 lines
9.6 KiB

  1. /*++
  2. Copyright (c) 1999-2000 Microsoft Corporation
  3. Module Name:
  4. cleanup.c
  5. Abstract:
  6. This module will cleanup registry with copied entry.
  7. Author:
  8. ATM Shafiqul Khalid (askhalid) 16-Feb-2000
  9. Revision History:
  10. --*/
  11. #include <windows.h>
  12. #include <windef.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include "wow64reg.h"
  16. #include <assert.h>
  17. #include "reflectr.h"
  18. DWORD
  19. DeleteValueFromSrc (
  20. HKEY SrcKey
  21. )
  22. /*++
  23. Routine Description:
  24. Delete Wow6432Value key from the node.
  25. Arguments:
  26. SrcKey - Handle to the src Key.
  27. Return Value:
  28. TRUE if everything under the has been deleted.
  29. FALSE otherwise.
  30. --*/
  31. {
  32. DWORD dwIndex =0;
  33. DWORD Ret;
  34. HKEY hKey;
  35. WCHAR Node[_MAX_PATH];
  36. //Delete wow6432Value Key
  37. if (RegDeleteValue( SrcKey, (LPCWSTR )WOW6432_VALUE_KEY_NAME) != ERROR_SUCCESS) {
  38. //Wow64RegDbgPrint (("\nSorry! couldn't remove wow6432 value key or it doesn't exist"))
  39. }
  40. for (;;) {
  41. DWORD Len = sizeof (Node)/sizeof (WCHAR);
  42. Ret = RegEnumKey(
  43. SrcKey,
  44. dwIndex,
  45. Node,
  46. Len
  47. );
  48. if (Ret != ERROR_SUCCESS)
  49. break;
  50. dwIndex++;
  51. Ret = RegOpenKeyEx(SrcKey, Node , 0, KEY_ALL_ACCESS, &hKey);
  52. if (Ret != ERROR_SUCCESS) {
  53. continue;
  54. }
  55. DeleteValueFromSrc (hKey);
  56. RegCloseKey (hKey);
  57. }
  58. return TRUE;
  59. }
  60. DWORD
  61. DeleteKey (
  62. HKEY DestKey,
  63. WCHAR *pKeyName,
  64. DWORD mode
  65. )
  66. /*++
  67. Routine Description:
  68. Delete key from the destination node if that was a copy from src node.
  69. Arguments:
  70. DestKey - Handle to the dest Key.
  71. pKeyName - Key to delete
  72. mode - deletion mode.
  73. ==> 0 default remove only copy key.
  74. ==> 1 remove all key disregarding copy attribute.
  75. Return Value:
  76. TRUE if everything under the has been deleted.
  77. FALSE otherwise.
  78. --*/
  79. {
  80. DWORD dwIndex =0;
  81. DWORD Ret;
  82. HKEY hKey;
  83. WCHAR Node[_MAX_PATH];
  84. BOOL bDeleteNode = FALSE;
  85. BOOL bEmptyNode = TRUE; // hope that it can delete everything
  86. WOW6432_VALUEKEY ValueDest;
  87. Ret = RegOpenKeyEx(DestKey, pKeyName , 0, KEY_ALL_ACCESS, &hKey);
  88. if (Ret != ERROR_SUCCESS) {
  89. return Ret;
  90. }
  91. GetWow6432ValueKey ( hKey, &ValueDest);
  92. if ( ValueDest.ValueType == Copy || mode == 1 ) {
  93. //delete all the values
  94. bDeleteNode = TRUE; //don't delete this node
  95. }
  96. // delete all the subkeys enumerate and delete
  97. for (;;) {
  98. DWORD Len = sizeof (Node)/sizeof (Node[0]);
  99. Ret = RegEnumKey(
  100. hKey,
  101. dwIndex,
  102. Node,
  103. Len
  104. );
  105. if (Ret != ERROR_SUCCESS)
  106. break;
  107. dwIndex++;
  108. if ( !wcscmp (Node, (LPCWSTR )NODE_NAME_32BIT) )
  109. continue;
  110. if (!DeleteKey (hKey, Node, mode )) {
  111. bEmptyNode = FALSE; // sorry couldn't delete everything
  112. dwIndex--; //skip the node
  113. }
  114. }
  115. RegCloseKey (hKey);
  116. //now delete the dest key.
  117. if (bDeleteNode) {
  118. if ( RegDeleteKey ( DestKey, pKeyName) == ERROR_SUCCESS)
  119. return ERROR_SUCCESS;
  120. }
  121. return -1; //unpredictable error
  122. }
  123. BOOL
  124. DeleteAll (
  125. PWCHAR Parent,
  126. PWCHAR SubNodeName,
  127. DWORD Mode,
  128. DWORD option //one means delete discarding wow6432valuekey
  129. )
  130. /*++
  131. Routine Description:
  132. Validate node. if node exist skip if doesn't then create the node and then return.
  133. Arguments:
  134. Parent - Name of the parent.
  135. SubNodeName - Name of the node under parent that need to be deleted.
  136. Mode - 0 means Subnode is under the parent.
  137. 1 means there were wild card and the subnode is under all key under parent.
  138. option - 0 means check wow6432valuekey for copy
  139. 1 means delete discarding wow6432valuekey
  140. Return Value:
  141. TRUE if the function succeed.
  142. FALSE otherwise.
  143. --*/
  144. {
  145. PWCHAR SplitLoc;
  146. DWORD Ret;
  147. WCHAR TempIsnNode1[_MAX_PATH];
  148. WCHAR TempIsnNode2[_MAX_PATH];
  149. if (SubNodeName == NULL) {
  150. WCHAR *p ;
  151. HKEY hKey;
  152. wcscpy (TempIsnNode1, Parent);
  153. p = &TempIsnNode1[wcslen(TempIsnNode1)];
  154. Wow64RegDbgPrint ( ("\nDeleting Key %S......", TempIsnNode1) );
  155. while ( p != TempIsnNode1 && *p !=L'\\') p--;
  156. if ( p != TempIsnNode1 ) {
  157. *p=UNICODE_NULL;
  158. hKey = OpenNode (TempIsnNode1);
  159. if ( hKey == NULL ){
  160. Wow64RegDbgPrint ( ("\nSorry! Couldn't open the key [%S]",TempIsnNode1));
  161. return FALSE;
  162. }
  163. DeleteKey (hKey, p+1, 1);
  164. DeleteValueFromSrc (hKey);
  165. RegCloseKey (hKey);
  166. }
  167. return TRUE; //empty node under parent
  168. }
  169. if (SubNodeName[0] == UNICODE_NULL)
  170. return TRUE;
  171. if ( Mode == 1) {
  172. HKEY Key = OpenNode (Parent);
  173. //
  174. // loop through all the subkey under parent
  175. //
  176. DWORD dwIndex =0;
  177. for (;;) {
  178. DWORD Len = sizeof ( TempIsnNode1 )/sizeof (WCHAR);
  179. TempIsnNode1 [0]=UNICODE_NULL;
  180. Ret = RegEnumKey(
  181. Key,
  182. dwIndex,
  183. TempIsnNode1,
  184. Len
  185. );
  186. if (Ret != ERROR_SUCCESS)
  187. break;
  188. if (Parent[0] != UNICODE_NULL) {
  189. wcscpy ( TempIsnNode2, Parent);
  190. wcscat (TempIsnNode2, (LPCWSTR )L"\\");
  191. wcscat (TempIsnNode2, TempIsnNode1);
  192. } else wcscpy (TempIsnNode2, TempIsnNode1);
  193. DeleteAll (TempIsnNode2, SubNodeName, 0, option);
  194. dwIndex++;
  195. }
  196. RegCloseKey (Key);
  197. return TRUE;
  198. }
  199. //
  200. // No wild card here
  201. //
  202. if ( ( SplitLoc = wcschr (SubNodeName, L'*') ) == NULL ) {
  203. if (Parent[0] != UNICODE_NULL) {
  204. wcscpy ( TempIsnNode2, Parent);
  205. wcscat (TempIsnNode2, (LPCWSTR )L"\\");
  206. wcscat (TempIsnNode2, SubNodeName);
  207. } else
  208. wcscpy (TempIsnNode2, SubNodeName);
  209. DeleteAll (TempIsnNode2, NULL, 0, option);
  210. return TRUE;
  211. }
  212. assert ( *(SplitLoc-1) == L'\\');
  213. *(SplitLoc-1) = UNICODE_NULL;
  214. SplitLoc++;
  215. if (*SplitLoc == L'\\')
  216. SplitLoc++;
  217. if (Parent[0] != UNICODE_NULL) {
  218. wcscat (Parent, (LPCWSTR )L"\\");
  219. wcscat (Parent, SubNodeName);
  220. } else
  221. wcscpy (Parent, SubNodeName);
  222. DeleteAll (Parent, SplitLoc, 1, option); //mode 1 means loop within all
  223. return TRUE;
  224. //for any wildcard split the string
  225. }
  226. //Cleanup program
  227. BOOL
  228. CleanupTable ()
  229. /*++
  230. Routine Description:
  231. Cleanup main table containing ISN node list.
  232. Arguments:
  233. None.
  234. Return Value:
  235. TRUE if the table has been removed successfully.
  236. FALSE otherwise.
  237. --*/
  238. {
  239. HKEY hKey;
  240. DWORD Ret;
  241. //
  242. // take action to remove registry entry
  243. //
  244. Ret = RegOpenKeyEx(
  245. HKEY_LOCAL_MACHINE,
  246. (LPCWSTR ) WOW64_REGISTRY_SETUP_KEY_NAME_REL_PARENT,
  247. 0,
  248. KEY_ALL_ACCESS,
  249. &hKey
  250. );
  251. if (Ret == ERROR_SUCCESS ) {
  252. Ret = RegDeleteKey (hKey, (LPCWSTR)WOW64_REGISTRY_ISN_NODE_NAME );
  253. if ( Ret == ERROR_SUCCESS) {
  254. Wow64RegDbgPrint ( ("\nSuccessfully removed ISN Table entry "));
  255. }
  256. RegCloseKey (hKey);
  257. }
  258. return TRUE;
  259. }
  260. BOOL
  261. CleanpRegistry ()
  262. /*++
  263. Routine Description:
  264. Cleanup Registry.
  265. Arguments:
  266. None.
  267. Return Value:
  268. TRUE if everything under the has been deleted.
  269. FALSE otherwise.
  270. --*/
  271. {
  272. extern ISN_NODE_TYPE *RedirectorTable;
  273. DWORD dwIndex;
  274. HKEY hKey;
  275. WCHAR TempIsnNode[256];
  276. WCHAR IsnNode[256];
  277. //
  278. // Initialize the table first then delete the table
  279. //
  280. InitializeIsnTable ();
  281. for ( dwIndex=0;dwIndex<wcslen (RedirectorTable[dwIndex].NodeValue);dwIndex++) {
  282. IsnNode[0] = UNICODE_NULL;
  283. wcscpy (TempIsnNode , RedirectorTable[dwIndex].NodeValue);
  284. wcscat (TempIsnNode , (LPCWSTR )L"\\");
  285. wcscat (TempIsnNode, (LPCWSTR )NODE_NAME_32BIT);
  286. Wow64RegDbgPrint ( ("\nDeleting Key %S==>%S", IsnNode, TempIsnNode));
  287. if (wcschr(TempIsnNode, L'*') != NULL ) { //wildcard exist
  288. DeleteAll ( IsnNode,
  289. TempIsnNode,
  290. 0,
  291. 1
  292. );
  293. } else {
  294. hKey = OpenNode (RedirectorTable[dwIndex].NodeValue);
  295. if ( hKey == NULL ){
  296. Wow64RegDbgPrint ( ("\nSorry! Couldn't open the key [%S]",RedirectorTable[dwIndex].NodeValue));
  297. continue;
  298. }
  299. DeleteKey (hKey, NODE_NAME_32BIT, 1);
  300. DeleteValueFromSrc (hKey);
  301. RegCloseKey (hKey);
  302. }
  303. }
  304. CleanupTable ();
  305. return TRUE;
  306. };