home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / winbase / security / certsvr / exit / exit.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-19  |  8.0 KB  |  443 lines

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996-1997
  5. //
  6. // File:        exit.cpp
  7. //
  8. // Contents:    CCertExit implementation
  9. //
  10. //---------------------------------------------------------------------------
  11.  
  12. #include "pch.cpp"
  13. #pragma hdrstop
  14.  
  15. #include <stdio.h>
  16. #include "exit.h"
  17.  
  18. #define myEXITEVENTS \
  19.     EXITEVENT_CERTISSUED | \
  20.     EXITEVENT_CERTPENDING | \
  21.     EXITEVENT_CERTDENIED | \
  22.     EXITEVENT_CERTREVOKED | \
  23.     EXITEVENT_CERTRETRIEVEPENDING | \
  24.     EXITEVENT_CRLISSUED | \
  25.     EXITEVENT_SHUTDOWN
  26.  
  27. #ifndef DBG_CERTSRV
  28. #error -- DBG_CERTSRV not defined!
  29. #endif
  30. BOOL fDebug = DBG_CERTSRV;
  31.  
  32. #define wszDESCRIPTION    L"Sample Exit Module"
  33.  
  34.  
  35. //+--------------------------------------------------------------------------
  36. // CCertExit::~CCertExit -- destructor
  37. //
  38. // free memory associated with this instance
  39. //+--------------------------------------------------------------------------
  40.  
  41. CCertExit::~CCertExit()
  42. {
  43.     if (NULL != m_strConfig)
  44.     {
  45.     SysFreeString(m_strConfig);
  46.     }
  47. }
  48.  
  49.  
  50. //+--------------------------------------------------------------------------
  51. // CCertExit::Initialize -- initialize for a CA & return interesting Event Mask
  52. //
  53. // Returns S_OK on success.
  54. //+--------------------------------------------------------------------------
  55.  
  56. STDMETHODIMP
  57. CCertExit::Initialize(
  58.     /* [in] */ BSTR const strConfig,
  59.     /* [retval][out] */ LONG __RPC_FAR *pEventMask)
  60. {
  61.     HRESULT hr = S_OK;
  62.  
  63.     m_strConfig = SysAllocString(strConfig);
  64.     if (NULL == m_strConfig)
  65.     {
  66.     hr = E_OUTOFMEMORY;
  67.     }
  68.     else
  69.     {
  70.     *pEventMask = myEXITEVENTS;
  71.     printf("Exit::Initialize(%ws) ==> %x\n", m_strConfig, *pEventMask);
  72.     }
  73.     return(hr);
  74. }
  75.  
  76.  
  77. HRESULT
  78. EnumerateExtensions(
  79.     IN ICertServerExit *pServer)
  80. {
  81.     HRESULT hr;
  82.     HRESULT hr2;
  83.     BSTR strName = NULL;
  84.     LONG ExtFlags;
  85.     VARIANT varValue;
  86.     BOOL fClose = FALSE;
  87.  
  88.     VariantInit(&varValue);
  89.     hr = pServer->EnumerateExtensionsSetup(0);
  90.     if (S_OK != hr)
  91.     {
  92.     if (fDebug)
  93.     {
  94.         printf("Exit:EnumerateExtensionsSetup: %x", hr);
  95.     }
  96.     goto error;
  97.     }
  98.     fClose = TRUE;
  99.     while (TRUE)
  100.     {
  101.     hr = pServer->EnumerateExtensions(&strName);
  102.     if (S_OK != hr)
  103.     {
  104.         if (S_FALSE == hr)
  105.         {
  106.         hr = S_OK;
  107.         break;
  108.         }
  109.         if (fDebug)
  110.         {
  111.         printf("Exit:EnumerateExtensions: %x", hr);
  112.         }
  113.         goto error;
  114.     }
  115.     hr = pServer->GetCertificateExtension(
  116.                     strName,
  117.                     PROPTYPE_BINARY,
  118.                     &varValue);
  119.     if (S_OK != hr)
  120.     {
  121.         if (fDebug)
  122.         {
  123.         printf("Exit:GetCertificateExtension: %x", hr);
  124.         }
  125.         goto error;
  126.     }
  127.     hr = pServer->GetCertificateExtensionFlags(&ExtFlags);
  128.     if (S_OK != hr)
  129.     {
  130.         if (fDebug)
  131.         {
  132.         printf("Exit:GetCertificateExtensionFlags: %x", hr);
  133.         }
  134.         goto error;
  135.     }
  136.     if (fDebug)
  137.     {
  138.         printf(
  139.         "Exit:EnumerateExtensions(%ws, Flags=%x, %x bytes)\n",
  140.         strName,
  141.         ExtFlags,
  142.         SysStringByteLen(varValue.bstrVal));
  143.     }
  144.     VariantClear(&varValue);
  145.     }
  146.  
  147. error:
  148.     if (fClose)
  149.     {
  150.     hr2 = pServer->EnumerateExtensionsClose();
  151.     if (S_OK != hr2)
  152.     {
  153.         if (fDebug)
  154.         {
  155.         printf("Exit:EnumerateExtensionsClose: %x", hr2);
  156.         }
  157.         if (S_OK == hr)
  158.         {
  159.         hr = hr2;
  160.         }
  161.         goto error;
  162.     }
  163.     }
  164.     if (NULL != strName)
  165.     {
  166.     SysFreeString(strName);
  167.     }
  168.     VariantClear(&varValue);
  169.     return(hr);
  170. }
  171.  
  172.  
  173. HRESULT
  174. EnumerateAttributes(
  175.     IN ICertServerExit *pServer)
  176. {
  177.     HRESULT hr;
  178.     HRESULT hr2;
  179.     BSTR strName = NULL;
  180.     BOOL fClose = FALSE;
  181.     BSTR strValue = NULL;
  182.  
  183.     hr = pServer->EnumerateAttributesSetup(0);
  184.     if (S_OK != hr)
  185.     {
  186.     if (fDebug)
  187.     {
  188.         printf("Exit:EnumerateAttributesSetup: %x", hr);
  189.     }
  190.     goto error;
  191.     }
  192.     fClose = TRUE;
  193.     while (TRUE)
  194.     {
  195.     hr = pServer->EnumerateAttributes(&strName);
  196.     if (S_OK != hr)
  197.     {
  198.         if (S_FALSE == hr)
  199.         {
  200.         hr = S_OK;
  201.         break;
  202.         }
  203.         if (fDebug)
  204.         {
  205.         printf("Exit:EnumerateAttributes: %x", hr);
  206.         }
  207.         goto error;
  208.     }
  209.     hr = pServer->GetRequestAttribute(strName, &strValue);
  210.     if (S_OK != hr)
  211.     {
  212.         if (fDebug)
  213.         {
  214.         printf("Exit:GetRequestAttribute: %x", hr);
  215.         }
  216.         goto error;
  217.     }
  218.     if (fDebug)
  219.     {
  220.         printf("Exit:EnumerateAttributes(%ws = %ws)\n", strName, strValue);
  221.     }
  222.     if (NULL != strValue)
  223.     {
  224.         SysFreeString(strValue);
  225.         strValue = NULL;
  226.     }
  227.     }
  228.  
  229. error:
  230.     if (fClose)
  231.     {
  232.     hr2 = pServer->EnumerateAttributesClose();
  233.     if (S_OK != hr2)
  234.     {
  235.         if (fDebug)
  236.         {
  237.         printf("Exit:EnumerateAttributesClose: %x", hr2);
  238.         }
  239.         if (S_OK == hr)
  240.         {
  241.         hr = hr2;
  242.         }
  243.         goto error;
  244.     }
  245.     }
  246.  
  247.     if (NULL != strName)
  248.     {
  249.     SysFreeString(strName);
  250.     }
  251.     if (NULL != strValue)
  252.     {
  253.     SysFreeString(strValue);
  254.     }
  255.     return(hr);
  256. }
  257.  
  258.  
  259. HRESULT
  260. CheckRequestProperties(
  261.     IN ICertServerExit *pServer)
  262. {
  263.     HRESULT hr;
  264.     VARIANT varValue;
  265.     BSTR strName = NULL;
  266.  
  267.     VariantInit(&varValue);
  268.  
  269.     strName = SysAllocString(wszPROPREQUESTREQUESTID);
  270.     if (NULL == strName)
  271.     {
  272.     hr = E_OUTOFMEMORY;
  273.     goto error;
  274.     }
  275.  
  276.     hr = pServer->GetRequestProperty(strName, PROPTYPE_LONG, &varValue);
  277.     if (S_OK != hr)
  278.     {
  279.     if (fDebug)
  280.     {
  281.         printf("Exit:GetRequestProperty: %x", hr);
  282.     }
  283.     goto error;
  284.     }
  285.     if (fDebug)
  286.     {
  287.     printf(
  288.         "Exit:CheckRequestProperties(%ws = %x)\n",
  289.         strName,
  290.         varValue.lVal);
  291.     }
  292.     VariantClear(&varValue);
  293.  
  294. error:
  295.     if (NULL != strName)
  296.     {
  297.     SysFreeString(strName);
  298.     }
  299.     return(hr);
  300. }
  301.  
  302.  
  303. HRESULT
  304. CheckCert(
  305.     LONG Context)
  306. {
  307.     HRESULT hr;
  308.     ICertServerExit *pServer = NULL;
  309.  
  310.     hr = CoCreateInstance(
  311.             CLSID_CCertServerExit,
  312.             NULL,        // pUnkOuter
  313.             CLSCTX_INPROC_SERVER,
  314.             IID_ICertServerExit,
  315.             (VOID **) &pServer);
  316.     if (S_OK != hr)
  317.     {
  318.     goto exit;
  319.     }
  320.  
  321.     hr = pServer->SetContext(Context);
  322.     if (S_OK != hr)
  323.     {
  324.     goto exit;
  325.     }
  326.     hr = EnumerateExtensions(pServer);
  327.     if (S_OK != hr)
  328.     {
  329.     goto exit;
  330.     }
  331.  
  332.     hr = EnumerateAttributes(pServer);
  333.     if (S_OK != hr)
  334.     {
  335.     goto exit;
  336.     }
  337.  
  338.     hr = CheckRequestProperties(pServer);
  339.     if (S_OK != hr)
  340.     {
  341.     goto exit;
  342.     }
  343.  
  344. exit:
  345.     if (NULL != pServer)
  346.     {
  347.     pServer->Release();
  348.     }
  349.     return(hr);
  350. }
  351.  
  352.  
  353. //+--------------------------------------------------------------------------
  354. // CCertExit::Notify -- Notify the exit module of an event
  355. //
  356. // Returns S_OK.
  357. //+--------------------------------------------------------------------------
  358.  
  359. STDMETHODIMP
  360. CCertExit::Notify(
  361.     /* [in] */ LONG Event,
  362.     /* [in] */ LONG Context)
  363. {
  364.     HRESULT hr = S_OK;
  365.     char *psz = "UNKNOWN EVENT";
  366.  
  367.     switch (Event)
  368.     {
  369.     case EXITEVENT_CERTISSUED:
  370.         psz = "certissued";
  371.         break;
  372.  
  373.     case EXITEVENT_CERTPENDING:
  374.         psz = "certpending";
  375.         break;
  376.  
  377.     case EXITEVENT_CERTDENIED:
  378.         psz = "certdenied";
  379.         break;
  380.  
  381.     case EXITEVENT_CERTREVOKED:
  382.         psz = "certrevoked";
  383.         break;
  384.  
  385.     case EXITEVENT_CERTRETRIEVEPENDING:
  386.         psz = "retrievepending";
  387.         break;
  388.  
  389.     case EXITEVENT_CRLISSUED:
  390.         psz = "crlissued";
  391.         break;
  392.  
  393.     case EXITEVENT_SHUTDOWN:
  394.         psz = "shutdown";
  395.         break;
  396.     }
  397.     printf("Exit::Notify(%s=%x, ctx=%u)\n", psz, Event, Context);
  398.  
  399.     if (EXITEVENT_CERTISSUED == Event)
  400.     {
  401.     hr = CheckCert(Context);
  402.     }
  403.     return(hr);
  404. }
  405.  
  406.  
  407. STDMETHODIMP
  408. CCertExit::GetDescription(
  409.     /* [retval][out] */ BSTR *pstrDescription)
  410. {
  411.     HRESULT hr = S_OK;
  412.  
  413.     *pstrDescription = SysAllocString(wszDESCRIPTION);
  414.     if (NULL == *pstrDescription)
  415.     {
  416.     hr = E_OUTOFMEMORY;
  417.     }
  418.     return(hr);
  419. }
  420.  
  421.  
  422. /////////////////////////////////////////////////////////////////////////////
  423. //
  424.  
  425. STDMETHODIMP
  426. CCertExit::InterfaceSupportsErrorInfo(REFIID riid)
  427. {
  428.     int i;
  429.     static const IID *arr[] =
  430.     {
  431.     &IID_ICertExit,
  432.     };
  433.  
  434.     for (i = 0; i < sizeof(arr)/sizeof(arr[0]); i++)
  435.     {
  436.     if (InlineIsEqualGUID(*arr[i],riid))
  437.     {
  438.         return(S_OK);
  439.     }
  440.     }
  441.     return(S_FALSE);
  442. }
  443.