Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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