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