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.

887 lines
25 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996-1998
  5. //
  6. // File: findlost.cpp
  7. //
  8. // Contents:
  9. // Find lost license
  10. //
  11. // History:
  12. // Feb 4, 98 HueiWang Created
  13. //---------------------------------------------------------------------------
  14. #include "pch.cpp"
  15. #include "globals.h"
  16. #include "findlost.h"
  17. #include "misc.h"
  18. #include "db.h"
  19. #include "clilic.h"
  20. #include "keypack.h"
  21. #include "kp.h"
  22. #include "lkpdesc.h"
  23. //++-------------------------------------------------------------------
  24. DWORD
  25. DBFindLicenseExact(
  26. IN PTLSDbWorkSpace pDbWkSpace,
  27. IN PLICENSEDPRODUCT pLicProduct,
  28. OUT LICENSEDCLIENT *pFoundLicense
  29. )
  30. /*++
  31. Abstract:
  32. Find license based on exact match of client HWID
  33. Parameter:
  34. pDbWkSpace : workspace handle.
  35. pLicProduct : product to request license.
  36. pFoundLicense: found license
  37. Returns:
  38. TLS_E_RECORD_NOTFOUND: HWID not found
  39. ++*/
  40. {
  41. DWORD dwStatus = ERROR_SUCCESS;
  42. BOOL bFound=FALSE;
  43. DWORD dwMatchHwidCount;
  44. LICENSEDCLIENT search_license;
  45. TLSLICENSEPACK search_keypack;
  46. TLSLICENSEPACK found_keypack;
  47. memset(&search_license, 0, sizeof(search_license));
  48. memset(pFoundLicense, 0, sizeof(LICENSEDCLIENT));
  49. search_license.dwSystemBiosChkSum = pLicProduct->Hwid.dwPlatformID;
  50. search_license.dwVideoBiosChkSum = pLicProduct->Hwid.Data1;
  51. search_license.dwFloppyBiosChkSum = pLicProduct->Hwid.Data2;
  52. search_license.dwHardDiskSize = pLicProduct->Hwid.Data3;
  53. search_license.dwRamSize = pLicProduct->Hwid.Data4;
  54. //
  55. // lock both tables -
  56. // Other threads might be in the process of allocating a temp. license
  57. // while this thread is searching
  58. //
  59. TLSDBLockKeyPackTable();
  60. TLSDBLockLicenseTable();
  61. dwStatus = TLSDBLicenseEnumBegin(
  62. pDbWkSpace,
  63. TRUE,
  64. LICENSE_COLUMN_SEARCH_HWID,
  65. &search_license
  66. );
  67. if(dwStatus != ERROR_SUCCESS)
  68. {
  69. goto cleanup;
  70. }
  71. while(bFound == FALSE)
  72. {
  73. dwStatus = TLSDBLicenseEnumNext(
  74. pDbWkSpace,
  75. pFoundLicense
  76. );
  77. if(dwStatus != ERROR_SUCCESS)
  78. {
  79. break;
  80. }
  81. //
  82. // Verify against client HWID
  83. //
  84. dwMatchHwidCount = 0;
  85. dwMatchHwidCount += (pFoundLicense->dwSystemBiosChkSum == pLicProduct->Hwid.dwPlatformID);
  86. dwMatchHwidCount += (pFoundLicense->dwVideoBiosChkSum == pLicProduct->Hwid.Data1);
  87. dwMatchHwidCount += (pFoundLicense->dwFloppyBiosChkSum == pLicProduct->Hwid.Data2);
  88. dwMatchHwidCount += (pFoundLicense->dwHardDiskSize == pLicProduct->Hwid.Data3);
  89. dwMatchHwidCount += (pFoundLicense->dwRamSize == pLicProduct->Hwid.Data4);
  90. if(dwMatchHwidCount != 5)
  91. {
  92. break;
  93. }
  94. //
  95. // See if this match our license pack
  96. //
  97. search_keypack.dwKeyPackId = pFoundLicense->dwKeyPackId;
  98. dwStatus = TLSDBKeyPackFind(
  99. pDbWkSpace,
  100. TRUE,
  101. LSKEYPACK_EXSEARCH_DWINTERNAL,
  102. &search_keypack,
  103. &found_keypack
  104. );
  105. if(dwStatus != ERROR_SUCCESS)
  106. {
  107. continue;
  108. }
  109. //
  110. // No actual license is issued for concurrent KeyPack.
  111. //
  112. if(found_keypack.ucAgreementType != LSKEYPACKTYPE_RETAIL &&
  113. found_keypack.ucAgreementType != LSKEYPACKTYPE_SELECT &&
  114. found_keypack.ucAgreementType != LSKEYPACKTYPE_OPEN &&
  115. found_keypack.ucAgreementType != LSKEYPACKTYPE_TEMPORARY &&
  116. found_keypack.ucAgreementType != LSKEYPACKTYPE_FREE )
  117. {
  118. continue;
  119. }
  120. UCHAR ucKeyPackStatus = found_keypack.ucKeyPackStatus & ~LSKEYPACKSTATUS_RESERVED;
  121. //
  122. // No license for pending activation key pack, use temporary license scheme.
  123. //
  124. if(ucKeyPackStatus != LSKEYPACKSTATUS_ACTIVE &&
  125. ucKeyPackStatus != LSKEYPACKSTATUS_TEMPORARY)
  126. {
  127. continue;
  128. }
  129. if(found_keypack.wMajorVersion != pLicProduct->pLicensedVersion->wMajorVersion ||
  130. found_keypack.wMinorVersion != pLicProduct->pLicensedVersion->wMinorVersion)
  131. {
  132. continue;
  133. }
  134. if(found_keypack.dwPlatformType != pLicProduct->LicensedProduct.dwPlatformID)
  135. {
  136. continue;
  137. }
  138. if(_tcsnicmp(found_keypack.szProductId,
  139. (LPTSTR)(pLicProduct->pbOrgProductID),
  140. ((pLicProduct->cbOrgProductID)/sizeof(TCHAR)) - 1)
  141. != 0)
  142. {
  143. continue;
  144. }
  145. //
  146. // Found our lost license.
  147. //
  148. bFound = TRUE;
  149. }
  150. TLSDBLicenseEnumEnd(pDbWkSpace);
  151. cleanup:
  152. if(dwStatus == TLS_I_NO_MORE_DATA)
  153. {
  154. SetLastError(dwStatus = TLS_E_RECORD_NOTFOUND);
  155. }
  156. TLSDBUnlockLicenseTable();
  157. TLSDBUnlockKeyPackTable();
  158. return dwStatus;
  159. }
  160. //++-------------------------------------------------------------------
  161. DWORD
  162. DBFindLostLicenseExact(
  163. IN PTLSDbWorkSpace pDbWkSpace,
  164. IN PTLSDBAllocateRequest pRequest, // ucKeyPackType not use
  165. // IN BOOL bMatchHwid,
  166. IN PHWID pHwid,
  167. IN OUT PTLSLICENSEPACK lpKeyPack,
  168. IN OUT PLICENSEDCLIENT lpLicense
  169. )
  170. /*++
  171. Abstract:
  172. Find lost license base on exact/closest match of client HWID
  173. Parameter:
  174. pDbWkSpace : workspace handle.
  175. pRequest : product to request license.
  176. bMatchHwid : TRUE if match HWID, FALSE otherwise.
  177. lpKeyPack : keyPack that license was issued from.
  178. lpLicense : Founded license record.
  179. Returns:
  180. ++*/
  181. {
  182. DWORD dwStatus = ERROR_SUCCESS;
  183. BOOL bFound=FALSE;
  184. DWORD dwMatchHwidCount;
  185. LICENSEDCLIENT search_license;
  186. LICENSEDCLIENT found_license;
  187. TLSLICENSEPACK search_keypack;
  188. TLSLICENSEPACK found_keypack;
  189. //
  190. // Ignore ucKeyPackType
  191. //
  192. pRequest->ucAgreementType = LSKEYPACKTYPE_FIRST;
  193. dwStatus = VerifyTLSDBAllocateRequest(pRequest);
  194. if(dwStatus != ERROR_SUCCESS)
  195. {
  196. return dwStatus;
  197. }
  198. memset(&search_license, 0, sizeof(search_license));
  199. memset(&found_license, 0, sizeof(found_license));
  200. search_license.dwSystemBiosChkSum = pHwid->dwPlatformID;
  201. search_license.dwVideoBiosChkSum = pHwid->Data1;
  202. search_license.dwFloppyBiosChkSum = pHwid->Data2;
  203. search_license.dwHardDiskSize = pHwid->Data3;
  204. search_license.dwRamSize = pHwid->Data4;
  205. //
  206. // lock both table -
  207. // Other thread might be in the process of allocating a temp. license while this
  208. // thread try to delete the temp. key pack.
  209. //
  210. TLSDBLockKeyPackTable();
  211. TLSDBLockLicenseTable();
  212. dwStatus = TLSDBLicenseEnumBegin(
  213. pDbWkSpace,
  214. TRUE,
  215. LICENSE_COLUMN_SEARCH_HWID,
  216. &search_license
  217. );
  218. if(dwStatus != ERROR_SUCCESS)
  219. {
  220. goto cleanup;
  221. }
  222. while(bFound == FALSE)
  223. {
  224. dwStatus = TLSDBLicenseEnumNext(
  225. pDbWkSpace,
  226. &found_license
  227. );
  228. if(dwStatus != ERROR_SUCCESS)
  229. {
  230. break;
  231. }
  232. //
  233. // Verify against client HWID
  234. //
  235. dwMatchHwidCount = 0;
  236. dwMatchHwidCount += (found_license.dwSystemBiosChkSum == pHwid->dwPlatformID);
  237. dwMatchHwidCount += (found_license.dwVideoBiosChkSum == pHwid->Data1);
  238. dwMatchHwidCount += (found_license.dwFloppyBiosChkSum == pHwid->Data2);
  239. dwMatchHwidCount += (found_license.dwHardDiskSize == pHwid->Data3);
  240. dwMatchHwidCount += (found_license.dwRamSize == pHwid->Data4);
  241. if(dwMatchHwidCount != 5)
  242. {
  243. break;
  244. }
  245. //
  246. // consider only valid license
  247. //
  248. if( found_license.ucLicenseStatus != LSLICENSE_STATUS_ACTIVE &&
  249. found_license.ucLicenseStatus != LSLICENSE_STATUS_PENDING &&
  250. found_license.ucLicenseStatus != LSLICENSE_STATUS_TEMPORARY)
  251. {
  252. continue;
  253. }
  254. //
  255. // See if this match our license pack
  256. //
  257. search_keypack.dwKeyPackId = found_license.dwKeyPackId;
  258. dwStatus = TLSDBKeyPackFind(
  259. pDbWkSpace,
  260. TRUE,
  261. LSKEYPACK_EXSEARCH_DWINTERNAL,
  262. &search_keypack,
  263. &found_keypack
  264. );
  265. if(dwStatus != ERROR_SUCCESS)
  266. {
  267. continue;
  268. }
  269. //
  270. // No actual license is issued for concurrent KeyPack.
  271. //
  272. if(found_keypack.ucAgreementType != LSKEYPACKTYPE_RETAIL &&
  273. found_keypack.ucAgreementType != LSKEYPACKTYPE_SELECT &&
  274. found_keypack.ucAgreementType != LSKEYPACKTYPE_OPEN &&
  275. found_keypack.ucAgreementType != LSKEYPACKTYPE_TEMPORARY &&
  276. found_keypack.ucAgreementType != LSKEYPACKTYPE_FREE )
  277. {
  278. continue;
  279. }
  280. UCHAR ucKeyPackStatus = found_keypack.ucKeyPackStatus & ~LSKEYPACKSTATUS_RESERVED;
  281. //
  282. // No license for pending activation key pack, use temporary license scheme.
  283. //
  284. if(ucKeyPackStatus != LSKEYPACKSTATUS_ACTIVE &&
  285. ucKeyPackStatus != LSKEYPACKSTATUS_TEMPORARY)
  286. {
  287. continue;
  288. }
  289. if(found_keypack.wMajorVersion != HIWORD(pRequest->dwVersion) ||
  290. found_keypack.wMinorVersion != LOWORD(pRequest->dwVersion) )
  291. {
  292. continue;
  293. }
  294. if(found_keypack.dwPlatformType != pRequest->dwPlatformId)
  295. {
  296. continue;
  297. }
  298. if(_tcscmp(found_keypack.szCompanyName, pRequest->szCompanyName) != 0)
  299. {
  300. continue;
  301. }
  302. if(_tcscmp(found_keypack.szProductId, pRequest->szProductId) != 0)
  303. {
  304. continue;
  305. }
  306. //
  307. // Found our lost license.
  308. //
  309. bFound = TRUE;
  310. *lpLicense = found_license;
  311. *lpKeyPack = found_keypack;
  312. }
  313. TLSDBLicenseEnumEnd(pDbWkSpace);
  314. cleanup:
  315. if(dwStatus == TLS_I_NO_MORE_DATA)
  316. {
  317. SetLastError(dwStatus = TLS_E_RECORD_NOTFOUND);
  318. }
  319. TLSDBUnlockLicenseTable();
  320. TLSDBUnlockKeyPackTable();
  321. return dwStatus;
  322. }
  323. //++-------------------------------------------------------------------
  324. DWORD
  325. DBFindLostLicenseMatch(
  326. IN PTLSDbWorkSpace pDbWkSpace,
  327. IN PTLSDBAllocateRequest pRequest, // ucKeyPackType not use
  328. IN BOOL bMatchHwid,
  329. IN PHWID pHwid,
  330. IN OUT PTLSLICENSEPACK lpKeyPack,
  331. IN OUT PLICENSEDCLIENT lpLicense
  332. )
  333. /*++
  334. Abstract:
  335. Find lost license base on exact/closest match of client HWID
  336. Parameter:
  337. pDbWkSpace : workspace handle.
  338. pRequest : product to request license.
  339. bMatchHwid : TRUE if match HWID, FALSE otherwise.
  340. lpKeyPack : keyPack that license was issued from.
  341. lpLicense : Founded license record.
  342. Returns:
  343. ++*/
  344. {
  345. DWORD status=ERROR_SUCCESS;
  346. BOOL bFound=FALSE;
  347. TLSLICENSEPACK search_keypack;
  348. TLSLICENSEPACK found_keypack;
  349. DWORD dwMatchHwidCount;
  350. found_keypack.pbDomainSid = NULL;
  351. //
  352. // Ignore ucKeyPackType
  353. //
  354. pRequest->ucAgreementType = LSKEYPACKTYPE_FIRST;
  355. status = VerifyTLSDBAllocateRequest(pRequest);
  356. if(status != ERROR_SUCCESS)
  357. return status;
  358. LICENSEDCLIENT search_license;
  359. LICENSEDCLIENT found_license;
  360. //---------------------------------------------------------------------------
  361. // Set up search parameter
  362. //
  363. memset(&search_keypack, 0, sizeof(search_keypack));
  364. _tcscpy(search_keypack.szCompanyName, pRequest->szCompanyName);
  365. _tcscpy(search_keypack.szProductId, pRequest->szProductId);
  366. search_keypack.wMajorVersion = HIWORD(pRequest->dwVersion);
  367. search_keypack.wMinorVersion = LOWORD(pRequest->dwVersion);
  368. search_keypack.dwPlatformType = pRequest->dwPlatformId;
  369. //
  370. // lock both table -
  371. // Other thread might be in the process of allocating a temp. license while this
  372. // thread try to delete the temp. key pack.
  373. //
  374. TLSDBLockKeyPackTable();
  375. TLSDBLockLicenseTable();
  376. //
  377. // KeyPack table index is based on keypack type
  378. //
  379. status=TLSDBKeyPackEnumBegin(
  380. pDbWkSpace,
  381. TRUE,
  382. LSKEYPACK_SEARCH_PRODUCTID,
  383. &search_keypack
  384. );
  385. if(status != ERROR_SUCCESS)
  386. goto cleanup;
  387. while( !bFound )
  388. {
  389. status = TLSDBKeyPackEnumNext(
  390. pDbWkSpace,
  391. &found_keypack
  392. );
  393. if(status != ERROR_SUCCESS)
  394. break;
  395. //
  396. // No actual license is issued for concurrent KeyPack.
  397. //
  398. if(found_keypack.ucAgreementType != LSKEYPACKTYPE_RETAIL &&
  399. found_keypack.ucAgreementType != LSKEYPACKTYPE_SELECT &&
  400. found_keypack.ucAgreementType != LSKEYPACKTYPE_OPEN &&
  401. found_keypack.ucAgreementType != LSKEYPACKTYPE_TEMPORARY &&
  402. found_keypack.ucAgreementType != LSKEYPACKTYPE_FREE )
  403. {
  404. continue;
  405. }
  406. UCHAR ucKeyPackStatus = found_keypack.ucKeyPackStatus & ~LSKEYPACKSTATUS_RESERVED;
  407. //
  408. // No license for pending activation key pack, use temporary license scheme.
  409. //
  410. if(ucKeyPackStatus != LSKEYPACKSTATUS_ACTIVE &&
  411. ucKeyPackStatus != LSKEYPACKSTATUS_TEMPORARY)
  412. {
  413. continue;
  414. }
  415. if(found_keypack.wMajorVersion != search_keypack.wMajorVersion ||
  416. found_keypack.wMinorVersion != search_keypack.wMinorVersion )
  417. {
  418. continue;
  419. }
  420. if(found_keypack.dwPlatformType != search_keypack.dwPlatformType)
  421. {
  422. continue;
  423. }
  424. if(_tcscmp(found_keypack.szCompanyName, search_keypack.szCompanyName) != 0)
  425. {
  426. continue;
  427. }
  428. if(_tcscmp(found_keypack.szProductId, search_keypack.szProductId) != 0)
  429. {
  430. continue;
  431. }
  432. // we found a candidate keypack, look into license table
  433. memset(&search_license, 0, sizeof(search_license));
  434. memset(&found_license, 0, sizeof(found_license));
  435. search_license.dwKeyPackId = found_keypack.dwKeyPackId;
  436. status = TLSDBLicenseEnumBegin(
  437. pDbWkSpace,
  438. TRUE,
  439. LICENSE_PROCESS_KEYPACKID,
  440. &search_license
  441. );
  442. if(status != ERROR_SUCCESS)
  443. break;
  444. while( bFound == FALSE )
  445. {
  446. status = TLSDBLicenseEnumNext(
  447. pDbWkSpace,
  448. &found_license
  449. );
  450. if(status != ERROR_SUCCESS)
  451. break;
  452. //
  453. // consider only valid license
  454. //
  455. if( found_license.ucLicenseStatus != LSLICENSE_STATUS_ACTIVE &&
  456. found_license.ucLicenseStatus != LSLICENSE_STATUS_PENDING &&
  457. found_license.ucLicenseStatus != LSLICENSE_STATUS_TEMPORARY)
  458. {
  459. continue;
  460. }
  461. //
  462. // Verify against client HWID
  463. //
  464. dwMatchHwidCount = 0;
  465. dwMatchHwidCount += (found_license.dwSystemBiosChkSum == pHwid->dwPlatformID);
  466. dwMatchHwidCount += (found_license.dwVideoBiosChkSum == pHwid->Data1);
  467. dwMatchHwidCount += (found_license.dwFloppyBiosChkSum == pHwid->Data2);
  468. dwMatchHwidCount += (found_license.dwHardDiskSize == pHwid->Data3);
  469. dwMatchHwidCount += (found_license.dwRamSize == pHwid->Data4);
  470. if(dwMatchHwidCount == 5 || (bMatchHwid == FALSE && dwMatchHwidCount >= LICENSE_MIN_MATCH))
  471. {
  472. bFound = TRUE;
  473. *lpLicense = found_license;
  474. *lpKeyPack = found_keypack;
  475. }
  476. }
  477. TLSDBLicenseEnumEnd(pDbWkSpace);
  478. }
  479. TLSDBKeyPackEnumEnd(pDbWkSpace);
  480. cleanup:
  481. //FreeTlsLicensePack(&found_keypack);
  482. if(status == TLS_I_NO_MORE_DATA)
  483. {
  484. SetLastError(status = TLS_E_RECORD_NOTFOUND);
  485. }
  486. TLSDBUnlockLicenseTable();
  487. TLSDBUnlockKeyPackTable();
  488. return status;
  489. }
  490. //++--------------------------------------------------------------------
  491. DWORD
  492. TLSFindLicense(
  493. IN PLICENSEDPRODUCT pLicProduct,
  494. OUT PLICENSEDCLIENT pLicClient
  495. )
  496. {
  497. PTLSDbWorkSpace pDbWkSpace = NULL;
  498. DWORD status = ERROR_SUCCESS;
  499. pDbWkSpace = AllocateWorkSpace(g_EnumDbTimeout);
  500. if(pDbWkSpace == NULL)
  501. {
  502. status=TLS_E_ALLOCATE_HANDLE;
  503. goto cleanup;
  504. }
  505. status = DBFindLicenseExact(pDbWkSpace,pLicProduct,pLicClient);
  506. ReleaseWorkSpace(&pDbWkSpace);
  507. cleanup:
  508. return status;
  509. }
  510. //++--------------------------------------------------------------------
  511. DWORD
  512. TLSFindDbLicensedProduct(
  513. IN PTLSDBLICENSEDPRODUCT pDbLicProduct,
  514. OUT PLICENSEDCLIENT pLicClient
  515. )
  516. {
  517. PTLSDbWorkSpace pDbWkSpace = NULL;
  518. DWORD status = ERROR_SUCCESS;
  519. LICENSEDPRODUCT LicProduct;
  520. LICENSED_VERSION_INFO LicVerInfo;
  521. pDbWkSpace = AllocateWorkSpace(g_EnumDbTimeout);
  522. if(pDbWkSpace == NULL)
  523. {
  524. status=TLS_E_ALLOCATE_HANDLE;
  525. goto cleanup;
  526. }
  527. memcpy(&(LicProduct.Hwid), &(pDbLicProduct->ClientHwid), sizeof(HWID));
  528. LicVerInfo.wMajorVersion = HIWORD(pDbLicProduct->dwProductVersion);
  529. LicVerInfo.wMinorVersion = LOWORD(pDbLicProduct->dwProductVersion);
  530. LicProduct.pLicensedVersion = &LicVerInfo;
  531. LicProduct.LicensedProduct.dwPlatformID = pDbLicProduct->dwPlatformID;
  532. LicProduct.pbOrgProductID = (PBYTE)(pDbLicProduct->szLicensedProductId);
  533. LicProduct.cbOrgProductID = _tcslen(pDbLicProduct->szLicensedProductId) * sizeof(TCHAR);
  534. status = DBFindLicenseExact(pDbWkSpace,&LicProduct,pLicClient);
  535. ReleaseWorkSpace(&pDbWkSpace);
  536. cleanup:
  537. return status;
  538. }
  539. //++--------------------------------------------------------------------
  540. DWORD
  541. TLSDBFindLostLicense(
  542. IN PTLSDbWorkSpace pDbWkSpace,
  543. IN PTLSDBLICENSEREQUEST pLicenseRequest,
  544. IN PHWID pHwid,
  545. IN OUT PTLSDBLICENSEDPRODUCT pLicensedProduct,
  546. OUT PUCHAR pucMarked
  547. )
  548. /*++
  549. Abstract:
  550. Wrapper to DBFindLostLicense().
  551. See DBFindLostLicense.
  552. ++*/
  553. {
  554. DWORD dwStatus=ERROR_SUCCESS;
  555. TLSLICENSEPACK keypack;
  556. LICENSEDCLIENT License;
  557. ULARGE_INTEGER ulSerialNumber;
  558. FILETIME notBefore;
  559. FILETIME notAfter;
  560. PMGENERATELICENSE PolModGenLicense;
  561. PPMCERTEXTENSION pPolModCertExtension=NULL;
  562. PMLICENSEREQUEST PolRequest;
  563. TLSDBAllocateRequest AllocateRequest;
  564. DWORD dwRetCode=ERROR_SUCCESS;
  565. keypack.pbDomainSid = NULL;
  566. AllocateRequest.szCompanyName = (LPTSTR)pLicenseRequest->pszCompanyName;
  567. AllocateRequest.szProductId = (LPTSTR)pLicenseRequest->pszProductId;
  568. AllocateRequest.dwVersion = pLicenseRequest->dwProductVersion;
  569. AllocateRequest.dwPlatformId = pLicenseRequest->dwPlatformID;
  570. AllocateRequest.dwLangId = pLicenseRequest->dwLanguageID;
  571. AllocateRequest.dwNumLicenses = 1;
  572. dwStatus = DBFindLostLicenseExact(
  573. pDbWkSpace,
  574. &AllocateRequest,
  575. //TRUE,
  576. pHwid,
  577. &keypack,
  578. &License
  579. );
  580. #if 0
  581. //
  582. // TermSrv does not support matching, comment out for now
  583. //
  584. if(dwStatus == TLS_E_RECORD_NOTFOUND)
  585. {
  586. //
  587. // find by matching, very expensive operation
  588. //
  589. dwStatus = DBFindLostLicenseMatch(
  590. pDbWkSpace,
  591. &AllocateRequest,
  592. FALSE,
  593. pHwid,
  594. &keypack,
  595. &License
  596. );
  597. if(dwStatus == ERROR_SUCCESS)
  598. {
  599. dwRetCode = TLS_W_LICENSE_PROXIMATE;
  600. }
  601. }
  602. #endif
  603. if(dwStatus != ERROR_SUCCESS)
  604. {
  605. goto cleanup;
  606. }
  607. UnixTimeToFileTime(License.ftIssueDate, &notBefore);
  608. UnixTimeToFileTime(License.ftExpireDate, &notAfter);
  609. ulSerialNumber.LowPart = License.dwLicenseId;
  610. ulSerialNumber.HighPart = keypack.dwKeyPackId;
  611. PolRequest.dwProductVersion = MAKELONG(keypack.wMinorVersion, keypack.wMajorVersion);
  612. PolRequest.pszProductId = (LPTSTR)keypack.szProductId;
  613. PolRequest.pszCompanyName = (LPTSTR)keypack.szCompanyName;
  614. PolRequest.dwLanguageId = pLicenseRequest->dwLanguageID;
  615. PolRequest.dwPlatformId = keypack.dwPlatformType;
  616. PolRequest.pszMachineName = License.szMachineName;
  617. PolRequest.pszUserName = License.szUserName;
  618. //
  619. // Inform Policy Module of license generation.
  620. //
  621. PolModGenLicense.pLicenseRequest = &PolRequest;
  622. PolModGenLicense.dwKeyPackType = keypack.ucAgreementType;
  623. PolModGenLicense.dwKeyPackId = keypack.dwKeyPackId;
  624. PolModGenLicense.dwKeyPackLicenseId = License.dwKeyPackLicenseId;
  625. PolModGenLicense.ClientLicenseSerialNumber = ulSerialNumber;
  626. PolModGenLicense.ftNotBefore = notBefore;
  627. PolModGenLicense.ftNotAfter = notAfter;
  628. dwStatus = pLicenseRequest->pPolicy->PMLicenseRequest(
  629. pLicenseRequest->hClient,
  630. REQUEST_GENLICENSE,
  631. (PVOID)&PolModGenLicense,
  632. (PVOID *)&pPolModCertExtension
  633. );
  634. if(dwStatus != ERROR_SUCCESS)
  635. {
  636. //
  637. // Error in policy module
  638. //
  639. goto cleanup;
  640. }
  641. //
  642. // Check error return from policy module
  643. //
  644. if(pPolModCertExtension != NULL)
  645. {
  646. if(pPolModCertExtension->pbData != NULL && pPolModCertExtension->cbData == 0 ||
  647. pPolModCertExtension->pbData == NULL && pPolModCertExtension->cbData != 0 )
  648. {
  649. // assuming no extension data
  650. pPolModCertExtension->cbData = 0;
  651. pPolModCertExtension->pbData = NULL;
  652. }
  653. if(CompareFileTime(&(pPolModCertExtension->ftNotBefore), &(pPolModCertExtension->ftNotAfter)) > 0)
  654. {
  655. //
  656. // invalid data return from policy module
  657. //
  658. TLSLogEvent(
  659. EVENTLOG_ERROR_TYPE,
  660. TLS_E_GENERATECLIENTELICENSE,
  661. dwStatus = TLS_E_POLICYMODULEERROR,
  662. pLicenseRequest->pPolicy->GetCompanyName(),
  663. pLicenseRequest->pPolicy->GetProductId()
  664. );
  665. goto cleanup;
  666. }
  667. //
  668. // Ignore not before and not after
  669. //
  670. }
  671. if(keypack.ucAgreementType == LSKEYPACKTYPE_TEMPORARY)
  672. {
  673. //
  674. // we found a temporary license
  675. //
  676. dwRetCode = TLS_I_FOUND_TEMPORARY_LICENSE;
  677. }
  678. //
  679. // License expired
  680. //
  681. if(License.ftExpireDate < time(NULL))
  682. {
  683. dwRetCode = TLS_E_LICENSE_EXPIRED;
  684. }
  685. //
  686. // Return licensed product
  687. //
  688. pLicensedProduct->pSubjectPublicKeyInfo = NULL;
  689. pLicensedProduct->dwQuantity = License.dwNumLicenses;
  690. pLicensedProduct->ulSerialNumber = ulSerialNumber;
  691. pLicensedProduct->dwKeyPackId = keypack.dwKeyPackId;
  692. pLicensedProduct->dwLicenseId = License.dwLicenseId;
  693. pLicensedProduct->dwKeyPackLicenseId = License.dwKeyPackLicenseId;
  694. pLicensedProduct->ClientHwid.dwPlatformID = License.dwSystemBiosChkSum;
  695. pLicensedProduct->ClientHwid.Data1 = License.dwVideoBiosChkSum;
  696. pLicensedProduct->ClientHwid.Data2 = License.dwFloppyBiosChkSum;
  697. pLicensedProduct->ClientHwid.Data3 = License.dwHardDiskSize;
  698. pLicensedProduct->ClientHwid.Data4 = License.dwRamSize;
  699. pLicensedProduct->bTemp = (keypack.ucAgreementType == LSKEYPACKTYPE_TEMPORARY);
  700. pLicensedProduct->NotBefore = notBefore;
  701. pLicensedProduct->NotAfter = notAfter;
  702. pLicensedProduct->dwProductVersion = MAKELONG(keypack.wMinorVersion, keypack.wMajorVersion);
  703. _tcscpy(pLicensedProduct->szCompanyName, keypack.szCompanyName);
  704. _tcscpy(pLicensedProduct->szLicensedProductId, keypack.szProductId);
  705. _tcscpy(pLicensedProduct->szRequestProductId, pLicenseRequest->pClientLicenseRequest->pszProductId);
  706. _tcscpy(pLicensedProduct->szUserName, License.szUserName);
  707. _tcscpy(pLicensedProduct->szMachineName, License.szMachineName);
  708. pLicensedProduct->dwLanguageID = pLicenseRequest->dwLanguageID;
  709. pLicensedProduct->dwPlatformID = pLicenseRequest->dwPlatformID;
  710. pLicensedProduct->pbPolicyData = (pPolModCertExtension) ? pPolModCertExtension->pbData : NULL;
  711. pLicensedProduct->cbPolicyData = (pPolModCertExtension) ? pPolModCertExtension->cbData : 0;
  712. if (NULL != pucMarked)
  713. {
  714. // this field is being reused for marking (e.g. user is authenticated)
  715. *pucMarked = License.ucEntryStatus;
  716. }
  717. cleanup:
  718. return (dwStatus == ERROR_SUCCESS) ? dwRetCode : dwStatus;
  719. }