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.

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