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.

1694 lines
52 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996-1998
  5. //
  6. // File: licreq.cpp
  7. //
  8. // Contents:
  9. // New license request
  10. //
  11. // History:
  12. // 09/13/98 HueiWang Created
  13. //---------------------------------------------------------------------------
  14. #include "pch.cpp"
  15. #include "licreq.h"
  16. #include "db.h"
  17. #include "findlost.h"
  18. #include "permlic.h"
  19. #include "templic.h"
  20. #include "gencert.h"
  21. #include "globals.h"
  22. #include "forward.h"
  23. #include "postjob.h"
  24. #include "cryptkey.h"
  25. #include "init.h"
  26. #include "clilic.h"
  27. DWORD
  28. TLSDBIssueNewLicenseFromLocal(
  29. IN PTLSDbWorkSpace pDbWkSpace,
  30. IN PTLSDBLICENSEREQUEST pRequest,
  31. IN BOOL bFindLostLicense,
  32. IN BOOL bRequireTempLicense,
  33. IN BOOL bAcceptFewerLicenses,
  34. IN OUT DWORD *pdwQuantity,
  35. OUT PTLSDBLICENSEDPRODUCT pLicensedProduct,
  36. IN DWORD dwSupportFlags
  37. );
  38. //
  39. // State of issuing function - used for counters
  40. //
  41. #define NONE_TRIED 0
  42. #define PERMANENT_ISSUE_TRIED 1
  43. #define TEMPORARY_ISSUE_TRIED 2
  44. #define PERMANENT_REISSUE_TRIED 3
  45. ////////////////////////////////////////////////////////////////////
  46. void
  47. TLSLicenseTobeReturnToPMLicenseToBeReturn(
  48. PTLSLicenseToBeReturn pTlsLicense,
  49. BOOL bTempLicense,
  50. PPMLICENSETOBERETURN pPmLicense
  51. )
  52. /*++
  53. --*/
  54. {
  55. pPmLicense->dwQuantity = pTlsLicense->dwQuantity;
  56. pPmLicense->dwProductVersion = pTlsLicense->dwProductVersion;
  57. pPmLicense->pszOrgProductId = pTlsLicense->pszOrgProductId;
  58. pPmLicense->pszCompanyName = pTlsLicense->pszCompanyName;
  59. pPmLicense->pszProductId = pTlsLicense->pszProductId;
  60. pPmLicense->pszUserName = pTlsLicense->pszUserName;
  61. pPmLicense->pszMachineName = pTlsLicense->pszMachineName;
  62. pPmLicense->dwPlatformID = pTlsLicense->dwPlatformID;
  63. pPmLicense->bTemp = bTempLicense;
  64. return;
  65. }
  66. ////////////////////////////////////////////////////////////////////
  67. DWORD
  68. TLSReturnClientLicensedProduct(
  69. IN PTLSDbWorkSpace pDbWkSpace,
  70. IN PMHANDLE hClient,
  71. IN CTLSPolicy* pPolicy,
  72. IN PTLSLicenseToBeReturn pClientLicense
  73. )
  74. /*++
  75. --*/
  76. {
  77. DWORD dwStatus = ERROR_SUCCESS;
  78. DWORD dwPolicyLicenseStatus;
  79. ULARGE_INTEGER serialNumber;
  80. HWID hwid;
  81. LICENSEREQUEST LicensedProduct;
  82. Product_Info ProductInfo;
  83. TLSLICENSEPACK LicensePack;
  84. LICENSEDCLIENT LicenseClient;
  85. PMLICENSETOBERETURN pmLicToBeReturn;
  86. DWORD dwLicenseStatus;
  87. dwStatus = LicenseDecryptHwid(
  88. &hwid,
  89. pClientLicense->cbEncryptedHwid,
  90. pClientLicense->pbEncryptedHwid,
  91. g_cbSecretKey,
  92. g_pbSecretKey
  93. );
  94. if(dwStatus != ERROR_SUCCESS)
  95. {
  96. SetLastError(dwStatus = TLS_E_INVALID_LICENSE);
  97. goto cleanup;
  98. }
  99. LicensedProduct.pbEncryptedHwid = pClientLicense->pbEncryptedHwid;
  100. LicensedProduct.cbEncryptedHwid = pClientLicense->cbEncryptedHwid;
  101. LicensedProduct.dwLanguageID = 0;
  102. LicensedProduct.dwPlatformID = pClientLicense->dwPlatformID;
  103. LicensedProduct.pProductInfo = &ProductInfo;
  104. ProductInfo.cbCompanyName = (lstrlen(pClientLicense->pszCompanyName) + 1) * sizeof(TCHAR);
  105. ProductInfo.pbCompanyName = (PBYTE)pClientLicense->pszCompanyName;
  106. ProductInfo.cbProductID = (lstrlen(pClientLicense->pszProductId) + 1) * sizeof(TCHAR);
  107. ProductInfo.pbProductID = (PBYTE)pClientLicense->pszProductId;
  108. //
  109. // Verify with local database
  110. //
  111. dwStatus = TLSDBValidateLicense(
  112. pDbWkSpace,
  113. &hwid,
  114. &LicensedProduct,
  115. pClientLicense->dwKeyPackId,
  116. pClientLicense->dwLicenseId,
  117. &LicensePack,
  118. &LicenseClient
  119. );
  120. if(dwStatus != ERROR_SUCCESS)
  121. {
  122. // tell caller this record is wrong.
  123. SetLastError(dwStatus = TLS_E_RECORD_NOTFOUND);
  124. goto cleanup;
  125. }
  126. if( LicenseClient.ucLicenseStatus == LSLICENSE_STATUS_UPGRADED ||
  127. LicenseClient.ucLicenseStatus == LSLICENSE_STATUS_REVOKE ||
  128. LicenseClient.ucLicenseStatus == LSLICENSE_STATUS_UNKNOWN )
  129. {
  130. // License already been return/revoke
  131. dwStatus = ERROR_SUCCESS;
  132. goto cleanup;
  133. }
  134. //
  135. //
  136. // only inform policy module if license status is
  137. // active, temporary, active_pending, concurrent
  138. // TODO - pass all status to policy module
  139. //
  140. if( LicenseClient.ucLicenseStatus == LSLICENSE_STATUS_TEMPORARY ||
  141. LicenseClient.ucLicenseStatus == LSLICENSE_STATUS_ACTIVE ||
  142. //LicenseClient.ucLicenseStatus == LSLICENSE_STATUS_PENDING_ACTIVE ||
  143. LicenseClient.ucLicenseStatus == LSLICENSE_STATUS_CONCURRENT )
  144. {
  145. serialNumber.HighPart = pClientLicense->dwKeyPackId;
  146. serialNumber.LowPart = pClientLicense->dwLicenseId;
  147. TLSLicenseTobeReturnToPMLicenseToBeReturn(
  148. pClientLicense,
  149. LicenseClient.ucLicenseStatus == LSLICENSE_STATUS_TEMPORARY,
  150. &pmLicToBeReturn
  151. );
  152. dwStatus = pPolicy->PMReturnLicense(
  153. hClient,
  154. &serialNumber,
  155. &pmLicToBeReturn,
  156. &dwPolicyLicenseStatus
  157. );
  158. if(dwStatus != ERROR_SUCCESS)
  159. {
  160. goto cleanup;
  161. }
  162. //
  163. // delete license on request.
  164. //
  165. dwLicenseStatus = (dwPolicyLicenseStatus == LICENSE_RETURN_KEEP) ?
  166. LSLICENSE_STATUS_UPGRADED : LSLICENSESTATUS_DELETE;
  167. }
  168. if (LicenseClient.dwNumLicenses == pClientLicense->dwQuantity)
  169. {
  170. // delete the whole license
  171. dwStatus = TLSDBReturnLicense(
  172. pDbWkSpace,
  173. pClientLicense->dwKeyPackId,
  174. pClientLicense->dwLicenseId,
  175. dwLicenseStatus
  176. );
  177. }
  178. else
  179. {
  180. dwStatus = TLSDBReturnLicenseToKeyPack(
  181. pDbWkSpace,
  182. pClientLicense->dwKeyPackId,
  183. pClientLicense->dwQuantity
  184. );
  185. if (dwStatus == ERROR_SUCCESS)
  186. {
  187. // Set number of CALs in license
  188. LICENSEDCLIENT license;
  189. license.dwLicenseId = pClientLicense->dwLicenseId;
  190. license.dwNumLicenses = LicenseClient.dwNumLicenses - pClientLicense->dwQuantity;
  191. license.ucLicenseStatus = LSLICENSE_STATUS_UPGRADED;
  192. dwStatus = TLSDBLicenseSetValue(pDbWkSpace,
  193. LSLICENSE_SEARCH_NUMLICENSES,
  194. &license,
  195. FALSE // bPointerOnRecord
  196. );
  197. }
  198. }
  199. cleanup:
  200. return dwStatus;
  201. }
  202. ////////////////////////////////////////////////////////////////////
  203. DWORD
  204. TLSDBMarkClientLicenseUpgraded(
  205. IN PTLSDbWorkSpace pDbWkSpace,
  206. IN PTLSDBLICENSEREQUEST pRequest,
  207. IN PTLSDBLICENSEDPRODUCT pLicensedProduct
  208. )
  209. /*++
  210. --*/
  211. {
  212. DWORD dwStatus = ERROR_SUCCESS;
  213. DWORD dwLicenseStatus;
  214. PMLICENSETOBERETURN pmLicense;
  215. if(pRequest == NULL || pRequest->pPolicy == NULL)
  216. {
  217. SetLastError(dwStatus = ERROR_INVALID_PARAMETER);
  218. return dwStatus;
  219. }
  220. pmLicense.dwQuantity = pLicensedProduct->dwQuantity;
  221. pmLicense.dwProductVersion = pLicensedProduct->dwProductVersion;
  222. pmLicense.pszOrgProductId = pLicensedProduct->szRequestProductId;
  223. pmLicense.pszCompanyName = pLicensedProduct->szCompanyName;
  224. pmLicense.pszProductId = pLicensedProduct->szLicensedProductId;
  225. pmLicense.pszUserName = pLicensedProduct->szUserName;
  226. pmLicense.pszMachineName = pLicensedProduct->szMachineName;
  227. pmLicense.dwPlatformID = pLicensedProduct->dwPlatformID;
  228. pmLicense.bTemp = pLicensedProduct->bTemp;
  229. //
  230. // Ask if we can delete the old license
  231. //
  232. dwStatus = pRequest->pPolicy->PMReturnLicense(
  233. pRequest->hClient,
  234. &pLicensedProduct->ulSerialNumber,
  235. &pmLicense,
  236. &dwLicenseStatus
  237. );
  238. //
  239. // MarkClientLicenseUpgrade() can only be called by FindLostLicense() which will only
  240. // return valid licenses.
  241. // TODO - Check license status.
  242. //
  243. if(dwStatus == ERROR_SUCCESS)
  244. {
  245. // Temporary license - delete license and don't bother about
  246. // Permenant license - keep license and DO NOT return license to keypack
  247. dwStatus = TLSDBReturnLicense(
  248. pDbWkSpace,
  249. pLicensedProduct->dwKeyPackId,
  250. pLicensedProduct->dwLicenseId,
  251. (dwLicenseStatus == LICENSE_RETURN_KEEP) ? LSLICENSE_STATUS_UPGRADED : LSLICENSESTATUS_DELETE
  252. );
  253. }
  254. return dwStatus;
  255. }
  256. //////////////////////////////////////////////////////////////
  257. DWORD
  258. TLSDBUpgradeClientLicense(
  259. IN PTLSDbWorkSpace pDbWkSpace,
  260. IN PTLSDBLICENSEREQUEST pRequest,
  261. IN PTLSDBLICENSEDPRODUCT pLicensedProduct,
  262. IN BOOL bAcceptFewerLicenses,
  263. IN OUT DWORD *pdwQuantity,
  264. IN OUT PTLSDBLICENSEDPRODUCT pUpgradedProduct,
  265. IN DWORD dwSupportFlags
  266. )
  267. /*
  268. Abstract:
  269. Upgrade a license - issue a new license and return old license
  270. Parameters:
  271. Returns
  272. */
  273. {
  274. DWORD dwStatus=ERROR_SUCCESS;
  275. dwStatus=TLSDBIssuePermanentLicense(
  276. pDbWkSpace,
  277. pRequest,
  278. TRUE, // bLatestVersion
  279. bAcceptFewerLicenses,
  280. pdwQuantity,
  281. pUpgradedProduct,
  282. dwSupportFlags
  283. );
  284. if (dwStatus == ERROR_SUCCESS)
  285. {
  286. //
  287. // Return license to keypack
  288. //
  289. dwStatus = TLSDBMarkClientLicenseUpgraded(
  290. pDbWkSpace,
  291. pRequest,
  292. pLicensedProduct
  293. );
  294. }
  295. return dwStatus;
  296. }
  297. //--------------------------------------------------------------------
  298. void
  299. LicensedProductToDbLicensedProduct(
  300. PLICENSEDPRODUCT pSrc,
  301. PTLSDBLICENSEDPRODUCT pDest
  302. )
  303. /*++
  304. ++*/
  305. {
  306. pDest->dwQuantity = pSrc->dwQuantity;
  307. pDest->ulSerialNumber = pSrc->ulSerialNumber;
  308. pDest->dwKeyPackId = pSrc->ulSerialNumber.HighPart;
  309. pDest->dwLicenseId = pSrc->ulSerialNumber.LowPart;
  310. pDest->ClientHwid = pSrc->Hwid;
  311. pDest->NotBefore = pSrc->NotBefore;
  312. pDest->NotAfter = pSrc->NotAfter;
  313. pDest->bTemp = ((pSrc->pLicensedVersion->dwFlags & LICENSED_VERSION_TEMPORARY) != 0);
  314. pDest->dwProductVersion = pSrc->LicensedProduct.pProductInfo->dwVersion;
  315. SAFESTRCPY(
  316. pDest->szCompanyName,
  317. (LPTSTR)(pSrc->LicensedProduct.pProductInfo->pbCompanyName)
  318. );
  319. SAFESTRCPY(
  320. pDest->szLicensedProductId,
  321. (LPTSTR)(pSrc->LicensedProduct.pProductInfo->pbProductID)
  322. );
  323. SAFESTRCPY(
  324. pDest->szRequestProductId,
  325. (LPTSTR)(pSrc->pbOrgProductID)
  326. );
  327. SAFESTRCPY(
  328. pDest->szUserName,
  329. pSrc->szLicensedUser
  330. );
  331. SAFESTRCPY(
  332. pDest->szMachineName,
  333. pSrc->szLicensedClient
  334. );
  335. pDest->dwLanguageID = pSrc->LicensedProduct.dwLanguageID;
  336. pDest->dwPlatformID = pSrc->LicensedProduct.dwPlatformID;
  337. pDest->pbPolicyData = pSrc->pbPolicyData;
  338. pDest->cbPolicyData = pSrc->cbPolicyData;
  339. }
  340. //--------------------------------------------------------------------
  341. void
  342. CopyDbLicensedProduct(
  343. PTLSDBLICENSEDPRODUCT pSrc,
  344. PTLSDBLICENSEDPRODUCT pDest
  345. )
  346. /*++
  347. ++*/
  348. {
  349. pDest->dwQuantity = pSrc->dwQuantity;
  350. pDest->ulSerialNumber = pSrc->ulSerialNumber;
  351. pDest->dwKeyPackId = pSrc->dwKeyPackId;
  352. pDest->dwLicenseId = pSrc->dwLicenseId;
  353. pDest->ClientHwid = pSrc->ClientHwid;
  354. pDest->NotBefore = pSrc->NotBefore;
  355. pDest->NotAfter = pSrc->NotAfter;
  356. pDest->bTemp = pSrc->bTemp;
  357. pDest->dwProductVersion = pSrc->dwProductVersion;
  358. SAFESTRCPY(
  359. pDest->szCompanyName,
  360. pSrc->szCompanyName
  361. );
  362. SAFESTRCPY(
  363. pDest->szLicensedProductId,
  364. pSrc->szLicensedProductId
  365. );
  366. SAFESTRCPY(
  367. pDest->szRequestProductId,
  368. pSrc->szRequestProductId
  369. );
  370. SAFESTRCPY(
  371. pDest->szUserName,
  372. pSrc->szUserName
  373. );
  374. SAFESTRCPY(
  375. pDest->szMachineName,
  376. pSrc->szMachineName
  377. );
  378. pDest->dwLanguageID = pSrc->dwLanguageID;
  379. pDest->dwPlatformID = pSrc->dwPlatformID;
  380. pDest->pbPolicyData = pSrc->pbPolicyData;
  381. pDest->cbPolicyData = pSrc->cbPolicyData;
  382. }
  383. //------------------------------------------------------------------
  384. DWORD
  385. TLSDBIssueNewLicenseFromLocal(
  386. IN PTLSDbWorkSpace pDbWkSpace,
  387. IN PTLSDBLICENSEREQUEST pRequest,
  388. IN BOOL bFindLostLicense,
  389. IN BOOL bRequireTempLicense,
  390. IN BOOL bAcceptFewerLicenses,
  391. IN OUT DWORD *pdwQuantity,
  392. IN OUT PTLSDBLICENSEDPRODUCT pLicensedProduct,
  393. IN DWORD dwSupportFlags
  394. )
  395. /*++
  396. Abstract:
  397. Allocate a license from locally installed license pack.
  398. Parameters:
  399. pDbWkSpace - workspace handle
  400. lpLsLicenseRequest - license request
  401. bAcceptTemporaryLicense - accept temporary license
  402. bFindLostLicense - TRUE if find lost license before issuing a new one
  403. bRequireTempLicense -TRUE if permanent license can't be issued (DoS fix)
  404. bAcceptFewerLicenses - TRUE if succeeding with fewer licenses than
  405. requested is acceptable
  406. pdwQuantity - on input, number of licenses requested
  407. on output, number of licenses actually allocated
  408. pLicensedProduct - return licensed product
  409. Returns:
  410. ++*/
  411. {
  412. DWORD status=TLS_E_RECORD_NOTFOUND;
  413. UCHAR ucMarked;
  414. if(bFindLostLicense == TRUE)
  415. {
  416. //
  417. // Try to find the lost license
  418. //
  419. status=TLSDBFindLostLicense(
  420. pDbWkSpace,
  421. pRequest,
  422. &pRequest->hWid,
  423. pLicensedProduct,
  424. &ucMarked
  425. );
  426. if( status != TLS_E_RECORD_NOTFOUND &&
  427. status != TLS_E_LICENSE_EXPIRED &&
  428. status != TLS_I_FOUND_TEMPORARY_LICENSE &&
  429. status != ERROR_SUCCESS)
  430. {
  431. goto cleanup;
  432. }
  433. //
  434. // If license has been expired or it is a temporary license,
  435. // try to allocate a new permanent one.
  436. //
  437. if ( status == TLS_E_LICENSE_EXPIRED
  438. || status == TLS_I_FOUND_TEMPORARY_LICENSE)
  439. {
  440. if ((!pLicensedProduct->bTemp) && CanIssuePermLicense())
  441. {
  442. TLSDBLICENSEDPRODUCT upgradeProduct;
  443. //
  444. // expired permanent
  445. //
  446. status = TLSDBReissueFoundPermanentLicense(
  447. USEHANDLE(pDbWkSpace),
  448. pLicensedProduct,
  449. &upgradeProduct
  450. );
  451. if (ERROR_SUCCESS == status)
  452. {
  453. *pLicensedProduct = upgradeProduct;
  454. status = TLS_I_LICENSE_UPGRADED;
  455. }
  456. else
  457. {
  458. //
  459. // reissuance failed, try to issue a new permanent
  460. //
  461. status = TLS_E_RECORD_NOTFOUND;
  462. }
  463. }
  464. //
  465. // no upgrade if license server hasn't been registered
  466. // or if DoS fix required and license isn't marked
  467. //
  468. else if (((!bRequireTempLicense)
  469. || (ucMarked & MARK_FLAG_USER_AUTHENTICATED))
  470. && CanIssuePermLicense())
  471. {
  472. DWORD upgrade_status;
  473. TLSDBLICENSEDPRODUCT upgradeProduct;
  474. upgrade_status = TLSDBUpgradeClientLicense(
  475. pDbWkSpace,
  476. pRequest,
  477. pLicensedProduct,
  478. bAcceptFewerLicenses,
  479. pdwQuantity,
  480. &upgradeProduct,
  481. dwSupportFlags
  482. );
  483. if(upgrade_status == ERROR_SUCCESS)
  484. {
  485. *pLicensedProduct = upgradeProduct;
  486. status = TLS_I_LICENSE_UPGRADED;
  487. }
  488. else if(upgrade_status != TLS_E_NO_LICENSE &&
  489. upgrade_status != TLS_E_PRODUCT_NOTINSTALL)
  490. {
  491. //
  492. // Error in upgrade license.
  493. //
  494. status = upgrade_status;
  495. }
  496. goto cleanup;
  497. }
  498. //
  499. // Temporary license has expired and can't allocate permanent
  500. // license, refuse connection
  501. //
  502. if( status == TLS_E_LICENSE_EXPIRED )
  503. {
  504. goto cleanup;
  505. }
  506. }
  507. else if ((status == ERROR_SUCCESS)
  508. && (pLicensedProduct->dwQuantity != *pdwQuantity))
  509. {
  510. // user has wrong number of licenses
  511. if (*pdwQuantity > pLicensedProduct->dwQuantity)
  512. {
  513. if (bRequireTempLicense || !CanIssuePermLicense())
  514. {
  515. goto try_next;
  516. }
  517. #define NUM_KEYPACKS 5
  518. DWORD upgrade_status;
  519. TLSDBLicenseAllocation allocation;
  520. DWORD dwAllocation[NUM_KEYPACKS];
  521. TLSLICENSEPACK keypack[NUM_KEYPACKS];
  522. TLSDBAllocateRequest AllocateRequest;
  523. for (int i=0; i < NUM_KEYPACKS; i++)
  524. {
  525. keypack[i].pbDomainSid = NULL;
  526. }
  527. memset(&allocation,0,sizeof(allocation));
  528. allocation.dwBufSize = NUM_KEYPACKS;
  529. allocation.pdwAllocationVector = dwAllocation;
  530. allocation.lpAllocateKeyPack = keypack;
  531. AllocateRequest.szCompanyName
  532. = (LPTSTR)pRequest->pszCompanyName;
  533. AllocateRequest.szProductId
  534. = (LPTSTR)pRequest->pszProductId;
  535. AllocateRequest.dwVersion
  536. = pRequest->dwProductVersion;
  537. AllocateRequest.dwPlatformId
  538. = pRequest->dwPlatformID;
  539. AllocateRequest.dwLangId
  540. = pRequest->dwLanguageID;
  541. AllocateRequest.dwNumLicenses
  542. = *pdwQuantity - pLicensedProduct->dwQuantity;
  543. AllocateRequest.dwScheme
  544. = ALLOCATE_ANY_GREATER_VERSION;
  545. AllocateRequest.ucAgreementType
  546. = LSKEYPACKTYPE_UNKNOWN;
  547. upgrade_status = AllocateLicensesFromDB(
  548. pDbWkSpace,
  549. &AllocateRequest,
  550. FALSE, // fCheckAgreementType
  551. &allocation
  552. );
  553. if ((upgrade_status == ERROR_SUCCESS)
  554. && ((allocation.dwTotalAllocated == 0)
  555. || (!bAcceptFewerLicenses
  556. && (allocation.dwTotalAllocated != *pdwQuantity-pLicensedProduct->dwQuantity))))
  557. {
  558. status = TLS_E_NO_LICENSE;
  559. goto cleanup;
  560. }
  561. else
  562. {
  563. *pdwQuantity = pLicensedProduct->dwQuantity + allocation.dwTotalAllocated;
  564. }
  565. if (TLS_I_NO_MORE_DATA == upgrade_status)
  566. {
  567. status = TLS_E_NO_LICENSE;
  568. goto cleanup;
  569. }
  570. if(upgrade_status == ERROR_SUCCESS)
  571. {
  572. status = TLS_I_LICENSE_UPGRADED;
  573. }
  574. else
  575. {
  576. //
  577. // Error in upgrade license.
  578. //
  579. status = upgrade_status;
  580. goto cleanup;
  581. }
  582. }
  583. else
  584. {
  585. // return unwanted licenses to keypack
  586. status = TLSDBReturnLicenseToKeyPack(
  587. pDbWkSpace,
  588. pLicensedProduct->dwKeyPackId,
  589. pLicensedProduct->dwQuantity - *pdwQuantity
  590. );
  591. if (status != ERROR_SUCCESS)
  592. {
  593. goto cleanup;
  594. }
  595. }
  596. {
  597. // Set number of CALs in license
  598. LICENSEDCLIENT license;
  599. license.dwLicenseId = pLicensedProduct->dwLicenseId;
  600. license.dwNumLicenses = *pdwQuantity;
  601. license.ucLicenseStatus = LSLICENSE_STATUS_UPGRADED;
  602. status = TLSDBLicenseSetValue(pDbWkSpace,
  603. LSLICENSE_SEARCH_NUMLICENSES,
  604. &license,
  605. FALSE // bPointerOnRecord
  606. );
  607. }
  608. goto cleanup;
  609. }
  610. }
  611. try_next:
  612. //
  613. // Issue permanent license only if license server has been registered
  614. // and user is allowed to have one
  615. //
  616. if((status == TLS_E_RECORD_NOTFOUND) && (!bRequireTempLicense))
  617. {
  618. if(CanIssuePermLicense() == FALSE)
  619. {
  620. SetLastError(status = TLS_E_NO_CERTIFICATE);
  621. }
  622. else
  623. {
  624. status=TLSDBIssuePermanentLicense(
  625. pDbWkSpace,
  626. pRequest,
  627. FALSE,
  628. bAcceptFewerLicenses,
  629. pdwQuantity,
  630. pLicensedProduct,
  631. dwSupportFlags
  632. );
  633. }
  634. }
  635. cleanup:
  636. return status;
  637. }
  638. //////////////////////////////////////////////////////////////////////
  639. DWORD
  640. TLSUpgradeLicenseRequest(
  641. IN BOOL bForwardRequest,
  642. IN PTLSForwardUpgradeLicenseRequest pForward,
  643. IN OUT DWORD *pdwSupportFlags,
  644. IN PTLSDBLICENSEREQUEST pRequest,
  645. IN PBYTE pbOldLicense,
  646. IN DWORD cbOldLicense,
  647. IN DWORD dwNumLicProduct,
  648. IN PLICENSEDPRODUCT pLicProduct,
  649. IN BOOL bRequireTempLicense,
  650. IN OUT PDWORD pcbEncodedCert,
  651. OUT PBYTE* ppbEncodedCert
  652. )
  653. /*++
  654. ++*/
  655. {
  656. DWORD dwStatus = TLS_E_NO_LICENSE;
  657. BOOL bAcceptTempLicense = FALSE;
  658. DWORD index;
  659. DWORD dwNumNewLicProduct;
  660. TLSDBLICENSEDPRODUCT NewLicProduct;
  661. PTLSDbWorkSpace pDbWkSpace=NULL;
  662. PTLSDBLICENSEDPRODUCT pGenCertProduct=NULL;
  663. FILETIME* pNotBefore=NULL;
  664. FILETIME* pNotAfter=NULL;
  665. DWORD dwNumChars;
  666. DWORD dwLicGenStatus;
  667. BOOL bDbHandleAcquired = FALSE;
  668. BOOL fReissue = FALSE;
  669. DWORD dwTried = NONE_TRIED;
  670. //
  671. // check to see if we can take temp. license
  672. //
  673. // The only case that we need to set temp. license's expiration date is
  674. // latest licensed product is temporary and client is requesting for version
  675. // greater than lastest license.
  676. //
  677. if(CompareTLSVersions(pRequest->dwProductVersion, pLicProduct->LicensedProduct.pProductInfo->dwVersion) > 0)
  678. {
  679. bAcceptTempLicense = TRUE;
  680. if(pLicProduct->pLicensedVersion->dwFlags & LICENSED_VERSION_TEMPORARY)
  681. {
  682. //
  683. // client holding 5.0 temp. and request 6.0 licenses.
  684. // we need to issue 6.0 license but the license expiration
  685. // date stay the same.
  686. //
  687. pNotBefore = &(pLicProduct->NotBefore);
  688. pNotAfter = &(pLicProduct->NotAfter);
  689. }
  690. }
  691. else if(CompareTLSVersions(pRequest->dwProductVersion, pLicProduct->LicensedProduct.pProductInfo->dwVersion) == 0)
  692. {
  693. if( IS_LICENSE_ISSUER_RTM(pLicProduct->pLicensedVersion->dwFlags) == FALSE &&
  694. TLSIsBetaNTServer() == FALSE )
  695. {
  696. // issuer is beta/eval, we are a RTM, accept temp. license
  697. bAcceptTempLicense = TRUE;
  698. }
  699. }
  700. if(ALLOCATEDBHANDLE(pDbWorkSpace, g_EnumDbTimeout) == FALSE)
  701. {
  702. dwStatus = TLS_E_ALLOCATE_HANDLE;
  703. goto cleanup;
  704. }
  705. CLEANUPSTMT;
  706. BEGIN_TRANSACTION(pDbWorkSpace);
  707. bDbHandleAcquired = TRUE;
  708. if (!bRequireTempLicense)
  709. {
  710. //
  711. // Check for reissuance first, if a) reissuance is supported, b)
  712. // the license is permanent, c) the license is the current version,
  713. // and d) the license is expired.
  714. //
  715. if ((*pdwSupportFlags & SUPPORT_PER_SEAT_REISSUANCE) &&
  716. ((_tcsnicmp((TCHAR *)pLicProduct->LicensedProduct.pProductInfo->pbProductID,
  717. TERMSERV_PRODUCTID_SKU,
  718. _tcslen(TERMSERV_PRODUCTID_SKU)) == 0) ||
  719. (_tcsnicmp((TCHAR *)pLicProduct->LicensedProduct.pProductInfo->pbProductID,
  720. TERMSERV_PRODUCTID_CONCURRENT_SKU,
  721. _tcslen(TERMSERV_PRODUCTID_CONCURRENT_SKU)) == 0)) &&
  722. (!(pLicProduct->pLicensedVersion->dwFlags &
  723. LICENSED_VERSION_TEMPORARY)) &&
  724. (CompareTLSVersions(pLicProduct->LicensedProduct.pProductInfo->dwVersion,
  725. pRequest->dwProductVersion)) == 0)
  726. {
  727. DWORD t;
  728. //
  729. // Checking expiration with filetimes is a pain; convert.
  730. //
  731. FileTimeToLicenseDate(&(pLicProduct->NotAfter), &t);
  732. if (t-g_dwReissueLeaseLeeway < time(NULL))
  733. {
  734. // do reissue
  735. fReissue = TRUE;
  736. if (CanIssuePermLicense())
  737. {
  738. dwStatus = TLSDBReissuePermanentLicense(
  739. USEHANDLE(pDbWkSpace),
  740. pLicProduct,
  741. &NewLicProduct
  742. );
  743. if (dwStatus == ERROR_SUCCESS)
  744. {
  745. dwTried = PERMANENT_REISSUE_TRIED;
  746. // skip past the next stuff if all goes well
  747. goto licenseReissued;
  748. }
  749. }
  750. else
  751. {
  752. dwStatus = TLS_E_RECORD_NOTFOUND;
  753. }
  754. if ((dwStatus == TLS_E_RECORD_NOTFOUND)
  755. && bForwardRequest
  756. && (_tcsicmp(pLicProduct->szIssuerId,
  757. (LPTSTR)g_pszServerPid) != 0))
  758. {
  759. // couldn't find the license, forward the request to issuer
  760. DWORD dwSupportFlagsTemp = *pdwSupportFlags;
  761. DWORD dwErrCode;
  762. dwStatus = ForwardUpgradeLicenseRequest(
  763. pLicProduct->szIssuerId,
  764. &dwSupportFlagsTemp,
  765. pForward->m_pRequest,
  766. pForward->m_ChallengeContext,
  767. pForward->m_cbChallengeResponse,
  768. pForward->m_pbChallengeResponse,
  769. pForward->m_cbOldLicense,
  770. pForward->m_pbOldLicense,
  771. pcbEncodedCert,
  772. ppbEncodedCert,
  773. &dwErrCode
  774. );
  775. if (ERROR_SUCCESS == dwStatus
  776. && LSERVER_S_SUCCESS == dwErrCode)
  777. {
  778. *pdwSupportFlags = dwSupportFlagsTemp;
  779. goto licenseReissued;
  780. }
  781. }
  782. // other failure cases just follow the existing codepath
  783. dwStatus = ERROR_SUCCESS;
  784. }
  785. }
  786. if(CanIssuePermLicense())
  787. {
  788. DWORD dwQuantity = 1;
  789. //
  790. // Try to issue a new license from local
  791. // if this server is registered
  792. //
  793. dwStatus = TLSDBIssueNewLicenseFromLocal(
  794. USEHANDLE(pDbWkSpace),
  795. pRequest,
  796. TRUE, // bFindLostLicense
  797. FALSE, // bRequireTempLicense
  798. FALSE, // bAcceptFewerLicenses
  799. &dwQuantity,
  800. &NewLicProduct,
  801. *pdwSupportFlags
  802. );
  803. if (TLS_I_FOUND_TEMPORARY_LICENSE == dwStatus)
  804. {
  805. // Found a temporary license; not what we want
  806. dwStatus = TLS_E_RECORD_NOTFOUND;
  807. }
  808. else
  809. {
  810. dwTried = PERMANENT_ISSUE_TRIED;
  811. }
  812. }
  813. else
  814. {
  815. dwStatus = TLS_E_NO_CERTIFICATE;
  816. }
  817. if(dwStatus != ERROR_SUCCESS && bForwardRequest == FALSE)
  818. {
  819. //
  820. // If remote server can't handle upgrade, we don't do anything but
  821. // return the license back to client, don't try to issue a temp.
  822. // license for this client if we are not the original contact
  823. // of client
  824. //
  825. goto cleanup;
  826. }
  827. if((dwStatus == TLS_E_PRODUCT_NOTINSTALL ||
  828. dwStatus == TLS_E_NO_CERTIFICATE ||
  829. dwStatus == TLS_E_NO_LICENSE ||
  830. dwStatus == TLS_E_RECORD_NOTFOUND) && bForwardRequest)
  831. {
  832. //
  833. // release our DB handle and forward request to other server
  834. //
  835. ROLLBACK_TRANSACTION(pDbWorkSpace);
  836. FREEDBHANDLE(pDbWorkSpace);
  837. bDbHandleAcquired = FALSE;
  838. DWORD dwForwardStatus;
  839. DWORD dwSupportFlagsTemp = *pdwSupportFlags;
  840. dwForwardStatus = TLSForwardUpgradeRequest(
  841. pForward,
  842. &dwSupportFlagsTemp,
  843. pRequest,
  844. pcbEncodedCert,
  845. ppbEncodedCert
  846. );
  847. if(dwForwardStatus == TLS_I_SERVICE_STOP || dwForwardStatus == ERROR_SUCCESS)
  848. {
  849. if (dwForwardStatus == ERROR_SUCCESS)
  850. {
  851. *pdwSupportFlags = dwSupportFlagsTemp;
  852. }
  853. dwStatus = dwForwardStatus;
  854. goto cleanup;
  855. }
  856. }
  857. if(bDbHandleAcquired == FALSE)
  858. {
  859. if(ALLOCATEDBHANDLE(pDbWorkSpace, g_GeneralDbTimeout) == FALSE)
  860. {
  861. dwStatus = TLS_E_ALLOCATE_HANDLE;
  862. goto cleanup;
  863. }
  864. CLEANUPSTMT;
  865. BEGIN_TRANSACTION(pDbWorkSpace);
  866. bDbHandleAcquired = TRUE;
  867. }
  868. }
  869. //
  870. // if can't get license from remote, try temporary
  871. //
  872. // always issue a temporary license
  873. if((dwStatus == TLS_E_PRODUCT_NOTINSTALL ||
  874. dwStatus == TLS_E_NO_CERTIFICATE ||
  875. dwStatus == TLS_E_NO_LICENSE ||
  876. dwStatus == TLS_E_RECORD_NOTFOUND) && bAcceptTempLicense)
  877. {
  878. // Issue a temporary license if can't allocate a permenent license
  879. if( TLSDBIssueTemporaryLicense(
  880. USEHANDLE(pDbWkSpace),
  881. pRequest,
  882. pNotBefore,
  883. pNotAfter,
  884. &NewLicProduct
  885. ) == ERROR_SUCCESS )
  886. {
  887. dwStatus = TLS_W_TEMPORARY_LICENSE_ISSUED;
  888. dwTried = TEMPORARY_ISSUE_TRIED;
  889. }
  890. }
  891. //
  892. // If we can find a server to upgrade or we can't issue temp
  893. // license, get out.
  894. //
  895. if(TLS_ERROR(dwStatus) == TRUE)
  896. {
  897. goto cleanup;
  898. }
  899. licenseReissued:
  900. //
  901. // Determine which licensed product should be in the license blob
  902. //
  903. pGenCertProduct = (PTLSDBLICENSEDPRODUCT)AllocateMemory(
  904. sizeof(TLSDBLICENSEDPRODUCT)*(dwNumLicProduct+1)
  905. );
  906. if(pGenCertProduct == NULL)
  907. {
  908. dwStatus = TLS_E_ALLOCATE_MEMORY;
  909. goto cleanup;
  910. }
  911. dwNumNewLicProduct = 0;
  912. //
  913. // Copy all licensed product with version greated than requested
  914. //
  915. for( index = 0;
  916. index < dwNumLicProduct && CompareTLSVersions((pLicProduct+index)->LicensedProduct.pProductInfo->dwVersion, NewLicProduct.dwProductVersion) > 0;
  917. index++, dwNumNewLicProduct++)
  918. {
  919. LicensedProductToDbLicensedProduct( pLicProduct+index, pGenCertProduct+dwNumNewLicProduct );
  920. }
  921. //
  922. // Append new license
  923. //
  924. *(pGenCertProduct+index) = NewLicProduct;
  925. dwNumNewLicProduct++;
  926. //
  927. // Append licensed product older than request
  928. //
  929. for(;index < dwNumLicProduct;index++)
  930. {
  931. BOOL bTemp;
  932. BOOL bDifferentProduct;
  933. BOOL bNotNewerVersion = (CompareTLSVersions(NewLicProduct.dwProductVersion, (pLicProduct+index)->LicensedProduct.pProductInfo->dwVersion) <= 0);
  934. bTemp = (((pLicProduct+index)->pLicensedVersion->dwFlags & LICENSED_VERSION_TEMPORARY) != 0);
  935. // if we are running on RTM server, treat license issued from beta server as temporary license
  936. if(bTemp == FALSE && TLSIsBetaNTServer() == FALSE)
  937. {
  938. bTemp = (IS_LICENSE_ISSUER_RTM((pLicProduct+index)->pLicensedVersion->dwFlags) == FALSE);
  939. }
  940. bDifferentProduct = (_tcscmp(NewLicProduct.szLicensedProductId, (LPTSTR)(pLicProduct+index)->LicensedProduct.pProductInfo->pbProductID) != 0);
  941. if (bNotNewerVersion && !bDifferentProduct && !(bTemp || fReissue))
  942. {
  943. //
  944. // we can't issue same version for the same product unless the old
  945. // one was a temp or it is being re-issued
  946. //
  947. SetLastError(dwStatus = TLS_E_INTERNAL);
  948. goto cleanup;
  949. }
  950. if(NewLicProduct.bTemp == FALSE || bTemp == TRUE)
  951. {
  952. if( IS_LICENSE_ISSUER_RTM((pLicProduct+index)->pLicensedVersion->dwFlags) == FALSE &&
  953. TLSIsBetaNTServer() == FALSE )
  954. {
  955. // we wipe out beta database so ignore return.
  956. continue;
  957. }
  958. if(_tcsicmp(pLicProduct->szIssuerId, (LPTSTR)g_pszServerPid) == 0)
  959. {
  960. //
  961. // Convert LicensedProduct to TLSLicenseToBeReturn
  962. // TODO - have its own version.
  963. //
  964. TLSLicenseToBeReturn tobeReturn;
  965. tobeReturn.dwQuantity = (pLicProduct+index)->dwQuantity;
  966. tobeReturn.dwKeyPackId = (pLicProduct+index)->ulSerialNumber.HighPart;
  967. tobeReturn.dwLicenseId = (pLicProduct+index)->ulSerialNumber.LowPart;
  968. tobeReturn.dwPlatformID = (pLicProduct+index)->LicensedProduct.dwPlatformID;
  969. tobeReturn.cbEncryptedHwid = (pLicProduct+index)->LicensedProduct.cbEncryptedHwid;
  970. tobeReturn.pbEncryptedHwid = (pLicProduct+index)->LicensedProduct.pbEncryptedHwid;
  971. tobeReturn.dwProductVersion = MAKELONG(
  972. (pLicProduct+index)->pLicensedVersion->wMinorVersion,
  973. (pLicProduct+index)->pLicensedVersion->wMajorVersion
  974. );
  975. tobeReturn.pszOrgProductId = (LPTSTR)(pLicProduct+index)->pbOrgProductID;
  976. tobeReturn.pszCompanyName = (LPTSTR) (pLicProduct+index)->LicensedProduct.pProductInfo->pbCompanyName;
  977. tobeReturn.pszProductId = (LPTSTR) (pLicProduct+index)->LicensedProduct.pProductInfo->pbProductID;
  978. tobeReturn.pszUserName = (LPTSTR) (pLicProduct+index)->szLicensedUser;
  979. tobeReturn.pszMachineName = (pLicProduct+index)->szLicensedClient;
  980. dwStatus = TLSReturnClientLicensedProduct(
  981. USEHANDLE(pDbWkSpace),
  982. pRequest->hClient,
  983. pRequest->pPolicy,
  984. &tobeReturn
  985. );
  986. }
  987. else
  988. {
  989. dwStatus = TLSPostReturnClientLicenseJob( pLicProduct+index );
  990. }
  991. //
  992. // Ignore can't find the record in database
  993. //
  994. dwStatus = ERROR_SUCCESS;
  995. }
  996. else
  997. {
  998. LicensedProductToDbLicensedProduct( pLicProduct + index, pGenCertProduct + dwNumNewLicProduct);
  999. dwNumNewLicProduct++;
  1000. }
  1001. }
  1002. dwLicGenStatus = TLSGenerateClientCertificate(
  1003. g_hCryptProv,
  1004. dwNumNewLicProduct,
  1005. pGenCertProduct,
  1006. pRequest->wLicenseDetail,
  1007. ppbEncodedCert,
  1008. pcbEncodedCert
  1009. );
  1010. if(dwLicGenStatus != ERROR_SUCCESS)
  1011. {
  1012. dwStatus = dwLicGenStatus;
  1013. }
  1014. cleanup:
  1015. if(bDbHandleAcquired == TRUE)
  1016. {
  1017. if(TLS_ERROR(dwStatus))
  1018. {
  1019. ROLLBACK_TRANSACTION(pDbWorkSpace);
  1020. }
  1021. else
  1022. {
  1023. COMMIT_TRANSACTION(pDbWorkSpace);
  1024. switch (dwTried)
  1025. {
  1026. case PERMANENT_ISSUE_TRIED:
  1027. InterlockedIncrement(&g_lPermanentLicensesIssued);
  1028. break;
  1029. case TEMPORARY_ISSUE_TRIED:
  1030. InterlockedIncrement(&g_lTemporaryLicensesIssued);
  1031. break;
  1032. case PERMANENT_REISSUE_TRIED:
  1033. InterlockedIncrement(&g_lPermanentLicensesReissued);
  1034. break;
  1035. }
  1036. }
  1037. FREEDBHANDLE(pDbWorkSpace);
  1038. }
  1039. if(TLS_ERROR(dwStatus) == FALSE)
  1040. {
  1041. if(NewLicProduct.dwNumLicenseLeft == 0 && NewLicProduct.bTemp == FALSE)
  1042. {
  1043. // ignore error if we can't get it out to
  1044. // other server
  1045. TLSAnnounceLKPToAllRemoteServer(NewLicProduct.dwKeyPackId, 0);
  1046. }
  1047. }
  1048. FreeMemory(pGenCertProduct);
  1049. return dwStatus;
  1050. }
  1051. //----------------------------------------------------------
  1052. DWORD
  1053. TLSNewLicenseRequest(
  1054. IN BOOL bForwardRequest,
  1055. IN OUT DWORD *pdwSupportFlags,
  1056. IN PTLSForwardNewLicenseRequest pForward,
  1057. IN PTLSDBLICENSEREQUEST pRequest,
  1058. IN BOOL bAcceptTempLicense,
  1059. IN BOOL bRequireTempLicense,
  1060. IN BOOL bFindLostLicense,
  1061. IN BOOL bAcceptFewerLicenses,
  1062. IN OUT DWORD *pdwQuantity,
  1063. OUT PDWORD pcbEncodedCert,
  1064. OUT PBYTE* ppbEncodedCert
  1065. )
  1066. /*++
  1067. Abstract:
  1068. Parameter:
  1069. Returns:
  1070. ++*/
  1071. {
  1072. DWORD dwStatus = TLS_E_NO_LICENSE;
  1073. TLSDBLICENSEDPRODUCT LicensedProduct;
  1074. PTLSDbWorkSpace pDbWorkSpace=NULL;
  1075. BOOL bDbHandleAcquired = FALSE;
  1076. DWORD dwSupportFlagsTemp = *pdwSupportFlags;
  1077. DWORD dwTried = NONE_TRIED;
  1078. if(ALLOCATEDBHANDLE(pDbWorkSpace, g_GeneralDbTimeout) == FALSE)
  1079. {
  1080. dwStatus = TLS_E_ALLOCATE_HANDLE;
  1081. goto cleanup;
  1082. }
  1083. CLEANUPSTMT;
  1084. BEGIN_TRANSACTION(pDbWorkSpace);
  1085. bDbHandleAcquired = TRUE;
  1086. try {
  1087. dwStatus = TLSDBIssueNewLicenseFromLocal(
  1088. USEHANDLE(pDbWorkSpace),
  1089. pRequest,
  1090. bFindLostLicense,
  1091. bRequireTempLicense,
  1092. bAcceptFewerLicenses,
  1093. pdwQuantity,
  1094. &LicensedProduct,
  1095. *pdwSupportFlags
  1096. );
  1097. dwTried = PERMANENT_ISSUE_TRIED;
  1098. }
  1099. catch( SE_Exception e ) {
  1100. dwStatus = e.getSeNumber();
  1101. }
  1102. catch(...) {
  1103. SetLastError(dwStatus = TLS_E_INTERNAL);
  1104. }
  1105. if (!bRequireTempLicense)
  1106. {
  1107. if( (dwStatus == TLS_E_PRODUCT_NOTINSTALL || dwStatus == TLS_I_FOUND_TEMPORARY_LICENSE ||
  1108. dwStatus == TLS_E_NO_LICENSE || dwStatus == TLS_E_NO_CERTIFICATE ||
  1109. dwStatus == TLS_E_RECORD_NOTFOUND) && bForwardRequest == TRUE )
  1110. {
  1111. //
  1112. // release our DB handle so others can proceed
  1113. //
  1114. ROLLBACK_TRANSACTION(pDbWorkSpace);
  1115. FREEDBHANDLE(pDbWorkSpace);
  1116. bDbHandleAcquired = FALSE;
  1117. DWORD dwForwardStatus;
  1118. DWORD dwQuantityTemp = *pdwQuantity;
  1119. //
  1120. // forward call here
  1121. //
  1122. dwForwardStatus = TLSForwardLicenseRequest(
  1123. pForward,
  1124. &dwSupportFlagsTemp,
  1125. pRequest,
  1126. bAcceptFewerLicenses,
  1127. &dwQuantityTemp,
  1128. pcbEncodedCert,
  1129. ppbEncodedCert
  1130. );
  1131. if(dwForwardStatus == TLS_I_SERVICE_STOP)
  1132. {
  1133. dwStatus = dwForwardStatus;
  1134. goto cleanup;
  1135. }
  1136. if(dwForwardStatus == ERROR_SUCCESS)
  1137. {
  1138. //
  1139. // remote server is able to issue perm. license,
  1140. // delete the license we are holding
  1141. //
  1142. *pdwSupportFlags = dwSupportFlagsTemp;
  1143. *pdwQuantity = dwQuantityTemp;
  1144. if(dwStatus == TLS_E_LICENSE_EXPIRED || dwStatus == TLS_I_FOUND_TEMPORARY_LICENSE)
  1145. {
  1146. //
  1147. // re-acquire DB handle only if we going to issue
  1148. // a temporary license
  1149. //
  1150. if(ALLOCATEDBHANDLE(pDbWorkSpace, g_GeneralDbTimeout) == FALSE)
  1151. {
  1152. dwStatus = TLS_E_ALLOCATE_HANDLE;
  1153. goto cleanup;
  1154. }
  1155. CLEANUPSTMT;
  1156. BEGIN_TRANSACTION(pDbWorkSpace);
  1157. bDbHandleAcquired = TRUE;
  1158. //
  1159. // need to mark this license has been upgraded
  1160. //
  1161. dwStatus = TLSDBMarkClientLicenseUpgraded(
  1162. USEHANDLE(pDbWorkSpace),
  1163. pRequest,
  1164. &LicensedProduct
  1165. );
  1166. if(TLS_ERROR(dwStatus))
  1167. {
  1168. ROLLBACK_TRANSACTION(pDbWorkSpace);
  1169. }
  1170. else
  1171. {
  1172. COMMIT_TRANSACTION(pDbWorkSpace);
  1173. }
  1174. bDbHandleAcquired = FALSE;
  1175. FREEDBHANDLE(pDbWorkSpace);
  1176. }
  1177. dwStatus = ERROR_SUCCESS;
  1178. // exit right here so we don't re-generate
  1179. // certificate
  1180. goto cleanup;
  1181. }
  1182. }
  1183. }
  1184. //
  1185. // if can't get license from remote, try temporary
  1186. //
  1187. // always issue a temporary license
  1188. if((dwStatus == TLS_E_PRODUCT_NOTINSTALL ||
  1189. dwStatus == TLS_E_NO_CERTIFICATE ||
  1190. dwStatus == TLS_E_NO_LICENSE ||
  1191. dwStatus == TLS_E_RECORD_NOTFOUND) && bAcceptTempLicense)
  1192. {
  1193. if(bDbHandleAcquired == FALSE)
  1194. {
  1195. //
  1196. // re-acquire DB handle only if we going to issue
  1197. // a temporary license
  1198. //
  1199. if(ALLOCATEDBHANDLE(pDbWorkSpace, g_GeneralDbTimeout) == FALSE)
  1200. {
  1201. dwStatus = TLS_E_ALLOCATE_HANDLE;
  1202. goto cleanup;
  1203. }
  1204. CLEANUPSTMT;
  1205. BEGIN_TRANSACTION(pDbWorkSpace);
  1206. bDbHandleAcquired = TRUE;
  1207. }
  1208. try {
  1209. // Issue a temporary license if can't allocate a permenent license
  1210. dwStatus=TLSDBIssueTemporaryLicense(
  1211. USEHANDLE(pDbWorkSpace),
  1212. pRequest,
  1213. NULL,
  1214. NULL,
  1215. &LicensedProduct
  1216. );
  1217. if(dwStatus == ERROR_SUCCESS)
  1218. {
  1219. dwTried = TEMPORARY_ISSUE_TRIED;
  1220. dwStatus = TLS_W_TEMPORARY_LICENSE_ISSUED;
  1221. }
  1222. }
  1223. catch( SE_Exception e ) {
  1224. dwStatus = e.getSeNumber();
  1225. }
  1226. catch(...) {
  1227. SetLastError(dwStatus = TLS_E_INTERNAL);
  1228. }
  1229. }
  1230. if(bDbHandleAcquired == TRUE)
  1231. {
  1232. if(TLS_ERROR(dwStatus))
  1233. {
  1234. ROLLBACK_TRANSACTION(pDbWorkSpace);
  1235. }
  1236. else
  1237. {
  1238. COMMIT_TRANSACTION(pDbWorkSpace);
  1239. switch (dwTried)
  1240. {
  1241. case PERMANENT_ISSUE_TRIED:
  1242. InterlockedExchangeAdd(&g_lPermanentLicensesIssued,
  1243. *pdwQuantity);
  1244. break;
  1245. case TEMPORARY_ISSUE_TRIED:
  1246. InterlockedIncrement(&g_lTemporaryLicensesIssued);
  1247. break;
  1248. }
  1249. }
  1250. FREEDBHANDLE(pDbWorkSpace);
  1251. }
  1252. //
  1253. // actually generate client certificate.
  1254. //
  1255. if(TLS_ERROR(dwStatus) == FALSE)
  1256. {
  1257. DWORD dwLicGenStatus;
  1258. //
  1259. // Post ssync job to inform other machine to delete this
  1260. // entry
  1261. //
  1262. if(LicensedProduct.dwNumLicenseLeft == 0 && LicensedProduct.bTemp == FALSE)
  1263. {
  1264. // ignore error if we can't get it out to
  1265. // other server
  1266. TLSAnnounceLKPToAllRemoteServer(LicensedProduct.dwKeyPackId, 0);
  1267. }
  1268. dwLicGenStatus = TLSGenerateClientCertificate(
  1269. g_hCryptProv,
  1270. 1, // dwNumLicensedProduct
  1271. &LicensedProduct,
  1272. pRequest->wLicenseDetail,
  1273. ppbEncodedCert,
  1274. pcbEncodedCert
  1275. );
  1276. if(dwLicGenStatus != ERROR_SUCCESS)
  1277. {
  1278. dwStatus = dwLicGenStatus;
  1279. }
  1280. };
  1281. cleanup:
  1282. return dwStatus;
  1283. }
  1284. //----------------------------------------------------------
  1285. DWORD
  1286. TLSCheckLicenseMarkRequest(
  1287. IN BOOL bForwardRequest,
  1288. IN PLICENSEDPRODUCT pLicProduct,
  1289. IN DWORD cbLicense,
  1290. IN PBYTE pLicense,
  1291. OUT PUCHAR pucMarkFlags
  1292. )
  1293. {
  1294. DWORD dwStatus = TLS_E_RECORD_NOTFOUND;
  1295. DWORD dwErrCode = ERROR_SUCCESS;
  1296. LICENSEDCLIENT licClient;
  1297. // NB: licenses are in descending order, so use the first one
  1298. if ((bForwardRequest) &&
  1299. (_tcsicmp(pLicProduct->szIssuerId, (LPTSTR)g_pszServerPid) != 0))
  1300. {
  1301. // Check remote license server
  1302. TCHAR szServer[LSERVER_MAX_STRING_SIZE+2];
  1303. TCHAR *pszServer = szServer;
  1304. TLS_HANDLE hHandle;
  1305. dwStatus = TLSResolveServerIdToServer(pLicProduct->szIssuerId,
  1306. szServer);
  1307. if (dwStatus != ERROR_SUCCESS)
  1308. {
  1309. // id not registered; use name
  1310. pszServer = pLicProduct->szIssuer;
  1311. }
  1312. hHandle = TLSConnectAndEstablishTrust(pszServer, NULL);
  1313. if(hHandle == NULL)
  1314. {
  1315. dwStatus = GetLastError();
  1316. }
  1317. // RPC to remote license server
  1318. dwStatus = TLSCheckLicenseMark(
  1319. hHandle,
  1320. cbLicense,
  1321. pLicense,
  1322. pucMarkFlags,
  1323. &dwErrCode
  1324. );
  1325. TLSDisconnectFromServer(hHandle);
  1326. if ((dwStatus == ERROR_SUCCESS) && (dwErrCode == LSERVER_S_SUCCESS))
  1327. {
  1328. goto cleanup;
  1329. }
  1330. }
  1331. // we're issuing server, or issuing server not found; try looking up HWID
  1332. dwStatus = TLSFindLicense(pLicProduct,&licClient);
  1333. if (ERROR_SUCCESS == dwStatus)
  1334. {
  1335. // this field is being reused for marking (e.g. user is authenticated)
  1336. *pucMarkFlags = licClient.ucEntryStatus;
  1337. }
  1338. cleanup:
  1339. return dwStatus;
  1340. }
  1341. //----------------------------------------------------------
  1342. DWORD
  1343. TLSMarkLicenseRequest(
  1344. IN BOOL bForwardRequest,
  1345. IN UCHAR ucMarkFlags,
  1346. IN PLICENSEDPRODUCT pLicProduct,
  1347. IN DWORD cbLicense,
  1348. IN PBYTE pLicense
  1349. )
  1350. {
  1351. DWORD dwStatus = TLS_E_RECORD_NOTFOUND;
  1352. DWORD dwErrCode = ERROR_SUCCESS;
  1353. PTLSDbWorkSpace pDbWkSpace=NULL;
  1354. LICENSEDCLIENT license;
  1355. // NB: licenses are in descending order, so use the first one
  1356. if ((bForwardRequest) &&
  1357. (_tcsicmp(pLicProduct->szIssuerId, (LPTSTR)g_pszServerPid) != 0))
  1358. {
  1359. // Check remote license server
  1360. TCHAR szServer[LSERVER_MAX_STRING_SIZE+2];
  1361. TCHAR *pszServer = szServer;
  1362. TLS_HANDLE hHandle;
  1363. dwStatus = TLSResolveServerIdToServer(pLicProduct->szIssuerId,
  1364. szServer);
  1365. if (dwStatus != ERROR_SUCCESS)
  1366. {
  1367. // id not registered; use name
  1368. pszServer = pLicProduct->szIssuer;
  1369. }
  1370. hHandle = TLSConnectAndEstablishTrust(pszServer, NULL);
  1371. if(hHandle == NULL)
  1372. {
  1373. dwStatus = GetLastError();
  1374. }
  1375. // RPC to remote license server
  1376. dwStatus = TLSMarkLicense(
  1377. hHandle,
  1378. ucMarkFlags,
  1379. cbLicense,
  1380. pLicense,
  1381. &dwErrCode
  1382. );
  1383. TLSDisconnectFromServer(hHandle);
  1384. if ((dwStatus == ERROR_SUCCESS) && (dwErrCode == LSERVER_S_SUCCESS))
  1385. {
  1386. goto cleanup;
  1387. }
  1388. }
  1389. // we're issuing server, or issuing server not found; try looking up HWID
  1390. dwStatus = TLSFindLicense(pLicProduct,&license);
  1391. if((ERROR_SUCCESS == dwStatus) &&
  1392. (ALLOCATEDBHANDLE(pDbWkSpace, g_GeneralDbTimeout)))
  1393. {
  1394. CLEANUPSTMT;
  1395. BEGIN_TRANSACTION(pDbWkSpace);
  1396. TLSDBLockLicenseTable();
  1397. try {
  1398. license.ucEntryStatus |= ucMarkFlags;
  1399. dwStatus=TLSDBLicenseUpdateEntry(
  1400. USEHANDLE(pDbWkSpace),
  1401. LSLICENSE_SEARCH_MARK_FLAGS,
  1402. &license,
  1403. FALSE
  1404. );
  1405. }
  1406. catch(...) {
  1407. dwStatus = TLS_E_INTERNAL;
  1408. }
  1409. TLSDBUnlockLicenseTable();
  1410. if(TLS_ERROR(dwStatus))
  1411. {
  1412. ROLLBACK_TRANSACTION(pDbWkSpace);
  1413. }
  1414. else
  1415. {
  1416. COMMIT_TRANSACTION(pDbWkSpace);
  1417. InterlockedIncrement(&g_lLicensesMarked);
  1418. }
  1419. FREEDBHANDLE(pDbWkSpace);
  1420. }
  1421. else
  1422. {
  1423. dwStatus=TLS_E_ALLOCATE_HANDLE;
  1424. }
  1425. cleanup:
  1426. return dwStatus;
  1427. }