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.

2704 lines
78 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Copyright (c) 1997-1999 Microsoft Corporation
  4. //
  5. // File: request.cpp
  6. //
  7. // Contents: TLS236 policy module routines
  8. //
  9. // History:
  10. //
  11. //---------------------------------------------------------------------------
  12. #include "pch.cpp"
  13. #include <rpc.h>
  14. #include <time.h>
  15. #define STRSAFE_NO_DEPRECATE
  16. #include <strsafe.h>
  17. extern CClientMgr* g_ClientMgr;
  18. HMODULE g_hInstance;
  19. #define NUMBER_PRODUCTSTRING_RESOURCE 3
  20. #define INDEX_COMPANYNAME 0
  21. #define INDEX_PRODUCTNAME 1
  22. #define INDEX_PRODUCTDESC 2
  23. #define NUMBER_OF_CLIENT_OS 5
  24. #define NUMBER_OF_TERMSRV_OS 4
  25. #define DENY_BUILTIN 0
  26. #define ALLOW_BUILTIN 1
  27. static char LicenseTable[NUMBER_OF_CLIENT_OS][NUMBER_OF_TERMSRV_OS] =
  28. {
  29. {DENY_BUILTIN, DENY_BUILTIN, DENY_BUILTIN, DENY_BUILTIN },
  30. {ALLOW_BUILTIN, DENY_BUILTIN, DENY_BUILTIN, DENY_BUILTIN },
  31. {ALLOW_BUILTIN, DENY_BUILTIN, DENY_BUILTIN, DENY_BUILTIN },
  32. {ALLOW_BUILTIN, DENY_BUILTIN, DENY_BUILTIN, DENY_BUILTIN },
  33. {ALLOW_BUILTIN, DENY_BUILTIN, DENY_BUILTIN, DENY_BUILTIN }
  34. };
  35. //
  36. // Windows 2000
  37. //
  38. LPTSTR g_pszUSFreeKeyPackProductDesc[NUMBER_PRODUCTSTRING_RESOURCE] = {
  39. US_IDS_COMPANYNAME,
  40. US_IDS_EX_PRODUCTNAME,
  41. US_IDS_EX_PRODUCTDESC
  42. };
  43. LPTSTR g_pszLocalizedFreeKeyPackProductDesc[NUMBER_PRODUCTSTRING_RESOURCE] = {
  44. NULL,
  45. NULL,
  46. NULL
  47. };
  48. LPTSTR g_pszUSStandardKeyPackProductString[NUMBER_PRODUCTSTRING_RESOURCE] = {
  49. US_IDS_COMPANYNAME,
  50. US_IDS_S_PRODUCTNAME,
  51. US_IDS_S_PRODUCTDESC
  52. };
  53. LPTSTR g_pszLocalizedStandardKeyPackProductString[NUMBER_PRODUCTSTRING_RESOURCE] = {
  54. NULL,
  55. NULL,
  56. NULL
  57. };
  58. LPTSTR g_pszUSInternetKeyPackProductDesc [NUMBER_PRODUCTSTRING_RESOURCE] = {
  59. US_IDS_COMPANYNAME,
  60. US_IDS_I_PRODUCTNAME,
  61. US_IDS_I_PRODUCTDESC
  62. };
  63. LPTSTR g_pszLocalizedInternetKeyPackProductDesc [NUMBER_PRODUCTSTRING_RESOURCE] = {
  64. NULL,
  65. NULL,
  66. NULL,
  67. };
  68. //
  69. // Whistler
  70. //
  71. LPTSTR g_pszUSStandardKeyPackProductString51[NUMBER_PRODUCTSTRING_RESOURCE] = {
  72. US_IDS_COMPANYNAME,
  73. US_IDS_S_PRODUCTNAME,
  74. US_IDS_S_PRODUCTDESC51
  75. };
  76. LPTSTR g_pszLocalizedStandardKeyPackProductString51[NUMBER_PRODUCTSTRING_RESOURCE] = {
  77. NULL,
  78. NULL,
  79. NULL
  80. };
  81. LPTSTR g_pszUSConcurrentKeyPackProductDesc51 [NUMBER_PRODUCTSTRING_RESOURCE] = {
  82. US_IDS_COMPANYNAME,
  83. US_IDS_C_PRODUCTNAME,
  84. US_IDS_C_PRODUCTDESC51
  85. };
  86. LPTSTR g_pszLocalizedConcurrentKeyPackProductDesc51 [NUMBER_PRODUCTSTRING_RESOURCE] = {
  87. NULL,
  88. NULL,
  89. NULL,
  90. };
  91. PMSUPPORTEDPRODUCT g_pszSupportedProduct[] = {
  92. { TERMSERV_PRODUCTID_CH, TERMSERV_PRODUCTID_SKU},
  93. { TERMSERV_INTERNET_CH, TERMSERV_INTERNET_SKU},
  94. { TERMSERV_CONCURRENT_CH, TERMSERV_CONCURRENT_SKU},
  95. { TERMSERV_WHISTLER_PRODUCTID_CH, TERMSERV_PRODUCTID_SKU}
  96. };
  97. DWORD g_dwNumSupportedProduct = sizeof(g_pszSupportedProduct)/sizeof(g_pszSupportedProduct[0]);
  98. DWORD g_dwVersion=CURRENT_TLSA02_VERSION;
  99. ////////////////////////////////////////////////////////
  100. LPTSTR
  101. LoadProductDescFromResource(
  102. IN DWORD dwResId,
  103. IN DWORD dwMaxSize
  104. )
  105. /*++
  106. Internal Routine
  107. --*/
  108. {
  109. LPTSTR pszString = NULL;
  110. pszString = (LPTSTR) MALLOC( sizeof(TCHAR) * (dwMaxSize + 1) );
  111. if(pszString != NULL)
  112. {
  113. if(LoadResourceString(dwResId, pszString, dwMaxSize + 1) == FALSE)
  114. {
  115. FREE(pszString);
  116. }
  117. }
  118. return pszString;
  119. }
  120. //-----------------------------------------------------
  121. void
  122. FreeProductDescString()
  123. {
  124. for(int i=0;
  125. i < sizeof(g_pszLocalizedFreeKeyPackProductDesc)/sizeof(g_pszLocalizedFreeKeyPackProductDesc[0]);
  126. i++)
  127. {
  128. if(g_pszLocalizedFreeKeyPackProductDesc[i] != NULL)
  129. {
  130. FREE(g_pszLocalizedFreeKeyPackProductDesc[i]);
  131. g_pszLocalizedFreeKeyPackProductDesc[i] = NULL;
  132. }
  133. }
  134. for(i=0;
  135. i < sizeof(g_pszLocalizedStandardKeyPackProductString)/sizeof(g_pszLocalizedStandardKeyPackProductString[0]);
  136. i++)
  137. {
  138. if(g_pszLocalizedStandardKeyPackProductString[i] != NULL)
  139. {
  140. FREE(g_pszLocalizedStandardKeyPackProductString[i]);
  141. g_pszLocalizedStandardKeyPackProductString[i] = NULL;
  142. }
  143. }
  144. for(i=0;
  145. i < sizeof(g_pszLocalizedInternetKeyPackProductDesc)/sizeof(g_pszLocalizedInternetKeyPackProductDesc[0]);
  146. i++)
  147. {
  148. if(g_pszLocalizedInternetKeyPackProductDesc[i] != NULL)
  149. {
  150. FREE(g_pszLocalizedInternetKeyPackProductDesc[i]);
  151. g_pszLocalizedInternetKeyPackProductDesc[i] = NULL;
  152. }
  153. }
  154. for(i=0;
  155. i < sizeof(g_pszLocalizedConcurrentKeyPackProductDesc51)/sizeof(g_pszLocalizedConcurrentKeyPackProductDesc51[0]);
  156. i++)
  157. {
  158. if(g_pszLocalizedConcurrentKeyPackProductDesc51[i] != NULL)
  159. {
  160. FREE(g_pszLocalizedConcurrentKeyPackProductDesc51[i]);
  161. g_pszLocalizedConcurrentKeyPackProductDesc51[i] = NULL;
  162. }
  163. }
  164. return;
  165. }
  166. ////////////////////////////////////////////////////////
  167. void
  168. InitPolicyModule(
  169. IN HMODULE hModule
  170. )
  171. /*++
  172. Initialize policy module, ignore error if we can't find localized string, we always
  173. insert a english product desc.
  174. --*/
  175. {
  176. BOOL bSuccess = TRUE;
  177. g_hInstance = hModule;
  178. //
  179. // Build IN CAL product desc.
  180. //
  181. g_pszLocalizedFreeKeyPackProductDesc[INDEX_COMPANYNAME] =
  182. LoadProductDescFromResource(
  183. IDS_COMPANYNAME,
  184. MAX_TERMSRV_PRODUCTID + 1
  185. );
  186. if(g_pszLocalizedFreeKeyPackProductDesc[INDEX_COMPANYNAME] == NULL)
  187. {
  188. bSuccess = FALSE;
  189. goto cleanup;
  190. }
  191. g_pszLocalizedFreeKeyPackProductDesc[INDEX_PRODUCTNAME] =
  192. LoadProductDescFromResource(
  193. IDS_EX_PRODUCTNAME,
  194. MAX_TERMSRV_PRODUCTID + 1
  195. );
  196. if(g_pszLocalizedFreeKeyPackProductDesc[INDEX_PRODUCTNAME] == NULL)
  197. {
  198. bSuccess = FALSE;
  199. goto cleanup;
  200. }
  201. g_pszLocalizedFreeKeyPackProductDesc[INDEX_PRODUCTDESC] =
  202. LoadProductDescFromResource(
  203. IDS_EX_PRODUCTDESC,
  204. MAX_TERMSRV_PRODUCTID + 1
  205. );
  206. if(g_pszLocalizedFreeKeyPackProductDesc[INDEX_PRODUCTDESC] == NULL)
  207. {
  208. bSuccess = FALSE;
  209. goto cleanup;
  210. }
  211. //
  212. // FULL CAL product Desc.
  213. //
  214. g_pszLocalizedStandardKeyPackProductString[INDEX_COMPANYNAME] =
  215. LoadProductDescFromResource(
  216. IDS_COMPANYNAME,
  217. MAX_TERMSRV_PRODUCTID + 1
  218. );
  219. if(g_pszLocalizedStandardKeyPackProductString[INDEX_COMPANYNAME] == NULL)
  220. {
  221. bSuccess = FALSE;
  222. goto cleanup;
  223. }
  224. g_pszLocalizedStandardKeyPackProductString[INDEX_PRODUCTNAME] =
  225. LoadProductDescFromResource(
  226. IDS_S_PRODUCTNAME,
  227. MAX_TERMSRV_PRODUCTID + 1
  228. );
  229. if(g_pszLocalizedStandardKeyPackProductString[INDEX_PRODUCTNAME] == NULL)
  230. {
  231. bSuccess = FALSE;
  232. goto cleanup;
  233. }
  234. g_pszLocalizedStandardKeyPackProductString[INDEX_PRODUCTDESC] =
  235. LoadProductDescFromResource(
  236. IDS_S_PRODUCTDESC,
  237. MAX_TERMSRV_PRODUCTID + 1
  238. );
  239. if(g_pszLocalizedStandardKeyPackProductString[INDEX_PRODUCTDESC] == NULL)
  240. {
  241. bSuccess = FALSE;
  242. goto cleanup;
  243. }
  244. //
  245. // Internet CAL product Desc.
  246. //
  247. g_pszLocalizedInternetKeyPackProductDesc[INDEX_COMPANYNAME] =
  248. LoadProductDescFromResource(
  249. IDS_COMPANYNAME,
  250. MAX_TERMSRV_PRODUCTID + 1
  251. );
  252. if(g_pszLocalizedInternetKeyPackProductDesc[INDEX_COMPANYNAME] == NULL)
  253. {
  254. bSuccess = FALSE;
  255. goto cleanup;
  256. }
  257. g_pszLocalizedInternetKeyPackProductDesc[INDEX_PRODUCTNAME] =
  258. LoadProductDescFromResource(
  259. IDS_I_PRODUCTNAME,
  260. MAX_TERMSRV_PRODUCTID + 1
  261. );
  262. if(g_pszLocalizedInternetKeyPackProductDesc[INDEX_PRODUCTNAME] == NULL)
  263. {
  264. bSuccess = FALSE;
  265. goto cleanup;
  266. }
  267. g_pszLocalizedInternetKeyPackProductDesc[INDEX_PRODUCTDESC] =
  268. LoadProductDescFromResource(
  269. IDS_I_PRODUCTDESC,
  270. MAX_TERMSRV_PRODUCTID + 1
  271. );
  272. if(g_pszLocalizedInternetKeyPackProductDesc[INDEX_PRODUCTDESC] == NULL)
  273. {
  274. bSuccess = FALSE;
  275. goto cleanup;
  276. }
  277. //
  278. // Whistler FULL CAL product Desc.
  279. //
  280. g_pszLocalizedStandardKeyPackProductString51[INDEX_COMPANYNAME] = LoadProductDescFromResource(
  281. IDS_COMPANYNAME,
  282. MAX_TERMSRV_PRODUCTID + 1
  283. );
  284. if(g_pszLocalizedStandardKeyPackProductString51[INDEX_COMPANYNAME] == NULL)
  285. {
  286. bSuccess = FALSE;
  287. goto cleanup;
  288. }
  289. g_pszLocalizedStandardKeyPackProductString51[INDEX_PRODUCTNAME] = LoadProductDescFromResource(
  290. IDS_S_PRODUCTNAME,
  291. MAX_TERMSRV_PRODUCTID + 1
  292. );
  293. if(g_pszLocalizedStandardKeyPackProductString51[INDEX_PRODUCTNAME] == NULL)
  294. {
  295. bSuccess = FALSE;
  296. goto cleanup;
  297. }
  298. g_pszLocalizedStandardKeyPackProductString51[INDEX_PRODUCTDESC] = LoadProductDescFromResource(
  299. IDS_S_PRODUCTDESC51,
  300. MAX_TERMSRV_PRODUCTID + 1
  301. );
  302. if(g_pszLocalizedStandardKeyPackProductString51[INDEX_PRODUCTDESC] == NULL)
  303. {
  304. bSuccess = FALSE;
  305. goto cleanup;
  306. }
  307. //
  308. // Whistler Concurrent CAL product Desc.
  309. //
  310. g_pszLocalizedConcurrentKeyPackProductDesc51[INDEX_COMPANYNAME] = LoadProductDescFromResource(
  311. IDS_COMPANYNAME,
  312. MAX_TERMSRV_PRODUCTID + 1
  313. );
  314. if(g_pszLocalizedConcurrentKeyPackProductDesc51[INDEX_COMPANYNAME] == NULL)
  315. {
  316. bSuccess = FALSE;
  317. goto cleanup;
  318. }
  319. g_pszLocalizedConcurrentKeyPackProductDesc51[INDEX_PRODUCTNAME] = LoadProductDescFromResource(
  320. IDS_C_PRODUCTNAME,
  321. MAX_TERMSRV_PRODUCTID + 1
  322. );
  323. if(g_pszLocalizedConcurrentKeyPackProductDesc51[INDEX_PRODUCTNAME] == NULL)
  324. {
  325. bSuccess = FALSE;
  326. goto cleanup;
  327. }
  328. g_pszLocalizedConcurrentKeyPackProductDesc51[INDEX_PRODUCTDESC] = LoadProductDescFromResource(
  329. IDS_C_PRODUCTDESC51,
  330. MAX_TERMSRV_PRODUCTID + 1
  331. );
  332. if(g_pszLocalizedConcurrentKeyPackProductDesc51[INDEX_PRODUCTDESC] == NULL)
  333. {
  334. bSuccess = FALSE;
  335. goto cleanup;
  336. }
  337. cleanup:
  338. if(bSuccess == FALSE)
  339. {
  340. FreeProductDescString();
  341. }
  342. return;
  343. }
  344. ////////////////////////////////////////////////////////
  345. BOOL
  346. LoadResourceString(
  347. IN DWORD dwId,
  348. IN OUT LPTSTR szBuf,
  349. IN DWORD dwBufSize
  350. )
  351. /*++
  352. ++*/
  353. {
  354. int dwRet=0;
  355. dwRet = LoadString(
  356. g_hInstance,
  357. dwId,
  358. szBuf,
  359. dwBufSize
  360. );
  361. return (dwRet != 0);
  362. }
  363. ////////////////////////////////////////////////////////
  364. DWORD
  365. AddA02KeyPack(
  366. IN LPCTSTR pszProductCode,
  367. IN DWORD dwVersion, // NT version
  368. IN BOOL bFreeOnly // add free license pack only
  369. )
  370. /*++
  371. ++*/
  372. {
  373. TCHAR szComputerName[MAX_COMPUTERNAME_LENGTH + 2];
  374. DWORD dwSize = MAX_COMPUTERNAME_LENGTH+1;
  375. TCHAR pszProductId[MAX_TERMSRV_PRODUCTID+1];
  376. TLS_HANDLE tlsHandle=NULL;
  377. DWORD dwStatus = ERROR_SUCCESS;
  378. DWORD dwLkpVersion=0;
  379. LPTSTR* pszDescs;
  380. LPTSTR* pszLocalizedDescs;
  381. //
  382. // Connect to license server
  383. //
  384. memset(szComputerName, 0, sizeof(szComputerName));
  385. if(GetComputerName(szComputerName, &dwSize) == FALSE)
  386. {
  387. dwStatus = GetLastError();
  388. goto cleanup;
  389. }
  390. tlsHandle = TLSConnectToLsServer( szComputerName );
  391. if(tlsHandle == NULL)
  392. {
  393. dwStatus = GetLastError();
  394. goto cleanup;
  395. }
  396. //
  397. // Windows 2000 resource string
  398. //
  399. if(HIWORD(dwVersion) < WINDOWS_VERSION_NT5)
  400. {
  401. dwLkpVersion = HIWORD(dwVersion);
  402. }
  403. else
  404. {
  405. dwLkpVersion = WINDOWS_VERSION_BASE + (HIWORD(dwVersion) - WINDOWS_VERSION_NT5);
  406. }
  407. if(_tcsicmp(pszProductCode, TERMSERV_PRODUCTID_SKU) == 0)
  408. {
  409. //
  410. // Add a free keypack
  411. //
  412. if (HIWORD(dwVersion) == 5 && LOWORD(dwVersion) == 0)
  413. {
  414. _stprintf(
  415. pszProductId,
  416. TERMSERV_PRODUCTID_FORMAT,
  417. TERMSERV_PRODUCTID_SKU,
  418. HIWORD(dwVersion),
  419. 0,
  420. TERMSERV_FREE_TYPE
  421. );
  422. pszDescs = g_pszUSFreeKeyPackProductDesc;
  423. pszLocalizedDescs = g_pszLocalizedFreeKeyPackProductDesc;
  424. dwStatus = InsertLicensePack(
  425. tlsHandle,
  426. dwVersion,
  427. dwLkpVersion,
  428. PLATFORMID_FREE,
  429. LSKEYPACKTYPE_FREE, // local license pack no replication
  430. pszProductId,
  431. pszProductId,
  432. pszDescs,
  433. pszLocalizedDescs
  434. );
  435. }
  436. if(bFreeOnly)
  437. {
  438. goto cleanup;
  439. }
  440. //
  441. // Don't add this if enforce licenses.
  442. //
  443. #if !defined(ENFORCE_LICENSING) || defined(PRIVATE_DBG)
  444. //
  445. // Add a full version keypack, platform type is always 0xFF
  446. //
  447. if (HIWORD(dwVersion) == 5 && (LOWORD(dwVersion) == 1 || LOWORD(dwVersion) == 2) )
  448. {
  449. _stprintf(
  450. pszProductId,
  451. TERMSERV_PRODUCTID_FORMAT,
  452. TERMSERV_PRODUCTID_SKU,
  453. HIWORD(dwVersion),
  454. 0,
  455. TERMSERV_FULLVERSION_TYPE
  456. );
  457. pszDescs = g_pszUSStandardKeyPackProductString51;
  458. pszLocalizedDescs = g_pszLocalizedStandardKeyPackProductString51;
  459. }
  460. else if (HIWORD(dwVersion) == 5 && LOWORD(dwVersion) == 0)
  461. {
  462. _stprintf(
  463. pszProductId,
  464. TERMSERV_PRODUCTID_FORMAT,
  465. TERMSERV_PRODUCTID_SKU,
  466. HIWORD(dwVersion),
  467. 0,
  468. TERMSERV_FULLVERSION_TYPE
  469. );
  470. pszDescs = g_pszUSStandardKeyPackProductString;
  471. pszLocalizedDescs = g_pszLocalizedStandardKeyPackProductString;
  472. }
  473. else
  474. {
  475. dwStatus = ERROR_INVALID_PARAMETER;
  476. goto cleanup;
  477. }
  478. dwStatus = InsertLicensePack(
  479. tlsHandle,
  480. dwVersion,
  481. dwLkpVersion,
  482. PLATFORMID_OTHERS,
  483. LSKEYPACKTYPE_RETAIL,
  484. pszProductId,
  485. pszProductId,
  486. pszDescs,
  487. pszLocalizedDescs
  488. );
  489. #endif
  490. }
  491. else if(_tcsicmp(pszProductCode, TERMSERV_INTERNET_SKU) == 0)
  492. {
  493. //
  494. // Don't add this if enforce licenses.
  495. //
  496. #if !defined(ENFORCE_LICENSING) || defined(PRIVATE_DBG)
  497. //
  498. // Add internet package
  499. //
  500. if (HIWORD(dwVersion) == 5 && LOWORD(dwVersion) == 0)
  501. {
  502. _stprintf(
  503. pszProductId,
  504. TERMSERV_PRODUCTID_FORMAT,
  505. TERMSERV_INTERNET_SKU,
  506. HIWORD(dwVersion),
  507. 0,
  508. TERMSERV_INTERNET_TYPE
  509. );
  510. pszDescs = g_pszUSInternetKeyPackProductDesc;
  511. pszLocalizedDescs = g_pszLocalizedInternetKeyPackProductDesc;
  512. }
  513. else
  514. {
  515. dwStatus = ERROR_INVALID_PARAMETER;
  516. goto cleanup;
  517. }
  518. dwStatus = InsertLicensePack(
  519. tlsHandle,
  520. dwVersion,
  521. dwLkpVersion,
  522. PLATFORMID_OTHERS,
  523. LSKEYPACKTYPE_RETAIL,
  524. pszProductId,
  525. pszProductId,
  526. pszDescs,
  527. pszLocalizedDescs
  528. );
  529. #endif
  530. }
  531. else if(_tcsicmp(pszProductCode, TERMSERV_CONCURRENT_SKU) == 0)
  532. {
  533. //
  534. // Don't add this if enforce licenses.
  535. //
  536. #if !defined(ENFORCE_LICENSING) || defined(PRIVATE_DBG)
  537. //
  538. // Add Concurrent package
  539. //
  540. if (HIWORD(dwVersion) == 5 && LOWORD(dwVersion) == 1)
  541. {
  542. _stprintf(
  543. pszProductId,
  544. TERMSERV_PRODUCTID_FORMAT,
  545. TERMSERV_CONCURRENT_SKU,
  546. HIWORD(dwVersion),
  547. 0,
  548. TERMSERV_CONCURRENT_TYPE
  549. );
  550. pszDescs = g_pszUSConcurrentKeyPackProductDesc51;
  551. pszLocalizedDescs = g_pszLocalizedConcurrentKeyPackProductDesc51;
  552. }
  553. else
  554. {
  555. dwStatus = ERROR_INVALID_PARAMETER;
  556. goto cleanup;
  557. }
  558. dwStatus = InsertLicensePack(
  559. tlsHandle,
  560. dwVersion,
  561. dwLkpVersion,
  562. PLATFORMID_OTHERS,
  563. LSKEYPACKTYPE_RETAIL,
  564. pszProductId,
  565. pszProductId,
  566. pszDescs,
  567. pszLocalizedDescs
  568. );
  569. #endif
  570. }
  571. cleanup:
  572. if(tlsHandle != NULL)
  573. {
  574. TLSDisconnectFromServer(tlsHandle);
  575. }
  576. return dwStatus;
  577. }
  578. ////////////////////////////////////////////////////////
  579. DWORD
  580. InsertLicensePack(
  581. IN TLS_HANDLE tlsHandle,
  582. IN DWORD dwProdVersion,
  583. IN DWORD dwDescVersion,
  584. IN DWORD dwPlatformType,
  585. IN UCHAR ucAgreementType,
  586. IN LPTSTR pszProductId,
  587. IN LPTSTR pszKeyPackId,
  588. IN LPTSTR pszUsDesc[],
  589. IN LPTSTR pszLocalizedDesc[]
  590. )
  591. /*++
  592. pdwResourceId
  593. US Company Name
  594. US Product Name
  595. US Product Desc
  596. Localize Company Name
  597. Localize Product Name
  598. Localize Product Desc
  599. ++*/
  600. {
  601. RPC_STATUS rpcStatus;
  602. BOOL bSuccess;
  603. DWORD dwStatus = ERROR_SUCCESS;
  604. TCHAR buffer[LSERVER_MAX_STRING_SIZE];
  605. struct tm expired_tm;
  606. LSKeyPack keypack;
  607. if(pszProductId == NULL || pszKeyPackId == NULL)
  608. {
  609. SetLastError(ERROR_INVALID_PARAMETER);
  610. goto cleanup;
  611. }
  612. memset(&keypack, 0, sizeof(keypack));
  613. keypack.ucKeyPackType = ucAgreementType;
  614. SAFESTRCPY(keypack.szKeyPackId, pszKeyPackId);
  615. SAFESTRCPY(keypack.szProductId, pszProductId);
  616. SAFESTRCPY(keypack.szCompanyName, pszUsDesc[INDEX_COMPANYNAME]);
  617. SAFESTRCPY(keypack.szProductName, pszUsDesc[INDEX_PRODUCTNAME]);
  618. SAFESTRCPY(keypack.szProductDesc, pszUsDesc[INDEX_PRODUCTDESC]);
  619. keypack.wMajorVersion = HIWORD(dwProdVersion);
  620. keypack.wMinorVersion = LOWORD(dwProdVersion);
  621. keypack.dwPlatformType = dwPlatformType;
  622. keypack.ucLicenseType = LSKEYPACKLICENSETYPE_NEW;
  623. keypack.dwLanguageId = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US);
  624. keypack.ucChannelOfPurchase = LSKEYPACKCHANNELOFPURCHASE_RETAIL;
  625. SAFESTRCPY(
  626. keypack.szBeginSerialNumber,
  627. _TEXT("0000001")
  628. );
  629. keypack.dwTotalLicenseInKeyPack = (ucAgreementType == LSKEYPACKTYPE_FREE) ? INT_MAX : 0;
  630. keypack.dwProductFlags = 0x00;
  631. keypack.ucKeyPackStatus = LSKEYPACKSTATUS_ACTIVE;
  632. keypack.dwActivateDate = (DWORD) time(NULL);
  633. memset(&expired_tm, 0, sizeof(expired_tm));
  634. expired_tm.tm_year = 2036 - 1900; // expire on 2036/1/1
  635. expired_tm.tm_mday = 1;
  636. keypack.dwExpirationDate = mktime(&expired_tm);
  637. rpcStatus = TLSKeyPackAdd(
  638. tlsHandle,
  639. &keypack,
  640. &dwStatus
  641. );
  642. if(rpcStatus != RPC_S_OK)
  643. {
  644. dwStatus = GetLastError();
  645. goto cleanup;
  646. }
  647. if(dwStatus != ERROR_SUCCESS)
  648. {
  649. goto cleanup;
  650. }
  651. if(GetSystemDefaultLangID() != keypack.dwLanguageId)
  652. {
  653. if( pszLocalizedDesc[INDEX_COMPANYNAME] != NULL &&
  654. pszLocalizedDesc[INDEX_PRODUCTNAME] != NULL &&
  655. pszLocalizedDesc[INDEX_PRODUCTDESC] != NULL
  656. )
  657. {
  658. //
  659. // Insert localize license pack description
  660. //
  661. keypack.dwLanguageId = GetSystemDefaultLangID();
  662. SAFESTRCPY(keypack.szCompanyName, pszLocalizedDesc[INDEX_COMPANYNAME]);
  663. SAFESTRCPY(keypack.szProductName, pszLocalizedDesc[INDEX_PRODUCTNAME]);
  664. SAFESTRCPY(keypack.szProductDesc, pszLocalizedDesc[INDEX_PRODUCTDESC]);
  665. keypack.ucKeyPackStatus = LSKEYPACKSTATUS_ADD_DESC;
  666. rpcStatus = TLSKeyPackAdd(
  667. tlsHandle,
  668. &keypack,
  669. &dwStatus
  670. );
  671. if(rpcStatus != RPC_S_OK)
  672. {
  673. dwStatus = GetLastError();
  674. goto cleanup;
  675. }
  676. if(dwStatus != ERROR_SUCCESS)
  677. {
  678. goto cleanup;
  679. }
  680. }
  681. }
  682. //
  683. // Activate keypack
  684. //
  685. keypack.ucKeyPackStatus = LSKEYPACKSTATUS_ACTIVE;
  686. keypack.dwActivateDate = (DWORD) time(NULL);
  687. memset(&expired_tm, 0, sizeof(expired_tm));
  688. expired_tm.tm_year = 2036 - 1900; // expire on 2036/1/1
  689. expired_tm.tm_mday = 1;
  690. keypack.dwExpirationDate = mktime(&expired_tm);
  691. rpcStatus = TLSKeyPackSetStatus(
  692. tlsHandle,
  693. LSKEYPACK_SET_KEYPACKSTATUS | LSKEYPACK_SET_ACTIVATEDATE | LSKEYPACK_SET_EXPIREDATE,
  694. &keypack,
  695. &dwStatus
  696. );
  697. if(rpcStatus != RPC_S_OK)
  698. {
  699. dwStatus = GetLastError();
  700. }
  701. cleanup:
  702. return dwStatus;
  703. }
  704. ////////////////////////////////////////////////////////
  705. BOOL
  706. LicenseTypeFromLookupTable(
  707. IN DWORD dwClientVer,
  708. IN DWORD dwTermSrvVer,
  709. OUT PDWORD pdwCALType
  710. )
  711. {
  712. if (dwClientVer < NUMBER_OF_CLIENT_OS && dwTermSrvVer < NUMBER_OF_TERMSRV_OS)
  713. {
  714. *pdwCALType = LicenseTable[dwClientVer][dwTermSrvVer];
  715. return TRUE;
  716. }
  717. else
  718. {
  719. return FALSE;
  720. }
  721. }
  722. ////////////////////////////////////////////////////////
  723. POLICYSTATUS
  724. AdjustNewLicenseRequest(
  725. IN CClient* pClient,
  726. IN PPMLICENSEREQUEST pRequest,
  727. IN OUT PPMLICENSEREQUEST* pAdjustedRequest,
  728. IN UCHAR ucMarkedTemp,
  729. OUT PDWORD pdwErrCode
  730. )
  731. /*++
  732. Abstract:
  733. AdjustNewLicenseRequest fine tune license request for product 236
  734. Parameter:
  735. pClient - Pointer to CClient object.
  736. pRequest - Original request from TermSrv.
  737. pAdjustedRequest - 'Fine tuned' license request.
  738. ucMarkedTemp - Flags on the temporary license passed in (if any)
  739. pdwErrCode - error code
  740. Return:
  741. ERROR_SUCCESS or error code.
  742. ++*/
  743. {
  744. POLICYSTATUS dwStatus = POLICY_SUCCESS;
  745. DWORD dwTermSrvOSId;
  746. DWORD dwTermSrvProductVersion;
  747. LPTSTR pszProductType=NULL;
  748. TCHAR pszProductId[MAX_TERMSRV_PRODUCTID+1];
  749. BOOL bTryInsert=FALSE;
  750. DWORD dwClientOSId;
  751. DWORD dwLicType;
  752. DWORD dwTermSrvIndex = 0;
  753. DWORD dwClientIndex = 0;
  754. //
  755. // Allocate memory for adjusted product ID
  756. //
  757. *pAdjustedRequest = (PPMLICENSEREQUEST) pClient->AllocateMemory(
  758. MEMORY_LICENSE_REQUEST,
  759. sizeof(PMLICENSEREQUEST)
  760. );
  761. if(*pAdjustedRequest == NULL)
  762. {
  763. dwStatus = POLICY_CRITICAL_ERROR;
  764. SetLastError( *pdwErrCode = ERROR_OUTOFMEMORY );
  765. goto cleanup;
  766. }
  767. // Following the OS Version change for .NET, to accomodate for Beta3 and interim builds of TS:
  768. // if a request comes in for 5.1 license, we change the request to 5.2.
  769. if((HIWORD(pRequest->dwProductVersion) == 5) && (LOWORD(pRequest->dwProductVersion) == 1))
  770. {
  771. pRequest->dwProductVersion = MAKELONG(2,5);
  772. }
  773. //
  774. // Fields we don't modify
  775. //
  776. (*pAdjustedRequest)->dwProductVersion = pRequest->dwProductVersion;
  777. (*pAdjustedRequest)->pszCompanyName = pRequest->pszCompanyName;
  778. (*pAdjustedRequest)->dwLanguageId = pRequest->dwLanguageId;
  779. (*pAdjustedRequest)->pszMachineName = pRequest->pszMachineName;
  780. (*pAdjustedRequest)->pszUserName = pRequest->pszUserName;
  781. (*pAdjustedRequest)->dwSupportFlags = pRequest->dwSupportFlags;
  782. //
  783. // request platform ID is the OS ID
  784. //
  785. //
  786. // TermServ exists from NT40 so termsrv OS ID start 2,
  787. // see platform.h
  788. //
  789. dwTermSrvOSId = HIWORD(pRequest->dwProductVersion) - 2;
  790. if((HIWORD(pRequest->dwProductVersion) == 5) && (LOWORD(pRequest->dwProductVersion) == 0))
  791. {
  792. dwTermSrvIndex = TERMSRV_OS_INDEX_WINNT_5_0;
  793. }
  794. else if((HIWORD(pRequest->dwProductVersion) == 5) && (LOWORD(pRequest->dwProductVersion) == 1))
  795. {
  796. dwTermSrvIndex = TERMSRV_OS_INDEX_WINNT_5_1;
  797. }
  798. else if((HIWORD(pRequest->dwProductVersion) == 5) && (LOWORD(pRequest->dwProductVersion) == 2))
  799. {
  800. dwTermSrvIndex = TERMSRV_OS_INDEX_WINNT_5_2;
  801. }
  802. else if((HIWORD(pRequest->dwProductVersion) > 5) || ((HIWORD(pRequest->dwProductVersion) == 5) && (LOWORD(pRequest->dwProductVersion) > 2)))
  803. {
  804. dwTermSrvIndex = TERMSRV_OS_INDEX_WINNT_POST_5_2;
  805. }
  806. else
  807. {
  808. dwStatus = POLICY_NOT_SUPPORTED;
  809. }
  810. dwClientOSId = GetOSId(pRequest->dwPlatformId);
  811. (*pAdjustedRequest)->fTemporary = FALSE;
  812. if(_tcsicmp(pRequest->pszProductId, TERMSERV_PRODUCTID_SKU) == 0)
  813. {
  814. switch(GetOSId(pRequest->dwPlatformId))
  815. {
  816. case CLIENT_OS_ID_WINNT_351:
  817. case CLIENT_OS_ID_WINNT_40:
  818. case CLIENT_OS_ID_OTHER:
  819. dwClientIndex = CLIENT_OS_INDEX_OTHER;
  820. break;
  821. case CLIENT_OS_ID_WINNT_50:
  822. {
  823. if((GetImageRevision(pRequest->dwPlatformId)) == 0)
  824. {
  825. dwClientIndex = CLIENT_OS_INDEX_WINNT_50;
  826. }
  827. else if((GetImageRevision(pRequest->dwPlatformId)) == CLIENT_OS_ID_MINOR_WINNT_51)
  828. {
  829. dwClientIndex = CLIENT_OS_INDEX_WINNT_51;
  830. }
  831. else if((GetImageRevision(pRequest->dwPlatformId)) == CLIENT_OS_ID_MINOR_WINNT_52)
  832. {
  833. dwClientIndex = CLIENT_OS_INDEX_WINNT_52;
  834. }
  835. else
  836. {
  837. dwClientIndex = CLIENT_OS_INDEX_WINNT_POST_52;
  838. }
  839. }
  840. break;
  841. case CLIENT_OS_ID_WINNT_POST_52:
  842. dwClientIndex = CLIENT_OS_INDEX_WINNT_POST_52;
  843. break;
  844. default:
  845. {
  846. dwClientIndex = CLIENT_OS_INDEX_OTHER;
  847. }
  848. break;
  849. }
  850. pszProductType = TERMSERV_FULLVERSION_TYPE;
  851. (*pAdjustedRequest)->dwPlatformId = PLATFORMID_OTHERS;
  852. dwTermSrvProductVersion = pRequest->dwProductVersion;
  853. if(LicenseTypeFromLookupTable(dwClientIndex, dwTermSrvIndex, &dwLicType))
  854. {
  855. if(dwLicType == ALLOW_BUILTIN)
  856. {
  857. pszProductType = TERMSERV_FREE_TYPE;
  858. (*pAdjustedRequest)->dwPlatformId = PLATFORMID_FREE;
  859. //
  860. // Add license pack if necessary
  861. //
  862. if(HIWORD(pRequest->dwProductVersion) != CURRENT_TLSA02_VERSION)
  863. {
  864. AddA02KeyPack( TERMSERV_PRODUCTID_SKU, pRequest->dwProductVersion, TRUE );
  865. }
  866. }
  867. }
  868. if ((*pAdjustedRequest)->dwPlatformId != PLATFORMID_FREE)
  869. {
  870. if (pRequest->dwSupportFlags & SUPPORT_PER_SEAT_POST_LOGON)
  871. {
  872. // We're doing the Per-Seat Post-Logon fix for DoS
  873. if ( !(ucMarkedTemp & MARK_FLAG_USER_AUTHENTICATED))
  874. {
  875. // No previous temporary, or temporary wasn't marked
  876. // as authenticated
  877. (*pAdjustedRequest)->fTemporary = TRUE;
  878. }
  879. }
  880. }
  881. }
  882. else
  883. {
  884. if (_tcsicmp(pRequest->pszProductId, TERMSERV_CONCURRENT_SKU) == 0)
  885. {
  886. pszProductType = TERMSERV_CONCURRENT_TYPE;
  887. }
  888. else if (_tcsicmp(pRequest->pszProductId, TERMSERV_INTERNET_SKU) == 0)
  889. {
  890. pszProductType = TERMSERV_INTERNET_TYPE;
  891. }
  892. else
  893. {
  894. dwStatus = POLICY_NOT_SUPPORTED;
  895. SetLastError( *pdwErrCode = ERROR_INVALID_PARAMETER );
  896. goto cleanup;
  897. }
  898. dwTermSrvProductVersion = pRequest->dwProductVersion;
  899. (*pAdjustedRequest)->dwPlatformId = PLATFORMID_OTHERS;
  900. }
  901. _sntprintf(pszProductId,
  902. MAX_TERMSRV_PRODUCTID,
  903. TERMSERV_PRODUCTID_FORMAT,
  904. pRequest->pszProductId,
  905. HIWORD(dwTermSrvProductVersion),
  906. LOWORD(dwTermSrvProductVersion),
  907. pszProductType);
  908. //
  909. // allocate memory for product Id
  910. //
  911. (*pAdjustedRequest)->pszProductId = (LPTSTR)pClient->AllocateMemory(
  912. MEMORY_STRING,
  913. (_tcslen(pszProductId) + 1) * sizeof(TCHAR)
  914. );
  915. if((*pAdjustedRequest)->pszProductId == NULL)
  916. {
  917. dwStatus = POLICY_CRITICAL_ERROR;
  918. SetLastError( *pdwErrCode = ERROR_OUTOFMEMORY );
  919. goto cleanup;
  920. }
  921. _tcscpy(
  922. (*pAdjustedRequest)->pszProductId,
  923. pszProductId
  924. );
  925. cleanup:
  926. return dwStatus;
  927. }
  928. ////////////////////////////////////////////////////////
  929. POLICYSTATUS
  930. ProcessLicenseRequest(
  931. PMHANDLE client,
  932. PPMLICENSEREQUEST pbRequest,
  933. PPMLICENSEREQUEST* pbAdjustedRequest,
  934. PDWORD pdwErrCode
  935. )
  936. /*++
  937. ++*/
  938. {
  939. CClient* pClient;
  940. POLICYSTATUS dwStatus = POLICY_SUCCESS;
  941. //
  942. // find client's object, client handle manager will
  943. // create a new one.
  944. pClient = g_ClientMgr->FindClient((PMHANDLE)client);
  945. if(pClient == NULL)
  946. {
  947. dwStatus = POLICY_CRITICAL_ERROR;
  948. *pdwErrCode = TLSA02_E_INTERNALERROR;
  949. goto cleanup;
  950. }
  951. //
  952. // AdjustNewLicenseRequest
  953. //
  954. dwStatus = AdjustNewLicenseRequest(
  955. pClient,
  956. pbRequest,
  957. pbAdjustedRequest,
  958. NULL, // no previous license
  959. pdwErrCode
  960. );
  961. cleanup:
  962. return dwStatus;
  963. }
  964. //--------------------------------------------------------------
  965. POLICYSTATUS
  966. ProcessAllocateRequest(
  967. PMHANDLE client,
  968. DWORD dwSuggestType,
  969. PDWORD pdwKeyPackType,
  970. PDWORD pdwErrCode
  971. )
  972. /*++
  973. Default sequence is always FREE/RETAIL/OPEN/SELECT/TEMPORARY
  974. ++*/
  975. {
  976. switch(dwSuggestType)
  977. {
  978. case LSKEYPACKTYPE_UNKNOWN:
  979. *pdwKeyPackType = LSKEYPACKTYPE_FREE;
  980. break;
  981. case LSKEYPACKTYPE_FREE:
  982. *pdwKeyPackType = LSKEYPACKTYPE_RETAIL;
  983. break;
  984. case LSKEYPACKTYPE_RETAIL:
  985. *pdwKeyPackType = LSKEYPACKTYPE_OPEN;
  986. break;
  987. case LSKEYPACKTYPE_OPEN:
  988. *pdwKeyPackType = LSKEYPACKTYPE_SELECT;
  989. break;
  990. case LSKEYPACKTYPE_SELECT:
  991. //
  992. // FALL THRU
  993. //
  994. default:
  995. //
  996. // No more keypack to look for, instruct license
  997. // server to terminate.
  998. //
  999. *pdwKeyPackType = LSKEYPACKTYPE_UNKNOWN;
  1000. break;
  1001. }
  1002. *pdwErrCode = ERROR_SUCCESS;
  1003. return POLICY_SUCCESS;
  1004. }
  1005. //-------------------------------------------------------------
  1006. POLICYSTATUS WINAPI
  1007. ProcessKeyPackDesc(
  1008. IN PMHANDLE client,
  1009. IN PPMKEYPACKDESCREQ pDescReq,
  1010. IN OUT PPMKEYPACKDESC* pDesc,
  1011. IN OUT PDWORD pdwErrCode
  1012. )
  1013. /*++
  1014. ++*/
  1015. {
  1016. CClient* pClient;
  1017. POLICYSTATUS dwStatus=POLICY_SUCCESS;
  1018. DWORD usLangId = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US);
  1019. BOOL bSuccess;
  1020. DWORD dwMajorVersion;
  1021. DWORD dwMinorVersion;
  1022. DWORD dwDescVersion;
  1023. TCHAR szPreFix[MAX_SKU_PREFIX];
  1024. TCHAR szPostFix[MAX_SKU_POSTFIX];
  1025. TCHAR szDesc[MAX_TERMSRV_PRODUCTID+1];
  1026. DWORD i;
  1027. LPTSTR* pszKeyPackDesc;
  1028. LPTSTR* pszUSKeyPackDesc;
  1029. if(pDescReq == NULL || pDesc == NULL)
  1030. {
  1031. dwStatus = POLICY_ERROR;
  1032. *pdwErrCode = ERROR_INVALID_PARAMETER;
  1033. goto cleanup;
  1034. }
  1035. //
  1036. // find client's object, client handle manager will
  1037. // create a new one.
  1038. pClient = g_ClientMgr->FindClient((PMHANDLE)client);
  1039. if(pClient == NULL)
  1040. {
  1041. dwStatus = POLICY_ERROR;
  1042. *pdwErrCode = TLSA02_E_INTERNALERROR;
  1043. goto cleanup;
  1044. }
  1045. for (i = 0; i < g_dwNumSupportedProduct; i++)
  1046. {
  1047. if(_tcsnicmp(
  1048. pDescReq->pszProductId,
  1049. g_pszSupportedProduct[i].szTLSProductCode,
  1050. _tcslen(g_pszSupportedProduct[i].szTLSProductCode)) == 0)
  1051. {
  1052. break;
  1053. }
  1054. }
  1055. if (i >= g_dwNumSupportedProduct)
  1056. {
  1057. //
  1058. // This is not ours
  1059. //
  1060. dwStatus = POLICY_ERROR;
  1061. SetLastError(*pdwErrCode = ERROR_INVALID_PARAMETER);
  1062. goto cleanup;
  1063. }
  1064. LONG lRet = _stscanf(
  1065. pDescReq->pszProductId,
  1066. TERMSERV_PRODUCTID_SCAN_FORMAT,
  1067. szPreFix,
  1068. &dwMajorVersion,
  1069. &dwMinorVersion,
  1070. szPostFix
  1071. );
  1072. if(lRet != 4)
  1073. {
  1074. dwStatus = POLICY_ERROR;
  1075. SetLastError(*pdwErrCode = ERROR_INVALID_PARAMETER);
  1076. goto cleanup;
  1077. }
  1078. if(dwMajorVersion < WINDOWS_VERSION_NT5)
  1079. {
  1080. dwDescVersion = dwMajorVersion;
  1081. }
  1082. else
  1083. {
  1084. dwDescVersion = WINDOWS_VERSION_BASE + dwMajorVersion - WINDOWS_VERSION_NT5;
  1085. }
  1086. //
  1087. // Detemine which resource string we should load,
  1088. // string dependency on resource ID.
  1089. //
  1090. if(_tcsicmp(szPreFix, TERMSERV_PRODUCTID_SKU) == 0)
  1091. {
  1092. if(_tcsicmp(szPostFix, TERMSERV_FULLVERSION_TYPE) == 0)
  1093. {
  1094. if (dwMajorVersion == 5 && (dwMinorVersion == 1 || dwMinorVersion == 2))
  1095. {
  1096. pszKeyPackDesc = g_pszLocalizedStandardKeyPackProductString51;
  1097. pszUSKeyPackDesc = g_pszUSStandardKeyPackProductString51;
  1098. }
  1099. else if (dwMajorVersion == 5 && dwMinorVersion == 0)
  1100. {
  1101. pszKeyPackDesc = g_pszLocalizedStandardKeyPackProductString;
  1102. pszUSKeyPackDesc = g_pszUSStandardKeyPackProductString;
  1103. }
  1104. else
  1105. {
  1106. dwStatus = POLICY_ERROR;
  1107. SetLastError(*pdwErrCode = ERROR_INVALID_PARAMETER);
  1108. goto cleanup;
  1109. }
  1110. if( pDescReq->dwLangId == usLangId ||
  1111. pszKeyPackDesc[INDEX_COMPANYNAME] == NULL ||
  1112. pszKeyPackDesc[INDEX_PRODUCTNAME] == NULL ||
  1113. pszKeyPackDesc[INDEX_PRODUCTDESC] == NULL )
  1114. {
  1115. //
  1116. // resource not found, use english desc.
  1117. pszKeyPackDesc = pszUSKeyPackDesc;
  1118. }
  1119. }
  1120. else if(_tcsicmp(szPostFix, TERMSERV_FREE_TYPE) == 0)
  1121. {
  1122. if (dwMajorVersion == 5 && dwMinorVersion == 0)
  1123. {
  1124. pszKeyPackDesc = g_pszLocalizedFreeKeyPackProductDesc;
  1125. pszUSKeyPackDesc = g_pszUSFreeKeyPackProductDesc;
  1126. }
  1127. else
  1128. {
  1129. dwStatus = POLICY_ERROR;
  1130. SetLastError(*pdwErrCode = ERROR_INVALID_PARAMETER);
  1131. goto cleanup;
  1132. }
  1133. if( pDescReq->dwLangId == usLangId ||
  1134. pszKeyPackDesc[INDEX_COMPANYNAME] == NULL ||
  1135. pszKeyPackDesc[INDEX_PRODUCTNAME] == NULL ||
  1136. pszKeyPackDesc[INDEX_PRODUCTDESC] == NULL )
  1137. {
  1138. //
  1139. // resource not found, use english desc.
  1140. pszKeyPackDesc = pszUSKeyPackDesc;
  1141. }
  1142. }
  1143. else
  1144. {
  1145. //
  1146. // Something wrong, this is not ours
  1147. //
  1148. SetLastError(*pdwErrCode = ERROR_INVALID_PARAMETER);
  1149. dwStatus = POLICY_ERROR;
  1150. goto cleanup;
  1151. }
  1152. }
  1153. else if(_tcsicmp(szPreFix, TERMSERV_INTERNET_SKU) == 0 && _tcsicmp(szPostFix, TERMSERV_INTERNET_TYPE) == 0)
  1154. {
  1155. if (dwMajorVersion == 5 && dwMinorVersion == 0)
  1156. {
  1157. pszKeyPackDesc = g_pszLocalizedInternetKeyPackProductDesc;
  1158. pszUSKeyPackDesc = g_pszUSInternetKeyPackProductDesc;
  1159. }
  1160. else
  1161. {
  1162. dwStatus = POLICY_ERROR;
  1163. SetLastError(*pdwErrCode = ERROR_INVALID_PARAMETER);
  1164. goto cleanup;
  1165. }
  1166. if( pDescReq->dwLangId == usLangId ||
  1167. pszKeyPackDesc[INDEX_COMPANYNAME] == NULL ||
  1168. pszKeyPackDesc[INDEX_PRODUCTNAME] == NULL ||
  1169. pszKeyPackDesc[INDEX_PRODUCTDESC] == NULL )
  1170. {
  1171. //
  1172. // resource not found, use english desc.
  1173. pszKeyPackDesc = pszUSKeyPackDesc;
  1174. }
  1175. }
  1176. else if(_tcsicmp(szPreFix, TERMSERV_CONCURRENT_SKU) == 0 && _tcsicmp(szPostFix, TERMSERV_CONCURRENT_TYPE) == 0)
  1177. {
  1178. if (dwMajorVersion == 5 && dwMinorVersion == 1)
  1179. {
  1180. pszKeyPackDesc = g_pszLocalizedConcurrentKeyPackProductDesc51;
  1181. pszUSKeyPackDesc = g_pszUSConcurrentKeyPackProductDesc51;
  1182. }
  1183. else
  1184. {
  1185. dwStatus = POLICY_ERROR;
  1186. SetLastError(*pdwErrCode = ERROR_INVALID_PARAMETER);
  1187. goto cleanup;
  1188. }
  1189. if( pDescReq->dwLangId == usLangId ||
  1190. pszKeyPackDesc[INDEX_COMPANYNAME] == NULL ||
  1191. pszKeyPackDesc[INDEX_PRODUCTNAME] == NULL ||
  1192. pszKeyPackDesc[INDEX_PRODUCTDESC] == NULL )
  1193. {
  1194. //
  1195. // resource not found, use english desc.
  1196. pszKeyPackDesc = g_pszUSConcurrentKeyPackProductDesc51;
  1197. }
  1198. }
  1199. else
  1200. {
  1201. //
  1202. // Something wrong, this is not ours
  1203. //
  1204. SetLastError(*pdwErrCode = ERROR_INVALID_PARAMETER);
  1205. dwStatus = POLICY_ERROR;
  1206. goto cleanup;
  1207. }
  1208. *pDesc = (PPMKEYPACKDESC)pClient->AllocateMemory(
  1209. MEMORY_KEYPACKDESC,
  1210. sizeof(PMKEYPACKDESC)
  1211. );
  1212. if(*pDesc == NULL)
  1213. {
  1214. SetLastError(*pdwErrCode = ERROR_OUTOFMEMORY);
  1215. dwStatus = POLICY_CRITICAL_ERROR;
  1216. goto cleanup;
  1217. }
  1218. SAFESTRCPY((*pDesc)->szCompanyName, pszKeyPackDesc[INDEX_COMPANYNAME]);
  1219. SAFESTRCPY((*pDesc)->szProductName, pszKeyPackDesc[INDEX_PRODUCTNAME]);
  1220. SAFESTRCPY((*pDesc)->szProductDesc, pszKeyPackDesc[INDEX_PRODUCTDESC]);
  1221. cleanup:
  1222. if(dwStatus != POLICY_SUCCESS)
  1223. {
  1224. *pDesc = NULL;
  1225. }
  1226. return dwStatus;
  1227. }
  1228. //-------------------------------------------------------------
  1229. POLICYSTATUS
  1230. ProcessGenLicenses(
  1231. PMHANDLE client,
  1232. PPMGENERATELICENSE pGenLicense,
  1233. PPMCERTEXTENSION *pCertExtension,
  1234. PDWORD pdwErrCode
  1235. )
  1236. /*++
  1237. ++*/
  1238. {
  1239. // No policy extension to return.
  1240. *pCertExtension = NULL;
  1241. *pdwErrCode = ERROR_SUCCESS;
  1242. return POLICY_SUCCESS;
  1243. }
  1244. //--------------------------------------------------------------
  1245. POLICYSTATUS
  1246. ProcessComplete(
  1247. PMHANDLE client,
  1248. DWORD dwErrCode,
  1249. PDWORD pdwErrCode
  1250. )
  1251. /*++
  1252. ++*/
  1253. {
  1254. //
  1255. // We don't store any data so ignore
  1256. // error code from license server
  1257. //
  1258. UNREFERENCED_PARAMETER(dwErrCode);
  1259. //
  1260. // Free memory allocated for the client
  1261. //
  1262. g_ClientMgr->DestroyClient( client );
  1263. *pdwErrCode = ERROR_SUCCESS;
  1264. return POLICY_SUCCESS;
  1265. }
  1266. //--------------------------------------------------------------
  1267. POLICYSTATUS WINAPI
  1268. PMLicenseRequest(
  1269. PMHANDLE client,
  1270. DWORD dwProgressCode,
  1271. PVOID pbProgressData,
  1272. PVOID* pbNewProgressData,
  1273. PDWORD pdwErrCode
  1274. )
  1275. /*++
  1276. ++*/
  1277. {
  1278. POLICYSTATUS dwStatus = POLICY_SUCCESS;
  1279. switch( dwProgressCode )
  1280. {
  1281. case REQUEST_NEW:
  1282. //
  1283. // License Server ask to fine tune the request.
  1284. //
  1285. dwStatus = ProcessLicenseRequest(
  1286. client,
  1287. (PPMLICENSEREQUEST) pbProgressData,
  1288. (PPMLICENSEREQUEST *) pbNewProgressData,
  1289. pdwErrCode
  1290. );
  1291. break;
  1292. case REQUEST_KEYPACKTYPE:
  1293. //
  1294. // License Server ask for the license pack type
  1295. //
  1296. dwStatus = ProcessAllocateRequest(
  1297. client,
  1298. #ifdef _WIN64
  1299. PtrToUlong(pbProgressData),
  1300. #else
  1301. (DWORD) pbProgressData,
  1302. #endif
  1303. (PDWORD) pbNewProgressData,
  1304. pdwErrCode
  1305. );
  1306. break;
  1307. case REQUEST_TEMPORARY:
  1308. //
  1309. // License Server ask if temporary license should be issued
  1310. //
  1311. *(BOOL *)pbNewProgressData = TRUE;
  1312. *pdwErrCode = ERROR_SUCCESS;
  1313. break;
  1314. case REQUEST_KEYPACKDESC:
  1315. //
  1316. // License Server is requesting a keypack description.
  1317. //
  1318. dwStatus = ProcessKeyPackDesc(
  1319. client,
  1320. (PPMKEYPACKDESCREQ) pbProgressData,
  1321. (PPMKEYPACKDESC *) pbNewProgressData,
  1322. pdwErrCode
  1323. );
  1324. break;
  1325. case REQUEST_GENLICENSE:
  1326. //
  1327. // License Server ask for certificate extension
  1328. //
  1329. dwStatus = ProcessGenLicenses(
  1330. client,
  1331. (PPMGENERATELICENSE) pbProgressData,
  1332. (PPMCERTEXTENSION *) pbNewProgressData,
  1333. pdwErrCode
  1334. );
  1335. break;
  1336. case REQUEST_COMPLETE:
  1337. //
  1338. // Request complete
  1339. //
  1340. dwStatus = ProcessComplete(
  1341. client,
  1342. #ifdef _WIN64
  1343. PtrToUlong(pbNewProgressData),
  1344. #else
  1345. (DWORD) pbNewProgressData,
  1346. #endif
  1347. pdwErrCode
  1348. );
  1349. break;
  1350. default:
  1351. //
  1352. // This tell License Server to use default value
  1353. //
  1354. *pbNewProgressData = NULL;
  1355. dwStatus = POLICY_ERROR;
  1356. *pdwErrCode = ERROR_INVALID_PARAMETER;
  1357. }
  1358. return dwStatus;
  1359. }
  1360. //------------------------------------------------------------
  1361. typedef enum {
  1362. UPGRADELICENSE_ERROR=0,
  1363. UPGRADELICENSE_INVALID_LICENSE,
  1364. UPGRADELICENSE_NEWLICENSE,
  1365. UPGRADELICENSE_UPGRADE,
  1366. UPGRADELICENSE_ALREADYHAVE
  1367. } UPGRADELICENSE_STATUS;
  1368. ////////////////////////////////////////////////////////
  1369. UPGRADELICENSE_STATUS
  1370. RequireUpgradeType(
  1371. PPMUPGRADEREQUEST pUpgrade
  1372. )
  1373. /*++
  1374. ++*/
  1375. {
  1376. UPGRADELICENSE_STATUS dwRetCode = UPGRADELICENSE_UPGRADE;
  1377. DWORD index;
  1378. DWORD dwClientOSId;
  1379. DWORD dwTermSrvOSId;
  1380. DWORD dwClientMinorOSId;
  1381. //
  1382. // Verify input parameters
  1383. //
  1384. if(pUpgrade == NULL || pUpgrade->dwNumProduct == 0 || pUpgrade->pProduct == NULL)
  1385. {
  1386. SetLastError(ERROR_INVALID_PARAMETER);
  1387. dwRetCode = UPGRADELICENSE_ERROR;
  1388. goto cleanup;
  1389. }
  1390. //
  1391. // Make sure we only upgrade to same product
  1392. //
  1393. if(_tcsnicmp(pUpgrade->pUpgradeRequest->pszProductId, TERMSERV_PRODUCTID_SKU, _tcslen(TERMSERV_PRODUCTID_SKU)) != 0)
  1394. {
  1395. dwRetCode = UPGRADELICENSE_ERROR;
  1396. goto cleanup;
  1397. }
  1398. //
  1399. // simple licensed product verification,
  1400. // licensed product is in decending order
  1401. //
  1402. for(index=0; index < pUpgrade->dwNumProduct-1; index++)
  1403. {
  1404. if( pUpgrade->pProduct[index].bTemporary == FALSE &&
  1405. pUpgrade->pProduct[index+1].bTemporary == TRUE )
  1406. {
  1407. dwRetCode = UPGRADELICENSE_INVALID_LICENSE;
  1408. break;
  1409. }
  1410. }
  1411. if(dwRetCode == UPGRADELICENSE_INVALID_LICENSE)
  1412. {
  1413. goto cleanup;
  1414. }
  1415. for(index=0; index < pUpgrade->dwNumProduct; index ++)
  1416. {
  1417. // If Licensed product version is greater than request
  1418. if( (CompareTLSVersions(pUpgrade->pProduct[index].LicensedProduct.dwProductVersion, pUpgrade->pUpgradeRequest->dwProductVersion) > 0))
  1419. {
  1420. dwRetCode = UPGRADELICENSE_NEWLICENSE;
  1421. break;
  1422. }
  1423. //
  1424. // If Licensed product version is older than request
  1425. if(CompareTLSVersions(pUpgrade->pProduct[index].LicensedProduct.dwProductVersion, pUpgrade->pUpgradeRequest->dwProductVersion) < 0)
  1426. {
  1427. break;
  1428. }
  1429. if( (CompareTLSVersions(pUpgrade->pProduct[index].LicensedProduct.dwProductVersion, pUpgrade->pUpgradeRequest->dwProductVersion) == 0) &&
  1430. (pUpgrade->pProduct[index].bTemporary))
  1431. {
  1432. // we want to break out of loop in the case where we have same version as request but is a temporary license
  1433. break;
  1434. }
  1435. if ((CompareTLSVersions(pUpgrade->pProduct[index].LicensedProduct.dwProductVersion,
  1436. pUpgrade->pUpgradeRequest->dwProductVersion) >= 0) &&
  1437. (!(pUpgrade->pProduct[index].bTemporary)))
  1438. {
  1439. DWORD dwVersion = MAKELONG(1,5);
  1440. if(pUpgrade->pProduct[index].LicensedProduct.dwProductVersion == dwVersion)
  1441. {
  1442. dwRetCode = UPGRADELICENSE_UPGRADE;
  1443. }
  1444. else
  1445. {
  1446. // we already have a license.
  1447. dwRetCode = UPGRADELICENSE_ALREADYHAVE;
  1448. }
  1449. break;
  1450. }
  1451. }
  1452. //
  1453. // Win98 client connect to TS 5 to get a Full CAL, then upgrade to NT5, instruct
  1454. // license server to issue a Free CAL.
  1455. //
  1456. dwTermSrvOSId = HIWORD(pUpgrade->pUpgradeRequest->dwProductVersion) - 2;
  1457. dwClientOSId = GetOSId(pUpgrade->pUpgradeRequest->dwPlatformId);
  1458. dwClientMinorOSId = GetImageRevision(pUpgrade->pUpgradeRequest->dwPlatformId);
  1459. if(dwRetCode == UPGRADELICENSE_ALREADYHAVE)
  1460. {
  1461. //
  1462. // do nothing.
  1463. }
  1464. else if(index >= pUpgrade->dwNumProduct || pUpgrade->pProduct[index].bTemporary == TRUE)
  1465. {
  1466. // all license is temp, ask for new license.
  1467. dwRetCode = UPGRADELICENSE_NEWLICENSE;
  1468. }
  1469. else
  1470. {
  1471. // prev. licensed product is perm, ask for upgrade license
  1472. // ClientOSId: HIBYTE(HIWORD) contains Major version and LOBYTE(LOWORD) contains Minor version
  1473. // TermsrvOSId: LOBYTE(HIWORD) contains Major version and LOBYTE(LOWORD) contains Minor version
  1474. if((HIBYTE(HIWORD(dwClientOSId)) == LOBYTE(HIWORD(dwTermSrvOSId)) ? LOBYTE(LOWORD(dwClientMinorOSId)) - LOBYTE(LOWORD(dwTermSrvOSId)) : \
  1475. HIBYTE(HIWORD(dwClientOSId)) - LOBYTE(HIWORD(dwTermSrvOSId))) >= 0)
  1476. {
  1477. dwRetCode = UPGRADELICENSE_NEWLICENSE;
  1478. }
  1479. else
  1480. {
  1481. dwRetCode = UPGRADELICENSE_UPGRADE;
  1482. }
  1483. }
  1484. cleanup:
  1485. return dwRetCode;
  1486. }
  1487. ////////////////////////////////////////////////////////
  1488. POLICYSTATUS
  1489. AdjustUpgradeLicenseRequest(
  1490. IN CClient* pClient,
  1491. IN PPMUPGRADEREQUEST pUpgradeRequest,
  1492. IN PPMLICENSEREQUEST* pAdjustedRequest,
  1493. OUT PDWORD pdwErrCode
  1494. )
  1495. /*++
  1496. ++*/
  1497. {
  1498. POLICYSTATUS dwStatus = POLICY_SUCCESS;
  1499. PPMLICENSEREQUEST pRequest;
  1500. TCHAR pszProductId[MAX_TERMSRV_PRODUCTID+1];
  1501. if(pUpgradeRequest == NULL || pUpgradeRequest->pUpgradeRequest == NULL)
  1502. {
  1503. SetLastError(*pdwErrCode = ERROR_INVALID_PARAMETER);
  1504. dwStatus = POLICY_ERROR;
  1505. goto cleanup;
  1506. }
  1507. *pAdjustedRequest = (PPMLICENSEREQUEST) pClient->AllocateMemory(
  1508. MEMORY_LICENSE_REQUEST,
  1509. sizeof(PMLICENSEREQUEST)
  1510. );
  1511. if(*pAdjustedRequest == NULL)
  1512. {
  1513. SetLastError(*pdwErrCode = ERROR_OUTOFMEMORY);
  1514. dwStatus = POLICY_CRITICAL_ERROR;
  1515. goto cleanup;
  1516. }
  1517. // Following the OS Version change for .NET, to accomodate for Beta3 and interim builds of TS:
  1518. // if a request comes in for 5.1 license, we change the request to 5.2.
  1519. pRequest = pUpgradeRequest->pUpgradeRequest;
  1520. if((HIWORD(pRequest->dwProductVersion) == 5) && (LOWORD(pRequest->dwProductVersion) == 1))
  1521. {
  1522. pRequest->dwProductVersion = MAKELONG(2,5);
  1523. }
  1524. //
  1525. // Fields we don't modify
  1526. //
  1527. (*pAdjustedRequest)->dwProductVersion = pRequest->dwProductVersion;
  1528. (*pAdjustedRequest)->pszCompanyName = pRequest->pszCompanyName;
  1529. (*pAdjustedRequest)->dwLanguageId = pRequest->dwLanguageId;
  1530. (*pAdjustedRequest)->pszMachineName = pRequest->pszMachineName;
  1531. (*pAdjustedRequest)->pszUserName = pRequest->pszUserName;
  1532. (*pAdjustedRequest)->dwSupportFlags = pRequest->dwSupportFlags;
  1533. //
  1534. // Change Request platform ID for upgrade
  1535. //
  1536. (*pAdjustedRequest)->dwPlatformId = PLATFORMID_OTHERS;
  1537. if (pRequest->dwSupportFlags & SUPPORT_PER_SEAT_POST_LOGON)
  1538. {
  1539. // We're doing the Per-Seat Post-Logon fix for DoS
  1540. (*pAdjustedRequest)->fTemporary = TRUE;
  1541. }
  1542. StringCbPrintf(
  1543. pszProductId,
  1544. sizeof(pszProductId),
  1545. TERMSERV_PRODUCTID_FORMAT,
  1546. TERMSERV_PRODUCTID_SKU,
  1547. HIWORD(pRequest->dwProductVersion),
  1548. LOWORD(pRequest->dwProductVersion),
  1549. TERMSERV_FULLVERSION_TYPE
  1550. );
  1551. //
  1552. // allocate memory for product Id
  1553. //
  1554. (*pAdjustedRequest)->pszProductId = (LPTSTR)pClient->AllocateMemory(
  1555. MEMORY_STRING,
  1556. (_tcslen(pszProductId) + 1) * sizeof(TCHAR)
  1557. );
  1558. if((*pAdjustedRequest)->pszProductId == NULL)
  1559. {
  1560. SetLastError( *pdwErrCode = ERROR_OUTOFMEMORY );
  1561. dwStatus = POLICY_CRITICAL_ERROR;
  1562. goto cleanup;
  1563. }
  1564. _tcscpy(
  1565. (*pAdjustedRequest)->pszProductId,
  1566. pszProductId
  1567. );
  1568. cleanup:
  1569. return dwStatus;
  1570. }
  1571. ////////////////////////////////////////////////////////
  1572. POLICYSTATUS
  1573. ProcessUpgradeRequest(
  1574. PMHANDLE hClient,
  1575. PPMUPGRADEREQUEST pUpgrade,
  1576. PPMLICENSEREQUEST* pbAdjustedRequest,
  1577. PDWORD pdwErrCode,
  1578. DWORD dwIndex
  1579. )
  1580. /*++
  1581. ++*/
  1582. {
  1583. POLICYSTATUS dwStatus = POLICY_SUCCESS;
  1584. CClient* pClient;
  1585. UPGRADELICENSE_STATUS upgradeStatus;
  1586. //
  1587. // find client's object, client handle manager will
  1588. // create a new one.
  1589. pClient = g_ClientMgr->FindClient(hClient);
  1590. if(pClient == NULL)
  1591. {
  1592. *pdwErrCode = TLSA02_E_INTERNALERROR;
  1593. dwStatus = POLICY_CRITICAL_ERROR;
  1594. goto cleanup;
  1595. }
  1596. upgradeStatus = RequireUpgradeType(pUpgrade);
  1597. switch(upgradeStatus)
  1598. {
  1599. case UPGRADELICENSE_NEWLICENSE:
  1600. {
  1601. UCHAR ucMarked = pUpgrade->pProduct[dwIndex].ucMarked;
  1602. dwStatus = AdjustNewLicenseRequest(
  1603. pClient,
  1604. pUpgrade->pUpgradeRequest,
  1605. pbAdjustedRequest,
  1606. ucMarked,
  1607. pdwErrCode
  1608. );
  1609. }
  1610. break;
  1611. case UPGRADELICENSE_UPGRADE:
  1612. dwStatus = AdjustUpgradeLicenseRequest(
  1613. pClient,
  1614. pUpgrade,
  1615. pbAdjustedRequest,
  1616. pdwErrCode
  1617. );
  1618. break;
  1619. case UPGRADELICENSE_ALREADYHAVE:
  1620. *pbAdjustedRequest = &(pUpgrade->pProduct->LicensedProduct);
  1621. *pdwErrCode = ERROR_SUCCESS;
  1622. break;
  1623. default:
  1624. SetLastError(*pdwErrCode = TLSA02_E_INVALIDDATA);
  1625. dwStatus = POLICY_ERROR;
  1626. }
  1627. cleanup:
  1628. return dwStatus;
  1629. }
  1630. ////////////////////////////////////////////////////////
  1631. POLICYSTATUS WINAPI
  1632. PMLicenseUpgrade(
  1633. PMHANDLE hClient,
  1634. DWORD dwProgressCode,
  1635. PVOID pbProgressData,
  1636. PVOID *ppbReturnData,
  1637. PDWORD pdwErrCode,
  1638. DWORD dwIndex
  1639. )
  1640. /*++
  1641. ++*/
  1642. {
  1643. POLICYSTATUS dwStatus = POLICY_SUCCESS;
  1644. switch(dwProgressCode)
  1645. {
  1646. case REQUEST_UPGRADE:
  1647. dwStatus = ProcessUpgradeRequest(
  1648. hClient,
  1649. (PPMUPGRADEREQUEST) pbProgressData,
  1650. (PPMLICENSEREQUEST *) ppbReturnData,
  1651. pdwErrCode,
  1652. dwIndex
  1653. );
  1654. break;
  1655. case REQUEST_COMPLETE:
  1656. dwStatus = ProcessComplete(
  1657. hClient,
  1658. #ifdef _WIN64
  1659. PtrToUlong(pbProgressData),
  1660. #else
  1661. (DWORD) (pbProgressData),
  1662. #endif
  1663. pdwErrCode
  1664. );
  1665. break;
  1666. default:
  1667. //
  1668. // use default
  1669. //
  1670. *ppbReturnData = NULL;
  1671. *pdwErrCode = ERROR_SUCCESS;
  1672. }
  1673. return dwStatus;
  1674. }
  1675. ////////////////////////////////////////////////////////
  1676. POLICYSTATUS
  1677. PMReturnLicense(
  1678. IN PMHANDLE hClient,
  1679. IN ULARGE_INTEGER* pLicenseSerialNumber,
  1680. IN PPMLICENSETOBERETURN pLicenseTobeReturn,
  1681. OUT PDWORD pdwLicenseStatus,
  1682. OUT PDWORD pdwErrCode
  1683. )
  1684. /*++
  1685. Abstract
  1686. Return module specific license return policy.
  1687. Parameters
  1688. hClient - Client handle, assign by License Server.
  1689. pLicenseSerialNumber - client license serial number.
  1690. LicensePackId - License Pack where license was allocated from.
  1691. LicensePackLicenseId - License serial number in license pack.
  1692. pdwLicenseStatus - return what license server should
  1693. do with the license
  1694. Returns:
  1695. Function returns ERROR_SUCCESS or any policy module specific
  1696. error code, pdwLicenseStatus returns license return policy
  1697. Currently defined code:
  1698. LICENSE_RETURN_KEEP - keep license, no return to license pack
  1699. LICENSE_RETURN_DELETE - delete license and return to license pack.
  1700. ++*/
  1701. {
  1702. POLICYSTATUS dwStatus = POLICY_SUCCESS;
  1703. *pdwErrCode = ERROR_SUCCESS;
  1704. if ((_tcsicmp(pLicenseTobeReturn->pszOrgProductId,
  1705. TERMSERV_INTERNET_SKU) == 0)
  1706. || (_tcsicmp(pLicenseTobeReturn->pszOrgProductId,
  1707. TERMSERV_CONCURRENT_SKU) == 0))
  1708. {
  1709. *pdwLicenseStatus = LICENSE_RETURN_DELETE;
  1710. }
  1711. else if(_tcsicmp(pLicenseTobeReturn->pszOrgProductId,
  1712. TERMSERV_PRODUCTID_SKU) == 0)
  1713. {
  1714. // Always return license back to license pack
  1715. *pdwLicenseStatus = (pLicenseTobeReturn->bTemp == TRUE) ?
  1716. LICENSE_RETURN_DELETE : LICENSE_RETURN_KEEP;
  1717. }
  1718. else
  1719. {
  1720. *pdwErrCode = ERROR_INVALID_DATA;
  1721. dwStatus = POLICY_ERROR;
  1722. }
  1723. return dwStatus;
  1724. }
  1725. ////////////////////////////////////////////////////////
  1726. POLICYSTATUS WINAPI
  1727. PMInitialize(
  1728. IN DWORD dwVersion,
  1729. IN LPCTSTR pszCompanyName,
  1730. IN LPCTSTR pszProductCode,
  1731. IN OUT PDWORD pdwNumProduct,
  1732. IN OUT PPMSUPPORTEDPRODUCT* ppszProduct,
  1733. OUT PDWORD pdwErrCode
  1734. )
  1735. /*++
  1736. Abstract:
  1737. Initialize internal data use by this policy module. License
  1738. Server calls PMInitialize() after all API is available.
  1739. Parameters:
  1740. dwVersion - License Server version
  1741. pszCompanyName : Name of the company as listed in license server's registry key.
  1742. pszProductCode : Name of the product that license server assume this product supported.
  1743. pdwNumProduct : Pointer to DWORD, on return, ploicy module will set product supported.
  1744. ppszProduct : Pointer array to list of product supported by this policy module.
  1745. Returns:
  1746. ERROR_SUCCESS or error code.
  1747. ++*/
  1748. {
  1749. POLICYSTATUS dwStatus = POLICY_SUCCESS;
  1750. //
  1751. // Initialize internal data here
  1752. //
  1753. if (CURRENT_TLSERVER_VERSION(dwVersion) < CURRENT_TLSA02_VERSION)
  1754. {
  1755. dwStatus = POLICY_CRITICAL_ERROR;
  1756. *pdwErrCode = TLSA02_E_INVALIDDATA;
  1757. goto cleanup;
  1758. }
  1759. g_ClientMgr = new CClientMgr;
  1760. if(g_ClientMgr != NULL)
  1761. {
  1762. g_dwVersion = dwVersion;
  1763. if(pdwNumProduct != NULL && ppszProduct != NULL)
  1764. {
  1765. *pdwNumProduct = g_dwNumSupportedProduct;
  1766. *ppszProduct = g_pszSupportedProduct;
  1767. }
  1768. else
  1769. {
  1770. //
  1771. // Stop processing since this might be license server critical error.
  1772. //
  1773. dwStatus = POLICY_CRITICAL_ERROR;
  1774. *pdwErrCode = TLSA02_E_INVALIDDATA;
  1775. }
  1776. }
  1777. else
  1778. {
  1779. dwStatus = POLICY_CRITICAL_ERROR;
  1780. *pdwErrCode = ERROR_OUTOFMEMORY;
  1781. }
  1782. cleanup:
  1783. return dwStatus;
  1784. }
  1785. ////////////////////////////////////////////////////////
  1786. void WINAPI
  1787. PMTerminate()
  1788. /*++
  1789. Abstract:
  1790. Free all internal data allocated by this policy module. License
  1791. Server calls PMTerminate() before it unload this policy module.
  1792. Parameter:
  1793. None.
  1794. Returns:
  1795. None.
  1796. ++*/
  1797. {
  1798. if(g_ClientMgr)
  1799. {
  1800. //
  1801. // Free internal data here
  1802. //
  1803. delete g_ClientMgr;
  1804. g_ClientMgr = NULL;
  1805. }
  1806. FreeProductDescString();
  1807. return;
  1808. }
  1809. ////////////////////////////////////////////////////////
  1810. POLICYSTATUS WINAPI
  1811. PMInitializeProduct(
  1812. IN LPCTSTR pszCompanyName,
  1813. IN LPCTSTR pszCHProductCode,
  1814. IN LPCTSTR pszTLSProductCode,
  1815. OUT PDWORD pdwErrCode
  1816. )
  1817. /*++
  1818. Abstract:
  1819. Return list of product code that this policy module supported
  1820. Parameters:
  1821. Returns:
  1822. ERROR_SUCCESS or error code.
  1823. Note:
  1824. License Server will not free the memory, policy module will need to
  1825. keep track.
  1826. ++*/
  1827. {
  1828. POLICYSTATUS dwStatus = POLICY_SUCCESS;
  1829. for(DWORD index = 0; index < g_dwNumSupportedProduct; index ++)
  1830. {
  1831. if( _tcsicmp(pszCHProductCode, g_pszSupportedProduct[index].szCHSetupCode) == 0 &&
  1832. _tcsicmp(pszTLSProductCode, g_pszSupportedProduct[index].szTLSProductCode) == 0)
  1833. {
  1834. break;
  1835. }
  1836. }
  1837. if(index >= g_dwNumSupportedProduct)
  1838. {
  1839. *pdwErrCode = ERROR_INVALID_PARAMETER;
  1840. dwStatus = POLICY_ERROR;
  1841. }
  1842. else
  1843. {
  1844. //
  1845. // Ignore error here
  1846. //
  1847. AddA02KeyPack(
  1848. pszTLSProductCode,
  1849. MAKELONG(0, CURRENT_TLSERVER_VERSION(g_dwVersion)),
  1850. FALSE
  1851. );
  1852. }
  1853. *pdwErrCode = ERROR_SUCCESS;
  1854. return dwStatus;
  1855. }
  1856. ////////////////////////////////////////////////////////
  1857. POLICYSTATUS WINAPI
  1858. PMUnloadProduct(
  1859. IN LPCTSTR pszCompanyName,
  1860. IN LPCTSTR pszCHProductCode,
  1861. IN LPCTSTR pszTLSProductCode,
  1862. OUT PDWORD pdwErrCode
  1863. )
  1864. /*++
  1865. Abstract:
  1866. Return list of product code that this policy module supported
  1867. Parameters:
  1868. Returns:
  1869. ERROR_SUCCESS or error code.
  1870. Note:
  1871. License Server will not free the memory, policy module will need to
  1872. keep track.
  1873. ++*/
  1874. {
  1875. *pdwErrCode = ERROR_SUCCESS;
  1876. return POLICY_SUCCESS;
  1877. }
  1878. ////////////////////////////////////////////////////////
  1879. POLICYSTATUS
  1880. ProcessRegisterLicensePack(
  1881. IN PMHANDLE client,
  1882. IN PPMREGISTERLICENSEPACK pmLicensePack,
  1883. IN OUT PPMLSKEYPACK pmLsKeyPack,
  1884. OUT PDWORD pdwErrCode
  1885. )
  1886. /*++
  1887. --*/
  1888. {
  1889. TCHAR* szUuid = NULL;
  1890. BOOL bInternetPackage=FALSE;
  1891. BOOL bConcurrentPackage=FALSE;
  1892. CClient* pClient;
  1893. POLICYSTATUS dwStatus = POLICY_SUCCESS;
  1894. if( pmLicensePack->SourceType != REGISTER_SOURCE_INTERNET &&
  1895. pmLicensePack->SourceType != REGISTER_SOURCE_PHONE )
  1896. {
  1897. dwStatus = POLICY_NOT_SUPPORTED;
  1898. *pdwErrCode = ERROR_INVALID_PARAMETER;
  1899. goto cleanup;
  1900. }
  1901. if( pmLicensePack->SourceType == REGISTER_SOURCE_INTERNET &&
  1902. (pmLicensePack->dwDescriptionCount == 0 || pmLicensePack->pDescription == NULL) )
  1903. {
  1904. dwStatus = POLICY_ERROR;
  1905. *pdwErrCode = ERROR_INVALID_PARAMETER;
  1906. goto cleanup;
  1907. }
  1908. if(CompareFileTime(&pmLicensePack->ActiveDate, &pmLicensePack->ExpireDate) > 0)
  1909. {
  1910. dwStatus = POLICY_ERROR;
  1911. *pdwErrCode = ERROR_INVALID_PARAMETER;
  1912. goto cleanup;
  1913. }
  1914. //
  1915. // find client's object, client handle manager will
  1916. // create a new one.
  1917. pClient = g_ClientMgr->FindClient((PMHANDLE)client);
  1918. if(pClient == NULL)
  1919. {
  1920. dwStatus = POLICY_ERROR;
  1921. *pdwErrCode = TLSA02_E_INTERNALERROR;
  1922. goto cleanup;
  1923. }
  1924. bInternetPackage = (_tcsicmp(pmLicensePack->szProductId, TERMSERV_INTERNET_SKU) == 0);
  1925. if (!bInternetPackage)
  1926. {
  1927. bConcurrentPackage = (_tcsicmp(pmLicensePack->szProductId, TERMSERV_CONCURRENT_SKU) == 0);
  1928. }
  1929. switch(pmLicensePack->dwKeyPackType)
  1930. {
  1931. case LICENSE_KEYPACK_TYPE_SELECT:
  1932. pmLsKeyPack->keypack.ucKeyPackType = LSKEYPACKTYPE_SELECT;
  1933. break;
  1934. case LICENSE_KEYPACK_TYPE_MOLP:
  1935. pmLsKeyPack->keypack.ucKeyPackType = LSKEYPACKTYPE_OPEN;
  1936. break;
  1937. case LICENSE_KEYPACK_TYPE_RETAIL:
  1938. pmLsKeyPack->keypack.ucKeyPackType = LSKEYPACKTYPE_RETAIL;
  1939. break;
  1940. default:
  1941. dwStatus = POLICY_ERROR;
  1942. *pdwErrCode = ERROR_INVALID_PARAMETER;
  1943. goto cleanup;
  1944. }
  1945. // we only use 0xFF
  1946. pmLsKeyPack->keypack.dwPlatformType = PLATFORMID_OTHERS;
  1947. pmLsKeyPack->keypack.ucLicenseType = (UCHAR)pmLicensePack->dwLicenseType;
  1948. pmLsKeyPack->keypack.dwLanguageId = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US); // field ignore by license server.
  1949. pmLsKeyPack->keypack.ucChannelOfPurchase = (pmLicensePack->dwDistChannel == LICENSE_DISTRIBUTION_CHANNEL_OEM) ?
  1950. LSKEYPACKCHANNELOFPURCHASE_OEM :
  1951. LSKEYPACKCHANNELOFPURCHASE_RETAIL;
  1952. pmLsKeyPack->keypack.dwProductFlags = LSKEYPACKPRODUCTFLAG_UNKNOWN;
  1953. pmLsKeyPack->IssueDate = pmLicensePack->IssueDate;
  1954. pmLsKeyPack->ActiveDate = pmLicensePack->ActiveDate;
  1955. pmLsKeyPack->ExpireDate = pmLicensePack->ExpireDate;
  1956. //
  1957. // Tel. registration does not pass us any begin serial number, ignore this field
  1958. //
  1959. _stprintf(
  1960. pmLsKeyPack->keypack.szBeginSerialNumber,
  1961. _TEXT("%ld"),
  1962. 0 // pmLicensePack->dwBeginSerialNum
  1963. );
  1964. pmLsKeyPack->keypack.wMajorVersion = HIWORD(pmLicensePack->dwProductVersion);
  1965. pmLsKeyPack->keypack.wMinorVersion = LOWORD(pmLicensePack->dwProductVersion);
  1966. _tcscpy(
  1967. pmLsKeyPack->keypack.szCompanyName,
  1968. pmLicensePack->szCompanyName
  1969. );
  1970. //
  1971. // KeyPackId, tel. registration does not pass any begin license serial number so to be able
  1972. // to track duplicate, pmLicensePack->KeypackSerialNum.Data1 is the actual license pack
  1973. // serial number, all other field are ignored.
  1974. //
  1975. StringCbPrintf(
  1976. pmLsKeyPack->keypack.szKeyPackId,
  1977. sizeof(pmLsKeyPack->keypack.szKeyPackId),
  1978. TERMSERV_KEYPACKID_FORMAT,
  1979. pmLicensePack->szProductId,
  1980. pmLsKeyPack->keypack.wMajorVersion,
  1981. pmLsKeyPack->keypack.wMinorVersion,
  1982. pmLsKeyPack->keypack.dwPlatformType,
  1983. pmLicensePack->KeypackSerialNum.Data1
  1984. );
  1985. StringCbPrintf(
  1986. pmLsKeyPack->keypack.szProductId,
  1987. sizeof(pmLsKeyPack->keypack.szProductId),
  1988. TERMSERV_PRODUCTID_FORMAT,
  1989. pmLicensePack->szProductId,
  1990. pmLsKeyPack->keypack.wMajorVersion,
  1991. pmLsKeyPack->keypack.wMinorVersion,
  1992. (!bInternetPackage)
  1993. ? ((!bConcurrentPackage) ? TERMSERV_FULLVERSION_TYPE : TERMSERV_CONCURRENT_TYPE)
  1994. : TERMSERV_INTERNET_TYPE
  1995. );
  1996. pmLsKeyPack->keypack.dwTotalLicenseInKeyPack = pmLicensePack->dwQuantity;
  1997. pmLsKeyPack->keypack.dwNumberOfLicenses = pmLicensePack->dwQuantity;
  1998. //
  1999. // Fill in list of product description
  2000. //
  2001. if( pmLicensePack->SourceType == REGISTER_SOURCE_INTERNET )
  2002. {
  2003. pmLsKeyPack->dwDescriptionCount = pmLicensePack->dwDescriptionCount;
  2004. pmLsKeyPack->pDescription = pmLicensePack->pDescription;
  2005. }
  2006. else
  2007. {
  2008. LPTSTR *pszDescs;
  2009. LPTSTR *pszLocalizedDescs;
  2010. //
  2011. // Verify version first...
  2012. //
  2013. if (pmLsKeyPack->keypack.wMajorVersion == 5 &&
  2014. (pmLsKeyPack->keypack.wMinorVersion == 1 || pmLsKeyPack->keypack.wMinorVersion == 2))
  2015. {
  2016. if (bConcurrentPackage)
  2017. {
  2018. pszDescs = g_pszUSConcurrentKeyPackProductDesc51;
  2019. pszLocalizedDescs = g_pszLocalizedConcurrentKeyPackProductDesc51;
  2020. }
  2021. else if (bInternetPackage)
  2022. {
  2023. *pdwErrCode = ERROR_INVALID_PARAMETER;
  2024. dwStatus = POLICY_NOT_SUPPORTED;
  2025. goto cleanup;
  2026. }
  2027. else
  2028. {
  2029. pszDescs = g_pszUSStandardKeyPackProductString51;
  2030. pszLocalizedDescs = g_pszLocalizedStandardKeyPackProductString51;
  2031. }
  2032. }
  2033. else if (pmLsKeyPack->keypack.wMajorVersion == 5 &&
  2034. pmLsKeyPack->keypack.wMinorVersion == 0)
  2035. {
  2036. if (bInternetPackage)
  2037. {
  2038. pszDescs = g_pszUSInternetKeyPackProductDesc;
  2039. pszLocalizedDescs = g_pszLocalizedInternetKeyPackProductDesc;
  2040. }
  2041. else if (bConcurrentPackage)
  2042. {
  2043. *pdwErrCode = ERROR_INVALID_PARAMETER;
  2044. dwStatus = POLICY_NOT_SUPPORTED;
  2045. goto cleanup;
  2046. }
  2047. else
  2048. {
  2049. pszDescs = g_pszUSStandardKeyPackProductString;
  2050. pszLocalizedDescs = g_pszLocalizedStandardKeyPackProductString;
  2051. }
  2052. }
  2053. else
  2054. {
  2055. *pdwErrCode = ERROR_INVALID_PARAMETER;
  2056. dwStatus = POLICY_NOT_SUPPORTED;
  2057. goto cleanup;
  2058. }
  2059. DWORD dwMakeLangID = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US);
  2060. DWORD dwGetLangID = GetSystemDefaultLangID();
  2061. // one for english and one for localized version
  2062. pmLsKeyPack->dwDescriptionCount = (dwGetLangID != dwMakeLangID)? 2 : 1;
  2063. pmLsKeyPack->pDescription =
  2064. (PPMREGISTERLKPDESC) pClient->AllocateMemory(
  2065. MEMORY_LICENSEREGISTRATION,
  2066. sizeof(PMREGISTERLKPDESC) * pmLsKeyPack->dwDescriptionCount
  2067. );
  2068. if(pmLsKeyPack->pDescription == NULL)
  2069. {
  2070. *pdwErrCode = ERROR_OUTOFMEMORY;
  2071. dwStatus = POLICY_CRITICAL_ERROR;
  2072. goto cleanup;
  2073. }
  2074. pmLsKeyPack->pDescription[0].Locale = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US);
  2075. if(pmLsKeyPack->dwDescriptionCount == 2)
  2076. {
  2077. pmLsKeyPack->pDescription[1].Locale = dwGetLangID;
  2078. }
  2079. if (bInternetPackage && (pmLsKeyPack->keypack.wMajorVersion == 5 &&
  2080. pmLsKeyPack->keypack.wMinorVersion == 0))
  2081. {
  2082. _tcscpy(
  2083. pmLsKeyPack->pDescription[0].szProductName,
  2084. g_pszUSInternetKeyPackProductDesc[INDEX_PRODUCTNAME]
  2085. );
  2086. _tcscpy(
  2087. pmLsKeyPack->pDescription[0].szProductDesc,
  2088. pszDescs[INDEX_PRODUCTDESC]
  2089. );
  2090. if(pmLsKeyPack->dwDescriptionCount == 2)
  2091. {
  2092. _tcscpy(
  2093. pmLsKeyPack->pDescription[1].szProductName,
  2094. g_pszLocalizedInternetKeyPackProductDesc[INDEX_PRODUCTNAME]
  2095. );
  2096. _tcscpy(
  2097. pmLsKeyPack->pDescription[1].szProductDesc,
  2098. pszLocalizedDescs[INDEX_PRODUCTDESC]
  2099. );
  2100. }
  2101. }
  2102. else if (bConcurrentPackage && (pmLsKeyPack->keypack.wMajorVersion == 5 &&
  2103. pmLsKeyPack->keypack.wMinorVersion == 1))
  2104. {
  2105. _tcscpy(
  2106. pmLsKeyPack->pDescription[0].szProductName,
  2107. g_pszUSConcurrentKeyPackProductDesc51[INDEX_PRODUCTNAME]
  2108. );
  2109. _tcscpy(
  2110. pmLsKeyPack->pDescription[0].szProductDesc,
  2111. pszDescs[INDEX_PRODUCTDESC]
  2112. );
  2113. if(pmLsKeyPack->dwDescriptionCount == 2)
  2114. {
  2115. _tcscpy(
  2116. pmLsKeyPack->pDescription[1].szProductName,
  2117. g_pszLocalizedConcurrentKeyPackProductDesc51[INDEX_PRODUCTNAME]
  2118. );
  2119. _tcscpy(
  2120. pmLsKeyPack->pDescription[1].szProductDesc,
  2121. pszLocalizedDescs[INDEX_PRODUCTDESC]
  2122. );
  2123. }
  2124. }
  2125. else
  2126. {
  2127. if(bInternetPackage || bConcurrentPackage)
  2128. {
  2129. *pdwErrCode = ERROR_INVALID_PARAMETER;
  2130. dwStatus = POLICY_NOT_SUPPORTED;
  2131. goto cleanup;
  2132. }
  2133. if (pmLsKeyPack->keypack.wMajorVersion == 5 &&
  2134. pmLsKeyPack->keypack.wMinorVersion == 0)
  2135. {
  2136. _tcscpy(
  2137. pmLsKeyPack->pDescription[0].szProductName,
  2138. g_pszUSStandardKeyPackProductString[INDEX_PRODUCTNAME]
  2139. );
  2140. _tcscpy(
  2141. pmLsKeyPack->pDescription[0].szProductDesc,
  2142. pszDescs[INDEX_PRODUCTDESC]
  2143. );
  2144. if(pmLsKeyPack->dwDescriptionCount == 2)
  2145. {
  2146. _tcscpy(
  2147. pmLsKeyPack->pDescription[1].szProductName,
  2148. g_pszLocalizedStandardKeyPackProductString[INDEX_PRODUCTNAME]
  2149. );
  2150. _tcscpy(
  2151. pmLsKeyPack->pDescription[1].szProductDesc,
  2152. pszLocalizedDescs[INDEX_PRODUCTDESC]
  2153. );
  2154. }
  2155. }
  2156. else if(pmLsKeyPack->keypack.wMajorVersion == 5 &&
  2157. (pmLsKeyPack->keypack.wMinorVersion == 1 || pmLsKeyPack->keypack.wMinorVersion == 2) )
  2158. {
  2159. _tcscpy(
  2160. pmLsKeyPack->pDescription[0].szProductName,
  2161. g_pszUSStandardKeyPackProductString51[INDEX_PRODUCTNAME]
  2162. );
  2163. _tcscpy(
  2164. pmLsKeyPack->pDescription[0].szProductDesc,
  2165. pszDescs[INDEX_PRODUCTDESC]
  2166. );
  2167. if(pmLsKeyPack->dwDescriptionCount == 2)
  2168. {
  2169. _tcscpy(
  2170. pmLsKeyPack->pDescription[1].szProductName,
  2171. g_pszLocalizedStandardKeyPackProductString51[INDEX_PRODUCTNAME]
  2172. );
  2173. _tcscpy(
  2174. pmLsKeyPack->pDescription[1].szProductDesc,
  2175. pszLocalizedDescs[INDEX_PRODUCTDESC]
  2176. );
  2177. }
  2178. }
  2179. }
  2180. }
  2181. cleanup:
  2182. return dwStatus;
  2183. }
  2184. ////////////////////////////////////////////////////////
  2185. POLICYSTATUS
  2186. CompleteRegisterLicensePack(
  2187. IN PMHANDLE client,
  2188. IN DWORD dwErrCode,
  2189. OUT PDWORD pdwErrCode
  2190. )
  2191. /*++
  2192. --*/
  2193. {
  2194. UNREFERENCED_PARAMETER(dwErrCode);
  2195. //
  2196. // Free memory allocated for the client
  2197. //
  2198. g_ClientMgr->DestroyClient( client );
  2199. *pdwErrCode = ERROR_SUCCESS;
  2200. return POLICY_SUCCESS;
  2201. }
  2202. ////////////////////////////////////////////////////////
  2203. POLICYSTATUS WINAPI
  2204. PMRegisterLicensePack(
  2205. PMHANDLE client,
  2206. DWORD dwProgressCode,
  2207. PVOID pbProgressData,
  2208. PVOID pbReturnData,
  2209. PDWORD pdwErrCode
  2210. )
  2211. /*++
  2212. --*/
  2213. {
  2214. POLICYSTATUS dwStatus = POLICY_SUCCESS;
  2215. switch(dwProgressCode)
  2216. {
  2217. case REGISTER_PROGRESS_NEW:
  2218. dwStatus = ProcessRegisterLicensePack(
  2219. client,
  2220. (PPMREGISTERLICENSEPACK) pbProgressData,
  2221. (PPMLSKEYPACK)pbReturnData,
  2222. pdwErrCode
  2223. );
  2224. break;
  2225. case REGISTER_PROGRESS_END:
  2226. dwStatus = CompleteRegisterLicensePack(
  2227. client,
  2228. #ifdef _WIN64
  2229. PtrToUlong(pbProgressData),
  2230. #else
  2231. (DWORD) pbProgressData,
  2232. #endif
  2233. pdwErrCode
  2234. );
  2235. break;
  2236. default:
  2237. *pdwErrCode = ERROR_INVALID_PARAMETER;
  2238. dwStatus = POLICY_ERROR;
  2239. }
  2240. return dwStatus;
  2241. }