2 * Object management functions
4 * Copyright 1999, 2000 Juergen Schmied
5 * Copyright 2005 Vitaliy Margolen
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
35 #define WIN32_NO_STATUS
36 #include "wine/debug.h"
39 #include "ntdll_misc.h"
40 #include "wine/server.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(ntdll
);
46 * Generic object functions
49 /******************************************************************************
50 * NtQueryObject [NTDLL.@]
51 * ZwQueryObject [NTDLL.@]
53 NTSTATUS WINAPI
NtQueryObject(IN HANDLE handle
,
54 IN OBJECT_INFORMATION_CLASS info_class
,
55 OUT PVOID ptr
, IN ULONG len
, OUT PULONG used_len
)
59 TRACE("(%p,0x%08x,%p,0x%08x,%p): stub\n",
60 handle
, info_class
, ptr
, len
, used_len
);
62 if (used_len
) *used_len
= 0;
66 case ObjectBasicInformation
:
68 POBJECT_BASIC_INFORMATION p
= (POBJECT_BASIC_INFORMATION
)ptr
;
70 if (len
< sizeof(*p
)) return STATUS_INVALID_BUFFER_SIZE
;
72 SERVER_START_REQ( get_object_info
)
75 status
= wine_server_call( req
);
76 if (status
== STATUS_SUCCESS
)
78 memset( p
, 0, sizeof(*p
) );
79 p
->GrantedAccess
= reply
->access
;
80 p
->PointerCount
= reply
->ref_count
;
81 p
->HandleCount
= 1; /* at least one */
82 if (used_len
) *used_len
= sizeof(*p
);
88 case ObjectDataInformation
:
90 OBJECT_DATA_INFORMATION
* p
= (OBJECT_DATA_INFORMATION
*)ptr
;
92 if (len
< sizeof(*p
)) return STATUS_INVALID_BUFFER_SIZE
;
94 SERVER_START_REQ( set_handle_info
)
99 status
= wine_server_call( req
);
100 if (status
== STATUS_SUCCESS
)
102 p
->InheritHandle
= (reply
->old_flags
& HANDLE_FLAG_INHERIT
) ? TRUE
: FALSE
;
103 p
->ProtectFromClose
= (reply
->old_flags
& HANDLE_FLAG_PROTECT_FROM_CLOSE
) ? TRUE
: FALSE
;
104 if (used_len
) *used_len
= sizeof(*p
);
111 FIXME("Unsupported information class %u\n", info_class
);
112 status
= STATUS_NOT_IMPLEMENTED
;
118 /******************************************************************
119 * NtSetInformationObject [NTDLL.@]
120 * ZwSetInformationObject [NTDLL.@]
123 NTSTATUS WINAPI
NtSetInformationObject(IN HANDLE handle
,
124 IN OBJECT_INFORMATION_CLASS info_class
,
125 IN PVOID ptr
, IN ULONG len
)
129 TRACE("(%p,0x%08x,%p,0x%08x): stub\n",
130 handle
, info_class
, ptr
, len
);
134 case ObjectDataInformation
:
136 OBJECT_DATA_INFORMATION
* p
= (OBJECT_DATA_INFORMATION
*)ptr
;
138 if (len
< sizeof(*p
)) return STATUS_INVALID_BUFFER_SIZE
;
140 SERVER_START_REQ( set_handle_info
)
142 req
->handle
= handle
;
144 req
->mask
= HANDLE_FLAG_INHERIT
| HANDLE_FLAG_PROTECT_FROM_CLOSE
;
145 if (p
->InheritHandle
) req
->flags
|= HANDLE_FLAG_INHERIT
;
146 if (p
->ProtectFromClose
) req
->flags
|= HANDLE_FLAG_PROTECT_FROM_CLOSE
;
147 status
= wine_server_call( req
);
153 FIXME("Unsupported information class %u\n", info_class
);
154 status
= STATUS_NOT_IMPLEMENTED
;
160 /******************************************************************************
161 * NtQuerySecurityObject [NTDLL.@]
163 * An ntdll analogue to GetKernelObjectSecurity().
166 * only the lowest 4 bit of SecurityObjectInformationClass are used
167 * 0x7-0xf returns STATUS_ACCESS_DENIED (even running with system privileges)
170 * We are constructing a fake sid (Administrators:Full, System:Full, Everyone:Read)
173 NtQuerySecurityObject(
175 IN SECURITY_INFORMATION RequestedInformation
,
176 OUT PSECURITY_DESCRIPTOR pSecurityDesriptor
,
178 OUT PULONG ResultLength
)
180 static const SID_IDENTIFIER_AUTHORITY localSidAuthority
= {SECURITY_NT_AUTHORITY
};
181 static const SID_IDENTIFIER_AUTHORITY worldSidAuthority
= {SECURITY_WORLD_SID_AUTHORITY
};
183 PISECURITY_DESCRIPTOR_RELATIVE psd
= (PISECURITY_DESCRIPTOR_RELATIVE
)Buffer
;
184 UINT BufferIndex
= sizeof(SECURITY_DESCRIPTOR_RELATIVE
);
186 FIXME("(%p,0x%08x,%p,0x%08x,%p) stub!\n",
187 Object
, RequestedInformation
, pSecurityDesriptor
, Length
, ResultLength
);
189 RequestedInformation
&= 0x0000000f;
191 ZeroMemory(Buffer
, 256);
192 RtlCreateSecurityDescriptor((PSECURITY_DESCRIPTOR
)psd
, SECURITY_DESCRIPTOR_REVISION
);
193 psd
->Control
= SE_SELF_RELATIVE
|
194 ((RequestedInformation
& DACL_SECURITY_INFORMATION
) ? SE_DACL_PRESENT
:0);
196 /* owner: administrator S-1-5-20-220*/
197 if (OWNER_SECURITY_INFORMATION
& RequestedInformation
)
199 SID
* psid
= (SID
*)&(Buffer
[BufferIndex
]);
201 psd
->Owner
= BufferIndex
;
202 BufferIndex
+= RtlLengthRequiredSid(2);
204 psid
->Revision
= SID_REVISION
;
205 psid
->SubAuthorityCount
= 2;
206 psid
->IdentifierAuthority
= localSidAuthority
;
207 psid
->SubAuthority
[0] = SECURITY_BUILTIN_DOMAIN_RID
;
208 psid
->SubAuthority
[1] = DOMAIN_ALIAS_RID_ADMINS
;
211 /* group: built in domain S-1-5-12 */
212 if (GROUP_SECURITY_INFORMATION
& RequestedInformation
)
214 SID
* psid
= (SID
*) &(Buffer
[BufferIndex
]);
216 psd
->Group
= BufferIndex
;
217 BufferIndex
+= RtlLengthRequiredSid(1);
219 psid
->Revision
= SID_REVISION
;
220 psid
->SubAuthorityCount
= 1;
221 psid
->IdentifierAuthority
= localSidAuthority
;
222 psid
->SubAuthority
[0] = SECURITY_LOCAL_SYSTEM_RID
;
225 /* discretionary ACL */
226 if (DACL_SECURITY_INFORMATION
& RequestedInformation
)
229 PACL pacl
= (PACL
)&(Buffer
[BufferIndex
]);
230 PACCESS_ALLOWED_ACE pace
;
233 psd
->Dacl
= BufferIndex
;
235 pacl
->AclRevision
= MIN_ACL_REVISION
;
237 pacl
->AclSize
= BufferIndex
; /* storing the start index temporary */
239 BufferIndex
+= sizeof(ACL
);
241 /* ACE System - full access */
242 pace
= (PACCESS_ALLOWED_ACE
)&(Buffer
[BufferIndex
]);
243 BufferIndex
+= sizeof(ACCESS_ALLOWED_ACE
)-sizeof(DWORD
);
245 pace
->Header
.AceType
= ACCESS_ALLOWED_ACE_TYPE
;
246 pace
->Header
.AceFlags
= CONTAINER_INHERIT_ACE
;
247 pace
->Header
.AceSize
= sizeof(ACCESS_ALLOWED_ACE
)-sizeof(DWORD
) + RtlLengthRequiredSid(1);
248 pace
->Mask
= STANDARD_RIGHTS_ALL
| SPECIFIC_RIGHTS_ALL
;
249 pace
->SidStart
= BufferIndex
;
251 /* SID S-1-5-12 (System) */
252 psid
= (SID
*)&(Buffer
[BufferIndex
]);
254 BufferIndex
+= RtlLengthRequiredSid(1);
256 psid
->Revision
= SID_REVISION
;
257 psid
->SubAuthorityCount
= 1;
258 psid
->IdentifierAuthority
= localSidAuthority
;
259 psid
->SubAuthority
[0] = SECURITY_LOCAL_SYSTEM_RID
;
261 /* ACE Administrators - full access*/
262 pace
= (PACCESS_ALLOWED_ACE
) &(Buffer
[BufferIndex
]);
263 BufferIndex
+= sizeof(ACCESS_ALLOWED_ACE
)-sizeof(DWORD
);
265 pace
->Header
.AceType
= ACCESS_ALLOWED_ACE_TYPE
;
266 pace
->Header
.AceFlags
= CONTAINER_INHERIT_ACE
;
267 pace
->Header
.AceSize
= sizeof(ACCESS_ALLOWED_ACE
)-sizeof(DWORD
) + RtlLengthRequiredSid(2);
268 pace
->Mask
= STANDARD_RIGHTS_ALL
| SPECIFIC_RIGHTS_ALL
;
269 pace
->SidStart
= BufferIndex
;
271 /* S-1-5-12 (Administrators) */
272 psid
= (SID
*)&(Buffer
[BufferIndex
]);
274 BufferIndex
+= RtlLengthRequiredSid(2);
276 psid
->Revision
= SID_REVISION
;
277 psid
->SubAuthorityCount
= 2;
278 psid
->IdentifierAuthority
= localSidAuthority
;
279 psid
->SubAuthority
[0] = SECURITY_BUILTIN_DOMAIN_RID
;
280 psid
->SubAuthority
[1] = DOMAIN_ALIAS_RID_ADMINS
;
282 /* ACE Everyone - read access */
283 pace
= (PACCESS_ALLOWED_ACE
)&(Buffer
[BufferIndex
]);
284 BufferIndex
+= sizeof(ACCESS_ALLOWED_ACE
)-sizeof(DWORD
);
286 pace
->Header
.AceType
= ACCESS_ALLOWED_ACE_TYPE
;
287 pace
->Header
.AceFlags
= CONTAINER_INHERIT_ACE
;
288 pace
->Header
.AceSize
= sizeof(ACCESS_ALLOWED_ACE
)-sizeof(DWORD
) + RtlLengthRequiredSid(1);
289 pace
->Mask
= READ_CONTROL
| 0x19;
290 pace
->SidStart
= BufferIndex
;
292 /* SID S-1-1-0 (Everyone) */
293 psid
= (SID
*)&(Buffer
[BufferIndex
]);
295 BufferIndex
+= RtlLengthRequiredSid(1);
297 psid
->Revision
= SID_REVISION
;
298 psid
->SubAuthorityCount
= 1;
299 psid
->IdentifierAuthority
= worldSidAuthority
;
300 psid
->SubAuthority
[0] = 0;
302 /* calculate used bytes */
303 pacl
->AclSize
= BufferIndex
- pacl
->AclSize
;
305 *ResultLength
= BufferIndex
;
306 TRACE("len=%u\n", *ResultLength
);
307 if (Length
< *ResultLength
) return STATUS_BUFFER_TOO_SMALL
;
308 memcpy(pSecurityDesriptor
, Buffer
, *ResultLength
);
310 return STATUS_SUCCESS
;
314 /******************************************************************************
315 * NtDuplicateObject [NTDLL.@]
316 * ZwDuplicateObject [NTDLL.@]
318 NTSTATUS WINAPI
NtDuplicateObject( HANDLE source_process
, HANDLE source
,
319 HANDLE dest_process
, PHANDLE dest
,
320 ACCESS_MASK access
, ULONG attributes
, ULONG options
)
323 SERVER_START_REQ( dup_handle
)
325 req
->src_process
= source_process
;
326 req
->src_handle
= source
;
327 req
->dst_process
= dest_process
;
328 req
->access
= access
;
329 req
->attributes
= attributes
;
330 req
->options
= options
;
332 if (!(ret
= wine_server_call( req
)))
334 if (dest
) *dest
= reply
->handle
;
339 int fd
= server_remove_fd_from_cache( source
);
340 if (fd
!= -1) close( fd
);
343 else if (options
& DUPLICATE_CLOSE_SOURCE
)
344 WARN( "failed to close handle %p in process %p\n", source
, source_process
);
351 /**************************************************************************
354 * Close a handle reference to an object.
357 * Handle [I] handle to close
360 * Success: ERROR_SUCCESS.
361 * Failure: An NTSTATUS error code.
363 NTSTATUS WINAPI
NtClose( HANDLE Handle
)
366 int fd
= server_remove_fd_from_cache( Handle
);
368 SERVER_START_REQ( close_handle
)
370 req
->handle
= Handle
;
371 ret
= wine_server_call( req
);
374 if (fd
!= -1) close( fd
);
379 * Directory functions
382 /**************************************************************************
383 * NtOpenDirectoryObject [NTDLL.@]
384 * ZwOpenDirectoryObject [NTDLL.@]
386 * Open a namespace directory object.
389 * DirectoryHandle [O] Destination for the new directory handle
390 * DesiredAccess [I] Desired access to the directory
391 * ObjectAttributes [I] Structure describing the directory
394 * Success: ERROR_SUCCESS.
395 * Failure: An NTSTATUS error code.
397 NTSTATUS WINAPI
NtOpenDirectoryObject(PHANDLE DirectoryHandle
, ACCESS_MASK DesiredAccess
,
398 POBJECT_ATTRIBUTES ObjectAttributes
)
401 TRACE("(%p,0x%08x)\n", DirectoryHandle
, DesiredAccess
);
402 dump_ObjectAttributes(ObjectAttributes
);
404 if (!DirectoryHandle
) return STATUS_ACCESS_VIOLATION
;
405 if (!ObjectAttributes
) return STATUS_INVALID_PARAMETER
;
406 /* Have to test it here because server won't know difference between
407 * ObjectName == NULL and ObjectName == "" */
408 if (!ObjectAttributes
->ObjectName
)
410 if (ObjectAttributes
->RootDirectory
)
411 return STATUS_OBJECT_NAME_INVALID
;
413 return STATUS_OBJECT_PATH_SYNTAX_BAD
;
416 SERVER_START_REQ(open_directory
)
418 req
->access
= DesiredAccess
;
419 req
->attributes
= ObjectAttributes
->Attributes
;
420 req
->rootdir
= ObjectAttributes
->RootDirectory
;
421 if (ObjectAttributes
->ObjectName
)
422 wine_server_add_data(req
, ObjectAttributes
->ObjectName
->Buffer
,
423 ObjectAttributes
->ObjectName
->Length
);
424 ret
= wine_server_call( req
);
425 *DirectoryHandle
= reply
->handle
;
431 /******************************************************************************
432 * NtCreateDirectoryObject [NTDLL.@]
433 * ZwCreateDirectoryObject [NTDLL.@]
435 * Create a namespace directory object.
438 * DirectoryHandle [O] Destination for the new directory handle
439 * DesiredAccess [I] Desired access to the directory
440 * ObjectAttributes [I] Structure describing the directory
443 * Success: ERROR_SUCCESS.
444 * Failure: An NTSTATUS error code.
446 NTSTATUS WINAPI
NtCreateDirectoryObject(PHANDLE DirectoryHandle
, ACCESS_MASK DesiredAccess
,
447 POBJECT_ATTRIBUTES ObjectAttributes
)
450 TRACE("(%p,0x%08x)\n", DirectoryHandle
, DesiredAccess
);
451 dump_ObjectAttributes(ObjectAttributes
);
453 if (!DirectoryHandle
) return STATUS_ACCESS_VIOLATION
;
455 SERVER_START_REQ(create_directory
)
457 req
->access
= DesiredAccess
;
458 req
->attributes
= ObjectAttributes
? ObjectAttributes
->Attributes
: 0;
459 req
->rootdir
= ObjectAttributes
? ObjectAttributes
->RootDirectory
: 0;
460 if (ObjectAttributes
&& ObjectAttributes
->ObjectName
)
461 wine_server_add_data(req
, ObjectAttributes
->ObjectName
->Buffer
,
462 ObjectAttributes
->ObjectName
->Length
);
463 ret
= wine_server_call( req
);
464 *DirectoryHandle
= reply
->handle
;
470 /******************************************************************************
471 * NtQueryDirectoryObject [NTDLL.@]
472 * ZwQueryDirectoryObject [NTDLL.@]
474 * Read information from a namespace directory.
477 * DirectoryHandle [I] Handle to a directory object
478 * Buffer [O] Buffer to hold the read data
479 * BufferLength [I] Size of the buffer in bytes
480 * ReturnSingleEntry [I] If TRUE, return a single entry, if FALSE, return as many as fit in the buffer
481 * RestartScan [I] If TRUE, start scanning from the start, if FALSE, scan from Context
482 * Context [I/O] Indicates what point of the directory the scan is at
483 * ReturnLength [O] Caller supplied storage for the number of bytes written (or NULL)
486 * Success: ERROR_SUCCESS.
487 * Failure: An NTSTATUS error code.
489 NTSTATUS WINAPI
NtQueryDirectoryObject(IN HANDLE DirectoryHandle
, OUT PDIRECTORY_BASIC_INFORMATION Buffer
,
490 IN ULONG BufferLength
, IN BOOLEAN ReturnSingleEntry
, IN BOOLEAN RestartScan
,
491 IN OUT PULONG Context
, OUT PULONG ReturnLength OPTIONAL
)
493 FIXME("(%p,%p,0x%08x,0x%08x,0x%08x,%p,%p), stub\n", DirectoryHandle
, Buffer
, BufferLength
, ReturnSingleEntry
,
494 RestartScan
, Context
, ReturnLength
);
496 return STATUS_NOT_IMPLEMENTED
;
503 /******************************************************************************
504 * NtOpenSymbolicLinkObject [NTDLL.@]
505 * ZwOpenSymbolicLinkObject [NTDLL.@]
507 * Open a namespace symbolic link object.
510 * LinkHandle [O] Destination for the new symbolic link handle
511 * DesiredAccess [I] Desired access to the symbolic link
512 * ObjectAttributes [I] Structure describing the symbolic link
515 * Success: ERROR_SUCCESS.
516 * Failure: An NTSTATUS error code.
518 NTSTATUS WINAPI
NtOpenSymbolicLinkObject(OUT PHANDLE LinkHandle
, IN ACCESS_MASK DesiredAccess
,
519 IN POBJECT_ATTRIBUTES ObjectAttributes
)
522 TRACE("(%p,0x%08x,%p)\n",LinkHandle
, DesiredAccess
, ObjectAttributes
);
523 dump_ObjectAttributes(ObjectAttributes
);
525 if (!LinkHandle
) return STATUS_ACCESS_VIOLATION
;
526 if (!ObjectAttributes
) return STATUS_INVALID_PARAMETER
;
527 /* Have to test it here because server won't know difference between
528 * ObjectName == NULL and ObjectName == "" */
529 if (!ObjectAttributes
->ObjectName
)
531 if (ObjectAttributes
->RootDirectory
)
532 return STATUS_OBJECT_NAME_INVALID
;
534 return STATUS_OBJECT_PATH_SYNTAX_BAD
;
537 SERVER_START_REQ(open_symlink
)
539 req
->access
= DesiredAccess
;
540 req
->attributes
= ObjectAttributes
->Attributes
;
541 req
->rootdir
= ObjectAttributes
->RootDirectory
;
542 if (ObjectAttributes
->ObjectName
)
543 wine_server_add_data(req
, ObjectAttributes
->ObjectName
->Buffer
,
544 ObjectAttributes
->ObjectName
->Length
);
545 ret
= wine_server_call( req
);
546 *LinkHandle
= reply
->handle
;
552 /******************************************************************************
553 * NtCreateSymbolicLinkObject [NTDLL.@]
554 * ZwCreateSymbolicLinkObject [NTDLL.@]
556 * Open a namespace symbolic link object.
559 * SymbolicLinkHandle [O] Destination for the new symbolic link handle
560 * DesiredAccess [I] Desired access to the symbolic link
561 * ObjectAttributes [I] Structure describing the symbolic link
562 * TargetName [I] Name of the target symbolic link points to
565 * Success: ERROR_SUCCESS.
566 * Failure: An NTSTATUS error code.
568 NTSTATUS WINAPI
NtCreateSymbolicLinkObject(OUT PHANDLE SymbolicLinkHandle
,IN ACCESS_MASK DesiredAccess
,
569 IN POBJECT_ATTRIBUTES ObjectAttributes
,
570 IN PUNICODE_STRING TargetName
)
573 TRACE("(%p,0x%08x,%p, -> %s)\n", SymbolicLinkHandle
, DesiredAccess
, ObjectAttributes
,
574 debugstr_us(TargetName
));
575 dump_ObjectAttributes(ObjectAttributes
);
577 if (!SymbolicLinkHandle
|| !TargetName
) return STATUS_ACCESS_VIOLATION
;
578 if (!TargetName
->Buffer
) return STATUS_INVALID_PARAMETER
;
580 SERVER_START_REQ(create_symlink
)
582 req
->access
= DesiredAccess
;
583 req
->attributes
= ObjectAttributes
? ObjectAttributes
->Attributes
: 0;
584 req
->rootdir
= ObjectAttributes
? ObjectAttributes
->RootDirectory
: 0;
585 if (ObjectAttributes
&& ObjectAttributes
->ObjectName
)
587 req
->name_len
= ObjectAttributes
->ObjectName
->Length
;
588 wine_server_add_data(req
, ObjectAttributes
->ObjectName
->Buffer
,
589 ObjectAttributes
->ObjectName
->Length
);
593 wine_server_add_data(req
, TargetName
->Buffer
, TargetName
->Length
);
594 ret
= wine_server_call( req
);
595 *SymbolicLinkHandle
= reply
->handle
;
601 /******************************************************************************
602 * NtQuerySymbolicLinkObject [NTDLL.@]
603 * ZwQuerySymbolicLinkObject [NTDLL.@]
605 * Query a namespace symbolic link object target name.
608 * LinkHandle [I] Handle to a symbolic link object
609 * LinkTarget [O] Destination for the symbolic link target
610 * ReturnedLength [O] Size of returned data
613 * Success: ERROR_SUCCESS.
614 * Failure: An NTSTATUS error code.
616 NTSTATUS WINAPI
NtQuerySymbolicLinkObject(IN HANDLE LinkHandle
, IN OUT PUNICODE_STRING LinkTarget
,
617 OUT PULONG ReturnedLength OPTIONAL
)
620 TRACE("(%p,%p,%p)\n", LinkHandle
, LinkTarget
, ReturnedLength
);
622 if (!LinkTarget
) return STATUS_ACCESS_VIOLATION
;
624 SERVER_START_REQ(query_symlink
)
626 req
->handle
= LinkHandle
;
627 wine_server_set_reply( req
, LinkTarget
->Buffer
, LinkTarget
->MaximumLength
);
628 if (!(ret
= wine_server_call( req
)))
630 LinkTarget
->Length
= wine_server_reply_size(reply
);
631 if (ReturnedLength
) *ReturnedLength
= LinkTarget
->Length
;
638 /******************************************************************************
639 * NtAllocateUuids [NTDLL.@]
641 NTSTATUS WINAPI
NtAllocateUuids(
642 PULARGE_INTEGER Time
,
646 FIXME("(%p,%p,%p), stub.\n", Time
, Range
, Sequence
);