1/* 2 * ConfigDSN.c 3 * 4 * $Id: ConfigDSN.c,v 1.8 2006/01/24 11:25:07 source Exp $ 5 * 6 * The iODBC driver manager. 7 * 8 * Copyright (C) 1996-2006 by OpenLink Software <iodbc@openlinksw.com> 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 75 76/* ----------- Finished and tested with shadowing ----------- */ 77 78#include <iodbc.h> 79#include <odbcinst.h> 80#include <iodbc_error.h> 81#include <iodbcadm.h> 82 83#include "gui.h" 84 85BOOL INSTAPI 86ConfigDSN ( 87 HWND hwndParent, 88 WORD fRequest, 89 LPCSTR lpszDriver, 90 LPCSTR lpszAttributes) 91{ 92 char *dsn = NULL, *connstr = NULL, *curr, *cour = NULL; 93 char dsnread[4096] = { 0 }; 94 char prov[4096] = { 0 }; 95 int driver_type = -1, flags = 0; 96 BOOL retcode = FALSE; 97 UWORD confMode = ODBC_USER_DSN; 98 99 /* Map the request User/System */ 100 if (fRequest < ODBC_ADD_DSN || fRequest > ODBC_REMOVE_DSN) 101 { 102 SQLPostInstallerError (ODBC_ERROR_INVALID_REQUEST_TYPE, NULL); 103 goto done; 104 } 105 106 if (!lpszDriver || !STRLEN (lpszDriver)) 107 { 108 SQLPostInstallerError (ODBC_ERROR_INVALID_NAME, NULL); 109 goto done; 110 } 111 112 /* Retrieve the config mode */ 113 SQLGetConfigMode (&confMode); 114 115 /* Retrieve the DSN if one exist */ 116 for (curr = (LPSTR) lpszAttributes; curr && *curr; 117 curr += (STRLEN (curr) + 1)) 118 { 119 if (!strncmp (curr, "DSN=", STRLEN ("DSN="))) 120 { 121 dsn = curr + STRLEN ("DSN="); 122 break; 123 } 124 } 125 126 /* Retrieve the corresponding driver */ 127 if (strstr (lpszDriver, "OpenLink") || strstr (lpszDriver, "Openlink") 128 || strstr (lpszDriver, "oplodbc")) 129 { 130 driver_type = 0; 131 132 for (curr = (LPSTR) lpszAttributes, cour = prov; curr && *curr; 133 curr += (STRLEN (curr) + 1), cour += (STRLEN (cour) + 1)) 134 { 135 if (!strncasecmp (curr, "Host=", STRLEN ("Host=")) 136 && STRLEN (curr + STRLEN ("Host="))) 137 { 138 STRCPY (cour, curr); 139 flags |= 0x1; 140 continue; 141 } 142 if (!strncasecmp (curr, "ServerType=", STRLEN ("ServerType=")) 143 && STRLEN (curr + STRLEN ("ServerType="))) 144 { 145 STRCPY (cour, curr); 146 flags |= 0x2; 147 continue; 148 } 149 STRCPY (cour, curr); 150 } 151 152 if (cour && !(flags & 1)) 153 { 154 STRCPY (cour, "Host=localhost\0"); 155 cour += (STRLEN (cour) + 1); 156 } 157 158 if (cour && !(flags & 2)) 159 { 160 STRCPY (cour, "ServerType=Proxy\0"); 161 cour += (STRLEN (cour) + 1); 162 } 163 164 if (cour) 165 *cour = 0; 166 } 167 else if ((strstr (lpszDriver, "Virtuoso") 168 || strstr (lpszDriver, "virtodbc"))) 169 driver_type = 1; 170 171 /* For each request */ 172 switch (fRequest) 173 { 174 case ODBC_ADD_DSN: 175 /* Check if the DSN with this name already exists */ 176 177 SQLSetConfigMode (confMode); 178#ifdef WIN32 179 if (hwndParent && dsn 180 && SQLGetPrivateProfileString ("ODBC 32 bit Data Sources", dsn, "", 181 dsnread, sizeof (dsnread), NULL) 182 && !create_confirm (hwndParent, dsn, 183 "Are you sure you want to overwrite this DSN ?")) 184#else 185 if (hwndParent && dsn 186 && SQLGetPrivateProfileString ("ODBC Data Sources", dsn, "", 187 dsnread, sizeof (dsnread), NULL) 188 && !create_confirm (hwndParent, dsn, 189 "Are you sure you want to overwrite this DSN ?")) 190#endif 191 goto done; 192 193 /* Call the right setup function */ 194 connstr = 195 create_gensetup (hwndParent, dsn, 196 STRLEN (prov) ? prov : lpszAttributes, TRUE); 197 198 /* Check output parameters */ 199 if (!connstr) 200 { 201 SQLPostInstallerError (ODBC_ERROR_OUT_OF_MEM, NULL); 202 goto done; 203 } 204 205 if (connstr == (LPSTR) - 1L) 206 goto done; 207 208 /* Add the DSN to the ODBC Data Sources */ 209 SQLSetConfigMode (confMode); 210 if (!SQLWriteDSNToIni (dsn = connstr + STRLEN ("DSN="), lpszDriver)) 211 goto done; 212 213 /* Add each keyword and values */ 214 for (curr = connstr; *curr; curr += (STRLEN (curr) + 1)) 215 { 216 if (strncmp (curr, "DSN=", STRLEN ("DSN="))) 217 { 218 STRCPY (dsnread, curr); 219 cour = strchr (dsnread, '='); 220 if (cour) 221 *cour = 0; 222 SQLSetConfigMode (confMode); 223 if (!SQLWritePrivateProfileString (dsn, dsnread, (cour 224 && STRLEN (cour + 1)) ? cour + 1 : NULL, NULL)) 225 goto done; 226 } 227 } 228 229 break; 230 231 case ODBC_CONFIG_DSN: 232 233 if (!dsn || !STRLEN (dsn)) 234 { 235 SQLPostInstallerError (ODBC_ERROR_INVALID_KEYWORD_VALUE, NULL); 236 goto done; 237 } 238 239 /* Call the right setup function */ 240 connstr = create_gensetup (hwndParent, dsn, 241 STRLEN (prov) ? prov : lpszAttributes, FALSE); 242 243 /* Check output parameters */ 244 if (!connstr) 245 { 246 SQLPostInstallerError (ODBC_ERROR_OUT_OF_MEM, NULL); 247 goto done; 248 } 249 250 if (connstr == (LPSTR) - 1L) 251 goto done; 252 253 /* Compare if the DSN changed */ 254 if (strcmp (connstr + STRLEN ("DSN="), dsn)) 255 { 256 /* Remove the previous DSN */ 257 SQLSetConfigMode (confMode); 258 if (!SQLRemoveDSNFromIni (dsn)) 259 goto done; 260 /* Add the new DSN section */ 261 SQLSetConfigMode (confMode); 262 if (!SQLWriteDSNToIni (dsn = connstr + STRLEN ("DSN="), lpszDriver)) 263 goto done; 264 } 265 266 /* Add each keyword and values */ 267 for (curr = connstr; *curr; curr += (STRLEN (curr) + 1)) 268 { 269 270 if (strncmp (curr, "DSN=", STRLEN ("DSN="))) 271 { 272 STRCPY (dsnread, curr); 273 cour = strchr (dsnread, '='); 274 if (cour) 275 *cour = 0; 276 SQLSetConfigMode (confMode); 277 if (!SQLWritePrivateProfileString (dsn, dsnread, (cour 278 && STRLEN (cour + 1)) ? cour + 1 : NULL, NULL)) 279 goto done; 280 } 281 } 282 283 break; 284 285 case ODBC_REMOVE_DSN: 286 if (!dsn || !STRLEN (dsn)) 287 { 288 SQLPostInstallerError (ODBC_ERROR_INVALID_KEYWORD_VALUE, NULL); 289 goto done; 290 } 291 292 /* Just remove the DSN */ 293 SQLSetConfigMode (confMode); 294 if (!SQLRemoveDSNFromIni (dsn)) 295 goto done; 296 break; 297 }; 298 299quit: 300 retcode = TRUE; 301 302done: 303 if (connstr && connstr != (LPSTR) - 1L && connstr != lpszAttributes 304 && connstr != prov) 305 free (connstr); 306 307 return retcode; 308} 309