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.

2164 lines
53 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Copyright (c) 1997-1999 Microsoft Corporation
  4. //
  5. // File: policy.cpp
  6. //
  7. // Contents: Loading product policy module
  8. //
  9. // History:
  10. //
  11. //---------------------------------------------------------------------------
  12. #include "pch.cpp"
  13. #include "utils.h"
  14. #include <windows.h>
  15. #include <wincrypt.h>
  16. #include <assert.h>
  17. #include "srvdef.h"
  18. #include "server.h"
  19. #include "policy.h"
  20. CTLSPolicyMgr PolicyMgr;
  21. TCHAR g_szDefPolCompanyName[LSERVER_MAX_STRING_SIZE+1];
  22. TCHAR g_szDefProductId[LSERVER_MAX_STRING_SIZE+1];
  23. //-------------------------------------------------------------
  24. //
  25. // Internal routine
  26. //
  27. HINSTANCE
  28. LoadPolicyModule(
  29. IN LPCTSTR pszDllName,
  30. OUT PDWORD pdwBufferSize,
  31. OUT LPTSTR pszBuffer
  32. )
  33. /*++
  34. Abstract:
  35. Load Policy module
  36. Parameters:
  37. pszDll : Name of the DLL.
  38. pdwBufferSize :
  39. pszBuffer
  40. Returns:
  41. --*/
  42. {
  43. TCHAR szDllFullPath[MAX_PATH+1];
  44. DWORD dwErrCode = ERROR_SUCCESS;
  45. HINSTANCE hPolicyModule = NULL;
  46. //
  47. // expand the environment string
  48. //
  49. memset(szDllFullPath, 0, sizeof(szDllFullPath));
  50. dwErrCode = ExpandEnvironmentStrings(
  51. pszDllName,
  52. szDllFullPath,
  53. sizeof(szDllFullPath)/sizeof(szDllFullPath[0])
  54. );
  55. if(dwErrCode == 0 && pszBuffer && pdwBufferSize && *pdwBufferSize)
  56. {
  57. _tcsncpy(pszBuffer, szDllFullPath, *pdwBufferSize);
  58. *pdwBufferSize = _tcslen(szDllFullPath);
  59. }
  60. dwErrCode = ERROR_SUCCESS;
  61. hPolicyModule = LoadLibrary(szDllFullPath);
  62. if(hPolicyModule == NULL)
  63. {
  64. dwErrCode = GetLastError();
  65. TLSLogEvent(
  66. EVENTLOG_ERROR_TYPE,
  67. TLS_E_LOADPOLICY,
  68. TLS_E_LOADPOLICYMODULE,
  69. pszDllName,
  70. dwErrCode
  71. );
  72. }
  73. return hPolicyModule;
  74. }
  75. //-------------------------------------------------------------
  76. typedef struct _RegEnumHandle {
  77. DWORD dwKeyIndex;
  78. HKEY hKey;
  79. } RegEnumHandle;
  80. //-------------------------------------------------------------
  81. DWORD
  82. RegEnumBegin(
  83. IN HKEY hRoot,
  84. IN LPCTSTR pszSubKey,
  85. OUT RegEnumHandle* phEnum
  86. )
  87. /*++
  88. ++*/
  89. {
  90. DWORD dwStatus;
  91. dwStatus = RegOpenKeyEx(
  92. hRoot,
  93. pszSubKey,
  94. 0,
  95. KEY_ALL_ACCESS,
  96. &(phEnum->hKey)
  97. );
  98. phEnum->dwKeyIndex = 0;
  99. return dwStatus;
  100. }
  101. //-------------------------------------------------------------
  102. DWORD
  103. RegEnumNext(
  104. RegEnumHandle* phEnum,
  105. LPTSTR lpName,
  106. LPDWORD lpcbName
  107. )
  108. /*++
  109. ++*/
  110. {
  111. DWORD dwStatus;
  112. FILETIME ftLastWriteTiem;
  113. dwStatus = RegEnumKeyEx(
  114. phEnum->hKey,
  115. phEnum->dwKeyIndex,
  116. lpName,
  117. lpcbName,
  118. 0,
  119. NULL,
  120. NULL,
  121. &ftLastWriteTiem
  122. );
  123. (phEnum->dwKeyIndex)++;
  124. return dwStatus;
  125. }
  126. //-------------------------------------------------------------
  127. DWORD
  128. RegEnumEnd(
  129. RegEnumHandle* phEnum
  130. )
  131. /*++
  132. ++*/
  133. {
  134. if(phEnum->hKey != NULL)
  135. RegCloseKey(phEnum->hKey);
  136. phEnum->dwKeyIndex = 0;
  137. return ERROR_SUCCESS;
  138. }
  139. //-------------------------------------------------------------
  140. DWORD
  141. ServiceInitPolicyModule(
  142. void
  143. )
  144. /*++
  145. ++*/
  146. {
  147. return PolicyMgr.InitProductPolicyModule();
  148. }
  149. //------------------------------------------------------------
  150. DWORD
  151. ServiceLoadPolicyModule(
  152. IN HKEY hKey,
  153. IN LPCTSTR pszCompanyName,
  154. IN LPCTSTR pszProductId,
  155. IN LPCTSTR pszDllRegValue,
  156. IN LPCTSTR pszDllFlagValue
  157. )
  158. /*++
  159. ++*/
  160. {
  161. DWORD dwStatus;
  162. DWORD dwSize;
  163. TCHAR szDllName[MAX_PATH+1];
  164. DWORD dwDllFlag;
  165. dwSize = sizeof(dwDllFlag);
  166. dwStatus = RegQueryValueEx(
  167. hKey,
  168. pszDllFlagValue,
  169. NULL,
  170. NULL,
  171. (PBYTE)&dwDllFlag,
  172. &dwSize
  173. );
  174. if(dwStatus != ERROR_SUCCESS)
  175. dwDllFlag = POLICY_DENY_ALL_REQUEST; // (pszProductId == NULL) ? POLICY_DENY_ALL_REQUEST : POLICY_USE_DEFAULT;
  176. dwSize = MAX_PATH * sizeof(TCHAR);
  177. dwStatus = RegQueryValueEx(
  178. hKey,
  179. pszDllRegValue,
  180. NULL,
  181. NULL,
  182. (PBYTE)szDllName,
  183. &dwSize
  184. );
  185. if(dwStatus == ERROR_SUCCESS)
  186. {
  187. dwStatus = PolicyMgr.AddPolicyModule(
  188. FALSE,
  189. pszCompanyName,
  190. pszProductId,
  191. szDllName,
  192. dwDllFlag
  193. );
  194. if(dwStatus != ERROR_SUCCESS)
  195. {
  196. LPCTSTR pString[1];
  197. pString[0] = szDllName;
  198. //
  199. // log event - use default or deny all request.
  200. //
  201. TLSLogEventString(
  202. EVENTLOG_WARNING_TYPE,
  203. (dwDllFlag == POLICY_DENY_ALL_REQUEST) ? TLS_W_LOADPOLICYMODULEDENYALLREQUEST : TLS_W_LOADPOLICYMODULEUSEDEFAULT,
  204. 1,
  205. pString
  206. );
  207. }
  208. }
  209. else if(pszProductId != NULL)
  210. {
  211. //
  212. // Load error indicate missing registry value
  213. //
  214. TLSLogEvent(
  215. EVENTLOG_ERROR_TYPE,
  216. TLS_E_LOADPOLICY,
  217. TLS_E_NOPOLICYMODULE,
  218. pszProductId,
  219. pszCompanyName
  220. );
  221. }
  222. return dwStatus;
  223. }
  224. //-------------------------------------------------------------
  225. DWORD
  226. ServiceLoadAllPolicyModule(
  227. IN HKEY hRoot,
  228. IN LPCTSTR pszSubkey
  229. )
  230. /*++
  231. ++*/
  232. {
  233. DWORD dwStatus;
  234. RegEnumHandle hCompany;
  235. RegEnumHandle hProductId;
  236. PolicyModule PolModule;
  237. DWORD dwSize;
  238. _set_se_translator( trans_se_func );
  239. //
  240. // Open registry key
  241. // Software\microsoft\termsrvlicensing\policy
  242. //
  243. dwStatus = RegEnumBegin(
  244. hRoot,
  245. pszSubkey,
  246. &hCompany
  247. );
  248. while(dwStatus == ERROR_SUCCESS)
  249. {
  250. //
  251. // Enumerater all key (company name) under
  252. // Software\microsoft\termsrvlicensing\policy
  253. //
  254. dwSize = sizeof(PolModule.m_szCompanyName)/sizeof(PolModule.m_szCompanyName[0]);
  255. dwStatus = RegEnumNext(
  256. &hCompany,
  257. PolModule.m_szCompanyName,
  258. &dwSize
  259. );
  260. if(dwStatus != ERROR_SUCCESS)
  261. break;
  262. //
  263. // ignore error here
  264. //
  265. //
  266. // Enumerate all product under company
  267. //
  268. dwStatus = RegEnumBegin(
  269. hCompany.hKey,
  270. PolModule.m_szCompanyName,
  271. &hProductId
  272. );
  273. if(dwStatus == ERROR_SUCCESS)
  274. {
  275. //
  276. // Load company wide policy module
  277. //
  278. ServiceLoadPolicyModule(
  279. hProductId.hKey,
  280. PolModule.m_szCompanyName,
  281. NULL,
  282. LSERVER_POLICY_DLLPATH,
  283. LSERVER_POLICY_DLLFLAG
  284. );
  285. }
  286. while(dwStatus == ERROR_SUCCESS)
  287. {
  288. dwSize = sizeof(PolModule.m_szProductId)/sizeof(PolModule.m_szProductId[0]);
  289. dwStatus = RegEnumNext(
  290. &hProductId,
  291. PolModule.m_szProductId,
  292. &dwSize
  293. );
  294. if(dwStatus == ERROR_SUCCESS)
  295. {
  296. HKEY hKey;
  297. dwStatus = RegOpenKeyEx(
  298. hProductId.hKey,
  299. PolModule.m_szProductId,
  300. 0,
  301. KEY_ALL_ACCESS,
  302. &hKey
  303. );
  304. if(dwStatus != ERROR_SUCCESS)
  305. continue;
  306. //
  307. // Open product registry key
  308. //
  309. ServiceLoadPolicyModule(
  310. hKey,
  311. PolModule.m_szCompanyName,
  312. PolModule.m_szProductId,
  313. LSERVER_POLICY_DLLPATH,
  314. LSERVER_POLICY_DLLFLAG
  315. );
  316. //
  317. // ignore any error code here
  318. //
  319. RegCloseKey(hKey);
  320. }
  321. }
  322. dwStatus = RegEnumEnd(&hProductId);
  323. }
  324. dwStatus = RegEnumEnd(&hCompany);
  325. return dwStatus;
  326. }
  327. //-------------------------------------------------------
  328. void
  329. ReleasePolicyModule(
  330. CTLSPolicy* ptr
  331. )
  332. /*++
  333. ++*/
  334. {
  335. PolicyMgr.ReleaseProductPolicyModule(ptr);
  336. }
  337. //-------------------------------------------------------
  338. BOOL
  339. TranslateCHCodeToTlsCode(
  340. IN LPCTSTR pszCompanyName,
  341. IN LPCTSTR pszCHProductId,
  342. IN LPTSTR pszTlsProductId,
  343. IN OUT PDWORD pdwBufferSize
  344. )
  345. /*++
  346. --*/
  347. {
  348. return PolicyMgr.TranslateCHCodeToTlsCode(
  349. pszCompanyName,
  350. pszCHProductId,
  351. pszTlsProductId,
  352. pdwBufferSize
  353. );
  354. }
  355. //-------------------------------------------------------
  356. CTLSPolicy*
  357. AcquirePolicyModule(
  358. IN LPCTSTR pszCompanyName,
  359. IN LPCTSTR pszProductId,
  360. IN BOOL bUseProductPolicy
  361. )
  362. /*++
  363. Abstract:
  364. Acquire a policy module base on company name and product code.
  365. Parameter:
  366. pszCompanyName : Company Name.
  367. pszProductId : Product Code.
  368. bUseProductPolicy : TRUE if only exact product policy module, FALSE uses
  369. default policy module if can't find a policy module for
  370. product.
  371. Return:
  372. Pointer to CTLSPolicy or NULL if not found.
  373. Remark:
  374. Default behavior.
  375. ++*/
  376. {
  377. CTLSPolicy* ptr;
  378. ptr = PolicyMgr.AcquireProductPolicyModule(
  379. pszCompanyName,
  380. pszProductId
  381. );
  382. if(ptr == NULL && bUseProductPolicy == FALSE)
  383. {
  384. ptr = PolicyMgr.AcquireProductPolicyModule(
  385. pszCompanyName,
  386. NULL
  387. );
  388. }
  389. if(ptr == NULL && bUseProductPolicy == FALSE)
  390. {
  391. ptr = PolicyMgr.AcquireProductPolicyModule(
  392. g_szDefPolCompanyName,
  393. g_szDefProductId
  394. );
  395. }
  396. if(ptr == NULL)
  397. {
  398. TLSLogEvent(
  399. EVENTLOG_WARNING_TYPE,
  400. TLS_E_LOADPOLICY,
  401. TLS_E_NOPOLICYMODULE,
  402. pszCompanyName,
  403. pszProductId
  404. );
  405. SetLastError(TLS_E_NOPOLICYMODULE);
  406. }
  407. return ptr;
  408. }
  409. /////////////////////////////////////////////////////////
  410. //
  411. // Class CTLSPolicyMgr
  412. //
  413. /////////////////////////////////////////////////////////
  414. CTLSPolicyMgr::CTLSPolicyMgr()
  415. /*++
  416. ++*/
  417. {
  418. CTLSPolicy* ptr;
  419. PolicyModule pm;
  420. //
  421. // Load default name for default policy module
  422. //
  423. LoadResourceString(
  424. IDS_DEFAULT_POLICY,
  425. g_szDefPolCompanyName,
  426. sizeof(g_szDefPolCompanyName) / sizeof(g_szDefPolCompanyName[0]) - 1
  427. );
  428. LoadResourceString(
  429. IDS_DEFAULT_POLICY,
  430. g_szDefProductId,
  431. sizeof(g_szDefProductId) / sizeof(g_szDefProductId[0]) - 1
  432. );
  433. lstrcpy(pm.m_szCompanyName, g_szDefPolCompanyName);
  434. lstrcpy(pm.m_szProductId, g_szDefProductId);
  435. //
  436. // Create a default policy module to handle all cases...
  437. //
  438. ptr = new CTLSPolicy;
  439. ptr->CreatePolicy(
  440. (HMODULE) INVALID_HANDLE_VALUE,
  441. g_szDefPolCompanyName,
  442. g_szDefProductId,
  443. PMReturnLicense,
  444. PMLicenseUpgrade,
  445. PMLicenseRequest,
  446. PMUnloadProduct,
  447. PMInitializeProduct,
  448. PMRegisterLicensePack
  449. );
  450. //m_ProductPolicyModuleRWLock.Acquire(WRITER_LOCK);
  451. m_ProductPolicyModule[pm] = ptr;
  452. //m_ProductPolicyModuleRWLock.Release(WRITER_LOCK);
  453. //m_Handles.insert(
  454. // pair<PolicyModule, CTLSPolicy*>(pm, ptr)
  455. // );
  456. }
  457. //-------------------------------------------------------
  458. CTLSPolicyMgr::~CTLSPolicyMgr()
  459. /*++
  460. ++*/
  461. {
  462. m_ProductPolicyModuleRWLock.Acquire(WRITER_LOCK);
  463. for( PMProductPolicyMapType::iterator it = m_ProductPolicyModule.begin();
  464. it != m_ProductPolicyModule.end();
  465. it++ )
  466. {
  467. CTLSPolicy* ptr = (CTLSPolicy*) (*it).second;
  468. delete ptr;
  469. }
  470. m_ProductPolicyModule.erase(m_ProductPolicyModule.begin(), m_ProductPolicyModule.end());
  471. m_ProductPolicyModuleRWLock.Release(WRITER_LOCK);
  472. m_LoadedPolicyRWLock.Acquire(WRITER_LOCK);
  473. for(PMLoadedModuleMapType::iterator loadedit = m_LoadedPolicy.begin();
  474. loadedit != m_LoadedPolicy.end();
  475. loadedit++ )
  476. {
  477. HMODULE hModule = (HMODULE) (*loadedit).second;
  478. if(hModule != NULL)
  479. {
  480. UnloadPolicyModule(hModule);
  481. FreeLibrary(hModule);
  482. }
  483. }
  484. m_LoadedPolicy.erase(m_LoadedPolicy.begin(), m_LoadedPolicy.end());
  485. m_LoadedPolicyRWLock.Release(WRITER_LOCK);
  486. m_ProductTranslationRWLock.Acquire(WRITER_LOCK);
  487. m_ProductTranslation.erase(m_ProductTranslation.begin(), m_ProductTranslation.end());
  488. m_ProductTranslationRWLock.Release(WRITER_LOCK);
  489. }
  490. //-------------------------------------------------------
  491. HMODULE
  492. CTLSPolicyMgr::LoadPolicyModule(
  493. LPCTSTR pszCompanyName,
  494. LPCTSTR pszProductCode,
  495. LPCTSTR pszDllName
  496. )
  497. /*++
  498. --*/
  499. {
  500. HMODULE hModule;
  501. PMLoadedModuleMapType::iterator it;
  502. PolicyModule pm;
  503. memset(&pm, 0, sizeof(pm));
  504. if(pszCompanyName)
  505. {
  506. _tcscpy(pm.m_szCompanyName, pszCompanyName);
  507. }
  508. if(pszProductCode)
  509. {
  510. _tcscpy(pm.m_szProductId, pszProductCode);
  511. }
  512. m_LoadedPolicyRWLock.Acquire(WRITER_LOCK);
  513. it = m_LoadedPolicy.find( pm );
  514. if(it != m_LoadedPolicy.end())
  515. {
  516. hModule = (HMODULE) (*it).second;
  517. }
  518. else
  519. {
  520. hModule = ::LoadPolicyModule(
  521. pszDllName,
  522. NULL,
  523. NULL
  524. );
  525. if(hModule != NULL)
  526. {
  527. m_LoadedPolicy[pm] = hModule;
  528. }
  529. }
  530. m_LoadedPolicyRWLock.Release(WRITER_LOCK);
  531. return hModule;
  532. }
  533. //-------------------------------------------------------
  534. DWORD
  535. CTLSPolicyMgr::UnloadPolicyModule(
  536. HMODULE hModule
  537. )
  538. /*++
  539. --*/
  540. {
  541. DWORD dwStatus = ERROR_SUCCESS;
  542. TLSPMTerminate pfnTerminate;
  543. if(hModule != NULL)
  544. {
  545. pfnTerminate = (TLSPMTerminate) GetProcAddress(
  546. hModule,
  547. TEMINATEPROCNAME
  548. );
  549. if(pfnTerminate != NULL)
  550. {
  551. pfnTerminate();
  552. }
  553. else
  554. {
  555. dwStatus = GetLastError();
  556. }
  557. }
  558. else
  559. {
  560. dwStatus = ERROR_INVALID_PARAMETER;
  561. }
  562. return dwStatus;
  563. }
  564. //-------------------------------------------------------
  565. DWORD
  566. CTLSPolicyMgr::UnloadPolicyModule(
  567. LPCTSTR pszCompanyName,
  568. LPCTSTR pszProductCode
  569. )
  570. /*++
  571. Not supported yet, need to remove all product policy in m_ProductPolicyModule()
  572. then unload DLL
  573. --*/
  574. {
  575. return ERROR_SUCCESS;
  576. }
  577. //-------------------------------------------------------
  578. DWORD
  579. CTLSPolicyMgr::InitProductPolicyModule()
  580. /*++
  581. ++*/
  582. {
  583. DWORD dwCount = 0;
  584. m_ProductPolicyModuleRWLock.Acquire(WRITER_LOCK);
  585. for( PMProductPolicyMapType::iterator it = m_ProductPolicyModule.begin();
  586. it != m_ProductPolicyModule.end();
  587. it++ )
  588. {
  589. CTLSPolicy* ptr = (CTLSPolicy*) (*it).second;
  590. if(ptr->InitializePolicyModule() == ERROR_SUCCESS)
  591. {
  592. dwCount++;
  593. }
  594. }
  595. m_ProductPolicyModuleRWLock.Release(WRITER_LOCK);
  596. return dwCount;
  597. }
  598. //-------------------------------------------------------
  599. CTLSPolicyMgr::PMProductTransationMapType::iterator
  600. CTLSPolicyMgr::FindProductTransation(
  601. LPCTSTR pszCompanyName,
  602. LPCTSTR pszCHProductCode
  603. )
  604. /*++
  605. --*/
  606. {
  607. PolicyModule pm;
  608. PMProductTransationMapType::iterator it;
  609. memset(&pm, 0, sizeof(pm));
  610. if(pszCompanyName)
  611. {
  612. lstrcpyn(
  613. pm.m_szCompanyName,
  614. pszCompanyName,
  615. sizeof(pm.m_szCompanyName)/sizeof(pm.m_szCompanyName[0])
  616. );
  617. }
  618. if(pszCHProductCode)
  619. {
  620. lstrcpyn(
  621. pm.m_szProductId,
  622. pszCHProductCode,
  623. sizeof(pm.m_szProductId)/sizeof(pm.m_szProductId[0])
  624. );
  625. }
  626. it = m_ProductTranslation.find( pm );
  627. return it;
  628. }
  629. //-------------------------------------------------------
  630. BOOL
  631. CTLSPolicyMgr::TranslateCHCodeToTlsCode(
  632. LPCTSTR pszCompanyName,
  633. LPCTSTR pszCHCode,
  634. LPTSTR pszTlsProductCode,
  635. PDWORD pdwBufferSize
  636. )
  637. /*++
  638. --*/
  639. {
  640. PMProductTransationMapType::iterator it;
  641. DWORD dwBufSize = *pdwBufferSize;
  642. SetLastError(ERROR_SUCCESS);
  643. m_ProductTranslationRWLock.Acquire(READER_LOCK);
  644. it = FindProductTransation(
  645. pszCompanyName,
  646. pszCHCode
  647. );
  648. if(it == m_ProductTranslation.end())
  649. {
  650. SetLastError(ERROR_INVALID_PARAMETER);
  651. }
  652. else
  653. {
  654. lstrcpyn(
  655. pszTlsProductCode,
  656. (*it).second.m_szProductId,
  657. dwBufSize
  658. );
  659. *pdwBufferSize = lstrlen((*it).second.m_szProductId);
  660. if(*pdwBufferSize >= dwBufSize)
  661. {
  662. SetLastError(ERROR_INSUFFICIENT_BUFFER);
  663. }
  664. }
  665. m_ProductTranslationRWLock.Release(READER_LOCK);
  666. return GetLastError() == ERROR_SUCCESS;
  667. }
  668. //-------------------------------------------------------
  669. void
  670. CTLSPolicyMgr::InsertProductTransation(
  671. LPCTSTR pszCompanyName,
  672. LPCTSTR pszCHProductCode,
  673. LPCTSTR pszTLSProductCode
  674. )
  675. /*++
  676. List must be locked before entering this routine.
  677. --*/
  678. {
  679. PolicyModule key;
  680. PolicyModule value;
  681. memset(&key, 0, sizeof(key));
  682. memset(&value, 0, sizeof(value));
  683. if(pszCompanyName)
  684. {
  685. lstrcpyn(
  686. key.m_szCompanyName,
  687. pszCompanyName,
  688. sizeof(key.m_szCompanyName)/sizeof(key.m_szCompanyName[0])
  689. );
  690. lstrcpyn(
  691. value.m_szCompanyName,
  692. pszCompanyName,
  693. sizeof(value.m_szCompanyName)/sizeof(value.m_szCompanyName[0])
  694. );
  695. }
  696. if(pszCHProductCode)
  697. {
  698. lstrcpyn(
  699. key.m_szProductId,
  700. pszCHProductCode,
  701. sizeof(key.m_szProductId)/sizeof(key.m_szProductId[0])
  702. );
  703. }
  704. if(pszTLSProductCode)
  705. {
  706. lstrcpyn(
  707. value.m_szProductId,
  708. pszTLSProductCode,
  709. sizeof(key.m_szProductId)/sizeof(key.m_szProductId[0])
  710. );
  711. }
  712. //
  713. // Replace if already exists.
  714. //
  715. m_ProductTranslation[key] = value;
  716. return;
  717. }
  718. //-------------------------------------------------------
  719. CTLSPolicyMgr::PMProductPolicyMapType::iterator
  720. CTLSPolicyMgr::FindProductPolicyModule(
  721. LPCTSTR pszCompanyName,
  722. LPCTSTR pszProductId
  723. )
  724. /*++
  725. Must acquire reader/writer lock before
  726. calling this routine
  727. ++*/
  728. {
  729. PolicyModule pm;
  730. PMProductPolicyMapType::iterator it;
  731. CTLSPolicy* ptr=NULL;
  732. memset(&pm, 0, sizeof(pm));
  733. if(pszCompanyName)
  734. {
  735. lstrcpyn(
  736. pm.m_szCompanyName,
  737. pszCompanyName,
  738. sizeof(pm.m_szCompanyName)/sizeof(pm.m_szCompanyName[0])
  739. );
  740. }
  741. if(pszProductId)
  742. {
  743. lstrcpyn(
  744. pm.m_szProductId,
  745. pszProductId,
  746. sizeof(pm.m_szProductId)/sizeof(pm.m_szProductId[0])
  747. );
  748. }
  749. it = m_ProductPolicyModule.find( pm );
  750. return it;
  751. }
  752. //-------------------------------------------------------
  753. DWORD
  754. CTLSPolicyMgr::GetSupportedProduct(
  755. IN HINSTANCE hPolicyModule,
  756. IN LPCTSTR pszDllName,
  757. IN LPCTSTR pszCompanyName,
  758. IN LPCTSTR pszProductId,
  759. IN OUT PDWORD pdwNumProducts,
  760. OUT PPMSUPPORTEDPRODUCT* pSupportedProduct
  761. )
  762. /*++
  763. Abstract:
  764. Get list of supported product from policy module
  765. Parameters:
  766. pszCompanyName : Name of the company in registry
  767. pszProductId : Name of the product in registry
  768. pdwNumProducts : Pointer to DWORD, return number of product supported by policy module
  769. ppszSupportedProduct : Pointer to string array, return number of product supported by policy module.
  770. Return:
  771. --*/
  772. {
  773. TLSPMInitialize pfnPMInitialize = NULL;
  774. POLICYSTATUS dwPolStatus = POLICY_SUCCESS;
  775. DWORD dwPolRetCode = ERROR_SUCCESS;
  776. DWORD dwStatus = ERROR_SUCCESS;
  777. EXCEPTION_RECORD ExceptionCode;
  778. PPMSUPPORTEDPRODUCT pProductList = NULL;
  779. DWORD dwIndex;
  780. *pSupportedProduct = NULL;
  781. *pdwNumProducts = 0;
  782. if(hPolicyModule != NULL && pszCompanyName != NULL && pdwNumProducts != NULL && pSupportedProduct != NULL)
  783. {
  784. pfnPMInitialize = (TLSPMInitialize) GetProcAddress(
  785. hPolicyModule,
  786. INITIALIZEPROCNAME
  787. );
  788. if(pfnPMInitialize != NULL)
  789. {
  790. __try {
  791. dwPolStatus = pfnPMInitialize(
  792. TLS_CURRENT_VERSION,
  793. pszCompanyName,
  794. pszProductId,
  795. pdwNumProducts,
  796. &pProductList,
  797. &dwPolRetCode
  798. );
  799. if(dwPolStatus != POLICY_SUCCESS)
  800. {
  801. TLSLogEvent(
  802. EVENTLOG_WARNING_TYPE,
  803. TLS_E_LOADPOLICY,
  804. TLS_E_POLICYMODULEPMINITALIZZE,
  805. pszCompanyName,
  806. pszProductId,
  807. dwPolRetCode
  808. );
  809. dwStatus = TLS_E_REQUESTDENYPOLICYERROR;
  810. }
  811. else if(*pdwNumProducts != 0 && pProductList != NULL)
  812. {
  813. *pSupportedProduct = (PPMSUPPORTEDPRODUCT)AllocateMemory(sizeof(PMSUPPORTEDPRODUCT) * (*pdwNumProducts));
  814. if(*pSupportedProduct != NULL)
  815. {
  816. for(dwIndex = 0; dwIndex < *pdwNumProducts && dwStatus == ERROR_SUCCESS; dwIndex ++)
  817. {
  818. (*pSupportedProduct)[dwIndex] = pProductList[dwIndex];
  819. }
  820. }
  821. else
  822. {
  823. dwStatus = ERROR_OUTOFMEMORY;
  824. }
  825. }
  826. }
  827. __except (
  828. ExceptionCode = *(GetExceptionInformation())->ExceptionRecord,
  829. EXCEPTION_EXECUTE_HANDLER )
  830. {
  831. dwStatus = ExceptionCode.ExceptionCode;
  832. TLSLogEvent(
  833. EVENTLOG_ERROR_TYPE,
  834. TLS_E_LOADPOLICY,
  835. TLS_E_POLICYMODULEEXCEPTION,
  836. pszCompanyName,
  837. pszProductId,
  838. dwStatus
  839. );
  840. DBGPrintf(
  841. DBG_INFORMATION,
  842. DBG_FACILITY_POLICY,
  843. DBG_ALL_LEVEL,
  844. _TEXT("<%s - %s> : PMInitialize() caused exception 0x%08x\n"),
  845. pszCompanyName,
  846. pszProductId,
  847. dwStatus
  848. );
  849. }
  850. }
  851. else
  852. {
  853. //
  854. // Policy module must support PMInitialize
  855. //
  856. dwStatus = TLS_E_LOADPOLICYMODULE_API;
  857. TLSLogEvent(
  858. EVENTLOG_ERROR_TYPE,
  859. TLS_E_LOADPOLICY,
  860. TLS_E_LOADPOLICYMODULE_API,
  861. INITIALIZEPROCNAME
  862. );
  863. }
  864. }
  865. else
  866. {
  867. dwStatus = ERROR_INVALID_PARAMETER;
  868. }
  869. if(dwStatus != ERROR_SUCCESS)
  870. {
  871. if(pSupportedProduct != NULL)
  872. {
  873. FreeMemory(pSupportedProduct);
  874. }
  875. }
  876. return dwStatus;
  877. }
  878. //-----------------------------------------------------------
  879. DWORD
  880. CTLSPolicyMgr::InsertProductPolicyModule(
  881. IN HMODULE hModule,
  882. IN BOOL bReplace,
  883. IN LPCTSTR pszCompanyName,
  884. IN LPCTSTR pszCHProductCode,
  885. IN LPCTSTR pszTLSProductCode,
  886. IN LPCTSTR pszDllName,
  887. IN DWORD dwFlag
  888. )
  889. /*++
  890. Abstract:
  891. Insert or replace an existing policy module
  892. Parameters:
  893. bReplace : TRUE if replace existing policy module, FALSE otherwise.
  894. pszCompanyName : Name of the company.
  895. pszProductId : Name of the product.
  896. pszDllName : Full path to the policy DLL.
  897. returns:
  898. ++*/
  899. {
  900. CTLSPolicy* ptr;
  901. DWORD dwErrCode = ERROR_SUCCESS;
  902. PMProductPolicyMapType::iterator it;
  903. PMProductTransationMapType::iterator translation_it;
  904. //
  905. // Lock module array
  906. //
  907. m_ProductPolicyModuleRWLock.Acquire(WRITER_LOCK);
  908. m_ProductTranslationRWLock.Acquire(WRITER_LOCK);
  909. it = FindProductPolicyModule(
  910. pszCompanyName,
  911. pszTLSProductCode
  912. );
  913. translation_it = FindProductTransation(
  914. pszCompanyName,
  915. pszCHProductCode
  916. );
  917. if( translation_it != m_ProductTranslation.end() && it == m_ProductPolicyModule.end() )
  918. {
  919. dwErrCode = TLS_E_INTERNAL;
  920. goto cleanup;
  921. }
  922. //
  923. // insert transation
  924. //
  925. InsertProductTransation(
  926. pszCompanyName,
  927. pszCHProductCode,
  928. pszTLSProductCode
  929. );
  930. //
  931. // Replace policy module -
  932. //
  933. ptr = new CTLSPolicy;
  934. if(ptr != NULL)
  935. {
  936. dwErrCode = ptr->Initialize(
  937. hModule,
  938. pszCompanyName,
  939. pszCHProductCode,
  940. pszTLSProductCode,
  941. pszDllName,
  942. dwFlag
  943. );
  944. if(dwErrCode == ERROR_SUCCESS || dwFlag == POLICY_DENY_ALL_REQUEST)
  945. {
  946. PolicyModule pm;
  947. if(pszCompanyName)
  948. {
  949. _tcscpy(pm.m_szCompanyName, pszCompanyName);
  950. }
  951. if(pszTLSProductCode)
  952. {
  953. _tcscpy(pm.m_szProductId, pszTLSProductCode);
  954. }
  955. // m_Handles.insert( pair<PolicyModule, CTLSPolicy*>(pm, ptr) );
  956. m_ProductPolicyModule[pm] = ptr;
  957. }
  958. }
  959. else
  960. {
  961. dwErrCode = ERROR_OUTOFMEMORY;
  962. }
  963. cleanup:
  964. m_ProductTranslationRWLock.Release(WRITER_LOCK);
  965. m_ProductPolicyModuleRWLock.Release(WRITER_LOCK);
  966. return dwErrCode;
  967. }
  968. //----------------------------------------------------------------------
  969. DWORD
  970. CTLSPolicyMgr::AddPolicyModule(
  971. IN BOOL bReplace,
  972. IN LPCTSTR pszCompanyName,
  973. IN LPCTSTR pszProductCode,
  974. IN LPCTSTR pszDllName,
  975. IN DWORD dwFlag
  976. )
  977. /*++
  978. Abstract:
  979. Insert or replace an existing policy module
  980. Parameters:
  981. bReplace : TRUE if replace existing policy module, FALSE otherwise.
  982. pszCompanyName : Name of the company.
  983. pszProductId : Name of the product.
  984. pszDllName : Full path to the policy DLL.
  985. returns:
  986. ++*/
  987. {
  988. DWORD dwErrCode = ERROR_SUCCESS;
  989. DWORD dwNumProduct;
  990. DWORD dwIndex;
  991. DWORD dwUnloadIndex;
  992. HINSTANCE hInstance = NULL;
  993. PMProductPolicyMapType::iterator it;
  994. PPMSUPPORTEDPRODUCT pSupportedProduct = NULL;
  995. //
  996. // Load policy module.
  997. //
  998. hInstance = LoadPolicyModule(
  999. pszCompanyName,
  1000. pszProductCode,
  1001. pszDllName
  1002. );
  1003. if(hInstance != NULL)
  1004. {
  1005. //
  1006. // Insert all support product
  1007. //
  1008. dwErrCode = GetSupportedProduct(
  1009. hInstance,
  1010. pszDllName,
  1011. pszCompanyName,
  1012. pszProductCode,
  1013. &dwNumProduct,
  1014. &pSupportedProduct
  1015. );
  1016. if(dwNumProduct != 0 && pSupportedProduct != NULL)
  1017. {
  1018. for(dwIndex=0;
  1019. dwIndex < dwNumProduct && dwErrCode == ERROR_SUCCESS;
  1020. dwIndex++)
  1021. {
  1022. dwErrCode = InsertProductPolicyModule(
  1023. hInstance,
  1024. bReplace,
  1025. pszCompanyName,
  1026. pSupportedProduct[dwIndex].szCHSetupCode,
  1027. pSupportedProduct[dwIndex].szTLSProductCode,
  1028. pszDllName,
  1029. dwFlag
  1030. );
  1031. }
  1032. }
  1033. else
  1034. {
  1035. dwErrCode = InsertProductPolicyModule(
  1036. hInstance,
  1037. bReplace,
  1038. pszCompanyName,
  1039. pszProductCode,
  1040. pszProductCode,
  1041. pszDllName,
  1042. dwFlag
  1043. );
  1044. }
  1045. }
  1046. else
  1047. {
  1048. dwErrCode = GetLastError();
  1049. }
  1050. if(dwErrCode != ERROR_SUCCESS)
  1051. {
  1052. //
  1053. // unload this policy module
  1054. //
  1055. for(dwUnloadIndex = 0; dwUnloadIndex < dwIndex; dwUnloadIndex++)
  1056. {
  1057. it = FindProductPolicyModule(
  1058. pszCompanyName,
  1059. pSupportedProduct[dwIndex].szTLSProductCode
  1060. );
  1061. if(it != m_ProductPolicyModule.end())
  1062. {
  1063. CTLSPolicy *ptr;
  1064. ptr = (CTLSPolicy *)(*it).second;
  1065. delete ptr;
  1066. m_ProductPolicyModule.erase(it);
  1067. }
  1068. }
  1069. //
  1070. // Let destructor to unload DLL
  1071. //
  1072. }
  1073. if(pSupportedProduct != NULL)
  1074. {
  1075. FreeMemory(pSupportedProduct);
  1076. }
  1077. return dwErrCode;
  1078. }
  1079. //-------------------------------------------------------
  1080. CTLSPolicy*
  1081. CTLSPolicyMgr::AcquireProductPolicyModule(
  1082. LPCTSTR pszCompanyName,
  1083. LPCTSTR pszProductId
  1084. )
  1085. /*++
  1086. ++*/
  1087. {
  1088. m_ProductPolicyModuleRWLock.Acquire(READER_LOCK);
  1089. PMProductPolicyMapType::iterator it;
  1090. CTLSPolicy* ptr=NULL;
  1091. it = FindProductPolicyModule(
  1092. pszCompanyName,
  1093. pszProductId
  1094. );
  1095. if(it != m_ProductPolicyModule.end())
  1096. {
  1097. ptr = (*it).second;
  1098. ptr->Acquire();
  1099. }
  1100. m_ProductPolicyModuleRWLock.Release(READER_LOCK);
  1101. return ptr;
  1102. }
  1103. //-------------------------------------------------------
  1104. void
  1105. CTLSPolicyMgr::ReleaseProductPolicyModule(
  1106. CTLSPolicy* p
  1107. )
  1108. /*++
  1109. ++*/
  1110. {
  1111. assert(p != NULL);
  1112. p->Release();
  1113. return;
  1114. }
  1115. /////////////////////////////////////////////////////////
  1116. //
  1117. // CTLSPolicy Implementation
  1118. //
  1119. /////////////////////////////////////////////////////////
  1120. //-------------------------------------------------------
  1121. DWORD
  1122. CTLSPolicy::InitializePolicyModule()
  1123. {
  1124. DWORD dwStatus=ERROR_SUCCESS;
  1125. if(m_dwModuleState == MODULE_LOADED)
  1126. {
  1127. //
  1128. // Initialize Policy Module
  1129. //
  1130. dwStatus = PMInitProduct();
  1131. }
  1132. else if(m_dwModuleState == MODULE_ERROR)
  1133. {
  1134. dwStatus = TLS_E_POLICYERROR;
  1135. }
  1136. else if(m_dwModuleState != MODULE_PMINITALIZED)
  1137. {
  1138. dwStatus = TLS_E_POLICYNOTINITIALIZE;
  1139. }
  1140. return dwStatus;
  1141. }
  1142. //-------------------------------------------------------
  1143. DWORD
  1144. CTLSPolicy::Initialize(
  1145. IN HINSTANCE hInstance,
  1146. IN LPCTSTR pszCompanyName,
  1147. IN LPCTSTR pszCHProductCode,
  1148. IN LPCTSTR pszTLSProductCode,
  1149. IN LPCTSTR pszDllName,
  1150. IN DWORD dwDllFlags // deny all request if failed to load
  1151. )
  1152. /*++
  1153. Abstract:
  1154. This routine load the policy module's DLL.
  1155. Parameters:
  1156. pszCompanyName : Name of the company.
  1157. pszProductId : Product Id.
  1158. pszDllName : Full path to policy module's DLL.
  1159. Returns:
  1160. ERROR_SUCCESS or error code from LoadLibrary() or
  1161. GetProAddress().
  1162. ++*/
  1163. {
  1164. m_dwFlags = dwDllFlags;
  1165. DWORD dwErrCode=ERROR_SUCCESS;
  1166. TCHAR szDllFullPath[MAX_PATH+1];
  1167. DWORD dwBuffSize = MAX_PATH;
  1168. if(hInstance == NULL)
  1169. {
  1170. dwErrCode = ERROR_INVALID_PARAMETER;
  1171. goto cleanup;
  1172. }
  1173. //
  1174. // Set the module state to unknown
  1175. //
  1176. SetModuleState(MODULE_UNKNOWN);
  1177. SetLastError(ERROR_SUCCESS);
  1178. //
  1179. // Load policy module
  1180. //
  1181. m_hPolicyModule = hInstance;
  1182. // make sure all require API is exported.
  1183. m_pfnReturnLicense = (TLSPMReturnLicense) GetProcAddress(
  1184. m_hPolicyModule,
  1185. RETURNLICENSEPROCNAME
  1186. );
  1187. if(m_pfnReturnLicense == NULL)
  1188. {
  1189. dwErrCode = GetLastError();
  1190. TLSLogEvent(
  1191. EVENTLOG_ERROR_TYPE,
  1192. TLS_E_LOADPOLICY,
  1193. TLS_E_LOADPOLICYMODULE_API,
  1194. RETURNLICENSEPROCNAME
  1195. );
  1196. goto cleanup;
  1197. }
  1198. m_pfnLicenseUpgrade = (TLSPMLicenseUpgrade) GetProcAddress(
  1199. m_hPolicyModule,
  1200. LICENSEUPGRADEPROCNAME
  1201. );
  1202. if(m_pfnLicenseUpgrade == NULL)
  1203. {
  1204. dwErrCode = GetLastError();
  1205. TLSLogEvent(
  1206. EVENTLOG_ERROR_TYPE,
  1207. TLS_E_LOADPOLICY,
  1208. TLS_E_LOADPOLICYMODULE_API,
  1209. LICENSEUPGRADEPROCNAME
  1210. );
  1211. goto cleanup;
  1212. }
  1213. m_pfnLicenseRequest = (TLSPMLicenseRequest) GetProcAddress(
  1214. m_hPolicyModule,
  1215. LICENSEREQUESTPROCNAME
  1216. );
  1217. if(m_pfnLicenseRequest == NULL)
  1218. {
  1219. dwErrCode = GetLastError();
  1220. TLSLogEvent(
  1221. EVENTLOG_ERROR_TYPE,
  1222. TLS_E_LOADPOLICY,
  1223. TLS_E_LOADPOLICYMODULE_API,
  1224. LICENSEREQUESTPROCNAME
  1225. );
  1226. goto cleanup;
  1227. }
  1228. m_pfnUnloadProduct = (TLSPMUnloadProduct) GetProcAddress(
  1229. m_hPolicyModule,
  1230. ULOADPRODUCTPROCNAME
  1231. );
  1232. if(m_pfnUnloadProduct == NULL)
  1233. {
  1234. dwErrCode = GetLastError();
  1235. TLSLogEvent(
  1236. EVENTLOG_ERROR_TYPE,
  1237. TLS_E_LOADPOLICY,
  1238. TLS_E_LOADPOLICYMODULE_API,
  1239. ULOADPRODUCTPROCNAME
  1240. );
  1241. goto cleanup;
  1242. }
  1243. m_pfnInitProduct = (TLSPMInitializeProduct) GetProcAddress(
  1244. m_hPolicyModule,
  1245. SUPPORTEDPRODUCTPROCNAME
  1246. );
  1247. if(m_pfnInitProduct == NULL)
  1248. {
  1249. dwErrCode = GetLastError();
  1250. TLSLogEvent(
  1251. EVENTLOG_ERROR_TYPE,
  1252. TLS_E_LOADPOLICY,
  1253. TLS_E_LOADPOLICYMODULE_API,
  1254. INITIALIZEPROCNAME
  1255. );
  1256. goto cleanup;
  1257. }
  1258. m_pfnRegisterLkp = (TLSPMRegisterLicensePack) GetProcAddress(
  1259. m_hPolicyModule,
  1260. REGISTERLKPPROCNAME
  1261. );
  1262. if(m_pfnRegisterLkp == NULL)
  1263. {
  1264. dwErrCode = GetLastError();
  1265. TLSLogEvent(
  1266. EVENTLOG_ERROR_TYPE,
  1267. TLS_E_LOADPOLICY,
  1268. TLS_E_LOADPOLICYMODULE_API,
  1269. REGISTERLKPPROCNAME
  1270. );
  1271. goto cleanup;
  1272. }
  1273. //
  1274. // Everything is OK, advance module state
  1275. //
  1276. SetModuleState(MODULE_LOADED);
  1277. if(pszCompanyName)
  1278. {
  1279. _tcsncpy(
  1280. m_szCompanyName,
  1281. pszCompanyName,
  1282. sizeof(m_szCompanyName) / sizeof(m_szCompanyName[0])
  1283. );
  1284. }
  1285. if(pszTLSProductCode)
  1286. {
  1287. _tcsncpy(
  1288. m_szProductId,
  1289. pszTLSProductCode,
  1290. sizeof(m_szProductId)/sizeof(m_szProductId[0])
  1291. );
  1292. }
  1293. else
  1294. {
  1295. LoadResourceString(
  1296. IDS_UNKNOWN_STRING,
  1297. m_szProductId,
  1298. sizeof(m_szProductId) / sizeof(m_szProductId[0])
  1299. );
  1300. }
  1301. if(pszCHProductCode)
  1302. {
  1303. _tcsncpy(
  1304. m_szCHProductId,
  1305. pszCHProductCode,
  1306. sizeof(m_szCHProductId)/sizeof(m_szCHProductId[0])
  1307. );
  1308. }
  1309. else
  1310. {
  1311. LoadResourceString(
  1312. IDS_UNKNOWN_STRING,
  1313. m_szCHProductId,
  1314. sizeof(m_szCHProductId) / sizeof(m_szCHProductId[0])
  1315. );
  1316. }
  1317. cleanup:
  1318. if(IsValid() == FALSE)
  1319. {
  1320. TLSLogEvent(
  1321. EVENTLOG_WARNING_TYPE,
  1322. TLS_E_LOADPOLICY,
  1323. (m_dwFlags == POLICY_DENY_ALL_REQUEST) ?
  1324. TLS_W_LOADPOLICYMODULEDENYALLREQUEST : TLS_W_LOADPOLICYMODULEUSEDEFAULT,
  1325. pszDllName
  1326. );
  1327. //
  1328. // don't report error again.
  1329. //
  1330. m_bAlreadyLogError = TRUE;
  1331. }
  1332. return dwErrCode;
  1333. }
  1334. //---------------------------------------------------------------------------
  1335. BOOL
  1336. CTLSPolicy::IsValid()
  1337. /*++
  1338. Abstract:
  1339. This routine determine if the CTLSPolicy object is valid or not.
  1340. Parameters:
  1341. None.
  1342. Returns:
  1343. TRUE if valid, FALSE otherwise.
  1344. ++*/
  1345. {
  1346. return (m_hPolicyModule != NULL &&
  1347. m_pfnReturnLicense != NULL &&
  1348. m_pfnLicenseUpgrade != NULL &&
  1349. m_pfnLicenseRequest != NULL &&
  1350. m_pfnUnloadProduct != NULL &&
  1351. m_pfnInitProduct != NULL &&
  1352. m_pfnRegisterLkp != NULL);
  1353. }
  1354. //---------------------------------------------------------------------------
  1355. void
  1356. CTLSPolicy::LogPolicyRequestStatus(
  1357. DWORD dwMsgId
  1358. )
  1359. /*++
  1360. --*/
  1361. {
  1362. if(m_dwLastCallStatus != POLICY_SUCCESS)
  1363. {
  1364. TLSLogEvent(
  1365. EVENTLOG_WARNING_TYPE,
  1366. TLS_E_POLICYERROR,
  1367. dwMsgId,
  1368. GetCompanyName(),
  1369. GetProductId(),
  1370. m_dwPolicyErrCode
  1371. );
  1372. if(m_dwLastCallStatus == POLICY_CRITICAL_ERROR)
  1373. {
  1374. TLSLogEvent(
  1375. EVENTLOG_ERROR_TYPE,
  1376. TLS_E_POLICYERROR,
  1377. TLS_E_CRITICALPOLICYMODULEERROR,
  1378. GetCompanyName(),
  1379. GetProductId,
  1380. m_dwPolicyErrCode
  1381. );
  1382. SetModuleState(MODULE_ERROR);
  1383. }
  1384. }
  1385. return;
  1386. }
  1387. //----------------------------------------------------------
  1388. DWORD
  1389. CTLSPolicy::PMReturnLicense(
  1390. PMHANDLE hClient,
  1391. ULARGE_INTEGER* pLicenseSerialNumber,
  1392. PPMLICENSETOBERETURN pLicenseTobeReturn,
  1393. PDWORD pdwLicenseStatus
  1394. )
  1395. /*++
  1396. ++*/
  1397. {
  1398. DWORD dwErrCode = ERROR_SUCCESS;
  1399. EXCEPTION_RECORD ExceptionCode;
  1400. _set_se_translator( trans_se_func );
  1401. dwErrCode = InitializePolicyModule();
  1402. if(dwErrCode != ERROR_SUCCESS)
  1403. {
  1404. return dwErrCode;
  1405. }
  1406. __try {
  1407. DBGPrintf(
  1408. DBG_INFORMATION,
  1409. DBG_FACILITY_POLICY,
  1410. DBG_ALL_LEVEL,
  1411. _TEXT("<%s - %s> : PMReturnLicense()\n"),
  1412. GetCompanyName(),
  1413. GetProductId()
  1414. );
  1415. m_dwLastCallStatus = m_pfnReturnLicense(
  1416. hClient,
  1417. pLicenseSerialNumber,
  1418. pLicenseTobeReturn,
  1419. pdwLicenseStatus,
  1420. &m_dwPolicyErrCode
  1421. );
  1422. if(m_dwLastCallStatus != POLICY_SUCCESS)
  1423. {
  1424. LogPolicyRequestStatus(TLS_E_POLICYDENYRETURNLICENSE);
  1425. dwErrCode = TLS_E_REQUESTDENYPOLICYERROR;
  1426. }
  1427. }
  1428. __except (
  1429. ExceptionCode = *(GetExceptionInformation())->ExceptionRecord,
  1430. EXCEPTION_EXECUTE_HANDLER )
  1431. {
  1432. m_dwPolicyErrCode = ExceptionCode.ExceptionCode;
  1433. dwErrCode = TLS_E_POLICYMODULEEXCEPTION;
  1434. m_dwLastCallStatus = POLICY_CRITICAL_ERROR;
  1435. TLSLogEvent(
  1436. EVENTLOG_ERROR_TYPE,
  1437. TLS_E_POLICYERROR,
  1438. TLS_E_POLICYMODULEEXCEPTION,
  1439. GetCompanyName(),
  1440. GetProductId(),
  1441. m_dwPolicyErrCode
  1442. );
  1443. DBGPrintf(
  1444. DBG_INFORMATION,
  1445. DBG_FACILITY_POLICY,
  1446. DBG_ALL_LEVEL,
  1447. _TEXT("<%s - %s> : PMReturnLicense() caused exception 0x%08x\n"),
  1448. GetCompanyName(),
  1449. GetProductId(),
  1450. m_dwPolicyErrCode
  1451. );
  1452. SetModuleState(MODULE_ERROR);
  1453. TLSASSERT(FALSE);
  1454. }
  1455. return dwErrCode;
  1456. }
  1457. //--------------------------------------------------------------
  1458. DWORD
  1459. CTLSPolicy::PMLicenseUpgrade(
  1460. PMHANDLE hClient,
  1461. DWORD dwProgressCode,
  1462. PVOID pbProgressData,
  1463. PVOID* ppbReturnData
  1464. )
  1465. /*++
  1466. ++*/
  1467. {
  1468. DWORD dwErrCode = ERROR_SUCCESS;
  1469. EXCEPTION_RECORD ExceptionCode;
  1470. _set_se_translator( trans_se_func );
  1471. dwErrCode = InitializePolicyModule();
  1472. if(dwErrCode != ERROR_SUCCESS)
  1473. {
  1474. return dwErrCode;
  1475. }
  1476. __try {
  1477. DBGPrintf(
  1478. DBG_INFORMATION,
  1479. DBG_FACILITY_POLICY,
  1480. DBG_ALL_LEVEL,
  1481. _TEXT("<%s - %s> : PMLicenseUpgrade()\n"),
  1482. GetCompanyName(),
  1483. GetProductId()
  1484. );
  1485. m_dwLastCallStatus = m_pfnLicenseUpgrade(
  1486. hClient,
  1487. dwProgressCode,
  1488. pbProgressData,
  1489. ppbReturnData,
  1490. &m_dwPolicyErrCode
  1491. );
  1492. if(m_dwLastCallStatus != ERROR_SUCCESS)
  1493. {
  1494. dwErrCode = TLS_E_REQUESTDENYPOLICYERROR;
  1495. LogPolicyRequestStatus(TLS_E_POLICYDENYUPGRADELICENSE);
  1496. }
  1497. }
  1498. __except (
  1499. ExceptionCode = *(GetExceptionInformation())->ExceptionRecord,
  1500. EXCEPTION_EXECUTE_HANDLER )
  1501. {
  1502. m_dwPolicyErrCode = ExceptionCode.ExceptionCode;
  1503. dwErrCode = TLS_E_POLICYMODULEEXCEPTION;
  1504. m_dwLastCallStatus = POLICY_CRITICAL_ERROR;
  1505. TLSLogEvent(
  1506. EVENTLOG_ERROR_TYPE,
  1507. TLS_E_POLICYERROR,
  1508. TLS_E_POLICYMODULEEXCEPTION,
  1509. GetCompanyName(),
  1510. GetProductId(),
  1511. m_dwPolicyErrCode
  1512. );
  1513. DBGPrintf(
  1514. DBG_INFORMATION,
  1515. DBG_FACILITY_POLICY,
  1516. DBG_ALL_LEVEL,
  1517. _TEXT("<%s - %s> : PMLicenseUpgrade() caused exception 0x%08x\n"),
  1518. GetCompanyName(),
  1519. GetProductId(),
  1520. m_dwPolicyErrCode
  1521. );
  1522. SetModuleState(MODULE_ERROR);
  1523. TLSASSERT(FALSE);
  1524. }
  1525. return dwErrCode;
  1526. }
  1527. //--------------------------------------------------------------
  1528. DWORD
  1529. CTLSPolicy::PMLicenseRequest(
  1530. PMHANDLE client,
  1531. DWORD dwProgressCode,
  1532. const PVOID pbProgressData,
  1533. PVOID* pbNewProgressData
  1534. )
  1535. /*++
  1536. ++*/
  1537. {
  1538. DWORD dwErrCode = ERROR_SUCCESS;
  1539. EXCEPTION_RECORD ExceptionCode;
  1540. _set_se_translator( trans_se_func );
  1541. dwErrCode = InitializePolicyModule();
  1542. if(dwErrCode != ERROR_SUCCESS)
  1543. {
  1544. return dwErrCode;
  1545. }
  1546. __try {
  1547. DBGPrintf(
  1548. DBG_INFORMATION,
  1549. DBG_FACILITY_POLICY,
  1550. DBG_ALL_LEVEL,
  1551. _TEXT("<%s - %s> : PMLicenseRequest()\n"),
  1552. GetCompanyName(),
  1553. GetProductId()
  1554. );
  1555. m_dwLastCallStatus = m_pfnLicenseRequest(
  1556. client,
  1557. dwProgressCode,
  1558. pbProgressData,
  1559. pbNewProgressData,
  1560. &m_dwPolicyErrCode
  1561. );
  1562. if(m_dwLastCallStatus != ERROR_SUCCESS)
  1563. {
  1564. LogPolicyRequestStatus(TLS_E_POLICYDENYNEWLICENSE);
  1565. dwErrCode = TLS_E_REQUESTDENYPOLICYERROR;
  1566. }
  1567. }
  1568. __except (
  1569. ExceptionCode = *(GetExceptionInformation())->ExceptionRecord,
  1570. EXCEPTION_EXECUTE_HANDLER )
  1571. {
  1572. m_dwPolicyErrCode = ExceptionCode.ExceptionCode;
  1573. dwErrCode = TLS_E_POLICYMODULEEXCEPTION;
  1574. m_dwLastCallStatus = POLICY_CRITICAL_ERROR;
  1575. TLSLogEvent(
  1576. EVENTLOG_ERROR_TYPE,
  1577. TLS_E_POLICYERROR,
  1578. TLS_E_POLICYMODULEEXCEPTION,
  1579. GetCompanyName(),
  1580. GetProductId(),
  1581. m_dwPolicyErrCode
  1582. );
  1583. DBGPrintf(
  1584. DBG_INFORMATION,
  1585. DBG_FACILITY_POLICY,
  1586. DBG_ALL_LEVEL,
  1587. _TEXT("<%s - %s> : PMLicenseRequest() caused exception 0x%08x\n"),
  1588. GetCompanyName(),
  1589. GetProductId(),
  1590. m_dwPolicyErrCode
  1591. );
  1592. SetModuleState(MODULE_ERROR);
  1593. TLSASSERT(FALSE);
  1594. }
  1595. return dwErrCode;
  1596. }
  1597. //--------------------------------------------------------------
  1598. DWORD
  1599. CTLSPolicy::PMUnload()
  1600. /*++
  1601. ++*/
  1602. {
  1603. DWORD dwErrCode = ERROR_SUCCESS;
  1604. EXCEPTION_RECORD ExceptionCode;
  1605. _set_se_translator( trans_se_func );
  1606. //
  1607. // Don't call PMUnloadProduct if policy module
  1608. // already in error state.
  1609. //
  1610. if(m_dwModuleState == MODULE_ERROR)
  1611. {
  1612. return ERROR_SUCCESS;
  1613. }
  1614. __try {
  1615. m_dwLastCallStatus = m_pfnUnloadProduct(
  1616. GetCompanyName(),
  1617. GetCHProductId(),
  1618. GetProductId(),
  1619. &m_dwPolicyErrCode
  1620. );
  1621. if(m_dwLastCallStatus != POLICY_SUCCESS)
  1622. {
  1623. LogPolicyRequestStatus(TLS_E_POLICYUNLOADPRODUCT);
  1624. dwErrCode = TLS_E_REQUESTDENYPOLICYERROR;
  1625. }
  1626. }
  1627. __except (
  1628. ExceptionCode = *(GetExceptionInformation())->ExceptionRecord,
  1629. EXCEPTION_EXECUTE_HANDLER )
  1630. {
  1631. m_dwPolicyErrCode = ExceptionCode.ExceptionCode;
  1632. dwErrCode = TLS_E_POLICYMODULEEXCEPTION;
  1633. m_dwLastCallStatus = POLICY_CRITICAL_ERROR;
  1634. TLSLogEvent(
  1635. EVENTLOG_ERROR_TYPE,
  1636. TLS_E_POLICYERROR,
  1637. TLS_E_POLICYMODULEEXCEPTION,
  1638. GetCompanyName(),
  1639. GetProductId(),
  1640. m_dwPolicyErrCode
  1641. );
  1642. SetModuleState(MODULE_ERROR);
  1643. TLSASSERT(FALSE);
  1644. }
  1645. //
  1646. // Always terminate module even error occurred
  1647. //
  1648. SetModuleState(MODULE_PMTERMINATED);
  1649. return dwErrCode;
  1650. }
  1651. //--------------------------------------------------------------
  1652. DWORD
  1653. CTLSPolicy::PMInitProduct()
  1654. /*++
  1655. ++*/
  1656. {
  1657. DWORD dwErrCode = ERROR_SUCCESS;
  1658. EXCEPTION_RECORD ExceptionCode;
  1659. _set_se_translator( trans_se_func );
  1660. if(IsValid() == FALSE)
  1661. {
  1662. return TLS_E_POLICYNOTINITIALIZE;
  1663. }
  1664. __try {
  1665. DBGPrintf(
  1666. DBG_INFORMATION,
  1667. DBG_FACILITY_POLICY,
  1668. DBG_ALL_LEVEL,
  1669. _TEXT("<%s - %s> : PMInitialize()\n"),
  1670. GetCompanyName(),
  1671. GetProductId()
  1672. );
  1673. m_dwLastCallStatus = m_pfnInitProduct(
  1674. GetCompanyName(),
  1675. GetCHProductId(),
  1676. GetProductId(),
  1677. &m_dwPolicyErrCode
  1678. );
  1679. if(m_dwLastCallStatus != POLICY_SUCCESS)
  1680. {
  1681. LogPolicyRequestStatus(TLS_E_POLICYINITPRODUCT);
  1682. dwErrCode = TLS_E_REQUESTDENYPOLICYERROR;
  1683. }
  1684. }
  1685. __except (
  1686. ExceptionCode = *(GetExceptionInformation())->ExceptionRecord,
  1687. EXCEPTION_EXECUTE_HANDLER )
  1688. {
  1689. m_dwPolicyErrCode = ExceptionCode.ExceptionCode;
  1690. dwErrCode = TLS_E_POLICYMODULEEXCEPTION;
  1691. m_dwLastCallStatus = POLICY_CRITICAL_ERROR;
  1692. TLSLogEvent(
  1693. EVENTLOG_ERROR_TYPE,
  1694. TLS_E_POLICYERROR,
  1695. TLS_E_POLICYMODULEEXCEPTION,
  1696. GetCompanyName(),
  1697. GetProductId(),
  1698. m_dwPolicyErrCode
  1699. );
  1700. SetModuleState(MODULE_ERROR);
  1701. TLSASSERT(FALSE);
  1702. }
  1703. SetModuleState(
  1704. (dwErrCode == ERROR_SUCCESS) ? MODULE_PMINITALIZED : MODULE_ERROR
  1705. );
  1706. return dwErrCode;
  1707. }
  1708. //--------------------------------------------------------------
  1709. void
  1710. CTLSPolicy::Unload()
  1711. /*++
  1712. ++*/
  1713. {
  1714. if(m_hPolicyModule == NULL || m_hPolicyModule == INVALID_HANDLE_VALUE)
  1715. return;
  1716. assert(GetRefCount() == 0);
  1717. m_pfnReturnLicense = NULL;
  1718. m_pfnLicenseUpgrade = NULL;
  1719. m_pfnLicenseRequest = NULL;
  1720. m_pfnUnloadProduct = NULL;
  1721. m_pfnInitProduct = NULL;
  1722. m_pfnRegisterLkp = NULL;
  1723. m_hPolicyModule = NULL;
  1724. m_RefCount = 0;
  1725. m_bAlreadyLogError = FALSE;
  1726. SetModuleState(MODULE_UNKNOWN);
  1727. }
  1728. //-------------------------------------------------------------------
  1729. DWORD
  1730. CTLSPolicy::PMRegisterLicensePack(
  1731. PMHANDLE hClient,
  1732. DWORD dwProgressCode,
  1733. const PVOID pbProgessData,
  1734. PVOID pbProgressRetData
  1735. )
  1736. /*++
  1737. ++*/
  1738. {
  1739. DWORD dwErrCode = ERROR_SUCCESS;
  1740. EXCEPTION_RECORD ExceptionCode;
  1741. _set_se_translator( trans_se_func );
  1742. dwErrCode = InitializePolicyModule();
  1743. if(dwErrCode != ERROR_SUCCESS)
  1744. {
  1745. return dwErrCode;
  1746. }
  1747. __try {
  1748. DBGPrintf(
  1749. DBG_INFORMATION,
  1750. DBG_FACILITY_POLICY,
  1751. DBG_ALL_LEVEL,
  1752. _TEXT("<%s - %s> : PMRegisterLicensePack()\n"),
  1753. GetCompanyName(),
  1754. GetProductId()
  1755. );
  1756. m_dwLastCallStatus = m_pfnRegisterLkp(
  1757. hClient,
  1758. dwProgressCode,
  1759. pbProgessData,
  1760. pbProgressRetData,
  1761. &m_dwPolicyErrCode
  1762. );
  1763. if(m_dwLastCallStatus != POLICY_SUCCESS)
  1764. {
  1765. LogPolicyRequestStatus(TLS_E_POLICYMODULEREGISTERLKP);
  1766. dwErrCode = TLS_E_REQUESTDENYPOLICYERROR;
  1767. }
  1768. }
  1769. __except (
  1770. ExceptionCode = *(GetExceptionInformation())->ExceptionRecord,
  1771. EXCEPTION_EXECUTE_HANDLER )
  1772. {
  1773. m_dwPolicyErrCode = ExceptionCode.ExceptionCode;
  1774. dwErrCode = TLS_E_POLICYMODULEEXCEPTION;
  1775. m_dwLastCallStatus = POLICY_CRITICAL_ERROR;
  1776. TLSLogEvent(
  1777. EVENTLOG_ERROR_TYPE,
  1778. TLS_E_POLICYERROR,
  1779. TLS_E_POLICYMODULEEXCEPTION,
  1780. GetCompanyName(),
  1781. GetProductId(),
  1782. m_dwPolicyErrCode
  1783. );
  1784. SetModuleState(MODULE_ERROR);
  1785. TLSASSERT(FALSE);
  1786. }
  1787. return dwErrCode;
  1788. }