1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <config_folders.h>
21 #include "odbcconfig.hxx"
22 
23 #include <rtl/bootstrap.hxx>
24 #include <rtl/ustring.hxx>
25 #include <rtl/ustrbuf.hxx>
26 #include <osl/diagnose.h>
27 #include <osl/process.h>
28 #include <osl/thread.hxx>
29 #include <vcl/svapp.hxx>
30 
31 #ifdef HAVE_ODBC_SUPPORT
32 
33 #if defined(_WIN32)
34 #define ODBC_LIBRARY    "ODBC32.DLL"
35 #endif
36 #ifdef UNX
37 #ifdef MACOSX
38 #define ODBC_LIBRARY        "libiodbc.dylib"
39 #else
40 #define ODBC_LIBRARY_PLAIN  "libodbc.so"
41 #define ODBC_LIBRARY_1      "libodbc.so.1"
42 #define ODBC_LIBRARY        "libodbc.so.2"
43 #endif
44 #endif
45 
46 #include <connectivity/odbc.hxx>
47 
48 #else
49 
50 #define ODBC_LIBRARY    ""
51 
52 #endif  // HAVE_ODBC_SUPPORT
53 
54 namespace dbaui
55 {
56 
57 #ifdef HAVE_ODBC_SUPPORT
58 typedef SQLRETURN (SQL_API* TSQLManageDataSource) (SQLHWND hwndParent);
59 typedef SQLRETURN (SQL_API* TSQLAllocHandle) (SQLSMALLINT HandleType, SQLHANDLE InputHandle, SQLHANDLE* OutputHandlePtr);
60 typedef SQLRETURN (SQL_API* TSQLFreeHandle) (SQLSMALLINT HandleType, SQLHANDLE Handle);
61 typedef SQLRETURN (SQL_API* TSQLSetEnvAttr) (SQLHENV EnvironmentHandle, SQLINTEGER Attribute, SQLPOINTER ValuePtr, SQLINTEGER StringLength);
62 typedef SQLRETURN (SQL_API* TSQLDataSources) (SQLHENV EnvironmentHandle, SQLUSMALLINT   Direction, SQLCHAR* ServerName,
63                                 SQLSMALLINT BufferLength1, SQLSMALLINT* NameLength1Ptr, SQLCHAR* Description, SQLSMALLINT BufferLength2, SQLSMALLINT* NameLength2Ptr);
64 
65 #define NSQLAllocHandle(a,b,c) (*reinterpret_cast<TSQLAllocHandle>(m_pAllocHandle))(a,b,c)
66 #define NSQLFreeHandle(a,b) (*reinterpret_cast<TSQLFreeHandle>(m_pFreeHandle))(a,b)
67 #define NSQLSetEnvAttr(a,b,c,d) (*reinterpret_cast<TSQLSetEnvAttr>(m_pSetEnvAttr))(a,b,c,d)
68 #define NSQLDataSources(a,b,c,d,e,f,g,h) (*reinterpret_cast<TSQLDataSources>(m_pDataSources))(a,b,c,d,e,f,g,h)
69 #endif
70 
71 // OOdbcLibWrapper
72 
load(const sal_Char * _pLibPath)73 bool OOdbcEnumeration::load(const sal_Char* _pLibPath)
74 {
75     m_sLibPath = OUString::createFromAscii(_pLibPath);
76 #if defined(HAVE_ODBC_SUPPORT) && !defined(DISABLE_DYNLOADING)
77     // load the module
78     m_pOdbcLib = osl_loadModule(m_sLibPath.pData, SAL_LOADMODULE_NOW);
79     return (nullptr != m_pOdbcLib);
80 #else
81     return sal_False;
82 #endif
83 }
84 
unload()85 void OOdbcEnumeration::unload()
86 {
87 #if defined(HAVE_ODBC_SUPPORT) && !defined(DISABLE_DYNLOADING)
88     if (isLoaded())
89     {
90         osl_unloadModule(m_pOdbcLib);
91         m_pOdbcLib = nullptr;
92     }
93 #endif
94 }
95 
loadSymbol(const sal_Char * _pFunctionName)96 oslGenericFunction OOdbcEnumeration::loadSymbol(const sal_Char* _pFunctionName)
97 {
98     return osl_getFunctionSymbol(m_pOdbcLib, OUString::createFromAscii(_pFunctionName).pData);
99 }
100 
101 
102 struct OdbcTypesImpl
103 {
104 #ifdef HAVE_ODBC_SUPPORT
105     SQLHANDLE   hEnvironment;
OdbcTypesImpldbaui::OdbcTypesImpl106     OdbcTypesImpl() : hEnvironment(nullptr) { }
107 #else
108     void*       pDummy;
109 #endif
110 };
111 
OOdbcEnumeration()112 OOdbcEnumeration::OOdbcEnumeration()
113     :m_pOdbcLib(nullptr)
114 #ifdef HAVE_ODBC_SUPPORT
115     ,m_pAllocHandle(nullptr)
116     ,m_pFreeHandle(nullptr)
117     ,m_pSetEnvAttr(nullptr)
118     ,m_pDataSources(nullptr)
119     ,m_pImpl(new OdbcTypesImpl)
120 #endif
121 {
122     bool bLoaded = load(ODBC_LIBRARY);
123 #ifdef ODBC_LIBRARY_1
124     if ( !bLoaded )
125         bLoaded = load(ODBC_LIBRARY_1);
126 #endif
127 #ifdef ODBC_LIBRARY_PLAIN
128     if ( !bLoaded )
129         bLoaded = load(ODBC_LIBRARY_PLAIN);
130 #endif
131 
132     if ( bLoaded )
133     {
134 #ifdef HAVE_ODBC_SUPPORT
135         // load the generic functions
136         m_pAllocHandle = loadSymbol("SQLAllocHandle");
137         m_pFreeHandle = loadSymbol("SQLFreeHandle");
138         m_pSetEnvAttr = loadSymbol("SQLSetEnvAttr");
139         m_pDataSources = loadSymbol("SQLDataSources");
140 
141         // all or nothing
142         if (!m_pAllocHandle || !m_pSetEnvAttr || !m_pDataSources || !m_pFreeHandle)
143         {
144             unload();
145             m_pAllocHandle = m_pFreeHandle = m_pSetEnvAttr = m_pDataSources = nullptr;
146         }
147 #endif
148     }
149 }
150 
~OOdbcEnumeration()151 OOdbcEnumeration::~OOdbcEnumeration()
152 {
153     freeEnv();
154     unload();
155 }
156 
157 // OOdbcEnumeration
allocEnv()158 bool OOdbcEnumeration::allocEnv()
159 {
160     OSL_ENSURE(isLoaded(), "OOdbcEnumeration::allocEnv: not loaded!");
161     if (!isLoaded())
162         return false;
163 
164 #ifdef HAVE_ODBC_SUPPORT
165     if (m_pImpl->hEnvironment)
166         // nothing to do
167         return true;
168     SQLRETURN nResult = NSQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &m_pImpl->hEnvironment);
169     if (SQL_SUCCESS != nResult)
170         // can't do anything without environment
171         return false;
172 
173     NSQLSetEnvAttr(m_pImpl->hEnvironment, SQL_ATTR_ODBC_VERSION, reinterpret_cast<SQLPOINTER>(SQL_OV_ODBC3), SQL_IS_INTEGER);
174     return true;
175 #else
176     return sal_False;
177 #endif
178 }
179 
freeEnv()180 void OOdbcEnumeration::freeEnv()
181 {
182 #ifdef HAVE_ODBC_SUPPORT
183     if (m_pImpl->hEnvironment)
184         NSQLFreeHandle(SQL_HANDLE_ENV, m_pImpl->hEnvironment);
185     m_pImpl->hEnvironment  = nullptr;
186 #endif
187 }
188 
getDatasourceNames(std::set<OUString> & _rNames)189 void OOdbcEnumeration::getDatasourceNames(std::set<OUString>& _rNames)
190 {
191     OSL_ENSURE(isLoaded(), "OOdbcEnumeration::getDatasourceNames: not loaded!");
192     if (!isLoaded())
193         return;
194 
195     if (!allocEnv())
196     {
197         OSL_FAIL("OOdbcEnumeration::getDatasourceNames: could not allocate an ODBC environment!");
198         return;
199     }
200 
201 #ifdef HAVE_ODBC_SUPPORT
202     // now that we have an environment collect the data source names
203     UCHAR szDSN[SQL_MAX_DSN_LENGTH+1];
204     SWORD pcbDSN;
205     UCHAR szDescription[1024+1];
206     SWORD pcbDescription;
207     SQLRETURN nResult = SQL_SUCCESS;
208     rtl_TextEncoding nTextEncoding = osl_getThreadTextEncoding();
209 
210     for (   nResult = NSQLDataSources(m_pImpl->hEnvironment, SQL_FETCH_FIRST, szDSN, sizeof(szDSN), &pcbDSN, szDescription, sizeof(szDescription)-1, &pcbDescription);
211             ;
212             nResult = NSQLDataSources(m_pImpl->hEnvironment, SQL_FETCH_NEXT, szDSN, sizeof(szDSN), &pcbDSN, szDescription, sizeof(szDescription)-1, &pcbDescription)
213         )
214     {
215         if (nResult != SQL_SUCCESS)
216             // no further error handling
217             break;
218         else
219         {
220             OUString aCurrentDsn(reinterpret_cast<const char*>(szDSN),pcbDSN, nTextEncoding);
221             _rNames.insert(aCurrentDsn);
222         }
223     }
224 #else
225     (void) _rNames;
226 #endif
227 }
228 
229 #ifdef HAVE_ODBC_ADMINISTRATION
230 
231 // ProcessTerminationWait
232 class ProcessTerminationWait : public ::osl::Thread
233 {
234     oslProcess       m_hProcessHandle;
235     Link<void*,void> m_aFinishHdl;
236     ImplSVEvent* m_nEventId;
237 
238 public:
ProcessTerminationWait(oslProcess _hProcessHandle,const Link<void *,void> & _rFinishHdl)239     ProcessTerminationWait( oslProcess _hProcessHandle, const Link<void*,void>& _rFinishHdl )
240         : m_hProcessHandle( _hProcessHandle )
241         , m_aFinishHdl( _rFinishHdl )
242         , m_nEventId(nullptr)
243     {
244     }
245 
disableCallback()246     void disableCallback()
247     {
248         // if finished event not posted yet, disable by turning it to a no-op Link
249         m_aFinishHdl = Link<void*, void>();
250         if (m_nEventId)
251         {
252             // already posted, remove it
253             Application::RemoveUserEvent(m_nEventId);
254             m_nEventId = nullptr;
255         }
256     }
257 
receivedCallback()258     void receivedCallback()
259     {
260         m_nEventId = nullptr;
261     }
262 
263 protected:
run()264     virtual void SAL_CALL run() override
265     {
266         osl_setThreadName("dbaui::ProcessTerminationWait");
267 
268         osl_joinProcess( m_hProcessHandle );
269         osl_freeProcessHandle( m_hProcessHandle );
270         m_nEventId = Application::PostUserEvent( m_aFinishHdl );
271     }
272 };
273 
274 // OOdbcManagement
OOdbcManagement(const Link<void *,void> & rAsyncFinishCallback)275 OOdbcManagement::OOdbcManagement(const Link<void*,void>& rAsyncFinishCallback)
276     : m_aAsyncFinishCallback(rAsyncFinishCallback)
277 {
278 }
279 
~OOdbcManagement()280 OOdbcManagement::~OOdbcManagement()
281 {
282     // wait for our thread to be finished
283     if ( m_pProcessWait.get() )
284         m_pProcessWait->join();
285 }
286 
manageDataSources_async()287 bool OOdbcManagement::manageDataSources_async()
288 {
289     OSL_PRECOND( !isRunning(), "OOdbcManagement::manageDataSources_async: still running from the previous call!" );
290     if ( isRunning() )
291         return false;
292 
293     // this is done in an external process, due to #i78733#
294     // (and note this whole functionality is supported on Windows only, ATM)
295     OUString sExecutableName( "$BRAND_BASE_DIR/" LIBO_LIBEXEC_FOLDER "/odbcconfig.exe" );
296     ::rtl::Bootstrap::expandMacros( sExecutableName ); //TODO: detect failure
297     oslProcess hProcessHandle(nullptr);
298     oslProcessError eError = osl_executeProcess( sExecutableName.pData, nullptr, 0, 0, nullptr, nullptr, nullptr, 0, &hProcessHandle );
299     if ( eError != osl_Process_E_None )
300         return false;
301 
302     m_pProcessWait.reset( new ProcessTerminationWait( hProcessHandle, m_aAsyncFinishCallback ) );
303     m_pProcessWait->create();
304     return true;
305 }
306 
disableCallback()307 void OOdbcManagement::disableCallback()
308 {
309     if (m_pProcessWait.get())
310         m_pProcessWait->disableCallback();
311 }
312 
receivedCallback()313 void OOdbcManagement::receivedCallback()
314 {
315     if (m_pProcessWait.get())
316         m_pProcessWait->receivedCallback();
317 }
318 
isRunning() const319 bool OOdbcManagement::isRunning() const
320 {
321     return ( m_pProcessWait.get() && m_pProcessWait->isRunning() );
322 }
323 
324 #endif // HAVE_ODBC_ADMINISTRATION
325 
326 }   // namespace dbaui
327 
328 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
329