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