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.

352 lines
7.3 KiB

  1. //----------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996
  5. //
  6. // File: group.cxx
  7. //
  8. // Contents: Group membership
  9. //
  10. // History: 08-06-96 t-danal created from grpmem, grmemadd, grmemdel
  11. //
  12. //----------------------------------------------------------------------------
  13. #include "main.hxx"
  14. #include "macro.hxx"
  15. #include "sconv.hxx"
  16. //
  17. // Dispatch Table Defs
  18. //
  19. #include "dispdef.hxx"
  20. DEFEXEC(ExecGroupMembers);
  21. DEFEXEC(ExecGroupAddMember);
  22. DEFEXEC(ExecGroupDelMember);
  23. DEFDISPTABLE(DispTable) = {
  24. {"mem", NULL, ExecGroupMembers},
  25. {"add", NULL, ExecGroupAddMember},
  26. {"del", NULL, ExecGroupDelMember}
  27. };
  28. DEFDISPSIZE(nDispTable, DispTable);
  29. //
  30. // Private defines
  31. //
  32. #define MAX_ADS_ENUM 100
  33. //
  34. // Local functions
  35. //
  36. HRESULT
  37. GroupEnumObject(
  38. LPWSTR szLocation
  39. ) ;
  40. HRESULT
  41. GroupAddObject(
  42. LPWSTR szLocation,
  43. LPWSTR szPath
  44. );
  45. HRESULT
  46. GroupRemoverObject(
  47. LPWSTR szLocation,
  48. LPWSTR szPath
  49. );
  50. //
  51. // Local function definitions
  52. //
  53. HRESULT
  54. GroupEnumObject(
  55. LPWSTR szLocation
  56. )
  57. {
  58. HRESULT hr;
  59. IADsGroup * pADsGroup = NULL;
  60. IADsMembers * pADsCollection = NULL;
  61. DWORD dwObjects = 0, i = 0;
  62. BOOL fContinue = TRUE;
  63. BSTR bstrADsPath = NULL;
  64. ULONG cElementFetched = 0L;
  65. IEnumVARIANT * pEnumVariant = NULL;
  66. IUnknown * pUnknown = NULL;
  67. VARIANT VariantArray[MAX_ADS_ENUM];
  68. IADs *pObject = NULL;
  69. IDispatch *pDispatch = NULL;
  70. BSTR bstrName = NULL;
  71. hr = ADsGetObject(
  72. szLocation,
  73. IID_IADsGroup,
  74. (void **)&pADsGroup
  75. );
  76. BAIL_ON_FAILURE(hr);
  77. hr = pADsGroup->Members(&pADsCollection);
  78. BAIL_ON_FAILURE(hr);
  79. hr = pADsCollection->get__NewEnum(&pUnknown);
  80. BAIL_ON_FAILURE(hr);
  81. hr = pUnknown->QueryInterface(IID_IEnumVARIANT, (void **)&pEnumVariant);
  82. BAIL_ON_FAILURE(hr);
  83. hr = pADsGroup->get_ADsPath(&bstrADsPath);
  84. BAIL_ON_FAILURE(hr);
  85. printf("Group: %ws\n", bstrADsPath);
  86. FREE_BSTR(bstrADsPath);
  87. while (fContinue) {
  88. hr = ADsEnumerateNext(
  89. pEnumVariant,
  90. MAX_ADS_ENUM,
  91. VariantArray,
  92. &cElementFetched
  93. );
  94. if (hr == S_FALSE)
  95. fContinue = FALSE;
  96. for (i = 0; i < cElementFetched; i++ ) {
  97. pDispatch = VariantArray[i].pdispVal;
  98. hr = pDispatch->QueryInterface(IID_IADs,
  99. (VOID **) &pObject) ;
  100. BAIL_ON_FAILURE(hr);
  101. hr = pObject->get_ADsPath(&bstrName) ;
  102. BAIL_ON_FAILURE(hr);
  103. printf("\tMember Object: %ws\n", bstrName);
  104. FREE_BSTR(bstrName);
  105. FREE_INTERFACE(pObject);
  106. FREE_INTERFACE(pDispatch);
  107. }
  108. memset(VariantArray, 0, sizeof(VARIANT)*MAX_ADS_ENUM);
  109. dwObjects += cElementFetched;
  110. }
  111. printf("Total Number of Objects enumerated is %d\n", dwObjects);
  112. hr = S_OK;
  113. error:
  114. FREE_INTERFACE(pADsGroup);
  115. FREE_INTERFACE(pADsCollection);
  116. FREE_INTERFACE(pEnumVariant);
  117. FREE_INTERFACE(pUnknown);
  118. FREE_INTERFACE(pObject);
  119. FREE_INTERFACE(pDispatch);
  120. FREE_BSTR(bstrName);
  121. FREE_BSTR(bstrADsPath);
  122. return(hr);
  123. }
  124. HRESULT
  125. GroupAddObject(
  126. LPWSTR szLocation,
  127. LPWSTR szPath
  128. )
  129. {
  130. HRESULT hr;
  131. IADsGroup * pADsGroup = NULL;
  132. hr = ADsGetObject(
  133. szLocation,
  134. IID_IADsGroup,
  135. (void **)&pADsGroup
  136. );
  137. BAIL_ON_FAILURE(hr);
  138. hr = pADsGroup->Add(
  139. szPath
  140. );
  141. pADsGroup->Release();
  142. BAIL_ON_FAILURE(hr);
  143. printf("grmemadd: Successfully added %ws to %ws\n", szPath, szLocation);
  144. return(S_OK);
  145. error:
  146. printf("grmemadd: Failed to add %ws to %ws\n", szPath, szLocation);
  147. return(hr);
  148. }
  149. HRESULT
  150. GroupRemoveObject(
  151. LPWSTR szLocation,
  152. LPWSTR szPath
  153. )
  154. {
  155. HRESULT hr;
  156. IADsGroup * pADsGroup = NULL;
  157. hr = ADsGetObject(
  158. szLocation,
  159. IID_IADsGroup,
  160. (void **)&pADsGroup
  161. );
  162. BAIL_ON_FAILURE(hr);
  163. hr = pADsGroup->Remove(
  164. szPath
  165. );
  166. pADsGroup->Release();
  167. BAIL_ON_FAILURE(hr);
  168. printf("grmemdel: Successfully removed %ws from %ws\n",
  169. szPath, szLocation);
  170. return(S_OK);
  171. error:
  172. printf("grmemdel: Failed to remove %ws from %ws\n",
  173. szPath, szLocation);
  174. return(hr);
  175. }
  176. //
  177. // Exec function definitions
  178. //
  179. int
  180. ExecGroup(char *szProgName, char *szAction, int argc, char * argv[])
  181. {
  182. if (!argc) {
  183. PrintUsage(szProgName, szAction, DispTable, nDispTable);
  184. return(1);
  185. }
  186. char *szPrevActions = szAction;
  187. szAction = argv[0];
  188. argc--;
  189. argv++;
  190. if (DoHelp(szProgName,
  191. szPrevActions, szAction, NULL,
  192. DispTable, nDispTable,
  193. NULL))
  194. return 0;
  195. return DispatchExec(DispTable, nDispTable,
  196. szProgName,
  197. szPrevActions, szAction,
  198. argc, argv);
  199. }
  200. int
  201. ExecGroupMembers(char *szProgName, char *szAction, int argc, char * argv[])
  202. {
  203. HRESULT hr;
  204. LPWSTR pszLocation = NULL ;
  205. if (argc != 1) {
  206. PrintUsage(szProgName, szAction, "<ADsPath of Group Object>");
  207. return(1);
  208. }
  209. pszLocation = AllocateUnicodeString(argv[0]);
  210. if (!pszLocation) {
  211. return(1);
  212. }
  213. hr = GroupEnumObject(
  214. pszLocation
  215. );
  216. FreeUnicodeString(pszLocation) ;
  217. if (FAILED(hr))
  218. return(1);
  219. return(0) ;
  220. }
  221. int
  222. ExecGroupAddMember(char *szProgName, char *szAction, int argc, char * argv[])
  223. {
  224. HRESULT hr;
  225. LPWSTR pszLocation = NULL ;
  226. LPWSTR pszPath = NULL;
  227. if (argc != 2) {
  228. PrintUsage(szProgName, szAction,
  229. "<Group Path> <Member Path>");
  230. return(0);
  231. }
  232. pszLocation = AllocateUnicodeString(argv[0]);
  233. if (!pszLocation) {
  234. hr = E_FAIL;
  235. BAIL_ON_FAILURE(hr);
  236. }
  237. pszPath = AllocateUnicodeString(argv[1]);
  238. if (!pszPath) {
  239. hr = E_FAIL;
  240. BAIL_ON_FAILURE(hr);
  241. }
  242. hr = GroupAddObject(
  243. pszLocation,
  244. pszPath
  245. );
  246. error:
  247. FreeUnicodeString(pszLocation) ;
  248. FreeUnicodeString(pszPath);
  249. if (FAILED(hr))
  250. return(1);
  251. return(0) ;
  252. }
  253. int
  254. ExecGroupDelMember(char *szProgName, char *szAction, int argc, char * argv[])
  255. {
  256. HRESULT hr;
  257. LPWSTR pszLocation = NULL ;
  258. LPWSTR pszPath = NULL;
  259. if (argc != 2) {
  260. PrintUsage(szProgName, szAction,
  261. "<Group Path> <Member Path>");
  262. return(1);
  263. }
  264. pszLocation = AllocateUnicodeString(argv[0]);
  265. if (!pszLocation) {
  266. hr = E_FAIL;
  267. BAIL_ON_FAILURE(hr);
  268. }
  269. pszPath = AllocateUnicodeString(argv[1]);
  270. if (!pszPath) {
  271. hr = E_FAIL;
  272. BAIL_ON_FAILURE(hr);
  273. }
  274. hr = GroupRemoveObject(
  275. pszLocation,
  276. pszPath
  277. );
  278. error:
  279. FreeUnicodeString(pszLocation) ;
  280. FreeUnicodeString(pszPath);
  281. if (FAILED(hr))
  282. return(1);
  283. return(0) ;
  284. }