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