[NTOSKRNL]
[reactos.git] / reactos / ntoskrnl / wmi / guidobj.c
1 /*
2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/io/wmidrv.c
5 * PURPOSE: I/O Windows Management Instrumentation (WMI) Support
6 * PROGRAMMERS: Timo Kreuzer (timo.kreuzer@reactos.org)
7 */
8
9 /* INCLUDES *****************************************************************/
10
11 #include <ntoskrnl.h>
12 #include <wmistr.h>
13 #include <wmiguid.h>
14 #include "wmip.h"
15
16 #define NDEBUG
17 #include <debug.h>
18
19
20 /* GLOBALS ******************************************************************/
21
22 POBJECT_TYPE WmipGuidObjectType;
23 GENERIC_MAPPING WmipGenericMapping;
24
25
26 /* FUNCTIONS *****************************************************************/
27
28 NTSTATUS
29 NTAPI
30 WmipSecurityMethod(
31 _In_ PVOID Object,
32 _In_ SECURITY_OPERATION_CODE OperationType,
33 _In_ PSECURITY_INFORMATION SecurityInformation,
34 _In_ PSECURITY_DESCRIPTOR SecurityDescriptor,
35 _Inout_ PULONG CapturedLength,
36 _Inout_ PSECURITY_DESCRIPTOR *ObjectSecurityDescriptor,
37 _In_ POOL_TYPE PoolType,
38 _In_ PGENERIC_MAPPING GenericMapping)
39 {
40 UNIMPLEMENTED_DBGBREAK();
41 return STATUS_NOT_IMPLEMENTED;
42 }
43
44 VOID
45 NTAPI
46 WmipDeleteMethod(
47 _In_ PVOID Object)
48 {
49 UNIMPLEMENTED_DBGBREAK();
50 }
51
52 VOID
53 NTAPI
54 WmipCloseMethod(
55 _In_opt_ PEPROCESS Process,
56 _In_ PVOID Object,
57 _In_ ACCESS_MASK GrantedAccess,
58 _In_ ULONG ProcessHandleCount,
59 _In_ ULONG SystemHandleCount)
60 {
61 UNIMPLEMENTED_DBGBREAK();
62 }
63
64 NTSTATUS
65 NTAPI
66 WmipInitializeGuidObjectType(
67 VOID)
68 {
69 static UNICODE_STRING GuidObjectName = RTL_CONSTANT_STRING(L"WmiGuid");
70 OBJECT_TYPE_INITIALIZER ObjectTypeInitializer;
71 NTSTATUS Status;
72
73 /* Setup the object type initializer */
74 RtlZeroMemory(&ObjectTypeInitializer, sizeof(ObjectTypeInitializer));
75 ObjectTypeInitializer.Length = sizeof(ObjectTypeInitializer);
76 ObjectTypeInitializer.InvalidAttributes = OBJ_OPENLINK;
77 ObjectTypeInitializer.GenericMapping = WmipGenericMapping;
78 ObjectTypeInitializer.PoolType = NonPagedPool;
79 ObjectTypeInitializer.MaintainHandleCount = FALSE;
80 ObjectTypeInitializer.ValidAccessMask = STANDARD_RIGHTS_ALL | 0xFFF;
81 ObjectTypeInitializer.SecurityRequired = TRUE;
82 ObjectTypeInitializer.DefaultNonPagedPoolCharge = sizeof(WMIP_GUID_OBJECT);;
83 ObjectTypeInitializer.SecurityProcedure = WmipSecurityMethod;
84 ObjectTypeInitializer.DeleteProcedure = WmipDeleteMethod;
85 ObjectTypeInitializer.CloseProcedure = WmipCloseMethod;
86
87 /* Create the object type */
88 Status = ObCreateObjectType(&GuidObjectName,
89 &ObjectTypeInitializer,
90 0,
91 &WmipGuidObjectType);
92 if (!NT_SUCCESS(Status))
93 {
94 DPRINT1("ObCreateObjectType failed: 0x%lx\n", Status);
95 }
96
97 return Status;
98 }
99
100 static
101 NTSTATUS
102 WmipCreateGuidObject(
103 _In_ PUNICODE_STRING GuidString,
104 _Out_ PWMIP_GUID_OBJECT *OutGuidObject)
105 {
106 OBJECT_ATTRIBUTES ObjectAttributes;
107 GUID Guid;
108 PWMIP_GUID_OBJECT GuidObject;
109 NTSTATUS Status;
110
111 /* Convert the string into a GUID structure */
112 Status = RtlGUIDFromString(GuidString, &Guid);
113 if (!NT_SUCCESS(Status))
114 {
115 DPRINT1("WMI: Invalid uuid format for guid '%wZ'\n", GuidString);
116 return Status;
117 }
118
119 /* Initialize object attributes for an unnamed object */
120 InitializeObjectAttributes(&ObjectAttributes,
121 NULL,
122 0,
123 NULL,
124 NULL); // FIXME: security descriptor!
125
126 /* Create the GUID object */
127 Status = ObCreateObject(KernelMode,
128 WmipGuidObjectType,
129 &ObjectAttributes,
130 KernelMode,
131 NULL,
132 sizeof(WMIP_GUID_OBJECT),
133 0,
134 0,
135 (PVOID*)&GuidObject);
136 if (!NT_SUCCESS(Status))
137 {
138 DPRINT1("WMI: failed to create GUID object: 0x%lx\n", Status);
139 return Status;
140 }
141
142 GuidObject->Guid = Guid;
143
144 *OutGuidObject = GuidObject;
145
146 return STATUS_SUCCESS;
147 }
148
149 NTSTATUS
150 NTAPI
151 WmipOpenGuidObject(
152 POBJECT_ATTRIBUTES ObjectAttributes,
153 ACCESS_MASK DesiredAccess,
154 KPROCESSOR_MODE AccessMode,
155 PHANDLE OutGuidObjectHandle,
156 PVOID *OutGuidObject)
157 {
158 static UNICODE_STRING Prefix = RTL_CONSTANT_STRING(L"\\WmiGuid\\");
159 UNICODE_STRING GuidString;
160 ULONG HandleAttributes;
161 PWMIP_GUID_OBJECT GuidObject;
162 NTSTATUS Status;
163 PAGED_CODE();
164
165 /* Check if we have the expected prefix */
166 if (!RtlPrefixUnicodeString(ObjectAttributes->ObjectName, &Prefix, FALSE))
167 {
168 DPRINT1("WMI: Invalid prefix for guid object '%wZ'\n",
169 ObjectAttributes->ObjectName);
170 return STATUS_INVALID_PARAMETER;
171 }
172
173 /* Extract the GUID string */
174 GuidString = *ObjectAttributes->ObjectName;
175 GuidString.Buffer += Prefix.Length / sizeof(WCHAR);
176 GuidString.Length -= Prefix.Length;
177
178 /* Create the GUID object */
179 Status = WmipCreateGuidObject(&GuidString, &GuidObject);
180 if (!NT_SUCCESS(Status))
181 {
182 DPRINT1("Failed to create GUID object: 0x%lx\n", Status);
183 return Status;
184 }
185
186 /* Set handle attributes */
187 HandleAttributes = (AccessMode == KernelMode) ? OBJ_KERNEL_HANDLE : 0;
188
189 /* Get a handle for the object */
190 Status = ObOpenObjectByPointer(GuidObject,
191 HandleAttributes,
192 0,
193 DesiredAccess,
194 WmipGuidObjectType,
195 AccessMode,
196 OutGuidObjectHandle);
197 if (!NT_SUCCESS(Status))
198 {
199 DPRINT1("ObOpenObjectByPointer failed: 0x%lx\n", Status);
200 ObfDereferenceObject(GuidObject);
201 }
202
203 *OutGuidObject = GuidObject;
204
205 return Status;
206 }
207