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.

312 lines
9.3 KiB

  1. // This file is the metadata version of the key storage serverice object for the w3 server.
  2. // it knows nothing about the LSA storage and only interacts with the metabase. Likewise,
  3. // it does not convert old keyset.exe keys into a newer format. Any old LSA keys should have
  4. // automatically converted to the new metabase format by the setup utility.
  5. // file created 4/1/1997 by BoydM
  6. #include "stdafx.h"
  7. #include "KeyObjs.h"
  8. #include "iiscnfg.h"
  9. #include "wrapmb.h"
  10. #include "cmnkey.h"
  11. #include "mdkey.h"
  12. #include "mdserv.h"
  13. #include "resource.h"
  14. // the service image index
  15. extern int g_iServiceImage;
  16. extern HINSTANCE g_hInstance;
  17. BOOL g_fUsingMetabase = FALSE;
  18. #define MAX_LEN (METADATA_MAX_NAME_LEN * sizeof(WCHAR))
  19. //--------------------------------------------------------
  20. CMDKeyService::CMDKeyService():
  21. m_pszwMachineName(NULL)
  22. {
  23. // specify the resources to use
  24. HINSTANCE hOldRes = AfxGetResourceHandle();
  25. AfxSetResourceHandle( g_hInstance );
  26. // set the icon id
  27. m_iImage = (WORD)g_iServiceImage;
  28. // load the service name
  29. m_szItemName.LoadString( IDS_SERV_NAME );
  30. // yes, we are using the metabase
  31. g_fUsingMetabase = TRUE;
  32. // restore the resources
  33. AfxSetResourceHandle( hOldRes );
  34. }
  35. //--------------------------------------------------------
  36. CMDKeyService::~CMDKeyService()
  37. {
  38. if ( m_pszwMachineName )
  39. delete m_pszwMachineName;
  40. }
  41. //--------------------------------------------------------
  42. void CMDKeyService::SetMachineName( WCHAR* pszw )
  43. {
  44. if ( pszw )
  45. {
  46. m_pszwMachineName = new WCHAR[wcslen(pszw)+1];
  47. wcscpy( m_pszwMachineName, pszw );
  48. }
  49. else
  50. {
  51. m_pszwMachineName = NULL;
  52. }
  53. }
  54. //--------------------------------------------------------
  55. void CMDKeyService::LoadKeys( CMachine* pMachine )
  56. {
  57. CString szKeyObject;
  58. DWORD index = 0;
  59. CString szKeyBase;
  60. CString sz;
  61. // keep a local list of the added keys in some attempt to keep this simpler
  62. CTypedPtrArray<CObArray, CMDKey*> rgbKeys;
  63. CString szIdent;
  64. BOOL fLoadTheKey;
  65. DWORD iKey;
  66. DWORD nKey;
  67. // create the metabase wrapper object
  68. CWrapMetaBase mbWrap;
  69. if ( !mbWrap.FInit() )
  70. {
  71. ASSERT( FALSE );
  72. goto cleanup; // can't get to metabase - no keys
  73. }
  74. // build the key name
  75. szKeyBase = SZ_META_BASE;
  76. szKeyBase += SZ_META_KEYBASE;
  77. // open the base metabase object that contains all the keys
  78. // if we were unable to open the object - then there aren't any keys
  79. if ( !mbWrap.Open( szKeyBase, METADATA_PERMISSION_READ ) )
  80. goto cleanup; // can't open metabase
  81. // read in all the keys and add them to the main list
  82. while ( mbWrap.EnumObjects("", sz.GetBuffer(MAX_LEN), MAX_LEN, index) )
  83. {
  84. sz.ReleaseBuffer();
  85. // make a new key object
  86. CMDKey* pKey = new CMDKey(this);
  87. if ( !pKey ) continue;
  88. // here's the deal. If this key is a member of a group of IP addresses that actually
  89. // use the same key, then we need to figure out the groups. We don't want to actually
  90. // load all the keys. - Just the first from each group. First we get the unique
  91. // identifier string for the key (the serial number), which should be cached in the
  92. // metabase for us.
  93. fLoadTheKey = FALSE;
  94. if ( pKey->FGetIdentString(&mbWrap, sz.GetBuffer(MAX_LEN), szIdent) )
  95. {
  96. sz.ReleaseBuffer();
  97. // we did get an ident string. Now we need to look through the list of keys we
  98. // have already loaded and see if there is a parental match for this one. If there
  99. // is, we add this key's metaname to the list of metanames for the parent and do NOT
  100. // load the rest of the data for the key - because it is already there for the parent.
  101. nKey = rgbKeys.GetSize();
  102. for ( iKey = 0; iKey < nKey; iKey++ )
  103. {
  104. CString szTest = rgbKeys[iKey]->m_szIdent;
  105. if ( szIdent == szTest )
  106. {
  107. // we found a sub-member! - add it to the parent's list
  108. rgbKeys[iKey]->AddBinding( sz );
  109. // get out of this sub-loop
  110. goto loadkey;
  111. }
  112. }
  113. // we did not find a parent for the key. Load it
  114. fLoadTheKey = TRUE;
  115. }
  116. else
  117. {
  118. sz.ReleaseBuffer();
  119. // if we did not get the ident string, then this is an incomplete key and
  120. // won't have multiple bindings anyway. Load it
  121. fLoadTheKey = TRUE;
  122. }
  123. // fill in the parts of the key by reading in the relevant info from the metabase
  124. // add the key to the tree - if we successfully read it in
  125. loadkey:
  126. if ( fLoadTheKey && pKey->FLoadKey(&mbWrap, (LPSTR)(LPCSTR)sz) )
  127. {
  128. // add it to visible tree view
  129. pKey->FAddToTree( this );
  130. // add it to the cached list for easier access as we load the keys
  131. rgbKeys.Add(pKey);
  132. }
  133. else
  134. delete pKey;
  135. // increment to the next object
  136. index++;
  137. }
  138. sz.ReleaseBuffer();
  139. // close the metabase on the target machine
  140. mbWrap.Close();
  141. // cleanup the metabase connection
  142. cleanup:
  143. ;
  144. }
  145. //--------------------------------------------------------
  146. BOOL CMDKeyService::FCommitChangesNow( )
  147. {
  148. BOOL fAnswer = FALSE;
  149. DWORD iKey = 1;
  150. CMDKey* pKey;
  151. CString szKeyBase;
  152. CStringArray rgbSZ;
  153. CString szEnum;
  154. DWORD iObject = 0;
  155. DWORD iString;
  156. DWORD nStrings;
  157. rgbSZ.SetSize( 0, 8 );
  158. BOOL f;
  159. // get the metabase going
  160. if ( !FInitMetabaseWrapper(m_pszwMachineName) )
  161. return FALSE;
  162. // create the metabase wrapper object
  163. CWrapMetaBase mbWrap;
  164. if ( !mbWrap.FInit() )
  165. {
  166. ASSERT( FALSE );
  167. goto cleanup; // can't get to metabase - no keys
  168. }
  169. // build the key name
  170. szKeyBase = SZ_META_BASE;
  171. szKeyBase += SZ_META_KEYBASE;
  172. // open the base metabase object that contains all the keys
  173. // if we were unable to open the object - then there aren't any keys
  174. if ( !mbWrap.Open( szKeyBase, METADATA_PERMISSION_WRITE | METADATA_PERMISSION_READ ) )
  175. {
  176. // the base object for the keys doesn't exist. This means
  177. // we have to create it first, then try to open it
  178. // open up the w3svc base, which is where the key base lives
  179. if ( !mbWrap.Open( SZ_META_BASE, METADATA_PERMISSION_WRITE ) )
  180. goto cleanup; // whoa! this shouldn't happen
  181. // create the key - if it doesn't work, croak
  182. if ( !mbWrap.AddObject(SZ_META_KEYBASE) )
  183. {
  184. mbWrap.Close();
  185. goto cleanup;
  186. }
  187. mbWrap.Close();
  188. // NOW try to open the base key - and it better work this time
  189. if ( !mbWrap.Open( szKeyBase, METADATA_PERMISSION_WRITE | METADATA_PERMISSION_READ ) )
  190. goto cleanup;
  191. }
  192. // update each of the keys
  193. pKey = GetFirstMDKey();
  194. while( pKey )
  195. {
  196. // tell the key to write itself out
  197. pKey->FWriteKey( &mbWrap, iKey, &rgbSZ );
  198. // get the next key
  199. pKey = GetNextMDKey( pKey );
  200. iKey++;
  201. }
  202. // scan the metabase looking for objects there were not just saved. If it hasn't
  203. // been just saved, or marked saved, we get rid of it.
  204. // read in all the keys and add them to the main list
  205. nStrings = rgbSZ.GetSize();
  206. while ( mbWrap.EnumObjects("", szEnum.GetBuffer(MAX_LEN), MAX_LEN, iObject) )
  207. {
  208. szEnum.ReleaseBuffer();
  209. // is it in the saved list?
  210. for ( iString = 0; iString < nStrings; iString++ )
  211. {
  212. if ( rgbSZ[iString] == szEnum )
  213. {
  214. // increment to the next object
  215. iObject++;
  216. goto nextObject;
  217. }
  218. }
  219. // if it is not in the list, delete the object
  220. f = mbWrap.DeleteObject( szEnum );
  221. nextObject:
  222. ;
  223. }
  224. szEnum.ReleaseBuffer();
  225. // close the metabase on the target machine
  226. mbWrap.Close();
  227. // write out the changes we made to the metabase
  228. mbWrap.Save();
  229. // string memory in a CStringArray object is automatically cleanup up
  230. // when the main object is deleted.
  231. // clear the dirty flag
  232. SetDirty( FALSE );
  233. fAnswer = TRUE;
  234. // cleanup the metabase connection
  235. cleanup:
  236. // now close the metabase again. We will open it when we need it
  237. FCloseMetabaseWrapper();
  238. // return the answer
  239. return fAnswer;
  240. }
  241. //--------------------------------------------------------
  242. BOOL CMDKeyService::FIsBindingInUse( CString szBinding )
  243. {
  244. CMDKey* pKey;
  245. // get the first key in the service list
  246. pKey = GetFirstMDKey();
  247. // loop the keys, testing each in turn
  248. while ( pKey )
  249. {
  250. // test it
  251. if ( pKey->FContainsBinding( szBinding ) )
  252. return TRUE;
  253. // get the next key in the list
  254. pKey = GetNextMDKey( pKey );
  255. }
  256. // nope. Didn't find it.
  257. return FALSE;
  258. }