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.

378 lines
10 KiB

  1. /*--------------------------------------------------------------------------
  2. *
  3. * Copyright (C) Cyclades Corporation, 2000-2001.
  4. * All rights reserved.
  5. *
  6. * Cyclades-Z Port Driver
  7. *
  8. * This file: cyzreg.c
  9. *
  10. * Description: This module contains the code that is used to get
  11. * values from the registry and to manipulate entries
  12. * in the registry.
  13. *
  14. * Notes: This code supports Windows 2000 and Windows XP,
  15. * x86 and IA64 processors.
  16. *
  17. * Complies with Cyclades SW Coding Standard rev 1.3.
  18. *
  19. *--------------------------------------------------------------------------
  20. */
  21. /*-------------------------------------------------------------------------
  22. *
  23. * Change History
  24. *
  25. *--------------------------------------------------------------------------
  26. * Initial implementation based on Microsoft sample code.
  27. *
  28. *--------------------------------------------------------------------------
  29. */
  30. #include "precomp.h"
  31. #ifdef ALLOC_PRAGMA
  32. #pragma alloc_text(INIT,CyzGetConfigDefaults)
  33. #pragma alloc_text(PAGESRP0,CyzGetRegistryKeyValue)
  34. #pragma alloc_text(PAGESRP0,CyzPutRegistryKeyValue)
  35. #endif // ALLOC_PRAGMA
  36. NTSTATUS
  37. CyzGetConfigDefaults(
  38. IN PCYZ_REGISTRY_DATA DriverDefaultsPtr,
  39. IN PUNICODE_STRING RegistryPath
  40. )
  41. /*++
  42. Routine Description:
  43. This routine reads the default configuration data from the
  44. registry for the serial driver.
  45. It also builds fields in the registry for several configuration
  46. options if they don't exist.
  47. Arguments:
  48. DriverDefaultsPtr - Pointer to a structure that will contain
  49. the default configuration values.
  50. RegistryPath - points to the entry for this driver in the
  51. current control set of the registry.
  52. Return Value:
  53. STATUS_SUCCESS if we got the defaults, otherwise we failed.
  54. The only way to fail this call is if the STATUS_INSUFFICIENT_RESOURCES.
  55. --*/
  56. {
  57. NTSTATUS Status = STATUS_SUCCESS; // return value
  58. //
  59. // We use this to query into the registry for defaults
  60. //
  61. RTL_QUERY_REGISTRY_TABLE paramTable[8];
  62. PWCHAR path;
  63. ULONG zero = 0;
  64. ULONG DbgDefault = 0;//SER_DBG_DEFAULT;
  65. ULONG notThereDefault = CYZ_UNINITIALIZED_DEFAULT;
  66. PAGED_CODE();
  67. //
  68. // Since the registry path parameter is a "counted" UNICODE string, it
  69. // might not be zero terminated. For a very short time allocate memory
  70. // to hold the registry path zero terminated so that we can use it to
  71. // delve into the registry.
  72. //
  73. // NOTE NOTE!!!! This is not an architected way of breaking into
  74. // a driver. It happens to work for this driver because the author
  75. // likes to do things this way.
  76. //
  77. path = ExAllocatePool (PagedPool, RegistryPath->Length+sizeof(WCHAR));
  78. if (!path) {
  79. Status = STATUS_INSUFFICIENT_RESOURCES;
  80. return (Status);
  81. }
  82. RtlZeroMemory (DriverDefaultsPtr, sizeof(CYZ_REGISTRY_DATA));
  83. RtlZeroMemory (&paramTable[0], sizeof(paramTable));
  84. RtlZeroMemory (path, RegistryPath->Length+sizeof(WCHAR));
  85. RtlMoveMemory (path, RegistryPath->Buffer, RegistryPath->Length);
  86. paramTable[0].Flags = RTL_QUERY_REGISTRY_DIRECT;
  87. paramTable[0].Name = L"BreakOnEntry";
  88. paramTable[0].EntryContext = &DriverDefaultsPtr->ShouldBreakOnEntry;
  89. paramTable[0].DefaultType = REG_DWORD;
  90. paramTable[0].DefaultData = &zero;
  91. paramTable[0].DefaultLength = sizeof(ULONG);
  92. paramTable[1].Flags = RTL_QUERY_REGISTRY_DIRECT;
  93. paramTable[1].Name = L"DebugLevel";
  94. paramTable[1].EntryContext = &DriverDefaultsPtr->DebugLevel;
  95. paramTable[1].DefaultType = REG_DWORD;
  96. paramTable[1].DefaultData = &DbgDefault;
  97. paramTable[1].DefaultLength = sizeof(ULONG);
  98. paramTable[2].Flags = RTL_QUERY_REGISTRY_DIRECT;
  99. paramTable[2].Name = L"PermitShare";
  100. paramTable[2].EntryContext = &DriverDefaultsPtr->PermitShareDefault;
  101. paramTable[2].DefaultType = REG_DWORD;
  102. paramTable[2].DefaultData = &notThereDefault;
  103. paramTable[2].DefaultLength = sizeof(ULONG);
  104. Status = RtlQueryRegistryValues( RTL_REGISTRY_ABSOLUTE | RTL_REGISTRY_OPTIONAL,
  105. path,
  106. &paramTable[0],
  107. NULL,
  108. NULL);
  109. if (!NT_SUCCESS(Status)) {
  110. DriverDefaultsPtr->ShouldBreakOnEntry = 0;
  111. DriverDefaultsPtr->DebugLevel = 0;
  112. }
  113. // TODO FANNY: SEE IF WE CAN ADD FIFO SIZE CONFIGURATION,
  114. // AS REQUESTED BY PLATFORM IN JAPAN.
  115. // Check to see if there was a forcefifo or an rxfifo size.
  116. // If there isn't then write out values so that they could
  117. // be adjusted later.
  118. //
  119. if (DriverDefaultsPtr->PermitShareDefault == notThereDefault) {
  120. DriverDefaultsPtr->PermitShareDefault = CYZ_PERMIT_SHARE_DEFAULT;
  121. //
  122. // Only share if the user actual changes switch.
  123. //
  124. RtlWriteRegistryValue(
  125. RTL_REGISTRY_ABSOLUTE,
  126. path,
  127. L"PermitShare",
  128. REG_DWORD,
  129. &DriverDefaultsPtr->PermitShareDefault,
  130. sizeof(ULONG)
  131. );
  132. }
  133. //
  134. // We don't need that path anymore.
  135. //
  136. if (path) {
  137. ExFreePool(path);
  138. }
  139. //
  140. // Set the defaults for other values
  141. //
  142. DriverDefaultsPtr->PermitSystemWideShare = FALSE;
  143. return (Status);
  144. }
  145. NTSTATUS
  146. CyzGetRegistryKeyValue (
  147. IN HANDLE Handle,
  148. IN PWCHAR KeyNameString,
  149. IN ULONG KeyNameStringLength,
  150. IN PVOID Data,
  151. IN ULONG DataLength
  152. )
  153. /*++
  154. Routine Description:
  155. Reads a registry key value from an already opened registry key.
  156. Arguments:
  157. Handle Handle to the opened registry key
  158. KeyNameString ANSI string to the desired key
  159. KeyNameStringLength Length of the KeyNameString
  160. Data Buffer to place the key value in
  161. DataLength Length of the data buffer
  162. Return Value:
  163. STATUS_SUCCESS if all works, otherwise status of system call that
  164. went wrong.
  165. --*/
  166. {
  167. UNICODE_STRING keyName;
  168. ULONG length;
  169. PKEY_VALUE_FULL_INFORMATION fullInfo;
  170. NTSTATUS ntStatus = STATUS_INSUFFICIENT_RESOURCES;
  171. PAGED_CODE();
  172. CyzDbgPrintEx(DPFLTR_TRACE_LEVEL, ">CyzGetRegistryKeyValue(XXX)\n");
  173. RtlInitUnicodeString (&keyName, KeyNameString);
  174. length = sizeof(KEY_VALUE_FULL_INFORMATION) + KeyNameStringLength
  175. + DataLength;
  176. fullInfo = ExAllocatePool(PagedPool, length);
  177. if (fullInfo) {
  178. ntStatus = ZwQueryValueKey (Handle,
  179. &keyName,
  180. KeyValueFullInformation,
  181. fullInfo,
  182. length,
  183. &length);
  184. if (NT_SUCCESS(ntStatus)) {
  185. //
  186. // If there is enough room in the data buffer, copy the output
  187. //
  188. if (DataLength >= fullInfo->DataLength) {
  189. RtlCopyMemory (Data,
  190. ((PUCHAR) fullInfo) + fullInfo->DataOffset,
  191. fullInfo->DataLength);
  192. }
  193. }
  194. ExFreePool(fullInfo);
  195. }
  196. CyzDbgPrintEx(DPFLTR_TRACE_LEVEL, "<CyzGetRegistryKeyValue %X\n",
  197. ntStatus);
  198. return ntStatus;
  199. }
  200. ULONG
  201. CyzGetRegistryKeyValueLength (
  202. IN HANDLE Handle,
  203. IN PWCHAR KeyNameString,
  204. IN ULONG KeyNameStringLength
  205. )
  206. /*++
  207. Routine Description:
  208. Reads a registry key value from an already opened registry key.
  209. Arguments:
  210. Handle Handle to the opened registry key
  211. KeyNameString ANSI string to the desired key
  212. KeyNameStringLength Length of the KeyNameString
  213. Return Value:
  214. ULONG Length of the key value
  215. --*/
  216. {
  217. UNICODE_STRING keyName;
  218. ULONG length;
  219. PKEY_VALUE_FULL_INFORMATION fullInfo;
  220. PAGED_CODE();
  221. RtlInitUnicodeString (&keyName, KeyNameString);
  222. length = sizeof(KEY_VALUE_FULL_INFORMATION) + KeyNameStringLength;
  223. fullInfo = ExAllocatePool(PagedPool, length);
  224. if (fullInfo) {
  225. ZwQueryValueKey (Handle,
  226. &keyName,
  227. KeyValueFullInformation,
  228. fullInfo,
  229. length,
  230. &length);
  231. ExFreePool(fullInfo);
  232. }
  233. return length;
  234. }
  235. NTSTATUS
  236. CyzPutRegistryKeyValue(IN HANDLE Handle, IN PWCHAR PKeyNameString,
  237. IN ULONG KeyNameStringLength, IN ULONG Dtype,
  238. IN PVOID PData, IN ULONG DataLength)
  239. /*++
  240. Routine Description:
  241. Writes a registry key value to an already opened registry key.
  242. Arguments:
  243. Handle Handle to the opened registry key
  244. PKeyNameString ANSI string to the desired key
  245. KeyNameStringLength Length of the KeyNameString
  246. Dtype REG_XYZ value type
  247. PData Buffer to place the key value in
  248. DataLength Length of the data buffer
  249. Return Value:
  250. STATUS_SUCCESS if all works, otherwise status of system call that
  251. went wrong.
  252. --*/
  253. {
  254. NTSTATUS status;
  255. UNICODE_STRING keyname;
  256. PAGED_CODE();
  257. CyzDbgPrintEx(DPFLTR_TRACE_LEVEL, ">CyzPutRegistryKeyValue(XXX)\n");
  258. RtlInitUnicodeString(&keyname, NULL);
  259. keyname.MaximumLength = (USHORT)(KeyNameStringLength + sizeof(WCHAR));
  260. keyname.Buffer = ExAllocatePool(PagedPool, keyname.MaximumLength);
  261. if (keyname.Buffer == NULL) {
  262. return STATUS_INSUFFICIENT_RESOURCES;
  263. }
  264. RtlAppendUnicodeToString(&keyname, PKeyNameString);
  265. status = ZwSetValueKey(Handle, &keyname, 0, Dtype, PData, DataLength);
  266. ExFreePool(keyname.Buffer);
  267. CyzDbgPrintEx(DPFLTR_TRACE_LEVEL, "<CyzPutRegistryKeyValue %X\n",
  268. status);
  269. return status;
  270. }