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.

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