sec.c 55 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 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
        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;

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

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

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

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

    return STATUS_SUCCESS;
}


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

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

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

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

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

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

    if (status != STATUS_SUCCESS)
        return status;

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

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

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

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

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

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

    return status;
1039 1040
}

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

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

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

    if (*lpdwRevision != SECURITY_DESCRIPTOR_REVISION)
        return STATUS_UNKNOWN_REVISION;

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

    return STATUS_SUCCESS;
}

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

1089 1090 1091
    return STATUS_SUCCESS;
}

1092 1093 1094 1095 1096 1097 1098 1099 1100

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

1231 1232 1233
	return status;
}

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

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

1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
/******************************************************************************
 *  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;
}

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

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

1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
/******************************************************************************
 *  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;
}

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

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

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

1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394
/******************************************************************************
 *  RtlValidAcl		[NTDLL.@]
 */
BOOLEAN WINAPI RtlValidAcl(PACL pAcl)
{
        BOOLEAN ret;
	TRACE("(%p)\n", pAcl);

	__TRY
	{
		PACE_HEADER	ace;
		int		i;

1395 1396
                if (pAcl->AclRevision < MIN_ACL_REVISION ||
                    pAcl->AclRevision > MAX_ACL_REVISION)
1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
                    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;
                        }
1409 1410
                        if (i != pAcl->AceCount)
                            ace = (PACE_HEADER)(((BYTE*)ace)+ace->AceSize);
1411 1412 1413
                    }
                }
	}
1414
	__EXCEPT_PAGE_FAULT
1415 1416
	{
		WARN("(%p): invalid pointer!\n", pAcl);
1417
		return FALSE;
1418 1419 1420
	}
	__ENDTRY
        return ret;
Juergen Schmied's avatar
Juergen Schmied committed
1421 1422 1423
}

/******************************************************************************
1424
 *  RtlGetAce		[NTDLL.@]
Juergen Schmied's avatar
Juergen Schmied committed
1425
 */
1426
NTSTATUS WINAPI RtlGetAce(PACL pAcl,DWORD dwAceIndex,LPVOID *pAce )
Juergen Schmied's avatar
Juergen Schmied committed
1427
{
1428 1429
	PACE_HEADER ace;

1430
	TRACE("(%p,%d,%p)\n",pAcl,dwAceIndex,pAce);
1431

1432
	if (dwAceIndex >= pAcl->AceCount)
1433 1434 1435 1436 1437 1438
		return STATUS_INVALID_PARAMETER;

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

1439
	*pAce = ace;
1440 1441

	return STATUS_SUCCESS;
Juergen Schmied's avatar
Juergen Schmied committed
1442 1443 1444 1445 1446 1447 1448
}

/*
 *	misc
 */

/******************************************************************************
1449
 *  RtlAdjustPrivilege		[NTDLL.@]
1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
 *
 * 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
1466
 */
1467 1468 1469 1470 1471
NTSTATUS WINAPI
RtlAdjustPrivilege(ULONG Privilege,
                   BOOLEAN Enable,
                   BOOLEAN CurrentThread,
                   PBOOLEAN Enabled)
Juergen Schmied's avatar
Juergen Schmied committed
1472
{
1473 1474 1475 1476 1477 1478
    TOKEN_PRIVILEGES NewState;
    TOKEN_PRIVILEGES OldState;
    ULONG ReturnLength;
    HANDLE TokenHandle;
    NTSTATUS Status;

1479
    TRACE("(%d, %s, %s, %p)\n", Privilege, Enable ? "TRUE" : "FALSE",
1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497
        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))
    {
1498
        WARN("Retrieving token handle failed (Status %x)\n", Status);
1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522
        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))
    {
1523
        WARN("NtAdjustPrivilegesToken() failed (Status %x)\n", Status);
1524 1525 1526 1527 1528 1529 1530 1531 1532
        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
1533 1534
}

1535
/******************************************************************************
1536
 *  RtlImpersonateSelf		[NTDLL.@]
1537 1538 1539 1540 1541 1542 1543 1544 1545 1546
 *
 * 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.
1547
 */
1548
NTSTATUS WINAPI
1549 1550
RtlImpersonateSelf(SECURITY_IMPERSONATION_LEVEL ImpersonationLevel)
{
1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585
    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;
1586 1587
}

1588 1589 1590 1591 1592 1593 1594 1595 1596 1597
/******************************************************************************
 *  NtImpersonateAnonymousToken      [NTDLL.@]
 */
NTSTATUS WINAPI
NtImpersonateAnonymousToken(HANDLE thread)
{
    FIXME("(%p): stub\n", thread);
    return STATUS_NOT_IMPLEMENTED;
}

1598
/******************************************************************************
1599
 *  NtAccessCheck		[NTDLL.@]
Jon Griffiths's avatar
Jon Griffiths committed
1600
 *  ZwAccessCheck		[NTDLL.@]
1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623
 *
 * 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.
1624
 */
1625
NTSTATUS WINAPI
1626
NtAccessCheck(
1627 1628 1629 1630 1631 1632 1633 1634
    PSECURITY_DESCRIPTOR SecurityDescriptor,
    HANDLE ClientToken,
    ACCESS_MASK DesiredAccess,
    PGENERIC_MAPPING GenericMapping,
    PPRIVILEGE_SET PrivilegeSet,
    PULONG ReturnLength,
    PULONG GrantedAccess,
    NTSTATUS *AccessStatus)
1635
{
1636 1637
    NTSTATUS status;

1638
    TRACE("(%p, %p, %08x, %p, %p, %p, %p, %p)\n",
1639 1640 1641
        SecurityDescriptor, ClientToken, DesiredAccess, GenericMapping,
        PrivilegeSet, ReturnLength, GrantedAccess, AccessStatus);

1642 1643 1644
    if (!PrivilegeSet || !ReturnLength)
        return STATUS_ACCESS_VIOLATION;

1645 1646 1647
    SERVER_START_REQ( access_check )
    {
        struct security_descriptor sd;
1648 1649 1650 1651 1652 1653 1654
        PSID owner;
        PSID group;
        PACL sacl;
        PACL dacl;
        BOOLEAN defaulted, present;
        DWORD revision;
        SECURITY_DESCRIPTOR_CONTROL control;
1655

1656
        req->handle = wine_server_obj_handle( ClientToken );
1657 1658 1659 1660 1661 1662 1663
        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 */
1664 1665 1666 1667 1668 1669 1670
        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 );
1671
        sd.sacl_len = ((present && sacl) ? acl_bytesInUse(sacl) : 0);
1672
        RtlGetDaclSecurityDescriptor( SecurityDescriptor, &present, &dacl, &defaulted );
1673
        sd.dacl_len = ((present && dacl) ? acl_bytesInUse(dacl) : 0);
1674

1675
        wine_server_add_data( req, &sd, sizeof(sd) );
1676 1677 1678 1679
        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 );
1680

1681
        wine_server_set_reply( req, PrivilegeSet->Privilege, *ReturnLength - FIELD_OFFSET( PRIVILEGE_SET, Privilege ) );
1682 1683 1684 1685 1686 1687 1688

        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)
1689
        {
1690
            *AccessStatus = reply->access_status;
1691 1692
            *GrantedAccess = reply->access_granted;
        }
1693 1694 1695 1696
    }
    SERVER_END_REQ;

    return status;
1697 1698
}

1699
/******************************************************************************
1700
 *  NtSetSecurityObject		[NTDLL.@]
1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712
 *  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.
 *
1713
 */
1714 1715 1716
NTSTATUS WINAPI NtSetSecurityObject(HANDLE Handle,
        SECURITY_INFORMATION SecurityInformation,
        PSECURITY_DESCRIPTOR SecurityDescriptor)
1717
{
1718 1719
    NTSTATUS status;
    struct security_descriptor sd;
1720 1721
    PACL dacl = NULL, sacl = NULL;
    PSID owner = NULL, group = NULL;
1722 1723 1724 1725 1726 1727 1728 1729 1730
    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) );
1731 1732
    status = RtlGetControlSecurityDescriptor( SecurityDescriptor, &control, &revision );
    if (status != STATUS_SUCCESS) return status;
1733 1734 1735 1736
    sd.control = control & ~SE_SELF_RELATIVE;

    if (SecurityInformation & OWNER_SECURITY_INFORMATION)
    {
1737 1738
        status = RtlGetOwnerSecurityDescriptor( SecurityDescriptor, &owner, &defaulted );
        if (status != STATUS_SUCCESS) return status;
1739 1740 1741 1742 1743 1744
        if (!(sd.owner_len = RtlLengthSid( owner )))
            return STATUS_INVALID_SECURITY_DESCR;
    }

    if (SecurityInformation & GROUP_SECURITY_INFORMATION)
    {
1745 1746
        status = RtlGetGroupSecurityDescriptor( SecurityDescriptor, &group, &defaulted );
        if (status != STATUS_SUCCESS) return status;
1747 1748 1749 1750 1751 1752
        if (!(sd.group_len = RtlLengthSid( group )))
            return STATUS_INVALID_SECURITY_DESCR;
    }

    if (SecurityInformation & SACL_SECURITY_INFORMATION)
    {
1753 1754
        status = RtlGetSaclSecurityDescriptor( SecurityDescriptor, &present, &sacl, &defaulted );
        if (status != STATUS_SUCCESS) return status;
1755
        sd.sacl_len = (sacl && present) ? acl_bytesInUse(sacl) : 0;
1756 1757 1758 1759 1760
        sd.control |= SE_SACL_PRESENT;
    }

    if (SecurityInformation & DACL_SECURITY_INFORMATION)
    {
1761 1762
        status = RtlGetDaclSecurityDescriptor( SecurityDescriptor, &present, &dacl, &defaulted );
        if (status != STATUS_SUCCESS) return status;
1763
        sd.dacl_len = (dacl && present) ? acl_bytesInUse(dacl) : 0;
1764 1765 1766 1767 1768
        sd.control |= SE_DACL_PRESENT;
    }

    SERVER_START_REQ( set_security_object )
    {
1769
        req->handle = wine_server_obj_handle( Handle );
1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781
        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;
1782 1783
}

1784
/******************************************************************************
1785
 * RtlConvertSidToUnicodeString (NTDLL.@)
1786 1787 1788 1789 1790
 *
 * 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";
1791 1792
 */
NTSTATUS WINAPI RtlConvertSidToUnicodeString(
1793
       PUNICODE_STRING String,
1794
       PSID pSid,
1795
       BOOLEAN AllocateString)
1796
{
1797 1798 1799
    static const WCHAR formatW[] = {'-','%','u',0};
    WCHAR buffer[2 + 10 + 10 + 10 * SID_MAX_SUB_AUTHORITIES];
    WCHAR *p = buffer;
1800
    const SID *sid = pSid;
1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812
    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);
1813

1814 1815 1816 1817 1818 1819 1820 1821
    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;
1822

1823
    memcpy( String->Buffer, buffer, len );
1824
    return STATUS_SUCCESS;
1825
}
1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837

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

1838
    TRACE("pAcl=%p pAclInfo=%p len=%d, class=%d\n", 
1839 1840 1841 1842 1843 1844
        pAcl, pAclInformation, nAclInformationLength, dwAclInformationClass);

    switch (dwAclInformationClass)
    {
        case AclRevisionInformation:
        {
1845
            PACL_REVISION_INFORMATION paclrev = pAclInformation;
1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856

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

            break;
        }

        case AclSizeInformation:
        {
1857
            PACL_SIZE_INFORMATION paclsize = pAclInformation;
1858 1859 1860 1861 1862 1863

            if (nAclInformationLength < sizeof(ACL_SIZE_INFORMATION))
                status = STATUS_INVALID_PARAMETER;
            else
            {
                paclsize->AceCount = pAcl->AceCount;
1864
                paclsize->AclBytesInUse = acl_bytesInUse(pAcl);
1865 1866
		if (pAcl->AclSize < paclsize->AclBytesInUse)
                {
1867
                    WARN("Acl uses %d bytes, but only has %d allocated!  Returning smaller of the two values.\n", pAcl->AclSize, paclsize->AclBytesInUse);
1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884
                    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;
}
1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897

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