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.

991 lines
23 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1996.
  5. //
  6. // File: var2ldap.cxx
  7. //
  8. // Contents: Variant to LDAP object 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. VarTypeToLdapTypeString(
  24. PVARIANT pVarSrcObject,
  25. PLDAPOBJECT pLdapDestObject
  26. )
  27. {
  28. HRESULT hr = S_OK;
  29. DWORD nStrLen;
  30. if(pVarSrcObject->vt != VT_BSTR){
  31. RRETURN(hr = E_ADS_CANT_CONVERT_DATATYPE);
  32. }
  33. if (!pVarSrcObject->bstrVal) {
  34. RRETURN(hr = E_ADS_BAD_PARAMETER);
  35. }
  36. nStrLen = _tcslen( pVarSrcObject->bstrVal );
  37. LDAPOBJECT_STRING(pLdapDestObject) =
  38. (LPTSTR) AllocADsMem( (nStrLen + 1) * sizeof(TCHAR));
  39. if ( LDAPOBJECT_STRING(pLdapDestObject) == NULL)
  40. {
  41. hr = E_OUTOFMEMORY;
  42. RRETURN(hr);
  43. }
  44. if ( nStrLen == 0 ) {
  45. _tcscpy( LDAPOBJECT_STRING(pLdapDestObject), L"");
  46. RRETURN(S_OK);
  47. }
  48. _tcscpy( LDAPOBJECT_STRING(pLdapDestObject), pVarSrcObject->bstrVal );
  49. RRETURN(S_OK);
  50. }
  51. HRESULT
  52. VarTypeToLdapTypeBoolean(
  53. PVARIANT pVarSrcObject,
  54. PLDAPOBJECT pLdapDestObject
  55. )
  56. {
  57. HRESULT hr = S_OK;
  58. LPTSTR pszStr = NULL;
  59. if(pVarSrcObject->vt != VT_BOOL){
  60. RRETURN(hr = E_ADS_CANT_CONVERT_DATATYPE);
  61. }
  62. if ( pVarSrcObject->boolVal )
  63. pszStr = TEXT("TRUE");
  64. else
  65. pszStr = TEXT("FALSE");
  66. LDAPOBJECT_STRING(pLdapDestObject) =
  67. (LPTSTR) AllocADsMem( (_tcslen(pszStr) + 1) * sizeof(TCHAR));
  68. if ( LDAPOBJECT_STRING(pLdapDestObject) == NULL)
  69. {
  70. RRETURN(hr = E_OUTOFMEMORY);
  71. }
  72. _tcscpy( LDAPOBJECT_STRING(pLdapDestObject), pszStr );
  73. RRETURN(hr);
  74. }
  75. HRESULT
  76. VarTypeToLdapTypeUTCTime(
  77. PVARIANT pVarSrcObject,
  78. PLDAPOBJECT pLdapDestObject
  79. )
  80. {
  81. HRESULT hr = S_OK;
  82. int nSuccess = 0;
  83. ADSVALUE adsValue;
  84. DWORD dwSyntaxId = 0;
  85. if (pVarSrcObject->vt != VT_DATE)
  86. RRETURN(hr = E_ADS_CANT_CONVERT_DATATYPE);
  87. adsValue.dwType = ADSTYPE_UTC_TIME;
  88. nSuccess = VariantTimeToSystemTime(
  89. pVarSrcObject->date,
  90. &adsValue.UTCTime
  91. );
  92. if (!nSuccess) {
  93. hr = E_ADS_CANT_CONVERT_DATATYPE;
  94. BAIL_ON_FAILURE(hr);
  95. }
  96. hr = AdsTypeToLdapTypeCopyTime(
  97. &adsValue,
  98. pLdapDestObject,
  99. &dwSyntaxId
  100. );
  101. BAIL_ON_FAILURE(hr);
  102. error:
  103. RRETURN(hr);
  104. }
  105. HRESULT
  106. VarTypeToLdapTypeGeneralizedTime(
  107. PVARIANT pVarSrcObject,
  108. PLDAPOBJECT pLdapDestObject
  109. )
  110. {
  111. HRESULT hr = S_OK;
  112. ADSVALUE adsValue;
  113. DWORD dwErr = 0;
  114. DWORD dwSyntaxId = 0;
  115. if (pVarSrcObject->vt != VT_DATE)
  116. RRETURN(hr = E_ADS_CANT_CONVERT_DATATYPE);
  117. adsValue.dwType = ADSTYPE_UTC_TIME;
  118. dwErr = VariantTimeToSystemTime(
  119. pVarSrcObject->date,
  120. &adsValue.UTCTime
  121. );
  122. if (dwErr != TRUE) {
  123. BAIL_ON_FAILURE(hr = E_ADS_CANT_CONVERT_DATATYPE);
  124. }
  125. hr = AdsTypeToLdapTypeCopyGeneralizedTime(
  126. &adsValue,
  127. pLdapDestObject,
  128. &dwSyntaxId
  129. );
  130. BAIL_ON_FAILURE(hr);
  131. error:
  132. RRETURN(hr);
  133. }
  134. HRESULT
  135. VarTypeToLdapTypeInteger(
  136. PVARIANT pVarSrcObject,
  137. PLDAPOBJECT pLdapDestObject
  138. )
  139. {
  140. HRESULT hr = S_OK;
  141. TCHAR Buffer[64];
  142. if( pVarSrcObject->vt != VT_I4 && pVarSrcObject->vt != VT_I2){
  143. RRETURN(hr = E_ADS_CANT_CONVERT_DATATYPE);
  144. }
  145. if(pVarSrcObject->vt == VT_I4)
  146. _ltot( pVarSrcObject->lVal, Buffer, 10 );
  147. else
  148. _itot( pVarSrcObject->iVal, Buffer, 10 );
  149. LDAPOBJECT_STRING(pLdapDestObject) =
  150. (LPTSTR) AllocADsMem( (_tcslen(Buffer) + 1) * sizeof(TCHAR));
  151. if ( LDAPOBJECT_STRING(pLdapDestObject) == NULL)
  152. {
  153. hr = E_OUTOFMEMORY;
  154. RRETURN(hr);
  155. }
  156. _tcscpy( LDAPOBJECT_STRING(pLdapDestObject), Buffer );
  157. RRETURN(hr);
  158. }
  159. HRESULT
  160. VarTypeToLdapTypeBinaryData(
  161. PVARIANT pVarSrcObject,
  162. PLDAPOBJECT pLdapDestObject
  163. )
  164. {
  165. HRESULT hr = S_OK;
  166. LONG dwSLBound = 0;
  167. LONG dwSUBound = 0;
  168. CHAR HUGEP *pArray = NULL;
  169. if( pVarSrcObject->vt != (VT_ARRAY | VT_UI1)) {
  170. RRETURN(hr = E_ADS_CANT_CONVERT_DATATYPE);
  171. }
  172. hr = SafeArrayGetLBound(V_ARRAY(pVarSrcObject),
  173. 1,
  174. (long FAR *) &dwSLBound );
  175. BAIL_ON_FAILURE(hr);
  176. hr = SafeArrayGetUBound(V_ARRAY(pVarSrcObject),
  177. 1,
  178. (long FAR *) &dwSUBound );
  179. BAIL_ON_FAILURE(hr);
  180. LDAPOBJECT_BERVAL(pLdapDestObject) =
  181. (struct berval *) AllocADsMem( sizeof(struct berval) + dwSUBound - dwSLBound + 1);
  182. if ( LDAPOBJECT_BERVAL(pLdapDestObject) == NULL)
  183. {
  184. hr = E_OUTOFMEMORY;
  185. BAIL_ON_FAILURE(hr);
  186. }
  187. LDAPOBJECT_BERVAL_LEN(pLdapDestObject) = dwSUBound - dwSLBound + 1;
  188. LDAPOBJECT_BERVAL_VAL(pLdapDestObject) = (CHAR *) ((LPBYTE) LDAPOBJECT_BERVAL(pLdapDestObject) + sizeof(struct berval));
  189. hr = SafeArrayAccessData( V_ARRAY(pVarSrcObject),
  190. (void HUGEP * FAR *) &pArray );
  191. BAIL_ON_FAILURE(hr);
  192. memcpy( LDAPOBJECT_BERVAL_VAL(pLdapDestObject), pArray,dwSUBound-dwSLBound+1);
  193. SafeArrayUnaccessData( V_ARRAY(pVarSrcObject) );
  194. error:
  195. RRETURN(hr);
  196. }
  197. HRESULT
  198. VarTypeToLdapTypeSecDes(
  199. LPWSTR pszServerName,
  200. CCredentials& Credentials,
  201. PVARIANT pVarSrcObject,
  202. PLDAPOBJECT pLdapDestObject
  203. )
  204. {
  205. HRESULT hr = S_OK;
  206. IADsSecurityDescriptor FAR * pSecDes = NULL;
  207. PSECURITY_DESCRIPTOR pSecurityDescriptor = NULL;
  208. DWORD dwSDLength = 0;
  209. IDispatch FAR * pDispatch = NULL;
  210. BOOL fNTDS = TRUE;
  211. if (V_VT(pVarSrcObject) != VT_DISPATCH){
  212. hr = E_FAIL;
  213. BAIL_ON_FAILURE(hr);
  214. }
  215. pDispatch = V_DISPATCH(pVarSrcObject);
  216. hr = pDispatch->QueryInterface(
  217. IID_IADsSecurityDescriptor,
  218. (void **)&pSecDes
  219. );
  220. BAIL_ON_FAILURE(hr);
  221. hr = ReadServerType(
  222. pszServerName,
  223. &Credentials,
  224. &fNTDS
  225. );
  226. BAIL_ON_FAILURE(hr);
  227. hr = ConvertSecurityDescriptorToSecDes(
  228. pszServerName,
  229. Credentials,
  230. pSecDes,
  231. &pSecurityDescriptor,
  232. &dwSDLength,
  233. fNTDS
  234. );
  235. BAIL_ON_FAILURE(hr);
  236. LDAPOBJECT_BERVAL(pLdapDestObject) =
  237. (struct berval *) AllocADsMem( sizeof(struct berval) + dwSDLength);
  238. if ( LDAPOBJECT_BERVAL(pLdapDestObject) == NULL) {
  239. hr = E_OUTOFMEMORY;
  240. BAIL_ON_FAILURE(hr);
  241. }
  242. LDAPOBJECT_BERVAL_LEN(pLdapDestObject) = dwSDLength;
  243. LDAPOBJECT_BERVAL_VAL(pLdapDestObject) =(CHAR *) ((LPBYTE) LDAPOBJECT_BERVAL(pLdapDestObject) + sizeof(struct berval));
  244. memcpy( LDAPOBJECT_BERVAL_VAL(pLdapDestObject), pSecurityDescriptor, dwSDLength);
  245. error:
  246. if (pSecurityDescriptor) {
  247. FreeADsMem(pSecurityDescriptor);
  248. }
  249. if (pSecDes) {
  250. pSecDes->Release();
  251. }
  252. RRETURN(hr);
  253. }
  254. HRESULT
  255. VarTypeToLdapTypeLargeInteger(
  256. PVARIANT pVarSrcObject,
  257. PLDAPOBJECT pLdapDestObject
  258. )
  259. {
  260. HRESULT hr = S_OK;
  261. IDispatch FAR * pDispatch = NULL;
  262. IADsLargeInteger *pLargeInteger = NULL;
  263. TCHAR Buffer[64];
  264. LARGE_INTEGER LargeInteger;
  265. if (V_VT(pVarSrcObject) != VT_DISPATCH){
  266. hr = E_FAIL;
  267. BAIL_ON_FAILURE(hr);
  268. }
  269. pDispatch = V_DISPATCH(pVarSrcObject);
  270. hr = pDispatch->QueryInterface(
  271. IID_IADsLargeInteger,
  272. (void **)&pLargeInteger
  273. );
  274. BAIL_ON_FAILURE(hr);
  275. hr = pLargeInteger->get_HighPart(&LargeInteger.HighPart);
  276. BAIL_ON_FAILURE(hr);
  277. hr = pLargeInteger->get_LowPart((LONG*)&LargeInteger.LowPart);
  278. BAIL_ON_FAILURE(hr);
  279. swprintf (Buffer, L"%I64d", LargeInteger);
  280. LDAPOBJECT_STRING(pLdapDestObject) = (LPTSTR) AllocADsStr( Buffer );
  281. if ( LDAPOBJECT_STRING(pLdapDestObject) == NULL )
  282. {
  283. hr = E_OUTOFMEMORY;
  284. RRETURN(hr);
  285. }
  286. error:
  287. if (pLargeInteger) {
  288. pLargeInteger->Release();
  289. }
  290. RRETURN(hr);
  291. }
  292. HRESULT
  293. VarTypeToLdapTypeDNWithBinary(
  294. PVARIANT pVarSrcObject,
  295. PLDAPOBJECT pLdapDestObject
  296. )
  297. {
  298. HRESULT hr = S_OK;
  299. IDispatch FAR * pDispatch = NULL;
  300. IADsDNWithBinary *pDNBinary = NULL;
  301. PADSVALUE pADsValue = NULL;
  302. VARIANT vBinary;
  303. BSTR bstrDN = NULL;
  304. LONG dwSLBound = 0;
  305. LONG dwSUBound = 0;
  306. DWORD dwSyntaxId = 0;
  307. DWORD dwLength = 0;
  308. LPBYTE lpByte = NULL;
  309. CHAR HUGEP *pArray = NULL;
  310. VariantInit(&vBinary);
  311. if (V_VT(pVarSrcObject) != VT_DISPATCH){
  312. hr = E_FAIL;
  313. BAIL_ON_FAILURE(hr);
  314. }
  315. pDispatch = V_DISPATCH(pVarSrcObject);
  316. hr = pDispatch->QueryInterface(
  317. IID_IADsDNWithBinary,
  318. (void **)&pDNBinary
  319. );
  320. BAIL_ON_FAILURE(hr);
  321. //
  322. // Convert to ADSVALUE and then to ldap representation.
  323. // This way the code to and from LDAP lives in one place.
  324. //
  325. hr = pDNBinary->get_BinaryValue(&vBinary);
  326. BAIL_ON_FAILURE(hr);
  327. if ((vBinary.vt != (VT_ARRAY | VT_UI1))
  328. && vBinary.vt != VT_EMPTY) {
  329. BAIL_ON_FAILURE(hr = E_FAIL);
  330. }
  331. hr = pDNBinary->get_DNString(&bstrDN);
  332. BAIL_ON_FAILURE(hr);
  333. //
  334. // Get the byte array in a usable format.
  335. //
  336. hr = SafeArrayGetLBound(
  337. V_ARRAY(&vBinary),
  338. 1,
  339. (long FAR *) &dwSLBound
  340. );
  341. BAIL_ON_FAILURE(hr);
  342. hr = SafeArrayGetUBound(
  343. V_ARRAY(&vBinary),
  344. 1,
  345. (long FAR *) &dwSUBound
  346. );
  347. BAIL_ON_FAILURE(hr);
  348. dwLength = dwSUBound - dwSLBound + 1;
  349. lpByte = (LPBYTE) AllocADsMem(dwLength);
  350. if (dwLength && !lpByte) {
  351. BAIL_ON_FAILURE(hr = E_OUTOFMEMORY);
  352. }
  353. hr = SafeArrayAccessData(
  354. V_ARRAY(&vBinary),
  355. (void HUGEP * FAR *) &pArray
  356. );
  357. BAIL_ON_FAILURE(hr);
  358. memcpy(lpByte, pArray, dwLength);
  359. SafeArrayUnaccessData( V_ARRAY(&vBinary) );
  360. pADsValue = (PADSVALUE) AllocADsMem(sizeof(ADSVALUE));
  361. if (!pADsValue) {
  362. BAIL_ON_FAILURE(hr = E_OUTOFMEMORY);
  363. }
  364. pADsValue->dwType = ADSTYPE_DN_WITH_BINARY;
  365. pADsValue->pDNWithBinary = (PADS_DN_WITH_BINARY)
  366. AllocADsMem(sizeof(ADS_DN_WITH_BINARY));
  367. if (!pADsValue->pDNWithBinary) {
  368. BAIL_ON_FAILURE(hr = E_OUTOFMEMORY);
  369. }
  370. pADsValue->pDNWithBinary->dwLength = dwLength;
  371. pADsValue->pDNWithBinary->lpBinaryValue = lpByte;
  372. pADsValue->pDNWithBinary->pszDNString = AllocADsStr(bstrDN);
  373. if (bstrDN && !pADsValue->pDNWithBinary->pszDNString) {
  374. BAIL_ON_FAILURE(hr = E_OUTOFMEMORY);
  375. }
  376. //
  377. // At this point the ADSIValue object is ready
  378. //
  379. hr = AdsTypeToLdapTypeCopyDNWithBinary(
  380. pADsValue,
  381. pLdapDestObject,
  382. &dwSyntaxId
  383. );
  384. BAIL_ON_FAILURE(hr);
  385. error:
  386. if (pDNBinary) {
  387. pDNBinary->Release();
  388. }
  389. VariantClear(&vBinary);
  390. if (bstrDN) {
  391. ADsFreeString(bstrDN);
  392. }
  393. //
  394. // Since we just have ptr to the byte array in the adsvalue
  395. // struct, if that is freed, we do not have to seperately
  396. // free the lpByte - if not we have to.
  397. //
  398. if (pADsValue) {
  399. //
  400. // Maybe we should replace with ADsTypeFreeAdsObjects.
  401. //
  402. if (pADsValue->pDNWithBinary) {
  403. if (pADsValue->pDNWithBinary->pszDNString) {
  404. FreeADsStr(pADsValue->pDNWithBinary->pszDNString);
  405. }
  406. if (pADsValue->pDNWithBinary->lpBinaryValue) {
  407. FreeADsMem(pADsValue->pDNWithBinary->lpBinaryValue);
  408. }
  409. FreeADsMem(pADsValue->pDNWithBinary);
  410. }
  411. FreeADsMem(pADsValue);
  412. }
  413. else if (lpByte) {
  414. FreeADsMem(lpByte);
  415. }
  416. RRETURN(hr);
  417. }
  418. HRESULT
  419. VarTypeToLdapTypeDNWithString(
  420. PVARIANT pVarSrcObject,
  421. PLDAPOBJECT pLdapDestObject
  422. )
  423. {
  424. HRESULT hr = S_OK;
  425. IDispatch FAR * pDispatch = NULL;
  426. IADsDNWithString *pDNString = NULL;
  427. PADSVALUE pADsValue = NULL;
  428. BSTR bstrStringValue = NULL;
  429. BSTR bstrDN = NULL;
  430. DWORD dwSyntaxId = 0;
  431. DWORD dwLength = 0;
  432. if (V_VT(pVarSrcObject) != VT_DISPATCH){
  433. hr = E_FAIL;
  434. BAIL_ON_FAILURE(hr);
  435. }
  436. pDispatch = V_DISPATCH(pVarSrcObject);
  437. hr = pDispatch->QueryInterface(
  438. IID_IADsDNWithString,
  439. (void **)&pDNString
  440. );
  441. BAIL_ON_FAILURE(hr);
  442. //
  443. // Convert to ADSVALUE and then to ldap representation.
  444. // This way the code to and from LDAP lives in one place.
  445. //
  446. hr = pDNString->get_StringValue(&bstrStringValue);
  447. BAIL_ON_FAILURE(hr);
  448. hr = pDNString->get_DNString(&bstrDN);
  449. BAIL_ON_FAILURE(hr);
  450. pADsValue = (PADSVALUE) AllocADsMem(sizeof(ADSVALUE));
  451. if (!pADsValue) {
  452. BAIL_ON_FAILURE(hr = E_OUTOFMEMORY);
  453. }
  454. pADsValue->dwType = ADSTYPE_DN_WITH_STRING;
  455. pADsValue->pDNWithString = (PADS_DN_WITH_STRING)
  456. AllocADsMem(sizeof(ADS_DN_WITH_STRING));
  457. if (!pADsValue->pDNWithString) {
  458. BAIL_ON_FAILURE(hr = E_OUTOFMEMORY);
  459. }
  460. //
  461. // Put String value in the DNString struct.
  462. //
  463. pADsValue->pDNWithString->pszStringValue = AllocADsStr(bstrStringValue);
  464. if (bstrStringValue && !pADsValue->pDNWithString->pszStringValue) {
  465. BAIL_ON_FAILURE(hr = E_OUTOFMEMORY);
  466. }
  467. pADsValue->pDNWithString->pszDNString = AllocADsStr(bstrDN);
  468. if (bstrDN && !pADsValue->pDNWithString->pszDNString) {
  469. BAIL_ON_FAILURE(hr = E_OUTOFMEMORY);
  470. }
  471. //
  472. // At this point the ADSIValue object is ready
  473. //
  474. hr = AdsTypeToLdapTypeCopyDNWithString(
  475. pADsValue,
  476. pLdapDestObject,
  477. &dwSyntaxId
  478. );
  479. BAIL_ON_FAILURE(hr);
  480. error:
  481. if (pDNString) {
  482. pDNString->Release();
  483. }
  484. if (bstrStringValue) {
  485. ADsFreeString(bstrStringValue);
  486. }
  487. if (bstrDN) {
  488. ADsFreeString(bstrDN);
  489. }
  490. if (pADsValue) {
  491. //
  492. // Maybe we should replace with ADsTypeFreeAdsObjects.
  493. //
  494. if (pADsValue->pDNWithString) {
  495. if (pADsValue->pDNWithString->pszDNString) {
  496. FreeADsStr(pADsValue->pDNWithString->pszDNString);
  497. }
  498. if (pADsValue->pDNWithString->pszStringValue) {
  499. FreeADsMem(pADsValue->pDNWithString->pszStringValue);
  500. }
  501. FreeADsMem(pADsValue->pDNWithString);
  502. }
  503. FreeADsMem(pADsValue);
  504. }
  505. RRETURN(hr);
  506. }
  507. HRESULT
  508. VarTypeToLdapTypeCopy(
  509. LPWSTR pszServerName,
  510. CCredentials& Credentials,
  511. DWORD dwLdapType,
  512. PVARIANT pVarSrcObject,
  513. PLDAPOBJECT pLdapDestObject,
  514. BOOL *pfIsString
  515. )
  516. {
  517. HRESULT hr = S_OK;
  518. *pfIsString = TRUE; // This will only be FALSE when the variant
  519. // contains binary data
  520. switch (dwLdapType){
  521. case LDAPTYPE_BITSTRING:
  522. case LDAPTYPE_PRINTABLESTRING:
  523. case LDAPTYPE_DIRECTORYSTRING:
  524. case LDAPTYPE_COUNTRYSTRING:
  525. case LDAPTYPE_DN:
  526. case LDAPTYPE_NUMERICSTRING:
  527. case LDAPTYPE_IA5STRING:
  528. case LDAPTYPE_CASEIGNORESTRING:
  529. case LDAPTYPE_CASEEXACTSTRING:
  530. // case LDAPTYPE_CASEIGNOREIA5STRING:
  531. case LDAPTYPE_OID:
  532. case LDAPTYPE_TELEPHONENUMBER:
  533. case LDAPTYPE_ATTRIBUTETYPEDESCRIPTION:
  534. case LDAPTYPE_OBJECTCLASSDESCRIPTION:
  535. //
  536. // These types are treatable as strings
  537. // (see RFCs 2252, 2256)
  538. //
  539. case LDAPTYPE_DELIVERYMETHOD:
  540. case LDAPTYPE_ENHANCEDGUIDE:
  541. case LDAPTYPE_FACSIMILETELEPHONENUMBER:
  542. case LDAPTYPE_GUIDE:
  543. case LDAPTYPE_NAMEANDOPTIONALUID:
  544. case LDAPTYPE_POSTALADDRESS:
  545. case LDAPTYPE_PRESENTATIONADDRESS:
  546. case LDAPTYPE_TELEXNUMBER:
  547. case LDAPTYPE_DSAQUALITYSYNTAX:
  548. case LDAPTYPE_DATAQUALITYSYNTAX:
  549. case LDAPTYPE_MAILPREFERENCE:
  550. case LDAPTYPE_OTHERMAILBOX:
  551. case LDAPTYPE_ACCESSPOINTDN:
  552. case LDAPTYPE_ORNAME:
  553. case LDAPTYPE_ORADDRESS:
  554. hr = VarTypeToLdapTypeString(
  555. pVarSrcObject,
  556. pLdapDestObject
  557. );
  558. break;
  559. case LDAPTYPE_BOOLEAN:
  560. hr = VarTypeToLdapTypeBoolean(
  561. pVarSrcObject,
  562. pLdapDestObject
  563. );
  564. break;
  565. case LDAPTYPE_INTEGER:
  566. hr = VarTypeToLdapTypeInteger(
  567. pVarSrcObject,
  568. pLdapDestObject
  569. );
  570. break;
  571. case LDAPTYPE_OCTETSTRING:
  572. case LDAPTYPE_CERTIFICATE:
  573. case LDAPTYPE_CERTIFICATELIST:
  574. case LDAPTYPE_CERTIFICATEPAIR:
  575. case LDAPTYPE_PASSWORD:
  576. case LDAPTYPE_TELETEXTERMINALIDENTIFIER:
  577. case LDAPTYPE_AUDIO:
  578. case LDAPTYPE_JPEG:
  579. case LDAPTYPE_FAX:
  580. *pfIsString = FALSE;
  581. hr = VarTypeToLdapTypeBinaryData(
  582. pVarSrcObject,
  583. pLdapDestObject
  584. );
  585. break;
  586. case LDAPTYPE_GENERALIZEDTIME:
  587. hr = VarTypeToLdapTypeGeneralizedTime(
  588. pVarSrcObject,
  589. pLdapDestObject
  590. );
  591. break;
  592. case LDAPTYPE_UTCTIME:
  593. hr = VarTypeToLdapTypeUTCTime(
  594. pVarSrcObject,
  595. pLdapDestObject
  596. );
  597. break;
  598. case LDAPTYPE_SECURITY_DESCRIPTOR:
  599. *pfIsString = FALSE;
  600. hr = VarTypeToLdapTypeSecDes(
  601. pszServerName,
  602. Credentials,
  603. pVarSrcObject,
  604. pLdapDestObject
  605. );
  606. break;
  607. case LDAPTYPE_INTEGER8:
  608. hr = VarTypeToLdapTypeLargeInteger(
  609. pVarSrcObject,
  610. pLdapDestObject
  611. );
  612. break;
  613. #if 0
  614. case LDAPTYPE_CASEEXACTLIST:
  615. case LDAPTYPE_CASEIGNORELIST:
  616. #endif
  617. case LDAPTYPE_DNWITHBINARY:
  618. hr = VarTypeToLdapTypeDNWithBinary(
  619. pVarSrcObject,
  620. pLdapDestObject
  621. );
  622. break;
  623. case LDAPTYPE_DNWITHSTRING:
  624. hr = VarTypeToLdapTypeDNWithString(
  625. pVarSrcObject,
  626. pLdapDestObject
  627. );
  628. break;
  629. default:
  630. hr = E_ADS_CANT_CONVERT_DATATYPE;
  631. break;
  632. }
  633. RRETURN(hr);
  634. }
  635. HRESULT
  636. VarTypeToLdapTypeCopyConstruct(
  637. LPWSTR pszServerName,
  638. CCredentials& Credentials,
  639. DWORD dwLdapType,
  640. LPVARIANT pVarSrcObjects,
  641. DWORD dwNumObjects,
  642. LDAPOBJECTARRAY *pLdapDestObjects
  643. )
  644. {
  645. DWORD i = 0;
  646. HRESULT hr = S_OK;
  647. if ( dwNumObjects == 0 )
  648. {
  649. pLdapDestObjects->dwCount = 0;
  650. pLdapDestObjects->pLdapObjects = NULL;
  651. RRETURN(S_OK);
  652. }
  653. pLdapDestObjects->pLdapObjects =
  654. (PLDAPOBJECT)AllocADsMem( dwNumObjects * sizeof(LDAPOBJECT));
  655. if (pLdapDestObjects->pLdapObjects == NULL)
  656. RRETURN(E_OUTOFMEMORY);
  657. pLdapDestObjects->dwCount = dwNumObjects;
  658. for (i = 0; i < dwNumObjects; i++ ) {
  659. hr = VarTypeToLdapTypeCopy(
  660. pszServerName,
  661. Credentials,
  662. dwLdapType,
  663. pVarSrcObjects + i,
  664. pLdapDestObjects->pLdapObjects + i,
  665. &(pLdapDestObjects->fIsString)
  666. );
  667. BAIL_ON_FAILURE(hr);
  668. }
  669. RRETURN(S_OK);
  670. error:
  671. LdapTypeFreeLdapObjects( pLdapDestObjects );
  672. RRETURN(hr);
  673. }
  674. HRESULT
  675. GetLdapSyntaxFromVariant(
  676. VARIANT * pvProp,
  677. PDWORD pdwSyntaxId,
  678. LPTSTR pszServer,
  679. LPTSTR pszAttrName,
  680. CCredentials& Credentials,
  681. DWORD dwPort
  682. )
  683. {
  684. // IADsSecurityDescriptor * pSecDes = NULL;
  685. // IADsLargeInteger * pLargeInt = NULL;
  686. IDispatch * pDispObj = NULL;
  687. IDispatch * pDispatch = NULL;
  688. HRESULT hr = S_OK;
  689. if (!pvProp) {
  690. hr = E_FAIL;
  691. BAIL_ON_FAILURE(hr);
  692. }
  693. switch (pvProp->vt) {
  694. case VT_BSTR:
  695. *pdwSyntaxId = LDAPTYPE_CASEIGNORESTRING;
  696. break;
  697. case VT_I8:
  698. *pdwSyntaxId = LDAPTYPE_INTEGER8;
  699. break;
  700. case VT_I4:
  701. *pdwSyntaxId = LDAPTYPE_INTEGER;
  702. break;
  703. case VT_I2:
  704. *pdwSyntaxId = LDAPTYPE_INTEGER;
  705. break;
  706. case VT_BOOL:
  707. *pdwSyntaxId = LDAPTYPE_BOOLEAN;
  708. break;
  709. case VT_DATE:
  710. //
  711. // We need to determine if it is a GeneralizedTime
  712. // or UTCTime property. If the lookup fails on the
  713. // server we will failover to GenTime.
  714. //
  715. hr = E_FAIL;
  716. if (pszAttrName) {
  717. //
  718. // pszAttrName will be null if we are coming in
  719. // from the property cache on putproperty
  720. //
  721. hr = LdapGetSyntaxOfAttributeOnServer(
  722. pszServer,
  723. pszAttrName,
  724. pdwSyntaxId,
  725. Credentials,
  726. dwPort
  727. );
  728. }
  729. if (FAILED(hr)) {
  730. // Default to GenTime
  731. *pdwSyntaxId = LDAPTYPE_GENERALIZEDTIME;
  732. }
  733. break;
  734. case (VT_ARRAY | VT_UI1):
  735. *pdwSyntaxId = LDAPTYPE_OCTETSTRING;
  736. break;
  737. case (VT_DISPATCH):
  738. pDispatch = V_DISPATCH(pvProp);
  739. // Security Descriptor
  740. hr = pDispatch->QueryInterface(
  741. IID_IADsSecurityDescriptor,
  742. (void **)&pDispObj
  743. );
  744. if (SUCCEEDED(hr)) {
  745. pDispObj->Release();
  746. *pdwSyntaxId = LDAPTYPE_SECURITY_DESCRIPTOR;
  747. break;
  748. }
  749. // Large Integer
  750. hr = pDispatch->QueryInterface(
  751. IID_IADsLargeInteger,
  752. (void **)&pDispObj
  753. );
  754. if (SUCCEEDED(hr)) {
  755. pDispObj->Release();
  756. *pdwSyntaxId = LDAPTYPE_INTEGER8;
  757. break;
  758. }
  759. // DN With Binary
  760. hr = pDispatch->QueryInterface(
  761. IID_IADsDNWithBinary,
  762. (void **)&pDispObj
  763. );
  764. if (SUCCEEDED(hr)) {
  765. pDispObj->Release();
  766. *pdwSyntaxId = LDAPTYPE_DNWITHBINARY;
  767. break;
  768. }
  769. // DN With String
  770. hr = pDispatch->QueryInterface(
  771. IID_IADsDNWithString,
  772. (void **)&pDispObj
  773. );
  774. if (SUCCEEDED(hr)) {
  775. pDispObj->Release();
  776. *pdwSyntaxId = LDAPTYPE_DNWITHSTRING;
  777. break;
  778. }
  779. BAIL_ON_FAILURE(hr = E_ADS_BAD_PARAMETER)
  780. break;
  781. default:
  782. RRETURN(E_FAIL);
  783. }
  784. RRETURN(S_OK);
  785. error:
  786. RRETURN(hr);
  787. }