Enforce smack policy from conf file
[platform/upstream/dbus.git] / bus / config-parser.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* config-parser.c  XML-library-agnostic configuration file parser
3  *
4  * Copyright (C) 2003, 2004 Red Hat, Inc.
5  *
6  * Licensed under the Academic Free License version 2.1
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23
24 #include <config.h>
25 #include "config-parser-common.h"
26 #include "config-parser.h"
27 #include "test.h"
28 #include "utils.h"
29 #include "policy.h"
30 #include "selinux.h"
31 #include "apparmor.h"
32 #include <dbus/dbus-list.h>
33 #include <dbus/dbus-internals.h>
34 #include <dbus/dbus-misc.h>
35 #include <dbus/dbus-sysdeps.h>
36 #include <string.h>
37
38 typedef enum
39 {
40   /* we ignore policies for unknown groups/users */
41   POLICY_IGNORED,
42
43   /* non-ignored */
44   POLICY_DEFAULT,
45   POLICY_MANDATORY,
46   POLICY_USER,
47   POLICY_GROUP,
48   POLICY_SMACK,
49   POLICY_CONSOLE
50 } PolicyType;
51
52 typedef struct
53 {
54   ElementType type;
55
56   unsigned int had_content : 1;
57
58   union
59   {
60     struct
61     {
62       unsigned int ignore_missing : 1;
63       unsigned int if_selinux_enabled : 1;
64       unsigned int selinux_root_relative : 1;
65     } include;
66
67     struct
68     {
69       PolicyType type;
70       union
71       {
72         unsigned long gid_uid_or_at_console;
73         char *smack_label;
74       };
75     } policy;
76
77     struct
78     {
79       char *name;
80       long value;
81     } limit;
82     
83   } d;
84
85 } Element;
86
87 /**
88  * Parser for bus configuration file. 
89  */
90 struct BusConfigParser
91 {
92   int refcount;        /**< Reference count */
93
94   DBusString basedir;  /**< Directory we resolve paths relative to */
95   
96   DBusList *stack;     /**< stack of Element */
97
98   char *user;          /**< user to run as */
99
100   char *servicehelper; /**< location of the setuid helper */
101
102   char *bus_type;          /**< Message bus type */
103   
104   DBusList *listen_on; /**< List of addresses to listen to */
105
106   DBusList *mechanisms; /**< Auth mechanisms */
107
108   DBusList *service_dirs; /**< Directories to look for session services in */
109
110   DBusList *conf_dirs;   /**< Directories to look for policy configuration in */
111
112   BusPolicy *policy;     /**< Security policy */
113
114   BusLimits limits;      /**< Limits */
115
116   char *pidfile;         /**< PID file */
117
118   DBusList *included_files;  /**< Included files stack */
119
120   DBusHashTable *service_context_table; /**< Map service names to SELinux contexts */
121
122   unsigned int fork : 1; /**< TRUE to fork into daemon mode */
123
124   unsigned int syslog : 1; /**< TRUE to enable syslog */
125   unsigned int keep_umask : 1; /**< TRUE to keep original umask when forking */
126
127   unsigned int is_toplevel : 1; /**< FALSE if we are a sub-config-file inside another one */
128
129   unsigned int allow_anonymous : 1; /**< TRUE to allow anonymous connections */
130 };
131
132 static Element*
133 push_element (BusConfigParser *parser,
134               ElementType      type)
135 {
136   Element *e;
137
138   _dbus_assert (type != ELEMENT_NONE);
139   
140   e = dbus_new0 (Element, 1);
141   if (e == NULL)
142     return NULL;
143
144   if (!_dbus_list_append (&parser->stack, e))
145     {
146       dbus_free (e);
147       return NULL;
148     }
149   
150   e->type = type;
151
152   return e;
153 }
154
155 static void
156 element_free (Element *e)
157 {
158   if (e->type == ELEMENT_LIMIT)
159     dbus_free (e->d.limit.name);
160   else if (e->type == ELEMENT_POLICY && e->d.policy.type == POLICY_SMACK)
161       dbus_free (e->d.policy.smack_label);
162   
163   dbus_free (e);
164 }
165
166 static void
167 pop_element (BusConfigParser *parser)
168 {
169   Element *e;
170
171   e = _dbus_list_pop_last (&parser->stack);
172   
173   element_free (e);
174 }
175
176 static Element*
177 peek_element (BusConfigParser *parser)
178 {
179   Element *e;
180
181   e = _dbus_list_get_last (&parser->stack);
182
183   return e;
184 }
185
186 static ElementType
187 top_element_type (BusConfigParser *parser)
188 {
189   Element *e;
190
191   e = _dbus_list_get_last (&parser->stack);
192
193   if (e)
194     return e->type;
195   else
196     return ELEMENT_NONE;
197 }
198
199 static dbus_bool_t
200 merge_service_context_hash (DBusHashTable *dest,
201                             DBusHashTable *from)
202 {
203   DBusHashIter iter;
204   char *service_copy;
205   char *context_copy;
206
207   service_copy = NULL;
208   context_copy = NULL;
209
210   _dbus_hash_iter_init (from, &iter);
211   while (_dbus_hash_iter_next (&iter))
212     {
213       const char *service = _dbus_hash_iter_get_string_key (&iter);
214       const char *context = _dbus_hash_iter_get_value (&iter);
215
216       service_copy = _dbus_strdup (service);
217       if (service_copy == NULL)
218         goto fail;
219       context_copy = _dbus_strdup (context);
220       if (context_copy == NULL)
221         goto fail; 
222       
223       if (!_dbus_hash_table_insert_string (dest, service_copy, context_copy))
224         goto fail;
225
226       service_copy = NULL;
227       context_copy = NULL;    
228     }
229
230   return TRUE;
231
232  fail:
233   if (service_copy)
234     dbus_free (service_copy);
235
236   if (context_copy)
237     dbus_free (context_copy);
238
239   return FALSE;
240 }
241
242 static dbus_bool_t
243 service_dirs_find_dir (DBusList **service_dirs,
244                        const char *dir)
245 {
246   DBusList *link;
247
248   _dbus_assert (dir != NULL);
249
250   for (link = *service_dirs; link; link = _dbus_list_get_next_link(service_dirs, link))
251     {
252       const char *link_dir;
253       
254       link_dir = (const char *)link->data;
255       if (strcmp (dir, link_dir) == 0)
256         return TRUE;
257     }
258
259   return FALSE;
260 }
261
262 static dbus_bool_t
263 service_dirs_append_unique_or_free (DBusList **service_dirs,
264                                     char *dir)
265 {
266   if (!service_dirs_find_dir (service_dirs, dir))
267     return _dbus_list_append (service_dirs, dir);  
268
269   dbus_free (dir);
270   return TRUE;
271 }
272
273 static void 
274 service_dirs_append_link_unique_or_free (DBusList **service_dirs,
275                                          DBusList *dir_link)
276 {
277   if (!service_dirs_find_dir (service_dirs, dir_link->data))
278     {
279       _dbus_list_append_link (service_dirs, dir_link);
280     }
281   else
282     {
283       dbus_free (dir_link->data);
284       _dbus_list_free_link (dir_link);
285     }
286 }
287
288 static dbus_bool_t
289 merge_included (BusConfigParser *parser,
290                 BusConfigParser *included,
291                 DBusError       *error)
292 {
293   DBusList *link;
294
295   if (!bus_policy_merge (parser->policy,
296                          included->policy))
297     {
298       BUS_SET_OOM (error);
299       return FALSE;
300     }
301
302   if (!merge_service_context_hash (parser->service_context_table,
303                                    included->service_context_table))
304     {
305       BUS_SET_OOM (error);
306       return FALSE;
307     }
308   
309   if (included->user != NULL)
310     {
311       dbus_free (parser->user);
312       parser->user = included->user;
313       included->user = NULL;
314     }
315
316   if (included->bus_type != NULL)
317     {
318       dbus_free (parser->bus_type);
319       parser->bus_type = included->bus_type;
320       included->bus_type = NULL;
321     }
322   
323   if (included->fork)
324     parser->fork = TRUE;
325
326   if (included->keep_umask)
327     parser->keep_umask = TRUE;
328
329   if (included->allow_anonymous)
330     parser->allow_anonymous = TRUE;
331
332   if (included->pidfile != NULL)
333     {
334       dbus_free (parser->pidfile);
335       parser->pidfile = included->pidfile;
336       included->pidfile = NULL;
337     }
338
339   if (included->servicehelper != NULL)
340     {
341       dbus_free (parser->servicehelper);
342       parser->servicehelper = included->servicehelper;
343       included->servicehelper = NULL;
344     }
345
346   while ((link = _dbus_list_pop_first_link (&included->listen_on)))
347     _dbus_list_append_link (&parser->listen_on, link);
348
349   while ((link = _dbus_list_pop_first_link (&included->mechanisms)))
350     _dbus_list_append_link (&parser->mechanisms, link);
351
352   while ((link = _dbus_list_pop_first_link (&included->service_dirs)))
353     service_dirs_append_link_unique_or_free (&parser->service_dirs, link);
354
355   while ((link = _dbus_list_pop_first_link (&included->conf_dirs)))
356     _dbus_list_append_link (&parser->conf_dirs, link);
357   
358   return TRUE;
359 }
360
361 static dbus_bool_t
362 seen_include (BusConfigParser  *parser,
363               const DBusString *file)
364 {
365   DBusList *iter;
366
367   iter = parser->included_files;
368   while (iter != NULL)
369     {
370       if (! strcmp (_dbus_string_get_const_data (file), iter->data))
371         return TRUE;
372
373       iter = _dbus_list_get_next_link (&parser->included_files, iter);
374     }
375
376   return FALSE;
377 }
378
379 BusConfigParser*
380 bus_config_parser_new (const DBusString      *basedir,
381                        dbus_bool_t            is_toplevel,
382                        const BusConfigParser *parent)
383 {
384   BusConfigParser *parser;
385
386   parser = dbus_new0 (BusConfigParser, 1);
387   if (parser == NULL)
388     return NULL;
389
390   parser->is_toplevel = !!is_toplevel;
391   
392   if (!_dbus_string_init (&parser->basedir))
393     {
394       dbus_free (parser);
395       return NULL;
396     }
397
398   if (((parser->policy = bus_policy_new ()) == NULL) ||
399       !_dbus_string_copy (basedir, 0, &parser->basedir, 0) ||
400       ((parser->service_context_table = _dbus_hash_table_new (DBUS_HASH_STRING,
401                                                               dbus_free,
402                                                               dbus_free)) == NULL))
403     {
404       if (parser->policy)
405         bus_policy_unref (parser->policy);
406       
407       _dbus_string_free (&parser->basedir);
408
409       dbus_free (parser);
410       return NULL;
411     }
412
413   if (parent != NULL)
414     {
415       /* Initialize the parser's limits from the parent. */
416       parser->limits = parent->limits;
417
418       /* Use the parent's list of included_files to avoid
419          circular inclusions. */
420       parser->included_files = parent->included_files;
421     }
422   else
423     {
424
425       /* Make up some numbers! woot! */
426       parser->limits.max_incoming_bytes = _DBUS_ONE_MEGABYTE * 127;
427       parser->limits.max_outgoing_bytes = _DBUS_ONE_MEGABYTE * 127;
428       parser->limits.max_message_size = _DBUS_ONE_MEGABYTE * 32;
429
430       /* We set relatively conservative values here since due to the
431       way SCM_RIGHTS works we need to preallocate an array for the
432       maximum number of file descriptors we can receive. Picking a
433       high value here thus translates directly to more memory
434       allocation. */
435       parser->limits.max_incoming_unix_fds = DBUS_DEFAULT_MESSAGE_UNIX_FDS*4;
436       parser->limits.max_outgoing_unix_fds = DBUS_DEFAULT_MESSAGE_UNIX_FDS*4;
437       parser->limits.max_message_unix_fds = DBUS_DEFAULT_MESSAGE_UNIX_FDS;
438       
439       /* Making this long means the user has to wait longer for an error
440        * message if something screws up, but making it too short means
441        * they might see a false failure.
442        */
443       parser->limits.activation_timeout = 25000; /* 25 seconds */
444
445       /* Making this long risks making a DOS attack easier, but too short
446        * and legitimate auth will fail.  If interactive auth (ask user for
447        * password) is allowed, then potentially it has to be quite long.
448        */
449       parser->limits.auth_timeout = 30000; /* 30 seconds */
450
451       /* Do not allow a fd to stay forever in dbus-daemon
452        * https://bugs.freedesktop.org/show_bug.cgi?id=80559
453        */
454       parser->limits.pending_fd_timeout = 150000; /* 2.5 minutes */
455       
456       parser->limits.max_incomplete_connections = 64;
457       parser->limits.max_connections_per_user = 256;
458       
459       /* Note that max_completed_connections / max_connections_per_user
460        * is the number of users that would have to work together to
461        * DOS all the other users.
462        */
463       parser->limits.max_completed_connections = 2048;
464       
465       parser->limits.max_pending_activations = 512;
466       parser->limits.max_services_per_connection = 512;
467
468       /* For this one, keep in mind that it isn't only the memory used
469        * by the match rules, but slowdown from linearly walking a big
470        * list of them. A client adding more than this is almost
471        * certainly a bad idea for that reason, and should change to a
472        * smaller number of wider-net match rules - getting every last
473        * message to the bus is probably better than having a thousand
474        * match rules.
475        */
476       parser->limits.max_match_rules_per_connection = 512;
477       
478       parser->limits.reply_timeout = -1; /* never */
479
480       /* this is effectively a limit on message queue size for messages
481        * that require a reply
482        */
483       parser->limits.max_replies_per_connection = 128;
484     }
485       
486   parser->refcount = 1;
487       
488   return parser;
489 }
490
491 BusConfigParser *
492 bus_config_parser_ref (BusConfigParser *parser)
493 {
494   _dbus_assert (parser->refcount > 0);
495
496   parser->refcount += 1;
497
498   return parser;
499 }
500
501 void
502 bus_config_parser_unref (BusConfigParser *parser)
503 {
504   _dbus_assert (parser->refcount > 0);
505
506   parser->refcount -= 1;
507
508   if (parser->refcount == 0)
509     {
510       while (parser->stack != NULL)
511         pop_element (parser);
512
513       dbus_free (parser->user);
514       dbus_free (parser->servicehelper);
515       dbus_free (parser->bus_type);
516       dbus_free (parser->pidfile);
517       
518       _dbus_list_foreach (&parser->listen_on,
519                           (DBusForeachFunction) dbus_free,
520                           NULL);
521
522       _dbus_list_clear (&parser->listen_on);
523
524       _dbus_list_foreach (&parser->service_dirs,
525                           (DBusForeachFunction) dbus_free,
526                           NULL);
527
528       _dbus_list_clear (&parser->service_dirs);
529
530       _dbus_list_foreach (&parser->conf_dirs,
531                           (DBusForeachFunction) dbus_free,
532                           NULL);
533
534       _dbus_list_clear (&parser->conf_dirs);
535
536       _dbus_list_foreach (&parser->mechanisms,
537                           (DBusForeachFunction) dbus_free,
538                           NULL);
539
540       _dbus_list_clear (&parser->mechanisms);
541       
542       _dbus_string_free (&parser->basedir);
543
544       if (parser->policy)
545         bus_policy_unref (parser->policy);
546
547       if (parser->service_context_table)
548         _dbus_hash_table_unref (parser->service_context_table);
549       
550       dbus_free (parser);
551     }
552 }
553
554 dbus_bool_t
555 bus_config_parser_check_doctype (BusConfigParser   *parser,
556                                  const char        *doctype,
557                                  DBusError         *error)
558 {
559   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
560
561   if (strcmp (doctype, "busconfig") != 0)
562     {
563       dbus_set_error (error,
564                       DBUS_ERROR_FAILED,
565                       "Configuration file has the wrong document type %s",
566                       doctype);
567       return FALSE;
568     }
569   else
570     return TRUE;
571 }
572
573 typedef struct
574 {
575   const char  *name;
576   const char **retloc;
577 } LocateAttr;
578
579 static dbus_bool_t
580 locate_attributes (BusConfigParser  *parser,
581                    const char       *element_name,
582                    const char      **attribute_names,
583                    const char      **attribute_values,
584                    DBusError        *error,
585                    const char       *first_attribute_name,
586                    const char      **first_attribute_retloc,
587                    ...)
588 {
589   va_list args;
590   const char *name;
591   const char **retloc;
592   int n_attrs;
593 #define MAX_ATTRS 24
594   LocateAttr attrs[MAX_ATTRS];
595   dbus_bool_t retval;
596   int i;
597
598   _dbus_assert (first_attribute_name != NULL);
599   _dbus_assert (first_attribute_retloc != NULL);
600
601   retval = TRUE;
602
603   n_attrs = 1;
604   attrs[0].name = first_attribute_name;
605   attrs[0].retloc = first_attribute_retloc;
606   *first_attribute_retloc = NULL;
607
608   va_start (args, first_attribute_retloc);
609
610   name = va_arg (args, const char*);
611   retloc = va_arg (args, const char**);
612
613   while (name != NULL)
614     {
615       _dbus_assert (retloc != NULL);
616       _dbus_assert (n_attrs < MAX_ATTRS);
617
618       attrs[n_attrs].name = name;
619       attrs[n_attrs].retloc = retloc;
620       n_attrs += 1;
621       *retloc = NULL;
622
623       name = va_arg (args, const char*);
624       retloc = va_arg (args, const char**);
625     }
626
627   va_end (args);
628
629   i = 0;
630   while (attribute_names[i])
631     {
632       int j;
633       dbus_bool_t found;
634       
635       found = FALSE;
636       j = 0;
637       while (j < n_attrs)
638         {
639           if (strcmp (attrs[j].name, attribute_names[i]) == 0)
640             {
641               retloc = attrs[j].retloc;
642
643               if (*retloc != NULL)
644                 {
645                   dbus_set_error (error, DBUS_ERROR_FAILED,
646                                   "Attribute \"%s\" repeated twice on the same <%s> element",
647                                   attrs[j].name, element_name);
648                   retval = FALSE;
649                   goto out;
650                 }
651
652               *retloc = attribute_values[i];
653               found = TRUE;
654             }
655
656           ++j;
657         }
658
659       if (!found)
660         {
661           dbus_set_error (error, DBUS_ERROR_FAILED,
662                           "Attribute \"%s\" is invalid on <%s> element in this context",
663                           attribute_names[i], element_name);
664           retval = FALSE;
665           goto out;
666         }
667
668       ++i;
669     }
670
671  out:
672   return retval;
673 }
674
675 static dbus_bool_t
676 check_no_attributes (BusConfigParser  *parser,
677                      const char       *element_name,
678                      const char      **attribute_names,
679                      const char      **attribute_values,
680                      DBusError        *error)
681 {
682   if (attribute_names[0] != NULL)
683     {
684       dbus_set_error (error, DBUS_ERROR_FAILED,
685                       "Attribute \"%s\" is invalid on <%s> element in this context",
686                       attribute_names[0], element_name);
687       return FALSE;
688     }
689
690   return TRUE;
691 }
692
693 static dbus_bool_t
694 start_busconfig_child (BusConfigParser   *parser,
695                        const char        *element_name,
696                        const char       **attribute_names,
697                        const char       **attribute_values,
698                        DBusError         *error)
699 {
700   ElementType element_type;
701
702   element_type = bus_config_parser_element_name_to_type (element_name);
703
704   if (element_type == ELEMENT_USER)
705     {
706       if (!check_no_attributes (parser, "user", attribute_names, attribute_values, error))
707         return FALSE;
708
709       if (push_element (parser, ELEMENT_USER) == NULL)
710         {
711           BUS_SET_OOM (error);
712           return FALSE;
713         }
714
715       return TRUE;
716     }
717   else if (element_type == ELEMENT_CONFIGTYPE)
718     {
719       if (!check_no_attributes (parser, "type", attribute_names, attribute_values, error))
720         return FALSE;
721
722       if (push_element (parser, ELEMENT_CONFIGTYPE) == NULL)
723         {
724           BUS_SET_OOM (error);
725           return FALSE;
726         }
727
728       return TRUE;
729     }
730   else if (element_type == ELEMENT_FORK)
731     {
732       if (!check_no_attributes (parser, "fork", attribute_names, attribute_values, error))
733         return FALSE;
734
735       if (push_element (parser, ELEMENT_FORK) == NULL)
736         {
737           BUS_SET_OOM (error);
738           return FALSE;
739         }
740
741       parser->fork = TRUE;
742       
743       return TRUE;
744     }
745   else if (element_type == ELEMENT_SYSLOG)
746     {
747       if (!check_no_attributes (parser, "syslog", attribute_names, attribute_values, error))
748         return FALSE;
749
750       if (push_element (parser, ELEMENT_SYSLOG) == NULL)
751         {
752           BUS_SET_OOM (error);
753           return FALSE;
754         }
755       
756       parser->syslog = TRUE;
757       
758       return TRUE;
759     }
760   else if (element_type == ELEMENT_KEEP_UMASK)
761     {
762       if (!check_no_attributes (parser, "keep_umask", attribute_names, attribute_values, error))
763         return FALSE;
764
765       if (push_element (parser, ELEMENT_KEEP_UMASK) == NULL)
766         {
767           BUS_SET_OOM (error);
768           return FALSE;
769         }
770
771       parser->keep_umask = TRUE;
772       
773       return TRUE;
774     }
775   else if (element_type == ELEMENT_PIDFILE)
776     {
777       if (!check_no_attributes (parser, "pidfile", attribute_names, attribute_values, error))
778         return FALSE;
779
780       if (push_element (parser, ELEMENT_PIDFILE) == NULL)
781         {
782           BUS_SET_OOM (error);
783           return FALSE;
784         }
785
786       return TRUE;
787     }
788   else if (element_type == ELEMENT_LISTEN)
789     {
790       if (!check_no_attributes (parser, "listen", attribute_names, attribute_values, error))
791         return FALSE;
792
793       if (push_element (parser, ELEMENT_LISTEN) == NULL)
794         {
795           BUS_SET_OOM (error);
796           return FALSE;
797         }
798
799       return TRUE;
800     }
801   else if (element_type == ELEMENT_AUTH)
802     {
803       if (!check_no_attributes (parser, "auth", attribute_names, attribute_values, error))
804         return FALSE;
805
806       if (push_element (parser, ELEMENT_AUTH) == NULL)
807         {
808           BUS_SET_OOM (error);
809           return FALSE;
810         }
811       
812       return TRUE;
813     }
814   else if (element_type == ELEMENT_SERVICEHELPER)
815     {
816       if (!check_no_attributes (parser, "servicehelper", attribute_names, attribute_values, error))
817         return FALSE;
818
819       if (push_element (parser, ELEMENT_SERVICEHELPER) == NULL)
820         {
821           BUS_SET_OOM (error);
822           return FALSE;
823         }
824
825       return TRUE;
826     }
827   else if (element_type == ELEMENT_INCLUDEDIR)
828     {
829       if (!check_no_attributes (parser, "includedir", attribute_names, attribute_values, error))
830         return FALSE;
831
832       if (push_element (parser, ELEMENT_INCLUDEDIR) == NULL)
833         {
834           BUS_SET_OOM (error);
835           return FALSE;
836         }
837
838       return TRUE;
839     }
840   else if (element_type == ELEMENT_STANDARD_SESSION_SERVICEDIRS)
841     {
842       DBusList *link;
843       DBusList *dirs;
844       dirs = NULL;
845
846       if (!check_no_attributes (parser, "standard_session_servicedirs", attribute_names, attribute_values, error))
847         return FALSE;
848
849       if (push_element (parser, ELEMENT_STANDARD_SESSION_SERVICEDIRS) == NULL)
850         {
851           BUS_SET_OOM (error);
852           return FALSE;
853         }
854
855       if (!_dbus_get_standard_session_servicedirs (&dirs))
856         {
857           BUS_SET_OOM (error);
858           return FALSE;
859         }
860
861         while ((link = _dbus_list_pop_first_link (&dirs)))
862           service_dirs_append_link_unique_or_free (&parser->service_dirs, link);
863
864       return TRUE;
865     }
866   else if (element_type == ELEMENT_STANDARD_SYSTEM_SERVICEDIRS)
867     {
868       DBusList *link;
869       DBusList *dirs;
870       dirs = NULL;
871
872       if (!check_no_attributes (parser, "standard_system_servicedirs", attribute_names, attribute_values, error))
873         return FALSE;
874
875       if (push_element (parser, ELEMENT_STANDARD_SYSTEM_SERVICEDIRS) == NULL)
876         {
877           BUS_SET_OOM (error);
878           return FALSE;
879         }
880
881       if (!_dbus_get_standard_system_servicedirs (&dirs))
882         {
883           BUS_SET_OOM (error);
884           return FALSE;
885         }
886
887         while ((link = _dbus_list_pop_first_link (&dirs)))
888           service_dirs_append_link_unique_or_free (&parser->service_dirs, link);
889
890       return TRUE;
891     }
892   else if (element_type == ELEMENT_ALLOW_ANONYMOUS)
893     {
894       if (!check_no_attributes (parser, "allow_anonymous", attribute_names, attribute_values, error))
895         return FALSE;
896
897       if (push_element (parser, ELEMENT_ALLOW_ANONYMOUS) == NULL)
898         {
899           BUS_SET_OOM (error);
900           return FALSE;
901         }
902
903       parser->allow_anonymous = TRUE;
904       return TRUE;
905     }
906   else if (element_type == ELEMENT_SERVICEDIR)
907     {
908       if (!check_no_attributes (parser, "servicedir", attribute_names, attribute_values, error))
909         return FALSE;
910
911       if (push_element (parser, ELEMENT_SERVICEDIR) == NULL)
912         {
913           BUS_SET_OOM (error);
914           return FALSE;
915         }
916
917       return TRUE;
918     }
919   else if (element_type == ELEMENT_INCLUDE)
920     {
921       Element *e;
922       const char *if_selinux_enabled;
923       const char *ignore_missing;
924       const char *selinux_root_relative;
925
926       if ((e = push_element (parser, ELEMENT_INCLUDE)) == NULL)
927         {
928           BUS_SET_OOM (error);
929           return FALSE;
930         }
931
932       e->d.include.ignore_missing = FALSE;
933       e->d.include.if_selinux_enabled = FALSE;
934       e->d.include.selinux_root_relative = FALSE;
935
936       if (!locate_attributes (parser, "include",
937                               attribute_names,
938                               attribute_values,
939                               error,
940                               "ignore_missing", &ignore_missing,
941                               "if_selinux_enabled", &if_selinux_enabled,
942                               "selinux_root_relative", &selinux_root_relative,
943                               NULL))
944         return FALSE;
945
946       if (ignore_missing != NULL)
947         {
948           if (strcmp (ignore_missing, "yes") == 0)
949             e->d.include.ignore_missing = TRUE;
950           else if (strcmp (ignore_missing, "no") == 0)
951             e->d.include.ignore_missing = FALSE;
952           else
953             {
954               dbus_set_error (error, DBUS_ERROR_FAILED,
955                               "ignore_missing attribute must have value \"yes\" or \"no\"");
956               return FALSE;
957             }
958         }
959
960       if (if_selinux_enabled != NULL)
961         {
962           if (strcmp (if_selinux_enabled, "yes") == 0)
963             e->d.include.if_selinux_enabled = TRUE;
964           else if (strcmp (if_selinux_enabled, "no") == 0)
965             e->d.include.if_selinux_enabled = FALSE;
966           else
967             {
968               dbus_set_error (error, DBUS_ERROR_FAILED,
969                               "if_selinux_enabled attribute must have value"
970                               " \"yes\" or \"no\"");
971               return FALSE;
972             }
973         }
974       
975       if (selinux_root_relative != NULL)
976         {
977           if (strcmp (selinux_root_relative, "yes") == 0)
978             e->d.include.selinux_root_relative = TRUE;
979           else if (strcmp (selinux_root_relative, "no") == 0)
980             e->d.include.selinux_root_relative = FALSE;
981           else
982             {
983               dbus_set_error (error, DBUS_ERROR_FAILED,
984                               "selinux_root_relative attribute must have value"
985                               " \"yes\" or \"no\"");
986               return FALSE;
987             }
988         }
989
990       return TRUE;
991     }
992   else if (element_type == ELEMENT_POLICY)
993     {
994       Element *e;
995       const char *context;
996       const char *user;
997       const char *group;
998       const char *at_console;
999       const char *smack;
1000
1001       if ((e = push_element (parser, ELEMENT_POLICY)) == NULL)
1002         {
1003           BUS_SET_OOM (error);
1004           return FALSE;
1005         }
1006
1007       e->d.policy.type = POLICY_IGNORED;
1008       
1009       if (!locate_attributes (parser, "policy",
1010                               attribute_names,
1011                               attribute_values,
1012                               error,
1013                               "context", &context,
1014                               "user", &user,
1015                               "group", &group,
1016                               "smack", &smack,
1017                               "at_console", &at_console,
1018                               NULL))
1019         return FALSE;
1020
1021       if (((context != NULL) + (user != NULL) + (group != NULL) +
1022           (smack != NULL) + (at_console != NULL)) != 1)
1023         {
1024           dbus_set_error (error, DBUS_ERROR_FAILED,
1025                           "<policy> element must have exactly one of (context|user|group|smack|at_console) attributes");
1026           return FALSE;
1027         }
1028
1029       if (context != NULL)
1030         {
1031           if (strcmp (context, "default") == 0)
1032             {
1033               e->d.policy.type = POLICY_DEFAULT;
1034             }
1035           else if (strcmp (context, "mandatory") == 0)
1036             {
1037               e->d.policy.type = POLICY_MANDATORY;
1038             }
1039           else
1040             {
1041               dbus_set_error (error, DBUS_ERROR_FAILED,
1042                               "context attribute on <policy> must have the value \"default\" or \"mandatory\", not \"%s\"",
1043                               context);
1044               return FALSE;
1045             }
1046         }
1047       else if (user != NULL)
1048         {
1049           DBusString username;
1050           _dbus_string_init_const (&username, user);
1051
1052           if (_dbus_parse_unix_user_from_config (&username,
1053                                                  &e->d.policy.gid_uid_or_at_console))
1054             e->d.policy.type = POLICY_USER;
1055           else
1056             _dbus_warn ("Unknown username \"%s\" in message bus configuration file\n",
1057                         user);
1058         }
1059       else if (group != NULL)
1060         {
1061           DBusString group_name;
1062           _dbus_string_init_const (&group_name, group);
1063
1064           if (_dbus_parse_unix_group_from_config (&group_name,
1065                                                   &e->d.policy.gid_uid_or_at_console))
1066             e->d.policy.type = POLICY_GROUP;
1067           else
1068             _dbus_warn ("Unknown group \"%s\" in message bus configuration file\n",
1069                         group);          
1070         }
1071       else if (smack != NULL)
1072         {
1073           e->d.policy.type = POLICY_SMACK;
1074           e->d.policy.smack_label = _dbus_strdup (smack);
1075           if (e->d.policy.smack_label == NULL)
1076             {
1077               BUS_SET_OOM (error);
1078               return FALSE;
1079             }
1080         }
1081       else if (at_console != NULL)
1082         {
1083            dbus_bool_t t;
1084            t = (strcmp (at_console, "true") == 0);
1085            if (t || strcmp (at_console, "false") == 0)
1086              {
1087                e->d.policy.gid_uid_or_at_console = t; 
1088                e->d.policy.type = POLICY_CONSOLE;
1089              }  
1090            else
1091              {
1092                dbus_set_error (error, DBUS_ERROR_FAILED,
1093                               "Unknown value \"%s\" for at_console in message bus configuration file",
1094                               at_console);
1095
1096                return FALSE;
1097              }
1098         }
1099       else
1100         {
1101           _dbus_assert_not_reached ("all <policy> attributes null and we didn't set error");
1102         }
1103       
1104       return TRUE;
1105     }
1106   else if (element_type == ELEMENT_LIMIT)
1107     {
1108       Element *e;
1109       const char *name;
1110
1111       if ((e = push_element (parser, ELEMENT_LIMIT)) == NULL)
1112         {
1113           BUS_SET_OOM (error);
1114           return FALSE;
1115         }
1116       
1117       if (!locate_attributes (parser, "limit",
1118                               attribute_names,
1119                               attribute_values,
1120                               error,
1121                               "name", &name,
1122                               NULL))
1123         return FALSE;
1124
1125       if (name == NULL)
1126         {
1127           dbus_set_error (error, DBUS_ERROR_FAILED,
1128                           "<limit> element must have a \"name\" attribute");
1129           return FALSE;
1130         }
1131
1132       e->d.limit.name = _dbus_strdup (name);
1133       if (e->d.limit.name == NULL)
1134         {
1135           BUS_SET_OOM (error);
1136           return FALSE;
1137         }
1138
1139       return TRUE;
1140     }
1141   else if (element_type == ELEMENT_SELINUX)
1142     {
1143       if (!check_no_attributes (parser, "selinux", attribute_names, attribute_values, error))
1144         return FALSE;
1145
1146       if (push_element (parser, ELEMENT_SELINUX) == NULL)
1147         {
1148           BUS_SET_OOM (error);
1149           return FALSE;
1150         }
1151
1152       return TRUE;
1153     }
1154   else if (element_type == ELEMENT_APPARMOR)
1155     {
1156       Element *e;
1157       const char *mode;
1158
1159       if ((e = push_element (parser, ELEMENT_APPARMOR)) == NULL)
1160         {
1161           BUS_SET_OOM (error);
1162           return FALSE;
1163         }
1164
1165       if (!locate_attributes (parser, "apparmor",
1166                               attribute_names,
1167                               attribute_values,
1168                               error,
1169                               "mode", &mode,
1170                               NULL))
1171         return FALSE;
1172
1173       return bus_apparmor_set_mode_from_config (mode, error);
1174     }
1175   else
1176     {
1177       dbus_set_error (error, DBUS_ERROR_FAILED,
1178                       "Element <%s> not allowed inside <%s> in configuration file",
1179                       element_name, "busconfig");
1180       return FALSE;
1181     }
1182 }
1183
1184 static dbus_bool_t
1185 append_rule_from_element (BusConfigParser   *parser,
1186                           const char        *element_name,
1187                           const char       **attribute_names,
1188                           const char       **attribute_values,
1189                           dbus_bool_t        allow,
1190                           DBusError         *error)
1191 {
1192   const char *log;
1193   const char *send_interface;
1194   const char *send_member;
1195   const char *send_error;
1196   const char *send_destination;
1197   const char *send_path;
1198   const char *send_type;
1199   const char *receive_interface;
1200   const char *receive_member;
1201   const char *receive_error;
1202   const char *receive_sender;
1203   const char *receive_path;
1204   const char *receive_type;
1205   const char *eavesdrop;
1206   const char *send_requested_reply;
1207   const char *receive_requested_reply;
1208   const char *own;
1209   const char *own_prefix;
1210   const char *user;
1211   const char *group;
1212
1213   BusPolicyRule *rule;
1214   
1215   if (!locate_attributes (parser, element_name,
1216                           attribute_names,
1217                           attribute_values,
1218                           error,
1219                           "send_interface", &send_interface,
1220                           "send_member", &send_member,
1221                           "send_error", &send_error,
1222                           "send_destination", &send_destination,
1223                           "send_path", &send_path,
1224                           "send_type", &send_type,
1225                           "receive_interface", &receive_interface,
1226                           "receive_member", &receive_member,
1227                           "receive_error", &receive_error,
1228                           "receive_sender", &receive_sender,
1229                           "receive_path", &receive_path,
1230                           "receive_type", &receive_type,
1231                           "eavesdrop", &eavesdrop,
1232                           "send_requested_reply", &send_requested_reply,
1233                           "receive_requested_reply", &receive_requested_reply,
1234                           "own", &own,
1235                           "own_prefix", &own_prefix,
1236                           "user", &user,
1237                           "group", &group,
1238                           "log", &log,
1239                           NULL))
1240     return FALSE;
1241
1242   if (!(send_interface || send_member || send_error || send_destination ||
1243         send_type || send_path ||
1244         receive_interface || receive_member || receive_error || receive_sender ||
1245         receive_type || receive_path || eavesdrop ||
1246         send_requested_reply || receive_requested_reply ||
1247         own || own_prefix || user || group))
1248     {
1249       dbus_set_error (error, DBUS_ERROR_FAILED,
1250                       "Element <%s> must have one or more attributes",
1251                       element_name);
1252       return FALSE;
1253     }
1254
1255   if ((send_member && (send_interface == NULL && send_path == NULL)) ||
1256       (receive_member && (receive_interface == NULL && receive_path == NULL)))
1257     {
1258       dbus_set_error (error, DBUS_ERROR_FAILED,
1259                       "On element <%s>, if you specify a member you must specify an interface or a path. Keep in mind that not all messages have an interface field.",
1260                       element_name);
1261       return FALSE;
1262     }
1263   
1264   /* Allowed combinations of elements are:
1265    *
1266    *   base, must be all send or all receive:
1267    *     nothing
1268    *     interface
1269    *     interface + member
1270    *     error
1271    * 
1272    *   base send_ can combine with send_destination, send_path, send_type, send_requested_reply
1273    *   base receive_ with receive_sender, receive_path, receive_type, receive_requested_reply, eavesdrop
1274    *
1275    *   user, group, own, own_prefix must occur alone
1276    *
1277    * Pretty sure the below stuff is broken, FIXME think about it more.
1278    */
1279
1280   if ((send_interface && (send_error ||
1281                           receive_interface ||
1282                           receive_member ||
1283                           receive_error ||
1284                           receive_sender ||
1285                           receive_requested_reply ||
1286                           own || own_prefix ||
1287                           user ||
1288                           group)) ||
1289
1290       (send_member && (send_error ||
1291                        receive_interface ||
1292                        receive_member ||
1293                        receive_error ||
1294                        receive_sender ||
1295                        receive_requested_reply ||
1296                        own || own_prefix ||
1297                        user ||
1298                        group)) ||
1299
1300       (send_error && (receive_interface ||
1301                       receive_member ||
1302                       receive_error ||
1303                       receive_sender ||
1304                       receive_requested_reply ||
1305                       own || own_prefix ||
1306                       user ||
1307                       group)) ||
1308
1309       (send_destination && (receive_interface ||
1310                             receive_member ||
1311                             receive_error ||
1312                             receive_sender ||
1313                             receive_requested_reply ||
1314                             own || own_prefix ||
1315                             user ||
1316                             group)) ||
1317
1318       (send_type && (receive_interface ||
1319                      receive_member ||
1320                      receive_error ||
1321                      receive_sender ||
1322                      receive_requested_reply ||
1323                      own || own_prefix ||
1324                      user ||
1325                      group)) ||
1326
1327       (send_path && (receive_interface ||
1328                      receive_member ||
1329                      receive_error ||
1330                      receive_sender ||
1331                      receive_requested_reply ||
1332                      own || own_prefix ||
1333                      user ||
1334                      group)) ||
1335
1336       (send_requested_reply && (receive_interface ||
1337                                 receive_member ||
1338                                 receive_error ||
1339                                 receive_sender ||
1340                                 receive_requested_reply ||
1341                                 own || own_prefix ||
1342                                 user ||
1343                                 group)) ||
1344
1345       (receive_interface && (receive_error ||
1346                              own || own_prefix ||
1347                              user ||
1348                              group)) ||
1349
1350       (receive_member && (receive_error ||
1351                           own || own_prefix ||
1352                           user ||
1353                           group)) ||
1354
1355       (receive_error && (own || own_prefix ||
1356                          user ||
1357                          group)) ||
1358
1359       (eavesdrop && (own || own_prefix ||
1360                      user ||
1361                      group)) ||
1362
1363       (receive_requested_reply && (own || own_prefix ||
1364                                    user ||
1365                                    group)) ||
1366
1367       (own && (own_prefix || user || group)) ||
1368
1369       (own_prefix && (own || user || group)) ||
1370
1371       (user && group))
1372     {
1373       dbus_set_error (error, DBUS_ERROR_FAILED,
1374                       "Invalid combination of attributes on element <%s>",
1375                       element_name);
1376       return FALSE;
1377     }
1378   
1379   rule = NULL;
1380
1381   /* In BusPolicyRule, NULL represents wildcard.
1382    * In the config file, '*' represents it.
1383    */
1384 #define IS_WILDCARD(str) ((str) && ((str)[0]) == '*' && ((str)[1]) == '\0')
1385
1386   if (send_interface || send_member || send_error || send_destination ||
1387       send_path || send_type || send_requested_reply)
1388     {
1389       int message_type;
1390       
1391       if (IS_WILDCARD (send_interface))
1392         send_interface = NULL;
1393       if (IS_WILDCARD (send_member))
1394         send_member = NULL;
1395       if (IS_WILDCARD (send_error))
1396         send_error = NULL;
1397       if (IS_WILDCARD (send_destination))
1398         send_destination = NULL;
1399       if (IS_WILDCARD (send_path))
1400         send_path = NULL;
1401       if (IS_WILDCARD (send_type))
1402         send_type = NULL;
1403
1404       message_type = DBUS_MESSAGE_TYPE_INVALID;
1405       if (send_type != NULL)
1406         {
1407           message_type = dbus_message_type_from_string (send_type);
1408           if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1409             {
1410               dbus_set_error (error, DBUS_ERROR_FAILED,
1411                               "Bad message type \"%s\"",
1412                               send_type);
1413               return FALSE;
1414             }
1415         }
1416
1417       if (eavesdrop &&
1418           !(strcmp (eavesdrop, "true") == 0 ||
1419             strcmp (eavesdrop, "false") == 0))
1420         {
1421           dbus_set_error (error, DBUS_ERROR_FAILED,
1422                           "Bad value \"%s\" for %s attribute, must be true or false",
1423                           "eavesdrop", eavesdrop);
1424           return FALSE;
1425         }
1426
1427       if (send_requested_reply &&
1428           !(strcmp (send_requested_reply, "true") == 0 ||
1429             strcmp (send_requested_reply, "false") == 0))
1430         {
1431           dbus_set_error (error, DBUS_ERROR_FAILED,
1432                           "Bad value \"%s\" for %s attribute, must be true or false",
1433                           "send_requested_reply", send_requested_reply);
1434           return FALSE;
1435         }
1436       
1437       rule = bus_policy_rule_new (BUS_POLICY_RULE_SEND, allow); 
1438       if (rule == NULL)
1439         goto nomem;
1440       
1441       if (eavesdrop)
1442         rule->d.send.eavesdrop = (strcmp (eavesdrop, "true") == 0);
1443
1444       if (log)
1445         rule->d.send.log = (strcmp (log, "true") == 0);
1446
1447       if (send_requested_reply)
1448         rule->d.send.requested_reply = (strcmp (send_requested_reply, "true") == 0);
1449
1450       rule->d.send.message_type = message_type;
1451       rule->d.send.path = _dbus_strdup (send_path);
1452       rule->d.send.interface = _dbus_strdup (send_interface);
1453       rule->d.send.member = _dbus_strdup (send_member);
1454       rule->d.send.error = _dbus_strdup (send_error);
1455       rule->d.send.destination = _dbus_strdup (send_destination);
1456       if (send_path && rule->d.send.path == NULL)
1457         goto nomem;
1458       if (send_interface && rule->d.send.interface == NULL)
1459         goto nomem;
1460       if (send_member && rule->d.send.member == NULL)
1461         goto nomem;
1462       if (send_error && rule->d.send.error == NULL)
1463         goto nomem;
1464       if (send_destination && rule->d.send.destination == NULL)
1465         goto nomem;
1466     }
1467   else if (receive_interface || receive_member || receive_error || receive_sender ||
1468            receive_path || receive_type || eavesdrop || receive_requested_reply)
1469     {
1470       int message_type;
1471       
1472       if (IS_WILDCARD (receive_interface))
1473         receive_interface = NULL;
1474       if (IS_WILDCARD (receive_member))
1475         receive_member = NULL;
1476       if (IS_WILDCARD (receive_error))
1477         receive_error = NULL;
1478       if (IS_WILDCARD (receive_sender))
1479         receive_sender = NULL;
1480       if (IS_WILDCARD (receive_path))
1481         receive_path = NULL;
1482       if (IS_WILDCARD (receive_type))
1483         receive_type = NULL;
1484
1485       message_type = DBUS_MESSAGE_TYPE_INVALID;
1486       if (receive_type != NULL)
1487         {
1488           message_type = dbus_message_type_from_string (receive_type);
1489           if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1490             {
1491               dbus_set_error (error, DBUS_ERROR_FAILED,
1492                               "Bad message type \"%s\"",
1493                               receive_type);
1494               return FALSE;
1495             }
1496         }
1497
1498
1499       if (eavesdrop &&
1500           !(strcmp (eavesdrop, "true") == 0 ||
1501             strcmp (eavesdrop, "false") == 0))
1502         {
1503           dbus_set_error (error, DBUS_ERROR_FAILED,
1504                           "Bad value \"%s\" for %s attribute, must be true or false",
1505                           "eavesdrop", eavesdrop);
1506           return FALSE;
1507         }
1508
1509       if (receive_requested_reply &&
1510           !(strcmp (receive_requested_reply, "true") == 0 ||
1511             strcmp (receive_requested_reply, "false") == 0))
1512         {
1513           dbus_set_error (error, DBUS_ERROR_FAILED,
1514                           "Bad value \"%s\" for %s attribute, must be true or false",
1515                           "receive_requested_reply", receive_requested_reply);
1516           return FALSE;
1517         }
1518       
1519       rule = bus_policy_rule_new (BUS_POLICY_RULE_RECEIVE, allow); 
1520       if (rule == NULL)
1521         goto nomem;
1522
1523       if (eavesdrop)
1524         rule->d.receive.eavesdrop = (strcmp (eavesdrop, "true") == 0);
1525
1526       if (receive_requested_reply)
1527         rule->d.receive.requested_reply = (strcmp (receive_requested_reply, "true") == 0);
1528       
1529       rule->d.receive.message_type = message_type;
1530       rule->d.receive.path = _dbus_strdup (receive_path);
1531       rule->d.receive.interface = _dbus_strdup (receive_interface);
1532       rule->d.receive.member = _dbus_strdup (receive_member);
1533       rule->d.receive.error = _dbus_strdup (receive_error);
1534       rule->d.receive.origin = _dbus_strdup (receive_sender);
1535
1536       if (receive_path && rule->d.receive.path == NULL)
1537         goto nomem;
1538       if (receive_interface && rule->d.receive.interface == NULL)
1539         goto nomem;
1540       if (receive_member && rule->d.receive.member == NULL)
1541         goto nomem;
1542       if (receive_error && rule->d.receive.error == NULL)
1543         goto nomem;
1544       if (receive_sender && rule->d.receive.origin == NULL)
1545         goto nomem;
1546     }
1547   else if (own || own_prefix)
1548     {
1549       rule = bus_policy_rule_new (BUS_POLICY_RULE_OWN, allow); 
1550       if (rule == NULL)
1551         goto nomem;
1552
1553       if (own)
1554         {
1555           if (IS_WILDCARD (own))
1556             own = NULL;
1557       
1558           rule->d.own.prefix = 0;
1559           rule->d.own.service_name = _dbus_strdup (own);
1560           if (own && rule->d.own.service_name == NULL)
1561             goto nomem;
1562         }
1563       else
1564         {
1565           rule->d.own.prefix = 1;
1566           rule->d.own.service_name = _dbus_strdup (own_prefix);
1567           if (rule->d.own.service_name == NULL)
1568             goto nomem;
1569         }
1570     }
1571   else if (user)
1572     {      
1573       if (IS_WILDCARD (user))
1574         {
1575           rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow); 
1576           if (rule == NULL)
1577             goto nomem;
1578
1579           rule->d.user.uid = DBUS_UID_UNSET;
1580         }
1581       else
1582         {
1583           DBusString username;
1584           dbus_uid_t uid;
1585           
1586           _dbus_string_init_const (&username, user);
1587       
1588           if (_dbus_parse_unix_user_from_config (&username, &uid))
1589             {
1590               rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow); 
1591               if (rule == NULL)
1592                 goto nomem;
1593
1594               rule->d.user.uid = uid;
1595             }
1596           else
1597             {
1598               _dbus_warn ("Unknown username \"%s\" on element <%s>\n",
1599                           user, element_name);
1600             }
1601         }
1602     }
1603   else if (group)
1604     {
1605       if (IS_WILDCARD (group))
1606         {
1607           rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow); 
1608           if (rule == NULL)
1609             goto nomem;
1610
1611           rule->d.group.gid = DBUS_GID_UNSET;
1612         }
1613       else
1614         {
1615           DBusString groupname;
1616           dbus_gid_t gid;
1617           
1618           _dbus_string_init_const (&groupname, group);
1619           
1620           if (_dbus_parse_unix_group_from_config (&groupname, &gid))
1621             {
1622               rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow); 
1623               if (rule == NULL)
1624                 goto nomem;
1625
1626               rule->d.group.gid = gid;
1627             }
1628           else
1629             {
1630               _dbus_warn ("Unknown group \"%s\" on element <%s>\n",
1631                           group, element_name);
1632             }
1633         }
1634     }
1635   else
1636     _dbus_assert_not_reached ("Did not handle some combination of attributes on <allow> or <deny>");
1637
1638   if (rule != NULL)
1639     {
1640       Element *pe;
1641       
1642       pe = peek_element (parser);      
1643       _dbus_assert (pe != NULL);
1644       _dbus_assert (pe->type == ELEMENT_POLICY);
1645
1646       switch (pe->d.policy.type)
1647         {
1648         case POLICY_IGNORED:
1649           /* drop the rule on the floor */
1650           break;
1651           
1652         case POLICY_DEFAULT:
1653           if (!bus_policy_append_default_rule (parser->policy, rule))
1654             goto nomem;
1655           break;
1656         case POLICY_MANDATORY:
1657           if (!bus_policy_append_mandatory_rule (parser->policy, rule))
1658             goto nomem;
1659           break;
1660         case POLICY_USER:
1661           if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1662             {
1663               dbus_set_error (error, DBUS_ERROR_FAILED,
1664                               "<%s> rule cannot be per-user because it has bus-global semantics",
1665                               element_name);
1666               goto failed;
1667             }
1668           
1669           if (!bus_policy_append_user_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1670                                             rule))
1671             goto nomem;
1672           break;
1673         case POLICY_GROUP:
1674           if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1675             {
1676               dbus_set_error (error, DBUS_ERROR_FAILED,
1677                               "<%s> rule cannot be per-group because it has bus-global semantics",
1678                               element_name);
1679               goto failed;
1680             }
1681           
1682           if (!bus_policy_append_group_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1683                                              rule))
1684             goto nomem;
1685           break;
1686         case POLICY_SMACK:
1687           if (!bus_policy_append_smack_rule (parser->policy, pe->d.policy.smack_label, rule))
1688             goto nomem;
1689           break;
1690         case POLICY_CONSOLE:
1691           if (!bus_policy_append_console_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1692                                                rule))
1693             goto nomem;
1694           break;
1695         }
1696  
1697       bus_policy_rule_unref (rule);
1698       rule = NULL;
1699     }
1700   
1701   return TRUE;
1702
1703  nomem:
1704   BUS_SET_OOM (error);
1705  failed:
1706   if (rule)
1707     bus_policy_rule_unref (rule);
1708   return FALSE;
1709 }
1710
1711 static dbus_bool_t
1712 start_policy_child (BusConfigParser   *parser,
1713                     const char        *element_name,
1714                     const char       **attribute_names,
1715                     const char       **attribute_values,
1716                     DBusError         *error)
1717 {
1718   if (strcmp (element_name, "allow") == 0)
1719     {
1720       if (!append_rule_from_element (parser, element_name,
1721                                      attribute_names, attribute_values,
1722                                      TRUE, error))
1723         return FALSE;
1724       
1725       if (push_element (parser, ELEMENT_ALLOW) == NULL)
1726         {
1727           BUS_SET_OOM (error);
1728           return FALSE;
1729         }
1730       
1731       return TRUE;
1732     }
1733   else if (strcmp (element_name, "deny") == 0)
1734     {
1735       if (!append_rule_from_element (parser, element_name,
1736                                      attribute_names, attribute_values,
1737                                      FALSE, error))
1738         return FALSE;
1739       
1740       if (push_element (parser, ELEMENT_DENY) == NULL)
1741         {
1742           BUS_SET_OOM (error);
1743           return FALSE;
1744         }
1745       
1746       return TRUE;
1747     }
1748   else
1749     {
1750       dbus_set_error (error, DBUS_ERROR_FAILED,
1751                       "Element <%s> not allowed inside <%s> in configuration file",
1752                       element_name, "policy");
1753       return FALSE;
1754     }
1755 }
1756
1757 static dbus_bool_t
1758 start_selinux_child (BusConfigParser   *parser,
1759                      const char        *element_name,
1760                      const char       **attribute_names,
1761                      const char       **attribute_values,
1762                      DBusError         *error)
1763 {
1764   char *own_copy;
1765   char *context_copy;
1766
1767   own_copy = NULL;
1768   context_copy = NULL;
1769
1770   if (strcmp (element_name, "associate") == 0)
1771     {
1772       const char *own;
1773       const char *context;
1774       
1775       if (!locate_attributes (parser, "associate",
1776                               attribute_names,
1777                               attribute_values,
1778                               error,
1779                               "own", &own,
1780                               "context", &context,
1781                               NULL))
1782         return FALSE;
1783       
1784       if (push_element (parser, ELEMENT_ASSOCIATE) == NULL)
1785         {
1786           BUS_SET_OOM (error);
1787           return FALSE;
1788         }
1789
1790       if (own == NULL || context == NULL)
1791         {
1792           dbus_set_error (error, DBUS_ERROR_FAILED,
1793                           "Element <associate> must have attributes own=\"<servicename>\" and context=\"<selinux context>\"");
1794           return FALSE;
1795         }
1796
1797       own_copy = _dbus_strdup (own);
1798       if (own_copy == NULL)
1799         goto oom;
1800       context_copy = _dbus_strdup (context);
1801       if (context_copy == NULL)
1802         goto oom;
1803
1804       if (!_dbus_hash_table_insert_string (parser->service_context_table,
1805                                            own_copy, context_copy))
1806         goto oom;
1807
1808       return TRUE;
1809     }
1810   else
1811     {
1812       dbus_set_error (error, DBUS_ERROR_FAILED,
1813                       "Element <%s> not allowed inside <%s> in configuration file",
1814                       element_name, "selinux");
1815       return FALSE;
1816     }
1817
1818  oom:
1819   if (own_copy)
1820     dbus_free (own_copy);
1821
1822   if (context_copy)  
1823     dbus_free (context_copy);
1824
1825   BUS_SET_OOM (error);
1826   return FALSE;
1827 }
1828
1829 dbus_bool_t
1830 bus_config_parser_start_element (BusConfigParser   *parser,
1831                                  const char        *element_name,
1832                                  const char       **attribute_names,
1833                                  const char       **attribute_values,
1834                                  DBusError         *error)
1835 {
1836   ElementType t;
1837
1838   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1839
1840   /* printf ("START: %s\n", element_name); */
1841   
1842   t = top_element_type (parser);
1843
1844   if (t == ELEMENT_NONE)
1845     {
1846       if (strcmp (element_name, "busconfig") == 0)
1847         {
1848           if (!check_no_attributes (parser, "busconfig", attribute_names, attribute_values, error))
1849             return FALSE;
1850           
1851           if (push_element (parser, ELEMENT_BUSCONFIG) == NULL)
1852             {
1853               BUS_SET_OOM (error);
1854               return FALSE;
1855             }
1856
1857           return TRUE;
1858         }
1859       else
1860         {
1861           dbus_set_error (error, DBUS_ERROR_FAILED,
1862                           "Unknown element <%s> at root of configuration file",
1863                           element_name);
1864           return FALSE;
1865         }
1866     }
1867   else if (t == ELEMENT_BUSCONFIG)
1868     {
1869       return start_busconfig_child (parser, element_name,
1870                                     attribute_names, attribute_values,
1871                                     error);
1872     }
1873   else if (t == ELEMENT_POLICY)
1874     {
1875       return start_policy_child (parser, element_name,
1876                                  attribute_names, attribute_values,
1877                                  error);
1878     }
1879   else if (t == ELEMENT_SELINUX)
1880     {
1881       return start_selinux_child (parser, element_name,
1882                                   attribute_names, attribute_values,
1883                                   error);
1884     }
1885   else
1886     {
1887       dbus_set_error (error, DBUS_ERROR_FAILED,
1888                       "Element <%s> is not allowed in this context",
1889                       element_name);
1890       return FALSE;
1891     }  
1892 }
1893
1894 static dbus_bool_t
1895 set_limit (BusConfigParser *parser,
1896            const char      *name,
1897            long             value,
1898            DBusError       *error)
1899 {
1900   dbus_bool_t must_be_positive;
1901   dbus_bool_t must_be_int;
1902
1903   must_be_int = FALSE;
1904   must_be_positive = FALSE;
1905   
1906   if (strcmp (name, "max_incoming_bytes") == 0)
1907     {
1908       must_be_positive = TRUE;
1909       parser->limits.max_incoming_bytes = value;
1910     }
1911   else if (strcmp (name, "max_incoming_unix_fds") == 0)
1912     {
1913       must_be_positive = TRUE;
1914       parser->limits.max_incoming_unix_fds = value;
1915     }
1916   else if (strcmp (name, "max_outgoing_bytes") == 0)
1917     {
1918       must_be_positive = TRUE;
1919       parser->limits.max_outgoing_bytes = value;
1920     }
1921   else if (strcmp (name, "max_outgoing_unix_fds") == 0)
1922     {
1923       must_be_positive = TRUE;
1924       parser->limits.max_outgoing_unix_fds = value;
1925     }
1926   else if (strcmp (name, "max_message_size") == 0)
1927     {
1928       must_be_positive = TRUE;
1929       parser->limits.max_message_size = value;
1930     }
1931   else if (strcmp (name, "max_message_unix_fds") == 0)
1932     {
1933       must_be_positive = TRUE;
1934       parser->limits.max_message_unix_fds = value;
1935     }
1936   else if (strcmp (name, "service_start_timeout") == 0)
1937     {
1938       must_be_positive = TRUE;
1939       must_be_int = TRUE;
1940       parser->limits.activation_timeout = value;
1941     }
1942   else if (strcmp (name, "auth_timeout") == 0)
1943     {
1944       must_be_positive = TRUE;
1945       must_be_int = TRUE;
1946       parser->limits.auth_timeout = value;
1947     }
1948   else if (strcmp (name, "pending_fd_timeout") == 0)
1949     {
1950       must_be_positive = TRUE;
1951       must_be_int = TRUE;
1952       parser->limits.pending_fd_timeout = value;
1953     }
1954   else if (strcmp (name, "reply_timeout") == 0)
1955     {
1956       must_be_positive = TRUE;
1957       must_be_int = TRUE;
1958       parser->limits.reply_timeout = value;
1959     }
1960   else if (strcmp (name, "max_completed_connections") == 0)
1961     {
1962       must_be_positive = TRUE;
1963       must_be_int = TRUE;
1964       parser->limits.max_completed_connections = value;
1965     }
1966   else if (strcmp (name, "max_incomplete_connections") == 0)
1967     {
1968       must_be_positive = TRUE;
1969       must_be_int = TRUE;
1970       parser->limits.max_incomplete_connections = value;
1971     }
1972   else if (strcmp (name, "max_connections_per_user") == 0)
1973     {
1974       must_be_positive = TRUE;
1975       must_be_int = TRUE;
1976       parser->limits.max_connections_per_user = value;
1977     }
1978   else if (strcmp (name, "max_pending_service_starts") == 0)
1979     {
1980       must_be_positive = TRUE;
1981       must_be_int = TRUE;
1982       parser->limits.max_pending_activations = value;
1983     }
1984   else if (strcmp (name, "max_names_per_connection") == 0)
1985     {
1986       must_be_positive = TRUE;
1987       must_be_int = TRUE;
1988       parser->limits.max_services_per_connection = value;
1989     }
1990   else if (strcmp (name, "max_match_rules_per_connection") == 0)
1991     {
1992       must_be_positive = TRUE;
1993       must_be_int = TRUE;
1994       parser->limits.max_match_rules_per_connection = value;
1995     }
1996   else if (strcmp (name, "max_replies_per_connection") == 0)
1997     {
1998       must_be_positive = TRUE;
1999       must_be_int = TRUE;
2000       parser->limits.max_replies_per_connection = value;
2001     }
2002   else
2003     {
2004       dbus_set_error (error, DBUS_ERROR_FAILED,
2005                       "There is no limit called \"%s\"\n",
2006                       name);
2007       return FALSE;
2008     }
2009   
2010   if (must_be_positive && value < 0)
2011     {
2012       dbus_set_error (error, DBUS_ERROR_FAILED,
2013                       "<limit name=\"%s\"> must be a positive number\n",
2014                       name);
2015       return FALSE;
2016     }
2017
2018   if (must_be_int &&
2019       (value < _DBUS_INT_MIN || value > _DBUS_INT_MAX))
2020     {
2021       dbus_set_error (error, DBUS_ERROR_FAILED,
2022                       "<limit name=\"%s\"> value is too large\n",
2023                       name);
2024       return FALSE;
2025     }
2026
2027   return TRUE;  
2028 }
2029
2030 dbus_bool_t
2031 bus_config_parser_end_element (BusConfigParser   *parser,
2032                                const char        *element_name,
2033                                DBusError         *error)
2034 {
2035   ElementType t;
2036   const char *n;
2037   Element *e;
2038
2039   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2040
2041   /* printf ("END: %s\n", element_name); */
2042   
2043   t = top_element_type (parser);
2044
2045   if (t == ELEMENT_NONE)
2046     {
2047       /* should probably be an assertion failure but
2048        * being paranoid about XML parsers
2049        */
2050       dbus_set_error (error, DBUS_ERROR_FAILED,
2051                       "XML parser ended element with no element on the stack");
2052       return FALSE;
2053     }
2054
2055   n = bus_config_parser_element_type_to_name (t);
2056   _dbus_assert (n != NULL);
2057   if (strcmp (n, element_name) != 0)
2058     {
2059       /* should probably be an assertion failure but
2060        * being paranoid about XML parsers
2061        */
2062       dbus_set_error (error, DBUS_ERROR_FAILED,
2063                       "XML element <%s> ended but topmost element on the stack was <%s>",
2064                       element_name, n);
2065       return FALSE;
2066     }
2067
2068   e = peek_element (parser);
2069   _dbus_assert (e != NULL);
2070
2071   switch (e->type)
2072     {
2073     case ELEMENT_NONE:
2074       _dbus_assert_not_reached ("element in stack has no type");
2075       break;
2076
2077     case ELEMENT_INCLUDE:
2078     case ELEMENT_USER:
2079     case ELEMENT_CONFIGTYPE:
2080     case ELEMENT_LISTEN:
2081     case ELEMENT_PIDFILE:
2082     case ELEMENT_AUTH:
2083     case ELEMENT_SERVICEDIR:
2084     case ELEMENT_SERVICEHELPER:
2085     case ELEMENT_INCLUDEDIR:
2086     case ELEMENT_LIMIT:
2087       if (!e->had_content)
2088         {
2089           dbus_set_error (error, DBUS_ERROR_FAILED,
2090                           "XML element <%s> was expected to have content inside it",
2091                           bus_config_parser_element_type_to_name (e->type));
2092           return FALSE;
2093         }
2094
2095       if (e->type == ELEMENT_LIMIT)
2096         {
2097           if (!set_limit (parser, e->d.limit.name, e->d.limit.value,
2098                           error))
2099             return FALSE;
2100         }
2101       break;
2102
2103     case ELEMENT_BUSCONFIG:
2104     case ELEMENT_POLICY:
2105     case ELEMENT_ALLOW:
2106     case ELEMENT_DENY:
2107     case ELEMENT_FORK:
2108     case ELEMENT_SYSLOG:
2109     case ELEMENT_KEEP_UMASK:
2110     case ELEMENT_SELINUX:
2111     case ELEMENT_ASSOCIATE:
2112     case ELEMENT_STANDARD_SESSION_SERVICEDIRS:
2113     case ELEMENT_STANDARD_SYSTEM_SERVICEDIRS:
2114     case ELEMENT_ALLOW_ANONYMOUS:
2115     case ELEMENT_APPARMOR:
2116       break;
2117     }
2118
2119   pop_element (parser);
2120
2121   return TRUE;
2122 }
2123
2124 static dbus_bool_t
2125 all_whitespace (const DBusString *str)
2126 {
2127   int i;
2128
2129   _dbus_string_skip_white (str, 0, &i);
2130
2131   return i == _dbus_string_get_length (str);
2132 }
2133
2134 static dbus_bool_t
2135 make_full_path (const DBusString *basedir,
2136                 const DBusString *filename,
2137                 DBusString       *full_path)
2138 {
2139   if (_dbus_path_is_absolute (filename))
2140     {
2141       return _dbus_string_copy (filename, 0, full_path, 0);
2142     }
2143   else
2144     {
2145       if (!_dbus_string_copy (basedir, 0, full_path, 0))
2146         return FALSE;
2147       
2148       if (!_dbus_concat_dir_and_file (full_path, filename))
2149         return FALSE;
2150
2151       return TRUE;
2152     }
2153 }
2154
2155 static dbus_bool_t
2156 include_file (BusConfigParser   *parser,
2157               const DBusString  *filename,
2158               dbus_bool_t        ignore_missing,
2159               DBusError         *error)
2160 {
2161   /* FIXME good test case for this would load each config file in the
2162    * test suite both alone, and as an include, and check
2163    * that the result is the same
2164    */
2165   BusConfigParser *included;
2166   const char *filename_str;
2167   DBusError tmp_error;
2168         
2169   dbus_error_init (&tmp_error);
2170
2171   filename_str = _dbus_string_get_const_data (filename);
2172
2173   /* Check to make sure this file hasn't already been included. */
2174   if (seen_include (parser, filename))
2175     {
2176       dbus_set_error (error, DBUS_ERROR_FAILED,
2177                       "Circular inclusion of file '%s'",
2178                       filename_str);
2179       return FALSE;
2180     }
2181   
2182   if (! _dbus_list_append (&parser->included_files, (void *) filename_str))
2183     {
2184       BUS_SET_OOM (error);
2185       return FALSE;
2186     }
2187
2188   /* Since parser is passed in as the parent, included
2189      inherits parser's limits. */
2190   included = bus_config_load (filename, FALSE, parser, &tmp_error);
2191
2192   _dbus_list_pop_last (&parser->included_files);
2193
2194   if (included == NULL)
2195     {
2196       _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
2197
2198       if (dbus_error_has_name (&tmp_error, DBUS_ERROR_FILE_NOT_FOUND) &&
2199           ignore_missing)
2200         {
2201           dbus_error_free (&tmp_error);
2202           return TRUE;
2203         }
2204       else
2205         {
2206           dbus_move_error (&tmp_error, error);
2207           return FALSE;
2208         }
2209     }
2210   else
2211     {
2212       _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
2213
2214       if (!merge_included (parser, included, error))
2215         {
2216           bus_config_parser_unref (included);
2217           return FALSE;
2218         }
2219
2220       /* Copy included's limits back to parser. */
2221       parser->limits = included->limits;
2222
2223       bus_config_parser_unref (included);
2224       return TRUE;
2225     }
2226 }
2227
2228 static dbus_bool_t
2229 servicehelper_path (BusConfigParser   *parser,
2230                     const DBusString  *filename,
2231                     DBusError         *error)
2232 {
2233   const char *filename_str;
2234   char *servicehelper;
2235
2236   filename_str = _dbus_string_get_const_data (filename);
2237
2238   /* copy to avoid overwriting with NULL on OOM */
2239   servicehelper = _dbus_strdup (filename_str);
2240
2241   /* check for OOM */
2242   if (servicehelper == NULL)
2243     {
2244       BUS_SET_OOM (error);
2245       return FALSE;
2246     }
2247
2248   /* save the latest servicehelper only if not OOM */
2249   dbus_free (parser->servicehelper);
2250   parser->servicehelper = servicehelper;
2251
2252   /* We don't check whether the helper exists; instead we
2253    * would just fail to ever activate anything if it doesn't.
2254    * This allows an admin to fix the problem if it doesn't exist.
2255    * It also allows the parser test suite to successfully parse
2256    * test cases without installing the helper. ;-)
2257    */
2258   
2259   return TRUE;
2260 }
2261
2262 static dbus_bool_t
2263 include_dir (BusConfigParser   *parser,
2264              const DBusString  *dirname,
2265              DBusError         *error)
2266 {
2267   DBusString filename;
2268   dbus_bool_t retval;
2269   DBusError tmp_error;
2270   DBusDirIter *dir;
2271   char *s;
2272   
2273   if (!_dbus_string_init (&filename))
2274     {
2275       BUS_SET_OOM (error);
2276       return FALSE;
2277     }
2278
2279   retval = FALSE;
2280   
2281   dir = _dbus_directory_open (dirname, error);
2282
2283   if (dir == NULL)
2284     {
2285       if (dbus_error_has_name (error, DBUS_ERROR_FILE_NOT_FOUND))
2286         {
2287           dbus_error_free (error);
2288           goto success;
2289         }
2290       else
2291         goto failed;
2292     }
2293
2294   dbus_error_init (&tmp_error);
2295   while (_dbus_directory_get_next_file (dir, &filename, &tmp_error))
2296     {
2297       DBusString full_path;
2298
2299       if (!_dbus_string_init (&full_path))
2300         {
2301           BUS_SET_OOM (error);
2302           goto failed;
2303         }
2304
2305       if (!_dbus_string_copy (dirname, 0, &full_path, 0))
2306         {
2307           BUS_SET_OOM (error);
2308           _dbus_string_free (&full_path);
2309           goto failed;
2310         }      
2311
2312       if (!_dbus_concat_dir_and_file (&full_path, &filename))
2313         {
2314           BUS_SET_OOM (error);
2315           _dbus_string_free (&full_path);
2316           goto failed;
2317         }
2318       
2319       if (_dbus_string_ends_with_c_str (&full_path, ".conf"))
2320         {
2321           if (!include_file (parser, &full_path, TRUE, error))
2322             {
2323               if (dbus_error_is_set (error))
2324                 {
2325                   /* We log to syslog unconditionally here, because this is
2326                    * the configuration parser, so we don't yet know whether
2327                    * this bus is going to want to write to syslog! (There's
2328                    * also some layer inversion going on, if we want to use
2329                    * the bus context.) */
2330                   _dbus_system_log (DBUS_SYSTEM_LOG_INFO,
2331                                     "Encountered error '%s' while parsing '%s'\n",
2332                                     error->message,
2333                                     _dbus_string_get_const_data (&full_path));
2334                   dbus_error_free (error);
2335                 }
2336             }
2337         }
2338
2339       _dbus_string_free (&full_path);
2340     }
2341
2342   if (dbus_error_is_set (&tmp_error))
2343     {
2344       dbus_move_error (&tmp_error, error);
2345       goto failed;
2346     }
2347
2348
2349   if (!_dbus_string_copy_data (dirname, &s))
2350     {
2351       BUS_SET_OOM (error);
2352       goto failed;
2353     }
2354
2355   if (!_dbus_list_append (&parser->conf_dirs, s))
2356     {
2357       dbus_free (s);
2358       BUS_SET_OOM (error);
2359       goto failed;
2360     }
2361
2362  success:
2363   retval = TRUE;
2364   
2365  failed:
2366   _dbus_string_free (&filename);
2367   
2368   if (dir)
2369     _dbus_directory_close (dir);
2370
2371   return retval;
2372 }
2373
2374 dbus_bool_t
2375 bus_config_parser_content (BusConfigParser   *parser,
2376                            const DBusString  *content,
2377                            DBusError         *error)
2378 {
2379   Element *e;
2380
2381   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2382
2383 #if 0
2384   {
2385     const char *c_str;
2386     
2387     _dbus_string_get_const_data (content, &c_str);
2388
2389     printf ("CONTENT %d bytes: %s\n", _dbus_string_get_length (content), c_str);
2390   }
2391 #endif
2392   
2393   e = peek_element (parser);
2394   if (e == NULL)
2395     {
2396       dbus_set_error (error, DBUS_ERROR_FAILED,
2397                       "Text content outside of any XML element in configuration file");
2398       return FALSE;
2399     }
2400   else if (e->had_content)
2401     {
2402       _dbus_assert_not_reached ("Element had multiple content blocks");
2403       return FALSE;
2404     }
2405
2406   switch (top_element_type (parser))
2407     {
2408     case ELEMENT_NONE:
2409       _dbus_assert_not_reached ("element at top of stack has no type");
2410       return FALSE;
2411
2412     case ELEMENT_BUSCONFIG:
2413     case ELEMENT_POLICY:
2414     case ELEMENT_ALLOW:
2415     case ELEMENT_DENY:
2416     case ELEMENT_FORK:
2417     case ELEMENT_SYSLOG:
2418     case ELEMENT_KEEP_UMASK:
2419     case ELEMENT_STANDARD_SESSION_SERVICEDIRS:    
2420     case ELEMENT_STANDARD_SYSTEM_SERVICEDIRS:    
2421     case ELEMENT_ALLOW_ANONYMOUS:
2422     case ELEMENT_SELINUX:
2423     case ELEMENT_ASSOCIATE:
2424     case ELEMENT_APPARMOR:
2425       if (all_whitespace (content))
2426         return TRUE;
2427       else
2428         {
2429           dbus_set_error (error, DBUS_ERROR_FAILED,
2430                           "No text content expected inside XML element %s in configuration file",
2431                           bus_config_parser_element_type_to_name (top_element_type (parser)));
2432           return FALSE;
2433         }
2434
2435     case ELEMENT_PIDFILE:
2436       {
2437         char *s;
2438
2439         e->had_content = TRUE;
2440         
2441         if (!_dbus_string_copy_data (content, &s))
2442           goto nomem;
2443           
2444         dbus_free (parser->pidfile);
2445         parser->pidfile = s;
2446       }
2447       break;
2448
2449     case ELEMENT_INCLUDE:
2450       {
2451         DBusString full_path, selinux_policy_root;
2452
2453         e->had_content = TRUE;
2454
2455         if (e->d.include.if_selinux_enabled
2456             && !bus_selinux_enabled ())
2457           break;
2458
2459         if (!_dbus_string_init (&full_path))
2460           goto nomem;
2461
2462         if (e->d.include.selinux_root_relative)
2463           {
2464             if (!bus_selinux_get_policy_root ())
2465               {
2466                 dbus_set_error (error, DBUS_ERROR_FAILED,
2467                                 "Could not determine SELinux policy root for relative inclusion");
2468                 _dbus_string_free (&full_path);
2469                 return FALSE;
2470               }
2471             _dbus_string_init_const (&selinux_policy_root,
2472                                      bus_selinux_get_policy_root ());
2473             if (!make_full_path (&selinux_policy_root, content, &full_path))
2474               {
2475                 _dbus_string_free (&full_path);
2476                 goto nomem;
2477               }
2478           }
2479         else if (!make_full_path (&parser->basedir, content, &full_path))
2480           {
2481             _dbus_string_free (&full_path);
2482             goto nomem;
2483           }
2484
2485         if (!include_file (parser, &full_path,
2486                            e->d.include.ignore_missing, error))
2487           {
2488             _dbus_string_free (&full_path);
2489             return FALSE;
2490           }
2491
2492         _dbus_string_free (&full_path);
2493       }
2494       break;
2495
2496     case ELEMENT_SERVICEHELPER:
2497       {
2498         DBusString full_path;
2499         
2500         e->had_content = TRUE;
2501
2502         if (!_dbus_string_init (&full_path))
2503           goto nomem;
2504         
2505         if (!make_full_path (&parser->basedir, content, &full_path))
2506           {
2507             _dbus_string_free (&full_path);
2508             goto nomem;
2509           }
2510
2511         if (!servicehelper_path (parser, &full_path, error))
2512           {
2513             _dbus_string_free (&full_path);
2514             return FALSE;
2515           }
2516
2517         _dbus_string_free (&full_path);
2518       }
2519       break;
2520       
2521     case ELEMENT_INCLUDEDIR:
2522       {
2523         DBusString full_path;
2524         
2525         e->had_content = TRUE;
2526
2527         if (!_dbus_string_init (&full_path))
2528           goto nomem;
2529         
2530         if (!make_full_path (&parser->basedir, content, &full_path))
2531           {
2532             _dbus_string_free (&full_path);
2533             goto nomem;
2534           }
2535         
2536         if (!include_dir (parser, &full_path, error))
2537           {
2538             _dbus_string_free (&full_path);
2539             return FALSE;
2540           }
2541
2542         _dbus_string_free (&full_path);
2543       }
2544       break;
2545       
2546     case ELEMENT_USER:
2547       {
2548         char *s;
2549
2550         e->had_content = TRUE;
2551         
2552         if (!_dbus_string_copy_data (content, &s))
2553           goto nomem;
2554           
2555         dbus_free (parser->user);
2556         parser->user = s;
2557       }
2558       break;
2559
2560     case ELEMENT_CONFIGTYPE:
2561       {
2562         char *s;
2563
2564         e->had_content = TRUE;
2565
2566         if (!_dbus_string_copy_data (content, &s))
2567           goto nomem;
2568         
2569         dbus_free (parser->bus_type);
2570         parser->bus_type = s;
2571       }
2572       break;
2573       
2574     case ELEMENT_LISTEN:
2575       {
2576         char *s;
2577
2578         e->had_content = TRUE;
2579         
2580         if (!_dbus_string_copy_data (content, &s))
2581           goto nomem;
2582
2583         if (!_dbus_list_append (&parser->listen_on,
2584                                 s))
2585           {
2586             dbus_free (s);
2587             goto nomem;
2588           }
2589       }
2590       break;
2591
2592     case ELEMENT_AUTH:
2593       {
2594         char *s;
2595         
2596         e->had_content = TRUE;
2597
2598         if (!_dbus_string_copy_data (content, &s))
2599           goto nomem;
2600
2601         if (!_dbus_list_append (&parser->mechanisms,
2602                                 s))
2603           {
2604             dbus_free (s);
2605             goto nomem;
2606           }
2607       }
2608       break;
2609
2610     case ELEMENT_SERVICEDIR:
2611       {
2612         char *s;
2613         DBusString full_path;
2614         
2615         e->had_content = TRUE;
2616
2617         if (!_dbus_string_init (&full_path))
2618           goto nomem;
2619         
2620         if (!make_full_path (&parser->basedir, content, &full_path))
2621           {
2622             _dbus_string_free (&full_path);
2623             goto nomem;
2624           }
2625         
2626         if (!_dbus_string_copy_data (&full_path, &s))
2627           {
2628             _dbus_string_free (&full_path);
2629             goto nomem;
2630           }
2631
2632         /* _only_ extra session directories can be specified */
2633         if (!service_dirs_append_unique_or_free (&parser->service_dirs, s))
2634           {
2635             _dbus_string_free (&full_path);
2636             dbus_free (s);
2637             goto nomem;
2638           }
2639
2640         _dbus_string_free (&full_path);
2641       }
2642       break;
2643
2644     case ELEMENT_LIMIT:
2645       {
2646         long val;
2647
2648         e->had_content = TRUE;
2649
2650         val = 0;
2651         if (!_dbus_string_parse_int (content, 0, &val, NULL))
2652           {
2653             dbus_set_error (error, DBUS_ERROR_FAILED,
2654                             "<limit name=\"%s\"> element has invalid value (could not parse as integer)",
2655                             e->d.limit.name);
2656             return FALSE;
2657           }
2658
2659         e->d.limit.value = val;
2660
2661         _dbus_verbose ("Loaded value %ld for limit %s\n",
2662                        e->d.limit.value,
2663                        e->d.limit.name);
2664       }
2665       break;
2666     }
2667
2668   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2669   return TRUE;
2670
2671  nomem:
2672   BUS_SET_OOM (error);
2673   return FALSE;
2674 }
2675
2676 dbus_bool_t
2677 bus_config_parser_finished (BusConfigParser   *parser,
2678                             DBusError         *error)
2679 {
2680   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2681
2682   if (parser->stack != NULL)
2683     {
2684       dbus_set_error (error, DBUS_ERROR_FAILED,
2685                       "Element <%s> was not closed in configuration file",
2686                       bus_config_parser_element_type_to_name (top_element_type (parser)));
2687
2688       return FALSE;
2689     }
2690
2691   if (parser->is_toplevel && parser->listen_on == NULL)
2692     {
2693       dbus_set_error (error, DBUS_ERROR_FAILED,
2694                       "Configuration file needs one or more <listen> elements giving addresses"); 
2695       return FALSE;
2696     }
2697   
2698   return TRUE;
2699 }
2700
2701 const char*
2702 bus_config_parser_get_user (BusConfigParser *parser)
2703 {
2704   return parser->user;
2705 }
2706
2707 const char*
2708 bus_config_parser_get_type (BusConfigParser *parser)
2709 {
2710   return parser->bus_type;
2711 }
2712
2713 DBusList**
2714 bus_config_parser_get_addresses (BusConfigParser *parser)
2715 {
2716   return &parser->listen_on;
2717 }
2718
2719 DBusList**
2720 bus_config_parser_get_mechanisms (BusConfigParser *parser)
2721 {
2722   return &parser->mechanisms;
2723 }
2724
2725 DBusList**
2726 bus_config_parser_get_service_dirs (BusConfigParser *parser)
2727 {
2728   return &parser->service_dirs;
2729 }
2730
2731 DBusList**
2732 bus_config_parser_get_conf_dirs (BusConfigParser *parser)
2733 {
2734   return &parser->conf_dirs;
2735 }
2736
2737 dbus_bool_t
2738 bus_config_parser_get_fork (BusConfigParser   *parser)
2739 {
2740   return parser->fork;
2741 }
2742
2743 dbus_bool_t
2744 bus_config_parser_get_syslog (BusConfigParser   *parser)
2745 {
2746   return parser->syslog;
2747 }
2748
2749 dbus_bool_t
2750 bus_config_parser_get_keep_umask (BusConfigParser   *parser)
2751 {
2752   return parser->keep_umask;
2753 }
2754
2755 dbus_bool_t
2756 bus_config_parser_get_allow_anonymous (BusConfigParser   *parser)
2757 {
2758   return parser->allow_anonymous;
2759 }
2760
2761 const char *
2762 bus_config_parser_get_pidfile (BusConfigParser   *parser)
2763 {
2764   return parser->pidfile;
2765 }
2766
2767 const char *
2768 bus_config_parser_get_servicehelper (BusConfigParser   *parser)
2769 {
2770   return parser->servicehelper;
2771 }
2772
2773 BusPolicy*
2774 bus_config_parser_steal_policy (BusConfigParser *parser)
2775 {
2776   BusPolicy *policy;
2777
2778   _dbus_assert (parser->policy != NULL); /* can only steal the policy 1 time */
2779   
2780   policy = parser->policy;
2781
2782   parser->policy = NULL;
2783
2784   return policy;
2785 }
2786
2787 /* Overwrite any limits that were set in the configuration file */
2788 void
2789 bus_config_parser_get_limits (BusConfigParser *parser,
2790                               BusLimits       *limits)
2791 {
2792   *limits = parser->limits;
2793 }
2794
2795 DBusHashTable*
2796 bus_config_parser_steal_service_context_table (BusConfigParser *parser)
2797 {
2798   DBusHashTable *table;
2799
2800   _dbus_assert (parser->service_context_table != NULL); /* can only steal once */
2801
2802   table = parser->service_context_table;
2803
2804   parser->service_context_table = NULL;
2805
2806   return table;
2807 }
2808
2809 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
2810 #include <stdio.h>
2811
2812 typedef enum
2813 {
2814   VALID,
2815   INVALID,
2816   UNKNOWN
2817 } Validity;
2818
2819 static dbus_bool_t
2820 do_check_own_rules (BusPolicy  *policy)
2821 {
2822   const struct {
2823     char *name;
2824     dbus_bool_t allowed;
2825   } checks[] = {
2826     {"org.freedesktop", FALSE},
2827     {"org.freedesktop.ManySystem", FALSE},
2828     {"org.freedesktop.ManySystems", TRUE},
2829     {"org.freedesktop.ManySystems.foo", TRUE},
2830     {"org.freedesktop.ManySystems.foo.bar", TRUE},
2831     {"org.freedesktop.ManySystems2", FALSE},
2832     {"org.freedesktop.ManySystems2.foo", FALSE},
2833     {"org.freedesktop.ManySystems2.foo.bar", FALSE},
2834     {NULL, FALSE}
2835   };
2836   int i = 0;
2837
2838   while (checks[i].name)
2839     {
2840       DBusString service_name;
2841       dbus_bool_t ret;
2842
2843       if (!_dbus_string_init (&service_name))
2844         _dbus_assert_not_reached ("couldn't init string");
2845       if (!_dbus_string_append (&service_name, checks[i].name))
2846         _dbus_assert_not_reached ("couldn't append string");
2847
2848       ret = bus_policy_check_can_own (policy, &service_name);
2849       printf ("        Check name %s: %s\n", checks[i].name,
2850               ret ? "allowed" : "not allowed");
2851       if (checks[i].allowed && !ret)
2852         {
2853           _dbus_warn ("Cannot own %s\n", checks[i].name);
2854           return FALSE;
2855         }
2856       if (!checks[i].allowed && ret)
2857         {
2858           _dbus_warn ("Can own %s\n", checks[i].name);
2859           return FALSE;
2860         }
2861       _dbus_string_free (&service_name);
2862
2863       i++;
2864     }
2865
2866   return TRUE;
2867 }
2868
2869 static dbus_bool_t
2870 do_load (const DBusString *full_path,
2871          Validity          validity,
2872          dbus_bool_t       oom_possible,
2873          dbus_bool_t       check_own_rules)
2874 {
2875   BusConfigParser *parser;
2876   DBusError error;
2877
2878   dbus_error_init (&error);
2879
2880   parser = bus_config_load (full_path, TRUE, NULL, &error);
2881   if (parser == NULL)
2882     {
2883       _DBUS_ASSERT_ERROR_IS_SET (&error);
2884
2885       if (oom_possible &&
2886           dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2887         {
2888           _dbus_verbose ("Failed to load valid file due to OOM\n");
2889           dbus_error_free (&error);
2890           return TRUE;
2891         }
2892       else if (validity == VALID)
2893         {
2894           _dbus_warn ("Failed to load valid file but still had memory: %s\n",
2895                       error.message);
2896
2897           dbus_error_free (&error);
2898           return FALSE;
2899         }
2900       else
2901         {
2902           dbus_error_free (&error);
2903           return TRUE;
2904         }
2905     }
2906   else
2907     {
2908       _DBUS_ASSERT_ERROR_IS_CLEAR (&error);
2909
2910       if (check_own_rules && do_check_own_rules (parser->policy) == FALSE)
2911         {
2912           return FALSE;
2913         }
2914
2915       bus_config_parser_unref (parser);
2916
2917       if (validity == INVALID)
2918         {
2919           _dbus_warn ("Accepted invalid file\n");
2920           return FALSE;
2921         }
2922
2923       return TRUE;
2924     }
2925 }
2926
2927 typedef struct
2928 {
2929   const DBusString *full_path;
2930   Validity          validity;
2931   dbus_bool_t       check_own_rules;
2932 } LoaderOomData;
2933
2934 static dbus_bool_t
2935 check_loader_oom_func (void *data)
2936 {
2937   LoaderOomData *d = data;
2938
2939   return do_load (d->full_path, d->validity, TRUE, d->check_own_rules);
2940 }
2941
2942 static dbus_bool_t
2943 process_test_valid_subdir (const DBusString *test_base_dir,
2944                            const char       *subdir,
2945                            Validity          validity)
2946 {
2947   DBusString test_directory;
2948   DBusString filename;
2949   DBusDirIter *dir;
2950   dbus_bool_t retval;
2951   DBusError error;
2952
2953   retval = FALSE;
2954   dir = NULL;
2955
2956   if (!_dbus_string_init (&test_directory))
2957     _dbus_assert_not_reached ("didn't allocate test_directory\n");
2958
2959   _dbus_string_init_const (&filename, subdir);
2960
2961   if (!_dbus_string_copy (test_base_dir, 0,
2962                           &test_directory, 0))
2963     _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2964
2965   if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2966     _dbus_assert_not_reached ("couldn't allocate full path");
2967
2968   _dbus_string_free (&filename);
2969   if (!_dbus_string_init (&filename))
2970     _dbus_assert_not_reached ("didn't allocate filename string\n");
2971
2972   dbus_error_init (&error);
2973   dir = _dbus_directory_open (&test_directory, &error);
2974   if (dir == NULL)
2975     {
2976       _dbus_warn ("Could not open %s: %s\n",
2977                   _dbus_string_get_const_data (&test_directory),
2978                   error.message);
2979       dbus_error_free (&error);
2980       goto failed;
2981     }
2982
2983   if (validity == VALID)
2984     printf ("Testing valid files:\n");
2985   else if (validity == INVALID)
2986     printf ("Testing invalid files:\n");
2987   else
2988     printf ("Testing unknown files:\n");
2989
2990  next:
2991   while (_dbus_directory_get_next_file (dir, &filename, &error))
2992     {
2993       DBusString full_path;
2994       LoaderOomData d;
2995
2996       if (!_dbus_string_init (&full_path))
2997         _dbus_assert_not_reached ("couldn't init string");
2998
2999       if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
3000         _dbus_assert_not_reached ("couldn't copy dir to full_path");
3001
3002       if (!_dbus_concat_dir_and_file (&full_path, &filename))
3003         _dbus_assert_not_reached ("couldn't concat file to dir");
3004
3005       if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
3006         {
3007           _dbus_verbose ("Skipping non-.conf file %s\n",
3008                          _dbus_string_get_const_data (&filename));
3009           _dbus_string_free (&full_path);
3010           goto next;
3011         }
3012
3013       printf ("    %s\n", _dbus_string_get_const_data (&filename));
3014
3015       _dbus_verbose (" expecting %s\n",
3016                      validity == VALID ? "valid" :
3017                      (validity == INVALID ? "invalid" :
3018                       (validity == UNKNOWN ? "unknown" : "???")));
3019
3020       d.full_path = &full_path;
3021       d.validity = validity;
3022       d.check_own_rules = _dbus_string_ends_with_c_str (&full_path,
3023           "check-own-rules.conf");
3024
3025       /* FIXME hackaround for an expat problem, see
3026        * https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=124747
3027        * http://freedesktop.org/pipermail/dbus/2004-May/001153.html
3028        */
3029       /* if (!_dbus_test_oom_handling ("config-loader", check_loader_oom_func, &d)) */
3030       if (!check_loader_oom_func (&d))
3031         _dbus_assert_not_reached ("test failed");
3032       
3033       _dbus_string_free (&full_path);
3034     }
3035
3036   if (dbus_error_is_set (&error))
3037     {
3038       _dbus_warn ("Could not get next file in %s: %s\n",
3039                   _dbus_string_get_const_data (&test_directory),
3040                   error.message);
3041       dbus_error_free (&error);
3042       goto failed;
3043     }
3044
3045   retval = TRUE;
3046
3047  failed:
3048
3049   if (dir)
3050     _dbus_directory_close (dir);
3051   _dbus_string_free (&test_directory);
3052   _dbus_string_free (&filename);
3053
3054   return retval;
3055 }
3056
3057 static dbus_bool_t
3058 bools_equal (dbus_bool_t a,
3059              dbus_bool_t b)
3060 {
3061   return a ? b : !b;
3062 }
3063
3064 static dbus_bool_t
3065 strings_equal_or_both_null (const char *a,
3066                             const char *b)
3067 {
3068   if (a == NULL || b == NULL)
3069     return a == b;
3070   else
3071     return !strcmp (a, b);
3072 }
3073
3074 static dbus_bool_t
3075 elements_equal (const Element *a,
3076                 const Element *b)
3077 {
3078   if (a->type != b->type)
3079     return FALSE;
3080
3081   if (!bools_equal (a->had_content, b->had_content))
3082     return FALSE;
3083
3084   switch (a->type)
3085     {
3086
3087     case ELEMENT_INCLUDE:
3088       if (!bools_equal (a->d.include.ignore_missing,
3089                         b->d.include.ignore_missing))
3090         return FALSE;
3091       break;
3092
3093     case ELEMENT_POLICY:
3094       if (a->d.policy.type != b->d.policy.type)
3095         return FALSE;
3096       if (a->d.policy.gid_uid_or_at_console != b->d.policy.gid_uid_or_at_console)
3097         return FALSE;
3098       break;
3099
3100     case ELEMENT_LIMIT:
3101       if (strcmp (a->d.limit.name, b->d.limit.name))
3102         return FALSE;
3103       if (a->d.limit.value != b->d.limit.value)
3104         return FALSE;
3105       break;
3106
3107     default:
3108       /* do nothing */
3109       break;
3110     }
3111
3112   return TRUE;
3113
3114 }
3115
3116 static dbus_bool_t
3117 lists_of_elements_equal (DBusList *a,
3118                          DBusList *b)
3119 {
3120   DBusList *ia;
3121   DBusList *ib;
3122
3123   ia = a;
3124   ib = b;
3125   
3126   while (ia != NULL && ib != NULL)
3127     {
3128       if (elements_equal (ia->data, ib->data))
3129         return FALSE;
3130       ia = _dbus_list_get_next_link (&a, ia);
3131       ib = _dbus_list_get_next_link (&b, ib);
3132     }
3133
3134   return ia == NULL && ib == NULL;
3135 }
3136
3137 static dbus_bool_t
3138 lists_of_c_strings_equal (DBusList *a,
3139                           DBusList *b)
3140 {
3141   DBusList *ia;
3142   DBusList *ib;
3143
3144   ia = a;
3145   ib = b;
3146   
3147   while (ia != NULL && ib != NULL)
3148     {
3149       if (strcmp (ia->data, ib->data))
3150         return FALSE;
3151       ia = _dbus_list_get_next_link (&a, ia);
3152       ib = _dbus_list_get_next_link (&b, ib);
3153     }
3154
3155   return ia == NULL && ib == NULL;
3156 }
3157
3158 static dbus_bool_t
3159 limits_equal (const BusLimits *a,
3160               const BusLimits *b)
3161 {
3162   return
3163     (a->max_incoming_bytes == b->max_incoming_bytes
3164      || a->max_incoming_unix_fds == b->max_incoming_unix_fds
3165      || a->max_outgoing_bytes == b->max_outgoing_bytes
3166      || a->max_outgoing_unix_fds == b->max_outgoing_unix_fds
3167      || a->max_message_size == b->max_message_size
3168      || a->max_message_unix_fds == b->max_message_unix_fds
3169      || a->activation_timeout == b->activation_timeout
3170      || a->auth_timeout == b->auth_timeout
3171      || a->pending_fd_timeout == b->pending_fd_timeout
3172      || a->max_completed_connections == b->max_completed_connections
3173      || a->max_incomplete_connections == b->max_incomplete_connections
3174      || a->max_connections_per_user == b->max_connections_per_user
3175      || a->max_pending_activations == b->max_pending_activations
3176      || a->max_services_per_connection == b->max_services_per_connection
3177      || a->max_match_rules_per_connection == b->max_match_rules_per_connection
3178      || a->max_replies_per_connection == b->max_replies_per_connection
3179      || a->reply_timeout == b->reply_timeout);
3180 }
3181
3182 static dbus_bool_t
3183 config_parsers_equal (const BusConfigParser *a,
3184                       const BusConfigParser *b)
3185 {
3186   if (!_dbus_string_equal (&a->basedir, &b->basedir))
3187     return FALSE;
3188
3189   if (!lists_of_elements_equal (a->stack, b->stack))
3190     return FALSE;
3191
3192   if (!strings_equal_or_both_null (a->user, b->user))
3193     return FALSE;
3194
3195   if (!lists_of_c_strings_equal (a->listen_on, b->listen_on))
3196     return FALSE;
3197
3198   if (!lists_of_c_strings_equal (a->mechanisms, b->mechanisms))
3199     return FALSE;
3200
3201   if (!lists_of_c_strings_equal (a->service_dirs, b->service_dirs))
3202     return FALSE;
3203   
3204   /* FIXME: compare policy */
3205
3206   /* FIXME: compare service selinux ID table */
3207
3208   if (! limits_equal (&a->limits, &b->limits))
3209     return FALSE;
3210
3211   if (!strings_equal_or_both_null (a->pidfile, b->pidfile))
3212     return FALSE;
3213
3214   if (! bools_equal (a->fork, b->fork))
3215     return FALSE;
3216
3217   if (! bools_equal (a->keep_umask, b->keep_umask))
3218     return FALSE;
3219
3220   if (! bools_equal (a->is_toplevel, b->is_toplevel))
3221     return FALSE;
3222
3223   return TRUE;
3224 }
3225
3226 static dbus_bool_t
3227 all_are_equiv (const DBusString *target_directory)
3228 {
3229   DBusString filename;
3230   DBusDirIter *dir;
3231   BusConfigParser *first_parser;
3232   BusConfigParser *parser;
3233   DBusError error;
3234   dbus_bool_t equal;
3235   dbus_bool_t retval;
3236
3237   dir = NULL;
3238   first_parser = NULL;
3239   parser = NULL;
3240   retval = FALSE;
3241
3242   if (!_dbus_string_init (&filename))
3243     _dbus_assert_not_reached ("didn't allocate filename string");
3244
3245   dbus_error_init (&error);
3246   dir = _dbus_directory_open (target_directory, &error);
3247   if (dir == NULL)
3248     {
3249       _dbus_warn ("Could not open %s: %s\n",
3250                   _dbus_string_get_const_data (target_directory),
3251                   error.message);
3252       dbus_error_free (&error);
3253       goto finished;
3254     }
3255
3256   printf ("Comparing equivalent files:\n");
3257
3258  next:
3259   while (_dbus_directory_get_next_file (dir, &filename, &error))
3260     {
3261       DBusString full_path;
3262
3263       if (!_dbus_string_init (&full_path))
3264         _dbus_assert_not_reached ("couldn't init string");
3265
3266       if (!_dbus_string_copy (target_directory, 0, &full_path, 0))
3267         _dbus_assert_not_reached ("couldn't copy dir to full_path");
3268
3269       if (!_dbus_concat_dir_and_file (&full_path, &filename))
3270         _dbus_assert_not_reached ("couldn't concat file to dir");
3271
3272       if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
3273         {
3274           _dbus_verbose ("Skipping non-.conf file %s\n",
3275                          _dbus_string_get_const_data (&filename));
3276           _dbus_string_free (&full_path);
3277           goto next;
3278         }
3279
3280       printf ("    %s\n", _dbus_string_get_const_data (&filename));
3281
3282       parser = bus_config_load (&full_path, TRUE, NULL, &error);
3283
3284       if (parser == NULL)
3285         {
3286           _dbus_warn ("Could not load file %s: %s\n",
3287                       _dbus_string_get_const_data (&full_path),
3288                       error.message);
3289           _dbus_string_free (&full_path);
3290           dbus_error_free (&error);
3291           goto finished;
3292         }
3293       else if (first_parser == NULL)
3294         {
3295           _dbus_string_free (&full_path);
3296           first_parser = parser;
3297         }
3298       else
3299         {
3300           _dbus_string_free (&full_path);
3301           equal = config_parsers_equal (first_parser, parser);
3302           bus_config_parser_unref (parser);
3303           if (! equal)
3304             goto finished;
3305         }
3306     }
3307
3308   retval = TRUE;
3309
3310  finished:
3311   _dbus_string_free (&filename);
3312   if (first_parser)
3313     bus_config_parser_unref (first_parser);
3314   if (dir)
3315     _dbus_directory_close (dir);
3316
3317   return retval;
3318   
3319 }
3320
3321 static dbus_bool_t
3322 process_test_equiv_subdir (const DBusString *test_base_dir,
3323                            const char       *subdir)
3324 {
3325   DBusString test_directory;
3326   DBusString filename;
3327   DBusDirIter *dir;
3328   DBusError error;
3329   dbus_bool_t equal;
3330   dbus_bool_t retval;
3331
3332   dir = NULL;
3333   retval = FALSE;
3334
3335   if (!_dbus_string_init (&test_directory))
3336     _dbus_assert_not_reached ("didn't allocate test_directory");
3337
3338   _dbus_string_init_const (&filename, subdir);
3339
3340   if (!_dbus_string_copy (test_base_dir, 0,
3341                           &test_directory, 0))
3342     _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
3343
3344   if (!_dbus_concat_dir_and_file (&test_directory, &filename))
3345     _dbus_assert_not_reached ("couldn't allocate full path");
3346
3347   _dbus_string_free (&filename);
3348   if (!_dbus_string_init (&filename))
3349     _dbus_assert_not_reached ("didn't allocate filename string");
3350
3351   dbus_error_init (&error);
3352   dir = _dbus_directory_open (&test_directory, &error);
3353   if (dir == NULL)
3354     {
3355       _dbus_warn ("Could not open %s: %s\n",
3356                   _dbus_string_get_const_data (&test_directory),
3357                   error.message);
3358       dbus_error_free (&error);
3359       goto finished;
3360     }
3361
3362   while (_dbus_directory_get_next_file (dir, &filename, &error))
3363     {
3364       DBusString full_path;
3365
3366       /* Skip CVS's magic directories! */
3367       if (_dbus_string_equal_c_str (&filename, "CVS"))
3368         continue;
3369
3370       if (!_dbus_string_init (&full_path))
3371         _dbus_assert_not_reached ("couldn't init string");
3372
3373       if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
3374         _dbus_assert_not_reached ("couldn't copy dir to full_path");
3375
3376       if (!_dbus_concat_dir_and_file (&full_path, &filename))
3377         _dbus_assert_not_reached ("couldn't concat file to dir");
3378       
3379       equal = all_are_equiv (&full_path);
3380       _dbus_string_free (&full_path);
3381
3382       if (!equal)
3383         goto finished;
3384     }
3385
3386   retval = TRUE;
3387
3388  finished:
3389   _dbus_string_free (&test_directory);
3390   _dbus_string_free (&filename);
3391   if (dir)
3392     _dbus_directory_close (dir);
3393
3394   return retval;
3395   
3396 }
3397
3398 static const char *test_session_service_dir_matches[] = 
3399         {
3400 #ifdef DBUS_UNIX
3401          "/testhome/foo/.testlocal/testshare/dbus-1/services",
3402          "/testusr/testlocal/testshare/dbus-1/services",
3403          "/testusr/testshare/dbus-1/services",
3404          DBUS_DATADIR"/dbus-1/services",
3405 #endif
3406 /* will be filled in test_default_session_servicedirs() */
3407 #ifdef DBUS_WIN
3408          NULL,
3409          NULL,
3410 #endif
3411          NULL
3412         };
3413
3414 static dbus_bool_t
3415 test_default_session_servicedirs (void)
3416 {
3417   DBusList *dirs;
3418   DBusList *link;
3419   DBusString progs;
3420   DBusString install_root_based;
3421   int i;
3422   dbus_bool_t ret = FALSE;
3423 #ifdef DBUS_WIN
3424   const char *tmp;
3425   const char *common_progs;
3426 #endif
3427
3428   /* On Unix we don't actually use these, but it's easier to handle the
3429    * deallocation if we always allocate them, whether needed or not */
3430   if (!_dbus_string_init (&progs) ||
3431       !_dbus_string_init (&install_root_based))
3432     _dbus_assert_not_reached ("OOM allocating strings");
3433
3434 #ifdef DBUS_WIN
3435   if (!_dbus_string_append (&install_root_based, DBUS_DATADIR) ||
3436       !_dbus_string_append (&install_root_based, "/dbus-1/services"))
3437     goto out;
3438
3439   tmp = _dbus_replace_install_prefix (
3440       _dbus_string_get_const_data (&install_root_based));
3441
3442   if (tmp == NULL ||
3443       !_dbus_string_set_length (&install_root_based, 0) ||
3444       !_dbus_string_append (&install_root_based, tmp))
3445     goto out;
3446
3447   test_session_service_dir_matches[0] = _dbus_string_get_const_data (
3448       &install_root_based);
3449
3450   common_progs = _dbus_getenv ("CommonProgramFiles");
3451
3452   if (common_progs) 
3453     {
3454       if (!_dbus_string_append (&progs, common_progs)) 
3455         goto out;
3456
3457       if (!_dbus_string_append (&progs, "/dbus-1/services")) 
3458         goto out;
3459
3460       test_session_service_dir_matches[1] = _dbus_string_get_const_data(&progs);
3461     }
3462 #endif
3463   dirs = NULL;
3464
3465   printf ("Testing retrieving the default session service directories\n");
3466   if (!_dbus_get_standard_session_servicedirs (&dirs))
3467     _dbus_assert_not_reached ("couldn't get stardard dirs");
3468
3469   /* make sure our defaults end with share/dbus-1/service */
3470   while ((link = _dbus_list_pop_first_link (&dirs)))
3471     {
3472       DBusString path;
3473       
3474       printf ("    default service dir: %s\n", (char *)link->data);
3475       _dbus_string_init_const (&path, (char *)link->data);
3476       if (!_dbus_string_ends_with_c_str (&path, "dbus-1/services"))
3477         {
3478           printf ("error with default session service directories\n");
3479               dbus_free (link->data);
3480           _dbus_list_free_link (link);
3481           goto out;
3482         }
3483  
3484       dbus_free (link->data);
3485       _dbus_list_free_link (link);
3486     }
3487
3488 #ifdef DBUS_UNIX
3489   if (!dbus_setenv ("XDG_DATA_HOME", "/testhome/foo/.testlocal/testshare"))
3490     _dbus_assert_not_reached ("couldn't setenv XDG_DATA_HOME");
3491
3492   if (!dbus_setenv ("XDG_DATA_DIRS", ":/testusr/testlocal/testshare: :/testusr/testshare:"))
3493     _dbus_assert_not_reached ("couldn't setenv XDG_DATA_DIRS");
3494 #endif
3495   if (!_dbus_get_standard_session_servicedirs (&dirs))
3496     _dbus_assert_not_reached ("couldn't get stardard dirs");
3497
3498   /* make sure we read and parse the env variable correctly */
3499   i = 0;
3500   while ((link = _dbus_list_pop_first_link (&dirs)))
3501     {
3502       printf ("    test service dir: %s\n", (char *)link->data);
3503       if (test_session_service_dir_matches[i] == NULL)
3504         {
3505           printf ("more directories parsed than in match set\n");
3506           dbus_free (link->data);
3507           _dbus_list_free_link (link);
3508           goto out;
3509         }
3510  
3511       if (strcmp (test_session_service_dir_matches[i], 
3512                   (char *)link->data) != 0)
3513         {
3514           printf ("%s directory does not match %s in the match set\n", 
3515                   (char *)link->data,
3516                   test_session_service_dir_matches[i]);
3517           dbus_free (link->data);
3518           _dbus_list_free_link (link);
3519           goto out;
3520         }
3521
3522       ++i;
3523
3524       dbus_free (link->data);
3525       _dbus_list_free_link (link);
3526     }
3527   
3528   if (test_session_service_dir_matches[i] != NULL)
3529     {
3530       printf ("extra data %s in the match set was not matched\n",
3531               test_session_service_dir_matches[i]);
3532       goto out;
3533     }
3534
3535   ret = TRUE;
3536
3537 out:
3538   _dbus_string_free (&install_root_based);
3539   _dbus_string_free (&progs);
3540   return ret;
3541 }
3542
3543 static const char *test_system_service_dir_matches[] = 
3544         {
3545 #ifdef DBUS_UNIX
3546          "/usr/local/share/dbus-1/system-services",
3547          "/usr/share/dbus-1/system-services",
3548 #endif
3549          DBUS_DATADIR"/dbus-1/system-services",
3550 #ifdef DBUS_UNIX
3551          "/lib/dbus-1/system-services",
3552 #endif
3553
3554 #ifdef DBUS_WIN
3555          NULL,
3556 #endif
3557          NULL
3558         };
3559
3560 static dbus_bool_t
3561 test_default_system_servicedirs (void)
3562 {
3563   DBusList *dirs;
3564   DBusList *link;
3565   DBusString progs;
3566 #ifndef DBUS_UNIX
3567   const char *common_progs;
3568 #endif
3569   int i;
3570
3571   /* On Unix we don't actually use this variable, but it's easier to handle the
3572    * deallocation if we always allocate it, whether needed or not */
3573   if (!_dbus_string_init (&progs))
3574     _dbus_assert_not_reached ("OOM allocating progs");
3575
3576 #ifndef DBUS_UNIX
3577   common_progs = _dbus_getenv ("CommonProgramFiles");
3578
3579   if (common_progs) 
3580     {
3581       if (!_dbus_string_append (&progs, common_progs)) 
3582         {
3583           _dbus_string_free (&progs);
3584           return FALSE;
3585         }
3586
3587       if (!_dbus_string_append (&progs, "/dbus-1/system-services")) 
3588         {
3589           _dbus_string_free (&progs);
3590           return FALSE;
3591         }
3592       test_system_service_dir_matches[1] = _dbus_string_get_const_data(&progs);
3593     }
3594 #endif
3595   dirs = NULL;
3596
3597   printf ("Testing retrieving the default system service directories\n");
3598   if (!_dbus_get_standard_system_servicedirs (&dirs))
3599     _dbus_assert_not_reached ("couldn't get stardard dirs");
3600
3601   /* make sure our defaults end with share/dbus-1/system-service */
3602   while ((link = _dbus_list_pop_first_link (&dirs)))
3603     {
3604       DBusString path;
3605       
3606       printf ("    default service dir: %s\n", (char *)link->data);
3607       _dbus_string_init_const (&path, (char *)link->data);
3608       if (!_dbus_string_ends_with_c_str (&path, "dbus-1/system-services"))
3609         {
3610           printf ("error with default system service directories\n");
3611               dbus_free (link->data);
3612           _dbus_list_free_link (link);
3613           _dbus_string_free (&progs);
3614           return FALSE;
3615         }
3616  
3617       dbus_free (link->data);
3618       _dbus_list_free_link (link);
3619     }
3620
3621 #ifdef DBUS_UNIX
3622   if (!dbus_setenv ("XDG_DATA_HOME", "/testhome/foo/.testlocal/testshare"))
3623     _dbus_assert_not_reached ("couldn't setenv XDG_DATA_HOME");
3624
3625   if (!dbus_setenv ("XDG_DATA_DIRS", ":/testusr/testlocal/testshare: :/testusr/testshare:"))
3626     _dbus_assert_not_reached ("couldn't setenv XDG_DATA_DIRS");
3627 #endif
3628   if (!_dbus_get_standard_system_servicedirs (&dirs))
3629     _dbus_assert_not_reached ("couldn't get stardard dirs");
3630
3631   /* make sure we read and parse the env variable correctly */
3632   i = 0;
3633   while ((link = _dbus_list_pop_first_link (&dirs)))
3634     {
3635       printf ("    test service dir: %s\n", (char *)link->data);
3636       if (test_system_service_dir_matches[i] == NULL)
3637         {
3638           printf ("more directories parsed than in match set\n");
3639           dbus_free (link->data);
3640           _dbus_list_free_link (link);
3641           _dbus_string_free (&progs);
3642           return FALSE;
3643         }
3644  
3645       if (strcmp (test_system_service_dir_matches[i], 
3646                   (char *)link->data) != 0)
3647         {
3648           printf ("%s directory does not match %s in the match set\n", 
3649                   (char *)link->data,
3650                   test_system_service_dir_matches[i]);
3651           dbus_free (link->data);
3652           _dbus_list_free_link (link);
3653           _dbus_string_free (&progs);
3654           return FALSE;
3655         }
3656
3657       ++i;
3658
3659       dbus_free (link->data);
3660       _dbus_list_free_link (link);
3661     }
3662   
3663   if (test_system_service_dir_matches[i] != NULL)
3664     {
3665       printf ("extra data %s in the match set was not matched\n",
3666               test_system_service_dir_matches[i]);
3667
3668       _dbus_string_free (&progs);
3669       return FALSE;
3670     }
3671     
3672   _dbus_string_free (&progs);
3673   return TRUE;
3674 }
3675                    
3676 dbus_bool_t
3677 bus_config_parser_test (const DBusString *test_data_dir)
3678 {
3679   if (test_data_dir == NULL ||
3680       _dbus_string_get_length (test_data_dir) == 0)
3681     {
3682       printf ("No test data\n");
3683       return TRUE;
3684     }
3685
3686   if (!test_default_session_servicedirs())
3687     return FALSE;
3688
3689 #ifdef DBUS_WIN
3690   printf("default system service dir skipped\n");
3691 #else
3692   if (!test_default_system_servicedirs())
3693     return FALSE;
3694 #endif
3695
3696   if (!process_test_valid_subdir (test_data_dir, "valid-config-files", VALID))
3697     return FALSE;
3698
3699 #ifndef DBUS_WIN
3700   if (!process_test_valid_subdir (test_data_dir, "valid-config-files-system", VALID))
3701     return FALSE;
3702 #endif
3703
3704   if (!process_test_valid_subdir (test_data_dir, "invalid-config-files", INVALID))
3705     return FALSE;
3706
3707   if (!process_test_equiv_subdir (test_data_dir, "equiv-config-files"))
3708     return FALSE;
3709
3710   return TRUE;
3711 }
3712
3713 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */
3714