2 * dlls/advapi32/security.c
3 * FIXME: for all functions thunking down to Rtl* functions: implement SetLastError()
12 #include "debugtools.h"
14 DECLARE_DEBUG_CHANNEL(advapi)
15 DECLARE_DEBUG_CHANNEL(security)
17 #define CallWin32ToNt(func) \
20 if (ret !=STATUS_SUCCESS) \
21 { SetLastError (RtlNtStatusToDosError(ret)); return FALSE; } \
25 /* ##############################
26 ###### TOKEN FUNCTIONS ######
27 ##############################
30 /******************************************************************************
31 * OpenProcessToken [ADVAPI32.109]
32 * Opens the access token associated with a process
35 * ProcessHandle [I] Handle to process
36 * DesiredAccess [I] Desired access to process
37 * TokenHandle [O] Pointer to handle of open access token
42 OpenProcessToken( HANDLE ProcessHandle, DWORD DesiredAccess,
45 CallWin32ToNt(NtOpenProcessToken( ProcessHandle, DesiredAccess, TokenHandle ));
48 /******************************************************************************
49 * OpenThreadToken [ADVAPI32.114]
58 OpenThreadToken( HANDLE ThreadHandle, DWORD DesiredAccess,
59 BOOL OpenAsSelf, HANDLE *TokenHandle)
61 CallWin32ToNt (NtOpenThreadToken(ThreadHandle, DesiredAccess, OpenAsSelf, TokenHandle));
64 /******************************************************************************
65 * AdjustTokenPrivileges [ADVAPI32.10]
69 * DisableAllPrivileges []
76 AdjustTokenPrivileges( HANDLE TokenHandle, BOOL DisableAllPrivileges,
77 LPVOID NewState, DWORD BufferLength,
78 LPVOID PreviousState, LPDWORD ReturnLength )
80 CallWin32ToNt(NtAdjustPrivilegesToken(TokenHandle, DisableAllPrivileges, NewState, BufferLength, PreviousState, ReturnLength));
83 /******************************************************************************
84 * GetTokenInformation [ADVAPI32.66]
95 GetTokenInformation( HANDLE token, TOKEN_INFORMATION_CLASS tokeninfoclass,
96 LPVOID tokeninfo, DWORD tokeninfolength, LPDWORD retlen )
98 CallWin32ToNt (NtQueryInformationToken( token, tokeninfoclass, tokeninfo, tokeninfolength, retlen));
101 /* ##############################
102 ###### SID FUNCTIONS ######
103 ##############################
106 /******************************************************************************
107 * AllocateAndInitializeSid [ADVAPI32.11]
110 * pIdentifierAuthority []
111 * nSubAuthorityCount []
123 AllocateAndInitializeSid( PSID_IDENTIFIER_AUTHORITY pIdentifierAuthority,
124 BYTE nSubAuthorityCount,
125 DWORD nSubAuthority0, DWORD nSubAuthority1,
126 DWORD nSubAuthority2, DWORD nSubAuthority3,
127 DWORD nSubAuthority4, DWORD nSubAuthority5,
128 DWORD nSubAuthority6, DWORD nSubAuthority7,
131 if (!(*pSid = HeapAlloc( GetProcessHeap(), 0,
132 GetSidLengthRequired(nSubAuthorityCount))))
134 (*pSid)->Revision = SID_REVISION;
135 if (pIdentifierAuthority)
136 memcpy(&(*pSid)->IdentifierAuthority, pIdentifierAuthority,
137 sizeof (SID_IDENTIFIER_AUTHORITY));
138 *GetSidSubAuthorityCount(*pSid) = nSubAuthorityCount;
140 if (nSubAuthorityCount > 0)
141 *GetSidSubAuthority(*pSid, 0) = nSubAuthority0;
142 if (nSubAuthorityCount > 1)
143 *GetSidSubAuthority(*pSid, 1) = nSubAuthority1;
144 if (nSubAuthorityCount > 2)
145 *GetSidSubAuthority(*pSid, 2) = nSubAuthority2;
146 if (nSubAuthorityCount > 3)
147 *GetSidSubAuthority(*pSid, 3) = nSubAuthority3;
148 if (nSubAuthorityCount > 4)
149 *GetSidSubAuthority(*pSid, 4) = nSubAuthority4;
150 if (nSubAuthorityCount > 5)
151 *GetSidSubAuthority(*pSid, 5) = nSubAuthority5;
152 if (nSubAuthorityCount > 6)
153 *GetSidSubAuthority(*pSid, 6) = nSubAuthority6;
154 if (nSubAuthorityCount > 7)
155 *GetSidSubAuthority(*pSid, 7) = nSubAuthority7;
160 /******************************************************************************
161 * FreeSid [ADVAPI32.42]
169 HeapFree( GetProcessHeap(), 0, pSid );
173 /******************************************************************************
174 * CopySid [ADVAPI32.24]
177 * nDestinationSidLength []
182 CopySid( DWORD nDestinationSidLength, PSID pDestinationSid, PSID pSourceSid )
185 if (!IsValidSid(pSourceSid))
188 if (nDestinationSidLength < GetLengthSid(pSourceSid))
191 memcpy(pDestinationSid, pSourceSid, GetLengthSid(pSourceSid));
196 /******************************************************************************
197 * IsValidSid [ADVAPI32.80]
203 IsValidSid( PSID pSid )
205 if (!pSid || pSid->Revision != SID_REVISION)
211 /******************************************************************************
212 * EqualSid [ADVAPI32.40]
219 EqualSid( PSID pSid1, PSID pSid2 )
221 if (!IsValidSid(pSid1) || !IsValidSid(pSid2))
224 if (*GetSidSubAuthorityCount(pSid1) != *GetSidSubAuthorityCount(pSid2))
227 if (memcmp(pSid1, pSid2, GetLengthSid(pSid1)) != 0)
233 /******************************************************************************
234 * EqualPrefixSid [ADVAPI32.39]
236 BOOL WINAPI EqualPrefixSid (PSID pSid1, PSID pSid2) {
237 if (!IsValidSid(pSid1) || !IsValidSid(pSid2))
240 if (*GetSidSubAuthorityCount(pSid1) != *GetSidSubAuthorityCount(pSid2))
243 if (memcmp(pSid1, pSid2, GetSidLengthRequired(pSid1->SubAuthorityCount - 1))
250 /******************************************************************************
251 * GetSidLengthRequired [ADVAPI32.63]
254 * nSubAuthorityCount []
257 GetSidLengthRequired( BYTE nSubAuthorityCount )
259 return sizeof (SID) + (nSubAuthorityCount - 1) * sizeof (DWORD);
262 /******************************************************************************
263 * InitializeSid [ADVAPI32.74]
266 * pIdentifierAuthority []
269 InitializeSid (PSID pSid, PSID_IDENTIFIER_AUTHORITY pIdentifierAuthority,
270 BYTE nSubAuthorityCount)
274 pSid->Revision = SID_REVISION;
275 if (pIdentifierAuthority)
276 memcpy(&pSid->IdentifierAuthority, pIdentifierAuthority,
277 sizeof (SID_IDENTIFIER_AUTHORITY));
278 *GetSidSubAuthorityCount(pSid) = nSubAuthorityCount;
280 for (i = 0; i < nSubAuthorityCount; i++)
281 *GetSidSubAuthority(pSid, i) = 0;
286 /******************************************************************************
287 * GetSidIdentifierAuthority [ADVAPI32.62]
292 PSID_IDENTIFIER_AUTHORITY WINAPI
293 GetSidIdentifierAuthority( PSID pSid )
295 return &pSid->IdentifierAuthority;
298 /******************************************************************************
299 * GetSidSubAuthority [ADVAPI32.64]
306 GetSidSubAuthority( PSID pSid, DWORD nSubAuthority )
308 return &pSid->SubAuthority[nSubAuthority];
311 /******************************************************************************
312 * GetSidSubAuthorityCount [ADVAPI32.65]
318 GetSidSubAuthorityCount (PSID pSid)
320 return &pSid->SubAuthorityCount;
323 /******************************************************************************
324 * GetLengthSid [ADVAPI32.48]
330 GetLengthSid (PSID pSid)
332 return GetSidLengthRequired( * GetSidSubAuthorityCount(pSid) );
335 /* ##############################################
336 ###### SECURITY DESCRIPTOR FUNCTIONS ######
337 ##############################################
340 /******************************************************************************
341 * InitializeSecurityDescriptor [ADVAPI32.73]
348 InitializeSecurityDescriptor( SECURITY_DESCRIPTOR *pDescr, DWORD revision )
350 CallWin32ToNt (RtlCreateSecurityDescriptor(pDescr, revision ));
353 /******************************************************************************
354 * GetSecurityDescriptorLength [ADVAPI32.55]
356 DWORD WINAPI GetSecurityDescriptorLength( SECURITY_DESCRIPTOR *pDescr)
358 return (RtlLengthSecurityDescriptor(pDescr));
361 /******************************************************************************
362 * GetSecurityDescriptorOwner [ADVAPI32.56]
366 * lpbOwnerDefaulted []
369 GetSecurityDescriptorOwner( SECURITY_DESCRIPTOR *pDescr, PSID *pOwner,
370 LPBOOL lpbOwnerDefaulted )
372 CallWin32ToNt (RtlGetOwnerSecurityDescriptor( pDescr, pOwner, (PBOOLEAN)lpbOwnerDefaulted ));
375 /******************************************************************************
376 * SetSecurityDescriptorOwner [ADVAPI32]
380 BOOL WINAPI SetSecurityDescriptorOwner( PSECURITY_DESCRIPTOR pSecurityDescriptor,
381 PSID pOwner, BOOL bOwnerDefaulted)
383 CallWin32ToNt (RtlSetOwnerSecurityDescriptor(pSecurityDescriptor, pOwner, bOwnerDefaulted));
385 /******************************************************************************
386 * GetSecurityDescriptorGroup [ADVAPI32.54]
388 BOOL WINAPI GetSecurityDescriptorGroup(
389 PSECURITY_DESCRIPTOR SecurityDescriptor,
391 LPBOOL GroupDefaulted)
393 CallWin32ToNt (RtlGetGroupSecurityDescriptor(SecurityDescriptor, Group, (PBOOLEAN)GroupDefaulted));
395 /******************************************************************************
396 * SetSecurityDescriptorGroup
398 BOOL WINAPI SetSecurityDescriptorGroup ( PSECURITY_DESCRIPTOR SecurityDescriptor,
399 PSID Group, BOOL GroupDefaulted)
401 CallWin32ToNt (RtlSetGroupSecurityDescriptor( SecurityDescriptor, Group, GroupDefaulted));
404 /******************************************************************************
405 * IsValidSecurityDescriptor [ADVAPI32.79]
411 IsValidSecurityDescriptor( PSECURITY_DESCRIPTOR SecurityDescriptor )
413 CallWin32ToNt (RtlValidSecurityDescriptor(SecurityDescriptor));
416 /******************************************************************************
417 * GetSecurityDescriptorDacl [ADVAPI.91]
419 BOOL WINAPI GetSecurityDescriptorDacl(
420 IN PSECURITY_DESCRIPTOR pSecurityDescriptor,
421 OUT LPBOOL lpbDaclPresent,
423 OUT LPBOOL lpbDaclDefaulted)
425 CallWin32ToNt (RtlGetDaclSecurityDescriptor(pSecurityDescriptor, (PBOOLEAN)lpbDaclPresent,
426 pDacl, (PBOOLEAN)lpbDaclDefaulted));
429 /******************************************************************************
430 * SetSecurityDescriptorDacl [ADVAPI.224]
433 SetSecurityDescriptorDacl (
434 PSECURITY_DESCRIPTOR lpsd,
439 CallWin32ToNt (RtlSetDaclSecurityDescriptor (lpsd, daclpresent, dacl, dacldefaulted ));
441 /******************************************************************************
442 * GetSecurityDescriptorSacl [ADVAPI.]
444 BOOL WINAPI GetSecurityDescriptorSacl(
445 IN PSECURITY_DESCRIPTOR lpsd,
446 OUT LPBOOL lpbSaclPresent,
448 OUT LPBOOL lpbSaclDefaulted)
450 CallWin32ToNt (RtlGetSaclSecurityDescriptor(lpsd, (PBOOLEAN)lpbSaclPresent,
451 pSacl, (PBOOLEAN)lpbSaclDefaulted));
454 /**************************************************************************
455 * SetSecurityDescriptorSacl [NTDLL.488]
457 BOOL WINAPI SetSecurityDescriptorSacl (
458 PSECURITY_DESCRIPTOR lpsd,
463 CallWin32ToNt (RtlSetSaclSecurityDescriptor(lpsd, saclpresent, lpsacl, sacldefaulted));
465 /******************************************************************************
466 * MakeSelfRelativeSD [ADVAPI32.95]
474 MakeSelfRelativeSD( PSECURITY_DESCRIPTOR lpabssecdesc,
475 PSECURITY_DESCRIPTOR lpselfsecdesc, LPDWORD lpbuflen )
477 FIXME_(advapi)("(%p,%p,%p),stub!\n",lpabssecdesc,lpselfsecdesc,lpbuflen);
481 /******************************************************************************
482 * GetSecurityDescriptorControl32 [ADVAPI32]
485 BOOL WINAPI GetSecurityDescriptorControl ( PSECURITY_DESCRIPTOR pSecurityDescriptor,
486 PSECURITY_DESCRIPTOR_CONTROL pControl, LPDWORD lpdwRevision)
487 { FIXME_(advapi)("(%p,%p,%p),stub!\n",pSecurityDescriptor,pControl,lpdwRevision);
491 /* ##############################
492 ###### MISC FUNCTIONS ######
493 ##############################
496 /******************************************************************************
497 * LookupPrivilegeValue32W [ADVAPI32.93]
498 * Retrieves LUID used on a system to represent the privilege name.
501 * lpLuid should be PLUID
504 * lpSystemName [I] Address of string specifying the system
505 * lpName [I] Address of string specifying the privilege
506 * lpLuid [I] Address of locally unique identifier
511 LookupPrivilegeValueW( LPCWSTR lpSystemName, LPCWSTR lpName, LPVOID lpLuid )
513 FIXME_(advapi)("(%s,%s,%p): stub\n",debugstr_w(lpSystemName),
514 debugstr_w(lpName), lpLuid);
518 /******************************************************************************
519 * LookupPrivilegeValue32A [ADVAPI32.92]
522 LookupPrivilegeValueA( LPCSTR lpSystemName, LPCSTR lpName, LPVOID lpLuid )
524 LPWSTR lpSystemNameW = HEAP_strdupAtoW(GetProcessHeap(), 0, lpSystemName);
525 LPWSTR lpNameW = HEAP_strdupAtoW(GetProcessHeap(), 0, lpName);
526 BOOL ret = LookupPrivilegeValueW( lpSystemNameW, lpNameW, lpLuid);
527 HeapFree(GetProcessHeap(), 0, lpNameW);
528 HeapFree(GetProcessHeap(), 0, lpSystemNameW);
532 /******************************************************************************
533 * GetFileSecurity32A [ADVAPI32.45]
535 * Obtains Specified information about the security of a file or directory
536 * The information obtained is constrained by the callers access rights and
540 GetFileSecurityA( LPCSTR lpFileName,
541 SECURITY_INFORMATION RequestedInformation,
542 PSECURITY_DESCRIPTOR pSecurityDescriptor,
543 DWORD nLength, LPDWORD lpnLengthNeeded )
545 FIXME_(advapi)("(%s) : stub\n", debugstr_a(lpFileName));
549 /******************************************************************************
550 * GetFileSecurity32W [ADVAPI32.46]
552 * Obtains Specified information about the security of a file or directory
553 * The information obtained is constrained by the callers access rights and
558 * RequestedInformation []
559 * pSecurityDescriptor []
564 GetFileSecurityW( LPCWSTR lpFileName,
565 SECURITY_INFORMATION RequestedInformation,
566 PSECURITY_DESCRIPTOR pSecurityDescriptor,
567 DWORD nLength, LPDWORD lpnLengthNeeded )
569 FIXME_(advapi)("(%s) : stub\n", debugstr_w(lpFileName) );
574 /******************************************************************************
575 * LookupAccountSid32A [ADVAPI32.86]
578 LookupAccountSidA( LPCSTR system, PSID sid, LPCSTR account,
579 LPDWORD accountSize, LPCSTR domain, LPDWORD domainSize,
580 PSID_NAME_USE name_use )
582 FIXME_(security)("(%s,%p,%p,%p,%p,%p,%p): stub\n",
583 system,sid,account,accountSize,domain,domainSize,name_use);
584 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
588 /******************************************************************************
589 * LookupAccountSid32W [ADVAPI32.87]
601 LookupAccountSidW( LPCWSTR system, PSID sid, LPCWSTR account,
602 LPDWORD accountSize, LPCWSTR domain, LPDWORD domainSize,
603 PSID_NAME_USE name_use )
605 FIXME_(security)("(%p,%p,%p,%p,%p,%p,%p): stub\n",
606 system,sid,account,accountSize,domain,domainSize,name_use);
607 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
611 /******************************************************************************
612 * SetFileSecurity32A [ADVAPI32.182]
613 * Sets the security of a file or directory
615 BOOL WINAPI SetFileSecurityA( LPCSTR lpFileName,
616 SECURITY_INFORMATION RequestedInformation,
617 PSECURITY_DESCRIPTOR pSecurityDescriptor)
619 FIXME_(advapi)("(%s) : stub\n", debugstr_a(lpFileName));
623 /******************************************************************************
624 * SetFileSecurity32W [ADVAPI32.183]
625 * Sets the security of a file or directory
629 * RequestedInformation []
630 * pSecurityDescriptor []
633 SetFileSecurityW( LPCWSTR lpFileName,
634 SECURITY_INFORMATION RequestedInformation,
635 PSECURITY_DESCRIPTOR pSecurityDescriptor )
637 FIXME_(advapi)("(%s) : stub\n", debugstr_w(lpFileName) );
641 /******************************************************************************
642 * QueryWindows31FilesMigration [ADVAPI32.266]
648 QueryWindows31FilesMigration( DWORD x1 )
650 FIXME_(advapi)("(%ld):stub\n",x1);
654 /******************************************************************************
655 * SynchronizeWindows31FilesAndWindowsNTRegistry [ADVAPI32.265]
664 SynchronizeWindows31FilesAndWindowsNTRegistry( DWORD x1, DWORD x2, DWORD x3,
667 FIXME_(advapi)("(0x%08lx,0x%08lx,0x%08lx,0x%08lx):stub\n",x1,x2,x3,x4);
671 /******************************************************************************
672 * LsaOpenPolicy [ADVAPI32.200]
681 LsaOpenPolicy(PLSA_UNICODE_STRING SystemName,
682 PLSA_OBJECT_ATTRIBUTES ObjectAttributes,
683 ACCESS_MASK DesiredAccess,
684 PLSA_HANDLE PolicyHandle)
686 FIXME_(advapi)("(%p,%p,0x%08lx,%p):stub\n",
687 SystemName, ObjectAttributes,
688 DesiredAccess, PolicyHandle);
689 return 0xc0000000; /* generic error */
692 /******************************************************************************
693 * NotifyBootConfigStatus [ADVAPI32.97]
699 NotifyBootConfigStatus( DWORD x1 )
701 FIXME_(advapi)("(0x%08lx):stub\n",x1);
705 /******************************************************************************
706 * RevertToSelf [ADVAPI32.180]
714 FIXME_(advapi)("(), stub\n");
718 /******************************************************************************
719 * ImpersonateSelf [ADVAPI32.71]
722 ImpersonateSelf(SECURITY_IMPERSONATION_LEVEL ImpersonationLevel)
724 FIXME_(advapi)("(%08x), stub\n", ImpersonationLevel);
728 /******************************************************************************
729 * AccessCheck32 [ADVAPI32.71]
732 AccessCheck(PSECURITY_DESCRIPTOR pSecurityDescriptor, HANDLE ClientToken,
733 DWORD DesiredAccess, PGENERIC_MAPPING GenericMapping, PPRIVILEGE_SET PrivilegeSet,
734 LPDWORD PrivilegeSetLength, LPDWORD GrantedAccess, LPBOOL AccessStatus)
736 FIXME_(advapi)("(%p, %04x, %08lx, %p, %p, %p, %p, %p), stub\n",
737 pSecurityDescriptor, ClientToken, DesiredAccess, GenericMapping,
738 PrivilegeSet, PrivilegeSetLength, GrantedAccess, AccessStatus);
739 *AccessStatus = TRUE;
743 /*************************************************************************
744 * SetThreadToken [ADVAPI32.231]
746 * Assigns an "impersonation token" to a thread so it can assume the
747 * security privledges of another thread or process. Can also remove
748 * a previously assigned token. Only supported on NT - it's a stub
749 * exactly like this one on Win9X.
753 BOOL WINAPI SetThreadToken(PHANDLE thread, HANDLE token)
755 FIXME_(advapi)("(%p, %x): stub\n", thread, token);
757 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);