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.

1122 lines
33 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation 1996-2001.
  5. //
  6. // File: cprivs.cpp
  7. //
  8. // Contents: implementation of CConfigPrivs
  9. //
  10. //----------------------------------------------------------------------------
  11. #include "stdafx.h"
  12. #include "wsecmgr.h"
  13. #include "CPrivs.h"
  14. #include "GetUser.h"
  15. #include "AddGrp.h"
  16. #include "snapmgr.h"
  17. #include "util.h"
  18. #ifdef _DEBUG
  19. #define new DEBUG_NEW
  20. #undef THIS_FILE
  21. static char THIS_FILE[] = __FILE__;
  22. #endif
  23. typedef struct _OBJECT_ATTRIBUTES {
  24. ULONG Length;
  25. HANDLE RootDirectory;
  26. PUNICODE_STRING ObjectName;
  27. ULONG Attributes;
  28. PVOID SecurityDescriptor; // Points to type SECURITY_DESCRIPTOR
  29. PVOID SecurityQualityOfService; // Points to type SECURITY_QUALITY_OF_SERVICE
  30. } OBJECT_ATTRIBUTES;
  31. typedef OBJECT_ATTRIBUTES *POBJECT_ATTRIBUTES;
  32. #define InitializeObjectAttributes( p, n, a, r, s ) { \
  33. (p)->Length = sizeof( OBJECT_ATTRIBUTES ); \
  34. (p)->RootDirectory = r; \
  35. (p)->Attributes = a; \
  36. (p)->ObjectName = n; \
  37. (p)->SecurityDescriptor = s; \
  38. (p)->SecurityQualityOfService = NULL; \
  39. }
  40. #define NT_SUCCESS(Status) ((NTSTATUS)(Status) >= 0)
  41. BOOL
  42. WseceditGetNameForSpecialSids(
  43. OUT PWSTR *ppszEveryone OPTIONAL,
  44. OUT PWSTR *ppszAuthUsers OPTIONAL,
  45. OUT PWSTR *ppszAdmins OPTIONAL,
  46. OUT PWSTR *ppszAdministrator OPTIONAL
  47. );
  48. PSID
  49. WseceditpGetAccountDomainSid(
  50. );
  51. /////////////////////////////////////////////////////////////////////////////
  52. // CConfigPrivs dialog
  53. /////////////////////////////////////////////////////////////////////////////
  54. CConfigPrivs::CConfigPrivs(UINT nTemplateID)
  55. : CAttribute(nTemplateID ? nTemplateID : IDD),
  56. m_fDirty(false)
  57. {
  58. //{{AFX_DATA_INIT(CConfigPrivs)
  59. //}}AFX_DATA_INIT
  60. m_pHelpIDs = (DWORD_PTR)a106HelpIDs;
  61. m_uTemplateResID = IDD;
  62. }
  63. void CConfigPrivs::DoDataExchange(CDataExchange* pDX)
  64. {
  65. CAttribute::DoDataExchange(pDX);
  66. DDX_Control(pDX, IDC_GRANTLIST, m_lbGrant);
  67. DDX_Control(pDX, IDC_REMOVE, m_btnRemove);
  68. DDX_Control(pDX, IDC_ADD, m_btnAdd);
  69. DDX_Control(pDX, IDC_TITLE, m_btnTitle);
  70. }
  71. BEGIN_MESSAGE_MAP(CConfigPrivs, CAttribute)
  72. ON_BN_CLICKED(IDC_ADD, OnAdd)
  73. ON_BN_CLICKED(IDC_REMOVE, OnRemove)
  74. ON_BN_CLICKED(IDC_CONFIGURE, OnConfigure)
  75. ON_LBN_SELCHANGE(IDC_GRANTLIST, OnSelChange)
  76. END_MESSAGE_MAP()
  77. /////////////////////////////////////////////////////////////////////////////
  78. // CConfigPrivs message handlers
  79. /////////////////////////////////////////////////////////////////////////////
  80. void CConfigPrivs::OnAdd()
  81. {
  82. CSCEAddGroup gu(this);
  83. PSCE_NAME_LIST pName = 0;
  84. if( IDD_CONFIG_PRIVS == m_uTemplateResID ) //Raid #404989
  85. {
  86. gu.m_fCheckName = FALSE;
  87. }
  88. gu.m_dwFlags = SCE_SHOW_USERS | SCE_SHOW_LOCALGROUPS | SCE_SHOW_GLOBAL | SCE_SHOW_WELLKNOWN | SCE_SHOW_BUILTIN;
  89. if( IDD_DOMAIN_PRIVS == m_uTemplateResID ) //Raid #477428, Yanggao
  90. {
  91. gu.m_dwFlags = gu.m_dwFlags | SCE_SHOW_COMPUTER;
  92. }
  93. gu.SetModeBits(m_pSnapin->GetModeBits());
  94. CString str;
  95. str.LoadString( IDS_ADD_USERGROUP );
  96. gu.m_sTitle.Format( IDS_ADD_TITLE, str );
  97. gu.m_sDescription.LoadString( IDS_ADD_USERGROUP );
  98. CThemeContextActivator activator;
  99. if (gu.DoModal() ==IDOK ) {
  100. pName = gu.GetUsers();
  101. UINT cstrMax = 0; //Raid #271219
  102. LPWSTR pstrMax = NULL;
  103. UINT cstr = 0;
  104. while(pName)
  105. {
  106. if (LB_ERR == m_lbGrant.FindStringExact(-1,pName->Name))
  107. {
  108. if( LB_ERR == m_lbGrant.AddString(pName->Name) )
  109. {
  110. return;
  111. }
  112. m_fDirty = true;
  113. cstr = wcslen(pName->Name);
  114. if( cstr > cstrMax )
  115. {
  116. cstrMax = cstr;
  117. pstrMax = pName->Name;
  118. }
  119. }
  120. pName = pName->Next;
  121. }
  122. SetModified(TRUE);
  123. CDC* pCDC = m_lbGrant.GetDC();
  124. CSize strsize = pCDC->GetOutputTextExtent(pstrMax);
  125. m_lbGrant.ReleaseDC(pCDC);
  126. RECT winsize;
  127. m_lbGrant.GetWindowRect(&winsize);
  128. if( strsize.cx > winsize.right-winsize.left )
  129. {
  130. m_lbGrant.SetHorizontalExtent(strsize.cx);
  131. }
  132. }
  133. }
  134. void CConfigPrivs::OnRemove()
  135. {
  136. int cbItems;
  137. int *pnItems;
  138. cbItems = m_lbGrant.GetSelCount();
  139. pnItems = new int [cbItems];
  140. if ( pnItems ) {
  141. m_lbGrant.GetSelItems(cbItems,pnItems);
  142. if (cbItems) {
  143. m_fDirty = true;
  144. SetModified(TRUE);
  145. }
  146. while(cbItems--) {
  147. m_lbGrant.DeleteString(pnItems[cbItems]);
  148. }
  149. delete[] pnItems;
  150. CWnd* pwnd = GetDlgItem(IDC_REMOVE); //Raid #466634, Yang Gao
  151. if( pwnd )
  152. {
  153. CWnd* pPrev = pwnd->GetWindow(GW_HWNDPREV); //Raid #482112, yanggao, 10/20/2001
  154. if(pPrev)
  155. {
  156. this->GotoDlgCtrl(pPrev);
  157. pwnd->EnableWindow(FALSE);
  158. }
  159. }
  160. }
  161. }
  162. void CConfigPrivs::OnConfigure()
  163. {
  164. CAttribute::OnConfigure();
  165. if (m_bConfigure == m_bOriginalConfigure) {
  166. m_fDirty = false;
  167. } else {
  168. m_fDirty = true;
  169. }
  170. //Raid #466634, Yang Gao
  171. CWnd* pwnd = GetDlgItem(IDC_REMOVE);
  172. if( pwnd )
  173. {
  174. if( m_lbGrant.GetCount() == 0 || m_lbGrant.GetSelCount() == 0 ) //Raid #475690, 476535, Yanggao
  175. {
  176. pwnd->EnableWindow(FALSE);
  177. }
  178. }
  179. }
  180. BOOL CConfigPrivs::OnApply()
  181. {
  182. if ( !m_bReadOnly )
  183. {
  184. PSCE_PRIVILEGE_ASSIGNMENT ppa = 0;
  185. PSCE_NAME_LIST pNames = 0;
  186. CString strItem;
  187. int cItems = 0;
  188. int i = 0;
  189. UpdateData(TRUE);
  190. if(!m_bConfigure)
  191. {
  192. PSCE_PRIVILEGE_ASSIGNMENT pDelete;
  193. pDelete = GetPrivData();
  194. //
  195. // Remove the item from the template
  196. if( pDelete && pDelete != (PSCE_PRIVILEGE_ASSIGNMENT)ULongToPtr(SCE_NO_VALUE) )
  197. {
  198. m_pData->SetID((LONG_PTR)NULL);
  199. if (m_pData->GetSetting()) //Raid #390777
  200. {
  201. m_pData->SetSetting((LONG_PTR)ULongToPtr(SCE_NO_VALUE));
  202. }
  203. m_pData->SetUnits((LPTSTR)pDelete->Name);
  204. m_pData->SetStatus(SCE_STATUS_NOT_CONFIGURED);
  205. m_pData->SetBase((LONG_PTR)ULongToPtr(SCE_NO_VALUE));
  206. m_pData->GetBaseProfile()->UpdatePrivilegeAssignedTo(
  207. TRUE, // Delete the profile.
  208. &pDelete);
  209. m_pData->GetBaseProfile()->SetDirty(AREA_PRIVILEGES);
  210. m_pData->Update(m_pSnapin);
  211. }
  212. }
  213. else if (m_fDirty)
  214. {
  215. ppa = GetPrivData();
  216. PWSTR pszPrivName = m_pData->GetUnits();
  217. if ( ppa ) {
  218. //
  219. // to handle configured privilege case where Units is NULL
  220. //
  221. pszPrivName = ppa->Name;
  222. }
  223. int cSpecialItems = m_lbGrant.GetCount();
  224. DWORD dwIds = 0;
  225. CString strDenyItem;
  226. //
  227. // simulate SCE engine behavior to special case certain privileges/rights
  228. //
  229. if ( pszPrivName )
  230. {
  231. //Prefast warning 400: Yields unexpected result in non-English locale.
  232. //Comments: It is always English locale.
  233. if ( _wcsicmp(pszPrivName, SE_INTERACTIVE_LOGON_NAME) == 0 )
  234. {
  235. if ( cSpecialItems == 0 ) {
  236. //
  237. // logon locally right cannot be assigned to no one
  238. //
  239. dwIds = IDS_PRIV_WARNING_LOCAL_LOGON;
  240. } else {
  241. PWSTR pszAdmins = NULL;
  242. //
  243. // get the administrators group name
  244. // logon locally right must be assigned to the administrator group
  245. //
  246. if ( WseceditGetNameForSpecialSids(NULL,
  247. NULL,
  248. &pszAdmins,
  249. NULL) )
  250. {
  251. for (i=0;i<cSpecialItems;i++)
  252. {
  253. m_lbGrant.GetText(i,strDenyItem);
  254. if ( (lstrcmpi((LPTSTR)(LPCTSTR)strDenyItem, pszAdmins)) == 0 )
  255. {
  256. break;
  257. }
  258. }
  259. if ( i >= cSpecialItems ) {
  260. //
  261. // cannot find administrators
  262. //
  263. dwIds = IDS_PRIV_WARNING_LOCAL_LOGON;
  264. }
  265. LocalFree(pszAdmins);
  266. }
  267. else
  268. {
  269. dwIds = IDS_PRIV_WARNING_ACCOUNT_TRANSLATION;
  270. }
  271. }
  272. }
  273. //Prefast warning 400: Yields unexpected result in non-English locale.
  274. //Comments: It is always English locale.
  275. else if (_wcsicmp(pszPrivName, SE_DENY_INTERACTIVE_LOGON_NAME) == 0 )
  276. {
  277. PWSTR pszEveryone = NULL;
  278. PWSTR pszAuthUsers = NULL;
  279. PWSTR pszAdmins = NULL;
  280. PWSTR pszAdministrator=NULL;
  281. //
  282. // deny logon locally right cannot be assigned to any of the following
  283. // everyone, authenticated users, administrators, administrator
  284. //
  285. if ( WseceditGetNameForSpecialSids(&pszEveryone,
  286. &pszAuthUsers,
  287. &pszAdmins,
  288. &pszAdministrator) )
  289. {
  290. //
  291. // make sure this check covers the free text administrator account as well
  292. //
  293. PWSTR pTemp = wcschr(pszAdministrator, L'\\');
  294. if ( pTemp ) {
  295. pTemp++;
  296. }
  297. for (i=0;i<cSpecialItems;i++)
  298. {
  299. m_lbGrant.GetText(i,strDenyItem);
  300. if ( lstrcmpi((LPTSTR)(LPCTSTR)strDenyItem, pszEveryone) == 0 ||
  301. lstrcmpi((LPTSTR)(LPCTSTR)strDenyItem, pszAuthUsers) == 0 ||
  302. lstrcmpi((LPTSTR)(LPCTSTR)strDenyItem, pszAdmins) == 0 ||
  303. lstrcmpi((LPTSTR)(LPCTSTR)strDenyItem, pszAdministrator) == 0 ||
  304. (pTemp && lstrcmpi((LPTSTR)(LPCTSTR)strDenyItem, pTemp) == 0 ) )
  305. {
  306. dwIds = IDS_PRIV_WARNING_DENYLOCAL_LOGON;
  307. break;
  308. }
  309. }
  310. LocalFree(pszEveryone);
  311. LocalFree(pszAuthUsers);
  312. LocalFree(pszAdmins);
  313. LocalFree(pszAdministrator);
  314. }
  315. else
  316. {
  317. dwIds = IDS_PRIV_WARNING_ACCOUNT_TRANSLATION;
  318. }
  319. }
  320. if (dwIds == IDS_PRIV_WARNING_LOCAL_LOGON ||
  321. dwIds == IDS_PRIV_WARNING_DENYLOCAL_LOGON ||
  322. dwIds == IDS_PRIV_WARNING_ACCOUNT_TRANSLATION )
  323. {
  324. //
  325. // if any of the items fail the check, display the warning
  326. // or popup a warning message box
  327. //
  328. CString strWarning;
  329. strWarning.LoadString(dwIds);
  330. CWnd *pWarn = GetDlgItem(IDC_WARNING);
  331. if (pWarn)
  332. {
  333. pWarn->SetWindowText(strWarning);
  334. pWarn->ShowWindow(SW_SHOW);
  335. pWarn = GetDlgItem(IDC_WARNING_ICON);
  336. if (pWarn)
  337. pWarn->ShowWindow(SW_SHOW);
  338. pWarn = GetDlgItem(IDC_ADD); //Raid 498449, yanggao
  339. if( pWarn )
  340. {
  341. this->GotoDlgCtrl(pWarn);
  342. }
  343. }
  344. else
  345. {
  346. //
  347. // Dialog box not available in some modes such as Local Policy
  348. //
  349. AfxMessageBox(strWarning);
  350. }
  351. return FALSE;
  352. }
  353. CWnd *pWarn = GetDlgItem(IDC_WARNING); //Raid #621124, Yanggao
  354. if (pWarn)
  355. {
  356. pWarn->ShowWindow(SW_HIDE);
  357. }
  358. pWarn = GetDlgItem(IDC_WARNING_ICON);
  359. if (pWarn)
  360. {
  361. pWarn->ShowWindow(SW_HIDE);
  362. }
  363. }
  364. if ( ppa == NULL && m_pData->GetUnits() )
  365. {
  366. if ( m_pData->GetBaseProfile()->UpdatePrivilegeAssignedTo(
  367. FALSE,
  368. &ppa,
  369. m_pData->GetUnits()
  370. ) == ERROR_SUCCESS)
  371. {
  372. m_pData->GetBaseProfile()->SetDirty(AREA_PRIVILEGES);
  373. SetPrivData(ppa);
  374. }
  375. }
  376. if ( ppa )
  377. {
  378. PSCE_NAME_LIST pNewList=NULL;
  379. cItems = m_lbGrant.GetCount();
  380. HRESULT hr=S_OK;
  381. if (cItems != LB_ERR && m_bConfigure)
  382. {
  383. for (i=0;i<cItems;i++)
  384. {
  385. m_lbGrant.GetText(i,strItem);
  386. if ( SceAddToNameList(&pNewList, (LPTSTR)(LPCTSTR)strItem, strItem.GetLength()) != SCESTATUS_SUCCESS)
  387. {
  388. hr = E_FAIL;
  389. break;
  390. }
  391. }
  392. }
  393. else
  394. hr = E_FAIL;
  395. if ( SUCCEEDED(hr) )
  396. {
  397. SceFreeMemory(ppa->AssignedTo,SCE_STRUCT_NAME_LIST);
  398. ppa->AssignedTo = pNewList;
  399. SetPrivData(ppa);
  400. m_pData->Update(m_pSnapin);
  401. m_fDirty = false;
  402. }
  403. else
  404. {
  405. //
  406. // free the new list, failed due to memory problem
  407. //
  408. if ( pNewList ) {
  409. SceFreeMemory(pNewList,SCE_STRUCT_NAME_LIST);
  410. }
  411. }
  412. }
  413. }
  414. }
  415. return CAttribute::OnApply();
  416. }
  417. void CConfigPrivs::OnCancel()
  418. {
  419. m_bConfigure = m_bOriginalConfigure;
  420. CAttribute::OnCancel();
  421. }
  422. PSCE_PRIVILEGE_ASSIGNMENT
  423. CConfigPrivs::GetPrivData() {
  424. ASSERT(m_pData);
  425. if (m_pData) {
  426. return (PSCE_PRIVILEGE_ASSIGNMENT) m_pData->GetID();
  427. }
  428. return NULL;
  429. }
  430. void
  431. CConfigPrivs::SetPrivData(PSCE_PRIVILEGE_ASSIGNMENT ppa) {
  432. ASSERT(m_pData);
  433. if (m_pData) {
  434. m_pData->SetID((LONG_PTR)ppa);
  435. if (ppa) {
  436. m_pData->SetBase((LONG_PTR)ppa->AssignedTo);
  437. } else {
  438. m_pData->SetBase(NULL);
  439. }
  440. }
  441. }
  442. BOOL CConfigPrivs::OnInitDialog()
  443. {
  444. CAttribute::OnInitDialog();
  445. PSCE_PRIVILEGE_ASSIGNMENT ppa;
  446. PSCE_NAME_LIST pNames;
  447. UpdateData(FALSE);
  448. ::SetMapMode(::GetDC(m_lbGrant.m_hWnd), MM_TEXT);
  449. ppa = GetPrivData();
  450. if ( ppa ) {
  451. pNames = ppa->AssignedTo;
  452. UINT cstrMax = 0; //Raid #271219
  453. LPWSTR pstrMax = NULL;
  454. UINT cstr = 0;
  455. while(pNames)
  456. {
  457. m_lbGrant.AddString(pNames->Name);
  458. cstr = wcslen(pNames->Name);
  459. if( cstr > cstrMax )
  460. {
  461. cstrMax = cstr;
  462. pstrMax = pNames->Name;
  463. }
  464. pNames = pNames->Next;
  465. }
  466. CDC* pCDC = m_lbGrant.GetDC();
  467. CSize strsize = pCDC->GetOutputTextExtent(pstrMax);
  468. m_lbGrant.ReleaseDC(pCDC);
  469. RECT winsize;
  470. m_lbGrant.GetWindowRect(&winsize);
  471. if( strsize.cx > winsize.right-winsize.left )
  472. {
  473. m_lbGrant.SetHorizontalExtent(strsize.cx);
  474. }
  475. m_bConfigure = TRUE;
  476. } else if(m_pData->GetBase() == (LONG_PTR)ULongToPtr(SCE_NO_VALUE)){
  477. m_bConfigure = FALSE;
  478. }
  479. if (m_pData->GetSetting())
  480. {
  481. CWnd *pWarn = GetDlgItem(IDC_WARNING);
  482. if (pWarn)
  483. {
  484. CString strWarning;
  485. strWarning.LoadString(IDS_PRIV_WARNING);
  486. pWarn->SetWindowText(strWarning);
  487. pWarn->ShowWindow(SW_SHOW);
  488. pWarn = GetDlgItem(IDC_WARNING_ICON);
  489. if (pWarn)
  490. {
  491. pWarn->ShowWindow(SW_SHOW);
  492. }
  493. }
  494. }
  495. m_bOriginalConfigure = m_bConfigure;
  496. //
  497. // Update the user controls depending on the setting.
  498. //
  499. AddUserControl(IDC_GRANTLIST);
  500. AddUserControl(IDC_ADD);
  501. AddUserControl(IDC_REMOVE);
  502. m_btnTitle.SetWindowText(m_pData->GetAttrPretty());
  503. UpdateData(FALSE);
  504. EnableUserControls(m_bConfigure);
  505. GetDlgItem(IDC_REMOVE)->EnableWindow(FALSE); //Raid #471511, Yanggao.
  506. // The listbox should always be enabled so that its contents may be viewed
  507. // Raid# 697665 "Local Computer Policy on a DC:Allow logon locally
  508. // properties page list box scroll bar is disabled preventing viewing of all users"
  509. // bryanwal
  510. m_lbGrant.EnableWindow (TRUE);
  511. return TRUE;
  512. // return TRUE unless you set the focus to a control
  513. // EXCEPTION: OCX Property Pages should return FALSE
  514. }
  515. void CConfigPrivs::SetInitialValue(DWORD_PTR dw)
  516. {
  517. }
  518. BOOL
  519. WseceditGetNameForSpecialSids(
  520. OUT PWSTR *ppszEveryone OPTIONAL,
  521. OUT PWSTR *ppszAuthUsers OPTIONAL,
  522. OUT PWSTR *ppszAdmins OPTIONAL,
  523. OUT PWSTR *ppszAdministrator OPTIONAL
  524. )
  525. /*++
  526. Routine Description:
  527. This routine returns the localized account name for the Everyone and the Auth User SIDs
  528. Arguments:
  529. ppszEveryone - ptr to fill in (should be freed outside)
  530. ppszAuthUsers - ptr to fill in (should be freed outside)
  531. ppszAdmins - ptr to fill in for Administrators
  532. ppszAdministrator - ptr to fill in for local administrator account
  533. Return value:
  534. TRUE if succeeded else FALSE
  535. -- */
  536. {
  537. //
  538. // buffers for the SIDs
  539. //
  540. SID Sid;
  541. DWORD dwSize = sizeof(SID);
  542. PSID pSid=NULL;
  543. BOOL bError = TRUE;
  544. //
  545. // variables for sid lookup
  546. //
  547. SID_NAME_USE tmp;
  548. DWORD dwSizeDom;
  549. PWSTR dummyBuf = NULL;
  550. if ( ppszEveryone ) {
  551. //
  552. // create the SID for "everyone"
  553. //
  554. if ( CreateWellKnownSid(
  555. WinWorldSid,
  556. NULL,
  557. &Sid,
  558. &dwSize)) {
  559. //
  560. // get the required size of the account name and domain buffer
  561. //
  562. dwSize = 0;
  563. dwSizeDom = 0;
  564. LookupAccountSid(
  565. NULL,
  566. &Sid,
  567. NULL,
  568. &dwSize,
  569. NULL,
  570. &dwSizeDom,
  571. &tmp
  572. );
  573. *ppszEveryone = (PWSTR)LocalAlloc(LMEM_ZEROINIT, ((dwSize + 1) * sizeof(WCHAR)));
  574. dummyBuf = (PWSTR)LocalAlloc(LMEM_ZEROINIT, ((dwSizeDom + 1) * sizeof(WCHAR)));
  575. if ( *ppszEveryone && dummyBuf ) {
  576. //
  577. // lookup the SID to get the account name - domain name is ignored
  578. //
  579. if ( LookupAccountSid(
  580. NULL,
  581. &Sid,
  582. *ppszEveryone,
  583. &dwSize,
  584. dummyBuf,
  585. &dwSizeDom,
  586. &tmp
  587. ) ) {
  588. bError = FALSE;
  589. }
  590. }
  591. }
  592. LocalFree(dummyBuf);
  593. dummyBuf = NULL;
  594. if (bError) {
  595. LocalFree(*ppszEveryone);
  596. *ppszEveryone = NULL;
  597. return FALSE;
  598. }
  599. }
  600. //
  601. // "Authenticated Users"
  602. //
  603. if ( ppszAuthUsers ) {
  604. dwSize = sizeof(SID);
  605. bError = TRUE;
  606. //
  607. // create the SID for "authenticated users"
  608. //
  609. if ( CreateWellKnownSid(
  610. WinAuthenticatedUserSid,
  611. NULL,
  612. &Sid,
  613. &dwSize)) {
  614. //
  615. // get the required size of account name and domain buffers
  616. //
  617. dwSize = 0;
  618. dwSizeDom = 0;
  619. LookupAccountSid(
  620. NULL,
  621. &Sid,
  622. NULL,
  623. &dwSize,
  624. NULL,
  625. &dwSizeDom,
  626. &tmp
  627. );
  628. *ppszAuthUsers = (PWSTR)LocalAlloc(LMEM_ZEROINIT, ((dwSize + 1) * sizeof(WCHAR)));
  629. dummyBuf = (PWSTR)LocalAlloc(LMEM_ZEROINIT, ((dwSizeDom + 1) * sizeof(WCHAR)));
  630. if ( *ppszAuthUsers && dummyBuf ) {
  631. //
  632. // lookup the SID to get account name - domain name is ignored
  633. //
  634. if ( LookupAccountSid(
  635. NULL,
  636. &Sid,
  637. *ppszAuthUsers,
  638. &dwSize,
  639. dummyBuf,
  640. &dwSizeDom,
  641. &tmp
  642. ) ) {
  643. bError = FALSE;
  644. }
  645. }
  646. }
  647. LocalFree(dummyBuf);
  648. dummyBuf = NULL;
  649. if (bError) {
  650. LocalFree(*ppszAuthUsers);
  651. *ppszAuthUsers = NULL;
  652. if ( ppszEveryone ) {
  653. LocalFree(*ppszEveryone);
  654. *ppszEveryone = NULL;
  655. }
  656. return FALSE;
  657. }
  658. }
  659. //
  660. // administrators group
  661. //
  662. if ( ppszAdmins ) {
  663. dwSize = 0;
  664. bError = TRUE;
  665. //
  666. // get the size for the well known SID of administrators group
  667. //
  668. CreateWellKnownSid(
  669. WinBuiltinAdministratorsSid,
  670. NULL,
  671. pSid,
  672. &dwSize);
  673. if ( dwSize > 0 ) {
  674. //
  675. // alocate buffer and create the well known SID
  676. // cannot use the SID buffer because Admins SID has more than
  677. // one subauthority
  678. //
  679. pSid = (PSID)LocalAlloc(LPTR, dwSize);
  680. if ( pSid &&
  681. CreateWellKnownSid(
  682. WinBuiltinAdministratorsSid,
  683. NULL,
  684. pSid,
  685. &dwSize) ) {
  686. dwSize = 0;
  687. dwSizeDom = 0;
  688. //
  689. // get the size for account name and domain buffers
  690. //
  691. LookupAccountSid(
  692. NULL,
  693. pSid,
  694. NULL,
  695. &dwSize,
  696. NULL,
  697. &dwSizeDom,
  698. &tmp
  699. );
  700. *ppszAdmins = (PWSTR)LocalAlloc(LMEM_ZEROINIT, ((dwSize + 1) * sizeof(WCHAR)));
  701. dummyBuf = (PWSTR)LocalAlloc(LMEM_ZEROINIT, ((dwSizeDom + 1) * sizeof(WCHAR)));
  702. if ( *ppszAdmins && dummyBuf ) {
  703. //
  704. // look up the name, domain name (BUILTIN) is ignored
  705. //
  706. if ( LookupAccountSid(
  707. NULL,
  708. pSid,
  709. *ppszAdmins,
  710. &dwSize,
  711. dummyBuf,
  712. &dwSizeDom,
  713. &tmp
  714. ) ) {
  715. bError = FALSE;
  716. }
  717. }
  718. }
  719. LocalFree(pSid);
  720. pSid = NULL;
  721. }
  722. LocalFree(dummyBuf);
  723. dummyBuf = NULL;
  724. if (bError) {
  725. //
  726. // anything fail will free all buffers and return FALSE
  727. //
  728. LocalFree(*ppszAdmins);
  729. *ppszAdmins = NULL;
  730. if ( ppszAuthUsers ) {
  731. LocalFree(*ppszAuthUsers);
  732. *ppszAuthUsers = NULL;
  733. }
  734. if ( ppszEveryone ) {
  735. LocalFree(*ppszEveryone);
  736. *ppszEveryone = NULL;
  737. }
  738. return FALSE;
  739. }
  740. }
  741. //
  742. // the administrator user account
  743. //
  744. if ( ppszAdministrator ) {
  745. dwSize = 0;
  746. bError = TRUE;
  747. PWSTR dummy2=NULL;
  748. //
  749. // Get Account domain SID first
  750. //
  751. PSID pDomSid = WseceditpGetAccountDomainSid();
  752. if ( pDomSid ) {
  753. //
  754. // get the size for the administrator account (local account domain is used)
  755. //
  756. CreateWellKnownSid(
  757. WinAccountAdministratorSid,
  758. pDomSid,
  759. pSid,
  760. &dwSize);
  761. if ( dwSize > 0 ) {
  762. //
  763. // cannot use the SID buffer because administrator account SID
  764. // has more than one subauthority
  765. //
  766. pSid = (PSID)LocalAlloc(LPTR, dwSize);
  767. if ( pSid &&
  768. CreateWellKnownSid(
  769. WinAccountAdministratorSid,
  770. pDomSid,
  771. pSid,
  772. &dwSize) ) {
  773. //
  774. // get size for the account name and domain buffer
  775. //
  776. dwSize = 0;
  777. dwSizeDom = 0;
  778. LookupAccountSid(
  779. NULL,
  780. pSid,
  781. NULL,
  782. &dwSize,
  783. NULL,
  784. &dwSizeDom,
  785. &tmp
  786. );
  787. dummy2 = (PWSTR)LocalAlloc(LMEM_ZEROINIT, ((dwSize + 1) * sizeof(WCHAR)));
  788. dummyBuf = (PWSTR)LocalAlloc(LMEM_ZEROINIT, ((dwSizeDom + 1) * sizeof(WCHAR)));
  789. if ( dummy2 && dummyBuf ) {
  790. //
  791. // lookup the account name and domain name
  792. //
  793. if ( LookupAccountSid(
  794. NULL,
  795. pSid,
  796. dummy2,
  797. &dwSize,
  798. dummyBuf,
  799. &dwSizeDom,
  800. &tmp
  801. ) ) {
  802. *ppszAdministrator = (PWSTR)LocalAlloc(LPTR, (dwSize+dwSizeDom+2)*sizeof(WCHAR));
  803. if ( *ppszAdministrator ) {
  804. //
  805. // the name to return is a fully qualified name such as Domain\Administrator
  806. //
  807. // This is a safe usage.
  808. wcscpy(*ppszAdministrator, dummyBuf);
  809. wcscat(*ppszAdministrator, L"\\");
  810. wcscat(*ppszAdministrator, dummy2);
  811. bError = FALSE;
  812. }
  813. }
  814. }
  815. }
  816. LocalFree(pSid);
  817. pSid = NULL;
  818. }
  819. LocalFree(dummyBuf);
  820. dummyBuf = NULL;
  821. LocalFree(dummy2);
  822. dummy2 = NULL;
  823. LocalFree(pDomSid);
  824. }
  825. if (bError) {
  826. //
  827. // anything fail will free all buffers and return FALSE
  828. //
  829. LocalFree(*ppszAdministrator);
  830. *ppszAdministrator = NULL;
  831. if ( ppszAdmins ) {
  832. LocalFree(*ppszAdmins);
  833. *ppszAdmins = NULL;
  834. }
  835. if ( ppszAuthUsers ) {
  836. LocalFree(*ppszAuthUsers);
  837. *ppszAuthUsers = NULL;
  838. }
  839. if ( ppszEveryone ) {
  840. LocalFree(*ppszEveryone);
  841. *ppszEveryone = NULL;
  842. }
  843. return FALSE;
  844. }
  845. }
  846. return TRUE;
  847. }
  848. PSID
  849. WseceditpGetAccountDomainSid(
  850. )
  851. {
  852. NTSTATUS Status;
  853. LSA_HANDLE PolicyHandle;
  854. OBJECT_ATTRIBUTES PolicyObjectAttributes;
  855. PPOLICY_ACCOUNT_DOMAIN_INFO PolicyAccountDomainInfo=NULL;
  856. PSID DomainSid=NULL;
  857. //
  858. // Open the policy database
  859. //
  860. InitializeObjectAttributes( &PolicyObjectAttributes,
  861. NULL, // Name
  862. 0, // Attributes
  863. NULL, // Root
  864. NULL ); // Security Descriptor
  865. Status = LsaOpenPolicy( NULL,
  866. (PLSA_OBJECT_ATTRIBUTES)&PolicyObjectAttributes,
  867. POLICY_VIEW_LOCAL_INFORMATION,
  868. &PolicyHandle );
  869. if ( NT_SUCCESS(Status) ) {
  870. //
  871. // Query the account domain information
  872. //
  873. Status = LsaQueryInformationPolicy( PolicyHandle,
  874. PolicyAccountDomainInformation,
  875. (PVOID *)&PolicyAccountDomainInfo );
  876. if ( NT_SUCCESS(Status) ) {
  877. DWORD Len = GetLengthSid(PolicyAccountDomainInfo->DomainSid);
  878. DomainSid = (PSID)LocalAlloc(LPTR, Len );
  879. if ( DomainSid ) {
  880. //This is not a safe usage. Status should be STATUS_SUCCESS for LsaQueryInformationPolicy and LsaOpenPolicy.
  881. //Status should be nonzero for CopySid. Raid #552428.
  882. Status = CopySid( Len, DomainSid, PolicyAccountDomainInfo->DomainSid );
  883. if ( !NT_SUCCESS(Status) ) {
  884. LocalFree(DomainSid);
  885. DomainSid = NULL;
  886. }
  887. }
  888. LsaFreeMemory(PolicyAccountDomainInfo);
  889. }
  890. LsaClose( PolicyHandle );
  891. }
  892. return(DomainSid);
  893. }
  894. void CConfigPrivs::OnSelChange() //Raid #466634, Yang Gao
  895. {
  896. CWnd* pwnd = this->GetDlgItem(IDC_REMOVE);
  897. if( pwnd )
  898. {
  899. if( m_lbGrant.GetSelCount() == 0 //Raid #476305, yanggao, for multi select listbox.
  900. || QueryReadOnly () ) // Raid# 697665 bryanwal. Listbox must enable to
  901. // view all items, but clicking it shouldn't enable
  902. // "Remove"
  903. {
  904. if( pwnd->IsWindowEnabled() )
  905. pwnd->EnableWindow(FALSE);
  906. }
  907. else
  908. {
  909. if( !pwnd->IsWindowEnabled() )
  910. pwnd->EnableWindow(TRUE);
  911. }
  912. }
  913. }