blob: 8d20a117c1253684913d2d554b20f9b3f64060cc [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% PPPP OOO L IIIII CCCC Y Y %
6% P P O O L I C Y Y %
7% PPPP O O L I C Y %
8% P O O L I C Y %
9% P OOO LLLLL IIIII CCCC Y %
10% %
11% %
12% MagickCore Policy Methods %
13% %
14% Software Design %
15% John Cristy %
16% July 1992 %
17% %
18% %
cristy7e41fe82010-12-04 23:12:08 +000019% Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000020% dedicated to making software imaging solutions freely available. %
21% %
22% You may not use this file except in compliance with the License. You may %
23% obtain a copy of the License at %
24% %
25% http://www.imagemagick.org/script/license.php %
26% %
27% Unless required by applicable law or agreed to in writing, software %
28% distributed under the License is distributed on an "AS IS" BASIS, %
29% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
30% See the License for the specific language governing permissions and %
31% limitations under the License. %
32% %
33%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
34%
35% We use linked-lists because splay-trees do not currently support duplicate
36% key / value pairs (.e.g X11 green compliance and SVG green compliance).
37%
38*/
39
40/*
41 Include declarations.
42*/
cristy4c08aed2011-07-01 19:47:50 +000043#include "MagickCore/studio.h"
44#include "MagickCore/client.h"
45#include "MagickCore/configure.h"
46#include "MagickCore/exception.h"
47#include "MagickCore/exception-private.h"
48#include "MagickCore/memory_.h"
49#include "MagickCore/monitor.h"
50#include "MagickCore/monitor-private.h"
51#include "MagickCore/option.h"
52#include "MagickCore/policy.h"
53#include "MagickCore/semaphore.h"
54#include "MagickCore/string_.h"
55#include "MagickCore/token.h"
56#include "MagickCore/utility.h"
57#include "MagickCore/xml-tree.h"
cristy3ed852e2009-09-05 21:47:34 +000058
59/*
60 Define declarations.
61*/
62#define PolicyFilename "policy.xml"
63
64/*
cristy54a531d2009-10-21 17:58:01 +000065 Typedef declarations.
cristy3ed852e2009-09-05 21:47:34 +000066*/
67struct _PolicyInfo
68{
69 char
cristy54a531d2009-10-21 17:58:01 +000070 *path;
cristy3ed852e2009-09-05 21:47:34 +000071
72 PolicyDomain
73 domain;
74
75 PolicyRights
76 rights;
77
78 char
cristy54a531d2009-10-21 17:58:01 +000079 *name,
cristy3ed852e2009-09-05 21:47:34 +000080 *pattern,
81 *value;
82
83 MagickBooleanType
cristy54a531d2009-10-21 17:58:01 +000084 exempt,
cristy3ed852e2009-09-05 21:47:34 +000085 stealth,
86 debug;
87
88 SemaphoreInfo
89 *semaphore;
90
cristybb503372010-05-27 20:51:26 +000091 size_t
cristy3ed852e2009-09-05 21:47:34 +000092 signature;
93};
cristy54a531d2009-10-21 17:58:01 +000094
95typedef struct _PolicyMapInfo
96{
97 const PolicyDomain
98 domain;
99
100 const PolicyRights
101 rights;
102
103 const char
104 *name,
105 *pattern,
106 *value;
107} PolicyMapInfo;
cristy3ed852e2009-09-05 21:47:34 +0000108
109/*
110 Static declarations.
111*/
cristy54a531d2009-10-21 17:58:01 +0000112static const PolicyMapInfo
113 PolicyMap[] =
114 {
cristyfa378d92009-10-23 15:41:15 +0000115 { UndefinedPolicyDomain, UndefinedPolicyRights, (const char *) NULL,
116 (const char *) NULL, (const char *) NULL }
cristy54a531d2009-10-21 17:58:01 +0000117 };
118
cristy3ed852e2009-09-05 21:47:34 +0000119static LinkedListInfo
120 *policy_list = (LinkedListInfo *) NULL;
121
122static SemaphoreInfo
123 *policy_semaphore = (SemaphoreInfo *) NULL;
124
125static volatile MagickBooleanType
126 instantiate_policy = MagickFalse;
127
128/*
129 Forward declarations.
130*/
131static MagickBooleanType
132 InitializePolicyList(ExceptionInfo *),
133 LoadPolicyLists(const char *,ExceptionInfo *);
134
135/*
136%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
137% %
138% %
139% %
cristy3ed852e2009-09-05 21:47:34 +0000140+ G e t P o l i c y I n f o %
141% %
142% %
143% %
144%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
145%
146% GetPolicyInfo() searches the policy list for the specified name and if found
147% returns attributes for that policy.
148%
149% The format of the GetPolicyInfo method is:
150%
151% PolicyInfo *GetPolicyInfo(const char *name,ExceptionInfo *exception)
152%
153% A description of each parameter follows:
154%
155% o name: the policy name.
156%
157% o exception: return any errors or warnings in this structure.
158%
159*/
160static PolicyInfo *GetPolicyInfo(const char *name,ExceptionInfo *exception)
161{
162 char
163 policyname[MaxTextExtent];
164
165 register PolicyInfo
166 *p;
167
168 register char
169 *q;
170
171 assert(exception != (ExceptionInfo *) NULL);
172 if ((policy_list == (LinkedListInfo *) NULL) ||
173 (instantiate_policy == MagickFalse))
174 if (InitializePolicyList(exception) == MagickFalse)
175 return((PolicyInfo *) NULL);
176 if ((policy_list == (LinkedListInfo *) NULL) ||
177 (IsLinkedListEmpty(policy_list) != MagickFalse))
178 return((PolicyInfo *) NULL);
179 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
180 return((PolicyInfo *) GetValueFromLinkedList(policy_list,0));
181 /*
182 Strip names of whitespace.
183 */
184 (void) CopyMagickString(policyname,name,MaxTextExtent);
185 for (q=policyname; *q != '\0'; q++)
186 {
187 if (isspace((int) ((unsigned char) *q)) == 0)
188 continue;
189 (void) CopyMagickString(q,q+1,MaxTextExtent);
190 q--;
191 }
192 /*
193 Search for policy tag.
194 */
cristyf84a1932010-01-03 18:00:18 +0000195 LockSemaphoreInfo(policy_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000196 ResetLinkedListIterator(policy_list);
197 p=(PolicyInfo *) GetNextValueInLinkedList(policy_list);
198 while (p != (PolicyInfo *) NULL)
199 {
200 if (LocaleCompare(policyname,p->name) == 0)
201 break;
202 p=(PolicyInfo *) GetNextValueInLinkedList(policy_list);
203 }
cristy6446d622009-11-02 01:17:45 +0000204 if (p != (PolicyInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000205 (void) InsertValueInLinkedList(policy_list,0,
206 RemoveElementByValueFromLinkedList(policy_list,p));
cristyf84a1932010-01-03 18:00:18 +0000207 UnlockSemaphoreInfo(policy_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000208 return(p);
209}
210
211/*
212%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
213% %
214% %
215% %
216% G e t P o l i c y I n f o L i s t %
217% %
218% %
219% %
220%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
221%
222% GetPolicyInfoList() returns any policies that match the specified pattern.
223%
224% The format of the GetPolicyInfoList function is:
225%
226% const PolicyInfo **GetPolicyInfoList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000227% size_t *number_policies,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000228%
229% A description of each parameter follows:
230%
231% o pattern: Specifies a pointer to a text string containing a pattern.
232%
233% o number_policies: returns the number of policies in the list.
234%
235% o exception: return any errors or warnings in this structure.
236%
237*/
238MagickExport const PolicyInfo **GetPolicyInfoList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000239 size_t *number_policies,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000240{
241 const PolicyInfo
242 **policies;
243
244 register const PolicyInfo
245 *p;
246
cristybb503372010-05-27 20:51:26 +0000247 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000248 i;
249
250 /*
251 Allocate policy list.
252 */
253 assert(pattern != (char *) NULL);
254 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
cristybb503372010-05-27 20:51:26 +0000255 assert(number_policies != (size_t *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000256 *number_policies=0;
257 p=GetPolicyInfo("*",exception);
258 if (p == (const PolicyInfo *) NULL)
259 return((const PolicyInfo **) NULL);
260 policies=(const PolicyInfo **) AcquireQuantumMemory((size_t)
261 GetNumberOfElementsInLinkedList(policy_list)+1UL,sizeof(*policies));
262 if (policies == (const PolicyInfo **) NULL)
263 return((const PolicyInfo **) NULL);
264 /*
265 Generate policy list.
266 */
cristyf84a1932010-01-03 18:00:18 +0000267 LockSemaphoreInfo(policy_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000268 ResetLinkedListIterator(policy_list);
269 p=(const PolicyInfo *) GetNextValueInLinkedList(policy_list);
270 for (i=0; p != (const PolicyInfo *) NULL; )
271 {
272 if ((p->stealth == MagickFalse) &&
273 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
274 policies[i++]=p;
275 p=(const PolicyInfo *) GetNextValueInLinkedList(policy_list);
276 }
cristyf84a1932010-01-03 18:00:18 +0000277 UnlockSemaphoreInfo(policy_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000278 policies[i]=(PolicyInfo *) NULL;
cristybb503372010-05-27 20:51:26 +0000279 *number_policies=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +0000280 return(policies);
281}
282
283/*
284%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
285% %
286% %
287% %
288% G e t P o l i c y L i s t %
289% %
290% %
291% %
292%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
293%
294% GetPolicyList() returns any policies that match the specified pattern.
295%
296% The format of the GetPolicyList function is:
297%
cristybb503372010-05-27 20:51:26 +0000298% char **GetPolicyList(const char *pattern,size_t *number_policies,
cristy3ed852e2009-09-05 21:47:34 +0000299% ExceptionInfo *exception)
300%
301% A description of each parameter follows:
302%
303% o pattern: a pointer to a text string containing a pattern.
304%
305% o number_policies: returns the number of policies in the list.
306%
307% o exception: return any errors or warnings in this structure.
308%
309*/
310MagickExport char **GetPolicyList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000311 size_t *number_policies,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000312{
313 char
314 **policies;
315
316 register const PolicyInfo
317 *p;
318
cristybb503372010-05-27 20:51:26 +0000319 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000320 i;
321
322 /*
323 Allocate policy list.
324 */
325 assert(pattern != (char *) NULL);
326 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
cristybb503372010-05-27 20:51:26 +0000327 assert(number_policies != (size_t *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000328 *number_policies=0;
329 p=GetPolicyInfo("*",exception);
330 if (p == (const PolicyInfo *) NULL)
331 return((char **) NULL);
332 policies=(char **) AcquireQuantumMemory((size_t)
333 GetNumberOfElementsInLinkedList(policy_list)+1UL,sizeof(*policies));
334 if (policies == (char **) NULL)
335 return((char **) NULL);
336 /*
337 Generate policy list.
338 */
cristyf84a1932010-01-03 18:00:18 +0000339 LockSemaphoreInfo(policy_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000340 ResetLinkedListIterator(policy_list);
341 p=(const PolicyInfo *) GetNextValueInLinkedList(policy_list);
342 for (i=0; p != (const PolicyInfo *) NULL; )
343 {
344 if ((p->stealth == MagickFalse) &&
345 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
346 policies[i++]=ConstantString(p->name);
347 p=(const PolicyInfo *) GetNextValueInLinkedList(policy_list);
348 }
cristyf84a1932010-01-03 18:00:18 +0000349 UnlockSemaphoreInfo(policy_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000350 policies[i]=(char *) NULL;
cristybb503372010-05-27 20:51:26 +0000351 *number_policies=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +0000352 return(policies);
353}
354
355/*
356%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
357% %
358% %
359% %
360% G e t P o l i c y V a l u e %
361% %
362% %
363% %
364%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
365%
366% GetPolicyValue() returns the value associated with the named policy.
367%
368% The format of the GetPolicyValue method is:
369%
370% char *GetPolicyValue(const char *name)
371%
372% A description of each parameter follows:
373%
374% o policy_info: The policy info.
375%
376*/
377MagickExport char *GetPolicyValue(const char *name)
378{
379 const char
380 *value;
381
382 const PolicyInfo
383 *policy_info;
384
385 ExceptionInfo
386 *exception;
387
388 assert(name != (const char *) NULL);
389 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",name);
390 exception=AcquireExceptionInfo();
391 policy_info=GetPolicyInfo(name,exception);
392 exception=DestroyExceptionInfo(exception);
393 if (policy_info == (PolicyInfo *) NULL)
394 return((char *) NULL);
395 value=policy_info->value;
396 if ((value == (const char *) NULL) || (*value == '\0'))
397 return((char *) NULL);
398 return(ConstantString(value));
399}
400
401/*
402%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
403% %
404% %
405% %
406+ I n i t i a l i z e P o l i c y L i s t %
407% %
408% %
409% %
410%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
411%
412% InitializePolicyList() initializes the policy list.
413%
414% The format of the InitializePolicyList method is:
415%
416% MagickBooleanType InitializePolicyList(ExceptionInfo *exception)
417%
418% A description of each parameter follows.
419%
420% o exception: return any errors or warnings in this structure.
421%
422*/
423static MagickBooleanType InitializePolicyList(ExceptionInfo *exception)
424{
425 if ((policy_list == (LinkedListInfo *) NULL) &&
426 (instantiate_policy == MagickFalse))
427 {
cristy4e1dff62009-10-25 20:36:03 +0000428 if (policy_semaphore == (SemaphoreInfo *) NULL)
429 AcquireSemaphoreInfo(&policy_semaphore);
cristyf84a1932010-01-03 18:00:18 +0000430 LockSemaphoreInfo(policy_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000431 if ((policy_list == (LinkedListInfo *) NULL) &&
432 (instantiate_policy == MagickFalse))
433 {
434 (void) LoadPolicyLists(PolicyFilename,exception);
435 instantiate_policy=MagickTrue;
436 }
cristyf84a1932010-01-03 18:00:18 +0000437 UnlockSemaphoreInfo(policy_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000438 }
439 return(policy_list != (LinkedListInfo *) NULL ? MagickTrue : MagickFalse);
440}
441
442/*
443%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
444% %
445% %
446% %
cristy3ed852e2009-09-05 21:47:34 +0000447% I s R i g h t s A u t h o r i z e d %
448% %
449% %
450% %
451%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
452%
453% IsRightsAuthorized() returns MagickTrue if the policy authorizes the
454% requested rights for the specified domain.
455%
456% The format of the IsRightsAuthorized method is:
457%
458% MagickBooleanType IsRightsAuthorized(const PolicyDomain domain,
459% const PolicyRights rights,const char *pattern)
460%
461% A description of each parameter follows:
462%
463% o domain: the policy domain.
464%
465% o rights: the policy rights.
466%
467% o pattern: the coder, delegate, filter, or path pattern.
468%
469*/
470MagickExport MagickBooleanType IsRightsAuthorized(const PolicyDomain domain,
471 const PolicyRights rights,const char *pattern)
472{
473 const PolicyInfo
474 *policy_info;
475
476 ExceptionInfo
477 *exception;
478
479 MagickBooleanType
480 authorized;
481
482 register PolicyInfo
483 *p;
484
485 (void) LogMagickEvent(PolicyEvent,GetMagickModule(),
486 "Domain: %s; rights=%s; pattern=\"%s\" ...",
cristy042ee782011-04-22 18:48:30 +0000487 CommandOptionToMnemonic(MagickPolicyDomainOptions,domain),
488 CommandOptionToMnemonic(MagickPolicyRightsOptions,rights),pattern);
cristy3ed852e2009-09-05 21:47:34 +0000489 exception=AcquireExceptionInfo();
490 policy_info=GetPolicyInfo("*",exception);
491 exception=DestroyExceptionInfo(exception);
492 if (policy_info == (PolicyInfo *) NULL)
493 return(MagickTrue);
494 authorized=MagickTrue;
cristyf84a1932010-01-03 18:00:18 +0000495 LockSemaphoreInfo(policy_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000496 ResetLinkedListIterator(policy_list);
497 p=(PolicyInfo *) GetNextValueInLinkedList(policy_list);
498 while ((p != (PolicyInfo *) NULL) && (authorized != MagickFalse))
499 {
500 if ((p->domain == domain) &&
501 (GlobExpression(pattern,p->pattern,MagickFalse) != MagickFalse))
502 {
503 if (((rights & ReadPolicyRights) != 0) &&
504 ((p->rights & ReadPolicyRights) == 0))
505 authorized=MagickFalse;
506 if (((rights & WritePolicyRights) != 0) &&
507 ((p->rights & WritePolicyRights) == 0))
508 authorized=MagickFalse;
509 if (((rights & ExecutePolicyRights) != 0) &&
510 ((p->rights & ExecutePolicyRights) == 0))
511 authorized=MagickFalse;
512 }
513 p=(PolicyInfo *) GetNextValueInLinkedList(policy_list);
514 }
cristyf84a1932010-01-03 18:00:18 +0000515 UnlockSemaphoreInfo(policy_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000516 return(authorized);
517}
518
519/*
520%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
521% %
522% %
523% %
524% L i s t P o l i c y I n f o %
525% %
526% %
527% %
528%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
529%
530% ListPolicyInfo() lists policies to the specified file.
531%
532% The format of the ListPolicyInfo method is:
533%
534% MagickBooleanType ListPolicyInfo(FILE *file,ExceptionInfo *exception)
535%
536% A description of each parameter follows.
537%
538% o file: List policy names to this file handle.
539%
540% o exception: return any errors or warnings in this structure.
541%
542*/
543MagickExport MagickBooleanType ListPolicyInfo(FILE *file,
544 ExceptionInfo *exception)
545{
546 const char
547 *path,
548 *domain;
549
550 const PolicyInfo
551 **policy_info;
552
cristybb503372010-05-27 20:51:26 +0000553 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000554 i;
555
cristybb503372010-05-27 20:51:26 +0000556 size_t
cristy3ed852e2009-09-05 21:47:34 +0000557 number_policies;
558
559 /*
560 List name and attributes of each policy in the list.
561 */
562 if (file == (const FILE *) NULL)
563 file=stdout;
564 policy_info=GetPolicyInfoList("*",&number_policies,exception);
565 if (policy_info == (const PolicyInfo **) NULL)
566 return(MagickFalse);
567 path=(const char *) NULL;
cristybb503372010-05-27 20:51:26 +0000568 for (i=0; i < (ssize_t) number_policies; i++)
cristy3ed852e2009-09-05 21:47:34 +0000569 {
570 if (policy_info[i]->stealth != MagickFalse)
571 continue;
572 if (((path == (const char *) NULL) ||
573 (LocaleCompare(path,policy_info[i]->path) != 0)) &&
574 (policy_info[i]->path != (char *) NULL))
cristyb51dff52011-05-19 16:55:47 +0000575 (void) FormatLocaleFile(file,"\nPath: %s\n",policy_info[i]->path);
cristy3ed852e2009-09-05 21:47:34 +0000576 path=policy_info[i]->path;
cristy042ee782011-04-22 18:48:30 +0000577 domain=CommandOptionToMnemonic(MagickPolicyDomainOptions,
cristy3ed852e2009-09-05 21:47:34 +0000578 policy_info[i]->domain);
cristyb51dff52011-05-19 16:55:47 +0000579 (void) FormatLocaleFile(file," Policy: %s\n",domain);
cristy7ce14bc2010-01-17 00:01:01 +0000580 if ((policy_info[i]->domain == ResourcePolicyDomain) ||
581 (policy_info[i]->domain == SystemPolicyDomain))
cristy3ed852e2009-09-05 21:47:34 +0000582 {
583 if (policy_info[i]->name != (char *) NULL)
cristyb51dff52011-05-19 16:55:47 +0000584 (void) FormatLocaleFile(file," name: %s\n",policy_info[i]->name);
cristy3ed852e2009-09-05 21:47:34 +0000585 if (policy_info[i]->value != (char *) NULL)
cristyb51dff52011-05-19 16:55:47 +0000586 (void) FormatLocaleFile(file," value: %s\n",policy_info[i]->value);
cristy3ed852e2009-09-05 21:47:34 +0000587 }
588 else
589 {
cristyb51dff52011-05-19 16:55:47 +0000590 (void) FormatLocaleFile(file," rights: ");
cristy3ed852e2009-09-05 21:47:34 +0000591 if (policy_info[i]->rights == NoPolicyRights)
cristyb51dff52011-05-19 16:55:47 +0000592 (void) FormatLocaleFile(file,"None ");
cristy3ed852e2009-09-05 21:47:34 +0000593 if ((policy_info[i]->rights & ReadPolicyRights) != 0)
cristyb51dff52011-05-19 16:55:47 +0000594 (void) FormatLocaleFile(file,"Read ");
cristy3ed852e2009-09-05 21:47:34 +0000595 if ((policy_info[i]->rights & WritePolicyRights) != 0)
cristyb51dff52011-05-19 16:55:47 +0000596 (void) FormatLocaleFile(file,"Write ");
cristy3ed852e2009-09-05 21:47:34 +0000597 if ((policy_info[i]->rights & ExecutePolicyRights) != 0)
cristyb51dff52011-05-19 16:55:47 +0000598 (void) FormatLocaleFile(file,"Execute ");
599 (void) FormatLocaleFile(file,"\n");
cristy3ed852e2009-09-05 21:47:34 +0000600 if (policy_info[i]->pattern != (char *) NULL)
cristy1e604812011-05-19 18:07:50 +0000601 (void) FormatLocaleFile(file," pattern: %s\n",
602 policy_info[i]->pattern);
cristy3ed852e2009-09-05 21:47:34 +0000603 }
604 }
605 policy_info=(const PolicyInfo **) RelinquishMagickMemory((void *)
606 policy_info);
607 (void) fflush(file);
608 return(MagickTrue);
609}
610
611/*
612%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
613% %
614% %
615% %
616+ L o a d P o l i c y L i s t %
617% %
618% %
619% %
620%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
621%
622% LoadPolicyList() loads the policy configuration file which provides a mapping
623% between policy attributes and a policy domain.
624%
625% The format of the LoadPolicyList method is:
626%
627% MagickBooleanType LoadPolicyList(const char *xml,const char *filename,
cristybb503372010-05-27 20:51:26 +0000628% const size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000629%
630% A description of each parameter follows:
631%
632% o xml: The policy list in XML format.
633%
634% o filename: The policy list filename.
635%
636% o depth: depth of <include /> statements.
637%
638% o exception: return any errors or warnings in this structure.
639%
640*/
641static MagickBooleanType LoadPolicyList(const char *xml,const char *filename,
cristybb503372010-05-27 20:51:26 +0000642 const size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000643{
644 char
645 keyword[MaxTextExtent],
646 *token;
647
648 PolicyInfo
649 *policy_info;
650
651 const char
652 *q;
653
654 MagickBooleanType
655 status;
656
657 /*
658 Load the policy map file.
659 */
660 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
661 "Loading policy file \"%s\" ...",filename);
662 if (xml == (char *) NULL)
663 return(MagickFalse);
664 if (policy_list == (LinkedListInfo *) NULL)
665 {
666 policy_list=NewLinkedList(0);
667 if (policy_list == (LinkedListInfo *) NULL)
668 {
669 ThrowFileException(exception,ResourceLimitError,
670 "MemoryAllocationFailed",filename);
671 return(MagickFalse);
672 }
673 }
674 status=MagickTrue;
675 policy_info=(PolicyInfo *) NULL;
676 token=AcquireString(xml);
677 for (q=(const char *) xml; *q != '\0'; )
678 {
679 /*
680 Interpret XML.
681 */
682 GetMagickToken(q,&q,token);
683 if (*token == '\0')
684 break;
685 (void) CopyMagickString(keyword,token,MaxTextExtent);
686 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
687 {
688 /*
689 Docdomain element.
690 */
691 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
692 GetMagickToken(q,&q,token);
693 continue;
694 }
695 if (LocaleNCompare(keyword,"<!--",4) == 0)
696 {
697 /*
698 Comment element.
699 */
700 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
701 GetMagickToken(q,&q,token);
702 continue;
703 }
704 if (LocaleCompare(keyword,"<include") == 0)
705 {
706 /*
707 Include element.
708 */
709 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
710 {
711 (void) CopyMagickString(keyword,token,MaxTextExtent);
712 GetMagickToken(q,&q,token);
713 if (*token != '=')
714 continue;
715 GetMagickToken(q,&q,token);
716 if (LocaleCompare(keyword,"file") == 0)
717 {
718 if (depth > 200)
719 (void) ThrowMagickException(exception,GetMagickModule(),
720 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
721 else
722 {
723 char
724 path[MaxTextExtent],
725 *xml;
726
727 GetPathComponent(filename,HeadPath,path);
728 if (*path != '\0')
729 (void) ConcatenateMagickString(path,DirectorySeparator,
730 MaxTextExtent);
731 if (*token == *DirectorySeparator)
732 (void) CopyMagickString(path,token,MaxTextExtent);
733 else
734 (void) ConcatenateMagickString(path,token,MaxTextExtent);
735 xml=FileToString(path,~0,exception);
736 if (xml != (char *) NULL)
737 {
738 status=LoadPolicyList(xml,path,depth+1,exception);
739 xml=(char *) RelinquishMagickMemory(xml);
740 }
741 }
742 }
743 }
744 continue;
745 }
746 if (LocaleCompare(keyword,"<policy") == 0)
747 {
748 /*
749 Policy element.
750 */
cristy73bd4a52010-10-05 11:24:23 +0000751 policy_info=(PolicyInfo *) AcquireMagickMemory(sizeof(*policy_info));
cristy3ed852e2009-09-05 21:47:34 +0000752 if (policy_info == (PolicyInfo *) NULL)
753 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
754 (void) ResetMagickMemory(policy_info,0,sizeof(*policy_info));
755 policy_info->path=ConstantString(filename);
cristy54a531d2009-10-21 17:58:01 +0000756 policy_info->exempt=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +0000757 policy_info->signature=MagickSignature;
758 continue;
759 }
760 if (policy_info == (PolicyInfo *) NULL)
761 continue;
762 if (LocaleCompare(keyword,"/>") == 0)
763 {
764 status=AppendValueToLinkedList(policy_list,policy_info);
765 if (status == MagickFalse)
766 (void) ThrowMagickException(exception,GetMagickModule(),
767 ResourceLimitError,"MemoryAllocationFailed","`%s'",
768 policy_info->name);
769 policy_info=(PolicyInfo *) NULL;
770 }
771 GetMagickToken(q,(const char **) NULL,token);
772 if (*token != '=')
773 continue;
774 GetMagickToken(q,&q,token);
775 GetMagickToken(q,&q,token);
776 switch (*keyword)
777 {
778 case 'D':
779 case 'd':
780 {
781 if (LocaleCompare((char *) keyword,"domain") == 0)
782 {
cristy042ee782011-04-22 18:48:30 +0000783 policy_info->domain=(PolicyDomain) ParseCommandOption(
cristy3ed852e2009-09-05 21:47:34 +0000784 MagickPolicyDomainOptions,MagickTrue,token);
785 break;
786 }
787 break;
788 }
789 case 'N':
790 case 'n':
791 {
792 if (LocaleCompare((char *) keyword,"name") == 0)
793 {
794 policy_info->name=ConstantString(token);
795 break;
796 }
797 break;
798 }
799 case 'P':
800 case 'p':
801 {
802 if (LocaleCompare((char *) keyword,"pattern") == 0)
803 {
804 policy_info->pattern=ConstantString(token);
805 break;
806 }
807 break;
808 }
809 case 'R':
810 case 'r':
811 {
812 if (LocaleCompare((char *) keyword,"rights") == 0)
813 {
cristy042ee782011-04-22 18:48:30 +0000814 policy_info->rights=(PolicyRights) ParseCommandOption(
cristy3ed852e2009-09-05 21:47:34 +0000815 MagickPolicyRightsOptions,MagickTrue,token);
816 break;
817 }
818 break;
819 }
820 case 'S':
821 case 's':
822 {
823 if (LocaleCompare((char *) keyword,"stealth") == 0)
824 {
825 policy_info->stealth=IsMagickTrue(token);
826 break;
827 }
828 break;
829 }
830 case 'V':
831 case 'v':
832 {
833 if (LocaleCompare((char *) keyword,"value") == 0)
834 {
835 policy_info->value=ConstantString(token);
836 break;
837 }
838 break;
839 }
840 default:
841 break;
842 }
843 }
844 token=(char *) RelinquishMagickMemory(token);
845 return(status);
846}
847
848/*
849%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
850% %
851% %
852% %
853% L o a d P o l i c y L i s t s %
854% %
855% %
856% %
857%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
858%
859% LoadPolicyList() loads one or more policy configuration file which provides a
860% mapping between policy attributes and a policy name.
861%
862% The format of the LoadPolicyLists method is:
863%
864% MagickBooleanType LoadPolicyLists(const char *filename,
865% ExceptionInfo *exception)
866%
867% A description of each parameter follows:
868%
869% o filename: the font file name.
870%
871% o exception: return any errors or warnings in this structure.
872%
873*/
874static MagickBooleanType LoadPolicyLists(const char *filename,
875 ExceptionInfo *exception)
876{
cristy3ed852e2009-09-05 21:47:34 +0000877 const StringInfo
878 *option;
879
880 LinkedListInfo
881 *options;
882
883 MagickStatusType
884 status;
885
cristybb503372010-05-27 20:51:26 +0000886 register ssize_t
cristy54a531d2009-10-21 17:58:01 +0000887 i;
888
889 /*
890 Load built-in policy map.
891 */
cristy3ed852e2009-09-05 21:47:34 +0000892 status=MagickFalse;
cristy54a531d2009-10-21 17:58:01 +0000893 if (policy_list == (LinkedListInfo *) NULL)
894 {
895 policy_list=NewLinkedList(0);
896 if (policy_list == (LinkedListInfo *) NULL)
897 {
898 ThrowFileException(exception,ResourceLimitError,
899 "MemoryAllocationFailed",filename);
900 return(MagickFalse);
901 }
902 }
cristybb503372010-05-27 20:51:26 +0000903 for (i=0; i < (ssize_t) (sizeof(PolicyMap)/sizeof(*PolicyMap)); i++)
cristy54a531d2009-10-21 17:58:01 +0000904 {
905 PolicyInfo
906 *policy_info;
907
908 register const PolicyMapInfo
909 *p;
910
911 p=PolicyMap+i;
cristy73bd4a52010-10-05 11:24:23 +0000912 policy_info=(PolicyInfo *) AcquireMagickMemory(sizeof(*policy_info));
cristy54a531d2009-10-21 17:58:01 +0000913 if (policy_info == (PolicyInfo *) NULL)
914 {
915 (void) ThrowMagickException(exception,GetMagickModule(),
916 ResourceLimitError,"MemoryAllocationFailed","`%s'",policy_info->name);
917 continue;
918 }
919 (void) ResetMagickMemory(policy_info,0,sizeof(*policy_info));
920 policy_info->path=(char *) "[built-in]";
921 policy_info->domain=p->domain;
922 policy_info->rights=p->rights;
923 policy_info->name=(char *) p->name;
924 policy_info->pattern=(char *) p->pattern;
925 policy_info->value=(char *) p->value;
926 policy_info->exempt=MagickTrue;
927 policy_info->signature=MagickSignature;
928 status=AppendValueToLinkedList(policy_list,policy_info);
929 if (status == MagickFalse)
930 (void) ThrowMagickException(exception,GetMagickModule(),
931 ResourceLimitError,"MemoryAllocationFailed","`%s'",policy_info->name);
932 }
933 /*
934 Load external policy map.
935 */
cristy3ed852e2009-09-05 21:47:34 +0000936 options=GetConfigureOptions(filename,exception);
937 option=(const StringInfo *) GetNextValueInLinkedList(options);
938 while (option != (const StringInfo *) NULL)
939 {
940 status|=LoadPolicyList((const char *) GetStringInfoDatum(option),
941 GetStringInfoPath(option),0,exception);
942 option=(const StringInfo *) GetNextValueInLinkedList(options);
943 }
944 options=DestroyConfigureOptions(options);
cristy3ed852e2009-09-05 21:47:34 +0000945 return(status != 0 ? MagickTrue : MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +0000946}
cristyf34a1452009-10-24 22:29:27 +0000947
948/*
949%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
950% %
951% %
952% %
953+ P o l i c y C o m p o n e n t G e n e s i s %
954% %
955% %
956% %
957%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
958%
959% PolicyComponentGenesis() instantiates the policy component.
960%
961% The format of the PolicyComponentGenesis method is:
962%
963% MagickBooleanType PolicyComponentGenesis(void)
964%
965*/
966MagickExport MagickBooleanType PolicyComponentGenesis(void)
967{
cristy165b6092009-10-26 13:52:10 +0000968 AcquireSemaphoreInfo(&policy_semaphore);
cristyf34a1452009-10-24 22:29:27 +0000969 return(MagickTrue);
970}
971
972/*
973%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
974% %
975% %
976% %
977+ P o l i c y C o m p o n e n t T e r m i n u s %
978% %
979% %
980% %
981%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
982%
983% PolicyComponentTerminus() destroys the policy component.
984%
985% The format of the PolicyComponentTerminus method is:
986%
987% PolicyComponentTerminus(void)
988%
989*/
990
991static void *DestroyPolicyElement(void *policy_info)
992{
993 register PolicyInfo
994 *p;
995
996 p=(PolicyInfo *) policy_info;
997 if (p->exempt == MagickFalse)
998 {
999 if (p->value != (char *) NULL)
1000 p->value=DestroyString(p->value);
1001 if (p->pattern != (char *) NULL)
1002 p->pattern=DestroyString(p->pattern);
1003 if (p->name != (char *) NULL)
1004 p->name=DestroyString(p->name);
1005 if (p->path != (char *) NULL)
1006 p->path=DestroyString(p->path);
1007 }
1008 p=(PolicyInfo *) RelinquishMagickMemory(p);
1009 return((void *) NULL);
1010}
1011
1012MagickExport void PolicyComponentTerminus(void)
1013{
cristy18b17442009-10-25 18:36:48 +00001014 if (policy_semaphore == (SemaphoreInfo *) NULL)
1015 AcquireSemaphoreInfo(&policy_semaphore);
cristyf84a1932010-01-03 18:00:18 +00001016 LockSemaphoreInfo(policy_semaphore);
cristyf34a1452009-10-24 22:29:27 +00001017 if (policy_list != (LinkedListInfo *) NULL)
1018 policy_list=DestroyLinkedList(policy_list,DestroyPolicyElement);
1019 instantiate_policy=MagickFalse;
cristyf84a1932010-01-03 18:00:18 +00001020 UnlockSemaphoreInfo(policy_semaphore);
cristyf34a1452009-10-24 22:29:27 +00001021 DestroySemaphoreInfo(&policy_semaphore);
1022}