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