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.

109 lines
3.3 KiB

  1. /*****************************************************************************
  2. A U D I T
  3. Name: audit.c
  4. Date: 21-Jan-1994
  5. Creator: Unknown
  6. Description:
  7. *****************************************************************************/
  8. #include <windows.h>
  9. #include "clipbook.h"
  10. #include "auditchk.h"
  11. //////////////////////////////////////////////////////////////////////////
  12. //
  13. // Purpose: Tests, enables, or disables the Security privilege, which
  14. // allows auditing to take place.
  15. //
  16. // Parameters:
  17. // fAudit - Flag, which can take on one of these values:
  18. // AUDIT_PRIVILEGE_CHECK - Turns on Security, then turns it off.
  19. // Used to test whether you CAN edit auditing.
  20. // AUDIT_PRIVILEGE_ON - Turns on auditing privilege.
  21. // AUDIT_PRIVILEGE_OFF - Turns off auditing privilege.
  22. //
  23. // Return: TRUE if the function succeeds, FALSE on failure.
  24. //
  25. //////////////////////////////////////////////////////////////////////////
  26. BOOL AuditPrivilege(
  27. int fAudit)
  28. {
  29. HANDLE hToken;
  30. LUID SecurityValue;
  31. TOKEN_PRIVILEGES tkp;
  32. BOOL fOK = FALSE;
  33. /* Retrieve a handle of the access token. */
  34. if (OpenProcessToken (GetCurrentProcess(),
  35. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
  36. &hToken))
  37. {
  38. /*
  39. * Enable the SE_SECURITY_NAME privilege or disable
  40. * all privileges, depending on the fEnable flag.
  41. */
  42. if (LookupPrivilegeValue ((LPSTR)NULL,
  43. SE_SECURITY_NAME,
  44. &SecurityValue))
  45. {
  46. tkp.PrivilegeCount = 1;
  47. tkp.Privileges[0].Luid = SecurityValue;
  48. // Try to turn on audit privilege
  49. if (AUDIT_PRIVILEGE_CHECK == fAudit || AUDIT_PRIVILEGE_ON == fAudit)
  50. {
  51. tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  52. AdjustTokenPrivileges (hToken,
  53. FALSE,
  54. &tkp,
  55. sizeof(TOKEN_PRIVILEGES),
  56. (PTOKEN_PRIVILEGES)NULL,
  57. (PDWORD)NULL);
  58. /* The return value of AdjustTokenPrivileges be texted. */
  59. if (GetLastError () == ERROR_SUCCESS)
  60. {
  61. fOK = TRUE;
  62. }
  63. }
  64. // Try to turn OFF audit privilege
  65. if (AUDIT_PRIVILEGE_CHECK == fAudit || AUDIT_PRIVILEGE_OFF == fAudit)
  66. {
  67. AdjustTokenPrivileges (hToken,
  68. TRUE,
  69. NULL,
  70. 0L,
  71. (PTOKEN_PRIVILEGES)NULL,
  72. (PDWORD)NULL);
  73. if (ERROR_SUCCESS == GetLastError () &&
  74. AUDIT_PRIVILEGE_OFF == fAudit)
  75. {
  76. fOK = TRUE;
  77. }
  78. }
  79. }
  80. }
  81. return fOK;
  82. }