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.

428 lines
10 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. if(!V_BSTR(&v) && (*(lppPathNames + i)))
  164. {
  165. goto Fail;
  166. }
  167. hr = SafeArrayPutElement(psa,
  168. (long FAR *)&i,
  169. &v
  170. );
  171. VariantClear( &v );
  172. if (FAILED(hr)) {
  173. goto Fail;
  174. }
  175. }
  176. V_VT(pVar) = VT_ARRAY | VT_VARIANT;
  177. V_ARRAY(pVar) = psa;
  178. return(ResultFromScode(S_OK));
  179. Fail:
  180. if (psa) {
  181. SafeArrayDestroy(psa);
  182. }
  183. return(E_FAIL);
  184. }
  185. //+---------------------------------------------------------------------------
  186. //
  187. // Function: ADsBuildVarArrayInt
  188. //
  189. // Synopsis: Build a variant array of integers
  190. //
  191. // Arguments: [lppObjectTypes] -- List of object types to be put in the array.
  192. //
  193. // [dwObjectTypes] -- Number of object types in the list.
  194. //
  195. // [ppvar] -- Pointer to a pointer of a Variant array.
  196. //
  197. // Returns: HRESULT
  198. //
  199. //----------------------------------------------------------------------------
  200. HRESULT
  201. ADsBuildVarArrayInt(
  202. LPDWORD lpdwObjectTypes,
  203. DWORD dwObjectTypes,
  204. VARIANT * pVar
  205. )
  206. {
  207. VARIANT v;
  208. SAFEARRAYBOUND sabNewArray;
  209. DWORD i;
  210. SAFEARRAY *psa = NULL;
  211. HRESULT hr = E_FAIL;
  212. if (!pVar) {
  213. hr = E_ADS_BAD_PARAMETER;
  214. goto Fail;
  215. }
  216. VariantInit(pVar);
  217. sabNewArray.cElements = dwObjectTypes;
  218. sabNewArray.lLbound = 0;
  219. psa = SafeArrayCreate(VT_VARIANT, 1, &sabNewArray);
  220. if (!psa) {
  221. goto Fail;
  222. }
  223. for (i = 0; i < dwObjectTypes; i++) {
  224. VariantInit(&v);
  225. V_VT(&v) = VT_I4;
  226. V_I4(&v) = *(lpdwObjectTypes + i);
  227. hr = SafeArrayPutElement(psa,
  228. (long FAR *)&i,
  229. &v
  230. );
  231. VariantClear( &v );
  232. if (FAILED(hr)) {
  233. goto Fail;
  234. }
  235. }
  236. V_VT(pVar) = VT_VARIANT | VT_ARRAY;
  237. V_ARRAY(pVar) = psa;
  238. return(ResultFromScode(S_OK));
  239. Fail:
  240. if (psa) {
  241. SafeArrayDestroy(psa);
  242. }
  243. return(E_FAIL);
  244. }
  245. //+---------------------------------------------------------------------------
  246. // Function: BinarySDToSecurityDescriptor.
  247. //
  248. // Synopsis: Convert the given binary blob to an equivalent object
  249. // implementing the IADsSecurityDescriptor interface.
  250. //
  251. // Arguments: pSecurityDescriptor - the binary SD to convert.
  252. // pVarsec - return value.
  253. // pszServerName - serverName the SD was
  254. // retrieved from (optional).
  255. // userName - not used, optional.
  256. // passWord - not used, optional.
  257. // dwFlags - not used, optional.
  258. //
  259. // Returns: HRESULT - S_OK or any failure error code
  260. //
  261. // Modifies: pVarsec to contain a VT_DISPATCH if successful.
  262. //
  263. //----------------------------------------------------------------------------
  264. HRESULT
  265. BinarySDToSecurityDescriptor(
  266. PSECURITY_DESCRIPTOR pSecurityDescriptor,
  267. VARIANT *pVarsec,
  268. LPCWSTR pszServerName, // defaulted to NULL
  269. LPCWSTR userName, // defaulted to NULL
  270. LPCWSTR passWord, // defaulted to NULL
  271. DWORD dwFlags // defaulted to 0
  272. )
  273. {
  274. CCredentials creds((LPWSTR)userName, (LPWSTR)passWord, dwFlags);
  275. //
  276. // Call internal routine in sec2var.cxx that does all the work.
  277. //
  278. RRETURN(ConvertSecDescriptorToVariant(
  279. (LPWSTR)pszServerName,
  280. creds,
  281. pSecurityDescriptor,
  282. pVarsec,
  283. TRUE // we will always expect NT format.
  284. )
  285. );
  286. }
  287. //+---------------------------------------------------------------------------
  288. // Function: SecurityDescriptorToBinarySD.
  289. //
  290. // Synopsis: Convert the given IADsSecurityDescriptor to (in the variant
  291. // to a binary security descriptor blob.
  292. //
  293. // Arguments: vVarSecDes - the binary SD to convert.
  294. // ppSecurityDescriptor - ptr to output binary blob.
  295. // pszServerName - serverName the SD
  296. // is being put on, optional.
  297. // userName - not used, optional.
  298. // passWord - not used, optional.
  299. // dwFlags - not used, optional.
  300. //
  301. // Returns: HRESULT - S_OK or any failure error code
  302. //
  303. // Modifies: pVarsec to contain a VT_DISPATCH if successful.
  304. //
  305. //----------------------------------------------------------------------------
  306. HRESULT
  307. SecurityDescriptorToBinarySD(
  308. VARIANT vVarSecDes,
  309. PSECURITY_DESCRIPTOR * ppSecurityDescriptor,
  310. PDWORD pdwSDLength,
  311. LPCWSTR pszServerName, // defaulted to NULL
  312. LPCWSTR userName, // defaulted to NULL
  313. LPCWSTR passWord, // defaulted to NULL
  314. DWORD dwFlags // defaulted to 0
  315. )
  316. {
  317. HRESULT hr = E_FAIL;
  318. IADsSecurityDescriptor *pIADsSecDesc = NULL;
  319. CCredentials creds((LPWSTR)userName, (LPWSTR)passWord, dwFlags);
  320. //
  321. // Verify it is a VT_DISPATCH and also that ptr is valid.
  322. //
  323. if ((vVarSecDes.vt != VT_DISPATCH)
  324. || (!V_DISPATCH(&vVarSecDes))
  325. ) {
  326. BAIL_ON_FAILURE(hr);
  327. }
  328. //
  329. // Get the interface ptr from the variant (it has to be IDispatch)
  330. //
  331. hr = vVarSecDes.pdispVal->QueryInterface(
  332. IID_IADsSecurityDescriptor,
  333. (void **) &pIADsSecDesc
  334. );
  335. BAIL_ON_FAILURE(hr);
  336. //
  337. // Call the helper routine in sec2var.cxx
  338. //
  339. hr = ConvertSecurityDescriptorToSecDes(
  340. (LPWSTR)pszServerName,
  341. creds,
  342. pIADsSecDesc,
  343. ppSecurityDescriptor,
  344. pdwSDLength,
  345. TRUE // always NT Sec desc mode
  346. );
  347. error:
  348. if (pIADsSecDesc) {
  349. pIADsSecDesc->Release();
  350. }
  351. RRETURN(hr);
  352. }
  353. // end of helpers.cxx