msvcrt: Added basic _popen tests.
[wine] / dlls / 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 #define COBJMACROS
20
21 #include "config.h"
22 #include <stdarg.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "objbase.h"
27 #include "wbemcli.h"
28
29 #include "wine/debug.h"
30 #include "wine/unicode.h"
31 #include "wbemprox_private.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(wbemprox);
34
35 typedef struct
36 {
37     IWbemLocator IWbemLocator_iface;
38     LONG refs;
39 } wbem_locator;
40
41 static inline wbem_locator *impl_from_IWbemLocator( IWbemLocator *iface )
42 {
43     return CONTAINING_RECORD(iface, wbem_locator, IWbemLocator_iface);
44 }
45
46 static ULONG WINAPI wbem_locator_AddRef(
47     IWbemLocator *iface )
48 {
49     wbem_locator *wl = impl_from_IWbemLocator( iface );
50     return InterlockedIncrement( &wl->refs );
51 }
52
53 static ULONG WINAPI wbem_locator_Release(
54     IWbemLocator *iface )
55 {
56     wbem_locator *wl = impl_from_IWbemLocator( iface );
57     LONG refs = InterlockedDecrement( &wl->refs );
58     if (!refs)
59     {
60         TRACE("destroying %p\n", wl);
61         heap_free( wl );
62     }
63     return refs;
64 }
65
66 static HRESULT WINAPI wbem_locator_QueryInterface(
67     IWbemLocator *iface,
68     REFIID riid,
69     void **ppvObject )
70 {
71     wbem_locator *This = impl_from_IWbemLocator( iface );
72
73     TRACE("%p %s %p\n", This, debugstr_guid( riid ), ppvObject );
74
75     if ( IsEqualGUID( riid, &IID_IWbemLocator ) ||
76          IsEqualGUID( riid, &IID_IUnknown ) )
77     {
78         *ppvObject = iface;
79     }
80     else
81     {
82         FIXME("interface %s not implemented\n", debugstr_guid(riid));
83         return E_NOINTERFACE;
84     }
85     IWbemLocator_AddRef( iface );
86     return S_OK;
87 }
88
89 static BOOL is_local_machine( const WCHAR *server )
90 {
91     static const WCHAR dotW[] = {'.',0};
92     WCHAR buffer[MAX_COMPUTERNAME_LENGTH + 1];
93     DWORD len = sizeof(buffer) / sizeof(buffer[0]);
94
95     if (!server || !strcmpW( server, dotW )) return TRUE;
96     if (GetComputerNameW( buffer, &len ) && !strcmpiW( server, buffer )) return TRUE;
97     return FALSE;
98 }
99
100 static HRESULT parse_resource( const WCHAR *resource, WCHAR **server, WCHAR **namespace )
101 {
102     static const WCHAR rootW[] = {'R','O','O','T'};
103     static const WCHAR cimv2W[] = {'C','I','M','V','2'};
104     HRESULT hr = WBEM_E_INVALID_NAMESPACE;
105     const WCHAR *p, *q;
106     unsigned int len;
107
108     *server = NULL;
109     *namespace = NULL;
110     p = q = resource;
111     if (*p == '\\' || *p == '/')
112     {
113         p++;
114         if (*p == '\\' || *p == '/') p++;
115         if (!*p) return WBEM_E_INVALID_NAMESPACE;
116         if (*p == '\\' || *p == '/') return WBEM_E_INVALID_PARAMETER;
117         q = p + 1;
118         while (*q && *q != '\\' && *q != '/') q++;
119         if (!*q) return WBEM_E_INVALID_NAMESPACE;
120         len = q - p;
121         if (!(*server = heap_alloc( (len + 1) * sizeof(WCHAR) )))
122         {
123             hr = E_OUTOFMEMORY;
124             goto done;
125         }
126         memcpy( *server, p, len * sizeof(WCHAR) );
127         (*server)[len] = 0;
128         q++;
129     }
130     if (!*q) goto done;
131     p = q;
132     while (*q && *q != '\\' && *q != '/') q++;
133     len = q - p;
134     if (len >= sizeof(rootW) / sizeof(rootW[0]) && memicmpW( rootW, p, len )) goto done;
135     if (!*q)
136     {
137         hr = S_OK;
138         goto done;
139     }
140     q++;
141     if ((len = strlenW( q )) != sizeof(cimv2W) / sizeof(cimv2W[0]) || memicmpW( q, cimv2W, len ))
142         goto done;
143     if (!(*namespace = heap_alloc( (len + 1) * sizeof(WCHAR) ))) hr = E_OUTOFMEMORY;
144     else
145     {
146         memcpy( *namespace, p, len * sizeof(WCHAR) );
147         (*namespace)[len] = 0;
148         hr = S_OK;
149     }
150
151 done:
152     if (hr != S_OK)
153     {
154         heap_free( *server );
155         heap_free( *namespace );
156     }
157     return hr;
158 }
159
160 static HRESULT WINAPI wbem_locator_ConnectServer(
161     IWbemLocator *iface,
162     const BSTR NetworkResource,
163     const BSTR User,
164     const BSTR Password,
165     const BSTR Locale,
166     LONG SecurityFlags,
167     const BSTR Authority,
168     IWbemContext *pCtx,
169     IWbemServices **ppNamespace)
170 {
171     HRESULT hr;
172     WCHAR *server, *namespace;
173
174     TRACE("%p, %s, %s, %s, %s, 0x%08x, %s, %p, %p)\n", iface, debugstr_w(NetworkResource), debugstr_w(User),
175           debugstr_w(Password), debugstr_w(Locale), SecurityFlags, debugstr_w(Authority), pCtx, ppNamespace);
176
177     hr = parse_resource( NetworkResource, &server, &namespace );
178     if (hr != S_OK) return hr;
179
180     if (!is_local_machine( server ))
181     {
182         FIXME("remote computer not supported\n");
183         heap_free( server );
184         heap_free( namespace );
185         return WBEM_E_TRANSPORT_FAILURE;
186     }
187     if (User || Password || Authority)
188         FIXME("authentication not supported\n");
189     if (Locale)
190         FIXME("specific locale not supported\n");
191     if (SecurityFlags)
192         FIXME("unsupported flags\n");
193
194     hr = WbemServices_create( NULL, namespace, (void **)ppNamespace );
195     heap_free( namespace );
196     if (SUCCEEDED( hr ))
197         return WBEM_NO_ERROR;
198
199     return WBEM_E_FAILED;
200 }
201
202 static const IWbemLocatorVtbl wbem_locator_vtbl =
203 {
204     wbem_locator_QueryInterface,
205     wbem_locator_AddRef,
206     wbem_locator_Release,
207     wbem_locator_ConnectServer
208 };
209
210 HRESULT WbemLocator_create( IUnknown *pUnkOuter, LPVOID *ppObj )
211 {
212     wbem_locator *wl;
213
214     TRACE("(%p,%p)\n", pUnkOuter, ppObj);
215
216     wl = heap_alloc( sizeof(*wl) );
217     if (!wl) return E_OUTOFMEMORY;
218
219     wl->IWbemLocator_iface.lpVtbl = &wbem_locator_vtbl;
220     wl->refs = 1;
221
222     *ppObj = &wl->IWbemLocator_iface;
223
224     TRACE("returning iface %p\n", *ppObj);
225     return S_OK;
226 }