ReactOS 0.4.15-dev-7674-gc0b4db1
sidcache.c File Reference
#include "precomp.h"
#include <ntsecapi.h>
#include <debug.h>
Include dependency graph for sidcache.c:

Go to the source code of this file.

Classes

struct  _SIDCACHEMGR
 
struct  _SIDCACHECALLBACKINFO
 
struct  _SIDQUEUEENTRY
 
struct  _SIDCACHEENTRY
 

Macros

#define NDEBUG
 
#define HandleToScm(Handle)   (PSIDCACHEMGR)(Handle)
 
#define ScmToHandle(Scm)   (HANDLE)(Scm)
 

Typedefs

typedef struct _SIDCACHEMGR SIDCACHEMGR
 
typedef struct _SIDCACHEMGRPSIDCACHEMGR
 
typedef struct _SIDCACHECALLBACKINFO SIDCACHECALLBACKINFO
 
typedef struct _SIDCACHECALLBACKINFOPSIDCACHECALLBACKINFO
 
typedef struct _SIDQUEUEENTRY SIDQUEUEENTRY
 
typedef struct _SIDQUEUEENTRYPSIDQUEUEENTRY
 
typedef struct _SIDCACHEENTRY SIDCACHEENTRY
 
typedef struct _SIDCACHEENTRYPSIDCACHEENTRY
 

Functions

static VOID FreeQueueEntry (IN PSIDCACHEMGR scm, IN PSIDQUEUEENTRY QueueEntry)
 
static VOID FreeCacheEntry (IN PSIDCACHEMGR scm, IN PSIDCACHEENTRY CacheEntry)
 
static VOID CleanupSidCacheMgr (IN PSIDCACHEMGR scm)
 
static PSIDCACHEMGR ReferenceSidCacheMgr (IN HANDLE SidCacheMgr)
 
static VOID DereferenceSidCacheMgr (IN PSIDCACHEMGR scm)
 
static BOOL OpenLSAPolicyHandle (IN LPWSTR SystemName, IN ACCESS_MASK DesiredAccess, OUT PLSA_HANDLE PolicyHandle)
 
static BOOL LookupSidInformation (IN PSIDCACHEMGR scm, IN PSID pSid, OUT PSIDREQRESULT *ReqResult)
 
static BOOL FindSidInCache (IN PSIDCACHEMGR scm, IN PSID pSid, OUT PSIDREQRESULT *ReqResult)
 
static VOID CacheLookupResults (IN PSIDCACHEMGR scm, IN PSID pSid, IN PSIDREQRESULT ReqResult)
 
static DWORD WINAPI LookupThreadProc (IN LPVOID lpParameter)
 
HANDLE CreateSidCacheMgr (IN HANDLE Heap, IN LPCWSTR SystemName)
 
VOID DestroySidCacheMgr (IN HANDLE SidCacheMgr)
 
static BOOL QueueSidLookup (IN PSIDCACHEMGR scm, IN PSID pSid, IN PSIDREQCOMPLETIONPROC CompletionProc, IN PVOID Context)
 
VOID DequeueSidLookup (IN HANDLE SidCacheMgr, IN PSID pSid)
 
VOID ReferenceSidReqResult (IN HANDLE SidCacheMgr, IN PSIDREQRESULT ReqResult)
 
VOID DereferenceSidReqResult (IN HANDLE SidCacheMgr, IN PSIDREQRESULT ReqResult)
 
BOOL LookupSidCache (IN HANDLE SidCacheMgr, IN PSID pSid, IN PSIDREQCOMPLETIONPROC CompletionProc, IN PVOID Context)
 

Macro Definition Documentation

◆ HandleToScm

#define HandleToScm (   Handle)    (PSIDCACHEMGR)(Handle)

Definition at line 36 of file sidcache.c.

◆ NDEBUG

#define NDEBUG

Definition at line 33 of file sidcache.c.

◆ ScmToHandle

#define ScmToHandle (   Scm)    (HANDLE)(Scm)

Definition at line 37 of file sidcache.c.

Typedef Documentation

◆ PSIDCACHECALLBACKINFO

◆ PSIDCACHEENTRY

◆ PSIDCACHEMGR

◆ PSIDQUEUEENTRY

◆ SIDCACHECALLBACKINFO

◆ SIDCACHEENTRY

◆ SIDCACHEMGR

◆ SIDQUEUEENTRY

Function Documentation

◆ CacheLookupResults()

static VOID CacheLookupResults ( IN PSIDCACHEMGR  scm,
IN PSID  pSid,
IN PSIDREQRESULT  ReqResult 
)
static

Definition at line 401 of file sidcache.c.

404{
405 PSIDCACHEENTRY CacheEntry;
406 DWORD SidLen;
407 SIZE_T AccountNameLen = 0;
408 SIZE_T DomainNameLen = 0;
409 SIZE_T CacheEntrySize = sizeof(SIDCACHEENTRY);
410
411 /* NOTE: assumes the lists are locked! */
412
413 SidLen = GetLengthSid(pSid);
414 CacheEntrySize += SidLen;
415
416 AccountNameLen = wcslen(ReqResult->AccountName);
417 CacheEntrySize += (AccountNameLen + 1) * sizeof(WCHAR);
418
419 DomainNameLen = wcslen(ReqResult->DomainName);
420 CacheEntrySize += (wcslen(ReqResult->DomainName) + 1) * sizeof(WCHAR);
421
422 CacheEntry = HeapAlloc(scm->Heap,
423 0,
424 CacheEntrySize);
425 if (CacheEntry != NULL)
426 {
427 PWSTR lpBuf = (PWSTR)((ULONG_PTR)(CacheEntry + 1) + SidLen);
428
429 CacheEntry->SidNameUse = ReqResult->SidNameUse;
430
431 /* append the SID */
432 CopySid(SidLen,
433 (PSID)(CacheEntry + 1),
434 pSid);
435
436 /* append the strings */
437 CacheEntry->AccountName = lpBuf;
438 wcscpy(lpBuf,
439 ReqResult->AccountName);
440 lpBuf += AccountNameLen + 1;
441
442 CacheEntry->DomainName = lpBuf;
443 wcscpy(lpBuf,
444 ReqResult->DomainName);
445 lpBuf += DomainNameLen + 1;
446
447 /* add the entry to the cache list */
448 InsertTailList(&scm->CacheListHead,
449 &CacheEntry->ListEntry);
450 }
451}
#define NULL
Definition: types.h:112
BOOL WINAPI CopySid(DWORD nDestinationSidLength, PSID pDestinationSid, PSID pSourceSid)
Definition: security.c:712
DWORD WINAPI GetLengthSid(PSID pSid)
Definition: security.c:919
#define HeapAlloc
Definition: compat.h:733
#define InsertTailList(ListHead, Entry)
unsigned long DWORD
Definition: ntddk_ex.h:95
_CRTIMP size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
static PSID pSid
Definition: security.c:74
_CRTIMP wchar_t *__cdecl wcscpy(_Out_writes_z_(_String_length_(_Source)+1) wchar_t *_Dest, _In_z_ const wchar_t *_Source)
struct _SIDCACHEENTRY SIDCACHEENTRY
SID_NAME_USE SidNameUse
Definition: sidcache.c:73
PWSTR DomainName
Definition: sidcache.c:75
LIST_ENTRY ListEntry
Definition: sidcache.c:72
PWSTR AccountName
Definition: sidcache.c:74
uint16_t * PWSTR
Definition: typedefs.h:56
ULONG_PTR SIZE_T
Definition: typedefs.h:80
uint32_t ULONG_PTR
Definition: typedefs.h:65
__wchar_t WCHAR
Definition: xmlstorage.h:180

Referenced by LookupSidCache(), and LookupThreadProc().

◆ CleanupSidCacheMgr()

static VOID CleanupSidCacheMgr ( IN PSIDCACHEMGR  scm)
static

Definition at line 112 of file sidcache.c.

113{
114 LsaClose(scm->LsaHandle);
115 CloseHandle(scm->LookupEvent);
116 CloseHandle(scm->LookupThread);
117
118 /* delete the queue */
119 while (!IsListEmpty(&scm->QueueListHead))
120 {
121 PSIDQUEUEENTRY QueueEntry;
122
123 QueueEntry = CONTAINING_RECORD(scm->QueueListHead.Flink,
125 ListEntry);
126 FreeQueueEntry(scm,
127 QueueEntry);
128 }
129
130 /* delete the cache */
131 while (!IsListEmpty(&scm->CacheListHead))
132 {
133 PSIDCACHEENTRY CacheEntry;
134
135 CacheEntry = CONTAINING_RECORD(scm->CacheListHead.Flink,
137 ListEntry);
138 FreeCacheEntry(scm,
139 CacheEntry);
140 }
141
142 DeleteCriticalSection(&scm->Lock);
143}
NTSTATUS WINAPI LsaClose(IN LSA_HANDLE ObjectHandle)
Definition: lsa.c:194
#define CloseHandle
Definition: compat.h:739
#define IsListEmpty(ListHead)
Definition: env_spec_w32.h:954
static VOID FreeQueueEntry(IN PSIDCACHEMGR scm, IN PSIDQUEUEENTRY QueueEntry)
Definition: sidcache.c:81
static VOID FreeCacheEntry(IN PSIDCACHEMGR scm, IN PSIDCACHEENTRY CacheEntry)
Definition: sidcache.c:100
#define CONTAINING_RECORD(address, type, field)
Definition: typedefs.h:260
void WINAPI DeleteCriticalSection(PCRITICAL_SECTION)

Referenced by LookupThreadProc().

◆ CreateSidCacheMgr()

HANDLE CreateSidCacheMgr ( IN HANDLE  Heap,
IN LPCWSTR  SystemName 
)

Definition at line 596 of file sidcache.c.

598{
599 PSIDCACHEMGR scm;
600
601 if (SystemName == NULL)
602 SystemName = L"";
603
604 scm = HeapAlloc(Heap,
605 0,
607 SystemName[wcslen(SystemName) + 1]));
608 if (scm != NULL)
609 {
610 /* zero the static part of the structure */
611 ZeroMemory(scm,
613 SystemName));
614
615 scm->RefCount = 1;
616 scm->Heap = Heap;
617
618 wcscpy(scm->SystemName,
619 SystemName);
620
624
626 FALSE,
627 FALSE,
628 NULL);
629 if (scm->LookupEvent == NULL)
630 {
631 goto Cleanup;
632 }
633
636 &scm->LsaHandle))
637 {
638 goto Cleanup;
639 }
640
642 0,
644 scm,
645 0,
646 NULL);
647 if (scm->LookupThread == NULL)
648 {
649Cleanup:
650 if (scm->LookupEvent != NULL)
651 {
653 }
654
655 if (scm->LsaHandle != NULL)
656 {
657 LsaClose(scm->LsaHandle);
658 }
659
660 HeapFree(Heap,
661 0,
662 scm);
663 scm = NULL;
664 }
665 }
666 else
667 {
669 }
670
671 return (HANDLE)scm;
672}
#define ERROR_NOT_ENOUGH_MEMORY
Definition: dderror.h:7
#define FALSE
Definition: types.h:117
#define SetLastError(x)
Definition: compat.h:752
#define HeapFree(x, y, z)
Definition: compat.h:735
HANDLE WINAPI DECLSPEC_HOTPATCH CreateThread(IN LPSECURITY_ATTRIBUTES lpThreadAttributes, IN DWORD dwStackSize, IN LPTHREAD_START_ROUTINE lpStartAddress, IN LPVOID lpParameter, IN DWORD dwCreationFlags, OUT LPDWORD lpThreadId)
Definition: thread.c:137
static const WCHAR Cleanup[]
Definition: register.c:80
#define InitializeListHead(ListHead)
Definition: env_spec_w32.h:944
#define POLICY_VIEW_LOCAL_INFORMATION
Definition: ntsecapi.h:61
#define POLICY_LOOKUP_NAMES
Definition: ntsecapi.h:72
#define L(x)
Definition: ntvdm.h:50
static DWORD WINAPI LookupThreadProc(IN LPVOID lpParameter)
Definition: sidcache.c:455
static BOOL OpenLSAPolicyHandle(IN LPWSTR SystemName, IN ACCESS_MASK DesiredAccess, OUT PLSA_HANDLE PolicyHandle)
Definition: sidcache.c:172
WCHAR SystemName[1]
Definition: sidcache.c:50
HANDLE LookupEvent
Definition: sidcache.c:48
LIST_ENTRY QueueListHead
Definition: sidcache.c:44
volatile LONG RefCount
Definition: sidcache.c:41
CRITICAL_SECTION Lock
Definition: sidcache.c:43
LSA_HANDLE LsaHandle
Definition: sidcache.c:42
LIST_ENTRY CacheListHead
Definition: sidcache.c:46
HANDLE Heap
Definition: sidcache.c:47
HANDLE LookupThread
Definition: sidcache.c:49
VOID WINAPI InitializeCriticalSection(OUT LPCRITICAL_SECTION lpCriticalSection)
Definition: synch.c:751
#define FIELD_OFFSET(t, f)
Definition: typedefs.h:255
#define ZeroMemory
Definition: winbase.h:1712
#define CreateEvent
Definition: winbase.h:3683

Referenced by CreateSecurityPage().

◆ DequeueSidLookup()

VOID DequeueSidLookup ( IN HANDLE  SidCacheMgr,
IN PSID  pSid 
)

Definition at line 791 of file sidcache.c.

793{
794 PLIST_ENTRY CurrentEntry;
795 PSIDQUEUEENTRY QueueEntry;
796 PSIDCACHEMGR scm;
797
798 scm = ReferenceSidCacheMgr(SidCacheMgr);
799 if (scm != NULL)
800 {
802
803 if (scm->QueueLookingUp != NULL &&
805 (PSID)(scm->QueueLookingUp + 1)))
806 {
807 /* don't free the queue lookup item! this will be
808 done in the lookup thread */
809 scm->QueueLookingUp = NULL;
810 }
811 else
812 {
813 for (CurrentEntry = scm->QueueListHead.Flink;
814 CurrentEntry != &scm->QueueListHead;
815 CurrentEntry = CurrentEntry->Flink)
816 {
817 QueueEntry = CONTAINING_RECORD(CurrentEntry,
819 ListEntry);
820
821 if (EqualSid(pSid,
822 (PSID)(QueueEntry + 1)))
823 {
824 FreeQueueEntry(scm,
825 QueueEntry);
826 break;
827 }
828 }
829 }
830
832
834 }
835}
BOOL WINAPI EqualSid(PSID pSid1, PSID pSid2)
Definition: security.c:829
static VOID DereferenceSidCacheMgr(IN PSIDCACHEMGR scm)
Definition: sidcache.c:161
static PSIDCACHEMGR ReferenceSidCacheMgr(IN HANDLE SidCacheMgr)
Definition: sidcache.c:147
Definition: typedefs.h:120
struct _LIST_ENTRY * Flink
Definition: typedefs.h:121
struct _SIDQUEUEENTRY * QueueLookingUp
Definition: sidcache.c:45
void WINAPI LeaveCriticalSection(LPCRITICAL_SECTION)
void WINAPI EnterCriticalSection(LPCRITICAL_SECTION)

◆ DereferenceSidCacheMgr()

static VOID DereferenceSidCacheMgr ( IN PSIDCACHEMGR  scm)
static

Definition at line 161 of file sidcache.c.

162{
163 if (InterlockedDecrement(&scm->RefCount) == 0)
164 {
165 /* Signal the lookup thread so it can terminate */
166 SetEvent(scm->LookupEvent);
167 }
168}
#define InterlockedDecrement
Definition: armddk.h:52
BOOL WINAPI DECLSPEC_HOTPATCH SetEvent(IN HANDLE hEvent)
Definition: synch.c:733

Referenced by DequeueSidLookup(), DereferenceSidReqResult(), DestroySidCacheMgr(), LookupSidCache(), and ReferenceSidReqResult().

◆ DereferenceSidReqResult()

VOID DereferenceSidReqResult ( IN HANDLE  SidCacheMgr,
IN PSIDREQRESULT  ReqResult 
)

Definition at line 855 of file sidcache.c.

857{
858 PSIDCACHEMGR scm;
859
860 scm = ReferenceSidCacheMgr(SidCacheMgr);
861 if (scm != NULL)
862 {
863 if (InterlockedDecrement(&ReqResult->RefCount) == 0)
864 {
865 HeapFree(scm->Heap,
866 0,
867 ReqResult);
868 }
869
871 }
872}

Referenced by FreePrincipalsList(), LookupSidCache(), LookupThreadProc(), and UpdatePrincipalListItem().

◆ DestroySidCacheMgr()

VOID DestroySidCacheMgr ( IN HANDLE  SidCacheMgr)

Definition at line 676 of file sidcache.c.

677{
678 PSIDCACHEMGR scm = HandleToScm(SidCacheMgr);
679
680 if (scm != NULL)
681 {
682 /* remove the keep-alive reference */
684 }
685}
#define HandleToScm(Handle)
Definition: sidcache.c:36

Referenced by CreateSecurityPage(), and DestroySecurityPage().

◆ FindSidInCache()

static BOOL FindSidInCache ( IN PSIDCACHEMGR  scm,
IN PSID  pSid,
OUT PSIDREQRESULT ReqResult 
)
static

Definition at line 336 of file sidcache.c.

339{
340 PSIDCACHEENTRY CacheEntry;
341 PLIST_ENTRY CurrentEntry;
342 PSIDREQRESULT ReqRes;
343 BOOL Ret = FALSE;
344
345 /* NOTE: assumes the lists are locked! */
346
347 for (CurrentEntry = scm->CacheListHead.Flink;
348 CurrentEntry != &scm->CacheListHead;
349 CurrentEntry = CurrentEntry->Flink)
350 {
351 CacheEntry = CONTAINING_RECORD(CurrentEntry,
353 ListEntry);
354
355 if (EqualSid(pSid,
356 (PSID)(CacheEntry + 1)))
357 {
358 SIZE_T ReqResultSize;
359 ULONG AccountNameLen, DomainNameLen;
360
361 Ret = TRUE;
362
363 AccountNameLen = wcslen(CacheEntry->AccountName);
364 DomainNameLen = wcslen(CacheEntry->DomainName);
365
366 ReqResultSize = sizeof(SIDREQRESULT) +
367 (((AccountNameLen + 1) +
368 (DomainNameLen + 1)) * sizeof(WCHAR));
369
370 ReqRes = HeapAlloc(scm->Heap,
371 0,
372 ReqResultSize);
373 if (ReqRes != NULL)
374 {
375 PWSTR Buffer = (PWSTR)(ReqRes + 1);
376
377 ReqRes->RefCount = 1;
378
379 ReqRes->AccountName = Buffer;
380 wcscpy(ReqRes->AccountName,
381 CacheEntry->AccountName);
382 Buffer += AccountNameLen + 1;
383
384 ReqRes->DomainName = Buffer;
385 wcscpy(ReqRes->DomainName,
386 CacheEntry->DomainName);
387 }
388
389 /* return the result, even if we weren't unable to
390 allocate enough memory! */
391 *ReqResult = ReqRes;
392 break;
393 }
394 }
395
396 return Ret;
397}
Definition: bufpool.h:45
#define TRUE
Definition: types.h:120
struct _SIDREQRESULT SIDREQRESULT
unsigned int BOOL
Definition: ntddk_ex.h:94
LONG RefCount
Definition: precomp.h:169
LPWSTR AccountName
Definition: precomp.h:171
LPWSTR DomainName
Definition: precomp.h:172
uint32_t ULONG
Definition: typedefs.h:59

Referenced by LookupSidCache(), and LookupThreadProc().

◆ FreeCacheEntry()

static VOID FreeCacheEntry ( IN PSIDCACHEMGR  scm,
IN PSIDCACHEENTRY  CacheEntry 
)
static

Definition at line 100 of file sidcache.c.

102{
103 RemoveEntryList(&CacheEntry->ListEntry);
104
105 HeapFree(scm->Heap,
106 0,
107 CacheEntry);
108}
#define RemoveEntryList(Entry)
Definition: env_spec_w32.h:986

Referenced by CleanupSidCacheMgr().

◆ FreeQueueEntry()

static VOID FreeQueueEntry ( IN PSIDCACHEMGR  scm,
IN PSIDQUEUEENTRY  QueueEntry 
)
static

Definition at line 81 of file sidcache.c.

83{
84 if (QueueEntry->ListEntry.Flink != NULL)
85 {
86 RemoveEntryList(&QueueEntry->ListEntry);
87 }
88
89 HeapFree(scm->Heap,
90 0,
91 QueueEntry->Callbacks);
92
93 HeapFree(scm->Heap,
94 0,
95 QueueEntry);
96}

Referenced by CleanupSidCacheMgr(), DequeueSidLookup(), and LookupThreadProc().

◆ LookupSidCache()

BOOL LookupSidCache ( IN HANDLE  SidCacheMgr,
IN PSID  pSid,
IN PSIDREQCOMPLETIONPROC  CompletionProc,
IN PVOID  Context 
)

Definition at line 876 of file sidcache.c.

880{
881 BOOL Found = FALSE;
882 PSIDREQRESULT ReqResult = NULL;
883 PSIDCACHEMGR scm;
884
885 scm = ReferenceSidCacheMgr(SidCacheMgr);
886 if (scm != NULL)
887 {
889
890 /* search the cache */
891 Found = FindSidInCache(scm,
892 pSid,
893 &ReqResult);
894
895 if (!Found)
896 {
897 /* the sid is not in the cache, queue it if not already queued */
898 if (!QueueSidLookup(scm,
899 pSid,
900 CompletionProc,
901 Context))
902 {
903 PSIDREQRESULT FoundReqResult = NULL;
904
905 /* unable to queue it, look it up now */
906
908
909 /* lookup everything we need */
910 if (!LookupSidInformation(scm,
911 pSid,
912 &ReqResult))
913 {
914 ReqResult = NULL;
915 }
916
918
919 /* see if the SID was added to the cache in the meanwhile */
920 if (!FindSidInCache(scm,
921 pSid,
922 &FoundReqResult))
923 {
924 if (ReqResult != NULL)
925 {
926 /* cache the results */
928 pSid,
929 ReqResult);
930 }
931 }
932 else
933 {
934 if (ReqResult != NULL)
935 {
936 /* free the information of our lookup and use the cached
937 information*/
939 ReqResult);
940 }
941
942 ReqResult = FoundReqResult;
943 }
944
945 Found = (ReqResult != NULL);
946 }
947 }
948
950
951 /* call the completion callback */
952 if (Found)
953 {
954 CompletionProc(SidCacheMgr,
955 pSid,
956 ReqResult,
957 Context);
958
959 if (ReqResult != NULL)
960 {
961 HeapFree(scm->Heap,
962 0,
963 ReqResult);
964 }
965 }
966
968 }
969
970 return Found;
971}
return Found
Definition: dirsup.c:1270
static VOID CacheLookupResults(IN PSIDCACHEMGR scm, IN PSID pSid, IN PSIDREQRESULT ReqResult)
Definition: sidcache.c:401
static BOOL LookupSidInformation(IN PSIDCACHEMGR scm, IN PSID pSid, OUT PSIDREQRESULT *ReqResult)
Definition: sidcache.c:214
static BOOL QueueSidLookup(IN PSIDCACHEMGR scm, IN PSID pSid, IN PSIDREQCOMPLETIONPROC CompletionProc, IN PVOID Context)
Definition: sidcache.c:689
static BOOL FindSidInCache(IN PSIDCACHEMGR scm, IN PSID pSid, OUT PSIDREQRESULT *ReqResult)
Definition: sidcache.c:336
VOID DereferenceSidReqResult(IN HANDLE SidCacheMgr, IN PSIDREQRESULT ReqResult)
Definition: sidcache.c:855

Referenced by AddPrincipalToList(), and ReloadPrincipalsList().

◆ LookupSidInformation()

static BOOL LookupSidInformation ( IN PSIDCACHEMGR  scm,
IN PSID  pSid,
OUT PSIDREQRESULT ReqResult 
)
static

Definition at line 214 of file sidcache.c.

217{
218 PLSA_REFERENCED_DOMAIN_LIST ReferencedDomain;
221 PLSA_UNICODE_STRING DomainName;
222 SID_NAME_USE SidNameUse = SidTypeUnknown;
223 PPOLICY_ACCOUNT_DOMAIN_INFO PolicyAccountDomainInfo = NULL;
225 DWORD AccountNameSize, DomainNameSize = 0;
226 PSIDREQRESULT ReqRet = NULL;
227 BOOL Ret = FALSE;
228
229 Status = LsaLookupSids(scm->LsaHandle,
230 1,
231 &pSid,
232 &ReferencedDomain,
233 &Names);
234 if (NT_SUCCESS(Status))
235 {
236 SidNameUse = Names->Use;
237
238 if (ReferencedDomain != NULL &&
239 Names->DomainIndex >= 0)
240 {
241 Domain = &ReferencedDomain->Domains[Names->DomainIndex];
242 DomainName = &Domain->Name;
243 }
244 else
245 {
246 Domain = NULL;
247 DomainName = NULL;
248 }
249
250 switch (SidNameUse)
251 {
252 case SidTypeAlias:
253 {
254 if (Domain != NULL)
255 {
256 /* query the domain name for BUILTIN accounts */
257 Status = LsaQueryInformationPolicy(scm->LsaHandle,
259 (PVOID*)&PolicyAccountDomainInfo);
260 if (NT_SUCCESS(Status))
261 {
262 DomainName = &PolicyAccountDomainInfo->DomainName;
263
264 /* make the user believe this is a group */
265 SidNameUse = (PolicyAccountDomainInfo != NULL ? SidTypeGroup : SidTypeUser);
266 }
267 }
268 break;
269 }
270
271 default:
272 {
273 DPRINT("Unhandled SID type: 0x%x\n", Names->Use);
274 break;
275 }
276 }
277
278 AccountNameSize = Names->Name.Length;
279 if (DomainName != NULL)
280 {
281 DomainNameSize = DomainName->Length;
282 }
283
284 ReqRet = HeapAlloc(scm->Heap,
285 0,
286 sizeof(SIDREQRESULT) +
287 (((AccountNameSize + DomainNameSize) + 2) * sizeof(WCHAR)));
288 if (ReqRet != NULL)
289 {
290 ReqRet->RefCount = 1;
291 ReqRet->AccountName = (LPWSTR)(ReqRet + 1);
292 ReqRet->DomainName = ReqRet->AccountName + (AccountNameSize / sizeof(WCHAR)) + 1;
293
294 CopyMemory(ReqRet->AccountName,
295 Names->Name.Buffer,
296 Names->Name.Length);
297
298 if (DomainName != NULL)
299 {
300 CopyMemory(ReqRet->DomainName,
301 DomainName->Buffer,
302 DomainName->Length);
303 }
304
305 ReqRet->AccountName[AccountNameSize / sizeof(WCHAR)] = L'\0';
306 ReqRet->DomainName[DomainNameSize / sizeof(WCHAR)] = L'\0';
307
308 ReqRet->SidNameUse = SidNameUse;
309 }
310
311 if (PolicyAccountDomainInfo != NULL)
312 {
313 LsaFreeMemory(PolicyAccountDomainInfo);
314 }
315
316 LsaFreeMemory(ReferencedDomain);
318
319 Ret = TRUE;
320 }
321 else if (Status == STATUS_NONE_MAPPED)
322 {
323 Ret = TRUE;
324 }
325
326 if (Ret)
327 {
328 *ReqResult = ReqRet;
329 }
330
331 return Ret;
332}
PWSTR Names[]
LONG NTSTATUS
Definition: precomp.h:26
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
NTSTATUS WINAPI LsaLookupSids(IN LSA_HANDLE PolicyHandle, IN ULONG Count, IN PSID *Sids, OUT PLSA_REFERENCED_DOMAIN_LIST *ReferencedDomains, OUT PLSA_TRANSLATED_NAME *Names)
Definition: lsa.c:1069
NTSTATUS WINAPI LsaQueryInformationPolicy(IN LSA_HANDLE PolicyHandle, IN POLICY_INFORMATION_CLASS InformationClass, OUT PVOID *Buffer)
Definition: lsa.c:1473
NTSTATUS WINAPI LsaFreeMemory(IN PVOID Buffer)
Definition: lsa.c:701
Status
Definition: gdiplustypes.h:25
enum _SID_NAME_USE SID_NAME_USE
@ SidTypeUnknown
Definition: lsa.idl:125
@ SidTypeAlias
Definition: lsa.idl:121
@ SidTypeGroup
Definition: lsa.idl:119
@ SidTypeUser
Definition: lsa.idl:118
@ PolicyAccountDomainInformation
Definition: ntsecapi.h:247
#define STATUS_NONE_MAPPED
Definition: ntstatus.h:351
#define DPRINT
Definition: sndvol32.h:71
PLSA_TRUST_INFORMATION Domains
Definition: ntsecapi.h:408
LSA_UNICODE_STRING Name
Definition: ntsecapi.h:403
LSA_UNICODE_STRING DomainName
Definition: ntsecapi.h:566
SID_NAME_USE SidNameUse
Definition: precomp.h:170
#define CopyMemory
Definition: winbase.h:1710
WCHAR * LPWSTR
Definition: xmlstorage.h:184

Referenced by LookupSidCache(), and LookupThreadProc().

◆ LookupThreadProc()

static DWORD WINAPI LookupThreadProc ( IN LPVOID  lpParameter)
static

Definition at line 455 of file sidcache.c.

456{
458 PSIDCACHEMGR scm = (PSIDCACHEMGR)lpParameter;
459
460 /* Reference the dll to avoid problems in case of accidental
461 FreeLibrary calls... */
462 if (!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
464 &hModule))
465 {
466 hModule = NULL;
467 }
468
469 while (scm->RefCount != 0)
470 {
471 PSIDQUEUEENTRY QueueEntry = NULL;
472
474
475 /* get the first item of the queue */
476 if (scm->QueueListHead.Flink != &scm->QueueListHead)
477 {
478 QueueEntry = CONTAINING_RECORD(scm->QueueListHead.Flink,
480 ListEntry);
481 RemoveEntryList(&QueueEntry->ListEntry);
482 QueueEntry->ListEntry.Flink = NULL;
483 }
484 else
485 {
487
488 /* wait for the next asynchronous lookup queued */
490 INFINITE);
491 continue;
492 }
493
494 scm->QueueLookingUp = QueueEntry;
495
497
498 if (QueueEntry != NULL)
499 {
500 PSIDREQRESULT ReqResult, FoundReqResult;
501 PSID pSid = (PSID)(QueueEntry + 1);
502
503 /* lookup the SID information */
504 if (!LookupSidInformation(scm,
505 pSid,
506 &ReqResult))
507 {
508 ReqResult = NULL;
509 }
510
512
513 /* see if the SID was added to the cache in the meanwhile */
514 if (!FindSidInCache(scm,
515 pSid,
516 &FoundReqResult))
517 {
518 if (ReqResult != NULL)
519 {
520 /* cache the results */
522 pSid,
523 ReqResult);
524 }
525 }
526 else
527 {
528 if (ReqResult != NULL)
529 {
530 /* free the information of our lookup and use the cached
531 information*/
533 ReqResult);
534 }
535
536 ReqResult = FoundReqResult;
537 }
538
539 /* notify the callers unless the lookup was cancelled */
540 if (scm->QueueLookingUp != NULL)
541 {
542 ULONG i = 0;
543
544 while (scm->QueueLookingUp != NULL &&
545 i < QueueEntry->CallbackCount)
546 {
548 PSIDREQCOMPLETIONPROC CompletionProc;
549
550 Context = QueueEntry->Callbacks[i].Context;
551 CompletionProc = QueueEntry->Callbacks[i].CompletionProc;
552
554
555 /* call the completion proc without holding the lock! */
556 CompletionProc(ScmToHandle(scm),
557 pSid,
558 ReqResult,
559 Context);
560
562
563 i++;
564 }
565
566 scm->QueueLookingUp = NULL;
567 }
568
570
571 /* free the queue item */
572 FreeQueueEntry(scm,
573 QueueEntry);
574 }
575 }
576
578
579 HeapFree(scm->Heap,
580 0,
581 scm);
582
583 if (hModule != NULL)
584 {
585 /* dereference the library and exit */
587 0);
588 }
589
590 return 0;
591}
static HINSTANCE hDllInstance
Definition: clb.c:30
VOID(* PSIDREQCOMPLETIONPROC)(IN HANDLE SidCacheMgr, IN PSID Sid, IN PSIDREQRESULT SidRequestResult, IN PVOID Context)
Definition: precomp.h:175
HMODULE hModule
Definition: animate.c:44
BOOL WINAPI GetModuleHandleExW(IN DWORD dwFlags, IN LPCWSTR lpwModuleName OPTIONAL, OUT HMODULE *phModule)
Definition: loader.c:866
VOID WINAPI FreeLibraryAndExitThread(HMODULE hLibModule, DWORD dwExitCode)
Definition: loader.c:507
#define INFINITE
Definition: serial.h:102
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
struct _SID * PSID
Definition: eventlog.c:35
static VOID CleanupSidCacheMgr(IN PSIDCACHEMGR scm)
Definition: sidcache.c:112
#define ScmToHandle(Scm)
Definition: sidcache.c:37
struct _SIDCACHEMGR * PSIDCACHEMGR
PSIDREQCOMPLETIONPROC CompletionProc
Definition: sidcache.c:56
LIST_ENTRY ListEntry
Definition: sidcache.c:63
PSIDCACHECALLBACKINFO Callbacks
Definition: sidcache.c:65
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition: synch.c:82
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185

Referenced by CreateSidCacheMgr().

◆ OpenLSAPolicyHandle()

static BOOL OpenLSAPolicyHandle ( IN LPWSTR  SystemName,
IN ACCESS_MASK  DesiredAccess,
OUT PLSA_HANDLE  PolicyHandle 
)
static

Definition at line 172 of file sidcache.c.

175{
176 LSA_OBJECT_ATTRIBUTES LsaObjectAttributes = {0};
177 LSA_UNICODE_STRING LsaSystemName, *psn;
179 SIZE_T NameLength;
180
181 if (SystemName != NULL && SystemName[0] != L'\0')
182 {
183 NameLength = wcslen(SystemName);
184 if (NameLength > UNICODE_STRING_MAX_CHARS)
185 {
186 return FALSE;
187 }
188
189 LsaSystemName.Buffer = SystemName;
190 LsaSystemName.Length = NameLength * sizeof(WCHAR);
191 LsaSystemName.MaximumLength = LsaSystemName.Length + sizeof(WCHAR);
192 psn = &LsaSystemName;
193 }
194 else
195 {
196 psn = NULL;
197 }
198
199 Status = LsaOpenPolicy(psn,
200 &LsaObjectAttributes,
202 PolicyHandle);
203 if (!NT_SUCCESS(Status))
204 {
206 return FALSE;
207 }
208
209 return TRUE;
210}
NTSTATUS WINAPI LsaOpenPolicy(IN PLSA_UNICODE_STRING SystemName OPTIONAL, IN PLSA_OBJECT_ATTRIBUTES ObjectAttributes, IN ACCESS_MASK DesiredAccess, OUT PLSA_HANDLE PolicyHandle)
Definition: lsa.c:1183
ULONG WINAPI LsaNtStatusToWinError(IN NTSTATUS Status)
Definition: lsa.c:1131
#define UNICODE_STRING_MAX_CHARS
USHORT MaximumLength
Definition: ntsecapi.h:164
_Must_inspect_result_ _In_ WDFDEVICE _In_ ULONG _In_ ACCESS_MASK DesiredAccess
Definition: wdfdevice.h:2658

Referenced by CreateSidCacheMgr().

◆ QueueSidLookup()

static BOOL QueueSidLookup ( IN PSIDCACHEMGR  scm,
IN PSID  pSid,
IN PSIDREQCOMPLETIONPROC  CompletionProc,
IN PVOID  Context 
)
static

Definition at line 689 of file sidcache.c.

693{
694 PLIST_ENTRY CurrentEntry;
695 PSIDQUEUEENTRY QueueEntry, FoundEntry = NULL;
696 BOOL Ret = FALSE;
697
698 /* NOTE: assumes the lists are locked! */
699
700 if (scm->QueueLookingUp != NULL &&
702 (PSID)(scm->QueueLookingUp + 1)))
703 {
704 FoundEntry = scm->QueueLookingUp;
705 }
706 else
707 {
708 for (CurrentEntry = scm->QueueListHead.Flink;
709 CurrentEntry != &scm->QueueListHead;
710 CurrentEntry = CurrentEntry->Flink)
711 {
712 QueueEntry = CONTAINING_RECORD(CurrentEntry,
714 ListEntry);
715
716 if (EqualSid(pSid,
717 (PSID)(QueueEntry + 1)))
718 {
719 FoundEntry = QueueEntry;
720 break;
721 }
722 }
723 }
724
725 if (FoundEntry == NULL)
726 {
727 DWORD SidLength = GetLengthSid(pSid);
728
729 FoundEntry = HeapAlloc(scm->Heap,
730 0,
731 sizeof(SIDQUEUEENTRY) + SidLength);
732 if (FoundEntry != NULL)
733 {
734 CopySid(SidLength,
735 (PSID)(FoundEntry + 1),
736 pSid);
737
738 FoundEntry->CallbackCount = 1;
739 FoundEntry->Callbacks = HeapAlloc(scm->Heap,
740 0,
741 sizeof(SIDCACHECALLBACKINFO));
742
743 if (FoundEntry->Callbacks != NULL)
744 {
745 FoundEntry->Callbacks[0].CompletionProc = CompletionProc;
746 FoundEntry->Callbacks[0].Context = Context;
747
748 /* append it to the queue */
749 InsertTailList(&scm->QueueListHead,
750 &FoundEntry->ListEntry);
751
752 /* signal the lookup event */
753 SetEvent(scm->LookupEvent);
754
755 Ret = TRUE;
756 }
757 else
758 {
759 /* unable to queue it because we couldn't allocate the callbacks
760 array, free the memory and return */
761 HeapFree(scm->Heap,
762 0,
763 FoundEntry);
764 }
765 }
766 }
767 else
768 {
770
771 /* add the callback */
772 Sidccb = HeapReAlloc(scm->Heap,
773 0,
774 FoundEntry->Callbacks,
775 (FoundEntry->CallbackCount + 1) * sizeof(SIDCACHECALLBACKINFO));
776 if (Sidccb != NULL)
777 {
778 FoundEntry->Callbacks = Sidccb;
779 FoundEntry->Callbacks[FoundEntry->CallbackCount].CompletionProc = CompletionProc;
780 FoundEntry->Callbacks[FoundEntry->CallbackCount++].Context = Context;
781
782 Ret = TRUE;
783 }
784 }
785
786 return Ret;
787}
#define HeapReAlloc
Definition: compat.h:734
ULONG CallbackCount
Definition: sidcache.c:64

Referenced by LookupSidCache().

◆ ReferenceSidCacheMgr()

static PSIDCACHEMGR ReferenceSidCacheMgr ( IN HANDLE  SidCacheMgr)
static

Definition at line 147 of file sidcache.c.

148{
149 PSIDCACHEMGR scm = HandleToScm(SidCacheMgr);
150
151 if (InterlockedIncrement(&scm->RefCount) != 1)
152 {
153 return scm;
154 }
155
156 return NULL;
157}
#define InterlockedIncrement
Definition: armddk.h:53

Referenced by DequeueSidLookup(), DereferenceSidReqResult(), LookupSidCache(), and ReferenceSidReqResult().

◆ ReferenceSidReqResult()

VOID ReferenceSidReqResult ( IN HANDLE  SidCacheMgr,
IN PSIDREQRESULT  ReqResult 
)

Definition at line 839 of file sidcache.c.

841{
842 PSIDCACHEMGR scm;
843
844 scm = ReferenceSidCacheMgr(SidCacheMgr);
845 if (scm != NULL)
846 {
847 InterlockedIncrement(&ReqResult->RefCount);
848
850 }
851}

Referenced by UpdatePrincipalListItem().