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.

230 lines
5.9 KiB

  1. // W3Key.cpp : Defines the initialization routines for the DLL.
  2. //
  3. #include "stdafx.h"
  4. #include "resource.h"
  5. #include <afxdllx.h>
  6. #include "KeyObjs.h"
  7. #include "wrapmb.h"
  8. #include "CmnKey.h"
  9. #include "W3Key.h"
  10. #include "W3Serv.h"
  11. #include "MDKey.h"
  12. #include "MDServ.h"
  13. #include "kmlsa.h"
  14. #ifdef _DEBUG
  15. #define new DEBUG_NEW
  16. #undef THIS_FILE
  17. static char THIS_FILE[] = __FILE__;
  18. #endif
  19. static AFX_EXTENSION_MODULE W3KeyDLL = { NULL, NULL };
  20. // the tree control
  21. CTreeCtrl* g_pTreeCtrl = NULL;
  22. // the service image index
  23. int g_iServiceImage = 0;
  24. BOOL fInitialized = FALSE;
  25. BOOL fInitMetaWrapper = FALSE;
  26. HINSTANCE g_hInstance = NULL;
  27. void LoadServiceIcon( CTreeCtrl* pTree );
  28. extern "C" int APIENTRY
  29. DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
  30. {
  31. if (dwReason == DLL_PROCESS_ATTACH)
  32. {
  33. TRACE0("W3KEY.DLL Initializing!\n");
  34. // save the instance
  35. g_hInstance = hInstance;
  36. // Extension DLL one-time initialization
  37. AfxInitExtensionModule(W3KeyDLL, hInstance);
  38. // one-time initialization of ip address window
  39. //IPAddrInit( hInstance );
  40. //
  41. // Changed to using common control - RonaldM
  42. //
  43. INITCOMMONCONTROLSEX icex;
  44. icex.dwSize = sizeof(icex);
  45. icex.dwICC = ICC_INTERNET_CLASSES;
  46. InitCommonControlsEx(&icex);
  47. //
  48. // End of RonaldM changes
  49. //
  50. // Insert this DLL into the resource chain
  51. new CDynLinkLibrary(W3KeyDLL);
  52. }
  53. else if (dwReason == DLL_PROCESS_DETACH)
  54. {
  55. TRACE0("W3KEY.DLL Terminating!\n");
  56. if ( fInitMetaWrapper )
  57. FCloseMetabaseWrapper();
  58. if ( fInitialized )
  59. CoUninitialize();
  60. }
  61. return 1; // ok
  62. }
  63. //=======================================================
  64. // the main routine called by the keyring application
  65. BOOL _cdecl LoadService( CMachine* pMachine )
  66. {
  67. BOOL fAnswer = FALSE;
  68. HRESULT hRes;
  69. // initialize the ole stuff if necessary
  70. if ( !fInitialized )
  71. {
  72. //DebugBreak();
  73. hRes = CoInitialize( NULL );
  74. }
  75. ASSERT( pMachine );
  76. if ( !pMachine ) return FALSE;
  77. // specify the resources to use
  78. HINSTANCE hOldRes = AfxGetResourceHandle();
  79. AfxSetResourceHandle( g_hInstance );
  80. // if this is the first time through, initialize the tree control stuff
  81. if ( !g_pTreeCtrl )
  82. {
  83. // get the tree control
  84. g_pTreeCtrl = pMachine->PGetTreeCtrl();
  85. ASSERT( g_pTreeCtrl );
  86. // since we are adding a service icon, we need to do that now too
  87. LoadServiceIcon( g_pTreeCtrl );
  88. }
  89. // see if we can access this machine
  90. // get the wide name of the machine we intend to target
  91. CString szName;
  92. DWORD err;
  93. pMachine->GetMachineName( szName );
  94. // allocate the cache for the machine name
  95. WCHAR* pszwMachineName = new WCHAR[MAX_PATH];
  96. if ( !pszwMachineName ) return FALSE;
  97. // unicodize the name
  98. MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, szName, -1, pszwMachineName, MAX_PATH );
  99. // drat the local name for the metabase should be null, while it should be an empty string
  100. // for LSA. Make a temp metabase name as appropriate
  101. WCHAR* pszwMeta;
  102. if ( szName.IsEmpty() )
  103. pszwMeta = NULL;
  104. else
  105. pszwMeta = pszwMachineName;
  106. // first we see if we can access the metabase at the proscribed location.
  107. if ( FInitMetabaseWrapper(pszwMeta) )
  108. {
  109. fInitMetaWrapper = TRUE;
  110. // hey--- the ole interface is there. We can use the metabase
  111. // now the whole purpose here is to setup a service object on the
  112. // machine object. So, create the service object
  113. try {
  114. CMDKeyService* pKServ = new CMDKeyService;
  115. // it needs to know the target machine
  116. pKServ->SetMachineName( pszwMeta );
  117. // add it to the machine
  118. pKServ->FAddToTree( pMachine );
  119. // load the keys
  120. pKServ->LoadKeys( pMachine );
  121. }
  122. catch( CException e )
  123. {
  124. goto cleanup;
  125. }
  126. // now close the metabase again. We will open it when we need it
  127. FCloseMetabaseWrapper();
  128. // success!
  129. fAnswer = TRUE;
  130. }
  131. else
  132. {
  133. // try to open the LSA policy. if this fails, then there is no server on the
  134. // target machine - we return a false
  135. HANDLE hPolicy = HOpenLSAPolicy( pszwMachineName, &err );
  136. // if we did not get the policy, then fail
  137. if ( !hPolicy )
  138. goto cleanup;
  139. // we did get the policy, so close it
  140. FCloseLSAPolicy( hPolicy, &err );
  141. // now the whole purpose here is to setup a service object on the
  142. // machine object. So, create the service object
  143. try {
  144. CW3KeyService* pKServ = new CW3KeyService;
  145. // add it to the machine
  146. pKServ->FAddToTree( pMachine );
  147. // load the keys
  148. pKServ->LoadKeys( pMachine );
  149. }
  150. catch( CException e )
  151. {
  152. goto cleanup;
  153. }
  154. // success! - well, LSA success anyway
  155. fAnswer = TRUE;
  156. }
  157. // clean up
  158. cleanup:
  159. delete pszwMachineName;
  160. // restore the resources
  161. AfxSetResourceHandle( hOldRes );
  162. // return successfully
  163. return fAnswer;
  164. }
  165. //-------------------------------------------------------------------
  166. void LoadServiceIcon( CTreeCtrl* pTree )
  167. {
  168. ASSERT( pTree );
  169. // get the image list from the tree
  170. CImageList* pImageList = pTree->GetImageList(TVSIL_NORMAL);
  171. ASSERT( pImageList );
  172. // load the service bitmap
  173. CBitmap bmpService;
  174. if ( !bmpService.LoadBitmap( IDB_SERVICE_BMP ) )
  175. {
  176. ASSERT( FALSE );
  177. return;
  178. }
  179. // connect the bitmap to the image list
  180. g_iServiceImage = pImageList->Add( &bmpService, 0x00FF00FF );
  181. }