Source code of Windows XP (NT5)
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.

608 lines
16 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997.
  5. //
  6. // File: R A S U I . C P P
  7. //
  8. // Contents: Implements the base class used to implement the Dialup,
  9. // Direct, Internet, and Vpn connection UI objects.
  10. //
  11. // Notes:
  12. //
  13. // Author: shaunco 17 Dec 1997 (and that's the code complete date!)
  14. //
  15. //----------------------------------------------------------------------------
  16. #include "pch.h"
  17. #pragma hdrstop
  18. #include "netshell.h"
  19. #include "nsbase.h"
  20. #include "nccom.h"
  21. #include "ncras.h"
  22. #include "rasui.h"
  23. #include "rasuip.h"
  24. //+---------------------------------------------------------------------------
  25. //
  26. // Function: HrCreateInboundConfigConnection
  27. //
  28. // Purpose: Create and return the Inbound configuration connection object.
  29. // This is called from inbui.cpp and rasui.cpp.
  30. //
  31. // Arguments:
  32. // ppCon [out] Returned connection object.
  33. //
  34. // Returns: S_OK or an error code
  35. //
  36. // Author: shaunco 25 Feb 1998
  37. //
  38. // Notes: This commonizes this operation which may be performed from
  39. // the direct connect wizard as well as the incoming connections
  40. // wizard.
  41. //
  42. HRESULT
  43. HrCreateInboundConfigConnection (
  44. INetConnection** ppCon)
  45. {
  46. static const CLSID CLSID_InboundConnection =
  47. {0xBA126AD9,0x2166,0x11D1,{0xB1,0xD0,0x00,0x80,0x5F,0xC1,0x27,0x0E}};
  48. Assert (ppCon);
  49. // Initialize the output parameter.
  50. //
  51. *ppCon = NULL;
  52. // Create an uninitialized inbound connection object.
  53. // Ask for the INetInboundConnection interface so we can
  54. // initialize it as the configuration connection.
  55. //
  56. HRESULT hr;
  57. INetInboundConnection* pInbCon;
  58. hr = HrCreateInstance(
  59. CLSID_InboundConnection,
  60. CLSCTX_LOCAL_SERVER | CLSCTX_NO_CODE_DOWNLOAD,
  61. &pInbCon);
  62. TraceHr(ttidError, FAL, hr, FALSE, "HrCreateInstance");
  63. if (SUCCEEDED(hr))
  64. {
  65. // Initialize the connection object and return the
  66. // INetConnection interface on it to the caller.
  67. // Pass TRUE so that the remoteaccess service is started.
  68. //
  69. hr = pInbCon->InitializeAsConfigConnection (TRUE);
  70. if (SUCCEEDED(hr))
  71. {
  72. hr = HrQIAndSetProxyBlanket(pInbCon, ppCon);
  73. }
  74. ReleaseObj (pInbCon);
  75. }
  76. TraceHr (ttidError, FAL, hr, FALSE, "HrCreateInboundConfigConnection");
  77. return hr;
  78. }
  79. //+---------------------------------------------------------------------------
  80. //
  81. // Member: CRasUiBase::CRasUiBase
  82. //
  83. // Purpose: Constructor/Destructor
  84. //
  85. // Arguments:
  86. // (none)
  87. //
  88. // Returns:
  89. //
  90. // Author: shaunco 20 Oct 1997
  91. //
  92. // Notes:
  93. //
  94. CRasUiBase::CRasUiBase ()
  95. {
  96. m_pCon = NULL;
  97. m_dwRasWizType = 0;
  98. ZeroMemory (&m_RasConInfo, sizeof(m_RasConInfo));
  99. }
  100. CRasUiBase::~CRasUiBase ()
  101. {
  102. RciFree (&m_RasConInfo);
  103. ReleaseObj (m_pCon);
  104. }
  105. //+---------------------------------------------------------------------------
  106. // INetConnectionUI
  107. //
  108. HRESULT
  109. CRasUiBase::HrSetConnection (
  110. INetConnection* pCon,
  111. CComObjectRootEx <CComObjectThreadModel>* pObj)
  112. {
  113. // Enter our critical section to protect the use of m_pCon.
  114. //
  115. CExceptionSafeComObjectLock EsLock (pObj);
  116. ReleaseObj (m_pCon);
  117. m_pCon = pCon;
  118. AddRefObj (m_pCon);
  119. return S_OK;
  120. }
  121. HRESULT
  122. CRasUiBase::HrConnect (
  123. HWND hwndParent,
  124. DWORD dwFlags,
  125. CComObjectRootEx <CComObjectThreadModel>* pObj,
  126. IUnknown* punk)
  127. {
  128. HRESULT hr = S_OK;
  129. if (!m_pCon)
  130. {
  131. hr = E_UNEXPECTED;
  132. }
  133. else
  134. {
  135. RASCON_INFO RasConInfo;
  136. hr = HrRciGetRasConnectionInfo (m_pCon, &RasConInfo);
  137. if (S_OK == hr)
  138. {
  139. // Dial the entry.
  140. //
  141. RASDIALDLG info;
  142. ZeroMemory (&info, sizeof(info));
  143. info.dwSize = sizeof (RASDIALDLG);
  144. info.hwndOwner = hwndParent;
  145. BOOL fRet = RasDialDlg (
  146. RasConInfo.pszwPbkFile,
  147. RasConInfo.pszwEntryName, NULL, &info);
  148. if (!fRet)
  149. {
  150. // If the fRet was FALSE, but the dwError is zero,
  151. // then the user cancelled. Else it was an error
  152. //
  153. if (0 == info.dwError)
  154. {
  155. hr = S_FALSE;
  156. }
  157. else
  158. {
  159. hr = HRESULT_FROM_WIN32(info.dwError);
  160. }
  161. TraceError ("RasDialDlg", (S_FALSE == hr) ? S_OK : hr);
  162. }
  163. RciFree (&RasConInfo);
  164. }
  165. }
  166. TraceError ("CRasUiBase::HrConnect", (S_FALSE == hr) ? S_OK : hr);
  167. return hr;
  168. }
  169. HRESULT
  170. CRasUiBase::HrDisconnect (
  171. IN HWND hwndParent,
  172. IN DWORD dwFlags)
  173. {
  174. HRESULT hr = S_OK;
  175. if (!m_pCon)
  176. {
  177. hr = E_UNEXPECTED;
  178. }
  179. else
  180. {
  181. // Note that we do not call m_pCon->Disconnect. This is because
  182. // CM has some bad architecture w.r.t. knowing when they have
  183. // disconnected the connection vs. the connection being dropped
  184. // and they need to redial. Thus, for CM's sake, the RasHangup
  185. // call has to take place inside of explorer and not from netman.
  186. // (Tough noogies for clients of INetConnection::Disconnect who
  187. // would like CM connections to be hungup correctly. But, that's
  188. // the nature of a messed up architecture and no one willing to make
  189. // it right -- the hacks start to creep into good code too.)
  190. //
  191. RASCON_INFO RasConInfo;
  192. hr = HrRciGetRasConnectionInfo (m_pCon, &RasConInfo);
  193. if (S_OK == hr)
  194. {
  195. HRASCONN hRasConn;
  196. hr = HrFindRasConnFromGuidId (&RasConInfo.guidId, &hRasConn, NULL);
  197. if (S_OK == hr)
  198. {
  199. hr = HrRasHangupUntilDisconnected (hRasConn);
  200. }
  201. else if (S_FALSE == hr)
  202. {
  203. // Not connected.
  204. //
  205. hr = S_OK;
  206. }
  207. RciFree (&RasConInfo);
  208. }
  209. }
  210. TraceHr (ttidError, FAL, hr, FALSE, "CRasUiBase::HrDisconnect");
  211. return hr;
  212. }
  213. //+---------------------------------------------------------------------------
  214. // INetConnectionPropertyUi2
  215. //
  216. HRESULT
  217. CRasUiBase::HrAddPropertyPages (
  218. HWND hwndParent,
  219. LPFNADDPROPSHEETPAGE pfnAddPage,
  220. LPARAM lParam)
  221. {
  222. HRESULT hr = S_OK;
  223. // Validate parameters.
  224. //
  225. if ((hwndParent && !IsWindow (hwndParent)) ||
  226. !pfnAddPage)
  227. {
  228. hr = E_POINTER;
  229. }
  230. // Must have called SetConnection prior to this.
  231. //
  232. else if (!m_pCon)
  233. {
  234. hr = E_UNEXPECTED;
  235. }
  236. else
  237. {
  238. RciFree (&m_RasConInfo);
  239. hr = HrRciGetRasConnectionInfo (m_pCon, &m_RasConInfo);
  240. if (S_OK == hr)
  241. {
  242. ZeroMemory (&m_RasEntryDlg, sizeof(m_RasEntryDlg));
  243. m_RasEntryDlg.dwSize = sizeof(m_RasEntryDlg);
  244. m_RasEntryDlg.hwndOwner = hwndParent;
  245. m_RasEntryDlg.dwFlags = RASEDFLAG_ShellOwned;
  246. m_RasEntryDlg.reserved2 = reinterpret_cast<ULONG_PTR>
  247. (&m_ShellCtx);
  248. ZeroMemory (&m_ShellCtx, sizeof(m_ShellCtx));
  249. m_ShellCtx.pfnAddPage = pfnAddPage;
  250. m_ShellCtx.lparam = lParam;
  251. BOOL fRet = RasEntryDlgW (
  252. m_RasConInfo.pszwPbkFile,
  253. m_RasConInfo.pszwEntryName,
  254. &m_RasEntryDlg);
  255. if (!fRet)
  256. {
  257. TraceError ("CRasUiBase::AddPropertyPages: RasEntryDlg "
  258. "returned an error",
  259. HRESULT_FROM_WIN32 (m_RasEntryDlg.dwError));
  260. }
  261. }
  262. hr = S_OK;
  263. }
  264. TraceError ("CRasUiBase::HrAddPropertyPages", hr);
  265. return hr;
  266. }
  267. //+---------------------------------------------------------------------------
  268. // INetConnectionWizardUi
  269. //
  270. HRESULT
  271. CRasUiBase::HrQueryMaxPageCount (
  272. INetConnectionWizardUiContext* pContext,
  273. DWORD* pcMaxPages)
  274. {
  275. HRESULT hr;
  276. // Validate parameters.
  277. //
  278. if (!pcMaxPages)
  279. {
  280. hr = E_POINTER;
  281. }
  282. else
  283. {
  284. *pcMaxPages = RasWizQueryMaxPageCount (m_dwRasWizType);
  285. hr = S_OK;
  286. }
  287. TraceError ("CRasUiBase::HrQueryMaxPageCount", hr);
  288. return hr;
  289. }
  290. BOOL bCallRasDlgEntry = TRUE;
  291. HRESULT
  292. CRasUiBase::HrAddWizardPages (
  293. INetConnectionWizardUiContext* pContext,
  294. LPFNADDPROPSHEETPAGE pfnAddPage,
  295. LPARAM lParam,
  296. DWORD dwFlags)
  297. {
  298. HRESULT hr = S_OK;
  299. if (!bCallRasDlgEntry)
  300. {
  301. return E_ABORT;
  302. }
  303. // Validate parameters.
  304. //
  305. if (!pfnAddPage)
  306. {
  307. hr = E_POINTER;
  308. }
  309. else
  310. {
  311. ZeroMemory (&m_ShellCtx, sizeof(m_ShellCtx));
  312. m_ShellCtx.pfnAddPage = pfnAddPage;
  313. m_ShellCtx.lparam = lParam;
  314. ZeroMemory (&m_RasEntryDlg, sizeof(m_RasEntryDlg));
  315. m_RasEntryDlg.dwSize = sizeof(m_RasEntryDlg);
  316. m_RasEntryDlg.dwFlags = dwFlags | RASEDFLAG_ShellOwned;
  317. m_RasEntryDlg.reserved2 = reinterpret_cast<ULONG_PTR>(&m_ShellCtx);
  318. BOOL fRet = RasEntryDlgW (NULL, NULL, &m_RasEntryDlg);
  319. if (fRet)
  320. {
  321. Assert (m_ShellCtx.pvWizardCtx);
  322. }
  323. else
  324. {
  325. TraceError ("CRasUiBase::HrAddWizardPages: RasEntryDlg "
  326. "returned an error",
  327. HRESULT_FROM_WIN32 (m_RasEntryDlg.dwError));
  328. if (0 == m_RasEntryDlg.dwError)
  329. {
  330. bCallRasDlgEntry = FALSE;
  331. // Don't call this again if user cancelled out of TAPI phone dialog box.
  332. }
  333. // RAS may not be installed or may otherwise have a problem.
  334. // We can safely ignore any errors.
  335. }
  336. }
  337. TraceError ("CRasUiBase::HrAddWizardPages", hr);
  338. return hr;
  339. }
  340. HRESULT
  341. CRasUiBase::HrGetSuggestedConnectionName (
  342. PWSTR* ppszwSuggestedName)
  343. {
  344. HRESULT hr;
  345. // Validate parameters.
  346. //
  347. if (!ppszwSuggestedName)
  348. {
  349. hr = E_POINTER;
  350. }
  351. else
  352. {
  353. Assert (m_ShellCtx.pvWizardCtx);
  354. WCHAR pszwSuggestedName [MAX_PATH];
  355. DWORD dwErr = RasWizGetUserInputConnectionName (
  356. m_ShellCtx.pvWizardCtx,
  357. pszwSuggestedName);
  358. hr = HRESULT_FROM_WIN32 (dwErr);
  359. TraceError ("RasWizGetUserInputConnectionName", hr);
  360. if (SUCCEEDED(hr))
  361. {
  362. hr = HrCoTaskMemAllocAndDupSz (
  363. pszwSuggestedName,
  364. ppszwSuggestedName);
  365. }
  366. }
  367. TraceError ("CRasUiBase::HrGetSuggestedConnectionName", hr);
  368. return hr;
  369. }
  370. HRESULT
  371. CRasUiBase::HrSetConnectionName (
  372. PCWSTR pszwConnectionName)
  373. {
  374. HRESULT hr;
  375. // Validate parameters.
  376. //
  377. if (!pszwConnectionName)
  378. {
  379. hr = E_POINTER;
  380. }
  381. else
  382. {
  383. Assert (m_ShellCtx.pvWizardCtx);
  384. m_strConnectionName = pszwConnectionName;
  385. DWORD dwErr = RasWizSetEntryName (m_dwRasWizType,
  386. m_ShellCtx.pvWizardCtx,
  387. pszwConnectionName);
  388. hr = HRESULT_FROM_WIN32 (dwErr);
  389. TraceError ("RasWizSetEntryName", hr);
  390. }
  391. TraceError ("CRasUiBase::HrSetConnectionName", hr);
  392. return hr;
  393. }
  394. HRESULT
  395. CRasUiBase::HrGetNewConnection (
  396. INetConnection** ppCon)
  397. {
  398. static const CLSID CLSID_DialupConnection =
  399. {0xBA126AD7,0x2166,0x11D1,{0xB1,0xD0,0x00,0x80,0x5F,0xC1,0x27,0x0E}};
  400. HRESULT hr;
  401. // Validate parameters.
  402. //
  403. if (!ppCon)
  404. {
  405. hr = E_POINTER;
  406. }
  407. // Must have called SetConnectionName prior to this.
  408. //
  409. else if (m_strConnectionName.empty())
  410. {
  411. hr = E_UNEXPECTED;
  412. }
  413. else
  414. {
  415. Assert (m_ShellCtx.pvWizardCtx);
  416. // Call into rasdlg to finish creating the entry and to return
  417. // us the phonebook and entry name. We'll use them to create
  418. // the connection object.
  419. //
  420. WCHAR pszwPbkFile [MAX_PATH];
  421. WCHAR pszwEntryName [MAX_PATH];
  422. DWORD dwFlags;
  423. DWORD dwErr = RasWizCreateNewEntry (m_dwRasWizType,
  424. m_ShellCtx.pvWizardCtx,
  425. pszwPbkFile, pszwEntryName, &dwFlags);
  426. hr = HRESULT_FROM_WIN32 (dwErr);
  427. TraceError ("RasWizCreateNewEntry", hr);
  428. if (SUCCEEDED(hr))
  429. {
  430. // Create the inbound configuration connection if requested.
  431. // This will happen when the direct connect wizard is invoked
  432. // and the user chooses to be the host.
  433. //
  434. if (dwFlags & NCC_FLAG_CREATE_INCOMING)
  435. {
  436. hr = HrCreateInboundConfigConnection (ppCon);
  437. }
  438. else
  439. {
  440. // Create an uninitialized dialup connection object.
  441. // Ask for the INetRasConnection interface so we can
  442. // initialize it.
  443. //
  444. INetRasConnection* pRasCon;
  445. hr = HrCreateInstance(
  446. CLSID_DialupConnection,
  447. CLSCTX_LOCAL_SERVER | CLSCTX_NO_CODE_DOWNLOAD,
  448. &pRasCon);
  449. TraceHr(ttidError, FAL, hr, FALSE, "HrCreateInstance");
  450. if (SUCCEEDED(hr))
  451. {
  452. NcSetProxyBlanket (pRasCon);
  453. // Initialize the connection object and return the
  454. // INetConnection interface on it to the caller.
  455. //
  456. RASCON_INFO RasConInfo;
  457. ZeroMemory (&RasConInfo, sizeof(RasConInfo));
  458. RasConInfo.pszwPbkFile = pszwPbkFile;
  459. RasConInfo.pszwEntryName = pszwEntryName;
  460. hr = pRasCon->SetRasConnectionInfo (&RasConInfo);
  461. if (SUCCEEDED(hr))
  462. {
  463. hr = HrQIAndSetProxyBlanket(pRasCon, ppCon);
  464. if (S_OK == hr)
  465. {
  466. NcSetProxyBlanket (*ppCon);
  467. }
  468. }
  469. ReleaseObj (pRasCon);
  470. }
  471. }
  472. }
  473. }
  474. TraceError ("CRasUiBase::HrGetNewConnection", hr);
  475. return hr;
  476. }
  477. HRESULT
  478. CRasUiBase::HrGetNewConnectionInfo (
  479. OUT DWORD* pdwFlags)
  480. {
  481. HRESULT hr;
  482. // Validate parameters.
  483. //
  484. if (!pdwFlags)
  485. {
  486. hr = E_POINTER;
  487. }
  488. else
  489. {
  490. *pdwFlags = 0;
  491. Assert (m_ShellCtx.pvWizardCtx);
  492. DWORD dwRasFlags;
  493. DWORD dwErr = RasWizGetNCCFlags (
  494. m_dwRasWizType, m_ShellCtx.pvWizardCtx,
  495. &dwRasFlags);
  496. hr = HRESULT_FROM_WIN32 (dwErr);
  497. TraceError ("RasWizGetNCCFlags", hr);
  498. if (SUCCEEDED(hr))
  499. {
  500. if (dwRasFlags & NCC_FLAG_CREATE_INCOMING)
  501. {
  502. *pdwFlags |= NCWF_INCOMINGCONNECTION;
  503. }
  504. if (dwRasFlags & NCC_FLAG_FIREWALL)
  505. {
  506. *pdwFlags |= NCWF_FIREWALLED;
  507. }
  508. if (dwRasFlags & NCC_FLAG_SHARED)
  509. {
  510. *pdwFlags |= NCWF_SHARED;
  511. }
  512. if (dwRasFlags & NCC_FLAG_ALL_USERS)
  513. {
  514. *pdwFlags |= NCWF_ALLUSER_CONNECTION;
  515. }
  516. if (dwRasFlags & NCC_FLAG_GLOBALCREDS)
  517. {
  518. *pdwFlags |= NCWF_GLOBAL_CREDENTIALS;
  519. }
  520. if (dwRasFlags & NCC_FLAG_DEFAULT_INTERNET)
  521. {
  522. *pdwFlags |= NCWF_DEFAULT;
  523. }
  524. }
  525. else if (E_INVALIDARG == hr)
  526. {
  527. hr = E_UNEXPECTED;
  528. }
  529. }
  530. TraceError ("CRasUiBase::HrGetNewConnectionInfo", hr);
  531. return hr;
  532. }