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.

424 lines
9.9 KiB

  1. //---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1995.
  5. //
  6. // File: helpers.cxx
  7. //
  8. // Contents: ADs C Wrappers (Helper functions)
  9. //
  10. // ADsBuildEnumerator
  11. // ADsFreeEnumerator
  12. // ADsEnumerateNext
  13. // ADsBuildVarArrayStr
  14. // ADsBuildVarArrayInt
  15. //
  16. // History:
  17. // 1-March-1995 KrishnaG -- Created
  18. //
  19. //----------------------------------------------------------------------------
  20. #include "oleds.hxx"
  21. #pragma hdrstop
  22. //+---------------------------------------------------------------------------
  23. //
  24. // Function: ADsBuildEnumerator
  25. //
  26. // Synopsis: C wrapper code to create an Enumerator object.
  27. //
  28. // Arguments: [pNetOleCollection] -- The input Collection object.
  29. //
  30. // [ppEnumVariant] -- The created Enumerator object.
  31. //
  32. // Returns: HRESULT
  33. //
  34. //----------------------------------------------------------------------------
  35. HRESULT
  36. ADsBuildEnumerator(
  37. IADsContainer *pADsContainer,
  38. IEnumVARIANT * * ppEnumVariant
  39. )
  40. {
  41. HRESULT hr;
  42. IUnknown *pUnk = NULL;
  43. //
  44. // Get a new enumerator object.
  45. //
  46. hr = pADsContainer->get__NewEnum( &pUnk );
  47. if (FAILED(hr)) {
  48. goto Fail;
  49. }
  50. //
  51. // QueryInterface the IUnknown pointer for an IEnumVARIANT interface,
  52. //
  53. hr = pUnk->QueryInterface(
  54. IID_IEnumVARIANT,
  55. (void FAR* FAR*)ppEnumVariant
  56. );
  57. if (FAILED(hr)) {
  58. goto Fail;
  59. }
  60. //
  61. // Release the IUnknown pointer.
  62. //
  63. pUnk->Release();
  64. return(hr);
  65. Fail:
  66. if (pUnk) {
  67. pUnk->Release();
  68. }
  69. return(hr);
  70. }
  71. //+---------------------------------------------------------------------------
  72. //
  73. // Function: ADsFreeEnumerator
  74. //
  75. // Synopsis: Frees an Enumerator object.
  76. //
  77. // Arguments: [pEnumerator] -- The Enumerator object to be freed.
  78. //
  79. // Returns: HRESULT
  80. //----------------------------------------------------------------------------
  81. HRESULT
  82. ADsFreeEnumerator(
  83. IEnumVARIANT *pEnumVariant
  84. )
  85. {
  86. HRESULT hr = E_ADS_BAD_PARAMETER;
  87. ULONG uRefCount = 0;
  88. if (pEnumVariant) {
  89. uRefCount = pEnumVariant->Release();
  90. return(S_OK);
  91. }
  92. return(hr);
  93. }
  94. //+---------------------------------------------------------------------------
  95. //
  96. // Function: ADsEnumerateNext
  97. //
  98. // Synopsis: C wrapper code for IEnumVARIANT::Next
  99. //
  100. // Arguments: [pEnumVariant] -- Input enumerator object.
  101. //
  102. // [cElements] -- Number of elements to retrieve.
  103. //
  104. // [pvar] -- VARIANT array.
  105. //
  106. // [pcElementFetched] -- Number of elements fetched.
  107. //
  108. // Returns: HRESULT
  109. //
  110. //----------------------------------------------------------------------------
  111. HRESULT
  112. ADsEnumerateNext(
  113. IEnumVARIANT *pEnumVariant,
  114. ULONG cElements,
  115. VARIANT * pvar,
  116. ULONG * pcElementsFetched
  117. )
  118. {
  119. return(pEnumVariant->Next(cElements, pvar, pcElementsFetched));
  120. }
  121. //+---------------------------------------------------------------------------
  122. //
  123. // Function: ADsBuildVarArrayStr
  124. //
  125. // Synopsis: Build a variant array of strings
  126. //
  127. // Arguments: [lppPathNames] -- List of pathnames to be put in the array.
  128. //
  129. // [dwPathNames] -- Number of pathnames in the list.
  130. //
  131. // [ppvar] -- Pointer to a pointer of a Variant array.
  132. //
  133. // Returns: HRESULT
  134. //
  135. //----------------------------------------------------------------------------
  136. HRESULT
  137. ADsBuildVarArrayStr(
  138. LPWSTR * lppPathNames,
  139. DWORD dwPathNames,
  140. VARIANT * pVar
  141. )
  142. {
  143. VARIANT v;
  144. SAFEARRAYBOUND sabNewArray;
  145. DWORD i;
  146. SAFEARRAY *psa = NULL;
  147. HRESULT hr = E_FAIL;
  148. if (!pVar) {
  149. hr = E_ADS_BAD_PARAMETER;
  150. goto Fail;
  151. }
  152. VariantInit(pVar);
  153. sabNewArray.cElements = dwPathNames;
  154. sabNewArray.lLbound = 0;
  155. psa = SafeArrayCreate(VT_VARIANT, 1, &sabNewArray);
  156. if (!psa) {
  157. goto Fail;
  158. }
  159. for (i = 0; i < dwPathNames; i++) {
  160. VariantInit(&v);
  161. V_VT(&v) = VT_BSTR;
  162. V_BSTR(&v) = SysAllocString(*(lppPathNames + i));
  163. hr = SafeArrayPutElement(psa,
  164. (long FAR *)&i,
  165. &v
  166. );
  167. VariantClear( &v );
  168. if (FAILED(hr)) {
  169. goto Fail;
  170. }
  171. }
  172. V_VT(pVar) = VT_ARRAY | VT_VARIANT;
  173. V_ARRAY(pVar) = psa;
  174. return(ResultFromScode(S_OK));
  175. Fail:
  176. if (psa) {
  177. SafeArrayDestroy(psa);
  178. }
  179. return(E_FAIL);
  180. }
  181. //+---------------------------------------------------------------------------
  182. //
  183. // Function: ADsBuildVarArrayInt
  184. //
  185. // Synopsis: Build a variant array of integers
  186. //
  187. // Arguments: [lppObjectTypes] -- List of object types to be put in the array.
  188. //
  189. // [dwObjectTypes] -- Number of object types in the list.
  190. //
  191. // [ppvar] -- Pointer to a pointer of a Variant array.
  192. //
  193. // Returns: HRESULT
  194. //
  195. //----------------------------------------------------------------------------
  196. HRESULT
  197. ADsBuildVarArrayInt(
  198. LPDWORD lpdwObjectTypes,
  199. DWORD dwObjectTypes,
  200. VARIANT * pVar
  201. )
  202. {
  203. VARIANT v;
  204. SAFEARRAYBOUND sabNewArray;
  205. DWORD i;
  206. SAFEARRAY *psa = NULL;
  207. HRESULT hr = E_FAIL;
  208. if (!pVar) {
  209. hr = E_ADS_BAD_PARAMETER;
  210. goto Fail;
  211. }
  212. VariantInit(pVar);
  213. sabNewArray.cElements = dwObjectTypes;
  214. sabNewArray.lLbound = 0;
  215. psa = SafeArrayCreate(VT_VARIANT, 1, &sabNewArray);
  216. if (!psa) {
  217. goto Fail;
  218. }
  219. for (i = 0; i < dwObjectTypes; i++) {
  220. VariantInit(&v);
  221. V_VT(&v) = VT_I4;
  222. V_I4(&v) = *(lpdwObjectTypes + i);
  223. hr = SafeArrayPutElement(psa,
  224. (long FAR *)&i,
  225. &v
  226. );
  227. VariantClear( &v );
  228. if (FAILED(hr)) {
  229. goto Fail;
  230. }
  231. }
  232. V_VT(pVar) = VT_VARIANT | VT_ARRAY;
  233. V_ARRAY(pVar) = psa;
  234. return(ResultFromScode(S_OK));
  235. Fail:
  236. if (psa) {
  237. SafeArrayDestroy(psa);
  238. }
  239. return(E_FAIL);
  240. }
  241. //+---------------------------------------------------------------------------
  242. // Function: BinarySDToSecurityDescriptor.
  243. //
  244. // Synopsis: Convert the given binary blob to an equivalent object
  245. // implementing the IADsSecurityDescriptor interface.
  246. //
  247. // Arguments: pSecurityDescriptor - the binary SD to convert.
  248. // pVarsec - return value.
  249. // pszServerName - serverName the SD was
  250. // retrieved from (optional).
  251. // userName - not used, optional.
  252. // passWord - not used, optional.
  253. // dwFlags - not used, optional.
  254. //
  255. // Returns: HRESULT - S_OK or any failure error code
  256. //
  257. // Modifies: pVarsec to contain a VT_DISPATCH if successful.
  258. //
  259. //----------------------------------------------------------------------------
  260. HRESULT
  261. BinarySDToSecurityDescriptor(
  262. PSECURITY_DESCRIPTOR pSecurityDescriptor,
  263. VARIANT *pVarsec,
  264. LPCWSTR pszServerName, // defaulted to NULL
  265. LPCWSTR userName, // defaulted to NULL
  266. LPCWSTR passWord, // defaulted to NULL
  267. DWORD dwFlags // defaulted to 0
  268. )
  269. {
  270. CCredentials creds((LPWSTR)userName, (LPWSTR)passWord, dwFlags);
  271. //
  272. // Call internal routine in sec2var.cxx that does all the work.
  273. //
  274. RRETURN(ConvertSecDescriptorToVariant(
  275. (LPWSTR)pszServerName,
  276. creds,
  277. pSecurityDescriptor,
  278. pVarsec,
  279. TRUE // we will always expect NT format.
  280. )
  281. );
  282. }
  283. //+---------------------------------------------------------------------------
  284. // Function: SecurityDescriptorToBinarySD.
  285. //
  286. // Synopsis: Convert the given IADsSecurityDescriptor to (in the variant
  287. // to a binary security descriptor blob.
  288. //
  289. // Arguments: vVarSecDes - the binary SD to convert.
  290. // ppSecurityDescriptor - ptr to output binary blob.
  291. // pszServerName - serverName the SD
  292. // is being put on, optional.
  293. // userName - not used, optional.
  294. // passWord - not used, optional.
  295. // dwFlags - not used, optional.
  296. //
  297. // Returns: HRESULT - S_OK or any failure error code
  298. //
  299. // Modifies: pVarsec to contain a VT_DISPATCH if successful.
  300. //
  301. //----------------------------------------------------------------------------
  302. HRESULT
  303. SecurityDescriptorToBinarySD(
  304. VARIANT vVarSecDes,
  305. PSECURITY_DESCRIPTOR * ppSecurityDescriptor,
  306. PDWORD pdwSDLength,
  307. LPCWSTR pszServerName, // defaulted to NULL
  308. LPCWSTR userName, // defaulted to NULL
  309. LPCWSTR passWord, // defaulted to NULL
  310. DWORD dwFlags // defaulted to 0
  311. )
  312. {
  313. HRESULT hr = E_FAIL;
  314. IADsSecurityDescriptor *pIADsSecDesc = NULL;
  315. CCredentials creds((LPWSTR)userName, (LPWSTR)passWord, dwFlags);
  316. //
  317. // Verify it is a VT_DISPATCH and also that ptr is valid.
  318. //
  319. if ((vVarSecDes.vt != VT_DISPATCH)
  320. || (!V_DISPATCH(&vVarSecDes))
  321. ) {
  322. BAIL_ON_FAILURE(hr);
  323. }
  324. //
  325. // Get the interface ptr from the variant (it has to be IDispatch)
  326. //
  327. hr = vVarSecDes.pdispVal->QueryInterface(
  328. IID_IADsSecurityDescriptor,
  329. (void **) &pIADsSecDesc
  330. );
  331. BAIL_ON_FAILURE(hr);
  332. //
  333. // Call the helper routine in sec2var.cxx
  334. //
  335. hr = ConvertSecurityDescriptorToSecDes(
  336. (LPWSTR)pszServerName,
  337. creds,
  338. pIADsSecDesc,
  339. ppSecurityDescriptor,
  340. pdwSDLength,
  341. TRUE // always NT Sec desc mode
  342. );
  343. error:
  344. if (pIADsSecDesc) {
  345. pIADsSecDesc->Release();
  346. }
  347. RRETURN(hr);
  348. }
  349. // end of helpers.cxx