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.

1385 lines
35 KiB

  1. /****************************************************************************
  2. PROGRAM: SECEDIT.C
  3. PURPOSE: Displays the usrs current token and eventually allows the user
  4. to edit parts of it.
  5. ****************************************************************************/
  6. #include "SECEDIT.h"
  7. #include "string.h"
  8. static char pszMainWindowClass[] = "Main Window Class";
  9. HANDLE hInst;
  10. // Global used to store handle to MYTOKEN
  11. HANDLE hMyToken;
  12. BOOL InitApplication(HANDLE);
  13. BOOL InitInstance(HANDLE, INT);
  14. LRESULT APIENTRY MainWndProc(HWND, UINT, WPARAM, LPARAM);
  15. BOOL EditWindowContext(HWND, HWND);
  16. INT_PTR APIENTRY MainDlgProc(HWND, UINT, WPARAM, LPARAM);
  17. INT_PTR APIENTRY MoreDlgProc(HWND, UINT, WPARAM, LPARAM);
  18. INT_PTR APIENTRY ListDlgProc(HWND, UINT, WPARAM, LPARAM);
  19. INT_PTR APIENTRY ActiveWindowDlgProc(HWND, UINT, WPARAM, LPARAM);
  20. INT_PTR APIENTRY AboutDlgProc(HWND, UINT, WPARAM, LPARAM);
  21. BOOL MainDlgInit(HWND, LPARAM);
  22. BOOL MainDlgEnd(HWND, BOOL);
  23. BOOL EnablePrivilege(HWND, BOOL);
  24. BOOL EnableGroup(HWND, BOOL);
  25. BOOL SetDefaultOwner(HWND);
  26. BOOL SetPrimaryGroup(HWND);
  27. BOOL MoreDlgInit(HWND hDlg);
  28. BOOL DisplayMyToken(HWND, HANDLE);
  29. BOOL ListDlgInit(HWND);
  30. BOOL APIENTRY WindowEnum(HWND, LPARAM);
  31. HWND ListDlgEnd(HWND);
  32. /****************************************************************************
  33. FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
  34. PURPOSE: calls initialization function, processes message loop
  35. ****************************************************************************/
  36. INT
  37. __stdcall
  38. WinMain(
  39. HINSTANCE hInstance,
  40. HINSTANCE hPrevInstance,
  41. LPSTR lpCmdLine,
  42. INT nCmdShow
  43. )
  44. {
  45. MSG Message;
  46. if (!hPrevInstance) {
  47. if (!InitApplication(hInstance)) {
  48. DbgPrint("SECEDIT - InitApplication failed\n");
  49. return (FALSE);
  50. }
  51. }
  52. if (!InitInstance(hInstance, nCmdShow)) {
  53. DbgPrint("SECEDIT - InitInstance failed\n");
  54. return (FALSE);
  55. }
  56. while (GetMessage(&Message, NULL, 0, 0)) {
  57. TranslateMessage(&Message);
  58. DispatchMessage(&Message);
  59. }
  60. return ((int)Message.wParam);
  61. }
  62. /****************************************************************************
  63. FUNCTION: InitApplication(HANDLE)
  64. PURPOSE: Initializes window data and registers window class
  65. ****************************************************************************/
  66. BOOL
  67. InitApplication(
  68. HANDLE hInstance
  69. )
  70. {
  71. WNDCLASS wc;
  72. NTSTATUS Status;
  73. // Register the main window class
  74. wc.style = 0;
  75. wc.lpfnWndProc = MainWndProc;
  76. wc.cbClsExtra = 0;
  77. wc.cbWndExtra = 0;
  78. wc.hInstance = hInstance;
  79. wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPICON));
  80. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  81. wc.hbrBackground = GetStockObject(WHITE_BRUSH);
  82. wc.lpszMenuName = (LPSTR)IDM_MAINMENU;
  83. wc.lpszClassName = pszMainWindowClass;
  84. return (RegisterClass(&wc));
  85. }
  86. /****************************************************************************
  87. FUNCTION: InitInstance(HANDLE, int)
  88. PURPOSE: Saves instance handle and creates main window
  89. ****************************************************************************/
  90. BOOL
  91. InitInstance(
  92. HANDLE hInstance,
  93. INT nCmdShow
  94. )
  95. {
  96. HWND hwnd;
  97. // Store instance in global
  98. hInst = hInstance;
  99. // Create the main window
  100. hwnd = CreateWindow(
  101. pszMainWindowClass,
  102. "Security Context Editor",
  103. WS_OVERLAPPEDWINDOW,
  104. CW_USEDEFAULT,
  105. CW_USEDEFAULT,
  106. CW_USEDEFAULT,
  107. CW_USEDEFAULT,
  108. NULL,
  109. NULL,
  110. hInstance,
  111. NULL);
  112. if (hwnd == NULL) {
  113. return(FALSE);
  114. }
  115. ShowWindow(hwnd, nCmdShow);
  116. UpdateWindow(hwnd);
  117. return (TRUE);
  118. }
  119. /****************************************************************************
  120. FUNCTION: MainWndProc(HWND, UINT, WPARAM, LONG)
  121. PURPOSE: Processes messages for main window
  122. COMMENTS:
  123. ****************************************************************************/
  124. LRESULT
  125. APIENTRY
  126. MainWndProc(
  127. HWND hwnd,
  128. UINT message,
  129. WPARAM wParam,
  130. LPARAM lParam
  131. )
  132. {
  133. HWND hwndEdit;
  134. WNDPROC lpProc;
  135. switch (message) {
  136. case WM_CREATE:
  137. SetHooks(hwnd);
  138. return(0); // Continue creating window
  139. break;
  140. case WM_COMMAND:
  141. switch (LOWORD(wParam)) {
  142. case IDM_PROGRAMMANAGER:
  143. hwndEdit = FindWindow(NULL, "Program Manager");
  144. if (hwndEdit == NULL) {
  145. DbgPrint("SECEDIT : Failed to find program manager window\n");
  146. break;
  147. }
  148. EditWindowContext(hwnd, hwndEdit);
  149. break;
  150. case IDM_WINDOWLIST:
  151. lpProc = (WNDPROC)MakeProcInstance(ListDlgProc, hInst);
  152. hwndEdit = (HWND)DialogBox(hInst,(LPSTR)IDD_WINDOWLIST, hwnd, lpProc);
  153. FreeProcInstance(lpProc);
  154. EditWindowContext(hwnd, hwndEdit);
  155. break;
  156. case IDM_ACTIVEWINDOW:
  157. lpProc = (WNDPROC)MakeProcInstance(ActiveWindowDlgProc, hInst);
  158. hwndEdit = (HWND)DialogBox(hInst,(LPSTR)IDD_ACTIVEWINDOW, hwnd, lpProc);
  159. FreeProcInstance(lpProc);
  160. break;
  161. case IDM_ABOUT:
  162. lpProc = (WNDPROC)MakeProcInstance(AboutDlgProc, hInst);
  163. DialogBox(hInst,(LPSTR)IDD_ABOUT, hwnd, lpProc);
  164. FreeProcInstance(lpProc);
  165. break;
  166. default:
  167. break;
  168. }
  169. break;
  170. case WM_SECEDITNOTIFY:
  171. // Our hook proc posted us a message
  172. SetForegroundWindow(hwnd);
  173. EditWindowContext(hwnd, (HWND)wParam);
  174. break;
  175. case WM_DESTROY:
  176. ReleaseHooks(hwnd);
  177. PostQuitMessage(0);
  178. break;
  179. default:
  180. return(DefWindowProc(hwnd, message, wParam, lParam));
  181. }
  182. return 0;
  183. }
  184. /****************************************************************************
  185. FUNCTION: EditWindowContext
  186. PURPOSE: Displays and allows the user to edit the security context
  187. of the specified window.
  188. Currently this means editting the security context of the
  189. process that owns this window
  190. RETURNS: TRUE on success, FALSE on failure
  191. ****************************************************************************/
  192. BOOL
  193. EditWindowContext(
  194. HWND hwndParent,
  195. HWND hwndEdit
  196. )
  197. {
  198. WNDPROC lpProc;
  199. if (hwndEdit == NULL) {
  200. DbgPrint("SECEDIT : hwndEdit = NULL\n");
  201. return(FALSE);
  202. }
  203. lpProc = (WNDPROC)MakeProcInstance(MainDlgProc, hInst);
  204. DialogBoxParam(hInst,(LPSTR)IDD_MAIN, hwndParent, lpProc, (LONG_PTR)hwndEdit);
  205. FreeProcInstance(lpProc);
  206. return(TRUE);
  207. }
  208. /****************************************************************************
  209. FUNCTION: MainDlgProc(HWND, unsigned, WORD, LONG)
  210. PURPOSE: Processes messages
  211. MESSAGES:
  212. WM_COMMAND - application menu (About dialog box)
  213. WM_DESTROY - destroy window
  214. COMMENTS:
  215. ****************************************************************************/
  216. INT_PTR
  217. APIENTRY
  218. MainDlgProc(
  219. HWND hDlg,
  220. UINT message,
  221. WPARAM wParam,
  222. LPARAM lParam
  223. )
  224. {
  225. WNDPROC lpProc;
  226. switch (message) {
  227. case WM_INITDIALOG:
  228. if (!MainDlgInit(hDlg, lParam)) {
  229. // Failed to initialize dialog, get out
  230. EndDialog(hDlg, FALSE);
  231. }
  232. return (TRUE);
  233. case WM_COMMAND:
  234. switch (LOWORD(wParam)) {
  235. case IDOK:
  236. // we're done, drop through to quit dialog....
  237. case IDCANCEL:
  238. MainDlgEnd(hDlg, LOWORD(wParam) == IDOK);
  239. EndDialog(hDlg, TRUE);
  240. return TRUE;
  241. break;
  242. case IDB_DISABLEPRIVILEGE:
  243. case IDB_ENABLEPRIVILEGE:
  244. EnablePrivilege(hDlg, LOWORD(wParam) == IDB_ENABLEPRIVILEGE);
  245. return(TRUE);
  246. break;
  247. case IDB_DISABLEGROUP:
  248. case IDB_ENABLEGROUP:
  249. EnableGroup(hDlg, LOWORD(wParam) == IDB_ENABLEGROUP);
  250. return(TRUE);
  251. break;
  252. case IDC_DEFAULTOWNER:
  253. SetDefaultOwner(hDlg);
  254. return(TRUE);
  255. case IDC_PRIMARYGROUP:
  256. SetPrimaryGroup(hDlg);
  257. return(TRUE);
  258. case IDB_MORE:
  259. lpProc = (WNDPROC)MakeProcInstance(MoreDlgProc, hInst);
  260. DialogBox(hInst,(LPSTR)IDD_MORE, hDlg, lpProc);
  261. FreeProcInstance(lpProc);
  262. return(TRUE);
  263. default:
  264. // We didn't process this message
  265. return FALSE;
  266. break;
  267. }
  268. break;
  269. default:
  270. // We didn't process this message
  271. return FALSE;
  272. }
  273. // We processed the message
  274. return TRUE;
  275. #ifdef NTBUILD
  276. DBG_UNREFERENCED_PARAMETER(lParam);
  277. #endif
  278. }
  279. /****************************************************************************
  280. FUNCTION: MainDlgInit(HWND)
  281. PURPOSE: Initialises the controls in the main dialog window.
  282. RETURNS: TRUE on success, FALSE if dialog should be terminated.
  283. ****************************************************************************/
  284. BOOL
  285. MainDlgInit(
  286. HWND hDlg,
  287. LPARAM lParam
  288. )
  289. {
  290. HWND hwnd = (HWND)lParam;
  291. CHAR string[MAX_STRING_BYTES];
  292. INT length;
  293. // Since we use a global to store the pointer to our MYTOKEN
  294. // structure, check we don't get called again before we have
  295. // quit out of the last dialog.
  296. if (hMyToken != NULL) {
  297. DbgPrint("SECEDIT: Already editting a context\n");
  298. return(FALSE);
  299. }
  300. if (hwnd == NULL) {
  301. DbgPrint("SECEDIT: Window handle is NULL\n");
  302. return(FALSE);
  303. }
  304. if (!LsaInit()) {
  305. DbgPrint("SECEDIT - LsaInit failed\n");
  306. return(FALSE);
  307. }
  308. hMyToken = OpenMyToken(hwnd);
  309. if (hMyToken == NULL) {
  310. DbgPrint("SECEDIT: Failed to open mytoken\n");
  311. strcpy(string, "Unable to access security context for\n<");
  312. length = strlen(string);
  313. GetWindowText(hwnd, &(string[length]), MAX_STRING_BYTES - length);
  314. strcat(string, ">");
  315. MessageBox(hDlg, string, NULL, MB_ICONSTOP | MB_APPLMODAL | MB_OK);
  316. LsaTerminate();
  317. return(FALSE);
  318. }
  319. DisplayMyToken(hDlg, hMyToken);
  320. // Set the dialog caption appropriately
  321. GetWindowText(hDlg, string, MAX_STRING_BYTES);
  322. strcat(string, " for <");
  323. length = strlen(string);
  324. GetWindowText(hwnd, &string[length], MAX_STRING_BYTES - length);
  325. strcat(string, ">");
  326. SetWindowText(hDlg, string);
  327. return(TRUE);
  328. }
  329. /****************************************************************************
  330. FUNCTION: MainDlgEnd(HWND)
  331. PURPOSE: Do whatever we have to do to clean up when dialog ends
  332. RETURNS: TRUE on success, FALSE on failure.
  333. ****************************************************************************/
  334. BOOL
  335. MainDlgEnd(
  336. HWND hDlg,
  337. BOOL fSaveChanges
  338. )
  339. {
  340. BOOL Success;
  341. LsaTerminate();
  342. Success = CloseMyToken(hDlg, hMyToken, fSaveChanges);
  343. hMyToken = NULL;
  344. return(Success);
  345. }
  346. /****************************************************************************
  347. FUNCTION: DisplayMyToken
  348. PURPOSE: Reads data out of mytoken and puts in dialog controls.
  349. RETURNS: TRUE on success, FALSE on failure
  350. ****************************************************************************/
  351. BOOL
  352. DisplayMyToken(
  353. HWND hDlg,
  354. HANDLE hMyToken
  355. )
  356. {
  357. PMYTOKEN pMyToken = (PMYTOKEN)hMyToken;
  358. CHAR string[MAX_STRING_BYTES];
  359. UINT GroupIndex;
  360. UINT PrivIndex;
  361. if (pMyToken == NULL) {
  362. return(FALSE);
  363. }
  364. //
  365. // Authentication ID
  366. //
  367. if (pMyToken->TokenStats != NULL) {
  368. wsprintf(string, "0x%lx-%lx",
  369. pMyToken->TokenStats->AuthenticationId.HighPart,
  370. pMyToken->TokenStats->AuthenticationId.LowPart);
  371. SetDlgItemText(hDlg, IDS_LOGONSESSION, string);
  372. } else {
  373. DbgPrint("SECEDIT : No token statistics in mytoken\n");
  374. }
  375. //
  376. // Groups
  377. //
  378. if (pMyToken->Groups != NULL) {
  379. for (GroupIndex=0; GroupIndex < pMyToken->Groups->GroupCount; GroupIndex++ ) {
  380. PSID Sid = pMyToken->Groups->Groups[GroupIndex].Sid;
  381. ULONG Attributes = pMyToken->Groups->Groups[GroupIndex].Attributes;
  382. USHORT ControlID;
  383. if (Attributes & SE_GROUP_ENABLED) {
  384. ControlID = IDL_ENABLEDGROUPS;
  385. } else {
  386. ControlID = IDL_DISABLEDGROUPS;
  387. }
  388. if (SID2Name(Sid, string, MAX_STRING_BYTES)) {
  389. // Add to disable or enabled group box
  390. AddLBItem(hDlg, ControlID, string, GroupIndex);
  391. // Add this group to default owner combo box if it's valid
  392. if (Attributes & SE_GROUP_OWNER) {
  393. AddCBItem(hDlg, IDC_DEFAULTOWNER, string, (LONG_PTR)Sid);
  394. }
  395. // Add this group to primary group combo box
  396. AddCBItem(hDlg, IDC_PRIMARYGROUP, string, (LONG_PTR)Sid);
  397. } else {
  398. DbgPrint("SECEDIT: Failed to convert Group sid to string\n");
  399. }
  400. }
  401. } else {
  402. DbgPrint("SECEDIT : No group info in mytoken\n");
  403. }
  404. //
  405. // User ID
  406. //
  407. if (pMyToken->UserId != NULL) {
  408. PSID Sid = pMyToken->UserId->User.Sid;
  409. if (SID2Name(Sid, string, MAX_STRING_BYTES)) {
  410. // Set user-name static text
  411. SetDlgItemText(hDlg, IDS_USERID, string);
  412. // Add to default owner combo box
  413. AddCBItem(hDlg, IDC_DEFAULTOWNER, string, (LONG_PTR)Sid);
  414. // Add to primary group combo box
  415. AddCBItem(hDlg, IDC_PRIMARYGROUP, string, (LONG_PTR)Sid);
  416. } else {
  417. DbgPrint("SECEDIT: Failed to convert User ID SID to string\n");
  418. }
  419. } else {
  420. DbgPrint("SECEDIT: No user id in mytoken\n");
  421. }
  422. //
  423. // Default Owner
  424. //
  425. if (pMyToken->DefaultOwner != NULL) {
  426. PSID Sid = pMyToken->DefaultOwner->Owner;
  427. if (SID2Name(Sid, string, MAX_STRING_BYTES)) {
  428. INT iItem;
  429. iItem = FindCBSid(hDlg, IDC_DEFAULTOWNER, Sid);
  430. if (iItem >= 0) {
  431. SendMessage(GetDlgItem(hDlg, IDC_DEFAULTOWNER), CB_SETCURSEL, iItem, 0);
  432. } else {
  433. DbgPrint("SECEDIT: Default Owner is not userID or one of our groups\n");
  434. }
  435. } else {
  436. DbgPrint("SECEDIT: Failed to convert Default Owner SID to string\n");
  437. }
  438. } else {
  439. DbgPrint("SECEDIT: No default owner in mytoken\n");
  440. }
  441. //
  442. // Primary group
  443. //
  444. if (pMyToken->PrimaryGroup != NULL) {
  445. PSID Sid = pMyToken->PrimaryGroup->PrimaryGroup;
  446. if (SID2Name(Sid, string, MAX_STRING_BYTES)) {
  447. INT iItem;
  448. iItem = FindCBSid(hDlg, IDC_PRIMARYGROUP, Sid);
  449. if (iItem < 0) {
  450. // Group is not already in combo-box, add it
  451. iItem = AddCBItem(hDlg, IDC_PRIMARYGROUP, string, (LONG_PTR)Sid);
  452. }
  453. // Select the primary group
  454. SendMessage(GetDlgItem(hDlg, IDC_PRIMARYGROUP), CB_SETCURSEL, iItem, 0);
  455. } else {
  456. DbgPrint("SECEDIT: Failed to convert primary group SID to string\n");
  457. }
  458. } else {
  459. DbgPrint("SECEDIT: No primary group in mytoken\n");
  460. }
  461. //
  462. // Privileges
  463. //
  464. if (pMyToken->Privileges != NULL) {
  465. for (PrivIndex=0; PrivIndex < pMyToken->Privileges->PrivilegeCount; PrivIndex++ ) {
  466. LUID Privilege = pMyToken->Privileges->Privileges[PrivIndex].Luid;
  467. ULONG Attributes = pMyToken->Privileges->Privileges[PrivIndex].Attributes;
  468. USHORT ControlID;
  469. if (Attributes & SE_PRIVILEGE_ENABLED) {
  470. ControlID = IDL_ENABLEDPRIVILEGES;
  471. } else {
  472. ControlID = IDL_DISABLEDPRIVILEGES;
  473. }
  474. if (PRIV2Name(Privilege, string, MAX_STRING_BYTES)) {
  475. // Add this privelege to the appropriate list-box
  476. AddLBItem(hDlg, ControlID, string, PrivIndex);
  477. } else {
  478. DbgPrint("SECEDIT: Failed to convert privilege to string\n");
  479. }
  480. }
  481. } else {
  482. DbgPrint("SECEDIT: No privelege info in mytoken\n");
  483. }
  484. return(TRUE);
  485. }
  486. /****************************************************************************
  487. FUNCTION: EnablePrivilege(HWND, fEnable)
  488. PURPOSE: Enables or disables one or more privileges.
  489. If fEnable = TRUE, the selected privileges in the disabled
  490. privilege control are enabled.
  491. Vice versa for fEnable = FALSE
  492. RETURNS: TRUE on success, FALSE on failure
  493. ****************************************************************************/
  494. BOOL
  495. EnablePrivilege(
  496. HWND hDlg,
  497. BOOL fEnable
  498. )
  499. {
  500. HWND hwndFrom;
  501. HWND hwndTo;
  502. USHORT idFrom;
  503. USHORT idTo;
  504. INT cItems;
  505. PINT pItems;
  506. PMYTOKEN pMyToken = (PMYTOKEN)hMyToken;
  507. PTOKEN_PRIVILEGES Privileges;
  508. if (pMyToken == NULL) {
  509. return(FALSE);
  510. }
  511. Privileges = pMyToken->Privileges;
  512. if (Privileges == NULL) {
  513. return(FALSE);
  514. }
  515. // Calculate source and destination controls
  516. //
  517. if (fEnable) {
  518. idFrom = IDL_DISABLEDPRIVILEGES;
  519. idTo = IDL_ENABLEDPRIVILEGES;
  520. } else {
  521. idFrom = IDL_ENABLEDPRIVILEGES;
  522. idTo = IDL_DISABLEDPRIVILEGES;
  523. }
  524. hwndFrom = GetDlgItem(hDlg, idFrom);
  525. hwndTo = GetDlgItem(hDlg, idTo);
  526. // Find how many items are selected
  527. //
  528. cItems = (int)SendMessage(hwndFrom, LB_GETSELCOUNT, 0, 0);
  529. if (cItems <= 0) {
  530. // No items selected
  531. return(TRUE);
  532. }
  533. // Allocate space for the item array
  534. //
  535. pItems = Alloc(cItems * sizeof(*pItems));
  536. if (pItems == NULL) {
  537. return(FALSE);
  538. }
  539. // Read the selected items into the array
  540. //
  541. cItems = (int)SendMessage(hwndFrom, LB_GETSELITEMS, (WPARAM)cItems, (LPARAM)pItems);
  542. if (cItems == LB_ERR) {
  543. // Something went wrong
  544. Free(pItems);
  545. return(FALSE);
  546. }
  547. while (cItems-- > 0) {
  548. INT iItem;
  549. UINT PrivIndex;
  550. UCHAR PrivilegeName[MAX_STRING_BYTES];
  551. iItem = pItems[cItems]; // Read the item index from the selected item array
  552. // Read the text and data from the source item
  553. //
  554. PrivIndex = (UINT)SendMessage(hwndFrom, LB_GETITEMDATA, iItem, 0);
  555. SendMessage(hwndFrom, LB_GETTEXT, iItem, (LPARAM)PrivilegeName);
  556. // Delete item from source control
  557. //
  558. SendMessage(hwndFrom, LB_DELETESTRING, iItem, 0);
  559. // Add privilege to destination control
  560. //
  561. iItem = (INT)SendMessage(hwndTo, LB_ADDSTRING, 0, (LPARAM)PrivilegeName);
  562. SendMessage(hwndTo, LB_SETITEMDATA, iItem, (LONG)PrivIndex);
  563. // Modify global data structure to reflect change
  564. //
  565. if (fEnable) {
  566. Privileges->Privileges[PrivIndex].Attributes |= SE_PRIVILEGE_ENABLED;
  567. } else {
  568. Privileges->Privileges[PrivIndex].Attributes &= ~SE_PRIVILEGE_ENABLED;
  569. }
  570. }
  571. // Free up space allocated for selected item array
  572. Free(pItems);
  573. return(TRUE);
  574. }
  575. /****************************************************************************
  576. FUNCTION: EnableGroup(HWND, fEnable)
  577. PURPOSE: Enables or disables one or more selected groups.
  578. If fEnable = TRUE, the selected groups in the disabled
  579. group control are enabled.
  580. If fEnable = FALSE the selected groups in the enabled
  581. group control are disabled.
  582. RETURNS: TRUE on success, FALSE on failure
  583. ****************************************************************************/
  584. BOOL
  585. EnableGroup(
  586. HWND hDlg,
  587. BOOL fEnable
  588. )
  589. {
  590. HWND hwndFrom;
  591. HWND hwndTo;
  592. USHORT idFrom;
  593. USHORT idTo;
  594. INT cItems;
  595. PINT pItems;
  596. PMYTOKEN pMyToken = (PMYTOKEN)hMyToken;
  597. PTOKEN_GROUPS Groups;
  598. if (pMyToken == NULL) {
  599. return(FALSE);
  600. }
  601. Groups = pMyToken->Groups;
  602. if (Groups == NULL) {
  603. return(FALSE);
  604. }
  605. // Calculate source and destination controls
  606. //
  607. if (fEnable) {
  608. idFrom = IDL_DISABLEDGROUPS;
  609. idTo = IDL_ENABLEDGROUPS;
  610. } else {
  611. idFrom = IDL_ENABLEDGROUPS;
  612. idTo = IDL_DISABLEDGROUPS;
  613. }
  614. hwndFrom = GetDlgItem(hDlg, idFrom);
  615. hwndTo = GetDlgItem(hDlg, idTo);
  616. // Find how many items are selected
  617. //
  618. cItems = (int)SendMessage(hwndFrom, LB_GETSELCOUNT, 0, 0);
  619. if (cItems <= 0) {
  620. // No items selected
  621. return(TRUE);
  622. }
  623. // Allocate space for the item array
  624. //
  625. pItems = Alloc(cItems * sizeof(*pItems));
  626. if (pItems == NULL) {
  627. return(FALSE);
  628. }
  629. // Read the selected items into the array
  630. //
  631. cItems = (int)SendMessage(hwndFrom, LB_GETSELITEMS, (WPARAM)cItems, (LPARAM)pItems);
  632. if (cItems == LB_ERR) {
  633. // Something went wrong
  634. Free(pItems);
  635. return(FALSE);
  636. }
  637. while (cItems-- > 0) {
  638. INT iItem;
  639. UINT GroupIndex;
  640. UCHAR GroupName[MAX_STRING_BYTES];
  641. iItem = pItems[cItems]; // Read the item index from the selected item array
  642. // Read the text and data from the source item
  643. //
  644. GroupIndex = (UINT)SendMessage(hwndFrom, LB_GETITEMDATA, iItem, 0);
  645. SendMessage(hwndFrom, LB_GETTEXT, iItem, (LPARAM)GroupName);
  646. // Check it's not a mandatory group (Can-not be disabled)
  647. //
  648. if (Groups->Groups[GroupIndex].Attributes & SE_GROUP_MANDATORY) {
  649. CHAR buf[256];
  650. strcpy(buf, "'");
  651. strcat(buf, GroupName);
  652. strcat(buf, "' is a mandatory group and cannot be disabled");
  653. MessageBox(hDlg, buf, NULL, MB_ICONSTOP | MB_APPLMODAL | MB_OK);
  654. continue; // skip to next group
  655. }
  656. // Delete item from source control
  657. //
  658. SendMessage(hwndFrom, LB_DELETESTRING, iItem, 0);
  659. // Add item to destination control
  660. //
  661. iItem = (INT)SendMessage(hwndTo, LB_ADDSTRING, 0, (LPARAM)GroupName);
  662. SendMessage(hwndTo, LB_SETITEMDATA, iItem, (LONG)GroupIndex);
  663. // Modify global data structure to reflect change
  664. //
  665. if (fEnable) {
  666. Groups->Groups[GroupIndex].Attributes |= SE_GROUP_ENABLED;
  667. } else {
  668. Groups->Groups[GroupIndex].Attributes &= ~SE_GROUP_ENABLED;
  669. }
  670. }
  671. // Free up space allocated for selected item array
  672. Free(pItems);
  673. return(TRUE);
  674. }
  675. /****************************************************************************
  676. FUNCTION: SetDefaultOwner()
  677. PURPOSE: Sets the default owner to the new value selected by the user.
  678. RETURNS: TRUE on success, FALSE on failure
  679. ****************************************************************************/
  680. BOOL
  681. SetDefaultOwner(
  682. HWND hDlg
  683. )
  684. {
  685. HWND hwnd;
  686. INT iItem;
  687. PMYTOKEN pMyToken = (PMYTOKEN)hMyToken;
  688. PTOKEN_OWNER DefaultOwner;
  689. if (pMyToken == NULL) {
  690. return(FALSE);
  691. }
  692. DefaultOwner = pMyToken->DefaultOwner;
  693. if (DefaultOwner == NULL) {
  694. return(FALSE);
  695. }
  696. hwnd = GetDlgItem(hDlg, IDC_DEFAULTOWNER);
  697. iItem = (INT)SendMessage(hwnd, CB_GETCURSEL, 0, 0);
  698. if (iItem == CB_ERR) {
  699. // No selection ?
  700. return(FALSE);
  701. }
  702. // Modify global data structure to reflect change
  703. DefaultOwner->Owner = (PSID)SendMessage(hwnd, CB_GETITEMDATA, iItem, 0);
  704. return(TRUE);
  705. }
  706. /****************************************************************************
  707. FUNCTION: SetPrimaryGroup()
  708. PURPOSE: Sets the primary group to the new value selected by the user.
  709. RETURNS: TRUE on success, FALSE on failure
  710. ****************************************************************************/
  711. BOOL
  712. SetPrimaryGroup(
  713. HWND hDlg
  714. )
  715. {
  716. HWND hwnd;
  717. INT iItem;
  718. PMYTOKEN pMyToken = (PMYTOKEN)hMyToken;
  719. PTOKEN_PRIMARY_GROUP PrimaryGroup;
  720. if (pMyToken == NULL) {
  721. return(FALSE);
  722. }
  723. PrimaryGroup = pMyToken->PrimaryGroup;
  724. if (PrimaryGroup == NULL) {
  725. return(FALSE);
  726. }
  727. hwnd = GetDlgItem(hDlg, IDC_PRIMARYGROUP);
  728. iItem = (INT)SendMessage(hwnd, CB_GETCURSEL, 0, 0);
  729. if (iItem == CB_ERR) {
  730. // No selection ?
  731. return(FALSE);
  732. }
  733. // Modify global data structure to reflect change
  734. PrimaryGroup->PrimaryGroup = (PSID)SendMessage(hwnd, CB_GETITEMDATA, iItem, 0);
  735. return(TRUE);
  736. }
  737. /****************************************************************************
  738. FUNCTION: MoreDlgProc(HWND, unsigned, WORD, LONG)
  739. PURPOSE: Processes messages
  740. ****************************************************************************/
  741. INT_PTR
  742. APIENTRY
  743. MoreDlgProc(
  744. HWND hDlg,
  745. UINT message,
  746. WPARAM wParam,
  747. LPARAM lParam
  748. )
  749. {
  750. switch (message) {
  751. case WM_INITDIALOG:
  752. if (!MoreDlgInit(hDlg)) {
  753. // Failed to initialize dialog, get out
  754. EndDialog(hDlg, FALSE);
  755. }
  756. return (TRUE);
  757. case WM_COMMAND:
  758. switch (LOWORD(wParam)) {
  759. case IDOK:
  760. // we're done, drop through to quit dialog....
  761. case IDCANCEL:
  762. EndDialog(hDlg, TRUE);
  763. return TRUE;
  764. break;
  765. default:
  766. // We didn't process this message
  767. return FALSE;
  768. break;
  769. }
  770. break;
  771. default:
  772. // We didn't process this message
  773. return FALSE;
  774. }
  775. // We processed the message
  776. return TRUE;
  777. DBG_UNREFERENCED_PARAMETER(lParam);
  778. }
  779. /****************************************************************************
  780. FUNCTION: MoreDlgInit(HWND)
  781. PURPOSE: Initialises the controls in the more dialog window.
  782. RETURNS: TRUE on success, FALSE on failure
  783. ****************************************************************************/
  784. BOOL
  785. MoreDlgInit(
  786. HWND hDlg
  787. )
  788. {
  789. CHAR string[MAX_STRING_BYTES];
  790. PMYTOKEN pMyToken = (PMYTOKEN)hMyToken;
  791. PTOKEN_STATISTICS Statistics;
  792. if (pMyToken == NULL) {
  793. return(FALSE);
  794. }
  795. Statistics = pMyToken->TokenStats;
  796. if (Statistics == NULL) {
  797. DbgPrint("SECEDIT: No token statistics in mytoken\n");
  798. return(FALSE);
  799. }
  800. if (LUID2String(Statistics->TokenId, string, MAX_STRING_BYTES)) {
  801. SetDlgItemText(hDlg, IDS_TOKENID, string);
  802. } else {
  803. DbgPrint("SECEDIT: Failed to convert tokenid luid to string\n");
  804. }
  805. if (Time2String(Statistics->ExpirationTime, string, MAX_STRING_BYTES)) {
  806. SetDlgItemText(hDlg, IDS_EXPIRATIONTIME, string);
  807. } else {
  808. DbgPrint("SECEDIT: Failed to convert expiration time to string\n");
  809. }
  810. if (TokenType2String(Statistics->TokenType, string, MAX_STRING_BYTES)) {
  811. SetDlgItemText(hDlg, IDS_TOKENTYPE, string);
  812. } else {
  813. DbgPrint("SECEDIT: Failed to convert token type to string\n");
  814. }
  815. if (Statistics->TokenType == TokenPrimary) {
  816. SetDlgItemText(hDlg, IDS_IMPERSONATION, "N/A");
  817. } else {
  818. if (ImpersonationLevel2String(Statistics->ImpersonationLevel, string, MAX_STRING_BYTES)) {
  819. SetDlgItemText(hDlg, IDS_IMPERSONATION, string);
  820. } else {
  821. DbgPrint("SECEDIT: Failed to convert impersonation level to string\n");
  822. }
  823. }
  824. if (Dynamic2String(Statistics->DynamicCharged, string, MAX_STRING_BYTES)) {
  825. SetDlgItemText(hDlg, IDS_DYNAMICCHARGED, string);
  826. } else {
  827. DbgPrint("SECEDIT: Failed to convert dynamic charged to string\n");
  828. }
  829. if (Dynamic2String(Statistics->DynamicAvailable, string, MAX_STRING_BYTES)) {
  830. SetDlgItemText(hDlg, IDS_DYNAMICAVAILABLE, string);
  831. } else {
  832. DbgPrint("SECEDIT: Failed to convert dynamic available to string\n");
  833. }
  834. if (LUID2String(Statistics->ModifiedId, string, MAX_STRING_BYTES)) {
  835. SetDlgItemText(hDlg, IDS_MODIFIEDID, string);
  836. } else {
  837. DbgPrint("SECEDIT: Failed to convert modifiedid luid to string\n");
  838. }
  839. return(TRUE);
  840. }
  841. /****************************************************************************
  842. FUNCTION: ListDlgProc(HWND, unsigned, WORD, LONG)
  843. PURPOSE: Processes messages
  844. ****************************************************************************/
  845. INT_PTR
  846. APIENTRY
  847. ListDlgProc(
  848. HWND hDlg,
  849. UINT message,
  850. WPARAM wParam,
  851. LPARAM lParam
  852. )
  853. {
  854. HWND hwndEdit = NULL;
  855. switch (message) {
  856. case WM_INITDIALOG:
  857. if (!ListDlgInit(hDlg)) {
  858. // Failed to initialize dialog, get out
  859. EndDialog(hDlg, FALSE);
  860. }
  861. return (TRUE);
  862. case WM_COMMAND:
  863. switch (LOWORD(wParam)) {
  864. case IDOK:
  865. hwndEdit = ListDlgEnd(hDlg);
  866. // We're done, drop through to enddialog...
  867. case IDCANCEL:
  868. EndDialog(hDlg, (INT_PTR)hwndEdit);
  869. return TRUE;
  870. break;
  871. default:
  872. // We didn't process this message
  873. return FALSE;
  874. break;
  875. }
  876. break;
  877. default:
  878. // We didn't process this message
  879. return FALSE;
  880. }
  881. // We processed the message
  882. return TRUE;
  883. DBG_UNREFERENCED_PARAMETER(lParam);
  884. }
  885. /****************************************************************************
  886. FUNCTION: ListDlgInit(HWND)
  887. PURPOSE: Initialise the window list dialog
  888. RETURNS: TRUE on success, FALSE on failure
  889. ****************************************************************************/
  890. BOOL
  891. ListDlgInit(
  892. HWND hDlg
  893. )
  894. {
  895. // Fill the list box with top-level windows and their handles
  896. EnumWindows(WindowEnum, (LONG_PTR)hDlg);
  897. return(TRUE);
  898. }
  899. /****************************************************************************
  900. FUNCTION: WindowEnum
  901. PURPOSE: Window enumeration call-back function.
  902. Adds each window to the window list-box
  903. RETURNS: TRUE to continue enumeration, FALSE to stop.
  904. ****************************************************************************/
  905. BOOL
  906. APIENTRY
  907. WindowEnum(
  908. HWND hwnd,
  909. LPARAM lParam
  910. )
  911. {
  912. HWND hDlg = (HWND)lParam;
  913. CHAR string[MAX_STRING_BYTES];
  914. if (GetWindowText(hwnd, string, MAX_STRING_BYTES) != 0) {
  915. // This window has a caption, so add it to the list-box
  916. AddLBItem(hDlg, IDLB_WINDOWLIST, string, (LONG_PTR)hwnd);
  917. }
  918. return(TRUE);
  919. }
  920. /****************************************************************************
  921. FUNCTION: ListDlgEnd(HWND)
  922. PURPOSE: Cleans up after window list dialog
  923. RETURNS: handle to window the user has selected or NULL
  924. ****************************************************************************/
  925. HWND
  926. ListDlgEnd(
  927. HWND hDlg
  928. )
  929. {
  930. HWND hwndListBox = GetDlgItem(hDlg, IDLB_WINDOWLIST);
  931. HWND hwndEdit;
  932. INT iItem;
  933. // Read selection from list-box and get its hwnd
  934. iItem = (INT)SendMessage(hwndListBox, LB_GETCURSEL, 0, 0);
  935. if (iItem == LB_ERR) {
  936. // No selection
  937. hwndEdit = NULL;
  938. } else {
  939. hwndEdit = (HWND)SendMessage(hwndListBox, LB_GETITEMDATA, iItem, 0);
  940. }
  941. return (hwndEdit);
  942. }
  943. /****************************************************************************
  944. FUNCTION: AboutDlgProc(HWND, unsigned, WORD, LONG)
  945. PURPOSE: Processes messages for About dialog
  946. ****************************************************************************/
  947. INT_PTR
  948. APIENTRY
  949. AboutDlgProc(
  950. HWND hDlg,
  951. UINT message,
  952. WPARAM wParam,
  953. LPARAM lParam
  954. )
  955. {
  956. switch (message) {
  957. case WM_COMMAND:
  958. switch (LOWORD(wParam)) {
  959. case IDOK:
  960. // we're done, drop through to quit dialog....
  961. case IDCANCEL:
  962. EndDialog(hDlg, TRUE);
  963. return TRUE;
  964. break;
  965. default:
  966. // We didn't process this message
  967. return FALSE;
  968. break;
  969. }
  970. break;
  971. default:
  972. // We didn't process this message
  973. return FALSE;
  974. }
  975. // We processed the message
  976. return TRUE;
  977. DBG_UNREFERENCED_PARAMETER(lParam);
  978. }
  979. /****************************************************************************
  980. FUNCTION: ActiveWindowDlgProc(HWND, unsigned, WORD, LONG)
  981. PURPOSE: Processes messages for Active Window Dialog
  982. ****************************************************************************/
  983. INT_PTR
  984. APIENTRY
  985. ActiveWindowDlgProc(
  986. HWND hDlg,
  987. UINT message,
  988. WPARAM wParam,
  989. LPARAM lParam
  990. )
  991. {
  992. switch (message) {
  993. case WM_COMMAND:
  994. switch (LOWORD(wParam)) {
  995. case IDOK:
  996. // we're done, drop through to quit dialog....
  997. case IDCANCEL:
  998. EndDialog(hDlg, TRUE);
  999. return TRUE;
  1000. break;
  1001. default:
  1002. // We didn't process this message
  1003. return FALSE;
  1004. break;
  1005. }
  1006. break;
  1007. default:
  1008. // We didn't process this message
  1009. return FALSE;
  1010. }
  1011. // We processed the message
  1012. return TRUE;
  1013. DBG_UNREFERENCED_PARAMETER(lParam);
  1014. }