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.

228 lines
5.5 KiB

  1. //*************************************************************
  2. //
  3. // Copyright (c) Microsoft Corporation 1998
  4. // All rights reserved
  5. //
  6. // events.cxx
  7. //
  8. //*************************************************************
  9. #include "fdeploy.hxx"
  10. CEvents * gpEvents = 0;
  11. CEvents::CEvents()
  12. {
  13. _hEventLog = NULL;
  14. _pUserSid = NULL;
  15. _Refs = 0;
  16. }
  17. CEvents::~CEvents()
  18. {
  19. if ( _hEventLog )
  20. CloseEventLog( _hEventLog );
  21. if ( _pUserSid )
  22. LocalFree( _pUserSid );
  23. }
  24. DWORD
  25. CEvents::Init()
  26. {
  27. DWORD Size;
  28. DWORD Type;
  29. DWORD Status;
  30. HKEY hKey;
  31. Status = ERROR_SUCCESS;
  32. if ( ! _hEventLog )
  33. _hEventLog = OpenEventLog( NULL, FDEPLOY_EVENT_SOURCE );
  34. if ( ! _hEventLog )
  35. return GetLastError();
  36. return ERROR_SUCCESS;
  37. }
  38. PSID
  39. CEvents::UserSid()
  40. {
  41. GetUserSid();
  42. // The caller does not own this sid and should not attempt to free it
  43. return _pUserSid;
  44. }
  45. void
  46. CEvents::GetUserSid()
  47. {
  48. DWORD Size;
  49. DWORD Status;
  50. HANDLE hToken;
  51. PTOKEN_USER pTokenUserData;
  52. BOOL bStatus;
  53. if ( _pUserSid )
  54. return;
  55. bStatus = OpenThreadToken( GetCurrentThread(), TOKEN_QUERY, TRUE, &hToken );
  56. if ( ! bStatus )
  57. return;
  58. Size = sizeof(TOKEN_USER) + sizeof(SID) + ((SID_MAX_SUB_AUTHORITIES-1) * sizeof(ULONG));
  59. __try
  60. {
  61. pTokenUserData = (PTOKEN_USER) alloca( Size );
  62. }
  63. __except(GetExceptionCode() == STATUS_STACK_OVERFLOW)
  64. {
  65. _resetstkoflw();
  66. pTokenUserData = NULL;
  67. CloseHandle( hToken );
  68. return;
  69. }
  70. bStatus = GetTokenInformation(
  71. hToken,
  72. TokenUser,
  73. pTokenUserData,
  74. Size,
  75. &Size );
  76. CloseHandle( hToken );
  77. if ( ! bStatus )
  78. return;
  79. Size = GetLengthSid( pTokenUserData->User.Sid );
  80. _pUserSid = (PSID) LocalAlloc( 0, Size );
  81. if ( _pUserSid )
  82. {
  83. bStatus = CopySid( Size, _pUserSid, pTokenUserData->User.Sid );
  84. if ( ! bStatus )
  85. {
  86. LocalFree( _pUserSid );
  87. _pUserSid = NULL;
  88. }
  89. }
  90. }
  91. void
  92. CEvents::Report(
  93. DWORD EventID,
  94. WORD Strings,
  95. ...
  96. )
  97. {
  98. va_list VAList;
  99. WCHAR ** ppwszStrings;
  100. WORD Type;
  101. DWORD DbgMsgLevel;
  102. switch ( EventID >> 30 )
  103. {
  104. case 3:
  105. Type = EVENTLOG_ERROR_TYPE;
  106. DbgMsgLevel = DM_WARNING;
  107. break;
  108. case 2:
  109. Type = EVENTLOG_WARNING_TYPE;
  110. DbgMsgLevel = DM_VERBOSE;
  111. break;
  112. case 1:
  113. case 0:
  114. Type = EVENTLOG_INFORMATION_TYPE;
  115. DbgMsgLevel = DM_VERBOSE;
  116. break;
  117. default:
  118. return;
  119. }
  120. ppwszStrings = 0;
  121. if ( Strings > 0 )
  122. {
  123. __try
  124. {
  125. ppwszStrings = (WCHAR **) alloca( Strings * sizeof(WCHAR *) );
  126. }
  127. __except(GetExceptionCode() == STATUS_STACK_OVERFLOW)
  128. {
  129. _resetstkoflw();
  130. return;
  131. }
  132. va_start( VAList, Strings );
  133. for ( DWORD n = 0; n < Strings; n++ )
  134. ppwszStrings[n] = va_arg( VAList, WCHAR * );
  135. va_end( VAList );
  136. }
  137. GetUserSid();
  138. (void) ReportEvent(
  139. _hEventLog,
  140. Type,
  141. 0,
  142. EventID,
  143. _pUserSid,
  144. Strings,
  145. 0,
  146. (LPCWCH *) ppwszStrings,
  147. NULL );
  148. //
  149. // Also make sure the event messages get sent to the debugger and log file.
  150. // Kind of hacky method, but makes it so every caller to ::Report doesn't
  151. // have to call _DebugMsg as well.
  152. // However, don't do this for the verbose messages otherwise it will be
  153. // dumped to the debugger twice.
  154. //
  155. if ( EVENT_FDEPLOY_VERBOSE == EventID )
  156. return;
  157. switch ( Strings )
  158. {
  159. case 0 :
  160. _DebugMsg( DbgMsgLevel | DM_NO_EVENTLOG, EventID );
  161. break;
  162. case 1 :
  163. _DebugMsg( DbgMsgLevel | DM_NO_EVENTLOG, EventID, ppwszStrings[0] );
  164. break;
  165. case 2 :
  166. _DebugMsg( DbgMsgLevel | DM_NO_EVENTLOG, EventID, ppwszStrings[0], ppwszStrings[1] );
  167. break;
  168. case 3 :
  169. _DebugMsg( DbgMsgLevel | DM_NO_EVENTLOG, EventID, ppwszStrings[0], ppwszStrings[1], ppwszStrings[2] );
  170. break;
  171. case 4 :
  172. _DebugMsg( DbgMsgLevel | DM_NO_EVENTLOG, EventID, ppwszStrings[0], ppwszStrings[1], ppwszStrings[2], ppwszStrings[3] );
  173. break;
  174. case 5 :
  175. _DebugMsg( DbgMsgLevel | DM_NO_EVENTLOG, EventID, ppwszStrings[0],
  176. ppwszStrings[1], ppwszStrings[2], ppwszStrings[3],
  177. ppwszStrings[4]);
  178. break;
  179. case 6 :
  180. _DebugMsg( DbgMsgLevel | DM_NO_EVENTLOG, EventID, ppwszStrings[0],
  181. ppwszStrings[1], ppwszStrings[2], ppwszStrings[3],
  182. ppwszStrings[4], ppwszStrings[5]);
  183. break;
  184. case 7 :
  185. _DebugMsg( DbgMsgLevel | DM_NO_EVENTLOG, EventID, ppwszStrings[0],
  186. ppwszStrings[1], ppwszStrings[2], ppwszStrings[3],
  187. ppwszStrings[4], ppwszStrings[5], ppwszStrings[6]);
  188. break;
  189. default :
  190. VerboseDebugDump( L"CEvents::Report called with more params then expected\n" );
  191. break;
  192. }
  193. }