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 PISECURITY_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
,
454 PSECURITY_DESCRIPTOR
*OldSecurityDescriptor
,
456 PGENERIC_MAPPING GenericMapping
)
458 DPRINT("CmiObjectSecurity() called\n");
460 switch (OperationCode
)
462 case SetSecurityDescriptor
:
463 DPRINT("Set security descriptor\n");
464 return STATUS_SUCCESS
;
466 case QuerySecurityDescriptor
:
467 DPRINT("Query security descriptor\n");
468 return CmiQuerySecurityDescriptor((PKEY_OBJECT
)ObjectBody
,
473 case DeleteSecurityDescriptor
:
474 DPRINT("Delete security descriptor\n");
475 return STATUS_SUCCESS
;
477 case AssignSecurityDescriptor
:
478 DPRINT("Assign security descriptor\n");
479 return CmiAssignSecurityDescriptor((PKEY_OBJECT
)ObjectBody
,
483 return STATUS_UNSUCCESSFUL
;
488 CmiObjectQueryName (PVOID ObjectBody
,
489 POBJECT_NAME_INFORMATION ObjectNameInfo
,
493 PKEY_OBJECT KeyObject
;
496 DPRINT ("CmiObjectQueryName() called\n");
498 KeyObject
= (PKEY_OBJECT
)ObjectBody
;
500 if (KeyObject
->ParentKey
!= KeyObject
)
502 Status
= ObQueryNameString (KeyObject
->ParentKey
,
509 /* KeyObject is the root key */
510 Status
= ObQueryNameString (HEADER_TO_OBJECT_NAME(BODY_TO_HEADER(KeyObject
))->Directory
,
516 if (!NT_SUCCESS(Status
) && Status
!= STATUS_INFO_LENGTH_MISMATCH
)
520 (*ReturnLength
) += sizeof(WCHAR
) + KeyObject
->Name
.Length
;
522 if (Status
== STATUS_INFO_LENGTH_MISMATCH
|| *ReturnLength
> Length
)
524 return STATUS_INFO_LENGTH_MISMATCH
;
527 if (ObjectNameInfo
->Name
.Buffer
== NULL
)
529 ObjectNameInfo
->Name
.Buffer
= (PWCHAR
)(ObjectNameInfo
+ 1);
530 ObjectNameInfo
->Name
.Length
= 0;
531 ObjectNameInfo
->Name
.MaximumLength
= Length
- sizeof(OBJECT_NAME_INFORMATION
);
535 DPRINT ("Parent path: %wZ\n", ObjectNameInfo
->Name
);
537 Status
= RtlAppendUnicodeToString (&ObjectNameInfo
->Name
,
539 if (!NT_SUCCESS (Status
))
542 Status
= RtlAppendUnicodeStringToString (&ObjectNameInfo
->Name
,
544 if (NT_SUCCESS (Status
))
546 DPRINT ("Total path: %wZ\n", &ObjectNameInfo
->Name
);
554 CmiAddKeyToList(PKEY_OBJECT ParentKey
,
558 DPRINT("ParentKey %.08x\n", ParentKey
);
561 if (ParentKey
->SizeOfSubKeys
<= ParentKey
->NumberOfSubKeys
)
563 PKEY_OBJECT
*tmpSubKeys
= ExAllocatePool(NonPagedPool
,
564 (ParentKey
->NumberOfSubKeys
+ 1) * sizeof(ULONG
));
566 if (ParentKey
->NumberOfSubKeys
> 0)
568 RtlCopyMemory (tmpSubKeys
,
570 ParentKey
->NumberOfSubKeys
* sizeof(ULONG
));
573 if (ParentKey
->SubKeys
)
574 ExFreePool(ParentKey
->SubKeys
);
576 ParentKey
->SubKeys
= tmpSubKeys
;
577 ParentKey
->SizeOfSubKeys
= ParentKey
->NumberOfSubKeys
+ 1;
580 /* FIXME: Please maintain the list in alphabetic order */
581 /* to allow a dichotomic search */
582 ParentKey
->SubKeys
[ParentKey
->NumberOfSubKeys
++] = NewKey
;
584 DPRINT("Reference parent key: 0x%p\n", ParentKey
);
586 ObReferenceObjectByPointer(ParentKey
,
587 STANDARD_RIGHTS_REQUIRED
,
590 NewKey
->ParentKey
= ParentKey
;
595 CmiRemoveKeyFromList(PKEY_OBJECT KeyToRemove
)
597 PKEY_OBJECT ParentKey
;
600 ParentKey
= KeyToRemove
->ParentKey
;
601 /* FIXME: If list maintained in alphabetic order, use dichotomic search */
602 for (Index
= 0; Index
< ParentKey
->NumberOfSubKeys
; Index
++)
604 if (ParentKey
->SubKeys
[Index
] == KeyToRemove
)
606 if (Index
< ParentKey
->NumberOfSubKeys
-1)
607 RtlMoveMemory(&ParentKey
->SubKeys
[Index
],
608 &ParentKey
->SubKeys
[Index
+ 1],
609 (ParentKey
->NumberOfSubKeys
- Index
- 1) * sizeof(PKEY_OBJECT
));
610 ParentKey
->NumberOfSubKeys
--;
612 DPRINT("Dereference parent key: 0x%x\n", ParentKey
);
614 ObDereferenceObject(ParentKey
);
615 return STATUS_SUCCESS
;
619 return STATUS_UNSUCCESSFUL
;
624 CmiScanKeyList(PKEY_OBJECT Parent
,
625 PUNICODE_STRING KeyName
,
627 PKEY_OBJECT
* ReturnedObject
)
632 DPRINT("Scanning key list for: %wZ (Parent: %wZ)\n",
633 KeyName
, &Parent
->Name
);
635 /* FIXME: if list maintained in alphabetic order, use dichotomic search */
636 for (Index
=0; Index
< Parent
->NumberOfSubKeys
; Index
++)
638 CurKey
= Parent
->SubKeys
[Index
];
639 if (Attributes
& OBJ_CASE_INSENSITIVE
)
641 if ((KeyName
->Length
== CurKey
->Name
.Length
)
642 && (_wcsicmp(KeyName
->Buffer
, CurKey
->Name
.Buffer
) == 0))
649 if ((KeyName
->Length
== CurKey
->Name
.Length
)
650 && (wcscmp(KeyName
->Buffer
, CurKey
->Name
.Buffer
) == 0))
657 if (Index
< Parent
->NumberOfSubKeys
)
659 if (CurKey
->Flags
& KO_MARKED_FOR_DELETE
)
661 *ReturnedObject
= NULL
;
662 return STATUS_UNSUCCESSFUL
;
664 ObReferenceObject(CurKey
);
665 *ReturnedObject
= CurKey
;
669 *ReturnedObject
= NULL
;
671 return STATUS_SUCCESS
;
676 CmiGetLinkTarget(PREGISTRY_HIVE RegistryHive
,
678 PUNICODE_STRING TargetPath
)
680 UNICODE_STRING LinkName
= RTL_CONSTANT_STRING(L
"SymbolicLinkValue");
681 PVALUE_CELL ValueCell
;
685 DPRINT("CmiGetLinkTarget() called\n");
687 /* Get Value block of interest */
688 Status
= CmiScanKeyForValue(RegistryHive
,
693 if (!NT_SUCCESS(Status
))
695 DPRINT1("CmiScanKeyForValue() failed (Status %lx)\n", Status
);
699 if (ValueCell
->DataType
!= REG_LINK
)
701 DPRINT1("Type != REG_LINK\n!");
702 return(STATUS_UNSUCCESSFUL
);
705 if (TargetPath
->Buffer
== NULL
&& TargetPath
->MaximumLength
== 0)
707 TargetPath
->Length
= 0;
708 TargetPath
->MaximumLength
= ValueCell
->DataSize
+ sizeof(WCHAR
);
709 TargetPath
->Buffer
= ExAllocatePool(NonPagedPool
,
710 TargetPath
->MaximumLength
);
713 TargetPath
->Length
= min(TargetPath
->MaximumLength
- sizeof(WCHAR
),
714 (ULONG
) ValueCell
->DataSize
);
716 if (ValueCell
->DataSize
> 0)
718 DataCell
= CmiGetCell (RegistryHive
, ValueCell
->DataOffset
, NULL
);
719 RtlCopyMemory(TargetPath
->Buffer
,
722 TargetPath
->Buffer
[TargetPath
->Length
/ sizeof(WCHAR
)] = 0;
726 RtlCopyMemory(TargetPath
->Buffer
,
727 &ValueCell
->DataOffset
,
729 TargetPath
->Buffer
[TargetPath
->Length
/ sizeof(WCHAR
)] = 0;
732 DPRINT("TargetPath '%wZ'\n", TargetPath
);
734 return(STATUS_SUCCESS
);