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.

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