[NTOSKRNL]
[reactos.git] / reactos / ntoskrnl / config / ntapi.c
index 3a95860..0e76f7c 100644 (file)
@@ -4,6 +4,7 @@
  * FILE:            ntoskrnl/config/cmapi.c
  * PURPOSE:         Configuration Manager - Internal Registry APIs
  * PROGRAMMERS:     Alex Ionescu (alex.ionescu@reactos.org)
+ *                  Eric Kohl
  */
 
 /* INCLUDES ******************************************************************/
@@ -23,11 +24,11 @@ NtCreateKey(OUT PHANDLE KeyHandle,
             IN ACCESS_MASK DesiredAccess,
             IN POBJECT_ATTRIBUTES ObjectAttributes,
             IN ULONG TitleIndex,
-            IN PUNICODE_STRING Class,
+            IN PUNICODE_STRING Class OPTIONAL,
             IN ULONG CreateOptions,
-            OUT PULONG Disposition)
+            OUT PULONG Disposition OPTIONAL)
 {
-    NTSTATUS Status = STATUS_SUCCESS;
+    NTSTATUS Status;
     KPROCESSOR_MODE PreviousMode = ExGetPreviousMode();
     CM_PARSE_CONTEXT ParseContext = {0};
     HANDLE Handle;
@@ -58,14 +59,15 @@ NtCreateKey(OUT PHANDLE KeyHandle,
             ProbeForRead(ObjectAttributes,
                          sizeof(OBJECT_ATTRIBUTES),
                          sizeof(ULONG));
+
+            if (Disposition) ProbeForWriteUlong(Disposition);
         }
         _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
         {
-            /* Get the error code */
-            Status = _SEH2_GetExceptionCode();
+            /* Return the exception code */
+            _SEH2_YIELD(return _SEH2_GetExceptionCode());
         }
         _SEH2_END;
-        if(!NT_SUCCESS(Status)) return Status;
     }
     else
     {
@@ -111,7 +113,7 @@ NtOpenKey(OUT PHANDLE KeyHandle,
 {
     CM_PARSE_CONTEXT ParseContext = {0};
     HANDLE Handle;
-    NTSTATUS Status = STATUS_SUCCESS;
+    NTSTATUS Status;
     KPROCESSOR_MODE PreviousMode = ExGetPreviousMode();
     PAGED_CODE();
     DPRINT("NtOpenKey(OB 0x%wZ)\n", ObjectAttributes->ObjectName);
@@ -133,11 +135,10 @@ NtOpenKey(OUT PHANDLE KeyHandle,
         }
         _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
         {
-            /* Get the status */
-            Status = _SEH2_GetExceptionCode();
+            /* Return the exception code */
+            _SEH2_YIELD(return _SEH2_GetExceptionCode());
         }
         _SEH2_END;
-        if(!NT_SUCCESS(Status)) return Status;
     }
 
     /* Just let the object manager handle this */
@@ -228,6 +229,7 @@ NtEnumerateKey(IN HANDLE KeyHandle,
                IN ULONG Length,
                OUT PULONG ResultLength)
 {
+    KPROCESSOR_MODE PreviousMode = ExGetPreviousMode();
     NTSTATUS Status;
     PCM_KEY_BODY KeyObject;
     REG_ENUMERATE_KEY_INFORMATION EnumerateKeyInfo;
@@ -254,6 +256,24 @@ NtEnumerateKey(IN HANDLE KeyHandle,
                                        NULL);
     if (!NT_SUCCESS(Status)) return Status;
 
+    if (PreviousMode != KernelMode)
+    {
+        _SEH2_TRY
+        {
+            ProbeForWriteUlong(ResultLength);
+            ProbeForWrite(KeyInformation,
+                          Length,
+                          sizeof(ULONG));
+        }
+        _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
+        {
+            /* Dereference and return status */
+            ObDereferenceObject(KeyObject);
+            _SEH2_YIELD(return _SEH2_GetExceptionCode());
+        }
+        _SEH2_END;
+    }
+
     /* Setup the callback */
     PostOperationInfo.Object = (PVOID)KeyObject;
     EnumerateKeyInfo.Object = (PVOID)KeyObject;
@@ -293,6 +313,7 @@ NtEnumerateValueKey(IN HANDLE KeyHandle,
                     IN ULONG Length,
                     OUT PULONG ResultLength)
 {
+    KPROCESSOR_MODE PreviousMode = ExGetPreviousMode();
     NTSTATUS Status;
     PCM_KEY_BODY KeyObject;
     REG_ENUMERATE_VALUE_KEY_INFORMATION EnumerateValueKeyInfo;
@@ -319,6 +340,24 @@ NtEnumerateValueKey(IN HANDLE KeyHandle,
                                        NULL);
     if (!NT_SUCCESS(Status)) return Status;
 
+    if (PreviousMode != KernelMode)
+    {
+        _SEH2_TRY
+        {
+            ProbeForWriteUlong(ResultLength);
+            ProbeForWrite(KeyValueInformation,
+                          Length,
+                          sizeof(ULONG));
+        }
+        _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
+        {
+            /* Dereference and return status */
+            ObDereferenceObject(KeyObject);
+            _SEH2_YIELD(return _SEH2_GetExceptionCode());
+        }
+        _SEH2_END;
+    }
+
     /* Setup the callback */
     PostOperationInfo.Object = (PVOID)KeyObject;
     EnumerateValueKeyInfo.Object = (PVOID)KeyObject;
@@ -358,6 +397,7 @@ NtQueryKey(IN HANDLE KeyHandle,
            IN ULONG Length,
            OUT PULONG ResultLength)
 {
+    KPROCESSOR_MODE PreviousMode = ExGetPreviousMode();
     NTSTATUS Status;
     PCM_KEY_BODY KeyObject;
     REG_QUERY_KEY_INFORMATION QueryKeyInfo;
@@ -414,6 +454,24 @@ NtQueryKey(IN HANDLE KeyHandle,
     /* Quit on failure */
     if (!NT_SUCCESS(Status)) return Status;
 
+    if (PreviousMode != KernelMode)
+    {
+        _SEH2_TRY
+        {
+            ProbeForWriteUlong(ResultLength);
+            ProbeForWrite(KeyInformation,
+                          Length,
+                          sizeof(ULONG));
+        }
+        _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
+        {
+            /* Dereference and return status */
+            ObDereferenceObject(KeyObject);
+            _SEH2_YIELD(return _SEH2_GetExceptionCode());
+        }
+        _SEH2_END;
+    }
+
     /* Setup the callback */
     PostOperationInfo.Object = (PVOID)KeyObject;
     QueryKeyInfo.Object = (PVOID)KeyObject;
@@ -452,6 +510,7 @@ NtQueryValueKey(IN HANDLE KeyHandle,
                 IN ULONG Length,
                 OUT PULONG ResultLength)
 {
+    KPROCESSOR_MODE PreviousMode = ExGetPreviousMode();
     NTSTATUS Status;
     PCM_KEY_BODY KeyObject;
     REG_QUERY_VALUE_KEY_INFORMATION QueryValueKeyInfo;
@@ -470,6 +529,24 @@ NtQueryValueKey(IN HANDLE KeyHandle,
                                        NULL);
     if (!NT_SUCCESS(Status)) return Status;
 
+    if (PreviousMode != KernelMode)
+    {
+        _SEH2_TRY
+        {
+            ProbeForWriteUlong(ResultLength);
+            ProbeForWrite(KeyValueInformation,
+                          Length,
+                          sizeof(ULONG));
+        }
+        _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
+        {
+            /* Dereference and return status */
+            ObDereferenceObject(KeyObject);
+            _SEH2_YIELD(return _SEH2_GetExceptionCode());
+        }
+        _SEH2_END;
+    }
+
     /* Make sure the name is aligned properly */
     if ((ValueNameCopy.Length & (sizeof(WCHAR) - 1)))
     {
@@ -814,6 +891,9 @@ NtInitializeRegistry(IN USHORT Flag)
     /* Always do this as kernel mode */
     if (KeGetPreviousMode() == UserMode) return ZwInitializeRegistry(Flag);
 
+    /* Enough of the system has booted by now */
+    Ki386PerfEnd();
+            
     /* Validate flag */
     if (Flag > CM_BOOT_FLAG_MAX) return STATUS_INVALID_PARAMETER;
 
@@ -936,8 +1016,91 @@ NTAPI
 NtQueryOpenSubKeys(IN POBJECT_ATTRIBUTES TargetKey,
                    OUT PULONG HandleCount)
 {
-    UNIMPLEMENTED;
-    return STATUS_NOT_IMPLEMENTED;
+    KPROCESSOR_MODE PreviousMode;
+    PCM_KEY_BODY KeyBody = NULL;
+    HANDLE KeyHandle;
+    NTSTATUS Status;
+
+    DPRINT("NtQueryOpenSubKeys()\n");
+
+    PAGED_CODE();
+
+    /* Get the processor mode */
+    PreviousMode = KeGetPreviousMode();
+
+    if (PreviousMode != KernelMode)
+    {
+        /* Prepare to probe parameters */
+        _SEH2_TRY
+        {
+            /* Probe target key */
+            ProbeForRead(TargetKey,
+                         sizeof(OBJECT_ATTRIBUTES),
+                         sizeof(ULONG));
+
+            /* Probe handle count */
+            ProbeForWriteUlong(HandleCount);
+        }
+        _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
+        {
+            /* Return the exception code */
+            _SEH2_YIELD(return _SEH2_GetExceptionCode());
+        }
+        _SEH2_END;
+    }
+
+    /* Open a handle to the key */
+    Status = ObOpenObjectByName(TargetKey,
+                                CmpKeyObjectType,
+                                PreviousMode,
+                                NULL,
+                                KEY_READ,
+                                NULL,
+                                &KeyHandle);
+    if (NT_SUCCESS(Status))
+    {
+        /* Reference the key object */
+        Status = ObReferenceObjectByHandle(KeyHandle,
+                                           KEY_READ,
+                                           CmpKeyObjectType,
+                                           PreviousMode,
+                                           (PVOID *)&KeyBody,
+                                           NULL);
+
+        /* Close the handle */
+        NtClose(KeyHandle);
+    }
+
+    /* Fail, if the key object could not be referenced */
+    if (!NT_SUCCESS(Status))
+        return Status;
+
+    /* Lock the registry exclusively */
+    CmpLockRegistryExclusive();
+
+    /* Fail, if we did not open a hive root key */
+    if (KeyBody->KeyControlBlock->KeyCell !=
+        KeyBody->KeyControlBlock->KeyHive->BaseBlock->RootCell)
+    {
+        DPRINT("Error: Key is not a hive root key!\n");
+        CmpUnlockRegistry();
+        ObDereferenceObject(KeyBody);
+        return STATUS_INVALID_PARAMETER;
+    }
+
+    /* Call the internal API */
+    *HandleCount = CmCountOpenSubKeys(KeyBody->KeyControlBlock,
+                                      FALSE);
+
+    /* Unlock the registry */
+    CmpUnlockRegistry();
+
+    /* Dereference the key object */
+    ObDereferenceObject(KeyBody);
+
+    DPRINT("Done.\n");
+
+    return Status;
 }
 
 NTSTATUS
@@ -1024,8 +1187,7 @@ NTSTATUS
 NTAPI
 NtUnloadKey(IN POBJECT_ATTRIBUTES KeyObjectAttributes)
 {
-    UNIMPLEMENTED;
-    return STATUS_NOT_IMPLEMENTED;
+    return NtUnloadKey2(KeyObjectAttributes, 0);
 }
 
 NTSTATUS
@@ -1033,8 +1195,170 @@ NTAPI
 NtUnloadKey2(IN POBJECT_ATTRIBUTES TargetKey,
              IN ULONG Flags)
 {
+#if 0
+    NTSTATUS Status;
+    OBJECT_ATTRIBUTES ObjectAttributes;
+    UNICODE_STRING ObjectName;
+    CM_PARSE_CONTEXT ParseContext = {0};
+    KPROCESSOR_MODE PreviousMode = ExGetPreviousMode();
+    PCM_KEY_BODY KeyBody = NULL;
+    ULONG ParentConv = 0, ChildConv = 0;
+    HANDLE Handle;
+    PAGED_CODE();
+
+    /* Validate privilege */
+    if (!SeSinglePrivilegeCheck(SeRestorePrivilege, PreviousMode))
+    {
+        /* Fail */
+        DPRINT1("Restore Privilege missing!\n");
+        return STATUS_PRIVILEGE_NOT_HELD;
+    }
+
+    /* Check for user-mode caller */
+    if (PreviousMode != KernelMode)
+    {
+        /* Prepare to probe parameters */
+        _SEH2_TRY
+        {
+            /* Probe object attributes */
+            ProbeForRead(TargetKey,
+                         sizeof(OBJECT_ATTRIBUTES),
+                         sizeof(ULONG));
+
+            ObjectAttributes = *TargetKey;
+
+            /* Probe the string */
+            ProbeForReadUnicodeString(&TargetKey->ObjectName);
+
+            ObjectName = *TargetKey->ObjectName;
+
+            ProbeForRead(ObjectName.Buffer,
+                         ObjectName.Length,
+                         sizeof(WCHAR));
+
+            ObjectAttributes.ObjectName = &ObjectName;
+        }
+        _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
+        {
+            /* Return the exception code */
+            _SEH2_YIELD(return _SEH2_GetExceptionCode());
+        }
+        _SEH2_END;
+    }
+    else
+    {
+        /* Save the target attributes directly */
+        ObjectAttributes = *TargetKey;
+    }
+
+    /* Setup the parse context */
+    ParseContext.CreateOperation = TRUE;
+    ParseContext.CreateOptions = REG_OPTION_BACKUP_RESTORE;
+
+    /* Do the create */
+    Status = ObOpenObjectByName(&ObjectAttributes,
+                                CmpKeyObjectType,
+                                KernelMode,
+                                NULL,
+                                KEY_WRITE,
+                                &ParseContext,
+                                &Handle);
+
+    /* Return if failure encountered */
+    if (!NT_SUCCESS(Status)) return Status;
+
+    /* Reference it */
+    Status = ObReferenceObjectByHandle(Handle,
+                                       KEY_WRITE,
+                                       CmpKeyObjectType,
+                                       KernelMode,
+                                       (PVOID *)&KeyBody,
+                                       NULL);
+
+    /* Close the handle */
+    ZwClose(Handle);
+
+    /* Return if failure encountered */
+    if (!NT_SUCCESS(Status)) return Status;
+
+    /* Acquire the lock depending on flags */
+    if (Flags == REG_FORCE_UNLOAD)
+    {
+        /* Lock registry exclusively */
+        CmpLockRegistryExclusive();
+    }
+    else
+    {
+        /* Lock registry */
+        CmpLockRegistry();
+
+        /* Acquire the hive loading lock */
+        ExAcquirePushLockExclusive(&CmpLoadHiveLock);
+
+        /* Lock parent and child */
+        if (KeyBody->KeyControlBlock->ParentKcb)
+            ParentConv = KeyBody->KeyControlBlock->ParentKcb->ConvKey;
+        else
+            ParentConv = KeyBody->KeyControlBlock->ConvKey;
+
+        ChildConv = KeyBody->KeyControlBlock->ConvKey;
+
+        CmpAcquireTwoKcbLocksExclusiveByKey(ChildConv, ParentConv);
+    }
+
+    /* Check if it's being deleted already */
+    if (KeyBody->KeyControlBlock->Delete)
+    {
+        /* Return appropriate status */
+        Status = STATUS_KEY_DELETED;
+        goto Quickie;
+    }
+
+    /* Check if it's a readonly key */
+    if (KeyBody->KeyControlBlock->ExtFlags & CM_KCB_READ_ONLY_KEY)
+    {
+        /* Return appropriate status */
+        Status = STATUS_ACCESS_DENIED;
+        goto Quickie;
+    }
+
+    /* Call the internal API */
+    Status = CmUnloadKey(KeyBody->KeyControlBlock,
+                         Flags);
+
+    /* Check if we failed, but really need to succeed */
+    if ((Status == STATUS_CANNOT_DELETE) && (Flags == REG_FORCE_UNLOAD))
+    {
+        /* TODO: We should perform another attempt here */
+        ASSERT(FALSE);
+    }
+
+    /* If CmUnloadKey failed we need to unlock registry ourselves */
+    if (!NT_SUCCESS(Status))
+    {
+        if (Flags != REG_FORCE_UNLOAD)
+        {
+            /* Release the hive loading lock */
+            ExReleasePushLockExclusive(&CmpLoadHiveLock);
+
+            /* Release two KCBs lock */
+            CmpReleaseTwoKcbLockByKey(ChildConv, ParentConv);
+        }
+
+        /* Unlock the registry */
+        CmpUnlockRegistry();
+    }
+
+Quickie:
+    /* Dereference the key */
+    ObDereferenceObject(KeyBody);
+
+    /* Return status */
+    return Status;
+#else
     UNIMPLEMENTED;
     return STATUS_NOT_IMPLEMENTED;
+#endif
 }
 
 NTSTATUS