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.

317 lines
9.8 KiB

  1. /*++
  2. Copyright (c) 1991-1992 Microsoft Corporation
  3. Module Name:
  4. AudArray.c
  5. Abstract:
  6. This file contains RxpConvertAuditArray.
  7. Author:
  8. John Rogers (JohnRo) 05-Nov-1991
  9. Environment:
  10. Portable to any flat, 32-bit environment. (Uses Win32 typedefs.)
  11. Requires ANSI C extensions: slash-slash comments, long external names.
  12. Notes:
  13. The logic in ErrConv.c is based on the logic in AudArray.c.
  14. Make sure that you check both files if you find a bug in either.
  15. Revision History:
  16. 05-Nov-1991 JohnRo
  17. Created.
  18. 08-Nov-1991 JohnRo
  19. Fix array increment bug. Fixed wrong inp var ptr being passed to
  20. convert variable data.
  21. 22-Nov-1991 JohnRo
  22. Set ae_data_size field.
  23. 07-Feb-1992 JohnRo
  24. Use NetApiBufferAllocate() instead of private version.
  25. 16-Jun-1992 JohnRo
  26. RAID 10311: NetAuditRead and NetErrorLogRead pointer arithmetic wrong.
  27. Use PREFIX_ equates.
  28. 07-Jul-1992 JohnRo
  29. RAID 9933: ALIGN_WORST should be 8 for x86 builds.
  30. 17-Aug-1992 JohnRo
  31. RAID 2920: Support UTC timezone in net code.
  32. 01-Oct-1992 JohnRo
  33. RAID 3556: Added NetpSystemTimeToGmtTime() for DosPrint APIs.
  34. 30-Oct-1992 JohnRo
  35. RAID 10218: Fixed potential trash of output record for unknown entries.
  36. --*/
  37. // These must be included first:
  38. #include <windows.h> // IN, LPTSTR, etc.
  39. #include <lmcons.h> // DEVLEN, NET_API_STATUS, etc.
  40. #include <lmaudit.h> // Needed by rxaudit.h
  41. // These may be included in any order:
  42. #include <align.h> // ALIGN_ and ROUND_ equates.
  43. #include <lmapibuf.h> // NetApiBufferAllocate().
  44. #include <lmerr.h> // ERROR_ and NERR_ equates.
  45. #include <netdebug.h> // NetpKdPrint(()), FORMAT_ equates.
  46. #include <prefix.h> // PREFIX_ equates.
  47. #include <rxaudit.h> // RxpAudit routine prototypes.
  48. #include <rxp.h> // RxpEstimateLogSize().
  49. #include <rxpdebug.h> // IF_DEBUG().
  50. #include <smbgtpt.h> // Smb{Get,Put} macros.
  51. #include <timelib.h> // NetpLocalTimeToGmtTime().
  52. #define DOWNLEVEL_AUDIT_FIXED_ENTRY_SIZE \
  53. ( 2 /* ae_len */ \
  54. + 2 /* ae_reserved */ \
  55. + 4 /* ae_time */ \
  56. + 2 /* ae_type */ \
  57. + 2 ) /* ae_data_offset */
  58. #define MIN_DOWNLEVEL_ENTRY_SIZE \
  59. ( DOWNLEVEL_AUDIT_FIXED_ENTRY_SIZE \
  60. + 2 /* min variable data size */ \
  61. + 2 ) /* ae_len2 */
  62. NET_API_STATUS
  63. RxpConvertAuditArray(
  64. IN LPVOID InputArray,
  65. IN DWORD InputByteCount,
  66. OUT LPBYTE * OutputArrayPtr, // will be alloc'ed (free w/ NetApiBufferFree).
  67. OUT LPDWORD OutputByteCountPtr
  68. )
  69. {
  70. DWORD EntryType;
  71. const LPBYTE InputArrayEndPtr
  72. = (LPVOID) ( ((LPBYTE)InputArray) + InputByteCount );
  73. LPBYTE InputBytePtr;
  74. DWORD InputDataOffset;
  75. DWORD InputTotalEntrySize;
  76. LPBYTE InputFixedPtr;
  77. LPBYTE InputVariablePtr;
  78. DWORD InputVariableSize;
  79. LPVOID OutputArray;
  80. DWORD OutputArraySize;
  81. DWORD OutputBytesUsed = 0;
  82. DWORD OutputEntrySizeSoFar;
  83. LPAUDIT_ENTRY OutputFixedPtr;
  84. DWORD OutputVariableSize;
  85. LPBYTE OutputVariablePtr;
  86. NET_API_STATUS Status;
  87. //
  88. // Error check caller's parameters.
  89. // Set output parameters to make error handling easier below.
  90. // (Also check for memory faults while we're at it.)
  91. //
  92. if (OutputArrayPtr != NULL) {
  93. *OutputArrayPtr = NULL;
  94. }
  95. if (OutputByteCountPtr != NULL) {
  96. *OutputByteCountPtr = 0;
  97. }
  98. if ( (OutputArrayPtr == NULL) || (OutputByteCountPtr == NULL) ) {
  99. return (ERROR_INVALID_PARAMETER);
  100. }
  101. if ( (InputArray == NULL) || (InputByteCount == 0) ) {
  102. return (ERROR_INVALID_PARAMETER);
  103. }
  104. //
  105. // Compute size needed for output buffer, taking into account:
  106. // per field expansion,
  107. // per entry expansion,
  108. // and alignment.
  109. //
  110. Status = RxpEstimateLogSize(
  111. DOWNLEVEL_AUDIT_FIXED_ENTRY_SIZE,
  112. InputByteCount, // input (downlevel) array size in bytes.
  113. FALSE, // no, we're not doing error log
  114. & OutputArraySize); // set estimated array size in bytes.
  115. if (Status != NO_ERROR) {
  116. return (Status); // (output vars are already set.)
  117. }
  118. NetpAssert( OutputArraySize > 0 );
  119. NetpAssert( OutputArraySize > InputByteCount );
  120. *OutputByteCountPtr = OutputArraySize;
  121. //
  122. // Allocate oversize area for output; we'll realloc it to shrink it.
  123. //
  124. Status = NetApiBufferAllocate(
  125. OutputArraySize,
  126. (LPVOID *) & OutputArray );
  127. if (Status != NERR_Success) {
  128. return (Status); // (output vars are already set.)
  129. }
  130. NetpAssert( OutputArray != NULL );
  131. NetpAssert( POINTER_IS_ALIGNED( OutputArray, ALIGN_WORST ) );
  132. //
  133. // Loop for each entry in the input area.
  134. //
  135. OutputFixedPtr = OutputArray;
  136. for (InputBytePtr = InputArray; InputBytePtr < InputArrayEndPtr; ) {
  137. InputFixedPtr = InputBytePtr;
  138. NetpAssert( POINTER_IS_ALIGNED( OutputFixedPtr, ALIGN_WORST ) );
  139. IF_DEBUG(AUDIT) {
  140. NetpKdPrint(( PREFIX_NETLIB
  141. "RxpConvertAuditArray: doing input entry at "
  142. FORMAT_LPVOID ", out entry at " FORMAT_LPVOID ".\n",
  143. (LPVOID) InputFixedPtr, (LPVOID) OutputFixedPtr ));
  144. }
  145. //
  146. // Process each field in input fixed entry.
  147. //
  148. InputTotalEntrySize = (DWORD) SmbGetUshort( (LPWORD) InputBytePtr );
  149. if (InputTotalEntrySize < MIN_DOWNLEVEL_ENTRY_SIZE) {
  150. goto FileCorrupt;
  151. }
  152. {
  153. LPBYTE EndPos = InputBytePtr + InputTotalEntrySize;
  154. if (EndPos > InputArrayEndPtr) {
  155. goto FileCorrupt;
  156. }
  157. EndPos -= sizeof(WORD); // the last ae_len2
  158. if (SmbGetUshort( (LPWORD) EndPos ) != InputTotalEntrySize) {
  159. goto FileCorrupt;
  160. }
  161. }
  162. InputBytePtr += sizeof(WORD); // skip ae_len.
  163. OutputFixedPtr->ae_reserved =
  164. (DWORD) SmbGetUshort( (LPWORD) InputBytePtr );
  165. InputBytePtr += sizeof(WORD); // skip ae_reserved
  166. {
  167. DWORD LocalTime = (DWORD) SmbGetUlong( (LPDWORD) InputBytePtr );
  168. DWORD GmtTime;
  169. NetpLocalTimeToGmtTime( LocalTime, & GmtTime );
  170. OutputFixedPtr->ae_time = GmtTime;
  171. InputBytePtr += sizeof(DWORD);
  172. }
  173. EntryType = (DWORD) SmbGetUshort( (LPWORD) InputBytePtr );
  174. OutputFixedPtr->ae_type = EntryType;
  175. InputBytePtr += sizeof(WORD);
  176. InputDataOffset = (DWORD) SmbGetUshort( (LPWORD) InputBytePtr );
  177. NetpAssert( InputDataOffset >= DOWNLEVEL_AUDIT_FIXED_ENTRY_SIZE );
  178. InputBytePtr += sizeof(WORD);
  179. OutputEntrySizeSoFar = sizeof(AUDIT_ENTRY);
  180. //
  181. // Process variable portion (if any):
  182. //
  183. InputVariablePtr = (LPVOID)
  184. ( ((LPBYTE) InputFixedPtr) + InputDataOffset );
  185. InputVariableSize =
  186. (InputTotalEntrySize - InputDataOffset)
  187. - sizeof(WORD); // don't include ae_len2.
  188. OutputVariablePtr = (LPVOID)
  189. ( ((LPBYTE) OutputFixedPtr) + sizeof(AUDIT_ENTRY) );
  190. // Align variable part.
  191. OutputVariablePtr = ROUND_UP_POINTER( OutputVariablePtr, ALIGN_WORST );
  192. OutputEntrySizeSoFar =
  193. ROUND_UP_COUNT( OutputEntrySizeSoFar, ALIGN_WORST );
  194. OutputFixedPtr->ae_data_offset = OutputEntrySizeSoFar;
  195. // Copy and convert the variable part.
  196. RxpConvertAuditEntryVariableData(
  197. EntryType,
  198. InputVariablePtr,
  199. OutputVariablePtr,
  200. InputVariableSize,
  201. & OutputVariableSize);
  202. #ifdef REVISED_AUDIT_ENTRY_STRUCT
  203. OutputFixedPtr->ae_data_size = OutputVariableSize;
  204. #endif
  205. // Account for variable area and ae_len2 in total length.
  206. OutputEntrySizeSoFar += (OutputVariableSize + sizeof(DWORD));
  207. // Round size up so next entry (if any) is worst-case aligned.
  208. OutputEntrySizeSoFar =
  209. ROUND_UP_COUNT( OutputEntrySizeSoFar, ALIGN_WORST );
  210. #define OutputEntrySize OutputEntrySizeSoFar
  211. OutputFixedPtr->ae_len = OutputEntrySize;
  212. {
  213. LPDWORD EndSizePtr = (LPVOID)
  214. ( ((LPBYTE)OutputFixedPtr)
  215. + OutputEntrySize - sizeof(DWORD) );
  216. *EndSizePtr = OutputEntrySize; // set ae_len2.
  217. }
  218. //
  219. // Update for next loop iteration.
  220. //
  221. InputBytePtr = (LPVOID)
  222. ( ((LPBYTE) InputFixedPtr)
  223. + InputTotalEntrySize);
  224. OutputFixedPtr = (LPVOID)
  225. ( ((LPBYTE) OutputFixedPtr) + OutputEntrySize );
  226. OutputBytesUsed += OutputEntrySize;
  227. NetpAssert( OutputBytesUsed <= OutputArraySize );
  228. }
  229. NetpAssert(OutputBytesUsed > 0);
  230. NetpAssert( OutputBytesUsed <= OutputArraySize );
  231. *OutputArrayPtr = OutputArray;
  232. *OutputByteCountPtr = OutputBytesUsed;
  233. return (NERR_Success);
  234. FileCorrupt:
  235. NetpKdPrint(( PREFIX_NETAPI
  236. "RxpConvertAuditArray: corrupt audit log!\n" ));
  237. if (OutputArray != NULL) {
  238. (VOID) NetApiBufferFree( OutputArray );
  239. }
  240. if (OutputArrayPtr != NULL) {
  241. *OutputArrayPtr = NULL;
  242. }
  243. if (OutputByteCountPtr != NULL) {
  244. *OutputByteCountPtr = 0;
  245. }
  246. return (NERR_LogFileCorrupt);
  247. }