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.

414 lines
9.9 KiB

  1. //Copyright (c) 1998 - 1999 Microsoft Corporation
  2. // TlsHunt.cpp : implementation file
  3. //
  4. #include "stdafx.h"
  5. #include <lm.h>
  6. #include "LicMgr.h"
  7. #include "defines.h"
  8. #include "LSServer.h"
  9. #include "MainFrm.h"
  10. #include "RtList.h"
  11. #include "lSmgrdoc.h"
  12. #include "LtView.h"
  13. #include "cntdlg.h"
  14. #include "treenode.h"
  15. #include "ntsecapi.h"
  16. #include "lrwizapi.h"
  17. #include "TlsHunt.h"
  18. #ifdef _DEBUG
  19. #define new DEBUG_NEW
  20. #undef THIS_FILE
  21. static char THIS_FILE[] = __FILE__;
  22. #endif
  23. /////////////////////////////////////////////////////////////////////////////
  24. // CTlsHunt dialog
  25. CTlsHunt::~CTlsHunt()
  26. {
  27. if(m_hThread != NULL)
  28. CloseHandle(m_hThread);
  29. }
  30. CTlsHunt::CTlsHunt(CWnd* pParent /*=NULL*/)
  31. : CDialog(CTlsHunt::IDD, pParent)
  32. {
  33. //{{AFX_DATA_INIT(CTlsHunt)
  34. // NOTE: the ClassWizard will add member initialization here
  35. //}}AFX_DATA_INIT
  36. m_hThread = NULL;
  37. memset(&m_EnumData, 0, sizeof(m_EnumData));
  38. }
  39. void CTlsHunt::DoDataExchange(CDataExchange* pDX)
  40. {
  41. CDialog::DoDataExchange(pDX);
  42. //{{AFX_DATA_MAP(CTlsHunt)
  43. // NOTE: the ClassWizard will add DDX and DDV calls here
  44. //}}AFX_DATA_MAP
  45. }
  46. BEGIN_MESSAGE_MAP(CTlsHunt, CDialog)
  47. //{{AFX_MSG_MAP(CTlsHunt)
  48. ON_WM_CREATE()
  49. //ON_MESSAGE(WM_DONEDISCOVERY, OnDoneDiscovery)
  50. ON_WM_CLOSE()
  51. ON_WM_CANCELMODE()
  52. //}}AFX_MSG_MAP
  53. END_MESSAGE_MAP()
  54. RPC_STATUS
  55. TryGetServerName(PCONTEXT_HANDLE hBinding,
  56. CString &Server);
  57. /////////////////////////////////////////////////////////////////////////////
  58. // CTlsHunt message handlers
  59. //------------------------------------------------------------------
  60. BOOL
  61. CTlsHunt::ServerEnumCallBack(
  62. TLS_HANDLE hHandle,
  63. LPCTSTR pszServerName,
  64. HANDLE dwUserData
  65. )
  66. /*++
  67. ++*/
  68. {
  69. ServerEnumData* pEnumData = (ServerEnumData *)dwUserData;
  70. BOOL bCancel;
  71. DWORD dwSupportFlags = 0;
  72. bCancel = (InterlockedExchange(&(pEnumData->dwDone), pEnumData->dwDone) == 1);
  73. if(bCancel == TRUE)
  74. {
  75. return TRUE;
  76. }
  77. if(pszServerName && pszServerName[0] != _TEXT('\0'))
  78. {
  79. #if DBG
  80. OutputDebugString(pszServerName);
  81. OutputDebugString(L"\n");
  82. #endif
  83. CString itemTxt;
  84. itemTxt.Format(IDS_TRYSERVER, pszServerName);
  85. pEnumData->pWaitDlg->SendDlgItemMessage(
  86. IDC_TLSERVER_NAME,
  87. WM_SETTEXT,
  88. 0,
  89. (LPARAM)(LPCTSTR)itemTxt
  90. );
  91. }
  92. if(hHandle)
  93. {
  94. DWORD dwStatus;
  95. if(pEnumData == NULL || pEnumData->pMainFrm == NULL)
  96. {
  97. SetLastError(ERROR_INVALID_PARAMETER);
  98. return TRUE;
  99. }
  100. dwStatus = TLSGetSupportFlags(
  101. hHandle,
  102. &dwSupportFlags
  103. );
  104. if ( ((dwStatus == RPC_S_OK) && (dwSupportFlags & SUPPORT_WHISTLER_52_CAL)) || (dwStatus == RPC_S_PROCNUM_OUT_OF_RANGE) )
  105. {
  106. CString strServerName;
  107. dwStatus = TryGetServerName(
  108. hHandle,
  109. strServerName
  110. );
  111. if(dwStatus == ERROR_SUCCESS && !strServerName.IsEmpty())
  112. {
  113. pEnumData->pMainFrm->ConnectServer(strServerName);
  114. pEnumData->dwNumServer++;
  115. }
  116. }
  117. }
  118. //
  119. // Continue enumeration
  120. //
  121. return InterlockedExchange(&(pEnumData->dwDone), pEnumData->dwDone) == 1;
  122. }
  123. /////////////////////////////////////////////////////////////////////
  124. DWORD WINAPI
  125. CTlsHunt::DiscoveryThread(
  126. PVOID ptr
  127. )
  128. /*++
  129. ++*/
  130. {
  131. DWORD hResult;
  132. ServerEnumData* pEnumData = (ServerEnumData *)ptr;
  133. LPWSTR* pszEnterpriseServer = NULL;
  134. DWORD dwCount;
  135. DWORD index;
  136. static BOOL fInitialized = FALSE;
  137. if (!fInitialized)
  138. {
  139. TLSInit();
  140. fInitialized = TRUE;
  141. }
  142. //
  143. // Look for all license server in domain
  144. //
  145. hResult = EnumerateTlsServer(
  146. CTlsHunt::ServerEnumCallBack,
  147. ptr,
  148. 3 * 1000,
  149. FALSE
  150. );
  151. // Find enterprise server
  152. if(pEnumData->dwDone == 0)
  153. {
  154. hResult = GetAllEnterpriseServers(
  155. &pszEnterpriseServer,
  156. &dwCount
  157. );
  158. if(hResult == ERROR_SUCCESS && dwCount != 0 && pszEnterpriseServer != NULL)
  159. {
  160. TLS_HANDLE TlsHandle = NULL;
  161. //
  162. // Inform dialog
  163. //
  164. for(index = 0; index < dwCount && pEnumData->dwDone == 0; index++)
  165. {
  166. if(pszEnterpriseServer[index] == NULL)
  167. continue;
  168. if(ServerEnumCallBack(
  169. NULL,
  170. pszEnterpriseServer[index],
  171. pEnumData
  172. ) == TRUE)
  173. {
  174. continue;
  175. }
  176. TlsHandle = TLSConnectToLsServer(
  177. pszEnterpriseServer[index]
  178. );
  179. if(TlsHandle == NULL)
  180. {
  181. continue;
  182. }
  183. DWORD dwVersion;
  184. RPC_STATUS rpcStatus;
  185. rpcStatus = TLSGetVersion(
  186. TlsHandle,
  187. &dwVersion
  188. );
  189. if(rpcStatus != RPC_S_OK)
  190. {
  191. continue;
  192. }
  193. if( TLSIsBetaNTServer() == IS_LSSERVER_RTM(dwVersion) )
  194. {
  195. continue;
  196. }
  197. ServerEnumCallBack(
  198. TlsHandle,
  199. pszEnterpriseServer[index],
  200. pEnumData
  201. );
  202. TLSDisconnectFromServer(TlsHandle);
  203. }
  204. } else
  205. {
  206. // Failure in GetAllEnterpriseServers
  207. pszEnterpriseServer = NULL;
  208. dwCount = 0;
  209. }
  210. }
  211. if(pszEnterpriseServer != NULL)
  212. {
  213. for( index = 0; index < dwCount; index ++)
  214. {
  215. if(pszEnterpriseServer[index] != NULL)
  216. {
  217. LocalFree(pszEnterpriseServer[index]);
  218. }
  219. }
  220. LocalFree(pszEnterpriseServer);
  221. }
  222. pEnumData->pWaitDlg->PostMessage(WM_DONEDISCOVERY);
  223. ExitThread(hResult);
  224. return hResult;
  225. }
  226. BOOL CTlsHunt::OnInitDialog()
  227. {
  228. CDialog::OnInitDialog();
  229. ASSERT(m_hThread != NULL);
  230. if(m_hThread != NULL)
  231. {
  232. ResumeThread(m_hThread);
  233. }
  234. return TRUE; // return TRUE unless you set the focus to a control
  235. // EXCEPTION: OCX Property Pages should return FALSE
  236. }
  237. int CTlsHunt::OnCreate(LPCREATESTRUCT lpCreateStruct)
  238. {
  239. if (CDialog::OnCreate(lpCreateStruct) == -1)
  240. return -1;
  241. m_EnumData.pWaitDlg = this;
  242. m_EnumData.pMainFrm = (CMainFrame *)GetParentFrame();
  243. m_EnumData.dwNumServer = 0;
  244. m_EnumData.dwDone = 0;
  245. DWORD dwId;
  246. m_hThread = (HANDLE)CreateThread(
  247. NULL,
  248. 0,
  249. CTlsHunt::DiscoveryThread,
  250. &m_EnumData,
  251. CREATE_SUSPENDED, // suspended thread
  252. &dwId
  253. );
  254. if(m_hThread == NULL)
  255. {
  256. //
  257. // Can't create thread.
  258. //
  259. AfxMessageBox(IDS_CREATETHREAD);
  260. return -1;
  261. }
  262. return 0;
  263. }
  264. void CTlsHunt::OnCancel()
  265. {
  266. if( m_hThread != NULL &&
  267. WaitForSingleObject(m_hThread, 0) == WAIT_TIMEOUT )
  268. {
  269. InterlockedExchange(&(m_EnumData.dwDone), 1);
  270. CString txt;
  271. txt.LoadString(IDS_CANCELDISCOVERY);
  272. SendDlgItemMessage(
  273. IDC_TLSERVER_NAME,
  274. WM_SETTEXT,
  275. 0,
  276. (LPARAM)(LPCTSTR)txt
  277. );
  278. CWnd* btn = GetDlgItem(IDCANCEL);
  279. ASSERT(btn);
  280. if(btn != NULL)
  281. {
  282. btn->EnableWindow(FALSE);
  283. }
  284. }
  285. else
  286. {
  287. CDialog::OnCancel();
  288. }
  289. }
  290. void CTlsHunt::OnDoneDiscovery()
  291. {
  292. if(m_hThread != NULL)
  293. {
  294. WaitForSingleObject(m_hThread, INFINITE);
  295. CloseHandle(m_hThread);
  296. m_hThread = NULL;
  297. }
  298. CDialog::EndDialog(0);
  299. }
  300. void CTlsHunt::OnClose()
  301. {
  302. if(m_hThread != NULL)
  303. {
  304. InterlockedExchange(&(m_EnumData.dwDone), 1);
  305. CString txt;
  306. txt.LoadString(IDS_CANCELDISCOVERY);
  307. SendDlgItemMessage(
  308. IDC_TLSERVER_NAME,
  309. WM_SETTEXT,
  310. 0,
  311. (LPARAM)(LPCTSTR)txt
  312. );
  313. CWnd* btn = GetDlgItem(IDCANCEL);
  314. ASSERT(btn);
  315. if(btn != NULL)
  316. {
  317. btn->EnableWindow(FALSE);
  318. }
  319. }
  320. else
  321. {
  322. CDialog::OnClose();
  323. }
  324. }
  325. BOOL CTlsHunt::PreTranslateMessage(MSG* pMsg)
  326. {
  327. if(pMsg->message == WM_DONEDISCOVERY)
  328. {
  329. OnDoneDiscovery();
  330. return TRUE;
  331. }
  332. return CDialog::PreTranslateMessage(pMsg);
  333. }