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.

473 lines
9.3 KiB

  1. //----------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996
  5. //
  6. // File: enum.cxx
  7. //
  8. // Contents: Active Directory container enumeration
  9. //
  10. //
  11. // History: 03-20-96 KrishnaG created
  12. // 08-01-96 t-danal add to oledscmd
  13. //
  14. //----------------------------------------------------------------------------
  15. #include "main.hxx"
  16. #include "macro.hxx"
  17. #include "sconv.hxx"
  18. #define DEFAULT_ACTION "obj"
  19. //
  20. // Dispatch Table Defs
  21. //
  22. #include "dispdef.hxx"
  23. DEFEXEC(ExecEnumObjects);
  24. DEFEXEC(ExecEnumJobs);
  25. DEFEXEC(ExecEnumSessions);
  26. DEFEXEC(ExecEnumResources);
  27. DEFDISPTABLE(DispTable) = {
  28. {DEFAULT_ACTION, NULL, ExecEnumObjects},
  29. {"job", NULL, ExecEnumJobs},
  30. {"ses", NULL, ExecEnumSessions},
  31. {"res", NULL, ExecEnumResources}
  32. };
  33. DEFDISPSIZE(nDispTable, DispTable);
  34. //
  35. // Private defines
  36. //
  37. #define MAX_ADS_FILTERS 10
  38. #define MAX_ADS_ENUM 100
  39. //
  40. // Imported functions
  41. //
  42. HRESULT
  43. DumpObject(
  44. IADs * pADs
  45. );
  46. HRESULT
  47. GetTransientObjects(
  48. LPWSTR szContainer,
  49. LPWSTR szType,
  50. IADs **ppADs,
  51. IADsCollection **ppCollection
  52. );
  53. //
  54. // Local functions
  55. //
  56. HRESULT
  57. EnumTransientObjects(
  58. LPWSTR szADsPath,
  59. LPWSTR szObjectType,
  60. BOOL fDump
  61. );
  62. HRESULT
  63. EnumObject(
  64. LPWSTR szLocation,
  65. LPWSTR * lppPathNames,
  66. DWORD dwPathNames
  67. ) ;
  68. HRESULT
  69. PrintLongFormat(
  70. IADs * pObject
  71. );
  72. HRESULT
  73. PrintShortFormat(
  74. IADs * pObject
  75. );
  76. //
  77. // Local function definitions
  78. //
  79. HRESULT
  80. EnumTransientObjects(
  81. LPWSTR szADsPath,
  82. LPWSTR szObjectType,
  83. BOOL fDump
  84. )
  85. {
  86. HRESULT hr;
  87. IADs * pADs = NULL;
  88. IADsCollection * pCollection = NULL;
  89. ULONG ulGet;
  90. IEnumVARIANT *pIEnumVar;
  91. IADs * pChildADs = NULL;
  92. VARIANT aVariant;
  93. hr = GetTransientObjects(szADsPath,
  94. szObjectType,
  95. &pADs,
  96. &pCollection);
  97. BAIL_ON_FAILURE(hr);
  98. //
  99. // printf("ADs Get objects succeeded \n");
  100. //
  101. pCollection->get__NewEnum((IUnknown **)&pIEnumVar);
  102. BAIL_ON_FAILURE(hr);
  103. hr = pIEnumVar->Next( 1, &aVariant, &ulGet );
  104. while( ulGet && hr != S_FALSE )
  105. {
  106. hr = V_DISPATCH(&aVariant)->QueryInterface(IID_IADs,
  107. (void**)&pChildADs);
  108. V_DISPATCH(&aVariant)->Release();
  109. if(pChildADs != NULL){
  110. hr = pADs->GetInfo();
  111. BAIL_ON_FAILURE(hr);
  112. if (fDump)
  113. printf("\n");
  114. hr = PrintShortFormat(pChildADs);
  115. BAIL_ON_FAILURE(hr);
  116. if (fDump) {
  117. hr = DumpObject(pChildADs);
  118. BAIL_ON_FAILURE(hr);
  119. //
  120. // printf("Dump objects succeeded \n");
  121. //
  122. }
  123. hr = pIEnumVar->Next( 1, &aVariant, &ulGet );
  124. }
  125. }
  126. hr = S_OK;
  127. error:
  128. if(pADs)
  129. pADs->Release();
  130. if(pCollection)
  131. pCollection->Release();
  132. if (hr == S_FALSE)
  133. hr = S_OK;
  134. return(hr);
  135. }
  136. HRESULT
  137. EnumObject(
  138. LPWSTR szLocation,
  139. LPWSTR * lppPathNames,
  140. DWORD dwPathNames
  141. )
  142. {
  143. ULONG cElementFetched = 0L;
  144. IEnumVARIANT * pEnumVariant = NULL;
  145. VARIANT Variant, * pVarFilter = NULL, VariantArray[MAX_ADS_ENUM];
  146. HRESULT hr;
  147. VARIANT VarFilter;
  148. IADsContainer * pADsContainer = NULL;
  149. DWORD dwObjects = 0, dwEnumCount = 0, i = 0;
  150. BOOL fContinue = TRUE;
  151. VariantInit(&VarFilter);
  152. hr = ADsGetObject(
  153. szLocation,
  154. IID_IADsContainer,
  155. (void **)&pADsContainer
  156. );
  157. BAIL_ON_FAILURE(hr);
  158. hr = ADsBuildVarArrayStr(
  159. lppPathNames,
  160. dwPathNames,
  161. &VarFilter
  162. );
  163. BAIL_ON_FAILURE(hr);
  164. hr = pADsContainer->put_Filter(VarFilter);
  165. BAIL_ON_FAILURE(hr);
  166. hr = ADsBuildEnumerator(
  167. pADsContainer,
  168. &pEnumVariant
  169. );
  170. BAIL_ON_FAILURE(hr);
  171. while (fContinue) {
  172. IADs *pObject ;
  173. hr = ADsEnumerateNext(
  174. pEnumVariant,
  175. MAX_ADS_ENUM,
  176. VariantArray,
  177. &cElementFetched
  178. );
  179. if (hr == S_FALSE) {
  180. fContinue = FALSE;
  181. }
  182. dwEnumCount++;
  183. for (i = 0; i < cElementFetched; i++ ) {
  184. IDispatch *pDispatch = NULL;
  185. BSTR bstrName ;
  186. pDispatch = VariantArray[i].pdispVal;
  187. hr = pDispatch->QueryInterface(IID_IADs,
  188. (VOID **) &pObject) ;
  189. BAIL_ON_FAILURE(hr);
  190. PrintLongFormat(pObject);
  191. pObject->Release();
  192. pDispatch->Release();
  193. }
  194. memset(VariantArray, 0, sizeof(VARIANT)*MAX_ADS_ENUM);
  195. dwObjects += cElementFetched;
  196. }
  197. printf("Total Number of Objects enumerated is %d\n", dwObjects);
  198. VariantClear(&VarFilter);
  199. if (pEnumVariant) {
  200. pEnumVariant->Release();
  201. }
  202. return(S_OK);
  203. error:
  204. VariantClear(&VarFilter);
  205. if (pEnumVariant) {
  206. pEnumVariant->Release();
  207. }
  208. return(hr);
  209. }
  210. HRESULT
  211. PrintLongFormat(IADs * pObject)
  212. {
  213. HRESULT hr = S_OK;
  214. BSTR bstrName = NULL;
  215. BSTR bstrClass = NULL;
  216. BSTR bstrSchema = NULL;
  217. hr = pObject->get_Name(&bstrName) ;
  218. BAIL_ON_FAILURE(hr);
  219. hr = pObject->get_Class(&bstrClass);
  220. BAIL_ON_FAILURE(hr);
  221. hr = pObject->get_Schema(&bstrSchema);
  222. printf("\tObject: %ws\tSchema: %ws\n", bstrName, bstrSchema) ;
  223. SysFreeString(bstrName) ;
  224. error:
  225. if (bstrClass) {
  226. SysFreeString(bstrClass);
  227. }
  228. if (bstrName) {
  229. SysFreeString(bstrName);
  230. }
  231. if (bstrSchema) {
  232. SysFreeString(bstrSchema);
  233. }
  234. return(hr);
  235. }
  236. HRESULT
  237. PrintShortFormat(IADs * pObject)
  238. {
  239. HRESULT hr = S_OK;
  240. BSTR bstrName = NULL;
  241. hr = pObject->get_Name(&bstrName);
  242. BAIL_ON_FAILURE(hr);
  243. printf("\tObject: %ws\n", bstrName);
  244. SysFreeString(bstrName) ;
  245. error:
  246. if (bstrName) {
  247. SysFreeString(bstrName);
  248. }
  249. return(hr);
  250. }
  251. //
  252. // Exec function definitions
  253. //
  254. int
  255. ExecEnum(char *szProgName, char *szAction, int argc, char * argv[])
  256. {
  257. if (!argc) {
  258. PrintUsage(szProgName, szAction, DispTable, nDispTable);
  259. return(1);
  260. }
  261. char *szPrevActions = szAction;
  262. szAction = argv[0];
  263. argc--;
  264. argv++;
  265. if (DoHelp(szProgName,
  266. szPrevActions, szAction, NULL,
  267. DispTable, nDispTable,
  268. NULL))
  269. return 0;
  270. if (!IsValidAction(szAction, DispTable, nDispTable))
  271. szAction = DEFAULT_ACTION;
  272. return DispatchExec(DispTable, nDispTable,
  273. szProgName,
  274. szPrevActions, szAction,
  275. argc, argv);
  276. }
  277. int
  278. ExecEnumObjects(char *szProgName, char *szAction, int argc, char * argv[])
  279. {
  280. HRESULT hr;
  281. LPWSTR pszLocation = NULL ;
  282. LPWSTR pszTmp = NULL ;
  283. LPWSTR apszTypes[MAX_ADS_FILTERS] ;
  284. if ((argc < 1) || (argc > 3)) {
  285. PrintUsage(szProgName, szAction, "<path> [<type> [<other_args>]]");
  286. return(1) ;
  287. }
  288. int i = 0 ;
  289. BAIL_ON_NULL( pszLocation = AllocateUnicodeString(argv[0]));
  290. apszTypes[0] = NULL ;
  291. if (argc > 1) {
  292. LPWSTR pszComma ;
  293. BAIL_ON_NULL(pszTmp = AllocateUnicodeString(argv[1]));
  294. apszTypes[i] = pszTmp ;
  295. while (pszComma = wcschr(apszTypes[i++],L',')) {
  296. apszTypes[i] = pszComma + 1;
  297. *pszComma = 0 ;
  298. }
  299. apszTypes[i] = NULL ;
  300. }
  301. hr = EnumObject(
  302. pszLocation,
  303. apszTypes,
  304. i
  305. );
  306. error:
  307. FreeUnicodeString(pszLocation) ;
  308. FreeUnicodeString(pszTmp) ;
  309. if (FAILED(hr))
  310. return(1);
  311. return(0) ;
  312. }
  313. int
  314. ExecEnumJobs(char *szProgName, char *szAction, int argc, char * argv[])
  315. {
  316. HRESULT hr;
  317. LPWSTR szPath = NULL;
  318. if (argc != 1) {
  319. PrintUsage(szProgName, szAction, "<conatiner>");
  320. return(1) ;
  321. }
  322. BAIL_ON_NULL(szPath = AllocateUnicodeString(argv[0]));
  323. hr = EnumTransientObjects(szPath,
  324. L"Job",
  325. TRUE);
  326. error:
  327. FreeUnicodeString(szPath);
  328. if (FAILED(hr))
  329. return(1);
  330. return(0) ;
  331. }
  332. int
  333. ExecEnumSessions(char *szProgName, char *szAction, int argc, char * argv[])
  334. {
  335. HRESULT hr;
  336. LPWSTR szPath = NULL;
  337. if (argc != 1) {
  338. PrintUsage(szProgName, szAction, "<conatiner>");
  339. return(1) ;
  340. }
  341. BAIL_ON_NULL(szPath = AllocateUnicodeString(argv[0]));
  342. hr = EnumTransientObjects(szPath,
  343. L"Session",
  344. TRUE);
  345. error:
  346. FreeUnicodeString(szPath);
  347. if (FAILED(hr))
  348. return(1);
  349. return(0) ;
  350. }
  351. int
  352. ExecEnumResources(char *szProgName, char *szAction, int argc, char * argv[])
  353. {
  354. HRESULT hr;
  355. LPWSTR szPath = NULL;
  356. if (argc != 1) {
  357. PrintUsage(szProgName, szAction, "<conatiner>");
  358. return(1) ;
  359. }
  360. BAIL_ON_NULL(szPath = AllocateUnicodeString(argv[0]));
  361. hr = EnumTransientObjects(szPath,
  362. L"Resource",
  363. TRUE);
  364. error:
  365. FreeUnicodeString(szPath);
  366. if (FAILED(hr))
  367. return(1);
  368. return(0) ;
  369. }