Attach to implicitly loaded builtin dlls on process startup.
[wine] / dlls / advapi32 / lsa.c
1 /*
2  * Implementation of the Local Security Authority API
3  *
4  * Copyright 1999 Juergen Schmied
5  * Copyright 2002 Andriy Palamarchuk
6  * Copyright 2004 Mike McCormack
7  * Copyright 2005 Hans Leidekker
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 #include <stdarg.h>
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winreg.h"
29 #include "winternl.h"
30 #include "ntstatus.h"
31 #include "ntsecapi.h"
32
33 #include "wine/debug.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(advapi);
36
37 #define ADVAPI_ForceLocalComputer(ServerName, FailureCode) \
38     if (!ADVAPI_IsLocalComputer(ServerName)) \
39 { \
40         FIXME("Action Implemented for local computer only. " \
41               "Requested for server %s\n", debugstr_w(ServerName)); \
42         return FailureCode; \
43 }
44
45 static void dumpLsaAttributes(PLSA_OBJECT_ATTRIBUTES oa)
46 {
47     if (oa)
48     {
49         TRACE("\n\tlength=%lu, rootdir=%p, objectname=%s\n\tattr=0x%08lx, sid=%p qos=%p\n",
50               oa->Length, oa->RootDirectory,
51               oa->ObjectName?debugstr_w(oa->ObjectName->Buffer):"null",
52               oa->Attributes, oa->SecurityDescriptor, oa->SecurityQualityOfService);
53     }
54 }
55
56 /************************************************************
57  * ADVAPI_IsLocalComputer
58  *
59  * Checks whether the server name indicates local machine.
60  */
61 static BOOL ADVAPI_IsLocalComputer(LPCWSTR ServerName)
62 {
63     DWORD dwSize = MAX_COMPUTERNAME_LENGTH + 1;
64     BOOL Result;
65     LPWSTR buf;
66
67     if (!ServerName || !ServerName[0])
68         return TRUE;
69
70     buf = HeapAlloc(GetProcessHeap(), 0, dwSize * sizeof(WCHAR));
71     Result = GetComputerNameW(buf,  &dwSize);
72     if (Result && (ServerName[0] == '\\') && (ServerName[1] == '\\'))
73         ServerName += 2;
74     Result = Result && !lstrcmpW(ServerName, buf);
75     HeapFree(GetProcessHeap(), 0, buf);
76
77     return Result;
78 }
79
80 /******************************************************************************
81  * LsaClose [ADVAPI32.@]
82  *
83  * Closes a handle to a Policy or TrustedDomain.
84  *
85  * PARAMS
86  *  ObjectHandle [I] Handle to a Policy or TrustedDomain.
87  *
88  * RETURNS
89  *  Success: STATUS_SUCCESS.
90  *  Failure: NTSTATUS code.
91  */
92 NTSTATUS WINAPI LsaClose(IN LSA_HANDLE ObjectHandle)
93 {
94     FIXME("(%p) stub\n", ObjectHandle);
95     return 0xc0000000;
96 }
97
98 /******************************************************************************
99  * LsaEnumerateTrustedDomains [ADVAPI32.@]
100  *
101  * Returns the names and SIDs of trusted domains.
102  *
103  * PARAMS
104  *  PolicyHandle          [I] Handle to a Policy object.
105  *  EnumerationContext    [I] Pointer to an enumeration handle.
106  *  Buffer                [O] Contains the names and SIDs of trusted domains.
107  *  PreferredMaximumLength[I] Preferred maximum size in bytes of Buffer.
108  *  CountReturned         [O] Number of elements in Buffer.
109  *
110  * RETURNS
111  *  Success: STATUS_SUCCESS,
112  *           STATUS_MORE_ENTRIES,
113  *           STATUS_NO_MORE_ENTRIES
114  *  Failure: NTSTATUS code.
115  *
116  * NOTES
117  *  LsaEnumerateTrustedDomains can be called multiple times to enumerate
118  *  all trusted domains.
119  */
120 NTSTATUS WINAPI LsaEnumerateTrustedDomains(
121     IN LSA_HANDLE PolicyHandle,
122     IN PLSA_ENUMERATION_HANDLE EnumerationContext,
123     OUT PVOID* Buffer,
124     IN ULONG PreferredMaximumLength,
125     OUT PULONG CountReturned)
126 {
127     FIXME("(%p,%p,%p,0x%08lx,%p) stub\n", PolicyHandle, EnumerationContext,
128           Buffer, PreferredMaximumLength, CountReturned);
129
130     if (CountReturned) *CountReturned = 0;
131     return STATUS_SUCCESS;
132 }
133
134 /******************************************************************************
135  * LsaFreeMemory [ADVAPI32.@]
136  *
137  * Frees memory allocated by a LSA function.
138  *
139  * PARAMS
140  *  Buffer [I] Memory buffer to free.
141  *
142  * RETURNS
143  *  Success: STATUS_SUCCESS.
144  *  Failure: NTSTATUS code.
145  */
146 NTSTATUS WINAPI LsaFreeMemory(IN PVOID Buffer)
147 {
148     TRACE("(%p)\n", Buffer);
149     return HeapFree(GetProcessHeap(), 0, Buffer);
150 }
151
152 /******************************************************************************
153  * LsaLookupNames [ADVAPI32.@]
154  *
155  * Returns the SIDs of an array of user, group, or local group names.
156  *
157  * PARAMS
158  *  PolicyHandle      [I] Handle to a Policy object.
159  *  Count             [I] Number of names in Names.
160  *  Names             [I] Array of names to lookup.
161  *  ReferencedDomains [O] Array of domains where the names were found.
162  *  Sids              [O] Array of SIDs corresponding to Names.
163  *
164  * RETURNS
165  *  Success: STATUS_SUCCESS,
166  *           STATUS_SOME_NOT_MAPPED
167  *  Failure: STATUS_NONE_MAPPED or NTSTATUS code.
168  */
169 NTSTATUS WINAPI LsaLookupNames(
170     IN LSA_HANDLE PolicyHandle,
171     IN ULONG Count,
172     IN PLSA_UNICODE_STRING Names,
173     OUT PLSA_REFERENCED_DOMAIN_LIST* ReferencedDomains,
174     OUT PLSA_TRANSLATED_SID* Sids)
175 {
176     FIXME("(%p,0x%08lx,%p,%p,%p) stub\n", PolicyHandle, Count, Names,
177           ReferencedDomains, Sids);
178
179     return STATUS_NONE_MAPPED;
180 }
181
182 /******************************************************************************
183  * LsaLookupSids [ADVAPI32.@]
184  *
185  * Looks up the names that correspond to an array of SIDs.
186  *
187  * PARAMS
188  *  PolicyHandle      [I] Handle to a Policy object.
189  *  Count             [I] Number of SIDs in the Sids array.
190  *  Sids              [I] Array of SIDs to lookup.
191  *  ReferencedDomains [O] Array of domains where the sids were found.
192  *  Names             [O] Array of names corresponding to Sids.
193  *
194  * RETURNS
195  *  Success: STATUS_SUCCESS,
196  *           STATUS_SOME_NOT_MAPPED
197  *  Failure: STATUS_NONE_MAPPED or NTSTATUS code.
198  */
199 NTSTATUS WINAPI LsaLookupSids(
200     IN LSA_HANDLE PolicyHandle,
201     IN ULONG Count,
202     IN PSID *Sids,
203     OUT PLSA_REFERENCED_DOMAIN_LIST *ReferencedDomains,
204     OUT PLSA_TRANSLATED_NAME *Names )
205 {
206     FIXME("(%p,%lu,%p,%p,%p) stub\n", PolicyHandle, Count, Sids,
207           ReferencedDomains, Names);
208
209     return FALSE;
210 }
211
212 /******************************************************************************
213  * LsaNtStatusToWinError [ADVAPI32.@]
214  *
215  * Converts an LSA NTSTATUS code to a Windows error code.
216  *
217  * PARAMS
218  *  Status [I] NTSTATUS code.
219  *
220  * RETURNS
221  *  Success: Corresponding Windows error code.
222  *  Failure: ERROR_MR_MID_NOT_FOUND.
223  */
224 ULONG WINAPI LsaNtStatusToWinError(NTSTATUS Status)
225 {
226     return RtlNtStatusToDosError(Status);
227 }
228
229 /******************************************************************************
230  * LsaOpenPolicy [ADVAPI32.@]
231  *
232  * Opens a handle to the Policy object on a local or remote system.
233  *
234  * PARAMS
235  *  SystemName       [I] Name of the target system.
236  *  ObjectAttributes [I] Connection attributes.
237  *  DesiredAccess    [I] Requested access rights.
238  *  PolicyHandle     [I/O] Handle to the Policy object.
239  *
240  * RETURNS
241  *  Success: STATUS_SUCCESS.
242  *  Failure: NTSTATUS code.
243  *
244  * NOTES
245  *  Set SystemName to NULL to open the local Policy object.
246  */
247 NTSTATUS WINAPI LsaOpenPolicy(
248     IN PLSA_UNICODE_STRING SystemName,
249     IN PLSA_OBJECT_ATTRIBUTES ObjectAttributes,
250     IN ACCESS_MASK DesiredAccess,
251     IN OUT PLSA_HANDLE PolicyHandle)
252 {
253     FIXME("(%s,%p,0x%08lx,%p) stub\n",
254           SystemName?debugstr_w(SystemName->Buffer):"(null)",
255           ObjectAttributes, DesiredAccess, PolicyHandle);
256
257     ADVAPI_ForceLocalComputer(SystemName ? SystemName->Buffer : NULL,
258                               STATUS_ACCESS_VIOLATION);
259     dumpLsaAttributes(ObjectAttributes);
260
261     if(PolicyHandle) *PolicyHandle = (LSA_HANDLE)0xcafe;
262     return STATUS_SUCCESS;
263 }
264
265 /******************************************************************************
266  * LsaQueryInformationPolicy [ADVAPI32.@]
267  *
268  * Returns information about a Policy object.
269  *
270  * PARAMS
271  *  PolicyHandle     [I] Handle to a Policy object.
272  *  InformationClass [I] Type of information to retrieve.
273  *  Buffer           [O] Pointer to the requested information.
274  *
275  * RETURNS
276  *  Success: STATUS_SUCCESS.
277  *  Failure: NTSTATUS code.
278  */
279 NTSTATUS WINAPI LsaQueryInformationPolicy(
280     IN LSA_HANDLE PolicyHandle,
281     IN POLICY_INFORMATION_CLASS InformationClass,
282     OUT PVOID *Buffer)
283 {
284     FIXME("(%p,0x%08x,%p) stub\n", PolicyHandle, InformationClass, Buffer);
285
286     if(!Buffer) return FALSE;
287     switch (InformationClass)
288     {
289         case PolicyAuditEventsInformation: /* 2 */
290         {
291             PPOLICY_AUDIT_EVENTS_INFO p = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
292                                                     sizeof(POLICY_AUDIT_EVENTS_INFO));
293             p->AuditingMode = FALSE; /* no auditing */
294             *Buffer = p;
295         }
296         break;
297         case PolicyPrimaryDomainInformation: /* 3 */
298         case PolicyAccountDomainInformation: /* 5 */
299         {
300             struct di
301             {
302                 POLICY_PRIMARY_DOMAIN_INFO ppdi;
303                 SID sid;
304             };
305
306             SID_IDENTIFIER_AUTHORITY localSidAuthority = {SECURITY_NT_AUTHORITY};
307
308             struct di * xdi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(xdi));
309             HKEY key;
310             BOOL useDefault = TRUE;
311             LONG ret;
312
313             if ((ret = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
314                  "System\\CurrentControlSet\\Services\\VxD\\VNETSUP", 0,
315                  KEY_READ, &key)) == ERROR_SUCCESS)
316             {
317                 DWORD size = 0;
318                 static const WCHAR wg[] = { 'W','o','r','k','g','r','o','u','p',0 };
319
320                 ret = RegQueryValueExW(key, wg, NULL, NULL, NULL, &size);
321                 if (ret == ERROR_MORE_DATA || ret == ERROR_SUCCESS)
322                 {
323                     xdi->ppdi.Name.Buffer = HeapAlloc(GetProcessHeap(),
324                                                       HEAP_ZERO_MEMORY, size);
325
326                     if ((ret = RegQueryValueExW(key, wg, NULL, NULL,
327                          (LPBYTE)xdi->ppdi.Name.Buffer, &size)) == ERROR_SUCCESS)
328                     {
329                         xdi->ppdi.Name.Length = (USHORT)size;
330                         useDefault = FALSE;
331                     }
332                     else
333                     {
334                         HeapFree(GetProcessHeap(), 0, xdi->ppdi.Name.Buffer);
335                         xdi->ppdi.Name.Buffer = NULL;
336                     }
337                 }
338                 RegCloseKey(key);
339             }
340             if (useDefault)
341                 RtlCreateUnicodeStringFromAsciiz(&(xdi->ppdi.Name), "DOMAIN");
342
343             TRACE("setting domain to %s\n", debugstr_w(xdi->ppdi.Name.Buffer));
344
345             xdi->ppdi.Sid = &(xdi->sid);
346             xdi->sid.Revision = SID_REVISION;
347             xdi->sid.SubAuthorityCount = 1;
348             xdi->sid.IdentifierAuthority = localSidAuthority;
349             xdi->sid.SubAuthority[0] = SECURITY_LOCAL_SYSTEM_RID;
350             *Buffer = xdi;
351         }
352         break;
353         case  PolicyAuditLogInformation:
354         case  PolicyPdAccountInformation:
355         case  PolicyLsaServerRoleInformation:
356         case  PolicyReplicaSourceInformation:
357         case  PolicyDefaultQuotaInformation:
358         case  PolicyModificationInformation:
359         case  PolicyAuditFullSetInformation:
360         case  PolicyAuditFullQueryInformation:
361         case  PolicyDnsDomainInformation:
362         {
363             FIXME("category not implemented\n");
364             return FALSE;
365         }
366     }
367     return TRUE;
368 }
369
370 /******************************************************************************
371  * LsaRetrievePrivateData [ADVAPI32.@]
372  *
373  * Retrieves data stored by LsaStorePrivateData.
374  *
375  * PARAMS
376  *  PolicyHandle [I] Handle to a Policy object.
377  *  KeyName      [I] Name of the key where the data is stored.
378  *  PrivateData  [O] Pointer to the private data.
379  *
380  * RETURNS
381  *  Success: STATUS_SUCCESS.
382  *  Failure: STATUS_OBJECT_NAME_NOT_FOUND or NTSTATUS code.
383  */
384 NTSTATUS WINAPI LsaRetrievePrivateData(
385     IN LSA_HANDLE PolicyHandle,
386     IN PLSA_UNICODE_STRING KeyName,
387     OUT PLSA_UNICODE_STRING* PrivateData)
388 {
389     FIXME("(%p,%p,%p) stub\n", PolicyHandle, KeyName, PrivateData);
390     return STATUS_OBJECT_NAME_NOT_FOUND;
391 }
392
393 /******************************************************************************
394  * LsaSetInformationPolicy [ADVAPI32.@]
395  *
396  * Modifies information in a Policy object.
397  *
398  * PARAMS
399  *  PolicyHandle     [I] Handle to a Policy object.
400  *  InformationClass [I] Type of information to set.
401  *  Buffer           [I] Pointer to the information to set.
402  *
403  * RETURNS
404  *  Success: STATUS_SUCCESS.
405  *  Failure: NTSTATUS code.
406  */
407 NTSTATUS WINAPI LsaSetInformationPolicy(
408     IN LSA_HANDLE PolicyHandle,
409     IN POLICY_INFORMATION_CLASS InformationClass,
410     IN PVOID Buffer)
411 {
412     FIXME("(%p,0x%08x,%p) stub\n", PolicyHandle, InformationClass, Buffer);
413
414     return STATUS_UNSUCCESSFUL;
415 }
416
417 /******************************************************************************
418  * LsaStorePrivateData [ADVAPI32.@]
419  *
420  * Stores or deletes a Policy object's data under the specified reg key.
421  *
422  * PARAMS
423  *  PolicyHandle [I] Handle to a Policy object.
424  *  KeyName      [I] Name of the key where the data will be stored.
425  *  PrivateData  [O] Pointer to the private data.
426  *
427  * RETURNS
428  *  Success: STATUS_SUCCESS.
429  *  Failure: STATUS_OBJECT_NAME_NOT_FOUND or NTSTATUS code.
430  */
431 NTSTATUS WINAPI LsaStorePrivateData(
432     IN LSA_HANDLE PolicyHandle,
433     IN PLSA_UNICODE_STRING KeyName,
434     IN PLSA_UNICODE_STRING PrivateData)
435 {
436     FIXME("(%p,%p,%p) stub\n", PolicyHandle, KeyName, PrivateData);
437     return STATUS_OBJECT_NAME_NOT_FOUND;
438 }