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.

622 lines
17 KiB

  1. //----------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1993.
  5. //
  6. // File: drt.cxx
  7. //
  8. // Contents: Main for OleDs DRT
  9. //
  10. //
  11. // History: 28-Oct-94 KrishnaG, created OleDs DRT
  12. // 28-Oct-94 ChuckC, rewritten.
  13. //
  14. //----------------------------------------------------------------------------
  15. //
  16. // System Includes
  17. //
  18. #define INC_OLE2
  19. #include <windows.h>
  20. //
  21. // CRunTime Includes
  22. //
  23. #include <stdlib.h>
  24. #include <limits.h>
  25. #include <io.h>
  26. #include <stdio.h>
  27. //
  28. // Public OleDs includes
  29. //
  30. //
  31. // Private defines
  32. //
  33. #define BAIL_ON_NULL(p) \
  34. if (!(p)) { \
  35. goto error; \
  36. }
  37. #define BAIL_ON_FAILURE(hr) \
  38. if (FAILED(hr)) { \
  39. goto error; \
  40. }
  41. #include "activeds.h"
  42. #include "main.hxx"
  43. #include "adsi.h"
  44. //
  45. // Globals representing the parameters
  46. //
  47. LPWSTR pszSearchBase, pszSearchFilter, pszAttrNames[10], pszAttrList;
  48. DWORD dwNumberAttributes = -1;
  49. //
  50. // Preferences
  51. //
  52. BOOL fASynchronous=FALSE, fDerefAliases=FALSE, fAttrsOnly=FALSE;
  53. DWORD fSizeLimit, fTimeLimit, dwTimeOut, dwPageSize, dwSearchScope;
  54. ADS_SEARCHPREF_INFO pSearchPref[10];
  55. DWORD dwCurrPref = 0;
  56. LPWSTR pszUserName=NULL, pszPassword=NULL;
  57. DWORD dwAuthFlags=0;
  58. DWORD cErr=0;
  59. char *prefNameLookup[] =
  60. {
  61. "ADS_SEARCHPREF_ASYNCHRONOUS",
  62. "ADS_SEARCHPREF_DEREF_ALIASES",
  63. "ADS_SEARCHPREF_SIZE_LIMIT",
  64. "ADS_SEARCHPREF_TIME_LIMIT",
  65. "ADS_SEARCHPREF_ATTRIBTYPES_ONLY",
  66. "ADS_SEARCHPREF_SEARCH_SCOPE",
  67. "ADS_SEARCHPREF_TIMEOUT",
  68. "ADS_SEARCHPREF_PAGESIZE",
  69. "ADS_SEARCHPREF_PAGED_TIME_LIMIT",
  70. "ADS_SEARCHPREF_CHASE_REFERRALS"
  71. };
  72. HRESULT
  73. ConvertTrusteeToSid(
  74. LPWSTR bstrTrustee,
  75. PSID * ppSid,
  76. PDWORD pdwSidSize
  77. );
  78. //+---------------------------------------------------------------------------
  79. //
  80. // Function: main
  81. //
  82. // Synopsis:
  83. //
  84. //----------------------------------------------------------------------------
  85. INT __cdecl
  86. main(int argc, char * argv[])
  87. {
  88. HRESULT hr=S_OK;
  89. HANDLE handle = NULL;
  90. ADS_SEARCH_HANDLE hSearchHandle=NULL;
  91. ADS_SEARCH_COLUMN Column;
  92. DWORD nRows = 0, i;
  93. LPWSTR pszColumnName = NULL;
  94. LPWSTR pszDest = NULL;
  95. LPBYTE pSid = NULL;
  96. DWORD dwSize = 0;
  97. #if 0 // Enable if you want to test binary values in filters and send
  98. // pszBinaryFilter instead of pszSearchFilter in ExecuteSearch
  99. WCHAR pszBinaryFilter[256] = L"objectSid=";
  100. LPWSTR pszDest = NULL;
  101. BYTE column[100] = {
  102. 0x01, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x15, 0x00,
  103. 0x00, 0x00, 0x59, 0x51, 0xb8, 0x17, 0x66, 0x72, 0x5d, 0x25,
  104. 0x64, 0x63, 0x3b, 0x0b, 0x29, 0x99, 0x21, 0x00 };
  105. hr = ADsEncodeBinaryData (
  106. column,
  107. 28,
  108. &pszDest
  109. );
  110. wcscat( pszBinaryFilter, pszDest );
  111. FreeADsMem( pszDest );
  112. #endif
  113. //
  114. // Sets the global variables with the parameters
  115. //
  116. hr = ProcessArgs(argc, argv);
  117. BAIL_ON_FAILURE(hr);
  118. hr = ADSIOpenDSObject(
  119. pszSearchBase,
  120. pszUserName,
  121. pszPassword,
  122. dwAuthFlags,
  123. &handle
  124. );
  125. BAIL_ON_FAILURE(hr);
  126. if (dwCurrPref) {
  127. hr = ADSISetSearchPreference(
  128. handle,
  129. pSearchPref,
  130. dwCurrPref
  131. );
  132. BAIL_ON_FAILURE(hr);
  133. if (hr != S_OK) {
  134. for (i=0; i<dwCurrPref; i++) {
  135. if (pSearchPref[i].dwStatus != ADS_STATUS_S_OK) {
  136. printf(
  137. "Error in setting the preference %s: status = %d\n",
  138. prefNameLookup[pSearchPref[i].dwSearchPref],
  139. pSearchPref[i].dwStatus
  140. );
  141. cErr++;
  142. }
  143. }
  144. }
  145. }
  146. hr = ADSIExecuteSearch(
  147. handle,
  148. pszSearchFilter,
  149. pszAttrNames,
  150. dwNumberAttributes,
  151. &hSearchHandle
  152. );
  153. BAIL_ON_FAILURE(hr);
  154. hr = ADSIGetNextRow(
  155. handle,
  156. hSearchHandle
  157. );
  158. BAIL_ON_FAILURE(hr);
  159. while (hr != S_ADS_NOMORE_ROWS) {
  160. nRows++;
  161. if (dwNumberAttributes == -1) {
  162. hr = ADSIGetNextColumnName(
  163. handle,
  164. hSearchHandle,
  165. &pszColumnName
  166. );
  167. BAIL_ON_FAILURE(hr);
  168. while (hr != S_ADS_NOMORE_COLUMNS) {
  169. hr = ADSIGetColumn(
  170. handle,
  171. hSearchHandle,
  172. pszColumnName,
  173. &Column
  174. );
  175. if (FAILED(hr) && hr != E_ADS_COLUMN_NOT_SET)
  176. goto error;
  177. if (SUCCEEDED(hr)) {
  178. PrintColumn(&Column, pszColumnName);
  179. ADSIFreeColumn(
  180. handle,
  181. &Column
  182. );
  183. }
  184. FreeADsMem(pszColumnName);
  185. hr = ADSIGetNextColumnName(
  186. handle,
  187. hSearchHandle,
  188. &pszColumnName
  189. );
  190. BAIL_ON_FAILURE(hr);
  191. }
  192. printf("\n");
  193. }
  194. else {
  195. for (int i=0; i<dwNumberAttributes; i++) {
  196. hr = ADSIGetColumn(
  197. handle,
  198. hSearchHandle,
  199. pszAttrNames[i],
  200. &Column
  201. );
  202. if (hr == E_ADS_COLUMN_NOT_SET)
  203. continue;
  204. BAIL_ON_FAILURE(hr);
  205. PrintColumn(&Column, pszAttrNames[i]);
  206. ADSIFreeColumn(
  207. handle,
  208. &Column
  209. );
  210. }
  211. printf("\n");
  212. }
  213. hr = ADSIGetNextRow(
  214. handle,
  215. hSearchHandle
  216. );
  217. BAIL_ON_FAILURE(hr);
  218. }
  219. wprintf (L"Total Rows: %d\n", nRows);
  220. if (cErr) {
  221. wprintf (L"%d warning(s) ignored", cErr);
  222. }
  223. if (hSearchHandle)
  224. ADSICloseSearchHandle(
  225. handle,
  226. hSearchHandle
  227. );
  228. if ( handle )
  229. ADSICloseDSObject( handle );
  230. FREE_UNICODE_STRING(pszSearchBase) ;
  231. FREE_UNICODE_STRING(pszSearchFilter) ;
  232. FREE_UNICODE_STRING(pszAttrList) ;
  233. return(0) ;
  234. error:
  235. if (hSearchHandle)
  236. ADSICloseSearchHandle(
  237. handle,
  238. hSearchHandle
  239. );
  240. if ( handle )
  241. ADSICloseDSObject( handle );
  242. FREE_UNICODE_STRING(pszSearchBase) ;
  243. FREE_UNICODE_STRING(pszSearchFilter) ;
  244. FREE_UNICODE_STRING(pszAttrList) ;
  245. FREE_UNICODE_STRING(pszUserName) ;
  246. FREE_UNICODE_STRING(pszPassword) ;
  247. wprintf (L"Error! hr = 0x%x\n", hr);
  248. return(1) ;
  249. }
  250. //+---------------------------------------------------------------------------
  251. //
  252. // Function: ProcessArgs
  253. //
  254. // Synopsis:
  255. //
  256. //----------------------------------------------------------------------------
  257. HRESULT
  258. ProcessArgs(
  259. int argc,
  260. char * argv[]
  261. )
  262. {
  263. argc--;
  264. int currArg = 1;
  265. LPWSTR pTemp;
  266. char *pszCurrPref, *pszCurrPrefValue;
  267. while (argc) {
  268. if (argv[currArg][0] != '/' && argv[currArg][0] != '-')
  269. BAIL_ON_FAILURE (E_FAIL);
  270. switch (argv[currArg][1]) {
  271. case 'b':
  272. argc--;
  273. currArg++;
  274. if (argc <= 0)
  275. BAIL_ON_FAILURE (E_FAIL);
  276. pszSearchBase = AllocateUnicodeString(argv[currArg]);
  277. BAIL_ON_NULL(pszSearchBase);
  278. break;
  279. case 'f':
  280. argc--;
  281. currArg++;
  282. if (argc <= 0)
  283. BAIL_ON_FAILURE (E_FAIL);
  284. pszSearchFilter = AllocateUnicodeString(argv[currArg]);
  285. BAIL_ON_NULL(pszSearchFilter);
  286. break;
  287. case 'a':
  288. argc--;
  289. currArg++;
  290. if (argc <= 0)
  291. BAIL_ON_FAILURE (E_FAIL);
  292. pszAttrList = AllocateUnicodeString(argv[currArg]);
  293. BAIL_ON_NULL(pszAttrList);
  294. dwNumberAttributes = 0;
  295. pTemp = wcstok(pszAttrList, L",");
  296. pszAttrNames[dwNumberAttributes] = RemoveWhiteSpaces(pTemp);
  297. dwNumberAttributes++;
  298. while (pTemp) {
  299. pTemp = wcstok(NULL, L",");
  300. pszAttrNames[dwNumberAttributes] = RemoveWhiteSpaces(pTemp);
  301. dwNumberAttributes++;
  302. }
  303. dwNumberAttributes--;
  304. break;
  305. case 'u':
  306. argc--;
  307. currArg++;
  308. if (argc <= 0)
  309. BAIL_ON_FAILURE (E_FAIL);
  310. pszUserName = AllocateUnicodeString(argv[currArg]);
  311. BAIL_ON_NULL(pszSearchBase);
  312. argc--;
  313. currArg++;
  314. if (argc <= 0)
  315. BAIL_ON_FAILURE (E_FAIL);
  316. pszPassword = AllocateUnicodeString(argv[currArg]);
  317. BAIL_ON_NULL(pszSearchBase);
  318. break;
  319. case 't':
  320. argc--;
  321. currArg++;
  322. if (argc <= 0)
  323. BAIL_ON_FAILURE (E_FAIL);
  324. pszCurrPref = strtok(argv[currArg], "=");
  325. pszCurrPrefValue = strtok(NULL, "=");
  326. if (!pszCurrPref || !pszCurrPrefValue)
  327. BAIL_ON_FAILURE(E_FAIL);
  328. if (!_stricmp(pszCurrPref, "SecureAuth")) {
  329. if (!_stricmp(pszCurrPrefValue, "yes" ))
  330. dwAuthFlags |= ADS_SECURE_AUTHENTICATION;
  331. else if (!_stricmp(pszCurrPrefValue, "no" ))
  332. dwAuthFlags &= ~ADS_SECURE_AUTHENTICATION;
  333. else
  334. BAIL_ON_FAILURE(E_FAIL);
  335. }
  336. else if (!_stricmp(pszCurrPref, "UseEncrypt")) {
  337. if (!_stricmp(pszCurrPrefValue, "yes" ))
  338. dwAuthFlags |= ADS_USE_ENCRYPTION;
  339. else if (!_stricmp(pszCurrPrefValue, "no" ))
  340. dwAuthFlags &= ~ADS_USE_ENCRYPTION;
  341. else
  342. BAIL_ON_FAILURE(E_FAIL);
  343. }
  344. else
  345. BAIL_ON_FAILURE(E_FAIL);
  346. break;
  347. case 'p':
  348. argc--;
  349. currArg++;
  350. if (argc <= 0)
  351. BAIL_ON_FAILURE (E_FAIL);
  352. pszCurrPref = strtok(argv[currArg], "=");
  353. pszCurrPrefValue = strtok(NULL, "=");
  354. if (!pszCurrPref || !pszCurrPrefValue)
  355. BAIL_ON_FAILURE(E_FAIL);
  356. if (!_stricmp(pszCurrPref, "asynchronous")) {
  357. pSearchPref[dwCurrPref].dwSearchPref = ADS_SEARCHPREF_ASYNCHRONOUS;
  358. pSearchPref[dwCurrPref].vValue.dwType = ADSTYPE_BOOLEAN;
  359. if (!_stricmp(pszCurrPrefValue, "yes" ))
  360. pSearchPref[dwCurrPref].vValue.Boolean = TRUE;
  361. else if (!_stricmp(pszCurrPrefValue, "no" ))
  362. pSearchPref[dwCurrPref].vValue.Boolean = FALSE;
  363. else
  364. BAIL_ON_FAILURE(E_FAIL);
  365. }
  366. else if (!_stricmp(pszCurrPref, "attrTypesOnly")) {
  367. pSearchPref[dwCurrPref].dwSearchPref = ADS_SEARCHPREF_ATTRIBTYPES_ONLY;
  368. pSearchPref[dwCurrPref].vValue.dwType = ADSTYPE_BOOLEAN;
  369. if (!_stricmp(pszCurrPrefValue, "yes" ))
  370. pSearchPref[dwCurrPref].vValue.Boolean = TRUE;
  371. else if (!_stricmp(pszCurrPrefValue, "no" ))
  372. pSearchPref[dwCurrPref].vValue.Boolean = FALSE;
  373. else
  374. BAIL_ON_FAILURE(E_FAIL);
  375. }
  376. else if (!_stricmp(pszCurrPref, "derefAliases")) {
  377. pSearchPref[dwCurrPref].dwSearchPref = ADS_SEARCHPREF_DEREF_ALIASES;
  378. pSearchPref[dwCurrPref].vValue.dwType = ADSTYPE_INTEGER;
  379. if (!_stricmp(pszCurrPrefValue, "yes" ))
  380. pSearchPref[dwCurrPref].vValue.Integer = ADS_DEREF_ALWAYS;
  381. else if (!_stricmp(pszCurrPrefValue, "no" ))
  382. pSearchPref[dwCurrPref].vValue.Integer = ADS_DEREF_NEVER;
  383. else
  384. BAIL_ON_FAILURE(E_FAIL);
  385. }
  386. else if (!_stricmp(pszCurrPref, "timeOut")) {
  387. pSearchPref[dwCurrPref].dwSearchPref = ADS_SEARCHPREF_TIMEOUT;
  388. pSearchPref[dwCurrPref].vValue.dwType = ADSTYPE_INTEGER;
  389. pSearchPref[dwCurrPref].vValue.Integer = atoi(pszCurrPrefValue);
  390. }
  391. else if (!_stricmp(pszCurrPref, "timeLimit")) {
  392. pSearchPref[dwCurrPref].dwSearchPref = ADS_SEARCHPREF_TIME_LIMIT;
  393. pSearchPref[dwCurrPref].vValue.dwType = ADSTYPE_INTEGER;
  394. pSearchPref[dwCurrPref].vValue.Integer = atoi(pszCurrPrefValue);
  395. }
  396. else if (!_stricmp(pszCurrPref, "sizeLimit")) {
  397. pSearchPref[dwCurrPref].dwSearchPref = ADS_SEARCHPREF_SIZE_LIMIT;
  398. pSearchPref[dwCurrPref].vValue.dwType = ADSTYPE_INTEGER;
  399. pSearchPref[dwCurrPref].vValue.Integer = atoi(pszCurrPrefValue);
  400. }
  401. else if (!_stricmp(pszCurrPref, "PageSize")) {
  402. pSearchPref[dwCurrPref].dwSearchPref = ADS_SEARCHPREF_PAGESIZE;
  403. pSearchPref[dwCurrPref].vValue.dwType = ADSTYPE_INTEGER;
  404. pSearchPref[dwCurrPref].vValue.Integer = atoi(pszCurrPrefValue);
  405. }
  406. else if (!_stricmp(pszCurrPref, "PagedTimeLimit")) {
  407. pSearchPref[dwCurrPref].dwSearchPref = ADS_SEARCHPREF_PAGED_TIME_LIMIT;
  408. pSearchPref[dwCurrPref].vValue.dwType = ADSTYPE_INTEGER;
  409. pSearchPref[dwCurrPref].vValue.Integer = atoi(pszCurrPrefValue);
  410. }
  411. else if (!_stricmp(pszCurrPref, "SearchScope")) {
  412. pSearchPref[dwCurrPref].dwSearchPref = ADS_SEARCHPREF_SEARCH_SCOPE;
  413. pSearchPref[dwCurrPref].vValue.dwType = ADSTYPE_INTEGER;
  414. if (!_stricmp(pszCurrPrefValue, "Base" ))
  415. pSearchPref[dwCurrPref].vValue.Integer = ADS_SCOPE_BASE;
  416. else if (!_stricmp(pszCurrPrefValue, "OneLevel" ))
  417. pSearchPref[dwCurrPref].vValue.Integer = ADS_SCOPE_ONELEVEL;
  418. else if (!_stricmp(pszCurrPrefValue, "Subtree" ))
  419. pSearchPref[dwCurrPref].vValue.Integer = ADS_SCOPE_SUBTREE;
  420. else
  421. BAIL_ON_FAILURE(E_FAIL);
  422. }
  423. else if (!_stricmp(pszCurrPref, "ChaseReferrals")) {
  424. pSearchPref[dwCurrPref].dwSearchPref = ADS_SEARCHPREF_CHASE_REFERRALS;
  425. pSearchPref[dwCurrPref].vValue.dwType = ADSTYPE_BOOLEAN;
  426. if (!_stricmp(pszCurrPrefValue, "yes" ))
  427. pSearchPref[dwCurrPref].vValue.Boolean = TRUE;
  428. else if (!_stricmp(pszCurrPrefValue, "no" ))
  429. pSearchPref[dwCurrPref].vValue.Boolean = FALSE;
  430. else
  431. BAIL_ON_FAILURE(E_FAIL);
  432. }
  433. else
  434. BAIL_ON_FAILURE(E_FAIL);
  435. dwCurrPref++;
  436. break;
  437. default:
  438. BAIL_ON_FAILURE(E_FAIL);
  439. }
  440. argc--;
  441. currArg++;
  442. }
  443. //
  444. // Check for Mandatory arguments;
  445. //
  446. if (!pszSearchBase || !pszSearchFilter)
  447. BAIL_ON_FAILURE(E_FAIL);
  448. if (dwNumberAttributes == 0) {
  449. //
  450. // Get all the attributes
  451. //
  452. dwNumberAttributes = -1;
  453. }
  454. return (S_OK);
  455. error:
  456. PrintUsage();
  457. return E_FAIL;
  458. }
  459. LPWSTR
  460. RemoveWhiteSpaces(
  461. LPWSTR pszText)
  462. {
  463. LPWSTR pChar;
  464. if(!pszText)
  465. return (pszText);
  466. while(*pszText && iswspace(*pszText))
  467. pszText++;
  468. for(pChar = pszText + wcslen(pszText) - 1; pChar >= pszText; pChar--) {
  469. if(!iswspace(*pChar))
  470. break;
  471. else
  472. *pChar = L'\0';
  473. }
  474. return pszText;
  475. }
  476. HRESULT
  477. ConvertTrusteeToSid(
  478. LPWSTR bstrTrustee,
  479. PSID * ppSid,
  480. PDWORD pdwSidSize
  481. )
  482. {
  483. HRESULT hr = S_OK;
  484. BYTE Sid[MAX_PATH];
  485. DWORD dwSidSize = sizeof(Sid);
  486. DWORD dwRet = 0;
  487. WCHAR szDomainName[MAX_PATH];
  488. DWORD dwDomainSize = sizeof(szDomainName)/sizeof(WCHAR);
  489. SID_NAME_USE eUse;
  490. PSID pSid = NULL;
  491. dwSidSize = sizeof(Sid);
  492. dwRet = LookupAccountNameW(
  493. NULL,
  494. bstrTrustee,
  495. Sid,
  496. &dwSidSize,
  497. szDomainName,
  498. &dwDomainSize,
  499. (PSID_NAME_USE)&eUse
  500. );
  501. if (!dwRet) {
  502. hr = HRESULT_FROM_WIN32(GetLastError());
  503. BAIL_ON_FAILURE(hr);
  504. }
  505. memcpy(pSid, Sid, dwSidSize);
  506. *pdwSidSize = dwSidSize;
  507. *ppSid = pSid;
  508. error:
  509. return(hr);
  510. }