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.

689 lines
15 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1996.
  5. //
  6. // File: ldap2var.cxx
  7. //
  8. // Contents: LDAP Object to Variant Copy Routines
  9. //
  10. // Functions:
  11. //
  12. // History: 25-Jun-96 yihsins Created.
  13. //
  14. //
  15. // Issues:
  16. //
  17. //----------------------------------------------------------------------------
  18. #include "ldap.hxx"
  19. //
  20. // LdapType objects copy code
  21. //
  22. HRESULT
  23. LdapTypeToVarTypeString(
  24. PLDAPOBJECT pLdapSrcObject,
  25. PVARIANT pVarDestObject
  26. )
  27. {
  28. HRESULT hr = S_OK;
  29. pVarDestObject->vt = VT_BSTR;
  30. hr = ADsAllocString(
  31. LDAPOBJECT_STRING(pLdapSrcObject),
  32. &(pVarDestObject->bstrVal)
  33. );
  34. RRETURN(hr);
  35. }
  36. HRESULT
  37. LdapTypeToVarTypeBoolean(
  38. PLDAPOBJECT pLdapSrcObject,
  39. PVARIANT pVarDestObject
  40. )
  41. {
  42. HRESULT hr = S_OK;
  43. LPTSTR pszSrc = LDAPOBJECT_STRING(pLdapSrcObject);
  44. pVarDestObject->vt = VT_BOOL;
  45. if ( _tcsicmp( pszSrc, TEXT("TRUE")) == 0 )
  46. {
  47. pVarDestObject->boolVal = VARIANT_TRUE;
  48. }
  49. else if ( _tcsicmp( pszSrc, TEXT("FALSE")) == 0 )
  50. {
  51. pVarDestObject->boolVal = VARIANT_FALSE;
  52. }
  53. else
  54. {
  55. hr = E_ADS_CANT_CONVERT_DATATYPE;
  56. }
  57. RRETURN(hr);
  58. }
  59. HRESULT
  60. LdapTypeToVarTypeInteger(
  61. PLDAPOBJECT pLdapSrcObject,
  62. PVARIANT pVarDestObject
  63. )
  64. {
  65. HRESULT hr = S_OK;
  66. pVarDestObject->vt = VT_I4;
  67. pVarDestObject->lVal = _ttol(LDAPOBJECT_STRING(pLdapSrcObject));
  68. RRETURN(hr);
  69. }
  70. HRESULT
  71. LdapTypeToVarTypeSecDes(
  72. LPWSTR pszServerName,
  73. CCredentials& Credentials,
  74. PLDAPOBJECT pLdapSrcObject,
  75. PVARIANT pVarDestObject
  76. )
  77. {
  78. HRESULT hr = S_OK;
  79. BOOL fNTDS = TRUE;
  80. pVarDestObject->vt = VT_DISPATCH;
  81. hr = ReadServerType(
  82. pszServerName,
  83. &Credentials,
  84. &fNTDS
  85. );
  86. BAIL_ON_FAILURE(hr);
  87. hr = ConvertSecDescriptorToVariant(
  88. pszServerName,
  89. Credentials,
  90. LDAPOBJECT_BERVAL_VAL(pLdapSrcObject),
  91. pVarDestObject,
  92. fNTDS
  93. );
  94. error:
  95. RRETURN(hr);
  96. }
  97. HRESULT
  98. LdapTypeToVarTypeLargeInteger(
  99. PLDAPOBJECT pLdapSrcObject,
  100. PVARIANT pVarDestObject
  101. )
  102. {
  103. HRESULT hr = S_OK;
  104. IADsLargeInteger * pLargeInteger = NULL;
  105. IDispatch * pDispatch = NULL;
  106. LARGE_INTEGER largeint;
  107. hr = CoCreateInstance(
  108. CLSID_LargeInteger,
  109. NULL,
  110. CLSCTX_INPROC_SERVER,
  111. IID_IADsLargeInteger,
  112. (void **) &pLargeInteger);
  113. BAIL_ON_FAILURE(hr);
  114. swscanf (LDAPOBJECT_STRING(pLdapSrcObject), L"%I64d", &largeint);
  115. hr = pLargeInteger->put_LowPart(largeint.LowPart);
  116. BAIL_ON_FAILURE(hr);
  117. hr = pLargeInteger->put_HighPart(largeint.HighPart);
  118. BAIL_ON_FAILURE(hr);
  119. hr = pLargeInteger->QueryInterface(
  120. IID_IDispatch,
  121. (void **) &pDispatch
  122. );
  123. BAIL_ON_FAILURE(hr);
  124. V_VT(pVarDestObject) = VT_DISPATCH;
  125. V_DISPATCH(pVarDestObject) = pDispatch;
  126. error:
  127. if (pLargeInteger) {
  128. pLargeInteger->Release();
  129. }
  130. RRETURN(hr);
  131. }
  132. HRESULT
  133. LdapTypeToVarTypeDNWithBinary(
  134. PLDAPOBJECT pLdapSrcObject,
  135. PVARIANT pVarDestObject
  136. )
  137. {
  138. HRESULT hr = S_OK;
  139. ADSVALUE AdsValue;
  140. IADsDNWithBinary *pDNWithBinary = NULL;
  141. IDispatch *pDispatch = NULL;
  142. SAFEARRAY *aList = NULL;
  143. SAFEARRAYBOUND aBound;
  144. CHAR HUGEP *pArray = NULL;
  145. BSTR bstrTemp = NULL;
  146. memset(&AdsValue, 0, sizeof(AdsValue));
  147. hr = CoCreateInstance(
  148. CLSID_DNWithBinary,
  149. NULL,
  150. CLSCTX_INPROC_SERVER,
  151. IID_IADsDNWithBinary,
  152. (void **) &pDNWithBinary
  153. );
  154. BAIL_ON_FAILURE(hr);
  155. //
  156. // Convert the ldapString to an adsvalue and then take it
  157. // to DNWithBinary object
  158. //
  159. hr = LdapTypeToAdsTypeDNWithBinary(
  160. pLdapSrcObject,
  161. &AdsValue
  162. );
  163. BAIL_ON_FAILURE(hr);
  164. if (AdsValue.pDNWithBinary->pszDNString) {
  165. hr = ADsAllocString(AdsValue.pDNWithBinary->pszDNString, &bstrTemp);
  166. BAIL_ON_FAILURE(hr);
  167. //
  168. // Put the value in the object - we can only set BSTR's
  169. //
  170. hr = pDNWithBinary->put_DNString(bstrTemp);
  171. BAIL_ON_FAILURE(hr);
  172. }
  173. aBound.lLbound = 0;
  174. aBound.cElements = AdsValue.pDNWithBinary->dwLength;
  175. aList = SafeArrayCreate( VT_UI1, 1, &aBound );
  176. if ( aList == NULL )
  177. {
  178. hr = E_OUTOFMEMORY;
  179. BAIL_ON_FAILURE(hr);
  180. }
  181. hr = SafeArrayAccessData( aList, (void HUGEP * FAR *) &pArray );
  182. BAIL_ON_FAILURE(hr);
  183. memcpy( pArray, AdsValue.pDNWithBinary->lpBinaryValue, aBound.cElements );
  184. SafeArrayUnaccessData( aList );
  185. V_VT(pVarDestObject) = VT_ARRAY | VT_UI1;
  186. V_ARRAY(pVarDestObject) = aList;
  187. hr = pDNWithBinary->put_BinaryValue(*pVarDestObject);
  188. VariantClear(pVarDestObject);
  189. BAIL_ON_FAILURE(hr);
  190. hr = pDNWithBinary->QueryInterface(
  191. IID_IDispatch,
  192. (void **) &pDispatch
  193. );
  194. BAIL_ON_FAILURE(hr);
  195. V_VT(pVarDestObject) = VT_DISPATCH;
  196. V_DISPATCH(pVarDestObject) = pDispatch;
  197. error:
  198. if (pDNWithBinary) {
  199. pDNWithBinary->Release();
  200. }
  201. if (AdsValue.pDNWithBinary) {
  202. if (AdsValue.pDNWithBinary->lpBinaryValue) {
  203. FreeADsMem(AdsValue.pDNWithBinary->lpBinaryValue);
  204. }
  205. if (AdsValue.pDNWithBinary->pszDNString) {
  206. FreeADsStr(AdsValue.pDNWithBinary->pszDNString);
  207. }
  208. FreeADsMem(AdsValue.pDNWithBinary);
  209. }
  210. if (bstrTemp) {
  211. ADsFreeString(bstrTemp);
  212. }
  213. RRETURN(hr);
  214. }
  215. HRESULT
  216. LdapTypeToVarTypeDNWithString(
  217. PLDAPOBJECT pLdapSrcObject,
  218. PVARIANT pVarDestObject
  219. )
  220. {
  221. HRESULT hr = S_OK;
  222. ADSVALUE AdsValue;
  223. IADsDNWithString *pDNWithString = NULL;
  224. IDispatch *pDispatch = NULL;
  225. BSTR bstrStrVal = NULL;
  226. BSTR bstrDNVal = NULL;
  227. memset(&AdsValue, 0, sizeof(AdsValue));
  228. hr = CoCreateInstance(
  229. CLSID_DNWithString,
  230. NULL,
  231. CLSCTX_INPROC_SERVER,
  232. IID_IADsDNWithString,
  233. (void **) &pDNWithString
  234. );
  235. BAIL_ON_FAILURE(hr);
  236. //
  237. // Convert the ldapString to an adsvalue and then take it
  238. // to DNWithString object
  239. //
  240. hr = LdapTypeToAdsTypeDNWithString(
  241. pLdapSrcObject,
  242. &AdsValue
  243. );
  244. BAIL_ON_FAILURE(hr);
  245. if (AdsValue.pDNWithString->pszDNString) {
  246. hr = ADsAllocString(AdsValue.pDNWithString->pszDNString, &bstrDNVal);
  247. BAIL_ON_FAILURE(hr);
  248. hr = pDNWithString->put_DNString(bstrDNVal);
  249. BAIL_ON_FAILURE(hr);
  250. }
  251. if (AdsValue.pDNWithString->pszStringValue) {
  252. hr = ADsAllocString(
  253. AdsValue.pDNWithString->pszStringValue,
  254. &bstrStrVal
  255. );
  256. BAIL_ON_FAILURE(hr);
  257. hr = pDNWithString->put_StringValue(bstrStrVal);
  258. BAIL_ON_FAILURE(hr);
  259. }
  260. hr = pDNWithString->QueryInterface(
  261. IID_IDispatch,
  262. (void **) &pDispatch
  263. );
  264. BAIL_ON_FAILURE(hr);
  265. V_VT(pVarDestObject) = VT_DISPATCH;
  266. V_DISPATCH(pVarDestObject) = pDispatch;
  267. error:
  268. if (pDNWithString) {
  269. pDNWithString->Release();
  270. }
  271. if (AdsValue.pDNWithString) {
  272. if (AdsValue.pDNWithString->pszStringValue) {
  273. FreeADsStr(AdsValue.pDNWithString->pszStringValue);
  274. }
  275. if (AdsValue.pDNWithString->pszDNString) {
  276. FreeADsStr(AdsValue.pDNWithString->pszDNString);
  277. }
  278. FreeADsMem(AdsValue.pDNWithString);
  279. }
  280. if (bstrDNVal) {
  281. ADsFreeString(bstrDNVal);
  282. }
  283. if (bstrStrVal) {
  284. ADsFreeString(bstrStrVal);
  285. }
  286. RRETURN(hr);
  287. }
  288. HRESULT
  289. LdapTypeToVarTypeBinaryData(
  290. PLDAPOBJECT pLdapSrcObject,
  291. PVARIANT pVarDestObject
  292. )
  293. {
  294. HRESULT hr = S_OK;
  295. SAFEARRAY *aList = NULL;
  296. SAFEARRAYBOUND aBound;
  297. CHAR HUGEP *pArray = NULL;
  298. aBound.lLbound = 0;
  299. aBound.cElements = LDAPOBJECT_BERVAL_LEN(pLdapSrcObject);
  300. aList = SafeArrayCreate( VT_UI1, 1, &aBound );
  301. if ( aList == NULL )
  302. {
  303. hr = E_OUTOFMEMORY;
  304. BAIL_ON_FAILURE(hr);
  305. }
  306. hr = SafeArrayAccessData( aList, (void HUGEP * FAR *) &pArray );
  307. BAIL_ON_FAILURE(hr);
  308. memcpy( pArray, LDAPOBJECT_BERVAL_VAL(pLdapSrcObject), aBound.cElements );
  309. SafeArrayUnaccessData( aList );
  310. V_VT(pVarDestObject) = VT_ARRAY | VT_UI1;
  311. V_ARRAY(pVarDestObject) = aList;
  312. RRETURN(hr);
  313. error:
  314. if ( aList )
  315. SafeArrayDestroy( aList );
  316. RRETURN(hr);
  317. }
  318. HRESULT
  319. LdapTypeToVarTypeUTCTime(
  320. PLDAPOBJECT pLdapSrcObject,
  321. PVARIANT pVarDestObject
  322. )
  323. {
  324. HRESULT hr = S_OK;
  325. int nSuccess = 0;
  326. ADSVALUE AdsValue;
  327. pVarDestObject->vt = VT_DATE;
  328. //
  329. // This converts to a SYSTEMTIME.
  330. //
  331. hr = LdapTypeToAdsTypeUTCTime(
  332. pLdapSrcObject,
  333. &AdsValue);
  334. BAIL_ON_FAILURE(hr);
  335. nSuccess = SystemTimeToVariantTime(
  336. &AdsValue.UTCTime,
  337. &pVarDestObject->date
  338. );
  339. if (!nSuccess) {
  340. hr =E_ADS_CANT_CONVERT_DATATYPE;
  341. BAIL_ON_FAILURE(hr);
  342. }
  343. error:
  344. RRETURN(hr);
  345. }
  346. HRESULT
  347. LdapTypeToVarTypeGeneralizedTime(
  348. PLDAPOBJECT pLdapSrcObject,
  349. PVARIANT pVarDestObject
  350. )
  351. {
  352. HRESULT hr = S_OK;
  353. BOOL fSuccess = FALSE;
  354. ADSVALUE AdsValue;
  355. pVarDestObject->vt = VT_DATE;
  356. //
  357. // This converts to a SYSTEMTIME.
  358. //
  359. hr = LdapTypeToAdsTypeGeneralizedTime(
  360. pLdapSrcObject,
  361. &AdsValue);
  362. BAIL_ON_FAILURE(hr);
  363. fSuccess = SystemTimeToVariantTime(
  364. &AdsValue.UTCTime,
  365. &pVarDestObject->date);
  366. if (!fSuccess) {
  367. BAIL_ON_FAILURE(hr = E_ADS_CANT_CONVERT_DATATYPE);
  368. }
  369. error:
  370. RRETURN(hr);
  371. }
  372. HRESULT
  373. LdapTypeToVarTypeCopy(
  374. LPWSTR pszServerName,
  375. CCredentials& Credentials,
  376. PLDAPOBJECT pLdapSrcObject,
  377. DWORD dwSyntaxId,
  378. PVARIANT pVarDestObject
  379. )
  380. {
  381. HRESULT hr = S_OK;
  382. switch (dwSyntaxId) {
  383. case LDAPTYPE_BITSTRING:
  384. case LDAPTYPE_PRINTABLESTRING:
  385. case LDAPTYPE_DIRECTORYSTRING:
  386. case LDAPTYPE_COUNTRYSTRING:
  387. case LDAPTYPE_DN:
  388. case LDAPTYPE_NUMERICSTRING:
  389. case LDAPTYPE_IA5STRING:
  390. case LDAPTYPE_CASEIGNORESTRING:
  391. case LDAPTYPE_CASEEXACTSTRING:
  392. // case LDAPTYPE_CASEIGNOREIA5STRING:
  393. case LDAPTYPE_OID:
  394. case LDAPTYPE_TELEPHONENUMBER:
  395. case LDAPTYPE_ATTRIBUTETYPEDESCRIPTION:
  396. case LDAPTYPE_OBJECTCLASSDESCRIPTION:
  397. //
  398. // These types are treatable as strings
  399. // (see RFCs 2252, 2256)
  400. //
  401. case LDAPTYPE_DELIVERYMETHOD:
  402. case LDAPTYPE_ENHANCEDGUIDE:
  403. case LDAPTYPE_FACSIMILETELEPHONENUMBER:
  404. case LDAPTYPE_GUIDE:
  405. case LDAPTYPE_NAMEANDOPTIONALUID:
  406. case LDAPTYPE_POSTALADDRESS:
  407. case LDAPTYPE_PRESENTATIONADDRESS:
  408. case LDAPTYPE_TELEXNUMBER:
  409. case LDAPTYPE_DSAQUALITYSYNTAX:
  410. case LDAPTYPE_DATAQUALITYSYNTAX:
  411. case LDAPTYPE_MAILPREFERENCE:
  412. case LDAPTYPE_OTHERMAILBOX:
  413. case LDAPTYPE_ACCESSPOINTDN:
  414. case LDAPTYPE_ORNAME:
  415. case LDAPTYPE_ORADDRESS:
  416. hr = LdapTypeToVarTypeString(
  417. pLdapSrcObject,
  418. pVarDestObject
  419. );
  420. break;
  421. case LDAPTYPE_BOOLEAN:
  422. hr = LdapTypeToVarTypeBoolean(
  423. pLdapSrcObject,
  424. pVarDestObject
  425. );
  426. break;
  427. case LDAPTYPE_INTEGER:
  428. hr = LdapTypeToVarTypeInteger(
  429. pLdapSrcObject,
  430. pVarDestObject
  431. );
  432. break;
  433. case LDAPTYPE_OCTETSTRING:
  434. case LDAPTYPE_CERTIFICATE:
  435. case LDAPTYPE_CERTIFICATELIST:
  436. case LDAPTYPE_CERTIFICATEPAIR:
  437. case LDAPTYPE_PASSWORD:
  438. case LDAPTYPE_TELETEXTERMINALIDENTIFIER:
  439. case LDAPTYPE_AUDIO:
  440. case LDAPTYPE_JPEG:
  441. case LDAPTYPE_FAX:
  442. hr = LdapTypeToVarTypeBinaryData(
  443. pLdapSrcObject,
  444. pVarDestObject
  445. );
  446. break;
  447. case LDAPTYPE_GENERALIZEDTIME:
  448. hr = LdapTypeToVarTypeGeneralizedTime(
  449. pLdapSrcObject,
  450. pVarDestObject
  451. );
  452. break;
  453. case LDAPTYPE_UTCTIME:
  454. hr = LdapTypeToVarTypeUTCTime(
  455. pLdapSrcObject,
  456. pVarDestObject
  457. );
  458. break;
  459. case LDAPTYPE_SECURITY_DESCRIPTOR:
  460. hr = LdapTypeToVarTypeSecDes(
  461. pszServerName,
  462. Credentials,
  463. pLdapSrcObject,
  464. pVarDestObject
  465. );
  466. break;
  467. case LDAPTYPE_INTEGER8:
  468. hr = LdapTypeToVarTypeLargeInteger(
  469. pLdapSrcObject,
  470. pVarDestObject
  471. );
  472. break;
  473. #if 0
  474. case LDAPTYPE_CASEEXACTLIST:
  475. case LDAPTYPE_CASEIGNORELIST:
  476. #endif
  477. case LDAPTYPE_DNWITHBINARY:
  478. hr = LdapTypeToVarTypeDNWithBinary(
  479. pLdapSrcObject,
  480. pVarDestObject
  481. );
  482. break;
  483. case LDAPTYPE_DNWITHSTRING:
  484. hr = LdapTypeToVarTypeDNWithString(
  485. pLdapSrcObject,
  486. pVarDestObject
  487. );
  488. break;
  489. default:
  490. //
  491. // LDAPTYPE_UNKNOWN (schemaless server property) will be
  492. // not be converted.
  493. //
  494. hr = E_ADS_CANT_CONVERT_DATATYPE;
  495. break;
  496. }
  497. RRETURN(hr);
  498. }
  499. HRESULT
  500. LdapTypeToVarTypeCopyConstruct(
  501. LPWSTR pszServerName,
  502. CCredentials& Credentials,
  503. LDAPOBJECTARRAY ldapSrcObjects,
  504. DWORD dwSyntaxId,
  505. PVARIANT pVarDestObjects
  506. )
  507. {
  508. long i = 0;
  509. HRESULT hr = S_OK;
  510. VariantInit( pVarDestObjects );
  511. //
  512. // The following are for handling are multi-value properties
  513. //
  514. SAFEARRAY *aList = NULL;
  515. SAFEARRAYBOUND aBound;
  516. aBound.lLbound = 0;
  517. aBound.cElements = ldapSrcObjects.dwCount;
  518. aList = SafeArrayCreate( VT_VARIANT, 1, &aBound );
  519. if ( aList == NULL )
  520. {
  521. hr = E_OUTOFMEMORY;
  522. BAIL_ON_FAILURE(hr);
  523. }
  524. for ( i = 0; i < (long) ldapSrcObjects.dwCount; i++ )
  525. {
  526. VARIANT v;
  527. VariantInit(&v);
  528. hr = LdapTypeToVarTypeCopy(
  529. pszServerName,
  530. Credentials,
  531. ldapSrcObjects.pLdapObjects + i,
  532. dwSyntaxId,
  533. &v
  534. );
  535. BAIL_ON_FAILURE(hr);
  536. hr = SafeArrayPutElement( aList, &i, &v );
  537. VariantClear(&v);
  538. BAIL_ON_FAILURE(hr);
  539. }
  540. V_VT(pVarDestObjects) = VT_ARRAY | VT_VARIANT;
  541. V_ARRAY(pVarDestObjects) = aList;
  542. RRETURN(S_OK);
  543. error:
  544. if ( aList )
  545. SafeArrayDestroy( aList );
  546. RRETURN(hr);
  547. }