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.

245 lines
6.8 KiB

  1. /****************************** Module Header ******************************\
  2. * Module Name: audit.c
  3. *
  4. * Copyright (c) 1991, Microsoft Corporation
  5. *
  6. * Implementation of routines that access/manipulate the system audit log
  7. *
  8. * History:
  9. * 12-09-91 Davidc Created.
  10. * 5-6-92 DaveHart Fleshed out.
  11. \***************************************************************************/
  12. #include "msgina.h"
  13. #include "authzi.h"
  14. #include "msaudite.h"
  15. /***************************************************************************\
  16. * GetAuditLogStatus
  17. *
  18. * Purpose : Fills the global data with audit log status information
  19. *
  20. * Returns: TRUE on success, FALSE on failure
  21. *
  22. * History:
  23. * 12-09-91 Davidc Created.
  24. * 5-6-92 DaveHart Fleshed out.
  25. \***************************************************************************/
  26. BOOL
  27. GetAuditLogStatus(
  28. PGLOBALS pGlobals
  29. )
  30. {
  31. EVENTLOG_FULL_INFORMATION EventLogFullInformation;
  32. DWORD dwBytesNeeded;
  33. HANDLE AuditLogHandle;
  34. //
  35. // Assume the log is not full. If we can't get to EventLog, tough.
  36. //
  37. pGlobals->AuditLogFull = FALSE;
  38. AuditLogHandle = OpenEventLog( NULL, TEXT("Security"));
  39. if (AuditLogHandle) {
  40. if (GetEventLogInformation(AuditLogHandle,
  41. EVENTLOG_FULL_INFO,
  42. &EventLogFullInformation,
  43. sizeof(EventLogFullInformation),
  44. &dwBytesNeeded ) ) {
  45. if (EventLogFullInformation.dwFull != FALSE) {
  46. pGlobals->AuditLogFull = TRUE;
  47. }
  48. }
  49. CloseEventLog(AuditLogHandle);
  50. }
  51. //
  52. // There's no way in the current event logger to tell how full the log
  53. // is, always indicate we're NOT near full.
  54. //
  55. pGlobals->AuditLogNearFull = FALSE;
  56. return TRUE;
  57. }
  58. /***************************************************************************\
  59. * DisableAuditing
  60. *
  61. * Purpose : Disable auditing via LSA.
  62. *
  63. * Returns: TRUE on success, FALSE on failure
  64. *
  65. * History:
  66. * 5-6-92 DaveHart Created.
  67. \***************************************************************************/
  68. BOOL
  69. DisableAuditing()
  70. {
  71. NTSTATUS Status, IgnoreStatus;
  72. PPOLICY_AUDIT_EVENTS_INFO AuditInfo;
  73. OBJECT_ATTRIBUTES ObjectAttributes;
  74. SECURITY_QUALITY_OF_SERVICE SecurityQualityOfService;
  75. LSA_HANDLE PolicyHandle;
  76. //
  77. // Set up the Security Quality Of Service for connecting to the
  78. // LSA policy object.
  79. //
  80. SecurityQualityOfService.Length = sizeof(SECURITY_QUALITY_OF_SERVICE);
  81. SecurityQualityOfService.ImpersonationLevel = SecurityImpersonation;
  82. SecurityQualityOfService.ContextTrackingMode = SECURITY_DYNAMIC_TRACKING;
  83. SecurityQualityOfService.EffectiveOnly = FALSE;
  84. //
  85. // Set up the object attributes to open the Lsa policy object
  86. //
  87. InitializeObjectAttributes(
  88. &ObjectAttributes,
  89. NULL,
  90. 0L,
  91. NULL,
  92. NULL
  93. );
  94. ObjectAttributes.SecurityQualityOfService = &SecurityQualityOfService;
  95. //
  96. // Open the local LSA policy object
  97. //
  98. Status = LsaOpenPolicy(
  99. NULL,
  100. &ObjectAttributes,
  101. POLICY_VIEW_AUDIT_INFORMATION | POLICY_SET_AUDIT_REQUIREMENTS,
  102. &PolicyHandle
  103. );
  104. if (!NT_SUCCESS(Status)) {
  105. DebugLog((DEB_ERROR, "Failed to open LsaPolicyObject Status = 0x%lx", Status));
  106. return FALSE;
  107. }
  108. Status = LsaQueryInformationPolicy(
  109. PolicyHandle,
  110. PolicyAuditEventsInformation,
  111. (PVOID *)&AuditInfo
  112. );
  113. if (!NT_SUCCESS(Status)) {
  114. IgnoreStatus = LsaClose(PolicyHandle);
  115. ASSERT(NT_SUCCESS(IgnoreStatus));
  116. DebugLog((DEB_ERROR, "Failed to query audit event info Status = 0x%lx", Status));
  117. return FALSE;
  118. }
  119. if (AuditInfo->AuditingMode) {
  120. AuditInfo->AuditingMode = FALSE;
  121. Status = LsaSetInformationPolicy(
  122. PolicyHandle,
  123. PolicyAuditEventsInformation,
  124. AuditInfo
  125. );
  126. } else {
  127. Status = STATUS_SUCCESS;
  128. }
  129. IgnoreStatus = LsaFreeMemory(AuditInfo);
  130. ASSERT(NT_SUCCESS(IgnoreStatus));
  131. IgnoreStatus = LsaClose(PolicyHandle);
  132. ASSERT(NT_SUCCESS(IgnoreStatus));
  133. if (!NT_SUCCESS(Status)) {
  134. DebugLog((DEB_ERROR, "Failed to disable auditing Status = 0x%lx", Status));
  135. return FALSE;
  136. }
  137. return TRUE;
  138. }
  139. DWORD
  140. GenerateCachedUnlockAudit(
  141. IN PSID pUserSid,
  142. IN PCWSTR pszUser,
  143. IN PCWSTR pszDomain
  144. )
  145. {
  146. DWORD dwRet = ERROR_SUCCESS;
  147. WCHAR szComputerName[CNLEN + sizeof('\0')] = L"-";
  148. DWORD dwComputerNameSize = ARRAYSIZE(szComputerName);
  149. LUID Luid;
  150. LUID SystemLuid = SYSTEM_LUID;
  151. if( !pUserSid || !pszUser )
  152. {
  153. DebugLog((DEB_ERROR, "GenerateCachedUnlockAudit got invalid parameters"));
  154. ASSERT(FALSE);
  155. dwRet = ERROR_INVALID_PARAMETER;
  156. goto ErrorReturn;
  157. }
  158. //
  159. // Generate a locally unique id to include in the logon sid
  160. // Note that this is a dummy SID. We don't want to use the logon
  161. // LUID as this is specific to logon/logoff. Also a NULL LUID
  162. // is seen as meaningless, so we have to generate a random one
  163. //
  164. if( !AllocateLocallyUniqueId(&Luid) )
  165. {
  166. dwRet = GetLastError();
  167. DebugLog((DEB_ERROR, "AllocateLocallyUniqueId failed, error = 0x%lx", dwRet));
  168. goto ErrorReturn;
  169. }
  170. //
  171. // Ignore the failure
  172. //
  173. GetComputerName(szComputerName, &dwComputerNameSize);
  174. if( !AuthziSourceAudit(
  175. APF_AuditSuccess,
  176. SE_CATEGID_LOGON, //category id
  177. SE_AUDITID_SUCCESSFUL_LOGON, //audit id
  178. L"Security",
  179. pUserSid, //the user sid
  180. 12, //count for va section
  181. APT_String, pszUser,
  182. APT_String, pszDomain ? pszDomain : L"-",
  183. APT_Luid, Luid,
  184. APT_Ulong, CachedUnlock,
  185. APT_String, L"Winlogon",
  186. APT_String, L"Winlogon unlock cache",
  187. APT_String, szComputerName,
  188. APT_String, L"-",
  189. APT_String, L"SYSTEM",
  190. APT_String, L"NT AUTHORITY",
  191. APT_Luid, SystemLuid,
  192. APT_Ulong, GetCurrentProcessId()
  193. ) )
  194. {
  195. DebugLog((DEB_ERROR, "AuthziSourceAudit failed, error = 0x%lx", dwRet));
  196. dwRet = GetLastError();
  197. }
  198. ErrorReturn:
  199. return dwRet;
  200. }