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.

360 lines
8.5 KiB

  1. #include "precomp.h"
  2. #define MAX_AUDIT_BUFFER 4096
  3. #define MAX_MSG_BUFFER 2048
  4. WCHAR gszAuditBuffer[MAX_AUDIT_BUFFER];
  5. WCHAR * gpszAuditBuffer = gszAuditBuffer;
  6. WCHAR gszAuditMsgBuffer[MAX_MSG_BUFFER];
  7. WCHAR * gpszAuditMsgBuffer = gszAuditMsgBuffer;
  8. DWORD
  9. PerformAudit(
  10. DWORD dwCategoryId,
  11. DWORD dwAuditId,
  12. PSID pSid,
  13. DWORD dwParamCnt,
  14. LPWSTR * ppszArgArray,
  15. BOOL bSuccess,
  16. BOOL bDoAudit
  17. )
  18. {
  19. SE_ADT_PARAMETER_ARRAY * pParArray = NULL;
  20. NTSTATUS ntStatus = STATUS_SUCCESS;
  21. DWORD dwStrSize = 0;
  22. DWORD i = 0;
  23. DWORD dwAllocSize = 0;
  24. BYTE * pbyteCurAddr = NULL;
  25. DWORD dwSidLength = RtlLengthSid(pSid);
  26. UNICODE_STRING * pusStrArray = NULL;
  27. WCHAR * pszModuleName = L"IPSec Server";
  28. //
  29. // dwCategoryId should be equal to SE_CATEGID_POLICY_CHANGE.
  30. //
  31. dwCategoryId = SE_CATEGID_POLICY_CHANGE;
  32. for (i = 0; i < dwParamCnt; i++) {
  33. dwStrSize += (wcslen(ppszArgArray[i]) + 1) * sizeof(WCHAR);
  34. }
  35. dwStrSize += (wcslen(pszModuleName) + 1) * sizeof(WCHAR);
  36. dwAllocSize = sizeof(SE_ADT_PARAMETER_ARRAY) +
  37. dwParamCnt * sizeof(UNICODE_STRING) + dwStrSize;
  38. dwAllocSize += PtrAlignSize(dwSidLength);
  39. if (dwAllocSize > MAX_AUDIT_BUFFER) {
  40. return (ERROR_BUFFER_OVERFLOW);
  41. }
  42. pParArray = (SE_ADT_PARAMETER_ARRAY *) gpszAuditBuffer;
  43. pParArray->CategoryId = dwCategoryId;
  44. pParArray->AuditId = dwAuditId;
  45. pParArray->ParameterCount = dwParamCnt + 2;
  46. pParArray->Length = dwAllocSize;
  47. pParArray->Flags = 0;
  48. if (bSuccess) {
  49. pParArray->Type = EVENTLOG_AUDIT_SUCCESS;
  50. }
  51. else {
  52. pParArray->Type = EVENTLOG_AUDIT_FAILURE;
  53. }
  54. pbyteCurAddr = (BYTE *) (pParArray + 1);
  55. pParArray->Parameters[0].Type = SeAdtParmTypeSid;
  56. pParArray->Parameters[0].Length = dwSidLength;
  57. pParArray->Parameters[0].Data[0] = 0;
  58. pParArray->Parameters[0].Data[1] = 0;
  59. pParArray->Parameters[0].Address = pSid;
  60. memcpy((BYTE *) pbyteCurAddr, (BYTE *) pSid, dwSidLength);
  61. pbyteCurAddr = (BYTE *) pbyteCurAddr + PtrAlignSize(dwSidLength);
  62. pusStrArray = (UNICODE_STRING *) pbyteCurAddr;
  63. pusStrArray[0].Length = wcslen(pszModuleName) * sizeof(WCHAR);
  64. pusStrArray[0].MaximumLength = pusStrArray[0].Length + sizeof(WCHAR);
  65. pusStrArray[0].Buffer = (LPWSTR) pszModuleName;
  66. pParArray->Parameters[1].Type = SeAdtParmTypeString;
  67. pParArray->Parameters[1].Length = sizeof(UNICODE_STRING) +
  68. pusStrArray[0].MaximumLength;
  69. pParArray->Parameters[1].Data[0] = 0;
  70. pParArray->Parameters[1].Data[1] = 0;
  71. pParArray->Parameters[1].Address = (PVOID) &pusStrArray[0];
  72. for (i = 0; i < dwParamCnt; i++) {
  73. pusStrArray[i+1].Length = wcslen(ppszArgArray[i]) * sizeof(WCHAR);
  74. pusStrArray[i+1].MaximumLength = pusStrArray[i+1].Length + sizeof(WCHAR);
  75. pusStrArray[i+1].Buffer = (LPWSTR) ppszArgArray[i];
  76. pParArray->Parameters[i+2].Type = SeAdtParmTypeString;
  77. pParArray->Parameters[i+2].Length = sizeof(UNICODE_STRING) +
  78. pusStrArray[i+1].MaximumLength;
  79. pParArray->Parameters[i+2].Data[0] = 0;
  80. pParArray->Parameters[i+2].Data[1] = 0;
  81. pParArray->Parameters[i+2].Address = (PVOID) &pusStrArray[i+1];
  82. }
  83. if (bDoAudit) {
  84. ntStatus = LsaIWriteAuditEvent(pParArray, 0);
  85. }
  86. return (ERROR_SUCCESS);
  87. }
  88. VOID
  89. AuditEvent(
  90. DWORD dwCategoryId,
  91. DWORD dwAuditId,
  92. DWORD dwStrId,
  93. LPWSTR * ppszArguments,
  94. BOOL bSuccess,
  95. BOOL bDoAudit
  96. )
  97. {
  98. DWORD dwError = 0;
  99. LPWSTR pszArgArray[3];
  100. DWORD dwParamCnt = 0;
  101. EnterCriticalSection(&gcSPDAuditSection);
  102. dwError = FormatMessage(
  103. FORMAT_MESSAGE_FROM_HMODULE |
  104. FORMAT_MESSAGE_ARGUMENT_ARRAY,
  105. ghIpsecServerModule,
  106. dwStrId,
  107. LANG_NEUTRAL,
  108. gpszAuditMsgBuffer,
  109. MAX_MSG_BUFFER,
  110. (va_list *) ppszArguments
  111. );
  112. if (dwError == 0) {
  113. wsprintf(
  114. gpszAuditMsgBuffer,
  115. L"IPSec Services encountered an error while auditing event ID 0x%x",
  116. dwStrId
  117. );
  118. }
  119. gpszAuditMsgBuffer[MAX_MSG_BUFFER - 1] = 0;
  120. if (dwError != 0) {
  121. switch (dwAuditId) {
  122. case SE_AUDITID_IPSEC_POLICY_CHANGED:
  123. dwParamCnt = 1;
  124. pszArgArray[0] = (LPWSTR) gpszAuditMsgBuffer;
  125. break;
  126. default:
  127. LeaveCriticalSection(&gcSPDAuditSection);
  128. return;
  129. }
  130. (VOID) PerformAudit(
  131. dwCategoryId,
  132. dwAuditId,
  133. gpIpsecServerSid,
  134. dwParamCnt,
  135. (LPWSTR *) pszArgArray,
  136. bSuccess,
  137. bDoAudit
  138. );
  139. }
  140. LeaveCriticalSection(&gcSPDAuditSection);
  141. return;
  142. }
  143. VOID
  144. AuditOneArgErrorEvent(
  145. DWORD dwCategoryId,
  146. DWORD dwAuditId,
  147. DWORD dwStrId,
  148. DWORD dwErrorCode,
  149. BOOL bSuccess,
  150. BOOL bDoAudit
  151. )
  152. {
  153. DWORD dwError = 0;
  154. LPVOID lpvMsgBuf = NULL;
  155. WCHAR szAuditLocalMsgBuffer[MAX_PATH];
  156. WCHAR * pszAuditLocalMsgBuffer = szAuditLocalMsgBuffer;
  157. szAuditLocalMsgBuffer[0] = L'\0';
  158. dwError = FormatMessage(
  159. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  160. FORMAT_MESSAGE_FROM_SYSTEM |
  161. FORMAT_MESSAGE_IGNORE_INSERTS,
  162. NULL,
  163. dwErrorCode,
  164. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  165. (LPWSTR) &lpvMsgBuf,
  166. 0,
  167. NULL
  168. );
  169. if (!dwError) {
  170. wsprintf(
  171. pszAuditLocalMsgBuffer,
  172. L"0x%x",
  173. dwErrorCode
  174. );
  175. AuditEvent(
  176. dwCategoryId,
  177. dwAuditId,
  178. dwStrId,
  179. (LPWSTR *) &pszAuditLocalMsgBuffer,
  180. bSuccess,
  181. bDoAudit
  182. );
  183. return;
  184. }
  185. AuditEvent(
  186. dwCategoryId,
  187. dwAuditId,
  188. dwStrId,
  189. (LPWSTR *) &lpvMsgBuf,
  190. bSuccess,
  191. bDoAudit
  192. );
  193. if (lpvMsgBuf) {
  194. LocalFree(lpvMsgBuf);
  195. }
  196. return;
  197. }
  198. VOID
  199. AuditIPSecPolicyEvent(
  200. DWORD dwCategoryId,
  201. DWORD dwAuditId,
  202. DWORD dwStrId,
  203. LPWSTR pszPolicyName,
  204. BOOL bSuccess,
  205. BOOL bDoAudit
  206. )
  207. {
  208. WCHAR szAuditLocalMsgBuffer[MAX_PATH];
  209. WCHAR * pszAuditLocalMsgBuffer = szAuditLocalMsgBuffer;
  210. szAuditLocalMsgBuffer[0] = L'\0';
  211. wsprintf(pszAuditLocalMsgBuffer, L"%s", pszPolicyName);
  212. AuditEvent(
  213. dwCategoryId,
  214. dwAuditId,
  215. dwStrId,
  216. (LPWSTR *) &pszAuditLocalMsgBuffer,
  217. bSuccess,
  218. bDoAudit
  219. );
  220. return;
  221. }
  222. VOID
  223. AuditIPSecPolicyErrorEvent(
  224. DWORD dwCategoryId,
  225. DWORD dwAuditId,
  226. DWORD dwStrId,
  227. LPWSTR pszPolicyName,
  228. DWORD dwErrorCode,
  229. BOOL bSuccess,
  230. BOOL bDoAudit
  231. )
  232. {
  233. DWORD dwError = 0;
  234. WCHAR szAuditPolicyMsgBuffer[MAX_PATH];
  235. WCHAR * pszAuditPolicyMsgBuffer = szAuditPolicyMsgBuffer;
  236. WCHAR szAuditErrorMsgBuffer[MAX_PATH];
  237. WCHAR * pszAuditErrorMsgBuffer = szAuditErrorMsgBuffer;
  238. LPWSTR pszArgArray[2];
  239. LPWSTR * ppszArgArray = pszArgArray;
  240. LPVOID lpvMsgBuf = NULL;
  241. szAuditPolicyMsgBuffer[0] = L'\0';
  242. szAuditErrorMsgBuffer[0] = L'\0';
  243. wsprintf(pszAuditPolicyMsgBuffer, L"%s", pszPolicyName);
  244. dwError = FormatMessage(
  245. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  246. FORMAT_MESSAGE_FROM_SYSTEM |
  247. FORMAT_MESSAGE_IGNORE_INSERTS,
  248. NULL,
  249. dwErrorCode,
  250. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  251. (LPWSTR) &lpvMsgBuf,
  252. 0,
  253. NULL
  254. );
  255. if (!dwError) {
  256. wsprintf(
  257. pszAuditErrorMsgBuffer,
  258. L"0x%x",
  259. dwErrorCode
  260. );
  261. pszArgArray[0] = pszAuditPolicyMsgBuffer;
  262. pszArgArray[1] = pszAuditErrorMsgBuffer;
  263. AuditEvent(
  264. dwCategoryId,
  265. dwAuditId,
  266. dwStrId,
  267. (LPWSTR *) ppszArgArray,
  268. bSuccess,
  269. bDoAudit
  270. );
  271. return;
  272. }
  273. pszArgArray[0] = pszAuditPolicyMsgBuffer;
  274. pszArgArray[1] = (LPWSTR) lpvMsgBuf;
  275. AuditEvent(
  276. dwCategoryId,
  277. dwAuditId,
  278. dwStrId,
  279. (LPWSTR *) ppszArgArray,
  280. bSuccess,
  281. bDoAudit
  282. );
  283. if (lpvMsgBuf) {
  284. LocalFree(lpvMsgBuf);
  285. }
  286. return;
  287. }