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.

597 lines
9.2 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997 - 2000
  5. //
  6. // File: H N A P I M G R. C P P
  7. //
  8. // Contents: OEM API
  9. //
  10. // Notes:
  11. //
  12. // Author: billi 21 Nov 2000
  13. //
  14. //----------------------------------------------------------------------------
  15. #include "pch.h"
  16. #pragma hdrstop
  17. #include <sddl.h>
  18. #include <wchar.h>
  19. HINSTANCE g_hOemInstance = NULL;
  20. BOOLEAN g_fOemNotifyUser = TRUE;
  21. BOOLEAN g_fSavedNotifyState = FALSE;
  22. BOOLEAN IsSecureContext()
  23. /*++
  24. IsSecureContext
  25. Routine Description:
  26. This routine checks if the current user belongs to an Administrator Group.
  27. Arguments:
  28. none
  29. Return Value:
  30. TRUE = Current process does belong to an Administrator group
  31. FALSE = Current process does Not belong to an Administrator group
  32. --*/
  33. {
  34. PSID psidAdministrators;
  35. BOOL bIsAdministrator = FALSE;
  36. SID_IDENTIFIER_AUTHORITY siaNtAuthority = SECURITY_NT_AUTHORITY;
  37. BOOL bResult = AllocateAndInitializeSid( &siaNtAuthority, 2,
  38. SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS,
  39. 0, 0, 0, 0, 0, 0, &psidAdministrators );
  40. _ASSERT( bResult );
  41. if ( bResult )
  42. {
  43. bResult = CheckTokenMembership( NULL, psidAdministrators, &bIsAdministrator );
  44. _ASSERT( bResult );
  45. FreeSid( psidAdministrators );
  46. }
  47. return (BOOLEAN)bIsAdministrator;
  48. }
  49. /*++
  50. CenterWindow
  51. Routine Description:
  52. Arguments:
  53. none
  54. Return Value:
  55. none
  56. --*/
  57. BOOLEAN
  58. CenterDialog(
  59. HWND hwndDlg // handle to dialog box
  60. )
  61. {
  62. RECT rcDlg, rcDesktop;
  63. HWND hwndDesktop;
  64. hwndDesktop = GetDesktopWindow();
  65. if ( GetWindowRect( hwndDlg, &rcDlg ) && GetWindowRect( hwndDesktop, &rcDesktop ) )
  66. {
  67. RECT rcCenter;
  68. // Create a rectangle in the middle of the screen
  69. rcDesktop.right -= rcDesktop.left;
  70. rcDlg.right -= rcDlg.left;
  71. rcDesktop.bottom -= rcDesktop.top;
  72. rcDlg.bottom -= rcDlg.top;
  73. if ( rcDesktop.right > rcDlg.right )
  74. {
  75. rcCenter.left = rcDesktop.left + ((rcDesktop.right - rcDlg.right) / 2);
  76. rcCenter.right = rcCenter.left + rcDlg.right;
  77. }
  78. else
  79. {
  80. rcCenter.left = rcDesktop.left;
  81. rcCenter.right = rcDesktop.right;
  82. }
  83. if ( rcDesktop.bottom > rcDlg.bottom )
  84. {
  85. rcCenter.top = rcDesktop.top + ((rcDesktop.bottom - rcDlg.bottom) / 2);
  86. rcCenter.bottom = rcCenter.top + rcDlg.bottom;
  87. }
  88. else
  89. {
  90. rcCenter.top = rcDesktop.top;
  91. rcCenter.bottom = rcDesktop.bottom;
  92. }
  93. return (BOOLEAN)SetWindowPos( hwndDlg, NULL,
  94. rcCenter.left, rcCenter.top, 0, 0,
  95. SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER );
  96. }
  97. return FALSE;
  98. }
  99. INT_PTR CALLBACK OemNotifyDialogProc(
  100. HWND hwndDlg, // handle to dialog box
  101. UINT uMsg, // message
  102. WPARAM wParam, // first message parameter
  103. LPARAM lParam // second message parameter
  104. )
  105. /*++
  106. OemNotifyDialogProc
  107. Routine Description:
  108. Arguments:
  109. none
  110. Return Value:
  111. none
  112. --*/
  113. {
  114. switch ( uMsg )
  115. {
  116. case WM_INITDIALOG:
  117. if ( CenterDialog( hwndDlg ) )
  118. {
  119. LPTSTR lpszFmt = new TCHAR[ NOTIFYFORMATBUFFERSIZE ];
  120. if ( NULL != lpszFmt )
  121. {
  122. if ( LoadString( g_hOemInstance,
  123. IDS_SECURITYNOTIFICATIONTEXT,
  124. lpszFmt,
  125. NOTIFYFORMATBUFFERSIZE ) > 0 )
  126. {
  127. TCHAR lpszCmdLine[MAX_PATH*2+1] = {0};
  128. GetModuleFileName (NULL, lpszCmdLine, MAX_PATH*2);
  129. LPTSTR lpszMsg = new TCHAR[ lstrlen(lpszCmdLine)*2 +
  130. lstrlen(lpszFmt) + 2 ];
  131. if ( NULL != lpszMsg )
  132. {
  133. wsprintf( lpszMsg, lpszFmt, lpszCmdLine, lpszCmdLine );
  134. SetDlgItemText( hwndDlg, IDC_TXT_NOTIFICATION, lpszMsg );
  135. delete lpszMsg;
  136. }
  137. }
  138. delete lpszFmt;
  139. }
  140. }
  141. break;
  142. case WM_COMMAND:
  143. switch ( LOWORD(wParam) )
  144. {
  145. case IDOK:
  146. // Fall through.
  147. case IDCANCEL:
  148. if ( IsDlgButtonChecked( hwndDlg, IDC_CHK_DISABLESHARESECURITYWARN )
  149. == BST_CHECKED )
  150. {
  151. g_fOemNotifyUser = FALSE;
  152. }
  153. EndDialog( hwndDlg, wParam );
  154. return TRUE;
  155. }
  156. break;
  157. }
  158. return FALSE;
  159. }
  160. BOOLEAN IsNotifyApproved()
  161. /*++
  162. IsNotifyApproved
  163. Routine Description:
  164. IsSecureContext, g_fOemNotifyUser, g_fSavedNotifyState, DialogBox determine the
  165. value returned. IsSecureContext MUST be TRUE to return TRUE. g_fSavedNotifyState
  166. holds the value returned by DialogBox on the previous call.
  167. Arguments:
  168. none
  169. Return Value:
  170. TRUE
  171. FALSE
  172. --*/
  173. {
  174. BOOLEAN bApproved = FALSE;
  175. if ( IsSecureContext() )
  176. {
  177. if ( g_fOemNotifyUser )
  178. {
  179. g_fSavedNotifyState = ( DialogBox( g_hOemInstance,
  180. MAKEINTRESOURCE(IDD_SecurityNotification),
  181. NULL,
  182. OemNotifyDialogProc ) == IDOK ) ?
  183. TRUE : FALSE;
  184. g_fOemNotifyUser = FALSE;
  185. }
  186. bApproved = g_fSavedNotifyState;
  187. }
  188. return bApproved;
  189. }
  190. HRESULT InitializeOemApi(
  191. HINSTANCE hInstance
  192. )
  193. /*++
  194. InitializedOemApi
  195. Routine Description:
  196. Arguments:
  197. none
  198. Return Value:
  199. HRESULT
  200. --*/
  201. {
  202. g_hOemInstance = hInstance;
  203. g_fOemNotifyUser = TRUE;
  204. g_fSavedNotifyState = FALSE;
  205. return S_OK;
  206. }
  207. HRESULT ReleaseOemApi()
  208. /*++
  209. ReleaseOemApi
  210. Routine Description:
  211. Arguments:
  212. none
  213. Return Value:
  214. HRESULT
  215. --*/
  216. {
  217. g_hOemInstance = NULL;
  218. return S_OK;
  219. }
  220. static HRESULT
  221. _ObtainCfgMgrObj(
  222. IHNetCfgMgr** ppHNetCfgMgr)
  223. /*++
  224. _ObtainCfgMgrObj
  225. Routine Description:
  226. Arguments:
  227. none
  228. Return Value:
  229. none
  230. --*/
  231. {
  232. HRESULT hr = S_OK;
  233. if ( NULL == ppHNetCfgMgr )
  234. {
  235. hr = E_POINTER;
  236. }
  237. else
  238. {
  239. hr = CoCreateInstance(
  240. CLSID_HNetCfgMgr,
  241. NULL,
  242. CLSCTX_INPROC_SERVER,
  243. IID_PPV_ARG(IHNetCfgMgr, ppHNetCfgMgr)
  244. );
  245. _ASSERT(NULL != *ppHNetCfgMgr);
  246. }
  247. return hr;
  248. }
  249. /*++
  250. _ObtainIcsSettingsObj
  251. Routine Description:
  252. Arguments:
  253. ppIcs -
  254. Return Value:
  255. HRESULT
  256. --*/
  257. HRESULT
  258. _ObtainIcsSettingsObj( IHNetIcsSettings** ppIcsSettings )
  259. {
  260. HRESULT hr;
  261. IHNetCfgMgr* pCfgMgr;
  262. hr = _ObtainCfgMgrObj( &pCfgMgr );
  263. if ( SUCCEEDED(hr) )
  264. {
  265. // Obtain interface pointer to the ICS Settings and enumerator for
  266. // public connections
  267. hr = pCfgMgr->QueryInterface(
  268. IID_PPV_ARG(IHNetIcsSettings, ppIcsSettings) );
  269. ReleaseObj( pCfgMgr );
  270. }
  271. return hr;
  272. }
  273. HRESULT
  274. CNetSharingConfiguration::Initialize(
  275. INetConnection *pNetConnection
  276. )
  277. /*++
  278. CNetSharingConfiguration::Initialize
  279. Routine Description:
  280. Arguments:
  281. none
  282. Return Value:
  283. none
  284. --*/
  285. {
  286. HRESULT hr;
  287. IHNetCfgMgr* pCfgMgr;
  288. hr = _ObtainCfgMgrObj( &pCfgMgr );
  289. if ( SUCCEEDED(hr) )
  290. {
  291. IHNetConnection* pHNetConnection;
  292. hr = pCfgMgr->GetIHNetConnectionForINetConnection( pNetConnection, &pHNetConnection );
  293. if ( SUCCEEDED(hr) )
  294. {
  295. IHNetProtocolSettings* pSettings;
  296. hr = pCfgMgr->QueryInterface(
  297. IID_PPV_ARG(IHNetProtocolSettings, &pSettings) );
  298. _ASSERT( SUCCEEDED(hr) );
  299. if ( SUCCEEDED(hr) )
  300. {
  301. EnterCriticalSection(&m_csSharingConfiguration);
  302. ReleaseObj(m_pHNetConnection);
  303. m_pHNetConnection = pHNetConnection;
  304. m_pHNetConnection->AddRef();
  305. ReleaseObj(m_pSettings);
  306. m_pSettings = pSettings;
  307. m_pSettings->AddRef();
  308. LeaveCriticalSection(&m_csSharingConfiguration);
  309. ReleaseObj(pSettings);
  310. }
  311. ReleaseObj(pHNetConnection);
  312. }
  313. ReleaseObj(pCfgMgr);
  314. }
  315. return hr;
  316. }
  317. /*++
  318. CNetSharingManager::GetSharingInstalled
  319. Routine Description:
  320. Arguments:
  321. none
  322. Return Value:
  323. none
  324. --*/
  325. STDMETHODIMP
  326. CNetSharingManager::get_SharingInstalled(
  327. VARIANT_BOOL *pbInstalled )
  328. {
  329. HNET_OEM_API_ENTER
  330. HRESULT hr = S_OK;
  331. if ( NULL == pbInstalled )
  332. {
  333. hr = E_POINTER;
  334. }
  335. else
  336. {
  337. BOOLEAN bInstalled = FALSE;
  338. SC_HANDLE ScmHandle;
  339. SC_HANDLE ServiceHandle;
  340. // Connect to the service control manager
  341. ScmHandle = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
  342. if ( ScmHandle )
  343. {
  344. // Open the shared access service
  345. ServiceHandle = OpenService( ScmHandle, c_wszSharedAccess, SERVICE_ALL_ACCESS );
  346. if ( ServiceHandle )
  347. {
  348. bInstalled = TRUE;
  349. CloseServiceHandle(ServiceHandle);
  350. }
  351. CloseServiceHandle(ScmHandle);
  352. }
  353. *pbInstalled = bInstalled ? VARIANT_TRUE : VARIANT_FALSE;
  354. }
  355. return hr;
  356. HNET_OEM_API_LEAVE
  357. }
  358. /*++
  359. CNetSharingManager::GetINetSharingConfigurationForINetConnection
  360. Routine Description:
  361. Arguments:
  362. none
  363. Return Value:
  364. none
  365. --*/
  366. STDMETHODIMP
  367. CNetSharingManager::get_INetSharingConfigurationForINetConnection(
  368. INetConnection* pNetConnection,
  369. INetSharingConfiguration** ppNetSharingConfiguration
  370. )
  371. {
  372. HNET_OEM_API_ENTER
  373. HRESULT hr;
  374. if ( NULL == ppNetSharingConfiguration )
  375. {
  376. hr = E_POINTER;
  377. }
  378. else if ( NULL == pNetConnection )
  379. {
  380. hr = E_INVALIDARG;
  381. }
  382. else
  383. {
  384. CComObject<CNetSharingConfiguration>* pNetConfig;
  385. hr = CComObject<CNetSharingConfiguration>::CreateInstance(&pNetConfig);
  386. if ( SUCCEEDED(hr) )
  387. {
  388. pNetConfig->AddRef();
  389. hr = pNetConfig->Initialize(pNetConnection);
  390. if ( SUCCEEDED(hr) )
  391. {
  392. hr = pNetConfig->QueryInterface(
  393. IID_PPV_ARG( INetSharingConfiguration, ppNetSharingConfiguration ) );
  394. }
  395. ReleaseObj(pNetConfig);
  396. }
  397. }
  398. return hr;
  399. HNET_OEM_API_LEAVE
  400. }