sec.c 52.3 KB
Newer Older
Juergen Schmied's avatar
Juergen Schmied committed
1 2 3 4
/*
 *	Security functions
 *
 *	Copyright 1996-1998 Marcus Meissner
5
 * 	Copyright 2003 CodeWeavers Inc. (Ulrich Czekalla)
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
Juergen Schmied's avatar
Juergen Schmied committed
20 21
 */

22
#include "config.h"
23
#include "wine/port.h"
24

25
#include <stdarg.h>
Juergen Schmied's avatar
Juergen Schmied committed
26 27 28 29 30
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#include <math.h>
31 32 33
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
34

35 36
#include "ntstatus.h"
#define WIN32_NO_STATUS
Juergen Schmied's avatar
Juergen Schmied committed
37
#include "windef.h"
38
#include "ntdll_misc.h"
39
#include "wine/exception.h"
40
#include "wine/library.h"
41
#include "wine/unicode.h"
42
#include "wine/debug.h"
Juergen Schmied's avatar
Juergen Schmied committed
43

44
WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
45

46 47
#define NT_SUCCESS(status) (status == STATUS_SUCCESS)

48 49
#define SELF_RELATIVE_FIELD(sd,field) ((BYTE *)(sd) + ((SECURITY_DESCRIPTOR_RELATIVE *)(sd))->field)

50 51 52 53 54 55 56 57 58 59 60 61 62 63
/* helper function to retrieve active length of an ACL */
static size_t acl_bytesInUse(PACL pAcl)
{
    int i;
    size_t bytesInUse = sizeof(ACL);
    PACE_HEADER ace = (PACE_HEADER) (pAcl + 1);
    for (i = 0; i < pAcl->AceCount; i++)
    {
	bytesInUse += ace->AceSize;
	ace = (PACE_HEADER)(((BYTE*)ace)+ace->AceSize);
    }
    return bytesInUse;
}

64 65 66 67 68 69 70
/* helper function to copy an ACL */
static BOOLEAN copy_acl(DWORD nDestinationAclLength, PACL pDestinationAcl, PACL pSourceAcl)
{
    DWORD size;

    if (!pSourceAcl || !RtlValidAcl(pSourceAcl))
        return FALSE;
71 72

    size = pSourceAcl->AclSize;
73 74 75 76 77 78 79
    if (nDestinationAclLength < size)
        return FALSE;

    memmove(pDestinationAcl, pSourceAcl, size);
    return TRUE;
}

80 81 82 83 84
/* generically adds an ACE to an ACL */
static NTSTATUS add_access_ace(PACL pAcl, DWORD dwAceRevision, DWORD dwAceFlags,
                               DWORD dwAccessMask, PSID pSid, DWORD dwAceType)
{
    ACE_HEADER *pAceHeader;
85
    DWORD dwLengthSid;
86
    DWORD dwAceSize;
87 88
    DWORD *pAccessMask;
    DWORD *pSidStart;
89

90
    if (!RtlValidSid(pSid))
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
        return STATUS_INVALID_SID;

    if (pAcl->AclRevision > MAX_ACL_REVISION || dwAceRevision > MAX_ACL_REVISION)
        return STATUS_REVISION_MISMATCH;

    if (!RtlValidAcl(pAcl))
        return STATUS_INVALID_ACL;

    if (!RtlFirstFreeAce(pAcl, &pAceHeader))
        return STATUS_INVALID_ACL;

    if (!pAceHeader)
        return STATUS_ALLOTTED_SPACE_EXCEEDED;

    /* calculate generic size of the ACE */
106 107
    dwLengthSid = RtlLengthSid(pSid);
    dwAceSize = sizeof(ACE_HEADER) + sizeof(DWORD) + dwLengthSid;
108
    if ((char *)pAceHeader + dwAceSize > (char *)pAcl + pAcl->AclSize)
109 110
        return STATUS_ALLOTTED_SPACE_EXCEEDED;

111
    /* fill the new ACE */
112 113 114
    pAceHeader->AceType = dwAceType;
    pAceHeader->AceFlags = dwAceFlags;
    pAceHeader->AceSize = dwAceSize;
115 116 117

    /* skip past the ACE_HEADER of the ACE */
    pAccessMask = (DWORD *)(pAceHeader + 1);
118 119
    *pAccessMask = dwAccessMask;

120 121
    /* skip past ACE->Mask */
    pSidStart = pAccessMask + 1;
122
    RtlCopySid(dwLengthSid, pSidStart, pSid);
123

124
    pAcl->AclRevision = max(pAcl->AclRevision, dwAceRevision);
125 126 127 128 129
    pAcl->AceCount++;

    return STATUS_SUCCESS;
}

Juergen Schmied's avatar
Juergen Schmied committed
130 131 132 133 134
/*
 *	SID FUNCTIONS
 */

/******************************************************************************
135
 *  RtlAllocateAndInitializeSid		[NTDLL.@]
Juergen Schmied's avatar
Juergen Schmied committed
136 137
 *
 */
138
NTSTATUS WINAPI RtlAllocateAndInitializeSid (
139 140 141 142 143 144 145
	PSID_IDENTIFIER_AUTHORITY pIdentifierAuthority,
	BYTE nSubAuthorityCount,
	DWORD nSubAuthority0, DWORD nSubAuthority1,
	DWORD nSubAuthority2, DWORD nSubAuthority3,
	DWORD nSubAuthority4, DWORD nSubAuthority5,
	DWORD nSubAuthority6, DWORD nSubAuthority7,
	PSID *pSid )
Juergen Schmied's avatar
Juergen Schmied committed
146
{
147
    SID *tmp_sid;
148

149
    TRACE("(%p, 0x%04x,0x%08x,0x%08x,0x%08x,0x%08x,0x%08x,0x%08x,0x%08x,0x%08x,%p)\n",
150 151 152 153
		pIdentifierAuthority,nSubAuthorityCount,
		nSubAuthority0, nSubAuthority1,	nSubAuthority2, nSubAuthority3,
		nSubAuthority4, nSubAuthority5,	nSubAuthority6, nSubAuthority7, pSid);

154 155 156 157 158 159 160 161 162
    if (nSubAuthorityCount > 8) return STATUS_INVALID_SID;

    if (!(tmp_sid= RtlAllocateHeap( GetProcessHeap(), 0,
                                    RtlLengthRequiredSid(nSubAuthorityCount))))
        return STATUS_NO_MEMORY;

    tmp_sid->Revision = SID_REVISION;

    if (pIdentifierAuthority)
163
        tmp_sid->IdentifierAuthority = *pIdentifierAuthority;
164 165 166 167 168
    tmp_sid->SubAuthorityCount = nSubAuthorityCount;

    switch( nSubAuthorityCount )
    {
        case 8: tmp_sid->SubAuthority[7]= nSubAuthority7;
169
            /* fall through */
170
        case 7: tmp_sid->SubAuthority[6]= nSubAuthority6;
171
            /* fall through */
172
        case 6: tmp_sid->SubAuthority[5]= nSubAuthority5;
173
            /* fall through */
174
        case 5: tmp_sid->SubAuthority[4]= nSubAuthority4;
175
            /* fall through */
176
        case 4: tmp_sid->SubAuthority[3]= nSubAuthority3;
177
            /* fall through */
178
        case 3: tmp_sid->SubAuthority[2]= nSubAuthority2;
179
            /* fall through */
180
        case 2: tmp_sid->SubAuthority[1]= nSubAuthority1;
181
            /* fall through */
182 183 184 185 186
        case 1: tmp_sid->SubAuthority[0]= nSubAuthority0;
        break;
    }
    *pSid = tmp_sid;
    return STATUS_SUCCESS;
Juergen Schmied's avatar
Juergen Schmied committed
187
}
188

Juergen Schmied's avatar
Juergen Schmied committed
189
/******************************************************************************
190
 *  RtlEqualSid		[NTDLL.@]
Juergen Schmied's avatar
Juergen Schmied committed
191
 *
Jon Griffiths's avatar
Jon Griffiths committed
192 193 194 195 196 197 198 199 200
 * Determine if two SIDs are equal.
 *
 * PARAMS
 *  pSid1 [I] Source SID
 *  pSid2 [I] SID to compare with
 *
 * RETURNS
 *  TRUE, if pSid1 is equal to pSid2,
 *  FALSE otherwise.
Juergen Schmied's avatar
Juergen Schmied committed
201
 */
202 203 204 205 206 207 208 209
BOOL WINAPI RtlEqualSid( PSID pSid1, PSID pSid2 )
{
    if (!RtlValidSid(pSid1) || !RtlValidSid(pSid2))
        return FALSE;

    if (*RtlSubAuthorityCountSid(pSid1) != *RtlSubAuthorityCountSid(pSid2))
        return FALSE;

210
    if (memcmp(pSid1, pSid2, RtlLengthSid(pSid1)) != 0)
211 212 213 214 215 216
        return FALSE;

    return TRUE;
}

/******************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
217
 * RtlEqualPrefixSid	[NTDLL.@]
218
 */
219
BOOL WINAPI RtlEqualPrefixSid (PSID pSid1, PSID pSid2)
220 221 222 223 224 225 226
{
    if (!RtlValidSid(pSid1) || !RtlValidSid(pSid2))
        return FALSE;

    if (*RtlSubAuthorityCountSid(pSid1) != *RtlSubAuthorityCountSid(pSid2))
        return FALSE;

227
    if (memcmp(pSid1, pSid2, RtlLengthRequiredSid(((SID*)pSid1)->SubAuthorityCount - 1)) != 0)
228 229 230
        return FALSE;

    return TRUE;
Juergen Schmied's avatar
Juergen Schmied committed
231 232
}

233

Juergen Schmied's avatar
Juergen Schmied committed
234
/******************************************************************************
235
 *  RtlFreeSid		[NTDLL.@]
Jon Griffiths's avatar
Jon Griffiths committed
236 237 238 239 240 241 242 243
 *
 * Free the resources used by a SID.
 *
 * PARAMS
 *  pSid [I] SID to Free.
 *
 * RETURNS
 *  STATUS_SUCCESS.
Juergen Schmied's avatar
Juergen Schmied committed
244
 */
245
DWORD WINAPI RtlFreeSid(PSID pSid)
Juergen Schmied's avatar
Juergen Schmied committed
246
{
247
	TRACE("(%p)\n", pSid);
248
	RtlFreeHeap( GetProcessHeap(), 0, pSid );
249
	return STATUS_SUCCESS;
Juergen Schmied's avatar
Juergen Schmied committed
250 251 252
}

/**************************************************************************
253
 * RtlLengthRequiredSid	[NTDLL.@]
254
 *
Jon Griffiths's avatar
Jon Griffiths committed
255 256
 * Determine the amount of memory a SID will use
 *
257
 * PARAMS
Jon Griffiths's avatar
Jon Griffiths committed
258 259 260 261
 *   nrofsubauths [I] Number of Sub Authorities in the SID.
 *
 * RETURNS
 *   The size, in bytes, of a SID with nrofsubauths Sub Authorities.
Juergen Schmied's avatar
Juergen Schmied committed
262 263 264
 */
DWORD WINAPI RtlLengthRequiredSid(DWORD nrofsubauths)
{
265
	return (nrofsubauths-1)*sizeof(DWORD) + sizeof(SID);
Juergen Schmied's avatar
Juergen Schmied committed
266 267 268
}

/**************************************************************************
269
 *                 RtlLengthSid				[NTDLL.@]
Jon Griffiths's avatar
Jon Griffiths committed
270 271 272 273
 *
 * Determine the amount of memory a SID is using
 *
 * PARAMS
274
 *  pSid [I] SID to get the size of.
Jon Griffiths's avatar
Jon Griffiths committed
275 276 277
 *
 * RETURNS
 *  The size, in bytes, of pSid.
Juergen Schmied's avatar
Juergen Schmied committed
278
 */
279
DWORD WINAPI RtlLengthSid(PSID pSid)
Juergen Schmied's avatar
Juergen Schmied committed
280
{
281
	TRACE("sid=%p\n",pSid);
282
	if (!pSid) return 0;
283
	return RtlLengthRequiredSid(*RtlSubAuthorityCountSid(pSid));
Juergen Schmied's avatar
Juergen Schmied committed
284 285 286
}

/**************************************************************************
287
 *                 RtlInitializeSid			[NTDLL.@]
Jon Griffiths's avatar
Jon Griffiths committed
288 289 290 291 292 293 294 295 296
 *
 * Initialise a SID.
 *
 * PARAMS
 *  pSid                 [I] SID to initialise
 *  pIdentifierAuthority [I] Identifier Authority
 *  nSubAuthorityCount   [I] Number of Sub Authorities
 *
 * RETURNS
297
 *  Success: TRUE. pSid is initialised with the details given.
Jon Griffiths's avatar
Jon Griffiths committed
298
 *  Failure: FALSE, if nSubAuthorityCount is >= SID_MAX_SUB_AUTHORITIES.
Juergen Schmied's avatar
Juergen Schmied committed
299
 */
300 301 302 303
BOOL WINAPI RtlInitializeSid(
	PSID pSid,
	PSID_IDENTIFIER_AUTHORITY pIdentifierAuthority,
	BYTE nSubAuthorityCount)
Juergen Schmied's avatar
Juergen Schmied committed
304
{
305
	int i;
306 307
	SID* pisid=pSid;

308 309
	if (nSubAuthorityCount >= SID_MAX_SUB_AUTHORITIES)
	  return FALSE;
Juergen Schmied's avatar
Juergen Schmied committed
310

311 312
	pisid->Revision = SID_REVISION;
	pisid->SubAuthorityCount = nSubAuthorityCount;
313
	if (pIdentifierAuthority)
314
	  pisid->IdentifierAuthority = *pIdentifierAuthority;
315 316

	for (i = 0; i < nSubAuthorityCount; i++)
317
	  *RtlSubAuthoritySid(pSid, i) = 0;
318 319

	return TRUE;
Juergen Schmied's avatar
Juergen Schmied committed
320 321 322
}

/**************************************************************************
323
 *                 RtlSubAuthoritySid			[NTDLL.@]
324
 *
Jon Griffiths's avatar
Jon Griffiths committed
325 326
 * Return the Sub Authority of a SID
 *
327
 * PARAMS
Jon Griffiths's avatar
Jon Griffiths committed
328 329 330 331 332
 *   pSid          [I] SID to get the Sub Authority from.
 *   nSubAuthority [I] Sub Authority number.
 *
 * RETURNS
 *   A pointer to The Sub Authority value of pSid.
Juergen Schmied's avatar
Juergen Schmied committed
333
 */
334
LPDWORD WINAPI RtlSubAuthoritySid( PSID pSid, DWORD nSubAuthority )
Juergen Schmied's avatar
Juergen Schmied committed
335
{
336
    return &(((SID*)pSid)->SubAuthority[nSubAuthority]);
Juergen Schmied's avatar
Juergen Schmied committed
337 338 339
}

/**************************************************************************
340
 * RtlIdentifierAuthoritySid	[NTDLL.@]
341
 *
Jon Griffiths's avatar
Jon Griffiths committed
342 343
 * Return the Identifier Authority of a SID.
 *
344
 * PARAMS
Jon Griffiths's avatar
Jon Griffiths committed
345 346 347 348
 *   pSid [I] SID to get the Identifier Authority from.
 *
 * RETURNS
 *   A pointer to the Identifier Authority value of pSid.
Juergen Schmied's avatar
Juergen Schmied committed
349
 */
350 351
PSID_IDENTIFIER_AUTHORITY WINAPI RtlIdentifierAuthoritySid( PSID pSid )
{
352
    return &(((SID*)pSid)->IdentifierAuthority);
353
}
Juergen Schmied's avatar
Juergen Schmied committed
354

355
/**************************************************************************
356
 *                 RtlSubAuthorityCountSid		[NTDLL.@]
357
 *
Jon Griffiths's avatar
Jon Griffiths committed
358 359
 * Get the number of Sub Authorities in a SID.
 *
360
 * PARAMS
Jon Griffiths's avatar
Jon Griffiths committed
361 362 363 364
 *   pSid [I] SID to get the count from.
 *
 * RETURNS
 *  A pointer to the Sub Authority count of pSid.
365 366
 */
LPBYTE WINAPI RtlSubAuthorityCountSid(PSID pSid)
Juergen Schmied's avatar
Juergen Schmied committed
367
{
368
    return &(((SID*)pSid)->SubAuthorityCount);
Juergen Schmied's avatar
Juergen Schmied committed
369 370 371
}

/**************************************************************************
372
 *                 RtlCopySid				[NTDLL.@]
Juergen Schmied's avatar
Juergen Schmied committed
373
 */
374
BOOLEAN WINAPI RtlCopySid( DWORD nDestinationSidLength, PSID pDestinationSid, PSID pSourceSid )
375 376 377 378 379
{
	if (!pSourceSid || !RtlValidSid(pSourceSid) ||
	    (nDestinationSidLength < RtlLengthSid(pSourceSid)))
          return FALSE;

380
	if (nDestinationSidLength < (((SID*)pSourceSid)->SubAuthorityCount*4+8))
381 382
	  return FALSE;

383
	memmove(pDestinationSid, pSourceSid, ((SID*)pSourceSid)->SubAuthorityCount*4+8);
384 385 386
	return TRUE;
}
/******************************************************************************
387
 * RtlValidSid [NTDLL.@]
388
 *
Jon Griffiths's avatar
Jon Griffiths committed
389 390
 * Determine if a SID is valid.
 *
391
 * PARAMS
Jon Griffiths's avatar
Jon Griffiths committed
392 393 394 395 396
 *   pSid [I] SID to check
 *
 * RETURNS
 *   TRUE if pSid is valid,
 *   FALSE otherwise.
397
 */
398
BOOLEAN WINAPI RtlValidSid( PSID pSid )
399
{
400 401 402 403
    BOOL ret;
    __TRY
    {
        ret = TRUE;
404 405
        if (!pSid || ((SID*)pSid)->Revision != SID_REVISION ||
            ((SID*)pSid)->SubAuthorityCount > SID_MAX_SUB_AUTHORITIES)
406 407 408 409
        {
            ret = FALSE;
        }
    }
410
    __EXCEPT_PAGE_FAULT
411 412 413 414
    {
        WARN("(%p): invalid pointer!\n", pSid);
        return FALSE;
    }
415 416
    __ENDTRY
    return ret;
Juergen Schmied's avatar
Juergen Schmied committed
417 418
}

419

Juergen Schmied's avatar
Juergen Schmied committed
420 421 422 423 424
/*
 *	security descriptor functions
 */

/**************************************************************************
425
 * RtlCreateSecurityDescriptor			[NTDLL.@]
Juergen Schmied's avatar
Juergen Schmied committed
426
 *
Jon Griffiths's avatar
Jon Griffiths committed
427 428 429 430 431 432
 * Initialise a SECURITY_DESCRIPTOR.
 *
 * PARAMS
 *  lpsd [O] Descriptor to initialise.
 *  rev  [I] Revision, must be set to SECURITY_DESCRIPTOR_REVISION.
 *
433
 * RETURNS
Jon Griffiths's avatar
Jon Griffiths committed
434 435
 *  Success: STATUS_SUCCESS.
 *  Failure: STATUS_UNKNOWN_REVISION if rev is incorrect.
Juergen Schmied's avatar
Juergen Schmied committed
436 437 438 439 440 441 442
 */
NTSTATUS WINAPI RtlCreateSecurityDescriptor(
	PSECURITY_DESCRIPTOR lpsd,
	DWORD rev)
{
	if (rev!=SECURITY_DESCRIPTOR_REVISION)
		return STATUS_UNKNOWN_REVISION;
443
	memset(lpsd,'\0',sizeof(SECURITY_DESCRIPTOR));
444
	((SECURITY_DESCRIPTOR*)lpsd)->Revision = SECURITY_DESCRIPTOR_REVISION;
Juergen Schmied's avatar
Juergen Schmied committed
445 446
	return STATUS_SUCCESS;
}
447 448 449 450 451 452 453 454 455 456

/**************************************************************************
 * RtlCopySecurityDescriptor            [NTDLL.@]
 *
 * Copies an absolute or sefl-relative SECURITY_DESCRIPTOR.
 *
 * PARAMS
 *  pSourceSD      [O] SD to copy from.
 *  pDestinationSD [I] Destination SD.
 *
457
 * RETURNS
458 459 460 461 462 463 464 465 466
 *  Success: STATUS_SUCCESS.
 *  Failure: STATUS_UNKNOWN_REVISION if rev is incorrect.
 */
NTSTATUS WINAPI RtlCopySecurityDescriptor(PSECURITY_DESCRIPTOR pSourceSD, PSECURITY_DESCRIPTOR pDestinationSD)
{
    PSID Owner, Group;
    PACL Dacl, Sacl;
    DWORD length;

467
    if (((SECURITY_DESCRIPTOR *)pSourceSD)->Control & SE_SELF_RELATIVE)
468
    {
469 470
        SECURITY_DESCRIPTOR_RELATIVE *src = pSourceSD;
        SECURITY_DESCRIPTOR_RELATIVE *dst = pDestinationSD;
471

472 473
        if (src->Revision != SECURITY_DESCRIPTOR_REVISION)
            return STATUS_UNKNOWN_REVISION;
474

475 476 477 478 479 480 481 482 483 484 485 486 487
        *dst = *src;
        if (src->Owner)
        {
            Owner = (PSID)SELF_RELATIVE_FIELD( src, Owner );
            length = RtlLengthSid( Owner );
            RtlCopySid(length, SELF_RELATIVE_FIELD( dst, Owner ), Owner);
        }
        if (src->Group)
        {
            Group = (PSID)SELF_RELATIVE_FIELD( src, Group );
            length = RtlLengthSid( Group );
            RtlCopySid(length, SELF_RELATIVE_FIELD( dst, Group ), Group);
        }
488
        if ((src->Control & SE_SACL_PRESENT) && src->Sacl)
489 490 491 492
        {
            Sacl = (PACL)SELF_RELATIVE_FIELD( src, Sacl );
            copy_acl(Sacl->AclSize, (PACL)SELF_RELATIVE_FIELD( dst, Sacl ), Sacl);
        }
493
        if ((src->Control & SE_DACL_PRESENT) && src->Dacl)
494 495 496 497
        {
            Dacl = (PACL)SELF_RELATIVE_FIELD( src, Dacl );
            copy_acl(Dacl->AclSize, (PACL)SELF_RELATIVE_FIELD( dst, Dacl ), Dacl);
        }
498 499 500
    }
    else
    {
501 502
        SECURITY_DESCRIPTOR *src = pSourceSD;
        SECURITY_DESCRIPTOR *dst = pDestinationSD;
503

504 505
        if (src->Revision != SECURITY_DESCRIPTOR_REVISION)
            return STATUS_UNKNOWN_REVISION;
506

507 508
        *dst = *src;
        if (src->Owner)
509
        {
510 511 512
            length = RtlLengthSid( src->Owner );
            dst->Owner = RtlAllocateHeap(GetProcessHeap(), 0, length);
            RtlCopySid(length, dst->Owner, src->Owner);
513
        }
514
        if (src->Group)
515
        {
516 517 518
            length = RtlLengthSid( src->Group );
            dst->Group = RtlAllocateHeap(GetProcessHeap(), 0, length);
            RtlCopySid(length, dst->Group, src->Group);
519
        }
520
        if (src->Control & SE_SACL_PRESENT)
521
        {
522 523 524
            length = src->Sacl->AclSize;
            dst->Sacl = RtlAllocateHeap(GetProcessHeap(), 0, length);
            copy_acl(length, dst->Sacl, src->Sacl);
525
        }
526
        if (src->Control & SE_DACL_PRESENT)
527
        {
528 529 530
            length = src->Dacl->AclSize;
            dst->Dacl = RtlAllocateHeap(GetProcessHeap(), 0, length);
            copy_acl(length, dst->Dacl, src->Dacl);
531 532 533 534 535 536
        }
    }

    return STATUS_SUCCESS;
}

Juergen Schmied's avatar
Juergen Schmied committed
537
/**************************************************************************
538
 * RtlValidSecurityDescriptor			[NTDLL.@]
Juergen Schmied's avatar
Juergen Schmied committed
539
 *
Jon Griffiths's avatar
Jon Griffiths committed
540 541 542 543 544 545 546 547
 * Determine if a SECURITY_DESCRIPTOR is valid.
 *
 * PARAMS
 *  SecurityDescriptor [I] Descriptor to check.
 *
 * RETURNS
 *   Success: STATUS_SUCCESS.
 *   Failure: STATUS_INVALID_SECURITY_DESCR or STATUS_UNKNOWN_REVISION.
Juergen Schmied's avatar
Juergen Schmied committed
548 549 550 551 552 553
 */
NTSTATUS WINAPI RtlValidSecurityDescriptor(
	PSECURITY_DESCRIPTOR SecurityDescriptor)
{
	if ( ! SecurityDescriptor )
		return STATUS_INVALID_SECURITY_DESCR;
554
	if ( ((SECURITY_DESCRIPTOR*)SecurityDescriptor)->Revision != SECURITY_DESCRIPTOR_REVISION )
Juergen Schmied's avatar
Juergen Schmied committed
555 556 557 558 559 560
		return STATUS_UNKNOWN_REVISION;

	return STATUS_SUCCESS;
}

/**************************************************************************
561
 *  RtlLengthSecurityDescriptor			[NTDLL.@]
Juergen Schmied's avatar
Juergen Schmied committed
562 563
 */
ULONG WINAPI RtlLengthSecurityDescriptor(
564
	PSECURITY_DESCRIPTOR pSecurityDescriptor)
Juergen Schmied's avatar
Juergen Schmied committed
565
{
566
	ULONG size;
567

568
	if ( pSecurityDescriptor == NULL )
Juergen Schmied's avatar
Juergen Schmied committed
569 570
		return 0;

571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
	if (((SECURITY_DESCRIPTOR *)pSecurityDescriptor)->Control & SE_SELF_RELATIVE)
        {
            SECURITY_DESCRIPTOR_RELATIVE *sd = pSecurityDescriptor;
            size = sizeof(*sd);
            if (sd->Owner) size += RtlLengthSid((PSID)SELF_RELATIVE_FIELD(sd,Owner));
            if (sd->Group) size += RtlLengthSid((PSID)SELF_RELATIVE_FIELD(sd,Group));
            if ((sd->Control & SE_SACL_PRESENT) && sd->Sacl)
		size += ((PACL)SELF_RELATIVE_FIELD(sd,Sacl))->AclSize;
            if ((sd->Control & SE_DACL_PRESENT) && sd->Dacl)
		size += ((PACL)SELF_RELATIVE_FIELD(sd,Dacl))->AclSize;
        }
        else
        {
            SECURITY_DESCRIPTOR *sd = pSecurityDescriptor;
            size = sizeof(*sd);
            if (sd->Owner) size += RtlLengthSid( sd->Owner );
            if (sd->Group) size += RtlLengthSid( sd->Group );
            if ((sd->Control & SE_SACL_PRESENT) && sd->Sacl) size += sd->Sacl->AclSize;
            if ((sd->Control & SE_DACL_PRESENT) && sd->Dacl) size += sd->Dacl->AclSize;
        }
	return size;
Juergen Schmied's avatar
Juergen Schmied committed
592 593 594
}

/******************************************************************************
595
 *  RtlGetDaclSecurityDescriptor		[NTDLL.@]
Juergen Schmied's avatar
Juergen Schmied committed
596 597 598 599 600 601 602 603
 *
 */
NTSTATUS WINAPI RtlGetDaclSecurityDescriptor(
	IN PSECURITY_DESCRIPTOR pSecurityDescriptor,
	OUT PBOOLEAN lpbDaclPresent,
	OUT PACL *pDacl,
	OUT PBOOLEAN lpbDaclDefaulted)
{
604 605
	SECURITY_DESCRIPTOR* lpsd=pSecurityDescriptor;

606
	TRACE("(%p,%p,%p,%p)\n",
607
	pSecurityDescriptor, lpbDaclPresent, pDacl, lpbDaclDefaulted);
Juergen Schmied's avatar
Juergen Schmied committed
608

609
	if (lpsd->Revision != SECURITY_DESCRIPTOR_REVISION)
Juergen Schmied's avatar
Juergen Schmied committed
610 611
	  return STATUS_UNKNOWN_REVISION ;

612
	if ( (*lpbDaclPresent = (SE_DACL_PRESENT & lpsd->Control) ? 1 : 0) )
Juergen Schmied's avatar
Juergen Schmied committed
613
	{
614 615 616 617 618 619 620 621 622
            if (lpsd->Control & SE_SELF_RELATIVE)
            {
                SECURITY_DESCRIPTOR_RELATIVE *sdr = pSecurityDescriptor;
                if (sdr->Dacl) *pDacl = (PACL)SELF_RELATIVE_FIELD( sdr, Dacl );
                else *pDacl = NULL;
            }
            else *pDacl = lpsd->Dacl;

            *lpbDaclDefaulted = (lpsd->Control & SE_DACL_DEFAULTED) != 0;
623 624 625 626 627 628
        }
        else
        {
            *pDacl = NULL;
            *lpbDaclDefaulted = 0;
        }
629

Juergen Schmied's avatar
Juergen Schmied committed
630 631 632 633
	return STATUS_SUCCESS;
}

/**************************************************************************
634
 *  RtlSetDaclSecurityDescriptor		[NTDLL.@]
Juergen Schmied's avatar
Juergen Schmied committed
635 636
 */
NTSTATUS WINAPI RtlSetDaclSecurityDescriptor (
637
	PSECURITY_DESCRIPTOR pSecurityDescriptor,
Juergen Schmied's avatar
Juergen Schmied committed
638 639 640 641
	BOOLEAN daclpresent,
	PACL dacl,
	BOOLEAN dacldefaulted )
{
642 643
	SECURITY_DESCRIPTOR* lpsd=pSecurityDescriptor;

Juergen Schmied's avatar
Juergen Schmied committed
644 645 646 647 648
	if (lpsd->Revision!=SECURITY_DESCRIPTOR_REVISION)
		return STATUS_UNKNOWN_REVISION;
	if (lpsd->Control & SE_SELF_RELATIVE)
		return STATUS_INVALID_SECURITY_DESCR;

649
	if (!daclpresent)
650 651 652
	{
		lpsd->Control &= ~SE_DACL_PRESENT;
		return STATUS_SUCCESS;
Juergen Schmied's avatar
Juergen Schmied committed
653 654 655 656 657 658 659 660 661 662 663 664 665 666
	}

	lpsd->Control |= SE_DACL_PRESENT;
	lpsd->Dacl = dacl;

	if (dacldefaulted)
		lpsd->Control |= SE_DACL_DEFAULTED;
	else
		lpsd->Control &= ~SE_DACL_DEFAULTED;

	return STATUS_SUCCESS;
}

/******************************************************************************
667
 *  RtlGetSaclSecurityDescriptor		[NTDLL.@]
Juergen Schmied's avatar
Juergen Schmied committed
668 669 670 671 672 673 674 675
 *
 */
NTSTATUS WINAPI RtlGetSaclSecurityDescriptor(
	IN PSECURITY_DESCRIPTOR pSecurityDescriptor,
	OUT PBOOLEAN lpbSaclPresent,
	OUT PACL *pSacl,
	OUT PBOOLEAN lpbSaclDefaulted)
{
676 677
	SECURITY_DESCRIPTOR* lpsd=pSecurityDescriptor;

678
	TRACE("(%p,%p,%p,%p)\n",
679
	pSecurityDescriptor, lpbSaclPresent, pSacl, lpbSaclDefaulted);
Juergen Schmied's avatar
Juergen Schmied committed
680

681
	if (lpsd->Revision != SECURITY_DESCRIPTOR_REVISION)
682
	  return STATUS_UNKNOWN_REVISION;
Juergen Schmied's avatar
Juergen Schmied committed
683

684
	if ( (*lpbSaclPresent = (SE_SACL_PRESENT & lpsd->Control) ? 1 : 0) )
Juergen Schmied's avatar
Juergen Schmied committed
685
	{
686 687 688 689 690 691 692 693 694
            if (lpsd->Control & SE_SELF_RELATIVE)
            {
                SECURITY_DESCRIPTOR_RELATIVE *sdr = pSecurityDescriptor;
                if (sdr->Sacl) *pSacl = (PACL)SELF_RELATIVE_FIELD( sdr, Sacl );
                else *pSacl = NULL;
            }
            else *pSacl = lpsd->Sacl;

            *lpbSaclDefaulted = (lpsd->Control & SE_SACL_DEFAULTED) != 0;
695
	}
Juergen Schmied's avatar
Juergen Schmied committed
696 697 698 699
	return STATUS_SUCCESS;
}

/**************************************************************************
700
 * RtlSetSaclSecurityDescriptor			[NTDLL.@]
Juergen Schmied's avatar
Juergen Schmied committed
701 702
 */
NTSTATUS WINAPI RtlSetSaclSecurityDescriptor (
703
	PSECURITY_DESCRIPTOR pSecurityDescriptor,
Juergen Schmied's avatar
Juergen Schmied committed
704 705 706 707
	BOOLEAN saclpresent,
	PACL sacl,
	BOOLEAN sacldefaulted)
{
708 709
	SECURITY_DESCRIPTOR* lpsd=pSecurityDescriptor;

Juergen Schmied's avatar
Juergen Schmied committed
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
	if (lpsd->Revision!=SECURITY_DESCRIPTOR_REVISION)
		return STATUS_UNKNOWN_REVISION;
	if (lpsd->Control & SE_SELF_RELATIVE)
		return STATUS_INVALID_SECURITY_DESCR;
	if (!saclpresent) {
		lpsd->Control &= ~SE_SACL_PRESENT;
		return 0;
	}
	lpsd->Control |= SE_SACL_PRESENT;
	lpsd->Sacl = sacl;
	if (sacldefaulted)
		lpsd->Control |= SE_SACL_DEFAULTED;
	else
		lpsd->Control &= ~SE_SACL_DEFAULTED;
	return STATUS_SUCCESS;
}

/**************************************************************************
728
 * RtlGetOwnerSecurityDescriptor		[NTDLL.@]
Juergen Schmied's avatar
Juergen Schmied committed
729 730
 */
NTSTATUS WINAPI RtlGetOwnerSecurityDescriptor(
731
	PSECURITY_DESCRIPTOR pSecurityDescriptor,
Juergen Schmied's avatar
Juergen Schmied committed
732 733 734
	PSID *Owner,
	PBOOLEAN OwnerDefaulted)
{
735 736 737
	SECURITY_DESCRIPTOR* lpsd=pSecurityDescriptor;

	if ( !lpsd  || !Owner || !OwnerDefaulted )
Juergen Schmied's avatar
Juergen Schmied committed
738 739
		return STATUS_INVALID_PARAMETER;

740 741 742 743 744
        if ( lpsd->Control & SE_OWNER_DEFAULTED )
            *OwnerDefaulted = TRUE;
        else
            *OwnerDefaulted = FALSE;

745 746 747 748 749
        if (lpsd->Control & SE_SELF_RELATIVE)
        {
            SECURITY_DESCRIPTOR_RELATIVE *sd = pSecurityDescriptor;
            if (sd->Owner) *Owner = (PSID)SELF_RELATIVE_FIELD( sd, Owner );
            else *Owner = NULL;
750
        }
751 752
        else
            *Owner = lpsd->Owner;
753

Juergen Schmied's avatar
Juergen Schmied committed
754 755 756 757
	return STATUS_SUCCESS;
}

/**************************************************************************
758
 *                 RtlSetOwnerSecurityDescriptor		[NTDLL.@]
Juergen Schmied's avatar
Juergen Schmied committed
759 760
 */
NTSTATUS WINAPI RtlSetOwnerSecurityDescriptor(
761
	PSECURITY_DESCRIPTOR pSecurityDescriptor,
Juergen Schmied's avatar
Juergen Schmied committed
762 763 764
	PSID owner,
	BOOLEAN ownerdefaulted)
{
765 766
	SECURITY_DESCRIPTOR* lpsd=pSecurityDescriptor;

Juergen Schmied's avatar
Juergen Schmied committed
767 768 769 770 771 772 773 774 775 776 777 778 779 780
	if (lpsd->Revision!=SECURITY_DESCRIPTOR_REVISION)
		return STATUS_UNKNOWN_REVISION;
	if (lpsd->Control & SE_SELF_RELATIVE)
		return STATUS_INVALID_SECURITY_DESCR;

	lpsd->Owner = owner;
	if (ownerdefaulted)
		lpsd->Control |= SE_OWNER_DEFAULTED;
	else
		lpsd->Control &= ~SE_OWNER_DEFAULTED;
	return STATUS_SUCCESS;
}

/**************************************************************************
781
 *                 RtlSetGroupSecurityDescriptor		[NTDLL.@]
Juergen Schmied's avatar
Juergen Schmied committed
782 783
 */
NTSTATUS WINAPI RtlSetGroupSecurityDescriptor (
784
	PSECURITY_DESCRIPTOR pSecurityDescriptor,
Juergen Schmied's avatar
Juergen Schmied committed
785 786 787
	PSID group,
	BOOLEAN groupdefaulted)
{
788 789
	SECURITY_DESCRIPTOR* lpsd=pSecurityDescriptor;

Juergen Schmied's avatar
Juergen Schmied committed
790 791 792 793 794 795 796 797 798 799 800 801
	if (lpsd->Revision!=SECURITY_DESCRIPTOR_REVISION)
		return STATUS_UNKNOWN_REVISION;
	if (lpsd->Control & SE_SELF_RELATIVE)
		return STATUS_INVALID_SECURITY_DESCR;

	lpsd->Group = group;
	if (groupdefaulted)
		lpsd->Control |= SE_GROUP_DEFAULTED;
	else
		lpsd->Control &= ~SE_GROUP_DEFAULTED;
	return STATUS_SUCCESS;
}
802

Juergen Schmied's avatar
Juergen Schmied committed
803
/**************************************************************************
804
 *                 RtlGetGroupSecurityDescriptor		[NTDLL.@]
Juergen Schmied's avatar
Juergen Schmied committed
805 806
 */
NTSTATUS WINAPI RtlGetGroupSecurityDescriptor(
807
	PSECURITY_DESCRIPTOR pSecurityDescriptor,
Juergen Schmied's avatar
Juergen Schmied committed
808 809 810
	PSID *Group,
	PBOOLEAN GroupDefaulted)
{
811 812 813
	SECURITY_DESCRIPTOR* lpsd=pSecurityDescriptor;

	if ( !lpsd || !Group || !GroupDefaulted )
Juergen Schmied's avatar
Juergen Schmied committed
814 815
		return STATUS_INVALID_PARAMETER;

816 817 818 819 820
        if ( lpsd->Control & SE_GROUP_DEFAULTED )
            *GroupDefaulted = TRUE;
        else
            *GroupDefaulted = FALSE;

821 822 823 824 825 826 827 828
        if (lpsd->Control & SE_SELF_RELATIVE)
        {
            SECURITY_DESCRIPTOR_RELATIVE *sd = pSecurityDescriptor;
            if (sd->Group) *Group = (PSID)SELF_RELATIVE_FIELD( sd, Group );
            else *Group = NULL;
        }
        else
            *Group = lpsd->Group;
829

Juergen Schmied's avatar
Juergen Schmied committed
830
	return STATUS_SUCCESS;
831
}
Juergen Schmied's avatar
Juergen Schmied committed
832

833
/**************************************************************************
834
 *                 RtlMakeSelfRelativeSD		[NTDLL.@]
835 836 837 838 839 840
 */
NTSTATUS WINAPI RtlMakeSelfRelativeSD(
	IN PSECURITY_DESCRIPTOR pAbsoluteSecurityDescriptor,
	IN PSECURITY_DESCRIPTOR pSelfRelativeSecurityDescriptor,
	IN OUT LPDWORD lpdwBufferLength)
{
841
    DWORD offsetRel;
842
    ULONG length;
843
    SECURITY_DESCRIPTOR* pAbs = pAbsoluteSecurityDescriptor;
844
    SECURITY_DESCRIPTOR_RELATIVE *pRel = pSelfRelativeSecurityDescriptor;
845

846
    TRACE(" %p %p %p(%d)\n", pAbs, pRel, lpdwBufferLength,
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
        lpdwBufferLength ? *lpdwBufferLength: -1);

    if (!lpdwBufferLength || !pAbs)
        return STATUS_INVALID_PARAMETER;

    length = RtlLengthSecurityDescriptor(pAbs);
    if (*lpdwBufferLength < length)
    {
        *lpdwBufferLength = length;
        return STATUS_BUFFER_TOO_SMALL;
    }

    if (!pRel)
        return STATUS_INVALID_PARAMETER;

    if (pAbs->Control & SE_SELF_RELATIVE)
    {
        memcpy(pRel, pAbs, length);
        return STATUS_SUCCESS;
    }

    pRel->Revision = pAbs->Revision;
    pRel->Sbz1 = pAbs->Sbz1;
    pRel->Control = pAbs->Control | SE_SELF_RELATIVE;

872
    offsetRel = sizeof(SECURITY_DESCRIPTOR_RELATIVE);
873 874
    if (pAbs->Owner)
    {
875
        pRel->Owner = offsetRel;
876 877 878 879 880 881
        length = RtlLengthSid(pAbs->Owner);
        memcpy((LPBYTE)pRel + offsetRel, pAbs->Owner, length);
        offsetRel += length;
    }
    else
    {
882
        pRel->Owner = 0;
883
    }
884

885
    if (pAbs->Group)
886
    {
887
        pRel->Group = offsetRel;
888 889
        length = RtlLengthSid(pAbs->Group);
        memcpy((LPBYTE)pRel + offsetRel, pAbs->Group, length);
890
        offsetRel += length;
891 892 893
    }
    else
    {
894
        pRel->Group = 0;
895 896 897 898
    }

    if (pAbs->Sacl)
    {
899
        pRel->Sacl = offsetRel;
900 901
        length = pAbs->Sacl->AclSize;
        memcpy((LPBYTE)pRel + offsetRel, pAbs->Sacl, length);
902
        offsetRel += length;
903 904 905
    }
    else
    {
906
        pRel->Sacl = 0;
907 908
    }

909
    if (pAbs->Dacl)
910
    {
911
        pRel->Dacl = offsetRel;
912 913 914 915 916
        length = pAbs->Dacl->AclSize;
        memcpy((LPBYTE)pRel + offsetRel, pAbs->Dacl, length);
    }
    else
    {
917
        pRel->Dacl = 0;
918 919 920 921 922 923 924
    }

    return STATUS_SUCCESS;
}


/**************************************************************************
925 926
 *                 RtlSelfRelativeToAbsoluteSD [NTDLL.@]
 */
927 928 929 930 931 932 933 934 935 936 937 938 939 940
NTSTATUS WINAPI RtlSelfRelativeToAbsoluteSD(
        IN PSECURITY_DESCRIPTOR pSelfRelativeSecurityDescriptor,
	OUT PSECURITY_DESCRIPTOR pAbsoluteSecurityDescriptor,
	OUT LPDWORD lpdwAbsoluteSecurityDescriptorSize,
	OUT PACL pDacl,
	OUT LPDWORD lpdwDaclSize,
	OUT PACL pSacl,
	OUT LPDWORD lpdwSaclSize,
	OUT PSID pOwner,
	OUT LPDWORD lpdwOwnerSize,
	OUT PSID pPrimaryGroup,
	OUT LPDWORD lpdwPrimaryGroupSize)
{
    NTSTATUS status = STATUS_SUCCESS;
941
    SECURITY_DESCRIPTOR* pAbs = pAbsoluteSecurityDescriptor;
942
    SECURITY_DESCRIPTOR_RELATIVE* pRel = pSelfRelativeSecurityDescriptor;
943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959

    if (!pRel ||
        !lpdwAbsoluteSecurityDescriptorSize ||
        !lpdwDaclSize ||
        !lpdwSaclSize ||
        !lpdwOwnerSize ||
        !lpdwPrimaryGroupSize ||
        ~pRel->Control & SE_SELF_RELATIVE)
        return STATUS_INVALID_PARAMETER;

    /* Confirm buffers are sufficiently large */
    if (*lpdwAbsoluteSecurityDescriptorSize < sizeof(SECURITY_DESCRIPTOR))
    {
        *lpdwAbsoluteSecurityDescriptorSize = sizeof(SECURITY_DESCRIPTOR);
        status = STATUS_BUFFER_TOO_SMALL;
    }

960
    if ((pRel->Control & SE_DACL_PRESENT) && pRel->Dacl &&
961
        *lpdwDaclSize  < ((PACL)SELF_RELATIVE_FIELD(pRel,Dacl))->AclSize)
962
    {
963
        *lpdwDaclSize = ((PACL)SELF_RELATIVE_FIELD(pRel,Dacl))->AclSize;
964 965 966
        status = STATUS_BUFFER_TOO_SMALL;
    }

967
    if ((pRel->Control & SE_SACL_PRESENT) && pRel->Sacl &&
968
        *lpdwSaclSize  < ((PACL)SELF_RELATIVE_FIELD(pRel,Sacl))->AclSize)
969
    {
970
        *lpdwSaclSize = ((PACL)SELF_RELATIVE_FIELD(pRel,Sacl))->AclSize;
971 972 973 974
        status = STATUS_BUFFER_TOO_SMALL;
    }

    if (pRel->Owner &&
975
        *lpdwOwnerSize < RtlLengthSid((PSID)SELF_RELATIVE_FIELD(pRel,Owner)))
976
    {
977
        *lpdwOwnerSize = RtlLengthSid((PSID)SELF_RELATIVE_FIELD(pRel,Owner));
978 979 980 981
        status = STATUS_BUFFER_TOO_SMALL;
    }

    if (pRel->Group &&
982
        *lpdwPrimaryGroupSize < RtlLengthSid((PSID)SELF_RELATIVE_FIELD(pRel,Group)))
983
    {
984
        *lpdwPrimaryGroupSize = RtlLengthSid((PSID)SELF_RELATIVE_FIELD(pRel,Group));
985 986 987 988 989 990
        status = STATUS_BUFFER_TOO_SMALL;
    }

    if (status != STATUS_SUCCESS)
        return status;

991
    /* Copy structures, and clear the ones we don't set */
992 993
    pAbs->Revision = pRel->Revision;
    pAbs->Control = pRel->Control & ~SE_SELF_RELATIVE;
994 995 996 997
    pAbs->Sacl = NULL;
    pAbs->Dacl = NULL;
    pAbs->Owner = NULL;
    pAbs->Group = NULL;
998

999
    if ((pRel->Control & SE_SACL_PRESENT) && pRel->Sacl)
1000
    {
1001
        PACL pAcl = (PACL)SELF_RELATIVE_FIELD( pRel, Sacl );
1002 1003 1004 1005 1006

        memcpy(pSacl, pAcl, pAcl->AclSize);
        pAbs->Sacl = pSacl;
    }

1007
    if ((pRel->Control & SE_DACL_PRESENT) && pRel->Dacl)
1008
    {
1009
        PACL pAcl = (PACL)SELF_RELATIVE_FIELD( pRel, Dacl );
1010 1011 1012 1013 1014 1015
        memcpy(pDacl, pAcl, pAcl->AclSize);
        pAbs->Dacl = pDacl;
    }

    if (pRel->Owner)
    {
1016
        PSID psid = (PSID)SELF_RELATIVE_FIELD( pRel, Owner );
1017 1018 1019 1020 1021 1022
        memcpy(pOwner, psid, RtlLengthSid(psid));
        pAbs->Owner = pOwner;
    }

    if (pRel->Group)
    {
1023
        PSID psid = (PSID)SELF_RELATIVE_FIELD( pRel, Group );
1024 1025 1026 1027 1028
        memcpy(pPrimaryGroup, psid, RtlLengthSid(psid));
        pAbs->Group = pPrimaryGroup;
    }

    return status;
1029 1030
}

1031 1032 1033 1034
/******************************************************************************
 * RtlGetControlSecurityDescriptor (NTDLL.@)
 */
NTSTATUS WINAPI RtlGetControlSecurityDescriptor(
James Hawkins's avatar
James Hawkins committed
1035
    PSECURITY_DESCRIPTOR pSecurityDescriptor,
1036 1037 1038
    PSECURITY_DESCRIPTOR_CONTROL pControl,
    LPDWORD lpdwRevision)
{
James Hawkins's avatar
James Hawkins committed
1039
    SECURITY_DESCRIPTOR *lpsd = pSecurityDescriptor;
1040

James Hawkins's avatar
James Hawkins committed
1041
    TRACE("(%p,%p,%p)\n",pSecurityDescriptor,pControl,lpdwRevision);
1042 1043

    *lpdwRevision = lpsd->Revision;
James Hawkins's avatar
James Hawkins committed
1044 1045 1046 1047

    if (*lpdwRevision != SECURITY_DESCRIPTOR_REVISION)
        return STATUS_UNKNOWN_REVISION;

1048 1049 1050 1051 1052
    *pControl = lpsd->Control;

    return STATUS_SUCCESS;
}

1053 1054 1055 1056 1057 1058 1059 1060
/******************************************************************************
 * RtlSetControlSecurityDescriptor (NTDLL.@)
 */
NTSTATUS WINAPI RtlSetControlSecurityDescriptor(
    PSECURITY_DESCRIPTOR SecurityDescriptor,
    SECURITY_DESCRIPTOR_CONTROL ControlBitsOfInterest,
    SECURITY_DESCRIPTOR_CONTROL ControlBitsToSet)
{
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
    SECURITY_DESCRIPTOR_CONTROL const immutable
       = SE_OWNER_DEFAULTED  | SE_GROUP_DEFAULTED
       | SE_DACL_PRESENT     | SE_DACL_DEFAULTED
       | SE_SACL_PRESENT     | SE_SACL_DEFAULTED
       | SE_RM_CONTROL_VALID | SE_SELF_RELATIVE
       ;

    SECURITY_DESCRIPTOR *lpsd = SecurityDescriptor;

    TRACE("(%p 0x%04x 0x%04x)\n", SecurityDescriptor,
          ControlBitsOfInterest, ControlBitsToSet);

    if ((ControlBitsOfInterest | ControlBitsToSet) & immutable)
        return STATUS_INVALID_PARAMETER;

    lpsd->Control |=  (ControlBitsOfInterest &  ControlBitsToSet);
    lpsd->Control &= ~(ControlBitsOfInterest & ~ControlBitsToSet);

1079 1080 1081
    return STATUS_SUCCESS;
}

1082 1083 1084 1085 1086 1087 1088 1089 1090

/**************************************************************************
 *                 RtlAbsoluteToSelfRelativeSD [NTDLL.@]
 */
NTSTATUS WINAPI RtlAbsoluteToSelfRelativeSD(
    PSECURITY_DESCRIPTOR AbsoluteSecurityDescriptor,
    PSECURITY_DESCRIPTOR SelfRelativeSecurityDescriptor,
    PULONG BufferLength)
{
1091 1092 1093
    SECURITY_DESCRIPTOR *abs = AbsoluteSecurityDescriptor;

    TRACE("%p %p %p\n", AbsoluteSecurityDescriptor,
1094
          SelfRelativeSecurityDescriptor, BufferLength);
1095 1096 1097 1098 1099 1100

    if (abs->Control & SE_SELF_RELATIVE)
        return STATUS_BAD_DESCRIPTOR_FORMAT;

    return RtlMakeSelfRelativeSD(AbsoluteSecurityDescriptor, 
        SelfRelativeSecurityDescriptor, BufferLength);
1101 1102 1103
}


Juergen Schmied's avatar
Juergen Schmied committed
1104 1105 1106 1107 1108
/*
 *	access control list's
 */

/**************************************************************************
1109
 *                 RtlCreateAcl				[NTDLL.@]
Juergen Schmied's avatar
Juergen Schmied committed
1110 1111 1112 1113
 *
 * NOTES
 *    This should return NTSTATUS
 */
1114
NTSTATUS WINAPI RtlCreateAcl(PACL acl,DWORD size,DWORD rev)
Juergen Schmied's avatar
Juergen Schmied committed
1115
{
1116
	TRACE("%p 0x%08x 0x%08x\n", acl, size, rev);
1117

1118
	if (rev < MIN_ACL_REVISION || rev > MAX_ACL_REVISION)
Juergen Schmied's avatar
Juergen Schmied committed
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
		return STATUS_INVALID_PARAMETER;
	if (size<sizeof(ACL))
		return STATUS_BUFFER_TOO_SMALL;
	if (size>0xFFFF)
		return STATUS_INVALID_PARAMETER;

	memset(acl,'\0',sizeof(ACL));
	acl->AclRevision	= rev;
	acl->AclSize		= size;
	acl->AceCount		= 0;
Jon Griffiths's avatar
Jon Griffiths committed
1129
	return STATUS_SUCCESS;
Juergen Schmied's avatar
Juergen Schmied committed
1130 1131 1132
}

/**************************************************************************
1133
 *                 RtlFirstFreeAce			[NTDLL.@]
Juergen Schmied's avatar
Juergen Schmied committed
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
 * looks for the AceCount+1 ACE, and if it is still within the alloced
 * ACL, return a pointer to it
 */
BOOLEAN WINAPI RtlFirstFreeAce(
	PACL acl,
	PACE_HEADER *x)
{
	PACE_HEADER	ace;
	int		i;

	*x = 0;
	ace = (PACE_HEADER)(acl+1);
	for (i=0;i<acl->AceCount;i++) {
1147
		if ((BYTE *)ace >= (BYTE *)acl + acl->AclSize)
Juergen Schmied's avatar
Juergen Schmied committed
1148 1149 1150
			return 0;
		ace = (PACE_HEADER)(((BYTE*)ace)+ace->AceSize);
	}
1151
	if ((BYTE *)ace >= (BYTE *)acl + acl->AclSize)
Juergen Schmied's avatar
Juergen Schmied committed
1152 1153 1154 1155 1156 1157
		return 0;
	*x = ace;
	return 1;
}

/**************************************************************************
1158
 *                 RtlAddAce				[NTDLL.@]
Juergen Schmied's avatar
Juergen Schmied committed
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
 */
NTSTATUS WINAPI RtlAddAce(
	PACL acl,
	DWORD rev,
	DWORD xnrofaces,
	PACE_HEADER acestart,
	DWORD acelen)
{
	PACE_HEADER	ace,targetace;
	int		nrofaces;

	if (acl->AclRevision != ACL_REVISION)
		return STATUS_INVALID_PARAMETER;
	if (!RtlFirstFreeAce(acl,&targetace))
		return STATUS_INVALID_PARAMETER;
	nrofaces=0;ace=acestart;
1175
	while (((BYTE *)ace - (BYTE *)acestart) < acelen) {
Juergen Schmied's avatar
Juergen Schmied committed
1176 1177 1178
		nrofaces++;
		ace = (PACE_HEADER)(((BYTE*)ace)+ace->AceSize);
	}
1179
	if ((BYTE *)targetace + acelen > (BYTE *)acl + acl->AclSize) /* too much aces */
Juergen Schmied's avatar
Juergen Schmied committed
1180
		return STATUS_INVALID_PARAMETER;
1181
	memcpy(targetace,acestart,acelen);
Juergen Schmied's avatar
Juergen Schmied committed
1182 1183 1184 1185
	acl->AceCount+=nrofaces;
	return STATUS_SUCCESS;
}

1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
/**************************************************************************
 *                 RtlDeleteAce				[NTDLL.@]
 */
NTSTATUS  WINAPI RtlDeleteAce(PACL pAcl, DWORD dwAceIndex)
{
	NTSTATUS status;
	PACE_HEADER pAce;

	status = RtlGetAce(pAcl,dwAceIndex,(LPVOID*)&pAce);

	if (STATUS_SUCCESS == status)
	{
		PACE_HEADER pcAce;
		DWORD len = 0;

1201
		/* skip over the ACE we are deleting */
1202
		pcAce = (PACE_HEADER)(((BYTE*)pAce)+pAce->AceSize);
1203 1204 1205
		dwAceIndex++;

		/* calculate the length of the rest */
1206 1207 1208 1209 1210 1211
		for (; dwAceIndex < pAcl->AceCount; dwAceIndex++)
		{
			len += pcAce->AceSize;
			pcAce = (PACE_HEADER)(((BYTE*)pcAce) + pcAce->AceSize);
		}

1212 1213 1214
		/* slide them all backwards */
		memmove(pAce, ((BYTE*)pAce)+pAce->AceSize, len);
		pAcl->AceCount--;
1215 1216
	}

1217
	TRACE("pAcl=%p dwAceIndex=%d status=0x%08x\n", pAcl, dwAceIndex, status);
1218

1219 1220 1221
	return status;
}

Juergen Schmied's avatar
Juergen Schmied committed
1222
/******************************************************************************
1223
 *  RtlAddAccessAllowedAce		[NTDLL.@]
Juergen Schmied's avatar
Juergen Schmied committed
1224
 */
1225
NTSTATUS WINAPI RtlAddAccessAllowedAce(
1226 1227 1228 1229
	IN OUT PACL pAcl,
	IN DWORD dwAceRevision,
	IN DWORD AccessMask,
	IN PSID pSid)
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
{
	return RtlAddAccessAllowedAceEx( pAcl, dwAceRevision, 0, AccessMask, pSid);
}
 
/******************************************************************************
 *  RtlAddAccessAllowedAceEx		[NTDLL.@]
 */
NTSTATUS WINAPI RtlAddAccessAllowedAceEx(
	IN OUT PACL pAcl,
	IN DWORD dwAceRevision,
	IN DWORD AceFlags,
	IN DWORD AccessMask,
	IN PSID pSid)
Juergen Schmied's avatar
Juergen Schmied committed
1243
{
1244
   TRACE("(%p,0x%08x,0x%08x,%p)\n", pAcl, dwAceRevision, AccessMask, pSid);
1245 1246 1247

    return add_access_ace(pAcl, dwAceRevision, AceFlags,
                          AccessMask, pSid, ACCESS_ALLOWED_ACE_TYPE);
1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
}

/******************************************************************************
 *  RtlAddAccessDeniedAce		[NTDLL.@]
 */
NTSTATUS WINAPI RtlAddAccessDeniedAce(
	IN OUT PACL pAcl,
	IN DWORD dwAceRevision,
	IN DWORD AccessMask,
	IN PSID pSid)
1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
{
	return RtlAddAccessDeniedAceEx( pAcl, dwAceRevision, 0, AccessMask, pSid);
}

/******************************************************************************
 *  RtlAddAccessDeniedAceEx		[NTDLL.@]
 */
NTSTATUS WINAPI RtlAddAccessDeniedAceEx(
	IN OUT PACL pAcl,
	IN DWORD dwAceRevision,
	IN DWORD AceFlags,
	IN DWORD AccessMask,
	IN PSID pSid)
1271
{
1272
   TRACE("(%p,0x%08x,0x%08x,%p)\n", pAcl, dwAceRevision, AccessMask, pSid);
1273 1274 1275

    return add_access_ace(pAcl, dwAceRevision, AceFlags,
                          AccessMask, pSid, ACCESS_DENIED_ACE_TYPE);
1276 1277
}

1278 1279 1280
/************************************************************************** 
 *  RtlAddAuditAccessAce     [NTDLL.@] 
 */ 
1281
NTSTATUS WINAPI RtlAddAuditAccessAceEx(
1282 1283
    IN OUT PACL pAcl, 
    IN DWORD dwAceRevision, 
1284
    IN DWORD dwAceFlags,
1285 1286 1287 1288 1289
    IN DWORD dwAccessMask, 
    IN PSID pSid, 
    IN BOOL bAuditSuccess, 
    IN BOOL bAuditFailure) 
{ 
1290
    TRACE("(%p,%d,0x%08x,0x%08x,%p,%u,%u)\n",pAcl,dwAceRevision,dwAceFlags,dwAccessMask,
1291 1292 1293 1294 1295 1296 1297 1298 1299 1300
          pSid,bAuditSuccess,bAuditFailure);

    if (bAuditSuccess)
        dwAceFlags |= SUCCESSFUL_ACCESS_ACE_FLAG;

    if (bAuditFailure)
        dwAceFlags |= FAILED_ACCESS_ACE_FLAG;

    return add_access_ace(pAcl, dwAceRevision, dwAceFlags,
                          dwAccessMask, pSid, SYSTEM_AUDIT_ACE_TYPE);
1301
} 
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315

/**************************************************************************
 *  RtlAddAuditAccessAce     [NTDLL.@]
 */
NTSTATUS WINAPI RtlAddAuditAccessAce(
    IN OUT PACL pAcl,
    IN DWORD dwAceRevision,
    IN DWORD dwAccessMask,
    IN PSID pSid,
    IN BOOL bAuditSuccess,
    IN BOOL bAuditFailure)
{
    return RtlAddAuditAccessAceEx(pAcl, dwAceRevision, 0, dwAccessMask, pSid, bAuditSuccess, bAuditFailure);
}
1316
 
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
/******************************************************************************
 *  RtlValidAcl		[NTDLL.@]
 */
BOOLEAN WINAPI RtlValidAcl(PACL pAcl)
{
        BOOLEAN ret;
	TRACE("(%p)\n", pAcl);

	__TRY
	{
		PACE_HEADER	ace;
		int		i;

1330 1331
                if (pAcl->AclRevision < MIN_ACL_REVISION ||
                    pAcl->AclRevision > MAX_ACL_REVISION)
1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343
                    ret = FALSE;
                else
                {
                    ace = (PACE_HEADER)(pAcl+1);
                    ret = TRUE;
                    for (i=0;i<=pAcl->AceCount;i++)
                    {
                        if ((char *)ace > (char *)pAcl + pAcl->AclSize)
                        {
                            ret = FALSE;
                            break;
                        }
1344 1345
                        if (i != pAcl->AceCount)
                            ace = (PACE_HEADER)(((BYTE*)ace)+ace->AceSize);
1346 1347 1348
                    }
                }
	}
1349
	__EXCEPT_PAGE_FAULT
1350 1351 1352 1353 1354 1355
	{
		WARN("(%p): invalid pointer!\n", pAcl);
		return 0;
	}
	__ENDTRY
        return ret;
Juergen Schmied's avatar
Juergen Schmied committed
1356 1357 1358
}

/******************************************************************************
1359
 *  RtlGetAce		[NTDLL.@]
Juergen Schmied's avatar
Juergen Schmied committed
1360
 */
1361
NTSTATUS WINAPI RtlGetAce(PACL pAcl,DWORD dwAceIndex,LPVOID *pAce )
Juergen Schmied's avatar
Juergen Schmied committed
1362
{
1363 1364
	PACE_HEADER ace;

1365
	TRACE("(%p,%d,%p)\n",pAcl,dwAceIndex,pAce);
1366

1367
	if (dwAceIndex >= pAcl->AceCount)
1368 1369 1370 1371 1372 1373
		return STATUS_INVALID_PARAMETER;

	ace = (PACE_HEADER)(pAcl + 1);
	for (;dwAceIndex;dwAceIndex--)
		ace = (PACE_HEADER)(((BYTE*)ace)+ace->AceSize);

1374
	*pAce = ace;
1375 1376

	return STATUS_SUCCESS;
Juergen Schmied's avatar
Juergen Schmied committed
1377 1378 1379 1380 1381 1382 1383
}

/*
 *	misc
 */

/******************************************************************************
1384
 *  RtlAdjustPrivilege		[NTDLL.@]
1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400
 *
 * Enables or disables a privilege from the calling thread or process.
 *
 * PARAMS
 *  Privilege     [I] Privilege index to change.
 *  Enable        [I] If TRUE, then enable the privilege otherwise disable.
 *  CurrentThread [I] If TRUE, then enable in calling thread, otherwise process.
 *  Enabled       [O] Whether privilege was previously enabled or disabled.
 *
 * RETURNS
 *  Success: STATUS_SUCCESS.
 *  Failure: NTSTATUS code.
 *
 * SEE ALSO
 *  NtAdjustPrivilegesToken, NtOpenThreadToken, NtOpenProcessToken.
 *
Juergen Schmied's avatar
Juergen Schmied committed
1401
 */
1402 1403 1404 1405 1406
NTSTATUS WINAPI
RtlAdjustPrivilege(ULONG Privilege,
                   BOOLEAN Enable,
                   BOOLEAN CurrentThread,
                   PBOOLEAN Enabled)
Juergen Schmied's avatar
Juergen Schmied committed
1407
{
1408 1409 1410 1411 1412 1413
    TOKEN_PRIVILEGES NewState;
    TOKEN_PRIVILEGES OldState;
    ULONG ReturnLength;
    HANDLE TokenHandle;
    NTSTATUS Status;

1414
    TRACE("(%d, %s, %s, %p)\n", Privilege, Enable ? "TRUE" : "FALSE",
1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432
        CurrentThread ? "TRUE" : "FALSE", Enabled);

    if (CurrentThread)
    {
        Status = NtOpenThreadToken(GetCurrentThread(),
                                   TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
                                   FALSE,
                                   &TokenHandle);
    }
    else
    {
        Status = NtOpenProcessToken(GetCurrentProcess(),
                                    TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
                                    &TokenHandle);
    }

    if (!NT_SUCCESS(Status))
    {
1433
        WARN("Retrieving token handle failed (Status %x)\n", Status);
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457
        return Status;
    }

    OldState.PrivilegeCount = 1;

    NewState.PrivilegeCount = 1;
    NewState.Privileges[0].Luid.LowPart = Privilege;
    NewState.Privileges[0].Luid.HighPart = 0;
    NewState.Privileges[0].Attributes = (Enable) ? SE_PRIVILEGE_ENABLED : 0;

    Status = NtAdjustPrivilegesToken(TokenHandle,
                                     FALSE,
                                     &NewState,
                                     sizeof(TOKEN_PRIVILEGES),
                                     &OldState,
                                     &ReturnLength);
    NtClose (TokenHandle);
    if (Status == STATUS_NOT_ALL_ASSIGNED)
    {
        TRACE("Failed to assign all privileges\n");
        return STATUS_PRIVILEGE_NOT_HELD;
    }
    if (!NT_SUCCESS(Status))
    {
1458
        WARN("NtAdjustPrivilegesToken() failed (Status %x)\n", Status);
1459 1460 1461 1462 1463 1464 1465 1466 1467
        return Status;
    }

    if (OldState.PrivilegeCount == 0)
        *Enabled = Enable;
    else
        *Enabled = (OldState.Privileges[0].Attributes & SE_PRIVILEGE_ENABLED);

    return STATUS_SUCCESS;
Juergen Schmied's avatar
Juergen Schmied committed
1468 1469
}

1470
/******************************************************************************
1471
 *  RtlImpersonateSelf		[NTDLL.@]
1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
 *
 * Makes an impersonation token that represents the process user and assigns
 * to the current thread.
 *
 * PARAMS
 *  ImpersonationLevel [I] Level at which to impersonate.
 *
 * RETURNS
 *  Success: STATUS_SUCCESS.
 *  Failure: NTSTATUS code.
1482
 */
1483
NTSTATUS WINAPI
1484 1485
RtlImpersonateSelf(SECURITY_IMPERSONATION_LEVEL ImpersonationLevel)
{
1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520
    NTSTATUS Status;
    OBJECT_ATTRIBUTES ObjectAttributes;
    HANDLE ProcessToken;
    HANDLE ImpersonationToken;

    TRACE("(%08x)\n", ImpersonationLevel);

    Status = NtOpenProcessToken( NtCurrentProcess(), TOKEN_DUPLICATE,
                                 &ProcessToken);
    if (Status != STATUS_SUCCESS)
        return Status;

    InitializeObjectAttributes( &ObjectAttributes, NULL, 0, NULL, NULL );

    Status = NtDuplicateToken( ProcessToken,
                               TOKEN_IMPERSONATE,
                               &ObjectAttributes,
                               ImpersonationLevel,
                               TokenImpersonation,
                               &ImpersonationToken );
    if (Status != STATUS_SUCCESS)
    {
        NtClose( ProcessToken );
        return Status;
    }

    Status = NtSetInformationThread( GetCurrentThread(),
                                     ThreadImpersonationToken,
                                     &ImpersonationToken,
                                     sizeof(ImpersonationToken) );

    NtClose( ImpersonationToken );
    NtClose( ProcessToken );

    return Status;
1521 1522
}

1523
/******************************************************************************
1524
 *  NtAccessCheck		[NTDLL.@]
Jon Griffiths's avatar
Jon Griffiths committed
1525
 *  ZwAccessCheck		[NTDLL.@]
1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548
 *
 * Checks that a user represented by a token is allowed to access an object
 * represented by a security descriptor.
 *
 * PARAMS
 *  SecurityDescriptor [I] The security descriptor of the object to check.
 *  ClientToken        [I] Token of the user accessing the object.
 *  DesiredAccess      [I] The desired access to the object.
 *  GenericMapping     [I] Mapping used to transform access rights in the SD to their specific forms.
 *  PrivilegeSet       [I/O] Privileges used during the access check.
 *  ReturnLength       [O] Number of bytes stored into PrivilegeSet.
 *  GrantedAccess      [O] The actual access rights granted.
 *  AccessStatus       [O] The status of the access check.
 *
 * RETURNS
 *  NTSTATUS code.
 *
 * NOTES
 *  DesiredAccess may be MAXIMUM_ALLOWED, in which case the function determines
 *  the maximum access rights allowed by the SD and returns them in
 *  GrantedAccess.
 *  The SecurityDescriptor must have a valid owner and groups present,
 *  otherwise the function will fail.
1549
 */
1550
NTSTATUS WINAPI
1551
NtAccessCheck(
1552 1553 1554 1555 1556 1557 1558 1559
    PSECURITY_DESCRIPTOR SecurityDescriptor,
    HANDLE ClientToken,
    ACCESS_MASK DesiredAccess,
    PGENERIC_MAPPING GenericMapping,
    PPRIVILEGE_SET PrivilegeSet,
    PULONG ReturnLength,
    PULONG GrantedAccess,
    NTSTATUS *AccessStatus)
1560
{
1561 1562
    NTSTATUS status;

1563
    TRACE("(%p, %p, %08x, %p, %p, %p, %p, %p)\n",
1564 1565 1566
        SecurityDescriptor, ClientToken, DesiredAccess, GenericMapping,
        PrivilegeSet, ReturnLength, GrantedAccess, AccessStatus);

1567 1568 1569
    if (!PrivilegeSet || !ReturnLength)
        return STATUS_ACCESS_VIOLATION;

1570 1571 1572
    SERVER_START_REQ( access_check )
    {
        struct security_descriptor sd;
1573 1574 1575 1576 1577 1578 1579
        PSID owner;
        PSID group;
        PACL sacl;
        PACL dacl;
        BOOLEAN defaulted, present;
        DWORD revision;
        SECURITY_DESCRIPTOR_CONTROL control;
1580

1581
        req->handle = wine_server_obj_handle( ClientToken );
1582 1583 1584 1585 1586 1587 1588
        req->desired_access = DesiredAccess;
        req->mapping_read = GenericMapping->GenericRead;
        req->mapping_write = GenericMapping->GenericWrite;
        req->mapping_execute = GenericMapping->GenericExecute;
        req->mapping_all = GenericMapping->GenericAll;

        /* marshal security descriptor */
1589 1590 1591 1592 1593 1594 1595
        RtlGetControlSecurityDescriptor( SecurityDescriptor, &control, &revision );
        sd.control = control & ~SE_SELF_RELATIVE;
        RtlGetOwnerSecurityDescriptor( SecurityDescriptor, &owner, &defaulted );
        sd.owner_len = RtlLengthSid( owner );
        RtlGetGroupSecurityDescriptor( SecurityDescriptor, &group, &defaulted );
        sd.group_len = RtlLengthSid( group );
        RtlGetSaclSecurityDescriptor( SecurityDescriptor, &present, &sacl, &defaulted );
1596
        sd.sacl_len = ((present && sacl) ? acl_bytesInUse(sacl) : 0);
1597
        RtlGetDaclSecurityDescriptor( SecurityDescriptor, &present, &dacl, &defaulted );
1598
        sd.dacl_len = ((present && dacl) ? acl_bytesInUse(dacl) : 0);
1599

1600
        wine_server_add_data( req, &sd, sizeof(sd) );
1601 1602 1603 1604
        wine_server_add_data( req, owner, sd.owner_len );
        wine_server_add_data( req, group, sd.group_len );
        wine_server_add_data( req, sacl, sd.sacl_len );
        wine_server_add_data( req, dacl, sd.dacl_len );
1605

1606
        wine_server_set_reply( req, PrivilegeSet->Privilege, *ReturnLength - FIELD_OFFSET( PRIVILEGE_SET, Privilege ) );
1607 1608 1609 1610 1611 1612 1613

        status = wine_server_call( req );

        *ReturnLength = FIELD_OFFSET( PRIVILEGE_SET, Privilege ) + reply->privileges_len;
        PrivilegeSet->PrivilegeCount = reply->privileges_len / sizeof(LUID_AND_ATTRIBUTES);

        if (status == STATUS_SUCCESS)
1614
        {
1615
            *AccessStatus = reply->access_status;
1616 1617
            *GrantedAccess = reply->access_granted;
        }
1618 1619 1620 1621
    }
    SERVER_END_REQ;

    return status;
1622 1623
}

1624
/******************************************************************************
1625
 *  NtSetSecurityObject		[NTDLL.@]
1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637
 *  ZwSetSecurityObject		[NTDLL.@]
 *
 * Sets specified parts of the object's security descriptor.
 *
 * PARAMS
 *  Handle              [I] Handle to the object to change security descriptor of.
 *  SecurityInformation [I] Specifies which parts of the security descriptor to set.
 *  SecurityDescriptor  [I] New parts of a security descriptor for the object.
 *
 * RETURNS
 *  NTSTATUS code.
 *
1638
 */
1639 1640 1641
NTSTATUS WINAPI NtSetSecurityObject(HANDLE Handle,
        SECURITY_INFORMATION SecurityInformation,
        PSECURITY_DESCRIPTOR SecurityDescriptor)
1642
{
1643 1644
    NTSTATUS status;
    struct security_descriptor sd;
1645 1646
    PACL dacl = NULL, sacl = NULL;
    PSID owner = NULL, group = NULL;
1647 1648 1649 1650 1651 1652 1653 1654 1655
    BOOLEAN defaulted, present;
    DWORD revision;
    SECURITY_DESCRIPTOR_CONTROL control;

    TRACE("%p 0x%08x %p\n", Handle, SecurityInformation, SecurityDescriptor);

    if (!SecurityDescriptor) return STATUS_ACCESS_VIOLATION;

    memset( &sd, 0, sizeof(sd) );
1656 1657
    status = RtlGetControlSecurityDescriptor( SecurityDescriptor, &control, &revision );
    if (status != STATUS_SUCCESS) return status;
1658 1659 1660 1661
    sd.control = control & ~SE_SELF_RELATIVE;

    if (SecurityInformation & OWNER_SECURITY_INFORMATION)
    {
1662 1663
        status = RtlGetOwnerSecurityDescriptor( SecurityDescriptor, &owner, &defaulted );
        if (status != STATUS_SUCCESS) return status;
1664 1665 1666 1667 1668 1669
        if (!(sd.owner_len = RtlLengthSid( owner )))
            return STATUS_INVALID_SECURITY_DESCR;
    }

    if (SecurityInformation & GROUP_SECURITY_INFORMATION)
    {
1670 1671
        status = RtlGetGroupSecurityDescriptor( SecurityDescriptor, &group, &defaulted );
        if (status != STATUS_SUCCESS) return status;
1672 1673 1674 1675 1676 1677
        if (!(sd.group_len = RtlLengthSid( group )))
            return STATUS_INVALID_SECURITY_DESCR;
    }

    if (SecurityInformation & SACL_SECURITY_INFORMATION)
    {
1678 1679
        status = RtlGetSaclSecurityDescriptor( SecurityDescriptor, &present, &sacl, &defaulted );
        if (status != STATUS_SUCCESS) return status;
1680
        sd.sacl_len = (sacl && present) ? acl_bytesInUse(sacl) : 0;
1681 1682 1683 1684 1685
        sd.control |= SE_SACL_PRESENT;
    }

    if (SecurityInformation & DACL_SECURITY_INFORMATION)
    {
1686 1687
        status = RtlGetDaclSecurityDescriptor( SecurityDescriptor, &present, &dacl, &defaulted );
        if (status != STATUS_SUCCESS) return status;
1688
        sd.dacl_len = (dacl && present) ? acl_bytesInUse(dacl) : 0;
1689 1690 1691 1692 1693
        sd.control |= SE_DACL_PRESENT;
    }

    SERVER_START_REQ( set_security_object )
    {
1694
        req->handle = wine_server_obj_handle( Handle );
1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706
        req->security_info = SecurityInformation;

        wine_server_add_data( req, &sd, sizeof(sd) );
        wine_server_add_data( req, owner, sd.owner_len );
        wine_server_add_data( req, group, sd.group_len );
        wine_server_add_data( req, sacl, sd.sacl_len );
        wine_server_add_data( req, dacl, sd.dacl_len );
        status = wine_server_call( req );
    }
    SERVER_END_REQ;

    return status;
1707 1708
}

1709
/******************************************************************************
1710
 * RtlConvertSidToUnicodeString (NTDLL.@)
1711 1712 1713 1714 1715
 *
 * The returned SID is used to access the USER registry hive usually
 *
 * the native function returns something like
 * "S-1-5-21-0000000000-000000000-0000000000-500";
1716 1717
 */
NTSTATUS WINAPI RtlConvertSidToUnicodeString(
1718
       PUNICODE_STRING String,
1719
       PSID pSid,
1720
       BOOLEAN AllocateString)
1721
{
1722 1723 1724
    static const WCHAR formatW[] = {'-','%','u',0};
    WCHAR buffer[2 + 10 + 10 + 10 * SID_MAX_SUB_AUTHORITIES];
    WCHAR *p = buffer;
1725
    const SID *sid = pSid;
1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737
    DWORD i, len;

    *p++ = 'S';
    p += sprintfW( p, formatW, sid->Revision );
    p += sprintfW( p, formatW, MAKELONG( MAKEWORD( sid->IdentifierAuthority.Value[5],
                                                   sid->IdentifierAuthority.Value[4] ),
                                         MAKEWORD( sid->IdentifierAuthority.Value[3],
                                                   sid->IdentifierAuthority.Value[2] )));
    for (i = 0; i < sid->SubAuthorityCount; i++)
        p += sprintfW( p, formatW, sid->SubAuthority[i] );

    len = (p + 1 - buffer) * sizeof(WCHAR);
1738

1739 1740 1741 1742 1743 1744 1745 1746
    String->Length = len - sizeof(WCHAR);
    if (AllocateString)
    {
        String->MaximumLength = len;
        if (!(String->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
            return STATUS_NO_MEMORY;
    }
    else if (len > String->MaximumLength) return STATUS_BUFFER_OVERFLOW;
1747

1748
    memcpy( String->Buffer, buffer, len );
1749
    return STATUS_SUCCESS;
1750
}
1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762

/******************************************************************************
 * RtlQueryInformationAcl (NTDLL.@)
 */
NTSTATUS WINAPI RtlQueryInformationAcl(
    PACL pAcl,
    LPVOID pAclInformation,
    DWORD nAclInformationLength,
    ACL_INFORMATION_CLASS dwAclInformationClass)
{
    NTSTATUS status = STATUS_SUCCESS;

1763
    TRACE("pAcl=%p pAclInfo=%p len=%d, class=%d\n", 
1764 1765 1766 1767 1768 1769
        pAcl, pAclInformation, nAclInformationLength, dwAclInformationClass);

    switch (dwAclInformationClass)
    {
        case AclRevisionInformation:
        {
1770
            PACL_REVISION_INFORMATION paclrev = pAclInformation;
1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781

            if (nAclInformationLength < sizeof(ACL_REVISION_INFORMATION))
                status = STATUS_INVALID_PARAMETER;
            else
                paclrev->AclRevision = pAcl->AclRevision;

            break;
        }

        case AclSizeInformation:
        {
1782
            PACL_SIZE_INFORMATION paclsize = pAclInformation;
1783 1784 1785 1786 1787 1788

            if (nAclInformationLength < sizeof(ACL_SIZE_INFORMATION))
                status = STATUS_INVALID_PARAMETER;
            else
            {
                paclsize->AceCount = pAcl->AceCount;
1789
                paclsize->AclBytesInUse = acl_bytesInUse(pAcl);
1790 1791
		if (pAcl->AclSize < paclsize->AclBytesInUse)
                {
1792
                    WARN("Acl uses %d bytes, but only has %d allocated!  Returning smaller of the two values.\n", pAcl->AclSize, paclsize->AclBytesInUse);
1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809
                    paclsize->AclBytesFree = 0;
                    paclsize->AclBytesInUse = pAcl->AclSize;
                }
                else
                    paclsize->AclBytesFree = pAcl->AclSize - paclsize->AclBytesInUse;
            }

            break;
        }

        default:
            WARN("Unknown AclInformationClass value: %d\n", dwAclInformationClass);
            status = STATUS_INVALID_PARAMETER;
    }

    return status;
}