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*
push_element(BusConfigParser * parser,ElementType type)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
element_free(Element * e)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
pop_element(BusConfigParser * parser)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*
peek_element(BusConfigParser * parser)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
top_element_type(BusConfigParser * parser)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
merge_service_context_hash(DBusHashTable * dest,DBusHashTable * from)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 *
bus_config_service_dir_new_take(char * path,BusServiceDirFlags flags)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
bus_config_service_dir_free(BusConfigServiceDir * self)255 bus_config_service_dir_free (BusConfigServiceDir *self)
256 {
257 dbus_free (self->path);
258 dbus_free (self);
259 }
260
261 static BusConfigServiceDir *
service_dirs_find_dir(DBusList ** service_dirs,const char * dir)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
service_dirs_append_link_unique_or_free(DBusList ** service_dirs,DBusList * dir_link)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
service_dirs_absorb_string_list(DBusList ** service_dirs,DBusList ** dirs,BusServiceDirFlags flags)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
merge_included(BusConfigParser * parser,BusConfigParser * included,DBusError * error)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
seen_include(BusConfigParser * parser,const DBusString * file)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*
bus_config_parser_new(const DBusString * basedir,dbus_bool_t is_toplevel,const BusConfigParser * parent)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 *
bus_config_parser_ref(BusConfigParser * parser)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
bus_config_parser_unref(BusConfigParser * parser)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
bus_config_parser_check_doctype(BusConfigParser * parser,const char * doctype,DBusError * error)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
locate_attributes(BusConfigParser * parser,const char * element_name,const char ** attribute_names,const char ** attribute_values,DBusError * error,const char * first_attribute_name,const char ** first_attribute_retloc,...)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
check_no_attributes(BusConfigParser * parser,const char * element_name,const char ** attribute_names,const char ** attribute_values,DBusError * error)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
start_busconfig_child(BusConfigParser * parser,const char * element_name,const char ** attribute_names,const char ** attribute_values,DBusError * error)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
parse_int_attribute(const char * name,const char * content,long min,long max,long def,long * value,DBusError * error)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
append_rule_from_element(BusConfigParser * parser,const char * element_name,const char ** attribute_names,const char ** attribute_values,dbus_bool_t allow,DBusError * error)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_destination != NULL &&
1558 send_broadcast != NULL &&
1559 strcmp (send_broadcast, "true") == 0)
1560 {
1561 /* Broadcast messages have no destination, so this cannot
1562 * possibly match */
1563 dbus_set_error (error, DBUS_ERROR_FAILED,
1564 "Rule with send_broadcast=\"true\" and "
1565 "send_destination=\"%s\" cannot match anything",
1566 send_destination);
1567 return FALSE;
1568 }
1569
1570 if (send_requested_reply &&
1571 !(strcmp (send_requested_reply, "true") == 0 ||
1572 strcmp (send_requested_reply, "false") == 0))
1573 {
1574 dbus_set_error (error, DBUS_ERROR_FAILED,
1575 "Bad value \"%s\" for %s attribute, must be true or false",
1576 "send_requested_reply", send_requested_reply);
1577 return FALSE;
1578 }
1579
1580 /* Matching only messages with DBUS_MAXIMUM_MESSAGE_UNIX_FDS or fewer
1581 * fds is the same as matching all messages, so we always set a maximum,
1582 * but perhaps an unrealistically high one. */
1583 if (!parse_int_attribute ("max_fds", max_fds_attr,
1584 0, DBUS_MAXIMUM_MESSAGE_UNIX_FDS,
1585 DBUS_MAXIMUM_MESSAGE_UNIX_FDS, &max_fds,
1586 error) ||
1587 !parse_int_attribute ("min_fds", min_fds_attr,
1588 0, DBUS_MAXIMUM_MESSAGE_UNIX_FDS, 0, &min_fds,
1589 error))
1590 return FALSE;
1591
1592 rule = bus_policy_rule_new (BUS_POLICY_RULE_SEND, allow);
1593 if (rule == NULL)
1594 goto nomem;
1595
1596 if (eavesdrop)
1597 rule->d.send.eavesdrop = (strcmp (eavesdrop, "true") == 0);
1598
1599 if (log)
1600 rule->d.send.log = (strcmp (log, "true") == 0);
1601
1602 if (send_requested_reply)
1603 rule->d.send.requested_reply = (strcmp (send_requested_reply, "true") == 0);
1604
1605 if (send_broadcast)
1606 {
1607 if (strcmp (send_broadcast, "true") == 0)
1608 rule->d.send.broadcast = BUS_POLICY_TRISTATE_TRUE;
1609 else
1610 rule->d.send.broadcast = BUS_POLICY_TRISTATE_FALSE;
1611 }
1612 else
1613 {
1614 rule->d.send.broadcast = BUS_POLICY_TRISTATE_ANY;
1615 }
1616
1617 rule->d.send.message_type = message_type;
1618 rule->d.send.path = _dbus_strdup (send_path);
1619 rule->d.send.interface = _dbus_strdup (send_interface);
1620 rule->d.send.member = _dbus_strdup (send_member);
1621 rule->d.send.error = _dbus_strdup (send_error);
1622 rule->d.send.destination = _dbus_strdup (send_destination);
1623 rule->d.send.max_fds = max_fds;
1624 rule->d.send.min_fds = min_fds;
1625
1626 if (send_path && rule->d.send.path == NULL)
1627 goto nomem;
1628 if (send_interface && rule->d.send.interface == NULL)
1629 goto nomem;
1630 if (send_member && rule->d.send.member == NULL)
1631 goto nomem;
1632 if (send_error && rule->d.send.error == NULL)
1633 goto nomem;
1634 if (send_destination && rule->d.send.destination == NULL)
1635 goto nomem;
1636 }
1637 else if (any_receive_attribute)
1638 {
1639 int message_type;
1640
1641 if (IS_WILDCARD (receive_interface))
1642 receive_interface = NULL;
1643 if (IS_WILDCARD (receive_member))
1644 receive_member = NULL;
1645 if (IS_WILDCARD (receive_error))
1646 receive_error = NULL;
1647 if (IS_WILDCARD (receive_sender))
1648 receive_sender = NULL;
1649 if (IS_WILDCARD (receive_path))
1650 receive_path = NULL;
1651 if (IS_WILDCARD (receive_type))
1652 receive_type = NULL;
1653
1654 message_type = DBUS_MESSAGE_TYPE_INVALID;
1655 if (receive_type != NULL)
1656 {
1657 message_type = dbus_message_type_from_string (receive_type);
1658 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1659 {
1660 dbus_set_error (error, DBUS_ERROR_FAILED,
1661 "Bad message type \"%s\"",
1662 receive_type);
1663 return FALSE;
1664 }
1665 }
1666
1667
1668 if (eavesdrop &&
1669 !(strcmp (eavesdrop, "true") == 0 ||
1670 strcmp (eavesdrop, "false") == 0))
1671 {
1672 dbus_set_error (error, DBUS_ERROR_FAILED,
1673 "Bad value \"%s\" for %s attribute, must be true or false",
1674 "eavesdrop", eavesdrop);
1675 return FALSE;
1676 }
1677
1678 if (receive_requested_reply &&
1679 !(strcmp (receive_requested_reply, "true") == 0 ||
1680 strcmp (receive_requested_reply, "false") == 0))
1681 {
1682 dbus_set_error (error, DBUS_ERROR_FAILED,
1683 "Bad value \"%s\" for %s attribute, must be true or false",
1684 "receive_requested_reply", receive_requested_reply);
1685 return FALSE;
1686 }
1687
1688 if (!parse_int_attribute ("max_fds", max_fds_attr,
1689 0, DBUS_MAXIMUM_MESSAGE_UNIX_FDS,
1690 DBUS_MAXIMUM_MESSAGE_UNIX_FDS, &max_fds,
1691 error) ||
1692 !parse_int_attribute ("min_fds", min_fds_attr,
1693 0, DBUS_MAXIMUM_MESSAGE_UNIX_FDS, 0, &min_fds,
1694 error))
1695 return FALSE;
1696
1697 rule = bus_policy_rule_new (BUS_POLICY_RULE_RECEIVE, allow);
1698 if (rule == NULL)
1699 goto nomem;
1700
1701 if (eavesdrop)
1702 rule->d.receive.eavesdrop = (strcmp (eavesdrop, "true") == 0);
1703
1704 if (receive_requested_reply)
1705 rule->d.receive.requested_reply = (strcmp (receive_requested_reply, "true") == 0);
1706
1707 rule->d.receive.message_type = message_type;
1708 rule->d.receive.path = _dbus_strdup (receive_path);
1709 rule->d.receive.interface = _dbus_strdup (receive_interface);
1710 rule->d.receive.member = _dbus_strdup (receive_member);
1711 rule->d.receive.error = _dbus_strdup (receive_error);
1712 rule->d.receive.origin = _dbus_strdup (receive_sender);
1713 rule->d.receive.max_fds = max_fds;
1714 rule->d.receive.min_fds = min_fds;
1715
1716 if (receive_path && rule->d.receive.path == NULL)
1717 goto nomem;
1718 if (receive_interface && rule->d.receive.interface == NULL)
1719 goto nomem;
1720 if (receive_member && rule->d.receive.member == NULL)
1721 goto nomem;
1722 if (receive_error && rule->d.receive.error == NULL)
1723 goto nomem;
1724 if (receive_sender && rule->d.receive.origin == NULL)
1725 goto nomem;
1726 }
1727 else if (own || own_prefix)
1728 {
1729 rule = bus_policy_rule_new (BUS_POLICY_RULE_OWN, allow);
1730 if (rule == NULL)
1731 goto nomem;
1732
1733 if (own)
1734 {
1735 if (IS_WILDCARD (own))
1736 own = NULL;
1737
1738 rule->d.own.prefix = 0;
1739 rule->d.own.service_name = _dbus_strdup (own);
1740 if (own && rule->d.own.service_name == NULL)
1741 goto nomem;
1742 }
1743 else
1744 {
1745 rule->d.own.prefix = 1;
1746 rule->d.own.service_name = _dbus_strdup (own_prefix);
1747 if (rule->d.own.service_name == NULL)
1748 goto nomem;
1749 }
1750 }
1751 else if (user)
1752 {
1753 if (IS_WILDCARD (user))
1754 {
1755 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1756 if (rule == NULL)
1757 goto nomem;
1758
1759 rule->d.user.uid = DBUS_UID_UNSET;
1760 }
1761 else
1762 {
1763 DBusString username;
1764 dbus_uid_t uid;
1765
1766 _dbus_string_init_const (&username, user);
1767
1768 if (_dbus_parse_unix_user_from_config (&username, &uid))
1769 {
1770 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1771 if (rule == NULL)
1772 goto nomem;
1773
1774 rule->d.user.uid = uid;
1775 }
1776 else
1777 {
1778 _dbus_warn ("Unknown username \"%s\" on element <%s>",
1779 user, element_name);
1780 }
1781 }
1782 }
1783 else if (group)
1784 {
1785 if (IS_WILDCARD (group))
1786 {
1787 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1788 if (rule == NULL)
1789 goto nomem;
1790
1791 rule->d.group.gid = DBUS_GID_UNSET;
1792 }
1793 else
1794 {
1795 DBusString groupname;
1796 dbus_gid_t gid;
1797
1798 _dbus_string_init_const (&groupname, group);
1799
1800 if (_dbus_parse_unix_group_from_config (&groupname, &gid))
1801 {
1802 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1803 if (rule == NULL)
1804 goto nomem;
1805
1806 rule->d.group.gid = gid;
1807 }
1808 else
1809 {
1810 _dbus_warn ("Unknown group \"%s\" on element <%s>",
1811 group, element_name);
1812 }
1813 }
1814 }
1815 else
1816 _dbus_assert_not_reached ("Did not handle some combination of attributes on <allow> or <deny>");
1817
1818 if (rule != NULL)
1819 {
1820 Element *pe;
1821
1822 pe = peek_element (parser);
1823 _dbus_assert (pe != NULL);
1824 _dbus_assert (pe->type == ELEMENT_POLICY);
1825
1826 switch (pe->d.policy.type)
1827 {
1828 case POLICY_IGNORED:
1829 default:
1830 /* drop the rule on the floor */
1831 break;
1832
1833 case POLICY_DEFAULT:
1834 if (!bus_policy_append_default_rule (parser->policy, rule))
1835 goto nomem;
1836 break;
1837 case POLICY_MANDATORY:
1838 if (!bus_policy_append_mandatory_rule (parser->policy, rule))
1839 goto nomem;
1840 break;
1841 case POLICY_USER:
1842 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1843 {
1844 dbus_set_error (error, DBUS_ERROR_FAILED,
1845 "<%s> rule cannot be per-user because it has bus-global semantics",
1846 element_name);
1847 goto failed;
1848 }
1849
1850 if (!bus_policy_append_user_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1851 rule))
1852 goto nomem;
1853 break;
1854 case POLICY_GROUP:
1855 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1856 {
1857 dbus_set_error (error, DBUS_ERROR_FAILED,
1858 "<%s> rule cannot be per-group because it has bus-global semantics",
1859 element_name);
1860 goto failed;
1861 }
1862
1863 if (!bus_policy_append_group_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1864 rule))
1865 goto nomem;
1866 break;
1867
1868
1869 case POLICY_CONSOLE:
1870 if (!bus_policy_append_console_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1871 rule))
1872 goto nomem;
1873 break;
1874 }
1875
1876 bus_policy_rule_unref (rule);
1877 rule = NULL;
1878 }
1879
1880 return TRUE;
1881
1882 nomem:
1883 BUS_SET_OOM (error);
1884 failed:
1885 if (rule)
1886 bus_policy_rule_unref (rule);
1887 return FALSE;
1888 }
1889
1890 static dbus_bool_t
start_policy_child(BusConfigParser * parser,const char * element_name,const char ** attribute_names,const char ** attribute_values,DBusError * error)1891 start_policy_child (BusConfigParser *parser,
1892 const char *element_name,
1893 const char **attribute_names,
1894 const char **attribute_values,
1895 DBusError *error)
1896 {
1897 if (strcmp (element_name, "allow") == 0)
1898 {
1899 if (!append_rule_from_element (parser, element_name,
1900 attribute_names, attribute_values,
1901 TRUE, error))
1902 return FALSE;
1903
1904 if (push_element (parser, ELEMENT_ALLOW) == NULL)
1905 {
1906 BUS_SET_OOM (error);
1907 return FALSE;
1908 }
1909
1910 return TRUE;
1911 }
1912 else if (strcmp (element_name, "deny") == 0)
1913 {
1914 if (!append_rule_from_element (parser, element_name,
1915 attribute_names, attribute_values,
1916 FALSE, error))
1917 return FALSE;
1918
1919 if (push_element (parser, ELEMENT_DENY) == NULL)
1920 {
1921 BUS_SET_OOM (error);
1922 return FALSE;
1923 }
1924
1925 return TRUE;
1926 }
1927 else
1928 {
1929 dbus_set_error (error, DBUS_ERROR_FAILED,
1930 "Element <%s> not allowed inside <%s> in configuration file",
1931 element_name, "policy");
1932 return FALSE;
1933 }
1934 }
1935
1936 static dbus_bool_t
start_selinux_child(BusConfigParser * parser,const char * element_name,const char ** attribute_names,const char ** attribute_values,DBusError * error)1937 start_selinux_child (BusConfigParser *parser,
1938 const char *element_name,
1939 const char **attribute_names,
1940 const char **attribute_values,
1941 DBusError *error)
1942 {
1943 char *own_copy;
1944 char *context_copy;
1945
1946 own_copy = NULL;
1947 context_copy = NULL;
1948
1949 if (strcmp (element_name, "associate") == 0)
1950 {
1951 const char *own;
1952 const char *context;
1953
1954 if (!locate_attributes (parser, "associate",
1955 attribute_names,
1956 attribute_values,
1957 error,
1958 "own", &own,
1959 "context", &context,
1960 NULL))
1961 return FALSE;
1962
1963 if (push_element (parser, ELEMENT_ASSOCIATE) == NULL)
1964 {
1965 BUS_SET_OOM (error);
1966 return FALSE;
1967 }
1968
1969 if (own == NULL || context == NULL)
1970 {
1971 dbus_set_error (error, DBUS_ERROR_FAILED,
1972 "Element <associate> must have attributes own=\"<servicename>\" and context=\"<selinux context>\"");
1973 return FALSE;
1974 }
1975
1976 own_copy = _dbus_strdup (own);
1977 if (own_copy == NULL)
1978 goto oom;
1979 context_copy = _dbus_strdup (context);
1980 if (context_copy == NULL)
1981 goto oom;
1982
1983 if (!_dbus_hash_table_insert_string (parser->service_context_table,
1984 own_copy, context_copy))
1985 goto oom;
1986
1987 return TRUE;
1988 }
1989 else
1990 {
1991 dbus_set_error (error, DBUS_ERROR_FAILED,
1992 "Element <%s> not allowed inside <%s> in configuration file",
1993 element_name, "selinux");
1994 return FALSE;
1995 }
1996
1997 oom:
1998 if (own_copy)
1999 dbus_free (own_copy);
2000
2001 if (context_copy)
2002 dbus_free (context_copy);
2003
2004 BUS_SET_OOM (error);
2005 return FALSE;
2006 }
2007
2008 dbus_bool_t
bus_config_parser_start_element(BusConfigParser * parser,const char * element_name,const char ** attribute_names,const char ** attribute_values,DBusError * error)2009 bus_config_parser_start_element (BusConfigParser *parser,
2010 const char *element_name,
2011 const char **attribute_names,
2012 const char **attribute_values,
2013 DBusError *error)
2014 {
2015 ElementType t;
2016
2017 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2018
2019 /* printf ("START: %s\n", element_name); */
2020
2021 t = top_element_type (parser);
2022
2023 if (t == ELEMENT_NONE)
2024 {
2025 if (strcmp (element_name, "busconfig") == 0)
2026 {
2027 if (!check_no_attributes (parser, "busconfig", attribute_names, attribute_values, error))
2028 return FALSE;
2029
2030 if (push_element (parser, ELEMENT_BUSCONFIG) == NULL)
2031 {
2032 BUS_SET_OOM (error);
2033 return FALSE;
2034 }
2035
2036 return TRUE;
2037 }
2038 else
2039 {
2040 dbus_set_error (error, DBUS_ERROR_FAILED,
2041 "Unknown element <%s> at root of configuration file",
2042 element_name);
2043 return FALSE;
2044 }
2045 }
2046 else if (t == ELEMENT_BUSCONFIG)
2047 {
2048 return start_busconfig_child (parser, element_name,
2049 attribute_names, attribute_values,
2050 error);
2051 }
2052 else if (t == ELEMENT_POLICY)
2053 {
2054 return start_policy_child (parser, element_name,
2055 attribute_names, attribute_values,
2056 error);
2057 }
2058 else if (t == ELEMENT_SELINUX)
2059 {
2060 return start_selinux_child (parser, element_name,
2061 attribute_names, attribute_values,
2062 error);
2063 }
2064 else
2065 {
2066 dbus_set_error (error, DBUS_ERROR_FAILED,
2067 "Element <%s> is not allowed in this context",
2068 element_name);
2069 return FALSE;
2070 }
2071 }
2072
2073 static dbus_bool_t
set_limit(BusConfigParser * parser,const char * name,long value,DBusError * error)2074 set_limit (BusConfigParser *parser,
2075 const char *name,
2076 long value,
2077 DBusError *error)
2078 {
2079 dbus_bool_t must_be_positive;
2080 dbus_bool_t must_be_int;
2081
2082 must_be_int = FALSE;
2083 must_be_positive = FALSE;
2084
2085 if (strcmp (name, "max_incoming_bytes") == 0)
2086 {
2087 must_be_positive = TRUE;
2088 parser->limits.max_incoming_bytes = value;
2089 }
2090 else if (strcmp (name, "max_incoming_unix_fds") == 0)
2091 {
2092 must_be_positive = TRUE;
2093 parser->limits.max_incoming_unix_fds = value;
2094 }
2095 else if (strcmp (name, "max_outgoing_bytes") == 0)
2096 {
2097 must_be_positive = TRUE;
2098 parser->limits.max_outgoing_bytes = value;
2099 }
2100 else if (strcmp (name, "max_outgoing_unix_fds") == 0)
2101 {
2102 must_be_positive = TRUE;
2103 parser->limits.max_outgoing_unix_fds = value;
2104 }
2105 else if (strcmp (name, "max_message_size") == 0)
2106 {
2107 must_be_positive = TRUE;
2108 parser->limits.max_message_size = value;
2109 }
2110 else if (strcmp (name, "max_message_unix_fds") == 0)
2111 {
2112 must_be_positive = TRUE;
2113 parser->limits.max_message_unix_fds = value;
2114 }
2115 else if (strcmp (name, "service_start_timeout") == 0)
2116 {
2117 must_be_positive = TRUE;
2118 must_be_int = TRUE;
2119 parser->limits.activation_timeout = value;
2120 }
2121 else if (strcmp (name, "auth_timeout") == 0)
2122 {
2123 must_be_positive = TRUE;
2124 must_be_int = TRUE;
2125 parser->limits.auth_timeout = value;
2126 }
2127 else if (strcmp (name, "pending_fd_timeout") == 0)
2128 {
2129 must_be_positive = TRUE;
2130 must_be_int = TRUE;
2131 parser->limits.pending_fd_timeout = value;
2132 }
2133 else if (strcmp (name, "reply_timeout") == 0)
2134 {
2135 must_be_positive = TRUE;
2136 must_be_int = TRUE;
2137 parser->limits.reply_timeout = value;
2138 }
2139 else if (strcmp (name, "max_completed_connections") == 0)
2140 {
2141 must_be_positive = TRUE;
2142 must_be_int = TRUE;
2143 parser->limits.max_completed_connections = value;
2144 }
2145 else if (strcmp (name, "max_incomplete_connections") == 0)
2146 {
2147 must_be_positive = TRUE;
2148 must_be_int = TRUE;
2149 parser->limits.max_incomplete_connections = value;
2150 }
2151 else if (strcmp (name, "max_connections_per_user") == 0)
2152 {
2153 must_be_positive = TRUE;
2154 must_be_int = TRUE;
2155 parser->limits.max_connections_per_user = value;
2156 }
2157 else if (strcmp (name, "max_pending_service_starts") == 0)
2158 {
2159 must_be_positive = TRUE;
2160 must_be_int = TRUE;
2161 parser->limits.max_pending_activations = value;
2162 }
2163 else if (strcmp (name, "max_names_per_connection") == 0)
2164 {
2165 must_be_positive = TRUE;
2166 must_be_int = TRUE;
2167 parser->limits.max_services_per_connection = value;
2168 }
2169 else if (strcmp (name, "max_match_rules_per_connection") == 0)
2170 {
2171 must_be_positive = TRUE;
2172 must_be_int = TRUE;
2173 parser->limits.max_match_rules_per_connection = value;
2174 }
2175 else if (strcmp (name, "max_replies_per_connection") == 0)
2176 {
2177 must_be_positive = TRUE;
2178 must_be_int = TRUE;
2179 parser->limits.max_replies_per_connection = value;
2180 }
2181 else
2182 {
2183 dbus_set_error (error, DBUS_ERROR_FAILED,
2184 "There is no limit called \"%s\"\n",
2185 name);
2186 return FALSE;
2187 }
2188
2189 if (must_be_positive && value < 0)
2190 {
2191 dbus_set_error (error, DBUS_ERROR_FAILED,
2192 "<limit name=\"%s\"> must be a positive number\n",
2193 name);
2194 return FALSE;
2195 }
2196
2197 if (must_be_int &&
2198 (value < _DBUS_INT_MIN || value > _DBUS_INT_MAX))
2199 {
2200 dbus_set_error (error, DBUS_ERROR_FAILED,
2201 "<limit name=\"%s\"> value is too large\n",
2202 name);
2203 return FALSE;
2204 }
2205
2206 return TRUE;
2207 }
2208
2209 dbus_bool_t
bus_config_parser_end_element(BusConfigParser * parser,const char * element_name,DBusError * error)2210 bus_config_parser_end_element (BusConfigParser *parser,
2211 const char *element_name,
2212 DBusError *error)
2213 {
2214 ElementType t;
2215 const char *n;
2216 Element *e;
2217
2218 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2219
2220 /* printf ("END: %s\n", element_name); */
2221
2222 t = top_element_type (parser);
2223
2224 if (t == ELEMENT_NONE)
2225 {
2226 /* should probably be an assertion failure but
2227 * being paranoid about XML parsers
2228 */
2229 dbus_set_error (error, DBUS_ERROR_FAILED,
2230 "XML parser ended element with no element on the stack");
2231 return FALSE;
2232 }
2233
2234 n = bus_config_parser_element_type_to_name (t);
2235 _dbus_assert (n != NULL);
2236 if (strcmp (n, element_name) != 0)
2237 {
2238 /* should probably be an assertion failure but
2239 * being paranoid about XML parsers
2240 */
2241 dbus_set_error (error, DBUS_ERROR_FAILED,
2242 "XML element <%s> ended but topmost element on the stack was <%s>",
2243 element_name, n);
2244 return FALSE;
2245 }
2246
2247 e = peek_element (parser);
2248 _dbus_assert (e != NULL);
2249
2250 switch (e->type)
2251 {
2252 case ELEMENT_NONE:
2253 default:
2254 _dbus_assert_not_reached ("element in stack has no type");
2255 break;
2256
2257 case ELEMENT_INCLUDE:
2258 case ELEMENT_USER:
2259 case ELEMENT_CONFIGTYPE:
2260 case ELEMENT_LISTEN:
2261 case ELEMENT_PIDFILE:
2262 case ELEMENT_AUTH:
2263 case ELEMENT_SERVICEDIR:
2264 case ELEMENT_SERVICEHELPER:
2265 case ELEMENT_INCLUDEDIR:
2266 case ELEMENT_LIMIT:
2267 if (!e->had_content)
2268 {
2269 dbus_set_error (error, DBUS_ERROR_FAILED,
2270 "XML element <%s> was expected to have content inside it",
2271 bus_config_parser_element_type_to_name (e->type));
2272 return FALSE;
2273 }
2274
2275 if (e->type == ELEMENT_LIMIT)
2276 {
2277 if (!set_limit (parser, e->d.limit.name, e->d.limit.value,
2278 error))
2279 return FALSE;
2280 }
2281 break;
2282
2283 case ELEMENT_BUSCONFIG:
2284 case ELEMENT_POLICY:
2285 case ELEMENT_ALLOW:
2286 case ELEMENT_DENY:
2287 case ELEMENT_FORK:
2288 case ELEMENT_SYSLOG:
2289 case ELEMENT_KEEP_UMASK:
2290 case ELEMENT_SELINUX:
2291 case ELEMENT_ASSOCIATE:
2292 case ELEMENT_STANDARD_SESSION_SERVICEDIRS:
2293 case ELEMENT_STANDARD_SYSTEM_SERVICEDIRS:
2294 case ELEMENT_ALLOW_ANONYMOUS:
2295 case ELEMENT_APPARMOR:
2296 break;
2297 }
2298
2299 pop_element (parser);
2300
2301 return TRUE;
2302 }
2303
2304 static dbus_bool_t
all_whitespace(const DBusString * str)2305 all_whitespace (const DBusString *str)
2306 {
2307 int i;
2308
2309 _dbus_string_skip_white (str, 0, &i);
2310
2311 return i == _dbus_string_get_length (str);
2312 }
2313
2314 static dbus_bool_t
make_full_path(const DBusString * basedir,const DBusString * filename,DBusString * full_path)2315 make_full_path (const DBusString *basedir,
2316 const DBusString *filename,
2317 DBusString *full_path)
2318 {
2319 if (_dbus_path_is_absolute (filename))
2320 {
2321 if (!_dbus_string_copy (filename, 0, full_path, 0))
2322 return FALSE;
2323 }
2324 else
2325 {
2326 if (!_dbus_string_copy (basedir, 0, full_path, 0))
2327 return FALSE;
2328
2329 if (!_dbus_concat_dir_and_file (full_path, filename))
2330 return FALSE;
2331 }
2332
2333 if (!_dbus_replace_install_prefix (full_path))
2334 return FALSE;
2335
2336 return TRUE;
2337 }
2338
2339 static dbus_bool_t
include_file(BusConfigParser * parser,const DBusString * filename,dbus_bool_t ignore_missing,DBusError * error)2340 include_file (BusConfigParser *parser,
2341 const DBusString *filename,
2342 dbus_bool_t ignore_missing,
2343 DBusError *error)
2344 {
2345 /* FIXME good test case for this would load each config file in the
2346 * test suite both alone, and as an include, and check
2347 * that the result is the same
2348 */
2349 BusConfigParser *included;
2350 const char *filename_str;
2351 DBusError tmp_error;
2352
2353 dbus_error_init (&tmp_error);
2354
2355 filename_str = _dbus_string_get_const_data (filename);
2356
2357 /* Check to make sure this file hasn't already been included. */
2358 if (seen_include (parser, filename))
2359 {
2360 dbus_set_error (error, DBUS_ERROR_FAILED,
2361 "Circular inclusion of file '%s'",
2362 filename_str);
2363 return FALSE;
2364 }
2365
2366 if (! _dbus_list_append (&parser->included_files, (void *) filename_str))
2367 {
2368 BUS_SET_OOM (error);
2369 return FALSE;
2370 }
2371
2372 /* Since parser is passed in as the parent, included
2373 inherits parser's limits. */
2374 included = bus_config_load (filename, FALSE, parser, &tmp_error);
2375
2376 _dbus_list_pop_last (&parser->included_files);
2377
2378 if (included == NULL)
2379 {
2380 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
2381
2382 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_FILE_NOT_FOUND) &&
2383 ignore_missing)
2384 {
2385 dbus_error_free (&tmp_error);
2386 return TRUE;
2387 }
2388 else
2389 {
2390 dbus_move_error (&tmp_error, error);
2391 return FALSE;
2392 }
2393 }
2394 else
2395 {
2396 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
2397
2398 if (!merge_included (parser, included, error))
2399 {
2400 bus_config_parser_unref (included);
2401 return FALSE;
2402 }
2403
2404 /* Copy included's limits back to parser. */
2405 parser->limits = included->limits;
2406
2407 bus_config_parser_unref (included);
2408 return TRUE;
2409 }
2410 }
2411
2412 static dbus_bool_t
servicehelper_path(BusConfigParser * parser,const DBusString * filename,DBusError * error)2413 servicehelper_path (BusConfigParser *parser,
2414 const DBusString *filename,
2415 DBusError *error)
2416 {
2417 const char *filename_str;
2418 char *servicehelper;
2419
2420 filename_str = _dbus_string_get_const_data (filename);
2421
2422 /* copy to avoid overwriting with NULL on OOM */
2423 servicehelper = _dbus_strdup (filename_str);
2424
2425 /* check for OOM */
2426 if (servicehelper == NULL)
2427 {
2428 BUS_SET_OOM (error);
2429 return FALSE;
2430 }
2431
2432 /* save the latest servicehelper only if not OOM */
2433 dbus_free (parser->servicehelper);
2434 parser->servicehelper = servicehelper;
2435
2436 /* We don't check whether the helper exists; instead we
2437 * would just fail to ever activate anything if it doesn't.
2438 * This allows an admin to fix the problem if it doesn't exist.
2439 * It also allows the parser test suite to successfully parse
2440 * test cases without installing the helper. ;-)
2441 */
2442
2443 return TRUE;
2444 }
2445
2446 static dbus_bool_t
include_dir(BusConfigParser * parser,const DBusString * dirname,DBusError * error)2447 include_dir (BusConfigParser *parser,
2448 const DBusString *dirname,
2449 DBusError *error)
2450 {
2451 DBusString filename;
2452 dbus_bool_t retval;
2453 DBusError tmp_error;
2454 DBusDirIter *dir;
2455 char *s;
2456
2457 if (!_dbus_string_init (&filename))
2458 {
2459 BUS_SET_OOM (error);
2460 return FALSE;
2461 }
2462
2463 retval = FALSE;
2464
2465 dir = _dbus_directory_open (dirname, error);
2466
2467 if (dir == NULL)
2468 {
2469 if (dbus_error_has_name (error, DBUS_ERROR_FILE_NOT_FOUND))
2470 {
2471 dbus_error_free (error);
2472 goto success;
2473 }
2474 else
2475 goto failed;
2476 }
2477
2478 dbus_error_init (&tmp_error);
2479 while (_dbus_directory_get_next_file (dir, &filename, &tmp_error))
2480 {
2481 DBusString full_path;
2482
2483 if (!_dbus_string_init (&full_path))
2484 {
2485 BUS_SET_OOM (error);
2486 goto failed;
2487 }
2488
2489 if (!_dbus_string_copy (dirname, 0, &full_path, 0))
2490 {
2491 BUS_SET_OOM (error);
2492 _dbus_string_free (&full_path);
2493 goto failed;
2494 }
2495
2496 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2497 {
2498 BUS_SET_OOM (error);
2499 _dbus_string_free (&full_path);
2500 goto failed;
2501 }
2502
2503 if (_dbus_string_ends_with_c_str (&full_path, ".conf"))
2504 {
2505 if (!include_file (parser, &full_path, TRUE, error))
2506 {
2507 if (dbus_error_is_set (error))
2508 {
2509 /* We use both syslog and stderr here, because this is
2510 * the configuration parser, so we don't yet know whether
2511 * this bus is going to want to write to syslog! Err on
2512 * the side of making sure the message gets to the sysadmin
2513 * somehow. */
2514 _dbus_init_system_log ("dbus-daemon",
2515 DBUS_LOG_FLAGS_STDERR | DBUS_LOG_FLAGS_SYSTEM_LOG);
2516 _dbus_log (DBUS_SYSTEM_LOG_INFO,
2517 "Encountered error '%s' while parsing '%s'",
2518 error->message,
2519 _dbus_string_get_const_data (&full_path));
2520 dbus_error_free (error);
2521 }
2522 }
2523 }
2524
2525 _dbus_string_free (&full_path);
2526 }
2527
2528 if (dbus_error_is_set (&tmp_error))
2529 {
2530 dbus_move_error (&tmp_error, error);
2531 goto failed;
2532 }
2533
2534
2535 if (!_dbus_string_copy_data (dirname, &s))
2536 {
2537 BUS_SET_OOM (error);
2538 goto failed;
2539 }
2540
2541 if (!_dbus_list_append (&parser->conf_dirs, s))
2542 {
2543 dbus_free (s);
2544 BUS_SET_OOM (error);
2545 goto failed;
2546 }
2547
2548 success:
2549 retval = TRUE;
2550
2551 failed:
2552 _dbus_string_free (&filename);
2553
2554 if (dir)
2555 _dbus_directory_close (dir);
2556
2557 return retval;
2558 }
2559
2560 dbus_bool_t
bus_config_parser_content(BusConfigParser * parser,const DBusString * content,DBusError * error)2561 bus_config_parser_content (BusConfigParser *parser,
2562 const DBusString *content,
2563 DBusError *error)
2564 {
2565 Element *e;
2566
2567 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2568
2569 #if 0
2570 {
2571 const char *c_str;
2572
2573 _dbus_string_get_const_data (content, &c_str);
2574
2575 printf ("CONTENT %d bytes: %s\n", _dbus_string_get_length (content), c_str);
2576 }
2577 #endif
2578
2579 e = peek_element (parser);
2580 if (e == NULL)
2581 {
2582 dbus_set_error (error, DBUS_ERROR_FAILED,
2583 "Text content outside of any XML element in configuration file");
2584 return FALSE;
2585 }
2586 else if (e->had_content)
2587 {
2588 _dbus_assert_not_reached ("Element had multiple content blocks");
2589 return FALSE;
2590 }
2591
2592 switch (top_element_type (parser))
2593 {
2594 case ELEMENT_NONE:
2595 default:
2596 _dbus_assert_not_reached ("element at top of stack has no type");
2597 return FALSE;
2598
2599 case ELEMENT_BUSCONFIG:
2600 case ELEMENT_POLICY:
2601 case ELEMENT_ALLOW:
2602 case ELEMENT_DENY:
2603 case ELEMENT_FORK:
2604 case ELEMENT_SYSLOG:
2605 case ELEMENT_KEEP_UMASK:
2606 case ELEMENT_STANDARD_SESSION_SERVICEDIRS:
2607 case ELEMENT_STANDARD_SYSTEM_SERVICEDIRS:
2608 case ELEMENT_ALLOW_ANONYMOUS:
2609 case ELEMENT_SELINUX:
2610 case ELEMENT_ASSOCIATE:
2611 case ELEMENT_APPARMOR:
2612 if (all_whitespace (content))
2613 return TRUE;
2614 else
2615 {
2616 dbus_set_error (error, DBUS_ERROR_FAILED,
2617 "No text content expected inside XML element %s in configuration file",
2618 bus_config_parser_element_type_to_name (top_element_type (parser)));
2619 return FALSE;
2620 }
2621
2622 case ELEMENT_PIDFILE:
2623 {
2624 char *s;
2625
2626 e->had_content = TRUE;
2627
2628 if (!_dbus_string_copy_data (content, &s))
2629 goto nomem;
2630
2631 dbus_free (parser->pidfile);
2632 parser->pidfile = s;
2633 }
2634 break;
2635
2636 case ELEMENT_INCLUDE:
2637 {
2638 DBusString full_path, selinux_policy_root;
2639
2640 e->had_content = TRUE;
2641
2642 if (e->d.include.if_selinux_enabled
2643 && !bus_selinux_enabled ())
2644 break;
2645
2646 if (!_dbus_string_init (&full_path))
2647 goto nomem;
2648
2649 if (e->d.include.selinux_root_relative)
2650 {
2651 if (!bus_selinux_get_policy_root ())
2652 {
2653 dbus_set_error (error, DBUS_ERROR_FAILED,
2654 "Could not determine SELinux policy root for relative inclusion");
2655 _dbus_string_free (&full_path);
2656 return FALSE;
2657 }
2658 _dbus_string_init_const (&selinux_policy_root,
2659 bus_selinux_get_policy_root ());
2660 if (!make_full_path (&selinux_policy_root, content, &full_path))
2661 {
2662 _dbus_string_free (&full_path);
2663 goto nomem;
2664 }
2665 }
2666 else if (!make_full_path (&parser->basedir, content, &full_path))
2667 {
2668 _dbus_string_free (&full_path);
2669 goto nomem;
2670 }
2671
2672 if (!include_file (parser, &full_path,
2673 e->d.include.ignore_missing, error))
2674 {
2675 _dbus_string_free (&full_path);
2676 return FALSE;
2677 }
2678
2679 _dbus_string_free (&full_path);
2680 }
2681 break;
2682
2683 case ELEMENT_SERVICEHELPER:
2684 {
2685 DBusString full_path;
2686
2687 e->had_content = TRUE;
2688
2689 if (!_dbus_string_init (&full_path))
2690 goto nomem;
2691
2692 if (!make_full_path (&parser->basedir, content, &full_path))
2693 {
2694 _dbus_string_free (&full_path);
2695 goto nomem;
2696 }
2697
2698 if (!servicehelper_path (parser, &full_path, error))
2699 {
2700 _dbus_string_free (&full_path);
2701 return FALSE;
2702 }
2703
2704 _dbus_string_free (&full_path);
2705 }
2706 break;
2707
2708 case ELEMENT_INCLUDEDIR:
2709 {
2710 DBusString full_path;
2711
2712 e->had_content = TRUE;
2713
2714 if (!_dbus_string_init (&full_path))
2715 goto nomem;
2716
2717 if (!make_full_path (&parser->basedir, content, &full_path))
2718 {
2719 _dbus_string_free (&full_path);
2720 goto nomem;
2721 }
2722
2723 if (!include_dir (parser, &full_path, error))
2724 {
2725 _dbus_string_free (&full_path);
2726 return FALSE;
2727 }
2728
2729 _dbus_string_free (&full_path);
2730 }
2731 break;
2732
2733 case ELEMENT_USER:
2734 {
2735 char *s;
2736
2737 e->had_content = TRUE;
2738
2739 if (!_dbus_string_copy_data (content, &s))
2740 goto nomem;
2741
2742 dbus_free (parser->user);
2743 parser->user = s;
2744 }
2745 break;
2746
2747 case ELEMENT_CONFIGTYPE:
2748 {
2749 char *s;
2750
2751 e->had_content = TRUE;
2752
2753 if (!_dbus_string_copy_data (content, &s))
2754 goto nomem;
2755
2756 dbus_free (parser->bus_type);
2757 parser->bus_type = s;
2758 }
2759 break;
2760
2761 case ELEMENT_LISTEN:
2762 {
2763 char *s;
2764
2765 e->had_content = TRUE;
2766
2767 if (!_dbus_string_copy_data (content, &s))
2768 goto nomem;
2769
2770 if (!_dbus_list_append (&parser->listen_on,
2771 s))
2772 {
2773 dbus_free (s);
2774 goto nomem;
2775 }
2776 }
2777 break;
2778
2779 case ELEMENT_AUTH:
2780 {
2781 char *s;
2782
2783 e->had_content = TRUE;
2784
2785 if (!_dbus_string_copy_data (content, &s))
2786 goto nomem;
2787
2788 if (!_dbus_list_append (&parser->mechanisms,
2789 s))
2790 {
2791 dbus_free (s);
2792 goto nomem;
2793 }
2794 }
2795 break;
2796
2797 case ELEMENT_SERVICEDIR:
2798 {
2799 char *s;
2800 BusConfigServiceDir *dir;
2801 DBusString full_path;
2802 DBusList *link;
2803
2804 e->had_content = TRUE;
2805
2806 if (!_dbus_string_init (&full_path))
2807 goto nomem;
2808
2809 if (!make_full_path (&parser->basedir, content, &full_path))
2810 {
2811 _dbus_string_free (&full_path);
2812 goto nomem;
2813 }
2814
2815 if (!_dbus_string_copy_data (&full_path, &s))
2816 {
2817 _dbus_string_free (&full_path);
2818 goto nomem;
2819 }
2820
2821 /* <servicedir/> has traditionally implied that we watch the
2822 * directory with inotify, and allow service files whose names do not
2823 * match the bus name */
2824 dir = bus_config_service_dir_new_take (s, BUS_SERVICE_DIR_FLAGS_NONE);
2825
2826 if (dir == NULL)
2827 {
2828 _dbus_string_free (&full_path);
2829 dbus_free (s);
2830 goto nomem;
2831 }
2832
2833 link = _dbus_list_alloc_link (dir);
2834
2835 if (link == NULL)
2836 {
2837 _dbus_string_free (&full_path);
2838 bus_config_service_dir_free (dir);
2839 goto nomem;
2840 }
2841
2842 /* cannot fail */
2843 service_dirs_append_link_unique_or_free (&parser->service_dirs, link);
2844 _dbus_string_free (&full_path);
2845 }
2846 break;
2847
2848 case ELEMENT_LIMIT:
2849 {
2850 long val;
2851
2852 e->had_content = TRUE;
2853
2854 val = 0;
2855 if (!_dbus_string_parse_int (content, 0, &val, NULL))
2856 {
2857 dbus_set_error (error, DBUS_ERROR_FAILED,
2858 "<limit name=\"%s\"> element has invalid value (could not parse as integer)",
2859 e->d.limit.name);
2860 return FALSE;
2861 }
2862
2863 e->d.limit.value = val;
2864
2865 _dbus_verbose ("Loaded value %ld for limit %s\n",
2866 e->d.limit.value,
2867 e->d.limit.name);
2868 }
2869 break;
2870 }
2871
2872 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2873 return TRUE;
2874
2875 nomem:
2876 BUS_SET_OOM (error);
2877 return FALSE;
2878 }
2879
2880 dbus_bool_t
bus_config_parser_finished(BusConfigParser * parser,DBusError * error)2881 bus_config_parser_finished (BusConfigParser *parser,
2882 DBusError *error)
2883 {
2884 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2885
2886 if (parser->stack != NULL)
2887 {
2888 dbus_set_error (error, DBUS_ERROR_FAILED,
2889 "Element <%s> was not closed in configuration file",
2890 bus_config_parser_element_type_to_name (top_element_type (parser)));
2891
2892 return FALSE;
2893 }
2894
2895 if (parser->is_toplevel && parser->listen_on == NULL)
2896 {
2897 dbus_set_error (error, DBUS_ERROR_FAILED,
2898 "Configuration file needs one or more <listen> elements giving addresses");
2899 return FALSE;
2900 }
2901
2902 return TRUE;
2903 }
2904
2905 const char*
bus_config_parser_get_user(BusConfigParser * parser)2906 bus_config_parser_get_user (BusConfigParser *parser)
2907 {
2908 return parser->user;
2909 }
2910
2911 const char*
bus_config_parser_get_type(BusConfigParser * parser)2912 bus_config_parser_get_type (BusConfigParser *parser)
2913 {
2914 return parser->bus_type;
2915 }
2916
2917 DBusList**
bus_config_parser_get_addresses(BusConfigParser * parser)2918 bus_config_parser_get_addresses (BusConfigParser *parser)
2919 {
2920 return &parser->listen_on;
2921 }
2922
2923 DBusList**
bus_config_parser_get_mechanisms(BusConfigParser * parser)2924 bus_config_parser_get_mechanisms (BusConfigParser *parser)
2925 {
2926 return &parser->mechanisms;
2927 }
2928
2929 DBusList**
bus_config_parser_get_service_dirs(BusConfigParser * parser)2930 bus_config_parser_get_service_dirs (BusConfigParser *parser)
2931 {
2932 return &parser->service_dirs;
2933 }
2934
2935 DBusList**
bus_config_parser_get_conf_dirs(BusConfigParser * parser)2936 bus_config_parser_get_conf_dirs (BusConfigParser *parser)
2937 {
2938 return &parser->conf_dirs;
2939 }
2940
2941 dbus_bool_t
bus_config_parser_get_fork(BusConfigParser * parser)2942 bus_config_parser_get_fork (BusConfigParser *parser)
2943 {
2944 return parser->fork;
2945 }
2946
2947 dbus_bool_t
bus_config_parser_get_syslog(BusConfigParser * parser)2948 bus_config_parser_get_syslog (BusConfigParser *parser)
2949 {
2950 return parser->syslog;
2951 }
2952
2953 dbus_bool_t
bus_config_parser_get_keep_umask(BusConfigParser * parser)2954 bus_config_parser_get_keep_umask (BusConfigParser *parser)
2955 {
2956 return parser->keep_umask;
2957 }
2958
2959 dbus_bool_t
bus_config_parser_get_allow_anonymous(BusConfigParser * parser)2960 bus_config_parser_get_allow_anonymous (BusConfigParser *parser)
2961 {
2962 return parser->allow_anonymous;
2963 }
2964
2965 const char *
bus_config_parser_get_pidfile(BusConfigParser * parser)2966 bus_config_parser_get_pidfile (BusConfigParser *parser)
2967 {
2968 return parser->pidfile;
2969 }
2970
2971 const char *
bus_config_parser_get_servicehelper(BusConfigParser * parser)2972 bus_config_parser_get_servicehelper (BusConfigParser *parser)
2973 {
2974 return parser->servicehelper;
2975 }
2976
2977 BusPolicy*
bus_config_parser_steal_policy(BusConfigParser * parser)2978 bus_config_parser_steal_policy (BusConfigParser *parser)
2979 {
2980 BusPolicy *policy;
2981
2982 _dbus_assert (parser->policy != NULL); /* can only steal the policy 1 time */
2983
2984 policy = parser->policy;
2985
2986 parser->policy = NULL;
2987
2988 return policy;
2989 }
2990
2991 /* Overwrite any limits that were set in the configuration file */
2992 void
bus_config_parser_get_limits(BusConfigParser * parser,BusLimits * limits)2993 bus_config_parser_get_limits (BusConfigParser *parser,
2994 BusLimits *limits)
2995 {
2996 *limits = parser->limits;
2997 }
2998
2999 DBusHashTable*
bus_config_parser_steal_service_context_table(BusConfigParser * parser)3000 bus_config_parser_steal_service_context_table (BusConfigParser *parser)
3001 {
3002 DBusHashTable *table;
3003
3004 _dbus_assert (parser->service_context_table != NULL); /* can only steal once */
3005
3006 table = parser->service_context_table;
3007
3008 parser->service_context_table = NULL;
3009
3010 return table;
3011 }
3012
3013 /*
3014 * Return a list of the directories that should be watched with inotify,
3015 * as strings. The list might be empty and is in arbitrary order.
3016 *
3017 * The list must be empty on entry. On success, the links are owned by the
3018 * caller and must be freed, but the data in each link remains owned by
3019 * the BusConfigParser and must not be freed: in GObject-Introspection
3020 * notation, it is (transfer container).
3021 */
3022 dbus_bool_t
bus_config_parser_get_watched_dirs(BusConfigParser * parser,DBusList ** watched_dirs)3023 bus_config_parser_get_watched_dirs (BusConfigParser *parser,
3024 DBusList **watched_dirs)
3025 {
3026 DBusList *link;
3027
3028 _dbus_assert (*watched_dirs == NULL);
3029
3030 for (link = _dbus_list_get_first_link (&parser->conf_dirs);
3031 link != NULL;
3032 link = _dbus_list_get_next_link (&parser->conf_dirs, link))
3033 {
3034 if (!_dbus_list_append (watched_dirs, link->data))
3035 goto oom;
3036 }
3037
3038 for (link = _dbus_list_get_first_link (&parser->service_dirs);
3039 link != NULL;
3040 link = _dbus_list_get_next_link (&parser->service_dirs, link))
3041 {
3042 BusConfigServiceDir *dir = link->data;
3043
3044 if (dir->flags & BUS_SERVICE_DIR_FLAGS_NO_WATCH)
3045 continue;
3046
3047 if (!_dbus_list_append (watched_dirs, dir->path))
3048 goto oom;
3049 }
3050
3051 return TRUE;
3052
3053 oom:
3054 _dbus_list_clear (watched_dirs);
3055 return FALSE;
3056 }
3057
3058 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
3059 #include <stdio.h>
3060
3061 typedef enum
3062 {
3063 VALID,
3064 INVALID,
3065 UNKNOWN
3066 } Validity;
3067
3068 static dbus_bool_t
do_check_own_rules(BusPolicy * policy)3069 do_check_own_rules (BusPolicy *policy)
3070 {
3071 const struct {
3072 const char *name;
3073 dbus_bool_t allowed;
3074 } checks[] = {
3075 {"org.freedesktop", FALSE},
3076 {"org.freedesktop.ManySystem", FALSE},
3077 {"org.freedesktop.ManySystems", TRUE},
3078 {"org.freedesktop.ManySystems.foo", TRUE},
3079 {"org.freedesktop.ManySystems.foo.bar", TRUE},
3080 {"org.freedesktop.ManySystems2", FALSE},
3081 {"org.freedesktop.ManySystems2.foo", FALSE},
3082 {"org.freedesktop.ManySystems2.foo.bar", FALSE},
3083 {NULL, FALSE}
3084 };
3085 int i = 0;
3086
3087 while (checks[i].name)
3088 {
3089 DBusString service_name;
3090 dbus_bool_t ret;
3091
3092 if (!_dbus_string_init (&service_name))
3093 _dbus_assert_not_reached ("couldn't init string");
3094 if (!_dbus_string_append (&service_name, checks[i].name))
3095 _dbus_assert_not_reached ("couldn't append string");
3096
3097 ret = bus_policy_check_can_own (policy, &service_name);
3098 printf (" Check name %s: %s\n", checks[i].name,
3099 ret ? "allowed" : "not allowed");
3100 if (checks[i].allowed && !ret)
3101 {
3102 _dbus_warn ("Cannot own %s", checks[i].name);
3103 return FALSE;
3104 }
3105 if (!checks[i].allowed && ret)
3106 {
3107 _dbus_warn ("Can own %s", checks[i].name);
3108 return FALSE;
3109 }
3110 _dbus_string_free (&service_name);
3111
3112 i++;
3113 }
3114
3115 return TRUE;
3116 }
3117
3118 static dbus_bool_t
do_load(const DBusString * full_path,Validity validity,dbus_bool_t oom_possible,dbus_bool_t check_own_rules)3119 do_load (const DBusString *full_path,
3120 Validity validity,
3121 dbus_bool_t oom_possible,
3122 dbus_bool_t check_own_rules)
3123 {
3124 BusConfigParser *parser;
3125 DBusError error;
3126
3127 dbus_error_init (&error);
3128
3129 parser = bus_config_load (full_path, TRUE, NULL, &error);
3130 if (parser == NULL)
3131 {
3132 _DBUS_ASSERT_ERROR_IS_SET (&error);
3133
3134 if (oom_possible &&
3135 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
3136 {
3137 _dbus_verbose ("Failed to load valid file due to OOM\n");
3138 dbus_error_free (&error);
3139 return TRUE;
3140 }
3141 else if (validity == VALID)
3142 {
3143 _dbus_warn ("Failed to load valid file but still had memory: %s",
3144 error.message);
3145
3146 dbus_error_free (&error);
3147 return FALSE;
3148 }
3149 else
3150 {
3151 dbus_error_free (&error);
3152 return TRUE;
3153 }
3154 }
3155 else
3156 {
3157 _DBUS_ASSERT_ERROR_IS_CLEAR (&error);
3158
3159 if (check_own_rules && do_check_own_rules (parser->policy) == FALSE)
3160 {
3161 return FALSE;
3162 }
3163
3164 bus_config_parser_unref (parser);
3165
3166 if (validity == INVALID)
3167 {
3168 _dbus_warn ("Accepted invalid file");
3169 return FALSE;
3170 }
3171
3172 return TRUE;
3173 }
3174 }
3175
3176 typedef struct
3177 {
3178 const DBusString *full_path;
3179 Validity validity;
3180 dbus_bool_t check_own_rules;
3181 } LoaderOomData;
3182
3183 static dbus_bool_t
check_loader_oom_func(void * data)3184 check_loader_oom_func (void *data)
3185 {
3186 LoaderOomData *d = data;
3187
3188 return do_load (d->full_path, d->validity, TRUE, d->check_own_rules);
3189 }
3190
3191 static dbus_bool_t
process_test_valid_subdir(const DBusString * test_base_dir,const char * subdir,Validity validity)3192 process_test_valid_subdir (const DBusString *test_base_dir,
3193 const char *subdir,
3194 Validity validity)
3195 {
3196 DBusString test_directory;
3197 DBusString filename;
3198 DBusDirIter *dir;
3199 dbus_bool_t retval;
3200 DBusError error;
3201
3202 retval = FALSE;
3203 dir = NULL;
3204
3205 if (!_dbus_string_init (&test_directory))
3206 _dbus_assert_not_reached ("didn't allocate test_directory");
3207
3208 _dbus_string_init_const (&filename, subdir);
3209
3210 if (!_dbus_string_copy (test_base_dir, 0,
3211 &test_directory, 0))
3212 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
3213
3214 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
3215 _dbus_assert_not_reached ("couldn't allocate full path");
3216
3217 _dbus_string_free (&filename);
3218 if (!_dbus_string_init (&filename))
3219 _dbus_assert_not_reached ("didn't allocate filename string");
3220
3221 dbus_error_init (&error);
3222 dir = _dbus_directory_open (&test_directory, &error);
3223 if (dir == NULL)
3224 {
3225 _dbus_warn ("Could not open %s: %s",
3226 _dbus_string_get_const_data (&test_directory),
3227 error.message);
3228 dbus_error_free (&error);
3229 goto failed;
3230 }
3231
3232 if (validity == VALID)
3233 printf ("Testing valid files:\n");
3234 else if (validity == INVALID)
3235 printf ("Testing invalid files:\n");
3236 else
3237 printf ("Testing unknown files:\n");
3238
3239 next:
3240 while (_dbus_directory_get_next_file (dir, &filename, &error))
3241 {
3242 DBusString full_path;
3243 LoaderOomData d;
3244
3245 if (!_dbus_string_init (&full_path))
3246 _dbus_assert_not_reached ("couldn't init string");
3247
3248 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
3249 _dbus_assert_not_reached ("couldn't copy dir to full_path");
3250
3251 if (!_dbus_concat_dir_and_file (&full_path, &filename))
3252 _dbus_assert_not_reached ("couldn't concat file to dir");
3253
3254 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
3255 {
3256 _dbus_verbose ("Skipping non-.conf file %s\n",
3257 _dbus_string_get_const_data (&filename));
3258 _dbus_string_free (&full_path);
3259 goto next;
3260 }
3261
3262 printf (" %s\n", _dbus_string_get_const_data (&filename));
3263
3264 _dbus_verbose (" expecting %s\n",
3265 validity == VALID ? "valid" :
3266 (validity == INVALID ? "invalid" :
3267 (validity == UNKNOWN ? "unknown" : "???")));
3268
3269 d.full_path = &full_path;
3270 d.validity = validity;
3271 d.check_own_rules = _dbus_string_ends_with_c_str (&full_path,
3272 "check-own-rules.conf");
3273
3274 /* FIXME hackaround for an expat problem, see
3275 * https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=124747
3276 * http://freedesktop.org/pipermail/dbus/2004-May/001153.html
3277 */
3278 /* if (!_dbus_test_oom_handling ("config-loader", check_loader_oom_func, &d)) */
3279 if (!check_loader_oom_func (&d))
3280 _dbus_assert_not_reached ("test failed");
3281
3282 _dbus_string_free (&full_path);
3283 }
3284
3285 if (dbus_error_is_set (&error))
3286 {
3287 _dbus_warn ("Could not get next file in %s: %s",
3288 _dbus_string_get_const_data (&test_directory),
3289 error.message);
3290 dbus_error_free (&error);
3291 goto failed;
3292 }
3293
3294 retval = TRUE;
3295
3296 failed:
3297
3298 if (dir)
3299 _dbus_directory_close (dir);
3300 _dbus_string_free (&test_directory);
3301 _dbus_string_free (&filename);
3302
3303 return retval;
3304 }
3305
3306 static dbus_bool_t
bools_equal(dbus_bool_t a,dbus_bool_t b)3307 bools_equal (dbus_bool_t a,
3308 dbus_bool_t b)
3309 {
3310 return a ? b : !b;
3311 }
3312
3313 static dbus_bool_t
strings_equal_or_both_null(const char * a,const char * b)3314 strings_equal_or_both_null (const char *a,
3315 const char *b)
3316 {
3317 if (a == NULL || b == NULL)
3318 return a == b;
3319 else
3320 return !strcmp (a, b);
3321 }
3322
3323 static dbus_bool_t
elements_equal(const Element * a,const Element * b)3324 elements_equal (const Element *a,
3325 const Element *b)
3326 {
3327 if (a->type != b->type)
3328 return FALSE;
3329
3330 if (!bools_equal (a->had_content, b->had_content))
3331 return FALSE;
3332
3333 switch (a->type)
3334 {
3335
3336 case ELEMENT_INCLUDE:
3337 if (!bools_equal (a->d.include.ignore_missing,
3338 b->d.include.ignore_missing))
3339 return FALSE;
3340 break;
3341
3342 case ELEMENT_POLICY:
3343 if (a->d.policy.type != b->d.policy.type)
3344 return FALSE;
3345 if (a->d.policy.gid_uid_or_at_console != b->d.policy.gid_uid_or_at_console)
3346 return FALSE;
3347 break;
3348
3349 case ELEMENT_LIMIT:
3350 if (strcmp (a->d.limit.name, b->d.limit.name))
3351 return FALSE;
3352 if (a->d.limit.value != b->d.limit.value)
3353 return FALSE;
3354 break;
3355
3356 case ELEMENT_NONE:
3357 case ELEMENT_BUSCONFIG:
3358 case ELEMENT_USER:
3359 case ELEMENT_LISTEN:
3360 case ELEMENT_AUTH:
3361 case ELEMENT_ALLOW:
3362 case ELEMENT_DENY:
3363 case ELEMENT_FORK:
3364 case ELEMENT_PIDFILE:
3365 case ELEMENT_SERVICEDIR:
3366 case ELEMENT_SERVICEHELPER:
3367 case ELEMENT_INCLUDEDIR:
3368 case ELEMENT_CONFIGTYPE:
3369 case ELEMENT_SELINUX:
3370 case ELEMENT_ASSOCIATE:
3371 case ELEMENT_STANDARD_SESSION_SERVICEDIRS:
3372 case ELEMENT_STANDARD_SYSTEM_SERVICEDIRS:
3373 case ELEMENT_KEEP_UMASK:
3374 case ELEMENT_SYSLOG:
3375 case ELEMENT_ALLOW_ANONYMOUS:
3376 case ELEMENT_APPARMOR:
3377 default:
3378 /* do nothing: nothing in the Element struct for these types */
3379 break;
3380 }
3381
3382 return TRUE;
3383
3384 }
3385
3386 static dbus_bool_t
lists_of_elements_equal(DBusList * a,DBusList * b)3387 lists_of_elements_equal (DBusList *a,
3388 DBusList *b)
3389 {
3390 DBusList *ia;
3391 DBusList *ib;
3392
3393 ia = a;
3394 ib = b;
3395
3396 while (ia != NULL && ib != NULL)
3397 {
3398 if (elements_equal (ia->data, ib->data))
3399 return FALSE;
3400 ia = _dbus_list_get_next_link (&a, ia);
3401 ib = _dbus_list_get_next_link (&b, ib);
3402 }
3403
3404 return ia == NULL && ib == NULL;
3405 }
3406
3407 static dbus_bool_t
lists_of_c_strings_equal(DBusList * a,DBusList * b)3408 lists_of_c_strings_equal (DBusList *a,
3409 DBusList *b)
3410 {
3411 DBusList *ia;
3412 DBusList *ib;
3413
3414 ia = a;
3415 ib = b;
3416
3417 while (ia != NULL && ib != NULL)
3418 {
3419 if (strcmp (ia->data, ib->data))
3420 return FALSE;
3421 ia = _dbus_list_get_next_link (&a, ia);
3422 ib = _dbus_list_get_next_link (&b, ib);
3423 }
3424
3425 return ia == NULL && ib == NULL;
3426 }
3427
3428 static dbus_bool_t
lists_of_service_dirs_equal(DBusList * a,DBusList * b)3429 lists_of_service_dirs_equal (DBusList *a,
3430 DBusList *b)
3431 {
3432 DBusList *ia;
3433 DBusList *ib;
3434
3435 ia = a;
3436 ib = b;
3437
3438 while (ia != NULL && ib != NULL)
3439 {
3440 BusConfigServiceDir *da = ia->data;
3441 BusConfigServiceDir *db = ib->data;
3442
3443 if (strcmp (da->path, db->path))
3444 return FALSE;
3445
3446 if (da->flags != db->flags)
3447 return FALSE;
3448
3449 ia = _dbus_list_get_next_link (&a, ia);
3450 ib = _dbus_list_get_next_link (&b, ib);
3451 }
3452
3453 return ia == NULL && ib == NULL;
3454 }
3455
3456 static dbus_bool_t
limits_equal(const BusLimits * a,const BusLimits * b)3457 limits_equal (const BusLimits *a,
3458 const BusLimits *b)
3459 {
3460 return
3461 (a->max_incoming_bytes == b->max_incoming_bytes
3462 || a->max_incoming_unix_fds == b->max_incoming_unix_fds
3463 || a->max_outgoing_bytes == b->max_outgoing_bytes
3464 || a->max_outgoing_unix_fds == b->max_outgoing_unix_fds
3465 || a->max_message_size == b->max_message_size
3466 || a->max_message_unix_fds == b->max_message_unix_fds
3467 || a->activation_timeout == b->activation_timeout
3468 || a->auth_timeout == b->auth_timeout
3469 || a->pending_fd_timeout == b->pending_fd_timeout
3470 || a->max_completed_connections == b->max_completed_connections
3471 || a->max_incomplete_connections == b->max_incomplete_connections
3472 || a->max_connections_per_user == b->max_connections_per_user
3473 || a->max_pending_activations == b->max_pending_activations
3474 || a->max_services_per_connection == b->max_services_per_connection
3475 || a->max_match_rules_per_connection == b->max_match_rules_per_connection
3476 || a->max_replies_per_connection == b->max_replies_per_connection
3477 || a->reply_timeout == b->reply_timeout);
3478 }
3479
3480 static dbus_bool_t
config_parsers_equal(const BusConfigParser * a,const BusConfigParser * b)3481 config_parsers_equal (const BusConfigParser *a,
3482 const BusConfigParser *b)
3483 {
3484 if (!_dbus_string_equal (&a->basedir, &b->basedir))
3485 return FALSE;
3486
3487 if (!lists_of_elements_equal (a->stack, b->stack))
3488 return FALSE;
3489
3490 if (!strings_equal_or_both_null (a->user, b->user))
3491 return FALSE;
3492
3493 if (!lists_of_c_strings_equal (a->listen_on, b->listen_on))
3494 return FALSE;
3495
3496 if (!lists_of_c_strings_equal (a->mechanisms, b->mechanisms))
3497 return FALSE;
3498
3499 if (!lists_of_service_dirs_equal (a->service_dirs, b->service_dirs))
3500 return FALSE;
3501
3502 /* FIXME: compare policy */
3503
3504 /* FIXME: compare service selinux ID table */
3505
3506 if (! limits_equal (&a->limits, &b->limits))
3507 return FALSE;
3508
3509 if (!strings_equal_or_both_null (a->pidfile, b->pidfile))
3510 return FALSE;
3511
3512 if (! bools_equal (a->fork, b->fork))
3513 return FALSE;
3514
3515 if (! bools_equal (a->keep_umask, b->keep_umask))
3516 return FALSE;
3517
3518 if (! bools_equal (a->is_toplevel, b->is_toplevel))
3519 return FALSE;
3520
3521 return TRUE;
3522 }
3523
3524 static dbus_bool_t
all_are_equiv(const DBusString * target_directory)3525 all_are_equiv (const DBusString *target_directory)
3526 {
3527 DBusString filename;
3528 DBusDirIter *dir;
3529 BusConfigParser *first_parser;
3530 BusConfigParser *parser;
3531 DBusError error;
3532 dbus_bool_t equal;
3533 dbus_bool_t retval;
3534
3535 dir = NULL;
3536 first_parser = NULL;
3537 parser = NULL;
3538 retval = FALSE;
3539
3540 if (!_dbus_string_init (&filename))
3541 _dbus_assert_not_reached ("didn't allocate filename string");
3542
3543 dbus_error_init (&error);
3544 dir = _dbus_directory_open (target_directory, &error);
3545 if (dir == NULL)
3546 {
3547 _dbus_warn ("Could not open %s: %s",
3548 _dbus_string_get_const_data (target_directory),
3549 error.message);
3550 dbus_error_free (&error);
3551 goto finished;
3552 }
3553
3554 printf ("Comparing equivalent files:\n");
3555
3556 next:
3557 while (_dbus_directory_get_next_file (dir, &filename, &error))
3558 {
3559 DBusString full_path;
3560
3561 if (!_dbus_string_init (&full_path))
3562 _dbus_assert_not_reached ("couldn't init string");
3563
3564 if (!_dbus_string_copy (target_directory, 0, &full_path, 0))
3565 _dbus_assert_not_reached ("couldn't copy dir to full_path");
3566
3567 if (!_dbus_concat_dir_and_file (&full_path, &filename))
3568 _dbus_assert_not_reached ("couldn't concat file to dir");
3569
3570 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
3571 {
3572 _dbus_verbose ("Skipping non-.conf file %s\n",
3573 _dbus_string_get_const_data (&filename));
3574 _dbus_string_free (&full_path);
3575 goto next;
3576 }
3577
3578 printf (" %s\n", _dbus_string_get_const_data (&filename));
3579
3580 parser = bus_config_load (&full_path, TRUE, NULL, &error);
3581
3582 if (parser == NULL)
3583 {
3584 _dbus_warn ("Could not load file %s: %s",
3585 _dbus_string_get_const_data (&full_path),
3586 error.message);
3587 _dbus_string_free (&full_path);
3588 dbus_error_free (&error);
3589 goto finished;
3590 }
3591 else if (first_parser == NULL)
3592 {
3593 _dbus_string_free (&full_path);
3594 first_parser = parser;
3595 }
3596 else
3597 {
3598 _dbus_string_free (&full_path);
3599 equal = config_parsers_equal (first_parser, parser);
3600 bus_config_parser_unref (parser);
3601 if (! equal)
3602 goto finished;
3603 }
3604 }
3605
3606 retval = TRUE;
3607
3608 finished:
3609 _dbus_string_free (&filename);
3610 if (first_parser)
3611 bus_config_parser_unref (first_parser);
3612 if (dir)
3613 _dbus_directory_close (dir);
3614
3615 return retval;
3616
3617 }
3618
3619 static dbus_bool_t
process_test_equiv_subdir(const DBusString * test_base_dir,const char * subdir)3620 process_test_equiv_subdir (const DBusString *test_base_dir,
3621 const char *subdir)
3622 {
3623 DBusString test_directory;
3624 DBusString filename;
3625 DBusDirIter *dir;
3626 DBusError error;
3627 dbus_bool_t equal;
3628 dbus_bool_t retval;
3629
3630 dir = NULL;
3631 retval = FALSE;
3632
3633 if (!_dbus_string_init (&test_directory))
3634 _dbus_assert_not_reached ("didn't allocate test_directory");
3635
3636 _dbus_string_init_const (&filename, subdir);
3637
3638 if (!_dbus_string_copy (test_base_dir, 0,
3639 &test_directory, 0))
3640 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
3641
3642 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
3643 _dbus_assert_not_reached ("couldn't allocate full path");
3644
3645 _dbus_string_free (&filename);
3646 if (!_dbus_string_init (&filename))
3647 _dbus_assert_not_reached ("didn't allocate filename string");
3648
3649 dbus_error_init (&error);
3650 dir = _dbus_directory_open (&test_directory, &error);
3651 if (dir == NULL)
3652 {
3653 _dbus_warn ("Could not open %s: %s",
3654 _dbus_string_get_const_data (&test_directory),
3655 error.message);
3656 dbus_error_free (&error);
3657 goto finished;
3658 }
3659
3660 while (_dbus_directory_get_next_file (dir, &filename, &error))
3661 {
3662 DBusString full_path;
3663
3664 /* Skip CVS's magic directories! */
3665 if (_dbus_string_equal_c_str (&filename, "CVS"))
3666 continue;
3667
3668 if (!_dbus_string_init (&full_path))
3669 _dbus_assert_not_reached ("couldn't init string");
3670
3671 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
3672 _dbus_assert_not_reached ("couldn't copy dir to full_path");
3673
3674 if (!_dbus_concat_dir_and_file (&full_path, &filename))
3675 _dbus_assert_not_reached ("couldn't concat file to dir");
3676
3677 equal = all_are_equiv (&full_path);
3678 _dbus_string_free (&full_path);
3679
3680 if (!equal)
3681 goto finished;
3682 }
3683
3684 retval = TRUE;
3685
3686 finished:
3687 _dbus_string_free (&test_directory);
3688 _dbus_string_free (&filename);
3689 if (dir)
3690 _dbus_directory_close (dir);
3691
3692 return retval;
3693
3694 }
3695
3696 static const char *test_session_service_dir_matches[] =
3697 {
3698 /* will be filled in test_default_session_servicedirs() */
3699 #ifdef DBUS_WIN
3700 NULL, /* install root-based */
3701 NULL, /* CommonProgramFiles-based */
3702 #else
3703 NULL, /* XDG_RUNTIME_DIR-based */
3704 NULL, /* XDG_DATA_HOME-based */
3705 NULL, /* XDG_DATA_DIRS-based */
3706 NULL, /* XDG_DATA_DIRS-based */
3707 DBUS_DATADIR "/dbus-1/services",
3708 #endif
3709 NULL
3710 };
3711
3712 static dbus_bool_t
test_default_session_servicedirs(const DBusString * test_base_dir)3713 test_default_session_servicedirs (const DBusString *test_base_dir)
3714 {
3715 BusConfigParser *parser = NULL;
3716 DBusError error = DBUS_ERROR_INIT;
3717 DBusList **dirs;
3718 DBusList *watched_dirs = NULL;
3719 DBusList *link;
3720 DBusString tmp;
3721 DBusString full_path;
3722 DBusString progs;
3723 DBusString install_root_based;
3724 DBusString runtime_dir_based;
3725 DBusString data_home_based;
3726 DBusString data_dirs_based;
3727 DBusString data_dirs_based2;
3728 int i;
3729 dbus_bool_t ret = FALSE;
3730 #ifdef DBUS_WIN
3731 const char *common_progs;
3732 #else
3733 const char *dbus_test_builddir;
3734 const char *xdg_data_home;
3735 const char *xdg_runtime_dir;
3736 #endif
3737
3738 /* On each platform we don't actually use all of these, but it's easier to
3739 * handle the deallocation if we always allocate them, whether needed or
3740 * not */
3741 if (!_dbus_string_init (&full_path) ||
3742 !_dbus_string_init (&progs) ||
3743 !_dbus_string_init (&install_root_based) ||
3744 !_dbus_string_init (&runtime_dir_based) ||
3745 !_dbus_string_init (&data_home_based) ||
3746 !_dbus_string_init (&data_dirs_based) ||
3747 !_dbus_string_init (&data_dirs_based2))
3748 _dbus_assert_not_reached ("OOM allocating strings");
3749
3750 if (!_dbus_string_copy (test_base_dir, 0,
3751 &full_path, 0))
3752 _dbus_assert_not_reached ("couldn't copy test_base_dir to full_path");
3753
3754 _dbus_string_init_const (&tmp, "valid-config-files");
3755
3756 if (!_dbus_concat_dir_and_file (&full_path, &tmp))
3757 _dbus_assert_not_reached ("couldn't allocate full path");
3758
3759 _dbus_string_init_const (&tmp, "standard-session-dirs.conf");
3760
3761 if (!_dbus_concat_dir_and_file (&full_path, &tmp))
3762 _dbus_assert_not_reached ("couldn't allocate full path");
3763
3764 #ifdef DBUS_WIN
3765 if (!_dbus_string_append (&install_root_based, DBUS_DATADIR) ||
3766 !_dbus_string_append (&install_root_based, "/dbus-1/services") ||
3767 !_dbus_replace_install_prefix (&install_root_based))
3768 goto out;
3769
3770 _dbus_assert (_dbus_path_is_absolute (&install_root_based));
3771
3772 test_session_service_dir_matches[0] = _dbus_string_get_const_data (
3773 &install_root_based);
3774
3775 common_progs = _dbus_getenv ("CommonProgramFiles");
3776
3777 if (common_progs)
3778 {
3779 if (!_dbus_string_append (&progs, common_progs))
3780 goto out;
3781
3782 if (!_dbus_string_append (&progs, "/dbus-1/services"))
3783 goto out;
3784
3785 test_session_service_dir_matches[1] = _dbus_string_get_const_data(&progs);
3786 }
3787 #else
3788 dbus_test_builddir = _dbus_getenv ("DBUS_TEST_BUILDDIR");
3789 xdg_data_home = _dbus_getenv ("XDG_DATA_HOME");
3790 xdg_runtime_dir = _dbus_getenv ("XDG_RUNTIME_DIR");
3791
3792 if (dbus_test_builddir == NULL || xdg_data_home == NULL ||
3793 xdg_runtime_dir == NULL)
3794 {
3795 printf ("Not testing default session service directories because a "
3796 "build-time testing environment variable is not set: "
3797 "see AM_TESTS_ENVIRONMENT in tests/Makefile.am\n");
3798 ret = TRUE;
3799 goto out;
3800 }
3801
3802 if (!_dbus_string_append (&data_dirs_based, dbus_test_builddir) ||
3803 !_dbus_string_append (&data_dirs_based, "/XDG_DATA_DIRS/dbus-1/services") ||
3804 !_dbus_string_append (&data_dirs_based2, dbus_test_builddir) ||
3805 !_dbus_string_append (&data_dirs_based2, "/XDG_DATA_DIRS2/dbus-1/services") ||
3806 !_dbus_string_append (&runtime_dir_based, xdg_runtime_dir) ||
3807 !_dbus_string_append (&data_home_based, xdg_data_home) ||
3808 !_dbus_string_append (&data_home_based, "/dbus-1/services"))
3809 _dbus_assert_not_reached ("out of memory");
3810
3811 if (!_dbus_ensure_directory (&runtime_dir_based, NULL))
3812 _dbus_assert_not_reached ("Unable to create fake XDG_RUNTIME_DIR");
3813
3814 if (!_dbus_string_append (&runtime_dir_based, "/dbus-1/services"))
3815 _dbus_assert_not_reached ("out of memory");
3816
3817 /* Sanity check: the Makefile sets this up. We assume that if this is
3818 * right, the XDG_DATA_DIRS will be too. */
3819 if (!_dbus_string_starts_with_c_str (&data_home_based, dbus_test_builddir))
3820 _dbus_assert_not_reached ("$XDG_DATA_HOME should start with $DBUS_TEST_BUILDDIR");
3821
3822 if (!_dbus_string_starts_with_c_str (&runtime_dir_based, dbus_test_builddir))
3823 _dbus_assert_not_reached ("$XDG_RUNTIME_DIR should start with $DBUS_TEST_BUILDDIR");
3824
3825 test_session_service_dir_matches[0] = _dbus_string_get_const_data (
3826 &runtime_dir_based);
3827 test_session_service_dir_matches[1] = _dbus_string_get_const_data (
3828 &data_home_based);
3829 test_session_service_dir_matches[2] = _dbus_string_get_const_data (
3830 &data_dirs_based);
3831 test_session_service_dir_matches[3] = _dbus_string_get_const_data (
3832 &data_dirs_based2);
3833 #endif
3834
3835 parser = bus_config_load (&full_path, TRUE, NULL, &error);
3836
3837 if (parser == NULL)
3838 _dbus_assert_not_reached (error.message);
3839
3840 dirs = bus_config_parser_get_service_dirs (parser);
3841
3842 for (link = _dbus_list_get_first_link (dirs), i = 0;
3843 link != NULL;
3844 link = _dbus_list_get_next_link (dirs, link), i++)
3845 {
3846 BusConfigServiceDir *dir = link->data;
3847 BusServiceDirFlags expected = BUS_SERVICE_DIR_FLAGS_NONE;
3848
3849 printf (" test service dir: '%s'\n", dir->path);
3850 printf (" current standard service dir: '%s'\n", test_session_service_dir_matches[i]);
3851 if (test_session_service_dir_matches[i] == NULL)
3852 {
3853 printf ("more directories parsed than in match set\n");
3854 goto out;
3855 }
3856
3857 if (strcmp (test_session_service_dir_matches[i], dir->path) != 0)
3858 {
3859 printf ("'%s' directory does not match '%s' in the match set\n",
3860 dir->path, test_session_service_dir_matches[i]);
3861 goto out;
3862 }
3863
3864 #ifndef DBUS_WIN
3865 /* On Unix we expect the first directory in the search path to be
3866 * in the XDG_RUNTIME_DIR, and we expect it to have special flags */
3867 if (i == 0)
3868 expected = (BUS_SERVICE_DIR_FLAGS_NO_WATCH |
3869 BUS_SERVICE_DIR_FLAGS_STRICT_NAMING);
3870 #endif
3871
3872 if (dir->flags != expected)
3873 {
3874 printf ("'%s' directory has flags 0x%x, should be 0x%x\n",
3875 dir->path, dir->flags, expected);
3876 goto out;
3877 }
3878 }
3879
3880 if (test_session_service_dir_matches[i] != NULL)
3881 {
3882 printf ("extra data %s in the match set was not matched\n",
3883 test_session_service_dir_matches[i]);
3884 goto out;
3885 }
3886
3887 if (!bus_config_parser_get_watched_dirs (parser, &watched_dirs))
3888 _dbus_assert_not_reached ("out of memory");
3889
3890 #ifdef DBUS_WIN
3891 /* We expect all directories to be watched (not that it matters on Windows,
3892 * because we don't know how) */
3893 i = 0;
3894 #else
3895 /* We expect all directories except the first to be watched, because
3896 * the first one is transient */
3897 i = 1;
3898 #endif
3899
3900 for (link = _dbus_list_get_first_link (&watched_dirs);
3901 link != NULL;
3902 link = _dbus_list_get_next_link (&watched_dirs, link), i++)
3903 {
3904 printf (" watched service dir: '%s'\n", (const char *) link->data);
3905 printf (" current standard service dir: '%s'\n",
3906 test_session_service_dir_matches[i]);
3907
3908 if (test_session_service_dir_matches[i] == NULL)
3909 {
3910 printf ("more directories parsed than in match set\n");
3911 goto out;
3912 }
3913
3914 if (strcmp (test_session_service_dir_matches[i],
3915 (const char *) link->data) != 0)
3916 {
3917 printf ("'%s' directory does not match '%s' in the match set\n",
3918 (const char *) link->data,
3919 test_session_service_dir_matches[i]);
3920 goto out;
3921 }
3922 }
3923
3924 if (test_session_service_dir_matches[i] != NULL)
3925 {
3926 printf ("extra data %s in the match set was not matched\n",
3927 test_session_service_dir_matches[i]);
3928 goto out;
3929 }
3930
3931 ret = TRUE;
3932
3933 out:
3934 if (parser != NULL)
3935 bus_config_parser_unref (parser);
3936
3937 _dbus_list_clear (&watched_dirs);
3938 _dbus_string_free (&full_path);
3939 _dbus_string_free (&install_root_based);
3940 _dbus_string_free (&progs);
3941 _dbus_string_free (&runtime_dir_based);
3942 _dbus_string_free (&data_home_based);
3943 _dbus_string_free (&data_dirs_based);
3944 _dbus_string_free (&data_dirs_based2);
3945 return ret;
3946 }
3947
3948 #ifndef DBUS_WIN
3949 static const char *test_system_service_dir_matches[] =
3950 {
3951 "/usr/local/share/dbus-1/system-services",
3952 "/usr/share/dbus-1/system-services",
3953 DBUS_DATADIR"/dbus-1/system-services",
3954 "/lib/dbus-1/system-services",
3955 NULL
3956 };
3957
3958 static dbus_bool_t
test_default_system_servicedirs(void)3959 test_default_system_servicedirs (void)
3960 {
3961 DBusList *dirs;
3962 DBusList *link;
3963 int i;
3964
3965 dirs = NULL;
3966
3967 if (!_dbus_get_standard_system_servicedirs (&dirs))
3968 _dbus_assert_not_reached ("couldn't get stardard dirs");
3969
3970 /* make sure we read and parse the env variable correctly */
3971 i = 0;
3972 while ((link = _dbus_list_pop_first_link (&dirs)))
3973 {
3974 printf (" test service dir: %s\n", (char *)link->data);
3975 if (test_system_service_dir_matches[i] == NULL)
3976 {
3977 printf ("more directories parsed than in match set\n");
3978 dbus_free (link->data);
3979 _dbus_list_free_link (link);
3980 return FALSE;
3981 }
3982
3983 if (strcmp (test_system_service_dir_matches[i],
3984 (char *)link->data) != 0)
3985 {
3986 printf ("%s directory does not match %s in the match set\n",
3987 (char *)link->data,
3988 test_system_service_dir_matches[i]);
3989 dbus_free (link->data);
3990 _dbus_list_free_link (link);
3991 return FALSE;
3992 }
3993
3994 ++i;
3995
3996 dbus_free (link->data);
3997 _dbus_list_free_link (link);
3998 }
3999
4000 if (test_system_service_dir_matches[i] != NULL)
4001 {
4002 printf ("extra data %s in the match set was not matched\n",
4003 test_system_service_dir_matches[i]);
4004
4005 return FALSE;
4006 }
4007
4008 return TRUE;
4009 }
4010 #endif
4011
4012 dbus_bool_t
bus_config_parser_test(const DBusString * test_data_dir)4013 bus_config_parser_test (const DBusString *test_data_dir)
4014 {
4015 if (test_data_dir == NULL ||
4016 _dbus_string_get_length (test_data_dir) == 0)
4017 {
4018 printf ("No test data\n");
4019 return TRUE;
4020 }
4021
4022 if (!test_default_session_servicedirs (test_data_dir))
4023 return FALSE;
4024
4025 #ifdef DBUS_WIN
4026 printf("default system service dir skipped\n");
4027 #else
4028 if (!test_default_system_servicedirs())
4029 return FALSE;
4030 #endif
4031
4032 if (!process_test_valid_subdir (test_data_dir, "valid-config-files", VALID))
4033 return FALSE;
4034
4035 #ifndef DBUS_WIN
4036 if (!process_test_valid_subdir (test_data_dir, "valid-config-files-system", VALID))
4037 return FALSE;
4038 #endif
4039
4040 if (!process_test_valid_subdir (test_data_dir, "invalid-config-files", INVALID))
4041 return FALSE;
4042
4043 if (!process_test_equiv_subdir (test_data_dir, "equiv-config-files"))
4044 return FALSE;
4045
4046 return TRUE;
4047 }
4048
4049 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */
4050
4051