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.

707 lines
15 KiB

  1. //---------------------------------------------------------------------------
  2. //
  3. //
  4. // Microsoft Windows
  5. // Copyright (C) Microsoft Corporation, 1992 - 1997
  6. //
  7. // File: cdsobj.cxx
  8. //
  9. // Contents: Microsoft ADs LDAP Provider DSObject
  10. //
  11. //
  12. // History: 02-20-97 yihsins Created.
  13. //
  14. //----------------------------------------------------------------------------
  15. #include "ldapc.hxx"
  16. #pragma hdrstop
  17. class ADS_OBJECT_HANDLE
  18. {
  19. public:
  20. ADS_LDP *_ld;
  21. LPWSTR _pszADsPath;
  22. LPWSTR _pszLDAPServer;
  23. LPWSTR _pszLDAPDn;
  24. CCredentials _Credentials;
  25. LDAP_SEARCH_PREF _SearchPref;
  26. DWORD _dwPort;
  27. ADS_OBJECT_HANDLE( ADS_LDP *ld,
  28. LPWSTR pszADsPath,
  29. LPWSTR pszLDAPServer,
  30. LPWSTR pszLDAPDn,
  31. CCredentials Credentials,
  32. DWORD dwPort
  33. );
  34. ~ADS_OBJECT_HANDLE();
  35. };
  36. ADS_OBJECT_HANDLE::ADS_OBJECT_HANDLE( ADS_LDP *ld,
  37. LPWSTR pszADsPath,
  38. LPWSTR pszLDAPServer,
  39. LPWSTR pszLDAPDn,
  40. CCredentials Credentials,
  41. DWORD dwPort
  42. )
  43. {
  44. _ld = ld;
  45. _pszADsPath = pszADsPath;
  46. _pszLDAPServer = pszLDAPServer;
  47. _pszLDAPDn = pszLDAPDn;
  48. _Credentials = Credentials;
  49. _dwPort = dwPort;
  50. LdapInitializeSearchPreferences(&_SearchPref, FALSE);
  51. }
  52. ADS_OBJECT_HANDLE::~ADS_OBJECT_HANDLE()
  53. {
  54. if ( _ld )
  55. {
  56. LdapCloseObject( _ld);
  57. _ld = NULL;
  58. }
  59. if ( _pszADsPath )
  60. {
  61. FreeADsStr( _pszADsPath );
  62. _pszADsPath = NULL;
  63. }
  64. if (_pszLDAPServer) {
  65. FreeADsStr(_pszLDAPServer);
  66. _pszLDAPServer = NULL;
  67. }
  68. if (_pszLDAPDn) {
  69. FreeADsStr(_pszLDAPDn);
  70. _pszLDAPDn = NULL;
  71. }
  72. //
  73. // Free sort keys if applicable.
  74. //
  75. if (_SearchPref._pSortKeys) {
  76. FreeSortKeys(_SearchPref._pSortKeys, _SearchPref._nSortKeys);
  77. }
  78. //
  79. // Free the VLV information if applicable
  80. //
  81. if (_SearchPref._pVLVInfo) {
  82. FreeLDAPVLVInfo(_SearchPref._pVLVInfo);
  83. }
  84. //
  85. // Free the attribute-scoped query information if applicable
  86. //
  87. if (_SearchPref._pAttribScoped) {
  88. FreeADsStr(_SearchPref._pAttribScoped);
  89. }
  90. }
  91. HRESULT
  92. ADSIOpenDSObject(
  93. LPWSTR pszDNName,
  94. LPWSTR pszUserName,
  95. LPWSTR pszPassword,
  96. LONG lnReserved,
  97. PHANDLE phDSObject
  98. )
  99. {
  100. HRESULT hr = S_OK;
  101. DWORD dwPort = 0;
  102. OBJECTINFO ObjectInfo;
  103. POBJECTINFO pObjectInfo = &ObjectInfo;
  104. ADS_LDP *ld = NULL;
  105. LPWSTR pszADsPath = NULL;
  106. LPWSTR pszLDAPServer = NULL;
  107. LPWSTR pszLDAPDn = NULL;
  108. ADS_OBJECT_HANDLE *pADsObjectHandle = NULL;
  109. LPWSTR szAttributes[2] = { L"objectClass", NULL };
  110. int nCount;
  111. LDAPMessage *res = NULL;
  112. LONG lnFlags = lnReserved;
  113. if (lnFlags & ADS_FAST_BIND) {
  114. // mask it out as openobject does not know about the flag
  115. lnFlags &= ~ADS_FAST_BIND;
  116. }
  117. CCredentials Credentials( pszUserName, pszPassword, lnFlags );
  118. pszADsPath = AllocADsStr( pszDNName );
  119. if ( pszADsPath == NULL )
  120. {
  121. hr = E_OUTOFMEMORY;
  122. BAIL_ON_FAILURE(hr);
  123. }
  124. hr = BuildLDAPPathFromADsPath2(
  125. pszDNName,
  126. &pszLDAPServer,
  127. &pszLDAPDn,
  128. &dwPort
  129. );
  130. BAIL_ON_FAILURE( hr);
  131. if (pszLDAPDn == NULL) {
  132. //
  133. // LDAP://Server is not valid in ldapc
  134. // LDAP://RootDSE is valid though
  135. //
  136. BAIL_ON_FAILURE(hr = E_ADS_BAD_PATHNAME);
  137. }
  138. if (!_wcsicmp(pszLDAPDn, L"rootdse")) {
  139. FreeADsStr(pszLDAPDn);
  140. pszLDAPDn = NULL;
  141. }
  142. hr = LdapOpenObject(
  143. pszLDAPServer,
  144. pszLDAPDn,
  145. &ld,
  146. Credentials,
  147. dwPort
  148. );
  149. BAIL_ON_FAILURE(hr);
  150. if (!(lnReserved & ADS_FAST_BIND)) {
  151. // if fast bind is not specified we need to get the objectClass
  152. hr = LdapSearchS(
  153. ld,
  154. pszLDAPDn,
  155. LDAP_SCOPE_BASE,
  156. L"(objectClass=*)",
  157. szAttributes,
  158. 0,
  159. &res
  160. );
  161. if ( FAILED(hr)
  162. || ((nCount = LdapCountEntries( ld, res)) == 0))
  163. {
  164. if (!FAILED(hr)) {
  165. hr = HRESULT_FROM_WIN32(ERROR_DS_NO_SUCH_OBJECT);
  166. }
  167. }
  168. // Need to free the message if one came back
  169. if (res) {
  170. LdapMsgFree(res);
  171. res = NULL;
  172. }
  173. BAIL_ON_FAILURE(hr);
  174. }
  175. pADsObjectHandle = new ADS_OBJECT_HANDLE(
  176. ld, pszADsPath,
  177. pszLDAPServer, pszLDAPDn,
  178. Credentials, dwPort
  179. );
  180. if ( pADsObjectHandle == NULL )
  181. {
  182. hr = E_OUTOFMEMORY;
  183. BAIL_ON_FAILURE(hr);
  184. }
  185. *phDSObject = (HANDLE) pADsObjectHandle;
  186. RRETURN(S_OK);
  187. error:
  188. if ( pszADsPath )
  189. FreeADsStr( pszADsPath );
  190. if ( pszLDAPServer )
  191. FreeADsStr( pszLDAPServer );
  192. if (pszLDAPDn) {
  193. FreeADsStr(pszLDAPDn);
  194. }
  195. if ( ld )
  196. LdapCloseObject( ld );
  197. *phDSObject = NULL;
  198. RRETURN(hr);
  199. }
  200. HRESULT
  201. ADSICloseDSObject(
  202. HANDLE hDSObject
  203. )
  204. {
  205. ADS_OBJECT_HANDLE *pADsObjectHandle = (ADS_OBJECT_HANDLE *) hDSObject;
  206. delete pADsObjectHandle;
  207. RRETURN(S_OK);
  208. }
  209. HRESULT
  210. ADSISetObjectAttributes(
  211. HANDLE hDSObject,
  212. PADS_ATTR_INFO pAttributeEntries,
  213. DWORD dwNumAttributes,
  214. DWORD *pdwNumAttributesModified
  215. )
  216. {
  217. HRESULT hr = S_OK;
  218. ADS_OBJECT_HANDLE *pADsObjectHandle = (ADS_OBJECT_HANDLE *) hDSObject;
  219. SECURITY_INFORMATION seInfo = OWNER_SECURITY_INFORMATION
  220. | GROUP_SECURITY_INFORMATION
  221. | DACL_SECURITY_INFORMATION;
  222. //
  223. // seInfo is the default value for now anyone wanting to set
  224. // the SACL will have to use IDirectoryObject.
  225. //
  226. hr = ADsSetObjectAttributes(
  227. pADsObjectHandle->_ld,
  228. pADsObjectHandle->_pszLDAPServer,
  229. pADsObjectHandle->_pszLDAPDn,
  230. pADsObjectHandle->_Credentials,
  231. pADsObjectHandle->_dwPort,
  232. seInfo,
  233. pAttributeEntries,
  234. dwNumAttributes,
  235. pdwNumAttributesModified
  236. );
  237. RRETURN(hr);
  238. }
  239. HRESULT
  240. ADSIGetObjectAttributes(
  241. HANDLE hDSObject,
  242. LPWSTR *pAttributeNames,
  243. DWORD dwNumberAttributes,
  244. PADS_ATTR_INFO *ppAttributeEntries,
  245. DWORD * pdwNumAttributesReturned
  246. )
  247. {
  248. HRESULT hr = S_OK;
  249. ADS_OBJECT_HANDLE *pADsObjectHandle = (ADS_OBJECT_HANDLE *) hDSObject;
  250. SECURITY_INFORMATION seInfo = OWNER_SECURITY_INFORMATION
  251. | GROUP_SECURITY_INFORMATION
  252. | DACL_SECURITY_INFORMATION;
  253. //
  254. // seInfo is the default value for now anyone wanting to read
  255. // the SACL will have to use IDirectoryObject.
  256. //
  257. hr = ADsGetObjectAttributes(
  258. pADsObjectHandle->_ld,
  259. pADsObjectHandle->_pszLDAPServer,
  260. pADsObjectHandle->_pszLDAPDn,
  261. pADsObjectHandle->_Credentials,
  262. pADsObjectHandle->_dwPort,
  263. seInfo,
  264. pAttributeNames,
  265. dwNumberAttributes,
  266. ppAttributeEntries,
  267. pdwNumAttributesReturned
  268. );
  269. RRETURN(hr);
  270. }
  271. HRESULT
  272. ADSICreateDSObject(
  273. HANDLE hParentDSObject,
  274. LPWSTR pszRDNName,
  275. PADS_ATTR_INFO pAttributeEntries,
  276. DWORD dwNumAttributes
  277. )
  278. {
  279. HRESULT hr = S_OK;
  280. ADS_OBJECT_HANDLE *pADsObjectHandle = (ADS_OBJECT_HANDLE *) hParentDSObject;
  281. hr = ADsCreateDSObject(
  282. pADsObjectHandle->_ld,
  283. pADsObjectHandle->_pszADsPath,
  284. pszRDNName,
  285. pAttributeEntries,
  286. dwNumAttributes
  287. );
  288. RRETURN(hr);
  289. }
  290. HRESULT
  291. ADSIDeleteDSObject(
  292. HANDLE hParentDSObject,
  293. LPWSTR pszRDNName
  294. )
  295. {
  296. HRESULT hr = S_OK;
  297. ADS_OBJECT_HANDLE *pADsObjectHandle = (ADS_OBJECT_HANDLE *) hParentDSObject;
  298. hr = ADsDeleteDSObject(
  299. pADsObjectHandle->_ld,
  300. pADsObjectHandle->_pszADsPath,
  301. pszRDNName
  302. );
  303. RRETURN(hr);
  304. }
  305. HRESULT
  306. ADSISetSearchPreference(
  307. HANDLE hDSObject,
  308. IN PADS_SEARCHPREF_INFO pSearchPrefs,
  309. IN DWORD dwNumPrefs
  310. )
  311. {
  312. HRESULT hr = S_OK;
  313. ADS_OBJECT_HANDLE *pADsObjectHandle = (ADS_OBJECT_HANDLE *) hDSObject;
  314. hr = ADsSetSearchPreference(
  315. pSearchPrefs,
  316. dwNumPrefs,
  317. &(pADsObjectHandle->_SearchPref),
  318. pADsObjectHandle->_pszLDAPServer,
  319. pADsObjectHandle->_pszLDAPDn,
  320. pADsObjectHandle->_Credentials,
  321. pADsObjectHandle->_dwPort
  322. );
  323. RRETURN(hr);
  324. }
  325. HRESULT
  326. ADSIExecuteSearch(
  327. HANDLE hDSObject,
  328. IN LPWSTR pszSearchFilter,
  329. IN LPWSTR * pAttributeNames,
  330. IN DWORD dwNumberAttributes,
  331. OUT PADS_SEARCH_HANDLE phSearchHandle
  332. )
  333. {
  334. HRESULT hr = S_OK;
  335. ADS_OBJECT_HANDLE *pADsObjectHandle = (ADS_OBJECT_HANDLE *) hDSObject;
  336. hr = ADsExecuteSearch(
  337. pADsObjectHandle->_SearchPref,
  338. pADsObjectHandle->_pszADsPath,
  339. pADsObjectHandle->_pszLDAPServer,
  340. pADsObjectHandle->_pszLDAPDn,
  341. pszSearchFilter,
  342. pAttributeNames,
  343. dwNumberAttributes,
  344. phSearchHandle
  345. );
  346. RRETURN(hr);
  347. }
  348. HRESULT
  349. ADSIAbandonSearch(
  350. HANDLE hDSObject,
  351. IN PADS_SEARCH_HANDLE phSearchHandle
  352. )
  353. {
  354. HRESULT hr = S_OK;
  355. ADsAssert(phSearchHandle);
  356. hr = ADsAbandonSearch(
  357. *phSearchHandle
  358. );
  359. RRETURN(hr);
  360. }
  361. HRESULT
  362. ADSICloseSearchHandle (
  363. HANDLE hDSObject,
  364. IN ADS_SEARCH_HANDLE hSearchHandle
  365. )
  366. {
  367. HRESULT hr = S_OK;
  368. hr = ADsCloseSearchHandle(
  369. hSearchHandle
  370. );
  371. RRETURN(hr);
  372. }
  373. HRESULT
  374. ADSIGetFirstRow(
  375. HANDLE hDSObject,
  376. IN ADS_SEARCH_HANDLE hSearchHandle
  377. )
  378. {
  379. HRESULT hr = S_OK;
  380. ADS_OBJECT_HANDLE *pADsObjectHandle = (ADS_OBJECT_HANDLE *) hDSObject;
  381. hr = ADsGetFirstRow(
  382. hSearchHandle,
  383. pADsObjectHandle->_Credentials
  384. );
  385. RRETURN(hr);
  386. }
  387. HRESULT
  388. ADSIGetNextRow(
  389. HANDLE hDSObject,
  390. IN ADS_SEARCH_HANDLE hSearchHandle
  391. )
  392. {
  393. HRESULT hr = S_OK;
  394. ADS_OBJECT_HANDLE *pADsObjectHandle = (ADS_OBJECT_HANDLE *) hDSObject;
  395. hr = ADsGetNextRow(
  396. hSearchHandle,
  397. pADsObjectHandle->_Credentials
  398. );
  399. RRETURN(hr);
  400. }
  401. HRESULT
  402. ADSIGetPreviousRow(
  403. HANDLE hDSObject,
  404. IN ADS_SEARCH_HANDLE hSearchHandle
  405. )
  406. {
  407. HRESULT hr = S_OK;
  408. ADS_OBJECT_HANDLE *pADsObjectHandle = (ADS_OBJECT_HANDLE *) hDSObject;
  409. hr = ADsGetPreviousRow(
  410. hSearchHandle,
  411. pADsObjectHandle->_Credentials
  412. );
  413. RRETURN(hr);
  414. }
  415. HRESULT
  416. ADSIGetColumn(
  417. HANDLE hDSObject,
  418. IN ADS_SEARCH_HANDLE hSearchHandle,
  419. IN LPWSTR pszColumnName,
  420. OUT PADS_SEARCH_COLUMN pColumn
  421. )
  422. {
  423. HRESULT hr = S_OK;
  424. ADS_OBJECT_HANDLE *pADsObjectHandle = (ADS_OBJECT_HANDLE *) hDSObject;
  425. hr = ADsGetColumn(
  426. hSearchHandle,
  427. pszColumnName,
  428. pADsObjectHandle->_Credentials,
  429. pADsObjectHandle->_dwPort,
  430. pColumn
  431. );
  432. RRETURN(hr);
  433. }
  434. HRESULT
  435. ADSIGetNextColumnName(
  436. HANDLE hDSObject,
  437. IN ADS_SEARCH_HANDLE hSearchHandle,
  438. OUT LPWSTR * ppszColumnName
  439. )
  440. {
  441. HRESULT hr = S_OK;
  442. ADS_OBJECT_HANDLE *pADsObjectHandle = (ADS_OBJECT_HANDLE *) hDSObject;
  443. hr = ADsGetNextColumnName(
  444. hSearchHandle,
  445. ppszColumnName
  446. );
  447. RRETURN(hr);
  448. }
  449. HRESULT
  450. ADSIFreeColumn(
  451. HANDLE hDSObject,
  452. IN PADS_SEARCH_COLUMN pColumn
  453. )
  454. {
  455. HRESULT hr = S_OK;
  456. ADS_OBJECT_HANDLE *pADsObjectHandle = (ADS_OBJECT_HANDLE *) hDSObject;
  457. hr = ADsFreeColumn(
  458. pColumn
  459. );
  460. RRETURN(hr);
  461. }
  462. HRESULT
  463. ADSIEnumAttributes(
  464. HANDLE hDSObject,
  465. LPWSTR * ppszAttrNames,
  466. DWORD dwNumAttributes,
  467. PADS_ATTR_DEF * ppAttrDefinition,
  468. DWORD * pdwNumAttributes
  469. )
  470. {
  471. HRESULT hr = S_OK;
  472. ADS_OBJECT_HANDLE *pADsObjectHandle = (ADS_OBJECT_HANDLE *) hDSObject;
  473. hr = ADsEnumAttributes(
  474. pADsObjectHandle->_pszLDAPServer,
  475. pADsObjectHandle->_pszLDAPDn,
  476. pADsObjectHandle->_Credentials,
  477. pADsObjectHandle->_dwPort,
  478. ppszAttrNames,
  479. dwNumAttributes,
  480. ppAttrDefinition,
  481. pdwNumAttributes
  482. );
  483. RRETURN(hr);
  484. }
  485. HRESULT
  486. ADSICreateAttributeDefinition(
  487. HANDLE hDSObject,
  488. LPWSTR pszAttributeName,
  489. PADS_ATTR_DEF pAttributeDefinition
  490. )
  491. {
  492. HRESULT hr = S_OK;
  493. hr = ADsCreateAttributeDefinition(
  494. pszAttributeName,
  495. pAttributeDefinition
  496. );
  497. RRETURN(hr);
  498. }
  499. HRESULT
  500. ADSIWriteAttributeDefinition(
  501. HANDLE hDSObject,
  502. LPWSTR pszAttributeName,
  503. PADS_ATTR_DEF pAttributeDefinition
  504. )
  505. {
  506. HRESULT hr = S_OK;
  507. hr = ADsWriteAttributeDefinition(
  508. pszAttributeName,
  509. pAttributeDefinition
  510. );
  511. RRETURN(hr);
  512. }
  513. HRESULT
  514. ADSIDeleteAttributeDefinition(
  515. HANDLE hDSObject,
  516. LPWSTR pszAttributeName
  517. )
  518. {
  519. HRESULT hr = S_OK;
  520. hr = ADsDeleteAttributeDefinition(
  521. pszAttributeName
  522. );
  523. RRETURN(hr);
  524. }
  525. //+------------------------------------------------------------------------
  526. //
  527. // Function: ADSIModifyRDN
  528. //
  529. // Synopsis: Rename the object from the ldapc layer. This is just
  530. // a wrapper for LDAPModRdnS.
  531. //
  532. //
  533. // Arguments: Handle to the object being renamed.
  534. // new RDN of the object.
  535. //
  536. //-------------------------------------------------------------------------
  537. HRESULT
  538. ADSIModifyRdn(
  539. HANDLE hDSObject,
  540. LPWSTR pszOldRdn,
  541. LPWSTR pszNewRdn
  542. )
  543. {
  544. HRESULT hr = S_OK;
  545. ADS_OBJECT_HANDLE *pADsObjectHandle = (ADS_OBJECT_HANDLE *) hDSObject;
  546. TCHAR *pszOldDN = NULL;
  547. DWORD dwLen = 0;
  548. if (!pszOldRdn || !pszNewRdn) {
  549. RRETURN(E_ADS_BAD_PARAMETER);
  550. }
  551. dwLen = wcslen(pADsObjectHandle->_pszLDAPDn) + wcslen(pszOldRdn) + 2;
  552. pszOldDN = (LPWSTR) AllocADsMem( dwLen * sizeof(WCHAR) );
  553. if (!pszOldDN) {
  554. RRETURN (hr = E_OUTOFMEMORY);
  555. }
  556. // Build the DN of the object being renamed
  557. wsprintf(pszOldDN, L"%s,", pszOldRdn);
  558. wcscat(pszOldDN, pADsObjectHandle->_pszLDAPDn);
  559. hr = LdapModRdnS(
  560. pADsObjectHandle->_ld,
  561. pszOldDN,
  562. pszNewRdn
  563. );
  564. if (pszOldDN) {
  565. FreeADsStr(pszOldDN);
  566. }
  567. RRETURN(hr);
  568. }