include: Update the PEB and TEB structures.
[wine.git] / dlls / wbemprox / wbemlocator.c
blob6e5bddea1567e43512cebb465cda30d77d619dbe
1 /*
2 * Copyright 2009 Hans Leidekker for CodeWeavers
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.
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.
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
19 #define COBJMACROS
21 #include <stdarg.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "objbase.h"
26 #include "wbemcli.h"
28 #include "wine/debug.h"
29 #include "wbemprox_private.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(wbemprox);
33 typedef struct
35 IWbemLocator IWbemLocator_iface;
36 LONG refs;
37 } wbem_locator;
39 static inline wbem_locator *impl_from_IWbemLocator( IWbemLocator *iface )
41 return CONTAINING_RECORD(iface, wbem_locator, IWbemLocator_iface);
44 static ULONG WINAPI wbem_locator_AddRef(
45 IWbemLocator *iface )
47 wbem_locator *wl = impl_from_IWbemLocator( iface );
48 return InterlockedIncrement( &wl->refs );
51 static ULONG WINAPI wbem_locator_Release(
52 IWbemLocator *iface )
54 wbem_locator *wl = impl_from_IWbemLocator( iface );
55 LONG refs = InterlockedDecrement( &wl->refs );
56 if (!refs)
58 TRACE("destroying %p\n", wl);
59 free( wl );
61 return refs;
64 static HRESULT WINAPI wbem_locator_QueryInterface(
65 IWbemLocator *iface,
66 REFIID riid,
67 void **ppvObject )
69 wbem_locator *This = impl_from_IWbemLocator( iface );
71 TRACE("%p %s %p\n", This, debugstr_guid( riid ), ppvObject );
73 if ( IsEqualGUID( riid, &IID_IWbemLocator ) ||
74 IsEqualGUID( riid, &IID_IUnknown ) )
76 *ppvObject = iface;
78 else
80 FIXME("interface %s not implemented\n", debugstr_guid(riid));
81 return E_NOINTERFACE;
83 IWbemLocator_AddRef( iface );
84 return S_OK;
87 static BOOL is_local_machine( const WCHAR *server )
89 WCHAR buffer[MAX_COMPUTERNAME_LENGTH + 1];
90 DWORD len = ARRAY_SIZE( buffer );
92 if (!server || !wcscmp( server, L"." ) || !wcsicmp( server, L"localhost" )) return TRUE;
93 if (GetComputerNameW( buffer, &len ) && !wcsicmp( server, buffer )) return TRUE;
94 return FALSE;
97 static HRESULT parse_resource( const WCHAR *resource, WCHAR **server, WCHAR **namespace )
99 HRESULT hr = WBEM_E_INVALID_NAMESPACE;
100 const WCHAR *p, *q;
101 unsigned int len;
103 *server = NULL;
104 *namespace = NULL;
105 p = q = resource;
106 if (*p == '\\' || *p == '/')
108 p++;
109 if (*p == '\\' || *p == '/') p++;
110 if (!*p) return WBEM_E_INVALID_NAMESPACE;
111 if (*p == '\\' || *p == '/') return WBEM_E_INVALID_PARAMETER;
112 q = p + 1;
113 while (*q && *q != '\\' && *q != '/') q++;
114 if (!*q) return WBEM_E_INVALID_NAMESPACE;
115 len = q - p;
116 if (!(*server = malloc( (len + 1) * sizeof(WCHAR) )))
118 hr = E_OUTOFMEMORY;
119 goto done;
121 memcpy( *server, p, len * sizeof(WCHAR) );
122 (*server)[len] = 0;
123 q++;
125 if (!*q) goto done;
126 p = q;
127 while (*q && *q != '\\' && *q != '/') q++;
128 len = q - p;
129 if (len >= ARRAY_SIZE( L"root" ) - 1 && wcsnicmp( L"root", p, len )) goto done;
130 if (!*q)
132 hr = S_OK;
133 goto done;
135 q++;
136 len = lstrlenW( q );
137 if (!(*namespace = malloc( (len + 1) * sizeof(WCHAR) ))) hr = E_OUTOFMEMORY;
138 else
140 memcpy( *namespace, q, len * sizeof(WCHAR) );
141 (*namespace)[len] = 0;
142 hr = S_OK;
145 done:
146 if (hr != S_OK)
148 free( *server );
149 free( *namespace );
151 return hr;
154 static HRESULT WINAPI wbem_locator_ConnectServer(
155 IWbemLocator *iface,
156 const BSTR NetworkResource,
157 const BSTR User,
158 const BSTR Password,
159 const BSTR Locale,
160 LONG SecurityFlags,
161 const BSTR Authority,
162 IWbemContext *context,
163 IWbemServices **ppNamespace)
165 HRESULT hr;
166 WCHAR *server, *namespace;
168 TRACE( "%p, %s, %s, %s, %s, %#lx, %s, %p, %p)\n", iface, debugstr_w(NetworkResource), debugstr_w(User),
169 debugstr_w(Password), debugstr_w(Locale), SecurityFlags, debugstr_w(Authority), context, ppNamespace );
171 hr = parse_resource( NetworkResource, &server, &namespace );
172 if (hr != S_OK) return hr;
174 if (!is_local_machine( server ))
176 FIXME("remote computer not supported\n");
177 free( server );
178 free( namespace );
179 return WBEM_E_TRANSPORT_FAILURE;
181 if (User || Password || Authority)
182 FIXME("authentication not supported\n");
183 if (Locale)
184 FIXME("specific locale not supported\n");
185 if (SecurityFlags)
186 FIXME("unsupported flags\n");
188 hr = WbemServices_create( namespace, context, (void **)ppNamespace );
189 free( namespace );
190 free( server );
191 if (SUCCEEDED( hr ))
192 return WBEM_NO_ERROR;
194 return hr;
197 static const IWbemLocatorVtbl wbem_locator_vtbl =
199 wbem_locator_QueryInterface,
200 wbem_locator_AddRef,
201 wbem_locator_Release,
202 wbem_locator_ConnectServer
205 HRESULT WbemLocator_create( LPVOID *ppObj, REFIID riid )
207 wbem_locator *wl;
209 TRACE("(%p)\n", ppObj);
211 if ( !IsEqualGUID( riid, &IID_IWbemLocator ) &&
212 !IsEqualGUID( riid, &IID_IUnknown ) )
213 return E_NOINTERFACE;
215 if (!(wl = malloc( sizeof(*wl) ))) return E_OUTOFMEMORY;
217 wl->IWbemLocator_iface.lpVtbl = &wbem_locator_vtbl;
218 wl->refs = 1;
220 *ppObj = &wl->IWbemLocator_iface;
222 TRACE("returning iface %p\n", *ppObj);
223 return S_OK;