⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 localpolicy.c

📁 voltage 公司提供的一个开发Ibe的工具包
💻 C
字号:
/* Copyright 2003-2005, Voltage Security, all rights reserved.
 */
#include "vibe.h"
#include "environment.h"
#include "base.h"
#include "libctx.h"
#include "policy.h"
#include "localpolicy.h"
#include "voltfile.h"

int VoltLocalPolicyGetInfoAlloc (
                                 VtPolicyCtx policyCtx,
                                 VoltPolicyGetType policyGetType,
                                 Pointer definition,
                                 Pointer *info
                                 );

void VoltLocalPolicyGetInfoFree (
                                 VtPolicyCtx policyCtx,
                                 Pointer info
                                 );

void VoltLocalPolicyCtxDestroy (
                                Pointer obj,
                                Pointer ctx
                                );

/* String based Policy implementation. Policy is provided in an
* XML string.
*/
int VtPolicyImplXmlBuffer (
   VtPolicyCtx *policyCtx,
   Pointer info,
   unsigned int flag
   )
{
  int status;
  VoltPolicyCtx *ctx = (VoltPolicyCtx *)(*policyCtx);
  VoltLibCtx *libCtx = (VoltLibCtx *)(ctx->voltObject.libraryCtx);
  unsigned char *buffer = (unsigned char *)0;
  VoltDefaultPolicyCtx *localPolicyCtx = (VoltDefaultPolicyCtx *)0;

  /* Check the flag, it should be VOLT_POLICY_CTX_SET_TYPE_FLAG.
   */
  if (flag != VOLT_POLICY_CTX_SET_TYPE_FLAG)
    return (VT_ERROR_INVALID_TYPE);

  /* The associated info should be a char array.
   */
  if (info == (Pointer)0)
    return (VT_ERROR_INVALID_ASSOCIATED_INFO);

  do
  {
    /* Allocate space for the local ctx, VoltDefaultPolicyCtx.
     */
    status = VT_ERROR_MEMORY;
    buffer = (unsigned char *)Z2Malloc (sizeof (VoltDefaultPolicyCtx), 0);
    if (buffer == (unsigned char *)0)
      break;
    Z2Memset (buffer, 0, sizeof (VoltDefaultPolicyCtx));

    localPolicyCtx = (VoltDefaultPolicyCtx *)buffer;

    /* Create the client policy.
     */
    status = VT_ERROR_INVALID_ASSOCIATED_INFO;
    localPolicyCtx->vsPolicyObj = m_vs_cp_new (
      (VtLibCtx)libCtx, (char *)info);
    if (localPolicyCtx->vsPolicyObj == (mVsClientPolicyT *)0)
      break;

    /* Set the fields of the policyCtx.
     */
    ctx->localCtx            = (Pointer)localPolicyCtx;
    ctx->LocalCtxDestroy     = VoltLocalPolicyCtxDestroy;
    ctx->PolicyGetInfoAlloc  = VoltLocalPolicyGetInfoAlloc;
    ctx->PolicyGetInfoFree   = VoltLocalPolicyGetInfoFree;

    status = 0;

  } while (0);

  /* If success, we're done.
   */
  if (status == 0)
    return (0);

  /* If error, destroy anything we created.
   */
  if (localPolicyCtx != (VoltDefaultPolicyCtx *)0)
  {
    if (localPolicyCtx->vsPolicyObj != (mVsClientPolicyT *)0)
      m_vs_cp_free (localPolicyCtx->vsPolicyObj);
  }

  if (buffer != (unsigned char *)0)
    Z2Free (buffer);

  return (status);
}

int VtPolicyImplXmlFile (
		      VtPolicyCtx *policyCtx,
		      Pointer info,
		      unsigned int flag
		      )
{
  int status;
  unsigned int bytesRead;
  VoltFileInt fileSize;
  VoltPolicyCtx *ctx = (VoltPolicyCtx *)(*policyCtx);
  VoltLibCtx *libCtx = (VoltLibCtx *)(ctx->voltObject.libraryCtx);
  VtFileCtxUseInfo *fileInfo;
  VtFileCtx fileCtxToUse;
  VoltFileHandle fileHandle = (VoltFileHandle)0;
  char *xmlString = (char *)0;

  do
  {
    /* Check the flag, it should be VOLT_POLICY_CTX_SET_TYPE_FLAG.
     */
    status = VT_ERROR_INVALID_TYPE;
    if (flag != VOLT_POLICY_CTX_SET_TYPE_FLAG)
      break;

    /* The info must be the FileCtxUseInfo.
     */
    status = VT_ERROR_INVALID_ASSOCIATED_INFO;
    if (info == (Pointer)0)
      break;

    fileInfo = (VtFileCtxUseInfo *)info;
    if (fileInfo->path == (unsigned char *)0)
      break;

    /* If there's no fileCtx in the info, get one from the libCtx. If
     * there's not one there, error.
     */    
    fileCtxToUse = fileInfo->fileCtx;
    if (fileCtxToUse == (VtFileCtx)0)
    {
      fileCtxToUse = (VtFileCtx)VoltGetLibCtxInfo (
        (VtLibCtx)libCtx, VOLT_LIB_CTX_INFO_TYPE_FILE_CTX);

      if (fileCtxToUse == (VtFileCtx)0)
        break;
    }    
    if (VOLT_OBJECT_TYPE_NOT_EQUAL (fileCtxToUse, VOLT_OBJECT_TYPE_FILE_CTX))
      break;

    /* What we read from the file is simply the XML string that is
     * the argument to VtPolicyImplXmlBuffer. So download that string,
     * then call VtPolicyImplXmlBuffer.
     */

    status = fileCtxToUse->CtxOpenFile (
      fileCtxToUse, &fileHandle, fileInfo->path,
      VOLT_FILE_MODE_READ_ONLY, 0);
    if (status != 0)
      break;

    status = fileCtxToUse->CtxGetFileSize (
      fileCtxToUse, fileHandle, (char *)0, &fileSize);
    if (status != 0)
      break;

    status = VT_ERROR_MEMORY;
    xmlString = (char *)Z2Malloc (fileSize + 1, 0);
    if (xmlString == (char *)0)
      break;

    status = fileCtxToUse->CtxReadFile (
      fileCtxToUse, 
      fileHandle, 
      (unsigned int)fileSize, 
      xmlString,       
      &bytesRead);
    if (status != 0)
      break;

    xmlString[bytesRead] = 0;
    status = VtPolicyImplXmlBuffer (policyCtx, (Pointer)xmlString, flag);

  } while (0);

  if (xmlString != (char *)0)
    Z2Free (xmlString);
  if (fileHandle != (VoltFileHandle)0)
    fileCtxToUse->CtxCloseFile (fileCtxToUse, &fileHandle);

  return (status);
}

void VoltLocalPolicyCtxDestroy (
   Pointer obj,
   Pointer ctx
   )
{
  VoltPolicyCtx *policyCtx = (VoltPolicyCtx *)obj;
  VoltLibCtx *libCtx;
  VoltDefaultPolicyCtx *localPolicyCtx = (VoltDefaultPolicyCtx *)ctx;

  /* Anything to destroy?
   */
  if ( (obj == (Pointer)0) || (ctx == (Pointer)0) )
    return;

  libCtx = (VoltLibCtx *)(policyCtx->voltObject.libraryCtx);

  /* Destroy the contents of the localCtx.
   */
  if (localPolicyCtx->vsPolicyObj != (mVsClientPolicyT *)0)
    m_vs_cp_free (localPolicyCtx->vsPolicyObj);

  /* Free up the localCtx.
   */
  Z2Free (ctx);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -