Added a first-cut version of MapVirtualKeyExW() that has the same
[wine] / dlls / ntdll / om.c
1 /*
2  *      Object management functions
3  */
4
5 #include <stdlib.h>
6 #include <string.h>
7 #include "debugtools.h"
8
9 #include "ntddk.h"
10 #include "ntdll_misc.h"
11 #include "server.h"
12
13 DEFAULT_DEBUG_CHANNEL(ntdll);
14
15 /* move to somewhere */
16 typedef void * POBJDIR_INFORMATION;
17
18 /*
19  *      Generic object functions
20  */
21  
22 /******************************************************************************
23  * NtQueryObject [NTDLL.161]
24  */
25 NTSTATUS WINAPI NtQueryObject(
26         IN HANDLE ObjectHandle,
27         IN OBJECT_INFORMATION_CLASS ObjectInformationClass,
28         OUT PVOID ObjectInformation,
29         IN ULONG Length,
30         OUT PULONG ResultLength)
31 {
32         FIXME("(0x%08x,0x%08x,%p,0x%08lx,%p): stub\n",
33         ObjectHandle, ObjectInformationClass, ObjectInformation, Length, ResultLength);
34         return 0;
35 }
36
37 /******************************************************************************
38  *  NtQuerySecurityObject       [NTDLL] 
39  *
40  * analogue to GetKernelObjectSecurity
41  *
42  * NOTES
43  *  only the lowest 4 bit of SecurityObjectInformationClass are used
44  *  0x7-0xf returns STATUS_ACCESS_DENIED (even running with system priviledges) 
45  *
46  * FIXME: we are constructing a fake sid 
47  *  (Administrators:Full, System:Full, Everyone:Read)
48  */
49 NTSTATUS WINAPI 
50 NtQuerySecurityObject(
51         IN HANDLE Object,
52         IN SECURITY_INFORMATION RequestedInformation,
53         OUT PSECURITY_DESCRIPTOR pSecurityDesriptor,
54         IN ULONG Length,
55         OUT PULONG ResultLength)
56 {
57         static SID_IDENTIFIER_AUTHORITY localSidAuthority = {SECURITY_NT_AUTHORITY};
58         static SID_IDENTIFIER_AUTHORITY worldSidAuthority = {SECURITY_WORLD_SID_AUTHORITY};
59         BYTE Buffer[256];
60         PISECURITY_DESCRIPTOR_RELATIVE psd = (PISECURITY_DESCRIPTOR_RELATIVE)Buffer;
61         UINT BufferIndex = sizeof(SECURITY_DESCRIPTOR_RELATIVE);
62         
63         FIXME("(0x%08x,0x%08lx,%p,0x%08lx,%p) stub!\n",
64         Object, RequestedInformation, pSecurityDesriptor, Length, ResultLength);
65
66         RequestedInformation &= 0x0000000f;
67
68         if (RequestedInformation & SACL_SECURITY_INFORMATION) return STATUS_ACCESS_DENIED;
69
70         ZeroMemory(Buffer, 256);
71         RtlCreateSecurityDescriptor((PSECURITY_DESCRIPTOR)psd, SECURITY_DESCRIPTOR_REVISION);
72         psd->Control = SE_SELF_RELATIVE | 
73           ((RequestedInformation & DACL_SECURITY_INFORMATION) ? SE_DACL_PRESENT:0);
74
75         /* owner: administrator S-1-5-20-220*/
76         if (OWNER_SECURITY_INFORMATION & RequestedInformation)
77         {
78           PSID psid = (PSID)&(Buffer[BufferIndex]);
79
80           psd->Owner = BufferIndex;
81           BufferIndex += RtlLengthRequiredSid(2);
82
83           psid->Revision = SID_REVISION;
84           psid->SubAuthorityCount = 2;
85           psid->IdentifierAuthority = localSidAuthority;
86           psid->SubAuthority[0] = SECURITY_BUILTIN_DOMAIN_RID;
87           psid->SubAuthority[1] = DOMAIN_ALIAS_RID_ADMINS;
88         }
89         
90         /* group: built in domain S-1-5-12 */
91         if (GROUP_SECURITY_INFORMATION & RequestedInformation)
92         {
93           PSID psid = (PSID) &(Buffer[BufferIndex]);
94
95           psd->Group = BufferIndex;
96           BufferIndex += RtlLengthRequiredSid(1);
97
98           psid->Revision = SID_REVISION;
99           psid->SubAuthorityCount = 1;
100           psid->IdentifierAuthority = localSidAuthority;
101           psid->SubAuthority[0] = SECURITY_LOCAL_SYSTEM_RID;
102         }
103
104         /* discretionary ACL */
105         if (DACL_SECURITY_INFORMATION & RequestedInformation)
106         {
107           /* acl header */
108           PACL pacl = (PACL)&(Buffer[BufferIndex]);
109           PACCESS_ALLOWED_ACE pace;
110           PSID psid;
111                           
112           psd->Dacl = BufferIndex;
113
114           pacl->AclRevision = MIN_ACL_REVISION;
115           pacl->AceCount = 3;
116           pacl->AclSize = BufferIndex; /* storing the start index temporary */
117
118           BufferIndex += sizeof(ACL);
119           
120           /* ACE System - full access */
121           pace = (PACCESS_ALLOWED_ACE)&(Buffer[BufferIndex]);
122           BufferIndex += sizeof(ACCESS_ALLOWED_ACE)-sizeof(DWORD);
123
124           pace->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
125           pace->Header.AceFlags = CONTAINER_INHERIT_ACE;
126           pace->Header.AceSize = sizeof(ACCESS_ALLOWED_ACE)-sizeof(DWORD) + RtlLengthRequiredSid(1);
127           pace->Mask = DELETE | READ_CONTROL | WRITE_DAC | WRITE_OWNER  | 0x3f;
128           pace->SidStart = BufferIndex;
129
130           /* SID S-1-5-12 (System) */
131           psid = (PSID)&(Buffer[BufferIndex]);
132
133           BufferIndex += RtlLengthRequiredSid(1);
134
135           psid->Revision = SID_REVISION;
136           psid->SubAuthorityCount = 1;
137           psid->IdentifierAuthority = localSidAuthority;
138           psid->SubAuthority[0] = SECURITY_LOCAL_SYSTEM_RID;
139           
140           /* ACE Administrators - full access*/
141           pace = (PACCESS_ALLOWED_ACE) &(Buffer[BufferIndex]);
142           BufferIndex += sizeof(ACCESS_ALLOWED_ACE)-sizeof(DWORD);
143
144           pace->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
145           pace->Header.AceFlags = CONTAINER_INHERIT_ACE;
146           pace->Header.AceSize = sizeof(ACCESS_ALLOWED_ACE)-sizeof(DWORD) + RtlLengthRequiredSid(2);
147           pace->Mask = DELETE | READ_CONTROL | WRITE_DAC | WRITE_OWNER  | 0x3f;
148           pace->SidStart = BufferIndex;
149
150           /* S-1-5-12 (Administrators) */
151           psid = (PSID)&(Buffer[BufferIndex]);
152
153           BufferIndex += RtlLengthRequiredSid(2);
154
155           psid->Revision = SID_REVISION;
156           psid->SubAuthorityCount = 2;
157           psid->IdentifierAuthority = localSidAuthority;
158           psid->SubAuthority[0] = SECURITY_BUILTIN_DOMAIN_RID;
159           psid->SubAuthority[1] = DOMAIN_ALIAS_RID_ADMINS;
160          
161           /* ACE Everyone - read access */
162           pace = (PACCESS_ALLOWED_ACE)&(Buffer[BufferIndex]);
163           BufferIndex += sizeof(ACCESS_ALLOWED_ACE)-sizeof(DWORD);
164
165           pace->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
166           pace->Header.AceFlags = CONTAINER_INHERIT_ACE;
167           pace->Header.AceSize = sizeof(ACCESS_ALLOWED_ACE)-sizeof(DWORD) + RtlLengthRequiredSid(1);
168           pace->Mask = READ_CONTROL| 0x19;
169           pace->SidStart = BufferIndex;
170
171           /* SID S-1-1-0 (Everyone) */
172           psid = (PSID)&(Buffer[BufferIndex]);
173
174           BufferIndex += RtlLengthRequiredSid(1);
175
176           psid->Revision = SID_REVISION;
177           psid->SubAuthorityCount = 1;
178           psid->IdentifierAuthority = worldSidAuthority;
179           psid->SubAuthority[0] = 0;
180
181           /* calculate used bytes */
182           pacl->AclSize = BufferIndex - pacl->AclSize;
183         }
184         *ResultLength = BufferIndex;
185         TRACE("len=%lu\n", *ResultLength);
186         if (Length < *ResultLength) return STATUS_BUFFER_TOO_SMALL;
187         memcpy(pSecurityDesriptor, Buffer, *ResultLength);
188
189         return STATUS_SUCCESS;
190 }
191 /******************************************************************************
192  *  NtDuplicateObject           [NTDLL] 
193  */
194 NTSTATUS WINAPI NtDuplicateObject(
195         IN HANDLE SourceProcessHandle,
196         IN PHANDLE SourceHandle,
197         IN HANDLE TargetProcessHandle,
198         OUT PHANDLE TargetHandle,
199         IN ACCESS_MASK DesiredAccess,
200         IN BOOLEAN InheritHandle,
201         ULONG Options)
202 {
203         FIXME("(0x%08x,%p,0x%08x,%p,0x%08lx,0x%08x,0x%08lx) stub!\n",
204         SourceProcessHandle,SourceHandle,TargetProcessHandle,TargetHandle,
205         DesiredAccess,InheritHandle,Options);
206         *TargetHandle = 0;
207         return 0;
208 }
209
210 /**************************************************************************
211  *                 NtClose                              [NTDLL.65]
212  * FUNCTION: Closes a handle reference to an object
213  * ARGUMENTS:
214  *      Handle  handle to close
215  */
216 NTSTATUS WINAPI NtClose( HANDLE Handle )
217 {
218     NTSTATUS ret;
219     SERVER_START_REQ
220     {
221         struct close_handle_request *req = server_alloc_req( sizeof(*req), 0 );
222         req->handle = Handle;
223         ret = server_call_noerr( REQ_CLOSE_HANDLE );
224     }
225     SERVER_END_REQ;
226     return ret;
227 }
228
229 /******************************************************************************
230  *  NtWaitForSingleObject               [NTDLL] 
231  */
232 NTSTATUS WINAPI NtWaitForSingleObject(
233         IN PHANDLE Object,
234         IN BOOLEAN Alertable,
235         IN PLARGE_INTEGER Time)
236 {
237         FIXME("(%p,0x%08x,%p),stub!\n",Object,Alertable,Time);
238         return 0;
239 }
240
241 /*
242  *      Directory functions
243  */
244
245 /**************************************************************************
246  * NtOpenDirectoryObject [NTDLL.124]
247  * FUNCTION: Opens a namespace directory object
248  * ARGUMENTS:
249  *  DirectoryHandle     Variable which receives the directory handle
250  *  DesiredAccess       Desired access to the directory
251  *  ObjectAttributes    Structure describing the directory
252  * RETURNS: Status
253  */
254 NTSTATUS WINAPI NtOpenDirectoryObject(
255         PHANDLE DirectoryHandle,
256         ACCESS_MASK DesiredAccess,
257         POBJECT_ATTRIBUTES ObjectAttributes)
258 {
259         FIXME("(%p,0x%08lx,%p): stub\n", 
260         DirectoryHandle, DesiredAccess, ObjectAttributes);
261         dump_ObjectAttributes(ObjectAttributes);
262         return 0;
263 }
264
265 /******************************************************************************
266  *  NtCreateDirectoryObject     [NTDLL] 
267  */
268 NTSTATUS WINAPI NtCreateDirectoryObject(
269         PHANDLE DirectoryHandle,
270         ACCESS_MASK DesiredAccess,
271         POBJECT_ATTRIBUTES ObjectAttributes) 
272 {
273         FIXME("(%p,0x%08lx,%p),stub!\n",
274         DirectoryHandle,DesiredAccess,ObjectAttributes);
275         dump_ObjectAttributes(ObjectAttributes);
276         return 0;
277 }
278
279 /******************************************************************************
280  * NtQueryDirectoryObject [NTDLL.149] 
281  * FUNCTION: Reads information from a namespace directory
282  * ARGUMENTS:
283  *  DirObjInformation   Buffer to hold the data read
284  *  BufferLength        Size of the buffer in bytes
285  *  GetNextIndex        If TRUE then set ObjectIndex to the index of the next object
286  *                      If FALSE then set ObjectIndex to the number of objects in the directory
287  *  IgnoreInputIndex    If TRUE start reading at index 0
288  *                      If FALSE start reading at the index specified by object index
289  *  ObjectIndex         Zero based index into the directory, interpretation depends on IgnoreInputIndex and GetNextIndex
290  *  DataWritten         Caller supplied storage for the number of bytes written (or NULL)
291  */
292 NTSTATUS WINAPI NtQueryDirectoryObject(
293         IN HANDLE DirObjHandle,
294         OUT POBJDIR_INFORMATION DirObjInformation,
295         IN ULONG BufferLength,
296         IN BOOLEAN GetNextIndex,
297         IN BOOLEAN IgnoreInputIndex,
298         IN OUT PULONG ObjectIndex,
299         OUT PULONG DataWritten OPTIONAL)
300 {
301         FIXME("(0x%08x,%p,0x%08lx,0x%08x,0x%08x,%p,%p) stub\n",
302                 DirObjHandle, DirObjInformation, BufferLength, GetNextIndex,
303                 IgnoreInputIndex, ObjectIndex, DataWritten);
304     return 0xc0000000; /* We don't have any. Whatever. (Yet.) */
305 }
306
307 /*
308  *      Link objects
309  */
310  
311 /******************************************************************************
312  *  NtOpenSymbolicLinkObject    [NTDLL] 
313  */
314 NTSTATUS WINAPI NtOpenSymbolicLinkObject(
315         OUT PHANDLE LinkHandle,
316         IN ACCESS_MASK DesiredAccess,
317         IN POBJECT_ATTRIBUTES ObjectAttributes)
318 {
319         FIXME("(%p,0x%08lx,%p) stub\n",
320         LinkHandle, DesiredAccess, ObjectAttributes);
321         dump_ObjectAttributes(ObjectAttributes);
322         return 0;
323 }
324
325 /******************************************************************************
326  *  NtCreateSymbolicLinkObject  [NTDLL] 
327  */
328 NTSTATUS WINAPI NtCreateSymbolicLinkObject(
329         OUT PHANDLE SymbolicLinkHandle,
330         IN ACCESS_MASK DesiredAccess,
331         IN POBJECT_ATTRIBUTES ObjectAttributes,
332         IN PUNICODE_STRING Name)
333 {
334         FIXME("(%p,0x%08lx,%p, %p) stub\n",
335         SymbolicLinkHandle, DesiredAccess, ObjectAttributes, debugstr_us(Name));
336         dump_ObjectAttributes(ObjectAttributes);
337         return 0;
338 }
339
340 /******************************************************************************
341  *  NtQuerySymbolicLinkObject   [NTDLL] 
342  */
343 NTSTATUS WINAPI NtQuerySymbolicLinkObject(
344         IN HANDLE LinkHandle,
345         IN OUT PUNICODE_STRING LinkTarget,
346         OUT PULONG ReturnedLength OPTIONAL)
347 {
348         FIXME("(0x%08x,%p,%p) stub\n",
349         LinkHandle, debugstr_us(LinkTarget), ReturnedLength);
350
351         return 0;
352 }
353
354 /******************************************************************************
355  *  NtAllocateUuids   [NTDLL]
356  *
357  * I have seen lpdwCount pointing to a pointer once...
358  */
359 NTSTATUS WINAPI NtAllocateUuids(LPDWORD lpdwCount, LPDWORD *p2, LPDWORD *p3)
360 {
361         FIXME("(%p[%ld],%p,%p), stub.\n", lpdwCount,
362                                          lpdwCount ? *lpdwCount : 0,
363                                          p2, p3);
364         return 0;
365 }