]> granicus.if.org Git - imagemagick/blobdiff - magick/policy.c
(no commit message)
[imagemagick] / magick / policy.c
index a6701e1ad4e394669aa1d1d75da8be14766f9b06..0b5064b63a8009b1d0b52cae6d97d26bf62739b3 100644 (file)
@@ -16,7 +16,7 @@
 %                                 July 1992                                   %
 %                                                                             %
 %                                                                             %
-%  Copyright 1999-2009 ImageMagick Studio LLC, a non-profit organization      %
+%  Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization      %
 %  dedicated to making software imaging solutions freely available.           %
 %                                                                             %
 %  You may not use this file except in compliance with the License.  You may  %
 #define PolicyFilename  "policy.xml"
 \f
 /*
-  Declare policy map.
-*/
-static const char
-  *PolicyMap = (const char *)
-    "<?xml version=\"1.0\"?>"
-    "<policymap>"
-    "</policymap>";
-\f
-/*
-  Domaindef declarations.
+  Typedef declarations.
 */
 struct _PolicyInfo
 {
   char
-    *path,
-    *name;
+    *path;
 
   PolicyDomain
     domain;
@@ -86,23 +76,46 @@ struct _PolicyInfo
     rights;
 
   char
+    *name,
     *pattern,
     *value;
 
   MagickBooleanType
+    exempt,
     stealth,
     debug;
 
   SemaphoreInfo
     *semaphore;
 
-  unsigned long
+  size_t
     signature;
 };
+
+typedef struct _PolicyMapInfo
+{
+  const PolicyDomain
+    domain;
+
+  const PolicyRights
+    rights;
+
+  const char
+    *name,
+    *pattern,
+    *value;
+} PolicyMapInfo;
 \f
 /*
   Static declarations.
 */
+static const PolicyMapInfo
+  PolicyMap[] =
+  {
+    { UndefinedPolicyDomain, UndefinedPolicyRights, (const char *) NULL,
+      (const char *) NULL, (const char *) NULL }
+  };
+
 static LinkedListInfo
   *policy_list = (LinkedListInfo *) NULL;
 
@@ -124,53 +137,6 @@ static MagickBooleanType
 %                                                                             %
 %                                                                             %
 %                                                                             %
-+   D e s t r o y P o l i c y F a c i l i t y                                 %
-%                                                                             %
-%                                                                             %
-%                                                                             %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%
-%  DestroyPolicyFacility() destroys the policy facility.
-%
-%  The format of the DestroyPolicyFacility method is:
-%
-%      DestroyPolicyFacility(void)
-%
-*/
-
-static void *DestroyPolicyElement(void *policy_info)
-{
-  register PolicyInfo
-    *p;
-
-  p=(PolicyInfo *) policy_info;
-  if (p->value != (char *) NULL)
-    p->value=DestroyString(p->value);
-  if (p->pattern != (char *) NULL)
-    p->pattern=DestroyString(p->pattern);
-  if (p->name != (char *) NULL)
-    p->name=DestroyString(p->name);
-  if (p->path != (char *) NULL)
-    p->path=DestroyString(p->path);
-  p=(PolicyInfo *) RelinquishMagickMemory(p);
-  return((void *) NULL);
-}
-
-MagickExport void DestroyPolicyFacility(void)
-{
-  AcquireSemaphoreInfo(&policy_semaphore);
-  if (policy_list != (LinkedListInfo *) NULL)
-    policy_list=DestroyLinkedList(policy_list,DestroyPolicyElement);
-  instantiate_policy=MagickFalse;
-  RelinquishSemaphoreInfo(policy_semaphore);
-  DestroySemaphoreInfo(&policy_semaphore);
-}
-\f
-/*
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%                                                                             %
-%                                                                             %
-%                                                                             %
 +   G e t P o l i c y I n f o                                                 %
 %                                                                             %
 %                                                                             %
@@ -226,7 +192,7 @@ static PolicyInfo *GetPolicyInfo(const char *name,ExceptionInfo *exception)
   /*
     Search for policy tag.
   */
-  AcquireSemaphoreInfo(&policy_semaphore);
+  LockSemaphoreInfo(policy_semaphore);
   ResetLinkedListIterator(policy_list);
   p=(PolicyInfo *) GetNextValueInLinkedList(policy_list);
   while (p != (PolicyInfo *) NULL)
@@ -235,13 +201,10 @@ static PolicyInfo *GetPolicyInfo(const char *name,ExceptionInfo *exception)
       break;
     p=(PolicyInfo *) GetNextValueInLinkedList(policy_list);
   }
-  if (p == (PolicyInfo *) NULL)
-    (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
-      "UnrecognizedPolicy","`%s'",name);
-  else
+  if (p != (PolicyInfo *) NULL)
     (void) InsertValueInLinkedList(policy_list,0,
       RemoveElementByValueFromLinkedList(policy_list,p));
-  RelinquishSemaphoreInfo(policy_semaphore);
+  UnlockSemaphoreInfo(policy_semaphore);
   return(p);
 }
 \f
@@ -261,7 +224,7 @@ static PolicyInfo *GetPolicyInfo(const char *name,ExceptionInfo *exception)
 %  The format of the GetPolicyInfoList function is:
 %
 %      const PolicyInfo **GetPolicyInfoList(const char *pattern,
-%        unsigned long *number_policies,ExceptionInfo *exception)
+%        size_t *number_policies,ExceptionInfo *exception)
 %
 %  A description of each parameter follows:
 %
@@ -273,7 +236,7 @@ static PolicyInfo *GetPolicyInfo(const char *name,ExceptionInfo *exception)
 %
 */
 MagickExport const PolicyInfo **GetPolicyInfoList(const char *pattern,
-  unsigned long *number_policies,ExceptionInfo *exception)
+  size_t *number_policies,ExceptionInfo *exception)
 {
   const PolicyInfo
     **policies;
@@ -281,7 +244,7 @@ MagickExport const PolicyInfo **GetPolicyInfoList(const char *pattern,
   register const PolicyInfo
     *p;
 
-  register long
+  register ssize_t
     i;
 
   /*
@@ -289,7 +252,7 @@ MagickExport const PolicyInfo **GetPolicyInfoList(const char *pattern,
   */
   assert(pattern != (char *) NULL);
   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
-  assert(number_policies != (unsigned long *) NULL);
+  assert(number_policies != (size_t *) NULL);
   *number_policies=0;
   p=GetPolicyInfo("*",exception);
   if (p == (const PolicyInfo *) NULL)
@@ -301,7 +264,7 @@ MagickExport const PolicyInfo **GetPolicyInfoList(const char *pattern,
   /*
     Generate policy list.
   */
-  AcquireSemaphoreInfo(&policy_semaphore);
+  LockSemaphoreInfo(policy_semaphore);
   ResetLinkedListIterator(policy_list);
   p=(const PolicyInfo *) GetNextValueInLinkedList(policy_list);
   for (i=0; p != (const PolicyInfo *) NULL; )
@@ -311,9 +274,9 @@ MagickExport const PolicyInfo **GetPolicyInfoList(const char *pattern,
       policies[i++]=p;
     p=(const PolicyInfo *) GetNextValueInLinkedList(policy_list);
   }
-  RelinquishSemaphoreInfo(policy_semaphore);
+  UnlockSemaphoreInfo(policy_semaphore);
   policies[i]=(PolicyInfo *) NULL;
-  *number_policies=(unsigned long) i;
+  *number_policies=(size_t) i;
   return(policies);
 }
 \f
@@ -332,7 +295,7 @@ MagickExport const PolicyInfo **GetPolicyInfoList(const char *pattern,
 %
 %  The format of the GetPolicyList function is:
 %
-%      char **GetPolicyList(const char *pattern,unsigned long *number_policies,
+%      char **GetPolicyList(const char *pattern,size_t *number_policies,
 %        ExceptionInfo *exception)
 %
 %  A description of each parameter follows:
@@ -345,7 +308,7 @@ MagickExport const PolicyInfo **GetPolicyInfoList(const char *pattern,
 %
 */
 MagickExport char **GetPolicyList(const char *pattern,
-  unsigned long *number_policies,ExceptionInfo *exception)
+  size_t *number_policies,ExceptionInfo *exception)
 {
   char
     **policies;
@@ -353,7 +316,7 @@ MagickExport char **GetPolicyList(const char *pattern,
   register const PolicyInfo
     *p;
 
-  register long
+  register ssize_t
     i;
 
   /*
@@ -361,7 +324,7 @@ MagickExport char **GetPolicyList(const char *pattern,
   */
   assert(pattern != (char *) NULL);
   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
-  assert(number_policies != (unsigned long *) NULL);
+  assert(number_policies != (size_t *) NULL);
   *number_policies=0;
   p=GetPolicyInfo("*",exception);
   if (p == (const PolicyInfo *) NULL)
@@ -373,7 +336,7 @@ MagickExport char **GetPolicyList(const char *pattern,
   /*
     Generate policy list.
   */
-  AcquireSemaphoreInfo(&policy_semaphore);
+  LockSemaphoreInfo(policy_semaphore);
   ResetLinkedListIterator(policy_list);
   p=(const PolicyInfo *) GetNextValueInLinkedList(policy_list);
   for (i=0; p != (const PolicyInfo *) NULL; )
@@ -383,9 +346,9 @@ MagickExport char **GetPolicyList(const char *pattern,
       policies[i++]=ConstantString(p->name);
     p=(const PolicyInfo *) GetNextValueInLinkedList(policy_list);
   }
-  RelinquishSemaphoreInfo(policy_semaphore);
+  UnlockSemaphoreInfo(policy_semaphore);
   policies[i]=(char *) NULL;
-  *number_policies=(unsigned long) i;
+  *number_policies=(size_t) i;
   return(policies);
 }
 \f
@@ -462,14 +425,16 @@ static MagickBooleanType InitializePolicyList(ExceptionInfo *exception)
   if ((policy_list == (LinkedListInfo *) NULL) &&
       (instantiate_policy == MagickFalse))
     {
-      AcquireSemaphoreInfo(&policy_semaphore);
+      if (policy_semaphore == (SemaphoreInfo *) NULL)
+        AcquireSemaphoreInfo(&policy_semaphore);
+      LockSemaphoreInfo(policy_semaphore);
       if ((policy_list == (LinkedListInfo *) NULL) &&
           (instantiate_policy == MagickFalse))
         {
           (void) LoadPolicyLists(PolicyFilename,exception);
           instantiate_policy=MagickTrue;
         }
-      RelinquishSemaphoreInfo(policy_semaphore);
+      UnlockSemaphoreInfo(policy_semaphore);
     }
   return(policy_list != (LinkedListInfo *) NULL ? MagickTrue : MagickFalse);
 }
@@ -479,31 +444,6 @@ static MagickBooleanType InitializePolicyList(ExceptionInfo *exception)
 %                                                                             %
 %                                                                             %
 %                                                                             %
-+   I n s t a n t i a t e P o l i c y F a c i l i t y                         %
-%                                                                             %
-%                                                                             %
-%                                                                             %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%
-%  InstantiatePolicyFacility() instantiates the policy facility.
-%
-%  The format of the InstantiatePolicyFacility method is:
-%
-%      MagickBooleanType InstantiatePolicyFacility(void)
-%
-*/
-MagickExport MagickBooleanType InstantiatePolicyFacility(void)
-{
-  AcquireSemaphoreInfo(&policy_semaphore);
-  RelinquishSemaphoreInfo(policy_semaphore);
-  return(MagickTrue);
-}
-\f
-/*
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%                                                                             %
-%                                                                             %
-%                                                                             %
 %   I s R i g h t s A u t h o r i z e d                                       %
 %                                                                             %
 %                                                                             %
@@ -552,7 +492,7 @@ MagickExport MagickBooleanType IsRightsAuthorized(const PolicyDomain domain,
   if (policy_info == (PolicyInfo *) NULL)
     return(MagickTrue);
   authorized=MagickTrue;
-  AcquireSemaphoreInfo(&policy_semaphore);
+  LockSemaphoreInfo(policy_semaphore);
   ResetLinkedListIterator(policy_list);
   p=(PolicyInfo *) GetNextValueInLinkedList(policy_list);
   while ((p != (PolicyInfo *) NULL) && (authorized != MagickFalse))
@@ -572,7 +512,7 @@ MagickExport MagickBooleanType IsRightsAuthorized(const PolicyDomain domain,
       }
     p=(PolicyInfo *) GetNextValueInLinkedList(policy_list);
   }
-  RelinquishSemaphoreInfo(policy_semaphore);
+  UnlockSemaphoreInfo(policy_semaphore);
   return(authorized);
 }
 \f
@@ -610,10 +550,10 @@ MagickExport MagickBooleanType ListPolicyInfo(FILE *file,
   const PolicyInfo
     **policy_info;
 
-  register long
+  register ssize_t
     i;
 
-  unsigned long
+  size_t
     number_policies;
 
   /*
@@ -625,7 +565,7 @@ MagickExport MagickBooleanType ListPolicyInfo(FILE *file,
   if (policy_info == (const PolicyInfo **) NULL)
     return(MagickFalse);
   path=(const char *) NULL;
-  for (i=0; i < (long) number_policies; i++)
+  for (i=0; i < (ssize_t) number_policies; i++)
   {
     if (policy_info[i]->stealth != MagickFalse)
       continue;
@@ -637,7 +577,8 @@ MagickExport MagickBooleanType ListPolicyInfo(FILE *file,
     domain=MagickOptionToMnemonic(MagickPolicyDomainOptions,
       policy_info[i]->domain);
     (void) fprintf(file,"  Policy: %s\n",domain);
-    if (policy_info[i]->domain == ResourcePolicyDomain)
+    if ((policy_info[i]->domain == ResourcePolicyDomain) ||
+        (policy_info[i]->domain == SystemPolicyDomain))
       {
         if (policy_info[i]->name != (char *) NULL)
           (void) fprintf(file,"    name: %s\n",policy_info[i]->name);
@@ -683,7 +624,7 @@ MagickExport MagickBooleanType ListPolicyInfo(FILE *file,
 %  The format of the LoadPolicyList method is:
 %
 %      MagickBooleanType LoadPolicyList(const char *xml,const char *filename,
-%        const unsigned long depth,ExceptionInfo *exception)
+%        const size_t depth,ExceptionInfo *exception)
 %
 %  A description of each parameter follows:
 %
@@ -697,7 +638,7 @@ MagickExport MagickBooleanType ListPolicyInfo(FILE *file,
 %
 */
 static MagickBooleanType LoadPolicyList(const char *xml,const char *filename,
-  const unsigned long depth,ExceptionInfo *exception)
+  const size_t depth,ExceptionInfo *exception)
 {
   char
     keyword[MaxTextExtent],
@@ -811,6 +752,7 @@ static MagickBooleanType LoadPolicyList(const char *xml,const char *filename,
           ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
         (void) ResetMagickMemory(policy_info,0,sizeof(*policy_info));
         policy_info->path=ConstantString(filename);
+        policy_info->exempt=MagickFalse;
         policy_info->signature=MagickSignature;
         continue;
       }
@@ -931,9 +873,6 @@ static MagickBooleanType LoadPolicyList(const char *xml,const char *filename,
 static MagickBooleanType LoadPolicyLists(const char *filename,
   ExceptionInfo *exception)
 {
-#if defined(MAGICKCORE_EMBEDDABLE_SUPPORT)
-  return(LoadPolicyList(PolicyMap,"built-in",0,exception));
-#else
   const StringInfo
     *option;
 
@@ -943,7 +882,56 @@ static MagickBooleanType LoadPolicyLists(const char *filename,
   MagickStatusType
     status;
 
+  register ssize_t
+    i;
+
+  /*
+    Load built-in policy map.
+  */
   status=MagickFalse;
+  if (policy_list == (LinkedListInfo *) NULL)
+    {
+      policy_list=NewLinkedList(0);
+      if (policy_list == (LinkedListInfo *) NULL)
+        {
+          ThrowFileException(exception,ResourceLimitError,
+            "MemoryAllocationFailed",filename);
+          return(MagickFalse);
+        }
+    }
+  for (i=0; i < (ssize_t) (sizeof(PolicyMap)/sizeof(*PolicyMap)); i++)
+  {
+    PolicyInfo
+      *policy_info;
+
+    register const PolicyMapInfo
+      *p;
+
+    p=PolicyMap+i;
+    policy_info=(PolicyInfo *) AcquireMagickMemory(sizeof(*policy_info));
+    if (policy_info == (PolicyInfo *) NULL)
+      {
+        (void) ThrowMagickException(exception,GetMagickModule(),
+          ResourceLimitError,"MemoryAllocationFailed","`%s'",policy_info->name);
+        continue;
+      }
+    (void) ResetMagickMemory(policy_info,0,sizeof(*policy_info));
+    policy_info->path=(char *) "[built-in]";
+    policy_info->domain=p->domain;
+    policy_info->rights=p->rights;
+    policy_info->name=(char *) p->name;
+    policy_info->pattern=(char *) p->pattern;
+    policy_info->value=(char *) p->value;
+    policy_info->exempt=MagickTrue;
+    policy_info->signature=MagickSignature;
+    status=AppendValueToLinkedList(policy_list,policy_info);
+    if (status == MagickFalse)
+      (void) ThrowMagickException(exception,GetMagickModule(),
+        ResourceLimitError,"MemoryAllocationFailed","`%s'",policy_info->name);
+  }
+  /*
+    Load external policy map.
+  */
   options=GetConfigureOptions(filename,exception);
   option=(const StringInfo *) GetNextValueInLinkedList(options);
   while (option != (const StringInfo *) NULL)
@@ -953,9 +941,81 @@ static MagickBooleanType LoadPolicyLists(const char *filename,
     option=(const StringInfo *) GetNextValueInLinkedList(options);
   }
   options=DestroyConfigureOptions(options);
-  if ((policy_list == (LinkedListInfo *) NULL) ||
-      (IsLinkedListEmpty(policy_list) != MagickFalse))
-    status|=LoadPolicyList(PolicyMap,"built-in",0,exception);
   return(status != 0 ? MagickTrue : MagickFalse);
-#endif
+}
+\f
+/*
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%                                                                             %
+%                                                                             %
+%                                                                             %
++   P o l i c y C o m p o n e n t G e n e s i s                               %
+%                                                                             %
+%                                                                             %
+%                                                                             %
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%
+%  PolicyComponentGenesis() instantiates the policy component.
+%
+%  The format of the PolicyComponentGenesis method is:
+%
+%      MagickBooleanType PolicyComponentGenesis(void)
+%
+*/
+MagickExport MagickBooleanType PolicyComponentGenesis(void)
+{
+  AcquireSemaphoreInfo(&policy_semaphore);
+  return(MagickTrue);
+}
+\f
+/*
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%                                                                             %
+%                                                                             %
+%                                                                             %
++   P o l i c y C o m p o n e n t T e r m i n u s                             %
+%                                                                             %
+%                                                                             %
+%                                                                             %
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%
+%  PolicyComponentTerminus() destroys the policy component.
+%
+%  The format of the PolicyComponentTerminus method is:
+%
+%      PolicyComponentTerminus(void)
+%
+*/
+
+static void *DestroyPolicyElement(void *policy_info)
+{
+  register PolicyInfo
+    *p;
+
+  p=(PolicyInfo *) policy_info;
+  if (p->exempt == MagickFalse)
+    {
+      if (p->value != (char *) NULL)
+        p->value=DestroyString(p->value);
+      if (p->pattern != (char *) NULL)
+        p->pattern=DestroyString(p->pattern);
+      if (p->name != (char *) NULL)
+        p->name=DestroyString(p->name);
+      if (p->path != (char *) NULL)
+        p->path=DestroyString(p->path);
+    }
+  p=(PolicyInfo *) RelinquishMagickMemory(p);
+  return((void *) NULL);
+}
+
+MagickExport void PolicyComponentTerminus(void)
+{
+  if (policy_semaphore == (SemaphoreInfo *) NULL)
+    AcquireSemaphoreInfo(&policy_semaphore);
+  LockSemaphoreInfo(policy_semaphore);
+  if (policy_list != (LinkedListInfo *) NULL)
+    policy_list=DestroyLinkedList(policy_list,DestroyPolicyElement);
+  instantiate_policy=MagickFalse;
+  UnlockSemaphoreInfo(policy_semaphore);
+  DestroySemaphoreInfo(&policy_semaphore);
 }