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.

445 lines
9.9 KiB

  1. /****************************************************************************
  2. PROGRAM: UTIL.C
  3. PURPOSE: System utility routines
  4. ****************************************************************************/
  5. #include "SECEDIT.h"
  6. #include <string.h>
  7. /****************************************************************************
  8. FUNCTION: Alloc
  9. PURPOSE: Allocates memory to hold the specified number of bytes
  10. RETURNS : Pointer to allocated memory or NULL on failure
  11. ****************************************************************************/
  12. PVOID
  13. Alloc(
  14. SIZE_T Bytes
  15. )
  16. {
  17. HANDLE hMem;
  18. PVOID Buffer;
  19. hMem = LocalAlloc(LMEM_MOVEABLE, Bytes + sizeof(hMem));
  20. if (hMem == NULL) {
  21. return(NULL);
  22. }
  23. // Lock down the memory
  24. //
  25. Buffer = LocalLock(hMem);
  26. if (Buffer == NULL) {
  27. LocalFree(hMem);
  28. return(NULL);
  29. }
  30. //
  31. // Store the handle at the start of the memory block and return
  32. // a pointer to just beyond it.
  33. //
  34. *((PHANDLE)Buffer) = hMem;
  35. return (PVOID)(((PHANDLE)Buffer)+1);
  36. }
  37. /****************************************************************************
  38. FUNCTION: GetAllocSize
  39. PURPOSE: Returns the allocated size of the specified memory block.
  40. The block must have been previously allocated using Alloc
  41. RETURNS : Size of memory block in bytes or 0 on error
  42. ****************************************************************************/
  43. SIZE_T
  44. GetAllocSize(
  45. PVOID Buffer)
  46. {
  47. HANDLE hMem;
  48. hMem = *(((PHANDLE)Buffer) - 1);
  49. return(LocalSize(hMem) - sizeof(hMem));
  50. }
  51. /****************************************************************************
  52. FUNCTION: Free
  53. PURPOSE: Frees the memory previously allocated with Alloc
  54. RETURNS : TRUE on success, otherwise FALSE
  55. ****************************************************************************/
  56. BOOL
  57. Free(
  58. PVOID Buffer
  59. )
  60. {
  61. HANDLE hMem;
  62. hMem = *(((PHANDLE)Buffer) - 1);
  63. LocalUnlock(hMem);
  64. return(LocalFree(hMem) == NULL);
  65. }
  66. /****************************************************************************
  67. FUNCTION: LUID2String
  68. PURPOSE: Converts a LUID into a readable string.
  69. RETURNS : TRUE on success otherwise FALSE.
  70. ****************************************************************************/
  71. BOOL
  72. LUID2String(
  73. LUID Luid,
  74. LPSTR String,
  75. UINT MaxStringBytes
  76. )
  77. {
  78. if (Luid.HighPart == 0) {
  79. wsprintf(String, "0x%lx", Luid.LowPart);
  80. } else {
  81. wsprintf(String, "0x%lx%08lx", Luid.HighPart, Luid.LowPart);
  82. }
  83. return(TRUE);
  84. }
  85. /****************************************************************************
  86. FUNCTION: Time2String
  87. PURPOSE: Converts a time into a readable string.
  88. RETURNS : TRUE on success otherwise FALSE.
  89. ****************************************************************************/
  90. BOOL
  91. Time2String(
  92. TIME Time,
  93. LPSTR String,
  94. UINT MaxStringBytes
  95. )
  96. {
  97. TIME_FIELDS TimeFields;
  98. RtlTimeToTimeFields(&Time, &TimeFields);
  99. if (TimeFields.Year > 2900) {
  100. strcpy(String, "Never");
  101. } else {
  102. wsprintf(String, "%d/%d/%d %02d:%02d:%02d",
  103. TimeFields.Year, TimeFields.Month, TimeFields.Day,
  104. TimeFields.Hour, TimeFields.Minute, TimeFields.Second);
  105. }
  106. return(TRUE);
  107. }
  108. /****************************************************************************
  109. FUNCTION: TokenType2String
  110. PURPOSE: Converts a tokentype into a readable string.
  111. RETURNS : TRUE on success otherwise FALSE.
  112. ****************************************************************************/
  113. BOOL
  114. TokenType2String(
  115. TOKEN_TYPE TokenType,
  116. LPSTR String,
  117. UINT MaxStringBytes
  118. )
  119. {
  120. switch (TokenType) {
  121. case TokenPrimary:
  122. strcpy(String, "Primary");
  123. break;
  124. case TokenImpersonation:
  125. strcpy(String, "Impersonation");
  126. break;
  127. default:
  128. DbgPrint("SECEDIT: TokenType2String fed unrecognised token type : 0x%x\n", TokenType);
  129. return(FALSE);
  130. break;
  131. }
  132. return(TRUE);
  133. }
  134. /****************************************************************************
  135. FUNCTION: ImpersonationLevel2String
  136. PURPOSE: Converts an impersonation level into a readable string.
  137. RETURNS : TRUE on success otherwise FALSE.
  138. ****************************************************************************/
  139. BOOL
  140. ImpersonationLevel2String(
  141. SECURITY_IMPERSONATION_LEVEL ImpersonationLevel,
  142. LPSTR String,
  143. UINT MaxStringBytes
  144. )
  145. {
  146. switch (ImpersonationLevel) {
  147. case SecurityAnonymous:
  148. strcpy(String, "Anonymous");
  149. break;
  150. case SecurityIdentification:
  151. strcpy(String, "Identification");
  152. break;
  153. case SecurityImpersonation:
  154. strcpy(String, "Impersonation");
  155. break;
  156. case SecurityDelegation:
  157. strcpy(String, "Delegation");
  158. break;
  159. default:
  160. DbgPrint("SECEDIT: ImpersonationLevel2String fed unrecognised impersonation level : 0x%x\n", ImpersonationLevel);
  161. return(FALSE);
  162. break;
  163. }
  164. return(TRUE);
  165. }
  166. /****************************************************************************
  167. FUNCTION: Dynamic2String
  168. PURPOSE: Converts an dynamic quota level into a readable string.
  169. RETURNS : TRUE on success otherwise FALSE.
  170. ****************************************************************************/
  171. BOOL
  172. Dynamic2String(
  173. ULONG Dynamic,
  174. LPSTR String,
  175. UINT MaxStringBytes
  176. )
  177. {
  178. wsprintf(String, "%ld", Dynamic);
  179. return(TRUE);
  180. }
  181. /****************************************************************************
  182. FUNCTION: AddItem
  183. PURPOSE: Adds the item string and data to the specified control
  184. The control is assumed to be a list-box unless fCBox == TRUE
  185. in which case the control is assumed to be a ComboBox
  186. RETURNS: Index at which the item was added or < 0 on error
  187. ****************************************************************************/
  188. INT
  189. AddItem(
  190. HWND hDlg,
  191. INT ControlID,
  192. LPSTR String,
  193. LONG_PTR Data,
  194. BOOL fCBox
  195. )
  196. {
  197. HWND hwnd;
  198. INT_PTR iItem;
  199. USHORT AddStringMsg = LB_ADDSTRING;
  200. USHORT SetDataMsg = LB_SETITEMDATA;
  201. if (fCBox) {
  202. AddStringMsg = CB_ADDSTRING;
  203. SetDataMsg = CB_SETITEMDATA;
  204. }
  205. hwnd = GetDlgItem(hDlg, ControlID);
  206. iItem = SendMessage(hwnd, AddStringMsg, 0, (LONG_PTR)String);
  207. if (iItem >= 0) {
  208. SendMessage(hwnd, SetDataMsg, iItem, Data);
  209. }
  210. return((INT)iItem);
  211. }
  212. /****************************************************************************
  213. FUNCTION: FindSid
  214. PURPOSE: Searches for the specified Sid in a control.
  215. RETURNS: Index of matching item or < 0 on error
  216. ****************************************************************************/
  217. INT
  218. FindSid(
  219. HWND hDlg,
  220. INT ControlID,
  221. PSID Sid,
  222. BOOL fCBox
  223. )
  224. {
  225. HWND hwnd;
  226. INT cItems;
  227. USHORT GetCountMsg = LB_GETCOUNT;
  228. USHORT GetDataMsg = LB_GETITEMDATA;
  229. if (fCBox) {
  230. GetCountMsg = CB_GETCOUNT;
  231. GetDataMsg = CB_GETITEMDATA;
  232. }
  233. hwnd = GetDlgItem(hDlg, ControlID);
  234. cItems = (INT)SendMessage(hwnd, GetCountMsg, 0, 0);
  235. if (cItems >= 0) {
  236. INT iItem;
  237. PSID ItemSid;
  238. for (iItem =0; iItem < cItems; iItem ++) {
  239. ItemSid = (PSID)SendMessage(hwnd, GetDataMsg, iItem, 0);
  240. if (RtlEqualSid(ItemSid, Sid)) {
  241. return(iItem);
  242. }
  243. }
  244. }
  245. return(-1);
  246. }
  247. static HHOOK hHookKeyboard = NULL;
  248. /****************************************************************************
  249. FUNCTION: SetHooks
  250. PURPOSE: Installs input hooks
  251. RETURNS: TRUE on success, FALSE on failure
  252. ****************************************************************************/
  253. BOOL
  254. SetHooks(
  255. HWND hwnd
  256. )
  257. {
  258. HANDLE hModHookDll;
  259. HOOKPROC lpfnKeyboardHookProc;
  260. if (hwnd == NULL) {
  261. // No-one to notify !
  262. return(FALSE);
  263. }
  264. if (hHookKeyboard != NULL) {
  265. // Hooks already installed
  266. return(FALSE);
  267. }
  268. hModHookDll = LoadLibrary("SECEDIT.DLL");
  269. if (hModHookDll == NULL) {
  270. DbgPrint("Failed to load secedit.dll\n");
  271. MessageBox(hwnd, "Failed to find secedit.dll.\nActive window context editting disabled.",
  272. NULL, MB_ICONINFORMATION | MB_APPLMODAL | MB_OK);
  273. return(FALSE);
  274. }
  275. lpfnKeyboardHookProc = (HOOKPROC)GetProcAddress(hModHookDll, "KeyboardHookProc");
  276. if (lpfnKeyboardHookProc == NULL) {
  277. DbgPrint("Failed to find keyboard hook entry point in secedit.dll\n");
  278. return(FALSE);
  279. }
  280. // Install sytem-wide keyboard hook
  281. hHookKeyboard = SetWindowsHookEx(WH_KEYBOARD, lpfnKeyboardHookProc, hModHookDll, 0);
  282. if (hHookKeyboard == NULL) {
  283. DbgPrint("SECEDIT: failed to install system keyboard hook\n");
  284. return(FALSE);
  285. }
  286. return(TRUE);
  287. }
  288. /****************************************************************************
  289. FUNCTION: ReleaseHooks
  290. PURPOSE: Uninstalls input hooks
  291. RETURNS: TRUE on success, FALSE on failure
  292. ****************************************************************************/
  293. BOOL
  294. ReleaseHooks(
  295. HWND hwnd
  296. )
  297. {
  298. BOOL Success;
  299. if (hHookKeyboard == NULL) {
  300. // Hooks not installed
  301. return(FALSE);
  302. }
  303. Success = UnhookWindowsHookEx(hHookKeyboard);
  304. if (!Success) {
  305. DbgPrint("SECEDIT: Failed to release keyboard hook\n");
  306. }
  307. // Reset global
  308. hHookKeyboard = NULL;
  309. return(Success);
  310. }