sec.c 56.1 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
		return STATUS_UNKNOWN_REVISION;

	return STATUS_SUCCESS;
}

560 561 562 563 564 565 566 567 568 569
/**************************************************************************
 * RtlValidRelativeSecurityDescriptor		[NTDLL.@]
 */
BOOLEAN WINAPI RtlValidRelativeSecurityDescriptor(PSECURITY_DESCRIPTOR descriptor,
    ULONG length, SECURITY_INFORMATION info)
{
    FIXME("%p,%u,%d: semi-stub\n", descriptor, length, info);
    return RtlValidSecurityDescriptor(descriptor) == STATUS_SUCCESS;
}

Juergen Schmied's avatar
Juergen Schmied committed
570
/**************************************************************************
571
 *  RtlLengthSecurityDescriptor			[NTDLL.@]
Juergen Schmied's avatar
Juergen Schmied committed
572 573
 */
ULONG WINAPI RtlLengthSecurityDescriptor(
574
	PSECURITY_DESCRIPTOR pSecurityDescriptor)
Juergen Schmied's avatar
Juergen Schmied committed
575
{
576
	ULONG size;
577

578
	if ( pSecurityDescriptor == NULL )
Juergen Schmied's avatar
Juergen Schmied committed
579 580
		return 0;

581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
	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
602 603 604
}

/******************************************************************************
605
 *  RtlGetDaclSecurityDescriptor		[NTDLL.@]
Juergen Schmied's avatar
Juergen Schmied committed
606 607 608 609 610 611 612 613
 *
 */
NTSTATUS WINAPI RtlGetDaclSecurityDescriptor(
	IN PSECURITY_DESCRIPTOR pSecurityDescriptor,
	OUT PBOOLEAN lpbDaclPresent,
	OUT PACL *pDacl,
	OUT PBOOLEAN lpbDaclDefaulted)
{
614 615
	SECURITY_DESCRIPTOR* lpsd=pSecurityDescriptor;

616
	TRACE("(%p,%p,%p,%p)\n",
617
	pSecurityDescriptor, lpbDaclPresent, pDacl, lpbDaclDefaulted);
Juergen Schmied's avatar
Juergen Schmied committed
618

619
	if (lpsd->Revision != SECURITY_DESCRIPTOR_REVISION)
Juergen Schmied's avatar
Juergen Schmied committed
620 621
	  return STATUS_UNKNOWN_REVISION ;

622
	if ( (*lpbDaclPresent = (SE_DACL_PRESENT & lpsd->Control) ? 1 : 0) )
Juergen Schmied's avatar
Juergen Schmied committed
623
	{
624 625 626 627 628 629 630 631 632
            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;
633 634 635 636 637 638
        }
        else
        {
            *pDacl = NULL;
            *lpbDaclDefaulted = 0;
        }
639

Juergen Schmied's avatar
Juergen Schmied committed
640 641 642 643
	return STATUS_SUCCESS;
}

/**************************************************************************
644
 *  RtlSetDaclSecurityDescriptor		[NTDLL.@]
Juergen Schmied's avatar
Juergen Schmied committed
645 646
 */
NTSTATUS WINAPI RtlSetDaclSecurityDescriptor (
647
	PSECURITY_DESCRIPTOR pSecurityDescriptor,
Juergen Schmied's avatar
Juergen Schmied committed
648 649 650 651
	BOOLEAN daclpresent,
	PACL dacl,
	BOOLEAN dacldefaulted )
{
652 653
	SECURITY_DESCRIPTOR* lpsd=pSecurityDescriptor;

Juergen Schmied's avatar
Juergen Schmied committed
654 655 656 657 658
	if (lpsd->Revision!=SECURITY_DESCRIPTOR_REVISION)
		return STATUS_UNKNOWN_REVISION;
	if (lpsd->Control & SE_SELF_RELATIVE)
		return STATUS_INVALID_SECURITY_DESCR;

659
	if (!daclpresent)
660 661 662
	{
		lpsd->Control &= ~SE_DACL_PRESENT;
		return STATUS_SUCCESS;
Juergen Schmied's avatar
Juergen Schmied committed
663 664 665 666 667 668 669 670 671 672 673 674 675 676
	}

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

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

	return STATUS_SUCCESS;
}

/******************************************************************************
677
 *  RtlGetSaclSecurityDescriptor		[NTDLL.@]
Juergen Schmied's avatar
Juergen Schmied committed
678 679 680 681 682 683 684 685
 *
 */
NTSTATUS WINAPI RtlGetSaclSecurityDescriptor(
	IN PSECURITY_DESCRIPTOR pSecurityDescriptor,
	OUT PBOOLEAN lpbSaclPresent,
	OUT PACL *pSacl,
	OUT PBOOLEAN lpbSaclDefaulted)
{
686 687
	SECURITY_DESCRIPTOR* lpsd=pSecurityDescriptor;

688
	TRACE("(%p,%p,%p,%p)\n",
689
	pSecurityDescriptor, lpbSaclPresent, pSacl, lpbSaclDefaulted);
Juergen Schmied's avatar
Juergen Schmied committed
690

691
	if (lpsd->Revision != SECURITY_DESCRIPTOR_REVISION)
692
	  return STATUS_UNKNOWN_REVISION;
Juergen Schmied's avatar
Juergen Schmied committed
693

694
	if ( (*lpbSaclPresent = (SE_SACL_PRESENT & lpsd->Control) ? 1 : 0) )
Juergen Schmied's avatar
Juergen Schmied committed
695
	{
696 697 698 699 700 701 702 703 704
            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;
705
	}
Juergen Schmied's avatar
Juergen Schmied committed
706 707 708 709
	return STATUS_SUCCESS;
}

/**************************************************************************
710
 * RtlSetSaclSecurityDescriptor			[NTDLL.@]
Juergen Schmied's avatar
Juergen Schmied committed
711 712
 */
NTSTATUS WINAPI RtlSetSaclSecurityDescriptor (
713
	PSECURITY_DESCRIPTOR pSecurityDescriptor,
Juergen Schmied's avatar
Juergen Schmied committed
714 715 716 717
	BOOLEAN saclpresent,
	PACL sacl,
	BOOLEAN sacldefaulted)
{
718 719
	SECURITY_DESCRIPTOR* lpsd=pSecurityDescriptor;

Juergen Schmied's avatar
Juergen Schmied committed
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
	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;
}

/**************************************************************************
738
 * RtlGetOwnerSecurityDescriptor		[NTDLL.@]
Juergen Schmied's avatar
Juergen Schmied committed
739 740
 */
NTSTATUS WINAPI RtlGetOwnerSecurityDescriptor(
741
	PSECURITY_DESCRIPTOR pSecurityDescriptor,
Juergen Schmied's avatar
Juergen Schmied committed
742 743 744
	PSID *Owner,
	PBOOLEAN OwnerDefaulted)
{
745 746 747
	SECURITY_DESCRIPTOR* lpsd=pSecurityDescriptor;

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

750 751 752 753 754
        if ( lpsd->Control & SE_OWNER_DEFAULTED )
            *OwnerDefaulted = TRUE;
        else
            *OwnerDefaulted = FALSE;

755 756 757 758 759
        if (lpsd->Control & SE_SELF_RELATIVE)
        {
            SECURITY_DESCRIPTOR_RELATIVE *sd = pSecurityDescriptor;
            if (sd->Owner) *Owner = (PSID)SELF_RELATIVE_FIELD( sd, Owner );
            else *Owner = NULL;
760
        }
761 762
        else
            *Owner = lpsd->Owner;
763

Juergen Schmied's avatar
Juergen Schmied committed
764 765 766 767
	return STATUS_SUCCESS;
}

/**************************************************************************
768
 *                 RtlSetOwnerSecurityDescriptor		[NTDLL.@]
Juergen Schmied's avatar
Juergen Schmied committed
769 770
 */
NTSTATUS WINAPI RtlSetOwnerSecurityDescriptor(
771
	PSECURITY_DESCRIPTOR pSecurityDescriptor,
Juergen Schmied's avatar
Juergen Schmied committed
772 773 774
	PSID owner,
	BOOLEAN ownerdefaulted)
{
775 776
	SECURITY_DESCRIPTOR* lpsd=pSecurityDescriptor;

Juergen Schmied's avatar
Juergen Schmied committed
777 778 779 780 781 782 783 784 785 786 787 788 789 790
	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;
}

/**************************************************************************
791
 *                 RtlSetGroupSecurityDescriptor		[NTDLL.@]
Juergen Schmied's avatar
Juergen Schmied committed
792 793
 */
NTSTATUS WINAPI RtlSetGroupSecurityDescriptor (
794
	PSECURITY_DESCRIPTOR pSecurityDescriptor,
Juergen Schmied's avatar
Juergen Schmied committed
795 796 797
	PSID group,
	BOOLEAN groupdefaulted)
{
798 799
	SECURITY_DESCRIPTOR* lpsd=pSecurityDescriptor;

Juergen Schmied's avatar
Juergen Schmied committed
800 801 802 803 804 805 806 807 808 809 810 811
	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;
}
812

Juergen Schmied's avatar
Juergen Schmied committed
813
/**************************************************************************
814
 *                 RtlGetGroupSecurityDescriptor		[NTDLL.@]
Juergen Schmied's avatar
Juergen Schmied committed
815 816
 */
NTSTATUS WINAPI RtlGetGroupSecurityDescriptor(
817
	PSECURITY_DESCRIPTOR pSecurityDescriptor,
Juergen Schmied's avatar
Juergen Schmied committed
818 819 820
	PSID *Group,
	PBOOLEAN GroupDefaulted)
{
821 822 823
	SECURITY_DESCRIPTOR* lpsd=pSecurityDescriptor;

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

826 827 828 829 830
        if ( lpsd->Control & SE_GROUP_DEFAULTED )
            *GroupDefaulted = TRUE;
        else
            *GroupDefaulted = FALSE;

831 832 833 834 835 836 837 838
        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;
839

Juergen Schmied's avatar
Juergen Schmied committed
840
	return STATUS_SUCCESS;
841
}
Juergen Schmied's avatar
Juergen Schmied committed
842

843
/**************************************************************************
844
 *                 RtlMakeSelfRelativeSD		[NTDLL.@]
845 846 847 848 849 850
 */
NTSTATUS WINAPI RtlMakeSelfRelativeSD(
	IN PSECURITY_DESCRIPTOR pAbsoluteSecurityDescriptor,
	IN PSECURITY_DESCRIPTOR pSelfRelativeSecurityDescriptor,
	IN OUT LPDWORD lpdwBufferLength)
{
851
    DWORD offsetRel;
852
    ULONG length;
853
    SECURITY_DESCRIPTOR* pAbs = pAbsoluteSecurityDescriptor;
854
    SECURITY_DESCRIPTOR_RELATIVE *pRel = pSelfRelativeSecurityDescriptor;
855

856
    TRACE(" %p %p %p(%d)\n", pAbs, pRel, lpdwBufferLength,
857 858 859 860 861 862
        lpdwBufferLength ? *lpdwBufferLength: -1);

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

    length = RtlLengthSecurityDescriptor(pAbs);
863
    if (!(pAbs->Control & SE_SELF_RELATIVE)) length -= (sizeof(*pAbs) - sizeof(*pRel));
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882
    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;

883
    offsetRel = sizeof(SECURITY_DESCRIPTOR_RELATIVE);
884 885
    if (pAbs->Owner)
    {
886
        pRel->Owner = offsetRel;
887 888 889 890 891 892
        length = RtlLengthSid(pAbs->Owner);
        memcpy((LPBYTE)pRel + offsetRel, pAbs->Owner, length);
        offsetRel += length;
    }
    else
    {
893
        pRel->Owner = 0;
894
    }
895

896
    if (pAbs->Group)
897
    {
898
        pRel->Group = offsetRel;
899 900
        length = RtlLengthSid(pAbs->Group);
        memcpy((LPBYTE)pRel + offsetRel, pAbs->Group, length);
901
        offsetRel += length;
902 903 904
    }
    else
    {
905
        pRel->Group = 0;
906 907 908 909
    }

    if (pAbs->Sacl)
    {
910
        pRel->Sacl = offsetRel;
911 912
        length = pAbs->Sacl->AclSize;
        memcpy((LPBYTE)pRel + offsetRel, pAbs->Sacl, length);
913
        offsetRel += length;
914 915 916
    }
    else
    {
917
        pRel->Sacl = 0;
918 919
    }

920
    if (pAbs->Dacl)
921
    {
922
        pRel->Dacl = offsetRel;
923 924 925 926 927
        length = pAbs->Dacl->AclSize;
        memcpy((LPBYTE)pRel + offsetRel, pAbs->Dacl, length);
    }
    else
    {
928
        pRel->Dacl = 0;
929 930 931 932 933 934 935
    }

    return STATUS_SUCCESS;
}


/**************************************************************************
936 937
 *                 RtlSelfRelativeToAbsoluteSD [NTDLL.@]
 */
938 939 940 941 942 943 944 945 946 947 948 949 950 951
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;
952
    SECURITY_DESCRIPTOR* pAbs = pAbsoluteSecurityDescriptor;
953
    SECURITY_DESCRIPTOR_RELATIVE* pRel = pSelfRelativeSecurityDescriptor;
954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970

    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;
    }

971
    if ((pRel->Control & SE_DACL_PRESENT) && pRel->Dacl &&
972
        *lpdwDaclSize  < ((PACL)SELF_RELATIVE_FIELD(pRel,Dacl))->AclSize)
973
    {
974
        *lpdwDaclSize = ((PACL)SELF_RELATIVE_FIELD(pRel,Dacl))->AclSize;
975 976 977
        status = STATUS_BUFFER_TOO_SMALL;
    }

978
    if ((pRel->Control & SE_SACL_PRESENT) && pRel->Sacl &&
979
        *lpdwSaclSize  < ((PACL)SELF_RELATIVE_FIELD(pRel,Sacl))->AclSize)
980
    {
981
        *lpdwSaclSize = ((PACL)SELF_RELATIVE_FIELD(pRel,Sacl))->AclSize;
982 983 984 985
        status = STATUS_BUFFER_TOO_SMALL;
    }

    if (pRel->Owner &&
986
        *lpdwOwnerSize < RtlLengthSid((PSID)SELF_RELATIVE_FIELD(pRel,Owner)))
987
    {
988
        *lpdwOwnerSize = RtlLengthSid((PSID)SELF_RELATIVE_FIELD(pRel,Owner));
989 990 991 992
        status = STATUS_BUFFER_TOO_SMALL;
    }

    if (pRel->Group &&
993
        *lpdwPrimaryGroupSize < RtlLengthSid((PSID)SELF_RELATIVE_FIELD(pRel,Group)))
994
    {
995
        *lpdwPrimaryGroupSize = RtlLengthSid((PSID)SELF_RELATIVE_FIELD(pRel,Group));
996 997 998 999 1000 1001
        status = STATUS_BUFFER_TOO_SMALL;
    }

    if (status != STATUS_SUCCESS)
        return status;

1002
    /* Copy structures, and clear the ones we don't set */
1003 1004
    pAbs->Revision = pRel->Revision;
    pAbs->Control = pRel->Control & ~SE_SELF_RELATIVE;
1005 1006 1007 1008
    pAbs->Sacl = NULL;
    pAbs->Dacl = NULL;
    pAbs->Owner = NULL;
    pAbs->Group = NULL;
1009

1010
    if ((pRel->Control & SE_SACL_PRESENT) && pRel->Sacl)
1011
    {
1012
        PACL pAcl = (PACL)SELF_RELATIVE_FIELD( pRel, Sacl );
1013 1014 1015 1016 1017

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

1018
    if ((pRel->Control & SE_DACL_PRESENT) && pRel->Dacl)
1019
    {
1020
        PACL pAcl = (PACL)SELF_RELATIVE_FIELD( pRel, Dacl );
1021 1022 1023 1024 1025 1026
        memcpy(pDacl, pAcl, pAcl->AclSize);
        pAbs->Dacl = pDacl;
    }

    if (pRel->Owner)
    {
1027
        PSID psid = (PSID)SELF_RELATIVE_FIELD( pRel, Owner );
1028 1029 1030 1031 1032 1033
        memcpy(pOwner, psid, RtlLengthSid(psid));
        pAbs->Owner = pOwner;
    }

    if (pRel->Group)
    {
1034
        PSID psid = (PSID)SELF_RELATIVE_FIELD( pRel, Group );
1035 1036 1037 1038 1039
        memcpy(pPrimaryGroup, psid, RtlLengthSid(psid));
        pAbs->Group = pPrimaryGroup;
    }

    return status;
1040 1041
}

1042 1043 1044 1045
/******************************************************************************
 * RtlGetControlSecurityDescriptor (NTDLL.@)
 */
NTSTATUS WINAPI RtlGetControlSecurityDescriptor(
James Hawkins's avatar
James Hawkins committed
1046
    PSECURITY_DESCRIPTOR pSecurityDescriptor,
1047 1048 1049
    PSECURITY_DESCRIPTOR_CONTROL pControl,
    LPDWORD lpdwRevision)
{
James Hawkins's avatar
James Hawkins committed
1050
    SECURITY_DESCRIPTOR *lpsd = pSecurityDescriptor;
1051

James Hawkins's avatar
James Hawkins committed
1052
    TRACE("(%p,%p,%p)\n",pSecurityDescriptor,pControl,lpdwRevision);
1053 1054

    *lpdwRevision = lpsd->Revision;
James Hawkins's avatar
James Hawkins committed
1055 1056 1057 1058

    if (*lpdwRevision != SECURITY_DESCRIPTOR_REVISION)
        return STATUS_UNKNOWN_REVISION;

1059 1060 1061 1062 1063
    *pControl = lpsd->Control;

    return STATUS_SUCCESS;
}

1064 1065 1066 1067 1068 1069 1070 1071
/******************************************************************************
 * RtlSetControlSecurityDescriptor (NTDLL.@)
 */
NTSTATUS WINAPI RtlSetControlSecurityDescriptor(
    PSECURITY_DESCRIPTOR SecurityDescriptor,
    SECURITY_DESCRIPTOR_CONTROL ControlBitsOfInterest,
    SECURITY_DESCRIPTOR_CONTROL ControlBitsToSet)
{
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
    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);

1090 1091 1092
    return STATUS_SUCCESS;
}

1093 1094 1095 1096 1097 1098 1099 1100 1101

/**************************************************************************
 *                 RtlAbsoluteToSelfRelativeSD [NTDLL.@]
 */
NTSTATUS WINAPI RtlAbsoluteToSelfRelativeSD(
    PSECURITY_DESCRIPTOR AbsoluteSecurityDescriptor,
    PSECURITY_DESCRIPTOR SelfRelativeSecurityDescriptor,
    PULONG BufferLength)
{
1102 1103 1104
    SECURITY_DESCRIPTOR *abs = AbsoluteSecurityDescriptor;

    TRACE("%p %p %p\n", AbsoluteSecurityDescriptor,
1105
          SelfRelativeSecurityDescriptor, BufferLength);
1106 1107 1108 1109 1110 1111

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

    return RtlMakeSelfRelativeSD(AbsoluteSecurityDescriptor, 
        SelfRelativeSecurityDescriptor, BufferLength);
1112 1113 1114
}


Juergen Schmied's avatar
Juergen Schmied committed
1115 1116 1117 1118 1119
/*
 *	access control list's
 */

/**************************************************************************
1120
 *                 RtlCreateAcl				[NTDLL.@]
Juergen Schmied's avatar
Juergen Schmied committed
1121 1122 1123 1124
 *
 * NOTES
 *    This should return NTSTATUS
 */
1125
NTSTATUS WINAPI RtlCreateAcl(PACL acl,DWORD size,DWORD rev)
Juergen Schmied's avatar
Juergen Schmied committed
1126
{
1127
	TRACE("%p 0x%08x 0x%08x\n", acl, size, rev);
1128

1129
	if (rev < MIN_ACL_REVISION || rev > MAX_ACL_REVISION)
Juergen Schmied's avatar
Juergen Schmied committed
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
		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
1140
	return STATUS_SUCCESS;
Juergen Schmied's avatar
Juergen Schmied committed
1141 1142 1143
}

/**************************************************************************
1144
 *                 RtlFirstFreeAce			[NTDLL.@]
Juergen Schmied's avatar
Juergen Schmied committed
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
 * 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++) {
1158
		if ((BYTE *)ace >= (BYTE *)acl + acl->AclSize)
1159
			return FALSE;
Juergen Schmied's avatar
Juergen Schmied committed
1160 1161
		ace = (PACE_HEADER)(((BYTE*)ace)+ace->AceSize);
	}
1162
	if ((BYTE *)ace >= (BYTE *)acl + acl->AclSize)
1163
		return FALSE;
Juergen Schmied's avatar
Juergen Schmied committed
1164
	*x = ace;
1165
	return TRUE;
Juergen Schmied's avatar
Juergen Schmied committed
1166 1167 1168
}

/**************************************************************************
1169
 *                 RtlAddAce				[NTDLL.@]
Juergen Schmied's avatar
Juergen Schmied committed
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
 */
NTSTATUS WINAPI RtlAddAce(
	PACL acl,
	DWORD rev,
	DWORD xnrofaces,
	PACE_HEADER acestart,
	DWORD acelen)
{
	PACE_HEADER	ace,targetace;
	int		nrofaces;

1181
	if (!RtlValidAcl(acl))
Juergen Schmied's avatar
Juergen Schmied committed
1182 1183 1184 1185
		return STATUS_INVALID_PARAMETER;
	if (!RtlFirstFreeAce(acl,&targetace))
		return STATUS_INVALID_PARAMETER;
	nrofaces=0;ace=acestart;
1186
	while (((BYTE *)ace - (BYTE *)acestart) < acelen) {
Juergen Schmied's avatar
Juergen Schmied committed
1187 1188 1189
		nrofaces++;
		ace = (PACE_HEADER)(((BYTE*)ace)+ace->AceSize);
	}
1190
	if ((BYTE *)targetace + acelen > (BYTE *)acl + acl->AclSize) /* too much aces */
Juergen Schmied's avatar
Juergen Schmied committed
1191
		return STATUS_INVALID_PARAMETER;
1192
	memcpy(targetace,acestart,acelen);
Juergen Schmied's avatar
Juergen Schmied committed
1193
	acl->AceCount+=nrofaces;
1194 1195
	if (rev > acl->AclRevision)
		acl->AclRevision = rev;
Juergen Schmied's avatar
Juergen Schmied committed
1196 1197 1198
	return STATUS_SUCCESS;
}

1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
/**************************************************************************
 *                 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;

1214
		/* skip over the ACE we are deleting */
1215
		pcAce = (PACE_HEADER)(((BYTE*)pAce)+pAce->AceSize);
1216 1217 1218
		dwAceIndex++;

		/* calculate the length of the rest */
1219 1220 1221 1222 1223 1224
		for (; dwAceIndex < pAcl->AceCount; dwAceIndex++)
		{
			len += pcAce->AceSize;
			pcAce = (PACE_HEADER)(((BYTE*)pcAce) + pcAce->AceSize);
		}

1225 1226 1227
		/* slide them all backwards */
		memmove(pAce, ((BYTE*)pAce)+pAce->AceSize, len);
		pAcl->AceCount--;
1228 1229
	}

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

1232 1233 1234
	return status;
}

Juergen Schmied's avatar
Juergen Schmied committed
1235
/******************************************************************************
1236
 *  RtlAddAccessAllowedAce		[NTDLL.@]
Juergen Schmied's avatar
Juergen Schmied committed
1237
 */
1238
NTSTATUS WINAPI RtlAddAccessAllowedAce(
1239 1240 1241 1242
	IN OUT PACL pAcl,
	IN DWORD dwAceRevision,
	IN DWORD AccessMask,
	IN PSID pSid)
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255
{
	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
1256
{
1257
   TRACE("(%p,0x%08x,0x%08x,%p)\n", pAcl, dwAceRevision, AccessMask, pSid);
1258 1259 1260

    return add_access_ace(pAcl, dwAceRevision, AceFlags,
                          AccessMask, pSid, ACCESS_ALLOWED_ACE_TYPE);
1261 1262
}

1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
/******************************************************************************
 *  RtlAddAccessAllowedObjectAce		[NTDLL.@]
 */
NTSTATUS WINAPI RtlAddAccessAllowedObjectAce(
    IN OUT PACL pAcl,
    IN DWORD dwAceRevision,
    IN DWORD dwAceFlags,
    IN DWORD dwAccessMask,
    IN GUID* pObjectTypeGuid,
    IN GUID* pInheritedObjectTypeGuid,
    IN PSID pSid)
{
    FIXME("%p %x %x %x %p %p %p - stub\n", pAcl, dwAceRevision, dwAceFlags, dwAccessMask,
          pObjectTypeGuid, pInheritedObjectTypeGuid, pSid);
    return STATUS_NOT_IMPLEMENTED;
}

1280 1281 1282 1283 1284 1285 1286 1287
/******************************************************************************
 *  RtlAddAccessDeniedAce		[NTDLL.@]
 */
NTSTATUS WINAPI RtlAddAccessDeniedAce(
	IN OUT PACL pAcl,
	IN DWORD dwAceRevision,
	IN DWORD AccessMask,
	IN PSID pSid)
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300
{
	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)
1301
{
1302
   TRACE("(%p,0x%08x,0x%08x,%p)\n", pAcl, dwAceRevision, AccessMask, pSid);
1303 1304 1305

    return add_access_ace(pAcl, dwAceRevision, AceFlags,
                          AccessMask, pSid, ACCESS_DENIED_ACE_TYPE);
1306 1307
}

1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324
/******************************************************************************
 *  RtlAddAccessDeniedObjectAce [NTDLL.@]
 */
NTSTATUS WINAPI RtlAddAccessDeniedObjectAce(
    IN OUT PACL pAcl,
    IN DWORD dwAceRevision,
    IN DWORD dwAceFlags,
    IN DWORD dwAccessMask,
    IN GUID* pObjectTypeGuid,
    IN GUID* pInheritedObjectTypeGuid,
    IN PSID pSid)
{
    FIXME("%p %x %x %x %p %p %p - stub\n", pAcl, dwAceRevision, dwAceFlags, dwAccessMask,
          pObjectTypeGuid, pInheritedObjectTypeGuid, pSid);
    return STATUS_NOT_IMPLEMENTED;
}

1325 1326 1327
/************************************************************************** 
 *  RtlAddAuditAccessAce     [NTDLL.@] 
 */ 
1328
NTSTATUS WINAPI RtlAddAuditAccessAceEx(
1329 1330
    IN OUT PACL pAcl, 
    IN DWORD dwAceRevision, 
1331
    IN DWORD dwAceFlags,
1332 1333 1334 1335 1336
    IN DWORD dwAccessMask, 
    IN PSID pSid, 
    IN BOOL bAuditSuccess, 
    IN BOOL bAuditFailure) 
{ 
1337
    TRACE("(%p,%d,0x%08x,0x%08x,%p,%u,%u)\n",pAcl,dwAceRevision,dwAceFlags,dwAccessMask,
1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
          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);
1348
} 
1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362

/**************************************************************************
 *  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);
}
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382

/******************************************************************************
 *  RtlAddAuditAccessObjectAce [NTDLL.@]
 */
NTSTATUS WINAPI RtlAddAuditAccessObjectAce(
    IN OUT PACL pAcl,
    IN DWORD dwAceRevision,
    IN DWORD dwAceFlags,
    IN DWORD dwAccessMask,
    IN GUID* pObjectTypeGuid,
    IN GUID* pInheritedObjectTypeGuid,
    IN PSID pSid,
    IN BOOL bAuditSuccess,
    IN BOOL bAuditFailure)
{
    FIXME("%p %x %x %x %p %p %p %d %d - stub\n", pAcl, dwAceRevision, dwAceFlags, dwAccessMask,
          pObjectTypeGuid, pInheritedObjectTypeGuid, pSid, bAuditSuccess, bAuditFailure);
    return STATUS_NOT_IMPLEMENTED;
}

1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409
/**************************************************************************
 *  RtlAddMandatoryAce     [NTDLL.@]
 */
NTSTATUS WINAPI RtlAddMandatoryAce(
    IN OUT PACL pAcl,
    IN DWORD dwAceRevision,
    IN DWORD dwAceFlags,
    IN DWORD dwMandatoryFlags,
    IN DWORD dwAceType,
    IN PSID pSid)
{
    static const DWORD valid_flags = SYSTEM_MANDATORY_LABEL_NO_WRITE_UP |
                                     SYSTEM_MANDATORY_LABEL_NO_READ_UP |
                                     SYSTEM_MANDATORY_LABEL_NO_EXECUTE_UP;

    TRACE("(%p, %u, 0x%08x, 0x%08x, %u, %p)\n", pAcl, dwAceRevision, dwAceFlags,
                                                dwMandatoryFlags, dwAceType, pSid);

    if (dwAceType != SYSTEM_MANDATORY_LABEL_ACE_TYPE)
        return STATUS_INVALID_PARAMETER;

    if (dwMandatoryFlags & ~valid_flags)
        return STATUS_INVALID_PARAMETER;

    return add_access_ace(pAcl, dwAceRevision, dwAceFlags, dwMandatoryFlags, pSid, dwAceType);
}

1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422
/******************************************************************************
 *  RtlValidAcl		[NTDLL.@]
 */
BOOLEAN WINAPI RtlValidAcl(PACL pAcl)
{
        BOOLEAN ret;
	TRACE("(%p)\n", pAcl);

	__TRY
	{
		PACE_HEADER	ace;
		int		i;

1423 1424
                if (pAcl->AclRevision < MIN_ACL_REVISION ||
                    pAcl->AclRevision > MAX_ACL_REVISION)
1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
                    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;
                        }
1437 1438
                        if (i != pAcl->AceCount)
                            ace = (PACE_HEADER)(((BYTE*)ace)+ace->AceSize);
1439 1440 1441
                    }
                }
	}
1442
	__EXCEPT_PAGE_FAULT
1443 1444
	{
		WARN("(%p): invalid pointer!\n", pAcl);
1445
		return FALSE;
1446 1447 1448
	}
	__ENDTRY
        return ret;
Juergen Schmied's avatar
Juergen Schmied committed
1449 1450 1451
}

/******************************************************************************
1452
 *  RtlGetAce		[NTDLL.@]
Juergen Schmied's avatar
Juergen Schmied committed
1453
 */
1454
NTSTATUS WINAPI RtlGetAce(PACL pAcl,DWORD dwAceIndex,LPVOID *pAce )
Juergen Schmied's avatar
Juergen Schmied committed
1455
{
1456 1457
	PACE_HEADER ace;

1458
	TRACE("(%p,%d,%p)\n",pAcl,dwAceIndex,pAce);
1459

1460
	if (dwAceIndex >= pAcl->AceCount)
1461 1462 1463 1464 1465 1466
		return STATUS_INVALID_PARAMETER;

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

1467
	*pAce = ace;
1468 1469

	return STATUS_SUCCESS;
Juergen Schmied's avatar
Juergen Schmied committed
1470 1471 1472 1473 1474 1475 1476
}

/*
 *	misc
 */

/******************************************************************************
1477
 *  RtlAdjustPrivilege		[NTDLL.@]
1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493
 *
 * 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
1494
 */
1495 1496 1497 1498 1499
NTSTATUS WINAPI
RtlAdjustPrivilege(ULONG Privilege,
                   BOOLEAN Enable,
                   BOOLEAN CurrentThread,
                   PBOOLEAN Enabled)
Juergen Schmied's avatar
Juergen Schmied committed
1500
{
1501 1502 1503 1504 1505 1506
    TOKEN_PRIVILEGES NewState;
    TOKEN_PRIVILEGES OldState;
    ULONG ReturnLength;
    HANDLE TokenHandle;
    NTSTATUS Status;

1507
    TRACE("(%d, %s, %s, %p)\n", Privilege, Enable ? "TRUE" : "FALSE",
1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525
        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))
    {
1526
        WARN("Retrieving token handle failed (Status %x)\n", Status);
1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550
        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))
    {
1551
        WARN("NtAdjustPrivilegesToken() failed (Status %x)\n", Status);
1552 1553 1554 1555 1556 1557 1558 1559 1560
        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
1561 1562
}

1563
/******************************************************************************
1564
 *  RtlImpersonateSelf		[NTDLL.@]
1565 1566 1567 1568 1569 1570 1571 1572 1573 1574
 *
 * 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.
1575
 */
1576
NTSTATUS WINAPI
1577 1578
RtlImpersonateSelf(SECURITY_IMPERSONATION_LEVEL ImpersonationLevel)
{
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613
    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;
1614 1615
}

1616 1617 1618 1619 1620 1621 1622 1623 1624 1625
/******************************************************************************
 *  NtImpersonateAnonymousToken      [NTDLL.@]
 */
NTSTATUS WINAPI
NtImpersonateAnonymousToken(HANDLE thread)
{
    FIXME("(%p): stub\n", thread);
    return STATUS_NOT_IMPLEMENTED;
}

1626
/******************************************************************************
1627
 *  NtAccessCheck		[NTDLL.@]
Jon Griffiths's avatar
Jon Griffiths committed
1628
 *  ZwAccessCheck		[NTDLL.@]
1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651
 *
 * 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.
1652
 */
1653
NTSTATUS WINAPI
1654
NtAccessCheck(
1655 1656 1657 1658 1659 1660 1661 1662
    PSECURITY_DESCRIPTOR SecurityDescriptor,
    HANDLE ClientToken,
    ACCESS_MASK DesiredAccess,
    PGENERIC_MAPPING GenericMapping,
    PPRIVILEGE_SET PrivilegeSet,
    PULONG ReturnLength,
    PULONG GrantedAccess,
    NTSTATUS *AccessStatus)
1663
{
1664 1665
    NTSTATUS status;

1666
    TRACE("(%p, %p, %08x, %p, %p, %p, %p, %p)\n",
1667 1668 1669
        SecurityDescriptor, ClientToken, DesiredAccess, GenericMapping,
        PrivilegeSet, ReturnLength, GrantedAccess, AccessStatus);

1670 1671 1672
    if (!PrivilegeSet || !ReturnLength)
        return STATUS_ACCESS_VIOLATION;

1673 1674 1675
    SERVER_START_REQ( access_check )
    {
        struct security_descriptor sd;
1676 1677 1678 1679 1680 1681 1682
        PSID owner;
        PSID group;
        PACL sacl;
        PACL dacl;
        BOOLEAN defaulted, present;
        DWORD revision;
        SECURITY_DESCRIPTOR_CONTROL control;
1683

1684
        req->handle = wine_server_obj_handle( ClientToken );
1685 1686 1687 1688 1689 1690 1691
        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 */
1692 1693 1694 1695 1696 1697 1698
        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 );
1699
        sd.sacl_len = ((present && sacl) ? acl_bytesInUse(sacl) : 0);
1700
        RtlGetDaclSecurityDescriptor( SecurityDescriptor, &present, &dacl, &defaulted );
1701
        sd.dacl_len = ((present && dacl) ? acl_bytesInUse(dacl) : 0);
1702

1703
        wine_server_add_data( req, &sd, sizeof(sd) );
1704 1705 1706 1707
        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 );
1708

1709
        wine_server_set_reply( req, PrivilegeSet->Privilege, *ReturnLength - FIELD_OFFSET( PRIVILEGE_SET, Privilege ) );
1710 1711 1712 1713 1714 1715 1716

        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)
1717
        {
1718
            *AccessStatus = reply->access_status;
1719 1720
            *GrantedAccess = reply->access_granted;
        }
1721 1722 1723 1724
    }
    SERVER_END_REQ;

    return status;
1725 1726
}

1727
/******************************************************************************
1728
 *  NtSetSecurityObject		[NTDLL.@]
1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740
 *  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.
 *
1741
 */
1742 1743 1744
NTSTATUS WINAPI NtSetSecurityObject(HANDLE Handle,
        SECURITY_INFORMATION SecurityInformation,
        PSECURITY_DESCRIPTOR SecurityDescriptor)
1745
{
1746 1747
    NTSTATUS status;
    struct security_descriptor sd;
1748 1749
    PACL dacl = NULL, sacl = NULL;
    PSID owner = NULL, group = NULL;
1750 1751 1752 1753 1754 1755 1756 1757 1758
    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) );
1759 1760
    status = RtlGetControlSecurityDescriptor( SecurityDescriptor, &control, &revision );
    if (status != STATUS_SUCCESS) return status;
1761 1762 1763 1764
    sd.control = control & ~SE_SELF_RELATIVE;

    if (SecurityInformation & OWNER_SECURITY_INFORMATION)
    {
1765 1766
        status = RtlGetOwnerSecurityDescriptor( SecurityDescriptor, &owner, &defaulted );
        if (status != STATUS_SUCCESS) return status;
1767 1768 1769 1770 1771 1772
        if (!(sd.owner_len = RtlLengthSid( owner )))
            return STATUS_INVALID_SECURITY_DESCR;
    }

    if (SecurityInformation & GROUP_SECURITY_INFORMATION)
    {
1773 1774
        status = RtlGetGroupSecurityDescriptor( SecurityDescriptor, &group, &defaulted );
        if (status != STATUS_SUCCESS) return status;
1775 1776 1777 1778
        if (!(sd.group_len = RtlLengthSid( group )))
            return STATUS_INVALID_SECURITY_DESCR;
    }

1779 1780
    if (SecurityInformation & SACL_SECURITY_INFORMATION ||
        SecurityInformation & LABEL_SECURITY_INFORMATION)
1781
    {
1782 1783
        status = RtlGetSaclSecurityDescriptor( SecurityDescriptor, &present, &sacl, &defaulted );
        if (status != STATUS_SUCCESS) return status;
1784
        sd.sacl_len = (sacl && present) ? acl_bytesInUse(sacl) : 0;
1785 1786 1787 1788 1789
        sd.control |= SE_SACL_PRESENT;
    }

    if (SecurityInformation & DACL_SECURITY_INFORMATION)
    {
1790 1791
        status = RtlGetDaclSecurityDescriptor( SecurityDescriptor, &present, &dacl, &defaulted );
        if (status != STATUS_SUCCESS) return status;
1792
        sd.dacl_len = (dacl && present) ? acl_bytesInUse(dacl) : 0;
1793 1794 1795 1796 1797
        sd.control |= SE_DACL_PRESENT;
    }

    SERVER_START_REQ( set_security_object )
    {
1798
        req->handle = wine_server_obj_handle( Handle );
1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810
        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;
1811 1812
}

1813
/******************************************************************************
1814
 * RtlConvertSidToUnicodeString (NTDLL.@)
1815 1816 1817 1818 1819
 *
 * 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";
1820 1821
 */
NTSTATUS WINAPI RtlConvertSidToUnicodeString(
1822
       PUNICODE_STRING String,
1823
       PSID pSid,
1824
       BOOLEAN AllocateString)
1825
{
1826 1827 1828
    static const WCHAR formatW[] = {'-','%','u',0};
    WCHAR buffer[2 + 10 + 10 + 10 * SID_MAX_SUB_AUTHORITIES];
    WCHAR *p = buffer;
1829
    const SID *sid = pSid;
1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841
    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);
1842

1843 1844 1845 1846 1847 1848 1849 1850
    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;
1851

1852
    memcpy( String->Buffer, buffer, len );
1853
    return STATUS_SUCCESS;
1854
}
1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866

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

1867
    TRACE("pAcl=%p pAclInfo=%p len=%d, class=%d\n", 
1868 1869 1870 1871 1872 1873
        pAcl, pAclInformation, nAclInformationLength, dwAclInformationClass);

    switch (dwAclInformationClass)
    {
        case AclRevisionInformation:
        {
1874
            PACL_REVISION_INFORMATION paclrev = pAclInformation;
1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885

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

            break;
        }

        case AclSizeInformation:
        {
1886
            PACL_SIZE_INFORMATION paclsize = pAclInformation;
1887 1888 1889 1890 1891 1892

            if (nAclInformationLength < sizeof(ACL_SIZE_INFORMATION))
                status = STATUS_INVALID_PARAMETER;
            else
            {
                paclsize->AceCount = pAcl->AceCount;
1893
                paclsize->AclBytesInUse = acl_bytesInUse(pAcl);
1894 1895
		if (pAcl->AclSize < paclsize->AclBytesInUse)
                {
1896
                    WARN("Acl uses %d bytes, but only has %d allocated!  Returning smaller of the two values.\n", pAcl->AclSize, paclsize->AclBytesInUse);
1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913
                    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;
}
1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926

BOOL WINAPI RtlConvertToAutoInheritSecurityObject(
        PSECURITY_DESCRIPTOR pdesc,
        PSECURITY_DESCRIPTOR cdesc,
        PSECURITY_DESCRIPTOR* ndesc,
        GUID* objtype,
        BOOL isdir,
        PGENERIC_MAPPING genmap )
{
    FIXME("%p %p %p %p %d %p - stub\n", pdesc, cdesc, ndesc, objtype, isdir, genmap);

    return FALSE;
}