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