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.

500 lines
13 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation 1996-2001.
  5. //
  6. // File: cservice.cpp
  7. //
  8. // Contents: implementation of CConfigService
  9. //
  10. //----------------------------------------------------------------------------
  11. #include "stdafx.h"
  12. #include "wsecmgr.h"
  13. #include "resource.h"
  14. #include "snapmgr.h"
  15. #include "attr.h"
  16. #include "Cservice.h"
  17. #include "util.h"
  18. #include "servperm.h"
  19. #ifdef _DEBUG
  20. #define new DEBUG_NEW
  21. #undef THIS_FILE
  22. static char THIS_FILE[] = __FILE__;
  23. #endif
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CConfigService dialog
  26. CConfigService::CConfigService(UINT nTemplateID)
  27. : CAttribute(nTemplateID ? nTemplateID : IDD),
  28. m_hwndSecurity(NULL),
  29. m_pNewSD(NULL),
  30. m_NewSeInfo(0)
  31. {
  32. //{{AFX_DATA_INIT(CConfigService)
  33. m_nStartupRadio = -1;
  34. //}}AFX_DATA_INIT
  35. m_pHelpIDs = (DWORD_PTR) a195HelpIDs;
  36. m_uTemplateResID = IDD;
  37. }
  38. void CConfigService::DoDataExchange(CDataExchange* pDX)
  39. {
  40. CAttribute::DoDataExchange(pDX);
  41. //{{AFX_DATA_MAP(CConfigService)
  42. DDX_Radio(pDX, IDC_ENABLED, m_nStartupRadio);
  43. DDX_Control(pDX, IDC_BASESD, m_bPermission);
  44. //}}AFX_DATA_MAP
  45. }
  46. BEGIN_MESSAGE_MAP(CConfigService, CAttribute)
  47. //{{AFX_MSG_MAP(CConfigService)
  48. ON_BN_CLICKED(IDC_CONFIGURE, OnConfigure)
  49. ON_BN_CLICKED(IDC_BASESD, OnChangeSecurity)
  50. ON_BN_CLICKED(IDC_DISABLED, OnDisabled)
  51. ON_BN_CLICKED(IDC_IGNORE, OnIgnore)
  52. ON_BN_CLICKED(IDC_ENABLED, OnEnabled)
  53. //}}AFX_MSG_MAP
  54. END_MESSAGE_MAP()
  55. /////////////////////////////////////////////////////////////////////////////
  56. // CConfigService message handlers
  57. BOOL CConfigService::OnApply()
  58. {
  59. if ( !m_bReadOnly )
  60. {
  61. DWORD dw = 0;
  62. int status = 0;
  63. UpdateData(TRUE);
  64. PEDITTEMPLATE pTemp = m_pData->GetBaseProfile();
  65. if (!m_bConfigure )
  66. {
  67. if ( m_pData->GetBase() != 0 )
  68. {
  69. if ( pTemp != NULL && pTemp->pTemplate != NULL )
  70. {
  71. //
  72. // look for the address stored in m_pData->GetBase()
  73. // if found it, delete it.
  74. //
  75. PSCE_SERVICES pServParent, pService;
  76. for ( pService=pTemp->pTemplate->pServices, pServParent=NULL;
  77. pService != NULL; pServParent=pService, pService=pService->Next )
  78. {
  79. if (pService == (PSCE_SERVICES)m_pData->GetBase() )
  80. {
  81. //
  82. // a configured service becomes not configured
  83. //
  84. if ( pServParent == NULL )
  85. {
  86. // the first service
  87. pTemp->pTemplate->pServices = pService->Next;
  88. }
  89. else
  90. pServParent->Next = pService->Next;
  91. pService->Next = NULL;
  92. break;
  93. }
  94. }
  95. m_pData->SetBase(NULL); //Raid #378271, 4/27/2001
  96. }
  97. else
  98. {
  99. // should never happen
  100. //
  101. // free the service node
  102. //
  103. SceFreeMemory((PVOID)(m_pData->GetBase()), SCE_STRUCT_SERVICES);
  104. m_pData->SetBase(0);
  105. }
  106. }
  107. if ( m_pNewSD )
  108. {
  109. LocalFree(m_pNewSD);
  110. m_pNewSD = NULL;
  111. }
  112. }
  113. else
  114. {
  115. switch(m_nStartupRadio)
  116. {
  117. case 0:
  118. // Automatic
  119. dw = SCE_STARTUP_AUTOMATIC;
  120. break;
  121. case 1:
  122. // Manual
  123. dw = SCE_STARTUP_MANUAL;
  124. break;
  125. case 2:
  126. // DISABLED
  127. dw = SCE_STARTUP_DISABLED;
  128. break;
  129. default: //Raid #470209, Yang Gao.
  130. //When it is configured it must have one of above values
  131. CString msg;
  132. msg.LoadString(IDS_ERROR_NO_START_MODE);
  133. AfxMessageBox(msg, MB_OK|MB_ICONEXCLAMATION); //Raid #495010, yanggao
  134. return FALSE;
  135. }
  136. PSCE_SERVICES pNode=(PSCE_SERVICES)(m_pData->GetBase());
  137. if ( NULL == pNode )
  138. {
  139. //
  140. // a node is changed from not configured to configured
  141. //
  142. pNode = CreateServiceNode(m_pData->GetUnits(),
  143. m_pData->GetAttr(),
  144. dw,
  145. m_pNewSD,
  146. m_NewSeInfo);
  147. if ( pNode != NULL )
  148. {
  149. //
  150. // add to the service list
  151. //
  152. pNode->Next = pTemp->pTemplate->pServices;
  153. pTemp->pTemplate->pServices = pNode;
  154. m_pData->SetBase((LONG_PTR)pNode);
  155. m_pNewSD = NULL;
  156. }
  157. else
  158. {
  159. //
  160. // no memory, error out
  161. //
  162. if ( m_pNewSD )
  163. {
  164. LocalFree(m_pNewSD);
  165. m_pNewSD = NULL;
  166. }
  167. }
  168. }
  169. else
  170. {
  171. //
  172. // an existing service
  173. //
  174. pNode->Startup = (BYTE)dw;
  175. if ( m_pNewSD != NULL )
  176. {
  177. if ( pNode->General.pSecurityDescriptor != m_pNewSD &&
  178. pNode->General.pSecurityDescriptor != NULL )
  179. {
  180. LocalFree(pNode->General.pSecurityDescriptor);
  181. }
  182. pNode->General.pSecurityDescriptor = m_pNewSD;
  183. m_pNewSD = NULL;
  184. pNode->SeInfo = m_NewSeInfo;
  185. }
  186. }
  187. }
  188. m_pData->Update(m_pSnapin);
  189. m_NewSeInfo = 0;
  190. m_hwndParent = NULL;
  191. }
  192. return CAttribute::OnApply();
  193. }
  194. void CConfigService::OnCancel()
  195. {
  196. if ( m_pNewSD )
  197. {
  198. LocalFree(m_pNewSD);
  199. m_pNewSD = NULL;
  200. }
  201. m_NewSeInfo = 0;
  202. m_hwndParent = NULL;
  203. m_bConfigure = m_bOriginalConfigure;
  204. CAttribute::OnCancel();
  205. }
  206. BOOL CConfigService::OnInitDialog()
  207. {
  208. CAttribute::OnInitDialog();
  209. m_bOriginalConfigure = m_bConfigure;
  210. AddUserControl(IDC_ENABLED);
  211. AddUserControl(IDC_DISABLED);
  212. AddUserControl(IDC_IGNORE);
  213. AddUserControl(IDC_BASESD);
  214. if (QueryReadOnly())
  215. {
  216. CString str;
  217. str.LoadString(IDS_VIEW_SECURITY);
  218. if ( GetDlgItem(IDC_SECURITY) )
  219. SetDlgItemText(IDC_SECURITY,str);
  220. else if ( GetDlgItem(IDC_BASESD) )
  221. SetDlgItemText(IDC_BASESD,str);
  222. }
  223. OnConfigure();
  224. return TRUE; // return TRUE unless you set the focus to a control
  225. // EXCEPTION: OCX Property Pages should return FALSE
  226. }
  227. void CConfigService::Initialize(CResult * pResult)
  228. {
  229. CAttribute::Initialize(pResult);
  230. PSCE_SERVICES pService;
  231. pService = (PSCE_SERVICES)(pResult->GetBase());
  232. if ( NULL == pService ) {
  233. m_bConfigure = FALSE;
  234. pService = (PSCE_SERVICES)(pResult->GetSetting());
  235. }
  236. m_pNewSD = NULL;
  237. m_NewSeInfo = 0;
  238. if ( pService != NULL ) {
  239. switch ( pService->Startup ) {
  240. case SCE_STARTUP_AUTOMATIC:
  241. m_nStartupRadio = 0;
  242. break;
  243. case SCE_STARTUP_MANUAL:
  244. m_nStartupRadio = 1;
  245. break;
  246. case SCE_STARTUP_DISABLED: // disabled
  247. m_nStartupRadio = 2;
  248. break;
  249. default: // not defined //Raid #470209, Yang Gao
  250. m_nStartupRadio = -1;
  251. break;
  252. }
  253. //
  254. // initialize SD and SeInfo
  255. //
  256. if ( pService->General.pSecurityDescriptor ) {
  257. MyMakeSelfRelativeSD(pService->General.pSecurityDescriptor,
  258. &m_pNewSD);
  259. }
  260. m_NewSeInfo = pService->SeInfo;
  261. }
  262. }
  263. PSCE_SERVICES
  264. CreateServiceNode(LPTSTR ServiceName,
  265. LPTSTR DisplayName,
  266. DWORD Startup,
  267. PSECURITY_DESCRIPTOR pSD,
  268. SECURITY_INFORMATION SeInfo)
  269. {
  270. if ( NULL == ServiceName ) {
  271. return NULL;
  272. }
  273. PSCE_SERVICES pTemp;
  274. pTemp = (PSCE_SERVICES)LocalAlloc(0,sizeof(SCE_SERVICES));
  275. if ( pTemp != NULL ) {
  276. pTemp->ServiceName = (LPTSTR)LocalAlloc(0, (wcslen(ServiceName)+1)*sizeof(TCHAR));
  277. if ( pTemp->ServiceName != NULL ) {
  278. if ( DisplayName != NULL ) {
  279. pTemp->DisplayName = (LPTSTR)LocalAlloc(0, (wcslen(DisplayName)+1)*sizeof(TCHAR));
  280. if ( pTemp->DisplayName != NULL ) {
  281. //This may not be a safe usage. pTemp->DisplayName is PWSTR, Consider fix.
  282. wcscpy(pTemp->DisplayName, DisplayName);
  283. } else {
  284. // no memory to allocate
  285. LocalFree(pTemp->ServiceName);
  286. LocalFree(pTemp);
  287. return NULL;
  288. }
  289. } else
  290. pTemp->DisplayName = NULL;
  291. //This may not be a safe usage. pTemp->ServiceName is PWSTR, Consider fix.
  292. wcscpy(pTemp->ServiceName, ServiceName);
  293. pTemp->Status = 0;
  294. pTemp->Startup = (BYTE)Startup;
  295. pTemp->General.pSecurityDescriptor = pSD;
  296. pTemp->SeInfo = SeInfo;
  297. pTemp->Next = NULL;
  298. return pTemp;
  299. } else {
  300. // no memory to allocate
  301. LocalFree(pTemp);
  302. return NULL;
  303. }
  304. }
  305. return NULL;
  306. }
  307. void CConfigService::OnConfigure()
  308. {
  309. CAttribute::OnConfigure();
  310. if( -1 == m_nStartupRadio && m_bConfigure ) //Raid #485374, Yanggao, 11/2/2001
  311. {
  312. PSCE_SERVICES pService = (PSCE_SERVICES)m_pData->GetProfileDefault();
  313. if( SCE_NO_VALUE != (DWORD)PtrToUlong((PVOID)pService) )
  314. {
  315. switch ( pService->Startup )
  316. {
  317. case SCE_STARTUP_AUTOMATIC:
  318. m_nStartupRadio = 0;
  319. break;
  320. case SCE_STARTUP_MANUAL:
  321. m_nStartupRadio = 1;
  322. break;
  323. case SCE_STARTUP_DISABLED:
  324. m_nStartupRadio = 2;
  325. break;
  326. default:
  327. m_nStartupRadio = -1;
  328. break;
  329. }
  330. UpdateData(FALSE);
  331. }
  332. }
  333. }
  334. void CConfigService::OnChangeSecurity()
  335. {
  336. if (IsWindow(m_hwndSecurity))
  337. {
  338. ::BringWindowToTop(m_hwndSecurity);
  339. return;
  340. }
  341. PSECURITY_DESCRIPTOR m_pOldSD = m_pNewSD; //Raid #358244, 4/5/2001
  342. SECURITY_INFORMATION m_OldSeInfo = m_NewSeInfo;
  343. if (!m_pNewSD)
  344. {
  345. GetDefaultServiceSecurity(&m_pNewSD,&m_NewSeInfo);
  346. }
  347. INT_PTR nRet = MyCreateSecurityPage2(FALSE,
  348. &m_pNewSD,
  349. &m_NewSeInfo,
  350. (LPCTSTR)(m_pData->GetAttr()),
  351. SE_SERVICE,
  352. QueryReadOnly() ? SECURITY_PAGE_RO_NP : SECURITY_PAGE_NO_PROTECT,
  353. GetSafeHwnd(),
  354. FALSE); // not modeless
  355. if ( -1 == nRet )
  356. {
  357. CString str;
  358. str.LoadString(IDS_CANT_ASSIGN_SECURITY);
  359. AfxMessageBox(str);
  360. if( m_pNewSD != m_pOldSD && m_pNewSD ) //Raid #358244, 4/5/2001
  361. {
  362. LocalFree(m_pNewSD);
  363. }
  364. m_pNewSD = m_pOldSD;
  365. m_NewSeInfo = m_OldSeInfo;
  366. }
  367. if( 0 == nRet ) //Raid #358244, 4/5/2001
  368. {
  369. if( m_pNewSD != m_pOldSD && m_pNewSD )
  370. {
  371. LocalFree(m_pNewSD);
  372. }
  373. m_pNewSD = m_pOldSD;
  374. m_NewSeInfo = m_OldSeInfo;
  375. }
  376. SetModified(TRUE);
  377. }
  378. void CConfigService::OnDisabled()
  379. {
  380. int prevValue = m_nStartupRadio; //Raid #490995, Yanggao
  381. UpdateData();
  382. if(m_nStartupRadio != prevValue)
  383. {
  384. SetModified(TRUE);
  385. }
  386. }
  387. void CConfigService::OnIgnore()
  388. {
  389. int prevValue = m_nStartupRadio; //Raid #490995, Yanggao
  390. UpdateData();
  391. if(m_nStartupRadio != prevValue)
  392. {
  393. SetModified(TRUE);
  394. }
  395. }
  396. void CConfigService::OnEnabled()
  397. {
  398. int prevValue = m_nStartupRadio; //Raid #490995, Yanggao
  399. UpdateData();
  400. if(m_nStartupRadio != prevValue)
  401. {
  402. SetModified(TRUE);
  403. }
  404. }
  405. void
  406. CConfigService::EnableUserControls( BOOL bEnable ) {
  407. CAttribute::EnableUserControls(bEnable);
  408. //
  409. // IDC_SECURITY needs to be available even in read only
  410. // mode so that the security page can be viewed.
  411. //
  412. // The page itself will be read only if necessary
  413. //
  414. if (QueryReadOnly() && bEnable)
  415. {
  416. CWnd* pWnd = GetDlgItem(IDC_SECURITY);
  417. if (pWnd)
  418. {
  419. pWnd->EnableWindow(TRUE);
  420. }
  421. else
  422. {
  423. pWnd = GetDlgItem(IDC_BASESD);
  424. if (pWnd)
  425. {
  426. pWnd->EnableWindow(TRUE);
  427. }
  428. }
  429. }
  430. }