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.

609 lines
15 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Copyright (c) 1997-1999 Microsoft Corporation
  4. //
  5. // File: policy.h
  6. //
  7. // Contents:
  8. //
  9. // History:
  10. //
  11. //---------------------------------------------------------------------------
  12. #ifndef __POLICY_MODULE_MGR__
  13. #define __POLICY_MODULE_MGR__
  14. #include "tlsstl.h" // STL.
  15. #include <stdio.h>
  16. #include <tchar.h>
  17. #include "tlsdef.h"
  18. #include "tlspol.h"
  19. #include "locks.h"
  20. #define RETURNLICENSEPROCNAME "PMReturnLicense"
  21. #define LICENSEUPGRADEPROCNAME "PMLicenseUpgrade"
  22. #define LICENSEREQUESTPROCNAME "PMLicenseRequest"
  23. #define TEMINATEPROCNAME "PMTerminate"
  24. #define INITIALIZEPROCNAME "PMInitialize"
  25. #define SUPPORTEDPRODUCTPROCNAME "PMInitializeProduct"
  26. #define ULOADPRODUCTPROCNAME "PMUnloadProduct"
  27. #define REGISTERLKPPROCNAME "PMRegisterLicensePack"
  28. #define POLICY_DENY_ALL_REQUEST 0x00000000
  29. #define POLICY_USE_DEFAULT 0x00000001
  30. typedef POLICYSTATUS (WINAPI *TLSPMReturnLicense)(
  31. PMHANDLE hClient,
  32. ULARGE_INTEGER* pLicenseSerialNumber,
  33. PPMLICENSETOBERETURN pLicenseToBeReturn,
  34. PDWORD pdwLicenseStatus,
  35. PDWORD pdwRetCode
  36. );
  37. typedef POLICYSTATUS (WINAPI *TLSPMLicenseUpgrade)(
  38. PMHANDLE hClient,
  39. DWORD dwProgressCode,
  40. PVOID pbProgressData,
  41. PVOID* ppbReturnData,
  42. PDWORD pdwRetCode
  43. );
  44. typedef POLICYSTATUS (WINAPI *TLSPMLicenseRequest)(
  45. PMHANDLE client,
  46. DWORD dwProgressCode,
  47. PVOID pbProgressData,
  48. PVOID* pbNewProgressData,
  49. PDWORD pdwRetCode
  50. );
  51. typedef void (WINAPI *TLSPMTerminate)();
  52. typedef POLICYSTATUS (WINAPI* TLSPMInitialize)(
  53. DWORD dwLicenseServerVersion,
  54. LPCTSTR pszCompanyName,
  55. LPCTSTR pszProductCode,
  56. PDWORD pdwNumProduct,
  57. PPMSUPPORTEDPRODUCT* ppProduct,
  58. PDWORD pdwRetCode
  59. );
  60. typedef POLICYSTATUS (WINAPI* TLSPMInitializeProduct)(
  61. LPCTSTR pszCompanyName,
  62. LPCTSTR pszCHProductId,
  63. LPCTSTR pszTLSProductId,
  64. PDWORD pdwRetCode
  65. );
  66. typedef POLICYSTATUS (WINAPI* TLSPMUnloadProduct)(
  67. LPCTSTR pszCompanyName,
  68. LPCTSTR pszCHProductId,
  69. LPCTSTR pszTLSProductCode,
  70. PDWORD pdwRetCode
  71. );
  72. typedef POLICYSTATUS (WINAPI* TLSPMRegisterLicensePack)(
  73. PMHANDLE hClient,
  74. DWORD dwProgressCode,
  75. PVOID pbProgressData,
  76. PVOID pbProgressRetData,
  77. PDWORD pdwRetCode
  78. );
  79. class CTLSPolicyMgr;
  80. ////////////////////////////////////////////////////////////////////////////
  81. class CTLSPolicy {
  82. friend class CTLSPolicyMgr;
  83. private:
  84. typedef enum {
  85. MODULE_UNKNOWN, // object has been initialized.
  86. MODULE_LOADED, // module has been loaded.
  87. MODULE_PMINITALIZED, // module has been initialized.
  88. MODULE_PMTERMINATED, // module has been terminated.
  89. MODULE_ERROR, // module initialization error or
  90. // cause an exeption
  91. MODULE_UNLOADED // module has been unloaded.
  92. } MODULE_STATE;
  93. long m_RefCount;
  94. DWORD m_dwFlags;
  95. BOOL m_bAlreadyLogError;
  96. POLICYSTATUS m_dwLastCallStatus; // Policy Module last call status
  97. DWORD m_dwPolicyErrCode; // Policy specific error code.
  98. MODULE_STATE m_dwModuleState;
  99. //
  100. // Policy module must be multi-thread safe.
  101. //
  102. TLSPMReturnLicense m_pfnReturnLicense;
  103. TLSPMLicenseUpgrade m_pfnLicenseUpgrade;
  104. TLSPMLicenseRequest m_pfnLicenseRequest;
  105. TLSPMUnloadProduct m_pfnUnloadProduct;
  106. TLSPMInitializeProduct m_pfnInitProduct;
  107. TLSPMRegisterLicensePack m_pfnRegisterLkp;
  108. HMODULE m_hPolicyModule;
  109. TCHAR m_szCompanyName[LSERVER_MAX_STRING_SIZE+1];
  110. TCHAR m_szProductId[LSERVER_MAX_STRING_SIZE+1];
  111. TCHAR m_szCHProductId[LSERVER_MAX_STRING_SIZE+1];
  112. //--------------------------------------------------------------
  113. void
  114. SetDllFlags(DWORD dllFlags)
  115. {
  116. m_dwFlags = dllFlags;
  117. }
  118. //--------------------------------------------------------------
  119. long
  120. Acquire()
  121. {
  122. return InterlockedIncrement(&m_RefCount);
  123. }
  124. //--------------------------------------------------------------
  125. long
  126. Release()
  127. {
  128. return InterlockedDecrement(&m_RefCount);
  129. }
  130. //--------------------------------------------------------------
  131. DWORD
  132. InitializePolicyModule();
  133. //--------------------------------------------------------------
  134. void
  135. SetModuleState(
  136. MODULE_STATE state
  137. )
  138. /*++
  139. ++*/
  140. {
  141. m_dwModuleState = state;
  142. return;
  143. }
  144. //-------------------------------------------------------------
  145. MODULE_STATE
  146. GetModuleState()
  147. {
  148. return m_dwModuleState;
  149. }
  150. //-------------------------------------------------------------
  151. void
  152. CreatePolicy(
  153. HMODULE hModule,
  154. LPCTSTR pszCompanyName,
  155. LPCTSTR pszProductId,
  156. TLSPMReturnLicense pfnReturnLicense,
  157. TLSPMLicenseUpgrade pfnLicenseUpgrade,
  158. TLSPMLicenseRequest pfnLicenseRequest,
  159. TLSPMUnloadProduct pfnUnloadProduct,
  160. TLSPMInitializeProduct pfnInitProduct,
  161. TLSPMRegisterLicensePack pfnRegisterLkp
  162. )
  163. /*++
  164. ++*/
  165. {
  166. m_hPolicyModule = hModule;
  167. m_pfnReturnLicense = pfnReturnLicense;
  168. m_pfnLicenseUpgrade = pfnLicenseUpgrade;
  169. m_pfnLicenseRequest = pfnLicenseRequest;
  170. m_pfnUnloadProduct = pfnUnloadProduct;
  171. m_pfnInitProduct = pfnInitProduct;
  172. m_pfnRegisterLkp = pfnRegisterLkp;
  173. SetModuleState(MODULE_PMINITALIZED);
  174. lstrcpyn(
  175. m_szCompanyName,
  176. pszCompanyName,
  177. sizeof(m_szCompanyName) / sizeof(m_szCompanyName[0])
  178. );
  179. lstrcpyn(
  180. m_szProductId,
  181. pszProductId,
  182. sizeof(m_szProductId) / sizeof(m_szProductId[0])
  183. );
  184. }
  185. void
  186. LogPolicyRequestStatus(
  187. DWORD dwMsgId
  188. );
  189. public:
  190. CTLSPolicy() :
  191. m_pfnReturnLicense(NULL),
  192. m_pfnLicenseUpgrade(NULL),
  193. m_pfnLicenseRequest(NULL),
  194. m_pfnUnloadProduct(NULL),
  195. m_pfnInitProduct(NULL),
  196. m_pfnRegisterLkp(NULL),
  197. m_hPolicyModule(NULL),
  198. m_RefCount(0),
  199. m_bAlreadyLogError(FALSE),
  200. m_dwModuleState(MODULE_UNKNOWN),
  201. m_dwFlags(0),
  202. m_dwLastCallStatus(POLICY_SUCCESS),
  203. m_dwPolicyErrCode(ERROR_SUCCESS)
  204. /*++
  205. Constructor
  206. ++*/
  207. {
  208. #ifdef DBG
  209. memset(m_szCompanyName, 0, sizeof(m_szCompanyName));
  210. memset(m_szProductId, 0, sizeof(m_szProductId));
  211. memset(m_szCHProductId, 0, sizeof(m_szCHProductId));
  212. #endif
  213. }
  214. //--------------------------------------------------------------
  215. ~CTLSPolicy()
  216. /*++
  217. Destructor
  218. ++*/
  219. {
  220. Unload();
  221. }
  222. //--------------------------------------------------------------
  223. DWORD
  224. GetProductFlags()
  225. {
  226. return m_dwFlags;
  227. }
  228. //--------------------------------------------------------------
  229. long
  230. GetRefCount()
  231. {
  232. return InterlockedExchange(&m_RefCount, m_RefCount);
  233. }
  234. //--------------------------------------------------------------
  235. void
  236. Unload();
  237. //--------------------------------------------------------------
  238. DWORD
  239. Initialize(
  240. HINSTANCE hInstance,
  241. LPCTSTR pszCompanyName,
  242. LPCTSTR pszCHProductId,
  243. LPCTSTR pszProductId,
  244. LPCTSTR pszDllName,
  245. DWORD dwDllFlag = POLICY_DENY_ALL_REQUEST
  246. );
  247. //--------------------------------------------------------------
  248. LPCTSTR
  249. GetCompanyName()
  250. {
  251. return m_szCompanyName;
  252. }
  253. //--------------------------------------------------------------
  254. LPCTSTR
  255. GetProductId()
  256. {
  257. return m_szProductId;
  258. }
  259. //--------------------------------------------------------------
  260. LPCTSTR
  261. GetCHProductId()
  262. {
  263. return m_szCHProductId;
  264. }
  265. //--------------------------------------------------------------
  266. DWORD
  267. GetPolicyRetCode() { return m_dwPolicyErrCode; }
  268. //--------------------------------------------------------------
  269. DWORD
  270. GetPolicyLastCallStatus() { return m_dwLastCallStatus; }
  271. //--------------------------------------------------------------
  272. BOOL
  273. IsValid();
  274. //--------------------------------------------------------------
  275. DWORD
  276. PMReturnLicense(
  277. PMHANDLE hClient,
  278. ULARGE_INTEGER* pLicenseSerialNumber,
  279. PPMLICENSETOBERETURN pLicenseTobeReturn,
  280. PDWORD pdwLicenseStatus
  281. );
  282. //--------------------------------------------------------------
  283. DWORD
  284. PMLicenseUpgrade(
  285. PMHANDLE hClient,
  286. DWORD dwProgressCode,
  287. PVOID pbProgressData,
  288. PVOID* ppbReturnData
  289. );
  290. //--------------------------------------------------------------
  291. DWORD
  292. PMLicenseRequest(
  293. PMHANDLE client,
  294. DWORD dwProgressCode,
  295. const PVOID pbProgressData,
  296. PVOID* pbNewProgressData
  297. );
  298. //--------------------------------------------------------------
  299. DWORD
  300. PMRegisterLicensePack(
  301. PMHANDLE hClient,
  302. DWORD dwProgressCode,
  303. const PVOID pbProgessData,
  304. PVOID pbProgressRetData
  305. );
  306. //--------------------------------------------------------------
  307. DWORD
  308. PMUnload();
  309. //--------------------------------------------------------------
  310. DWORD
  311. PMInitProduct();
  312. };
  313. /////////////////////////////////////////////////////////////////////
  314. typedef struct __PolicyModule {
  315. TCHAR m_szCompanyName[LSERVER_MAX_STRING_SIZE+1];
  316. TCHAR m_szProductId[LSERVER_MAX_STRING_SIZE+1];
  317. friend bool operator<(const __PolicyModule&, const __PolicyModule&);
  318. __PolicyModule()
  319. {
  320. memset(m_szCompanyName, 0, sizeof(m_szCompanyName));
  321. memset(m_szProductId, 0, sizeof(m_szProductId));
  322. }
  323. } PolicyModule;
  324. inline bool
  325. operator<(
  326. const PolicyModule& a,
  327. const PolicyModule& b
  328. )
  329. /*++
  330. ++*/
  331. {
  332. int iComp;
  333. iComp = _tcsicmp(a.m_szCompanyName, b.m_szCompanyName);
  334. if(iComp == 0)
  335. {
  336. iComp = _tcsicmp(a.m_szProductId, b.m_szProductId);
  337. }
  338. return iComp < 0;
  339. }
  340. //////////////////////////////////////////////////////////
  341. //
  342. // CTLSPolicyMgr must be a global object so destructor is
  343. // invoked at the end.
  344. //
  345. class CTLSPolicyMgr {
  346. private:
  347. typedef map<PolicyModule, HMODULE, less<PolicyModule> > PMLoadedModuleMapType;
  348. typedef map<PolicyModule, CTLSPolicy*, less<PolicyModule> > PMProductPolicyMapType;
  349. typedef map<PolicyModule, PolicyModule, less<PolicyModule> > PMProductTransationMapType;
  350. //
  351. // Policy module per product
  352. //
  353. CRWLock m_ProductPolicyModuleRWLock;
  354. PMProductPolicyMapType m_ProductPolicyModule;
  355. //
  356. // List of loaded policy module
  357. //
  358. CRWLock m_LoadedPolicyRWLock;
  359. PMLoadedModuleMapType m_LoadedPolicy;
  360. //
  361. // List of Product transation
  362. //
  363. CRWLock m_ProductTranslationRWLock;
  364. PMProductTransationMapType m_ProductTranslation;
  365. HMODULE
  366. LoadPolicyModule(
  367. LPCTSTR pszCompanyName,
  368. LPCTSTR pszProductCode,
  369. LPCTSTR pszDllName
  370. );
  371. //
  372. // Find policy module handle for a particular product
  373. //
  374. PMProductPolicyMapType::iterator
  375. FindProductPolicyModule(
  376. LPCTSTR pszCompanyName,
  377. LPCTSTR pszProductId
  378. );
  379. //
  380. // Find policy module handle for a particular product
  381. //
  382. PMProductTransationMapType::iterator
  383. FindProductTransation(
  384. LPCTSTR pszCompanyName,
  385. LPCTSTR pszCHProductCode
  386. );
  387. void
  388. InsertProductTransation(
  389. LPCTSTR pszCompanyName,
  390. LPCTSTR pszCHProductCode,
  391. LPCTSTR pszTLSProductCode
  392. );
  393. DWORD
  394. GetSupportedProduct(
  395. HMODULE hModule,
  396. LPCTSTR pszDllName,
  397. LPCTSTR pszCompanyName,
  398. LPCTSTR pszProductId,
  399. PDWORD pdwNumProducts,
  400. PPMSUPPORTEDPRODUCT* pSupportedProduct
  401. );
  402. DWORD
  403. InsertProductPolicyModule(
  404. HMODULE hModule,
  405. BOOL bReplace,
  406. LPCTSTR pszCompanyName,
  407. LPCTSTR pszCHProductId,
  408. LPCTSTR pszTLSProductId,
  409. LPCTSTR pszDllName,
  410. DWORD dwFlag
  411. );
  412. DWORD
  413. UnloadPolicyModule(
  414. HINSTANCE hModule
  415. );
  416. public:
  417. CTLSPolicyMgr();
  418. ~CTLSPolicyMgr();
  419. //
  420. // Insert a policy module handle into Handle arrays.
  421. //
  422. DWORD
  423. AddPolicyModule(
  424. BOOL bReplace,
  425. LPCTSTR pszCompanyName,
  426. LPCTSTR pszProductId,
  427. LPCTSTR pszDllName,
  428. DWORD dwFlag
  429. );
  430. DWORD
  431. UnloadPolicyModule(
  432. LPCTSTR pszCompanyName,
  433. LPCTSTR pszProductCode
  434. );
  435. //
  436. // Find a policy module for specific product
  437. //
  438. CTLSPolicy*
  439. AcquireProductPolicyModule(
  440. LPCTSTR pszCompanyName,
  441. LPCTSTR pszProductId
  442. );
  443. //
  444. // Find a translation for CH.
  445. //
  446. BOOL
  447. TranslateCHCodeToTlsCode(
  448. LPCTSTR pszCompanyName,
  449. LPCTSTR pszCHProductCode,
  450. LPTSTR pszTlsProductCode,
  451. PDWORD pdwBufferSize
  452. );
  453. //
  454. // Release a product policy module
  455. //
  456. void
  457. ReleaseProductPolicyModule( CTLSPolicy* p );
  458. //
  459. // Initialize All Product Policy Module
  460. //
  461. DWORD
  462. InitProductPolicyModule();
  463. };
  464. #ifdef __cplusplus
  465. extern "C" {
  466. #endif
  467. DWORD
  468. ServiceInitPolicyModule(void);
  469. DWORD
  470. ServiceLoadAllPolicyModule(
  471. IN HKEY hRoot,
  472. IN LPCTSTR pszSubkey
  473. );
  474. CTLSPolicy*
  475. AcquirePolicyModule(
  476. IN LPCTSTR pszCompanyName,
  477. IN LPCTSTR pszProductId,
  478. IN BOOL bUseProductPolicy
  479. );
  480. BOOL
  481. TranslateCHCodeToTlsCode(
  482. IN LPCTSTR pszCompanyName,
  483. IN LPCTSTR pszCHProductId,
  484. IN LPTSTR pszTLSProductId,
  485. IN OUT PDWORD pdwBufferSize
  486. );
  487. void
  488. ReleasePolicyModule(
  489. CTLSPolicy* ptr
  490. );
  491. #ifdef __cplusplus
  492. }
  493. #endif
  494. #endif