1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* config-parser.c XML-library-agnostic configuration file parser
4 * Copyright (C) 2003, 2004 Red Hat, Inc.
6 * Licensed under the Academic Free License version 2.1
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.
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.
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
25 #include "config-parser-common.h"
26 #include "config-parser.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>
40 /* we ignore policies for unknown groups/users */
55 unsigned int had_content : 1;
61 unsigned int ignore_missing : 1;
62 unsigned int if_selinux_enabled : 1;
63 unsigned int selinux_root_relative : 1;
69 unsigned long gid_uid_or_at_console;
83 * Parser for bus configuration file.
85 struct BusConfigParser
87 int refcount; /**< Reference count */
89 DBusString basedir; /**< Directory we resolve paths relative to */
91 DBusList *stack; /**< stack of Element */
93 char *user; /**< user to run as */
95 char *servicehelper; /**< location of the setuid helper */
97 char *bus_type; /**< Message bus type */
99 DBusList *listen_on; /**< List of addresses to listen to */
101 DBusList *mechanisms; /**< Auth mechanisms */
103 DBusList *service_dirs; /**< BusConfigServiceDirs to look for session services in */
105 DBusList *conf_dirs; /**< Directories to look for policy configuration in */
107 BusPolicy *policy; /**< Security policy */
109 BusLimits limits; /**< Limits */
111 char *pidfile; /**< PID file */
113 DBusList *included_files; /**< Included files stack */
115 DBusHashTable *service_context_table; /**< Map service names to SELinux contexts */
117 unsigned int fork : 1; /**< TRUE to fork into daemon mode */
119 unsigned int syslog : 1; /**< TRUE to enable syslog */
120 unsigned int keep_umask : 1; /**< TRUE to keep original umask when forking */
122 unsigned int is_toplevel : 1; /**< FALSE if we are a sub-config-file inside another one */
124 unsigned int allow_anonymous : 1; /**< TRUE to allow anonymous connections */
128 push_element (BusConfigParser *parser,
133 _dbus_assert (type != ELEMENT_NONE);
135 e = dbus_new0 (Element, 1);
139 if (!_dbus_list_append (&parser->stack, e))
151 element_free (Element *e)
153 if (e->type == ELEMENT_LIMIT)
154 dbus_free (e->d.limit.name);
160 pop_element (BusConfigParser *parser)
164 e = _dbus_list_pop_last (&parser->stack);
170 peek_element (BusConfigParser *parser)
174 e = _dbus_list_get_last (&parser->stack);
180 top_element_type (BusConfigParser *parser)
184 e = _dbus_list_get_last (&parser->stack);
193 merge_service_context_hash (DBusHashTable *dest,
203 _dbus_hash_iter_init (from, &iter);
204 while (_dbus_hash_iter_next (&iter))
206 const char *service = _dbus_hash_iter_get_string_key (&iter);
207 const char *context = _dbus_hash_iter_get_value (&iter);
209 service_copy = _dbus_strdup (service);
210 if (service_copy == NULL)
212 context_copy = _dbus_strdup (context);
213 if (context_copy == NULL)
216 if (!_dbus_hash_table_insert_string (dest, service_copy, context_copy))
227 dbus_free (service_copy);
230 dbus_free (context_copy);
236 * Create a new BusConfigServiceDir. Takes ownership of path if #TRUE is returned.
238 * @returns #FALSE on OOM
240 static BusConfigServiceDir *
241 bus_config_service_dir_new_take (char *path,
242 BusServiceDirFlags flags)
244 BusConfigServiceDir *self = dbus_new0 (BusConfigServiceDir, 1);
249 self->path = path; /* take ownership */
255 bus_config_service_dir_free (BusConfigServiceDir *self)
257 dbus_free (self->path);
261 static BusConfigServiceDir *
262 service_dirs_find_dir (DBusList **service_dirs,
267 _dbus_assert (dir != NULL);
269 for (link = *service_dirs; link; link = _dbus_list_get_next_link(service_dirs, link))
271 BusConfigServiceDir *link_dir = link->data;
273 if (strcmp (dir, link_dir->path) == 0)
281 service_dirs_append_link_unique_or_free (DBusList **service_dirs,
284 BusConfigServiceDir *dir = dir_link->data;
285 BusConfigServiceDir *already = service_dirs_find_dir (service_dirs,
290 _dbus_list_append_link (service_dirs, dir_link);
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);
306 * Consume links from dirs (a list of paths), converting them into links in
307 * service_dirs (a list of unique BusServiceDir).
309 * On success, return TRUE. dirs will be empty, and every original entry in
310 * dirs will have a corresponding service_dirs entry.
312 * On OOM, return FALSE. Each original entry of dirs has either been
313 * appended to service_dirs or left in dirs.
316 service_dirs_absorb_string_list (DBusList **service_dirs,
318 BusServiceDirFlags flags)
322 _dbus_assert (service_dirs != NULL);
323 _dbus_assert (dirs != NULL);
325 while ((link = _dbus_list_pop_first_link (dirs)))
327 char *path = link->data;
328 BusConfigServiceDir *dir = bus_config_service_dir_new_take (path, flags);
332 /* OOM - roll back (this does not need to allocate memory) */
333 _dbus_list_prepend_link (service_dirs, link);
337 /* Ownership of path has been taken by dir */
339 service_dirs_append_link_unique_or_free (service_dirs, link);
342 _dbus_assert (*dirs == NULL);
347 merge_included (BusConfigParser *parser,
348 BusConfigParser *included,
353 if (!bus_policy_merge (parser->policy,
360 if (!merge_service_context_hash (parser->service_context_table,
361 included->service_context_table))
367 if (included->user != NULL)
369 dbus_free (parser->user);
370 parser->user = included->user;
371 included->user = NULL;
374 if (included->bus_type != NULL)
376 dbus_free (parser->bus_type);
377 parser->bus_type = included->bus_type;
378 included->bus_type = NULL;
384 if (included->keep_umask)
385 parser->keep_umask = TRUE;
387 if (included->allow_anonymous)
388 parser->allow_anonymous = TRUE;
390 if (included->pidfile != NULL)
392 dbus_free (parser->pidfile);
393 parser->pidfile = included->pidfile;
394 included->pidfile = NULL;
397 if (included->servicehelper != NULL)
399 dbus_free (parser->servicehelper);
400 parser->servicehelper = included->servicehelper;
401 included->servicehelper = NULL;
404 while ((link = _dbus_list_pop_first_link (&included->listen_on)))
405 _dbus_list_append_link (&parser->listen_on, link);
407 while ((link = _dbus_list_pop_first_link (&included->mechanisms)))
408 _dbus_list_append_link (&parser->mechanisms, link);
410 while ((link = _dbus_list_pop_first_link (&included->service_dirs)))
411 service_dirs_append_link_unique_or_free (&parser->service_dirs, link);
413 while ((link = _dbus_list_pop_first_link (&included->conf_dirs)))
414 _dbus_list_append_link (&parser->conf_dirs, link);
420 seen_include (BusConfigParser *parser,
421 const DBusString *file)
425 iter = parser->included_files;
428 if (! strcmp (_dbus_string_get_const_data (file), iter->data))
431 iter = _dbus_list_get_next_link (&parser->included_files, iter);
438 bus_config_parser_new (const DBusString *basedir,
439 dbus_bool_t is_toplevel,
440 const BusConfigParser *parent)
442 BusConfigParser *parser;
444 parser = dbus_new0 (BusConfigParser, 1);
448 parser->is_toplevel = !!is_toplevel;
450 if (!_dbus_string_init (&parser->basedir))
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,
460 dbus_free)) == NULL))
463 bus_policy_unref (parser->policy);
465 _dbus_string_free (&parser->basedir);
473 /* Initialize the parser's limits from the parent. */
474 parser->limits = parent->limits;
476 /* Use the parent's list of included_files to avoid
477 circular inclusions. */
478 parser->included_files = parent->included_files;
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;
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
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;
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.
501 parser->limits.activation_timeout = 25000; /* 25 seconds */
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.
507 parser->limits.auth_timeout = 30000; /* 30 seconds */
509 /* Do not allow a fd to stay forever in dbus-daemon
510 * https://bugs.freedesktop.org/show_bug.cgi?id=80559
512 parser->limits.pending_fd_timeout = 150000; /* 2.5 minutes */
514 parser->limits.max_incomplete_connections = 64;
515 parser->limits.max_connections_per_user = 256;
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.
521 parser->limits.max_completed_connections = 2048;
523 parser->limits.max_pending_activations = 512;
524 parser->limits.max_services_per_connection = 512;
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
534 parser->limits.max_match_rules_per_connection = 512;
536 parser->limits.reply_timeout = -1; /* never */
538 /* this is effectively a limit on message queue size for messages
539 * that require a reply
541 parser->limits.max_replies_per_connection = 128;
544 parser->refcount = 1;
550 bus_config_parser_ref (BusConfigParser *parser)
552 _dbus_assert (parser->refcount > 0);
554 parser->refcount += 1;
560 bus_config_parser_unref (BusConfigParser *parser)
562 _dbus_assert (parser->refcount > 0);
564 parser->refcount -= 1;
566 if (parser->refcount == 0)
568 while (parser->stack != NULL)
569 pop_element (parser);
571 dbus_free (parser->user);
572 dbus_free (parser->servicehelper);
573 dbus_free (parser->bus_type);
574 dbus_free (parser->pidfile);
576 _dbus_list_foreach (&parser->listen_on,
577 (DBusForeachFunction) dbus_free,
580 _dbus_list_clear (&parser->listen_on);
582 _dbus_list_foreach (&parser->service_dirs,
583 (DBusForeachFunction) bus_config_service_dir_free,
586 _dbus_list_clear (&parser->service_dirs);
588 _dbus_list_foreach (&parser->conf_dirs,
589 (DBusForeachFunction) dbus_free,
592 _dbus_list_clear (&parser->conf_dirs);
594 _dbus_list_foreach (&parser->mechanisms,
595 (DBusForeachFunction) dbus_free,
598 _dbus_list_clear (&parser->mechanisms);
600 _dbus_string_free (&parser->basedir);
603 bus_policy_unref (parser->policy);
605 if (parser->service_context_table)
606 _dbus_hash_table_unref (parser->service_context_table);
613 bus_config_parser_check_doctype (BusConfigParser *parser,
617 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
619 if (strcmp (doctype, "busconfig") != 0)
621 dbus_set_error (error,
623 "Configuration file has the wrong document type %s",
638 locate_attributes (BusConfigParser *parser,
639 const char *element_name,
640 const char **attribute_names,
641 const char **attribute_values,
643 const char *first_attribute_name,
644 const char **first_attribute_retloc,
652 LocateAttr attrs[MAX_ATTRS];
656 _dbus_assert (first_attribute_name != NULL);
657 _dbus_assert (first_attribute_retloc != NULL);
662 attrs[0].name = first_attribute_name;
663 attrs[0].retloc = first_attribute_retloc;
664 *first_attribute_retloc = NULL;
666 va_start (args, first_attribute_retloc);
668 name = va_arg (args, const char*);
669 retloc = va_arg (args, const char**);
673 _dbus_assert (retloc != NULL);
674 _dbus_assert (n_attrs < MAX_ATTRS);
676 attrs[n_attrs].name = name;
677 attrs[n_attrs].retloc = retloc;
681 name = va_arg (args, const char*);
682 retloc = va_arg (args, const char**);
688 while (attribute_names[i])
697 if (strcmp (attrs[j].name, attribute_names[i]) == 0)
699 retloc = attrs[j].retloc;
703 dbus_set_error (error, DBUS_ERROR_FAILED,
704 "Attribute \"%s\" repeated twice on the same <%s> element",
705 attrs[j].name, element_name);
710 *retloc = attribute_values[i];
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);
734 check_no_attributes (BusConfigParser *parser,
735 const char *element_name,
736 const char **attribute_names,
737 const char **attribute_values,
740 if (attribute_names[0] != NULL)
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);
752 start_busconfig_child (BusConfigParser *parser,
753 const char *element_name,
754 const char **attribute_names,
755 const char **attribute_values,
758 ElementType element_type;
760 element_type = bus_config_parser_element_name_to_type (element_name);
762 if (element_type == ELEMENT_USER)
764 if (!check_no_attributes (parser, "user", attribute_names, attribute_values, error))
767 if (push_element (parser, ELEMENT_USER) == NULL)
775 else if (element_type == ELEMENT_CONFIGTYPE)
777 if (!check_no_attributes (parser, "type", attribute_names, attribute_values, error))
780 if (push_element (parser, ELEMENT_CONFIGTYPE) == NULL)
788 else if (element_type == ELEMENT_FORK)
790 if (!check_no_attributes (parser, "fork", attribute_names, attribute_values, error))
793 if (push_element (parser, ELEMENT_FORK) == NULL)
803 else if (element_type == ELEMENT_SYSLOG)
805 if (!check_no_attributes (parser, "syslog", attribute_names, attribute_values, error))
808 if (push_element (parser, ELEMENT_SYSLOG) == NULL)
814 parser->syslog = TRUE;
818 else if (element_type == ELEMENT_KEEP_UMASK)
820 if (!check_no_attributes (parser, "keep_umask", attribute_names, attribute_values, error))
823 if (push_element (parser, ELEMENT_KEEP_UMASK) == NULL)
829 parser->keep_umask = TRUE;
833 else if (element_type == ELEMENT_PIDFILE)
835 if (!check_no_attributes (parser, "pidfile", attribute_names, attribute_values, error))
838 if (push_element (parser, ELEMENT_PIDFILE) == NULL)
846 else if (element_type == ELEMENT_LISTEN)
848 if (!check_no_attributes (parser, "listen", attribute_names, attribute_values, error))
851 if (push_element (parser, ELEMENT_LISTEN) == NULL)
859 else if (element_type == ELEMENT_AUTH)
861 if (!check_no_attributes (parser, "auth", attribute_names, attribute_values, error))
864 if (push_element (parser, ELEMENT_AUTH) == NULL)
872 else if (element_type == ELEMENT_SERVICEHELPER)
874 if (!check_no_attributes (parser, "servicehelper", attribute_names, attribute_values, error))
877 if (push_element (parser, ELEMENT_SERVICEHELPER) == NULL)
885 else if (element_type == ELEMENT_INCLUDEDIR)
887 if (!check_no_attributes (parser, "includedir", attribute_names, attribute_values, error))
890 if (push_element (parser, ELEMENT_INCLUDEDIR) == NULL)
898 else if (element_type == ELEMENT_STANDARD_SESSION_SERVICEDIRS)
900 DBusError local_error = DBUS_ERROR_INIT;
901 DBusList *dirs = NULL;
903 if (!check_no_attributes (parser, "standard_session_servicedirs", attribute_names, attribute_values, error))
906 if (push_element (parser, ELEMENT_STANDARD_SESSION_SERVICEDIRS) == NULL)
912 if (_dbus_set_up_transient_session_servicedirs (&dirs, &local_error))
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))
919 _dbus_list_foreach (&dirs, (DBusForeachFunction) dbus_free,
921 _dbus_list_clear (&dirs);
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);
933 _dbus_assert (dirs == NULL);
935 if (!_dbus_get_standard_session_servicedirs (&dirs))
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))
948 _dbus_list_foreach (&dirs, (DBusForeachFunction) dbus_free,
950 _dbus_list_clear (&dirs);
954 _dbus_assert (dirs == NULL);
958 else if (element_type == ELEMENT_STANDARD_SYSTEM_SERVICEDIRS)
963 if (!check_no_attributes (parser, "standard_system_servicedirs", attribute_names, attribute_values, error))
966 if (push_element (parser, ELEMENT_STANDARD_SYSTEM_SERVICEDIRS) == NULL)
972 if (!_dbus_get_standard_system_servicedirs (&dirs))
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))
986 _dbus_list_foreach (&dirs, (DBusForeachFunction) dbus_free,
988 _dbus_list_clear (&dirs);
994 else if (element_type == ELEMENT_ALLOW_ANONYMOUS)
996 if (!check_no_attributes (parser, "allow_anonymous", attribute_names, attribute_values, error))
999 if (push_element (parser, ELEMENT_ALLOW_ANONYMOUS) == NULL)
1001 BUS_SET_OOM (error);
1005 parser->allow_anonymous = TRUE;
1008 else if (element_type == ELEMENT_SERVICEDIR)
1010 if (!check_no_attributes (parser, "servicedir", attribute_names, attribute_values, error))
1013 if (push_element (parser, ELEMENT_SERVICEDIR) == NULL)
1015 BUS_SET_OOM (error);
1021 else if (element_type == ELEMENT_INCLUDE)
1024 const char *if_selinux_enabled;
1025 const char *ignore_missing;
1026 const char *selinux_root_relative;
1028 if ((e = push_element (parser, ELEMENT_INCLUDE)) == NULL)
1030 BUS_SET_OOM (error);
1034 e->d.include.ignore_missing = FALSE;
1035 e->d.include.if_selinux_enabled = FALSE;
1036 e->d.include.selinux_root_relative = FALSE;
1038 if (!locate_attributes (parser, "include",
1042 "ignore_missing", &ignore_missing,
1043 "if_selinux_enabled", &if_selinux_enabled,
1044 "selinux_root_relative", &selinux_root_relative,
1048 if (ignore_missing != NULL)
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;
1056 dbus_set_error (error, DBUS_ERROR_FAILED,
1057 "ignore_missing attribute must have value \"yes\" or \"no\"");
1062 if (if_selinux_enabled != NULL)
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;
1070 dbus_set_error (error, DBUS_ERROR_FAILED,
1071 "if_selinux_enabled attribute must have value"
1072 " \"yes\" or \"no\"");
1077 if (selinux_root_relative != NULL)
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;
1085 dbus_set_error (error, DBUS_ERROR_FAILED,
1086 "selinux_root_relative attribute must have value"
1087 " \"yes\" or \"no\"");
1094 else if (element_type == ELEMENT_POLICY)
1097 const char *context;
1100 const char *at_console;
1102 if ((e = push_element (parser, ELEMENT_POLICY)) == NULL)
1104 BUS_SET_OOM (error);
1108 e->d.policy.type = POLICY_IGNORED;
1110 if (!locate_attributes (parser, "policy",
1114 "context", &context,
1117 "at_console", &at_console,
1121 if (((context && user) ||
1122 (context && group) ||
1123 (context && at_console)) ||
1125 (user && at_console)) ||
1126 (group && at_console) ||
1127 !(context || user || group || at_console))
1129 dbus_set_error (error, DBUS_ERROR_FAILED,
1130 "<policy> element must have exactly one of (context|user|group|at_console) attributes");
1134 if (context != NULL)
1136 if (strcmp (context, "default") == 0)
1138 e->d.policy.type = POLICY_DEFAULT;
1140 else if (strcmp (context, "mandatory") == 0)
1142 e->d.policy.type = POLICY_MANDATORY;
1146 dbus_set_error (error, DBUS_ERROR_FAILED,
1147 "context attribute on <policy> must have the value \"default\" or \"mandatory\", not \"%s\"",
1152 else if (user != NULL)
1154 DBusString username;
1155 _dbus_string_init_const (&username, user);
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;
1161 _dbus_warn ("Unknown username \"%s\" in message bus configuration file",
1164 else if (group != NULL)
1166 DBusString group_name;
1167 _dbus_string_init_const (&group_name, group);
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;
1173 _dbus_warn ("Unknown group \"%s\" in message bus configuration file",
1176 else if (at_console != NULL)
1179 t = (strcmp (at_console, "true") == 0);
1180 if (t || strcmp (at_console, "false") == 0)
1182 e->d.policy.gid_uid_or_at_console = t;
1183 e->d.policy.type = POLICY_CONSOLE;
1187 dbus_set_error (error, DBUS_ERROR_FAILED,
1188 "Unknown value \"%s\" for at_console in message bus configuration file",
1196 _dbus_assert_not_reached ("all <policy> attributes null and we didn't set error");
1201 else if (element_type == ELEMENT_LIMIT)
1206 if ((e = push_element (parser, ELEMENT_LIMIT)) == NULL)
1208 BUS_SET_OOM (error);
1212 if (!locate_attributes (parser, "limit",
1222 dbus_set_error (error, DBUS_ERROR_FAILED,
1223 "<limit> element must have a \"name\" attribute");
1227 e->d.limit.name = _dbus_strdup (name);
1228 if (e->d.limit.name == NULL)
1230 BUS_SET_OOM (error);
1236 else if (element_type == ELEMENT_SELINUX)
1238 if (!check_no_attributes (parser, "selinux", attribute_names, attribute_values, error))
1241 if (push_element (parser, ELEMENT_SELINUX) == NULL)
1243 BUS_SET_OOM (error);
1249 else if (element_type == ELEMENT_APPARMOR)
1254 if ((e = push_element (parser, ELEMENT_APPARMOR)) == NULL)
1256 BUS_SET_OOM (error);
1260 if (!locate_attributes (parser, "apparmor",
1268 return bus_apparmor_set_mode_from_config (mode, error);
1272 dbus_set_error (error, DBUS_ERROR_FAILED,
1273 "Element <%s> not allowed inside <%s> in configuration file",
1274 element_name, "busconfig");
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).
1286 parse_int_attribute (const char *name,
1287 const char *content,
1294 DBusString parse_string;
1298 if (content == NULL)
1301 _dbus_string_init_const (&parse_string, content);
1303 if (!_dbus_string_parse_int (&parse_string, 0, value, NULL) ||
1304 *value < min || *value > max)
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);
1317 append_rule_from_element (BusConfigParser *parser,
1318 const char *element_name,
1319 const char **attribute_names,
1320 const char **attribute_values,
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;
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;
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;
1355 /* TRUE if any message-matching modifier is present */
1356 dbus_bool_t any_message_attribute;
1358 /* Non-message-related attributes */
1360 const char *own_prefix;
1364 BusPolicyRule *rule;
1366 if (!locate_attributes (parser, element_name,
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,
1389 "own_prefix", &own_prefix,
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);
1423 if (!(any_send_attribute ||
1424 any_receive_attribute ||
1425 own || own_prefix || user || group))
1427 dbus_set_error (error, DBUS_ERROR_FAILED,
1428 "Element <%s> must have one or more attributes",
1433 if ((send_member && (send_interface == NULL && send_path == NULL)) ||
1434 (receive_member && (receive_interface == NULL && receive_path == NULL)))
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.",
1442 /* Allowed combinations of elements are:
1444 * base, must be all send or all receive:
1447 * interface + member
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
1453 * user, group, own, own_prefix must occur alone
1456 if (any_message_attribute +
1458 (own_prefix != NULL) +
1460 (group != NULL)) > 1)
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",
1470 if (any_send_attribute && any_receive_attribute)
1472 dbus_set_error (error, DBUS_ERROR_FAILED,
1473 "Invalid combination of attributes on element <%s>: "
1474 "send and receive attributes cannot be combined",
1479 if ((send_member != NULL || send_interface != NULL) && send_error != NULL)
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 "
1489 if ((receive_member != NULL || receive_interface != NULL) &&
1490 receive_error != NULL)
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",
1502 /* In BusPolicyRule, NULL represents wildcard.
1503 * In the config file, '*' represents it.
1505 #define IS_WILDCARD(str) ((str) && ((str)[0]) == '*' && ((str)[1]) == '\0')
1507 if (any_send_attribute)
1511 if (IS_WILDCARD (send_interface))
1512 send_interface = NULL;
1513 if (IS_WILDCARD (send_member))
1515 if (IS_WILDCARD (send_error))
1517 if (IS_WILDCARD (send_destination))
1518 send_destination = NULL;
1519 if (IS_WILDCARD (send_path))
1521 if (IS_WILDCARD (send_type))
1524 message_type = DBUS_MESSAGE_TYPE_INVALID;
1525 if (send_type != NULL)
1527 message_type = dbus_message_type_from_string (send_type);
1528 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1530 dbus_set_error (error, DBUS_ERROR_FAILED,
1531 "Bad message type \"%s\"",
1538 !(strcmp (eavesdrop, "true") == 0 ||
1539 strcmp (eavesdrop, "false") == 0))
1541 dbus_set_error (error, DBUS_ERROR_FAILED,
1542 "Bad value \"%s\" for %s attribute, must be true or false",
1543 "eavesdrop", eavesdrop);
1547 if (send_broadcast &&
1548 !(strcmp (send_broadcast, "true") == 0 ||
1549 strcmp (send_broadcast, "false") == 0))
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");
1557 if (send_destination != NULL &&
1558 send_broadcast != NULL &&
1559 strcmp (send_broadcast, "true") == 0)
1561 /* Broadcast messages have no destination, so this cannot
1563 dbus_set_error (error, DBUS_ERROR_FAILED,
1564 "Rule with send_broadcast=\"true\" and "
1565 "send_destination=\"%s\" cannot match anything",
1570 if (send_requested_reply &&
1571 !(strcmp (send_requested_reply, "true") == 0 ||
1572 strcmp (send_requested_reply, "false") == 0))
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);
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,
1587 !parse_int_attribute ("min_fds", min_fds_attr,
1588 0, DBUS_MAXIMUM_MESSAGE_UNIX_FDS, 0, &min_fds,
1592 rule = bus_policy_rule_new (BUS_POLICY_RULE_SEND, allow);
1597 rule->d.send.eavesdrop = (strcmp (eavesdrop, "true") == 0);
1600 rule->d.send.log = (strcmp (log, "true") == 0);
1602 if (send_requested_reply)
1603 rule->d.send.requested_reply = (strcmp (send_requested_reply, "true") == 0);
1607 if (strcmp (send_broadcast, "true") == 0)
1608 rule->d.send.broadcast = BUS_POLICY_TRISTATE_TRUE;
1610 rule->d.send.broadcast = BUS_POLICY_TRISTATE_FALSE;
1614 rule->d.send.broadcast = BUS_POLICY_TRISTATE_ANY;
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;
1626 if (send_path && rule->d.send.path == NULL)
1628 if (send_interface && rule->d.send.interface == NULL)
1630 if (send_member && rule->d.send.member == NULL)
1632 if (send_error && rule->d.send.error == NULL)
1634 if (send_destination && rule->d.send.destination == NULL)
1637 else if (any_receive_attribute)
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;
1654 message_type = DBUS_MESSAGE_TYPE_INVALID;
1655 if (receive_type != NULL)
1657 message_type = dbus_message_type_from_string (receive_type);
1658 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1660 dbus_set_error (error, DBUS_ERROR_FAILED,
1661 "Bad message type \"%s\"",
1669 !(strcmp (eavesdrop, "true") == 0 ||
1670 strcmp (eavesdrop, "false") == 0))
1672 dbus_set_error (error, DBUS_ERROR_FAILED,
1673 "Bad value \"%s\" for %s attribute, must be true or false",
1674 "eavesdrop", eavesdrop);
1678 if (receive_requested_reply &&
1679 !(strcmp (receive_requested_reply, "true") == 0 ||
1680 strcmp (receive_requested_reply, "false") == 0))
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);
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,
1692 !parse_int_attribute ("min_fds", min_fds_attr,
1693 0, DBUS_MAXIMUM_MESSAGE_UNIX_FDS, 0, &min_fds,
1697 rule = bus_policy_rule_new (BUS_POLICY_RULE_RECEIVE, allow);
1702 rule->d.receive.eavesdrop = (strcmp (eavesdrop, "true") == 0);
1704 if (receive_requested_reply)
1705 rule->d.receive.requested_reply = (strcmp (receive_requested_reply, "true") == 0);
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;
1716 if (receive_path && rule->d.receive.path == NULL)
1718 if (receive_interface && rule->d.receive.interface == NULL)
1720 if (receive_member && rule->d.receive.member == NULL)
1722 if (receive_error && rule->d.receive.error == NULL)
1724 if (receive_sender && rule->d.receive.origin == NULL)
1727 else if (own || own_prefix)
1729 rule = bus_policy_rule_new (BUS_POLICY_RULE_OWN, allow);
1735 if (IS_WILDCARD (own))
1738 rule->d.own.prefix = 0;
1739 rule->d.own.service_name = _dbus_strdup (own);
1740 if (own && rule->d.own.service_name == NULL)
1745 rule->d.own.prefix = 1;
1746 rule->d.own.service_name = _dbus_strdup (own_prefix);
1747 if (rule->d.own.service_name == NULL)
1753 if (IS_WILDCARD (user))
1755 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1759 rule->d.user.uid = DBUS_UID_UNSET;
1763 DBusString username;
1766 _dbus_string_init_const (&username, user);
1768 if (_dbus_parse_unix_user_from_config (&username, &uid))
1770 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1774 rule->d.user.uid = uid;
1778 _dbus_warn ("Unknown username \"%s\" on element <%s>",
1779 user, element_name);
1785 if (IS_WILDCARD (group))
1787 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1791 rule->d.group.gid = DBUS_GID_UNSET;
1795 DBusString groupname;
1798 _dbus_string_init_const (&groupname, group);
1800 if (_dbus_parse_unix_group_from_config (&groupname, &gid))
1802 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1806 rule->d.group.gid = gid;
1810 _dbus_warn ("Unknown group \"%s\" on element <%s>",
1811 group, element_name);
1816 _dbus_assert_not_reached ("Did not handle some combination of attributes on <allow> or <deny>");
1822 pe = peek_element (parser);
1823 _dbus_assert (pe != NULL);
1824 _dbus_assert (pe->type == ELEMENT_POLICY);
1826 switch (pe->d.policy.type)
1828 case POLICY_IGNORED:
1830 /* drop the rule on the floor */
1833 case POLICY_DEFAULT:
1834 if (!bus_policy_append_default_rule (parser->policy, rule))
1837 case POLICY_MANDATORY:
1838 if (!bus_policy_append_mandatory_rule (parser->policy, rule))
1842 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1844 dbus_set_error (error, DBUS_ERROR_FAILED,
1845 "<%s> rule cannot be per-user because it has bus-global semantics",
1850 if (!bus_policy_append_user_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1855 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1857 dbus_set_error (error, DBUS_ERROR_FAILED,
1858 "<%s> rule cannot be per-group because it has bus-global semantics",
1863 if (!bus_policy_append_group_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1869 case POLICY_CONSOLE:
1870 if (!bus_policy_append_console_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1876 bus_policy_rule_unref (rule);
1883 BUS_SET_OOM (error);
1886 bus_policy_rule_unref (rule);
1891 start_policy_child (BusConfigParser *parser,
1892 const char *element_name,
1893 const char **attribute_names,
1894 const char **attribute_values,
1897 if (strcmp (element_name, "allow") == 0)
1899 if (!append_rule_from_element (parser, element_name,
1900 attribute_names, attribute_values,
1904 if (push_element (parser, ELEMENT_ALLOW) == NULL)
1906 BUS_SET_OOM (error);
1912 else if (strcmp (element_name, "deny") == 0)
1914 if (!append_rule_from_element (parser, element_name,
1915 attribute_names, attribute_values,
1919 if (push_element (parser, ELEMENT_DENY) == NULL)
1921 BUS_SET_OOM (error);
1929 dbus_set_error (error, DBUS_ERROR_FAILED,
1930 "Element <%s> not allowed inside <%s> in configuration file",
1931 element_name, "policy");
1937 start_selinux_child (BusConfigParser *parser,
1938 const char *element_name,
1939 const char **attribute_names,
1940 const char **attribute_values,
1947 context_copy = NULL;
1949 if (strcmp (element_name, "associate") == 0)
1952 const char *context;
1954 if (!locate_attributes (parser, "associate",
1959 "context", &context,
1963 if (push_element (parser, ELEMENT_ASSOCIATE) == NULL)
1965 BUS_SET_OOM (error);
1969 if (own == NULL || context == NULL)
1971 dbus_set_error (error, DBUS_ERROR_FAILED,
1972 "Element <associate> must have attributes own=\"<servicename>\" and context=\"<selinux context>\"");
1976 own_copy = _dbus_strdup (own);
1977 if (own_copy == NULL)
1979 context_copy = _dbus_strdup (context);
1980 if (context_copy == NULL)
1983 if (!_dbus_hash_table_insert_string (parser->service_context_table,
1984 own_copy, context_copy))
1991 dbus_set_error (error, DBUS_ERROR_FAILED,
1992 "Element <%s> not allowed inside <%s> in configuration file",
1993 element_name, "selinux");
1999 dbus_free (own_copy);
2002 dbus_free (context_copy);
2004 BUS_SET_OOM (error);
2009 bus_config_parser_start_element (BusConfigParser *parser,
2010 const char *element_name,
2011 const char **attribute_names,
2012 const char **attribute_values,
2017 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2019 /* printf ("START: %s\n", element_name); */
2021 t = top_element_type (parser);
2023 if (t == ELEMENT_NONE)
2025 if (strcmp (element_name, "busconfig") == 0)
2027 if (!check_no_attributes (parser, "busconfig", attribute_names, attribute_values, error))
2030 if (push_element (parser, ELEMENT_BUSCONFIG) == NULL)
2032 BUS_SET_OOM (error);
2040 dbus_set_error (error, DBUS_ERROR_FAILED,
2041 "Unknown element <%s> at root of configuration file",
2046 else if (t == ELEMENT_BUSCONFIG)
2048 return start_busconfig_child (parser, element_name,
2049 attribute_names, attribute_values,
2052 else if (t == ELEMENT_POLICY)
2054 return start_policy_child (parser, element_name,
2055 attribute_names, attribute_values,
2058 else if (t == ELEMENT_SELINUX)
2060 return start_selinux_child (parser, element_name,
2061 attribute_names, attribute_values,
2066 dbus_set_error (error, DBUS_ERROR_FAILED,
2067 "Element <%s> is not allowed in this context",
2074 set_limit (BusConfigParser *parser,
2079 dbus_bool_t must_be_positive;
2080 dbus_bool_t must_be_int;
2082 must_be_int = FALSE;
2083 must_be_positive = FALSE;
2085 if (strcmp (name, "max_incoming_bytes") == 0)
2087 must_be_positive = TRUE;
2088 parser->limits.max_incoming_bytes = value;
2090 else if (strcmp (name, "max_incoming_unix_fds") == 0)
2092 must_be_positive = TRUE;
2093 parser->limits.max_incoming_unix_fds = value;
2095 else if (strcmp (name, "max_outgoing_bytes") == 0)
2097 must_be_positive = TRUE;
2098 parser->limits.max_outgoing_bytes = value;
2100 else if (strcmp (name, "max_outgoing_unix_fds") == 0)
2102 must_be_positive = TRUE;
2103 parser->limits.max_outgoing_unix_fds = value;
2105 else if (strcmp (name, "max_message_size") == 0)
2107 must_be_positive = TRUE;
2108 parser->limits.max_message_size = value;
2110 else if (strcmp (name, "max_message_unix_fds") == 0)
2112 must_be_positive = TRUE;
2113 parser->limits.max_message_unix_fds = value;
2115 else if (strcmp (name, "service_start_timeout") == 0)
2117 must_be_positive = TRUE;
2119 parser->limits.activation_timeout = value;
2121 else if (strcmp (name, "auth_timeout") == 0)
2123 must_be_positive = TRUE;
2125 parser->limits.auth_timeout = value;
2127 else if (strcmp (name, "pending_fd_timeout") == 0)
2129 must_be_positive = TRUE;
2131 parser->limits.pending_fd_timeout = value;
2133 else if (strcmp (name, "reply_timeout") == 0)
2135 must_be_positive = TRUE;
2137 parser->limits.reply_timeout = value;
2139 else if (strcmp (name, "max_completed_connections") == 0)
2141 must_be_positive = TRUE;
2143 parser->limits.max_completed_connections = value;
2145 else if (strcmp (name, "max_incomplete_connections") == 0)
2147 must_be_positive = TRUE;
2149 parser->limits.max_incomplete_connections = value;
2151 else if (strcmp (name, "max_connections_per_user") == 0)
2153 must_be_positive = TRUE;
2155 parser->limits.max_connections_per_user = value;
2157 else if (strcmp (name, "max_pending_service_starts") == 0)
2159 must_be_positive = TRUE;
2161 parser->limits.max_pending_activations = value;
2163 else if (strcmp (name, "max_names_per_connection") == 0)
2165 must_be_positive = TRUE;
2167 parser->limits.max_services_per_connection = value;
2169 else if (strcmp (name, "max_match_rules_per_connection") == 0)
2171 must_be_positive = TRUE;
2173 parser->limits.max_match_rules_per_connection = value;
2175 else if (strcmp (name, "max_replies_per_connection") == 0)
2177 must_be_positive = TRUE;
2179 parser->limits.max_replies_per_connection = value;
2183 dbus_set_error (error, DBUS_ERROR_FAILED,
2184 "There is no limit called \"%s\"\n",
2189 if (must_be_positive && value < 0)
2191 dbus_set_error (error, DBUS_ERROR_FAILED,
2192 "<limit name=\"%s\"> must be a positive number\n",
2198 (value < _DBUS_INT_MIN || value > _DBUS_INT_MAX))
2200 dbus_set_error (error, DBUS_ERROR_FAILED,
2201 "<limit name=\"%s\"> value is too large\n",
2210 bus_config_parser_end_element (BusConfigParser *parser,
2211 const char *element_name,
2218 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2220 /* printf ("END: %s\n", element_name); */
2222 t = top_element_type (parser);
2224 if (t == ELEMENT_NONE)
2226 /* should probably be an assertion failure but
2227 * being paranoid about XML parsers
2229 dbus_set_error (error, DBUS_ERROR_FAILED,
2230 "XML parser ended element with no element on the stack");
2234 n = bus_config_parser_element_type_to_name (t);
2235 _dbus_assert (n != NULL);
2236 if (strcmp (n, element_name) != 0)
2238 /* should probably be an assertion failure but
2239 * being paranoid about XML parsers
2241 dbus_set_error (error, DBUS_ERROR_FAILED,
2242 "XML element <%s> ended but topmost element on the stack was <%s>",
2247 e = peek_element (parser);
2248 _dbus_assert (e != NULL);
2254 _dbus_assert_not_reached ("element in stack has no type");
2257 case ELEMENT_INCLUDE:
2259 case ELEMENT_CONFIGTYPE:
2260 case ELEMENT_LISTEN:
2261 case ELEMENT_PIDFILE:
2263 case ELEMENT_SERVICEDIR:
2264 case ELEMENT_SERVICEHELPER:
2265 case ELEMENT_INCLUDEDIR:
2267 if (!e->had_content)
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));
2275 if (e->type == ELEMENT_LIMIT)
2277 if (!set_limit (parser, e->d.limit.name, e->d.limit.value,
2283 case ELEMENT_BUSCONFIG:
2284 case ELEMENT_POLICY:
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:
2299 pop_element (parser);
2305 all_whitespace (const DBusString *str)
2309 _dbus_string_skip_white (str, 0, &i);
2311 return i == _dbus_string_get_length (str);
2315 make_full_path (const DBusString *basedir,
2316 const DBusString *filename,
2317 DBusString *full_path)
2319 if (_dbus_path_is_absolute (filename))
2321 if (!_dbus_string_copy (filename, 0, full_path, 0))
2326 if (!_dbus_string_copy (basedir, 0, full_path, 0))
2329 if (!_dbus_concat_dir_and_file (full_path, filename))
2333 if (!_dbus_replace_install_prefix (full_path))
2340 include_file (BusConfigParser *parser,
2341 const DBusString *filename,
2342 dbus_bool_t ignore_missing,
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
2349 BusConfigParser *included;
2350 const char *filename_str;
2351 DBusError tmp_error;
2353 dbus_error_init (&tmp_error);
2355 filename_str = _dbus_string_get_const_data (filename);
2357 /* Check to make sure this file hasn't already been included. */
2358 if (seen_include (parser, filename))
2360 dbus_set_error (error, DBUS_ERROR_FAILED,
2361 "Circular inclusion of file '%s'",
2366 if (! _dbus_list_append (&parser->included_files, (void *) filename_str))
2368 BUS_SET_OOM (error);
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);
2376 _dbus_list_pop_last (&parser->included_files);
2378 if (included == NULL)
2380 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
2382 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_FILE_NOT_FOUND) &&
2385 dbus_error_free (&tmp_error);
2390 dbus_move_error (&tmp_error, error);
2396 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
2398 if (!merge_included (parser, included, error))
2400 bus_config_parser_unref (included);
2404 /* Copy included's limits back to parser. */
2405 parser->limits = included->limits;
2407 bus_config_parser_unref (included);
2413 servicehelper_path (BusConfigParser *parser,
2414 const DBusString *filename,
2417 const char *filename_str;
2418 char *servicehelper;
2420 filename_str = _dbus_string_get_const_data (filename);
2422 /* copy to avoid overwriting with NULL on OOM */
2423 servicehelper = _dbus_strdup (filename_str);
2426 if (servicehelper == NULL)
2428 BUS_SET_OOM (error);
2432 /* save the latest servicehelper only if not OOM */
2433 dbus_free (parser->servicehelper);
2434 parser->servicehelper = servicehelper;
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. ;-)
2447 include_dir (BusConfigParser *parser,
2448 const DBusString *dirname,
2451 DBusString filename;
2453 DBusError tmp_error;
2457 if (!_dbus_string_init (&filename))
2459 BUS_SET_OOM (error);
2465 dir = _dbus_directory_open (dirname, error);
2469 if (dbus_error_has_name (error, DBUS_ERROR_FILE_NOT_FOUND))
2471 dbus_error_free (error);
2478 dbus_error_init (&tmp_error);
2479 while (_dbus_directory_get_next_file (dir, &filename, &tmp_error))
2481 DBusString full_path;
2483 if (!_dbus_string_init (&full_path))
2485 BUS_SET_OOM (error);
2489 if (!_dbus_string_copy (dirname, 0, &full_path, 0))
2491 BUS_SET_OOM (error);
2492 _dbus_string_free (&full_path);
2496 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2498 BUS_SET_OOM (error);
2499 _dbus_string_free (&full_path);
2503 if (_dbus_string_ends_with_c_str (&full_path, ".conf"))
2505 if (!include_file (parser, &full_path, TRUE, error))
2507 if (dbus_error_is_set (error))
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
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'",
2519 _dbus_string_get_const_data (&full_path));
2520 dbus_error_free (error);
2525 _dbus_string_free (&full_path);
2528 if (dbus_error_is_set (&tmp_error))
2530 dbus_move_error (&tmp_error, error);
2535 if (!_dbus_string_copy_data (dirname, &s))
2537 BUS_SET_OOM (error);
2541 if (!_dbus_list_append (&parser->conf_dirs, s))
2544 BUS_SET_OOM (error);
2552 _dbus_string_free (&filename);
2555 _dbus_directory_close (dir);
2561 bus_config_parser_content (BusConfigParser *parser,
2562 const DBusString *content,
2567 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2573 _dbus_string_get_const_data (content, &c_str);
2575 printf ("CONTENT %d bytes: %s\n", _dbus_string_get_length (content), c_str);
2579 e = peek_element (parser);
2582 dbus_set_error (error, DBUS_ERROR_FAILED,
2583 "Text content outside of any XML element in configuration file");
2586 else if (e->had_content)
2588 _dbus_assert_not_reached ("Element had multiple content blocks");
2592 switch (top_element_type (parser))
2596 _dbus_assert_not_reached ("element at top of stack has no type");
2599 case ELEMENT_BUSCONFIG:
2600 case ELEMENT_POLICY:
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))
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)));
2622 case ELEMENT_PIDFILE:
2626 e->had_content = TRUE;
2628 if (!_dbus_string_copy_data (content, &s))
2631 dbus_free (parser->pidfile);
2632 parser->pidfile = s;
2636 case ELEMENT_INCLUDE:
2638 DBusString full_path, selinux_policy_root;
2640 e->had_content = TRUE;
2642 if (e->d.include.if_selinux_enabled
2643 && !bus_selinux_enabled ())
2646 if (!_dbus_string_init (&full_path))
2649 if (e->d.include.selinux_root_relative)
2651 if (!bus_selinux_get_policy_root ())
2653 dbus_set_error (error, DBUS_ERROR_FAILED,
2654 "Could not determine SELinux policy root for relative inclusion");
2655 _dbus_string_free (&full_path);
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))
2662 _dbus_string_free (&full_path);
2666 else if (!make_full_path (&parser->basedir, content, &full_path))
2668 _dbus_string_free (&full_path);
2672 if (!include_file (parser, &full_path,
2673 e->d.include.ignore_missing, error))
2675 _dbus_string_free (&full_path);
2679 _dbus_string_free (&full_path);
2683 case ELEMENT_SERVICEHELPER:
2685 DBusString full_path;
2687 e->had_content = TRUE;
2689 if (!_dbus_string_init (&full_path))
2692 if (!make_full_path (&parser->basedir, content, &full_path))
2694 _dbus_string_free (&full_path);
2698 if (!servicehelper_path (parser, &full_path, error))
2700 _dbus_string_free (&full_path);
2704 _dbus_string_free (&full_path);
2708 case ELEMENT_INCLUDEDIR:
2710 DBusString full_path;
2712 e->had_content = TRUE;
2714 if (!_dbus_string_init (&full_path))
2717 if (!make_full_path (&parser->basedir, content, &full_path))
2719 _dbus_string_free (&full_path);
2723 if (!include_dir (parser, &full_path, error))
2725 _dbus_string_free (&full_path);
2729 _dbus_string_free (&full_path);
2737 e->had_content = TRUE;
2739 if (!_dbus_string_copy_data (content, &s))
2742 dbus_free (parser->user);
2747 case ELEMENT_CONFIGTYPE:
2751 e->had_content = TRUE;
2753 if (!_dbus_string_copy_data (content, &s))
2756 dbus_free (parser->bus_type);
2757 parser->bus_type = s;
2761 case ELEMENT_LISTEN:
2765 e->had_content = TRUE;
2767 if (!_dbus_string_copy_data (content, &s))
2770 if (!_dbus_list_append (&parser->listen_on,
2783 e->had_content = TRUE;
2785 if (!_dbus_string_copy_data (content, &s))
2788 if (!_dbus_list_append (&parser->mechanisms,
2797 case ELEMENT_SERVICEDIR:
2800 BusConfigServiceDir *dir;
2801 DBusString full_path;
2804 e->had_content = TRUE;
2806 if (!_dbus_string_init (&full_path))
2809 if (!make_full_path (&parser->basedir, content, &full_path))
2811 _dbus_string_free (&full_path);
2815 if (!_dbus_string_copy_data (&full_path, &s))
2817 _dbus_string_free (&full_path);
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);
2828 _dbus_string_free (&full_path);
2833 link = _dbus_list_alloc_link (dir);
2837 _dbus_string_free (&full_path);
2838 bus_config_service_dir_free (dir);
2843 service_dirs_append_link_unique_or_free (&parser->service_dirs, link);
2844 _dbus_string_free (&full_path);
2852 e->had_content = TRUE;
2855 if (!_dbus_string_parse_int (content, 0, &val, NULL))
2857 dbus_set_error (error, DBUS_ERROR_FAILED,
2858 "<limit name=\"%s\"> element has invalid value (could not parse as integer)",
2863 e->d.limit.value = val;
2865 _dbus_verbose ("Loaded value %ld for limit %s\n",
2872 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2876 BUS_SET_OOM (error);
2881 bus_config_parser_finished (BusConfigParser *parser,
2884 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2886 if (parser->stack != NULL)
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)));
2895 if (parser->is_toplevel && parser->listen_on == NULL)
2897 dbus_set_error (error, DBUS_ERROR_FAILED,
2898 "Configuration file needs one or more <listen> elements giving addresses");
2906 bus_config_parser_get_user (BusConfigParser *parser)
2908 return parser->user;
2912 bus_config_parser_get_type (BusConfigParser *parser)
2914 return parser->bus_type;
2918 bus_config_parser_get_addresses (BusConfigParser *parser)
2920 return &parser->listen_on;
2924 bus_config_parser_get_mechanisms (BusConfigParser *parser)
2926 return &parser->mechanisms;
2930 bus_config_parser_get_service_dirs (BusConfigParser *parser)
2932 return &parser->service_dirs;
2936 bus_config_parser_get_conf_dirs (BusConfigParser *parser)
2938 return &parser->conf_dirs;
2942 bus_config_parser_get_fork (BusConfigParser *parser)
2944 return parser->fork;
2948 bus_config_parser_get_syslog (BusConfigParser *parser)
2950 return parser->syslog;
2954 bus_config_parser_get_keep_umask (BusConfigParser *parser)
2956 return parser->keep_umask;
2960 bus_config_parser_get_allow_anonymous (BusConfigParser *parser)
2962 return parser->allow_anonymous;
2966 bus_config_parser_get_pidfile (BusConfigParser *parser)
2968 return parser->pidfile;
2972 bus_config_parser_get_servicehelper (BusConfigParser *parser)
2974 return parser->servicehelper;
2978 bus_config_parser_steal_policy (BusConfigParser *parser)
2982 _dbus_assert (parser->policy != NULL); /* can only steal the policy 1 time */
2984 policy = parser->policy;
2986 parser->policy = NULL;
2991 /* Overwrite any limits that were set in the configuration file */
2993 bus_config_parser_get_limits (BusConfigParser *parser,
2996 *limits = parser->limits;
3000 bus_config_parser_steal_service_context_table (BusConfigParser *parser)
3002 DBusHashTable *table;
3004 _dbus_assert (parser->service_context_table != NULL); /* can only steal once */
3006 table = parser->service_context_table;
3008 parser->service_context_table = NULL;
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.
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).
3023 bus_config_parser_get_watched_dirs (BusConfigParser *parser,
3024 DBusList **watched_dirs)
3028 _dbus_assert (*watched_dirs == NULL);
3030 for (link = _dbus_list_get_first_link (&parser->conf_dirs);
3032 link = _dbus_list_get_next_link (&parser->conf_dirs, link))
3034 if (!_dbus_list_append (watched_dirs, link->data))
3038 for (link = _dbus_list_get_first_link (&parser->service_dirs);
3040 link = _dbus_list_get_next_link (&parser->service_dirs, link))
3042 BusConfigServiceDir *dir = link->data;
3044 if (dir->flags & BUS_SERVICE_DIR_FLAGS_NO_WATCH)
3047 if (!_dbus_list_append (watched_dirs, dir->path))
3054 _dbus_list_clear (watched_dirs);
3058 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
3069 do_check_own_rules (BusPolicy *policy)
3073 dbus_bool_t allowed;
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},
3087 while (checks[i].name)
3089 DBusString service_name;
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");
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)
3102 _dbus_warn ("Cannot own %s", checks[i].name);
3105 if (!checks[i].allowed && ret)
3107 _dbus_warn ("Can own %s", checks[i].name);
3110 _dbus_string_free (&service_name);
3119 do_load (const DBusString *full_path,
3121 dbus_bool_t oom_possible,
3122 dbus_bool_t check_own_rules)
3124 BusConfigParser *parser;
3127 dbus_error_init (&error);
3129 parser = bus_config_load (full_path, TRUE, NULL, &error);
3132 _DBUS_ASSERT_ERROR_IS_SET (&error);
3135 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
3137 _dbus_verbose ("Failed to load valid file due to OOM\n");
3138 dbus_error_free (&error);
3141 else if (validity == VALID)
3143 _dbus_warn ("Failed to load valid file but still had memory: %s",
3146 dbus_error_free (&error);
3151 dbus_error_free (&error);
3157 _DBUS_ASSERT_ERROR_IS_CLEAR (&error);
3159 if (check_own_rules && do_check_own_rules (parser->policy) == FALSE)
3164 bus_config_parser_unref (parser);
3166 if (validity == INVALID)
3168 _dbus_warn ("Accepted invalid file");
3178 const DBusString *full_path;
3180 dbus_bool_t check_own_rules;
3184 check_loader_oom_func (void *data)
3186 LoaderOomData *d = data;
3188 return do_load (d->full_path, d->validity, TRUE, d->check_own_rules);
3192 process_test_valid_subdir (const DBusString *test_base_dir,
3196 DBusString test_directory;
3197 DBusString filename;
3205 if (!_dbus_string_init (&test_directory))
3206 _dbus_assert_not_reached ("didn't allocate test_directory");
3208 _dbus_string_init_const (&filename, subdir);
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");
3214 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
3215 _dbus_assert_not_reached ("couldn't allocate full path");
3217 _dbus_string_free (&filename);
3218 if (!_dbus_string_init (&filename))
3219 _dbus_assert_not_reached ("didn't allocate filename string");
3221 dbus_error_init (&error);
3222 dir = _dbus_directory_open (&test_directory, &error);
3225 _dbus_warn ("Could not open %s: %s",
3226 _dbus_string_get_const_data (&test_directory),
3228 dbus_error_free (&error);
3232 if (validity == VALID)
3233 printf ("Testing valid files:\n");
3234 else if (validity == INVALID)
3235 printf ("Testing invalid files:\n");
3237 printf ("Testing unknown files:\n");
3240 while (_dbus_directory_get_next_file (dir, &filename, &error))
3242 DBusString full_path;
3245 if (!_dbus_string_init (&full_path))
3246 _dbus_assert_not_reached ("couldn't init string");
3248 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
3249 _dbus_assert_not_reached ("couldn't copy dir to full_path");
3251 if (!_dbus_concat_dir_and_file (&full_path, &filename))
3252 _dbus_assert_not_reached ("couldn't concat file to dir");
3254 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
3256 _dbus_verbose ("Skipping non-.conf file %s\n",
3257 _dbus_string_get_const_data (&filename));
3258 _dbus_string_free (&full_path);
3262 printf (" %s\n", _dbus_string_get_const_data (&filename));
3264 _dbus_verbose (" expecting %s\n",
3265 validity == VALID ? "valid" :
3266 (validity == INVALID ? "invalid" :
3267 (validity == UNKNOWN ? "unknown" : "???")));
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");
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
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");
3282 _dbus_string_free (&full_path);
3285 if (dbus_error_is_set (&error))
3287 _dbus_warn ("Could not get next file in %s: %s",
3288 _dbus_string_get_const_data (&test_directory),
3290 dbus_error_free (&error);
3299 _dbus_directory_close (dir);
3300 _dbus_string_free (&test_directory);
3301 _dbus_string_free (&filename);
3307 bools_equal (dbus_bool_t a,
3314 strings_equal_or_both_null (const char *a,
3317 if (a == NULL || b == NULL)
3320 return !strcmp (a, b);
3324 elements_equal (const Element *a,
3327 if (a->type != b->type)
3330 if (!bools_equal (a->had_content, b->had_content))
3336 case ELEMENT_INCLUDE:
3337 if (!bools_equal (a->d.include.ignore_missing,
3338 b->d.include.ignore_missing))
3342 case ELEMENT_POLICY:
3343 if (a->d.policy.type != b->d.policy.type)
3345 if (a->d.policy.gid_uid_or_at_console != b->d.policy.gid_uid_or_at_console)
3350 if (strcmp (a->d.limit.name, b->d.limit.name))
3352 if (a->d.limit.value != b->d.limit.value)
3357 case ELEMENT_BUSCONFIG:
3359 case ELEMENT_LISTEN:
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:
3378 /* do nothing: nothing in the Element struct for these types */
3387 lists_of_elements_equal (DBusList *a,
3396 while (ia != NULL && ib != NULL)
3398 if (elements_equal (ia->data, ib->data))
3400 ia = _dbus_list_get_next_link (&a, ia);
3401 ib = _dbus_list_get_next_link (&b, ib);
3404 return ia == NULL && ib == NULL;
3408 lists_of_c_strings_equal (DBusList *a,
3417 while (ia != NULL && ib != NULL)
3419 if (strcmp (ia->data, ib->data))
3421 ia = _dbus_list_get_next_link (&a, ia);
3422 ib = _dbus_list_get_next_link (&b, ib);
3425 return ia == NULL && ib == NULL;
3429 lists_of_service_dirs_equal (DBusList *a,
3438 while (ia != NULL && ib != NULL)
3440 BusConfigServiceDir *da = ia->data;
3441 BusConfigServiceDir *db = ib->data;
3443 if (strcmp (da->path, db->path))
3446 if (da->flags != db->flags)
3449 ia = _dbus_list_get_next_link (&a, ia);
3450 ib = _dbus_list_get_next_link (&b, ib);
3453 return ia == NULL && ib == NULL;
3457 limits_equal (const BusLimits *a,
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);
3481 config_parsers_equal (const BusConfigParser *a,
3482 const BusConfigParser *b)
3484 if (!_dbus_string_equal (&a->basedir, &b->basedir))
3487 if (!lists_of_elements_equal (a->stack, b->stack))
3490 if (!strings_equal_or_both_null (a->user, b->user))
3493 if (!lists_of_c_strings_equal (a->listen_on, b->listen_on))
3496 if (!lists_of_c_strings_equal (a->mechanisms, b->mechanisms))
3499 if (!lists_of_service_dirs_equal (a->service_dirs, b->service_dirs))
3502 /* FIXME: compare policy */
3504 /* FIXME: compare service selinux ID table */
3506 if (! limits_equal (&a->limits, &b->limits))
3509 if (!strings_equal_or_both_null (a->pidfile, b->pidfile))
3512 if (! bools_equal (a->fork, b->fork))
3515 if (! bools_equal (a->keep_umask, b->keep_umask))
3518 if (! bools_equal (a->is_toplevel, b->is_toplevel))
3525 all_are_equiv (const DBusString *target_directory)
3527 DBusString filename;
3529 BusConfigParser *first_parser;
3530 BusConfigParser *parser;
3536 first_parser = NULL;
3540 if (!_dbus_string_init (&filename))
3541 _dbus_assert_not_reached ("didn't allocate filename string");
3543 dbus_error_init (&error);
3544 dir = _dbus_directory_open (target_directory, &error);
3547 _dbus_warn ("Could not open %s: %s",
3548 _dbus_string_get_const_data (target_directory),
3550 dbus_error_free (&error);
3554 printf ("Comparing equivalent files:\n");
3557 while (_dbus_directory_get_next_file (dir, &filename, &error))
3559 DBusString full_path;
3561 if (!_dbus_string_init (&full_path))
3562 _dbus_assert_not_reached ("couldn't init string");
3564 if (!_dbus_string_copy (target_directory, 0, &full_path, 0))
3565 _dbus_assert_not_reached ("couldn't copy dir to full_path");
3567 if (!_dbus_concat_dir_and_file (&full_path, &filename))
3568 _dbus_assert_not_reached ("couldn't concat file to dir");
3570 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
3572 _dbus_verbose ("Skipping non-.conf file %s\n",
3573 _dbus_string_get_const_data (&filename));
3574 _dbus_string_free (&full_path);
3578 printf (" %s\n", _dbus_string_get_const_data (&filename));
3580 parser = bus_config_load (&full_path, TRUE, NULL, &error);
3584 _dbus_warn ("Could not load file %s: %s",
3585 _dbus_string_get_const_data (&full_path),
3587 _dbus_string_free (&full_path);
3588 dbus_error_free (&error);
3591 else if (first_parser == NULL)
3593 _dbus_string_free (&full_path);
3594 first_parser = parser;
3598 _dbus_string_free (&full_path);
3599 equal = config_parsers_equal (first_parser, parser);
3600 bus_config_parser_unref (parser);
3609 _dbus_string_free (&filename);
3611 bus_config_parser_unref (first_parser);
3613 _dbus_directory_close (dir);
3620 process_test_equiv_subdir (const DBusString *test_base_dir,
3623 DBusString test_directory;
3624 DBusString filename;
3633 if (!_dbus_string_init (&test_directory))
3634 _dbus_assert_not_reached ("didn't allocate test_directory");
3636 _dbus_string_init_const (&filename, subdir);
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");
3642 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
3643 _dbus_assert_not_reached ("couldn't allocate full path");
3645 _dbus_string_free (&filename);
3646 if (!_dbus_string_init (&filename))
3647 _dbus_assert_not_reached ("didn't allocate filename string");
3649 dbus_error_init (&error);
3650 dir = _dbus_directory_open (&test_directory, &error);
3653 _dbus_warn ("Could not open %s: %s",
3654 _dbus_string_get_const_data (&test_directory),
3656 dbus_error_free (&error);
3660 while (_dbus_directory_get_next_file (dir, &filename, &error))
3662 DBusString full_path;
3664 /* Skip CVS's magic directories! */
3665 if (_dbus_string_equal_c_str (&filename, "CVS"))
3668 if (!_dbus_string_init (&full_path))
3669 _dbus_assert_not_reached ("couldn't init string");
3671 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
3672 _dbus_assert_not_reached ("couldn't copy dir to full_path");
3674 if (!_dbus_concat_dir_and_file (&full_path, &filename))
3675 _dbus_assert_not_reached ("couldn't concat file to dir");
3677 equal = all_are_equiv (&full_path);
3678 _dbus_string_free (&full_path);
3687 _dbus_string_free (&test_directory);
3688 _dbus_string_free (&filename);
3690 _dbus_directory_close (dir);
3696 static const char *test_session_service_dir_matches[] =
3698 /* will be filled in test_default_session_servicedirs() */
3700 NULL, /* install root-based */
3701 NULL, /* CommonProgramFiles-based */
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",
3713 test_default_session_servicedirs (const DBusString *test_base_dir)
3715 BusConfigParser *parser = NULL;
3716 DBusError error = DBUS_ERROR_INIT;
3718 DBusList *watched_dirs = NULL;
3721 DBusString full_path;
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;
3729 dbus_bool_t ret = FALSE;
3731 const char *common_progs;
3733 const char *dbus_test_builddir;
3734 const char *xdg_data_home;
3735 const char *xdg_runtime_dir;
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
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");
3750 if (!_dbus_string_copy (test_base_dir, 0,
3752 _dbus_assert_not_reached ("couldn't copy test_base_dir to full_path");
3754 _dbus_string_init_const (&tmp, "valid-config-files");
3756 if (!_dbus_concat_dir_and_file (&full_path, &tmp))
3757 _dbus_assert_not_reached ("couldn't allocate full path");
3759 _dbus_string_init_const (&tmp, "standard-session-dirs.conf");
3761 if (!_dbus_concat_dir_and_file (&full_path, &tmp))
3762 _dbus_assert_not_reached ("couldn't allocate full path");
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))
3770 _dbus_assert (_dbus_path_is_absolute (&install_root_based));
3772 test_session_service_dir_matches[0] = _dbus_string_get_const_data (
3773 &install_root_based);
3775 common_progs = _dbus_getenv ("CommonProgramFiles");
3779 if (!_dbus_string_append (&progs, common_progs))
3782 if (!_dbus_string_append (&progs, "/dbus-1/services"))
3785 test_session_service_dir_matches[1] = _dbus_string_get_const_data(&progs);
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");
3792 if (dbus_test_builddir == NULL || xdg_data_home == NULL ||
3793 xdg_runtime_dir == NULL)
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");
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");
3811 if (!_dbus_ensure_directory (&runtime_dir_based, NULL))
3812 _dbus_assert_not_reached ("Unable to create fake XDG_RUNTIME_DIR");
3814 if (!_dbus_string_append (&runtime_dir_based, "/dbus-1/services"))
3815 _dbus_assert_not_reached ("out of memory");
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");
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");
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 (
3829 test_session_service_dir_matches[2] = _dbus_string_get_const_data (
3831 test_session_service_dir_matches[3] = _dbus_string_get_const_data (
3835 parser = bus_config_load (&full_path, TRUE, NULL, &error);
3838 _dbus_assert_not_reached (error.message);
3840 dirs = bus_config_parser_get_service_dirs (parser);
3842 for (link = _dbus_list_get_first_link (dirs), i = 0;
3844 link = _dbus_list_get_next_link (dirs, link), i++)
3846 BusConfigServiceDir *dir = link->data;
3847 BusServiceDirFlags expected = BUS_SERVICE_DIR_FLAGS_NONE;
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)
3853 printf ("more directories parsed than in match set\n");
3857 if (strcmp (test_session_service_dir_matches[i], dir->path) != 0)
3859 printf ("'%s' directory does not match '%s' in the match set\n",
3860 dir->path, test_session_service_dir_matches[i]);
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 */
3868 expected = (BUS_SERVICE_DIR_FLAGS_NO_WATCH |
3869 BUS_SERVICE_DIR_FLAGS_STRICT_NAMING);
3872 if (dir->flags != expected)
3874 printf ("'%s' directory has flags 0x%x, should be 0x%x\n",
3875 dir->path, dir->flags, expected);
3880 if (test_session_service_dir_matches[i] != NULL)
3882 printf ("extra data %s in the match set was not matched\n",
3883 test_session_service_dir_matches[i]);
3887 if (!bus_config_parser_get_watched_dirs (parser, &watched_dirs))
3888 _dbus_assert_not_reached ("out of memory");
3891 /* We expect all directories to be watched (not that it matters on Windows,
3892 * because we don't know how) */
3895 /* We expect all directories except the first to be watched, because
3896 * the first one is transient */
3900 for (link = _dbus_list_get_first_link (&watched_dirs);
3902 link = _dbus_list_get_next_link (&watched_dirs, link), i++)
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]);
3908 if (test_session_service_dir_matches[i] == NULL)
3910 printf ("more directories parsed than in match set\n");
3914 if (strcmp (test_session_service_dir_matches[i],
3915 (const char *) link->data) != 0)
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]);
3924 if (test_session_service_dir_matches[i] != NULL)
3926 printf ("extra data %s in the match set was not matched\n",
3927 test_session_service_dir_matches[i]);
3935 bus_config_parser_unref (parser);
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);
3949 static const char *test_system_service_dir_matches[] =
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",
3959 test_default_system_servicedirs (void)
3967 if (!_dbus_get_standard_system_servicedirs (&dirs))
3968 _dbus_assert_not_reached ("couldn't get stardard dirs");
3970 /* make sure we read and parse the env variable correctly */
3972 while ((link = _dbus_list_pop_first_link (&dirs)))
3974 printf (" test service dir: %s\n", (char *)link->data);
3975 if (test_system_service_dir_matches[i] == NULL)
3977 printf ("more directories parsed than in match set\n");
3978 dbus_free (link->data);
3979 _dbus_list_free_link (link);
3983 if (strcmp (test_system_service_dir_matches[i],
3984 (char *)link->data) != 0)
3986 printf ("%s directory does not match %s in the match set\n",
3988 test_system_service_dir_matches[i]);
3989 dbus_free (link->data);
3990 _dbus_list_free_link (link);
3996 dbus_free (link->data);
3997 _dbus_list_free_link (link);
4000 if (test_system_service_dir_matches[i] != NULL)
4002 printf ("extra data %s in the match set was not matched\n",
4003 test_system_service_dir_matches[i]);
4013 bus_config_parser_test (const DBusString *test_data_dir)
4015 if (test_data_dir == NULL ||
4016 _dbus_string_get_length (test_data_dir) == 0)
4018 printf ("No test data\n");
4022 if (!test_default_session_servicedirs (test_data_dir))
4026 printf("default system service dir skipped\n");
4028 if (!test_default_system_servicedirs())
4032 if (!process_test_valid_subdir (test_data_dir, "valid-config-files", VALID))
4036 if (!process_test_valid_subdir (test_data_dir, "valid-config-files-system", VALID))
4040 if (!process_test_valid_subdir (test_data_dir, "invalid-config-files", INVALID))
4043 if (!process_test_equiv_subdir (test_data_dir, "equiv-config-files"))
4049 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */