2003-04-12 Havoc Pennington <hp@pobox.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 struct
51 {
52   ElementType type;
53
54   unsigned int had_content : 1;
55
56   union
57   {
58     struct
59     {
60       unsigned int ignore_missing : 1;
61     } include;
62
63     struct
64     {
65       char *context;
66       char *user;
67       char *group;
68       DBusList *rules;
69     } policy;
70
71     struct
72     {
73       int foo;
74     } limit;
75
76   } d;
77
78 } Element;
79
80 struct BusConfigParser
81 {
82   int refcount;
83
84   DBusString basedir;  /**< Directory we resolve paths relative to */
85   
86   DBusList *stack;     /**< stack of Element */
87
88   char *user;          /**< user to run as */
89
90   char *bus_type;          /**< Message bus type */
91   
92   DBusList *listen_on; /**< List of addresses to listen to */
93
94   DBusList *mechanisms; /**< Auth mechanisms */
95
96   DBusList *service_dirs; /**< Directories to look for services in */
97
98   BusPolicy *policy;     /**< Security policy */
99   
100   unsigned int fork : 1; /**< TRUE to fork into daemon mode */
101
102   char *pidfile;
103 };
104
105 static const char*
106 element_type_to_name (ElementType type)
107 {
108   switch (type)
109     {
110     case ELEMENT_NONE:
111       return NULL;
112     case ELEMENT_BUSCONFIG:
113       return "busconfig";
114     case ELEMENT_INCLUDE:
115       return "include";
116     case ELEMENT_USER:
117       return "user";
118     case ELEMENT_LISTEN:
119       return "listen";
120     case ELEMENT_AUTH:
121       return "auth";
122     case ELEMENT_POLICY:
123       return "policy";
124     case ELEMENT_LIMIT:
125       return "limit";
126     case ELEMENT_ALLOW:
127       return "allow";
128     case ELEMENT_DENY:
129       return "deny";
130     case ELEMENT_FORK:
131       return "fork";
132     case ELEMENT_PIDFILE:
133       return "pidfile";
134     case ELEMENT_SERVICEDIR:
135       return "servicedir";
136     case ELEMENT_INCLUDEDIR:
137       return "includedir";
138     case ELEMENT_TYPE:
139       return "type";
140     }
141
142   _dbus_assert_not_reached ("bad element type");
143
144   return NULL;
145 }
146
147 static Element*
148 push_element (BusConfigParser *parser,
149               ElementType      type)
150 {
151   Element *e;
152
153   _dbus_assert (type != ELEMENT_NONE);
154   
155   e = dbus_new0 (Element, 1);
156   if (e == NULL)
157     return NULL;
158
159   if (!_dbus_list_append (&parser->stack, e))
160     {
161       dbus_free (e);
162       return NULL;
163     }
164   
165   e->type = type;
166
167   return e;
168 }
169
170 static void
171 element_free (Element *e)
172 {
173
174   dbus_free (e);
175 }
176
177 static void
178 pop_element (BusConfigParser *parser)
179 {
180   Element *e;
181
182   e = _dbus_list_pop_last (&parser->stack);
183   
184   element_free (e);
185 }
186
187 static Element*
188 peek_element (BusConfigParser *parser)
189 {
190   Element *e;
191
192   e = _dbus_list_get_last (&parser->stack);
193
194   return e;
195 }
196
197 static ElementType
198 top_element_type (BusConfigParser *parser)
199 {
200   Element *e;
201
202   e = _dbus_list_get_last (&parser->stack);
203
204   if (e)
205     return e->type;
206   else
207     return ELEMENT_NONE;
208 }
209
210 static dbus_bool_t
211 merge_included (BusConfigParser *parser,
212                 BusConfigParser *included,
213                 DBusError       *error)
214 {
215   DBusList *link;
216
217   if (included->user != NULL)
218     {
219       dbus_free (parser->user);
220       parser->user = included->user;
221       included->user = NULL;
222     }
223
224   if (included->bus_type != NULL)
225     {
226       dbus_free (parser->bus_type);
227       parser->bus_type = included->bus_type;
228       included->bus_type = NULL;
229     }
230   
231   if (included->fork)
232     parser->fork = TRUE;
233
234   if (included->pidfile != NULL)
235     {
236       dbus_free (parser->pidfile);
237       parser->pidfile = included->pidfile;
238       included->pidfile = NULL;
239     }
240   
241   while ((link = _dbus_list_pop_first_link (&included->listen_on)))
242     _dbus_list_append_link (&parser->listen_on, link);
243
244   while ((link = _dbus_list_pop_first_link (&included->mechanisms)))
245     _dbus_list_append_link (&parser->mechanisms, link);
246
247   while ((link = _dbus_list_pop_first_link (&included->service_dirs)))
248     _dbus_list_append_link (&parser->service_dirs, link);
249   
250   return TRUE;
251 }
252
253 BusConfigParser*
254 bus_config_parser_new (const DBusString *basedir)
255 {
256   BusConfigParser *parser;
257
258   parser = dbus_new0 (BusConfigParser, 1);
259   if (parser == NULL)
260     return NULL;
261
262   if (!_dbus_string_init (&parser->basedir))
263     {
264       dbus_free (parser);
265       return NULL;
266     }
267
268   if (((parser->policy = bus_policy_new ()) == NULL) ||
269       !_dbus_string_copy (basedir, 0, &parser->basedir, 0))
270     {
271       if (parser->policy)
272         bus_policy_unref (parser->policy);
273       
274       _dbus_string_free (&parser->basedir);
275       dbus_free (parser);
276       return NULL;
277     }
278   
279   parser->refcount = 1;
280
281   return parser;
282 }
283
284 void
285 bus_config_parser_ref (BusConfigParser *parser)
286 {
287   _dbus_assert (parser->refcount > 0);
288
289   parser->refcount += 1;
290 }
291
292 void
293 bus_config_parser_unref (BusConfigParser *parser)
294 {
295   _dbus_assert (parser->refcount > 0);
296
297   parser->refcount -= 1;
298
299   if (parser->refcount == 0)
300     {
301       while (parser->stack != NULL)
302         pop_element (parser);
303
304       dbus_free (parser->user);
305       dbus_free (parser->bus_type);
306       dbus_free (parser->pidfile);
307       
308       _dbus_list_foreach (&parser->listen_on,
309                           (DBusForeachFunction) dbus_free,
310                           NULL);
311
312       _dbus_list_clear (&parser->listen_on);
313
314       _dbus_list_foreach (&parser->service_dirs,
315                           (DBusForeachFunction) dbus_free,
316                           NULL);
317
318       _dbus_list_clear (&parser->service_dirs);
319
320       _dbus_list_foreach (&parser->mechanisms,
321                           (DBusForeachFunction) dbus_free,
322                           NULL);
323
324       _dbus_list_clear (&parser->mechanisms);
325       
326       _dbus_string_free (&parser->basedir);
327
328       if (parser->policy)
329         bus_policy_unref (parser->policy);
330       
331       dbus_free (parser);
332     }
333 }
334
335 dbus_bool_t
336 bus_config_parser_check_doctype (BusConfigParser   *parser,
337                                  const char        *doctype,
338                                  DBusError         *error)
339 {
340   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
341
342   if (strcmp (doctype, "busconfig") != 0)
343     {
344       dbus_set_error (error,
345                       DBUS_ERROR_FAILED,
346                       "Configuration file has the wrong document type %s",
347                       doctype);
348       return FALSE;
349     }
350   else
351     return TRUE;
352 }
353
354 typedef struct
355 {
356   const char  *name;
357   const char **retloc;
358 } LocateAttr;
359
360 static dbus_bool_t
361 locate_attributes (BusConfigParser  *parser,
362                    const char       *element_name,
363                    const char      **attribute_names,
364                    const char      **attribute_values,
365                    DBusError        *error,
366                    const char       *first_attribute_name,
367                    const char      **first_attribute_retloc,
368                    ...)
369 {
370   va_list args;
371   const char *name;
372   const char **retloc;
373   int n_attrs;
374 #define MAX_ATTRS 24
375   LocateAttr attrs[MAX_ATTRS];
376   dbus_bool_t retval;
377   int i;
378
379   _dbus_assert (first_attribute_name != NULL);
380   _dbus_assert (first_attribute_retloc != NULL);
381
382   retval = TRUE;
383
384   n_attrs = 1;
385   attrs[0].name = first_attribute_name;
386   attrs[0].retloc = first_attribute_retloc;
387   *first_attribute_retloc = NULL;
388
389   va_start (args, first_attribute_retloc);
390
391   name = va_arg (args, const char*);
392   retloc = va_arg (args, const char**);
393
394   while (name != NULL)
395     {
396       _dbus_assert (retloc != NULL);
397       _dbus_assert (n_attrs < MAX_ATTRS);
398
399       attrs[n_attrs].name = name;
400       attrs[n_attrs].retloc = retloc;
401       n_attrs += 1;
402       *retloc = NULL;
403
404       name = va_arg (args, const char*);
405       retloc = va_arg (args, const char**);
406     }
407
408   va_end (args);
409
410   if (!retval)
411     return retval;
412
413   i = 0;
414   while (attribute_names[i])
415     {
416       int j;
417       dbus_bool_t found;
418       
419       found = FALSE;
420       j = 0;
421       while (j < n_attrs)
422         {
423           if (strcmp (attrs[j].name, attribute_names[i]) == 0)
424             {
425               retloc = attrs[j].retloc;
426
427               if (*retloc != NULL)
428                 {
429                   dbus_set_error (error, DBUS_ERROR_FAILED,
430                                   "Attribute \"%s\" repeated twice on the same <%s> element",
431                                   attrs[j].name, element_name);
432                   retval = FALSE;
433                   goto out;
434                 }
435
436               *retloc = attribute_values[i];
437               found = TRUE;
438             }
439
440           ++j;
441         }
442
443       if (!found)
444         {
445           dbus_set_error (error, DBUS_ERROR_FAILED,
446                           "Attribute \"%s\" is invalid on <%s> element in this context",
447                           attribute_names[i], element_name);
448           retval = FALSE;
449           goto out;
450         }
451
452       ++i;
453     }
454
455  out:
456   return retval;
457 }
458
459 static dbus_bool_t
460 check_no_attributes (BusConfigParser  *parser,
461                      const char       *element_name,
462                      const char      **attribute_names,
463                      const char      **attribute_values,
464                      DBusError        *error)
465 {
466   if (attribute_names[0] != NULL)
467     {
468       dbus_set_error (error, DBUS_ERROR_FAILED,
469                       "Attribute \"%s\" is invalid on <%s> element in this context",
470                       attribute_names[0], element_name);
471       return FALSE;
472     }
473
474   return TRUE;
475 }
476
477 static dbus_bool_t
478 start_busconfig_child (BusConfigParser   *parser,
479                        const char        *element_name,
480                        const char       **attribute_names,
481                        const char       **attribute_values,
482                        DBusError         *error)
483 {
484   if (strcmp (element_name, "user") == 0)
485     {
486       if (!check_no_attributes (parser, "user", attribute_names, attribute_values, error))
487         return FALSE;
488
489       if (push_element (parser, ELEMENT_USER) == NULL)
490         {
491           BUS_SET_OOM (error);
492           return FALSE;
493         }
494
495       return TRUE;
496     }
497   else if (strcmp (element_name, "type") == 0)
498     {
499       if (!check_no_attributes (parser, "type", attribute_names, attribute_values, error))
500         return FALSE;
501
502       if (push_element (parser, ELEMENT_TYPE) == NULL)
503         {
504           BUS_SET_OOM (error);
505           return FALSE;
506         }
507
508       return TRUE;
509     }
510   else if (strcmp (element_name, "fork") == 0)
511     {
512       if (!check_no_attributes (parser, "fork", attribute_names, attribute_values, error))
513         return FALSE;
514
515       if (push_element (parser, ELEMENT_FORK) == NULL)
516         {
517           BUS_SET_OOM (error);
518           return FALSE;
519         }
520
521       parser->fork = TRUE;
522       
523       return TRUE;
524     }
525   else if (strcmp (element_name, "pidfile") == 0)
526     {
527       if (!check_no_attributes (parser, "pidfile", attribute_names, attribute_values, error))
528         return FALSE;
529
530       if (push_element (parser, ELEMENT_PIDFILE) == NULL)
531         {
532           BUS_SET_OOM (error);
533           return FALSE;
534         }
535
536       return TRUE;
537     }
538   else if (strcmp (element_name, "listen") == 0)
539     {
540       if (!check_no_attributes (parser, "listen", attribute_names, attribute_values, error))
541         return FALSE;
542
543       if (push_element (parser, ELEMENT_LISTEN) == NULL)
544         {
545           BUS_SET_OOM (error);
546           return FALSE;
547         }
548
549       return TRUE;
550     }
551   else if (strcmp (element_name, "auth") == 0)
552     {
553       if (!check_no_attributes (parser, "auth", attribute_names, attribute_values, error))
554         return FALSE;
555
556       if (push_element (parser, ELEMENT_AUTH) == NULL)
557         {
558           BUS_SET_OOM (error);
559           return FALSE;
560         }
561
562       return TRUE;
563     }
564   else if (strcmp (element_name, "includedir") == 0)
565     {
566       if (!check_no_attributes (parser, "includedir", attribute_names, attribute_values, error))
567         return FALSE;
568
569       if (push_element (parser, ELEMENT_INCLUDEDIR) == NULL)
570         {
571           BUS_SET_OOM (error);
572           return FALSE;
573         }
574
575       return TRUE;
576     }
577   else if (strcmp (element_name, "servicedir") == 0)
578     {
579       if (!check_no_attributes (parser, "servicedir", attribute_names, attribute_values, error))
580         return FALSE;
581
582       if (push_element (parser, ELEMENT_SERVICEDIR) == NULL)
583         {
584           BUS_SET_OOM (error);
585           return FALSE;
586         }
587
588       return TRUE;
589     }
590   else if (strcmp (element_name, "include") == 0)
591     {
592       Element *e;
593       const char *ignore_missing;
594
595       if ((e = push_element (parser, ELEMENT_INCLUDE)) == NULL)
596         {
597           BUS_SET_OOM (error);
598           return FALSE;
599         }
600
601       e->d.include.ignore_missing = FALSE;
602
603       if (!locate_attributes (parser, "include",
604                               attribute_names,
605                               attribute_values,
606                               error,
607                               "ignore_missing", &ignore_missing,
608                               NULL))
609         return FALSE;
610
611       if (ignore_missing != NULL)
612         {
613           if (strcmp (ignore_missing, "yes") == 0)
614             e->d.include.ignore_missing = TRUE;
615           else if (strcmp (ignore_missing, "no") == 0)
616             e->d.include.ignore_missing = FALSE;
617           else
618             {
619               dbus_set_error (error, DBUS_ERROR_FAILED,
620                               "ignore_missing attribute must have value \"yes\" or \"no\"");
621               return FALSE;
622             }
623         }
624
625       return TRUE;
626     }
627   else if (strcmp (element_name, "policy") == 0)
628     {
629       Element *e;
630       const char *context;
631       const char *user;
632       const char *group;
633
634       if ((e = push_element (parser, ELEMENT_POLICY)) == NULL)
635         {
636           BUS_SET_OOM (error);
637           return FALSE;
638         }
639
640       if (!locate_attributes (parser, "policy",
641                               attribute_names,
642                               attribute_values,
643                               error,
644                               "context", &context,
645                               "user", &user,
646                               "group", &group,
647                               NULL))
648         return FALSE;
649
650       if (((context && user) ||
651            (context && group)) ||
652           (user && group) ||
653           !(context || user || group))
654         {
655           dbus_set_error (error, DBUS_ERROR_FAILED,
656                           "<policy> element must have exactly one of (context|user|group) attributes");
657           return FALSE;
658         }
659
660       if (context != NULL)
661         {
662           if (strcmp (context, "default") == 0)
663             {
664
665             }
666           else if (strcmp (context, "mandatory") == 0)
667             {
668
669             }
670           else
671             {
672               dbus_set_error (error, DBUS_ERROR_FAILED,
673                               "context attribute on <policy> must have the value \"default\" or \"mandatory\", not \"%s\"",
674                               context);
675               return FALSE;
676             }
677           
678           /* FIXME */
679
680         }
681       else if (user != NULL)
682         {
683           /* FIXME */
684
685         }
686       else if (group != NULL)
687         {
688           /* FIXME */
689
690         }
691       else
692         {
693           _dbus_assert_not_reached ("all <policy> attributes null and we didn't set error");
694         }
695       
696       return TRUE;
697     }
698   else
699     {
700       dbus_set_error (error, DBUS_ERROR_FAILED,
701                       "Element <%s> not allowed inside <%s> in configuration file",
702                       element_name, "busconfig");
703       return FALSE;
704     }
705 }
706
707 static dbus_bool_t
708 start_policy_child (BusConfigParser   *parser,
709                     const char        *element_name,
710                     const char       **attribute_names,
711                     const char       **attribute_values,
712                     DBusError         *error)
713 {
714   if (strcmp (element_name, "allow") == 0)
715     {
716       if (push_element (parser, ELEMENT_ALLOW) == NULL)
717         {
718           BUS_SET_OOM (error);
719           return FALSE;
720         }
721       
722       return TRUE;
723     }
724   else if (strcmp (element_name, "deny") == 0)
725     {
726       if (push_element (parser, ELEMENT_DENY) == NULL)
727         {
728           BUS_SET_OOM (error);
729           return FALSE;
730         }
731       
732       return TRUE;
733     }
734   else
735     {
736       dbus_set_error (error, DBUS_ERROR_FAILED,
737                       "Element <%s> not allowed inside <%s> in configuration file",
738                       element_name, "policy");
739       return FALSE;
740     }
741 }
742
743 dbus_bool_t
744 bus_config_parser_start_element (BusConfigParser   *parser,
745                                  const char        *element_name,
746                                  const char       **attribute_names,
747                                  const char       **attribute_values,
748                                  DBusError         *error)
749 {
750   ElementType t;
751
752   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
753
754   /* printf ("START: %s\n", element_name); */
755   
756   t = top_element_type (parser);
757
758   if (t == ELEMENT_NONE)
759     {
760       if (strcmp (element_name, "busconfig") == 0)
761         {
762           if (!check_no_attributes (parser, "busconfig", attribute_names, attribute_values, error))
763             return FALSE;
764           
765           if (push_element (parser, ELEMENT_BUSCONFIG) == NULL)
766             {
767               BUS_SET_OOM (error);
768               return FALSE;
769             }
770
771           return TRUE;
772         }
773       else
774         {
775           dbus_set_error (error, DBUS_ERROR_FAILED,
776                           "Unknown element <%s> at root of configuration file",
777                           element_name);
778           return FALSE;
779         }
780     }
781   else if (t == ELEMENT_BUSCONFIG)
782     {
783       return start_busconfig_child (parser, element_name,
784                                     attribute_names, attribute_values,
785                                     error);
786     }
787   else if (t == ELEMENT_POLICY)
788     {
789       return start_policy_child (parser, element_name,
790                                  attribute_names, attribute_values,
791                                  error);
792     }
793   else
794     {
795       dbus_set_error (error, DBUS_ERROR_FAILED,
796                       "Element <%s> is not allowed in this context",
797                       element_name);
798       return FALSE;
799     }  
800 }
801
802 dbus_bool_t
803 bus_config_parser_end_element (BusConfigParser   *parser,
804                                const char        *element_name,
805                                DBusError         *error)
806 {
807   ElementType t;
808   const char *n;
809   Element *e;
810
811   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
812
813   /* printf ("END: %s\n", element_name); */
814   
815   t = top_element_type (parser);
816
817   if (t == ELEMENT_NONE)
818     {
819       /* should probably be an assertion failure but
820        * being paranoid about XML parsers
821        */
822       dbus_set_error (error, DBUS_ERROR_FAILED,
823                       "XML parser ended element with no element on the stack");
824       return FALSE;
825     }
826
827   n = element_type_to_name (t);
828   _dbus_assert (n != NULL);
829   if (strcmp (n, element_name) != 0)
830     {
831       /* should probably be an assertion failure but
832        * being paranoid about XML parsers
833        */
834       dbus_set_error (error, DBUS_ERROR_FAILED,
835                       "XML element <%s> ended but topmost element on the stack was <%s>",
836                       element_name, n);
837       return FALSE;
838     }
839
840   e = peek_element (parser);
841   _dbus_assert (e != NULL);
842
843   switch (e->type)
844     {
845     case ELEMENT_NONE:
846       _dbus_assert_not_reached ("element in stack has no type");
847       break;
848
849     case ELEMENT_INCLUDE:
850     case ELEMENT_USER:
851     case ELEMENT_TYPE:
852     case ELEMENT_LISTEN:
853     case ELEMENT_PIDFILE:
854     case ELEMENT_AUTH:
855     case ELEMENT_SERVICEDIR:
856     case ELEMENT_INCLUDEDIR:
857       if (!e->had_content)
858         {
859           dbus_set_error (error, DBUS_ERROR_FAILED,
860                           "XML element <%s> was expected to have content inside it",
861                           element_type_to_name (e->type));
862           return FALSE;
863         }
864       break;
865
866     case ELEMENT_BUSCONFIG:
867     case ELEMENT_POLICY:
868     case ELEMENT_LIMIT:
869     case ELEMENT_ALLOW:
870     case ELEMENT_DENY:
871     case ELEMENT_FORK:
872       break;
873     }
874
875   pop_element (parser);
876
877   return TRUE;
878 }
879
880 static dbus_bool_t
881 all_whitespace (const DBusString *str)
882 {
883   int i;
884
885   _dbus_string_skip_white (str, 0, &i);
886
887   return i == _dbus_string_get_length (str);
888 }
889
890 static dbus_bool_t
891 make_full_path (const DBusString *basedir,
892                 const DBusString *filename,
893                 DBusString       *full_path)
894 {
895   if (_dbus_path_is_absolute (filename))
896     {
897       return _dbus_string_copy (filename, 0, full_path, 0);
898     }
899   else
900     {
901       if (!_dbus_string_copy (basedir, 0, full_path, 0))
902         return FALSE;
903       
904       if (!_dbus_concat_dir_and_file (full_path, filename))
905         return FALSE;
906
907       return TRUE;
908     }
909 }
910
911 static dbus_bool_t
912 include_file (BusConfigParser   *parser,
913               const DBusString  *filename,
914               dbus_bool_t        ignore_missing,
915               DBusError         *error)
916 {
917   /* FIXME good test case for this would load each config file in the
918    * test suite both alone, and as an include, and check
919    * that the result is the same
920    */
921   BusConfigParser *included;
922   DBusError tmp_error;
923         
924   dbus_error_init (&tmp_error);
925   included = bus_config_load (filename, &tmp_error);
926   if (included == NULL)
927     {
928       _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
929
930       if (dbus_error_has_name (&tmp_error, DBUS_ERROR_FILE_NOT_FOUND) &&
931           ignore_missing)
932         {
933           dbus_error_free (&tmp_error);
934           return TRUE;
935         }
936       else
937         {
938           dbus_move_error (&tmp_error, error);
939           return FALSE;
940         }
941     }
942   else
943     {
944       _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
945
946       if (!merge_included (parser, included, error))
947         {
948           bus_config_parser_unref (included);
949           return FALSE;
950         }
951
952       bus_config_parser_unref (included);
953       return TRUE;
954     }
955 }
956
957 static dbus_bool_t
958 include_dir (BusConfigParser   *parser,
959              const DBusString  *dirname,
960              DBusError         *error)
961 {
962   DBusString filename;
963   dbus_bool_t retval;
964   DBusError tmp_error;
965   DBusDirIter *dir;
966   
967   if (!_dbus_string_init (&filename))
968     {
969       BUS_SET_OOM (error);
970       return FALSE;
971     }
972
973   retval = FALSE;
974   
975   dir = _dbus_directory_open (dirname, error);
976
977   if (dir == NULL)
978     goto failed;
979
980   dbus_error_init (&tmp_error);
981   while (_dbus_directory_get_next_file (dir, &filename, &tmp_error))
982     {
983       DBusString full_path;
984
985       if (!_dbus_string_init (&full_path))
986         {
987           BUS_SET_OOM (error);
988           goto failed;
989         }
990
991       if (!_dbus_string_copy (dirname, 0, &full_path, 0))
992         {
993           BUS_SET_OOM (error);
994           _dbus_string_free (&full_path);
995           goto failed;
996         }      
997
998       if (!_dbus_concat_dir_and_file (&full_path, &filename))
999         {
1000           BUS_SET_OOM (error);
1001           _dbus_string_free (&full_path);
1002           goto failed;
1003         }
1004       
1005       if (_dbus_string_ends_with_c_str (&full_path, ".conf"))
1006         {
1007           if (!include_file (parser, &full_path, TRUE, error))
1008             {
1009               _dbus_string_free (&full_path);
1010               goto failed;
1011             }
1012         }
1013
1014       _dbus_string_free (&full_path);
1015     }
1016
1017   if (dbus_error_is_set (&tmp_error))
1018     {
1019       dbus_move_error (&tmp_error, error);
1020       goto failed;
1021     }
1022   
1023   retval = TRUE;
1024   
1025  failed:
1026   _dbus_string_free (&filename);
1027   
1028   if (dir)
1029     _dbus_directory_close (dir);
1030
1031   return retval;
1032 }
1033
1034 dbus_bool_t
1035 bus_config_parser_content (BusConfigParser   *parser,
1036                            const DBusString  *content,
1037                            DBusError         *error)
1038 {
1039   Element *e;
1040
1041   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1042
1043 #if 0
1044   {
1045     const char *c_str;
1046     
1047     _dbus_string_get_const_data (content, &c_str);
1048
1049     printf ("CONTENT %d bytes: %s\n", _dbus_string_get_length (content), c_str);
1050   }
1051 #endif
1052   
1053   e = peek_element (parser);
1054   if (e == NULL)
1055     {
1056       dbus_set_error (error, DBUS_ERROR_FAILED,
1057                       "Text content outside of any XML element in configuration file");
1058       return FALSE;
1059     }
1060   else if (e->had_content)
1061     {
1062       _dbus_assert_not_reached ("Element had multiple content blocks");
1063       return FALSE;
1064     }
1065
1066   switch (top_element_type (parser))
1067     {
1068     case ELEMENT_NONE:
1069       _dbus_assert_not_reached ("element at top of stack has no type");
1070       return FALSE;
1071
1072     case ELEMENT_BUSCONFIG:
1073     case ELEMENT_POLICY:
1074     case ELEMENT_LIMIT:
1075     case ELEMENT_ALLOW:
1076     case ELEMENT_DENY:
1077     case ELEMENT_FORK:
1078       if (all_whitespace (content))
1079         return TRUE;
1080       else
1081         {
1082           dbus_set_error (error, DBUS_ERROR_FAILED,
1083                           "No text content expected inside XML element %s in configuration file",
1084                           element_type_to_name (top_element_type (parser)));
1085           return FALSE;
1086         }
1087
1088     case ELEMENT_PIDFILE:
1089       {
1090         char *s;
1091
1092         e->had_content = TRUE;
1093         
1094         if (!_dbus_string_copy_data (content, &s))
1095           goto nomem;
1096           
1097         dbus_free (parser->pidfile);
1098         parser->pidfile = s;
1099       }
1100       break;
1101
1102     case ELEMENT_INCLUDE:
1103       {
1104         DBusString full_path;
1105         
1106         e->had_content = TRUE;
1107
1108         if (!_dbus_string_init (&full_path))
1109           goto nomem;
1110         
1111         if (!make_full_path (&parser->basedir, content, &full_path))
1112           {
1113             _dbus_string_free (&full_path);
1114             goto nomem;
1115           }
1116         
1117         if (!include_file (parser, &full_path,
1118                            e->d.include.ignore_missing, error))
1119           {
1120             _dbus_string_free (&full_path);
1121             return FALSE;
1122           }
1123
1124         _dbus_string_free (&full_path);
1125       }
1126       break;
1127
1128     case ELEMENT_INCLUDEDIR:
1129       {
1130         DBusString full_path;
1131         
1132         e->had_content = TRUE;
1133
1134         if (!_dbus_string_init (&full_path))
1135           goto nomem;
1136         
1137         if (!make_full_path (&parser->basedir, content, &full_path))
1138           {
1139             _dbus_string_free (&full_path);
1140             goto nomem;
1141           }
1142         
1143         if (!include_dir (parser, &full_path, error))
1144           {
1145             _dbus_string_free (&full_path);
1146             return FALSE;
1147           }
1148
1149         _dbus_string_free (&full_path);
1150       }
1151       break;
1152       
1153     case ELEMENT_USER:
1154       {
1155         char *s;
1156
1157         e->had_content = TRUE;
1158         
1159         if (!_dbus_string_copy_data (content, &s))
1160           goto nomem;
1161           
1162         dbus_free (parser->user);
1163         parser->user = s;
1164       }
1165       break;
1166
1167     case ELEMENT_TYPE:
1168       {
1169         char *s;
1170
1171         e->had_content = TRUE;
1172
1173         if (!_dbus_string_copy_data (content, &s))
1174           goto nomem;
1175         
1176         dbus_free (parser->bus_type);
1177         parser->bus_type = s;
1178       }
1179       break;
1180       
1181     case ELEMENT_LISTEN:
1182       {
1183         char *s;
1184
1185         e->had_content = TRUE;
1186         
1187         if (!_dbus_string_copy_data (content, &s))
1188           goto nomem;
1189
1190         if (!_dbus_list_append (&parser->listen_on,
1191                                 s))
1192           {
1193             dbus_free (s);
1194             goto nomem;
1195           }
1196       }
1197       break;
1198
1199     case ELEMENT_AUTH:
1200       {
1201         char *s;
1202         
1203         e->had_content = TRUE;
1204
1205         if (!_dbus_string_copy_data (content, &s))
1206           goto nomem;
1207
1208         if (!_dbus_list_append (&parser->mechanisms,
1209                                 s))
1210           {
1211             dbus_free (s);
1212             goto nomem;
1213           }
1214       }
1215       break;
1216
1217     case ELEMENT_SERVICEDIR:
1218       {
1219         char *s;
1220         DBusString full_path;
1221         
1222         e->had_content = TRUE;
1223
1224         if (!_dbus_string_init (&full_path))
1225           goto nomem;
1226         
1227         if (!make_full_path (&parser->basedir, content, &full_path))
1228           {
1229             _dbus_string_free (&full_path);
1230             goto nomem;
1231           }
1232         
1233         if (!_dbus_string_copy_data (&full_path, &s))
1234           {
1235             _dbus_string_free (&full_path);
1236             goto nomem;
1237           }
1238
1239         if (!_dbus_list_append (&parser->service_dirs, s))
1240           {
1241             _dbus_string_free (&full_path);
1242             dbus_free (s);
1243             goto nomem;
1244           }
1245
1246         _dbus_string_free (&full_path);
1247       }
1248       break;
1249     }
1250
1251   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1252   return TRUE;
1253
1254  nomem:
1255   BUS_SET_OOM (error);
1256   return FALSE;
1257 }
1258
1259 dbus_bool_t
1260 bus_config_parser_finished (BusConfigParser   *parser,
1261                             DBusError         *error)
1262 {
1263   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1264
1265   if (parser->stack != NULL)
1266     {
1267       dbus_set_error (error, DBUS_ERROR_FAILED,
1268                       "Element <%s> was not closed in configuration file",
1269                       element_type_to_name (top_element_type (parser)));
1270
1271       return FALSE;
1272     }
1273
1274   if (parser->listen_on == NULL)
1275     {
1276       dbus_set_error (error, DBUS_ERROR_FAILED,
1277                       "Configuration file needs one or more <listen> elements giving addresses"); 
1278       return FALSE;
1279     }
1280   
1281   return TRUE;
1282 }
1283
1284 const char*
1285 bus_config_parser_get_user (BusConfigParser *parser)
1286 {
1287   return parser->user;
1288 }
1289
1290 const char*
1291 bus_config_parser_get_type (BusConfigParser *parser)
1292 {
1293   return parser->bus_type;
1294 }
1295
1296 DBusList**
1297 bus_config_parser_get_addresses (BusConfigParser *parser)
1298 {
1299   return &parser->listen_on;
1300 }
1301
1302 DBusList**
1303 bus_config_parser_get_mechanisms (BusConfigParser *parser)
1304 {
1305   return &parser->mechanisms;
1306 }
1307
1308 DBusList**
1309 bus_config_parser_get_service_dirs (BusConfigParser *parser)
1310 {
1311   return &parser->service_dirs;
1312 }
1313
1314 dbus_bool_t
1315 bus_config_parser_get_fork (BusConfigParser   *parser)
1316 {
1317   return parser->fork;
1318 }
1319
1320 const char *
1321 bus_config_parser_get_pidfile (BusConfigParser   *parser)
1322 {
1323   return parser->pidfile;
1324 }
1325
1326 BusPolicy*
1327 bus_config_parser_steal_policy (BusConfigParser *parser)
1328 {
1329   BusPolicy *policy;
1330
1331   _dbus_assert (parser->policy != NULL); /* can only steal the policy 1 time */
1332   
1333   policy = parser->policy;
1334
1335   parser->policy = NULL;
1336
1337   return policy;
1338 }
1339
1340 #ifdef DBUS_BUILD_TESTS
1341 #include <stdio.h>
1342
1343 typedef enum
1344 {
1345   VALID,
1346   INVALID,
1347   UNKNOWN
1348 } Validity;
1349
1350 static dbus_bool_t
1351 do_load (const DBusString *full_path,
1352          Validity          validity,
1353          dbus_bool_t       oom_possible)
1354 {
1355   BusConfigParser *parser;
1356   DBusError error;
1357
1358   dbus_error_init (&error);
1359
1360   parser = bus_config_load (full_path, &error);
1361   if (parser == NULL)
1362     {
1363       _DBUS_ASSERT_ERROR_IS_SET (&error);
1364
1365       if (oom_possible &&
1366           dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1367         {
1368           _dbus_verbose ("Failed to load valid file due to OOM\n");
1369           dbus_error_free (&error);
1370           return TRUE;
1371         }
1372       else if (validity == VALID)
1373         {
1374           _dbus_warn ("Failed to load valid file but still had memory: %s\n",
1375                       error.message);
1376
1377           dbus_error_free (&error);
1378           return FALSE;
1379         }
1380       else
1381         {
1382           dbus_error_free (&error);
1383           return TRUE;
1384         }
1385     }
1386   else
1387     {
1388       _DBUS_ASSERT_ERROR_IS_CLEAR (&error);
1389
1390       bus_config_parser_unref (parser);
1391
1392       if (validity == INVALID)
1393         {
1394           _dbus_warn ("Accepted invalid file\n");
1395           return FALSE;
1396         }
1397
1398       return TRUE;
1399     }
1400 }
1401
1402 typedef struct
1403 {
1404   const DBusString *full_path;
1405   Validity          validity;
1406 } LoaderOomData;
1407
1408 static dbus_bool_t
1409 check_loader_oom_func (void *data)
1410 {
1411   LoaderOomData *d = data;
1412
1413   return do_load (d->full_path, d->validity, TRUE);
1414 }
1415
1416 static dbus_bool_t
1417 process_test_subdir (const DBusString *test_base_dir,
1418                      const char       *subdir,
1419                      Validity          validity)
1420 {
1421   DBusString test_directory;
1422   DBusString filename;
1423   DBusDirIter *dir;
1424   dbus_bool_t retval;
1425   DBusError error;
1426
1427   retval = FALSE;
1428   dir = NULL;
1429
1430   if (!_dbus_string_init (&test_directory))
1431     _dbus_assert_not_reached ("didn't allocate test_directory\n");
1432
1433   _dbus_string_init_const (&filename, subdir);
1434
1435   if (!_dbus_string_copy (test_base_dir, 0,
1436                           &test_directory, 0))
1437     _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
1438
1439   if (!_dbus_concat_dir_and_file (&test_directory, &filename))
1440     _dbus_assert_not_reached ("couldn't allocate full path");
1441
1442   _dbus_string_free (&filename);
1443   if (!_dbus_string_init (&filename))
1444     _dbus_assert_not_reached ("didn't allocate filename string\n");
1445
1446   dbus_error_init (&error);
1447   dir = _dbus_directory_open (&test_directory, &error);
1448   if (dir == NULL)
1449     {
1450       _dbus_warn ("Could not open %s: %s\n",
1451                   _dbus_string_get_const_data (&test_directory),
1452                   error.message);
1453       dbus_error_free (&error);
1454       goto failed;
1455     }
1456
1457   printf ("Testing:\n");
1458
1459  next:
1460   while (_dbus_directory_get_next_file (dir, &filename, &error))
1461     {
1462       DBusString full_path;
1463       LoaderOomData d;
1464
1465       if (!_dbus_string_init (&full_path))
1466         _dbus_assert_not_reached ("couldn't init string");
1467
1468       if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
1469         _dbus_assert_not_reached ("couldn't copy dir to full_path");
1470
1471       if (!_dbus_concat_dir_and_file (&full_path, &filename))
1472         _dbus_assert_not_reached ("couldn't concat file to dir");
1473
1474       if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
1475         {
1476           _dbus_verbose ("Skipping non-.conf file %s\n",
1477                          _dbus_string_get_const_data (&filename));
1478           _dbus_string_free (&full_path);
1479           goto next;
1480         }
1481
1482       printf ("    %s\n", _dbus_string_get_const_data (&filename));
1483
1484       _dbus_verbose (" expecting %s\n",
1485                      validity == VALID ? "valid" :
1486                      (validity == INVALID ? "invalid" :
1487                       (validity == UNKNOWN ? "unknown" : "???")));
1488
1489       d.full_path = &full_path;
1490       d.validity = validity;
1491       if (!_dbus_test_oom_handling ("config-loader", check_loader_oom_func, &d))
1492         _dbus_assert_not_reached ("test failed");
1493
1494       _dbus_string_free (&full_path);
1495     }
1496
1497   if (dbus_error_is_set (&error))
1498     {
1499       _dbus_warn ("Could not get next file in %s: %s\n",
1500                   _dbus_string_get_const_data (&test_directory),
1501                   error.message);
1502       dbus_error_free (&error);
1503       goto failed;
1504     }
1505
1506   retval = TRUE;
1507
1508  failed:
1509
1510   if (dir)
1511     _dbus_directory_close (dir);
1512   _dbus_string_free (&test_directory);
1513   _dbus_string_free (&filename);
1514
1515   return retval;
1516 }
1517
1518 dbus_bool_t
1519 bus_config_parser_test (const DBusString *test_data_dir)
1520 {
1521   if (test_data_dir == NULL ||
1522       _dbus_string_get_length (test_data_dir) == 0)
1523     {
1524       printf ("No test data\n");
1525       return TRUE;
1526     }
1527
1528   if (!process_test_subdir (test_data_dir, "valid-config-files", VALID))
1529     return FALSE;
1530
1531   return TRUE;
1532 }
1533
1534 #endif /* DBUS_BUILD_TESTS */
1535