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.

463 lines
13 KiB

  1. //Copyright (c) 1998 - 1999 Microsoft Corporation
  2. // c2cfgDlg.cpp : implementation file
  3. //
  4. #include "stdafx.h"
  5. #include "c2cfg.h"
  6. #include "c2cfgDlg.h"
  7. #include "security.h"
  8. #include <hydra\winsta.h>
  9. #include <HYDRA\regapi.h>
  10. #include "..\..\inc\utildll.h"
  11. #ifdef _DEBUG
  12. #define new DEBUG_NEW
  13. #undef THIS_FILE
  14. static char THIS_FILE[] = __FILE__;
  15. #endif
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CAboutDlg dialog used for App About
  18. class CAboutDlg : public CDialog
  19. {
  20. public:
  21. CAboutDlg();
  22. // Dialog Data
  23. //{{AFX_DATA(CAboutDlg)
  24. enum { IDD = IDD_ABOUTBOX };
  25. //}}AFX_DATA
  26. // ClassWizard generated virtual function overrides
  27. //{{AFX_VIRTUAL(CAboutDlg)
  28. protected:
  29. virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
  30. //}}AFX_VIRTUAL
  31. // Implementation
  32. protected:
  33. //{{AFX_MSG(CAboutDlg)
  34. //}}AFX_MSG
  35. DECLARE_MESSAGE_MAP()
  36. };
  37. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  38. {
  39. //{{AFX_DATA_INIT(CAboutDlg)
  40. //}}AFX_DATA_INIT
  41. }
  42. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  43. {
  44. CDialog::DoDataExchange(pDX);
  45. //{{AFX_DATA_MAP(CAboutDlg)
  46. //}}AFX_DATA_MAP
  47. }
  48. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  49. //{{AFX_MSG_MAP(CAboutDlg)
  50. // No message handlers
  51. //}}AFX_MSG_MAP
  52. END_MESSAGE_MAP()
  53. /////////////////////////////////////////////////////////////////////////////
  54. // CC2cfgDlg dialog
  55. CC2cfgDlg::CC2cfgDlg(CWnd* pParent /*=NULL*/)
  56. : CDialog(CC2cfgDlg::IDD, pParent)
  57. {
  58. //{{AFX_DATA_INIT(CC2cfgDlg)
  59. //}}AFX_DATA_INIT
  60. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  61. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  62. }
  63. void CC2cfgDlg::DoDataExchange(CDataExchange* pDX)
  64. {
  65. CDialog::DoDataExchange(pDX);
  66. //{{AFX_DATA_MAP(CC2cfgDlg)
  67. //}}AFX_DATA_MAP
  68. }
  69. BEGIN_MESSAGE_MAP(CC2cfgDlg, CDialog)
  70. //{{AFX_MSG_MAP(CC2cfgDlg)
  71. ON_WM_SYSCOMMAND()
  72. ON_WM_PAINT()
  73. ON_WM_QUERYDRAGICON()
  74. ON_BN_CLICKED(ID_HELP, OnHelp)
  75. //}}AFX_MSG_MAP
  76. END_MESSAGE_MAP()
  77. /////////////////////////////////////////////////////////////////////////////
  78. // CC2cfgDlg message handlers
  79. BOOL CC2cfgDlg::OnInitDialog()
  80. {
  81. WCHAR pwcSecLevel[sizeof("Medium")];
  82. PWCHAR pwcSecurityPath = SECURITY_REG_NAME;
  83. PWCHAR pwcSecurity = CTXSECURITY_SECURITYLEVEL;
  84. ULONG ulSize;
  85. ULONG ulType;
  86. CString sErrorString;
  87. CString sErrorTitle;
  88. CWnd *wndRButton;
  89. CString sSecLevelString;
  90. CDialog::OnInitDialog();
  91. // Add "About..." menu item to system menu.
  92. // IDM_ABOUTBOX must be in the system command range.
  93. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  94. ASSERT(IDM_ABOUTBOX < 0xF000);
  95. CMenu* pSysMenu = GetSystemMenu(FALSE);
  96. CString strAboutMenu;
  97. strAboutMenu.LoadString(IDS_ABOUTBOX);
  98. if (!strAboutMenu.IsEmpty())
  99. {
  100. pSysMenu->AppendMenu(MF_SEPARATOR);
  101. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  102. }
  103. // Set the icon for this dialog. The framework does this automatically
  104. // when the application's main window is not a dialog
  105. SetIcon(m_hIcon, TRUE); // Set big icon
  106. SetIcon(m_hIcon, FALSE); // Set small icon
  107. // TODO: Add extra initialization here
  108. if( TestUserForAdmin( TRUE ) != TRUE ) // param TRUE specifies check for domain admin
  109. {
  110. if( TestUserForAdmin( FALSE ) != TRUE ) // param FALSE specifies check for local admin
  111. { sErrorString.LoadString( IDS_NOT_ADMIN );
  112. sErrorTitle.LoadString( IDS_C2_ERR );
  113. MessageBox( sErrorString, sErrorTitle, MB_OK | MB_ICONEXCLAMATION );
  114. DestroyWindow();
  115. return FALSE;
  116. }
  117. }
  118. if( RegOpenKeyExW( HKEY_LOCAL_MACHINE, pwcSecurityPath,0,KEY_ALL_ACCESS,&hKey ) )
  119. {
  120. //error message box
  121. sErrorString.LoadString( IDS_ERR_REG );
  122. sErrorTitle.LoadString( IDS_C2_ERR );
  123. MessageBox( sErrorString, sErrorTitle,MB_OK );
  124. DestroyWindow();
  125. return FALSE;
  126. }
  127. if( RegQueryValueExW( hKey,pwcSecurity,0,&ulType,(LPBYTE)pwcSecLevel,&ulSize) )
  128. {
  129. //error message box
  130. sErrorString.LoadString( IDS_ERR_REG );
  131. sErrorTitle.LoadString( IDS_C2_ERR );
  132. MessageBox( sErrorString, sErrorTitle,MB_OK );
  133. RegCloseKey( hKey );
  134. DestroyWindow();
  135. return FALSE;
  136. }
  137. if( wcscmp( pwcSecLevel,L"Default")== 0 )
  138. {
  139. CheckRadioButton( IDC_HIGH, IDC_LOW, IDC_LOW );
  140. sSecLevelString.LoadString( IDS_TEXT_DEFAULT );
  141. }
  142. else if( wcscmp( pwcSecLevel,L"Low")== 0 )
  143. {
  144. CheckRadioButton( IDC_HIGH, IDC_LOW, IDC_LOW );
  145. sSecLevelString.LoadString( IDS_TEXT_LOW );
  146. }
  147. else if( wcscmp( pwcSecLevel,L"Medium")== 0 )
  148. {
  149. CheckRadioButton( IDC_HIGH, IDC_LOW, IDC_MED );
  150. sSecLevelString.LoadString( IDS_TEXT_MED );
  151. // disable low button
  152. wndRButton = GetDlgItem( IDC_LOW );
  153. wndRButton->EnableWindow( FALSE );
  154. }
  155. else
  156. {
  157. CheckRadioButton( IDC_HIGH, IDC_LOW, IDC_HIGH );
  158. sSecLevelString.LoadString( IDS_TEXT_HIGH );
  159. //disable other two buttons
  160. wndRButton = GetDlgItem( IDC_MED );
  161. wndRButton->EnableWindow( FALSE );
  162. wndRButton = GetDlgItem( IDC_LOW );
  163. wndRButton->EnableWindow( FALSE );
  164. }
  165. SetDlgItemText( IDC_STATUS, sSecLevelString );
  166. return TRUE; // return TRUE unless you set the focus to a control
  167. }
  168. void CC2cfgDlg::OnSysCommand(UINT nID, LPARAM lParam)
  169. {
  170. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  171. {
  172. CAboutDlg dlgAbout;
  173. dlgAbout.DoModal();
  174. }
  175. else
  176. {
  177. CDialog::OnSysCommand(nID, lParam);
  178. }
  179. }
  180. // If you add a minimize button to your dialog, you will need the code below
  181. // to draw the icon. For MFC applications using the document/view model,
  182. // this is automatically done for you by the framework.
  183. void CC2cfgDlg::OnPaint()
  184. {
  185. if (IsIconic())
  186. {
  187. CPaintDC dc(this); // device context for painting
  188. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  189. // Center icon in client rectangle
  190. int cxIcon = GetSystemMetrics(SM_CXICON);
  191. int cyIcon = GetSystemMetrics(SM_CYICON);
  192. CRect rect;
  193. GetClientRect(&rect);
  194. int x = (rect.Width() - cxIcon + 1) / 2;
  195. int y = (rect.Height() - cyIcon + 1) / 2;
  196. // Draw the icon
  197. dc.DrawIcon(x, y, m_hIcon);
  198. }
  199. else
  200. {
  201. CDialog::OnPaint();
  202. }
  203. }
  204. // The system calls this to obtain the cursor to display while the user drags
  205. // the minimized window.
  206. HCURSOR CC2cfgDlg::OnQueryDragIcon()
  207. {
  208. return (HCURSOR) m_hIcon;
  209. }
  210. HKEY g_RegEventKey;
  211. void CC2cfgDlg::OnOK()
  212. {
  213. // TODO: Add extra validation here
  214. WCHAR pwcPath[MAX_PATH];
  215. WCHAR pwc_src[MAX_PATH];
  216. WCHAR pwc_dest[MAX_PATH];
  217. WCHAR pwcNTF_file[20];
  218. WCHAR pwcREG_file[20];
  219. WCHAR C2CONFIG[] = L"c2config";
  220. WCHAR pwcSecLevel[sizeof("Medium")];
  221. ULONG ulSize;
  222. PWCHAR pwcSecurity = CTXSECURITY_SECURITYLEVEL;
  223. WCHAR szDir[MAX_PATH];
  224. CString sErrorString;
  225. CString sErrorTitle;
  226. CString sMessage;
  227. CString sMessageTitle;
  228. DWORD idRegSecCheck,
  229. idDirSecCheck;
  230. /************
  231. Structures containing event handles to wait on and boolean variable
  232. to set when event occurs
  233. **************/
  234. EVENT_CHECK_TYPE DirectoryEventCheck,
  235. RegistryEventCheck;
  236. BOOLEAN PosixDeleted = FALSE;
  237. BOOLEAN OS2Deleted = FALSE;
  238. WCHAR szBuffer[MAX_PATH];
  239. WCHAR szFileName[MAX_PATH];
  240. PWCHAR pwc;
  241. STARTUPINFOW StartUpInfo;
  242. PROCESS_INFORMATION ProcessInfo;
  243. if( IsDlgButtonChecked(IDC_HIGH) )
  244. {
  245. wcscpy( pwcNTF_file, L"\\c2ntfhi.inf" );
  246. wcscpy( pwcREG_file, L"\\c2reghi.inf" );
  247. wcscpy( pwcSecLevel, L"High");
  248. }
  249. else if( IsDlgButtonChecked(IDC_MED) )
  250. {
  251. wcscpy( pwcNTF_file, L"\\c2ntfmed.inf" );
  252. wcscpy( pwcREG_file, L"\\c2regmed.inf" );
  253. wcscpy( pwcSecLevel, L"Medium" );
  254. }
  255. else
  256. {
  257. // low
  258. wcscpy( pwcNTF_file, L"\\c2ntflow.inf" );
  259. wcscpy( pwcREG_file, L"\\c2reglow.inf" );
  260. wcscpy( pwcSecLevel, L"Low");
  261. }
  262. /*************************************************************************************
  263. What I'm trying to do: C2config.exe takes in the files c2regacl.inf (registry)
  264. and c2ntfacl.inf( file system ). These files are scripts to set the ACL's on the
  265. registry and file system . There a 3 different
  266. flavors of each of these files. One each for LOW, MEDIUM, and HIGH c2 security. I
  267. simply select a file based on the security level selected and then copy it to the
  268. the generic name c2regacl.inf or c2ntfacl.inf.
  269. The user must still run 2 functions in c2config.exe to use these inf files. After
  270. c2config.exe runs I do a check to see if they were run.
  271. *************************************************************************************/
  272. GetSystemDirectoryW( pwcPath, MAX_PATH );
  273. wcscpy( pwc_src, pwcPath );
  274. wcscat( pwc_src, pwcNTF_file );
  275. wcscpy( pwc_dest, pwcPath );
  276. wcscat( pwc_dest, L"\\c2ntfacl.inf" );
  277. if( !CopyFileW(pwc_src, pwc_dest, FALSE) )
  278. {
  279. //error message box
  280. sErrorString.Format( IDS_ERR_NO_FILE, pwcNTF_file+1 );
  281. sErrorTitle.LoadString( IDS_C2_ERR );
  282. MessageBox( sErrorString, sErrorTitle,MB_OK );
  283. RegCloseKey( hKey );
  284. CDialog::OnOK();
  285. return;
  286. }
  287. *pwc_src = *pwc_dest = L'\0';
  288. wcscpy( pwc_src, pwcPath );
  289. wcscat( pwc_src, pwcREG_file );
  290. wcscpy( pwc_dest, pwcPath );
  291. wcscat( pwc_dest, L"\\c2regacl.inf" );
  292. if( !CopyFileW(pwc_src, pwc_dest, FALSE) )
  293. {
  294. //send an error box out
  295. sErrorString.Format( IDS_ERR_NO_FILE, pwcREG_file+1 );
  296. sErrorTitle.LoadString( IDS_C2_ERR );
  297. MessageBox( sErrorString, sErrorTitle,MB_OK );
  298. RegCloseKey( hKey );
  299. CDialog::OnOK();
  300. return;
  301. }
  302. memset( &StartUpInfo,'\0', sizeof(STARTUPINFO) );
  303. StartUpInfo.cb = sizeof(STARTUPINFO);
  304. StartUpInfo.wShowWindow = SW_SHOWDEFAULT;
  305. /*****************************************************************************
  306. These threads are started to check if c2config is run correctly -- changing
  307. the security ACL's on the registry and specified files. Theses threads are
  308. passed an event handle and a boolean variable. If the security they are
  309. looking at is changed, the event is triggered and the bolean variable is set.
  310. *****************************************************************************/
  311. RegistryEventCheck.handle = CreateEvent( NULL, FALSE, FALSE, NULL );
  312. RegistryEventCheck.bEventTriggered = FALSE;
  313. if( RegistryEventCheck.handle != NULL )
  314. {
  315. CreateThread( NULL,
  316. 0,
  317. (LPTHREAD_START_ROUTINE)RegistrySecurityCheck,
  318. &RegistryEventCheck,
  319. 0,
  320. &idRegSecCheck
  321. );
  322. }
  323. /* I only want to keep the drive letter info part of the system directory */
  324. GetSystemDirectoryW( szDir, MAX_PATH );
  325. szDir[3] = L'\0';
  326. DirectoryEventCheck.handle = FindFirstChangeNotificationW( szDir,
  327. TRUE,
  328. FILE_NOTIFY_CHANGE_SECURITY );
  329. DirectoryEventCheck.bEventTriggered = FALSE;
  330. if( DirectoryEventCheck.handle != INVALID_HANDLE_VALUE )
  331. {
  332. CreateThread( NULL,
  333. 0,
  334. (LPTHREAD_START_ROUTINE)DirectorySecurityCheck,
  335. &DirectoryEventCheck,
  336. 0,
  337. &idDirSecCheck
  338. );
  339. }
  340. /******* Run C2config and then I'll check to see what the user did **************/
  341. CreateProcessW(NULL, C2CONFIG, NULL, NULL, FALSE, 0, NULL, NULL, &StartUpInfo, &ProcessInfo );
  342. WaitForSingleObject( ProcessInfo.hProcess, INFINITE );
  343. /********************************************************************
  344. I can't guarentee that the created threads ever stop waiting for an
  345. event. So they would not always be able to close there handles. So
  346. I do it here.
  347. ***********************************************************************/
  348. RegCloseKey( g_RegEventKey );
  349. CloseHandle( RegistryEventCheck.handle );
  350. FindCloseChangeNotification( DirectoryEventCheck.handle );
  351. /**********************************************************************
  352. Both the Registry and the Directory Security levels must be set, or
  353. I do not record the change in the directory.
  354. **********************************************************************/
  355. wcscpy( szFileName, L"psxss.exe" );
  356. if( SearchPathW( NULL, szFileName, NULL, MAX_PATH, szBuffer, &pwc ) == 0 )
  357. PosixDeleted = TRUE;
  358. wcscpy( szFileName, L"os2.exe" );
  359. if( SearchPathW( NULL, szFileName, NULL, MAX_PATH, szBuffer, &pwc ) == 0 )
  360. OS2Deleted = TRUE;
  361. if( (RegistryEventCheck.bEventTriggered == TRUE) &&
  362. (DirectoryEventCheck.bEventTriggered == TRUE) &&
  363. (PosixDeleted == TRUE) &&
  364. (OS2Deleted == TRUE)
  365. )
  366. {
  367. ulSize = ( wcslen(pwcSecLevel) + 1 ) * sizeof(WCHAR);
  368. RegSetValueExW( hKey,pwcSecurity,0,REG_SZ,(LPBYTE)pwcSecLevel,ulSize );
  369. sMessage.Format( IDS_SUCCESS_FM, pwcSecLevel );
  370. sMessageTitle.LoadString( IDS_C2 );
  371. MessageBox( sMessage, sMessageTitle, MB_OK );
  372. }
  373. else
  374. {
  375. sMessage.LoadString( IDS_FAIL );
  376. sMessageTitle.LoadString( IDS_C2 );
  377. MessageBox( sMessage, sMessageTitle,MB_OK );
  378. }
  379. RegCloseKey( hKey );
  380. CDialog::OnOK();
  381. }
  382. void CC2cfgDlg::WinHelp(DWORD dwData, UINT nCmd)
  383. {
  384. // TODO: Add your specialized code here and/or call the base class
  385. CDialog::WinHelp(dwData, nCmd);
  386. }
  387. void CC2cfgDlg::OnHelp()
  388. {
  389. // TODO: Add your control notification handler code here
  390. WinHelp(0, HELP_CONTENTS);
  391. }