ConfigDSN.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * ConfigDSN.c
  3. *
  4. * $Id$
  5. *
  6. * The iODBC driver manager.
  7. *
  8. * Copyright (C) 1996-2021 OpenLink Software <[email protected]>
  9. * All Rights Reserved.
  10. *
  11. * This software is released under the terms of either of the following
  12. * licenses:
  13. *
  14. * - GNU Library General Public License (see LICENSE.LGPL)
  15. * - The BSD License (see LICENSE.BSD).
  16. *
  17. * Note that the only valid version of the LGPL license as far as this
  18. * project is concerned is the original GNU Library General Public License
  19. * Version 2, dated June 1991.
  20. *
  21. * While not mandated by the BSD license, any patches you make to the
  22. * iODBC source code may be contributed back into the iODBC project
  23. * at your discretion. Contributions will benefit the Open Source and
  24. * Data Access community as a whole. Submissions may be made at:
  25. *
  26. * http://www.iodbc.org
  27. *
  28. *
  29. * GNU Library Generic Public License Version 2
  30. * ============================================
  31. * This library is free software; you can redistribute it and/or
  32. * modify it under the terms of the GNU Library General Public
  33. * License as published by the Free Software Foundation; only
  34. * Version 2 of the License dated June 1991.
  35. *
  36. * This library is distributed in the hope that it will be useful,
  37. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  38. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  39. * Library General Public License for more details.
  40. *
  41. * You should have received a copy of the GNU Library General Public
  42. * License along with this library; if not, write to the Free
  43. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  44. *
  45. *
  46. * The BSD License
  47. * ===============
  48. * Redistribution and use in source and binary forms, with or without
  49. * modification, are permitted provided that the following conditions
  50. * are met:
  51. *
  52. * 1. Redistributions of source code must retain the above copyright
  53. * notice, this list of conditions and the following disclaimer.
  54. * 2. Redistributions in binary form must reproduce the above copyright
  55. * notice, this list of conditions and the following disclaimer in
  56. * the documentation and/or other materials provided with the
  57. * distribution.
  58. * 3. Neither the name of OpenLink Software Inc. nor the names of its
  59. * contributors may be used to endorse or promote products derived
  60. * from this software without specific prior written permission.
  61. *
  62. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  63. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  64. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  65. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL OPENLINK OR
  66. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  67. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  68. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  69. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  70. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  71. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  72. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  73. */
  74. /* ----------- Finished and tested with shadowing ----------- */
  75. #include <iodbc.h>
  76. #include <odbcinst.h>
  77. #include <iodbc_error.h>
  78. #include <iodbcadm.h>
  79. #include "gui.h"
  80. BOOL INSTAPI
  81. ConfigDSN (
  82. HWND hwndParent,
  83. WORD fRequest,
  84. LPCSTR lpszDriver,
  85. LPCSTR lpszAttributes)
  86. {
  87. char *dsn = NULL, *connstr = NULL, *curr, *cour = NULL;
  88. char dsnread[4096] = { 0 };
  89. char prov[4096] = { 0 };
  90. int driver_type = -1, flags = 0;
  91. BOOL retcode = FALSE;
  92. UWORD confMode = ODBC_USER_DSN;
  93. /* Map the request User/System */
  94. if (fRequest < ODBC_ADD_DSN || fRequest > ODBC_REMOVE_DSN)
  95. {
  96. SQLPostInstallerError (ODBC_ERROR_INVALID_REQUEST_TYPE, NULL);
  97. goto done;
  98. }
  99. if (!lpszDriver || !STRLEN (lpszDriver))
  100. {
  101. SQLPostInstallerError (ODBC_ERROR_INVALID_NAME, NULL);
  102. goto done;
  103. }
  104. /* Retrieve the config mode */
  105. SQLGetConfigMode (&confMode);
  106. /* Retrieve the DSN if one exist */
  107. for (curr = (LPSTR) lpszAttributes; curr && *curr;
  108. curr += (STRLEN (curr) + 1))
  109. {
  110. if (!strncmp (curr, "DSN=", STRLEN ("DSN=")))
  111. {
  112. dsn = curr + STRLEN ("DSN=");
  113. break;
  114. }
  115. }
  116. /* Retrieve the corresponding driver */
  117. #if !defined (__APPLE__)
  118. if (strstr (lpszDriver, "OpenLink") || strstr (lpszDriver, "Openlink")
  119. || strstr (lpszDriver, "oplodbc"))
  120. {
  121. driver_type = 0;
  122. for (curr = (LPSTR) lpszAttributes, cour = prov; curr && *curr;
  123. curr += (STRLEN (curr) + 1), cour += (STRLEN (cour) + 1))
  124. {
  125. if (!strncasecmp (curr, "Host=", STRLEN ("Host="))
  126. && STRLEN (curr + STRLEN ("Host=")))
  127. {
  128. STRCPY (cour, curr);
  129. flags |= 0x1;
  130. continue;
  131. }
  132. if (!strncasecmp (curr, "ServerType=", STRLEN ("ServerType="))
  133. && STRLEN (curr + STRLEN ("ServerType=")))
  134. {
  135. STRCPY (cour, curr);
  136. flags |= 0x2;
  137. continue;
  138. }
  139. STRCPY (cour, curr);
  140. }
  141. if (cour && !(flags & 1))
  142. {
  143. STRCPY (cour, "Host=localhost\0");
  144. cour += (STRLEN (cour) + 1);
  145. }
  146. if (cour && !(flags & 2))
  147. {
  148. STRCPY (cour, "ServerType=Proxy\0");
  149. cour += (STRLEN (cour) + 1);
  150. }
  151. if (cour)
  152. *cour = 0;
  153. }
  154. else if ((strstr (lpszDriver, "Virtuoso")
  155. || strstr (lpszDriver, "virtodbc")))
  156. driver_type = 1;
  157. #endif
  158. /* For each request */
  159. switch (fRequest)
  160. {
  161. case ODBC_ADD_DSN:
  162. /* Check if the DSN with this name already exists */
  163. SQLSetConfigMode (confMode);
  164. #ifdef WIN32
  165. if (hwndParent && dsn
  166. && SQLGetPrivateProfileString ("ODBC 32 bit Data Sources", dsn, "",
  167. dsnread, sizeof (dsnread), NULL)
  168. && !create_confirm (hwndParent, dsn,
  169. "Are you sure you want to overwrite this DSN ?"))
  170. #else
  171. if (hwndParent && dsn
  172. && SQLGetPrivateProfileString ("ODBC Data Sources", dsn, "",
  173. dsnread, sizeof (dsnread), NULL)
  174. && !create_confirm (hwndParent, dsn,
  175. "Are you sure you want to overwrite this DSN ?"))
  176. #endif
  177. goto done;
  178. /* Call the right setup function */
  179. connstr =
  180. create_gensetup (hwndParent, dsn,
  181. STRLEN (prov) ? prov : lpszAttributes, TRUE);
  182. /* Check output parameters */
  183. if (!connstr)
  184. {
  185. SQLPostInstallerError (ODBC_ERROR_OUT_OF_MEM, NULL);
  186. goto done;
  187. }
  188. if (connstr == (LPSTR) - 1L)
  189. goto done;
  190. /* Add the DSN to the ODBC Data Sources */
  191. SQLSetConfigMode (confMode);
  192. if (!SQLWriteDSNToIni (dsn = connstr + STRLEN ("DSN="), lpszDriver))
  193. goto done;
  194. /* Add each keyword and values */
  195. for (curr = connstr; *curr; curr += (STRLEN (curr) + 1))
  196. {
  197. if (strncmp (curr, "DSN=", STRLEN ("DSN=")))
  198. {
  199. STRCPY (dsnread, curr);
  200. cour = strchr (dsnread, '=');
  201. if (cour)
  202. *cour = 0;
  203. SQLSetConfigMode (confMode);
  204. if (!SQLWritePrivateProfileString (dsn, dsnread, (cour
  205. && STRLEN (cour + 1)) ? cour + 1 : NULL, NULL))
  206. goto done;
  207. }
  208. }
  209. break;
  210. case ODBC_CONFIG_DSN:
  211. if (!dsn || !STRLEN (dsn))
  212. {
  213. SQLPostInstallerError (ODBC_ERROR_INVALID_KEYWORD_VALUE, NULL);
  214. goto done;
  215. }
  216. /* Call the right setup function */
  217. connstr = create_gensetup (hwndParent, dsn,
  218. STRLEN (prov) ? prov : lpszAttributes, FALSE);
  219. /* Check output parameters */
  220. if (!connstr)
  221. {
  222. SQLPostInstallerError (ODBC_ERROR_OUT_OF_MEM, NULL);
  223. goto done;
  224. }
  225. if (connstr == (LPSTR) - 1L)
  226. goto done;
  227. /* Remove the previous DSN */
  228. SQLSetConfigMode (confMode);
  229. if (!SQLRemoveDSNFromIni (dsn))
  230. goto done;
  231. /* Add the new DSN section */
  232. SQLSetConfigMode (confMode);
  233. if (!SQLWriteDSNToIni (dsn = connstr + STRLEN ("DSN="), lpszDriver))
  234. goto done;
  235. /* Add each keyword and values */
  236. for (curr = connstr; *curr; curr += (STRLEN (curr) + 1))
  237. {
  238. if (strncmp (curr, "DSN=", STRLEN ("DSN=")))
  239. {
  240. STRCPY (dsnread, curr);
  241. cour = strchr (dsnread, '=');
  242. if (cour)
  243. *cour = 0;
  244. SQLSetConfigMode (confMode);
  245. if (!SQLWritePrivateProfileString (dsn, dsnread, (cour
  246. && STRLEN (cour + 1)) ? cour + 1 : NULL, NULL))
  247. goto done;
  248. }
  249. }
  250. break;
  251. case ODBC_REMOVE_DSN:
  252. if (!dsn || !STRLEN (dsn))
  253. {
  254. SQLPostInstallerError (ODBC_ERROR_INVALID_KEYWORD_VALUE, NULL);
  255. goto done;
  256. }
  257. /* Just remove the DSN */
  258. SQLSetConfigMode (confMode);
  259. if (!SQLRemoveDSNFromIni (dsn))
  260. goto done;
  261. break;
  262. };
  263. quit:
  264. retcode = TRUE;
  265. done:
  266. if (connstr && connstr != (LPSTR) - 1L && connstr != lpszAttributes
  267. && connstr != prov)
  268. free (connstr);
  269. return retcode;
  270. }