1/*
2 *  drvconn.c
3 *
4 *  $Id: drvconn.c,v 1.8 2006/01/20 15:58:34 source Exp $
5 *
6 *  The data_sources dialog for SQLDriverConnect and a login box procedures
7 *
8 *  The iODBC driver manager.
9 *
10 *  Copyright (C) 1996-2006 by 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 *
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 "gui.h"
79
80#include <iodbc.h>
81#include <herr.h>
82#include <dlproc.h>
83
84
85SQLRETURN SQL_API
86_iodbcdm_drvconn_dialbox (
87    HWND	  hwnd,
88    LPSTR	  szInOutConnStr,
89    DWORD	  cbInOutConnStr,
90    int	 	* sqlStat,
91    SQLUSMALLINT  fDriverCompletion,
92    UWORD	* config)
93{
94  RETCODE retcode = SQL_ERROR;
95  char *szDSN = NULL, *szDriver = NULL, *szUID = NULL, *szPWD = NULL, *curr;
96  TLOGIN log_t;
97
98  /* Check input parameters */
99  if (!hwnd || !szInOutConnStr || cbInOutConnStr < 1)
100    goto quit;
101
102  /* Check if the DSN is already set or DRIVER */
103  for (curr = szInOutConnStr; *curr; curr += (STRLEN (curr) + 1))
104    {
105      if (!strncasecmp (curr, "DSN=", STRLEN ("DSN=")))
106	{
107	  szDSN = curr + STRLEN ("DSN=");
108	  continue;
109	}
110      if (!strncasecmp (curr, "DRIVER=", STRLEN ("DRIVER=")))
111	{
112	  szDriver = curr + STRLEN ("DRIVER=");
113	  continue;
114	}
115      if (!strncasecmp (curr, "UID=", STRLEN ("UID=")))
116	{
117	  szUID = curr + STRLEN ("UID=");
118	  continue;
119	}
120      if (!strncasecmp (curr, "UserName=", STRLEN ("UserName=")))
121	{
122	  szUID = curr + STRLEN ("UserName=");
123	  continue;
124	}
125      if (!strncasecmp (curr, "LastUser=", STRLEN ("LastUser=")))
126	{
127	  szUID = curr + STRLEN ("LastUser=");
128	  continue;
129	}
130      if (!strncasecmp (curr, "PWD=", STRLEN ("PWD=")))
131	{
132	  szPWD = curr + STRLEN ("PWD=");
133	  continue;
134	}
135      if (!strncasecmp (curr, "Password=", STRLEN ("Password=")))
136	{
137	  szPWD = curr + STRLEN ("Password=");
138	  continue;
139	}
140    }
141
142  if (fDriverCompletion != SQL_DRIVER_NOPROMPT && (!szUID || !szPWD))
143    {
144      create_login (hwnd, szUID, szPWD, szDSN ? szDSN : "(File DSN)", &log_t);
145
146      if (log_t.user && !szUID)
147	{
148	  sprintf (curr, "UID=%s", log_t.user);
149	  curr += STRLEN (curr);
150	  *curr++ = '\0';
151	  free (log_t.user);
152	}
153
154      if (log_t.pwd && !szPWD)
155	{
156	  sprintf (curr, "PWD=%s", log_t.pwd);
157	  curr += STRLEN (curr);
158	  *curr++ = '\0';
159	  free (log_t.pwd);
160	}
161
162      /* add list-terminating '\0' */
163      *curr = '\0';
164    }
165
166  retcode = log_t.ok ? SQL_SUCCESS : SQL_NO_DATA_FOUND;
167
168quit:
169  for (curr = szInOutConnStr; *curr; curr = szDSN + 1)
170    {
171      szDSN = curr + STRLEN (curr);
172      if (szDSN[1])
173	szDSN[0] = ';';
174    }
175
176  return retcode;
177}
178