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