4 * Copyright 1996-1998 Marcus Meissner
5 * Copyright 2003 CodeWeavers Inc. (Ulrich Czekalla)
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
36 #define WIN32_NO_STATUS
38 #include "wine/exception.h"
39 #include "ntdll_misc.h"
40 #include "wine/library.h"
41 #include "wine/unicode.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
46 #define NT_SUCCESS(status) (status == STATUS_SUCCESS)
48 /* helper function to copy an ACL */
49 static BOOLEAN copy_acl(DWORD nDestinationAclLength, PACL pDestinationAcl, PACL pSourceAcl)
53 if (!pSourceAcl || !RtlValidAcl(pSourceAcl))
56 size = ((ACL *)pSourceAcl)->AclSize;
57 if (nDestinationAclLength < size)
60 memmove(pDestinationAcl, pSourceAcl, size);
64 /* generically adds an ACE to an ACL */
65 static NTSTATUS add_access_ace(PACL pAcl, DWORD dwAceRevision, DWORD dwAceFlags,
66 DWORD dwAccessMask, PSID pSid, DWORD dwAceType)
68 ACE_HEADER *pAceHeader;
74 if (!RtlValidSid(pSid))
75 return STATUS_INVALID_SID;
77 if (pAcl->AclRevision > MAX_ACL_REVISION || dwAceRevision > MAX_ACL_REVISION)
78 return STATUS_REVISION_MISMATCH;
80 if (!RtlValidAcl(pAcl))
81 return STATUS_INVALID_ACL;
83 if (!RtlFirstFreeAce(pAcl, &pAceHeader))
84 return STATUS_INVALID_ACL;
87 return STATUS_ALLOTTED_SPACE_EXCEEDED;
89 /* calculate generic size of the ACE */
90 dwLengthSid = RtlLengthSid(pSid);
91 dwAceSize = sizeof(ACE_HEADER) + sizeof(DWORD) + dwLengthSid;
92 if ((char *)pAceHeader + dwAceSize > (char *)pAcl + pAcl->AclSize)
93 return STATUS_ALLOTTED_SPACE_EXCEEDED;
95 /* fill the new ACE */
96 pAceHeader->AceType = dwAceType;
97 pAceHeader->AceFlags = dwAceFlags;
98 pAceHeader->AceSize = dwAceSize;
100 /* skip past the ACE_HEADER of the ACE */
101 pAccessMask = (DWORD *)(pAceHeader + 1);
102 *pAccessMask = dwAccessMask;
104 /* skip past ACE->Mask */
105 pSidStart = pAccessMask + 1;
106 RtlCopySid(dwLengthSid, (PSID)pSidStart, pSid);
108 pAcl->AclRevision = max(pAcl->AclRevision, dwAceRevision);
111 return STATUS_SUCCESS;
118 /******************************************************************************
119 * RtlAllocateAndInitializeSid [NTDLL.@]
122 NTSTATUS WINAPI RtlAllocateAndInitializeSid (
123 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,
133 TRACE("(%p, 0x%04x,0x%08x,0x%08x,0x%08x,0x%08x,0x%08x,0x%08x,0x%08x,0x%08x,%p)\n",
134 pIdentifierAuthority,nSubAuthorityCount,
135 nSubAuthority0, nSubAuthority1, nSubAuthority2, nSubAuthority3,
136 nSubAuthority4, nSubAuthority5, nSubAuthority6, nSubAuthority7, pSid);
138 if (nSubAuthorityCount > 8) return STATUS_INVALID_SID;
140 if (!(tmp_sid= RtlAllocateHeap( GetProcessHeap(), 0,
141 RtlLengthRequiredSid(nSubAuthorityCount))))
142 return STATUS_NO_MEMORY;
144 tmp_sid->Revision = SID_REVISION;
146 if (pIdentifierAuthority)
147 memcpy(&tmp_sid->IdentifierAuthority, pIdentifierAuthority, sizeof(SID_IDENTIFIER_AUTHORITY));
148 tmp_sid->SubAuthorityCount = nSubAuthorityCount;
150 switch( nSubAuthorityCount )
152 case 8: tmp_sid->SubAuthority[7]= nSubAuthority7;
153 case 7: tmp_sid->SubAuthority[6]= nSubAuthority6;
154 case 6: tmp_sid->SubAuthority[5]= nSubAuthority5;
155 case 5: tmp_sid->SubAuthority[4]= nSubAuthority4;
156 case 4: tmp_sid->SubAuthority[3]= nSubAuthority3;
157 case 3: tmp_sid->SubAuthority[2]= nSubAuthority2;
158 case 2: tmp_sid->SubAuthority[1]= nSubAuthority1;
159 case 1: tmp_sid->SubAuthority[0]= nSubAuthority0;
163 return STATUS_SUCCESS;
166 /******************************************************************************
167 * RtlEqualSid [NTDLL.@]
169 * Determine if two SIDs are equal.
172 * pSid1 [I] Source SID
173 * pSid2 [I] SID to compare with
176 * TRUE, if pSid1 is equal to pSid2,
179 BOOL WINAPI RtlEqualSid( PSID pSid1, PSID pSid2 )
181 if (!RtlValidSid(pSid1) || !RtlValidSid(pSid2))
184 if (*RtlSubAuthorityCountSid(pSid1) != *RtlSubAuthorityCountSid(pSid2))
187 if (memcmp(pSid1, pSid2, RtlLengthSid(pSid1)) != 0)
193 /******************************************************************************
194 * RtlEqualPrefixSid [NTDLL.@]
196 BOOL WINAPI RtlEqualPrefixSid (PSID pSid1, PSID pSid2)
198 if (!RtlValidSid(pSid1) || !RtlValidSid(pSid2))
201 if (*RtlSubAuthorityCountSid(pSid1) != *RtlSubAuthorityCountSid(pSid2))
204 if (memcmp(pSid1, pSid2, RtlLengthRequiredSid(((SID*)pSid1)->SubAuthorityCount - 1)) != 0)
211 /******************************************************************************
212 * RtlFreeSid [NTDLL.@]
214 * Free the resources used by a SID.
217 * pSid [I] SID to Free.
222 DWORD WINAPI RtlFreeSid(PSID pSid)
224 TRACE("(%p)\n", pSid);
225 RtlFreeHeap( GetProcessHeap(), 0, pSid );
226 return STATUS_SUCCESS;
229 /**************************************************************************
230 * RtlLengthRequiredSid [NTDLL.@]
232 * Determine the amount of memory a SID will use
235 * nrofsubauths [I] Number of Sub Authorities in the SID.
238 * The size, in bytes, of a SID with nrofsubauths Sub Authorities.
240 DWORD WINAPI RtlLengthRequiredSid(DWORD nrofsubauths)
242 return (nrofsubauths-1)*sizeof(DWORD) + sizeof(SID);
245 /**************************************************************************
246 * RtlLengthSid [NTDLL.@]
248 * Determine the amount of memory a SID is using
251 * pSid [I] SID to get the size of.
254 * The size, in bytes, of pSid.
256 DWORD WINAPI RtlLengthSid(PSID pSid)
258 TRACE("sid=%p\n",pSid);
260 return RtlLengthRequiredSid(*RtlSubAuthorityCountSid(pSid));
263 /**************************************************************************
264 * RtlInitializeSid [NTDLL.@]
269 * pSid [I] SID to initialise
270 * pIdentifierAuthority [I] Identifier Authority
271 * nSubAuthorityCount [I] Number of Sub Authorities
274 * Success: TRUE. pSid is initialised with the details given.
275 * Failure: FALSE, if nSubAuthorityCount is >= SID_MAX_SUB_AUTHORITIES.
277 BOOL WINAPI RtlInitializeSid(
279 PSID_IDENTIFIER_AUTHORITY pIdentifierAuthority,
280 BYTE nSubAuthorityCount)
285 if (nSubAuthorityCount >= SID_MAX_SUB_AUTHORITIES)
288 pisid->Revision = SID_REVISION;
289 pisid->SubAuthorityCount = nSubAuthorityCount;
290 if (pIdentifierAuthority)
291 memcpy(&pisid->IdentifierAuthority, pIdentifierAuthority, sizeof (SID_IDENTIFIER_AUTHORITY));
293 for (i = 0; i < nSubAuthorityCount; i++)
294 *RtlSubAuthoritySid(pSid, i) = 0;
299 /**************************************************************************
300 * RtlSubAuthoritySid [NTDLL.@]
302 * Return the Sub Authority of a SID
305 * pSid [I] SID to get the Sub Authority from.
306 * nSubAuthority [I] Sub Authority number.
309 * A pointer to The Sub Authority value of pSid.
311 LPDWORD WINAPI RtlSubAuthoritySid( PSID pSid, DWORD nSubAuthority )
313 return &(((SID*)pSid)->SubAuthority[nSubAuthority]);
316 /**************************************************************************
317 * RtlIdentifierAuthoritySid [NTDLL.@]
319 * Return the Identifier Authority of a SID.
322 * pSid [I] SID to get the Identifier Authority from.
325 * A pointer to the Identifier Authority value of pSid.
327 PSID_IDENTIFIER_AUTHORITY WINAPI RtlIdentifierAuthoritySid( PSID pSid )
329 return &(((SID*)pSid)->IdentifierAuthority);
332 /**************************************************************************
333 * RtlSubAuthorityCountSid [NTDLL.@]
335 * Get the number of Sub Authorities in a SID.
338 * pSid [I] SID to get the count from.
341 * A pointer to the Sub Authority count of pSid.
343 LPBYTE WINAPI RtlSubAuthorityCountSid(PSID pSid)
345 return &(((SID*)pSid)->SubAuthorityCount);
348 /**************************************************************************
349 * RtlCopySid [NTDLL.@]
351 BOOLEAN WINAPI RtlCopySid( DWORD nDestinationSidLength, PSID pDestinationSid, PSID pSourceSid )
353 if (!pSourceSid || !RtlValidSid(pSourceSid) ||
354 (nDestinationSidLength < RtlLengthSid(pSourceSid)))
357 if (nDestinationSidLength < (((SID*)pSourceSid)->SubAuthorityCount*4+8))
360 memmove(pDestinationSid, pSourceSid, ((SID*)pSourceSid)->SubAuthorityCount*4+8);
363 /******************************************************************************
364 * RtlValidSid [NTDLL.@]
366 * Determine if a SID is valid.
369 * pSid [I] SID to check
372 * TRUE if pSid is valid,
375 BOOLEAN WINAPI RtlValidSid( PSID pSid )
381 if (!pSid || ((SID*)pSid)->Revision != SID_REVISION ||
382 ((SID*)pSid)->SubAuthorityCount > SID_MAX_SUB_AUTHORITIES)
389 WARN("(%p): invalid pointer!\n", pSid);
398 * security descriptor functions
401 /**************************************************************************
402 * RtlCreateSecurityDescriptor [NTDLL.@]
404 * Initialise a SECURITY_DESCRIPTOR.
407 * lpsd [O] Descriptor to initialise.
408 * rev [I] Revision, must be set to SECURITY_DESCRIPTOR_REVISION.
411 * Success: STATUS_SUCCESS.
412 * Failure: STATUS_UNKNOWN_REVISION if rev is incorrect.
414 NTSTATUS WINAPI RtlCreateSecurityDescriptor(
415 PSECURITY_DESCRIPTOR lpsd,
418 if (rev!=SECURITY_DESCRIPTOR_REVISION)
419 return STATUS_UNKNOWN_REVISION;
420 memset(lpsd,'\0',sizeof(SECURITY_DESCRIPTOR));
421 ((SECURITY_DESCRIPTOR*)lpsd)->Revision = SECURITY_DESCRIPTOR_REVISION;
422 return STATUS_SUCCESS;
425 /**************************************************************************
426 * RtlCopySecurityDescriptor [NTDLL.@]
428 * Copies an absolute or sefl-relative SECURITY_DESCRIPTOR.
431 * pSourceSD [O] SD to copy from.
432 * pDestinationSD [I] Destination SD.
435 * Success: STATUS_SUCCESS.
436 * Failure: STATUS_UNKNOWN_REVISION if rev is incorrect.
438 NTSTATUS WINAPI RtlCopySecurityDescriptor(PSECURITY_DESCRIPTOR pSourceSD, PSECURITY_DESCRIPTOR pDestinationSD)
440 SECURITY_DESCRIPTOR *srcSD = (SECURITY_DESCRIPTOR *)pSourceSD;
441 SECURITY_DESCRIPTOR *destSD = (SECURITY_DESCRIPTOR *)pDestinationSD;
444 BOOLEAN defaulted, present;
446 BOOL isSelfRelative = srcSD->Control & SE_SELF_RELATIVE;
448 if (srcSD->Revision != SECURITY_DESCRIPTOR_REVISION)
449 return STATUS_UNKNOWN_REVISION;
451 /* copy initial data */
452 destSD->Revision = srcSD->Revision;
453 destSD->Sbz1 = srcSD->Sbz1;
454 destSD->Control = srcSD->Control;
457 RtlGetOwnerSecurityDescriptor(pSourceSD, &Owner, &defaulted);
458 length = RtlLengthSid(Owner);
462 destSD->Owner = srcSD->Owner;
463 RtlCopySid(length, (LPBYTE)destSD + (DWORD_PTR)destSD->Owner, Owner);
467 destSD->Owner = RtlAllocateHeap(GetProcessHeap(), 0, length);
468 RtlCopySid(length, destSD->Owner, Owner);
472 RtlGetGroupSecurityDescriptor(pSourceSD, &Group, &defaulted);
473 length = RtlLengthSid(Group);
477 destSD->Group = srcSD->Group;
478 RtlCopySid(length, (LPBYTE)destSD + (DWORD_PTR)destSD->Group, Group);
482 destSD->Group = RtlAllocateHeap(GetProcessHeap(), 0, length);
483 RtlCopySid(length, destSD->Group, Group);
487 if (srcSD->Control & SE_DACL_PRESENT)
489 RtlGetDaclSecurityDescriptor(pSourceSD, &present, &Dacl, &defaulted);
490 length = Dacl->AclSize;
494 destSD->Dacl = srcSD->Dacl;
495 copy_acl(length, (PACL)((LPBYTE)destSD + (DWORD_PTR)destSD->Dacl), Dacl);
499 destSD->Dacl = RtlAllocateHeap(GetProcessHeap(), 0, length);
500 copy_acl(length, destSD->Dacl, Dacl);
505 if (srcSD->Control & SE_SACL_PRESENT)
507 RtlGetSaclSecurityDescriptor(pSourceSD, &present, &Sacl, &defaulted);
508 length = Sacl->AclSize;
512 destSD->Sacl = srcSD->Sacl;
513 copy_acl(length, (PACL)((LPBYTE)destSD + (DWORD_PTR)destSD->Sacl), Sacl);
517 destSD->Sacl = RtlAllocateHeap(GetProcessHeap(), 0, length);
518 copy_acl(length, destSD->Sacl, Sacl);
522 return STATUS_SUCCESS;
525 /**************************************************************************
526 * RtlValidSecurityDescriptor [NTDLL.@]
528 * Determine if a SECURITY_DESCRIPTOR is valid.
531 * SecurityDescriptor [I] Descriptor to check.
534 * Success: STATUS_SUCCESS.
535 * Failure: STATUS_INVALID_SECURITY_DESCR or STATUS_UNKNOWN_REVISION.
537 NTSTATUS WINAPI RtlValidSecurityDescriptor(
538 PSECURITY_DESCRIPTOR SecurityDescriptor)
540 if ( ! SecurityDescriptor )
541 return STATUS_INVALID_SECURITY_DESCR;
542 if ( ((SECURITY_DESCRIPTOR*)SecurityDescriptor)->Revision != SECURITY_DESCRIPTOR_REVISION )
543 return STATUS_UNKNOWN_REVISION;
545 return STATUS_SUCCESS;
548 /**************************************************************************
549 * RtlLengthSecurityDescriptor [NTDLL.@]
551 ULONG WINAPI RtlLengthSecurityDescriptor(
552 PSECURITY_DESCRIPTOR pSecurityDescriptor)
554 SECURITY_DESCRIPTOR* lpsd=pSecurityDescriptor;
555 ULONG_PTR offset = 0;
556 ULONG Size = SECURITY_DESCRIPTOR_MIN_LENGTH;
561 if ( lpsd->Control & SE_SELF_RELATIVE)
562 offset = (ULONG_PTR) lpsd;
564 if ( lpsd->Owner != NULL )
565 Size += RtlLengthSid((PSID)((LPBYTE)lpsd->Owner + offset));
567 if ( lpsd->Group != NULL )
568 Size += RtlLengthSid((PSID)((LPBYTE)lpsd->Group + offset));
570 if ( (lpsd->Control & SE_SACL_PRESENT) &&
572 Size += ((PACL)((LPBYTE)lpsd->Sacl + offset))->AclSize;
574 if ( (lpsd->Control & SE_DACL_PRESENT) &&
576 Size += ((PACL)((LPBYTE)lpsd->Dacl + offset))->AclSize;
581 /******************************************************************************
582 * RtlGetDaclSecurityDescriptor [NTDLL.@]
585 NTSTATUS WINAPI RtlGetDaclSecurityDescriptor(
586 IN PSECURITY_DESCRIPTOR pSecurityDescriptor,
587 OUT PBOOLEAN lpbDaclPresent,
589 OUT PBOOLEAN lpbDaclDefaulted)
591 SECURITY_DESCRIPTOR* lpsd=pSecurityDescriptor;
593 TRACE("(%p,%p,%p,%p)\n",
594 pSecurityDescriptor, lpbDaclPresent, pDacl, lpbDaclDefaulted);
596 if (lpsd->Revision != SECURITY_DESCRIPTOR_REVISION)
597 return STATUS_UNKNOWN_REVISION ;
599 if ( (*lpbDaclPresent = (SE_DACL_PRESENT & lpsd->Control) ? 1 : 0) )
601 if ( SE_SELF_RELATIVE & lpsd->Control)
602 *pDacl = (PACL) ((LPBYTE)lpsd + (DWORD_PTR)lpsd->Dacl);
606 *lpbDaclDefaulted = (( SE_DACL_DEFAULTED & lpsd->Control ) ? 1 : 0);
609 return STATUS_SUCCESS;
612 /**************************************************************************
613 * RtlSetDaclSecurityDescriptor [NTDLL.@]
615 NTSTATUS WINAPI RtlSetDaclSecurityDescriptor (
616 PSECURITY_DESCRIPTOR pSecurityDescriptor,
619 BOOLEAN dacldefaulted )
621 SECURITY_DESCRIPTOR* lpsd=pSecurityDescriptor;
623 if (lpsd->Revision!=SECURITY_DESCRIPTOR_REVISION)
624 return STATUS_UNKNOWN_REVISION;
625 if (lpsd->Control & SE_SELF_RELATIVE)
626 return STATUS_INVALID_SECURITY_DESCR;
629 { lpsd->Control &= ~SE_DACL_PRESENT;
633 lpsd->Control |= SE_DACL_PRESENT;
637 lpsd->Control |= SE_DACL_DEFAULTED;
639 lpsd->Control &= ~SE_DACL_DEFAULTED;
641 return STATUS_SUCCESS;
644 /******************************************************************************
645 * RtlGetSaclSecurityDescriptor [NTDLL.@]
648 NTSTATUS WINAPI RtlGetSaclSecurityDescriptor(
649 IN PSECURITY_DESCRIPTOR pSecurityDescriptor,
650 OUT PBOOLEAN lpbSaclPresent,
652 OUT PBOOLEAN lpbSaclDefaulted)
654 SECURITY_DESCRIPTOR* lpsd=pSecurityDescriptor;
656 TRACE("(%p,%p,%p,%p)\n",
657 pSecurityDescriptor, lpbSaclPresent, *pSacl, lpbSaclDefaulted);
659 if (lpsd->Revision != SECURITY_DESCRIPTOR_REVISION)
660 return STATUS_UNKNOWN_REVISION;
662 if ( (*lpbSaclPresent = (SE_SACL_PRESENT & lpsd->Control) ? 1 : 0) )
664 if (SE_SELF_RELATIVE & lpsd->Control)
665 *pSacl = (PACL) ((LPBYTE)lpsd + (DWORD_PTR)lpsd->Sacl);
669 *lpbSaclDefaulted = (( SE_SACL_DEFAULTED & lpsd->Control ) ? 1 : 0);
672 return STATUS_SUCCESS;
675 /**************************************************************************
676 * RtlSetSaclSecurityDescriptor [NTDLL.@]
678 NTSTATUS WINAPI RtlSetSaclSecurityDescriptor (
679 PSECURITY_DESCRIPTOR pSecurityDescriptor,
682 BOOLEAN sacldefaulted)
684 SECURITY_DESCRIPTOR* lpsd=pSecurityDescriptor;
686 if (lpsd->Revision!=SECURITY_DESCRIPTOR_REVISION)
687 return STATUS_UNKNOWN_REVISION;
688 if (lpsd->Control & SE_SELF_RELATIVE)
689 return STATUS_INVALID_SECURITY_DESCR;
691 lpsd->Control &= ~SE_SACL_PRESENT;
694 lpsd->Control |= SE_SACL_PRESENT;
697 lpsd->Control |= SE_SACL_DEFAULTED;
699 lpsd->Control &= ~SE_SACL_DEFAULTED;
700 return STATUS_SUCCESS;
703 /**************************************************************************
704 * RtlGetOwnerSecurityDescriptor [NTDLL.@]
706 NTSTATUS WINAPI RtlGetOwnerSecurityDescriptor(
707 PSECURITY_DESCRIPTOR pSecurityDescriptor,
709 PBOOLEAN OwnerDefaulted)
711 SECURITY_DESCRIPTOR* lpsd=pSecurityDescriptor;
713 if ( !lpsd || !Owner || !OwnerDefaulted )
714 return STATUS_INVALID_PARAMETER;
716 if ( lpsd->Control & SE_OWNER_DEFAULTED )
717 *OwnerDefaulted = TRUE;
719 *OwnerDefaulted = FALSE;
721 if (lpsd->Owner != NULL)
723 if (lpsd->Control & SE_SELF_RELATIVE)
724 *Owner = (PSID)((LPBYTE)lpsd + (ULONG_PTR)lpsd->Owner);
726 *Owner = lpsd->Owner;
732 return STATUS_SUCCESS;
735 /**************************************************************************
736 * RtlSetOwnerSecurityDescriptor [NTDLL.@]
738 NTSTATUS WINAPI RtlSetOwnerSecurityDescriptor(
739 PSECURITY_DESCRIPTOR pSecurityDescriptor,
741 BOOLEAN ownerdefaulted)
743 SECURITY_DESCRIPTOR* lpsd=pSecurityDescriptor;
745 if (lpsd->Revision!=SECURITY_DESCRIPTOR_REVISION)
746 return STATUS_UNKNOWN_REVISION;
747 if (lpsd->Control & SE_SELF_RELATIVE)
748 return STATUS_INVALID_SECURITY_DESCR;
752 lpsd->Control |= SE_OWNER_DEFAULTED;
754 lpsd->Control &= ~SE_OWNER_DEFAULTED;
755 return STATUS_SUCCESS;
758 /**************************************************************************
759 * RtlSetGroupSecurityDescriptor [NTDLL.@]
761 NTSTATUS WINAPI RtlSetGroupSecurityDescriptor (
762 PSECURITY_DESCRIPTOR pSecurityDescriptor,
764 BOOLEAN groupdefaulted)
766 SECURITY_DESCRIPTOR* lpsd=pSecurityDescriptor;
768 if (lpsd->Revision!=SECURITY_DESCRIPTOR_REVISION)
769 return STATUS_UNKNOWN_REVISION;
770 if (lpsd->Control & SE_SELF_RELATIVE)
771 return STATUS_INVALID_SECURITY_DESCR;
775 lpsd->Control |= SE_GROUP_DEFAULTED;
777 lpsd->Control &= ~SE_GROUP_DEFAULTED;
778 return STATUS_SUCCESS;
781 /**************************************************************************
782 * RtlGetGroupSecurityDescriptor [NTDLL.@]
784 NTSTATUS WINAPI RtlGetGroupSecurityDescriptor(
785 PSECURITY_DESCRIPTOR pSecurityDescriptor,
787 PBOOLEAN GroupDefaulted)
789 SECURITY_DESCRIPTOR* lpsd=pSecurityDescriptor;
791 if ( !lpsd || !Group || !GroupDefaulted )
792 return STATUS_INVALID_PARAMETER;
794 if ( lpsd->Control & SE_GROUP_DEFAULTED )
795 *GroupDefaulted = TRUE;
797 *GroupDefaulted = FALSE;
799 if (lpsd->Group != NULL)
801 if (lpsd->Control & SE_SELF_RELATIVE)
802 *Group = (PSID)((LPBYTE)lpsd + (ULONG_PTR)lpsd->Group);
804 *Group = lpsd->Group;
809 return STATUS_SUCCESS;
812 /**************************************************************************
813 * RtlMakeSelfRelativeSD [NTDLL.@]
815 NTSTATUS WINAPI RtlMakeSelfRelativeSD(
816 IN PSECURITY_DESCRIPTOR pAbsoluteSecurityDescriptor,
817 IN PSECURITY_DESCRIPTOR pSelfRelativeSecurityDescriptor,
818 IN OUT LPDWORD lpdwBufferLength)
822 SECURITY_DESCRIPTOR* pAbs = pAbsoluteSecurityDescriptor;
823 SECURITY_DESCRIPTOR* pRel = pSelfRelativeSecurityDescriptor;
825 TRACE(" %p %p %p(%d)\n", pAbs, pRel, lpdwBufferLength,
826 lpdwBufferLength ? *lpdwBufferLength: -1);
828 if (!lpdwBufferLength || !pAbs)
829 return STATUS_INVALID_PARAMETER;
831 length = RtlLengthSecurityDescriptor(pAbs);
832 if (*lpdwBufferLength < length)
834 *lpdwBufferLength = length;
835 return STATUS_BUFFER_TOO_SMALL;
839 return STATUS_INVALID_PARAMETER;
841 if (pAbs->Control & SE_SELF_RELATIVE)
843 memcpy(pRel, pAbs, length);
844 return STATUS_SUCCESS;
847 pRel->Revision = pAbs->Revision;
848 pRel->Sbz1 = pAbs->Sbz1;
849 pRel->Control = pAbs->Control | SE_SELF_RELATIVE;
851 offsetRel = sizeof(SECURITY_DESCRIPTOR);
854 pRel->Owner = (PSID) offsetRel;
855 length = RtlLengthSid(pAbs->Owner);
856 memcpy((LPBYTE)pRel + offsetRel, pAbs->Owner, length);
866 pRel->Group = (PSID) offsetRel;
867 length = RtlLengthSid(pAbs->Group);
868 memcpy((LPBYTE)pRel + offsetRel, pAbs->Group, length);
878 pRel->Sacl = (PACL) offsetRel;
879 length = pAbs->Sacl->AclSize;
880 memcpy((LPBYTE)pRel + offsetRel, pAbs->Sacl, length);
890 pRel->Dacl = (PACL) offsetRel;
891 length = pAbs->Dacl->AclSize;
892 memcpy((LPBYTE)pRel + offsetRel, pAbs->Dacl, length);
899 return STATUS_SUCCESS;
903 /**************************************************************************
904 * RtlSelfRelativeToAbsoluteSD [NTDLL.@]
906 NTSTATUS WINAPI RtlSelfRelativeToAbsoluteSD(
907 IN PSECURITY_DESCRIPTOR pSelfRelativeSecurityDescriptor,
908 OUT PSECURITY_DESCRIPTOR pAbsoluteSecurityDescriptor,
909 OUT LPDWORD lpdwAbsoluteSecurityDescriptorSize,
911 OUT LPDWORD lpdwDaclSize,
913 OUT LPDWORD lpdwSaclSize,
915 OUT LPDWORD lpdwOwnerSize,
916 OUT PSID pPrimaryGroup,
917 OUT LPDWORD lpdwPrimaryGroupSize)
919 NTSTATUS status = STATUS_SUCCESS;
920 SECURITY_DESCRIPTOR* pAbs = pAbsoluteSecurityDescriptor;
921 SECURITY_DESCRIPTOR* pRel = pSelfRelativeSecurityDescriptor;
924 !lpdwAbsoluteSecurityDescriptorSize ||
928 !lpdwPrimaryGroupSize ||
929 ~pRel->Control & SE_SELF_RELATIVE)
930 return STATUS_INVALID_PARAMETER;
932 /* Confirm buffers are sufficiently large */
933 if (*lpdwAbsoluteSecurityDescriptorSize < sizeof(SECURITY_DESCRIPTOR))
935 *lpdwAbsoluteSecurityDescriptorSize = sizeof(SECURITY_DESCRIPTOR);
936 status = STATUS_BUFFER_TOO_SMALL;
939 if (pRel->Control & SE_DACL_PRESENT &&
940 *lpdwDaclSize < ((PACL)((LPBYTE)pRel->Dacl + (ULONG_PTR)pRel))->AclSize)
942 *lpdwDaclSize = ((PACL)((LPBYTE)pRel->Dacl + (ULONG_PTR)pRel))->AclSize;
943 status = STATUS_BUFFER_TOO_SMALL;
946 if (pRel->Control & SE_SACL_PRESENT &&
947 *lpdwSaclSize < ((PACL)((LPBYTE)pRel->Sacl + (ULONG_PTR)pRel))->AclSize)
949 *lpdwSaclSize = ((PACL)((LPBYTE)pRel->Sacl + (ULONG_PTR)pRel))->AclSize;
950 status = STATUS_BUFFER_TOO_SMALL;
954 *lpdwOwnerSize < RtlLengthSid((PSID)((LPBYTE)pRel->Owner + (ULONG_PTR)pRel)))
956 *lpdwOwnerSize = RtlLengthSid((PSID)((LPBYTE)pRel->Owner + (ULONG_PTR)pRel));
957 status = STATUS_BUFFER_TOO_SMALL;
961 *lpdwPrimaryGroupSize < RtlLengthSid((PSID)((LPBYTE)pRel->Group + (ULONG_PTR)pRel)))
963 *lpdwPrimaryGroupSize = RtlLengthSid((PSID)((LPBYTE)pRel->Group + (ULONG_PTR)pRel));
964 status = STATUS_BUFFER_TOO_SMALL;
967 if (status != STATUS_SUCCESS)
970 /* Copy structures, and clear the ones we don't set */
971 pAbs->Revision = pRel->Revision;
972 pAbs->Control = pRel->Control & ~SE_SELF_RELATIVE;
978 if (pRel->Control & SE_SACL_PRESENT)
980 PACL pAcl = (PACL)((LPBYTE)pRel->Sacl + (ULONG_PTR)pRel);
982 memcpy(pSacl, pAcl, pAcl->AclSize);
986 if (pRel->Control & SE_DACL_PRESENT)
988 PACL pAcl = (PACL)((LPBYTE)pRel->Dacl + (ULONG_PTR)pRel);
989 memcpy(pDacl, pAcl, pAcl->AclSize);
995 PSID psid = (PSID)((LPBYTE)pRel->Owner + (ULONG_PTR)pRel);
996 memcpy(pOwner, psid, RtlLengthSid(psid));
997 pAbs->Owner = pOwner;
1002 PSID psid = (PSID)((LPBYTE)pRel->Group + (ULONG_PTR)pRel);
1003 memcpy(pPrimaryGroup, psid, RtlLengthSid(psid));
1004 pAbs->Group = pPrimaryGroup;
1010 /******************************************************************************
1011 * RtlGetControlSecurityDescriptor (NTDLL.@)
1013 NTSTATUS WINAPI RtlGetControlSecurityDescriptor(
1014 PSECURITY_DESCRIPTOR pSecurityDescriptor,
1015 PSECURITY_DESCRIPTOR_CONTROL pControl,
1016 LPDWORD lpdwRevision)
1018 SECURITY_DESCRIPTOR *lpsd = pSecurityDescriptor;
1020 TRACE("(%p,%p,%p)\n",pSecurityDescriptor,pControl,lpdwRevision);
1022 *lpdwRevision = lpsd->Revision;
1024 if (*lpdwRevision != SECURITY_DESCRIPTOR_REVISION)
1025 return STATUS_UNKNOWN_REVISION;
1027 *pControl = lpsd->Control;
1029 return STATUS_SUCCESS;
1033 /**************************************************************************
1034 * RtlAbsoluteToSelfRelativeSD [NTDLL.@]
1036 NTSTATUS WINAPI RtlAbsoluteToSelfRelativeSD(
1037 PSECURITY_DESCRIPTOR AbsoluteSecurityDescriptor,
1038 PSECURITY_DESCRIPTOR SelfRelativeSecurityDescriptor,
1039 PULONG BufferLength)
1041 SECURITY_DESCRIPTOR *abs = AbsoluteSecurityDescriptor;
1043 TRACE("%p %p %p\n", AbsoluteSecurityDescriptor,
1044 SelfRelativeSecurityDescriptor, BufferLength);
1046 if (abs->Control & SE_SELF_RELATIVE)
1047 return STATUS_BAD_DESCRIPTOR_FORMAT;
1049 return RtlMakeSelfRelativeSD(AbsoluteSecurityDescriptor,
1050 SelfRelativeSecurityDescriptor, BufferLength);
1055 * access control list's
1058 /**************************************************************************
1059 * RtlCreateAcl [NTDLL.@]
1062 * This should return NTSTATUS
1064 NTSTATUS WINAPI RtlCreateAcl(PACL acl,DWORD size,DWORD rev)
1066 TRACE("%p 0x%08x 0x%08x\n", acl, size, rev);
1068 if (rev!=ACL_REVISION)
1069 return STATUS_INVALID_PARAMETER;
1070 if (size<sizeof(ACL))
1071 return STATUS_BUFFER_TOO_SMALL;
1073 return STATUS_INVALID_PARAMETER;
1075 memset(acl,'\0',sizeof(ACL));
1076 acl->AclRevision = rev;
1077 acl->AclSize = size;
1079 return STATUS_SUCCESS;
1082 /**************************************************************************
1083 * RtlFirstFreeAce [NTDLL.@]
1084 * looks for the AceCount+1 ACE, and if it is still within the alloced
1085 * ACL, return a pointer to it
1087 BOOLEAN WINAPI RtlFirstFreeAce(
1095 ace = (PACE_HEADER)(acl+1);
1096 for (i=0;i<acl->AceCount;i++) {
1097 if ((BYTE *)ace >= (BYTE *)acl + acl->AclSize)
1099 ace = (PACE_HEADER)(((BYTE*)ace)+ace->AceSize);
1101 if ((BYTE *)ace >= (BYTE *)acl + acl->AclSize)
1107 /**************************************************************************
1108 * RtlAddAce [NTDLL.@]
1110 NTSTATUS WINAPI RtlAddAce(
1114 PACE_HEADER acestart,
1117 PACE_HEADER ace,targetace;
1120 if (acl->AclRevision != ACL_REVISION)
1121 return STATUS_INVALID_PARAMETER;
1122 if (!RtlFirstFreeAce(acl,&targetace))
1123 return STATUS_INVALID_PARAMETER;
1124 nrofaces=0;ace=acestart;
1125 while (((BYTE *)ace - (BYTE *)acestart) < acelen) {
1127 ace = (PACE_HEADER)(((BYTE*)ace)+ace->AceSize);
1129 if ((BYTE *)targetace + acelen > (BYTE *)acl + acl->AclSize) /* too much aces */
1130 return STATUS_INVALID_PARAMETER;
1131 memcpy((LPBYTE)targetace,acestart,acelen);
1132 acl->AceCount+=nrofaces;
1133 return STATUS_SUCCESS;
1136 /**************************************************************************
1137 * RtlDeleteAce [NTDLL.@]
1139 NTSTATUS WINAPI RtlDeleteAce(PACL pAcl, DWORD dwAceIndex)
1144 status = RtlGetAce(pAcl,dwAceIndex,(LPVOID*)&pAce);
1146 if (STATUS_SUCCESS == status)
1151 /* skip over the ACE we are deleting */
1152 pcAce = (PACE_HEADER)(((BYTE*)pAce)+pAce->AceSize);
1155 /* calculate the length of the rest */
1156 for (; dwAceIndex < pAcl->AceCount; dwAceIndex++)
1158 len += pcAce->AceSize;
1159 pcAce = (PACE_HEADER)(((BYTE*)pcAce) + pcAce->AceSize);
1162 /* slide them all backwards */
1163 memmove(pAce, ((BYTE*)pAce)+pAce->AceSize, len);
1167 TRACE("pAcl=%p dwAceIndex=%d status=0x%08x\n", pAcl, dwAceIndex, status);
1172 /******************************************************************************
1173 * RtlAddAccessAllowedAce [NTDLL.@]
1175 NTSTATUS WINAPI RtlAddAccessAllowedAce(
1177 IN DWORD dwAceRevision,
1178 IN DWORD AccessMask,
1181 return RtlAddAccessAllowedAceEx( pAcl, dwAceRevision, 0, AccessMask, pSid);
1184 /******************************************************************************
1185 * RtlAddAccessAllowedAceEx [NTDLL.@]
1187 NTSTATUS WINAPI RtlAddAccessAllowedAceEx(
1189 IN DWORD dwAceRevision,
1191 IN DWORD AccessMask,
1194 TRACE("(%p,0x%08x,0x%08x,%p)\n", pAcl, dwAceRevision, AccessMask, pSid);
1196 return add_access_ace(pAcl, dwAceRevision, AceFlags,
1197 AccessMask, pSid, ACCESS_ALLOWED_ACE_TYPE);
1200 /******************************************************************************
1201 * RtlAddAccessDeniedAce [NTDLL.@]
1203 NTSTATUS WINAPI RtlAddAccessDeniedAce(
1205 IN DWORD dwAceRevision,
1206 IN DWORD AccessMask,
1209 return RtlAddAccessDeniedAceEx( pAcl, dwAceRevision, 0, AccessMask, pSid);
1212 /******************************************************************************
1213 * RtlAddAccessDeniedAceEx [NTDLL.@]
1215 NTSTATUS WINAPI RtlAddAccessDeniedAceEx(
1217 IN DWORD dwAceRevision,
1219 IN DWORD AccessMask,
1222 TRACE("(%p,0x%08x,0x%08x,%p)\n", pAcl, dwAceRevision, AccessMask, pSid);
1224 return add_access_ace(pAcl, dwAceRevision, AceFlags,
1225 AccessMask, pSid, ACCESS_DENIED_ACE_TYPE);
1228 /**************************************************************************
1229 * RtlAddAuditAccessAce [NTDLL.@]
1231 NTSTATUS WINAPI RtlAddAuditAccessAceEx(
1233 IN DWORD dwAceRevision,
1234 IN DWORD dwAceFlags,
1235 IN DWORD dwAccessMask,
1237 IN BOOL bAuditSuccess,
1238 IN BOOL bAuditFailure)
1240 TRACE("(%p,%d,0x%08x,0x%08x,%p,%u,%u)\n",pAcl,dwAceRevision,dwAceFlags,dwAccessMask,
1241 pSid,bAuditSuccess,bAuditFailure);
1244 dwAceFlags |= SUCCESSFUL_ACCESS_ACE_FLAG;
1247 dwAceFlags |= FAILED_ACCESS_ACE_FLAG;
1249 return add_access_ace(pAcl, dwAceRevision, dwAceFlags,
1250 dwAccessMask, pSid, SYSTEM_AUDIT_ACE_TYPE);
1253 /**************************************************************************
1254 * RtlAddAuditAccessAce [NTDLL.@]
1256 NTSTATUS WINAPI RtlAddAuditAccessAce(
1258 IN DWORD dwAceRevision,
1259 IN DWORD dwAccessMask,
1261 IN BOOL bAuditSuccess,
1262 IN BOOL bAuditFailure)
1264 return RtlAddAuditAccessAceEx(pAcl, dwAceRevision, 0, dwAccessMask, pSid, bAuditSuccess, bAuditFailure);
1267 /******************************************************************************
1268 * RtlValidAcl [NTDLL.@]
1270 BOOLEAN WINAPI RtlValidAcl(PACL pAcl)
1273 TRACE("(%p)\n", pAcl);
1280 if (pAcl->AclRevision != ACL_REVISION)
1284 ace = (PACE_HEADER)(pAcl+1);
1286 for (i=0;i<=pAcl->AceCount;i++)
1288 if ((char *)ace > (char *)pAcl + pAcl->AclSize)
1293 ace = (PACE_HEADER)(((BYTE*)ace)+ace->AceSize);
1299 WARN("(%p): invalid pointer!\n", pAcl);
1306 /******************************************************************************
1307 * RtlGetAce [NTDLL.@]
1309 NTSTATUS WINAPI RtlGetAce(PACL pAcl,DWORD dwAceIndex,LPVOID *pAce )
1313 TRACE("(%p,%d,%p)\n",pAcl,dwAceIndex,pAce);
1315 if ((dwAceIndex < 0) || (dwAceIndex > pAcl->AceCount))
1316 return STATUS_INVALID_PARAMETER;
1318 ace = (PACE_HEADER)(pAcl + 1);
1319 for (;dwAceIndex;dwAceIndex--)
1320 ace = (PACE_HEADER)(((BYTE*)ace)+ace->AceSize);
1322 *pAce = (LPVOID) ace;
1324 return STATUS_SUCCESS;
1331 /******************************************************************************
1332 * RtlAdjustPrivilege [NTDLL.@]
1334 * Enables or disables a privilege from the calling thread or process.
1337 * Privilege [I] Privilege index to change.
1338 * Enable [I] If TRUE, then enable the privilege otherwise disable.
1339 * CurrentThread [I] If TRUE, then enable in calling thread, otherwise process.
1340 * Enabled [O] Whether privilege was previously enabled or disabled.
1343 * Success: STATUS_SUCCESS.
1344 * Failure: NTSTATUS code.
1347 * NtAdjustPrivilegesToken, NtOpenThreadToken, NtOpenProcessToken.
1351 RtlAdjustPrivilege(ULONG Privilege,
1353 BOOLEAN CurrentThread,
1356 TOKEN_PRIVILEGES NewState;
1357 TOKEN_PRIVILEGES OldState;
1362 TRACE("(%d, %s, %s, %p)\n", Privilege, Enable ? "TRUE" : "FALSE",
1363 CurrentThread ? "TRUE" : "FALSE", Enabled);
1367 Status = NtOpenThreadToken(GetCurrentThread(),
1368 TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
1374 Status = NtOpenProcessToken(GetCurrentProcess(),
1375 TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
1379 if (!NT_SUCCESS(Status))
1381 WARN("Retrieving token handle failed (Status %x)\n", Status);
1385 OldState.PrivilegeCount = 1;
1387 NewState.PrivilegeCount = 1;
1388 NewState.Privileges[0].Luid.LowPart = Privilege;
1389 NewState.Privileges[0].Luid.HighPart = 0;
1390 NewState.Privileges[0].Attributes = (Enable) ? SE_PRIVILEGE_ENABLED : 0;
1392 Status = NtAdjustPrivilegesToken(TokenHandle,
1395 sizeof(TOKEN_PRIVILEGES),
1398 NtClose (TokenHandle);
1399 if (Status == STATUS_NOT_ALL_ASSIGNED)
1401 TRACE("Failed to assign all privileges\n");
1402 return STATUS_PRIVILEGE_NOT_HELD;
1404 if (!NT_SUCCESS(Status))
1406 WARN("NtAdjustPrivilegesToken() failed (Status %x)\n", Status);
1410 if (OldState.PrivilegeCount == 0)
1413 *Enabled = (OldState.Privileges[0].Attributes & SE_PRIVILEGE_ENABLED);
1415 return STATUS_SUCCESS;
1418 /******************************************************************************
1419 * RtlImpersonateSelf [NTDLL.@]
1421 * Makes an impersonation token that represents the process user and assigns
1422 * to the current thread.
1425 * ImpersonationLevel [I] Level at which to impersonate.
1428 * Success: STATUS_SUCCESS.
1429 * Failure: NTSTATUS code.
1432 RtlImpersonateSelf(SECURITY_IMPERSONATION_LEVEL ImpersonationLevel)
1435 OBJECT_ATTRIBUTES ObjectAttributes;
1436 HANDLE ProcessToken;
1437 HANDLE ImpersonationToken;
1439 TRACE("(%08x)\n", ImpersonationLevel);
1441 Status = NtOpenProcessToken( NtCurrentProcess(), TOKEN_DUPLICATE,
1443 if (Status != STATUS_SUCCESS)
1446 InitializeObjectAttributes( &ObjectAttributes, NULL, 0, NULL, NULL );
1448 Status = NtDuplicateToken( ProcessToken,
1453 &ImpersonationToken );
1454 if (Status != STATUS_SUCCESS)
1456 NtClose( ProcessToken );
1460 Status = NtSetInformationThread( GetCurrentThread(),
1461 ThreadImpersonationToken,
1462 &ImpersonationToken,
1463 sizeof(ImpersonationToken) );
1465 NtClose( ImpersonationToken );
1466 NtClose( ProcessToken );
1471 /******************************************************************************
1472 * NtAccessCheck [NTDLL.@]
1473 * ZwAccessCheck [NTDLL.@]
1475 * Checks that a user represented by a token is allowed to access an object
1476 * represented by a security descriptor.
1479 * SecurityDescriptor [I] The security descriptor of the object to check.
1480 * ClientToken [I] Token of the user accessing the object.
1481 * DesiredAccess [I] The desired access to the object.
1482 * GenericMapping [I] Mapping used to transform access rights in the SD to their specific forms.
1483 * PrivilegeSet [I/O] Privileges used during the access check.
1484 * ReturnLength [O] Number of bytes stored into PrivilegeSet.
1485 * GrantedAccess [O] The actual access rights granted.
1486 * AccessStatus [O] The status of the access check.
1492 * DesiredAccess may be MAXIMUM_ALLOWED, in which case the function determines
1493 * the maximum access rights allowed by the SD and returns them in
1495 * The SecurityDescriptor must have a valid owner and groups present,
1496 * otherwise the function will fail.
1500 PSECURITY_DESCRIPTOR SecurityDescriptor,
1502 ACCESS_MASK DesiredAccess,
1503 PGENERIC_MAPPING GenericMapping,
1504 PPRIVILEGE_SET PrivilegeSet,
1505 PULONG ReturnLength,
1506 PULONG GrantedAccess,
1507 NTSTATUS *AccessStatus)
1511 TRACE("(%p, %p, %08x, %p, %p, %p, %p, %p)\n",
1512 SecurityDescriptor, ClientToken, DesiredAccess, GenericMapping,
1513 PrivilegeSet, ReturnLength, GrantedAccess, AccessStatus);
1515 SERVER_START_REQ( access_check )
1517 struct security_descriptor sd;
1522 BOOLEAN defaulted, present;
1524 SECURITY_DESCRIPTOR_CONTROL control;
1526 req->handle = ClientToken;
1527 req->desired_access = DesiredAccess;
1528 req->mapping_read = GenericMapping->GenericRead;
1529 req->mapping_write = GenericMapping->GenericWrite;
1530 req->mapping_execute = GenericMapping->GenericExecute;
1531 req->mapping_all = GenericMapping->GenericAll;
1533 /* marshal security descriptor */
1534 RtlGetControlSecurityDescriptor( SecurityDescriptor, &control, &revision );
1535 sd.control = control & ~SE_SELF_RELATIVE;
1536 RtlGetOwnerSecurityDescriptor( SecurityDescriptor, &owner, &defaulted );
1537 sd.owner_len = RtlLengthSid( owner );
1538 RtlGetGroupSecurityDescriptor( SecurityDescriptor, &group, &defaulted );
1539 sd.group_len = RtlLengthSid( group );
1540 RtlGetSaclSecurityDescriptor( SecurityDescriptor, &present, &sacl, &defaulted );
1541 sd.sacl_len = (present ? sacl->AclSize : 0);
1542 RtlGetDaclSecurityDescriptor( SecurityDescriptor, &present, &dacl, &defaulted );
1543 sd.dacl_len = (present ? dacl->AclSize : 0);
1545 wine_server_add_data( req, &sd, sizeof(sd) );
1546 wine_server_add_data( req, owner, sd.owner_len );
1547 wine_server_add_data( req, group, sd.group_len );
1548 wine_server_add_data( req, sacl, sd.sacl_len );
1549 wine_server_add_data( req, dacl, sd.dacl_len );
1551 wine_server_set_reply( req, &PrivilegeSet->Privilege, *ReturnLength - FIELD_OFFSET( PRIVILEGE_SET, Privilege ) );
1553 status = wine_server_call( req );
1555 *ReturnLength = FIELD_OFFSET( PRIVILEGE_SET, Privilege ) + reply->privileges_len;
1556 PrivilegeSet->PrivilegeCount = reply->privileges_len / sizeof(LUID_AND_ATTRIBUTES);
1558 if (status == STATUS_SUCCESS)
1560 *AccessStatus = reply->access_status;
1561 *GrantedAccess = reply->access_granted;
1569 /******************************************************************************
1570 * NtSetSecurityObject [NTDLL.@]
1571 * ZwSetSecurityObject [NTDLL.@]
1573 * Sets specified parts of the object's security descriptor.
1576 * Handle [I] Handle to the object to change security descriptor of.
1577 * SecurityInformation [I] Specifies which parts of the security descriptor to set.
1578 * SecurityDescriptor [I] New parts of a security descriptor for the object.
1584 NTSTATUS WINAPI NtSetSecurityObject(HANDLE Handle,
1585 SECURITY_INFORMATION SecurityInformation,
1586 PSECURITY_DESCRIPTOR SecurityDescriptor)
1589 struct security_descriptor sd;
1590 PACL dacl = NULL, sacl = NULL;
1591 PSID owner = NULL, group = NULL;
1592 BOOLEAN defaulted, present;
1594 SECURITY_DESCRIPTOR_CONTROL control;
1596 TRACE("%p 0x%08x %p\n", Handle, SecurityInformation, SecurityDescriptor);
1598 if (!SecurityDescriptor) return STATUS_ACCESS_VIOLATION;
1600 memset( &sd, 0, sizeof(sd) );
1601 status = RtlGetControlSecurityDescriptor( SecurityDescriptor, &control, &revision );
1602 if (status != STATUS_SUCCESS) return status;
1603 sd.control = control & ~SE_SELF_RELATIVE;
1605 if (SecurityInformation & OWNER_SECURITY_INFORMATION)
1607 status = RtlGetOwnerSecurityDescriptor( SecurityDescriptor, &owner, &defaulted );
1608 if (status != STATUS_SUCCESS) return status;
1609 if (!(sd.owner_len = RtlLengthSid( owner )))
1610 return STATUS_INVALID_SECURITY_DESCR;
1613 if (SecurityInformation & GROUP_SECURITY_INFORMATION)
1615 status = RtlGetGroupSecurityDescriptor( SecurityDescriptor, &group, &defaulted );
1616 if (status != STATUS_SUCCESS) return status;
1617 if (!(sd.group_len = RtlLengthSid( group )))
1618 return STATUS_INVALID_SECURITY_DESCR;
1621 if (SecurityInformation & SACL_SECURITY_INFORMATION)
1623 status = RtlGetSaclSecurityDescriptor( SecurityDescriptor, &present, &sacl, &defaulted );
1624 if (status != STATUS_SUCCESS) return status;
1625 sd.sacl_len = (sacl && present) ? sacl->AclSize : 0;
1626 sd.control |= SE_SACL_PRESENT;
1629 if (SecurityInformation & DACL_SECURITY_INFORMATION)
1631 status = RtlGetDaclSecurityDescriptor( SecurityDescriptor, &present, &dacl, &defaulted );
1632 if (status != STATUS_SUCCESS) return status;
1633 sd.dacl_len = (dacl && present) ? dacl->AclSize : 0;
1634 sd.control |= SE_DACL_PRESENT;
1637 SERVER_START_REQ( set_security_object )
1639 req->handle = Handle;
1640 req->security_info = SecurityInformation;
1642 wine_server_add_data( req, &sd, sizeof(sd) );
1643 wine_server_add_data( req, owner, sd.owner_len );
1644 wine_server_add_data( req, group, sd.group_len );
1645 wine_server_add_data( req, sacl, sd.sacl_len );
1646 wine_server_add_data( req, dacl, sd.dacl_len );
1647 status = wine_server_call( req );
1654 /******************************************************************************
1655 * RtlConvertSidToUnicodeString (NTDLL.@)
1657 * The returned SID is used to access the USER registry hive usually
1659 * the native function returns something like
1660 * "S-1-5-21-0000000000-000000000-0000000000-500";
1662 NTSTATUS WINAPI RtlConvertSidToUnicodeString(
1663 PUNICODE_STRING String,
1665 BOOLEAN AllocateString)
1667 static const WCHAR formatW[] = {'-','%','u',0};
1668 WCHAR buffer[2 + 10 + 10 + 10 * SID_MAX_SUB_AUTHORITIES];
1670 const SID *sid = (const SID *)pSid;
1674 p += sprintfW( p, formatW, sid->Revision );
1675 p += sprintfW( p, formatW, MAKELONG( MAKEWORD( sid->IdentifierAuthority.Value[5],
1676 sid->IdentifierAuthority.Value[4] ),
1677 MAKEWORD( sid->IdentifierAuthority.Value[3],
1678 sid->IdentifierAuthority.Value[2] )));
1679 for (i = 0; i < sid->SubAuthorityCount; i++)
1680 p += sprintfW( p, formatW, sid->SubAuthority[i] );
1682 len = (p + 1 - buffer) * sizeof(WCHAR);
1684 String->Length = len - sizeof(WCHAR);
1687 String->MaximumLength = len;
1688 if (!(String->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
1689 return STATUS_NO_MEMORY;
1691 else if (len > String->MaximumLength) return STATUS_BUFFER_OVERFLOW;
1693 memcpy( String->Buffer, buffer, len );
1694 return STATUS_SUCCESS;
1697 /******************************************************************************
1698 * RtlQueryInformationAcl (NTDLL.@)
1700 NTSTATUS WINAPI RtlQueryInformationAcl(
1702 LPVOID pAclInformation,
1703 DWORD nAclInformationLength,
1704 ACL_INFORMATION_CLASS dwAclInformationClass)
1706 NTSTATUS status = STATUS_SUCCESS;
1708 TRACE("pAcl=%p pAclInfo=%p len=%d, class=%d\n",
1709 pAcl, pAclInformation, nAclInformationLength, dwAclInformationClass);
1711 switch (dwAclInformationClass)
1713 case AclRevisionInformation:
1715 PACL_REVISION_INFORMATION paclrev = (PACL_REVISION_INFORMATION) pAclInformation;
1717 if (nAclInformationLength < sizeof(ACL_REVISION_INFORMATION))
1718 status = STATUS_INVALID_PARAMETER;
1720 paclrev->AclRevision = pAcl->AclRevision;
1725 case AclSizeInformation:
1727 PACL_SIZE_INFORMATION paclsize = (PACL_SIZE_INFORMATION) pAclInformation;
1729 if (nAclInformationLength < sizeof(ACL_SIZE_INFORMATION))
1730 status = STATUS_INVALID_PARAMETER;
1736 paclsize->AceCount = pAcl->AceCount;
1738 paclsize->AclBytesInUse = 0;
1739 ace = (PACE_HEADER) (pAcl + 1);
1741 for (i = 0; i < pAcl->AceCount; i++)
1743 paclsize->AclBytesInUse += ace->AceSize;
1744 ace = (PACE_HEADER)(((BYTE*)ace)+ace->AceSize);
1747 if (pAcl->AclSize < paclsize->AclBytesInUse)
1749 WARN("Acl has %d bytes free\n", paclsize->AclBytesFree);
1750 paclsize->AclBytesFree = 0;
1751 paclsize->AclBytesInUse = pAcl->AclSize;
1754 paclsize->AclBytesFree = pAcl->AclSize - paclsize->AclBytesInUse;
1761 WARN("Unknown AclInformationClass value: %d\n", dwAclInformationClass);
1762 status = STATUS_INVALID_PARAMETER;