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.

377 lines
9.4 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. regops.c
  5. Abstract:
  6. Routines that manage the merging of registry keys. Given a key
  7. and a value, these functions allow the user to perform the same
  8. types of actions as those specified in usermig.inf and wkstamig.inf
  9. (i.e.: Copying, Suppressing, and Forcing various registry keys to be
  10. merged into the NT registry.)
  11. Routines:
  12. Author:
  13. Marc R. Whitten (marcw) 01-Aug-1997
  14. Revision History:
  15. Jim Schmidt (jimschm) 25-Mar-1998 Updated to properly support
  16. tree notation, fixed value suppression
  17. bug.
  18. --*/
  19. #include "pch.h"
  20. #include "memdbp.h"
  21. #define DBG_REGOPS "RegOps"
  22. /*++
  23. Routine Description:
  24. IsRegObjectMarkedForOperation builds an encoded key, escaping multi-byte
  25. characters and syntax characters, and then performs a MemDb lookup to see
  26. if the object is marked with the bit specified by OperationMask. A set of
  27. macros are built on top of this routine in regops.h.
  28. Arguments:
  29. Key - Specifies the unencoded registry key, with abriviated roots
  30. (i.e., HKLM\Software\Foo)
  31. Value - Specifies the registry key value name
  32. TreeState - Specifies KEY_ONLY to query against the key and optional
  33. value, KEY_TREE to query against the key with a star at the
  34. end, or TREE_OPTIONAL to query both.
  35. OperationMask - Specifies an operation mask. See merge.h.
  36. Return Value:
  37. TRUE if the key is in MemDb, or FALSE if it is not.
  38. --*/
  39. BOOL
  40. IsRegObjectMarkedForOperationA (
  41. IN PCSTR Key,
  42. IN PCSTR Value, OPTIONAL
  43. IN TREE_STATE TreeState,
  44. IN DWORD OperationMask
  45. )
  46. {
  47. PCSTR regObject;
  48. BOOL rIsMarked = FALSE;
  49. DWORD value;
  50. MYASSERT (TreeState != KEY_TREE || !Value);
  51. if (TreeState == KEY_ONLY || TreeState == TREE_OPTIONAL) {
  52. regObject = CreateEncodedRegistryStringExA(Key,Value,FALSE);
  53. if (MemDbGetStoredEndPatternValueA(regObject,&value)) {
  54. rIsMarked = (value & OperationMask) == OperationMask;
  55. }
  56. FreeEncodedRegistryStringA(regObject);
  57. }
  58. if (!rIsMarked && TreeState == KEY_TREE || TreeState == TREE_OPTIONAL && !Value) {
  59. regObject = CreateEncodedRegistryStringExA(Key,Value,TRUE);
  60. if (MemDbGetStoredEndPatternValueA(regObject,&value)) {
  61. rIsMarked = (value & OperationMask) == OperationMask;
  62. }
  63. FreeEncodedRegistryStringA(regObject);
  64. }
  65. return rIsMarked;
  66. }
  67. BOOL
  68. IsRegObjectMarkedForOperationW (
  69. IN PCWSTR Key,
  70. IN PCWSTR Value, OPTIONAL
  71. IN TREE_STATE TreeState,
  72. IN DWORD OperationMask
  73. )
  74. {
  75. PCWSTR regObject;
  76. BOOL rIsMarked = FALSE;
  77. DWORD value;
  78. MYASSERT (TreeState != KEY_TREE || !Value);
  79. if (TreeState == KEY_ONLY || TreeState == TREE_OPTIONAL) {
  80. regObject = CreateEncodedRegistryStringExW(Key,Value,FALSE);
  81. if (MemDbGetStoredEndPatternValueW(regObject,&value)) {
  82. rIsMarked = (value & OperationMask) == OperationMask;
  83. }
  84. FreeEncodedRegistryStringW(regObject);
  85. }
  86. if (!rIsMarked && TreeState == KEY_TREE || TreeState == TREE_OPTIONAL && !Value) {
  87. regObject = CreateEncodedRegistryStringExW(Key,Value,TRUE);
  88. if (MemDbGetStoredEndPatternValueW(regObject,&value)) {
  89. rIsMarked = (value & OperationMask) == OperationMask;
  90. }
  91. FreeEncodedRegistryStringW(regObject);
  92. }
  93. return rIsMarked;
  94. }
  95. /*++
  96. Routine Description:
  97. MarkRegObjectForOperation creates an encoded string and sets the operation
  98. bit in memdb. This routine is used to suppress operations from occurring
  99. on a registry key, registry value, or registry key tree.
  100. Arguments:
  101. Key - Specifies an unencoded registry key, with abriviated root
  102. (i.e., HKLM\Software\Foo).
  103. Value - Specifies the registry key value name.
  104. Tree - Specifies TRUE if Key specifies an entire registry key tree
  105. (in which case Value must be NULL), or FALSE if Key
  106. specifies a key that has different behavior for its subkeys.
  107. OperationMask - Specifies the suppression operation. See merge.h.
  108. Return Value:
  109. TRUE if the set was successful.
  110. --*/
  111. BOOL
  112. MarkRegObjectForOperationA (
  113. IN PCSTR Key,
  114. IN PCSTR Value, OPTIONAL
  115. IN BOOL Tree,
  116. IN DWORD OperationMask
  117. )
  118. {
  119. PCSTR regObject;
  120. BOOL rSuccess = TRUE;
  121. if (Tree && Value) {
  122. Tree = FALSE;
  123. }
  124. regObject = CreateEncodedRegistryStringExA(Key,Value,Tree);
  125. rSuccess = MarkObjectForOperationA (regObject, OperationMask);
  126. FreeEncodedRegistryStringA(regObject);
  127. return rSuccess;
  128. }
  129. BOOL
  130. MarkRegObjectForOperationW (
  131. IN PCWSTR Key,
  132. IN PCWSTR Value, OPTIONAL
  133. IN BOOL Tree,
  134. IN DWORD OperationMask
  135. )
  136. {
  137. PCWSTR regObject;
  138. BOOL rSuccess = TRUE;
  139. if (Tree && Value) {
  140. Tree = FALSE;
  141. }
  142. regObject = CreateEncodedRegistryStringExW(Key,Value,Tree);
  143. rSuccess = MarkObjectForOperationW (regObject, OperationMask);
  144. FreeEncodedRegistryStringW(regObject);
  145. return rSuccess;
  146. }
  147. /*++
  148. Routine Description:
  149. MarkObjectForOperation sets operation bits on a specified registry object,
  150. unless operation bits have already been specified.
  151. Arguments:
  152. Object - Specifies the encoded registry object. See memdbdef.h for
  153. syntax (the HKLM or HKR categories).
  154. OperationMask - Specifies the suppression operation for the particular
  155. object.
  156. Return Value:
  157. TRUE if the set operation was successful, or FALSE if an operation was
  158. already specified.
  159. --*/
  160. BOOL
  161. MarkObjectForOperationA (
  162. IN PCSTR Object,
  163. IN DWORD OperationMask
  164. )
  165. {
  166. DWORD Value;
  167. if (MemDbGetValueA (Object, &Value)) {
  168. DEBUGMSG_IF ((
  169. Value == OperationMask,
  170. DBG_REGOPS,
  171. "%hs is already in memdb.",
  172. Object
  173. ));
  174. DEBUGMSG_IF ((
  175. Value != OperationMask,
  176. DBG_REGOPS,
  177. "%hs is already in memdb with different flags %u. New flags ignored: %u.",
  178. Object,
  179. Value,
  180. OperationMask
  181. ));
  182. return FALSE;
  183. }
  184. return MemDbSetValueA (Object, OperationMask);
  185. }
  186. BOOL
  187. MarkObjectForOperationW (
  188. IN PCWSTR Object,
  189. IN DWORD OperationMask
  190. )
  191. {
  192. DWORD Value;
  193. if (MemDbGetValueW (Object, &Value)) {
  194. DEBUGMSG_IF ((
  195. Value == OperationMask,
  196. DBG_REGOPS,
  197. "%ls is already in memdb.",
  198. Object
  199. ));
  200. DEBUGMSG_IF ((
  201. Value != OperationMask,
  202. DBG_REGOPS,
  203. "%ls is already in memdb with different flags %u. New flags ignored: %u.",
  204. Object,
  205. Value,
  206. OperationMask
  207. ));
  208. return FALSE;
  209. }
  210. return MemDbSetValueW (Object, OperationMask);
  211. }
  212. BOOL
  213. ForceWin9xSettingA (
  214. IN PCSTR SourceKey,
  215. IN PCSTR SourceValue,
  216. IN BOOL SourceTree,
  217. IN PCSTR DestinationKey,
  218. IN PCSTR DestinationValue,
  219. IN BOOL DestinationTree
  220. )
  221. {
  222. PCSTR regSource;
  223. CHAR keySource[MEMDB_MAX];
  224. PCSTR regDestination;
  225. CHAR keyDestination[MEMDB_MAX];
  226. DWORD offset = 0;
  227. BOOL rSuccess = TRUE;
  228. regSource = CreateEncodedRegistryStringExA (SourceKey, SourceValue, SourceTree);
  229. MemDbBuildKeyA (keySource, MEMDB_CATEGORY_FORCECOPYA, regSource, NULL, NULL);
  230. regDestination = CreateEncodedRegistryStringExA (DestinationKey, DestinationValue, DestinationTree);
  231. MemDbBuildKeyA (keyDestination, MEMDB_CATEGORY_FORCECOPYA, regDestination, NULL, NULL);
  232. rSuccess = MemDbSetValueExA (keyDestination, NULL, NULL, NULL, 0, &offset);
  233. if (rSuccess) {
  234. rSuccess = MemDbSetValueA (keySource, offset);
  235. }
  236. FreeEncodedRegistryStringA (regDestination);
  237. FreeEncodedRegistryStringA (regSource);
  238. return rSuccess;
  239. }
  240. BOOL
  241. ForceWin9xSettingW (
  242. IN PCWSTR SourceKey,
  243. IN PCWSTR SourceValue,
  244. IN BOOL SourceTree,
  245. IN PCWSTR DestinationKey,
  246. IN PCWSTR DestinationValue,
  247. IN BOOL DestinationTree
  248. )
  249. {
  250. PCWSTR regSource;
  251. WCHAR keySource[MEMDB_MAX];
  252. PCWSTR regDestination;
  253. WCHAR keyDestination[MEMDB_MAX];
  254. DWORD offset = 0;
  255. BOOL rSuccess = TRUE;
  256. regSource = CreateEncodedRegistryStringExW (SourceKey, SourceValue, SourceTree);
  257. MemDbBuildKeyW (keySource, MEMDB_CATEGORY_FORCECOPYW, regSource, NULL, NULL);
  258. regDestination = CreateEncodedRegistryStringExW (DestinationKey, DestinationValue, DestinationTree);
  259. MemDbBuildKeyW (keyDestination, MEMDB_CATEGORY_FORCECOPYW, regDestination, NULL, NULL);
  260. rSuccess = MemDbSetValueExW (keyDestination, NULL, NULL, NULL, 0, &offset);
  261. if (rSuccess) {
  262. rSuccess = MemDbSetValueW (keySource, offset);
  263. }
  264. FreeEncodedRegistryStringW (regDestination);
  265. FreeEncodedRegistryStringW (regSource);
  266. return rSuccess;
  267. }