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 = 512; /* 256 -> 512 */
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,
1189 BusPolicyRuleAccess access,
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;
1212 const char *privilege;
1214 BusPolicyRule *rule;
1216 if (!locate_attributes (parser, element_name,
1220 "send_interface", &send_interface,
1221 "send_member", &send_member,
1222 "send_error", &send_error,
1223 "send_destination", &send_destination,
1224 "send_path", &send_path,
1225 "send_type", &send_type,
1226 "receive_interface", &receive_interface,
1227 "receive_member", &receive_member,
1228 "receive_error", &receive_error,
1229 "receive_sender", &receive_sender,
1230 "receive_path", &receive_path,
1231 "receive_type", &receive_type,
1232 "eavesdrop", &eavesdrop,
1233 "send_requested_reply", &send_requested_reply,
1234 "receive_requested_reply", &receive_requested_reply,
1236 "own_prefix", &own_prefix,
1240 "privilege", &privilege,
1244 if (!(send_interface || send_member || send_error || send_destination ||
1245 send_type || send_path ||
1246 receive_interface || receive_member || receive_error || receive_sender ||
1247 receive_type || receive_path || eavesdrop ||
1248 send_requested_reply || receive_requested_reply ||
1250 own || own_prefix || user || group))
1252 dbus_set_error (error, DBUS_ERROR_FAILED,
1253 "Element <%s> must have one or more attributes",
1258 if ((send_member && (send_interface == NULL && send_path == NULL)) ||
1259 (receive_member && (receive_interface == NULL && receive_path == NULL)))
1261 dbus_set_error (error, DBUS_ERROR_FAILED,
1262 "On element <%s>, if you specify a member you must specify an interface or a path. Keep in mind that not all messages have an interface field.",
1267 if (access == BUS_POLICY_RULE_ACCESS_CHECK)
1269 if (privilege == NULL || !*privilege)
1271 dbus_set_error (error, DBUS_ERROR_FAILED,
1272 "On element <%s>, you must specify the privilege to be checked.",
1279 if (privilege != NULL && *privilege)
1281 dbus_set_error (error, DBUS_ERROR_FAILED,
1282 "On element <%s>, privilege %s is used outside of a check rule.",
1283 element_name, privilege);
1287 privilege = NULL; /* replace (potentially) empty string with NULL pointer, it wouldn't be used anyway */
1290 /* Allowed combinations of elements are:
1292 * base, must be all send or all receive:
1295 * interface + member
1298 * base send_ can combine with send_destination, send_path, send_type, send_requested_reply
1299 * base receive_ with receive_sender, receive_path, receive_type, receive_requested_reply, eavesdrop
1301 * user, group, own, own_prefix must occur alone
1303 * Pretty sure the below stuff is broken, FIXME think about it more.
1306 if ((send_interface && (send_error ||
1307 receive_interface ||
1311 receive_requested_reply ||
1312 own || own_prefix ||
1316 (send_member && (send_error ||
1317 receive_interface ||
1321 receive_requested_reply ||
1322 own || own_prefix ||
1326 (send_error && (receive_interface ||
1330 receive_requested_reply ||
1331 own || own_prefix ||
1335 (send_destination && (receive_interface ||
1339 receive_requested_reply ||
1340 own || own_prefix ||
1344 (send_type && (receive_interface ||
1348 receive_requested_reply ||
1349 own || own_prefix ||
1353 (send_path && (receive_interface ||
1357 receive_requested_reply ||
1358 own || own_prefix ||
1362 (send_requested_reply && (receive_interface ||
1366 receive_requested_reply ||
1367 own || own_prefix ||
1371 (receive_interface && (receive_error ||
1372 own || own_prefix ||
1376 (receive_member && (receive_error ||
1377 own || own_prefix ||
1381 (receive_error && (own || own_prefix ||
1385 (eavesdrop && (own || own_prefix ||
1389 (receive_requested_reply && (own || own_prefix ||
1393 (own && (own_prefix || user || group)) ||
1395 (own_prefix && (own || user || group)) ||
1399 dbus_set_error (error, DBUS_ERROR_FAILED,
1400 "Invalid combination of attributes on element <%s>",
1407 /* In BusPolicyRule, NULL represents wildcard.
1408 * In the config file, '*' represents it.
1410 #define IS_WILDCARD(str) ((str) && ((str)[0]) == '*' && ((str)[1]) == '\0')
1412 if (send_interface || send_member || send_error || send_destination ||
1413 send_path || send_type || send_requested_reply)
1417 if (IS_WILDCARD (send_interface))
1418 send_interface = NULL;
1419 if (IS_WILDCARD (send_member))
1421 if (IS_WILDCARD (send_error))
1423 if (IS_WILDCARD (send_destination))
1424 send_destination = NULL;
1425 if (IS_WILDCARD (send_path))
1427 if (IS_WILDCARD (send_type))
1430 message_type = DBUS_MESSAGE_TYPE_INVALID;
1431 if (send_type != NULL)
1433 message_type = dbus_message_type_from_string (send_type);
1434 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1436 dbus_set_error (error, DBUS_ERROR_FAILED,
1437 "Bad message type \"%s\"",
1444 !(strcmp (eavesdrop, "true") == 0 ||
1445 strcmp (eavesdrop, "false") == 0))
1447 dbus_set_error (error, DBUS_ERROR_FAILED,
1448 "Bad value \"%s\" for %s attribute, must be true or false",
1449 "eavesdrop", eavesdrop);
1453 if (send_requested_reply &&
1454 !(strcmp (send_requested_reply, "true") == 0 ||
1455 strcmp (send_requested_reply, "false") == 0))
1457 dbus_set_error (error, DBUS_ERROR_FAILED,
1458 "Bad value \"%s\" for %s attribute, must be true or false",
1459 "send_requested_reply", send_requested_reply);
1463 rule = bus_policy_rule_new (BUS_POLICY_RULE_SEND, access);
1468 rule->d.send.eavesdrop = (strcmp (eavesdrop, "true") == 0);
1471 rule->d.send.log = (strcmp (log, "true") == 0);
1473 if (send_requested_reply)
1474 rule->d.send.requested_reply = (strcmp (send_requested_reply, "true") == 0);
1476 rule->d.send.message_type = message_type;
1477 rule->d.send.path = _dbus_strdup (send_path);
1478 rule->d.send.interface = _dbus_strdup (send_interface);
1479 rule->d.send.member = _dbus_strdup (send_member);
1480 rule->d.send.error = _dbus_strdup (send_error);
1481 rule->d.send.destination = _dbus_strdup (send_destination);
1482 if (send_path && rule->d.send.path == NULL)
1484 if (send_interface && rule->d.send.interface == NULL)
1486 if (send_member && rule->d.send.member == NULL)
1488 if (send_error && rule->d.send.error == NULL)
1490 if (send_destination && rule->d.send.destination == NULL)
1493 else if (receive_interface || receive_member || receive_error || receive_sender ||
1494 receive_path || receive_type || eavesdrop || receive_requested_reply)
1498 if (IS_WILDCARD (receive_interface))
1499 receive_interface = NULL;
1500 if (IS_WILDCARD (receive_member))
1501 receive_member = NULL;
1502 if (IS_WILDCARD (receive_error))
1503 receive_error = NULL;
1504 if (IS_WILDCARD (receive_sender))
1505 receive_sender = NULL;
1506 if (IS_WILDCARD (receive_path))
1507 receive_path = NULL;
1508 if (IS_WILDCARD (receive_type))
1509 receive_type = NULL;
1511 message_type = DBUS_MESSAGE_TYPE_INVALID;
1512 if (receive_type != NULL)
1514 message_type = dbus_message_type_from_string (receive_type);
1515 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1517 dbus_set_error (error, DBUS_ERROR_FAILED,
1518 "Bad message type \"%s\"",
1526 !(strcmp (eavesdrop, "true") == 0 ||
1527 strcmp (eavesdrop, "false") == 0))
1529 dbus_set_error (error, DBUS_ERROR_FAILED,
1530 "Bad value \"%s\" for %s attribute, must be true or false",
1531 "eavesdrop", eavesdrop);
1535 if (receive_requested_reply &&
1536 !(strcmp (receive_requested_reply, "true") == 0 ||
1537 strcmp (receive_requested_reply, "false") == 0))
1539 dbus_set_error (error, DBUS_ERROR_FAILED,
1540 "Bad value \"%s\" for %s attribute, must be true or false",
1541 "receive_requested_reply", receive_requested_reply);
1545 rule = bus_policy_rule_new (BUS_POLICY_RULE_RECEIVE, access);
1550 rule->d.receive.eavesdrop = (strcmp (eavesdrop, "true") == 0);
1552 if (receive_requested_reply)
1553 rule->d.receive.requested_reply = (strcmp (receive_requested_reply, "true") == 0);
1555 rule->d.receive.message_type = message_type;
1556 rule->d.receive.path = _dbus_strdup (receive_path);
1557 rule->d.receive.interface = _dbus_strdup (receive_interface);
1558 rule->d.receive.member = _dbus_strdup (receive_member);
1559 rule->d.receive.error = _dbus_strdup (receive_error);
1560 rule->d.receive.origin = _dbus_strdup (receive_sender);
1562 if (receive_path && rule->d.receive.path == NULL)
1564 if (receive_interface && rule->d.receive.interface == NULL)
1566 if (receive_member && rule->d.receive.member == NULL)
1568 if (receive_error && rule->d.receive.error == NULL)
1570 if (receive_sender && rule->d.receive.origin == NULL)
1573 else if (own || own_prefix)
1575 rule = bus_policy_rule_new (BUS_POLICY_RULE_OWN, access);
1581 if (IS_WILDCARD (own))
1584 rule->d.own.prefix = 0;
1585 rule->d.own.service_name = _dbus_strdup (own);
1586 if (own && rule->d.own.service_name == NULL)
1591 rule->d.own.prefix = 1;
1592 rule->d.own.service_name = _dbus_strdup (own_prefix);
1593 if (rule->d.own.service_name == NULL)
1599 if (IS_WILDCARD (user))
1601 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, access);
1605 rule->d.user.uid = DBUS_UID_UNSET;
1609 DBusString username;
1612 _dbus_string_init_const (&username, user);
1614 if (_dbus_parse_unix_user_from_config (&username, &uid))
1616 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, access);
1620 rule->d.user.uid = uid;
1624 _dbus_warn ("Unknown username \"%s\" on element <%s>\n",
1625 user, element_name);
1631 if (IS_WILDCARD (group))
1633 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, access);
1637 rule->d.group.gid = DBUS_GID_UNSET;
1641 DBusString groupname;
1644 _dbus_string_init_const (&groupname, group);
1646 if (_dbus_parse_unix_group_from_config (&groupname, &gid))
1648 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, access);
1652 rule->d.group.gid = gid;
1656 _dbus_warn ("Unknown group \"%s\" on element <%s>\n",
1657 group, element_name);
1662 _dbus_assert_not_reached ("Did not handle some combination of attributes on <allow> or <deny>");
1668 pe = peek_element (parser);
1669 _dbus_assert (pe != NULL);
1670 _dbus_assert (pe->type == ELEMENT_POLICY);
1672 rule->privilege = _dbus_strdup (privilege);
1673 if (privilege && !rule->privilege)
1676 switch (pe->d.policy.type)
1678 case POLICY_IGNORED:
1679 /* drop the rule on the floor */
1682 case POLICY_DEFAULT:
1683 if (!bus_policy_append_default_rule (parser->policy, rule))
1686 case POLICY_MANDATORY:
1687 if (!bus_policy_append_mandatory_rule (parser->policy, rule))
1691 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1693 dbus_set_error (error, DBUS_ERROR_FAILED,
1694 "<%s> rule cannot be per-user because it has bus-global semantics",
1699 if (!bus_policy_append_user_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1704 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1706 dbus_set_error (error, DBUS_ERROR_FAILED,
1707 "<%s> rule cannot be per-group because it has bus-global semantics",
1712 if (!bus_policy_append_group_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1717 if (!bus_policy_append_smack_rule (parser->policy, pe->d.policy.smack_label, rule))
1720 case POLICY_CONSOLE:
1721 if (!bus_policy_append_console_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1727 bus_policy_rule_unref (rule);
1734 BUS_SET_OOM (error);
1737 bus_policy_rule_unref (rule);
1742 start_policy_child (BusConfigParser *parser,
1743 const char *element_name,
1744 const char **attribute_names,
1745 const char **attribute_values,
1748 if (strcmp (element_name, "allow") == 0)
1750 if (!append_rule_from_element (parser, element_name,
1751 attribute_names, attribute_values,
1752 BUS_POLICY_RULE_ACCESS_ALLOW, error))
1755 if (push_element (parser, ELEMENT_ALLOW) == NULL)
1757 BUS_SET_OOM (error);
1763 else if (strcmp (element_name, "deny") == 0)
1765 if (!append_rule_from_element (parser, element_name,
1766 attribute_names, attribute_values,
1767 BUS_POLICY_RULE_ACCESS_DENY, error))
1770 if (push_element (parser, ELEMENT_DENY) == NULL)
1772 BUS_SET_OOM (error);
1778 else if (strcmp (element_name, "check") == 0)
1780 if (!append_rule_from_element (parser, element_name,
1781 attribute_names, attribute_values,
1782 BUS_POLICY_RULE_ACCESS_CHECK, error))
1785 if (push_element (parser, ELEMENT_CHECK) == NULL)
1787 BUS_SET_OOM (error);
1795 dbus_set_error (error, DBUS_ERROR_FAILED,
1796 "Element <%s> not allowed inside <%s> in configuration file",
1797 element_name, "policy");
1803 start_selinux_child (BusConfigParser *parser,
1804 const char *element_name,
1805 const char **attribute_names,
1806 const char **attribute_values,
1813 context_copy = NULL;
1815 if (strcmp (element_name, "associate") == 0)
1818 const char *context;
1820 if (!locate_attributes (parser, "associate",
1825 "context", &context,
1829 if (push_element (parser, ELEMENT_ASSOCIATE) == NULL)
1831 BUS_SET_OOM (error);
1835 if (own == NULL || context == NULL)
1837 dbus_set_error (error, DBUS_ERROR_FAILED,
1838 "Element <associate> must have attributes own=\"<servicename>\" and context=\"<selinux context>\"");
1842 own_copy = _dbus_strdup (own);
1843 if (own_copy == NULL)
1845 context_copy = _dbus_strdup (context);
1846 if (context_copy == NULL)
1849 if (!_dbus_hash_table_insert_string (parser->service_context_table,
1850 own_copy, context_copy))
1857 dbus_set_error (error, DBUS_ERROR_FAILED,
1858 "Element <%s> not allowed inside <%s> in configuration file",
1859 element_name, "selinux");
1865 dbus_free (own_copy);
1868 dbus_free (context_copy);
1870 BUS_SET_OOM (error);
1875 bus_config_parser_start_element (BusConfigParser *parser,
1876 const char *element_name,
1877 const char **attribute_names,
1878 const char **attribute_values,
1883 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1885 /* printf ("START: %s\n", element_name); */
1887 t = top_element_type (parser);
1889 if (t == ELEMENT_NONE)
1891 if (strcmp (element_name, "busconfig") == 0)
1893 if (!check_no_attributes (parser, "busconfig", attribute_names, attribute_values, error))
1896 if (push_element (parser, ELEMENT_BUSCONFIG) == NULL)
1898 BUS_SET_OOM (error);
1906 dbus_set_error (error, DBUS_ERROR_FAILED,
1907 "Unknown element <%s> at root of configuration file",
1912 else if (t == ELEMENT_BUSCONFIG)
1914 return start_busconfig_child (parser, element_name,
1915 attribute_names, attribute_values,
1918 else if (t == ELEMENT_POLICY)
1920 return start_policy_child (parser, element_name,
1921 attribute_names, attribute_values,
1924 else if (t == ELEMENT_SELINUX)
1926 return start_selinux_child (parser, element_name,
1927 attribute_names, attribute_values,
1932 dbus_set_error (error, DBUS_ERROR_FAILED,
1933 "Element <%s> is not allowed in this context",
1940 set_limit (BusConfigParser *parser,
1945 dbus_bool_t must_be_positive;
1946 dbus_bool_t must_be_int;
1948 must_be_int = FALSE;
1949 must_be_positive = FALSE;
1951 if (strcmp (name, "max_incoming_bytes") == 0)
1953 must_be_positive = TRUE;
1954 parser->limits.max_incoming_bytes = value;
1956 else if (strcmp (name, "max_incoming_unix_fds") == 0)
1958 must_be_positive = TRUE;
1959 parser->limits.max_incoming_unix_fds = value;
1961 else if (strcmp (name, "max_outgoing_bytes") == 0)
1963 must_be_positive = TRUE;
1964 parser->limits.max_outgoing_bytes = value;
1966 else if (strcmp (name, "max_outgoing_unix_fds") == 0)
1968 must_be_positive = TRUE;
1969 parser->limits.max_outgoing_unix_fds = value;
1971 else if (strcmp (name, "max_message_size") == 0)
1973 must_be_positive = TRUE;
1974 parser->limits.max_message_size = value;
1976 else if (strcmp (name, "max_message_unix_fds") == 0)
1978 must_be_positive = TRUE;
1979 parser->limits.max_message_unix_fds = value;
1981 else if (strcmp (name, "service_start_timeout") == 0)
1983 must_be_positive = TRUE;
1985 parser->limits.activation_timeout = value;
1987 else if (strcmp (name, "auth_timeout") == 0)
1989 must_be_positive = TRUE;
1991 parser->limits.auth_timeout = value;
1993 else if (strcmp (name, "pending_fd_timeout") == 0)
1995 must_be_positive = TRUE;
1997 parser->limits.pending_fd_timeout = value;
1999 else if (strcmp (name, "reply_timeout") == 0)
2001 must_be_positive = TRUE;
2003 parser->limits.reply_timeout = value;
2005 else if (strcmp (name, "max_completed_connections") == 0)
2007 must_be_positive = TRUE;
2009 parser->limits.max_completed_connections = value;
2011 else if (strcmp (name, "max_incomplete_connections") == 0)
2013 must_be_positive = TRUE;
2015 parser->limits.max_incomplete_connections = value;
2017 else if (strcmp (name, "max_connections_per_user") == 0)
2019 must_be_positive = TRUE;
2021 parser->limits.max_connections_per_user = value;
2023 else if (strcmp (name, "max_pending_service_starts") == 0)
2025 must_be_positive = TRUE;
2027 parser->limits.max_pending_activations = value;
2029 else if (strcmp (name, "max_names_per_connection") == 0)
2031 must_be_positive = TRUE;
2033 parser->limits.max_services_per_connection = value;
2035 else if (strcmp (name, "max_match_rules_per_connection") == 0)
2037 must_be_positive = TRUE;
2039 parser->limits.max_match_rules_per_connection = value;
2041 else if (strcmp (name, "max_replies_per_connection") == 0)
2043 must_be_positive = TRUE;
2045 parser->limits.max_replies_per_connection = value;
2049 dbus_set_error (error, DBUS_ERROR_FAILED,
2050 "There is no limit called \"%s\"\n",
2055 if (must_be_positive && value < 0)
2057 dbus_set_error (error, DBUS_ERROR_FAILED,
2058 "<limit name=\"%s\"> must be a positive number\n",
2064 (value < _DBUS_INT_MIN || value > _DBUS_INT_MAX))
2066 dbus_set_error (error, DBUS_ERROR_FAILED,
2067 "<limit name=\"%s\"> value is too large\n",
2076 bus_config_parser_end_element (BusConfigParser *parser,
2077 const char *element_name,
2084 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2086 /* printf ("END: %s\n", element_name); */
2088 t = top_element_type (parser);
2090 if (t == ELEMENT_NONE)
2092 /* should probably be an assertion failure but
2093 * being paranoid about XML parsers
2095 dbus_set_error (error, DBUS_ERROR_FAILED,
2096 "XML parser ended element with no element on the stack");
2100 n = bus_config_parser_element_type_to_name (t);
2101 _dbus_assert (n != NULL);
2102 if (strcmp (n, element_name) != 0)
2104 /* should probably be an assertion failure but
2105 * being paranoid about XML parsers
2107 dbus_set_error (error, DBUS_ERROR_FAILED,
2108 "XML element <%s> ended but topmost element on the stack was <%s>",
2113 e = peek_element (parser);
2114 _dbus_assert (e != NULL);
2119 _dbus_assert_not_reached ("element in stack has no type");
2122 case ELEMENT_INCLUDE:
2124 case ELEMENT_CONFIGTYPE:
2125 case ELEMENT_LISTEN:
2126 case ELEMENT_PIDFILE:
2128 case ELEMENT_SERVICEDIR:
2129 case ELEMENT_SERVICEHELPER:
2130 case ELEMENT_INCLUDEDIR:
2132 if (!e->had_content)
2134 dbus_set_error (error, DBUS_ERROR_FAILED,
2135 "XML element <%s> was expected to have content inside it",
2136 bus_config_parser_element_type_to_name (e->type));
2140 if (e->type == ELEMENT_LIMIT)
2142 if (!set_limit (parser, e->d.limit.name, e->d.limit.value,
2148 case ELEMENT_BUSCONFIG:
2149 case ELEMENT_POLICY:
2154 case ELEMENT_SYSLOG:
2155 case ELEMENT_KEEP_UMASK:
2156 case ELEMENT_SELINUX:
2157 case ELEMENT_ASSOCIATE:
2158 case ELEMENT_STANDARD_SESSION_SERVICEDIRS:
2159 case ELEMENT_STANDARD_SYSTEM_SERVICEDIRS:
2160 case ELEMENT_ALLOW_ANONYMOUS:
2161 case ELEMENT_APPARMOR:
2165 pop_element (parser);
2171 all_whitespace (const DBusString *str)
2175 _dbus_string_skip_white (str, 0, &i);
2177 return i == _dbus_string_get_length (str);
2181 make_full_path (const DBusString *basedir,
2182 const DBusString *filename,
2183 DBusString *full_path)
2185 if (_dbus_path_is_absolute (filename))
2187 return _dbus_string_copy (filename, 0, full_path, 0);
2191 if (!_dbus_string_copy (basedir, 0, full_path, 0))
2194 if (!_dbus_concat_dir_and_file (full_path, filename))
2202 include_file (BusConfigParser *parser,
2203 const DBusString *filename,
2204 dbus_bool_t ignore_missing,
2207 /* FIXME good test case for this would load each config file in the
2208 * test suite both alone, and as an include, and check
2209 * that the result is the same
2211 BusConfigParser *included;
2212 const char *filename_str;
2213 DBusError tmp_error;
2215 dbus_error_init (&tmp_error);
2217 filename_str = _dbus_string_get_const_data (filename);
2219 /* Check to make sure this file hasn't already been included. */
2220 if (seen_include (parser, filename))
2222 dbus_set_error (error, DBUS_ERROR_FAILED,
2223 "Circular inclusion of file '%s'",
2228 if (! _dbus_list_append (&parser->included_files, (void *) filename_str))
2230 BUS_SET_OOM (error);
2234 /* Since parser is passed in as the parent, included
2235 inherits parser's limits. */
2236 included = bus_config_load (filename, FALSE, parser, &tmp_error);
2238 _dbus_list_pop_last (&parser->included_files);
2240 if (included == NULL)
2242 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
2244 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_FILE_NOT_FOUND) &&
2247 dbus_error_free (&tmp_error);
2252 dbus_move_error (&tmp_error, error);
2258 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
2260 if (!merge_included (parser, included, error))
2262 bus_config_parser_unref (included);
2266 /* Copy included's limits back to parser. */
2267 parser->limits = included->limits;
2269 bus_config_parser_unref (included);
2275 servicehelper_path (BusConfigParser *parser,
2276 const DBusString *filename,
2279 const char *filename_str;
2280 char *servicehelper;
2282 filename_str = _dbus_string_get_const_data (filename);
2284 /* copy to avoid overwriting with NULL on OOM */
2285 servicehelper = _dbus_strdup (filename_str);
2288 if (servicehelper == NULL)
2290 BUS_SET_OOM (error);
2294 /* save the latest servicehelper only if not OOM */
2295 dbus_free (parser->servicehelper);
2296 parser->servicehelper = servicehelper;
2298 /* We don't check whether the helper exists; instead we
2299 * would just fail to ever activate anything if it doesn't.
2300 * This allows an admin to fix the problem if it doesn't exist.
2301 * It also allows the parser test suite to successfully parse
2302 * test cases without installing the helper. ;-)
2309 include_dir (BusConfigParser *parser,
2310 const DBusString *dirname,
2313 DBusString filename;
2315 DBusError tmp_error;
2319 if (!_dbus_string_init (&filename))
2321 BUS_SET_OOM (error);
2327 dir = _dbus_directory_open (dirname, error);
2331 if (dbus_error_has_name (error, DBUS_ERROR_FILE_NOT_FOUND))
2333 dbus_error_free (error);
2340 dbus_error_init (&tmp_error);
2341 while (_dbus_directory_get_next_file (dir, &filename, &tmp_error))
2343 DBusString full_path;
2345 if (!_dbus_string_init (&full_path))
2347 BUS_SET_OOM (error);
2351 if (!_dbus_string_copy (dirname, 0, &full_path, 0))
2353 BUS_SET_OOM (error);
2354 _dbus_string_free (&full_path);
2358 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2360 BUS_SET_OOM (error);
2361 _dbus_string_free (&full_path);
2365 if (_dbus_string_ends_with_c_str (&full_path, ".conf"))
2367 if (!include_file (parser, &full_path, TRUE, error))
2369 if (dbus_error_is_set (error))
2371 /* We log to syslog unconditionally here, because this is
2372 * the configuration parser, so we don't yet know whether
2373 * this bus is going to want to write to syslog! (There's
2374 * also some layer inversion going on, if we want to use
2375 * the bus context.) */
2376 _dbus_system_log (DBUS_SYSTEM_LOG_INFO,
2377 "Encountered error '%s' while parsing '%s'\n",
2379 _dbus_string_get_const_data (&full_path));
2380 dbus_error_free (error);
2385 _dbus_string_free (&full_path);
2388 if (dbus_error_is_set (&tmp_error))
2390 dbus_move_error (&tmp_error, error);
2395 if (!_dbus_string_copy_data (dirname, &s))
2397 BUS_SET_OOM (error);
2401 if (!_dbus_list_append (&parser->conf_dirs, s))
2404 BUS_SET_OOM (error);
2412 _dbus_string_free (&filename);
2415 _dbus_directory_close (dir);
2421 bus_config_parser_content (BusConfigParser *parser,
2422 const DBusString *content,
2427 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2433 _dbus_string_get_const_data (content, &c_str);
2435 printf ("CONTENT %d bytes: %s\n", _dbus_string_get_length (content), c_str);
2439 e = peek_element (parser);
2442 dbus_set_error (error, DBUS_ERROR_FAILED,
2443 "Text content outside of any XML element in configuration file");
2446 else if (e->had_content)
2448 _dbus_assert_not_reached ("Element had multiple content blocks");
2452 switch (top_element_type (parser))
2455 _dbus_assert_not_reached ("element at top of stack has no type");
2458 case ELEMENT_BUSCONFIG:
2459 case ELEMENT_POLICY:
2464 case ELEMENT_SYSLOG:
2465 case ELEMENT_KEEP_UMASK:
2466 case ELEMENT_STANDARD_SESSION_SERVICEDIRS:
2467 case ELEMENT_STANDARD_SYSTEM_SERVICEDIRS:
2468 case ELEMENT_ALLOW_ANONYMOUS:
2469 case ELEMENT_SELINUX:
2470 case ELEMENT_ASSOCIATE:
2471 case ELEMENT_APPARMOR:
2472 if (all_whitespace (content))
2476 dbus_set_error (error, DBUS_ERROR_FAILED,
2477 "No text content expected inside XML element %s in configuration file",
2478 bus_config_parser_element_type_to_name (top_element_type (parser)));
2482 case ELEMENT_PIDFILE:
2486 e->had_content = TRUE;
2488 if (!_dbus_string_copy_data (content, &s))
2491 dbus_free (parser->pidfile);
2492 parser->pidfile = s;
2496 case ELEMENT_INCLUDE:
2498 DBusString full_path, selinux_policy_root;
2500 e->had_content = TRUE;
2502 if (e->d.include.if_selinux_enabled
2503 && !bus_selinux_enabled ())
2506 if (!_dbus_string_init (&full_path))
2509 if (e->d.include.selinux_root_relative)
2511 if (!bus_selinux_get_policy_root ())
2513 dbus_set_error (error, DBUS_ERROR_FAILED,
2514 "Could not determine SELinux policy root for relative inclusion");
2515 _dbus_string_free (&full_path);
2518 _dbus_string_init_const (&selinux_policy_root,
2519 bus_selinux_get_policy_root ());
2520 if (!make_full_path (&selinux_policy_root, content, &full_path))
2522 _dbus_string_free (&full_path);
2526 else if (!make_full_path (&parser->basedir, content, &full_path))
2528 _dbus_string_free (&full_path);
2532 if (!include_file (parser, &full_path,
2533 e->d.include.ignore_missing, error))
2535 _dbus_string_free (&full_path);
2539 _dbus_string_free (&full_path);
2543 case ELEMENT_SERVICEHELPER:
2545 DBusString full_path;
2547 e->had_content = TRUE;
2549 if (!_dbus_string_init (&full_path))
2552 if (!make_full_path (&parser->basedir, content, &full_path))
2554 _dbus_string_free (&full_path);
2558 if (!servicehelper_path (parser, &full_path, error))
2560 _dbus_string_free (&full_path);
2564 _dbus_string_free (&full_path);
2568 case ELEMENT_INCLUDEDIR:
2570 DBusString full_path;
2572 e->had_content = TRUE;
2574 if (!_dbus_string_init (&full_path))
2577 if (!make_full_path (&parser->basedir, content, &full_path))
2579 _dbus_string_free (&full_path);
2583 if (!include_dir (parser, &full_path, error))
2585 _dbus_string_free (&full_path);
2589 _dbus_string_free (&full_path);
2597 e->had_content = TRUE;
2599 if (!_dbus_string_copy_data (content, &s))
2602 dbus_free (parser->user);
2607 case ELEMENT_CONFIGTYPE:
2611 e->had_content = TRUE;
2613 if (!_dbus_string_copy_data (content, &s))
2616 dbus_free (parser->bus_type);
2617 parser->bus_type = s;
2621 case ELEMENT_LISTEN:
2625 e->had_content = TRUE;
2627 if (!_dbus_string_copy_data (content, &s))
2630 if (!_dbus_list_append (&parser->listen_on,
2643 e->had_content = TRUE;
2645 if (!_dbus_string_copy_data (content, &s))
2648 if (!_dbus_list_append (&parser->mechanisms,
2657 case ELEMENT_SERVICEDIR:
2660 DBusString full_path;
2662 e->had_content = TRUE;
2664 if (!_dbus_string_init (&full_path))
2667 if (!make_full_path (&parser->basedir, content, &full_path))
2669 _dbus_string_free (&full_path);
2673 if (!_dbus_string_copy_data (&full_path, &s))
2675 _dbus_string_free (&full_path);
2679 /* _only_ extra session directories can be specified */
2680 if (!service_dirs_append_unique_or_free (&parser->service_dirs, s))
2682 _dbus_string_free (&full_path);
2687 _dbus_string_free (&full_path);
2695 e->had_content = TRUE;
2698 if (!_dbus_string_parse_int (content, 0, &val, NULL))
2700 dbus_set_error (error, DBUS_ERROR_FAILED,
2701 "<limit name=\"%s\"> element has invalid value (could not parse as integer)",
2706 e->d.limit.value = val;
2708 _dbus_verbose ("Loaded value %ld for limit %s\n",
2715 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2719 BUS_SET_OOM (error);
2724 bus_config_parser_finished (BusConfigParser *parser,
2727 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2729 if (parser->stack != NULL)
2731 dbus_set_error (error, DBUS_ERROR_FAILED,
2732 "Element <%s> was not closed in configuration file",
2733 bus_config_parser_element_type_to_name (top_element_type (parser)));
2738 if (parser->is_toplevel && parser->listen_on == NULL)
2740 dbus_set_error (error, DBUS_ERROR_FAILED,
2741 "Configuration file needs one or more <listen> elements giving addresses");
2749 bus_config_parser_get_user (BusConfigParser *parser)
2751 return parser->user;
2755 bus_config_parser_get_type (BusConfigParser *parser)
2757 return parser->bus_type;
2761 bus_config_parser_get_addresses (BusConfigParser *parser)
2763 return &parser->listen_on;
2767 bus_config_parser_get_mechanisms (BusConfigParser *parser)
2769 return &parser->mechanisms;
2773 bus_config_parser_get_service_dirs (BusConfigParser *parser)
2775 return &parser->service_dirs;
2779 bus_config_parser_get_conf_dirs (BusConfigParser *parser)
2781 return &parser->conf_dirs;
2785 bus_config_parser_get_fork (BusConfigParser *parser)
2787 return parser->fork;
2791 bus_config_parser_get_syslog (BusConfigParser *parser)
2793 return parser->syslog;
2797 bus_config_parser_get_keep_umask (BusConfigParser *parser)
2799 return parser->keep_umask;
2803 bus_config_parser_get_allow_anonymous (BusConfigParser *parser)
2805 return parser->allow_anonymous;
2809 bus_config_parser_get_pidfile (BusConfigParser *parser)
2811 return parser->pidfile;
2815 bus_config_parser_get_servicehelper (BusConfigParser *parser)
2817 return parser->servicehelper;
2821 bus_config_parser_steal_policy (BusConfigParser *parser)
2825 _dbus_assert (parser->policy != NULL); /* can only steal the policy 1 time */
2827 policy = parser->policy;
2829 parser->policy = NULL;
2834 /* Overwrite any limits that were set in the configuration file */
2836 bus_config_parser_get_limits (BusConfigParser *parser,
2839 *limits = parser->limits;
2843 bus_config_parser_steal_service_context_table (BusConfigParser *parser)
2845 DBusHashTable *table;
2847 _dbus_assert (parser->service_context_table != NULL); /* can only steal once */
2849 table = parser->service_context_table;
2851 parser->service_context_table = NULL;
2856 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
2867 do_check_own_rules (BusPolicy *policy)
2871 dbus_bool_t allowed;
2873 {"org.freedesktop", FALSE},
2874 {"org.freedesktop.ManySystem", FALSE},
2875 {"org.freedesktop.ManySystems", TRUE},
2876 {"org.freedesktop.ManySystems.foo", TRUE},
2877 {"org.freedesktop.ManySystems.foo.bar", TRUE},
2878 {"org.freedesktop.ManySystems2", FALSE},
2879 {"org.freedesktop.ManySystems2.foo", FALSE},
2880 {"org.freedesktop.ManySystems2.foo.bar", FALSE},
2885 while (checks[i].name)
2887 DBusString service_name;
2890 if (!_dbus_string_init (&service_name))
2891 _dbus_assert_not_reached ("couldn't init string");
2892 if (!_dbus_string_append (&service_name, checks[i].name))
2893 _dbus_assert_not_reached ("couldn't append string");
2895 ret = bus_policy_check_can_own (policy, &service_name);
2896 printf (" Check name %s: %s\n", checks[i].name,
2897 ret ? "allowed" : "not allowed");
2898 if (checks[i].allowed && !ret)
2900 _dbus_warn ("Cannot own %s\n", checks[i].name);
2903 if (!checks[i].allowed && ret)
2905 _dbus_warn ("Can own %s\n", checks[i].name);
2908 _dbus_string_free (&service_name);
2917 do_load (const DBusString *full_path,
2919 dbus_bool_t oom_possible,
2920 dbus_bool_t check_own_rules)
2922 BusConfigParser *parser;
2925 dbus_error_init (&error);
2927 parser = bus_config_load (full_path, TRUE, NULL, &error);
2928 if (dbus_error_is_set (&error))
2929 _dbus_verbose ("Failed to load file: %s\n", error.message);
2932 _DBUS_ASSERT_ERROR_IS_SET (&error);
2935 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2937 _dbus_verbose ("Failed to load valid file due to OOM\n");
2938 dbus_error_free (&error);
2941 else if (validity == VALID)
2943 _dbus_warn ("Failed to load valid file but still had memory: %s\n",
2946 dbus_error_free (&error);
2951 dbus_error_free (&error);
2957 _DBUS_ASSERT_ERROR_IS_CLEAR (&error);
2959 if (check_own_rules && do_check_own_rules (parser->policy) == FALSE)
2964 bus_config_parser_unref (parser);
2966 if (validity == INVALID)
2968 _dbus_warn ("Accepted invalid file\n");
2978 const DBusString *full_path;
2980 dbus_bool_t check_own_rules;
2984 check_loader_oom_func (void *data)
2986 LoaderOomData *d = data;
2988 return do_load (d->full_path, d->validity, TRUE, d->check_own_rules);
2992 process_test_valid_subdir (const DBusString *test_base_dir,
2996 DBusString test_directory;
2997 DBusString filename;
3005 if (!_dbus_string_init (&test_directory))
3006 _dbus_assert_not_reached ("didn't allocate test_directory\n");
3008 _dbus_string_init_const (&filename, subdir);
3010 if (!_dbus_string_copy (test_base_dir, 0,
3011 &test_directory, 0))
3012 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
3014 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
3015 _dbus_assert_not_reached ("couldn't allocate full path");
3017 _dbus_string_free (&filename);
3018 if (!_dbus_string_init (&filename))
3019 _dbus_assert_not_reached ("didn't allocate filename string\n");
3021 dbus_error_init (&error);
3022 dir = _dbus_directory_open (&test_directory, &error);
3025 _dbus_warn ("Could not open %s: %s\n",
3026 _dbus_string_get_const_data (&test_directory),
3028 dbus_error_free (&error);
3032 if (validity == VALID)
3033 printf ("Testing valid files:\n");
3034 else if (validity == INVALID)
3035 printf ("Testing invalid files:\n");
3037 printf ("Testing unknown files:\n");
3040 while (_dbus_directory_get_next_file (dir, &filename, &error))
3042 DBusString full_path;
3045 if (!_dbus_string_init (&full_path))
3046 _dbus_assert_not_reached ("couldn't init string");
3048 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
3049 _dbus_assert_not_reached ("couldn't copy dir to full_path");
3051 if (!_dbus_concat_dir_and_file (&full_path, &filename))
3052 _dbus_assert_not_reached ("couldn't concat file to dir");
3054 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
3056 _dbus_verbose ("Skipping non-.conf file %s\n",
3057 _dbus_string_get_const_data (&filename));
3058 _dbus_string_free (&full_path);
3062 printf (" %s\n", _dbus_string_get_const_data (&filename));
3064 _dbus_verbose (" expecting %s\n",
3065 validity == VALID ? "valid" :
3066 (validity == INVALID ? "invalid" :
3067 (validity == UNKNOWN ? "unknown" : "???")));
3069 d.full_path = &full_path;
3070 d.validity = validity;
3071 d.check_own_rules = _dbus_string_ends_with_c_str (&full_path,
3072 "check-own-rules.conf");
3074 /* FIXME hackaround for an expat problem, see
3075 * https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=124747
3076 * http://freedesktop.org/pipermail/dbus/2004-May/001153.html
3078 /* if (!_dbus_test_oom_handling ("config-loader", check_loader_oom_func, &d)) */
3079 if (!check_loader_oom_func (&d))
3080 _dbus_assert_not_reached ("test failed");
3082 _dbus_string_free (&full_path);
3085 if (dbus_error_is_set (&error))
3087 _dbus_warn ("Could not get next file in %s: %s\n",
3088 _dbus_string_get_const_data (&test_directory),
3090 dbus_error_free (&error);
3099 _dbus_directory_close (dir);
3100 _dbus_string_free (&test_directory);
3101 _dbus_string_free (&filename);
3107 bools_equal (dbus_bool_t a,
3114 strings_equal_or_both_null (const char *a,
3117 if (a == NULL || b == NULL)
3120 return !strcmp (a, b);
3124 elements_equal (const Element *a,
3127 if (a->type != b->type)
3130 if (!bools_equal (a->had_content, b->had_content))
3136 case ELEMENT_INCLUDE:
3137 if (!bools_equal (a->d.include.ignore_missing,
3138 b->d.include.ignore_missing))
3142 case ELEMENT_POLICY:
3143 if (a->d.policy.type != b->d.policy.type)
3145 if (a->d.policy.gid_uid_or_at_console != b->d.policy.gid_uid_or_at_console)
3150 if (strcmp (a->d.limit.name, b->d.limit.name))
3152 if (a->d.limit.value != b->d.limit.value)
3166 lists_of_elements_equal (DBusList *a,
3175 while (ia != NULL && ib != NULL)
3177 if (elements_equal (ia->data, ib->data))
3179 ia = _dbus_list_get_next_link (&a, ia);
3180 ib = _dbus_list_get_next_link (&b, ib);
3183 return ia == NULL && ib == NULL;
3187 lists_of_c_strings_equal (DBusList *a,
3196 while (ia != NULL && ib != NULL)
3198 if (strcmp (ia->data, ib->data))
3200 ia = _dbus_list_get_next_link (&a, ia);
3201 ib = _dbus_list_get_next_link (&b, ib);
3204 return ia == NULL && ib == NULL;
3208 limits_equal (const BusLimits *a,
3212 (a->max_incoming_bytes == b->max_incoming_bytes
3213 || a->max_incoming_unix_fds == b->max_incoming_unix_fds
3214 || a->max_outgoing_bytes == b->max_outgoing_bytes
3215 || a->max_outgoing_unix_fds == b->max_outgoing_unix_fds
3216 || a->max_message_size == b->max_message_size
3217 || a->max_message_unix_fds == b->max_message_unix_fds
3218 || a->activation_timeout == b->activation_timeout
3219 || a->auth_timeout == b->auth_timeout
3220 || a->pending_fd_timeout == b->pending_fd_timeout
3221 || a->max_completed_connections == b->max_completed_connections
3222 || a->max_incomplete_connections == b->max_incomplete_connections
3223 || a->max_connections_per_user == b->max_connections_per_user
3224 || a->max_pending_activations == b->max_pending_activations
3225 || a->max_services_per_connection == b->max_services_per_connection
3226 || a->max_match_rules_per_connection == b->max_match_rules_per_connection
3227 || a->max_replies_per_connection == b->max_replies_per_connection
3228 || a->reply_timeout == b->reply_timeout);
3232 config_parsers_equal (const BusConfigParser *a,
3233 const BusConfigParser *b)
3235 if (!_dbus_string_equal (&a->basedir, &b->basedir))
3238 if (!lists_of_elements_equal (a->stack, b->stack))
3241 if (!strings_equal_or_both_null (a->user, b->user))
3244 if (!lists_of_c_strings_equal (a->listen_on, b->listen_on))
3247 if (!lists_of_c_strings_equal (a->mechanisms, b->mechanisms))
3250 if (!lists_of_c_strings_equal (a->service_dirs, b->service_dirs))
3253 /* FIXME: compare policy */
3255 /* FIXME: compare service selinux ID table */
3257 if (! limits_equal (&a->limits, &b->limits))
3260 if (!strings_equal_or_both_null (a->pidfile, b->pidfile))
3263 if (! bools_equal (a->fork, b->fork))
3266 if (! bools_equal (a->keep_umask, b->keep_umask))
3269 if (! bools_equal (a->is_toplevel, b->is_toplevel))
3276 all_are_equiv (const DBusString *target_directory)
3278 DBusString filename;
3280 BusConfigParser *first_parser;
3281 BusConfigParser *parser;
3287 first_parser = NULL;
3291 if (!_dbus_string_init (&filename))
3292 _dbus_assert_not_reached ("didn't allocate filename string");
3294 dbus_error_init (&error);
3295 dir = _dbus_directory_open (target_directory, &error);
3298 _dbus_warn ("Could not open %s: %s\n",
3299 _dbus_string_get_const_data (target_directory),
3301 dbus_error_free (&error);
3305 printf ("Comparing equivalent files:\n");
3308 while (_dbus_directory_get_next_file (dir, &filename, &error))
3310 DBusString full_path;
3312 if (!_dbus_string_init (&full_path))
3313 _dbus_assert_not_reached ("couldn't init string");
3315 if (!_dbus_string_copy (target_directory, 0, &full_path, 0))
3316 _dbus_assert_not_reached ("couldn't copy dir to full_path");
3318 if (!_dbus_concat_dir_and_file (&full_path, &filename))
3319 _dbus_assert_not_reached ("couldn't concat file to dir");
3321 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
3323 _dbus_verbose ("Skipping non-.conf file %s\n",
3324 _dbus_string_get_const_data (&filename));
3325 _dbus_string_free (&full_path);
3329 printf (" %s\n", _dbus_string_get_const_data (&filename));
3331 parser = bus_config_load (&full_path, TRUE, NULL, &error);
3335 _dbus_warn ("Could not load file %s: %s\n",
3336 _dbus_string_get_const_data (&full_path),
3338 _dbus_string_free (&full_path);
3339 dbus_error_free (&error);
3342 else if (first_parser == NULL)
3344 _dbus_string_free (&full_path);
3345 first_parser = parser;
3349 _dbus_string_free (&full_path);
3350 equal = config_parsers_equal (first_parser, parser);
3351 bus_config_parser_unref (parser);
3360 _dbus_string_free (&filename);
3362 bus_config_parser_unref (first_parser);
3364 _dbus_directory_close (dir);
3371 process_test_equiv_subdir (const DBusString *test_base_dir,
3374 DBusString test_directory;
3375 DBusString filename;
3384 if (!_dbus_string_init (&test_directory))
3385 _dbus_assert_not_reached ("didn't allocate test_directory");
3387 _dbus_string_init_const (&filename, subdir);
3389 if (!_dbus_string_copy (test_base_dir, 0,
3390 &test_directory, 0))
3391 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
3393 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
3394 _dbus_assert_not_reached ("couldn't allocate full path");
3396 _dbus_string_free (&filename);
3397 if (!_dbus_string_init (&filename))
3398 _dbus_assert_not_reached ("didn't allocate filename string");
3400 dbus_error_init (&error);
3401 dir = _dbus_directory_open (&test_directory, &error);
3404 _dbus_warn ("Could not open %s: %s\n",
3405 _dbus_string_get_const_data (&test_directory),
3407 dbus_error_free (&error);
3411 while (_dbus_directory_get_next_file (dir, &filename, &error))
3413 DBusString full_path;
3415 /* Skip CVS's magic directories! */
3416 if (_dbus_string_equal_c_str (&filename, "CVS"))
3419 if (!_dbus_string_init (&full_path))
3420 _dbus_assert_not_reached ("couldn't init string");
3422 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
3423 _dbus_assert_not_reached ("couldn't copy dir to full_path");
3425 if (!_dbus_concat_dir_and_file (&full_path, &filename))
3426 _dbus_assert_not_reached ("couldn't concat file to dir");
3428 equal = all_are_equiv (&full_path);
3429 _dbus_string_free (&full_path);
3438 _dbus_string_free (&test_directory);
3439 _dbus_string_free (&filename);
3441 _dbus_directory_close (dir);
3447 static const char *test_session_service_dir_matches[] =
3450 "/testhome/foo/.testlocal/testshare/dbus-1/services",
3451 "/testusr/testlocal/testshare/dbus-1/services",
3452 "/testusr/testshare/dbus-1/services",
3453 DBUS_DATADIR"/dbus-1/services",
3455 /* will be filled in test_default_session_servicedirs() */
3464 test_default_session_servicedirs (void)
3469 DBusString install_root_based;
3471 dbus_bool_t ret = FALSE;
3474 const char *common_progs;
3477 /* On Unix we don't actually use these, but it's easier to handle the
3478 * deallocation if we always allocate them, whether needed or not */
3479 if (!_dbus_string_init (&progs) ||
3480 !_dbus_string_init (&install_root_based))
3481 _dbus_assert_not_reached ("OOM allocating strings");
3484 if (!_dbus_string_append (&install_root_based, DBUS_DATADIR) ||
3485 !_dbus_string_append (&install_root_based, "/dbus-1/services"))
3488 tmp = _dbus_replace_install_prefix (
3489 _dbus_string_get_const_data (&install_root_based));
3492 !_dbus_string_set_length (&install_root_based, 0) ||
3493 !_dbus_string_append (&install_root_based, tmp))
3496 test_session_service_dir_matches[0] = _dbus_string_get_const_data (
3497 &install_root_based);
3499 common_progs = _dbus_getenv ("CommonProgramFiles");
3503 if (!_dbus_string_append (&progs, common_progs))
3506 if (!_dbus_string_append (&progs, "/dbus-1/services"))
3509 test_session_service_dir_matches[1] = _dbus_string_get_const_data(&progs);
3514 printf ("Testing retrieving the default session service directories\n");
3515 if (!_dbus_get_standard_session_servicedirs (&dirs))
3516 _dbus_assert_not_reached ("couldn't get stardard dirs");
3518 /* make sure our defaults end with share/dbus-1/service */
3519 while ((link = _dbus_list_pop_first_link (&dirs)))
3523 printf (" default service dir: %s\n", (char *)link->data);
3524 _dbus_string_init_const (&path, (char *)link->data);
3525 if (!_dbus_string_ends_with_c_str (&path, "dbus-1/services"))
3527 printf ("error with default session service directories\n");
3528 dbus_free (link->data);
3529 _dbus_list_free_link (link);
3533 dbus_free (link->data);
3534 _dbus_list_free_link (link);
3538 if (!dbus_setenv ("XDG_DATA_HOME", "/testhome/foo/.testlocal/testshare"))
3539 _dbus_assert_not_reached ("couldn't setenv XDG_DATA_HOME");
3541 if (!dbus_setenv ("XDG_DATA_DIRS", ":/testusr/testlocal/testshare: :/testusr/testshare:"))
3542 _dbus_assert_not_reached ("couldn't setenv XDG_DATA_DIRS");
3544 if (!_dbus_get_standard_session_servicedirs (&dirs))
3545 _dbus_assert_not_reached ("couldn't get stardard dirs");
3547 /* make sure we read and parse the env variable correctly */
3549 while ((link = _dbus_list_pop_first_link (&dirs)))
3551 printf (" test service dir: %s\n", (char *)link->data);
3552 if (test_session_service_dir_matches[i] == NULL)
3554 printf ("more directories parsed than in match set\n");
3555 dbus_free (link->data);
3556 _dbus_list_free_link (link);
3560 if (strcmp (test_session_service_dir_matches[i],
3561 (char *)link->data) != 0)
3563 printf ("%s directory does not match %s in the match set\n",
3565 test_session_service_dir_matches[i]);
3566 dbus_free (link->data);
3567 _dbus_list_free_link (link);
3573 dbus_free (link->data);
3574 _dbus_list_free_link (link);
3577 if (test_session_service_dir_matches[i] != NULL)
3579 printf ("extra data %s in the match set was not matched\n",
3580 test_session_service_dir_matches[i]);
3587 _dbus_string_free (&install_root_based);
3588 _dbus_string_free (&progs);
3592 static const char *test_system_service_dir_matches[] =
3595 "/usr/local/share/dbus-1/system-services",
3596 "/usr/share/dbus-1/system-services",
3598 DBUS_DATADIR"/dbus-1/system-services",
3600 "/lib/dbus-1/system-services",
3610 test_default_system_servicedirs (void)
3616 const char *common_progs;
3620 /* On Unix we don't actually use this variable, but it's easier to handle the
3621 * deallocation if we always allocate it, whether needed or not */
3622 if (!_dbus_string_init (&progs))
3623 _dbus_assert_not_reached ("OOM allocating progs");
3626 common_progs = _dbus_getenv ("CommonProgramFiles");
3630 if (!_dbus_string_append (&progs, common_progs))
3632 _dbus_string_free (&progs);
3636 if (!_dbus_string_append (&progs, "/dbus-1/system-services"))
3638 _dbus_string_free (&progs);
3641 test_system_service_dir_matches[1] = _dbus_string_get_const_data(&progs);
3646 printf ("Testing retrieving the default system service directories\n");
3647 if (!_dbus_get_standard_system_servicedirs (&dirs))
3648 _dbus_assert_not_reached ("couldn't get stardard dirs");
3650 /* make sure our defaults end with share/dbus-1/system-service */
3651 while ((link = _dbus_list_pop_first_link (&dirs)))
3655 printf (" default service dir: %s\n", (char *)link->data);
3656 _dbus_string_init_const (&path, (char *)link->data);
3657 if (!_dbus_string_ends_with_c_str (&path, "dbus-1/system-services"))
3659 printf ("error with default system service directories\n");
3660 dbus_free (link->data);
3661 _dbus_list_free_link (link);
3662 _dbus_string_free (&progs);
3666 dbus_free (link->data);
3667 _dbus_list_free_link (link);
3671 if (!dbus_setenv ("XDG_DATA_HOME", "/testhome/foo/.testlocal/testshare"))
3672 _dbus_assert_not_reached ("couldn't setenv XDG_DATA_HOME");
3674 if (!dbus_setenv ("XDG_DATA_DIRS", ":/testusr/testlocal/testshare: :/testusr/testshare:"))
3675 _dbus_assert_not_reached ("couldn't setenv XDG_DATA_DIRS");
3677 if (!_dbus_get_standard_system_servicedirs (&dirs))
3678 _dbus_assert_not_reached ("couldn't get stardard dirs");
3680 /* make sure we read and parse the env variable correctly */
3682 while ((link = _dbus_list_pop_first_link (&dirs)))
3684 printf (" test service dir: %s\n", (char *)link->data);
3685 if (test_system_service_dir_matches[i] == NULL)
3687 printf ("more directories parsed than in match set\n");
3688 dbus_free (link->data);
3689 _dbus_list_free_link (link);
3690 _dbus_string_free (&progs);
3694 if (strcmp (test_system_service_dir_matches[i],
3695 (char *)link->data) != 0)
3697 printf ("%s directory does not match %s in the match set\n",
3699 test_system_service_dir_matches[i]);
3700 dbus_free (link->data);
3701 _dbus_list_free_link (link);
3702 _dbus_string_free (&progs);
3708 dbus_free (link->data);
3709 _dbus_list_free_link (link);
3712 if (test_system_service_dir_matches[i] != NULL)
3714 printf ("extra data %s in the match set was not matched\n",
3715 test_system_service_dir_matches[i]);
3717 _dbus_string_free (&progs);
3721 _dbus_string_free (&progs);
3726 bus_config_parser_test (const DBusString *test_data_dir)
3728 if (test_data_dir == NULL ||
3729 _dbus_string_get_length (test_data_dir) == 0)
3731 printf ("No test data\n");
3735 if (!test_default_session_servicedirs())
3739 printf("default system service dir skipped\n");
3741 if (!test_default_system_servicedirs())
3745 if (!process_test_valid_subdir (test_data_dir, "valid-config-files", VALID))
3749 if (!process_test_valid_subdir (test_data_dir, "valid-config-files-system", VALID))
3753 if (!process_test_valid_subdir (test_data_dir, "invalid-config-files", INVALID))
3756 if (!process_test_equiv_subdir (test_data_dir, "equiv-config-files"))
3762 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */