1 /*
2  *  SQLCreateDataSource.c
3  *
4  *  $Id$
5  *
6  *  Add, modify or delete datasources
7  *
8  *  The iODBC driver manager.
9  *
10  *  Copyright (C) 1996-2021 OpenLink Software <iodbc@openlinksw.com>
11  *  All Rights Reserved.
12  *
13  *  This software is released under the terms of either of the following
14  *  licenses:
15  *
16  *      - GNU Library General Public License (see LICENSE.LGPL)
17  *      - The BSD License (see LICENSE.BSD).
18  *
TEST(ListenerTest,GetEventImmediate)19  *  Note that the only valid version of the LGPL license as far as this
20  *  project is concerned is the original GNU Library General Public License
21  *  Version 2, dated June 1991.
22  *
23  *  While not mandated by the BSD license, any patches you make to the
24  *  iODBC source code may be contributed back into the iODBC project
25  *  at your discretion. Contributions will benefit the Open Source and
26  *  Data Access community as a whole. Submissions may be made at:
27  *
28  *      http://www.iodbc.org
29  *
30  *
31  *  GNU Library Generic Public License Version 2
32  *  ============================================
33  *  This library is free software; you can redistribute it and/or
34  *  modify it under the terms of the GNU Library General Public
35  *  License as published by the Free Software Foundation; only
36  *  Version 2 of the License dated June 1991.
37  *
38  *  This library is distributed in the hope that it will be useful,
39  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
40  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
41  *  Library General Public License for more details.
42  *
43  *  You should have received a copy of the GNU Library General Public
44  *  License along with this library; if not, write to the Free
45  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
46  *
47  *
48  *  The BSD License
49  *  ===============
50  *  Redistribution and use in source and binary forms, with or without
51  *  modification, are permitted provided that the following conditions
52  *  are met:
53  *
54  *  1. Redistributions of source code must retain the above copyright
55  *     notice, this list of conditions and the following disclaimer.
56  *  2. Redistributions in binary form must reproduce the above copyright
57  *     notice, this list of conditions and the following disclaimer in
58  *     the documentation and/or other materials provided with the
59  *     distribution.
60  *  3. Neither the name of OpenLink Software Inc. nor the names of its
61  *     contributors may be used to endorse or promote products derived
62  *     from this software without specific prior written permission.
63  *
64  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
65  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
66  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
67  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL OPENLINK OR
68  *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
69  *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
70  *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
71  *  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
72  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
73  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
74  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
75  */
76 
77 
78 #include <iodbc.h>
79 #include <odbcinst.h>
80 #include <iodbcadm.h>
81 #include "unicode.h"
82 
83 #include "iodbc_error.h"
84 #include "dlf.h"
85 
86 #if defined (__APPLE__) && !defined (NO_FRAMEWORKS)
87 #include <Carbon/Carbon.h>
88 #endif
89 
90 extern BOOL ValidDSN (LPCSTR lpszDSN);
91 extern BOOL ValidDSNW (LPCWSTR lpszDSN);
__anon638ef9720102null92 
93 #if defined (__APPLE__)
94 
95 #define CALL_DRVCONN_DIALBOX() \
96     if (bundle_dll != NULL) \
97       { \
98 	if ((pDrvConn = (pDrvConnFunc) CFBundleGetFunctionPointerForName (bundle_dll, CFSTR ("iodbcdm_drvconn_dialbox"))) != NULL) \
99 	  pDrvConn (parent, dsn, sizeof (dsn), NULL, SQL_DRIVER_PROMPT, &config); \
100 	retcode = TRUE; \
101       }
102 
103 #define CALL_DRVCONN_DIALBOXW() \
104     if (bundle_dll != NULL) \
105       { \
106 	if ((pDrvConnW = (pDrvConnWFunc) CFBundleGetFunctionPointerForName (bundle_dll, CFSTR ("iodbcdm_drvconn_dialboxw"))) != NULL) \
107 	  pDrvConnW (parent, (LPWSTR) dsn, sizeof (dsn) / sizeof (wchar_t), NULL, SQL_DRIVER_PROMPT, &config); \
108 	retcode = TRUE; \
109       }
110 
111 #else
112 
113 #define CALL_DRVCONN_DIALBOX(path) \
114     if ((handle = DLL_OPEN (path)) != NULL) \
115       { \
116 	if ((pDrvConn = (pDrvConnFunc) DLL_PROC (handle, "iodbcdm_drvconn_dialbox")) != NULL) \
117 	  pDrvConn (parent, dsn, sizeof (dsn), NULL, SQL_DRIVER_PROMPT, &config); \
118 	retcode = TRUE; \
119 	DLL_CLOSE (handle); \
120       }
121 
122 #define CALL_DRVCONN_DIALBOXW(path) \
123     if ((handle = DLL_OPEN (path)) != NULL) \
124       { \
125 	if ((pDrvConnW = (pDrvConnWFunc) DLL_PROC (handle, "iodbcdm_drvconn_dialboxw")) != NULL) \
126 	  pDrvConnW (parent, (LPWSTR) dsn, sizeof (dsn) / sizeof (wchar_t), NULL, SQL_DRIVER_PROMPT, &config); \
127 	retcode = TRUE; \
128 	DLL_CLOSE (handle); \
129       }
130 #endif
131 
132 BOOL
133 CreateDataSource (HWND parent, LPCSTR lpszDSN, SQLCHAR waMode)
134 {
135   char dsn[1024] = { 0 };
136   UWORD config = ODBC_USER_DSN;
137   BOOL retcode = FALSE;
138   void *handle;
139   pDrvConnFunc pDrvConn = NULL;
140   pDrvConnWFunc pDrvConnW = NULL;
141 #if defined (__APPLE__) && !defined (NO_FRAMEWORKS)
142   CFBundleRef bundle = NULL;
143   CFBundleRef bundle_dll = NULL;
144   CFURLRef liburl;
145 #endif
146 
147   /* Load the Admin dialbox function */
148 #if defined (__APPLE__)
149 # if !defined(NO_FRAMEWORKS)
150   bundle = CFBundleGetBundleWithIdentifier (CFSTR ("org.iodbc.core"));
151   if (bundle)
152     {
153       /* Search for the iODBCadm library */
154       liburl =
155 	  CFBundleCopyResourceURL (bundle, CFSTR ("iODBCadm.bundle"),
156 	  NULL, NULL);
157       if (liburl)
158 	{
159       	  bundle_dll = CFBundleCreate (NULL, liburl);
160           CFRelease (liburl);
161 	  if (waMode == 'A')
162 	    {
163 	      CALL_DRVCONN_DIALBOX ();
164 	    }
165 	  else
166 	    {
167 	      CALL_DRVCONN_DIALBOXW ();
168 	    }
169 	}
170     }
171 # endif
172 #else
173   if (waMode == 'A')
174     {
175       CALL_DRVCONN_DIALBOX ("libiodbcadm.so.2");
176     }
177   else
178     {
179       CALL_DRVCONN_DIALBOXW ("libiodbcadm.so.2");
180     }
181 #endif
182 
183 
184   return retcode;
185 }
186 
187 
188 BOOL INSTAPI
189 SQLCreateDataSource_Internal (HWND hwndParent, SQLPOINTER lpszDSN,
190     SQLCHAR waMode)
191 {
192   BOOL retcode = FALSE;
193 
194   /* Check input parameters */
195   CLEAR_ERROR ();
196   if (!hwndParent)
197     {
198       PUSH_ERROR (ODBC_ERROR_INVALID_HWND);
199       goto quit;
200     }
201 
202   if (waMode == 'A')
203     {
204       if ((!lpszDSN && !ValidDSN (lpszDSN)) || (!lpszDSN
205 	      && !STRLEN (lpszDSN)))
206 	{
207 	  PUSH_ERROR (ODBC_ERROR_INVALID_DSN);
208 	  goto quit;
209 	}
210     }
211   else
212     {
213       if ((!lpszDSN && !ValidDSNW (lpszDSN)) || (!lpszDSN
214 	      && !WCSLEN (lpszDSN)))
215 	{
216 	  PUSH_ERROR (ODBC_ERROR_INVALID_DSN);
217 	  goto quit;
218 	}
219     }
220 
221   retcode = CreateDataSource (hwndParent, lpszDSN, waMode);
222 
223 quit:
224   return retcode;
225 }
226 
227 BOOL INSTAPI
228 SQLCreateDataSource (HWND hwndParent, LPCSTR lpszDSN)
229 {
230   return SQLCreateDataSource_Internal (hwndParent, (SQLPOINTER) lpszDSN, 'A');
231 }
232 
233 BOOL INSTAPI
234 SQLCreateDataSourceW (HWND hwndParent, LPCWSTR lpszDSN)
235 {
236   return SQLCreateDataSource_Internal (hwndParent, (SQLPOINTER) lpszDSN, 'W');
237 }
238