2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: ntoskrnl/cm/regobj.c
5 * PURPOSE: Registry object manipulation routines.
9 #define NTOS_MODE_KERNEL
12 #include <internal/ob.h>
15 #include <internal/registry.h>
16 #include <ntos/minmax.h>
19 #include <internal/debug.h>
25 CmiGetLinkTarget(PREGISTRY_HIVE RegistryHive
,
27 PUNICODE_STRING TargetPath
);
29 /* FUNCTONS *****************************************************************/
32 CmiObjectParse(PVOID ParsedObject
,
34 PUNICODE_STRING FullPath
,
38 BLOCK_OFFSET BlockOffset
;
39 PKEY_OBJECT FoundObject
;
40 PKEY_OBJECT ParsedKey
;
46 UNICODE_STRING LinkPath
;
47 UNICODE_STRING TargetPath
;
48 UNICODE_STRING KeyName
;
50 ParsedKey
= ParsedObject
;
52 VERIFY_KEY_OBJECT(ParsedKey
);
58 DPRINT("*Path is NULL\n");
59 return STATUS_UNSUCCESSFUL
;
62 DPRINT("Path '%S'\n", *Path
);
64 /* Extract relevant path name */
66 if (*StartPtr
== L
'\\')
69 EndPtr
= wcschr(StartPtr
, L
'\\');
71 Length
= ((PCHAR
)EndPtr
- (PCHAR
)StartPtr
) / sizeof(WCHAR
);
73 Length
= wcslen(StartPtr
);
76 KeyName
.Length
= Length
* sizeof(WCHAR
);
77 KeyName
.MaximumLength
= KeyName
.Length
+ sizeof(WCHAR
);
78 KeyName
.Buffer
= ExAllocatePool(NonPagedPool
,
79 KeyName
.MaximumLength
);
80 RtlCopyMemory(KeyName
.Buffer
,
83 KeyName
.Buffer
[KeyName
.Length
/ sizeof(WCHAR
)] = 0;
86 FoundObject
= CmiScanKeyList(ParsedKey
,
89 if (FoundObject
== NULL
)
91 Status
= CmiScanForSubKey(ParsedKey
->RegistryHive
,
98 if (!NT_SUCCESS(Status
) || (SubKeyCell
== NULL
))
100 RtlFreeUnicodeString(&KeyName
);
101 return(STATUS_UNSUCCESSFUL
);
104 if ((SubKeyCell
->Flags
& REG_KEY_LINK_CELL
) &&
105 !((Attributes
& OBJ_OPENLINK
) && (EndPtr
== NULL
)))
107 RtlInitUnicodeString(&LinkPath
, NULL
);
108 Status
= CmiGetLinkTarget(ParsedKey
->RegistryHive
,
111 if (NT_SUCCESS(Status
))
113 DPRINT("LinkPath '%wZ'\n", &LinkPath
);
115 /* build new FullPath for reparsing */
116 TargetPath
.MaximumLength
= LinkPath
.MaximumLength
;
119 TargetPath
.MaximumLength
+= (wcslen(EndPtr
) * sizeof(WCHAR
));
121 TargetPath
.Length
= TargetPath
.MaximumLength
- sizeof(WCHAR
);
122 TargetPath
.Buffer
= ExAllocatePool(NonPagedPool
,
123 TargetPath
.MaximumLength
);
124 wcscpy(TargetPath
.Buffer
, LinkPath
.Buffer
);
127 wcscat(TargetPath
.Buffer
, EndPtr
);
130 RtlFreeUnicodeString(FullPath
);
131 RtlFreeUnicodeString(&LinkPath
);
132 FullPath
->Length
= TargetPath
.Length
;
133 FullPath
->MaximumLength
= TargetPath
.MaximumLength
;
134 FullPath
->Buffer
= TargetPath
.Buffer
;
136 DPRINT("FullPath '%wZ'\n", FullPath
);
138 /* reinitialize Path for reparsing */
139 *Path
= FullPath
->Buffer
;
143 RtlFreeUnicodeString(&KeyName
);
144 return(STATUS_REPARSE
);
148 /* Create new key object and put into linked list */
149 DPRINT("CmiObjectParse: %s\n", Path
);
150 Status
= ObRosCreateObject(NULL
,
151 STANDARD_RIGHTS_REQUIRED
,
154 (PVOID
*)&FoundObject
);
155 if (!NT_SUCCESS(Status
))
157 RtlFreeUnicodeString(&KeyName
);
161 FoundObject
->Flags
= 0;
162 FoundObject
->KeyCell
= SubKeyCell
;
163 FoundObject
->BlockOffset
= BlockOffset
;
164 FoundObject
->RegistryHive
= ParsedKey
->RegistryHive
;
165 RtlCreateUnicodeString(&FoundObject
->Name
,
167 CmiAddKeyToList(ParsedKey
, FoundObject
);
168 DPRINT("Created object 0x%x\n", FoundObject
);
172 if ((FoundObject
->KeyCell
->Flags
& REG_KEY_LINK_CELL
) &&
173 !((Attributes
& OBJ_OPENLINK
) && (EndPtr
== NULL
)))
175 DPRINT("Found link\n");
177 RtlInitUnicodeString(&LinkPath
, NULL
);
178 Status
= CmiGetLinkTarget(FoundObject
->RegistryHive
,
179 FoundObject
->KeyCell
,
181 if (NT_SUCCESS(Status
))
183 DPRINT("LinkPath '%wZ'\n", &LinkPath
);
185 /* build new FullPath for reparsing */
186 TargetPath
.MaximumLength
= LinkPath
.MaximumLength
;
189 TargetPath
.MaximumLength
+= (wcslen(EndPtr
) * sizeof(WCHAR
));
191 TargetPath
.Length
= TargetPath
.MaximumLength
- sizeof(WCHAR
);
192 TargetPath
.Buffer
= ExAllocatePool(NonPagedPool
,
193 TargetPath
.MaximumLength
);
194 wcscpy(TargetPath
.Buffer
, LinkPath
.Buffer
);
197 wcscat(TargetPath
.Buffer
, EndPtr
);
200 RtlFreeUnicodeString(FullPath
);
201 RtlFreeUnicodeString(&LinkPath
);
202 FullPath
->Length
= TargetPath
.Length
;
203 FullPath
->MaximumLength
= TargetPath
.MaximumLength
;
204 FullPath
->Buffer
= TargetPath
.Buffer
;
206 DPRINT("FullPath '%wZ'\n", FullPath
);
208 /* reinitialize Path for reparsing */
209 *Path
= FullPath
->Buffer
;
213 RtlFreeUnicodeString(&KeyName
);
214 return(STATUS_REPARSE
);
218 ObReferenceObjectByPointer(FoundObject
,
219 STANDARD_RIGHTS_REQUIRED
,
224 DPRINT("CmiObjectParse: %s\n", FoundObject
->Name
);
228 VERIFY_KEY_OBJECT(FoundObject
);
230 *NextObject
= FoundObject
;
232 RtlFreeUnicodeString(&KeyName
);
234 return(STATUS_SUCCESS
);
239 CmiObjectCreate(PVOID ObjectBody
,
242 POBJECT_ATTRIBUTES ObjectAttributes
)
244 PKEY_OBJECT KeyObject
= ObjectBody
;
247 KeyObject
->ParentKey
= Parent
;
250 Start
= RemainingPath
;
253 RtlCreateUnicodeString(&KeyObject
->Name
,
258 RtlInitUnicodeString(&KeyObject
->Name
,
262 return STATUS_SUCCESS
;
267 CmiObjectDelete(PVOID DeletedObject
)
269 PKEY_OBJECT KeyObject
;
271 DPRINT("Delete key object (%p)\n", DeletedObject
);
273 KeyObject
= (PKEY_OBJECT
) DeletedObject
;
275 if (!NT_SUCCESS(CmiRemoveKeyFromList(KeyObject
)))
277 DPRINT1("Key not found in parent list ???\n");
280 RtlFreeUnicodeString(&KeyObject
->Name
);
282 if (KeyObject
->Flags
& KO_MARKED_FOR_DELETE
)
284 DPRINT("delete really key\n");
286 CmiRemoveSubKey(KeyObject
->RegistryHive
,
287 KeyObject
->ParentKey
,
290 if (!IsNoFileHive(KeyObject
->RegistryHive
))
299 CmiObjectSecurity(PVOID ObjectBody
,
300 SECURITY_OPERATION_CODE OperationCode
,
301 SECURITY_INFORMATION SecurityInformation
,
302 PSECURITY_DESCRIPTOR SecurityDescriptor
,
305 DPRINT1 ("CmiObjectSecurity() called\n");
307 return STATUS_SUCCESS
;
312 CmiObjectQueryName (PVOID ObjectBody
,
313 POBJECT_NAME_INFORMATION ObjectNameInfo
,
317 POBJECT_NAME_INFORMATION LocalInfo
;
318 PKEY_OBJECT KeyObject
;
319 ULONG LocalReturnLength
;
322 DPRINT ("CmiObjectQueryName() called\n");
324 KeyObject
= (PKEY_OBJECT
)ObjectBody
;
326 LocalInfo
= ExAllocatePool (NonPagedPool
,
327 sizeof(OBJECT_NAME_INFORMATION
) +
328 MAX_PATH
* sizeof(WCHAR
));
329 if (LocalInfo
== NULL
)
330 return STATUS_INSUFFICIENT_RESOURCES
;
332 if (KeyObject
->ParentKey
!= KeyObject
)
334 Status
= ObQueryNameString (KeyObject
->ParentKey
,
336 MAX_PATH
* sizeof(WCHAR
),
341 /* KeyObject is the root key */
342 Status
= ObQueryNameString (BODY_TO_HEADER(KeyObject
)->Parent
,
344 MAX_PATH
* sizeof(WCHAR
),
348 if (!NT_SUCCESS (Status
))
350 ExFreePool (LocalInfo
);
353 DPRINT ("Parent path: %wZ\n", &LocalInfo
->Name
);
355 Status
= RtlAppendUnicodeStringToString (&ObjectNameInfo
->Name
,
357 ExFreePool (LocalInfo
);
358 if (!NT_SUCCESS (Status
))
361 Status
= RtlAppendUnicodeToString (&ObjectNameInfo
->Name
,
363 if (!NT_SUCCESS (Status
))
366 Status
= RtlAppendUnicodeStringToString (&ObjectNameInfo
->Name
,
368 if (NT_SUCCESS (Status
))
370 DPRINT ("Total path: %wZ\n", &ObjectNameInfo
->Name
);
378 CmiAddKeyToList(PKEY_OBJECT ParentKey
,
383 DPRINT("ParentKey %.08x\n", ParentKey
);
385 KeAcquireSpinLock(&CmiKeyListLock
, &OldIrql
);
387 if (ParentKey
->SizeOfSubKeys
<= ParentKey
->NumberOfSubKeys
)
389 PKEY_OBJECT
*tmpSubKeys
= ExAllocatePool(NonPagedPool
,
390 (ParentKey
->NumberOfSubKeys
+ 1) * sizeof(ULONG
));
392 if (ParentKey
->NumberOfSubKeys
> 0)
394 RtlCopyMemory (tmpSubKeys
,
396 ParentKey
->NumberOfSubKeys
* sizeof(ULONG
));
399 if (ParentKey
->SubKeys
)
400 ExFreePool(ParentKey
->SubKeys
);
402 ParentKey
->SubKeys
= tmpSubKeys
;
403 ParentKey
->SizeOfSubKeys
= ParentKey
->NumberOfSubKeys
+ 1;
406 /* FIXME: Please maintain the list in alphabetic order */
407 /* to allow a dichotomic search */
408 ParentKey
->SubKeys
[ParentKey
->NumberOfSubKeys
++] = NewKey
;
410 DPRINT("Reference parent key: 0x%x\n", ParentKey
);
412 ObReferenceObjectByPointer(ParentKey
,
413 STANDARD_RIGHTS_REQUIRED
,
416 NewKey
->ParentKey
= ParentKey
;
417 KeReleaseSpinLock(&CmiKeyListLock
, OldIrql
);
422 CmiRemoveKeyFromList(PKEY_OBJECT KeyToRemove
)
424 PKEY_OBJECT ParentKey
;
428 ParentKey
= KeyToRemove
->ParentKey
;
429 KeAcquireSpinLock(&CmiKeyListLock
, &OldIrql
);
430 /* FIXME: If list maintained in alphabetic order, use dichotomic search */
431 for (Index
= 0; Index
< ParentKey
->NumberOfSubKeys
; Index
++)
433 if (ParentKey
->SubKeys
[Index
] == KeyToRemove
)
435 if (Index
< ParentKey
->NumberOfSubKeys
-1)
436 RtlMoveMemory(&ParentKey
->SubKeys
[Index
],
437 &ParentKey
->SubKeys
[Index
+ 1],
438 (ParentKey
->NumberOfSubKeys
- Index
- 1) * sizeof(PKEY_OBJECT
));
439 ParentKey
->NumberOfSubKeys
--;
440 KeReleaseSpinLock(&CmiKeyListLock
, OldIrql
);
442 DPRINT("Dereference parent key: 0x%x\n", ParentKey
);
444 ObDereferenceObject(ParentKey
);
445 return STATUS_SUCCESS
;
448 KeReleaseSpinLock(&CmiKeyListLock
, OldIrql
);
450 return STATUS_UNSUCCESSFUL
;
455 CmiScanKeyList(PKEY_OBJECT Parent
,
456 PUNICODE_STRING KeyName
,
463 DPRINT("Scanning key list for: %wZ (Parent: %wZ)\n",
464 KeyName
, &Parent
->Name
);
466 KeAcquireSpinLock(&CmiKeyListLock
, &OldIrql
);
467 /* FIXME: if list maintained in alphabetic order, use dichotomic search */
468 for (Index
=0; Index
< Parent
->NumberOfSubKeys
; Index
++)
470 CurKey
= Parent
->SubKeys
[Index
];
471 if (Attributes
& OBJ_CASE_INSENSITIVE
)
473 if ((KeyName
->Length
== CurKey
->Name
.Length
)
474 && (_wcsicmp(KeyName
->Buffer
, CurKey
->Name
.Buffer
) == 0))
476 KeReleaseSpinLock(&CmiKeyListLock
, OldIrql
);
482 if ((KeyName
->Length
== CurKey
->Name
.Length
)
483 && (wcscmp(KeyName
->Buffer
, CurKey
->Name
.Buffer
) == 0))
485 KeReleaseSpinLock(&CmiKeyListLock
, OldIrql
);
490 KeReleaseSpinLock(&CmiKeyListLock
, OldIrql
);
497 CmiGetLinkTarget(PREGISTRY_HIVE RegistryHive
,
499 PUNICODE_STRING TargetPath
)
501 UNICODE_STRING LinkName
= UNICODE_STRING_INITIALIZER(L
"SymbolicLinkValue");
502 PVALUE_CELL ValueCell
;
506 DPRINT("CmiGetLinkTarget() called\n");
508 /* Get Value block of interest */
509 Status
= CmiScanKeyForValue(RegistryHive
,
514 if (!NT_SUCCESS(Status
))
516 DPRINT1("CmiScanKeyForValue() failed (Status %lx)\n", Status
);
520 if (ValueCell
->DataType
!= REG_LINK
)
522 DPRINT1("Type != REG_LINK\n!");
523 return(STATUS_UNSUCCESSFUL
);
526 if (TargetPath
->Buffer
== NULL
&& TargetPath
->MaximumLength
== 0)
528 TargetPath
->Length
= 0;
529 TargetPath
->MaximumLength
= ValueCell
->DataSize
+ sizeof(WCHAR
);
530 TargetPath
->Buffer
= ExAllocatePool(NonPagedPool
,
531 TargetPath
->MaximumLength
);
534 TargetPath
->Length
= min(TargetPath
->MaximumLength
- sizeof(WCHAR
),
535 (ULONG
) ValueCell
->DataSize
);
537 if (ValueCell
->DataSize
> 0)
539 DataCell
= CmiGetBlock(RegistryHive
, ValueCell
->DataOffset
, NULL
);
540 RtlCopyMemory(TargetPath
->Buffer
,
543 TargetPath
->Buffer
[TargetPath
->Length
/ sizeof(WCHAR
)] = 0;
547 RtlCopyMemory(TargetPath
->Buffer
,
548 &ValueCell
->DataOffset
,
550 TargetPath
->Buffer
[TargetPath
->Length
/ sizeof(WCHAR
)] = 0;
553 DPRINT("TargetPath '%wZ'\n", TargetPath
);
555 return(STATUS_SUCCESS
);