2003-12-02 Richard Hult <richard@imendio.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 Red Hat, Inc.
5  *
6  * Licensed under the Academic Free License version 2.0
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 <dbus/dbus-list.h>
28 #include <dbus/dbus-internals.h>
29 #include <string.h>
30
31 typedef enum
32 {
33   ELEMENT_NONE,
34   ELEMENT_BUSCONFIG,
35   ELEMENT_INCLUDE,
36   ELEMENT_USER,
37   ELEMENT_LISTEN,
38   ELEMENT_AUTH,
39   ELEMENT_POLICY,
40   ELEMENT_LIMIT,
41   ELEMENT_ALLOW,
42   ELEMENT_DENY,
43   ELEMENT_FORK,
44   ELEMENT_PIDFILE,
45   ELEMENT_SERVICEDIR,
46   ELEMENT_INCLUDEDIR,
47   ELEMENT_TYPE
48 } ElementType;
49
50 typedef enum
51 {
52   /* we ignore policies for unknown groups/users */
53   POLICY_IGNORED,
54
55   /* non-ignored */
56   POLICY_DEFAULT,
57   POLICY_MANDATORY,
58   POLICY_USER,
59   POLICY_GROUP
60 } PolicyType;
61
62 typedef struct
63 {
64   ElementType type;
65
66   unsigned int had_content : 1;
67
68   union
69   {
70     struct
71     {
72       unsigned int ignore_missing : 1;
73     } include;
74
75     struct
76     {
77       PolicyType type;
78       unsigned long gid_or_uid;      
79     } policy;
80
81     struct
82     {
83       char *name;
84       long value;
85     } limit;
86     
87   } d;
88
89 } Element;
90
91 /**
92  * Parser for bus configuration file. 
93  */
94 struct BusConfigParser
95 {
96   int refcount;        /**< Reference count */
97
98   DBusString basedir;  /**< Directory we resolve paths relative to */
99   
100   DBusList *stack;     /**< stack of Element */
101
102   char *user;          /**< user to run as */
103
104   char *bus_type;          /**< Message bus type */
105   
106   DBusList *listen_on; /**< List of addresses to listen to */
107
108   DBusList *mechanisms; /**< Auth mechanisms */
109
110   DBusList *service_dirs; /**< Directories to look for services in */
111
112   BusPolicy *policy;     /**< Security policy */
113
114   BusLimits limits;      /**< Limits */
115
116   char *pidfile;         /**< PID file */
117
118   unsigned int fork : 1; /**< TRUE to fork into daemon mode */
119
120   unsigned int is_toplevel : 1; /**< FALSE if we are a sub-config-file inside another one */
121 };
122
123 static const char*
124 element_type_to_name (ElementType type)
125 {
126   switch (type)
127     {
128     case ELEMENT_NONE:
129       return NULL;
130     case ELEMENT_BUSCONFIG:
131       return "busconfig";
132     case ELEMENT_INCLUDE:
133       return "include";
134     case ELEMENT_USER:
135       return "user";
136     case ELEMENT_LISTEN:
137       return "listen";
138     case ELEMENT_AUTH:
139       return "auth";
140     case ELEMENT_POLICY:
141       return "policy";
142     case ELEMENT_LIMIT:
143       return "limit";
144     case ELEMENT_ALLOW:
145       return "allow";
146     case ELEMENT_DENY:
147       return "deny";
148     case ELEMENT_FORK:
149       return "fork";
150     case ELEMENT_PIDFILE:
151       return "pidfile";
152     case ELEMENT_SERVICEDIR:
153       return "servicedir";
154     case ELEMENT_INCLUDEDIR:
155       return "includedir";
156     case ELEMENT_TYPE:
157       return "type";
158     }
159
160   _dbus_assert_not_reached ("bad element type");
161
162   return NULL;
163 }
164
165 static Element*
166 push_element (BusConfigParser *parser,
167               ElementType      type)
168 {
169   Element *e;
170
171   _dbus_assert (type != ELEMENT_NONE);
172   
173   e = dbus_new0 (Element, 1);
174   if (e == NULL)
175     return NULL;
176
177   if (!_dbus_list_append (&parser->stack, e))
178     {
179       dbus_free (e);
180       return NULL;
181     }
182   
183   e->type = type;
184
185   return e;
186 }
187
188 static void
189 element_free (Element *e)
190 {
191   if (e->type == ELEMENT_LIMIT)
192     dbus_free (e->d.limit.name);
193   
194   dbus_free (e);
195 }
196
197 static void
198 pop_element (BusConfigParser *parser)
199 {
200   Element *e;
201
202   e = _dbus_list_pop_last (&parser->stack);
203   
204   element_free (e);
205 }
206
207 static Element*
208 peek_element (BusConfigParser *parser)
209 {
210   Element *e;
211
212   e = _dbus_list_get_last (&parser->stack);
213
214   return e;
215 }
216
217 static ElementType
218 top_element_type (BusConfigParser *parser)
219 {
220   Element *e;
221
222   e = _dbus_list_get_last (&parser->stack);
223
224   if (e)
225     return e->type;
226   else
227     return ELEMENT_NONE;
228 }
229
230 static dbus_bool_t
231 merge_included (BusConfigParser *parser,
232                 BusConfigParser *included,
233                 DBusError       *error)
234 {
235   DBusList *link;
236
237   if (!bus_policy_merge (parser->policy,
238                          included->policy))
239     {
240       BUS_SET_OOM (error);
241       return FALSE;
242     }
243   
244   if (included->user != NULL)
245     {
246       dbus_free (parser->user);
247       parser->user = included->user;
248       included->user = NULL;
249     }
250
251   if (included->bus_type != NULL)
252     {
253       dbus_free (parser->bus_type);
254       parser->bus_type = included->bus_type;
255       included->bus_type = NULL;
256     }
257   
258   if (included->fork)
259     parser->fork = TRUE;
260
261   if (included->pidfile != NULL)
262     {
263       dbus_free (parser->pidfile);
264       parser->pidfile = included->pidfile;
265       included->pidfile = NULL;
266     }
267   
268   while ((link = _dbus_list_pop_first_link (&included->listen_on)))
269     _dbus_list_append_link (&parser->listen_on, link);
270
271   while ((link = _dbus_list_pop_first_link (&included->mechanisms)))
272     _dbus_list_append_link (&parser->mechanisms, link);
273
274   while ((link = _dbus_list_pop_first_link (&included->service_dirs)))
275     _dbus_list_append_link (&parser->service_dirs, link);
276   
277   return TRUE;
278 }
279
280 BusConfigParser*
281 bus_config_parser_new (const DBusString *basedir,
282                        dbus_bool_t       is_toplevel)
283 {
284   BusConfigParser *parser;
285
286   parser = dbus_new0 (BusConfigParser, 1);
287   if (parser == NULL)
288     return NULL;
289
290   parser->is_toplevel = !!is_toplevel;
291   
292   if (!_dbus_string_init (&parser->basedir))
293     {
294       dbus_free (parser);
295       return NULL;
296     }
297
298   if (((parser->policy = bus_policy_new ()) == NULL) ||
299       !_dbus_string_copy (basedir, 0, &parser->basedir, 0))
300     {
301       if (parser->policy)
302         bus_policy_unref (parser->policy);
303       
304       _dbus_string_free (&parser->basedir);
305       dbus_free (parser);
306       return NULL;
307     }
308
309   /* Make up some numbers! woot! */
310   parser->limits.max_incoming_bytes = _DBUS_ONE_MEGABYTE * 63;
311   parser->limits.max_outgoing_bytes = _DBUS_ONE_MEGABYTE * 63;
312   parser->limits.max_message_size = _DBUS_ONE_MEGABYTE * 32;
313
314   /* Making this long means the user has to wait longer for an error
315    * message if something screws up, but making it too short means
316    * they might see a false failure.
317    */
318   parser->limits.activation_timeout = 25000; /* 25 seconds */
319
320   /* Making this long risks making a DOS attack easier, but too short
321    * and legitimate auth will fail.  If interactive auth (ask user for
322    * password) is allowed, then potentially it has to be quite long.
323    */
324   parser->limits.auth_timeout = 30000; /* 30 seconds */
325
326   parser->limits.max_incomplete_connections = 32;
327   parser->limits.max_connections_per_user = 128;
328
329   /* Note that max_completed_connections / max_connections_per_user
330    * is the number of users that would have to work together to
331    * DOS all the other users.
332    */
333   parser->limits.max_completed_connections = 1024;
334
335   parser->limits.max_pending_activations = 256;
336   parser->limits.max_services_per_connection = 256;
337
338   parser->limits.max_match_rules_per_connection = 128;
339
340   parser->limits.reply_timeout = 5 * 60 * 1000; /* 5 minutes */
341   parser->limits.max_replies_per_connection = 32;
342   
343   parser->refcount = 1;
344
345   return parser;
346 }
347
348 BusConfigParser *
349 bus_config_parser_ref (BusConfigParser *parser)
350 {
351   _dbus_assert (parser->refcount > 0);
352
353   parser->refcount += 1;
354
355   return parser;
356 }
357
358 void
359 bus_config_parser_unref (BusConfigParser *parser)
360 {
361   _dbus_assert (parser->refcount > 0);
362
363   parser->refcount -= 1;
364
365   if (parser->refcount == 0)
366     {
367       while (parser->stack != NULL)
368         pop_element (parser);
369
370       dbus_free (parser->user);
371       dbus_free (parser->bus_type);
372       dbus_free (parser->pidfile);
373       
374       _dbus_list_foreach (&parser->listen_on,
375                           (DBusForeachFunction) dbus_free,
376                           NULL);
377
378       _dbus_list_clear (&parser->listen_on);
379
380       _dbus_list_foreach (&parser->service_dirs,
381                           (DBusForeachFunction) dbus_free,
382                           NULL);
383
384       _dbus_list_clear (&parser->service_dirs);
385
386       _dbus_list_foreach (&parser->mechanisms,
387                           (DBusForeachFunction) dbus_free,
388                           NULL);
389
390       _dbus_list_clear (&parser->mechanisms);
391       
392       _dbus_string_free (&parser->basedir);
393
394       if (parser->policy)
395         bus_policy_unref (parser->policy);
396       
397       dbus_free (parser);
398     }
399 }
400
401 dbus_bool_t
402 bus_config_parser_check_doctype (BusConfigParser   *parser,
403                                  const char        *doctype,
404                                  DBusError         *error)
405 {
406   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
407
408   if (strcmp (doctype, "busconfig") != 0)
409     {
410       dbus_set_error (error,
411                       DBUS_ERROR_FAILED,
412                       "Configuration file has the wrong document type %s",
413                       doctype);
414       return FALSE;
415     }
416   else
417     return TRUE;
418 }
419
420 typedef struct
421 {
422   const char  *name;
423   const char **retloc;
424 } LocateAttr;
425
426 static dbus_bool_t
427 locate_attributes (BusConfigParser  *parser,
428                    const char       *element_name,
429                    const char      **attribute_names,
430                    const char      **attribute_values,
431                    DBusError        *error,
432                    const char       *first_attribute_name,
433                    const char      **first_attribute_retloc,
434                    ...)
435 {
436   va_list args;
437   const char *name;
438   const char **retloc;
439   int n_attrs;
440 #define MAX_ATTRS 24
441   LocateAttr attrs[MAX_ATTRS];
442   dbus_bool_t retval;
443   int i;
444
445   _dbus_assert (first_attribute_name != NULL);
446   _dbus_assert (first_attribute_retloc != NULL);
447
448   retval = TRUE;
449
450   n_attrs = 1;
451   attrs[0].name = first_attribute_name;
452   attrs[0].retloc = first_attribute_retloc;
453   *first_attribute_retloc = NULL;
454
455   va_start (args, first_attribute_retloc);
456
457   name = va_arg (args, const char*);
458   retloc = va_arg (args, const char**);
459
460   while (name != NULL)
461     {
462       _dbus_assert (retloc != NULL);
463       _dbus_assert (n_attrs < MAX_ATTRS);
464
465       attrs[n_attrs].name = name;
466       attrs[n_attrs].retloc = retloc;
467       n_attrs += 1;
468       *retloc = NULL;
469
470       name = va_arg (args, const char*);
471       retloc = va_arg (args, const char**);
472     }
473
474   va_end (args);
475
476   if (!retval)
477     return retval;
478
479   i = 0;
480   while (attribute_names[i])
481     {
482       int j;
483       dbus_bool_t found;
484       
485       found = FALSE;
486       j = 0;
487       while (j < n_attrs)
488         {
489           if (strcmp (attrs[j].name, attribute_names[i]) == 0)
490             {
491               retloc = attrs[j].retloc;
492
493               if (*retloc != NULL)
494                 {
495                   dbus_set_error (error, DBUS_ERROR_FAILED,
496                                   "Attribute \"%s\" repeated twice on the same <%s> element",
497                                   attrs[j].name, element_name);
498                   retval = FALSE;
499                   goto out;
500                 }
501
502               *retloc = attribute_values[i];
503               found = TRUE;
504             }
505
506           ++j;
507         }
508
509       if (!found)
510         {
511           dbus_set_error (error, DBUS_ERROR_FAILED,
512                           "Attribute \"%s\" is invalid on <%s> element in this context",
513                           attribute_names[i], element_name);
514           retval = FALSE;
515           goto out;
516         }
517
518       ++i;
519     }
520
521  out:
522   return retval;
523 }
524
525 static dbus_bool_t
526 check_no_attributes (BusConfigParser  *parser,
527                      const char       *element_name,
528                      const char      **attribute_names,
529                      const char      **attribute_values,
530                      DBusError        *error)
531 {
532   if (attribute_names[0] != NULL)
533     {
534       dbus_set_error (error, DBUS_ERROR_FAILED,
535                       "Attribute \"%s\" is invalid on <%s> element in this context",
536                       attribute_names[0], element_name);
537       return FALSE;
538     }
539
540   return TRUE;
541 }
542
543 static dbus_bool_t
544 start_busconfig_child (BusConfigParser   *parser,
545                        const char        *element_name,
546                        const char       **attribute_names,
547                        const char       **attribute_values,
548                        DBusError         *error)
549 {
550   if (strcmp (element_name, "user") == 0)
551     {
552       if (!check_no_attributes (parser, "user", attribute_names, attribute_values, error))
553         return FALSE;
554
555       if (push_element (parser, ELEMENT_USER) == NULL)
556         {
557           BUS_SET_OOM (error);
558           return FALSE;
559         }
560
561       return TRUE;
562     }
563   else if (strcmp (element_name, "type") == 0)
564     {
565       if (!check_no_attributes (parser, "type", attribute_names, attribute_values, error))
566         return FALSE;
567
568       if (push_element (parser, ELEMENT_TYPE) == NULL)
569         {
570           BUS_SET_OOM (error);
571           return FALSE;
572         }
573
574       return TRUE;
575     }
576   else if (strcmp (element_name, "fork") == 0)
577     {
578       if (!check_no_attributes (parser, "fork", attribute_names, attribute_values, error))
579         return FALSE;
580
581       if (push_element (parser, ELEMENT_FORK) == NULL)
582         {
583           BUS_SET_OOM (error);
584           return FALSE;
585         }
586
587       parser->fork = TRUE;
588       
589       return TRUE;
590     }
591   else if (strcmp (element_name, "pidfile") == 0)
592     {
593       if (!check_no_attributes (parser, "pidfile", attribute_names, attribute_values, error))
594         return FALSE;
595
596       if (push_element (parser, ELEMENT_PIDFILE) == NULL)
597         {
598           BUS_SET_OOM (error);
599           return FALSE;
600         }
601
602       return TRUE;
603     }
604   else if (strcmp (element_name, "listen") == 0)
605     {
606       if (!check_no_attributes (parser, "listen", attribute_names, attribute_values, error))
607         return FALSE;
608
609       if (push_element (parser, ELEMENT_LISTEN) == NULL)
610         {
611           BUS_SET_OOM (error);
612           return FALSE;
613         }
614
615       return TRUE;
616     }
617   else if (strcmp (element_name, "auth") == 0)
618     {
619       if (!check_no_attributes (parser, "auth", attribute_names, attribute_values, error))
620         return FALSE;
621
622       if (push_element (parser, ELEMENT_AUTH) == NULL)
623         {
624           BUS_SET_OOM (error);
625           return FALSE;
626         }
627
628       return TRUE;
629     }
630   else if (strcmp (element_name, "includedir") == 0)
631     {
632       if (!check_no_attributes (parser, "includedir", attribute_names, attribute_values, error))
633         return FALSE;
634
635       if (push_element (parser, ELEMENT_INCLUDEDIR) == NULL)
636         {
637           BUS_SET_OOM (error);
638           return FALSE;
639         }
640
641       return TRUE;
642     }
643   else if (strcmp (element_name, "servicedir") == 0)
644     {
645       if (!check_no_attributes (parser, "servicedir", attribute_names, attribute_values, error))
646         return FALSE;
647
648       if (push_element (parser, ELEMENT_SERVICEDIR) == NULL)
649         {
650           BUS_SET_OOM (error);
651           return FALSE;
652         }
653
654       return TRUE;
655     }
656   else if (strcmp (element_name, "include") == 0)
657     {
658       Element *e;
659       const char *ignore_missing;
660
661       if ((e = push_element (parser, ELEMENT_INCLUDE)) == NULL)
662         {
663           BUS_SET_OOM (error);
664           return FALSE;
665         }
666
667       e->d.include.ignore_missing = FALSE;
668
669       if (!locate_attributes (parser, "include",
670                               attribute_names,
671                               attribute_values,
672                               error,
673                               "ignore_missing", &ignore_missing,
674                               NULL))
675         return FALSE;
676
677       if (ignore_missing != NULL)
678         {
679           if (strcmp (ignore_missing, "yes") == 0)
680             e->d.include.ignore_missing = TRUE;
681           else if (strcmp (ignore_missing, "no") == 0)
682             e->d.include.ignore_missing = FALSE;
683           else
684             {
685               dbus_set_error (error, DBUS_ERROR_FAILED,
686                               "ignore_missing attribute must have value \"yes\" or \"no\"");
687               return FALSE;
688             }
689         }
690
691       return TRUE;
692     }
693   else if (strcmp (element_name, "policy") == 0)
694     {
695       Element *e;
696       const char *context;
697       const char *user;
698       const char *group;
699
700       if ((e = push_element (parser, ELEMENT_POLICY)) == NULL)
701         {
702           BUS_SET_OOM (error);
703           return FALSE;
704         }
705
706       e->d.policy.type = POLICY_IGNORED;
707       
708       if (!locate_attributes (parser, "policy",
709                               attribute_names,
710                               attribute_values,
711                               error,
712                               "context", &context,
713                               "user", &user,
714                               "group", &group,
715                               NULL))
716         return FALSE;
717
718       if (((context && user) ||
719            (context && group)) ||
720           (user && group) ||
721           !(context || user || group))
722         {
723           dbus_set_error (error, DBUS_ERROR_FAILED,
724                           "<policy> element must have exactly one of (context|user|group) attributes");
725           return FALSE;
726         }
727
728       if (context != NULL)
729         {
730           if (strcmp (context, "default") == 0)
731             {
732               e->d.policy.type = POLICY_DEFAULT;
733             }
734           else if (strcmp (context, "mandatory") == 0)
735             {
736               e->d.policy.type = POLICY_MANDATORY;
737             }
738           else
739             {
740               dbus_set_error (error, DBUS_ERROR_FAILED,
741                               "context attribute on <policy> must have the value \"default\" or \"mandatory\", not \"%s\"",
742                               context);
743               return FALSE;
744             }
745         }
746       else if (user != NULL)
747         {
748           DBusString username;
749           _dbus_string_init_const (&username, user);
750
751           if (_dbus_get_user_id (&username,
752                                  &e->d.policy.gid_or_uid))
753             e->d.policy.type = POLICY_USER;
754           else
755             _dbus_warn ("Unknown username \"%s\" in message bus configuration file\n",
756                         user);
757         }
758       else if (group != NULL)
759         {
760           DBusString group_name;
761           _dbus_string_init_const (&group_name, group);
762
763           if (_dbus_get_group_id (&group_name,
764                                   &e->d.policy.gid_or_uid))
765             e->d.policy.type = POLICY_GROUP;
766           else
767             _dbus_warn ("Unknown group \"%s\" in message bus configuration file\n",
768                         group);          
769         }
770       else
771         {
772           _dbus_assert_not_reached ("all <policy> attributes null and we didn't set error");
773         }
774       
775       return TRUE;
776     }
777   else if (strcmp (element_name, "limit") == 0)
778     {
779       Element *e;
780       const char *name;
781
782       if ((e = push_element (parser, ELEMENT_LIMIT)) == NULL)
783         {
784           BUS_SET_OOM (error);
785           return FALSE;
786         }
787       
788       if (!locate_attributes (parser, "limit",
789                               attribute_names,
790                               attribute_values,
791                               error,
792                               "name", &name,
793                               NULL))
794         return FALSE;
795
796       if (name == NULL)
797         {
798           dbus_set_error (error, DBUS_ERROR_FAILED,
799                           "<limit> element must have a \"name\" attribute");
800           return FALSE;
801         }
802
803       e->d.limit.name = _dbus_strdup (name);
804       if (e->d.limit.name == NULL)
805         {
806           BUS_SET_OOM (error);
807           return FALSE;
808         }
809
810       return TRUE;
811     }
812   else
813     {
814       dbus_set_error (error, DBUS_ERROR_FAILED,
815                       "Element <%s> not allowed inside <%s> in configuration file",
816                       element_name, "busconfig");
817       return FALSE;
818     }
819 }
820
821 static dbus_bool_t
822 append_rule_from_element (BusConfigParser   *parser,
823                           const char        *element_name,
824                           const char       **attribute_names,
825                           const char       **attribute_values,
826                           dbus_bool_t        allow,
827                           DBusError         *error)
828 {
829   const char *send_interface;
830   const char *send_member;
831   const char *send_error;
832   const char *send_destination;
833   const char *send_path;
834   const char *send_type;
835   const char *receive_interface;
836   const char *receive_member;
837   const char *receive_error;
838   const char *receive_sender;
839   const char *receive_path;
840   const char *receive_type;
841   const char *eavesdrop;
842   const char *requested_reply;
843   const char *own;
844   const char *user;
845   const char *group;
846   BusPolicyRule *rule;
847   
848   if (!locate_attributes (parser, element_name,
849                           attribute_names,
850                           attribute_values,
851                           error,
852                           "send_interface", &send_interface,
853                           "send_member", &send_member,
854                           "send_error", &send_error,
855                           "send_destination", &send_destination,
856                           "send_path", &send_path,
857                           "send_type", &send_type,
858                           "receive_interface", &receive_interface,
859                           "receive_member", &receive_member,
860                           "receive_error", &receive_error,
861                           "receive_sender", &receive_sender,
862                           "receive_path", &receive_path,
863                           "receive_type", &receive_type,
864                           "eavesdrop", &eavesdrop,
865                           "requested_reply", &requested_reply,
866                           "own", &own,
867                           "user", &user,
868                           "group", &group,
869                           NULL))
870     return FALSE;
871
872   if (!(send_interface || send_member || send_error || send_destination ||
873         send_type || send_path ||
874         receive_interface || receive_member || receive_error || receive_sender ||
875         receive_type || receive_path || eavesdrop || requested_reply ||
876         own || user || group))
877     {
878       dbus_set_error (error, DBUS_ERROR_FAILED,
879                       "Element <%s> must have one or more attributes",
880                       element_name);
881       return FALSE;
882     }
883
884   if ((send_member && (send_interface == NULL && send_path == NULL)) ||
885       (receive_member && (receive_interface == NULL && receive_path == NULL)))
886     {
887       dbus_set_error (error, DBUS_ERROR_FAILED,
888                       "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.",
889                       element_name);
890       return FALSE;
891     }
892   
893   /* Allowed combinations of elements are:
894    *
895    *   base, must be all send or all receive:
896    *     nothing
897    *     interface
898    *     interface + member
899    *     error
900    * 
901    *   base send_ can combine with send_destination, send_path, send_type
902    *   base receive_ with receive_sender, receive_path, receive_type, eavesdrop, requested_reply
903    *
904    *   user, group, own must occur alone
905    *
906    * Pretty sure the below stuff is broken, FIXME think about it more.
907    */
908
909   if (((send_interface && send_error) ||
910        (send_interface && receive_interface) ||
911        (send_interface && receive_member) ||
912        (send_interface && receive_error) ||
913        (send_interface && receive_sender) ||
914        (send_interface && eavesdrop) ||
915        (send_interface && requested_reply) ||
916        (send_interface && own) ||
917        (send_interface && user) ||
918        (send_interface && group)) ||
919
920       ((send_member && send_error) ||
921        (send_member && receive_interface) ||
922        (send_member && receive_member) ||
923        (send_member && receive_error) ||
924        (send_member && receive_sender) ||
925        (send_member && eavesdrop) ||
926        (send_member && requested_reply) ||
927        (send_member && own) ||
928        (send_member && user) ||
929        (send_member && group)) ||
930       
931       ((send_error && receive_interface) ||
932        (send_error && receive_member) ||
933        (send_error && receive_error) ||
934        (send_error && receive_sender) ||
935        (send_error && eavesdrop) ||
936        (send_error && requested_reply) ||
937        (send_error && own) ||
938        (send_error && user) ||
939        (send_error && group)) ||
940
941       ((send_destination && receive_interface) ||
942        (send_destination && receive_member) ||
943        (send_destination && receive_error) ||
944        (send_destination && receive_sender) ||
945        (send_destination && eavesdrop) ||
946        (send_destination && requested_reply) ||
947        (send_destination && own) ||
948        (send_destination && user) ||
949        (send_destination && group)) ||
950
951       ((send_type && receive_interface) ||
952        (send_type && receive_member) ||
953        (send_type && receive_error) ||
954        (send_type && receive_sender) ||
955        (send_type && eavesdrop) ||
956        (send_type && requested_reply) ||
957        (send_type && own) ||
958        (send_type && user) ||
959        (send_type && group)) ||
960
961       ((send_path && receive_interface) ||
962        (send_path && receive_member) ||
963        (send_path && receive_error) ||
964        (send_path && receive_sender) ||
965        (send_path && eavesdrop) ||
966        (send_path && requested_reply) ||
967        (send_path && own) ||
968        (send_path && user) ||
969        (send_path && group)) ||
970       
971       ((receive_interface && receive_error) ||
972        (receive_interface && own) ||
973        (receive_interface && user) ||
974        (receive_interface && group)) ||
975
976       ((receive_member && receive_error) ||
977        (receive_member && own) ||
978        (receive_member && user) ||
979        (receive_member && group)) ||
980       
981       ((receive_error && own) ||
982        (receive_error && user) ||
983        (receive_error && group)) ||
984
985       ((eavesdrop && own) ||
986        (eavesdrop && user) ||
987        (eavesdrop && group)) ||
988
989       ((requested_reply && own) ||
990        (requested_reply && user) ||
991        (requested_reply && group)) ||
992       
993       ((own && user) ||
994        (own && group)) ||
995
996       ((user && group)))
997     {
998       dbus_set_error (error, DBUS_ERROR_FAILED,
999                       "Invalid combination of attributes on element <%s>",
1000                       element_name);
1001       return FALSE;
1002     }
1003   
1004   rule = NULL;
1005
1006   /* In BusPolicyRule, NULL represents wildcard.
1007    * In the config file, '*' represents it.
1008    */
1009 #define IS_WILDCARD(str) ((str) && ((str)[0]) == '*' && ((str)[1]) == '\0')
1010
1011   if (send_interface || send_member || send_error || send_destination ||
1012       send_path || send_type)
1013     {
1014       int message_type;
1015       
1016       if (IS_WILDCARD (send_interface))
1017         send_interface = NULL;
1018       if (IS_WILDCARD (send_member))
1019         send_member = NULL;
1020       if (IS_WILDCARD (send_error))
1021         send_error = NULL;
1022       if (IS_WILDCARD (send_destination))
1023         send_destination = NULL;
1024       if (IS_WILDCARD (send_path))
1025         send_path = NULL;
1026       if (IS_WILDCARD (send_type))
1027         send_type = NULL;
1028
1029       message_type = DBUS_MESSAGE_TYPE_INVALID;
1030       if (send_type != NULL)
1031         {
1032           message_type = dbus_message_type_from_string (send_type);
1033           if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1034             {
1035               dbus_set_error (error, DBUS_ERROR_FAILED,
1036                               "Bad message type \"%s\"",
1037                               send_type);
1038               return FALSE;
1039             }
1040         }
1041       
1042       rule = bus_policy_rule_new (BUS_POLICY_RULE_SEND, allow); 
1043       if (rule == NULL)
1044         goto nomem;
1045       
1046       rule->d.send.message_type = message_type;
1047       rule->d.send.path = _dbus_strdup (send_path);
1048       rule->d.send.interface = _dbus_strdup (send_interface);
1049       rule->d.send.member = _dbus_strdup (send_member);
1050       rule->d.send.error = _dbus_strdup (send_error);
1051       rule->d.send.destination = _dbus_strdup (send_destination);
1052       if (send_path && rule->d.send.path == NULL)
1053         goto nomem;
1054       if (send_interface && rule->d.send.interface == NULL)
1055         goto nomem;
1056       if (send_member && rule->d.send.member == NULL)
1057         goto nomem;
1058       if (send_error && rule->d.send.error == NULL)
1059         goto nomem;
1060       if (send_destination && rule->d.send.destination == NULL)
1061         goto nomem;
1062     }
1063   else if (receive_interface || receive_member || receive_error || receive_sender ||
1064            receive_path || receive_type || eavesdrop || requested_reply)
1065     {
1066       int message_type;
1067       
1068       if (IS_WILDCARD (receive_interface))
1069         receive_interface = NULL;
1070       if (IS_WILDCARD (receive_member))
1071         receive_member = NULL;
1072       if (IS_WILDCARD (receive_error))
1073         receive_error = NULL;
1074       if (IS_WILDCARD (receive_sender))
1075         receive_sender = NULL;
1076       if (IS_WILDCARD (receive_path))
1077         receive_path = NULL;
1078       if (IS_WILDCARD (receive_type))
1079         receive_type = NULL;
1080
1081       message_type = DBUS_MESSAGE_TYPE_INVALID;
1082       if (receive_type != NULL)
1083         {
1084           message_type = dbus_message_type_from_string (receive_type);
1085           if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1086             {
1087               dbus_set_error (error, DBUS_ERROR_FAILED,
1088                               "Bad message type \"%s\"",
1089                               receive_type);
1090               return FALSE;
1091             }
1092         }
1093
1094
1095       if (eavesdrop &&
1096           !(strcmp (eavesdrop, "true") == 0 ||
1097             strcmp (eavesdrop, "false") == 0))
1098         {
1099           dbus_set_error (error, DBUS_ERROR_FAILED,
1100                           "Bad value \"%s\" for %s attribute, must be true or false",
1101                           "eavesdrop", eavesdrop);
1102           return FALSE;
1103         }
1104
1105       if (requested_reply &&
1106           !(strcmp (requested_reply, "true") == 0 ||
1107             strcmp (requested_reply, "false") == 0))
1108         {
1109           dbus_set_error (error, DBUS_ERROR_FAILED,
1110                           "Bad value \"%s\" for %s attribute, must be true or false",
1111                           "requested_reply", requested_reply);
1112           return FALSE;
1113         }
1114       
1115       rule = bus_policy_rule_new (BUS_POLICY_RULE_RECEIVE, allow); 
1116       if (rule == NULL)
1117         goto nomem;
1118
1119       if (eavesdrop)
1120         rule->d.receive.eavesdrop = (strcmp (eavesdrop, "true") == 0);
1121
1122       if (requested_reply)
1123         rule->d.receive.requested_reply = (strcmp (requested_reply, "true") == 0);
1124       
1125       rule->d.receive.message_type = message_type;
1126       rule->d.receive.path = _dbus_strdup (receive_path);
1127       rule->d.receive.interface = _dbus_strdup (receive_interface);
1128       rule->d.receive.member = _dbus_strdup (receive_member);
1129       rule->d.receive.error = _dbus_strdup (receive_error);
1130       rule->d.receive.origin = _dbus_strdup (receive_sender);
1131
1132       if (receive_path && rule->d.receive.path == NULL)
1133         goto nomem;
1134       if (receive_interface && rule->d.receive.interface == NULL)
1135         goto nomem;
1136       if (receive_member && rule->d.receive.member == NULL)
1137         goto nomem;
1138       if (receive_error && rule->d.receive.error == NULL)
1139         goto nomem;
1140       if (receive_sender && rule->d.receive.origin == NULL)
1141         goto nomem;
1142     }
1143   else if (own)
1144     {
1145       rule = bus_policy_rule_new (BUS_POLICY_RULE_OWN, allow); 
1146       if (rule == NULL)
1147         goto nomem;
1148
1149       if (IS_WILDCARD (own))
1150         own = NULL;
1151       
1152       rule->d.own.service_name = _dbus_strdup (own);
1153       if (own && rule->d.own.service_name == NULL)
1154         goto nomem;
1155     }
1156   else if (user)
1157     {      
1158       if (IS_WILDCARD (user))
1159         {
1160           rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow); 
1161           if (rule == NULL)
1162             goto nomem;
1163
1164           rule->d.user.uid = DBUS_UID_UNSET;
1165         }
1166       else
1167         {
1168           DBusString username;
1169           dbus_uid_t uid;
1170           
1171           _dbus_string_init_const (&username, user);
1172       
1173           if (_dbus_get_user_id (&username, &uid))
1174             {
1175               rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow); 
1176               if (rule == NULL)
1177                 goto nomem;
1178
1179               rule->d.user.uid = uid;
1180             }
1181           else
1182             {
1183               _dbus_warn ("Unknown username \"%s\" on element <%s>\n",
1184                           user, element_name);
1185             }
1186         }
1187     }
1188   else if (group)
1189     {
1190       if (IS_WILDCARD (group))
1191         {
1192           rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow); 
1193           if (rule == NULL)
1194             goto nomem;
1195
1196           rule->d.group.gid = DBUS_GID_UNSET;
1197         }
1198       else
1199         {
1200           DBusString groupname;
1201           dbus_gid_t gid;
1202           
1203           _dbus_string_init_const (&groupname, group);
1204           
1205           if (_dbus_get_user_id (&groupname, &gid))
1206             {
1207               rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow); 
1208               if (rule == NULL)
1209                 goto nomem;
1210
1211               rule->d.group.gid = gid;
1212             }
1213           else
1214             {
1215               _dbus_warn ("Unknown group \"%s\" on element <%s>\n",
1216                           group, element_name);
1217             }
1218         }
1219     }
1220   else
1221     _dbus_assert_not_reached ("Did not handle some combination of attributes on <allow> or <deny>");
1222
1223   if (rule != NULL)
1224     {
1225       Element *pe;
1226       
1227       pe = peek_element (parser);      
1228       _dbus_assert (pe != NULL);
1229       _dbus_assert (pe->type == ELEMENT_POLICY);
1230
1231       switch (pe->d.policy.type)
1232         {
1233         case POLICY_IGNORED:
1234           /* drop the rule on the floor */
1235           break;
1236           
1237         case POLICY_DEFAULT:
1238           if (!bus_policy_append_default_rule (parser->policy, rule))
1239             goto nomem;
1240           break;
1241         case POLICY_MANDATORY:
1242           if (!bus_policy_append_mandatory_rule (parser->policy, rule))
1243             goto nomem;
1244           break;
1245         case POLICY_USER:
1246           if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1247             {
1248               dbus_set_error (error, DBUS_ERROR_FAILED,
1249                               "<%s> rule cannot be per-user because it has bus-global semantics",
1250                               element_name);
1251               goto failed;
1252             }
1253           
1254           if (!bus_policy_append_user_rule (parser->policy, pe->d.policy.gid_or_uid,
1255                                             rule))
1256             goto nomem;
1257           break;
1258         case POLICY_GROUP:
1259           if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1260             {
1261               dbus_set_error (error, DBUS_ERROR_FAILED,
1262                               "<%s> rule cannot be per-group because it has bus-global semantics",
1263                               element_name);
1264               goto failed;
1265             }
1266           
1267           if (!bus_policy_append_group_rule (parser->policy, pe->d.policy.gid_or_uid,
1268                                              rule))
1269             goto nomem;
1270           break;
1271         }
1272       
1273       bus_policy_rule_unref (rule);
1274       rule = NULL;
1275     }
1276   
1277   return TRUE;
1278
1279  nomem:
1280   BUS_SET_OOM (error);
1281  failed:
1282   if (rule)
1283     bus_policy_rule_unref (rule);
1284   return FALSE;
1285 }
1286
1287 static dbus_bool_t
1288 start_policy_child (BusConfigParser   *parser,
1289                     const char        *element_name,
1290                     const char       **attribute_names,
1291                     const char       **attribute_values,
1292                     DBusError         *error)
1293 {
1294   if (strcmp (element_name, "allow") == 0)
1295     {
1296       if (!append_rule_from_element (parser, element_name,
1297                                      attribute_names, attribute_values,
1298                                      TRUE, error))
1299         return FALSE;
1300       
1301       if (push_element (parser, ELEMENT_ALLOW) == NULL)
1302         {
1303           BUS_SET_OOM (error);
1304           return FALSE;
1305         }
1306       
1307       return TRUE;
1308     }
1309   else if (strcmp (element_name, "deny") == 0)
1310     {
1311       if (!append_rule_from_element (parser, element_name,
1312                                      attribute_names, attribute_values,
1313                                      FALSE, error))
1314         return FALSE;
1315       
1316       if (push_element (parser, ELEMENT_DENY) == NULL)
1317         {
1318           BUS_SET_OOM (error);
1319           return FALSE;
1320         }
1321       
1322       return TRUE;
1323     }
1324   else
1325     {
1326       dbus_set_error (error, DBUS_ERROR_FAILED,
1327                       "Element <%s> not allowed inside <%s> in configuration file",
1328                       element_name, "policy");
1329       return FALSE;
1330     }
1331 }
1332
1333 dbus_bool_t
1334 bus_config_parser_start_element (BusConfigParser   *parser,
1335                                  const char        *element_name,
1336                                  const char       **attribute_names,
1337                                  const char       **attribute_values,
1338                                  DBusError         *error)
1339 {
1340   ElementType t;
1341
1342   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1343
1344   /* printf ("START: %s\n", element_name); */
1345   
1346   t = top_element_type (parser);
1347
1348   if (t == ELEMENT_NONE)
1349     {
1350       if (strcmp (element_name, "busconfig") == 0)
1351         {
1352           if (!check_no_attributes (parser, "busconfig", attribute_names, attribute_values, error))
1353             return FALSE;
1354           
1355           if (push_element (parser, ELEMENT_BUSCONFIG) == NULL)
1356             {
1357               BUS_SET_OOM (error);
1358               return FALSE;
1359             }
1360
1361           return TRUE;
1362         }
1363       else
1364         {
1365           dbus_set_error (error, DBUS_ERROR_FAILED,
1366                           "Unknown element <%s> at root of configuration file",
1367                           element_name);
1368           return FALSE;
1369         }
1370     }
1371   else if (t == ELEMENT_BUSCONFIG)
1372     {
1373       return start_busconfig_child (parser, element_name,
1374                                     attribute_names, attribute_values,
1375                                     error);
1376     }
1377   else if (t == ELEMENT_POLICY)
1378     {
1379       return start_policy_child (parser, element_name,
1380                                  attribute_names, attribute_values,
1381                                  error);
1382     }
1383   else
1384     {
1385       dbus_set_error (error, DBUS_ERROR_FAILED,
1386                       "Element <%s> is not allowed in this context",
1387                       element_name);
1388       return FALSE;
1389     }  
1390 }
1391
1392 static dbus_bool_t
1393 set_limit (BusConfigParser *parser,
1394            const char      *name,
1395            long             value,
1396            DBusError       *error)
1397 {
1398   dbus_bool_t must_be_positive;
1399   dbus_bool_t must_be_int;
1400
1401   must_be_int = FALSE;
1402   must_be_positive = FALSE;
1403   
1404   if (strcmp (name, "max_incoming_bytes") == 0)
1405     {
1406       must_be_positive = TRUE;
1407       parser->limits.max_incoming_bytes = value;
1408     }
1409   else if (strcmp (name, "max_outgoing_bytes") == 0)
1410     {
1411       must_be_positive = TRUE;
1412       parser->limits.max_outgoing_bytes = value;
1413     }
1414   else if (strcmp (name, "max_message_size") == 0)
1415     {
1416       must_be_positive = TRUE;
1417       parser->limits.max_message_size = value;
1418     }
1419   else if (strcmp (name, "activation_timeout") == 0)
1420     {
1421       must_be_positive = TRUE;
1422       must_be_int = TRUE;
1423       parser->limits.activation_timeout = value;
1424     }
1425   else if (strcmp (name, "auth_timeout") == 0)
1426     {
1427       must_be_positive = TRUE;
1428       must_be_int = TRUE;
1429       parser->limits.auth_timeout = value;
1430     }
1431   else if (strcmp (name, "reply_timeout") == 0)
1432     {
1433       must_be_positive = TRUE;
1434       must_be_int = TRUE;
1435       parser->limits.reply_timeout = value;
1436     }
1437   else if (strcmp (name, "max_completed_connections") == 0)
1438     {
1439       must_be_positive = TRUE;
1440       must_be_int = TRUE;
1441       parser->limits.max_completed_connections = value;
1442     }
1443   else if (strcmp (name, "max_incomplete_connections") == 0)
1444     {
1445       must_be_positive = TRUE;
1446       must_be_int = TRUE;
1447       parser->limits.max_incomplete_connections = value;
1448     }
1449   else if (strcmp (name, "max_connections_per_user") == 0)
1450     {
1451       must_be_positive = TRUE;
1452       must_be_int = TRUE;
1453       parser->limits.max_connections_per_user = value;
1454     }
1455   else if (strcmp (name, "max_pending_activations") == 0)
1456     {
1457       must_be_positive = TRUE;
1458       must_be_int = TRUE;
1459       parser->limits.max_pending_activations = value;
1460     }
1461   else if (strcmp (name, "max_services_per_connection") == 0)
1462     {
1463       must_be_positive = TRUE;
1464       must_be_int = TRUE;
1465       parser->limits.max_services_per_connection = value;
1466     }
1467   else if (strcmp (name, "max_replies_per_connection") == 0)
1468     {
1469       must_be_positive = TRUE;
1470       must_be_int = TRUE;
1471       parser->limits.max_replies_per_connection = value;
1472     }
1473   else
1474     {
1475       dbus_set_error (error, DBUS_ERROR_FAILED,
1476                       "There is no limit called \"%s\"\n",
1477                       name);
1478       return FALSE;
1479     }
1480   
1481   if (must_be_positive && value < 0)
1482     {
1483       dbus_set_error (error, DBUS_ERROR_FAILED,
1484                       "<limit name=\"%s\"> must be a positive number\n",
1485                       name);
1486       return FALSE;
1487     }
1488
1489   if (must_be_int &&
1490       (value < _DBUS_INT_MIN || value > _DBUS_INT_MAX))
1491     {
1492       dbus_set_error (error, DBUS_ERROR_FAILED,
1493                       "<limit name=\"%s\"> value is too large\n",
1494                       name);
1495       return FALSE;
1496     }
1497
1498   return TRUE;  
1499 }
1500
1501 dbus_bool_t
1502 bus_config_parser_end_element (BusConfigParser   *parser,
1503                                const char        *element_name,
1504                                DBusError         *error)
1505 {
1506   ElementType t;
1507   const char *n;
1508   Element *e;
1509
1510   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1511
1512   /* printf ("END: %s\n", element_name); */
1513   
1514   t = top_element_type (parser);
1515
1516   if (t == ELEMENT_NONE)
1517     {
1518       /* should probably be an assertion failure but
1519        * being paranoid about XML parsers
1520        */
1521       dbus_set_error (error, DBUS_ERROR_FAILED,
1522                       "XML parser ended element with no element on the stack");
1523       return FALSE;
1524     }
1525
1526   n = element_type_to_name (t);
1527   _dbus_assert (n != NULL);
1528   if (strcmp (n, element_name) != 0)
1529     {
1530       /* should probably be an assertion failure but
1531        * being paranoid about XML parsers
1532        */
1533       dbus_set_error (error, DBUS_ERROR_FAILED,
1534                       "XML element <%s> ended but topmost element on the stack was <%s>",
1535                       element_name, n);
1536       return FALSE;
1537     }
1538
1539   e = peek_element (parser);
1540   _dbus_assert (e != NULL);
1541
1542   switch (e->type)
1543     {
1544     case ELEMENT_NONE:
1545       _dbus_assert_not_reached ("element in stack has no type");
1546       break;
1547
1548     case ELEMENT_INCLUDE:
1549     case ELEMENT_USER:
1550     case ELEMENT_TYPE:
1551     case ELEMENT_LISTEN:
1552     case ELEMENT_PIDFILE:
1553     case ELEMENT_AUTH:
1554     case ELEMENT_SERVICEDIR:
1555     case ELEMENT_INCLUDEDIR:
1556     case ELEMENT_LIMIT:
1557       if (!e->had_content)
1558         {
1559           dbus_set_error (error, DBUS_ERROR_FAILED,
1560                           "XML element <%s> was expected to have content inside it",
1561                           element_type_to_name (e->type));
1562           return FALSE;
1563         }
1564
1565       if (e->type == ELEMENT_LIMIT)
1566         {
1567           if (!set_limit (parser, e->d.limit.name, e->d.limit.value,
1568                           error))
1569             return FALSE;
1570         }
1571       break;
1572
1573     case ELEMENT_BUSCONFIG:
1574     case ELEMENT_POLICY:
1575     case ELEMENT_ALLOW:
1576     case ELEMENT_DENY:
1577     case ELEMENT_FORK:
1578       break;
1579     }
1580
1581   pop_element (parser);
1582
1583   return TRUE;
1584 }
1585
1586 static dbus_bool_t
1587 all_whitespace (const DBusString *str)
1588 {
1589   int i;
1590
1591   _dbus_string_skip_white (str, 0, &i);
1592
1593   return i == _dbus_string_get_length (str);
1594 }
1595
1596 static dbus_bool_t
1597 make_full_path (const DBusString *basedir,
1598                 const DBusString *filename,
1599                 DBusString       *full_path)
1600 {
1601   if (_dbus_path_is_absolute (filename))
1602     {
1603       return _dbus_string_copy (filename, 0, full_path, 0);
1604     }
1605   else
1606     {
1607       if (!_dbus_string_copy (basedir, 0, full_path, 0))
1608         return FALSE;
1609       
1610       if (!_dbus_concat_dir_and_file (full_path, filename))
1611         return FALSE;
1612
1613       return TRUE;
1614     }
1615 }
1616
1617 static dbus_bool_t
1618 include_file (BusConfigParser   *parser,
1619               const DBusString  *filename,
1620               dbus_bool_t        ignore_missing,
1621               DBusError         *error)
1622 {
1623   /* FIXME good test case for this would load each config file in the
1624    * test suite both alone, and as an include, and check
1625    * that the result is the same
1626    */
1627   BusConfigParser *included;
1628   DBusError tmp_error;
1629         
1630   dbus_error_init (&tmp_error);
1631   included = bus_config_load (filename, FALSE, &tmp_error);
1632   if (included == NULL)
1633     {
1634       _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
1635
1636       if (dbus_error_has_name (&tmp_error, DBUS_ERROR_FILE_NOT_FOUND) &&
1637           ignore_missing)
1638         {
1639           dbus_error_free (&tmp_error);
1640           return TRUE;
1641         }
1642       else
1643         {
1644           dbus_move_error (&tmp_error, error);
1645           return FALSE;
1646         }
1647     }
1648   else
1649     {
1650       _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
1651
1652       if (!merge_included (parser, included, error))
1653         {
1654           bus_config_parser_unref (included);
1655           return FALSE;
1656         }
1657
1658       bus_config_parser_unref (included);
1659       return TRUE;
1660     }
1661 }
1662
1663 static dbus_bool_t
1664 include_dir (BusConfigParser   *parser,
1665              const DBusString  *dirname,
1666              DBusError         *error)
1667 {
1668   DBusString filename;
1669   dbus_bool_t retval;
1670   DBusError tmp_error;
1671   DBusDirIter *dir;
1672   
1673   if (!_dbus_string_init (&filename))
1674     {
1675       BUS_SET_OOM (error);
1676       return FALSE;
1677     }
1678
1679   retval = FALSE;
1680   
1681   dir = _dbus_directory_open (dirname, error);
1682
1683   if (dir == NULL)
1684     goto failed;
1685
1686   dbus_error_init (&tmp_error);
1687   while (_dbus_directory_get_next_file (dir, &filename, &tmp_error))
1688     {
1689       DBusString full_path;
1690
1691       if (!_dbus_string_init (&full_path))
1692         {
1693           BUS_SET_OOM (error);
1694           goto failed;
1695         }
1696
1697       if (!_dbus_string_copy (dirname, 0, &full_path, 0))
1698         {
1699           BUS_SET_OOM (error);
1700           _dbus_string_free (&full_path);
1701           goto failed;
1702         }      
1703
1704       if (!_dbus_concat_dir_and_file (&full_path, &filename))
1705         {
1706           BUS_SET_OOM (error);
1707           _dbus_string_free (&full_path);
1708           goto failed;
1709         }
1710       
1711       if (_dbus_string_ends_with_c_str (&full_path, ".conf"))
1712         {
1713           if (!include_file (parser, &full_path, TRUE, error))
1714             {
1715               _dbus_string_free (&full_path);
1716               goto failed;
1717             }
1718         }
1719
1720       _dbus_string_free (&full_path);
1721     }
1722
1723   if (dbus_error_is_set (&tmp_error))
1724     {
1725       dbus_move_error (&tmp_error, error);
1726       goto failed;
1727     }
1728   
1729   retval = TRUE;
1730   
1731  failed:
1732   _dbus_string_free (&filename);
1733   
1734   if (dir)
1735     _dbus_directory_close (dir);
1736
1737   return retval;
1738 }
1739
1740 dbus_bool_t
1741 bus_config_parser_content (BusConfigParser   *parser,
1742                            const DBusString  *content,
1743                            DBusError         *error)
1744 {
1745   Element *e;
1746
1747   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1748
1749 #if 0
1750   {
1751     const char *c_str;
1752     
1753     _dbus_string_get_const_data (content, &c_str);
1754
1755     printf ("CONTENT %d bytes: %s\n", _dbus_string_get_length (content), c_str);
1756   }
1757 #endif
1758   
1759   e = peek_element (parser);
1760   if (e == NULL)
1761     {
1762       dbus_set_error (error, DBUS_ERROR_FAILED,
1763                       "Text content outside of any XML element in configuration file");
1764       return FALSE;
1765     }
1766   else if (e->had_content)
1767     {
1768       _dbus_assert_not_reached ("Element had multiple content blocks");
1769       return FALSE;
1770     }
1771
1772   switch (top_element_type (parser))
1773     {
1774     case ELEMENT_NONE:
1775       _dbus_assert_not_reached ("element at top of stack has no type");
1776       return FALSE;
1777
1778     case ELEMENT_BUSCONFIG:
1779     case ELEMENT_POLICY:
1780     case ELEMENT_ALLOW:
1781     case ELEMENT_DENY:
1782     case ELEMENT_FORK:
1783       if (all_whitespace (content))
1784         return TRUE;
1785       else
1786         {
1787           dbus_set_error (error, DBUS_ERROR_FAILED,
1788                           "No text content expected inside XML element %s in configuration file",
1789                           element_type_to_name (top_element_type (parser)));
1790           return FALSE;
1791         }
1792
1793     case ELEMENT_PIDFILE:
1794       {
1795         char *s;
1796
1797         e->had_content = TRUE;
1798         
1799         if (!_dbus_string_copy_data (content, &s))
1800           goto nomem;
1801           
1802         dbus_free (parser->pidfile);
1803         parser->pidfile = s;
1804       }
1805       break;
1806
1807     case ELEMENT_INCLUDE:
1808       {
1809         DBusString full_path;
1810         
1811         e->had_content = TRUE;
1812
1813         if (!_dbus_string_init (&full_path))
1814           goto nomem;
1815         
1816         if (!make_full_path (&parser->basedir, content, &full_path))
1817           {
1818             _dbus_string_free (&full_path);
1819             goto nomem;
1820           }
1821         
1822         if (!include_file (parser, &full_path,
1823                            e->d.include.ignore_missing, error))
1824           {
1825             _dbus_string_free (&full_path);
1826             return FALSE;
1827           }
1828
1829         _dbus_string_free (&full_path);
1830       }
1831       break;
1832
1833     case ELEMENT_INCLUDEDIR:
1834       {
1835         DBusString full_path;
1836         
1837         e->had_content = TRUE;
1838
1839         if (!_dbus_string_init (&full_path))
1840           goto nomem;
1841         
1842         if (!make_full_path (&parser->basedir, content, &full_path))
1843           {
1844             _dbus_string_free (&full_path);
1845             goto nomem;
1846           }
1847         
1848         if (!include_dir (parser, &full_path, error))
1849           {
1850             _dbus_string_free (&full_path);
1851             return FALSE;
1852           }
1853
1854         _dbus_string_free (&full_path);
1855       }
1856       break;
1857       
1858     case ELEMENT_USER:
1859       {
1860         char *s;
1861
1862         e->had_content = TRUE;
1863         
1864         if (!_dbus_string_copy_data (content, &s))
1865           goto nomem;
1866           
1867         dbus_free (parser->user);
1868         parser->user = s;
1869       }
1870       break;
1871
1872     case ELEMENT_TYPE:
1873       {
1874         char *s;
1875
1876         e->had_content = TRUE;
1877
1878         if (!_dbus_string_copy_data (content, &s))
1879           goto nomem;
1880         
1881         dbus_free (parser->bus_type);
1882         parser->bus_type = s;
1883       }
1884       break;
1885       
1886     case ELEMENT_LISTEN:
1887       {
1888         char *s;
1889
1890         e->had_content = TRUE;
1891         
1892         if (!_dbus_string_copy_data (content, &s))
1893           goto nomem;
1894
1895         if (!_dbus_list_append (&parser->listen_on,
1896                                 s))
1897           {
1898             dbus_free (s);
1899             goto nomem;
1900           }
1901       }
1902       break;
1903
1904     case ELEMENT_AUTH:
1905       {
1906         char *s;
1907         
1908         e->had_content = TRUE;
1909
1910         if (!_dbus_string_copy_data (content, &s))
1911           goto nomem;
1912
1913         if (!_dbus_list_append (&parser->mechanisms,
1914                                 s))
1915           {
1916             dbus_free (s);
1917             goto nomem;
1918           }
1919       }
1920       break;
1921
1922     case ELEMENT_SERVICEDIR:
1923       {
1924         char *s;
1925         DBusString full_path;
1926         
1927         e->had_content = TRUE;
1928
1929         if (!_dbus_string_init (&full_path))
1930           goto nomem;
1931         
1932         if (!make_full_path (&parser->basedir, content, &full_path))
1933           {
1934             _dbus_string_free (&full_path);
1935             goto nomem;
1936           }
1937         
1938         if (!_dbus_string_copy_data (&full_path, &s))
1939           {
1940             _dbus_string_free (&full_path);
1941             goto nomem;
1942           }
1943
1944         if (!_dbus_list_append (&parser->service_dirs, s))
1945           {
1946             _dbus_string_free (&full_path);
1947             dbus_free (s);
1948             goto nomem;
1949           }
1950
1951         _dbus_string_free (&full_path);
1952       }
1953       break;
1954
1955     case ELEMENT_LIMIT:
1956       {
1957         long val;
1958
1959         e->had_content = TRUE;
1960
1961         val = 0;
1962         if (!_dbus_string_parse_int (content, 0, &val, NULL))
1963           {
1964             dbus_set_error (error, DBUS_ERROR_FAILED,
1965                             "<limit name=\"%s\"> element has invalid value (could not parse as integer)",
1966                             e->d.limit.name);
1967             return FALSE;
1968           }
1969
1970         e->d.limit.value = val;
1971
1972         _dbus_verbose ("Loaded value %ld for limit %s\n",
1973                        e->d.limit.value,
1974                        e->d.limit.name);
1975       }
1976       break;
1977     }
1978
1979   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1980   return TRUE;
1981
1982  nomem:
1983   BUS_SET_OOM (error);
1984   return FALSE;
1985 }
1986
1987 dbus_bool_t
1988 bus_config_parser_finished (BusConfigParser   *parser,
1989                             DBusError         *error)
1990 {
1991   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1992
1993   if (parser->stack != NULL)
1994     {
1995       dbus_set_error (error, DBUS_ERROR_FAILED,
1996                       "Element <%s> was not closed in configuration file",
1997                       element_type_to_name (top_element_type (parser)));
1998
1999       return FALSE;
2000     }
2001
2002   if (parser->is_toplevel && parser->listen_on == NULL)
2003     {
2004       dbus_set_error (error, DBUS_ERROR_FAILED,
2005                       "Configuration file needs one or more <listen> elements giving addresses"); 
2006       return FALSE;
2007     }
2008   
2009   return TRUE;
2010 }
2011
2012 const char*
2013 bus_config_parser_get_user (BusConfigParser *parser)
2014 {
2015   return parser->user;
2016 }
2017
2018 const char*
2019 bus_config_parser_get_type (BusConfigParser *parser)
2020 {
2021   return parser->bus_type;
2022 }
2023
2024 DBusList**
2025 bus_config_parser_get_addresses (BusConfigParser *parser)
2026 {
2027   return &parser->listen_on;
2028 }
2029
2030 DBusList**
2031 bus_config_parser_get_mechanisms (BusConfigParser *parser)
2032 {
2033   return &parser->mechanisms;
2034 }
2035
2036 DBusList**
2037 bus_config_parser_get_service_dirs (BusConfigParser *parser)
2038 {
2039   return &parser->service_dirs;
2040 }
2041
2042 dbus_bool_t
2043 bus_config_parser_get_fork (BusConfigParser   *parser)
2044 {
2045   return parser->fork;
2046 }
2047
2048 const char *
2049 bus_config_parser_get_pidfile (BusConfigParser   *parser)
2050 {
2051   return parser->pidfile;
2052 }
2053
2054 BusPolicy*
2055 bus_config_parser_steal_policy (BusConfigParser *parser)
2056 {
2057   BusPolicy *policy;
2058
2059   _dbus_assert (parser->policy != NULL); /* can only steal the policy 1 time */
2060   
2061   policy = parser->policy;
2062
2063   parser->policy = NULL;
2064
2065   return policy;
2066 }
2067
2068 /* Overwrite any limits that were set in the configuration file */
2069 void
2070 bus_config_parser_get_limits (BusConfigParser *parser,
2071                               BusLimits       *limits)
2072 {
2073   *limits = parser->limits;
2074 }
2075
2076 #ifdef DBUS_BUILD_TESTS
2077 #include <stdio.h>
2078
2079 typedef enum
2080 {
2081   VALID,
2082   INVALID,
2083   UNKNOWN
2084 } Validity;
2085
2086 static dbus_bool_t
2087 do_load (const DBusString *full_path,
2088          Validity          validity,
2089          dbus_bool_t       oom_possible)
2090 {
2091   BusConfigParser *parser;
2092   DBusError error;
2093
2094   dbus_error_init (&error);
2095
2096   parser = bus_config_load (full_path, TRUE, &error);
2097   if (parser == NULL)
2098     {
2099       _DBUS_ASSERT_ERROR_IS_SET (&error);
2100
2101       if (oom_possible &&
2102           dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2103         {
2104           _dbus_verbose ("Failed to load valid file due to OOM\n");
2105           dbus_error_free (&error);
2106           return TRUE;
2107         }
2108       else if (validity == VALID)
2109         {
2110           _dbus_warn ("Failed to load valid file but still had memory: %s\n",
2111                       error.message);
2112
2113           dbus_error_free (&error);
2114           return FALSE;
2115         }
2116       else
2117         {
2118           dbus_error_free (&error);
2119           return TRUE;
2120         }
2121     }
2122   else
2123     {
2124       _DBUS_ASSERT_ERROR_IS_CLEAR (&error);
2125
2126       bus_config_parser_unref (parser);
2127
2128       if (validity == INVALID)
2129         {
2130           _dbus_warn ("Accepted invalid file\n");
2131           return FALSE;
2132         }
2133
2134       return TRUE;
2135     }
2136 }
2137
2138 typedef struct
2139 {
2140   const DBusString *full_path;
2141   Validity          validity;
2142 } LoaderOomData;
2143
2144 static dbus_bool_t
2145 check_loader_oom_func (void *data)
2146 {
2147   LoaderOomData *d = data;
2148
2149   return do_load (d->full_path, d->validity, TRUE);
2150 }
2151
2152 static dbus_bool_t
2153 process_test_subdir (const DBusString *test_base_dir,
2154                      const char       *subdir,
2155                      Validity          validity)
2156 {
2157   DBusString test_directory;
2158   DBusString filename;
2159   DBusDirIter *dir;
2160   dbus_bool_t retval;
2161   DBusError error;
2162
2163   retval = FALSE;
2164   dir = NULL;
2165
2166   if (!_dbus_string_init (&test_directory))
2167     _dbus_assert_not_reached ("didn't allocate test_directory\n");
2168
2169   _dbus_string_init_const (&filename, subdir);
2170
2171   if (!_dbus_string_copy (test_base_dir, 0,
2172                           &test_directory, 0))
2173     _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2174
2175   if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2176     _dbus_assert_not_reached ("couldn't allocate full path");
2177
2178   _dbus_string_free (&filename);
2179   if (!_dbus_string_init (&filename))
2180     _dbus_assert_not_reached ("didn't allocate filename string\n");
2181
2182   dbus_error_init (&error);
2183   dir = _dbus_directory_open (&test_directory, &error);
2184   if (dir == NULL)
2185     {
2186       _dbus_warn ("Could not open %s: %s\n",
2187                   _dbus_string_get_const_data (&test_directory),
2188                   error.message);
2189       dbus_error_free (&error);
2190       goto failed;
2191     }
2192
2193   printf ("Testing:\n");
2194
2195  next:
2196   while (_dbus_directory_get_next_file (dir, &filename, &error))
2197     {
2198       DBusString full_path;
2199       LoaderOomData d;
2200
2201       if (!_dbus_string_init (&full_path))
2202         _dbus_assert_not_reached ("couldn't init string");
2203
2204       if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2205         _dbus_assert_not_reached ("couldn't copy dir to full_path");
2206
2207       if (!_dbus_concat_dir_and_file (&full_path, &filename))
2208         _dbus_assert_not_reached ("couldn't concat file to dir");
2209
2210       if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
2211         {
2212           _dbus_verbose ("Skipping non-.conf file %s\n",
2213                          _dbus_string_get_const_data (&filename));
2214           _dbus_string_free (&full_path);
2215           goto next;
2216         }
2217
2218       printf ("    %s\n", _dbus_string_get_const_data (&filename));
2219
2220       _dbus_verbose (" expecting %s\n",
2221                      validity == VALID ? "valid" :
2222                      (validity == INVALID ? "invalid" :
2223                       (validity == UNKNOWN ? "unknown" : "???")));
2224
2225       d.full_path = &full_path;
2226       d.validity = validity;
2227       if (!_dbus_test_oom_handling ("config-loader", check_loader_oom_func, &d))
2228         _dbus_assert_not_reached ("test failed");
2229
2230       _dbus_string_free (&full_path);
2231     }
2232
2233   if (dbus_error_is_set (&error))
2234     {
2235       _dbus_warn ("Could not get next file in %s: %s\n",
2236                   _dbus_string_get_const_data (&test_directory),
2237                   error.message);
2238       dbus_error_free (&error);
2239       goto failed;
2240     }
2241
2242   retval = TRUE;
2243
2244  failed:
2245
2246   if (dir)
2247     _dbus_directory_close (dir);
2248   _dbus_string_free (&test_directory);
2249   _dbus_string_free (&filename);
2250
2251   return retval;
2252 }
2253
2254 dbus_bool_t
2255 bus_config_parser_test (const DBusString *test_data_dir)
2256 {
2257   if (test_data_dir == NULL ||
2258       _dbus_string_get_length (test_data_dir) == 0)
2259     {
2260       printf ("No test data\n");
2261       return TRUE;
2262     }
2263
2264   if (!process_test_subdir (test_data_dir, "valid-config-files", VALID))
2265     return FALSE;
2266
2267   return TRUE;
2268 }
2269
2270 #endif /* DBUS_BUILD_TESTS */
2271