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.

292 lines
6.4 KiB

  1. //----------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2001.
  5. //
  6. // File: New.cpp
  7. //
  8. // Contents: Wireless Policy Snapin - New Policy Creation
  9. //
  10. //
  11. // History: TaroonM
  12. // 10/30/01
  13. //
  14. //----------------------------------------------------------------------------
  15. #include "stdafx.h"
  16. #include <htmlhelp.h>
  17. #include "activeds.h"
  18. #include "iadsp.h"
  19. #include "new.h"
  20. HRESULT
  21. CreateDirectoryAndBindToObject(
  22. IDirectoryObject * pParentContainer,
  23. LPWSTR pszCommonName,
  24. LPWSTR pszObjectClass,
  25. IDirectoryObject ** ppDirectoryObject
  26. )
  27. {
  28. ADS_ATTR_INFO AttrInfo[2];
  29. ADSVALUE classValue;
  30. HRESULT hr = S_OK;
  31. IADsContainer * pADsContainer = NULL;
  32. IDispatch * pDispatch = NULL;
  33. //
  34. // Populate ADS_ATTR_INFO structure for new object
  35. //
  36. classValue.dwType = ADSTYPE_CASE_IGNORE_STRING;
  37. classValue.CaseIgnoreString = pszObjectClass;
  38. AttrInfo[0].pszAttrName = L"objectClass";
  39. AttrInfo[0].dwControlCode = ADS_ATTR_UPDATE;
  40. AttrInfo[0].dwADsType = ADSTYPE_CASE_IGNORE_STRING;
  41. AttrInfo[0].pADsValues = &classValue;
  42. AttrInfo[0].dwNumValues = 1;
  43. hr = pParentContainer->CreateDSObject(
  44. pszCommonName,
  45. AttrInfo,
  46. 1,
  47. &pDispatch
  48. );
  49. if ((FAILED(hr) && (hr == E_ADS_OBJECT_EXISTS)) ||
  50. (FAILED(hr) && (hr == HRESULT_FROM_WIN32(ERROR_OBJECT_ALREADY_EXISTS)))){
  51. hr = pParentContainer->QueryInterface(
  52. IID_IADsContainer,
  53. (void **)&pADsContainer
  54. );
  55. BAIL_ON_FAILURE(hr);
  56. hr = pADsContainer->GetObject(
  57. pszObjectClass,
  58. pszCommonName,
  59. &pDispatch
  60. );
  61. BAIL_ON_FAILURE(hr);
  62. }
  63. hr = pDispatch->QueryInterface(
  64. IID_IDirectoryObject,
  65. (void **)ppDirectoryObject
  66. );
  67. error:
  68. if (pADsContainer) {
  69. pADsContainer->Release();
  70. }
  71. if (pDispatch) {
  72. pDispatch->Release();
  73. }
  74. return(hr);
  75. }
  76. HRESULT
  77. CreateChildPath(
  78. LPWSTR pszParentPath,
  79. LPWSTR pszChildComponent,
  80. BSTR * ppszChildPath
  81. )
  82. {
  83. HRESULT hr = S_OK;
  84. IADsPathname *pPathname = NULL;
  85. hr = CoCreateInstance(
  86. CLSID_Pathname,
  87. NULL,
  88. CLSCTX_ALL,
  89. IID_IADsPathname,
  90. (void**)&pPathname
  91. );
  92. BAIL_ON_FAILURE(hr);
  93. hr = pPathname->Set(pszParentPath, ADS_SETTYPE_FULL);
  94. BAIL_ON_FAILURE(hr);
  95. hr = pPathname->AddLeafElement(pszChildComponent);
  96. BAIL_ON_FAILURE(hr);
  97. hr = pPathname->Retrieve(ADS_FORMAT_X500, ppszChildPath);
  98. BAIL_ON_FAILURE(hr);
  99. error:
  100. if (pPathname) {
  101. pPathname->Release();
  102. }
  103. return(hr);
  104. }
  105. HRESULT
  106. ConvertADsPathToDN(
  107. LPWSTR pszPathName,
  108. BSTR * ppszPolicyDN
  109. )
  110. {
  111. HRESULT hr = S_OK;
  112. IADsPathname *pPathname = NULL;
  113. hr = CoCreateInstance(
  114. CLSID_Pathname,
  115. NULL,
  116. CLSCTX_ALL,
  117. IID_IADsPathname,
  118. (void**)&pPathname
  119. );
  120. BAIL_ON_FAILURE(hr);
  121. hr = pPathname->Set(pszPathName, ADS_SETTYPE_FULL);
  122. BAIL_ON_FAILURE(hr);
  123. hr = pPathname->Retrieve(ADS_FORMAT_X500_DN, ppszPolicyDN);
  124. BAIL_ON_FAILURE(hr);
  125. error:
  126. if (pPathname) {
  127. pPathname->Release();
  128. }
  129. return(hr);
  130. }
  131. // Create Container for Our Policies.
  132. HRESULT
  133. AddWirelessPolicyContainerToGPO(
  134. const CString & szMachinePath
  135. )
  136. {
  137. HRESULT hr = S_OK;
  138. IDirectoryObject * pMachineContainer = NULL;
  139. IDirectoryObject * pWindowsContainer = NULL;
  140. IDirectoryObject * pMicrosoftContainer = NULL;
  141. IDirectoryObject * pWirelessContainer = NULL;
  142. BSTR pszMicrosoftPath = NULL;
  143. BSTR pszWindowsPath = NULL;
  144. BSTR pszWirelessPath = NULL;
  145. CString szCompleteMachinePath;
  146. LPWSTR szMachineContainerPath;
  147. CString prefixMachinePath;
  148. prefixMachinePath = L"LDAP://";
  149. szCompleteMachinePath = prefixMachinePath + szMachinePath;
  150. szMachineContainerPath = szCompleteMachinePath.GetBuffer(0);
  151. hr = ADsGetObject(
  152. szMachineContainerPath,
  153. IID_IDirectoryObject,
  154. (void **)&pMachineContainer
  155. );
  156. BAIL_ON_FAILURE(hr);
  157. // Build the fully qualified ADsPath for my object
  158. hr = CreateChildPath(
  159. szMachineContainerPath,
  160. L"cn=Microsoft",
  161. &pszMicrosoftPath
  162. );
  163. BAIL_ON_FAILURE(hr);
  164. hr = CreateChildPath(
  165. pszMicrosoftPath,
  166. L"cn=Windows",
  167. &pszWindowsPath
  168. );
  169. BAIL_ON_FAILURE(hr);
  170. hr = CreateChildPath(
  171. pszWindowsPath,
  172. L"cn=Wireless",
  173. &pszWirelessPath
  174. );
  175. BAIL_ON_FAILURE(hr);
  176. hr = ADsGetObject(
  177. pszWirelessPath,
  178. IID_IDirectoryObject,
  179. (void **)&pWirelessContainer
  180. );
  181. if (FAILED(hr)) {
  182. //
  183. // Bind to the Machine Container
  184. //
  185. hr = CreateDirectoryAndBindToObject(
  186. pMachineContainer,
  187. L"cn=Microsoft",
  188. L"container",
  189. &pMicrosoftContainer
  190. );
  191. BAIL_ON_FAILURE(hr);
  192. hr = CreateDirectoryAndBindToObject(
  193. pMicrosoftContainer,
  194. L"cn=Windows",
  195. L"container",
  196. &pWindowsContainer
  197. );
  198. BAIL_ON_FAILURE(hr);
  199. hr = CreateDirectoryAndBindToObject(
  200. pWindowsContainer,
  201. L"cn=Wireless",
  202. L"container",
  203. &pWirelessContainer
  204. );
  205. BAIL_ON_FAILURE(hr);
  206. }
  207. error:
  208. if (pWirelessContainer) {
  209. pWirelessContainer->Release();
  210. }
  211. if (pWindowsContainer) {
  212. pWindowsContainer->Release();
  213. }
  214. if (pMicrosoftContainer) {
  215. pMicrosoftContainer->Release();
  216. }
  217. if (pMachineContainer) {
  218. pMachineContainer->Release();
  219. }
  220. if (pszMicrosoftPath) {
  221. SysFreeString(pszMicrosoftPath);
  222. }
  223. if (pszWindowsPath) {
  224. SysFreeString(pszWindowsPath);
  225. }
  226. return(hr);
  227. }