3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/cm/regobj.c
6 * PURPOSE: Registry object manipulation routines.
8 * PROGRAMMERS: No programmer listed.
13 #include <internal/debug.h>
17 extern LIST_ENTRY CmiKeyObjectListHead
;
18 extern ULONG CmiTimer
;
21 CmiGetLinkTarget(PREGISTRY_HIVE RegistryHive
,
23 PUNICODE_STRING TargetPath
);
25 /* FUNCTONS *****************************************************************/
28 CmiObjectParse(PVOID ParsedObject
,
30 PUNICODE_STRING FullPath
,
34 BLOCK_OFFSET BlockOffset
;
35 PKEY_OBJECT FoundObject
;
36 PKEY_OBJECT ParsedKey
;
42 UNICODE_STRING LinkPath
;
43 UNICODE_STRING TargetPath
;
44 UNICODE_STRING KeyName
;
46 ParsedKey
= ParsedObject
;
48 VERIFY_KEY_OBJECT(ParsedKey
);
54 DPRINT("*Path is NULL\n");
55 return STATUS_UNSUCCESSFUL
;
58 DPRINT("Path '%S'\n", *Path
);
60 /* Extract relevant path name */
62 if (*StartPtr
== L
'\\')
65 EndPtr
= wcschr(StartPtr
, L
'\\');
67 Length
= ((PCHAR
)EndPtr
- (PCHAR
)StartPtr
) / sizeof(WCHAR
);
69 Length
= wcslen(StartPtr
);
72 KeyName
.Length
= Length
* sizeof(WCHAR
);
73 KeyName
.MaximumLength
= KeyName
.Length
+ sizeof(WCHAR
);
74 KeyName
.Buffer
= ExAllocatePool(NonPagedPool
,
75 KeyName
.MaximumLength
);
76 RtlCopyMemory(KeyName
.Buffer
,
79 KeyName
.Buffer
[KeyName
.Length
/ sizeof(WCHAR
)] = 0;
81 /* Acquire hive lock */
82 KeEnterCriticalRegion();
83 ExAcquireResourceExclusiveLite(&CmiRegistryLock
, TRUE
);
86 Status
= CmiScanKeyList(ParsedKey
,
90 if (!NT_SUCCESS(Status
))
92 ExReleaseResourceLite(&CmiRegistryLock
);
93 KeLeaveCriticalRegion();
94 RtlFreeUnicodeString(&KeyName
);
97 if (FoundObject
== NULL
)
99 Status
= CmiScanForSubKey(ParsedKey
->RegistryHive
,
106 if (!NT_SUCCESS(Status
) || (SubKeyCell
== NULL
))
108 ExReleaseResourceLite(&CmiRegistryLock
);
109 KeLeaveCriticalRegion();
110 RtlFreeUnicodeString(&KeyName
);
111 return(STATUS_UNSUCCESSFUL
);
114 if ((SubKeyCell
->Flags
& REG_KEY_LINK_CELL
) &&
115 !((Attributes
& OBJ_OPENLINK
) && (EndPtr
== NULL
)))
117 RtlInitUnicodeString(&LinkPath
, NULL
);
118 Status
= CmiGetLinkTarget(ParsedKey
->RegistryHive
,
121 if (NT_SUCCESS(Status
))
123 ExReleaseResourceLite(&CmiRegistryLock
);
124 KeLeaveCriticalRegion();
126 DPRINT("LinkPath '%wZ'\n", &LinkPath
);
128 /* build new FullPath for reparsing */
129 TargetPath
.MaximumLength
= LinkPath
.MaximumLength
;
132 TargetPath
.MaximumLength
+= (wcslen(EndPtr
) * sizeof(WCHAR
));
134 TargetPath
.Length
= TargetPath
.MaximumLength
- sizeof(WCHAR
);
135 TargetPath
.Buffer
= ExAllocatePool(NonPagedPool
,
136 TargetPath
.MaximumLength
);
137 wcscpy(TargetPath
.Buffer
, LinkPath
.Buffer
);
140 wcscat(TargetPath
.Buffer
, EndPtr
);
143 RtlFreeUnicodeString(FullPath
);
144 RtlFreeUnicodeString(&LinkPath
);
145 FullPath
->Length
= TargetPath
.Length
;
146 FullPath
->MaximumLength
= TargetPath
.MaximumLength
;
147 FullPath
->Buffer
= TargetPath
.Buffer
;
149 DPRINT("FullPath '%wZ'\n", FullPath
);
151 /* reinitialize Path for reparsing */
152 *Path
= FullPath
->Buffer
;
156 RtlFreeUnicodeString(&KeyName
);
157 return(STATUS_REPARSE
);
161 /* Create new key object and put into linked list */
162 DPRINT("CmiObjectParse: %S\n", *Path
);
163 Status
= ObCreateObject(KernelMode
,
171 (PVOID
*)&FoundObject
);
172 if (!NT_SUCCESS(Status
))
174 ExReleaseResourceLite(&CmiRegistryLock
);
175 KeLeaveCriticalRegion();
176 RtlFreeUnicodeString(&KeyName
);
179 DPRINT("Inserting Key into Object Tree\n");
180 Status
= ObInsertObject((PVOID
)FoundObject
,
186 DPRINT("Status %x\n", Status
);
188 /* Add the keep-alive reference */
189 ObReferenceObject(FoundObject
);
191 FoundObject
->Flags
= 0;
192 FoundObject
->KeyCell
= SubKeyCell
;
193 FoundObject
->KeyCellOffset
= BlockOffset
;
194 FoundObject
->RegistryHive
= ParsedKey
->RegistryHive
;
195 InsertTailList(&CmiKeyObjectListHead
, &FoundObject
->ListEntry
);
196 RtlpCreateUnicodeString(&FoundObject
->Name
,
197 KeyName
.Buffer
, NonPagedPool
);
198 CmiAddKeyToList(ParsedKey
, FoundObject
);
199 DPRINT("Created object 0x%p\n", FoundObject
);
203 if ((FoundObject
->KeyCell
->Flags
& REG_KEY_LINK_CELL
) &&
204 !((Attributes
& OBJ_OPENLINK
) && (EndPtr
== NULL
)))
206 DPRINT("Found link\n");
208 RtlInitUnicodeString(&LinkPath
, NULL
);
209 Status
= CmiGetLinkTarget(FoundObject
->RegistryHive
,
210 FoundObject
->KeyCell
,
212 if (NT_SUCCESS(Status
))
214 DPRINT("LinkPath '%wZ'\n", &LinkPath
);
216 ExReleaseResourceLite(&CmiRegistryLock
);
217 KeLeaveCriticalRegion();
219 ObDereferenceObject(FoundObject
);
221 /* build new FullPath for reparsing */
222 TargetPath
.MaximumLength
= LinkPath
.MaximumLength
;
225 TargetPath
.MaximumLength
+= (wcslen(EndPtr
) * sizeof(WCHAR
));
227 TargetPath
.Length
= TargetPath
.MaximumLength
- sizeof(WCHAR
);
228 TargetPath
.Buffer
= ExAllocatePool(NonPagedPool
,
229 TargetPath
.MaximumLength
);
230 wcscpy(TargetPath
.Buffer
, LinkPath
.Buffer
);
233 wcscat(TargetPath
.Buffer
, EndPtr
);
236 RtlFreeUnicodeString(FullPath
);
237 RtlFreeUnicodeString(&LinkPath
);
238 FullPath
->Length
= TargetPath
.Length
;
239 FullPath
->MaximumLength
= TargetPath
.MaximumLength
;
240 FullPath
->Buffer
= TargetPath
.Buffer
;
242 DPRINT("FullPath '%wZ'\n", FullPath
);
244 /* reinitialize Path for reparsing */
245 *Path
= FullPath
->Buffer
;
249 RtlFreeUnicodeString(&KeyName
);
250 return(STATUS_REPARSE
);
255 RemoveEntryList(&FoundObject
->ListEntry
);
256 InsertHeadList(&CmiKeyObjectListHead
, &FoundObject
->ListEntry
);
257 FoundObject
->TimeStamp
= CmiTimer
;
259 ExReleaseResourceLite(&CmiRegistryLock
);
260 KeLeaveCriticalRegion();
262 DPRINT("CmiObjectParse: %s\n", FoundObject
->Name
);
266 VERIFY_KEY_OBJECT(FoundObject
);
268 *NextObject
= FoundObject
;
270 RtlFreeUnicodeString(&KeyName
);
272 return(STATUS_SUCCESS
);
276 CmiObjectDelete(PVOID DeletedObject
)
278 PKEY_OBJECT ParentKeyObject
;
279 PKEY_OBJECT KeyObject
;
281 DPRINT("Delete key object (%p)\n", DeletedObject
);
283 KeyObject
= (PKEY_OBJECT
) DeletedObject
;
284 ParentKeyObject
= KeyObject
->ParentKey
;
286 ObReferenceObject (ParentKeyObject
);
288 /* Acquire hive lock */
289 KeEnterCriticalRegion();
290 ExAcquireResourceExclusiveLite(&CmiRegistryLock
, TRUE
);
292 if (!NT_SUCCESS(CmiRemoveKeyFromList(KeyObject
)))
294 DPRINT1("Key not found in parent list ???\n");
297 RemoveEntryList(&KeyObject
->ListEntry
);
298 RtlFreeUnicodeString(&KeyObject
->Name
);
300 if (KeyObject
->Flags
& KO_MARKED_FOR_DELETE
)
302 DPRINT("delete really key\n");
304 CmiRemoveSubKey(KeyObject
->RegistryHive
,
308 KeQuerySystemTime (&ParentKeyObject
->KeyCell
->LastWriteTime
);
309 CmiMarkBlockDirty (ParentKeyObject
->RegistryHive
,
310 ParentKeyObject
->KeyCellOffset
);
312 if (!IsNoFileHive (KeyObject
->RegistryHive
) ||
313 !IsNoFileHive (ParentKeyObject
->RegistryHive
))
319 ObDereferenceObject (ParentKeyObject
);
321 ExReleaseResourceLite(&CmiRegistryLock
);
322 KeLeaveCriticalRegion();
324 if (KeyObject
->NumberOfSubKeys
)
326 KEBUGCHECK(REGISTRY_ERROR
);
329 if (KeyObject
->SizeOfSubKeys
)
331 ExFreePool(KeyObject
->SubKeys
);
337 CmiQuerySecurityDescriptor(PKEY_OBJECT KeyObject
,
338 SECURITY_INFORMATION SecurityInformation
,
339 PSECURITY_DESCRIPTOR SecurityDescriptor
,
347 DPRINT("CmiQuerySecurityDescriptor() called\n");
351 * This is a big hack!!
352 * We need to retrieve the security descriptor from the keys security cell!
355 if (SecurityInformation
== 0)
357 return STATUS_ACCESS_DENIED
;
360 SidSize
= RtlLengthSid(SeWorldSid
);
361 SdSize
= sizeof(SECURITY_DESCRIPTOR
) + (2 * SidSize
);
363 if (*BufferLength
< SdSize
)
365 *BufferLength
= SdSize
;
366 return STATUS_BUFFER_TOO_SMALL
;
369 *BufferLength
= SdSize
;
371 Status
= RtlCreateSecurityDescriptor(SecurityDescriptor
,
372 SECURITY_DESCRIPTOR_REVISION
);
373 if (!NT_SUCCESS(Status
))
378 SecurityDescriptor
->Control
|= SE_SELF_RELATIVE
;
379 Current
= (ULONG_PTR
)SecurityDescriptor
+ sizeof(SECURITY_DESCRIPTOR
);
381 if (SecurityInformation
& OWNER_SECURITY_INFORMATION
)
383 RtlCopyMemory((PVOID
)Current
,
386 SecurityDescriptor
->Owner
= (PSID
)((ULONG_PTR
)Current
- (ULONG_PTR
)SecurityDescriptor
);
390 if (SecurityInformation
& GROUP_SECURITY_INFORMATION
)
392 RtlCopyMemory((PVOID
)Current
,
395 SecurityDescriptor
->Group
= (PSID
)((ULONG_PTR
)Current
- (ULONG_PTR
)SecurityDescriptor
);
399 if (SecurityInformation
& DACL_SECURITY_INFORMATION
)
401 SecurityDescriptor
->Control
|= SE_DACL_PRESENT
;
404 if (SecurityInformation
& SACL_SECURITY_INFORMATION
)
406 SecurityDescriptor
->Control
|= SE_SACL_PRESENT
;
409 return STATUS_SUCCESS
;
414 CmiAssignSecurityDescriptor(PKEY_OBJECT KeyObject
,
415 PSECURITY_DESCRIPTOR SecurityDescriptor
)
420 DPRINT1("CmiAssignSecurityDescriptor() callled\n");
422 DPRINT1("KeyObject %p\n", KeyObject
);
423 DPRINT1("KeyObject->RegistryHive %p\n", KeyObject
->RegistryHive
);
425 Hive
= KeyObject
->RegistryHive
;
428 DPRINT1("Create new root security cell\n");
429 return STATUS_SUCCESS
;
432 if (Hive
->RootSecurityCell
== NULL
)
434 DPRINT1("Create new root security cell\n");
439 DPRINT1("Search for security cell\n");
444 return STATUS_SUCCESS
;
449 CmiObjectSecurity(PVOID ObjectBody
,
450 SECURITY_OPERATION_CODE OperationCode
,
451 SECURITY_INFORMATION SecurityInformation
,
452 PSECURITY_DESCRIPTOR SecurityDescriptor
,
455 DPRINT("CmiObjectSecurity() called\n");
457 switch (OperationCode
)
459 case SetSecurityDescriptor
:
460 DPRINT("Set security descriptor\n");
461 return STATUS_SUCCESS
;
463 case QuerySecurityDescriptor
:
464 DPRINT("Query security descriptor\n");
465 return CmiQuerySecurityDescriptor((PKEY_OBJECT
)ObjectBody
,
470 case DeleteSecurityDescriptor
:
471 DPRINT("Delete security descriptor\n");
472 return STATUS_SUCCESS
;
474 case AssignSecurityDescriptor
:
475 DPRINT("Assign security descriptor\n");
476 return CmiAssignSecurityDescriptor((PKEY_OBJECT
)ObjectBody
,
480 return STATUS_UNSUCCESSFUL
;
485 CmiObjectQueryName (PVOID ObjectBody
,
486 POBJECT_NAME_INFORMATION ObjectNameInfo
,
490 POBJECT_NAME_INFORMATION LocalInfo
;
491 PKEY_OBJECT KeyObject
;
492 ULONG LocalReturnLength
;
495 DPRINT ("CmiObjectQueryName() called\n");
497 KeyObject
= (PKEY_OBJECT
)ObjectBody
;
499 LocalInfo
= ExAllocatePool (NonPagedPool
,
500 sizeof(OBJECT_NAME_INFORMATION
) +
501 MAX_PATH
* sizeof(WCHAR
));
502 if (LocalInfo
== NULL
)
503 return STATUS_INSUFFICIENT_RESOURCES
;
505 if (KeyObject
->ParentKey
!= KeyObject
)
507 Status
= ObQueryNameString (KeyObject
->ParentKey
,
509 MAX_PATH
* sizeof(WCHAR
),
514 /* KeyObject is the root key */
515 Status
= ObQueryNameString (HEADER_TO_OBJECT_NAME(BODY_TO_HEADER(KeyObject
))->Directory
,
517 MAX_PATH
* sizeof(WCHAR
),
521 if (!NT_SUCCESS (Status
))
523 ExFreePool (LocalInfo
);
526 DPRINT ("Parent path: %wZ\n", &LocalInfo
->Name
);
528 Status
= RtlAppendUnicodeStringToString (&ObjectNameInfo
->Name
,
530 ExFreePool (LocalInfo
);
531 if (!NT_SUCCESS (Status
))
534 Status
= RtlAppendUnicodeToString (&ObjectNameInfo
->Name
,
536 if (!NT_SUCCESS (Status
))
539 Status
= RtlAppendUnicodeStringToString (&ObjectNameInfo
->Name
,
541 if (NT_SUCCESS (Status
))
543 DPRINT ("Total path: %wZ\n", &ObjectNameInfo
->Name
);
551 CmiAddKeyToList(PKEY_OBJECT ParentKey
,
555 DPRINT("ParentKey %.08x\n", ParentKey
);
558 if (ParentKey
->SizeOfSubKeys
<= ParentKey
->NumberOfSubKeys
)
560 PKEY_OBJECT
*tmpSubKeys
= ExAllocatePool(NonPagedPool
,
561 (ParentKey
->NumberOfSubKeys
+ 1) * sizeof(ULONG
));
563 if (ParentKey
->NumberOfSubKeys
> 0)
565 RtlCopyMemory (tmpSubKeys
,
567 ParentKey
->NumberOfSubKeys
* sizeof(ULONG
));
570 if (ParentKey
->SubKeys
)
571 ExFreePool(ParentKey
->SubKeys
);
573 ParentKey
->SubKeys
= tmpSubKeys
;
574 ParentKey
->SizeOfSubKeys
= ParentKey
->NumberOfSubKeys
+ 1;
577 /* FIXME: Please maintain the list in alphabetic order */
578 /* to allow a dichotomic search */
579 ParentKey
->SubKeys
[ParentKey
->NumberOfSubKeys
++] = NewKey
;
581 DPRINT("Reference parent key: 0x%p\n", ParentKey
);
583 ObReferenceObjectByPointer(ParentKey
,
584 STANDARD_RIGHTS_REQUIRED
,
587 NewKey
->ParentKey
= ParentKey
;
592 CmiRemoveKeyFromList(PKEY_OBJECT KeyToRemove
)
594 PKEY_OBJECT ParentKey
;
597 ParentKey
= KeyToRemove
->ParentKey
;
598 /* FIXME: If list maintained in alphabetic order, use dichotomic search */
599 for (Index
= 0; Index
< ParentKey
->NumberOfSubKeys
; Index
++)
601 if (ParentKey
->SubKeys
[Index
] == KeyToRemove
)
603 if (Index
< ParentKey
->NumberOfSubKeys
-1)
604 RtlMoveMemory(&ParentKey
->SubKeys
[Index
],
605 &ParentKey
->SubKeys
[Index
+ 1],
606 (ParentKey
->NumberOfSubKeys
- Index
- 1) * sizeof(PKEY_OBJECT
));
607 ParentKey
->NumberOfSubKeys
--;
609 DPRINT("Dereference parent key: 0x%x\n", ParentKey
);
611 ObDereferenceObject(ParentKey
);
612 return STATUS_SUCCESS
;
616 return STATUS_UNSUCCESSFUL
;
621 CmiScanKeyList(PKEY_OBJECT Parent
,
622 PUNICODE_STRING KeyName
,
624 PKEY_OBJECT
* ReturnedObject
)
629 DPRINT("Scanning key list for: %wZ (Parent: %wZ)\n",
630 KeyName
, &Parent
->Name
);
632 /* FIXME: if list maintained in alphabetic order, use dichotomic search */
633 for (Index
=0; Index
< Parent
->NumberOfSubKeys
; Index
++)
635 CurKey
= Parent
->SubKeys
[Index
];
636 if (Attributes
& OBJ_CASE_INSENSITIVE
)
638 if ((KeyName
->Length
== CurKey
->Name
.Length
)
639 && (_wcsicmp(KeyName
->Buffer
, CurKey
->Name
.Buffer
) == 0))
646 if ((KeyName
->Length
== CurKey
->Name
.Length
)
647 && (wcscmp(KeyName
->Buffer
, CurKey
->Name
.Buffer
) == 0))
654 if (Index
< Parent
->NumberOfSubKeys
)
656 if (CurKey
->Flags
& KO_MARKED_FOR_DELETE
)
658 *ReturnedObject
= NULL
;
659 return STATUS_UNSUCCESSFUL
;
661 ObReferenceObject(CurKey
);
662 *ReturnedObject
= CurKey
;
666 *ReturnedObject
= NULL
;
668 return STATUS_SUCCESS
;
673 CmiGetLinkTarget(PREGISTRY_HIVE RegistryHive
,
675 PUNICODE_STRING TargetPath
)
677 UNICODE_STRING LinkName
= RTL_CONSTANT_STRING(L
"SymbolicLinkValue");
678 PVALUE_CELL ValueCell
;
682 DPRINT("CmiGetLinkTarget() called\n");
684 /* Get Value block of interest */
685 Status
= CmiScanKeyForValue(RegistryHive
,
690 if (!NT_SUCCESS(Status
))
692 DPRINT1("CmiScanKeyForValue() failed (Status %lx)\n", Status
);
696 if (ValueCell
->DataType
!= REG_LINK
)
698 DPRINT1("Type != REG_LINK\n!");
699 return(STATUS_UNSUCCESSFUL
);
702 if (TargetPath
->Buffer
== NULL
&& TargetPath
->MaximumLength
== 0)
704 TargetPath
->Length
= 0;
705 TargetPath
->MaximumLength
= ValueCell
->DataSize
+ sizeof(WCHAR
);
706 TargetPath
->Buffer
= ExAllocatePool(NonPagedPool
,
707 TargetPath
->MaximumLength
);
710 TargetPath
->Length
= min(TargetPath
->MaximumLength
- sizeof(WCHAR
),
711 (ULONG
) ValueCell
->DataSize
);
713 if (ValueCell
->DataSize
> 0)
715 DataCell
= CmiGetCell (RegistryHive
, ValueCell
->DataOffset
, NULL
);
716 RtlCopyMemory(TargetPath
->Buffer
,
719 TargetPath
->Buffer
[TargetPath
->Length
/ sizeof(WCHAR
)] = 0;
723 RtlCopyMemory(TargetPath
->Buffer
,
724 &ValueCell
->DataOffset
,
726 TargetPath
->Buffer
[TargetPath
->Length
/ sizeof(WCHAR
)] = 0;
729 DPRINT("TargetPath '%wZ'\n", TargetPath
);
731 return(STATUS_SUCCESS
);