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