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.

342 lines
9.8 KiB

  1. /*++
  2. 1998 Seagate Software, Inc. All rights reserved.
  3. Module Name:
  4. HsmAdmin.cpp
  5. Abstract:
  6. Main module file - defines the overall COM server.
  7. Author:
  8. Rohde Wakefield [rohde] 04-Mar-1997
  9. Revision History:
  10. --*/
  11. #include "stdafx.h"
  12. // Include typedefs for all classes declared in DLL
  13. #include "CSakSnap.h"
  14. #include "CSakData.h"
  15. #include "About.h"
  16. #include "Ca.h"
  17. #include "HsmCom.h"
  18. #include "ManVol.h"
  19. #include "ManVolLs.h"
  20. #include "MeSe.h"
  21. #ifdef _MERGE_PROXYSTUB
  22. #include "dlldatax.h"
  23. extern "C" HINSTANCE hProxyDll;
  24. #endif
  25. CComModule _Module;
  26. CHsmAdminApp g_App;
  27. CComPtr<IWsbTrace> g_pTrace;
  28. //
  29. // Marks the beginning of the map of ATL objects in this DLL for which
  30. // class factories will be supplied. When CComModule::RegisterServer is
  31. // called, it updates the system registry for each object in the object map.
  32. //
  33. BEGIN_OBJECT_MAP(ObjectMap)
  34. OBJECT_ENTRY(CLSID_CAbout, CAbout)
  35. OBJECT_ENTRY(CLSID_CUiCar, CUiCar)
  36. OBJECT_ENTRY(CLSID_CUiHsmCom, CUiHsmCom)
  37. OBJECT_ENTRY(CLSID_CUiManVol, CUiManVol)
  38. OBJECT_ENTRY(CLSID_CUiManVolLst, CUiManVolLst)
  39. OBJECT_ENTRY(CLSID_CUiMedSet, CUiMedSet)
  40. OBJECT_ENTRY(CLSID_HsmAdminDataSnapin, CSakDataPrimaryImpl)
  41. OBJECT_ENTRY(CLSID_HsmAdminDataExtension, CSakDataExtensionImpl)
  42. OBJECT_ENTRY(CLSID_HsmAdmin, CSakSnap)
  43. END_OBJECT_MAP()
  44. BOOL CHsmAdminApp::InitInstance()
  45. {
  46. _Module.Init(ObjectMap, m_hInstance);
  47. AfxEnableControlContainer( );
  48. try {
  49. #ifdef _MERGE_PROXYSTUB
  50. hProxyDll = m_hInstance;
  51. #endif
  52. CString keyName;
  53. keyName.Format( L"ClsID\\%ls", WsbGuidAsString( CLSID_CWsbTrace ) );
  54. CRegKey key;
  55. if( key.Open( HKEY_CLASSES_ROOT, keyName, KEY_READ ) != ERROR_SUCCESS ) {
  56. throw( GetLastError( ) );
  57. }
  58. if( SUCCEEDED( g_pTrace.CoCreateInstance( CLSID_CWsbTrace ) ) ) {
  59. CString tracePath, regPath;
  60. CWsbStringPtr outString;
  61. outString.Alloc( 256 );
  62. regPath = L"SOFTWARE\\Microsoft\\RemoteStorage\\RsAdmin";
  63. //
  64. // We want to put the path where the trace file should go.
  65. //
  66. if( WsbGetRegistryValueString( 0, regPath, L"WsbTraceFileName", outString, 256, 0 ) != S_OK ) {
  67. WCHAR * systemPath;
  68. systemPath = _wgetenv( L"SystemRoot" );
  69. tracePath.Format( L"%ls\\System32\\RemoteStorage\\Trace\\RsAdmin.Trc", systemPath );
  70. WsbSetRegistryValueString( 0, regPath, L"WsbTraceFileName", tracePath );
  71. //
  72. // Try a little to make sure the trace directory exists.
  73. //
  74. tracePath.Format( L"%ls\\System32\\RemoteStorage", systemPath );
  75. CreateDirectory( tracePath, 0 );
  76. tracePath += L"\\Trace";
  77. CreateDirectory( tracePath, 0 );
  78. }
  79. g_pTrace->SetRegistryEntry( (LPWSTR)(LPCWSTR)regPath );
  80. g_pTrace->LoadFromRegistry();
  81. }
  82. } catch( ... ) { }
  83. WsbTraceIn( L"CHsmAdminApp::InitInstance", L"" );
  84. HRESULT hr = S_OK;
  85. try {
  86. //
  87. // Need to give complete path to POPUP help file
  88. //
  89. CWsbStringPtr helpFile;
  90. WsbAffirmHr( helpFile.LoadFromRsc( _Module.m_hInst, IDS_HELPFILEPOPUP ) );
  91. CWsbStringPtr winDir;
  92. WsbAffirmHr( winDir.Alloc( RS_WINDIR_SIZE ) );
  93. WsbAffirmStatus( ::GetWindowsDirectory( (WCHAR*)winDir, RS_WINDIR_SIZE ) != 0 );
  94. CString helpFilePath = CString( winDir ) + L"\\help\\" + CString( helpFile );
  95. m_pszHelpFilePath = _tcsdup( helpFilePath );
  96. } WsbCatch( hr );
  97. BOOL retval = CWinApp::InitInstance( );
  98. WsbTraceOut( L"CHsmAdminApp::InitInstance", L"BOOL = <%ls>", WsbBoolAsString( retval ) );
  99. return( retval );
  100. }
  101. int CHsmAdminApp::ExitInstance()
  102. {
  103. WsbTraceIn( L"CHsmAdminApp::ExitInstance", L"" );
  104. _Module.Term();
  105. int retval = CWinApp::ExitInstance();
  106. WsbTraceOut( L"CHsmAdminApp::ExitInstance", L"int = <%ls>", WsbLongAsString( retval ) );
  107. return( retval );
  108. }
  109. void CHsmAdminApp::ParseCommandLine(CCommandLineInfo& rCmdInfo)
  110. {
  111. int argc = 0;
  112. WCHAR **argv;
  113. WsbTraceIn( L"CHsmAdminApp::ParseCommandLine", L"" );
  114. argv = CommandLineToArgvW( GetCommandLineW(), &argc );
  115. if (argc > 0) {
  116. WsbAffirmPointer(argv);
  117. }
  118. for (int i = 1; i < argc; i++)
  119. {
  120. CString pszParam = argv[i];
  121. BOOL bFlag = FALSE;
  122. BOOL bLast = ((i + 1) == argc);
  123. WsbTrace( L"CHsmAdminApp::ParseCommandLine: arg[%d] = \"%s\"\n",
  124. i, (LPCTSTR)pszParam);
  125. if( pszParam[0] == '-' || pszParam[0] == '/' )
  126. {
  127. // remove flag specifier
  128. bFlag = TRUE;
  129. pszParam = pszParam.Mid( 1 );
  130. }
  131. rCmdInfo.ParseParam( pszParam, bFlag, bLast );
  132. }
  133. WsbTraceOut( L"CHsmAdminApp::ParseCommandLine", L"" );
  134. }
  135. /////////////////////////////////////////////////////////////////////////////
  136. // Used to determine whether the DLL can be unloaded by OLE
  137. STDAPI DllCanUnloadNow(void)
  138. {
  139. WsbTraceIn( L"DllCanUnloadNow", L"" );
  140. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  141. HRESULT hr = S_OK;
  142. #ifdef _MERGE_PROXYSTUB
  143. hr = ( S_OK == PrxDllCanUnloadNow() ) ? S_OK : S_FALSE;
  144. #endif
  145. if( S_OK == hr ) {
  146. hr = (AfxDllCanUnloadNow()==S_OK && _Module.GetLockCount()==0) ? S_OK : S_FALSE;
  147. }
  148. WsbTraceOut( L"DllCanUnloadNow", L"hr = <%ls>", WsbHrAsString( hr ) );
  149. return( hr );
  150. }
  151. /////////////////////////////////////////////////////////////////////////////
  152. // Returns a class factory to create an object of the requested type
  153. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
  154. {
  155. WsbTraceIn( L"DllGetClassObject", L"" );
  156. HRESULT hr = CLASS_E_CLASSNOTAVAILABLE;
  157. #ifdef _MERGE_PROXYSTUB
  158. hr = PrxDllGetClassObject( rclsid, riid, ppv );
  159. #endif
  160. if( CLASS_E_CLASSNOTAVAILABLE == hr ) {
  161. hr = _Module.GetClassObject(rclsid, riid, ppv);
  162. }
  163. WsbTraceOut( L"DllGetClassObject", L"hr = <%ls>", WsbHrAsString( hr ) );
  164. return( hr );
  165. }
  166. /////////////////////////////////////////////////////////////////////////////
  167. // DllRegisterServer - Adds entries to the system registry
  168. STDAPI DllRegisterServer(void)
  169. {
  170. WsbTraceIn( L"DllRegisterServer", L"" );
  171. // registers object, typelib and all interfaces in typelib
  172. HRESULT hr = S_OK;
  173. #ifdef _MERGE_PROXYSTUB
  174. hr = PrxDllRegisterServer();
  175. #endif
  176. if( SUCCEEDED( hr ) ) {
  177. hr = CoInitialize( 0 );
  178. if (SUCCEEDED(hr)) {
  179. hr = _Module.RegisterServer( TRUE );
  180. CoUninitialize( );
  181. }
  182. //
  183. // Need to over-ride the rgs name description for multi language support
  184. //
  185. CWsbStringPtr name, nameIndirect, regPath;
  186. HRESULT hrMUI = S_OK;
  187. UINT uLen = 0;
  188. if( SUCCEEDED( name.LoadFromRsc( _Module.m_hInst, IDS_HSMCOM_DESCRIPTION ) ) ) {
  189. const OLECHAR* mmcPath = L"SOFTWARE\\Microsoft\\MMC\\SnapIns\\";
  190. // Create indirect string
  191. hrMUI = nameIndirect.Alloc(MAX_PATH);
  192. if (S_OK == hrMUI) {
  193. uLen = GetSystemDirectory(nameIndirect, MAX_PATH);
  194. if (uLen > MAX_PATH) {
  195. // Try again with larger buffer
  196. hrMUI = nameIndirect.Realloc(uLen);
  197. if (S_OK == hrMUI) {
  198. uLen = GetSystemDirectory(nameIndirect, uLen);
  199. if (0 == uLen) {
  200. hrMUI = S_FALSE;
  201. }
  202. }
  203. }
  204. }
  205. if (S_OK == hrMUI) {
  206. hrMUI = nameIndirect.Prepend(OLESTR("@"));
  207. }
  208. if (S_OK == hrMUI) {
  209. WCHAR resId[64];
  210. wsprintf(resId, OLESTR("\\rsadmin.dll,-%d"), IDS_HSMCOM_DESCRIPTION);
  211. hrMUI = nameIndirect.Append(resId);
  212. }
  213. // Sanpin
  214. regPath = mmcPath;
  215. regPath.Append( WsbGuidAsString( CLSID_HsmAdminDataSnapin ) );
  216. // Set the MUI support value
  217. if (S_OK == hrMUI) {
  218. WsbSetRegistryValueString( 0, regPath, L"NameStringIndirect", nameIndirect );
  219. }
  220. // Set the fallback value
  221. WsbSetRegistryValueString( 0, regPath, L"NameString", name );
  222. // Extension
  223. regPath = mmcPath;
  224. regPath.Append( WsbGuidAsString( CLSID_HsmAdminDataExtension ) );
  225. // Set the MUI support value
  226. if (S_OK == hrMUI) {
  227. WsbSetRegistryValueString( 0, regPath, L"NameStringIndirect", nameIndirect );
  228. }
  229. // Set the fallback value
  230. WsbSetRegistryValueString( 0, regPath, L"NameString", name );
  231. }
  232. }
  233. WsbTraceOut( L"DllRegisterServer", L"hr = <%ls>", WsbHrAsString( hr ) );
  234. return( hr );
  235. }
  236. /////////////////////////////////////////////////////////////////////////////
  237. // DllUnregisterServer - Removes entries from the system registry
  238. STDAPI DllUnregisterServer(void)
  239. {
  240. HRESULT hr;
  241. WsbTraceIn( L"DllUnregisterServer", L"" );
  242. #ifdef _MERGE_PROXYSTUB
  243. PrxDllUnregisterServer();
  244. #endif
  245. hr = CoInitialize( 0 );
  246. if (SUCCEEDED(hr)) {
  247. _Module.UnregisterServer();
  248. CoUninitialize( );
  249. hr = S_OK;
  250. }
  251. WsbTraceOut( L"DllUnregisterServer", L"hr = <%ls>", WsbHrAsString( hr ) );
  252. return( hr );
  253. }