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.
12 #include <ddk/ntddk.h>
14 #include <internal/ob.h>
17 #include <internal/pool.h>
18 #include <internal/registry.h>
19 #include <ntos/minmax.h>
22 #include <internal/debug.h>
29 CmiGetLinkTarget(PREGISTRY_HIVE RegistryHive
,
31 PUNICODE_STRING TargetPath
);
33 /* FUNCTONS *****************************************************************/
36 CmiObjectParse(PVOID ParsedObject
,
38 PUNICODE_STRING FullPath
,
42 BLOCK_OFFSET BlockOffset
;
43 PKEY_OBJECT FoundObject
;
44 PKEY_OBJECT ParsedKey
;
49 UNICODE_STRING LinkPath
;
50 UNICODE_STRING TargetPath
;
52 ParsedKey
= ParsedObject
;
54 VERIFY_KEY_OBJECT(ParsedKey
);
60 DPRINT("*Path is NULL\n");
61 return STATUS_UNSUCCESSFUL
;
64 DPRINT("Path '%S'\n", *Path
);
66 if ((*Path
[0]) == '\\')
68 end
= wcschr((*Path
) + 1, '\\');
71 wcstombs(cPath
, (*Path
) + 1, wcslen((*Path
) + 1));
72 cPath
[wcslen((*Path
) + 1)] = 0;
76 end
= wcschr((*Path
), '\\');
79 wcstombs(cPath
, (*Path
), wcslen((*Path
)));
80 cPath
[wcslen((*Path
))] = 0;
83 FoundObject
= CmiScanKeyList(ParsedKey
, cPath
, Attributes
);
84 if (FoundObject
== NULL
)
86 Status
= CmiScanForSubKey(ParsedKey
->RegistryHive
,
93 if (!NT_SUCCESS(Status
) || (SubKeyCell
== NULL
))
99 return(STATUS_UNSUCCESSFUL
);
102 if ((SubKeyCell
->Type
== REG_LINK_KEY_CELL_TYPE
) &&
103 !((Attributes
& OBJ_OPENLINK
) && (end
== NULL
)))
105 RtlInitUnicodeString(&LinkPath
, NULL
);
106 Status
= CmiGetLinkTarget(ParsedKey
->RegistryHive
,
109 if (NT_SUCCESS(Status
))
111 DPRINT("LinkPath '%wZ'\n", &LinkPath
);
113 /* build new FullPath for reparsing */
114 TargetPath
.MaximumLength
= LinkPath
.MaximumLength
;
118 TargetPath
.MaximumLength
+= (wcslen(end
) * sizeof(WCHAR
));
120 TargetPath
.Length
= TargetPath
.MaximumLength
- sizeof(WCHAR
);
121 TargetPath
.Buffer
= ExAllocatePool(NonPagedPool
,
122 TargetPath
.MaximumLength
);
123 wcscpy(TargetPath
.Buffer
, LinkPath
.Buffer
);
126 wcscat(TargetPath
.Buffer
, end
);
129 RtlFreeUnicodeString(FullPath
);
130 RtlFreeUnicodeString(&LinkPath
);
131 FullPath
->Length
= TargetPath
.Length
;
132 FullPath
->MaximumLength
= TargetPath
.MaximumLength
;
133 FullPath
->Buffer
= TargetPath
.Buffer
;
135 DPRINT("FullPath '%wZ'\n", FullPath
);
137 /* reinitialize Path for reparsing */
138 *Path
= FullPath
->Buffer
;
141 return(STATUS_REPARSE
);
145 /* Create new key object and put into linked list */
146 DPRINT("CmiObjectParse: %s\n", cPath
);
147 Status
= ObCreateObject(NULL
,
148 STANDARD_RIGHTS_REQUIRED
,
151 (PVOID
*)&FoundObject
);
152 if (!NT_SUCCESS(Status
))
157 FoundObject
->Flags
= 0;
158 FoundObject
->Name
= SubKeyCell
->Name
;
159 FoundObject
->NameSize
= SubKeyCell
->NameSize
;
160 FoundObject
->KeyCell
= SubKeyCell
;
161 FoundObject
->BlockOffset
= BlockOffset
;
162 FoundObject
->RegistryHive
= ParsedKey
->RegistryHive
;
163 CmiAddKeyToList(ParsedKey
, FoundObject
);
164 DPRINT("Created object 0x%x\n", FoundObject
);
168 if ((FoundObject
->KeyCell
->Type
== REG_LINK_KEY_CELL_TYPE
) &&
169 !((Attributes
& OBJ_OPENLINK
) && (end
== NULL
)))
171 RtlInitUnicodeString(&LinkPath
, NULL
);
172 Status
= CmiGetLinkTarget(FoundObject
->RegistryHive
,
173 FoundObject
->KeyCell
,
175 if (NT_SUCCESS(Status
))
177 DPRINT("LinkPath '%wZ'\n", &LinkPath
);
179 /* build new FullPath for reparsing */
180 TargetPath
.MaximumLength
= LinkPath
.MaximumLength
;
184 TargetPath
.MaximumLength
+= (wcslen(end
) * sizeof(WCHAR
));
186 TargetPath
.Length
= TargetPath
.MaximumLength
- sizeof(WCHAR
);
187 TargetPath
.Buffer
= ExAllocatePool(NonPagedPool
,
188 TargetPath
.MaximumLength
);
189 wcscpy(TargetPath
.Buffer
, LinkPath
.Buffer
);
192 wcscat(TargetPath
.Buffer
, end
);
195 RtlFreeUnicodeString(FullPath
);
196 RtlFreeUnicodeString(&LinkPath
);
197 FullPath
->Length
= TargetPath
.Length
;
198 FullPath
->MaximumLength
= TargetPath
.MaximumLength
;
199 FullPath
->Buffer
= TargetPath
.Buffer
;
201 DPRINT("FullPath '%wZ'\n", FullPath
);
203 /* reinitialize Path for reparsing */
204 *Path
= FullPath
->Buffer
;
207 return(STATUS_REPARSE
);
211 ObReferenceObjectByPointer(FoundObject
,
212 STANDARD_RIGHTS_REQUIRED
,
217 DPRINT("CmiObjectParse: %s\n", FoundObject
->Name
);
220 char buffer
[_BUFFER_LEN
];
221 memset(buffer
, 0, _BUFFER_LEN
);
222 strncpy(buffer
, FoundObject
->Name
, min(FoundObject
->NameSize
, _BUFFER_LEN
- 1));
223 DPRINT("CmiObjectParse: %s\n", buffer
);
237 VERIFY_KEY_OBJECT(FoundObject
);
239 *NextObject
= FoundObject
;
241 return(STATUS_SUCCESS
);
246 CmiObjectCreate(PVOID ObjectBody
,
249 struct _OBJECT_ATTRIBUTES
* ObjectAttributes
)
251 PKEY_OBJECT pKey
= ObjectBody
;
253 pKey
->ParentKey
= Parent
;
256 if(RemainingPath
[0]== L
'\\')
258 pKey
->Name
= (PCHAR
)(&RemainingPath
[1]);
259 pKey
->NameSize
= wcslen(RemainingPath
) - 1;
263 pKey
->Name
= (PCHAR
)RemainingPath
;
264 pKey
->NameSize
= wcslen(RemainingPath
);
272 return STATUS_SUCCESS
;
277 CmiObjectDelete(PVOID DeletedObject
)
279 PKEY_OBJECT KeyObject
;
281 DPRINT("Delete object key\n");
283 KeyObject
= (PKEY_OBJECT
) DeletedObject
;
285 if (!NT_SUCCESS(CmiRemoveKeyFromList(KeyObject
)))
287 DPRINT1("Key not found in parent list ???\n");
290 if (KeyObject
->Flags
& KO_MARKED_FOR_DELETE
)
292 DPRINT("delete really key\n");
293 CmiDestroyBlock(KeyObject
->RegistryHive
,
295 KeyObject
->BlockOffset
);
299 CmiReleaseBlock(KeyObject
->RegistryHive
, KeyObject
->KeyCell
);
305 CmiAddKeyToList(PKEY_OBJECT ParentKey
,
310 DPRINT("ParentKey %.08x\n", ParentKey
);
312 KeAcquireSpinLock(&CmiKeyListLock
, &OldIrql
);
314 if (ParentKey
->SizeOfSubKeys
<= ParentKey
->NumberOfSubKeys
)
316 PKEY_OBJECT
*tmpSubKeys
= ExAllocatePool(NonPagedPool
,
317 (ParentKey
->NumberOfSubKeys
+ 1) * sizeof(DWORD
));
319 if (ParentKey
->NumberOfSubKeys
> 0)
323 ParentKey
->NumberOfSubKeys
* sizeof(DWORD
));
326 if (ParentKey
->SubKeys
)
327 ExFreePool(ParentKey
->SubKeys
);
329 ParentKey
->SubKeys
= tmpSubKeys
;
330 ParentKey
->SizeOfSubKeys
= ParentKey
->NumberOfSubKeys
+ 1;
333 /* FIXME: Please maintain the list in alphabetic order */
334 /* to allow a dichotomic search */
335 ParentKey
->SubKeys
[ParentKey
->NumberOfSubKeys
++] = NewKey
;
337 DPRINT("Reference parent key: 0x%x\n", ParentKey
);
339 ObReferenceObjectByPointer(ParentKey
,
340 STANDARD_RIGHTS_REQUIRED
,
343 NewKey
->ParentKey
= ParentKey
;
344 KeReleaseSpinLock(&CmiKeyListLock
, OldIrql
);
349 CmiRemoveKeyFromList(PKEY_OBJECT KeyToRemove
)
351 PKEY_OBJECT ParentKey
;
355 ParentKey
= KeyToRemove
->ParentKey
;
356 KeAcquireSpinLock(&CmiKeyListLock
, &OldIrql
);
357 /* FIXME: If list maintained in alphabetic order, use dichotomic search */
358 for (Index
= 0; Index
< ParentKey
->NumberOfSubKeys
; Index
++)
360 if (ParentKey
->SubKeys
[Index
] == KeyToRemove
)
362 if (Index
< ParentKey
->NumberOfSubKeys
-1)
363 RtlMoveMemory(&ParentKey
->SubKeys
[Index
],
364 &ParentKey
->SubKeys
[Index
+ 1],
365 (ParentKey
->NumberOfSubKeys
- Index
- 1) * sizeof(PKEY_OBJECT
));
366 ParentKey
->NumberOfSubKeys
--;
367 KeReleaseSpinLock(&CmiKeyListLock
, OldIrql
);
369 DPRINT("Dereference parent key: 0x%x\n", ParentKey
);
371 ObDereferenceObject(ParentKey
);
372 return STATUS_SUCCESS
;
375 KeReleaseSpinLock(&CmiKeyListLock
, OldIrql
);
377 return STATUS_UNSUCCESSFUL
;
382 CmiScanKeyList(PKEY_OBJECT Parent
,
392 DPRINT("Scanning key list for: %s (Parent: %s)\n",
393 KeyName
, Parent
->Name
);
396 char buffer
[_BUFFER_LEN
];
397 memset(buffer
, 0, _BUFFER_LEN
);
398 strncpy(buffer
, Parent
->Name
, min(Parent
->NameSize
, _BUFFER_LEN
- 1));
399 DPRINT("Scanning key list for: %s (Parent: %s)\n", KeyName
, buffer
);
403 NameSize
= strlen(KeyName
);
404 KeAcquireSpinLock(&CmiKeyListLock
, &OldIrql
);
405 /* FIXME: if list maintained in alphabetic order, use dichotomic search */
406 for (Index
=0; Index
< Parent
->NumberOfSubKeys
; Index
++)
408 CurKey
= Parent
->SubKeys
[Index
];
409 if (Attributes
& OBJ_CASE_INSENSITIVE
)
411 if ((NameSize
== CurKey
->NameSize
)
412 && (_strnicmp(KeyName
, CurKey
->Name
, NameSize
) == 0))
414 KeReleaseSpinLock(&CmiKeyListLock
, OldIrql
);
420 if ((NameSize
== CurKey
->NameSize
)
421 && (strncmp(KeyName
,CurKey
->Name
,NameSize
) == 0))
423 KeReleaseSpinLock(&CmiKeyListLock
, OldIrql
);
428 KeReleaseSpinLock(&CmiKeyListLock
, OldIrql
);
435 CmiGetLinkTarget(PREGISTRY_HIVE RegistryHive
,
437 PUNICODE_STRING TargetPath
)
439 PVALUE_CELL ValueCell
;
443 /* Get Value block of interest */
444 Status
= CmiScanKeyForValue(RegistryHive
,
449 if (!NT_SUCCESS(Status
))
454 if (ValueCell
->DataType
!= REG_LINK
)
456 DPRINT1("Type != REG_LINK\n!");
457 return(STATUS_UNSUCCESSFUL
);
460 if (TargetPath
->Buffer
== NULL
&& TargetPath
->MaximumLength
== 0)
462 TargetPath
->Length
= 0;
463 TargetPath
->MaximumLength
= ValueCell
->DataSize
+ sizeof(WCHAR
);
464 TargetPath
->Buffer
= ExAllocatePool(NonPagedPool
,
465 TargetPath
->MaximumLength
);
468 TargetPath
->Length
= min(TargetPath
->MaximumLength
- sizeof(WCHAR
),
469 ValueCell
->DataSize
);
471 if (ValueCell
->DataSize
> 0)
473 DataCell
= CmiGetBlock(RegistryHive
, ValueCell
->DataOffset
, NULL
);
474 RtlCopyMemory(TargetPath
->Buffer
,
477 TargetPath
->Buffer
[TargetPath
->Length
/ sizeof(WCHAR
)] = 0;
478 CmiReleaseBlock(RegistryHive
, DataCell
);
482 RtlCopyMemory(TargetPath
->Buffer
,
483 &ValueCell
->DataOffset
,
485 TargetPath
->Buffer
[TargetPath
->Length
/ sizeof(WCHAR
)] = 0;
488 return(STATUS_SUCCESS
);