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