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.

1042 lines
26 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Copyright (c) 1997-1999 Microsoft Corporation
  4. //
  5. // File: upg.cpp
  6. //
  7. // Contents:
  8. //
  9. // History:
  10. //
  11. //---------------------------------------------------------------------------
  12. #include "upg.h"
  13. #include <time.h>
  14. //----------------------------------------------------
  15. //
  16. // Global variables
  17. //
  18. //
  19. #ifndef LICENOC_SMALL_UPG
  20. JBInstance g_JbInstance;
  21. JBSession g_JetSession(g_JbInstance);
  22. JBDatabase g_JetDatabase(g_JetSession);
  23. PBYTE g_pbSetupId=NULL;
  24. DWORD g_cbSetupId=0;
  25. PBYTE g_pbDomainSid=NULL;
  26. DWORD g_cbDomainSid=0;
  27. TCHAR g_szComputerName[MAX_COMPUTERNAME_LENGTH + 1];
  28. #endif
  29. TCHAR g_szOdbcDsn[128]=NT4LSERVER_DEFAULT_DSN; // ODBC DSN
  30. TCHAR g_szOdbcUser[128]=NT4LSERVER_DEFAULT_USER; // ODBC User Name
  31. TCHAR g_szOdbcPwd[128]=NT4LSERVER_DEFAULT_PWD; // ODBC Password
  32. TCHAR g_szMdbFile[MAX_PATH+1];
  33. //--------------------------------------------------------------------------
  34. #ifndef LICENOC_SMALL_UPG
  35. DWORD
  36. UpgradeIssuedLicenseTable(
  37. CSQLStmt* lpSqlStmt,
  38. JBDatabase& jbDatabase
  39. )
  40. /*++
  41. Note:
  42. Transaction must be active
  43. ++*/
  44. {
  45. DWORD dwStatus = ERROR_SUCCESS;
  46. NT4LICENSE nt4License;
  47. NT4LICENSERECORD nt4LicenseRecord(&nt4License);
  48. LICENSEDCLIENT LicensedClient;
  49. LicensedTable LicensedTable(jbDatabase);
  50. TLSLicensedIndexMatchHwid hwidHint;
  51. BOOL bSuccess;
  52. bSuccess = LicensedTable.OpenTable(
  53. TRUE,
  54. JET_bitTableUpdatable
  55. );
  56. if(bSuccess == FALSE)
  57. {
  58. dwStatus = SET_JB_ERROR(LicensedTable.GetLastJetError());
  59. SetLastError(dwStatus);
  60. goto cleanup;
  61. }
  62. lpSqlStmt->Cleanup();
  63. dwStatus = NT4LicenseEnumBegin(
  64. lpSqlStmt,
  65. &nt4LicenseRecord
  66. );
  67. if(dwStatus != ERROR_SUCCESS)
  68. goto cleanup;
  69. while( (dwStatus = NT4RecordEnumNext(lpSqlStmt)) == ERROR_SUCCESS )
  70. {
  71. memset(&LicensedClient, 0, sizeof(LicensedClient));
  72. LicensedClient.ucEntryStatus = 0;
  73. LicensedClient.dwLicenseId = nt4License.dwLicenseId;
  74. LicensedClient.dwKeyPackId = nt4License.dwKeyPackId;
  75. LicensedClient.dwKeyPackLicenseId = 0;
  76. // LicensedClient.ftLastModifyTime;
  77. SAFESTRCPY(LicensedClient.szMachineName, nt4License.szMachineName);
  78. SAFESTRCPY(LicensedClient.szUserName, nt4License.szUserName);
  79. LicensedClient.ftIssueDate = nt4License.ftIssueDate;
  80. LicensedClient.ftExpireDate = nt4License.ftExpireDate;
  81. LicensedClient.ucLicenseStatus = nt4License.ucLicenseStatus;
  82. LicensedClient.dwNumLicenses = nt4License.dwNumLicenses;
  83. LicensedClient.dwSystemBiosChkSum = nt4LicenseRecord.checkSum.dwSystemBiosChkSum;
  84. LicensedClient.dwVideoBiosChkSum = nt4LicenseRecord.checkSum.dwVideoBiosChkSum;
  85. LicensedClient.dwFloppyBiosChkSum = nt4LicenseRecord.checkSum.dwFloppyBiosChkSum;
  86. LicensedClient.dwHardDiskSize = nt4LicenseRecord.checkSum.dwHardDiskSize;
  87. LicensedClient.dwRamSize = nt4LicenseRecord.checkSum.dwRamSize;
  88. hwidHint = LicensedClient;
  89. LicensedClient.dbLowerBound = hwidHint.dbLowerBound;
  90. //
  91. if(LicensedTable.InsertRecord(LicensedClient) == FALSE)
  92. {
  93. dwStatus = SET_JB_ERROR(LicensedTable.GetLastJetError());
  94. goto cleanup;
  95. }
  96. }
  97. NT4RecordEnumEnd(lpSqlStmt);
  98. if(dwStatus != ERROR_ODBC_NO_DATA_FOUND)
  99. {
  100. // something wrong in fetch().
  101. goto cleanup;
  102. }
  103. dwStatus = ERROR_SUCCESS;
  104. cleanup:
  105. lpSqlStmt->Cleanup();
  106. LicensedTable.CloseTable();
  107. return dwStatus;
  108. }
  109. //--------------------------------------------------------------------------
  110. DWORD
  111. UpgradeLicensePackDescTable(
  112. CSQLStmt* lpSqlStmt,
  113. JBDatabase& jbDatabase
  114. )
  115. /*++
  116. Note:
  117. Transaction must be active
  118. ++*/
  119. {
  120. DWORD dwStatus = ERROR_SUCCESS;
  121. NT4KEYPACKDESC nt4KpDesc;
  122. NT4KEYPACKDESCRECORD nt4KpDescRecord(&nt4KpDesc);
  123. LICPACKDESC LicPackDesc;
  124. LicPackDescTable LicPackDescTable(jbDatabase);
  125. BOOL bSuccess;
  126. bSuccess = LicPackDescTable.OpenTable(
  127. TRUE,
  128. JET_bitTableUpdatable
  129. );
  130. if(bSuccess == FALSE)
  131. {
  132. dwStatus = SET_JB_ERROR(LicPackDescTable.GetLastJetError());
  133. SetLastError(dwStatus);
  134. goto cleanup;
  135. }
  136. lpSqlStmt->Cleanup();
  137. dwStatus = NT4KeyPackDescEnumBegin(
  138. lpSqlStmt,
  139. &nt4KpDescRecord
  140. );
  141. if(dwStatus != ERROR_SUCCESS)
  142. goto cleanup;
  143. while( (dwStatus = NT4RecordEnumNext(lpSqlStmt)) == ERROR_SUCCESS )
  144. {
  145. memset(&LicPackDesc, 0, sizeof(LicPackDesc));
  146. LicPackDesc.ucEntryStatus = 0;
  147. LicPackDesc.dwKeyPackId = nt4KpDesc.dwKeyPackId;
  148. LicPackDesc.dwLanguageId = nt4KpDesc.dwLanguageId;
  149. // LicPackDesc.ftLastModifyTime = 0;
  150. SAFESTRCPY(LicPackDesc.szCompanyName, nt4KpDesc.szCompanyName);
  151. SAFESTRCPY(LicPackDesc.szProductName, nt4KpDesc.szProductName);
  152. SAFESTRCPY(LicPackDesc.szProductDesc, nt4KpDesc.szProductDesc);
  153. //
  154. if(LicPackDescTable.InsertRecord(LicPackDesc) == FALSE)
  155. {
  156. dwStatus = SET_JB_ERROR(LicPackDescTable.GetLastJetError());
  157. goto cleanup;
  158. }
  159. }
  160. NT4RecordEnumEnd(lpSqlStmt);
  161. if(dwStatus != ERROR_ODBC_NO_DATA_FOUND)
  162. {
  163. // something wrong in fetch().
  164. goto cleanup;
  165. }
  166. dwStatus = ERROR_SUCCESS;
  167. cleanup:
  168. lpSqlStmt->Cleanup();
  169. LicPackDescTable.CloseTable();
  170. return dwStatus;
  171. }
  172. //--------------------------------------------------------------------------
  173. DWORD
  174. UpgradeLicensePackTable(
  175. CSQLStmt* lpSqlStmt,
  176. JBDatabase& jbDatabase
  177. )
  178. /*++
  179. Note:
  180. Transaction must be active
  181. ++*/
  182. {
  183. DWORD dwStatus = ERROR_SUCCESS;
  184. NT4KEYPACK nt4KeyPack;
  185. NT4KEYPACKRECORD nt4KeyPackRecord(&nt4KeyPack);
  186. LICENSEPACK LicPack;
  187. LicPackTable LicPackTable(jbDatabase);
  188. BOOL bSuccess=TRUE;
  189. //FILETIME ftCurTime;
  190. //SYSTEMTIME sysTime;
  191. //GetSystemTime(&sysTime);
  192. //if(SystemTimeToFileTime(&sysTime, &ftCurTime) == FALSE)
  193. //{
  194. // dwStatus = GetLastError();
  195. // goto cleanup;
  196. //}
  197. //
  198. // Open License KeyPack table
  199. //
  200. bSuccess = LicPackTable.OpenTable(
  201. TRUE,
  202. JET_bitTableUpdatable
  203. );
  204. if(bSuccess == FALSE)
  205. {
  206. dwStatus = SET_JB_ERROR(LicPackTable.GetLastJetError());
  207. SetLastError(dwStatus);
  208. goto cleanup;
  209. }
  210. lpSqlStmt->Cleanup();
  211. dwStatus = NT4KeyPackEnumBegin(
  212. lpSqlStmt,
  213. &nt4KeyPackRecord
  214. );
  215. if(dwStatus != ERROR_SUCCESS)
  216. goto cleanup;
  217. while( (dwStatus = NT4RecordEnumNext(lpSqlStmt)) == ERROR_SUCCESS )
  218. {
  219. memset(&LicPack, 0, sizeof(LicPack));
  220. //
  221. // Copy over license pack information.
  222. //
  223. LicPack.ucEntryStatus = 0;
  224. LicPack.dwKeyPackId = nt4KeyPack.dwKeyPackId;
  225. //LicPack.ftLastModifyTime = 0; // don't want this license pack to
  226. // be populated to other server
  227. LicPack.dwAttribute = 0;
  228. if( nt4KeyPack.ucKeyPackType > LSKEYPACKTYPE_LAST ||
  229. nt4KeyPack.ucKeyPackType < LSKEYPACKTYPE_FIRST)
  230. {
  231. //
  232. // This is an invalid key pack, ignore
  233. //
  234. continue;
  235. }
  236. if( nt4KeyPack.ucKeyPackType == LSKEYPACKTYPE_SELECT ||
  237. nt4KeyPack.ucKeyPackType == LSKEYPACKTYPE_TEMPORARY ||
  238. nt4KeyPack.ucKeyPackType == LSKEYPACKTYPE_FREE)
  239. {
  240. LicPack.dwNextSerialNumber = nt4KeyPack.dwNumberOfLicenses + 1;
  241. }
  242. else
  243. {
  244. LicPack.dwNextSerialNumber = nt4KeyPack.dwTotalLicenseInKeyPack - nt4KeyPack.dwNumberOfLicenses + 1;
  245. }
  246. LicPack.dwActivateDate = nt4KeyPack.dwActivateDate;
  247. LicPack.dwExpirationDate = nt4KeyPack.dwExpirationDate;
  248. LicPack.dwNumberOfLicenses = nt4KeyPack.dwNumberOfLicenses;
  249. LicPack.ucKeyPackStatus = nt4KeyPack.ucKeyPackStatus;
  250. LicPack.pbDomainSid = NULL;
  251. LicPack.cbDomainSid = 0;
  252. memcpy(
  253. LicPack.szInstallId,
  254. g_pbSetupId,
  255. min(sizeof(LicPack.szInstallId)/sizeof(LicPack.szInstallId[0]) - 1, g_cbSetupId)
  256. );
  257. // LicPack.szDomainName
  258. SAFESTRCPY(LicPack.szTlsServerName, g_szComputerName);
  259. //
  260. // Standard KeyPack Property..
  261. //
  262. LicPack.ucAgreementType = nt4KeyPack.ucKeyPackType;
  263. SAFESTRCPY(LicPack.szCompanyName, nt4KeyPack.szCompanyName)
  264. SAFESTRCPY(LicPack.szProductId, nt4KeyPack.szProductId);
  265. LicPack.wMajorVersion = nt4KeyPack.wMajorVersion;
  266. LicPack.wMinorVersion = nt4KeyPack.wMinorVersion;
  267. //
  268. // TermSrv specific code
  269. //
  270. if(_tcsicmp(LicPack.szProductId, NT4HYDRAPRODUCT_FULLVERSION_SKU) == 0)
  271. {
  272. LicPack.dwPlatformType = 0xFF;
  273. SAFESTRCPY(LicPack.szKeyPackId, nt4KeyPack.szProductId);
  274. }
  275. else if(_tcsicmp(LicPack.szProductId, NT4HYDRAPRODUCT_UPGRADE_SKU) == 0)
  276. {
  277. LicPack.dwPlatformType = 0x1;
  278. SAFESTRCPY(LicPack.szKeyPackId, nt4KeyPack.szProductId);
  279. }
  280. else if(_tcsicmp(LicPack.szProductId, NT4HYDRAPRODUCT_EXISTING_SKU) == 0)
  281. {
  282. LicPack.dwPlatformType = 0x2;
  283. SAFESTRCPY(LicPack.szKeyPackId, nt4KeyPack.szProductId);
  284. }
  285. else
  286. {
  287. SAFESTRCPY(LicPack.szKeyPackId, nt4KeyPack.szKeyPackId);
  288. LicPack.dwPlatformType = nt4KeyPack.dwPlatformType;
  289. }
  290. LicPack.ucLicenseType = nt4KeyPack.ucLicenseType;
  291. LicPack.ucChannelOfPurchase = nt4KeyPack.ucChannelOfPurchase;
  292. SAFESTRCPY(LicPack.szBeginSerialNumber, nt4KeyPack.szBeginSerialNumber);
  293. LicPack.dwTotalLicenseInKeyPack = nt4KeyPack.dwTotalLicenseInKeyPack;
  294. LicPack.dwProductFlags = nt4KeyPack.dwProductFlags;
  295. //
  296. if(LicPackTable.InsertRecord(LicPack) == FALSE)
  297. {
  298. dwStatus = SET_JB_ERROR(LicPackTable.GetLastJetError());
  299. goto cleanup;
  300. }
  301. }
  302. NT4RecordEnumEnd(lpSqlStmt);
  303. if(dwStatus != ERROR_ODBC_NO_DATA_FOUND)
  304. {
  305. // something wrong in fetch().
  306. goto cleanup;
  307. }
  308. dwStatus = ERROR_SUCCESS;
  309. cleanup:
  310. lpSqlStmt->Cleanup();
  311. LicPackTable.CloseTable();
  312. return dwStatus;
  313. }
  314. #endif
  315. //--------------------------------------------------------------------------
  316. DWORD
  317. GetNT4DbConfig(
  318. LPTSTR pszDsn,
  319. LPTSTR pszUserName,
  320. LPTSTR pszPwd,
  321. LPTSTR pszMdbFile
  322. )
  323. /*++
  324. ++*/
  325. {
  326. HKEY hKey = NULL;
  327. DWORD dwStatus = ERROR_SUCCESS;
  328. TCHAR szOdbcDsn[128]=NT4LSERVER_DEFAULT_DSN; // ODBC DSN
  329. TCHAR szOdbcUser[128]=NT4LSERVER_DEFAULT_USER; // ODBC User Name
  330. TCHAR szOdbcPwd[128]=NT4LSERVER_DEFAULT_PWD; // ODBC Password
  331. TCHAR szMdbFile[MAX_PATH+1];
  332. DWORD dwBuffer=0;
  333. PBYTE pbData = NULL;
  334. DWORD cbData = 0;
  335. BOOL bSuccess;
  336. //
  337. // Open NT4 license server specific registry key
  338. //
  339. dwStatus = RegOpenKeyEx(
  340. HKEY_LOCAL_MACHINE,
  341. NT4LSERVER_REGKEY,
  342. 0,
  343. KEY_ALL_ACCESS,
  344. &hKey
  345. );
  346. if(dwStatus != ERROR_SUCCESS)
  347. {
  348. dwStatus = ERROR_INVALID_NT4_SETUP;
  349. goto cleanup;
  350. }
  351. //
  352. // Load ODBC DSN and User name from registry,
  353. // ignore error return and use default value.
  354. //
  355. dwBuffer = sizeof(szOdbcDsn);
  356. dwStatus = RegQueryValueEx(
  357. hKey,
  358. NT4LSERVER_PARAMETERS_DSN,
  359. NULL,
  360. NULL,
  361. (LPBYTE)szOdbcDsn,
  362. &dwBuffer
  363. );
  364. if(dwStatus == ERROR_SUCCESS && pszDsn)
  365. {
  366. lstrcpy(pszDsn, szOdbcDsn);
  367. }
  368. dwBuffer = sizeof(szOdbcUser);
  369. dwStatus = RegQueryValueEx(
  370. hKey,
  371. NT4LSERVER_PARAMETERS_USER,
  372. NULL,
  373. NULL,
  374. (LPBYTE)szOdbcUser,
  375. &dwBuffer
  376. );
  377. if(dwStatus == ERROR_SUCCESS && pszUserName)
  378. {
  379. lstrcpy(pszUserName, szOdbcUser);
  380. }
  381. //
  382. // Load database password from LSA
  383. //
  384. dwStatus = RetrieveKey(
  385. LSERVER_LSA_PASSWORD_KEYNAME,
  386. &pbData,
  387. &cbData
  388. );
  389. #ifndef PRIVATE_DBG
  390. if(dwStatus != ERROR_SUCCESS)
  391. {
  392. //
  393. // Invalid NT4 license server setup or hydra beta2
  394. // license server which we don't support.
  395. //
  396. dwStatus = ERROR_INVALID_NT4_SETUP;
  397. goto cleanup;
  398. }
  399. #endif
  400. dwStatus = ERROR_SUCCESS;
  401. memset(szOdbcPwd, 0, sizeof(szOdbcPwd));
  402. memcpy(
  403. (PBYTE)szOdbcPwd,
  404. pbData,
  405. min(cbData, sizeof(szOdbcPwd) - sizeof(TCHAR))
  406. );
  407. if(pszPwd != NULL)
  408. {
  409. lstrcpy(pszPwd, szOdbcPwd);
  410. }
  411. //
  412. // Verify data source is properly installed
  413. //
  414. bSuccess = IsDataSourceInstalled(
  415. szOdbcDsn,
  416. ODBC_SYSTEM_DSN,
  417. szMdbFile,
  418. MAX_PATH
  419. );
  420. if(bSuccess == FALSE)
  421. {
  422. dwStatus = ERROR_INVALID_NT4_SETUP;
  423. goto cleanup;
  424. }
  425. if(pszMdbFile != NULL)
  426. {
  427. _tcscpy(pszMdbFile, szMdbFile);
  428. }
  429. cleanup:
  430. if(hKey != NULL)
  431. {
  432. RegCloseKey(hKey);
  433. }
  434. if(pbData != NULL)
  435. {
  436. LocalFree(pbData);
  437. }
  438. return dwStatus;
  439. }
  440. //--------------------------------------------------------------------------
  441. DWORD
  442. DeleteNT4ODBCDataSource()
  443. /*++
  444. --*/
  445. {
  446. BOOL bSuccess;
  447. DWORD dwStatus = ERROR_SUCCESS;
  448. //
  449. // Get Hydra 4 DB configuration, make sure
  450. // data source is properly config.
  451. //
  452. dwStatus = GetNT4DbConfig(
  453. g_szOdbcDsn,
  454. g_szOdbcUser,
  455. g_szOdbcPwd,
  456. g_szMdbFile
  457. );
  458. if(dwStatus == ERROR_SUCCESS)
  459. {
  460. bSuccess = ConfigDataSource(
  461. NULL,
  462. FALSE,
  463. _TEXT(SZACCESSDRIVERNAME),
  464. g_szOdbcDsn,
  465. g_szOdbcUser,
  466. g_szOdbcPwd,
  467. g_szMdbFile
  468. );
  469. if(bSuccess == FALSE)
  470. {
  471. dwStatus = ERROR_DELETE_ODBC_DSN;
  472. }
  473. }
  474. return dwStatus;
  475. }
  476. //--------------------------------------------------------------------------
  477. #ifndef LICENOC_SMALL_UPG
  478. DWORD
  479. UpgradeNT4Database(
  480. IN DWORD dwServerRole,
  481. IN LPCTSTR pszDbFilePath,
  482. IN LPCTSTR pszDbFileName,
  483. IN BOOL bAlwaysDeleteDataSource
  484. )
  485. /*++
  486. Abstract:
  487. This routine is to upgrade Hydra4 Terminal Licensing Server database to
  488. NT5. Hydra4 uses ODBC/Jet while NT5 uses JetBlue as database engine,
  489. other changes includes table structure and additional table.
  490. Parameters:
  491. dwServerRole - Server role in enterprise, 0 - ordinaly server, 1 - enterprise server
  492. pszDbFilePath - Directory for NT5 Terminal Licensing Server Database. License
  493. Server uses this directory as JetBlue instance's log/temp/system path.
  494. pszDbFileName - Database file name, default to TlsLic.edb if NULL.
  495. Returns:
  496. ERROR_SUCCESS or error code, see upg.h for list of error code and description
  497. ++*/
  498. {
  499. DWORD dwStatus=ERROR_SUCCESS;
  500. TCHAR szFileName[MAX_PATH+1];
  501. CSQLStmt sqlStmt;
  502. UUID uuid;
  503. unsigned short *szUuid=NULL;
  504. BOOL bSuccess;
  505. DWORD dwNt4DbVersion;
  506. DWORD dwComputerName = sizeof(g_szComputerName)/sizeof(g_szComputerName[0]);
  507. BOOL bNT4LserverExists = FALSE;
  508. memset(g_szComputerName, 0, sizeof(g_szComputerName));
  509. GetComputerName(g_szComputerName, &dwComputerName);
  510. //
  511. // Verify input parameter. pszDbFilePath must not have '\' as last
  512. // character.
  513. //
  514. if(pszDbFilePath == NULL || *pszDbFilePath == _TEXT('0'))
  515. {
  516. SetLastError(dwStatus = ERROR_INVALID_PARAMETER);
  517. goto cleanup;
  518. }
  519. if(pszDbFilePath[_tcslen(pszDbFilePath) - 1] == _TEXT('\\'))
  520. {
  521. SetLastError(dwStatus = ERROR_INVALID_PARAMETER);
  522. goto cleanup;
  523. }
  524. //
  525. // Make sure target directory exist.
  526. //
  527. if(FileExists(pszDbFilePath, NULL) == FALSE)
  528. {
  529. dwStatus = ERROR_TARGETFILE_NOT_FOUND;
  530. goto cleanup;
  531. }
  532. //
  533. // Database file can't exist, we have no idea what it is.
  534. //
  535. wsprintf(
  536. szFileName,
  537. _TEXT("%s\\%s"),
  538. pszDbFilePath,
  539. (pszDbFileName) ? pszDbFileName : LSERVER_DEFAULT_EDB
  540. );
  541. //
  542. // Verify File does not exist, this file might
  543. // not be a valid JetBlue database file, setup
  544. // should verify and prompt user for different
  545. // file name
  546. //
  547. if(FileExists(szFileName, NULL) == TRUE)
  548. {
  549. dwStatus = ERROR_DEST_FILE_EXIST;
  550. goto cleanup;
  551. }
  552. //
  553. // Get Hydra 4 DB configuration, make sure
  554. // data source is properly config.
  555. //
  556. dwStatus = GetNT4DbConfig(
  557. g_szOdbcDsn,
  558. g_szOdbcUser,
  559. g_szOdbcPwd,
  560. g_szMdbFile
  561. );
  562. if(dwStatus != ERROR_SUCCESS)
  563. {
  564. goto cleanup;
  565. }
  566. bNT4LserverExists = TRUE;
  567. //
  568. // Generate License Server Setup Id
  569. //
  570. dwStatus = RetrieveKey(
  571. LSERVER_LSA_SETUPID,
  572. &g_pbSetupId,
  573. &g_cbSetupId
  574. );
  575. #ifndef PRIVATE_DBG
  576. if(dwStatus == ERROR_SUCCESS)
  577. {
  578. //
  579. // NT4 Hydra does not use this setup ID.
  580. //
  581. dwStatus = ERROR_INVALID_NT4_SETUP;
  582. goto cleanup;
  583. }
  584. #endif
  585. //
  586. // Generate License Server Unique Setup ID
  587. //
  588. UuidCreate(&uuid);
  589. UuidToString(&uuid, &szUuid);
  590. g_pbSetupId = (PBYTE)AllocateMemory((_tcslen(szUuid)+1)*sizeof(TCHAR));
  591. if(g_pbSetupId == NULL)
  592. {
  593. dwStatus = GetLastError();
  594. goto cleanup;
  595. }
  596. memcpy(
  597. g_pbSetupId,
  598. szUuid,
  599. (_tcslen(szUuid) + 1)*sizeof(TCHAR)
  600. );
  601. g_cbSetupId = (_tcslen(szUuid) + 1)*sizeof(TCHAR);
  602. RpcStringFree(&szUuid);
  603. szUuid = NULL;
  604. //
  605. // Open one SQL handle to Hydra data source
  606. //
  607. dwStatus = OpenSqlStmtHandle(
  608. &sqlStmt,
  609. g_szOdbcDsn,
  610. g_szOdbcUser,
  611. g_szOdbcPwd,
  612. g_szMdbFile
  613. );
  614. if(dwStatus != ERROR_SUCCESS)
  615. {
  616. goto cleanup;
  617. }
  618. //
  619. // Verify this is latest Hydra 4
  620. //
  621. dwStatus = GetNt4DatabaseVersion(
  622. &sqlStmt,
  623. (LONG *)&dwNt4DbVersion
  624. );
  625. if(dwStatus != ERROR_SUCCESS)
  626. {
  627. goto cleanup;
  628. }
  629. if(dwNt4DbVersion != HYDRA_DATABASE_NT40_VERSION)
  630. {
  631. //
  632. // Only support release database version
  633. //
  634. dwStatus = ERROR_NOTSUPPORT_DB_VERSION;
  635. goto cleanup;
  636. }
  637. //
  638. // Initialize JetBlue and create an empty database
  639. //
  640. dwStatus = JetBlueInitAndCreateEmptyDatabase(
  641. pszDbFilePath,
  642. (pszDbFileName) ? pszDbFileName : LSERVER_DEFAULT_EDB,
  643. g_szOdbcUser,
  644. g_szOdbcPwd,
  645. g_JbInstance,
  646. g_JetSession,
  647. g_JetDatabase
  648. );
  649. if(dwStatus != ERROR_SUCCESS)
  650. {
  651. goto cleanup;
  652. }
  653. //
  654. // Begin JetBlue Transaction,
  655. // JetBlue Transaction is session based.
  656. //
  657. g_JetSession.BeginTransaction();
  658. //
  659. // Copy over licensed key pack table
  660. //
  661. dwStatus = UpgradeLicensePackTable(
  662. &sqlStmt,
  663. g_JetDatabase
  664. );
  665. if(dwStatus != ERROR_SUCCESS)
  666. {
  667. goto cleanup;
  668. }
  669. //
  670. // Copy over licensed key pack desc. table
  671. //
  672. dwStatus = UpgradeLicensePackDescTable(
  673. &sqlStmt,
  674. g_JetDatabase
  675. );
  676. if(dwStatus != ERROR_SUCCESS)
  677. {
  678. goto cleanup;
  679. }
  680. //
  681. // Copy over issued license table
  682. //
  683. dwStatus = UpgradeIssuedLicenseTable(
  684. &sqlStmt,
  685. g_JetDatabase
  686. );
  687. if(dwStatus != ERROR_SUCCESS)
  688. {
  689. goto cleanup;
  690. }
  691. //
  692. // Write License Server Setup ID to LSA
  693. //
  694. dwStatus = StoreKey(
  695. LSERVER_LSA_SETUPID,
  696. g_pbSetupId,
  697. g_cbSetupId
  698. );
  699. //
  700. // TODO : if we could not write setup ID to LSA
  701. //
  702. cleanup:
  703. if(dwStatus == ERROR_SUCCESS)
  704. {
  705. g_JetSession.CommitTransaction();
  706. }
  707. else
  708. {
  709. g_JetSession.RollbackTransaction();
  710. }
  711. //
  712. // Close ODBC handle
  713. //
  714. sqlStmt.Close();
  715. CSQLStmt::Shutdown();
  716. g_JetDatabase.CloseDatabase();
  717. g_JetSession.EndSession();
  718. if(dwStatus != ERROR_SUCCESS)
  719. {
  720. //
  721. // Reset last run status, password is not use on JetBlue,
  722. // LSERVER_LSA_SETUPID will not get setup if error.
  723. //
  724. TLServerLastRunState lastRun;
  725. memset(&lastRun, 0, sizeof(TLServerLastRunState));
  726. lastRun.dwVersion = LSERVER_LSA_STRUCT_VERSION;
  727. StoreKey(
  728. LSERVER_LSA_LASTRUN,
  729. (PBYTE)&lastRun,
  730. sizeof(TLServerLastRunState)
  731. );
  732. }
  733. if( (dwStatus == ERROR_SUCCESS) ||
  734. (bAlwaysDeleteDataSource == TRUE && bNT4LserverExists == TRUE) )
  735. {
  736. //
  737. // Remove "Hydra License" from data source.
  738. // Non-critical error if we can't remove data source.
  739. //
  740. ConfigDataSource(
  741. NULL,
  742. FALSE,
  743. _TEXT(SZACCESSDRIVERNAME),
  744. g_szOdbcDsn,
  745. g_szOdbcUser,
  746. g_szOdbcPwd,
  747. g_szMdbFile
  748. );
  749. }
  750. FreeMemory(g_pbSetupId);
  751. return dwStatus;
  752. }
  753. #endif
  754. //---------------------------------------------------
  755. //
  756. // License Server secret key info.
  757. //
  758. #define LSERVER_LSA_PRIVATEKEY_SIGNATURE _TEXT("TermServLiceningSignKey-12d4b7c8-77d5-11d1-8c24-00c04fa3080d")
  759. #define LSERVER_LSA_PRIVATEKEY_EXCHANGE _TEXT("TermServLicensingExchKey-12d4b7c8-77d5-11d1-8c24-00c04fa3080d")
  760. #define LSERVER_LSA_LSERVERID _TEXT("TermServLicensingServerId-12d4b7c8-77d5-11d1-8c24-00c04fa3080d")
  761. #define LSERVER_SOFTWARE_REGBASE \
  762. _TEXT("SOFTWARE\\Microsoft\\") _TEXT(SZSERVICENAME)
  763. #define LSERVER_CERTIFICATE_STORE _TEXT("Certificates")
  764. #define LSERVER_SELFSIGN_CERTIFICATE_REGKEY \
  765. LSERVER_REGISTRY_BASE _TEXT(SZSERVICENAME) _TEXT("\\") LSERVER_SECRET
  766. #define LSERVER_SERVER_CERTIFICATE_REGKEY \
  767. LSERVER_SOFTWARE_REGBASE _TEXT("\\") LSERVER_CERTIFICATE_STORE
  768. #define LSERVER_CLIENT_CERTIFICATE_ISSUER _TEXT("Parm0")
  769. #define LSERVER_SIGNATURE_CERT_KEY _TEXT("Parm1")
  770. #define LSERVER_EXCHANGE_CERT_KEY _TEXT("Parm2")
  771. void
  772. CleanLicenseServerSecret()
  773. /*++
  774. --*/
  775. {
  776. DWORD dwStatus = ERROR_SUCCESS;
  777. HKEY hKey = NULL;
  778. //
  779. // Wipe out SPK in LSA
  780. //
  781. dwStatus = StoreKey(
  782. LSERVER_LSA_LSERVERID,
  783. (PBYTE) NULL,
  784. 0
  785. );
  786. dwStatus = StoreKey(
  787. LSERVER_LSA_LASTRUN,
  788. (PBYTE) NULL,
  789. 0
  790. );
  791. dwStatus = StoreKey(
  792. LSERVER_LSA_PRIVATEKEY_EXCHANGE,
  793. (PBYTE) NULL,
  794. 0
  795. );
  796. dwStatus = StoreKey(
  797. LSERVER_LSA_PRIVATEKEY_SIGNATURE,
  798. (PBYTE) NULL,
  799. 0
  800. );
  801. dwStatus=RegOpenKeyEx(
  802. HKEY_LOCAL_MACHINE,
  803. LSERVER_SERVER_CERTIFICATE_REGKEY,
  804. 0,
  805. KEY_ALL_ACCESS,
  806. &hKey
  807. );
  808. if(dwStatus == ERROR_SUCCESS)
  809. {
  810. //
  811. // Ignore error
  812. RegDeleteValue(
  813. hKey,
  814. LSERVER_SIGNATURE_CERT_KEY
  815. );
  816. RegDeleteValue(
  817. hKey,
  818. LSERVER_EXCHANGE_CERT_KEY
  819. );
  820. RegDeleteValue(
  821. hKey,
  822. LSERVER_CLIENT_CERTIFICATE_ISSUER
  823. );
  824. }
  825. if(hKey != NULL)
  826. {
  827. RegCloseKey(hKey);
  828. }
  829. return;
  830. }