]> granicus.if.org Git - imagemagick/blobdiff - MagickCore/policy.c
Update web pages
[imagemagick] / MagickCore / policy.c
index e05907701e50b321cc557a32803d903c16299bb6..f3d1be3d1f6e00fa307dc1c2a0ccc77346543a33 100644 (file)
@@ -16,7 +16,7 @@
 %                                 July 1992                                   %
 %                                                                             %
 %                                                                             %
-%  Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization      %
+%  Copyright 1999-2015 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  %
@@ -58,6 +58,7 @@
 #include "MagickCore/utility.h"
 #include "MagickCore/utility-private.h"
 #include "MagickCore/xml-tree.h"
+#include "MagickCore/xml-tree-private.h"
 \f
 /*
   Define declarations.
@@ -120,7 +121,7 @@ static const PolicyMapInfo
   };
 
 static LinkedListInfo
-  *policy_list = (LinkedListInfo *) NULL;
+  *policy_cache = (LinkedListInfo *) NULL;
 
 static SemaphoreInfo
   *policy_semaphore = (SemaphoreInfo *) NULL;
@@ -129,8 +130,103 @@ static SemaphoreInfo
   Forward declarations.
 */
 static MagickBooleanType
-  InitializePolicyList(ExceptionInfo *),
-  LoadPolicyLists(const char *,ExceptionInfo *);
+  IsPolicyCacheInstantiated(ExceptionInfo *),
+  LoadPolicyCache(LinkedListInfo *,const char *,const char *,const size_t,
+    ExceptionInfo *);
+\f
+/*
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%                                                                             %
+%                                                                             %
+%                                                                             %
+%  A c q u i r e P o l i c y C a c h e                                        %
+%                                                                             %
+%                                                                             %
+%                                                                             %
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%
+%  AcquirePolicyCache() caches one or more policy configurations which provides
+%  a mapping between policy attributes and a policy name.
+%
+%  The format of the AcquirePolicyCache method is:
+%
+%      LinkedListInfo *AcquirePolicyCache(const char *filename,
+%        ExceptionInfo *exception)
+%
+%  A description of each parameter follows:
+%
+%    o filename: the font file name.
+%
+%    o exception: return any errors or warnings in this structure.
+%
+*/
+static LinkedListInfo *AcquirePolicyCache(const char *filename,
+  ExceptionInfo *exception)
+{
+  const StringInfo
+    *option;
+
+  LinkedListInfo
+    *options,
+    *policy_cache;
+
+  MagickStatusType
+    status;
+
+  register ssize_t
+    i;
+
+  /*
+    Load external policy map.
+  */
+  policy_cache=NewLinkedList(0);
+  if (policy_cache == (LinkedListInfo *) NULL)
+    ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
+  status=MagickTrue;
+  options=GetConfigureOptions(filename,exception);
+  option=(const StringInfo *) GetNextValueInLinkedList(options);
+  while (option != (const StringInfo *) NULL)
+  {
+    status&=LoadPolicyCache(policy_cache,(const char *)
+      GetStringInfoDatum(option),GetStringInfoPath(option),0,exception);
+    option=(const StringInfo *) GetNextValueInLinkedList(options);
+  }
+  options=DestroyConfigureOptions(options);
+  /*
+    Load built-in policy map.
+  */
+  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'",p->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=MagickCoreSignature;
+    status&=AppendValueToLinkedList(policy_cache,policy_info);
+    if (status == MagickFalse)
+      (void) ThrowMagickException(exception,GetMagickModule(),
+        ResourceLimitError,"MemoryAllocationFailed","`%s'",policy_info->name);
+  }
+  return(policy_cache);
+}
 \f
 /*
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -160,7 +256,7 @@ static MagickBooleanType
 static PolicyInfo *GetPolicyInfo(const char *name,ExceptionInfo *exception)
 {
   char
-    policyname[MaxTextExtent];
+    policyname[MagickPathExtent];
 
   register PolicyInfo
     *p;
@@ -169,37 +265,41 @@ static PolicyInfo *GetPolicyInfo(const char *name,ExceptionInfo *exception)
     *q;
 
   assert(exception != (ExceptionInfo *) NULL);
-  if (policy_list == (LinkedListInfo *) NULL)
-    if (InitializePolicyList(exception) == MagickFalse)
-      return((PolicyInfo *) NULL);
-  if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
-    return((PolicyInfo *) GetValueFromLinkedList(policy_list,0));
+  if (IsPolicyCacheInstantiated(exception) == MagickFalse)
+    return((PolicyInfo *) NULL);
   /*
     Strip names of whitespace.
   */
-  (void) CopyMagickString(policyname,name,MaxTextExtent);
+  *policyname='\0';
+  if (name != (const char *) NULL)
+    (void) CopyMagickString(policyname,name,MagickPathExtent);
   for (q=policyname; *q != '\0'; q++)
   {
     if (isspace((int) ((unsigned char) *q)) == 0)
       continue;
-    (void) CopyMagickString(q,q+1,MaxTextExtent);
+    (void) CopyMagickString(q,q+1,MagickPathExtent);
     q--;
   }
   /*
     Search for policy tag.
   */
   LockSemaphoreInfo(policy_semaphore);
-  ResetLinkedListIterator(policy_list);
-  p=(PolicyInfo *) GetNextValueInLinkedList(policy_list);
+  ResetLinkedListIterator(policy_cache);
+  p=(PolicyInfo *) GetNextValueInLinkedList(policy_cache);
+  if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
+    {
+      UnlockSemaphoreInfo(policy_semaphore);
+      return(p);
+    }
   while (p != (PolicyInfo *) NULL)
   {
     if (LocaleCompare(policyname,p->name) == 0)
       break;
-    p=(PolicyInfo *) GetNextValueInLinkedList(policy_list);
+    p=(PolicyInfo *) GetNextValueInLinkedList(policy_cache);
   }
   if (p != (PolicyInfo *) NULL)
-    (void) InsertValueInLinkedList(policy_list,0,
-      RemoveElementByValueFromLinkedList(policy_list,p));
+    (void) InsertValueInLinkedList(policy_cache,0,
+      RemoveElementByValueFromLinkedList(policy_cache,p));
   UnlockSemaphoreInfo(policy_semaphore);
   return(p);
 }
@@ -254,21 +354,21 @@ MagickExport const PolicyInfo **GetPolicyInfoList(const char *pattern,
   if (p == (const PolicyInfo *) NULL)
     return((const PolicyInfo **) NULL);
   policies=(const PolicyInfo **) AcquireQuantumMemory((size_t)
-    GetNumberOfElementsInLinkedList(policy_list)+1UL,sizeof(*policies));
+    GetNumberOfElementsInLinkedList(policy_cache)+1UL,sizeof(*policies));
   if (policies == (const PolicyInfo **) NULL)
     return((const PolicyInfo **) NULL);
   /*
     Generate policy list.
   */
   LockSemaphoreInfo(policy_semaphore);
-  ResetLinkedListIterator(policy_list);
-  p=(const PolicyInfo *) GetNextValueInLinkedList(policy_list);
+  ResetLinkedListIterator(policy_cache);
+  p=(const PolicyInfo *) GetNextValueInLinkedList(policy_cache);
   for (i=0; p != (const PolicyInfo *) NULL; )
   {
     if ((p->stealth == MagickFalse) &&
         (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
       policies[i++]=p;
-    p=(const PolicyInfo *) GetNextValueInLinkedList(policy_list);
+    p=(const PolicyInfo *) GetNextValueInLinkedList(policy_cache);
   }
   UnlockSemaphoreInfo(policy_semaphore);
   policies[i]=(PolicyInfo *) NULL;
@@ -326,21 +426,21 @@ MagickExport char **GetPolicyList(const char *pattern,
   if (p == (const PolicyInfo *) NULL)
     return((char **) NULL);
   policies=(char **) AcquireQuantumMemory((size_t)
-    GetNumberOfElementsInLinkedList(policy_list)+1UL,sizeof(*policies));
+    GetNumberOfElementsInLinkedList(policy_cache)+1UL,sizeof(*policies));
   if (policies == (char **) NULL)
     return((char **) NULL);
   /*
     Generate policy list.
   */
   LockSemaphoreInfo(policy_semaphore);
-  ResetLinkedListIterator(policy_list);
-  p=(const PolicyInfo *) GetNextValueInLinkedList(policy_list);
+  ResetLinkedListIterator(policy_cache);
+  p=(const PolicyInfo *) GetNextValueInLinkedList(policy_cache);
   for (i=0; p != (const PolicyInfo *) NULL; )
   {
     if ((p->stealth == MagickFalse) &&
         (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
       policies[i++]=ConstantString(p->name);
-    p=(const PolicyInfo *) GetNextValueInLinkedList(policy_list);
+    p=(const PolicyInfo *) GetNextValueInLinkedList(policy_cache);
   }
   UnlockSemaphoreInfo(policy_semaphore);
   policies[i]=(char *) NULL;
@@ -399,32 +499,36 @@ MagickExport char *GetPolicyValue(const char *name)
 %                                                                             %
 %                                                                             %
 %                                                                             %
-+   I n i t i a l i z e P o l i c y L i s t                                   %
++   I s P o l i c y C a c h e I n s t a n t i a t e d                         %
 %                                                                             %
 %                                                                             %
 %                                                                             %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
-%  InitializePolicyList() initializes the policy list.
+%  IsPolicyCacheInstantiated() determines if the policy list is instantiated.
+%  If not, it instantiates the list and returns it.
 %
-%  The format of the InitializePolicyList method is:
+%  The format of the IsPolicyInstantiated method is:
 %
-%      MagickBooleanType InitializePolicyList(ExceptionInfo *exception)
+%      MagickBooleanType IsPolicyCacheInstantiated(ExceptionInfo *exception)
 %
 %  A description of each parameter follows.
 %
 %    o exception: return any errors or warnings in this structure.
 %
 */
-static MagickBooleanType InitializePolicyList(ExceptionInfo *exception)
+static MagickBooleanType IsPolicyCacheInstantiated(ExceptionInfo *exception)
 {
-  if (policy_semaphore == (SemaphoreInfo *) NULL)
-    ActivateSemaphoreInfo(&policy_semaphore);
-  LockSemaphoreInfo(policy_semaphore);
-  if (policy_list == (LinkedListInfo *) NULL)
-    (void) LoadPolicyLists(PolicyFilename,exception);
-  UnlockSemaphoreInfo(policy_semaphore);
-  return(policy_list != (LinkedListInfo *) NULL ? MagickTrue : MagickFalse);
+  if (policy_cache == (LinkedListInfo *) NULL)
+    {
+      if (policy_semaphore == (SemaphoreInfo *) NULL)
+        ActivateSemaphoreInfo(&policy_semaphore);
+      LockSemaphoreInfo(policy_semaphore);
+      if (policy_cache == (LinkedListInfo *) NULL)
+        policy_cache=AcquirePolicyCache(PolicyFilename,exception);
+      UnlockSemaphoreInfo(policy_semaphore);
+    }
+  return(policy_cache != (LinkedListInfo *) NULL ? MagickTrue : MagickFalse);
 }
 \f
 /*
@@ -481,8 +585,8 @@ MagickExport MagickBooleanType IsRightsAuthorized(const PolicyDomain domain,
     return(MagickTrue);
   authorized=MagickTrue;
   LockSemaphoreInfo(policy_semaphore);
-  ResetLinkedListIterator(policy_list);
-  p=(PolicyInfo *) GetNextValueInLinkedList(policy_list);
+  ResetLinkedListIterator(policy_cache);
+  p=(PolicyInfo *) GetNextValueInLinkedList(policy_cache);
   while ((p != (PolicyInfo *) NULL) && (authorized != MagickFalse))
   {
     if ((p->domain == domain) &&
@@ -498,7 +602,7 @@ MagickExport MagickBooleanType IsRightsAuthorized(const PolicyDomain domain,
             ((p->rights & ExecutePolicyRights) == 0))
           authorized=MagickFalse;
       }
-    p=(PolicyInfo *) GetNextValueInLinkedList(policy_list);
+    p=(PolicyInfo *) GetNextValueInLinkedList(policy_cache);
   }
   UnlockSemaphoreInfo(policy_semaphore);
   return(authorized);
@@ -607,13 +711,14 @@ MagickExport MagickBooleanType ListPolicyInfo(FILE *file,
 %                                                                             %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
-%  LoadPolicyList() loads the policy configuration file which provides a mapping
+%  LoadPolicyCache() loads the policy configurations which provides a mapping
 %  between policy attributes and a policy domain.
 %
-%  The format of the LoadPolicyList method is:
+%  The format of the LoadPolicyCache method is:
 %
-%      MagickBooleanType LoadPolicyList(const char *xml,const char *filename,
-%        const size_t depth,ExceptionInfo *exception)
+%      MagickBooleanType LoadPolicyCache(LinkedListInfo *policy_cache,
+%        const char *xml,const char *filename,const size_t depth,
+%        ExceptionInfo *exception)
 %
 %  A description of each parameter follows:
 %
@@ -626,22 +731,23 @@ MagickExport MagickBooleanType ListPolicyInfo(FILE *file,
 %    o exception: return any errors or warnings in this structure.
 %
 */
-static MagickBooleanType LoadPolicyList(const char *xml,const char *filename,
-  const size_t depth,ExceptionInfo *exception)
+static MagickBooleanType LoadPolicyCache(LinkedListInfo *policy_cache,
+  const char *xml,const char *filename,const size_t depth,
+  ExceptionInfo *exception)
 {
   char
-    keyword[MaxTextExtent],
+    keyword[MagickPathExtent],
     *token;
 
-  PolicyInfo
-    *policy_info;
-
   const char
     *q;
 
-  MagickBooleanType
+  MagickStatusType
     status;
 
+  PolicyInfo
+    *policy_info;
+
   /*
     Load the policy map file.
   */
@@ -649,16 +755,6 @@ static MagickBooleanType LoadPolicyList(const char *xml,const char *filename,
     "Loading policy file \"%s\" ...",filename);
   if (xml == (char *) NULL)
     return(MagickFalse);
-  if (policy_list == (LinkedListInfo *) NULL)
-    {
-      policy_list=NewLinkedList(0);
-      if (policy_list == (LinkedListInfo *) NULL)
-        {
-          ThrowFileException(exception,ResourceLimitError,
-            "MemoryAllocationFailed",filename);
-          return(MagickFalse);
-        }
-    }
   status=MagickTrue;
   policy_info=(PolicyInfo *) NULL;
   token=AcquireString(xml);
@@ -670,7 +766,7 @@ static MagickBooleanType LoadPolicyList(const char *xml,const char *filename,
     GetMagickToken(q,&q,token);
     if (*token == '\0')
       break;
-    (void) CopyMagickString(keyword,token,MaxTextExtent);
+    (void) CopyMagickString(keyword,token,MagickPathExtent);
     if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
       {
         /*
@@ -696,7 +792,7 @@ static MagickBooleanType LoadPolicyList(const char *xml,const char *filename,
         */
         while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
         {
-          (void) CopyMagickString(keyword,token,MaxTextExtent);
+          (void) CopyMagickString(keyword,token,MagickPathExtent);
           GetMagickToken(q,&q,token);
           if (*token != '=')
             continue;
@@ -709,21 +805,22 @@ static MagickBooleanType LoadPolicyList(const char *xml,const char *filename,
               else
                 {
                   char
-                    path[MaxTextExtent],
+                    path[MagickPathExtent],
                     *xml;
 
                   GetPathComponent(filename,HeadPath,path);
                   if (*path != '\0')
                     (void) ConcatenateMagickString(path,DirectorySeparator,
-                      MaxTextExtent);
+                      MagickPathExtent);
                   if (*token == *DirectorySeparator)
-                    (void) CopyMagickString(path,token,MaxTextExtent);
+                    (void) CopyMagickString(path,token,MagickPathExtent);
                   else
-                    (void) ConcatenateMagickString(path,token,MaxTextExtent);
-                  xml=FileToString(path,~0UL,exception);
+                    (void) ConcatenateMagickString(path,token,MagickPathExtent);
+                  xml=FileToXML(path,~0UL);
                   if (xml != (char *) NULL)
                     {
-                      status=LoadPolicyList(xml,path,depth+1,exception);
+                      status&=LoadPolicyCache(policy_cache,xml,path,depth+1,
+                        exception);
                       xml=(char *) RelinquishMagickMemory(xml);
                     }
                 }
@@ -742,14 +839,14 @@ static MagickBooleanType LoadPolicyList(const char *xml,const char *filename,
         (void) ResetMagickMemory(policy_info,0,sizeof(*policy_info));
         policy_info->path=ConstantString(filename);
         policy_info->exempt=MagickFalse;
-        policy_info->signature=MagickSignature;
+        policy_info->signature=MagickCoreSignature;
         continue;
       }
     if (policy_info == (PolicyInfo *) NULL)
       continue;
     if (LocaleCompare(keyword,"/>") == 0)
       {
-        status=AppendValueToLinkedList(policy_list,policy_info);
+        status=AppendValueToLinkedList(policy_cache,policy_info);
         if (status == MagickFalse)
           (void) ThrowMagickException(exception,GetMagickModule(),
             ResourceLimitError,"MemoryAllocationFailed","`%s'",
@@ -831,106 +928,6 @@ static MagickBooleanType LoadPolicyList(const char *xml,const char *filename,
     }
   }
   token=(char *) RelinquishMagickMemory(token);
-  return(status);
-}
-\f
-/*
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%                                                                             %
-%                                                                             %
-%                                                                             %
-%  L o a d P o l i c y L i s t s                                              %
-%                                                                             %
-%                                                                             %
-%                                                                             %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%
-%  LoadPolicyList() loads one or more policy configuration file which provides a
-%  mapping between policy attributes and a policy name.
-%
-%  The format of the LoadPolicyLists method is:
-%
-%      MagickBooleanType LoadPolicyLists(const char *filename,
-%        ExceptionInfo *exception)
-%
-%  A description of each parameter follows:
-%
-%    o filename: the font file name.
-%
-%    o exception: return any errors or warnings in this structure.
-%
-*/
-static MagickBooleanType LoadPolicyLists(const char *filename,
-  ExceptionInfo *exception)
-{
-  const StringInfo
-    *option;
-
-  LinkedListInfo
-    *options;
-
-  MagickStatusType
-    status;
-
-  register ssize_t
-    i;
-
-  /*
-    Load external policy map.
-  */
-  if (policy_list == (LinkedListInfo *) NULL)
-    {
-      policy_list=NewLinkedList(0);
-      if (policy_list == (LinkedListInfo *) NULL)
-        {
-          ThrowFileException(exception,ResourceLimitError,
-            "MemoryAllocationFailed",filename);
-          return(MagickFalse);
-        }
-    }
-  status=MagickTrue;
-  options=GetConfigureOptions(filename,exception);
-  option=(const StringInfo *) GetNextValueInLinkedList(options);
-  while (option != (const StringInfo *) NULL)
-  {
-    status&=LoadPolicyList((const char *) GetStringInfoDatum(option),
-      GetStringInfoPath(option),0,exception);
-    option=(const StringInfo *) GetNextValueInLinkedList(options);
-  }
-  options=DestroyConfigureOptions(options);
-  /*
-    Load built-in policy map.
-  */
-  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'",p->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);
-  }
   return(status != 0 ? MagickTrue : MagickFalse);
 }
 \f
@@ -954,7 +951,8 @@ static MagickBooleanType LoadPolicyLists(const char *filename,
 */
 MagickPrivate MagickBooleanType PolicyComponentGenesis(void)
 {
-  policy_semaphore=AcquireSemaphoreInfo();
+  if (policy_semaphore == (SemaphoreInfo *) NULL)
+    policy_semaphore=AcquireSemaphoreInfo();
   return(MagickTrue);
 }
 \f
@@ -1003,8 +1001,8 @@ MagickPrivate void PolicyComponentTerminus(void)
   if (policy_semaphore == (SemaphoreInfo *) NULL)
     ActivateSemaphoreInfo(&policy_semaphore);
   LockSemaphoreInfo(policy_semaphore);
-  if (policy_list != (LinkedListInfo *) NULL)
-    policy_list=DestroyLinkedList(policy_list,DestroyPolicyElement);
+  if (policy_cache != (LinkedListInfo *) NULL)
+    policy_cache=DestroyLinkedList(policy_cache,DestroyPolicyElement);
   UnlockSemaphoreInfo(policy_semaphore);
   RelinquishSemaphoreInfo(&policy_semaphore);
 }