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