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