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.

326 lines
8.5 KiB

  1. /*++
  2. Copyright (c) 1994-95 Microsoft Corporation
  3. Module Name:
  4. licobj.cpp
  5. Abstract:
  6. License object implementation.
  7. Author:
  8. Don Ryan (donryan) 04-Jan-1995
  9. Environment:
  10. User Mode - Win32
  11. Revision History:
  12. Jeff Parham (jeffparh) 12-Nov-1995
  13. Copied from LLSMGR, converted to handle level 1 licenses,
  14. removed OLE support.
  15. --*/
  16. #include "stdafx.h"
  17. #include "ccfapi.h"
  18. #include "licobj.h"
  19. #include <strsafe.h> //include last
  20. #ifdef _DEBUG
  21. #undef THIS_FILE
  22. static char BASED_CODE THIS_FILE[] = __FILE__;
  23. #endif
  24. IMPLEMENT_DYNCREATE(CLicense, CObject)
  25. CLicense::CLicense( LPCTSTR pProduct /* = NULL */,
  26. LPCTSTR pVendor /* = NULL */,
  27. LPCTSTR pAdmin /* = NULL */,
  28. DWORD dwPurchaseDate /* = 0 */,
  29. long lQuantity /* = 0 */,
  30. LPCTSTR pDescription /* = NULL */,
  31. DWORD dwAllowedModes /* = LLS_LICENSE_MODE_ALLOW_PER_SEAT */,
  32. DWORD dwCertificateID /* = 0 */,
  33. LPCTSTR pSource /* = TEXT("None") */,
  34. DWORD dwExpirationDate /* = 0 */,
  35. DWORD dwMaxQuantity /* = 0 */,
  36. LPDWORD pdwSecrets /* = NULL */ )
  37. /*++
  38. Routine Description:
  39. Constructor for CLicense object.
  40. Arguments:
  41. None.
  42. Return Values:
  43. None.
  44. --*/
  45. {
  46. ASSERT(pProduct && *pProduct);
  47. m_strAdmin = pAdmin;
  48. m_strVendor = pVendor;
  49. m_strProduct = pProduct;
  50. m_strDescription = pDescription;
  51. m_strSource = pSource;
  52. m_lQuantity = lQuantity;
  53. m_dwAllowedModes = dwAllowedModes;
  54. m_dwCertificateID = dwCertificateID;
  55. m_dwPurchaseDate = dwPurchaseDate;
  56. m_dwExpirationDate = dwExpirationDate;
  57. m_dwMaxQuantity = dwMaxQuantity;
  58. if ( NULL == pdwSecrets )
  59. {
  60. ZeroMemory( m_adwSecrets, sizeof( m_adwSecrets ) );
  61. }
  62. else
  63. {
  64. memcpy( m_adwSecrets, pdwSecrets, sizeof( m_adwSecrets ) );
  65. }
  66. m_strSourceDisplayName = TEXT("");
  67. m_strAllowedModes = TEXT("");
  68. }
  69. CString CLicense::GetSourceDisplayName()
  70. /*++
  71. Routine Description:
  72. Retrieve the display name for the certificate source that was used to
  73. install these licenses. Note that if the source that was used is not
  74. installed locally, the display name is not retrievable, and the source
  75. name will be returned instead.
  76. Arguments:
  77. None.
  78. Return Values:
  79. CString.
  80. --*/
  81. {
  82. if ( m_strSourceDisplayName.IsEmpty() )
  83. {
  84. if ( !m_strSource.CompareNoCase( TEXT( "None" ) ) )
  85. {
  86. m_strSourceDisplayName.LoadString( IDS_SOURCE_NONE );
  87. }
  88. else
  89. {
  90. LONG lError;
  91. CString strKeyName = TEXT( "Software\\LSAPI\\Microsoft\\CertificateSources\\" )
  92. + m_strSource;
  93. HKEY hKeySource;
  94. lError = RegOpenKeyEx( HKEY_LOCAL_MACHINE, strKeyName, 0, KEY_READ, &hKeySource );
  95. if ( ERROR_SUCCESS == lError )
  96. {
  97. const DWORD cchSourceDisplayName = 80;
  98. DWORD cbSourceDisplayName = sizeof( TCHAR ) * cchSourceDisplayName;
  99. LPTSTR pszSourceDisplayName;
  100. DWORD dwType;
  101. pszSourceDisplayName = m_strSourceDisplayName.GetBuffer( cchSourceDisplayName );
  102. if ( NULL != pszSourceDisplayName )
  103. {
  104. lError = RegQueryValueEx( hKeySource, REG_VALUE_NAME, NULL, &dwType, (LPBYTE) pszSourceDisplayName, &cbSourceDisplayName );
  105. m_strSourceDisplayName.ReleaseBuffer();
  106. }
  107. RegCloseKey( hKeySource );
  108. }
  109. if ( ( ERROR_SUCCESS != lError ) || m_strSourceDisplayName.IsEmpty() )
  110. {
  111. m_strSourceDisplayName = m_strSource;
  112. }
  113. }
  114. }
  115. return m_strSourceDisplayName;
  116. }
  117. DWORD CLicense::CreateLicenseInfo( PLLS_LICENSE_INFO_1 pLicInfo1 )
  118. /*++
  119. Routine Description:
  120. Create a LLS_LICENSE_INFO_1 structure corresponding to this object.
  121. Arguments:
  122. pLicInfo1 (PLLS_LICENSE_INFO_1)
  123. On return, holds the created structure.
  124. Return Values:
  125. ERROR_SUCCESS or ERROR_NOT_ENOUGH_MEMORY.
  126. --*/
  127. {
  128. DWORD dwError;
  129. HRESULT hr;
  130. size_t cch1, cch2, cch3, cch4, cch5;
  131. ASSERT(NULL != pLicInfo1);
  132. cch1 = m_strProduct.GetLength() + 1;
  133. pLicInfo1->Product = (LPTSTR) LocalAlloc(LMEM_FIXED, sizeof(TCHAR) * cch1);
  134. cch2 = m_strVendor.GetLength() + 1;
  135. pLicInfo1->Vendor = (LPTSTR) LocalAlloc(LMEM_FIXED, sizeof(TCHAR) * cch2);
  136. cch3 = m_strAdmin.GetLength() + 1;
  137. pLicInfo1->Admin = (LPTSTR) LocalAlloc(LMEM_FIXED, sizeof(TCHAR) * cch3);
  138. cch4 = m_strDescription.GetLength();
  139. pLicInfo1->Comment = (LPTSTR) LocalAlloc(LMEM_FIXED, sizeof(TCHAR) * cch4);
  140. cch5 = m_strSource.GetLength() + 1;
  141. pLicInfo1->Source = (LPTSTR) LocalAlloc(LMEM_FIXED, sizeof(TCHAR) * cch5);
  142. if ( ( NULL == pLicInfo1->Product )
  143. || ( NULL == pLicInfo1->Vendor )
  144. || ( NULL == pLicInfo1->Admin )
  145. || ( NULL == pLicInfo1->Comment )
  146. || ( NULL == pLicInfo1->Source ) )
  147. {
  148. dwError = ERROR_NOT_ENOUGH_MEMORY;
  149. }
  150. else
  151. {
  152. hr = StringCchCopy( pLicInfo1->Product, cch1, m_strProduct );
  153. ASSERT(SUCCEEDED(hr));
  154. hr = StringCchCopy( pLicInfo1->Vendor, cch2, m_strVendor );
  155. ASSERT(SUCCEEDED(hr));
  156. hr = StringCchCopy( pLicInfo1->Admin, cch3, m_strAdmin );
  157. ASSERT(SUCCEEDED(hr));
  158. hr = StringCchCopy( pLicInfo1->Comment, cch4, m_strDescription );
  159. ASSERT(SUCCEEDED(hr));
  160. hr = StringCchCopy( pLicInfo1->Source, cch5, m_strSource );
  161. ASSERT(SUCCEEDED(hr));
  162. pLicInfo1->Quantity = m_lQuantity;
  163. pLicInfo1->MaxQuantity = m_dwMaxQuantity;
  164. pLicInfo1->Date = m_dwPurchaseDate;
  165. pLicInfo1->AllowedModes = m_dwAllowedModes;
  166. pLicInfo1->CertificateID = m_dwCertificateID;
  167. pLicInfo1->ExpirationDate = m_dwExpirationDate;
  168. memcpy( pLicInfo1->Secrets, m_adwSecrets, sizeof( m_adwSecrets ) );
  169. dwError = ERROR_SUCCESS;
  170. }
  171. if ( ERROR_SUCCESS != dwError )
  172. {
  173. if ( NULL != pLicInfo1->Product ) LocalFree( pLicInfo1->Product );
  174. if ( NULL != pLicInfo1->Vendor ) LocalFree( pLicInfo1->Vendor );
  175. if ( NULL != pLicInfo1->Admin ) LocalFree( pLicInfo1->Admin );
  176. if ( NULL != pLicInfo1->Comment ) LocalFree( pLicInfo1->Comment );
  177. if ( NULL != pLicInfo1->Source ) LocalFree( pLicInfo1->Source );
  178. ZeroMemory( pLicInfo1, sizeof( *pLicInfo1 ) );
  179. }
  180. return dwError;
  181. }
  182. void CLicense::DestroyLicenseInfo( PLLS_LICENSE_INFO_1 pLicInfo1 )
  183. /*++
  184. Routine Description:
  185. Frees a license structure previously created by CreateLicenseInfo().
  186. Arguments:
  187. pLicInfo1 (PLLS_LICENSE_INFO_1)
  188. The structure previously created by CreateLicenseInfo().
  189. Return Values:
  190. None.
  191. --*/
  192. {
  193. ASSERT(NULL != pLicInfo1);
  194. if ( NULL != pLicInfo1->Product ) LocalFree( pLicInfo1->Product );
  195. if ( NULL != pLicInfo1->Vendor ) LocalFree( pLicInfo1->Vendor );
  196. if ( NULL != pLicInfo1->Admin ) LocalFree( pLicInfo1->Admin );
  197. if ( NULL != pLicInfo1->Comment ) LocalFree( pLicInfo1->Comment );
  198. if ( NULL != pLicInfo1->Source ) LocalFree( pLicInfo1->Source );
  199. }
  200. CString CLicense::GetAllowedModesString()
  201. /*++
  202. Routine Description:
  203. Get a string corresponding to the license mode(s) for which this license
  204. was installed.
  205. Arguments:
  206. None.
  207. Return Values:
  208. CString.
  209. --*/
  210. {
  211. if ( m_strAllowedModes.IsEmpty() )
  212. {
  213. UINT uStringID;
  214. switch ( m_dwAllowedModes & ( LLS_LICENSE_MODE_ALLOW_PER_SEAT | LLS_LICENSE_MODE_ALLOW_PER_SERVER ) )
  215. {
  216. case ( LLS_LICENSE_MODE_ALLOW_PER_SEAT | LLS_LICENSE_MODE_ALLOW_PER_SERVER ):
  217. uStringID = IDS_LICENSE_MODE_EITHER;
  218. break;
  219. case LLS_LICENSE_MODE_ALLOW_PER_SEAT:
  220. uStringID = IDS_LICENSE_MODE_PER_SEAT;
  221. break;
  222. case LLS_LICENSE_MODE_ALLOW_PER_SERVER:
  223. uStringID = IDS_LICENSE_MODE_PER_SERVER;
  224. break;
  225. default:
  226. uStringID = IDS_LICENSE_MODE_UNKNOWN;
  227. break;
  228. }
  229. m_strAllowedModes.LoadString( uStringID );
  230. }
  231. return m_strAllowedModes;
  232. }