Leaked source code of windows server 2003
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.

225 lines
5.1 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1997.
  5. //
  6. // File: pathmgmt.cxx
  7. //
  8. // Contents:
  9. //
  10. // Functions:
  11. //
  12. // History: 25-April-97 KrishnaG Created.
  13. //
  14. //----------------------------------------------------------------------------
  15. #include "ldap.hxx"
  16. #pragma hdrstop
  17. HRESULT
  18. BuildADsPath(
  19. BSTR Parent,
  20. BSTR Name,
  21. BSTR *pADsPath
  22. )
  23. {
  24. LPWSTR pszADsPath = NULL;
  25. HRESULT hr = S_OK;
  26. hr = BuildADsPathFromParent( Parent, Name, &pszADsPath );
  27. BAIL_ON_FAILURE(hr);
  28. hr = ADsAllocString( pszADsPath, pADsPath);
  29. BAIL_ON_FAILURE(hr);
  30. error:
  31. if ( pszADsPath )
  32. FreeADsMem( pszADsPath );
  33. RRETURN(hr);
  34. }
  35. HRESULT
  36. BuildSchemaPath(
  37. BSTR bstrADsPath,
  38. BSTR bstrClass,
  39. BSTR *pSchemaPath
  40. )
  41. {
  42. TCHAR *pszADsSchema = NULL;
  43. OBJECTINFO ObjectInfo;
  44. POBJECTINFO pObjectInfo = &ObjectInfo;
  45. HRESULT hr = S_OK;
  46. WCHAR szPort[32];
  47. DWORD dwLen;
  48. memset(pObjectInfo, 0, sizeof(OBJECTINFO));
  49. if (bstrClass && *bstrClass) {
  50. hr = ADsObject(bstrADsPath, pObjectInfo);
  51. BAIL_ON_FAILURE(hr);
  52. if (pObjectInfo->TreeName) {
  53. dwLen = wcslen(pObjectInfo->NamespaceName) +
  54. wcslen(pObjectInfo->TreeName) +
  55. wcslen(bstrClass) +
  56. 11 + // ":///schema/"
  57. 1;
  58. if ( IS_EXPLICIT_PORT(pObjectInfo->PortNumber) ) {
  59. wsprintf(szPort, L":%d", pObjectInfo->PortNumber);
  60. dwLen += wcslen(szPort);
  61. }
  62. pszADsSchema = (LPWSTR) AllocADsMem( dwLen * sizeof(WCHAR) );
  63. if ( pszADsSchema == NULL )
  64. {
  65. hr = E_OUTOFMEMORY;
  66. BAIL_ON_FAILURE(hr);
  67. }
  68. wsprintf(pszADsSchema,TEXT("%s://"),pObjectInfo->NamespaceName);
  69. wcscat(pszADsSchema, pObjectInfo->TreeName);
  70. if (IS_EXPLICIT_PORT(pObjectInfo->PortNumber) ) {
  71. wsprintf(szPort, L":%d", pObjectInfo->PortNumber);
  72. wcscat(pszADsSchema, szPort);
  73. }
  74. wcscat(pszADsSchema, TEXT("/schema/"));
  75. wcscat(pszADsSchema, bstrClass);
  76. }else {
  77. pszADsSchema = (LPTSTR) AllocADsMem(
  78. ( _tcslen(pObjectInfo->NamespaceName) +
  79. _tcslen(bstrClass) +
  80. 12 ) * sizeof(TCHAR)); //includes ":///schema/"
  81. if ( pszADsSchema == NULL )
  82. {
  83. hr = E_OUTOFMEMORY;
  84. BAIL_ON_FAILURE(hr);
  85. }
  86. _stprintf(pszADsSchema,TEXT("%s://"),pObjectInfo->NamespaceName);
  87. _tcscat(pszADsSchema, TEXT("schema/"));
  88. _tcscat(pszADsSchema, bstrClass);
  89. }
  90. }
  91. hr = ADsAllocString( pszADsSchema? pszADsSchema : TEXT(""), pSchemaPath);
  92. error:
  93. if ( pszADsSchema )
  94. FreeADsStr( pszADsSchema );
  95. FreeObjectInfo( &ObjectInfo );
  96. RRETURN(hr);
  97. }
  98. HRESULT
  99. BuildADsPathFromLDAPDN(
  100. BSTR bstrParent,
  101. BSTR bstrObject,
  102. LPTSTR *ppszADsPath
  103. )
  104. {
  105. HRESULT hr = S_OK;
  106. LPTSTR pszTemp = NULL;
  107. int i;
  108. int j;
  109. int nCount;
  110. int *aIndex = NULL;
  111. if ( bstrObject == NULL || *bstrObject == 0 )
  112. {
  113. BAIL_ON_FAILURE(hr=E_ADS_BAD_PATHNAME);
  114. }
  115. *ppszADsPath = (LPTSTR) AllocADsMem( ( _tcslen(bstrParent)
  116. + _tcslen(bstrObject)
  117. + 1) * sizeof(TCHAR) );
  118. if ( *ppszADsPath == NULL )
  119. {
  120. BAIL_ON_FAILURE(hr=E_OUTOFMEMORY);
  121. }
  122. _tcscpy( *ppszADsPath, bstrParent );
  123. pszTemp = _tcschr( *ppszADsPath, TEXT(':'));
  124. if ( pszTemp )
  125. {
  126. pszTemp += _tcslen(TEXT("://"));
  127. if ( pszTemp )
  128. {
  129. pszTemp = _tcschr( pszTemp, TEXT('/'));
  130. }
  131. }
  132. if ( pszTemp == NULL )
  133. {
  134. BAIL_ON_FAILURE(hr=E_ADS_BAD_PATHNAME);
  135. }
  136. i = 0;
  137. nCount = 1;
  138. while ( bstrObject[i] != 0 )
  139. {
  140. if ( bstrObject[i++] == TEXT(',') )
  141. nCount++;
  142. }
  143. aIndex = (int *) AllocADsMem( nCount * sizeof(int));
  144. if ( aIndex == NULL )
  145. {
  146. hr = E_OUTOFMEMORY;
  147. BAIL_ON_FAILURE(hr);
  148. }
  149. i = 0; j = 0;
  150. aIndex[j++] = 0;
  151. while ( bstrObject[i] != 0 )
  152. {
  153. if ( bstrObject[i++] == TEXT(',') )
  154. aIndex[j++] = i;
  155. }
  156. for ( i = nCount; i > 0; i-- )
  157. {
  158. *(pszTemp++) = TEXT('/');
  159. j = aIndex[i-1];
  160. while ( ( bstrObject[j] != 0 ) && ( bstrObject[j] != TEXT(',') ))
  161. *(pszTemp++) = bstrObject[j++];
  162. }
  163. *pszTemp = 0;
  164. error:
  165. if ( aIndex )
  166. FreeADsMem( aIndex );
  167. if ( FAILED(hr))
  168. {
  169. if ( *ppszADsPath )
  170. FreeADsStr( *ppszADsPath );
  171. *ppszADsPath = NULL;
  172. }
  173. return hr;
  174. }
  175.