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.

1298 lines
36 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996-1998
  5. //
  6. // File: upgrade.cpp
  7. //
  8. // Contents: All database upgrade related.
  9. //
  10. // History: 12-09-97 HueiWang
  11. //
  12. //---------------------------------------------------------------------------
  13. #include "pch.cpp"
  14. #include "server.h"
  15. #include "upgdb.h"
  16. #include "globals.h"
  17. #include "kp.h"
  18. #include "keypack.h"
  19. #include "lkpdesc.h"
  20. #include "permlic.h"
  21. //---------------------------------------------------------------------
  22. DWORD
  23. TLSCreateUpgradeDatabase(
  24. IN JBDatabase& jbDatabase
  25. )
  26. /*++
  27. Abstract:
  28. This routine create a empty license server database.
  29. Parameters:
  30. jbDatabase : database handle.
  31. Returns:
  32. Jet error code
  33. ++*/
  34. {
  35. BOOL bSuccess;
  36. DWORD dwStatus=ERROR_SUCCESS;
  37. DWORD dwCurrentVersion=0;
  38. PBYTE pbSetupId = NULL;
  39. DWORD cbSetupId = 0;
  40. VersionTable verTable(jbDatabase);
  41. LicPackTable LicPackTable(jbDatabase);
  42. LicensedTable LicensedTable(jbDatabase);
  43. LicPackDescTable LicPackDescTable(jbDatabase);
  44. BackupSourceTable BckSrcTable(jbDatabase);
  45. WorkItemTable WkItemTable(jbDatabase);
  46. //--------------------------------------------------------
  47. TLSVersion version_search;
  48. TLSVersion version_found;
  49. DWORD dwDbVersion;
  50. BOOL bUpdateVersionRec = FALSE;
  51. if(TLSIsBetaNTServer() == TRUE)
  52. {
  53. dwDbVersion = TLS_BETA_DBVERSION;
  54. }
  55. else
  56. {
  57. dwDbVersion = TLS_CURRENT_DBVERSION;
  58. }
  59. version_search.dwVersion = dwDbVersion;
  60. _tcsncpy(
  61. version_search.szInstallId,
  62. (LPTSTR)g_pszServerPid,
  63. min(sizeof(version_search.szInstallId)/sizeof(version_search.szInstallId[0]) - 1, g_cbServerPid/sizeof(TCHAR))
  64. );
  65. //version_search.pbDomainSid = g_pbDomainSid;
  66. //version_search.cbDomainSid = g_cbDomainSid;
  67. if(verTable.OpenTable(FALSE, TRUE) == FALSE)
  68. {
  69. JET_ERR jetErr = verTable.GetLastJetError();
  70. if( jetErr != JET_errObjectNotFound ||
  71. verTable.OpenTable(TRUE, TRUE) == FALSE ||
  72. verTable.InsertRecord(version_search) == FALSE )
  73. {
  74. SetLastError(
  75. dwStatus = SET_JB_ERROR(verTable.GetLastJetError())
  76. );
  77. goto cleanup;
  78. }
  79. dwCurrentVersion = 0;
  80. }
  81. else
  82. {
  83. // load the version table
  84. // must have at least entry in the table.
  85. bSuccess = verTable.EnumerateBegin(
  86. FALSE,
  87. ENUMERATE_COMPARE_NO_FIELDS,
  88. NULL
  89. );
  90. if(bSuccess == FALSE)
  91. {
  92. dwStatus = SET_JB_ERROR(verTable.GetLastJetError());
  93. SetLastError(dwStatus);
  94. goto cleanup;
  95. }
  96. if(verTable.EnumerateNext(version_found) != RECORD_ENUM_MORE_DATA)
  97. {
  98. SetLastError(dwStatus = TLS_E_INTERNAL);
  99. goto cleanup;
  100. }
  101. verTable.EnumerateEnd();
  102. if( DATABASE_VERSION(version_found.dwVersion) > DATABASE_VERSION(dwDbVersion) &&
  103. DATABASE_VERSION(version_found.dwVersion) != W2K_RTM_JETBLUE_DBVERSION )
  104. {
  105. //
  106. // Database was created by in-compatible license server.
  107. //
  108. DBGPrintf(
  109. DBG_ERROR,
  110. DBG_FACILITY_UPGRADE,
  111. DBGLEVEL_FUNCTION_ERROR,
  112. _TEXT("Beta 3 database version 0x%08x, 0x%08x\n"),
  113. version_found.dwVersion,
  114. dwDbVersion
  115. );
  116. //
  117. // critical error, database version > what we can support
  118. //
  119. SetLastError(dwStatus = TLS_E_INCOMPATIBLEDATABSE);
  120. goto cleanup;
  121. }
  122. if( TLSIsBetaNTServer() == FALSE &&
  123. DATABASE_VERSION(version_found.dwVersion) == W2K_BETA3_JETBLUE_DBVERSION )
  124. {
  125. //
  126. //
  127. // Beta3 license database, wipe out and restart from scratch
  128. //
  129. DBGPrintf(
  130. DBG_ERROR,
  131. DBG_FACILITY_UPGRADE,
  132. DBGLEVEL_FUNCTION_ERROR,
  133. _TEXT("Beta 3 database version 0x%08x, 0x%08x\n"),
  134. version_found.dwVersion,
  135. dwDbVersion
  136. );
  137. dwStatus = TLS_E_BETADATABSE;
  138. goto cleanup;
  139. }
  140. if(IS_ENFORCE_VERSION(version_found.dwVersion) != IS_ENFORCE_VERSION(dwDbVersion))
  141. {
  142. //
  143. // Enforce/non-enforce in-compatible, wipe out database and restart from
  144. // scratch
  145. //
  146. DBGPrintf(
  147. DBG_ERROR,
  148. DBG_FACILITY_UPGRADE,
  149. DBGLEVEL_FUNCTION_ERROR,
  150. _TEXT("Enforce/Non-enforce database 0x%08x, 0x%08x\n"),
  151. version_found.dwVersion,
  152. dwDbVersion
  153. );
  154. //#if ENFORCE_LICENSING
  155. //TLSLogWarningEvent(dwStatus = TLS_W_DB_ENFORCE_NONENFORCE);
  156. //#endif
  157. //bUpdateVersionRec = TRUE;
  158. dwStatus = TLS_E_INCOMPATIBLEDATABSE;
  159. goto cleanup;
  160. }
  161. //
  162. // Server ID
  163. //
  164. if( _tcscmp(version_found.szInstallId, version_search.szInstallId) != 0 )
  165. {
  166. //
  167. // Check if this is pre-beta3 which uses GUID
  168. //
  169. dwStatus = RetrieveKey(
  170. LSERVER_LSA_SETUPID,
  171. &pbSetupId,
  172. &cbSetupId
  173. );
  174. if( dwStatus != ERROR_SUCCESS ||
  175. _tcscmp(version_found.szInstallId, (LPTSTR)pbSetupId) != 0 )
  176. {
  177. DBGPrintf(
  178. DBG_ERROR,
  179. DBG_FACILITY_UPGRADE,
  180. DBGLEVEL_FUNCTION_ERROR,
  181. _TEXT("Database does not belong to this machine\n")
  182. );
  183. #if ENFORCE_LICENSING
  184. TLSLogWarningEvent(dwStatus = TLS_W_NOTOWNER_DATABASE);
  185. #endif
  186. bUpdateVersionRec = TRUE;
  187. }
  188. }
  189. if(bUpdateVersionRec == TRUE)
  190. {
  191. //
  192. // take ownership of this database, no other DB operation, current
  193. // record is still at version, update record.
  194. //
  195. if(verTable.UpdateRecord(version_search) == FALSE)
  196. {
  197. SetLastError(
  198. dwStatus = SET_JB_ERROR(verTable.GetLastJetError())
  199. );
  200. goto cleanup;
  201. }
  202. }
  203. dwCurrentVersion = DATABASE_VERSION(version_search.dwVersion);
  204. }
  205. //--------------------------------------------------------
  206. bSuccess = LicPackTable.UpgradeTable(
  207. dwCurrentVersion,
  208. DATABASE_VERSION(dwDbVersion)
  209. );
  210. if(bSuccess == FALSE)
  211. {
  212. SetLastError(
  213. dwStatus = SET_JB_ERROR(LicPackTable.GetLastJetError())
  214. );
  215. goto cleanup;
  216. }
  217. //--------------------------------------------------------
  218. bSuccess = LicensedTable.UpgradeTable(
  219. dwCurrentVersion,
  220. DATABASE_VERSION(dwDbVersion)
  221. );
  222. if(bSuccess == FALSE)
  223. {
  224. SetLastError(
  225. dwStatus = SET_JB_ERROR(LicensedTable.GetLastJetError())
  226. );
  227. goto cleanup;
  228. }
  229. //--------------------------------------------------------
  230. bSuccess = LicPackDescTable.UpgradeTable(
  231. dwCurrentVersion,
  232. DATABASE_VERSION(dwDbVersion)
  233. ) ;
  234. if(bSuccess == FALSE)
  235. {
  236. SetLastError(
  237. dwStatus = SET_JB_ERROR(LicPackDescTable.GetLastJetError())
  238. );
  239. goto cleanup;
  240. }
  241. //--------------------------------------------------------
  242. bSuccess = BckSrcTable.UpgradeTable(
  243. dwCurrentVersion,
  244. DATABASE_VERSION(dwDbVersion)
  245. );
  246. if(bSuccess == FALSE)
  247. {
  248. SetLastError(
  249. dwStatus = SET_JB_ERROR(BckSrcTable.GetLastJetError())
  250. );
  251. goto cleanup;
  252. }
  253. //--------------------------------------------------------
  254. bSuccess = WkItemTable.UpgradeTable(
  255. dwCurrentVersion,
  256. DATABASE_VERSION(dwDbVersion)
  257. );
  258. if(bSuccess == FALSE)
  259. {
  260. SetLastError(
  261. dwStatus = SET_JB_ERROR(WkItemTable.GetLastJetError())
  262. );
  263. goto cleanup;
  264. }
  265. cleanup:
  266. if(pbSetupId != NULL)
  267. {
  268. LocalFree(pbSetupId);
  269. }
  270. //
  271. // We use global so don't free the memory
  272. //
  273. version_search.pbDomainSid = NULL;
  274. version_search.cbDomainSid = 0;
  275. verTable.CloseTable();
  276. return dwStatus;
  277. }
  278. //---------------------------------------------------------------------
  279. BOOL
  280. Upgrade236LicensePack(
  281. PTLSLICENSEPACK pLicensePack
  282. )
  283. /*++
  284. ++*/
  285. {
  286. DWORD dwMajorVersion;
  287. DWORD dwMinorVersion;
  288. BOOL bRequireUpgrade=FALSE;
  289. TCHAR szPreFix[MAX_SKU_PREFIX];
  290. TCHAR szPostFix[MAX_SKU_POSTFIX];
  291. DWORD dwPlatformType;
  292. memset(szPreFix, 0, sizeof(szPreFix));
  293. memset(szPostFix, 0, sizeof(szPostFix));
  294. _stscanf(
  295. pLicensePack->szProductId,
  296. TERMSERV_PRODUCTID_FORMAT,
  297. szPreFix,
  298. &dwMajorVersion,
  299. &dwMinorVersion,
  300. szPostFix
  301. );
  302. if(_tcscmp(szPreFix, TERMSERV_PRODUCTID_SKU) != 0)
  303. {
  304. //
  305. // Not our license pack
  306. //
  307. goto cleanup;
  308. }
  309. if(_tcscmp(szPostFix, TERMSERV_FULLVERSION_TYPE) == 0)
  310. {
  311. dwPlatformType = PLATFORMID_OTHERS;
  312. }
  313. else if(_tcscmp(szPostFix, TERMSERV_FREE_TYPE) == 0)
  314. {
  315. dwPlatformType = PLATFORMID_FREE;
  316. }
  317. else
  318. {
  319. // ignore this error...
  320. goto cleanup;
  321. }
  322. // fix entries caused by bug 402870.
  323. // Remote license pack must have remote bit in status and platformtype
  324. if( pLicensePack->ucKeyPackStatus & LSKEYPACKSTATUS_REMOTE &&
  325. !(pLicensePack->dwPlatformType & LSKEYPACK_PLATFORM_REMOTE) )
  326. {
  327. pLicensePack->dwPlatformType |= LSKEYPACK_PLATFORM_REMOTE;
  328. bRequireUpgrade = TRUE;
  329. goto cleanup;
  330. }
  331. //
  332. // If platform type is correct, no need to upgrade
  333. //
  334. if( (pLicensePack->dwPlatformType & ~LSKEYPACK_PLATFORM_REMOTE) == dwPlatformType )
  335. {
  336. goto cleanup;
  337. }
  338. //
  339. // Update platform Type.
  340. //
  341. pLicensePack->dwPlatformType = dwPlatformType;
  342. if( pLicensePack->ucKeyPackStatus & LSKEYPACKSTATUS_REMOTE )
  343. {
  344. pLicensePack->dwPlatformType |= LSKEYPACK_PLATFORM_REMOTE;
  345. }
  346. bRequireUpgrade = TRUE;
  347. cleanup:
  348. return bRequireUpgrade;
  349. }
  350. //----------------------------------------------------------
  351. DWORD
  352. TLSAddTermServCertificatePack(
  353. IN PTLSDbWorkSpace pDbWkSpace,
  354. IN BOOL bLogWarning
  355. )
  356. /*++
  357. Abstract:
  358. This routine add a sepecifc license pack to issuing/generating
  359. certificate for terminal server.
  360. Parameter:
  361. pDbWkSpace : workspace handle.
  362. bLogWarning : Log low license count warning, ignore if enforce
  363. Return:
  364. JET Error code.
  365. ++*/
  366. {
  367. DWORD dwStatus=ERROR_SUCCESS;
  368. TLSLICENSEPACK licensePack;
  369. DWORD dwKpDescStatus = ERROR_SUCCESS;
  370. struct tm convertTime;
  371. time_t expired_time;
  372. time_t activate_time;
  373. //
  374. // Set activation date to 1970 - client/server might not sync. up in time
  375. //
  376. memset(&convertTime, 0, sizeof(convertTime));
  377. convertTime.tm_year = 1980 - 1900; // expire on 2036/1/1
  378. convertTime.tm_mday = 1;
  379. activate_time = mktime(&convertTime);
  380. if(activate_time == (time_t) -1)
  381. {
  382. DBGPrintf(
  383. DBG_ERROR,
  384. DBG_FACILITY_UPGRADE,
  385. DBGLEVEL_FUNCTION_ERROR,
  386. _TEXT("Can't calculate keypack activate time\n")
  387. );
  388. return TLS_E_UPGRADE_DATABASE;
  389. }
  390. //
  391. // Expiration date
  392. //
  393. memset(&convertTime, 0, sizeof(convertTime));
  394. convertTime.tm_year = 2036 - 1900; // expire on 2036/1/1
  395. convertTime.tm_mday = 1;
  396. expired_time = mktime(&convertTime);
  397. if(expired_time == (time_t) -1)
  398. {
  399. DBGPrintf(
  400. DBG_ERROR,
  401. DBG_FACILITY_UPGRADE,
  402. DBGLEVEL_FUNCTION_ERROR,
  403. _TEXT("Can't calculate keypack expiration time\n")
  404. );
  405. return TLS_E_UPGRADE_DATABASE;
  406. }
  407. // Add a special keypack to hydra server
  408. LSKeyPack hsKeyPack;
  409. pDbWkSpace->BeginTransaction();
  410. memset(&hsKeyPack, 0, sizeof(LSKeyPack));
  411. hsKeyPack.ucKeyPackType = LSKEYPACKTYPE_FREE;
  412. SAFESTRCPY(hsKeyPack.szKeyPackId, HYDRAPRODUCT_HS_CERTIFICATE_KEYPACKID);
  413. if(!LoadResourceString(
  414. IDS_HS_COMPANYNAME,
  415. hsKeyPack.szCompanyName,
  416. sizeof(hsKeyPack.szCompanyName) / sizeof(hsKeyPack.szCompanyName[0])))
  417. {
  418. DBGPrintf(
  419. DBG_ERROR,
  420. DBG_FACILITY_UPGRADE,
  421. DBGLEVEL_FUNCTION_ERROR,
  422. _TEXT("Can't load resources for IDS_HS_COMPANYNAME\n")
  423. );
  424. SetLastError(dwStatus = TLS_E_UPGRADE_DATABASE);
  425. goto cleanup;
  426. }
  427. if(!LoadResourceString(
  428. IDS_HS_PRODUCTNAME,
  429. hsKeyPack.szProductName,
  430. sizeof(hsKeyPack.szProductName) / sizeof(hsKeyPack.szProductName[0])))
  431. {
  432. DBGPrintf(
  433. DBG_ERROR,
  434. DBG_FACILITY_UPGRADE,
  435. DBGLEVEL_FUNCTION_ERROR,
  436. _TEXT("Can't load resources for IDS_HS_PRODUCTNAME\n")
  437. );
  438. SetLastError(dwStatus = TLS_E_UPGRADE_DATABASE);
  439. goto cleanup;
  440. }
  441. if(!LoadResourceString(
  442. IDS_HS_PRODUCTDESC,
  443. hsKeyPack.szProductDesc,
  444. sizeof(hsKeyPack.szProductDesc) / sizeof(hsKeyPack.szProductDesc[0])))
  445. {
  446. DBGPrintf(
  447. DBG_ERROR,
  448. DBG_FACILITY_UPGRADE,
  449. DBGLEVEL_FUNCTION_ERROR,
  450. _TEXT("Can't load resources for IDS_HS_PRODUCTDESC\n")
  451. );
  452. SetLastError(dwStatus = TLS_E_UPGRADE_DATABASE);
  453. goto cleanup;
  454. }
  455. SAFESTRCPY(hsKeyPack.szProductId, HYDRAPRODUCT_HS_CERTIFICATE_SKU);
  456. hsKeyPack.ucKeyPackStatus = LSKEYPACKSTATUS_ACTIVE;
  457. hsKeyPack.dwActivateDate = activate_time;
  458. hsKeyPack.dwExpirationDate = expired_time;
  459. hsKeyPack.wMajorVersion=HIWORD(HYDRACERT_PRODUCT_VERSION);
  460. hsKeyPack.wMinorVersion=LOWORD(HYDRACERT_PRODUCT_VERSION);
  461. hsKeyPack.dwPlatformType=CLIENT_PLATFORMID_WINDOWS_NT_FREE;
  462. hsKeyPack.ucLicenseType=LSKEYPACKLICENSETYPE_NEW;
  463. hsKeyPack.dwLanguageId=MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US);
  464. hsKeyPack.ucChannelOfPurchase=LSKEYPACKCHANNELOFPURCHASE_RETAIL;
  465. SAFESTRCPY(hsKeyPack.szBeginSerialNumber, _TEXT("0"));
  466. hsKeyPack.dwTotalLicenseInKeyPack = 0;
  467. hsKeyPack.dwProductFlags = 0;
  468. dwStatus = TLSDBLicenseKeyPackAdd(
  469. pDbWkSpace,
  470. &hsKeyPack
  471. );
  472. if(dwStatus == ERROR_SUCCESS)
  473. {
  474. hsKeyPack.ucKeyPackStatus = LSKEYPACKSTATUS_ACTIVE;
  475. hsKeyPack.dwActivateDate = activate_time;
  476. hsKeyPack.dwExpirationDate = expired_time;
  477. dwStatus=TLSDBLicenseKeyPackSetStatus(
  478. pDbWkSpace,
  479. LSKEYPACK_SET_ALLSTATUS,
  480. &hsKeyPack
  481. );
  482. #if DBG
  483. if(dwStatus != ERROR_SUCCESS)
  484. {
  485. TLSASSERT(FALSE);
  486. }
  487. if(hsKeyPack.dwKeyPackId != 1)
  488. {
  489. // this can only success in empty database.
  490. TLSASSERT(FALSE);
  491. }
  492. #endif
  493. }
  494. if(dwStatus != ERROR_SUCCESS)
  495. {
  496. DBGPrintf(
  497. DBG_ERROR,
  498. DBG_FACILITY_UPGRADE,
  499. DBGLEVEL_FUNCTION_ERROR,
  500. _TEXT("Can't add keypack or set status - %d\n"),
  501. dwStatus
  502. );
  503. }
  504. pDbWkSpace->CommitTransaction();
  505. //
  506. // Begin another transaction for upgrading 236 product
  507. //
  508. pDbWkSpace->BeginTransaction();
  509. memset(&licensePack, 0, sizeof(licensePack));
  510. //
  511. // Terminal Server specific code...
  512. //
  513. dwStatus = TLSDBKeyPackEnumBegin(
  514. pDbWkSpace,
  515. FALSE,
  516. LSKEYPACK_SEARCH_NONE,
  517. &licensePack
  518. );
  519. while(dwStatus == ERROR_SUCCESS)
  520. {
  521. dwStatus = TLSDBKeyPackEnumNext(
  522. pDbWkSpace,
  523. &licensePack
  524. );
  525. if(dwStatus != ERROR_SUCCESS)
  526. {
  527. break;
  528. }
  529. if( Upgrade236LicensePack(&licensePack) == TRUE ||
  530. (licensePack.ucAgreementType & LSKEYPACK_REMOTE_TYPE) )
  531. {
  532. BOOL bSuccess;
  533. LicPackTable& licpackTable=pDbWkSpace->m_LicPackTable;
  534. if( licensePack.ucAgreementType & LSKEYPACK_REMOTE_TYPE ||
  535. licensePack.ucKeyPackStatus & LSKEYPACKSTATUS_REMOTE )
  536. {
  537. licensePack.dwPlatformType |= LSKEYPACK_PLATFORM_REMOTE;
  538. licensePack.ucKeyPackStatus |= LSKEYPACKSTATUS_REMOTE;
  539. licensePack.ucAgreementType &= ~LSKEYPACK_RESERVED_TYPE;
  540. }
  541. //
  542. // use the same timestamp for this record, tlsdb require update entry's timestamp
  543. //
  544. bSuccess = licpackTable.UpdateRecord(
  545. licensePack,
  546. LICENSEDPACK_PROCESS_PLATFORMTYPE | LICENSEDPACK_PROCESS_MODIFYTIME |
  547. LICENSEDPACK_PROCESS_AGREEMENTTYPE | LICENSEDPACK_PROCESS_KEYPACKSTATUS
  548. );
  549. if(bSuccess == FALSE)
  550. {
  551. dwStatus = SET_JB_ERROR(licpackTable.GetLastJetError());
  552. }
  553. }
  554. #ifndef ENFORCE_LICENSING
  555. if(bLogWarning == FALSE)
  556. {
  557. DBGPrintf(
  558. DBG_ERROR,
  559. DBG_FACILITY_UPGRADE,
  560. DBGLEVEL_FUNCTION_ERROR,
  561. _TEXT("Ignore low license count warning...\n")
  562. );
  563. continue;
  564. }
  565. if(licensePack.ucAgreementType & LSKEYPACK_REMOTE_TYPE)
  566. {
  567. // don't log warning for remote license pack.
  568. continue;
  569. }
  570. if(licensePack.ucKeyPackStatus & LSKEYPACKSTATUS_REMOTE)
  571. {
  572. // don't log warning for remote license pack.
  573. continue;
  574. }
  575. //
  576. // log low license count warning message
  577. //
  578. if( licensePack.ucAgreementType == LSKEYPACKTYPE_OPEN ||
  579. licensePack.ucAgreementType == LSKEYPACKTYPE_RETAIL ||
  580. licensePack.ucAgreementType == LSKEYPACKTYPE_SELECT )
  581. {
  582. if(licensePack.dwNumberOfLicenses > g_LowLicenseCountWarning)
  583. {
  584. continue;
  585. }
  586. //
  587. LICPACKDESC kpDescSearch, kpDescFound;
  588. memset(&kpDescSearch, 0, sizeof(kpDescSearch));
  589. memset(&kpDescFound, 0, sizeof(kpDescFound));
  590. kpDescSearch.dwKeyPackId = licensePack.dwKeyPackId;
  591. kpDescSearch.dwLanguageId = GetSystemDefaultLangID();
  592. dwKpDescStatus = TLSDBKeyPackDescFind(
  593. pDbWkSpace,
  594. TRUE,
  595. LSKEYPACK_SEARCH_KEYPACKID | LSKEYPACK_SEARCH_LANGID,
  596. &kpDescSearch,
  597. &kpDescFound
  598. );
  599. if( dwKpDescStatus == TLS_E_RECORD_NOTFOUND &&
  600. GetSystemDefaultLangID() != MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US) )
  601. {
  602. // use english description
  603. kpDescSearch.dwLanguageId = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US);
  604. dwKpDescStatus = TLSDBKeyPackDescFind(
  605. pDbWkSpace,
  606. TRUE,
  607. LSKEYPACK_SEARCH_KEYPACKID | LSKEYPACK_SEARCH_LANGID,
  608. &kpDescSearch,
  609. &kpDescFound
  610. );
  611. }
  612. if(dwKpDescStatus != ERROR_SUCCESS)
  613. {
  614. // ignore this.
  615. continue;
  616. }
  617. if( _tcsicmp( licensePack.szCompanyName, PRODUCT_INFO_COMPANY_NAME ) == 0 )
  618. {
  619. //
  620. // check with known termsrv product ID.
  621. //
  622. if( _tcsnicmp( licensePack.szProductId,
  623. TERMSERV_PRODUCTID_SKU,
  624. _tcslen(TERMSERV_PRODUCTID_SKU)) == 0 )
  625. {
  626. TLSResetLogLowLicenseWarning(
  627. licensePack.szCompanyName,
  628. TERMSERV_PRODUCTID_SKU,
  629. MAKELONG(licensePack.wMinorVersion, licensePack.wMajorVersion),
  630. TRUE
  631. );
  632. }
  633. else if(_tcsnicmp( licensePack.szProductId,
  634. TERMSERV_PRODUCTID_INTERNET_SKU,
  635. _tcslen(TERMSERV_PRODUCTID_INTERNET_SKU)) == 0 )
  636. {
  637. TLSResetLogLowLicenseWarning(
  638. licensePack.szCompanyName,
  639. TERMSERV_PRODUCTID_INTERNET_SKU,
  640. MAKELONG(licensePack.wMinorVersion, licensePack.wMajorVersion),
  641. TRUE
  642. );
  643. }
  644. else
  645. {
  646. TLSResetLogLowLicenseWarning(
  647. licensePack.szCompanyName,
  648. licensePack.szProductId,
  649. MAKELONG(licensePack.wMinorVersion, licensePack.wMajorVersion),
  650. TRUE
  651. );
  652. }
  653. }
  654. else
  655. {
  656. TLSResetLogLowLicenseWarning(
  657. licensePack.szCompanyName,
  658. licensePack.szProductId,
  659. MAKELONG(licensePack.wMinorVersion, licensePack.wMajorVersion),
  660. TRUE
  661. );
  662. }
  663. {
  664. LPCTSTR pString[3];
  665. TCHAR szCount[25];
  666. memset(szCount, 0, sizeof(szCount));
  667. _sntprintf(
  668. szCount,
  669. sizeof(szCount)/sizeof(szCount[0]) - 1,
  670. _TEXT("%d"),
  671. licensePack.dwNumberOfLicenses
  672. );
  673. pString[0] = g_szComputerName;
  674. pString[1] = szCount;
  675. pString[2] = kpDescFound.szProductDesc;
  676. TLSLogEventString(
  677. EVENTLOG_WARNING_TYPE,
  678. TLS_W_LOWLICENSECOUNT,
  679. sizeof(pString)/sizeof(pString[0]),
  680. pString
  681. );
  682. }
  683. }
  684. #endif
  685. }
  686. if(dwStatus == TLS_I_NO_MORE_DATA)
  687. {
  688. dwStatus = ERROR_SUCCESS;
  689. }
  690. TLSDBKeyPackEnumEnd( pDbWkSpace );
  691. cleanup:
  692. if(dwStatus == ERROR_SUCCESS)
  693. {
  694. pDbWkSpace->CommitTransaction();
  695. }
  696. else
  697. {
  698. pDbWkSpace->RollbackTransaction();
  699. }
  700. return dwStatus;
  701. }
  702. //-----------------------------------------------------------------------------
  703. // Upgrade License Server Database
  704. //-----------------------------------------------------------------------------
  705. DWORD
  706. TLSUpgradeDatabase(
  707. IN JBInstance& jbInstance,
  708. IN LPTSTR szDatabaseFile,
  709. IN LPTSTR szUserName,
  710. IN LPTSTR szPassword
  711. )
  712. /*++
  713. ++*/
  714. {
  715. DWORD dwStatus=ERROR_SUCCESS;
  716. BOOL bSuccess;
  717. DWORD dwCurrentDbVersion;
  718. BOOL bCreateEmpty=FALSE;
  719. if(jbInstance.IsValid() == FALSE)
  720. {
  721. SetLastError(dwStatus = ERROR_INVALID_PARAMETER);
  722. return dwStatus;
  723. }
  724. JBSession jbSession(jbInstance);
  725. JBDatabase jbDatabase(jbSession);
  726. //
  727. // open version table to determine the current database version stamp.
  728. //
  729. VersionTable verTable(jbDatabase);
  730. TLSVersion version_search;
  731. TLSVersion version_found;
  732. //----------------------------------------------------------
  733. //
  734. // initialize a session, then database
  735. //
  736. bSuccess = jbSession.BeginSession(szUserName, szPassword);
  737. if(bSuccess == FALSE)
  738. {
  739. LPTSTR pString = NULL;
  740. TLSGetESEError(jbSession.GetLastJetError(), &pString);
  741. TLSLogEvent(
  742. EVENTLOG_ERROR_TYPE,
  743. TLS_E_DBGENERAL,
  744. TLS_E_JB_BEGINSESSION,
  745. jbSession.GetLastJetError(),
  746. (pString != NULL) ? pString : _TEXT("")
  747. );
  748. if(pString != NULL)
  749. {
  750. LocalFree(pString);
  751. }
  752. DBGPrintf(
  753. DBG_ERROR,
  754. DBG_FACILITY_UPGRADE,
  755. DBG_ALL_LEVEL,
  756. _TEXT("Can't start a jet session - %d\n"),
  757. jbSession.GetLastJetError()
  758. );
  759. SetLastError(dwStatus = TLS_E_UPGRADE_DATABASE);
  760. goto cleanup;
  761. }
  762. //
  763. // Open the database
  764. //
  765. bSuccess = jbDatabase.OpenDatabase(szDatabaseFile);
  766. if( bSuccess == FALSE )
  767. {
  768. JET_ERR jetErr = jbDatabase.GetLastJetError();
  769. if(jetErr == JET_errDatabaseCorrupted)
  770. {
  771. //
  772. // report corrupted database
  773. //
  774. TLSLogEvent(
  775. EVENTLOG_ERROR_TYPE,
  776. TLS_E_DBGENERAL,
  777. TLS_E_CORRUPT_DATABASE
  778. );
  779. SetLastError(dwStatus = TLS_E_CORRUPT_DATABASE);
  780. goto cleanup;
  781. }
  782. else if(jetErr != JET_errFileNotFound)
  783. {
  784. LPTSTR pString = NULL;
  785. TLSGetESEError(jetErr, &pString);
  786. //
  787. // other type of error
  788. //
  789. TLSLogEvent(
  790. EVENTLOG_ERROR_TYPE,
  791. TLS_E_DBGENERAL,
  792. TLS_E_JB_OPENDATABASE,
  793. szDatabaseFile,
  794. jbDatabase.GetLastJetError(),
  795. (pString != NULL) ? pString : _TEXT("")
  796. );
  797. if(pString != NULL)
  798. {
  799. LocalFree(pString);
  800. }
  801. dwStatus = SET_JB_ERROR(jetErr);
  802. SetLastError(dwStatus);
  803. goto cleanup;
  804. }
  805. //
  806. // database does not exist, create one
  807. //
  808. bSuccess = jbDatabase.CreateDatabase(szDatabaseFile);
  809. if(bSuccess == FALSE)
  810. {
  811. LPTSTR pString = NULL;
  812. TLSGetESEError(jbDatabase.GetLastJetError(), &pString);
  813. TLSLogEvent(
  814. EVENTLOG_ERROR_TYPE,
  815. TLS_E_DBGENERAL,
  816. TLS_E_JB_CREATEDATABASE,
  817. szDatabaseFile,
  818. jbDatabase.GetLastJetError(),
  819. (pString != NULL) ? pString : _TEXT("")
  820. );
  821. if(pString != NULL)
  822. {
  823. LocalFree(pString);
  824. }
  825. DBGPrintf(
  826. DBG_ERROR,
  827. DBG_FACILITY_JETBLUE,
  828. DBGLEVEL_FUNCTION_ERROR,
  829. _TEXT("Error : can't create new database - error code %d\n"),
  830. jbDatabase.GetLastJetError()
  831. );
  832. dwStatus = SET_JB_ERROR(jbDatabase.GetLastJetError());
  833. SetLastError(dwStatus);
  834. goto cleanup;
  835. }
  836. bCreateEmpty=TRUE;
  837. }
  838. jbSession.BeginTransaction();
  839. //
  840. // Create/upgrade all the tables
  841. //
  842. dwStatus = TLSCreateUpgradeDatabase(
  843. jbDatabase
  844. );
  845. if(TLS_ERROR(dwStatus) == TRUE)
  846. {
  847. jbSession.RollbackTransaction();
  848. }
  849. else
  850. {
  851. jbSession.CommitTransaction();
  852. }
  853. cleanup:
  854. jbDatabase.CloseDatabase();
  855. jbSession.EndSession();
  856. if(TLS_ERROR(dwStatus) == TRUE)
  857. {
  858. return dwStatus;
  859. }
  860. return (bCreateEmpty) ? TLS_I_CREATE_EMPTYDATABASE : dwStatus;
  861. }
  862. //---------------------------------------------------------------------
  863. DWORD
  864. TLSLogRemoveLicenseEvent(
  865. IN PTLSDbWorkSpace pDbWkSpace,
  866. IN PTLSLICENSEPACK plicensePack,
  867. IN DWORD dwNumLicenses
  868. )
  869. /*++
  870. Abstract:
  871. Log a 'license has been removed' event.
  872. Parameter:
  873. pDbWkSpace : DB work space handle.
  874. pLicensePack : Pointer to license pack that available licenses
  875. are to be remove.
  876. dwNumLicenses : Number of licenses has been removed.
  877. Returns:
  878. ERROR_SUCCESS or error code.
  879. --*/
  880. {
  881. DWORD dwKpDescStatus = ERROR_SUCCESS;
  882. //
  883. LICPACKDESC kpDescSearch, kpDescFound;
  884. memset(&kpDescSearch, 0, sizeof(kpDescSearch));
  885. memset(&kpDescFound, 0, sizeof(kpDescFound));
  886. kpDescSearch.dwKeyPackId = plicensePack->dwKeyPackId;
  887. kpDescSearch.dwLanguageId = GetSystemDefaultLangID();
  888. dwKpDescStatus = TLSDBKeyPackDescFind(
  889. pDbWkSpace,
  890. TRUE,
  891. LSKEYPACK_SEARCH_KEYPACKID | LSKEYPACK_SEARCH_LANGID,
  892. &kpDescSearch,
  893. &kpDescFound
  894. );
  895. if( dwKpDescStatus == TLS_E_RECORD_NOTFOUND &&
  896. GetSystemDefaultLangID() != MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US) )
  897. {
  898. // use english description
  899. kpDescSearch.dwLanguageId = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US);
  900. dwKpDescStatus = TLSDBKeyPackDescFind(
  901. pDbWkSpace,
  902. TRUE,
  903. LSKEYPACK_SEARCH_KEYPACKID | LSKEYPACK_SEARCH_LANGID,
  904. &kpDescSearch,
  905. &kpDescFound
  906. );
  907. }
  908. if(dwKpDescStatus == ERROR_SUCCESS || dwKpDescStatus == TLS_E_RECORD_NOTFOUND)
  909. {
  910. //
  911. // Log event.
  912. //
  913. TCHAR szNumLicenses[25];
  914. LPCTSTR pString[3];
  915. wsprintf(
  916. szNumLicenses,
  917. _TEXT("%d"),
  918. dwNumLicenses
  919. );
  920. pString[0] = szNumLicenses;
  921. pString[1] = (dwKpDescStatus == ERROR_SUCCESS) ?
  922. kpDescFound.szProductDesc :
  923. plicensePack->szProductId;
  924. pString[2] = g_szComputerName;
  925. TLSLogEventString(
  926. EVENTLOG_WARNING_TYPE,
  927. TLS_W_REMOVELICENSES,
  928. sizeof(pString)/sizeof(pString[0]),
  929. pString
  930. );
  931. dwKpDescStatus = ERROR_SUCCESS;
  932. }
  933. return dwKpDescStatus;
  934. }
  935. //---------------------------------------------------------------------
  936. DWORD
  937. TLSRemoveLicensesFromInvalidDatabase(
  938. IN PTLSDbWorkSpace pDbWkSpace
  939. )
  940. /*++
  941. Abstract:
  942. Remove available licenses from all license pack.
  943. Parameter:
  944. pDbWkSpace : Pointer to DB work space handle.
  945. Return:
  946. ERROR_SUCCESS or error code.
  947. --*/
  948. {
  949. DWORD dwStatus = ERROR_SUCCESS;
  950. if(pDbWkSpace == NULL)
  951. {
  952. dwStatus = ERROR_INVALID_PARAMETER;
  953. return dwStatus;
  954. }
  955. pDbWkSpace->BeginTransaction();
  956. // all license keypack inserted has the setup id on it.
  957. TLSLICENSEPACK found;
  958. memset( &found, 0, sizeof(found) );
  959. dwStatus = TLSDBKeyPackEnumBegin(
  960. pDbWkSpace,
  961. FALSE,
  962. LSKEYPACK_SEARCH_NONE,
  963. &found
  964. );
  965. if(dwStatus != ERROR_SUCCESS)
  966. {
  967. TLSASSERT(FALSE);
  968. goto cleanup;
  969. }
  970. while(dwStatus == ERROR_SUCCESS)
  971. {
  972. dwStatus = TLSDBKeyPackEnumNext(
  973. pDbWkSpace,
  974. &found
  975. );
  976. if(dwStatus != ERROR_SUCCESS)
  977. {
  978. continue;
  979. }
  980. if( _tcsicmp(found.szKeyPackId, HYDRAPRODUCT_HS_CERTIFICATE_KEYPACKID) == 0 &&
  981. _tcsicmp(found.szProductId, HYDRAPRODUCT_HS_CERTIFICATE_SKU) == 0 )
  982. {
  983. // don't touch terminal server certificate keypack
  984. continue;
  985. }
  986. if( (found.ucAgreementType & LSKEYPACK_REMOTE_TYPE) ||
  987. (found.ucKeyPackStatus & LSKEYPACKSTATUS_REMOTE) )
  988. {
  989. #if 0
  990. // Don't bother about remote keypack
  991. // remote license keypack, delete it.
  992. dwStatus = TLSDBKeyPackDeleteEntry(
  993. pDbWkSpace,
  994. TRUE,
  995. &found
  996. );
  997. //
  998. // non-critical error if failed.
  999. //
  1000. #endif
  1001. dwStatus = ERROR_SUCCESS;
  1002. continue;
  1003. }
  1004. UCHAR ucAgreementType = found.ucAgreementType & ~LSKEYPACK_RESERVED_TYPE;
  1005. UCHAR ucKeyPackStatus = found.ucKeyPackStatus & ~LSKEYPACKSTATUS_RESERVED;
  1006. if(ucKeyPackStatus == LSKEYPACKSTATUS_RETURNED)
  1007. {
  1008. //
  1009. // This license pack has been restored before.
  1010. //
  1011. continue;
  1012. }
  1013. //
  1014. // Select, retail, concurrent, open
  1015. //
  1016. if( ucAgreementType == LSKEYPACKTYPE_SELECT ||
  1017. ucAgreementType == LSKEYPACKTYPE_RETAIL ||
  1018. ucAgreementType == LSKEYPACKTYPE_CONCURRENT ||
  1019. ucAgreementType == LSKEYPACKTYPE_OPEN )
  1020. {
  1021. DWORD dwNumLicenses = found.dwNumberOfLicenses;
  1022. //
  1023. // Mark license pack returned so that no license can be issued
  1024. // from this license pack.
  1025. //
  1026. found.ucKeyPackStatus = LSKEYPACKSTATUS_RETURNED;
  1027. dwStatus = TLSDBKeyPackUpdateEntry(
  1028. pDbWkSpace,
  1029. TRUE,
  1030. LSKEYPACK_EXSEARCH_KEYPACKSTATUS,
  1031. &found
  1032. );
  1033. if(dwStatus != ERROR_SUCCESS)
  1034. {
  1035. DBGPrintf(
  1036. DBG_ERROR,
  1037. DBG_FACILITY_UPGRADE,
  1038. DBG_ALL_LEVEL,
  1039. _TEXT("TLSDBKeyPackUpdateEntry() failed - %d\n"),
  1040. dwStatus
  1041. );
  1042. TLSASSERT(FALSE);
  1043. continue;
  1044. }
  1045. // log an event
  1046. dwStatus = TLSLogRemoveLicenseEvent(
  1047. pDbWkSpace,
  1048. &found,
  1049. dwNumLicenses
  1050. );
  1051. if(dwStatus != ERROR_SUCCESS)
  1052. {
  1053. continue;
  1054. }
  1055. }
  1056. }
  1057. TLSDBKeyPackEnumEnd(pDbWkSpace);
  1058. if(dwStatus == TLS_I_NO_MORE_DATA)
  1059. {
  1060. dwStatus = ERROR_SUCCESS;
  1061. }
  1062. else if(dwStatus != ERROR_SUCCESS)
  1063. {
  1064. goto cleanup;
  1065. }
  1066. cleanup:
  1067. if(TLS_ERROR(dwStatus) == TRUE)
  1068. {
  1069. pDbWkSpace->RollbackTransaction();
  1070. }
  1071. else
  1072. {
  1073. pDbWkSpace->CommitTransaction();
  1074. }
  1075. return dwStatus;
  1076. }