1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
|
/*
* iodbcinst.c
*
* $Id$
*
* Minimum set of ODBC Installer code to allow Virtuoso to edit odbc.ini
* udbc.ini and odbcinst.ini files
*
* This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
* project.
*
* Copyright (C) 1998-2012 OpenLink Software
*
* This project is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; only version 2 of the License, dated June 1991.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
/*
* Just so the object file is not empty
*/
#if 0
static char _iodbcinst_version[] = "";
#endif
#if !defined (WIN32)
#include "odbcinc.h"
#include "libutil.h"
#include <pwd.h>
#if 0
#define Debug(X) log_info X
#else
#define Debug(X)
#endif
/*
* Limits and Constants
*/
#define ERROR_NUM 8
#define MAX_ENTRIES 1024
#define INTERNAL_SECTIONS \
"UDBC,ODBC,Communications,Protocol TCP,Protocol SPX,Protocol DECNET,Default"
#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE 1
#endif
/*
* Global variables
*/
#if defined (UNIX_ODBC)
static PCONFIG cfg_odbc;
static PCONFIG cfg_odbcinst;
#else
static PCONFIG cfg_udbc;
#endif
static int _iodbcinst_initialized = 0;
static int configMode;
/*
* iODBCinst error code array and macros
*/
static DWORD ierror[ERROR_NUM] = {0};
static LPSTR errormsg[ERROR_NUM] = {0};
static SQLSMALLINT numerrors = -1;
#define CLEAR_ERROR() \
numerrors = -1;
#define PUSH_ERROR(error) \
if(numerrors < ERROR_NUM) \
{ \
ierror[++numerrors] = (error); \
errormsg[numerrors] = NULL; \
}
#define POP_ERROR(error) \
if(numerrors != -1) \
{ \
errormsg[numerrors] = NULL; \
(error) = ierror[numerrors--]; \
}
#define IS_ERROR() \
(numerrors != -1) ? 1 : 0
/*
* ----------------------------------------------------------------------
* Internal functions
* ----------------------------------------------------------------------
*/
static void
_iodbcinst_initialize (void)
{
char *odbcini;
char *udbcini;
char *odbcinstini;
char *ptr;
char path[256];
Debug (("_iodbcinst_initialize"));
_iodbcinst_initialized = 1;
/*
* Find out where odbc.ini resides
*
* 1. Check for ODBCINI environment and see if the file is accessible
* 2. Check for HOME environment variable
* 3. Check for home directory in /etc/passwd
* 4. Try to use /etc/odbc.ini
*/
if ((odbcini = getenv ("ODBCINI")) == NULL || access (odbcini, 4))
{
if ((ptr = getenv ("HOME")) == NULL);
{
ptr = (char *) getpwuid (getuid ());
if (ptr != NULL)
ptr = ((struct passwd *) ptr)->pw_dir;
}
if (ptr != NULL)
snprintf (path, sizeof (path), "%s/.odbc.ini", ptr);
if (access (path, 4))
odbcini = "/etc/odbc.ini";
else
odbcini = path;
}
/*
* Find out where odbcinst.ini resides
*
* 1. Check for ODBCINSTINI environment
* 2. Use /etc/odbcinst.ini
*/
if ((odbcinstini = getenv ("ODBCINSTINI")) == NULL)
odbcinstini = "/etc/odbcinst.ini";
/*
* Find out where udbc.ini resides
*
* 1. Check for UDBCINI environment
* 2. Use /etc/udbc.ini
*/
if ((udbcini = getenv ("UDBCINI")) == NULL)
udbcini = "/etc/udbc.ini";
#if defined (UNIX_ODBC)
/*
* Try to open the 2 ini files
*/
if (cfg_init (&cfg_odbc, odbcini))
log_error ("iodbcinst: Unable to open %s", odbcini);
if (cfg_init (&cfg_odbcinst, odbcinstini))
log_error ("iodbcinst: Unable to open %s", odbcinstini);
#else
if (cfg_init (&cfg_udbc, udbcini))
log_error ("iodbcinst: Unable to open %s", udbcini);
#endif
}
static int
SortFun (const void *p1, const void *p2)
{
char **s1 = (char **) p1;
char **s2 = (char **) p2;
return stricmp (*s1, *s2);
}
static int
_iodbcinst_argv_to_buf (char **array, int num_elem,
LPSTR lpszRetBuffer, int cbRetBuffer)
{
int i, count;
char *ptr;
/*
* Sort the section names
*/
if (num_elem > 1)
qsort (array, num_elem, sizeof (char *), SortFun);
/*
* Initialize the buffer
*/
count = 0;
ptr = lpszRetBuffer;
memset (lpszRetBuffer, '\0', cbRetBuffer);
/*
* Now copy the entries back into the buffer separated by a '\0'
* character.
*/
for (i = 0; i < num_elem; i++)
{
int l;
l = strlen (array[i]) + 1;
/*
* Check if this name will fit into the buffer
*/
if (count + l + 1 >= cbRetBuffer)
break;
memcpy (ptr, array[i], l);
ptr = ptr + l;
count = count + l;
}
return count;
}
static int
_iodbcinst_read_sections (PCONFIG pCfg,
LPSTR lpszRetBuffer, int cbRetBuffer)
{
int count = 0;
char **array;
int i, max_elem;
Debug (("_iodbcinst_read_sections (%p, %p, %d)",
pCfg, lpszRetBuffer, cbRetBuffer));
/*
* Initialize
*/
i = 0, max_elem = 0;
array = (char **) calloc (MAX_ENTRIES, sizeof (char *));
if (array == NULL)
{
PUSH_ERROR (ODBC_ERROR_OUT_OF_MEM);
return 0;
}
/*
* Read all section names into array
*/
cfg_rewind (pCfg);
while (i < MAX_ENTRIES && cfg_nextentry (pCfg) == 0)
{
if (cfg_section (pCfg) &&
#if !defined (UNIX_ODBC)
strindex (INTERNAL_SECTIONS, pCfg->section) == 0 &&
#endif
(array[i++] = strdup (pCfg->section)) == NULL)
{
PUSH_ERROR (ODBC_ERROR_OUT_OF_MEM);
goto done;
}
}
max_elem = i;
/*
* Copy the information back into the buffer.
*/
count = _iodbcinst_argv_to_buf (array, max_elem, lpszRetBuffer, cbRetBuffer);
done:
if (array)
{
for (i = 0; i < max_elem; i++)
if (array[i])
free (array[i]);
free (array);
}
return count;
}
static int
_iodbcinst_read_keys (PCONFIG pCfg,
LPSTR lpszSection, LPSTR lpszRetBuffer, int cbRetBuffer)
{
int count = 0;
char **array;
int i, max_elem = 0;
Debug (("_iodbcinst_read_keys (%p, %s, %p, %d)",
pCfg, lpszSection, lpszRetBuffer, cbRetBuffer));
/*
* Initialize
*/
i = 0, max_elem = 0;
array = (char **) calloc (MAX_ENTRIES, sizeof (char *));
if (array == NULL)
{
PUSH_ERROR (ODBC_ERROR_OUT_OF_MEM);
return 0;
}
/*
* Find the section we are looking for
*/
if (cfg_find (pCfg, lpszSection, NULL) != 0)
{
PUSH_ERROR (ODBC_ERROR_GENERAL_ERR);
count = 0;
goto done;
}
while (i < MAX_ENTRIES && cfg_nextentry (pCfg) == 0)
{
if (cfg_section (pCfg))
break;
array[i++] = strdup (pCfg->id);
}
max_elem = i;
/*
* Copy the information back into the buffer.
*/
count = _iodbcinst_argv_to_buf (array, max_elem, lpszRetBuffer, cbRetBuffer);
done:
if (array)
{
for (i = 0; i < max_elem; i++)
if (array[i])
free (array[i]);
free (array);
}
return count;
}
/*
* ----------------------------------------------------------------------
* External functions
* ----------------------------------------------------------------------
*/
#ifndef HIDE_CONFLICTING_IODBC_FUNCS
int INSTAPI
SQLGetPrivateProfileString (
LPCSTR lpszSection,
LPCSTR lpszEntry,
LPCSTR lpszDefault,
LPSTR lpszRetBuffer,
int cbRetBuffer,
LPCSTR lpszFilename)
{
PCONFIG pCfg;
char *ptr;
Debug (("SQLGetPrivateProfileString('%s', '%s', '%s', %p, %d, '%s')",
lpszSection, lpszEntry, lpszDefault, lpszRetBuffer, cbRetBuffer,
lpszFilename));
/*
* Initialize
*/
if (!_iodbcinst_initialized)
_iodbcinst_initialize ();
/*
* Check input parameters
*/
CLEAR_ERROR ();
#if defined (UNIX_ODBC)
/*
* Check which ini file to read
*/
if (!stricmp (lpszFilename, "odbc.ini"))
pCfg = cfg_odbc;
else if (!stricmp (lpszFilename, "odbcinst.ini"))
pCfg = cfg_odbcinst;
else
{
PUSH_ERROR (ODBC_ERROR_GENERAL_ERR);
return 0;
}
#else
/*
* UDBC fortunately uses only one file
*/
pCfg = cfg_udbc;
#endif
/*
* Make sure we use the most up-to-date content
*/
cfg_refresh (pCfg);
/*
* Check for special cases
*/
if (lpszSection == NULL)
{
/*
* If the Section name is NULL then we retrieve all section names
*/
return _iodbcinst_read_sections (pCfg, lpszRetBuffer, cbRetBuffer);
}
else if (lpszEntry == NULL)
{
/*
* If the Entry name is NULL we retrieve all keys within that section
*/
return _iodbcinst_read_keys (pCfg, (LPSTR) lpszSection, lpszRetBuffer,
cbRetBuffer);
}
/*
* Otherwise we just get the value for that Section/Entry combination
*/
if (cfg_find (pCfg, (char *) lpszSection, (char *) lpszEntry))
ptr = (char *) lpszDefault;
else
ptr = pCfg->value;
/*
* Put the (default) value into the buffer if it will fit
*/
if (ptr && strlen (ptr) < cbRetBuffer)
{
strcpy_size_ck (lpszRetBuffer, ptr, cbRetBuffer);
return strlen (ptr);
}
/*
* If all else fails, just signal we returned 0 bytes into the buffer
*/
return 0;
}
#endif
BOOL INSTAPI
SQLWritePrivateProfileString (
LPCSTR lpszSection,
LPCSTR lpszEntry,
LPCSTR lpszString,
LPCSTR lpszFilename)
{
PCONFIG pCfg;
Debug (("SQLWritePrivateProfileString ('%s', '%s', '%s', '%s')",
lpszSection, lpszEntry, lpszString, lpszFilename));
if (!_iodbcinst_initialized)
_iodbcinst_initialize ();
/*
* Check input parameters
*/
CLEAR_ERROR ();
#if defined (UNIX_ODBC)
/*
* Check which ini file to read
*/
if (!stricmp (lpszFilename, "odbc.ini"))
pCfg = cfg_odbc;
else if (!stricmp (lpszFilename, "odbcinst.ini"))
pCfg = cfg_odbcinst;
else
{
PUSH_ERROR (ODBC_ERROR_GENERAL_ERR);
return SQL_FALSE;
}
#else
/*
* UDBC fortunately uses only one file
*/
pCfg = cfg_udbc;
#endif
/*
* Write the entry into the correct file
*
* If the String is NULL this will remove the entry from the section
* If the Entry is NULL this will remove the entire section
*/
if (cfg_write (pCfg, (char *) lpszSection, (char *) lpszEntry, (char *) lpszString))
{
PUSH_ERROR (ODBC_ERROR_REQUEST_FAILED);
return SQL_FALSE;
}
/*
* Commit the changes into the ini file
*/
if (cfg_commit (pCfg))
{
PUSH_ERROR (ODBC_ERROR_REQUEST_FAILED);
return SQL_FALSE;
}
/*
* All done
*/
return SQL_TRUE;
}
BOOL INSTAPI
SQLSetConfigMode (SQLUSMALLINT wConfigMode)
{
Debug (("SQLSetConfigMode (%d)", wConfigMode));
if (!_iodbcinst_initialized)
_iodbcinst_initialize();
/*
* Check input parameters
*/
CLEAR_ERROR ();
switch (wConfigMode)
{
case ODBC_BOTH_DSN:
case ODBC_USER_DSN:
case ODBC_SYSTEM_DSN:
configMode = wConfigMode;
break;
default:
PUSH_ERROR (ODBC_ERROR_INVALID_PARAM_SEQUENCE);
return SQL_FALSE;
};
return SQL_TRUE;
}
BOOL INSTAPI
SQLWriteDSNToIni (LPCSTR lpszDSN, LPCSTR lpszDriver)
{
#if defined (UNIX_ODBC)
char driver[1024];
#endif
Debug (("SQLWriteDSNToIni ('%s', '%s')", lpszDSN, lpszDriver));
/*
* Check input parameters
*/
CLEAR_ERROR ();
#if defined (UNIX_ODBC)
/*
* Get the path to the driver from the odbcinst.ini file
*/
SQLGetPrivateProfileString (lpszDriver,
"Driver", "UNKNOWN", driver, sizeof (driver), "odbcinst.ini");
/*
* Add the DSN entry to [ODBC Data Sources]
*/
SQLWritePrivateProfileString (
"ODBC Data Sources", lpszDSN, lpszDriver, "odbc.ini");
/*
* Add a new section for this DSN and only fill in the DRIVER key
* The rest must be filled in by the application
*/
SQLWritePrivateProfileString (
lpszDSN, "DRIVER", driver, "odbc.ini");
#endif
/*
* All done
*/
return SQL_TRUE;
}
BOOL INSTAPI
SQLRemoveDSNFromIni (LPCSTR lpszDSN)
{
Debug (("SQLRemoveDSNFromIni ('%s')", lpszDSN));
/*
* Check input parameters
*/
CLEAR_ERROR ();
#if defined (UNIX_ODBC)
/*
* First remove the entry in [ODBC Data Sources]
*/
SQLWritePrivateProfileString ("ODBC Data Sources", lpszDSN, NULL, "odbc.ini");
/*
* Then remove the entire DSN
*/
SQLWritePrivateProfileString (lpszDSN, NULL, NULL, "odbc.ini");
#else
/*
* Just remove the entire DSN
*/
SQLWritePrivateProfileString (lpszDSN, NULL, NULL, "udbc.ini");
#endif
return SQL_TRUE;
}
BOOL INSTAPI
SQLGetInstalledDrivers (LPSTR lpszBuf, WORD cbBufMax, WORD * pcbBufOut)
{
int count;
Debug (("SQLGetInstalledDrivers(%p, %d, %p)",
lpszBuf, cbBufMax, pcbBufOut));
/*
* Check input parameters
*/
CLEAR_ERROR ();
#if defined (UNIX_ODBC)
/*
* Return the list of installed drivers
*/
count = SQLGetPrivateProfileString (
"ODBC Drivers", NULL, "", lpszBuf, cbBufMax, "odbcinst.ini");
#else
strncpy (lpszBuf, "OpenLink Generic UDBC Driver", cbBufMax - 1);
lpszBuf[cbBufMax - 1] = '\0';
count = strlen (lpszBuf);
#endif
/*
* Return the number of characters in the buffer
*/
if (pcbBufOut)
*pcbBufOut = count;
/*
* If something goes wrong this function returns SQL_FALSE and the
* Virtuoso VSP page will show:
*
* This function not supported in current server version
*/
return count ? SQL_TRUE : SQL_FALSE;
}
/*
* This is a stub for now
*/
BOOL INSTAPI
SQLWriteFileDSN (LPCSTR lpszFileName,
LPCSTR lpszAppName, LPCSTR lpszKeyName, LPSTR lpszString)
{
Debug (("SQLWriteFileDSN (%s, %s, %s, %s)",
lpszFileName, lpszAppName, lpszKeyName, lpszString));
/*
* Check input parameters
*/
CLEAR_ERROR ();
return SQL_FALSE;
}
#endif /* WIN32 */
|