fdopen: don't rewind the file after creating the FILE* handle. Added
[wine] / dlls / advapi32 / security.c
1 /*
2  * Copyright 1999, 2000 Juergen Schmied <juergen.schmied@debitel.net>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  *  FIXME: for all functions thunking down to Rtl* functions:  implement SetLastError()
19  */
20
21 #include <string.h>
22
23 #include "windef.h"
24 #include "winerror.h"
25 #include "rpcnterr.h"
26 #include "heap.h"
27 #include "winternl.h"
28 #include "ntsecapi.h"
29
30 #include "wine/debug.h"
31 #include "wine/unicode.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(advapi);
34
35 #define CallWin32ToNt(func) \
36         { NTSTATUS ret; \
37           ret = (func); \
38           if (ret !=STATUS_SUCCESS) \
39           { SetLastError (RtlNtStatusToDosError(ret)); return FALSE; } \
40           return TRUE; \
41         }
42
43 static void dumpLsaAttributes( PLSA_OBJECT_ATTRIBUTES oa )
44 {
45         if (oa)
46         {
47           TRACE("\n\tlength=%lu, rootdir=%p, objectname=%s\n\tattr=0x%08lx, sid=%p qos=%p\n",
48                 oa->Length, oa->RootDirectory,
49                 oa->ObjectName?debugstr_w(oa->ObjectName->Buffer):"null",
50                 oa->Attributes, oa->SecurityDescriptor, oa->SecurityQualityOfService);
51         }
52 }
53
54 /************************************************************
55  *                ADVAPI_IsLocalComputer
56  *
57  * Checks whether the server name indicates local machine.
58  */
59 BOOL ADVAPI_IsLocalComputer(LPCWSTR ServerName)
60 {
61     if (!ServerName)
62     {
63         return TRUE;
64     }
65     else
66     {
67         DWORD dwSize = MAX_COMPUTERNAME_LENGTH + 1;
68         BOOL Result;
69         LPWSTR buf;
70
71         buf = HeapAlloc(GetProcessHeap(), 0, dwSize * sizeof(WCHAR));
72         Result = GetComputerNameW(buf,  &dwSize);
73         if (Result && (ServerName[0] == '\\') && (ServerName[1] == '\\'))
74             ServerName += 2;
75         Result = Result && !lstrcmpW(ServerName, buf);
76         HeapFree(GetProcessHeap(), 0, buf);
77
78         return Result;
79     }
80 }
81
82 #define ADVAPI_ForceLocalComputer(ServerName, FailureCode) \
83     if (!ADVAPI_IsLocalComputer(ServerName)) \
84     { \
85         FIXME("Action Implemented for local computer only. " \
86               "Requested for server %s\n", debugstr_w(ServerName)); \
87         return FailureCode; \
88     }
89
90 /*      ##############################
91         ######  TOKEN FUNCTIONS ######
92         ##############################
93 */
94
95 /******************************************************************************
96  * OpenProcessToken                     [ADVAPI32.@]
97  * Opens the access token associated with a process
98  *
99  * PARAMS
100  *   ProcessHandle [I] Handle to process
101  *   DesiredAccess [I] Desired access to process
102  *   TokenHandle   [O] Pointer to handle of open access token
103  *
104  * RETURNS STD
105  */
106 BOOL WINAPI
107 OpenProcessToken( HANDLE ProcessHandle, DWORD DesiredAccess,
108                   HANDLE *TokenHandle )
109 {
110         CallWin32ToNt(NtOpenProcessToken( ProcessHandle, DesiredAccess, TokenHandle ));
111 }
112
113 /******************************************************************************
114  * OpenThreadToken [ADVAPI32.@]
115  *
116  * PARAMS
117  *   thread        []
118  *   desiredaccess []
119  *   openasself    []
120  *   thandle       []
121  */
122 BOOL WINAPI
123 OpenThreadToken( HANDLE ThreadHandle, DWORD DesiredAccess,
124                  BOOL OpenAsSelf, HANDLE *TokenHandle)
125 {
126         CallWin32ToNt (NtOpenThreadToken(ThreadHandle, DesiredAccess, OpenAsSelf, TokenHandle));
127 }
128
129 /******************************************************************************
130  * AdjustTokenPrivileges [ADVAPI32.@]
131  *
132  * PARAMS
133  *   TokenHandle          []
134  *   DisableAllPrivileges []
135  *   NewState             []
136  *   BufferLength         []
137  *   PreviousState        []
138  *   ReturnLength         []
139  */
140 BOOL WINAPI
141 AdjustTokenPrivileges( HANDLE TokenHandle, BOOL DisableAllPrivileges,
142                        LPVOID NewState, DWORD BufferLength,
143                        LPVOID PreviousState, LPDWORD ReturnLength )
144 {
145         CallWin32ToNt(NtAdjustPrivilegesToken(TokenHandle, DisableAllPrivileges, NewState, BufferLength, PreviousState, ReturnLength));
146 }
147
148 /******************************************************************************
149  * CheckTokenMembership [ADVAPI32.@]
150  *
151  * PARAMS
152  *   TokenHandle [I]
153  *   SidToCheck  [I]
154  *   IsMember    [O]
155  */
156 BOOL WINAPI
157 CheckTokenMembership( HANDLE TokenHandle, PSID SidToCheck,
158                       PBOOL IsMember )
159 {
160   FIXME("(%p %p %p) stub!\n", TokenHandle, SidToCheck, IsMember);
161
162   *IsMember = TRUE;
163   return(TRUE);
164 }
165
166 /******************************************************************************
167  * GetTokenInformation [ADVAPI32.@]
168  *
169  * PARAMS
170  *   token           [I]
171  *   tokeninfoclass  [I]
172  *   tokeninfo       [O]
173  *   tokeninfolength [I]
174  *   retlen          [O]
175  *
176  */
177 BOOL WINAPI
178 GetTokenInformation( HANDLE token, TOKEN_INFORMATION_CLASS tokeninfoclass,
179                      LPVOID tokeninfo, DWORD tokeninfolength, LPDWORD retlen )
180 {
181     TRACE("(%p, %s, %p, %ld, %p): \n",
182           token,
183           (tokeninfoclass == TokenUser) ? "TokenUser" :
184           (tokeninfoclass == TokenGroups) ? "TokenGroups" :
185           (tokeninfoclass == TokenPrivileges) ? "TokenPrivileges" :
186           (tokeninfoclass == TokenOwner) ? "TokenOwner" :
187           (tokeninfoclass == TokenPrimaryGroup) ? "TokenPrimaryGroup" :
188           (tokeninfoclass == TokenDefaultDacl) ? "TokenDefaultDacl" :
189           (tokeninfoclass == TokenSource) ? "TokenSource" :
190           (tokeninfoclass == TokenType) ? "TokenType" :
191           (tokeninfoclass == TokenImpersonationLevel) ? "TokenImpersonationLevel" :
192           (tokeninfoclass == TokenStatistics) ? "TokenStatistics" :
193           (tokeninfoclass == TokenRestrictedSids) ? "TokenRestrictedSids" :
194           (tokeninfoclass == TokenSessionId) ? "TokenSessionId" :
195           (tokeninfoclass == TokenGroupsAndPrivileges) ? "TokenGroupsAndPrivileges" :
196           (tokeninfoclass == TokenSessionReference) ? "TokenSessionReference" :
197           (tokeninfoclass == TokenSandBoxInert) ? "TokenSandBoxInert" :
198           "Unknown",
199           tokeninfo, tokeninfolength, retlen);
200     CallWin32ToNt (NtQueryInformationToken( token, tokeninfoclass, tokeninfo, tokeninfolength, retlen));
201 }
202
203 /******************************************************************************
204  * SetTokenInformation [ADVAPI32.@]
205  *
206  * PARAMS
207  *   token           [I]
208  *   tokeninfoclass  [I]
209  *   tokeninfo       [I]
210  *   tokeninfolength [I]
211  *
212  */
213 BOOL WINAPI
214 SetTokenInformation( HANDLE token, TOKEN_INFORMATION_CLASS tokeninfoclass,
215                      LPVOID tokeninfo, DWORD tokeninfolength )
216 {
217     FIXME("(%p, %s, %p, %ld): stub\n",
218           token,
219           (tokeninfoclass == TokenUser) ? "TokenUser" :
220           (tokeninfoclass == TokenGroups) ? "TokenGroups" :
221           (tokeninfoclass == TokenPrivileges) ? "TokenPrivileges" :
222           (tokeninfoclass == TokenOwner) ? "TokenOwner" :
223           (tokeninfoclass == TokenPrimaryGroup) ? "TokenPrimaryGroup" :
224           (tokeninfoclass == TokenDefaultDacl) ? "TokenDefaultDacl" :
225           (tokeninfoclass == TokenSource) ? "TokenSource" :
226           (tokeninfoclass == TokenType) ? "TokenType" :
227           (tokeninfoclass == TokenImpersonationLevel) ? "TokenImpersonationLevel" :
228           (tokeninfoclass == TokenStatistics) ? "TokenStatistics" :
229           (tokeninfoclass == TokenRestrictedSids) ? "TokenRestrictedSids" :
230           (tokeninfoclass == TokenSessionId) ? "TokenSessionId" :
231           (tokeninfoclass == TokenGroupsAndPrivileges) ? "TokenGroupsAndPrivileges" :
232           (tokeninfoclass == TokenSessionReference) ? "TokenSessionReference" :
233           (tokeninfoclass == TokenSandBoxInert) ? "TokenSandBoxInert" :
234           "Unknown",
235           tokeninfo, tokeninfolength);
236
237     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
238
239     return FALSE;
240 }
241
242 /*************************************************************************
243  * SetThreadToken [ADVAPI32.@]
244  *
245  * Assigns an "impersonation token" to a thread so it can assume the
246  * security privledges of another thread or process.  Can also remove
247  * a previously assigned token.  Only supported on NT - it's a stub
248  * exactly like this one on Win9X.
249  *
250  */
251
252 BOOL WINAPI SetThreadToken(PHANDLE thread, HANDLE token)
253 {
254     FIXME("(%p, %p): stub (NT impl. only)\n", thread, token);
255
256     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
257
258     return FALSE;
259 }
260
261 /*      ##############################
262         ######  SID FUNCTIONS   ######
263         ##############################
264 */
265
266 /******************************************************************************
267  * AllocateAndInitializeSid [ADVAPI32.@]
268  *
269  * PARAMS
270  *   pIdentifierAuthority []
271  *   nSubAuthorityCount   []
272  *   nSubAuthority0       []
273  *   nSubAuthority1       []
274  *   nSubAuthority2       []
275  *   nSubAuthority3       []
276  *   nSubAuthority4       []
277  *   nSubAuthority5       []
278  *   nSubAuthority6       []
279  *   nSubAuthority7       []
280  *   pSid                 []
281  */
282 BOOL WINAPI
283 AllocateAndInitializeSid( PSID_IDENTIFIER_AUTHORITY pIdentifierAuthority,
284                           BYTE nSubAuthorityCount,
285                           DWORD nSubAuthority0, DWORD nSubAuthority1,
286                           DWORD nSubAuthority2, DWORD nSubAuthority3,
287                           DWORD nSubAuthority4, DWORD nSubAuthority5,
288                           DWORD nSubAuthority6, DWORD nSubAuthority7,
289                           PSID *pSid )
290 {
291         CallWin32ToNt (RtlAllocateAndInitializeSid(
292                 pIdentifierAuthority, nSubAuthorityCount,
293                 nSubAuthority0, nSubAuthority1, nSubAuthority2, nSubAuthority3,
294                 nSubAuthority4, nSubAuthority5, nSubAuthority6, nSubAuthority7,
295                 pSid ));
296 }
297
298 /******************************************************************************
299  * FreeSid [ADVAPI32.@]
300  *
301  * PARAMS
302  *   pSid []
303  */
304 PVOID WINAPI
305 FreeSid( PSID pSid )
306 {
307         RtlFreeSid(pSid);
308         return NULL; /* is documented like this */
309 }
310
311 /******************************************************************************
312  * CopySid [ADVAPI32.@]
313  *
314  * PARAMS
315  *   nDestinationSidLength []
316  *   pDestinationSid       []
317  *   pSourceSid            []
318  */
319 BOOL WINAPI
320 CopySid( DWORD nDestinationSidLength, PSID pDestinationSid, PSID pSourceSid )
321 {
322         return RtlCopySid(nDestinationSidLength, pDestinationSid, pSourceSid);
323 }
324
325 /******************************************************************************
326  * IsValidSid [ADVAPI32.@]
327  *
328  * PARAMS
329  *   pSid []
330  */
331 BOOL WINAPI
332 IsValidSid( PSID pSid )
333 {
334         return RtlValidSid( pSid );
335 }
336
337 /******************************************************************************
338  * EqualSid [ADVAPI32.@]
339  *
340  * PARAMS
341  *   pSid1 []
342  *   pSid2 []
343  */
344 BOOL WINAPI
345 EqualSid( PSID pSid1, PSID pSid2 )
346 {
347         return RtlEqualSid( pSid1, pSid2 );
348 }
349
350 /******************************************************************************
351  * EqualPrefixSid [ADVAPI32.@]
352  */
353 BOOL WINAPI EqualPrefixSid (PSID pSid1, PSID pSid2)
354 {
355         return RtlEqualPrefixSid(pSid1, pSid2);
356 }
357
358 /******************************************************************************
359  * GetSidLengthRequired [ADVAPI32.@]
360  *
361  * PARAMS
362  *   nSubAuthorityCount []
363  */
364 DWORD WINAPI
365 GetSidLengthRequired( BYTE nSubAuthorityCount )
366 {
367         return RtlLengthRequiredSid(nSubAuthorityCount);
368 }
369
370 /******************************************************************************
371  * InitializeSid [ADVAPI32.@]
372  *
373  * PARAMS
374  *   pIdentifierAuthority []
375  */
376 BOOL WINAPI
377 InitializeSid (
378         PSID pSid,
379         PSID_IDENTIFIER_AUTHORITY pIdentifierAuthority,
380         BYTE nSubAuthorityCount)
381 {
382         return RtlInitializeSid(pSid, pIdentifierAuthority, nSubAuthorityCount);
383 }
384
385 /******************************************************************************
386  * GetSidIdentifierAuthority [ADVAPI32.@]
387  *
388  * PARAMS
389  *   pSid []
390  */
391 PSID_IDENTIFIER_AUTHORITY WINAPI
392 GetSidIdentifierAuthority( PSID pSid )
393 {
394         return RtlIdentifierAuthoritySid(pSid);
395 }
396
397 /******************************************************************************
398  * GetSidSubAuthority [ADVAPI32.@]
399  *
400  * PARAMS
401  *   pSid          []
402  *   nSubAuthority []
403  */
404 PDWORD WINAPI
405 GetSidSubAuthority( PSID pSid, DWORD nSubAuthority )
406 {
407         return RtlSubAuthoritySid(pSid, nSubAuthority);
408 }
409
410 /******************************************************************************
411  * GetSidSubAuthorityCount [ADVAPI32.@]
412  *
413  * PARAMS
414  *   pSid []
415  */
416 PUCHAR WINAPI
417 GetSidSubAuthorityCount (PSID pSid)
418 {
419         return RtlSubAuthorityCountSid(pSid);
420 }
421
422 /******************************************************************************
423  * GetLengthSid [ADVAPI32.@]
424  *
425  * PARAMS
426  *   pSid []
427  */
428 DWORD WINAPI
429 GetLengthSid (PSID pSid)
430 {
431         return RtlLengthSid(pSid);
432 }
433
434 /*      ##############################################
435         ######  SECURITY DESCRIPTOR FUNCTIONS   ######
436         ##############################################
437 */
438
439 /******************************************************************************
440  * InitializeSecurityDescriptor [ADVAPI32.@]
441  *
442  * PARAMS
443  *   pDescr   []
444  *   revision []
445  */
446 BOOL WINAPI
447 InitializeSecurityDescriptor( SECURITY_DESCRIPTOR *pDescr, DWORD revision )
448 {
449         CallWin32ToNt (RtlCreateSecurityDescriptor(pDescr, revision ));
450 }
451
452 /******************************************************************************
453  * GetSecurityDescriptorLength [ADVAPI32.@]
454  */
455 DWORD WINAPI GetSecurityDescriptorLength( SECURITY_DESCRIPTOR *pDescr)
456 {
457         return (RtlLengthSecurityDescriptor(pDescr));
458 }
459
460 /******************************************************************************
461  * GetSecurityDescriptorOwner [ADVAPI32.@]
462  *
463  * PARAMS
464  *   pOwner            []
465  *   lpbOwnerDefaulted []
466  */
467 BOOL WINAPI
468 GetSecurityDescriptorOwner( SECURITY_DESCRIPTOR *pDescr, PSID *pOwner,
469                             LPBOOL lpbOwnerDefaulted )
470 {
471         CallWin32ToNt (RtlGetOwnerSecurityDescriptor( pDescr, pOwner, (PBOOLEAN)lpbOwnerDefaulted ));
472 }
473
474 /******************************************************************************
475  * SetSecurityDescriptorOwner [ADVAPI32.@]
476  *
477  * PARAMS
478  */
479 BOOL WINAPI SetSecurityDescriptorOwner( PSECURITY_DESCRIPTOR pSecurityDescriptor,
480                                    PSID pOwner, BOOL bOwnerDefaulted)
481 {
482         CallWin32ToNt (RtlSetOwnerSecurityDescriptor(pSecurityDescriptor, pOwner, bOwnerDefaulted));
483 }
484 /******************************************************************************
485  * GetSecurityDescriptorGroup                   [ADVAPI32.@]
486  */
487 BOOL WINAPI GetSecurityDescriptorGroup(
488         PSECURITY_DESCRIPTOR SecurityDescriptor,
489         PSID *Group,
490         LPBOOL GroupDefaulted)
491 {
492         CallWin32ToNt (RtlGetGroupSecurityDescriptor(SecurityDescriptor, Group, (PBOOLEAN)GroupDefaulted));
493 }
494 /******************************************************************************
495  * SetSecurityDescriptorGroup [ADVAPI32.@]
496  */
497 BOOL WINAPI SetSecurityDescriptorGroup ( PSECURITY_DESCRIPTOR SecurityDescriptor,
498                                            PSID Group, BOOL GroupDefaulted)
499 {
500         CallWin32ToNt (RtlSetGroupSecurityDescriptor( SecurityDescriptor, Group, GroupDefaulted));
501 }
502
503 /******************************************************************************
504  * IsValidSecurityDescriptor [ADVAPI32.@]
505  *
506  * PARAMS
507  *   lpsecdesc []
508  */
509 BOOL WINAPI
510 IsValidSecurityDescriptor( PSECURITY_DESCRIPTOR SecurityDescriptor )
511 {
512         CallWin32ToNt (RtlValidSecurityDescriptor(SecurityDescriptor));
513 }
514
515 /******************************************************************************
516  *  GetSecurityDescriptorDacl                   [ADVAPI32.@]
517  */
518 BOOL WINAPI GetSecurityDescriptorDacl(
519         IN PSECURITY_DESCRIPTOR pSecurityDescriptor,
520         OUT LPBOOL lpbDaclPresent,
521         OUT PACL *pDacl,
522         OUT LPBOOL lpbDaclDefaulted)
523 {
524         CallWin32ToNt (RtlGetDaclSecurityDescriptor(pSecurityDescriptor, (PBOOLEAN)lpbDaclPresent,
525                                                pDacl, (PBOOLEAN)lpbDaclDefaulted));
526 }
527
528 /******************************************************************************
529  *  SetSecurityDescriptorDacl                   [ADVAPI32.@]
530  */
531 BOOL WINAPI
532 SetSecurityDescriptorDacl (
533         PSECURITY_DESCRIPTOR lpsd,
534         BOOL daclpresent,
535         PACL dacl,
536         BOOL dacldefaulted )
537 {
538         CallWin32ToNt (RtlSetDaclSecurityDescriptor (lpsd, daclpresent, dacl, dacldefaulted ));
539 }
540 /******************************************************************************
541  *  GetSecurityDescriptorSacl                   [ADVAPI32.@]
542  */
543 BOOL WINAPI GetSecurityDescriptorSacl(
544         IN PSECURITY_DESCRIPTOR lpsd,
545         OUT LPBOOL lpbSaclPresent,
546         OUT PACL *pSacl,
547         OUT LPBOOL lpbSaclDefaulted)
548 {
549         CallWin32ToNt (RtlGetSaclSecurityDescriptor(lpsd,
550            (PBOOLEAN)lpbSaclPresent, pSacl, (PBOOLEAN)lpbSaclDefaulted));
551 }
552
553 /**************************************************************************
554  * SetSecurityDescriptorSacl                    [ADVAPI32.@]
555  */
556 BOOL WINAPI SetSecurityDescriptorSacl (
557         PSECURITY_DESCRIPTOR lpsd,
558         BOOL saclpresent,
559         PACL lpsacl,
560         BOOL sacldefaulted)
561 {
562         CallWin32ToNt (RtlSetSaclSecurityDescriptor(lpsd, saclpresent, lpsacl, sacldefaulted));
563 }
564 /******************************************************************************
565  * MakeSelfRelativeSD [ADVAPI32.@]
566  *
567  * PARAMS
568  *   lpabssecdesc  []
569  *   lpselfsecdesc []
570  *   lpbuflen      []
571  */
572 BOOL WINAPI
573 MakeSelfRelativeSD(
574         IN PSECURITY_DESCRIPTOR pAbsoluteSecurityDescriptor,
575         IN PSECURITY_DESCRIPTOR pSelfRelativeSecurityDescriptor,
576         IN OUT LPDWORD lpdwBufferLength)
577 {
578         CallWin32ToNt (RtlMakeSelfRelativeSD(pAbsoluteSecurityDescriptor,pSelfRelativeSecurityDescriptor, lpdwBufferLength));
579 }
580
581 /******************************************************************************
582  * GetSecurityDescriptorControl                 [ADVAPI32.@]
583  */
584
585 BOOL WINAPI GetSecurityDescriptorControl ( PSECURITY_DESCRIPTOR  pSecurityDescriptor,
586                  PSECURITY_DESCRIPTOR_CONTROL pControl, LPDWORD lpdwRevision)
587 {
588         CallWin32ToNt (RtlGetControlSecurityDescriptor(pSecurityDescriptor,pControl,lpdwRevision));
589 }
590
591 /*      ##############################
592         ######  ACL FUNCTIONS   ######
593         ##############################
594 */
595
596 /*************************************************************************
597  * InitializeAcl [ADVAPI32.@]
598  */
599 DWORD WINAPI InitializeAcl(PACL acl, DWORD size, DWORD rev)
600 {
601         CallWin32ToNt (RtlCreateAcl(acl, size, rev));
602 }
603
604 /*      ##############################
605         ######  MISC FUNCTIONS  ######
606         ##############################
607 */
608
609 /******************************************************************************
610  * LookupPrivilegeValueW                        [ADVAPI32.@]
611  * Retrieves LUID used on a system to represent the privilege name.
612  *
613  * PARAMS
614  *   lpSystemName [I] Address of string specifying the system
615  *   lpName       [I] Address of string specifying the privilege
616  *   lpLuid       [I] Address of locally unique identifier
617  *
618  * RETURNS STD
619  */
620 BOOL WINAPI
621 LookupPrivilegeValueW( LPCWSTR lpSystemName, LPCWSTR lpName, PLUID lpLuid )
622 {
623     FIXME("(%s,%s,%p): stub\n",debugstr_w(lpSystemName),
624         debugstr_w(lpName), lpLuid);
625     lpLuid->LowPart = 0x12345678;
626     lpLuid->HighPart = 0x87654321;
627     return TRUE;
628 }
629
630 /******************************************************************************
631  * LookupPrivilegeValueA                        [ADVAPI32.@]
632  */
633 BOOL WINAPI
634 LookupPrivilegeValueA( LPCSTR lpSystemName, LPCSTR lpName, PLUID lpLuid )
635 {
636     LPWSTR lpSystemNameW = HEAP_strdupAtoW(GetProcessHeap(), 0, lpSystemName);
637     LPWSTR lpNameW = HEAP_strdupAtoW(GetProcessHeap(), 0, lpName);
638     BOOL ret;
639
640     ret = LookupPrivilegeValueW( lpSystemNameW, lpNameW, lpLuid);
641     HeapFree(GetProcessHeap(), 0, lpNameW);
642     HeapFree(GetProcessHeap(), 0, lpSystemNameW);
643     return ret;
644 }
645
646 /******************************************************************************
647  * GetFileSecurityA [ADVAPI32.@]
648  *
649  * Obtains Specified information about the security of a file or directory
650  * The information obtained is constrained by the callers access rights and
651  * privileges
652  */
653 BOOL WINAPI
654 GetFileSecurityA( LPCSTR lpFileName,
655                     SECURITY_INFORMATION RequestedInformation,
656                     PSECURITY_DESCRIPTOR pSecurityDescriptor,
657                     DWORD nLength, LPDWORD lpnLengthNeeded )
658 {
659   FIXME("(%s) : stub\n", debugstr_a(lpFileName));
660   return TRUE;
661 }
662
663 /******************************************************************************
664  * GetFileSecurityW [ADVAPI32.@]
665  *
666  * Obtains Specified information about the security of a file or directory
667  * The information obtained is constrained by the callers access rights and
668  * privileges
669  *
670  * PARAMS
671  *   lpFileName           []
672  *   RequestedInformation []
673  *   pSecurityDescriptor  []
674  *   nLength              []
675  *   lpnLengthNeeded      []
676  */
677 BOOL WINAPI
678 GetFileSecurityW( LPCWSTR lpFileName,
679                     SECURITY_INFORMATION RequestedInformation,
680                     PSECURITY_DESCRIPTOR pSecurityDescriptor,
681                     DWORD nLength, LPDWORD lpnLengthNeeded )
682 {
683   FIXME("(%s) : stub\n", debugstr_w(lpFileName) );
684   return TRUE;
685 }
686
687
688 /******************************************************************************
689  * LookupAccountSidA [ADVAPI32.@]
690  */
691 BOOL WINAPI
692 LookupAccountSidA(
693         IN LPCSTR system,
694         IN PSID sid,
695         OUT LPSTR account,
696         IN OUT LPDWORD accountSize,
697         OUT LPSTR domain,
698         IN OUT LPDWORD domainSize,
699         OUT PSID_NAME_USE name_use )
700 {
701         static const char ac[] = "Administrator";
702         static const char dm[] = "DOMAIN";
703         FIXME("(%s,sid=%p,%p,%p(%lu),%p,%p(%lu),%p): semi-stub\n",
704               debugstr_a(system),sid,
705               account,accountSize,accountSize?*accountSize:0,
706               domain,domainSize,domainSize?*domainSize:0,
707               name_use);
708
709         if (accountSize) *accountSize = strlen(ac)+1;
710         if (account && (*accountSize > strlen(ac)))
711           strcpy(account, ac);
712
713         if (domainSize) *domainSize = strlen(dm)+1;
714         if (domain && (*domainSize > strlen(dm)))
715           strcpy(domain,dm);
716
717         if (name_use) *name_use = SidTypeUser;
718         return TRUE;
719 }
720
721 /******************************************************************************
722  * LookupAccountSidW [ADVAPI32.@]
723  *
724  * PARAMS
725  *   system      []
726  *   sid         []
727  *   account     []
728  *   accountSize []
729  *   domain      []
730  *   domainSize  []
731  *   name_use    []
732  */
733 BOOL WINAPI
734 LookupAccountSidW(
735         IN LPCWSTR system,
736         IN PSID sid,
737         OUT LPWSTR account,
738         IN OUT LPDWORD accountSize,
739         OUT LPWSTR domain,
740         IN OUT LPDWORD domainSize,
741         OUT PSID_NAME_USE name_use )
742 {
743     static const WCHAR ac[] = {'A','d','m','i','n','i','s','t','r','a','t','o','r',0};
744     static const WCHAR dm[] = {'D','O','M','A','I','N',0};
745         FIXME("(%s,sid=%p,%p,%p(%lu),%p,%p(%lu),%p): semi-stub\n",
746               debugstr_w(system),sid,
747               account,accountSize,accountSize?*accountSize:0,
748               domain,domainSize,domainSize?*domainSize:0,
749               name_use);
750
751         if (accountSize) *accountSize = strlenW(ac)+1;
752         if (account && (*accountSize > strlenW(ac)))
753             strcpyW(account, ac);
754
755         if (domainSize) *domainSize = strlenW(dm)+1;
756         if (domain && (*domainSize > strlenW(dm)))
757             strcpyW(domain,dm);
758
759         if (name_use) *name_use = SidTypeUser;
760         return TRUE;
761 }
762
763 /******************************************************************************
764  * SetFileSecurityA [ADVAPI32.@]
765  * Sets the security of a file or directory
766  */
767 BOOL WINAPI SetFileSecurityA( LPCSTR lpFileName,
768                                 SECURITY_INFORMATION RequestedInformation,
769                                 PSECURITY_DESCRIPTOR pSecurityDescriptor)
770 {
771   FIXME("(%s) : stub\n", debugstr_a(lpFileName));
772   return TRUE;
773 }
774
775 /******************************************************************************
776  * SetFileSecurityW [ADVAPI32.@]
777  * Sets the security of a file or directory
778  *
779  * PARAMS
780  *   lpFileName           []
781  *   RequestedInformation []
782  *   pSecurityDescriptor  []
783  */
784 BOOL WINAPI
785 SetFileSecurityW( LPCWSTR lpFileName,
786                     SECURITY_INFORMATION RequestedInformation,
787                     PSECURITY_DESCRIPTOR pSecurityDescriptor )
788 {
789   FIXME("(%s) : stub\n", debugstr_w(lpFileName) );
790   return TRUE;
791 }
792
793 /******************************************************************************
794  * QueryWindows31FilesMigration [ADVAPI32.@]
795  *
796  * PARAMS
797  *   x1 []
798  */
799 BOOL WINAPI
800 QueryWindows31FilesMigration( DWORD x1 )
801 {
802         FIXME("(%ld):stub\n",x1);
803         return TRUE;
804 }
805
806 /******************************************************************************
807  * SynchronizeWindows31FilesAndWindowsNTRegistry [ADVAPI32.@]
808  *
809  * PARAMS
810  *   x1 []
811  *   x2 []
812  *   x3 []
813  *   x4 []
814  */
815 BOOL WINAPI
816 SynchronizeWindows31FilesAndWindowsNTRegistry( DWORD x1, DWORD x2, DWORD x3,
817                                                DWORD x4 )
818 {
819         FIXME("(0x%08lx,0x%08lx,0x%08lx,0x%08lx):stub\n",x1,x2,x3,x4);
820         return TRUE;
821 }
822
823 /******************************************************************************
824  * LsaOpenPolicy [ADVAPI32.@]
825  *
826  * PARAMS
827  *   x1 []
828  *   x2 []
829  *   x3 []
830  *   x4 []
831  */
832 NTSTATUS WINAPI
833 LsaOpenPolicy(
834         IN PLSA_UNICODE_STRING SystemName,
835         IN PLSA_OBJECT_ATTRIBUTES ObjectAttributes,
836         IN ACCESS_MASK DesiredAccess,
837         IN OUT PLSA_HANDLE PolicyHandle)
838 {
839         FIXME("(%s,%p,0x%08lx,%p):stub\n",
840               SystemName?debugstr_w(SystemName->Buffer):"null",
841               ObjectAttributes, DesiredAccess, PolicyHandle);
842         ADVAPI_ForceLocalComputer(SystemName ? SystemName->Buffer : NULL,
843                                   STATUS_ACCESS_VIOLATION);
844         dumpLsaAttributes(ObjectAttributes);
845         if(PolicyHandle) *PolicyHandle = (LSA_HANDLE)0xcafe;
846         return STATUS_SUCCESS;
847 }
848
849 /******************************************************************************
850  * LsaQueryInformationPolicy [ADVAPI32.@]
851  */
852 NTSTATUS WINAPI
853 LsaQueryInformationPolicy(
854         IN LSA_HANDLE PolicyHandle,
855         IN POLICY_INFORMATION_CLASS InformationClass,
856         OUT PVOID *Buffer)
857 {
858         FIXME("(%p,0x%08x,%p):stub\n",
859               PolicyHandle, InformationClass, Buffer);
860
861         if(!Buffer) return FALSE;
862         switch (InformationClass)
863         {
864           case PolicyAuditEventsInformation: /* 2 */
865             {
866               PPOLICY_AUDIT_EVENTS_INFO p = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(POLICY_AUDIT_EVENTS_INFO));
867               p->AuditingMode = FALSE; /* no auditing */
868               *Buffer = p;
869             }
870             break;
871           case PolicyPrimaryDomainInformation: /* 3 */
872           case PolicyAccountDomainInformation: /* 5 */
873             {
874               struct di
875               { POLICY_PRIMARY_DOMAIN_INFO ppdi;
876                 SID sid;
877               };
878               SID_IDENTIFIER_AUTHORITY localSidAuthority = {SECURITY_NT_AUTHORITY};
879
880               struct di * xdi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(xdi));
881               RtlInitUnicodeString(&(xdi->ppdi.Name), HEAP_strdupAtoW(GetProcessHeap(),0,"DOMAIN"));
882               xdi->ppdi.Sid = &(xdi->sid);
883               xdi->sid.Revision = SID_REVISION;
884               xdi->sid.SubAuthorityCount = 1;
885               xdi->sid.IdentifierAuthority = localSidAuthority;
886               xdi->sid.SubAuthority[0] = SECURITY_LOCAL_SYSTEM_RID;
887               *Buffer = xdi;
888             }
889             break;
890           case  PolicyAuditLogInformation:
891           case  PolicyPdAccountInformation:
892           case  PolicyLsaServerRoleInformation:
893           case  PolicyReplicaSourceInformation:
894           case  PolicyDefaultQuotaInformation:
895           case  PolicyModificationInformation:
896           case  PolicyAuditFullSetInformation:
897           case  PolicyAuditFullQueryInformation:
898           case  PolicyDnsDomainInformation:
899             {
900               FIXME("category not implemented\n");
901               return FALSE;
902             }
903         }
904         return TRUE;
905 }
906
907 /******************************************************************************
908  * LsaLookupSids [ADVAPI32.@]
909  */
910 typedef struct
911 {
912         SID_NAME_USE Use;
913         LSA_UNICODE_STRING Name;
914         LONG DomainIndex;
915 } LSA_TRANSLATED_NAME, *PLSA_TRANSLATED_NAME;
916
917 typedef struct
918 {
919         LSA_UNICODE_STRING Name;
920         PSID Sid;
921 } LSA_TRUST_INFORMATION, *PLSA_TRUST_INFORMATION;
922
923 typedef struct
924 {
925         ULONG Entries;
926         PLSA_TRUST_INFORMATION Domains;
927 } LSA_REFERENCED_DOMAIN_LIST, *PLSA_REFERENCED_DOMAIN_LIST;
928
929 NTSTATUS WINAPI
930 LsaLookupSids(
931         IN LSA_HANDLE PolicyHandle,
932         IN ULONG Count,
933         IN PSID *Sids,
934         OUT PLSA_REFERENCED_DOMAIN_LIST *ReferencedDomains,
935         OUT PLSA_TRANSLATED_NAME *Names )
936 {
937         FIXME("%p %lu %p %p %p\n",
938           PolicyHandle, Count, Sids, ReferencedDomains, Names);
939         return FALSE;
940 }
941
942 /******************************************************************************
943  * LsaFreeMemory [ADVAPI32.@]
944  */
945 NTSTATUS WINAPI
946 LsaFreeMemory(IN PVOID Buffer)
947 {
948         TRACE("(%p)\n",Buffer);
949         return HeapFree(GetProcessHeap(), 0, Buffer);
950 }
951 /******************************************************************************
952  * LsaClose [ADVAPI32.@]
953  */
954 NTSTATUS WINAPI
955 LsaClose(IN LSA_HANDLE ObjectHandle)
956 {
957         FIXME("(%p):stub\n",ObjectHandle);
958         return 0xc0000000;
959 }
960
961 /******************************************************************************
962  * LsaNtStatusToWinError [ADVAPI32.@]
963  *
964  * PARAMS
965  *   Status [I]
966  */
967 ULONG WINAPI
968 LsaNtStatusToWinError(NTSTATUS Status)
969 {
970     return RtlNtStatusToDosError(Status);
971 }
972
973 /******************************************************************************
974  * NotifyBootConfigStatus [ADVAPI32.@]
975  *
976  * PARAMS
977  *   x1 []
978  */
979 BOOL WINAPI
980 NotifyBootConfigStatus( DWORD x1 )
981 {
982         FIXME("(0x%08lx):stub\n",x1);
983         return 1;
984 }
985
986 /******************************************************************************
987  * RevertToSelf [ADVAPI32.@]
988  *
989  * PARAMS
990  *   void []
991  */
992 BOOL WINAPI
993 RevertToSelf( void )
994 {
995         FIXME("(), stub\n");
996         return TRUE;
997 }
998
999 /******************************************************************************
1000  * ImpersonateSelf [ADVAPI32.@]
1001  */
1002 BOOL WINAPI
1003 ImpersonateSelf(SECURITY_IMPERSONATION_LEVEL ImpersonationLevel)
1004 {
1005         return RtlImpersonateSelf(ImpersonationLevel);
1006 }
1007
1008 /******************************************************************************
1009  * ImpersonateLoggedOnUser [ADVAPI32.@]
1010  */
1011 BOOL WINAPI ImpersonateLoggedOnUser(HANDLE hToken)
1012 {
1013     FIXME("(%p):stub returning FALSE\n", hToken);
1014     return FALSE;
1015 }
1016
1017 /******************************************************************************
1018  * AccessCheck [ADVAPI32.@]
1019  *
1020  * FIXME check cast LPBOOL to PBOOLEAN
1021  */
1022 BOOL WINAPI
1023 AccessCheck(
1024         PSECURITY_DESCRIPTOR SecurityDescriptor,
1025         HANDLE ClientToken,
1026         DWORD DesiredAccess,
1027         PGENERIC_MAPPING GenericMapping,
1028         PPRIVILEGE_SET PrivilegeSet,
1029         LPDWORD PrivilegeSetLength,
1030         LPDWORD GrantedAccess,
1031         LPBOOL AccessStatus)
1032 {
1033         CallWin32ToNt (NtAccessCheck(SecurityDescriptor, ClientToken, DesiredAccess,
1034           GenericMapping, PrivilegeSet, PrivilegeSetLength, GrantedAccess, (PBOOLEAN)AccessStatus));
1035 }
1036
1037 /*************************************************************************
1038  * SetKernelObjectSecurity [ADVAPI32.@]
1039  */
1040 BOOL WINAPI SetKernelObjectSecurity (
1041         IN HANDLE Handle,
1042         IN SECURITY_INFORMATION SecurityInformation,
1043         IN PSECURITY_DESCRIPTOR SecurityDescriptor )
1044 {
1045         CallWin32ToNt (NtSetSecurityObject (Handle, SecurityInformation, SecurityDescriptor));
1046 }
1047
1048 /******************************************************************************
1049  *  AddAccessAllowedAce [ADVAPI32.@]
1050  */
1051 BOOL WINAPI AddAccessAllowedAce(
1052         IN OUT PACL pAcl,
1053         IN DWORD dwAceRevision,
1054         IN DWORD AccessMask,
1055         IN PSID pSid)
1056 {
1057         return RtlAddAccessAllowedAce(pAcl, dwAceRevision, AccessMask, pSid);
1058 }
1059
1060 /******************************************************************************
1061  * LookupAccountNameA [ADVAPI32.@]
1062  */
1063 BOOL WINAPI
1064 LookupAccountNameA(
1065         IN LPCSTR system,
1066         IN LPCSTR account,
1067         OUT PSID sid,
1068         OUT LPDWORD cbSid,
1069         LPSTR ReferencedDomainName,
1070         IN OUT LPDWORD cbReferencedDomainName,
1071         OUT PSID_NAME_USE name_use )
1072 {
1073     FIXME("(%s,%s,%p,%p,%p,%p,%p), stub.\n",system,account,sid,cbSid,ReferencedDomainName,cbReferencedDomainName,name_use);
1074     return FALSE;
1075 }
1076
1077 /******************************************************************************
1078  * GetAce [ADVAPI32.@]
1079  */
1080 BOOL WINAPI GetAce(PACL pAcl,DWORD dwAceIndex,LPVOID *pAce )
1081 {
1082     CallWin32ToNt(RtlGetAce(pAcl, dwAceIndex, pAce));
1083 }
1084
1085 /******************************************************************************
1086  * PrivilegeCheck [ADVAPI32.@]
1087  */
1088 BOOL WINAPI PrivilegeCheck( HANDLE ClientToken, PPRIVILEGE_SET RequiredPrivileges, LPBOOL pfResult)
1089 {
1090         FIXME("stub %p %p %p\n", ClientToken, RequiredPrivileges, pfResult);
1091         if (pfResult)
1092                 *pfResult=TRUE;
1093         return TRUE;
1094 }