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.

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