[RSHELL]
[reactos.git] / dll / win32 / wbemprox / wbemlocator.c
1 /*
2 * Copyright 2009 Hans Leidekker for CodeWeavers
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17 */
18
19 #include "wbemprox_private.h"
20
21 typedef struct
22 {
23 IWbemLocator IWbemLocator_iface;
24 LONG refs;
25 } wbem_locator;
26
27 static inline wbem_locator *impl_from_IWbemLocator( IWbemLocator *iface )
28 {
29 return CONTAINING_RECORD(iface, wbem_locator, IWbemLocator_iface);
30 }
31
32 static ULONG WINAPI wbem_locator_AddRef(
33 IWbemLocator *iface )
34 {
35 wbem_locator *wl = impl_from_IWbemLocator( iface );
36 return InterlockedIncrement( &wl->refs );
37 }
38
39 static ULONG WINAPI wbem_locator_Release(
40 IWbemLocator *iface )
41 {
42 wbem_locator *wl = impl_from_IWbemLocator( iface );
43 LONG refs = InterlockedDecrement( &wl->refs );
44 if (!refs)
45 {
46 TRACE("destroying %p\n", wl);
47 heap_free( wl );
48 }
49 return refs;
50 }
51
52 static HRESULT WINAPI wbem_locator_QueryInterface(
53 IWbemLocator *iface,
54 REFIID riid,
55 void **ppvObject )
56 {
57 wbem_locator *This = impl_from_IWbemLocator( iface );
58
59 TRACE("%p %s %p\n", This, debugstr_guid( riid ), ppvObject );
60
61 if ( IsEqualGUID( riid, &IID_IWbemLocator ) ||
62 IsEqualGUID( riid, &IID_IUnknown ) )
63 {
64 *ppvObject = iface;
65 }
66 else
67 {
68 FIXME("interface %s not implemented\n", debugstr_guid(riid));
69 return E_NOINTERFACE;
70 }
71 IWbemLocator_AddRef( iface );
72 return S_OK;
73 }
74
75 static BOOL is_local_machine( const WCHAR *server )
76 {
77 static const WCHAR dotW[] = {'.',0};
78 WCHAR buffer[MAX_COMPUTERNAME_LENGTH + 1];
79 DWORD len = sizeof(buffer) / sizeof(buffer[0]);
80
81 if (!server || !strcmpW( server, dotW )) return TRUE;
82 if (GetComputerNameW( buffer, &len ) && !strcmpiW( server, buffer )) return TRUE;
83 return FALSE;
84 }
85
86 static HRESULT parse_resource( const WCHAR *resource, WCHAR **server, WCHAR **namespace )
87 {
88 static const WCHAR rootW[] = {'R','O','O','T'};
89 static const WCHAR cimv2W[] = {'C','I','M','V','2'};
90 static const WCHAR defaultW[] = {'D','E','F','A','U','L','T'};
91 HRESULT hr = WBEM_E_INVALID_NAMESPACE;
92 const WCHAR *p, *q;
93 unsigned int len;
94
95 *server = NULL;
96 *namespace = NULL;
97 p = q = resource;
98 if (*p == '\\' || *p == '/')
99 {
100 p++;
101 if (*p == '\\' || *p == '/') p++;
102 if (!*p) return WBEM_E_INVALID_NAMESPACE;
103 if (*p == '\\' || *p == '/') return WBEM_E_INVALID_PARAMETER;
104 q = p + 1;
105 while (*q && *q != '\\' && *q != '/') q++;
106 if (!*q) return WBEM_E_INVALID_NAMESPACE;
107 len = q - p;
108 if (!(*server = heap_alloc( (len + 1) * sizeof(WCHAR) )))
109 {
110 hr = E_OUTOFMEMORY;
111 goto done;
112 }
113 memcpy( *server, p, len * sizeof(WCHAR) );
114 (*server)[len] = 0;
115 q++;
116 }
117 if (!*q) goto done;
118 p = q;
119 while (*q && *q != '\\' && *q != '/') q++;
120 len = q - p;
121 if (len >= sizeof(rootW) / sizeof(rootW[0]) && memicmpW( rootW, p, len )) goto done;
122 if (!*q)
123 {
124 hr = S_OK;
125 goto done;
126 }
127 q++;
128 len = strlenW( q );
129 if ((len != sizeof(cimv2W) / sizeof(cimv2W[0]) || memicmpW( q, cimv2W, len )) &&
130 (len != sizeof(defaultW) / sizeof(defaultW[0]) || memicmpW( q, defaultW, len )))
131 goto done;
132 if (!(*namespace = heap_alloc( (len + 1) * sizeof(WCHAR) ))) hr = E_OUTOFMEMORY;
133 else
134 {
135 memcpy( *namespace, p, len * sizeof(WCHAR) );
136 (*namespace)[len] = 0;
137 hr = S_OK;
138 }
139
140 done:
141 if (hr != S_OK)
142 {
143 heap_free( *server );
144 heap_free( *namespace );
145 }
146 return hr;
147 }
148
149 static HRESULT WINAPI wbem_locator_ConnectServer(
150 IWbemLocator *iface,
151 const BSTR NetworkResource,
152 const BSTR User,
153 const BSTR Password,
154 const BSTR Locale,
155 LONG SecurityFlags,
156 const BSTR Authority,
157 IWbemContext *pCtx,
158 IWbemServices **ppNamespace)
159 {
160 HRESULT hr;
161 WCHAR *server, *namespace;
162
163 TRACE("%p, %s, %s, %s, %s, 0x%08x, %s, %p, %p)\n", iface, debugstr_w(NetworkResource), debugstr_w(User),
164 debugstr_w(Password), debugstr_w(Locale), SecurityFlags, debugstr_w(Authority), pCtx, ppNamespace);
165
166 hr = parse_resource( NetworkResource, &server, &namespace );
167 if (hr != S_OK) return hr;
168
169 if (!is_local_machine( server ))
170 {
171 FIXME("remote computer not supported\n");
172 heap_free( server );
173 heap_free( namespace );
174 return WBEM_E_TRANSPORT_FAILURE;
175 }
176 if (User || Password || Authority)
177 FIXME("authentication not supported\n");
178 if (Locale)
179 FIXME("specific locale not supported\n");
180 if (SecurityFlags)
181 FIXME("unsupported flags\n");
182
183 hr = WbemServices_create( NULL, namespace, (void **)ppNamespace );
184 heap_free( namespace );
185 heap_free( server );
186 if (SUCCEEDED( hr ))
187 return WBEM_NO_ERROR;
188
189 return WBEM_E_FAILED;
190 }
191
192 static const IWbemLocatorVtbl wbem_locator_vtbl =
193 {
194 wbem_locator_QueryInterface,
195 wbem_locator_AddRef,
196 wbem_locator_Release,
197 wbem_locator_ConnectServer
198 };
199
200 HRESULT WbemLocator_create( IUnknown *pUnkOuter, LPVOID *ppObj )
201 {
202 wbem_locator *wl;
203
204 TRACE("(%p,%p)\n", pUnkOuter, ppObj);
205
206 wl = heap_alloc( sizeof(*wl) );
207 if (!wl) return E_OUTOFMEMORY;
208
209 wl->IWbemLocator_iface.lpVtbl = &wbem_locator_vtbl;
210 wl->refs = 1;
211
212 *ppObj = &wl->IWbemLocator_iface;
213
214 TRACE("returning iface %p\n", *ppObj);
215 return S_OK;
216 }