Leaked source code of windows server 2003
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.

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