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