Source code of Windows XP (NT5)
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.

581 lines
16 KiB

  1. /*++
  2. Copyright (c) 1991 Microsoft Corporation
  3. Module Name:
  4. registry.c
  5. Abstract:
  6. (This file has been copied from the temporary hack that BryanWi and
  7. ScottBi did in kernel mode. I saw no need to have it be in kernel
  8. mode and it had many bugs caused as a result of being in kernel mode,
  9. so I made it caller mode. Jim Kelly).
  10. This module represents a quick and dirty Nt level registry. Each key
  11. in the Registry is implemented as a file directory within a directory
  12. tree whose root is the directory "\Registry" on the system disk.
  13. A key's data is stored within a file called "Data.Reg" in the key's
  14. directory, and a key's attributes is stored as the file "Attr.Reg"
  15. within the directory.
  16. Author:
  17. Bryan M. Willman (bryanwi) 30-Apr-1991
  18. Scott Birrell (ScottBi) 6-Jun-1991
  19. Environment:
  20. callable from Kernel or user mode.
  21. Revision History:
  22. --*/
  23. #include "ntrtlp.h"
  24. #if defined(ALLOC_PRAGMA) && defined(NTOS_KERNEL_RUNTIME)
  25. #pragma alloc_text(PAGE,RtlpNtOpenKey)
  26. #pragma alloc_text(PAGE,RtlpNtCreateKey)
  27. #pragma alloc_text(PAGE,RtlpNtQueryValueKey)
  28. #pragma alloc_text(PAGE,RtlpNtSetValueKey)
  29. #pragma alloc_text(PAGE,RtlpNtMakeTemporaryKey)
  30. #pragma alloc_text(PAGE,RtlpNtEnumerateSubKey)
  31. #endif
  32. #define REG_INVALID_ATTRIBUTES (OBJ_EXCLUSIVE | OBJ_PERMANENT)
  33. //
  34. // Temporary Registry User APIs.
  35. //
  36. // NOTE: These are temporary implementations. Although there is no code
  37. // within that requires these API to be implemented as system services, the
  38. // eventual replacements for these routines will use the Object Manager and
  39. // hence require to be system services.
  40. //
  41. NTSTATUS
  42. RtlpNtOpenKey(
  43. OUT PHANDLE KeyHandle,
  44. IN ACCESS_MASK DesiredAccess,
  45. IN POBJECT_ATTRIBUTES ObjectAttributes,
  46. IN ULONG Options
  47. )
  48. /*++
  49. Routine Description:
  50. This function opens a key in the Registry. The key must already exist.
  51. Arguments:
  52. KeyHandle - Receives a value called a Handle which is used to access
  53. the specified key in the Registration Database.
  54. DesiredAccess - Specifies the Accesses desired
  55. REG_KEY_READ - Generic Read access to key
  56. REG_KEY_QUERY_VALUE - Query Key's value
  57. REG_KEY_WRITE - Generic Write access to key
  58. REG_KEY_SET_VALUE - Set Key's value
  59. ObjectAttributes - Specifies the attributes of the key being opened.
  60. Note that a key name must be specified. If a Root Directory
  61. is specified, the name is relative to the root. The name of the
  62. object must be within the name space allocated to the Registry, that
  63. is, all names beginning "\Registry". RootHandle, if present, must
  64. be a handle to "\", or "\Registry", or a key under "\Registry".
  65. Options - REG_OPTION_READ_FUZZY - Allow Read access on handle even if
  66. it is open for Read/Write access.
  67. Return Value:
  68. NTSTATUS - Result code from call. The following are returned
  69. STATUS_SUCCESS - The open was successful.
  70. STATUS_INVALID_PARAMETER - A parameter other that object name was
  71. invalid.
  72. STATUS_OBJECT_NAME_INVALID - The key name has invalid syntax
  73. STATUS_OBJECT_NAME_NOT_FOUND - No key of the given name exists
  74. STATUS_ACCESS_DENIED - Caller does not have the requested access
  75. to the specified key.
  76. --*/
  77. {
  78. RTL_PAGED_CODE();
  79. if (ARGUMENT_PRESENT(ObjectAttributes)) {
  80. ObjectAttributes->Attributes &= ~(REG_INVALID_ATTRIBUTES);
  81. }
  82. return( NtOpenKey( KeyHandle,
  83. DesiredAccess,
  84. ObjectAttributes
  85. ) );
  86. DBG_UNREFERENCED_PARAMETER( Options );
  87. }
  88. NTSTATUS
  89. RtlpNtCreateKey(
  90. OUT PHANDLE KeyHandle,
  91. IN ACCESS_MASK DesiredAccess,
  92. IN POBJECT_ATTRIBUTES ObjectAttributes,
  93. IN ULONG Options,
  94. IN PUNICODE_STRING Provider,
  95. OUT OPTIONAL PULONG Disposition
  96. )
  97. /*++
  98. Routine Description:
  99. This function creates or opens the specified key in the Registry. If
  100. the key does not exist, it is created. If the key already exists, it
  101. is opened.
  102. Arguments:
  103. KeyHandle - Receives a value called a Handle which is used to access
  104. the specified key in the Registration Database.
  105. DesiredAccess - Specifies the Accesses desired
  106. REG_KEY_READ - Generic Read access to key
  107. REG_KEY_QUERY_VALUE - Query Key's value
  108. REG_KEY_WRITE - Generic Write access to key
  109. REG_KEY_SET_VALUE - Set Key's value
  110. ObjectAttributes - Specifies the attributes of the key being opened.
  111. Note that a key name must be specified. If a Root Directory
  112. is specified, the name is relative to the root. The name of the
  113. object must be within the name space allocated to the Registry, that
  114. is, all names beginning "\Registry". RootHandle, if present, must
  115. be a handle to "\", or "\Registry", or a key under "\Registry".
  116. Options - REG_OPTION_READ_FUZZY - Allow Read access on handle even if it is
  117. open for READ_WRITE access.
  118. REG_OPTION_VOLATILE - Object is not to be stored across boots.
  119. Provider - This parameter is reserved for future use and must currently
  120. be set to NULL. It will be used in the future to specify the name of
  121. the provider to be used for operations on this node and its descendant
  122. nodes.
  123. Disposition - This optional parameter is a pointer to a variable that
  124. will receive a value indicating whether a new Registry key was
  125. created or an existing one opened.
  126. REG_CREATED_NEW_KEY - A new Registry Key was created
  127. REG_OPENED_EXISTING_KEY - An existing Registry Key was opened
  128. Return Value:
  129. NTSTATUS - Result code from call. The following are returned
  130. STATUS_SUCCESS - The open was successful.
  131. STATUS_INVALID_PARAMETER - A parameter other that object name was
  132. --*/
  133. {
  134. RTL_PAGED_CODE();
  135. if (ARGUMENT_PRESENT(ObjectAttributes)) {
  136. ObjectAttributes->Attributes &= ~(REG_INVALID_ATTRIBUTES);
  137. }
  138. return(NtCreateKey( KeyHandle,
  139. DesiredAccess,
  140. ObjectAttributes,
  141. 0, //TitleIndex
  142. NULL, //Class OPTIONAL,
  143. REG_OPTION_NON_VOLATILE, //CreateOptions,
  144. Disposition
  145. ) );
  146. DBG_UNREFERENCED_PARAMETER( Options );
  147. DBG_UNREFERENCED_PARAMETER( Provider );
  148. }
  149. NTSTATUS
  150. RtlpNtQueryValueKey(
  151. IN HANDLE KeyHandle,
  152. OUT OPTIONAL PULONG KeyValueType,
  153. OUT OPTIONAL PVOID KeyValue,
  154. IN OUT OPTIONAL PULONG KeyValueLength,
  155. OUT OPTIONAL PLARGE_INTEGER LastWriteTime
  156. )
  157. /*++
  158. Routine Description:
  159. This function queries the value of a key.
  160. Arguments:
  161. KeyHandle - Handle of a key opened for GENERIC_READ access via NtOpenKey.
  162. KeyValueType - Optional pointer to variable that will receive the
  163. client-defined type of the key value (if any). If no value has been
  164. set for the key, 0 is returned.
  165. KeyValue - Optional pointer to buffer in which part or all of the key's
  166. value (as set on the most recent call to NtSetValueKey) will be
  167. returned. If the key's value is too large to fit into the supplied
  168. buffer, as much of the value as will fit into the buffer will be
  169. returned and the warning STATUS_BUFFER_OVERFLOW is returned. If no
  170. value has ever been set, nothing is returned. If NULL is specified
  171. for this parameter, no Key Value is returned.
  172. KeyValueLength - On input, this optional parameter points to a variable
  173. that contains the length in bytes of the KeyValue buffer (if any). If
  174. no KeyValue buffer is specified, the variable content on entry is
  175. ignored. On return, the referenced variable (if any) receives the
  176. FULL length in bytes of the key value. If the key's value is too
  177. large to fit into the supplied buffer, as much of the value as will
  178. fit into the buffer will be returned and the warning
  179. STATUS_BUFFER_OVERFLOW is returned.
  180. The returned length is intended for use by calling code in allocating
  181. a buffer of sufficient size to hold the key's value. After receiving
  182. STATUS_BUFFER_OVERFLOW from NtQueryValueKey, calling code may make a
  183. subsequent call to NtQueryValueKey with a buffer of size equal to the
  184. length returned by the prior call.
  185. If no value has been set for the key, 0 is returned.
  186. LastWriteTime - Optional parameter to variable which receives a time stamp
  187. specifying the last time that the key was written.
  188. Return Value:
  189. NTSTATUS - Result code
  190. STATUS_SUCCESS - Call was successful
  191. STATUS_INVALID_PARAMETER - Invalid parameter
  192. STATUS_ACCESS_DENIED - Caller does not have GENERIC_READ access to
  193. the specified key
  194. STATUS_BUFFER_OVERFLOW - This is a warning that the key's value
  195. is too large for the buffer specified by the KeyValue and
  196. KeyValueLength parameters. Use the length returned to
  197. determine the size of buffer to allocate for a subsequent
  198. call of NtQueryValueKey.
  199. --*/
  200. {
  201. UNICODE_STRING NullName;
  202. NTSTATUS Status;
  203. PKEY_VALUE_PARTIAL_INFORMATION ValueInformation;
  204. ULONG ValueLength;
  205. RTL_PAGED_CODE();
  206. //
  207. // Compute the size of the buffer needed to hold the key value information.
  208. //
  209. ValueLength = 0;
  210. if (ARGUMENT_PRESENT(KeyValueLength)) {
  211. ValueLength = *KeyValueLength;
  212. }
  213. ValueLength += FIELD_OFFSET(KEY_VALUE_PARTIAL_INFORMATION, Data);
  214. ValueInformation = RtlAllocateHeap(RtlProcessHeap(), 0, ValueLength);
  215. if (ValueInformation == NULL) {
  216. return STATUS_INSUFFICIENT_RESOURCES;
  217. }
  218. //
  219. // Query the key value.
  220. //
  221. NullName.Length = 0;
  222. Status = NtQueryValueKey(KeyHandle,
  223. &NullName,
  224. KeyValuePartialInformation,
  225. ValueInformation,
  226. ValueLength,
  227. &ValueLength);
  228. //
  229. // Temporary hack to allow query of "" attribute when it hasn't
  230. // yet been set.
  231. //
  232. if (Status == STATUS_OBJECT_NAME_NOT_FOUND) {
  233. Status = STATUS_SUCCESS;
  234. ValueInformation->DataLength = 0;
  235. ValueInformation->Type = 0;
  236. }
  237. //
  238. // If requested return the key value length and the key type.
  239. //
  240. if (NT_SUCCESS(Status) || (Status == STATUS_BUFFER_OVERFLOW)) {
  241. if (ARGUMENT_PRESENT(KeyValueLength)) {
  242. *KeyValueLength = ValueInformation->DataLength;
  243. }
  244. if (ARGUMENT_PRESENT(KeyValueType)) {
  245. *KeyValueType = ValueInformation->Type;
  246. }
  247. }
  248. //
  249. // If the query was successful and buffer overflow did not occur, then
  250. // return the key value information.
  251. //
  252. if (NT_SUCCESS(Status) && ARGUMENT_PRESENT(KeyValue)) {
  253. RtlCopyMemory(KeyValue,
  254. &ValueInformation->Data[0],
  255. ValueInformation->DataLength);
  256. }
  257. RtlFreeHeap(RtlProcessHeap(), 0, ValueInformation);
  258. return Status;
  259. }
  260. NTSTATUS
  261. RtlpNtSetValueKey(
  262. IN HANDLE KeyHandle,
  263. IN ULONG KeyValueType,
  264. IN OPTIONAL PVOID KeyValue,
  265. IN ULONG KeyValueLength
  266. )
  267. /*++
  268. Routine Description:
  269. This function sets the type and value of a key.
  270. Arguments:
  271. KeyHandle - Specifies a handle of the key whose type and value are to
  272. be set. The key must have been opened with GENERIC_WRITE access.
  273. KeyValueType - This is a value that the client of the registry defines to
  274. distinguish different client-defined types of data value stored
  275. with the key. When setting the value of a key that has previously
  276. had a Type and Value stored, the Type may be changed.
  277. KeyValue - Optional pointer to the data to be optionally stored as the
  278. value of the key. If NULL is specified for this parameter, only
  279. the value type will be written.
  280. KeyValueLength - Specifies the length in bytes of the data to be stored as
  281. the key's value. A zero value indicates that no data is being stored:
  282. if zero is specified, the Value parameter will be ignored.
  283. Return Value:
  284. NTSTATUS - Result code. The following values are returned
  285. STATUS_SUCCESS - The call was successful
  286. STATUS_INVALID_PARAMETER - Invalid Parameter(s)
  287. --*/
  288. {
  289. UNICODE_STRING NullName;
  290. NullName.Length = 0;
  291. RTL_PAGED_CODE();
  292. return( NtSetValueKey( KeyHandle,
  293. &NullName, // ValueName
  294. 0, // TitleIndex
  295. KeyValueType,
  296. KeyValue,
  297. KeyValueLength
  298. ) );
  299. }
  300. NTSTATUS
  301. RtlpNtMakeTemporaryKey(
  302. IN HANDLE KeyHandle
  303. )
  304. /*++
  305. Routine Description:
  306. This function makes a Registry key temporary. The key will be deleted
  307. when the last handle to it is closed.
  308. Arguments:
  309. KeyHandle - Specifies the handle of the Key. This is also the handle
  310. of the key's directory.
  311. Return Value:
  312. NTSTATUS - Standard Nt Result Code
  313. STATUS_INVALID_HANDLE - The specified handle is invalid.
  314. STATUS_ACCESS_DENIED - The specified handle does not specify delet
  315. access.
  316. --*/
  317. {
  318. RTL_PAGED_CODE();
  319. return( NtDeleteKey(KeyHandle) );
  320. }
  321. NTSTATUS
  322. RtlpNtEnumerateSubKey(
  323. IN HANDLE KeyHandle,
  324. OUT PUNICODE_STRING SubKeyName,
  325. IN ULONG Index,
  326. OUT PLARGE_INTEGER LastWriteTime
  327. )
  328. /*++
  329. Routine Description:
  330. This function finds the name of the next sub key of a given key. By
  331. making successive calls, all of the sub keys of a key can be determined.
  332. Arguments:
  333. KeyHandle - Handle of the key whose sub keys are to be enumerated.
  334. SubKeyName - Pointer to a Unicode String in which the name of the sub
  335. key will be returned.
  336. Index - Specifies the (ZERO-based) number of the sub key to be returned.
  337. LastWriteTime - Receives the time stamp that specifies when the key
  338. was last written.
  339. Return Value:
  340. NTSTATUS - Result code
  341. STATUS_SUCCESS - The call succeeded
  342. STATUS_INVALID_PARAMETER - Invalid parameter
  343. STATUS_NO_MORE_ENTRIES - There is no key having the specified index
  344. STATUS_BUFFER_OVERFLOW - The buffer of the output string was not
  345. large enough to hold the next sub-key name. SubKeyName->Length
  346. contains the number of bytes required.
  347. STATUS_NO_MEMORY - There was not sufficient heap to perform the
  348. requested operation.
  349. --*/
  350. {
  351. NTSTATUS Status;
  352. PKEY_BASIC_INFORMATION KeyInformation = NULL;
  353. ULONG LocalBufferLength, ResultLength;
  354. RTL_PAGED_CODE();
  355. LocalBufferLength = 0;
  356. if (SubKeyName->MaximumLength > 0) {
  357. LocalBufferLength = SubKeyName->MaximumLength +
  358. FIELD_OFFSET(KEY_BASIC_INFORMATION, Name);
  359. KeyInformation = RtlAllocateHeap( RtlProcessHeap(), 0,
  360. LocalBufferLength
  361. );
  362. if (KeyInformation == NULL) {
  363. return(STATUS_NO_MEMORY);
  364. }
  365. }
  366. Status = NtEnumerateKey( KeyHandle,
  367. Index,
  368. KeyBasicInformation, //KeyInformationClass
  369. (PVOID)KeyInformation,
  370. LocalBufferLength,
  371. &ResultLength
  372. );
  373. if (NT_SUCCESS(Status) && (KeyInformation != NULL)) {
  374. if ( SubKeyName->MaximumLength >= KeyInformation->NameLength) {
  375. SubKeyName->Length = (USHORT)KeyInformation->NameLength;
  376. RtlCopyMemory( SubKeyName->Buffer,
  377. &KeyInformation->Name[0],
  378. SubKeyName->Length
  379. );
  380. } else {
  381. Status = STATUS_BUFFER_OVERFLOW;
  382. }
  383. }
  384. //
  385. // Return the length required if we failed due to a small buffer
  386. //
  387. if (Status == STATUS_BUFFER_OVERFLOW) {
  388. SubKeyName->Length = (USHORT)(ResultLength -
  389. FIELD_OFFSET(KEY_BASIC_INFORMATION, Name));
  390. }
  391. //
  392. // Free up any memory we allocated
  393. //
  394. if (KeyInformation != NULL) {
  395. RtlFreeHeap( RtlProcessHeap(), 0,
  396. KeyInformation
  397. );
  398. }
  399. return(Status);
  400. DBG_UNREFERENCED_PARAMETER( LastWriteTime );
  401. }