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.

5603 lines
139 KiB

  1. /*++
  2. Copyright (c) 1991 - 1999 Microsoft Corporation
  3. Module Name:
  4. nlpcache.c
  5. Abstract:
  6. This module contains routines which implement user account caching:
  7. NlpCacheInitialize
  8. NlpCacheTerminate
  9. NlpAddCacheEntry
  10. NlpGetCacheEntry
  11. NlpDeleteCacheEntry
  12. NlpChangeCachePassword
  13. The cache contains the most recent validated logon information. There is
  14. only 1 (that's right - one) cache slot. This will probably change though
  15. Author:
  16. Richard L Firth (rfirth) 17-Dec-1991
  17. Revision History:
  18. Scott Field (sfield) 04-Jun-99
  19. Add supplemental cache data.
  20. Store all cache related data in single location.
  21. Encrypt interesting elements of cache entry using per-entry key mixed with per-machine key.
  22. MAC interesting cache elements for integrity check.
  23. Drastically reduce lock contention.
  24. Avoid NtFlushKey() for single location cache elements.
  25. Avoid persisting a new cache entry that matches an existing one.
  26. Attempt reg query with stack based buffer first.
  27. Chandana Surlu 21-Jul-96 Stolen from \\kernel\razzle3\src\security\msv1_0\nlpcache.c
  28. --*/
  29. #include <global.h>
  30. #undef EXTERN
  31. #include "msp.h"
  32. #include "nlp.h"
  33. #include "nlpcache.h"
  34. //
  35. // manifests
  36. //
  37. #if DBG
  38. #include <stdio.h>
  39. #endif
  40. //
  41. // Revision numbers
  42. //
  43. // NT 3.0 didn't explicitly store a revision number.
  44. // However, we are designating that release to be revision 0x00010000 (1.0).
  45. // NT 3.5 prior to build 622 is revision 0x00010001 (1.1).
  46. // NT 3.5 is revision 0x00010002 (1.2).
  47. // NT 4.0 SP 4 is revision 0x00010003 (1.3)
  48. // NT 5.0 build 2054+ is revision 0x00010004 (1.4)
  49. //
  50. #define NLP_CACHE_REVISION_NT_1_0 (0x00010000) // NT 3.0
  51. #define NLP_CACHE_REVISION_NT_1_0B (0x00010002) // NT 3.5
  52. #define NLP_CACHE_REVISION_NT_4_SP4 (0x00010003) // NT 4.0 SP 4 to save passwords as salted.
  53. #define NLP_CACHE_REVISION_NT_5_0 (0x00010004) // NT 5.0 to support opaque cache data and single location data storage.
  54. #define NLP_CACHE_REVISION (NLP_CACHE_REVISION_NT_5_0)
  55. //
  56. // The logon cache may be controlled via a value in the registry.
  57. // If the registry key does not exist, then this default constant defines
  58. // how many logon cache entries will be active. The max constant
  59. // places an upper limit on how many cache entries we will support.
  60. // If the user specifies more than the max value, we will use the
  61. // max value instead.
  62. //
  63. #define NLP_DEFAULT_LOGON_CACHE_COUNT (10)
  64. #define NLP_MAX_LOGON_CACHE_COUNT (50)
  65. //
  66. // length of per-machine cache encryption key.
  67. //
  68. #define NLP_CACHE_ENCRYPTION_KEY_LEN (64)
  69. //
  70. // name of LSA secret containing cache encryption key.
  71. //
  72. #define NLP_CACHE_ENCRYPTION_KEY_NAME L"NL$KM"
  73. //
  74. // macros
  75. //
  76. #define AllocateCacheEntry(n) (PLOGON_CACHE_ENTRY)I_NtLmAllocate(n)
  77. #define FreeCacheEntry(p) I_NtLmFree((PVOID)p)
  78. #define AllocateFromHeap(n) I_NtLmAllocate(n)
  79. #define FreeToHeap(p) I_NtLmFree((PVOID)p)
  80. //
  81. // guard against simultaneous access
  82. //
  83. #define READ_CACHE() RtlAcquireResourceShared(&NlpLogonCacheCritSec, TRUE)
  84. #define WRITE_CACHE() RtlAcquireResourceExclusive(&NlpLogonCacheCritSec, TRUE)
  85. #define READ_TO_WRITE_CACHE() RtlConvertSharedToExclusive(&NlpLogonCacheCritSec)
  86. #define LEAVE_CACHE() RtlReleaseResource(&NlpLogonCacheCritSec)
  87. #define INVALIDATE_HANDLE(handle) (*((PHANDLE)(&handle)) = INVALID_HANDLE_VALUE)
  88. #define IS_VALID_HANDLE(handle) (handle != INVALID_HANDLE_VALUE)
  89. ////////////////////////////////////////////////////////////////////////
  90. // //
  91. // datatypes //
  92. // //
  93. ////////////////////////////////////////////////////////////////////////
  94. typedef enum _NLP_SET_TIME_HINT {
  95. NLP_SMALL_TIME,
  96. NLP_BIG_TIME,
  97. NLP_NOW_TIME
  98. } NLP_SET_TIME_HINT, *PNLP_SET_TIME_HINT;
  99. #define BIG_PART_1 0x7fffffff // largest positive large int is 63 bits on
  100. #define BIG_PART_2 0xffffffff
  101. #define SMALL_PART_1 0x0 // smallest positive large int is 64 bits off
  102. #define SMALL_PART_2 0x0
  103. //
  104. // This structure is saved on disk and provides information
  105. // about the rest of the cache. This structure is in a value
  106. // named "NL$Control" under the cache registry key.
  107. //
  108. typedef struct _NLP_CACHE_CONTROL {
  109. //
  110. // Revision of the cache on-disk structure
  111. //
  112. ULONG Revision;
  113. //
  114. // The current on-disk size of the cache (number of entries)
  115. //
  116. ULONG Entries;
  117. } NLP_CACHE_CONTROL, *PNLP_CACHE_CONTROL;
  118. //
  119. // This data structure is a single cache table entry (CTE)
  120. // Each entry in the cache has a corresponding CTE.
  121. //
  122. typedef struct _NLP_CTE {
  123. //
  124. // CTEs are linked on either an invalid list (in any order)
  125. // or on a valid list (in ascending order of time).
  126. // This makes it easy to figure out which entry is to be
  127. // flushed when adding to the cache.
  128. //
  129. LIST_ENTRY Link;
  130. //
  131. // Time the cache entry was established.
  132. // This is used to determine which cache
  133. // entry is the oldest, and therefore will
  134. // be flushed from the cache first to make
  135. // room for new entries.
  136. //
  137. LARGE_INTEGER Time;
  138. //
  139. // This field contains the index of the CTE within the
  140. // CTE table. This index is used to generate the names
  141. // of the entrie's secret key and cache key in the registry.
  142. // This field is valid even if the entry is marked Inactive.
  143. //
  144. ULONG Index;
  145. //
  146. // Normally, we walk the active and inactive lists
  147. // to find entries. When growing or shrinking the
  148. // cache, however, it is nice to be able to walk the
  149. // table using indexes. In this case, it is nice to
  150. // have a local way of determining whether an entry
  151. // is on the active or inactive list. This field
  152. // provides that capability.
  153. //
  154. // TRUE ==> on active list
  155. // FALSE ==> not on active list
  156. //
  157. BOOLEAN Active;
  158. } NLP_CTE, *PNLP_CTE;
  159. //
  160. // This structure is used for keeping track of all information that
  161. // is stored on backing store.
  162. //
  163. typedef struct _NLP_CACHE_AND_SECRETS {
  164. PLOGON_CACHE_ENTRY CacheEntry;
  165. ULONG EntrySize;
  166. PLSAPR_CR_CIPHER_VALUE NewSecret;
  167. PLSAPR_CR_CIPHER_VALUE OldSecret;
  168. BOOLEAN Active;
  169. } NLP_CACHE_AND_SECRETS, *PNLP_CACHE_AND_SECRETS;
  170. ////////////////////////////////////////////////////////////////////////
  171. // //
  172. // Local Prototypes //
  173. // //
  174. ////////////////////////////////////////////////////////////////////////
  175. NTSTATUS
  176. NlpInternalCacheInitialize(
  177. VOID
  178. );
  179. NTSTATUS
  180. NlpOpenCache( VOID );
  181. VOID
  182. NlpCloseCache( VOID );
  183. NTSTATUS
  184. NlpGetCacheControlInfo( VOID );
  185. NTSTATUS
  186. NlpCacheKeyInitialize(
  187. VOID
  188. );
  189. NTSTATUS
  190. NlpBuildCteTable( VOID );
  191. NTSTATUS
  192. NlpChangeCacheSizeIfNecessary( VOID );
  193. NTSTATUS
  194. NlpWriteCacheControl( VOID );
  195. VOID
  196. NlpMakeCacheEntryName(
  197. IN ULONG EntryIndex,
  198. OUT PUNICODE_STRING Name
  199. );
  200. NTSTATUS
  201. NlpMakeNewCacheEntry(
  202. ULONG Index
  203. );
  204. NTSTATUS
  205. NlpEliminateCacheEntry(
  206. IN ULONG Index
  207. );
  208. NTSTATUS
  209. NlpReadCacheEntryByIndex(
  210. IN ULONG Index,
  211. OUT PLOGON_CACHE_ENTRY* CacheEntry,
  212. OUT PULONG EntrySize
  213. );
  214. VOID
  215. NlpAddEntryToActiveList(
  216. IN ULONG Index
  217. );
  218. VOID
  219. NlpAddEntryToInactiveList(
  220. IN ULONG Index
  221. );
  222. VOID
  223. NlpGetFreeEntryIndex(
  224. OUT PULONG Index
  225. );
  226. NTSTATUS
  227. NlpBuildCacheEntry(
  228. IN PNETLOGON_INTERACTIVE_INFO LogonInfo,
  229. IN PNETLOGON_VALIDATION_SAM_INFO4 AccountInfo,
  230. IN ULONG CacheFlags,
  231. OUT PLOGON_CACHE_ENTRY* ppCacheEntry,
  232. OUT PULONG pEntryLength
  233. );
  234. BOOLEAN
  235. NlpCompareCacheEntry(
  236. IN PLOGON_CACHE_ENTRY CacheEntry1,
  237. IN ULONG EntrySize1,
  238. IN PLOGON_CACHE_ENTRY CacheEntry2,
  239. IN ULONG EntrySize2
  240. );
  241. NTSTATUS
  242. NlpEncryptCacheEntry(
  243. IN PLOGON_CACHE_ENTRY CacheEntry,
  244. IN ULONG EntrySize
  245. );
  246. NTSTATUS
  247. NlpDecryptCacheEntry(
  248. IN PLOGON_CACHE_ENTRY CacheEntry,
  249. IN ULONG EntrySize
  250. );
  251. NTSTATUS
  252. NlpAddSupplementalCacheData(
  253. IN PVOID SupplementalCacheData,
  254. IN ULONG SupplementalCacheDataLength,
  255. IN OUT PLOGON_CACHE_ENTRY *ppCacheEntry,
  256. IN OUT PULONG pEntryLength
  257. );
  258. NTSTATUS
  259. NlpOpenCache( VOID );
  260. VOID
  261. NlpCloseCache( VOID );
  262. NTSTATUS
  263. NlpOpenSecret(
  264. IN ULONG Index
  265. );
  266. VOID
  267. NlpCloseSecret( VOID );
  268. NTSTATUS
  269. NlpWriteSecret(
  270. IN PLSAPR_CR_CIPHER_VALUE NewSecret,
  271. IN PLSAPR_CR_CIPHER_VALUE OldSecret
  272. );
  273. NTSTATUS
  274. NlpReadSecret(
  275. OUT PLSAPR_CR_CIPHER_VALUE * NewSecret,
  276. OUT PLSAPR_CR_CIPHER_VALUE * OldSecret
  277. );
  278. NTSTATUS
  279. NlpMakeSecretPassword(
  280. OUT PLSAPR_CR_CIPHER_VALUE Passwords,
  281. IN PUNICODE_STRING UserName,
  282. IN PNT_OWF_PASSWORD NtOwfPassword OPTIONAL,
  283. IN PLM_OWF_PASSWORD LmOwfPassword OPTIONAL
  284. );
  285. NTSTATUS
  286. NlpMakeSecretPasswordNT5(
  287. IN OUT PCACHE_PASSWORDS Passwords,
  288. IN PUNICODE_STRING UserName,
  289. IN PNT_OWF_PASSWORD NtOwfPassword OPTIONAL,
  290. IN PLM_OWF_PASSWORD LmOwfPassword OPTIONAL
  291. );
  292. BOOLEAN
  293. NlpCheckMitCacheEntry(
  294. IN PLOGON_CACHE_ENTRY CacheEntry,
  295. IN PUNICODE_STRING DomainName,
  296. IN PUNICODE_STRING UserName
  297. );
  298. NTSTATUS
  299. NlpReadCacheEntry(
  300. IN PUNICODE_STRING DomainName,
  301. IN PUNICODE_STRING UserName,
  302. OUT PULONG Index,
  303. OUT PLOGON_CACHE_ENTRY* CacheEntry,
  304. OUT PULONG EntrySize
  305. );
  306. NTSTATUS
  307. NlpWriteCacheEntry(
  308. IN ULONG Index,
  309. IN PLOGON_CACHE_ENTRY Entry,
  310. IN ULONG EntrySize
  311. );
  312. VOID
  313. NlpCopyAndUpdateAccountInfo(
  314. IN USHORT Length,
  315. IN PUNICODE_STRING pUnicodeString,
  316. IN OUT PUCHAR* pSource,
  317. IN OUT PUCHAR* pDest
  318. );
  319. VOID
  320. NlpSetTimeField(
  321. OUT POLD_LARGE_INTEGER pTimeField,
  322. IN NLP_SET_TIME_HINT Hint
  323. );
  324. NTSTATUS
  325. NlpBuildAccountInfo(
  326. IN PLOGON_CACHE_ENTRY pCacheEntry,
  327. IN ULONG EntryLength,
  328. OUT PNETLOGON_VALIDATION_SAM_INFO4* AccountInfo
  329. );
  330. /////////////////////////////////////////////////////////////////////////
  331. // //
  332. // Diagnostic support services prototypes //
  333. // //
  334. /////////////////////////////////////////////////////////////////////////
  335. #if DBG
  336. PCHAR
  337. DumpOwfPasswordToString(
  338. OUT PCHAR Buffer,
  339. IN PLM_OWF_PASSWORD Password
  340. );
  341. VOID
  342. DumpLogonInfo(
  343. IN PNETLOGON_LOGON_IDENTITY_INFO LogonInfo
  344. );
  345. char*
  346. MapWeekday(
  347. IN CSHORT Weekday
  348. );
  349. VOID
  350. DumpTime(
  351. IN LPSTR String,
  352. IN POLD_LARGE_INTEGER OldTime
  353. );
  354. VOID
  355. DumpGroupIds(
  356. IN LPSTR String,
  357. IN ULONG Count,
  358. IN PGROUP_MEMBERSHIP GroupIds
  359. );
  360. VOID
  361. DumpSessKey(
  362. IN LPSTR String,
  363. IN PUSER_SESSION_KEY Key
  364. );
  365. VOID
  366. DumpSid(
  367. LPSTR String,
  368. PISID Sid
  369. );
  370. VOID
  371. DumpAccountInfo(
  372. IN PNETLOGON_VALIDATION_SAM_INFO4 AccountInfo
  373. );
  374. VOID
  375. DumpCacheEntry(
  376. IN ULONG Index,
  377. IN PLOGON_CACHE_ENTRY pEntry
  378. );
  379. #endif //DBG
  380. ////////////////////////////////////////////////////////////////////////
  381. // //
  382. // global data //
  383. // //
  384. ////////////////////////////////////////////////////////////////////////
  385. //
  386. // This boolean indicates whether or not we have been able to
  387. // initialize caching yet. It turn out that during authentication
  388. // package load time, we can't do everything we would like to (like
  389. // call LSA RPC routines). So, we delay initializing until we can
  390. // call LSA. All publicly exposed interfaces must check this value
  391. // before assuming work can be done.
  392. //
  393. BOOLEAN NlpInitializationNotYetPerformed = TRUE;
  394. RTL_RESOURCE NlpLogonCacheCritSec;
  395. HANDLE NlpCacheHandle = (HANDLE) INVALID_HANDLE_VALUE;
  396. LSAPR_HANDLE NlpSecretHandle = (LSAPR_HANDLE) INVALID_HANDLE_VALUE;
  397. //
  398. // control information about the cache (number of entries, etc).
  399. //
  400. NLP_CACHE_CONTROL NlpCacheControl;
  401. //
  402. // This structure is generated and maintained only in memory.
  403. // It indicates which cache entries are valid and which aren't.
  404. // It also indicates what time each entry was established so we
  405. // know which order to discard them in.
  406. //
  407. // This field is a pointer to an array of CTEs. The number of CTEs
  408. // in the array is in NlpCacheControl.Entries. This structure is
  409. // allocated at initialization time.
  410. //
  411. PNLP_CTE NlpCteTable;
  412. //
  413. // The Cache Table Entries in NlpCteTable are linked on either an
  414. // active or inactive list. The entries on the active list are in
  415. // ascending time order - so the last one on the list is the first
  416. // one to be discarded when a flush is needed to add a new entry.
  417. //
  418. LIST_ENTRY NlpActiveCtes;
  419. LIST_ENTRY NlpInactiveCtes;
  420. //
  421. // global, per-machine key used for encrypting NT_5_0 version cache
  422. // entries.
  423. //
  424. CHAR NlpCacheEncryptionKey[ NLP_CACHE_ENCRYPTION_KEY_LEN ];
  425. #if DBG
  426. #ifdef DUMP_CACHE_INFO
  427. ULONG DumpCacheInfo = 1;
  428. #else
  429. ULONG DumpCacheInfo = 0;
  430. #endif
  431. #endif
  432. ////////////////////////////////////////////////////////////////////////
  433. // //
  434. // Services Exported by this module //
  435. // //
  436. ////////////////////////////////////////////////////////////////////////
  437. NTSTATUS
  438. NlpCacheInitialize(
  439. VOID
  440. )
  441. /*++
  442. Routine Description:
  443. This routine is called to initialize cached logon processing.
  444. Unfortunately, there isn't much we can do when we are called.
  445. (we can't open LSA, for example). So, defer initialization
  446. until later.
  447. Arguments:
  448. None.
  449. Return Value:
  450. NTSTATUS
  451. --*/
  452. {
  453. RtlInitializeResource(&NlpLogonCacheCritSec);
  454. return STATUS_SUCCESS;
  455. }
  456. NTSTATUS
  457. NlpCacheTerminate(
  458. VOID
  459. )
  460. /*++
  461. Routine Description:
  462. Called when process detaches
  463. Arguments:
  464. None.
  465. Return Value:
  466. NTSTATUS
  467. --*/
  468. {
  469. #if DBG
  470. if (DumpCacheInfo) {
  471. DbgPrint("NlpCacheTerminate\n");
  472. }
  473. #endif
  474. if (!NlpInitializationNotYetPerformed) {
  475. NlpCloseCache();
  476. NlpCloseSecret();
  477. if (IS_VALID_HANDLE(NlpCacheHandle)) {
  478. NtClose( NlpCacheHandle );
  479. }
  480. FreeToHeap( NlpCteTable );
  481. }
  482. RtlDeleteResource(&NlpLogonCacheCritSec);
  483. return STATUS_SUCCESS;
  484. }
  485. NTSTATUS
  486. NlpGetCacheEntry(
  487. IN PNETLOGON_LOGON_IDENTITY_INFO LogonInfo,
  488. OUT PNETLOGON_VALIDATION_SAM_INFO4* AccountInfo,
  489. OUT PCACHE_PASSWORDS Passwords,
  490. OUT PVOID *ppSupplementalCacheData OPTIONAL ,
  491. OUT PULONG SupplementalCacheDataLength OPTIONAL
  492. )
  493. /*++
  494. Routine Description:
  495. If the user logging on has information stored in the cache,
  496. then it is retrieved. Also returns the cached password from
  497. 'secret' storage
  498. Arguments:
  499. LogonInfo - pointer to NETLOGON_IDENTITY_INFO structure which contains
  500. the domain name, user name for this user
  501. AccountInfo - pointer to NETLOGON_VALIDATION_SAM_INFO4 structure to
  502. receive this user's specific interactive logon information
  503. Passwords - pointer to CACHE_PASSWORDS structure to receive passwords
  504. returned from secret storage
  505. Return Value:
  506. NTSTATUS
  507. Success = STATUS_SUCCESS
  508. *AccountInfo points to a NETLOGON_VALIDATION_SAM_INFO4
  509. structure. This must be freed by caller
  510. *Passwords contain USER_INTERNAL1_INFORMATION structure
  511. which contains NT OWF password and LM OWF password. These
  512. must be used to validate the logon
  513. Failure = STATUS_LOGON_FAILURE
  514. The user logging on isn't in the cache.
  515. --*/
  516. {
  517. NTSTATUS
  518. NtStatus;
  519. PNETLOGON_VALIDATION_SAM_INFO4
  520. SamInfo = NULL;
  521. PLOGON_CACHE_ENTRY
  522. CacheEntry = NULL;
  523. ULONG
  524. EntrySize,
  525. Index;
  526. PLSAPR_CR_CIPHER_VALUE
  527. CurrentSecret = NULL,
  528. OldSecret = NULL;
  529. BOOLEAN fCacheLocked = FALSE;
  530. *AccountInfo = NULL;
  531. if( ppSupplementalCacheData )
  532. *ppSupplementalCacheData = NULL;
  533. #if DBG
  534. if (DumpCacheInfo) {
  535. DbgPrint("NlpGetCacheEntry\n");
  536. DumpLogonInfo(LogonInfo);
  537. }
  538. #endif
  539. if (NlpInitializationNotYetPerformed) {
  540. NtStatus = NlpInternalCacheInitialize();
  541. if (!NT_SUCCESS(NtStatus)) {
  542. return(NtStatus);
  543. }
  544. }
  545. if (NlpCacheControl.Entries == 0) {
  546. return(STATUS_LOGON_FAILURE);
  547. }
  548. //
  549. // TODO: consider comparing LogonDomainName to NlpSamDomainName
  550. // and failing cached logon attempts at local machine.
  551. //
  552. READ_CACHE();
  553. fCacheLocked = TRUE;
  554. //
  555. // Find the cache entry and open its secret (if found)
  556. //
  557. NtStatus = NlpReadCacheEntry(&LogonInfo->LogonDomainName,
  558. &LogonInfo->UserName,
  559. &Index,
  560. &CacheEntry,
  561. &EntrySize);
  562. if(!NT_SUCCESS(NtStatus)) {
  563. LEAVE_CACHE();
  564. return (NtStatus);
  565. }
  566. if( CacheEntry->Revision >= NLP_CACHE_REVISION_NT_5_0 ) {
  567. //
  568. // for NT5, we can release the cache lock now, since all data
  569. // stored in one place.
  570. //
  571. LEAVE_CACHE();
  572. fCacheLocked = FALSE;
  573. //
  574. // if caller wanted supplemental data, give it to them.
  575. //
  576. if( ppSupplementalCacheData && SupplementalCacheDataLength )
  577. {
  578. LPBYTE Source;
  579. *SupplementalCacheDataLength = CacheEntry->SupplementalCacheDataLength;
  580. *ppSupplementalCacheData = MIDL_user_allocate( *SupplementalCacheDataLength );
  581. if( *ppSupplementalCacheData == NULL ) {
  582. NtStatus = STATUS_NO_MEMORY;
  583. goto Cleanup;
  584. }
  585. //
  586. // note: the decrypt operation that occurred during the
  587. // ReadCacheEntry validates any data and pointers through
  588. // integrity checking via HMAC. Having said that, we can be
  589. // lazy and not do boundry checking.
  590. //
  591. Source = ((LPBYTE)CacheEntry + CacheEntry->SupplementalCacheDataOffset);
  592. CopyMemory( *ppSupplementalCacheData,
  593. Source,
  594. *SupplementalCacheDataLength
  595. );
  596. }
  597. }
  598. NtStatus = NlpBuildAccountInfo(CacheEntry, EntrySize, &SamInfo);
  599. if (!NT_SUCCESS(NtStatus))
  600. {
  601. goto Cleanup;
  602. }
  603. if( CacheEntry->Revision >= NLP_CACHE_REVISION_NT_5_0 ) {
  604. //
  605. // for NT5, the Passwords are stored in the CacheEntry.
  606. // note: passwords are assumed to be salted.
  607. //
  608. RtlCopyMemory( Passwords, &(CacheEntry->CachePasswords), sizeof(*Passwords) );
  609. } else {
  610. //
  611. // prior to NT5, the Passwords are stored separately in their
  612. // own LSA secret.
  613. //
  614. NtStatus = NlpReadSecret(&CurrentSecret, &OldSecret);
  615. if(!NT_SUCCESS(NtStatus))
  616. {
  617. goto Cleanup;
  618. }
  619. if ( CurrentSecret == NULL )
  620. {
  621. NtStatus = STATUS_LOGON_FAILURE;
  622. goto Cleanup;
  623. }
  624. //
  625. // can release the cache lock now, since second data item fetched.
  626. //
  627. LEAVE_CACHE();
  628. fCacheLocked = FALSE;
  629. //
  630. // Check to see which version of the passwords are stored
  631. // here - the normal or the salted.
  632. //
  633. RtlCopyMemory((PVOID)Passwords,
  634. (PVOID)CurrentSecret->Buffer,
  635. (ULONG)CurrentSecret->Length
  636. );
  637. if ( CacheEntry->Revision < NLP_CACHE_REVISION_NT_4_SP4 )
  638. {
  639. if (Passwords->SecretPasswords.NtPasswordPresent)
  640. {
  641. NtStatus = NlpComputeSaltedHashedPassword(
  642. &Passwords->SecretPasswords.NtOwfPassword,
  643. &Passwords->SecretPasswords.NtOwfPassword,
  644. &SamInfo->EffectiveName
  645. );
  646. if(!NT_SUCCESS(NtStatus))
  647. {
  648. goto Cleanup;
  649. }
  650. }
  651. if (Passwords->SecretPasswords.LmPasswordPresent)
  652. {
  653. NtStatus = NlpComputeSaltedHashedPassword(
  654. &Passwords->SecretPasswords.LmOwfPassword,
  655. &Passwords->SecretPasswords.LmOwfPassword,
  656. &SamInfo->EffectiveName
  657. );
  658. if(!NT_SUCCESS(NtStatus))
  659. {
  660. goto Cleanup;
  661. }
  662. }
  663. }
  664. }
  665. Cleanup:
  666. if( fCacheLocked ) {
  667. LEAVE_CACHE();
  668. }
  669. //
  670. // free structure allocated by NlpReadCacheEntry
  671. //
  672. if( CacheEntry ) {
  673. ZeroMemory( CacheEntry, EntrySize );
  674. FreeToHeap(CacheEntry);
  675. }
  676. //
  677. // free structures allocated by NlpReadSecret
  678. //
  679. if (CurrentSecret) {
  680. MIDL_user_free(CurrentSecret);
  681. }
  682. if (OldSecret) {
  683. MIDL_user_free(OldSecret);
  684. }
  685. if( NT_SUCCESS( NtStatus ) ) {
  686. *AccountInfo = SamInfo;
  687. } else {
  688. if ( SamInfo != NULL ) {
  689. MIDL_user_free( SamInfo );
  690. }
  691. if( ppSupplementalCacheData && *ppSupplementalCacheData ) {
  692. MIDL_user_free( *ppSupplementalCacheData );
  693. *ppSupplementalCacheData = NULL;
  694. }
  695. }
  696. return(NtStatus);
  697. }
  698. NTSTATUS
  699. NlpAddCacheEntry(
  700. IN PNETLOGON_INTERACTIVE_INFO LogonInfo,
  701. IN PNETLOGON_VALIDATION_SAM_INFO4 AccountInfo,
  702. IN PVOID SupplementalCacheData,
  703. IN ULONG SupplementalCacheDataLength,
  704. IN ULONG CacheFlags
  705. )
  706. /*++
  707. Routine Description:
  708. Adds this domain:user interactive logon information to the cache.
  709. Arguments:
  710. LogonInfo - pointer to NETLOGON_INTERACTIVE_INFO structure which contains
  711. the domain name, user name and password for this user. These
  712. are what the user typed to WinLogon
  713. AccountInfo - pointer to NETLOGON_VALIDATION_SAM_INFO4 structure which
  714. contains this user's specific interactive logon information
  715. Return Value:
  716. NTSTATUS
  717. Success = STATUS_SUCCESS
  718. AccountInfo successfully added to cache
  719. Failure = STATUS_NO_MEMORY
  720. --*/
  721. {
  722. NTSTATUS
  723. NtStatus;
  724. PLOGON_CACHE_ENTRY
  725. CacheEntry = NULL;
  726. PLOGON_CACHE_ENTRY
  727. CacheEntryExisting = NULL;
  728. ULONG
  729. EntrySize,
  730. EntrySizeExisting,
  731. Index;
  732. PUNICODE_STRING LogonDomainName;
  733. UNICODE_STRING NullString = {0,0,NULL};
  734. BOOLEAN fCacheLocked = FALSE;
  735. #if DBG
  736. if (DumpCacheInfo) {
  737. DbgPrint("NlpAddCacheEntry\n");
  738. DumpLogonInfo(&LogonInfo->Identity);
  739. DumpAccountInfo(AccountInfo);
  740. }
  741. #endif
  742. if (NlpInitializationNotYetPerformed) {
  743. NtStatus = NlpInternalCacheInitialize();
  744. if (!NT_SUCCESS(NtStatus)) {
  745. return(NtStatus);
  746. }
  747. }
  748. if (NlpCacheControl.Entries == 0) {
  749. return(STATUS_SUCCESS);
  750. }
  751. //
  752. // LogonUser() allows for a NULL domain name to be supplied, which
  753. // causes netlogon search logic to kick in. this can result to logon
  754. // packages requesting to cache local account information.
  755. // In this case, use the LogonDomainName that SAM provides to make
  756. // a decision about whether to allow caching. In the same scenario,
  757. // if we decide caching is allowed, the cache entry target domain
  758. // is also set based on what SAM returned.
  759. //
  760. // For MIT logons, don't use logon info.
  761. //
  762. //
  763. if ((CacheFlags & MSV1_0_CACHE_LOGON_REQUEST_MIT_LOGON) != 0) {
  764. LogonDomainName = &(NullString);
  765. }
  766. else if( LogonInfo->Identity.LogonDomainName.Length != 0 ) {
  767. LogonDomainName = &(LogonInfo->Identity.LogonDomainName);
  768. } else {
  769. LogonDomainName = &(AccountInfo->LogonDomainName);
  770. }
  771. if( NlpSamDomainName.Buffer &&
  772. RtlEqualDomainName(LogonDomainName, &NlpSamDomainName)
  773. )
  774. {
  775. #if DBG
  776. if (DumpCacheInfo) {
  777. DbgPrint("NlpAddCacheEntry: attempt to cache against local account skipped.\n");
  778. }
  779. #endif
  780. return STATUS_SUCCESS;
  781. }
  782. //
  783. // build base cache entry.
  784. //
  785. NtStatus = NlpBuildCacheEntry(
  786. LogonInfo,
  787. AccountInfo,
  788. CacheFlags,
  789. &CacheEntry,
  790. &EntrySize
  791. );
  792. if(!NT_SUCCESS(NtStatus) )
  793. {
  794. return (NtStatus);
  795. }
  796. //
  797. // add any supplemental data to the cache entry.
  798. // (this is new for NT5).
  799. //
  800. NtStatus = NlpAddSupplementalCacheData(
  801. SupplementalCacheData,
  802. SupplementalCacheDataLength,
  803. &CacheEntry,
  804. &EntrySize
  805. );
  806. if(!NT_SUCCESS(NtStatus)) {
  807. goto Cleanup;
  808. }
  809. //
  810. // add in salted OWFs.
  811. //
  812. NtStatus = NlpMakeSecretPasswordNT5(
  813. &CacheEntry->CachePasswords,
  814. &AccountInfo->EffectiveName,
  815. &LogonInfo->NtOwfPassword,
  816. &LogonInfo->LmOwfPassword
  817. );
  818. if(!NT_SUCCESS(NtStatus)) {
  819. goto Cleanup;
  820. }
  821. READ_CACHE();
  822. fCacheLocked = TRUE;
  823. //
  824. // See if this entry already exists in the cache.
  825. // If so, use the same index.
  826. //
  827. NtStatus = NlpReadCacheEntry( LogonDomainName,
  828. &LogonInfo->Identity.UserName,
  829. &Index,
  830. &CacheEntryExisting,
  831. &EntrySizeExisting
  832. );
  833. //
  834. // If we didn't find an entry, then we need to allocate an
  835. // entry.
  836. //
  837. if (!NT_SUCCESS(NtStatus)) {
  838. NlpGetFreeEntryIndex( &Index );
  839. CacheEntryExisting = NULL;
  840. } else {
  841. //
  842. // We already have an entry for this user.
  843. // Discard the structure we got back but
  844. // use the same index.
  845. //
  846. // TODO: check if existing entry matches new built entry.
  847. // if so, avoid write.
  848. BOOLEAN fMatchesExisting;
  849. fMatchesExisting = NlpCompareCacheEntry(
  850. CacheEntry,
  851. EntrySize,
  852. CacheEntryExisting,
  853. EntrySizeExisting
  854. );
  855. if( fMatchesExisting )
  856. {
  857. goto Cleanup;
  858. }
  859. }
  860. //
  861. // encrypt sensitive portions of the cache entry.
  862. // note: this was done prior to locking the cache, but, in the interest
  863. // of allowing for cache compare above, the encryption is deferred until
  864. // now.
  865. //
  866. NtStatus = NlpEncryptCacheEntry(CacheEntry, EntrySize);
  867. if(!NT_SUCCESS(NtStatus)) {
  868. goto Cleanup;
  869. }
  870. //
  871. // we already have the read lock, convert it to write-lock.
  872. //
  873. READ_TO_WRITE_CACHE();
  874. //
  875. // now, write the entry out...
  876. //
  877. NtStatus = NlpWriteCacheEntry(Index, CacheEntry, EntrySize);
  878. if (NT_SUCCESS(NtStatus)) {
  879. NlpCteTable[Index].Time = CacheEntry->Time;
  880. NlpAddEntryToActiveList( Index );
  881. }
  882. Cleanup:
  883. if( fCacheLocked )
  884. {
  885. LEAVE_CACHE();
  886. }
  887. if( CacheEntry ) {
  888. ZeroMemory( CacheEntry, EntrySize );
  889. FreeCacheEntry( CacheEntry );
  890. }
  891. if( CacheEntryExisting ) {
  892. ZeroMemory( CacheEntryExisting, EntrySizeExisting );
  893. FreeCacheEntry( CacheEntryExisting );
  894. }
  895. return(NtStatus);
  896. }
  897. NTSTATUS
  898. NlpAddSupplementalCacheData(
  899. IN PVOID SupplementalCacheData,
  900. IN ULONG SupplementalCacheDataLength,
  901. IN OUT PLOGON_CACHE_ENTRY *ppCacheEntry,
  902. IN OUT PULONG pEntryLength
  903. )
  904. /*++
  905. Routine Description:
  906. Extends the supplied LOGON_CACHE_ENTRY with opaque authentication package
  907. SupplementalCacheData (eg: smart-card logon cache info).
  908. Return Value:
  909. NTSTATUS
  910. Success = STATUS_SUCCESS
  911. Failure =
  912. --*/
  913. {
  914. PLOGON_CACHE_ENTRY NewCacheEntry = NULL;
  915. if( (*ppCacheEntry)->Revision < NLP_CACHE_REVISION_NT_5_0 ) {
  916. return STATUS_SUCCESS;
  917. }
  918. (*ppCacheEntry)->SupplementalCacheDataLength = SupplementalCacheDataLength;
  919. (*ppCacheEntry)->SupplementalCacheDataOffset = *pEntryLength;
  920. if( SupplementalCacheData == NULL || SupplementalCacheDataLength == 0 ) {
  921. return STATUS_SUCCESS;
  922. }
  923. //
  924. // allocate new entry, and copy existing entry + supplemental data to end.
  925. //
  926. NewCacheEntry = AllocateCacheEntry( *pEntryLength + SupplementalCacheDataLength );
  927. if( NewCacheEntry == NULL ) {
  928. return STATUS_NO_MEMORY;
  929. }
  930. CopyMemory( NewCacheEntry, *ppCacheEntry, *pEntryLength );
  931. CopyMemory( ((PBYTE)(NewCacheEntry) + *pEntryLength),
  932. SupplementalCacheData,
  933. SupplementalCacheDataLength
  934. );
  935. ZeroMemory( *ppCacheEntry, *pEntryLength );
  936. FreeCacheEntry( *ppCacheEntry );
  937. *ppCacheEntry = NewCacheEntry;
  938. *pEntryLength += SupplementalCacheDataLength;
  939. return STATUS_SUCCESS;
  940. }
  941. NTSTATUS
  942. NlpDeleteCacheEntry(
  943. IN PNETLOGON_INTERACTIVE_INFO LogonInfo
  944. )
  945. /*++
  946. Routine Description:
  947. Deletes a user account from the local user account cache, if the corresponding
  948. entry can be found. We actually just null out the current contents instead of
  949. destroying the storage - this should save us some time when we next come to
  950. add an entry to the cache
  951. Arguments:
  952. LogonInfo - pointer to NETLOGON_INTERACTIVE_INFO structure which contains
  953. the domain name, user name and password for this user
  954. Return Value:
  955. NTSTATUS
  956. Success = STATUS_SUCCESS
  957. Failure =
  958. --*/
  959. {
  960. NTSTATUS
  961. NtStatus;
  962. PLOGON_CACHE_ENTRY
  963. CacheEntry = NULL;
  964. ULONG
  965. EntrySize,
  966. Index;
  967. if (NlpInitializationNotYetPerformed) {
  968. NtStatus = NlpInternalCacheInitialize();
  969. if (!NT_SUCCESS(NtStatus)) {
  970. return(NtStatus);
  971. }
  972. }
  973. if (NlpCacheControl.Entries == 0) {
  974. return(STATUS_SUCCESS);
  975. }
  976. WRITE_CACHE();
  977. //
  978. // See if this entry exists in the cache.
  979. //
  980. NtStatus = NlpReadCacheEntry( &LogonInfo->Identity.LogonDomainName,
  981. &LogonInfo->Identity.UserName,
  982. &Index,
  983. &CacheEntry,
  984. &EntrySize
  985. );
  986. //
  987. // If we didn't find an entry, then there is nothing to do.
  988. //
  989. if (!NT_SUCCESS(NtStatus)) {
  990. LEAVE_CACHE();
  991. return(STATUS_SUCCESS);
  992. }
  993. //
  994. // Mark it as invalid.
  995. //
  996. CacheEntry->Valid = FALSE;
  997. NtStatus = NlpWriteCacheEntry( Index, CacheEntry, EntrySize );
  998. if (NT_SUCCESS(NtStatus)) {
  999. //
  1000. // Put the CTE entry on the inactive list.
  1001. //
  1002. NlpAddEntryToInactiveList( Index );
  1003. }
  1004. LEAVE_CACHE();
  1005. //
  1006. // Free the structure returned from NlpReadCacheEntry()
  1007. //
  1008. if( CacheEntry ) {
  1009. ZeroMemory( CacheEntry, EntrySize );
  1010. FreeToHeap( CacheEntry );
  1011. }
  1012. return(NtStatus);
  1013. }
  1014. VOID
  1015. NlpChangeCachePassword(
  1016. IN PUNICODE_STRING DomainName,
  1017. IN PUNICODE_STRING UserName,
  1018. IN PLM_OWF_PASSWORD LmOwfPassword,
  1019. IN PNT_OWF_PASSWORD NtOwfPassword
  1020. )
  1021. /*++
  1022. Routine Description:
  1023. Update a cached password to the specified value, if we have
  1024. the specified account cached.
  1025. Arguments:
  1026. DomainName - The name of the domain in which the account exists.
  1027. UserName - The name of the account whose password is to be changed.
  1028. LmOwfPassword - The new LM compatible password.
  1029. NtOwfPassword - The new NT compatible password.
  1030. Return Value:
  1031. None.
  1032. --*/
  1033. {
  1034. NTSTATUS
  1035. NtStatus;
  1036. PLOGON_CACHE_ENTRY
  1037. CacheEntry = NULL;
  1038. ULONG
  1039. EntrySize,
  1040. Index;
  1041. PLSAPR_CR_CIPHER_VALUE
  1042. CurrentSecret = NULL,
  1043. OldSecret = NULL;
  1044. LSAPR_CR_CIPHER_VALUE
  1045. Passwords;
  1046. #if DBG
  1047. if (DumpCacheInfo) {
  1048. DbgPrint("NlpChangeCachePassword\n");
  1049. }
  1050. #endif
  1051. if (NlpInitializationNotYetPerformed) {
  1052. NtStatus = NlpInternalCacheInitialize();
  1053. if (!NT_SUCCESS(NtStatus)) {
  1054. return;
  1055. }
  1056. }
  1057. if (NlpCacheControl.Entries == 0) {
  1058. return;
  1059. }
  1060. WRITE_CACHE();
  1061. NtStatus = NlpReadCacheEntry( DomainName,
  1062. UserName,
  1063. &Index,
  1064. &CacheEntry,
  1065. &EntrySize);
  1066. if(!NT_SUCCESS( NtStatus) ) {
  1067. LEAVE_CACHE();
  1068. return ;
  1069. }
  1070. if( CacheEntry->Revision >= NLP_CACHE_REVISION_NT_5_0 ) {
  1071. UNICODE_STRING CachedUser;
  1072. CachedUser.Length =
  1073. CachedUser.MaximumLength = CacheEntry->UserNameLength;
  1074. CachedUser.Buffer = (PWSTR) ((PBYTE) CacheEntry + sizeof(LOGON_CACHE_ENTRY));
  1075. NtStatus = NlpMakeSecretPasswordNT5( &CacheEntry->CachePasswords,
  1076. &CachedUser,
  1077. NtOwfPassword,
  1078. LmOwfPassword );
  1079. if(NT_SUCCESS(NtStatus)) {
  1080. //
  1081. // encrypt the entry...
  1082. //
  1083. NtStatus = NlpEncryptCacheEntry( CacheEntry, EntrySize );
  1084. }
  1085. if(NT_SUCCESS( NtStatus )) {
  1086. //
  1087. // now, write the entry out...
  1088. //
  1089. NtStatus = NlpWriteCacheEntry(Index, CacheEntry, EntrySize);
  1090. #ifdef DBG
  1091. if(DumpCacheInfo) {
  1092. if( NT_SUCCESS( NtStatus ) ) {
  1093. DbgPrint("NlpChangeCachePassword: SUCCEED write NT5 version cache entry.\n");
  1094. } else {
  1095. DbgPrint("NlpChangeCachePassword: FAIL write NT5 version cache entry.\n");
  1096. }
  1097. }
  1098. #endif
  1099. }
  1100. } else {
  1101. NtStatus = NlpOpenSecret( Index );
  1102. if (NT_SUCCESS(NtStatus)) {
  1103. NtStatus = NlpReadSecret(&CurrentSecret, &OldSecret);
  1104. if (NT_SUCCESS(NtStatus)) {
  1105. UNICODE_STRING CachedUser;
  1106. //
  1107. // Grab the various strings from the cache entry.
  1108. //
  1109. ASSERT( CacheEntry->Revision >= NLP_CACHE_REVISION_NT_1_0B );
  1110. CachedUser.Length =
  1111. CachedUser.MaximumLength = CacheEntry->UserNameLength;
  1112. CachedUser.Buffer = (PWSTR) ((PBYTE) CacheEntry + sizeof(LOGON_CACHE_ENTRY_NT_4_SP4));
  1113. NtStatus = NlpMakeSecretPassword( &Passwords,
  1114. &CachedUser,
  1115. NtOwfPassword,
  1116. LmOwfPassword );
  1117. if (NT_SUCCESS(NtStatus)) {
  1118. NtStatus = NlpWriteSecret(&Passwords, CurrentSecret);
  1119. //
  1120. // free the buffer allocated to store the passwords
  1121. //
  1122. FreeToHeap(Passwords.Buffer);
  1123. }
  1124. //
  1125. // free strings returned by NlpReadSecret
  1126. //
  1127. if (CurrentSecret) {
  1128. MIDL_user_free(CurrentSecret);
  1129. }
  1130. if (OldSecret) {
  1131. MIDL_user_free(OldSecret);
  1132. }
  1133. }
  1134. }
  1135. }
  1136. LEAVE_CACHE();
  1137. //
  1138. // free structure allocated by NlpReadCacheEntry
  1139. //
  1140. if( CacheEntry ) {
  1141. ZeroMemory( CacheEntry, EntrySize );
  1142. FreeToHeap(CacheEntry);
  1143. }
  1144. return;
  1145. }
  1146. ////////////////////////////////////////////////////////////////////////
  1147. // //
  1148. // Services Internal to this module //
  1149. // //
  1150. ////////////////////////////////////////////////////////////////////////
  1151. NTSTATUS
  1152. NlpInternalCacheInitialize(
  1153. VOID
  1154. )
  1155. /*++
  1156. Routine Description:
  1157. This routine is called to initialize cached logon processing.
  1158. This routine will automatically adjust the size of the logon
  1159. cache if necessary to accomodate a new user-specified length
  1160. (specified in the Winlogon part of the registry).
  1161. NOTE: If called too early, this routine won't be able to call
  1162. LSA's RPC routines. In this case, initialization is
  1163. defered until later.
  1164. Arguments:
  1165. None.
  1166. Return Value:
  1167. NTSTATUS
  1168. --*/
  1169. {
  1170. NTSTATUS
  1171. NtStatus;
  1172. // DbgPrint("\n\n\n REMEMBER TO TAKE THIS BREAKPOINT OUT BEFORE CHECKIN.\n\n\n");
  1173. // DumpCacheInfo = 1; // Remember to take this out too !!!!!!
  1174. // DbgBreakPoint(); // Remember to take this out before checking
  1175. #if DBG
  1176. if (DumpCacheInfo) {
  1177. DbgPrint("NlpCacheInitialize\n");
  1178. }
  1179. #endif
  1180. //
  1181. // Upon return from this routine, if logon caching is enabled,
  1182. // the following will be true:
  1183. //
  1184. // A handle to the registry key in which all cache entries
  1185. // are held will be open (NlpCacheHandle).
  1186. //
  1187. // A global structure defining how many cache entries there are
  1188. // will be initialized (NlpCacheControl).
  1189. //
  1190. // The Cache Table Entry table (CTE table) will be initialized
  1191. // (NlpCteTable).
  1192. //
  1193. // The active and inactive CTE lists will be built
  1194. // (NlpActiveCtes and NlpInactiveCtes).
  1195. //
  1196. // A global cache encryption key will be initialized.
  1197. //
  1198. WRITE_CACHE();
  1199. //
  1200. // Check again if the cache is initialized now that the crit sect is locked.
  1201. //
  1202. if (NlpInitializationNotYetPerformed) {
  1203. //
  1204. // Open the local system's policy object
  1205. //
  1206. //
  1207. // Successfully, or unsucessfully,
  1208. // The definition of "initialized" is we could call LSA's RPC
  1209. // routines.
  1210. //
  1211. NlpInitializationNotYetPerformed = FALSE;
  1212. //
  1213. // Open the registry key containing cache entries.
  1214. // This will remain open.
  1215. //
  1216. NtStatus = NlpOpenCache();
  1217. if (NT_SUCCESS(NtStatus)) {
  1218. //
  1219. // Get information on the current cache structure
  1220. // (number of entries, et cetera). This information is
  1221. // placed in a global variable for use throughout this
  1222. // module.
  1223. //
  1224. NtStatus = NlpGetCacheControlInfo();
  1225. //
  1226. // Initialize the per-machine cache encryption key.
  1227. //
  1228. if(NT_SUCCESS( NtStatus) ) {
  1229. NtStatus = NlpCacheKeyInitialize();
  1230. }
  1231. //
  1232. // Now build the CTE table
  1233. //
  1234. if (NT_SUCCESS(NtStatus)) {
  1235. NtStatus = NlpBuildCteTable();
  1236. }
  1237. //
  1238. // If we were successful, then see if we need to change
  1239. // the cache due to new user-specified cache size.
  1240. //
  1241. if (NT_SUCCESS(NtStatus)) {
  1242. NtStatus = NlpChangeCacheSizeIfNecessary();
  1243. }
  1244. if (!NT_SUCCESS(NtStatus)) {
  1245. NlpCloseCache();
  1246. }
  1247. }
  1248. //
  1249. // If we had an error, then set our entry count to zero
  1250. // to prevent using any cache information.
  1251. //
  1252. if (!NT_SUCCESS(NtStatus)) {
  1253. NlpCacheControl.Entries = 0;
  1254. }
  1255. } else {
  1256. NtStatus = STATUS_SUCCESS;
  1257. }
  1258. LEAVE_CACHE();
  1259. return(NtStatus);
  1260. }
  1261. NTSTATUS
  1262. NlpCacheKeyInitialize(
  1263. VOID
  1264. )
  1265. /*++
  1266. Routine Description:
  1267. Initializes the Global variable NlpCacheEncryptionKey with a per-machine
  1268. cache encryption key. If the per-machine key does not exist as an LSA
  1269. secret, it is created.
  1270. --*/
  1271. {
  1272. LSAPR_HANDLE SecretHandle;
  1273. UNICODE_STRING ValueName;
  1274. BOOLEAN SecretCreationNeeded = FALSE;
  1275. NTSTATUS NtStatus;
  1276. RtlInitUnicodeString( &ValueName, NLP_CACHE_ENCRYPTION_KEY_NAME );
  1277. NtStatus = I_LsarOpenSecret(NtLmGlobalPolicyHandle,
  1278. (PLSAPR_UNICODE_STRING) &ValueName,
  1279. SECRET_QUERY_VALUE | SECRET_SET_VALUE,
  1280. &SecretHandle
  1281. );
  1282. if (!NT_SUCCESS(NtStatus)) {
  1283. //
  1284. // create new key, if not present.
  1285. //
  1286. if (NtStatus != STATUS_OBJECT_NAME_NOT_FOUND) {
  1287. return (NtStatus);
  1288. }
  1289. NtStatus = I_LsarCreateSecret(NtLmGlobalPolicyHandle,
  1290. (PLSAPR_UNICODE_STRING) &ValueName,
  1291. SECRET_SET_VALUE,
  1292. &SecretHandle
  1293. );
  1294. if (!NT_SUCCESS(NtStatus)) {
  1295. return (NtStatus);
  1296. }
  1297. SecretCreationNeeded = TRUE;
  1298. } else {
  1299. //
  1300. // query current value...
  1301. //
  1302. LARGE_INTEGER
  1303. CurrentTime;
  1304. PLSAPR_CR_CIPHER_VALUE CurrentSecret = NULL;
  1305. NtStatus = I_LsarQuerySecret(SecretHandle,
  1306. &CurrentSecret,
  1307. &CurrentTime,
  1308. NULL,
  1309. NULL
  1310. );
  1311. if(NT_SUCCESS( NtStatus ) ) {
  1312. if( CurrentSecret == NULL ) {
  1313. //
  1314. // non existing data, create it.
  1315. //
  1316. SecretCreationNeeded = TRUE;
  1317. } else {
  1318. //
  1319. // size of data is wrong, bail now and leave things as-is.
  1320. //
  1321. if( CurrentSecret->Length != sizeof( NlpCacheEncryptionKey ) ) {
  1322. NtStatus = STATUS_SECRET_TOO_LONG;
  1323. } else {
  1324. //
  1325. // capture existing data into global.
  1326. //
  1327. CopyMemory( NlpCacheEncryptionKey, CurrentSecret->Buffer, CurrentSecret->Length );
  1328. }
  1329. MIDL_user_free(CurrentSecret);
  1330. }
  1331. }
  1332. }
  1333. if( SecretCreationNeeded ) {
  1334. LSAPR_CR_CIPHER_VALUE SecretValue;
  1335. SspGenerateRandomBits( NlpCacheEncryptionKey, sizeof(NlpCacheEncryptionKey) );
  1336. //
  1337. // write out secret...
  1338. //
  1339. SecretValue.Length = sizeof(NlpCacheEncryptionKey);
  1340. SecretValue.MaximumLength = SecretValue.Length;
  1341. SecretValue.Buffer = (PBYTE)NlpCacheEncryptionKey;
  1342. NtStatus = I_LsarSetSecret(SecretHandle,
  1343. &SecretValue,
  1344. NULL
  1345. );
  1346. }
  1347. I_LsarClose( &SecretHandle );
  1348. return (NtStatus);
  1349. }
  1350. BOOLEAN
  1351. NlpCompareCacheEntry(
  1352. IN PLOGON_CACHE_ENTRY CacheEntry1,
  1353. IN ULONG EntrySize1,
  1354. IN PLOGON_CACHE_ENTRY CacheEntry2,
  1355. IN ULONG EntrySize2
  1356. )
  1357. /*++
  1358. Routine Description:
  1359. Compare two in-memory cache entries, for the purpose of avoiding
  1360. un-necessary cache updates.
  1361. Certain fields are not taken into account during the compare,
  1362. ie: the Random encryption key.
  1363. --*/
  1364. {
  1365. LARGE_INTEGER Time1;
  1366. LARGE_INTEGER Time2;
  1367. CHAR RandomKey1[16];
  1368. CHAR RandomKey2[16];
  1369. CHAR MAC1[16];
  1370. CHAR MAC2[16];
  1371. BOOLEAN fEqual = FALSE;
  1372. if( EntrySize1 != EntrySize2 )
  1373. {
  1374. return FALSE;
  1375. }
  1376. if( CacheEntry1->Revision != CacheEntry2->Revision )
  1377. {
  1378. return FALSE;
  1379. }
  1380. //
  1381. // scoop up the current values of the 'volatile' fields,
  1382. // whack them to zero,
  1383. // do the memory compare,
  1384. // put the saved values back.
  1385. //
  1386. ASSERT(( sizeof(RandomKey1) == sizeof(CacheEntry1->RandomKey) ));
  1387. ASSERT(( sizeof(MAC1) == sizeof(CacheEntry1->MAC) ));
  1388. ASSERT(( sizeof(Time1) == sizeof(CacheEntry1->Time) ));
  1389. Time1 = CacheEntry1->Time;
  1390. Time2 = CacheEntry2->Time;
  1391. RtlZeroMemory(&CacheEntry1->Time, sizeof(CacheEntry1->Time));
  1392. RtlZeroMemory(&CacheEntry2->Time, sizeof(CacheEntry2->Time));
  1393. RtlCopyMemory(RandomKey1, CacheEntry1->RandomKey, sizeof(RandomKey1));
  1394. RtlCopyMemory(RandomKey2, CacheEntry2->RandomKey, sizeof(RandomKey2));
  1395. ZeroMemory(CacheEntry1->RandomKey, sizeof(CacheEntry1->RandomKey));
  1396. ZeroMemory(CacheEntry2->RandomKey, sizeof(CacheEntry2->RandomKey));
  1397. RtlCopyMemory(MAC1, CacheEntry1->MAC, sizeof(MAC1));
  1398. RtlCopyMemory(MAC2, CacheEntry2->MAC, sizeof(MAC2));
  1399. ZeroMemory(CacheEntry1->MAC, sizeof(CacheEntry1->MAC));
  1400. ZeroMemory(CacheEntry2->MAC, sizeof(CacheEntry2->MAC));
  1401. if( memcmp(CacheEntry1, CacheEntry2, EntrySize1) == 0 )
  1402. {
  1403. fEqual = TRUE;
  1404. }
  1405. CacheEntry1->Time = Time1;
  1406. CacheEntry2->Time = Time2;
  1407. RtlCopyMemory(CacheEntry1->RandomKey, RandomKey1, sizeof(RandomKey1));
  1408. RtlCopyMemory(CacheEntry2->RandomKey, RandomKey2, sizeof(RandomKey2));
  1409. RtlCopyMemory(CacheEntry1->MAC, MAC1, sizeof(MAC1));
  1410. RtlCopyMemory(CacheEntry2->MAC, MAC2, sizeof(MAC2));
  1411. return fEqual;
  1412. }
  1413. NTSTATUS
  1414. NlpEncryptCacheEntry(
  1415. IN PLOGON_CACHE_ENTRY CacheEntry,
  1416. IN ULONG EntrySize
  1417. )
  1418. /*++
  1419. Routine Description:
  1420. Encrypts the sensitive portions of the input CacheEntry.
  1421. --*/
  1422. {
  1423. HMACMD5_CTX hmacCtx;
  1424. RC4_KEYSTRUCT rc4key;
  1425. CHAR DerivedKey[ MD5DIGESTLEN ];
  1426. PBYTE pbData;
  1427. ULONG cbData;
  1428. if( CacheEntry->Revision < NLP_CACHE_REVISION_NT_5_0 ) {
  1429. return STATUS_SUCCESS;
  1430. }
  1431. //
  1432. // derive encryption key from global machine LSA secret, and random
  1433. // cache entry key.
  1434. //
  1435. HMACMD5Init(&hmacCtx, NlpCacheEncryptionKey, sizeof(NlpCacheEncryptionKey));
  1436. HMACMD5Update(&hmacCtx, CacheEntry->RandomKey, sizeof(CacheEntry->RandomKey));
  1437. HMACMD5Final(&hmacCtx, DerivedKey);
  1438. //
  1439. // begin encrypting at the cachepasswords field.
  1440. //
  1441. pbData = (PBYTE)&(CacheEntry->CachePasswords);
  1442. //
  1443. // data length is EntrySize - header up to CachePasswords.
  1444. //
  1445. cbData = EntrySize - (ULONG)( pbData - (PBYTE)CacheEntry );
  1446. //
  1447. // MAC the data for integrity checking.
  1448. //
  1449. HMACMD5Init(&hmacCtx, DerivedKey, sizeof(DerivedKey));
  1450. HMACMD5Update(&hmacCtx, pbData, cbData);
  1451. HMACMD5Final(&hmacCtx, CacheEntry->MAC);
  1452. //
  1453. // now encrypt it...
  1454. //
  1455. rc4_key( &rc4key, sizeof(DerivedKey), DerivedKey );
  1456. rc4( &rc4key, cbData, pbData );
  1457. ZeroMemory( DerivedKey, sizeof(DerivedKey) );
  1458. return STATUS_SUCCESS;
  1459. }
  1460. NTSTATUS
  1461. NlpDecryptCacheEntry(
  1462. IN PLOGON_CACHE_ENTRY CacheEntry,
  1463. IN ULONG EntrySize
  1464. )
  1465. /*++
  1466. Routine Description:
  1467. Decrypts the sensitive portions of the input CacheEntry, and verified
  1468. integrity of decrypted data.
  1469. --*/
  1470. {
  1471. HMACMD5_CTX hmacCtx;
  1472. RC4_KEYSTRUCT rc4key;
  1473. CHAR DerivedKey[ MD5DIGESTLEN ];
  1474. CHAR MAC[ MD5DIGESTLEN ];
  1475. PBYTE pbData;
  1476. ULONG cbData;
  1477. if( CacheEntry->Revision < NLP_CACHE_REVISION_NT_5_0 ) {
  1478. return STATUS_SUCCESS;
  1479. }
  1480. //
  1481. // derive encryption key from global machine LSA secret, and random
  1482. // cache entry key.
  1483. //
  1484. HMACMD5Init(&hmacCtx, NlpCacheEncryptionKey, sizeof(NlpCacheEncryptionKey));
  1485. HMACMD5Update(&hmacCtx, CacheEntry->RandomKey, sizeof(CacheEntry->RandomKey));
  1486. HMACMD5Final(&hmacCtx, DerivedKey);
  1487. //
  1488. // begin decrypting at the cachepasswords field.
  1489. //
  1490. pbData = (PBYTE)&(CacheEntry->CachePasswords);
  1491. //
  1492. // data length is EntrySize - header up to CachePasswords.
  1493. //
  1494. cbData = EntrySize - (ULONG)( pbData - (PBYTE)CacheEntry );
  1495. //
  1496. // now decrypt it...
  1497. //
  1498. rc4_key( &rc4key, sizeof(DerivedKey), DerivedKey );
  1499. rc4( &rc4key, cbData, pbData );
  1500. //
  1501. // compute MAC on decrypted data for integrity checking.
  1502. //
  1503. HMACMD5Init(&hmacCtx, DerivedKey, sizeof(DerivedKey));
  1504. HMACMD5Update(&hmacCtx, pbData, cbData);
  1505. HMACMD5Final(&hmacCtx, MAC);
  1506. ZeroMemory( DerivedKey, sizeof(DerivedKey) );
  1507. //
  1508. // verify MAC.
  1509. //
  1510. if( memcmp( MAC, CacheEntry->MAC, sizeof(MAC) ) != 0 ) {
  1511. return STATUS_LOGON_FAILURE;
  1512. }
  1513. return STATUS_SUCCESS;
  1514. }
  1515. NTSTATUS
  1516. NlpBuildCacheEntry(
  1517. IN PNETLOGON_INTERACTIVE_INFO LogonInfo,
  1518. IN PNETLOGON_VALIDATION_SAM_INFO4 AccountInfo,
  1519. IN ULONG CacheFlags,
  1520. OUT PLOGON_CACHE_ENTRY* ppCacheEntry,
  1521. OUT PULONG pEntryLength
  1522. )
  1523. /*++
  1524. Routine Description:
  1525. Builds a LOGON_CACHE_ENTRY from a NETLOGON_VALIDATION_SAM_INFO4 structure.
  1526. We only cache those fields that we cannot easily re-invent
  1527. Arguments:
  1528. LogonInfo - pointer to NETLOGON_INTERACTIVE_INFO structure containing
  1529. user's name and logon domain name
  1530. AccountInfo - pointer to NETLOGON_VALIDATION_SAM_INFO4 from successful
  1531. logon
  1532. ppCacheEntry - pointer to place to return pointer to allocated
  1533. LOGON_CACHE_ENTRY
  1534. pEntryLength - size of the buffer returned in *ppCacheEntry
  1535. Return Value:
  1536. NTSTATUS
  1537. Success = STATUS_SUCCESS
  1538. *ppCacheEntry contains pointer to allocated LOGON_CACHE_ENTRY
  1539. structure
  1540. Failure = STATUS_NO_MEMORY
  1541. *ppCacheEntry undefined
  1542. --*/
  1543. {
  1544. PLOGON_CACHE_ENTRY pEntry;
  1545. ULONG length;
  1546. PCHAR dataptr;
  1547. UNICODE_STRING SamAccountName;
  1548. UNICODE_STRING NetbiosDomainName;
  1549. UNICODE_STRING DnsDomainName;
  1550. UNICODE_STRING Upn;
  1551. NTSTATUS NtStatus;
  1552. //
  1553. // Grab the various forms of the account name
  1554. //
  1555. NlpGetAccountNames( &LogonInfo->Identity,
  1556. AccountInfo,
  1557. &SamAccountName,
  1558. &NetbiosDomainName,
  1559. &DnsDomainName,
  1560. &Upn );
  1561. //
  1562. // assumes GROUP_MEMBERSHIP is integral multiple of DWORDs
  1563. //
  1564. length = ROUND_UP_COUNT(sizeof(LOGON_CACHE_ENTRY), sizeof(ULONG))
  1565. + ROUND_UP_COUNT(NetbiosDomainName.Length, sizeof(ULONG))
  1566. + ROUND_UP_COUNT(SamAccountName.Length, sizeof(ULONG))
  1567. + ROUND_UP_COUNT(DnsDomainName.Length, sizeof(ULONG))
  1568. + ROUND_UP_COUNT(Upn.Length, sizeof(ULONG))
  1569. + ROUND_UP_COUNT(AccountInfo->EffectiveName.Length, sizeof(ULONG))
  1570. + ROUND_UP_COUNT(AccountInfo->FullName.Length, sizeof(ULONG))
  1571. + ROUND_UP_COUNT(AccountInfo->LogonScript.Length, sizeof(ULONG))
  1572. + ROUND_UP_COUNT(AccountInfo->ProfilePath.Length, sizeof(ULONG))
  1573. + ROUND_UP_COUNT(AccountInfo->HomeDirectory.Length, sizeof(ULONG))
  1574. + ROUND_UP_COUNT(AccountInfo->HomeDirectoryDrive.Length, sizeof(ULONG))
  1575. + ROUND_UP_COUNT(AccountInfo->LogonDomainName.Length, sizeof(ULONG))
  1576. + ROUND_UP_COUNT(AccountInfo->GroupCount * sizeof(GROUP_MEMBERSHIP), sizeof(ULONG))
  1577. + ROUND_UP_COUNT(RtlLengthSid(AccountInfo->LogonDomainId), sizeof(ULONG))
  1578. + ROUND_UP_COUNT(AccountInfo->LogonServer.Length, sizeof(ULONG));
  1579. if (AccountInfo->UserFlags & LOGON_EXTRA_SIDS) {
  1580. if (AccountInfo->SidCount) {
  1581. ULONG i;
  1582. length += ROUND_UP_COUNT(AccountInfo->SidCount * sizeof(ULONG), sizeof(ULONG));
  1583. for (i = 0; i < AccountInfo->SidCount ; i++ ) {
  1584. length += ROUND_UP_COUNT(RtlLengthSid(AccountInfo->ExtraSids[i].Sid), sizeof(ULONG));
  1585. }
  1586. }
  1587. }
  1588. pEntry = AllocateCacheEntry(length);
  1589. if (pEntry == NULL) {
  1590. return STATUS_NO_MEMORY;
  1591. }
  1592. RtlZeroMemory( pEntry, length );
  1593. pEntry->Revision = NLP_CACHE_REVISION;
  1594. NtQuerySystemTime( &pEntry->Time );
  1595. pEntry->Valid = TRUE;
  1596. pEntry->LogonPackage = LogonInfo->Identity.ParameterControl;
  1597. dataptr = (PCHAR)(pEntry + 1);
  1598. *pEntryLength = length;
  1599. ASSERT(!((ULONG_PTR)dataptr & (sizeof(ULONG) - 1)));
  1600. //
  1601. // each of these (unicode) strings and other structures are copied to the
  1602. // end of the fixed LOGON_CACHE_ENTRY structure, each aligned on DWORD
  1603. // boundaries
  1604. //
  1605. length = pEntry->UserNameLength = SamAccountName.Length;
  1606. RtlCopyMemory(dataptr, SamAccountName.Buffer, length);
  1607. dataptr = ROUND_UP_POINTER(dataptr+length, sizeof(ULONG));
  1608. length = pEntry->DomainNameLength = NetbiosDomainName.Length;
  1609. if (length) {
  1610. RtlCopyMemory(dataptr, NetbiosDomainName.Buffer, length);
  1611. dataptr = ROUND_UP_POINTER(dataptr+length, sizeof(ULONG));
  1612. }
  1613. length = pEntry->DnsDomainNameLength = DnsDomainName.Length;
  1614. if (length) {
  1615. RtlCopyMemory(dataptr, DnsDomainName.Buffer, length);
  1616. dataptr = ROUND_UP_POINTER(dataptr+length, sizeof(ULONG));
  1617. }
  1618. length = pEntry->UpnLength = Upn.Length;
  1619. if (length) {
  1620. RtlCopyMemory(dataptr, Upn.Buffer, length);
  1621. dataptr = ROUND_UP_POINTER(dataptr+length, sizeof(ULONG));
  1622. }
  1623. length = pEntry->EffectiveNameLength = AccountInfo->EffectiveName.Length;
  1624. if (length) {
  1625. RtlCopyMemory(dataptr, AccountInfo->EffectiveName.Buffer, length);
  1626. dataptr = ROUND_UP_POINTER(dataptr+length, sizeof(ULONG));
  1627. }
  1628. length = pEntry->FullNameLength = AccountInfo->FullName.Length;
  1629. if (length) {
  1630. RtlCopyMemory(dataptr, AccountInfo->FullName.Buffer, length);
  1631. dataptr = ROUND_UP_POINTER(dataptr+length, sizeof(ULONG));
  1632. }
  1633. length = pEntry->LogonScriptLength = AccountInfo->LogonScript.Length;
  1634. if (length) {
  1635. RtlCopyMemory(dataptr, AccountInfo->LogonScript.Buffer, length);
  1636. dataptr = ROUND_UP_POINTER(dataptr+length, sizeof(ULONG));
  1637. }
  1638. length = pEntry->ProfilePathLength = AccountInfo->ProfilePath.Length;
  1639. if (length) {
  1640. RtlCopyMemory(dataptr, AccountInfo->ProfilePath.Buffer, length);
  1641. dataptr = ROUND_UP_POINTER(dataptr+length, sizeof(ULONG));
  1642. }
  1643. length = pEntry->HomeDirectoryLength = AccountInfo->HomeDirectory.Length;
  1644. if (length) {
  1645. RtlCopyMemory(dataptr, AccountInfo->HomeDirectory.Buffer, length);
  1646. dataptr = ROUND_UP_POINTER(dataptr+length, sizeof(ULONG));
  1647. }
  1648. length = pEntry->HomeDirectoryDriveLength = AccountInfo->HomeDirectoryDrive.Length;
  1649. if (length) {
  1650. RtlCopyMemory(dataptr, AccountInfo->HomeDirectoryDrive.Buffer, length);
  1651. dataptr = ROUND_UP_POINTER(dataptr+length, sizeof(ULONG));
  1652. }
  1653. pEntry->UserId = AccountInfo->UserId;
  1654. pEntry->PrimaryGroupId = AccountInfo->PrimaryGroupId;
  1655. length = pEntry->GroupCount = AccountInfo->GroupCount;
  1656. length *= sizeof(GROUP_MEMBERSHIP);
  1657. if (length) {
  1658. RtlCopyMemory(dataptr, AccountInfo->GroupIds, length);
  1659. dataptr = ROUND_UP_POINTER(dataptr+length, sizeof(ULONG));
  1660. }
  1661. length = pEntry->LogonDomainNameLength = AccountInfo->LogonDomainName.Length;
  1662. if (length) {
  1663. RtlCopyMemory(dataptr, AccountInfo->LogonDomainName.Buffer, length);
  1664. dataptr = ROUND_UP_POINTER(dataptr+length, sizeof(ULONG));
  1665. }
  1666. if (AccountInfo->UserFlags & LOGON_EXTRA_SIDS) {
  1667. length = pEntry->SidCount = AccountInfo->SidCount;
  1668. length *= sizeof(ULONG);
  1669. if (length) {
  1670. ULONG i, sidLength;
  1671. PULONG sidAttributes = (PULONG) dataptr;
  1672. dataptr = ROUND_UP_POINTER(dataptr+length, sizeof(ULONG));
  1673. //
  1674. // Now copy over all the SIDs
  1675. //
  1676. for (i = 0; i < AccountInfo->SidCount ; i++ ) {
  1677. sidAttributes[i] = AccountInfo->ExtraSids[i].Attributes;
  1678. sidLength = RtlLengthSid(AccountInfo->ExtraSids[i].Sid);
  1679. RtlCopySid(sidLength,(PSID) dataptr,AccountInfo->ExtraSids[i].Sid);
  1680. dataptr = ROUND_UP_POINTER(dataptr + sidLength, sizeof(ULONG));
  1681. }
  1682. pEntry->SidLength = (ULONG) (dataptr - (PCHAR) sidAttributes);
  1683. } else {
  1684. pEntry->SidLength = 0;
  1685. }
  1686. } else {
  1687. pEntry->SidCount = 0;
  1688. pEntry->SidLength = 0;
  1689. }
  1690. length = pEntry->LogonDomainIdLength = (USHORT) RtlLengthSid(AccountInfo->LogonDomainId);
  1691. NtStatus = RtlCopySid(pEntry->LogonDomainIdLength,
  1692. (PSID)dataptr,
  1693. AccountInfo->LogonDomainId
  1694. );
  1695. ASSERT(NT_SUCCESS(NtStatus));
  1696. dataptr = ROUND_UP_POINTER(dataptr+length, sizeof(ULONG));
  1697. //
  1698. // copy in the LogonServer
  1699. //
  1700. length = pEntry->LogonServerLength = AccountInfo->LogonServer.Length;
  1701. if (length) {
  1702. RtlCopyMemory(dataptr, AccountInfo->LogonServer.Buffer, length);
  1703. dataptr = ROUND_UP_POINTER(dataptr+length, sizeof(ULONG));
  1704. }
  1705. //
  1706. // fill in randomkey for this cache entry.
  1707. //
  1708. SspGenerateRandomBits( pEntry->RandomKey, sizeof(pEntry->RandomKey) );
  1709. pEntry->CacheFlags = CacheFlags;
  1710. *ppCacheEntry = pEntry;
  1711. #if DBG
  1712. if (DumpCacheInfo) {
  1713. DbgPrint("BuildCacheEntry:\n");
  1714. DumpCacheEntry(999,pEntry);
  1715. }
  1716. #endif
  1717. return STATUS_SUCCESS;
  1718. }
  1719. NTSTATUS
  1720. NlpOpenCache( VOID )
  1721. /*++
  1722. Routine Description:
  1723. Opens the registry node for read or write (depending on Switch) and opens
  1724. the secret storage in the same mode. If successful, the NlpCacheHandle
  1725. is valid.
  1726. Arguments:
  1727. Return Value:
  1728. NTSTATUS
  1729. Success = STATUS_SUCCESS
  1730. NlpCacheHandle contains handle to use for reading/writing
  1731. registry
  1732. Failure =
  1733. --*/
  1734. {
  1735. NTSTATUS NtStatus;
  1736. ULONG Disposition;
  1737. OBJECT_ATTRIBUTES ObjectAttributes;
  1738. UNICODE_STRING ObjectName;
  1739. ObjectName.Length = ObjectName.MaximumLength = CACHE_NAME_SIZE;
  1740. ObjectName.Buffer = CACHE_NAME;
  1741. InitializeObjectAttributes(&ObjectAttributes,
  1742. &ObjectName,
  1743. OBJ_CASE_INSENSITIVE,
  1744. 0, // RootDirectory
  1745. NULL // default is reasonable from SYSTEM context
  1746. );
  1747. NtStatus = NtCreateKey(&NlpCacheHandle,
  1748. (KEY_WRITE | KEY_READ),
  1749. &ObjectAttributes,
  1750. CACHE_TITLE_INDEX,
  1751. NULL, // class name
  1752. 0, // create options
  1753. &Disposition
  1754. );
  1755. return NtStatus;
  1756. }
  1757. VOID
  1758. NlpCloseCache( VOID )
  1759. /*++
  1760. Routine Description:
  1761. Closes handles opened by NlpOpenCache
  1762. Arguments:
  1763. None.
  1764. Return Value:
  1765. None.
  1766. --*/
  1767. {
  1768. #if DBG
  1769. NTSTATUS NtStatus;
  1770. if (DumpCacheInfo) {
  1771. DbgPrint("CloseCache: Closing NlpCacheHandle (%#08x)\n", NlpCacheHandle);
  1772. }
  1773. if (IS_VALID_HANDLE(NlpCacheHandle)) {
  1774. NtStatus = NtClose(NlpCacheHandle);
  1775. if (DumpCacheInfo) {
  1776. DbgPrint("CloseCache: NtClose returns %#08x\n", NtStatus);
  1777. }
  1778. ASSERT(NT_SUCCESS(NtStatus));
  1779. INVALIDATE_HANDLE(NlpCacheHandle);
  1780. }
  1781. #else
  1782. if (IS_VALID_HANDLE(NlpCacheHandle)) {
  1783. NtClose(NlpCacheHandle);
  1784. INVALIDATE_HANDLE(NlpCacheHandle);
  1785. }
  1786. #endif
  1787. }
  1788. NTSTATUS
  1789. NlpOpenSecret(
  1790. IN ULONG Index
  1791. )
  1792. /*++
  1793. Routine Description:
  1794. Opens a cache entry's secret storage object for read (in order to LsaQuerySecret) and
  1795. write (in order to LsaSetSecret). If successful, the handle value
  1796. is placed in the global variable NlpSecretHandle.
  1797. If the secret does not exist, it will be created.
  1798. Arguments:
  1799. Index - The index of the entry being opened. This is used to build
  1800. a name of the object.
  1801. Return Value:
  1802. NTSTATUS
  1803. Success = STATUS_SUCCESS
  1804. NlpSecretHandle can be used to read/write secret storage
  1805. Failure =
  1806. --*/
  1807. {
  1808. NTSTATUS
  1809. NtStatus;
  1810. UNICODE_STRING
  1811. ValueName;
  1812. WCHAR
  1813. NameBuffer[32];
  1814. //
  1815. // Close previous handle if necessary
  1816. //
  1817. if (IS_VALID_HANDLE(NlpSecretHandle)) {
  1818. I_LsarClose( &NlpSecretHandle );
  1819. }
  1820. ValueName.Buffer = &NameBuffer[0];
  1821. ValueName.MaximumLength = 32;
  1822. ValueName.Length = 0;
  1823. NlpMakeCacheEntryName( Index, &ValueName );
  1824. NtStatus = I_LsarOpenSecret(NtLmGlobalPolicyHandle,
  1825. (PLSAPR_UNICODE_STRING) &ValueName,
  1826. SECRET_QUERY_VALUE | SECRET_SET_VALUE,
  1827. &NlpSecretHandle
  1828. );
  1829. if (!NT_SUCCESS(NtStatus)) {
  1830. if (NtStatus == STATUS_OBJECT_NAME_NOT_FOUND) {
  1831. NtStatus = I_LsarCreateSecret(NtLmGlobalPolicyHandle,
  1832. (PLSAPR_UNICODE_STRING) &ValueName,
  1833. SECRET_SET_VALUE | SECRET_QUERY_VALUE,
  1834. &NlpSecretHandle
  1835. );
  1836. if (!NT_SUCCESS(NtStatus)) {
  1837. INVALIDATE_HANDLE(NlpSecretHandle);
  1838. }
  1839. } else {
  1840. INVALIDATE_HANDLE(NlpSecretHandle);
  1841. }
  1842. }
  1843. return(NtStatus);
  1844. }
  1845. VOID
  1846. NlpCloseSecret( VOID )
  1847. /*++
  1848. Routine Description:
  1849. Closes the handles opened via NlpOpenSecret
  1850. Arguments:
  1851. None.
  1852. Return Value:
  1853. None.
  1854. --*/
  1855. {
  1856. NTSTATUS
  1857. NtStatus;
  1858. if (IS_VALID_HANDLE(NlpSecretHandle)) {
  1859. NtStatus = I_LsarClose(&NlpSecretHandle);
  1860. #if DBG
  1861. if (DumpCacheInfo) {
  1862. DbgPrint("CloseSecret: LsaClose returns %#08x\n", NtStatus);
  1863. }
  1864. #endif
  1865. ASSERT(NT_SUCCESS(NtStatus));
  1866. INVALIDATE_HANDLE(NlpSecretHandle);
  1867. }
  1868. }
  1869. NTSTATUS
  1870. NlpWriteSecret(
  1871. IN PLSAPR_CR_CIPHER_VALUE NewSecret,
  1872. IN PLSAPR_CR_CIPHER_VALUE OldSecret
  1873. )
  1874. /*++
  1875. Routine Description:
  1876. Writes the password (and optionally the previous password) to the LSA
  1877. secret storage
  1878. Arguments:
  1879. NewSecret - pointer to UNICODE_STRING containing current password
  1880. OldSecret - pointer to UNICODE_STRING containing previous password
  1881. Return Value:
  1882. NTSTATUS
  1883. Success =
  1884. Failure =
  1885. --*/
  1886. {
  1887. return I_LsarSetSecret(NlpSecretHandle, NewSecret, OldSecret);
  1888. }
  1889. NTSTATUS
  1890. NlpReadSecret(
  1891. OUT PLSAPR_CR_CIPHER_VALUE * NewSecret,
  1892. OUT PLSAPR_CR_CIPHER_VALUE * OldSecret
  1893. )
  1894. /*++
  1895. Routine Description:
  1896. Reads the new and old secrets (UNICODE_STRINGs) for the
  1897. currently open LSA secret
  1898. The Lsa routine returns us pointers to UNICODE strings
  1899. Arguments:
  1900. NewSecret - pointer to returned pointer to UNICODE_STRING containing
  1901. most recent password (if any)
  1902. OldSecret - pointer to returned pointer to UNICODE_STRING containing
  1903. previous password (if any)
  1904. Return Value:
  1905. NTSTATUS
  1906. Success
  1907. Failure
  1908. --*/
  1909. {
  1910. NTSTATUS
  1911. NtStatus;
  1912. LARGE_INTEGER
  1913. NewTime,
  1914. OldTime;
  1915. NtStatus = I_LsarQuerySecret(NlpSecretHandle,
  1916. NewSecret,
  1917. &NewTime,
  1918. OldSecret,
  1919. &OldTime
  1920. );
  1921. #if DBG
  1922. {
  1923. char newNt[80];
  1924. char newLm[80];
  1925. char oldNt[80];
  1926. char oldLm[80];
  1927. if (DumpCacheInfo) {
  1928. DbgPrint("NlpReadSecret: NewSecret.Nt = \"%s\"\n"
  1929. " NewSecret.Lm = \"%s\"\n"
  1930. " OldSecret.Nt = \"%s\"\n"
  1931. " OldSecret.Lm = \"%s\"\n",
  1932. *NewSecret
  1933. ? DumpOwfPasswordToString(newNt, (PLM_OWF_PASSWORD)((*NewSecret)->Buffer))
  1934. : "",
  1935. *NewSecret
  1936. ? DumpOwfPasswordToString(newLm, (PLM_OWF_PASSWORD)((*NewSecret)->Buffer)+1)
  1937. : "",
  1938. *OldSecret
  1939. ? DumpOwfPasswordToString(oldNt, (PLM_OWF_PASSWORD)((*OldSecret)->Buffer))
  1940. : "",
  1941. *OldSecret
  1942. ? DumpOwfPasswordToString(oldLm, (PLM_OWF_PASSWORD)((*OldSecret)->Buffer)+1)
  1943. : ""
  1944. );
  1945. }
  1946. }
  1947. #endif
  1948. return NtStatus;
  1949. }
  1950. NTSTATUS
  1951. NlpComputeSaltedHashedPassword(
  1952. OUT PNT_OWF_PASSWORD SaltedOwfPassword,
  1953. IN PNT_OWF_PASSWORD OwfPassword,
  1954. IN PUNICODE_STRING UserName
  1955. )
  1956. /*++
  1957. Routine Description:
  1958. Computes the salted hash of a password by concatenating the user name
  1959. with the OWF and computing the OWF of the combination.
  1960. Arguments:
  1961. SaltedOwfPassword - receives the LM or NT salted password/
  1962. OwfPassword - Contains the NT or LM owf password.
  1963. UserName - Contains the name of the user, used for salt.
  1964. Return Value:
  1965. NTSTATUS
  1966. Success = STATUS_SUCCESS
  1967. Passwords created OK
  1968. Failure = STATUS_NO_MEMORY
  1969. Not enough storage to create Passwords
  1970. --*/
  1971. {
  1972. NTSTATUS Status;
  1973. UNICODE_STRING TempString;
  1974. UNICODE_STRING LowerUserName;
  1975. //
  1976. // Compute the lower case user name.
  1977. //
  1978. Status = RtlDowncaseUnicodeString( &LowerUserName,
  1979. UserName,
  1980. TRUE );
  1981. if ( !NT_SUCCESS(Status)) {
  1982. return Status;
  1983. }
  1984. //
  1985. // Build a string that is a concatenation of the OWF and LowerCase username.
  1986. //
  1987. TempString.Length = TempString.MaximumLength = LowerUserName.Length + sizeof(NT_OWF_PASSWORD);
  1988. TempString.Buffer = AllocateFromHeap( TempString.Length );
  1989. if (TempString.Buffer == NULL) {
  1990. RtlFreeUnicodeString( &LowerUserName );
  1991. return(STATUS_INSUFFICIENT_RESOURCES);
  1992. }
  1993. RtlCopyMemory(
  1994. TempString.Buffer,
  1995. OwfPassword,
  1996. sizeof(NT_OWF_PASSWORD) );
  1997. RtlCopyMemory(
  1998. (PUCHAR) TempString.Buffer + sizeof(NT_OWF_PASSWORD),
  1999. LowerUserName.Buffer,
  2000. LowerUserName.Length );
  2001. //
  2002. // The Salted hash is the OWF of that.
  2003. //
  2004. Status = RtlCalculateNtOwfPassword(
  2005. &TempString,
  2006. SaltedOwfPassword
  2007. );
  2008. FreeToHeap(TempString.Buffer);
  2009. RtlFreeUnicodeString( &LowerUserName );
  2010. return(Status);
  2011. }
  2012. NTSTATUS
  2013. NlpMakeSecretPassword(
  2014. OUT PLSAPR_CR_CIPHER_VALUE Passwords,
  2015. IN PUNICODE_STRING UserName,
  2016. IN PNT_OWF_PASSWORD NtOwfPassword OPTIONAL,
  2017. IN PLM_OWF_PASSWORD LmOwfPassword OPTIONAL
  2018. )
  2019. /*++
  2020. Routine Description:
  2021. Converts a (fixed length structure) NT_OWF_PASSWORD and a LM_OWF_PASSWORD
  2022. to a UNICODE_STRING. Allocates memory for the unicode string in this function
  2023. The calling function must free up the string buffer allocated in this routine.
  2024. The caller uses FreeToHeap (RtlFreeHeap)
  2025. Arguments:
  2026. Passwords - returned UNICODE_STRING which actually contains a
  2027. CACHE_PASSWORDS structure
  2028. NtOwfPassword - pointer to encrypted, fixed-length NT password
  2029. LmOwfPassword - pointer to encrypted, fixed-length LM password
  2030. Return Value:
  2031. NTSTATUS
  2032. Success = STATUS_SUCCESS
  2033. Passwords created OK
  2034. Failure = STATUS_NO_MEMORY
  2035. Not enough storage to create Passwords
  2036. --*/
  2037. {
  2038. NTSTATUS Status = STATUS_SUCCESS;
  2039. PCACHE_PASSWORDS pwd;
  2040. Passwords->Buffer = NULL;
  2041. pwd = (PCACHE_PASSWORDS)AllocateFromHeap(sizeof(*pwd));
  2042. if (pwd == NULL) {
  2043. return STATUS_NO_MEMORY;
  2044. }
  2045. //
  2046. // concatenate the fixed length NT_OWF_PASSWORD and LM_OWF_PASSWORD structures
  2047. // into a buffer which we then use as a UNICODE_STRING
  2048. //
  2049. if (ARGUMENT_PRESENT(NtOwfPassword)) {
  2050. Status = NlpComputeSaltedHashedPassword(
  2051. &pwd->SecretPasswords.NtOwfPassword,
  2052. NtOwfPassword,
  2053. UserName
  2054. );
  2055. if (!NT_SUCCESS(Status)) {
  2056. goto Cleanup;
  2057. }
  2058. pwd->SecretPasswords.NtPasswordPresent = TRUE;
  2059. } else {
  2060. RtlZeroMemory((PVOID)&pwd->SecretPasswords.NtOwfPassword,
  2061. sizeof(pwd->SecretPasswords.NtOwfPassword)
  2062. );
  2063. pwd->SecretPasswords.NtPasswordPresent = FALSE;
  2064. }
  2065. if (ARGUMENT_PRESENT(LmOwfPassword)) {
  2066. Status = NlpComputeSaltedHashedPassword(
  2067. &pwd->SecretPasswords.LmOwfPassword,
  2068. LmOwfPassword,
  2069. UserName
  2070. );
  2071. if (!NT_SUCCESS(Status)) {
  2072. goto Cleanup;
  2073. }
  2074. pwd->SecretPasswords.LmPasswordPresent = TRUE;
  2075. } else {
  2076. RtlZeroMemory((PVOID)&pwd->SecretPasswords.LmOwfPassword,
  2077. sizeof(pwd->SecretPasswords.LmOwfPassword)
  2078. );
  2079. pwd->SecretPasswords.LmPasswordPresent = FALSE;
  2080. }
  2081. Passwords->Length = Passwords->MaximumLength = sizeof(*pwd);
  2082. Passwords->Buffer = (PUCHAR)pwd;
  2083. Cleanup:
  2084. if( !NT_SUCCESS( Status ) ) {
  2085. if( pwd != NULL )
  2086. FreeToHeap( pwd );
  2087. }
  2088. return Status;
  2089. }
  2090. NTSTATUS
  2091. NlpMakeSecretPasswordNT5(
  2092. IN OUT PCACHE_PASSWORDS Passwords,
  2093. IN PUNICODE_STRING UserName,
  2094. IN PNT_OWF_PASSWORD NtOwfPassword OPTIONAL,
  2095. IN PLM_OWF_PASSWORD LmOwfPassword OPTIONAL
  2096. )
  2097. /*++
  2098. Routine Description:
  2099. Populates CACHE_PASSWORDS structure with salted forms of NtOwfPassword
  2100. and LmOwfPassword.
  2101. Arguments:
  2102. Passwords - populated CACHE_PASSWORDS structure.
  2103. NtOwfPassword - pointer to encrypted, fixed-length NT password
  2104. LmOwfPassword - pointer to encrypted, fixed-length LM password
  2105. Return Value:
  2106. NTSTATUS
  2107. Success = STATUS_SUCCESS
  2108. Passwords created OK
  2109. Failure = STATUS_NO_MEMORY
  2110. Not enough storage to create Passwords
  2111. --*/
  2112. {
  2113. NTSTATUS Status = STATUS_SUCCESS;
  2114. PCACHE_PASSWORDS pwd;
  2115. pwd = Passwords;
  2116. //
  2117. // concatenate the fixed length NT_OWF_PASSWORD and LM_OWF_PASSWORD structures
  2118. // into a buffer which we then use as a UNICODE_STRING
  2119. //
  2120. if (ARGUMENT_PRESENT(NtOwfPassword)) {
  2121. Status = NlpComputeSaltedHashedPassword(
  2122. &pwd->SecretPasswords.NtOwfPassword,
  2123. NtOwfPassword,
  2124. UserName
  2125. );
  2126. if (!NT_SUCCESS(Status)) {
  2127. goto Cleanup;
  2128. }
  2129. pwd->SecretPasswords.NtPasswordPresent = TRUE;
  2130. } else {
  2131. RtlZeroMemory((PVOID)&pwd->SecretPasswords.NtOwfPassword,
  2132. sizeof(pwd->SecretPasswords.NtOwfPassword)
  2133. );
  2134. pwd->SecretPasswords.NtPasswordPresent = FALSE;
  2135. }
  2136. //
  2137. // Windows2000:
  2138. // never store LMOWF -- since we never need it, and, this would
  2139. // be the first thing attacked once a cache entry is unwrapped.
  2140. //
  2141. #if 0
  2142. if (ARGUMENT_PRESENT(LmOwfPassword)) {
  2143. Status = NlpComputeSaltedHashedPassword(
  2144. &pwd->SecretPasswords.LmOwfPassword,
  2145. LmOwfPassword,
  2146. UserName
  2147. );
  2148. if (!NT_SUCCESS(Status)) {
  2149. goto Cleanup;
  2150. }
  2151. pwd->SecretPasswords.LmPasswordPresent = TRUE;
  2152. } else
  2153. #else
  2154. UNREFERENCED_PARAMETER( LmOwfPassword );
  2155. #endif
  2156. {
  2157. RtlZeroMemory((PVOID)&pwd->SecretPasswords.LmOwfPassword,
  2158. sizeof(pwd->SecretPasswords.LmOwfPassword)
  2159. );
  2160. pwd->SecretPasswords.LmPasswordPresent = FALSE;
  2161. }
  2162. Cleanup:
  2163. return Status;
  2164. }
  2165. BOOLEAN
  2166. NlpCheckMitCacheEntry(
  2167. IN PLOGON_CACHE_ENTRY CacheEntry,
  2168. IN PUNICODE_STRING DomainName,
  2169. IN PUNICODE_STRING UserName
  2170. )
  2171. /*++
  2172. Routine Description:
  2173. Uses the supplemental data found in a cached MIT logon to
  2174. determine whether this is the correct entry
  2175. Arguments:
  2176. DomainName - The name of the domain in which the account exists.
  2177. This can be the Netbios or Dns Domain Name.
  2178. UserName - The name of the account whose password is to be changed.
  2179. This can be the Sam Account Name.
  2180. If DomainName is empty, this is the UPN of the account
  2181. Index - receives the index of the entry retrieved.
  2182. CacheEntry - pointer to place to return pointer to LOGON_CACHE_ENTRY
  2183. EntrySize - size of returned LOGON_CACHE_ENTRY
  2184. Return Value:
  2185. BOOLEAN
  2186. TRUE - The user is doing a cached MIT logon, and this is the correct
  2187. entry.
  2188. FALSE - Doh! Not it.
  2189. --*/
  2190. {
  2191. UNICODE_STRING MitRealm;
  2192. UNICODE_STRING MitUser;
  2193. PBYTE Tmp, Start;
  2194. if (CacheEntry->SupplementalCacheDataLength < (2 * sizeof(UNICODE_STRING)) ||
  2195. CacheEntry->SupplementalCacheDataOffset < sizeof(LOGON_CACHE_ENTRY))
  2196. {
  2197. return FALSE;
  2198. }
  2199. // Supplemental data will contain 2 UNICODE_STRINGs & buffers, in format
  2200. // MIT User <buffer> MIT Realm <buffer>. All buffers are offset from
  2201. // beginning of supplemental data.
  2202. Tmp = Start = (PBYTE) RtlOffsetToPointer(CacheEntry, CacheEntry->SupplementalCacheDataOffset);
  2203. RtlCopyMemory(
  2204. &MitUser,
  2205. Tmp,
  2206. sizeof(UNICODE_STRING)
  2207. );
  2208. Tmp += sizeof(UNICODE_STRING);
  2209. MitUser.Buffer = (PWSTR) RtlOffsetToPointer(Start, MitUser.Buffer);
  2210. Tmp += ROUND_UP_COUNT(MitUser.Length, ALIGN_LONG);
  2211. RtlCopyMemory(
  2212. &MitRealm,
  2213. Tmp,
  2214. sizeof(UNICODE_STRING)
  2215. );
  2216. MitRealm.Buffer = (PWSTR) RtlOffsetToPointer(Start, MitRealm.Buffer);
  2217. // This must be a UPN
  2218. if (DomainName->Length == 0)
  2219. {
  2220. // obviously, it isn't this cache entry.
  2221. if (MitRealm.Length != 0)
  2222. {
  2223. return FALSE;
  2224. }
  2225. return (RtlEqualUnicodeString(
  2226. &MitUser,
  2227. UserName,
  2228. TRUE
  2229. ));
  2230. }
  2231. return (RtlEqualUnicodeString(
  2232. &MitUser,
  2233. UserName,
  2234. TRUE
  2235. ) &&
  2236. RtlEqualUnicodeString(
  2237. &MitRealm,
  2238. DomainName,
  2239. TRUE
  2240. ));
  2241. }
  2242. NTSTATUS
  2243. NlpReadCacheEntry(
  2244. IN PUNICODE_STRING DomainName,
  2245. IN PUNICODE_STRING UserName,
  2246. OUT PULONG Index,
  2247. OUT PLOGON_CACHE_ENTRY* CacheEntry,
  2248. OUT PULONG EntrySize
  2249. )
  2250. /*++
  2251. Routine Description:
  2252. Searches the active entry list for a domain\username
  2253. match in the cache. If a match is found, then it
  2254. is returned.
  2255. Arguments:
  2256. DomainName - The name of the domain in which the account exists.
  2257. This can be the Netbios or Dns Domain Name.
  2258. UserName - The name of the account whose password is to be changed.
  2259. This can be the Sam Account Name.
  2260. If DomainName is empty, this is the UPN of the account
  2261. Index - receives the index of the entry retrieved.
  2262. CacheEntry - pointer to place to return pointer to LOGON_CACHE_ENTRY
  2263. EntrySize - size of returned LOGON_CACHE_ENTRY
  2264. Return Value:
  2265. NTSTATUS
  2266. Success = STATUS_SUCCESS
  2267. *ppEntry points to allocated LOGON_CACHE_ENTRY
  2268. *EntrySize is size of returned data
  2269. Failure = STATUS_NO_MEMORY
  2270. Couldn't allocate buffer for LOGON_CACHE_ENTRY
  2271. --*/
  2272. {
  2273. NTSTATUS NtStatus = STATUS_SUCCESS;
  2274. UNICODE_STRING CachedUser;
  2275. UNICODE_STRING CachedDomain;
  2276. UNICODE_STRING CachedDnsDomain;
  2277. UNICODE_STRING CachedUpn;
  2278. PNLP_CTE
  2279. Next;
  2280. //
  2281. // Walk the active list looking for a domain/name match
  2282. //
  2283. Next = (PNLP_CTE)NlpActiveCtes.Flink;
  2284. while (Next != (PNLP_CTE)&NlpActiveCtes) {
  2285. NtStatus = NlpReadCacheEntryByIndex( Next->Index,
  2286. CacheEntry,
  2287. EntrySize
  2288. );
  2289. if (!NT_SUCCESS(NtStatus)) {
  2290. break; // out of while-loop
  2291. }
  2292. //
  2293. // Grab the various strings from the cache entry.
  2294. //
  2295. ASSERT((*CacheEntry)->Revision >= NLP_CACHE_REVISION_NT_1_0B );
  2296. //
  2297. // decrypt the cache entry...
  2298. //
  2299. NtStatus = NlpDecryptCacheEntry( *CacheEntry, *EntrySize );
  2300. if(!NT_SUCCESS(NtStatus)) {
  2301. //
  2302. // for failed decrypt, continue the search.
  2303. // the reason for this is because the decrypt does an integrity
  2304. // check. We don't want one corrupt cache entry to cause (possibly)
  2305. // the whole cache to be invalidated.
  2306. //
  2307. FreeToHeap( (*CacheEntry) );
  2308. Next = (PNLP_CTE)(Next->Link.Flink);
  2309. continue;
  2310. }
  2311. CachedUser.Length =
  2312. CachedUser.MaximumLength = (*CacheEntry)->UserNameLength;
  2313. if( (*CacheEntry)->Revision >= NLP_CACHE_REVISION_NT_5_0 ) {
  2314. CachedUser.Buffer = (PWSTR) ((PBYTE) *CacheEntry + sizeof(LOGON_CACHE_ENTRY));
  2315. } else {
  2316. CachedUser.Buffer = (PWSTR) ((PBYTE) *CacheEntry + sizeof(LOGON_CACHE_ENTRY_NT_4_SP4));
  2317. }
  2318. CachedDomain.Length =
  2319. CachedDomain.MaximumLength = (*CacheEntry)->DomainNameLength;
  2320. CachedDomain.Buffer = (PWSTR)((LPBYTE)CachedUser.Buffer +
  2321. ROUND_UP_COUNT((*CacheEntry)->UserNameLength, sizeof(ULONG)));
  2322. CachedDnsDomain.Length =
  2323. CachedDnsDomain.MaximumLength = (*CacheEntry)->DnsDomainNameLength;
  2324. CachedDnsDomain.Buffer = (PWSTR)((LPBYTE)CachedDomain.Buffer +
  2325. ROUND_UP_COUNT((*CacheEntry)->DomainNameLength, sizeof(ULONG)));
  2326. CachedUpn.Length =
  2327. CachedUpn.MaximumLength = (*CacheEntry)->UpnLength;
  2328. CachedUpn.Buffer = (PWSTR)((LPBYTE)CachedDnsDomain.Buffer +
  2329. ROUND_UP_COUNT((*CacheEntry)->DnsDomainNameLength, sizeof(ULONG)));
  2330. //
  2331. // If this cache entry has the MIT flag set, then the supplemental
  2332. // data field contains a UNICODE_STRING MITUserName, followed by a
  2333. // UNICODE_STRING MITRealmName, followed by buffers.
  2334. //
  2335. if ((*CacheEntry)->CacheFlags == MSV1_0_CACHE_LOGON_REQUEST_MIT_LOGON)
  2336. {
  2337. if (NlpCheckMitCacheEntry(
  2338. *CacheEntry,
  2339. DomainName,
  2340. UserName
  2341. ))
  2342. {
  2343. //
  2344. // found it!
  2345. //
  2346. break; // out of while loop
  2347. }
  2348. //
  2349. // If the caller passed in a domain name,
  2350. // the user name is the SamAccountName,
  2351. // and the domain name is either the Netbios or Dns Domain Name.
  2352. //
  2353. } else if ( DomainName->Length != 0 ) {
  2354. if (RtlEqualUnicodeString(UserName, &CachedUser, (BOOLEAN) TRUE ) ) {
  2355. if ( RtlEqualDomainName(DomainName, &CachedDomain ) ||
  2356. RtlEqualUnicodeString(DomainName, &CachedDnsDomain, (BOOLEAN) TRUE ) ) {
  2357. //
  2358. // found it !
  2359. //
  2360. break; // out of while-loop
  2361. }
  2362. }
  2363. //
  2364. // If no domain name was passed in,
  2365. // the user name is the UPN.
  2366. //
  2367. } else {
  2368. if (RtlEqualUnicodeString(UserName, &CachedUpn, (BOOLEAN) TRUE ) ) {
  2369. //
  2370. // found it !
  2371. //
  2372. break; // out of while-loop
  2373. }
  2374. }
  2375. //
  2376. // Not the right entry, free the registry structure
  2377. // and go on to the next one.
  2378. //
  2379. FreeToHeap( (*CacheEntry) );
  2380. Next = (PNLP_CTE)(Next->Link.Flink);
  2381. }
  2382. if (Next != (PNLP_CTE)&NlpActiveCtes && NT_SUCCESS(NtStatus)) {
  2383. //
  2384. // We found a match - Open the corresponding secret
  2385. //
  2386. (*Index) = Next->Index;
  2387. if( (*CacheEntry)->Revision < NLP_CACHE_REVISION_NT_5_0 ) {
  2388. //
  2389. // versions prior to NT5 require us open the corresponding LSA secret.
  2390. //
  2391. NtStatus = NlpOpenSecret(Next->Index);
  2392. if (!NT_SUCCESS(NtStatus)) {
  2393. FreeToHeap( (*CacheEntry) );
  2394. return(NtStatus);
  2395. }
  2396. }
  2397. } else {
  2398. NtStatus = STATUS_LOGON_FAILURE;
  2399. }
  2400. return(NtStatus);
  2401. }
  2402. NTSTATUS
  2403. NlpWriteCacheEntry(
  2404. IN ULONG Index,
  2405. IN PLOGON_CACHE_ENTRY Entry,
  2406. IN ULONG EntrySize
  2407. )
  2408. /*++
  2409. Routine Description:
  2410. Writes a LOGON_CACHE_ENTRY to the registry cache.
  2411. It is the caller's responsibility to place the corresponding
  2412. CTE table entry in the correct active/inactive list.
  2413. Arguments:
  2414. Index - Index of entry to write out.
  2415. Entry - pointer to LOGON_CACHE_ENTRY to write to cache
  2416. EntrySize - size of this entry (in bytes (must be multiple of 4 thoough))
  2417. Return Value:
  2418. NTSTATUS
  2419. Success = STATUS_SUCCESS
  2420. The LOGON_CACHE_ENTRY is now in the registry (hopefully
  2421. on disk)
  2422. Failure =
  2423. --*/
  2424. {
  2425. NTSTATUS
  2426. NtStatus;
  2427. UNICODE_STRING
  2428. ValueName;
  2429. WCHAR
  2430. NameBuffer[32];
  2431. ValueName.MaximumLength = 32;
  2432. ValueName.Length = 0;
  2433. ValueName.Buffer = &NameBuffer[0];
  2434. NlpMakeCacheEntryName( Index, &ValueName );
  2435. NtStatus = NtSetValueKey(NlpCacheHandle,
  2436. &ValueName,
  2437. 0, // TitleIndex
  2438. REG_BINARY, // Type
  2439. (PVOID)Entry,
  2440. EntrySize
  2441. );
  2442. return(NtStatus);
  2443. }
  2444. VOID
  2445. NlpCopyAndUpdateAccountInfo(
  2446. IN USHORT Length,
  2447. IN PUNICODE_STRING pUnicodeString,
  2448. IN OUT PUCHAR* pSource,
  2449. IN OUT PUCHAR* pDest
  2450. )
  2451. /*++
  2452. Routine Description:
  2453. Updates a UNICODE_STRING structure and copies the associated buffer to
  2454. *pDest, if Length is non-zero
  2455. Arguments:
  2456. Length - length of UNICODE_STRING.Buffer to copy
  2457. pUnicodeString - pointer to UNICODE_STRING structure to update
  2458. pSource - pointer to pointer to source WCHAR string
  2459. pDest - pointer to pointer to place to copy WCHAR string
  2460. Return Value:
  2461. None.
  2462. if string was copied, *Source and *Dest are updated to point to the next
  2463. naturally aligned (DWORD) positions in the input and output buffers resp.
  2464. --*/
  2465. {
  2466. PUCHAR source = *pSource;
  2467. PUCHAR dest = *pDest;
  2468. pUnicodeString->Length = Length;
  2469. pUnicodeString->MaximumLength = Length;
  2470. pUnicodeString->Buffer = (PWSTR)dest;
  2471. if (Length) {
  2472. RtlCopyMemory(dest, source, Length);
  2473. *pSource = ROUND_UP_POINTER(source + Length, sizeof(ULONG));
  2474. *pDest = ROUND_UP_POINTER(dest + Length, sizeof(ULONG));
  2475. }
  2476. }
  2477. VOID
  2478. NlpSetTimeField(
  2479. OUT POLD_LARGE_INTEGER pTimeField,
  2480. IN NLP_SET_TIME_HINT Hint
  2481. )
  2482. /*++
  2483. Routine Description:
  2484. Sets a LARGE_INTEGER time field to one of 3 values:
  2485. NLP_BIG_TIME = maximum positive large integer (0x7fffffffffffffff)
  2486. NLP_SMALL_TIME = smallest positive large integer (0)
  2487. NLP_NOW_TIME = current system time
  2488. Arguments:
  2489. pTimeField - pointer to LARGE_INTEGER structure to update
  2490. Hint - NLP_BIG_TIME, NLP_SMALL_TIME or NLP_NOW_TIME
  2491. Return Value:
  2492. None.
  2493. --*/
  2494. {
  2495. LARGE_INTEGER Time;
  2496. switch (Hint) {
  2497. case NLP_SMALL_TIME:
  2498. pTimeField->HighPart = SMALL_PART_1;
  2499. pTimeField->LowPart = SMALL_PART_2;
  2500. break;
  2501. case NLP_BIG_TIME:
  2502. pTimeField->HighPart = BIG_PART_1;
  2503. pTimeField->LowPart = BIG_PART_2;
  2504. break;
  2505. case NLP_NOW_TIME:
  2506. NtQuerySystemTime(&Time);
  2507. NEW_TO_OLD_LARGE_INTEGER( Time, (*pTimeField) );
  2508. break;
  2509. }
  2510. }
  2511. NTSTATUS
  2512. NlpBuildAccountInfo(
  2513. IN PLOGON_CACHE_ENTRY pCacheEntry,
  2514. IN ULONG EntryLength,
  2515. OUT PNETLOGON_VALIDATION_SAM_INFO4 *AccountInfo
  2516. )
  2517. /*++
  2518. Routine Description:
  2519. Performs the reverse of NlpBuildCacheEntry - creates a NETLOGON_VALIDATION_SAM_INFO4
  2520. structure from a cache entry
  2521. Arguments:
  2522. pCacheEntry - pointer to LOGON_CACHE_ENTRY
  2523. EntryLength - inclusive size of *pCacheEntry, including variable data
  2524. AccountInfo - pointer to place to create NETLOGON_VALIDATION_SAM_INFO4
  2525. Return Value:
  2526. NTSTATUS
  2527. Success = STATUS_SUCCESS
  2528. Failure = STATUS_NO_MEMORY
  2529. --*/
  2530. {
  2531. PNETLOGON_VALIDATION_SAM_INFO4 pSamInfo;
  2532. PUCHAR source;
  2533. PUCHAR dest;
  2534. ULONG length;
  2535. ULONG sidLength;
  2536. ULONG commonBits;
  2537. LPWSTR computerName;
  2538. ULONG computerNameLength = 0;
  2539. //
  2540. // commonBits is the size of the variable data area common to both the
  2541. // LOGON_CACHE_ENTRY and NETLOGON_VALIDATION_SAM_INFO4 structures
  2542. //
  2543. commonBits = ROUND_UP_COUNT(pCacheEntry->EffectiveNameLength, sizeof(ULONG))
  2544. + ROUND_UP_COUNT(pCacheEntry->FullNameLength, sizeof(ULONG))
  2545. + ROUND_UP_COUNT(pCacheEntry->LogonScriptLength, sizeof(ULONG))
  2546. + ROUND_UP_COUNT(pCacheEntry->ProfilePathLength, sizeof(ULONG))
  2547. + ROUND_UP_COUNT(pCacheEntry->HomeDirectoryLength, sizeof(ULONG))
  2548. + ROUND_UP_COUNT(pCacheEntry->HomeDirectoryDriveLength, sizeof(ULONG))
  2549. + ROUND_UP_COUNT(pCacheEntry->GroupCount * sizeof(GROUP_MEMBERSHIP), sizeof(ULONG))
  2550. + ROUND_UP_COUNT(pCacheEntry->LogonDomainNameLength, sizeof(ULONG))
  2551. ;
  2552. if( pCacheEntry->Revision >= NLP_CACHE_REVISION_NT_5_0 )
  2553. {
  2554. commonBits += ROUND_UP_COUNT(pCacheEntry->DnsDomainNameLength, sizeof(ULONG))
  2555. + ROUND_UP_COUNT(pCacheEntry->UpnLength, sizeof(ULONG))
  2556. ;
  2557. if( pCacheEntry->LogonServerLength != 0 )
  2558. {
  2559. computerNameLength = pCacheEntry->LogonServerLength;
  2560. computerName = NULL;
  2561. }
  2562. }
  2563. if( computerNameLength == 0 )
  2564. {
  2565. //
  2566. // will GetComputerName ever fail??? Its only used to fake the logon
  2567. // server name when we logon using the cached information, so its
  2568. // probably ok to use a NULL string if GetComputerName phones in sick
  2569. //
  2570. computerName = NlpComputerName.Buffer;
  2571. computerNameLength = NlpComputerName.Length / sizeof(WCHAR);
  2572. ASSERT( computerName );
  2573. ASSERT( computerNameLength );
  2574. #if DBG
  2575. if (DumpCacheInfo) {
  2576. DbgPrint("ComputerName = %ws, length = %d\n", computerName, computerNameLength);
  2577. }
  2578. #endif
  2579. }
  2580. ASSERT(pCacheEntry->Revision >= NLP_CACHE_REVISION_NT_1_0B);
  2581. //
  2582. // Account for possible roundup for NETLOGON_SID_AND_ATTRIBUTES structure
  2583. //
  2584. commonBits += sizeof(PVOID);
  2585. commonBits += ROUND_UP_COUNT(sizeof(NETLOGON_SID_AND_ATTRIBUTES) * pCacheEntry->SidCount, sizeof(ULONG))
  2586. + ROUND_UP_COUNT(pCacheEntry->SidLength, sizeof(ULONG))
  2587. ;
  2588. sidLength = pCacheEntry->LogonDomainIdLength;
  2589. //
  2590. // length is the required amount of buffer in which to build a working
  2591. // NETLOGON_VALIDATION_SAM_INFO4 structure
  2592. //
  2593. length = ROUND_UP_COUNT(sizeof(NETLOGON_VALIDATION_SAM_INFO4), sizeof(ULONG))
  2594. + commonBits
  2595. + sidLength
  2596. + computerNameLength * sizeof(WCHAR)
  2597. ;
  2598. #if DBG
  2599. if (DumpCacheInfo) {
  2600. DbgPrint("NlpBuildAccountInfo: %d bytes required\n", length);
  2601. }
  2602. #endif
  2603. // MIDL_user_allocate zeros the buffer. This routine depends on that.
  2604. pSamInfo = (PNETLOGON_VALIDATION_SAM_INFO4)MIDL_user_allocate(length);
  2605. if (pSamInfo == NULL) {
  2606. return STATUS_NO_MEMORY;
  2607. }
  2608. //
  2609. // point dest at the first (aligned) byte at the start of the variable data
  2610. // area at the end of the sam info structure
  2611. //
  2612. dest = (PUCHAR)(pSamInfo + 1);
  2613. //
  2614. // point source at the first string to be copied out of the variable length
  2615. // data area at the end of the cache entry
  2616. //
  2617. ASSERT(pCacheEntry->Revision >= NLP_CACHE_REVISION_NT_1_0B );
  2618. if( pCacheEntry->Revision >= NLP_CACHE_REVISION_NT_5_0 ) {
  2619. source = (PUCHAR)(pCacheEntry + 1);
  2620. } else {
  2621. source = (PUCHAR)( (PLOGON_CACHE_ENTRY_NT_4_SP4)pCacheEntry + 1 );
  2622. }
  2623. source += ROUND_UP_COUNT(pCacheEntry->UserNameLength, sizeof(ULONG))
  2624. + ROUND_UP_COUNT(pCacheEntry->DomainNameLength, sizeof(ULONG)) ;
  2625. if( pCacheEntry->Revision >= NLP_CACHE_REVISION_NT_5_0 )
  2626. {
  2627. NlpCopyAndUpdateAccountInfo(pCacheEntry->DnsDomainNameLength,
  2628. &pSamInfo->DnsLogonDomainName,
  2629. &source,
  2630. &dest
  2631. );
  2632. NlpCopyAndUpdateAccountInfo(pCacheEntry->UpnLength,
  2633. &pSamInfo->Upn,
  2634. &source,
  2635. &dest
  2636. );
  2637. } else {
  2638. //
  2639. // Fill in the new field for the PNETLOGON_VALIDATION_SAM_INFO4 structure
  2640. //
  2641. RtlInitUnicodeString( &pSamInfo->DnsLogonDomainName, NULL );
  2642. RtlInitUnicodeString( &pSamInfo->Upn, NULL );
  2643. source += ROUND_UP_COUNT(pCacheEntry->DnsDomainNameLength, sizeof(ULONG))
  2644. + ROUND_UP_COUNT(pCacheEntry->UpnLength, sizeof(ULONG));
  2645. }
  2646. //
  2647. // pull out the variable length data from the end of the LOGON_CACHE_ENTRY
  2648. // and stick them at the end of the NETLOGON_VALIDATION_SAM_INFO4 structure.
  2649. // These must be copied out IN THE SAME ORDER as NlpBuildCacheEntry put them
  2650. // in. If we want to change the order of things in the buffer, the order
  2651. // must be changed in both routines (this & NlpBuildCacheEntry)
  2652. //
  2653. //
  2654. // create the UNICODE_STRING structures in the NETLOGON_VALIDATION_SAM_INFO4
  2655. // structure and copy the strings to the end of the buffer. 0 length strings
  2656. // will get a pointer which should be ignored
  2657. //
  2658. NlpCopyAndUpdateAccountInfo(pCacheEntry->EffectiveNameLength,
  2659. &pSamInfo->EffectiveName,
  2660. &source,
  2661. &dest
  2662. );
  2663. NlpCopyAndUpdateAccountInfo(pCacheEntry->FullNameLength,
  2664. &pSamInfo->FullName,
  2665. &source,
  2666. &dest
  2667. );
  2668. NlpCopyAndUpdateAccountInfo(pCacheEntry->LogonScriptLength,
  2669. &pSamInfo->LogonScript,
  2670. &source,
  2671. &dest
  2672. );
  2673. NlpCopyAndUpdateAccountInfo(pCacheEntry->ProfilePathLength,
  2674. &pSamInfo->ProfilePath,
  2675. &source,
  2676. &dest
  2677. );
  2678. NlpCopyAndUpdateAccountInfo(pCacheEntry->HomeDirectoryLength,
  2679. &pSamInfo->HomeDirectory,
  2680. &source,
  2681. &dest
  2682. );
  2683. NlpCopyAndUpdateAccountInfo(pCacheEntry->HomeDirectoryDriveLength,
  2684. &pSamInfo->HomeDirectoryDrive,
  2685. &source,
  2686. &dest
  2687. );
  2688. //
  2689. // copy the group membership array
  2690. //
  2691. pSamInfo->GroupIds = (PGROUP_MEMBERSHIP)dest;
  2692. length = pCacheEntry->GroupCount * sizeof(GROUP_MEMBERSHIP);
  2693. RtlCopyMemory(dest, source, length);
  2694. dest = ROUND_UP_POINTER(dest + length, sizeof(ULONG));
  2695. source = ROUND_UP_POINTER(source + length, sizeof(ULONG));
  2696. //
  2697. // final UNICODE_STRING from LOGON_CACHE_ENTRY. Reorganize this to:
  2698. // strings, groups, SID?
  2699. //
  2700. NlpCopyAndUpdateAccountInfo(pCacheEntry->LogonDomainNameLength,
  2701. &pSamInfo->LogonDomainName,
  2702. &source,
  2703. &dest
  2704. );
  2705. //
  2706. // Copy all the SIDs
  2707. //
  2708. if (pCacheEntry->Revision >= NLP_CACHE_REVISION_NT_1_0B ) {
  2709. pSamInfo->SidCount = pCacheEntry->SidCount;
  2710. if (pCacheEntry->SidCount) {
  2711. ULONG i, sidLength;
  2712. PULONG SidAttributes = (PULONG) source;
  2713. source = ROUND_UP_POINTER(source + pCacheEntry->SidCount * sizeof(ULONG), sizeof(ULONG));
  2714. //
  2715. // Structures containing pointers must start on 8-byte boundries
  2716. //
  2717. dest = ROUND_UP_POINTER(dest, sizeof(PVOID));
  2718. pSamInfo->ExtraSids = (PNETLOGON_SID_AND_ATTRIBUTES) dest;
  2719. dest = ROUND_UP_POINTER(dest + pCacheEntry->SidCount * sizeof(NETLOGON_SID_AND_ATTRIBUTES), sizeof(ULONG));
  2720. for (i = 0; i < pCacheEntry->SidCount ; i++ ) {
  2721. pSamInfo->ExtraSids[i].Attributes = SidAttributes[i];
  2722. sidLength = RtlLengthSid((PSID) source);
  2723. RtlCopySid(sidLength, (PSID) dest, (PSID) source);
  2724. pSamInfo->ExtraSids[i].Sid = (PSID) dest;
  2725. dest = ROUND_UP_POINTER(dest + sidLength, sizeof(ULONG));
  2726. source = ROUND_UP_POINTER(source + sidLength, sizeof(ULONG));
  2727. }
  2728. ASSERT((ULONG) (source - (PCHAR) SidAttributes) == pCacheEntry->SidLength);
  2729. } else {
  2730. pSamInfo->ExtraSids = NULL;
  2731. }
  2732. } else {
  2733. pSamInfo->ExtraSids = NULL;
  2734. pSamInfo->SidCount = 0;
  2735. }
  2736. //
  2737. // copy the LogonDomainId SID
  2738. //
  2739. RtlCopySid(sidLength, (PSID)dest, (PSID)source);
  2740. pSamInfo->LogonDomainId = (PSID)dest;
  2741. dest = ROUND_UP_POINTER(dest + sidLength, sizeof(ULONG));
  2742. source = ROUND_UP_POINTER(source + sidLength, sizeof(ULONG));
  2743. if( computerName != NULL )
  2744. {
  2745. //
  2746. // final UNICODE_STRING. This one from stack. Note that we have finished
  2747. // with source
  2748. //
  2749. source = (PUCHAR)computerName;
  2750. NlpCopyAndUpdateAccountInfo((USHORT)(computerNameLength * sizeof(WCHAR)),
  2751. &pSamInfo->LogonServer,
  2752. &source,
  2753. &dest
  2754. );
  2755. } else {
  2756. //
  2757. // Sanity check that we have a proper cache revision.
  2758. //
  2759. if( pCacheEntry->Revision >= NLP_CACHE_REVISION_NT_5_0 )
  2760. {
  2761. //
  2762. // final UNICODE_STRING from LOGON_CACHE_ENTRY.
  2763. //
  2764. NlpCopyAndUpdateAccountInfo((USHORT)pCacheEntry->LogonServerLength,
  2765. &pSamInfo->LogonServer,
  2766. &source,
  2767. &dest
  2768. );
  2769. }
  2770. }
  2771. //
  2772. // copy the non-variable fields
  2773. //
  2774. pSamInfo->UserId = pCacheEntry->UserId;
  2775. pSamInfo->PrimaryGroupId = pCacheEntry->PrimaryGroupId;
  2776. pSamInfo->GroupCount = pCacheEntry->GroupCount;
  2777. //
  2778. // finally, invent some fields
  2779. //
  2780. NlpSetTimeField(&pSamInfo->LogonTime, NLP_NOW_TIME);
  2781. NlpSetTimeField(&pSamInfo->LogoffTime, NLP_BIG_TIME);
  2782. NlpSetTimeField(&pSamInfo->KickOffTime, NLP_BIG_TIME);
  2783. NlpSetTimeField(&pSamInfo->PasswordLastSet, NLP_SMALL_TIME);
  2784. NlpSetTimeField(&pSamInfo->PasswordCanChange, NLP_BIG_TIME);
  2785. NlpSetTimeField(&pSamInfo->PasswordMustChange, NLP_BIG_TIME);
  2786. pSamInfo->LogonCount = 0;
  2787. pSamInfo->BadPasswordCount = 0;
  2788. pSamInfo->UserFlags = LOGON_EXTRA_SIDS;
  2789. if (pCacheEntry->LogonPackage != 0) {
  2790. pSamInfo->UserFlags |= pCacheEntry->LogonPackage << PRIMARY_CRED_LOGON_PACKAGE_SHIFT;
  2791. }
  2792. // RtlZeroMemory(&pSamInfo->UserSessionKey, sizeof(pSamInfo->UserSessionKey));
  2793. #if DBG
  2794. if (DumpCacheInfo) {
  2795. DbgPrint("NlpBuildAccountInfo:\n");
  2796. DumpAccountInfo(pSamInfo);
  2797. }
  2798. #endif
  2799. *AccountInfo = pSamInfo;
  2800. return STATUS_SUCCESS;
  2801. UNREFERENCED_PARAMETER( EntryLength );
  2802. }
  2803. NTSTATUS
  2804. NlpGetCacheControlInfo( VOID )
  2805. /*++
  2806. Routine Description:
  2807. This function retrieves the cache control information from the
  2808. registry. This information is placed in global data for use
  2809. throughout this module. The Cache Table Entry table will also
  2810. be initialized.
  2811. If this routine returns success, then it may be assumed that
  2812. everything completed successfully.
  2813. Arguments:
  2814. None.
  2815. Return Value:
  2816. --*/
  2817. {
  2818. NTSTATUS
  2819. NtStatus;
  2820. UNICODE_STRING
  2821. CacheControlValueName;
  2822. ULONG
  2823. RequiredSize;
  2824. PKEY_VALUE_PARTIAL_INFORMATION
  2825. RegInfo = NULL;
  2826. //
  2827. // read the current control info, if it is there.
  2828. // If it is not there, then we may be dealing with a down-level
  2829. // system and might have a single cache entry in the registry.
  2830. //
  2831. RtlInitUnicodeString( &CacheControlValueName, L"NL$Control" );
  2832. NtStatus = NtQueryValueKey(NlpCacheHandle,
  2833. &CacheControlValueName,
  2834. KeyValuePartialInformation,
  2835. NULL,
  2836. 0,
  2837. &RequiredSize
  2838. );
  2839. if (NT_SUCCESS(NtStatus) || NtStatus == STATUS_OBJECT_NAME_NOT_FOUND) {
  2840. NTSTATUS TempStatus;
  2841. //
  2842. // Hmmm - no entry, that means we are dealing with a
  2843. // first release system here (that didn't have
  2844. // this value).
  2845. //
  2846. //
  2847. // Set up for 1 cache entry.
  2848. // create the secret and cache key entry
  2849. //
  2850. TempStatus = NlpMakeNewCacheEntry( 0 );
  2851. if ( NT_SUCCESS(TempStatus) ) {
  2852. //
  2853. // Now flush out the control information
  2854. //
  2855. NlpCacheControl.Revision = NLP_CACHE_REVISION;
  2856. NlpCacheControl.Entries = 1;
  2857. TempStatus = NlpWriteCacheControl();
  2858. if ( NT_SUCCESS(TempStatus) ) {
  2859. //
  2860. // If a version 1.0 entry exists,
  2861. // copy the old form of cache entry to the new structure.
  2862. //
  2863. // if (NT_SUCCESS(NtStatus)) {
  2864. // TempStatus = NlpConvert1_0To1_0B();
  2865. // }
  2866. }
  2867. }
  2868. NtStatus = TempStatus;
  2869. } else if ( NtStatus == STATUS_BUFFER_TOO_SMALL ) {
  2870. //
  2871. // allocate buffer then do query again, this time receiving data
  2872. //
  2873. RegInfo = (PKEY_VALUE_PARTIAL_INFORMATION)AllocateFromHeap(RequiredSize);
  2874. if (RegInfo == NULL) {
  2875. NtStatus = STATUS_NO_MEMORY;
  2876. goto Cleanup;
  2877. }
  2878. NtStatus = NtQueryValueKey(NlpCacheHandle,
  2879. &CacheControlValueName,
  2880. KeyValuePartialInformation,
  2881. (PVOID)RegInfo,
  2882. RequiredSize,
  2883. &RequiredSize
  2884. );
  2885. if (!NT_SUCCESS(NtStatus)) {
  2886. goto Cleanup;
  2887. }
  2888. //
  2889. // check the revision - we can't deal with up-level revisions.
  2890. //
  2891. if (RegInfo->DataLength < sizeof(NLP_CACHE_CONTROL)) {
  2892. NtStatus = STATUS_UNKNOWN_REVISION;
  2893. goto Cleanup;
  2894. }
  2895. RtlCopyMemory( &NlpCacheControl, &(RegInfo->Data[0]), sizeof(NLP_CACHE_CONTROL) );
  2896. if (NlpCacheControl.Revision > NLP_CACHE_REVISION) {
  2897. NtStatus = STATUS_UNKNOWN_REVISION;
  2898. goto Cleanup;
  2899. }
  2900. //
  2901. // If this is an older cache, update it with the latest revision
  2902. //
  2903. if (NlpCacheControl.Revision != NLP_CACHE_REVISION) {
  2904. // There is no conversion. All the version of cache control have
  2905. // been the same.
  2906. NlpCacheControl.Revision = NLP_CACHE_REVISION;
  2907. NtStatus = NlpWriteCacheControl();
  2908. if (!NT_SUCCESS(NtStatus)) {
  2909. goto Cleanup;
  2910. }
  2911. }
  2912. NtStatus = STATUS_SUCCESS;
  2913. }
  2914. Cleanup:
  2915. if (!NT_SUCCESS(NtStatus)) {
  2916. NlpCacheControl.Entries = 0; // Disable logon cache
  2917. }
  2918. if( RegInfo ) {
  2919. FreeToHeap( RegInfo );
  2920. }
  2921. return(NtStatus);
  2922. }
  2923. NTSTATUS
  2924. NlpBuildCteTable( VOID )
  2925. /*++
  2926. Routine Description:
  2927. This function initializes the CTE table from the contents of
  2928. the cache in the registry.
  2929. Arguments:
  2930. None.
  2931. Return Value:
  2932. STATUS_SUCCESS - the cache is initialized.
  2933. Other - The cache has been disabled.
  2934. --*/
  2935. {
  2936. NTSTATUS
  2937. NtStatus = STATUS_SUCCESS;
  2938. PLOGON_CACHE_ENTRY
  2939. CacheEntry;
  2940. ULONG
  2941. EntrySize,
  2942. i;
  2943. //
  2944. // Initialize the active and inactive CTE lists
  2945. //
  2946. InitializeListHead( &NlpActiveCtes );
  2947. InitializeListHead( &NlpInactiveCtes );
  2948. //
  2949. // Allocate a CTE table
  2950. //
  2951. NlpCteTable = AllocateFromHeap( sizeof( NLP_CTE ) *
  2952. NlpCacheControl.Entries );
  2953. if (NlpCteTable == NULL) {
  2954. //
  2955. // Can't allocate table, disable caching
  2956. //
  2957. NlpCacheControl.Entries = 0; // Disable cache
  2958. return(STATUS_NO_MEMORY);
  2959. }
  2960. for (i=0; i<NlpCacheControl.Entries; i++) {
  2961. NtStatus = NlpReadCacheEntryByIndex( i,
  2962. &CacheEntry,
  2963. &EntrySize);
  2964. if (!NT_SUCCESS(NtStatus) ) {
  2965. NlpCacheControl.Entries = 0; // Disable cache
  2966. return(NtStatus);
  2967. }
  2968. //
  2969. //
  2970. if (EntrySize < sizeof(LOGON_CACHE_ENTRY_NT_4_SP4)) {
  2971. //
  2972. // Hmmm, something is bad.
  2973. // disable caching and return an error
  2974. //
  2975. NlpCacheControl.Entries = 0; // Disable cache
  2976. FreeToHeap( CacheEntry );
  2977. return( STATUS_INTERNAL_DB_CORRUPTION );
  2978. }
  2979. if (CacheEntry->Revision > NLP_CACHE_REVISION) {
  2980. NlpCacheControl.Entries = 0; // Disable cache
  2981. FreeToHeap( CacheEntry );
  2982. return(STATUS_UNKNOWN_REVISION);
  2983. }
  2984. NlpCteTable[i].Index = i;
  2985. NlpCteTable[i].Active = CacheEntry->Valid;
  2986. NlpCteTable[i].Time = CacheEntry->Time;
  2987. InsertTailList( &NlpInactiveCtes, &NlpCteTable[i].Link );
  2988. if (NlpCteTable[i].Active) {
  2989. NlpAddEntryToActiveList( i );
  2990. }
  2991. FreeToHeap( CacheEntry );
  2992. }
  2993. return(NtStatus);
  2994. }
  2995. NTSTATUS
  2996. NlpChangeCacheSizeIfNecessary( VOID )
  2997. /*++
  2998. Routine Description:
  2999. This function checks to see if the user has requested a
  3000. different cache size than what we currently have.
  3001. If so, then we try to grow or shrink our cache appropriately.
  3002. If this succeeds, then the global cache control information is
  3003. updated appropriately. If it fails then one of two things will
  3004. happen:
  3005. 1) If the user was trying to shrink the cache, then it will
  3006. be disabled (entries set to zero).
  3007. 2) If the user was trying to grow the cache, then we will leave
  3008. it as it is.
  3009. In either of these two failure conditions, an error is returned.
  3010. Arguments:
  3011. None.
  3012. Return Value:
  3013. STATUS_SUCCESS
  3014. --*/
  3015. {
  3016. NTSTATUS
  3017. NtStatus;
  3018. UINT
  3019. CachedLogonsCount;
  3020. PNLP_CTE
  3021. NewCteTable,
  3022. Next;
  3023. LIST_ENTRY
  3024. NewActive,
  3025. NewInactive;
  3026. PNLP_CACHE_AND_SECRETS
  3027. CacheAndSecrets;
  3028. ULONG
  3029. ErrorCacheSize,
  3030. EntrySize,
  3031. i,
  3032. j;
  3033. // Find out how many logons to cache.
  3034. // This is a user setable value and it may be different than
  3035. // the last time we booted.
  3036. //
  3037. CachedLogonsCount = GetProfileInt(
  3038. TEXT("Winlogon"),
  3039. TEXT("CachedLogonsCount"),
  3040. NLP_DEFAULT_LOGON_CACHE_COUNT // Default value
  3041. );
  3042. //
  3043. // Minimize the user-supplied value with the maximum allowable
  3044. // value.
  3045. //
  3046. if (CachedLogonsCount > NLP_MAX_LOGON_CACHE_COUNT) {
  3047. CachedLogonsCount = NLP_MAX_LOGON_CACHE_COUNT;
  3048. }
  3049. //
  3050. // Compare it to what we already have and see if we need
  3051. // to change the size of the cache
  3052. //
  3053. if (CachedLogonsCount == NlpCacheControl.Entries) {
  3054. //
  3055. // No change
  3056. //
  3057. return(STATUS_SUCCESS);
  3058. }
  3059. //
  3060. // Set the size of the cache to be used in case of error
  3061. // changing the size. If we are trying to grow the cache,
  3062. // then use the existing cache on error. If we are trying
  3063. // to shrink the cache, then disable caching on error.
  3064. //
  3065. if (CachedLogonsCount > NlpCacheControl.Entries) {
  3066. ErrorCacheSize = NlpCacheControl.Entries;
  3067. } else {
  3068. ErrorCacheSize = 0;
  3069. }
  3070. //
  3071. // Allocate a CTE table the size of the new table
  3072. //
  3073. NewCteTable = AllocateFromHeap( sizeof( NLP_CTE ) *
  3074. CachedLogonsCount );
  3075. if (NewCteTable == NULL) {
  3076. //
  3077. // Can't shrink table, disable caching
  3078. //
  3079. NlpCacheControl.Entries = ErrorCacheSize;
  3080. return(STATUS_NO_MEMORY);
  3081. }
  3082. //
  3083. // Now the tricky parts ...
  3084. //
  3085. if (CachedLogonsCount > NlpCacheControl.Entries) {
  3086. //
  3087. // Try to grow the cache -
  3088. // Create additional secrets and cache entries.
  3089. //
  3090. // Copy time fields and set index
  3091. //
  3092. for (i=0; i < NlpCacheControl.Entries; i++) {
  3093. NewCteTable[i].Index = i;
  3094. NewCteTable[i].Time = NlpCteTable[i].Time;
  3095. }
  3096. //
  3097. // Place existing entries on either the active or inactive list
  3098. //
  3099. InitializeListHead( &NewActive );
  3100. for (Next = (PNLP_CTE)NlpActiveCtes.Flink;
  3101. Next != (PNLP_CTE)(&NlpActiveCtes);
  3102. Next = (PNLP_CTE)Next->Link.Flink
  3103. ) {
  3104. InsertTailList( &NewActive, &NewCteTable[Next->Index].Link );
  3105. NewCteTable[Next->Index].Active = TRUE;
  3106. }
  3107. InitializeListHead( &NewInactive );
  3108. for (Next = (PNLP_CTE)NlpInactiveCtes.Flink;
  3109. Next != (PNLP_CTE)(&NlpInactiveCtes);
  3110. Next = (PNLP_CTE)Next->Link.Flink
  3111. ) {
  3112. InsertTailList( &NewInactive, &NewCteTable[Next->Index].Link );
  3113. NewCteTable[Next->Index].Active = FALSE;
  3114. }
  3115. //
  3116. // Make all the new table entries.
  3117. // Mark them as invalid.
  3118. //
  3119. for (i=NlpCacheControl.Entries; i<CachedLogonsCount; i++) {
  3120. //
  3121. // Add the CTE entry to the inactive list
  3122. //
  3123. InsertTailList( &NewInactive, &NewCteTable[i].Link );
  3124. NewCteTable[i].Active = FALSE;
  3125. NewCteTable[i].Index = i;
  3126. NtStatus = NlpMakeNewCacheEntry( i );
  3127. if (!NT_SUCCESS(NtStatus)) {
  3128. FreeToHeap( NewCteTable );
  3129. return(NtStatus);
  3130. }
  3131. }
  3132. } else {
  3133. //
  3134. // Try to shrink the cache.
  3135. //
  3136. if (CachedLogonsCount != 0) {
  3137. //
  3138. // 0 size implies disabling the cache.
  3139. // That is a degenerate case of shrinking that
  3140. // requires only the last few steps of shrinking.
  3141. //
  3142. //
  3143. // Allocate an array of pointers for reading registry and secret
  3144. // info into. Clear it to assist in cleanup.
  3145. //
  3146. CacheAndSecrets = (PNLP_CACHE_AND_SECRETS)
  3147. AllocateFromHeap( sizeof( NLP_CACHE_AND_SECRETS ) *
  3148. CachedLogonsCount );
  3149. if (CacheAndSecrets == NULL) {
  3150. FreeToHeap( NlpCteTable );
  3151. NlpCacheControl.Entries = ErrorCacheSize;
  3152. return(STATUS_NO_MEMORY);
  3153. }
  3154. RtlZeroMemory( CacheAndSecrets,
  3155. (sizeof( NLP_CACHE_AND_SECRETS ) * CachedLogonsCount) );
  3156. //
  3157. // Set up the new CTE table to be inactive
  3158. //
  3159. InitializeListHead( &NewActive );
  3160. InitializeListHead( &NewInactive );
  3161. for (i=0; i<CachedLogonsCount; i++) {
  3162. InsertTailList( &NewInactive, &NewCteTable[i].Link );
  3163. NewCteTable[i].Index = i;
  3164. NewCteTable[i].Active = FALSE;
  3165. }
  3166. //
  3167. // Walk the current active list, reading
  3168. // entries and copying information into the new CTE table.
  3169. //
  3170. i = 0;
  3171. Next = (PNLP_CTE)NlpActiveCtes.Flink;
  3172. while (Next != (PNLP_CTE)&NlpActiveCtes && i<CachedLogonsCount) {
  3173. NtStatus = NlpReadCacheEntryByIndex( Next->Index,
  3174. &CacheAndSecrets[i].CacheEntry,
  3175. &CacheAndSecrets[i].EntrySize
  3176. // &EntrySize
  3177. );
  3178. if (NT_SUCCESS(NtStatus)) {
  3179. //
  3180. // for pre-Win2000 cache entries, read the associated secret.
  3181. //
  3182. if( CacheAndSecrets[i].CacheEntry->Revision < NLP_CACHE_REVISION_NT_5_0 ) {
  3183. NtStatus = NlpOpenSecret( Next->Index );
  3184. if (NT_SUCCESS(NtStatus)) {
  3185. NtStatus = NlpReadSecret( &CacheAndSecrets[i].NewSecret,
  3186. &CacheAndSecrets[i].OldSecret);
  3187. NlpCloseSecret();
  3188. }
  3189. }
  3190. if (NT_SUCCESS(NtStatus)) {
  3191. //
  3192. // Only make this entry active if everything was
  3193. // successfully read in.
  3194. //
  3195. CacheAndSecrets[i].Active = TRUE;
  3196. i++; // advance our new CTE table index
  3197. }
  3198. }
  3199. Next = (PNLP_CTE)(Next->Link.Flink);
  3200. } // end-while
  3201. //
  3202. // At this point "i" indicates how many CacheAndSecrets entries
  3203. // are active. Furthermore, the entries were assembled
  3204. // in the CacheAndSecrets array in ascending time order, which
  3205. // is the order they need to be placed in the new CTE table.
  3206. //
  3207. for ( j=0; j<i; j++) {
  3208. Next = &NewCteTable[j];
  3209. //
  3210. // The Time field in the original cache entry is not aligned
  3211. // properly, so copy each field individually.
  3212. //
  3213. Next->Time.LowPart = CacheAndSecrets[j].CacheEntry->Time.LowPart;
  3214. Next->Time.HighPart = CacheAndSecrets[j].CacheEntry->Time.HighPart;
  3215. //
  3216. // Try writing out the new entry's information
  3217. //
  3218. NtStatus = NlpWriteCacheEntry( j,
  3219. CacheAndSecrets[j].CacheEntry,
  3220. CacheAndSecrets[j].EntrySize
  3221. );
  3222. if (NT_SUCCESS(NtStatus)) {
  3223. if( CacheAndSecrets[j].CacheEntry->Revision < NLP_CACHE_REVISION_NT_5_0 ) {
  3224. //
  3225. // for pre-Win2000 cache entries, write the secret back out.
  3226. // note: we don't bother to try to migrate pre-win2000 -> Win2000
  3227. // here, because this will happen later, as a side-effect
  3228. // of updating cache entry during successful DC validated logon.
  3229. //
  3230. NtStatus = NlpOpenSecret( j );
  3231. if (NT_SUCCESS(NtStatus)) {
  3232. NtStatus = NlpWriteSecret(CacheAndSecrets[j].NewSecret,
  3233. CacheAndSecrets[j].OldSecret);
  3234. }
  3235. }
  3236. if (NT_SUCCESS(NtStatus)) {
  3237. //
  3238. // move the corresponding entry into the new CTEs
  3239. // active list.
  3240. //
  3241. Next->Active = TRUE;
  3242. RemoveEntryList( &Next->Link );
  3243. InsertTailList( &NewActive, &Next->Link );
  3244. }
  3245. }
  3246. //
  3247. // Free the CacheEntry and secret information
  3248. //
  3249. if (CacheAndSecrets[j].CacheEntry != NULL) {
  3250. FreeToHeap( CacheAndSecrets[j].CacheEntry );
  3251. }
  3252. if (CacheAndSecrets[j].NewSecret != NULL) {
  3253. MIDL_user_free( CacheAndSecrets[j].NewSecret );
  3254. }
  3255. if (CacheAndSecrets[j].OldSecret != NULL) {
  3256. MIDL_user_free( CacheAndSecrets[j].OldSecret );
  3257. }
  3258. }
  3259. //
  3260. // Free the CacheAndSecrets array
  3261. // (everything in it has already been freed)
  3262. //
  3263. if (CacheAndSecrets != NULL) {
  3264. FreeToHeap( CacheAndSecrets );
  3265. }
  3266. //
  3267. // Change remaining entries to invalid (on disk)
  3268. //
  3269. for ( j=i; j<CachedLogonsCount; j++) {
  3270. NlpMakeNewCacheEntry( j );
  3271. }
  3272. } // end-if (CachedLogonsCount != 0)
  3273. //
  3274. // Now get rid of extra (no longer needed) entries
  3275. //
  3276. for ( j=CachedLogonsCount; j<NlpCacheControl.Entries; j++) {
  3277. NlpEliminateCacheEntry( j );
  3278. }
  3279. }
  3280. //
  3281. // We have successfully:
  3282. //
  3283. // Allocated the new CTE table.
  3284. //
  3285. // Filled the CTE table with copies of the currently
  3286. // active CTEs (including putting each CTE on an active
  3287. // or inactive list).
  3288. //
  3289. // Established new CTE entries, including the corresponding
  3290. // secrets and cache keys in the registry, for the
  3291. // new CTEs.
  3292. //
  3293. //
  3294. // All we have left to do is:
  3295. //
  3296. //
  3297. // Update the cache control structure in the registry
  3298. // to indicate we have a new length
  3299. //
  3300. // move the new CTE over to the real Active and Inactive
  3301. // list heads (rather than the local ones we've used so far)
  3302. //
  3303. // deallocate the old CTE table.
  3304. //
  3305. // Re-set the entries count in the in-memory
  3306. // cache-control structure NlpCacheControl.
  3307. //
  3308. NlpCacheControl.Entries = CachedLogonsCount;
  3309. NtStatus = NlpWriteCacheControl();
  3310. if (CachedLogonsCount > 0) { // Only necessary if there is a new CTE table
  3311. if (!NT_SUCCESS(NtStatus)) {
  3312. FreeToHeap( NewCteTable );
  3313. NlpCacheControl.Entries = ErrorCacheSize;
  3314. return(NtStatus);
  3315. }
  3316. InsertHeadList( &NewActive, &NlpActiveCtes );
  3317. RemoveEntryList( &NewActive );
  3318. InsertHeadList( &NewInactive, &NlpInactiveCtes );
  3319. RemoveEntryList( &NewInactive );
  3320. FreeToHeap( NlpCteTable );
  3321. NlpCteTable = NewCteTable;
  3322. }
  3323. return(NtStatus);
  3324. }
  3325. NTSTATUS
  3326. NlpWriteCacheControl( VOID )
  3327. /*++
  3328. Routine Description:
  3329. This function writes a new cache length out to the
  3330. cache control structure stored in the registry.
  3331. Note:
  3332. When lengthening the cache, call this routine after the cache
  3333. entries and corresponding secrets have been established for
  3334. the new length.
  3335. When shortening the cache, call this routine before the cache
  3336. entries and corresponding secrets being discarded have actually
  3337. been discarded.
  3338. This ensures that if the system crashes during the resizing
  3339. operation, it will be in a valid state when the system comes
  3340. back up.
  3341. Arguments:
  3342. None.
  3343. Return Value:
  3344. STATUS_SUCCESS
  3345. --*/
  3346. {
  3347. NTSTATUS
  3348. NtStatus;
  3349. UNICODE_STRING
  3350. CacheControlValueName;
  3351. RtlInitUnicodeString( &CacheControlValueName, L"NL$Control" );
  3352. NtStatus = NtSetValueKey( NlpCacheHandle,
  3353. &CacheControlValueName, // Name
  3354. 0, // TitleIndex
  3355. REG_BINARY, // Type
  3356. &NlpCacheControl, // Data
  3357. sizeof(NLP_CACHE_CONTROL) // DataLength
  3358. );
  3359. return(NtStatus);
  3360. }
  3361. VOID
  3362. NlpMakeCacheEntryName(
  3363. IN ULONG EntryIndex,
  3364. OUT PUNICODE_STRING Name
  3365. )
  3366. /*++
  3367. Routine Description:
  3368. This function builds a name of a cache entry value or secret name
  3369. for a cached entry. The name is based upon the index of the cache
  3370. entry.
  3371. Names are of the form:
  3372. "NLP1" through "NLPnnn"
  3373. where "nnn" is the largest allowable entry count (see
  3374. NLP_MAX_LOGON_CACHE_COUNT).
  3375. The output UNICODE_STRING buffer is expected to be large enough
  3376. to accept this string with a null termination on it.
  3377. Arguments:
  3378. EntryIndex - The index of the cache entry whose name is desired.
  3379. Name - A unicode string large enough to accept the name.
  3380. Return Value:
  3381. STATUS_SUCCESS
  3382. --*/
  3383. {
  3384. NTSTATUS
  3385. NtStatus;
  3386. UNICODE_STRING
  3387. TmpString;
  3388. WCHAR
  3389. TmpStringBuffer[17];
  3390. ASSERT(Name->MaximumLength >= 7*sizeof(WCHAR) );
  3391. ASSERT( EntryIndex <= NLP_MAX_LOGON_CACHE_COUNT );
  3392. Name->Length = 0;
  3393. RtlAppendUnicodeToString( Name, L"NL$" );
  3394. TmpString.MaximumLength = 16;
  3395. TmpString.Length = 0;
  3396. TmpString.Buffer = TmpStringBuffer;
  3397. NtStatus = RtlIntegerToUnicodeString ( (EntryIndex+1), // make 1 based index
  3398. 10, // Base 10
  3399. &TmpString
  3400. );
  3401. ASSERT(NT_SUCCESS(NtStatus));
  3402. RtlAppendUnicodeStringToString( Name, &TmpString );
  3403. return;
  3404. }
  3405. NTSTATUS
  3406. NlpMakeNewCacheEntry(
  3407. ULONG Index
  3408. )
  3409. /*++
  3410. Routine Description:
  3411. This routine creates a secret and a cache entry value for a
  3412. new cache entry with the specified index.
  3413. The secret handle is NOT left open.
  3414. Arguments:
  3415. Index - The index of the cache entry whose name is desired.
  3416. Name - A unicode string large enough to accept the name.
  3417. Return Value:
  3418. STATUS_SUCCESS
  3419. --*/
  3420. {
  3421. NTSTATUS
  3422. NtStatus;
  3423. LOGON_CACHE_ENTRY
  3424. Entry;
  3425. UNICODE_STRING
  3426. ValueName;
  3427. WCHAR
  3428. NameBuffer[32];
  3429. LSAPR_HANDLE
  3430. SecretHandle;
  3431. ValueName.Length = 0;
  3432. ValueName.MaximumLength = 32;
  3433. ValueName.Buffer = &NameBuffer[0];
  3434. NlpMakeCacheEntryName( Index, &ValueName );
  3435. NtStatus = I_LsarOpenSecret( NtLmGlobalPolicyHandle,
  3436. (PLSAPR_UNICODE_STRING) &ValueName,
  3437. DELETE,
  3438. &SecretHandle
  3439. );
  3440. if( NT_SUCCESS( NtStatus ) ) {
  3441. //
  3442. // for Windows2000, we remove old style cache entry related
  3443. // LSA secrets.
  3444. //
  3445. //
  3446. // Deleting and object causes its handle to be closed
  3447. //
  3448. I_LsarDelete( SecretHandle );
  3449. // I_LsarClose( &SecretHandle );
  3450. }
  3451. //
  3452. // Create the cache entry marked as invalid
  3453. //
  3454. RtlZeroMemory( &Entry, sizeof(Entry) );
  3455. Entry.Revision = NLP_CACHE_REVISION;
  3456. Entry.Valid = FALSE;
  3457. NtStatus = NtSetValueKey( NlpCacheHandle,
  3458. &ValueName, // Name
  3459. 0, // TitleIndex
  3460. REG_BINARY, // Type
  3461. &Entry, // Data
  3462. sizeof(LOGON_CACHE_ENTRY) // DataLength
  3463. );
  3464. return(NtStatus);
  3465. }
  3466. NTSTATUS
  3467. NlpEliminateCacheEntry(
  3468. IN ULONG Index
  3469. )
  3470. /*++
  3471. Routine Description:
  3472. Delete the registry value and secret object related to a
  3473. CTE entry.
  3474. Arguments:
  3475. Index - The index of the entry whose value and secret are to
  3476. be deleted. This value is used only to build a name with
  3477. (not to reference the CTE table).
  3478. Return Value:
  3479. --*/
  3480. {
  3481. NTSTATUS
  3482. NtStatus;
  3483. UNICODE_STRING
  3484. ValueName;
  3485. WCHAR
  3486. NameBuffer[32];
  3487. LSAPR_HANDLE
  3488. SecretHandle;
  3489. ValueName.Buffer = &NameBuffer[0];
  3490. ValueName.MaximumLength = 32;
  3491. ValueName.Length = 0;
  3492. NlpMakeCacheEntryName( Index, &ValueName );
  3493. NtStatus = I_LsarOpenSecret(NtLmGlobalPolicyHandle,
  3494. (PLSAPR_UNICODE_STRING) &ValueName,
  3495. DELETE,
  3496. &SecretHandle
  3497. );
  3498. if (NT_SUCCESS(NtStatus)) {
  3499. //
  3500. // Deleting and object causes its handle to be closed
  3501. //
  3502. NtStatus = I_LsarDelete( SecretHandle );
  3503. }
  3504. //
  3505. // Now delete the registry value
  3506. //
  3507. NtStatus = NtDeleteValueKey( NlpCacheHandle, &ValueName );
  3508. return(NtStatus);
  3509. }
  3510. NTSTATUS
  3511. NlpReadCacheEntryByIndex(
  3512. IN ULONG Index,
  3513. OUT PLOGON_CACHE_ENTRY* CacheEntry,
  3514. OUT PULONG EntrySize
  3515. )
  3516. /*++
  3517. Routine Description:
  3518. Reads a cache entry from registry
  3519. Arguments:
  3520. Index - CTE table index of the entry to open.
  3521. This is used to build the entry's value and secret names.
  3522. CacheEntry - pointer to place to return pointer to LOGON_CACHE_ENTRY
  3523. EntrySize - size of returned LOGON_CACHE_ENTRY
  3524. Return Value:
  3525. NTSTATUS
  3526. Success = STATUS_SUCCESS
  3527. *ppEntry points to allocated LOGON_CACHE_ENTRY
  3528. *EntrySize is size of returned data
  3529. Failure = STATUS_NO_MEMORY
  3530. Couldn't allocate buffer for LOGON_CACHE_ENTRY
  3531. --*/
  3532. {
  3533. NTSTATUS
  3534. NtStatus;
  3535. UNICODE_STRING
  3536. ValueName;
  3537. WCHAR
  3538. NameBuffer[32];
  3539. ULONG
  3540. RequiredSize;
  3541. PKEY_VALUE_FULL_INFORMATION
  3542. RegInfo;
  3543. PLOGON_CACHE_ENTRY
  3544. RCacheEntry; // CacheEntry in registry buffer
  3545. BYTE FastBuffer[ 512 ];
  3546. PBYTE SlowBuffer = NULL;
  3547. ValueName.Buffer = &NameBuffer[0];
  3548. ValueName.MaximumLength = 32;
  3549. ValueName.Length = 0;
  3550. NlpMakeCacheEntryName( Index, &ValueName );
  3551. RegInfo = (PKEY_VALUE_FULL_INFORMATION)FastBuffer;
  3552. RequiredSize = sizeof(FastBuffer);
  3553. //
  3554. // perform first query to find out how much buffer to allocate
  3555. //
  3556. NtStatus = NtQueryValueKey(NlpCacheHandle,
  3557. &ValueName,
  3558. KeyValueFullInformation,
  3559. (PVOID)RegInfo,
  3560. RequiredSize,
  3561. &RequiredSize
  3562. );
  3563. if( (NtStatus == STATUS_BUFFER_TOO_SMALL) ||
  3564. (NtStatus == STATUS_BUFFER_OVERFLOW) ) {
  3565. //
  3566. // allocate buffer then do query again, this time receiving data
  3567. //
  3568. SlowBuffer = (PBYTE)AllocateFromHeap(RequiredSize);
  3569. if (SlowBuffer == NULL) {
  3570. return(STATUS_NO_MEMORY);
  3571. }
  3572. RegInfo = (PKEY_VALUE_FULL_INFORMATION)SlowBuffer;
  3573. NtStatus = NtQueryValueKey(NlpCacheHandle,
  3574. &ValueName,
  3575. KeyValueFullInformation,
  3576. (PVOID)RegInfo,
  3577. RequiredSize,
  3578. &RequiredSize
  3579. );
  3580. }
  3581. if (NT_SUCCESS(NtStatus)) {
  3582. #if DBG
  3583. if (DumpCacheInfo) {
  3584. DbgPrint("NlpReadCacheEntryByIndex: Index : %d\n"
  3585. " NtQueryValueKey returns: %d bytes\n"
  3586. " DataOffset=%d\n"
  3587. " DataLength=%d\n",
  3588. Index, RequiredSize, RegInfo->DataOffset, RegInfo->DataLength);
  3589. }
  3590. #endif
  3591. if( RegInfo->DataLength == 0 ) {
  3592. NtStatus = STATUS_INTERNAL_DB_CORRUPTION;
  3593. *CacheEntry = NULL;
  3594. *EntrySize = 0;
  3595. } else {
  3596. RCacheEntry = (PLOGON_CACHE_ENTRY)((PCHAR)RegInfo + RegInfo->DataOffset);
  3597. *EntrySize = RegInfo->DataLength;
  3598. (*CacheEntry) = (PLOGON_CACHE_ENTRY)AllocateFromHeap( (*EntrySize) );
  3599. if ((*CacheEntry) == NULL) {
  3600. NtStatus = STATUS_NO_MEMORY;
  3601. } else {
  3602. RtlCopyMemory( (*CacheEntry),
  3603. RCacheEntry,
  3604. (*EntrySize) );
  3605. }
  3606. }
  3607. }
  3608. if( SlowBuffer )
  3609. FreeToHeap( SlowBuffer );
  3610. return(NtStatus);
  3611. }
  3612. VOID
  3613. NlpAddEntryToActiveList(
  3614. IN ULONG Index
  3615. )
  3616. /*++
  3617. Routine Description:
  3618. Place a CTE entry in the active CTE list.
  3619. This requires placing the entry in the right location in
  3620. the list chronologically. The beginning of the list is
  3621. the most recently updated (or referenced) cache entry.
  3622. The end of the list is the oldest active cache entry.
  3623. Note - The entry may be already in the active list (but
  3624. in the wrong place), or may be on the inactive list.
  3625. It will be removed from whichever list it is on.
  3626. Arguments:
  3627. Index - CTE table index of the entry to make active..
  3628. Return Value:
  3629. None.
  3630. --*/
  3631. {
  3632. PNLP_CTE
  3633. Next;
  3634. //
  3635. // Remove the entry from its current list, and then place it
  3636. // in the active list.
  3637. //
  3638. RemoveEntryList( &NlpCteTable[Index].Link );
  3639. //
  3640. // Now walk the active list until we find a place to insert
  3641. // the entry. It must follow all entries with more recent
  3642. // time stamps.
  3643. //
  3644. Next = (PNLP_CTE)NlpActiveCtes.Flink;
  3645. while (Next != (PNLP_CTE)&NlpActiveCtes) {
  3646. if ( NlpCteTable[Index].Time.QuadPart > Next->Time.QuadPart ) {
  3647. //
  3648. // More recent than this entry - add it here
  3649. //
  3650. break; // out of while-loop
  3651. }
  3652. Next = (PNLP_CTE)(Next->Link.Flink); // Advance to next entry
  3653. }
  3654. //
  3655. // Use the preceding entry as the list head.
  3656. //
  3657. InsertHeadList( Next->Link.Blink, &NlpCteTable[Index].Link );
  3658. //
  3659. // Mark the entry as valid
  3660. //
  3661. NlpCteTable[Index].Active = TRUE;
  3662. return;
  3663. }
  3664. VOID
  3665. NlpAddEntryToInactiveList(
  3666. IN ULONG Index
  3667. )
  3668. /*++
  3669. Routine Description:
  3670. Move the CTE entry to the inactive list.
  3671. It doesn't matter if the entry is already inactive.
  3672. Arguments:
  3673. Index - CTE table index of the entry to make inactive.
  3674. Return Value:
  3675. None.
  3676. --*/
  3677. {
  3678. //
  3679. // Remove the entry from its current list, and then place it
  3680. // in the inactive list.
  3681. //
  3682. RemoveEntryList( &NlpCteTable[Index].Link );
  3683. InsertTailList( &NlpInactiveCtes, &NlpCteTable[Index].Link );
  3684. //
  3685. // Mark the entry as invalid
  3686. //
  3687. NlpCteTable[Index].Active = FALSE;
  3688. return;
  3689. }
  3690. VOID
  3691. NlpGetFreeEntryIndex(
  3692. OUT PULONG Index
  3693. )
  3694. /*++
  3695. Routine Description:
  3696. This routine returns the index of either a free entry,
  3697. or, lacking any free entries, the oldest active entry.
  3698. The entry is left on the list it is already on. If it
  3699. is used by the caller, then the caller must ensure it is
  3700. re-assigned to the active list (using NlpAddEntryToActiveList()).
  3701. This routine is only callable if the cache is enabled (that is,
  3702. NlpCacheControl.Entries != 0).
  3703. Arguments:
  3704. Index - Receives the index of the next available entry.
  3705. Return Value:
  3706. None.
  3707. --*/
  3708. {
  3709. //
  3710. // See if the Inactive list is empty.
  3711. //
  3712. if (NlpInactiveCtes.Flink != &NlpInactiveCtes) {
  3713. (*Index) = ((PNLP_CTE)(NlpInactiveCtes.Flink))->Index;
  3714. } else {
  3715. //
  3716. // Have to return the oldest active entry.
  3717. //
  3718. (*Index) = ((PNLP_CTE)(NlpActiveCtes.Blink))->Index;
  3719. }
  3720. return;
  3721. }
  3722. /////////////////////////////////////////////////////////////////////////
  3723. // //
  3724. // Diagnostic support services //
  3725. // //
  3726. /////////////////////////////////////////////////////////////////////////
  3727. //
  3728. // diagnostic dump routines
  3729. //
  3730. #if DBG
  3731. PCHAR
  3732. DumpOwfPasswordToString(
  3733. OUT PCHAR Buffer,
  3734. IN PLM_OWF_PASSWORD Password
  3735. )
  3736. {
  3737. int i;
  3738. PCHAR bufptr;
  3739. for (i = 0, bufptr = Buffer; i < sizeof(*Password); ++i) {
  3740. sprintf(bufptr, "%02.2x ", ((PCHAR)Password)[i] & 0xff);
  3741. bufptr += 3;
  3742. }
  3743. return Buffer;
  3744. }
  3745. VOID
  3746. DumpLogonInfo(
  3747. IN PNETLOGON_LOGON_IDENTITY_INFO LogonInfo
  3748. )
  3749. {
  3750. DbgPrint( "\n"
  3751. "NETLOGON_INTERACTIVE_INFO:\n"
  3752. "DomainName : \"%*.*ws\"\n"
  3753. "UserName : \"%*.*ws\"\n"
  3754. "Parm Ctrl : %u (%x)\n"
  3755. "LogonId : %u.%u (%x.%x)\n"
  3756. "Workstation : \"%*.*ws\"\n",
  3757. LogonInfo->LogonDomainName.Length/sizeof(WCHAR),
  3758. LogonInfo->LogonDomainName.Length/sizeof(WCHAR),
  3759. LogonInfo->LogonDomainName.Buffer,
  3760. LogonInfo->UserName.Length/sizeof(WCHAR),
  3761. LogonInfo->UserName.Length/sizeof(WCHAR),
  3762. LogonInfo->UserName.Buffer,
  3763. LogonInfo->ParameterControl,
  3764. LogonInfo->ParameterControl,
  3765. LogonInfo->LogonId.HighPart,
  3766. LogonInfo->LogonId.LowPart,
  3767. LogonInfo->LogonId.HighPart,
  3768. LogonInfo->LogonId.LowPart,
  3769. LogonInfo->Workstation.Length/sizeof(WCHAR),
  3770. LogonInfo->Workstation.Length/sizeof(WCHAR),
  3771. LogonInfo->Workstation.Buffer
  3772. );
  3773. }
  3774. char*
  3775. MapWeekday(
  3776. IN CSHORT Weekday
  3777. )
  3778. {
  3779. switch (Weekday) {
  3780. case 0: return "Sunday";
  3781. case 1: return "Monday";
  3782. case 2: return "Tuesday";
  3783. case 3: return "Wednesday";
  3784. case 4: return "Thursday";
  3785. case 5: return "Friday";
  3786. case 6: return "Saturday";
  3787. }
  3788. return "???";
  3789. }
  3790. VOID
  3791. DumpTime(
  3792. IN LPSTR String,
  3793. IN POLD_LARGE_INTEGER OldTime
  3794. )
  3795. {
  3796. TIME_FIELDS tf;
  3797. LARGE_INTEGER Time;
  3798. OLD_TO_NEW_LARGE_INTEGER( (*OldTime), Time );
  3799. RtlTimeToTimeFields(&Time, &tf);
  3800. DbgPrint("%s%02d:%02d:%02d.%03d %02d/%02d/%d (%s [%d])\n",
  3801. String,
  3802. tf.Hour,
  3803. tf.Minute,
  3804. tf.Second,
  3805. tf.Milliseconds,
  3806. tf.Month,
  3807. tf.Day,
  3808. tf.Year,
  3809. MapWeekday(tf.Weekday),
  3810. tf.Weekday
  3811. );
  3812. }
  3813. VOID
  3814. DumpGroupIds(
  3815. IN LPSTR String,
  3816. IN ULONG Count,
  3817. IN PGROUP_MEMBERSHIP GroupIds
  3818. )
  3819. {
  3820. DbgPrint(String);
  3821. if (!Count) {
  3822. DbgPrint("No group IDs!\n");
  3823. } else {
  3824. char tab[80];
  3825. memset(tab, ' ', strlen(String));
  3826. // tab[strcspn(String, "%")] = 0;
  3827. tab[strlen(String)] = 0;
  3828. while (Count--) {
  3829. DbgPrint("%d, %d\n", GroupIds->RelativeId, GroupIds->Attributes);
  3830. if (Count) {
  3831. DbgPrint(tab);
  3832. }
  3833. ++GroupIds;
  3834. }
  3835. }
  3836. }
  3837. VOID
  3838. DumpSessKey(
  3839. IN LPSTR String,
  3840. IN PUSER_SESSION_KEY Key
  3841. )
  3842. {
  3843. int len;
  3844. DbgPrint(String);
  3845. DbgPrint("%02.2x-%02.2x-%02.2x-%02.2x-%02.2x-%02.2x-%02.2x-%02.2x\n",
  3846. ((PUCHAR)&Key->data[0])[0],
  3847. ((PUCHAR)&Key->data[0])[1],
  3848. ((PUCHAR)&Key->data[0])[2],
  3849. ((PUCHAR)&Key->data[0])[3],
  3850. ((PUCHAR)&Key->data[0])[4],
  3851. ((PUCHAR)&Key->data[0])[5],
  3852. ((PUCHAR)&Key->data[0])[6],
  3853. ((PUCHAR)&Key->data[0])[7]
  3854. );
  3855. len = strlen(String);
  3856. DbgPrint("%-*.*s", len, len, "");
  3857. DbgPrint("%02.2x-%02.2x-%02.2x-%02.2x-%02.2x-%02.2x-%02.2x-%02.2x\n",
  3858. ((PUCHAR)&Key->data[1])[0],
  3859. ((PUCHAR)&Key->data[1])[1],
  3860. ((PUCHAR)&Key->data[1])[2],
  3861. ((PUCHAR)&Key->data[1])[3],
  3862. ((PUCHAR)&Key->data[1])[4],
  3863. ((PUCHAR)&Key->data[1])[5],
  3864. ((PUCHAR)&Key->data[1])[6],
  3865. ((PUCHAR)&Key->data[1])[7]
  3866. );
  3867. }
  3868. VOID
  3869. DumpSid(
  3870. LPSTR String,
  3871. PISID Sid
  3872. )
  3873. {
  3874. DbgPrint(String);
  3875. if ( Sid == NULL ) {
  3876. DbgPrint(0, "(null)\n");
  3877. } else {
  3878. UNICODE_STRING SidString;
  3879. NTSTATUS Status;
  3880. Status = RtlConvertSidToUnicodeString( &SidString, Sid, TRUE );
  3881. if ( !NT_SUCCESS(Status) ) {
  3882. DbgPrint("Invalid 0x%lX\n", Status );
  3883. } else {
  3884. DbgPrint( "%wZ\n", &SidString );
  3885. RtlFreeUnicodeString( &SidString );
  3886. }
  3887. }
  3888. }
  3889. VOID
  3890. DumpAccountInfo(
  3891. IN PNETLOGON_VALIDATION_SAM_INFO4 AccountInfo
  3892. )
  3893. {
  3894. DbgPrint( "\n"
  3895. "NETLOGON_VALIDATION_SAM_INFO:\n");
  3896. DumpTime( "LogonTime : ", &AccountInfo->LogonTime);
  3897. DumpTime( "LogoffTime : ", &AccountInfo->LogoffTime);
  3898. DumpTime( "KickOffTime : ", &AccountInfo->KickOffTime);
  3899. DumpTime( "PasswordLastSet : ", &AccountInfo->PasswordLastSet);
  3900. DumpTime( "PasswordCanChange : ", &AccountInfo->PasswordCanChange);
  3901. DumpTime( "PasswordMustChange : ", &AccountInfo->PasswordMustChange);
  3902. DbgPrint( "EffectiveName : \"%*.*ws\"\n"
  3903. "Upn : \"%*.*ws\"\n"
  3904. "FullName : \"%*.*ws\"\n"
  3905. "LogonScript : \"%*.*ws\"\n"
  3906. "ProfilePath : \"%*.*ws\"\n"
  3907. "HomeDirectory : \"%*.*ws\"\n"
  3908. "HomeDirectoryDrive : \"%*.*ws\"\n"
  3909. "LogonCount : %d\n"
  3910. "BadPasswordCount : %d\n"
  3911. "UserId : %d\n"
  3912. "PrimaryGroupId : %d\n"
  3913. "GroupCount : %d\n",
  3914. AccountInfo->EffectiveName.Length/sizeof(WCHAR),
  3915. AccountInfo->EffectiveName.Length/sizeof(WCHAR),
  3916. AccountInfo->EffectiveName.Buffer,
  3917. AccountInfo->Upn.Length/sizeof(WCHAR),
  3918. AccountInfo->Upn.Length/sizeof(WCHAR),
  3919. AccountInfo->Upn.Buffer,
  3920. AccountInfo->FullName.Length/sizeof(WCHAR),
  3921. AccountInfo->FullName.Length/sizeof(WCHAR),
  3922. AccountInfo->FullName.Buffer,
  3923. AccountInfo->LogonScript.Length/sizeof(WCHAR),
  3924. AccountInfo->LogonScript.Length/sizeof(WCHAR),
  3925. AccountInfo->LogonScript.Buffer,
  3926. AccountInfo->ProfilePath.Length/sizeof(WCHAR),
  3927. AccountInfo->ProfilePath.Length/sizeof(WCHAR),
  3928. AccountInfo->ProfilePath.Buffer,
  3929. AccountInfo->HomeDirectory.Length/sizeof(WCHAR),
  3930. AccountInfo->HomeDirectory.Length/sizeof(WCHAR),
  3931. AccountInfo->HomeDirectory.Buffer,
  3932. AccountInfo->HomeDirectoryDrive.Length/sizeof(WCHAR),
  3933. AccountInfo->HomeDirectoryDrive.Length/sizeof(WCHAR),
  3934. AccountInfo->HomeDirectoryDrive.Buffer,
  3935. AccountInfo->LogonCount,
  3936. AccountInfo->BadPasswordCount,
  3937. AccountInfo->UserId,
  3938. AccountInfo->PrimaryGroupId,
  3939. AccountInfo->GroupCount
  3940. );
  3941. DumpGroupIds("GroupIds : ",
  3942. AccountInfo->GroupCount,
  3943. AccountInfo->GroupIds
  3944. );
  3945. DbgPrint( "UserFlags : 0x%08x\n",
  3946. AccountInfo->UserFlags
  3947. );
  3948. DumpSessKey("UserSessionKey : ", &AccountInfo->UserSessionKey);
  3949. DbgPrint( "LogonServer : \"%*.*ws\"\n"
  3950. "LogonDomainName : \"%*.*ws\"\n"
  3951. "DnsLogonDomainName : \"%*.*ws\"\n",
  3952. AccountInfo->LogonServer.Length/sizeof(WCHAR),
  3953. AccountInfo->LogonServer.Length/sizeof(WCHAR),
  3954. AccountInfo->LogonServer.Buffer,
  3955. AccountInfo->LogonDomainName.Length/sizeof(WCHAR),
  3956. AccountInfo->LogonDomainName.Length/sizeof(WCHAR),
  3957. AccountInfo->LogonDomainName.Buffer,
  3958. AccountInfo->DnsLogonDomainName.Length/sizeof(WCHAR),
  3959. AccountInfo->DnsLogonDomainName.Length/sizeof(WCHAR),
  3960. AccountInfo->DnsLogonDomainName.Buffer
  3961. );
  3962. DumpSid( "LogonDomainId : ", (PISID)AccountInfo->LogonDomainId);
  3963. }
  3964. VOID
  3965. DumpCacheEntry(
  3966. IN ULONG Index,
  3967. IN PLOGON_CACHE_ENTRY pEntry
  3968. )
  3969. {
  3970. PUCHAR dataptr;
  3971. ULONG length;
  3972. DbgPrint( "\n"
  3973. "LOGON_CACHE_ENTRY:\n"
  3974. "CTE Index : %d\n", Index);
  3975. if (pEntry->Valid != TRUE) {
  3976. DbgPrint( "State : INVALID\n");
  3977. return;
  3978. }
  3979. dataptr = (PUCHAR)(pEntry+1);
  3980. length = pEntry->UserNameLength;
  3981. DbgPrint( "State : VALID\n");
  3982. DbgPrint( "UserName : \"%*.*ws\"\n", length/2, length/2, dataptr);
  3983. dataptr = ROUND_UP_POINTER(dataptr+length, sizeof(ULONG));
  3984. length = pEntry->DomainNameLength;
  3985. DbgPrint( "DomainName : \"%*.*ws\"\n", length/2, length/2, dataptr);
  3986. dataptr = ROUND_UP_POINTER(dataptr+length, sizeof(ULONG));
  3987. length = pEntry->DnsDomainNameLength;
  3988. DbgPrint( "DnsDomainname : \"%*.*ws\"\n", length/2, length/2, dataptr);
  3989. dataptr = ROUND_UP_POINTER(dataptr+length, sizeof(ULONG));
  3990. length = pEntry->UpnLength;
  3991. DbgPrint( "Upn : \"%*.*ws\"\n", length/2, length/2, dataptr);
  3992. dataptr = ROUND_UP_POINTER(dataptr+length, sizeof(ULONG));
  3993. length = pEntry->EffectiveNameLength;
  3994. DbgPrint( "EffectiveName : \"%*.*ws\"\n", length/2, length/2, dataptr);
  3995. dataptr = ROUND_UP_POINTER(dataptr+length, sizeof(ULONG));
  3996. length = pEntry->FullNameLength;
  3997. DbgPrint( "FullName : \"%*.*ws\"\n", length/2, length/2, dataptr);
  3998. dataptr = ROUND_UP_POINTER(dataptr+length, sizeof(ULONG));
  3999. length = pEntry->LogonScriptLength;
  4000. DbgPrint( "LogonScript : \"%*.*ws\"\n", length/2, length/2, dataptr);
  4001. dataptr = ROUND_UP_POINTER(dataptr+length, sizeof(ULONG));
  4002. length = pEntry->ProfilePathLength;
  4003. DbgPrint( "ProfilePath : \"%*.*ws\"\n", length/2, length/2, dataptr);
  4004. dataptr = ROUND_UP_POINTER(dataptr+length, sizeof(ULONG));
  4005. length = pEntry->HomeDirectoryLength;
  4006. DbgPrint( "HomeDirectory : \"%*.*ws\"\n", length/2, length/2, dataptr);
  4007. dataptr = ROUND_UP_POINTER(dataptr+length, sizeof(ULONG));
  4008. length = pEntry->HomeDirectoryDriveLength;
  4009. DbgPrint( "HomeDirectoryDrive : \"%*.*ws\"\n", length/2, length/2, dataptr);
  4010. dataptr = ROUND_UP_POINTER(dataptr+length, sizeof(ULONG));
  4011. DbgPrint( "UserId : %d\n"
  4012. "PrimaryGroupId : %d\n"
  4013. "GroupCount : %d\n",
  4014. pEntry->UserId,
  4015. pEntry->PrimaryGroupId,
  4016. pEntry->GroupCount
  4017. );
  4018. DumpGroupIds(
  4019. "GroupIds : ",
  4020. pEntry->GroupCount,
  4021. (PGROUP_MEMBERSHIP)dataptr
  4022. );
  4023. dataptr = ROUND_UP_POINTER((dataptr+pEntry->GroupCount * sizeof(GROUP_MEMBERSHIP)), sizeof(ULONG));
  4024. length = pEntry->LogonDomainNameLength;
  4025. DbgPrint( "LogonDomainName : \"%*.*ws\"\n", length/2, length/2, dataptr);
  4026. dataptr = ROUND_UP_POINTER(dataptr+length, sizeof(ULONG));
  4027. if (pEntry->SidCount) {
  4028. ULONG i, sidLength;
  4029. PULONG SidAttributes = (PULONG) dataptr;
  4030. dataptr = ROUND_UP_POINTER(dataptr + pEntry->SidCount * sizeof(ULONG), sizeof(ULONG));
  4031. for (i = 0; i < pEntry->SidCount ; i++ ) {
  4032. sidLength = RtlLengthSid ((PSID) dataptr);
  4033. DumpSid("Sid : ",(PISID) dataptr);
  4034. DbgPrint("\tAttributes = 0x%x\n",SidAttributes[i]);
  4035. dataptr = ROUND_UP_POINTER(dataptr + sidLength, sizeof(ULONG));
  4036. }
  4037. }
  4038. DumpSid( "LogonDomainId : ", (PISID)dataptr);
  4039. }
  4040. #endif