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.

2842 lines
84 KiB

  1. /*************************************************************************
  2. *
  3. * inimap.c
  4. *
  5. * Handle Copy-On-Reference Ini File Mapping
  6. *
  7. * copyright notice: Copyright 1998 Micrsoft
  8. *
  9. *
  10. *************************************************************************/
  11. #include "precomp.h"
  12. #pragma hdrstop
  13. #define LOCAL
  14. #include "regmap.h"
  15. //#include "basedll.h"
  16. #if DBG
  17. ULONG
  18. DbgPrint(
  19. PCH Format,
  20. ...
  21. );
  22. #define DBGPRINT(x) DbgPrint x
  23. #if DBGTRACE
  24. #define TRACE0(x) DbgPrint x
  25. #define TRACE1(x) DbgPrint x
  26. #else
  27. #define TRACE0(x)
  28. #define TRACE1(x)
  29. #endif
  30. #else
  31. #define DBGPRINT(x)
  32. #define TRACE0(x)
  33. #define TRACE1(x)
  34. #endif
  35. #define IS_NEWLINE_CHAR( c ) ((c == 0x0D) || (c == 0x0A))
  36. /*
  37. * INI_BUF_SIZE defines the maximum number of characters that can
  38. * be on a single INI file line. If a line contains more than this
  39. * number of characters, the additional characters will be lost.
  40. */
  41. #define INI_BUF_SIZE 1024
  42. /* Internal Functions */
  43. BOOL
  44. TermsrvDoesFileExist(
  45. PUNICODE_STRING pFileName
  46. );
  47. BOOL
  48. TermsrvBuildSysIniPath(
  49. PUNICODE_STRING pIniPath,
  50. PUNICODE_STRING pSysPath,
  51. PUNICODE_STRING pUserPath
  52. );
  53. BOOL
  54. TermsrvCopyIniFile(
  55. PUNICODE_STRING pSysPath,
  56. PUNICODE_STRING pUserPath,
  57. PUNICODE_STRING pFileName
  58. );
  59. BOOL
  60. TermsrvGetUnicodeRemainder(
  61. PUNICODE_STRING pFullPath,
  62. PUNICODE_STRING pPrefix,
  63. PUNICODE_STRING pRemainder
  64. );
  65. NTSTATUS
  66. TermsrvIniCopyLoop(
  67. HANDLE SrcHandle,
  68. HANDLE DestHandle
  69. );
  70. NTSTATUS
  71. TermsrvPutString(
  72. HANDLE DestHandle,
  73. PCHAR pStr,
  74. ULONG StringSize
  75. );
  76. NTSTATUS
  77. TermsrvProcessBuffer(
  78. PCHAR *ppStr,
  79. PULONG pStrSize,
  80. PULONG pStrBufSize,
  81. PBOOL pSawNL,
  82. PCHAR pIOBuf,
  83. PULONG pIOBufIndex,
  84. PULONG pIOBufFillSize
  85. );
  86. NTSTATUS
  87. TermsrvGetString(
  88. HANDLE SrcHandle,
  89. PCHAR *ppStringPtr,
  90. PULONG pStringSize,
  91. PCHAR pIOBuf,
  92. ULONG IOBufSize,
  93. PULONG pIOBufIndex,
  94. PULONG pIOBufFillSize
  95. );
  96. NTSTATUS
  97. TermsrvIniCopyAndChangeLoop(
  98. HANDLE SrcHandle,
  99. HANDLE DestHandle,
  100. PUNICODE_STRING pUserFullPath,
  101. PUNICODE_STRING pSysFullPath
  102. );
  103. BOOL
  104. TermsrvReallocateBuf(
  105. PCHAR *ppStr,
  106. PULONG pStrBufSize,
  107. ULONG NewSize
  108. );
  109. PCHAR
  110. Ctxstristr( PCHAR pstring1,
  111. PCHAR pstring2
  112. );
  113. NTSTATUS
  114. TermsrvCheckKeys(HANDLE hKeySysRoot,
  115. HANDLE hKeyUsrRoot,
  116. PKEY_BASIC_INFORMATION pKeySysInfo,
  117. PKEY_FULL_INFORMATION pKeyUsrInfo,
  118. ULONG ulcsys,
  119. ULONG ulcusr,
  120. DWORD indentLevel);
  121. NTSTATUS
  122. TermsrvCloneKey(HANDLE hKeySys,
  123. HANDLE hKeyUsr,
  124. PKEY_FULL_INFORMATION pDefKeyInfo,
  125. BOOL fCreateSubKeys);
  126. void TermsrvCheckNewRegEntries(IN LPCWSTR wszBaseKeyName);
  127. BOOL
  128. TermsrvGetUserSyncTime(PULONG pultime);
  129. BOOL
  130. TermsrvSetUserSyncTime(void);
  131. NTSTATUS
  132. TermsrvCheckNewIniFilesInternal(IN LPCWSTR wszBaseKeyName);
  133. NTSTATUS
  134. GetFullKeyPath(
  135. IN HANDLE hKeyParent,
  136. IN LPCWSTR wszKey,
  137. OUT LPWSTR *pwszKeyPath);
  138. PWINSTATIONQUERYINFORMATIONW pWinStationQueryInformationW;
  139. DWORD g_debugIniMap=FALSE;
  140. DWORD IsDebugIniMapEnabled()
  141. {
  142. HKEY hKey;
  143. DWORD rc;
  144. DWORD res=0;
  145. DWORD size;
  146. rc = RegOpenKeyEx( HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Windows NT\\CurrentVersion\\Terminal Server\\Install",
  147. 0, KEY_READ, &hKey );
  148. size = sizeof(DWORD);
  149. if (rc == ERROR_SUCCESS )
  150. {
  151. rc = RegQueryValueEx( hKey, L"debug", NULL , NULL , (LPBYTE ) &res, & size ) ;
  152. if (rc != ERROR_SUCCESS )
  153. {
  154. res = FALSE;
  155. }
  156. RegCloseKey( hKey );
  157. }
  158. return res;
  159. }
  160. void Indent( ULONG indent)
  161. {
  162. ULONG i;
  163. for ( i = 1; i <indent ; i++ )
  164. {
  165. DbgPrint("%ws", L"\t");
  166. }
  167. }
  168. // last param is a unicode string
  169. void Debug1( DWORD indent, DWORD line, WCHAR *where, UNICODE_STRING *pS )
  170. {
  171. WCHAR s[1024];
  172. if (g_debugIniMap)
  173. {
  174. wcsncpy( s, pS->Buffer, pS->Length );
  175. s[pS->Length + 1 ] = L'\0';
  176. Indent( indent );
  177. DbgPrint("L: %4d, %10ws: %ws \n", line, where, s );
  178. }
  179. }
  180. // last param two params, one is the wchar str and the last one is the length. Boy I miss c++ and func overloading...
  181. void Debug2( DWORD indent, DWORD line, WCHAR *where, WCHAR *pS , DWORD length)
  182. {
  183. WCHAR s[1024];
  184. if (g_debugIniMap)
  185. {
  186. wcsncpy( s, pS, length );
  187. s[length + 1 ] = L'\0';
  188. Indent( indent );
  189. DbgPrint("L: %4d, %10ws: %ws \n", line, where, s );
  190. }
  191. }
  192. void DebugTime( DWORD indent, DWORD line, WCHAR *comment, LARGE_INTEGER li )
  193. {
  194. if (g_debugIniMap)
  195. {
  196. Indent( indent );
  197. DbgPrint("L: %4d, %5ws : %I64x \n", line , comment , li.QuadPart );
  198. }
  199. }
  200. /*****************************************************************************
  201. *
  202. * TermsrvGetUserSyncTime
  203. *
  204. * This routine will get the last time we sync'd up this user's .ini files
  205. * and registry values with the system versions.
  206. *
  207. * ENTRY:
  208. * PULONG pultime: pointer to receive last sync time (in seconds since 1970)
  209. *
  210. * EXIT:
  211. * SUCCESS:
  212. * returns TRUE
  213. * FAILURE:
  214. * returns FALSE
  215. *
  216. ****************************************************************************/
  217. BOOL TermsrvGetUserSyncTime(PULONG pultime)
  218. {
  219. ULONG ullen, ultmp;
  220. NTSTATUS Status;
  221. HANDLE hKey, hKeyRoot;
  222. OBJECT_ATTRIBUTES ObjectAttr;
  223. PKEY_VALUE_PARTIAL_INFORMATION pKeyValInfo;
  224. UNICODE_STRING UniString, UserSID;
  225. PWCHAR pwch;
  226. // Allocate a buffer for the key value name and time info
  227. ullen = sizeof(KEY_VALUE_PARTIAL_INFORMATION) + sizeof(ULONG);
  228. pKeyValInfo = RtlAllocateHeap(RtlProcessHeap(),
  229. 0,
  230. ullen);
  231. // If we didn't get the buffer, return
  232. if (!pKeyValInfo) {
  233. return(FALSE);
  234. }
  235. Status = RtlOpenCurrentUser(KEY_READ,
  236. &hKeyRoot);
  237. if (NT_SUCCESS(Status)) {
  238. // Now open up the Citrix key for this user
  239. RtlInitUnicodeString(&UniString,
  240. USER_SOFTWARE_TERMSRV);
  241. InitializeObjectAttributes(&ObjectAttr,
  242. &UniString,
  243. OBJ_CASE_INSENSITIVE,
  244. hKeyRoot,
  245. NULL);
  246. Status = NtCreateKey(&hKey,
  247. KEY_READ,
  248. &ObjectAttr,
  249. 0,
  250. NULL,
  251. REG_OPTION_NON_VOLATILE,
  252. &ultmp);
  253. NtClose(hKeyRoot);
  254. }
  255. // If we opened the key, and it was already there, get the value
  256. if (NT_SUCCESS(Status) && (ultmp == REG_OPENED_EXISTING_KEY)) {
  257. RtlInitUnicodeString(&UniString, TERMSRV_USER_SYNCTIME);
  258. Status = NtQueryValueKey(hKey,
  259. &UniString,
  260. KeyValuePartialInformation,
  261. pKeyValInfo,
  262. ullen,
  263. &ultmp);
  264. NtClose(hKey);
  265. if (NT_SUCCESS(Status)) {
  266. *pultime = *(PULONG)pKeyValInfo->Data;
  267. }
  268. } else {
  269. Status = STATUS_OBJECT_NAME_NOT_FOUND;
  270. }
  271. RtlFreeHeap( RtlProcessHeap(), 0, pKeyValInfo );
  272. return(NT_SUCCESS(Status));
  273. }
  274. /*****************************************************************************
  275. *
  276. * TermsrvSetUserSyncTime
  277. *
  278. * This routine will set the current time as this user's last .ini file
  279. * sync time.
  280. *
  281. * ENTRY:
  282. *
  283. * EXIT:
  284. * SUCCESS:
  285. * returns TRUE
  286. * FAILURE:
  287. * returns FALSE
  288. *
  289. ****************************************************************************/
  290. BOOL TermsrvSetUserSyncTime(void)
  291. {
  292. ULONG ultmp;
  293. NTSTATUS Status;
  294. HANDLE hKey, hKeyRoot;
  295. OBJECT_ATTRIBUTES ObjectAttr;
  296. UNICODE_STRING UniString;
  297. PWCHAR pwch;
  298. FILETIME FileTime;
  299. Status = RtlOpenCurrentUser(KEY_WRITE,
  300. &hKeyRoot);
  301. if (NT_SUCCESS(Status)) {
  302. // Now open up the Citrix key for this user
  303. RtlInitUnicodeString(&UniString,
  304. USER_SOFTWARE_TERMSRV);
  305. InitializeObjectAttributes(&ObjectAttr,
  306. &UniString,
  307. OBJ_CASE_INSENSITIVE,
  308. hKeyRoot,
  309. NULL);
  310. Status = NtCreateKey(&hKey,
  311. KEY_WRITE,
  312. &ObjectAttr,
  313. 0,
  314. NULL,
  315. REG_OPTION_NON_VOLATILE,
  316. &ultmp);
  317. NtClose(hKeyRoot);
  318. }
  319. // If we opened the key, and set the sync time value
  320. if (NT_SUCCESS(Status)) {
  321. // Get the system time, convert to local time, and convert to seconds
  322. GetSystemTimeAsFileTime(&FileTime);
  323. RtlTimeToSecondsSince1970((PLARGE_INTEGER)&FileTime,
  324. &ultmp);
  325. RtlInitUnicodeString(&UniString,
  326. TERMSRV_USER_SYNCTIME);
  327. // Now store it under the citrix key in the registry
  328. Status = NtSetValueKey(hKey,
  329. &UniString,
  330. 0,
  331. REG_DWORD,
  332. &ultmp,
  333. sizeof(ultmp));
  334. NtClose(hKey);
  335. } else {
  336. Status = STATUS_OBJECT_NAME_NOT_FOUND;
  337. }
  338. return(NT_SUCCESS(Status));
  339. }
  340. /*****************************************************************************
  341. *
  342. * TermsrvCORIniFile
  343. *
  344. * Copy On Reference an Ini file
  345. *
  346. * This function is called to copy an ini file from the system
  347. * directory to a users local ini file directory.
  348. *
  349. * The path supplied is the fully translated TERMSRV INI file path,
  350. * whichs points to a users directory.
  351. *
  352. * This string is used to find the system ini file, and copy it to the
  353. * users directory.
  354. *
  355. * All paths are NT paths, NOT WIN32 paths.
  356. *
  357. * Example:
  358. *
  359. * \DosDevices\U:\users\default\windows\win.ini is the path given
  360. *
  361. * %SystemRoot%\win.ini is the "default" location with ini mapping off.
  362. *
  363. * If \DosDevices\U:\users\default\windows\win.ini does not exist, test to see if
  364. * %SystemRoot%\win.ini exists, and if does, copy the system version
  365. * to the users directory.
  366. *
  367. * NOTE: If the path is to the normal unmapped system directory, just
  368. * return since there is no mapping occuring.
  369. *
  370. * ENTRY:
  371. * pUserFullPath (input)
  372. * Translated TERMSRV INI path name
  373. *
  374. * EXIT:
  375. *
  376. ****************************************************************************/
  377. VOID
  378. TermsrvCORIniFile(
  379. PUNICODE_STRING pUserFullPath
  380. )
  381. {
  382. DWORD Result;
  383. BOOL rc;
  384. UNICODE_STRING SysFullPath;
  385. UNICODE_STRING UserBasePath;
  386. /*
  387. * If in install mode, just return to make
  388. * everything behave as stock NT.
  389. */
  390. if ( IsSystemLUID() || TermsrvAppInstallMode() ) {
  391. TRACE0(("TermsrvCORIniFile: INI file mapping is OFF\n"));
  392. return;
  393. }
  394. if (!TermsrvPerUserWinDirMapping()) {
  395. return;
  396. }
  397. /*
  398. * If a NULL file name, just return
  399. */
  400. if( (pUserFullPath == NULL) || (pUserFullPath->Buffer == NULL) ) {
  401. TRACE0(("TermsrvCORIniFile: NULL File INI file name\n"));
  402. return;
  403. }
  404. /*
  405. * Test if user file exists
  406. */
  407. if( TermsrvDoesFileExist( pUserFullPath ) ) {
  408. TRACE0(("TermsrvCORIniFile: File %ws Exists\n",pUserFullPath->Buffer));
  409. //
  410. // Nothing to do if the user already has a copy
  411. //
  412. return;
  413. }
  414. else {
  415. TRACE0(("TermsrvCORIniFile: File %ws DOES NOT Exist!\n",pUserFullPath->Buffer));
  416. }
  417. /*
  418. * The requested ini file does not exist in the users local
  419. * directory. We must change the path name to point to the system
  420. * directory, and test if the ini file exists there.
  421. */
  422. /*
  423. * Build full system path to the Ini file.
  424. *
  425. * This also parses out the users base path and returns that as well.
  426. */
  427. if( !TermsrvBuildSysIniPath( pUserFullPath, &SysFullPath, &UserBasePath ) ) {
  428. TRACE0(("TermsrvCORIniFile: Error building Sys Ini Path!\n"));
  429. return;
  430. }
  431. /*
  432. * Test if system version exists
  433. */
  434. if( !TermsrvDoesFileExist( &SysFullPath ) ) {
  435. //
  436. // It does not exist in the system directory either,
  437. // so we just return.
  438. //
  439. TRACE0(("TermsrvCORIniFile: Path %ws does not exist in system dir, Length %d\n",SysFullPath.Buffer,SysFullPath.Length));
  440. TRACE0(("TermsrvCORIniFile: UserPath %ws\n",pUserFullPath->Buffer));
  441. RtlFreeHeap( RtlProcessHeap(), 0, SysFullPath.Buffer );
  442. RtlFreeHeap( RtlProcessHeap(), 0, UserBasePath.Buffer );
  443. return;
  444. }
  445. /*
  446. * Now Copy it.
  447. *
  448. * The copy routine could also translate any paths internal to the
  449. * ini file that point to the system directory, to point to the user
  450. * directory in the base path.
  451. */
  452. rc = TermsrvCopyIniFile( &SysFullPath, &UserBasePath, pUserFullPath);
  453. #if DBG
  454. if( !rc ) {
  455. DBGPRINT(("TermsrvCORIniFile: Could not copy file %ws to %ws\n",SysFullPath.Buffer,pUserFullPath->Buffer));
  456. }
  457. #endif
  458. RtlFreeHeap( RtlProcessHeap(), 0, SysFullPath.Buffer );
  459. RtlFreeHeap( RtlProcessHeap(), 0, UserBasePath.Buffer );
  460. return;
  461. }
  462. /*****************************************************************************
  463. *
  464. * TermsrvDoesFileExist
  465. *
  466. * Returns whether the file exists or not.
  467. *
  468. * Must use NT, not WIN32 pathnames.
  469. *
  470. * ENTRY:
  471. * Param1 (input/output)
  472. * Comments
  473. *
  474. * EXIT:
  475. * STATUS_SUCCESS - no error
  476. *
  477. ****************************************************************************/
  478. BOOL
  479. TermsrvDoesFileExist(
  480. PUNICODE_STRING pFileName
  481. )
  482. {
  483. NTSTATUS Status;
  484. FILE_BASIC_INFORMATION BasicInfo;
  485. OBJECT_ATTRIBUTES Obja;
  486. InitializeObjectAttributes(
  487. &Obja,
  488. pFileName,
  489. OBJ_CASE_INSENSITIVE,
  490. NULL,
  491. NULL
  492. );
  493. /*
  494. * Now query it
  495. */
  496. Status = NtQueryAttributesFile( &Obja, &BasicInfo );
  497. if( NT_SUCCESS( Status ) ) {
  498. return( TRUE );
  499. }
  500. return( FALSE );
  501. }
  502. /*****************************************************************************
  503. *
  504. * TermsrvBuildSysIniPath
  505. *
  506. * Builds the full ini path to pointing to the system directory
  507. * from the users private ini path.
  508. *
  509. * Also returns the users base ini path directory to be used by
  510. * the ini file path conversion when copying.
  511. *
  512. * ENTRY:
  513. * Param1 (input/output)
  514. * Comments
  515. *
  516. * EXIT:
  517. * STATUS_SUCCESS - no error
  518. *
  519. ****************************************************************************/
  520. BOOL
  521. TermsrvBuildSysIniPath(
  522. PUNICODE_STRING pUserFullPath,
  523. PUNICODE_STRING pSysFullPath,
  524. PUNICODE_STRING pUserBasePath
  525. )
  526. {
  527. BOOL rc = FALSE;
  528. NTSTATUS Status;
  529. UNICODE_STRING SysBasePath;
  530. UNICODE_STRING IniPathTail;
  531. UNICODE_STRING UniSysDir;
  532. WCHAR CtxWindowsPath[MAX_PATH+1];
  533. UNICODE_STRING CtxWindowsDir = {
  534. sizeof(CtxWindowsPath),
  535. sizeof(CtxWindowsPath),
  536. CtxWindowsPath
  537. };
  538. OBJECT_ATTRIBUTES ObjectAttr;
  539. HKEY hKey = 0;
  540. ULONG ul;
  541. PKEY_VALUE_FULL_INFORMATION pKeyValInfo;
  542. WCHAR SystemWindowsDirectory[MAX_PATH+1];
  543. if (!TermsrvPerUserWinDirMapping()) {
  544. return FALSE;
  545. }
  546. SysBasePath.Buffer = NULL;
  547. pKeyValInfo = RtlAllocateHeap(RtlProcessHeap(),
  548. 0,
  549. sizeof(KEY_VALUE_FULL_INFORMATION) + MAX_PATH
  550. );
  551. if (pKeyValInfo) {
  552. RtlInitUnicodeString(&UniSysDir,
  553. TERMSRV_COMPAT
  554. );
  555. InitializeObjectAttributes(&ObjectAttr,
  556. &UniSysDir,
  557. OBJ_CASE_INSENSITIVE,
  558. NULL,
  559. NULL
  560. );
  561. Status = NtOpenKey(&hKey, KEY_READ, &ObjectAttr);
  562. if (NT_SUCCESS(Status)) {
  563. RtlInitUnicodeString(&UniSysDir, L"SYSDIR");
  564. Status = NtQueryValueKey(hKey,
  565. &UniSysDir,
  566. KeyValueFullInformation,
  567. pKeyValInfo,
  568. sizeof(KEY_VALUE_FULL_INFORMATION) +
  569. MAX_PATH,
  570. &ul
  571. );
  572. if (NT_SUCCESS(Status)) {
  573. NtClose(hKey);
  574. if (ul = wcslen((PWCHAR)((PCHAR)pKeyValInfo +
  575. pKeyValInfo->DataOffset))) {
  576. RtlInitUnicodeString(&UniSysDir,
  577. (PWCHAR)((PCHAR)pKeyValInfo +
  578. pKeyValInfo->DataOffset)
  579. );
  580. // Convert to an NT path
  581. rc = RtlDosPathNameToNtPathName_U(
  582. UniSysDir.Buffer,
  583. &SysBasePath,
  584. NULL,
  585. NULL
  586. );
  587. // Was this a valid path? If not, use actual system directory.
  588. if (rc && !TermsrvDoesFileExist(&SysBasePath)) {
  589. RtlFreeHeap( RtlProcessHeap(), 0, SysBasePath.Buffer );
  590. SysBasePath.Buffer = NULL;
  591. rc = FALSE;
  592. }
  593. // if the path is the root, get rid of last backslash
  594. if (ul == 3 && SysBasePath.Buffer) {
  595. SysBasePath.Buffer[SysBasePath.Length/sizeof(WCHAR)] = L'\0';
  596. SysBasePath.Length -= 2;
  597. }
  598. }
  599. }
  600. }
  601. }
  602. GetSystemWindowsDirectory(SystemWindowsDirectory,(MAX_PATH * sizeof(WCHAR)));
  603. if (!rc) {
  604. /*
  605. * We must convert the SystemWindowsDirectory from a WIN32 path to
  606. * an NT path.
  607. */
  608. rc = RtlDosPathNameToNtPathName_U( SystemWindowsDirectory,
  609. &SysBasePath,
  610. NULL,
  611. NULL
  612. );
  613. }
  614. if (pKeyValInfo) {
  615. RtlFreeHeap( RtlProcessHeap(), 0, pKeyValInfo );
  616. }
  617. TRACE0(("BaseWindowsDirectory is %ws\n",SystemWindowsDirectory));
  618. if( !rc ) {
  619. DBGPRINT(("BuildSysIniPath: Error translating system path to NT path %ws\n",SystemWindowsDirectory));
  620. return( FALSE );
  621. }
  622. TRACE0(("BuildSysIniPath: NT SYS path is %ws\n",SysBasePath.Buffer));
  623. /*
  624. * Get the users windows path prefix
  625. */
  626. Status = GetPerUserWindowsDirectory( &CtxWindowsDir );
  627. if( !NT_SUCCESS( Status ) ) {
  628. DBGPRINT(("BuildSysIniPath: Could not get TermsrvWindowsDir 0x%x\n",Status));
  629. RtlFreeHeap( RtlProcessHeap(), 0, SysBasePath.Buffer );
  630. return( FALSE );
  631. }
  632. /*
  633. * Now convert it into an NT path
  634. */
  635. rc = RtlDosPathNameToNtPathName_U(
  636. CtxWindowsDir.Buffer,
  637. pUserBasePath,
  638. NULL,
  639. NULL
  640. );
  641. if( !rc ) {
  642. DBGPRINT(("BuildSysIniPath: Could not convert TermsrvWindowsDir %d\n",rc));
  643. RtlFreeHeap( RtlProcessHeap(), 0, SysBasePath.Buffer );
  644. return( FALSE );
  645. }
  646. TRACE0(("BuildSysIniPath: Users Ini PathBase is %ws\n",pUserBasePath->Buffer));
  647. //
  648. // Here we have:
  649. //
  650. // SysBasePath, UserBasePath
  651. //
  652. // UserFullPath, must now build SysFullPath
  653. //
  654. rc = TermsrvGetUnicodeRemainder( pUserFullPath, pUserBasePath, &IniPathTail );
  655. if( !rc ) {
  656. WCHAR szShortPath[MAX_PATH];
  657. WCHAR szPath[MAX_PATH];
  658. UNICODE_STRING ShortPath;
  659. //
  660. // GetShortPathName doesn't take NT Path. Strip out "\??\"
  661. //
  662. if (!wcsncmp(pUserBasePath->Buffer,L"\\??\\",4)) {
  663. wcsncpy(szPath,&(pUserBasePath->Buffer[4]),(pUserBasePath->Length - 4));
  664. } else {
  665. wcsncpy(szPath,pUserBasePath->Buffer,pUserBasePath->Length);
  666. }
  667. if (GetShortPathNameW(szPath,szShortPath,MAX_PATH)) {
  668. if (!wcsncmp(pUserBasePath->Buffer,L"\\??\\",4)) {
  669. wcscpy(szPath,L"\\??\\");
  670. wcscat(szPath,szShortPath);
  671. } else {
  672. wcscpy(szPath,szShortPath);
  673. }
  674. RtlInitUnicodeString(&ShortPath,szPath);
  675. rc = TermsrvGetUnicodeRemainder( pUserFullPath, &ShortPath, &IniPathTail );
  676. }
  677. if (!rc) {
  678. RtlFreeHeap( RtlProcessHeap(), 0, SysBasePath.Buffer );
  679. RtlFreeHeap( RtlProcessHeap(), 0, pUserBasePath->Buffer );
  680. return( FALSE );
  681. }
  682. }
  683. pSysFullPath->Length = 0;
  684. pSysFullPath->MaximumLength = (MAX_PATH+1)*sizeof(WCHAR);
  685. pSysFullPath->Buffer = RtlAllocateHeap( RtlProcessHeap(), 0, pSysFullPath->MaximumLength );
  686. if( pSysFullPath->Buffer == NULL ) {
  687. DBGPRINT(("BuildSysPath: Error in memory allocate\n"));
  688. RtlFreeHeap( RtlProcessHeap(), 0, SysBasePath.Buffer );
  689. RtlFreeHeap( RtlProcessHeap(), 0, IniPathTail.Buffer );
  690. RtlFreeHeap( RtlProcessHeap(), 0, pUserBasePath->Buffer );
  691. return( FALSE );
  692. }
  693. TRACE0(("BuildSysPath: IniPathTail :%ws:, Length %d\n",IniPathTail.Buffer,IniPathTail.Length));
  694. RtlCopyUnicodeString( pSysFullPath, &SysBasePath );
  695. if ((pSysFullPath->Buffer[pSysFullPath->Length/sizeof(WCHAR) -1 ] != L'\\') &&
  696. (IniPathTail.Buffer[0] != L'\\')) { // check whether need "\\"
  697. Status = RtlAppendUnicodeToString(pSysFullPath, L"\\");
  698. if ( !NT_SUCCESS( Status) ) {
  699. DBGPRINT(("BuildSysPath: Error appending UnicodeStirng\n",Status));
  700. return( FALSE );
  701. }
  702. }
  703. Status = RtlAppendUnicodeStringToString( pSysFullPath, &IniPathTail );
  704. if( !NT_SUCCESS( Status ) ) {
  705. DBGPRINT(("BuildSysPath: Error 0x%x appending UnicodeString\n",Status));
  706. RtlFreeHeap( RtlProcessHeap(), 0, SysBasePath.Buffer );
  707. RtlFreeHeap( RtlProcessHeap(), 0, IniPathTail.Buffer );
  708. RtlFreeHeap( RtlProcessHeap(), 0, pUserBasePath->Buffer );
  709. RtlFreeHeap( RtlProcessHeap(), 0, pSysFullPath->Buffer );
  710. return( FALSE );
  711. }
  712. TRACE0(("BuildSysPath: SysFullPath :%ws:, Length %d\n",pSysFullPath->Buffer,pSysFullPath->Length));
  713. /*
  714. * Free the local resources allocated
  715. */
  716. RtlFreeHeap( RtlProcessHeap(), 0, SysBasePath.Buffer );
  717. RtlFreeHeap( RtlProcessHeap(), 0, IniPathTail.Buffer );
  718. return( TRUE );
  719. }
  720. /*****************************************************************************
  721. *
  722. * TermsrvGetUnicodeRemainder
  723. *
  724. * Given the full path, and a prefix, return the remainder of
  725. * the UNICODE_STRING in newly allocated buffer space.
  726. *
  727. * ENTRY:
  728. * Param1 (input/output)
  729. * Comments
  730. *
  731. * EXIT:
  732. * TRUE - no error
  733. * FALSE - error
  734. *
  735. ****************************************************************************/
  736. BOOL
  737. TermsrvGetUnicodeRemainder(
  738. PUNICODE_STRING pFullPath,
  739. PUNICODE_STRING pPrefix,
  740. PUNICODE_STRING pRemainder
  741. )
  742. {
  743. WCHAR c1, c2;
  744. USHORT Index, RemIndex;
  745. USHORT PathLen, PrefixLen, RemLen;
  746. PathLen = pFullPath->Length / sizeof(WCHAR);
  747. PrefixLen = pPrefix->Length / sizeof(WCHAR);
  748. if( (PathLen == 0) || (PrefixLen == 0) ) {
  749. TRACE1(("TermsrvGetUnicodeRemainder: 0 PathLength Full %d, Prefix %d\n",PathLen,PrefixLen));
  750. return( FALSE );
  751. }
  752. Index = 0;
  753. while( PathLen && PrefixLen ) {
  754. c1 = pFullPath->Buffer[Index];
  755. c2 = pPrefix->Buffer[Index];
  756. // Do a fast case insensitive compare
  757. if( (c1 != c2) && (towupper(c1) != towupper(c2)) ) {
  758. TRACE1(("TermsrvGetUnicodeRemainder: Non matching character Index %d\n",Index));
  759. return( FALSE );
  760. }
  761. PathLen--;
  762. PrefixLen--;
  763. Index++;
  764. }
  765. // If prefix is longer, its an error
  766. if( PrefixLen ) {
  767. TRACE1(("TermsrvGetUnicodeRemainder: Prefix is longer\n"));
  768. return(FALSE);
  769. }
  770. // If PathLen is 0, there is no remainder.
  771. if( PathLen == 0 ) {
  772. RemLen = 0;
  773. }
  774. else {
  775. RemLen = PathLen;
  776. }
  777. // Allocate memory for remainder, including a UNICODE_NULL
  778. pRemainder->Length = RemLen*sizeof(WCHAR);
  779. pRemainder->MaximumLength = (RemLen+1)*sizeof(WCHAR);
  780. pRemainder->Buffer = RtlAllocateHeap( RtlProcessHeap(), 0, pRemainder->MaximumLength );
  781. if( pRemainder->Buffer == NULL ) {
  782. TRACE1(("TermsrvGetUnicodeRemainder: Memory allocation error\n"));
  783. return( FALSE );
  784. }
  785. RemIndex = 0;
  786. while( RemLen ) {
  787. pRemainder->Buffer[RemIndex] = pFullPath->Buffer[Index];
  788. Index++;
  789. RemIndex++;
  790. RemLen--;
  791. }
  792. // Now include the UNICODE_NULL
  793. pRemainder->Buffer[RemIndex] = UNICODE_NULL;
  794. TRACE0(("TermsrvGetUnicodeRemainder: Remainder %ws\n",pRemainder->Buffer));
  795. return( TRUE );
  796. }
  797. /*****************************************************************************
  798. *
  799. * TermsrvCopyIniFile
  800. *
  801. * Copies the INI file from the system directory to the
  802. * users directory.
  803. *
  804. * Any paths inside the INI file that match pUserBasePath and do not point
  805. * to a shareable application resource will be translated.
  806. *
  807. * ENTRY:
  808. * PUNICODE_STRING pSysFullPath (In) - Path of ini file in system dir (source)
  809. * PUNICODE_STRING pUserBasePath (In) - Optional, User's windows home dir
  810. * PUNICODE_STRING pUserFullPath (In) - Path of ini file in user's home dir (dest)
  811. *
  812. * Notes:
  813. * If pUserBasePath is NULL, no path substitution is done as the ini file is
  814. * copied from the system directory to the user's home directory.
  815. *
  816. * EXIT:
  817. * STATUS_SUCCESS - no error
  818. *
  819. ****************************************************************************/
  820. BOOL
  821. TermsrvCopyIniFile(
  822. PUNICODE_STRING pSysFullPath,
  823. PUNICODE_STRING pUserBasePath,
  824. PUNICODE_STRING pUserFullPath
  825. )
  826. {
  827. NTSTATUS Status;
  828. HANDLE SrcHandle, DestHandle;
  829. OBJECT_ATTRIBUTES SrcObja;
  830. OBJECT_ATTRIBUTES DestObja;
  831. IO_STATUS_BLOCK SrcIosb;
  832. IO_STATUS_BLOCK DestIosb;
  833. PWCHAR pwch, pwcIniName;
  834. ULONG ulCompatFlags;
  835. TRACE0(("TermsrvCopyIniFile: From %ws, TO -> %ws\n",pSysFullPath->Buffer,pUserFullPath->Buffer));
  836. TRACE0(("UserBasePath %ws\n",pUserBasePath->Buffer));
  837. /*
  838. * This must all be done at the NT level
  839. */
  840. InitializeObjectAttributes(
  841. &SrcObja,
  842. pSysFullPath,
  843. OBJ_CASE_INSENSITIVE,
  844. NULL,
  845. NULL
  846. );
  847. InitializeObjectAttributes(
  848. &DestObja,
  849. pUserFullPath,
  850. OBJ_CASE_INSENSITIVE,
  851. NULL,
  852. NULL
  853. );
  854. // Open the src
  855. SrcIosb.Status = STATUS_SUCCESS;
  856. Status = NtOpenFile(
  857. &SrcHandle,
  858. FILE_GENERIC_READ,
  859. &SrcObja,
  860. &SrcIosb,
  861. FILE_SHARE_READ|FILE_SHARE_WRITE,
  862. FILE_SYNCHRONOUS_IO_NONALERT // OpenOptions
  863. );
  864. if( NT_SUCCESS(Status) ) {
  865. // Get final I/O status
  866. Status = SrcIosb.Status;
  867. }
  868. if( !NT_SUCCESS(Status) ) {
  869. DBGPRINT(("TermsrvCopyIniFile: Error 0x%x opening SrcFile %ws\n",Status,pSysFullPath->Buffer));
  870. return( FALSE );
  871. }
  872. // Create the destination file
  873. DestIosb.Status = STATUS_SUCCESS;
  874. Status = NtCreateFile(
  875. &DestHandle,
  876. FILE_READ_DATA | FILE_WRITE_DATA | FILE_READ_ATTRIBUTES | SYNCHRONIZE,
  877. &DestObja,
  878. &DestIosb,
  879. NULL, // Allocation size
  880. FILE_ATTRIBUTE_NORMAL, // dwFlagsAndAttributes
  881. FILE_SHARE_WRITE, // dwShareMode
  882. FILE_OVERWRITE_IF, // CreateDisposition
  883. FILE_SYNCHRONOUS_IO_NONALERT | FILE_NON_DIRECTORY_FILE, // CreateFlags
  884. NULL, // EaBuffer
  885. 0 // EaLength
  886. );
  887. if( NT_SUCCESS(Status) ) {
  888. // Get final I/O status
  889. Status = DestIosb.Status;
  890. }
  891. if( !NT_SUCCESS(Status) ) {
  892. DBGPRINT(("TermsrvCopyIniFile: Error 0x%x Creating DestFile %ws\n",Status,pUserFullPath->Buffer));
  893. NtClose( SrcHandle );
  894. return( FALSE );
  895. }
  896. TRACE0(("TermsrvCopyFile: Create Disposition 0x%x\n",DestIosb.Information));
  897. // Get the ini file name
  898. pwch = wcsrchr(pSysFullPath->Buffer, L'\\') + 1;
  899. pwcIniName = RtlAllocateHeap( RtlProcessHeap(),
  900. 0,
  901. (wcslen(pwch) + 1)*sizeof(WCHAR));
  902. if(!pwcIniName)
  903. {
  904. DBGPRINT(("TermsrvCopyIniFile: Error Allocating pwcIniName\n"));
  905. NtClose( SrcHandle );
  906. NtClose( DestHandle );
  907. return( FALSE );
  908. }
  909. wcscpy(pwcIniName, pwch);
  910. pwch = wcsrchr(pwcIniName, L'.');
  911. if (pwch) {
  912. *pwch = L'\0';
  913. }
  914. GetTermsrCompatFlags(pwcIniName, &ulCompatFlags, CompatibilityIniFile);
  915. RtlFreeHeap( RtlProcessHeap(), 0, pwcIniName );
  916. /*
  917. * Now do the copy loop
  918. */
  919. if (pUserBasePath && !(ulCompatFlags & TERMSRV_COMPAT_ININOSUB)) {
  920. Status = TermsrvIniCopyAndChangeLoop( SrcHandle,
  921. DestHandle,
  922. pUserBasePath,
  923. pSysFullPath
  924. );
  925. } else {
  926. Status = TermsrvIniCopyLoop( SrcHandle, DestHandle );
  927. }
  928. if( !NT_SUCCESS(Status) ) {
  929. DBGPRINT(("TermsrvCopyIniFile: Error 0x%x Doing copy loop\n",Status));
  930. NtClose( SrcHandle );
  931. NtClose( DestHandle );
  932. return( FALSE );
  933. }
  934. /*
  935. * Close the file handles
  936. */
  937. NtClose( SrcHandle );
  938. NtClose( DestHandle );
  939. return( TRUE );
  940. }
  941. /*****************************************************************************
  942. *
  943. * TermsrvIniCopyLoop
  944. *
  945. * Actual copy loop. This copies the src ini file to the destination
  946. * ini file.
  947. *
  948. * ENTRY:
  949. * Param1 (input/output)
  950. * Comments
  951. *
  952. * EXIT:
  953. * STATUS_SUCCESS - no error
  954. *
  955. ****************************************************************************/
  956. NTSTATUS
  957. TermsrvIniCopyLoop(
  958. HANDLE SrcHandle,
  959. HANDLE DestHandle
  960. )
  961. {
  962. NTSTATUS Status;
  963. PCHAR pBuf = NULL;
  964. IO_STATUS_BLOCK Iosb;
  965. pBuf = LocalAlloc( LPTR, INI_BUF_SIZE );
  966. if ( !pBuf ) {
  967. Status = STATUS_INSUFFICIENT_RESOURCES;
  968. goto Cleanup;
  969. }
  970. while( 1 ) {
  971. Iosb.Status = STATUS_SUCCESS;
  972. Status = NtReadFile(
  973. SrcHandle,
  974. NULL, // Event
  975. NULL, // APC routine
  976. NULL, // APC context
  977. &Iosb,
  978. pBuf,
  979. INI_BUF_SIZE,
  980. NULL, // ByteOffset (not used since in synchronous I/O)
  981. NULL // Key
  982. );
  983. if( Status == STATUS_PENDING ) {
  984. Status = NtWaitForSingleObject( SrcHandle, FALSE, NULL );
  985. }
  986. if( NT_SUCCESS(Status) ) {
  987. // Get final I/O status
  988. Status = Iosb.Status;
  989. }
  990. if( !NT_SUCCESS(Status) ) {
  991. if( Status == STATUS_END_OF_FILE ) {
  992. Status = STATUS_SUCCESS;
  993. goto Cleanup;
  994. }
  995. DBGPRINT(("TermsrvIniCopyLoop: Error 0x%x doing NtReadFile\n",Status));
  996. goto Cleanup;
  997. }
  998. Iosb.Status = STATUS_SUCCESS;
  999. Status = NtWriteFile(
  1000. DestHandle,
  1001. NULL, // Event
  1002. NULL, // APC routine
  1003. NULL, // APC context
  1004. &Iosb,
  1005. pBuf,
  1006. (ULONG)Iosb.Information, // Actual amount read
  1007. NULL, // ByteOffset (not used since in synchronous I/O)
  1008. NULL // Key
  1009. );
  1010. if( Status == STATUS_PENDING ) {
  1011. Status = NtWaitForSingleObject( DestHandle, FALSE, NULL );
  1012. }
  1013. if( NT_SUCCESS(Status) ) {
  1014. // Get final I/O status
  1015. Status = Iosb.Status;
  1016. }
  1017. if( !NT_SUCCESS(Status) ) {
  1018. DBGPRINT(("TermsrvIniCopyLoop: Error 0x%x doing NtWriteFile\n",Status));
  1019. goto Cleanup;
  1020. }
  1021. } // end while(1)
  1022. Cleanup:
  1023. if ( pBuf ) {
  1024. LocalFree( pBuf );
  1025. }
  1026. return( Status );
  1027. }
  1028. /*****************************************************************************
  1029. *
  1030. * TermsrvIniCopyAndChangeLoop
  1031. *
  1032. * Actual copy loop. This copies the src ini file to the destination
  1033. * ini file. It also handles any path translations.
  1034. *
  1035. * ENTRY:
  1036. * HANDLE SrcHandle (In) - Source file handle
  1037. * HANDLE DestHandle (In) - Destination file handle
  1038. * PUNICODE_STRING pUserFullPath (In) - Ptr to Uni string with user's home
  1039. * windows dir
  1040. *
  1041. * EXIT:
  1042. * STATUS_SUCCESS - no error
  1043. *
  1044. ****************************************************************************/
  1045. NTSTATUS
  1046. TermsrvIniCopyAndChangeLoop(
  1047. HANDLE SrcHandle,
  1048. HANDLE DestHandle,
  1049. PUNICODE_STRING pUserFullPath,
  1050. PUNICODE_STRING pSysFullPath
  1051. )
  1052. {
  1053. PCHAR pStr, pch, ptemp, pnext;
  1054. PWCHAR pwch;
  1055. NTSTATUS Status;
  1056. ULONG StringSize;
  1057. CHAR IOBuf[512];
  1058. ULONG IOBufSize = 512;
  1059. ULONG IOBufIndex = 0;
  1060. ULONG IOBufFillSize = 0;
  1061. ANSI_STRING AnsiUserDir, AnsiSysDir;
  1062. UNICODE_STRING UniString;
  1063. // Get the DOS filename from the NT file name
  1064. if (pwch = wcschr(pUserFullPath->Buffer, L':')) {
  1065. pwch--;
  1066. } else {
  1067. pwch = pUserFullPath->Buffer;
  1068. }
  1069. RtlInitUnicodeString( &UniString, pwch );
  1070. Status = RtlUnicodeStringToAnsiString( &AnsiUserDir,
  1071. &UniString,
  1072. TRUE
  1073. );
  1074. if (!NT_SUCCESS(Status)) {
  1075. DBGPRINT(("TermsrvIniCopyAndChangeLoop: Error 0x%x converting user dir\n", Status));
  1076. return(Status);
  1077. }
  1078. // Get the system directory from the fully qualified system path
  1079. if (pwch = wcschr(pSysFullPath->Buffer, L':')) {
  1080. pwch--;
  1081. } else {
  1082. pwch = pUserFullPath->Buffer;
  1083. }
  1084. RtlInitUnicodeString( &UniString, pwch );
  1085. Status = RtlUnicodeStringToAnsiString( &AnsiSysDir,
  1086. &UniString,
  1087. TRUE
  1088. );
  1089. if (!NT_SUCCESS(Status)) {
  1090. DBGPRINT(("TermsrvIniCopyAndChangeLoop: Error 0x%x converting system dir\n", Status));
  1091. RtlFreeAnsiString( &AnsiUserDir );
  1092. return(Status);
  1093. }
  1094. pch = strrchr(AnsiSysDir.Buffer, '\\');
  1095. // unless something has gone wrong, we should always have a pch since a full-path always
  1096. // has at least "\" in it, and actually in our case, we have atleast two slashes inside,
  1097. // since we are dealing with a string such as "\A\file.ini", where 'A' is a folder
  1098. // name that has at least one letter in it
  1099. if (pch)
  1100. {
  1101. if ((pch - AnsiSysDir.Buffer) > 2) {
  1102. *pch = '\0';
  1103. } else {
  1104. *(pch+1) = '\0';
  1105. }
  1106. AnsiSysDir.Length = (USHORT) strlen(AnsiSysDir.Buffer);
  1107. while( 1 ) {
  1108. pStr = NULL;
  1109. StringSize = 0;
  1110. /*
  1111. * Get a string from the source ini file
  1112. */
  1113. Status = TermsrvGetString(
  1114. SrcHandle,
  1115. &pStr,
  1116. &StringSize,
  1117. IOBuf,
  1118. IOBufSize,
  1119. &IOBufIndex,
  1120. &IOBufFillSize
  1121. );
  1122. if( !NT_SUCCESS(Status) ) {
  1123. ASSERT( pStr == NULL );
  1124. RtlFreeAnsiString( &AnsiUserDir );
  1125. RtlFreeAnsiString( &AnsiSysDir );
  1126. if( Status == STATUS_END_OF_FILE ) {
  1127. return( STATUS_SUCCESS );
  1128. }
  1129. return( Status );
  1130. }
  1131. /*
  1132. * Process the string for any ini path translations
  1133. */
  1134. ASSERT( pStr != NULL );
  1135. // Go through the string looking for anything that contains the system
  1136. // directory.
  1137. if (pch = Ctxstristr(pStr, AnsiSysDir.Buffer)) {
  1138. // See if this entry might point to an ini file
  1139. if ((ptemp = strchr(pch, '.')) && !(_strnicmp(ptemp, ".ini", 4))) {
  1140. // Check to make sure this is the right string to replace
  1141. pnext = pch + AnsiSysDir.Length + 1;
  1142. while (pch && (pnext < ptemp)) {
  1143. // Check for another entry
  1144. if (*pnext == ',') {
  1145. pch = Ctxstristr(pnext, AnsiSysDir.Buffer);
  1146. if (pch) {
  1147. pnext = pch + AnsiSysDir.Length + 1;
  1148. }
  1149. }
  1150. pnext++;
  1151. }
  1152. // Check that this .ini is in the system directory
  1153. pnext = pch + AnsiSysDir.Length + 1;
  1154. while (pch && (pnext < ptemp)) {
  1155. if (*pnext == '\\') {
  1156. pch = NULL;
  1157. }
  1158. pnext++;
  1159. }
  1160. if (pch && (pch < ptemp)) {
  1161. ptemp = RtlAllocateHeap( RtlProcessHeap(),
  1162. 0,
  1163. StringSize + AnsiUserDir.Length );
  1164. strncpy(ptemp, pStr, (size_t)(pch - pStr)); // copy up to sys dir
  1165. ptemp[pch - pStr] = '\0';
  1166. strcat(ptemp, AnsiUserDir.Buffer); // subst user dir
  1167. if (AnsiSysDir.Length == 3) {
  1168. strcat(ptemp, "\\");
  1169. }
  1170. strcat(ptemp, pch + AnsiSysDir.Length); // append rest of line
  1171. RtlFreeHeap( RtlProcessHeap(), 0, pStr );
  1172. StringSize = strlen(ptemp);
  1173. pStr = ptemp;
  1174. }
  1175. }
  1176. }
  1177. /*
  1178. * Write out the translated string
  1179. */
  1180. Status = TermsrvPutString(
  1181. DestHandle,
  1182. pStr,
  1183. StringSize
  1184. );
  1185. RtlFreeHeap( RtlProcessHeap(), 0, pStr );
  1186. if( !NT_SUCCESS(Status) ) {
  1187. DBGPRINT(("TermsrvIniCopyLoop: Error 0x%x doing NtWriteFile\n",Status));
  1188. RtlFreeAnsiString( &AnsiUserDir );
  1189. RtlFreeAnsiString( &AnsiSysDir );
  1190. return( Status );
  1191. }
  1192. } // end while(1)
  1193. }
  1194. else
  1195. {
  1196. return STATUS_UNSUCCESSFUL;
  1197. }
  1198. }
  1199. /*****************************************************************************
  1200. *
  1201. * TermsrvGetString
  1202. *
  1203. * This function gets a "string" from an ini file and returns it to the
  1204. * caller. Since processing must be done in memory on the strings, they
  1205. * are returned NULL terminated, but this NULL is NOT included in the
  1206. * returned string size. Of course, buffer size calculations take this
  1207. * NULL into account. Strings retain any <CR><LF> characters and are
  1208. * not stripped out like the C runtime.
  1209. *
  1210. * The I/O buffer used is passed in by the caller. If the IoBufIndex is
  1211. * not 0, this is an indication that there is still data left in the buffer
  1212. * from a previous operation. This data is used before reading additional
  1213. * data from the file handle. This handles the case where string breaks
  1214. * do not occur at buffer boundries.
  1215. *
  1216. * Strings are returned in newly allocated memory on the process heap.
  1217. * The caller is reponsible for freeing them when done.
  1218. *
  1219. * ENTRY:
  1220. * Param1 (input/output)
  1221. * Comments
  1222. *
  1223. * EXIT:
  1224. * STATUS_SUCCESS - no error
  1225. *
  1226. ****************************************************************************/
  1227. NTSTATUS
  1228. TermsrvGetString(
  1229. HANDLE SrcHandle,
  1230. PCHAR *ppStringPtr,
  1231. PULONG pStringSize,
  1232. PCHAR pIOBuf,
  1233. ULONG IOBufSize,
  1234. PULONG pIOBufIndex,
  1235. PULONG pIOBufFillSize
  1236. )
  1237. {
  1238. NTSTATUS Status;
  1239. IO_STATUS_BLOCK Iosb;
  1240. BOOL SawNL = FALSE;
  1241. ULONG StrSize = 0;
  1242. ULONG StrBufSize = 512;
  1243. PCHAR pStr = NULL;
  1244. /*
  1245. * first process any left over data in the current I/O buffer
  1246. */
  1247. if( *pIOBufIndex < *pIOBufFillSize ) {
  1248. Status = TermsrvProcessBuffer(
  1249. &pStr,
  1250. &StrSize,
  1251. &StrBufSize,
  1252. &SawNL,
  1253. pIOBuf,
  1254. pIOBufIndex,
  1255. pIOBufFillSize
  1256. );
  1257. if( Status == STATUS_SUCCESS ) {
  1258. *ppStringPtr = pStr;
  1259. *pStringSize = StrSize;
  1260. return( STATUS_SUCCESS );
  1261. }
  1262. else if (Status == STATUS_MORE_PROCESSING_REQUIRED) {
  1263. /*
  1264. * emptied the buffer
  1265. */
  1266. *pIOBufIndex = 0;
  1267. *pIOBufFillSize = 0;
  1268. // fall through to read more data
  1269. }
  1270. else {
  1271. // Error
  1272. if( pStr ) {
  1273. RtlFreeHeap( RtlProcessHeap(), 0, pStr );
  1274. }
  1275. *ppStringPtr = NULL;
  1276. *pStringSize = 0;
  1277. return( Status );
  1278. }
  1279. }
  1280. while( 1 ) {
  1281. ASSERT( *pIOBufIndex == 0 );
  1282. ASSERT( *pIOBufFillSize == 0 );
  1283. Iosb.Status = STATUS_SUCCESS;
  1284. Status = NtReadFile(
  1285. SrcHandle,
  1286. NULL, // Event
  1287. NULL, // APC routine
  1288. NULL, // APC context
  1289. &Iosb,
  1290. pIOBuf,
  1291. IOBufSize,
  1292. NULL, // ByteOffset (not used since in synchronous I/O)
  1293. NULL // Key
  1294. );
  1295. if( Status == STATUS_PENDING ) {
  1296. Status = NtWaitForSingleObject( SrcHandle, FALSE, NULL );
  1297. }
  1298. if( NT_SUCCESS(Status) ) {
  1299. // Get final I/O status
  1300. Status = Iosb.Status;
  1301. }
  1302. if( !NT_SUCCESS(Status) ) {
  1303. if( (Status == STATUS_END_OF_FILE) && (StrSize != 0) ) {
  1304. // Force the string finished
  1305. pStr[StrSize] = (CHAR)NULL;
  1306. *pStringSize = StrSize;
  1307. *ppStringPtr = pStr;
  1308. return( STATUS_SUCCESS );
  1309. }
  1310. // Free the buffer
  1311. if( pStr ) {
  1312. RtlFreeHeap( RtlProcessHeap(), 0, pStr );
  1313. }
  1314. *ppStringPtr = NULL;
  1315. *pStringSize = 0;
  1316. if (Status != STATUS_END_OF_FILE)
  1317. DBGPRINT(("TermsrvIniCopyLoop: Error 0x%x doing NtReadFile\n",Status));
  1318. return( Status );
  1319. }
  1320. // Fill in the count
  1321. *pIOBufFillSize = (ULONG)Iosb.Information;
  1322. /*
  1323. * Now process this buffer of data
  1324. */
  1325. Status = TermsrvProcessBuffer(
  1326. &pStr,
  1327. &StrSize,
  1328. &StrBufSize,
  1329. &SawNL,
  1330. pIOBuf,
  1331. pIOBufIndex,
  1332. pIOBufFillSize
  1333. );
  1334. if( Status == STATUS_SUCCESS ) {
  1335. *ppStringPtr = pStr;
  1336. *pStringSize = StrSize;
  1337. return( STATUS_SUCCESS );
  1338. }
  1339. else if (Status == STATUS_MORE_PROCESSING_REQUIRED) {
  1340. /*
  1341. * emptied the buffer
  1342. */
  1343. *pIOBufIndex = 0;
  1344. *pIOBufFillSize = 0;
  1345. // fall through to read more data
  1346. }
  1347. else {
  1348. // Error
  1349. if( pStr ) {
  1350. RtlFreeHeap( RtlProcessHeap(), 0, pStr );
  1351. }
  1352. *ppStringPtr = NULL;
  1353. *pStringSize = 0;
  1354. return( Status );
  1355. }
  1356. } // end while(1)
  1357. }
  1358. /*****************************************************************************
  1359. *
  1360. * TermsrvProcessBuffer
  1361. *
  1362. * Process a buffer of data.
  1363. *
  1364. * This uses state passed in by the caller since the string can be
  1365. * partially built, and the buffer may not be fully processed when
  1366. * a string completes.
  1367. *
  1368. * Can return if it completes a string with data still in buffer.
  1369. *
  1370. * ENTRY:
  1371. * Param1 (input/output)
  1372. * Comments
  1373. *
  1374. * EXIT:
  1375. * STATUS_SUCCESS - no error
  1376. *
  1377. ****************************************************************************/
  1378. NTSTATUS
  1379. TermsrvProcessBuffer(
  1380. PCHAR *ppStr,
  1381. PULONG pStrSize,
  1382. PULONG pStrBufSize,
  1383. PBOOL pSawNL,
  1384. PCHAR pIOBuf,
  1385. PULONG pIOBufIndex,
  1386. PULONG pIOBufFillSize
  1387. )
  1388. {
  1389. PCHAR pStr;
  1390. ULONG Index;
  1391. BOOL SawNL;
  1392. /*
  1393. * See if we are starting a new string
  1394. */
  1395. if( *ppStr == NULL ) {
  1396. pStr = RtlAllocateHeap( RtlProcessHeap(), 0, *pStrBufSize );
  1397. if( pStr == NULL ) {
  1398. DBGPRINT(("TermsrvProcessBuf: Memory allocation failure\n"));
  1399. return( STATUS_NO_MEMORY );
  1400. }
  1401. // Set it to our caller
  1402. *ppStr = pStr;
  1403. }
  1404. /*
  1405. * Get passed in state to local variables
  1406. */
  1407. pStr = *ppStr;
  1408. Index = *pStrSize;
  1409. SawNL = *pSawNL;
  1410. while ( *pIOBufIndex < *pIOBufFillSize ) {
  1411. pStr[Index] = pIOBuf[*pIOBufIndex];
  1412. if( IS_NEWLINE_CHAR( pStr[Index] ) ) {
  1413. /*
  1414. * Mark the we saw an end of string character.
  1415. * We will keep putting them into the buffer until a
  1416. * non-NL character is encountered. This handles the
  1417. * variations for <CR><LF>, <CR> alone, or <CR><LF><CR>
  1418. * if its been mangled by a buggy editor.
  1419. */
  1420. SawNL = TRUE;
  1421. }
  1422. else {
  1423. /*
  1424. * If we saw a previous NL character, and this character
  1425. * is not one, we do not take this one, but put a NULL in
  1426. * its place and return. NOTE: Do not bump the count, since
  1427. * the count does not include the NULL.
  1428. */
  1429. if( SawNL ) {
  1430. pStr[Index] = (CHAR)NULL;
  1431. *pStrSize = Index;
  1432. return( STATUS_SUCCESS );
  1433. }
  1434. }
  1435. Index++;
  1436. (*pIOBufIndex)++;
  1437. if( Index >= *pStrBufSize ) {
  1438. // Grow the string buffer
  1439. if( !TermsrvReallocateBuf( &pStr, pStrBufSize, (*pStrBufSize) * 2 ) ) {
  1440. if( pStr ) {
  1441. RtlFreeHeap( RtlProcessHeap(), 0, pStr );
  1442. }
  1443. *ppStr = NULL;
  1444. DBGPRINT(("TermsrvIniCopyLoop: Memory re-allocation failure\n"));
  1445. return( STATUS_NO_MEMORY );
  1446. }
  1447. // Memory buffer has been re-allocated
  1448. *ppStr = pStr;
  1449. *pStrBufSize = (*pStrBufSize) * 2;
  1450. }
  1451. }
  1452. *pStrSize = Index;
  1453. *pSawNL = SawNL;
  1454. /*
  1455. * emptied the buffer without building a whole string
  1456. */
  1457. return( STATUS_MORE_PROCESSING_REQUIRED );
  1458. }
  1459. /*****************************************************************************
  1460. *
  1461. * TermsrvReallocateBuf
  1462. *
  1463. * Grow the buffer, copy data to new buffer.
  1464. *
  1465. * ENTRY:
  1466. * Param1 (input/output)
  1467. * Comments
  1468. *
  1469. * EXIT:
  1470. * STATUS_SUCCESS - no error
  1471. *
  1472. ****************************************************************************/
  1473. BOOL
  1474. TermsrvReallocateBuf(
  1475. PCHAR *ppStr,
  1476. PULONG pStrBufSize,
  1477. ULONG NewSize
  1478. )
  1479. {
  1480. PCHAR ptr;
  1481. ULONG CopyCount;
  1482. CopyCount = *pStrBufSize;
  1483. ptr = RtlAllocateHeap( RtlProcessHeap(), 0, NewSize );
  1484. if( ptr == NULL ) {
  1485. return( FALSE );
  1486. }
  1487. RtlMoveMemory( ptr, *ppStr, CopyCount );
  1488. RtlFreeHeap( RtlProcessHeap(), 0, *ppStr );
  1489. *ppStr = ptr;
  1490. return( TRUE );
  1491. }
  1492. /*****************************************************************************
  1493. *
  1494. * TermsrvPutString
  1495. *
  1496. * Write out the current string to the destination file handle
  1497. *
  1498. * ENTRY:
  1499. * Param1 (input/output)
  1500. * Comments
  1501. *
  1502. * EXIT:
  1503. * STATUS_SUCCESS - no error
  1504. *
  1505. ****************************************************************************/
  1506. NTSTATUS
  1507. TermsrvPutString(
  1508. HANDLE DestHandle,
  1509. PCHAR pStr,
  1510. ULONG StringSize
  1511. )
  1512. {
  1513. NTSTATUS Status;
  1514. IO_STATUS_BLOCK Iosb;
  1515. Iosb.Status = STATUS_SUCCESS;
  1516. Status = NtWriteFile(
  1517. DestHandle,
  1518. NULL, // Event
  1519. NULL, // APC routine
  1520. NULL, // APC context
  1521. &Iosb,
  1522. pStr,
  1523. StringSize,
  1524. NULL, // ByteOffset (not used since in synchronous I/O)
  1525. NULL // Key
  1526. );
  1527. if( Status == STATUS_PENDING ) {
  1528. Status = NtWaitForSingleObject( DestHandle, FALSE, NULL );
  1529. }
  1530. if( NT_SUCCESS(Status) ) {
  1531. // Get final I/O status
  1532. Status = Iosb.Status;
  1533. }
  1534. return( Status );
  1535. }
  1536. /*****************************************************************************
  1537. *
  1538. * TermsrvCheckNewIniFiles
  1539. *
  1540. * This routine will check the timestamps of the .ini files installed by the
  1541. * system administrator, and see if any of the user's .ini file are out of
  1542. * date, and if so, they will be renamed.
  1543. *
  1544. * ENTRY:
  1545. *
  1546. * EXIT:
  1547. * No return value.
  1548. *
  1549. ****************************************************************************/
  1550. void TermsrvCheckNewIniFiles(void)
  1551. {
  1552. NTSTATUS Status;
  1553. #if defined (_IA64_)
  1554. Status = TermsrvCheckNewIniFilesInternal(REG_NTAPI_SOFTWARE_WOW6432_TSERVER);
  1555. if (!NT_SUCCESS(Status)) {
  1556. return;
  1557. }
  1558. #endif //_IA64_
  1559. Status = TermsrvCheckNewIniFilesInternal(REG_NTAPI_SOFTWARE_TSERVER);
  1560. if (!NT_SUCCESS(Status)) {
  1561. return;
  1562. }
  1563. // Update the user's sync time in the registry
  1564. TermsrvSetUserSyncTime();
  1565. }
  1566. /*****************************************************************************
  1567. *
  1568. * TermsrvCheckNewIniFilesInternal
  1569. *
  1570. * This routine will check the timestamps of the .ini files installed by the
  1571. * system administrator, and see if any of the user's .ini file are out of
  1572. * date, and if so, they will be renamed.
  1573. *
  1574. * ENTRY:
  1575. * LPCWSTR wszBaseKeyName
  1576. *
  1577. * EXIT:
  1578. * No return value.
  1579. *
  1580. ****************************************************************************/
  1581. NTSTATUS
  1582. TermsrvCheckNewIniFilesInternal(
  1583. IN LPCWSTR wszBaseKeyName)
  1584. {
  1585. PWCHAR pwch;
  1586. UNICODE_STRING UniString, UniUserDir, UniNTDir = {0,0,NULL};
  1587. OBJECT_ATTRIBUTES ObjectAttr;
  1588. FILE_NETWORK_OPEN_INFORMATION BasicInfo;
  1589. HANDLE hKey = NULL, hWinDir = NULL;
  1590. NTSTATUS Status;
  1591. ULONG ulcnt, ullen, ultmp;
  1592. WCHAR wcWinDir[MAX_PATH], wcbuff[MAX_PATH];
  1593. PKEY_VALUE_FULL_INFORMATION pKeyValInfo;
  1594. PKEY_BASIC_INFORMATION pKeyInfo;
  1595. IO_STATUS_BLOCK IOStatus;
  1596. g_debugIniMap = IsDebugIniMapEnabled();
  1597. // Allocate a buffer for the key value name and time info
  1598. ullen = sizeof(KEY_VALUE_FULL_INFORMATION) + MAX_PATH*sizeof(WCHAR) +
  1599. sizeof(ULONG);
  1600. pKeyValInfo = RtlAllocateHeap(RtlProcessHeap(),
  1601. 0,
  1602. ullen);
  1603. // If we didn't get the buffer, return
  1604. if (!pKeyValInfo) {
  1605. return STATUS_NO_MEMORY;
  1606. }
  1607. // Open up the registry key to get the last sync time for this user
  1608. wcscpy(wcbuff,wszBaseKeyName);
  1609. wcscat(wcbuff,TERMSRV_INIFILE_TIMES_SHORT);
  1610. RtlInitUnicodeString(&UniString,
  1611. wcbuff);
  1612. InitializeObjectAttributes(&ObjectAttr,
  1613. &UniString,
  1614. OBJ_CASE_INSENSITIVE,
  1615. NULL,
  1616. NULL);
  1617. Status = NtOpenKey(&hKey, KEY_READ, &ObjectAttr);
  1618. // If we successfully opened the key, check if there are any new entries
  1619. if (NT_SUCCESS(Status)) {
  1620. // Since we already allocated a hunk of memory, use the value buffer
  1621. // for the key info query
  1622. pKeyInfo = (PKEY_BASIC_INFORMATION)pKeyValInfo;
  1623. // Get the last time anyone wrote to "IniFile Times" key
  1624. Status = NtQueryKey(hKey,
  1625. KeyBasicInformation,
  1626. pKeyInfo,
  1627. ullen,
  1628. &ultmp);
  1629. // We got the last write time OK, now get the last time we sync'd
  1630. if (NT_SUCCESS(Status) && TermsrvGetUserSyncTime(&ultmp)) {
  1631. // Convert the time value to seconds since 1970
  1632. RtlTimeToSecondsSince1970 (&pKeyInfo->LastWriteTime,
  1633. &ulcnt);
  1634. // If no .ini files or reg entries have been updated since the last
  1635. // time we sync'd this user, just return
  1636. if (ultmp >= ulcnt) {
  1637. NtClose(hKey);
  1638. RtlFreeHeap(RtlProcessHeap(), 0, pKeyValInfo);
  1639. return STATUS_SUCCESS;
  1640. }
  1641. }
  1642. TermsrvCheckNewRegEntries(wszBaseKeyName);
  1643. // Set up UniUserDir to point at wcbuff
  1644. UniUserDir.Buffer = wcWinDir;
  1645. UniUserDir.Length = 0;
  1646. UniUserDir.MaximumLength = sizeof(wcbuff);
  1647. Status = GetPerUserWindowsDirectory(&UniUserDir);
  1648. if (NT_SUCCESS(Status)) {
  1649. // Convert to an NT path
  1650. if (RtlDosPathNameToNtPathName_U(UniUserDir.Buffer,
  1651. &UniNTDir,
  1652. NULL,
  1653. NULL)) {
  1654. InitializeObjectAttributes(&ObjectAttr,
  1655. &UniNTDir,
  1656. OBJ_CASE_INSENSITIVE,
  1657. NULL,
  1658. NULL);
  1659. // Open the user's windows directory
  1660. IOStatus.Status = STATUS_SUCCESS;
  1661. Status = NtOpenFile(&hWinDir,
  1662. FILE_GENERIC_READ,
  1663. &ObjectAttr,
  1664. &IOStatus,
  1665. FILE_SHARE_READ|FILE_SHARE_WRITE,
  1666. FILE_SYNCHRONOUS_IO_NONALERT);
  1667. } else {
  1668. Status = STATUS_NO_SUCH_FILE;
  1669. }
  1670. }
  1671. // Go through each of the keys, checking if it's newer than the user's
  1672. // version of the file, and if so rename it
  1673. ulcnt = 0;
  1674. wcscat(wcWinDir, L"\\");
  1675. UniUserDir.Length += 2; // add in length of \ seperator
  1676. while (NT_SUCCESS(Status)) {
  1677. Status = NtEnumerateValueKey(hKey,
  1678. ulcnt++,
  1679. KeyValueFullInformation,
  1680. pKeyValInfo,
  1681. ullen,
  1682. &ultmp);
  1683. if (NT_SUCCESS(Status)) {
  1684. RtlMoveMemory(wcbuff, pKeyValInfo->Name, pKeyValInfo->NameLength);
  1685. wcbuff[pKeyValInfo->NameLength/sizeof(WCHAR)] = L'\0';
  1686. // Get rid of the .ini extension
  1687. if (pwch = wcschr(wcbuff, L'.')) {
  1688. *pwch = L'\0';
  1689. }
  1690. // Get the compatibility flags for this .ini file
  1691. GetTermsrCompatFlags(wcbuff,
  1692. &ultmp,
  1693. CompatibilityIniFile);
  1694. // If we removed the extension, put it back
  1695. if (pwch) {
  1696. *pwch = '.';
  1697. }
  1698. // If the INISYNC compat bit is set, don't rename the file
  1699. if ((ultmp & (TERMSRV_COMPAT_INISYNC | TERMSRV_COMPAT_WIN16)) !=
  1700. (TERMSRV_COMPAT_INISYNC | TERMSRV_COMPAT_WIN16)) {
  1701. RtlInitUnicodeString(&UniString, wcbuff);
  1702. // Query the last write time of the .ini file
  1703. InitializeObjectAttributes(&ObjectAttr,
  1704. &UniString,
  1705. OBJ_CASE_INSENSITIVE,
  1706. hWinDir,
  1707. NULL);
  1708. // Get the last write time
  1709. if (NT_SUCCESS(NtQueryFullAttributesFile( &ObjectAttr,
  1710. &BasicInfo ))) {
  1711. // Convert the last write time to seconds
  1712. RtlTimeToSecondsSince1970(&BasicInfo.LastWriteTime,
  1713. &ultmp);
  1714. // Check if the system version is newer than the user's
  1715. // version
  1716. if (*(PULONG)((PCHAR)pKeyValInfo +
  1717. pKeyValInfo->DataOffset) > ultmp) {
  1718. // Concatenate the .ini name onto the user's path
  1719. wcscpy(wcWinDir + (UniUserDir.Length/sizeof(WCHAR)),
  1720. wcbuff);
  1721. // Create the target name to rename the file
  1722. // (inifile.ctx)
  1723. wcscpy(wcbuff, wcWinDir);
  1724. pwch = wcsrchr(wcbuff, L'.');
  1725. if (pwch) {
  1726. wcscpy(pwch, L".ctx");
  1727. } else {
  1728. wcscat(pwch, L".ctx");
  1729. }
  1730. // Rename the .ini file
  1731. MoveFileExW(wcWinDir,
  1732. wcbuff,
  1733. MOVEFILE_REPLACE_EXISTING);
  1734. }
  1735. }
  1736. }
  1737. }
  1738. }
  1739. // Close the handles, if they were opened
  1740. if (hKey) {
  1741. NtClose(hKey);
  1742. }
  1743. if (hWinDir) {
  1744. NtClose(hWinDir);
  1745. }
  1746. }
  1747. // Free the memory we allocated
  1748. RtlFreeHeap( RtlProcessHeap(), 0, pKeyValInfo );
  1749. if (UniNTDir.Buffer) {
  1750. RtlFreeHeap(RtlProcessHeap(), 0, UniNTDir.Buffer);
  1751. }
  1752. return STATUS_SUCCESS;
  1753. }
  1754. /*****************************************************************************
  1755. *
  1756. * TermsrvCheckKeys
  1757. *
  1758. * This recursive routine will check for any subkeys under the system root
  1759. * key passed in. It will delete the corresponding key in the user's
  1760. * software registry if the user's key is older than the system key. If the
  1761. * INISYNC bit is set for this registry key or the key is still has subkeys,
  1762. * it won't be deleted. If the key doesn't exist in the user's registry,
  1763. * it will be added.
  1764. *
  1765. * ENTRY:
  1766. * HANDLE hKeySysRoot: handle to key in system section of registry
  1767. * HANDLE hKeyUsrRoot: handle to key in user section of registry
  1768. * PKEY_BASIC_INFORMATION pKeySysInfo: Ptr to buffer for key basic info struc
  1769. * PKEY_FULL_INFORMATION pKeyUsrInfo: Ptr to buffer for full key info struc
  1770. * ULONG ulcsys: Size of SysInfo buffer
  1771. * ULONG ulcusr: Size of UsrInfo buffer
  1772. *
  1773. * EXIT:
  1774. * SUCCESS:
  1775. * STATUS_SUCCESS
  1776. * FAILURE:
  1777. * NTSTATUS Return Code
  1778. *
  1779. ****************************************************************************/
  1780. NTSTATUS TermsrvCheckKeys(HANDLE hKeySysRoot,
  1781. HANDLE hKeyUsrRoot,
  1782. PKEY_BASIC_INFORMATION pKeySysInfo,
  1783. PKEY_FULL_INFORMATION pKeyUsrInfo,
  1784. ULONG ulcsys,
  1785. ULONG ulcusr,
  1786. DWORD indentLevel )
  1787. {
  1788. NTSTATUS Status = STATUS_SUCCESS, Status2;
  1789. ULONG ultemp, ulcnt = 0;
  1790. UNICODE_STRING UniPath, UniString;
  1791. OBJECT_ATTRIBUTES ObjAttr;
  1792. HANDLE hKeyUsr = NULL, hKeySys = NULL;
  1793. LPWSTR wcbuff = NULL;
  1794. ULONG aulbuf[4];
  1795. PKEY_FULL_INFORMATION pKeyUsrFullInfoSaved = NULL ;
  1796. ULONG sizeFullInfo;
  1797. PKEY_VALUE_PARTIAL_INFORMATION pValKeyInfo =
  1798. (PKEY_VALUE_PARTIAL_INFORMATION)aulbuf;
  1799. ++indentLevel;
  1800. Status = GetFullKeyPath(hKeyUsrRoot,NULL,&wcbuff);
  1801. if(!NT_SUCCESS(Status)) {
  1802. return Status;
  1803. }
  1804. // Get the compatibility flags for this entry
  1805. GetTermsrCompatFlags(wcbuff,
  1806. &ultemp,
  1807. CompatibilityRegEntry);
  1808. LocalFree(wcbuff);
  1809. // If the INISYNC or NOREGMAP bits are set for this entry,
  1810. // return, since there's nothing to do
  1811. if ((ultemp & TERMSRV_COMPAT_WIN32) &&
  1812. (ultemp & (TERMSRV_COMPAT_NOREGMAP | TERMSRV_COMPAT_INISYNC))) {
  1813. return(STATUS_NO_MORE_ENTRIES);
  1814. }
  1815. // Save the current info for the current user key
  1816. // @@@
  1817. if (!hKeyUsrRoot)
  1818. {
  1819. DBGPRINT(("ERROR : LINE : %4d, why is this null? \n", __LINE__ ));
  1820. return(STATUS_NO_MORE_ENTRIES );
  1821. }
  1822. // use a zero length query to get the actual length
  1823. Status = NtQueryKey(hKeyUsrRoot,
  1824. KeyFullInformation,
  1825. pKeyUsrFullInfoSaved,
  1826. 0,
  1827. &ultemp) ;
  1828. if ( !NT_SUCCESS( Status ) )
  1829. {
  1830. if (Status == STATUS_BUFFER_TOO_SMALL )
  1831. {
  1832. sizeFullInfo = ultemp;
  1833. pKeyUsrFullInfoSaved = RtlAllocateHeap(RtlProcessHeap(), 0, sizeFullInfo );
  1834. if ( ! pKeyUsrFullInfoSaved )
  1835. {
  1836. return STATUS_NO_MEMORY;
  1837. }
  1838. Status = NtQueryKey(hKeyUsrRoot,
  1839. KeyFullInformation,
  1840. pKeyUsrFullInfoSaved,
  1841. sizeFullInfo,
  1842. &ultemp);
  1843. if( !NT_SUCCESS(Status ) )
  1844. {
  1845. DBGPRINT(("ERROR : LINE : %4d, Status =0x%lx , ultemp=%d\n", __LINE__ , Status, ultemp));
  1846. RtlFreeHeap( RtlProcessHeap(), 0, pKeyUsrFullInfoSaved);
  1847. return( Status );
  1848. }
  1849. }
  1850. else
  1851. {
  1852. DBGPRINT(("ERROR : LINE : %4d, Status =0x%lx \n", __LINE__ , Status ));
  1853. return Status;
  1854. }
  1855. }
  1856. // Go through each of the subkeys, checking for user keys that are older
  1857. // than the system version of the keys
  1858. while (NT_SUCCESS(Status)) {
  1859. Status = NtEnumerateKey(hKeySysRoot,
  1860. ulcnt++,
  1861. KeyBasicInformation,
  1862. pKeySysInfo,
  1863. ulcsys,
  1864. &ultemp);
  1865. // See if there are any user keys under this key that are out of date
  1866. if (NT_SUCCESS(Status)) {
  1867. // Null terminate the key name
  1868. pKeySysInfo->Name[pKeySysInfo->NameLength/sizeof(WCHAR)] = L'\0';
  1869. // Create a unicode string for the key name
  1870. RtlInitUnicodeString(&UniPath, pKeySysInfo->Name);
  1871. InitializeObjectAttributes(&ObjAttr,
  1872. &UniPath,
  1873. OBJ_CASE_INSENSITIVE,
  1874. hKeySysRoot,
  1875. NULL);
  1876. Debug1( indentLevel, __LINE__, L"system", &UniPath );
  1877. // Open up the system key
  1878. Status2 = NtOpenKey(&hKeySys,
  1879. KEY_READ,
  1880. &ObjAttr);
  1881. // We opened up the system key, now open the user key
  1882. if (NT_SUCCESS(Status2)) {
  1883. // Setup the object attr struc for the user key
  1884. InitializeObjectAttributes(&ObjAttr,
  1885. &UniPath,
  1886. OBJ_CASE_INSENSITIVE,
  1887. hKeyUsrRoot,
  1888. NULL);
  1889. // Open up the user key
  1890. Status2 = NtOpenKey(&hKeyUsr,
  1891. MAXIMUM_ALLOWED,
  1892. &ObjAttr);
  1893. // Check if there are any subkeys under this key
  1894. if (NT_SUCCESS(Status2)) {
  1895. Debug1(indentLevel, __LINE__, L"user", &UniPath );
  1896. TermsrvCheckKeys(hKeySys,
  1897. hKeyUsr,
  1898. pKeySysInfo,
  1899. pKeyUsrInfo,
  1900. ulcsys,
  1901. ulcusr,
  1902. indentLevel);
  1903. NtClose(hKeyUsr);
  1904. }
  1905. // key doesn't exist, clone system key to user
  1906. else {
  1907. Status2 = GetFullKeyPath(hKeyUsrRoot,pKeySysInfo->Name,&wcbuff);
  1908. if(NT_SUCCESS(Status2)) {
  1909. // don't clone if mapping off for this registry entry
  1910. GetTermsrCompatFlags(wcbuff,
  1911. &ultemp,
  1912. CompatibilityRegEntry);
  1913. LocalFree(wcbuff);
  1914. if (((ultemp & (TERMSRV_COMPAT_WIN32 | TERMSRV_COMPAT_NOREGMAP)) !=
  1915. (TERMSRV_COMPAT_WIN32 | TERMSRV_COMPAT_NOREGMAP) ))
  1916. {
  1917. Status2 = NtQueryKey(hKeySys,
  1918. KeyFullInformation,
  1919. pKeyUsrInfo,
  1920. ulcusr,
  1921. &ultemp);
  1922. if (NT_SUCCESS(Status2)) {
  1923. // don't clone if key previously deleted
  1924. RtlInitUnicodeString(&UniString, TERMSRV_COPYONCEFLAG);
  1925. Status2 = NtQueryValueKey(hKeySys,
  1926. &UniString,
  1927. KeyValuePartialInformation,
  1928. pValKeyInfo,
  1929. sizeof(aulbuf),
  1930. &ultemp);
  1931. if (!(NT_SUCCESS(Status2) && (pValKeyInfo->Data))) {
  1932. // Setup the unicode string for the class
  1933. RtlInitUnicodeString(&UniString,
  1934. pKeyUsrInfo->ClassLength ?
  1935. pKeyUsrInfo->Class : NULL);
  1936. Debug1(indentLevel, __LINE__, L"creating user key", ObjAttr.ObjectName );
  1937. Status2 = NtCreateKey(&hKeyUsr,
  1938. MAXIMUM_ALLOWED,
  1939. &ObjAttr,
  1940. 0,
  1941. &UniString,
  1942. REG_OPTION_NON_VOLATILE,
  1943. &ultemp);
  1944. if (NT_SUCCESS(Status2)) {
  1945. Debug1(indentLevel, __LINE__, L"cloning key", ObjAttr.ObjectName );
  1946. TermsrvCloneKey(hKeySys,
  1947. hKeyUsr,
  1948. pKeyUsrInfo,
  1949. TRUE);
  1950. }
  1951. }
  1952. }
  1953. }
  1954. }
  1955. }
  1956. NtClose(hKeySys);
  1957. }
  1958. }
  1959. }
  1960. // Get the info for the user key
  1961. if (NtQueryKey(hKeyUsrRoot,
  1962. KeyFullInformation,
  1963. pKeyUsrInfo,
  1964. ulcusr,
  1965. &ultemp) == STATUS_SUCCESS) {
  1966. // Now get the info for the system key (again)
  1967. if (NtQueryKey(hKeySysRoot,
  1968. KeyBasicInformation,
  1969. pKeySysInfo,
  1970. ulcsys,
  1971. &ultemp) == STATUS_SUCCESS) {
  1972. // Get the compatibility flags for this registry entry
  1973. pKeySysInfo->Name[pKeySysInfo->NameLength/sizeof(WCHAR)] = L'\0';
  1974. GetTermsrCompatFlags(pKeySysInfo->Name,
  1975. &ultemp,
  1976. CompatibilityRegEntry);
  1977. //check if it's older than the system version
  1978. if( pKeyUsrFullInfoSaved->LastWriteTime.QuadPart <
  1979. pKeySysInfo->LastWriteTime.QuadPart)
  1980. {
  1981. DebugTime(indentLevel, __LINE__, L"User key time", pKeyUsrFullInfoSaved->LastWriteTime );
  1982. DebugTime(indentLevel, __LINE__, L"Sys key time", pKeySysInfo->LastWriteTime);
  1983. Debug2( indentLevel, __LINE__, L"Key Old, values being cloned", pKeySysInfo->Name, pKeySysInfo->NameLength );
  1984. if(NtQueryKey(hKeySysRoot,
  1985. KeyFullInformation,
  1986. pKeyUsrInfo,
  1987. ulcusr,
  1988. &ultemp) == STATUS_SUCCESS) {
  1989. TermsrvCloneKey(hKeySysRoot,
  1990. hKeyUsrRoot,
  1991. pKeyUsrInfo,//actually it is system key information
  1992. FALSE);
  1993. }
  1994. }
  1995. }
  1996. }
  1997. RtlFreeHeap( RtlProcessHeap(), 0, pKeyUsrFullInfoSaved);
  1998. return(Status);
  1999. }
  2000. /*****************************************************************************
  2001. *
  2002. * TermsrvCheckNewRegEntries
  2003. *
  2004. * This routine will check the user's registry keys, and see if any of them
  2005. * are older than the system versions. If so, the old key will be removed.
  2006. *
  2007. * ENTRY:
  2008. * IN LPCWSTR wszBaseKeyName
  2009. *
  2010. * EXIT:
  2011. * No return value.
  2012. *
  2013. ****************************************************************************/
  2014. void
  2015. TermsrvCheckNewRegEntries(
  2016. IN LPCWSTR wszBaseKeyName)
  2017. {
  2018. NTSTATUS Status;
  2019. ULONG ulcsys, ulcusr;
  2020. UNICODE_STRING UniPath;
  2021. OBJECT_ATTRIBUTES ObjAttr;
  2022. PKEY_BASIC_INFORMATION pKeySysInfo = NULL;
  2023. PKEY_FULL_INFORMATION pKeyUserInfo = NULL;
  2024. HANDLE hKeyUser, hKeySys = NULL;
  2025. WCHAR wcuser[MAX_PATH], wcsys[MAX_PATH];
  2026. DWORD indentLevel = 0;
  2027. // Oct 15, 1999
  2028. // This is BAD ! The Status bit was not initiazted to zero, which causes intermitent
  2029. // execution by this function. The problem is that even if the status bit is init'd
  2030. // to zero, then we can get the wrong behavior which will cause Office97 installation to
  2031. // go wrong.
  2032. // See BUG ID 412419
  2033. // Here is what woudl happen: Install any app ( say TsClient). This would cause
  2034. // an update to a Key in HKDU called Explorer\ShellFolders, just a refresh (no real change).
  2035. // Then, if Admin did an logout and login, the call from UserInit.EXE into this
  2036. // function (assuming by chance Status=0 was on the stack) would cause deletion of
  2037. // that key (ShellFolder). But once Explorer starts, it writes to the ShellFolder with
  2038. // a subset of original 19 values.
  2039. // The problem is that if then, you decide to install Office97, setup.exe would look
  2040. // in the same key for a value called "template" which is now missing. Explorer
  2041. // would then create it, but it would point to some other than default location.
  2042. // When all was done, our office97 compat script would be looking to where the
  2043. // "template" value used to point (the default location), not where it is pointing now.
  2044. //
  2045. // we decide to disable this func by calling return right here, and instead, rely
  2046. // on the TS mechanism to fault in keys.
  2047. // Oct 31, 1999
  2048. // I have decided to initialize the status var and let this func run, in addition to
  2049. // marking the Explorer\ShellFolders as a do-not propagate key tree.
  2050. //
  2051. // It was discovered that after installing Office2000, when a user clicks on the
  2052. // start-menu-> Open Office Docs link, MSI starts to run since user's hive is missing some
  2053. // keys.
  2054. //
  2055. // The reason MSI does not see the keys in HKCU is because MSI has the TS-aware bit set,
  2056. // which means that there are no faulting-in for any keys. The same is true for the Explorer.
  2057. // On the other hand, when an app such as Office runs, since it is not ts-aware, we
  2058. // fault in the keys that office touches. I verified this to work as expected.
  2059. // The problem is that when you click on "Open Office Documents", you do so from the
  2060. // explorer and when Explorer opens keys in the registry, since explorer is TS-aware,
  2061. // those keys are not faulted in. I have verified that if you mark explorer as a non-TS-aware
  2062. // app, the problem goes away.
  2063. // We made a recent change in TS code (B-bug 412419, bld 2156+) to fix a different
  2064. // problem which has now uncovered the reliance of the Explorer to get keys
  2065. // faulted in during login.
  2066. // Specifically, TS used to fault in all keys at login time,
  2067. // regardless of the need. Post 2156, TS faults in only keys upon
  2068. // access by non-ts-aware apps. This was to fix a bug based on
  2069. // what we considered to be our most informed and well tested opinion. That
  2070. // has turned out to be wrong.
  2071. // It is too risky to mark explorer non-ts-aware this late, so we must change the fix for 412419.
  2072. // This should also make Bruno Amice very happy, since the fix will be as it was
  2073. // advocated by him.
  2074. Status = STATUS_SUCCESS;
  2075. // Get a buffer for the system key info
  2076. ulcsys = sizeof(KEY_BASIC_INFORMATION) + MAX_PATH*sizeof(WCHAR);
  2077. pKeySysInfo = RtlAllocateHeap(RtlProcessHeap(),
  2078. 0,
  2079. ulcsys);
  2080. if (!pKeySysInfo) {
  2081. Status = STATUS_NO_MEMORY;
  2082. }
  2083. // Get a buffer for the user key info
  2084. if (NT_SUCCESS(Status)) {
  2085. ulcusr = sizeof(KEY_FULL_INFORMATION) + MAX_PATH*sizeof(WCHAR);
  2086. pKeyUserInfo = RtlAllocateHeap(RtlProcessHeap(),
  2087. 0,
  2088. ulcusr);
  2089. if (!pKeyUserInfo) {
  2090. Status = STATUS_NO_MEMORY;
  2091. }
  2092. }
  2093. // We have the necessary buffers, start checking the keys
  2094. if (NT_SUCCESS(Status)) {
  2095. // Build a string that points to Citrix\Install\Software
  2096. wcscpy(wcsys, wszBaseKeyName);
  2097. wcscat(wcsys, TERMSRV_INSTALL_SOFTWARE_SHORT);
  2098. // Build up a string for this user's software section
  2099. Status = RtlFormatCurrentUserKeyPath( &UniPath );
  2100. if (NT_SUCCESS(Status)) {
  2101. wcscpy(wcuser, UniPath.Buffer);
  2102. wcscat(wcuser, L"\\Software");
  2103. // Free the original user path
  2104. RtlFreeHeap( RtlProcessHeap(), 0, UniPath.Buffer );
  2105. }
  2106. if (NT_SUCCESS(Status)) {
  2107. // Create a unicode string for the system key path
  2108. RtlInitUnicodeString(&UniPath, wcsys);
  2109. InitializeObjectAttributes(&ObjAttr,
  2110. &UniPath,
  2111. OBJ_CASE_INSENSITIVE,
  2112. NULL,
  2113. NULL);
  2114. Debug1(indentLevel, __LINE__, L"system", &UniPath );
  2115. Status = NtOpenKey(&hKeySys,
  2116. KEY_READ,
  2117. &ObjAttr);
  2118. }
  2119. if (NT_SUCCESS(Status)) {
  2120. // Create a unicode string for the user key path
  2121. RtlInitUnicodeString(&UniPath, wcuser);
  2122. InitializeObjectAttributes(&ObjAttr,
  2123. &UniPath,
  2124. OBJ_CASE_INSENSITIVE,
  2125. NULL,
  2126. NULL);
  2127. Debug1(indentLevel, __LINE__, L"user", &UniPath );
  2128. Status = NtOpenKey(&hKeyUser,
  2129. KEY_READ | DELETE,
  2130. &ObjAttr);
  2131. }
  2132. // Go through each of the keys, checking if the system version is
  2133. // newer than the user version
  2134. if (NT_SUCCESS(Status)) {
  2135. TermsrvCheckKeys(hKeySys,
  2136. hKeyUser,
  2137. pKeySysInfo,
  2138. pKeyUserInfo,
  2139. ulcsys,
  2140. ulcusr,
  2141. indentLevel );
  2142. // Close the user key
  2143. NtClose(hKeyUser);
  2144. }
  2145. // If we allocated the system key, close it
  2146. if (hKeySys) {
  2147. NtClose(hKeySys);
  2148. }
  2149. }
  2150. // Free up any memory we allocated
  2151. if (pKeySysInfo) {
  2152. RtlFreeHeap( RtlProcessHeap(), 0, pKeySysInfo);
  2153. }
  2154. if (pKeyUserInfo) {
  2155. RtlFreeHeap( RtlProcessHeap(), 0, pKeyUserInfo);
  2156. }
  2157. }
  2158. /*****************************************************************************
  2159. *
  2160. * Ctxstristr
  2161. *
  2162. * This is a case insensitive version of strstr.
  2163. *
  2164. * ENTRY:
  2165. * PCHAR pstring1 (In) - String to search in
  2166. * PCHAR pstring2 (In) - String to search for
  2167. *
  2168. * EXIT:
  2169. * TRUE - User ini file should be sync'd
  2170. * FALSE - User ini file should be sync'd
  2171. *
  2172. ****************************************************************************/
  2173. PCHAR
  2174. Ctxstristr(
  2175. PCHAR pstring1,
  2176. PCHAR pstring2)
  2177. {
  2178. PCHAR pch, ps1, ps2;
  2179. pch = pstring1;
  2180. while (*pch)
  2181. {
  2182. ps1 = pch;
  2183. ps2 = pstring2;
  2184. while (*ps1 && *ps2 && !(toupper(*ps1) - toupper(*ps2))) {
  2185. ps1++;
  2186. ps2++;
  2187. }
  2188. if (!*ps2) {
  2189. return(pch);
  2190. }
  2191. pch++;
  2192. }
  2193. return(NULL);
  2194. }
  2195. /*****************************************************************************
  2196. *
  2197. * TermsrvLogInstallIniFile
  2198. *
  2199. * This routine will write the time the .ini file was last updated into the
  2200. * Terminal Server\install section of the registry.
  2201. *
  2202. * ENTRY:
  2203. *
  2204. * EXIT:
  2205. * TRUE - Success
  2206. * FALSE - Failure
  2207. *
  2208. ****************************************************************************/
  2209. BOOL TermsrvLogInstallIniFile(PUNICODE_STRING NtFileName)
  2210. {
  2211. PWCHAR pwch;
  2212. UNICODE_STRING UniString;
  2213. OBJECT_ATTRIBUTES ObjectAttr;
  2214. FILE_NETWORK_OPEN_INFORMATION BasicInfo;
  2215. HANDLE hKey;
  2216. NTSTATUS Status;
  2217. ULONG ultmp;
  2218. if (!TermsrvPerUserWinDirMapping()) {
  2219. return FALSE;
  2220. }
  2221. // Open up the registry key to store the last write time of the file
  2222. RtlInitUnicodeString(&UniString,
  2223. TERMSRV_INSTALL);
  2224. InitializeObjectAttributes(&ObjectAttr,
  2225. &UniString,
  2226. OBJ_CASE_INSENSITIVE,
  2227. NULL,
  2228. NULL);
  2229. // Open or create the Terminal Server\Install path
  2230. Status = NtCreateKey(&hKey,
  2231. KEY_WRITE,
  2232. &ObjectAttr,
  2233. 0,
  2234. NULL,
  2235. REG_OPTION_NON_VOLATILE,
  2236. &ultmp);
  2237. // Now open or create the IniFile Times key
  2238. if (NT_SUCCESS(Status)) {
  2239. NtClose(hKey);
  2240. RtlInitUnicodeString(&UniString,
  2241. TERMSRV_INIFILE_TIMES);
  2242. InitializeObjectAttributes(&ObjectAttr,
  2243. &UniString,
  2244. OBJ_CASE_INSENSITIVE,
  2245. NULL,
  2246. NULL);
  2247. Status = NtCreateKey(&hKey,
  2248. KEY_WRITE,
  2249. &ObjectAttr,
  2250. 0,
  2251. NULL,
  2252. REG_OPTION_NON_VOLATILE,
  2253. &ultmp);
  2254. }
  2255. // Opened up the registry key, now get the last write time of the file
  2256. if (NT_SUCCESS(Status)) {
  2257. // Query the last write time of the .ini file
  2258. InitializeObjectAttributes(&ObjectAttr,
  2259. NtFileName,
  2260. OBJ_CASE_INSENSITIVE,
  2261. NULL,
  2262. NULL);
  2263. Status = NtQueryFullAttributesFile( &ObjectAttr, &BasicInfo );
  2264. // Got the last write time, convert to seconds and write it out
  2265. if (NT_SUCCESS(Status)) {
  2266. // Just save the .ini filename, get rid of the path
  2267. pwch = wcsrchr(NtFileName->Buffer, L'\\') + 1;
  2268. if (!pwch) {
  2269. pwch = NtFileName->Buffer;
  2270. }
  2271. // Convert to seconds (so it fits in a DWORD)
  2272. RtlTimeToSecondsSince1970 (&BasicInfo.LastWriteTime,
  2273. &ultmp);
  2274. RtlInitUnicodeString(&UniString,
  2275. pwch);
  2276. // Write it out the .ini file name and the last write time
  2277. Status = NtSetValueKey(hKey,
  2278. &UniString,
  2279. 0,
  2280. REG_DWORD,
  2281. &ultmp,
  2282. sizeof(ultmp));
  2283. }
  2284. // Close the registry key
  2285. NtClose(hKey);
  2286. }
  2287. return(NT_SUCCESS(Status));
  2288. }
  2289. /**********************************************************************
  2290. *
  2291. * BOOL TermsrvLogInstallIniFileEx( WCHAR *pDosFileName )
  2292. *
  2293. * This wraps TermsrvLogInstallIniFile() for dos name files instead of
  2294. * NT-file-objects
  2295. *
  2296. * The file name must have the full path since func will try to get
  2297. * access time for that file.
  2298. *
  2299. * EXIT:
  2300. * TRUE - Success
  2301. * FALSE - Failure
  2302. **********************************************************************/
  2303. BOOL TermsrvLogInstallIniFileEx( WCHAR *pDosFileName )
  2304. {
  2305. UNICODE_STRING uniString;
  2306. BOOL rc= FALSE;
  2307. if ( RtlDosPathNameToNtPathName_U( pDosFileName, &uniString, 0, 0 ) )
  2308. {
  2309. if ( rc = TermsrvLogInstallIniFile( & uniString ) )
  2310. {
  2311. RtlFreeHeap( RtlProcessHeap(), 0, uniString.Buffer );
  2312. }
  2313. }
  2314. return rc;
  2315. }
  2316. /**********************************************************************
  2317. *
  2318. * GetFullKeyPath()
  2319. *
  2320. * PURPOSE:
  2321. * Creates full key path given the key handle and subkey name.
  2322. *
  2323. * PARAMETERS:
  2324. * IN HANDLE hKeyParent - key handle
  2325. * IN LPCWSTR wszKey - subkey name (may be NULL)
  2326. * OUT LPWSTR *pwszKeyPath - on return contains a full key path
  2327. * (caller must freee allocated memory with LocalFree()).
  2328. *
  2329. * EXIT: NTSTATUS
  2330. *
  2331. **********************************************************************/
  2332. NTSTATUS
  2333. GetFullKeyPath(
  2334. IN HANDLE hKeyParent,
  2335. IN LPCWSTR wszKey,
  2336. OUT LPWSTR *pwszKeyPath)
  2337. {
  2338. NTSTATUS Status = STATUS_NO_MEMORY;
  2339. PKEY_NAME_INFORMATION pNameInfo;
  2340. ULONG cbSize = 0;
  2341. *pwszKeyPath = NULL;
  2342. cbSize = sizeof(KEY_NAME_INFORMATION) + MAX_PATH*sizeof(WCHAR);
  2343. pNameInfo = (PKEY_NAME_INFORMATION) LocalAlloc(LPTR, cbSize);
  2344. if(pNameInfo)
  2345. {
  2346. Status = NtQueryKey(
  2347. hKeyParent,
  2348. KeyNameInformation,
  2349. pNameInfo,
  2350. cbSize,
  2351. &cbSize);
  2352. if(Status == STATUS_BUFFER_OVERFLOW)
  2353. {
  2354. LocalFree(pNameInfo);
  2355. pNameInfo = (PKEY_NAME_INFORMATION) LocalAlloc(LPTR, cbSize);
  2356. if(pNameInfo)
  2357. {
  2358. Status = NtQueryKey(
  2359. hKeyParent,
  2360. KeyNameInformation,
  2361. pNameInfo,
  2362. cbSize,
  2363. &cbSize);
  2364. }
  2365. else
  2366. {
  2367. return STATUS_NO_MEMORY;
  2368. }
  2369. }
  2370. if(NT_SUCCESS(Status))
  2371. {
  2372. cbSize = pNameInfo->NameLength + sizeof(WCHAR);
  2373. if(wszKey)
  2374. {
  2375. cbSize += wcslen(wszKey)*sizeof(WCHAR) + sizeof(L'\\');
  2376. }
  2377. *pwszKeyPath = (LPWSTR) LocalAlloc(LPTR, cbSize);
  2378. if(*pwszKeyPath)
  2379. {
  2380. memcpy(*pwszKeyPath,pNameInfo->Name,pNameInfo->NameLength);
  2381. (*pwszKeyPath)[pNameInfo->NameLength/sizeof(WCHAR)] = 0;
  2382. if(wszKey)
  2383. {
  2384. wcscat(*pwszKeyPath,L"\\");
  2385. wcscat(*pwszKeyPath,wszKey);
  2386. }
  2387. }
  2388. else
  2389. {
  2390. Status = STATUS_NO_MEMORY;
  2391. }
  2392. }
  2393. LocalFree(pNameInfo);
  2394. }
  2395. return Status;
  2396. }