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.

266 lines
7.1 KiB

  1. //================================================================================
  2. // Copyright (C) 2000 Microsoft Corporation
  3. // Author : ThiruB
  4. // Description: ADSI test app
  5. //================================================================================
  6. #define INC_OLE2
  7. #include <activeds.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <align.h>
  11. #include <lmcons.h>
  12. #include <netlib.h>
  13. #include <lmapibuf.h>
  14. #include <dsgetdc.h>
  15. #include <adsi.h>
  16. LPWSTR
  17. ConvertToLPWSTR(
  18. IN /* LPSTR */ char * s
  19. )
  20. {
  21. LPWSTR u, v;
  22. if( NULL == s ) return L"";
  23. u = LocalAlloc(LMEM_FIXED, (strlen(s)+1)*sizeof(WCHAR));
  24. if( NULL == u ) return L"";
  25. v = u;
  26. while( *v++ = *s++)
  27. ;
  28. return u;
  29. } // ConvertToLPWSTR()
  30. void PrintRow( HANDLE lh,
  31. ADS_SEARCH_HANDLE sh,
  32. LPWSTR attribs[],
  33. DWORD count
  34. )
  35. {
  36. DWORD i, j;
  37. HRESULT hr;
  38. ADS_SEARCH_COLUMN col;
  39. for ( i = 0; i < count; i++ ) {
  40. hr = ADSIGetColumn( lh, sh, attribs[ i ], &col );
  41. if ( FAILED( hr)) {
  42. printf( "Failed for attribute : %ws, ErrorCode: %ld\n",
  43. attribs[ i ],
  44. GetLastError());
  45. continue;
  46. }
  47. printf( "AttrName : %ws\n", col.pszAttrName );
  48. for ( j = 0; j < col.dwNumValues; j++ ) {
  49. printf( "\tAttr Value : %ws\n", col.pADsValues[j].DNString );
  50. }
  51. } // for
  52. } // PrintRow()
  53. void DumpObject( LPWSTR url, LPWSTR searchFilter )
  54. {
  55. HANDLE ldapHandle;
  56. ADS_SEARCH_HANDLE sh;
  57. HRESULT hr;
  58. ADS_SEARCHPREF_INFO SearchPref[3];
  59. LPWSTR attribs[] = { L"name", L"dhcpServers" };
  60. // LPWSTR attribs[] = { L"dhcpServers" };
  61. // LPWSTR searchFilter = L"(objectClass=dHCPClass)";
  62. // LPWSTR searchFilter = L"(dhcpServers~=127)";
  63. hr = ADSIOpenDSObject( url, NULL, NULL, ADS_SECURE_AUTHENTICATION, &ldapHandle );
  64. if ( SUCCEEDED( hr )) {
  65. printf( "Successfully opened %ws\n", url );
  66. }
  67. else {
  68. printf( "Failed to open : %ws\n", url );
  69. return;
  70. }
  71. SearchPref[0].dwSearchPref = ADS_SEARCHPREF_SEARCH_SCOPE;
  72. SearchPref[0].vValue.dwType = ADSTYPE_INTEGER;
  73. SearchPref[0].vValue.Integer = ADS_SCOPE_SUBTREE;
  74. // SearchPref.dwStatus = ERROR_SUCCESS;
  75. SearchPref[1].dwSearchPref = ADS_SEARCHPREF_PAGESIZE;
  76. SearchPref[1].vValue.dwType = ADSTYPE_INTEGER;
  77. SearchPref[1].vValue.Integer = 250;
  78. // Make it cache the results at the client side. This is
  79. // default, but try it anyway.
  80. SearchPref[2].dwSearchPref = ADS_SEARCHPREF_CACHE_RESULTS;
  81. SearchPref[2].vValue.dwType = ADSTYPE_BOOLEAN;
  82. SearchPref[2].vValue.Boolean = TRUE;
  83. hr = ADSISetSearchPreference(
  84. /* hDSObject */ ldapHandle,
  85. /* pSearchPrefs */ SearchPref,
  86. /* dwNumPrefs */ 3
  87. );
  88. if ( FAILED( hr )) {
  89. printf( "SetSearchPrefs failed\n");
  90. return;
  91. }
  92. // Begin Search
  93. hr = ADSIExecuteSearch( ldapHandle,
  94. searchFilter,
  95. attribs,
  96. sizeof(attribs) / sizeof(*attribs),
  97. &sh);
  98. if ( FAILED( hr)) {
  99. printf( "Search failed for %ws\n", searchFilter );
  100. return;
  101. }
  102. // Search
  103. hr = ADSIGetFirstRow ( ldapHandle, sh );
  104. if ( !FAILED( hr ) ) {
  105. PrintRow( ldapHandle, sh,
  106. attribs, sizeof(attribs) / sizeof(*attribs) );
  107. }
  108. else {
  109. printf( "GetFirstRow failed \n");
  110. return;
  111. }
  112. while ( 1 ) {
  113. hr = ADSIGetNextRow( ldapHandle, sh );
  114. if ((SUCCEEDED( hr )) &&
  115. (hr != S_ADS_NOMORE_ROWS)) {
  116. PrintRow ( ldapHandle, sh,
  117. attribs, sizeof(attribs) / sizeof(*attribs) );
  118. }
  119. else {
  120. break;
  121. }
  122. } // while
  123. // End Search
  124. hr = ADSICloseSearchHandle( ldapHandle, sh );
  125. ADSICloseDSObject( ldapHandle );
  126. } // DumpObject
  127. void _cdecl main( int argc, char *argv[] ) {
  128. LPWSTR url;
  129. LPWSTR filter;
  130. if ( argc > 2 ) {
  131. url = ConvertToLPWSTR( argv[ 1 ] );
  132. filter = ConvertToLPWSTR( argv[ 2 ] );
  133. DumpObject( url, filter );
  134. }
  135. } // main()
  136. void _cdecl _bad_main(int argc, char *argv[]) {
  137. HANDLE ldapHandle;
  138. HRESULT hr;
  139. DWORD dwLastError;
  140. WCHAR szErrorBuf[MAX_PATH];
  141. WCHAR szNameBuf[MAX_PATH];
  142. HRESULT hr_return = S_OK;
  143. hr = ADSIOpenDSObject(
  144. L"LDAP://192.168.73.1/ROOTDSE",
  145. NULL,
  146. NULL,
  147. 0,
  148. &ldapHandle );
  149. if ( SUCCEEDED( hr ) )
  150. {
  151. printf( "Successfully opened the ROOTDSE object! \n" );
  152. ADSICloseDSObject( ldapHandle );
  153. }
  154. else
  155. {
  156. printf( "Falied to open ROOTDSE ..\n" );
  157. }
  158. hr = ADSIOpenDSObject(
  159. // L"LDAP://192.168.73.1/CN=DhcpRoot,CN=NetServices,CN=Services,CN=Configuration,DC=rterala-test1,DC=officenet",
  160. L"LDAP://192.168.73.1/CN=DhcpRoot,CN=NetServices,CN=Services,CN=Configuration,CN=rterala-test1.rterala.officenet",
  161. NULL,
  162. NULL,
  163. ADS_SECURE_AUTHENTICATION,
  164. &ldapHandle );
  165. if ( SUCCEEDED( hr ) )
  166. {
  167. printf( "Successfully opened the DhcpRoot object in RCOST.COM! \n" );
  168. ADSICloseDSObject( ldapHandle );
  169. }
  170. else
  171. {
  172. if ( FAILED( hr ) )
  173. {
  174. printf( "Failed to open the DHCP ROOT object Errcode is 0x%Xh \n", hr );
  175. if (HRESULT_FACILITY(hr)==FACILITY_WIN32)
  176. {
  177. hr_return = ADsGetLastError( &dwLastError,
  178. szErrorBuf,
  179. MAX_PATH-1,
  180. szNameBuf,
  181. MAX_PATH-1);
  182. if (SUCCEEDED(hr_return))
  183. {
  184. wprintf(L"Error Code: %d\n Error Text: %ws\n Provider: %ws\n", dwLastError, szErrorBuf, szNameBuf);
  185. }
  186. }
  187. }
  188. }
  189. hr = ADSIOpenDSObject( L"LDAP://192.168.73.1/CN=DhcpRoot,CN=NetServices,CN=Services,CN=Configuration,DC=rterala-test1,DC=officenet",
  190. // L"Administrator",
  191. // L"b4,checkin",
  192. NULL, NULL,
  193. ADS_SECURE_AUTHENTICATION,
  194. &ldapHandle );
  195. if ( SUCCEEDED( hr ) )
  196. {
  197. printf( "Successfully opened the DhcpRoot object in RCOST.COM! \n" );
  198. ADSICloseDSObject( &ldapHandle );
  199. }
  200. else
  201. {
  202. if ( FAILED( hr ) )
  203. {
  204. printf( "Failed to open the DHCP ROOT object Errcode is 0x%Xh \n", hr );
  205. }
  206. if ( HRESULT_FACILITY(hr) == FACILITY_WIN32 )
  207. {
  208. hr_return = ADsGetLastError( &dwLastError,
  209. szErrorBuf,
  210. MAX_PATH-1,
  211. szNameBuf,
  212. MAX_PATH-1 );
  213. if ( SUCCEEDED( hr_return ) )
  214. {
  215. wprintf(L"Error code: %d \n Error Text: %ws\n Provider: %ws \n", dwLastError, szErrorBuf, szNameBuf );
  216. }
  217. }
  218. }
  219. exit(0);
  220. } // main()