netstat: Initial implementation.
[wine] / dlls / oledb32 / datainit.c
1 /*
2  * Copyright (C) 2012 Alistair Leslie-Hughes
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 #include <stdarg.h>
19
20 #define COBJMACROS
21 #define NONAMELESSUNION
22 #define NONAMELESSSTRUCT
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winnls.h"
27 #include "ole2.h"
28 #include "msdasc.h"
29 #include "oledberr.h"
30 #include "initguid.h"
31 #include "oledb_private.h"
32
33 #include "wine/debug.h"
34 #include "wine/unicode.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(oledb);
37
38 DEFINE_GUID(DBPROPSET_DBINIT, 0xc8b522bc, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
39
40 typedef struct datainit
41 {
42     IDataInitialize IDataInitialize_iface;
43
44     LONG ref;
45 } datainit;
46
47 static inline datainit *impl_from_IDataInitialize(IDataInitialize *iface)
48 {
49     return CONTAINING_RECORD(iface, datainit, IDataInitialize_iface);
50 }
51
52 typedef struct
53 {
54     IDBInitialize IDBInitialize_iface;
55     IDBProperties IDBProperties_iface;
56
57     LONG ref;
58 } dbinit;
59
60 static inline dbinit *impl_from_IDBInitialize(IDBInitialize *iface)
61 {
62     return CONTAINING_RECORD(iface, dbinit, IDBInitialize_iface);
63 }
64
65 static inline dbinit *impl_from_IDBProperties(IDBProperties *iface)
66 {
67     return CONTAINING_RECORD(iface, dbinit, IDBProperties_iface);
68 }
69
70 static HRESULT WINAPI dbprops_QueryInterface(IDBProperties *iface, REFIID riid, void **ppvObject)
71 {
72     dbinit *This = impl_from_IDBProperties(iface);
73
74     return IDBInitialize_QueryInterface(&This->IDBInitialize_iface, riid, ppvObject);
75 }
76
77 static ULONG WINAPI dbprops_AddRef(IDBProperties *iface)
78 {
79     dbinit *This = impl_from_IDBProperties(iface);
80
81     return IDBInitialize_AddRef(&This->IDBInitialize_iface);
82 }
83
84 static ULONG WINAPI dbprops_Release(IDBProperties *iface)
85 {
86     dbinit *This = impl_from_IDBProperties(iface);
87
88     return IDBInitialize_Release(&This->IDBInitialize_iface);
89 }
90
91 static HRESULT WINAPI dbprops_GetProperties(IDBProperties *iface, ULONG cPropertyIDSets,
92             const DBPROPIDSET rgPropertyIDSets[], ULONG *pcPropertySets, DBPROPSET **prgPropertySets)
93 {
94     dbinit *This = impl_from_IDBProperties(iface);
95
96     FIXME("(%p)->(%d %p %p %p)\n", This, cPropertyIDSets, rgPropertyIDSets, pcPropertySets, prgPropertySets);
97
98     return E_NOTIMPL;
99 }
100
101 static HRESULT WINAPI dbprops_GetPropertyInfo(IDBProperties *iface, ULONG cPropertyIDSets,
102             const DBPROPIDSET rgPropertyIDSets[], ULONG *pcPropertyInfoSets,
103             DBPROPINFOSET **prgPropertyInfoSets, OLECHAR **ppDescBuffer)
104 {
105     dbinit *This = impl_from_IDBProperties(iface);
106
107     FIXME("(%p)->(%d %p %p %p %p)\n", This, cPropertyIDSets, rgPropertyIDSets, pcPropertyInfoSets,
108                 prgPropertyInfoSets, ppDescBuffer);
109
110     return E_NOTIMPL;
111 }
112
113 static HRESULT WINAPI dbprops_SetProperties(IDBProperties *iface, ULONG cPropertySets,
114             DBPROPSET rgPropertySets[])
115 {
116     dbinit *This = impl_from_IDBProperties(iface);
117
118     FIXME("(%p)->(%d %p)\n", This, cPropertySets, rgPropertySets);
119
120     return E_NOTIMPL;
121 }
122
123 static const struct IDBPropertiesVtbl dbprops_vtbl =
124 {
125     dbprops_QueryInterface,
126     dbprops_AddRef,
127     dbprops_Release,
128     dbprops_GetProperties,
129     dbprops_GetPropertyInfo,
130     dbprops_SetProperties
131 };
132
133 static HRESULT WINAPI dbinit_QueryInterface(IDBInitialize *iface, REFIID riid, void **obj)
134 {
135     dbinit *This = impl_from_IDBInitialize(iface);
136     TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), obj);
137
138     *obj = NULL;
139
140     if(IsEqualIID(riid, &IID_IUnknown) ||
141        IsEqualIID(riid, &IID_IDBInitialize))
142     {
143         *obj = iface;
144     }
145     else if(IsEqualIID(riid, &IID_IDBProperties))
146     {
147         *obj = &This->IDBProperties_iface;
148     }
149     else
150     {
151         FIXME("interface %s not implemented\n", debugstr_guid(riid));
152         return E_NOINTERFACE;
153     }
154
155     IDBInitialize_AddRef(iface);
156     return S_OK;
157 }
158
159 static ULONG WINAPI dbinit_AddRef(IDBInitialize *iface)
160 {
161     dbinit *This = impl_from_IDBInitialize(iface);
162     TRACE("(%p)\n", This);
163
164     return InterlockedIncrement(&This->ref);
165 }
166
167 static ULONG WINAPI dbinit_Release(IDBInitialize *iface)
168 {
169     dbinit *This = impl_from_IDBInitialize(iface);
170     LONG ref;
171
172     TRACE("(%p)\n", This);
173
174     ref = InterlockedDecrement(&This->ref);
175     if(ref == 0)
176     {
177         HeapFree(GetProcessHeap(), 0, This);
178     }
179
180     return ref;
181 }
182
183 static HRESULT WINAPI dbinit_Initialize(IDBInitialize *iface)
184 {
185     dbinit *This = impl_from_IDBInitialize(iface);
186
187     FIXME("(%p) stub\n", This);
188
189     return S_OK;
190 }
191
192 static HRESULT WINAPI dbinit_Uninitialize(IDBInitialize *iface)
193 {
194     dbinit *This = impl_from_IDBInitialize(iface);
195
196     FIXME("(%p) stub\n", This);
197
198     return S_OK;
199 }
200
201 static const IDBInitializeVtbl dbinit_vtbl =
202 {
203     dbinit_QueryInterface,
204     dbinit_AddRef,
205     dbinit_Release,
206     dbinit_Initialize,
207     dbinit_Uninitialize
208 };
209
210 static HRESULT create_db_init(void **obj)
211 {
212     dbinit *This;
213
214     TRACE("()\n");
215
216     *obj = NULL;
217
218     This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
219     if(!This) return E_OUTOFMEMORY;
220
221     This->IDBInitialize_iface.lpVtbl = &dbinit_vtbl;
222     This->IDBProperties_iface.lpVtbl = &dbprops_vtbl;
223     This->ref = 1;
224
225     *obj = &This->IDBInitialize_iface;
226
227     return S_OK;
228 }
229
230 static HRESULT WINAPI datainit_QueryInterface(IDataInitialize *iface, REFIID riid, void **obj)
231 {
232     datainit *This = impl_from_IDataInitialize(iface);
233     TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), obj);
234
235     *obj = NULL;
236
237     if(IsEqualIID(riid, &IID_IUnknown) ||
238        IsEqualIID(riid, &IID_IDataInitialize))
239     {
240         *obj = &This->IDataInitialize_iface;
241     }
242     else
243     {
244         FIXME("interface %s not implemented\n", debugstr_guid(riid));
245         return E_NOINTERFACE;
246     }
247
248     IDataInitialize_AddRef(iface);
249     return S_OK;
250 }
251
252 static ULONG WINAPI datainit_AddRef(IDataInitialize *iface)
253 {
254     datainit *This = impl_from_IDataInitialize(iface);
255     TRACE("(%p)\n", This);
256
257     return InterlockedIncrement(&This->ref);
258 }
259
260 static ULONG WINAPI datainit_Release(IDataInitialize *iface)
261 {
262     datainit *This = impl_from_IDataInitialize(iface);
263     LONG ref;
264
265     TRACE("(%p)\n", This);
266
267     ref = InterlockedDecrement(&This->ref);
268     if(ref == 0)
269     {
270         HeapFree(GetProcessHeap(), 0, This);
271     }
272
273     return ref;
274 }
275
276 static void free_dbpropset(ULONG count, DBPROPSET *propset)
277 {
278     int i;
279
280     for (i = 0; i < count; i++)
281     {
282         int p;
283
284         for (p = 0; p < propset[i].cProperties; p++)
285             VariantClear(&propset[i].rgProperties[p].vValue);
286
287         CoTaskMemFree(propset[i].rgProperties);
288     }
289     CoTaskMemFree(propset);
290 }
291
292 /*** IDataInitialize methods ***/
293 static HRESULT WINAPI datainit_GetDataSource(IDataInitialize *iface, IUnknown *outer, DWORD clsctx,
294                                 LPWSTR initstring, REFIID riid, IUnknown **datasource)
295 {
296     static const WCHAR providerW[] = {'P','r','o','v','i','d','e','r','=',0};
297     static const WCHAR msdasqlW[] = {'M','S','D','A','S','Q','L',0};
298     datainit *This = impl_from_IDataInitialize(iface);
299     WCHAR *prov = NULL;
300     CLSID provclsid;
301     HRESULT hr;
302
303     FIXME("(%p)->(%p 0x%x %s %s %p): semi-stub\n", This, outer, clsctx, debugstr_w(initstring), debugstr_guid(riid), datasource);
304
305     /* first get provider name */
306     provclsid = IID_NULL;
307     if (initstring && (prov = strstrW(initstring, providerW)))
308     {
309         WCHAR *start, *progid;
310         int len;
311
312         prov += sizeof(providerW)/sizeof(WCHAR)-1;
313         start = prov;
314         while (*prov && *prov != ';')
315             ++prov;
316         TRACE("got provider %s\n", debugstr_wn(start, prov-start));
317
318         len = prov - start;
319         progid = CoTaskMemAlloc((len+1)*sizeof(WCHAR));
320         if (!progid) return E_OUTOFMEMORY;
321
322         memcpy(progid, start, len*sizeof(WCHAR));
323         progid[len] = 0;
324
325         hr = CLSIDFromProgID(progid, &provclsid);
326         CoTaskMemFree(progid);
327         if (FAILED(hr))
328         {
329             ERR("provider %s not registered\n", debugstr_wn(start, prov-start));
330             return hr;
331         }
332     }
333     else
334     {
335         hr = CLSIDFromProgID(msdasqlW, &provclsid);
336         if (FAILED(hr))
337             ERR("ODBC provider for OLE DB not registered\n");
338     }
339
340     /* check for provider mismatch if it was specified in init string */
341     if (*datasource && prov)
342     {
343         IDBProperties *dbprops;
344         DBPROPIDSET propidset;
345         DBPROPSET *propset;
346         enum DBPROPENUM prop;
347         CLSID initprov;
348         ULONG count;
349
350         hr = IUnknown_QueryInterface(*datasource, &IID_IDBProperties, (void**)&dbprops);
351         if (FAILED(hr))
352         {
353             WARN("provider doesn't support IDBProperties\n");
354             return hr;
355         }
356
357         prop = DBPROP_INIT_DATASOURCE;
358         propidset.rgPropertyIDs = &prop;
359         propidset.cPropertyIDs = 1;
360         propidset.guidPropertySet = DBPROPSET_DBINIT;
361         count = 0;
362         propset = NULL;
363         hr = IDBProperties_GetProperties(dbprops, 1, &propidset, &count, &propset);
364         IDBProperties_Release(dbprops);
365         if (FAILED(hr))
366         {
367             WARN("GetProperties failed for datasource, 0x%08x\n", hr);
368             return hr;
369         }
370
371         TRACE("initial data source provider %s\n", debugstr_w(V_BSTR(&propset->rgProperties[0].vValue)));
372         initprov = IID_NULL;
373         CLSIDFromProgID(V_BSTR(&propset->rgProperties[0].vValue), &initprov);
374         free_dbpropset(count, propset);
375
376         if (!IsEqualIID(&provclsid, &initprov)) return DB_E_MISMATCHEDPROVIDER;
377     }
378
379     if (!*datasource)
380     {
381         if (!IsEqualIID(&provclsid, &IID_NULL))
382             hr = CoCreateInstance(&provclsid, outer, clsctx, riid, (void**)datasource);
383
384         if (FAILED(hr) && IsEqualIID(riid, &IID_IDBInitialize))
385             hr = create_db_init((void**)datasource);
386     }
387
388     /* FIXME: set properties from init string */
389
390     return hr;
391 }
392
393 /* returns character length of string representation */
394 static int get_propvalue_length(DBPROP *prop)
395 {
396     VARIANT str;
397     HRESULT hr;
398
399     if (V_VT(&prop->vValue) == VT_BSTR) return SysStringLen(V_BSTR(&prop->vValue));
400
401     VariantInit(&str);
402     hr = VariantChangeType(&str, &prop->vValue, 0, VT_BSTR);
403     if (hr == S_OK)
404     {
405         int len = SysStringLen(V_BSTR(&str));
406         VariantClear(&str);
407         return len;
408     }
409
410     return 0;
411 }
412
413 static void write_propvalue_str(WCHAR *str, DBPROP *prop)
414 {
415     VARIANT *v = &prop->vValue;
416     VARIANT vstr;
417     HRESULT hr;
418
419     if (V_VT(v) == VT_BSTR)
420     {
421         strcatW(str, V_BSTR(v));
422         return;
423     }
424
425     VariantInit(&vstr);
426     hr = VariantChangeType(&vstr, v, VARIANT_ALPHABOOL, VT_BSTR);
427     if (hr == S_OK)
428     {
429         strcatW(str, V_BSTR(&vstr));
430         VariantClear(&vstr);
431     }
432 }
433
434 static WCHAR *get_propinfo_descr(DBPROP *prop, DBPROPINFOSET *propinfoset)
435 {
436     int i;
437
438     for (i = 0; i < propinfoset->cPropertyInfos; i++)
439         if (propinfoset->rgPropertyInfos[i].dwPropertyID == prop->dwPropertyID)
440             return propinfoset->rgPropertyInfos[i].pwszDescription;
441
442     return NULL;
443 }
444
445 static void free_dbpropinfoset(ULONG count, DBPROPINFOSET *propinfoset)
446 {
447     int i;
448
449     for (i = 0; i < count; i++)
450     {
451         int p;
452
453         for (p = 0; p < propinfoset[i].cPropertyInfos; p++)
454             VariantClear(&propinfoset[i].rgPropertyInfos[p].vValues);
455
456         CoTaskMemFree(propinfoset[i].rgPropertyInfos);
457     }
458     CoTaskMemFree(propinfoset);
459 }
460
461 static HRESULT WINAPI datainit_GetInitializationString(IDataInitialize *iface, IUnknown *datasource,
462                                 boolean include_pass, LPWSTR *init_string)
463 {
464     static const WCHAR provW[] = {'P','r','o','v','i','d','e','r','=',0};
465     static const WCHAR colW[] = {';',0};
466     datainit *This = impl_from_IDataInitialize(iface);
467     DBPROPINFOSET *propinfoset;
468     IDBProperties *props;
469     DBPROPIDSET propidset;
470     ULONG count, infocount;
471     WCHAR *progid, *desc;
472     DBPROPSET *propset;
473     IPersist *persist;
474     HRESULT hr;
475     CLSID clsid;
476     int i, len;
477
478     TRACE("(%p)->(%p %d %p)\n", This, datasource, include_pass, init_string);
479
480     /* IPersist support is mandatory for data sources */
481     hr = IUnknown_QueryInterface(datasource, &IID_IPersist, (void**)&persist);
482     if (FAILED(hr)) return hr;
483
484     memset(&clsid, 0, sizeof(clsid));
485     hr = IPersist_GetClassID(persist, &clsid);
486     IPersist_Release(persist);
487     if (FAILED(hr)) return hr;
488
489     progid = NULL;
490     ProgIDFromCLSID(&clsid, &progid);
491     TRACE("clsid=%s, progid=%s\n", debugstr_guid(&clsid), debugstr_w(progid));
492
493     /* now get initialization properties */
494     hr = IUnknown_QueryInterface(datasource, &IID_IDBProperties, (void**)&props);
495     if (FAILED(hr))
496     {
497         WARN("IDBProperties not supported\n");
498         CoTaskMemFree(progid);
499         return hr;
500     }
501
502     propidset.rgPropertyIDs = NULL;
503     propidset.cPropertyIDs = 0;
504     propidset.guidPropertySet = DBPROPSET_DBINIT;
505     propset = NULL;
506     count = 0;
507     hr = IDBProperties_GetProperties(props, 1, &propidset, &count, &propset);
508     if (FAILED(hr))
509     {
510         WARN("failed to get data source properties, 0x%08x\n", hr);
511         CoTaskMemFree(progid);
512         return hr;
513     }
514
515     infocount = 0;
516     IDBProperties_GetPropertyInfo(props, 1, &propidset, &infocount, &propinfoset, &desc);
517     IDBProperties_Release(props);
518
519     /* check if we need to skip password */
520     len = strlenW(progid) + strlenW(provW) + 1; /* including ';' */
521     for (i = 0; i < count; i++)
522     {
523         WCHAR *descr = get_propinfo_descr(&propset->rgProperties[i], propinfoset);
524         if (descr)
525         {
526             /* include '=' and ';' */
527             len += strlenW(descr) + 2;
528             len += get_propvalue_length(&propset->rgProperties[i]);
529         }
530
531         if ((propset->rgProperties[i].dwPropertyID == DBPROP_AUTH_PERSIST_SENSITIVE_AUTHINFO) &&
532             (V_BOOL(&propset->rgProperties[i].vValue) == VARIANT_FALSE))
533            include_pass = FALSE;
534     }
535
536     len *= sizeof(WCHAR);
537     *init_string = CoTaskMemAlloc(len);
538     *init_string[0] = 0;
539
540     /* provider name */
541     strcatW(*init_string, provW);
542     strcatW(*init_string, progid);
543     strcatW(*init_string, colW);
544     CoTaskMemFree(progid);
545
546     for (i = 0; i < count; i++)
547     {
548         WCHAR *descr;
549
550         if (!include_pass && propset->rgProperties[i].dwPropertyID == DBPROP_AUTH_PASSWORD) continue;
551
552         descr = get_propinfo_descr(&propset->rgProperties[i], propinfoset);
553         if (descr)
554         {
555             static const WCHAR eqW[] = {'=',0};
556             strcatW(*init_string, descr);
557             strcatW(*init_string, eqW);
558             write_propvalue_str(*init_string, &propset->rgProperties[i]);
559             strcatW(*init_string, colW);
560         }
561     }
562
563     free_dbpropset(count, propset);
564     free_dbpropinfoset(infocount, propinfoset);
565     CoTaskMemFree(desc);
566
567     if (!include_pass)
568         TRACE("%s\n", debugstr_w(*init_string));
569     return S_OK;
570 }
571
572 static HRESULT WINAPI datainit_CreateDBInstance(IDataInitialize *iface, REFCLSID provider,
573                                 IUnknown *outer, DWORD clsctx, LPWSTR reserved, REFIID riid,
574                                 IUnknown **datasource)
575 {
576     datainit *This = impl_from_IDataInitialize(iface);
577
578     TRACE("(%p)->(%s %p 0x%08x %s %s %p)\n", This, debugstr_guid(provider), outer, clsctx, debugstr_w(reserved),
579         debugstr_guid(riid), datasource);
580
581     return CoCreateInstance(provider, outer, clsctx, riid, (void**)datasource);
582 }
583
584 static HRESULT WINAPI datainit_RemoteCreateDBInstanceEx(IDataInitialize *iface, REFCLSID clsidProvider,
585                                 IUnknown *pUnkOuter, DWORD dwClsCtx, LPWSTR pwszReserved, COSERVERINFO *pServerInfo,
586                                 DWORD cmq, GUID **rgpIID, IUnknown **rgpItf, HRESULT *rghr)
587 {
588     datainit *This = impl_from_IDataInitialize(iface);
589
590     FIXME("(%p)->()\n", This);
591
592     return E_NOTIMPL;
593 }
594
595 static HRESULT WINAPI datainit_LoadStringFromStorage(IDataInitialize *iface, LPWSTR pwszFileName,
596                                 LPWSTR *ppwszInitializationString)
597 {
598     datainit *This = impl_from_IDataInitialize(iface);
599
600     FIXME("(%p)->(%s %p)\n", This, debugstr_w(pwszFileName), ppwszInitializationString);
601
602     return E_NOTIMPL;
603 }
604
605 static HRESULT WINAPI datainit_WriteStringToStorage(IDataInitialize *iface, LPWSTR pwszFileName,
606                                 LPWSTR pwszInitializationString, DWORD dwCreationDisposition)
607 {
608     datainit *This = impl_from_IDataInitialize(iface);
609
610     FIXME("(%p)->(%s %s %d)\n", This, debugstr_w(pwszFileName), debugstr_w(pwszInitializationString), dwCreationDisposition);
611
612     return E_NOTIMPL;
613 }
614
615
616 static const struct IDataInitializeVtbl datainit_vtbl =
617 {
618     datainit_QueryInterface,
619     datainit_AddRef,
620     datainit_Release,
621     datainit_GetDataSource,
622     datainit_GetInitializationString,
623     datainit_CreateDBInstance,
624     datainit_RemoteCreateDBInstanceEx,
625     datainit_LoadStringFromStorage,
626     datainit_WriteStringToStorage
627 };
628
629 HRESULT create_data_init(IUnknown *outer, void **obj)
630 {
631     datainit *This;
632
633     TRACE("(%p)\n", obj);
634
635     if(outer) return CLASS_E_NOAGGREGATION;
636
637     *obj = NULL;
638
639     This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
640     if(!This) return E_OUTOFMEMORY;
641
642     This->IDataInitialize_iface.lpVtbl = &datainit_vtbl;
643     This->ref = 1;
644
645     *obj = &This->IDataInitialize_iface;
646
647     return S_OK;
648 }