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