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 */
56 unsigned int had_content : 1;
62 unsigned int ignore_missing : 1;
63 unsigned int if_selinux_enabled : 1;
64 unsigned int selinux_root_relative : 1;
72 unsigned long gid_uid_or_at_console;
88 * Parser for bus configuration file.
90 struct BusConfigParser
92 int refcount; /**< Reference count */
94 DBusString basedir; /**< Directory we resolve paths relative to */
96 DBusList *stack; /**< stack of Element */
98 char *user; /**< user to run as */
100 char *servicehelper; /**< location of the setuid helper */
102 char *bus_type; /**< Message bus type */
104 DBusList *listen_on; /**< List of addresses to listen to */
106 DBusList *mechanisms; /**< Auth mechanisms */
108 DBusList *service_dirs; /**< Directories to look for session services in */
110 DBusList *conf_dirs; /**< Directories to look for policy configuration in */
112 BusPolicy *policy; /**< Security policy */
114 BusLimits limits; /**< Limits */
116 char *pidfile; /**< PID file */
118 DBusList *included_files; /**< Included files stack */
120 DBusHashTable *service_context_table; /**< Map service names to SELinux contexts */
122 unsigned int fork : 1; /**< TRUE to fork into daemon mode */
124 unsigned int syslog : 1; /**< TRUE to enable syslog */
125 unsigned int keep_umask : 1; /**< TRUE to keep original umask when forking */
127 unsigned int is_toplevel : 1; /**< FALSE if we are a sub-config-file inside another one */
129 unsigned int allow_anonymous : 1; /**< TRUE to allow anonymous connections */
133 push_element (BusConfigParser *parser,
138 _dbus_assert (type != ELEMENT_NONE);
140 e = dbus_new0 (Element, 1);
144 if (!_dbus_list_append (&parser->stack, e))
156 element_free (Element *e)
158 if (e->type == ELEMENT_LIMIT)
159 dbus_free (e->d.limit.name);
160 else if (e->type == ELEMENT_POLICY && e->d.policy.type == POLICY_SMACK)
161 dbus_free (e->d.policy.smack_label);
167 pop_element (BusConfigParser *parser)
171 e = _dbus_list_pop_last (&parser->stack);
177 peek_element (BusConfigParser *parser)
181 e = _dbus_list_get_last (&parser->stack);
187 top_element_type (BusConfigParser *parser)
191 e = _dbus_list_get_last (&parser->stack);
200 merge_service_context_hash (DBusHashTable *dest,
210 _dbus_hash_iter_init (from, &iter);
211 while (_dbus_hash_iter_next (&iter))
213 const char *service = _dbus_hash_iter_get_string_key (&iter);
214 const char *context = _dbus_hash_iter_get_value (&iter);
216 service_copy = _dbus_strdup (service);
217 if (service_copy == NULL)
219 context_copy = _dbus_strdup (context);
220 if (context_copy == NULL)
223 if (!_dbus_hash_table_insert_string (dest, service_copy, context_copy))
234 dbus_free (service_copy);
237 dbus_free (context_copy);
243 service_dirs_find_dir (DBusList **service_dirs,
248 _dbus_assert (dir != NULL);
250 for (link = *service_dirs; link; link = _dbus_list_get_next_link(service_dirs, link))
252 const char *link_dir;
254 link_dir = (const char *)link->data;
255 if (strcmp (dir, link_dir) == 0)
263 service_dirs_append_unique_or_free (DBusList **service_dirs,
266 if (!service_dirs_find_dir (service_dirs, dir))
267 return _dbus_list_append (service_dirs, dir);
274 service_dirs_append_link_unique_or_free (DBusList **service_dirs,
277 if (!service_dirs_find_dir (service_dirs, dir_link->data))
279 _dbus_list_append_link (service_dirs, dir_link);
283 dbus_free (dir_link->data);
284 _dbus_list_free_link (dir_link);
289 merge_included (BusConfigParser *parser,
290 BusConfigParser *included,
295 if (!bus_policy_merge (parser->policy,
302 if (!merge_service_context_hash (parser->service_context_table,
303 included->service_context_table))
309 if (included->user != NULL)
311 dbus_free (parser->user);
312 parser->user = included->user;
313 included->user = NULL;
316 if (included->bus_type != NULL)
318 dbus_free (parser->bus_type);
319 parser->bus_type = included->bus_type;
320 included->bus_type = NULL;
326 if (included->keep_umask)
327 parser->keep_umask = TRUE;
329 if (included->allow_anonymous)
330 parser->allow_anonymous = TRUE;
332 if (included->pidfile != NULL)
334 dbus_free (parser->pidfile);
335 parser->pidfile = included->pidfile;
336 included->pidfile = NULL;
339 if (included->servicehelper != NULL)
341 dbus_free (parser->servicehelper);
342 parser->servicehelper = included->servicehelper;
343 included->servicehelper = NULL;
346 while ((link = _dbus_list_pop_first_link (&included->listen_on)))
347 _dbus_list_append_link (&parser->listen_on, link);
349 while ((link = _dbus_list_pop_first_link (&included->mechanisms)))
350 _dbus_list_append_link (&parser->mechanisms, link);
352 while ((link = _dbus_list_pop_first_link (&included->service_dirs)))
353 service_dirs_append_link_unique_or_free (&parser->service_dirs, link);
355 while ((link = _dbus_list_pop_first_link (&included->conf_dirs)))
356 _dbus_list_append_link (&parser->conf_dirs, link);
362 seen_include (BusConfigParser *parser,
363 const DBusString *file)
367 iter = parser->included_files;
370 if (! strcmp (_dbus_string_get_const_data (file), iter->data))
373 iter = _dbus_list_get_next_link (&parser->included_files, iter);
380 bus_config_parser_new (const DBusString *basedir,
381 dbus_bool_t is_toplevel,
382 const BusConfigParser *parent)
384 BusConfigParser *parser;
386 parser = dbus_new0 (BusConfigParser, 1);
390 parser->is_toplevel = !!is_toplevel;
392 if (!_dbus_string_init (&parser->basedir))
398 if (((parser->policy = bus_policy_new ()) == NULL) ||
399 !_dbus_string_copy (basedir, 0, &parser->basedir, 0) ||
400 ((parser->service_context_table = _dbus_hash_table_new (DBUS_HASH_STRING,
402 dbus_free)) == NULL))
405 bus_policy_unref (parser->policy);
407 _dbus_string_free (&parser->basedir);
415 /* Initialize the parser's limits from the parent. */
416 parser->limits = parent->limits;
418 /* Use the parent's list of included_files to avoid
419 circular inclusions. */
420 parser->included_files = parent->included_files;
425 /* Make up some numbers! woot! */
426 parser->limits.max_incoming_bytes = _DBUS_ONE_MEGABYTE * 127;
427 parser->limits.max_outgoing_bytes = _DBUS_ONE_MEGABYTE * 127;
428 parser->limits.max_message_size = _DBUS_ONE_MEGABYTE * 32;
430 /* We set relatively conservative values here since due to the
431 way SCM_RIGHTS works we need to preallocate an array for the
432 maximum number of file descriptors we can receive. Picking a
433 high value here thus translates directly to more memory
435 parser->limits.max_incoming_unix_fds = DBUS_DEFAULT_MESSAGE_UNIX_FDS*4;
436 parser->limits.max_outgoing_unix_fds = DBUS_DEFAULT_MESSAGE_UNIX_FDS*4;
437 parser->limits.max_message_unix_fds = DBUS_DEFAULT_MESSAGE_UNIX_FDS;
439 /* Making this long means the user has to wait longer for an error
440 * message if something screws up, but making it too short means
441 * they might see a false failure.
443 parser->limits.activation_timeout = 25000; /* 25 seconds */
445 /* Making this long risks making a DOS attack easier, but too short
446 * and legitimate auth will fail. If interactive auth (ask user for
447 * password) is allowed, then potentially it has to be quite long.
449 parser->limits.auth_timeout = 30000; /* 30 seconds */
451 /* Do not allow a fd to stay forever in dbus-daemon
452 * https://bugs.freedesktop.org/show_bug.cgi?id=80559
454 parser->limits.pending_fd_timeout = 150000; /* 2.5 minutes */
456 parser->limits.max_incomplete_connections = 64;
457 parser->limits.max_connections_per_user = 256;
459 /* Note that max_completed_connections / max_connections_per_user
460 * is the number of users that would have to work together to
461 * DOS all the other users.
463 parser->limits.max_completed_connections = 2048;
465 parser->limits.max_pending_activations = 512;
466 parser->limits.max_services_per_connection = 512;
468 /* For this one, keep in mind that it isn't only the memory used
469 * by the match rules, but slowdown from linearly walking a big
470 * list of them. A client adding more than this is almost
471 * certainly a bad idea for that reason, and should change to a
472 * smaller number of wider-net match rules - getting every last
473 * message to the bus is probably better than having a thousand
476 parser->limits.max_match_rules_per_connection = 512;
478 parser->limits.reply_timeout = -1; /* never */
480 /* this is effectively a limit on message queue size for messages
481 * that require a reply
483 parser->limits.max_replies_per_connection = 128;
486 parser->refcount = 1;
492 bus_config_parser_ref (BusConfigParser *parser)
494 _dbus_assert (parser->refcount > 0);
496 parser->refcount += 1;
502 bus_config_parser_unref (BusConfigParser *parser)
504 _dbus_assert (parser->refcount > 0);
506 parser->refcount -= 1;
508 if (parser->refcount == 0)
510 while (parser->stack != NULL)
511 pop_element (parser);
513 dbus_free (parser->user);
514 dbus_free (parser->servicehelper);
515 dbus_free (parser->bus_type);
516 dbus_free (parser->pidfile);
518 _dbus_list_foreach (&parser->listen_on,
519 (DBusForeachFunction) dbus_free,
522 _dbus_list_clear (&parser->listen_on);
524 _dbus_list_foreach (&parser->service_dirs,
525 (DBusForeachFunction) dbus_free,
528 _dbus_list_clear (&parser->service_dirs);
530 _dbus_list_foreach (&parser->conf_dirs,
531 (DBusForeachFunction) dbus_free,
534 _dbus_list_clear (&parser->conf_dirs);
536 _dbus_list_foreach (&parser->mechanisms,
537 (DBusForeachFunction) dbus_free,
540 _dbus_list_clear (&parser->mechanisms);
542 _dbus_string_free (&parser->basedir);
545 bus_policy_unref (parser->policy);
547 if (parser->service_context_table)
548 _dbus_hash_table_unref (parser->service_context_table);
555 bus_config_parser_check_doctype (BusConfigParser *parser,
559 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
561 if (strcmp (doctype, "busconfig") != 0)
563 dbus_set_error (error,
565 "Configuration file has the wrong document type %s",
580 locate_attributes (BusConfigParser *parser,
581 const char *element_name,
582 const char **attribute_names,
583 const char **attribute_values,
585 const char *first_attribute_name,
586 const char **first_attribute_retloc,
594 LocateAttr attrs[MAX_ATTRS];
598 _dbus_assert (first_attribute_name != NULL);
599 _dbus_assert (first_attribute_retloc != NULL);
604 attrs[0].name = first_attribute_name;
605 attrs[0].retloc = first_attribute_retloc;
606 *first_attribute_retloc = NULL;
608 va_start (args, first_attribute_retloc);
610 name = va_arg (args, const char*);
611 retloc = va_arg (args, const char**);
615 _dbus_assert (retloc != NULL);
616 _dbus_assert (n_attrs < MAX_ATTRS);
618 attrs[n_attrs].name = name;
619 attrs[n_attrs].retloc = retloc;
623 name = va_arg (args, const char*);
624 retloc = va_arg (args, const char**);
630 while (attribute_names[i])
639 if (strcmp (attrs[j].name, attribute_names[i]) == 0)
641 retloc = attrs[j].retloc;
645 dbus_set_error (error, DBUS_ERROR_FAILED,
646 "Attribute \"%s\" repeated twice on the same <%s> element",
647 attrs[j].name, element_name);
652 *retloc = attribute_values[i];
661 dbus_set_error (error, DBUS_ERROR_FAILED,
662 "Attribute \"%s\" is invalid on <%s> element in this context",
663 attribute_names[i], element_name);
676 check_no_attributes (BusConfigParser *parser,
677 const char *element_name,
678 const char **attribute_names,
679 const char **attribute_values,
682 if (attribute_names[0] != NULL)
684 dbus_set_error (error, DBUS_ERROR_FAILED,
685 "Attribute \"%s\" is invalid on <%s> element in this context",
686 attribute_names[0], element_name);
694 start_busconfig_child (BusConfigParser *parser,
695 const char *element_name,
696 const char **attribute_names,
697 const char **attribute_values,
700 ElementType element_type;
702 element_type = bus_config_parser_element_name_to_type (element_name);
704 if (element_type == ELEMENT_USER)
706 if (!check_no_attributes (parser, "user", attribute_names, attribute_values, error))
709 if (push_element (parser, ELEMENT_USER) == NULL)
717 else if (element_type == ELEMENT_CONFIGTYPE)
719 if (!check_no_attributes (parser, "type", attribute_names, attribute_values, error))
722 if (push_element (parser, ELEMENT_CONFIGTYPE) == NULL)
730 else if (element_type == ELEMENT_FORK)
732 if (!check_no_attributes (parser, "fork", attribute_names, attribute_values, error))
735 if (push_element (parser, ELEMENT_FORK) == NULL)
745 else if (element_type == ELEMENT_SYSLOG)
747 if (!check_no_attributes (parser, "syslog", attribute_names, attribute_values, error))
750 if (push_element (parser, ELEMENT_SYSLOG) == NULL)
756 parser->syslog = TRUE;
760 else if (element_type == ELEMENT_KEEP_UMASK)
762 if (!check_no_attributes (parser, "keep_umask", attribute_names, attribute_values, error))
765 if (push_element (parser, ELEMENT_KEEP_UMASK) == NULL)
771 parser->keep_umask = TRUE;
775 else if (element_type == ELEMENT_PIDFILE)
777 if (!check_no_attributes (parser, "pidfile", attribute_names, attribute_values, error))
780 if (push_element (parser, ELEMENT_PIDFILE) == NULL)
788 else if (element_type == ELEMENT_LISTEN)
790 if (!check_no_attributes (parser, "listen", attribute_names, attribute_values, error))
793 if (push_element (parser, ELEMENT_LISTEN) == NULL)
801 else if (element_type == ELEMENT_AUTH)
803 if (!check_no_attributes (parser, "auth", attribute_names, attribute_values, error))
806 if (push_element (parser, ELEMENT_AUTH) == NULL)
814 else if (element_type == ELEMENT_SERVICEHELPER)
816 if (!check_no_attributes (parser, "servicehelper", attribute_names, attribute_values, error))
819 if (push_element (parser, ELEMENT_SERVICEHELPER) == NULL)
827 else if (element_type == ELEMENT_INCLUDEDIR)
829 if (!check_no_attributes (parser, "includedir", attribute_names, attribute_values, error))
832 if (push_element (parser, ELEMENT_INCLUDEDIR) == NULL)
840 else if (element_type == ELEMENT_STANDARD_SESSION_SERVICEDIRS)
846 if (!check_no_attributes (parser, "standard_session_servicedirs", attribute_names, attribute_values, error))
849 if (push_element (parser, ELEMENT_STANDARD_SESSION_SERVICEDIRS) == NULL)
855 if (!_dbus_get_standard_session_servicedirs (&dirs))
861 while ((link = _dbus_list_pop_first_link (&dirs)))
862 service_dirs_append_link_unique_or_free (&parser->service_dirs, link);
866 else if (element_type == ELEMENT_STANDARD_SYSTEM_SERVICEDIRS)
872 if (!check_no_attributes (parser, "standard_system_servicedirs", attribute_names, attribute_values, error))
875 if (push_element (parser, ELEMENT_STANDARD_SYSTEM_SERVICEDIRS) == NULL)
881 if (!_dbus_get_standard_system_servicedirs (&dirs))
887 while ((link = _dbus_list_pop_first_link (&dirs)))
888 service_dirs_append_link_unique_or_free (&parser->service_dirs, link);
892 else if (element_type == ELEMENT_ALLOW_ANONYMOUS)
894 if (!check_no_attributes (parser, "allow_anonymous", attribute_names, attribute_values, error))
897 if (push_element (parser, ELEMENT_ALLOW_ANONYMOUS) == NULL)
903 parser->allow_anonymous = TRUE;
906 else if (element_type == ELEMENT_SERVICEDIR)
908 if (!check_no_attributes (parser, "servicedir", attribute_names, attribute_values, error))
911 if (push_element (parser, ELEMENT_SERVICEDIR) == NULL)
919 else if (element_type == ELEMENT_INCLUDE)
922 const char *if_selinux_enabled;
923 const char *ignore_missing;
924 const char *selinux_root_relative;
926 if ((e = push_element (parser, ELEMENT_INCLUDE)) == NULL)
932 e->d.include.ignore_missing = FALSE;
933 e->d.include.if_selinux_enabled = FALSE;
934 e->d.include.selinux_root_relative = FALSE;
936 if (!locate_attributes (parser, "include",
940 "ignore_missing", &ignore_missing,
941 "if_selinux_enabled", &if_selinux_enabled,
942 "selinux_root_relative", &selinux_root_relative,
946 if (ignore_missing != NULL)
948 if (strcmp (ignore_missing, "yes") == 0)
949 e->d.include.ignore_missing = TRUE;
950 else if (strcmp (ignore_missing, "no") == 0)
951 e->d.include.ignore_missing = FALSE;
954 dbus_set_error (error, DBUS_ERROR_FAILED,
955 "ignore_missing attribute must have value \"yes\" or \"no\"");
960 if (if_selinux_enabled != NULL)
962 if (strcmp (if_selinux_enabled, "yes") == 0)
963 e->d.include.if_selinux_enabled = TRUE;
964 else if (strcmp (if_selinux_enabled, "no") == 0)
965 e->d.include.if_selinux_enabled = FALSE;
968 dbus_set_error (error, DBUS_ERROR_FAILED,
969 "if_selinux_enabled attribute must have value"
970 " \"yes\" or \"no\"");
975 if (selinux_root_relative != NULL)
977 if (strcmp (selinux_root_relative, "yes") == 0)
978 e->d.include.selinux_root_relative = TRUE;
979 else if (strcmp (selinux_root_relative, "no") == 0)
980 e->d.include.selinux_root_relative = FALSE;
983 dbus_set_error (error, DBUS_ERROR_FAILED,
984 "selinux_root_relative attribute must have value"
985 " \"yes\" or \"no\"");
992 else if (element_type == ELEMENT_POLICY)
998 const char *at_console;
1001 if ((e = push_element (parser, ELEMENT_POLICY)) == NULL)
1003 BUS_SET_OOM (error);
1007 e->d.policy.type = POLICY_IGNORED;
1009 if (!locate_attributes (parser, "policy",
1013 "context", &context,
1017 "at_console", &at_console,
1021 if (((context != NULL) + (user != NULL) + (group != NULL) +
1022 (smack != NULL) + (at_console != NULL)) != 1)
1024 dbus_set_error (error, DBUS_ERROR_FAILED,
1025 "<policy> element must have exactly one of (context|user|group|smack|at_console) attributes");
1029 if (context != NULL)
1031 if (strcmp (context, "default") == 0)
1033 e->d.policy.type = POLICY_DEFAULT;
1035 else if (strcmp (context, "mandatory") == 0)
1037 e->d.policy.type = POLICY_MANDATORY;
1041 dbus_set_error (error, DBUS_ERROR_FAILED,
1042 "context attribute on <policy> must have the value \"default\" or \"mandatory\", not \"%s\"",
1047 else if (user != NULL)
1049 DBusString username;
1050 _dbus_string_init_const (&username, user);
1052 if (_dbus_parse_unix_user_from_config (&username,
1053 &e->d.policy.gid_uid_or_at_console))
1054 e->d.policy.type = POLICY_USER;
1056 _dbus_warn ("Unknown username \"%s\" in message bus configuration file\n",
1059 else if (group != NULL)
1061 DBusString group_name;
1062 _dbus_string_init_const (&group_name, group);
1064 if (_dbus_parse_unix_group_from_config (&group_name,
1065 &e->d.policy.gid_uid_or_at_console))
1066 e->d.policy.type = POLICY_GROUP;
1068 _dbus_warn ("Unknown group \"%s\" in message bus configuration file\n",
1071 else if (smack != NULL)
1073 e->d.policy.type = POLICY_SMACK;
1074 e->d.policy.smack_label = _dbus_strdup (smack);
1075 if (e->d.policy.smack_label == NULL)
1077 BUS_SET_OOM (error);
1081 else if (at_console != NULL)
1084 t = (strcmp (at_console, "true") == 0);
1085 if (t || strcmp (at_console, "false") == 0)
1087 e->d.policy.gid_uid_or_at_console = t;
1088 e->d.policy.type = POLICY_CONSOLE;
1092 dbus_set_error (error, DBUS_ERROR_FAILED,
1093 "Unknown value \"%s\" for at_console in message bus configuration file",
1101 _dbus_assert_not_reached ("all <policy> attributes null and we didn't set error");
1106 else if (element_type == ELEMENT_LIMIT)
1111 if ((e = push_element (parser, ELEMENT_LIMIT)) == NULL)
1113 BUS_SET_OOM (error);
1117 if (!locate_attributes (parser, "limit",
1127 dbus_set_error (error, DBUS_ERROR_FAILED,
1128 "<limit> element must have a \"name\" attribute");
1132 e->d.limit.name = _dbus_strdup (name);
1133 if (e->d.limit.name == NULL)
1135 BUS_SET_OOM (error);
1141 else if (element_type == ELEMENT_SELINUX)
1143 if (!check_no_attributes (parser, "selinux", attribute_names, attribute_values, error))
1146 if (push_element (parser, ELEMENT_SELINUX) == NULL)
1148 BUS_SET_OOM (error);
1154 else if (element_type == ELEMENT_APPARMOR)
1159 if ((e = push_element (parser, ELEMENT_APPARMOR)) == NULL)
1161 BUS_SET_OOM (error);
1165 if (!locate_attributes (parser, "apparmor",
1173 return bus_apparmor_set_mode_from_config (mode, error);
1177 dbus_set_error (error, DBUS_ERROR_FAILED,
1178 "Element <%s> not allowed inside <%s> in configuration file",
1179 element_name, "busconfig");
1185 append_rule_from_element (BusConfigParser *parser,
1186 const char *element_name,
1187 const char **attribute_names,
1188 const char **attribute_values,
1193 const char *send_interface;
1194 const char *send_member;
1195 const char *send_error;
1196 const char *send_destination;
1197 const char *send_path;
1198 const char *send_type;
1199 const char *receive_interface;
1200 const char *receive_member;
1201 const char *receive_error;
1202 const char *receive_sender;
1203 const char *receive_path;
1204 const char *receive_type;
1205 const char *eavesdrop;
1206 const char *send_requested_reply;
1207 const char *receive_requested_reply;
1209 const char *own_prefix;
1213 BusPolicyRule *rule;
1215 if (!locate_attributes (parser, element_name,
1219 "send_interface", &send_interface,
1220 "send_member", &send_member,
1221 "send_error", &send_error,
1222 "send_destination", &send_destination,
1223 "send_path", &send_path,
1224 "send_type", &send_type,
1225 "receive_interface", &receive_interface,
1226 "receive_member", &receive_member,
1227 "receive_error", &receive_error,
1228 "receive_sender", &receive_sender,
1229 "receive_path", &receive_path,
1230 "receive_type", &receive_type,
1231 "eavesdrop", &eavesdrop,
1232 "send_requested_reply", &send_requested_reply,
1233 "receive_requested_reply", &receive_requested_reply,
1235 "own_prefix", &own_prefix,
1242 if (!(send_interface || send_member || send_error || send_destination ||
1243 send_type || send_path ||
1244 receive_interface || receive_member || receive_error || receive_sender ||
1245 receive_type || receive_path || eavesdrop ||
1246 send_requested_reply || receive_requested_reply ||
1247 own || own_prefix || user || group))
1249 dbus_set_error (error, DBUS_ERROR_FAILED,
1250 "Element <%s> must have one or more attributes",
1255 if ((send_member && (send_interface == NULL && send_path == NULL)) ||
1256 (receive_member && (receive_interface == NULL && receive_path == NULL)))
1258 dbus_set_error (error, DBUS_ERROR_FAILED,
1259 "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.",
1264 /* Allowed combinations of elements are:
1266 * base, must be all send or all receive:
1269 * interface + member
1272 * base send_ can combine with send_destination, send_path, send_type, send_requested_reply
1273 * base receive_ with receive_sender, receive_path, receive_type, receive_requested_reply, eavesdrop
1275 * user, group, own, own_prefix must occur alone
1277 * Pretty sure the below stuff is broken, FIXME think about it more.
1280 if ((send_interface && (send_error ||
1281 receive_interface ||
1285 receive_requested_reply ||
1286 own || own_prefix ||
1290 (send_member && (send_error ||
1291 receive_interface ||
1295 receive_requested_reply ||
1296 own || own_prefix ||
1300 (send_error && (receive_interface ||
1304 receive_requested_reply ||
1305 own || own_prefix ||
1309 (send_destination && (receive_interface ||
1313 receive_requested_reply ||
1314 own || own_prefix ||
1318 (send_type && (receive_interface ||
1322 receive_requested_reply ||
1323 own || own_prefix ||
1327 (send_path && (receive_interface ||
1331 receive_requested_reply ||
1332 own || own_prefix ||
1336 (send_requested_reply && (receive_interface ||
1340 receive_requested_reply ||
1341 own || own_prefix ||
1345 (receive_interface && (receive_error ||
1346 own || own_prefix ||
1350 (receive_member && (receive_error ||
1351 own || own_prefix ||
1355 (receive_error && (own || own_prefix ||
1359 (eavesdrop && (own || own_prefix ||
1363 (receive_requested_reply && (own || own_prefix ||
1367 (own && (own_prefix || user || group)) ||
1369 (own_prefix && (own || user || group)) ||
1373 dbus_set_error (error, DBUS_ERROR_FAILED,
1374 "Invalid combination of attributes on element <%s>",
1381 /* In BusPolicyRule, NULL represents wildcard.
1382 * In the config file, '*' represents it.
1384 #define IS_WILDCARD(str) ((str) && ((str)[0]) == '*' && ((str)[1]) == '\0')
1386 if (send_interface || send_member || send_error || send_destination ||
1387 send_path || send_type || send_requested_reply)
1391 if (IS_WILDCARD (send_interface))
1392 send_interface = NULL;
1393 if (IS_WILDCARD (send_member))
1395 if (IS_WILDCARD (send_error))
1397 if (IS_WILDCARD (send_destination))
1398 send_destination = NULL;
1399 if (IS_WILDCARD (send_path))
1401 if (IS_WILDCARD (send_type))
1404 message_type = DBUS_MESSAGE_TYPE_INVALID;
1405 if (send_type != NULL)
1407 message_type = dbus_message_type_from_string (send_type);
1408 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1410 dbus_set_error (error, DBUS_ERROR_FAILED,
1411 "Bad message type \"%s\"",
1418 !(strcmp (eavesdrop, "true") == 0 ||
1419 strcmp (eavesdrop, "false") == 0))
1421 dbus_set_error (error, DBUS_ERROR_FAILED,
1422 "Bad value \"%s\" for %s attribute, must be true or false",
1423 "eavesdrop", eavesdrop);
1427 if (send_requested_reply &&
1428 !(strcmp (send_requested_reply, "true") == 0 ||
1429 strcmp (send_requested_reply, "false") == 0))
1431 dbus_set_error (error, DBUS_ERROR_FAILED,
1432 "Bad value \"%s\" for %s attribute, must be true or false",
1433 "send_requested_reply", send_requested_reply);
1437 rule = bus_policy_rule_new (BUS_POLICY_RULE_SEND, allow);
1442 rule->d.send.eavesdrop = (strcmp (eavesdrop, "true") == 0);
1445 rule->d.send.log = (strcmp (log, "true") == 0);
1447 if (send_requested_reply)
1448 rule->d.send.requested_reply = (strcmp (send_requested_reply, "true") == 0);
1450 rule->d.send.message_type = message_type;
1451 rule->d.send.path = _dbus_strdup (send_path);
1452 rule->d.send.interface = _dbus_strdup (send_interface);
1453 rule->d.send.member = _dbus_strdup (send_member);
1454 rule->d.send.error = _dbus_strdup (send_error);
1455 rule->d.send.destination = _dbus_strdup (send_destination);
1456 if (send_path && rule->d.send.path == NULL)
1458 if (send_interface && rule->d.send.interface == NULL)
1460 if (send_member && rule->d.send.member == NULL)
1462 if (send_error && rule->d.send.error == NULL)
1464 if (send_destination && rule->d.send.destination == NULL)
1467 else if (receive_interface || receive_member || receive_error || receive_sender ||
1468 receive_path || receive_type || eavesdrop || receive_requested_reply)
1472 if (IS_WILDCARD (receive_interface))
1473 receive_interface = NULL;
1474 if (IS_WILDCARD (receive_member))
1475 receive_member = NULL;
1476 if (IS_WILDCARD (receive_error))
1477 receive_error = NULL;
1478 if (IS_WILDCARD (receive_sender))
1479 receive_sender = NULL;
1480 if (IS_WILDCARD (receive_path))
1481 receive_path = NULL;
1482 if (IS_WILDCARD (receive_type))
1483 receive_type = NULL;
1485 message_type = DBUS_MESSAGE_TYPE_INVALID;
1486 if (receive_type != NULL)
1488 message_type = dbus_message_type_from_string (receive_type);
1489 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1491 dbus_set_error (error, DBUS_ERROR_FAILED,
1492 "Bad message type \"%s\"",
1500 !(strcmp (eavesdrop, "true") == 0 ||
1501 strcmp (eavesdrop, "false") == 0))
1503 dbus_set_error (error, DBUS_ERROR_FAILED,
1504 "Bad value \"%s\" for %s attribute, must be true or false",
1505 "eavesdrop", eavesdrop);
1509 if (receive_requested_reply &&
1510 !(strcmp (receive_requested_reply, "true") == 0 ||
1511 strcmp (receive_requested_reply, "false") == 0))
1513 dbus_set_error (error, DBUS_ERROR_FAILED,
1514 "Bad value \"%s\" for %s attribute, must be true or false",
1515 "receive_requested_reply", receive_requested_reply);
1519 rule = bus_policy_rule_new (BUS_POLICY_RULE_RECEIVE, allow);
1524 rule->d.receive.eavesdrop = (strcmp (eavesdrop, "true") == 0);
1526 if (receive_requested_reply)
1527 rule->d.receive.requested_reply = (strcmp (receive_requested_reply, "true") == 0);
1529 rule->d.receive.message_type = message_type;
1530 rule->d.receive.path = _dbus_strdup (receive_path);
1531 rule->d.receive.interface = _dbus_strdup (receive_interface);
1532 rule->d.receive.member = _dbus_strdup (receive_member);
1533 rule->d.receive.error = _dbus_strdup (receive_error);
1534 rule->d.receive.origin = _dbus_strdup (receive_sender);
1536 if (receive_path && rule->d.receive.path == NULL)
1538 if (receive_interface && rule->d.receive.interface == NULL)
1540 if (receive_member && rule->d.receive.member == NULL)
1542 if (receive_error && rule->d.receive.error == NULL)
1544 if (receive_sender && rule->d.receive.origin == NULL)
1547 else if (own || own_prefix)
1549 rule = bus_policy_rule_new (BUS_POLICY_RULE_OWN, allow);
1555 if (IS_WILDCARD (own))
1558 rule->d.own.prefix = 0;
1559 rule->d.own.service_name = _dbus_strdup (own);
1560 if (own && rule->d.own.service_name == NULL)
1565 rule->d.own.prefix = 1;
1566 rule->d.own.service_name = _dbus_strdup (own_prefix);
1567 if (rule->d.own.service_name == NULL)
1573 if (IS_WILDCARD (user))
1575 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1579 rule->d.user.uid = DBUS_UID_UNSET;
1583 DBusString username;
1586 _dbus_string_init_const (&username, user);
1588 if (_dbus_parse_unix_user_from_config (&username, &uid))
1590 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1594 rule->d.user.uid = uid;
1598 _dbus_warn ("Unknown username \"%s\" on element <%s>\n",
1599 user, element_name);
1605 if (IS_WILDCARD (group))
1607 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1611 rule->d.group.gid = DBUS_GID_UNSET;
1615 DBusString groupname;
1618 _dbus_string_init_const (&groupname, group);
1620 if (_dbus_parse_unix_group_from_config (&groupname, &gid))
1622 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1626 rule->d.group.gid = gid;
1630 _dbus_warn ("Unknown group \"%s\" on element <%s>\n",
1631 group, element_name);
1636 _dbus_assert_not_reached ("Did not handle some combination of attributes on <allow> or <deny>");
1642 pe = peek_element (parser);
1643 _dbus_assert (pe != NULL);
1644 _dbus_assert (pe->type == ELEMENT_POLICY);
1646 switch (pe->d.policy.type)
1648 case POLICY_IGNORED:
1649 /* drop the rule on the floor */
1652 case POLICY_DEFAULT:
1653 if (!bus_policy_append_default_rule (parser->policy, rule))
1656 case POLICY_MANDATORY:
1657 if (!bus_policy_append_mandatory_rule (parser->policy, rule))
1661 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1663 dbus_set_error (error, DBUS_ERROR_FAILED,
1664 "<%s> rule cannot be per-user because it has bus-global semantics",
1669 if (!bus_policy_append_user_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1674 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1676 dbus_set_error (error, DBUS_ERROR_FAILED,
1677 "<%s> rule cannot be per-group because it has bus-global semantics",
1682 if (!bus_policy_append_group_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1687 if (!bus_policy_append_smack_rule (parser->policy, pe->d.policy.smack_label, rule))
1690 case POLICY_CONSOLE:
1691 if (!bus_policy_append_console_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1697 bus_policy_rule_unref (rule);
1704 BUS_SET_OOM (error);
1707 bus_policy_rule_unref (rule);
1712 start_policy_child (BusConfigParser *parser,
1713 const char *element_name,
1714 const char **attribute_names,
1715 const char **attribute_values,
1718 if (strcmp (element_name, "allow") == 0)
1720 if (!append_rule_from_element (parser, element_name,
1721 attribute_names, attribute_values,
1725 if (push_element (parser, ELEMENT_ALLOW) == NULL)
1727 BUS_SET_OOM (error);
1733 else if (strcmp (element_name, "deny") == 0)
1735 if (!append_rule_from_element (parser, element_name,
1736 attribute_names, attribute_values,
1740 if (push_element (parser, ELEMENT_DENY) == NULL)
1742 BUS_SET_OOM (error);
1750 dbus_set_error (error, DBUS_ERROR_FAILED,
1751 "Element <%s> not allowed inside <%s> in configuration file",
1752 element_name, "policy");
1758 start_selinux_child (BusConfigParser *parser,
1759 const char *element_name,
1760 const char **attribute_names,
1761 const char **attribute_values,
1768 context_copy = NULL;
1770 if (strcmp (element_name, "associate") == 0)
1773 const char *context;
1775 if (!locate_attributes (parser, "associate",
1780 "context", &context,
1784 if (push_element (parser, ELEMENT_ASSOCIATE) == NULL)
1786 BUS_SET_OOM (error);
1790 if (own == NULL || context == NULL)
1792 dbus_set_error (error, DBUS_ERROR_FAILED,
1793 "Element <associate> must have attributes own=\"<servicename>\" and context=\"<selinux context>\"");
1797 own_copy = _dbus_strdup (own);
1798 if (own_copy == NULL)
1800 context_copy = _dbus_strdup (context);
1801 if (context_copy == NULL)
1804 if (!_dbus_hash_table_insert_string (parser->service_context_table,
1805 own_copy, context_copy))
1812 dbus_set_error (error, DBUS_ERROR_FAILED,
1813 "Element <%s> not allowed inside <%s> in configuration file",
1814 element_name, "selinux");
1820 dbus_free (own_copy);
1823 dbus_free (context_copy);
1825 BUS_SET_OOM (error);
1830 bus_config_parser_start_element (BusConfigParser *parser,
1831 const char *element_name,
1832 const char **attribute_names,
1833 const char **attribute_values,
1838 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1840 /* printf ("START: %s\n", element_name); */
1842 t = top_element_type (parser);
1844 if (t == ELEMENT_NONE)
1846 if (strcmp (element_name, "busconfig") == 0)
1848 if (!check_no_attributes (parser, "busconfig", attribute_names, attribute_values, error))
1851 if (push_element (parser, ELEMENT_BUSCONFIG) == NULL)
1853 BUS_SET_OOM (error);
1861 dbus_set_error (error, DBUS_ERROR_FAILED,
1862 "Unknown element <%s> at root of configuration file",
1867 else if (t == ELEMENT_BUSCONFIG)
1869 return start_busconfig_child (parser, element_name,
1870 attribute_names, attribute_values,
1873 else if (t == ELEMENT_POLICY)
1875 return start_policy_child (parser, element_name,
1876 attribute_names, attribute_values,
1879 else if (t == ELEMENT_SELINUX)
1881 return start_selinux_child (parser, element_name,
1882 attribute_names, attribute_values,
1887 dbus_set_error (error, DBUS_ERROR_FAILED,
1888 "Element <%s> is not allowed in this context",
1895 set_limit (BusConfigParser *parser,
1900 dbus_bool_t must_be_positive;
1901 dbus_bool_t must_be_int;
1903 must_be_int = FALSE;
1904 must_be_positive = FALSE;
1906 if (strcmp (name, "max_incoming_bytes") == 0)
1908 must_be_positive = TRUE;
1909 parser->limits.max_incoming_bytes = value;
1911 else if (strcmp (name, "max_incoming_unix_fds") == 0)
1913 must_be_positive = TRUE;
1914 parser->limits.max_incoming_unix_fds = value;
1916 else if (strcmp (name, "max_outgoing_bytes") == 0)
1918 must_be_positive = TRUE;
1919 parser->limits.max_outgoing_bytes = value;
1921 else if (strcmp (name, "max_outgoing_unix_fds") == 0)
1923 must_be_positive = TRUE;
1924 parser->limits.max_outgoing_unix_fds = value;
1926 else if (strcmp (name, "max_message_size") == 0)
1928 must_be_positive = TRUE;
1929 parser->limits.max_message_size = value;
1931 else if (strcmp (name, "max_message_unix_fds") == 0)
1933 must_be_positive = TRUE;
1934 parser->limits.max_message_unix_fds = value;
1936 else if (strcmp (name, "service_start_timeout") == 0)
1938 must_be_positive = TRUE;
1940 parser->limits.activation_timeout = value;
1942 else if (strcmp (name, "auth_timeout") == 0)
1944 must_be_positive = TRUE;
1946 parser->limits.auth_timeout = value;
1948 else if (strcmp (name, "pending_fd_timeout") == 0)
1950 must_be_positive = TRUE;
1952 parser->limits.pending_fd_timeout = value;
1954 else if (strcmp (name, "reply_timeout") == 0)
1956 must_be_positive = TRUE;
1958 parser->limits.reply_timeout = value;
1960 else if (strcmp (name, "max_completed_connections") == 0)
1962 must_be_positive = TRUE;
1964 parser->limits.max_completed_connections = value;
1966 else if (strcmp (name, "max_incomplete_connections") == 0)
1968 must_be_positive = TRUE;
1970 parser->limits.max_incomplete_connections = value;
1972 else if (strcmp (name, "max_connections_per_user") == 0)
1974 must_be_positive = TRUE;
1976 parser->limits.max_connections_per_user = value;
1978 else if (strcmp (name, "max_pending_service_starts") == 0)
1980 must_be_positive = TRUE;
1982 parser->limits.max_pending_activations = value;
1984 else if (strcmp (name, "max_names_per_connection") == 0)
1986 must_be_positive = TRUE;
1988 parser->limits.max_services_per_connection = value;
1990 else if (strcmp (name, "max_match_rules_per_connection") == 0)
1992 must_be_positive = TRUE;
1994 parser->limits.max_match_rules_per_connection = value;
1996 else if (strcmp (name, "max_replies_per_connection") == 0)
1998 must_be_positive = TRUE;
2000 parser->limits.max_replies_per_connection = value;
2004 dbus_set_error (error, DBUS_ERROR_FAILED,
2005 "There is no limit called \"%s\"\n",
2010 if (must_be_positive && value < 0)
2012 dbus_set_error (error, DBUS_ERROR_FAILED,
2013 "<limit name=\"%s\"> must be a positive number\n",
2019 (value < _DBUS_INT_MIN || value > _DBUS_INT_MAX))
2021 dbus_set_error (error, DBUS_ERROR_FAILED,
2022 "<limit name=\"%s\"> value is too large\n",
2031 bus_config_parser_end_element (BusConfigParser *parser,
2032 const char *element_name,
2039 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2041 /* printf ("END: %s\n", element_name); */
2043 t = top_element_type (parser);
2045 if (t == ELEMENT_NONE)
2047 /* should probably be an assertion failure but
2048 * being paranoid about XML parsers
2050 dbus_set_error (error, DBUS_ERROR_FAILED,
2051 "XML parser ended element with no element on the stack");
2055 n = bus_config_parser_element_type_to_name (t);
2056 _dbus_assert (n != NULL);
2057 if (strcmp (n, element_name) != 0)
2059 /* should probably be an assertion failure but
2060 * being paranoid about XML parsers
2062 dbus_set_error (error, DBUS_ERROR_FAILED,
2063 "XML element <%s> ended but topmost element on the stack was <%s>",
2068 e = peek_element (parser);
2069 _dbus_assert (e != NULL);
2074 _dbus_assert_not_reached ("element in stack has no type");
2077 case ELEMENT_INCLUDE:
2079 case ELEMENT_CONFIGTYPE:
2080 case ELEMENT_LISTEN:
2081 case ELEMENT_PIDFILE:
2083 case ELEMENT_SERVICEDIR:
2084 case ELEMENT_SERVICEHELPER:
2085 case ELEMENT_INCLUDEDIR:
2087 if (!e->had_content)
2089 dbus_set_error (error, DBUS_ERROR_FAILED,
2090 "XML element <%s> was expected to have content inside it",
2091 bus_config_parser_element_type_to_name (e->type));
2095 if (e->type == ELEMENT_LIMIT)
2097 if (!set_limit (parser, e->d.limit.name, e->d.limit.value,
2103 case ELEMENT_BUSCONFIG:
2104 case ELEMENT_POLICY:
2108 case ELEMENT_SYSLOG:
2109 case ELEMENT_KEEP_UMASK:
2110 case ELEMENT_SELINUX:
2111 case ELEMENT_ASSOCIATE:
2112 case ELEMENT_STANDARD_SESSION_SERVICEDIRS:
2113 case ELEMENT_STANDARD_SYSTEM_SERVICEDIRS:
2114 case ELEMENT_ALLOW_ANONYMOUS:
2115 case ELEMENT_APPARMOR:
2119 pop_element (parser);
2125 all_whitespace (const DBusString *str)
2129 _dbus_string_skip_white (str, 0, &i);
2131 return i == _dbus_string_get_length (str);
2135 make_full_path (const DBusString *basedir,
2136 const DBusString *filename,
2137 DBusString *full_path)
2139 if (_dbus_path_is_absolute (filename))
2141 return _dbus_string_copy (filename, 0, full_path, 0);
2145 if (!_dbus_string_copy (basedir, 0, full_path, 0))
2148 if (!_dbus_concat_dir_and_file (full_path, filename))
2156 include_file (BusConfigParser *parser,
2157 const DBusString *filename,
2158 dbus_bool_t ignore_missing,
2161 /* FIXME good test case for this would load each config file in the
2162 * test suite both alone, and as an include, and check
2163 * that the result is the same
2165 BusConfigParser *included;
2166 const char *filename_str;
2167 DBusError tmp_error;
2169 dbus_error_init (&tmp_error);
2171 filename_str = _dbus_string_get_const_data (filename);
2173 /* Check to make sure this file hasn't already been included. */
2174 if (seen_include (parser, filename))
2176 dbus_set_error (error, DBUS_ERROR_FAILED,
2177 "Circular inclusion of file '%s'",
2182 if (! _dbus_list_append (&parser->included_files, (void *) filename_str))
2184 BUS_SET_OOM (error);
2188 /* Since parser is passed in as the parent, included
2189 inherits parser's limits. */
2190 included = bus_config_load (filename, FALSE, parser, &tmp_error);
2192 _dbus_list_pop_last (&parser->included_files);
2194 if (included == NULL)
2196 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
2198 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_FILE_NOT_FOUND) &&
2201 dbus_error_free (&tmp_error);
2206 dbus_move_error (&tmp_error, error);
2212 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
2214 if (!merge_included (parser, included, error))
2216 bus_config_parser_unref (included);
2220 /* Copy included's limits back to parser. */
2221 parser->limits = included->limits;
2223 bus_config_parser_unref (included);
2229 servicehelper_path (BusConfigParser *parser,
2230 const DBusString *filename,
2233 const char *filename_str;
2234 char *servicehelper;
2236 filename_str = _dbus_string_get_const_data (filename);
2238 /* copy to avoid overwriting with NULL on OOM */
2239 servicehelper = _dbus_strdup (filename_str);
2242 if (servicehelper == NULL)
2244 BUS_SET_OOM (error);
2248 /* save the latest servicehelper only if not OOM */
2249 dbus_free (parser->servicehelper);
2250 parser->servicehelper = servicehelper;
2252 /* We don't check whether the helper exists; instead we
2253 * would just fail to ever activate anything if it doesn't.
2254 * This allows an admin to fix the problem if it doesn't exist.
2255 * It also allows the parser test suite to successfully parse
2256 * test cases without installing the helper. ;-)
2263 include_dir (BusConfigParser *parser,
2264 const DBusString *dirname,
2267 DBusString filename;
2269 DBusError tmp_error;
2273 if (!_dbus_string_init (&filename))
2275 BUS_SET_OOM (error);
2281 dir = _dbus_directory_open (dirname, error);
2285 if (dbus_error_has_name (error, DBUS_ERROR_FILE_NOT_FOUND))
2287 dbus_error_free (error);
2294 dbus_error_init (&tmp_error);
2295 while (_dbus_directory_get_next_file (dir, &filename, &tmp_error))
2297 DBusString full_path;
2299 if (!_dbus_string_init (&full_path))
2301 BUS_SET_OOM (error);
2305 if (!_dbus_string_copy (dirname, 0, &full_path, 0))
2307 BUS_SET_OOM (error);
2308 _dbus_string_free (&full_path);
2312 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2314 BUS_SET_OOM (error);
2315 _dbus_string_free (&full_path);
2319 if (_dbus_string_ends_with_c_str (&full_path, ".conf"))
2321 if (!include_file (parser, &full_path, TRUE, error))
2323 if (dbus_error_is_set (error))
2325 /* We log to syslog unconditionally here, because this is
2326 * the configuration parser, so we don't yet know whether
2327 * this bus is going to want to write to syslog! (There's
2328 * also some layer inversion going on, if we want to use
2329 * the bus context.) */
2330 _dbus_system_log (DBUS_SYSTEM_LOG_INFO,
2331 "Encountered error '%s' while parsing '%s'\n",
2333 _dbus_string_get_const_data (&full_path));
2334 dbus_error_free (error);
2339 _dbus_string_free (&full_path);
2342 if (dbus_error_is_set (&tmp_error))
2344 dbus_move_error (&tmp_error, error);
2349 if (!_dbus_string_copy_data (dirname, &s))
2351 BUS_SET_OOM (error);
2355 if (!_dbus_list_append (&parser->conf_dirs, s))
2358 BUS_SET_OOM (error);
2366 _dbus_string_free (&filename);
2369 _dbus_directory_close (dir);
2375 bus_config_parser_content (BusConfigParser *parser,
2376 const DBusString *content,
2381 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2387 _dbus_string_get_const_data (content, &c_str);
2389 printf ("CONTENT %d bytes: %s\n", _dbus_string_get_length (content), c_str);
2393 e = peek_element (parser);
2396 dbus_set_error (error, DBUS_ERROR_FAILED,
2397 "Text content outside of any XML element in configuration file");
2400 else if (e->had_content)
2402 _dbus_assert_not_reached ("Element had multiple content blocks");
2406 switch (top_element_type (parser))
2409 _dbus_assert_not_reached ("element at top of stack has no type");
2412 case ELEMENT_BUSCONFIG:
2413 case ELEMENT_POLICY:
2417 case ELEMENT_SYSLOG:
2418 case ELEMENT_KEEP_UMASK:
2419 case ELEMENT_STANDARD_SESSION_SERVICEDIRS:
2420 case ELEMENT_STANDARD_SYSTEM_SERVICEDIRS:
2421 case ELEMENT_ALLOW_ANONYMOUS:
2422 case ELEMENT_SELINUX:
2423 case ELEMENT_ASSOCIATE:
2424 case ELEMENT_APPARMOR:
2425 if (all_whitespace (content))
2429 dbus_set_error (error, DBUS_ERROR_FAILED,
2430 "No text content expected inside XML element %s in configuration file",
2431 bus_config_parser_element_type_to_name (top_element_type (parser)));
2435 case ELEMENT_PIDFILE:
2439 e->had_content = TRUE;
2441 if (!_dbus_string_copy_data (content, &s))
2444 dbus_free (parser->pidfile);
2445 parser->pidfile = s;
2449 case ELEMENT_INCLUDE:
2451 DBusString full_path, selinux_policy_root;
2453 e->had_content = TRUE;
2455 if (e->d.include.if_selinux_enabled
2456 && !bus_selinux_enabled ())
2459 if (!_dbus_string_init (&full_path))
2462 if (e->d.include.selinux_root_relative)
2464 if (!bus_selinux_get_policy_root ())
2466 dbus_set_error (error, DBUS_ERROR_FAILED,
2467 "Could not determine SELinux policy root for relative inclusion");
2468 _dbus_string_free (&full_path);
2471 _dbus_string_init_const (&selinux_policy_root,
2472 bus_selinux_get_policy_root ());
2473 if (!make_full_path (&selinux_policy_root, content, &full_path))
2475 _dbus_string_free (&full_path);
2479 else if (!make_full_path (&parser->basedir, content, &full_path))
2481 _dbus_string_free (&full_path);
2485 if (!include_file (parser, &full_path,
2486 e->d.include.ignore_missing, error))
2488 _dbus_string_free (&full_path);
2492 _dbus_string_free (&full_path);
2496 case ELEMENT_SERVICEHELPER:
2498 DBusString full_path;
2500 e->had_content = TRUE;
2502 if (!_dbus_string_init (&full_path))
2505 if (!make_full_path (&parser->basedir, content, &full_path))
2507 _dbus_string_free (&full_path);
2511 if (!servicehelper_path (parser, &full_path, error))
2513 _dbus_string_free (&full_path);
2517 _dbus_string_free (&full_path);
2521 case ELEMENT_INCLUDEDIR:
2523 DBusString full_path;
2525 e->had_content = TRUE;
2527 if (!_dbus_string_init (&full_path))
2530 if (!make_full_path (&parser->basedir, content, &full_path))
2532 _dbus_string_free (&full_path);
2536 if (!include_dir (parser, &full_path, error))
2538 _dbus_string_free (&full_path);
2542 _dbus_string_free (&full_path);
2550 e->had_content = TRUE;
2552 if (!_dbus_string_copy_data (content, &s))
2555 dbus_free (parser->user);
2560 case ELEMENT_CONFIGTYPE:
2564 e->had_content = TRUE;
2566 if (!_dbus_string_copy_data (content, &s))
2569 dbus_free (parser->bus_type);
2570 parser->bus_type = s;
2574 case ELEMENT_LISTEN:
2578 e->had_content = TRUE;
2580 if (!_dbus_string_copy_data (content, &s))
2583 if (!_dbus_list_append (&parser->listen_on,
2596 e->had_content = TRUE;
2598 if (!_dbus_string_copy_data (content, &s))
2601 if (!_dbus_list_append (&parser->mechanisms,
2610 case ELEMENT_SERVICEDIR:
2613 DBusString full_path;
2615 e->had_content = TRUE;
2617 if (!_dbus_string_init (&full_path))
2620 if (!make_full_path (&parser->basedir, content, &full_path))
2622 _dbus_string_free (&full_path);
2626 if (!_dbus_string_copy_data (&full_path, &s))
2628 _dbus_string_free (&full_path);
2632 /* _only_ extra session directories can be specified */
2633 if (!service_dirs_append_unique_or_free (&parser->service_dirs, s))
2635 _dbus_string_free (&full_path);
2640 _dbus_string_free (&full_path);
2648 e->had_content = TRUE;
2651 if (!_dbus_string_parse_int (content, 0, &val, NULL))
2653 dbus_set_error (error, DBUS_ERROR_FAILED,
2654 "<limit name=\"%s\"> element has invalid value (could not parse as integer)",
2659 e->d.limit.value = val;
2661 _dbus_verbose ("Loaded value %ld for limit %s\n",
2668 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2672 BUS_SET_OOM (error);
2677 bus_config_parser_finished (BusConfigParser *parser,
2680 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2682 if (parser->stack != NULL)
2684 dbus_set_error (error, DBUS_ERROR_FAILED,
2685 "Element <%s> was not closed in configuration file",
2686 bus_config_parser_element_type_to_name (top_element_type (parser)));
2691 if (parser->is_toplevel && parser->listen_on == NULL)
2693 dbus_set_error (error, DBUS_ERROR_FAILED,
2694 "Configuration file needs one or more <listen> elements giving addresses");
2702 bus_config_parser_get_user (BusConfigParser *parser)
2704 return parser->user;
2708 bus_config_parser_get_type (BusConfigParser *parser)
2710 return parser->bus_type;
2714 bus_config_parser_get_addresses (BusConfigParser *parser)
2716 return &parser->listen_on;
2720 bus_config_parser_get_mechanisms (BusConfigParser *parser)
2722 return &parser->mechanisms;
2726 bus_config_parser_get_service_dirs (BusConfigParser *parser)
2728 return &parser->service_dirs;
2732 bus_config_parser_get_conf_dirs (BusConfigParser *parser)
2734 return &parser->conf_dirs;
2738 bus_config_parser_get_fork (BusConfigParser *parser)
2740 return parser->fork;
2744 bus_config_parser_get_syslog (BusConfigParser *parser)
2746 return parser->syslog;
2750 bus_config_parser_get_keep_umask (BusConfigParser *parser)
2752 return parser->keep_umask;
2756 bus_config_parser_get_allow_anonymous (BusConfigParser *parser)
2758 return parser->allow_anonymous;
2762 bus_config_parser_get_pidfile (BusConfigParser *parser)
2764 return parser->pidfile;
2768 bus_config_parser_get_servicehelper (BusConfigParser *parser)
2770 return parser->servicehelper;
2774 bus_config_parser_steal_policy (BusConfigParser *parser)
2778 _dbus_assert (parser->policy != NULL); /* can only steal the policy 1 time */
2780 policy = parser->policy;
2782 parser->policy = NULL;
2787 /* Overwrite any limits that were set in the configuration file */
2789 bus_config_parser_get_limits (BusConfigParser *parser,
2792 *limits = parser->limits;
2796 bus_config_parser_steal_service_context_table (BusConfigParser *parser)
2798 DBusHashTable *table;
2800 _dbus_assert (parser->service_context_table != NULL); /* can only steal once */
2802 table = parser->service_context_table;
2804 parser->service_context_table = NULL;
2809 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
2820 do_check_own_rules (BusPolicy *policy)
2824 dbus_bool_t allowed;
2826 {"org.freedesktop", FALSE},
2827 {"org.freedesktop.ManySystem", FALSE},
2828 {"org.freedesktop.ManySystems", TRUE},
2829 {"org.freedesktop.ManySystems.foo", TRUE},
2830 {"org.freedesktop.ManySystems.foo.bar", TRUE},
2831 {"org.freedesktop.ManySystems2", FALSE},
2832 {"org.freedesktop.ManySystems2.foo", FALSE},
2833 {"org.freedesktop.ManySystems2.foo.bar", FALSE},
2838 while (checks[i].name)
2840 DBusString service_name;
2843 if (!_dbus_string_init (&service_name))
2844 _dbus_assert_not_reached ("couldn't init string");
2845 if (!_dbus_string_append (&service_name, checks[i].name))
2846 _dbus_assert_not_reached ("couldn't append string");
2848 ret = bus_policy_check_can_own (policy, &service_name);
2849 printf (" Check name %s: %s\n", checks[i].name,
2850 ret ? "allowed" : "not allowed");
2851 if (checks[i].allowed && !ret)
2853 _dbus_warn ("Cannot own %s\n", checks[i].name);
2856 if (!checks[i].allowed && ret)
2858 _dbus_warn ("Can own %s\n", checks[i].name);
2861 _dbus_string_free (&service_name);
2870 do_load (const DBusString *full_path,
2872 dbus_bool_t oom_possible,
2873 dbus_bool_t check_own_rules)
2875 BusConfigParser *parser;
2878 dbus_error_init (&error);
2880 parser = bus_config_load (full_path, TRUE, NULL, &error);
2883 _DBUS_ASSERT_ERROR_IS_SET (&error);
2886 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2888 _dbus_verbose ("Failed to load valid file due to OOM\n");
2889 dbus_error_free (&error);
2892 else if (validity == VALID)
2894 _dbus_warn ("Failed to load valid file but still had memory: %s\n",
2897 dbus_error_free (&error);
2902 dbus_error_free (&error);
2908 _DBUS_ASSERT_ERROR_IS_CLEAR (&error);
2910 if (check_own_rules && do_check_own_rules (parser->policy) == FALSE)
2915 bus_config_parser_unref (parser);
2917 if (validity == INVALID)
2919 _dbus_warn ("Accepted invalid file\n");
2929 const DBusString *full_path;
2931 dbus_bool_t check_own_rules;
2935 check_loader_oom_func (void *data)
2937 LoaderOomData *d = data;
2939 return do_load (d->full_path, d->validity, TRUE, d->check_own_rules);
2943 process_test_valid_subdir (const DBusString *test_base_dir,
2947 DBusString test_directory;
2948 DBusString filename;
2956 if (!_dbus_string_init (&test_directory))
2957 _dbus_assert_not_reached ("didn't allocate test_directory\n");
2959 _dbus_string_init_const (&filename, subdir);
2961 if (!_dbus_string_copy (test_base_dir, 0,
2962 &test_directory, 0))
2963 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2965 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2966 _dbus_assert_not_reached ("couldn't allocate full path");
2968 _dbus_string_free (&filename);
2969 if (!_dbus_string_init (&filename))
2970 _dbus_assert_not_reached ("didn't allocate filename string\n");
2972 dbus_error_init (&error);
2973 dir = _dbus_directory_open (&test_directory, &error);
2976 _dbus_warn ("Could not open %s: %s\n",
2977 _dbus_string_get_const_data (&test_directory),
2979 dbus_error_free (&error);
2983 if (validity == VALID)
2984 printf ("Testing valid files:\n");
2985 else if (validity == INVALID)
2986 printf ("Testing invalid files:\n");
2988 printf ("Testing unknown files:\n");
2991 while (_dbus_directory_get_next_file (dir, &filename, &error))
2993 DBusString full_path;
2996 if (!_dbus_string_init (&full_path))
2997 _dbus_assert_not_reached ("couldn't init string");
2999 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
3000 _dbus_assert_not_reached ("couldn't copy dir to full_path");
3002 if (!_dbus_concat_dir_and_file (&full_path, &filename))
3003 _dbus_assert_not_reached ("couldn't concat file to dir");
3005 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
3007 _dbus_verbose ("Skipping non-.conf file %s\n",
3008 _dbus_string_get_const_data (&filename));
3009 _dbus_string_free (&full_path);
3013 printf (" %s\n", _dbus_string_get_const_data (&filename));
3015 _dbus_verbose (" expecting %s\n",
3016 validity == VALID ? "valid" :
3017 (validity == INVALID ? "invalid" :
3018 (validity == UNKNOWN ? "unknown" : "???")));
3020 d.full_path = &full_path;
3021 d.validity = validity;
3022 d.check_own_rules = _dbus_string_ends_with_c_str (&full_path,
3023 "check-own-rules.conf");
3025 /* FIXME hackaround for an expat problem, see
3026 * https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=124747
3027 * http://freedesktop.org/pipermail/dbus/2004-May/001153.html
3029 /* if (!_dbus_test_oom_handling ("config-loader", check_loader_oom_func, &d)) */
3030 if (!check_loader_oom_func (&d))
3031 _dbus_assert_not_reached ("test failed");
3033 _dbus_string_free (&full_path);
3036 if (dbus_error_is_set (&error))
3038 _dbus_warn ("Could not get next file in %s: %s\n",
3039 _dbus_string_get_const_data (&test_directory),
3041 dbus_error_free (&error);
3050 _dbus_directory_close (dir);
3051 _dbus_string_free (&test_directory);
3052 _dbus_string_free (&filename);
3058 bools_equal (dbus_bool_t a,
3065 strings_equal_or_both_null (const char *a,
3068 if (a == NULL || b == NULL)
3071 return !strcmp (a, b);
3075 elements_equal (const Element *a,
3078 if (a->type != b->type)
3081 if (!bools_equal (a->had_content, b->had_content))
3087 case ELEMENT_INCLUDE:
3088 if (!bools_equal (a->d.include.ignore_missing,
3089 b->d.include.ignore_missing))
3093 case ELEMENT_POLICY:
3094 if (a->d.policy.type != b->d.policy.type)
3096 if (a->d.policy.gid_uid_or_at_console != b->d.policy.gid_uid_or_at_console)
3101 if (strcmp (a->d.limit.name, b->d.limit.name))
3103 if (a->d.limit.value != b->d.limit.value)
3117 lists_of_elements_equal (DBusList *a,
3126 while (ia != NULL && ib != NULL)
3128 if (elements_equal (ia->data, ib->data))
3130 ia = _dbus_list_get_next_link (&a, ia);
3131 ib = _dbus_list_get_next_link (&b, ib);
3134 return ia == NULL && ib == NULL;
3138 lists_of_c_strings_equal (DBusList *a,
3147 while (ia != NULL && ib != NULL)
3149 if (strcmp (ia->data, ib->data))
3151 ia = _dbus_list_get_next_link (&a, ia);
3152 ib = _dbus_list_get_next_link (&b, ib);
3155 return ia == NULL && ib == NULL;
3159 limits_equal (const BusLimits *a,
3163 (a->max_incoming_bytes == b->max_incoming_bytes
3164 || a->max_incoming_unix_fds == b->max_incoming_unix_fds
3165 || a->max_outgoing_bytes == b->max_outgoing_bytes
3166 || a->max_outgoing_unix_fds == b->max_outgoing_unix_fds
3167 || a->max_message_size == b->max_message_size
3168 || a->max_message_unix_fds == b->max_message_unix_fds
3169 || a->activation_timeout == b->activation_timeout
3170 || a->auth_timeout == b->auth_timeout
3171 || a->pending_fd_timeout == b->pending_fd_timeout
3172 || a->max_completed_connections == b->max_completed_connections
3173 || a->max_incomplete_connections == b->max_incomplete_connections
3174 || a->max_connections_per_user == b->max_connections_per_user
3175 || a->max_pending_activations == b->max_pending_activations
3176 || a->max_services_per_connection == b->max_services_per_connection
3177 || a->max_match_rules_per_connection == b->max_match_rules_per_connection
3178 || a->max_replies_per_connection == b->max_replies_per_connection
3179 || a->reply_timeout == b->reply_timeout);
3183 config_parsers_equal (const BusConfigParser *a,
3184 const BusConfigParser *b)
3186 if (!_dbus_string_equal (&a->basedir, &b->basedir))
3189 if (!lists_of_elements_equal (a->stack, b->stack))
3192 if (!strings_equal_or_both_null (a->user, b->user))
3195 if (!lists_of_c_strings_equal (a->listen_on, b->listen_on))
3198 if (!lists_of_c_strings_equal (a->mechanisms, b->mechanisms))
3201 if (!lists_of_c_strings_equal (a->service_dirs, b->service_dirs))
3204 /* FIXME: compare policy */
3206 /* FIXME: compare service selinux ID table */
3208 if (! limits_equal (&a->limits, &b->limits))
3211 if (!strings_equal_or_both_null (a->pidfile, b->pidfile))
3214 if (! bools_equal (a->fork, b->fork))
3217 if (! bools_equal (a->keep_umask, b->keep_umask))
3220 if (! bools_equal (a->is_toplevel, b->is_toplevel))
3227 all_are_equiv (const DBusString *target_directory)
3229 DBusString filename;
3231 BusConfigParser *first_parser;
3232 BusConfigParser *parser;
3238 first_parser = NULL;
3242 if (!_dbus_string_init (&filename))
3243 _dbus_assert_not_reached ("didn't allocate filename string");
3245 dbus_error_init (&error);
3246 dir = _dbus_directory_open (target_directory, &error);
3249 _dbus_warn ("Could not open %s: %s\n",
3250 _dbus_string_get_const_data (target_directory),
3252 dbus_error_free (&error);
3256 printf ("Comparing equivalent files:\n");
3259 while (_dbus_directory_get_next_file (dir, &filename, &error))
3261 DBusString full_path;
3263 if (!_dbus_string_init (&full_path))
3264 _dbus_assert_not_reached ("couldn't init string");
3266 if (!_dbus_string_copy (target_directory, 0, &full_path, 0))
3267 _dbus_assert_not_reached ("couldn't copy dir to full_path");
3269 if (!_dbus_concat_dir_and_file (&full_path, &filename))
3270 _dbus_assert_not_reached ("couldn't concat file to dir");
3272 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
3274 _dbus_verbose ("Skipping non-.conf file %s\n",
3275 _dbus_string_get_const_data (&filename));
3276 _dbus_string_free (&full_path);
3280 printf (" %s\n", _dbus_string_get_const_data (&filename));
3282 parser = bus_config_load (&full_path, TRUE, NULL, &error);
3286 _dbus_warn ("Could not load file %s: %s\n",
3287 _dbus_string_get_const_data (&full_path),
3289 _dbus_string_free (&full_path);
3290 dbus_error_free (&error);
3293 else if (first_parser == NULL)
3295 _dbus_string_free (&full_path);
3296 first_parser = parser;
3300 _dbus_string_free (&full_path);
3301 equal = config_parsers_equal (first_parser, parser);
3302 bus_config_parser_unref (parser);
3311 _dbus_string_free (&filename);
3313 bus_config_parser_unref (first_parser);
3315 _dbus_directory_close (dir);
3322 process_test_equiv_subdir (const DBusString *test_base_dir,
3325 DBusString test_directory;
3326 DBusString filename;
3335 if (!_dbus_string_init (&test_directory))
3336 _dbus_assert_not_reached ("didn't allocate test_directory");
3338 _dbus_string_init_const (&filename, subdir);
3340 if (!_dbus_string_copy (test_base_dir, 0,
3341 &test_directory, 0))
3342 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
3344 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
3345 _dbus_assert_not_reached ("couldn't allocate full path");
3347 _dbus_string_free (&filename);
3348 if (!_dbus_string_init (&filename))
3349 _dbus_assert_not_reached ("didn't allocate filename string");
3351 dbus_error_init (&error);
3352 dir = _dbus_directory_open (&test_directory, &error);
3355 _dbus_warn ("Could not open %s: %s\n",
3356 _dbus_string_get_const_data (&test_directory),
3358 dbus_error_free (&error);
3362 while (_dbus_directory_get_next_file (dir, &filename, &error))
3364 DBusString full_path;
3366 /* Skip CVS's magic directories! */
3367 if (_dbus_string_equal_c_str (&filename, "CVS"))
3370 if (!_dbus_string_init (&full_path))
3371 _dbus_assert_not_reached ("couldn't init string");
3373 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
3374 _dbus_assert_not_reached ("couldn't copy dir to full_path");
3376 if (!_dbus_concat_dir_and_file (&full_path, &filename))
3377 _dbus_assert_not_reached ("couldn't concat file to dir");
3379 equal = all_are_equiv (&full_path);
3380 _dbus_string_free (&full_path);
3389 _dbus_string_free (&test_directory);
3390 _dbus_string_free (&filename);
3392 _dbus_directory_close (dir);
3398 static const char *test_session_service_dir_matches[] =
3401 "/testhome/foo/.testlocal/testshare/dbus-1/services",
3402 "/testusr/testlocal/testshare/dbus-1/services",
3403 "/testusr/testshare/dbus-1/services",
3404 DBUS_DATADIR"/dbus-1/services",
3406 /* will be filled in test_default_session_servicedirs() */
3415 test_default_session_servicedirs (void)
3420 DBusString install_root_based;
3422 dbus_bool_t ret = FALSE;
3425 const char *common_progs;
3428 /* On Unix we don't actually use these, but it's easier to handle the
3429 * deallocation if we always allocate them, whether needed or not */
3430 if (!_dbus_string_init (&progs) ||
3431 !_dbus_string_init (&install_root_based))
3432 _dbus_assert_not_reached ("OOM allocating strings");
3435 if (!_dbus_string_append (&install_root_based, DBUS_DATADIR) ||
3436 !_dbus_string_append (&install_root_based, "/dbus-1/services"))
3439 tmp = _dbus_replace_install_prefix (
3440 _dbus_string_get_const_data (&install_root_based));
3443 !_dbus_string_set_length (&install_root_based, 0) ||
3444 !_dbus_string_append (&install_root_based, tmp))
3447 test_session_service_dir_matches[0] = _dbus_string_get_const_data (
3448 &install_root_based);
3450 common_progs = _dbus_getenv ("CommonProgramFiles");
3454 if (!_dbus_string_append (&progs, common_progs))
3457 if (!_dbus_string_append (&progs, "/dbus-1/services"))
3460 test_session_service_dir_matches[1] = _dbus_string_get_const_data(&progs);
3465 printf ("Testing retrieving the default session service directories\n");
3466 if (!_dbus_get_standard_session_servicedirs (&dirs))
3467 _dbus_assert_not_reached ("couldn't get stardard dirs");
3469 /* make sure our defaults end with share/dbus-1/service */
3470 while ((link = _dbus_list_pop_first_link (&dirs)))
3474 printf (" default service dir: %s\n", (char *)link->data);
3475 _dbus_string_init_const (&path, (char *)link->data);
3476 if (!_dbus_string_ends_with_c_str (&path, "dbus-1/services"))
3478 printf ("error with default session service directories\n");
3479 dbus_free (link->data);
3480 _dbus_list_free_link (link);
3484 dbus_free (link->data);
3485 _dbus_list_free_link (link);
3489 if (!dbus_setenv ("XDG_DATA_HOME", "/testhome/foo/.testlocal/testshare"))
3490 _dbus_assert_not_reached ("couldn't setenv XDG_DATA_HOME");
3492 if (!dbus_setenv ("XDG_DATA_DIRS", ":/testusr/testlocal/testshare: :/testusr/testshare:"))
3493 _dbus_assert_not_reached ("couldn't setenv XDG_DATA_DIRS");
3495 if (!_dbus_get_standard_session_servicedirs (&dirs))
3496 _dbus_assert_not_reached ("couldn't get stardard dirs");
3498 /* make sure we read and parse the env variable correctly */
3500 while ((link = _dbus_list_pop_first_link (&dirs)))
3502 printf (" test service dir: %s\n", (char *)link->data);
3503 if (test_session_service_dir_matches[i] == NULL)
3505 printf ("more directories parsed than in match set\n");
3506 dbus_free (link->data);
3507 _dbus_list_free_link (link);
3511 if (strcmp (test_session_service_dir_matches[i],
3512 (char *)link->data) != 0)
3514 printf ("%s directory does not match %s in the match set\n",
3516 test_session_service_dir_matches[i]);
3517 dbus_free (link->data);
3518 _dbus_list_free_link (link);
3524 dbus_free (link->data);
3525 _dbus_list_free_link (link);
3528 if (test_session_service_dir_matches[i] != NULL)
3530 printf ("extra data %s in the match set was not matched\n",
3531 test_session_service_dir_matches[i]);
3538 _dbus_string_free (&install_root_based);
3539 _dbus_string_free (&progs);
3543 static const char *test_system_service_dir_matches[] =
3546 "/usr/local/share/dbus-1/system-services",
3547 "/usr/share/dbus-1/system-services",
3549 DBUS_DATADIR"/dbus-1/system-services",
3551 "/lib/dbus-1/system-services",
3561 test_default_system_servicedirs (void)
3567 const char *common_progs;
3571 /* On Unix we don't actually use this variable, but it's easier to handle the
3572 * deallocation if we always allocate it, whether needed or not */
3573 if (!_dbus_string_init (&progs))
3574 _dbus_assert_not_reached ("OOM allocating progs");
3577 common_progs = _dbus_getenv ("CommonProgramFiles");
3581 if (!_dbus_string_append (&progs, common_progs))
3583 _dbus_string_free (&progs);
3587 if (!_dbus_string_append (&progs, "/dbus-1/system-services"))
3589 _dbus_string_free (&progs);
3592 test_system_service_dir_matches[1] = _dbus_string_get_const_data(&progs);
3597 printf ("Testing retrieving the default system service directories\n");
3598 if (!_dbus_get_standard_system_servicedirs (&dirs))
3599 _dbus_assert_not_reached ("couldn't get stardard dirs");
3601 /* make sure our defaults end with share/dbus-1/system-service */
3602 while ((link = _dbus_list_pop_first_link (&dirs)))
3606 printf (" default service dir: %s\n", (char *)link->data);
3607 _dbus_string_init_const (&path, (char *)link->data);
3608 if (!_dbus_string_ends_with_c_str (&path, "dbus-1/system-services"))
3610 printf ("error with default system service directories\n");
3611 dbus_free (link->data);
3612 _dbus_list_free_link (link);
3613 _dbus_string_free (&progs);
3617 dbus_free (link->data);
3618 _dbus_list_free_link (link);
3622 if (!dbus_setenv ("XDG_DATA_HOME", "/testhome/foo/.testlocal/testshare"))
3623 _dbus_assert_not_reached ("couldn't setenv XDG_DATA_HOME");
3625 if (!dbus_setenv ("XDG_DATA_DIRS", ":/testusr/testlocal/testshare: :/testusr/testshare:"))
3626 _dbus_assert_not_reached ("couldn't setenv XDG_DATA_DIRS");
3628 if (!_dbus_get_standard_system_servicedirs (&dirs))
3629 _dbus_assert_not_reached ("couldn't get stardard dirs");
3631 /* make sure we read and parse the env variable correctly */
3633 while ((link = _dbus_list_pop_first_link (&dirs)))
3635 printf (" test service dir: %s\n", (char *)link->data);
3636 if (test_system_service_dir_matches[i] == NULL)
3638 printf ("more directories parsed than in match set\n");
3639 dbus_free (link->data);
3640 _dbus_list_free_link (link);
3641 _dbus_string_free (&progs);
3645 if (strcmp (test_system_service_dir_matches[i],
3646 (char *)link->data) != 0)
3648 printf ("%s directory does not match %s in the match set\n",
3650 test_system_service_dir_matches[i]);
3651 dbus_free (link->data);
3652 _dbus_list_free_link (link);
3653 _dbus_string_free (&progs);
3659 dbus_free (link->data);
3660 _dbus_list_free_link (link);
3663 if (test_system_service_dir_matches[i] != NULL)
3665 printf ("extra data %s in the match set was not matched\n",
3666 test_system_service_dir_matches[i]);
3668 _dbus_string_free (&progs);
3672 _dbus_string_free (&progs);
3677 bus_config_parser_test (const DBusString *test_data_dir)
3679 if (test_data_dir == NULL ||
3680 _dbus_string_get_length (test_data_dir) == 0)
3682 printf ("No test data\n");
3686 if (!test_default_session_servicedirs())
3690 printf("default system service dir skipped\n");
3692 if (!test_default_system_servicedirs())
3696 if (!process_test_valid_subdir (test_data_dir, "valid-config-files", VALID))
3700 if (!process_test_valid_subdir (test_data_dir, "valid-config-files-system", VALID))
3704 if (!process_test_valid_subdir (test_data_dir, "invalid-config-files", INVALID))
3707 if (!process_test_equiv_subdir (test_data_dir, "equiv-config-files"))
3713 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */