policy.c

Go to the documentation of this file.
00001 /*
00002 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00003 %                                                                             %
00004 %                                                                             %
00005 %                  PPPP    OOO   L      IIIII   CCCC  Y   Y                   %
00006 %                  P   P  O   O  L        I    C       Y Y                    %
00007 %                  PPPP   O   O  L        I    C        Y                     %
00008 %                  P      O   O  L        I    C        Y                     %
00009 %                  P       OOO   LLLLL  IIIII   CCCC    Y                     %
00010 %                                                                             %
00011 %                                                                             %
00012 %                         MagickCore Policy Methods                           %
00013 %                                                                             %
00014 %                              Software Design                                %
00015 %                                John Cristy                                  %
00016 %                                 July 1992                                   %
00017 %                                                                             %
00018 %                                                                             %
00019 %  Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization      %
00020 %  dedicated to making software imaging solutions freely available.           %
00021 %                                                                             %
00022 %  You may not use this file except in compliance with the License.  You may  %
00023 %  obtain a copy of the License at                                            %
00024 %                                                                             %
00025 %    http://www.imagemagick.org/script/license.php                            %
00026 %                                                                             %
00027 %  Unless required by applicable law or agreed to in writing, software        %
00028 %  distributed under the License is distributed on an "AS IS" BASIS,          %
00029 %  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
00030 %  See the License for the specific language governing permissions and        %
00031 %  limitations under the License.                                             %
00032 %                                                                             %
00033 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00034 %
00035 %  We use linked-lists because splay-trees do not currently support duplicate
00036 %  key / value pairs (.e.g X11 green compliance and SVG green compliance).
00037 %
00038 */
00039 
00040 /*
00041   Include declarations.
00042 */
00043 #include "magick/studio.h"
00044 #include "magick/client.h"
00045 #include "magick/configure.h"
00046 #include "magick/exception.h"
00047 #include "magick/exception-private.h"
00048 #include "magick/memory_.h"
00049 #include "magick/monitor.h"
00050 #include "magick/monitor-private.h"
00051 #include "magick/option.h"
00052 #include "magick/policy.h"
00053 #include "magick/semaphore.h"
00054 #include "magick/string_.h"
00055 #include "magick/token.h"
00056 #include "magick/utility.h"
00057 #include "magick/xml-tree.h"
00058 
00059 /*
00060   Define declarations.
00061 */
00062 #define PolicyFilename  "policy.xml"
00063 
00064 /*
00065   Typedef declarations.
00066 */
00067 struct _PolicyInfo
00068 {
00069   char
00070     *path;
00071 
00072   PolicyDomain
00073     domain;
00074 
00075   PolicyRights
00076     rights;
00077 
00078   char
00079     *name,
00080     *pattern,
00081     *value;
00082 
00083   MagickBooleanType
00084     exempt,
00085     stealth,
00086     debug;
00087 
00088   SemaphoreInfo
00089     *semaphore;
00090 
00091   unsigned long
00092     signature;
00093 };
00094 
00095 typedef struct _PolicyMapInfo
00096 {
00097   const PolicyDomain
00098     domain;
00099 
00100   const PolicyRights
00101     rights;
00102 
00103   const char
00104     *name,
00105     *pattern,
00106     *value;
00107 } PolicyMapInfo;
00108 
00109 /*
00110   Static declarations.
00111 */
00112 static const PolicyMapInfo
00113   PolicyMap[] =
00114   {
00115     { UndefinedPolicyDomain, UndefinedPolicyRights, (const char *) NULL,
00116       (const char *) NULL, (const char *) NULL }
00117   };
00118 
00119 static LinkedListInfo
00120   *policy_list = (LinkedListInfo *) NULL;
00121 
00122 static SemaphoreInfo
00123   *policy_semaphore = (SemaphoreInfo *) NULL;
00124 
00125 static volatile MagickBooleanType
00126   instantiate_policy = MagickFalse;
00127 
00128 /*
00129   Forward declarations.
00130 */
00131 static MagickBooleanType
00132   InitializePolicyList(ExceptionInfo *),
00133   LoadPolicyLists(const char *,ExceptionInfo *);
00134 
00135 /*
00136 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00137 %                                                                             %
00138 %                                                                             %
00139 %                                                                             %
00140 +   G e t P o l i c y I n f o                                                 %
00141 %                                                                             %
00142 %                                                                             %
00143 %                                                                             %
00144 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00145 %
00146 %  GetPolicyInfo() searches the policy list for the specified name and if found
00147 %  returns attributes for that policy.
00148 %
00149 %  The format of the GetPolicyInfo method is:
00150 %
00151 %      PolicyInfo *GetPolicyInfo(const char *name,ExceptionInfo *exception)
00152 %
00153 %  A description of each parameter follows:
00154 %
00155 %    o name: the policy name.
00156 %
00157 %    o exception: return any errors or warnings in this structure.
00158 %
00159 */
00160 static PolicyInfo *GetPolicyInfo(const char *name,ExceptionInfo *exception)
00161 {
00162   char
00163     policyname[MaxTextExtent];
00164 
00165   register PolicyInfo
00166     *p;
00167 
00168   register char
00169     *q;
00170 
00171   assert(exception != (ExceptionInfo *) NULL);
00172   if ((policy_list == (LinkedListInfo *) NULL) ||
00173       (instantiate_policy == MagickFalse))
00174     if (InitializePolicyList(exception) == MagickFalse)
00175       return((PolicyInfo *) NULL);
00176   if ((policy_list == (LinkedListInfo *) NULL) ||
00177       (IsLinkedListEmpty(policy_list) != MagickFalse))
00178     return((PolicyInfo *) NULL);
00179   if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
00180     return((PolicyInfo *) GetValueFromLinkedList(policy_list,0));
00181   /*
00182     Strip names of whitespace.
00183   */
00184   (void) CopyMagickString(policyname,name,MaxTextExtent);
00185   for (q=policyname; *q != '\0'; q++)
00186   {
00187     if (isspace((int) ((unsigned char) *q)) == 0)
00188       continue;
00189     (void) CopyMagickString(q,q+1,MaxTextExtent);
00190     q--;
00191   }
00192   /*
00193     Search for policy tag.
00194   */
00195   LockSemaphoreInfo(policy_semaphore);
00196   ResetLinkedListIterator(policy_list);
00197   p=(PolicyInfo *) GetNextValueInLinkedList(policy_list);
00198   while (p != (PolicyInfo *) NULL)
00199   {
00200     if (LocaleCompare(policyname,p->name) == 0)
00201       break;
00202     p=(PolicyInfo *) GetNextValueInLinkedList(policy_list);
00203   }
00204   if (p != (PolicyInfo *) NULL)
00205     (void) InsertValueInLinkedList(policy_list,0,
00206       RemoveElementByValueFromLinkedList(policy_list,p));
00207   UnlockSemaphoreInfo(policy_semaphore);
00208   return(p);
00209 }
00210 
00211 /*
00212 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00213 %                                                                             %
00214 %                                                                             %
00215 %                                                                             %
00216 %   G e t P o l i c y I n f o L i s t                                         %
00217 %                                                                             %
00218 %                                                                             %
00219 %                                                                             %
00220 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00221 %
00222 %  GetPolicyInfoList() returns any policies that match the specified pattern.
00223 %
00224 %  The format of the GetPolicyInfoList function is:
00225 %
00226 %      const PolicyInfo **GetPolicyInfoList(const char *pattern,
00227 %        unsigned long *number_policies,ExceptionInfo *exception)
00228 %
00229 %  A description of each parameter follows:
00230 %
00231 %    o pattern: Specifies a pointer to a text string containing a pattern.
00232 %
00233 %    o number_policies:  returns the number of policies in the list.
00234 %
00235 %    o exception: return any errors or warnings in this structure.
00236 %
00237 */
00238 MagickExport const PolicyInfo **GetPolicyInfoList(const char *pattern,
00239   unsigned long *number_policies,ExceptionInfo *exception)
00240 {
00241   const PolicyInfo
00242     **policies;
00243 
00244   register const PolicyInfo
00245     *p;
00246 
00247   register long
00248     i;
00249 
00250   /*
00251     Allocate policy list.
00252   */
00253   assert(pattern != (char *) NULL);
00254   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
00255   assert(number_policies != (unsigned long *) NULL);
00256   *number_policies=0;
00257   p=GetPolicyInfo("*",exception);
00258   if (p == (const PolicyInfo *) NULL)
00259     return((const PolicyInfo **) NULL);
00260   policies=(const PolicyInfo **) AcquireQuantumMemory((size_t)
00261     GetNumberOfElementsInLinkedList(policy_list)+1UL,sizeof(*policies));
00262   if (policies == (const PolicyInfo **) NULL)
00263     return((const PolicyInfo **) NULL);
00264   /*
00265     Generate policy list.
00266   */
00267   LockSemaphoreInfo(policy_semaphore);
00268   ResetLinkedListIterator(policy_list);
00269   p=(const PolicyInfo *) GetNextValueInLinkedList(policy_list);
00270   for (i=0; p != (const PolicyInfo *) NULL; )
00271   {
00272     if ((p->stealth == MagickFalse) &&
00273         (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
00274       policies[i++]=p;
00275     p=(const PolicyInfo *) GetNextValueInLinkedList(policy_list);
00276   }
00277   UnlockSemaphoreInfo(policy_semaphore);
00278   policies[i]=(PolicyInfo *) NULL;
00279   *number_policies=(unsigned long) i;
00280   return(policies);
00281 }
00282 
00283 /*
00284 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00285 %                                                                             %
00286 %                                                                             %
00287 %                                                                             %
00288 %   G e t P o l i c y L i s t                                                 %
00289 %                                                                             %
00290 %                                                                             %
00291 %                                                                             %
00292 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00293 %
00294 %  GetPolicyList() returns any policies that match the specified pattern.
00295 %
00296 %  The format of the GetPolicyList function is:
00297 %
00298 %      char **GetPolicyList(const char *pattern,unsigned long *number_policies,
00299 %        ExceptionInfo *exception)
00300 %
00301 %  A description of each parameter follows:
00302 %
00303 %    o pattern: a pointer to a text string containing a pattern.
00304 %
00305 %    o number_policies:  returns the number of policies in the list.
00306 %
00307 %    o exception: return any errors or warnings in this structure.
00308 %
00309 */
00310 MagickExport char **GetPolicyList(const char *pattern,
00311   unsigned long *number_policies,ExceptionInfo *exception)
00312 {
00313   char
00314     **policies;
00315 
00316   register const PolicyInfo
00317     *p;
00318 
00319   register long
00320     i;
00321 
00322   /*
00323     Allocate policy list.
00324   */
00325   assert(pattern != (char *) NULL);
00326   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
00327   assert(number_policies != (unsigned long *) NULL);
00328   *number_policies=0;
00329   p=GetPolicyInfo("*",exception);
00330   if (p == (const PolicyInfo *) NULL)
00331     return((char **) NULL);
00332   policies=(char **) AcquireQuantumMemory((size_t)
00333     GetNumberOfElementsInLinkedList(policy_list)+1UL,sizeof(*policies));
00334   if (policies == (char **) NULL)
00335     return((char **) NULL);
00336   /*
00337     Generate policy list.
00338   */
00339   LockSemaphoreInfo(policy_semaphore);
00340   ResetLinkedListIterator(policy_list);
00341   p=(const PolicyInfo *) GetNextValueInLinkedList(policy_list);
00342   for (i=0; p != (const PolicyInfo *) NULL; )
00343   {
00344     if ((p->stealth == MagickFalse) &&
00345         (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
00346       policies[i++]=ConstantString(p->name);
00347     p=(const PolicyInfo *) GetNextValueInLinkedList(policy_list);
00348   }
00349   UnlockSemaphoreInfo(policy_semaphore);
00350   policies[i]=(char *) NULL;
00351   *number_policies=(unsigned long) i;
00352   return(policies);
00353 }
00354 
00355 /*
00356 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00357 %                                                                             %
00358 %                                                                             %
00359 %                                                                             %
00360 %   G e t P o l i c y V a l u e                                               %
00361 %                                                                             %
00362 %                                                                             %
00363 %                                                                             %
00364 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00365 %
00366 %  GetPolicyValue() returns the value associated with the named policy.
00367 %
00368 %  The format of the GetPolicyValue method is:
00369 %
00370 %      char *GetPolicyValue(const char *name)
00371 %
00372 %  A description of each parameter follows:
00373 %
00374 %    o policy_info:  The policy info.
00375 %
00376 */
00377 MagickExport char *GetPolicyValue(const char *name)
00378 {
00379   const char
00380     *value;
00381 
00382   const PolicyInfo
00383     *policy_info;
00384 
00385   ExceptionInfo
00386     *exception;
00387 
00388   assert(name != (const char *) NULL);
00389   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",name);
00390   exception=AcquireExceptionInfo();
00391   policy_info=GetPolicyInfo(name,exception);
00392   exception=DestroyExceptionInfo(exception);
00393   if (policy_info == (PolicyInfo *) NULL)
00394     return((char *) NULL);
00395   value=policy_info->value;
00396   if ((value == (const char *) NULL) || (*value == '\0'))
00397     return((char *) NULL);
00398   return(ConstantString(value));
00399 }
00400 
00401 /*
00402 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00403 %                                                                             %
00404 %                                                                             %
00405 %                                                                             %
00406 +   I n i t i a l i z e P o l i c y L i s t                                   %
00407 %                                                                             %
00408 %                                                                             %
00409 %                                                                             %
00410 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00411 %
00412 %  InitializePolicyList() initializes the policy list.
00413 %
00414 %  The format of the InitializePolicyList method is:
00415 %
00416 %      MagickBooleanType InitializePolicyList(ExceptionInfo *exception)
00417 %
00418 %  A description of each parameter follows.
00419 %
00420 %    o exception: return any errors or warnings in this structure.
00421 %
00422 */
00423 static MagickBooleanType InitializePolicyList(ExceptionInfo *exception)
00424 {
00425   if ((policy_list == (LinkedListInfo *) NULL) &&
00426       (instantiate_policy == MagickFalse))
00427     {
00428       if (policy_semaphore == (SemaphoreInfo *) NULL)
00429         AcquireSemaphoreInfo(&policy_semaphore);
00430       LockSemaphoreInfo(policy_semaphore);
00431       if ((policy_list == (LinkedListInfo *) NULL) &&
00432           (instantiate_policy == MagickFalse))
00433         {
00434           (void) LoadPolicyLists(PolicyFilename,exception);
00435           instantiate_policy=MagickTrue;
00436         }
00437       UnlockSemaphoreInfo(policy_semaphore);
00438     }
00439   return(policy_list != (LinkedListInfo *) NULL ? MagickTrue : MagickFalse);
00440 }
00441 
00442 /*
00443 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00444 %                                                                             %
00445 %                                                                             %
00446 %                                                                             %
00447 %   I s R i g h t s A u t h o r i z e d                                       %
00448 %                                                                             %
00449 %                                                                             %
00450 %                                                                             %
00451 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00452 %
00453 %  IsRightsAuthorized() returns MagickTrue if the policy authorizes the
00454 %  requested rights for the specified domain.
00455 %
00456 %  The format of the IsRightsAuthorized method is:
00457 %
00458 %      MagickBooleanType IsRightsAuthorized(const PolicyDomain domain,
00459 %        const PolicyRights rights,const char *pattern)
00460 %
00461 %  A description of each parameter follows:
00462 %
00463 %    o domain: the policy domain.
00464 %
00465 %    o rights: the policy rights.
00466 %
00467 %    o pattern: the coder, delegate, filter, or path pattern.
00468 %
00469 */
00470 MagickExport MagickBooleanType IsRightsAuthorized(const PolicyDomain domain,
00471   const PolicyRights rights,const char *pattern)
00472 {
00473   const PolicyInfo
00474     *policy_info;
00475 
00476   ExceptionInfo
00477     *exception;
00478 
00479   MagickBooleanType
00480     authorized;
00481 
00482   register PolicyInfo
00483     *p;
00484 
00485   (void) LogMagickEvent(PolicyEvent,GetMagickModule(),
00486     "Domain: %s; rights=%s; pattern=\"%s\" ...",
00487     MagickOptionToMnemonic(MagickPolicyDomainOptions,domain),
00488     MagickOptionToMnemonic(MagickPolicyRightsOptions,rights),pattern);
00489   exception=AcquireExceptionInfo();
00490   policy_info=GetPolicyInfo("*",exception);
00491   exception=DestroyExceptionInfo(exception);
00492   if (policy_info == (PolicyInfo *) NULL)
00493     return(MagickTrue);
00494   authorized=MagickTrue;
00495   LockSemaphoreInfo(policy_semaphore);
00496   ResetLinkedListIterator(policy_list);
00497   p=(PolicyInfo *) GetNextValueInLinkedList(policy_list);
00498   while ((p != (PolicyInfo *) NULL) && (authorized != MagickFalse))
00499   {
00500     if ((p->domain == domain) &&
00501         (GlobExpression(pattern,p->pattern,MagickFalse) != MagickFalse))
00502       {
00503         if (((rights & ReadPolicyRights) != 0) &&
00504             ((p->rights & ReadPolicyRights) == 0))
00505           authorized=MagickFalse;
00506         if (((rights & WritePolicyRights) != 0) &&
00507             ((p->rights & WritePolicyRights) == 0))
00508           authorized=MagickFalse;
00509         if (((rights & ExecutePolicyRights) != 0) &&
00510             ((p->rights & ExecutePolicyRights) == 0))
00511           authorized=MagickFalse;
00512       }
00513     p=(PolicyInfo *) GetNextValueInLinkedList(policy_list);
00514   }
00515   UnlockSemaphoreInfo(policy_semaphore);
00516   return(authorized);
00517 }
00518 
00519 /*
00520 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00521 %                                                                             %
00522 %                                                                             %
00523 %                                                                             %
00524 %  L i s t P o l i c y I n f o                                                %
00525 %                                                                             %
00526 %                                                                             %
00527 %                                                                             %
00528 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00529 %
00530 %  ListPolicyInfo() lists policies to the specified file.
00531 %
00532 %  The format of the ListPolicyInfo method is:
00533 %
00534 %      MagickBooleanType ListPolicyInfo(FILE *file,ExceptionInfo *exception)
00535 %
00536 %  A description of each parameter follows.
00537 %
00538 %    o file:  List policy names to this file handle.
00539 %
00540 %    o exception: return any errors or warnings in this structure.
00541 %
00542 */
00543 MagickExport MagickBooleanType ListPolicyInfo(FILE *file,
00544   ExceptionInfo *exception)
00545 {
00546   const char
00547     *path,
00548     *domain;
00549 
00550   const PolicyInfo
00551     **policy_info;
00552 
00553   register long
00554     i;
00555 
00556   unsigned long
00557     number_policies;
00558 
00559   /*
00560     List name and attributes of each policy in the list.
00561   */
00562   if (file == (const FILE *) NULL)
00563     file=stdout;
00564   policy_info=GetPolicyInfoList("*",&number_policies,exception);
00565   if (policy_info == (const PolicyInfo **) NULL)
00566     return(MagickFalse);
00567   path=(const char *) NULL;
00568   for (i=0; i < (long) number_policies; i++)
00569   {
00570     if (policy_info[i]->stealth != MagickFalse)
00571       continue;
00572     if (((path == (const char *) NULL) ||
00573          (LocaleCompare(path,policy_info[i]->path) != 0)) &&
00574          (policy_info[i]->path != (char *) NULL))
00575       (void) fprintf(file,"\nPath: %s\n",policy_info[i]->path);
00576     path=policy_info[i]->path;
00577     domain=MagickOptionToMnemonic(MagickPolicyDomainOptions,
00578       policy_info[i]->domain);
00579     (void) fprintf(file,"  Policy: %s\n",domain);
00580     if ((policy_info[i]->domain == ResourcePolicyDomain) ||
00581         (policy_info[i]->domain == SystemPolicyDomain))
00582       {
00583         if (policy_info[i]->name != (char *) NULL)
00584           (void) fprintf(file,"    name: %s\n",policy_info[i]->name);
00585         if (policy_info[i]->value != (char *) NULL)
00586           (void) fprintf(file,"    value: %s\n",policy_info[i]->value);
00587       }
00588     else
00589       {
00590         (void) fprintf(file,"    rights: ");
00591         if (policy_info[i]->rights == NoPolicyRights)
00592           (void) fprintf(file,"None ");
00593         if ((policy_info[i]->rights & ReadPolicyRights) != 0)
00594           (void) fprintf(file,"Read ");
00595         if ((policy_info[i]->rights & WritePolicyRights) != 0)
00596           (void) fprintf(file,"Write ");
00597         if ((policy_info[i]->rights & ExecutePolicyRights) != 0)
00598           (void) fprintf(file,"Execute ");
00599         (void) fprintf(file,"\n");
00600         if (policy_info[i]->pattern != (char *) NULL)
00601           (void) fprintf(file,"    pattern: %s\n",policy_info[i]->pattern);
00602       }
00603   }
00604   policy_info=(const PolicyInfo **) RelinquishMagickMemory((void *)
00605     policy_info);
00606   (void) fflush(file);
00607   return(MagickTrue);
00608 }
00609 
00610 /*
00611 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00612 %                                                                             %
00613 %                                                                             %
00614 %                                                                             %
00615 +   L o a d P o l i c y L i s t                                               %
00616 %                                                                             %
00617 %                                                                             %
00618 %                                                                             %
00619 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00620 %
00621 %  LoadPolicyList() loads the policy configuration file which provides a mapping
00622 %  between policy attributes and a policy domain.
00623 %
00624 %  The format of the LoadPolicyList method is:
00625 %
00626 %      MagickBooleanType LoadPolicyList(const char *xml,const char *filename,
00627 %        const unsigned long depth,ExceptionInfo *exception)
00628 %
00629 %  A description of each parameter follows:
00630 %
00631 %    o xml:  The policy list in XML format.
00632 %
00633 %    o filename:  The policy list filename.
00634 %
00635 %    o depth: depth of <include /> statements.
00636 %
00637 %    o exception: return any errors or warnings in this structure.
00638 %
00639 */
00640 static MagickBooleanType LoadPolicyList(const char *xml,const char *filename,
00641   const unsigned long depth,ExceptionInfo *exception)
00642 {
00643   char
00644     keyword[MaxTextExtent],
00645     *token;
00646 
00647   PolicyInfo
00648     *policy_info;
00649 
00650   const char
00651     *q;
00652 
00653   MagickBooleanType
00654     status;
00655 
00656   /*
00657     Load the policy map file.
00658   */
00659   (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
00660     "Loading policy file \"%s\" ...",filename);
00661   if (xml == (char *) NULL)
00662     return(MagickFalse);
00663   if (policy_list == (LinkedListInfo *) NULL)
00664     {
00665       policy_list=NewLinkedList(0);
00666       if (policy_list == (LinkedListInfo *) NULL)
00667         {
00668           ThrowFileException(exception,ResourceLimitError,
00669             "MemoryAllocationFailed",filename);
00670           return(MagickFalse);
00671         }
00672     }
00673   status=MagickTrue;
00674   policy_info=(PolicyInfo *) NULL;
00675   token=AcquireString(xml);
00676   for (q=(const char *) xml; *q != '\0'; )
00677   {
00678     /*
00679       Interpret XML.
00680     */
00681     GetMagickToken(q,&q,token);
00682     if (*token == '\0')
00683       break;
00684     (void) CopyMagickString(keyword,token,MaxTextExtent);
00685     if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
00686       {
00687         /*
00688           Docdomain element.
00689         */
00690         while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
00691           GetMagickToken(q,&q,token);
00692         continue;
00693       }
00694     if (LocaleNCompare(keyword,"<!--",4) == 0)
00695       {
00696         /*
00697           Comment element.
00698         */
00699         while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
00700           GetMagickToken(q,&q,token);
00701         continue;
00702       }
00703     if (LocaleCompare(keyword,"<include") == 0)
00704       {
00705         /*
00706           Include element.
00707         */
00708         while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
00709         {
00710           (void) CopyMagickString(keyword,token,MaxTextExtent);
00711           GetMagickToken(q,&q,token);
00712           if (*token != '=')
00713             continue;
00714           GetMagickToken(q,&q,token);
00715           if (LocaleCompare(keyword,"file") == 0)
00716             {
00717               if (depth > 200)
00718                 (void) ThrowMagickException(exception,GetMagickModule(),
00719                   ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
00720               else
00721                 {
00722                   char
00723                     path[MaxTextExtent],
00724                     *xml;
00725 
00726                   GetPathComponent(filename,HeadPath,path);
00727                   if (*path != '\0')
00728                     (void) ConcatenateMagickString(path,DirectorySeparator,
00729                       MaxTextExtent);
00730                   if (*token == *DirectorySeparator)
00731                     (void) CopyMagickString(path,token,MaxTextExtent);
00732                   else
00733                     (void) ConcatenateMagickString(path,token,MaxTextExtent);
00734                   xml=FileToString(path,~0,exception);
00735                   if (xml != (char *) NULL)
00736                     {
00737                       status=LoadPolicyList(xml,path,depth+1,exception);
00738                       xml=(char *) RelinquishMagickMemory(xml);
00739                     }
00740                 }
00741             }
00742         }
00743         continue;
00744       }
00745     if (LocaleCompare(keyword,"<policy") == 0)
00746       {
00747         /*
00748           Policy element.
00749         */
00750         policy_info=(PolicyInfo *) AcquireAlignedMemory(1,sizeof(*policy_info));
00751         if (policy_info == (PolicyInfo *) NULL)
00752           ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
00753         (void) ResetMagickMemory(policy_info,0,sizeof(*policy_info));
00754         policy_info->path=ConstantString(filename);
00755         policy_info->exempt=MagickFalse;
00756         policy_info->signature=MagickSignature;
00757         continue;
00758       }
00759     if (policy_info == (PolicyInfo *) NULL)
00760       continue;
00761     if (LocaleCompare(keyword,"/>") == 0)
00762       {
00763         status=AppendValueToLinkedList(policy_list,policy_info);
00764         if (status == MagickFalse)
00765           (void) ThrowMagickException(exception,GetMagickModule(),
00766             ResourceLimitError,"MemoryAllocationFailed","`%s'",
00767             policy_info->name);
00768         policy_info=(PolicyInfo *) NULL;
00769       }
00770     GetMagickToken(q,(const char **) NULL,token);
00771     if (*token != '=')
00772       continue;
00773     GetMagickToken(q,&q,token);
00774     GetMagickToken(q,&q,token);
00775     switch (*keyword)
00776     {
00777       case 'D':
00778       case 'd':
00779       {
00780         if (LocaleCompare((char *) keyword,"domain") == 0)
00781           {
00782             policy_info->domain=(PolicyDomain) ParseMagickOption(
00783               MagickPolicyDomainOptions,MagickTrue,token);
00784             break;
00785           }
00786         break;
00787       }
00788       case 'N':
00789       case 'n':
00790       {
00791         if (LocaleCompare((char *) keyword,"name") == 0)
00792           {
00793             policy_info->name=ConstantString(token);
00794             break;
00795           }
00796         break;
00797       }
00798       case 'P':
00799       case 'p':
00800       {
00801         if (LocaleCompare((char *) keyword,"pattern") == 0)
00802           {
00803             policy_info->pattern=ConstantString(token);
00804             break;
00805           }
00806         break;
00807       }
00808       case 'R':
00809       case 'r':
00810       {
00811         if (LocaleCompare((char *) keyword,"rights") == 0)
00812           {
00813             policy_info->rights=(PolicyRights) ParseMagickOption(
00814               MagickPolicyRightsOptions,MagickTrue,token);
00815             break;
00816           }
00817         break;
00818       }
00819       case 'S':
00820       case 's':
00821       {
00822         if (LocaleCompare((char *) keyword,"stealth") == 0)
00823           {
00824             policy_info->stealth=IsMagickTrue(token);
00825             break;
00826           }
00827         break;
00828       }
00829       case 'V':
00830       case 'v':
00831       {
00832         if (LocaleCompare((char *) keyword,"value") == 0)
00833           {
00834             policy_info->value=ConstantString(token);
00835             break;
00836           }
00837         break;
00838       }
00839       default:
00840         break;
00841     }
00842   }
00843   token=(char *) RelinquishMagickMemory(token);
00844   return(status);
00845 }
00846 
00847 /*
00848 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00849 %                                                                             %
00850 %                                                                             %
00851 %                                                                             %
00852 %  L o a d P o l i c y L i s t s                                              %
00853 %                                                                             %
00854 %                                                                             %
00855 %                                                                             %
00856 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00857 %
00858 %  LoadPolicyList() loads one or more policy configuration file which provides a
00859 %  mapping between policy attributes and a policy name.
00860 %
00861 %  The format of the LoadPolicyLists method is:
00862 %
00863 %      MagickBooleanType LoadPolicyLists(const char *filename,
00864 %        ExceptionInfo *exception)
00865 %
00866 %  A description of each parameter follows:
00867 %
00868 %    o filename: the font file name.
00869 %
00870 %    o exception: return any errors or warnings in this structure.
00871 %
00872 */
00873 static MagickBooleanType LoadPolicyLists(const char *filename,
00874   ExceptionInfo *exception)
00875 {
00876   const StringInfo
00877     *option;
00878 
00879   LinkedListInfo
00880     *options;
00881 
00882   MagickStatusType
00883     status;
00884 
00885   register long
00886     i;
00887 
00888   /*
00889     Load built-in policy map.
00890   */
00891   status=MagickFalse;
00892   if (policy_list == (LinkedListInfo *) NULL)
00893     {
00894       policy_list=NewLinkedList(0);
00895       if (policy_list == (LinkedListInfo *) NULL)
00896         {
00897           ThrowFileException(exception,ResourceLimitError,
00898             "MemoryAllocationFailed",filename);
00899           return(MagickFalse);
00900         }
00901     }
00902   for (i=0; i < (long) (sizeof(PolicyMap)/sizeof(*PolicyMap)); i++)
00903   {
00904     PolicyInfo
00905       *policy_info;
00906 
00907     register const PolicyMapInfo
00908       *p;
00909 
00910     p=PolicyMap+i;
00911     policy_info=(PolicyInfo *) AcquireAlignedMemory(1,sizeof(*policy_info));
00912     if (policy_info == (PolicyInfo *) NULL)
00913       {
00914         (void) ThrowMagickException(exception,GetMagickModule(),
00915           ResourceLimitError,"MemoryAllocationFailed","`%s'",policy_info->name);
00916         continue;
00917       }
00918     (void) ResetMagickMemory(policy_info,0,sizeof(*policy_info));
00919     policy_info->path=(char *) "[built-in]";
00920     policy_info->domain=p->domain;
00921     policy_info->rights=p->rights;
00922     policy_info->name=(char *) p->name;
00923     policy_info->pattern=(char *) p->pattern;
00924     policy_info->value=(char *) p->value;
00925     policy_info->exempt=MagickTrue;
00926     policy_info->signature=MagickSignature;
00927     status=AppendValueToLinkedList(policy_list,policy_info);
00928     if (status == MagickFalse)
00929       (void) ThrowMagickException(exception,GetMagickModule(),
00930         ResourceLimitError,"MemoryAllocationFailed","`%s'",policy_info->name);
00931   }
00932   /*
00933     Load external policy map.
00934   */
00935   options=GetConfigureOptions(filename,exception);
00936   option=(const StringInfo *) GetNextValueInLinkedList(options);
00937   while (option != (const StringInfo *) NULL)
00938   {
00939     status|=LoadPolicyList((const char *) GetStringInfoDatum(option),
00940       GetStringInfoPath(option),0,exception);
00941     option=(const StringInfo *) GetNextValueInLinkedList(options);
00942   }
00943   options=DestroyConfigureOptions(options);
00944   return(status != 0 ? MagickTrue : MagickFalse);
00945 }
00946 
00947 /*
00948 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00949 %                                                                             %
00950 %                                                                             %
00951 %                                                                             %
00952 +   P o l i c y C o m p o n e n t G e n e s i s                               %
00953 %                                                                             %
00954 %                                                                             %
00955 %                                                                             %
00956 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00957 %
00958 %  PolicyComponentGenesis() instantiates the policy component.
00959 %
00960 %  The format of the PolicyComponentGenesis method is:
00961 %
00962 %      MagickBooleanType PolicyComponentGenesis(void)
00963 %
00964 */
00965 MagickExport MagickBooleanType PolicyComponentGenesis(void)
00966 {
00967   AcquireSemaphoreInfo(&policy_semaphore);
00968   return(MagickTrue);
00969 }
00970 
00971 /*
00972 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00973 %                                                                             %
00974 %                                                                             %
00975 %                                                                             %
00976 +   P o l i c y C o m p o n e n t T e r m i n u s                             %
00977 %                                                                             %
00978 %                                                                             %
00979 %                                                                             %
00980 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00981 %
00982 %  PolicyComponentTerminus() destroys the policy component.
00983 %
00984 %  The format of the PolicyComponentTerminus method is:
00985 %
00986 %      PolicyComponentTerminus(void)
00987 %
00988 */
00989 
00990 static void *DestroyPolicyElement(void *policy_info)
00991 {
00992   register PolicyInfo
00993     *p;
00994 
00995   p=(PolicyInfo *) policy_info;
00996   if (p->exempt == MagickFalse)
00997     {
00998       if (p->value != (char *) NULL)
00999         p->value=DestroyString(p->value);
01000       if (p->pattern != (char *) NULL)
01001         p->pattern=DestroyString(p->pattern);
01002       if (p->name != (char *) NULL)
01003         p->name=DestroyString(p->name);
01004       if (p->path != (char *) NULL)
01005         p->path=DestroyString(p->path);
01006     }
01007   p=(PolicyInfo *) RelinquishMagickMemory(p);
01008   return((void *) NULL);
01009 }
01010 
01011 MagickExport void PolicyComponentTerminus(void)
01012 {
01013   if (policy_semaphore == (SemaphoreInfo *) NULL)
01014     AcquireSemaphoreInfo(&policy_semaphore);
01015   LockSemaphoreInfo(policy_semaphore);
01016   if (policy_list != (LinkedListInfo *) NULL)
01017     policy_list=DestroyLinkedList(policy_list,DestroyPolicyElement);
01018   instantiate_policy=MagickFalse;
01019   UnlockSemaphoreInfo(policy_semaphore);
01020   DestroySemaphoreInfo(&policy_semaphore);
01021 }
Generated by  doxygen 1.6.2-20100208