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.

1424 lines
39 KiB

  1. /*++
  2. Copyright (c) 1991 Microsoft Corporation
  3. Module Name:
  4. Regkey.c
  5. Abstract:
  6. This module contains the server side implementation for the Win32
  7. Registry APIs to open, create, flush and close keys. That is:
  8. - BaseRegCloseKey
  9. - BaseRegCreateKey
  10. - BaseRegFlushKey
  11. - BaseRegOpenKey
  12. Author:
  13. David J. Gilman (davegi) 15-Nov-1991
  14. Notes:
  15. These notes apply to the Win32 Registry API implementation as a whole
  16. and not just to this module.
  17. On the client side, modules contain RPC wrappers for both the new
  18. Win32 and compatible Win 3.1 APIs. The Win 3.1 wrappers generally
  19. supply default parameters before calling the Win32 wrappers. In some
  20. cases they may need to call multiple Win32 wrappers in order to
  21. function correctly (e.g. RegSetValue sometimes needs to call
  22. RegCreateKeyEx). The Win32 wrappers are quite thin and usually do
  23. nothing more than map a predefined handle to a real handle and perform
  24. ANSI<->Unicode translations. In some cases (e.g. RegCreateKeyEx) the
  25. wrapper also converts some argument (e.g. SECURITY_ATTRIBUTES) to an
  26. RPCable representation. In both the Win 3.1 and Win32 cases ANSI and
  27. Unicode implementations are provided.
  28. On the server side, there is one entry point for each of the Win32
  29. APIs. Each contains an identical interface with the client side
  30. wrapper with the exception that all string / count arguments are
  31. passed as a single counted Unicode string. Pictorially, for an API
  32. named "F":
  33. RegWin31FA() RegWin31FW() (client side)
  34. | |
  35. | |
  36. | |
  37. | |
  38. V V
  39. RegWin32FExA() RegWin32FExW()
  40. | |
  41. ^ ^
  42. v v (RPC)
  43. | |
  44. | |
  45. +----> BaseRegF() <---+ (server side)
  46. This yields smaller code (as the string conversion is done only once
  47. per API) at the cost of slightly higher maintenance (i.e. Win 3.1
  48. default parameter replacement and Win32 string conversions must be
  49. manually kept in synch).
  50. Another option would be to have a calling sequence that looks like,
  51. RegWin31FA() RegWin31FW()
  52. | |
  53. | |
  54. | |
  55. V V
  56. RegWin32FExA() -----> RegWin32FExW()
  57. and have the RegWin32FExW() API perform all of the actual work. This
  58. method is generally less efficient. It requires the RegWin32FExA()
  59. API to convert its ANSI string arguments to counted Unicode strings,
  60. extract the buffers to call the RegWin32FExW() API only to have it
  61. rebuild a counted Unicode string. However in some cases (e.g.
  62. RegConnectRegistry) where a counted Unicode string was not needed in
  63. the Unicode API this method is used.
  64. Details of an API's functionality, arguments and return value can be
  65. found in the base implementations (e.g. BaseRegF()). All other
  66. function headers contain only minimal routine descriptions and no
  67. descriptions of their arguments or return value.
  68. The comment string "Win3.1ism" indicates special code for Win 3.1
  69. compatability.
  70. Throughout the implementation the following variable names are used
  71. and always refer to the same thing:
  72. Obja - An OBJECT_ATTRIBUTES structure.
  73. Status - A NTSTATUS value.
  74. Error - A Win32 Registry error code (n.b. one of the error
  75. values is ERROR_SUCCESS).
  76. --*/
  77. #include <rpc.h>
  78. #include <string.h>
  79. #include <wchar.h>
  80. #include "regrpc.h"
  81. #include "localreg.h"
  82. #include "regclass.h"
  83. #include "regecls.h"
  84. #include "regsec.h"
  85. #include <malloc.h>
  86. #ifdef LOCAL
  87. #include "tsappcmp.h"
  88. #ifdef LEAK_TRACK
  89. #include "regleak.h"
  90. #endif // LEAK_TRACK
  91. #endif
  92. NTSTATUS
  93. BaseRegCreateMultipartKey(
  94. IN HKEY hkDestKey,
  95. IN PUNICODE_STRING pDestSubKey,
  96. IN PUNICODE_STRING lpClass OPTIONAL,
  97. IN DWORD dwOptions,
  98. IN REGSAM samDesired,
  99. IN PRPC_SECURITY_ATTRIBUTES pRpcSecurityAttributes OPTIONAL,
  100. OUT PHKEY phkResult,
  101. OUT LPDWORD lpdwDisposition OPTIONAL,
  102. ULONG Attributes);
  103. BOOL
  104. InitializeRegCreateKey(
  105. )
  106. /*++
  107. Routine Description:
  108. This function was used to initialize a critical section that no longer
  109. exists. This critical section was used when a key name '\', and multiple
  110. multiple keys were to be created. The API used the wcstok defined in the
  111. kernel, which was not multi-threaded safe.
  112. This function now will always return TRUE. It will not be removed from the code
  113. to avoid change in the rpc interface.
  114. Arguments:
  115. None.
  116. Return Value:
  117. Returns TRUE always.
  118. --*/
  119. {
  120. return( TRUE );
  121. }
  122. BOOL
  123. CleanupRegCreateKey(
  124. )
  125. /*++
  126. Routine Description:
  127. This function was used to clean up a critical section that no longer
  128. exists. This critical section was used when a key name '\', and multiple
  129. multiple keys were to be created. The API used the wcstok defined in the
  130. kernel, which was not multi-threaded safe.
  131. This function now will always return TRUE. It will not be removed from the code
  132. to avoid change in the rpc interface.
  133. Arguments:
  134. None.
  135. Return Value:
  136. Returns TRUE if the cleanup succeeds.
  137. --*/
  138. {
  139. return( TRUE );
  140. }
  141. error_status_t
  142. BaseRegCloseKeyInternal(
  143. IN OUT PHKEY phKey
  144. )
  145. /*++
  146. Routine Description:
  147. Closes a key handle.
  148. Arguments:
  149. phKey - Supplies a handle to an open key to be closed.
  150. Return Value:
  151. Returns ERROR_SUCCESS (0) for success; error-code for failure.
  152. --*/
  153. {
  154. NTSTATUS Status;
  155. #if defined(LEAK_TRACK)
  156. BOOL fTrack;
  157. #endif // defined(LEAK_TRACK)
  158. //
  159. // Call out to Perflib if the HKEY is HKEY_PERFOMANCE_DATA.
  160. //
  161. if(( *phKey == HKEY_PERFORMANCE_DATA ) ||
  162. ( *phKey == HKEY_PERFORMANCE_TEXT ) ||
  163. ( *phKey == HKEY_PERFORMANCE_NLSTEXT )) {
  164. Status = PerfRegCloseKey( phKey );
  165. return (error_status_t)Status;
  166. }
  167. ASSERT( IsPredefinedRegistryHandle( *phKey ) == FALSE );
  168. #ifdef LOCAL
  169. //
  170. // now we need to remove any state for registry key enumeration associated
  171. // with this key if it's a class registration parent
  172. //
  173. if (REG_CLASS_IS_SPECIAL_KEY(*phKey)) {
  174. // this may not succeed since someone could have already removed this key
  175. (void) EnumTableRemoveKey(
  176. &gClassesEnumTable,
  177. *phKey,
  178. ENUM_TABLE_REMOVEKEY_CRITERIA_ANYTHREAD);
  179. }
  180. #if defined(LEAK_TRACK)
  181. if (g_RegLeakTraceInfo.bEnableLeakTrack) {
  182. fTrack = RegLeakTableIsTrackedObject(&gLeakTable, *phKey);
  183. }
  184. #endif // defined(LEAK_TRACK)
  185. #endif // LOCAL
  186. Status = NtClose( *phKey );
  187. if( NT_SUCCESS( Status )) {
  188. #ifdef LOCAL
  189. #if defined(LEAK_TRACK)
  190. if (g_RegLeakTraceInfo.bEnableLeakTrack) {
  191. if (fTrack) {
  192. (void) UnTrackObject(*phKey);
  193. }
  194. }
  195. #endif // defined(LEAK_TRACK)
  196. #endif // LOCAL
  197. //
  198. // Set the handle to NULL so that RPC knows that it has been closed.
  199. //
  200. *phKey = NULL;
  201. return ERROR_SUCCESS;
  202. } else {
  203. return (error_status_t)RtlNtStatusToDosError( Status );
  204. }
  205. }
  206. error_status_t
  207. BaseRegCloseKey(
  208. IN OUT PHKEY phKey
  209. )
  210. /*++
  211. Routine Description:
  212. Closes a key handle.
  213. Arguments:
  214. phKey - Supplies a handle to an open key to be closed.
  215. Return Value:
  216. Returns ERROR_SUCCESS (0) for success; error-code for failure.
  217. --*/
  218. {
  219. error_status_t Error;
  220. #ifndef LOCAL
  221. RPC_STATUS _rpcstatus = RpcImpersonateClient( NULL );
  222. #if DBG
  223. if( _rpcstatus != ERROR_SUCCESS ) {
  224. DbgPrint("WINREG: BaseRegCloseKey: Failed to impersonate in process %p, thread %p, for handle %p \n",NtCurrentProcess(),NtCurrentThread(),*phKey);
  225. }
  226. #endif
  227. #endif //LOCAL
  228. Error = BaseRegCloseKeyInternal(phKey);
  229. #ifndef LOCAL
  230. #if DBG
  231. if( _rpcstatus != ERROR_SUCCESS ) {
  232. DbgPrint("WINREG: BaseRegCloseKeyInternal without impersonation returned %lx\n",Error);
  233. }
  234. #endif
  235. if (_rpcstatus == ERROR_SUCCESS) {
  236. RpcRevertToSelf();
  237. }
  238. #endif
  239. return Error;
  240. }
  241. error_status_t
  242. BaseRegCreateKey(
  243. IN HKEY hKey,
  244. IN PUNICODE_STRING lpSubKey,
  245. IN PUNICODE_STRING lpClass OPTIONAL,
  246. IN DWORD dwOptions,
  247. IN REGSAM samDesired,
  248. IN PRPC_SECURITY_ATTRIBUTES pRpcSecurityAttributes OPTIONAL,
  249. OUT PHKEY phkResult,
  250. OUT LPDWORD lpdwDisposition OPTIONAL
  251. )
  252. /*++
  253. Routine Description:
  254. Create a new key, with the specified name, or open an already existing
  255. key. RegCreateKeyExW is atomic, meaning that one can use it to create
  256. a key as a lock. If a second caller creates the same key, the call
  257. will return a value that says whether the key already existed or not,
  258. and thus whether the caller "owns" the "lock" or not. RegCreateKeyExW
  259. does NOT truncate an existing entry, so the lock entry may contain
  260. data.
  261. Arguments:
  262. hKey - Supplies a handle to an open key. The lpSubKey key path
  263. parameter is relative to this key handle. Any of the predefined
  264. reserved handle values or a previously opened key handle may be used
  265. for hKey.
  266. lpSubKey - Supplies the downward key path to the key to create.
  267. lpSubKey is always relative to the key specified by hKey.
  268. This parameter may not be NULL.
  269. lpClass - Supplies the class (object type) of this key. Ignored if
  270. the key already exists. No class is associated with this key if
  271. this parameter is NULL.
  272. dwOptions - Supplies special options. Only one is currently defined:
  273. REG_VOLATILE - Specifies that this key should not be preserved
  274. across reboot. The default is not volatile. This is ignored
  275. if the key already exists.
  276. WARNING: All descendent keys of a volatile key are also volatile.
  277. samDesired - Supplies the requested security access mask. This
  278. access mask describes the desired security access to the newly
  279. created key.
  280. lpSecurityAttributes - Supplies a pointer to a SECURITY_ATTRIBUTES
  281. structure for the newly created key. This parameter is ignored
  282. if NULL or not supported by the OS.
  283. phkResult - Returns an open handle to the newly created key.
  284. lpdwDisposition - Returns the disposition state, which can be one of:
  285. REG_CREATED_NEW_KEY - the key did not exist and was created.
  286. REG_OPENED_EXISTING_KEY - the key already existed, and was simply
  287. opened without being changed.
  288. This parameter is ignored if NULL.
  289. Return Value:
  290. Returns ERROR_SUCCESS (0) for success; error-code for failure.
  291. If successful, RegCreateKeyEx creates the new key (or opens the key if
  292. it already exists), and returns an open handle to the newly created
  293. key in phkResult. Newly created keys have no value; RegSetValue, or
  294. RegSetValueEx must be called to set values. hKey must have been
  295. opened for KEY_CREATE_SUB_KEY access.
  296. --*/
  297. {
  298. OBJECT_ATTRIBUTES Obja;
  299. ULONG Attributes;
  300. NTSTATUS Status;
  301. #if DBG
  302. HANDLE DebugKey = hKey;
  303. #endif
  304. HKEY hkDestKey;
  305. UNICODE_STRING DestClassSubkey;
  306. PUNICODE_STRING pDestSubkey;
  307. DWORD dwDisposition;
  308. BOOL fRetryOnAccessDenied;
  309. BOOL fRetried;
  310. BOOL fTrySingleCreate;
  311. #if LOCAL
  312. SKeySemantics keyinfo;
  313. BYTE rgNameInfoBuf[REG_MAX_CLASSKEY_LEN];
  314. REGSAM OriginalSam = samDesired;
  315. UNICODE_STRING TmpStr = *lpSubKey; //used to keep original SubKey string
  316. memset(&keyinfo, 0, sizeof(keyinfo));
  317. #endif
  318. ASSERT( IsPredefinedRegistryHandle( hKey ) == FALSE );
  319. ASSERT( lpSubKey->Length > 0 );
  320. DestClassSubkey.Buffer = NULL;
  321. //
  322. // For class registrations, retry on access denied in machine hive --
  323. // if we do retry, this will be set to FALSE so we only retry once
  324. //
  325. fRetryOnAccessDenied = TRUE;
  326. fRetried = FALSE;
  327. //
  328. // First attempt should do create with a single ntcreatekey call
  329. // If that doesn't work, this gets set to false so we remember if we
  330. // have to retry for access denied in the machine hive
  331. //
  332. fTrySingleCreate = TRUE;
  333. hkDestKey = NULL;
  334. pDestSubkey = NULL;
  335. //
  336. // Quick check for a "restricted" handle
  337. //
  338. if ( REGSEC_CHECK_HANDLE( hKey ) )
  339. {
  340. if ( ! REGSEC_CHECK_PATH( hKey, lpSubKey ) )
  341. {
  342. return( ERROR_ACCESS_DENIED );
  343. }
  344. hKey = REGSEC_CLEAR_HANDLE( hKey );
  345. }
  346. //
  347. // Check for malformed arguments from malicious clients
  348. //
  349. if ((lpSubKey->Length < sizeof(UNICODE_NULL)) ||
  350. (lpSubKey->Buffer == NULL) ||
  351. ((lpSubKey->Length % sizeof(WCHAR)) != 0) ||
  352. (lpSubKey->Buffer[lpSubKey->Length / sizeof(WCHAR) - 1] != L'\0')) {
  353. return(ERROR_INVALID_PARAMETER);
  354. }
  355. //
  356. // Impersonate the client.
  357. //
  358. RPC_IMPERSONATE_CLIENT( NULL );
  359. //
  360. // Initialize the variable that will contain the handle to NULL
  361. // to ensure that in case of error the API will not return a
  362. // bogus handle. This is required otherwise RPC will get confused.
  363. // Note that RPC should have already initialized it to 0.
  364. //
  365. *phkResult = NULL;
  366. //
  367. // Subtract the NULLs from the Length of the provided strings.
  368. // These were added on the client side so that the NULLs were
  369. // transmitted by RPC.
  370. //
  371. lpSubKey->Length -= sizeof( UNICODE_NULL );
  372. if( lpSubKey->Buffer[0] == ( WCHAR )'\\' ) {
  373. //
  374. // Do not accept a key name that starts with '\', even though
  375. // the code below would handle it. This is to ensure that
  376. // RegCreateKeyEx and RegOpenKeyEx will behave in the same way
  377. // when they get a key name that starts with '\'.
  378. //
  379. Status = STATUS_OBJECT_PATH_INVALID;
  380. goto cleanup;
  381. }
  382. if ( lpClass->Length > 0 ) {
  383. lpClass->Length -= sizeof( UNICODE_NULL );
  384. }
  385. //
  386. // Determine the correct set of attributes.
  387. //
  388. Attributes = OBJ_CASE_INSENSITIVE;
  389. if( ARGUMENT_PRESENT( pRpcSecurityAttributes )) {
  390. if( pRpcSecurityAttributes->bInheritHandle ) {
  391. Attributes |= OBJ_INHERIT;
  392. }
  393. }
  394. if (dwOptions & REG_OPTION_OPEN_LINK) {
  395. Attributes |= OBJ_OPENLINK;
  396. }
  397. #ifdef LOCAL
  398. if (REG_CLASS_IS_SPECIAL_KEY(hKey) ||
  399. ( (gdwRegistryExtensionFlags & TERMSRV_ENABLE_PER_USER_CLASSES_REDIRECTION )
  400. && ExtractClassKey(&hKey,lpSubKey) ) ) {
  401. //
  402. // Find more information
  403. // about this key -- the most important piece of information
  404. // is whether it's a class registration key
  405. //
  406. keyinfo._pFullPath = (PKEY_NAME_INFORMATION) rgNameInfoBuf;
  407. keyinfo._cbFullPath = sizeof(rgNameInfoBuf);
  408. keyinfo._fAllocedNameBuf = FALSE;
  409. //
  410. // see if this is a class registration
  411. //
  412. Status = BaseRegGetKeySemantics(hKey, lpSubKey, &keyinfo);
  413. // if we can't determine what type of key this is, leave
  414. if (!NT_SUCCESS(Status)) {
  415. goto cleanup;
  416. }
  417. Status = BaseRegMapClassRegistrationKey(
  418. hKey,
  419. lpSubKey,
  420. &keyinfo,
  421. &DestClassSubkey,
  422. &fRetryOnAccessDenied,
  423. &hkDestKey,
  424. &pDestSubkey);
  425. if (!NT_SUCCESS(Status)) {
  426. goto cleanup;
  427. }
  428. } else
  429. #endif // LOCAL
  430. {
  431. #ifdef LOCAL
  432. memset(&keyinfo, 0, sizeof(keyinfo));
  433. #endif // LOCAL
  434. hkDestKey = hKey;
  435. pDestSubkey = lpSubKey;
  436. }
  437. for (;;) {
  438. #ifdef LOCAL
  439. Status = STATUS_OBJECT_NAME_NOT_FOUND;
  440. if (fTrySingleCreate)
  441. {
  442. #endif
  443. //
  444. // Validate the security descriptor.
  445. //
  446. if( ARGUMENT_PRESENT( pRpcSecurityAttributes ) &&
  447. (pRpcSecurityAttributes->RpcSecurityDescriptor.lpSecurityDescriptor ||
  448. pRpcSecurityAttributes->RpcSecurityDescriptor.cbInSecurityDescriptor))
  449. {
  450. if( !RtlValidRelativeSecurityDescriptor((PSECURITY_DESCRIPTOR)(pRpcSecurityAttributes->RpcSecurityDescriptor.lpSecurityDescriptor),
  451. pRpcSecurityAttributes->RpcSecurityDescriptor.cbInSecurityDescriptor,
  452. 0 )) {
  453. //
  454. // We were passed a bogus security descriptor to set. Bail out
  455. //
  456. Status = STATUS_INVALID_PARAMETER;
  457. goto cleanup;
  458. }
  459. }
  460. //
  461. // Try to create the specified key. This will work if there is only
  462. // one key being created or if the key already exists. If more than
  463. // one key needs to be created, this will fail and we will have to
  464. // do all the complicated stuff to create each intermediate key.
  465. //
  466. InitializeObjectAttributes(&Obja,
  467. pDestSubkey,
  468. Attributes,
  469. hkDestKey,
  470. ARGUMENT_PRESENT( pRpcSecurityAttributes )
  471. ? pRpcSecurityAttributes
  472. ->RpcSecurityDescriptor.lpSecurityDescriptor
  473. : NULL);
  474. Status = NtCreateKey(phkResult,
  475. samDesired,
  476. &Obja,
  477. 0,
  478. lpClass,
  479. dwOptions,
  480. &dwDisposition);
  481. #ifdef LOCAL
  482. if (gpfnTermsrvCreateRegEntry && NT_SUCCESS(Status) && (dwDisposition == REG_CREATED_NEW_KEY)) {
  483. //
  484. // Terminal Server application compatiblity
  485. // Store the newly created key in the Terminal Server registry tracking database
  486. //
  487. gpfnTermsrvCreateRegEntry(*phkResult,
  488. &Obja,
  489. 0,
  490. lpClass,
  491. dwOptions);
  492. }
  493. }
  494. #ifdef CLASSES_RETRY_ON_ACCESS_DENIED
  495. if (fTrySingleCreate && (STATUS_ACCESS_DENIED == Status) && keyinfo._fCombinedClasses &&
  496. fRetryOnAccessDenied ) {
  497. Status = BaseRegMapClassOnAccessDenied(
  498. &keyinfo,
  499. &hkDestKey,
  500. pDestSubkey,
  501. &fRetryOnAccessDenied);
  502. if (NT_SUCCESS(Status)) {
  503. fRetried = TRUE;
  504. continue;
  505. }
  506. // we failed for some reason -- exit
  507. break;
  508. }
  509. #else
  510. //if (it's terminal server; we're trying to create single key;
  511. //we've got ASSESS_DENIED trying to create key
  512. //a key we want to create is HKCR subkey(keyinfo._fCombinedClasses!=0);
  513. //Registry flag is set to allow per user classes redirection.
  514. //(fRetryOnAccessDenied !=0 - means that parent key is not in the user hive))
  515. //then try to create the key in the user hive.
  516. if ( (gdwRegistryExtensionFlags & TERMSRV_ENABLE_PER_USER_CLASSES_REDIRECTION)
  517. && fTrySingleCreate && (STATUS_ACCESS_DENIED == Status)
  518. && keyinfo._fCombinedClasses && fRetryOnAccessDenied
  519. ) {
  520. if (DestClassSubkey.Buffer) {
  521. RegClassHeapFree(DestClassSubkey.Buffer);
  522. DestClassSubkey.Buffer=NULL;
  523. }
  524. Status = BaseRegMapClassOnAccessDenied(
  525. &keyinfo,
  526. &hkDestKey,
  527. pDestSubkey,
  528. &fRetryOnAccessDenied);
  529. if (NT_SUCCESS(Status)) {
  530. fRetried = TRUE;
  531. continue;
  532. }
  533. // we failed for some reason -- exit
  534. break;
  535. }
  536. #endif // CLASSES_RETRY_ON_ACCESS_DENIED
  537. #endif // LOCAL
  538. fTrySingleCreate = FALSE;
  539. if (NT_SUCCESS(Status)) {
  540. if (lpdwDisposition) {
  541. *lpdwDisposition = dwDisposition;
  542. }
  543. } else {
  544. Status = BaseRegCreateMultipartKey(
  545. hkDestKey,
  546. pDestSubkey,
  547. lpClass,
  548. dwOptions,
  549. samDesired,
  550. pRpcSecurityAttributes,
  551. phkResult,
  552. lpdwDisposition,
  553. Attributes);
  554. }
  555. #ifdef LOCAL
  556. #ifdef CLASSES_RETRY_ON_ACCESS_DENIED
  557. if ((STATUS_ACCESS_DENIED == Status) && keyinfo._fCombinedClasses &&
  558. fRetryOnAccessDenied ) {
  559. Status = BaseRegMapClassOnAccessDenied(
  560. &keyinfo,
  561. &hkDestKey,
  562. pDestSubkey,
  563. &fRetryOnAccessDenied);
  564. if (NT_SUCCESS(Status)) {
  565. fRetried = TRUE;
  566. continue;
  567. }
  568. break;
  569. }
  570. #else
  571. //We've tried to create single key and failed (Status !=STATUS_ACCESS_DENIED)
  572. //then we tried to create multipart key and got access denied
  573. //thus we've got here
  574. if ( (gdwRegistryExtensionFlags & TERMSRV_ENABLE_PER_USER_CLASSES_REDIRECTION)
  575. && (STATUS_ACCESS_DENIED == Status)
  576. && keyinfo._fCombinedClasses && fRetryOnAccessDenied
  577. ) {
  578. if (DestClassSubkey.Buffer) {
  579. RegClassHeapFree(DestClassSubkey.Buffer);
  580. DestClassSubkey.Buffer=NULL;
  581. }
  582. Status = BaseRegMapClassOnAccessDenied(
  583. &keyinfo,
  584. &hkDestKey,
  585. pDestSubkey,
  586. &fRetryOnAccessDenied);
  587. if (NT_SUCCESS(Status)) {
  588. fRetried = TRUE;
  589. continue;
  590. }
  591. break;
  592. }
  593. #endif // CLASSES_RETRY_ON_ACCESS_DENIED
  594. if (NT_SUCCESS(Status)) {
  595. if (keyinfo._fCombinedClasses) {
  596. // mark this key as part of hkcr
  597. *phkResult = REG_CLASS_SET_SPECIAL_KEY(*phkResult);
  598. }
  599. }
  600. #endif // LOCAL
  601. break;
  602. }
  603. cleanup:
  604. #ifdef CLASSES_RETRY_ON_ACCESS_DENIED
  605. //
  606. // Memory was allocated if we retried, so free it
  607. //
  608. if (fRetried && pDestSubkey->Buffer) {
  609. RtlFreeHeap(RtlProcessHeap(), 0, pDestSubkey->Buffer);
  610. pDestSubkey->Buffer = NULL;
  611. }
  612. #endif // CLASSES_RETRY_ON_ACCESS_DENIED
  613. if (hkDestKey && (hkDestKey != hKey)) {
  614. NtClose(hkDestKey);
  615. }
  616. #ifdef LOCAL
  617. if (DestClassSubkey.Buffer) {
  618. RegClassHeapFree(DestClassSubkey.Buffer);
  619. }
  620. BaseRegReleaseKeySemantics(&keyinfo);
  621. *lpSubKey = TmpStr; //restore original SubKey string
  622. #endif // LOCAL
  623. if (NT_SUCCESS(Status)) {
  624. #ifdef LOCAL
  625. #if defined(LEAK_TRACK)
  626. if (g_RegLeakTraceInfo.bEnableLeakTrack) {
  627. (void) TrackObject(*phkResult);
  628. }
  629. #endif // defined(LEAK_TRACK)
  630. #endif LOCAL
  631. // disabled, for the case where we specifically close a predefined key inside
  632. // RegOpenKeyExA and RegOpenKeyExW
  633. //ASSERT( *phkResult != DebugKey );
  634. }
  635. RPC_REVERT_TO_SELF();
  636. return (error_status_t)RtlNtStatusToDosError( Status );
  637. }
  638. NTSTATUS
  639. BaseRegCreateMultipartKey(
  640. IN HKEY hkDestKey,
  641. IN PUNICODE_STRING pDestSubKey,
  642. IN PUNICODE_STRING lpClass OPTIONAL,
  643. IN DWORD dwOptions,
  644. IN REGSAM samDesired,
  645. IN PRPC_SECURITY_ATTRIBUTES pRpcSecurityAttributes OPTIONAL,
  646. OUT PHKEY phkResult,
  647. OUT LPDWORD lpdwDisposition OPTIONAL,
  648. ULONG Attributes)
  649. /*++
  650. Routine Description:
  651. This function creates registry keys for which multiple path components
  652. are nonexistent. It parses the key path and creates each intermediate
  653. subkey.
  654. Arguments:
  655. See BaseRegCreateKey.
  656. Return Value:
  657. Returns STATUS_SUCCESS on success, other NTSTATUS if failed.
  658. --*/
  659. {
  660. LPWSTR KeyBuffer;
  661. ULONG NumberOfSubKeys;
  662. LPWSTR p;
  663. ULONG i;
  664. LPWSTR Token;
  665. UNICODE_STRING KeyName;
  666. HANDLE TempHandle1;
  667. HANDLE TempHandle2;
  668. OBJECT_ATTRIBUTES Obja;
  669. NTSTATUS Status;
  670. DWORD dwDisposition;
  671. #ifdef LOCAL
  672. REGSAM OriginalSam = samDesired;
  673. #endif // LOCAL
  674. dwDisposition = REG_OPENED_EXISTING_KEY;
  675. TempHandle1 = NULL;
  676. //
  677. // Win3.1ism - Loop through each '\' separated component in the
  678. // supplied sub key and create a key for each component. This is
  679. // guaranteed to work at least once because lpSubKey was validated
  680. // on the client side.
  681. //
  682. //
  683. // Initialize the buffer to be tokenized.
  684. //
  685. KeyBuffer = pDestSubKey->Buffer;
  686. //
  687. // Find out the number of subkeys to be created
  688. //
  689. NumberOfSubKeys = 1;
  690. p = KeyBuffer;
  691. while ( ( p = wcschr( p, ( WCHAR )'\\' ) ) != NULL ) {
  692. p++;
  693. NumberOfSubKeys++;
  694. }
  695. for( i = 0, Token = KeyBuffer; i < NumberOfSubKeys; i++ ) {
  696. ASSERT(Token != NULL);
  697. if( ( *Token == ( WCHAR )'\\' ) &&
  698. ( i != NumberOfSubKeys - 1 ) ) {
  699. //
  700. // If the first character of the key name is '\', and the key
  701. // is not the last to be created, then ignore this key name.
  702. // This condition can happen if the key name contains
  703. // consecutive '\'.
  704. // This behavior is consistent with the one we had in the past
  705. // when the API used wcstok() to get the key names.
  706. // Note that if the key name is an empty string, we return a handle
  707. // that is different than hKey, even though both point to the same
  708. // key. This is by design.
  709. //
  710. Token++;
  711. continue;
  712. }
  713. //
  714. // Convert the token to a counted Unicode string.
  715. //
  716. KeyName.Buffer = Token;
  717. if (i == NumberOfSubKeys - 1) {
  718. KeyName.Length = wcslen(Token)*sizeof(WCHAR);
  719. } else {
  720. KeyName.Length = (USHORT)(wcschr(Token, ( WCHAR )'\\') - Token)*sizeof(WCHAR);
  721. }
  722. //
  723. // Remember the intermediate handle (NULL the first time through).
  724. //
  725. TempHandle2 = TempHandle1;
  726. {
  727. //
  728. // Initialize the OBJECT_ATTRIBUTES structure, close the
  729. // intermediate key and create or open the key.
  730. //
  731. InitializeObjectAttributes(
  732. &Obja,
  733. &KeyName,
  734. Attributes,
  735. hkDestKey,
  736. ARGUMENT_PRESENT( pRpcSecurityAttributes )
  737. ? pRpcSecurityAttributes
  738. ->RpcSecurityDescriptor.lpSecurityDescriptor
  739. : NULL
  740. );
  741. Status = NtCreateKey(
  742. &TempHandle1,
  743. ( i == NumberOfSubKeys - 1 )? samDesired :
  744. (samDesired & KEY_WOW64_RES) | MAXIMUM_ALLOWED,
  745. &Obja,
  746. 0,
  747. lpClass,
  748. dwOptions,
  749. &dwDisposition
  750. );
  751. if (NT_SUCCESS(Status) && lpdwDisposition) {
  752. *lpdwDisposition = dwDisposition;
  753. }
  754. #ifdef LOCAL
  755. // This code is in Hydra 4. We have disabled this for NT 5
  756. // for now till we are sure that its needed to get some imporatant
  757. // app to work on Hydra 5. Otherwise this should be removed
  758. if ( gdwRegistryExtensionFlags & TERMSRV_ENABLE_ACCESS_FLAG_MODIFICATION ) {
  759. // For Terminal Server only.
  760. // Some apps try to create/open the key with all of the access bits
  761. // turned on. We'll mask off the ones they don't have access to by
  762. // default, (at least under HKEY_LOCAL_MACHINE\Software) and try to
  763. // open the key again.
  764. if (Status == STATUS_ACCESS_DENIED) {
  765. //MAXIMUM_ALLOWED does not include ACCESS_SYSTEM_SECURITY
  766. //so if user asks for this permission, we need to add it.
  767. //It could result in ACCESS_DENIED error but for
  768. //TS App. Compat. it is not important.
  769. Status = NtCreateKey(
  770. &TempHandle1,
  771. (samDesired & (KEY_WOW64_RES | ACCESS_SYSTEM_SECURITY) ) | MAXIMUM_ALLOWED,
  772. &Obja,
  773. 0,
  774. lpClass,
  775. dwOptions,
  776. &dwDisposition);
  777. // Give app back the original error
  778. if (!NT_SUCCESS(Status)) {
  779. Status = STATUS_ACCESS_DENIED;
  780. }
  781. if (lpdwDisposition) {
  782. *lpdwDisposition = dwDisposition;
  783. }
  784. }
  785. }
  786. if (gpfnTermsrvCreateRegEntry && NT_SUCCESS(Status) && (dwDisposition == REG_CREATED_NEW_KEY)) {
  787. //
  788. // Terminal Server application compatiblity
  789. // Store the newly created key in the Terminal Server registry tracking database
  790. //
  791. gpfnTermsrvCreateRegEntry(TempHandle1,
  792. &Obja,
  793. 0,
  794. lpClass,
  795. dwOptions);
  796. }
  797. #endif
  798. }
  799. //
  800. // Initialize the next object directory (i.e. parent key) handle.
  801. //
  802. hkDestKey = TempHandle1;
  803. //
  804. // Close the intermediate key.
  805. // This fails the first time through the loop since the
  806. // handle is NULL.
  807. //
  808. if( TempHandle2 != NULL ) {
  809. NtClose( TempHandle2 );
  810. }
  811. //
  812. // If creating the key failed, map and return the error.
  813. //
  814. if( ! NT_SUCCESS( Status )) {
  815. return Status;
  816. }
  817. Token = wcschr( Token, ( WCHAR )'\\') + 1;
  818. }
  819. //
  820. // Only set the return value once we know we've
  821. // succeeded.
  822. //
  823. *phkResult = hkDestKey;
  824. return STATUS_SUCCESS;
  825. }
  826. error_status_t
  827. BaseRegFlushKey(
  828. IN HKEY hKey
  829. )
  830. /*++
  831. Routine Description:
  832. Flush changes to backing store. Flush will not return until the data
  833. has been written to backing store. It will flush all the attributes
  834. of a single key. Closing a key without flushing it will NOT abort
  835. changes.
  836. Arguments:
  837. hKey - Supplies a handle to the open key.
  838. Return Value:
  839. Returns ERROR_SUCCESS (0) for success; error-code for failure.
  840. If successful, RegFlushKey will flush to backing store any changes
  841. made to the key.
  842. Notes:
  843. RegFlushKey may also flush other data in the Registry, and therefore
  844. can be expensive, it should not be called gratuitously.
  845. --*/
  846. {
  847. if ((hKey == HKEY_PERFORMANCE_DATA) ||
  848. (hKey == HKEY_PERFORMANCE_TEXT) ||
  849. (hKey == HKEY_PERFORMANCE_NLSTEXT)) {
  850. return(ERROR_SUCCESS);
  851. }
  852. ASSERT( IsPredefinedRegistryHandle( hKey ) == FALSE );
  853. //
  854. // Call the Nt Api to flush the key, map the NTSTATUS code to a
  855. // Win32 Registry error code and return.
  856. //
  857. return (error_status_t)RtlNtStatusToDosError( NtFlushKey( hKey ));
  858. }
  859. error_status_t
  860. BaseRegOpenKey(
  861. IN HKEY hKey,
  862. IN PUNICODE_STRING lpSubKey,
  863. IN DWORD dwOptions,
  864. IN REGSAM samDesired,
  865. OUT PHKEY phkResult
  866. )
  867. /*++
  868. Routine Description:
  869. Open a key for access, returning a handle to the key. If the key is
  870. not present, it is not created (see RegCreateKeyExW).
  871. Arguments:
  872. hKey - Supplies a handle to an open key. The lpSubKey pathname
  873. parameter is relative to this key handle. Any of the predefined
  874. reserved handle values or a previously opened key handle may be used
  875. for hKey. NULL is not permitted.
  876. lpSubKey - Supplies the downward key path to the key to open.
  877. lpSubKey is always relative to the key specified by hKey.
  878. dwOptions -- reserved.
  879. samDesired -- This access mask describes the desired security access
  880. for the key.
  881. phkResult -- Returns the handle to the newly opened key.
  882. Return Value:
  883. Returns ERROR_SUCCESS (0) for success; error-code for failure.
  884. If successful, RegOpenKeyEx will return the handle to the newly opened
  885. key in phkResult.
  886. --*/
  887. {
  888. OBJECT_ATTRIBUTES Obja;
  889. NTSTATUS Status = STATUS_OBJECT_NAME_NOT_FOUND;
  890. error_status_t ret = ERROR_SUCCESS;
  891. #ifdef LOCAL
  892. UNICODE_STRING TmpStr = *lpSubKey; //used to keep original SubKey string
  893. #endif
  894. UNREFERENCED_PARAMETER( dwOptions );
  895. ASSERT( IsPredefinedRegistryHandle( hKey ) == FALSE );
  896. //
  897. // Need to NULL this out param for compat with NT4, even though SDK
  898. // does not define this out param on api failure -- bad apps were written
  899. // which rely on this. Used to get NULLed by call to NtOpenKey, but since
  900. // we don't always call that now, we need to do this here in user mode. Also
  901. // need an exception wrapper since NtOpenKey would simply return an error if
  902. // the pointer were invalid, whereas in user mode we access violate if we simply
  903. // assign -- yet another fix needed for app compatibility as some apps on NT 4
  904. // were actually passing in a bad pointer and ignoring the error returned
  905. // by the api as part of their normal operation.
  906. //
  907. __try {
  908. *phkResult = NULL;
  909. } __except ( EXCEPTION_EXECUTE_HANDLER ) {
  910. Status = GetExceptionCode();
  911. #if DBG
  912. DbgPrint( "WINREG Error: Exception %x in BaseRegOpenKey\n",
  913. Status );
  914. #endif
  915. ret = RtlNtStatusToDosError( Status );
  916. }
  917. //
  918. // This will only be true if there was an exception above --
  919. // return the exception code as an error
  920. //
  921. if (ERROR_SUCCESS != ret) {
  922. return ret;
  923. }
  924. //
  925. // Quick check for a "restricted" handle
  926. //
  927. if ( REGSEC_CHECK_HANDLE( hKey ) )
  928. {
  929. if ( ! REGSEC_CHECK_PATH( hKey, lpSubKey ) )
  930. {
  931. return( ERROR_ACCESS_DENIED );
  932. }
  933. hKey = REGSEC_CLEAR_HANDLE( hKey );
  934. }
  935. //
  936. // Impersonate the client.
  937. //
  938. RPC_IMPERSONATE_CLIENT( NULL );
  939. //
  940. // Subtract the NULLs from the Length of the provided string.
  941. // This was added on the client side so that the NULL was
  942. // transmited by RPC.
  943. //
  944. lpSubKey->Length -= sizeof( UNICODE_NULL );
  945. //
  946. // Initialize the OBJECT_ATTRIBUTES structure and open the key.
  947. //
  948. InitializeObjectAttributes(
  949. &Obja,
  950. lpSubKey,
  951. dwOptions & REG_OPTION_OPEN_LINK ? (OBJ_OPENLINK | OBJ_CASE_INSENSITIVE)
  952. : OBJ_CASE_INSENSITIVE,
  953. hKey,
  954. NULL
  955. );
  956. #ifdef LOCAL
  957. if ( REG_CLASS_IS_SPECIAL_KEY(hKey) ||
  958. ( (gdwRegistryExtensionFlags & TERMSRV_ENABLE_PER_USER_CLASSES_REDIRECTION)
  959. && ExtractClassKey(&hKey,lpSubKey) ) ) {
  960. Status = BaseRegOpenClassKey(
  961. hKey,
  962. lpSubKey,
  963. dwOptions,
  964. samDesired,
  965. phkResult);
  966. } else
  967. #endif // LOCAL
  968. {
  969. //
  970. // Obja was initialized above
  971. //
  972. Status = NtOpenKey(
  973. phkResult,
  974. samDesired,
  975. &Obja);
  976. }
  977. RPC_REVERT_TO_SELF();
  978. ret = (error_status_t)RtlNtStatusToDosError( Status );
  979. #ifdef LOCAL
  980. if (STATUS_ACCESS_DENIED == Status)
  981. {
  982. //If key could not be opened with SamDesired access
  983. //open it with MAXIMUM_ALLOWED.
  984. //do it only if it's terminal server and proper
  985. //flag is set in the registry.
  986. if ( gdwRegistryExtensionFlags & TERMSRV_ENABLE_ACCESS_FLAG_MODIFICATION )
  987. {
  988. {
  989. //MAXIMUM_ALLOWED does not include ACCESS_SYSTEM_SECURITY
  990. //so if user asks for this permission, we need to add it.
  991. //It could result in ACCESS_DENIED error but for
  992. //TS App. Compat. it is not important.
  993. if(REG_CLASS_IS_SPECIAL_KEY(hKey))
  994. {
  995. Status = BaseRegOpenClassKey(
  996. hKey,
  997. lpSubKey,
  998. dwOptions,
  999. (samDesired & (KEY_WOW64_RES | ACCESS_SYSTEM_SECURITY)) | MAXIMUM_ALLOWED,
  1000. phkResult);
  1001. }
  1002. else
  1003. {
  1004. Status = NtOpenKey(
  1005. phkResult,
  1006. (samDesired & (KEY_WOW64_RES | ACCESS_SYSTEM_SECURITY)) | MAXIMUM_ALLOWED,
  1007. &Obja);
  1008. }
  1009. // Give app back the original error
  1010. if (!NT_SUCCESS(Status)) {
  1011. Status = STATUS_ACCESS_DENIED;
  1012. }
  1013. ret = (error_status_t)RtlNtStatusToDosError( Status );
  1014. }
  1015. }
  1016. }
  1017. if ((!REG_CLASS_IS_SPECIAL_KEY(hKey)) && !NT_SUCCESS(Status) && gpfnTermsrvOpenRegEntry) {
  1018. //
  1019. // Obja was initialized above
  1020. //
  1021. if (gpfnTermsrvOpenRegEntry(phkResult,
  1022. samDesired,
  1023. &Obja)) {
  1024. Status = STATUS_SUCCESS;
  1025. ret = (error_status_t)RtlNtStatusToDosError( Status );
  1026. }
  1027. }
  1028. #if defined(LEAK_TRACK)
  1029. if (g_RegLeakTraceInfo.bEnableLeakTrack) {
  1030. if (ERROR_SUCCESS == ret) {
  1031. (void) TrackObject(*phkResult);
  1032. }
  1033. }
  1034. #endif (LEAK_TRACK)
  1035. *lpSubKey = TmpStr; //Restore original SubKey string
  1036. #endif // LOCAL
  1037. return ret;
  1038. }
  1039. //
  1040. // BaseRegGetVersion - new for Chicago to determine what version a registry
  1041. // key is connected to.
  1042. //
  1043. error_status_t
  1044. BaseRegGetVersion(
  1045. IN HKEY hKey,
  1046. OUT LPDWORD lpdwVersion
  1047. )
  1048. /*++
  1049. Routine Description:
  1050. New for Win95, allows a caller to determine what version a registry
  1051. key is connected to.
  1052. Arguments:
  1053. hKey - Supplies a handle to an open key.
  1054. lpdwVersion - Returns the registry version.
  1055. Return Value:
  1056. Returns ERROR_SUCCESS (0) for success;
  1057. If successful, BaseRegGetVersion returns the registry version in lpdwVersion
  1058. --*/
  1059. {
  1060. if (lpdwVersion != NULL) {
  1061. *lpdwVersion = REMOTE_REGISTRY_VERSION;
  1062. return(ERROR_SUCCESS);
  1063. }
  1064. //
  1065. // ERROR_NOACCESS is kind of a weird thing to return,
  1066. // but we want to return something different in the
  1067. // NULL case because that is how we tell whether we
  1068. // are talking to a Win95 machine. Win95's implementation
  1069. // of BaseRegGetVersion does not actually fill in the
  1070. // version. It just returns ERROR_SUCCESS or
  1071. // ERROR_INVALID_PARAMETER.
  1072. //
  1073. return(ERROR_NOACCESS);
  1074. }