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