Make long lines behave nicely.
[wine] / dlls / advapi32 / security.c
1 /*
2  * Copyright 1999, 2000 Juergen Schmied <juergen.schmied@debitel.net>
3  * Copyright 2003 CodeWeavers Inc. (Ulrich Czekalla)
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  */
20
21 #include <stdarg.h>
22 #include <string.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winerror.h"
27 #include "rpcnterr.h"
28 #include "winreg.h"
29 #include "winternl.h"
30 #include "ntstatus.h"
31 #include "ntsecapi.h"
32 #include "accctrl.h"
33 #include "sddl.h"
34 #include "winsvc.h"
35 #include "aclapi.h"
36
37 #include "wine/debug.h"
38 #include "wine/unicode.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(advapi);
41
42 static BOOL ParseStringSidToSid(LPCWSTR StringSid, PSID pSid, LPDWORD cBytes);
43 static BOOL ParseStringAclToAcl(LPCWSTR StringAcl, LPDWORD lpdwFlags, 
44     PACL pAcl, LPDWORD cBytes);
45 static BYTE ParseAceStringFlags(LPCWSTR* StringAcl);
46 static BYTE ParseAceStringType(LPCWSTR* StringAcl);
47 static DWORD ParseAceStringRights(LPCWSTR* StringAcl);
48 static BOOL ParseStringSecurityDescriptorToSecurityDescriptor(
49     LPCWSTR StringSecurityDescriptor,
50     SECURITY_DESCRIPTOR* SecurityDescriptor,
51     LPDWORD cBytes);
52 static DWORD ParseAclStringFlags(LPCWSTR* StringAcl);
53
54 typedef struct _ACEFLAG
55 {
56    LPCWSTR wstr;
57    DWORD value;
58 } ACEFLAG, *LPACEFLAG;
59
60 /*
61  * ACE access rights
62  */
63 static const WCHAR SDDL_READ_CONTROL[]     = {'R','C',0};
64 static const WCHAR SDDL_WRITE_DAC[]        = {'W','D',0};
65 static const WCHAR SDDL_WRITE_OWNER[]      = {'W','O',0};
66 static const WCHAR SDDL_STANDARD_DELETE[]  = {'S','D',0};
67 static const WCHAR SDDL_GENERIC_ALL[]      = {'G','A',0};
68 static const WCHAR SDDL_GENERIC_READ[]     = {'G','R',0};
69 static const WCHAR SDDL_GENERIC_WRITE[]    = {'G','W',0};
70 static const WCHAR SDDL_GENERIC_EXECUTE[]  = {'G','X',0};
71
72 /*
73  * ACE types
74  */
75 static const WCHAR SDDL_ACCESS_ALLOWED[]        = {'A',0};
76 static const WCHAR SDDL_ACCESS_DENIED[]         = {'D',0};
77 static const WCHAR SDDL_OBJECT_ACCESS_ALLOWED[] = {'O','A',0};
78 static const WCHAR SDDL_OBJECT_ACCESS_DENIED[]  = {'O','D',0};
79 static const WCHAR SDDL_AUDIT[]                 = {'A','U',0};
80 static const WCHAR SDDL_ALARM[]                 = {'A','L',0};
81 static const WCHAR SDDL_OBJECT_AUDIT[]          = {'O','U',0};
82 static const WCHAR SDDL_OBJECT_ALARMp[]         = {'O','L',0};
83
84 /*
85  * ACE flags
86  */
87 static const WCHAR SDDL_CONTAINER_INHERIT[]  = {'C','I',0};
88 static const WCHAR SDDL_OBJECT_INHERIT[]     = {'O','I',0};
89 static const WCHAR SDDL_NO_PROPAGATE[]       = {'N','P',0};
90 static const WCHAR SDDL_INHERIT_ONLY[]       = {'I','O',0};
91 static const WCHAR SDDL_INHERITED[]          = {'I','D',0};
92 static const WCHAR SDDL_AUDIT_SUCCESS[]      = {'S','A',0};
93 static const WCHAR SDDL_AUDIT_FAILURE[]      = {'F','A',0};
94
95 /* set last error code from NT status and get the proper boolean return value */
96 /* used for functions that are a simple wrapper around the corresponding ntdll API */
97 static inline BOOL set_ntstatus( NTSTATUS status )
98 {
99     if (status) SetLastError( RtlNtStatusToDosError( status ));
100     return !status;
101 }
102
103 static void dumpLsaAttributes( PLSA_OBJECT_ATTRIBUTES oa )
104 {
105         if (oa)
106         {
107           TRACE("\n\tlength=%lu, rootdir=%p, objectname=%s\n\tattr=0x%08lx, sid=%p qos=%p\n",
108                 oa->Length, oa->RootDirectory,
109                 oa->ObjectName?debugstr_w(oa->ObjectName->Buffer):"null",
110                 oa->Attributes, oa->SecurityDescriptor, oa->SecurityQualityOfService);
111         }
112 }
113
114 /************************************************************
115  *                ADVAPI_IsLocalComputer
116  *
117  * Checks whether the server name indicates local machine.
118  */
119 BOOL ADVAPI_IsLocalComputer(LPCWSTR ServerName)
120 {
121     if (!ServerName)
122     {
123         return TRUE;
124     }
125     else if (!ServerName[0])
126     {
127         return TRUE;
128     }
129     else
130     {
131         DWORD dwSize = MAX_COMPUTERNAME_LENGTH + 1;
132         BOOL Result;
133         LPWSTR buf;
134
135         buf = HeapAlloc(GetProcessHeap(), 0, dwSize * sizeof(WCHAR));
136         Result = GetComputerNameW(buf,  &dwSize);
137         if (Result && (ServerName[0] == '\\') && (ServerName[1] == '\\'))
138             ServerName += 2;
139         Result = Result && !lstrcmpW(ServerName, buf);
140         HeapFree(GetProcessHeap(), 0, buf);
141
142         return Result;
143     }
144 }
145
146 #define ADVAPI_ForceLocalComputer(ServerName, FailureCode) \
147     if (!ADVAPI_IsLocalComputer(ServerName)) \
148     { \
149         FIXME("Action Implemented for local computer only. " \
150               "Requested for server %s\n", debugstr_w(ServerName)); \
151         return FailureCode; \
152     }
153
154 /*      ##############################
155         ######  TOKEN FUNCTIONS ######
156         ##############################
157 */
158
159 /******************************************************************************
160  * OpenProcessToken                     [ADVAPI32.@]
161  * Opens the access token associated with a process handle.
162  *
163  * PARAMS
164  *   ProcessHandle [I] Handle to process
165  *   DesiredAccess [I] Desired access to process
166  *   TokenHandle   [O] Pointer to handle of open access token
167  *
168  * RETURNS
169  *  Success: TRUE. TokenHandle contains the access token.
170  *  Failure: FALSE.
171  *
172  * NOTES
173  *  See NtOpenProcessToken.
174  */
175 BOOL WINAPI
176 OpenProcessToken( HANDLE ProcessHandle, DWORD DesiredAccess,
177                   HANDLE *TokenHandle )
178 {
179         return set_ntstatus(NtOpenProcessToken( ProcessHandle, DesiredAccess, TokenHandle ));
180 }
181
182 /******************************************************************************
183  * OpenThreadToken [ADVAPI32.@]
184  *
185  * Opens the access token associated with a thread handle.
186  *
187  * PARAMS
188  *   ThreadHandle  [I] Handle to process
189  *   DesiredAccess [I] Desired access to the thread
190  *   OpenAsSelf    [I] ???
191  *   TokenHandle   [O] Destination for the token handle
192  *
193  * RETURNS
194  *  Success: TRUE. TokenHandle contains the access token.
195  *  Failure: FALSE.
196  *
197  * NOTES
198  *  See NtOpenThreadToken.
199  */
200 BOOL WINAPI
201 OpenThreadToken( HANDLE ThreadHandle, DWORD DesiredAccess,
202                  BOOL OpenAsSelf, HANDLE *TokenHandle)
203 {
204         return set_ntstatus( NtOpenThreadToken(ThreadHandle, DesiredAccess, OpenAsSelf, TokenHandle));
205 }
206
207 BOOL WINAPI
208 AdjustTokenGroups( HANDLE TokenHandle, BOOL ResetToDefault, PTOKEN_GROUPS NewState,
209                    DWORD BufferLength, PTOKEN_GROUPS PreviousState, PDWORD ReturnLength )
210 {
211     return set_ntstatus( NtAdjustGroupsToken(TokenHandle, ResetToDefault, NewState, BufferLength,
212                                              PreviousState, ReturnLength));
213 }
214
215 /******************************************************************************
216  * AdjustTokenPrivileges [ADVAPI32.@]
217  *
218  * Adjust the privileges of an open token handle.
219  * 
220  * PARAMS
221  *  TokenHandle          [I]   Handle from OpenProcessToken() or OpenThreadToken() 
222  *  DisableAllPrivileges [I]   TRUE=Remove all privileges, FALSE=Use NewState
223  *  NewState             [I]   Desired new privileges of the token
224  *  BufferLength         [I]   Length of NewState
225  *  PreviousState        [O]   Destination for the previous state
226  *  ReturnLength         [I/O] Size of PreviousState
227  *
228  *
229  * RETURNS
230  *  Success: TRUE. Privileges are set to NewState and PreviousState is updated.
231  *  Failure: FALSE.
232  *
233  * NOTES
234  *  See NtAdjustPrivilegesToken.
235  */
236 BOOL WINAPI
237 AdjustTokenPrivileges( HANDLE TokenHandle, BOOL DisableAllPrivileges,
238                        LPVOID NewState, DWORD BufferLength,
239                        LPVOID PreviousState, LPDWORD ReturnLength )
240 {
241         return set_ntstatus( NtAdjustPrivilegesToken(TokenHandle, DisableAllPrivileges,
242                                                      NewState, BufferLength, PreviousState,
243                                                      ReturnLength));
244 }
245
246 /******************************************************************************
247  * CheckTokenMembership [ADVAPI32.@]
248  *
249  * Determine if an access token is a member of a SID.
250  * 
251  * PARAMS
252  *   TokenHandle [I] Handle from OpenProcessToken() or OpenThreadToken()
253  *   SidToCheck  [I] SID that possibly contains the token
254  *   IsMember    [O] Destination for result.
255  *
256  * RETURNS
257  *  Success: TRUE. IsMember is TRUE if TokenHandle is a member, FALSE otherwise.
258  *  Failure: FALSE.
259  */
260 BOOL WINAPI
261 CheckTokenMembership( HANDLE TokenHandle, PSID SidToCheck,
262                       PBOOL IsMember )
263 {
264   FIXME("(%p %p %p) stub!\n", TokenHandle, SidToCheck, IsMember);
265
266   *IsMember = TRUE;
267   return(TRUE);
268 }
269
270 /******************************************************************************
271  * GetTokenInformation [ADVAPI32.@]
272  *
273  * PARAMS
274  *   token           [I] Handle from OpenProcessToken() or OpenThreadToken()
275  *   tokeninfoclass  [I] A TOKEN_INFORMATION_CLASS from "winnt.h"
276  *   tokeninfo       [O] Destination for token information
277  *   tokeninfolength [I] Length of tokeninfo
278  *   retlen          [O] Destination for returned token information length
279  *
280  * RETURNS
281  *  Success: TRUE. tokeninfo contains retlen bytes of token information
282  *  Failure: FALSE.
283  *
284  * NOTES
285  *  See NtQueryInformationToken.
286  */
287 BOOL WINAPI
288 GetTokenInformation( HANDLE token, TOKEN_INFORMATION_CLASS tokeninfoclass,
289                      LPVOID tokeninfo, DWORD tokeninfolength, LPDWORD retlen )
290 {
291     TRACE("(%p, %s, %p, %ld, %p): \n",
292           token,
293           (tokeninfoclass == TokenUser) ? "TokenUser" :
294           (tokeninfoclass == TokenGroups) ? "TokenGroups" :
295           (tokeninfoclass == TokenPrivileges) ? "TokenPrivileges" :
296           (tokeninfoclass == TokenOwner) ? "TokenOwner" :
297           (tokeninfoclass == TokenPrimaryGroup) ? "TokenPrimaryGroup" :
298           (tokeninfoclass == TokenDefaultDacl) ? "TokenDefaultDacl" :
299           (tokeninfoclass == TokenSource) ? "TokenSource" :
300           (tokeninfoclass == TokenType) ? "TokenType" :
301           (tokeninfoclass == TokenImpersonationLevel) ? "TokenImpersonationLevel" :
302           (tokeninfoclass == TokenStatistics) ? "TokenStatistics" :
303           (tokeninfoclass == TokenRestrictedSids) ? "TokenRestrictedSids" :
304           (tokeninfoclass == TokenSessionId) ? "TokenSessionId" :
305           (tokeninfoclass == TokenGroupsAndPrivileges) ? "TokenGroupsAndPrivileges" :
306           (tokeninfoclass == TokenSessionReference) ? "TokenSessionReference" :
307           (tokeninfoclass == TokenSandBoxInert) ? "TokenSandBoxInert" :
308           "Unknown",
309           tokeninfo, tokeninfolength, retlen);
310     return set_ntstatus( NtQueryInformationToken( token, tokeninfoclass, tokeninfo,
311                                                   tokeninfolength, retlen));
312 }
313
314 /******************************************************************************
315  * SetTokenInformation [ADVAPI32.@]
316  *
317  * Set information for an access token.
318  *
319  * PARAMS
320  *   token           [I] Handle from OpenProcessToken() or OpenThreadToken()
321  *   tokeninfoclass  [I] A TOKEN_INFORMATION_CLASS from "winnt.h"
322  *   tokeninfo       [I] Token information to set
323  *   tokeninfolength [I] Length of tokeninfo
324  *
325  * RETURNS
326  *  Success: TRUE. The information for the token is set to tokeninfo.
327  *  Failure: FALSE.
328  */
329 BOOL WINAPI
330 SetTokenInformation( HANDLE token, TOKEN_INFORMATION_CLASS tokeninfoclass,
331                      LPVOID tokeninfo, DWORD tokeninfolength )
332 {
333     TRACE("(%p, %s, %p, %ld): stub\n",
334           token,
335           (tokeninfoclass == TokenUser) ? "TokenUser" :
336           (tokeninfoclass == TokenGroups) ? "TokenGroups" :
337           (tokeninfoclass == TokenPrivileges) ? "TokenPrivileges" :
338           (tokeninfoclass == TokenOwner) ? "TokenOwner" :
339           (tokeninfoclass == TokenPrimaryGroup) ? "TokenPrimaryGroup" :
340           (tokeninfoclass == TokenDefaultDacl) ? "TokenDefaultDacl" :
341           (tokeninfoclass == TokenSource) ? "TokenSource" :
342           (tokeninfoclass == TokenType) ? "TokenType" :
343           (tokeninfoclass == TokenImpersonationLevel) ? "TokenImpersonationLevel" :
344           (tokeninfoclass == TokenStatistics) ? "TokenStatistics" :
345           (tokeninfoclass == TokenRestrictedSids) ? "TokenRestrictedSids" :
346           (tokeninfoclass == TokenSessionId) ? "TokenSessionId" :
347           (tokeninfoclass == TokenGroupsAndPrivileges) ? "TokenGroupsAndPrivileges" :
348           (tokeninfoclass == TokenSessionReference) ? "TokenSessionReference" :
349           (tokeninfoclass == TokenSandBoxInert) ? "TokenSandBoxInert" :
350           "Unknown",
351           tokeninfo, tokeninfolength);
352
353     return set_ntstatus( NtSetInformationToken( token, tokeninfoclass, tokeninfo, tokeninfolength ));
354 }
355
356 /*************************************************************************
357  * SetThreadToken [ADVAPI32.@]
358  *
359  * Assigns an 'impersonation token' to a thread so it can assume the
360  * security privileges of another thread or process.  Can also remove
361  * a previously assigned token. 
362  *
363  * PARAMS
364  *   thread          [O] Handle to thread to set the token for
365  *   token           [I] Token to set
366  *
367  * RETURNS
368  *  Success: TRUE. The threads access token is set to token
369  *  Failure: FALSE.
370  *
371  * NOTES
372  *  Only supported on NT or higher. On Win9X this function does nothing.
373  *  See SetTokenInformation.
374  */
375 BOOL WINAPI SetThreadToken(PHANDLE thread, HANDLE token)
376 {
377     return set_ntstatus( NtSetInformationThread( thread ? *thread : GetCurrentThread(),
378                                                  ThreadImpersonationToken, &token, sizeof token ));
379 }
380
381 /*      ##############################
382         ######  SID FUNCTIONS   ######
383         ##############################
384 */
385
386 /******************************************************************************
387  * AllocateAndInitializeSid [ADVAPI32.@]
388  *
389  * PARAMS
390  *   pIdentifierAuthority []
391  *   nSubAuthorityCount   []
392  *   nSubAuthority0       []
393  *   nSubAuthority1       []
394  *   nSubAuthority2       []
395  *   nSubAuthority3       []
396  *   nSubAuthority4       []
397  *   nSubAuthority5       []
398  *   nSubAuthority6       []
399  *   nSubAuthority7       []
400  *   pSid                 []
401  */
402 BOOL WINAPI
403 AllocateAndInitializeSid( PSID_IDENTIFIER_AUTHORITY pIdentifierAuthority,
404                           BYTE nSubAuthorityCount,
405                           DWORD nSubAuthority0, DWORD nSubAuthority1,
406                           DWORD nSubAuthority2, DWORD nSubAuthority3,
407                           DWORD nSubAuthority4, DWORD nSubAuthority5,
408                           DWORD nSubAuthority6, DWORD nSubAuthority7,
409                           PSID *pSid )
410 {
411     return set_ntstatus( RtlAllocateAndInitializeSid(
412                              pIdentifierAuthority, nSubAuthorityCount,
413                              nSubAuthority0, nSubAuthority1, nSubAuthority2, nSubAuthority3,
414                              nSubAuthority4, nSubAuthority5, nSubAuthority6, nSubAuthority7,
415                              pSid ));
416 }
417
418 /******************************************************************************
419  * FreeSid [ADVAPI32.@]
420  *
421  * PARAMS
422  *   pSid []
423  */
424 PVOID WINAPI
425 FreeSid( PSID pSid )
426 {
427         RtlFreeSid(pSid);
428         return NULL; /* is documented like this */
429 }
430
431 /******************************************************************************
432  * CopySid [ADVAPI32.@]
433  *
434  * PARAMS
435  *   nDestinationSidLength []
436  *   pDestinationSid       []
437  *   pSourceSid            []
438  */
439 BOOL WINAPI
440 CopySid( DWORD nDestinationSidLength, PSID pDestinationSid, PSID pSourceSid )
441 {
442         return RtlCopySid(nDestinationSidLength, pDestinationSid, pSourceSid);
443 }
444
445 BOOL WINAPI
446 IsTokenRestricted( HANDLE TokenHandle )
447 {
448     FIXME("%p - stub\n", TokenHandle);
449
450     return FALSE;
451 }
452
453 /******************************************************************************
454  * IsValidSid [ADVAPI32.@]
455  *
456  * PARAMS
457  *   pSid []
458  */
459 BOOL WINAPI
460 IsValidSid( PSID pSid )
461 {
462         return RtlValidSid( pSid );
463 }
464
465 /******************************************************************************
466  * EqualSid [ADVAPI32.@]
467  *
468  * PARAMS
469  *   pSid1 []
470  *   pSid2 []
471  */
472 BOOL WINAPI
473 EqualSid( PSID pSid1, PSID pSid2 )
474 {
475         return RtlEqualSid( pSid1, pSid2 );
476 }
477
478 /******************************************************************************
479  * EqualPrefixSid [ADVAPI32.@]
480  */
481 BOOL WINAPI EqualPrefixSid (PSID pSid1, PSID pSid2)
482 {
483         return RtlEqualPrefixSid(pSid1, pSid2);
484 }
485
486 /******************************************************************************
487  * GetSidLengthRequired [ADVAPI32.@]
488  *
489  * PARAMS
490  *   nSubAuthorityCount []
491  */
492 DWORD WINAPI
493 GetSidLengthRequired( BYTE nSubAuthorityCount )
494 {
495         return RtlLengthRequiredSid(nSubAuthorityCount);
496 }
497
498 /******************************************************************************
499  * InitializeSid [ADVAPI32.@]
500  *
501  * PARAMS
502  *   pIdentifierAuthority []
503  */
504 BOOL WINAPI
505 InitializeSid (
506         PSID pSid,
507         PSID_IDENTIFIER_AUTHORITY pIdentifierAuthority,
508         BYTE nSubAuthorityCount)
509 {
510         return RtlInitializeSid(pSid, pIdentifierAuthority, nSubAuthorityCount);
511 }
512
513 DWORD WINAPI
514 GetEffectiveRightsFromAclA( PACL pacl, PTRUSTEEA pTrustee, PACCESS_MASK pAccessRights )
515 {
516     FIXME("%p %p %p - stub\n", pacl, pTrustee, pAccessRights);
517
518     return 1;
519 }
520
521 DWORD WINAPI
522 GetEffectiveRightsFromAclW( PACL pacl, PTRUSTEEW pTrustee, PACCESS_MASK pAccessRights )
523 {
524     FIXME("%p %p %p - stub\n", pacl, pTrustee, pAccessRights);
525
526     return 1;
527 }
528
529 /******************************************************************************
530  * GetSidIdentifierAuthority [ADVAPI32.@]
531  *
532  * PARAMS
533  *   pSid []
534  */
535 PSID_IDENTIFIER_AUTHORITY WINAPI
536 GetSidIdentifierAuthority( PSID pSid )
537 {
538         return RtlIdentifierAuthoritySid(pSid);
539 }
540
541 /******************************************************************************
542  * GetSidSubAuthority [ADVAPI32.@]
543  *
544  * PARAMS
545  *   pSid          []
546  *   nSubAuthority []
547  */
548 PDWORD WINAPI
549 GetSidSubAuthority( PSID pSid, DWORD nSubAuthority )
550 {
551         return RtlSubAuthoritySid(pSid, nSubAuthority);
552 }
553
554 /******************************************************************************
555  * GetSidSubAuthorityCount [ADVAPI32.@]
556  *
557  * PARAMS
558  *   pSid []
559  */
560 PUCHAR WINAPI
561 GetSidSubAuthorityCount (PSID pSid)
562 {
563         return RtlSubAuthorityCountSid(pSid);
564 }
565
566 /******************************************************************************
567  * GetLengthSid [ADVAPI32.@]
568  *
569  * PARAMS
570  *   pSid []
571  */
572 DWORD WINAPI
573 GetLengthSid (PSID pSid)
574 {
575         return RtlLengthSid(pSid);
576 }
577
578 /*      ##############################################
579         ######  SECURITY DESCRIPTOR FUNCTIONS   ######
580         ##############################################
581 */
582
583 /******************************************************************************
584  * InitializeSecurityDescriptor [ADVAPI32.@]
585  *
586  * PARAMS
587  *   pDescr   []
588  *   revision []
589  */
590 BOOL WINAPI
591 InitializeSecurityDescriptor( PSECURITY_DESCRIPTOR pDescr, DWORD revision )
592 {
593         return set_ntstatus( RtlCreateSecurityDescriptor(pDescr, revision ));
594 }
595
596
597 /******************************************************************************
598  * MakeAbsoluteSD [ADVAPI32.@]
599  */
600 BOOL WINAPI MakeAbsoluteSD (
601         IN PSECURITY_DESCRIPTOR pSelfRelativeSecurityDescriptor,
602         OUT PSECURITY_DESCRIPTOR pAbsoluteSecurityDescriptor,
603         OUT LPDWORD lpdwAbsoluteSecurityDescriptorSize,
604         OUT PACL pDacl,
605         OUT LPDWORD lpdwDaclSize,
606         OUT PACL pSacl,
607         OUT LPDWORD lpdwSaclSize,
608         OUT PSID pOwner,
609         OUT LPDWORD lpdwOwnerSize,
610         OUT PSID pPrimaryGroup,
611         OUT LPDWORD lpdwPrimaryGroupSize)
612 {
613     return set_ntstatus( RtlSelfRelativeToAbsoluteSD(pSelfRelativeSecurityDescriptor,
614                                                      pAbsoluteSecurityDescriptor,
615                                                      lpdwAbsoluteSecurityDescriptorSize,
616                                                      pDacl, lpdwDaclSize, pSacl, lpdwSaclSize,
617                                                      pOwner, lpdwOwnerSize,
618                                                      pPrimaryGroup, lpdwPrimaryGroupSize));
619 }
620
621 BOOL WINAPI GetKernelObjectSecurity(
622         HANDLE Handle,
623         SECURITY_INFORMATION RequestedInformation,
624         PSECURITY_DESCRIPTOR pSecurityDescriptor,
625         DWORD nLength,
626         LPDWORD lpnLengthNeeded )
627 {
628     FIXME("%p 0x%08lx %p 0x%08lx %p - stub\n", Handle, RequestedInformation,
629           pSecurityDescriptor, nLength, lpnLengthNeeded);
630
631     return FALSE;
632 }
633
634 BOOL WINAPI GetPrivateObjectSecurity(
635         PSECURITY_DESCRIPTOR ObjectDescriptor,
636         SECURITY_INFORMATION SecurityInformation,
637         PSECURITY_DESCRIPTOR ResultantDescriptor,
638         DWORD DescriptorLength,
639         PDWORD ReturnLength )
640 {
641     FIXME("%p 0x%08lx %p 0x%08lx %p - stub\n", ObjectDescriptor, SecurityInformation,
642           ResultantDescriptor, DescriptorLength, ReturnLength);
643
644     return FALSE;
645 }
646
647 /******************************************************************************
648  * GetSecurityDescriptorLength [ADVAPI32.@]
649  */
650 DWORD WINAPI GetSecurityDescriptorLength( PSECURITY_DESCRIPTOR pDescr)
651 {
652         return RtlLengthSecurityDescriptor(pDescr);
653 }
654
655 /******************************************************************************
656  * GetSecurityDescriptorOwner [ADVAPI32.@]
657  *
658  * PARAMS
659  *   pOwner            []
660  *   lpbOwnerDefaulted []
661  */
662 BOOL WINAPI
663 GetSecurityDescriptorOwner( PSECURITY_DESCRIPTOR pDescr, PSID *pOwner,
664                             LPBOOL lpbOwnerDefaulted )
665 {
666     BOOLEAN defaulted;
667     BOOL ret = set_ntstatus( RtlGetOwnerSecurityDescriptor( pDescr, pOwner, &defaulted ));
668     *lpbOwnerDefaulted = defaulted;
669     return ret;
670 }
671
672 /******************************************************************************
673  * SetSecurityDescriptorOwner [ADVAPI32.@]
674  *
675  * PARAMS
676  */
677 BOOL WINAPI SetSecurityDescriptorOwner( PSECURITY_DESCRIPTOR pSecurityDescriptor,
678                                    PSID pOwner, BOOL bOwnerDefaulted)
679 {
680     return set_ntstatus( RtlSetOwnerSecurityDescriptor(pSecurityDescriptor, pOwner, bOwnerDefaulted));
681 }
682 /******************************************************************************
683  * GetSecurityDescriptorGroup                   [ADVAPI32.@]
684  */
685 BOOL WINAPI GetSecurityDescriptorGroup(
686         PSECURITY_DESCRIPTOR SecurityDescriptor,
687         PSID *Group,
688         LPBOOL GroupDefaulted)
689 {
690     BOOLEAN defaulted;
691     BOOL ret = set_ntstatus( RtlGetGroupSecurityDescriptor(SecurityDescriptor, Group, &defaulted ));
692     *GroupDefaulted = defaulted;
693     return ret;
694 }
695 /******************************************************************************
696  * SetSecurityDescriptorGroup [ADVAPI32.@]
697  */
698 BOOL WINAPI SetSecurityDescriptorGroup ( PSECURITY_DESCRIPTOR SecurityDescriptor,
699                                            PSID Group, BOOL GroupDefaulted)
700 {
701     return set_ntstatus( RtlSetGroupSecurityDescriptor( SecurityDescriptor, Group, GroupDefaulted));
702 }
703
704 /******************************************************************************
705  * IsValidSecurityDescriptor [ADVAPI32.@]
706  *
707  * PARAMS
708  *   lpsecdesc []
709  */
710 BOOL WINAPI
711 IsValidSecurityDescriptor( PSECURITY_DESCRIPTOR SecurityDescriptor )
712 {
713     return set_ntstatus( RtlValidSecurityDescriptor(SecurityDescriptor));
714 }
715
716 /******************************************************************************
717  *  GetSecurityDescriptorDacl                   [ADVAPI32.@]
718  */
719 BOOL WINAPI GetSecurityDescriptorDacl(
720         IN PSECURITY_DESCRIPTOR pSecurityDescriptor,
721         OUT LPBOOL lpbDaclPresent,
722         OUT PACL *pDacl,
723         OUT LPBOOL lpbDaclDefaulted)
724 {
725     BOOLEAN present, defaulted;
726     BOOL ret = set_ntstatus( RtlGetDaclSecurityDescriptor(pSecurityDescriptor, &present, pDacl, &defaulted));
727     *lpbDaclPresent = present;
728     *lpbDaclDefaulted = defaulted;
729     return ret;
730 }
731
732 /******************************************************************************
733  *  SetSecurityDescriptorDacl                   [ADVAPI32.@]
734  */
735 BOOL WINAPI
736 SetSecurityDescriptorDacl (
737         PSECURITY_DESCRIPTOR lpsd,
738         BOOL daclpresent,
739         PACL dacl,
740         BOOL dacldefaulted )
741 {
742     return set_ntstatus( RtlSetDaclSecurityDescriptor (lpsd, daclpresent, dacl, dacldefaulted ) );
743 }
744 /******************************************************************************
745  *  GetSecurityDescriptorSacl                   [ADVAPI32.@]
746  */
747 BOOL WINAPI GetSecurityDescriptorSacl(
748         IN PSECURITY_DESCRIPTOR lpsd,
749         OUT LPBOOL lpbSaclPresent,
750         OUT PACL *pSacl,
751         OUT LPBOOL lpbSaclDefaulted)
752 {
753     BOOLEAN present, defaulted;
754     BOOL ret = set_ntstatus( RtlGetSaclSecurityDescriptor(lpsd, &present, pSacl, &defaulted) );
755     *lpbSaclPresent = present;
756     *lpbSaclDefaulted = defaulted;
757     return ret;
758 }
759
760 /**************************************************************************
761  * SetSecurityDescriptorSacl                    [ADVAPI32.@]
762  */
763 BOOL WINAPI SetSecurityDescriptorSacl (
764         PSECURITY_DESCRIPTOR lpsd,
765         BOOL saclpresent,
766         PACL lpsacl,
767         BOOL sacldefaulted)
768 {
769     return set_ntstatus (RtlSetSaclSecurityDescriptor(lpsd, saclpresent, lpsacl, sacldefaulted));
770 }
771 /******************************************************************************
772  * MakeSelfRelativeSD [ADVAPI32.@]
773  *
774  * PARAMS
775  *   lpabssecdesc  []
776  *   lpselfsecdesc []
777  *   lpbuflen      []
778  */
779 BOOL WINAPI
780 MakeSelfRelativeSD(
781         IN PSECURITY_DESCRIPTOR pAbsoluteSecurityDescriptor,
782         IN PSECURITY_DESCRIPTOR pSelfRelativeSecurityDescriptor,
783         IN OUT LPDWORD lpdwBufferLength)
784 {
785     return set_ntstatus( RtlMakeSelfRelativeSD( pAbsoluteSecurityDescriptor,
786                                                 pSelfRelativeSecurityDescriptor, lpdwBufferLength));
787 }
788
789 /******************************************************************************
790  * GetSecurityDescriptorControl                 [ADVAPI32.@]
791  */
792
793 BOOL WINAPI GetSecurityDescriptorControl ( PSECURITY_DESCRIPTOR  pSecurityDescriptor,
794                  PSECURITY_DESCRIPTOR_CONTROL pControl, LPDWORD lpdwRevision)
795 {
796     return set_ntstatus( RtlGetControlSecurityDescriptor(pSecurityDescriptor,pControl,lpdwRevision));
797 }
798
799 /*      ##############################
800         ######  ACL FUNCTIONS   ######
801         ##############################
802 */
803
804 /*************************************************************************
805  * InitializeAcl [ADVAPI32.@]
806  */
807 BOOL WINAPI InitializeAcl(PACL acl, DWORD size, DWORD rev)
808 {
809     return set_ntstatus( RtlCreateAcl(acl, size, rev));
810 }
811
812 BOOL WINAPI ImpersonateNamedPipeClient( HANDLE hNamedPipe )
813 {
814     FIXME("%p - stub\n", hNamedPipe);
815
816     return FALSE;
817 }
818
819 /******************************************************************************
820  *  AddAccessAllowedAce [ADVAPI32.@]
821  */
822 BOOL WINAPI AddAccessAllowedAce(
823         IN OUT PACL pAcl,
824         IN DWORD dwAceRevision,
825         IN DWORD AccessMask,
826         IN PSID pSid)
827 {
828     return set_ntstatus(RtlAddAccessAllowedAce(pAcl, dwAceRevision, AccessMask, pSid));
829 }
830
831 /******************************************************************************
832  *  AddAccessAllowedAceEx [ADVAPI32.@]
833  */
834 BOOL WINAPI AddAccessAllowedAceEx(
835         IN OUT PACL pAcl,
836         IN DWORD dwAceRevision,
837         IN DWORD AceFlags,
838         IN DWORD AccessMask,
839         IN PSID pSid)
840 {
841     return set_ntstatus(RtlAddAccessAllowedAceEx(pAcl, dwAceRevision, AceFlags, AccessMask, pSid));
842 }
843
844 /******************************************************************************
845  *  AddAccessDeniedAce [ADVAPI32.@]
846  */
847 BOOL WINAPI AddAccessDeniedAce(
848         IN OUT PACL pAcl,
849         IN DWORD dwAceRevision,
850         IN DWORD AccessMask,
851         IN PSID pSid)
852 {
853     return set_ntstatus(RtlAddAccessDeniedAce(pAcl, dwAceRevision, AccessMask, pSid));
854 }
855
856 /******************************************************************************
857  *  AddAccessDeniedAceEx [ADVAPI32.@]
858  */
859 BOOL WINAPI AddAccessDeniedAceEx(
860         IN OUT PACL pAcl,
861         IN DWORD dwAceRevision,
862         IN DWORD AceFlags,
863         IN DWORD AccessMask,
864         IN PSID pSid)
865 {
866     return set_ntstatus(RtlAddAccessDeniedAceEx(pAcl, dwAceRevision, AceFlags, AccessMask, pSid));
867 }
868
869 /******************************************************************************
870  *  AddAce [ADVAPI32.@]
871  */
872 BOOL WINAPI AddAce(
873         IN OUT PACL pAcl,
874         IN DWORD dwAceRevision,
875         IN DWORD dwStartingAceIndex,
876         LPVOID pAceList,
877         DWORD nAceListLength)
878 {
879     return set_ntstatus(RtlAddAce(pAcl, dwAceRevision, dwStartingAceIndex, pAceList, nAceListLength));
880 }
881
882 /******************************************************************************
883  * DeleteAce [ADVAPI32.@]
884  */
885 BOOL WINAPI DeleteAce(PACL pAcl, DWORD dwAceIndex)
886 {
887     return set_ntstatus(RtlDeleteAce(pAcl, dwAceIndex));
888 }
889
890 /******************************************************************************
891  *  FindFirstFreeAce [ADVAPI32.@]
892  */
893 BOOL WINAPI FindFirstFreeAce(IN PACL pAcl, LPVOID * pAce)
894 {
895         return RtlFirstFreeAce(pAcl, (PACE_HEADER *)pAce);
896 }
897
898 /******************************************************************************
899  * GetAce [ADVAPI32.@]
900  */
901 BOOL WINAPI GetAce(PACL pAcl,DWORD dwAceIndex,LPVOID *pAce )
902 {
903     return set_ntstatus(RtlGetAce(pAcl, dwAceIndex, pAce));
904 }
905
906 /******************************************************************************
907  * GetAclInformation [ADVAPI32.@]
908  */
909 BOOL WINAPI GetAclInformation(
910   PACL pAcl,
911   LPVOID pAclInformation,
912   DWORD nAclInformationLength,
913   ACL_INFORMATION_CLASS dwAclInformationClass)
914 {
915     return set_ntstatus(RtlQueryInformationAcl(pAcl, pAclInformation,
916                                                nAclInformationLength, dwAclInformationClass));
917 }
918
919 /******************************************************************************
920  *  IsValidAcl [ADVAPI32.@]
921  */
922 BOOL WINAPI IsValidAcl(IN PACL pAcl)
923 {
924         return RtlValidAcl(pAcl);
925 }
926
927 /*      ##############################
928         ######  MISC FUNCTIONS  ######
929         ##############################
930 */
931
932 /******************************************************************************
933  * AllocateLocallyUniqueId [ADVAPI32.@]
934  *
935  * PARAMS
936  *   lpLuid []
937  */
938 BOOL WINAPI AllocateLocallyUniqueId( PLUID lpLuid )
939 {
940     return set_ntstatus(NtAllocateLocallyUniqueId(lpLuid));
941 }
942
943 static const WCHAR SE_CREATE_TOKEN_NAME_W[] =
944  { 'S','e','C','r','e','a','t','e','T','o','k','e','n','P','r','i','v','i','l','e','g','e',0 };
945 static const WCHAR SE_ASSIGNPRIMARYTOKEN_NAME_W[] =
946  { 'S','e','A','s','s','i','g','n','P','r','i','m','a','r','y','T','o','k','e','n','P','r','i','v','i','l','e','g','e',0 };
947 static const WCHAR SE_LOCK_MEMORY_NAME_W[] =
948  { 'S','e','L','o','c','k','M','e','m','o','r','y','P','r','i','v','i','l','e','g','e',0 };
949 static const WCHAR SE_INCREASE_QUOTA_NAME_W[] =
950  { 'S','e','I','n','c','r','e','a','s','e','Q','u','o','t','a','P','r','i','v','i','l','e','g','e',0 };
951 static const WCHAR SE_MACHINE_ACCOUNT_NAME_W[] =
952  { 'S','e','M','a','c','h','i','n','e','A','c','c','o','u','n','t','P','r','i','v','i','l','e','g','e',0 };
953 static const WCHAR SE_TCB_NAME_W[] =
954  { 'S','e','T','c','b','P','r','i','v','i','l','e','g','e',0 };
955 static const WCHAR SE_SECURITY_NAME_W[] =
956  { 'S','e','S','e','c','u','r','i','t','y','P','r','i','v','i','l','e','g','e',0 };
957 static const WCHAR SE_TAKE_OWNERSHIP_NAME_W[] =
958  { 'S','e','T','a','k','e','O','w','n','e','r','s','h','i','p','P','r','i','v','i','l','e','g','e',0 };
959 static const WCHAR SE_LOAD_DRIVER_NAME_W[] =
960  { 'S','e','L','o','a','d','D','r','i','v','e','r','P','r','i','v','i','l','e','g','e',0 };
961 static const WCHAR SE_SYSTEM_PROFILE_NAME_W[] =
962  { 'S','e','S','y','s','t','e','m','P','r','o','f','i','l','e','P','r','i','v','i','l','e','g','e',0 };
963 static const WCHAR SE_SYSTEMTIME_NAME_W[] =
964  { 'S','e','S','y','s','t','e','m','t','i','m','e','P','r','i','v','i','l','e','g','e',0 };
965 static const WCHAR SE_PROF_SINGLE_PROCESS_NAME_W[] =
966  { 'S','e','P','r','o','f','i','l','e','S','i','n','g','l','e','P','r','o','c','e','s','s','P','r','i','v','i','l','e','g','e',0 };
967 static const WCHAR SE_INC_BASE_PRIORITY_NAME_W[] =
968  { 'S','e','I','n','c','r','e','a','s','e','B','a','s','e','P','r','i','o','r','i','t','y','P','r','i','v','i','l','e','g','e',0 };
969 static const WCHAR SE_CREATE_PAGEFILE_NAME_W[] =
970  { 'S','e','C','r','e','a','t','e','P','a','g','e','f','i','l','e','P','r','i','v','i','l','e','g','e',0 };
971 static const WCHAR SE_CREATE_PERMANENT_NAME_W[] =
972  { 'S','e','C','r','e','a','t','e','P','e','r','m','a','n','e','n','t','P','r','i','v','i','l','e','g','e',0 };
973 static const WCHAR SE_BACKUP_NAME_W[] =
974  { 'S','e','B','a','c','k','u','p','P','r','i','v','i','l','e','g','e',0 };
975 static const WCHAR SE_RESTORE_NAME_W[] =
976  { 'S','e','R','e','s','t','o','r','e','P','r','i','v','i','l','e','g','e',0 };
977 static const WCHAR SE_SHUTDOWN_NAME_W[] =
978  { 'S','e','S','h','u','t','d','o','w','n','P','r','i','v','i','l','e','g','e',0 };
979 static const WCHAR SE_DEBUG_NAME_W[] =
980  { 'S','e','D','e','b','u','g','P','r','i','v','i','l','e','g','e',0 };
981 static const WCHAR SE_AUDIT_NAME_W[] =
982  { 'S','e','A','u','d','i','t','P','r','i','v','i','l','e','g','e',0 };
983 static const WCHAR SE_SYSTEM_ENVIRONMENT_NAME_W[] =
984  { 'S','e','S','y','s','t','e','m','E','n','v','i','r','o','n','m','e','n','t','P','r','i','v','i','l','e','g','e',0 };
985 static const WCHAR SE_CHANGE_NOTIFY_NAME_W[] =
986  { 'S','e','C','h','a','n','g','e','N','o','t','i','f','y','P','r','i','v','i','l','e','g','e',0 };
987 static const WCHAR SE_REMOTE_SHUTDOWN_NAME_W[] =
988  { 'S','e','R','e','m','o','t','e','S','h','u','t','d','o','w','n','P','r','i','v','i','l','e','g','e',0 };
989 static const WCHAR SE_UNDOCK_NAME_W[] =
990  { 'S','e','U','n','d','o','c','k','P','r','i','v','i','l','e','g','e',0 };
991 static const WCHAR SE_SYNC_AGENT_NAME_W[] =
992  { 'S','e','S','y','n','c','A','g','e','n','t','P','r','i','v','i','l','e','g','e',0 };
993 static const WCHAR SE_ENABLE_DELEGATION_NAME_W[] =
994  { 'S','e','E','n','a','b','l','e','D','e','l','e','g','a','t','i','o','n','P','r','i','v','i','l','e','g','e',0 };
995 static const WCHAR SE_MANAGE_VOLUME_NAME_W[] =
996  { 'S','e','M','a','n','a','g','e','V','o','l','u','m','e','P','r','i','v','i','l','e','g','e',0 };
997 static const WCHAR SE_IMPERSONATE_NAME_W[] =
998  { 'S','e','I','m','p','e','r','s','o','n','a','t','e','P','r','i','v','i','l','e','g','e',0 };
999 static const WCHAR SE_CREATE_GLOBAL_NAME_W[] =
1000  { 'S','e','C','r','e','a','t','e','G','l','o','b','a','l','P','r','i','v','i','l','e','g','e',0 };
1001
1002 static const WCHAR * const WellKnownPrivNames[SE_MAX_WELL_KNOWN_PRIVILEGE + 1] =
1003 {
1004     NULL,
1005     NULL,
1006     SE_CREATE_TOKEN_NAME_W,
1007     SE_ASSIGNPRIMARYTOKEN_NAME_W,
1008     SE_LOCK_MEMORY_NAME_W,
1009     SE_INCREASE_QUOTA_NAME_W,
1010     SE_MACHINE_ACCOUNT_NAME_W,
1011     SE_TCB_NAME_W,
1012     SE_SECURITY_NAME_W,
1013     SE_TAKE_OWNERSHIP_NAME_W,
1014     SE_LOAD_DRIVER_NAME_W,
1015     SE_SYSTEM_PROFILE_NAME_W,
1016     SE_SYSTEMTIME_NAME_W,
1017     SE_PROF_SINGLE_PROCESS_NAME_W,
1018     SE_INC_BASE_PRIORITY_NAME_W,
1019     SE_CREATE_PAGEFILE_NAME_W,
1020     SE_CREATE_PERMANENT_NAME_W,
1021     SE_BACKUP_NAME_W,
1022     SE_RESTORE_NAME_W,
1023     SE_SHUTDOWN_NAME_W,
1024     SE_DEBUG_NAME_W,
1025     SE_AUDIT_NAME_W,
1026     SE_SYSTEM_ENVIRONMENT_NAME_W,
1027     SE_CHANGE_NOTIFY_NAME_W,
1028     SE_REMOTE_SHUTDOWN_NAME_W,
1029     SE_UNDOCK_NAME_W,
1030     SE_SYNC_AGENT_NAME_W,
1031     SE_ENABLE_DELEGATION_NAME_W,
1032     SE_MANAGE_VOLUME_NAME_W,
1033     SE_IMPERSONATE_NAME_W,
1034     SE_CREATE_GLOBAL_NAME_W,
1035 };
1036
1037 /******************************************************************************
1038  * LookupPrivilegeValueW                        [ADVAPI32.@]
1039  *
1040  * See LookupPrivilegeValueA.
1041  */
1042 BOOL WINAPI
1043 LookupPrivilegeValueW( LPCWSTR lpSystemName, LPCWSTR lpName, PLUID lpLuid )
1044 {
1045     UINT i;
1046
1047     TRACE("%s,%s,%p\n",debugstr_w(lpSystemName), debugstr_w(lpName), lpLuid);
1048
1049     if (!ADVAPI_IsLocalComputer(lpSystemName))
1050     {
1051         SetLastError(RPC_S_SERVER_UNAVAILABLE);
1052         return FALSE;
1053     }
1054     if (!lpName)
1055     {
1056         SetLastError(ERROR_NO_SUCH_PRIVILEGE);
1057         return FALSE;
1058     }
1059     for( i=SE_MIN_WELL_KNOWN_PRIVILEGE; i<SE_MAX_WELL_KNOWN_PRIVILEGE; i++ )
1060     {
1061         if( !WellKnownPrivNames[i] )
1062             continue;
1063         if( strcmpiW( WellKnownPrivNames[i], lpName) )
1064             continue;
1065         lpLuid->LowPart = i;
1066         lpLuid->HighPart = 0;
1067         TRACE( "%s -> %08lx-%08lx\n",debugstr_w( lpSystemName ),
1068                lpLuid->HighPart, lpLuid->LowPart );
1069         return TRUE;
1070     }
1071     SetLastError(ERROR_NO_SUCH_PRIVILEGE);
1072     return FALSE;
1073 }
1074
1075 /******************************************************************************
1076  * LookupPrivilegeValueA                        [ADVAPI32.@]
1077  *
1078  * Retrieves LUID used on a system to represent the privilege name.
1079  *
1080  * PARAMS
1081  *  lpSystemName [I] Name of the system
1082  *  lpName       [I] Name of the privilege
1083  *  lpLuid       [O] Destination for the resulting LUID
1084  *
1085  * RETURNS
1086  *  Success: TRUE. lpLuid contains the requested LUID.
1087  *  Failure: FALSE.
1088  */
1089 BOOL WINAPI
1090 LookupPrivilegeValueA( LPCSTR lpSystemName, LPCSTR lpName, PLUID lpLuid )
1091 {
1092     UNICODE_STRING lpSystemNameW;
1093     UNICODE_STRING lpNameW;
1094     BOOL ret;
1095
1096     RtlCreateUnicodeStringFromAsciiz(&lpSystemNameW, lpSystemName);
1097     RtlCreateUnicodeStringFromAsciiz(&lpNameW,lpName);
1098     ret = LookupPrivilegeValueW(lpSystemNameW.Buffer, lpNameW.Buffer, lpLuid);
1099     RtlFreeUnicodeString(&lpNameW);
1100     RtlFreeUnicodeString(&lpSystemNameW);
1101     return ret;
1102 }
1103
1104 BOOL WINAPI LookupPrivilegeDisplayNameA( LPCSTR lpSystemName, LPCSTR lpName, LPSTR lpDisplayName,
1105                                          LPDWORD cchDisplayName, LPDWORD lpLanguageId )
1106 {
1107     FIXME("%s %s %s %p %p - stub\n", debugstr_a(lpSystemName), debugstr_a(lpName),
1108           debugstr_a(lpDisplayName), cchDisplayName, lpLanguageId);
1109
1110     return FALSE;
1111 }
1112
1113 BOOL WINAPI LookupPrivilegeDisplayNameW( LPCWSTR lpSystemName, LPCWSTR lpName, LPWSTR lpDisplayName,
1114                                          LPDWORD cchDisplayName, LPDWORD lpLanguageId )
1115 {
1116     FIXME("%s %s %s %p %p - stub\n", debugstr_w(lpSystemName), debugstr_w(lpName),
1117           debugstr_w(lpDisplayName), cchDisplayName, lpLanguageId);
1118
1119     return FALSE;
1120 }
1121
1122 /******************************************************************************
1123  * LookupPrivilegeNameA                 [ADVAPI32.@]
1124  *
1125  * See LookupPrivilegeNameW
1126  */
1127 BOOL WINAPI
1128 LookupPrivilegeNameA( LPCSTR lpSystemName, PLUID lpLuid, LPSTR lpName,
1129  LPDWORD cchName)
1130 {
1131     UNICODE_STRING lpSystemNameW;
1132     BOOL ret;
1133     DWORD wLen = 0;
1134
1135     TRACE("%s %p %p %p\n", debugstr_a(lpSystemName), lpLuid, lpName, cchName);
1136
1137     RtlCreateUnicodeStringFromAsciiz(&lpSystemNameW, lpSystemName);
1138     ret = LookupPrivilegeNameW(lpSystemNameW.Buffer, lpLuid, NULL, &wLen);
1139     if (!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1140     {
1141         LPWSTR lpNameW = HeapAlloc(GetProcessHeap(), 0, wLen * sizeof(WCHAR));
1142
1143         ret = LookupPrivilegeNameW(lpSystemNameW.Buffer, lpLuid, lpNameW,
1144          &wLen);
1145         if (ret)
1146         {
1147             /* Windows crashes if cchName is NULL, so will I */
1148             int len = WideCharToMultiByte(CP_ACP, 0, lpNameW, -1, lpName,
1149              *cchName, NULL, NULL);
1150
1151             if (len == 0)
1152             {
1153                 /* WideCharToMultiByte failed */
1154                 ret = FALSE;
1155             }
1156             else if (len > *cchName)
1157             {
1158                 *cchName = len;
1159                 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1160                 ret = FALSE;
1161             }
1162             else
1163             {
1164                 /* WideCharToMultiByte succeeded, output length needs to be
1165                  * length not including NULL terminator
1166                  */
1167                 *cchName = len - 1;
1168             }
1169         }
1170         HeapFree(GetProcessHeap(), 0, lpNameW);
1171     }
1172     RtlFreeUnicodeString(&lpSystemNameW);
1173     return ret;
1174 }
1175
1176 /******************************************************************************
1177  * LookupPrivilegeNameW                 [ADVAPI32.@]
1178  *
1179  * Retrieves the privilege name referred to by the LUID lpLuid.
1180  *
1181  * PARAMS
1182  *  lpSystemName [I]   Name of the system
1183  *  lpLuid       [I]   Privilege value
1184  *  lpName       [O]   Name of the privilege
1185  *  cchName      [I/O] Number of characters in lpName.
1186  *
1187  * RETURNS
1188  *  Success: TRUE. lpName contains the name of the privilege whose value is
1189  *  *lpLuid.
1190  *  Failure: FALSE.
1191  *
1192  * REMARKS
1193  *  Only well-known privilege names (those defined in winnt.h) can be retrieved
1194  *  using this function.
1195  *  If the length of lpName is too small, on return *cchName will contain the
1196  *  number of WCHARs needed to contain the privilege, including the NULL
1197  *  terminator, and GetLastError will return ERROR_INSUFFICIENT_BUFFER.
1198  *  On success, *cchName will contain the number of characters stored in
1199  *  lpName, NOT including the NULL terminator.
1200  */
1201 BOOL WINAPI
1202 LookupPrivilegeNameW( LPCWSTR lpSystemName, PLUID lpLuid, LPWSTR lpName,
1203  LPDWORD cchName)
1204 {
1205     size_t privNameLen;
1206
1207     TRACE("%s,%p,%p,%p\n",debugstr_w(lpSystemName), lpLuid, lpName, cchName);
1208
1209     if (!ADVAPI_IsLocalComputer(lpSystemName))
1210     {
1211         SetLastError(RPC_S_SERVER_UNAVAILABLE);
1212         return FALSE;
1213     }
1214     if (lpLuid->HighPart || (lpLuid->LowPart < SE_MIN_WELL_KNOWN_PRIVILEGE ||
1215      lpLuid->LowPart > SE_MAX_WELL_KNOWN_PRIVILEGE))
1216     {
1217         SetLastError(ERROR_NO_SUCH_PRIVILEGE);
1218         return FALSE;
1219     }
1220     privNameLen = strlenW(WellKnownPrivNames[lpLuid->LowPart]);
1221     /* Windows crashes if cchName is NULL, so will I */
1222     if (*cchName <= privNameLen)
1223     {
1224         *cchName = privNameLen + 1;
1225         SetLastError(ERROR_INSUFFICIENT_BUFFER);
1226         return FALSE;
1227     }
1228     else
1229     {
1230         strcpyW(lpName, WellKnownPrivNames[lpLuid->LowPart]);
1231         *cchName = privNameLen;
1232         return TRUE;
1233     }
1234 }
1235
1236 /******************************************************************************
1237  * GetFileSecurityA [ADVAPI32.@]
1238  *
1239  * Obtains Specified information about the security of a file or directory.
1240  *
1241  * PARAMS
1242  *  lpFileName           [I] Name of the file to get info for
1243  *  RequestedInformation [I] SE_ flags from "winnt.h"
1244  *  pSecurityDescriptor  [O] Destination for security information
1245  *  nLength              [I] Length of pSecurityDescriptor
1246  *  lpnLengthNeeded      [O] Destination for length of returned security information
1247  *
1248  * RETURNS
1249  *  Success: TRUE. pSecurityDescriptor contains the requested information.
1250  *  Failure: FALSE. lpnLengthNeeded contains the required space to return the info. 
1251  *
1252  * NOTES
1253  *  The information returned is constrained by the callers access rights and
1254  *  privileges.
1255  */
1256 BOOL WINAPI
1257 GetFileSecurityA( LPCSTR lpFileName,
1258                     SECURITY_INFORMATION RequestedInformation,
1259                     PSECURITY_DESCRIPTOR pSecurityDescriptor,
1260                     DWORD nLength, LPDWORD lpnLengthNeeded )
1261 {
1262     DWORD len;
1263     BOOL r;
1264     LPWSTR name = NULL;
1265
1266     if( lpFileName )
1267     {
1268         len = MultiByteToWideChar( CP_ACP, 0, lpFileName, -1, NULL, 0 );
1269         name = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1270         MultiByteToWideChar( CP_ACP, 0, lpFileName, -1, name, len );
1271     }
1272
1273     r = GetFileSecurityW( name, RequestedInformation, pSecurityDescriptor,
1274                           nLength, lpnLengthNeeded );
1275     HeapFree( GetProcessHeap(), 0, name );
1276
1277     return r;
1278 }
1279
1280 /******************************************************************************
1281  * GetFileSecurityW [ADVAPI32.@]
1282  *
1283  * See GetFileSecurityA.
1284  */
1285 BOOL WINAPI
1286 GetFileSecurityW( LPCWSTR lpFileName,
1287                     SECURITY_INFORMATION RequestedInformation,
1288                     PSECURITY_DESCRIPTOR pSecurityDescriptor,
1289                     DWORD nLength, LPDWORD lpnLengthNeeded )
1290 {
1291   FIXME("(%s) : stub\n", debugstr_w(lpFileName) );
1292   return TRUE;
1293 }
1294
1295
1296 /******************************************************************************
1297  * LookupAccountSidA [ADVAPI32.@]
1298  */
1299 BOOL WINAPI
1300 LookupAccountSidA(
1301         IN LPCSTR system,
1302         IN PSID sid,
1303         OUT LPSTR account,
1304         IN OUT LPDWORD accountSize,
1305         OUT LPSTR domain,
1306         IN OUT LPDWORD domainSize,
1307         OUT PSID_NAME_USE name_use )
1308 {
1309         static const char ac[] = "Administrator";
1310         static const char dm[] = "DOMAIN";
1311         FIXME("(%s,sid=%p,%p,%p(%lu),%p,%p(%lu),%p): semi-stub\n",
1312               debugstr_a(system),sid,
1313               account,accountSize,accountSize?*accountSize:0,
1314               domain,domainSize,domainSize?*domainSize:0,
1315               name_use);
1316
1317         if (accountSize) *accountSize = strlen(ac)+1;
1318         if (account && (*accountSize > strlen(ac)))
1319           strcpy(account, ac);
1320
1321         if (domainSize) *domainSize = strlen(dm)+1;
1322         if (domain && (*domainSize > strlen(dm)))
1323           strcpy(domain,dm);
1324
1325         if (name_use) *name_use = SidTypeUser;
1326         return TRUE;
1327 }
1328
1329 /******************************************************************************
1330  * LookupAccountSidW [ADVAPI32.@]
1331  *
1332  * PARAMS
1333  *   system      []
1334  *   sid         []
1335  *   account     []
1336  *   accountSize []
1337  *   domain      []
1338  *   domainSize  []
1339  *   name_use    []
1340  */
1341 BOOL WINAPI
1342 LookupAccountSidW(
1343         IN LPCWSTR system,
1344         IN PSID sid,
1345         OUT LPWSTR account,
1346         IN OUT LPDWORD accountSize,
1347         OUT LPWSTR domain,
1348         IN OUT LPDWORD domainSize,
1349         OUT PSID_NAME_USE name_use )
1350 {
1351     static const WCHAR ac[] = {'A','d','m','i','n','i','s','t','r','a','t','o','r',0};
1352     static const WCHAR dm[] = {'D','O','M','A','I','N',0};
1353         FIXME("(%s,sid=%p,%p,%p(%lu),%p,%p(%lu),%p): semi-stub\n",
1354               debugstr_w(system),sid,
1355               account,accountSize,accountSize?*accountSize:0,
1356               domain,domainSize,domainSize?*domainSize:0,
1357               name_use);
1358
1359         if (accountSize) *accountSize = strlenW(ac)+1;
1360         if (account && (*accountSize > strlenW(ac)))
1361             strcpyW(account, ac);
1362
1363         if (domainSize) *domainSize = strlenW(dm)+1;
1364         if (domain && (*domainSize > strlenW(dm)))
1365             strcpyW(domain,dm);
1366
1367         if (name_use) *name_use = SidTypeUser;
1368         return TRUE;
1369 }
1370
1371 /******************************************************************************
1372  * SetFileSecurityA [ADVAPI32.@]
1373  * Sets the security of a file or directory
1374  */
1375 BOOL WINAPI SetFileSecurityA( LPCSTR lpFileName,
1376                                 SECURITY_INFORMATION RequestedInformation,
1377                                 PSECURITY_DESCRIPTOR pSecurityDescriptor)
1378 {
1379     DWORD len;
1380     BOOL r;
1381     LPWSTR name = NULL;
1382
1383     if( lpFileName )
1384     {
1385         len = MultiByteToWideChar( CP_ACP, 0, lpFileName, -1, NULL, 0 );
1386         name = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1387         MultiByteToWideChar( CP_ACP, 0, lpFileName, -1, name, len );
1388     }
1389
1390     r = SetFileSecurityW( name, RequestedInformation, pSecurityDescriptor );
1391     HeapFree( GetProcessHeap(), 0, name );
1392
1393     return r;
1394 }
1395
1396 /******************************************************************************
1397  * SetFileSecurityW [ADVAPI32.@]
1398  * Sets the security of a file or directory
1399  *
1400  * PARAMS
1401  *   lpFileName           []
1402  *   RequestedInformation []
1403  *   pSecurityDescriptor  []
1404  */
1405 BOOL WINAPI
1406 SetFileSecurityW( LPCWSTR lpFileName,
1407                     SECURITY_INFORMATION RequestedInformation,
1408                     PSECURITY_DESCRIPTOR pSecurityDescriptor )
1409 {
1410   FIXME("(%s) : stub\n", debugstr_w(lpFileName) );
1411   return TRUE;
1412 }
1413
1414 /******************************************************************************
1415  * QueryWindows31FilesMigration [ADVAPI32.@]
1416  *
1417  * PARAMS
1418  *   x1 []
1419  */
1420 BOOL WINAPI
1421 QueryWindows31FilesMigration( DWORD x1 )
1422 {
1423         FIXME("(%ld):stub\n",x1);
1424         return TRUE;
1425 }
1426
1427 /******************************************************************************
1428  * SynchronizeWindows31FilesAndWindowsNTRegistry [ADVAPI32.@]
1429  *
1430  * PARAMS
1431  *   x1 []
1432  *   x2 []
1433  *   x3 []
1434  *   x4 []
1435  */
1436 BOOL WINAPI
1437 SynchronizeWindows31FilesAndWindowsNTRegistry( DWORD x1, DWORD x2, DWORD x3,
1438                                                DWORD x4 )
1439 {
1440         FIXME("(0x%08lx,0x%08lx,0x%08lx,0x%08lx):stub\n",x1,x2,x3,x4);
1441         return TRUE;
1442 }
1443
1444 NTSTATUS WINAPI
1445 LsaEnumerateTrustedDomains(
1446     LSA_HANDLE PolicyHandle,
1447     PLSA_ENUMERATION_HANDLE EnumerationContext,
1448     PVOID* Buffer,
1449     ULONG PreferedMaximumLength,
1450     PULONG CountReturned)
1451 {
1452     FIXME("(%p,%p,%p,0x%08lx,%p):stub\n", PolicyHandle, EnumerationContext,
1453           Buffer, PreferedMaximumLength, CountReturned);
1454
1455     if (CountReturned) *CountReturned = 0;
1456     return STATUS_SUCCESS;
1457 }
1458
1459 NTSTATUS WINAPI
1460 LsaLookupNames(
1461   LSA_HANDLE PolicyHandle,
1462   ULONG Count,
1463   PLSA_UNICODE_STRING Names,
1464   PLSA_REFERENCED_DOMAIN_LIST* ReferencedDomains,
1465   PLSA_TRANSLATED_SID* Sids)
1466 {
1467     FIXME("(%p,0x%08lx,%p,%p,%p):stub\n", PolicyHandle, Count, Names,
1468           ReferencedDomains, Sids);
1469
1470     return STATUS_NONE_MAPPED;
1471 }
1472
1473 /******************************************************************************
1474  * LsaOpenPolicy [ADVAPI32.@]
1475  *
1476  * PARAMS
1477  *   SystemName       [I]
1478  *   ObjectAttributes [I]
1479  *   DesiredAccess    [I]
1480  *   PolicyHandle     [I/O]
1481  */
1482 NTSTATUS WINAPI
1483 LsaOpenPolicy(
1484         IN PLSA_UNICODE_STRING SystemName,
1485         IN PLSA_OBJECT_ATTRIBUTES ObjectAttributes,
1486         IN ACCESS_MASK DesiredAccess,
1487         IN OUT PLSA_HANDLE PolicyHandle)
1488 {
1489         FIXME("(%s,%p,0x%08lx,%p):stub\n",
1490               SystemName?debugstr_w(SystemName->Buffer):"null",
1491               ObjectAttributes, DesiredAccess, PolicyHandle);
1492         ADVAPI_ForceLocalComputer(SystemName ? SystemName->Buffer : NULL,
1493                                   STATUS_ACCESS_VIOLATION);
1494         dumpLsaAttributes(ObjectAttributes);
1495         if(PolicyHandle) *PolicyHandle = (LSA_HANDLE)0xcafe;
1496         return STATUS_SUCCESS;
1497 }
1498
1499 /******************************************************************************
1500  * LsaQueryInformationPolicy [ADVAPI32.@]
1501  */
1502 NTSTATUS WINAPI
1503 LsaQueryInformationPolicy(
1504         IN LSA_HANDLE PolicyHandle,
1505         IN POLICY_INFORMATION_CLASS InformationClass,
1506         OUT PVOID *Buffer)
1507 {
1508         FIXME("(%p,0x%08x,%p):stub\n",
1509               PolicyHandle, InformationClass, Buffer);
1510
1511         if(!Buffer) return FALSE;
1512         switch (InformationClass)
1513         {
1514           case PolicyAuditEventsInformation: /* 2 */
1515             {
1516               PPOLICY_AUDIT_EVENTS_INFO p = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(POLICY_AUDIT_EVENTS_INFO));
1517               p->AuditingMode = FALSE; /* no auditing */
1518               *Buffer = p;
1519             }
1520             break;
1521           case PolicyPrimaryDomainInformation: /* 3 */
1522           case PolicyAccountDomainInformation: /* 5 */
1523             {
1524               struct di
1525               { POLICY_PRIMARY_DOMAIN_INFO ppdi;
1526                 SID sid;
1527               };
1528               SID_IDENTIFIER_AUTHORITY localSidAuthority = {SECURITY_NT_AUTHORITY};
1529
1530               struct di * xdi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(xdi));
1531               HKEY key;
1532               BOOL useDefault = TRUE;
1533               LONG ret;
1534
1535               if ((ret = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1536                "System\\CurrentControlSet\\Services\\VxD\\VNETSUP", 0,
1537                KEY_READ, &key)) == ERROR_SUCCESS)
1538               {
1539                   DWORD size = 0;
1540                   static const WCHAR wg[] = { 'W','o','r','k','g','r','o','u','p',0 };
1541
1542                   ret = RegQueryValueExW(key, wg, NULL, NULL, NULL, &size);
1543                   if (ret == ERROR_MORE_DATA || ret == ERROR_SUCCESS)
1544                   {
1545                       xdi->ppdi.Name.Buffer = HeapAlloc(GetProcessHeap(),
1546                        HEAP_ZERO_MEMORY, size);
1547                       if ((ret = RegQueryValueExW(key, wg, NULL, NULL,
1548                        (LPBYTE)xdi->ppdi.Name.Buffer, &size)) == ERROR_SUCCESS)
1549                       {
1550                           xdi->ppdi.Name.Length = (USHORT)size;
1551                           useDefault = FALSE;
1552                       }
1553                       else
1554                       {
1555                           HeapFree(GetProcessHeap(), 0, xdi->ppdi.Name.Buffer);
1556                           xdi->ppdi.Name.Buffer = NULL;
1557                       }
1558                   }
1559                   RegCloseKey(key);
1560               }
1561               if (useDefault)
1562                   RtlCreateUnicodeStringFromAsciiz(&(xdi->ppdi.Name), "DOMAIN");
1563               TRACE("setting domain to %s\n", debugstr_w(xdi->ppdi.Name.Buffer));
1564
1565               xdi->ppdi.Sid = &(xdi->sid);
1566               xdi->sid.Revision = SID_REVISION;
1567               xdi->sid.SubAuthorityCount = 1;
1568               xdi->sid.IdentifierAuthority = localSidAuthority;
1569               xdi->sid.SubAuthority[0] = SECURITY_LOCAL_SYSTEM_RID;
1570               *Buffer = xdi;
1571             }
1572             break;
1573           case  PolicyAuditLogInformation:
1574           case  PolicyPdAccountInformation:
1575           case  PolicyLsaServerRoleInformation:
1576           case  PolicyReplicaSourceInformation:
1577           case  PolicyDefaultQuotaInformation:
1578           case  PolicyModificationInformation:
1579           case  PolicyAuditFullSetInformation:
1580           case  PolicyAuditFullQueryInformation:
1581           case  PolicyDnsDomainInformation:
1582             {
1583               FIXME("category not implemented\n");
1584               return FALSE;
1585             }
1586         }
1587         return TRUE;
1588 }
1589
1590 NTSTATUS WINAPI
1591 LsaSetInformationPolicy(
1592     LSA_HANDLE PolicyHandle,
1593     POLICY_INFORMATION_CLASS InformationClass,
1594     PVOID Buffer)
1595 {
1596     FIXME("(%p,0x%08x,%p):stub\n", PolicyHandle, InformationClass, Buffer);
1597
1598     return STATUS_UNSUCCESSFUL;    
1599 }
1600
1601 /******************************************************************************
1602  * LsaLookupSids [ADVAPI32.@]
1603  */
1604 NTSTATUS WINAPI
1605 LsaLookupSids(
1606         IN LSA_HANDLE PolicyHandle,
1607         IN ULONG Count,
1608         IN PSID *Sids,
1609         OUT PLSA_REFERENCED_DOMAIN_LIST *ReferencedDomains,
1610         OUT PLSA_TRANSLATED_NAME *Names )
1611 {
1612         FIXME("%p %lu %p %p %p\n",
1613           PolicyHandle, Count, Sids, ReferencedDomains, Names);
1614         return FALSE;
1615 }
1616
1617 /******************************************************************************
1618  * LsaFreeMemory [ADVAPI32.@]
1619  */
1620 NTSTATUS WINAPI
1621 LsaFreeMemory(IN PVOID Buffer)
1622 {
1623         TRACE("(%p)\n",Buffer);
1624         return HeapFree(GetProcessHeap(), 0, Buffer);
1625 }
1626 /******************************************************************************
1627  * LsaClose [ADVAPI32.@]
1628  */
1629 NTSTATUS WINAPI
1630 LsaClose(IN LSA_HANDLE ObjectHandle)
1631 {
1632         FIXME("(%p):stub\n",ObjectHandle);
1633         return 0xc0000000;
1634 }
1635
1636 /******************************************************************************
1637  * LsaStorePrivateData [ADVAPI32.@]
1638  */
1639 NTSTATUS WINAPI LsaStorePrivateData( LSA_HANDLE PolicyHandle,
1640     PLSA_UNICODE_STRING KeyName, PLSA_UNICODE_STRING PrivateData)
1641 {
1642     FIXME("%p %p %p\n", PolicyHandle, KeyName, PrivateData);
1643     return STATUS_OBJECT_NAME_NOT_FOUND;
1644 }
1645
1646 /******************************************************************************
1647  * LsaRetrievePrivateData [ADVAPI32.@]
1648  */
1649 NTSTATUS WINAPI LsaRetrievePrivateData( LSA_HANDLE PolicyHandle,
1650     PLSA_UNICODE_STRING KeyName, PLSA_UNICODE_STRING* PrivateData)
1651 {
1652     FIXME("%p %p %p\n", PolicyHandle, KeyName, PrivateData);
1653     return STATUS_OBJECT_NAME_NOT_FOUND;
1654 }
1655
1656 /******************************************************************************
1657  * LsaNtStatusToWinError [ADVAPI32.@]
1658  *
1659  * PARAMS
1660  *   Status [I]
1661  */
1662 ULONG WINAPI
1663 LsaNtStatusToWinError(NTSTATUS Status)
1664 {
1665     return RtlNtStatusToDosError(Status);
1666 }
1667
1668 /******************************************************************************
1669  * NotifyBootConfigStatus [ADVAPI32.@]
1670  *
1671  * PARAMS
1672  *   x1 []
1673  */
1674 BOOL WINAPI
1675 NotifyBootConfigStatus( DWORD x1 )
1676 {
1677         FIXME("(0x%08lx):stub\n",x1);
1678         return 1;
1679 }
1680
1681 /******************************************************************************
1682  * RevertToSelf [ADVAPI32.@]
1683  *
1684  * PARAMS
1685  *   void []
1686  */
1687 BOOL WINAPI
1688 RevertToSelf( void )
1689 {
1690         FIXME("(), stub\n");
1691         return TRUE;
1692 }
1693
1694 /******************************************************************************
1695  * ImpersonateSelf [ADVAPI32.@]
1696  */
1697 BOOL WINAPI
1698 ImpersonateSelf(SECURITY_IMPERSONATION_LEVEL ImpersonationLevel)
1699 {
1700         return RtlImpersonateSelf(ImpersonationLevel);
1701 }
1702
1703 /******************************************************************************
1704  * ImpersonateLoggedOnUser [ADVAPI32.@]
1705  */
1706 BOOL WINAPI ImpersonateLoggedOnUser(HANDLE hToken)
1707 {
1708     FIXME("(%p):stub returning FALSE\n", hToken);
1709     return FALSE;
1710 }
1711
1712 /******************************************************************************
1713  * AccessCheck [ADVAPI32.@]
1714  */
1715 BOOL WINAPI
1716 AccessCheck(
1717         PSECURITY_DESCRIPTOR SecurityDescriptor,
1718         HANDLE ClientToken,
1719         DWORD DesiredAccess,
1720         PGENERIC_MAPPING GenericMapping,
1721         PPRIVILEGE_SET PrivilegeSet,
1722         LPDWORD PrivilegeSetLength,
1723         LPDWORD GrantedAccess,
1724         LPBOOL AccessStatus)
1725 {
1726     NTSTATUS access_status;
1727     BOOL ret = set_ntstatus( NtAccessCheck(SecurityDescriptor, ClientToken, DesiredAccess,
1728                                            GenericMapping, PrivilegeSet, PrivilegeSetLength,
1729                                            GrantedAccess, &access_status) );
1730     if (ret) *AccessStatus = set_ntstatus( access_status );
1731     return ret;
1732 }
1733
1734
1735 /******************************************************************************
1736  * AccessCheckByType [ADVAPI32.@]
1737  */
1738 BOOL WINAPI AccessCheckByType(
1739     PSECURITY_DESCRIPTOR pSecurityDescriptor, 
1740     PSID PrincipalSelfSid,
1741     HANDLE ClientToken, 
1742     DWORD DesiredAccess, 
1743     POBJECT_TYPE_LIST ObjectTypeList,
1744     DWORD ObjectTypeListLength,
1745     PGENERIC_MAPPING GenericMapping,
1746     PPRIVILEGE_SET PrivilegeSet,
1747     LPDWORD PrivilegeSetLength, 
1748     LPDWORD GrantedAccess,
1749     LPBOOL AccessStatus)
1750 {
1751         FIXME("stub\n");
1752
1753         *AccessStatus = TRUE;
1754
1755         return !*AccessStatus;
1756 }
1757
1758 VOID WINAPI MapGenericMask( PDWORD AccessMask, PGENERIC_MAPPING GenericMapping )
1759 {
1760         FIXME("%p %p - stub\n", AccessMask, GenericMapping);
1761
1762     *AccessMask |= GenericMapping->GenericRead;
1763     *AccessMask |= GenericMapping->GenericWrite;
1764     *AccessMask |= GenericMapping->GenericExecute;
1765     *AccessMask |= GenericMapping->GenericAll;
1766 }
1767
1768 /*************************************************************************
1769  * SetKernelObjectSecurity [ADVAPI32.@]
1770  */
1771 BOOL WINAPI SetKernelObjectSecurity (
1772         IN HANDLE Handle,
1773         IN SECURITY_INFORMATION SecurityInformation,
1774         IN PSECURITY_DESCRIPTOR SecurityDescriptor )
1775 {
1776     return set_ntstatus (NtSetSecurityObject (Handle, SecurityInformation, SecurityDescriptor));
1777 }
1778
1779
1780 /******************************************************************************
1781  *  AddAuditAccessAce [ADVAPI32.@]
1782  */
1783 BOOL WINAPI AddAuditAccessAce(
1784         IN OUT PACL pAcl,
1785         IN DWORD dwAceRevision,
1786         IN DWORD dwAccessMask,
1787         IN PSID pSid,
1788         IN BOOL bAuditSuccess,
1789         IN BOOL bAuditFailure)
1790 {
1791         FIXME("Stub\n");
1792         return TRUE;
1793 }
1794
1795 /******************************************************************************
1796  * LookupAccountNameA [ADVAPI32.@]
1797  */
1798 BOOL WINAPI
1799 LookupAccountNameA(
1800         IN LPCSTR system,
1801         IN LPCSTR account,
1802         OUT PSID sid,
1803         OUT LPDWORD cbSid,
1804         LPSTR ReferencedDomainName,
1805         IN OUT LPDWORD cbReferencedDomainName,
1806         OUT PSID_NAME_USE name_use )
1807 {
1808     /* Default implementation: Always return a default SID */
1809     SID_IDENTIFIER_AUTHORITY identifierAuthority = {SECURITY_NT_AUTHORITY};
1810     BOOL ret;
1811     PSID pSid;
1812     static const char dm[] = "DOMAIN";
1813
1814     FIXME("(%s,%s,%p,%p,%p,%p,%p), stub.\n",system,account,sid,cbSid,ReferencedDomainName,cbReferencedDomainName,name_use);
1815
1816     ret = AllocateAndInitializeSid(&identifierAuthority,
1817         2,
1818         SECURITY_BUILTIN_DOMAIN_RID,
1819         DOMAIN_ALIAS_RID_ADMINS,
1820         0, 0, 0, 0, 0, 0,
1821         &pSid);
1822
1823     if (!ret)
1824        return FALSE;
1825     if(!RtlValidSid(pSid))
1826     {
1827        FreeSid(pSid);
1828        return FALSE;
1829     }
1830
1831     if (sid != NULL && (*cbSid >= GetLengthSid(pSid)))
1832        CopySid(*cbSid, sid, pSid);
1833     if (*cbSid < GetLengthSid(pSid))
1834     {
1835        SetLastError(ERROR_INSUFFICIENT_BUFFER);
1836        ret = FALSE;
1837     }
1838     *cbSid = GetLengthSid(pSid);
1839     
1840     if (ReferencedDomainName != NULL && (*cbReferencedDomainName > strlen(dm)))
1841       strcpy(ReferencedDomainName, dm);
1842     if (*cbReferencedDomainName <= strlen(dm))
1843     {
1844        SetLastError(ERROR_INSUFFICIENT_BUFFER);
1845        ret = FALSE;
1846     }
1847     *cbReferencedDomainName = strlen(dm)+1;
1848
1849     FreeSid(pSid);
1850
1851     return ret;
1852 }
1853
1854 BOOL WINAPI LookupAccountNameW( LPCWSTR lpSystemName, LPCWSTR lpAccountName, PSID Sid,
1855                                 LPDWORD cbSid, LPWSTR ReferencedDomainName,
1856                                 LPDWORD cchReferencedDomainName, PSID_NAME_USE peUse )
1857 {
1858     FIXME("%s %s %p %p %p %p %p - stub\n", debugstr_w(lpSystemName), debugstr_w(lpAccountName),
1859           Sid, cbSid, ReferencedDomainName, cchReferencedDomainName, peUse);
1860
1861     return FALSE;
1862 }
1863
1864 /******************************************************************************
1865  * PrivilegeCheck [ADVAPI32.@]
1866  */
1867 BOOL WINAPI PrivilegeCheck( HANDLE ClientToken, PPRIVILEGE_SET RequiredPrivileges, LPBOOL pfResult)
1868 {
1869         FIXME("stub %p %p %p\n", ClientToken, RequiredPrivileges, pfResult);
1870         if (pfResult)
1871                 *pfResult=TRUE;
1872         return TRUE;
1873 }
1874
1875 /******************************************************************************
1876  * AccessCheckAndAuditAlarmA [ADVAPI32.@]
1877  */
1878 BOOL WINAPI AccessCheckAndAuditAlarmA(LPCSTR Subsystem, LPVOID HandleId, LPSTR ObjectTypeName,
1879   LPSTR ObjectName, PSECURITY_DESCRIPTOR SecurityDescriptor, DWORD DesiredAccess,
1880   PGENERIC_MAPPING GenericMapping, BOOL ObjectCreation, LPDWORD GrantedAccess,
1881   LPBOOL AccessStatus, LPBOOL pfGenerateOnClose)
1882 {
1883         FIXME("stub (%s,%p,%s,%s,%p,%08lx,%p,%x,%p,%p,%p)\n", debugstr_a(Subsystem),
1884                 HandleId, debugstr_a(ObjectTypeName), debugstr_a(ObjectName),
1885                 SecurityDescriptor, DesiredAccess, GenericMapping,
1886                 ObjectCreation, GrantedAccess, AccessStatus, pfGenerateOnClose);
1887         return TRUE;
1888 }
1889
1890 /******************************************************************************
1891  * AccessCheckAndAuditAlarmW [ADVAPI32.@]
1892  */
1893 BOOL WINAPI AccessCheckAndAuditAlarmW(LPCWSTR Subsystem, LPVOID HandleId, LPWSTR ObjectTypeName,
1894   LPWSTR ObjectName, PSECURITY_DESCRIPTOR SecurityDescriptor, DWORD DesiredAccess,
1895   PGENERIC_MAPPING GenericMapping, BOOL ObjectCreation, LPDWORD GrantedAccess,
1896   LPBOOL AccessStatus, LPBOOL pfGenerateOnClose)
1897 {
1898         FIXME("stub (%s,%p,%s,%s,%p,%08lx,%p,%x,%p,%p,%p)\n", debugstr_w(Subsystem),
1899                 HandleId, debugstr_w(ObjectTypeName), debugstr_w(ObjectName),
1900                 SecurityDescriptor, DesiredAccess, GenericMapping,
1901                 ObjectCreation, GrantedAccess, AccessStatus, pfGenerateOnClose);
1902         return TRUE;
1903 }
1904
1905 BOOL WINAPI ObjectCloseAuditAlarmA(LPCSTR SubsystemName, LPVOID HandleId, BOOL GenerateOnClose)
1906 {
1907     FIXME("stub (%s,%p,%x)\n", debugstr_a(SubsystemName), HandleId, GenerateOnClose);
1908
1909     return TRUE;
1910 }
1911
1912 BOOL WINAPI ObjectCloseAuditAlarmW(LPCWSTR SubsystemName, LPVOID HandleId, BOOL GenerateOnClose)
1913 {
1914     FIXME("stub (%s,%p,%x)\n", debugstr_w(SubsystemName), HandleId, GenerateOnClose);
1915
1916     return TRUE;
1917 }
1918
1919 BOOL WINAPI ObjectOpenAuditAlarmA(LPCSTR SubsystemName, LPVOID HandleId, LPSTR ObjectTypeName,
1920   LPSTR ObjectName, PSECURITY_DESCRIPTOR pSecurityDescriptor, HANDLE ClientToken, DWORD DesiredAccess,
1921   DWORD GrantedAccess, PPRIVILEGE_SET Privileges, BOOL ObjectCreation, BOOL AccessGranted,
1922   LPBOOL GenerateOnClose)
1923 {
1924         FIXME("stub (%s,%p,%s,%s,%p,%p,0x%08lx,0x%08lx,%p,%x,%x,%p)\n", debugstr_a(SubsystemName),
1925                 HandleId, debugstr_a(ObjectTypeName), debugstr_a(ObjectName), pSecurityDescriptor,
1926         ClientToken, DesiredAccess, GrantedAccess, Privileges, ObjectCreation, AccessGranted,
1927         GenerateOnClose);
1928
1929     return TRUE;
1930 }
1931
1932 BOOL WINAPI ObjectOpenAuditAlarmW(LPCWSTR SubsystemName, LPVOID HandleId, LPWSTR ObjectTypeName,
1933   LPWSTR ObjectName, PSECURITY_DESCRIPTOR pSecurityDescriptor, HANDLE ClientToken, DWORD DesiredAccess,
1934   DWORD GrantedAccess, PPRIVILEGE_SET Privileges, BOOL ObjectCreation, BOOL AccessGranted,
1935   LPBOOL GenerateOnClose)
1936 {
1937     FIXME("stub (%s,%p,%s,%s,%p,%p,0x%08lx,0x%08lx,%p,%x,%x,%p)\n", debugstr_w(SubsystemName),
1938         HandleId, debugstr_w(ObjectTypeName), debugstr_w(ObjectName), pSecurityDescriptor,
1939         ClientToken, DesiredAccess, GrantedAccess, Privileges, ObjectCreation, AccessGranted,
1940         GenerateOnClose);
1941
1942     return TRUE;
1943 }
1944
1945 BOOL WINAPI ObjectPrivilegeAuditAlarmA( LPCSTR SubsystemName, LPVOID HandleId, HANDLE ClientToken,
1946   DWORD DesiredAccess, PPRIVILEGE_SET Privileges, BOOL AccessGranted)
1947 {
1948     FIXME("stub (%s,%p,%p,0x%08lx,%p,%x)\n", debugstr_a(SubsystemName), HandleId, ClientToken,
1949           DesiredAccess, Privileges, AccessGranted);
1950
1951     return TRUE;
1952 }
1953
1954 BOOL WINAPI ObjectPrivilegeAuditAlarmW( LPCWSTR SubsystemName, LPVOID HandleId, HANDLE ClientToken,
1955   DWORD DesiredAccess, PPRIVILEGE_SET Privileges, BOOL AccessGranted)
1956 {
1957     FIXME("stub (%s,%p,%p,0x%08lx,%p,%x)\n", debugstr_w(SubsystemName), HandleId, ClientToken,
1958           DesiredAccess, Privileges, AccessGranted);
1959
1960     return TRUE;
1961 }
1962
1963 BOOL WINAPI PrivilegedServiceAuditAlarmA( LPCSTR SubsystemName, LPCSTR ServiceName, HANDLE ClientToken,
1964                                    PPRIVILEGE_SET Privileges, BOOL AccessGranted)
1965 {
1966     FIXME("stub (%s,%s,%p,%p,%x)\n", debugstr_a(SubsystemName), debugstr_a(ServiceName),
1967           ClientToken, Privileges, AccessGranted);
1968
1969     return TRUE;
1970 }
1971
1972 BOOL WINAPI PrivilegedServiceAuditAlarmW( LPCWSTR SubsystemName, LPCWSTR ServiceName, HANDLE ClientToken,
1973                                    PPRIVILEGE_SET Privileges, BOOL AccessGranted)
1974 {
1975     FIXME("stub %s,%s,%p,%p,%x)\n", debugstr_w(SubsystemName), debugstr_w(ServiceName),
1976           ClientToken, Privileges, AccessGranted);
1977
1978     return TRUE;
1979 }
1980
1981 /******************************************************************************
1982  * GetSecurityInfo [ADVAPI32.@]
1983  */
1984 DWORD WINAPI GetSecurityInfo(
1985     HANDLE hObject, SE_OBJECT_TYPE ObjectType,
1986     SECURITY_INFORMATION SecurityInfo, PSID *ppsidOwner,
1987     PSID *ppsidGroup, PACL *ppDacl, PACL *ppSacl,
1988     PSECURITY_DESCRIPTOR *ppSecurityDescriptor
1989 )
1990 {
1991   FIXME("stub!\n");
1992   return ERROR_BAD_PROVIDER;
1993 }
1994
1995 /******************************************************************************
1996  * GetSecurityInfoExW [ADVAPI32.@]
1997  */
1998 DWORD WINAPI GetSecurityInfoExW(
1999         HANDLE hObject, SE_OBJECT_TYPE ObjectType, 
2000         SECURITY_INFORMATION SecurityInfo, LPCWSTR lpProvider,
2001         LPCWSTR lpProperty, PACTRL_ACCESSW *ppAccessList, 
2002         PACTRL_AUDITW *ppAuditList, LPWSTR *lppOwner, LPWSTR *lppGroup
2003 )
2004 {
2005   FIXME("stub!\n");
2006   return ERROR_BAD_PROVIDER; 
2007 }
2008
2009 /******************************************************************************
2010  * BuildExplicitAccessWithNameA [ADVAPI32.@]
2011  */
2012 VOID WINAPI BuildExplicitAccessWithNameA( PEXPLICIT_ACCESSA pExplicitAccess,
2013                                           LPSTR pTrusteeName, DWORD AccessPermissions,
2014                                           ACCESS_MODE AccessMode, DWORD Inheritance )
2015 {
2016     TRACE("%p %s 0x%08lx 0x%08x 0x%08lx\n", pExplicitAccess, debugstr_a(pTrusteeName),
2017           AccessPermissions, AccessMode, Inheritance);
2018
2019     pExplicitAccess->grfAccessPermissions = AccessPermissions;
2020     pExplicitAccess->grfAccessMode = AccessMode;
2021     pExplicitAccess->grfInheritance = Inheritance;
2022
2023     pExplicitAccess->Trustee.pMultipleTrustee = NULL;
2024     pExplicitAccess->Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
2025     pExplicitAccess->Trustee.TrusteeForm = TRUSTEE_IS_NAME;
2026     pExplicitAccess->Trustee.TrusteeType = TRUSTEE_IS_UNKNOWN;
2027     pExplicitAccess->Trustee.ptstrName = pTrusteeName;
2028 }
2029
2030 /******************************************************************************
2031  * BuildExplicitAccessWithNameW [ADVAPI32.@]
2032  */
2033 VOID WINAPI BuildExplicitAccessWithNameW( PEXPLICIT_ACCESSW pExplicitAccess,
2034                                           LPWSTR pTrusteeName, DWORD AccessPermissions,
2035                                           ACCESS_MODE AccessMode, DWORD Inheritance )
2036 {
2037     TRACE("%p %s 0x%08lx 0x%08x 0x%08lx\n", pExplicitAccess, debugstr_w(pTrusteeName),
2038           AccessPermissions, AccessMode, Inheritance);
2039
2040     pExplicitAccess->grfAccessPermissions = AccessPermissions;
2041     pExplicitAccess->grfAccessMode = AccessMode;
2042     pExplicitAccess->grfInheritance = Inheritance;
2043
2044     pExplicitAccess->Trustee.pMultipleTrustee = NULL;
2045     pExplicitAccess->Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
2046     pExplicitAccess->Trustee.TrusteeForm = TRUSTEE_IS_NAME;
2047     pExplicitAccess->Trustee.TrusteeType = TRUSTEE_IS_UNKNOWN;
2048     pExplicitAccess->Trustee.ptstrName = pTrusteeName;
2049 }
2050
2051 /******************************************************************************
2052  * BuildTrusteeWithObjectsAndNameA [ADVAPI32.@]
2053  */
2054 VOID WINAPI BuildTrusteeWithObjectsAndNameA( PTRUSTEEA pTrustee, POBJECTS_AND_NAME_A pObjName,
2055                                              SE_OBJECT_TYPE ObjectType, LPSTR ObjectTypeName,
2056                                              LPSTR InheritedObjectTypeName, LPSTR Name )
2057 {
2058     TRACE("%p %p 0x%08x %p %p %s\n", pTrustee, pObjName,
2059           ObjectType, ObjectTypeName, InheritedObjectTypeName, debugstr_a(Name));
2060
2061     pTrustee->pMultipleTrustee = NULL;
2062     pTrustee->MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
2063     pTrustee->TrusteeForm = TRUSTEE_IS_OBJECTS_AND_NAME;
2064     pTrustee->TrusteeType = TRUSTEE_IS_UNKNOWN;
2065     pTrustee->ptstrName = Name;
2066 }
2067
2068 /******************************************************************************
2069  * BuildTrusteeWithObjectsAndNameW [ADVAPI32.@]
2070  */
2071 VOID WINAPI BuildTrusteeWithObjectsAndNameW( PTRUSTEEW pTrustee, POBJECTS_AND_NAME_W pObjName,
2072                                              SE_OBJECT_TYPE ObjectType, LPWSTR ObjectTypeName,
2073                                              LPWSTR InheritedObjectTypeName, LPWSTR Name )
2074 {
2075     TRACE("%p %p 0x%08x %p %p %s\n", pTrustee, pObjName,
2076           ObjectType, ObjectTypeName, InheritedObjectTypeName, debugstr_w(Name));
2077
2078     pTrustee->pMultipleTrustee = NULL;
2079     pTrustee->MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
2080     pTrustee->TrusteeForm = TRUSTEE_IS_OBJECTS_AND_NAME;
2081     pTrustee->TrusteeType = TRUSTEE_IS_UNKNOWN;
2082     pTrustee->ptstrName = Name;
2083 }
2084
2085 VOID WINAPI BuildTrusteeWithObjectsAndSidA( PTRUSTEEA pTrustee, POBJECTS_AND_SID pObjSid,
2086                                             GUID* pObjectGuid, GUID* pInheritedObjectGuid, PSID pSid )
2087 {
2088     TRACE("%p %p %p %p %p\n", pTrustee, pObjSid, pObjectGuid, pInheritedObjectGuid, pSid);
2089
2090     pTrustee->pMultipleTrustee = NULL;
2091     pTrustee->MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
2092     pTrustee->TrusteeForm = TRUSTEE_IS_OBJECTS_AND_SID;
2093     pTrustee->TrusteeType = TRUSTEE_IS_UNKNOWN;
2094     pTrustee->ptstrName = (LPSTR) pSid;
2095 }
2096
2097 VOID WINAPI BuildTrusteeWithObjectsAndSidW( PTRUSTEEW pTrustee, POBJECTS_AND_SID pObjSid,
2098                                             GUID* pObjectGuid, GUID* pInheritedObjectGuid, PSID pSid )
2099 {
2100     TRACE("%p %p %p %p %p\n", pTrustee, pObjSid, pObjectGuid, pInheritedObjectGuid, pSid);
2101
2102     pTrustee->pMultipleTrustee = NULL;
2103     pTrustee->MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
2104     pTrustee->TrusteeForm = TRUSTEE_IS_OBJECTS_AND_SID;
2105     pTrustee->TrusteeType = TRUSTEE_IS_UNKNOWN;
2106     pTrustee->ptstrName = (LPWSTR) pSid;
2107 }
2108
2109 /******************************************************************************
2110  * BuildTrusteeWithSidA [ADVAPI32.@]
2111  */
2112 VOID WINAPI BuildTrusteeWithSidA(PTRUSTEEA pTrustee, PSID pSid)
2113 {
2114     TRACE("%p %p\n", pTrustee, pSid);
2115
2116     pTrustee->pMultipleTrustee = NULL;
2117     pTrustee->MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
2118     pTrustee->TrusteeForm = TRUSTEE_IS_SID;
2119     pTrustee->TrusteeType = TRUSTEE_IS_UNKNOWN;
2120     pTrustee->ptstrName = (LPSTR) pSid;
2121 }
2122
2123 /******************************************************************************
2124  * BuildTrusteeWithSidW [ADVAPI32.@]
2125  */
2126 VOID WINAPI BuildTrusteeWithSidW(PTRUSTEEW pTrustee, PSID pSid)
2127 {
2128     TRACE("%p %p\n", pTrustee, pSid);
2129
2130     pTrustee->pMultipleTrustee = NULL;
2131     pTrustee->MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
2132     pTrustee->TrusteeForm = TRUSTEE_IS_SID;
2133     pTrustee->TrusteeType = TRUSTEE_IS_UNKNOWN;
2134     pTrustee->ptstrName = (LPWSTR) pSid;
2135 }
2136
2137 /******************************************************************************
2138  * BuildTrusteeWithNameA [ADVAPI32.@]
2139  */
2140 VOID WINAPI BuildTrusteeWithNameA(PTRUSTEEA pTrustee, LPSTR name)
2141 {
2142     TRACE("%p %s\n", pTrustee, debugstr_a(name) );
2143
2144     pTrustee->pMultipleTrustee = NULL;
2145     pTrustee->MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
2146     pTrustee->TrusteeForm = TRUSTEE_IS_NAME;
2147     pTrustee->TrusteeType = TRUSTEE_IS_UNKNOWN;
2148     pTrustee->ptstrName = name;
2149 }
2150
2151 /******************************************************************************
2152  * BuildTrusteeWithNameW [ADVAPI32.@]
2153  */
2154 VOID WINAPI BuildTrusteeWithNameW(PTRUSTEEW pTrustee, LPWSTR name)
2155 {
2156     TRACE("%p %s\n", pTrustee, debugstr_w(name) );
2157
2158     pTrustee->pMultipleTrustee = NULL;
2159     pTrustee->MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
2160     pTrustee->TrusteeForm = TRUSTEE_IS_NAME;
2161     pTrustee->TrusteeType = TRUSTEE_IS_UNKNOWN;
2162     pTrustee->ptstrName = name;
2163 }
2164
2165 BOOL WINAPI SetAclInformation( PACL pAcl, LPVOID pAclInformation,
2166                                DWORD nAclInformationLength,
2167                                ACL_INFORMATION_CLASS dwAclInformationClass )
2168 {
2169     FIXME("%p %p 0x%08lx 0x%08x - stub\n", pAcl, pAclInformation,
2170           nAclInformationLength, dwAclInformationClass);
2171
2172     return TRUE;
2173 }
2174
2175 /******************************************************************************
2176  * SetEntriesInAclA [ADVAPI32.@]
2177  */
2178 DWORD WINAPI SetEntriesInAclA( ULONG count, PEXPLICIT_ACCESSA pEntries,
2179                                PACL OldAcl, PACL* NewAcl )
2180 {
2181     FIXME("%ld %p %p %p\n",count,pEntries,OldAcl,NewAcl);
2182     return ERROR_CALL_NOT_IMPLEMENTED;
2183 }
2184
2185 /******************************************************************************
2186  * SetEntriesInAclW [ADVAPI32.@]
2187  */
2188 DWORD WINAPI SetEntriesInAclW( ULONG count, PEXPLICIT_ACCESSW pEntries,
2189                                PACL OldAcl, PACL* NewAcl )
2190 {
2191     FIXME("%ld %p %p %p\n",count,pEntries,OldAcl,NewAcl);
2192     return ERROR_CALL_NOT_IMPLEMENTED;
2193 }
2194
2195 /******************************************************************************
2196  * SetNamedSecurityInfoA [ADVAPI32.@]
2197  */
2198 DWORD WINAPI SetNamedSecurityInfoA(LPSTR pObjectName,
2199         SE_OBJECT_TYPE ObjectType, SECURITY_INFORMATION SecurityInfo,
2200         PSID psidOwner, PSID psidGroup, PACL pDacl, PACL pSacl)
2201 {
2202     DWORD len;
2203     LPWSTR wstr = NULL;
2204     DWORD r;
2205
2206     TRACE("%s %d %ld %p %p %p %p\n", debugstr_a(pObjectName), ObjectType,
2207            SecurityInfo, psidOwner, psidGroup, pDacl, pSacl);
2208
2209     if( pObjectName )
2210     {
2211         len = MultiByteToWideChar( CP_ACP, 0, pObjectName, -1, NULL, 0 );
2212         wstr = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR));
2213         MultiByteToWideChar( CP_ACP, 0, pObjectName, -1, wstr, len );
2214     }
2215
2216     r = SetNamedSecurityInfoW( wstr, ObjectType, SecurityInfo, psidOwner,
2217                            psidGroup, pDacl, pSacl );
2218
2219     HeapFree( GetProcessHeap(), 0, wstr );
2220
2221     return r;
2222 }
2223
2224 BOOL WINAPI SetPrivateObjectSecurity( SECURITY_INFORMATION SecurityInformation,
2225     PSECURITY_DESCRIPTOR ModificationDescriptor,
2226     PSECURITY_DESCRIPTOR* ObjectsSecurityDescriptor,
2227     PGENERIC_MAPPING GenericMapping,
2228     HANDLE Token )
2229 {
2230     FIXME("0x%08lx %p %p %p %p - stub\n", SecurityInformation, ModificationDescriptor,
2231           ObjectsSecurityDescriptor, GenericMapping, Token);
2232
2233     return TRUE;
2234 }
2235
2236 BOOL WINAPI SetSecurityDescriptorControl( PSECURITY_DESCRIPTOR pSecurityDescriptor,
2237   SECURITY_DESCRIPTOR_CONTROL ControlBitsOfInterest,
2238   SECURITY_DESCRIPTOR_CONTROL ControlBitsToSet )
2239 {
2240     FIXME("%p 0x%08x 0x%08x - stub\n", pSecurityDescriptor, ControlBitsOfInterest,
2241           ControlBitsToSet);
2242
2243     return TRUE;
2244 }
2245
2246 BOOL WINAPI AreAllAccessesGranted( DWORD GrantedAccess, DWORD DesiredAccess )
2247 {
2248     return RtlAreAllAccessesGranted( GrantedAccess, DesiredAccess );
2249 }
2250
2251 /******************************************************************************
2252  * AreAnyAccessesGranted [ADVAPI32.@]
2253  *
2254  * Determines whether or not any of a set of specified access permissions have
2255  * been granted or not.
2256  *
2257  * PARAMS
2258  *   GrantedAccess [I] The permissions that have been granted.
2259  *   DesiredAccess [I] The permissions that you want to have.
2260  *
2261  * RETURNS
2262  *   Nonzero if any of the permissions have been granted, zero if none of the
2263  *   permissions have been granted.
2264  */
2265
2266 BOOL WINAPI AreAnyAccessesGranted( DWORD GrantedAccess, DWORD DesiredAccess )
2267 {
2268     return RtlAreAnyAccessesGranted( GrantedAccess, DesiredAccess );
2269 }
2270
2271 /******************************************************************************
2272  * SetNamedSecurityInfoW [ADVAPI32.@]
2273  */
2274 DWORD WINAPI SetNamedSecurityInfoW(LPWSTR pObjectName,
2275         SE_OBJECT_TYPE ObjectType, SECURITY_INFORMATION SecurityInfo,
2276         PSID psidOwner, PSID psidGroup, PACL pDacl, PACL pSacl)
2277 {
2278     FIXME("%s %d %ld %p %p %p %p\n", debugstr_w(pObjectName), ObjectType,
2279            SecurityInfo, psidOwner, psidGroup, pDacl, pSacl);
2280     return ERROR_CALL_NOT_IMPLEMENTED;
2281 }
2282
2283 /******************************************************************************
2284  * GetExplicitEntriesFromAclA [ADVAPI32.@]
2285  */
2286 DWORD WINAPI GetExplicitEntriesFromAclA( PACL pacl, PULONG pcCountOfExplicitEntries,
2287         PEXPLICIT_ACCESSA* pListOfExplicitEntries)
2288 {
2289     FIXME("%p %p %p\n",pacl, pcCountOfExplicitEntries, pListOfExplicitEntries);
2290     return ERROR_CALL_NOT_IMPLEMENTED;
2291 }
2292
2293 /******************************************************************************
2294  * GetExplicitEntriesFromAclW [ADVAPI32.@]
2295  */
2296 DWORD WINAPI GetExplicitEntriesFromAclW( PACL pacl, PULONG pcCountOfExplicitEntries,
2297         PEXPLICIT_ACCESSW* pListOfExplicitEntries)
2298 {
2299     FIXME("%p %p %p\n",pacl, pcCountOfExplicitEntries, pListOfExplicitEntries);
2300     return ERROR_CALL_NOT_IMPLEMENTED;
2301 }
2302
2303
2304 /******************************************************************************
2305  * ParseAclStringFlags
2306  */
2307 static DWORD ParseAclStringFlags(LPCWSTR* StringAcl)
2308 {
2309     DWORD flags = 0;
2310     LPCWSTR szAcl = *StringAcl;
2311
2312     while (*szAcl != '(')
2313     {
2314         if (*szAcl == 'P')
2315         {
2316             flags |= SE_DACL_PROTECTED;
2317         }
2318         else if (*szAcl == 'A')
2319         {
2320             szAcl++;
2321             if (*szAcl == 'R')
2322                 flags |= SE_DACL_AUTO_INHERIT_REQ;
2323             else if (*szAcl == 'I')
2324                 flags |= SE_DACL_AUTO_INHERITED;
2325         }
2326         szAcl++;
2327     }
2328
2329     *StringAcl = szAcl;
2330     return flags;
2331 }
2332
2333 /******************************************************************************
2334  * ParseAceStringType
2335  */
2336 ACEFLAG AceType[] =
2337 {
2338     { SDDL_ACCESS_ALLOWED, ACCESS_ALLOWED_ACE_TYPE },
2339     { SDDL_ALARM,          SYSTEM_ALARM_ACE_TYPE },
2340     { SDDL_AUDIT,          SYSTEM_AUDIT_ACE_TYPE },
2341     { SDDL_ACCESS_DENIED,  ACCESS_DENIED_ACE_TYPE },
2342     /*
2343     { SDDL_OBJECT_ACCESS_ALLOWED, ACCESS_ALLOWED_OBJECT_ACE_TYPE },
2344     { SDDL_OBJECT_ACCESS_DENIED,  ACCESS_DENIED_OBJECT_ACE_TYPE },
2345     { SDDL_OBJECT_ALARM,          SYSTEM_ALARM_OBJECT_ACE_TYPE },
2346     { SDDL_OBJECT_AUDIT,          SYSTEM_AUDIT_OBJECT_ACE_TYPE },
2347     */
2348     { NULL, 0 },
2349 };
2350
2351 static BYTE ParseAceStringType(LPCWSTR* StringAcl)
2352 {
2353     UINT len = 0;
2354     LPCWSTR szAcl = *StringAcl;
2355     LPACEFLAG lpaf = AceType;
2356
2357     while (lpaf->wstr &&
2358         (len = strlenW(lpaf->wstr)) &&
2359         strncmpW(lpaf->wstr, szAcl, len))
2360         lpaf++;
2361
2362     if (!lpaf->wstr)
2363         return 0;
2364
2365     *StringAcl += len;
2366     return lpaf->value;
2367 }
2368
2369
2370 /******************************************************************************
2371  * ParseAceStringFlags
2372  */
2373 ACEFLAG AceFlags[] =
2374 {
2375     { SDDL_CONTAINER_INHERIT, CONTAINER_INHERIT_ACE },
2376     { SDDL_AUDIT_FAILURE,     FAILED_ACCESS_ACE_FLAG },
2377     { SDDL_INHERITED,         INHERITED_ACE },
2378     { SDDL_INHERIT_ONLY,      INHERIT_ONLY_ACE },
2379     { SDDL_NO_PROPAGATE,      NO_PROPAGATE_INHERIT_ACE },
2380     { SDDL_OBJECT_INHERIT,    OBJECT_INHERIT_ACE },
2381     { SDDL_AUDIT_SUCCESS,     SUCCESSFUL_ACCESS_ACE_FLAG },
2382     { NULL, 0 },
2383 };
2384
2385 static BYTE ParseAceStringFlags(LPCWSTR* StringAcl)
2386 {
2387     UINT len = 0;
2388     BYTE flags = 0;
2389     LPCWSTR szAcl = *StringAcl;
2390
2391     while (*szAcl != ';')
2392     {
2393         LPACEFLAG lpaf = AceFlags;
2394
2395         while (lpaf->wstr &&
2396                (len = strlenW(lpaf->wstr)) &&
2397                strncmpW(lpaf->wstr, szAcl, len))
2398             lpaf++;
2399
2400         if (!lpaf->wstr)
2401             return 0;
2402
2403         flags |= lpaf->value;
2404         szAcl += len;
2405     }
2406
2407     *StringAcl = szAcl;
2408     return flags;
2409 }
2410
2411
2412 /******************************************************************************
2413  * ParseAceStringRights
2414  */
2415 ACEFLAG AceRights[] =
2416 {
2417     { SDDL_GENERIC_ALL,     GENERIC_ALL },
2418     { SDDL_GENERIC_READ,    GENERIC_READ },
2419     { SDDL_GENERIC_WRITE,   GENERIC_WRITE },
2420     { SDDL_GENERIC_EXECUTE, GENERIC_EXECUTE },
2421     { SDDL_READ_CONTROL,    READ_CONTROL },
2422     { SDDL_STANDARD_DELETE, DELETE },
2423     { SDDL_WRITE_DAC,       WRITE_DAC },
2424     { SDDL_WRITE_OWNER,     WRITE_OWNER },
2425     { NULL, 0 },
2426 };
2427
2428 static DWORD ParseAceStringRights(LPCWSTR* StringAcl)
2429 {
2430     UINT len = 0;
2431     DWORD rights = 0;
2432     LPCWSTR szAcl = *StringAcl;
2433
2434     if ((*szAcl == '0') && (*(szAcl + 1) == 'x'))
2435     {
2436         LPCWSTR p = szAcl;
2437
2438         while (*p && *p != ';')
2439             p++;
2440
2441         if (p - szAcl <= 8)
2442         {
2443             rights = strtoulW(szAcl, NULL, 16);
2444             *StringAcl = p;
2445         }
2446         else
2447             WARN("Invalid rights string format: %s\n", debugstr_wn(szAcl, p - szAcl));
2448     }
2449     else
2450     {
2451         while (*szAcl != ';')
2452         {
2453             LPACEFLAG lpaf = AceRights;
2454
2455             while (lpaf->wstr &&
2456                (len = strlenW(lpaf->wstr)) &&
2457                strncmpW(lpaf->wstr, szAcl, len))
2458             {
2459                lpaf++;
2460             }
2461
2462             if (!lpaf->wstr)
2463                 return 0;
2464
2465             rights |= lpaf->value;
2466             szAcl += len;
2467         }
2468     }
2469
2470     *StringAcl = szAcl;
2471     return rights;
2472 }
2473
2474
2475 /******************************************************************************
2476  * ParseStringAclToAcl
2477  * 
2478  * dacl_flags(string_ace1)(string_ace2)... (string_acen) 
2479  */
2480 static BOOL ParseStringAclToAcl(LPCWSTR StringAcl, LPDWORD lpdwFlags, 
2481     PACL pAcl, LPDWORD cBytes)
2482 {
2483     DWORD val;
2484     DWORD sidlen;
2485     DWORD length = sizeof(ACL);
2486     PACCESS_ALLOWED_ACE pAce = NULL; /* pointer to current ACE */
2487
2488     TRACE("%s\n", debugstr_w(StringAcl));
2489
2490     if (!StringAcl)
2491         return FALSE;
2492
2493     if (pAcl) /* pAce is only useful if we're setting values */
2494         pAce = (PACCESS_ALLOWED_ACE) ((LPBYTE)pAcl + sizeof(PACL));
2495
2496     /* Parse ACL flags */
2497     *lpdwFlags = ParseAclStringFlags(&StringAcl);
2498
2499     /* Parse ACE */
2500     while (*StringAcl == '(')
2501     {
2502         StringAcl++;
2503
2504         /* Parse ACE type */
2505         val = ParseAceStringType(&StringAcl);
2506         if (pAce)
2507             pAce->Header.AceType = (BYTE) val;
2508         if (*StringAcl != ';')
2509             goto lerr;
2510         StringAcl++;
2511
2512         /* Parse ACE flags */
2513         val = ParseAceStringFlags(&StringAcl);
2514         if (pAce)
2515             pAce->Header.AceFlags = (BYTE) val;
2516         if (*StringAcl != ';')
2517             goto lerr;
2518         StringAcl++;
2519
2520         /* Parse ACE rights */
2521         val = ParseAceStringRights(&StringAcl);
2522         if (pAce)
2523             pAce->Mask = val;
2524         if (*StringAcl != ';')
2525             goto lerr;
2526         StringAcl++;
2527
2528         /* Parse ACE object guid */
2529         if (*StringAcl != ';')
2530         {
2531             FIXME("Support for *_OBJECT_ACE_TYPE not implemented\n");
2532             goto lerr;
2533         }
2534         StringAcl++;
2535
2536         /* Parse ACE inherit object guid */
2537         if (*StringAcl != ';')
2538         {
2539             FIXME("Support for *_OBJECT_ACE_TYPE not implemented\n");
2540             goto lerr;
2541         }
2542         StringAcl++;
2543
2544         /* Parse ACE account sid */
2545         if (ParseStringSidToSid(StringAcl, pAce ? (PSID)&pAce->SidStart : NULL, &sidlen))
2546         {
2547             while (*StringAcl && *StringAcl != ')')
2548                 StringAcl++;
2549         }
2550
2551         if (*StringAcl != ')')
2552             goto lerr;
2553         StringAcl++;
2554
2555         length += sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD) + sidlen;
2556     }
2557
2558     *cBytes = length;
2559     return TRUE;
2560
2561 lerr:
2562     WARN("Invalid ACE string format\n");
2563     return FALSE;
2564 }
2565
2566
2567 /******************************************************************************
2568  * ParseStringSecurityDescriptorToSecurityDescriptor
2569  */
2570 static BOOL ParseStringSecurityDescriptorToSecurityDescriptor(
2571     LPCWSTR StringSecurityDescriptor,
2572     SECURITY_DESCRIPTOR* SecurityDescriptor,
2573     LPDWORD cBytes)
2574 {
2575     BOOL bret = FALSE;
2576     WCHAR toktype;
2577     WCHAR tok[MAX_PATH];
2578     LPCWSTR lptoken;
2579     LPBYTE lpNext = NULL;
2580
2581     *cBytes = 0;
2582
2583     if (SecurityDescriptor)
2584         lpNext = ((LPBYTE) SecurityDescriptor) + sizeof(SECURITY_DESCRIPTOR);
2585
2586     while (*StringSecurityDescriptor)
2587     {
2588         toktype = *StringSecurityDescriptor;
2589
2590         /* Expect char identifier followed by ':' */
2591         StringSecurityDescriptor++;
2592         if (*StringSecurityDescriptor != ':')
2593         {
2594             SetLastError(ERROR_INVALID_PARAMETER);
2595             goto lend;
2596         }
2597         StringSecurityDescriptor++;
2598
2599         /* Extract token */
2600         lptoken = StringSecurityDescriptor;
2601         while (*lptoken && *lptoken != ':')
2602             lptoken++;
2603
2604         if (*lptoken)
2605             lptoken--;
2606
2607         strncpyW(tok, StringSecurityDescriptor, lptoken - StringSecurityDescriptor);
2608
2609         switch (toktype)
2610         {
2611             case 'O':
2612             {
2613                 DWORD bytes;
2614
2615                 if (!ParseStringSidToSid(tok, (PSID)lpNext, &bytes))
2616                     goto lend;
2617
2618                 if (SecurityDescriptor)
2619                 {
2620                     SecurityDescriptor->Owner = (PSID) ((DWORD) lpNext -
2621                         (DWORD) SecurityDescriptor);
2622                     lpNext += bytes; /* Advance to next token */
2623                 }
2624
2625                 *cBytes += bytes;
2626
2627                 break;
2628             }
2629
2630             case 'G':
2631             {
2632                 DWORD bytes;
2633
2634                 if (!ParseStringSidToSid(tok, (PSID)lpNext, &bytes))
2635                     goto lend;
2636
2637                 if (SecurityDescriptor)
2638                 {
2639                     SecurityDescriptor->Group = (PSID) ((DWORD) lpNext - 
2640                         (DWORD) SecurityDescriptor);
2641                     lpNext += bytes; /* Advance to next token */
2642                 }
2643
2644                 *cBytes += bytes;
2645
2646                 break;
2647             }
2648
2649             case 'D':
2650             {
2651                 DWORD flags;
2652                 DWORD bytes;
2653
2654                 if (!ParseStringAclToAcl(tok, &flags, (PACL)lpNext, &bytes))
2655                     goto lend;
2656
2657                 if (SecurityDescriptor)
2658                 {
2659                     SecurityDescriptor->Control |= SE_DACL_PRESENT | flags;
2660                     SecurityDescriptor->Dacl = (PACL) ((DWORD) lpNext -
2661                         (DWORD) SecurityDescriptor);
2662                     lpNext += bytes; /* Advance to next token */
2663                 }
2664
2665                 *cBytes += bytes;
2666
2667                 break;
2668             }
2669
2670             case 'S':
2671             {
2672                 DWORD flags;
2673                 DWORD bytes;
2674
2675                 if (!ParseStringAclToAcl(tok, &flags, (PACL)lpNext, &bytes))
2676                     goto lend;
2677
2678                 if (SecurityDescriptor)
2679                 {
2680                     SecurityDescriptor->Control |= SE_SACL_PRESENT | flags;
2681                     SecurityDescriptor->Sacl = (PACL) ((DWORD) lpNext -
2682                         (DWORD) SecurityDescriptor);
2683                     lpNext += bytes; /* Advance to next token */
2684                 }
2685
2686                 *cBytes += bytes;
2687
2688                 break;
2689             }
2690
2691             default:
2692                 FIXME("Unknown token\n");
2693                 SetLastError(ERROR_INVALID_PARAMETER);
2694                 goto lend;
2695         }
2696
2697         StringSecurityDescriptor = lptoken;
2698     }
2699
2700     bret = TRUE;
2701
2702 lend:
2703     return bret;
2704 }
2705
2706 /******************************************************************************
2707  * ConvertStringSecurityDescriptorToSecurityDescriptorA [ADVAPI32.@]
2708  */
2709 BOOL WINAPI ConvertStringSecurityDescriptorToSecurityDescriptorA(
2710         LPCSTR StringSecurityDescriptor,
2711         DWORD StringSDRevision,
2712         PSECURITY_DESCRIPTOR* SecurityDescriptor,
2713         PULONG SecurityDescriptorSize)
2714 {
2715     UINT len;
2716     BOOL ret = FALSE;
2717     LPWSTR StringSecurityDescriptorW;
2718
2719     len = MultiByteToWideChar(CP_ACP, 0, StringSecurityDescriptor, -1, NULL, 0);
2720     StringSecurityDescriptorW = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
2721
2722     if (StringSecurityDescriptorW)
2723     {
2724         MultiByteToWideChar(CP_ACP, 0, StringSecurityDescriptor, -1, StringSecurityDescriptorW, len);
2725
2726         ret = ConvertStringSecurityDescriptorToSecurityDescriptorW(StringSecurityDescriptorW,
2727                                                                    StringSDRevision, SecurityDescriptor,
2728                                                                    SecurityDescriptorSize);
2729         HeapFree(GetProcessHeap(), 0, StringSecurityDescriptorW);
2730     }
2731
2732     return ret;
2733 }
2734
2735 /******************************************************************************
2736  * ConvertStringSecurityDescriptorToSecurityDescriptorW [ADVAPI32.@]
2737  */
2738 BOOL WINAPI ConvertStringSecurityDescriptorToSecurityDescriptorW(
2739         LPCWSTR StringSecurityDescriptor,
2740         DWORD StringSDRevision,
2741         PSECURITY_DESCRIPTOR* SecurityDescriptor,
2742         PULONG SecurityDescriptorSize)
2743 {
2744     DWORD cBytes;
2745     SECURITY_DESCRIPTOR* psd;
2746     BOOL bret = FALSE;
2747
2748     TRACE("%s\n", debugstr_w(StringSecurityDescriptor));
2749
2750     if (GetVersion() & 0x80000000)
2751     {
2752         SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2753         goto lend;
2754     }
2755     else if (StringSDRevision != SID_REVISION)
2756     {
2757         SetLastError(ERROR_UNKNOWN_REVISION);
2758         goto lend;
2759     }
2760
2761     /* Compute security descriptor length */
2762     if (!ParseStringSecurityDescriptorToSecurityDescriptor(StringSecurityDescriptor,
2763         NULL, &cBytes))
2764         goto lend;
2765
2766     psd = *SecurityDescriptor = (SECURITY_DESCRIPTOR*) LocalAlloc(
2767         GMEM_ZEROINIT, cBytes);
2768
2769     psd->Revision = SID_REVISION;
2770     psd->Control |= SE_SELF_RELATIVE;
2771
2772     if (!ParseStringSecurityDescriptorToSecurityDescriptor(StringSecurityDescriptor,
2773         psd, &cBytes))
2774     {
2775         LocalFree(psd);
2776         goto lend;
2777     }
2778
2779     if (SecurityDescriptorSize)
2780         *SecurityDescriptorSize = cBytes;
2781
2782     bret = TRUE;
2783  
2784 lend:
2785     TRACE(" ret=%d\n", bret);
2786     return bret;
2787 }
2788
2789 /******************************************************************************
2790  * ConvertStringSidToSidW [ADVAPI32.@]
2791  */
2792 BOOL WINAPI ConvertStringSidToSidW(LPCWSTR StringSid, PSID* Sid)
2793 {
2794     BOOL bret = FALSE;
2795     DWORD cBytes;
2796
2797     TRACE("%s, %p\n", debugstr_w(StringSid), Sid);
2798     if (GetVersion() & 0x80000000)
2799         SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2800     else if (!StringSid || !Sid)
2801         SetLastError(ERROR_INVALID_PARAMETER);
2802     else if (ParseStringSidToSid(StringSid, NULL, &cBytes))
2803     {
2804         PSID pSid = *Sid = (PSID) LocalAlloc(0, cBytes);
2805
2806         bret = ParseStringSidToSid(StringSid, pSid, &cBytes);
2807         if (!bret)
2808             LocalFree(*Sid); 
2809     }
2810     TRACE("returning %s\n", bret ? "TRUE" : "FALSE");
2811     return bret;
2812 }
2813
2814 /******************************************************************************
2815  * ConvertStringSidToSidA [ADVAPI32.@]
2816  */
2817 BOOL WINAPI ConvertStringSidToSidA(LPCSTR StringSid, PSID* Sid)
2818 {
2819     BOOL bret = FALSE;
2820
2821     TRACE("%s, %p\n", debugstr_a(StringSid), Sid);
2822     if (GetVersion() & 0x80000000)
2823         SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2824     else if (!StringSid || !Sid)
2825         SetLastError(ERROR_INVALID_PARAMETER);
2826     else
2827     {
2828         UINT len = MultiByteToWideChar(CP_ACP, 0, StringSid, -1, NULL, 0);
2829         LPWSTR wStringSid = (LPWSTR)HeapAlloc(GetProcessHeap(), 0,
2830          len * sizeof(WCHAR));
2831
2832         MultiByteToWideChar(CP_ACP, 0, StringSid, -1, wStringSid, len);
2833         bret = ConvertStringSidToSidW(wStringSid, Sid);
2834         HeapFree(GetProcessHeap(), 0, wStringSid);
2835     }
2836     TRACE("returning %s\n", bret ? "TRUE" : "FALSE");
2837     return bret;
2838 }
2839
2840 /******************************************************************************
2841  * ConvertSidToStringSidW [ADVAPI32.@]
2842  *
2843  *  format of SID string is:
2844  *    S-<count>-<auth>-<subauth1>-<subauth2>-<subauth3>...
2845  *  where
2846  *    <rev> is the revision of the SID encoded as decimal
2847  *    <auth> is the identifier authority encoded as hex
2848  *    <subauthN> is the subauthority id encoded as decimal
2849  */
2850 BOOL WINAPI ConvertSidToStringSidW( PSID pSid, LPWSTR *pstr )
2851 {
2852     DWORD sz, i;
2853     LPWSTR str;
2854     WCHAR fmt[] = { 'S','-','%','u','-','%','d',0 };
2855     WCHAR subauthfmt[] = { '-','%','u',0 };
2856     SID* pisid=pSid;
2857
2858     TRACE("%p %p\n", pSid, pstr );
2859
2860     if( !IsValidSid( pSid ) )
2861         return FALSE;
2862
2863     if (pisid->Revision != SDDL_REVISION)
2864         return FALSE;
2865     if (pisid->IdentifierAuthority.Value[0] ||
2866      pisid->IdentifierAuthority.Value[1])
2867     {
2868         FIXME("not matching MS' bugs\n");
2869         return FALSE;
2870     }
2871
2872     sz = 14 + pisid->SubAuthorityCount * 11;
2873     str = LocalAlloc( 0, sz*sizeof(WCHAR) );
2874     sprintfW( str, fmt, pisid->Revision, MAKELONG(
2875      MAKEWORD( pisid->IdentifierAuthority.Value[5],
2876      pisid->IdentifierAuthority.Value[4] ),
2877      MAKEWORD( pisid->IdentifierAuthority.Value[3],
2878      pisid->IdentifierAuthority.Value[2] ) ) );
2879     for( i=0; i<pisid->SubAuthorityCount; i++ )
2880         sprintfW( str + strlenW(str), subauthfmt, pisid->SubAuthority[i] );
2881     *pstr = str;
2882
2883     return TRUE;
2884 }
2885
2886 /******************************************************************************
2887  * ConvertSidToStringSidA [ADVAPI32.@]
2888  */
2889 BOOL WINAPI ConvertSidToStringSidA(PSID pSid, LPSTR *pstr)
2890 {
2891     LPWSTR wstr = NULL;
2892     LPSTR str;
2893     UINT len;
2894
2895     TRACE("%p %p\n", pSid, pstr );
2896
2897     if( !ConvertSidToStringSidW( pSid, &wstr ) )
2898         return FALSE;
2899
2900     len = WideCharToMultiByte( CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL );
2901     str = LocalAlloc( 0, len );
2902     WideCharToMultiByte( CP_ACP, 0, wstr, -1, str, len, NULL, NULL );
2903     LocalFree( wstr );
2904
2905     *pstr = str;
2906
2907     return TRUE;
2908 }
2909
2910 BOOL WINAPI CreatePrivateObjectSecurity(
2911         PSECURITY_DESCRIPTOR ParentDescriptor,
2912         PSECURITY_DESCRIPTOR CreatorDescriptor,
2913         PSECURITY_DESCRIPTOR* NewDescriptor,
2914         BOOL IsDirectoryObject,
2915         HANDLE Token,
2916         PGENERIC_MAPPING GenericMapping )
2917 {
2918     FIXME("%p %p %p %d %p %p - stub\n", ParentDescriptor, CreatorDescriptor,
2919           NewDescriptor, IsDirectoryObject, Token, GenericMapping);
2920
2921     return FALSE;
2922 }
2923
2924 BOOL WINAPI DestroyPrivateObjectSecurity( PSECURITY_DESCRIPTOR* ObjectDescriptor )
2925 {
2926     FIXME("%p - stub\n", ObjectDescriptor);
2927
2928     return TRUE;
2929 }
2930
2931 BOOL WINAPI CreateProcessAsUserA(
2932         HANDLE hToken,
2933         LPCSTR lpApplicationName,
2934         LPSTR lpCommandLine,
2935         LPSECURITY_ATTRIBUTES lpProcessAttributes,
2936         LPSECURITY_ATTRIBUTES lpThreadAttributes,
2937         BOOL bInheritHandles,
2938         DWORD dwCreationFlags,
2939         LPVOID lpEnvironment,
2940         LPCSTR lpCurrentDirectory,
2941         LPSTARTUPINFOA lpStartupInfo,
2942         LPPROCESS_INFORMATION lpProcessInformation )
2943 {
2944     FIXME("%p %s %s %p %p %d 0x%08lx %p %s %p %p - stub\n", hToken, debugstr_a(lpApplicationName),
2945           debugstr_a(lpCommandLine), lpProcessAttributes, lpThreadAttributes, bInheritHandles,
2946           dwCreationFlags, lpEnvironment, debugstr_a(lpCurrentDirectory), lpStartupInfo, lpProcessInformation);
2947
2948     return FALSE;
2949 }
2950
2951 BOOL WINAPI CreateProcessAsUserW(
2952         HANDLE hToken,
2953         LPCWSTR lpApplicationName,
2954         LPWSTR lpCommandLine,
2955         LPSECURITY_ATTRIBUTES lpProcessAttributes,
2956         LPSECURITY_ATTRIBUTES lpThreadAttributes,
2957         BOOL bInheritHandles,
2958         DWORD dwCreationFlags,
2959         LPVOID lpEnvironment,
2960         LPCWSTR lpCurrentDirectory,
2961         LPSTARTUPINFOW lpStartupInfo,
2962         LPPROCESS_INFORMATION lpProcessInformation )
2963 {
2964     FIXME("%p %s %s %p %p %d 0x%08lx %p %s %p %p - stub\n", hToken, debugstr_w(lpApplicationName),
2965           debugstr_w(lpCommandLine), lpProcessAttributes, lpThreadAttributes, bInheritHandles,
2966           dwCreationFlags, lpEnvironment, debugstr_w(lpCurrentDirectory), lpStartupInfo, lpProcessInformation);
2967
2968     return FALSE;
2969 }
2970
2971 /******************************************************************************
2972  * ComputeStringSidSize
2973  */
2974 static DWORD ComputeStringSidSize(LPCWSTR StringSid)
2975 {
2976     int ctok = 0;
2977     DWORD size = sizeof(SID);
2978
2979     while (*StringSid)
2980     {
2981         if (*StringSid == '-')
2982             ctok++;
2983         StringSid++;
2984     }
2985
2986     if (ctok > 3)
2987         size += (ctok - 3) * sizeof(DWORD);
2988
2989     return size;
2990 }
2991
2992 BOOL WINAPI DuplicateTokenEx(
2993         HANDLE ExistingTokenHandle, DWORD dwDesiredAccess,
2994         LPSECURITY_ATTRIBUTES lpTokenAttributes,
2995         SECURITY_IMPERSONATION_LEVEL ImpersonationLevel,
2996         TOKEN_TYPE TokenType,
2997         PHANDLE DuplicateTokenHandle )
2998 {
2999     FIXME("%p 0x%08lx 0x%08x 0x%08x %p - stub\n", ExistingTokenHandle, dwDesiredAccess,
3000           ImpersonationLevel, TokenType, DuplicateTokenHandle);
3001
3002     return FALSE;
3003 }
3004
3005 BOOL WINAPI DuplicateToken(
3006         HANDLE ExistingTokenHandle,
3007         SECURITY_IMPERSONATION_LEVEL ImpersonationLevel,
3008         PHANDLE DuplicateTokenHandle )
3009 {
3010     return DuplicateTokenEx( ExistingTokenHandle, 0, NULL, ImpersonationLevel,
3011                              TokenImpersonation, DuplicateTokenHandle );
3012 }
3013
3014 BOOL WINAPI EnumDependentServicesA(
3015         SC_HANDLE hService,
3016         DWORD dwServiceState,
3017         LPENUM_SERVICE_STATUSA lpServices,
3018         DWORD cbBufSize,
3019         LPDWORD pcbBytesNeeded,
3020         LPDWORD lpServicesReturned )
3021 {
3022     FIXME("%p 0x%08lx %p 0x%08lx %p %p - stub\n", hService, dwServiceState,
3023           lpServices, cbBufSize, pcbBytesNeeded, lpServicesReturned);
3024
3025     return FALSE;
3026 }
3027
3028 BOOL WINAPI EnumDependentServicesW(
3029         SC_HANDLE hService,
3030         DWORD dwServiceState,
3031         LPENUM_SERVICE_STATUSW lpServices,
3032         DWORD cbBufSize,
3033         LPDWORD pcbBytesNeeded,
3034         LPDWORD lpServicesReturned )
3035 {
3036     FIXME("%p 0x%08lx %p 0x%08lx %p %p - stub\n", hService, dwServiceState,
3037           lpServices, cbBufSize, pcbBytesNeeded, lpServicesReturned);
3038
3039     return FALSE;
3040 }
3041
3042 /******************************************************************************
3043  * ParseStringSidToSid
3044  */
3045 static BOOL ParseStringSidToSid(LPCWSTR StringSid, PSID pSid, LPDWORD cBytes)
3046 {
3047     BOOL bret = FALSE;
3048     SID* pisid=pSid;
3049
3050     TRACE("%s, %p, %p\n", debugstr_w(StringSid), pSid, cBytes);
3051     if (!StringSid)
3052     {
3053         SetLastError(ERROR_INVALID_PARAMETER);
3054         TRACE("StringSid is NULL, returning FALSE\n");
3055         return FALSE;
3056     }
3057
3058     *cBytes = ComputeStringSidSize(StringSid);
3059     if (!pisid) /* Simply compute the size */
3060     {
3061         TRACE("only size requested, returning TRUE\n");
3062         return TRUE;
3063     }
3064
3065     if (*StringSid != 'S' || *StringSid != '-') /* S-R-I-S-S */
3066     {
3067         DWORD i = 0, identAuth;
3068         DWORD csubauth = ((*cBytes - sizeof(SID)) / sizeof(DWORD)) + 1;
3069
3070         StringSid += 2; /* Advance to Revision */
3071         pisid->Revision = atoiW(StringSid);
3072
3073         if (pisid->Revision != SDDL_REVISION)
3074         {
3075             TRACE("Revision %d is unknown\n", pisid->Revision);
3076             goto lend; /* ERROR_INVALID_SID */
3077         }
3078         if (csubauth == 0)
3079         {
3080             TRACE("SubAuthorityCount is 0\n");
3081             goto lend; /* ERROR_INVALID_SID */
3082         }
3083
3084         pisid->SubAuthorityCount = csubauth;
3085
3086         /* Advance to identifier authority */
3087         while (*StringSid && *StringSid != '-')
3088             StringSid++;
3089         if (*StringSid == '-')
3090             StringSid++;
3091
3092         /* MS' implementation can't handle values greater than 2^32 - 1, so
3093          * we don't either; assume most significant bytes are always 0
3094          */
3095         pisid->IdentifierAuthority.Value[0] = 0;
3096         pisid->IdentifierAuthority.Value[1] = 0;
3097         identAuth = atoiW(StringSid);
3098         pisid->IdentifierAuthority.Value[5] = identAuth & 0xff;
3099         pisid->IdentifierAuthority.Value[4] = (identAuth & 0xff00) >> 8;
3100         pisid->IdentifierAuthority.Value[3] = (identAuth & 0xff0000) >> 16;
3101         pisid->IdentifierAuthority.Value[2] = (identAuth & 0xff000000) >> 24;
3102
3103         /* Advance to first sub authority */
3104         while (*StringSid && *StringSid != '-')
3105             StringSid++;
3106         if (*StringSid == '-')
3107             StringSid++;
3108
3109         while (*StringSid)
3110         {       
3111             while (*StringSid && *StringSid != '-')
3112                 StringSid++;
3113
3114             pisid->SubAuthority[i++] = atoiW(StringSid);
3115         }
3116
3117         if (i != pisid->SubAuthorityCount)
3118             goto lend; /* ERROR_INVALID_SID */
3119
3120         bret = TRUE;
3121     }
3122     else /* String constant format  - Only available in winxp and above */
3123     {
3124         pisid->Revision = SDDL_REVISION;
3125         pisid->SubAuthorityCount = 1;
3126
3127         FIXME("String constant not supported: %s\n", debugstr_wn(StringSid, 2));
3128
3129         /* TODO: Lookup string of well-known SIDs in table */
3130         pisid->IdentifierAuthority.Value[5] = 0;
3131         pisid->SubAuthority[0] = 0;
3132
3133         bret = TRUE;
3134     }
3135
3136 lend:
3137     if (!bret)
3138         SetLastError(ERROR_INVALID_SID);
3139
3140     TRACE("returning %s\n", bret ? "TRUE" : "FALSE");
3141     return bret;
3142 }
3143
3144 /******************************************************************************
3145  * GetNamedSecurityInfoA [ADVAPI32.@]
3146  */
3147 DWORD WINAPI GetNamedSecurityInfoA(LPSTR pObjectName,
3148         SE_OBJECT_TYPE ObjectType, SECURITY_INFORMATION SecurityInfo,
3149         PSID* ppsidOwner, PSID* ppsidGroup, PACL* ppDacl, PACL* ppSacl,
3150         PSECURITY_DESCRIPTOR* ppSecurityDescriptor)
3151 {
3152     DWORD len;
3153     LPWSTR wstr = NULL;
3154     DWORD r;
3155
3156     TRACE("%s %d %ld %p %p %p %p %p\n", pObjectName, ObjectType, SecurityInfo,
3157         ppsidOwner, ppsidGroup, ppDacl, ppSacl, ppSecurityDescriptor);
3158
3159     if( pObjectName )
3160     {
3161         len = MultiByteToWideChar( CP_ACP, 0, pObjectName, -1, NULL, 0 );
3162         wstr = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR));
3163         MultiByteToWideChar( CP_ACP, 0, pObjectName, -1, wstr, len );
3164     }
3165
3166     r = GetNamedSecurityInfoW( wstr, ObjectType, SecurityInfo, ppsidOwner,
3167                            ppsidGroup, ppDacl, ppSacl, ppSecurityDescriptor );
3168
3169     HeapFree( GetProcessHeap(), 0, wstr );
3170
3171     return r;
3172 }
3173
3174 /******************************************************************************
3175  * GetNamedSecurityInfoW [ADVAPI32.@]
3176  */
3177 DWORD WINAPI GetNamedSecurityInfoW(LPWSTR pObjectName,
3178         SE_OBJECT_TYPE ObjectType, SECURITY_INFORMATION SecurityInfo,
3179         PSID* ppsidOwner, PSID* ppsidGroup, PACL* ppDacl, PACL* ppSacl,
3180         PSECURITY_DESCRIPTOR* ppSecurityDescriptor)
3181 {
3182     FIXME("%s %d %ld %p %p %p %p %p\n", debugstr_w(pObjectName), ObjectType, SecurityInfo,
3183         ppsidOwner, ppsidGroup, ppDacl, ppSacl, ppSecurityDescriptor);
3184     return ERROR_CALL_NOT_IMPLEMENTED;
3185 }
3186
3187 /******************************************************************************
3188  * DecryptFileW [ADVAPI32.@]
3189  */
3190 BOOL WINAPI DecryptFileW(LPCWSTR lpFileName, DWORD dwReserved)
3191 {
3192     FIXME("%s %08lx\n", debugstr_w(lpFileName), dwReserved);
3193     return TRUE;
3194 }
3195
3196 /******************************************************************************
3197  * DecryptFileA [ADVAPI32.@]
3198  */
3199 BOOL WINAPI DecryptFileA(LPCSTR lpFileName, DWORD dwReserved)
3200 {
3201     FIXME("%s %08lx\n", debugstr_a(lpFileName), dwReserved);
3202     return TRUE;
3203 }
3204
3205 /******************************************************************************
3206  * EncryptFileW [ADVAPI32.@]
3207  */
3208 BOOL WINAPI EncryptFileW(LPCWSTR lpFileName)
3209 {
3210     FIXME("%s\n", debugstr_w(lpFileName));
3211     return TRUE;
3212 }
3213
3214 /******************************************************************************
3215  * EncryptFileA [ADVAPI32.@]
3216  */
3217 BOOL WINAPI EncryptFileA(LPCSTR lpFileName)
3218 {
3219     FIXME("%s\n", debugstr_a(lpFileName));
3220     return TRUE;
3221 }