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.

542 lines
15 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997.
  5. //
  6. // File: events.cxx
  7. //
  8. // Contents:
  9. //
  10. // History: ?-??-?? ??? Created
  11. // 6-17-99 a-sergiv Added event filtering
  12. //
  13. //--------------------------------------------------------------------------
  14. #include "act.hxx"
  15. BOOL
  16. GetTextualSid(
  17. PSID pSid, // binary Sid
  18. LPTSTR TextualSid, // buffer for Textual representaion of Sid
  19. LPDWORD cchSidSize // required/provided TextualSid buffersize
  20. )
  21. {
  22. PSID_IDENTIFIER_AUTHORITY psia;
  23. DWORD dwSubAuthorities;
  24. DWORD dwCounter;
  25. DWORD cchSidCopy;
  26. //
  27. // test if Sid passed in is valid
  28. //
  29. if(!IsValidSid(pSid)) return FALSE;
  30. // obtain SidIdentifierAuthority
  31. psia = GetSidIdentifierAuthority(pSid);
  32. // obtain sidsubauthority count
  33. dwSubAuthorities = *GetSidSubAuthorityCount(pSid);
  34. //
  35. // compute approximate buffer length
  36. // S-SID_REVISION- + identifierauthority- + subauthorities- + NULL
  37. //
  38. cchSidCopy = (15 + 12 + (12 * dwSubAuthorities) + 1) * sizeof(TCHAR);
  39. //
  40. // check provided buffer length.
  41. // If not large enough, indicate proper size and setlasterror
  42. //
  43. if(*cchSidSize < cchSidCopy) {
  44. *cchSidSize = cchSidCopy;
  45. SetLastError(ERROR_INSUFFICIENT_BUFFER);
  46. return FALSE;
  47. }
  48. //
  49. // prepare S-SID_REVISION-
  50. //
  51. cchSidCopy = wsprintf(TextualSid, TEXT("S-%lu-"), SID_REVISION );
  52. //
  53. // prepare SidIdentifierAuthority
  54. //
  55. if ( (psia->Value[0] != 0) || (psia->Value[1] != 0) ) {
  56. cchSidCopy += wsprintf(TextualSid + cchSidCopy,
  57. TEXT("0x%02hx%02hx%02hx%02hx%02hx%02hx"),
  58. (USHORT)psia->Value[0],
  59. (USHORT)psia->Value[1],
  60. (USHORT)psia->Value[2],
  61. (USHORT)psia->Value[3],
  62. (USHORT)psia->Value[4],
  63. (USHORT)psia->Value[5]);
  64. } else {
  65. cchSidCopy += wsprintf(TextualSid + cchSidCopy,
  66. TEXT("%lu"),
  67. (ULONG)(psia->Value[5] ) +
  68. (ULONG)(psia->Value[4] << 8) +
  69. (ULONG)(psia->Value[3] << 16) +
  70. (ULONG)(psia->Value[2] << 24) );
  71. }
  72. //
  73. // loop through SidSubAuthorities
  74. //
  75. for(dwCounter = 0 ; dwCounter < dwSubAuthorities ; dwCounter++) {
  76. cchSidCopy += wsprintf(TextualSid + cchSidCopy, TEXT("-%lu"),
  77. *GetSidSubAuthority(pSid, dwCounter) );
  78. }
  79. //
  80. // tell the caller how many chars we provided, not including NULL
  81. //
  82. *cchSidSize = cchSidCopy;
  83. return TRUE;
  84. }
  85. void
  86. LogRegisterTimeout(
  87. GUID * pClsid,
  88. DWORD clsctx,
  89. CToken * pClientToken
  90. )
  91. {
  92. // Apply event filters
  93. DWORD dwActLogLvl = GetActivationFailureLoggingLevel();
  94. if(dwActLogLvl == 2)
  95. return;
  96. if(dwActLogLvl != 1 && clsctx & CLSCTX_NO_FAILURE_LOG)
  97. return;
  98. // %1 is the clsid
  99. HANDLE LogHandle;
  100. LPWSTR Strings[1]; // array of message strings.
  101. WCHAR wszClsid[GUIDSTR_MAX];
  102. // Get the clsid
  103. wStringFromGUID2( *pClsid, wszClsid, sizeof(wszClsid) );
  104. Strings[0] = wszClsid;
  105. // Get the log handle, then report then event.
  106. LogHandle = RegisterEventSource( NULL,
  107. SCM_EVENT_SOURCE );
  108. if ( LogHandle )
  109. {
  110. ReportEvent( LogHandle,
  111. EVENTLOG_ERROR_TYPE,
  112. 0, // event category
  113. EVENT_RPCSS_SERVER_START_TIMEOUT,
  114. pClientToken ? pClientToken->GetSid() : NULL, // SID
  115. 1, // 1 strings passed
  116. 0, // 0 bytes of binary
  117. (LPCTSTR *)Strings, // array of strings
  118. NULL ); // no raw data
  119. // clean up the event log handle
  120. DeregisterEventSource(LogHandle);
  121. }
  122. }
  123. void
  124. LogServerStartError(
  125. GUID * pClsid,
  126. DWORD clsctx,
  127. CToken * pClientToken,
  128. WCHAR * pwszCommandLine
  129. )
  130. {
  131. // Apply event filters
  132. DWORD dwActLogLvl = GetActivationFailureLoggingLevel();
  133. if(dwActLogLvl == 2)
  134. return;
  135. if(dwActLogLvl != 1 && clsctx & CLSCTX_NO_FAILURE_LOG)
  136. return;
  137. HANDLE LogHandle;
  138. LPWSTR Strings[3]; // array of message strings.
  139. WCHAR wszErrnum[20];
  140. WCHAR wszClsid[GUIDSTR_MAX];
  141. // Save the command line
  142. Strings[0] = pwszCommandLine;
  143. // Save the error number
  144. wsprintf(wszErrnum, L"%lu",GetLastError() );
  145. Strings[1] = wszErrnum;
  146. // Get the clsid
  147. wStringFromGUID2( *pClsid, wszClsid, sizeof(wszClsid) );
  148. Strings[2] = wszClsid;
  149. // Get the log handle, then report then event.
  150. LogHandle = RegisterEventSource( NULL,
  151. SCM_EVENT_SOURCE );
  152. if ( LogHandle )
  153. {
  154. ReportEvent( LogHandle,
  155. EVENTLOG_ERROR_TYPE,
  156. 0, // event category
  157. EVENT_RPCSS_CREATEPROCESS_FAILURE,
  158. pClientToken->GetSid(), // SID
  159. 3, // 3 strings passed
  160. 0, // 0 bytes of binary
  161. (LPCTSTR *)Strings, // array of strings
  162. NULL ); // no raw data
  163. // clean up the event log handle
  164. DeregisterEventSource(LogHandle);
  165. }
  166. }
  167. void
  168. LogRunAsServerStartError(
  169. GUID * pClsid,
  170. DWORD clsctx,
  171. CToken * pClientToken,
  172. WCHAR * pwszCommandLine,
  173. WCHAR * pwszRunAsUser,
  174. WCHAR * pwszRunAsDomain
  175. )
  176. {
  177. // Apply event filters
  178. DWORD dwActLogLvl = GetActivationFailureLoggingLevel();
  179. if(dwActLogLvl == 2)
  180. return;
  181. if(dwActLogLvl != 1 && clsctx & CLSCTX_NO_FAILURE_LOG)
  182. return;
  183. HANDLE LogHandle;
  184. LPWSTR Strings[5];
  185. WCHAR wszErrnum[20];
  186. WCHAR wszClsid[GUIDSTR_MAX];
  187. // for this message,
  188. // %1 is the command line, and %2 is the error number string
  189. // %3 is the CLSID, %4 is the RunAs domain name, %5 is the RunAs Userid
  190. // Save the command line
  191. Strings[0] = pwszCommandLine;
  192. // Save the error number
  193. wsprintf(wszErrnum, L"%lu",GetLastError() );
  194. Strings[1] = wszErrnum;
  195. // Get the clsid
  196. wStringFromGUID2(*pClsid, wszClsid, sizeof(wszClsid));
  197. Strings[2] = wszClsid;
  198. // Put in the RunAs identity
  199. Strings[3] = pwszRunAsDomain;
  200. Strings[4] = pwszRunAsUser;
  201. // Get the log handle, then report then event.
  202. LogHandle = RegisterEventSource( NULL,
  203. SCM_EVENT_SOURCE );
  204. if ( LogHandle )
  205. {
  206. ReportEvent( LogHandle,
  207. EVENTLOG_ERROR_TYPE,
  208. 0, // event category
  209. EVENT_RPCSS_RUNAS_CREATEPROCESS_FAILURE,
  210. pClientToken ? pClientToken->GetSid() : NULL, // SID
  211. 5, // 5 strings passed
  212. 0, // 0 bytes of binary
  213. (LPCTSTR *)Strings, // array of strings
  214. NULL ); // no raw data
  215. // clean up the event log handle
  216. DeregisterEventSource(LogHandle);
  217. }
  218. }
  219. void
  220. LogServiceStartError(
  221. GUID * pClsid,
  222. DWORD clsctx,
  223. CToken * pClientToken,
  224. WCHAR * pwszServiceName,
  225. WCHAR * pwszServiceArgs,
  226. DWORD err
  227. )
  228. {
  229. // Apply event filters
  230. DWORD dwActLogLvl = GetActivationFailureLoggingLevel();
  231. if(dwActLogLvl == 2)
  232. return;
  233. if(dwActLogLvl != 1 && clsctx & CLSCTX_NO_FAILURE_LOG)
  234. return;
  235. HANDLE LogHandle;
  236. LPWSTR Strings[4];
  237. WCHAR wszClsid[GUIDSTR_MAX];
  238. WCHAR wszErrnum[20];
  239. // %1 is the error number
  240. // %2 is the service name
  241. // %3 is the serviceargs
  242. // %4 is the clsid
  243. // Save the error number
  244. wsprintf(wszErrnum, L"%lu",err );
  245. Strings[0] = wszErrnum;
  246. Strings[1] = pwszServiceName;
  247. Strings[2] = pwszServiceArgs;
  248. // Get the clsid
  249. wStringFromGUID2(*pClsid, wszClsid, sizeof(wszClsid));
  250. Strings[3] = wszClsid;
  251. // Get the log handle, then report then event.
  252. LogHandle = RegisterEventSource( NULL,
  253. SCM_EVENT_SOURCE );
  254. if ( LogHandle )
  255. {
  256. ReportEvent( LogHandle,
  257. EVENTLOG_ERROR_TYPE,
  258. 0, // event category
  259. EVENT_RPCSS_START_SERVICE_FAILURE,
  260. pClientToken ? pClientToken->GetSid() : NULL, // SID
  261. 4, // 4 strings passed
  262. 0, // 0 bytes of binary
  263. (LPCTSTR *)Strings, // array of strings
  264. NULL ); // no raw data
  265. // clean up the event log handle
  266. DeregisterEventSource(LogHandle);
  267. }
  268. }
  269. void
  270. LogLaunchAccessFailed(
  271. GUID * pClsid,
  272. DWORD clsctx,
  273. CToken * pClientToken,
  274. BOOL bDefaultLaunchPermission
  275. )
  276. {
  277. // Apply event filters
  278. DWORD dwActLogLvl = GetActivationFailureLoggingLevel();
  279. if(dwActLogLvl == 2)
  280. return;
  281. if(dwActLogLvl != 1 && clsctx & CLSCTX_NO_FAILURE_LOG)
  282. return;
  283. HANDLE LogHandle;
  284. LPWSTR Strings[4];
  285. PSID pSid = pClientToken ? pClientToken->GetSid() : NULL;
  286. WCHAR wszClsid[GUIDSTR_MAX];
  287. // for this message, %1 is the clsid
  288. // %2 is username
  289. // %3 is domainname
  290. // %4 is textual SID
  291. ///////////////////////////////////////////////////
  292. //
  293. // Get the clsid
  294. //
  295. wStringFromGUID2(*pClsid, wszClsid, sizeof(wszClsid));
  296. Strings[0] = wszClsid;
  297. ///////////////////////////////////////////////////
  298. //
  299. // Get the user name, domain name
  300. //
  301. #define NAMELEN 256
  302. DWORD unamelen = NAMELEN;
  303. DWORD dnamelen = NAMELEN;
  304. SID_NAME_USE sidNameUse;
  305. WCHAR username[NAMELEN] = L"Unavailable";
  306. WCHAR domainname[NAMELEN] = L"Unavailable";
  307. Strings[1] = username;
  308. Strings[2] = domainname;
  309. if (pSid != NULL)
  310. {
  311. LookupAccountSid (NULL, pSid,
  312. username, &unamelen,
  313. domainname, &dnamelen,
  314. &sidNameUse);
  315. }
  316. ///////////////////////////////////////////////////
  317. //
  318. // Get SID as text
  319. //
  320. BOOL worked = FALSE;
  321. DWORD sidLen = NAMELEN;
  322. WCHAR sidAsText[NAMELEN];
  323. if (pSid != NULL)
  324. {
  325. worked = GetTextualSid (pSid, sidAsText, &sidLen);
  326. }
  327. Strings[3] = worked ? sidAsText : L"Unavailable";
  328. // Get the log handle, then report then event.
  329. LogHandle = RegisterEventSource( NULL,
  330. SCM_EVENT_SOURCE );
  331. if ( LogHandle )
  332. {
  333. ReportEvent( LogHandle,
  334. EVENTLOG_ERROR_TYPE,
  335. 0, // event category
  336. bDefaultLaunchPermission ? EVENT_RPCSS_DEFAULT_LAUNCH_ACCESS_DENIED : EVENT_RPCSS_LAUNCH_ACCESS_DENIED,
  337. pClientToken ? pClientToken->GetSid() : NULL, // SID
  338. 4, // 1 strings passed
  339. 0, // 0 bytes of binary
  340. (LPCTSTR *)Strings, // array of strings
  341. NULL ); // no raw data
  342. // clean up the event log handle
  343. DeregisterEventSource(LogHandle);
  344. }
  345. }
  346. void
  347. LogRemoteSideUnavailable(
  348. DWORD clsctx,
  349. WCHAR * pwszServerName )
  350. {
  351. // Apply event filters
  352. DWORD dwActLogLvl = GetActivationFailureLoggingLevel();
  353. if(dwActLogLvl == 2)
  354. return;
  355. if(dwActLogLvl != 1 && clsctx & CLSCTX_NO_FAILURE_LOG)
  356. return;
  357. HANDLE LogHandle;
  358. LPWSTR Strings[1];
  359. CToken * pToken;
  360. RPC_STATUS Status;
  361. // %1 is the remote machine name
  362. Strings[0] = pwszServerName;
  363. // Get the log handle, then report then event.
  364. LogHandle = RegisterEventSource( NULL, SCM_EVENT_SOURCE );
  365. if ( ! LogHandle )
  366. return;
  367. Status = LookupOrCreateToken( NULL, FALSE, &pToken );
  368. if ( Status != RPC_S_OK )
  369. return;
  370. ReportEvent( LogHandle,
  371. EVENTLOG_ERROR_TYPE,
  372. 0, // event category
  373. EVENT_RPCSS_REMOTE_SIDE_UNAVAILABLE,
  374. pToken->GetSid(),
  375. 1, // 1 strings passed
  376. 0, // 0 bytes of binary
  377. (LPCTSTR *)Strings, // array of strings
  378. NULL ); // no raw data
  379. // clean up the event log handle
  380. DeregisterEventSource(LogHandle);
  381. pToken->Release();
  382. }
  383. void
  384. LogRemoteSideFailure(
  385. CLSID * pClsid,
  386. DWORD clsctx,
  387. WCHAR * pwszServerName,
  388. WCHAR * pwszPathForServer,
  389. HRESULT hr )
  390. {
  391. // Apply event filters
  392. DWORD dwActLogLvl = GetActivationFailureLoggingLevel();
  393. if(dwActLogLvl == 2)
  394. return;
  395. if(dwActLogLvl != 1 && clsctx & CLSCTX_NO_FAILURE_LOG)
  396. return;
  397. HANDLE LogHandle;
  398. LPWSTR Strings[4];
  399. WCHAR wszClsid[GUIDSTR_MAX];
  400. WCHAR wszErrnum[20];
  401. CToken * pToken;
  402. RPC_STATUS Status;
  403. // %1 is the error number
  404. // %2 is the remote machine name
  405. // %3 is the clsid
  406. // %4 is the PathForServer
  407. // Save the error number
  408. wsprintf(wszErrnum, L"%lu",hr );
  409. Strings[0] = wszErrnum;
  410. Strings[1] = pwszServerName;
  411. // Get the clsid
  412. wStringFromGUID2( *pClsid, wszClsid, sizeof(wszClsid) );
  413. Strings[2] = wszClsid;
  414. Strings[3] = pwszPathForServer;
  415. // Get the log handle, then report then event.
  416. LogHandle = RegisterEventSource( NULL, SCM_EVENT_SOURCE );
  417. if ( ! LogHandle )
  418. return;
  419. Status = LookupOrCreateToken( NULL, FALSE, &pToken );
  420. if ( Status != RPC_S_OK )
  421. return;
  422. if ( pwszPathForServer )
  423. {
  424. ReportEvent( LogHandle,
  425. EVENTLOG_ERROR_TYPE,
  426. 0, // event category
  427. EVENT_RPCSS_REMOTE_SIDE_ERROR_WITH_FILE,
  428. pToken->GetSid(),
  429. 4, // 4 strings passed
  430. 0, // 0 bytes of binary
  431. (LPCTSTR *)Strings, // array of strings
  432. NULL ); // no raw data
  433. }
  434. else
  435. {
  436. ReportEvent( LogHandle,
  437. EVENTLOG_ERROR_TYPE,
  438. 0, // event category
  439. EVENT_RPCSS_REMOTE_SIDE_ERROR,
  440. pToken->GetSid(),
  441. 3, // 3 strings passed
  442. 0, // 0 bytes of binary
  443. (LPCTSTR *)Strings, // array of strings
  444. NULL ); // no raw data
  445. }
  446. // clean up the event log handle
  447. DeregisterEventSource(LogHandle);
  448. pToken->Release();
  449. }