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.

446 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. // CWinNTLocalGroupCollectionEnum::CWinNTLocalGroupCollectionEnum()
  11. // CWinNTLocalGroupCollectionEnum::CWinNTLocalGroupCollectionEnum
  12. // CWinNTLocalGroupCollectionEnum::EnumObjects
  13. // CWinNTLocalGroupCollectionEnum::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. CWinNTLocalGroupCollectionEnum::Create(
  37. CWinNTLocalGroupCollectionEnum 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. CWinNTLocalGroupCollectionEnum FAR* penumvariant = NULL;
  51. *ppenumvariant = NULL;
  52. penumvariant = new CWinNTLocalGroupCollectionEnum();
  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. CWinNTLocalGroupCollectionEnum::CWinNTLocalGroupCollectionEnum():
  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. CWinNTLocalGroupCollectionEnum::CWinNTLocalGroupCollectionEnum(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. CWinNTLocalGroupCollectionEnum::~CWinNTLocalGroupCollectionEnum()
  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 (_pObjList) {
  121. delete _pObjList;
  122. }
  123. if (_lpServerName) {
  124. FreeADsStr(_lpServerName) ;
  125. }
  126. if(_Parent) {
  127. ADsFreeString(_Parent);
  128. }
  129. if(_DomainName) {
  130. ADsFreeString(_DomainName);
  131. }
  132. if(_ServerName) {
  133. ADsFreeString(_ServerName);
  134. }
  135. if(_ADsPath) {
  136. ADsFreeString(_ADsPath);
  137. }
  138. if(_GroupName) {
  139. ADsFreeString(_GroupName);
  140. }
  141. }
  142. HRESULT
  143. CWinNTLocalGroupCollectionEnum::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. hr = GetComputerMemberObject(&pDispatch);
  159. if (hr == S_FALSE) {
  160. break;
  161. }
  162. //
  163. // Apply the IADsMembers::put_Filter filter.
  164. // If the enumerated object is not one of the types to be returned,
  165. // go on to the next member of the group.
  166. //
  167. hr = pDispatch->QueryInterface(IID_IADs, (void **)&pIADs);
  168. BAIL_ON_FAILURE(hr);
  169. //
  170. // Determine the object class of the enumerated object and the corresponding
  171. // object class ID number (as specified in the Filters global array).
  172. //
  173. hr = pIADs->get_Class(&pszClass);
  174. BAIL_ON_FAILURE(hr);
  175. hr = IsValidFilter(pszClass, &dwClassID, gpFilters, gdwMaxFilters);
  176. if (SUCCEEDED(hr)) {
  177. //
  178. // Enumerate through the object classes listed in the user-specified filter
  179. // until we either find a match (fFound = TRUE) or we reach the end of the
  180. // list.
  181. //
  182. hr = _pObjList->Reset();
  183. while (SUCCEEDED(hr)) {
  184. hr = _pObjList->GetCurrentObject(&dwFilterID);
  185. if (SUCCEEDED(hr)
  186. && (dwFilterID == dwClassID)
  187. ) {
  188. fFound = TRUE;
  189. break;
  190. }
  191. hr = _pObjList->Next();
  192. }
  193. if (!fFound) {
  194. //
  195. // not on the list of objects to return, try again
  196. // with the next member of the group
  197. //
  198. pDispatch->Release();
  199. pIADs->Release();
  200. if (pszClass) {
  201. ADsFreeString(pszClass);
  202. }
  203. continue;
  204. }
  205. }
  206. pIADs->Release();
  207. if (pszClass) {
  208. ADsFreeString(pszClass);
  209. }
  210. //
  211. // Return it.
  212. //
  213. VariantInit(&pvar[i]);
  214. pvar[i].vt = VT_DISPATCH;
  215. pvar[i].pdispVal = pDispatch;
  216. (*pcElementFetched)++;
  217. i++;
  218. }
  219. RRETURN_EXP_IF_ERR(hr);
  220. error:
  221. if (pDispatch) {
  222. pDispatch->Release();
  223. }
  224. if (pIADs) {
  225. pIADs->Release();
  226. }
  227. if (pszClass) {
  228. ADsFreeString(pszClass);
  229. }
  230. RRETURN_EXP_IF_ERR(hr);
  231. }
  232. HRESULT
  233. CWinNTLocalGroupCollectionEnum::GetComputerMemberObject(
  234. IDispatch ** ppDispatch
  235. )
  236. {
  237. HRESULT hr = S_OK;
  238. LPCOMPUTER_GROUP_MEMBER pComputerGrpMember = NULL;
  239. LPBYTE pBuffer = NULL;
  240. DWORD dwReturned = 0;
  241. BOOL dwRet = 0;
  242. if (!_hGroup) {
  243. dwRet = WinNTLocalGroupOpen(
  244. _DomainName,
  245. _ServerName,
  246. _GroupName,
  247. &_hGroup
  248. );
  249. if (!dwRet) {
  250. goto error;
  251. }
  252. }
  253. dwRet = WinNTLocalGroupEnum(
  254. _hGroup,
  255. 1,
  256. &pBuffer,
  257. &dwReturned
  258. );
  259. if (!dwRet) {
  260. goto error;
  261. }
  262. pComputerGrpMember = (LPCOMPUTER_GROUP_MEMBER)pBuffer;
  263. switch (pComputerGrpMember->Type) {
  264. case WINNT_USER_ID :
  265. hr = CWinNTUser::CreateUser(
  266. pComputerGrpMember->Parent,
  267. pComputerGrpMember->ParentType,
  268. pComputerGrpMember->Domain,
  269. pComputerGrpMember->Computer,
  270. pComputerGrpMember->Name,
  271. ADS_OBJECT_BOUND,
  272. NULL, // UserFlags
  273. NULL, // FullName
  274. NULL, // Description
  275. pComputerGrpMember->Sid,
  276. IID_IDispatch,
  277. _Credentials,
  278. (void **)ppDispatch
  279. );
  280. break;
  281. case WINNT_GROUP_ID:
  282. case WINNT_LOCALGROUP_ID:
  283. hr = CWinNTGroup::CreateGroup(
  284. pComputerGrpMember->Parent,
  285. pComputerGrpMember->ParentType,
  286. pComputerGrpMember->Domain,
  287. pComputerGrpMember->Computer,
  288. pComputerGrpMember->Name,
  289. pComputerGrpMember->Type == WINNT_GROUP_ID ?
  290. WINNT_GROUP_GLOBAL :
  291. WINNT_GROUP_LOCAL,
  292. ADS_OBJECT_BOUND,
  293. pComputerGrpMember->Sid,
  294. IID_IDispatch,
  295. _Credentials,
  296. (void **)ppDispatch
  297. );
  298. break;
  299. default:
  300. goto error;
  301. }
  302. BAIL_ON_FAILURE(hr);
  303. hr = S_OK;
  304. cleanup:
  305. if (pBuffer) {
  306. FreeADsMem(pBuffer);
  307. }
  308. RRETURN(hr);
  309. error:
  310. *ppDispatch = NULL;
  311. hr = S_FALSE;
  312. goto cleanup;
  313. }
  314. //+---------------------------------------------------------------------------
  315. //
  316. // Function: CWinNTLocalGroupCollectionEnum::Next
  317. //
  318. // Synopsis: Returns cElements number of requested NetOle objects in the
  319. // array supplied in pvar.
  320. //
  321. // Arguments: [cElements] -- The number of elements requested by client
  322. // [pvar] -- ptr to array of VARIANTs to for return objects
  323. // [pcElementFetched] -- if non-NULL, then number of elements
  324. // -- actually returned is placed here
  325. //
  326. // Returns: HRESULT -- S_OK if number of elements requested are returned
  327. // -- S_FALSE if number of elements is < requested
  328. //
  329. // Modifies:
  330. //
  331. // History: 11-3-95 krishnag Created.
  332. //
  333. //----------------------------------------------------------------------------
  334. STDMETHODIMP
  335. CWinNTLocalGroupCollectionEnum::Next(
  336. ULONG cElements,
  337. VARIANT FAR* pvar,
  338. ULONG FAR* pcElementFetched
  339. )
  340. {
  341. ULONG cElementFetched = 0;
  342. HRESULT hr = S_OK;
  343. hr = EnumGroupMembers(
  344. cElements,
  345. pvar,
  346. &cElementFetched
  347. );
  348. if (pcElementFetched) {
  349. *pcElementFetched = cElementFetched;
  350. }
  351. RRETURN_EXP_IF_ERR(hr);
  352. }