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.

466 lines
11 KiB

  1. //---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1995
  5. //
  6. // File: cenumGroupCollection.cxx
  7. //
  8. // Contents: Windows NT 3.5 GroupCollection Enumeration Code
  9. //
  10. // CWinNTGroupCollectionEnum::CWinNTGroupCollectionEnum()
  11. // CWinNTGroupCollectionEnum::CWinNTGroupCollectionEnum
  12. // CWinNTGroupCollectionEnum::EnumObjects
  13. // CWinNTGroupCollectionEnum::EnumObjects
  14. //
  15. // History:
  16. //----------------------------------------------------------------------------
  17. #include "winnt.hxx"
  18. #pragma hdrstop
  19. //+---------------------------------------------------------------------------
  20. //
  21. // Function: CWinNTEnumVariant::Create
  22. //
  23. // Synopsis:
  24. //
  25. // Arguments: [pCollection]
  26. // [ppEnumVariant]
  27. //
  28. // Returns: HRESULT
  29. //
  30. // Modifies:
  31. //
  32. // History: 01-30-95 krishnag Created.
  33. //
  34. //----------------------------------------------------------------------------
  35. HRESULT
  36. CWinNTGroupCollectionEnum::Create(
  37. CWinNTGroupCollectionEnum FAR* FAR* ppenumvariant,
  38. BSTR Parent,
  39. ULONG ParentType,
  40. BSTR ADsPath,
  41. BSTR DomainName,
  42. BSTR ServerName,
  43. BSTR GroupName,
  44. ULONG GroupType,
  45. VARIANT var,
  46. CWinNTCredentials& Credentials
  47. )
  48. {
  49. HRESULT hr = NOERROR;
  50. CWinNTGroupCollectionEnum FAR* penumvariant = NULL;
  51. *ppenumvariant = NULL;
  52. penumvariant = new CWinNTGroupCollectionEnum();
  53. if (!penumvariant) {
  54. hr = E_OUTOFMEMORY;
  55. BAIL_ON_FAILURE(hr);
  56. }
  57. hr = ADsAllocString( Parent, &penumvariant->_Parent);
  58. BAIL_ON_FAILURE(hr);
  59. hr = ADsAllocString( DomainName, &penumvariant->_DomainName);
  60. BAIL_ON_FAILURE(hr);
  61. hr = ADsAllocString( ServerName, &penumvariant->_ServerName);
  62. BAIL_ON_FAILURE(hr);
  63. penumvariant->_ParentType = ParentType;
  64. hr = ADsAllocString(ADsPath, &penumvariant->_ADsPath);
  65. BAIL_ON_FAILURE(hr);
  66. hr = ADsAllocString( GroupName, &penumvariant->_GroupName);
  67. BAIL_ON_FAILURE(hr);
  68. penumvariant->_GroupType = GroupType;
  69. hr = ObjectTypeList::CreateObjectTypeList(
  70. var,
  71. &penumvariant->_pObjList
  72. );
  73. BAIL_ON_FAILURE(hr);
  74. penumvariant->_Credentials = Credentials;
  75. hr = penumvariant->_Credentials.Ref(ServerName, DomainName, ParentType);
  76. BAIL_ON_FAILURE(hr);
  77. *ppenumvariant = penumvariant;
  78. RRETURN(hr);
  79. error:
  80. delete penumvariant;
  81. RRETURN_EXP_IF_ERR(hr);
  82. }
  83. CWinNTGroupCollectionEnum::CWinNTGroupCollectionEnum():
  84. _Parent(NULL),
  85. _ParentType(0),
  86. _ADsPath(NULL),
  87. _DomainName(NULL),
  88. _ServerName(NULL),
  89. _GroupName(NULL),
  90. _lpServerName(NULL),
  91. _hGroup(NULL)
  92. {
  93. _pObjList = NULL;
  94. }
  95. CWinNTGroupCollectionEnum::CWinNTGroupCollectionEnum(ObjectTypeList ObjList):
  96. _Parent(NULL),
  97. _ParentType(0),
  98. _ADsPath(NULL),
  99. _DomainName(NULL),
  100. _ServerName(NULL),
  101. _GroupName(NULL),
  102. _lpServerName(NULL),
  103. _hGroup(NULL)
  104. {
  105. _pObjList = NULL;
  106. }
  107. CWinNTGroupCollectionEnum::~CWinNTGroupCollectionEnum()
  108. {
  109. if (_hGroup) {
  110. if (_GroupType == WINNT_GROUP_GLOBAL) {
  111. WinNTGlobalGroupClose(
  112. _hGroup
  113. );
  114. }else {
  115. WinNTLocalGroupClose(
  116. _hGroup
  117. );
  118. }
  119. }
  120. if (_Parent) {
  121. ADsFreeString(_Parent);
  122. }
  123. if (_DomainName) {
  124. ADsFreeString(_DomainName);
  125. }
  126. if (_ServerName) {
  127. ADsFreeString(_ServerName);
  128. }
  129. if (_ADsPath) {
  130. ADsFreeString(_ADsPath);
  131. }
  132. if (_GroupName) {
  133. ADsFreeString(_GroupName);
  134. }
  135. if (_pObjList) {
  136. delete _pObjList;
  137. }
  138. if (_lpServerName) {
  139. FreeADsStr(_lpServerName) ;
  140. }
  141. }
  142. HRESULT
  143. CWinNTGroupCollectionEnum::EnumGroupMembers(
  144. ULONG cElements,
  145. VARIANT FAR* pvar,
  146. ULONG FAR* pcElementFetched
  147. )
  148. {
  149. HRESULT hr = S_FALSE;
  150. IDispatch *pDispatch = NULL;
  151. DWORD i = 0;
  152. IADs * pIADs = NULL;
  153. BSTR pszClass = NULL;
  154. DWORD dwClassID;
  155. DWORD dwFilterID;
  156. BOOL fFound = FALSE;
  157. while (i < cElements) {
  158. //
  159. // This is the global group case
  160. //
  161. hr = GetDomainMemberObject(&pDispatch);
  162. if (hr == S_FALSE) {
  163. break;
  164. }
  165. //
  166. // Apply the IADsMembers::put_Filter filter.
  167. // If the enumerated object is not one of the types to be returned,
  168. // go on to the next member of the group.
  169. //
  170. hr = pDispatch->QueryInterface(IID_IADs, (void **)&pIADs);
  171. BAIL_ON_FAILURE(hr);
  172. //
  173. // Determine the object class of the enumerated object and the corresponding
  174. // object class ID number (as specified in the Filters global array).
  175. //
  176. hr = pIADs->get_Class(&pszClass);
  177. BAIL_ON_FAILURE(hr);
  178. hr = IsValidFilter(pszClass, &dwClassID, gpFilters, gdwMaxFilters);
  179. if (SUCCEEDED(hr)) {
  180. //
  181. // Enumerate through the object classes listed in the user-specified filter
  182. // until we either find a match (fFound = TRUE) or we reach the end of the
  183. // list.
  184. //
  185. hr = _pObjList->Reset();
  186. while (SUCCEEDED(hr)) {
  187. hr = _pObjList->GetCurrentObject(&dwFilterID);
  188. if (SUCCEEDED(hr)
  189. && (dwFilterID == dwClassID)
  190. ) {
  191. fFound = TRUE;
  192. break;
  193. }
  194. hr = _pObjList->Next();
  195. }
  196. if (!fFound) {
  197. //
  198. // not on the list of objects to return, try again
  199. // with the next member of the group
  200. //
  201. pDispatch->Release();
  202. pIADs->Release();
  203. if (pszClass) {
  204. ADsFreeString(pszClass);
  205. }
  206. continue;
  207. }
  208. }
  209. pIADs->Release();
  210. if (pszClass) {
  211. ADsFreeString(pszClass);
  212. }
  213. //
  214. // Return it.
  215. //
  216. VariantInit(&pvar[i]);
  217. pvar[i].vt = VT_DISPATCH;
  218. pvar[i].pdispVal = pDispatch;
  219. (*pcElementFetched)++;
  220. i++;
  221. }
  222. RRETURN_EXP_IF_ERR(hr);
  223. error:
  224. if (pDispatch) {
  225. pDispatch->Release();
  226. }
  227. if (pIADs) {
  228. pIADs->Release();
  229. }
  230. if (pszClass) {
  231. ADsFreeString(pszClass);
  232. }
  233. RRETURN_EXP_IF_ERR(hr);
  234. }
  235. HRESULT
  236. CWinNTGroupCollectionEnum::GetDomainMemberObject(
  237. IDispatch ** ppDispatch
  238. )
  239. {
  240. HRESULT hr = S_OK;
  241. LPDOMAIN_GROUP_MEMBER pDomainGrpMember = NULL;
  242. LPBYTE pBuffer = NULL;
  243. DWORD dwReturned = 0;
  244. BOOL dwRet = 0;
  245. WCHAR szHostServerName[MAX_PATH];
  246. if (!_lpServerName) {
  247. hr = WinNTGetCachedDCName(
  248. _DomainName,
  249. szHostServerName,
  250. _Credentials.GetFlags()
  251. );
  252. BAIL_ON_FAILURE(hr);
  253. if (!(_lpServerName = AllocADsStr(szHostServerName))) {
  254. hr = HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY) ;
  255. BAIL_ON_FAILURE(hr);
  256. }
  257. }
  258. if (!_hGroup) {
  259. dwRet = WinNTGlobalGroupOpen(
  260. _DomainName,
  261. _lpServerName + 2, // skip the 2 leading back slashes
  262. _GroupName,
  263. &_hGroup
  264. );
  265. if (!dwRet) {
  266. goto error;
  267. }
  268. }
  269. dwRet = WinNTGlobalGroupEnum(
  270. _hGroup,
  271. 1,
  272. &pBuffer,
  273. &dwReturned
  274. );
  275. if (!dwRet) {
  276. goto error;
  277. }
  278. pDomainGrpMember = (LPDOMAIN_GROUP_MEMBER)pBuffer;
  279. switch (pDomainGrpMember->Type) {
  280. case WINNT_USER_ID :
  281. hr = CWinNTUser::CreateUser(
  282. pDomainGrpMember->Parent,
  283. WINNT_DOMAIN_ID,
  284. pDomainGrpMember->Domain,
  285. NULL,
  286. pDomainGrpMember->Name,
  287. ADS_OBJECT_BOUND,
  288. IID_IDispatch,
  289. _Credentials,
  290. (void **)ppDispatch
  291. );
  292. break;
  293. case WINNT_GROUP_ID:
  294. hr = CWinNTGroup::CreateGroup(
  295. pDomainGrpMember->Parent,
  296. WINNT_DOMAIN_ID,
  297. pDomainGrpMember->Domain,
  298. NULL,
  299. pDomainGrpMember->Name,
  300. WINNT_GROUP_GLOBAL,
  301. ADS_OBJECT_BOUND,
  302. IID_IDispatch,
  303. _Credentials,
  304. (void **)ppDispatch
  305. );
  306. break;
  307. default:
  308. goto error;
  309. }
  310. BAIL_ON_FAILURE(hr);
  311. hr = S_OK;
  312. cleanup:
  313. if (pBuffer) {
  314. FreeADsMem(pBuffer);
  315. }
  316. RRETURN(hr);
  317. error:
  318. *ppDispatch = NULL;
  319. hr = S_FALSE;
  320. goto cleanup;
  321. }
  322. //+---------------------------------------------------------------------------
  323. //
  324. // Function: CWinNTGroupCollectionEnum::Next
  325. //
  326. // Synopsis: Returns cElements number of requested NetOle objects in the
  327. // array supplied in pvar.
  328. //
  329. // Arguments: [cElements] -- The number of elements requested by client
  330. // [pvar] -- ptr to array of VARIANTs to for return objects
  331. // [pcElementFetched] -- if non-NULL, then number of elements
  332. // -- actually returned is placed here
  333. //
  334. // Returns: HRESULT -- S_OK if number of elements requested are returned
  335. // -- S_FALSE if number of elements is < requested
  336. //
  337. // Modifies:
  338. //
  339. // History: 11-3-95 krishnag Created.
  340. //
  341. //----------------------------------------------------------------------------
  342. STDMETHODIMP
  343. CWinNTGroupCollectionEnum::Next(
  344. ULONG cElements,
  345. VARIANT FAR* pvar,
  346. ULONG FAR* pcElementFetched
  347. )
  348. {
  349. ULONG cElementFetched = 0;
  350. HRESULT hr = S_OK;
  351. if (NULL == pvar)
  352. {
  353. hr = S_FALSE;
  354. }
  355. else
  356. {
  357. hr = EnumGroupMembers(
  358. cElements,
  359. pvar,
  360. &cElementFetched
  361. );
  362. }
  363. if (pcElementFetched) {
  364. *pcElementFetched = cElementFetched;
  365. }
  366. RRETURN_EXP_IF_ERR(hr);
  367. }