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.

198 lines
6.8 KiB

  1. // metatool.cpp : implementation file
  2. //
  3. // some common tools used for "smart" writing to the metabase
  4. #include "stdafx.h"
  5. #include <iadmw.h>
  6. #define _COMIMPORT
  7. #include <common.h>
  8. #include <idlg.h>
  9. #include "wrapmb.h"
  10. #include "metatool.h"
  11. //----------------------------------------------------------------
  12. // open the metabase with an option to create the directory if it doesn't
  13. // exist. It would be nice to move this into wrapmb, but that is too big
  14. // a change for now. Maybe we can do that later.
  15. BOOL OpenAndCreate( CWrapMetaBase* pmb, LPCTSTR pszTarget, DWORD perm, BOOL fCreate )
  16. {
  17. BOOL f;
  18. CString szTarget = pszTarget;
  19. // start by just trying to open it. easy easy.
  20. if ( pmb->Open(szTarget, perm) )
  21. return TRUE;
  22. // if requested, try to create the key if it doesn't exist
  23. if ( fCreate )
  24. {
  25. // find the nearest openable parent directory and open it
  26. CString szPartial;
  27. CString szBase = szTarget;
  28. do
  29. {
  30. szBase = szBase.Left( szBase.ReverseFind(_T('/')) );
  31. szPartial = szTarget.Right( szTarget.GetLength() - szBase.GetLength() - 1 );
  32. f = pmb->Open( szBase, METADATA_PERMISSION_WRITE | perm );
  33. } while (!f && !szBase.IsEmpty());
  34. // if all that failed, fail
  35. if ( !f ) return FALSE;
  36. // create the key that we really want
  37. f = pmb->AddObject( szPartial );
  38. pmb->Close();
  39. // if all that failed, fail
  40. if ( !f ) return FALSE;
  41. // try again
  42. if ( pmb->Open(szTarget, perm) )
  43. return TRUE;
  44. }
  45. // total washout
  46. return FALSE;
  47. }
  48. //----------------------------------------------------------------
  49. // starting at the root, check for values set on sub-keys that may need to be overridden
  50. // and propmt the user for what to do
  51. void CheckInheritence( LPCTSTR pszServer, LPCTSTR pszInheritRoot, DWORD idData )
  52. {
  53. CComAuthInfo auth(pszServer);
  54. CInheritanceDlg dlgInherit(
  55. idData,
  56. FROM_WRITE_PROPERTY,
  57. &auth,
  58. pszInheritRoot
  59. );
  60. // if it worked, then run the dialog
  61. if ( !dlgInherit.IsEmpty() )
  62. dlgInherit.DoModal();
  63. }
  64. // notice that the dwords and generic blobs are handled seperately even though
  65. // we count route the dwords through the blob mechanisms. This is done for two
  66. // reasone. 1) Handling dwords is much more efficient than handling blobs.
  67. // and 2) Most of the values are dwords.
  68. //----------------------------------------------------------------
  69. // opens the metabase, writes out the value, then uses the inheritence
  70. // checking functionality from the iisui.dll to check for the inherited
  71. // properties and propt the user for what to do
  72. BOOL SetMetaDword(IMSAdminBase* pMB, LPCTSTR pszServer, LPCTSTR pszMetaRoot, LPCTSTR pszSub, DWORD idData, DWORD iType, DWORD dwValue, BOOL fCheckInheritence)
  73. {
  74. BOOL fAnswer = FALSE;
  75. BOOL fChanged = TRUE;
  76. DWORD dword;
  77. CWrapMetaBase mbWrap;
  78. if ( !mbWrap.FInit(pMB) ) return FALSE;
  79. // open the target
  80. if ( OpenAndCreate( &mbWrap, pszMetaRoot,
  81. METADATA_PERMISSION_WRITE | METADATA_PERMISSION_READ, TRUE ) )
  82. {
  83. // attempt to get the current value - no inheritence
  84. if ( mbWrap.GetDword(pszSub, idData, iType, &dword) )
  85. {
  86. // set the changed flag
  87. fChanged = (dwValue != dword);
  88. }
  89. // save it out, if it changed or is not there
  90. if ( fChanged )
  91. fAnswer = mbWrap.SetDword( pszSub, idData, iType, dwValue );
  92. // close the metabase
  93. mbWrap.Close();
  94. }
  95. else
  96. fChanged = FALSE;
  97. // set up and run the inheritence checking dialog
  98. if ( fCheckInheritence && fChanged )
  99. {
  100. CString szInheritRoot = pszMetaRoot;
  101. szInheritRoot += pszSub;
  102. CheckInheritence( pszServer, szInheritRoot, idData );
  103. }
  104. return fAnswer;
  105. }
  106. //----------------------------------------------------------------
  107. // assumes that the metabase is actually open to the parent of the one we are interested
  108. // and that the real target name is passed into szSub
  109. BOOL SetMetaData(IMSAdminBase* pMB, LPCTSTR pszServer, LPCTSTR pszMetaRoot, LPCTSTR pszSub, DWORD idData, DWORD iType, DWORD iDataType, PVOID pData, DWORD cbData, BOOL fCheckInheritence )
  110. {
  111. BOOL fAnswer = FALSE;
  112. BOOL fChanged = TRUE;
  113. DWORD cbTestData;
  114. CWrapMetaBase mbWrap;
  115. if ( !mbWrap.FInit(pMB) ) return FALSE;
  116. // open the target
  117. if ( OpenAndCreate( &mbWrap, pszMetaRoot,
  118. METADATA_PERMISSION_WRITE | METADATA_PERMISSION_READ, TRUE ) )
  119. {
  120. // attempt to get the current value - no inheritence
  121. PVOID pTestData = mbWrap.GetData( pszSub, idData, iType,
  122. iDataType, &cbTestData );
  123. if ( pTestData )
  124. {
  125. // set the changed flag
  126. if ( cbData == cbTestData )
  127. {
  128. fChanged = (memcmp(pData, pTestData, cbData) != 0);
  129. }
  130. mbWrap.FreeWrapData( pTestData );
  131. }
  132. // save it out, if it changed or is not there
  133. if ( fChanged )
  134. fAnswer = mbWrap.SetData( pszSub, idData, iType, iDataType, pData, cbData );
  135. // close the metabase
  136. mbWrap.Close();
  137. }
  138. else
  139. fChanged = FALSE;
  140. // set up and run the inheritence checking dialog
  141. if ( fCheckInheritence && fChanged )
  142. {
  143. CString szInheritRoot = pszMetaRoot;
  144. szInheritRoot += pszSub;
  145. CheckInheritence( pszServer, szInheritRoot, idData );
  146. }
  147. return fAnswer;
  148. }
  149. //----------------------------------------------------------------
  150. // assumes that the metabase is actually open to the parent of the one we are interested
  151. // and that the real target name is passed into szSub
  152. BOOL SetMetaString(IMSAdminBase* pMB, LPCTSTR pszServer, LPCTSTR pszMetaRoot, LPCTSTR pszSub, DWORD idData, DWORD iType, CString sz, BOOL fCheckInheritence)
  153. {
  154. return SetMetaData(pMB, pszServer, pszMetaRoot, pszSub, idData,
  155. iType, STRING_METADATA, (LPTSTR)(LPCTSTR)sz,
  156. (sz.GetLength()+1)*sizeof(TCHAR), fCheckInheritence );
  157. }
  158. //----------------------------------------------------------------
  159. // assumes that the metabase is actually open to the parent of the one we are interested
  160. // and that the real target name is passed into szSub
  161. // the cchmsz is the total count of characters in the multi string including the nulls
  162. BOOL SetMetaMultiSz(IMSAdminBase* pMB, LPCTSTR pszServer, LPCTSTR pszMetaRoot, LPCTSTR pszSub, DWORD idData, DWORD iType, PVOID pData, DWORD cchmsz, BOOL fCheckInheritence )
  163. {
  164. return SetMetaData(pMB, pszServer, pszMetaRoot, pszSub, idData,
  165. iType, MULTISZ_METADATA, pData, (cchmsz+1)*sizeof(TCHAR), fCheckInheritence );
  166. }