Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1541 lines
41 KiB

  1. /***************************************************************************/
  2. /* CONNECT.C */
  3. /* Copyright (C) 1995-96 SYWARE Inc., All rights reserved */
  4. /***************************************************************************/
  5. // Commenting #define out - causing compiler error - not sure if needed, compiles
  6. // okay without it.
  7. //#define WINVER 0x0400
  8. #include "precomp.h"
  9. #include "resource.h"
  10. #include "wbemidl.h"
  11. #include <comdef.h>
  12. //smart pointer
  13. _COM_SMARTPTR_TYPEDEF(IWbemServices, IID_IWbemServices);
  14. _COM_SMARTPTR_TYPEDEF(IEnumWbemClassObject, IID_IEnumWbemClassObject);
  15. //_COM_SMARTPTR_TYPEDEF(IWbemContext, IID_IWbemContext );
  16. _COM_SMARTPTR_TYPEDEF(IWbemLocator, IID_IWbemLocator);
  17. #include "drdbdr.h"
  18. #include "odbcinst.h"
  19. BOOL INTFUNC ReadInNamespace (LPSTR lpFrom, SDWORD cbFrom, LPSTR *lpTo,
  20. UWORD FAR *pfType, LPSTR FAR *lpRemainder,
  21. SDWORD FAR *pcbRemainder);
  22. #define TYPE_NONE 0
  23. #define TYPE_BRACE 1
  24. #define TYPE_SIMPLE_IDENTIFIER 2
  25. /***************************************************************************/
  26. extern "C" BOOL EXPFUNC dlgDirectory(
  27. HWND hDlg,
  28. UINT message,
  29. WPARAM wParam,
  30. LPARAM lParam)
  31. {
  32. LPUSTR lpszDatabase;
  33. switch (message) {
  34. case WM_INITDIALOG:
  35. SendDlgItemMessage(hDlg, DATABASE_NAME, EM_LIMITTEXT,
  36. MAX_DATABASE_NAME_LENGTH, 0L);
  37. SetWindowLong(hDlg, DWL_USER, lParam);
  38. SetDlgItemText(hDlg, DATABASE_NAME, (LPSTR) lParam);
  39. return (TRUE);
  40. case WM_COMMAND:
  41. switch (GET_WM_COMMAND_ID(wParam, lParam)) {
  42. case IDOK:
  43. /* Get answer */
  44. lpszDatabase = (LPUSTR) GetWindowLong(hDlg, DWL_USER);
  45. GetDlgItemText(hDlg, DATABASE_NAME, (LPSTR) lpszDatabase,
  46. MAX_DATABASE_NAME_LENGTH);
  47. AnsiToOem((LPCSTR) lpszDatabase, (LPSTR) lpszDatabase);
  48. /* Clear off leading blanks */
  49. while (*lpszDatabase == ' ')
  50. s_lstrcpy(lpszDatabase, lpszDatabase+1);
  51. /* Clear off trailing blanks */
  52. while (*lpszDatabase != '\0') {
  53. if (lpszDatabase[s_lstrlen(lpszDatabase)-1] != ' ')
  54. break;
  55. lpszDatabase[s_lstrlen(lpszDatabase)-1] = '\0';
  56. }
  57. /* Get rid of terminating backslash (if any) */
  58. if (s_lstrlen(lpszDatabase) > 0) {
  59. if (lpszDatabase[s_lstrlen(lpszDatabase)-1] == '\\')
  60. lpszDatabase[s_lstrlen(lpszDatabase)-1] = '\0';
  61. }
  62. else {
  63. /* If no directory specified, use the current directory */
  64. s_lstrcpy(lpszDatabase, ".");
  65. }
  66. /* Return */
  67. EndDialog(hDlg, TRUE);
  68. return (TRUE);
  69. case IDCANCEL:
  70. EndDialog(hDlg, FALSE);
  71. return (TRUE);
  72. }
  73. break;
  74. }
  75. return (FALSE);
  76. }
  77. /***************************************************************************/
  78. RETCODE SQL_API SQLAllocEnv(
  79. HENV FAR *phenv)
  80. {
  81. HGLOBAL henv;
  82. LPENV lpenv;
  83. //To make guarentee Ole is initialized per thread
  84. COleInitializationManager myOleManager;
  85. /* Allocate memory for the handle */
  86. henv = GlobalAlloc (GMEM_MOVEABLE | GMEM_ZEROINIT, sizeof (ENV));
  87. if (henv == NULL || (lpenv = (LPENV)GlobalLock (henv)) == NULL) {
  88. if (henv)
  89. GlobalFree(henv);
  90. *phenv = SQL_NULL_HENV;
  91. OleUninitialize();
  92. return SQL_ERROR;
  93. }
  94. /* So far no connections on this environment */
  95. lpenv->lpdbcs = NULL;
  96. ISAMCheckTracingOption();
  97. ISAMCheckWorkingThread_AllocEnv();
  98. /* Return success */
  99. lpenv->errcode = ERR_SUCCESS;
  100. (lpenv->szISAMError)[0] = 0;
  101. *phenv = (HENV FAR *)lpenv;
  102. return SQL_SUCCESS;
  103. }
  104. /***************************************************************************/
  105. RETCODE SQL_API SQLAllocConnect(
  106. HENV henv,
  107. HDBC FAR *phdbc)
  108. {
  109. LPENV lpenv;
  110. LPDBC lpdbc;
  111. HGLOBAL hdbc;
  112. //To make guarentee Ole is initialized per thread
  113. COleInitializationManager myOleManager;
  114. /* Allocate memory for the handle */
  115. lpenv = (LPENV) henv;
  116. lpenv->errcode = ERR_SUCCESS;
  117. hdbc = GlobalAlloc (GMEM_MOVEABLE | GMEM_ZEROINIT, sizeof (DBC));
  118. if (hdbc == NULL || (lpdbc = (LPDBC)GlobalLock (hdbc)) == NULL) {
  119. if (hdbc)
  120. GlobalFree(hdbc);
  121. lpenv->errcode = ERR_MEMALLOCFAIL;
  122. *phdbc = SQL_NULL_HDBC;
  123. return SQL_ERROR;
  124. }
  125. /* Put handle on list of connection handles */
  126. lpdbc->lpNext = lpenv->lpdbcs;
  127. lpenv->lpdbcs = lpdbc;
  128. /* So far no statements for this connection */
  129. lpdbc->lpstmts = NULL;
  130. /* Rember which environment goes with this connection */
  131. lpdbc->lpenv = lpenv;
  132. /* So far no connection */
  133. (lpdbc->szDSN)[0] = 0;
  134. lpdbc->lpISAM = NULL;
  135. /* Initialize transaction information */
  136. lpdbc->fTxnIsolation = -1;
  137. lpdbc->fAutoCommitTxn = FALSE;
  138. /* Return success */
  139. lpdbc->errcode = ERR_SUCCESS;
  140. (lpdbc->szISAMError)[0] = 0;
  141. *phdbc = (HDBC FAR *) lpdbc;
  142. return SQL_SUCCESS;
  143. }
  144. /***************************************************************************/
  145. RETCODE SQL_API SQLConnect(
  146. HDBC hdbc,
  147. UCHAR FAR *szDSN,
  148. SWORD cbDSN,
  149. UCHAR FAR *szUID,
  150. SWORD cbUID,
  151. UCHAR FAR *szAuthStr,
  152. SWORD cbAuthStr)
  153. {
  154. LPDBC lpdbc;
  155. UCHAR szDatabase[MAX_DATABASE_NAME_LENGTH+1];
  156. UCHAR szUsername[MAX_USER_NAME_LENGTH+1];
  157. UCHAR szPassword[MAX_PASSWORD_LENGTH+1];
  158. SWORD err;
  159. //To make guarentee Ole is initialized per thread
  160. COleInitializationManager myOleManager;
  161. /* Get connection handle */
  162. lpdbc = (LPDBC) hdbc;
  163. lpdbc->errcode = ERR_SUCCESS;
  164. /* Error if already connected */
  165. if (s_lstrlen((char*)lpdbc->szDSN) != 0) {
  166. lpdbc->errcode = ERR_CONNECTIONINUSE;
  167. return SQL_ERROR;
  168. }
  169. /* Save name of DSN */
  170. cbDSN = (SWORD) TrueSize((LPUSTR)szDSN, cbDSN, SQL_MAX_DSN_LENGTH);
  171. _fmemcpy(lpdbc->szDSN, szDSN, cbDSN);
  172. lpdbc->szDSN[cbDSN] = '\0';
  173. /* Get user name */
  174. cbUID = (SWORD) TrueSize((LPUSTR)szUID, cbUID, MAX_USER_NAME_LENGTH);
  175. _fmemcpy(szUsername, szUID, cbUID);
  176. szUsername[cbUID] = '\0';
  177. if (s_lstrlen((char*)szUsername) == 0) {
  178. SQLGetPrivateProfileString((char*)lpdbc->szDSN, KEY_USERNAME, "",
  179. (char*)szUsername, MAX_USER_NAME_LENGTH+1, ODBC_INI);
  180. }
  181. /* Get password */
  182. cbAuthStr = (SWORD) TrueSize((LPUSTR)szAuthStr, cbAuthStr, MAX_PASSWORD_LENGTH);
  183. _fmemcpy(szPassword, szAuthStr, cbAuthStr);
  184. szPassword[cbAuthStr] = '\0';
  185. if (s_lstrlen((char*)szPassword) == 0) {
  186. SQLGetPrivateProfileString((char*)lpdbc->szDSN, KEY_PASSWORD, "",
  187. (char*)szPassword, MAX_PASSWORD_LENGTH+1, ODBC_INI);
  188. }
  189. // in this case we will open the default namespace in deep mode
  190. CMapStringToOb *pMapStringToOb = new CMapStringToOb;
  191. DWORD dwDummyValue = 0;
  192. ISAMGetNestedNamespaces (NULL, "root\\default", NULL, dwDummyValue, dwDummyValue, NULL,
  193. // WBEM_AUTHENTICATION_DEFAULT,
  194. (char*)szUsername, (char*)szPassword, FALSE, NULL, NULL, pMapStringToOb);
  195. szDatabase[0] = 0;
  196. lstrcpy ((char*)szDatabase, "root\\default");
  197. if (lpdbc->lpISAM != NULL)
  198. {
  199. ISAMClose(lpdbc->lpISAM);
  200. lpdbc->lpISAM = NULL;
  201. }
  202. err = ISAMOpen((LPUSTR)"", (LPUSTR)szDatabase, NULL,
  203. // WBEM_AUTHENTICATION_DEFAULT,
  204. (LPUSTR)szUsername, (LPUSTR)szPassword, NULL, NULL, FALSE, pMapStringToOb,
  205. &(lpdbc->lpISAM), (LPUSTR)lpdbc->szISAMError, TRUE, FALSE, FALSE, FALSE);
  206. if (err != NO_ISAM_ERR) {
  207. lpdbc->lpISAM = NULL;
  208. (lpdbc->szDSN)[0] = 0;
  209. lpdbc->errcode = err;
  210. return SQL_ERROR;
  211. }
  212. /* Initialize transaction states */
  213. lpdbc->fAutoCommitTxn = TRUE;
  214. lpdbc->fTxnIsolation = lpdbc->lpISAM->fDefaultTxnIsolation;
  215. return SQL_SUCCESS;
  216. }
  217. /***************************************************************************/
  218. RETCODE SQL_API SQLDriverConnect(
  219. HDBC hdbc,
  220. HWND hwnd,
  221. UCHAR FAR *szConnStrIn,
  222. SWORD cbConnStrIn,
  223. UCHAR FAR *szConnStrOut,
  224. SWORD cbConnStrOutMax,
  225. SWORD FAR *pcbConnStrOut,
  226. UWORD fDriverCompletion)
  227. {
  228. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  229. LPDBC lpdbc;
  230. //To make guarentee Ole is initialized per thread
  231. COleInitializationManager myOleManager;
  232. /* Get connection handle */
  233. lpdbc = (LPDBC) hdbc;
  234. lpdbc->errcode = ERR_SUCCESS;
  235. ODBCTRACE ("\nWBEM ODBC Driver : Enter SQLDriverConnect\n");
  236. CString myInputConnectionString;
  237. myInputConnectionString.Format("\nWBEM ODBC Driver :\nszConnStrIn = %s\ncbConnStrIn = %ld\n",
  238. szConnStrIn, cbConnStrIn);
  239. ODBCTRACE(myInputConnectionString);
  240. MyImpersonator im (lpdbc, "SQLDriverConnect");
  241. //Test
  242. switch (fDriverCompletion)
  243. {
  244. case SQL_DRIVER_PROMPT:
  245. ODBCTRACE("\nWBEM ODBC Driver : fDriverCompletion = SQL_DRIVER_PROMPT\n");
  246. break;
  247. case SQL_DRIVER_COMPLETE:
  248. ODBCTRACE("\nWBEM ODBC Driver : fDriverCompletion = SQL_DRIVER_COMPLETE\n");
  249. break;
  250. case SQL_DRIVER_COMPLETE_REQUIRED:
  251. ODBCTRACE("\nWBEM ODBC Driver : fDriverCompletion = SQL_DRIVER_COMPLETE_REQUIRED\n");
  252. break;
  253. case SQL_DRIVER_NOPROMPT:
  254. ODBCTRACE("\nWBEM ODBC Driver : fDriverCompletion = SQL_DRIVER_NOPROMPT\n");
  255. break;
  256. default:
  257. ODBCTRACE("\nWBEM ODBC Driver : fDriverCompletion = dunno\n");
  258. break;
  259. }
  260. /* Error if already connected */
  261. if (s_lstrlen((char*)lpdbc->szDSN) != 0) {
  262. lpdbc->errcode = ERR_CONNECTIONINUSE;
  263. return SQL_ERROR;
  264. }
  265. /* Parse the connection string */
  266. ConnectionStringManager connManager (hdbc, hwnd, szConnStrIn, fDriverCompletion);
  267. RETCODE rc = connManager.Process();
  268. if (rc != SQL_SUCCESS)
  269. return rc;
  270. char* lpszConnStr = connManager.GenerateConnString();
  271. ULONG cOutputLen = lstrlen ((char*)lpszConnStr);
  272. /* Return the connections string */
  273. if (szConnStrOut)
  274. {
  275. szConnStrOut[0] = 0;
  276. lpdbc->errcode = ReturnString(szConnStrOut, cbConnStrOutMax,
  277. pcbConnStrOut, (LPUSTR)lpszConnStr);
  278. }
  279. else
  280. {
  281. //No space to return result
  282. lpdbc->errcode = ERR_DATATRUNCATED;
  283. }
  284. if (lpdbc->errcode == ERR_DATATRUNCATED)
  285. {
  286. delete lpszConnStr;
  287. return SQL_SUCCESS_WITH_INFO;
  288. }
  289. //Tidy up
  290. delete lpszConnStr;
  291. lpdbc->szISAMError[0] = 0;
  292. CString myOutputConnectionString;
  293. myOutputConnectionString.Format("\nWBEM ODBC Driver :\nszConnStrOut = %s\ncbConnStrOutMax = %ld\n",
  294. szConnStrOut, cbConnStrOutMax);
  295. ODBCTRACE(myOutputConnectionString);
  296. ODBCTRACE ("\nWBEM ODBC Driver : Exit SQLDriverConnect\n");
  297. return SQL_SUCCESS;
  298. }
  299. ConnectionStringManager :: ConnectionStringManager(HDBC fHdbc, HWND hwind, UCHAR FAR *szConnStr, UWORD fDvrCompletion)
  300. {
  301. //Initialize
  302. lpdbc = (LPDBC) fHdbc;
  303. hwnd = hwind;
  304. fDriverCompletion = fDvrCompletion;
  305. fOptimization = TRUE; //HMM Level 1 optimization is on by default
  306. ptr = (char*)szConnStr;
  307. lpszOutputNamespaces = NULL;
  308. pMapStringToOb = new CMapStringToOb();
  309. pMapStringToObOut = new CMapStringToOb();
  310. szDSN[0] = 0;
  311. szDriver[0] = 0;
  312. szDatabase[0] = 0;
  313. szUsername[0] = 0;
  314. szPassword[0] = 0;
  315. szOptimization[0] =0;
  316. szServer[0] = 0;
  317. szHome[0] = 0;
  318. fUsernameSpecified = FALSE;
  319. fPasswordSpecified = FALSE;
  320. fServerSpecified = FALSE;
  321. fImpersonate = FALSE;
  322. fPassthroughOnly = FALSE;
  323. fIntpretEmptPwdAsBlank = FALSE;
  324. fSysProp = FALSE;
  325. // m_loginMethod = WBEM_AUTHENTICATION_DEFAULT;
  326. szLocale = NULL;
  327. szAuthority = NULL;
  328. }
  329. ConnectionStringManager :: ~ConnectionStringManager()
  330. {
  331. //Tidy up
  332. delete lpszOutputNamespaces;
  333. //Check if we have specified enough info in input connection string
  334. //if so the output pMapStringToObOut will be empty and is not used (so can be deleted)
  335. //if pMapStringToOb is not empty, then we delete pMapStringToOb
  336. int cOut = pMapStringToObOut->GetCount();
  337. BOOL fIsOutEmpty = pMapStringToObOut->IsEmpty();
  338. if ( (!fIsOutEmpty) && (cOut > 0) )
  339. {
  340. //pMapStringToObOut in use, delete pMapStringToOb
  341. int cIn = pMapStringToOb->GetCount ();
  342. BOOL fIsInEmpty = pMapStringToOb->IsEmpty();
  343. if ( (!fIsInEmpty) && (cIn > 0) )
  344. {
  345. CString key;
  346. CNamespace *pNamespace;
  347. for (POSITION pos = pMapStringToOb->GetStartPosition (); pos != NULL; )
  348. {
  349. if (pos)
  350. {
  351. pMapStringToOb->GetNextAssoc (pos, key, (CObject*&)pNamespace);
  352. delete pNamespace;
  353. }
  354. }
  355. }
  356. delete pMapStringToOb;
  357. }
  358. else
  359. {
  360. //pMapStringToObOut not in use, delete pMapStringToObOut
  361. delete pMapStringToObOut;
  362. }
  363. delete szLocale;
  364. delete szAuthority;
  365. }
  366. RETCODE ConnectionStringManager :: Process()
  367. {
  368. RETCODE rc = SQL_SUCCESS;
  369. //First Parse the connection string
  370. rc = Parse();
  371. if (rc != SQL_SUCCESS)
  372. return rc;
  373. //Complement connection string with information in ODBC.INI file
  374. GetINI();
  375. //If there is still missing information get it from user via a dialog box
  376. rc = ShowDialog();
  377. return rc;
  378. }
  379. //Parse the connection string by separating it into its attribute-keyword pairs
  380. RETCODE ConnectionStringManager :: Parse()
  381. {
  382. LPSTR lpszKeyword;
  383. LPSTR lpszValue;
  384. UCHAR chr;
  385. foundDriver = FALSE,
  386. foundDSN = FALSE;
  387. SWORD myStatuscode = NO_ISAM_ERR;
  388. while (*ptr)
  389. {
  390. /* Skip any leading white spaces */
  391. while (*ptr == ' ')
  392. ptr++;
  393. /* Point at start of next keyword */
  394. lpszKeyword = ptr;
  395. /* Find the end of the keyword */
  396. while ((*ptr != '\0') && (*ptr != '='))
  397. ptr++;
  398. /* Error if no value */
  399. if ((*ptr == '\0') || (ptr == lpszKeyword)) {
  400. lpdbc->errcode = ERR_INVALIDCONNSTR;
  401. return SQL_ERROR;
  402. }
  403. /* Put a zero terminator on the keyword */
  404. *ptr = '\0';
  405. ptr++;
  406. /* Point at start of the keyword's value */
  407. lpszValue = ptr;
  408. /* Find the end of the value */
  409. while ((*ptr != '\0') && (*ptr != ';'))
  410. ptr++;
  411. /* Put a zero terminator on the value */
  412. chr = *ptr;
  413. *ptr = '\0';
  414. /* Save the keyword */
  415. if (!lstrcmpi(lpszKeyword, KEY_DSN))
  416. {
  417. if (!foundDriver && !foundDSN)
  418. {
  419. if (lstrlen (lpszValue) < SQL_MAX_DSN_LENGTH)
  420. {
  421. lstrcpy((char*)szDSN, lpszValue);
  422. foundDSN = TRUE;
  423. }
  424. else
  425. {
  426. // shouldn't get here (DM should catch it)
  427. return SQL_ERROR;
  428. }
  429. }
  430. }
  431. else if (!lstrcmpi(lpszKeyword, KEY_DRIVER))
  432. {
  433. if (!foundDriver && !foundDSN)
  434. {
  435. if (lstrlen (lpszValue) < MAX_DRIVER_LENGTH)
  436. {
  437. foundDriver = TRUE;
  438. lstrcpy((char*)szDriver, lpszValue);
  439. }
  440. else
  441. {
  442. return SQL_ERROR;
  443. }
  444. }
  445. }
  446. else if (!lstrcmpi(lpszKeyword, KEY_DATABASE))
  447. {
  448. if (lstrlen (lpszValue) < MAX_DATABASE_NAME_LENGTH)
  449. lstrcpy ((char*)szDatabase, lpszValue);
  450. else
  451. {
  452. return SQL_ERROR;
  453. }
  454. }
  455. else if (!lstrcmpi(lpszKeyword, KEY_UIDPWDDEFINED))
  456. {
  457. fUsernameSpecified = TRUE;
  458. fPasswordSpecified = TRUE;
  459. }
  460. else if (!lstrcmpi(lpszKeyword, KEY_IMPERSONATE))
  461. {
  462. fImpersonate = TRUE;
  463. }
  464. else if (!lstrcmpi(lpszKeyword, KEY_PASSTHROUGHONLY))
  465. {
  466. fPassthroughOnly = TRUE;
  467. }
  468. /*
  469. else if (!lstrcmpi(lpszKeyword, KEY_LOGINMETHOD))
  470. {
  471. if (!lstrcmpi(lpszValue, "Default"))
  472. {
  473. m_loginMethod = WBEM_AUTHENTICATION_DEFAULT;
  474. }
  475. else if (!lstrcmpi(lpszValue, "NTLM"))
  476. {
  477. m_loginMethod = WBEM_AUTHENTICATION_NTLM;
  478. }
  479. else
  480. {
  481. return SQL_ERROR;
  482. }
  483. }
  484. */
  485. else if (!lstrcmpi(lpszKeyword, KEY_LOCALE))
  486. {
  487. //RAID 42256
  488. // int localeLen = lstrlen (lpszValue);
  489. int localeLen = _mbstrlen (lpszValue);
  490. if (szLocale)
  491. delete szLocale;
  492. szLocale = new char [localeLen + 1];
  493. szLocale[0] = 0;
  494. lstrcpy (szLocale, lpszValue);
  495. }
  496. else if (!lstrcmpi(lpszKeyword, KEY_AUTHORITY))
  497. {
  498. //RAID 42256
  499. int authLen = _mbstrlen (lpszValue);
  500. if (szAuthority)
  501. delete szAuthority;
  502. szAuthority = new char [authLen + 1];
  503. szAuthority[0] = 0;
  504. lstrcpy (szAuthority, lpszValue);
  505. }
  506. else if (!lstrcmpi(lpszKeyword, KEY_USERNAME))
  507. {
  508. fUsernameSpecified = TRUE;
  509. //RAID 42256
  510. if (_mbstrlen (lpszValue) < MAX_USER_NAME_LENGTH)
  511. lstrcpy ((char*)szUsername, lpszValue);
  512. else
  513. return SQL_ERROR;
  514. }
  515. else if (!lstrcmpi(lpszKeyword, KEY_PASSWORD))
  516. {
  517. fPasswordSpecified = TRUE;
  518. //RAID 42256
  519. if (_mbstrlen (lpszValue) < MAX_PASSWORD_LENGTH)
  520. lstrcpy ((char*)szPassword, lpszValue);
  521. else
  522. return SQL_ERROR;
  523. }
  524. else if (!lstrcmpi(lpszKeyword, KEY_INTPRET_PWD_BLK))
  525. {
  526. fIntpretEmptPwdAsBlank = TRUE;
  527. }
  528. else if (!lstrcmpi(lpszKeyword, KEY_SYSPROPS))
  529. {
  530. if ( _stricmp(lpszValue, "TRUE") == 0)
  531. {
  532. fSysProp = TRUE;
  533. }
  534. else
  535. {
  536. fSysProp = FALSE;
  537. }
  538. }
  539. else if (!lstrcmpi(lpszKeyword, KEY_OPTIMIZATION))
  540. {
  541. if (lstrlen (lpszValue) < MAX_OPTIMIZATION_LENGTH)
  542. {
  543. lstrcpy ((char*)szOptimization, lpszValue);
  544. //Check this value to update optimzation state
  545. if ( (strcmp(lpszValue, "OFF") == 0) ||
  546. (strcmp(lpszValue, "FALSE") == 0) ||
  547. (strcmp(lpszValue, "0") == 0) ||
  548. (strcmp(lpszValue, "NO") == 0))
  549. {
  550. fOptimization = FALSE;
  551. }
  552. }
  553. }
  554. else if (!lstrcmpi(lpszKeyword, KEY_NAMESPACES))
  555. {
  556. // make copy of comma separate list of namespaces
  557. // pospone parsing of namespaces until the end
  558. // as this is affected by the values of UID, PWD and SERVER
  559. //RAID 42256
  560. lpszOutputNamespaces = new char [_mbstrlen (lpszValue) + 1];
  561. lpszOutputNamespaces[0] = 0;
  562. lstrcpy (lpszOutputNamespaces, lpszValue);
  563. }
  564. else if (!lstrcmpi(lpszKeyword, KEY_SERVER))
  565. {
  566. fServerSpecified = TRUE;
  567. if (_mbstrlen (lpszValue) < MAX_SERVER_NAME_LENGTH)
  568. lstrcpy ((char*)szServer, lpszValue);
  569. else
  570. return SQL_ERROR;
  571. }
  572. else if (!lstrcmpi(lpszKeyword, KEY_HOME))
  573. {
  574. //RAID 42256
  575. if (_mbstrlen (lpszValue) < MAX_HOME_NAME_LENGTH)
  576. lstrcpy ((char*)szHome, lpszValue);
  577. else
  578. return SQL_ERROR;
  579. }
  580. /* Restore the input string */
  581. lpszKeyword[lstrlen(lpszKeyword)] = '=';
  582. *ptr = chr;
  583. if (*ptr != '\0')
  584. ptr++;
  585. }
  586. /****** TEST ******/
  587. // fImpersonate = TRUE;
  588. //Now it is time to parse the namespaces, if any where specified
  589. char *lpszRemainder = lpszOutputNamespaces;
  590. //RAID 42256
  591. SDWORD cbRemainder = lpszOutputNamespaces ? _mbstrlen (lpszOutputNamespaces) : 0;
  592. LPSTR lpszToken = NULL;
  593. UWORD fType;
  594. while (lpszOutputNamespaces)
  595. {
  596. if (ReadInNamespace (lpszRemainder, cbRemainder, &lpszToken, &fType,
  597. &lpszRemainder, &cbRemainder) && fType != TYPE_NONE)
  598. {
  599. if (fType == TYPE_SIMPLE_IDENTIFIER)
  600. {
  601. /* Create a CNamespace and add it to the namespaceMap*/
  602. //However, before we do so check if HOME has been defined
  603. //If so, we assume the NAMESPACES list contain relative names
  604. //We need to convert them to fully qualified names by prepending
  605. //the HOME pathname to them
  606. SWORD cbHomeLen = (szHome ? strlen((char*)szHome) : 0);
  607. //RAID 42256
  608. SWORD cbTokenLen = (SWORD) _mbstrlen(lpszToken);
  609. if (cbHomeLen)
  610. {
  611. char* lpTempStr = new char [cbHomeLen + cbTokenLen + 2];
  612. lpTempStr[0] = 0;
  613. sprintf(lpTempStr, "%s\\%s", (char*)szHome, lpszToken);
  614. DWORD dwDummyValue = 0;
  615. myStatuscode = ISAMGetNestedNamespaces (NULL, lpTempStr, NULL, dwDummyValue, dwDummyValue, (char*) szServer,
  616. // m_loginMethod,
  617. (char*)szUsername,
  618. (char*)szPassword, fIntpretEmptPwdAsBlank, szLocale, szAuthority, pMapStringToOb, FALSE);
  619. delete lpTempStr;
  620. if (myStatuscode != NO_ISAM_ERR)
  621. return SQL_ERROR;
  622. }
  623. else
  624. {
  625. DWORD dwDummyValue = 0;
  626. myStatuscode = ISAMGetNestedNamespaces (NULL, lpszToken, NULL, dwDummyValue, dwDummyValue, (char*) szServer,
  627. // m_loginMethod,
  628. (char*)szUsername, (char*)szPassword, fIntpretEmptPwdAsBlank, szLocale, szAuthority, pMapStringToOb, FALSE);
  629. if (myStatuscode != NO_ISAM_ERR)
  630. return SQL_ERROR;
  631. }
  632. delete lpszToken;
  633. lpszToken = NULL;
  634. }
  635. else
  636. { // TYPE_BRACE -> isolate the name and the deep flag
  637. char *name = strtok (lpszToken, ", ");
  638. char *deep = strtok (NULL, ", ");
  639. BOOL bDeep = FALSE;
  640. //However, before we do so check if HOME has been defined
  641. //If so, we assume the NAMESPACES list contain relative names
  642. //We need to convert them to fully qualified names by prepending
  643. //the HOME pathname to them
  644. //RAID 42256
  645. SWORD cbHomeLen = (szHome ? _mbstrlen((char*)szHome) : 0);
  646. SWORD cbTokenLen = (SWORD) _mbstrlen(name);
  647. BOOL fIsDeepMode = (deep && !_strcmpi (deep, "deep")) ? TRUE : FALSE;
  648. if (cbHomeLen)
  649. {
  650. char* lpTempStr = new char [cbHomeLen + cbTokenLen + 2];
  651. lpTempStr[0] = 0;
  652. sprintf(lpTempStr, "%s\\%s", (char*)szHome, name);
  653. DWORD dwDummyValue = 0;
  654. myStatuscode = ISAMGetNestedNamespaces (NULL, lpTempStr, NULL, dwDummyValue, dwDummyValue, (char*)szServer,
  655. // m_loginMethod,
  656. (char*)szUsername, (char*)szPassword, fIntpretEmptPwdAsBlank, szLocale, szAuthority, pMapStringToOb, fIsDeepMode);
  657. delete lpTempStr;
  658. if (myStatuscode != NO_ISAM_ERR)
  659. return SQL_ERROR;
  660. }
  661. else
  662. {
  663. DWORD dwDummyValue = 0;
  664. myStatuscode = ISAMGetNestedNamespaces (NULL, name, NULL, dwDummyValue, dwDummyValue, (char*)szServer,
  665. // m_loginMethod,
  666. (char*)szUsername, (char*)szPassword, fIntpretEmptPwdAsBlank, szLocale, szAuthority, pMapStringToOb, fIsDeepMode);
  667. if (myStatuscode != NO_ISAM_ERR)
  668. return SQL_ERROR;
  669. }
  670. delete lpszToken;
  671. lpszToken = NULL;
  672. }
  673. }
  674. else
  675. break;
  676. }
  677. return SQL_SUCCESS;
  678. }
  679. void ConnectionStringManager :: GetINI()
  680. {
  681. if (foundDSN)
  682. {
  683. /* Use the registry to compliment the information in the conn string */
  684. /* Get the database name from ODBC.INI file if not specified */
  685. if (lstrlen((char*)szDatabase) == 0) {
  686. if (lstrlen((char*)szDSN) > 0) {
  687. SQLGetPrivateProfileString((char*)szDSN, KEY_DATABASE, ".",
  688. (char*)szDatabase, MAX_DATABASE_NAME_LENGTH+1, ODBC_INI);
  689. }
  690. }
  691. /* Get the username from ODBC.INI file if not specified */
  692. if (lstrlen((char*)szUsername) == 0) {
  693. if (lstrlen((char*)szDSN) > 0) {
  694. SQLGetPrivateProfileString((char*)szDSN, KEY_USERNAME, "",
  695. (char*)szUsername, MAX_USER_NAME_LENGTH+1, ODBC_INI);
  696. }
  697. }
  698. /* Get the password from ODBC.INI file if not specified */
  699. if (lstrlen((char*)szPassword) == 0) {
  700. if (lstrlen((char*)szDSN) > 0) {
  701. SQLGetPrivateProfileString((char*)szDSN, KEY_PASSWORD, "",
  702. (char*)szPassword, MAX_PASSWORD_LENGTH+1, ODBC_INI);
  703. }
  704. }
  705. }
  706. }
  707. RETCODE ConnectionStringManager :: ShowDialog()
  708. {
  709. ODBCTRACE ("\nWBEM ODBC Driver : ConnectionStringManager :: ShowDialog\n");
  710. /* Get missing information from from user */
  711. SWORD err = ISAM_ERROR;
  712. //Check for no prompt
  713. if (fDriverCompletion == SQL_DRIVER_NOPROMPT)
  714. {
  715. if ((lstrlen ((char*)szDatabase) == 0) ||
  716. (pMapStringToOb->GetCount() == 0) ||
  717. (NO_ISAM_ERR != (err = ISAMOpen((LPUSTR)szServer,(LPUSTR)szDatabase, NULL,
  718. // m_loginMethod,
  719. (LPUSTR)szUsername, (LPUSTR)szPassword, (LPUSTR)szLocale, (LPUSTR)szAuthority, fSysProp, pMapStringToOb,
  720. &(lpdbc->lpISAM), (LPUSTR)lpdbc->szISAMError, fOptimization, fImpersonate, fPassthroughOnly, fIntpretEmptPwdAsBlank))))
  721. {
  722. lstrcpy((char*)lpdbc->szISAMError, "");
  723. LoadString(s_hModule, ERR_UNABLETOCONNECT, (char*)lpdbc->szISAMError,
  724. MAX_ERROR_LENGTH+1);
  725. lpdbc->lpISAM = NULL;
  726. lpdbc->errcode = err;
  727. return SQL_ERROR;
  728. }
  729. }
  730. else if ((fDriverCompletion == SQL_DRIVER_PROMPT) ||
  731. (!fUsernameSpecified) ||
  732. (lstrlen ((char*)szDatabase) == 0) ||
  733. (!fPasswordSpecified) ||
  734. (pMapStringToOb->GetCount() == 0) ||
  735. ((NO_ISAM_ERR != (err = ISAMOpen((LPUSTR)szServer,(LPUSTR)szDatabase, NULL,
  736. // m_loginMethod,
  737. (LPUSTR)szUsername, (LPUSTR)szPassword, (LPUSTR)szLocale, (LPUSTR)szAuthority, fSysProp, pMapStringToOb,
  738. &(lpdbc->lpISAM), (LPUSTR)lpdbc->szISAMError, fOptimization, fImpersonate, fPassthroughOnly, fIntpretEmptPwdAsBlank))) &&
  739. (fDriverCompletion != SQL_DRIVER_NOPROMPT)))
  740. {
  741. CWnd parentWnd;
  742. parentWnd.Attach (hwnd);
  743. //Clear out namespace list as this will be replaced
  744. //the CConnectionDialog will return a fully qualified namespace list
  745. //so we can clear out the HOME pathname
  746. delete lpszOutputNamespaces;
  747. szHome[0] =0;
  748. lpszOutputNamespaces = NULL;
  749. //Check if server name has been specified
  750. BOOL fConnParmSpecified = (fServerSpecified && fPasswordSpecified && fUsernameSpecified) ? TRUE : FALSE;
  751. // BOOL fConnParmSpecified = (fServerSpecified) ? TRUE : FALSE;
  752. CConnectionDialog connectionDialog (&parentWnd, (char*)szServer,
  753. // m_loginMethod,
  754. (char*)szUsername, (char*)szPassword,
  755. &szLocale, &szAuthority, &fSysProp, fConnParmSpecified,
  756. pMapStringToOb, pMapStringToObOut,
  757. (fDriverCompletion == SQL_DRIVER_COMPLETE_REQUIRED),
  758. &lpszOutputNamespaces, &fImpersonate, &fPassthroughOnly, &fIntpretEmptPwdAsBlank);
  759. connectionDialog.DoModal ();
  760. /*
  761. //Update locale and authority
  762. if (szLocale)
  763. delete szLocale;
  764. if (szAuthority)
  765. delete szAuthority;
  766. //these values are deleted in destructor
  767. szLocale = connectionDialog.GetLocale();
  768. szAuthority = connectionDialog.GetAuthority();
  769. */
  770. //Test
  771. CString myText("\nWBEM ODBC Driver : fIntpretEmptPwdAsBlank = ");
  772. if (fIntpretEmptPwdAsBlank)
  773. {
  774. myText += "BLANK\n";
  775. }
  776. else
  777. {
  778. myText += "NULL\n";
  779. }
  780. ODBCTRACE(myText);
  781. parentWnd.Detach ();
  782. }
  783. /* Analyse dialog box output */
  784. if (err != NO_ISAM_ERR)
  785. {
  786. ImpersonationManager* tmp = NULL;
  787. if (fImpersonate)
  788. {
  789. //Now check if impersonation is necessary
  790. //only if connecting locally
  791. if (IsLocalServer((char*)szServer))
  792. {
  793. tmp = new ImpersonationManager((char*)szUsername, (char*)szPassword, (char*)szAuthority);
  794. }
  795. else
  796. {
  797. ODBCTRACE("\nWBEM ODBC Driver : Server not detected as local, not impersonating\n");
  798. }
  799. }
  800. //If szDatabase was previously specified, check if it
  801. //is still in the namespace list
  802. BOOL fUseOldszDatabase = FALSE;
  803. if ( lstrlen((char*)szDatabase) )
  804. {
  805. //Search namespace list for old szDatabase
  806. CString key;
  807. CNamespace *pNamespace;
  808. for (POSITION pos = pMapStringToObOut->GetStartPosition (); (!fUseOldszDatabase) && (pos != NULL); )
  809. {
  810. if (pos)
  811. {
  812. pMapStringToObOut->GetNextAssoc (pos, key, (CObject*&)pNamespace);
  813. int len = (pNamespace->GetName ()).GetLength ();
  814. if (len <= MAX_DATABASE_NAME_LENGTH)
  815. {
  816. LPTSTR str = (pNamespace->GetName ()).GetBuffer (len);
  817. if (strcmp((char*)szDatabase, str) == 0)
  818. {
  819. //Found original szDatabase
  820. fUseOldszDatabase = TRUE;
  821. err = ISAMOpen((LPUSTR)szServer,(LPUSTR)szDatabase, NULL,
  822. // m_loginMethod,
  823. (LPUSTR)szUsername, (LPUSTR)szPassword, (LPUSTR)szLocale, (LPUSTR)szAuthority, fSysProp, pMapStringToObOut,
  824. &(lpdbc->lpISAM), (LPUSTR)lpdbc->szISAMError, fOptimization, fImpersonate, fPassthroughOnly, fIntpretEmptPwdAsBlank);
  825. if (err != NO_ISAM_ERR)
  826. {
  827. lpdbc->lpISAM = NULL;
  828. lpdbc->errcode = err;
  829. delete tmp;
  830. return SQL_ERROR;
  831. }
  832. delete tmp;
  833. return SQL_SUCCESS;
  834. }
  835. }
  836. }
  837. }
  838. }
  839. // make szDatabase = to first namespace on list for now
  840. if ((!fUseOldszDatabase) && pMapStringToObOut->GetCount ())
  841. {
  842. CString key;
  843. CNamespace *pNamespace;
  844. POSITION pos = pMapStringToObOut->GetStartPosition ();
  845. if (pos)
  846. {
  847. pMapStringToObOut->GetNextAssoc (pos, key, (CObject*&)pNamespace);
  848. int len = (pNamespace->GetName ()).GetLength ();
  849. if (len <= MAX_DATABASE_NAME_LENGTH)
  850. {
  851. LPTSTR str = (pNamespace->GetName ()).GetBuffer (len);
  852. szDatabase[0] = 0;
  853. lstrcpy ((char*)szDatabase, str);
  854. if (lpdbc->lpISAM != NULL)
  855. {
  856. ISAMClose(lpdbc->lpISAM);
  857. lpdbc->lpISAM = NULL;
  858. }
  859. err = ISAMOpen((LPUSTR)szServer,(LPUSTR)szDatabase, NULL,
  860. // m_loginMethod,
  861. (LPUSTR)szUsername, (LPUSTR)szPassword, (LPUSTR)szLocale, (LPUSTR)szAuthority, fSysProp, pMapStringToObOut,
  862. &(lpdbc->lpISAM), (LPUSTR)lpdbc->szISAMError, fOptimization, fImpersonate, fPassthroughOnly, fIntpretEmptPwdAsBlank);
  863. }
  864. else
  865. err = ISAM_NS_OVERMAX;
  866. }
  867. else
  868. err = ISAM_NS_LISTFAIL;
  869. }
  870. else
  871. {
  872. //Canceled out of the dialog box, so quit
  873. delete tmp;
  874. return SQL_NO_DATA_FOUND;
  875. }
  876. delete tmp;
  877. }
  878. if (err != NO_ISAM_ERR)
  879. {
  880. lpdbc->lpISAM = NULL;
  881. lpdbc->errcode = err;
  882. return SQL_ERROR;
  883. }
  884. return SQL_SUCCESS;
  885. }
  886. char* ConnectionStringManager :: GenerateConnString()
  887. {
  888. ULONG CLenConnStr = MAX_DRIVER_LENGTH + MAX_DATABASE_NAME_LENGTH +
  889. MAX_USER_NAME_LENGTH + MAX_PASSWORD_LENGTH +
  890. MAX_BOOLFLAG_LENGTH +
  891. MAX_KEYWORD_SEPARATOR_LENGTH + /* combined length of kewords + separators */
  892. MAX_SERVER_NAME_LENGTH + MAX_OPTIMIZATION_LENGTH + MAX_LOGIN_METHOD_LENGTH + strlen (KEY_NAMESPACES);
  893. //Check locale and authority
  894. if (szLocale && lstrlen(szLocale))
  895. {
  896. CLenConnStr = CLenConnStr + 8 + lstrlen(szLocale);
  897. }
  898. if (szAuthority && lstrlen(szAuthority))
  899. {
  900. CLenConnStr = CLenConnStr + 11 + lstrlen(szAuthority);
  901. }
  902. //RAID 42256
  903. if (lpszOutputNamespaces)
  904. CLenConnStr += _mbstrlen (lpszOutputNamespaces);
  905. char *lpszConnStr = new char [CLenConnStr + 1];
  906. lpszConnStr[0] = 0;
  907. /* Put the datasource or driver name on the connection string */
  908. (lpdbc->szDSN)[0] = 0;
  909. if (lstrlen((char*)szDSN) != 0)
  910. {
  911. lstrcpy((char*)lpdbc->szDSN, (char*)szDSN);
  912. lstrcpy((char*)lpszConnStr, KEY_DSN);
  913. lstrcat((char*)lpszConnStr, "=");
  914. lstrcat((char*)lpszConnStr, (char*)szDSN);
  915. }
  916. else {
  917. lstrcpy((char*)lpdbc->szDSN, " ");
  918. lstrcpy((char*)lpszConnStr, KEY_DRIVER);
  919. lstrcat((char*)lpszConnStr, "=");
  920. lstrcat((char*)lpszConnStr, (char*)szDriver);
  921. }
  922. /* Put the database name on the connection string */
  923. if (lstrlen((char*)szDatabase) != 0) {
  924. lstrcat((char*)lpszConnStr, ";");
  925. lstrcat((char*)lpszConnStr, KEY_DATABASE);
  926. lstrcat((char*)lpszConnStr, "=");
  927. lstrcat((char*)lpszConnStr, (char*)szDatabase);
  928. }
  929. /* Put the server on the connection string */
  930. // if (lstrlen((char*)szServer) != 0)
  931. {
  932. lstrcat((char*)lpszConnStr, ";");
  933. lstrcat((char*)lpszConnStr, KEY_SERVER);
  934. lstrcat((char*)lpszConnStr, "=");
  935. lstrcat((char*)lpszConnStr, (char*)szServer);
  936. }
  937. /* Put the home on the connection string */
  938. if (lstrlen((char*)szHome) != 0)
  939. {
  940. lstrcat((char*)lpszConnStr, ";");
  941. lstrcat((char*)lpszConnStr, KEY_HOME);
  942. lstrcat((char*)lpszConnStr, "=");
  943. lstrcat((char*)lpszConnStr, (char*)szHome);
  944. }
  945. /*
  946. // Put the login method on the connection string
  947. lstrcat((char*)lpszConnStr, ";");
  948. lstrcat((char*)lpszConnStr, KEY_LOGINMETHOD);
  949. lstrcat((char*)lpszConnStr, "=");
  950. switch (m_loginMethod)
  951. {
  952. case WBEM_AUTHENTICATION_DEFAULT:
  953. lstrcat((char*)lpszConnStr, (char*)"Default");
  954. break;
  955. case WBEM_AUTHENTICATION_NTLM:
  956. lstrcat((char*)lpszConnStr, (char*)"NTLM");
  957. break;
  958. default:
  959. lstrcat((char*)lpszConnStr, (char*)"WBEM");
  960. break;
  961. }
  962. */
  963. /* put flag to indicate user name and password defined */
  964. {
  965. lstrcat((char*)lpszConnStr, ";");
  966. lstrcat((char*)lpszConnStr, KEY_UIDPWDDEFINED);
  967. lstrcat((char*)lpszConnStr, "=");
  968. }
  969. /* put flag to indicate if impersonation is requested */
  970. if (fImpersonate)
  971. {
  972. lstrcat((char*)lpszConnStr, ";");
  973. lstrcat((char*)lpszConnStr, KEY_IMPERSONATE);
  974. lstrcat((char*)lpszConnStr, "=");
  975. }
  976. /* put flag to indicate if passthrough only mode is requested */
  977. if (fPassthroughOnly)
  978. {
  979. lstrcat((char*)lpszConnStr, ";");
  980. lstrcat((char*)lpszConnStr, KEY_PASSTHROUGHONLY);
  981. lstrcat((char*)lpszConnStr, "=");
  982. }
  983. /* Put the user name on the connection string */
  984. // if (lstrlen ((char*)szUsername) != 0)
  985. {
  986. lstrcat((char*)lpszConnStr, ";");
  987. lstrcat((char*)lpszConnStr, KEY_USERNAME);
  988. lstrcat((char*)lpszConnStr, "=");
  989. lstrcat((char*)lpszConnStr, (char*)szUsername);
  990. }
  991. /* Put the password on the connection string */
  992. // if (lstrlen((char*)szPassword) != 0)
  993. {
  994. lstrcat((char*)lpszConnStr, ";");
  995. lstrcat((char*)lpszConnStr, KEY_PASSWORD);
  996. lstrcat((char*)lpszConnStr, "=");
  997. lstrcat((char*)lpszConnStr, (char*)szPassword);
  998. }
  999. /* put flag to indicate if you interpet an empty password as blank */
  1000. if (fIntpretEmptPwdAsBlank)
  1001. {
  1002. lstrcat((char*)lpszConnStr, ";");
  1003. lstrcat((char*)lpszConnStr, KEY_INTPRET_PWD_BLK);
  1004. lstrcat((char*)lpszConnStr, "=");
  1005. }
  1006. /* Put the locale on the connection string */
  1007. if (szLocale && lstrlen(szLocale))
  1008. {
  1009. lstrcat((char*)lpszConnStr, ";");
  1010. lstrcat((char*)lpszConnStr, KEY_LOCALE);
  1011. lstrcat((char*)lpszConnStr, "=");
  1012. lstrcat((char*)lpszConnStr, (char*)szLocale);
  1013. }
  1014. /* Put the authority on the connection string */
  1015. if (szAuthority && lstrlen(szAuthority))
  1016. {
  1017. lstrcat((char*)lpszConnStr, ";");
  1018. lstrcat((char*)lpszConnStr, KEY_AUTHORITY);
  1019. lstrcat((char*)lpszConnStr, "=");
  1020. lstrcat((char*)lpszConnStr, (char*)szAuthority);
  1021. }
  1022. /* Put the system properties flag on the connection string */
  1023. lstrcat((char*)lpszConnStr, ";");
  1024. lstrcat((char*)lpszConnStr, KEY_SYSPROPS);
  1025. lstrcat((char*)lpszConnStr, "=");
  1026. if (fSysProp)
  1027. {
  1028. lstrcat((char*)lpszConnStr, (char*)"TRUE");
  1029. }
  1030. else
  1031. {
  1032. lstrcat((char*)lpszConnStr, (char*)"FALSE");
  1033. }
  1034. /* Put the namespaces into the output connection string */
  1035. //RAID 42256
  1036. if (lpszOutputNamespaces && _mbstrlen((char*)lpszOutputNamespaces) != 0) {
  1037. lstrcat((char*)lpszConnStr, ";");
  1038. lstrcat((char*)lpszConnStr, KEY_NAMESPACES);
  1039. lstrcat((char*)lpszConnStr, "=");
  1040. lstrcat((char*)lpszConnStr, (char*)lpszOutputNamespaces);
  1041. }
  1042. return lpszConnStr;
  1043. }
  1044. /***************************************************************************/
  1045. RETCODE SQL_API SQLBrowseConnect(
  1046. HDBC hdbc,
  1047. UCHAR FAR *szConnStrIn,
  1048. SWORD cbConnStrIn,
  1049. UCHAR FAR *szConnStrOut,
  1050. SWORD cbConnStrOutMax,
  1051. SWORD FAR *pcbConnStrOut)
  1052. {
  1053. LPDBC lpdbc;
  1054. lpdbc = (LPDBC) hdbc;
  1055. lpdbc->errcode = ERR_NOTSUPPORTED;
  1056. return SQL_ERROR;
  1057. }
  1058. /***************************************************************************/
  1059. RETCODE SQL_API SQLDisconnect(
  1060. HDBC hdbc)
  1061. {
  1062. LPDBC lpdbc;
  1063. LPSTMT lpstmt;
  1064. RETCODE rc;
  1065. /* Get connection handle */
  1066. lpdbc = (LPDBC) hdbc;
  1067. lpdbc->errcode = ERR_SUCCESS;
  1068. //To make guarentee Ole is initialized per thread
  1069. COleInitializationManager myOleManager;
  1070. MyImpersonator im(lpdbc, "SQLDisconnect");
  1071. /* If transaction in progress, fail */
  1072. if (lpdbc->lpISAM->fTxnCapable != SQL_TC_NONE) {
  1073. for (lpstmt = lpdbc->lpstmts; lpstmt!=NULL ;lpstmt = lpstmt->lpNext) {
  1074. if (lpstmt->fISAMTxnStarted) {
  1075. lpdbc->errcode = ERR_TXNINPROGRESS;
  1076. return SQL_ERROR;
  1077. }
  1078. }
  1079. }
  1080. /* Close all active statements */
  1081. lpstmt = lpdbc->lpstmts;
  1082. while (lpstmt != NULL) {
  1083. rc = SQLFreeStmt((HSTMT) lpstmt, SQL_CLOSE);
  1084. if ((rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO)) {
  1085. lpdbc->errcode = lpstmt->errcode;
  1086. s_lstrcpy(lpdbc->szISAMError, lpstmt->szISAMError);
  1087. return rc;
  1088. }
  1089. lpstmt = lpstmt->lpNext;
  1090. }
  1091. /* Disconnect */
  1092. (lpdbc->szDSN)[0] = 0;
  1093. return SQL_SUCCESS;
  1094. }
  1095. /***************************************************************************/
  1096. RETCODE SQL_API SQLFreeConnect(
  1097. HDBC hdbc)
  1098. {
  1099. LPDBC lpdbc;
  1100. RETCODE rc;
  1101. LPDBC lpdbcPrev;
  1102. /* Get connection handle */
  1103. lpdbc = (LPDBC) hdbc;
  1104. lpdbc->errcode = ERR_SUCCESS;
  1105. //To make guarentee Ole is initialized per thread
  1106. COleInitializationManager myOleManager;
  1107. // ODBCTRACE(_T("\nWBEM ODBC Driver : SQLFreeConnect\n"));
  1108. // MyImpersonator im (lpdbc);
  1109. /* Deallocate all statements */
  1110. while (lpdbc->lpstmts != NULL) {
  1111. rc = SQLFreeStmt((HSTMT) lpdbc->lpstmts, SQL_DROP);
  1112. if ((rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO)) {
  1113. lpdbc->errcode = lpdbc->lpstmts->errcode;
  1114. s_lstrcpy((char*)lpdbc->szISAMError, (char*)lpdbc->lpstmts->szISAMError);
  1115. return rc;
  1116. }
  1117. }
  1118. /* Close ISAM */
  1119. if (lpdbc->lpISAM != NULL)
  1120. {
  1121. ISAMClose(lpdbc->lpISAM);
  1122. lpdbc->lpISAM = NULL;
  1123. }
  1124. /* Take connection off the list */
  1125. if (lpdbc->lpenv->lpdbcs == lpdbc) {
  1126. lpdbc->lpenv->lpdbcs = lpdbc->lpNext;
  1127. }
  1128. else {
  1129. lpdbcPrev = lpdbc->lpenv->lpdbcs;
  1130. while (lpdbcPrev->lpNext != lpdbc)
  1131. lpdbcPrev = lpdbcPrev->lpNext;
  1132. lpdbcPrev->lpNext = lpdbc->lpNext;
  1133. }
  1134. /* Free the memory */
  1135. GlobalUnlock (GlobalPtrHandle(hdbc));
  1136. GlobalFree (GlobalPtrHandle(hdbc));
  1137. ODBCTRACE("\n****** EXIT SQLFreeConnect ******\n");
  1138. return SQL_SUCCESS;
  1139. }
  1140. /***************************************************************************/
  1141. RETCODE SQL_API SQLFreeEnv(
  1142. HENV henv)
  1143. {
  1144. LPENV lpenv;
  1145. RETCODE rc;
  1146. //To make guarentee Ole is initialized per thread
  1147. COleInitializationManager myOleManager;
  1148. /* Get environment handle */
  1149. lpenv = (LPENV) henv;
  1150. lpenv->errcode = ERR_SUCCESS;
  1151. /* Deallocate all connections */
  1152. while (lpenv->lpdbcs != NULL) {
  1153. rc = SQLFreeConnect((HDBC) lpenv->lpdbcs);
  1154. if ((rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO)) {
  1155. lpenv->errcode = lpenv->lpdbcs->errcode;
  1156. s_lstrcpy((char*)lpenv->szISAMError, (char*)lpenv->lpdbcs->szISAMError);
  1157. return rc;
  1158. }
  1159. }
  1160. //decrement reference count on working thread
  1161. ISAMCheckWorkingThread_FreeEnv();
  1162. /* Free the memory */
  1163. GlobalUnlock (GlobalPtrHandle(henv));
  1164. GlobalFree (GlobalPtrHandle(henv));
  1165. return SQL_SUCCESS;
  1166. }
  1167. /***************************************************************************/
  1168. /////
  1169. BOOL INTFUNC ReadInNamespace (LPSTR lpFrom, SDWORD cbFrom, LPSTR *lpTo,
  1170. UWORD FAR *pfType, LPSTR FAR *lpRemainder,
  1171. SDWORD FAR *pcbRemainder)
  1172. // Retrives a token from input string, and returns the token and a pointer
  1173. // to the remainder of the input string. This makes no assumption about the length
  1174. // of the string (including whitespace) between the braces.
  1175. {
  1176. int len;
  1177. // Remove leading blanks & ,'s
  1178. while ((cbFrom != 0) &&
  1179. ((*lpFrom == ' ') ||
  1180. (*lpFrom == '\012') ||
  1181. (*lpFrom == '\015') ||
  1182. (*lpFrom == '\011') ||
  1183. (*lpFrom == ','))) {
  1184. lpFrom++;
  1185. cbFrom--;
  1186. }
  1187. // Leave if no more
  1188. if (cbFrom == 0) {
  1189. *lpTo = NULL;
  1190. *lpRemainder = lpFrom;
  1191. *pcbRemainder = cbFrom;
  1192. *pfType = TYPE_NONE;
  1193. return TRUE;
  1194. }
  1195. // What kind of token?
  1196. switch (*lpFrom) {
  1197. // End of input
  1198. case '\0':
  1199. *lpTo = NULL;
  1200. *lpRemainder = lpFrom;
  1201. *pcbRemainder = cbFrom;
  1202. *pfType = TYPE_NONE;
  1203. return TRUE;
  1204. // Braced identifier (of form {identifier})
  1205. case '{':
  1206. len = 0;
  1207. lpFrom++;
  1208. cbFrom--;
  1209. while (TRUE) {
  1210. if (cbFrom == 0)
  1211. return FALSE;
  1212. switch (*lpFrom) {
  1213. case '\0':
  1214. return FALSE;
  1215. case '}':
  1216. {
  1217. *lpTo = new char [len+1];
  1218. int i = 0;
  1219. while (i < len)
  1220. {
  1221. *(*lpTo+i) = *(lpFrom-len+i);
  1222. i++;
  1223. }
  1224. *(*lpTo+i) = '\0';
  1225. lpFrom++;
  1226. cbFrom--;
  1227. *lpRemainder = lpFrom;
  1228. *pcbRemainder = cbFrom;
  1229. *pfType = TYPE_BRACE;
  1230. return TRUE;
  1231. }
  1232. default:
  1233. break;
  1234. }
  1235. len++;
  1236. lpFrom++;
  1237. cbFrom--;
  1238. }
  1239. break; // Control should never get here
  1240. // a simple identifier
  1241. default:
  1242. len = 0;
  1243. while (TRUE) {
  1244. if (cbFrom == 0) {
  1245. *lpTo = new char [len + 1];
  1246. int i = 0;
  1247. while (i < len)
  1248. {
  1249. *(*lpTo+i) = *(lpFrom-len+i);
  1250. i++;
  1251. }
  1252. *(*lpTo+i) = '\0';
  1253. *lpRemainder = lpFrom;
  1254. *pcbRemainder = cbFrom;
  1255. *pfType = TYPE_SIMPLE_IDENTIFIER;
  1256. return TRUE;
  1257. }
  1258. switch (*lpFrom) {
  1259. case ' ':
  1260. case '\012':
  1261. case '\015':
  1262. case '\011':
  1263. case '\0':
  1264. case ',':
  1265. {
  1266. int i = 0;
  1267. //Bug fix Micks code
  1268. if (! (*lpTo) )
  1269. {
  1270. *lpTo = new char [len + 1];
  1271. }
  1272. while (i < len)
  1273. {
  1274. *(*lpTo+i) = *(lpFrom-len+i);
  1275. i++;
  1276. }
  1277. *(*lpTo+i) = '\0';
  1278. *lpRemainder = lpFrom;
  1279. *pcbRemainder = cbFrom;
  1280. *pfType = TYPE_SIMPLE_IDENTIFIER;
  1281. return TRUE;
  1282. }
  1283. default:
  1284. break;
  1285. }
  1286. len++;
  1287. lpFrom++;
  1288. cbFrom--;
  1289. }
  1290. break; // Control should never get here
  1291. }
  1292. // Control should never get here
  1293. return FALSE;
  1294. }