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.

883 lines
25 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996-1998
  5. //
  6. // File: db.cpp
  7. //
  8. // Contents:
  9. // all routine deal with cross table query
  10. //
  11. // History:
  12. // Feb 4, 98 HueiWang Created
  13. //---------------------------------------------------------------------------
  14. #include "pch.cpp"
  15. #include "globals.h"
  16. #include "db.h"
  17. #include "clilic.h"
  18. #include "keypack.h"
  19. #include "kp.h"
  20. #include "lkpdesc.h"
  21. /****************************************************************************
  22. Function:
  23. LSDBValidateLicense()
  24. Description:
  25. Routine to validate license agaist database, must call LSDecodeLicense()
  26. to convert hydra license to LICENSEREQUEST structure.
  27. Arguments:
  28. IN CSQLStmt* - SQL Statement handle to use
  29. IN PLICENSEREQUEST - License in the form of LICENSEREQUEST structure
  30. IN dwKeyPackId - KeyPack table's ID that is license is issued from
  31. IN dwLicenseId - License tables's License ID
  32. OUT LPKEYPACK - KeyPack record this license is issued from, NULL if not
  33. interested in this value.
  34. OUT LPLICENSE - Corresponding license record for this license, NULL if
  35. not interest in this value.
  36. Returns:
  37. ERROR_SUCCESS
  38. TLS_E_INVALID_LICENSE
  39. TLS_E_INTERNAL
  40. ODBC error.
  41. ****************************************************************************/
  42. DWORD
  43. TLSDBValidateLicense(
  44. PTLSDbWorkSpace pDbWkSpace,
  45. //IN PBYTE pbLicense,
  46. //IN DWORD cbLicense,
  47. IN PHWID phWid,
  48. IN PLICENSEREQUEST pLicensedProduct,
  49. IN DWORD dwKeyPackId,
  50. IN DWORD dwLicenseId,
  51. OUT PTLSLICENSEPACK lpKeyPack,
  52. OUT LPLICENSEDCLIENT lpLicense
  53. )
  54. /*
  55. */
  56. {
  57. DWORD dwStatus=ERROR_SUCCESS;
  58. DWORD dwMatchCount=0;
  59. if(pDbWkSpace == NULL)
  60. {
  61. SetLastError(dwStatus = ERROR_INVALID_PARAMETER);
  62. TLSASSERT(FALSE);
  63. return dwStatus;
  64. }
  65. TLSLICENSEPACK keypack_search;
  66. TLSLICENSEPACK keypack_found;
  67. LICENSEDCLIENT license_search;
  68. LICENSEDCLIENT license_found;
  69. int count=0;
  70. BOOL bValid=TRUE;
  71. memset(&license_search, 0, sizeof(LICENSEDCLIENT));
  72. keypack_found.pbDomainSid = NULL;
  73. license_search.dwLicenseId = dwLicenseId;
  74. dwStatus = TLSDBLicenseEnumBegin(
  75. pDbWkSpace,
  76. TRUE,
  77. LSLICENSE_SEARCH_LICENSEID,
  78. &license_search
  79. );
  80. if(dwStatus != ERROR_SUCCESS)
  81. {
  82. if(IS_JB_ERROR(dwStatus) != TRUE)
  83. {
  84. SetLastError(dwStatus = TLS_E_INVALID_LICENSE);
  85. }
  86. goto cleanup;
  87. }
  88. do
  89. {
  90. dwStatus=TLSDBLicenseEnumNext(
  91. pDbWkSpace,
  92. &license_found
  93. );
  94. if(dwStatus != ERROR_SUCCESS)
  95. break;
  96. count++;
  97. } while(count < 1);
  98. TLSDBLicenseEnumEnd(pDbWkSpace);
  99. if(count != 1)
  100. {
  101. // can't find the license
  102. SetLastError(dwStatus = TLS_E_INVALID_LICENSE);
  103. goto cleanup;
  104. }
  105. if(count > 1)
  106. {
  107. // more than one entry in database has identical
  108. // license id
  109. SetLastError(dwStatus = TLS_E_INTERNAL);
  110. goto cleanup;
  111. }
  112. //
  113. // Not issue by this license server???
  114. //
  115. if(license_found.dwKeyPackId != dwKeyPackId)
  116. {
  117. SetLastError(dwStatus = TLS_E_INVALID_LICENSE);
  118. goto cleanup;
  119. }
  120. //
  121. // new license request might pass different HWID
  122. //
  123. dwMatchCount += (int)(license_found.dwSystemBiosChkSum == phWid->dwPlatformID);
  124. dwMatchCount += (int)(license_found.dwVideoBiosChkSum == phWid->Data1);
  125. dwMatchCount += (int)(license_found.dwFloppyBiosChkSum == phWid->Data2);
  126. dwMatchCount += (int)(license_found.dwHardDiskSize == phWid->Data3);
  127. dwMatchCount += (int)(license_found.dwRamSize == phWid->Data4);
  128. if(dwMatchCount < LICENSE_MIN_MATCH)
  129. {
  130. SetLastError(dwStatus = TLS_E_INVALID_LICENSE);
  131. }
  132. //
  133. // Verify against KeyPack Table
  134. //
  135. memset(&keypack_search, 0, sizeof(keypack_search));
  136. keypack_search.dwKeyPackId = dwKeyPackId;
  137. dwStatus = TLSDBKeyPackFind(
  138. pDbWkSpace,
  139. TRUE,
  140. LSKEYPACK_EXSEARCH_DWINTERNAL,
  141. &keypack_search,
  142. &keypack_found
  143. );
  144. if(dwStatus != ERROR_SUCCESS)
  145. {
  146. if(IS_JB_ERROR(dwStatus) != TRUE)
  147. {
  148. SetLastError(dwStatus = TLS_E_INVALID_LICENSE);
  149. }
  150. goto cleanup;
  151. }
  152. // match KeyPack's Product ID, Version, Language ID, PlatformID
  153. // structure change, no more product version.
  154. if(pLicensedProduct->dwPlatformID != keypack_found.dwPlatformType ||
  155. _tcsicmp((LPTSTR)pLicensedProduct->pProductInfo->pbCompanyName, keypack_found.szCompanyName) ||
  156. _tcsicmp((LPTSTR)pLicensedProduct->pProductInfo->pbProductID, keypack_found.szProductId) )
  157. {
  158. SetLastError(dwStatus = TLS_E_INVALID_LICENSE);
  159. }
  160. cleanup:
  161. //FreeTlsLicensePack(&keypack_found);
  162. if(dwStatus == ERROR_SUCCESS)
  163. {
  164. if(lpKeyPack)
  165. {
  166. *lpKeyPack = keypack_found;
  167. }
  168. if(lpLicense)
  169. {
  170. *lpLicense = license_found;
  171. }
  172. }
  173. return dwStatus;
  174. }
  175. /*************************************************************************
  176. Function:
  177. LSDBDeleteLicense()
  178. *************************************************************************/
  179. DWORD
  180. TLSDBDeleteLicense(
  181. PTLSDbWorkSpace pDbWkSpace,
  182. IN DWORD dwKeyPackId,
  183. DWORD dwLicenseId
  184. )
  185. /*
  186. */
  187. {
  188. // TODO - license entry base on license id
  189. // 1) Return license back to key pack
  190. // 2) 'Physically' delete the license.
  191. return ERROR_SUCCESS;
  192. }
  193. /*************************************************************************
  194. Function:
  195. LSDBRevokeLicense()
  196. *************************************************************************/
  197. DWORD
  198. TLSDBRevokeLicense(
  199. PTLSDbWorkSpace pDbWkSpace,
  200. IN DWORD dwKeyPacKId,
  201. IN DWORD dwLicenseId
  202. )
  203. {
  204. // Set License Status to revoked
  205. // Return License to KeyPack
  206. // call LSDBDeleteKeyPack() and if not successful, insert into RevokeLicenseTable
  207. return ERROR_SUCCESS;
  208. }
  209. /*************************************************************************
  210. Function:
  211. LSDBReturnLicense()
  212. *************************************************************************/
  213. DWORD
  214. TLSDBReturnLicense(
  215. PTLSDbWorkSpace pDbWkSpace,
  216. IN DWORD dwKeyPackId,
  217. IN DWORD dwLicenseId,
  218. IN DWORD dwNewLicenseStatus
  219. )
  220. /*
  221. */
  222. {
  223. DWORD dwStatus=ERROR_SUCCESS;
  224. DWORD dwQuantity = 1;
  225. TLSDBLockKeyPackTable();
  226. TLSDBLockLicenseTable();
  227. //
  228. // no verification on record got updated.
  229. //
  230. LICENSEDCLIENT license;
  231. license.dwLicenseId = dwLicenseId;
  232. license.ucLicenseStatus = dwNewLicenseStatus;
  233. //
  234. // use undocumented feature to delete license
  235. //
  236. DBGPrintf(
  237. DBG_INFORMATION,
  238. DBG_FACILITY_RETURN,
  239. DBGLEVEL_FUNCTION_DETAILSIMPLE,
  240. _TEXT("Deleting license ID %d issued by keypack %d\n"),
  241. license.dwLicenseId,
  242. dwKeyPackId
  243. );
  244. if (dwNewLicenseStatus == LSLICENSESTATUS_DELETE)
  245. {
  246. // get number of CALs in this license
  247. LICENSEDCLIENT licenseFound;
  248. dwStatus = TLSDBLicenseFind(
  249. pDbWkSpace,
  250. TRUE,
  251. LSLICENSE_SEARCH_LICENSEID,
  252. &license,
  253. &licenseFound
  254. );
  255. if(dwStatus == ERROR_SUCCESS)
  256. {
  257. dwQuantity = licenseFound.dwNumLicenses;
  258. }
  259. }
  260. dwStatus = TLSDBLicenseSetValue(
  261. pDbWkSpace,
  262. LSLICENSE_EXSEARCH_LICENSESTATUS,
  263. &license,
  264. FALSE
  265. );
  266. if(dwStatus == ERROR_SUCCESS && dwNewLicenseStatus == LSLICENSESTATUS_DELETE)
  267. {
  268. dwStatus = TLSDBReturnLicenseToKeyPack(
  269. pDbWkSpace,
  270. dwKeyPackId,
  271. dwQuantity
  272. );
  273. }
  274. TLSDBUnlockLicenseTable();
  275. TLSDBUnlockKeyPackTable();
  276. return dwStatus;
  277. }
  278. /*************************************************************************
  279. Function:
  280. LSDBReturnLicenseToKeyPack()
  281. *************************************************************************/
  282. DWORD
  283. TLSDBReturnLicenseToKeyPack(
  284. IN PTLSDbWorkSpace pDbWkSpace,
  285. IN DWORD dwKeyPackId,
  286. IN int dwNumLicense
  287. )
  288. {
  289. DWORD dwStatus = ERROR_SUCCESS;
  290. TLSDBLockKeyPackTable();
  291. #ifdef DBG
  292. DWORD dwPrevNumLicense=0;
  293. #endif
  294. TLSLICENSEPACK found;
  295. TLSLICENSEPACK search;
  296. found.pbDomainSid = NULL;
  297. do {
  298. // retrieve number of licenses
  299. search.dwKeyPackId = dwKeyPackId;
  300. dwStatus = TLSDBKeyPackFind(
  301. pDbWkSpace,
  302. TRUE,
  303. LSKEYPACK_EXSEARCH_DWINTERNAL,
  304. &search,
  305. &found
  306. );
  307. if(dwStatus != ERROR_SUCCESS)
  308. {
  309. if(IS_JB_ERROR(dwStatus) == FALSE)
  310. {
  311. SetLastError(dwStatus = TLS_E_RECORD_NOTFOUND);
  312. }
  313. break;
  314. }
  315. if(search.dwKeyPackId != found.dwKeyPackId)
  316. {
  317. TLSASSERT(FALSE);
  318. }
  319. #ifdef DBG
  320. dwPrevNumLicense = found.dwNumberOfLicenses;
  321. #endif
  322. // set the number of licenses issued by 1
  323. switch( (found.ucAgreementType & ~LSKEYPACK_RESERVED_TYPE) )
  324. {
  325. case LSKEYPACKTYPE_RETAIL:
  326. case LSKEYPACKTYPE_CONCURRENT:
  327. case LSKEYPACKTYPE_OPEN:
  328. case LSKEYPACKTYPE_SELECT:
  329. // number of licenses available
  330. found.dwNumberOfLicenses += dwNumLicense;
  331. break;
  332. case LSKEYPACKTYPE_FREE:
  333. case LSKEYPACKTYPE_TEMPORARY:
  334. // number of license issued
  335. if(found.dwNumberOfLicenses > 0)
  336. {
  337. found.dwNumberOfLicenses -= dwNumLicense;
  338. }
  339. break;
  340. default:
  341. SetLastError(dwStatus = TLS_E_CORRUPT_DATABASE);
  342. }
  343. #ifdef DBG
  344. DBGPrintf(
  345. DBG_INFORMATION,
  346. DBG_FACILITY_RETURN,
  347. DBGLEVEL_FUNCTION_DETAILSIMPLE,
  348. _TEXT("Returning license to keypack %d - from %d to %d\n"),
  349. found.dwKeyPackId,
  350. dwPrevNumLicense,
  351. found.dwNumberOfLicenses
  352. );
  353. #endif
  354. //
  355. // use undocumented feature to delete temp. keypack
  356. if( (found.ucAgreementType & ~LSKEYPACK_RESERVED_TYPE) == LSKEYPACKTYPE_TEMPORARY &&
  357. found.dwNumberOfLicenses == 0)
  358. {
  359. found.ucKeyPackStatus = LSKEYPACKSTATUS_DELETE;
  360. // delete keypack desc table
  361. LICPACKDESC keyPackDesc;
  362. memset(&keyPackDesc, 0, sizeof(LICPACKDESC));
  363. keyPackDesc.dwKeyPackId = found.dwKeyPackId;
  364. TLSDBKeyPackDescSetValue(
  365. pDbWkSpace,
  366. KEYPACKDESC_SET_DELETE_ENTRY,
  367. &keyPackDesc
  368. );
  369. }
  370. dwStatus=TLSDBKeyPackSetValues(
  371. pDbWkSpace,
  372. TRUE,
  373. LSKEYPACK_EXSEARCH_AVAILABLE,
  374. &found
  375. );
  376. } while(FALSE);
  377. //FreeTlsLicensePack(&found);
  378. TLSDBUnlockKeyPackTable();
  379. return dwStatus;
  380. }
  381. /*************************************************************************
  382. Function:
  383. LSDBRevokeKeyPack()
  384. *************************************************************************/
  385. DWORD
  386. TLSDBRevokeKeyPack(
  387. IN PTLSDbWorkSpace pDbWkSpace,
  388. IN DWORD dwKeyPackId
  389. )
  390. {
  391. // Set Key Pack Status to Revoke
  392. // Insert this key pack into RevokeKeyPackTable ???
  393. return ERROR_SUCCESS;
  394. }
  395. /*************************************************************************
  396. Function:
  397. LSDBReturnKeyPack()
  398. *************************************************************************/
  399. DWORD
  400. TLSDBReturnKeyPack(
  401. IN PTLSDbWorkSpace pDbWkSpace,
  402. IN DWORD dwKeyPackId
  403. )
  404. {
  405. // Same as RevokeKeyPack except status is return
  406. // Delete Key pack only when all license has been returned.
  407. return ERROR_SUCCESS;
  408. }
  409. /*************************************************************************
  410. Function:
  411. LSDBDeleteKeyPack()
  412. *************************************************************************/
  413. DWORD
  414. TLSDBDeleteKeyPack(
  415. PTLSDbWorkSpace pDbWkSpace,
  416. IN DWORD dwKeyPackId
  417. )
  418. {
  419. // Delete Only when all license has been returned.
  420. return ERROR_SUCCESS;
  421. }
  422. //+------------------------------------------------------------------------
  423. // Function:
  424. // AllocateLicenses()
  425. //
  426. // Description:
  427. // Allocate license from key Pack
  428. //
  429. // Arguments:
  430. // IN lpSqlStmt - sql statement handle
  431. // IN ucKeyPackType - key pack type to allocate license from
  432. // IN szCompanyName - Product Company
  433. // IN szProductId - Product Name
  434. // IN dwVersion - Product Version
  435. // IN dwPlatformId - Product PlatformId
  436. // IN dwLangId - Product Lanugage Id
  437. // IN OUT lpdwNumLicense - number of license to be allocated and on
  438. // return, number of licenses actually allocated
  439. // IN bufSize - number of interested keypack that has requested license
  440. // IN OUT lpAllocationVector - number of license allocated from list of
  441. // key pack that has requested licenses.
  442. // IN OUT LPKEYPACK - key Pack that license was allocated from
  443. //
  444. // Returns:
  445. // TLS_E_INVALID_DATA Invalid parameter
  446. // TLS_I_NO_MORE_DATA No key pack has the requested license
  447. //
  448. // Notes:
  449. // To keep code clean/simple, call ReturnLicenses() for returning
  450. // licenses
  451. //-------------------------------------------------------------------------
  452. DWORD
  453. VerifyTLSDBAllocateRequest(
  454. IN PTLSDBAllocateRequest pRequest
  455. )
  456. /*
  457. */
  458. {
  459. DWORD dwStatus = ERROR_SUCCESS;
  460. UCHAR ucAgreementType;
  461. if(pRequest == NULL)
  462. {
  463. SetLastError(dwStatus = ERROR_INVALID_PARAMETER);
  464. goto cleanup;
  465. }
  466. ucAgreementType = (pRequest->ucAgreementType & ~pRequest->ucAgreementType);
  467. if(ucAgreementType < LSKEYPACKTYPE_FIRST || ucAgreementType > LSKEYPACKTYPE_LAST)
  468. {
  469. DBGPrintf(
  470. DBG_ERROR,
  471. DBGLEVEL_FUNCTION_DETAILSIMPLE,
  472. DBG_ALL_LEVEL,
  473. _TEXT("AllocateLicenses() invalid keypack type - %d\n"),
  474. pRequest->ucAgreementType
  475. );
  476. SetLastError(dwStatus = TLS_E_INVALID_DATA);
  477. goto cleanup;
  478. }
  479. if(pRequest->szCompanyName == NULL || _tcslen(pRequest->szCompanyName) == 0)
  480. {
  481. DBGPrintf(
  482. DBG_ERROR,
  483. DBGLEVEL_FUNCTION_DETAILSIMPLE,
  484. DBG_ALL_LEVEL,
  485. _TEXT("AllocateLicenses() invalid company name\n")
  486. );
  487. SetLastError(dwStatus = TLS_E_INVALID_DATA);
  488. goto cleanup;
  489. }
  490. if(pRequest->szProductId == NULL || _tcslen(pRequest->szProductId) == 0)
  491. {
  492. DBGPrintf(
  493. DBG_ERROR,
  494. DBGLEVEL_FUNCTION_DETAILSIMPLE,
  495. DBG_ALL_LEVEL,
  496. _TEXT("AllocateLicenses() invalid product id\n")
  497. );
  498. SetLastError(dwStatus = TLS_E_INVALID_DATA);
  499. }
  500. cleanup:
  501. return dwStatus;
  502. }
  503. //----------------------------------------------------------------------
  504. DWORD
  505. AllocateLicensesFromDB(
  506. IN PTLSDbWorkSpace pDbWkSpace,
  507. IN PTLSDBAllocateRequest pRequest,
  508. IN BOOL fCheckAgreementType,
  509. IN OUT PTLSDBLicenseAllocation pAllocated
  510. )
  511. /*
  512. */
  513. {
  514. DWORD status=ERROR_SUCCESS;
  515. if(pDbWkSpace == NULL || pRequest == NULL || pAllocated == NULL)
  516. {
  517. DBGPrintf(
  518. DBG_ERROR,
  519. DBGLEVEL_FUNCTION_DETAILSIMPLE,
  520. DBG_ALL_LEVEL,
  521. _TEXT("pDbWkSpace is NULL...\n")
  522. );
  523. SetLastError(status = ERROR_INVALID_PARAMETER);
  524. TLSASSERT(FALSE);
  525. return status;
  526. }
  527. status = VerifyTLSDBAllocateRequest(pRequest);
  528. if(status != ERROR_SUCCESS)
  529. return status;
  530. if(pAllocated->dwBufSize <= 0)
  531. {
  532. DBGPrintf(
  533. DBG_ERROR,
  534. DBG_FACILITY_ALLOCATELICENSE,
  535. DBGLEVEL_FUNCTION_DETAILSIMPLE,
  536. _TEXT("AllocateLicenses() invalid return buffer size\n")
  537. );
  538. SetLastError(status = TLS_E_INVALID_DATA);
  539. return status;
  540. }
  541. #ifdef DBG
  542. DWORD dwPrevNumLicense;
  543. #endif
  544. BOOL bProductInstalled=FALSE;
  545. DWORD bufIndex=0;
  546. TLSLICENSEPACK keypack_search;
  547. TLSLICENSEPACK keypack_found;
  548. DWORD dwNumLicenses = pRequest->dwNumLicenses; // number of license wanted/returned
  549. DWORD dwTotalAllocated=0;
  550. memset(&keypack_search, 0, sizeof(keypack_search));
  551. memset(&keypack_found, 0, sizeof(keypack_found));
  552. keypack_search.ucAgreementType = pRequest->ucAgreementType;
  553. _tcscpy(keypack_search.szCompanyName, pRequest->szCompanyName);
  554. _tcscpy(keypack_search.szProductId, pRequest->szProductId);
  555. keypack_search.wMajorVersion = HIWORD(pRequest->dwVersion);
  556. keypack_search.wMinorVersion = LOWORD(pRequest->dwVersion);
  557. keypack_search.dwPlatformType = pRequest->dwPlatformId;
  558. LicPackTable& licpack_table=pDbWkSpace->m_LicPackTable;
  559. time_t current_time=time(NULL);
  560. //
  561. // Lock Key Pack table
  562. // Only update requires locking, read might get in-correct value.
  563. //
  564. //
  565. // Only allow one thread to enter - Jet not fast enough in updating entry
  566. //
  567. TLSDBLockKeyPackTable();
  568. status = TLSDBKeyPackEnumBegin(
  569. pDbWkSpace,
  570. TRUE,
  571. LSKEYPACK_SEARCH_PRODUCTID | (fCheckAgreementType ? LICENSEDPACK_FIND_LICENSEPACK : 0),
  572. &keypack_search
  573. );
  574. if(status != ERROR_SUCCESS)
  575. goto cleanup;
  576. try {
  577. while(status == ERROR_SUCCESS && dwNumLicenses != 0 && bufIndex < pAllocated->dwBufSize)
  578. {
  579. status = TLSDBKeyPackEnumNext(
  580. pDbWkSpace,
  581. &keypack_found
  582. );
  583. if(status != ERROR_SUCCESS)
  584. break;
  585. //
  586. // Skip remote keypack
  587. //
  588. if(keypack_found.ucAgreementType & LSKEYPACK_REMOTE_TYPE)
  589. {
  590. continue;
  591. }
  592. if(keypack_found.ucKeyPackStatus & LSKEYPACKSTATUS_REMOTE)
  593. {
  594. continue;
  595. }
  596. if(fCheckAgreementType
  597. && (keypack_found.ucAgreementType != pRequest->ucAgreementType))
  598. {
  599. continue;
  600. }
  601. UCHAR ucKeyPackStatus = keypack_found.ucKeyPackStatus & ~LSKEYPACKSTATUS_RESERVED;
  602. // Allocating licenses
  603. //
  604. // Throw away any key pack that has bad status
  605. // one of the reason why can't returning license in this routine
  606. // for returning license, we should not care about key pack
  607. // status.
  608. if(ucKeyPackStatus == LSKEYPACKSTATUS_UNKNOWN ||
  609. ucKeyPackStatus == LSKEYPACKSTATUS_RETURNED ||
  610. ucKeyPackStatus == LSKEYPACKSTATUS_REVOKED ||
  611. ucKeyPackStatus == LSKEYPACKSTATUS_OTHERS)
  612. {
  613. continue;
  614. }
  615. //
  616. // we find the product, make sure the version is what we want.
  617. //
  618. bProductInstalled=TRUE;
  619. // Expired keypack
  620. // TODO - update table here.
  621. if((DWORD)keypack_found.dwExpirationDate < current_time)
  622. continue;
  623. //
  624. // never allocate from older version
  625. //
  626. if( keypack_found.wMajorVersion < HIWORD(pRequest->dwVersion) )
  627. {
  628. continue;
  629. }
  630. //
  631. // Same major version but older minor
  632. //
  633. if( keypack_found.wMajorVersion == HIWORD(pRequest->dwVersion) &&
  634. keypack_found.wMinorVersion < LOWORD(pRequest->dwVersion) )
  635. {
  636. continue;
  637. }
  638. if(pRequest->dwScheme == ALLOCATE_EXACT_VERSION)
  639. {
  640. if(keypack_found.wMajorVersion != HIWORD(pRequest->dwVersion) ||
  641. keypack_found.wMinorVersion < LOWORD(pRequest->dwVersion) )
  642. {
  643. continue;
  644. }
  645. }
  646. UCHAR ucAgreementType = (keypack_found.ucAgreementType & ~LSKEYPACK_RESERVED_TYPE);
  647. //
  648. // Verify number of licenses left
  649. //
  650. if((ucAgreementType == LSKEYPACKTYPE_SELECT ||
  651. ucAgreementType == LSKEYPACKTYPE_RETAIL ||
  652. ucAgreementType == LSKEYPACKTYPE_CONCURRENT ||
  653. ucAgreementType == LSKEYPACKTYPE_OPEN) &&
  654. keypack_found.dwNumberOfLicenses == 0)
  655. {
  656. continue;
  657. }
  658. pAllocated->lpAllocateKeyPack[bufIndex] = keypack_found;
  659. #ifdef DBG
  660. dwPrevNumLicense = pAllocated->lpAllocateKeyPack[bufIndex].dwNumberOfLicenses;
  661. #endif
  662. if( ucAgreementType != LSKEYPACKTYPE_RETAIL &&
  663. ucAgreementType != LSKEYPACKTYPE_CONCURRENT &&
  664. ucAgreementType != LSKEYPACKTYPE_OPEN &&
  665. ucAgreementType != LSKEYPACKTYPE_SELECT )
  666. {
  667. // For Free/temporary license, number of available license is
  668. // how many license has been issued
  669. pAllocated->lpAllocateKeyPack[bufIndex].dwNumberOfLicenses += dwNumLicenses;
  670. pAllocated->pdwAllocationVector[bufIndex] = dwNumLicenses;
  671. dwTotalAllocated += dwNumLicenses;
  672. pAllocated->lpAllocateKeyPack[bufIndex].dwNextSerialNumber += dwNumLicenses;
  673. dwNumLicenses=0;
  674. }
  675. else
  676. {
  677. int allocated=min(dwNumLicenses, keypack_found.dwNumberOfLicenses);
  678. pAllocated->lpAllocateKeyPack[bufIndex].dwNumberOfLicenses -= allocated;
  679. dwNumLicenses -= allocated;
  680. pAllocated->pdwAllocationVector[bufIndex] = allocated;
  681. dwTotalAllocated += allocated;
  682. pAllocated->lpAllocateKeyPack[bufIndex].dwNextSerialNumber += allocated;
  683. }
  684. #if DBG
  685. DBGPrintf(
  686. DBG_INFORMATION,
  687. DBG_FACILITY_ALLOCATELICENSE,
  688. DBGLEVEL_FUNCTION_DETAILSIMPLE,
  689. _TEXT("Updating keypack %d number of license from %d to %d\n"),
  690. pAllocated->lpAllocateKeyPack[bufIndex].dwKeyPackId,
  691. dwPrevNumLicense,
  692. pAllocated->lpAllocateKeyPack[bufIndex].dwNumberOfLicenses
  693. );
  694. #endif
  695. //
  696. // Update number of licenses available for this keypack and license id in keypack
  697. //
  698. GetSystemTimeAsFileTime(&(pAllocated->lpAllocateKeyPack[bufIndex].ftLastModifyTime));
  699. if(licpack_table.UpdateRecord(
  700. pAllocated->lpAllocateKeyPack[bufIndex],
  701. LICENSEDPACK_ALLOCATE_LICENSE_UPDATE_FIELD
  702. ) == FALSE)
  703. {
  704. SetLastError(status = SET_JB_ERROR(licpack_table.GetLastJetError()));
  705. TLSASSERT(FALSE);
  706. break;
  707. }
  708. #ifdef DBG
  709. TLSLICENSEPACK test;
  710. if(licpack_table.FetchRecord(test) == FALSE)
  711. {
  712. SetLastError(status = SET_JB_ERROR(licpack_table.GetLastJetError()));
  713. TLSASSERT(FALSE);
  714. }
  715. if(test.dwKeyPackId != pAllocated->lpAllocateKeyPack[bufIndex].dwKeyPackId ||
  716. test.dwNumberOfLicenses != pAllocated->lpAllocateKeyPack[bufIndex].dwNumberOfLicenses)
  717. {
  718. TLSASSERT(FALSE);
  719. }
  720. //FreeTlsLicensePack(&test);
  721. #endif
  722. bufIndex++;
  723. }
  724. }
  725. catch(...)
  726. {
  727. SetLastError(status = TLS_E_INTERNAL);
  728. }
  729. //
  730. // terminate enumeration.
  731. //
  732. TLSDBKeyPackEnumEnd(pDbWkSpace);
  733. if(status == TLS_I_NO_MORE_DATA)
  734. {
  735. if(bufIndex != 0)
  736. {
  737. status = ERROR_SUCCESS;
  738. }
  739. else if(!bProductInstalled)
  740. {
  741. SetLastError(status = TLS_E_PRODUCT_NOTINSTALL);
  742. }
  743. }
  744. pAllocated->dwBufSize = bufIndex;
  745. pAllocated->dwTotalAllocated = dwTotalAllocated;
  746. pAllocated->dwBufSize = bufIndex;
  747. cleanup:
  748. TLSDBUnlockKeyPackTable();
  749. return status;
  750. }