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