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.

261 lines
6.4 KiB

  1. //============================================================================
  2. // Copyright (C) Microsoft Corporation, 1996 - 1999
  3. //
  4. // File: CnctDlg.cpp
  5. //
  6. // History:
  7. // 05/24/96 Michael Clark Created.
  8. //
  9. // Implements the Router Connection dialog
  10. //============================================================================
  11. //
  12. #include "precompiled.h"
  13. #include "afx.h"
  14. #include "CnctDlg.h"
  15. #ifdef _DEBUG
  16. #define new DEBUG_NEW
  17. #undef THIS_FILE
  18. static char THIS_FILE[] = __FILE__;
  19. #endif
  20. const TCHAR c_szIPCShare[] = TEXT("IPC$");
  21. /////////////////////////////////////////////////////////////////////////////
  22. //
  23. // CConnectAsDlg dialog
  24. //
  25. /////////////////////////////////////////////////////////////////////////////
  26. CConnectAsDlg::CConnectAsDlg(CWnd* pParent /*=NULL*/)
  27. : CHelpDialog(CConnectAsDlg::IDD, pParent)
  28. {
  29. //{{AFX_DATA_INIT(CConnectAsDlg)
  30. m_sUserName = _T("");
  31. m_sPassword = _T("");
  32. m_stTempPassword = m_sPassword;
  33. m_sRouterName= _T("");
  34. //}}AFX_DATA_INIT
  35. // SetHelpMap(m_dwHelpMap);
  36. }
  37. void CConnectAsDlg::DoDataExchange(CDataExchange* pDX)
  38. {
  39. CHelpDialog::DoDataExchange(pDX);
  40. //{{AFX_DATA_MAP(CConnectAsDlg)
  41. DDX_Text(pDX, IDC_EDIT_USERNAME, m_sUserName);
  42. DDX_Text(pDX, IDC_EDIT_USER_PASSWORD, m_stTempPassword);
  43. DDX_Text(pDX, IDC_EDIT_MACHINENAME, m_sRouterName);
  44. DDV_MaxChars( pDX, m_sRouterName, MAX_PATH );
  45. //}}AFX_DATA_MAP
  46. if (pDX->m_bSaveAndValidate)
  47. {
  48. // Copy the data into the new buffer
  49. // ------------------------------------------------------------
  50. m_sPassword = m_stTempPassword;
  51. // Clear out the temp password, by copying 0's
  52. // into its buffer
  53. // ------------------------------------------------------------
  54. int cPassword = m_stTempPassword.GetLength();
  55. ::ZeroMemory(m_stTempPassword.GetBuffer(0),
  56. cPassword * sizeof(TCHAR));
  57. m_stTempPassword.ReleaseBuffer();
  58. // Encode the password into the real password buffer
  59. // ------------------------------------------------------------
  60. m_ucSeed = CONNECTAS_ENCRYPT_SEED;
  61. RtlEncodeW(&m_ucSeed, m_sPassword.GetBuffer(0));
  62. m_sPassword.ReleaseBuffer();
  63. }
  64. }
  65. IMPLEMENT_DYNCREATE(CConnectAsDlg, CHelpDialog)
  66. BEGIN_MESSAGE_MAP(CConnectAsDlg, CHelpDialog)
  67. //{{AFX_MSG_MAP(CConnectAsDlg)
  68. // NOTE: the ClassWizard will add message map macros here
  69. //}}AFX_MSG_MAP
  70. END_MESSAGE_MAP()
  71. BOOL CConnectAsDlg::OnInitDialog()
  72. {
  73. BOOL fReturn;
  74. fReturn = CHelpDialog::OnInitDialog();
  75. // Bring this window to the top
  76. BringWindowToTop();
  77. return fReturn;
  78. }
  79. /*!--------------------------------------------------------------------------
  80. ConnectAsAdmin
  81. Connect to the remote machine as administrator with user-supplied
  82. credentials.
  83. Returns
  84. S_OK - if a connection was established
  85. S_FALSE - if user cancelled out
  86. other - error condition
  87. Author: KennT
  88. ---------------------------------------------------------------------------*/
  89. HRESULT ConnectAsAdmin( IN LPCTSTR szRouterName)
  90. {
  91. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  92. //
  93. // allow user to specify credentials
  94. //
  95. DWORD dwRes = (DWORD) -1;
  96. HRESULT hr = S_OK;
  97. CConnectAsDlg caDlg;
  98. ::CString stIPCShare;
  99. ::CString stRouterName;
  100. ::CString stPassword;
  101. stRouterName = szRouterName;
  102. //
  103. // set message text in connect as dialog.
  104. //
  105. caDlg.m_sRouterName = szRouterName;
  106. //
  107. // loop till connect succeeds or user cancels
  108. //
  109. while ( TRUE )
  110. {
  111. // We need to ensure that this dialog is brought to
  112. // the top (if it gets lost behind the main window, we
  113. // are really in trouble).
  114. dwRes = caDlg.DoModal();
  115. if ( dwRes == IDCANCEL )
  116. {
  117. hr = S_FALSE;
  118. break;
  119. }
  120. //
  121. // Create remote resource name
  122. //
  123. stIPCShare.Empty();
  124. if ( stRouterName.Left(2) != TEXT( "\\\\" ) )
  125. {
  126. stIPCShare = TEXT( "\\\\" );
  127. }
  128. stIPCShare += stRouterName;
  129. stIPCShare += TEXT( "\\" );
  130. stIPCShare += c_szIPCShare;
  131. NETRESOURCE nr;
  132. nr.dwType = RESOURCETYPE_ANY;
  133. nr.lpLocalName = NULL;
  134. nr.lpRemoteName = (LPTSTR) (LPCTSTR) stIPCShare;
  135. nr.lpProvider = NULL;
  136. //
  137. // connect to \\router\ipc$ to try and establish credentials.
  138. // May not be the best way to establish credentials but is
  139. // the most expendient for now.
  140. //
  141. // Need to unencode the password in the ConnectAsDlg
  142. stPassword = caDlg.m_sPassword;
  143. RtlDecodeW(caDlg.m_ucSeed, stPassword.GetBuffer(0));
  144. stPassword.ReleaseBuffer();
  145. dwRes = WNetAddConnection2(
  146. &nr,
  147. (LPCTSTR) stPassword,
  148. (LPCTSTR) caDlg.m_sUserName,
  149. 0
  150. );
  151. ZeroMemory(stPassword.GetBuffer(0),
  152. stPassword.GetLength() * sizeof(TCHAR));
  153. stPassword.ReleaseBuffer();
  154. if ( dwRes != NO_ERROR )
  155. {
  156. PBYTE pbMsgBuf = NULL;
  157. ::FormatMessage(
  158. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  159. NULL,
  160. dwRes,
  161. MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), // Default language
  162. (LPTSTR) &pbMsgBuf,
  163. 0,
  164. NULL
  165. );
  166. AfxMessageBox( (LPCTSTR) pbMsgBuf );
  167. LocalFree( pbMsgBuf );
  168. }
  169. else
  170. {
  171. //
  172. // connection succeeded
  173. //
  174. hr = S_OK;
  175. break;
  176. }
  177. }
  178. return hr;
  179. }
  180. // Some helper functions
  181. DWORD RtlEncodeW(PUCHAR pucSeed, LPWSTR pswzString)
  182. {
  183. UNICODE_STRING ustring;
  184. ustring.Length = lstrlenW(pswzString) * sizeof(WCHAR);
  185. ustring.MaximumLength = ustring.Length;
  186. ustring.Buffer = pswzString;
  187. RtlRunEncodeUnicodeString(pucSeed, &ustring);
  188. return 0;
  189. }
  190. DWORD RtlDecodeW(UCHAR ucSeed, LPWSTR pswzString)
  191. {
  192. UNICODE_STRING ustring;
  193. ustring.Length = lstrlenW(pswzString) * sizeof(WCHAR);
  194. ustring.MaximumLength = ustring.Length;
  195. ustring.Buffer = pswzString;
  196. RtlRunDecodeUnicodeString(ucSeed, &ustring);
  197. return 0;
  198. }