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