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.

1184 lines
29 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_OPEN:
  302. // number of license available to client
  303. lpKeyPack->dwNumberOfLicenses = lpKeyPack->dwTotalLicenseInKeyPack;
  304. break;
  305. default:
  306. // number of licenses issued.
  307. lpKeyPack->dwNumberOfLicenses = 0;
  308. break;
  309. }
  310. //
  311. // Begin serial number in this keypack
  312. //
  313. lpKeyPack->dwNextSerialNumber = 1;
  314. GetSystemTimeAsFileTime(&(lpKeyPack->ftLastModifyTime));
  315. //
  316. // Mark beta keypack.
  317. //
  318. if(TLSIsBetaNTServer() == TRUE)
  319. {
  320. lpKeyPack->ucKeyPackStatus |= LSKEYPACKSTATUS_BETA;
  321. }
  322. //
  323. // insert the record
  324. //
  325. bSuccess = licpackTable.InsertRecord(
  326. *lpKeyPack
  327. );
  328. if(bSuccess == FALSE)
  329. {
  330. if(licpackTable.GetLastJetError() == JET_errKeyDuplicate)
  331. {
  332. SetLastError(dwStatus=TLS_E_DUPLICATE_RECORD);
  333. }
  334. else
  335. {
  336. LPTSTR pString = NULL;
  337. TLSGetESEError(licpackTable.GetLastJetError(), &pString);
  338. TLSLogEvent(
  339. EVENTLOG_ERROR_TYPE,
  340. TLS_E_DBGENERAL,
  341. TLS_E_JB_BASE,
  342. licpackTable.GetLastJetError(),
  343. (pString != NULL) ? pString : _TEXT("")
  344. );
  345. if(pString != NULL)
  346. {
  347. LocalFree(pString);
  348. }
  349. SetLastError(dwStatus = SET_JB_ERROR(licpackTable.GetLastJetError()));
  350. TLSASSERT(FALSE);
  351. }
  352. }
  353. cleanup:
  354. TLSDBUnlockKeyPackTable();
  355. SetLastError(dwStatus);
  356. return dwStatus;
  357. }
  358. //-----------------------------------------------------
  359. DWORD
  360. TLSDBKeyPackDeleteEntry(
  361. IN PTLSDbWorkSpace pDbWkSpace,
  362. IN BOOL bDeleteAllRecord,
  363. IN PTLSLICENSEPACK lpKeyPack
  364. )
  365. /*++
  366. Abstract:
  367. Delete an entry from both LicPack and LicPackStatus table.
  368. Parameter:
  369. pDbWkSpace : workspace handle.
  370. bDeleteAllRecord : Delete all record with same ID.
  371. lpKeyPack : record to be delete.
  372. Returns:
  373. Note:
  374. If not deleting same record, current record must be point
  375. to the record that going to be deleted.
  376. ++*/
  377. {
  378. DWORD dwStatus=ERROR_SUCCESS;
  379. BOOL bSuccess;
  380. LicPackTable& licpackTable = pDbWkSpace->m_LicPackTable;
  381. GetSystemTimeAsFileTime(&(lpKeyPack->ftLastModifyTime));
  382. //
  383. // BACKUP - Need to update this record to deleted state instead of delete it.
  384. //
  385. if(bDeleteAllRecord == TRUE)
  386. {
  387. //
  388. // Delete record from LicPack Table.
  389. //
  390. bSuccess = licpackTable.DeleteAllRecord(
  391. TRUE,
  392. LICENSEDPACK_PROCESS_DWINTERNAL,
  393. *lpKeyPack
  394. );
  395. }
  396. else
  397. {
  398. bSuccess = licpackTable.DeleteRecord();
  399. }
  400. if(bSuccess == FALSE)
  401. {
  402. SetLastError(dwStatus = SET_JB_ERROR(licpackTable.GetLastJetError()));
  403. if(licpackTable.GetLastJetError() != JET_errRecordNotFound)
  404. {
  405. LPTSTR pString = NULL;
  406. TLSGetESEError(licpackTable.GetLastJetError(), &pString);
  407. TLSLogEvent(
  408. EVENTLOG_ERROR_TYPE,
  409. TLS_E_DBGENERAL,
  410. TLS_E_JB_BASE,
  411. licpackTable.GetLastJetError(),
  412. (pString != NULL) ? pString : _TEXT("")
  413. );
  414. if(pString != NULL)
  415. {
  416. LocalFree(pString);
  417. }
  418. TLSASSERT(FALSE);
  419. }
  420. }
  421. return dwStatus;
  422. }
  423. //-----------------------------------------------------
  424. DWORD
  425. TLSDBKeyPackUpdateEntry(
  426. IN PTLSDbWorkSpace pDbWkSpace,
  427. IN BOOL bPointerOnRecord,
  428. IN DWORD dwUpdateParm,
  429. IN PTLSLICENSEPACK lpKeyPack
  430. )
  431. /*
  432. */
  433. {
  434. DWORD dwStatus = ERROR_SUCCESS;
  435. BOOL bSuccess;
  436. TLSLICENSEPACK found;
  437. LicPackTable& licpackTable=pDbWkSpace->m_LicPackTable;
  438. if(bPointerOnRecord == FALSE)
  439. {
  440. found.pbDomainSid = NULL;
  441. dwStatus = TLSDBKeyPackFind(
  442. pDbWkSpace,
  443. TRUE,
  444. LICENSEDPACK_PROCESS_DWINTERNAL,
  445. lpKeyPack,
  446. &found
  447. );
  448. lpKeyPack->dwKeyPackId = found.dwKeyPackId;
  449. if(dwStatus != ERROR_SUCCESS)
  450. {
  451. TLSASSERT(FALSE);
  452. goto cleanup;
  453. }
  454. }
  455. #if DBG
  456. {
  457. //
  458. // try to cache some bug...
  459. //
  460. TLSLICENSEPACK found1;
  461. found.pbDomainSid = NULL;
  462. bSuccess = licpackTable.FetchRecord( found1, 0xFFFFFFFF );
  463. //
  464. // Make sure we update right record
  465. if( found1.dwKeyPackId != lpKeyPack->dwKeyPackId )
  466. {
  467. TLSASSERT(FALSE);
  468. }
  469. //
  470. // check input parameter
  471. if( ValidKeyPackParameter( lpKeyPack, FALSE ) == FALSE )
  472. {
  473. TLSASSERT(FALSE);
  474. }
  475. }
  476. #endif
  477. //
  478. // Update the timestamp for this record.
  479. //
  480. GetSystemTimeAsFileTime(&(lpKeyPack->ftLastModifyTime));
  481. bSuccess = licpackTable.UpdateRecord(
  482. *lpKeyPack,
  483. (dwUpdateParm & ~LICENSEDPACK_PROCESS_DWINTERNAL) | LICENSEDPACK_PROCESS_MODIFYTIME
  484. );
  485. if(bSuccess == FALSE)
  486. {
  487. LPTSTR pString = NULL;
  488. TLSGetESEError(licpackTable.GetLastJetError(), &pString);
  489. TLSLogEvent(
  490. EVENTLOG_ERROR_TYPE,
  491. TLS_E_DBGENERAL,
  492. TLS_E_JB_BASE,
  493. licpackTable.GetLastJetError(),
  494. (pString != NULL) ? pString : _TEXT("")
  495. );
  496. if(pString != NULL)
  497. {
  498. LocalFree(pString);
  499. }
  500. SetLastError(dwStatus = SET_JB_ERROR(licpackTable.GetLastJetError()));
  501. TLSASSERT(FALSE);
  502. }
  503. cleanup:
  504. return dwStatus;
  505. }
  506. //-----------------------------------------------------
  507. DWORD
  508. TLSDBKeyPackUpdateNumOfAvailableLicense(
  509. IN PTLSDbWorkSpace pDbWkSpace,
  510. IN BOOL bAdd,
  511. IN PTLSLICENSEPACK lpKeyPack
  512. )
  513. /*++
  514. Abstract:
  515. Update number of license available .
  516. Parameter:
  517. pDbWkSpace : workspace handle.
  518. bAdd : TRUE if add license
  519. lpKeyPack :
  520. Returns:
  521. ++*/
  522. {
  523. DWORD dwStatus=ERROR_SUCCESS;
  524. if(pDbWkSpace == NULL || lpKeyPack == NULL)
  525. {
  526. SetLastError(dwStatus = ERROR_INVALID_PARAMETER);
  527. TLSASSERT(pDbWkSpace != NULL);
  528. return dwStatus;
  529. }
  530. TLSLICENSEPACK found;
  531. DWORD dwNumberLicenseToAddRemove;
  532. BOOL bRemoveMoreThanAvailable=FALSE;
  533. found.pbDomainSid = NULL;
  534. TLSDBLockKeyPackTable();
  535. dwStatus = TLSDBKeyPackFind(
  536. pDbWkSpace,
  537. TRUE,
  538. LICENSEDPACK_PROCESS_DWINTERNAL,
  539. lpKeyPack,
  540. &found
  541. );
  542. if(dwStatus != ERROR_SUCCESS)
  543. {
  544. goto cleanup;
  545. }
  546. dwNumberLicenseToAddRemove = lpKeyPack->dwTotalLicenseInKeyPack;
  547. *lpKeyPack = found;
  548. if(dwNumberLicenseToAddRemove == 0)
  549. {
  550. // query only, a hook so that test program can get actual
  551. // numbers - Jet not re-reading MDB file problem
  552. goto cleanup;
  553. }
  554. //
  555. // Only allow add/remove license on retail
  556. //
  557. if( (found.ucAgreementType & ~LSKEYPACK_RESERVED_TYPE) != LSKEYPACKTYPE_RETAIL)
  558. {
  559. DBGPrintf(
  560. DBG_INFORMATION,
  561. DBG_FACILITY_KEYPACK,
  562. DBGLEVEL_FUNCTION_DETAILSIMPLE,
  563. _TEXT("LSDBKeyPackUpdate : Invalid parameter...\n")
  564. );
  565. SetLastError(dwStatus = TLS_E_INVALID_DATA);
  566. goto cleanup;
  567. }
  568. if(bAdd)
  569. {
  570. // increase both total and number available
  571. found.dwNumberOfLicenses += dwNumberLicenseToAddRemove;
  572. found.dwTotalLicenseInKeyPack += dwNumberLicenseToAddRemove;
  573. }
  574. else
  575. {
  576. //
  577. // if number of licenses to remove is greater that what's available,
  578. // remove all available license and set the return code to invalid data.
  579. //
  580. if(found.dwNumberOfLicenses < dwNumberLicenseToAddRemove)
  581. {
  582. bRemoveMoreThanAvailable = TRUE;
  583. }
  584. dwNumberLicenseToAddRemove = min(dwNumberLicenseToAddRemove, found.dwNumberOfLicenses);
  585. found.dwNumberOfLicenses -= dwNumberLicenseToAddRemove;
  586. found.dwTotalLicenseInKeyPack -= dwNumberLicenseToAddRemove;
  587. }
  588. dwStatus = TLSDBKeyPackSetValues(
  589. pDbWkSpace,
  590. TRUE,
  591. LSKEYPACK_SEARCH_TOTALLICENSES | LSKEYPACK_EXSEARCH_AVAILABLE,
  592. &found
  593. );
  594. *lpKeyPack = found;
  595. cleanup:
  596. TLSDBUnlockKeyPackTable();
  597. if(dwStatus == ERROR_SUCCESS && bRemoveMoreThanAvailable)
  598. SetLastError(dwStatus = TLS_W_REMOVE_TOOMANY);
  599. return dwStatus;
  600. }
  601. //+-----------------------------------------------------------------
  602. DWORD
  603. TLSDBKeyPackAdd(
  604. IN PTLSDbWorkSpace pDbWkSpace,
  605. IN OUT PTLSLICENSEPACK lpKeyPack // return internal tracking ID
  606. )
  607. /*++
  608. Abstract:
  609. Install a product keypack into keypack table, routine only set
  610. keypack status to pending, must call corresponding set status
  611. routine to activate key pack.
  612. Parameters:
  613. pDbWkSpace : Workspace handle.
  614. lpKeyPack : value to be inserted.
  615. Returns:
  616. ++*/
  617. {
  618. DWORD lNextKeyPackId;
  619. DWORD dwStatus;
  620. if(!ValidKeyPackParameter(lpKeyPack, TRUE))
  621. {
  622. SetLastError(TLS_E_INVALID_DATA);
  623. return TLS_E_INVALID_DATA;
  624. }
  625. TLSLICENSEPACK found;
  626. TLSDBLockKeyPackTable();
  627. found.pbDomainSid = NULL;
  628. //
  629. // No duplicate entry for product installed
  630. //
  631. dwStatus = TLSDBKeyPackFind(
  632. pDbWkSpace,
  633. TRUE,
  634. LICENSEDPACK_FIND_PRODUCT,
  635. lpKeyPack,
  636. &found
  637. );
  638. if(dwStatus == ERROR_SUCCESS)
  639. {
  640. // Product already installed
  641. switch( (found.ucKeyPackStatus & ~LSKEYPACKSTATUS_RESERVED) )
  642. {
  643. case LSKEYPACKSTATUS_TEMPORARY:
  644. // case 1 : keypack is installed by temporary license
  645. if(found.ucAgreementType == lpKeyPack->ucAgreementType)
  646. {
  647. dwStatus = TLS_E_DUPLICATE_RECORD;
  648. }
  649. break;
  650. case LSKEYPACKSTATUS_ACTIVE:
  651. case LSKEYPACKSTATUS_PENDING:
  652. // case 2 : duplicate entry
  653. dwStatus = TLS_E_DUPLICATE_RECORD;
  654. break;
  655. case LSKEYPACKSTATUS_RETURNED:
  656. case LSKEYPACKSTATUS_REVOKED:
  657. case LSKEYPACKSTATUS_OTHERS:
  658. // de-activated license key pack.
  659. // keep it.
  660. break;
  661. default:
  662. dwStatus = TLS_E_CORRUPT_DATABASE;
  663. #if DBG
  664. TLSASSERT(FALSE);
  665. #endif
  666. }
  667. if(dwStatus != ERROR_SUCCESS)
  668. goto cleanup;
  669. }
  670. else if(dwStatus == TLS_E_RECORD_NOTFOUND)
  671. {
  672. //
  673. // Always use new keypack ID
  674. // temporary license will be deleted after all license
  675. // has been returned.
  676. //
  677. lpKeyPack->dwKeyPackId = TLSDBGetNextKeyPackId();
  678. dwStatus = TLSDBKeyPackAddEntry(
  679. pDbWkSpace,
  680. lpKeyPack
  681. );
  682. }
  683. cleanup:
  684. TLSDBUnlockKeyPackTable();
  685. return dwStatus;
  686. }
  687. //+-------------------------------------------------------------------
  688. DWORD
  689. TLSDBKeyPackEnumBegin(
  690. IN PTLSDbWorkSpace pDbWkSpace,
  691. IN BOOL bMatchAll,
  692. IN DWORD dwSearchParm,
  693. IN PTLSLICENSEPACK lpSearch
  694. )
  695. /*
  696. Abstract:
  697. Begin enumeration of licensepack table.
  698. Parameter:
  699. pDbWkSpace : workspace handle.
  700. bMatchAll : match all search value.
  701. dwSearchParm : value to search.
  702. lpSearch : search value
  703. Return:
  704. */
  705. {
  706. DWORD dwStatus = ERROR_SUCCESS;
  707. BOOL bSuccess = TRUE;
  708. if(pDbWkSpace == NULL)
  709. {
  710. SetLastError(dwStatus = ERROR_INVALID_PARAMETER);
  711. TLSASSERT(FALSE);
  712. return dwStatus;
  713. }
  714. LicPackTable& licpackTable=pDbWkSpace->m_LicPackTable;
  715. bSuccess = licpackTable.EnumerateBegin(
  716. bMatchAll,
  717. dwSearchParm,
  718. lpSearch
  719. );
  720. if(bSuccess == FALSE)
  721. {
  722. LPTSTR pString = NULL;
  723. TLSGetESEError(licpackTable.GetLastJetError(), &pString);
  724. TLSLogEvent(
  725. EVENTLOG_ERROR_TYPE,
  726. TLS_E_DBGENERAL,
  727. TLS_E_JB_BASE,
  728. licpackTable.GetLastJetError(),
  729. (pString != NULL) ? pString : _TEXT("")
  730. );
  731. if(pString != NULL)
  732. {
  733. LocalFree(pString);
  734. }
  735. SetLastError(dwStatus = SET_JB_ERROR(licpackTable.GetLastJetError()));
  736. TLSASSERT(FALSE);
  737. }
  738. return dwStatus;
  739. }
  740. //+-------------------------------------------------------------------
  741. DWORD
  742. TLSDBKeyPackEnumNext(
  743. IN PTLSDbWorkSpace pDbWkSpace,
  744. IN OUT PTLSLICENSEPACK lpKeyPack
  745. )
  746. /*++
  747. Abstract:
  748. Fetch next row of record that match search condition.
  749. Parameters:
  750. pDbWkSpace : workspace handle.
  751. lpKeyPack : return founded keypack.
  752. Return:
  753. ++*/
  754. {
  755. DWORD dwStatus = ERROR_SUCCESS;
  756. if(pDbWkSpace == NULL || lpKeyPack == NULL)
  757. {
  758. SetLastError(dwStatus = ERROR_INVALID_PARAMETER);
  759. TLSASSERT(FALSE);
  760. return dwStatus;
  761. }
  762. //FreeTlsLicensePack(lpKeyPack);
  763. LicPackTable& licpackTable=pDbWkSpace->m_LicPackTable;
  764. switch(licpackTable.EnumerateNext(*lpKeyPack))
  765. {
  766. case RECORD_ENUM_ERROR:
  767. {
  768. LPTSTR pString = NULL;
  769. TLSGetESEError(licpackTable.GetLastJetError(), &pString);
  770. TLSLogEvent(
  771. EVENTLOG_ERROR_TYPE,
  772. TLS_E_DBGENERAL,
  773. TLS_E_JB_BASE,
  774. licpackTable.GetLastJetError(),
  775. (pString != NULL) ? pString : _TEXT("")
  776. );
  777. if(pString != NULL)
  778. {
  779. LocalFree(pString);
  780. }
  781. }
  782. dwStatus = SET_JB_ERROR(licpackTable.GetLastJetError());
  783. TLSASSERT(FALSE);
  784. break;
  785. case RECORD_ENUM_MORE_DATA:
  786. dwStatus = ERROR_SUCCESS;
  787. break;
  788. case RECORD_ENUM_END:
  789. dwStatus = TLS_I_NO_MORE_DATA;
  790. }
  791. return dwStatus;
  792. }
  793. //+-------------------------------------------------------------------
  794. void
  795. TLSDBKeyPackEnumEnd(
  796. IN PTLSDbWorkSpace pDbWkSpace
  797. )
  798. /*++
  799. Abstract:
  800. End enumeration of licensepack.
  801. Parameter:
  802. pDbWkSpace : workspace handle.
  803. ++*/
  804. {
  805. TLSASSERT(pDbWkSpace != NULL);
  806. pDbWkSpace->m_LicPackTable.EnumerateEnd();
  807. return;
  808. }
  809. //+-------------------------------------------------------------------
  810. DWORD
  811. TLSDBKeyPackSetValues(
  812. IN PTLSDbWorkSpace pDbWkSpace,
  813. IN BOOL bPointerOnRecord,
  814. IN DWORD dwSetParm,
  815. IN PTLSLICENSEPACK lpKeyPack
  816. )
  817. /*++
  818. Abstract:
  819. Set column value of a license pack record.
  820. Parameter;
  821. pDbWkSpace : workspace handle.
  822. bInternal : call is from internal routine, no error checking.
  823. dwSetParm : Columns to be set.
  824. lpKeyPack : value to be set.
  825. Returns.
  826. ++*/
  827. {
  828. DWORD dwStatus=ERROR_SUCCESS;
  829. if(pDbWkSpace == NULL || lpKeyPack == NULL)
  830. {
  831. SetLastError(dwStatus = ERROR_INVALID_PARAMETER);
  832. TLSASSERT(FALSE);
  833. goto cleanup;
  834. }
  835. if(bPointerOnRecord == FALSE)
  836. {
  837. TLSLICENSEPACK found;
  838. // Find this keypack's internal keypack id
  839. dwStatus = TLSDBKeyPackFind(
  840. pDbWkSpace,
  841. TRUE,
  842. LICENSEDPACK_FIND_PRODUCT,
  843. lpKeyPack,
  844. &found
  845. );
  846. //FreeTlsLicensePack(&found);
  847. if(dwStatus != ERROR_SUCCESS)
  848. {
  849. goto cleanup;
  850. }
  851. lpKeyPack->dwKeyPackId = found.dwKeyPackId;
  852. }
  853. if(lpKeyPack->ucKeyPackStatus == LSKEYPACKSTATUS_DELETE)
  854. {
  855. dwStatus = TLSDBKeyPackDeleteEntry(
  856. pDbWkSpace,
  857. TRUE, // delete all records with same ID
  858. lpKeyPack
  859. );
  860. }
  861. else
  862. {
  863. dwStatus = TLSDBKeyPackUpdateEntry(
  864. pDbWkSpace,
  865. TRUE,
  866. dwSetParm,
  867. lpKeyPack
  868. );
  869. }
  870. cleanup:
  871. return dwStatus;
  872. }
  873. //+-------------------------------------------------------------------
  874. DWORD
  875. TLSDBKeyPackSetStatus(
  876. IN PTLSDbWorkSpace pDbWkSpace,
  877. IN DWORD dwSetStatus,
  878. IN PTLSLICENSEPACK lpKeyPack
  879. )
  880. /*++
  881. Abstract:
  882. Stub routine for RPC to set status of a keypack
  883. ++*/
  884. {
  885. return TLSDBKeyPackSetValues(
  886. pDbWkSpace,
  887. FALSE,
  888. dwSetStatus,
  889. lpKeyPack
  890. );
  891. }
  892. //+-------------------------------------------------------------------
  893. DWORD
  894. TLSDBKeyPackGetAvailableLicenses(
  895. IN PTLSDbWorkSpace pDbWkSpace,
  896. IN DWORD dwSearchParm,
  897. IN PTLSLICENSEPACK lplsKeyPack,
  898. IN OUT LPDWORD lpdwAvail
  899. )
  900. /*++
  901. Abstract:
  902. retrieve number of available licenses for the key pack.
  903. Parameter:
  904. pDbWkSpace : workspace handle.
  905. dwSearchParm : search parameters.
  906. lpLsKeyPack : search value.
  907. lpdwAvail : return number of available licenses.
  908. Return:
  909. ++*/
  910. {
  911. DWORD dwStatus = ERROR_SUCCESS;
  912. TLSLICENSEPACK found;
  913. dwStatus = TLSDBKeyPackFind(
  914. pDbWkSpace,
  915. TRUE,
  916. dwSearchParm,
  917. lplsKeyPack,
  918. &found
  919. );
  920. if(dwStatus == ERROR_SUCCESS)
  921. {
  922. switch(found.ucAgreementType)
  923. {
  924. case LSKEYPACKTYPE_SELECT:
  925. case LSKEYPACKTYPE_RETAIL:
  926. case LSKEYPACKTYPE_OPEN:
  927. *lpdwAvail = found.dwNumberOfLicenses;
  928. break;
  929. default:
  930. *lpdwAvail = LONG_MAX;
  931. }
  932. }
  933. return dwStatus;
  934. }