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.

299 lines
7.2 KiB

  1. /*
  2. This is cleanri
  3. */
  4. #include <windows.h>
  5. #include <tchar.h>
  6. #include <winldap.h>
  7. #include <dsgetdc.h>
  8. #define SECURITY_WIN32
  9. #include <security.h>
  10. #include "resource.h"
  11. HINSTANCE g_hInstance = NULL;
  12. #ifdef DBG
  13. #define DebugOut OutputDebugString
  14. #else
  15. #define DebugOut 1 ? (void)0 : (void)
  16. #endif
  17. #define ARRAYSIZE( array ) sizeof( array ) / sizeof(( array )[ 0 ] )
  18. //
  19. // Ldap_InitializeConnection( )
  20. //
  21. DWORD
  22. Ldap_InitializeConnection(
  23. PLDAP * LdapHandle )
  24. {
  25. DWORD LdapError = LDAP_SUCCESS;
  26. if ( !( *LdapHandle ) ) {
  27. ULONG temp = DS_DIRECTORY_SERVICE_REQUIRED |
  28. DS_IP_REQUIRED |
  29. DS_GC_SERVER_REQUIRED;
  30. DebugOut( L"Initializing LDAP connection.\n" );
  31. *LdapHandle = ldap_init( NULL, LDAP_PORT);
  32. if ( !*LdapHandle )
  33. {
  34. DebugOut( L"ldap_init() failed.\n" );
  35. LdapError = LDAP_UNAVAILABLE;
  36. goto e0;
  37. }
  38. ldap_set_option( *LdapHandle, LDAP_OPT_GETDSNAME_FLAGS, &temp );
  39. temp = LDAP_VERSION3;
  40. ldap_set_option( *LdapHandle, LDAP_OPT_VERSION, &temp );
  41. LdapError = ldap_connect( *LdapHandle, 0 );
  42. if ( LdapError != LDAP_SUCCESS )
  43. {
  44. DebugOut( L"ldap_connect() failed.\n" );
  45. goto e1;
  46. }
  47. LdapError = ldap_bind_s( *LdapHandle, NULL, NULL, LDAP_AUTH_SSPI );
  48. if ( LdapError != LDAP_SUCCESS )
  49. {
  50. DebugOut( L"ldap_bind_s() failed.\n" );
  51. goto e1;
  52. }
  53. }
  54. DebugOut( L"LDAP initialization succeeded.\n" );
  55. e0:
  56. return LdapError;
  57. e1:
  58. ldap_unbind( *LdapHandle );
  59. *LdapHandle = NULL;
  60. goto e0;
  61. }
  62. void
  63. ErrorMessage(
  64. ULONG LdapError )
  65. {
  66. TCHAR szTmp[ 512 ];
  67. TCHAR szText[ 512 ];
  68. TCHAR szTitle[ 64 ];
  69. szTitle[0] = TEXT('\0');
  70. LoadString( g_hInstance, IDS_UNABLE_TITLE, szTitle, ARRAYSIZE(szTitle) );
  71. szTmp[0] = TEXT('\0');
  72. LoadString( g_hInstance, IDS_UNABLE_TEXT, szTmp, ARRAYSIZE(szTmp) );
  73. wsprintf( szText, szTmp, LdapError );
  74. MessageBox( NULL, szText, szTitle, MB_OK );
  75. }
  76. VOID
  77. Usage(
  78. VOID
  79. )
  80. {
  81. TCHAR szText[ 512 ];
  82. TCHAR szTitle[ 100 ];
  83. LoadString( g_hInstance, IDS_USAGE_TITLE, szTitle, ARRAYSIZE(szTitle) );
  84. LoadString( g_hInstance, IDS_USAGE_TEXT, szText, ARRAYSIZE(szText) );
  85. MessageBox( NULL, szText, szTitle, MB_OK );
  86. }
  87. //WINAPI
  88. //
  89. // WinMain()
  90. //
  91. int APIENTRY
  92. _tWinMain(
  93. HINSTANCE hInstance,
  94. HINSTANCE hPrevInstance,
  95. LPTSTR lpCmdLine,
  96. int nCmdShow)
  97. {
  98. LPWSTR pszDN = NULL;
  99. LPWSTR *ppszDN = NULL;
  100. PLDAP LdapHandle = NULL;
  101. PLDAPMessage LdapMessage = NULL;
  102. int iErr = -1;
  103. PTSTR CmdLine = lpCmdLine;
  104. PTSTR p,q;
  105. ULONG nSize;
  106. ULONG LdapError;
  107. LPWSTR pszAttributes[ 2 ];
  108. DWORD dwCount;
  109. PLDAPMessage LdapCurrent;
  110. g_hInstance = hInstance;
  111. if (CmdLine) {
  112. p = CmdLine;
  113. while (*p) {
  114. if (*p == TEXT('/') || *p == TEXT('-')) {
  115. q = p+1;
  116. if (*q == TEXT('?')) {
  117. Usage();
  118. return ERROR_SUCCESS;
  119. }
  120. }
  121. p++;
  122. }
  123. }
  124. if ( !GetComputerObjectName( NameFullyQualifiedDN, NULL, &nSize ) )
  125. {
  126. DebugOut( L"GetComputerObjectName() failed.\n" );
  127. iErr = -2;
  128. goto Cleanup;
  129. }
  130. DebugOut( L"Got GetComputerObjectName() nSize.\n" );
  131. pszDN = (LPWSTR) LocalAlloc( LMEM_FIXED, nSize * sizeof(WCHAR) );
  132. if ( !pszDN )
  133. {
  134. DebugOut( L"Out of memory.\n" );
  135. iErr = -3;
  136. goto Cleanup;
  137. }
  138. DebugOut( L"Allocated memory.\n" );
  139. if ( !GetComputerObjectName( NameFullyQualifiedDN, pszDN, &nSize ) )
  140. {
  141. DebugOut( L"GetComputerObjectName() failed.\n" );
  142. iErr = -4;
  143. goto Cleanup;
  144. }
  145. DebugOut( L"Got GetComputerObjectName().\n" );
  146. DebugOut( L"Computer DN: " );
  147. DebugOut( pszDN );
  148. DebugOut( L"\n" );
  149. if ( Ldap_InitializeConnection( &LdapHandle ) )
  150. {
  151. DebugOut( L"Ldap failed to initialize.\n" );
  152. iErr = -5;
  153. goto Cleanup;
  154. }
  155. DebugOut( L"Ldap initialized.\n" );
  156. pszAttributes[0] = L"netbootSCPBL";
  157. pszAttributes[1] = NULL;
  158. TrySearchAgain:
  159. LdapError = ldap_search_ext_s( LdapHandle,
  160. pszDN,
  161. LDAP_SCOPE_BASE,
  162. L"objectClass=Computer",
  163. pszAttributes,
  164. FALSE,
  165. NULL,
  166. NULL,
  167. NULL,
  168. 0,
  169. &LdapMessage );
  170. switch( LdapError )
  171. {
  172. case LDAP_SUCCESS:
  173. break;
  174. case LDAP_BUSY:
  175. case LDAP_TIMEOUT:
  176. DebugOut( L"ldap_search_ext_s() failed. Trying again.\n" );
  177. goto TrySearchAgain;
  178. default:
  179. DebugOut( L"ldap_search_ext_s() failed. Displaying pop-up.\n" );
  180. ErrorMessage( LdapError );
  181. iErr = -6;
  182. goto Cleanup;
  183. }
  184. DebugOut( L"ldap_search_ext_s() succeeded.\n" );
  185. dwCount = ldap_count_entries( LdapHandle, LdapMessage );
  186. if ( dwCount == 0 )
  187. {
  188. DebugOut( L"ldap_search_ext_s() returned a count of zero. Nothing to do.\n" );
  189. iErr = -7;
  190. goto Cleanup; // NOP
  191. }
  192. DebugOut( L"ldap_search_ext_s() found an SCP.\n" );
  193. LdapCurrent = ldap_first_entry( LdapHandle, LdapMessage );
  194. if ( !LdapCurrent )
  195. {
  196. DebugOut( L"Couldn't retrieve the first entry. Aborting...\n" );
  197. iErr = -8;
  198. goto Cleanup;
  199. }
  200. ppszDN = ldap_get_values( LdapHandle, LdapCurrent, L"netbootSCPBL");
  201. if ( !ppszDN )
  202. {
  203. DebugOut( L"No DN to SCP. How did we did we get here then?\n" );
  204. iErr = -9;
  205. goto Cleanup;
  206. }
  207. DebugOut( L"SCP DN: " );
  208. DebugOut( *ppszDN );
  209. DebugOut( L"\n" );
  210. TryDeleteAgain:
  211. LdapError = ldap_delete_s( LdapHandle, *ppszDN );
  212. switch ( LdapError )
  213. {
  214. case LDAP_SUCCESS:
  215. break;
  216. case LDAP_BUSY:
  217. case LDAP_TIMEOUT:
  218. DebugOut( L"ldap_delete_s() failed. Trying again.\n" );
  219. goto TryDeleteAgain;
  220. default:
  221. {
  222. LPWSTR pszExtendedError;
  223. DebugOut( L"ldap_delete_s() failed. Displaying pop-up.\n" );
  224. ldap_get_option(LdapHandle, LDAP_OPT_SERVER_ERROR, (void *) &pszExtendedError);
  225. DebugOut( L"Extended Error: " );
  226. DebugOut( pszExtendedError );
  227. DebugOut( L"\n" );
  228. ErrorMessage( LdapError );
  229. iErr = -9;
  230. goto Cleanup;
  231. }
  232. }
  233. DebugOut( L"SCP deletion succeeded.\n" );
  234. iErr = ERROR_SUCCESS;
  235. Cleanup:
  236. DebugOut( L"Cleaning up...\n" );
  237. if ( ppszDN )
  238. ldap_value_free( ppszDN );
  239. if ( LdapMessage )
  240. ldap_msgfree( LdapMessage );
  241. if ( LdapHandle )
  242. ldap_unbind( LdapHandle );
  243. if ( pszDN )
  244. LocalFree( pszDN );
  245. return iErr;
  246. }