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.

224 lines
4.9 KiB

  1. /*++
  2. Copyright (c) 2001 Microsoft Corporation
  3. Module Name:
  4. scaudit.cxx
  5. Abstract:
  6. Auditing related functions.
  7. Author:
  8. 16-May-2001 kumarp
  9. */
  10. #include "precomp.hxx"
  11. #pragma hdrstop
  12. #include "scaudit.h"
  13. #include "authz.h"
  14. #include "authzi.h"
  15. #include "msaudite.h"
  16. #include "account.h"
  17. DWORD
  18. ScGenerateServiceInstallAudit(
  19. IN PCWSTR pszServiceName,
  20. IN PCWSTR pszServiceImageName,
  21. IN DWORD dwServiceType,
  22. IN DWORD dwStartType,
  23. IN PCWSTR pszServiceAccount
  24. )
  25. /*++
  26. Routine Description:
  27. Generate SE_AUDITID_SERVICE_INSTALL audit event.
  28. Arguments:
  29. pszServiceName - name of the service installed
  30. pszServiceImageName - name of the service binary
  31. dwServiceType - type of the service
  32. dwStartType - start type of the service
  33. pszServiceAccount - user account under which the service will run
  34. Return Value:
  35. Win32 error code
  36. Notes:
  37. --*/
  38. {
  39. NTSTATUS Status = STATUS_SUCCESS;
  40. DWORD dwError = NO_ERROR;
  41. BOOL fResult = FALSE;
  42. BOOL fImpersonated = FALSE;
  43. AUTHZ_AUDIT_EVENT_TYPE_HANDLE hAuditEventType = NULL;
  44. AUTHZ_AUDIT_EVENT_HANDLE hAuditEvent = NULL;
  45. AUDIT_PARAMS AuditParams = {0};
  46. #define NUM_AUDIT_PARAMS 8
  47. AUDIT_PARAM ParamArray[NUM_AUDIT_PARAMS];
  48. PSID pUserSid = NULL;
  49. ASSERT( pszServiceName && *pszServiceName );
  50. ASSERT( pszServiceImageName && *pszServiceImageName );
  51. ASSERT( pszServiceAccount ? *pszServiceAccount : TRUE );
  52. ASSERT( (dwStartType == SERVICE_BOOT_START) ||
  53. (dwStartType <= SERVICE_DISABLED) );
  54. ASSERT( !(dwServiceType & ~SERVICE_TYPE_ALL) );
  55. RtlZeroMemory( ParamArray, sizeof(AUDIT_PARAM)*NUM_AUDIT_PARAMS );
  56. if ( pszServiceAccount == NULL )
  57. {
  58. pszServiceAccount = SC_LOCAL_SYSTEM_USER_NAME;
  59. }
  60. //
  61. // initialize the event of type SE_AUDITID_SERVICE_INSTALL
  62. //
  63. fResult = AuthziInitializeAuditEventType(
  64. 0,
  65. SE_CATEGID_DETAILED_TRACKING,
  66. SE_AUDITID_SERVICE_INSTALL,
  67. 6,
  68. &hAuditEventType
  69. );
  70. if ( !fResult )
  71. {
  72. goto Error;
  73. }
  74. //
  75. // impersonate the client so that AuthziInitializeAuditParams can
  76. // get the client context from the thread token
  77. //
  78. Status = I_RpcMapWin32Status(RpcImpersonateClient( NULL ));
  79. if ( !NT_SUCCESS( Status ))
  80. {
  81. dwError = RtlNtStatusToDosError( Status );
  82. goto Cleanup;
  83. }
  84. fImpersonated = TRUE;
  85. AuditParams.Parameters = ParamArray;
  86. //
  87. // add parameter values to the event
  88. //
  89. fResult = AuthziInitializeAuditParams(
  90. APF_AuditSuccess,
  91. &AuditParams,
  92. &pUserSid,
  93. L"Security",
  94. 6,
  95. APT_String, pszServiceName,
  96. APT_String, pszServiceImageName,
  97. APT_Ulong, dwServiceType,
  98. APT_Ulong, dwStartType,
  99. APT_String, pszServiceAccount,
  100. APT_LogonId | AP_ClientLogonId
  101. );
  102. if ( !fResult )
  103. {
  104. goto Error;
  105. }
  106. //
  107. // some more initialization
  108. //
  109. fResult = AuthziInitializeAuditEvent(
  110. 0, // flags
  111. NULL, // resource manager
  112. hAuditEventType,
  113. &AuditParams,
  114. NULL, // hAuditQueue
  115. INFINITE, // time out
  116. L"", L"", L"", L"", // obj access strings
  117. &hAuditEvent);
  118. if ( !fResult )
  119. {
  120. goto Error;
  121. }
  122. if ( fImpersonated )
  123. {
  124. fImpersonated = FALSE;
  125. (void) I_RpcMapWin32Status(RpcRevertToSelf());
  126. }
  127. //
  128. // finally, send the event to auditing module
  129. //
  130. fResult = AuthziLogAuditEvent(
  131. 0, // flags
  132. hAuditEvent,
  133. NULL); // reserved
  134. if ( !fResult )
  135. {
  136. goto Error;
  137. }
  138. Cleanup:
  139. if ( fImpersonated )
  140. {
  141. Status = I_RpcMapWin32Status(RpcRevertToSelf());
  142. if ( !NT_SUCCESS( Status ))
  143. {
  144. dwError = RtlNtStatusToDosError( Status );
  145. }
  146. }
  147. if ( hAuditEvent )
  148. {
  149. AuthzFreeAuditEvent( hAuditEvent );
  150. }
  151. if ( hAuditEventType )
  152. {
  153. AuthziFreeAuditEventType( hAuditEventType );
  154. }
  155. if ( pUserSid )
  156. {
  157. LocalFree( pUserSid );
  158. }
  159. #if DBG
  160. if ( dwError != NO_ERROR )
  161. {
  162. SC_LOG1(ERROR, "ScGenerateServiceInstallAudit failed: %lx\n", dwError);
  163. }
  164. #endif
  165. return dwError;
  166. Error:
  167. dwError = GetLastError();
  168. goto Cleanup;
  169. }