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.

242 lines
6.0 KiB

  1. //--------------------------------------------------------------------
  2. // Copyright (C) Microsoft Corporation, 1999 - 2002, All Rights Reserved
  3. //
  4. // eventlog.cpp
  5. //
  6. // Implementation of a simple event logging class.
  7. //
  8. //--------------------------------------------------------------------
  9. #include "stdafx.h"
  10. #include "resource.h"
  11. #if !defined(BITS_V12_ON_NT4)
  12. #include "eventlog.tmh"
  13. #endif
  14. //--------------------------------------------------------------------
  15. // EVENT_LOG::EVENT_LOG()
  16. //
  17. //--------------------------------------------------------------------
  18. EVENT_LOG::EVENT_LOG()
  19. : m_OwnerString( new WCHAR[USER_NAME_LENGTH] ),
  20. m_UserString( new WCHAR[USER_NAME_LENGTH] )
  21. {
  22. m_hEventLog = RegisterEventSourceW( NULL, WS_EVENT_SOURCE );
  23. if (!m_hEventLog)
  24. {
  25. ThrowLastError();
  26. }
  27. }
  28. //--------------------------------------------------------------------
  29. // EVENT_LOG:;~EVENT_LOG()
  30. //
  31. //--------------------------------------------------------------------
  32. EVENT_LOG::~EVENT_LOG()
  33. {
  34. if (m_hEventLog)
  35. {
  36. DeregisterEventSource(m_hEventLog);
  37. }
  38. delete m_OwnerString;
  39. delete m_UserString;
  40. }
  41. HRESULT EVENT_LOG::ReportGenericJobChange(
  42. GUID & Id,
  43. LPCWSTR Title,
  44. SidHandle Owner,
  45. SidHandle User,
  46. DWORD EventType
  47. )
  48. /*
  49. This is a helper routine for a couple of different events that use the same insertion strings.
  50. Currently there are two, for job cancellation and for take-ownership.
  51. <EventType> should be the event ID from the .MC file. This fn doesn't verify that
  52. the event in question expects these insertion strings.
  53. */
  54. {
  55. GUIDSTR GuidString;
  56. StringFromGUID2( Id, GuidString, RTL_NUMBER_OF( GuidString ));
  57. SidToUser( Owner.get(), m_OwnerString, USER_NAME_LENGTH );
  58. SidToUser( User.get(), m_UserString, USER_NAME_LENGTH );
  59. //
  60. LPCWSTR Strings[4];
  61. Strings[0] = GuidString;
  62. Strings[1] = Title;
  63. Strings[2] = m_OwnerString;
  64. Strings[3] = m_UserString;
  65. BOOL b;
  66. b = ReportEvent(
  67. m_hEventLog,
  68. EVENTLOG_INFORMATION_TYPE,
  69. 0, // no category
  70. EventType,
  71. NULL, // no user
  72. RTL_NUMBER_OF(Strings),
  73. 0, // no additional data
  74. Strings,
  75. NULL // no additional data
  76. );
  77. if (!b)
  78. {
  79. DWORD s = GetLastError();
  80. LogError("unable to log job-change event (%x) %!winerr!", EventType, s);
  81. return HRESULT_FROM_WIN32( s );
  82. }
  83. return S_OK;
  84. }
  85. HRESULT EVENT_LOG::ReportFileDeletionFailure(
  86. GUID & Id,
  87. LPCWSTR Title,
  88. LPCWSTR FileList,
  89. bool fMoreFiles
  90. )
  91. {
  92. GUIDSTR GuidString;
  93. StringFromGUID2( Id, GuidString, RTL_NUMBER_OF( GuidString ));
  94. LPCWSTR Strings[3];
  95. Strings[0] = GuidString;
  96. Strings[1] = Title;
  97. Strings[2] = FileList;
  98. BOOL b;
  99. b = ReportEvent(
  100. m_hEventLog,
  101. EVENTLOG_WARNING_TYPE,
  102. 0, // no category
  103. fMoreFiles ? MC_FILE_DELETION_FAILED_MORE : MC_FILE_DELETION_FAILED,
  104. NULL, // no user
  105. RTL_NUMBER_OF(Strings),
  106. 0, // no additional data
  107. Strings,
  108. NULL // no additional data
  109. );
  110. if (!b)
  111. {
  112. DWORD s = GetLastError();
  113. LogError("unable to log file-deletion-failure event %!winerr!", s);
  114. return HRESULT_FROM_WIN32( s );
  115. }
  116. return S_OK;
  117. }
  118. HRESULT EVENT_LOG::ReportStateFileCleared()
  119. {
  120. BOOL b;
  121. b = ReportEvent(
  122. m_hEventLog,
  123. EVENTLOG_ERROR_TYPE,
  124. 0, // no category
  125. MC_STATE_FILE_CORRUPT,
  126. NULL, // no user
  127. 0, // no plug-in strings
  128. 0, // no additional data
  129. NULL, // no plug-in strings
  130. NULL // no additional data
  131. );
  132. if (!b)
  133. {
  134. DWORD s = GetLastError();
  135. LogError("unable to log state-file-cleared event %!winerr!", s);
  136. return HRESULT_FROM_WIN32( s );
  137. }
  138. return S_OK;
  139. }
  140. HRESULT EVENT_LOG::SidToUser( PSID Sid, LPWSTR Name, size_t Length )
  141. {
  142. DWORD s;
  143. DWORD NameLength = 0;
  144. DWORD DomainLength = 0;
  145. SID_NAME_USE Use;
  146. //
  147. // Determine the usern-name and domain-name lengths.
  148. //
  149. LookupAccountSid( NULL, // default lookup spaces
  150. Sid,
  151. NULL,
  152. &NameLength,
  153. NULL,
  154. &DomainLength,
  155. &Use
  156. );
  157. s = GetLastError();
  158. if (s == ERROR_NONE_MAPPED)
  159. {
  160. return GetUnknownUserName( Name, Length );
  161. }
  162. if (s != ERROR_INSUFFICIENT_BUFFER)
  163. {
  164. LogError("LookupAccountSid #1 failed %!winerr!", s);
  165. return HRESULT_FROM_WIN32(s);
  166. }
  167. if (NameLength + DomainLength > Length)
  168. {
  169. return HRESULT_FROM_WIN32( ERROR_INSUFFICIENT_BUFFER );
  170. }
  171. //
  172. // Capture the user-name and domain-name.
  173. //
  174. NameLength = Length - DomainLength;
  175. if (!LookupAccountSid( NULL,
  176. Sid,
  177. Name + DomainLength,
  178. &NameLength,
  179. Name,
  180. &DomainLength,
  181. &Use ))
  182. {
  183. s = GetLastError();
  184. LogError("LookupAccountSid #2 failed %!winerr!", s);
  185. return HRESULT_FROM_WIN32(s);
  186. }
  187. //
  188. // The domain and user name are separated by a NULL instead of a backslash; fix that.
  189. //
  190. Name[wcslen(Name)] = '\\';
  191. return S_OK;
  192. }
  193. HRESULT
  194. EVENT_LOG::GetUnknownUserName(
  195. WCHAR Name[],
  196. size_t Length
  197. )
  198. {
  199. DWORD s;
  200. if (!LoadString( g_hInstance, IDS_UNKNOWN_USER, Name, Length ))
  201. {
  202. s = GetLastError();
  203. LogError("LoadString failed %!winerr!", s);
  204. return HRESULT_FROM_WIN32(s);
  205. }
  206. return S_OK;
  207. }