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.

1188 lines
28 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996-1998
  5. //
  6. // File: keypack.cpp
  7. //
  8. // Contents:
  9. // KeyPack Table related function.
  10. //
  11. // History:
  12. // Feb. 4, 98 HueiWang Created.
  13. //---------------------------------------------------------------------------
  14. #include "pch.cpp"
  15. #include "globals.h"
  16. #include "keypack.h"
  17. #include "misc.h"
  18. //++--------------------------------------------------------------------
  19. static BOOL
  20. ValidKeyPackParameter(
  21. IN PTLSLICENSEPACK lpKeyPack,
  22. IN BOOL bAdd
  23. )
  24. /*++
  25. Abstract:
  26. Validate Licese KeyPack value
  27. Parameter:
  28. Returns:
  29. ++*/
  30. {
  31. BOOL bValid=FALSE;
  32. UCHAR ucAgreementType = (lpKeyPack->ucAgreementType & ~LSKEYPACK_RESERVED_TYPE);
  33. UCHAR ucKeyPackStatus = (lpKeyPack->ucKeyPackStatus & ~LSKEYPACKSTATUS_RESERVED);
  34. do {
  35. // verify input parameter
  36. if(ucAgreementType < LSKEYPACKTYPE_FIRST ||
  37. ucAgreementType > LSKEYPACKTYPE_LAST)
  38. {
  39. DBGPrintf(
  40. DBG_INFORMATION,
  41. DBG_FACILITY_KEYPACK,
  42. DBGLEVEL_FUNCTION_DETAILSIMPLE,
  43. _TEXT("Invalid ucKeyPackType - %d\n"),
  44. lpKeyPack->ucAgreementType
  45. );
  46. break;
  47. }
  48. if((ucKeyPackStatus < LSKEYPACKSTATUS_FIRST ||
  49. ucKeyPackStatus > LSKEYPACKSTATUS_LAST) &&
  50. ucKeyPackStatus != LSKEYPACKSTATUS_DELETE)
  51. {
  52. DBGPrintf(
  53. DBG_INFORMATION,
  54. DBG_FACILITY_KEYPACK,
  55. DBGLEVEL_FUNCTION_DETAILSIMPLE,
  56. _TEXT("Invalid ucKeyPackStatus - %d\n"),
  57. lpKeyPack->ucKeyPackStatus
  58. );
  59. break;
  60. }
  61. if(lpKeyPack->ucLicenseType < LSKEYPACKLICENSETYPE_FIRST ||
  62. lpKeyPack->ucLicenseType > LSKEYPACKLICENSETYPE_LAST)
  63. {
  64. DBGPrintf(
  65. DBG_INFORMATION,
  66. DBG_FACILITY_KEYPACK,
  67. DBGLEVEL_FUNCTION_DETAILSIMPLE,
  68. _TEXT("Invalid ucLicenseType - %d\n"),
  69. lpKeyPack->ucLicenseType
  70. );
  71. break;
  72. }
  73. if(!bAdd)
  74. {
  75. bValid = TRUE;
  76. break;
  77. }
  78. //
  79. // Following value is required for adding entry into keypack
  80. //
  81. if(lpKeyPack->ucChannelOfPurchase < LSKEYPACKCHANNELOFPURCHASE_FIRST ||
  82. lpKeyPack->ucChannelOfPurchase > LSKEYPACKCHANNELOFPURCHASE_LAST)
  83. {
  84. DBGPrintf(
  85. DBG_INFORMATION,
  86. DBG_FACILITY_KEYPACK,
  87. DBGLEVEL_FUNCTION_DETAILSIMPLE,
  88. _TEXT("Invalid ucChannelOfPurchase - %d\n"),
  89. lpKeyPack->ucChannelOfPurchase
  90. );
  91. break;
  92. }
  93. if(!_tcslen(lpKeyPack->szCompanyName))
  94. {
  95. DBGPrintf(
  96. DBG_INFORMATION,
  97. DBG_FACILITY_KEYPACK,
  98. DBGLEVEL_FUNCTION_DETAILSIMPLE,
  99. _TEXT("Null szCompanyName\n")
  100. );
  101. break;
  102. }
  103. if(!_tcslen(lpKeyPack->szKeyPackId))
  104. {
  105. DBGPrintf(
  106. DBG_INFORMATION,
  107. DBG_FACILITY_KEYPACK,
  108. DBGLEVEL_FUNCTION_DETAILSIMPLE,
  109. _TEXT("Null szKeyPackId\n")
  110. );
  111. break;
  112. }
  113. if(!_tcslen(lpKeyPack->szProductId))
  114. {
  115. DBGPrintf(
  116. DBG_INFORMATION,
  117. DBG_FACILITY_KEYPACK,
  118. DBGLEVEL_FUNCTION_DETAILSIMPLE,
  119. _TEXT("Null szProductId\n")
  120. );
  121. break;
  122. }
  123. if(!_tcslen(lpKeyPack->szBeginSerialNumber))
  124. {
  125. DBGPrintf(
  126. DBG_INFORMATION,
  127. DBG_FACILITY_KEYPACK,
  128. DBGLEVEL_FUNCTION_DETAILSIMPLE,
  129. _TEXT("Null szBeginSerialNumber\n")
  130. );
  131. break;
  132. }
  133. bValid=TRUE;
  134. } while(FALSE);
  135. return bValid;
  136. }
  137. //++-----------------------------------------------------------
  138. void
  139. TLSDBLockKeyPackTable()
  140. /*++
  141. Abstract:
  142. Lock both LicPack and LicPackStatus table.
  143. Parameter:
  144. None.
  145. ++*/
  146. {
  147. DBGPrintf(
  148. DBG_INFORMATION,
  149. DBG_FACILITY_LOCK,
  150. DBGLEVEL_FUNCTION_TRACE,
  151. _TEXT("Locking table KeyPackTable\n")
  152. );
  153. LicPackTable::LockTable();
  154. }
  155. //++-----------------------------------------------------------
  156. void
  157. TLSDBUnlockKeyPackTable()
  158. /*++
  159. Abstract:
  160. Unlock both LicPack and LicPackStatus table.
  161. Parameter:
  162. None:
  163. ++*/
  164. {
  165. DBGPrintf(
  166. DBG_INFORMATION,
  167. DBG_FACILITY_LOCK,
  168. DBGLEVEL_FUNCTION_TRACE,
  169. _TEXT("Unlocking table KeyPackTable\n")
  170. );
  171. LicPackTable::UnlockTable();
  172. }
  173. //++--------------------------------------------------------------
  174. DWORD
  175. TLSDBKeyPackFind(
  176. IN PTLSDbWorkSpace pDbWkSpace,
  177. IN BOOL bMatchAllParm,
  178. IN DWORD dwSearchParm,
  179. IN PTLSLICENSEPACK lpKeyPack,
  180. IN OUT PTLSLICENSEPACK lpFound
  181. )
  182. /*++
  183. Abstract:
  184. Find a license pack based on search parameter.
  185. Parameter:
  186. pDbWkSpace : workspace handle.
  187. bMatchAllParm : TRUE if match all parameter, FALSE otherwise.
  188. dwSearchParm : search parameter.
  189. lpKeyPack : value to search for.
  190. lpFound : record found.
  191. Return:
  192. ++*/
  193. {
  194. DWORD dwStatus=ERROR_SUCCESS;
  195. BOOL bSuccess;
  196. if(pDbWkSpace == NULL || lpKeyPack == NULL)
  197. {
  198. SetLastError(dwStatus = ERROR_INVALID_PARAMETER);
  199. TLSASSERT(FALSE);
  200. return dwStatus;
  201. }
  202. LicPackTable& licpackTable=pDbWkSpace->m_LicPackTable;
  203. //
  204. // Find the record in LicPack table
  205. //
  206. bSuccess = licpackTable.FindRecord(
  207. bMatchAllParm,
  208. dwSearchParm,
  209. *lpKeyPack,
  210. *lpFound
  211. );
  212. if(bSuccess != TRUE)
  213. {
  214. if(licpackTable.GetLastJetError() == JET_errRecordNotFound)
  215. {
  216. SetLastError(dwStatus = TLS_E_RECORD_NOTFOUND);
  217. }
  218. else
  219. {
  220. LPTSTR pString = NULL;
  221. TLSGetESEError(licpackTable.GetLastJetError(), &pString);
  222. TLSLogEvent(
  223. EVENTLOG_ERROR_TYPE,
  224. TLS_E_DBGENERAL,
  225. TLS_E_JB_BASE,
  226. licpackTable.GetLastJetError(),
  227. (pString != NULL) ? pString : _TEXT("")
  228. );
  229. if(pString != NULL)
  230. {
  231. LocalFree(pString);
  232. }
  233. SetLastError(
  234. dwStatus = (SET_JB_ERROR(licpackTable.GetLastJetError()))
  235. );
  236. TLSASSERT(licpackTable.GetLastJetError() == JET_errRecordNotFound);
  237. }
  238. }
  239. return dwStatus;
  240. }
  241. //++-------------------------------------------------------------------
  242. DWORD
  243. TLSDBKeyPackAddEntry(
  244. IN PTLSDbWorkSpace pDbWkSpace,
  245. IN PTLSLICENSEPACK lpKeyPack
  246. )
  247. /*++
  248. Abstract:
  249. Add an entry into LicPack and LicPackStatus table.
  250. Parameter:
  251. pDbWkSpace :
  252. lpKeyPack ;
  253. Returns:
  254. ++*/
  255. {
  256. DWORD dwStatus = ERROR_SUCCESS;
  257. TLSLICENSEPACK found;
  258. BOOL bSuccess;
  259. LicPackTable& licpackTable=pDbWkSpace->m_LicPackTable;
  260. found.pbDomainSid = NULL;
  261. found.cbDomainSid = 0;
  262. //
  263. //
  264. //
  265. TLSDBLockKeyPackTable();
  266. TLSASSERT(pDbWkSpace != NULL && lpKeyPack != NULL);
  267. if(pDbWkSpace == NULL || lpKeyPack == NULL)
  268. {
  269. SetLastError(dwStatus = ERROR_INVALID_PARAMETER);
  270. goto cleanup;
  271. }
  272. // lpKeyPack->pbDomainSid == NULL || lpKeyPack->cbDomainSid == 0 ||
  273. if( _tcslen(lpKeyPack->szInstallId) == 0 || _tcslen(lpKeyPack->szTlsServerName) == 0 )
  274. {
  275. TLSASSERT(FALSE);
  276. SetLastError(dwStatus = ERROR_INVALID_PARAMETER);
  277. goto cleanup;
  278. }
  279. //
  280. // make sure no duplicate entry via primary index
  281. dwStatus = TLSDBKeyPackFind(
  282. pDbWkSpace,
  283. TRUE,
  284. LICENSEDPACK_PROCESS_DWINTERNAL,
  285. lpKeyPack,
  286. &found
  287. );
  288. if(dwStatus == ERROR_SUCCESS)
  289. {
  290. dwStatus = TLS_E_DUPLICATE_RECORD;
  291. goto cleanup;
  292. }
  293. dwStatus = ERROR_SUCCESS;
  294. //
  295. // Mark internal field -
  296. //
  297. switch( (lpKeyPack->ucAgreementType & ~LSKEYPACK_RESERVED_TYPE) )
  298. {
  299. case LSKEYPACKTYPE_SELECT:
  300. case LSKEYPACKTYPE_RETAIL:
  301. case LSKEYPACKTYPE_CONCURRENT:
  302. case LSKEYPACKTYPE_OPEN:
  303. // number of license available to client
  304. lpKeyPack->dwNumberOfLicenses = lpKeyPack->dwTotalLicenseInKeyPack;
  305. break;
  306. default:
  307. // number of licenses issued.
  308. lpKeyPack->dwNumberOfLicenses = 0;
  309. break;
  310. }
  311. //
  312. // Begin serial number in this keypack
  313. //
  314. lpKeyPack->dwNextSerialNumber = 1;
  315. GetSystemTimeAsFileTime(&(lpKeyPack->ftLastModifyTime));
  316. //
  317. // Mark beta keypack.
  318. //
  319. if(TLSIsBetaNTServer() == TRUE)
  320. {
  321. lpKeyPack->ucKeyPackStatus |= LSKEYPACKSTATUS_BETA;
  322. }
  323. //
  324. // insert the record
  325. //
  326. bSuccess = licpackTable.InsertRecord(
  327. *lpKeyPack
  328. );
  329. if(bSuccess == FALSE)
  330. {
  331. if(licpackTable.GetLastJetError() == JET_errKeyDuplicate)
  332. {
  333. SetLastError(dwStatus=TLS_E_DUPLICATE_RECORD);
  334. }
  335. else
  336. {
  337. LPTSTR pString = NULL;
  338. TLSGetESEError(licpackTable.GetLastJetError(), &pString);
  339. TLSLogEvent(
  340. EVENTLOG_ERROR_TYPE,
  341. TLS_E_DBGENERAL,
  342. TLS_E_JB_BASE,
  343. licpackTable.GetLastJetError(),
  344. (pString != NULL) ? pString : _TEXT("")
  345. );
  346. if(pString != NULL)
  347. {
  348. LocalFree(pString);
  349. }
  350. SetLastError(dwStatus = SET_JB_ERROR(licpackTable.GetLastJetError()));
  351. TLSASSERT(FALSE);
  352. }
  353. }
  354. cleanup:
  355. TLSDBUnlockKeyPackTable();
  356. SetLastError(dwStatus);
  357. return dwStatus;
  358. }
  359. //-----------------------------------------------------
  360. DWORD
  361. TLSDBKeyPackDeleteEntry(
  362. IN PTLSDbWorkSpace pDbWkSpace,
  363. IN BOOL bDeleteAllRecord,
  364. IN PTLSLICENSEPACK lpKeyPack
  365. )
  366. /*++
  367. Abstract:
  368. Delete an entry from both LicPack and LicPackStatus table.
  369. Parameter:
  370. pDbWkSpace : workspace handle.
  371. bDeleteAllRecord : Delete all record with same ID.
  372. lpKeyPack : record to be delete.
  373. Returns:
  374. Note:
  375. If not deleting same record, current record must be point
  376. to the record that going to be deleted.
  377. ++*/
  378. {
  379. DWORD dwStatus=ERROR_SUCCESS;
  380. BOOL bSuccess;
  381. LicPackTable& licpackTable = pDbWkSpace->m_LicPackTable;
  382. GetSystemTimeAsFileTime(&(lpKeyPack->ftLastModifyTime));
  383. //
  384. // BACKUP - Need to update this record to deleted state instead of delete it.
  385. //
  386. if(bDeleteAllRecord == TRUE)
  387. {
  388. //
  389. // Delete record from LicPack Table.
  390. //
  391. bSuccess = licpackTable.DeleteAllRecord(
  392. TRUE,
  393. LICENSEDPACK_PROCESS_DWINTERNAL,
  394. *lpKeyPack
  395. );
  396. }
  397. else
  398. {
  399. bSuccess = licpackTable.DeleteRecord();
  400. }
  401. if(bSuccess == FALSE)
  402. {
  403. SetLastError(dwStatus = SET_JB_ERROR(licpackTable.GetLastJetError()));
  404. if(licpackTable.GetLastJetError() != JET_errRecordNotFound)
  405. {
  406. LPTSTR pString = NULL;
  407. TLSGetESEError(licpackTable.GetLastJetError(), &pString);
  408. TLSLogEvent(
  409. EVENTLOG_ERROR_TYPE,
  410. TLS_E_DBGENERAL,
  411. TLS_E_JB_BASE,
  412. licpackTable.GetLastJetError(),
  413. (pString != NULL) ? pString : _TEXT("")
  414. );
  415. if(pString != NULL)
  416. {
  417. LocalFree(pString);
  418. }
  419. TLSASSERT(FALSE);
  420. }
  421. }
  422. return dwStatus;
  423. }
  424. //-----------------------------------------------------
  425. DWORD
  426. TLSDBKeyPackUpdateEntry(
  427. IN PTLSDbWorkSpace pDbWkSpace,
  428. IN BOOL bPointerOnRecord,
  429. IN DWORD dwUpdateParm,
  430. IN PTLSLICENSEPACK lpKeyPack
  431. )
  432. /*
  433. */
  434. {
  435. DWORD dwStatus = ERROR_SUCCESS;
  436. BOOL bSuccess;
  437. TLSLICENSEPACK found;
  438. LicPackTable& licpackTable=pDbWkSpace->m_LicPackTable;
  439. if(bPointerOnRecord == FALSE)
  440. {
  441. found.pbDomainSid = NULL;
  442. dwStatus = TLSDBKeyPackFind(
  443. pDbWkSpace,
  444. TRUE,
  445. LICENSEDPACK_PROCESS_DWINTERNAL,
  446. lpKeyPack,
  447. &found
  448. );
  449. lpKeyPack->dwKeyPackId = found.dwKeyPackId;
  450. if(dwStatus != ERROR_SUCCESS)
  451. {
  452. TLSASSERT(FALSE);
  453. goto cleanup;
  454. }
  455. }
  456. #if DBG
  457. {
  458. //
  459. // try to cache some bug...
  460. //
  461. TLSLICENSEPACK found1;
  462. found.pbDomainSid = NULL;
  463. bSuccess = licpackTable.FetchRecord( found1, 0xFFFFFFFF );
  464. //
  465. // Make sure we update right record
  466. if( found1.dwKeyPackId != lpKeyPack->dwKeyPackId )
  467. {
  468. TLSASSERT(FALSE);
  469. }
  470. //
  471. // check input parameter
  472. if( ValidKeyPackParameter( lpKeyPack, FALSE ) == FALSE )
  473. {
  474. TLSASSERT(FALSE);
  475. }
  476. }
  477. #endif
  478. //
  479. // Update the timestamp for this record.
  480. //
  481. GetSystemTimeAsFileTime(&(lpKeyPack->ftLastModifyTime));
  482. bSuccess = licpackTable.UpdateRecord(
  483. *lpKeyPack,
  484. (dwUpdateParm & ~LICENSEDPACK_PROCESS_DWINTERNAL) | LICENSEDPACK_PROCESS_MODIFYTIME
  485. );
  486. if(bSuccess == FALSE)
  487. {
  488. LPTSTR pString = NULL;
  489. TLSGetESEError(licpackTable.GetLastJetError(), &pString);
  490. TLSLogEvent(
  491. EVENTLOG_ERROR_TYPE,
  492. TLS_E_DBGENERAL,
  493. TLS_E_JB_BASE,
  494. licpackTable.GetLastJetError(),
  495. (pString != NULL) ? pString : _TEXT("")
  496. );
  497. if(pString != NULL)
  498. {
  499. LocalFree(pString);
  500. }
  501. SetLastError(dwStatus = SET_JB_ERROR(licpackTable.GetLastJetError()));
  502. TLSASSERT(FALSE);
  503. }
  504. cleanup:
  505. return dwStatus;
  506. }
  507. //-----------------------------------------------------
  508. DWORD
  509. TLSDBKeyPackUpdateNumOfAvailableLicense(
  510. IN PTLSDbWorkSpace pDbWkSpace,
  511. IN BOOL bAdd,
  512. IN PTLSLICENSEPACK lpKeyPack
  513. )
  514. /*++
  515. Abstract:
  516. Update number of license available .
  517. Parameter:
  518. pDbWkSpace : workspace handle.
  519. bAdd : TRUE if add license
  520. lpKeyPack :
  521. Returns:
  522. ++*/
  523. {
  524. DWORD dwStatus=ERROR_SUCCESS;
  525. if(pDbWkSpace == NULL || lpKeyPack == NULL)
  526. {
  527. SetLastError(dwStatus = ERROR_INVALID_PARAMETER);
  528. TLSASSERT(pDbWkSpace != NULL);
  529. return dwStatus;
  530. }
  531. TLSLICENSEPACK found;
  532. DWORD dwNumberLicenseToAddRemove;
  533. BOOL bRemoveMoreThanAvailable=FALSE;
  534. found.pbDomainSid = NULL;
  535. TLSDBLockKeyPackTable();
  536. dwStatus = TLSDBKeyPackFind(
  537. pDbWkSpace,
  538. TRUE,
  539. LICENSEDPACK_PROCESS_DWINTERNAL,
  540. lpKeyPack,
  541. &found
  542. );
  543. if(dwStatus != ERROR_SUCCESS)
  544. {
  545. goto cleanup;
  546. }
  547. dwNumberLicenseToAddRemove = lpKeyPack->dwTotalLicenseInKeyPack;
  548. *lpKeyPack = found;
  549. if(dwNumberLicenseToAddRemove == 0)
  550. {
  551. // query only, a hook so that test program can get actual
  552. // numbers - Jet not re-reading MDB file problem
  553. goto cleanup;
  554. }
  555. //
  556. // Only allow add/remove license on retail
  557. //
  558. if( (found.ucAgreementType & ~LSKEYPACK_RESERVED_TYPE) != LSKEYPACKTYPE_RETAIL &&
  559. (found.ucAgreementType & ~LSKEYPACK_RESERVED_TYPE) != LSKEYPACKTYPE_CONCURRENT)
  560. {
  561. DBGPrintf(
  562. DBG_INFORMATION,
  563. DBG_FACILITY_KEYPACK,
  564. DBGLEVEL_FUNCTION_DETAILSIMPLE,
  565. _TEXT("LSDBKeyPackUpdate : Invalid parameter...\n")
  566. );
  567. SetLastError(dwStatus = TLS_E_INVALID_DATA);
  568. goto cleanup;
  569. }
  570. if(bAdd)
  571. {
  572. // increase both total and number available
  573. found.dwNumberOfLicenses += dwNumberLicenseToAddRemove;
  574. found.dwTotalLicenseInKeyPack += dwNumberLicenseToAddRemove;
  575. }
  576. else
  577. {
  578. //
  579. // if number of licenses to remove is greater that what's available,
  580. // remove all available license and set the return code to invalid data.
  581. //
  582. if(found.dwNumberOfLicenses < dwNumberLicenseToAddRemove)
  583. {
  584. bRemoveMoreThanAvailable = TRUE;
  585. }
  586. dwNumberLicenseToAddRemove = min(dwNumberLicenseToAddRemove, found.dwNumberOfLicenses);
  587. found.dwNumberOfLicenses -= dwNumberLicenseToAddRemove;
  588. found.dwTotalLicenseInKeyPack -= dwNumberLicenseToAddRemove;
  589. }
  590. dwStatus = TLSDBKeyPackSetValues(
  591. pDbWkSpace,
  592. TRUE,
  593. LSKEYPACK_SEARCH_TOTALLICENSES | LSKEYPACK_EXSEARCH_AVAILABLE,
  594. &found
  595. );
  596. *lpKeyPack = found;
  597. cleanup:
  598. TLSDBUnlockKeyPackTable();
  599. if(dwStatus == ERROR_SUCCESS && bRemoveMoreThanAvailable)
  600. SetLastError(dwStatus = TLS_W_REMOVE_TOOMANY);
  601. return dwStatus;
  602. }
  603. //+-----------------------------------------------------------------
  604. DWORD
  605. TLSDBKeyPackAdd(
  606. IN PTLSDbWorkSpace pDbWkSpace,
  607. IN OUT PTLSLICENSEPACK lpKeyPack // return internal tracking ID
  608. )
  609. /*++
  610. Abstract:
  611. Install a product keypack into keypack table, routine only set
  612. keypack status to pending, must call corresponding set status
  613. routine to activate key pack.
  614. Parameters:
  615. pDbWkSpace : Workspace handle.
  616. lpKeyPack : value to be inserted.
  617. Returns:
  618. ++*/
  619. {
  620. DWORD lNextKeyPackId;
  621. DWORD dwStatus;
  622. if(!ValidKeyPackParameter(lpKeyPack, TRUE))
  623. {
  624. SetLastError(TLS_E_INVALID_DATA);
  625. return TLS_E_INVALID_DATA;
  626. }
  627. TLSLICENSEPACK found;
  628. TLSDBLockKeyPackTable();
  629. found.pbDomainSid = NULL;
  630. //
  631. // No duplicate entry for product installed
  632. //
  633. dwStatus = TLSDBKeyPackFind(
  634. pDbWkSpace,
  635. TRUE,
  636. LICENSEDPACK_FIND_PRODUCT,
  637. lpKeyPack,
  638. &found
  639. );
  640. if(dwStatus == ERROR_SUCCESS)
  641. {
  642. // Product already installed
  643. switch( (found.ucKeyPackStatus & ~LSKEYPACKSTATUS_RESERVED) )
  644. {
  645. case LSKEYPACKSTATUS_TEMPORARY:
  646. // case 1 : keypack is installed by temporary license
  647. if(found.ucAgreementType == lpKeyPack->ucAgreementType)
  648. {
  649. dwStatus = TLS_E_DUPLICATE_RECORD;
  650. }
  651. break;
  652. case LSKEYPACKSTATUS_ACTIVE:
  653. case LSKEYPACKSTATUS_PENDING:
  654. // case 2 : duplicate entry
  655. dwStatus = TLS_E_DUPLICATE_RECORD;
  656. break;
  657. case LSKEYPACKSTATUS_RETURNED:
  658. case LSKEYPACKSTATUS_REVOKED:
  659. case LSKEYPACKSTATUS_OTHERS:
  660. // de-activated license key pack.
  661. // keep it.
  662. break;
  663. default:
  664. dwStatus = TLS_E_CORRUPT_DATABASE;
  665. #if DBG
  666. TLSASSERT(FALSE);
  667. #endif
  668. }
  669. if(dwStatus != ERROR_SUCCESS)
  670. goto cleanup;
  671. }
  672. else if(dwStatus == TLS_E_RECORD_NOTFOUND)
  673. {
  674. //
  675. // Always use new keypack ID
  676. // temporary license will be deleted after all license
  677. // has been returned.
  678. //
  679. lpKeyPack->dwKeyPackId = TLSDBGetNextKeyPackId();
  680. dwStatus = TLSDBKeyPackAddEntry(
  681. pDbWkSpace,
  682. lpKeyPack
  683. );
  684. }
  685. cleanup:
  686. TLSDBUnlockKeyPackTable();
  687. return dwStatus;
  688. }
  689. //+-------------------------------------------------------------------
  690. DWORD
  691. TLSDBKeyPackEnumBegin(
  692. IN PTLSDbWorkSpace pDbWkSpace,
  693. IN BOOL bMatchAll,
  694. IN DWORD dwSearchParm,
  695. IN PTLSLICENSEPACK lpSearch
  696. )
  697. /*
  698. Abstract:
  699. Begin enumeration of licensepack table.
  700. Parameter:
  701. pDbWkSpace : workspace handle.
  702. bMatchAll : match all search value.
  703. dwSearchParm : value to search.
  704. lpSearch : search value
  705. Return:
  706. */
  707. {
  708. DWORD dwStatus = ERROR_SUCCESS;
  709. BOOL bSuccess = TRUE;
  710. if(pDbWkSpace == NULL)
  711. {
  712. SetLastError(dwStatus = ERROR_INVALID_PARAMETER);
  713. TLSASSERT(FALSE);
  714. return dwStatus;
  715. }
  716. LicPackTable& licpackTable=pDbWkSpace->m_LicPackTable;
  717. bSuccess = licpackTable.EnumerateBegin(
  718. bMatchAll,
  719. dwSearchParm,
  720. lpSearch
  721. );
  722. if(bSuccess == FALSE)
  723. {
  724. LPTSTR pString = NULL;
  725. TLSGetESEError(licpackTable.GetLastJetError(), &pString);
  726. TLSLogEvent(
  727. EVENTLOG_ERROR_TYPE,
  728. TLS_E_DBGENERAL,
  729. TLS_E_JB_BASE,
  730. licpackTable.GetLastJetError(),
  731. (pString != NULL) ? pString : _TEXT("")
  732. );
  733. if(pString != NULL)
  734. {
  735. LocalFree(pString);
  736. }
  737. SetLastError(dwStatus = SET_JB_ERROR(licpackTable.GetLastJetError()));
  738. TLSASSERT(FALSE);
  739. }
  740. return dwStatus;
  741. }
  742. //+-------------------------------------------------------------------
  743. DWORD
  744. TLSDBKeyPackEnumNext(
  745. IN PTLSDbWorkSpace pDbWkSpace,
  746. IN OUT PTLSLICENSEPACK lpKeyPack
  747. )
  748. /*++
  749. Abstract:
  750. Fetch next row of record that match search condition.
  751. Parameters:
  752. pDbWkSpace : workspace handle.
  753. lpKeyPack : return founded keypack.
  754. Return:
  755. ++*/
  756. {
  757. DWORD dwStatus = ERROR_SUCCESS;
  758. if(pDbWkSpace == NULL || lpKeyPack == NULL)
  759. {
  760. SetLastError(dwStatus = ERROR_INVALID_PARAMETER);
  761. TLSASSERT(FALSE);
  762. return dwStatus;
  763. }
  764. //FreeTlsLicensePack(lpKeyPack);
  765. LicPackTable& licpackTable=pDbWkSpace->m_LicPackTable;
  766. switch(licpackTable.EnumerateNext(*lpKeyPack))
  767. {
  768. case RECORD_ENUM_ERROR:
  769. {
  770. LPTSTR pString = NULL;
  771. TLSGetESEError(licpackTable.GetLastJetError(), &pString);
  772. TLSLogEvent(
  773. EVENTLOG_ERROR_TYPE,
  774. TLS_E_DBGENERAL,
  775. TLS_E_JB_BASE,
  776. licpackTable.GetLastJetError(),
  777. (pString != NULL) ? pString : _TEXT("")
  778. );
  779. if(pString != NULL)
  780. {
  781. LocalFree(pString);
  782. }
  783. }
  784. dwStatus = SET_JB_ERROR(licpackTable.GetLastJetError());
  785. TLSASSERT(FALSE);
  786. break;
  787. case RECORD_ENUM_MORE_DATA:
  788. dwStatus = ERROR_SUCCESS;
  789. break;
  790. case RECORD_ENUM_END:
  791. dwStatus = TLS_I_NO_MORE_DATA;
  792. }
  793. return dwStatus;
  794. }
  795. //+-------------------------------------------------------------------
  796. void
  797. TLSDBKeyPackEnumEnd(
  798. IN PTLSDbWorkSpace pDbWkSpace
  799. )
  800. /*++
  801. Abstract:
  802. End enumeration of licensepack.
  803. Parameter:
  804. pDbWkSpace : workspace handle.
  805. ++*/
  806. {
  807. TLSASSERT(pDbWkSpace != NULL);
  808. pDbWkSpace->m_LicPackTable.EnumerateEnd();
  809. return;
  810. }
  811. //+-------------------------------------------------------------------
  812. DWORD
  813. TLSDBKeyPackSetValues(
  814. IN PTLSDbWorkSpace pDbWkSpace,
  815. IN BOOL bPointerOnRecord,
  816. IN DWORD dwSetParm,
  817. IN PTLSLICENSEPACK lpKeyPack
  818. )
  819. /*++
  820. Abstract:
  821. Set column value of a license pack record.
  822. Parameter;
  823. pDbWkSpace : workspace handle.
  824. bInternal : call is from internal routine, no error checking.
  825. dwSetParm : Columns to be set.
  826. lpKeyPack : value to be set.
  827. Returns.
  828. ++*/
  829. {
  830. DWORD dwStatus=ERROR_SUCCESS;
  831. if(pDbWkSpace == NULL || lpKeyPack == NULL)
  832. {
  833. SetLastError(dwStatus = ERROR_INVALID_PARAMETER);
  834. TLSASSERT(FALSE);
  835. goto cleanup;
  836. }
  837. if(bPointerOnRecord == FALSE)
  838. {
  839. TLSLICENSEPACK found;
  840. // Find this keypack's internal keypack id
  841. dwStatus = TLSDBKeyPackFind(
  842. pDbWkSpace,
  843. TRUE,
  844. LICENSEDPACK_FIND_PRODUCT,
  845. lpKeyPack,
  846. &found
  847. );
  848. //FreeTlsLicensePack(&found);
  849. if(dwStatus != ERROR_SUCCESS)
  850. {
  851. goto cleanup;
  852. }
  853. lpKeyPack->dwKeyPackId = found.dwKeyPackId;
  854. }
  855. if(lpKeyPack->ucKeyPackStatus == LSKEYPACKSTATUS_DELETE)
  856. {
  857. dwStatus = TLSDBKeyPackDeleteEntry(
  858. pDbWkSpace,
  859. TRUE, // delete all records with same ID
  860. lpKeyPack
  861. );
  862. }
  863. else
  864. {
  865. dwStatus = TLSDBKeyPackUpdateEntry(
  866. pDbWkSpace,
  867. TRUE,
  868. dwSetParm,
  869. lpKeyPack
  870. );
  871. }
  872. cleanup:
  873. return dwStatus;
  874. }
  875. //+-------------------------------------------------------------------
  876. DWORD
  877. TLSDBKeyPackSetStatus(
  878. IN PTLSDbWorkSpace pDbWkSpace,
  879. IN DWORD dwSetStatus,
  880. IN PTLSLICENSEPACK lpKeyPack
  881. )
  882. /*++
  883. Abstract:
  884. Stub routine for RPC to set status of a keypack
  885. ++*/
  886. {
  887. return TLSDBKeyPackSetValues(
  888. pDbWkSpace,
  889. FALSE,
  890. dwSetStatus,
  891. lpKeyPack
  892. );
  893. }
  894. //+-------------------------------------------------------------------
  895. DWORD
  896. TLSDBKeyPackGetAvailableLicenses(
  897. IN PTLSDbWorkSpace pDbWkSpace,
  898. IN DWORD dwSearchParm,
  899. IN PTLSLICENSEPACK lplsKeyPack,
  900. IN OUT LPDWORD lpdwAvail
  901. )
  902. /*++
  903. Abstract:
  904. retrieve number of available licenses for the key pack.
  905. Parameter:
  906. pDbWkSpace : workspace handle.
  907. dwSearchParm : search parameters.
  908. lpLsKeyPack : search value.
  909. lpdwAvail : return number of available licenses.
  910. Return:
  911. ++*/
  912. {
  913. DWORD dwStatus = ERROR_SUCCESS;
  914. TLSLICENSEPACK found;
  915. dwStatus = TLSDBKeyPackFind(
  916. pDbWkSpace,
  917. TRUE,
  918. dwSearchParm,
  919. lplsKeyPack,
  920. &found
  921. );
  922. if(dwStatus == ERROR_SUCCESS)
  923. {
  924. switch(found.ucAgreementType)
  925. {
  926. case LSKEYPACKTYPE_SELECT:
  927. case LSKEYPACKTYPE_RETAIL:
  928. case LSKEYPACKTYPE_CONCURRENT:
  929. case LSKEYPACKTYPE_OPEN:
  930. *lpdwAvail = found.dwNumberOfLicenses;
  931. break;
  932. default:
  933. *lpdwAvail = LONG_MAX;
  934. }
  935. }
  936. //FreeTlsLicensePack(&found);
  937. return dwStatus;
  938. }