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.

200 lines
4.6 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1999 - 1999
  6. //
  7. // File: cspelog.cpp
  8. //
  9. // Contents: implements policy and exit module logging routines.
  10. //
  11. //--------------------------------------------------------------------------
  12. #include <pch.cpp>
  13. #pragma hdrstop
  14. #include "csdisp.h"
  15. #define __dwFILE__ __dwFILE_CERTLIB_CSPELOG_CPP__
  16. HRESULT
  17. SetModuleErrorInfo(
  18. IN ICreateErrorInfo *pCreateErrorInfo)
  19. {
  20. HRESULT hr;
  21. IErrorInfo *pErrorInfo = NULL;
  22. hr = pCreateErrorInfo->QueryInterface(
  23. IID_IErrorInfo,
  24. (VOID **) &pErrorInfo);
  25. _JumpIfError(hr, error, "QueryInterface");
  26. hr = SetErrorInfo(0, pErrorInfo);
  27. _JumpIfError(hr, error, "SetErrorInfo");
  28. error:
  29. if (NULL != pErrorInfo)
  30. {
  31. pErrorInfo->Release();
  32. }
  33. return(hr);
  34. }
  35. HRESULT
  36. LogModuleStatus(
  37. IN HMODULE hModule,
  38. IN HRESULT hrMsg,
  39. IN DWORD dwLogID, // Resource ID of log string
  40. IN BOOL fPolicy,
  41. IN WCHAR const *pwszSource,
  42. IN WCHAR const * const *ppwszInsert, // array of insert strings
  43. OPTIONAL OUT ICreateErrorInfo **ppCreateErrorInfo)
  44. {
  45. HRESULT hr;
  46. WCHAR *pwszResult = NULL;
  47. ICreateErrorInfo *pCreateErrorInfo = NULL;
  48. if (NULL != ppCreateErrorInfo)
  49. {
  50. *ppCreateErrorInfo = NULL;
  51. }
  52. if (0 == FormatMessage(
  53. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  54. FORMAT_MESSAGE_ARGUMENT_ARRAY |
  55. FORMAT_MESSAGE_FROM_HMODULE,
  56. hModule,
  57. dwLogID,
  58. 0,
  59. (WCHAR *) &pwszResult,
  60. 0,
  61. (va_list *) ppwszInsert))
  62. {
  63. hr = myHLastError();
  64. _JumpError(hr, error, "FormatMessage");
  65. }
  66. hr = CreateErrorInfo(&pCreateErrorInfo);
  67. _JumpIfError(hr, error, "CreateErrorInfo");
  68. hr = pCreateErrorInfo->SetGUID(fPolicy? IID_ICertPolicy : IID_ICertExit);
  69. _PrintIfError(hr, "SetGUID");
  70. if (S_OK != hrMsg)
  71. {
  72. WCHAR wszhr[cwcDWORDSPRINTF];
  73. wsprintf(wszhr, L"0x%x", hrMsg);
  74. hr = myPrependString(wszhr, L", ", &pwszResult);
  75. _PrintIfError(hr, "myPrependString");
  76. }
  77. DBGPRINT((
  78. fPolicy? DBG_SS_CERTPOL : DBG_SS_CERTEXIT,
  79. "LogModuleStatus(%ws): %ws\n",
  80. pwszSource,
  81. pwszResult));
  82. hr = pCreateErrorInfo->SetDescription(pwszResult);
  83. _PrintIfError(hr, "SetDescription");
  84. // Set ProgId:
  85. hr = pCreateErrorInfo->SetSource(const_cast<WCHAR *>(pwszSource));
  86. _PrintIfError(hr, "SetSource");
  87. if (NULL != ppCreateErrorInfo)
  88. {
  89. *ppCreateErrorInfo = pCreateErrorInfo;
  90. pCreateErrorInfo = NULL;
  91. }
  92. else
  93. {
  94. hr = SetModuleErrorInfo(pCreateErrorInfo);
  95. _JumpIfError(hr, error, "SetModuleErrorInfo");
  96. }
  97. hr = S_OK;
  98. error:
  99. if (NULL != pwszResult)
  100. {
  101. LocalFree(pwszResult);
  102. }
  103. if (NULL != pCreateErrorInfo)
  104. {
  105. pCreateErrorInfo->Release();
  106. }
  107. return(hr);
  108. }
  109. HRESULT
  110. LogPolicyEvent(
  111. IN HMODULE hModule,
  112. IN HRESULT hrMsg,
  113. IN DWORD dwLogID, // Resource ID of log string
  114. IN ICertServerPolicy *pServer,
  115. IN WCHAR const *pwszPropEvent,
  116. IN WCHAR const * const *ppwszInsert) // array of insert strings
  117. {
  118. HRESULT hr;
  119. WCHAR *pwszValue = NULL;
  120. BSTR strName = NULL;
  121. VARIANT varValue;
  122. if (0 == FormatMessage(
  123. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  124. FORMAT_MESSAGE_ARGUMENT_ARRAY |
  125. FORMAT_MESSAGE_FROM_HMODULE,
  126. hModule,
  127. dwLogID,
  128. 0,
  129. (WCHAR *) &pwszValue,
  130. 0,
  131. (va_list *) ppwszInsert))
  132. {
  133. hr = myHLastError();
  134. _JumpError(hr, error, "FormatMessage");
  135. }
  136. DBGPRINT((DBG_SS_CERTPOL, "LogPolicyEvent: %ws\n", pwszValue));
  137. varValue.vt = VT_EMPTY;
  138. if (!myConvertWszToBstr(&strName, pwszPropEvent, -1))
  139. {
  140. hr = E_OUTOFMEMORY;
  141. _JumpError(hr, error, "myConvertWszToBstr");
  142. }
  143. if (S_OK != hrMsg)
  144. {
  145. WCHAR wszhr[cwcDWORDSPRINTF];
  146. wsprintf(wszhr, L"0x%x", hrMsg);
  147. hr = myPrependString(wszhr, L",", &pwszValue);
  148. _PrintIfError(hr, "myPrependString");
  149. }
  150. varValue.bstrVal = NULL;
  151. if (!myConvertWszToBstr(&varValue.bstrVal, pwszValue, -1))
  152. {
  153. hr = E_OUTOFMEMORY;
  154. _JumpError(hr, error, "myConvertWszToBstr");
  155. }
  156. varValue.vt = VT_BSTR;
  157. hr = pServer->SetCertificateProperty(strName, PROPTYPE_STRING, &varValue);
  158. _JumpIfError(hr, error, "SetCertificateProperty");
  159. error:
  160. VariantClear(&varValue);
  161. if (NULL != strName)
  162. {
  163. SysFreeString(strName);
  164. }
  165. if (NULL != pwszValue)
  166. {
  167. LocalFree(pwszValue);
  168. }
  169. return(hr);
  170. }