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.

432 lines
15 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1997 - 1999
  6. //
  7. // File: newobj.h
  8. //
  9. //--------------------------------------------------------------------------
  10. /////////////////////////////////////////////////////////////////////
  11. // newobj.h
  12. //
  13. // This file contains function prototypes to create new ADs object.
  14. //
  15. // HISTORY
  16. // 20-Aug-97 Dan Morin Creation.
  17. //
  18. /////////////////////////////////////////////////////////////////////
  19. #ifndef __NEWOBJ_H_INCLUDED__
  20. #define __NEWOBJ_H_INCLUDED__
  21. #ifndef __DSSNAP_H__
  22. #include "dssnap.h" // CDSComponentData
  23. #endif
  24. #ifndef __GSZ_H_INCLUDED__
  25. #include "gsz.h"
  26. #endif
  27. #include "copyobj.h"
  28. /////////////////////////////////////////////////////////////////////
  29. // typedef PFn_HrCreateADsObject()
  30. //
  31. // Interface of the "create routine" to create a new ADs object.
  32. //
  33. // Typically the routine brings a dialog so the user can enter
  34. // information relevant to create the object. The routine
  35. // then validates the data and create the object. If the data
  36. // is invalid and/or the object creation fails, the routine should
  37. // display an error message and return S_FALSE.
  38. //
  39. // RETURN
  40. // S_OK - The object was created and persisted successfully.
  41. // S_FALSE - The user hit the "Cancel" button or object creation failed.
  42. // Return E_* - A very serious error occured.
  43. //
  44. typedef HRESULT (* PFn_HrCreateADsObject)(INOUT class CNewADsObjectCreateInfo * pNewADsObjectCreateInfo);
  45. BOOL FindHandlerFunction(/*IN*/ LPCWSTR lpszObjectClass,
  46. /*OUT*/ PFn_HrCreateADsObject* ppfFunc,
  47. /*OUT*/ void** ppVoid);
  48. HRESULT HrCreateFixedNameHelper(/*IN*/ LPCWSTR lpszObjectClass,
  49. /*IN*/ LPCWSTR lpszAttrString, // typically "cn="
  50. /*IN*/ IADsContainer* pIADsContainer);
  51. /////////////////////////////////////////////////////////////////////
  52. // class CNewADsObjectCreateInfo
  53. //
  54. // Class to store temporary data to create a new ADs object.
  55. //
  56. // CONTROL FLOW
  57. // 1. Construct CNewADsObjectCreateInfo object.
  58. // 2. Use SetContainerInfo() to set additional pointers.
  59. // 3. Use HrDoModal() to invoke the dialog.
  60. // 3.1. Perform a lookup to match the objectclass to the best "create routine".
  61. // 3.2. The "create routine" will validate the data, use SetName() to set object name
  62. // and HrAddVariant*() and store each attribute into a list of variants.
  63. // 3.3. The "create routine" will use HrSetInfo() to create and persist object.
  64. // 3.3.1. If object creation fails, routine HrSetInfo() will display an error message.
  65. // 3.4. Routine HrDoModal() return S_OK if object was created successfully.
  66. // 4. Object CNewADsObjectCreateInfo returns pIADs pointer to caller. Caller
  67. // must pIADs->Release() when no longer need reference to object.
  68. //
  69. // REMARKS
  70. // The core of "create routine" is typically encapsulated inside a dialog object.
  71. // In fact, the object creation which uses HrSetInfo() is mostly done inside the OnOK() handler.
  72. //
  73. // HISTORY
  74. // 20-Aug-97 Dan Morin Creation.
  75. //
  76. ///////////////////////////////////////////////////////////////////////////
  77. // CNewADsObjectCreateInfo
  78. class CNewADsObjectCreateInfo
  79. {
  80. public:
  81. IADsContainer * m_pIADsContainer; // IN: Container to create object
  82. LPCWSTR m_pszObjectClass; // IN: Class of object to create. eg: "user", "computer", "volume".
  83. CString m_strDefaultObjectName;
  84. private:
  85. MyBasePathsInfo* m_pBasePathsInfo;
  86. CDSClassCacheItemBase* m_pDsCacheItem; // IN: Pointer to cache entry
  87. CDSComponentData* m_pCD;
  88. IADs* m_pIADs; // OUT: Pointer to IADs interface. Caller must call pIADs->Release() when done.
  89. HWND m_hWnd; // IN: MMC console main window, for modal dialogs
  90. CString m_strObjectName; // OUT: Name of object. eg: "danmorin", "mycomputer", "myvollume"
  91. CString m_strADsName; // OUT: OPTIONAL: Concatenated ADs name. eg: "cn=danmorin"
  92. CString m_szCacheNamingAttribute; // typically "cn" or "ou"
  93. CString m_szContainerCanonicalName; // for display purposes cached after first call
  94. // to GetContainerCanonicalName()
  95. CCopyObjectHandlerBase* m_pCopyHandler;
  96. private:
  97. class CVariantInfo
  98. {
  99. public:
  100. CVariantInfo()
  101. {
  102. m_bPostCommit = FALSE;
  103. }
  104. ~CVariantInfo() { }
  105. const CVariantInfo& operator=(CVariantInfo& src)
  106. {
  107. m_bPostCommit = src.m_bPostCommit;
  108. m_szAttrName = src.m_szAttrName;
  109. HRESULT hr = ::VariantCopy(&m_varAttrValue, &src.m_varAttrValue);
  110. ASSERT(SUCCEEDED(hr));
  111. return *this;
  112. }
  113. HRESULT Write(IADs* pIADs, BOOL bPostCommit)
  114. {
  115. HRESULT hr = S_OK;
  116. if (bPostCommit == m_bPostCommit)
  117. {
  118. CComBSTR bstrAttrName = m_szAttrName;
  119. hr = pIADs->Put(IN bstrAttrName, IN m_varAttrValue);
  120. }
  121. return hr;
  122. }
  123. BOOL m_bPostCommit; // TRUE if the attribute has to be written during post commit
  124. CString m_szAttrName; // Name of the attribute
  125. CComVariant m_varAttrValue; // Value of the attribute (stored in a variant)
  126. };
  127. class CVariantInfoList : public CList<CVariantInfo*, CVariantInfo*>
  128. {
  129. public:
  130. ~CVariantInfoList() { Flush();}
  131. void Flush()
  132. {
  133. while (!IsEmpty())
  134. delete RemoveHead();
  135. }
  136. void Initialize(CVariantInfoList* pList)
  137. {
  138. Flush();
  139. for (POSITION pos = pList->GetHeadPosition(); pos != NULL; )
  140. {
  141. CVariantInfo* pCurrInfo = pList->GetNext(pos);
  142. CVariantInfo* pNewInfo = new CVariantInfo;
  143. *pNewInfo = *pCurrInfo;
  144. AddTail(pNewInfo);
  145. }
  146. }
  147. HRESULT Write(IADs* pIADs, BOOL bPostCommit)
  148. {
  149. ASSERT(pIADs != NULL);
  150. HRESULT hr = S_OK;
  151. for (POSITION pos = GetHeadPosition(); pos != NULL; )
  152. {
  153. CVariantInfo* pVariantInfo = GetNext(pos);
  154. hr = pVariantInfo->Write(pIADs, bPostCommit);
  155. if (FAILED(hr))
  156. break;
  157. }
  158. return hr;
  159. }
  160. };
  161. CVariantInfoList m_defaultVariantList;
  162. BOOL m_bPostCommit; // state to manage default variant list
  163. PVOID m_pvCreationParameter; // IN: Some of the HrCreate functions need this
  164. PFn_HrCreateADsObject m_pfnCreateObject; // function pointer for object creation
  165. DSCLASSCREATIONINFO* m_pCreateInfo; // loaded from the DS display specifiers
  166. public:
  167. // Construction/Initialization
  168. CNewADsObjectCreateInfo(MyBasePathsInfo* pBasePathsInfo, LPCTSTR pszObjectClass);
  169. ~CNewADsObjectCreateInfo();
  170. void SetContainerInfo(IN IADsContainer * pIADsContainer, IN CDSClassCacheItemBase* pDsCacheItem,
  171. IN CDSComponentData* pCD,
  172. IN LPCWSTR lpszAttrString = NULL);
  173. // copy operations
  174. HRESULT SetCopyInfo(IADs* pIADsCopyFrom);
  175. HRESULT SetCopyInfo(LPCWSTR lpszCopyFromLDAPPath);
  176. IADs* GetCopyFromObject()
  177. {
  178. if (m_pCopyHandler == NULL)
  179. return NULL;
  180. return m_pCopyHandler->GetCopyFrom();
  181. }
  182. CCopyObjectHandlerBase* GetCopyHandler() { return m_pCopyHandler;}
  183. CMandatoryADsAttributeList* GetMandatoryAttributeListFromCacheItem()
  184. {
  185. ASSERT(m_pDsCacheItem != NULL);
  186. ASSERT(m_pCD != NULL);
  187. if ((m_pDsCacheItem == NULL) || (m_pCD == NULL))
  188. return NULL;
  189. return m_pDsCacheItem->GetMandatoryAttributeList(m_pCD);
  190. }
  191. BOOL IsStandaloneUI() { return (m_pDsCacheItem == NULL) || (m_pCD == NULL);}
  192. BOOL IsContainer()
  193. {
  194. if (m_pDsCacheItem == NULL)
  195. {
  196. // we do not know, so the default icon might not be a container...
  197. return FALSE;
  198. }
  199. return m_pDsCacheItem->IsContainer();
  200. }
  201. HRESULT HrLoadCreationInfo();
  202. HRESULT HrDoModal(HWND hWnd);
  203. HRESULT HrCreateNew(LPCWSTR pszName, BOOL bSilentError = FALSE, BOOL bAllowCopy = TRUE);
  204. HRESULT HrSetInfo(BOOL fSilentError = FALSE);
  205. HRESULT HrDeleteFromBackend();
  206. IADs* PGetIADsPtr() { return m_pIADs;}
  207. void SetIADsPtr(IADs* pIADs)
  208. {
  209. if (m_pIADs != NULL)
  210. m_pIADs->Release();
  211. m_pIADs = pIADs;
  212. if (m_pIADs != NULL)
  213. m_pIADs->AddRef();
  214. }
  215. const PVOID QueryCreationParameter() { return m_pvCreationParameter; }
  216. void SetCreationParameter(PVOID pVoid) { m_pvCreationParameter = pVoid; }
  217. HWND GetParentHwnd() { return m_hWnd;}
  218. MyBasePathsInfo* GetBasePathsInfo() { return m_pBasePathsInfo;}
  219. public:
  220. void SetPostCommit(BOOL bPostCommit)
  221. {
  222. m_bPostCommit = bPostCommit;
  223. }
  224. HRESULT HrAddDefaultAttributes()
  225. {
  226. ASSERT(m_pIADs != NULL);
  227. return m_defaultVariantList.Write(m_pIADs, m_bPostCommit);
  228. }
  229. LPCWSTR GetName() { return m_strObjectName; }
  230. LPCWSTR GetContainerCanonicalName();
  231. HRESULT HrAddVariantFromName(BSTR bstrAttrName);
  232. // The following are wrappers to easily create common variant types
  233. HRESULT HrAddVariantBstr(BSTR bstrAttrName, LPCWSTR pszAttrValue, BOOL bDefaultList = FALSE);
  234. HRESULT HrAddVariantBstrIfNotEmpty(BSTR bstrAttrName, LPCWSTR pszAttrValue, BOOL bDefaultList = FALSE);
  235. HRESULT HrAddVariantLong(BSTR bstrAttrName, LONG lAttrValue, BOOL bDefaultList = FALSE);
  236. HRESULT HrAddVariantBoolean(BSTR bstrAttrName, BOOL fAttrValue, BOOL bDefaultList = FALSE);
  237. HRESULT HrAddVariantCopyVar(BSTR bstrAttrName, VARIANT varSrc, BOOL bDefaultList = FALSE);
  238. HRESULT HrGetAttributeVariant(BSTR bstrAttrName, OUT VARIANT * pvarData);
  239. public:
  240. DSCLASSCREATIONINFO* GetCreateInfo()
  241. {
  242. ASSERT(m_pCreateInfo != NULL);
  243. return m_pCreateInfo;
  244. }
  245. private:
  246. // Private routines
  247. HRESULT _RemoveAttribute(BSTR bstrAttrName, BOOL bDefaultList);
  248. // helpers for the default list
  249. VARIANT* _PAllocateVariantInfo(BSTR bstrAttrName);
  250. HRESULT _RemoveVariantInfo(BSTR bstrAttrName);
  251. // helpers for the ADSI object
  252. HRESULT _HrSetAttributeVariant(BSTR bstrAttrName, IN VARIANT * pvarData);
  253. HRESULT _HrClearAttribute(BSTR bstrAttrName);
  254. }; // CNewADsObjectCreateInfo
  255. /////////////////////////////////////////////////////////////////////
  256. //
  257. // Prototype of the "create routines" to create new ADs objects.
  258. //
  259. // All those routines have the same interface as PFn_HrCreateADsObject().
  260. //
  261. HRESULT HrCreateADsUser(INOUT CNewADsObjectCreateInfo * pNewADsObjectCreateInfo);
  262. HRESULT HrCreateADsVolume(INOUT CNewADsObjectCreateInfo * pNewADsObjectCreateInfo);
  263. HRESULT HrCreateADsComputer(INOUT CNewADsObjectCreateInfo * pNewADsObjectCreateInfo);
  264. HRESULT HrCreateADsPrintQueue(INOUT CNewADsObjectCreateInfo * pNewADsObjectCreateInfo);
  265. HRESULT HrCreateADsGroup(INOUT CNewADsObjectCreateInfo * pNewADsObjectCreateInfo);
  266. HRESULT HrCreateADsContact(INOUT CNewADsObjectCreateInfo * pNewADsObjectCreateInfo);
  267. HRESULT HrCreateADsNtDsConnection(INOUT CNewADsObjectCreateInfo * pNewADsObjectCreateInfo);
  268. // The CreationParameter for HrCreateADsFixedNamemust be an LPCWSTR for the name of the object.
  269. // The user does not need to enter any further parameters.
  270. HRESULT HrCreateADsFixedName(INOUT CNewADsObjectCreateInfo * pNewADsObjectCreateInfo);
  271. #ifdef FRS_CREATE
  272. HRESULT HrCreateADsNtFrsMember(INOUT CNewADsObjectCreateInfo * pNewADsObjectCreateInfo);
  273. HRESULT HrCreateADsNtFrsSubscriber(INOUT CNewADsObjectCreateInfo * pNewADsObjectCreateInfo);
  274. HRESULT CreateADsNtFrsSubscriptions(CNewADsObjectCreateInfo * pNewADsObjectCreateInfo);
  275. #endif // FRS_CREATE
  276. HRESULT HrCreateADsServer(INOUT CNewADsObjectCreateInfo * pNewADsObjectCreateInfo);
  277. HRESULT HrCreateADsSubnet(INOUT CNewADsObjectCreateInfo * pNewADsObjectCreateInfo);
  278. HRESULT HrCreateADsSite(INOUT CNewADsObjectCreateInfo * pNewADsObjectCreateInfo);
  279. HRESULT HrCreateADsSiteLink(INOUT CNewADsObjectCreateInfo * pNewADsObjectCreateInfo);
  280. HRESULT HrCreateADsSiteLinkBridge(INOUT CNewADsObjectCreateInfo * pNewADsObjectCreateInfo);
  281. HRESULT HrCreateADsOrganizationalUnit(INOUT CNewADsObjectCreateInfo * pNewADsObjectCreateInfo);
  282. HRESULT HrCreateADsSimpleObject(INOUT CNewADsObjectCreateInfo * pNewADsObjectCreateInfo);
  283. HRESULT HrCreateADsObjectGenericWizard(INOUT CNewADsObjectCreateInfo * pNewADsObjectCreateInfo);
  284. HRESULT HrCreateADsObjectOverride(INOUT CNewADsObjectCreateInfo * pNewADsObjectCreateInfo);
  285. /////////////////////////////////////////////////////////////////////
  286. // Other misc routines.
  287. /////////////////////////////////////////////////////////////////////////
  288. // CDsAdminCreateObj
  289. class CDsAdminCreateObj:
  290. public CComObjectRoot,
  291. public CComCoClass<CDsAdminCreateObj, &CLSID_DsAdminCreateObj>,
  292. public IDsAdminCreateObj
  293. {
  294. public:
  295. BEGIN_COM_MAP(CDsAdminCreateObj)
  296. COM_INTERFACE_ENTRY(IDsAdminCreateObj)
  297. END_COM_MAP()
  298. DECLARE_REGISTRY_CLSID()
  299. CDsAdminCreateObj()
  300. {
  301. m_pNewADsObjectCreateInfo = NULL;
  302. }
  303. ~CDsAdminCreateObj()
  304. {
  305. if (m_pNewADsObjectCreateInfo != NULL)
  306. delete m_pNewADsObjectCreateInfo;
  307. }
  308. // IDsAdminCreateObj
  309. STDMETHODIMP Initialize(IADsContainer* pADsContainerObj,
  310. IADs* pADsCopySource,
  311. LPCWSTR lpszClassName);
  312. STDMETHODIMP CreateModal(HWND hwndParent,
  313. IADs** ppADsObj);
  314. private:
  315. // member variables
  316. CString m_szObjectClass;
  317. CString m_szNamingAttribute;
  318. CComPtr<IADsContainer> m_spADsContainerObj;
  319. MyBasePathsInfo m_basePathsInfo;
  320. CNewADsObjectCreateInfo* m_pNewADsObjectCreateInfo;
  321. HRESULT _GetNamingAttribute();
  322. };
  323. //+----------------------------------------------------------------------------
  324. //
  325. // Class: CSmartBytePtr
  326. //
  327. // Purpose: A simple smart pointer class that does allocation with
  328. // GlobalAlloc and cleanup with GlobalFree.
  329. //
  330. //-----------------------------------------------------------------------------
  331. class CSmartBytePtr
  332. {
  333. public:
  334. CSmartBytePtr(void) {m_ptr = NULL; m_fDetached = FALSE;}
  335. CSmartBytePtr(DWORD dwSize) {
  336. m_ptr = (PBYTE)GlobalAlloc(GMEM_FIXED | GMEM_ZEROINIT, dwSize);
  337. m_fDetached = FALSE;
  338. }
  339. ~CSmartBytePtr(void) {if (!m_fDetached && m_ptr) GlobalFree(m_ptr);}
  340. BYTE* operator=(const CSmartBytePtr& src) {return src.m_ptr;}
  341. void operator=(BYTE* src) {if (!m_fDetached && m_ptr) GlobalFree(m_ptr); m_ptr = src;}
  342. operator const BYTE*() {return m_ptr;}
  343. operator BYTE*() {return m_ptr;}
  344. BYTE** operator&() {if (!m_fDetached && m_ptr) GlobalFree(m_ptr); return &m_ptr;}
  345. operator BOOL() const {return m_ptr != NULL;}
  346. BOOL operator!() {return m_ptr == NULL;}
  347. BYTE* Detach() {m_fDetached = TRUE; return m_ptr;}
  348. BOOL ReAlloc(DWORD dwSize) {
  349. if (m_ptr) GlobalFree(m_ptr);
  350. m_ptr = (PBYTE)GlobalAlloc(GMEM_FIXED | GMEM_ZEROINIT, dwSize);
  351. m_fDetached = FALSE;
  352. return(m_ptr != NULL);
  353. }
  354. private:
  355. BYTE * m_ptr;
  356. BOOL m_fDetached;
  357. };
  358. #endif // ~__NEWOBJ_H_INCLUDED__